gdb: remove TYPE_FIELD_BITPOS
[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 struct file_and_directory;
1210 static void dwarf_decode_lines (struct line_header *,
1211 const file_and_directory &,
1212 struct dwarf2_cu *, dwarf2_psymtab *,
1213 CORE_ADDR, int decode_mapping);
1214
1215 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1216 const char *);
1217
1218 static struct symbol *new_symbol (struct die_info *, struct type *,
1219 struct dwarf2_cu *, struct symbol * = NULL);
1220
1221 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1222 struct dwarf2_cu *);
1223
1224 static void dwarf2_const_value_attr (const struct attribute *attr,
1225 struct type *type,
1226 const char *name,
1227 struct obstack *obstack,
1228 struct dwarf2_cu *cu, LONGEST *value,
1229 const gdb_byte **bytes,
1230 struct dwarf2_locexpr_baton **baton);
1231
1232 static struct type *read_subrange_index_type (struct die_info *die,
1233 struct dwarf2_cu *cu);
1234
1235 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1236
1237 static int need_gnat_info (struct dwarf2_cu *);
1238
1239 static struct type *die_descriptive_type (struct die_info *,
1240 struct dwarf2_cu *);
1241
1242 static void set_descriptive_type (struct type *, struct die_info *,
1243 struct dwarf2_cu *);
1244
1245 static struct type *die_containing_type (struct die_info *,
1246 struct dwarf2_cu *);
1247
1248 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1249 struct dwarf2_cu *);
1250
1251 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1252
1253 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1254
1255 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1256
1257 static char *typename_concat (struct obstack *obs, const char *prefix,
1258 const char *suffix, int physname,
1259 struct dwarf2_cu *cu);
1260
1261 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1262
1263 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1264
1265 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1266
1267 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1268
1269 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1270
1271 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1272
1273 /* Return the .debug_loclists section to use for cu. */
1274 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1275
1276 /* Return the .debug_rnglists section to use for cu. */
1277 static struct dwarf2_section_info *cu_debug_rnglists_section
1278 (struct dwarf2_cu *cu, dwarf_tag tag);
1279
1280 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1281 values. Keep the items ordered with increasing constraints compliance. */
1282 enum pc_bounds_kind
1283 {
1284 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1285 PC_BOUNDS_NOT_PRESENT,
1286
1287 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1288 were present but they do not form a valid range of PC addresses. */
1289 PC_BOUNDS_INVALID,
1290
1291 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1292 PC_BOUNDS_RANGES,
1293
1294 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1295 PC_BOUNDS_HIGH_LOW,
1296 };
1297
1298 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1299 CORE_ADDR *, CORE_ADDR *,
1300 struct dwarf2_cu *,
1301 dwarf2_psymtab *);
1302
1303 static void get_scope_pc_bounds (struct die_info *,
1304 CORE_ADDR *, CORE_ADDR *,
1305 struct dwarf2_cu *);
1306
1307 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1308 CORE_ADDR, struct dwarf2_cu *);
1309
1310 static void dwarf2_add_field (struct field_info *, struct die_info *,
1311 struct dwarf2_cu *);
1312
1313 static void dwarf2_attach_fields_to_type (struct field_info *,
1314 struct type *, struct dwarf2_cu *);
1315
1316 static void dwarf2_add_member_fn (struct field_info *,
1317 struct die_info *, struct type *,
1318 struct dwarf2_cu *);
1319
1320 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1321 struct type *,
1322 struct dwarf2_cu *);
1323
1324 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1325
1326 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1327
1328 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1329
1330 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1331
1332 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1333
1334 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1335
1336 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1337
1338 static struct type *read_module_type (struct die_info *die,
1339 struct dwarf2_cu *cu);
1340
1341 static const char *namespace_name (struct die_info *die,
1342 int *is_anonymous, struct dwarf2_cu *);
1343
1344 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1345
1346 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1347 bool * = nullptr);
1348
1349 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1350 struct dwarf2_cu *);
1351
1352 static struct die_info *read_die_and_siblings_1
1353 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1354 struct die_info *);
1355
1356 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1357 const gdb_byte *info_ptr,
1358 const gdb_byte **new_info_ptr,
1359 struct die_info *parent);
1360
1361 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1362 struct die_info **, const gdb_byte *,
1363 int);
1364
1365 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1366 struct die_info **, const gdb_byte *);
1367
1368 static void process_die (struct die_info *, struct dwarf2_cu *);
1369
1370 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1371 struct objfile *);
1372
1373 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1374
1375 static const char *dwarf2_full_name (const char *name,
1376 struct die_info *die,
1377 struct dwarf2_cu *cu);
1378
1379 static const char *dwarf2_physname (const char *name, struct die_info *die,
1380 struct dwarf2_cu *cu);
1381
1382 static struct die_info *dwarf2_extension (struct die_info *die,
1383 struct dwarf2_cu **);
1384
1385 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1386
1387 static void dump_die_for_error (struct die_info *);
1388
1389 static void dump_die_1 (struct ui_file *, int level, int max_level,
1390 struct die_info *);
1391
1392 /*static*/ void dump_die (struct die_info *, int max_level);
1393
1394 static void store_in_ref_table (struct die_info *,
1395 struct dwarf2_cu *);
1396
1397 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1398 const struct attribute *,
1399 struct dwarf2_cu **);
1400
1401 static struct die_info *follow_die_ref (struct die_info *,
1402 const struct attribute *,
1403 struct dwarf2_cu **);
1404
1405 static struct die_info *follow_die_sig (struct die_info *,
1406 const struct attribute *,
1407 struct dwarf2_cu **);
1408
1409 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1410 struct dwarf2_cu *);
1411
1412 static struct type *get_DW_AT_signature_type (struct die_info *,
1413 const struct attribute *,
1414 struct dwarf2_cu *);
1415
1416 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1417 dwarf2_per_objfile *per_objfile);
1418
1419 static void read_signatured_type (signatured_type *sig_type,
1420 dwarf2_per_objfile *per_objfile);
1421
1422 static int attr_to_dynamic_prop (const struct attribute *attr,
1423 struct die_info *die, struct dwarf2_cu *cu,
1424 struct dynamic_prop *prop, struct type *type);
1425
1426 /* memory allocation interface */
1427
1428 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1429
1430 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1431
1432 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1433
1434 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1435 struct dwarf2_loclist_baton *baton,
1436 const struct attribute *attr);
1437
1438 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1439 struct symbol *sym,
1440 struct dwarf2_cu *cu,
1441 int is_block);
1442
1443 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1444 const gdb_byte *info_ptr,
1445 const struct abbrev_info *abbrev);
1446
1447 static hashval_t partial_die_hash (const void *item);
1448
1449 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1450
1451 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1452 (sect_offset sect_off, unsigned int offset_in_dwz,
1453 dwarf2_per_objfile *per_objfile);
1454
1455 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1456 struct die_info *comp_unit_die,
1457 enum language pretend_language);
1458
1459 static struct type *set_die_type (struct die_info *, struct type *,
1460 struct dwarf2_cu *, bool = false);
1461
1462 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1463
1464 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1465 dwarf2_per_objfile *per_objfile,
1466 dwarf2_cu *existing_cu,
1467 bool skip_partial,
1468 enum language pretend_language);
1469
1470 static void process_full_comp_unit (dwarf2_cu *cu,
1471 enum language pretend_language);
1472
1473 static void process_full_type_unit (dwarf2_cu *cu,
1474 enum language pretend_language);
1475
1476 static struct type *get_die_type_at_offset (sect_offset,
1477 dwarf2_per_cu_data *per_cu,
1478 dwarf2_per_objfile *per_objfile);
1479
1480 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1481
1482 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1483 dwarf2_per_objfile *per_objfile,
1484 enum language pretend_language);
1485
1486 static void process_queue (dwarf2_per_objfile *per_objfile);
1487
1488 /* Class, the destructor of which frees all allocated queue entries. This
1489 will only have work to do if an error was thrown while processing the
1490 dwarf. If no error was thrown then the queue entries should have all
1491 been processed, and freed, as we went along. */
1492
1493 class dwarf2_queue_guard
1494 {
1495 public:
1496 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1497 : m_per_objfile (per_objfile)
1498 {
1499 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1500
1501 m_per_objfile->per_bfd->queue.emplace ();
1502 }
1503
1504 /* Free any entries remaining on the queue. There should only be
1505 entries left if we hit an error while processing the dwarf. */
1506 ~dwarf2_queue_guard ()
1507 {
1508 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1509
1510 m_per_objfile->per_bfd->queue.reset ();
1511 }
1512
1513 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1514
1515 private:
1516 dwarf2_per_objfile *m_per_objfile;
1517 };
1518
1519 dwarf2_queue_item::~dwarf2_queue_item ()
1520 {
1521 /* Anything still marked queued is likely to be in an
1522 inconsistent state, so discard it. */
1523 if (per_cu->queued)
1524 {
1525 per_objfile->remove_cu (per_cu);
1526 per_cu->queued = 0;
1527 }
1528 }
1529
1530 /* See dwarf2/read.h. */
1531
1532 void
1533 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1534 {
1535 if (data->is_debug_types)
1536 delete static_cast<signatured_type *> (data);
1537 else
1538 delete data;
1539 }
1540
1541 /* The return type of find_file_and_directory. Note, the enclosed
1542 string pointers are only valid while this object is valid. */
1543
1544 struct file_and_directory
1545 {
1546 /* The filename. This is never NULL. */
1547 const char *name;
1548
1549 /* The compilation directory. NULL if not known. If we needed to
1550 compute a new string, it will be stored in the per-BFD string
1551 bcache; otherwise, points directly to the DW_AT_comp_dir string
1552 attribute owned by the obstack that owns the DIE. */
1553 const char *comp_dir;
1554 };
1555
1556 static file_and_directory find_file_and_directory (struct die_info *die,
1557 struct dwarf2_cu *cu);
1558
1559 static const char *compute_include_file_name
1560 (const struct line_header *lh,
1561 const file_entry &fe,
1562 const file_and_directory &cu_info,
1563 gdb::unique_xmalloc_ptr<char> *name_holder);
1564
1565 static htab_up allocate_signatured_type_table ();
1566
1567 static htab_up allocate_dwo_unit_table ();
1568
1569 static struct dwo_unit *lookup_dwo_unit_in_dwp
1570 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1571 const char *comp_dir, ULONGEST signature, int is_debug_types);
1572
1573 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1574
1575 static struct dwo_unit *lookup_dwo_comp_unit
1576 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1577 ULONGEST signature);
1578
1579 static struct dwo_unit *lookup_dwo_type_unit
1580 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1581
1582 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1583
1584 /* A unique pointer to a dwo_file. */
1585
1586 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1587
1588 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1589
1590 static void check_producer (struct dwarf2_cu *cu);
1591 \f
1592 /* Various complaints about symbol reading that don't abort the process. */
1593
1594 static void
1595 dwarf2_debug_line_missing_file_complaint (void)
1596 {
1597 complaint (_(".debug_line section has line data without a file"));
1598 }
1599
1600 static void
1601 dwarf2_debug_line_missing_end_sequence_complaint (void)
1602 {
1603 complaint (_(".debug_line section has line "
1604 "program sequence without an end"));
1605 }
1606
1607 static void
1608 dwarf2_complex_location_expr_complaint (void)
1609 {
1610 complaint (_("location expression too complex"));
1611 }
1612
1613 static void
1614 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1615 int arg3)
1616 {
1617 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1618 arg1, arg2, arg3);
1619 }
1620
1621 static void
1622 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1623 {
1624 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1625 arg1, arg2);
1626 }
1627
1628 /* Hash function for line_header_hash. */
1629
1630 static hashval_t
1631 line_header_hash (const struct line_header *ofs)
1632 {
1633 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1634 }
1635
1636 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1637
1638 static hashval_t
1639 line_header_hash_voidp (const void *item)
1640 {
1641 const struct line_header *ofs = (const struct line_header *) item;
1642
1643 return line_header_hash (ofs);
1644 }
1645
1646 /* Equality function for line_header_hash. */
1647
1648 static int
1649 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1650 {
1651 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1652 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1653
1654 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1655 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1656 }
1657
1658 \f
1659
1660 /* An iterator for all_comp_units that is based on index. This
1661 approach makes it possible to iterate over all_comp_units safely,
1662 when some caller in the loop may add new units. */
1663
1664 class all_comp_units_iterator
1665 {
1666 public:
1667
1668 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1669 : m_per_bfd (per_bfd),
1670 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1671 {
1672 }
1673
1674 all_comp_units_iterator &operator++ ()
1675 {
1676 ++m_index;
1677 return *this;
1678 }
1679
1680 dwarf2_per_cu_data *operator* () const
1681 {
1682 return m_per_bfd->get_cu (m_index);
1683 }
1684
1685 bool operator== (const all_comp_units_iterator &other) const
1686 {
1687 return m_index == other.m_index;
1688 }
1689
1690
1691 bool operator!= (const all_comp_units_iterator &other) const
1692 {
1693 return m_index != other.m_index;
1694 }
1695
1696 private:
1697
1698 dwarf2_per_bfd *m_per_bfd;
1699 size_t m_index;
1700 };
1701
1702 /* A range adapter for the all_comp_units_iterator. */
1703 class all_comp_units_range
1704 {
1705 public:
1706
1707 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1708 : m_per_bfd (per_bfd)
1709 {
1710 }
1711
1712 all_comp_units_iterator begin ()
1713 {
1714 return all_comp_units_iterator (m_per_bfd, true);
1715 }
1716
1717 all_comp_units_iterator end ()
1718 {
1719 return all_comp_units_iterator (m_per_bfd, false);
1720 }
1721
1722 private:
1723
1724 dwarf2_per_bfd *m_per_bfd;
1725 };
1726
1727 /* See declaration. */
1728
1729 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1730 bool can_copy_)
1731 : obfd (obfd),
1732 can_copy (can_copy_)
1733 {
1734 if (names == NULL)
1735 names = &dwarf2_elf_names;
1736
1737 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1738 locate_sections (obfd, sec, *names);
1739 }
1740
1741 dwarf2_per_bfd::~dwarf2_per_bfd ()
1742 {
1743 for (auto &per_cu : all_comp_units)
1744 {
1745 per_cu->imported_symtabs_free ();
1746 per_cu->free_cached_file_names ();
1747 }
1748
1749 /* Everything else should be on this->obstack. */
1750 }
1751
1752 /* See read.h. */
1753
1754 void
1755 dwarf2_per_objfile::remove_all_cus ()
1756 {
1757 gdb_assert (!this->per_bfd->queue.has_value ());
1758
1759 for (auto pair : m_dwarf2_cus)
1760 delete pair.second;
1761
1762 m_dwarf2_cus.clear ();
1763 }
1764
1765 /* A helper class that calls free_cached_comp_units on
1766 destruction. */
1767
1768 class free_cached_comp_units
1769 {
1770 public:
1771
1772 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1773 : m_per_objfile (per_objfile)
1774 {
1775 }
1776
1777 ~free_cached_comp_units ()
1778 {
1779 m_per_objfile->remove_all_cus ();
1780 }
1781
1782 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1783
1784 private:
1785
1786 dwarf2_per_objfile *m_per_objfile;
1787 };
1788
1789 /* See read.h. */
1790
1791 bool
1792 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1793 {
1794 if (per_cu->index < this->m_symtabs.size ())
1795 return this->m_symtabs[per_cu->index] != nullptr;
1796 return false;
1797 }
1798
1799 /* See read.h. */
1800
1801 compunit_symtab *
1802 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1803 {
1804 if (per_cu->index < this->m_symtabs.size ())
1805 return this->m_symtabs[per_cu->index];
1806 return nullptr;
1807 }
1808
1809 /* See read.h. */
1810
1811 void
1812 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1813 compunit_symtab *symtab)
1814 {
1815 if (per_cu->index >= this->m_symtabs.size ())
1816 this->m_symtabs.resize (per_cu->index + 1);
1817 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1818 this->m_symtabs[per_cu->index] = symtab;
1819 }
1820
1821 /* Try to locate the sections we need for DWARF 2 debugging
1822 information and return true if we have enough to do something.
1823 NAMES points to the dwarf2 section names, or is NULL if the standard
1824 ELF names are used. CAN_COPY is true for formats where symbol
1825 interposition is possible and so symbol values must follow copy
1826 relocation rules. */
1827
1828 int
1829 dwarf2_has_info (struct objfile *objfile,
1830 const struct dwarf2_debug_sections *names,
1831 bool can_copy)
1832 {
1833 if (objfile->flags & OBJF_READNEVER)
1834 return 0;
1835
1836 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1837
1838 if (per_objfile == NULL)
1839 {
1840 dwarf2_per_bfd *per_bfd;
1841
1842 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1843 BFD doesn't require relocations.
1844
1845 We don't share with objfiles for which -readnow was requested,
1846 because it would complicate things when loading the same BFD with
1847 -readnow and then without -readnow. */
1848 if (!gdb_bfd_requires_relocations (objfile->obfd)
1849 && (objfile->flags & OBJF_READNOW) == 0)
1850 {
1851 /* See if one has been created for this BFD yet. */
1852 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1853
1854 if (per_bfd == nullptr)
1855 {
1856 /* No, create it now. */
1857 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1858 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1859 }
1860 }
1861 else
1862 {
1863 /* No sharing possible, create one specifically for this objfile. */
1864 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1865 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1866 }
1867
1868 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1869 }
1870
1871 return (!per_objfile->per_bfd->info.is_virtual
1872 && per_objfile->per_bfd->info.s.section != NULL
1873 && !per_objfile->per_bfd->abbrev.is_virtual
1874 && per_objfile->per_bfd->abbrev.s.section != NULL);
1875 }
1876
1877 /* See declaration. */
1878
1879 void
1880 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1881 const dwarf2_debug_sections &names)
1882 {
1883 flagword aflag = bfd_section_flags (sectp);
1884
1885 if ((aflag & SEC_HAS_CONTENTS) == 0)
1886 {
1887 }
1888 else if (elf_section_data (sectp)->this_hdr.sh_size
1889 > bfd_get_file_size (abfd))
1890 {
1891 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1892 warning (_("Discarding section %s which has a section size (%s"
1893 ") larger than the file size [in module %s]"),
1894 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1895 bfd_get_filename (abfd));
1896 }
1897 else if (names.info.matches (sectp->name))
1898 {
1899 this->info.s.section = sectp;
1900 this->info.size = bfd_section_size (sectp);
1901 }
1902 else if (names.abbrev.matches (sectp->name))
1903 {
1904 this->abbrev.s.section = sectp;
1905 this->abbrev.size = bfd_section_size (sectp);
1906 }
1907 else if (names.line.matches (sectp->name))
1908 {
1909 this->line.s.section = sectp;
1910 this->line.size = bfd_section_size (sectp);
1911 }
1912 else if (names.loc.matches (sectp->name))
1913 {
1914 this->loc.s.section = sectp;
1915 this->loc.size = bfd_section_size (sectp);
1916 }
1917 else if (names.loclists.matches (sectp->name))
1918 {
1919 this->loclists.s.section = sectp;
1920 this->loclists.size = bfd_section_size (sectp);
1921 }
1922 else if (names.macinfo.matches (sectp->name))
1923 {
1924 this->macinfo.s.section = sectp;
1925 this->macinfo.size = bfd_section_size (sectp);
1926 }
1927 else if (names.macro.matches (sectp->name))
1928 {
1929 this->macro.s.section = sectp;
1930 this->macro.size = bfd_section_size (sectp);
1931 }
1932 else if (names.str.matches (sectp->name))
1933 {
1934 this->str.s.section = sectp;
1935 this->str.size = bfd_section_size (sectp);
1936 }
1937 else if (names.str_offsets.matches (sectp->name))
1938 {
1939 this->str_offsets.s.section = sectp;
1940 this->str_offsets.size = bfd_section_size (sectp);
1941 }
1942 else if (names.line_str.matches (sectp->name))
1943 {
1944 this->line_str.s.section = sectp;
1945 this->line_str.size = bfd_section_size (sectp);
1946 }
1947 else if (names.addr.matches (sectp->name))
1948 {
1949 this->addr.s.section = sectp;
1950 this->addr.size = bfd_section_size (sectp);
1951 }
1952 else if (names.frame.matches (sectp->name))
1953 {
1954 this->frame.s.section = sectp;
1955 this->frame.size = bfd_section_size (sectp);
1956 }
1957 else if (names.eh_frame.matches (sectp->name))
1958 {
1959 this->eh_frame.s.section = sectp;
1960 this->eh_frame.size = bfd_section_size (sectp);
1961 }
1962 else if (names.ranges.matches (sectp->name))
1963 {
1964 this->ranges.s.section = sectp;
1965 this->ranges.size = bfd_section_size (sectp);
1966 }
1967 else if (names.rnglists.matches (sectp->name))
1968 {
1969 this->rnglists.s.section = sectp;
1970 this->rnglists.size = bfd_section_size (sectp);
1971 }
1972 else if (names.types.matches (sectp->name))
1973 {
1974 struct dwarf2_section_info type_section;
1975
1976 memset (&type_section, 0, sizeof (type_section));
1977 type_section.s.section = sectp;
1978 type_section.size = bfd_section_size (sectp);
1979
1980 this->types.push_back (type_section);
1981 }
1982 else if (names.gdb_index.matches (sectp->name))
1983 {
1984 this->gdb_index.s.section = sectp;
1985 this->gdb_index.size = bfd_section_size (sectp);
1986 }
1987 else if (names.debug_names.matches (sectp->name))
1988 {
1989 this->debug_names.s.section = sectp;
1990 this->debug_names.size = bfd_section_size (sectp);
1991 }
1992 else if (names.debug_aranges.matches (sectp->name))
1993 {
1994 this->debug_aranges.s.section = sectp;
1995 this->debug_aranges.size = bfd_section_size (sectp);
1996 }
1997
1998 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1999 && bfd_section_vma (sectp) == 0)
2000 this->has_section_at_zero = true;
2001 }
2002
2003 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2004 SECTION_NAME. */
2005
2006 void
2007 dwarf2_get_section_info (struct objfile *objfile,
2008 enum dwarf2_section_enum sect,
2009 asection **sectp, const gdb_byte **bufp,
2010 bfd_size_type *sizep)
2011 {
2012 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2013 struct dwarf2_section_info *info;
2014
2015 /* We may see an objfile without any DWARF, in which case we just
2016 return nothing. */
2017 if (per_objfile == NULL)
2018 {
2019 *sectp = NULL;
2020 *bufp = NULL;
2021 *sizep = 0;
2022 return;
2023 }
2024 switch (sect)
2025 {
2026 case DWARF2_DEBUG_FRAME:
2027 info = &per_objfile->per_bfd->frame;
2028 break;
2029 case DWARF2_EH_FRAME:
2030 info = &per_objfile->per_bfd->eh_frame;
2031 break;
2032 default:
2033 gdb_assert_not_reached ("unexpected section");
2034 }
2035
2036 info->read (objfile);
2037
2038 *sectp = info->get_bfd_section ();
2039 *bufp = info->buffer;
2040 *sizep = info->size;
2041 }
2042
2043 \f
2044 /* DWARF quick_symbol_functions support. */
2045
2046 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2047 unique line tables, so we maintain a separate table of all .debug_line
2048 derived entries to support the sharing.
2049 All the quick functions need is the list of file names. We discard the
2050 line_header when we're done and don't need to record it here. */
2051 struct quick_file_names
2052 {
2053 /* The data used to construct the hash key. */
2054 struct stmt_list_hash hash;
2055
2056 /* The number of entries in file_names, real_names. */
2057 unsigned int num_file_names;
2058
2059 /* The CU directory, as given by DW_AT_comp_dir. May be
2060 nullptr. */
2061 const char *comp_dir;
2062
2063 /* The file names from the line table, after being run through
2064 file_full_name. */
2065 const char **file_names;
2066
2067 /* The file names from the line table after being run through
2068 gdb_realpath. These are computed lazily. */
2069 const char **real_names;
2070 };
2071
2072 /* When using the index (and thus not using psymtabs), each CU has an
2073 object of this type. This is used to hold information needed by
2074 the various "quick" methods. */
2075 struct dwarf2_per_cu_quick_data
2076 {
2077 /* The file table. This can be NULL if there was no file table
2078 or it's currently not read in.
2079 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2080 struct quick_file_names *file_names;
2081
2082 /* A temporary mark bit used when iterating over all CUs in
2083 expand_symtabs_matching. */
2084 unsigned int mark : 1;
2085
2086 /* True if we've tried to read the file table. There will be no
2087 point in trying to read it again next time. */
2088 bool files_read : 1;
2089 };
2090
2091 /* A subclass of psymbol_functions that arranges to read the DWARF
2092 partial symbols when needed. */
2093 struct lazy_dwarf_reader : public psymbol_functions
2094 {
2095 using psymbol_functions::psymbol_functions;
2096
2097 bool can_lazily_read_symbols () override
2098 {
2099 return true;
2100 }
2101
2102 void read_partial_symbols (struct objfile *objfile) override
2103 {
2104 if (dwarf2_has_info (objfile, nullptr))
2105 dwarf2_build_psymtabs (objfile, this);
2106 }
2107 };
2108
2109 static quick_symbol_functions_up
2110 make_lazy_dwarf_reader ()
2111 {
2112 return quick_symbol_functions_up (new lazy_dwarf_reader);
2113 }
2114
2115 struct dwarf2_base_index_functions : public quick_symbol_functions
2116 {
2117 bool has_symbols (struct objfile *objfile) override;
2118
2119 bool has_unexpanded_symtabs (struct objfile *objfile) override;
2120
2121 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2122
2123 void forget_cached_source_info (struct objfile *objfile) override;
2124
2125 enum language lookup_global_symbol_language (struct objfile *objfile,
2126 const char *name,
2127 domain_enum domain,
2128 bool *symbol_found_p) override
2129 {
2130 *symbol_found_p = false;
2131 return language_unknown;
2132 }
2133
2134 void print_stats (struct objfile *objfile, bool print_bcache) override;
2135
2136 void expand_all_symtabs (struct objfile *objfile) override;
2137
2138 struct compunit_symtab *find_pc_sect_compunit_symtab
2139 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2140 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2141
2142 struct compunit_symtab *find_compunit_symtab_by_address
2143 (struct objfile *objfile, CORE_ADDR address) override
2144 {
2145 return nullptr;
2146 }
2147
2148 void map_symbol_filenames (struct objfile *objfile,
2149 gdb::function_view<symbol_filename_ftype> fun,
2150 bool need_fullname) override;
2151 };
2152
2153 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2154 {
2155 void dump (struct objfile *objfile) override;
2156
2157 void expand_matching_symbols
2158 (struct objfile *,
2159 const lookup_name_info &lookup_name,
2160 domain_enum domain,
2161 int global,
2162 symbol_compare_ftype *ordered_compare) override;
2163
2164 bool expand_symtabs_matching
2165 (struct objfile *objfile,
2166 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2167 const lookup_name_info *lookup_name,
2168 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2169 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2170 block_search_flags search_flags,
2171 domain_enum domain,
2172 enum search_domain kind) override;
2173 };
2174
2175 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2176 {
2177 void dump (struct objfile *objfile) override;
2178
2179 void expand_matching_symbols
2180 (struct objfile *,
2181 const lookup_name_info &lookup_name,
2182 domain_enum domain,
2183 int global,
2184 symbol_compare_ftype *ordered_compare) override;
2185
2186 bool expand_symtabs_matching
2187 (struct objfile *objfile,
2188 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2189 const lookup_name_info *lookup_name,
2190 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2191 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2192 block_search_flags search_flags,
2193 domain_enum domain,
2194 enum search_domain kind) override;
2195 };
2196
2197 static quick_symbol_functions_up
2198 make_dwarf_gdb_index ()
2199 {
2200 return quick_symbol_functions_up (new dwarf2_gdb_index);
2201 }
2202
2203 static quick_symbol_functions_up
2204 make_dwarf_debug_names ()
2205 {
2206 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2207 }
2208
2209 /* Utility hash function for a stmt_list_hash. */
2210
2211 static hashval_t
2212 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2213 {
2214 hashval_t v = 0;
2215
2216 if (stmt_list_hash->dwo_unit != NULL)
2217 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2218 v += to_underlying (stmt_list_hash->line_sect_off);
2219 return v;
2220 }
2221
2222 /* Utility equality function for a stmt_list_hash. */
2223
2224 static int
2225 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2226 const struct stmt_list_hash *rhs)
2227 {
2228 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2229 return 0;
2230 if (lhs->dwo_unit != NULL
2231 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2232 return 0;
2233
2234 return lhs->line_sect_off == rhs->line_sect_off;
2235 }
2236
2237 /* Hash function for a quick_file_names. */
2238
2239 static hashval_t
2240 hash_file_name_entry (const void *e)
2241 {
2242 const struct quick_file_names *file_data
2243 = (const struct quick_file_names *) e;
2244
2245 return hash_stmt_list_entry (&file_data->hash);
2246 }
2247
2248 /* Equality function for a quick_file_names. */
2249
2250 static int
2251 eq_file_name_entry (const void *a, const void *b)
2252 {
2253 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2254 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2255
2256 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2257 }
2258
2259 /* Create a quick_file_names hash table. */
2260
2261 static htab_up
2262 create_quick_file_names_table (unsigned int nr_initial_entries)
2263 {
2264 return htab_up (htab_create_alloc (nr_initial_entries,
2265 hash_file_name_entry, eq_file_name_entry,
2266 nullptr, xcalloc, xfree));
2267 }
2268
2269 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2270 function is unrelated to symtabs, symtab would have to be created afterwards.
2271 You should call age_cached_comp_units after processing the CU. */
2272
2273 static dwarf2_cu *
2274 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2275 bool skip_partial)
2276 {
2277 if (per_cu->is_debug_types)
2278 load_full_type_unit (per_cu, per_objfile);
2279 else
2280 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2281 skip_partial, language_minimal);
2282
2283 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2284 if (cu == nullptr)
2285 return nullptr; /* Dummy CU. */
2286
2287 dwarf2_find_base_address (cu->dies, cu);
2288
2289 return cu;
2290 }
2291
2292 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2293
2294 static void
2295 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2296 dwarf2_per_objfile *per_objfile, bool skip_partial)
2297 {
2298 /* Skip type_unit_groups, reading the type units they contain
2299 is handled elsewhere. */
2300 if (per_cu->type_unit_group_p ())
2301 return;
2302
2303 {
2304 /* The destructor of dwarf2_queue_guard frees any entries left on
2305 the queue. After this point we're guaranteed to leave this function
2306 with the dwarf queue empty. */
2307 dwarf2_queue_guard q_guard (per_objfile);
2308
2309 if (!per_objfile->symtab_set_p (per_cu))
2310 {
2311 queue_comp_unit (per_cu, per_objfile, language_minimal);
2312 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2313
2314 /* If we just loaded a CU from a DWO, and we're working with an index
2315 that may badly handle TUs, load all the TUs in that DWO as well.
2316 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2317 if (!per_cu->is_debug_types
2318 && cu != NULL
2319 && cu->dwo_unit != NULL
2320 && per_objfile->per_bfd->index_table != NULL
2321 && per_objfile->per_bfd->index_table->version <= 7
2322 /* DWP files aren't supported yet. */
2323 && get_dwp_file (per_objfile) == NULL)
2324 queue_and_load_all_dwo_tus (cu);
2325 }
2326
2327 process_queue (per_objfile);
2328 }
2329
2330 /* Age the cache, releasing compilation units that have not
2331 been used recently. */
2332 per_objfile->age_comp_units ();
2333 }
2334
2335 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2336 the per-objfile for which this symtab is instantiated.
2337
2338 Returns the resulting symbol table. */
2339
2340 static struct compunit_symtab *
2341 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2342 dwarf2_per_objfile *per_objfile,
2343 bool skip_partial)
2344 {
2345 gdb_assert (per_objfile->per_bfd->using_index);
2346
2347 if (!per_objfile->symtab_set_p (per_cu))
2348 {
2349 free_cached_comp_units freer (per_objfile);
2350 scoped_restore decrementer = increment_reading_symtab ();
2351 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2352 process_cu_includes (per_objfile);
2353 }
2354
2355 return per_objfile->get_symtab (per_cu);
2356 }
2357
2358 /* See read.h. */
2359
2360 dwarf2_per_cu_data_up
2361 dwarf2_per_bfd::allocate_per_cu ()
2362 {
2363 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2364 result->per_bfd = this;
2365 result->index = all_comp_units.size ();
2366 return result;
2367 }
2368
2369 /* See read.h. */
2370
2371 signatured_type_up
2372 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2373 {
2374 signatured_type_up result (new signatured_type (signature));
2375 result->per_bfd = this;
2376 result->index = all_comp_units.size ();
2377 result->is_debug_types = true;
2378 tu_stats.nr_tus++;
2379 return result;
2380 }
2381
2382 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2383 obstack, and constructed with the specified field values. */
2384
2385 static dwarf2_per_cu_data_up
2386 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2387 struct dwarf2_section_info *section,
2388 int is_dwz,
2389 sect_offset sect_off, ULONGEST length)
2390 {
2391 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2392 the_cu->sect_off = sect_off;
2393 the_cu->length = length;
2394 the_cu->section = section;
2395 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2396 struct dwarf2_per_cu_quick_data);
2397 the_cu->is_dwz = is_dwz;
2398 return the_cu;
2399 }
2400
2401 /* A helper for create_cus_from_index that handles a given list of
2402 CUs. */
2403
2404 static void
2405 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2406 const gdb_byte *cu_list, offset_type n_elements,
2407 struct dwarf2_section_info *section,
2408 int is_dwz)
2409 {
2410 for (offset_type i = 0; i < n_elements; i += 2)
2411 {
2412 gdb_static_assert (sizeof (ULONGEST) >= 8);
2413
2414 sect_offset sect_off
2415 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2416 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2417 cu_list += 2 * 8;
2418
2419 dwarf2_per_cu_data_up per_cu
2420 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2421 length);
2422 per_bfd->all_comp_units.push_back (std::move (per_cu));
2423 }
2424 }
2425
2426 /* Read the CU list from the mapped index, and use it to create all
2427 the CU objects for PER_BFD. */
2428
2429 static void
2430 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2431 const gdb_byte *cu_list, offset_type cu_list_elements,
2432 const gdb_byte *dwz_list, offset_type dwz_elements)
2433 {
2434 gdb_assert (per_bfd->all_comp_units.empty ());
2435 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2436
2437 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2438 &per_bfd->info, 0);
2439
2440 if (dwz_elements == 0)
2441 return;
2442
2443 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2444 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2445 &dwz->info, 1);
2446 }
2447
2448 /* Create the signatured type hash table from the index. */
2449
2450 static void
2451 create_signatured_type_table_from_index
2452 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2453 const gdb_byte *bytes, offset_type elements)
2454 {
2455 htab_up sig_types_hash = allocate_signatured_type_table ();
2456
2457 for (offset_type i = 0; i < elements; i += 3)
2458 {
2459 signatured_type_up sig_type;
2460 ULONGEST signature;
2461 void **slot;
2462 cu_offset type_offset_in_tu;
2463
2464 gdb_static_assert (sizeof (ULONGEST) >= 8);
2465 sect_offset sect_off
2466 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2467 type_offset_in_tu
2468 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2469 BFD_ENDIAN_LITTLE);
2470 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2471 bytes += 3 * 8;
2472
2473 sig_type = per_bfd->allocate_signatured_type (signature);
2474 sig_type->type_offset_in_tu = type_offset_in_tu;
2475 sig_type->section = section;
2476 sig_type->sect_off = sect_off;
2477 sig_type->v.quick
2478 = OBSTACK_ZALLOC (&per_bfd->obstack,
2479 struct dwarf2_per_cu_quick_data);
2480
2481 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2482 *slot = sig_type.get ();
2483
2484 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2485 }
2486
2487 per_bfd->signatured_types = std::move (sig_types_hash);
2488 }
2489
2490 /* Create the signatured type hash table from .debug_names. */
2491
2492 static void
2493 create_signatured_type_table_from_debug_names
2494 (dwarf2_per_objfile *per_objfile,
2495 const mapped_debug_names &map,
2496 struct dwarf2_section_info *section,
2497 struct dwarf2_section_info *abbrev_section)
2498 {
2499 struct objfile *objfile = per_objfile->objfile;
2500
2501 section->read (objfile);
2502 abbrev_section->read (objfile);
2503
2504 htab_up sig_types_hash = allocate_signatured_type_table ();
2505
2506 for (uint32_t i = 0; i < map.tu_count; ++i)
2507 {
2508 signatured_type_up sig_type;
2509 void **slot;
2510
2511 sect_offset sect_off
2512 = (sect_offset) (extract_unsigned_integer
2513 (map.tu_table_reordered + i * map.offset_size,
2514 map.offset_size,
2515 map.dwarf5_byte_order));
2516
2517 comp_unit_head cu_header;
2518 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2519 abbrev_section,
2520 section->buffer + to_underlying (sect_off),
2521 rcuh_kind::TYPE);
2522
2523 sig_type = per_objfile->per_bfd->allocate_signatured_type
2524 (cu_header.signature);
2525 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2526 sig_type->section = section;
2527 sig_type->sect_off = sect_off;
2528 sig_type->v.quick
2529 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2530 struct dwarf2_per_cu_quick_data);
2531
2532 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2533 *slot = sig_type.get ();
2534
2535 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2536 }
2537
2538 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2539 }
2540
2541 /* Read the address map data from the mapped index, and use it to
2542 populate the psymtabs_addrmap. */
2543
2544 static void
2545 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2546 struct mapped_index *index)
2547 {
2548 struct objfile *objfile = per_objfile->objfile;
2549 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2550 struct gdbarch *gdbarch = objfile->arch ();
2551 const gdb_byte *iter, *end;
2552 struct addrmap *mutable_map;
2553 CORE_ADDR baseaddr;
2554
2555 auto_obstack temp_obstack;
2556
2557 mutable_map = addrmap_create_mutable (&temp_obstack);
2558
2559 iter = index->address_table.data ();
2560 end = iter + index->address_table.size ();
2561
2562 baseaddr = objfile->text_section_offset ();
2563
2564 while (iter < end)
2565 {
2566 ULONGEST hi, lo, cu_index;
2567 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2568 iter += 8;
2569 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2570 iter += 8;
2571 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2572 iter += 4;
2573
2574 if (lo > hi)
2575 {
2576 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2577 hex_string (lo), hex_string (hi));
2578 continue;
2579 }
2580
2581 if (cu_index >= per_bfd->all_comp_units.size ())
2582 {
2583 complaint (_(".gdb_index address table has invalid CU number %u"),
2584 (unsigned) cu_index);
2585 continue;
2586 }
2587
2588 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2589 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2590 addrmap_set_empty (mutable_map, lo, hi - 1,
2591 per_bfd->get_cu (cu_index));
2592 }
2593
2594 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2595 &per_bfd->obstack);
2596 }
2597
2598 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2599 populate the psymtabs_addrmap. */
2600
2601 static void
2602 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2603 struct dwarf2_section_info *section)
2604 {
2605 struct objfile *objfile = per_objfile->objfile;
2606 bfd *abfd = objfile->obfd;
2607 struct gdbarch *gdbarch = objfile->arch ();
2608 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2609 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2610
2611 auto_obstack temp_obstack;
2612 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2613
2614 std::unordered_map<sect_offset,
2615 dwarf2_per_cu_data *,
2616 gdb::hash_enum<sect_offset>>
2617 debug_info_offset_to_per_cu;
2618 for (const auto &per_cu : per_bfd->all_comp_units)
2619 {
2620 /* A TU will not need aranges, and skipping them here is an easy
2621 way of ignoring .debug_types -- and possibly seeing a
2622 duplicate section offset -- entirely. */
2623 if (per_cu->is_debug_types)
2624 continue;
2625
2626 const auto insertpair
2627 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2628 per_cu.get ());
2629 if (!insertpair.second)
2630 {
2631 warning (_("Section .debug_aranges in %s has duplicate "
2632 "debug_info_offset %s, ignoring .debug_aranges."),
2633 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2634 return;
2635 }
2636 }
2637
2638 section->read (objfile);
2639
2640 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2641
2642 const gdb_byte *addr = section->buffer;
2643
2644 while (addr < section->buffer + section->size)
2645 {
2646 const gdb_byte *const entry_addr = addr;
2647 unsigned int bytes_read;
2648
2649 const LONGEST entry_length = read_initial_length (abfd, addr,
2650 &bytes_read);
2651 addr += bytes_read;
2652
2653 const gdb_byte *const entry_end = addr + entry_length;
2654 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2655 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2656 if (addr + entry_length > section->buffer + section->size)
2657 {
2658 warning (_("Section .debug_aranges in %s entry at offset %s "
2659 "length %s exceeds section length %s, "
2660 "ignoring .debug_aranges."),
2661 objfile_name (objfile),
2662 plongest (entry_addr - section->buffer),
2663 plongest (bytes_read + entry_length),
2664 pulongest (section->size));
2665 return;
2666 }
2667
2668 /* The version number. */
2669 const uint16_t version = read_2_bytes (abfd, addr);
2670 addr += 2;
2671 if (version != 2)
2672 {
2673 warning (_("Section .debug_aranges in %s entry at offset %s "
2674 "has unsupported version %d, ignoring .debug_aranges."),
2675 objfile_name (objfile),
2676 plongest (entry_addr - section->buffer), version);
2677 return;
2678 }
2679
2680 const uint64_t debug_info_offset
2681 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2682 addr += offset_size;
2683 const auto per_cu_it
2684 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2685 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2686 {
2687 warning (_("Section .debug_aranges in %s entry at offset %s "
2688 "debug_info_offset %s does not exists, "
2689 "ignoring .debug_aranges."),
2690 objfile_name (objfile),
2691 plongest (entry_addr - section->buffer),
2692 pulongest (debug_info_offset));
2693 return;
2694 }
2695 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2696
2697 const uint8_t address_size = *addr++;
2698 if (address_size < 1 || address_size > 8)
2699 {
2700 warning (_("Section .debug_aranges in %s entry at offset %s "
2701 "address_size %u is invalid, ignoring .debug_aranges."),
2702 objfile_name (objfile),
2703 plongest (entry_addr - section->buffer), address_size);
2704 return;
2705 }
2706
2707 const uint8_t segment_selector_size = *addr++;
2708 if (segment_selector_size != 0)
2709 {
2710 warning (_("Section .debug_aranges in %s entry at offset %s "
2711 "segment_selector_size %u is not supported, "
2712 "ignoring .debug_aranges."),
2713 objfile_name (objfile),
2714 plongest (entry_addr - section->buffer),
2715 segment_selector_size);
2716 return;
2717 }
2718
2719 /* Must pad to an alignment boundary that is twice the address
2720 size. It is undocumented by the DWARF standard but GCC does
2721 use it. However, not every compiler does this. We can see
2722 whether it has happened by looking at the total length of the
2723 contents of the aranges for this CU -- it if isn't a multiple
2724 of twice the address size, then we skip any leftover
2725 bytes. */
2726 addr += (entry_end - addr) % (2 * address_size);
2727
2728 for (;;)
2729 {
2730 if (addr + 2 * address_size > entry_end)
2731 {
2732 warning (_("Section .debug_aranges in %s entry at offset %s "
2733 "address list is not properly terminated, "
2734 "ignoring .debug_aranges."),
2735 objfile_name (objfile),
2736 plongest (entry_addr - section->buffer));
2737 return;
2738 }
2739 ULONGEST start = extract_unsigned_integer (addr, address_size,
2740 dwarf5_byte_order);
2741 addr += address_size;
2742 ULONGEST length = extract_unsigned_integer (addr, address_size,
2743 dwarf5_byte_order);
2744 addr += address_size;
2745 if (start == 0 && length == 0)
2746 break;
2747 if (start == 0 && !per_bfd->has_section_at_zero)
2748 {
2749 /* Symbol was eliminated due to a COMDAT group. */
2750 continue;
2751 }
2752 ULONGEST end = start + length;
2753 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2754 - baseaddr);
2755 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2756 - baseaddr);
2757 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2758 }
2759 }
2760
2761 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2762 &per_bfd->obstack);
2763 }
2764
2765 /* A helper function that reads the .gdb_index from BUFFER and fills
2766 in MAP. FILENAME is the name of the file containing the data;
2767 it is used for error reporting. DEPRECATED_OK is true if it is
2768 ok to use deprecated sections.
2769
2770 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2771 out parameters that are filled in with information about the CU and
2772 TU lists in the section.
2773
2774 Returns true if all went well, false otherwise. */
2775
2776 static bool
2777 read_gdb_index_from_buffer (const char *filename,
2778 bool deprecated_ok,
2779 gdb::array_view<const gdb_byte> buffer,
2780 struct mapped_index *map,
2781 const gdb_byte **cu_list,
2782 offset_type *cu_list_elements,
2783 const gdb_byte **types_list,
2784 offset_type *types_list_elements)
2785 {
2786 const gdb_byte *addr = &buffer[0];
2787 offset_view metadata (buffer);
2788
2789 /* Version check. */
2790 offset_type version = metadata[0];
2791 /* Versions earlier than 3 emitted every copy of a psymbol. This
2792 causes the index to behave very poorly for certain requests. Version 3
2793 contained incomplete addrmap. So, it seems better to just ignore such
2794 indices. */
2795 if (version < 4)
2796 {
2797 static int warning_printed = 0;
2798 if (!warning_printed)
2799 {
2800 warning (_("Skipping obsolete .gdb_index section in %s."),
2801 filename);
2802 warning_printed = 1;
2803 }
2804 return 0;
2805 }
2806 /* Index version 4 uses a different hash function than index version
2807 5 and later.
2808
2809 Versions earlier than 6 did not emit psymbols for inlined
2810 functions. Using these files will cause GDB not to be able to
2811 set breakpoints on inlined functions by name, so we ignore these
2812 indices unless the user has done
2813 "set use-deprecated-index-sections on". */
2814 if (version < 6 && !deprecated_ok)
2815 {
2816 static int warning_printed = 0;
2817 if (!warning_printed)
2818 {
2819 warning (_("\
2820 Skipping deprecated .gdb_index section in %s.\n\
2821 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2822 to use the section anyway."),
2823 filename);
2824 warning_printed = 1;
2825 }
2826 return 0;
2827 }
2828 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2829 of the TU (for symbols coming from TUs),
2830 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2831 Plus gold-generated indices can have duplicate entries for global symbols,
2832 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2833 These are just performance bugs, and we can't distinguish gdb-generated
2834 indices from gold-generated ones, so issue no warning here. */
2835
2836 /* Indexes with higher version than the one supported by GDB may be no
2837 longer backward compatible. */
2838 if (version > 8)
2839 return 0;
2840
2841 map->version = version;
2842
2843 int i = 1;
2844 *cu_list = addr + metadata[i];
2845 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2846 ++i;
2847
2848 *types_list = addr + metadata[i];
2849 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2850 ++i;
2851
2852 const gdb_byte *address_table = addr + metadata[i];
2853 const gdb_byte *address_table_end = addr + metadata[i + 1];
2854 map->address_table
2855 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2856 ++i;
2857
2858 const gdb_byte *symbol_table = addr + metadata[i];
2859 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2860 map->symbol_table
2861 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2862 symbol_table_end));
2863
2864 ++i;
2865 map->constant_pool = buffer.slice (metadata[i]);
2866
2867 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2868 {
2869 /* An empty constant pool implies that all symbol table entries are
2870 empty. Make map->symbol_table.empty () == true. */
2871 map->symbol_table
2872 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2873 symbol_table));
2874 }
2875
2876 return 1;
2877 }
2878
2879 /* Callback types for dwarf2_read_gdb_index. */
2880
2881 typedef gdb::function_view
2882 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2883 get_gdb_index_contents_ftype;
2884 typedef gdb::function_view
2885 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2886 get_gdb_index_contents_dwz_ftype;
2887
2888 /* Read .gdb_index. If everything went ok, initialize the "quick"
2889 elements of all the CUs and return 1. Otherwise, return 0. */
2890
2891 static int
2892 dwarf2_read_gdb_index
2893 (dwarf2_per_objfile *per_objfile,
2894 get_gdb_index_contents_ftype get_gdb_index_contents,
2895 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2896 {
2897 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2898 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2899 struct dwz_file *dwz;
2900 struct objfile *objfile = per_objfile->objfile;
2901 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2902
2903 gdb::array_view<const gdb_byte> main_index_contents
2904 = get_gdb_index_contents (objfile, per_bfd);
2905
2906 if (main_index_contents.empty ())
2907 return 0;
2908
2909 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2910 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2911 use_deprecated_index_sections,
2912 main_index_contents, map.get (), &cu_list,
2913 &cu_list_elements, &types_list,
2914 &types_list_elements))
2915 return 0;
2916
2917 /* Don't use the index if it's empty. */
2918 if (map->symbol_table.empty ())
2919 return 0;
2920
2921 /* If there is a .dwz file, read it so we can get its CU list as
2922 well. */
2923 dwz = dwarf2_get_dwz_file (per_bfd);
2924 if (dwz != NULL)
2925 {
2926 struct mapped_index dwz_map;
2927 const gdb_byte *dwz_types_ignore;
2928 offset_type dwz_types_elements_ignore;
2929
2930 gdb::array_view<const gdb_byte> dwz_index_content
2931 = get_gdb_index_contents_dwz (objfile, dwz);
2932
2933 if (dwz_index_content.empty ())
2934 return 0;
2935
2936 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2937 1, dwz_index_content, &dwz_map,
2938 &dwz_list, &dwz_list_elements,
2939 &dwz_types_ignore,
2940 &dwz_types_elements_ignore))
2941 {
2942 warning (_("could not read '.gdb_index' section from %s; skipping"),
2943 bfd_get_filename (dwz->dwz_bfd.get ()));
2944 return 0;
2945 }
2946 }
2947
2948 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2949 dwz_list_elements);
2950
2951 if (types_list_elements)
2952 {
2953 /* We can only handle a single .debug_types when we have an
2954 index. */
2955 if (per_bfd->types.size () != 1)
2956 return 0;
2957
2958 dwarf2_section_info *section = &per_bfd->types[0];
2959
2960 create_signatured_type_table_from_index (per_bfd, section, types_list,
2961 types_list_elements);
2962 }
2963
2964 create_addrmap_from_index (per_objfile, map.get ());
2965
2966 per_bfd->index_table = std::move (map);
2967 per_bfd->using_index = 1;
2968 per_bfd->quick_file_names_table =
2969 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2970
2971 return 1;
2972 }
2973
2974 /* die_reader_func for dw2_get_file_names. */
2975
2976 static void
2977 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2978 struct die_info *comp_unit_die)
2979 {
2980 struct dwarf2_cu *cu = reader->cu;
2981 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2982 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2983 struct dwarf2_per_cu_data *lh_cu;
2984 struct attribute *attr;
2985 void **slot;
2986 struct quick_file_names *qfn;
2987
2988 gdb_assert (! this_cu->is_debug_types);
2989
2990 this_cu->v.quick->files_read = true;
2991 /* Our callers never want to match partial units -- instead they
2992 will match the enclosing full CU. */
2993 if (comp_unit_die->tag == DW_TAG_partial_unit)
2994 return;
2995
2996 lh_cu = this_cu;
2997 slot = NULL;
2998
2999 line_header_up lh;
3000 sect_offset line_offset {};
3001
3002 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3003 if (attr != nullptr && attr->form_is_unsigned ())
3004 {
3005 struct quick_file_names find_entry;
3006
3007 line_offset = (sect_offset) attr->as_unsigned ();
3008
3009 /* We may have already read in this line header (TU line header sharing).
3010 If we have we're done. */
3011 find_entry.hash.dwo_unit = cu->dwo_unit;
3012 find_entry.hash.line_sect_off = line_offset;
3013 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3014 &find_entry, INSERT);
3015 if (*slot != NULL)
3016 {
3017 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3018 return;
3019 }
3020
3021 lh = dwarf_decode_line_header (line_offset, cu);
3022 }
3023
3024 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3025
3026 int offset = 0;
3027 if (strcmp (fnd.name, "<unknown>") != 0)
3028 ++offset;
3029 else if (lh == nullptr)
3030 return;
3031
3032 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3033 qfn->hash.dwo_unit = cu->dwo_unit;
3034 qfn->hash.line_sect_off = line_offset;
3035 /* There may not be a DW_AT_stmt_list. */
3036 if (slot != nullptr)
3037 *slot = qfn;
3038
3039 std::vector<const char *> include_names;
3040 if (lh != nullptr)
3041 {
3042 for (const auto &entry : lh->file_names ())
3043 {
3044 gdb::unique_xmalloc_ptr<char> name_holder;
3045 const char *include_name =
3046 compute_include_file_name (lh.get (), entry, fnd, &name_holder);
3047 if (include_name != nullptr)
3048 {
3049 include_name = per_objfile->objfile->intern (include_name);
3050 include_names.push_back (include_name);
3051 }
3052 }
3053 }
3054
3055 qfn->num_file_names = offset + include_names.size ();
3056 qfn->comp_dir = fnd.comp_dir;
3057 qfn->file_names =
3058 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3059 qfn->num_file_names);
3060 if (offset != 0)
3061 qfn->file_names[0] = xstrdup (fnd.name);
3062
3063 if (!include_names.empty ())
3064 memcpy (&qfn->file_names[offset], include_names.data (),
3065 include_names.size () * sizeof (const char *));
3066
3067 qfn->real_names = NULL;
3068
3069 lh_cu->v.quick->file_names = qfn;
3070 }
3071
3072 /* A helper for the "quick" functions which attempts to read the line
3073 table for THIS_CU. */
3074
3075 static struct quick_file_names *
3076 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3077 dwarf2_per_objfile *per_objfile)
3078 {
3079 /* This should never be called for TUs. */
3080 gdb_assert (! this_cu->is_debug_types);
3081 /* Nor type unit groups. */
3082 gdb_assert (! this_cu->type_unit_group_p ());
3083
3084 if (this_cu->v.quick->files_read)
3085 return this_cu->v.quick->file_names;
3086
3087 cutu_reader reader (this_cu, per_objfile);
3088 if (!reader.dummy_p)
3089 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3090
3091 return this_cu->v.quick->file_names;
3092 }
3093
3094 /* A helper for the "quick" functions which computes and caches the
3095 real path for a given file name from the line table. */
3096
3097 static const char *
3098 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3099 struct quick_file_names *qfn, int index)
3100 {
3101 if (qfn->real_names == NULL)
3102 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3103 qfn->num_file_names, const char *);
3104
3105 if (qfn->real_names[index] == NULL)
3106 {
3107 const char *dirname = nullptr;
3108
3109 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
3110 dirname = qfn->comp_dir;
3111
3112 gdb::unique_xmalloc_ptr<char> fullname;
3113 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
3114
3115 qfn->real_names[index] = fullname.release ();
3116 }
3117
3118 return qfn->real_names[index];
3119 }
3120
3121 struct symtab *
3122 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3123 {
3124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3125 dwarf2_per_cu_data *dwarf_cu
3126 = per_objfile->per_bfd->all_comp_units.back ().get ();
3127 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3128
3129 if (cust == NULL)
3130 return NULL;
3131
3132 return compunit_primary_filetab (cust);
3133 }
3134
3135 /* See read.h. */
3136
3137 void
3138 dwarf2_per_cu_data::free_cached_file_names ()
3139 {
3140 if (per_bfd == nullptr || !per_bfd->using_index || v.quick == nullptr)
3141 return;
3142
3143 struct quick_file_names *file_data = v.quick->file_names;
3144 if (file_data != nullptr && file_data->real_names != nullptr)
3145 {
3146 for (int i = 0; i < file_data->num_file_names; ++i)
3147 {
3148 xfree ((void *) file_data->real_names[i]);
3149 file_data->real_names[i] = nullptr;
3150 }
3151 }
3152 }
3153
3154 void
3155 dwarf2_base_index_functions::forget_cached_source_info
3156 (struct objfile *objfile)
3157 {
3158 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3159
3160 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
3161 per_cu->free_cached_file_names ();
3162 }
3163
3164 /* Struct used to manage iterating over all CUs looking for a symbol. */
3165
3166 struct dw2_symtab_iterator
3167 {
3168 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3169 dwarf2_per_objfile *per_objfile;
3170 /* If set, only look for symbols that match that block. Valid values are
3171 GLOBAL_BLOCK and STATIC_BLOCK. */
3172 gdb::optional<block_enum> block_index;
3173 /* The kind of symbol we're looking for. */
3174 domain_enum domain;
3175 /* The list of CUs from the index entry of the symbol,
3176 or NULL if not found. */
3177 offset_view vec;
3178 /* The next element in VEC to look at. */
3179 int next;
3180 /* The number of elements in VEC, or zero if there is no match. */
3181 int length;
3182 /* Have we seen a global version of the symbol?
3183 If so we can ignore all further global instances.
3184 This is to work around gold/15646, inefficient gold-generated
3185 indices. */
3186 int global_seen;
3187 };
3188
3189 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3190
3191 static void
3192 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3193 dwarf2_per_objfile *per_objfile,
3194 gdb::optional<block_enum> block_index,
3195 domain_enum domain, offset_type namei)
3196 {
3197 iter->per_objfile = per_objfile;
3198 iter->block_index = block_index;
3199 iter->domain = domain;
3200 iter->next = 0;
3201 iter->global_seen = 0;
3202 iter->vec = {};
3203 iter->length = 0;
3204
3205 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3206 /* index is NULL if OBJF_READNOW. */
3207 if (index == NULL)
3208 return;
3209
3210 gdb_assert (!index->symbol_name_slot_invalid (namei));
3211 offset_type vec_idx = index->symbol_vec_index (namei);
3212
3213 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3214 iter->length = iter->vec[0];
3215 }
3216
3217 /* Return the next matching CU or NULL if there are no more. */
3218
3219 static struct dwarf2_per_cu_data *
3220 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3221 {
3222 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3223
3224 for ( ; iter->next < iter->length; ++iter->next)
3225 {
3226 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3227 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3228 gdb_index_symbol_kind symbol_kind =
3229 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3230 /* Only check the symbol attributes if they're present.
3231 Indices prior to version 7 don't record them,
3232 and indices >= 7 may elide them for certain symbols
3233 (gold does this). */
3234 int attrs_valid =
3235 (per_objfile->per_bfd->index_table->version >= 7
3236 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3237
3238 /* Don't crash on bad data. */
3239 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3240 {
3241 complaint (_(".gdb_index entry has bad CU index"
3242 " [in module %s]"), objfile_name (per_objfile->objfile));
3243 continue;
3244 }
3245
3246 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3247
3248 /* Skip if already read in. */
3249 if (per_objfile->symtab_set_p (per_cu))
3250 continue;
3251
3252 /* Check static vs global. */
3253 if (attrs_valid)
3254 {
3255 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3256
3257 if (iter->block_index.has_value ())
3258 {
3259 bool want_static = *iter->block_index == STATIC_BLOCK;
3260
3261 if (is_static != want_static)
3262 continue;
3263 }
3264
3265 /* Work around gold/15646. */
3266 if (!is_static
3267 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3268 {
3269 if (iter->global_seen)
3270 continue;
3271
3272 iter->global_seen = 1;
3273 }
3274 }
3275
3276 /* Only check the symbol's kind if it has one. */
3277 if (attrs_valid)
3278 {
3279 switch (iter->domain)
3280 {
3281 case VAR_DOMAIN:
3282 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3283 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3284 /* Some types are also in VAR_DOMAIN. */
3285 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3286 continue;
3287 break;
3288 case STRUCT_DOMAIN:
3289 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3290 continue;
3291 break;
3292 case LABEL_DOMAIN:
3293 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3294 continue;
3295 break;
3296 case MODULE_DOMAIN:
3297 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3298 continue;
3299 break;
3300 default:
3301 break;
3302 }
3303 }
3304
3305 ++iter->next;
3306 return per_cu;
3307 }
3308
3309 return NULL;
3310 }
3311
3312 void
3313 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3314 bool print_bcache)
3315 {
3316 if (print_bcache)
3317 return;
3318
3319 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3320 int total = per_objfile->per_bfd->all_comp_units.size ();
3321 int count = 0;
3322
3323 for (int i = 0; i < total; ++i)
3324 {
3325 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3326
3327 if (!per_objfile->symtab_set_p (per_cu))
3328 ++count;
3329 }
3330 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3331 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3332 }
3333
3334 /* This dumps minimal information about the index.
3335 It is called via "mt print objfiles".
3336 One use is to verify .gdb_index has been loaded by the
3337 gdb.dwarf2/gdb-index.exp testcase. */
3338
3339 void
3340 dwarf2_gdb_index::dump (struct objfile *objfile)
3341 {
3342 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3343
3344 gdb_assert (per_objfile->per_bfd->using_index);
3345 printf_filtered (".gdb_index:");
3346 if (per_objfile->per_bfd->index_table != NULL)
3347 {
3348 printf_filtered (" version %d\n",
3349 per_objfile->per_bfd->index_table->version);
3350 }
3351 else
3352 printf_filtered (" faked for \"readnow\"\n");
3353 printf_filtered ("\n");
3354 }
3355
3356 void
3357 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3358 {
3359 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3360 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3361
3362 for (int i = 0; i < total_units; ++i)
3363 {
3364 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3365
3366 /* We don't want to directly expand a partial CU, because if we
3367 read it with the wrong language, then assertion failures can
3368 be triggered later on. See PR symtab/23010. So, tell
3369 dw2_instantiate_symtab to skip partial CUs -- any important
3370 partial CU will be read via DW_TAG_imported_unit anyway. */
3371 dw2_instantiate_symtab (per_cu, per_objfile, true);
3372 }
3373 }
3374
3375 static bool
3376 dw2_expand_symtabs_matching_symbol
3377 (mapped_index_base &index,
3378 const lookup_name_info &lookup_name_in,
3379 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3380 gdb::function_view<bool (offset_type)> match_callback,
3381 dwarf2_per_objfile *per_objfile);
3382
3383 static bool
3384 dw2_expand_symtabs_matching_one
3385 (dwarf2_per_cu_data *per_cu,
3386 dwarf2_per_objfile *per_objfile,
3387 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3388 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3389
3390 void
3391 dwarf2_gdb_index::expand_matching_symbols
3392 (struct objfile *objfile,
3393 const lookup_name_info &name, domain_enum domain,
3394 int global,
3395 symbol_compare_ftype *ordered_compare)
3396 {
3397 /* Used for Ada. */
3398 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3399
3400 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3401
3402 if (per_objfile->per_bfd->index_table != nullptr)
3403 {
3404 mapped_index &index = *per_objfile->per_bfd->index_table;
3405
3406 const char *match_name = name.ada ().lookup_name ().c_str ();
3407 auto matcher = [&] (const char *symname)
3408 {
3409 if (ordered_compare == nullptr)
3410 return true;
3411 return ordered_compare (symname, match_name) == 0;
3412 };
3413
3414 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3415 [&] (offset_type namei)
3416 {
3417 struct dw2_symtab_iterator iter;
3418 struct dwarf2_per_cu_data *per_cu;
3419
3420 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3421 namei);
3422 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3423 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3424 nullptr);
3425 return true;
3426 }, per_objfile);
3427 }
3428 else
3429 {
3430 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3431 proceed assuming all symtabs have been read in. */
3432 }
3433 }
3434
3435 /* Starting from a search name, return the string that finds the upper
3436 bound of all strings that start with SEARCH_NAME in a sorted name
3437 list. Returns the empty string to indicate that the upper bound is
3438 the end of the list. */
3439
3440 static std::string
3441 make_sort_after_prefix_name (const char *search_name)
3442 {
3443 /* When looking to complete "func", we find the upper bound of all
3444 symbols that start with "func" by looking for where we'd insert
3445 the closest string that would follow "func" in lexicographical
3446 order. Usually, that's "func"-with-last-character-incremented,
3447 i.e. "fund". Mind non-ASCII characters, though. Usually those
3448 will be UTF-8 multi-byte sequences, but we can't be certain.
3449 Especially mind the 0xff character, which is a valid character in
3450 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3451 rule out compilers allowing it in identifiers. Note that
3452 conveniently, strcmp/strcasecmp are specified to compare
3453 characters interpreted as unsigned char. So what we do is treat
3454 the whole string as a base 256 number composed of a sequence of
3455 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3456 to 0, and carries 1 to the following more-significant position.
3457 If the very first character in SEARCH_NAME ends up incremented
3458 and carries/overflows, then the upper bound is the end of the
3459 list. The string after the empty string is also the empty
3460 string.
3461
3462 Some examples of this operation:
3463
3464 SEARCH_NAME => "+1" RESULT
3465
3466 "abc" => "abd"
3467 "ab\xff" => "ac"
3468 "\xff" "a" "\xff" => "\xff" "b"
3469 "\xff" => ""
3470 "\xff\xff" => ""
3471 "" => ""
3472
3473 Then, with these symbols for example:
3474
3475 func
3476 func1
3477 fund
3478
3479 completing "func" looks for symbols between "func" and
3480 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3481 which finds "func" and "func1", but not "fund".
3482
3483 And with:
3484
3485 funcÿ (Latin1 'ÿ' [0xff])
3486 funcÿ1
3487 fund
3488
3489 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3490 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3491
3492 And with:
3493
3494 ÿÿ (Latin1 'ÿ' [0xff])
3495 ÿÿ1
3496
3497 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3498 the end of the list.
3499 */
3500 std::string after = search_name;
3501 while (!after.empty () && (unsigned char) after.back () == 0xff)
3502 after.pop_back ();
3503 if (!after.empty ())
3504 after.back () = (unsigned char) after.back () + 1;
3505 return after;
3506 }
3507
3508 /* See declaration. */
3509
3510 std::pair<std::vector<name_component>::const_iterator,
3511 std::vector<name_component>::const_iterator>
3512 mapped_index_base::find_name_components_bounds
3513 (const lookup_name_info &lookup_name_without_params, language lang,
3514 dwarf2_per_objfile *per_objfile) const
3515 {
3516 auto *name_cmp
3517 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3518
3519 const char *lang_name
3520 = lookup_name_without_params.language_lookup_name (lang);
3521
3522 /* Comparison function object for lower_bound that matches against a
3523 given symbol name. */
3524 auto lookup_compare_lower = [&] (const name_component &elem,
3525 const char *name)
3526 {
3527 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3528 const char *elem_name = elem_qualified + elem.name_offset;
3529 return name_cmp (elem_name, name) < 0;
3530 };
3531
3532 /* Comparison function object for upper_bound that matches against a
3533 given symbol name. */
3534 auto lookup_compare_upper = [&] (const char *name,
3535 const name_component &elem)
3536 {
3537 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3538 const char *elem_name = elem_qualified + elem.name_offset;
3539 return name_cmp (name, elem_name) < 0;
3540 };
3541
3542 auto begin = this->name_components.begin ();
3543 auto end = this->name_components.end ();
3544
3545 /* Find the lower bound. */
3546 auto lower = [&] ()
3547 {
3548 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3549 return begin;
3550 else
3551 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3552 } ();
3553
3554 /* Find the upper bound. */
3555 auto upper = [&] ()
3556 {
3557 if (lookup_name_without_params.completion_mode ())
3558 {
3559 /* In completion mode, we want UPPER to point past all
3560 symbols names that have the same prefix. I.e., with
3561 these symbols, and completing "func":
3562
3563 function << lower bound
3564 function1
3565 other_function << upper bound
3566
3567 We find the upper bound by looking for the insertion
3568 point of "func"-with-last-character-incremented,
3569 i.e. "fund". */
3570 std::string after = make_sort_after_prefix_name (lang_name);
3571 if (after.empty ())
3572 return end;
3573 return std::lower_bound (lower, end, after.c_str (),
3574 lookup_compare_lower);
3575 }
3576 else
3577 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3578 } ();
3579
3580 return {lower, upper};
3581 }
3582
3583 /* See declaration. */
3584
3585 void
3586 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3587 {
3588 if (!this->name_components.empty ())
3589 return;
3590
3591 this->name_components_casing = case_sensitivity;
3592 auto *name_cmp
3593 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3594
3595 /* The code below only knows how to break apart components of C++
3596 symbol names (and other languages that use '::' as
3597 namespace/module separator) and Ada symbol names. */
3598 auto count = this->symbol_name_count ();
3599 for (offset_type idx = 0; idx < count; idx++)
3600 {
3601 if (this->symbol_name_slot_invalid (idx))
3602 continue;
3603
3604 const char *name = this->symbol_name_at (idx, per_objfile);
3605
3606 /* Add each name component to the name component table. */
3607 unsigned int previous_len = 0;
3608
3609 if (strstr (name, "::") != nullptr)
3610 {
3611 for (unsigned int current_len = cp_find_first_component (name);
3612 name[current_len] != '\0';
3613 current_len += cp_find_first_component (name + current_len))
3614 {
3615 gdb_assert (name[current_len] == ':');
3616 this->name_components.push_back ({previous_len, idx});
3617 /* Skip the '::'. */
3618 current_len += 2;
3619 previous_len = current_len;
3620 }
3621 }
3622 else
3623 {
3624 /* Handle the Ada encoded (aka mangled) form here. */
3625 for (const char *iter = strstr (name, "__");
3626 iter != nullptr;
3627 iter = strstr (iter, "__"))
3628 {
3629 this->name_components.push_back ({previous_len, idx});
3630 iter += 2;
3631 previous_len = iter - name;
3632 }
3633 }
3634
3635 this->name_components.push_back ({previous_len, idx});
3636 }
3637
3638 /* Sort name_components elements by name. */
3639 auto name_comp_compare = [&] (const name_component &left,
3640 const name_component &right)
3641 {
3642 const char *left_qualified
3643 = this->symbol_name_at (left.idx, per_objfile);
3644 const char *right_qualified
3645 = this->symbol_name_at (right.idx, per_objfile);
3646
3647 const char *left_name = left_qualified + left.name_offset;
3648 const char *right_name = right_qualified + right.name_offset;
3649
3650 return name_cmp (left_name, right_name) < 0;
3651 };
3652
3653 std::sort (this->name_components.begin (),
3654 this->name_components.end (),
3655 name_comp_compare);
3656 }
3657
3658 /* Helper for dw2_expand_symtabs_matching that works with a
3659 mapped_index_base instead of the containing objfile. This is split
3660 to a separate function in order to be able to unit test the
3661 name_components matching using a mock mapped_index_base. For each
3662 symbol name that matches, calls MATCH_CALLBACK, passing it the
3663 symbol's index in the mapped_index_base symbol table. */
3664
3665 static bool
3666 dw2_expand_symtabs_matching_symbol
3667 (mapped_index_base &index,
3668 const lookup_name_info &lookup_name_in,
3669 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3670 gdb::function_view<bool (offset_type)> match_callback,
3671 dwarf2_per_objfile *per_objfile)
3672 {
3673 lookup_name_info lookup_name_without_params
3674 = lookup_name_in.make_ignore_params ();
3675
3676 /* Build the symbol name component sorted vector, if we haven't
3677 yet. */
3678 index.build_name_components (per_objfile);
3679
3680 /* The same symbol may appear more than once in the range though.
3681 E.g., if we're looking for symbols that complete "w", and we have
3682 a symbol named "w1::w2", we'll find the two name components for
3683 that same symbol in the range. To be sure we only call the
3684 callback once per symbol, we first collect the symbol name
3685 indexes that matched in a temporary vector and ignore
3686 duplicates. */
3687 std::vector<offset_type> matches;
3688
3689 struct name_and_matcher
3690 {
3691 symbol_name_matcher_ftype *matcher;
3692 const char *name;
3693
3694 bool operator== (const name_and_matcher &other) const
3695 {
3696 return matcher == other.matcher && strcmp (name, other.name) == 0;
3697 }
3698 };
3699
3700 /* A vector holding all the different symbol name matchers, for all
3701 languages. */
3702 std::vector<name_and_matcher> matchers;
3703
3704 for (int i = 0; i < nr_languages; i++)
3705 {
3706 enum language lang_e = (enum language) i;
3707
3708 const language_defn *lang = language_def (lang_e);
3709 symbol_name_matcher_ftype *name_matcher
3710 = lang->get_symbol_name_matcher (lookup_name_without_params);
3711
3712 name_and_matcher key {
3713 name_matcher,
3714 lookup_name_without_params.language_lookup_name (lang_e)
3715 };
3716
3717 /* Don't insert the same comparison routine more than once.
3718 Note that we do this linear walk. This is not a problem in
3719 practice because the number of supported languages is
3720 low. */
3721 if (std::find (matchers.begin (), matchers.end (), key)
3722 != matchers.end ())
3723 continue;
3724 matchers.push_back (std::move (key));
3725
3726 auto bounds
3727 = index.find_name_components_bounds (lookup_name_without_params,
3728 lang_e, per_objfile);
3729
3730 /* Now for each symbol name in range, check to see if we have a name
3731 match, and if so, call the MATCH_CALLBACK callback. */
3732
3733 for (; bounds.first != bounds.second; ++bounds.first)
3734 {
3735 const char *qualified
3736 = index.symbol_name_at (bounds.first->idx, per_objfile);
3737
3738 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3739 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3740 continue;
3741
3742 matches.push_back (bounds.first->idx);
3743 }
3744 }
3745
3746 std::sort (matches.begin (), matches.end ());
3747
3748 /* Finally call the callback, once per match. */
3749 ULONGEST prev = -1;
3750 bool result = true;
3751 for (offset_type idx : matches)
3752 {
3753 if (prev != idx)
3754 {
3755 if (!match_callback (idx))
3756 {
3757 result = false;
3758 break;
3759 }
3760 prev = idx;
3761 }
3762 }
3763
3764 /* Above we use a type wider than idx's for 'prev', since 0 and
3765 (offset_type)-1 are both possible values. */
3766 static_assert (sizeof (prev) > sizeof (offset_type), "");
3767
3768 return result;
3769 }
3770
3771 #if GDB_SELF_TEST
3772
3773 namespace selftests { namespace dw2_expand_symtabs_matching {
3774
3775 /* A mock .gdb_index/.debug_names-like name index table, enough to
3776 exercise dw2_expand_symtabs_matching_symbol, which works with the
3777 mapped_index_base interface. Builds an index from the symbol list
3778 passed as parameter to the constructor. */
3779 class mock_mapped_index : public mapped_index_base
3780 {
3781 public:
3782 mock_mapped_index (gdb::array_view<const char *> symbols)
3783 : m_symbol_table (symbols)
3784 {}
3785
3786 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3787
3788 /* Return the number of names in the symbol table. */
3789 size_t symbol_name_count () const override
3790 {
3791 return m_symbol_table.size ();
3792 }
3793
3794 /* Get the name of the symbol at IDX in the symbol table. */
3795 const char *symbol_name_at
3796 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3797 {
3798 return m_symbol_table[idx];
3799 }
3800
3801 private:
3802 gdb::array_view<const char *> m_symbol_table;
3803 };
3804
3805 /* Convenience function that converts a NULL pointer to a "<null>"
3806 string, to pass to print routines. */
3807
3808 static const char *
3809 string_or_null (const char *str)
3810 {
3811 return str != NULL ? str : "<null>";
3812 }
3813
3814 /* Check if a lookup_name_info built from
3815 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3816 index. EXPECTED_LIST is the list of expected matches, in expected
3817 matching order. If no match expected, then an empty list is
3818 specified. Returns true on success. On failure prints a warning
3819 indicating the file:line that failed, and returns false. */
3820
3821 static bool
3822 check_match (const char *file, int line,
3823 mock_mapped_index &mock_index,
3824 const char *name, symbol_name_match_type match_type,
3825 bool completion_mode,
3826 std::initializer_list<const char *> expected_list,
3827 dwarf2_per_objfile *per_objfile)
3828 {
3829 lookup_name_info lookup_name (name, match_type, completion_mode);
3830
3831 bool matched = true;
3832
3833 auto mismatch = [&] (const char *expected_str,
3834 const char *got)
3835 {
3836 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3837 "expected=\"%s\", got=\"%s\"\n"),
3838 file, line,
3839 (match_type == symbol_name_match_type::FULL
3840 ? "FULL" : "WILD"),
3841 name, string_or_null (expected_str), string_or_null (got));
3842 matched = false;
3843 };
3844
3845 auto expected_it = expected_list.begin ();
3846 auto expected_end = expected_list.end ();
3847
3848 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3849 nullptr,
3850 [&] (offset_type idx)
3851 {
3852 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3853 const char *expected_str
3854 = expected_it == expected_end ? NULL : *expected_it++;
3855
3856 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3857 mismatch (expected_str, matched_name);
3858 return true;
3859 }, per_objfile);
3860
3861 const char *expected_str
3862 = expected_it == expected_end ? NULL : *expected_it++;
3863 if (expected_str != NULL)
3864 mismatch (expected_str, NULL);
3865
3866 return matched;
3867 }
3868
3869 /* The symbols added to the mock mapped_index for testing (in
3870 canonical form). */
3871 static const char *test_symbols[] = {
3872 "function",
3873 "std::bar",
3874 "std::zfunction",
3875 "std::zfunction2",
3876 "w1::w2",
3877 "ns::foo<char*>",
3878 "ns::foo<int>",
3879 "ns::foo<long>",
3880 "ns2::tmpl<int>::foo2",
3881 "(anonymous namespace)::A::B::C",
3882
3883 /* These are used to check that the increment-last-char in the
3884 matching algorithm for completion doesn't match "t1_fund" when
3885 completing "t1_func". */
3886 "t1_func",
3887 "t1_func1",
3888 "t1_fund",
3889 "t1_fund1",
3890
3891 /* A UTF-8 name with multi-byte sequences to make sure that
3892 cp-name-parser understands this as a single identifier ("função"
3893 is "function" in PT). */
3894 u8"u8função",
3895
3896 /* \377 (0xff) is Latin1 'ÿ'. */
3897 "yfunc\377",
3898
3899 /* \377 (0xff) is Latin1 'ÿ'. */
3900 "\377",
3901 "\377\377123",
3902
3903 /* A name with all sorts of complications. Starts with "z" to make
3904 it easier for the completion tests below. */
3905 #define Z_SYM_NAME \
3906 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3907 "::tuple<(anonymous namespace)::ui*, " \
3908 "std::default_delete<(anonymous namespace)::ui>, void>"
3909
3910 Z_SYM_NAME
3911 };
3912
3913 /* Returns true if the mapped_index_base::find_name_component_bounds
3914 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3915 in completion mode. */
3916
3917 static bool
3918 check_find_bounds_finds (mapped_index_base &index,
3919 const char *search_name,
3920 gdb::array_view<const char *> expected_syms,
3921 dwarf2_per_objfile *per_objfile)
3922 {
3923 lookup_name_info lookup_name (search_name,
3924 symbol_name_match_type::FULL, true);
3925
3926 auto bounds = index.find_name_components_bounds (lookup_name,
3927 language_cplus,
3928 per_objfile);
3929
3930 size_t distance = std::distance (bounds.first, bounds.second);
3931 if (distance != expected_syms.size ())
3932 return false;
3933
3934 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3935 {
3936 auto nc_elem = bounds.first + exp_elem;
3937 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3938 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3939 return false;
3940 }
3941
3942 return true;
3943 }
3944
3945 /* Test the lower-level mapped_index::find_name_component_bounds
3946 method. */
3947
3948 static void
3949 test_mapped_index_find_name_component_bounds ()
3950 {
3951 mock_mapped_index mock_index (test_symbols);
3952
3953 mock_index.build_name_components (NULL /* per_objfile */);
3954
3955 /* Test the lower-level mapped_index::find_name_component_bounds
3956 method in completion mode. */
3957 {
3958 static const char *expected_syms[] = {
3959 "t1_func",
3960 "t1_func1",
3961 };
3962
3963 SELF_CHECK (check_find_bounds_finds
3964 (mock_index, "t1_func", expected_syms,
3965 NULL /* per_objfile */));
3966 }
3967
3968 /* Check that the increment-last-char in the name matching algorithm
3969 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3970 {
3971 static const char *expected_syms1[] = {
3972 "\377",
3973 "\377\377123",
3974 };
3975 SELF_CHECK (check_find_bounds_finds
3976 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3977
3978 static const char *expected_syms2[] = {
3979 "\377\377123",
3980 };
3981 SELF_CHECK (check_find_bounds_finds
3982 (mock_index, "\377\377", expected_syms2,
3983 NULL /* per_objfile */));
3984 }
3985 }
3986
3987 /* Test dw2_expand_symtabs_matching_symbol. */
3988
3989 static void
3990 test_dw2_expand_symtabs_matching_symbol ()
3991 {
3992 mock_mapped_index mock_index (test_symbols);
3993
3994 /* We let all tests run until the end even if some fails, for debug
3995 convenience. */
3996 bool any_mismatch = false;
3997
3998 /* Create the expected symbols list (an initializer_list). Needed
3999 because lists have commas, and we need to pass them to CHECK,
4000 which is a macro. */
4001 #define EXPECT(...) { __VA_ARGS__ }
4002
4003 /* Wrapper for check_match that passes down the current
4004 __FILE__/__LINE__. */
4005 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4006 any_mismatch |= !check_match (__FILE__, __LINE__, \
4007 mock_index, \
4008 NAME, MATCH_TYPE, COMPLETION_MODE, \
4009 EXPECTED_LIST, NULL)
4010
4011 /* Identity checks. */
4012 for (const char *sym : test_symbols)
4013 {
4014 /* Should be able to match all existing symbols. */
4015 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4016 EXPECT (sym));
4017
4018 /* Should be able to match all existing symbols with
4019 parameters. */
4020 std::string with_params = std::string (sym) + "(int)";
4021 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4022 EXPECT (sym));
4023
4024 /* Should be able to match all existing symbols with
4025 parameters and qualifiers. */
4026 with_params = std::string (sym) + " ( int ) const";
4027 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4028 EXPECT (sym));
4029
4030 /* This should really find sym, but cp-name-parser.y doesn't
4031 know about lvalue/rvalue qualifiers yet. */
4032 with_params = std::string (sym) + " ( int ) &&";
4033 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4034 {});
4035 }
4036
4037 /* Check that the name matching algorithm for completion doesn't get
4038 confused with Latin1 'ÿ' / 0xff. */
4039 {
4040 static const char str[] = "\377";
4041 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4042 EXPECT ("\377", "\377\377123"));
4043 }
4044
4045 /* Check that the increment-last-char in the matching algorithm for
4046 completion doesn't match "t1_fund" when completing "t1_func". */
4047 {
4048 static const char str[] = "t1_func";
4049 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4050 EXPECT ("t1_func", "t1_func1"));
4051 }
4052
4053 /* Check that completion mode works at each prefix of the expected
4054 symbol name. */
4055 {
4056 static const char str[] = "function(int)";
4057 size_t len = strlen (str);
4058 std::string lookup;
4059
4060 for (size_t i = 1; i < len; i++)
4061 {
4062 lookup.assign (str, i);
4063 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4064 EXPECT ("function"));
4065 }
4066 }
4067
4068 /* While "w" is a prefix of both components, the match function
4069 should still only be called once. */
4070 {
4071 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4072 EXPECT ("w1::w2"));
4073 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4074 EXPECT ("w1::w2"));
4075 }
4076
4077 /* Same, with a "complicated" symbol. */
4078 {
4079 static const char str[] = Z_SYM_NAME;
4080 size_t len = strlen (str);
4081 std::string lookup;
4082
4083 for (size_t i = 1; i < len; i++)
4084 {
4085 lookup.assign (str, i);
4086 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4087 EXPECT (Z_SYM_NAME));
4088 }
4089 }
4090
4091 /* In FULL mode, an incomplete symbol doesn't match. */
4092 {
4093 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4094 {});
4095 }
4096
4097 /* A complete symbol with parameters matches any overload, since the
4098 index has no overload info. */
4099 {
4100 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4101 EXPECT ("std::zfunction", "std::zfunction2"));
4102 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4103 EXPECT ("std::zfunction", "std::zfunction2"));
4104 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4105 EXPECT ("std::zfunction", "std::zfunction2"));
4106 }
4107
4108 /* Check that whitespace is ignored appropriately. A symbol with a
4109 template argument list. */
4110 {
4111 static const char expected[] = "ns::foo<int>";
4112 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4113 EXPECT (expected));
4114 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4115 EXPECT (expected));
4116 }
4117
4118 /* Check that whitespace is ignored appropriately. A symbol with a
4119 template argument list that includes a pointer. */
4120 {
4121 static const char expected[] = "ns::foo<char*>";
4122 /* Try both completion and non-completion modes. */
4123 static const bool completion_mode[2] = {false, true};
4124 for (size_t i = 0; i < 2; i++)
4125 {
4126 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4127 completion_mode[i], EXPECT (expected));
4128 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4129 completion_mode[i], EXPECT (expected));
4130
4131 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4132 completion_mode[i], EXPECT (expected));
4133 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4134 completion_mode[i], EXPECT (expected));
4135 }
4136 }
4137
4138 {
4139 /* Check method qualifiers are ignored. */
4140 static const char expected[] = "ns::foo<char*>";
4141 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4142 symbol_name_match_type::FULL, true, EXPECT (expected));
4143 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4144 symbol_name_match_type::FULL, true, EXPECT (expected));
4145 CHECK_MATCH ("foo < char * > ( int ) const",
4146 symbol_name_match_type::WILD, true, EXPECT (expected));
4147 CHECK_MATCH ("foo < char * > ( int ) &&",
4148 symbol_name_match_type::WILD, true, EXPECT (expected));
4149 }
4150
4151 /* Test lookup names that don't match anything. */
4152 {
4153 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4154 {});
4155
4156 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4157 {});
4158 }
4159
4160 /* Some wild matching tests, exercising "(anonymous namespace)",
4161 which should not be confused with a parameter list. */
4162 {
4163 static const char *syms[] = {
4164 "A::B::C",
4165 "B::C",
4166 "C",
4167 "A :: B :: C ( int )",
4168 "B :: C ( int )",
4169 "C ( int )",
4170 };
4171
4172 for (const char *s : syms)
4173 {
4174 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4175 EXPECT ("(anonymous namespace)::A::B::C"));
4176 }
4177 }
4178
4179 {
4180 static const char expected[] = "ns2::tmpl<int>::foo2";
4181 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4182 EXPECT (expected));
4183 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4184 EXPECT (expected));
4185 }
4186
4187 SELF_CHECK (!any_mismatch);
4188
4189 #undef EXPECT
4190 #undef CHECK_MATCH
4191 }
4192
4193 static void
4194 run_test ()
4195 {
4196 test_mapped_index_find_name_component_bounds ();
4197 test_dw2_expand_symtabs_matching_symbol ();
4198 }
4199
4200 }} // namespace selftests::dw2_expand_symtabs_matching
4201
4202 #endif /* GDB_SELF_TEST */
4203
4204 /* If FILE_MATCHER is NULL or if PER_CU has
4205 dwarf2_per_cu_quick_data::MARK set (see
4206 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4207 EXPANSION_NOTIFY on it. */
4208
4209 static bool
4210 dw2_expand_symtabs_matching_one
4211 (dwarf2_per_cu_data *per_cu,
4212 dwarf2_per_objfile *per_objfile,
4213 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4214 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4215 {
4216 if (file_matcher == NULL || per_cu->v.quick->mark)
4217 {
4218 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4219
4220 compunit_symtab *symtab
4221 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4222 gdb_assert (symtab != nullptr);
4223
4224 if (expansion_notify != NULL && symtab_was_null)
4225 return expansion_notify (symtab);
4226 }
4227 return true;
4228 }
4229
4230 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4231 matched, to expand corresponding CUs that were marked. IDX is the
4232 index of the symbol name that matched. */
4233
4234 static bool
4235 dw2_expand_marked_cus
4236 (dwarf2_per_objfile *per_objfile, offset_type idx,
4237 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4238 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4239 block_search_flags search_flags,
4240 search_domain kind)
4241 {
4242 offset_type vec_len, vec_idx;
4243 bool global_seen = false;
4244 mapped_index &index = *per_objfile->per_bfd->index_table;
4245
4246 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4247 vec_len = vec[0];
4248 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4249 {
4250 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4251 /* This value is only valid for index versions >= 7. */
4252 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4253 gdb_index_symbol_kind symbol_kind =
4254 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4255 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4256 /* Only check the symbol attributes if they're present.
4257 Indices prior to version 7 don't record them,
4258 and indices >= 7 may elide them for certain symbols
4259 (gold does this). */
4260 int attrs_valid =
4261 (index.version >= 7
4262 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4263
4264 /* Work around gold/15646. */
4265 if (attrs_valid
4266 && !is_static
4267 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4268 {
4269 if (global_seen)
4270 continue;
4271
4272 global_seen = true;
4273 }
4274
4275 /* Only check the symbol's kind if it has one. */
4276 if (attrs_valid)
4277 {
4278 if (is_static)
4279 {
4280 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4281 continue;
4282 }
4283 else
4284 {
4285 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4286 continue;
4287 }
4288
4289 switch (kind)
4290 {
4291 case VARIABLES_DOMAIN:
4292 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4293 continue;
4294 break;
4295 case FUNCTIONS_DOMAIN:
4296 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4297 continue;
4298 break;
4299 case TYPES_DOMAIN:
4300 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4301 continue;
4302 break;
4303 case MODULES_DOMAIN:
4304 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4305 continue;
4306 break;
4307 default:
4308 break;
4309 }
4310 }
4311
4312 /* Don't crash on bad data. */
4313 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4314 {
4315 complaint (_(".gdb_index entry has bad CU index"
4316 " [in module %s]"), objfile_name (per_objfile->objfile));
4317 continue;
4318 }
4319
4320 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4321 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4322 expansion_notify))
4323 return false;
4324 }
4325
4326 return true;
4327 }
4328
4329 /* If FILE_MATCHER is non-NULL, set all the
4330 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4331 that match FILE_MATCHER. */
4332
4333 static void
4334 dw_expand_symtabs_matching_file_matcher
4335 (dwarf2_per_objfile *per_objfile,
4336 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4337 {
4338 if (file_matcher == NULL)
4339 return;
4340
4341 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4342 htab_eq_pointer,
4343 NULL, xcalloc, xfree));
4344 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4345 htab_eq_pointer,
4346 NULL, xcalloc, xfree));
4347
4348 /* The rule is CUs specify all the files, including those used by
4349 any TU, so there's no need to scan TUs here. */
4350
4351 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4352 {
4353 QUIT;
4354
4355 if (per_cu->is_debug_types)
4356 continue;
4357 per_cu->v.quick->mark = 0;
4358
4359 /* We only need to look at symtabs not already expanded. */
4360 if (per_objfile->symtab_set_p (per_cu.get ()))
4361 continue;
4362
4363 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4364 per_objfile);
4365 if (file_data == NULL)
4366 continue;
4367
4368 if (htab_find (visited_not_found.get (), file_data) != NULL)
4369 continue;
4370 else if (htab_find (visited_found.get (), file_data) != NULL)
4371 {
4372 per_cu->v.quick->mark = 1;
4373 continue;
4374 }
4375
4376 for (int j = 0; j < file_data->num_file_names; ++j)
4377 {
4378 const char *this_real_name;
4379
4380 if (file_matcher (file_data->file_names[j], false))
4381 {
4382 per_cu->v.quick->mark = 1;
4383 break;
4384 }
4385
4386 /* Before we invoke realpath, which can get expensive when many
4387 files are involved, do a quick comparison of the basenames. */
4388 if (!basenames_may_differ
4389 && !file_matcher (lbasename (file_data->file_names[j]),
4390 true))
4391 continue;
4392
4393 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4394 if (file_matcher (this_real_name, false))
4395 {
4396 per_cu->v.quick->mark = 1;
4397 break;
4398 }
4399 }
4400
4401 void **slot = htab_find_slot (per_cu->v.quick->mark
4402 ? visited_found.get ()
4403 : visited_not_found.get (),
4404 file_data, INSERT);
4405 *slot = file_data;
4406 }
4407 }
4408
4409 bool
4410 dwarf2_gdb_index::expand_symtabs_matching
4411 (struct objfile *objfile,
4412 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4413 const lookup_name_info *lookup_name,
4414 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4415 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4416 block_search_flags search_flags,
4417 domain_enum domain,
4418 enum search_domain kind)
4419 {
4420 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4421
4422 /* index_table is NULL if OBJF_READNOW. */
4423 if (!per_objfile->per_bfd->index_table)
4424 return true;
4425
4426 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4427
4428 /* This invariant is documented in quick-functions.h. */
4429 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4430 if (lookup_name == nullptr)
4431 {
4432 for (dwarf2_per_cu_data *per_cu
4433 : all_comp_units_range (per_objfile->per_bfd))
4434 {
4435 QUIT;
4436
4437 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4438 file_matcher,
4439 expansion_notify))
4440 return false;
4441 }
4442 return true;
4443 }
4444
4445 mapped_index &index = *per_objfile->per_bfd->index_table;
4446
4447 bool result
4448 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4449 symbol_matcher,
4450 [&] (offset_type idx)
4451 {
4452 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4453 expansion_notify, search_flags, kind))
4454 return false;
4455 return true;
4456 }, per_objfile);
4457
4458 return result;
4459 }
4460
4461 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4462 symtab. */
4463
4464 static struct compunit_symtab *
4465 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4466 CORE_ADDR pc)
4467 {
4468 int i;
4469
4470 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4471 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4472 return cust;
4473
4474 if (cust->includes == NULL)
4475 return NULL;
4476
4477 for (i = 0; cust->includes[i]; ++i)
4478 {
4479 struct compunit_symtab *s = cust->includes[i];
4480
4481 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4482 if (s != NULL)
4483 return s;
4484 }
4485
4486 return NULL;
4487 }
4488
4489 struct compunit_symtab *
4490 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4491 (struct objfile *objfile,
4492 struct bound_minimal_symbol msymbol,
4493 CORE_ADDR pc,
4494 struct obj_section *section,
4495 int warn_if_readin)
4496 {
4497 struct dwarf2_per_cu_data *data;
4498 struct compunit_symtab *result;
4499
4500 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4501 if (per_objfile->per_bfd->index_addrmap == nullptr)
4502 return NULL;
4503
4504 CORE_ADDR baseaddr = objfile->text_section_offset ();
4505 data = ((struct dwarf2_per_cu_data *)
4506 addrmap_find (per_objfile->per_bfd->index_addrmap,
4507 pc - baseaddr));
4508 if (!data)
4509 return NULL;
4510
4511 if (warn_if_readin && per_objfile->symtab_set_p (data))
4512 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4513 paddress (objfile->arch (), pc));
4514
4515 result = recursively_find_pc_sect_compunit_symtab
4516 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4517
4518 gdb_assert (result != NULL);
4519 return result;
4520 }
4521
4522 void
4523 dwarf2_base_index_functions::map_symbol_filenames
4524 (struct objfile *objfile,
4525 gdb::function_view<symbol_filename_ftype> fun,
4526 bool need_fullname)
4527 {
4528 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4529
4530 /* Use caches to ensure we only call FUN once for each filename. */
4531 filename_seen_cache filenames_cache;
4532 std::unordered_set<quick_file_names *> qfn_cache;
4533
4534 /* The rule is CUs specify all the files, including those used by any TU,
4535 so there's no need to scan TUs here. We can ignore file names coming
4536 from already-expanded CUs. It is possible that an expanded CU might
4537 reuse the file names data from a currently unexpanded CU, in this
4538 case we don't want to report the files from the unexpanded CU. */
4539
4540 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4541 {
4542 if (!per_cu->is_debug_types
4543 && per_objfile->symtab_set_p (per_cu.get ()))
4544 {
4545 if (per_cu->v.quick->file_names != nullptr)
4546 qfn_cache.insert (per_cu->v.quick->file_names);
4547 }
4548 }
4549
4550 for (dwarf2_per_cu_data *per_cu
4551 : all_comp_units_range (per_objfile->per_bfd))
4552 {
4553 /* We only need to look at symtabs not already expanded. */
4554 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4555 continue;
4556
4557 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4558 if (file_data == nullptr
4559 || qfn_cache.find (file_data) != qfn_cache.end ())
4560 continue;
4561
4562 for (int j = 0; j < file_data->num_file_names; ++j)
4563 {
4564 const char *filename = file_data->file_names[j];
4565 const char *key = filename;
4566 const char *fullname = nullptr;
4567
4568 if (need_fullname)
4569 {
4570 fullname = dw2_get_real_path (per_objfile, file_data, j);
4571 key = fullname;
4572 }
4573
4574 if (!filenames_cache.seen (key))
4575 fun (filename, fullname);
4576 }
4577 }
4578 }
4579
4580 bool
4581 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4582 {
4583 return true;
4584 }
4585
4586 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4587
4588 bool
4589 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4590 {
4591 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4592
4593 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4594 {
4595 /* Is this already expanded? */
4596 if (per_objfile->symtab_set_p (per_cu.get ()))
4597 continue;
4598
4599 /* It has not yet been expanded. */
4600 return true;
4601 }
4602
4603 return false;
4604 }
4605
4606 /* DWARF-5 debug_names reader. */
4607
4608 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4609 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4610
4611 /* A helper function that reads the .debug_names section in SECTION
4612 and fills in MAP. FILENAME is the name of the file containing the
4613 section; it is used for error reporting.
4614
4615 Returns true if all went well, false otherwise. */
4616
4617 static bool
4618 read_debug_names_from_section (struct objfile *objfile,
4619 const char *filename,
4620 struct dwarf2_section_info *section,
4621 mapped_debug_names &map)
4622 {
4623 if (section->empty ())
4624 return false;
4625
4626 /* Older elfutils strip versions could keep the section in the main
4627 executable while splitting it for the separate debug info file. */
4628 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4629 return false;
4630
4631 section->read (objfile);
4632
4633 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4634
4635 const gdb_byte *addr = section->buffer;
4636
4637 bfd *const abfd = section->get_bfd_owner ();
4638
4639 unsigned int bytes_read;
4640 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4641 addr += bytes_read;
4642
4643 map.dwarf5_is_dwarf64 = bytes_read != 4;
4644 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4645 if (bytes_read + length != section->size)
4646 {
4647 /* There may be multiple per-CU indices. */
4648 warning (_("Section .debug_names in %s length %s does not match "
4649 "section length %s, ignoring .debug_names."),
4650 filename, plongest (bytes_read + length),
4651 pulongest (section->size));
4652 return false;
4653 }
4654
4655 /* The version number. */
4656 uint16_t version = read_2_bytes (abfd, addr);
4657 addr += 2;
4658 if (version != 5)
4659 {
4660 warning (_("Section .debug_names in %s has unsupported version %d, "
4661 "ignoring .debug_names."),
4662 filename, version);
4663 return false;
4664 }
4665
4666 /* Padding. */
4667 uint16_t padding = read_2_bytes (abfd, addr);
4668 addr += 2;
4669 if (padding != 0)
4670 {
4671 warning (_("Section .debug_names in %s has unsupported padding %d, "
4672 "ignoring .debug_names."),
4673 filename, padding);
4674 return false;
4675 }
4676
4677 /* comp_unit_count - The number of CUs in the CU list. */
4678 map.cu_count = read_4_bytes (abfd, addr);
4679 addr += 4;
4680
4681 /* local_type_unit_count - The number of TUs in the local TU
4682 list. */
4683 map.tu_count = read_4_bytes (abfd, addr);
4684 addr += 4;
4685
4686 /* foreign_type_unit_count - The number of TUs in the foreign TU
4687 list. */
4688 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4689 addr += 4;
4690 if (foreign_tu_count != 0)
4691 {
4692 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4693 "ignoring .debug_names."),
4694 filename, static_cast<unsigned long> (foreign_tu_count));
4695 return false;
4696 }
4697
4698 /* bucket_count - The number of hash buckets in the hash lookup
4699 table. */
4700 map.bucket_count = read_4_bytes (abfd, addr);
4701 addr += 4;
4702
4703 /* name_count - The number of unique names in the index. */
4704 map.name_count = read_4_bytes (abfd, addr);
4705 addr += 4;
4706
4707 /* abbrev_table_size - The size in bytes of the abbreviations
4708 table. */
4709 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4710 addr += 4;
4711
4712 /* augmentation_string_size - The size in bytes of the augmentation
4713 string. This value is rounded up to a multiple of 4. */
4714 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4715 addr += 4;
4716 map.augmentation_is_gdb = ((augmentation_string_size
4717 == sizeof (dwarf5_augmentation))
4718 && memcmp (addr, dwarf5_augmentation,
4719 sizeof (dwarf5_augmentation)) == 0);
4720 augmentation_string_size += (-augmentation_string_size) & 3;
4721 addr += augmentation_string_size;
4722
4723 /* List of CUs */
4724 map.cu_table_reordered = addr;
4725 addr += map.cu_count * map.offset_size;
4726
4727 /* List of Local TUs */
4728 map.tu_table_reordered = addr;
4729 addr += map.tu_count * map.offset_size;
4730
4731 /* Hash Lookup Table */
4732 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4733 addr += map.bucket_count * 4;
4734 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4735 addr += map.name_count * 4;
4736
4737 /* Name Table */
4738 map.name_table_string_offs_reordered = addr;
4739 addr += map.name_count * map.offset_size;
4740 map.name_table_entry_offs_reordered = addr;
4741 addr += map.name_count * map.offset_size;
4742
4743 const gdb_byte *abbrev_table_start = addr;
4744 for (;;)
4745 {
4746 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4747 addr += bytes_read;
4748 if (index_num == 0)
4749 break;
4750
4751 const auto insertpair
4752 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4753 if (!insertpair.second)
4754 {
4755 warning (_("Section .debug_names in %s has duplicate index %s, "
4756 "ignoring .debug_names."),
4757 filename, pulongest (index_num));
4758 return false;
4759 }
4760 mapped_debug_names::index_val &indexval = insertpair.first->second;
4761 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4762 addr += bytes_read;
4763
4764 for (;;)
4765 {
4766 mapped_debug_names::index_val::attr attr;
4767 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4768 addr += bytes_read;
4769 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4770 addr += bytes_read;
4771 if (attr.form == DW_FORM_implicit_const)
4772 {
4773 attr.implicit_const = read_signed_leb128 (abfd, addr,
4774 &bytes_read);
4775 addr += bytes_read;
4776 }
4777 if (attr.dw_idx == 0 && attr.form == 0)
4778 break;
4779 indexval.attr_vec.push_back (std::move (attr));
4780 }
4781 }
4782 if (addr != abbrev_table_start + abbrev_table_size)
4783 {
4784 warning (_("Section .debug_names in %s has abbreviation_table "
4785 "of size %s vs. written as %u, ignoring .debug_names."),
4786 filename, plongest (addr - abbrev_table_start),
4787 abbrev_table_size);
4788 return false;
4789 }
4790 map.entry_pool = addr;
4791
4792 return true;
4793 }
4794
4795 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4796 list. */
4797
4798 static void
4799 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4800 const mapped_debug_names &map,
4801 dwarf2_section_info &section,
4802 bool is_dwz)
4803 {
4804 if (!map.augmentation_is_gdb)
4805 {
4806 for (uint32_t i = 0; i < map.cu_count; ++i)
4807 {
4808 sect_offset sect_off
4809 = (sect_offset) (extract_unsigned_integer
4810 (map.cu_table_reordered + i * map.offset_size,
4811 map.offset_size,
4812 map.dwarf5_byte_order));
4813 /* We don't know the length of the CU, because the CU list in a
4814 .debug_names index can be incomplete, so we can't use the start
4815 of the next CU as end of this CU. We create the CUs here with
4816 length 0, and in cutu_reader::cutu_reader we'll fill in the
4817 actual length. */
4818 dwarf2_per_cu_data_up per_cu
4819 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4820 sect_off, 0);
4821 per_bfd->all_comp_units.push_back (std::move (per_cu));
4822 }
4823 return;
4824 }
4825
4826 sect_offset sect_off_prev;
4827 for (uint32_t i = 0; i <= map.cu_count; ++i)
4828 {
4829 sect_offset sect_off_next;
4830 if (i < map.cu_count)
4831 {
4832 sect_off_next
4833 = (sect_offset) (extract_unsigned_integer
4834 (map.cu_table_reordered + i * map.offset_size,
4835 map.offset_size,
4836 map.dwarf5_byte_order));
4837 }
4838 else
4839 sect_off_next = (sect_offset) section.size;
4840 if (i >= 1)
4841 {
4842 const ULONGEST length = sect_off_next - sect_off_prev;
4843 dwarf2_per_cu_data_up per_cu
4844 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4845 sect_off_prev, length);
4846 per_bfd->all_comp_units.push_back (std::move (per_cu));
4847 }
4848 sect_off_prev = sect_off_next;
4849 }
4850 }
4851
4852 /* Read the CU list from the mapped index, and use it to create all
4853 the CU objects for this dwarf2_per_objfile. */
4854
4855 static void
4856 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4857 const mapped_debug_names &map,
4858 const mapped_debug_names &dwz_map)
4859 {
4860 gdb_assert (per_bfd->all_comp_units.empty ());
4861 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4862
4863 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4864 false /* is_dwz */);
4865
4866 if (dwz_map.cu_count == 0)
4867 return;
4868
4869 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4870 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4871 true /* is_dwz */);
4872 }
4873
4874 /* Read .debug_names. If everything went ok, initialize the "quick"
4875 elements of all the CUs and return true. Otherwise, return false. */
4876
4877 static bool
4878 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4879 {
4880 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4881 mapped_debug_names dwz_map;
4882 struct objfile *objfile = per_objfile->objfile;
4883 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4884
4885 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4886 &per_bfd->debug_names, *map))
4887 return false;
4888
4889 /* Don't use the index if it's empty. */
4890 if (map->name_count == 0)
4891 return false;
4892
4893 /* If there is a .dwz file, read it so we can get its CU list as
4894 well. */
4895 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4896 if (dwz != NULL)
4897 {
4898 if (!read_debug_names_from_section (objfile,
4899 bfd_get_filename (dwz->dwz_bfd.get ()),
4900 &dwz->debug_names, dwz_map))
4901 {
4902 warning (_("could not read '.debug_names' section from %s; skipping"),
4903 bfd_get_filename (dwz->dwz_bfd.get ()));
4904 return false;
4905 }
4906 }
4907
4908 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4909
4910 if (map->tu_count != 0)
4911 {
4912 /* We can only handle a single .debug_types when we have an
4913 index. */
4914 if (per_bfd->types.size () != 1)
4915 return false;
4916
4917 dwarf2_section_info *section = &per_bfd->types[0];
4918
4919 create_signatured_type_table_from_debug_names
4920 (per_objfile, *map, section, &per_bfd->abbrev);
4921 }
4922
4923 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4924
4925 per_bfd->debug_names_table = std::move (map);
4926 per_bfd->using_index = 1;
4927 per_bfd->quick_file_names_table =
4928 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4929
4930 return true;
4931 }
4932
4933 /* Type used to manage iterating over all CUs looking for a symbol for
4934 .debug_names. */
4935
4936 class dw2_debug_names_iterator
4937 {
4938 public:
4939 dw2_debug_names_iterator (const mapped_debug_names &map,
4940 block_search_flags block_index,
4941 domain_enum domain,
4942 const char *name, dwarf2_per_objfile *per_objfile)
4943 : m_map (map), m_block_index (block_index), m_domain (domain),
4944 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4945 m_per_objfile (per_objfile)
4946 {}
4947
4948 dw2_debug_names_iterator (const mapped_debug_names &map,
4949 search_domain search, uint32_t namei,
4950 dwarf2_per_objfile *per_objfile,
4951 domain_enum domain = UNDEF_DOMAIN)
4952 : m_map (map),
4953 m_domain (domain),
4954 m_search (search),
4955 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4956 m_per_objfile (per_objfile)
4957 {}
4958
4959 dw2_debug_names_iterator (const mapped_debug_names &map,
4960 block_search_flags block_index, domain_enum domain,
4961 uint32_t namei, dwarf2_per_objfile *per_objfile)
4962 : m_map (map), m_block_index (block_index), m_domain (domain),
4963 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4964 m_per_objfile (per_objfile)
4965 {}
4966
4967 /* Return the next matching CU or NULL if there are no more. */
4968 dwarf2_per_cu_data *next ();
4969
4970 private:
4971 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4972 const char *name,
4973 dwarf2_per_objfile *per_objfile);
4974 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4975 uint32_t namei,
4976 dwarf2_per_objfile *per_objfile);
4977
4978 /* The internalized form of .debug_names. */
4979 const mapped_debug_names &m_map;
4980
4981 /* Restrict the search to these blocks. */
4982 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4983 | SEARCH_STATIC_BLOCK);
4984
4985 /* The kind of symbol we're looking for. */
4986 const domain_enum m_domain = UNDEF_DOMAIN;
4987 const search_domain m_search = ALL_DOMAIN;
4988
4989 /* The list of CUs from the index entry of the symbol, or NULL if
4990 not found. */
4991 const gdb_byte *m_addr;
4992
4993 dwarf2_per_objfile *m_per_objfile;
4994 };
4995
4996 const char *
4997 mapped_debug_names::namei_to_name
4998 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4999 {
5000 const ULONGEST namei_string_offs
5001 = extract_unsigned_integer ((name_table_string_offs_reordered
5002 + namei * offset_size),
5003 offset_size,
5004 dwarf5_byte_order);
5005 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5006 }
5007
5008 /* Find a slot in .debug_names for the object named NAME. If NAME is
5009 found, return pointer to its pool data. If NAME cannot be found,
5010 return NULL. */
5011
5012 const gdb_byte *
5013 dw2_debug_names_iterator::find_vec_in_debug_names
5014 (const mapped_debug_names &map, const char *name,
5015 dwarf2_per_objfile *per_objfile)
5016 {
5017 int (*cmp) (const char *, const char *);
5018
5019 gdb::unique_xmalloc_ptr<char> without_params;
5020 if (current_language->la_language == language_cplus
5021 || current_language->la_language == language_fortran
5022 || current_language->la_language == language_d)
5023 {
5024 /* NAME is already canonical. Drop any qualifiers as
5025 .debug_names does not contain any. */
5026
5027 if (strchr (name, '(') != NULL)
5028 {
5029 without_params = cp_remove_params (name);
5030 if (without_params != NULL)
5031 name = without_params.get ();
5032 }
5033 }
5034
5035 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5036
5037 const uint32_t full_hash = dwarf5_djb_hash (name);
5038 uint32_t namei
5039 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5040 (map.bucket_table_reordered
5041 + (full_hash % map.bucket_count)), 4,
5042 map.dwarf5_byte_order);
5043 if (namei == 0)
5044 return NULL;
5045 --namei;
5046 if (namei >= map.name_count)
5047 {
5048 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5049 "[in module %s]"),
5050 namei, map.name_count,
5051 objfile_name (per_objfile->objfile));
5052 return NULL;
5053 }
5054
5055 for (;;)
5056 {
5057 const uint32_t namei_full_hash
5058 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5059 (map.hash_table_reordered + namei), 4,
5060 map.dwarf5_byte_order);
5061 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5062 return NULL;
5063
5064 if (full_hash == namei_full_hash)
5065 {
5066 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5067
5068 #if 0 /* An expensive sanity check. */
5069 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5070 {
5071 complaint (_("Wrong .debug_names hash for string at index %u "
5072 "[in module %s]"),
5073 namei, objfile_name (dwarf2_per_objfile->objfile));
5074 return NULL;
5075 }
5076 #endif
5077
5078 if (cmp (namei_string, name) == 0)
5079 {
5080 const ULONGEST namei_entry_offs
5081 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5082 + namei * map.offset_size),
5083 map.offset_size, map.dwarf5_byte_order);
5084 return map.entry_pool + namei_entry_offs;
5085 }
5086 }
5087
5088 ++namei;
5089 if (namei >= map.name_count)
5090 return NULL;
5091 }
5092 }
5093
5094 const gdb_byte *
5095 dw2_debug_names_iterator::find_vec_in_debug_names
5096 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5097 {
5098 if (namei >= map.name_count)
5099 {
5100 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5101 "[in module %s]"),
5102 namei, map.name_count,
5103 objfile_name (per_objfile->objfile));
5104 return NULL;
5105 }
5106
5107 const ULONGEST namei_entry_offs
5108 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5109 + namei * map.offset_size),
5110 map.offset_size, map.dwarf5_byte_order);
5111 return map.entry_pool + namei_entry_offs;
5112 }
5113
5114 /* See dw2_debug_names_iterator. */
5115
5116 dwarf2_per_cu_data *
5117 dw2_debug_names_iterator::next ()
5118 {
5119 if (m_addr == NULL)
5120 return NULL;
5121
5122 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5123 struct objfile *objfile = m_per_objfile->objfile;
5124 bfd *const abfd = objfile->obfd;
5125
5126 again:
5127
5128 unsigned int bytes_read;
5129 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5130 m_addr += bytes_read;
5131 if (abbrev == 0)
5132 return NULL;
5133
5134 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5135 if (indexval_it == m_map.abbrev_map.cend ())
5136 {
5137 complaint (_("Wrong .debug_names undefined abbrev code %s "
5138 "[in module %s]"),
5139 pulongest (abbrev), objfile_name (objfile));
5140 return NULL;
5141 }
5142 const mapped_debug_names::index_val &indexval = indexval_it->second;
5143 enum class symbol_linkage {
5144 unknown,
5145 static_,
5146 extern_,
5147 } symbol_linkage_ = symbol_linkage::unknown;
5148 dwarf2_per_cu_data *per_cu = NULL;
5149 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5150 {
5151 ULONGEST ull;
5152 switch (attr.form)
5153 {
5154 case DW_FORM_implicit_const:
5155 ull = attr.implicit_const;
5156 break;
5157 case DW_FORM_flag_present:
5158 ull = 1;
5159 break;
5160 case DW_FORM_udata:
5161 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5162 m_addr += bytes_read;
5163 break;
5164 case DW_FORM_ref4:
5165 ull = read_4_bytes (abfd, m_addr);
5166 m_addr += 4;
5167 break;
5168 case DW_FORM_ref8:
5169 ull = read_8_bytes (abfd, m_addr);
5170 m_addr += 8;
5171 break;
5172 case DW_FORM_ref_sig8:
5173 ull = read_8_bytes (abfd, m_addr);
5174 m_addr += 8;
5175 break;
5176 default:
5177 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5178 dwarf_form_name (attr.form),
5179 objfile_name (objfile));
5180 return NULL;
5181 }
5182 switch (attr.dw_idx)
5183 {
5184 case DW_IDX_compile_unit:
5185 /* Don't crash on bad data. */
5186 if (ull >= per_bfd->all_comp_units.size ())
5187 {
5188 complaint (_(".debug_names entry has bad CU index %s"
5189 " [in module %s]"),
5190 pulongest (ull),
5191 objfile_name (objfile));
5192 continue;
5193 }
5194 per_cu = per_bfd->get_cu (ull);
5195 break;
5196 case DW_IDX_type_unit:
5197 /* Don't crash on bad data. */
5198 if (ull >= per_bfd->tu_stats.nr_tus)
5199 {
5200 complaint (_(".debug_names entry has bad TU index %s"
5201 " [in module %s]"),
5202 pulongest (ull),
5203 objfile_name (objfile));
5204 continue;
5205 }
5206 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5207 break;
5208 case DW_IDX_die_offset:
5209 /* In a per-CU index (as opposed to a per-module index), index
5210 entries without CU attribute implicitly refer to the single CU. */
5211 if (per_cu == NULL)
5212 per_cu = per_bfd->get_cu (0);
5213 break;
5214 case DW_IDX_GNU_internal:
5215 if (!m_map.augmentation_is_gdb)
5216 break;
5217 symbol_linkage_ = symbol_linkage::static_;
5218 break;
5219 case DW_IDX_GNU_external:
5220 if (!m_map.augmentation_is_gdb)
5221 break;
5222 symbol_linkage_ = symbol_linkage::extern_;
5223 break;
5224 }
5225 }
5226
5227 /* Skip if already read in. */
5228 if (m_per_objfile->symtab_set_p (per_cu))
5229 goto again;
5230
5231 /* Check static vs global. */
5232 if (symbol_linkage_ != symbol_linkage::unknown)
5233 {
5234 if (symbol_linkage_ == symbol_linkage::static_)
5235 {
5236 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5237 goto again;
5238 }
5239 else
5240 {
5241 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5242 goto again;
5243 }
5244 }
5245
5246 /* Match dw2_symtab_iter_next, symbol_kind
5247 and debug_names::psymbol_tag. */
5248 switch (m_domain)
5249 {
5250 case VAR_DOMAIN:
5251 switch (indexval.dwarf_tag)
5252 {
5253 case DW_TAG_variable:
5254 case DW_TAG_subprogram:
5255 /* Some types are also in VAR_DOMAIN. */
5256 case DW_TAG_typedef:
5257 case DW_TAG_structure_type:
5258 break;
5259 default:
5260 goto again;
5261 }
5262 break;
5263 case STRUCT_DOMAIN:
5264 switch (indexval.dwarf_tag)
5265 {
5266 case DW_TAG_typedef:
5267 case DW_TAG_structure_type:
5268 break;
5269 default:
5270 goto again;
5271 }
5272 break;
5273 case LABEL_DOMAIN:
5274 switch (indexval.dwarf_tag)
5275 {
5276 case 0:
5277 case DW_TAG_variable:
5278 break;
5279 default:
5280 goto again;
5281 }
5282 break;
5283 case MODULE_DOMAIN:
5284 switch (indexval.dwarf_tag)
5285 {
5286 case DW_TAG_module:
5287 break;
5288 default:
5289 goto again;
5290 }
5291 break;
5292 default:
5293 break;
5294 }
5295
5296 /* Match dw2_expand_symtabs_matching, symbol_kind and
5297 debug_names::psymbol_tag. */
5298 switch (m_search)
5299 {
5300 case VARIABLES_DOMAIN:
5301 switch (indexval.dwarf_tag)
5302 {
5303 case DW_TAG_variable:
5304 break;
5305 default:
5306 goto again;
5307 }
5308 break;
5309 case FUNCTIONS_DOMAIN:
5310 switch (indexval.dwarf_tag)
5311 {
5312 case DW_TAG_subprogram:
5313 break;
5314 default:
5315 goto again;
5316 }
5317 break;
5318 case TYPES_DOMAIN:
5319 switch (indexval.dwarf_tag)
5320 {
5321 case DW_TAG_typedef:
5322 case DW_TAG_structure_type:
5323 break;
5324 default:
5325 goto again;
5326 }
5327 break;
5328 case MODULES_DOMAIN:
5329 switch (indexval.dwarf_tag)
5330 {
5331 case DW_TAG_module:
5332 break;
5333 default:
5334 goto again;
5335 }
5336 default:
5337 break;
5338 }
5339
5340 return per_cu;
5341 }
5342
5343 /* This dumps minimal information about .debug_names. It is called
5344 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5345 uses this to verify that .debug_names has been loaded. */
5346
5347 void
5348 dwarf2_debug_names_index::dump (struct objfile *objfile)
5349 {
5350 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5351
5352 gdb_assert (per_objfile->per_bfd->using_index);
5353 printf_filtered (".debug_names:");
5354 if (per_objfile->per_bfd->debug_names_table)
5355 printf_filtered (" exists\n");
5356 else
5357 printf_filtered (" faked for \"readnow\"\n");
5358 printf_filtered ("\n");
5359 }
5360
5361 void
5362 dwarf2_debug_names_index::expand_matching_symbols
5363 (struct objfile *objfile,
5364 const lookup_name_info &name, domain_enum domain,
5365 int global,
5366 symbol_compare_ftype *ordered_compare)
5367 {
5368 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5369
5370 /* debug_names_table is NULL if OBJF_READNOW. */
5371 if (!per_objfile->per_bfd->debug_names_table)
5372 return;
5373
5374 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5375 const block_search_flags block_flags
5376 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5377
5378 const char *match_name = name.ada ().lookup_name ().c_str ();
5379 auto matcher = [&] (const char *symname)
5380 {
5381 if (ordered_compare == nullptr)
5382 return true;
5383 return ordered_compare (symname, match_name) == 0;
5384 };
5385
5386 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5387 [&] (offset_type namei)
5388 {
5389 /* The name was matched, now expand corresponding CUs that were
5390 marked. */
5391 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5392 per_objfile);
5393
5394 struct dwarf2_per_cu_data *per_cu;
5395 while ((per_cu = iter.next ()) != NULL)
5396 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5397 nullptr);
5398 return true;
5399 }, per_objfile);
5400 }
5401
5402 bool
5403 dwarf2_debug_names_index::expand_symtabs_matching
5404 (struct objfile *objfile,
5405 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5406 const lookup_name_info *lookup_name,
5407 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5408 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5409 block_search_flags search_flags,
5410 domain_enum domain,
5411 enum search_domain kind)
5412 {
5413 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5414
5415 /* debug_names_table is NULL if OBJF_READNOW. */
5416 if (!per_objfile->per_bfd->debug_names_table)
5417 return true;
5418
5419 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5420
5421 /* This invariant is documented in quick-functions.h. */
5422 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5423 if (lookup_name == nullptr)
5424 {
5425 for (dwarf2_per_cu_data *per_cu
5426 : all_comp_units_range (per_objfile->per_bfd))
5427 {
5428 QUIT;
5429
5430 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5431 file_matcher,
5432 expansion_notify))
5433 return false;
5434 }
5435 return true;
5436 }
5437
5438 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5439
5440 bool result
5441 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5442 symbol_matcher,
5443 [&] (offset_type namei)
5444 {
5445 /* The name was matched, now expand corresponding CUs that were
5446 marked. */
5447 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5448
5449 struct dwarf2_per_cu_data *per_cu;
5450 while ((per_cu = iter.next ()) != NULL)
5451 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5452 file_matcher,
5453 expansion_notify))
5454 return false;
5455 return true;
5456 }, per_objfile);
5457
5458 return result;
5459 }
5460
5461 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5462 to either a dwarf2_per_bfd or dwz_file object. */
5463
5464 template <typename T>
5465 static gdb::array_view<const gdb_byte>
5466 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5467 {
5468 dwarf2_section_info *section = &section_owner->gdb_index;
5469
5470 if (section->empty ())
5471 return {};
5472
5473 /* Older elfutils strip versions could keep the section in the main
5474 executable while splitting it for the separate debug info file. */
5475 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5476 return {};
5477
5478 section->read (obj);
5479
5480 /* dwarf2_section_info::size is a bfd_size_type, while
5481 gdb::array_view works with size_t. On 32-bit hosts, with
5482 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5483 is 32-bit. So we need an explicit narrowing conversion here.
5484 This is fine, because it's impossible to allocate or mmap an
5485 array/buffer larger than what size_t can represent. */
5486 return gdb::make_array_view (section->buffer, section->size);
5487 }
5488
5489 /* Lookup the index cache for the contents of the index associated to
5490 DWARF2_OBJ. */
5491
5492 static gdb::array_view<const gdb_byte>
5493 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5494 {
5495 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5496 if (build_id == nullptr)
5497 return {};
5498
5499 return global_index_cache.lookup_gdb_index (build_id,
5500 &dwarf2_per_bfd->index_cache_res);
5501 }
5502
5503 /* Same as the above, but for DWZ. */
5504
5505 static gdb::array_view<const gdb_byte>
5506 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5507 {
5508 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5509 if (build_id == nullptr)
5510 return {};
5511
5512 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5513 }
5514
5515 /* See dwarf2/public.h. */
5516
5517 void
5518 dwarf2_initialize_objfile (struct objfile *objfile)
5519 {
5520 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5521 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5522
5523 dwarf_read_debug_printf ("called");
5524
5525 /* If we're about to read full symbols, don't bother with the
5526 indices. In this case we also don't care if some other debug
5527 format is making psymtabs, because they are all about to be
5528 expanded anyway. */
5529 if ((objfile->flags & OBJF_READNOW))
5530 {
5531 dwarf_read_debug_printf ("readnow requested");
5532
5533 /* When using READNOW, the using_index flag (set below) indicates that
5534 PER_BFD was already initialized, when we loaded some other objfile. */
5535 if (per_bfd->using_index)
5536 {
5537 dwarf_read_debug_printf ("using_index already set");
5538 objfile->qf.push_front (make_dwarf_gdb_index ());
5539 return;
5540 }
5541
5542 per_bfd->using_index = 1;
5543 create_all_comp_units (per_objfile);
5544 per_bfd->quick_file_names_table
5545 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5546
5547 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5548 {
5549 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5550
5551 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5552 struct dwarf2_per_cu_quick_data);
5553 }
5554
5555 /* Arrange for gdb to see the "quick" functions. However, these
5556 functions will be no-ops because we will have expanded all
5557 symtabs. */
5558 objfile->qf.push_front (make_dwarf_gdb_index ());
5559 return;
5560 }
5561
5562 /* Was a debug names index already read when we processed an objfile sharing
5563 PER_BFD? */
5564 if (per_bfd->debug_names_table != nullptr)
5565 {
5566 dwarf_read_debug_printf ("re-using shared debug names table");
5567 objfile->qf.push_front (make_dwarf_debug_names ());
5568 return;
5569 }
5570
5571 /* Was a GDB index already read when we processed an objfile sharing
5572 PER_BFD? */
5573 if (per_bfd->index_table != nullptr)
5574 {
5575 dwarf_read_debug_printf ("re-using shared index table");
5576 objfile->qf.push_front (make_dwarf_gdb_index ());
5577 return;
5578 }
5579
5580 /* There might already be partial symtabs built for this BFD. This happens
5581 when loading the same binary twice with the index-cache enabled. If so,
5582 don't try to read an index. The objfile / per_objfile initialization will
5583 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5584 code path. */
5585 if (per_bfd->partial_symtabs != nullptr)
5586 {
5587 dwarf_read_debug_printf ("re-using shared partial symtabs");
5588 objfile->qf.push_front (make_lazy_dwarf_reader ());
5589 return;
5590 }
5591
5592 if (dwarf2_read_debug_names (per_objfile))
5593 {
5594 dwarf_read_debug_printf ("found debug names");
5595 objfile->qf.push_front (make_dwarf_debug_names ());
5596 return;
5597 }
5598
5599 if (dwarf2_read_gdb_index (per_objfile,
5600 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5601 get_gdb_index_contents_from_section<dwz_file>))
5602 {
5603 dwarf_read_debug_printf ("found gdb index from file");
5604 objfile->qf.push_front (make_dwarf_gdb_index ());
5605 return;
5606 }
5607
5608 /* ... otherwise, try to find the index in the index cache. */
5609 if (dwarf2_read_gdb_index (per_objfile,
5610 get_gdb_index_contents_from_cache,
5611 get_gdb_index_contents_from_cache_dwz))
5612 {
5613 dwarf_read_debug_printf ("found gdb index from cache");
5614 global_index_cache.hit ();
5615 objfile->qf.push_front (make_dwarf_gdb_index ());
5616 return;
5617 }
5618
5619 global_index_cache.miss ();
5620 objfile->qf.push_front (make_lazy_dwarf_reader ());
5621 }
5622
5623 \f
5624
5625 /* Build a partial symbol table. */
5626
5627 void
5628 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5629 {
5630 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5631 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5632
5633 if (per_bfd->partial_symtabs != nullptr)
5634 {
5635 /* Partial symbols were already read, so now we can simply
5636 attach them. */
5637 if (psf == nullptr)
5638 {
5639 psf = new psymbol_functions (per_bfd->partial_symtabs);
5640 objfile->qf.emplace_front (psf);
5641 }
5642 else
5643 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5644 return;
5645 }
5646
5647 if (psf == nullptr)
5648 {
5649 psf = new psymbol_functions;
5650 objfile->qf.emplace_front (psf);
5651 }
5652 const std::shared_ptr<psymtab_storage> &partial_symtabs
5653 = psf->get_partial_symtabs ();
5654
5655 /* Set the local reference to partial symtabs, so that we don't try
5656 to read them again if reading another objfile with the same BFD.
5657 If we can't in fact share, this won't make a difference anyway as
5658 the dwarf2_per_bfd object won't be shared. */
5659 per_bfd->partial_symtabs = partial_symtabs;
5660
5661 try
5662 {
5663 /* This isn't really ideal: all the data we allocate on the
5664 objfile's obstack is still uselessly kept around. However,
5665 freeing it seems unsafe. */
5666 psymtab_discarder psymtabs (partial_symtabs.get ());
5667 dwarf2_build_psymtabs_hard (per_objfile);
5668 psymtabs.keep ();
5669
5670 /* (maybe) store an index in the cache. */
5671 global_index_cache.store (per_objfile);
5672 }
5673 catch (const gdb_exception_error &except)
5674 {
5675 exception_print (gdb_stderr, except);
5676 }
5677 }
5678
5679 /* Find the base address of the compilation unit for range lists and
5680 location lists. It will normally be specified by DW_AT_low_pc.
5681 In DWARF-3 draft 4, the base address could be overridden by
5682 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5683 compilation units with discontinuous ranges. */
5684
5685 static void
5686 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5687 {
5688 struct attribute *attr;
5689
5690 cu->base_address.reset ();
5691
5692 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5693 if (attr != nullptr)
5694 cu->base_address = attr->as_address ();
5695 else
5696 {
5697 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5698 if (attr != nullptr)
5699 cu->base_address = attr->as_address ();
5700 }
5701 }
5702
5703 /* Helper function that returns the proper abbrev section for
5704 THIS_CU. */
5705
5706 static struct dwarf2_section_info *
5707 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5708 {
5709 struct dwarf2_section_info *abbrev;
5710 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5711
5712 if (this_cu->is_dwz)
5713 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5714 else
5715 abbrev = &per_bfd->abbrev;
5716
5717 return abbrev;
5718 }
5719
5720 /* Fetch the abbreviation table offset from a comp or type unit header. */
5721
5722 static sect_offset
5723 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5724 struct dwarf2_section_info *section,
5725 sect_offset sect_off)
5726 {
5727 bfd *abfd = section->get_bfd_owner ();
5728 const gdb_byte *info_ptr;
5729 unsigned int initial_length_size, offset_size;
5730 uint16_t version;
5731
5732 section->read (per_objfile->objfile);
5733 info_ptr = section->buffer + to_underlying (sect_off);
5734 read_initial_length (abfd, info_ptr, &initial_length_size);
5735 offset_size = initial_length_size == 4 ? 4 : 8;
5736 info_ptr += initial_length_size;
5737
5738 version = read_2_bytes (abfd, info_ptr);
5739 info_ptr += 2;
5740 if (version >= 5)
5741 {
5742 /* Skip unit type and address size. */
5743 info_ptr += 2;
5744 }
5745
5746 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5747 }
5748
5749 /* A partial symtab that is used only for include files. */
5750 struct dwarf2_include_psymtab : public partial_symtab
5751 {
5752 dwarf2_include_psymtab (const char *filename,
5753 psymtab_storage *partial_symtabs,
5754 objfile_per_bfd_storage *objfile_per_bfd)
5755 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5756 {
5757 }
5758
5759 void read_symtab (struct objfile *objfile) override
5760 {
5761 /* It's an include file, no symbols to read for it.
5762 Everything is in the includer symtab. */
5763
5764 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5765 expansion of the includer psymtab. We use the dependencies[0] field to
5766 model the includer. But if we go the regular route of calling
5767 expand_psymtab here, and having expand_psymtab call expand_dependencies
5768 to expand the includer, we'll only use expand_psymtab on the includer
5769 (making it a non-toplevel psymtab), while if we expand the includer via
5770 another path, we'll use read_symtab (making it a toplevel psymtab).
5771 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5772 psymtab, and trigger read_symtab on the includer here directly. */
5773 includer ()->read_symtab (objfile);
5774 }
5775
5776 void expand_psymtab (struct objfile *objfile) override
5777 {
5778 /* This is not called by read_symtab, and should not be called by any
5779 expand_dependencies. */
5780 gdb_assert (false);
5781 }
5782
5783 bool readin_p (struct objfile *objfile) const override
5784 {
5785 return includer ()->readin_p (objfile);
5786 }
5787
5788 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5789 {
5790 return nullptr;
5791 }
5792
5793 private:
5794 partial_symtab *includer () const
5795 {
5796 /* An include psymtab has exactly one dependency: the psymtab that
5797 includes it. */
5798 gdb_assert (this->number_of_dependencies == 1);
5799 return this->dependencies[0];
5800 }
5801 };
5802
5803 /* Allocate a new partial symtab for file named NAME and mark this new
5804 partial symtab as being an include of PST. */
5805
5806 static void
5807 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5808 const char *name,
5809 dwarf2_psymtab *pst,
5810 psymtab_storage *partial_symtabs,
5811 objfile_per_bfd_storage *objfile_per_bfd)
5812 {
5813 dwarf2_include_psymtab *subpst
5814 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5815
5816 if (!IS_ABSOLUTE_PATH (subpst->filename))
5817 subpst->dirname = pst->dirname;
5818
5819 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5820 subpst->dependencies[0] = pst;
5821 subpst->number_of_dependencies = 1;
5822 }
5823
5824 /* Read the Line Number Program data and extract the list of files
5825 included by the source file represented by PST. Build an include
5826 partial symtab for each of these included files. */
5827
5828 static void
5829 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5830 struct die_info *die,
5831 const file_and_directory &fnd,
5832 dwarf2_psymtab *pst)
5833 {
5834 line_header_up lh;
5835 struct attribute *attr;
5836
5837 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5838 if (attr != nullptr && attr->form_is_unsigned ())
5839 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5840 if (lh == NULL)
5841 return; /* No linetable, so no includes. */
5842
5843 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5844 that we pass in the raw text_low here; that is ok because we're
5845 only decoding the line table to make include partial symtabs, and
5846 so the addresses aren't really used. */
5847 dwarf_decode_lines (lh.get (), fnd, cu, pst,
5848 pst->raw_text_low (), 1);
5849 }
5850
5851 static hashval_t
5852 hash_signatured_type (const void *item)
5853 {
5854 const struct signatured_type *sig_type
5855 = (const struct signatured_type *) item;
5856
5857 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5858 return sig_type->signature;
5859 }
5860
5861 static int
5862 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5863 {
5864 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5865 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5866
5867 return lhs->signature == rhs->signature;
5868 }
5869
5870 /* Allocate a hash table for signatured types. */
5871
5872 static htab_up
5873 allocate_signatured_type_table ()
5874 {
5875 return htab_up (htab_create_alloc (41,
5876 hash_signatured_type,
5877 eq_signatured_type,
5878 NULL, xcalloc, xfree));
5879 }
5880
5881 /* A helper for create_debug_types_hash_table. Read types from SECTION
5882 and fill them into TYPES_HTAB. It will process only type units,
5883 therefore DW_UT_type. */
5884
5885 static void
5886 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5887 struct dwo_file *dwo_file,
5888 dwarf2_section_info *section, htab_up &types_htab,
5889 rcuh_kind section_kind)
5890 {
5891 struct objfile *objfile = per_objfile->objfile;
5892 struct dwarf2_section_info *abbrev_section;
5893 bfd *abfd;
5894 const gdb_byte *info_ptr, *end_ptr;
5895
5896 abbrev_section = &dwo_file->sections.abbrev;
5897
5898 dwarf_read_debug_printf ("Reading %s for %s",
5899 section->get_name (),
5900 abbrev_section->get_file_name ());
5901
5902 section->read (objfile);
5903 info_ptr = section->buffer;
5904
5905 if (info_ptr == NULL)
5906 return;
5907
5908 /* We can't set abfd until now because the section may be empty or
5909 not present, in which case the bfd is unknown. */
5910 abfd = section->get_bfd_owner ();
5911
5912 /* We don't use cutu_reader here because we don't need to read
5913 any dies: the signature is in the header. */
5914
5915 end_ptr = info_ptr + section->size;
5916 while (info_ptr < end_ptr)
5917 {
5918 signatured_type_up sig_type;
5919 struct dwo_unit *dwo_tu;
5920 void **slot;
5921 const gdb_byte *ptr = info_ptr;
5922 struct comp_unit_head header;
5923 unsigned int length;
5924
5925 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5926
5927 /* Initialize it due to a false compiler warning. */
5928 header.signature = -1;
5929 header.type_cu_offset_in_tu = (cu_offset) -1;
5930
5931 /* We need to read the type's signature in order to build the hash
5932 table, but we don't need anything else just yet. */
5933
5934 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5935 abbrev_section, ptr, section_kind);
5936
5937 length = header.get_length ();
5938
5939 /* Skip dummy type units. */
5940 if (ptr >= info_ptr + length
5941 || peek_abbrev_code (abfd, ptr) == 0
5942 || (header.unit_type != DW_UT_type
5943 && header.unit_type != DW_UT_split_type))
5944 {
5945 info_ptr += length;
5946 continue;
5947 }
5948
5949 if (types_htab == NULL)
5950 types_htab = allocate_dwo_unit_table ();
5951
5952 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5953 dwo_tu->dwo_file = dwo_file;
5954 dwo_tu->signature = header.signature;
5955 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5956 dwo_tu->section = section;
5957 dwo_tu->sect_off = sect_off;
5958 dwo_tu->length = length;
5959
5960 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5961 gdb_assert (slot != NULL);
5962 if (*slot != NULL)
5963 complaint (_("debug type entry at offset %s is duplicate to"
5964 " the entry at offset %s, signature %s"),
5965 sect_offset_str (sect_off),
5966 sect_offset_str (dwo_tu->sect_off),
5967 hex_string (header.signature));
5968 *slot = dwo_tu;
5969
5970 dwarf_read_debug_printf_v (" offset %s, signature %s",
5971 sect_offset_str (sect_off),
5972 hex_string (header.signature));
5973
5974 info_ptr += length;
5975 }
5976 }
5977
5978 /* Create the hash table of all entries in the .debug_types
5979 (or .debug_types.dwo) section(s).
5980 DWO_FILE is a pointer to the DWO file object.
5981
5982 The result is a pointer to the hash table or NULL if there are no types.
5983
5984 Note: This function processes DWO files only, not DWP files. */
5985
5986 static void
5987 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5988 struct dwo_file *dwo_file,
5989 gdb::array_view<dwarf2_section_info> type_sections,
5990 htab_up &types_htab)
5991 {
5992 for (dwarf2_section_info &section : type_sections)
5993 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5994 rcuh_kind::TYPE);
5995 }
5996
5997 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5998 If SLOT is non-NULL, it is the entry to use in the hash table.
5999 Otherwise we find one. */
6000
6001 static struct signatured_type *
6002 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6003 {
6004 if (per_objfile->per_bfd->all_comp_units.size ()
6005 == per_objfile->per_bfd->all_comp_units.capacity ())
6006 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6007
6008 signatured_type_up sig_type_holder
6009 = per_objfile->per_bfd->allocate_signatured_type (sig);
6010 signatured_type *sig_type = sig_type_holder.get ();
6011
6012 per_objfile->per_bfd->all_comp_units.emplace_back
6013 (sig_type_holder.release ());
6014 if (per_objfile->per_bfd->using_index)
6015 {
6016 sig_type->v.quick =
6017 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6018 struct dwarf2_per_cu_quick_data);
6019 }
6020
6021 if (slot == NULL)
6022 {
6023 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6024 sig_type, INSERT);
6025 }
6026 gdb_assert (*slot == NULL);
6027 *slot = sig_type;
6028 /* The rest of sig_type must be filled in by the caller. */
6029 return sig_type;
6030 }
6031
6032 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6033 Fill in SIG_ENTRY with DWO_ENTRY. */
6034
6035 static void
6036 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6037 struct signatured_type *sig_entry,
6038 struct dwo_unit *dwo_entry)
6039 {
6040 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6041
6042 /* Make sure we're not clobbering something we don't expect to. */
6043 gdb_assert (! sig_entry->queued);
6044 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6045 if (per_bfd->using_index)
6046 {
6047 gdb_assert (sig_entry->v.quick != NULL);
6048 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6049 }
6050 else
6051 gdb_assert (sig_entry->v.psymtab == NULL);
6052 gdb_assert (sig_entry->signature == dwo_entry->signature);
6053 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6054 gdb_assert (sig_entry->type_unit_group == NULL);
6055 gdb_assert (sig_entry->dwo_unit == NULL);
6056
6057 sig_entry->section = dwo_entry->section;
6058 sig_entry->sect_off = dwo_entry->sect_off;
6059 sig_entry->length = dwo_entry->length;
6060 sig_entry->reading_dwo_directly = 1;
6061 sig_entry->per_bfd = per_bfd;
6062 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6063 sig_entry->dwo_unit = dwo_entry;
6064 }
6065
6066 /* Subroutine of lookup_signatured_type.
6067 If we haven't read the TU yet, create the signatured_type data structure
6068 for a TU to be read in directly from a DWO file, bypassing the stub.
6069 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6070 using .gdb_index, then when reading a CU we want to stay in the DWO file
6071 containing that CU. Otherwise we could end up reading several other DWO
6072 files (due to comdat folding) to process the transitive closure of all the
6073 mentioned TUs, and that can be slow. The current DWO file will have every
6074 type signature that it needs.
6075 We only do this for .gdb_index because in the psymtab case we already have
6076 to read all the DWOs to build the type unit groups. */
6077
6078 static struct signatured_type *
6079 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6080 {
6081 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6082 struct dwo_file *dwo_file;
6083 struct dwo_unit find_dwo_entry, *dwo_entry;
6084 void **slot;
6085
6086 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6087
6088 /* If TU skeletons have been removed then we may not have read in any
6089 TUs yet. */
6090 if (per_objfile->per_bfd->signatured_types == NULL)
6091 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6092
6093 /* We only ever need to read in one copy of a signatured type.
6094 Use the global signatured_types array to do our own comdat-folding
6095 of types. If this is the first time we're reading this TU, and
6096 the TU has an entry in .gdb_index, replace the recorded data from
6097 .gdb_index with this TU. */
6098
6099 signatured_type find_sig_entry (sig);
6100 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6101 &find_sig_entry, INSERT);
6102 signatured_type *sig_entry = (struct signatured_type *) *slot;
6103
6104 /* We can get here with the TU already read, *or* in the process of being
6105 read. Don't reassign the global entry to point to this DWO if that's
6106 the case. Also note that if the TU is already being read, it may not
6107 have come from a DWO, the program may be a mix of Fission-compiled
6108 code and non-Fission-compiled code. */
6109
6110 /* Have we already tried to read this TU?
6111 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6112 needn't exist in the global table yet). */
6113 if (sig_entry != NULL && sig_entry->tu_read)
6114 return sig_entry;
6115
6116 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6117 dwo_unit of the TU itself. */
6118 dwo_file = cu->dwo_unit->dwo_file;
6119
6120 /* Ok, this is the first time we're reading this TU. */
6121 if (dwo_file->tus == NULL)
6122 return NULL;
6123 find_dwo_entry.signature = sig;
6124 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6125 &find_dwo_entry);
6126 if (dwo_entry == NULL)
6127 return NULL;
6128
6129 /* If the global table doesn't have an entry for this TU, add one. */
6130 if (sig_entry == NULL)
6131 sig_entry = add_type_unit (per_objfile, sig, slot);
6132
6133 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6134 sig_entry->tu_read = 1;
6135 return sig_entry;
6136 }
6137
6138 /* Subroutine of lookup_signatured_type.
6139 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6140 then try the DWP file. If the TU stub (skeleton) has been removed then
6141 it won't be in .gdb_index. */
6142
6143 static struct signatured_type *
6144 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6145 {
6146 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6147 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6148 struct dwo_unit *dwo_entry;
6149 void **slot;
6150
6151 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6152 gdb_assert (dwp_file != NULL);
6153
6154 /* If TU skeletons have been removed then we may not have read in any
6155 TUs yet. */
6156 if (per_objfile->per_bfd->signatured_types == NULL)
6157 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6158
6159 signatured_type find_sig_entry (sig);
6160 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6161 &find_sig_entry, INSERT);
6162 signatured_type *sig_entry = (struct signatured_type *) *slot;
6163
6164 /* Have we already tried to read this TU?
6165 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6166 needn't exist in the global table yet). */
6167 if (sig_entry != NULL)
6168 return sig_entry;
6169
6170 if (dwp_file->tus == NULL)
6171 return NULL;
6172 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6173 1 /* is_debug_types */);
6174 if (dwo_entry == NULL)
6175 return NULL;
6176
6177 sig_entry = add_type_unit (per_objfile, sig, slot);
6178 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6179
6180 return sig_entry;
6181 }
6182
6183 /* Lookup a signature based type for DW_FORM_ref_sig8.
6184 Returns NULL if signature SIG is not present in the table.
6185 It is up to the caller to complain about this. */
6186
6187 static struct signatured_type *
6188 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6189 {
6190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6191
6192 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6193 {
6194 /* We're in a DWO/DWP file, and we're using .gdb_index.
6195 These cases require special processing. */
6196 if (get_dwp_file (per_objfile) == NULL)
6197 return lookup_dwo_signatured_type (cu, sig);
6198 else
6199 return lookup_dwp_signatured_type (cu, sig);
6200 }
6201 else
6202 {
6203 if (per_objfile->per_bfd->signatured_types == NULL)
6204 return NULL;
6205 signatured_type find_entry (sig);
6206 return ((struct signatured_type *)
6207 htab_find (per_objfile->per_bfd->signatured_types.get (),
6208 &find_entry));
6209 }
6210 }
6211
6212 /* Low level DIE reading support. */
6213
6214 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6215
6216 static void
6217 init_cu_die_reader (struct die_reader_specs *reader,
6218 struct dwarf2_cu *cu,
6219 struct dwarf2_section_info *section,
6220 struct dwo_file *dwo_file,
6221 struct abbrev_table *abbrev_table)
6222 {
6223 gdb_assert (section->readin && section->buffer != NULL);
6224 reader->abfd = section->get_bfd_owner ();
6225 reader->cu = cu;
6226 reader->dwo_file = dwo_file;
6227 reader->die_section = section;
6228 reader->buffer = section->buffer;
6229 reader->buffer_end = section->buffer + section->size;
6230 reader->abbrev_table = abbrev_table;
6231 }
6232
6233 /* Subroutine of cutu_reader to simplify it.
6234 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6235 There's just a lot of work to do, and cutu_reader is big enough
6236 already.
6237
6238 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6239 from it to the DIE in the DWO. If NULL we are skipping the stub.
6240 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6241 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6242 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6243 STUB_COMP_DIR may be non-NULL.
6244 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6245 are filled in with the info of the DIE from the DWO file.
6246 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6247 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6248 kept around for at least as long as *RESULT_READER.
6249
6250 The result is non-zero if a valid (non-dummy) DIE was found. */
6251
6252 static int
6253 read_cutu_die_from_dwo (dwarf2_cu *cu,
6254 struct dwo_unit *dwo_unit,
6255 struct die_info *stub_comp_unit_die,
6256 const char *stub_comp_dir,
6257 struct die_reader_specs *result_reader,
6258 const gdb_byte **result_info_ptr,
6259 struct die_info **result_comp_unit_die,
6260 abbrev_table_up *result_dwo_abbrev_table)
6261 {
6262 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6263 dwarf2_per_cu_data *per_cu = cu->per_cu;
6264 struct objfile *objfile = per_objfile->objfile;
6265 bfd *abfd;
6266 const gdb_byte *begin_info_ptr, *info_ptr;
6267 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6268 int i,num_extra_attrs;
6269 struct dwarf2_section_info *dwo_abbrev_section;
6270 struct die_info *comp_unit_die;
6271
6272 /* At most one of these may be provided. */
6273 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6274
6275 /* These attributes aren't processed until later:
6276 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6277 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6278 referenced later. However, these attributes are found in the stub
6279 which we won't have later. In order to not impose this complication
6280 on the rest of the code, we read them here and copy them to the
6281 DWO CU/TU die. */
6282
6283 stmt_list = NULL;
6284 low_pc = NULL;
6285 high_pc = NULL;
6286 ranges = NULL;
6287 comp_dir = NULL;
6288
6289 if (stub_comp_unit_die != NULL)
6290 {
6291 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6292 DWO file. */
6293 if (!per_cu->is_debug_types)
6294 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6295 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6296 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6297 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6298 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6299
6300 cu->addr_base = stub_comp_unit_die->addr_base ();
6301
6302 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6303 We need the value before we can process DW_AT_ranges values from the
6304 DWO. */
6305 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6306
6307 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6308 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6309 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6310 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6311 section. */
6312 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6313 }
6314 else if (stub_comp_dir != NULL)
6315 {
6316 /* Reconstruct the comp_dir attribute to simplify the code below. */
6317 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6318 comp_dir->name = DW_AT_comp_dir;
6319 comp_dir->form = DW_FORM_string;
6320 comp_dir->set_string_noncanonical (stub_comp_dir);
6321 }
6322
6323 /* Set up for reading the DWO CU/TU. */
6324 cu->dwo_unit = dwo_unit;
6325 dwarf2_section_info *section = dwo_unit->section;
6326 section->read (objfile);
6327 abfd = section->get_bfd_owner ();
6328 begin_info_ptr = info_ptr = (section->buffer
6329 + to_underlying (dwo_unit->sect_off));
6330 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6331
6332 if (per_cu->is_debug_types)
6333 {
6334 signatured_type *sig_type = (struct signatured_type *) per_cu;
6335
6336 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6337 section, dwo_abbrev_section,
6338 info_ptr, rcuh_kind::TYPE);
6339 /* This is not an assert because it can be caused by bad debug info. */
6340 if (sig_type->signature != cu->header.signature)
6341 {
6342 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6343 " TU at offset %s [in module %s]"),
6344 hex_string (sig_type->signature),
6345 hex_string (cu->header.signature),
6346 sect_offset_str (dwo_unit->sect_off),
6347 bfd_get_filename (abfd));
6348 }
6349 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6350 /* For DWOs coming from DWP files, we don't know the CU length
6351 nor the type's offset in the TU until now. */
6352 dwo_unit->length = cu->header.get_length ();
6353 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6354
6355 /* Establish the type offset that can be used to lookup the type.
6356 For DWO files, we don't know it until now. */
6357 sig_type->type_offset_in_section
6358 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6359 }
6360 else
6361 {
6362 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6363 section, dwo_abbrev_section,
6364 info_ptr, rcuh_kind::COMPILE);
6365 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6366 /* For DWOs coming from DWP files, we don't know the CU length
6367 until now. */
6368 dwo_unit->length = cu->header.get_length ();
6369 }
6370
6371 dwo_abbrev_section->read (objfile);
6372 *result_dwo_abbrev_table
6373 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6374 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6375 result_dwo_abbrev_table->get ());
6376
6377 /* Read in the die, but leave space to copy over the attributes
6378 from the stub. This has the benefit of simplifying the rest of
6379 the code - all the work to maintain the illusion of a single
6380 DW_TAG_{compile,type}_unit DIE is done here. */
6381 num_extra_attrs = ((stmt_list != NULL)
6382 + (low_pc != NULL)
6383 + (high_pc != NULL)
6384 + (ranges != NULL)
6385 + (comp_dir != NULL));
6386 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6387 num_extra_attrs);
6388
6389 /* Copy over the attributes from the stub to the DIE we just read in. */
6390 comp_unit_die = *result_comp_unit_die;
6391 i = comp_unit_die->num_attrs;
6392 if (stmt_list != NULL)
6393 comp_unit_die->attrs[i++] = *stmt_list;
6394 if (low_pc != NULL)
6395 comp_unit_die->attrs[i++] = *low_pc;
6396 if (high_pc != NULL)
6397 comp_unit_die->attrs[i++] = *high_pc;
6398 if (ranges != NULL)
6399 comp_unit_die->attrs[i++] = *ranges;
6400 if (comp_dir != NULL)
6401 comp_unit_die->attrs[i++] = *comp_dir;
6402 comp_unit_die->num_attrs += num_extra_attrs;
6403
6404 if (dwarf_die_debug)
6405 {
6406 fprintf_unfiltered (gdb_stdlog,
6407 "Read die from %s@0x%x of %s:\n",
6408 section->get_name (),
6409 (unsigned) (begin_info_ptr - section->buffer),
6410 bfd_get_filename (abfd));
6411 dump_die (comp_unit_die, dwarf_die_debug);
6412 }
6413
6414 /* Skip dummy compilation units. */
6415 if (info_ptr >= begin_info_ptr + dwo_unit->length
6416 || peek_abbrev_code (abfd, info_ptr) == 0)
6417 return 0;
6418
6419 *result_info_ptr = info_ptr;
6420 return 1;
6421 }
6422
6423 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6424 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6425 signature is part of the header. */
6426 static gdb::optional<ULONGEST>
6427 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6428 {
6429 if (cu->header.version >= 5)
6430 return cu->header.signature;
6431 struct attribute *attr;
6432 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6433 if (attr == nullptr || !attr->form_is_unsigned ())
6434 return gdb::optional<ULONGEST> ();
6435 return attr->as_unsigned ();
6436 }
6437
6438 /* Subroutine of cutu_reader to simplify it.
6439 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6440 Returns NULL if the specified DWO unit cannot be found. */
6441
6442 static struct dwo_unit *
6443 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6444 {
6445 dwarf2_per_cu_data *per_cu = cu->per_cu;
6446 struct dwo_unit *dwo_unit;
6447 const char *comp_dir;
6448
6449 gdb_assert (cu != NULL);
6450
6451 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6452 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6453 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6454
6455 if (per_cu->is_debug_types)
6456 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6457 else
6458 {
6459 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6460
6461 if (!signature.has_value ())
6462 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6463 " [in module %s]"),
6464 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6465
6466 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6467 }
6468
6469 return dwo_unit;
6470 }
6471
6472 /* Subroutine of cutu_reader to simplify it.
6473 See it for a description of the parameters.
6474 Read a TU directly from a DWO file, bypassing the stub. */
6475
6476 void
6477 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6478 dwarf2_per_objfile *per_objfile,
6479 dwarf2_cu *existing_cu)
6480 {
6481 struct signatured_type *sig_type;
6482
6483 /* Verify we can do the following downcast, and that we have the
6484 data we need. */
6485 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6486 sig_type = (struct signatured_type *) this_cu;
6487 gdb_assert (sig_type->dwo_unit != NULL);
6488
6489 dwarf2_cu *cu;
6490
6491 if (existing_cu != nullptr)
6492 {
6493 cu = existing_cu;
6494 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6495 /* There's no need to do the rereading_dwo_cu handling that
6496 cutu_reader does since we don't read the stub. */
6497 }
6498 else
6499 {
6500 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6501 in per_objfile yet. */
6502 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6503 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6504 cu = m_new_cu.get ();
6505 }
6506
6507 /* A future optimization, if needed, would be to use an existing
6508 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6509 could share abbrev tables. */
6510
6511 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6512 NULL /* stub_comp_unit_die */,
6513 sig_type->dwo_unit->dwo_file->comp_dir,
6514 this, &info_ptr,
6515 &comp_unit_die,
6516 &m_dwo_abbrev_table) == 0)
6517 {
6518 /* Dummy die. */
6519 dummy_p = true;
6520 }
6521 }
6522
6523 /* Initialize a CU (or TU) and read its DIEs.
6524 If the CU defers to a DWO file, read the DWO file as well.
6525
6526 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6527 Otherwise the table specified in the comp unit header is read in and used.
6528 This is an optimization for when we already have the abbrev table.
6529
6530 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6531 allocated. */
6532
6533 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6534 dwarf2_per_objfile *per_objfile,
6535 struct abbrev_table *abbrev_table,
6536 dwarf2_cu *existing_cu,
6537 bool skip_partial)
6538 : die_reader_specs {},
6539 m_this_cu (this_cu)
6540 {
6541 struct objfile *objfile = per_objfile->objfile;
6542 struct dwarf2_section_info *section = this_cu->section;
6543 bfd *abfd = section->get_bfd_owner ();
6544 const gdb_byte *begin_info_ptr;
6545 struct signatured_type *sig_type = NULL;
6546 struct dwarf2_section_info *abbrev_section;
6547 /* Non-zero if CU currently points to a DWO file and we need to
6548 reread it. When this happens we need to reread the skeleton die
6549 before we can reread the DWO file (this only applies to CUs, not TUs). */
6550 int rereading_dwo_cu = 0;
6551
6552 if (dwarf_die_debug)
6553 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6554 this_cu->is_debug_types ? "type" : "comp",
6555 sect_offset_str (this_cu->sect_off));
6556
6557 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6558 file (instead of going through the stub), short-circuit all of this. */
6559 if (this_cu->reading_dwo_directly)
6560 {
6561 /* Narrow down the scope of possibilities to have to understand. */
6562 gdb_assert (this_cu->is_debug_types);
6563 gdb_assert (abbrev_table == NULL);
6564 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6565 return;
6566 }
6567
6568 /* This is cheap if the section is already read in. */
6569 section->read (objfile);
6570
6571 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6572
6573 abbrev_section = get_abbrev_section_for_cu (this_cu);
6574
6575 dwarf2_cu *cu;
6576
6577 if (existing_cu != nullptr)
6578 {
6579 cu = existing_cu;
6580 /* If this CU is from a DWO file we need to start over, we need to
6581 refetch the attributes from the skeleton CU.
6582 This could be optimized by retrieving those attributes from when we
6583 were here the first time: the previous comp_unit_die was stored in
6584 comp_unit_obstack. But there's no data yet that we need this
6585 optimization. */
6586 if (cu->dwo_unit != NULL)
6587 rereading_dwo_cu = 1;
6588 }
6589 else
6590 {
6591 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6592 in per_objfile yet. */
6593 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6594 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6595 cu = m_new_cu.get ();
6596 }
6597
6598 /* Get the header. */
6599 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6600 {
6601 /* We already have the header, there's no need to read it in again. */
6602 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6603 }
6604 else
6605 {
6606 if (this_cu->is_debug_types)
6607 {
6608 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6609 section, abbrev_section,
6610 info_ptr, rcuh_kind::TYPE);
6611
6612 /* Since per_cu is the first member of struct signatured_type,
6613 we can go from a pointer to one to a pointer to the other. */
6614 sig_type = (struct signatured_type *) this_cu;
6615 gdb_assert (sig_type->signature == cu->header.signature);
6616 gdb_assert (sig_type->type_offset_in_tu
6617 == cu->header.type_cu_offset_in_tu);
6618 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6619
6620 /* LENGTH has not been set yet for type units if we're
6621 using .gdb_index. */
6622 this_cu->length = cu->header.get_length ();
6623
6624 /* Establish the type offset that can be used to lookup the type. */
6625 sig_type->type_offset_in_section =
6626 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6627
6628 this_cu->dwarf_version = cu->header.version;
6629 }
6630 else
6631 {
6632 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6633 section, abbrev_section,
6634 info_ptr,
6635 rcuh_kind::COMPILE);
6636
6637 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6638 if (this_cu->length == 0)
6639 this_cu->length = cu->header.get_length ();
6640 else
6641 gdb_assert (this_cu->length == cu->header.get_length ());
6642 this_cu->dwarf_version = cu->header.version;
6643 }
6644 }
6645
6646 /* Skip dummy compilation units. */
6647 if (info_ptr >= begin_info_ptr + this_cu->length
6648 || peek_abbrev_code (abfd, info_ptr) == 0)
6649 {
6650 dummy_p = true;
6651 return;
6652 }
6653
6654 /* If we don't have them yet, read the abbrevs for this compilation unit.
6655 And if we need to read them now, make sure they're freed when we're
6656 done. */
6657 if (abbrev_table != NULL)
6658 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6659 else
6660 {
6661 abbrev_section->read (objfile);
6662 m_abbrev_table_holder
6663 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6664 abbrev_table = m_abbrev_table_holder.get ();
6665 }
6666
6667 /* Read the top level CU/TU die. */
6668 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6669 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6670
6671 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6672 {
6673 dummy_p = true;
6674 return;
6675 }
6676
6677 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6678 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6679 table from the DWO file and pass the ownership over to us. It will be
6680 referenced from READER, so we must make sure to free it after we're done
6681 with READER.
6682
6683 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6684 DWO CU, that this test will fail (the attribute will not be present). */
6685 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6686 if (dwo_name != nullptr)
6687 {
6688 struct dwo_unit *dwo_unit;
6689 struct die_info *dwo_comp_unit_die;
6690
6691 if (comp_unit_die->has_children)
6692 {
6693 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6694 " has children (offset %s) [in module %s]"),
6695 sect_offset_str (this_cu->sect_off),
6696 bfd_get_filename (abfd));
6697 }
6698 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6699 if (dwo_unit != NULL)
6700 {
6701 if (read_cutu_die_from_dwo (cu, dwo_unit,
6702 comp_unit_die, NULL,
6703 this, &info_ptr,
6704 &dwo_comp_unit_die,
6705 &m_dwo_abbrev_table) == 0)
6706 {
6707 /* Dummy die. */
6708 dummy_p = true;
6709 return;
6710 }
6711 comp_unit_die = dwo_comp_unit_die;
6712 }
6713 else
6714 {
6715 /* Yikes, we couldn't find the rest of the DIE, we only have
6716 the stub. A complaint has already been logged. There's
6717 not much more we can do except pass on the stub DIE to
6718 die_reader_func. We don't want to throw an error on bad
6719 debug info. */
6720 }
6721 }
6722 }
6723
6724 void
6725 cutu_reader::keep ()
6726 {
6727 /* Done, clean up. */
6728 gdb_assert (!dummy_p);
6729 if (m_new_cu != NULL)
6730 {
6731 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6732 now. */
6733 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6734 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6735 }
6736 }
6737
6738 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6739 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6740 assumed to have already done the lookup to find the DWO file).
6741
6742 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6743 THIS_CU->is_debug_types, but nothing else.
6744
6745 We fill in THIS_CU->length.
6746
6747 THIS_CU->cu is always freed when done.
6748 This is done in order to not leave THIS_CU->cu in a state where we have
6749 to care whether it refers to the "main" CU or the DWO CU.
6750
6751 When parent_cu is passed, it is used to provide a default value for
6752 str_offsets_base and addr_base from the parent. */
6753
6754 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6755 dwarf2_per_objfile *per_objfile,
6756 struct dwarf2_cu *parent_cu,
6757 struct dwo_file *dwo_file)
6758 : die_reader_specs {},
6759 m_this_cu (this_cu)
6760 {
6761 struct objfile *objfile = per_objfile->objfile;
6762 struct dwarf2_section_info *section = this_cu->section;
6763 bfd *abfd = section->get_bfd_owner ();
6764 struct dwarf2_section_info *abbrev_section;
6765 const gdb_byte *begin_info_ptr, *info_ptr;
6766
6767 if (dwarf_die_debug)
6768 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6769 this_cu->is_debug_types ? "type" : "comp",
6770 sect_offset_str (this_cu->sect_off));
6771
6772 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6773
6774 abbrev_section = (dwo_file != NULL
6775 ? &dwo_file->sections.abbrev
6776 : get_abbrev_section_for_cu (this_cu));
6777
6778 /* This is cheap if the section is already read in. */
6779 section->read (objfile);
6780
6781 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6782
6783 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6784 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6785 section, abbrev_section, info_ptr,
6786 (this_cu->is_debug_types
6787 ? rcuh_kind::TYPE
6788 : rcuh_kind::COMPILE));
6789
6790 if (parent_cu != nullptr)
6791 {
6792 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6793 m_new_cu->addr_base = parent_cu->addr_base;
6794 }
6795 this_cu->length = m_new_cu->header.get_length ();
6796
6797 /* Skip dummy compilation units. */
6798 if (info_ptr >= begin_info_ptr + this_cu->length
6799 || peek_abbrev_code (abfd, info_ptr) == 0)
6800 {
6801 dummy_p = true;
6802 return;
6803 }
6804
6805 abbrev_section->read (objfile);
6806 m_abbrev_table_holder
6807 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6808
6809 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6810 m_abbrev_table_holder.get ());
6811 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6812 }
6813
6814 \f
6815 /* Type Unit Groups.
6816
6817 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6818 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6819 so that all types coming from the same compilation (.o file) are grouped
6820 together. A future step could be to put the types in the same symtab as
6821 the CU the types ultimately came from. */
6822
6823 static hashval_t
6824 hash_type_unit_group (const void *item)
6825 {
6826 const struct type_unit_group *tu_group
6827 = (const struct type_unit_group *) item;
6828
6829 return hash_stmt_list_entry (&tu_group->hash);
6830 }
6831
6832 static int
6833 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6834 {
6835 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6836 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6837
6838 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6839 }
6840
6841 /* Allocate a hash table for type unit groups. */
6842
6843 static htab_up
6844 allocate_type_unit_groups_table ()
6845 {
6846 return htab_up (htab_create_alloc (3,
6847 hash_type_unit_group,
6848 eq_type_unit_group,
6849 htab_delete_entry<type_unit_group>,
6850 xcalloc, xfree));
6851 }
6852
6853 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6854 partial symtabs. We combine several TUs per psymtab to not let the size
6855 of any one psymtab grow too big. */
6856 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6857 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6858
6859 /* Helper routine for get_type_unit_group.
6860 Create the type_unit_group object used to hold one or more TUs. */
6861
6862 static std::unique_ptr<type_unit_group>
6863 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6864 {
6865 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6866 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6867
6868 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6869 tu_group->per_bfd = per_bfd;
6870
6871 if (per_bfd->using_index)
6872 {
6873 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6874 struct dwarf2_per_cu_quick_data);
6875 }
6876 else
6877 {
6878 unsigned int line_offset = to_underlying (line_offset_struct);
6879 dwarf2_psymtab *pst;
6880 std::string name;
6881
6882 /* Give the symtab a useful name for debug purposes. */
6883 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6884 name = string_printf ("<type_units_%d>",
6885 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6886 else
6887 name = string_printf ("<type_units_at_0x%x>", line_offset);
6888
6889 pst = create_partial_symtab (tu_group.get (), per_objfile,
6890 name.c_str ());
6891 pst->anonymous = true;
6892 }
6893
6894 tu_group->hash.dwo_unit = cu->dwo_unit;
6895 tu_group->hash.line_sect_off = line_offset_struct;
6896
6897 return tu_group;
6898 }
6899
6900 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6901 STMT_LIST is a DW_AT_stmt_list attribute. */
6902
6903 static struct type_unit_group *
6904 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6905 {
6906 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6907 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6908 struct type_unit_group *tu_group;
6909 void **slot;
6910 unsigned int line_offset;
6911 struct type_unit_group type_unit_group_for_lookup;
6912
6913 if (per_objfile->per_bfd->type_unit_groups == NULL)
6914 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6915
6916 /* Do we need to create a new group, or can we use an existing one? */
6917
6918 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6919 {
6920 line_offset = stmt_list->as_unsigned ();
6921 ++tu_stats->nr_symtab_sharers;
6922 }
6923 else
6924 {
6925 /* Ugh, no stmt_list. Rare, but we have to handle it.
6926 We can do various things here like create one group per TU or
6927 spread them over multiple groups to split up the expansion work.
6928 To avoid worst case scenarios (too many groups or too large groups)
6929 we, umm, group them in bunches. */
6930 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6931 | (tu_stats->nr_stmt_less_type_units
6932 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6933 ++tu_stats->nr_stmt_less_type_units;
6934 }
6935
6936 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6937 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6938 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6939 &type_unit_group_for_lookup, INSERT);
6940 if (*slot == nullptr)
6941 {
6942 sect_offset line_offset_struct = (sect_offset) line_offset;
6943 std::unique_ptr<type_unit_group> grp
6944 = create_type_unit_group (cu, line_offset_struct);
6945 *slot = grp.release ();
6946 ++tu_stats->nr_symtabs;
6947 }
6948
6949 tu_group = (struct type_unit_group *) *slot;
6950 gdb_assert (tu_group != nullptr);
6951 return tu_group;
6952 }
6953 \f
6954 /* Partial symbol tables. */
6955
6956 /* Create a psymtab named NAME and assign it to PER_CU.
6957
6958 The caller must fill in the following details:
6959 dirname, textlow, texthigh. */
6960
6961 static dwarf2_psymtab *
6962 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6963 dwarf2_per_objfile *per_objfile,
6964 const char *name)
6965 {
6966 dwarf2_psymtab *pst
6967 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6968 per_objfile->objfile->per_bfd, per_cu);
6969
6970 pst->psymtabs_addrmap_supported = true;
6971
6972 /* This is the glue that links PST into GDB's symbol API. */
6973 per_cu->v.psymtab = pst;
6974
6975 return pst;
6976 }
6977
6978 /* DIE reader function for process_psymtab_comp_unit. */
6979
6980 static void
6981 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6982 const gdb_byte *info_ptr,
6983 struct die_info *comp_unit_die,
6984 enum language pretend_language)
6985 {
6986 struct dwarf2_cu *cu = reader->cu;
6987 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6988 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6989 struct objfile *objfile = per_objfile->objfile;
6990 struct gdbarch *gdbarch = objfile->arch ();
6991 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6992 CORE_ADDR baseaddr;
6993 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6994 dwarf2_psymtab *pst;
6995 enum pc_bounds_kind cu_bounds_kind;
6996
6997 gdb_assert (! per_cu->is_debug_types);
6998
6999 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7000
7001 /* Allocate a new partial symbol table structure. */
7002 gdb::unique_xmalloc_ptr<char> debug_filename;
7003 static const char artificial[] = "<artificial>";
7004 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
7005 if (strcmp (fnd.name, artificial) == 0)
7006 {
7007 debug_filename.reset (concat (artificial, "@",
7008 sect_offset_str (per_cu->sect_off),
7009 (char *) NULL));
7010 fnd.name = debug_filename.get ();
7011 }
7012
7013 pst = create_partial_symtab (per_cu, per_objfile, fnd.name);
7014
7015 /* This must be done before calling dwarf2_build_include_psymtabs. */
7016 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7017
7018 baseaddr = objfile->text_section_offset ();
7019
7020 dwarf2_find_base_address (comp_unit_die, cu);
7021
7022 /* Possibly set the default values of LOWPC and HIGHPC from
7023 `DW_AT_ranges'. */
7024 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7025 &best_highpc, cu, pst);
7026 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7027 {
7028 CORE_ADDR low
7029 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7030 - baseaddr);
7031 CORE_ADDR high
7032 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7033 - baseaddr - 1);
7034 /* Store the contiguous range if it is not empty; it can be
7035 empty for CUs with no code. */
7036 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7037 low, high, pst);
7038 }
7039
7040 /* Check if comp unit has_children.
7041 If so, read the rest of the partial symbols from this comp unit.
7042 If not, there's no more debug_info for this comp unit. */
7043 if (comp_unit_die->has_children)
7044 {
7045 struct partial_die_info *first_die;
7046 CORE_ADDR lowpc, highpc;
7047
7048 lowpc = ((CORE_ADDR) -1);
7049 highpc = ((CORE_ADDR) 0);
7050
7051 first_die = load_partial_dies (reader, info_ptr, 1);
7052
7053 scan_partial_symbols (first_die, &lowpc, &highpc,
7054 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7055
7056 /* If we didn't find a lowpc, set it to highpc to avoid
7057 complaints from `maint check'. */
7058 if (lowpc == ((CORE_ADDR) -1))
7059 lowpc = highpc;
7060
7061 /* If the compilation unit didn't have an explicit address range,
7062 then use the information extracted from its child dies. */
7063 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7064 {
7065 best_lowpc = lowpc;
7066 best_highpc = highpc;
7067 }
7068 }
7069 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7070 best_lowpc + baseaddr)
7071 - baseaddr);
7072 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7073 best_highpc + baseaddr)
7074 - baseaddr);
7075
7076 pst->end ();
7077
7078 if (!cu->per_cu->imported_symtabs_empty ())
7079 {
7080 int i;
7081 int len = cu->per_cu->imported_symtabs_size ();
7082
7083 /* Fill in 'dependencies' here; we fill in 'users' in a
7084 post-pass. */
7085 pst->number_of_dependencies = len;
7086 pst->dependencies
7087 = per_bfd->partial_symtabs->allocate_dependencies (len);
7088 for (i = 0; i < len; ++i)
7089 {
7090 pst->dependencies[i]
7091 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7092 }
7093
7094 cu->per_cu->imported_symtabs_free ();
7095 }
7096
7097 /* Get the list of files included in the current compilation unit,
7098 and build a psymtab for each of them. */
7099 dwarf2_build_include_psymtabs (cu, comp_unit_die, fnd, pst);
7100
7101 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7102 ", %d global, %d static syms",
7103 per_cu->is_debug_types ? "type" : "comp",
7104 sect_offset_str (per_cu->sect_off),
7105 paddress (gdbarch, pst->text_low (objfile)),
7106 paddress (gdbarch, pst->text_high (objfile)),
7107 (int) pst->global_psymbols.size (),
7108 (int) pst->static_psymbols.size ());
7109 }
7110
7111 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7112 Process compilation unit THIS_CU for a psymtab. */
7113
7114 static void
7115 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7116 dwarf2_per_objfile *per_objfile,
7117 bool want_partial_unit,
7118 enum language pretend_language)
7119 {
7120 /* If this compilation unit was already read in, free the
7121 cached copy in order to read it in again. This is
7122 necessary because we skipped some symbols when we first
7123 read in the compilation unit (see load_partial_dies).
7124 This problem could be avoided, but the benefit is unclear. */
7125 per_objfile->remove_cu (this_cu);
7126
7127 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7128
7129 if (reader.comp_unit_die == nullptr)
7130 return;
7131
7132 switch (reader.comp_unit_die->tag)
7133 {
7134 case DW_TAG_compile_unit:
7135 this_cu->unit_type = DW_UT_compile;
7136 break;
7137 case DW_TAG_partial_unit:
7138 this_cu->unit_type = DW_UT_partial;
7139 break;
7140 case DW_TAG_type_unit:
7141 this_cu->unit_type = DW_UT_type;
7142 break;
7143 default:
7144 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7145 dwarf_tag_name (reader.comp_unit_die->tag),
7146 sect_offset_str (reader.cu->per_cu->sect_off),
7147 objfile_name (per_objfile->objfile));
7148 }
7149
7150 if (reader.dummy_p)
7151 {
7152 /* Nothing. */
7153 }
7154 else if (this_cu->is_debug_types)
7155 build_type_psymtabs_reader (&reader, reader.info_ptr,
7156 reader.comp_unit_die);
7157 else if (want_partial_unit
7158 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7159 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7160 reader.comp_unit_die,
7161 pretend_language);
7162
7163 /* Age out any secondary CUs. */
7164 per_objfile->age_comp_units ();
7165 }
7166
7167 /* Reader function for build_type_psymtabs. */
7168
7169 static void
7170 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7171 const gdb_byte *info_ptr,
7172 struct die_info *type_unit_die)
7173 {
7174 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7175 struct dwarf2_cu *cu = reader->cu;
7176 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7177 struct signatured_type *sig_type;
7178 struct type_unit_group *tu_group;
7179 struct attribute *attr;
7180 struct partial_die_info *first_die;
7181 CORE_ADDR lowpc, highpc;
7182 dwarf2_psymtab *pst;
7183
7184 gdb_assert (per_cu->is_debug_types);
7185 sig_type = (struct signatured_type *) per_cu;
7186
7187 if (! type_unit_die->has_children)
7188 return;
7189
7190 attr = type_unit_die->attr (DW_AT_stmt_list);
7191 tu_group = get_type_unit_group (cu, attr);
7192
7193 if (tu_group->tus == nullptr)
7194 tu_group->tus = new std::vector<signatured_type *>;
7195 tu_group->tus->push_back (sig_type);
7196
7197 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7198 pst = create_partial_symtab (per_cu, per_objfile, "");
7199 pst->anonymous = true;
7200
7201 first_die = load_partial_dies (reader, info_ptr, 1);
7202
7203 lowpc = (CORE_ADDR) -1;
7204 highpc = (CORE_ADDR) 0;
7205 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7206
7207 pst->end ();
7208 }
7209
7210 /* Struct used to sort TUs by their abbreviation table offset. */
7211
7212 struct tu_abbrev_offset
7213 {
7214 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7215 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7216 {}
7217
7218 /* This is used when sorting. */
7219 bool operator< (const tu_abbrev_offset &other) const
7220 {
7221 return abbrev_offset < other.abbrev_offset;
7222 }
7223
7224 signatured_type *sig_type;
7225 sect_offset abbrev_offset;
7226 };
7227
7228 /* Efficiently read all the type units.
7229
7230 The efficiency is because we sort TUs by the abbrev table they use and
7231 only read each abbrev table once. In one program there are 200K TUs
7232 sharing 8K abbrev tables.
7233
7234 The main purpose of this function is to support building the
7235 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7236 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7237 can collapse the search space by grouping them by stmt_list.
7238 The savings can be significant, in the same program from above the 200K TUs
7239 share 8K stmt_list tables.
7240
7241 FUNC is expected to call get_type_unit_group, which will create the
7242 struct type_unit_group if necessary and add it to
7243 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7244
7245 static void
7246 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7247 {
7248 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7249 abbrev_table_up abbrev_table;
7250 sect_offset abbrev_offset;
7251
7252 /* It's up to the caller to not call us multiple times. */
7253 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7254
7255 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7256 return;
7257
7258 /* TUs typically share abbrev tables, and there can be way more TUs than
7259 abbrev tables. Sort by abbrev table to reduce the number of times we
7260 read each abbrev table in.
7261 Alternatives are to punt or to maintain a cache of abbrev tables.
7262 This is simpler and efficient enough for now.
7263
7264 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7265 symtab to use). Typically TUs with the same abbrev offset have the same
7266 stmt_list value too so in practice this should work well.
7267
7268 The basic algorithm here is:
7269
7270 sort TUs by abbrev table
7271 for each TU with same abbrev table:
7272 read abbrev table if first user
7273 read TU top level DIE
7274 [IWBN if DWO skeletons had DW_AT_stmt_list]
7275 call FUNC */
7276
7277 dwarf_read_debug_printf ("Building type unit groups ...");
7278
7279 /* Sort in a separate table to maintain the order of all_comp_units
7280 for .gdb_index: TU indices directly index all_type_units. */
7281 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7282 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7283
7284 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7285 {
7286 if (cu->is_debug_types)
7287 {
7288 auto sig_type = static_cast<signatured_type *> (cu.get ());
7289 sorted_by_abbrev.emplace_back
7290 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7291 sig_type->sect_off));
7292 }
7293 }
7294
7295 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7296
7297 abbrev_offset = (sect_offset) ~(unsigned) 0;
7298
7299 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7300 {
7301 /* Switch to the next abbrev table if necessary. */
7302 if (abbrev_table == NULL
7303 || tu.abbrev_offset != abbrev_offset)
7304 {
7305 abbrev_offset = tu.abbrev_offset;
7306 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7307 abbrev_table =
7308 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7309 ++tu_stats->nr_uniq_abbrev_tables;
7310 }
7311
7312 cutu_reader reader (tu.sig_type, per_objfile,
7313 abbrev_table.get (), nullptr, false);
7314 if (!reader.dummy_p)
7315 build_type_psymtabs_reader (&reader, reader.info_ptr,
7316 reader.comp_unit_die);
7317 }
7318 }
7319
7320 /* Print collected type unit statistics. */
7321
7322 static void
7323 print_tu_stats (dwarf2_per_objfile *per_objfile)
7324 {
7325 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7326
7327 dwarf_read_debug_printf ("Type unit statistics:");
7328 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7329 dwarf_read_debug_printf (" %d uniq abbrev tables",
7330 tu_stats->nr_uniq_abbrev_tables);
7331 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7332 tu_stats->nr_symtabs);
7333 dwarf_read_debug_printf (" %d symtab sharers",
7334 tu_stats->nr_symtab_sharers);
7335 dwarf_read_debug_printf (" %d type units without a stmt_list",
7336 tu_stats->nr_stmt_less_type_units);
7337 dwarf_read_debug_printf (" %d all_type_units reallocs",
7338 tu_stats->nr_all_type_units_reallocs);
7339 }
7340
7341 /* Traversal function for build_type_psymtabs. */
7342
7343 static int
7344 build_type_psymtab_dependencies (void **slot, void *info)
7345 {
7346 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7347 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7348 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7349 dwarf2_psymtab *pst = tu_group->v.psymtab;
7350 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7351 int i;
7352
7353 gdb_assert (len > 0);
7354 gdb_assert (tu_group->type_unit_group_p ());
7355
7356 pst->number_of_dependencies = len;
7357 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7358 for (i = 0; i < len; ++i)
7359 {
7360 struct signatured_type *iter = tu_group->tus->at (i);
7361 gdb_assert (iter->is_debug_types);
7362 pst->dependencies[i] = iter->v.psymtab;
7363 iter->type_unit_group = tu_group;
7364 }
7365
7366 delete tu_group->tus;
7367 tu_group->tus = nullptr;
7368
7369 return 1;
7370 }
7371
7372 /* Traversal function for process_skeletonless_type_unit.
7373 Read a TU in a DWO file and build partial symbols for it. */
7374
7375 static int
7376 process_skeletonless_type_unit (void **slot, void *info)
7377 {
7378 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7379 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7380
7381 /* If this TU doesn't exist in the global table, add it and read it in. */
7382
7383 if (per_objfile->per_bfd->signatured_types == NULL)
7384 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7385
7386 signatured_type find_entry (dwo_unit->signature);
7387 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7388 &find_entry, INSERT);
7389 /* If we've already seen this type there's nothing to do. What's happening
7390 is we're doing our own version of comdat-folding here. */
7391 if (*slot != NULL)
7392 return 1;
7393
7394 /* This does the job that create_all_comp_units would have done for
7395 this TU. */
7396 signatured_type *entry
7397 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7398 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7399 *slot = entry;
7400
7401 /* This does the job that build_type_psymtabs would have done. */
7402 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7403 if (!reader.dummy_p)
7404 build_type_psymtabs_reader (&reader, reader.info_ptr,
7405 reader.comp_unit_die);
7406
7407 return 1;
7408 }
7409
7410 /* Traversal function for process_skeletonless_type_units. */
7411
7412 static int
7413 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7414 {
7415 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7416
7417 if (dwo_file->tus != NULL)
7418 htab_traverse_noresize (dwo_file->tus.get (),
7419 process_skeletonless_type_unit, info);
7420
7421 return 1;
7422 }
7423
7424 /* Scan all TUs of DWO files, verifying we've processed them.
7425 This is needed in case a TU was emitted without its skeleton.
7426 Note: This can't be done until we know what all the DWO files are. */
7427
7428 static void
7429 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7430 {
7431 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7432 if (get_dwp_file (per_objfile) == NULL
7433 && per_objfile->per_bfd->dwo_files != NULL)
7434 {
7435 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7436 process_dwo_file_for_skeletonless_type_units,
7437 per_objfile);
7438 }
7439 }
7440
7441 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7442
7443 static void
7444 set_partial_user (dwarf2_per_objfile *per_objfile)
7445 {
7446 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7447 {
7448 dwarf2_psymtab *pst = per_cu->v.psymtab;
7449
7450 if (pst == NULL)
7451 continue;
7452
7453 for (int j = 0; j < pst->number_of_dependencies; ++j)
7454 {
7455 /* Set the 'user' field only if it is not already set. */
7456 if (pst->dependencies[j]->user == NULL)
7457 pst->dependencies[j]->user = pst;
7458 }
7459 }
7460 }
7461
7462 /* Build the partial symbol table by doing a quick pass through the
7463 .debug_info and .debug_abbrev sections. */
7464
7465 static void
7466 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7467 {
7468 struct objfile *objfile = per_objfile->objfile;
7469 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7470
7471 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7472 objfile_name (objfile));
7473
7474 scoped_restore restore_reading_psyms
7475 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7476
7477 per_bfd->info.read (objfile);
7478
7479 /* Any cached compilation units will be linked by the per-objfile
7480 read_in_chain. Make sure to free them when we're done. */
7481 free_cached_comp_units freer (per_objfile);
7482
7483 create_all_comp_units (per_objfile);
7484 build_type_psymtabs (per_objfile);
7485
7486 /* Create a temporary address map on a temporary obstack. We later
7487 copy this to the final obstack. */
7488 auto_obstack temp_obstack;
7489
7490 scoped_restore save_psymtabs_addrmap
7491 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7492 addrmap_create_mutable (&temp_obstack));
7493
7494 for (const auto &per_cu : per_bfd->all_comp_units)
7495 {
7496 if (per_cu->v.psymtab != NULL)
7497 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7498 continue;
7499 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7500 language_minimal);
7501 }
7502
7503 /* This has to wait until we read the CUs, we need the list of DWOs. */
7504 process_skeletonless_type_units (per_objfile);
7505
7506 /* Now that all TUs have been processed we can fill in the dependencies. */
7507 if (per_bfd->type_unit_groups != NULL)
7508 {
7509 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7510 build_type_psymtab_dependencies, per_objfile);
7511 }
7512
7513 if (dwarf_read_debug > 0)
7514 print_tu_stats (per_objfile);
7515
7516 set_partial_user (per_objfile);
7517
7518 per_bfd->partial_symtabs->psymtabs_addrmap
7519 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7520 per_bfd->partial_symtabs->obstack ());
7521 /* At this point we want to keep the address map. */
7522 save_psymtabs_addrmap.release ();
7523
7524 dwarf_read_debug_printf ("Done building psymtabs of %s",
7525 objfile_name (objfile));
7526 }
7527
7528 /* Load the partial DIEs for a secondary CU into memory.
7529 This is also used when rereading a primary CU with load_all_dies. */
7530
7531 static void
7532 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7533 dwarf2_per_objfile *per_objfile,
7534 dwarf2_cu *existing_cu)
7535 {
7536 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7537
7538 if (!reader.dummy_p)
7539 {
7540 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7541 language_minimal);
7542
7543 /* Check if comp unit has_children.
7544 If so, read the rest of the partial symbols from this comp unit.
7545 If not, there's no more debug_info for this comp unit. */
7546 if (reader.comp_unit_die->has_children)
7547 load_partial_dies (&reader, reader.info_ptr, 0);
7548
7549 reader.keep ();
7550 }
7551 }
7552
7553 static void
7554 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7555 struct dwarf2_section_info *section,
7556 struct dwarf2_section_info *abbrev_section,
7557 unsigned int is_dwz,
7558 htab_up &types_htab,
7559 rcuh_kind section_kind)
7560 {
7561 const gdb_byte *info_ptr;
7562 struct objfile *objfile = per_objfile->objfile;
7563
7564 dwarf_read_debug_printf ("Reading %s for %s",
7565 section->get_name (),
7566 section->get_file_name ());
7567
7568 section->read (objfile);
7569
7570 info_ptr = section->buffer;
7571
7572 while (info_ptr < section->buffer + section->size)
7573 {
7574 dwarf2_per_cu_data_up this_cu;
7575
7576 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7577
7578 comp_unit_head cu_header;
7579 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7580 abbrev_section, info_ptr,
7581 section_kind);
7582
7583 /* Save the compilation unit for later lookup. */
7584 if (cu_header.unit_type != DW_UT_type)
7585 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7586 else
7587 {
7588 if (types_htab == nullptr)
7589 types_htab = allocate_signatured_type_table ();
7590
7591 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7592 (cu_header.signature);
7593 signatured_type *sig_ptr = sig_type.get ();
7594 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7595 this_cu.reset (sig_type.release ());
7596
7597 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7598 gdb_assert (slot != nullptr);
7599 if (*slot != nullptr)
7600 complaint (_("debug type entry at offset %s is duplicate to"
7601 " the entry at offset %s, signature %s"),
7602 sect_offset_str (sect_off),
7603 sect_offset_str (sig_ptr->sect_off),
7604 hex_string (sig_ptr->signature));
7605 *slot = sig_ptr;
7606 }
7607 this_cu->sect_off = sect_off;
7608 this_cu->length = cu_header.length + cu_header.initial_length_size;
7609 this_cu->is_dwz = is_dwz;
7610 this_cu->section = section;
7611
7612 info_ptr = info_ptr + this_cu->length;
7613 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7614 }
7615 }
7616
7617 /* Create a list of all compilation units in OBJFILE.
7618 This is only done for -readnow and building partial symtabs. */
7619
7620 static void
7621 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7622 {
7623 htab_up types_htab;
7624
7625 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7626 &per_objfile->per_bfd->abbrev, 0,
7627 types_htab, rcuh_kind::COMPILE);
7628 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7629 read_comp_units_from_section (per_objfile, &section,
7630 &per_objfile->per_bfd->abbrev, 0,
7631 types_htab, rcuh_kind::TYPE);
7632
7633 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7634 if (dwz != NULL)
7635 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7636 types_htab, rcuh_kind::COMPILE);
7637
7638 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7639 }
7640
7641 /* Process all loaded DIEs for compilation unit CU, starting at
7642 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7643 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7644 DW_AT_ranges). See the comments of add_partial_subprogram on how
7645 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7646
7647 static void
7648 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7649 CORE_ADDR *highpc, int set_addrmap,
7650 struct dwarf2_cu *cu)
7651 {
7652 struct partial_die_info *pdi;
7653
7654 /* Now, march along the PDI's, descending into ones which have
7655 interesting children but skipping the children of the other ones,
7656 until we reach the end of the compilation unit. */
7657
7658 pdi = first_die;
7659
7660 while (pdi != NULL)
7661 {
7662 pdi->fixup (cu);
7663
7664 /* Anonymous namespaces or modules have no name but have interesting
7665 children, so we need to look at them. Ditto for anonymous
7666 enums. */
7667
7668 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7669 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7670 || pdi->tag == DW_TAG_imported_unit
7671 || pdi->tag == DW_TAG_inlined_subroutine)
7672 {
7673 switch (pdi->tag)
7674 {
7675 case DW_TAG_subprogram:
7676 case DW_TAG_inlined_subroutine:
7677 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7678 if (cu->per_cu->lang == language_cplus)
7679 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7680 set_addrmap, cu);
7681 break;
7682 case DW_TAG_constant:
7683 case DW_TAG_variable:
7684 case DW_TAG_typedef:
7685 case DW_TAG_union_type:
7686 if (!pdi->is_declaration
7687 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7688 {
7689 add_partial_symbol (pdi, cu);
7690 }
7691 break;
7692 case DW_TAG_class_type:
7693 case DW_TAG_interface_type:
7694 case DW_TAG_structure_type:
7695 if (!pdi->is_declaration)
7696 {
7697 add_partial_symbol (pdi, cu);
7698 }
7699 if ((cu->per_cu->lang == language_rust
7700 || cu->per_cu->lang == language_cplus)
7701 && pdi->has_children)
7702 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7703 set_addrmap, cu);
7704 break;
7705 case DW_TAG_enumeration_type:
7706 if (!pdi->is_declaration)
7707 add_partial_enumeration (pdi, cu);
7708 break;
7709 case DW_TAG_base_type:
7710 case DW_TAG_subrange_type:
7711 /* File scope base type definitions are added to the partial
7712 symbol table. */
7713 add_partial_symbol (pdi, cu);
7714 break;
7715 case DW_TAG_namespace:
7716 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7717 break;
7718 case DW_TAG_module:
7719 if (!pdi->is_declaration)
7720 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7721 break;
7722 case DW_TAG_imported_unit:
7723 {
7724 struct dwarf2_per_cu_data *per_cu;
7725
7726 /* For now we don't handle imported units in type units. */
7727 if (cu->per_cu->is_debug_types)
7728 {
7729 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7730 " supported in type units [in module %s]"),
7731 objfile_name (cu->per_objfile->objfile));
7732 }
7733
7734 per_cu = dwarf2_find_containing_comp_unit
7735 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
7736
7737 /* Go read the partial unit, if needed. */
7738 if (per_cu->v.psymtab == NULL)
7739 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7740 cu->per_cu->lang);
7741
7742 if (pdi->die_parent == nullptr
7743 && per_cu->unit_type == DW_UT_compile
7744 && per_cu->lang == language_cplus)
7745 /* Regard import as hint. See corresponding code in
7746 process_imported_unit_die. */
7747 break;
7748
7749 cu->per_cu->imported_symtabs_push (per_cu);
7750 }
7751 break;
7752 case DW_TAG_imported_declaration:
7753 add_partial_symbol (pdi, cu);
7754 break;
7755 default:
7756 break;
7757 }
7758 }
7759
7760 /* If the die has a sibling, skip to the sibling. */
7761
7762 pdi = pdi->die_sibling;
7763 }
7764 }
7765
7766 /* Functions used to compute the fully scoped name of a partial DIE.
7767
7768 Normally, this is simple. For C++, the parent DIE's fully scoped
7769 name is concatenated with "::" and the partial DIE's name.
7770 Enumerators are an exception; they use the scope of their parent
7771 enumeration type, i.e. the name of the enumeration type is not
7772 prepended to the enumerator.
7773
7774 There are two complexities. One is DW_AT_specification; in this
7775 case "parent" means the parent of the target of the specification,
7776 instead of the direct parent of the DIE. The other is compilers
7777 which do not emit DW_TAG_namespace; in this case we try to guess
7778 the fully qualified name of structure types from their members'
7779 linkage names. This must be done using the DIE's children rather
7780 than the children of any DW_AT_specification target. We only need
7781 to do this for structures at the top level, i.e. if the target of
7782 any DW_AT_specification (if any; otherwise the DIE itself) does not
7783 have a parent. */
7784
7785 /* Compute the scope prefix associated with PDI's parent, in
7786 compilation unit CU. The result will be allocated on CU's
7787 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7788 field. NULL is returned if no prefix is necessary. */
7789 static const char *
7790 partial_die_parent_scope (struct partial_die_info *pdi,
7791 struct dwarf2_cu *cu)
7792 {
7793 const char *grandparent_scope;
7794 struct partial_die_info *parent, *real_pdi;
7795
7796 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7797 then this means the parent of the specification DIE. */
7798
7799 real_pdi = pdi;
7800 while (real_pdi->has_specification)
7801 {
7802 auto res = find_partial_die (real_pdi->spec_offset,
7803 real_pdi->spec_is_dwz, cu);
7804 real_pdi = res.pdi;
7805 cu = res.cu;
7806 }
7807
7808 parent = real_pdi->die_parent;
7809 if (parent == NULL)
7810 return NULL;
7811
7812 if (parent->scope_set)
7813 return parent->scope;
7814
7815 parent->fixup (cu);
7816
7817 grandparent_scope = partial_die_parent_scope (parent, cu);
7818
7819 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7820 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7821 Work around this problem here. */
7822 if (cu->per_cu->lang == language_cplus
7823 && parent->tag == DW_TAG_namespace
7824 && strcmp (parent->name (cu), "::") == 0
7825 && grandparent_scope == NULL)
7826 {
7827 parent->scope = NULL;
7828 parent->scope_set = 1;
7829 return NULL;
7830 }
7831
7832 /* Nested subroutines in Fortran get a prefix. */
7833 if (pdi->tag == DW_TAG_enumerator)
7834 /* Enumerators should not get the name of the enumeration as a prefix. */
7835 parent->scope = grandparent_scope;
7836 else if (parent->tag == DW_TAG_namespace
7837 || parent->tag == DW_TAG_module
7838 || parent->tag == DW_TAG_structure_type
7839 || parent->tag == DW_TAG_class_type
7840 || parent->tag == DW_TAG_interface_type
7841 || parent->tag == DW_TAG_union_type
7842 || parent->tag == DW_TAG_enumeration_type
7843 || (cu->per_cu->lang == language_fortran
7844 && parent->tag == DW_TAG_subprogram
7845 && pdi->tag == DW_TAG_subprogram))
7846 {
7847 if (grandparent_scope == NULL)
7848 parent->scope = parent->name (cu);
7849 else
7850 parent->scope = typename_concat (&cu->comp_unit_obstack,
7851 grandparent_scope,
7852 parent->name (cu), 0, cu);
7853 }
7854 else
7855 {
7856 /* FIXME drow/2004-04-01: What should we be doing with
7857 function-local names? For partial symbols, we should probably be
7858 ignoring them. */
7859 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7860 dwarf_tag_name (parent->tag),
7861 sect_offset_str (pdi->sect_off));
7862 parent->scope = grandparent_scope;
7863 }
7864
7865 parent->scope_set = 1;
7866 return parent->scope;
7867 }
7868
7869 /* Return the fully scoped name associated with PDI, from compilation unit
7870 CU. The result will be allocated with malloc. */
7871
7872 static gdb::unique_xmalloc_ptr<char>
7873 partial_die_full_name (struct partial_die_info *pdi,
7874 struct dwarf2_cu *cu)
7875 {
7876 const char *parent_scope;
7877
7878 /* If this is a template instantiation, we can not work out the
7879 template arguments from partial DIEs. So, unfortunately, we have
7880 to go through the full DIEs. At least any work we do building
7881 types here will be reused if full symbols are loaded later. */
7882 if (pdi->has_template_arguments)
7883 {
7884 pdi->fixup (cu);
7885
7886 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7887 {
7888 struct die_info *die;
7889 struct attribute attr;
7890 struct dwarf2_cu *ref_cu = cu;
7891
7892 /* DW_FORM_ref_addr is using section offset. */
7893 attr.name = (enum dwarf_attribute) 0;
7894 attr.form = DW_FORM_ref_addr;
7895 attr.u.unsnd = to_underlying (pdi->sect_off);
7896 die = follow_die_ref (NULL, &attr, &ref_cu);
7897
7898 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7899 }
7900 }
7901
7902 parent_scope = partial_die_parent_scope (pdi, cu);
7903 if (parent_scope == NULL)
7904 return NULL;
7905 else
7906 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7907 pdi->name (cu),
7908 0, cu));
7909 }
7910
7911 static void
7912 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7913 {
7914 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7915 struct objfile *objfile = per_objfile->objfile;
7916 struct gdbarch *gdbarch = objfile->arch ();
7917 CORE_ADDR addr = 0;
7918 const char *actual_name = NULL;
7919 CORE_ADDR baseaddr;
7920
7921 baseaddr = objfile->text_section_offset ();
7922
7923 gdb::unique_xmalloc_ptr<char> built_actual_name
7924 = partial_die_full_name (pdi, cu);
7925 if (built_actual_name != NULL)
7926 actual_name = built_actual_name.get ();
7927
7928 if (actual_name == NULL)
7929 actual_name = pdi->name (cu);
7930
7931 partial_symbol psymbol;
7932 memset (&psymbol, 0, sizeof (psymbol));
7933 psymbol.ginfo.set_language (cu->per_cu->lang,
7934 &objfile->objfile_obstack);
7935 psymbol.ginfo.set_section_index (-1);
7936
7937 /* The code below indicates that the psymbol should be installed by
7938 setting this. */
7939 gdb::optional<psymbol_placement> where;
7940
7941 switch (pdi->tag)
7942 {
7943 case DW_TAG_inlined_subroutine:
7944 case DW_TAG_subprogram:
7945 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7946 - baseaddr);
7947 if (pdi->is_external
7948 || cu->per_cu->lang == language_ada
7949 || (cu->per_cu->lang == language_fortran
7950 && pdi->die_parent != NULL
7951 && pdi->die_parent->tag == DW_TAG_subprogram))
7952 {
7953 /* Normally, only "external" DIEs are part of the global scope.
7954 But in Ada and Fortran, we want to be able to access nested
7955 procedures globally. So all Ada and Fortran subprograms are
7956 stored in the global scope. */
7957 where = psymbol_placement::GLOBAL;
7958 }
7959 else
7960 where = psymbol_placement::STATIC;
7961
7962 psymbol.domain = VAR_DOMAIN;
7963 psymbol.aclass = LOC_BLOCK;
7964 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7965 psymbol.ginfo.value.address = addr;
7966
7967 if (pdi->main_subprogram && actual_name != NULL)
7968 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
7969 break;
7970 case DW_TAG_constant:
7971 psymbol.domain = VAR_DOMAIN;
7972 psymbol.aclass = LOC_STATIC;
7973 where = (pdi->is_external
7974 ? psymbol_placement::GLOBAL
7975 : psymbol_placement::STATIC);
7976 break;
7977 case DW_TAG_variable:
7978 if (pdi->d.locdesc)
7979 addr = decode_locdesc (pdi->d.locdesc, cu);
7980
7981 if (pdi->d.locdesc
7982 && addr == 0
7983 && !per_objfile->per_bfd->has_section_at_zero)
7984 {
7985 /* A global or static variable may also have been stripped
7986 out by the linker if unused, in which case its address
7987 will be nullified; do not add such variables into partial
7988 symbol table then. */
7989 }
7990 else if (pdi->is_external)
7991 {
7992 /* Global Variable.
7993 Don't enter into the minimal symbol tables as there is
7994 a minimal symbol table entry from the ELF symbols already.
7995 Enter into partial symbol table if it has a location
7996 descriptor or a type.
7997 If the location descriptor is missing, new_symbol will create
7998 a LOC_UNRESOLVED symbol, the address of the variable will then
7999 be determined from the minimal symbol table whenever the variable
8000 is referenced.
8001 The address for the partial symbol table entry is not
8002 used by GDB, but it comes in handy for debugging partial symbol
8003 table building. */
8004
8005 if (pdi->d.locdesc || pdi->has_type)
8006 {
8007 psymbol.domain = VAR_DOMAIN;
8008 psymbol.aclass = LOC_STATIC;
8009 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8010 psymbol.ginfo.value.address = addr;
8011 where = psymbol_placement::GLOBAL;
8012 }
8013 }
8014 else
8015 {
8016 int has_loc = pdi->d.locdesc != NULL;
8017
8018 /* Static Variable. Skip symbols whose value we cannot know (those
8019 without location descriptors or constant values). */
8020 if (!has_loc && !pdi->has_const_value)
8021 return;
8022
8023 psymbol.domain = VAR_DOMAIN;
8024 psymbol.aclass = LOC_STATIC;
8025 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8026 if (has_loc)
8027 psymbol.ginfo.value.address = addr;
8028 where = psymbol_placement::STATIC;
8029 }
8030 break;
8031 case DW_TAG_array_type:
8032 case DW_TAG_typedef:
8033 case DW_TAG_base_type:
8034 case DW_TAG_subrange_type:
8035 psymbol.domain = VAR_DOMAIN;
8036 psymbol.aclass = LOC_TYPEDEF;
8037 where = psymbol_placement::STATIC;
8038 break;
8039 case DW_TAG_imported_declaration:
8040 case DW_TAG_namespace:
8041 psymbol.domain = VAR_DOMAIN;
8042 psymbol.aclass = LOC_TYPEDEF;
8043 where = psymbol_placement::GLOBAL;
8044 break;
8045 case DW_TAG_module:
8046 /* With Fortran 77 there might be a "BLOCK DATA" module
8047 available without any name. If so, we skip the module as it
8048 doesn't bring any value. */
8049 if (actual_name != nullptr)
8050 {
8051 psymbol.domain = MODULE_DOMAIN;
8052 psymbol.aclass = LOC_TYPEDEF;
8053 where = psymbol_placement::GLOBAL;
8054 }
8055 break;
8056 case DW_TAG_class_type:
8057 case DW_TAG_interface_type:
8058 case DW_TAG_structure_type:
8059 case DW_TAG_union_type:
8060 case DW_TAG_enumeration_type:
8061 /* Skip external references. The DWARF standard says in the section
8062 about "Structure, Union, and Class Type Entries": "An incomplete
8063 structure, union or class type is represented by a structure,
8064 union or class entry that does not have a byte size attribute
8065 and that has a DW_AT_declaration attribute." */
8066 if (!pdi->has_byte_size && pdi->is_declaration)
8067 return;
8068
8069 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8070 static vs. global. */
8071 psymbol.domain = STRUCT_DOMAIN;
8072 psymbol.aclass = LOC_TYPEDEF;
8073 where = (cu->per_cu->lang == language_cplus
8074 ? psymbol_placement::GLOBAL
8075 : psymbol_placement::STATIC);
8076 break;
8077 case DW_TAG_enumerator:
8078 psymbol.domain = VAR_DOMAIN;
8079 psymbol.aclass = LOC_CONST;
8080 where = (cu->per_cu->lang == language_cplus
8081 ? psymbol_placement::GLOBAL
8082 : psymbol_placement::STATIC);
8083 break;
8084 default:
8085 break;
8086 }
8087
8088 if (where.has_value ())
8089 {
8090 if (built_actual_name != nullptr)
8091 actual_name = objfile->intern (actual_name);
8092 if (pdi->linkage_name == nullptr
8093 || cu->per_cu->lang == language_ada)
8094 psymbol.ginfo.set_linkage_name (actual_name);
8095 else
8096 {
8097 psymbol.ginfo.set_demangled_name (actual_name,
8098 &objfile->objfile_obstack);
8099 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8100 }
8101 cu->per_cu->v.psymtab->add_psymbol
8102 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8103 objfile);
8104 }
8105 }
8106
8107 /* Read a partial die corresponding to a namespace; also, add a symbol
8108 corresponding to that namespace to the symbol table. NAMESPACE is
8109 the name of the enclosing namespace. */
8110
8111 static void
8112 add_partial_namespace (struct partial_die_info *pdi,
8113 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8114 int set_addrmap, struct dwarf2_cu *cu)
8115 {
8116 /* Add a symbol for the namespace. */
8117
8118 add_partial_symbol (pdi, cu);
8119
8120 /* Now scan partial symbols in that namespace. */
8121
8122 if (pdi->has_children)
8123 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8124 }
8125
8126 /* Read a partial die corresponding to a Fortran module. */
8127
8128 static void
8129 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8130 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8131 {
8132 /* Add a symbol for the namespace. */
8133
8134 add_partial_symbol (pdi, cu);
8135
8136 /* Now scan partial symbols in that module. */
8137
8138 if (pdi->has_children)
8139 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8140 }
8141
8142 static int
8143 dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
8144 dwarf2_psymtab *, dwarf_tag);
8145
8146 /* Read a partial die corresponding to a subprogram or an inlined
8147 subprogram and create a partial symbol for that subprogram.
8148 When the CU language allows it, this routine also defines a partial
8149 symbol for each nested subprogram that this subprogram contains.
8150 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8151 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8152
8153 PDI may also be a lexical block, in which case we simply search
8154 recursively for subprograms defined inside that lexical block.
8155 Again, this is only performed when the CU language allows this
8156 type of definitions. */
8157
8158 static void
8159 add_partial_subprogram (struct partial_die_info *pdi,
8160 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8161 int set_addrmap, struct dwarf2_cu *cu)
8162 {
8163 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8164 {
8165 if (pdi->has_pc_info)
8166 {
8167 if (pdi->lowpc < *lowpc)
8168 *lowpc = pdi->lowpc;
8169 if (pdi->highpc > *highpc)
8170 *highpc = pdi->highpc;
8171 if (set_addrmap)
8172 {
8173 struct objfile *objfile = cu->per_objfile->objfile;
8174 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8175 struct gdbarch *gdbarch = objfile->arch ();
8176 CORE_ADDR baseaddr;
8177 CORE_ADDR this_highpc;
8178 CORE_ADDR this_lowpc;
8179
8180 baseaddr = objfile->text_section_offset ();
8181 this_lowpc
8182 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8183 pdi->lowpc + baseaddr)
8184 - baseaddr);
8185 this_highpc
8186 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8187 pdi->highpc + baseaddr)
8188 - baseaddr);
8189 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8190 this_lowpc, this_highpc - 1,
8191 cu->per_cu->v.psymtab);
8192 }
8193 }
8194
8195 if (pdi->has_range_info
8196 && dwarf2_ranges_read (pdi->ranges_offset, &pdi->lowpc, &pdi->highpc,
8197 cu,
8198 set_addrmap ? cu->per_cu->v.psymtab : nullptr,
8199 pdi->tag))
8200 {
8201 if (pdi->lowpc < *lowpc)
8202 *lowpc = pdi->lowpc;
8203 if (pdi->highpc > *highpc)
8204 *highpc = pdi->highpc;
8205 }
8206
8207 if (pdi->has_pc_info || pdi->has_range_info
8208 || (!pdi->is_external && pdi->may_be_inlined))
8209 {
8210 if (!pdi->is_declaration)
8211 /* Ignore subprogram DIEs that do not have a name, they are
8212 illegal. Do not emit a complaint at this point, we will
8213 do so when we convert this psymtab into a symtab. */
8214 if (pdi->name (cu))
8215 add_partial_symbol (pdi, cu);
8216 }
8217 }
8218
8219 if (! pdi->has_children)
8220 return;
8221
8222 if (cu->per_cu->lang == language_ada
8223 || cu->per_cu->lang == language_fortran)
8224 {
8225 pdi = pdi->die_child;
8226 while (pdi != NULL)
8227 {
8228 pdi->fixup (cu);
8229 if (pdi->tag == DW_TAG_subprogram
8230 || pdi->tag == DW_TAG_inlined_subroutine
8231 || pdi->tag == DW_TAG_lexical_block)
8232 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8233 pdi = pdi->die_sibling;
8234 }
8235 }
8236 }
8237
8238 /* Read a partial die corresponding to an enumeration type. */
8239
8240 static void
8241 add_partial_enumeration (struct partial_die_info *enum_pdi,
8242 struct dwarf2_cu *cu)
8243 {
8244 struct partial_die_info *pdi;
8245
8246 if (enum_pdi->name (cu) != NULL)
8247 add_partial_symbol (enum_pdi, cu);
8248
8249 pdi = enum_pdi->die_child;
8250 while (pdi)
8251 {
8252 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8253 complaint (_("malformed enumerator DIE ignored"));
8254 else
8255 add_partial_symbol (pdi, cu);
8256 pdi = pdi->die_sibling;
8257 }
8258 }
8259
8260 /* Return the initial uleb128 in the die at INFO_PTR. */
8261
8262 static unsigned int
8263 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8264 {
8265 unsigned int bytes_read;
8266
8267 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8268 }
8269
8270 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8271 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8272
8273 Return the corresponding abbrev, or NULL if the number is zero (indicating
8274 an empty DIE). In either case *BYTES_READ will be set to the length of
8275 the initial number. */
8276
8277 static const struct abbrev_info *
8278 peek_die_abbrev (const die_reader_specs &reader,
8279 const gdb_byte *info_ptr, unsigned int *bytes_read)
8280 {
8281 dwarf2_cu *cu = reader.cu;
8282 bfd *abfd = reader.abfd;
8283 unsigned int abbrev_number
8284 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8285
8286 if (abbrev_number == 0)
8287 return NULL;
8288
8289 const abbrev_info *abbrev
8290 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8291 if (!abbrev)
8292 {
8293 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8294 " at offset %s [in module %s]"),
8295 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8296 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8297 }
8298
8299 return abbrev;
8300 }
8301
8302 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8303 Returns a pointer to the end of a series of DIEs, terminated by an empty
8304 DIE. Any children of the skipped DIEs will also be skipped. */
8305
8306 static const gdb_byte *
8307 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8308 {
8309 while (1)
8310 {
8311 unsigned int bytes_read;
8312 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8313 &bytes_read);
8314
8315 if (abbrev == NULL)
8316 return info_ptr + bytes_read;
8317 else
8318 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8319 }
8320 }
8321
8322 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8323 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8324 abbrev corresponding to that skipped uleb128 should be passed in
8325 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8326 children. */
8327
8328 static const gdb_byte *
8329 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8330 const struct abbrev_info *abbrev)
8331 {
8332 unsigned int bytes_read;
8333 struct attribute attr;
8334 bfd *abfd = reader->abfd;
8335 struct dwarf2_cu *cu = reader->cu;
8336 const gdb_byte *buffer = reader->buffer;
8337 const gdb_byte *buffer_end = reader->buffer_end;
8338 unsigned int form, i;
8339
8340 for (i = 0; i < abbrev->num_attrs; i++)
8341 {
8342 /* The only abbrev we care about is DW_AT_sibling. */
8343 if (abbrev->attrs[i].name == DW_AT_sibling)
8344 {
8345 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8346 if (attr.form == DW_FORM_ref_addr)
8347 complaint (_("ignoring absolute DW_AT_sibling"));
8348 else
8349 {
8350 sect_offset off = attr.get_ref_die_offset ();
8351 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8352
8353 if (sibling_ptr < info_ptr)
8354 complaint (_("DW_AT_sibling points backwards"));
8355 else if (sibling_ptr > reader->buffer_end)
8356 reader->die_section->overflow_complaint ();
8357 else
8358 return sibling_ptr;
8359 }
8360 }
8361
8362 /* If it isn't DW_AT_sibling, skip this attribute. */
8363 form = abbrev->attrs[i].form;
8364 skip_attribute:
8365 switch (form)
8366 {
8367 case DW_FORM_ref_addr:
8368 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8369 and later it is offset sized. */
8370 if (cu->header.version == 2)
8371 info_ptr += cu->header.addr_size;
8372 else
8373 info_ptr += cu->header.offset_size;
8374 break;
8375 case DW_FORM_GNU_ref_alt:
8376 info_ptr += cu->header.offset_size;
8377 break;
8378 case DW_FORM_addr:
8379 info_ptr += cu->header.addr_size;
8380 break;
8381 case DW_FORM_data1:
8382 case DW_FORM_ref1:
8383 case DW_FORM_flag:
8384 case DW_FORM_strx1:
8385 info_ptr += 1;
8386 break;
8387 case DW_FORM_flag_present:
8388 case DW_FORM_implicit_const:
8389 break;
8390 case DW_FORM_data2:
8391 case DW_FORM_ref2:
8392 case DW_FORM_strx2:
8393 info_ptr += 2;
8394 break;
8395 case DW_FORM_strx3:
8396 info_ptr += 3;
8397 break;
8398 case DW_FORM_data4:
8399 case DW_FORM_ref4:
8400 case DW_FORM_strx4:
8401 info_ptr += 4;
8402 break;
8403 case DW_FORM_data8:
8404 case DW_FORM_ref8:
8405 case DW_FORM_ref_sig8:
8406 info_ptr += 8;
8407 break;
8408 case DW_FORM_data16:
8409 info_ptr += 16;
8410 break;
8411 case DW_FORM_string:
8412 read_direct_string (abfd, info_ptr, &bytes_read);
8413 info_ptr += bytes_read;
8414 break;
8415 case DW_FORM_sec_offset:
8416 case DW_FORM_strp:
8417 case DW_FORM_GNU_strp_alt:
8418 info_ptr += cu->header.offset_size;
8419 break;
8420 case DW_FORM_exprloc:
8421 case DW_FORM_block:
8422 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8423 info_ptr += bytes_read;
8424 break;
8425 case DW_FORM_block1:
8426 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8427 break;
8428 case DW_FORM_block2:
8429 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8430 break;
8431 case DW_FORM_block4:
8432 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8433 break;
8434 case DW_FORM_addrx:
8435 case DW_FORM_strx:
8436 case DW_FORM_sdata:
8437 case DW_FORM_udata:
8438 case DW_FORM_ref_udata:
8439 case DW_FORM_GNU_addr_index:
8440 case DW_FORM_GNU_str_index:
8441 case DW_FORM_rnglistx:
8442 case DW_FORM_loclistx:
8443 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8444 break;
8445 case DW_FORM_indirect:
8446 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8447 info_ptr += bytes_read;
8448 /* We need to continue parsing from here, so just go back to
8449 the top. */
8450 goto skip_attribute;
8451
8452 default:
8453 error (_("Dwarf Error: Cannot handle %s "
8454 "in DWARF reader [in module %s]"),
8455 dwarf_form_name (form),
8456 bfd_get_filename (abfd));
8457 }
8458 }
8459
8460 if (abbrev->has_children)
8461 return skip_children (reader, info_ptr);
8462 else
8463 return info_ptr;
8464 }
8465
8466 /* Locate ORIG_PDI's sibling.
8467 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8468
8469 static const gdb_byte *
8470 locate_pdi_sibling (const struct die_reader_specs *reader,
8471 struct partial_die_info *orig_pdi,
8472 const gdb_byte *info_ptr)
8473 {
8474 /* Do we know the sibling already? */
8475
8476 if (orig_pdi->sibling)
8477 return orig_pdi->sibling;
8478
8479 /* Are there any children to deal with? */
8480
8481 if (!orig_pdi->has_children)
8482 return info_ptr;
8483
8484 /* Skip the children the long way. */
8485
8486 return skip_children (reader, info_ptr);
8487 }
8488
8489 /* Expand this partial symbol table into a full symbol table. SELF is
8490 not NULL. */
8491
8492 void
8493 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8494 {
8495 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8496
8497 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8498
8499 /* If this psymtab is constructed from a debug-only objfile, the
8500 has_section_at_zero flag will not necessarily be correct. We
8501 can get the correct value for this flag by looking at the data
8502 associated with the (presumably stripped) associated objfile. */
8503 if (objfile->separate_debug_objfile_backlink)
8504 {
8505 dwarf2_per_objfile *per_objfile_backlink
8506 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8507
8508 per_objfile->per_bfd->has_section_at_zero
8509 = per_objfile_backlink->per_bfd->has_section_at_zero;
8510 }
8511
8512 expand_psymtab (objfile);
8513
8514 process_cu_includes (per_objfile);
8515 }
8516 \f
8517 /* Reading in full CUs. */
8518
8519 /* Add PER_CU to the queue. */
8520
8521 static void
8522 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8523 dwarf2_per_objfile *per_objfile,
8524 enum language pretend_language)
8525 {
8526 per_cu->queued = 1;
8527
8528 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8529 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8530 }
8531
8532 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8533
8534 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8535 dependency.
8536
8537 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8538 DIEs, false otherwise.
8539
8540 Explanation: there is an invariant that if a CU is queued for expansion
8541 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8542 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8543 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8544 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8545 invariant is respected.
8546
8547 The caller is therefore not required to load the CU's DIEs (we return false)
8548 if:
8549
8550 - the CU is already expanded, and therefore does not get enqueued
8551 - the CU gets enqueued for expansion, but its DIEs are already loaded
8552
8553 Note that the caller should not use this function's return value as an
8554 indicator of whether the CU's DIEs are loaded right now, it should check
8555 that by calling `dwarf2_per_objfile::get_cu` instead. */
8556
8557 static int
8558 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8559 dwarf2_per_cu_data *per_cu,
8560 dwarf2_per_objfile *per_objfile,
8561 enum language pretend_language)
8562 {
8563 /* We may arrive here during partial symbol reading, if we need full
8564 DIEs to process an unusual case (e.g. template arguments). Do
8565 not queue PER_CU, just tell our caller to load its DIEs. */
8566 if (per_cu->per_bfd->reading_partial_symbols)
8567 {
8568 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8569
8570 if (cu == NULL || cu->dies == NULL)
8571 return 1;
8572 return 0;
8573 }
8574
8575 /* Mark the dependence relation so that we don't flush PER_CU
8576 too early. */
8577 if (dependent_cu != NULL)
8578 dependent_cu->add_dependence (per_cu);
8579
8580 /* If it's already on the queue, we have nothing to do. */
8581 if (per_cu->queued)
8582 {
8583 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8584 loaded. */
8585 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8586
8587 /* If the CU is queued for expansion, it should not already be
8588 expanded. */
8589 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8590
8591 /* The DIEs are already loaded, the caller doesn't need to do it. */
8592 return 0;
8593 }
8594
8595 bool queued = false;
8596 if (!per_objfile->symtab_set_p (per_cu))
8597 {
8598 /* Add it to the queue. */
8599 queue_comp_unit (per_cu, per_objfile, pretend_language);
8600 queued = true;
8601 }
8602
8603 /* If the compilation unit is already loaded, just mark it as
8604 used. */
8605 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8606 if (cu != nullptr)
8607 cu->last_used = 0;
8608
8609 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8610 and the DIEs are not already loaded. */
8611 return queued && cu == nullptr;
8612 }
8613
8614 /* Process the queue. */
8615
8616 static void
8617 process_queue (dwarf2_per_objfile *per_objfile)
8618 {
8619 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8620 objfile_name (per_objfile->objfile));
8621
8622 /* The queue starts out with one item, but following a DIE reference
8623 may load a new CU, adding it to the end of the queue. */
8624 while (!per_objfile->per_bfd->queue->empty ())
8625 {
8626 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8627 dwarf2_per_cu_data *per_cu = item.per_cu;
8628
8629 if (!per_objfile->symtab_set_p (per_cu))
8630 {
8631 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8632
8633 /* Skip dummy CUs. */
8634 if (cu != nullptr)
8635 {
8636 unsigned int debug_print_threshold;
8637 char buf[100];
8638
8639 if (per_cu->is_debug_types)
8640 {
8641 struct signatured_type *sig_type =
8642 (struct signatured_type *) per_cu;
8643
8644 sprintf (buf, "TU %s at offset %s",
8645 hex_string (sig_type->signature),
8646 sect_offset_str (per_cu->sect_off));
8647 /* There can be 100s of TUs.
8648 Only print them in verbose mode. */
8649 debug_print_threshold = 2;
8650 }
8651 else
8652 {
8653 sprintf (buf, "CU at offset %s",
8654 sect_offset_str (per_cu->sect_off));
8655 debug_print_threshold = 1;
8656 }
8657
8658 if (dwarf_read_debug >= debug_print_threshold)
8659 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8660
8661 if (per_cu->is_debug_types)
8662 process_full_type_unit (cu, item.pretend_language);
8663 else
8664 process_full_comp_unit (cu, item.pretend_language);
8665
8666 if (dwarf_read_debug >= debug_print_threshold)
8667 dwarf_read_debug_printf ("Done expanding %s", buf);
8668 }
8669 }
8670
8671 per_cu->queued = 0;
8672 per_objfile->per_bfd->queue->pop ();
8673 }
8674
8675 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8676 objfile_name (per_objfile->objfile));
8677 }
8678
8679 /* Read in full symbols for PST, and anything it depends on. */
8680
8681 void
8682 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8683 {
8684 gdb_assert (!readin_p (objfile));
8685
8686 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8687 free_cached_comp_units freer (per_objfile);
8688 expand_dependencies (objfile);
8689
8690 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8691 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8692 }
8693
8694 /* See psympriv.h. */
8695
8696 bool
8697 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8698 {
8699 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8700 return per_objfile->symtab_set_p (per_cu_data);
8701 }
8702
8703 /* See psympriv.h. */
8704
8705 compunit_symtab *
8706 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8707 {
8708 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8709 return per_objfile->get_symtab (per_cu_data);
8710 }
8711
8712 /* Trivial hash function for die_info: the hash value of a DIE
8713 is its offset in .debug_info for this objfile. */
8714
8715 static hashval_t
8716 die_hash (const void *item)
8717 {
8718 const struct die_info *die = (const struct die_info *) item;
8719
8720 return to_underlying (die->sect_off);
8721 }
8722
8723 /* Trivial comparison function for die_info structures: two DIEs
8724 are equal if they have the same offset. */
8725
8726 static int
8727 die_eq (const void *item_lhs, const void *item_rhs)
8728 {
8729 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8730 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8731
8732 return die_lhs->sect_off == die_rhs->sect_off;
8733 }
8734
8735 /* Load the DIEs associated with PER_CU into memory.
8736
8737 In some cases, the caller, while reading partial symbols, will need to load
8738 the full symbols for the CU for some reason. It will already have a
8739 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8740 rather than creating a new one. */
8741
8742 static void
8743 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8744 dwarf2_per_objfile *per_objfile,
8745 dwarf2_cu *existing_cu,
8746 bool skip_partial,
8747 enum language pretend_language)
8748 {
8749 gdb_assert (! this_cu->is_debug_types);
8750
8751 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8752 if (reader.dummy_p)
8753 return;
8754
8755 struct dwarf2_cu *cu = reader.cu;
8756 const gdb_byte *info_ptr = reader.info_ptr;
8757
8758 gdb_assert (cu->die_hash == NULL);
8759 cu->die_hash =
8760 htab_create_alloc_ex (cu->header.length / 12,
8761 die_hash,
8762 die_eq,
8763 NULL,
8764 &cu->comp_unit_obstack,
8765 hashtab_obstack_allocate,
8766 dummy_obstack_deallocate);
8767
8768 if (reader.comp_unit_die->has_children)
8769 reader.comp_unit_die->child
8770 = read_die_and_siblings (&reader, reader.info_ptr,
8771 &info_ptr, reader.comp_unit_die);
8772 cu->dies = reader.comp_unit_die;
8773 /* comp_unit_die is not stored in die_hash, no need. */
8774
8775 /* We try not to read any attributes in this function, because not
8776 all CUs needed for references have been loaded yet, and symbol
8777 table processing isn't initialized. But we have to set the CU language,
8778 or we won't be able to build types correctly.
8779 Similarly, if we do not read the producer, we can not apply
8780 producer-specific interpretation. */
8781 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8782
8783 reader.keep ();
8784 }
8785
8786 /* Add a DIE to the delayed physname list. */
8787
8788 static void
8789 add_to_method_list (struct type *type, int fnfield_index, int index,
8790 const char *name, struct die_info *die,
8791 struct dwarf2_cu *cu)
8792 {
8793 struct delayed_method_info mi;
8794 mi.type = type;
8795 mi.fnfield_index = fnfield_index;
8796 mi.index = index;
8797 mi.name = name;
8798 mi.die = die;
8799 cu->method_list.push_back (mi);
8800 }
8801
8802 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8803 "const" / "volatile". If so, decrements LEN by the length of the
8804 modifier and return true. Otherwise return false. */
8805
8806 template<size_t N>
8807 static bool
8808 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8809 {
8810 size_t mod_len = sizeof (mod) - 1;
8811 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8812 {
8813 len -= mod_len;
8814 return true;
8815 }
8816 return false;
8817 }
8818
8819 /* Compute the physnames of any methods on the CU's method list.
8820
8821 The computation of method physnames is delayed in order to avoid the
8822 (bad) condition that one of the method's formal parameters is of an as yet
8823 incomplete type. */
8824
8825 static void
8826 compute_delayed_physnames (struct dwarf2_cu *cu)
8827 {
8828 /* Only C++ delays computing physnames. */
8829 if (cu->method_list.empty ())
8830 return;
8831 gdb_assert (cu->per_cu->lang == language_cplus);
8832
8833 for (const delayed_method_info &mi : cu->method_list)
8834 {
8835 const char *physname;
8836 struct fn_fieldlist *fn_flp
8837 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8838 physname = dwarf2_physname (mi.name, mi.die, cu);
8839 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8840 = physname ? physname : "";
8841
8842 /* Since there's no tag to indicate whether a method is a
8843 const/volatile overload, extract that information out of the
8844 demangled name. */
8845 if (physname != NULL)
8846 {
8847 size_t len = strlen (physname);
8848
8849 while (1)
8850 {
8851 if (physname[len] == ')') /* shortcut */
8852 break;
8853 else if (check_modifier (physname, len, " const"))
8854 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8855 else if (check_modifier (physname, len, " volatile"))
8856 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8857 else
8858 break;
8859 }
8860 }
8861 }
8862
8863 /* The list is no longer needed. */
8864 cu->method_list.clear ();
8865 }
8866
8867 /* Go objects should be embedded in a DW_TAG_module DIE,
8868 and it's not clear if/how imported objects will appear.
8869 To keep Go support simple until that's worked out,
8870 go back through what we've read and create something usable.
8871 We could do this while processing each DIE, and feels kinda cleaner,
8872 but that way is more invasive.
8873 This is to, for example, allow the user to type "p var" or "b main"
8874 without having to specify the package name, and allow lookups
8875 of module.object to work in contexts that use the expression
8876 parser. */
8877
8878 static void
8879 fixup_go_packaging (struct dwarf2_cu *cu)
8880 {
8881 gdb::unique_xmalloc_ptr<char> package_name;
8882 struct pending *list;
8883 int i;
8884
8885 for (list = *cu->get_builder ()->get_global_symbols ();
8886 list != NULL;
8887 list = list->next)
8888 {
8889 for (i = 0; i < list->nsyms; ++i)
8890 {
8891 struct symbol *sym = list->symbol[i];
8892
8893 if (sym->language () == language_go
8894 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8895 {
8896 gdb::unique_xmalloc_ptr<char> this_package_name
8897 (go_symbol_package_name (sym));
8898
8899 if (this_package_name == NULL)
8900 continue;
8901 if (package_name == NULL)
8902 package_name = std::move (this_package_name);
8903 else
8904 {
8905 struct objfile *objfile = cu->per_objfile->objfile;
8906 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8907 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8908 (symbol_symtab (sym) != NULL
8909 ? symtab_to_filename_for_display
8910 (symbol_symtab (sym))
8911 : objfile_name (objfile)),
8912 this_package_name.get (), package_name.get ());
8913 }
8914 }
8915 }
8916 }
8917
8918 if (package_name != NULL)
8919 {
8920 struct objfile *objfile = cu->per_objfile->objfile;
8921 const char *saved_package_name = objfile->intern (package_name.get ());
8922 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8923 saved_package_name);
8924 struct symbol *sym;
8925
8926 sym = new (&objfile->objfile_obstack) symbol;
8927 sym->set_language (language_go, &objfile->objfile_obstack);
8928 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8929 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8930 e.g., "main" finds the "main" module and not C's main(). */
8931 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8932 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8933 SYMBOL_TYPE (sym) = type;
8934
8935 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8936 }
8937 }
8938
8939 /* Allocate a fully-qualified name consisting of the two parts on the
8940 obstack. */
8941
8942 static const char *
8943 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8944 {
8945 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8946 }
8947
8948 /* A helper that allocates a variant part to attach to a Rust enum
8949 type. OBSTACK is where the results should be allocated. TYPE is
8950 the type we're processing. DISCRIMINANT_INDEX is the index of the
8951 discriminant. It must be the index of one of the fields of TYPE,
8952 or -1 to mean there is no discriminant (univariant enum).
8953 DEFAULT_INDEX is the index of the default field; or -1 if there is
8954 no default. RANGES is indexed by "effective" field number (the
8955 field index, but omitting the discriminant and default fields) and
8956 must hold the discriminant values used by the variants. Note that
8957 RANGES must have a lifetime at least as long as OBSTACK -- either
8958 already allocated on it, or static. */
8959
8960 static void
8961 alloc_rust_variant (struct obstack *obstack, struct type *type,
8962 int discriminant_index, int default_index,
8963 gdb::array_view<discriminant_range> ranges)
8964 {
8965 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8966 gdb_assert (discriminant_index == -1
8967 || (discriminant_index >= 0
8968 && discriminant_index < type->num_fields ()));
8969 gdb_assert (default_index == -1
8970 || (default_index >= 0 && default_index < type->num_fields ()));
8971
8972 /* We have one variant for each non-discriminant field. */
8973 int n_variants = type->num_fields ();
8974 if (discriminant_index != -1)
8975 --n_variants;
8976
8977 variant *variants = new (obstack) variant[n_variants];
8978 int var_idx = 0;
8979 int range_idx = 0;
8980 for (int i = 0; i < type->num_fields (); ++i)
8981 {
8982 if (i == discriminant_index)
8983 continue;
8984
8985 variants[var_idx].first_field = i;
8986 variants[var_idx].last_field = i + 1;
8987
8988 /* The default field does not need a range, but other fields do.
8989 We skipped the discriminant above. */
8990 if (i != default_index)
8991 {
8992 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8993 ++range_idx;
8994 }
8995
8996 ++var_idx;
8997 }
8998
8999 gdb_assert (range_idx == ranges.size ());
9000 gdb_assert (var_idx == n_variants);
9001
9002 variant_part *part = new (obstack) variant_part;
9003 part->discriminant_index = discriminant_index;
9004 /* If there is no discriminant, then whether it is signed is of no
9005 consequence. */
9006 part->is_unsigned
9007 = (discriminant_index == -1
9008 ? false
9009 : type->field (discriminant_index).type ()->is_unsigned ());
9010 part->variants = gdb::array_view<variant> (variants, n_variants);
9011
9012 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9013 gdb::array_view<variant_part> *prop_value
9014 = new (storage) gdb::array_view<variant_part> (part, 1);
9015
9016 struct dynamic_prop prop;
9017 prop.set_variant_parts (prop_value);
9018
9019 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9020 }
9021
9022 /* Some versions of rustc emitted enums in an unusual way.
9023
9024 Ordinary enums were emitted as unions. The first element of each
9025 structure in the union was named "RUST$ENUM$DISR". This element
9026 held the discriminant.
9027
9028 These versions of Rust also implemented the "non-zero"
9029 optimization. When the enum had two values, and one is empty and
9030 the other holds a pointer that cannot be zero, the pointer is used
9031 as the discriminant, with a zero value meaning the empty variant.
9032 Here, the union's first member is of the form
9033 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9034 where the fieldnos are the indices of the fields that should be
9035 traversed in order to find the field (which may be several fields deep)
9036 and the variantname is the name of the variant of the case when the
9037 field is zero.
9038
9039 This function recognizes whether TYPE is of one of these forms,
9040 and, if so, smashes it to be a variant type. */
9041
9042 static void
9043 quirk_rust_enum (struct type *type, struct objfile *objfile)
9044 {
9045 gdb_assert (type->code () == TYPE_CODE_UNION);
9046
9047 /* We don't need to deal with empty enums. */
9048 if (type->num_fields () == 0)
9049 return;
9050
9051 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9052 if (type->num_fields () == 1
9053 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
9054 {
9055 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
9056
9057 /* Decode the field name to find the offset of the
9058 discriminant. */
9059 ULONGEST bit_offset = 0;
9060 struct type *field_type = type->field (0).type ();
9061 while (name[0] >= '0' && name[0] <= '9')
9062 {
9063 char *tail;
9064 unsigned long index = strtoul (name, &tail, 10);
9065 name = tail;
9066 if (*name != '$'
9067 || index >= field_type->num_fields ()
9068 || (field_type->field (index).loc_kind ()
9069 != FIELD_LOC_KIND_BITPOS))
9070 {
9071 complaint (_("Could not parse Rust enum encoding string \"%s\""
9072 "[in module %s]"),
9073 type->field (0).name (),
9074 objfile_name (objfile));
9075 return;
9076 }
9077 ++name;
9078
9079 bit_offset += field_type->field (index).loc_bitpos ();
9080 field_type = field_type->field (index).type ();
9081 }
9082
9083 /* Smash this type to be a structure type. We have to do this
9084 because the type has already been recorded. */
9085 type->set_code (TYPE_CODE_STRUCT);
9086 type->set_num_fields (3);
9087 /* Save the field we care about. */
9088 struct field saved_field = type->field (0);
9089 type->set_fields
9090 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9091
9092 /* Put the discriminant at index 0. */
9093 type->field (0).set_type (field_type);
9094 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9095 type->field (0).set_name ("<<discriminant>>");
9096 type->field (0).set_loc_bitpos (bit_offset);
9097
9098 /* The order of fields doesn't really matter, so put the real
9099 field at index 1 and the data-less field at index 2. */
9100 type->field (1) = saved_field;
9101 type->field (1).set_name
9102 (rust_last_path_segment (type->field (1).type ()->name ()));
9103 type->field (1).type ()->set_name
9104 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9105 type->field (1).name ()));
9106
9107 const char *dataless_name
9108 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9109 name);
9110 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9111 dataless_name);
9112 type->field (2).set_type (dataless_type);
9113 /* NAME points into the original discriminant name, which
9114 already has the correct lifetime. */
9115 type->field (2).set_name (name);
9116 type->field (2).set_loc_bitpos (0);
9117
9118 /* Indicate that this is a variant type. */
9119 static discriminant_range ranges[1] = { { 0, 0 } };
9120 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9121 }
9122 /* A union with a single anonymous field is probably an old-style
9123 univariant enum. */
9124 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
9125 {
9126 /* Smash this type to be a structure type. We have to do this
9127 because the type has already been recorded. */
9128 type->set_code (TYPE_CODE_STRUCT);
9129
9130 struct type *field_type = type->field (0).type ();
9131 const char *variant_name
9132 = rust_last_path_segment (field_type->name ());
9133 type->field (0).set_name (variant_name);
9134 field_type->set_name
9135 (rust_fully_qualify (&objfile->objfile_obstack,
9136 type->name (), variant_name));
9137
9138 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9139 }
9140 else
9141 {
9142 struct type *disr_type = nullptr;
9143 for (int i = 0; i < type->num_fields (); ++i)
9144 {
9145 disr_type = type->field (i).type ();
9146
9147 if (disr_type->code () != TYPE_CODE_STRUCT)
9148 {
9149 /* All fields of a true enum will be structs. */
9150 return;
9151 }
9152 else if (disr_type->num_fields () == 0)
9153 {
9154 /* Could be data-less variant, so keep going. */
9155 disr_type = nullptr;
9156 }
9157 else if (strcmp (disr_type->field (0).name (),
9158 "RUST$ENUM$DISR") != 0)
9159 {
9160 /* Not a Rust enum. */
9161 return;
9162 }
9163 else
9164 {
9165 /* Found one. */
9166 break;
9167 }
9168 }
9169
9170 /* If we got here without a discriminant, then it's probably
9171 just a union. */
9172 if (disr_type == nullptr)
9173 return;
9174
9175 /* Smash this type to be a structure type. We have to do this
9176 because the type has already been recorded. */
9177 type->set_code (TYPE_CODE_STRUCT);
9178
9179 /* Make space for the discriminant field. */
9180 struct field *disr_field = &disr_type->field (0);
9181 field *new_fields
9182 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9183 * sizeof (struct field)));
9184 memcpy (new_fields + 1, type->fields (),
9185 type->num_fields () * sizeof (struct field));
9186 type->set_fields (new_fields);
9187 type->set_num_fields (type->num_fields () + 1);
9188
9189 /* Install the discriminant at index 0 in the union. */
9190 type->field (0) = *disr_field;
9191 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9192 type->field (0).set_name ("<<discriminant>>");
9193
9194 /* We need a way to find the correct discriminant given a
9195 variant name. For convenience we build a map here. */
9196 struct type *enum_type = disr_field->type ();
9197 std::unordered_map<std::string, ULONGEST> discriminant_map;
9198 for (int i = 0; i < enum_type->num_fields (); ++i)
9199 {
9200 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
9201 {
9202 const char *name
9203 = rust_last_path_segment (enum_type->field (i).name ());
9204 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9205 }
9206 }
9207
9208 int n_fields = type->num_fields ();
9209 /* We don't need a range entry for the discriminant, but we do
9210 need one for every other field, as there is no default
9211 variant. */
9212 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9213 discriminant_range,
9214 n_fields - 1);
9215 /* Skip the discriminant here. */
9216 for (int i = 1; i < n_fields; ++i)
9217 {
9218 /* Find the final word in the name of this variant's type.
9219 That name can be used to look up the correct
9220 discriminant. */
9221 const char *variant_name
9222 = rust_last_path_segment (type->field (i).type ()->name ());
9223
9224 auto iter = discriminant_map.find (variant_name);
9225 if (iter != discriminant_map.end ())
9226 {
9227 ranges[i - 1].low = iter->second;
9228 ranges[i - 1].high = iter->second;
9229 }
9230
9231 /* In Rust, each element should have the size of the
9232 enclosing enum. */
9233 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9234
9235 /* Remove the discriminant field, if it exists. */
9236 struct type *sub_type = type->field (i).type ();
9237 if (sub_type->num_fields () > 0)
9238 {
9239 sub_type->set_num_fields (sub_type->num_fields () - 1);
9240 sub_type->set_fields (sub_type->fields () + 1);
9241 }
9242 type->field (i).set_name (variant_name);
9243 sub_type->set_name
9244 (rust_fully_qualify (&objfile->objfile_obstack,
9245 type->name (), variant_name));
9246 }
9247
9248 /* Indicate that this is a variant type. */
9249 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9250 gdb::array_view<discriminant_range> (ranges,
9251 n_fields - 1));
9252 }
9253 }
9254
9255 /* Rewrite some Rust unions to be structures with variants parts. */
9256
9257 static void
9258 rust_union_quirks (struct dwarf2_cu *cu)
9259 {
9260 gdb_assert (cu->per_cu->lang == language_rust);
9261 for (type *type_ : cu->rust_unions)
9262 quirk_rust_enum (type_, cu->per_objfile->objfile);
9263 /* We don't need this any more. */
9264 cu->rust_unions.clear ();
9265 }
9266
9267 /* See read.h. */
9268
9269 type_unit_group_unshareable *
9270 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9271 {
9272 auto iter = this->m_type_units.find (tu_group);
9273 if (iter != this->m_type_units.end ())
9274 return iter->second.get ();
9275
9276 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9277 type_unit_group_unshareable *result = uniq.get ();
9278 this->m_type_units[tu_group] = std::move (uniq);
9279 return result;
9280 }
9281
9282 struct type *
9283 dwarf2_per_objfile::get_type_for_signatured_type
9284 (signatured_type *sig_type) const
9285 {
9286 auto iter = this->m_type_map.find (sig_type);
9287 if (iter == this->m_type_map.end ())
9288 return nullptr;
9289
9290 return iter->second;
9291 }
9292
9293 void dwarf2_per_objfile::set_type_for_signatured_type
9294 (signatured_type *sig_type, struct type *type)
9295 {
9296 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9297
9298 this->m_type_map[sig_type] = type;
9299 }
9300
9301 /* A helper function for computing the list of all symbol tables
9302 included by PER_CU. */
9303
9304 static void
9305 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9306 htab_t all_children, htab_t all_type_symtabs,
9307 dwarf2_per_cu_data *per_cu,
9308 dwarf2_per_objfile *per_objfile,
9309 struct compunit_symtab *immediate_parent)
9310 {
9311 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9312 if (*slot != NULL)
9313 {
9314 /* This inclusion and its children have been processed. */
9315 return;
9316 }
9317
9318 *slot = per_cu;
9319
9320 /* Only add a CU if it has a symbol table. */
9321 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9322 if (cust != NULL)
9323 {
9324 /* If this is a type unit only add its symbol table if we haven't
9325 seen it yet (type unit per_cu's can share symtabs). */
9326 if (per_cu->is_debug_types)
9327 {
9328 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9329 if (*slot == NULL)
9330 {
9331 *slot = cust;
9332 result->push_back (cust);
9333 if (cust->user == NULL)
9334 cust->user = immediate_parent;
9335 }
9336 }
9337 else
9338 {
9339 result->push_back (cust);
9340 if (cust->user == NULL)
9341 cust->user = immediate_parent;
9342 }
9343 }
9344
9345 if (!per_cu->imported_symtabs_empty ())
9346 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9347 {
9348 recursively_compute_inclusions (result, all_children,
9349 all_type_symtabs, ptr, per_objfile,
9350 cust);
9351 }
9352 }
9353
9354 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9355 PER_CU. */
9356
9357 static void
9358 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9359 dwarf2_per_objfile *per_objfile)
9360 {
9361 gdb_assert (! per_cu->is_debug_types);
9362
9363 if (!per_cu->imported_symtabs_empty ())
9364 {
9365 int len;
9366 std::vector<compunit_symtab *> result_symtabs;
9367 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9368
9369 /* If we don't have a symtab, we can just skip this case. */
9370 if (cust == NULL)
9371 return;
9372
9373 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9374 htab_eq_pointer,
9375 NULL, xcalloc, xfree));
9376 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9377 htab_eq_pointer,
9378 NULL, xcalloc, xfree));
9379
9380 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9381 {
9382 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9383 all_type_symtabs.get (), ptr,
9384 per_objfile, cust);
9385 }
9386
9387 /* Now we have a transitive closure of all the included symtabs. */
9388 len = result_symtabs.size ();
9389 cust->includes
9390 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9391 struct compunit_symtab *, len + 1);
9392 memcpy (cust->includes, result_symtabs.data (),
9393 len * sizeof (compunit_symtab *));
9394 cust->includes[len] = NULL;
9395 }
9396 }
9397
9398 /* Compute the 'includes' field for the symtabs of all the CUs we just
9399 read. */
9400
9401 static void
9402 process_cu_includes (dwarf2_per_objfile *per_objfile)
9403 {
9404 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9405 {
9406 if (! iter->is_debug_types)
9407 compute_compunit_symtab_includes (iter, per_objfile);
9408 }
9409
9410 per_objfile->per_bfd->just_read_cus.clear ();
9411 }
9412
9413 /* Generate full symbol information for CU, whose DIEs have
9414 already been loaded into memory. */
9415
9416 static void
9417 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9418 {
9419 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9420 struct objfile *objfile = per_objfile->objfile;
9421 struct gdbarch *gdbarch = objfile->arch ();
9422 CORE_ADDR lowpc, highpc;
9423 struct compunit_symtab *cust;
9424 CORE_ADDR baseaddr;
9425 struct block *static_block;
9426 CORE_ADDR addr;
9427
9428 baseaddr = objfile->text_section_offset ();
9429
9430 /* Clear the list here in case something was left over. */
9431 cu->method_list.clear ();
9432
9433 dwarf2_find_base_address (cu->dies, cu);
9434
9435 /* Before we start reading the top-level DIE, ensure it has a valid tag
9436 type. */
9437 switch (cu->dies->tag)
9438 {
9439 case DW_TAG_compile_unit:
9440 case DW_TAG_partial_unit:
9441 case DW_TAG_type_unit:
9442 break;
9443 default:
9444 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9445 dwarf_tag_name (cu->dies->tag),
9446 sect_offset_str (cu->per_cu->sect_off),
9447 objfile_name (per_objfile->objfile));
9448 }
9449
9450 /* Do line number decoding in read_file_scope () */
9451 process_die (cu->dies, cu);
9452
9453 /* For now fudge the Go package. */
9454 if (cu->per_cu->lang == language_go)
9455 fixup_go_packaging (cu);
9456
9457 /* Now that we have processed all the DIEs in the CU, all the types
9458 should be complete, and it should now be safe to compute all of the
9459 physnames. */
9460 compute_delayed_physnames (cu);
9461
9462 if (cu->per_cu->lang == language_rust)
9463 rust_union_quirks (cu);
9464
9465 /* Some compilers don't define a DW_AT_high_pc attribute for the
9466 compilation unit. If the DW_AT_high_pc is missing, synthesize
9467 it, by scanning the DIE's below the compilation unit. */
9468 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9469
9470 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9471 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9472
9473 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9474 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9475 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9476 addrmap to help ensure it has an accurate map of pc values belonging to
9477 this comp unit. */
9478 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9479
9480 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9481 SECT_OFF_TEXT (objfile),
9482 0);
9483
9484 if (cust != NULL)
9485 {
9486 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9487
9488 /* Set symtab language to language from DW_AT_language. If the
9489 compilation is from a C file generated by language preprocessors, do
9490 not set the language if it was already deduced by start_subfile. */
9491 if (!(cu->per_cu->lang == language_c
9492 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9493 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
9494
9495 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9496 produce DW_AT_location with location lists but it can be possibly
9497 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9498 there were bugs in prologue debug info, fixed later in GCC-4.5
9499 by "unwind info for epilogues" patch (which is not directly related).
9500
9501 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9502 needed, it would be wrong due to missing DW_AT_producer there.
9503
9504 Still one can confuse GDB by using non-standard GCC compilation
9505 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9506 */
9507 if (cu->has_loclist && gcc_4_minor >= 5)
9508 cust->locations_valid = 1;
9509
9510 if (gcc_4_minor >= 5)
9511 cust->epilogue_unwind_valid = 1;
9512
9513 cust->set_call_site_htab (cu->call_site_htab);
9514 }
9515
9516 per_objfile->set_symtab (cu->per_cu, cust);
9517
9518 /* Push it for inclusion processing later. */
9519 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9520
9521 /* Not needed any more. */
9522 cu->reset_builder ();
9523 }
9524
9525 /* Generate full symbol information for type unit CU, whose DIEs have
9526 already been loaded into memory. */
9527
9528 static void
9529 process_full_type_unit (dwarf2_cu *cu,
9530 enum language pretend_language)
9531 {
9532 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9533 struct objfile *objfile = per_objfile->objfile;
9534 struct compunit_symtab *cust;
9535 struct signatured_type *sig_type;
9536
9537 gdb_assert (cu->per_cu->is_debug_types);
9538 sig_type = (struct signatured_type *) cu->per_cu;
9539
9540 /* Clear the list here in case something was left over. */
9541 cu->method_list.clear ();
9542
9543 /* The symbol tables are set up in read_type_unit_scope. */
9544 process_die (cu->dies, cu);
9545
9546 /* For now fudge the Go package. */
9547 if (cu->per_cu->lang == language_go)
9548 fixup_go_packaging (cu);
9549
9550 /* Now that we have processed all the DIEs in the CU, all the types
9551 should be complete, and it should now be safe to compute all of the
9552 physnames. */
9553 compute_delayed_physnames (cu);
9554
9555 if (cu->per_cu->lang == language_rust)
9556 rust_union_quirks (cu);
9557
9558 /* TUs share symbol tables.
9559 If this is the first TU to use this symtab, complete the construction
9560 of it with end_expandable_symtab. Otherwise, complete the addition of
9561 this TU's symbols to the existing symtab. */
9562 type_unit_group_unshareable *tug_unshare =
9563 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9564 if (tug_unshare->compunit_symtab == NULL)
9565 {
9566 buildsym_compunit *builder = cu->get_builder ();
9567 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9568 tug_unshare->compunit_symtab = cust;
9569
9570 if (cust != NULL)
9571 {
9572 /* Set symtab language to language from DW_AT_language. If the
9573 compilation is from a C file generated by language preprocessors,
9574 do not set the language if it was already deduced by
9575 start_subfile. */
9576 if (!(cu->per_cu->lang == language_c
9577 && COMPUNIT_FILETABS (cust)->language != language_c))
9578 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
9579 }
9580 }
9581 else
9582 {
9583 cu->get_builder ()->augment_type_symtab ();
9584 cust = tug_unshare->compunit_symtab;
9585 }
9586
9587 per_objfile->set_symtab (cu->per_cu, cust);
9588
9589 /* Not needed any more. */
9590 cu->reset_builder ();
9591 }
9592
9593 /* Process an imported unit DIE. */
9594
9595 static void
9596 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9597 {
9598 struct attribute *attr;
9599
9600 /* For now we don't handle imported units in type units. */
9601 if (cu->per_cu->is_debug_types)
9602 {
9603 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9604 " supported in type units [in module %s]"),
9605 objfile_name (cu->per_objfile->objfile));
9606 }
9607
9608 attr = dwarf2_attr (die, DW_AT_import, cu);
9609 if (attr != NULL)
9610 {
9611 sect_offset sect_off = attr->get_ref_die_offset ();
9612 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9613 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9614 dwarf2_per_cu_data *per_cu
9615 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9616
9617 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9618 into another compilation unit, at root level. Regard this as a hint,
9619 and ignore it. */
9620 if (die->parent && die->parent->parent == NULL
9621 && per_cu->unit_type == DW_UT_compile
9622 && per_cu->lang == language_cplus)
9623 return;
9624
9625 /* If necessary, add it to the queue and load its DIEs. */
9626 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9627 cu->per_cu->lang))
9628 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9629 false, cu->per_cu->lang);
9630
9631 cu->per_cu->imported_symtabs_push (per_cu);
9632 }
9633 }
9634
9635 /* RAII object that represents a process_die scope: i.e.,
9636 starts/finishes processing a DIE. */
9637 class process_die_scope
9638 {
9639 public:
9640 process_die_scope (die_info *die, dwarf2_cu *cu)
9641 : m_die (die), m_cu (cu)
9642 {
9643 /* We should only be processing DIEs not already in process. */
9644 gdb_assert (!m_die->in_process);
9645 m_die->in_process = true;
9646 }
9647
9648 ~process_die_scope ()
9649 {
9650 m_die->in_process = false;
9651
9652 /* If we're done processing the DIE for the CU that owns the line
9653 header, we don't need the line header anymore. */
9654 if (m_cu->line_header_die_owner == m_die)
9655 {
9656 delete m_cu->line_header;
9657 m_cu->line_header = NULL;
9658 m_cu->line_header_die_owner = NULL;
9659 }
9660 }
9661
9662 private:
9663 die_info *m_die;
9664 dwarf2_cu *m_cu;
9665 };
9666
9667 /* Process a die and its children. */
9668
9669 static void
9670 process_die (struct die_info *die, struct dwarf2_cu *cu)
9671 {
9672 process_die_scope scope (die, cu);
9673
9674 switch (die->tag)
9675 {
9676 case DW_TAG_padding:
9677 break;
9678 case DW_TAG_compile_unit:
9679 case DW_TAG_partial_unit:
9680 read_file_scope (die, cu);
9681 break;
9682 case DW_TAG_type_unit:
9683 read_type_unit_scope (die, cu);
9684 break;
9685 case DW_TAG_subprogram:
9686 /* Nested subprograms in Fortran get a prefix. */
9687 if (cu->per_cu->lang == language_fortran
9688 && die->parent != NULL
9689 && die->parent->tag == DW_TAG_subprogram)
9690 cu->processing_has_namespace_info = true;
9691 /* Fall through. */
9692 case DW_TAG_inlined_subroutine:
9693 read_func_scope (die, cu);
9694 break;
9695 case DW_TAG_lexical_block:
9696 case DW_TAG_try_block:
9697 case DW_TAG_catch_block:
9698 read_lexical_block_scope (die, cu);
9699 break;
9700 case DW_TAG_call_site:
9701 case DW_TAG_GNU_call_site:
9702 read_call_site_scope (die, cu);
9703 break;
9704 case DW_TAG_class_type:
9705 case DW_TAG_interface_type:
9706 case DW_TAG_structure_type:
9707 case DW_TAG_union_type:
9708 process_structure_scope (die, cu);
9709 break;
9710 case DW_TAG_enumeration_type:
9711 process_enumeration_scope (die, cu);
9712 break;
9713
9714 /* These dies have a type, but processing them does not create
9715 a symbol or recurse to process the children. Therefore we can
9716 read them on-demand through read_type_die. */
9717 case DW_TAG_subroutine_type:
9718 case DW_TAG_set_type:
9719 case DW_TAG_pointer_type:
9720 case DW_TAG_ptr_to_member_type:
9721 case DW_TAG_reference_type:
9722 case DW_TAG_rvalue_reference_type:
9723 case DW_TAG_string_type:
9724 break;
9725
9726 case DW_TAG_array_type:
9727 /* We only need to handle this case for Ada -- in other
9728 languages, it's normal for the compiler to emit a typedef
9729 instead. */
9730 if (cu->per_cu->lang != language_ada)
9731 break;
9732 /* FALLTHROUGH */
9733 case DW_TAG_base_type:
9734 case DW_TAG_subrange_type:
9735 case DW_TAG_typedef:
9736 /* Add a typedef symbol for the type definition, if it has a
9737 DW_AT_name. */
9738 new_symbol (die, read_type_die (die, cu), cu);
9739 break;
9740 case DW_TAG_common_block:
9741 read_common_block (die, cu);
9742 break;
9743 case DW_TAG_common_inclusion:
9744 break;
9745 case DW_TAG_namespace:
9746 cu->processing_has_namespace_info = true;
9747 read_namespace (die, cu);
9748 break;
9749 case DW_TAG_module:
9750 cu->processing_has_namespace_info = true;
9751 read_module (die, cu);
9752 break;
9753 case DW_TAG_imported_declaration:
9754 cu->processing_has_namespace_info = true;
9755 if (read_namespace_alias (die, cu))
9756 break;
9757 /* The declaration is not a global namespace alias. */
9758 /* Fall through. */
9759 case DW_TAG_imported_module:
9760 cu->processing_has_namespace_info = true;
9761 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9762 || cu->per_cu->lang != language_fortran))
9763 complaint (_("Tag '%s' has unexpected children"),
9764 dwarf_tag_name (die->tag));
9765 read_import_statement (die, cu);
9766 break;
9767
9768 case DW_TAG_imported_unit:
9769 process_imported_unit_die (die, cu);
9770 break;
9771
9772 case DW_TAG_variable:
9773 read_variable (die, cu);
9774 break;
9775
9776 default:
9777 new_symbol (die, NULL, cu);
9778 break;
9779 }
9780 }
9781 \f
9782 /* DWARF name computation. */
9783
9784 /* A helper function for dwarf2_compute_name which determines whether DIE
9785 needs to have the name of the scope prepended to the name listed in the
9786 die. */
9787
9788 static int
9789 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9790 {
9791 struct attribute *attr;
9792
9793 switch (die->tag)
9794 {
9795 case DW_TAG_namespace:
9796 case DW_TAG_typedef:
9797 case DW_TAG_class_type:
9798 case DW_TAG_interface_type:
9799 case DW_TAG_structure_type:
9800 case DW_TAG_union_type:
9801 case DW_TAG_enumeration_type:
9802 case DW_TAG_enumerator:
9803 case DW_TAG_subprogram:
9804 case DW_TAG_inlined_subroutine:
9805 case DW_TAG_member:
9806 case DW_TAG_imported_declaration:
9807 return 1;
9808
9809 case DW_TAG_variable:
9810 case DW_TAG_constant:
9811 /* We only need to prefix "globally" visible variables. These include
9812 any variable marked with DW_AT_external or any variable that
9813 lives in a namespace. [Variables in anonymous namespaces
9814 require prefixing, but they are not DW_AT_external.] */
9815
9816 if (dwarf2_attr (die, DW_AT_specification, cu))
9817 {
9818 struct dwarf2_cu *spec_cu = cu;
9819
9820 return die_needs_namespace (die_specification (die, &spec_cu),
9821 spec_cu);
9822 }
9823
9824 attr = dwarf2_attr (die, DW_AT_external, cu);
9825 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9826 && die->parent->tag != DW_TAG_module)
9827 return 0;
9828 /* A variable in a lexical block of some kind does not need a
9829 namespace, even though in C++ such variables may be external
9830 and have a mangled name. */
9831 if (die->parent->tag == DW_TAG_lexical_block
9832 || die->parent->tag == DW_TAG_try_block
9833 || die->parent->tag == DW_TAG_catch_block
9834 || die->parent->tag == DW_TAG_subprogram)
9835 return 0;
9836 return 1;
9837
9838 default:
9839 return 0;
9840 }
9841 }
9842
9843 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9844 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9845 defined for the given DIE. */
9846
9847 static struct attribute *
9848 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9849 {
9850 struct attribute *attr;
9851
9852 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9853 if (attr == NULL)
9854 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9855
9856 return attr;
9857 }
9858
9859 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9860 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9861 defined for the given DIE. */
9862
9863 static const char *
9864 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9865 {
9866 const char *linkage_name;
9867
9868 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9869 if (linkage_name == NULL)
9870 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9871
9872 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9873 See https://github.com/rust-lang/rust/issues/32925. */
9874 if (cu->per_cu->lang == language_rust && linkage_name != NULL
9875 && strchr (linkage_name, '{') != NULL)
9876 linkage_name = NULL;
9877
9878 return linkage_name;
9879 }
9880
9881 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9882 compute the physname for the object, which include a method's:
9883 - formal parameters (C++),
9884 - receiver type (Go),
9885
9886 The term "physname" is a bit confusing.
9887 For C++, for example, it is the demangled name.
9888 For Go, for example, it's the mangled name.
9889
9890 For Ada, return the DIE's linkage name rather than the fully qualified
9891 name. PHYSNAME is ignored..
9892
9893 The result is allocated on the objfile->per_bfd's obstack and
9894 canonicalized. */
9895
9896 static const char *
9897 dwarf2_compute_name (const char *name,
9898 struct die_info *die, struct dwarf2_cu *cu,
9899 int physname)
9900 {
9901 struct objfile *objfile = cu->per_objfile->objfile;
9902
9903 if (name == NULL)
9904 name = dwarf2_name (die, cu);
9905
9906 enum language lang = cu->per_cu->lang;
9907
9908 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9909 but otherwise compute it by typename_concat inside GDB.
9910 FIXME: Actually this is not really true, or at least not always true.
9911 It's all very confusing. compute_and_set_names doesn't try to demangle
9912 Fortran names because there is no mangling standard. So new_symbol
9913 will set the demangled name to the result of dwarf2_full_name, and it is
9914 the demangled name that GDB uses if it exists. */
9915 if (lang == language_ada
9916 || (lang == language_fortran && physname))
9917 {
9918 /* For Ada unit, we prefer the linkage name over the name, as
9919 the former contains the exported name, which the user expects
9920 to be able to reference. Ideally, we want the user to be able
9921 to reference this entity using either natural or linkage name,
9922 but we haven't started looking at this enhancement yet. */
9923 const char *linkage_name = dw2_linkage_name (die, cu);
9924
9925 if (linkage_name != NULL)
9926 return linkage_name;
9927 }
9928
9929 /* These are the only languages we know how to qualify names in. */
9930 if (name != NULL
9931 && (lang == language_cplus
9932 || lang == language_fortran || lang == language_d
9933 || lang == language_rust))
9934 {
9935 if (die_needs_namespace (die, cu))
9936 {
9937 const char *prefix;
9938 const char *canonical_name = NULL;
9939
9940 string_file buf;
9941
9942 prefix = determine_prefix (die, cu);
9943 if (*prefix != '\0')
9944 {
9945 gdb::unique_xmalloc_ptr<char> prefixed_name
9946 (typename_concat (NULL, prefix, name, physname, cu));
9947
9948 buf.puts (prefixed_name.get ());
9949 }
9950 else
9951 buf.puts (name);
9952
9953 /* Template parameters may be specified in the DIE's DW_AT_name, or
9954 as children with DW_TAG_template_type_param or
9955 DW_TAG_value_type_param. If the latter, add them to the name
9956 here. If the name already has template parameters, then
9957 skip this step; some versions of GCC emit both, and
9958 it is more efficient to use the pre-computed name.
9959
9960 Something to keep in mind about this process: it is very
9961 unlikely, or in some cases downright impossible, to produce
9962 something that will match the mangled name of a function.
9963 If the definition of the function has the same debug info,
9964 we should be able to match up with it anyway. But fallbacks
9965 using the minimal symbol, for instance to find a method
9966 implemented in a stripped copy of libstdc++, will not work.
9967 If we do not have debug info for the definition, we will have to
9968 match them up some other way.
9969
9970 When we do name matching there is a related problem with function
9971 templates; two instantiated function templates are allowed to
9972 differ only by their return types, which we do not add here. */
9973
9974 if (lang == language_cplus && strchr (name, '<') == NULL)
9975 {
9976 struct attribute *attr;
9977 struct die_info *child;
9978 int first = 1;
9979
9980 die->building_fullname = 1;
9981
9982 for (child = die->child; child != NULL; child = child->sibling)
9983 {
9984 struct type *type;
9985 LONGEST value;
9986 const gdb_byte *bytes;
9987 struct dwarf2_locexpr_baton *baton;
9988 struct value *v;
9989
9990 if (child->tag != DW_TAG_template_type_param
9991 && child->tag != DW_TAG_template_value_param)
9992 continue;
9993
9994 if (first)
9995 {
9996 buf.puts ("<");
9997 first = 0;
9998 }
9999 else
10000 buf.puts (", ");
10001
10002 attr = dwarf2_attr (child, DW_AT_type, cu);
10003 if (attr == NULL)
10004 {
10005 complaint (_("template parameter missing DW_AT_type"));
10006 buf.puts ("UNKNOWN_TYPE");
10007 continue;
10008 }
10009 type = die_type (child, cu);
10010
10011 if (child->tag == DW_TAG_template_type_param)
10012 {
10013 cu->language_defn->print_type (type, "", &buf, -1, 0,
10014 &type_print_raw_options);
10015 continue;
10016 }
10017
10018 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10019 if (attr == NULL)
10020 {
10021 complaint (_("template parameter missing "
10022 "DW_AT_const_value"));
10023 buf.puts ("UNKNOWN_VALUE");
10024 continue;
10025 }
10026
10027 dwarf2_const_value_attr (attr, type, name,
10028 &cu->comp_unit_obstack, cu,
10029 &value, &bytes, &baton);
10030
10031 if (type->has_no_signedness ())
10032 /* GDB prints characters as NUMBER 'CHAR'. If that's
10033 changed, this can use value_print instead. */
10034 cu->language_defn->printchar (value, type, &buf);
10035 else
10036 {
10037 struct value_print_options opts;
10038
10039 if (baton != NULL)
10040 v = dwarf2_evaluate_loc_desc (type, NULL,
10041 baton->data,
10042 baton->size,
10043 baton->per_cu,
10044 baton->per_objfile);
10045 else if (bytes != NULL)
10046 {
10047 v = allocate_value (type);
10048 memcpy (value_contents_writeable (v).data (), bytes,
10049 TYPE_LENGTH (type));
10050 }
10051 else
10052 v = value_from_longest (type, value);
10053
10054 /* Specify decimal so that we do not depend on
10055 the radix. */
10056 get_formatted_print_options (&opts, 'd');
10057 opts.raw = 1;
10058 value_print (v, &buf, &opts);
10059 release_value (v);
10060 }
10061 }
10062
10063 die->building_fullname = 0;
10064
10065 if (!first)
10066 {
10067 /* Close the argument list, with a space if necessary
10068 (nested templates). */
10069 if (!buf.empty () && buf.string ().back () == '>')
10070 buf.puts (" >");
10071 else
10072 buf.puts (">");
10073 }
10074 }
10075
10076 /* For C++ methods, append formal parameter type
10077 information, if PHYSNAME. */
10078
10079 if (physname && die->tag == DW_TAG_subprogram
10080 && lang == language_cplus)
10081 {
10082 struct type *type = read_type_die (die, cu);
10083
10084 c_type_print_args (type, &buf, 1, lang,
10085 &type_print_raw_options);
10086
10087 if (lang == language_cplus)
10088 {
10089 /* Assume that an artificial first parameter is
10090 "this", but do not crash if it is not. RealView
10091 marks unnamed (and thus unused) parameters as
10092 artificial; there is no way to differentiate
10093 the two cases. */
10094 if (type->num_fields () > 0
10095 && TYPE_FIELD_ARTIFICIAL (type, 0)
10096 && type->field (0).type ()->code () == TYPE_CODE_PTR
10097 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10098 buf.puts (" const");
10099 }
10100 }
10101
10102 const std::string &intermediate_name = buf.string ();
10103
10104 if (lang == language_cplus)
10105 canonical_name
10106 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10107 objfile);
10108
10109 /* If we only computed INTERMEDIATE_NAME, or if
10110 INTERMEDIATE_NAME is already canonical, then we need to
10111 intern it. */
10112 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10113 name = objfile->intern (intermediate_name);
10114 else
10115 name = canonical_name;
10116 }
10117 }
10118
10119 return name;
10120 }
10121
10122 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10123 If scope qualifiers are appropriate they will be added. The result
10124 will be allocated on the storage_obstack, or NULL if the DIE does
10125 not have a name. NAME may either be from a previous call to
10126 dwarf2_name or NULL.
10127
10128 The output string will be canonicalized (if C++). */
10129
10130 static const char *
10131 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10132 {
10133 return dwarf2_compute_name (name, die, cu, 0);
10134 }
10135
10136 /* Construct a physname for the given DIE in CU. NAME may either be
10137 from a previous call to dwarf2_name or NULL. The result will be
10138 allocated on the objfile_objstack or NULL if the DIE does not have a
10139 name.
10140
10141 The output string will be canonicalized (if C++). */
10142
10143 static const char *
10144 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10145 {
10146 struct objfile *objfile = cu->per_objfile->objfile;
10147 const char *retval, *mangled = NULL, *canon = NULL;
10148 int need_copy = 1;
10149
10150 /* In this case dwarf2_compute_name is just a shortcut not building anything
10151 on its own. */
10152 if (!die_needs_namespace (die, cu))
10153 return dwarf2_compute_name (name, die, cu, 1);
10154
10155 if (cu->per_cu->lang != language_rust)
10156 mangled = dw2_linkage_name (die, cu);
10157
10158 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10159 has computed. */
10160 gdb::unique_xmalloc_ptr<char> demangled;
10161 if (mangled != NULL)
10162 {
10163 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10164 {
10165 /* Do nothing (do not demangle the symbol name). */
10166 }
10167 else
10168 {
10169 /* Use DMGL_RET_DROP for C++ template functions to suppress
10170 their return type. It is easier for GDB users to search
10171 for such functions as `name(params)' than `long name(params)'.
10172 In such case the minimal symbol names do not match the full
10173 symbol names but for template functions there is never a need
10174 to look up their definition from their declaration so
10175 the only disadvantage remains the minimal symbol variant
10176 `long name(params)' does not have the proper inferior type. */
10177 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
10178 | DMGL_RET_DROP));
10179 }
10180 if (demangled)
10181 canon = demangled.get ();
10182 else
10183 {
10184 canon = mangled;
10185 need_copy = 0;
10186 }
10187 }
10188
10189 if (canon == NULL || check_physname)
10190 {
10191 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10192
10193 if (canon != NULL && strcmp (physname, canon) != 0)
10194 {
10195 /* It may not mean a bug in GDB. The compiler could also
10196 compute DW_AT_linkage_name incorrectly. But in such case
10197 GDB would need to be bug-to-bug compatible. */
10198
10199 complaint (_("Computed physname <%s> does not match demangled <%s> "
10200 "(from linkage <%s>) - DIE at %s [in module %s]"),
10201 physname, canon, mangled, sect_offset_str (die->sect_off),
10202 objfile_name (objfile));
10203
10204 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10205 is available here - over computed PHYSNAME. It is safer
10206 against both buggy GDB and buggy compilers. */
10207
10208 retval = canon;
10209 }
10210 else
10211 {
10212 retval = physname;
10213 need_copy = 0;
10214 }
10215 }
10216 else
10217 retval = canon;
10218
10219 if (need_copy)
10220 retval = objfile->intern (retval);
10221
10222 return retval;
10223 }
10224
10225 /* Inspect DIE in CU for a namespace alias. If one exists, record
10226 a new symbol for it.
10227
10228 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10229
10230 static int
10231 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10232 {
10233 struct attribute *attr;
10234
10235 /* If the die does not have a name, this is not a namespace
10236 alias. */
10237 attr = dwarf2_attr (die, DW_AT_name, cu);
10238 if (attr != NULL)
10239 {
10240 int num;
10241 struct die_info *d = die;
10242 struct dwarf2_cu *imported_cu = cu;
10243
10244 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10245 keep inspecting DIEs until we hit the underlying import. */
10246 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10247 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10248 {
10249 attr = dwarf2_attr (d, DW_AT_import, cu);
10250 if (attr == NULL)
10251 break;
10252
10253 d = follow_die_ref (d, attr, &imported_cu);
10254 if (d->tag != DW_TAG_imported_declaration)
10255 break;
10256 }
10257
10258 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10259 {
10260 complaint (_("DIE at %s has too many recursively imported "
10261 "declarations"), sect_offset_str (d->sect_off));
10262 return 0;
10263 }
10264
10265 if (attr != NULL)
10266 {
10267 struct type *type;
10268 sect_offset sect_off = attr->get_ref_die_offset ();
10269
10270 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10271 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10272 {
10273 /* This declaration is a global namespace alias. Add
10274 a symbol for it whose type is the aliased namespace. */
10275 new_symbol (die, type, cu);
10276 return 1;
10277 }
10278 }
10279 }
10280
10281 return 0;
10282 }
10283
10284 /* Return the using directives repository (global or local?) to use in the
10285 current context for CU.
10286
10287 For Ada, imported declarations can materialize renamings, which *may* be
10288 global. However it is impossible (for now?) in DWARF to distinguish
10289 "external" imported declarations and "static" ones. As all imported
10290 declarations seem to be static in all other languages, make them all CU-wide
10291 global only in Ada. */
10292
10293 static struct using_direct **
10294 using_directives (struct dwarf2_cu *cu)
10295 {
10296 if (cu->per_cu->lang == language_ada
10297 && cu->get_builder ()->outermost_context_p ())
10298 return cu->get_builder ()->get_global_using_directives ();
10299 else
10300 return cu->get_builder ()->get_local_using_directives ();
10301 }
10302
10303 /* Read the import statement specified by the given die and record it. */
10304
10305 static void
10306 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10307 {
10308 struct objfile *objfile = cu->per_objfile->objfile;
10309 struct attribute *import_attr;
10310 struct die_info *imported_die, *child_die;
10311 struct dwarf2_cu *imported_cu;
10312 const char *imported_name;
10313 const char *imported_name_prefix;
10314 const char *canonical_name;
10315 const char *import_alias;
10316 const char *imported_declaration = NULL;
10317 const char *import_prefix;
10318 std::vector<const char *> excludes;
10319
10320 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10321 if (import_attr == NULL)
10322 {
10323 complaint (_("Tag '%s' has no DW_AT_import"),
10324 dwarf_tag_name (die->tag));
10325 return;
10326 }
10327
10328 imported_cu = cu;
10329 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10330 imported_name = dwarf2_name (imported_die, imported_cu);
10331 if (imported_name == NULL)
10332 {
10333 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10334
10335 The import in the following code:
10336 namespace A
10337 {
10338 typedef int B;
10339 }
10340
10341 int main ()
10342 {
10343 using A::B;
10344 B b;
10345 return b;
10346 }
10347
10348 ...
10349 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10350 <52> DW_AT_decl_file : 1
10351 <53> DW_AT_decl_line : 6
10352 <54> DW_AT_import : <0x75>
10353 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10354 <59> DW_AT_name : B
10355 <5b> DW_AT_decl_file : 1
10356 <5c> DW_AT_decl_line : 2
10357 <5d> DW_AT_type : <0x6e>
10358 ...
10359 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10360 <76> DW_AT_byte_size : 4
10361 <77> DW_AT_encoding : 5 (signed)
10362
10363 imports the wrong die ( 0x75 instead of 0x58 ).
10364 This case will be ignored until the gcc bug is fixed. */
10365 return;
10366 }
10367
10368 /* Figure out the local name after import. */
10369 import_alias = dwarf2_name (die, cu);
10370
10371 /* Figure out where the statement is being imported to. */
10372 import_prefix = determine_prefix (die, cu);
10373
10374 /* Figure out what the scope of the imported die is and prepend it
10375 to the name of the imported die. */
10376 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10377
10378 if (imported_die->tag != DW_TAG_namespace
10379 && imported_die->tag != DW_TAG_module)
10380 {
10381 imported_declaration = imported_name;
10382 canonical_name = imported_name_prefix;
10383 }
10384 else if (strlen (imported_name_prefix) > 0)
10385 canonical_name = obconcat (&objfile->objfile_obstack,
10386 imported_name_prefix,
10387 (cu->per_cu->lang == language_d
10388 ? "."
10389 : "::"),
10390 imported_name, (char *) NULL);
10391 else
10392 canonical_name = imported_name;
10393
10394 if (die->tag == DW_TAG_imported_module
10395 && cu->per_cu->lang == language_fortran)
10396 for (child_die = die->child; child_die && child_die->tag;
10397 child_die = child_die->sibling)
10398 {
10399 /* DWARF-4: A Fortran use statement with a “rename list” may be
10400 represented by an imported module entry with an import attribute
10401 referring to the module and owned entries corresponding to those
10402 entities that are renamed as part of being imported. */
10403
10404 if (child_die->tag != DW_TAG_imported_declaration)
10405 {
10406 complaint (_("child DW_TAG_imported_declaration expected "
10407 "- DIE at %s [in module %s]"),
10408 sect_offset_str (child_die->sect_off),
10409 objfile_name (objfile));
10410 continue;
10411 }
10412
10413 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10414 if (import_attr == NULL)
10415 {
10416 complaint (_("Tag '%s' has no DW_AT_import"),
10417 dwarf_tag_name (child_die->tag));
10418 continue;
10419 }
10420
10421 imported_cu = cu;
10422 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10423 &imported_cu);
10424 imported_name = dwarf2_name (imported_die, imported_cu);
10425 if (imported_name == NULL)
10426 {
10427 complaint (_("child DW_TAG_imported_declaration has unknown "
10428 "imported name - DIE at %s [in module %s]"),
10429 sect_offset_str (child_die->sect_off),
10430 objfile_name (objfile));
10431 continue;
10432 }
10433
10434 excludes.push_back (imported_name);
10435
10436 process_die (child_die, cu);
10437 }
10438
10439 add_using_directive (using_directives (cu),
10440 import_prefix,
10441 canonical_name,
10442 import_alias,
10443 imported_declaration,
10444 excludes,
10445 0,
10446 &objfile->objfile_obstack);
10447 }
10448
10449 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10450 types, but gives them a size of zero. Starting with version 14,
10451 ICC is compatible with GCC. */
10452
10453 static bool
10454 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10455 {
10456 if (!cu->checked_producer)
10457 check_producer (cu);
10458
10459 return cu->producer_is_icc_lt_14;
10460 }
10461
10462 /* ICC generates a DW_AT_type for C void functions. This was observed on
10463 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10464 which says that void functions should not have a DW_AT_type. */
10465
10466 static bool
10467 producer_is_icc (struct dwarf2_cu *cu)
10468 {
10469 if (!cu->checked_producer)
10470 check_producer (cu);
10471
10472 return cu->producer_is_icc;
10473 }
10474
10475 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10476 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10477 this, it was first present in GCC release 4.3.0. */
10478
10479 static bool
10480 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10481 {
10482 if (!cu->checked_producer)
10483 check_producer (cu);
10484
10485 return cu->producer_is_gcc_lt_4_3;
10486 }
10487
10488 static file_and_directory
10489 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10490 {
10491 file_and_directory res;
10492
10493 /* Find the filename. Do not use dwarf2_name here, since the filename
10494 is not a source language identifier. */
10495 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10496 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10497
10498 if (res.comp_dir == NULL
10499 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10500 && IS_ABSOLUTE_PATH (res.name))
10501 {
10502 std::string comp_dir_storage = ldirname (res.name);
10503 if (!comp_dir_storage.empty ())
10504 res.comp_dir
10505 = cu->per_objfile->objfile->intern (comp_dir_storage.c_str ());
10506 }
10507 if (res.comp_dir != NULL)
10508 {
10509 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10510 directory, get rid of it. */
10511 const char *cp = strchr (res.comp_dir, ':');
10512
10513 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10514 res.comp_dir = cp + 1;
10515 }
10516
10517 if (res.name == NULL)
10518 res.name = "<unknown>";
10519
10520 return res;
10521 }
10522
10523 /* Handle DW_AT_stmt_list for a compilation unit.
10524 DIE is the DW_TAG_compile_unit die for CU.
10525 COMP_DIR is the compilation directory. LOWPC is passed to
10526 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10527
10528 static void
10529 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10530 const file_and_directory &fnd, CORE_ADDR lowpc) /* ARI: editCase function */
10531 {
10532 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10533 struct attribute *attr;
10534 struct line_header line_header_local;
10535 hashval_t line_header_local_hash;
10536 void **slot;
10537 int decode_mapping;
10538
10539 gdb_assert (! cu->per_cu->is_debug_types);
10540
10541 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10542 if (attr == NULL || !attr->form_is_unsigned ())
10543 return;
10544
10545 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10546
10547 /* The line header hash table is only created if needed (it exists to
10548 prevent redundant reading of the line table for partial_units).
10549 If we're given a partial_unit, we'll need it. If we're given a
10550 compile_unit, then use the line header hash table if it's already
10551 created, but don't create one just yet. */
10552
10553 if (per_objfile->line_header_hash == NULL
10554 && die->tag == DW_TAG_partial_unit)
10555 {
10556 per_objfile->line_header_hash
10557 .reset (htab_create_alloc (127, line_header_hash_voidp,
10558 line_header_eq_voidp,
10559 htab_delete_entry<line_header>,
10560 xcalloc, xfree));
10561 }
10562
10563 line_header_local.sect_off = line_offset;
10564 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10565 line_header_local_hash = line_header_hash (&line_header_local);
10566 if (per_objfile->line_header_hash != NULL)
10567 {
10568 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10569 &line_header_local,
10570 line_header_local_hash, NO_INSERT);
10571
10572 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10573 is not present in *SLOT (since if there is something in *SLOT then
10574 it will be for a partial_unit). */
10575 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10576 {
10577 gdb_assert (*slot != NULL);
10578 cu->line_header = (struct line_header *) *slot;
10579 return;
10580 }
10581 }
10582
10583 /* dwarf_decode_line_header does not yet provide sufficient information.
10584 We always have to call also dwarf_decode_lines for it. */
10585 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10586 if (lh == NULL)
10587 return;
10588
10589 cu->line_header = lh.release ();
10590 cu->line_header_die_owner = die;
10591
10592 if (per_objfile->line_header_hash == NULL)
10593 slot = NULL;
10594 else
10595 {
10596 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10597 &line_header_local,
10598 line_header_local_hash, INSERT);
10599 gdb_assert (slot != NULL);
10600 }
10601 if (slot != NULL && *slot == NULL)
10602 {
10603 /* This newly decoded line number information unit will be owned
10604 by line_header_hash hash table. */
10605 *slot = cu->line_header;
10606 cu->line_header_die_owner = NULL;
10607 }
10608 else
10609 {
10610 /* We cannot free any current entry in (*slot) as that struct line_header
10611 may be already used by multiple CUs. Create only temporary decoded
10612 line_header for this CU - it may happen at most once for each line
10613 number information unit. And if we're not using line_header_hash
10614 then this is what we want as well. */
10615 gdb_assert (die->tag != DW_TAG_partial_unit);
10616 }
10617 decode_mapping = (die->tag != DW_TAG_partial_unit);
10618 dwarf_decode_lines (cu->line_header, fnd, cu, nullptr, lowpc,
10619 decode_mapping);
10620
10621 }
10622
10623 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10624
10625 static void
10626 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10627 {
10628 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10629 struct objfile *objfile = per_objfile->objfile;
10630 struct gdbarch *gdbarch = objfile->arch ();
10631 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10632 CORE_ADDR highpc = ((CORE_ADDR) 0);
10633 struct attribute *attr;
10634 struct die_info *child_die;
10635 CORE_ADDR baseaddr;
10636
10637 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
10638 baseaddr = objfile->text_section_offset ();
10639
10640 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10641
10642 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10643 from finish_block. */
10644 if (lowpc == ((CORE_ADDR) -1))
10645 lowpc = highpc;
10646 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10647
10648 file_and_directory fnd = find_file_and_directory (die, cu);
10649
10650 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10651
10652 gdb_assert (per_objfile->sym_cu == nullptr);
10653 scoped_restore restore_sym_cu
10654 = make_scoped_restore (&per_objfile->sym_cu, cu);
10655
10656 /* Decode line number information if present. We do this before
10657 processing child DIEs, so that the line header table is available
10658 for DW_AT_decl_file. */
10659 handle_DW_AT_stmt_list (die, cu, fnd, lowpc);
10660
10661 /* Process all dies in compilation unit. */
10662 if (die->child != NULL)
10663 {
10664 child_die = die->child;
10665 while (child_die && child_die->tag)
10666 {
10667 process_die (child_die, cu);
10668 child_die = child_die->sibling;
10669 }
10670 }
10671 per_objfile->sym_cu = nullptr;
10672
10673 /* Decode macro information, if present. Dwarf 2 macro information
10674 refers to information in the line number info statement program
10675 header, so we can only read it if we've read the header
10676 successfully. */
10677 attr = dwarf2_attr (die, DW_AT_macros, cu);
10678 if (attr == NULL)
10679 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10680 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10681 {
10682 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10683 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10684
10685 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10686 }
10687 else
10688 {
10689 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10690 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10691 {
10692 unsigned int macro_offset = attr->as_unsigned ();
10693
10694 dwarf_decode_macros (cu, macro_offset, 0);
10695 }
10696 }
10697 }
10698
10699 void
10700 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10701 {
10702 struct type_unit_group *tu_group;
10703 int first_time;
10704 struct attribute *attr;
10705 unsigned int i;
10706 struct signatured_type *sig_type;
10707
10708 gdb_assert (per_cu->is_debug_types);
10709 sig_type = (struct signatured_type *) per_cu;
10710
10711 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10712
10713 /* If we're using .gdb_index (includes -readnow) then
10714 per_cu->type_unit_group may not have been set up yet. */
10715 if (sig_type->type_unit_group == NULL)
10716 sig_type->type_unit_group = get_type_unit_group (this, attr);
10717 tu_group = sig_type->type_unit_group;
10718
10719 /* If we've already processed this stmt_list there's no real need to
10720 do it again, we could fake it and just recreate the part we need
10721 (file name,index -> symtab mapping). If data shows this optimization
10722 is useful we can do it then. */
10723 type_unit_group_unshareable *tug_unshare
10724 = per_objfile->get_type_unit_group_unshareable (tu_group);
10725 first_time = tug_unshare->compunit_symtab == NULL;
10726
10727 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10728 debug info. */
10729 line_header_up lh;
10730 if (attr != NULL && attr->form_is_unsigned ())
10731 {
10732 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10733 lh = dwarf_decode_line_header (line_offset, this);
10734 }
10735 if (lh == NULL)
10736 {
10737 if (first_time)
10738 start_symtab ("", NULL, 0);
10739 else
10740 {
10741 gdb_assert (tug_unshare->symtabs == NULL);
10742 gdb_assert (m_builder == nullptr);
10743 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10744 m_builder.reset (new struct buildsym_compunit
10745 (COMPUNIT_OBJFILE (cust), "",
10746 COMPUNIT_DIRNAME (cust),
10747 compunit_language (cust),
10748 0, cust));
10749 list_in_scope = get_builder ()->get_file_symbols ();
10750 }
10751 return;
10752 }
10753
10754 line_header = lh.release ();
10755 line_header_die_owner = die;
10756
10757 if (first_time)
10758 {
10759 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10760
10761 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10762 still initializing it, and our caller (a few levels up)
10763 process_full_type_unit still needs to know if this is the first
10764 time. */
10765
10766 tug_unshare->symtabs
10767 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10768 struct symtab *, line_header->file_names_size ());
10769
10770 auto &file_names = line_header->file_names ();
10771 for (i = 0; i < file_names.size (); ++i)
10772 {
10773 file_entry &fe = file_names[i];
10774 dwarf2_start_subfile (this, fe.name,
10775 fe.include_dir (line_header));
10776 buildsym_compunit *b = get_builder ();
10777 if (b->get_current_subfile ()->symtab == NULL)
10778 {
10779 /* NOTE: start_subfile will recognize when it's been
10780 passed a file it has already seen. So we can't
10781 assume there's a simple mapping from
10782 cu->line_header->file_names to subfiles, plus
10783 cu->line_header->file_names may contain dups. */
10784 b->get_current_subfile ()->symtab
10785 = allocate_symtab (cust, b->get_current_subfile ()->name);
10786 }
10787
10788 fe.symtab = b->get_current_subfile ()->symtab;
10789 tug_unshare->symtabs[i] = fe.symtab;
10790 }
10791 }
10792 else
10793 {
10794 gdb_assert (m_builder == nullptr);
10795 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10796 m_builder.reset (new struct buildsym_compunit
10797 (COMPUNIT_OBJFILE (cust), "",
10798 COMPUNIT_DIRNAME (cust),
10799 compunit_language (cust),
10800 0, cust));
10801 list_in_scope = get_builder ()->get_file_symbols ();
10802
10803 auto &file_names = line_header->file_names ();
10804 for (i = 0; i < file_names.size (); ++i)
10805 {
10806 file_entry &fe = file_names[i];
10807 fe.symtab = tug_unshare->symtabs[i];
10808 }
10809 }
10810
10811 /* The main symtab is allocated last. Type units don't have DW_AT_name
10812 so they don't have a "real" (so to speak) symtab anyway.
10813 There is later code that will assign the main symtab to all symbols
10814 that don't have one. We need to handle the case of a symbol with a
10815 missing symtab (DW_AT_decl_file) anyway. */
10816 }
10817
10818 /* Process DW_TAG_type_unit.
10819 For TUs we want to skip the first top level sibling if it's not the
10820 actual type being defined by this TU. In this case the first top
10821 level sibling is there to provide context only. */
10822
10823 static void
10824 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10825 {
10826 struct die_info *child_die;
10827
10828 prepare_one_comp_unit (cu, die, language_minimal);
10829
10830 /* Initialize (or reinitialize) the machinery for building symtabs.
10831 We do this before processing child DIEs, so that the line header table
10832 is available for DW_AT_decl_file. */
10833 cu->setup_type_unit_groups (die);
10834
10835 if (die->child != NULL)
10836 {
10837 child_die = die->child;
10838 while (child_die && child_die->tag)
10839 {
10840 process_die (child_die, cu);
10841 child_die = child_die->sibling;
10842 }
10843 }
10844 }
10845 \f
10846 /* DWO/DWP files.
10847
10848 http://gcc.gnu.org/wiki/DebugFission
10849 http://gcc.gnu.org/wiki/DebugFissionDWP
10850
10851 To simplify handling of both DWO files ("object" files with the DWARF info)
10852 and DWP files (a file with the DWOs packaged up into one file), we treat
10853 DWP files as having a collection of virtual DWO files. */
10854
10855 static hashval_t
10856 hash_dwo_file (const void *item)
10857 {
10858 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10859 hashval_t hash;
10860
10861 hash = htab_hash_string (dwo_file->dwo_name);
10862 if (dwo_file->comp_dir != NULL)
10863 hash += htab_hash_string (dwo_file->comp_dir);
10864 return hash;
10865 }
10866
10867 static int
10868 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10869 {
10870 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10871 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10872
10873 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10874 return 0;
10875 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10876 return lhs->comp_dir == rhs->comp_dir;
10877 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10878 }
10879
10880 /* Allocate a hash table for DWO files. */
10881
10882 static htab_up
10883 allocate_dwo_file_hash_table ()
10884 {
10885 return htab_up (htab_create_alloc (41,
10886 hash_dwo_file,
10887 eq_dwo_file,
10888 htab_delete_entry<dwo_file>,
10889 xcalloc, xfree));
10890 }
10891
10892 /* Lookup DWO file DWO_NAME. */
10893
10894 static void **
10895 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10896 const char *dwo_name,
10897 const char *comp_dir)
10898 {
10899 struct dwo_file find_entry;
10900 void **slot;
10901
10902 if (per_objfile->per_bfd->dwo_files == NULL)
10903 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10904
10905 find_entry.dwo_name = dwo_name;
10906 find_entry.comp_dir = comp_dir;
10907 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10908 INSERT);
10909
10910 return slot;
10911 }
10912
10913 static hashval_t
10914 hash_dwo_unit (const void *item)
10915 {
10916 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10917
10918 /* This drops the top 32 bits of the id, but is ok for a hash. */
10919 return dwo_unit->signature;
10920 }
10921
10922 static int
10923 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10924 {
10925 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10926 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10927
10928 /* The signature is assumed to be unique within the DWO file.
10929 So while object file CU dwo_id's always have the value zero,
10930 that's OK, assuming each object file DWO file has only one CU,
10931 and that's the rule for now. */
10932 return lhs->signature == rhs->signature;
10933 }
10934
10935 /* Allocate a hash table for DWO CUs,TUs.
10936 There is one of these tables for each of CUs,TUs for each DWO file. */
10937
10938 static htab_up
10939 allocate_dwo_unit_table ()
10940 {
10941 /* Start out with a pretty small number.
10942 Generally DWO files contain only one CU and maybe some TUs. */
10943 return htab_up (htab_create_alloc (3,
10944 hash_dwo_unit,
10945 eq_dwo_unit,
10946 NULL, xcalloc, xfree));
10947 }
10948
10949 /* die_reader_func for create_dwo_cu. */
10950
10951 static void
10952 create_dwo_cu_reader (const struct die_reader_specs *reader,
10953 const gdb_byte *info_ptr,
10954 struct die_info *comp_unit_die,
10955 struct dwo_file *dwo_file,
10956 struct dwo_unit *dwo_unit)
10957 {
10958 struct dwarf2_cu *cu = reader->cu;
10959 sect_offset sect_off = cu->per_cu->sect_off;
10960 struct dwarf2_section_info *section = cu->per_cu->section;
10961
10962 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10963 if (!signature.has_value ())
10964 {
10965 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10966 " its dwo_id [in module %s]"),
10967 sect_offset_str (sect_off), dwo_file->dwo_name);
10968 return;
10969 }
10970
10971 dwo_unit->dwo_file = dwo_file;
10972 dwo_unit->signature = *signature;
10973 dwo_unit->section = section;
10974 dwo_unit->sect_off = sect_off;
10975 dwo_unit->length = cu->per_cu->length;
10976
10977 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10978 sect_offset_str (sect_off),
10979 hex_string (dwo_unit->signature));
10980 }
10981
10982 /* Create the dwo_units for the CUs in a DWO_FILE.
10983 Note: This function processes DWO files only, not DWP files. */
10984
10985 static void
10986 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10987 dwarf2_cu *cu, struct dwo_file &dwo_file,
10988 dwarf2_section_info &section, htab_up &cus_htab)
10989 {
10990 struct objfile *objfile = per_objfile->objfile;
10991 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10992 const gdb_byte *info_ptr, *end_ptr;
10993
10994 section.read (objfile);
10995 info_ptr = section.buffer;
10996
10997 if (info_ptr == NULL)
10998 return;
10999
11000 dwarf_read_debug_printf ("Reading %s for %s:",
11001 section.get_name (),
11002 section.get_file_name ());
11003
11004 end_ptr = info_ptr + section.size;
11005 while (info_ptr < end_ptr)
11006 {
11007 struct dwarf2_per_cu_data per_cu;
11008 struct dwo_unit read_unit {};
11009 struct dwo_unit *dwo_unit;
11010 void **slot;
11011 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11012
11013 per_cu.per_bfd = per_bfd;
11014 per_cu.is_debug_types = 0;
11015 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11016 per_cu.section = &section;
11017
11018 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11019 if (!reader.dummy_p)
11020 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11021 &dwo_file, &read_unit);
11022 info_ptr += per_cu.length;
11023
11024 // If the unit could not be parsed, skip it.
11025 if (read_unit.dwo_file == NULL)
11026 continue;
11027
11028 if (cus_htab == NULL)
11029 cus_htab = allocate_dwo_unit_table ();
11030
11031 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11032 struct dwo_unit);
11033 *dwo_unit = read_unit;
11034 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11035 gdb_assert (slot != NULL);
11036 if (*slot != NULL)
11037 {
11038 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11039 sect_offset dup_sect_off = dup_cu->sect_off;
11040
11041 complaint (_("debug cu entry at offset %s is duplicate to"
11042 " the entry at offset %s, signature %s"),
11043 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11044 hex_string (dwo_unit->signature));
11045 }
11046 *slot = (void *)dwo_unit;
11047 }
11048 }
11049
11050 /* DWP file .debug_{cu,tu}_index section format:
11051 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11052 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11053
11054 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11055 officially standard DWP format was published with DWARF v5 and is called
11056 Version 5. There are no versions 3 or 4.
11057
11058 DWP Version 1:
11059
11060 Both index sections have the same format, and serve to map a 64-bit
11061 signature to a set of section numbers. Each section begins with a header,
11062 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11063 indexes, and a pool of 32-bit section numbers. The index sections will be
11064 aligned at 8-byte boundaries in the file.
11065
11066 The index section header consists of:
11067
11068 V, 32 bit version number
11069 -, 32 bits unused
11070 N, 32 bit number of compilation units or type units in the index
11071 M, 32 bit number of slots in the hash table
11072
11073 Numbers are recorded using the byte order of the application binary.
11074
11075 The hash table begins at offset 16 in the section, and consists of an array
11076 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11077 order of the application binary). Unused slots in the hash table are 0.
11078 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11079
11080 The parallel table begins immediately after the hash table
11081 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11082 array of 32-bit indexes (using the byte order of the application binary),
11083 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11084 table contains a 32-bit index into the pool of section numbers. For unused
11085 hash table slots, the corresponding entry in the parallel table will be 0.
11086
11087 The pool of section numbers begins immediately following the hash table
11088 (at offset 16 + 12 * M from the beginning of the section). The pool of
11089 section numbers consists of an array of 32-bit words (using the byte order
11090 of the application binary). Each item in the array is indexed starting
11091 from 0. The hash table entry provides the index of the first section
11092 number in the set. Additional section numbers in the set follow, and the
11093 set is terminated by a 0 entry (section number 0 is not used in ELF).
11094
11095 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11096 section must be the first entry in the set, and the .debug_abbrev.dwo must
11097 be the second entry. Other members of the set may follow in any order.
11098
11099 ---
11100
11101 DWP Versions 2 and 5:
11102
11103 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11104 and the entries in the index tables are now offsets into these sections.
11105 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11106 section.
11107
11108 Index Section Contents:
11109 Header
11110 Hash Table of Signatures dwp_hash_table.hash_table
11111 Parallel Table of Indices dwp_hash_table.unit_table
11112 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11113 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11114
11115 The index section header consists of:
11116
11117 V, 32 bit version number
11118 L, 32 bit number of columns in the table of section offsets
11119 N, 32 bit number of compilation units or type units in the index
11120 M, 32 bit number of slots in the hash table
11121
11122 Numbers are recorded using the byte order of the application binary.
11123
11124 The hash table has the same format as version 1.
11125 The parallel table of indices has the same format as version 1,
11126 except that the entries are origin-1 indices into the table of sections
11127 offsets and the table of section sizes.
11128
11129 The table of offsets begins immediately following the parallel table
11130 (at offset 16 + 12 * M from the beginning of the section). The table is
11131 a two-dimensional array of 32-bit words (using the byte order of the
11132 application binary), with L columns and N+1 rows, in row-major order.
11133 Each row in the array is indexed starting from 0. The first row provides
11134 a key to the remaining rows: each column in this row provides an identifier
11135 for a debug section, and the offsets in the same column of subsequent rows
11136 refer to that section. The section identifiers for Version 2 are:
11137
11138 DW_SECT_INFO 1 .debug_info.dwo
11139 DW_SECT_TYPES 2 .debug_types.dwo
11140 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11141 DW_SECT_LINE 4 .debug_line.dwo
11142 DW_SECT_LOC 5 .debug_loc.dwo
11143 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11144 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11145 DW_SECT_MACRO 8 .debug_macro.dwo
11146
11147 The section identifiers for Version 5 are:
11148
11149 DW_SECT_INFO_V5 1 .debug_info.dwo
11150 DW_SECT_RESERVED_V5 2 --
11151 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11152 DW_SECT_LINE_V5 4 .debug_line.dwo
11153 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11154 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11155 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11156 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11157
11158 The offsets provided by the CU and TU index sections are the base offsets
11159 for the contributions made by each CU or TU to the corresponding section
11160 in the package file. Each CU and TU header contains an abbrev_offset
11161 field, used to find the abbreviations table for that CU or TU within the
11162 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11163 be interpreted as relative to the base offset given in the index section.
11164 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11165 should be interpreted as relative to the base offset for .debug_line.dwo,
11166 and offsets into other debug sections obtained from DWARF attributes should
11167 also be interpreted as relative to the corresponding base offset.
11168
11169 The table of sizes begins immediately following the table of offsets.
11170 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11171 with L columns and N rows, in row-major order. Each row in the array is
11172 indexed starting from 1 (row 0 is shared by the two tables).
11173
11174 ---
11175
11176 Hash table lookup is handled the same in version 1 and 2:
11177
11178 We assume that N and M will not exceed 2^32 - 1.
11179 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11180
11181 Given a 64-bit compilation unit signature or a type signature S, an entry
11182 in the hash table is located as follows:
11183
11184 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11185 the low-order k bits all set to 1.
11186
11187 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11188
11189 3) If the hash table entry at index H matches the signature, use that
11190 entry. If the hash table entry at index H is unused (all zeroes),
11191 terminate the search: the signature is not present in the table.
11192
11193 4) Let H = (H + H') modulo M. Repeat at Step 3.
11194
11195 Because M > N and H' and M are relatively prime, the search is guaranteed
11196 to stop at an unused slot or find the match. */
11197
11198 /* Create a hash table to map DWO IDs to their CU/TU entry in
11199 .debug_{info,types}.dwo in DWP_FILE.
11200 Returns NULL if there isn't one.
11201 Note: This function processes DWP files only, not DWO files. */
11202
11203 static struct dwp_hash_table *
11204 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11205 struct dwp_file *dwp_file, int is_debug_types)
11206 {
11207 struct objfile *objfile = per_objfile->objfile;
11208 bfd *dbfd = dwp_file->dbfd.get ();
11209 const gdb_byte *index_ptr, *index_end;
11210 struct dwarf2_section_info *index;
11211 uint32_t version, nr_columns, nr_units, nr_slots;
11212 struct dwp_hash_table *htab;
11213
11214 if (is_debug_types)
11215 index = &dwp_file->sections.tu_index;
11216 else
11217 index = &dwp_file->sections.cu_index;
11218
11219 if (index->empty ())
11220 return NULL;
11221 index->read (objfile);
11222
11223 index_ptr = index->buffer;
11224 index_end = index_ptr + index->size;
11225
11226 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11227 For now it's safe to just read 4 bytes (particularly as it's difficult to
11228 tell if you're dealing with Version 5 before you've read the version). */
11229 version = read_4_bytes (dbfd, index_ptr);
11230 index_ptr += 4;
11231 if (version == 2 || version == 5)
11232 nr_columns = read_4_bytes (dbfd, index_ptr);
11233 else
11234 nr_columns = 0;
11235 index_ptr += 4;
11236 nr_units = read_4_bytes (dbfd, index_ptr);
11237 index_ptr += 4;
11238 nr_slots = read_4_bytes (dbfd, index_ptr);
11239 index_ptr += 4;
11240
11241 if (version != 1 && version != 2 && version != 5)
11242 {
11243 error (_("Dwarf Error: unsupported DWP file version (%s)"
11244 " [in module %s]"),
11245 pulongest (version), dwp_file->name);
11246 }
11247 if (nr_slots != (nr_slots & -nr_slots))
11248 {
11249 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11250 " is not power of 2 [in module %s]"),
11251 pulongest (nr_slots), dwp_file->name);
11252 }
11253
11254 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11255 htab->version = version;
11256 htab->nr_columns = nr_columns;
11257 htab->nr_units = nr_units;
11258 htab->nr_slots = nr_slots;
11259 htab->hash_table = index_ptr;
11260 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11261
11262 /* Exit early if the table is empty. */
11263 if (nr_slots == 0 || nr_units == 0
11264 || (version == 2 && nr_columns == 0)
11265 || (version == 5 && nr_columns == 0))
11266 {
11267 /* All must be zero. */
11268 if (nr_slots != 0 || nr_units != 0
11269 || (version == 2 && nr_columns != 0)
11270 || (version == 5 && nr_columns != 0))
11271 {
11272 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11273 " all zero [in modules %s]"),
11274 dwp_file->name);
11275 }
11276 return htab;
11277 }
11278
11279 if (version == 1)
11280 {
11281 htab->section_pool.v1.indices =
11282 htab->unit_table + sizeof (uint32_t) * nr_slots;
11283 /* It's harder to decide whether the section is too small in v1.
11284 V1 is deprecated anyway so we punt. */
11285 }
11286 else if (version == 2)
11287 {
11288 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11289 int *ids = htab->section_pool.v2.section_ids;
11290 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11291 /* Reverse map for error checking. */
11292 int ids_seen[DW_SECT_MAX + 1];
11293 int i;
11294
11295 if (nr_columns < 2)
11296 {
11297 error (_("Dwarf Error: bad DWP hash table, too few columns"
11298 " in section table [in module %s]"),
11299 dwp_file->name);
11300 }
11301 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11302 {
11303 error (_("Dwarf Error: bad DWP hash table, too many columns"
11304 " in section table [in module %s]"),
11305 dwp_file->name);
11306 }
11307 memset (ids, 255, sizeof_ids);
11308 memset (ids_seen, 255, sizeof (ids_seen));
11309 for (i = 0; i < nr_columns; ++i)
11310 {
11311 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11312
11313 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11314 {
11315 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11316 " in section table [in module %s]"),
11317 id, dwp_file->name);
11318 }
11319 if (ids_seen[id] != -1)
11320 {
11321 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11322 " id %d in section table [in module %s]"),
11323 id, dwp_file->name);
11324 }
11325 ids_seen[id] = i;
11326 ids[i] = id;
11327 }
11328 /* Must have exactly one info or types section. */
11329 if (((ids_seen[DW_SECT_INFO] != -1)
11330 + (ids_seen[DW_SECT_TYPES] != -1))
11331 != 1)
11332 {
11333 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11334 " DWO info/types section [in module %s]"),
11335 dwp_file->name);
11336 }
11337 /* Must have an abbrev section. */
11338 if (ids_seen[DW_SECT_ABBREV] == -1)
11339 {
11340 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11341 " section [in module %s]"),
11342 dwp_file->name);
11343 }
11344 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11345 htab->section_pool.v2.sizes =
11346 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11347 * nr_units * nr_columns);
11348 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11349 * nr_units * nr_columns))
11350 > index_end)
11351 {
11352 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11353 " [in module %s]"),
11354 dwp_file->name);
11355 }
11356 }
11357 else /* version == 5 */
11358 {
11359 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11360 int *ids = htab->section_pool.v5.section_ids;
11361 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11362 /* Reverse map for error checking. */
11363 int ids_seen[DW_SECT_MAX_V5 + 1];
11364
11365 if (nr_columns < 2)
11366 {
11367 error (_("Dwarf Error: bad DWP hash table, too few columns"
11368 " in section table [in module %s]"),
11369 dwp_file->name);
11370 }
11371 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11372 {
11373 error (_("Dwarf Error: bad DWP hash table, too many columns"
11374 " in section table [in module %s]"),
11375 dwp_file->name);
11376 }
11377 memset (ids, 255, sizeof_ids);
11378 memset (ids_seen, 255, sizeof (ids_seen));
11379 for (int i = 0; i < nr_columns; ++i)
11380 {
11381 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11382
11383 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11384 {
11385 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11386 " in section table [in module %s]"),
11387 id, dwp_file->name);
11388 }
11389 if (ids_seen[id] != -1)
11390 {
11391 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11392 " id %d in section table [in module %s]"),
11393 id, dwp_file->name);
11394 }
11395 ids_seen[id] = i;
11396 ids[i] = id;
11397 }
11398 /* Must have seen an info section. */
11399 if (ids_seen[DW_SECT_INFO_V5] == -1)
11400 {
11401 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11402 " DWO info/types section [in module %s]"),
11403 dwp_file->name);
11404 }
11405 /* Must have an abbrev section. */
11406 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11407 {
11408 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11409 " section [in module %s]"),
11410 dwp_file->name);
11411 }
11412 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11413 htab->section_pool.v5.sizes
11414 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11415 * nr_units * nr_columns);
11416 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11417 * nr_units * nr_columns))
11418 > index_end)
11419 {
11420 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11421 " [in module %s]"),
11422 dwp_file->name);
11423 }
11424 }
11425
11426 return htab;
11427 }
11428
11429 /* Update SECTIONS with the data from SECTP.
11430
11431 This function is like the other "locate" section routines, but in
11432 this context the sections to read comes from the DWP V1 hash table,
11433 not the full ELF section table.
11434
11435 The result is non-zero for success, or zero if an error was found. */
11436
11437 static int
11438 locate_v1_virtual_dwo_sections (asection *sectp,
11439 struct virtual_v1_dwo_sections *sections)
11440 {
11441 const struct dwop_section_names *names = &dwop_section_names;
11442
11443 if (names->abbrev_dwo.matches (sectp->name))
11444 {
11445 /* There can be only one. */
11446 if (sections->abbrev.s.section != NULL)
11447 return 0;
11448 sections->abbrev.s.section = sectp;
11449 sections->abbrev.size = bfd_section_size (sectp);
11450 }
11451 else if (names->info_dwo.matches (sectp->name)
11452 || names->types_dwo.matches (sectp->name))
11453 {
11454 /* There can be only one. */
11455 if (sections->info_or_types.s.section != NULL)
11456 return 0;
11457 sections->info_or_types.s.section = sectp;
11458 sections->info_or_types.size = bfd_section_size (sectp);
11459 }
11460 else if (names->line_dwo.matches (sectp->name))
11461 {
11462 /* There can be only one. */
11463 if (sections->line.s.section != NULL)
11464 return 0;
11465 sections->line.s.section = sectp;
11466 sections->line.size = bfd_section_size (sectp);
11467 }
11468 else if (names->loc_dwo.matches (sectp->name))
11469 {
11470 /* There can be only one. */
11471 if (sections->loc.s.section != NULL)
11472 return 0;
11473 sections->loc.s.section = sectp;
11474 sections->loc.size = bfd_section_size (sectp);
11475 }
11476 else if (names->macinfo_dwo.matches (sectp->name))
11477 {
11478 /* There can be only one. */
11479 if (sections->macinfo.s.section != NULL)
11480 return 0;
11481 sections->macinfo.s.section = sectp;
11482 sections->macinfo.size = bfd_section_size (sectp);
11483 }
11484 else if (names->macro_dwo.matches (sectp->name))
11485 {
11486 /* There can be only one. */
11487 if (sections->macro.s.section != NULL)
11488 return 0;
11489 sections->macro.s.section = sectp;
11490 sections->macro.size = bfd_section_size (sectp);
11491 }
11492 else if (names->str_offsets_dwo.matches (sectp->name))
11493 {
11494 /* There can be only one. */
11495 if (sections->str_offsets.s.section != NULL)
11496 return 0;
11497 sections->str_offsets.s.section = sectp;
11498 sections->str_offsets.size = bfd_section_size (sectp);
11499 }
11500 else
11501 {
11502 /* No other kind of section is valid. */
11503 return 0;
11504 }
11505
11506 return 1;
11507 }
11508
11509 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11510 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11511 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11512 This is for DWP version 1 files. */
11513
11514 static struct dwo_unit *
11515 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11516 struct dwp_file *dwp_file,
11517 uint32_t unit_index,
11518 const char *comp_dir,
11519 ULONGEST signature, int is_debug_types)
11520 {
11521 const struct dwp_hash_table *dwp_htab =
11522 is_debug_types ? dwp_file->tus : dwp_file->cus;
11523 bfd *dbfd = dwp_file->dbfd.get ();
11524 const char *kind = is_debug_types ? "TU" : "CU";
11525 struct dwo_file *dwo_file;
11526 struct dwo_unit *dwo_unit;
11527 struct virtual_v1_dwo_sections sections;
11528 void **dwo_file_slot;
11529 int i;
11530
11531 gdb_assert (dwp_file->version == 1);
11532
11533 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11534 kind, pulongest (unit_index), hex_string (signature),
11535 dwp_file->name);
11536
11537 /* Fetch the sections of this DWO unit.
11538 Put a limit on the number of sections we look for so that bad data
11539 doesn't cause us to loop forever. */
11540
11541 #define MAX_NR_V1_DWO_SECTIONS \
11542 (1 /* .debug_info or .debug_types */ \
11543 + 1 /* .debug_abbrev */ \
11544 + 1 /* .debug_line */ \
11545 + 1 /* .debug_loc */ \
11546 + 1 /* .debug_str_offsets */ \
11547 + 1 /* .debug_macro or .debug_macinfo */ \
11548 + 1 /* trailing zero */)
11549
11550 memset (&sections, 0, sizeof (sections));
11551
11552 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11553 {
11554 asection *sectp;
11555 uint32_t section_nr =
11556 read_4_bytes (dbfd,
11557 dwp_htab->section_pool.v1.indices
11558 + (unit_index + i) * sizeof (uint32_t));
11559
11560 if (section_nr == 0)
11561 break;
11562 if (section_nr >= dwp_file->num_sections)
11563 {
11564 error (_("Dwarf Error: bad DWP hash table, section number too large"
11565 " [in module %s]"),
11566 dwp_file->name);
11567 }
11568
11569 sectp = dwp_file->elf_sections[section_nr];
11570 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11571 {
11572 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11573 " [in module %s]"),
11574 dwp_file->name);
11575 }
11576 }
11577
11578 if (i < 2
11579 || sections.info_or_types.empty ()
11580 || sections.abbrev.empty ())
11581 {
11582 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11583 " [in module %s]"),
11584 dwp_file->name);
11585 }
11586 if (i == MAX_NR_V1_DWO_SECTIONS)
11587 {
11588 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11589 " [in module %s]"),
11590 dwp_file->name);
11591 }
11592
11593 /* It's easier for the rest of the code if we fake a struct dwo_file and
11594 have dwo_unit "live" in that. At least for now.
11595
11596 The DWP file can be made up of a random collection of CUs and TUs.
11597 However, for each CU + set of TUs that came from the same original DWO
11598 file, we can combine them back into a virtual DWO file to save space
11599 (fewer struct dwo_file objects to allocate). Remember that for really
11600 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11601
11602 std::string virtual_dwo_name =
11603 string_printf ("virtual-dwo/%d-%d-%d-%d",
11604 sections.abbrev.get_id (),
11605 sections.line.get_id (),
11606 sections.loc.get_id (),
11607 sections.str_offsets.get_id ());
11608 /* Can we use an existing virtual DWO file? */
11609 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11610 comp_dir);
11611 /* Create one if necessary. */
11612 if (*dwo_file_slot == NULL)
11613 {
11614 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11615 virtual_dwo_name.c_str ());
11616
11617 dwo_file = new struct dwo_file;
11618 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11619 dwo_file->comp_dir = comp_dir;
11620 dwo_file->sections.abbrev = sections.abbrev;
11621 dwo_file->sections.line = sections.line;
11622 dwo_file->sections.loc = sections.loc;
11623 dwo_file->sections.macinfo = sections.macinfo;
11624 dwo_file->sections.macro = sections.macro;
11625 dwo_file->sections.str_offsets = sections.str_offsets;
11626 /* The "str" section is global to the entire DWP file. */
11627 dwo_file->sections.str = dwp_file->sections.str;
11628 /* The info or types section is assigned below to dwo_unit,
11629 there's no need to record it in dwo_file.
11630 Also, we can't simply record type sections in dwo_file because
11631 we record a pointer into the vector in dwo_unit. As we collect more
11632 types we'll grow the vector and eventually have to reallocate space
11633 for it, invalidating all copies of pointers into the previous
11634 contents. */
11635 *dwo_file_slot = dwo_file;
11636 }
11637 else
11638 {
11639 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11640 virtual_dwo_name.c_str ());
11641
11642 dwo_file = (struct dwo_file *) *dwo_file_slot;
11643 }
11644
11645 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11646 dwo_unit->dwo_file = dwo_file;
11647 dwo_unit->signature = signature;
11648 dwo_unit->section =
11649 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11650 *dwo_unit->section = sections.info_or_types;
11651 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11652
11653 return dwo_unit;
11654 }
11655
11656 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11657 simplify them. Given a pointer to the containing section SECTION, and
11658 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11659 virtual section of just that piece. */
11660
11661 static struct dwarf2_section_info
11662 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11663 struct dwarf2_section_info *section,
11664 bfd_size_type offset, bfd_size_type size)
11665 {
11666 struct dwarf2_section_info result;
11667 asection *sectp;
11668
11669 gdb_assert (section != NULL);
11670 gdb_assert (!section->is_virtual);
11671
11672 memset (&result, 0, sizeof (result));
11673 result.s.containing_section = section;
11674 result.is_virtual = true;
11675
11676 if (size == 0)
11677 return result;
11678
11679 sectp = section->get_bfd_section ();
11680
11681 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11682 bounds of the real section. This is a pretty-rare event, so just
11683 flag an error (easier) instead of a warning and trying to cope. */
11684 if (sectp == NULL
11685 || offset + size > bfd_section_size (sectp))
11686 {
11687 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11688 " in section %s [in module %s]"),
11689 sectp ? bfd_section_name (sectp) : "<unknown>",
11690 objfile_name (per_objfile->objfile));
11691 }
11692
11693 result.virtual_offset = offset;
11694 result.size = size;
11695 return result;
11696 }
11697
11698 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11699 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11700 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11701 This is for DWP version 2 files. */
11702
11703 static struct dwo_unit *
11704 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11705 struct dwp_file *dwp_file,
11706 uint32_t unit_index,
11707 const char *comp_dir,
11708 ULONGEST signature, int is_debug_types)
11709 {
11710 const struct dwp_hash_table *dwp_htab =
11711 is_debug_types ? dwp_file->tus : dwp_file->cus;
11712 bfd *dbfd = dwp_file->dbfd.get ();
11713 const char *kind = is_debug_types ? "TU" : "CU";
11714 struct dwo_file *dwo_file;
11715 struct dwo_unit *dwo_unit;
11716 struct virtual_v2_or_v5_dwo_sections sections;
11717 void **dwo_file_slot;
11718 int i;
11719
11720 gdb_assert (dwp_file->version == 2);
11721
11722 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11723 kind, pulongest (unit_index), hex_string (signature),
11724 dwp_file->name);
11725
11726 /* Fetch the section offsets of this DWO unit. */
11727
11728 memset (&sections, 0, sizeof (sections));
11729
11730 for (i = 0; i < dwp_htab->nr_columns; ++i)
11731 {
11732 uint32_t offset = read_4_bytes (dbfd,
11733 dwp_htab->section_pool.v2.offsets
11734 + (((unit_index - 1) * dwp_htab->nr_columns
11735 + i)
11736 * sizeof (uint32_t)));
11737 uint32_t size = read_4_bytes (dbfd,
11738 dwp_htab->section_pool.v2.sizes
11739 + (((unit_index - 1) * dwp_htab->nr_columns
11740 + i)
11741 * sizeof (uint32_t)));
11742
11743 switch (dwp_htab->section_pool.v2.section_ids[i])
11744 {
11745 case DW_SECT_INFO:
11746 case DW_SECT_TYPES:
11747 sections.info_or_types_offset = offset;
11748 sections.info_or_types_size = size;
11749 break;
11750 case DW_SECT_ABBREV:
11751 sections.abbrev_offset = offset;
11752 sections.abbrev_size = size;
11753 break;
11754 case DW_SECT_LINE:
11755 sections.line_offset = offset;
11756 sections.line_size = size;
11757 break;
11758 case DW_SECT_LOC:
11759 sections.loc_offset = offset;
11760 sections.loc_size = size;
11761 break;
11762 case DW_SECT_STR_OFFSETS:
11763 sections.str_offsets_offset = offset;
11764 sections.str_offsets_size = size;
11765 break;
11766 case DW_SECT_MACINFO:
11767 sections.macinfo_offset = offset;
11768 sections.macinfo_size = size;
11769 break;
11770 case DW_SECT_MACRO:
11771 sections.macro_offset = offset;
11772 sections.macro_size = size;
11773 break;
11774 }
11775 }
11776
11777 /* It's easier for the rest of the code if we fake a struct dwo_file and
11778 have dwo_unit "live" in that. At least for now.
11779
11780 The DWP file can be made up of a random collection of CUs and TUs.
11781 However, for each CU + set of TUs that came from the same original DWO
11782 file, we can combine them back into a virtual DWO file to save space
11783 (fewer struct dwo_file objects to allocate). Remember that for really
11784 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11785
11786 std::string virtual_dwo_name =
11787 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11788 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11789 (long) (sections.line_size ? sections.line_offset : 0),
11790 (long) (sections.loc_size ? sections.loc_offset : 0),
11791 (long) (sections.str_offsets_size
11792 ? sections.str_offsets_offset : 0));
11793 /* Can we use an existing virtual DWO file? */
11794 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11795 comp_dir);
11796 /* Create one if necessary. */
11797 if (*dwo_file_slot == NULL)
11798 {
11799 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11800 virtual_dwo_name.c_str ());
11801
11802 dwo_file = new struct dwo_file;
11803 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11804 dwo_file->comp_dir = comp_dir;
11805 dwo_file->sections.abbrev =
11806 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11807 sections.abbrev_offset,
11808 sections.abbrev_size);
11809 dwo_file->sections.line =
11810 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11811 sections.line_offset,
11812 sections.line_size);
11813 dwo_file->sections.loc =
11814 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11815 sections.loc_offset, sections.loc_size);
11816 dwo_file->sections.macinfo =
11817 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11818 sections.macinfo_offset,
11819 sections.macinfo_size);
11820 dwo_file->sections.macro =
11821 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11822 sections.macro_offset,
11823 sections.macro_size);
11824 dwo_file->sections.str_offsets =
11825 create_dwp_v2_or_v5_section (per_objfile,
11826 &dwp_file->sections.str_offsets,
11827 sections.str_offsets_offset,
11828 sections.str_offsets_size);
11829 /* The "str" section is global to the entire DWP file. */
11830 dwo_file->sections.str = dwp_file->sections.str;
11831 /* The info or types section is assigned below to dwo_unit,
11832 there's no need to record it in dwo_file.
11833 Also, we can't simply record type sections in dwo_file because
11834 we record a pointer into the vector in dwo_unit. As we collect more
11835 types we'll grow the vector and eventually have to reallocate space
11836 for it, invalidating all copies of pointers into the previous
11837 contents. */
11838 *dwo_file_slot = dwo_file;
11839 }
11840 else
11841 {
11842 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11843 virtual_dwo_name.c_str ());
11844
11845 dwo_file = (struct dwo_file *) *dwo_file_slot;
11846 }
11847
11848 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11849 dwo_unit->dwo_file = dwo_file;
11850 dwo_unit->signature = signature;
11851 dwo_unit->section =
11852 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11853 *dwo_unit->section = create_dwp_v2_or_v5_section
11854 (per_objfile,
11855 is_debug_types
11856 ? &dwp_file->sections.types
11857 : &dwp_file->sections.info,
11858 sections.info_or_types_offset,
11859 sections.info_or_types_size);
11860 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11861
11862 return dwo_unit;
11863 }
11864
11865 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11866 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11867 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11868 This is for DWP version 5 files. */
11869
11870 static struct dwo_unit *
11871 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11872 struct dwp_file *dwp_file,
11873 uint32_t unit_index,
11874 const char *comp_dir,
11875 ULONGEST signature, int is_debug_types)
11876 {
11877 const struct dwp_hash_table *dwp_htab
11878 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11879 bfd *dbfd = dwp_file->dbfd.get ();
11880 const char *kind = is_debug_types ? "TU" : "CU";
11881 struct dwo_file *dwo_file;
11882 struct dwo_unit *dwo_unit;
11883 struct virtual_v2_or_v5_dwo_sections sections {};
11884 void **dwo_file_slot;
11885
11886 gdb_assert (dwp_file->version == 5);
11887
11888 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11889 kind, pulongest (unit_index), hex_string (signature),
11890 dwp_file->name);
11891
11892 /* Fetch the section offsets of this DWO unit. */
11893
11894 /* memset (&sections, 0, sizeof (sections)); */
11895
11896 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11897 {
11898 uint32_t offset = read_4_bytes (dbfd,
11899 dwp_htab->section_pool.v5.offsets
11900 + (((unit_index - 1)
11901 * dwp_htab->nr_columns
11902 + i)
11903 * sizeof (uint32_t)));
11904 uint32_t size = read_4_bytes (dbfd,
11905 dwp_htab->section_pool.v5.sizes
11906 + (((unit_index - 1) * dwp_htab->nr_columns
11907 + i)
11908 * sizeof (uint32_t)));
11909
11910 switch (dwp_htab->section_pool.v5.section_ids[i])
11911 {
11912 case DW_SECT_ABBREV_V5:
11913 sections.abbrev_offset = offset;
11914 sections.abbrev_size = size;
11915 break;
11916 case DW_SECT_INFO_V5:
11917 sections.info_or_types_offset = offset;
11918 sections.info_or_types_size = size;
11919 break;
11920 case DW_SECT_LINE_V5:
11921 sections.line_offset = offset;
11922 sections.line_size = size;
11923 break;
11924 case DW_SECT_LOCLISTS_V5:
11925 sections.loclists_offset = offset;
11926 sections.loclists_size = size;
11927 break;
11928 case DW_SECT_MACRO_V5:
11929 sections.macro_offset = offset;
11930 sections.macro_size = size;
11931 break;
11932 case DW_SECT_RNGLISTS_V5:
11933 sections.rnglists_offset = offset;
11934 sections.rnglists_size = size;
11935 break;
11936 case DW_SECT_STR_OFFSETS_V5:
11937 sections.str_offsets_offset = offset;
11938 sections.str_offsets_size = size;
11939 break;
11940 case DW_SECT_RESERVED_V5:
11941 default:
11942 break;
11943 }
11944 }
11945
11946 /* It's easier for the rest of the code if we fake a struct dwo_file and
11947 have dwo_unit "live" in that. At least for now.
11948
11949 The DWP file can be made up of a random collection of CUs and TUs.
11950 However, for each CU + set of TUs that came from the same original DWO
11951 file, we can combine them back into a virtual DWO file to save space
11952 (fewer struct dwo_file objects to allocate). Remember that for really
11953 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11954
11955 std::string virtual_dwo_name =
11956 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11957 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11958 (long) (sections.line_size ? sections.line_offset : 0),
11959 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11960 (long) (sections.str_offsets_size
11961 ? sections.str_offsets_offset : 0),
11962 (long) (sections.macro_size ? sections.macro_offset : 0),
11963 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11964 /* Can we use an existing virtual DWO file? */
11965 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11966 virtual_dwo_name.c_str (),
11967 comp_dir);
11968 /* Create one if necessary. */
11969 if (*dwo_file_slot == NULL)
11970 {
11971 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11972 virtual_dwo_name.c_str ());
11973
11974 dwo_file = new struct dwo_file;
11975 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11976 dwo_file->comp_dir = comp_dir;
11977 dwo_file->sections.abbrev =
11978 create_dwp_v2_or_v5_section (per_objfile,
11979 &dwp_file->sections.abbrev,
11980 sections.abbrev_offset,
11981 sections.abbrev_size);
11982 dwo_file->sections.line =
11983 create_dwp_v2_or_v5_section (per_objfile,
11984 &dwp_file->sections.line,
11985 sections.line_offset, sections.line_size);
11986 dwo_file->sections.macro =
11987 create_dwp_v2_or_v5_section (per_objfile,
11988 &dwp_file->sections.macro,
11989 sections.macro_offset,
11990 sections.macro_size);
11991 dwo_file->sections.loclists =
11992 create_dwp_v2_or_v5_section (per_objfile,
11993 &dwp_file->sections.loclists,
11994 sections.loclists_offset,
11995 sections.loclists_size);
11996 dwo_file->sections.rnglists =
11997 create_dwp_v2_or_v5_section (per_objfile,
11998 &dwp_file->sections.rnglists,
11999 sections.rnglists_offset,
12000 sections.rnglists_size);
12001 dwo_file->sections.str_offsets =
12002 create_dwp_v2_or_v5_section (per_objfile,
12003 &dwp_file->sections.str_offsets,
12004 sections.str_offsets_offset,
12005 sections.str_offsets_size);
12006 /* The "str" section is global to the entire DWP file. */
12007 dwo_file->sections.str = dwp_file->sections.str;
12008 /* The info or types section is assigned below to dwo_unit,
12009 there's no need to record it in dwo_file.
12010 Also, we can't simply record type sections in dwo_file because
12011 we record a pointer into the vector in dwo_unit. As we collect more
12012 types we'll grow the vector and eventually have to reallocate space
12013 for it, invalidating all copies of pointers into the previous
12014 contents. */
12015 *dwo_file_slot = dwo_file;
12016 }
12017 else
12018 {
12019 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12020 virtual_dwo_name.c_str ());
12021
12022 dwo_file = (struct dwo_file *) *dwo_file_slot;
12023 }
12024
12025 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12026 dwo_unit->dwo_file = dwo_file;
12027 dwo_unit->signature = signature;
12028 dwo_unit->section
12029 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12030 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12031 &dwp_file->sections.info,
12032 sections.info_or_types_offset,
12033 sections.info_or_types_size);
12034 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12035
12036 return dwo_unit;
12037 }
12038
12039 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12040 Returns NULL if the signature isn't found. */
12041
12042 static struct dwo_unit *
12043 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12044 struct dwp_file *dwp_file, const char *comp_dir,
12045 ULONGEST signature, int is_debug_types)
12046 {
12047 const struct dwp_hash_table *dwp_htab =
12048 is_debug_types ? dwp_file->tus : dwp_file->cus;
12049 bfd *dbfd = dwp_file->dbfd.get ();
12050 uint32_t mask = dwp_htab->nr_slots - 1;
12051 uint32_t hash = signature & mask;
12052 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12053 unsigned int i;
12054 void **slot;
12055 struct dwo_unit find_dwo_cu;
12056
12057 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12058 find_dwo_cu.signature = signature;
12059 slot = htab_find_slot (is_debug_types
12060 ? dwp_file->loaded_tus.get ()
12061 : dwp_file->loaded_cus.get (),
12062 &find_dwo_cu, INSERT);
12063
12064 if (*slot != NULL)
12065 return (struct dwo_unit *) *slot;
12066
12067 /* Use a for loop so that we don't loop forever on bad debug info. */
12068 for (i = 0; i < dwp_htab->nr_slots; ++i)
12069 {
12070 ULONGEST signature_in_table;
12071
12072 signature_in_table =
12073 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12074 if (signature_in_table == signature)
12075 {
12076 uint32_t unit_index =
12077 read_4_bytes (dbfd,
12078 dwp_htab->unit_table + hash * sizeof (uint32_t));
12079
12080 if (dwp_file->version == 1)
12081 {
12082 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12083 unit_index, comp_dir,
12084 signature, is_debug_types);
12085 }
12086 else if (dwp_file->version == 2)
12087 {
12088 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12089 unit_index, comp_dir,
12090 signature, is_debug_types);
12091 }
12092 else /* version == 5 */
12093 {
12094 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12095 unit_index, comp_dir,
12096 signature, is_debug_types);
12097 }
12098 return (struct dwo_unit *) *slot;
12099 }
12100 if (signature_in_table == 0)
12101 return NULL;
12102 hash = (hash + hash2) & mask;
12103 }
12104
12105 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12106 " [in module %s]"),
12107 dwp_file->name);
12108 }
12109
12110 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12111 Open the file specified by FILE_NAME and hand it off to BFD for
12112 preliminary analysis. Return a newly initialized bfd *, which
12113 includes a canonicalized copy of FILE_NAME.
12114 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12115 SEARCH_CWD is true if the current directory is to be searched.
12116 It will be searched before debug-file-directory.
12117 If successful, the file is added to the bfd include table of the
12118 objfile's bfd (see gdb_bfd_record_inclusion).
12119 If unable to find/open the file, return NULL.
12120 NOTE: This function is derived from symfile_bfd_open. */
12121
12122 static gdb_bfd_ref_ptr
12123 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12124 const char *file_name, int is_dwp, int search_cwd)
12125 {
12126 int desc;
12127 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12128 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12129 to debug_file_directory. */
12130 const char *search_path;
12131 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12132
12133 gdb::unique_xmalloc_ptr<char> search_path_holder;
12134 if (search_cwd)
12135 {
12136 if (!debug_file_directory.empty ())
12137 {
12138 search_path_holder.reset (concat (".", dirname_separator_string,
12139 debug_file_directory.c_str (),
12140 (char *) NULL));
12141 search_path = search_path_holder.get ();
12142 }
12143 else
12144 search_path = ".";
12145 }
12146 else
12147 search_path = debug_file_directory.c_str ();
12148
12149 /* Add the path for the executable binary to the list of search paths. */
12150 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12151 search_path_holder.reset (concat (objfile_dir.c_str (),
12152 dirname_separator_string,
12153 search_path, nullptr));
12154 search_path = search_path_holder.get ();
12155
12156 openp_flags flags = OPF_RETURN_REALPATH;
12157 if (is_dwp)
12158 flags |= OPF_SEARCH_IN_PATH;
12159
12160 gdb::unique_xmalloc_ptr<char> absolute_name;
12161 desc = openp (search_path, flags, file_name,
12162 O_RDONLY | O_BINARY, &absolute_name);
12163 if (desc < 0)
12164 return NULL;
12165
12166 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12167 gnutarget, desc));
12168 if (sym_bfd == NULL)
12169 return NULL;
12170 bfd_set_cacheable (sym_bfd.get (), 1);
12171
12172 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12173 return NULL;
12174
12175 /* Success. Record the bfd as having been included by the objfile's bfd.
12176 This is important because things like demangled_names_hash lives in the
12177 objfile's per_bfd space and may have references to things like symbol
12178 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12179 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12180
12181 return sym_bfd;
12182 }
12183
12184 /* Try to open DWO file FILE_NAME.
12185 COMP_DIR is the DW_AT_comp_dir attribute.
12186 The result is the bfd handle of the file.
12187 If there is a problem finding or opening the file, return NULL.
12188 Upon success, the canonicalized path of the file is stored in the bfd,
12189 same as symfile_bfd_open. */
12190
12191 static gdb_bfd_ref_ptr
12192 open_dwo_file (dwarf2_per_objfile *per_objfile,
12193 const char *file_name, const char *comp_dir)
12194 {
12195 if (IS_ABSOLUTE_PATH (file_name))
12196 return try_open_dwop_file (per_objfile, file_name,
12197 0 /*is_dwp*/, 0 /*search_cwd*/);
12198
12199 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12200
12201 if (comp_dir != NULL)
12202 {
12203 gdb::unique_xmalloc_ptr<char> path_to_try
12204 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12205
12206 /* NOTE: If comp_dir is a relative path, this will also try the
12207 search path, which seems useful. */
12208 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12209 0 /*is_dwp*/,
12210 1 /*search_cwd*/));
12211 if (abfd != NULL)
12212 return abfd;
12213 }
12214
12215 /* That didn't work, try debug-file-directory, which, despite its name,
12216 is a list of paths. */
12217
12218 if (debug_file_directory.empty ())
12219 return NULL;
12220
12221 return try_open_dwop_file (per_objfile, file_name,
12222 0 /*is_dwp*/, 1 /*search_cwd*/);
12223 }
12224
12225 /* This function is mapped across the sections and remembers the offset and
12226 size of each of the DWO debugging sections we are interested in. */
12227
12228 static void
12229 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12230 dwo_sections *dwo_sections)
12231 {
12232 const struct dwop_section_names *names = &dwop_section_names;
12233
12234 if (names->abbrev_dwo.matches (sectp->name))
12235 {
12236 dwo_sections->abbrev.s.section = sectp;
12237 dwo_sections->abbrev.size = bfd_section_size (sectp);
12238 }
12239 else if (names->info_dwo.matches (sectp->name))
12240 {
12241 dwo_sections->info.s.section = sectp;
12242 dwo_sections->info.size = bfd_section_size (sectp);
12243 }
12244 else if (names->line_dwo.matches (sectp->name))
12245 {
12246 dwo_sections->line.s.section = sectp;
12247 dwo_sections->line.size = bfd_section_size (sectp);
12248 }
12249 else if (names->loc_dwo.matches (sectp->name))
12250 {
12251 dwo_sections->loc.s.section = sectp;
12252 dwo_sections->loc.size = bfd_section_size (sectp);
12253 }
12254 else if (names->loclists_dwo.matches (sectp->name))
12255 {
12256 dwo_sections->loclists.s.section = sectp;
12257 dwo_sections->loclists.size = bfd_section_size (sectp);
12258 }
12259 else if (names->macinfo_dwo.matches (sectp->name))
12260 {
12261 dwo_sections->macinfo.s.section = sectp;
12262 dwo_sections->macinfo.size = bfd_section_size (sectp);
12263 }
12264 else if (names->macro_dwo.matches (sectp->name))
12265 {
12266 dwo_sections->macro.s.section = sectp;
12267 dwo_sections->macro.size = bfd_section_size (sectp);
12268 }
12269 else if (names->rnglists_dwo.matches (sectp->name))
12270 {
12271 dwo_sections->rnglists.s.section = sectp;
12272 dwo_sections->rnglists.size = bfd_section_size (sectp);
12273 }
12274 else if (names->str_dwo.matches (sectp->name))
12275 {
12276 dwo_sections->str.s.section = sectp;
12277 dwo_sections->str.size = bfd_section_size (sectp);
12278 }
12279 else if (names->str_offsets_dwo.matches (sectp->name))
12280 {
12281 dwo_sections->str_offsets.s.section = sectp;
12282 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12283 }
12284 else if (names->types_dwo.matches (sectp->name))
12285 {
12286 struct dwarf2_section_info type_section;
12287
12288 memset (&type_section, 0, sizeof (type_section));
12289 type_section.s.section = sectp;
12290 type_section.size = bfd_section_size (sectp);
12291 dwo_sections->types.push_back (type_section);
12292 }
12293 }
12294
12295 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12296 by PER_CU. This is for the non-DWP case.
12297 The result is NULL if DWO_NAME can't be found. */
12298
12299 static struct dwo_file *
12300 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12301 const char *comp_dir)
12302 {
12303 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12304
12305 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12306 if (dbfd == NULL)
12307 {
12308 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12309
12310 return NULL;
12311 }
12312
12313 dwo_file_up dwo_file (new struct dwo_file);
12314 dwo_file->dwo_name = dwo_name;
12315 dwo_file->comp_dir = comp_dir;
12316 dwo_file->dbfd = std::move (dbfd);
12317
12318 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12319 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12320 &dwo_file->sections);
12321
12322 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12323 dwo_file->cus);
12324
12325 if (cu->per_cu->dwarf_version < 5)
12326 {
12327 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12328 dwo_file->sections.types, dwo_file->tus);
12329 }
12330 else
12331 {
12332 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12333 &dwo_file->sections.info, dwo_file->tus,
12334 rcuh_kind::COMPILE);
12335 }
12336
12337 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12338
12339 return dwo_file.release ();
12340 }
12341
12342 /* This function is mapped across the sections and remembers the offset and
12343 size of each of the DWP debugging sections common to version 1 and 2 that
12344 we are interested in. */
12345
12346 static void
12347 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12348 dwp_file *dwp_file)
12349 {
12350 const struct dwop_section_names *names = &dwop_section_names;
12351 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12352
12353 /* Record the ELF section number for later lookup: this is what the
12354 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12355 gdb_assert (elf_section_nr < dwp_file->num_sections);
12356 dwp_file->elf_sections[elf_section_nr] = sectp;
12357
12358 /* Look for specific sections that we need. */
12359 if (names->str_dwo.matches (sectp->name))
12360 {
12361 dwp_file->sections.str.s.section = sectp;
12362 dwp_file->sections.str.size = bfd_section_size (sectp);
12363 }
12364 else if (names->cu_index.matches (sectp->name))
12365 {
12366 dwp_file->sections.cu_index.s.section = sectp;
12367 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12368 }
12369 else if (names->tu_index.matches (sectp->name))
12370 {
12371 dwp_file->sections.tu_index.s.section = sectp;
12372 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12373 }
12374 }
12375
12376 /* This function is mapped across the sections and remembers the offset and
12377 size of each of the DWP version 2 debugging sections that we are interested
12378 in. This is split into a separate function because we don't know if we
12379 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12380
12381 static void
12382 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12383 {
12384 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12385 const struct dwop_section_names *names = &dwop_section_names;
12386 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12387
12388 /* Record the ELF section number for later lookup: this is what the
12389 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12390 gdb_assert (elf_section_nr < dwp_file->num_sections);
12391 dwp_file->elf_sections[elf_section_nr] = sectp;
12392
12393 /* Look for specific sections that we need. */
12394 if (names->abbrev_dwo.matches (sectp->name))
12395 {
12396 dwp_file->sections.abbrev.s.section = sectp;
12397 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12398 }
12399 else if (names->info_dwo.matches (sectp->name))
12400 {
12401 dwp_file->sections.info.s.section = sectp;
12402 dwp_file->sections.info.size = bfd_section_size (sectp);
12403 }
12404 else if (names->line_dwo.matches (sectp->name))
12405 {
12406 dwp_file->sections.line.s.section = sectp;
12407 dwp_file->sections.line.size = bfd_section_size (sectp);
12408 }
12409 else if (names->loc_dwo.matches (sectp->name))
12410 {
12411 dwp_file->sections.loc.s.section = sectp;
12412 dwp_file->sections.loc.size = bfd_section_size (sectp);
12413 }
12414 else if (names->macinfo_dwo.matches (sectp->name))
12415 {
12416 dwp_file->sections.macinfo.s.section = sectp;
12417 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12418 }
12419 else if (names->macro_dwo.matches (sectp->name))
12420 {
12421 dwp_file->sections.macro.s.section = sectp;
12422 dwp_file->sections.macro.size = bfd_section_size (sectp);
12423 }
12424 else if (names->str_offsets_dwo.matches (sectp->name))
12425 {
12426 dwp_file->sections.str_offsets.s.section = sectp;
12427 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12428 }
12429 else if (names->types_dwo.matches (sectp->name))
12430 {
12431 dwp_file->sections.types.s.section = sectp;
12432 dwp_file->sections.types.size = bfd_section_size (sectp);
12433 }
12434 }
12435
12436 /* This function is mapped across the sections and remembers the offset and
12437 size of each of the DWP version 5 debugging sections that we are interested
12438 in. This is split into a separate function because we don't know if we
12439 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12440
12441 static void
12442 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12443 {
12444 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12445 const struct dwop_section_names *names = &dwop_section_names;
12446 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12447
12448 /* Record the ELF section number for later lookup: this is what the
12449 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12450 gdb_assert (elf_section_nr < dwp_file->num_sections);
12451 dwp_file->elf_sections[elf_section_nr] = sectp;
12452
12453 /* Look for specific sections that we need. */
12454 if (names->abbrev_dwo.matches (sectp->name))
12455 {
12456 dwp_file->sections.abbrev.s.section = sectp;
12457 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12458 }
12459 else if (names->info_dwo.matches (sectp->name))
12460 {
12461 dwp_file->sections.info.s.section = sectp;
12462 dwp_file->sections.info.size = bfd_section_size (sectp);
12463 }
12464 else if (names->line_dwo.matches (sectp->name))
12465 {
12466 dwp_file->sections.line.s.section = sectp;
12467 dwp_file->sections.line.size = bfd_section_size (sectp);
12468 }
12469 else if (names->loclists_dwo.matches (sectp->name))
12470 {
12471 dwp_file->sections.loclists.s.section = sectp;
12472 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12473 }
12474 else if (names->macro_dwo.matches (sectp->name))
12475 {
12476 dwp_file->sections.macro.s.section = sectp;
12477 dwp_file->sections.macro.size = bfd_section_size (sectp);
12478 }
12479 else if (names->rnglists_dwo.matches (sectp->name))
12480 {
12481 dwp_file->sections.rnglists.s.section = sectp;
12482 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12483 }
12484 else if (names->str_offsets_dwo.matches (sectp->name))
12485 {
12486 dwp_file->sections.str_offsets.s.section = sectp;
12487 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12488 }
12489 }
12490
12491 /* Hash function for dwp_file loaded CUs/TUs. */
12492
12493 static hashval_t
12494 hash_dwp_loaded_cutus (const void *item)
12495 {
12496 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12497
12498 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12499 return dwo_unit->signature;
12500 }
12501
12502 /* Equality function for dwp_file loaded CUs/TUs. */
12503
12504 static int
12505 eq_dwp_loaded_cutus (const void *a, const void *b)
12506 {
12507 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12508 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12509
12510 return dua->signature == dub->signature;
12511 }
12512
12513 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12514
12515 static htab_up
12516 allocate_dwp_loaded_cutus_table ()
12517 {
12518 return htab_up (htab_create_alloc (3,
12519 hash_dwp_loaded_cutus,
12520 eq_dwp_loaded_cutus,
12521 NULL, xcalloc, xfree));
12522 }
12523
12524 /* Try to open DWP file FILE_NAME.
12525 The result is the bfd handle of the file.
12526 If there is a problem finding or opening the file, return NULL.
12527 Upon success, the canonicalized path of the file is stored in the bfd,
12528 same as symfile_bfd_open. */
12529
12530 static gdb_bfd_ref_ptr
12531 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12532 {
12533 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12534 1 /*is_dwp*/,
12535 1 /*search_cwd*/));
12536 if (abfd != NULL)
12537 return abfd;
12538
12539 /* Work around upstream bug 15652.
12540 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12541 [Whether that's a "bug" is debatable, but it is getting in our way.]
12542 We have no real idea where the dwp file is, because gdb's realpath-ing
12543 of the executable's path may have discarded the needed info.
12544 [IWBN if the dwp file name was recorded in the executable, akin to
12545 .gnu_debuglink, but that doesn't exist yet.]
12546 Strip the directory from FILE_NAME and search again. */
12547 if (!debug_file_directory.empty ())
12548 {
12549 /* Don't implicitly search the current directory here.
12550 If the user wants to search "." to handle this case,
12551 it must be added to debug-file-directory. */
12552 return try_open_dwop_file (per_objfile, lbasename (file_name),
12553 1 /*is_dwp*/,
12554 0 /*search_cwd*/);
12555 }
12556
12557 return NULL;
12558 }
12559
12560 /* Initialize the use of the DWP file for the current objfile.
12561 By convention the name of the DWP file is ${objfile}.dwp.
12562 The result is NULL if it can't be found. */
12563
12564 static std::unique_ptr<struct dwp_file>
12565 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12566 {
12567 struct objfile *objfile = per_objfile->objfile;
12568
12569 /* Try to find first .dwp for the binary file before any symbolic links
12570 resolving. */
12571
12572 /* If the objfile is a debug file, find the name of the real binary
12573 file and get the name of dwp file from there. */
12574 std::string dwp_name;
12575 if (objfile->separate_debug_objfile_backlink != NULL)
12576 {
12577 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12578 const char *backlink_basename = lbasename (backlink->original_name);
12579
12580 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12581 }
12582 else
12583 dwp_name = objfile->original_name;
12584
12585 dwp_name += ".dwp";
12586
12587 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12588 if (dbfd == NULL
12589 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12590 {
12591 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12592 dwp_name = objfile_name (objfile);
12593 dwp_name += ".dwp";
12594 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12595 }
12596
12597 if (dbfd == NULL)
12598 {
12599 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12600
12601 return std::unique_ptr<dwp_file> ();
12602 }
12603
12604 const char *name = bfd_get_filename (dbfd.get ());
12605 std::unique_ptr<struct dwp_file> dwp_file
12606 (new struct dwp_file (name, std::move (dbfd)));
12607
12608 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12609 dwp_file->elf_sections =
12610 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12611 dwp_file->num_sections, asection *);
12612
12613 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12614 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12615 dwp_file.get ());
12616
12617 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12618
12619 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12620
12621 /* The DWP file version is stored in the hash table. Oh well. */
12622 if (dwp_file->cus && dwp_file->tus
12623 && dwp_file->cus->version != dwp_file->tus->version)
12624 {
12625 /* Technically speaking, we should try to limp along, but this is
12626 pretty bizarre. We use pulongest here because that's the established
12627 portability solution (e.g, we cannot use %u for uint32_t). */
12628 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12629 " TU version %s [in DWP file %s]"),
12630 pulongest (dwp_file->cus->version),
12631 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12632 }
12633
12634 if (dwp_file->cus)
12635 dwp_file->version = dwp_file->cus->version;
12636 else if (dwp_file->tus)
12637 dwp_file->version = dwp_file->tus->version;
12638 else
12639 dwp_file->version = 2;
12640
12641 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12642 {
12643 if (dwp_file->version == 2)
12644 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12645 dwp_file.get ());
12646 else
12647 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12648 dwp_file.get ());
12649 }
12650
12651 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12652 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12653
12654 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12655 dwarf_read_debug_printf (" %s CUs, %s TUs",
12656 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12657 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12658
12659 return dwp_file;
12660 }
12661
12662 /* Wrapper around open_and_init_dwp_file, only open it once. */
12663
12664 static struct dwp_file *
12665 get_dwp_file (dwarf2_per_objfile *per_objfile)
12666 {
12667 if (!per_objfile->per_bfd->dwp_checked)
12668 {
12669 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12670 per_objfile->per_bfd->dwp_checked = 1;
12671 }
12672 return per_objfile->per_bfd->dwp_file.get ();
12673 }
12674
12675 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12676 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12677 or in the DWP file for the objfile, referenced by THIS_UNIT.
12678 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12679 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12680
12681 This is called, for example, when wanting to read a variable with a
12682 complex location. Therefore we don't want to do file i/o for every call.
12683 Therefore we don't want to look for a DWO file on every call.
12684 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12685 then we check if we've already seen DWO_NAME, and only THEN do we check
12686 for a DWO file.
12687
12688 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12689 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12690
12691 static struct dwo_unit *
12692 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12693 ULONGEST signature, int is_debug_types)
12694 {
12695 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12696 struct objfile *objfile = per_objfile->objfile;
12697 const char *kind = is_debug_types ? "TU" : "CU";
12698 void **dwo_file_slot;
12699 struct dwo_file *dwo_file;
12700 struct dwp_file *dwp_file;
12701
12702 /* First see if there's a DWP file.
12703 If we have a DWP file but didn't find the DWO inside it, don't
12704 look for the original DWO file. It makes gdb behave differently
12705 depending on whether one is debugging in the build tree. */
12706
12707 dwp_file = get_dwp_file (per_objfile);
12708 if (dwp_file != NULL)
12709 {
12710 const struct dwp_hash_table *dwp_htab =
12711 is_debug_types ? dwp_file->tus : dwp_file->cus;
12712
12713 if (dwp_htab != NULL)
12714 {
12715 struct dwo_unit *dwo_cutu =
12716 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12717 is_debug_types);
12718
12719 if (dwo_cutu != NULL)
12720 {
12721 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12722 kind, hex_string (signature),
12723 host_address_to_string (dwo_cutu));
12724
12725 return dwo_cutu;
12726 }
12727 }
12728 }
12729 else
12730 {
12731 /* No DWP file, look for the DWO file. */
12732
12733 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12734 if (*dwo_file_slot == NULL)
12735 {
12736 /* Read in the file and build a table of the CUs/TUs it contains. */
12737 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12738 }
12739 /* NOTE: This will be NULL if unable to open the file. */
12740 dwo_file = (struct dwo_file *) *dwo_file_slot;
12741
12742 if (dwo_file != NULL)
12743 {
12744 struct dwo_unit *dwo_cutu = NULL;
12745
12746 if (is_debug_types && dwo_file->tus)
12747 {
12748 struct dwo_unit find_dwo_cutu;
12749
12750 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12751 find_dwo_cutu.signature = signature;
12752 dwo_cutu
12753 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12754 &find_dwo_cutu);
12755 }
12756 else if (!is_debug_types && dwo_file->cus)
12757 {
12758 struct dwo_unit find_dwo_cutu;
12759
12760 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12761 find_dwo_cutu.signature = signature;
12762 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12763 &find_dwo_cutu);
12764 }
12765
12766 if (dwo_cutu != NULL)
12767 {
12768 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12769 kind, dwo_name, hex_string (signature),
12770 host_address_to_string (dwo_cutu));
12771
12772 return dwo_cutu;
12773 }
12774 }
12775 }
12776
12777 /* We didn't find it. This could mean a dwo_id mismatch, or
12778 someone deleted the DWO/DWP file, or the search path isn't set up
12779 correctly to find the file. */
12780
12781 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12782 kind, dwo_name, hex_string (signature));
12783
12784 /* This is a warning and not a complaint because it can be caused by
12785 pilot error (e.g., user accidentally deleting the DWO). */
12786 {
12787 /* Print the name of the DWP file if we looked there, helps the user
12788 better diagnose the problem. */
12789 std::string dwp_text;
12790
12791 if (dwp_file != NULL)
12792 dwp_text = string_printf (" [in DWP file %s]",
12793 lbasename (dwp_file->name));
12794
12795 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12796 " [in module %s]"),
12797 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12798 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12799 }
12800 return NULL;
12801 }
12802
12803 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12804 See lookup_dwo_cutu_unit for details. */
12805
12806 static struct dwo_unit *
12807 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12808 ULONGEST signature)
12809 {
12810 gdb_assert (!cu->per_cu->is_debug_types);
12811
12812 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12813 }
12814
12815 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12816 See lookup_dwo_cutu_unit for details. */
12817
12818 static struct dwo_unit *
12819 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12820 {
12821 gdb_assert (cu->per_cu->is_debug_types);
12822
12823 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12824
12825 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12826 }
12827
12828 /* Traversal function for queue_and_load_all_dwo_tus. */
12829
12830 static int
12831 queue_and_load_dwo_tu (void **slot, void *info)
12832 {
12833 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12834 dwarf2_cu *cu = (dwarf2_cu *) info;
12835 ULONGEST signature = dwo_unit->signature;
12836 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12837
12838 if (sig_type != NULL)
12839 {
12840 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12841 a real dependency of PER_CU on SIG_TYPE. That is detected later
12842 while processing PER_CU. */
12843 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12844 cu->per_cu->lang))
12845 load_full_type_unit (sig_type, cu->per_objfile);
12846 cu->per_cu->imported_symtabs_push (sig_type);
12847 }
12848
12849 return 1;
12850 }
12851
12852 /* Queue all TUs contained in the DWO of CU to be read in.
12853 The DWO may have the only definition of the type, though it may not be
12854 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12855 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12856
12857 static void
12858 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12859 {
12860 struct dwo_unit *dwo_unit;
12861 struct dwo_file *dwo_file;
12862
12863 gdb_assert (cu != nullptr);
12864 gdb_assert (!cu->per_cu->is_debug_types);
12865 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12866
12867 dwo_unit = cu->dwo_unit;
12868 gdb_assert (dwo_unit != NULL);
12869
12870 dwo_file = dwo_unit->dwo_file;
12871 if (dwo_file->tus != NULL)
12872 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12873 }
12874
12875 /* Read in various DIEs. */
12876
12877 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12878 Inherit only the children of the DW_AT_abstract_origin DIE not being
12879 already referenced by DW_AT_abstract_origin from the children of the
12880 current DIE. */
12881
12882 static void
12883 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12884 {
12885 struct die_info *child_die;
12886 sect_offset *offsetp;
12887 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12888 struct die_info *origin_die;
12889 /* Iterator of the ORIGIN_DIE children. */
12890 struct die_info *origin_child_die;
12891 struct attribute *attr;
12892 struct dwarf2_cu *origin_cu;
12893 struct pending **origin_previous_list_in_scope;
12894
12895 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12896 if (!attr)
12897 return;
12898
12899 /* Note that following die references may follow to a die in a
12900 different cu. */
12901
12902 origin_cu = cu;
12903 origin_die = follow_die_ref (die, attr, &origin_cu);
12904
12905 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12906 symbols in. */
12907 origin_previous_list_in_scope = origin_cu->list_in_scope;
12908 origin_cu->list_in_scope = cu->list_in_scope;
12909
12910 if (die->tag != origin_die->tag
12911 && !(die->tag == DW_TAG_inlined_subroutine
12912 && origin_die->tag == DW_TAG_subprogram))
12913 complaint (_("DIE %s and its abstract origin %s have different tags"),
12914 sect_offset_str (die->sect_off),
12915 sect_offset_str (origin_die->sect_off));
12916
12917 /* Find if the concrete and abstract trees are structurally the
12918 same. This is a shallow traversal and it is not bullet-proof;
12919 the compiler can trick the debugger into believing that the trees
12920 are isomorphic, whereas they actually are not. However, the
12921 likelyhood of this happening is pretty low, and a full-fledged
12922 check would be an overkill. */
12923 bool are_isomorphic = true;
12924 die_info *concrete_child = die->child;
12925 die_info *abstract_child = origin_die->child;
12926 while (concrete_child != nullptr || abstract_child != nullptr)
12927 {
12928 if (concrete_child == nullptr
12929 || abstract_child == nullptr
12930 || concrete_child->tag != abstract_child->tag)
12931 {
12932 are_isomorphic = false;
12933 break;
12934 }
12935
12936 concrete_child = concrete_child->sibling;
12937 abstract_child = abstract_child->sibling;
12938 }
12939
12940 /* Walk the origin's children in parallel to the concrete children.
12941 This helps match an origin child in case the debug info misses
12942 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12943 origin tree may not have the same tree structure as the concrete
12944 DIE, though. */
12945 die_info *corresponding_abstract_child
12946 = are_isomorphic ? origin_die->child : nullptr;
12947
12948 std::vector<sect_offset> offsets;
12949
12950 for (child_die = die->child;
12951 child_die && child_die->tag;
12952 child_die = child_die->sibling)
12953 {
12954 struct die_info *child_origin_die;
12955 struct dwarf2_cu *child_origin_cu;
12956
12957 /* We are trying to process concrete instance entries:
12958 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12959 it's not relevant to our analysis here. i.e. detecting DIEs that are
12960 present in the abstract instance but not referenced in the concrete
12961 one. */
12962 if (child_die->tag == DW_TAG_call_site
12963 || child_die->tag == DW_TAG_GNU_call_site)
12964 {
12965 if (are_isomorphic)
12966 corresponding_abstract_child
12967 = corresponding_abstract_child->sibling;
12968 continue;
12969 }
12970
12971 /* For each CHILD_DIE, find the corresponding child of
12972 ORIGIN_DIE. If there is more than one layer of
12973 DW_AT_abstract_origin, follow them all; there shouldn't be,
12974 but GCC versions at least through 4.4 generate this (GCC PR
12975 40573). */
12976 child_origin_die = child_die;
12977 child_origin_cu = cu;
12978 while (1)
12979 {
12980 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12981 child_origin_cu);
12982 if (attr == NULL)
12983 break;
12984 child_origin_die = follow_die_ref (child_origin_die, attr,
12985 &child_origin_cu);
12986 }
12987
12988 /* If missing DW_AT_abstract_origin, try the corresponding child
12989 of the origin. Clang emits such lexical scopes. */
12990 if (child_origin_die == child_die
12991 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12992 && are_isomorphic
12993 && child_die->tag == DW_TAG_lexical_block)
12994 child_origin_die = corresponding_abstract_child;
12995
12996 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12997 counterpart may exist. */
12998 if (child_origin_die != child_die)
12999 {
13000 if (child_die->tag != child_origin_die->tag
13001 && !(child_die->tag == DW_TAG_inlined_subroutine
13002 && child_origin_die->tag == DW_TAG_subprogram))
13003 complaint (_("Child DIE %s and its abstract origin %s have "
13004 "different tags"),
13005 sect_offset_str (child_die->sect_off),
13006 sect_offset_str (child_origin_die->sect_off));
13007 if (child_origin_die->parent != origin_die)
13008 complaint (_("Child DIE %s and its abstract origin %s have "
13009 "different parents"),
13010 sect_offset_str (child_die->sect_off),
13011 sect_offset_str (child_origin_die->sect_off));
13012 else
13013 offsets.push_back (child_origin_die->sect_off);
13014 }
13015
13016 if (are_isomorphic)
13017 corresponding_abstract_child = corresponding_abstract_child->sibling;
13018 }
13019 std::sort (offsets.begin (), offsets.end ());
13020 sect_offset *offsets_end = offsets.data () + offsets.size ();
13021 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13022 if (offsetp[-1] == *offsetp)
13023 complaint (_("Multiple children of DIE %s refer "
13024 "to DIE %s as their abstract origin"),
13025 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13026
13027 offsetp = offsets.data ();
13028 origin_child_die = origin_die->child;
13029 while (origin_child_die && origin_child_die->tag)
13030 {
13031 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13032 while (offsetp < offsets_end
13033 && *offsetp < origin_child_die->sect_off)
13034 offsetp++;
13035 if (offsetp >= offsets_end
13036 || *offsetp > origin_child_die->sect_off)
13037 {
13038 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13039 Check whether we're already processing ORIGIN_CHILD_DIE.
13040 This can happen with mutually referenced abstract_origins.
13041 PR 16581. */
13042 if (!origin_child_die->in_process)
13043 process_die (origin_child_die, origin_cu);
13044 }
13045 origin_child_die = origin_child_die->sibling;
13046 }
13047 origin_cu->list_in_scope = origin_previous_list_in_scope;
13048
13049 if (cu != origin_cu)
13050 compute_delayed_physnames (origin_cu);
13051 }
13052
13053 static void
13054 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13055 {
13056 struct objfile *objfile = cu->per_objfile->objfile;
13057 struct gdbarch *gdbarch = objfile->arch ();
13058 struct context_stack *newobj;
13059 CORE_ADDR lowpc;
13060 CORE_ADDR highpc;
13061 struct die_info *child_die;
13062 struct attribute *attr, *call_line, *call_file;
13063 const char *name;
13064 CORE_ADDR baseaddr;
13065 struct block *block;
13066 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13067 std::vector<struct symbol *> template_args;
13068 struct template_symbol *templ_func = NULL;
13069
13070 if (inlined_func)
13071 {
13072 /* If we do not have call site information, we can't show the
13073 caller of this inlined function. That's too confusing, so
13074 only use the scope for local variables. */
13075 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13076 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13077 if (call_line == NULL || call_file == NULL)
13078 {
13079 read_lexical_block_scope (die, cu);
13080 return;
13081 }
13082 }
13083
13084 baseaddr = objfile->text_section_offset ();
13085
13086 name = dwarf2_name (die, cu);
13087
13088 /* Ignore functions with missing or empty names. These are actually
13089 illegal according to the DWARF standard. */
13090 if (name == NULL)
13091 {
13092 complaint (_("missing name for subprogram DIE at %s"),
13093 sect_offset_str (die->sect_off));
13094 return;
13095 }
13096
13097 /* Ignore functions with missing or invalid low and high pc attributes. */
13098 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13099 <= PC_BOUNDS_INVALID)
13100 {
13101 attr = dwarf2_attr (die, DW_AT_external, cu);
13102 if (attr == nullptr || !attr->as_boolean ())
13103 complaint (_("cannot get low and high bounds "
13104 "for subprogram DIE at %s"),
13105 sect_offset_str (die->sect_off));
13106 return;
13107 }
13108
13109 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13110 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13111
13112 /* If we have any template arguments, then we must allocate a
13113 different sort of symbol. */
13114 for (child_die = die->child; child_die; child_die = child_die->sibling)
13115 {
13116 if (child_die->tag == DW_TAG_template_type_param
13117 || child_die->tag == DW_TAG_template_value_param)
13118 {
13119 templ_func = new (&objfile->objfile_obstack) template_symbol;
13120 templ_func->subclass = SYMBOL_TEMPLATE;
13121 break;
13122 }
13123 }
13124
13125 gdb_assert (cu->get_builder () != nullptr);
13126 newobj = cu->get_builder ()->push_context (0, lowpc);
13127 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13128 (struct symbol *) templ_func);
13129
13130 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13131 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13132 cu->per_cu->lang);
13133
13134 /* If there is a location expression for DW_AT_frame_base, record
13135 it. */
13136 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13137 if (attr != nullptr)
13138 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13139
13140 /* If there is a location for the static link, record it. */
13141 newobj->static_link = NULL;
13142 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13143 if (attr != nullptr)
13144 {
13145 newobj->static_link
13146 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13147 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13148 cu->addr_type ());
13149 }
13150
13151 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13152
13153 if (die->child != NULL)
13154 {
13155 child_die = die->child;
13156 while (child_die && child_die->tag)
13157 {
13158 if (child_die->tag == DW_TAG_template_type_param
13159 || child_die->tag == DW_TAG_template_value_param)
13160 {
13161 struct symbol *arg = new_symbol (child_die, NULL, cu);
13162
13163 if (arg != NULL)
13164 template_args.push_back (arg);
13165 }
13166 else
13167 process_die (child_die, cu);
13168 child_die = child_die->sibling;
13169 }
13170 }
13171
13172 inherit_abstract_dies (die, cu);
13173
13174 /* If we have a DW_AT_specification, we might need to import using
13175 directives from the context of the specification DIE. See the
13176 comment in determine_prefix. */
13177 if (cu->per_cu->lang == language_cplus
13178 && dwarf2_attr (die, DW_AT_specification, cu))
13179 {
13180 struct dwarf2_cu *spec_cu = cu;
13181 struct die_info *spec_die = die_specification (die, &spec_cu);
13182
13183 while (spec_die)
13184 {
13185 child_die = spec_die->child;
13186 while (child_die && child_die->tag)
13187 {
13188 if (child_die->tag == DW_TAG_imported_module)
13189 process_die (child_die, spec_cu);
13190 child_die = child_die->sibling;
13191 }
13192
13193 /* In some cases, GCC generates specification DIEs that
13194 themselves contain DW_AT_specification attributes. */
13195 spec_die = die_specification (spec_die, &spec_cu);
13196 }
13197 }
13198
13199 struct context_stack cstk = cu->get_builder ()->pop_context ();
13200 /* Make a block for the local symbols within. */
13201 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13202 cstk.static_link, lowpc, highpc);
13203
13204 /* For C++, set the block's scope. */
13205 if ((cu->per_cu->lang == language_cplus
13206 || cu->per_cu->lang == language_fortran
13207 || cu->per_cu->lang == language_d
13208 || cu->per_cu->lang == language_rust)
13209 && cu->processing_has_namespace_info)
13210 block_set_scope (block, determine_prefix (die, cu),
13211 &objfile->objfile_obstack);
13212
13213 /* If we have address ranges, record them. */
13214 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13215
13216 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13217
13218 /* Attach template arguments to function. */
13219 if (!template_args.empty ())
13220 {
13221 gdb_assert (templ_func != NULL);
13222
13223 templ_func->n_template_arguments = template_args.size ();
13224 templ_func->template_arguments
13225 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13226 templ_func->n_template_arguments);
13227 memcpy (templ_func->template_arguments,
13228 template_args.data (),
13229 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13230
13231 /* Make sure that the symtab is set on the new symbols. Even
13232 though they don't appear in this symtab directly, other parts
13233 of gdb assume that symbols do, and this is reasonably
13234 true. */
13235 for (symbol *sym : template_args)
13236 symbol_set_symtab (sym, symbol_symtab (templ_func));
13237 }
13238
13239 /* In C++, we can have functions nested inside functions (e.g., when
13240 a function declares a class that has methods). This means that
13241 when we finish processing a function scope, we may need to go
13242 back to building a containing block's symbol lists. */
13243 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13244 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13245
13246 /* If we've finished processing a top-level function, subsequent
13247 symbols go in the file symbol list. */
13248 if (cu->get_builder ()->outermost_context_p ())
13249 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13250 }
13251
13252 /* Process all the DIES contained within a lexical block scope. Start
13253 a new scope, process the dies, and then close the scope. */
13254
13255 static void
13256 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13257 {
13258 struct objfile *objfile = cu->per_objfile->objfile;
13259 struct gdbarch *gdbarch = objfile->arch ();
13260 CORE_ADDR lowpc, highpc;
13261 struct die_info *child_die;
13262 CORE_ADDR baseaddr;
13263
13264 baseaddr = objfile->text_section_offset ();
13265
13266 /* Ignore blocks with missing or invalid low and high pc attributes. */
13267 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13268 as multiple lexical blocks? Handling children in a sane way would
13269 be nasty. Might be easier to properly extend generic blocks to
13270 describe ranges. */
13271 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13272 {
13273 case PC_BOUNDS_NOT_PRESENT:
13274 /* DW_TAG_lexical_block has no attributes, process its children as if
13275 there was no wrapping by that DW_TAG_lexical_block.
13276 GCC does no longer produces such DWARF since GCC r224161. */
13277 for (child_die = die->child;
13278 child_die != NULL && child_die->tag;
13279 child_die = child_die->sibling)
13280 {
13281 /* We might already be processing this DIE. This can happen
13282 in an unusual circumstance -- where a subroutine A
13283 appears lexically in another subroutine B, but A actually
13284 inlines B. The recursion is broken here, rather than in
13285 inherit_abstract_dies, because it seems better to simply
13286 drop concrete children here. */
13287 if (!child_die->in_process)
13288 process_die (child_die, cu);
13289 }
13290 return;
13291 case PC_BOUNDS_INVALID:
13292 return;
13293 }
13294 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13295 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13296
13297 cu->get_builder ()->push_context (0, lowpc);
13298 if (die->child != NULL)
13299 {
13300 child_die = die->child;
13301 while (child_die && child_die->tag)
13302 {
13303 process_die (child_die, cu);
13304 child_die = child_die->sibling;
13305 }
13306 }
13307 inherit_abstract_dies (die, cu);
13308 struct context_stack cstk = cu->get_builder ()->pop_context ();
13309
13310 if (*cu->get_builder ()->get_local_symbols () != NULL
13311 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13312 {
13313 struct block *block
13314 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13315 cstk.start_addr, highpc);
13316
13317 /* Note that recording ranges after traversing children, as we
13318 do here, means that recording a parent's ranges entails
13319 walking across all its children's ranges as they appear in
13320 the address map, which is quadratic behavior.
13321
13322 It would be nicer to record the parent's ranges before
13323 traversing its children, simply overriding whatever you find
13324 there. But since we don't even decide whether to create a
13325 block until after we've traversed its children, that's hard
13326 to do. */
13327 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13328 }
13329 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13330 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13331 }
13332
13333 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13334
13335 static void
13336 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13337 {
13338 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13339 struct objfile *objfile = per_objfile->objfile;
13340 struct gdbarch *gdbarch = objfile->arch ();
13341 CORE_ADDR pc, baseaddr;
13342 struct attribute *attr;
13343 void **slot;
13344 int nparams;
13345 struct die_info *child_die;
13346
13347 baseaddr = objfile->text_section_offset ();
13348
13349 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13350 if (attr == NULL)
13351 {
13352 /* This was a pre-DWARF-5 GNU extension alias
13353 for DW_AT_call_return_pc. */
13354 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13355 }
13356 if (!attr)
13357 {
13358 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13359 "DIE %s [in module %s]"),
13360 sect_offset_str (die->sect_off), objfile_name (objfile));
13361 return;
13362 }
13363 pc = attr->as_address () + baseaddr;
13364 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13365 pc -= baseaddr;
13366
13367 if (cu->call_site_htab == NULL)
13368 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
13369 call_site::eq, NULL,
13370 &objfile->objfile_obstack,
13371 hashtab_obstack_allocate, NULL);
13372 struct call_site call_site_local (pc, nullptr, nullptr);
13373 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13374 if (*slot != NULL)
13375 {
13376 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13377 "DIE %s [in module %s]"),
13378 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13379 objfile_name (objfile));
13380 return;
13381 }
13382
13383 /* Count parameters at the caller. */
13384
13385 nparams = 0;
13386 for (child_die = die->child; child_die && child_die->tag;
13387 child_die = child_die->sibling)
13388 {
13389 if (child_die->tag != DW_TAG_call_site_parameter
13390 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13391 {
13392 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13393 "DW_TAG_call_site child DIE %s [in module %s]"),
13394 child_die->tag, sect_offset_str (child_die->sect_off),
13395 objfile_name (objfile));
13396 continue;
13397 }
13398
13399 nparams++;
13400 }
13401
13402 struct call_site *call_site
13403 = new (XOBNEWVAR (&objfile->objfile_obstack,
13404 struct call_site,
13405 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
13406 struct call_site (pc, cu->per_cu, per_objfile);
13407 *slot = call_site;
13408
13409 /* We never call the destructor of call_site, so we must ensure it is
13410 trivially destructible. */
13411 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
13412
13413 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13414 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13415 {
13416 struct die_info *func_die;
13417
13418 /* Skip also over DW_TAG_inlined_subroutine. */
13419 for (func_die = die->parent;
13420 func_die && func_die->tag != DW_TAG_subprogram
13421 && func_die->tag != DW_TAG_subroutine_type;
13422 func_die = func_die->parent);
13423
13424 /* DW_AT_call_all_calls is a superset
13425 of DW_AT_call_all_tail_calls. */
13426 if (func_die
13427 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13428 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13429 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13430 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13431 {
13432 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13433 not complete. But keep CALL_SITE for look ups via call_site_htab,
13434 both the initial caller containing the real return address PC and
13435 the final callee containing the current PC of a chain of tail
13436 calls do not need to have the tail call list complete. But any
13437 function candidate for a virtual tail call frame searched via
13438 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13439 determined unambiguously. */
13440 }
13441 else
13442 {
13443 struct type *func_type = NULL;
13444
13445 if (func_die)
13446 func_type = get_die_type (func_die, cu);
13447 if (func_type != NULL)
13448 {
13449 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13450
13451 /* Enlist this call site to the function. */
13452 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13453 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13454 }
13455 else
13456 complaint (_("Cannot find function owning DW_TAG_call_site "
13457 "DIE %s [in module %s]"),
13458 sect_offset_str (die->sect_off), objfile_name (objfile));
13459 }
13460 }
13461
13462 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13463 if (attr == NULL)
13464 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13465 if (attr == NULL)
13466 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13467 if (attr == NULL)
13468 {
13469 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13470 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13471 }
13472
13473 call_site->target.set_loc_dwarf_block (nullptr);
13474 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13475 /* Keep NULL DWARF_BLOCK. */;
13476 else if (attr->form_is_block ())
13477 {
13478 struct dwarf2_locexpr_baton *dlbaton;
13479 struct dwarf_block *block = attr->as_block ();
13480
13481 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13482 dlbaton->data = block->data;
13483 dlbaton->size = block->size;
13484 dlbaton->per_objfile = per_objfile;
13485 dlbaton->per_cu = cu->per_cu;
13486
13487 call_site->target.set_loc_dwarf_block (dlbaton);
13488 }
13489 else if (attr->form_is_ref ())
13490 {
13491 struct dwarf2_cu *target_cu = cu;
13492 struct die_info *target_die;
13493
13494 target_die = follow_die_ref (die, attr, &target_cu);
13495 gdb_assert (target_cu->per_objfile->objfile == objfile);
13496 if (die_is_declaration (target_die, target_cu))
13497 {
13498 const char *target_physname;
13499
13500 /* Prefer the mangled name; otherwise compute the demangled one. */
13501 target_physname = dw2_linkage_name (target_die, target_cu);
13502 if (target_physname == NULL)
13503 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13504 if (target_physname == NULL)
13505 complaint (_("DW_AT_call_target target DIE has invalid "
13506 "physname, for referencing DIE %s [in module %s]"),
13507 sect_offset_str (die->sect_off), objfile_name (objfile));
13508 else
13509 call_site->target.set_loc_physname (target_physname);
13510 }
13511 else
13512 {
13513 CORE_ADDR lowpc;
13514
13515 /* DW_AT_entry_pc should be preferred. */
13516 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13517 <= PC_BOUNDS_INVALID)
13518 complaint (_("DW_AT_call_target target DIE has invalid "
13519 "low pc, for referencing DIE %s [in module %s]"),
13520 sect_offset_str (die->sect_off), objfile_name (objfile));
13521 else
13522 {
13523 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
13524 - baseaddr);
13525 call_site->target.set_loc_physaddr (lowpc);
13526 }
13527 }
13528 }
13529 else
13530 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13531 "block nor reference, for DIE %s [in module %s]"),
13532 sect_offset_str (die->sect_off), objfile_name (objfile));
13533
13534 for (child_die = die->child;
13535 child_die && child_die->tag;
13536 child_die = child_die->sibling)
13537 {
13538 struct call_site_parameter *parameter;
13539 struct attribute *loc, *origin;
13540
13541 if (child_die->tag != DW_TAG_call_site_parameter
13542 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13543 {
13544 /* Already printed the complaint above. */
13545 continue;
13546 }
13547
13548 gdb_assert (call_site->parameter_count < nparams);
13549 parameter = &call_site->parameter[call_site->parameter_count];
13550
13551 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13552 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13553 register is contained in DW_AT_call_value. */
13554
13555 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13556 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13557 if (origin == NULL)
13558 {
13559 /* This was a pre-DWARF-5 GNU extension alias
13560 for DW_AT_call_parameter. */
13561 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13562 }
13563 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13564 {
13565 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13566
13567 sect_offset sect_off = origin->get_ref_die_offset ();
13568 if (!cu->header.offset_in_cu_p (sect_off))
13569 {
13570 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13571 binding can be done only inside one CU. Such referenced DIE
13572 therefore cannot be even moved to DW_TAG_partial_unit. */
13573 complaint (_("DW_AT_call_parameter offset is not in CU for "
13574 "DW_TAG_call_site child DIE %s [in module %s]"),
13575 sect_offset_str (child_die->sect_off),
13576 objfile_name (objfile));
13577 continue;
13578 }
13579 parameter->u.param_cu_off
13580 = (cu_offset) (sect_off - cu->header.sect_off);
13581 }
13582 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13583 {
13584 complaint (_("No DW_FORM_block* DW_AT_location for "
13585 "DW_TAG_call_site child DIE %s [in module %s]"),
13586 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13587 continue;
13588 }
13589 else
13590 {
13591 struct dwarf_block *block = loc->as_block ();
13592
13593 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13594 (block->data, &block->data[block->size]);
13595 if (parameter->u.dwarf_reg != -1)
13596 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13597 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13598 &block->data[block->size],
13599 &parameter->u.fb_offset))
13600 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13601 else
13602 {
13603 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13604 "for DW_FORM_block* DW_AT_location is supported for "
13605 "DW_TAG_call_site child DIE %s "
13606 "[in module %s]"),
13607 sect_offset_str (child_die->sect_off),
13608 objfile_name (objfile));
13609 continue;
13610 }
13611 }
13612
13613 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13614 if (attr == NULL)
13615 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13616 if (attr == NULL || !attr->form_is_block ())
13617 {
13618 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13619 "DW_TAG_call_site child DIE %s [in module %s]"),
13620 sect_offset_str (child_die->sect_off),
13621 objfile_name (objfile));
13622 continue;
13623 }
13624
13625 struct dwarf_block *block = attr->as_block ();
13626 parameter->value = block->data;
13627 parameter->value_size = block->size;
13628
13629 /* Parameters are not pre-cleared by memset above. */
13630 parameter->data_value = NULL;
13631 parameter->data_value_size = 0;
13632 call_site->parameter_count++;
13633
13634 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13635 if (attr == NULL)
13636 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13637 if (attr != nullptr)
13638 {
13639 if (!attr->form_is_block ())
13640 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13641 "DW_TAG_call_site child DIE %s [in module %s]"),
13642 sect_offset_str (child_die->sect_off),
13643 objfile_name (objfile));
13644 else
13645 {
13646 block = attr->as_block ();
13647 parameter->data_value = block->data;
13648 parameter->data_value_size = block->size;
13649 }
13650 }
13651 }
13652 }
13653
13654 /* Helper function for read_variable. If DIE represents a virtual
13655 table, then return the type of the concrete object that is
13656 associated with the virtual table. Otherwise, return NULL. */
13657
13658 static struct type *
13659 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13660 {
13661 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13662 if (attr == NULL)
13663 return NULL;
13664
13665 /* Find the type DIE. */
13666 struct die_info *type_die = NULL;
13667 struct dwarf2_cu *type_cu = cu;
13668
13669 if (attr->form_is_ref ())
13670 type_die = follow_die_ref (die, attr, &type_cu);
13671 if (type_die == NULL)
13672 return NULL;
13673
13674 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13675 return NULL;
13676 return die_containing_type (type_die, type_cu);
13677 }
13678
13679 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13680
13681 static void
13682 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13683 {
13684 struct rust_vtable_symbol *storage = NULL;
13685
13686 if (cu->per_cu->lang == language_rust)
13687 {
13688 struct type *containing_type = rust_containing_type (die, cu);
13689
13690 if (containing_type != NULL)
13691 {
13692 struct objfile *objfile = cu->per_objfile->objfile;
13693
13694 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13695 storage->concrete_type = containing_type;
13696 storage->subclass = SYMBOL_RUST_VTABLE;
13697 }
13698 }
13699
13700 struct symbol *res = new_symbol (die, NULL, cu, storage);
13701 struct attribute *abstract_origin
13702 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13703 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13704 if (res == NULL && loc && abstract_origin)
13705 {
13706 /* We have a variable without a name, but with a location and an abstract
13707 origin. This may be a concrete instance of an abstract variable
13708 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13709 later. */
13710 struct dwarf2_cu *origin_cu = cu;
13711 struct die_info *origin_die
13712 = follow_die_ref (die, abstract_origin, &origin_cu);
13713 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13714 per_objfile->per_bfd->abstract_to_concrete
13715 [origin_die->sect_off].push_back (die->sect_off);
13716 }
13717 }
13718
13719 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13720 reading .debug_rnglists.
13721 Callback's type should be:
13722 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13723 Return true if the attributes are present and valid, otherwise,
13724 return false. */
13725
13726 template <typename Callback>
13727 static bool
13728 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13729 dwarf_tag tag, Callback &&callback)
13730 {
13731 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13732 struct objfile *objfile = per_objfile->objfile;
13733 bfd *obfd = objfile->obfd;
13734 /* Base address selection entry. */
13735 gdb::optional<CORE_ADDR> base;
13736 const gdb_byte *buffer;
13737 bool overflow = false;
13738 ULONGEST addr_index;
13739 struct dwarf2_section_info *rnglists_section;
13740
13741 base = cu->base_address;
13742 rnglists_section = cu_debug_rnglists_section (cu, tag);
13743 rnglists_section->read (objfile);
13744
13745 if (offset >= rnglists_section->size)
13746 {
13747 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13748 offset);
13749 return false;
13750 }
13751 buffer = rnglists_section->buffer + offset;
13752
13753 while (1)
13754 {
13755 /* Initialize it due to a false compiler warning. */
13756 CORE_ADDR range_beginning = 0, range_end = 0;
13757 const gdb_byte *buf_end = (rnglists_section->buffer
13758 + rnglists_section->size);
13759 unsigned int bytes_read;
13760
13761 if (buffer == buf_end)
13762 {
13763 overflow = true;
13764 break;
13765 }
13766 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13767 switch (rlet)
13768 {
13769 case DW_RLE_end_of_list:
13770 break;
13771 case DW_RLE_base_address:
13772 if (buffer + cu->header.addr_size > buf_end)
13773 {
13774 overflow = true;
13775 break;
13776 }
13777 base = cu->header.read_address (obfd, buffer, &bytes_read);
13778 buffer += bytes_read;
13779 break;
13780 case DW_RLE_base_addressx:
13781 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13782 buffer += bytes_read;
13783 base = read_addr_index (cu, addr_index);
13784 break;
13785 case DW_RLE_start_length:
13786 if (buffer + cu->header.addr_size > buf_end)
13787 {
13788 overflow = true;
13789 break;
13790 }
13791 range_beginning = cu->header.read_address (obfd, buffer,
13792 &bytes_read);
13793 buffer += bytes_read;
13794 range_end = (range_beginning
13795 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13796 buffer += bytes_read;
13797 if (buffer > buf_end)
13798 {
13799 overflow = true;
13800 break;
13801 }
13802 break;
13803 case DW_RLE_startx_length:
13804 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13805 buffer += bytes_read;
13806 range_beginning = read_addr_index (cu, addr_index);
13807 if (buffer > buf_end)
13808 {
13809 overflow = true;
13810 break;
13811 }
13812 range_end = (range_beginning
13813 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13814 buffer += bytes_read;
13815 break;
13816 case DW_RLE_offset_pair:
13817 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13818 buffer += bytes_read;
13819 if (buffer > buf_end)
13820 {
13821 overflow = true;
13822 break;
13823 }
13824 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13825 buffer += bytes_read;
13826 if (buffer > buf_end)
13827 {
13828 overflow = true;
13829 break;
13830 }
13831 break;
13832 case DW_RLE_start_end:
13833 if (buffer + 2 * cu->header.addr_size > buf_end)
13834 {
13835 overflow = true;
13836 break;
13837 }
13838 range_beginning = cu->header.read_address (obfd, buffer,
13839 &bytes_read);
13840 buffer += bytes_read;
13841 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13842 buffer += bytes_read;
13843 break;
13844 case DW_RLE_startx_endx:
13845 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13846 buffer += bytes_read;
13847 range_beginning = read_addr_index (cu, addr_index);
13848 if (buffer > buf_end)
13849 {
13850 overflow = true;
13851 break;
13852 }
13853 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13854 buffer += bytes_read;
13855 range_end = read_addr_index (cu, addr_index);
13856 break;
13857 default:
13858 complaint (_("Invalid .debug_rnglists data (no base address)"));
13859 return false;
13860 }
13861 if (rlet == DW_RLE_end_of_list || overflow)
13862 break;
13863 if (rlet == DW_RLE_base_address)
13864 continue;
13865
13866 if (range_beginning > range_end)
13867 {
13868 /* Inverted range entries are invalid. */
13869 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13870 return false;
13871 }
13872
13873 /* Empty range entries have no effect. */
13874 if (range_beginning == range_end)
13875 continue;
13876
13877 /* Only DW_RLE_offset_pair needs the base address added. */
13878 if (rlet == DW_RLE_offset_pair)
13879 {
13880 if (!base.has_value ())
13881 {
13882 /* We have no valid base address for the DW_RLE_offset_pair. */
13883 complaint (_("Invalid .debug_rnglists data (no base address for "
13884 "DW_RLE_offset_pair)"));
13885 return false;
13886 }
13887
13888 range_beginning += *base;
13889 range_end += *base;
13890 }
13891
13892 /* A not-uncommon case of bad debug info.
13893 Don't pollute the addrmap with bad data. */
13894 if (range_beginning == 0
13895 && !per_objfile->per_bfd->has_section_at_zero)
13896 {
13897 complaint (_(".debug_rnglists entry has start address of zero"
13898 " [in module %s]"), objfile_name (objfile));
13899 continue;
13900 }
13901
13902 callback (range_beginning, range_end);
13903 }
13904
13905 if (overflow)
13906 {
13907 complaint (_("Offset %d is not terminated "
13908 "for DW_AT_ranges attribute"),
13909 offset);
13910 return false;
13911 }
13912
13913 return true;
13914 }
13915
13916 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13917 Callback's type should be:
13918 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13919 Return 1 if the attributes are present and valid, otherwise, return 0. */
13920
13921 template <typename Callback>
13922 static int
13923 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13924 Callback &&callback)
13925 {
13926 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13927 struct objfile *objfile = per_objfile->objfile;
13928 struct comp_unit_head *cu_header = &cu->header;
13929 bfd *obfd = objfile->obfd;
13930 unsigned int addr_size = cu_header->addr_size;
13931 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13932 /* Base address selection entry. */
13933 gdb::optional<CORE_ADDR> base;
13934 unsigned int dummy;
13935 const gdb_byte *buffer;
13936
13937 if (cu_header->version >= 5)
13938 return dwarf2_rnglists_process (offset, cu, tag, callback);
13939
13940 base = cu->base_address;
13941
13942 per_objfile->per_bfd->ranges.read (objfile);
13943 if (offset >= per_objfile->per_bfd->ranges.size)
13944 {
13945 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13946 offset);
13947 return 0;
13948 }
13949 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13950
13951 while (1)
13952 {
13953 CORE_ADDR range_beginning, range_end;
13954
13955 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13956 buffer += addr_size;
13957 range_end = cu->header.read_address (obfd, buffer, &dummy);
13958 buffer += addr_size;
13959 offset += 2 * addr_size;
13960
13961 /* An end of list marker is a pair of zero addresses. */
13962 if (range_beginning == 0 && range_end == 0)
13963 /* Found the end of list entry. */
13964 break;
13965
13966 /* Each base address selection entry is a pair of 2 values.
13967 The first is the largest possible address, the second is
13968 the base address. Check for a base address here. */
13969 if ((range_beginning & mask) == mask)
13970 {
13971 /* If we found the largest possible address, then we already
13972 have the base address in range_end. */
13973 base = range_end;
13974 continue;
13975 }
13976
13977 if (!base.has_value ())
13978 {
13979 /* We have no valid base address for the ranges
13980 data. */
13981 complaint (_("Invalid .debug_ranges data (no base address)"));
13982 return 0;
13983 }
13984
13985 if (range_beginning > range_end)
13986 {
13987 /* Inverted range entries are invalid. */
13988 complaint (_("Invalid .debug_ranges data (inverted range)"));
13989 return 0;
13990 }
13991
13992 /* Empty range entries have no effect. */
13993 if (range_beginning == range_end)
13994 continue;
13995
13996 range_beginning += *base;
13997 range_end += *base;
13998
13999 /* A not-uncommon case of bad debug info.
14000 Don't pollute the addrmap with bad data. */
14001 if (range_beginning == 0
14002 && !per_objfile->per_bfd->has_section_at_zero)
14003 {
14004 complaint (_(".debug_ranges entry has start address of zero"
14005 " [in module %s]"), objfile_name (objfile));
14006 continue;
14007 }
14008
14009 callback (range_beginning, range_end);
14010 }
14011
14012 return 1;
14013 }
14014
14015 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14016 Return 1 if the attributes are present and valid, otherwise, return 0.
14017 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14018
14019 static int
14020 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14021 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14022 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14023 {
14024 struct objfile *objfile = cu->per_objfile->objfile;
14025 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14026 struct gdbarch *gdbarch = objfile->arch ();
14027 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14028 int low_set = 0;
14029 CORE_ADDR low = 0;
14030 CORE_ADDR high = 0;
14031 int retval;
14032
14033 retval = dwarf2_ranges_process (offset, cu, tag,
14034 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14035 {
14036 if (ranges_pst != NULL)
14037 {
14038 CORE_ADDR lowpc;
14039 CORE_ADDR highpc;
14040
14041 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14042 range_beginning + baseaddr)
14043 - baseaddr);
14044 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14045 range_end + baseaddr)
14046 - baseaddr);
14047 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14048 lowpc, highpc - 1, ranges_pst);
14049 }
14050
14051 /* FIXME: This is recording everything as a low-high
14052 segment of consecutive addresses. We should have a
14053 data structure for discontiguous block ranges
14054 instead. */
14055 if (! low_set)
14056 {
14057 low = range_beginning;
14058 high = range_end;
14059 low_set = 1;
14060 }
14061 else
14062 {
14063 if (range_beginning < low)
14064 low = range_beginning;
14065 if (range_end > high)
14066 high = range_end;
14067 }
14068 });
14069 if (!retval)
14070 return 0;
14071
14072 if (! low_set)
14073 /* If the first entry is an end-of-list marker, the range
14074 describes an empty scope, i.e. no instructions. */
14075 return 0;
14076
14077 if (low_return)
14078 *low_return = low;
14079 if (high_return)
14080 *high_return = high;
14081 return 1;
14082 }
14083
14084 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14085 definition for the return value. *LOWPC and *HIGHPC are set iff
14086 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14087
14088 static enum pc_bounds_kind
14089 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14090 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14091 dwarf2_psymtab *pst)
14092 {
14093 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14094 struct attribute *attr;
14095 struct attribute *attr_high;
14096 CORE_ADDR low = 0;
14097 CORE_ADDR high = 0;
14098 enum pc_bounds_kind ret;
14099
14100 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14101 if (attr_high)
14102 {
14103 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14104 if (attr != nullptr)
14105 {
14106 low = attr->as_address ();
14107 high = attr_high->as_address ();
14108 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14109 high += low;
14110 }
14111 else
14112 /* Found high w/o low attribute. */
14113 return PC_BOUNDS_INVALID;
14114
14115 /* Found consecutive range of addresses. */
14116 ret = PC_BOUNDS_HIGH_LOW;
14117 }
14118 else
14119 {
14120 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14121 if (attr != nullptr && attr->form_is_unsigned ())
14122 {
14123 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14124 on DWARF version). */
14125 ULONGEST ranges_offset = attr->as_unsigned ();
14126
14127 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14128 this value. */
14129 if (die->tag != DW_TAG_compile_unit)
14130 ranges_offset += cu->gnu_ranges_base;
14131
14132 /* Value of the DW_AT_ranges attribute is the offset in the
14133 .debug_ranges section. */
14134 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14135 die->tag))
14136 return PC_BOUNDS_INVALID;
14137 /* Found discontinuous range of addresses. */
14138 ret = PC_BOUNDS_RANGES;
14139 }
14140 else
14141 return PC_BOUNDS_NOT_PRESENT;
14142 }
14143
14144 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14145 if (high <= low)
14146 return PC_BOUNDS_INVALID;
14147
14148 /* When using the GNU linker, .gnu.linkonce. sections are used to
14149 eliminate duplicate copies of functions and vtables and such.
14150 The linker will arbitrarily choose one and discard the others.
14151 The AT_*_pc values for such functions refer to local labels in
14152 these sections. If the section from that file was discarded, the
14153 labels are not in the output, so the relocs get a value of 0.
14154 If this is a discarded function, mark the pc bounds as invalid,
14155 so that GDB will ignore it. */
14156 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14157 return PC_BOUNDS_INVALID;
14158
14159 *lowpc = low;
14160 if (highpc)
14161 *highpc = high;
14162 return ret;
14163 }
14164
14165 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14166 its low and high PC addresses. Do nothing if these addresses could not
14167 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14168 and HIGHPC to the high address if greater than HIGHPC. */
14169
14170 static void
14171 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14172 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14173 struct dwarf2_cu *cu)
14174 {
14175 CORE_ADDR low, high;
14176 struct die_info *child = die->child;
14177
14178 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14179 {
14180 *lowpc = std::min (*lowpc, low);
14181 *highpc = std::max (*highpc, high);
14182 }
14183
14184 /* If the language does not allow nested subprograms (either inside
14185 subprograms or lexical blocks), we're done. */
14186 if (cu->per_cu->lang != language_ada)
14187 return;
14188
14189 /* Check all the children of the given DIE. If it contains nested
14190 subprograms, then check their pc bounds. Likewise, we need to
14191 check lexical blocks as well, as they may also contain subprogram
14192 definitions. */
14193 while (child && child->tag)
14194 {
14195 if (child->tag == DW_TAG_subprogram
14196 || child->tag == DW_TAG_lexical_block)
14197 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14198 child = child->sibling;
14199 }
14200 }
14201
14202 /* Get the low and high pc's represented by the scope DIE, and store
14203 them in *LOWPC and *HIGHPC. If the correct values can't be
14204 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14205
14206 static void
14207 get_scope_pc_bounds (struct die_info *die,
14208 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14209 struct dwarf2_cu *cu)
14210 {
14211 CORE_ADDR best_low = (CORE_ADDR) -1;
14212 CORE_ADDR best_high = (CORE_ADDR) 0;
14213 CORE_ADDR current_low, current_high;
14214
14215 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14216 >= PC_BOUNDS_RANGES)
14217 {
14218 best_low = current_low;
14219 best_high = current_high;
14220 }
14221 else
14222 {
14223 struct die_info *child = die->child;
14224
14225 while (child && child->tag)
14226 {
14227 switch (child->tag) {
14228 case DW_TAG_subprogram:
14229 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14230 break;
14231 case DW_TAG_namespace:
14232 case DW_TAG_module:
14233 /* FIXME: carlton/2004-01-16: Should we do this for
14234 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14235 that current GCC's always emit the DIEs corresponding
14236 to definitions of methods of classes as children of a
14237 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14238 the DIEs giving the declarations, which could be
14239 anywhere). But I don't see any reason why the
14240 standards says that they have to be there. */
14241 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14242
14243 if (current_low != ((CORE_ADDR) -1))
14244 {
14245 best_low = std::min (best_low, current_low);
14246 best_high = std::max (best_high, current_high);
14247 }
14248 break;
14249 default:
14250 /* Ignore. */
14251 break;
14252 }
14253
14254 child = child->sibling;
14255 }
14256 }
14257
14258 *lowpc = best_low;
14259 *highpc = best_high;
14260 }
14261
14262 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14263 in DIE. */
14264
14265 static void
14266 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14267 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14268 {
14269 struct objfile *objfile = cu->per_objfile->objfile;
14270 struct gdbarch *gdbarch = objfile->arch ();
14271 struct attribute *attr;
14272 struct attribute *attr_high;
14273
14274 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14275 if (attr_high)
14276 {
14277 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14278 if (attr != nullptr)
14279 {
14280 CORE_ADDR low = attr->as_address ();
14281 CORE_ADDR high = attr_high->as_address ();
14282
14283 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14284 high += low;
14285
14286 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14287 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14288 cu->get_builder ()->record_block_range (block, low, high - 1);
14289 }
14290 }
14291
14292 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14293 if (attr != nullptr && attr->form_is_unsigned ())
14294 {
14295 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14296 on DWARF version). */
14297 ULONGEST ranges_offset = attr->as_unsigned ();
14298
14299 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14300 this value. */
14301 if (die->tag != DW_TAG_compile_unit)
14302 ranges_offset += cu->gnu_ranges_base;
14303
14304 std::vector<blockrange> blockvec;
14305 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14306 [&] (CORE_ADDR start, CORE_ADDR end)
14307 {
14308 start += baseaddr;
14309 end += baseaddr;
14310 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14311 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14312 cu->get_builder ()->record_block_range (block, start, end - 1);
14313 blockvec.emplace_back (start, end);
14314 });
14315
14316 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14317 }
14318 }
14319
14320 /* Check whether the producer field indicates either of GCC < 4.6, or the
14321 Intel C/C++ compiler, and cache the result in CU. */
14322
14323 static void
14324 check_producer (struct dwarf2_cu *cu)
14325 {
14326 int major, minor;
14327
14328 if (cu->producer == NULL)
14329 {
14330 /* For unknown compilers expect their behavior is DWARF version
14331 compliant.
14332
14333 GCC started to support .debug_types sections by -gdwarf-4 since
14334 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14335 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14336 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14337 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14338 }
14339 else if (producer_is_gcc (cu->producer, &major, &minor))
14340 {
14341 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14342 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14343 }
14344 else if (producer_is_icc (cu->producer, &major, &minor))
14345 {
14346 cu->producer_is_icc = true;
14347 cu->producer_is_icc_lt_14 = major < 14;
14348 }
14349 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14350 cu->producer_is_codewarrior = true;
14351 else
14352 {
14353 /* For other non-GCC compilers, expect their behavior is DWARF version
14354 compliant. */
14355 }
14356
14357 cu->checked_producer = true;
14358 }
14359
14360 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14361 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14362 during 4.6.0 experimental. */
14363
14364 static bool
14365 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14366 {
14367 if (!cu->checked_producer)
14368 check_producer (cu);
14369
14370 return cu->producer_is_gxx_lt_4_6;
14371 }
14372
14373
14374 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14375 with incorrect is_stmt attributes. */
14376
14377 static bool
14378 producer_is_codewarrior (struct dwarf2_cu *cu)
14379 {
14380 if (!cu->checked_producer)
14381 check_producer (cu);
14382
14383 return cu->producer_is_codewarrior;
14384 }
14385
14386 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14387 If that attribute is not available, return the appropriate
14388 default. */
14389
14390 static enum dwarf_access_attribute
14391 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14392 {
14393 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14394 if (attr != nullptr)
14395 {
14396 LONGEST value = attr->constant_value (-1);
14397 if (value == DW_ACCESS_public
14398 || value == DW_ACCESS_protected
14399 || value == DW_ACCESS_private)
14400 return (dwarf_access_attribute) value;
14401 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14402 plongest (value));
14403 }
14404
14405 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14406 {
14407 /* The default DWARF 2 accessibility for members is public, the default
14408 accessibility for inheritance is private. */
14409
14410 if (die->tag != DW_TAG_inheritance)
14411 return DW_ACCESS_public;
14412 else
14413 return DW_ACCESS_private;
14414 }
14415 else
14416 {
14417 /* DWARF 3+ defines the default accessibility a different way. The same
14418 rules apply now for DW_TAG_inheritance as for the members and it only
14419 depends on the container kind. */
14420
14421 if (die->parent->tag == DW_TAG_class_type)
14422 return DW_ACCESS_private;
14423 else
14424 return DW_ACCESS_public;
14425 }
14426 }
14427
14428 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
14429 *OFFSET to the byte offset. If the attribute was not found return
14430 0, otherwise return 1. If it was found but could not properly be
14431 handled, set *OFFSET to 0. */
14432
14433 static int
14434 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14435 LONGEST *offset)
14436 {
14437 struct attribute *attr;
14438
14439 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14440 if (attr != NULL)
14441 {
14442 *offset = 0;
14443
14444 /* Note that we do not check for a section offset first here.
14445 This is because DW_AT_data_member_location is new in DWARF 4,
14446 so if we see it, we can assume that a constant form is really
14447 a constant and not a section offset. */
14448 if (attr->form_is_constant ())
14449 *offset = attr->constant_value (0);
14450 else if (attr->form_is_section_offset ())
14451 dwarf2_complex_location_expr_complaint ();
14452 else if (attr->form_is_block ())
14453 *offset = decode_locdesc (attr->as_block (), cu);
14454 else
14455 dwarf2_complex_location_expr_complaint ();
14456
14457 return 1;
14458 }
14459 else
14460 {
14461 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14462 if (attr != nullptr)
14463 {
14464 *offset = attr->constant_value (0);
14465 return 1;
14466 }
14467 }
14468
14469 return 0;
14470 }
14471
14472 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
14473 store the results in FIELD. */
14474
14475 static void
14476 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14477 struct field *field)
14478 {
14479 struct attribute *attr;
14480
14481 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14482 if (attr != NULL)
14483 {
14484 if (attr->form_is_constant ())
14485 {
14486 LONGEST offset = attr->constant_value (0);
14487 field->set_loc_bitpos (offset * bits_per_byte);
14488 }
14489 else if (attr->form_is_section_offset ())
14490 dwarf2_complex_location_expr_complaint ();
14491 else if (attr->form_is_block ())
14492 {
14493 bool handled;
14494 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14495 if (handled)
14496 field->set_loc_bitpos (offset * bits_per_byte);
14497 else
14498 {
14499 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14500 struct objfile *objfile = per_objfile->objfile;
14501 struct dwarf2_locexpr_baton *dlbaton
14502 = XOBNEW (&objfile->objfile_obstack,
14503 struct dwarf2_locexpr_baton);
14504 dlbaton->data = attr->as_block ()->data;
14505 dlbaton->size = attr->as_block ()->size;
14506 /* When using this baton, we want to compute the address
14507 of the field, not the value. This is why
14508 is_reference is set to false here. */
14509 dlbaton->is_reference = false;
14510 dlbaton->per_objfile = per_objfile;
14511 dlbaton->per_cu = cu->per_cu;
14512
14513 field->set_loc_dwarf_block (dlbaton);
14514 }
14515 }
14516 else
14517 dwarf2_complex_location_expr_complaint ();
14518 }
14519 else
14520 {
14521 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14522 if (attr != nullptr)
14523 field->set_loc_bitpos (attr->constant_value (0));
14524 }
14525 }
14526
14527 /* Add an aggregate field to the field list. */
14528
14529 static void
14530 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14531 struct dwarf2_cu *cu)
14532 {
14533 struct objfile *objfile = cu->per_objfile->objfile;
14534 struct gdbarch *gdbarch = objfile->arch ();
14535 struct nextfield *new_field;
14536 struct attribute *attr;
14537 struct field *fp;
14538 const char *fieldname = "";
14539
14540 if (die->tag == DW_TAG_inheritance)
14541 {
14542 fip->baseclasses.emplace_back ();
14543 new_field = &fip->baseclasses.back ();
14544 }
14545 else
14546 {
14547 fip->fields.emplace_back ();
14548 new_field = &fip->fields.back ();
14549 }
14550
14551 new_field->offset = die->sect_off;
14552
14553 new_field->accessibility = dwarf2_access_attribute (die, cu);
14554 if (new_field->accessibility != DW_ACCESS_public)
14555 fip->non_public_fields = true;
14556
14557 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14558 if (attr != nullptr)
14559 new_field->virtuality = attr->as_virtuality ();
14560 else
14561 new_field->virtuality = DW_VIRTUALITY_none;
14562
14563 fp = &new_field->field;
14564
14565 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14566 {
14567 /* Data member other than a C++ static data member. */
14568
14569 /* Get type of field. */
14570 fp->set_type (die_type (die, cu));
14571
14572 fp->set_loc_bitpos (0);
14573
14574 /* Get bit size of field (zero if none). */
14575 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14576 if (attr != nullptr)
14577 {
14578 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14579 }
14580 else
14581 {
14582 FIELD_BITSIZE (*fp) = 0;
14583 }
14584
14585 /* Get bit offset of field. */
14586 handle_member_location (die, cu, fp);
14587 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14588 if (attr != nullptr && attr->form_is_constant ())
14589 {
14590 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14591 {
14592 /* For big endian bits, the DW_AT_bit_offset gives the
14593 additional bit offset from the MSB of the containing
14594 anonymous object to the MSB of the field. We don't
14595 have to do anything special since we don't need to
14596 know the size of the anonymous object. */
14597 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
14598 }
14599 else
14600 {
14601 /* For little endian bits, compute the bit offset to the
14602 MSB of the anonymous object, subtract off the number of
14603 bits from the MSB of the field to the MSB of the
14604 object, and then subtract off the number of bits of
14605 the field itself. The result is the bit offset of
14606 the LSB of the field. */
14607 int anonymous_size;
14608 int bit_offset = attr->constant_value (0);
14609
14610 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14611 if (attr != nullptr && attr->form_is_constant ())
14612 {
14613 /* The size of the anonymous object containing
14614 the bit field is explicit, so use the
14615 indicated size (in bytes). */
14616 anonymous_size = attr->constant_value (0);
14617 }
14618 else
14619 {
14620 /* The size of the anonymous object containing
14621 the bit field must be inferred from the type
14622 attribute of the data member containing the
14623 bit field. */
14624 anonymous_size = TYPE_LENGTH (fp->type ());
14625 }
14626 fp->set_loc_bitpos (fp->loc_bitpos ()
14627 + anonymous_size * bits_per_byte
14628 - bit_offset - FIELD_BITSIZE (*fp));
14629 }
14630 }
14631
14632 /* Get name of field. */
14633 fieldname = dwarf2_name (die, cu);
14634 if (fieldname == NULL)
14635 fieldname = "";
14636
14637 /* The name is already allocated along with this objfile, so we don't
14638 need to duplicate it for the type. */
14639 fp->set_name (fieldname);
14640
14641 /* Change accessibility for artificial fields (e.g. virtual table
14642 pointer or virtual base class pointer) to private. */
14643 if (dwarf2_attr (die, DW_AT_artificial, cu))
14644 {
14645 FIELD_ARTIFICIAL (*fp) = 1;
14646 new_field->accessibility = DW_ACCESS_private;
14647 fip->non_public_fields = true;
14648 }
14649 }
14650 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14651 {
14652 /* C++ static member. */
14653
14654 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14655 is a declaration, but all versions of G++ as of this writing
14656 (so through at least 3.2.1) incorrectly generate
14657 DW_TAG_variable tags. */
14658
14659 const char *physname;
14660
14661 /* Get name of field. */
14662 fieldname = dwarf2_name (die, cu);
14663 if (fieldname == NULL)
14664 return;
14665
14666 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14667 if (attr
14668 /* Only create a symbol if this is an external value.
14669 new_symbol checks this and puts the value in the global symbol
14670 table, which we want. If it is not external, new_symbol
14671 will try to put the value in cu->list_in_scope which is wrong. */
14672 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14673 {
14674 /* A static const member, not much different than an enum as far as
14675 we're concerned, except that we can support more types. */
14676 new_symbol (die, NULL, cu);
14677 }
14678
14679 /* Get physical name. */
14680 physname = dwarf2_physname (fieldname, die, cu);
14681
14682 /* The name is already allocated along with this objfile, so we don't
14683 need to duplicate it for the type. */
14684 fp->set_loc_physname (physname ? physname : "");
14685 fp->set_type (die_type (die, cu));
14686 fp->set_name (fieldname);
14687 }
14688 else if (die->tag == DW_TAG_inheritance)
14689 {
14690 /* C++ base class field. */
14691 handle_member_location (die, cu, fp);
14692 FIELD_BITSIZE (*fp) = 0;
14693 fp->set_type (die_type (die, cu));
14694 fp->set_name (fp->type ()->name ());
14695 }
14696 else
14697 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14698 }
14699
14700 /* Can the type given by DIE define another type? */
14701
14702 static bool
14703 type_can_define_types (const struct die_info *die)
14704 {
14705 switch (die->tag)
14706 {
14707 case DW_TAG_typedef:
14708 case DW_TAG_class_type:
14709 case DW_TAG_structure_type:
14710 case DW_TAG_union_type:
14711 case DW_TAG_enumeration_type:
14712 return true;
14713
14714 default:
14715 return false;
14716 }
14717 }
14718
14719 /* Add a type definition defined in the scope of the FIP's class. */
14720
14721 static void
14722 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14723 struct dwarf2_cu *cu)
14724 {
14725 struct decl_field fp;
14726 memset (&fp, 0, sizeof (fp));
14727
14728 gdb_assert (type_can_define_types (die));
14729
14730 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14731 fp.name = dwarf2_name (die, cu);
14732 fp.type = read_type_die (die, cu);
14733
14734 /* Save accessibility. */
14735 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14736 switch (accessibility)
14737 {
14738 case DW_ACCESS_public:
14739 /* The assumed value if neither private nor protected. */
14740 break;
14741 case DW_ACCESS_private:
14742 fp.is_private = 1;
14743 break;
14744 case DW_ACCESS_protected:
14745 fp.is_protected = 1;
14746 break;
14747 }
14748
14749 if (die->tag == DW_TAG_typedef)
14750 fip->typedef_field_list.push_back (fp);
14751 else
14752 fip->nested_types_list.push_back (fp);
14753 }
14754
14755 /* A convenience typedef that's used when finding the discriminant
14756 field for a variant part. */
14757 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14758 offset_map_type;
14759
14760 /* Compute the discriminant range for a given variant. OBSTACK is
14761 where the results will be stored. VARIANT is the variant to
14762 process. IS_UNSIGNED indicates whether the discriminant is signed
14763 or unsigned. */
14764
14765 static const gdb::array_view<discriminant_range>
14766 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14767 bool is_unsigned)
14768 {
14769 std::vector<discriminant_range> ranges;
14770
14771 if (variant.default_branch)
14772 return {};
14773
14774 if (variant.discr_list_data == nullptr)
14775 {
14776 discriminant_range r
14777 = {variant.discriminant_value, variant.discriminant_value};
14778 ranges.push_back (r);
14779 }
14780 else
14781 {
14782 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14783 variant.discr_list_data->size);
14784 while (!data.empty ())
14785 {
14786 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14787 {
14788 complaint (_("invalid discriminant marker: %d"), data[0]);
14789 break;
14790 }
14791 bool is_range = data[0] == DW_DSC_range;
14792 data = data.slice (1);
14793
14794 ULONGEST low, high;
14795 unsigned int bytes_read;
14796
14797 if (data.empty ())
14798 {
14799 complaint (_("DW_AT_discr_list missing low value"));
14800 break;
14801 }
14802 if (is_unsigned)
14803 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14804 else
14805 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14806 &bytes_read);
14807 data = data.slice (bytes_read);
14808
14809 if (is_range)
14810 {
14811 if (data.empty ())
14812 {
14813 complaint (_("DW_AT_discr_list missing high value"));
14814 break;
14815 }
14816 if (is_unsigned)
14817 high = read_unsigned_leb128 (nullptr, data.data (),
14818 &bytes_read);
14819 else
14820 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14821 &bytes_read);
14822 data = data.slice (bytes_read);
14823 }
14824 else
14825 high = low;
14826
14827 ranges.push_back ({ low, high });
14828 }
14829 }
14830
14831 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14832 ranges.size ());
14833 std::copy (ranges.begin (), ranges.end (), result);
14834 return gdb::array_view<discriminant_range> (result, ranges.size ());
14835 }
14836
14837 static const gdb::array_view<variant_part> create_variant_parts
14838 (struct obstack *obstack,
14839 const offset_map_type &offset_map,
14840 struct field_info *fi,
14841 const std::vector<variant_part_builder> &variant_parts);
14842
14843 /* Fill in a "struct variant" for a given variant field. RESULT is
14844 the variant to fill in. OBSTACK is where any needed allocations
14845 will be done. OFFSET_MAP holds the mapping from section offsets to
14846 fields for the type. FI describes the fields of the type we're
14847 processing. FIELD is the variant field we're converting. */
14848
14849 static void
14850 create_one_variant (variant &result, struct obstack *obstack,
14851 const offset_map_type &offset_map,
14852 struct field_info *fi, const variant_field &field)
14853 {
14854 result.discriminants = convert_variant_range (obstack, field, false);
14855 result.first_field = field.first_field + fi->baseclasses.size ();
14856 result.last_field = field.last_field + fi->baseclasses.size ();
14857 result.parts = create_variant_parts (obstack, offset_map, fi,
14858 field.variant_parts);
14859 }
14860
14861 /* Fill in a "struct variant_part" for a given variant part. RESULT
14862 is the variant part to fill in. OBSTACK is where any needed
14863 allocations will be done. OFFSET_MAP holds the mapping from
14864 section offsets to fields for the type. FI describes the fields of
14865 the type we're processing. BUILDER is the variant part to be
14866 converted. */
14867
14868 static void
14869 create_one_variant_part (variant_part &result,
14870 struct obstack *obstack,
14871 const offset_map_type &offset_map,
14872 struct field_info *fi,
14873 const variant_part_builder &builder)
14874 {
14875 auto iter = offset_map.find (builder.discriminant_offset);
14876 if (iter == offset_map.end ())
14877 {
14878 result.discriminant_index = -1;
14879 /* Doesn't matter. */
14880 result.is_unsigned = false;
14881 }
14882 else
14883 {
14884 result.discriminant_index = iter->second;
14885 result.is_unsigned
14886 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14887 }
14888
14889 size_t n = builder.variants.size ();
14890 variant *output = new (obstack) variant[n];
14891 for (size_t i = 0; i < n; ++i)
14892 create_one_variant (output[i], obstack, offset_map, fi,
14893 builder.variants[i]);
14894
14895 result.variants = gdb::array_view<variant> (output, n);
14896 }
14897
14898 /* Create a vector of variant parts that can be attached to a type.
14899 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14900 holds the mapping from section offsets to fields for the type. FI
14901 describes the fields of the type we're processing. VARIANT_PARTS
14902 is the vector to convert. */
14903
14904 static const gdb::array_view<variant_part>
14905 create_variant_parts (struct obstack *obstack,
14906 const offset_map_type &offset_map,
14907 struct field_info *fi,
14908 const std::vector<variant_part_builder> &variant_parts)
14909 {
14910 if (variant_parts.empty ())
14911 return {};
14912
14913 size_t n = variant_parts.size ();
14914 variant_part *result = new (obstack) variant_part[n];
14915 for (size_t i = 0; i < n; ++i)
14916 create_one_variant_part (result[i], obstack, offset_map, fi,
14917 variant_parts[i]);
14918
14919 return gdb::array_view<variant_part> (result, n);
14920 }
14921
14922 /* Compute the variant part vector for FIP, attaching it to TYPE when
14923 done. */
14924
14925 static void
14926 add_variant_property (struct field_info *fip, struct type *type,
14927 struct dwarf2_cu *cu)
14928 {
14929 /* Map section offsets of fields to their field index. Note the
14930 field index here does not take the number of baseclasses into
14931 account. */
14932 offset_map_type offset_map;
14933 for (int i = 0; i < fip->fields.size (); ++i)
14934 offset_map[fip->fields[i].offset] = i;
14935
14936 struct objfile *objfile = cu->per_objfile->objfile;
14937 gdb::array_view<variant_part> parts
14938 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14939 fip->variant_parts);
14940
14941 struct dynamic_prop prop;
14942 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14943 obstack_copy (&objfile->objfile_obstack, &parts,
14944 sizeof (parts)));
14945
14946 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14947 }
14948
14949 /* Create the vector of fields, and attach it to the type. */
14950
14951 static void
14952 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14953 struct dwarf2_cu *cu)
14954 {
14955 int nfields = fip->nfields ();
14956
14957 /* Record the field count, allocate space for the array of fields,
14958 and create blank accessibility bitfields if necessary. */
14959 type->set_num_fields (nfields);
14960 type->set_fields
14961 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14962
14963 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
14964 {
14965 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14966
14967 TYPE_FIELD_PRIVATE_BITS (type) =
14968 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14969 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14970
14971 TYPE_FIELD_PROTECTED_BITS (type) =
14972 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14973 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14974
14975 TYPE_FIELD_IGNORE_BITS (type) =
14976 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14977 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14978 }
14979
14980 /* If the type has baseclasses, allocate and clear a bit vector for
14981 TYPE_FIELD_VIRTUAL_BITS. */
14982 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
14983 {
14984 int num_bytes = B_BYTES (fip->baseclasses.size ());
14985 unsigned char *pointer;
14986
14987 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14988 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14989 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14990 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14991 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14992 }
14993
14994 if (!fip->variant_parts.empty ())
14995 add_variant_property (fip, type, cu);
14996
14997 /* Copy the saved-up fields into the field vector. */
14998 for (int i = 0; i < nfields; ++i)
14999 {
15000 struct nextfield &field
15001 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15002 : fip->fields[i - fip->baseclasses.size ()]);
15003
15004 type->field (i) = field.field;
15005 switch (field.accessibility)
15006 {
15007 case DW_ACCESS_private:
15008 if (cu->per_cu->lang != language_ada)
15009 SET_TYPE_FIELD_PRIVATE (type, i);
15010 break;
15011
15012 case DW_ACCESS_protected:
15013 if (cu->per_cu->lang != language_ada)
15014 SET_TYPE_FIELD_PROTECTED (type, i);
15015 break;
15016
15017 case DW_ACCESS_public:
15018 break;
15019
15020 default:
15021 /* Unknown accessibility. Complain and treat it as public. */
15022 {
15023 complaint (_("unsupported accessibility %d"),
15024 field.accessibility);
15025 }
15026 break;
15027 }
15028 if (i < fip->baseclasses.size ())
15029 {
15030 switch (field.virtuality)
15031 {
15032 case DW_VIRTUALITY_virtual:
15033 case DW_VIRTUALITY_pure_virtual:
15034 if (cu->per_cu->lang == language_ada)
15035 error (_("unexpected virtuality in component of Ada type"));
15036 SET_TYPE_FIELD_VIRTUAL (type, i);
15037 break;
15038 }
15039 }
15040 }
15041 }
15042
15043 /* Return true if this member function is a constructor, false
15044 otherwise. */
15045
15046 static int
15047 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15048 {
15049 const char *fieldname;
15050 const char *type_name;
15051 int len;
15052
15053 if (die->parent == NULL)
15054 return 0;
15055
15056 if (die->parent->tag != DW_TAG_structure_type
15057 && die->parent->tag != DW_TAG_union_type
15058 && die->parent->tag != DW_TAG_class_type)
15059 return 0;
15060
15061 fieldname = dwarf2_name (die, cu);
15062 type_name = dwarf2_name (die->parent, cu);
15063 if (fieldname == NULL || type_name == NULL)
15064 return 0;
15065
15066 len = strlen (fieldname);
15067 return (strncmp (fieldname, type_name, len) == 0
15068 && (type_name[len] == '\0' || type_name[len] == '<'));
15069 }
15070
15071 /* Add a member function to the proper fieldlist. */
15072
15073 static void
15074 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15075 struct type *type, struct dwarf2_cu *cu)
15076 {
15077 struct objfile *objfile = cu->per_objfile->objfile;
15078 struct attribute *attr;
15079 int i;
15080 struct fnfieldlist *flp = nullptr;
15081 struct fn_field *fnp;
15082 const char *fieldname;
15083 struct type *this_type;
15084
15085 if (cu->per_cu->lang == language_ada)
15086 error (_("unexpected member function in Ada type"));
15087
15088 /* Get name of member function. */
15089 fieldname = dwarf2_name (die, cu);
15090 if (fieldname == NULL)
15091 return;
15092
15093 /* Look up member function name in fieldlist. */
15094 for (i = 0; i < fip->fnfieldlists.size (); i++)
15095 {
15096 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15097 {
15098 flp = &fip->fnfieldlists[i];
15099 break;
15100 }
15101 }
15102
15103 /* Create a new fnfieldlist if necessary. */
15104 if (flp == nullptr)
15105 {
15106 fip->fnfieldlists.emplace_back ();
15107 flp = &fip->fnfieldlists.back ();
15108 flp->name = fieldname;
15109 i = fip->fnfieldlists.size () - 1;
15110 }
15111
15112 /* Create a new member function field and add it to the vector of
15113 fnfieldlists. */
15114 flp->fnfields.emplace_back ();
15115 fnp = &flp->fnfields.back ();
15116
15117 /* Delay processing of the physname until later. */
15118 if (cu->per_cu->lang == language_cplus)
15119 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15120 die, cu);
15121 else
15122 {
15123 const char *physname = dwarf2_physname (fieldname, die, cu);
15124 fnp->physname = physname ? physname : "";
15125 }
15126
15127 fnp->type = alloc_type (objfile);
15128 this_type = read_type_die (die, cu);
15129 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15130 {
15131 int nparams = this_type->num_fields ();
15132
15133 /* TYPE is the domain of this method, and THIS_TYPE is the type
15134 of the method itself (TYPE_CODE_METHOD). */
15135 smash_to_method_type (fnp->type, type,
15136 TYPE_TARGET_TYPE (this_type),
15137 this_type->fields (),
15138 this_type->num_fields (),
15139 this_type->has_varargs ());
15140
15141 /* Handle static member functions.
15142 Dwarf2 has no clean way to discern C++ static and non-static
15143 member functions. G++ helps GDB by marking the first
15144 parameter for non-static member functions (which is the this
15145 pointer) as artificial. We obtain this information from
15146 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15147 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15148 fnp->voffset = VOFFSET_STATIC;
15149 }
15150 else
15151 complaint (_("member function type missing for '%s'"),
15152 dwarf2_full_name (fieldname, die, cu));
15153
15154 /* Get fcontext from DW_AT_containing_type if present. */
15155 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15156 fnp->fcontext = die_containing_type (die, cu);
15157
15158 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15159 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15160
15161 /* Get accessibility. */
15162 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15163 switch (accessibility)
15164 {
15165 case DW_ACCESS_private:
15166 fnp->is_private = 1;
15167 break;
15168 case DW_ACCESS_protected:
15169 fnp->is_protected = 1;
15170 break;
15171 }
15172
15173 /* Check for artificial methods. */
15174 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15175 if (attr && attr->as_boolean ())
15176 fnp->is_artificial = 1;
15177
15178 /* Check for defaulted methods. */
15179 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15180 if (attr != nullptr)
15181 fnp->defaulted = attr->defaulted ();
15182
15183 /* Check for deleted methods. */
15184 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15185 if (attr != nullptr && attr->as_boolean ())
15186 fnp->is_deleted = 1;
15187
15188 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15189
15190 /* Get index in virtual function table if it is a virtual member
15191 function. For older versions of GCC, this is an offset in the
15192 appropriate virtual table, as specified by DW_AT_containing_type.
15193 For everyone else, it is an expression to be evaluated relative
15194 to the object address. */
15195
15196 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15197 if (attr != nullptr)
15198 {
15199 if (attr->form_is_block () && attr->as_block ()->size > 0)
15200 {
15201 struct dwarf_block *block = attr->as_block ();
15202
15203 if (block->data[0] == DW_OP_constu)
15204 {
15205 /* Old-style GCC. */
15206 fnp->voffset = decode_locdesc (block, cu) + 2;
15207 }
15208 else if (block->data[0] == DW_OP_deref
15209 || (block->size > 1
15210 && block->data[0] == DW_OP_deref_size
15211 && block->data[1] == cu->header.addr_size))
15212 {
15213 fnp->voffset = decode_locdesc (block, cu);
15214 if ((fnp->voffset % cu->header.addr_size) != 0)
15215 dwarf2_complex_location_expr_complaint ();
15216 else
15217 fnp->voffset /= cu->header.addr_size;
15218 fnp->voffset += 2;
15219 }
15220 else
15221 dwarf2_complex_location_expr_complaint ();
15222
15223 if (!fnp->fcontext)
15224 {
15225 /* If there is no `this' field and no DW_AT_containing_type,
15226 we cannot actually find a base class context for the
15227 vtable! */
15228 if (this_type->num_fields () == 0
15229 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15230 {
15231 complaint (_("cannot determine context for virtual member "
15232 "function \"%s\" (offset %s)"),
15233 fieldname, sect_offset_str (die->sect_off));
15234 }
15235 else
15236 {
15237 fnp->fcontext
15238 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15239 }
15240 }
15241 }
15242 else if (attr->form_is_section_offset ())
15243 {
15244 dwarf2_complex_location_expr_complaint ();
15245 }
15246 else
15247 {
15248 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15249 fieldname);
15250 }
15251 }
15252 else
15253 {
15254 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15255 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15256 {
15257 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15258 complaint (_("Member function \"%s\" (offset %s) is virtual "
15259 "but the vtable offset is not specified"),
15260 fieldname, sect_offset_str (die->sect_off));
15261 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15262 TYPE_CPLUS_DYNAMIC (type) = 1;
15263 }
15264 }
15265 }
15266
15267 /* Create the vector of member function fields, and attach it to the type. */
15268
15269 static void
15270 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15271 struct dwarf2_cu *cu)
15272 {
15273 if (cu->per_cu->lang == language_ada)
15274 error (_("unexpected member functions in Ada type"));
15275
15276 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15277 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15278 TYPE_ALLOC (type,
15279 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15280
15281 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15282 {
15283 struct fnfieldlist &nf = fip->fnfieldlists[i];
15284 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15285
15286 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15287 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15288 fn_flp->fn_fields = (struct fn_field *)
15289 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15290
15291 for (int k = 0; k < nf.fnfields.size (); ++k)
15292 fn_flp->fn_fields[k] = nf.fnfields[k];
15293 }
15294
15295 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15296 }
15297
15298 /* Returns non-zero if NAME is the name of a vtable member in CU's
15299 language, zero otherwise. */
15300 static int
15301 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15302 {
15303 static const char vptr[] = "_vptr";
15304
15305 /* Look for the C++ form of the vtable. */
15306 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15307 return 1;
15308
15309 return 0;
15310 }
15311
15312 /* GCC outputs unnamed structures that are really pointers to member
15313 functions, with the ABI-specified layout. If TYPE describes
15314 such a structure, smash it into a member function type.
15315
15316 GCC shouldn't do this; it should just output pointer to member DIEs.
15317 This is GCC PR debug/28767. */
15318
15319 static void
15320 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15321 {
15322 struct type *pfn_type, *self_type, *new_type;
15323
15324 /* Check for a structure with no name and two children. */
15325 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15326 return;
15327
15328 /* Check for __pfn and __delta members. */
15329 if (type->field (0).name () == NULL
15330 || strcmp (type->field (0).name (), "__pfn") != 0
15331 || type->field (1).name () == NULL
15332 || strcmp (type->field (1).name (), "__delta") != 0)
15333 return;
15334
15335 /* Find the type of the method. */
15336 pfn_type = type->field (0).type ();
15337 if (pfn_type == NULL
15338 || pfn_type->code () != TYPE_CODE_PTR
15339 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15340 return;
15341
15342 /* Look for the "this" argument. */
15343 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15344 if (pfn_type->num_fields () == 0
15345 /* || pfn_type->field (0).type () == NULL */
15346 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15347 return;
15348
15349 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15350 new_type = alloc_type (objfile);
15351 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15352 pfn_type->fields (), pfn_type->num_fields (),
15353 pfn_type->has_varargs ());
15354 smash_to_methodptr_type (type, new_type);
15355 }
15356
15357 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15358 requires rewriting, then copy it and return the updated copy.
15359 Otherwise return nullptr. */
15360
15361 static struct type *
15362 rewrite_array_type (struct type *type)
15363 {
15364 if (type->code () != TYPE_CODE_ARRAY)
15365 return nullptr;
15366
15367 struct type *index_type = type->index_type ();
15368 range_bounds *current_bounds = index_type->bounds ();
15369
15370 /* Handle multi-dimensional arrays. */
15371 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15372 if (new_target == nullptr)
15373 {
15374 /* Maybe we don't need to rewrite this array. */
15375 if (current_bounds->low.kind () == PROP_CONST
15376 && current_bounds->high.kind () == PROP_CONST)
15377 return nullptr;
15378 }
15379
15380 /* Either the target type was rewritten, or the bounds have to be
15381 updated. Either way we want to copy the type and update
15382 everything. */
15383 struct type *copy = copy_type (type);
15384 int nfields = copy->num_fields ();
15385 field *new_fields
15386 = ((struct field *) TYPE_ZALLOC (copy,
15387 nfields * sizeof (struct field)));
15388 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15389 copy->set_fields (new_fields);
15390 if (new_target != nullptr)
15391 TYPE_TARGET_TYPE (copy) = new_target;
15392
15393 struct type *index_copy = copy_type (index_type);
15394 range_bounds *bounds
15395 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15396 sizeof (range_bounds));
15397 *bounds = *current_bounds;
15398 bounds->low.set_const_val (1);
15399 bounds->high.set_const_val (0);
15400 index_copy->set_bounds (bounds);
15401 copy->set_index_type (index_copy);
15402
15403 return copy;
15404 }
15405
15406 /* While some versions of GCC will generate complicated DWARF for an
15407 array (see quirk_ada_thick_pointer), more recent versions were
15408 modified to emit an explicit thick pointer structure. However, in
15409 this case, the array still has DWARF expressions for its ranges,
15410 and these must be ignored. */
15411
15412 static void
15413 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15414 struct type *type)
15415 {
15416 gdb_assert (cu->per_cu->lang == language_ada);
15417
15418 /* Check for a structure with two children. */
15419 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15420 return;
15421
15422 /* Check for P_ARRAY and P_BOUNDS members. */
15423 if (type->field (0).name () == NULL
15424 || strcmp (type->field (0).name (), "P_ARRAY") != 0
15425 || type->field (1).name () == NULL
15426 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
15427 return;
15428
15429 /* Make sure we're looking at a pointer to an array. */
15430 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15431 return;
15432
15433 /* The Ada code already knows how to handle these types, so all that
15434 we need to do is turn the bounds into static bounds. However, we
15435 don't want to rewrite existing array or index types in-place,
15436 because those may be referenced in other contexts where this
15437 rewriting is undesirable. */
15438 struct type *new_ary_type
15439 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15440 if (new_ary_type != nullptr)
15441 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15442 }
15443
15444 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15445 appropriate error checking and issuing complaints if there is a
15446 problem. */
15447
15448 static ULONGEST
15449 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15450 {
15451 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15452
15453 if (attr == nullptr)
15454 return 0;
15455
15456 if (!attr->form_is_constant ())
15457 {
15458 complaint (_("DW_AT_alignment must have constant form"
15459 " - DIE at %s [in module %s]"),
15460 sect_offset_str (die->sect_off),
15461 objfile_name (cu->per_objfile->objfile));
15462 return 0;
15463 }
15464
15465 LONGEST val = attr->constant_value (0);
15466 if (val < 0)
15467 {
15468 complaint (_("DW_AT_alignment value must not be negative"
15469 " - DIE at %s [in module %s]"),
15470 sect_offset_str (die->sect_off),
15471 objfile_name (cu->per_objfile->objfile));
15472 return 0;
15473 }
15474 ULONGEST align = val;
15475
15476 if (align == 0)
15477 {
15478 complaint (_("DW_AT_alignment value must not be zero"
15479 " - DIE at %s [in module %s]"),
15480 sect_offset_str (die->sect_off),
15481 objfile_name (cu->per_objfile->objfile));
15482 return 0;
15483 }
15484 if ((align & (align - 1)) != 0)
15485 {
15486 complaint (_("DW_AT_alignment value must be a power of 2"
15487 " - DIE at %s [in module %s]"),
15488 sect_offset_str (die->sect_off),
15489 objfile_name (cu->per_objfile->objfile));
15490 return 0;
15491 }
15492
15493 return align;
15494 }
15495
15496 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15497 the alignment for TYPE. */
15498
15499 static void
15500 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15501 struct type *type)
15502 {
15503 if (!set_type_align (type, get_alignment (cu, die)))
15504 complaint (_("DW_AT_alignment value too large"
15505 " - DIE at %s [in module %s]"),
15506 sect_offset_str (die->sect_off),
15507 objfile_name (cu->per_objfile->objfile));
15508 }
15509
15510 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15511 constant for a type, according to DWARF5 spec, Table 5.5. */
15512
15513 static bool
15514 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15515 {
15516 switch (value)
15517 {
15518 case DW_CC_normal:
15519 case DW_CC_pass_by_reference:
15520 case DW_CC_pass_by_value:
15521 return true;
15522
15523 default:
15524 complaint (_("unrecognized DW_AT_calling_convention value "
15525 "(%s) for a type"), pulongest (value));
15526 return false;
15527 }
15528 }
15529
15530 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15531 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15532 also according to GNU-specific values (see include/dwarf2.h). */
15533
15534 static bool
15535 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15536 {
15537 switch (value)
15538 {
15539 case DW_CC_normal:
15540 case DW_CC_program:
15541 case DW_CC_nocall:
15542 return true;
15543
15544 case DW_CC_GNU_renesas_sh:
15545 case DW_CC_GNU_borland_fastcall_i386:
15546 case DW_CC_GDB_IBM_OpenCL:
15547 return true;
15548
15549 default:
15550 complaint (_("unrecognized DW_AT_calling_convention value "
15551 "(%s) for a subroutine"), pulongest (value));
15552 return false;
15553 }
15554 }
15555
15556 /* Called when we find the DIE that starts a structure or union scope
15557 (definition) to create a type for the structure or union. Fill in
15558 the type's name and general properties; the members will not be
15559 processed until process_structure_scope. A symbol table entry for
15560 the type will also not be done until process_structure_scope (assuming
15561 the type has a name).
15562
15563 NOTE: we need to call these functions regardless of whether or not the
15564 DIE has a DW_AT_name attribute, since it might be an anonymous
15565 structure or union. This gets the type entered into our set of
15566 user defined types. */
15567
15568 static struct type *
15569 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15570 {
15571 struct objfile *objfile = cu->per_objfile->objfile;
15572 struct type *type;
15573 struct attribute *attr;
15574 const char *name;
15575
15576 /* If the definition of this type lives in .debug_types, read that type.
15577 Don't follow DW_AT_specification though, that will take us back up
15578 the chain and we want to go down. */
15579 attr = die->attr (DW_AT_signature);
15580 if (attr != nullptr)
15581 {
15582 type = get_DW_AT_signature_type (die, attr, cu);
15583
15584 /* The type's CU may not be the same as CU.
15585 Ensure TYPE is recorded with CU in die_type_hash. */
15586 return set_die_type (die, type, cu);
15587 }
15588
15589 type = alloc_type (objfile);
15590 INIT_CPLUS_SPECIFIC (type);
15591
15592 name = dwarf2_name (die, cu);
15593 if (name != NULL)
15594 {
15595 if (cu->per_cu->lang == language_cplus
15596 || cu->per_cu->lang == language_d
15597 || cu->per_cu->lang == language_rust)
15598 {
15599 const char *full_name = dwarf2_full_name (name, die, cu);
15600
15601 /* dwarf2_full_name might have already finished building the DIE's
15602 type. If so, there is no need to continue. */
15603 if (get_die_type (die, cu) != NULL)
15604 return get_die_type (die, cu);
15605
15606 type->set_name (full_name);
15607 }
15608 else
15609 {
15610 /* The name is already allocated along with this objfile, so
15611 we don't need to duplicate it for the type. */
15612 type->set_name (name);
15613 }
15614 }
15615
15616 if (die->tag == DW_TAG_structure_type)
15617 {
15618 type->set_code (TYPE_CODE_STRUCT);
15619 }
15620 else if (die->tag == DW_TAG_union_type)
15621 {
15622 type->set_code (TYPE_CODE_UNION);
15623 }
15624 else
15625 {
15626 type->set_code (TYPE_CODE_STRUCT);
15627 }
15628
15629 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
15630 type->set_is_declared_class (true);
15631
15632 /* Store the calling convention in the type if it's available in
15633 the die. Otherwise the calling convention remains set to
15634 the default value DW_CC_normal. */
15635 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15636 if (attr != nullptr
15637 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15638 {
15639 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15640 TYPE_CPLUS_CALLING_CONVENTION (type)
15641 = (enum dwarf_calling_convention) (attr->constant_value (0));
15642 }
15643
15644 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15645 if (attr != nullptr)
15646 {
15647 if (attr->form_is_constant ())
15648 TYPE_LENGTH (type) = attr->constant_value (0);
15649 else
15650 {
15651 struct dynamic_prop prop;
15652 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15653 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15654 TYPE_LENGTH (type) = 0;
15655 }
15656 }
15657 else
15658 {
15659 TYPE_LENGTH (type) = 0;
15660 }
15661
15662 maybe_set_alignment (cu, die, type);
15663
15664 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15665 {
15666 /* ICC<14 does not output the required DW_AT_declaration on
15667 incomplete types, but gives them a size of zero. */
15668 type->set_is_stub (true);
15669 }
15670 else
15671 type->set_stub_is_supported (true);
15672
15673 if (die_is_declaration (die, cu))
15674 type->set_is_stub (true);
15675 else if (attr == NULL && die->child == NULL
15676 && producer_is_realview (cu->producer))
15677 /* RealView does not output the required DW_AT_declaration
15678 on incomplete types. */
15679 type->set_is_stub (true);
15680
15681 /* We need to add the type field to the die immediately so we don't
15682 infinitely recurse when dealing with pointers to the structure
15683 type within the structure itself. */
15684 set_die_type (die, type, cu);
15685
15686 /* set_die_type should be already done. */
15687 set_descriptive_type (type, die, cu);
15688
15689 return type;
15690 }
15691
15692 static void handle_struct_member_die
15693 (struct die_info *child_die,
15694 struct type *type,
15695 struct field_info *fi,
15696 std::vector<struct symbol *> *template_args,
15697 struct dwarf2_cu *cu);
15698
15699 /* A helper for handle_struct_member_die that handles
15700 DW_TAG_variant_part. */
15701
15702 static void
15703 handle_variant_part (struct die_info *die, struct type *type,
15704 struct field_info *fi,
15705 std::vector<struct symbol *> *template_args,
15706 struct dwarf2_cu *cu)
15707 {
15708 variant_part_builder *new_part;
15709 if (fi->current_variant_part == nullptr)
15710 {
15711 fi->variant_parts.emplace_back ();
15712 new_part = &fi->variant_parts.back ();
15713 }
15714 else if (!fi->current_variant_part->processing_variant)
15715 {
15716 complaint (_("nested DW_TAG_variant_part seen "
15717 "- DIE at %s [in module %s]"),
15718 sect_offset_str (die->sect_off),
15719 objfile_name (cu->per_objfile->objfile));
15720 return;
15721 }
15722 else
15723 {
15724 variant_field &current = fi->current_variant_part->variants.back ();
15725 current.variant_parts.emplace_back ();
15726 new_part = &current.variant_parts.back ();
15727 }
15728
15729 /* When we recurse, we want callees to add to this new variant
15730 part. */
15731 scoped_restore save_current_variant_part
15732 = make_scoped_restore (&fi->current_variant_part, new_part);
15733
15734 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15735 if (discr == NULL)
15736 {
15737 /* It's a univariant form, an extension we support. */
15738 }
15739 else if (discr->form_is_ref ())
15740 {
15741 struct dwarf2_cu *target_cu = cu;
15742 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15743
15744 new_part->discriminant_offset = target_die->sect_off;
15745 }
15746 else
15747 {
15748 complaint (_("DW_AT_discr does not have DIE reference form"
15749 " - DIE at %s [in module %s]"),
15750 sect_offset_str (die->sect_off),
15751 objfile_name (cu->per_objfile->objfile));
15752 }
15753
15754 for (die_info *child_die = die->child;
15755 child_die != NULL;
15756 child_die = child_die->sibling)
15757 handle_struct_member_die (child_die, type, fi, template_args, cu);
15758 }
15759
15760 /* A helper for handle_struct_member_die that handles
15761 DW_TAG_variant. */
15762
15763 static void
15764 handle_variant (struct die_info *die, struct type *type,
15765 struct field_info *fi,
15766 std::vector<struct symbol *> *template_args,
15767 struct dwarf2_cu *cu)
15768 {
15769 if (fi->current_variant_part == nullptr)
15770 {
15771 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15772 "- DIE at %s [in module %s]"),
15773 sect_offset_str (die->sect_off),
15774 objfile_name (cu->per_objfile->objfile));
15775 return;
15776 }
15777 if (fi->current_variant_part->processing_variant)
15778 {
15779 complaint (_("nested DW_TAG_variant seen "
15780 "- DIE at %s [in module %s]"),
15781 sect_offset_str (die->sect_off),
15782 objfile_name (cu->per_objfile->objfile));
15783 return;
15784 }
15785
15786 scoped_restore save_processing_variant
15787 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15788 true);
15789
15790 fi->current_variant_part->variants.emplace_back ();
15791 variant_field &variant = fi->current_variant_part->variants.back ();
15792 variant.first_field = fi->fields.size ();
15793
15794 /* In a variant we want to get the discriminant and also add a
15795 field for our sole member child. */
15796 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15797 if (discr == nullptr || !discr->form_is_constant ())
15798 {
15799 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15800 if (discr == nullptr || discr->as_block ()->size == 0)
15801 variant.default_branch = true;
15802 else
15803 variant.discr_list_data = discr->as_block ();
15804 }
15805 else
15806 variant.discriminant_value = discr->constant_value (0);
15807
15808 for (die_info *variant_child = die->child;
15809 variant_child != NULL;
15810 variant_child = variant_child->sibling)
15811 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15812
15813 variant.last_field = fi->fields.size ();
15814 }
15815
15816 /* A helper for process_structure_scope that handles a single member
15817 DIE. */
15818
15819 static void
15820 handle_struct_member_die (struct die_info *child_die, struct type *type,
15821 struct field_info *fi,
15822 std::vector<struct symbol *> *template_args,
15823 struct dwarf2_cu *cu)
15824 {
15825 if (child_die->tag == DW_TAG_member
15826 || child_die->tag == DW_TAG_variable)
15827 {
15828 /* NOTE: carlton/2002-11-05: A C++ static data member
15829 should be a DW_TAG_member that is a declaration, but
15830 all versions of G++ as of this writing (so through at
15831 least 3.2.1) incorrectly generate DW_TAG_variable
15832 tags for them instead. */
15833 dwarf2_add_field (fi, child_die, cu);
15834 }
15835 else if (child_die->tag == DW_TAG_subprogram)
15836 {
15837 /* Rust doesn't have member functions in the C++ sense.
15838 However, it does emit ordinary functions as children
15839 of a struct DIE. */
15840 if (cu->per_cu->lang == language_rust)
15841 read_func_scope (child_die, cu);
15842 else
15843 {
15844 /* C++ member function. */
15845 dwarf2_add_member_fn (fi, child_die, type, cu);
15846 }
15847 }
15848 else if (child_die->tag == DW_TAG_inheritance)
15849 {
15850 /* C++ base class field. */
15851 dwarf2_add_field (fi, child_die, cu);
15852 }
15853 else if (type_can_define_types (child_die))
15854 dwarf2_add_type_defn (fi, child_die, cu);
15855 else if (child_die->tag == DW_TAG_template_type_param
15856 || child_die->tag == DW_TAG_template_value_param)
15857 {
15858 struct symbol *arg = new_symbol (child_die, NULL, cu);
15859
15860 if (arg != NULL)
15861 template_args->push_back (arg);
15862 }
15863 else if (child_die->tag == DW_TAG_variant_part)
15864 handle_variant_part (child_die, type, fi, template_args, cu);
15865 else if (child_die->tag == DW_TAG_variant)
15866 handle_variant (child_die, type, fi, template_args, cu);
15867 }
15868
15869 /* Finish creating a structure or union type, including filling in
15870 its members and creating a symbol for it. */
15871
15872 static void
15873 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15874 {
15875 struct objfile *objfile = cu->per_objfile->objfile;
15876 struct die_info *child_die;
15877 struct type *type;
15878
15879 type = get_die_type (die, cu);
15880 if (type == NULL)
15881 type = read_structure_type (die, cu);
15882
15883 bool has_template_parameters = false;
15884 if (die->child != NULL && ! die_is_declaration (die, cu))
15885 {
15886 struct field_info fi;
15887 std::vector<struct symbol *> template_args;
15888
15889 child_die = die->child;
15890
15891 while (child_die && child_die->tag)
15892 {
15893 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15894 child_die = child_die->sibling;
15895 }
15896
15897 /* Attach template arguments to type. */
15898 if (!template_args.empty ())
15899 {
15900 has_template_parameters = true;
15901 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15902 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15903 TYPE_TEMPLATE_ARGUMENTS (type)
15904 = XOBNEWVEC (&objfile->objfile_obstack,
15905 struct symbol *,
15906 TYPE_N_TEMPLATE_ARGUMENTS (type));
15907 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15908 template_args.data (),
15909 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15910 * sizeof (struct symbol *)));
15911 }
15912
15913 /* Attach fields and member functions to the type. */
15914 if (fi.nfields () > 0)
15915 dwarf2_attach_fields_to_type (&fi, type, cu);
15916 if (!fi.fnfieldlists.empty ())
15917 {
15918 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15919
15920 /* Get the type which refers to the base class (possibly this
15921 class itself) which contains the vtable pointer for the current
15922 class from the DW_AT_containing_type attribute. This use of
15923 DW_AT_containing_type is a GNU extension. */
15924
15925 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15926 {
15927 struct type *t = die_containing_type (die, cu);
15928
15929 set_type_vptr_basetype (type, t);
15930 if (type == t)
15931 {
15932 int i;
15933
15934 /* Our own class provides vtbl ptr. */
15935 for (i = t->num_fields () - 1;
15936 i >= TYPE_N_BASECLASSES (t);
15937 --i)
15938 {
15939 const char *fieldname = t->field (i).name ();
15940
15941 if (is_vtable_name (fieldname, cu))
15942 {
15943 set_type_vptr_fieldno (type, i);
15944 break;
15945 }
15946 }
15947
15948 /* Complain if virtual function table field not found. */
15949 if (i < TYPE_N_BASECLASSES (t))
15950 complaint (_("virtual function table pointer "
15951 "not found when defining class '%s'"),
15952 type->name () ? type->name () : "");
15953 }
15954 else
15955 {
15956 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15957 }
15958 }
15959 else if (cu->producer
15960 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15961 {
15962 /* The IBM XLC compiler does not provide direct indication
15963 of the containing type, but the vtable pointer is
15964 always named __vfp. */
15965
15966 int i;
15967
15968 for (i = type->num_fields () - 1;
15969 i >= TYPE_N_BASECLASSES (type);
15970 --i)
15971 {
15972 if (strcmp (type->field (i).name (), "__vfp") == 0)
15973 {
15974 set_type_vptr_fieldno (type, i);
15975 set_type_vptr_basetype (type, type);
15976 break;
15977 }
15978 }
15979 }
15980 }
15981
15982 /* Copy fi.typedef_field_list linked list elements content into the
15983 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15984 if (!fi.typedef_field_list.empty ())
15985 {
15986 int count = fi.typedef_field_list.size ();
15987
15988 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15989 TYPE_TYPEDEF_FIELD_ARRAY (type)
15990 = ((struct decl_field *)
15991 TYPE_ALLOC (type,
15992 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15993 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15994
15995 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15996 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15997 }
15998
15999 /* Copy fi.nested_types_list linked list elements content into the
16000 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16001 if (!fi.nested_types_list.empty ()
16002 && cu->per_cu->lang != language_ada)
16003 {
16004 int count = fi.nested_types_list.size ();
16005
16006 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16007 TYPE_NESTED_TYPES_ARRAY (type)
16008 = ((struct decl_field *)
16009 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16010 TYPE_NESTED_TYPES_COUNT (type) = count;
16011
16012 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16013 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16014 }
16015 }
16016
16017 quirk_gcc_member_function_pointer (type, objfile);
16018 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
16019 cu->rust_unions.push_back (type);
16020 else if (cu->per_cu->lang == language_ada)
16021 quirk_ada_thick_pointer_struct (die, cu, type);
16022
16023 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16024 snapshots) has been known to create a die giving a declaration
16025 for a class that has, as a child, a die giving a definition for a
16026 nested class. So we have to process our children even if the
16027 current die is a declaration. Normally, of course, a declaration
16028 won't have any children at all. */
16029
16030 child_die = die->child;
16031
16032 while (child_die != NULL && child_die->tag)
16033 {
16034 if (child_die->tag == DW_TAG_member
16035 || child_die->tag == DW_TAG_variable
16036 || child_die->tag == DW_TAG_inheritance
16037 || child_die->tag == DW_TAG_template_value_param
16038 || child_die->tag == DW_TAG_template_type_param)
16039 {
16040 /* Do nothing. */
16041 }
16042 else
16043 process_die (child_die, cu);
16044
16045 child_die = child_die->sibling;
16046 }
16047
16048 /* Do not consider external references. According to the DWARF standard,
16049 these DIEs are identified by the fact that they have no byte_size
16050 attribute, and a declaration attribute. */
16051 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16052 || !die_is_declaration (die, cu)
16053 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16054 {
16055 struct symbol *sym = new_symbol (die, type, cu);
16056
16057 if (has_template_parameters)
16058 {
16059 struct symtab *symtab;
16060 if (sym != nullptr)
16061 symtab = symbol_symtab (sym);
16062 else if (cu->line_header != nullptr)
16063 {
16064 /* Any related symtab will do. */
16065 symtab
16066 = cu->line_header->file_names ()[0].symtab;
16067 }
16068 else
16069 {
16070 symtab = nullptr;
16071 complaint (_("could not find suitable "
16072 "symtab for template parameter"
16073 " - DIE at %s [in module %s]"),
16074 sect_offset_str (die->sect_off),
16075 objfile_name (objfile));
16076 }
16077
16078 if (symtab != nullptr)
16079 {
16080 /* Make sure that the symtab is set on the new symbols.
16081 Even though they don't appear in this symtab directly,
16082 other parts of gdb assume that symbols do, and this is
16083 reasonably true. */
16084 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16085 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16086 }
16087 }
16088 }
16089 }
16090
16091 /* Assuming DIE is an enumeration type, and TYPE is its associated
16092 type, update TYPE using some information only available in DIE's
16093 children. In particular, the fields are computed. */
16094
16095 static void
16096 update_enumeration_type_from_children (struct die_info *die,
16097 struct type *type,
16098 struct dwarf2_cu *cu)
16099 {
16100 struct die_info *child_die;
16101 int unsigned_enum = 1;
16102 int flag_enum = 1;
16103
16104 auto_obstack obstack;
16105 std::vector<struct field> fields;
16106
16107 for (child_die = die->child;
16108 child_die != NULL && child_die->tag;
16109 child_die = child_die->sibling)
16110 {
16111 struct attribute *attr;
16112 LONGEST value;
16113 const gdb_byte *bytes;
16114 struct dwarf2_locexpr_baton *baton;
16115 const char *name;
16116
16117 if (child_die->tag != DW_TAG_enumerator)
16118 continue;
16119
16120 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16121 if (attr == NULL)
16122 continue;
16123
16124 name = dwarf2_name (child_die, cu);
16125 if (name == NULL)
16126 name = "<anonymous enumerator>";
16127
16128 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16129 &value, &bytes, &baton);
16130 if (value < 0)
16131 {
16132 unsigned_enum = 0;
16133 flag_enum = 0;
16134 }
16135 else
16136 {
16137 if (count_one_bits_ll (value) >= 2)
16138 flag_enum = 0;
16139 }
16140
16141 fields.emplace_back ();
16142 struct field &field = fields.back ();
16143 field.set_name (dwarf2_physname (name, child_die, cu));
16144 field.set_loc_enumval (value);
16145 }
16146
16147 if (!fields.empty ())
16148 {
16149 type->set_num_fields (fields.size ());
16150 type->set_fields
16151 ((struct field *)
16152 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16153 memcpy (type->fields (), fields.data (),
16154 sizeof (struct field) * fields.size ());
16155 }
16156
16157 if (unsigned_enum)
16158 type->set_is_unsigned (true);
16159
16160 if (flag_enum)
16161 type->set_is_flag_enum (true);
16162 }
16163
16164 /* Given a DW_AT_enumeration_type die, set its type. We do not
16165 complete the type's fields yet, or create any symbols. */
16166
16167 static struct type *
16168 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16169 {
16170 struct objfile *objfile = cu->per_objfile->objfile;
16171 struct type *type;
16172 struct attribute *attr;
16173 const char *name;
16174
16175 /* If the definition of this type lives in .debug_types, read that type.
16176 Don't follow DW_AT_specification though, that will take us back up
16177 the chain and we want to go down. */
16178 attr = die->attr (DW_AT_signature);
16179 if (attr != nullptr)
16180 {
16181 type = get_DW_AT_signature_type (die, attr, cu);
16182
16183 /* The type's CU may not be the same as CU.
16184 Ensure TYPE is recorded with CU in die_type_hash. */
16185 return set_die_type (die, type, cu);
16186 }
16187
16188 type = alloc_type (objfile);
16189
16190 type->set_code (TYPE_CODE_ENUM);
16191 name = dwarf2_full_name (NULL, die, cu);
16192 if (name != NULL)
16193 type->set_name (name);
16194
16195 attr = dwarf2_attr (die, DW_AT_type, cu);
16196 if (attr != NULL)
16197 {
16198 struct type *underlying_type = die_type (die, cu);
16199
16200 TYPE_TARGET_TYPE (type) = underlying_type;
16201 }
16202
16203 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16204 if (attr != nullptr)
16205 {
16206 TYPE_LENGTH (type) = attr->constant_value (0);
16207 }
16208 else
16209 {
16210 TYPE_LENGTH (type) = 0;
16211 }
16212
16213 maybe_set_alignment (cu, die, type);
16214
16215 /* The enumeration DIE can be incomplete. In Ada, any type can be
16216 declared as private in the package spec, and then defined only
16217 inside the package body. Such types are known as Taft Amendment
16218 Types. When another package uses such a type, an incomplete DIE
16219 may be generated by the compiler. */
16220 if (die_is_declaration (die, cu))
16221 type->set_is_stub (true);
16222
16223 /* If this type has an underlying type that is not a stub, then we
16224 may use its attributes. We always use the "unsigned" attribute
16225 in this situation, because ordinarily we guess whether the type
16226 is unsigned -- but the guess can be wrong and the underlying type
16227 can tell us the reality. However, we defer to a local size
16228 attribute if one exists, because this lets the compiler override
16229 the underlying type if needed. */
16230 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16231 {
16232 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16233 underlying_type = check_typedef (underlying_type);
16234
16235 type->set_is_unsigned (underlying_type->is_unsigned ());
16236
16237 if (TYPE_LENGTH (type) == 0)
16238 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16239
16240 if (TYPE_RAW_ALIGN (type) == 0
16241 && TYPE_RAW_ALIGN (underlying_type) != 0)
16242 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16243 }
16244
16245 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16246
16247 set_die_type (die, type, cu);
16248
16249 /* Finish the creation of this type by using the enum's children.
16250 Note that, as usual, this must come after set_die_type to avoid
16251 infinite recursion when trying to compute the names of the
16252 enumerators. */
16253 update_enumeration_type_from_children (die, type, cu);
16254
16255 return type;
16256 }
16257
16258 /* Given a pointer to a die which begins an enumeration, process all
16259 the dies that define the members of the enumeration, and create the
16260 symbol for the enumeration type.
16261
16262 NOTE: We reverse the order of the element list. */
16263
16264 static void
16265 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16266 {
16267 struct type *this_type;
16268
16269 this_type = get_die_type (die, cu);
16270 if (this_type == NULL)
16271 this_type = read_enumeration_type (die, cu);
16272
16273 if (die->child != NULL)
16274 {
16275 struct die_info *child_die;
16276 const char *name;
16277
16278 child_die = die->child;
16279 while (child_die && child_die->tag)
16280 {
16281 if (child_die->tag != DW_TAG_enumerator)
16282 {
16283 process_die (child_die, cu);
16284 }
16285 else
16286 {
16287 name = dwarf2_name (child_die, cu);
16288 if (name)
16289 new_symbol (child_die, this_type, cu);
16290 }
16291
16292 child_die = child_die->sibling;
16293 }
16294 }
16295
16296 /* If we are reading an enum from a .debug_types unit, and the enum
16297 is a declaration, and the enum is not the signatured type in the
16298 unit, then we do not want to add a symbol for it. Adding a
16299 symbol would in some cases obscure the true definition of the
16300 enum, giving users an incomplete type when the definition is
16301 actually available. Note that we do not want to do this for all
16302 enums which are just declarations, because C++0x allows forward
16303 enum declarations. */
16304 if (cu->per_cu->is_debug_types
16305 && die_is_declaration (die, cu))
16306 {
16307 struct signatured_type *sig_type;
16308
16309 sig_type = (struct signatured_type *) cu->per_cu;
16310 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16311 if (sig_type->type_offset_in_section != die->sect_off)
16312 return;
16313 }
16314
16315 new_symbol (die, this_type, cu);
16316 }
16317
16318 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16319 expression for an index type and finds the corresponding field
16320 offset in the hidden "P_BOUNDS" structure. Returns true on success
16321 and updates *FIELD, false if it fails to recognize an
16322 expression. */
16323
16324 static bool
16325 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16326 int *bounds_offset, struct field *field,
16327 struct dwarf2_cu *cu)
16328 {
16329 struct attribute *attr = dwarf2_attr (die, name, cu);
16330 if (attr == nullptr || !attr->form_is_block ())
16331 return false;
16332
16333 const struct dwarf_block *block = attr->as_block ();
16334 const gdb_byte *start = block->data;
16335 const gdb_byte *end = block->data + block->size;
16336
16337 /* The expression to recognize generally looks like:
16338
16339 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16340 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16341
16342 However, the second "plus_uconst" may be missing:
16343
16344 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16345 DW_OP_deref_size: 4)
16346
16347 This happens when the field is at the start of the structure.
16348
16349 Also, the final deref may not be sized:
16350
16351 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16352 DW_OP_deref)
16353
16354 This happens when the size of the index type happens to be the
16355 same as the architecture's word size. This can occur with or
16356 without the second plus_uconst. */
16357
16358 if (end - start < 2)
16359 return false;
16360 if (*start++ != DW_OP_push_object_address)
16361 return false;
16362 if (*start++ != DW_OP_plus_uconst)
16363 return false;
16364
16365 uint64_t this_bound_off;
16366 start = gdb_read_uleb128 (start, end, &this_bound_off);
16367 if (start == nullptr || (int) this_bound_off != this_bound_off)
16368 return false;
16369 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16370 is consistent among all bounds. */
16371 if (*bounds_offset == -1)
16372 *bounds_offset = this_bound_off;
16373 else if (*bounds_offset != this_bound_off)
16374 return false;
16375
16376 if (start == end || *start++ != DW_OP_deref)
16377 return false;
16378
16379 int offset = 0;
16380 if (start ==end)
16381 return false;
16382 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16383 {
16384 /* This means an offset of 0. */
16385 }
16386 else if (*start++ != DW_OP_plus_uconst)
16387 return false;
16388 else
16389 {
16390 /* The size is the parameter to DW_OP_plus_uconst. */
16391 uint64_t val;
16392 start = gdb_read_uleb128 (start, end, &val);
16393 if (start == nullptr)
16394 return false;
16395 if ((int) val != val)
16396 return false;
16397 offset = val;
16398 }
16399
16400 if (start == end)
16401 return false;
16402
16403 uint64_t size;
16404 if (*start == DW_OP_deref_size)
16405 {
16406 start = gdb_read_uleb128 (start + 1, end, &size);
16407 if (start == nullptr)
16408 return false;
16409 }
16410 else if (*start == DW_OP_deref)
16411 {
16412 size = cu->header.addr_size;
16413 ++start;
16414 }
16415 else
16416 return false;
16417
16418 field->set_loc_bitpos (8 * offset);
16419 if (size != TYPE_LENGTH (field->type ()))
16420 FIELD_BITSIZE (*field) = 8 * size;
16421
16422 return true;
16423 }
16424
16425 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16426 some kinds of Ada arrays:
16427
16428 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16429 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16430 <11e0> DW_AT_data_location: 2 byte block: 97 6
16431 (DW_OP_push_object_address; DW_OP_deref)
16432 <11e3> DW_AT_type : <0x1173>
16433 <11e7> DW_AT_sibling : <0x1201>
16434 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16435 <11ec> DW_AT_type : <0x1206>
16436 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16437 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16438 DW_OP_deref_size: 4)
16439 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16440 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16441 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16442
16443 This actually represents a "thick pointer", which is a structure
16444 with two elements: one that is a pointer to the array data, and one
16445 that is a pointer to another structure; this second structure holds
16446 the array bounds.
16447
16448 This returns a new type on success, or nullptr if this didn't
16449 recognize the type. */
16450
16451 static struct type *
16452 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16453 struct type *type)
16454 {
16455 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16456 /* So far we've only seen this with block form. */
16457 if (attr == nullptr || !attr->form_is_block ())
16458 return nullptr;
16459
16460 /* Note that this will fail if the structure layout is changed by
16461 the compiler. However, we have no good way to recognize some
16462 other layout, because we don't know what expression the compiler
16463 might choose to emit should this happen. */
16464 struct dwarf_block *blk = attr->as_block ();
16465 if (blk->size != 2
16466 || blk->data[0] != DW_OP_push_object_address
16467 || blk->data[1] != DW_OP_deref)
16468 return nullptr;
16469
16470 int bounds_offset = -1;
16471 int max_align = -1;
16472 std::vector<struct field> range_fields;
16473 for (struct die_info *child_die = die->child;
16474 child_die;
16475 child_die = child_die->sibling)
16476 {
16477 if (child_die->tag == DW_TAG_subrange_type)
16478 {
16479 struct type *underlying = read_subrange_index_type (child_die, cu);
16480
16481 int this_align = type_align (underlying);
16482 if (this_align > max_align)
16483 max_align = this_align;
16484
16485 range_fields.emplace_back ();
16486 range_fields.emplace_back ();
16487
16488 struct field &lower = range_fields[range_fields.size () - 2];
16489 struct field &upper = range_fields[range_fields.size () - 1];
16490
16491 lower.set_type (underlying);
16492 FIELD_ARTIFICIAL (lower) = 1;
16493
16494 upper.set_type (underlying);
16495 FIELD_ARTIFICIAL (upper) = 1;
16496
16497 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16498 &bounds_offset, &lower, cu)
16499 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16500 &bounds_offset, &upper, cu))
16501 return nullptr;
16502 }
16503 }
16504
16505 /* This shouldn't really happen, but double-check that we found
16506 where the bounds are stored. */
16507 if (bounds_offset == -1)
16508 return nullptr;
16509
16510 struct objfile *objfile = cu->per_objfile->objfile;
16511 for (int i = 0; i < range_fields.size (); i += 2)
16512 {
16513 char name[20];
16514
16515 /* Set the name of each field in the bounds. */
16516 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16517 range_fields[i].set_name (objfile->intern (name));
16518 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16519 range_fields[i + 1].set_name (objfile->intern (name));
16520 }
16521
16522 struct type *bounds = alloc_type (objfile);
16523 bounds->set_code (TYPE_CODE_STRUCT);
16524
16525 bounds->set_num_fields (range_fields.size ());
16526 bounds->set_fields
16527 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16528 * sizeof (struct field))));
16529 memcpy (bounds->fields (), range_fields.data (),
16530 bounds->num_fields () * sizeof (struct field));
16531
16532 int last_fieldno = range_fields.size () - 1;
16533 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
16534 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16535 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16536
16537 /* Rewrite the existing array type in place. Specifically, we
16538 remove any dynamic properties we might have read, and we replace
16539 the index types. */
16540 struct type *iter = type;
16541 for (int i = 0; i < range_fields.size (); i += 2)
16542 {
16543 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16544 iter->main_type->dyn_prop_list = nullptr;
16545 iter->set_index_type
16546 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16547 iter = TYPE_TARGET_TYPE (iter);
16548 }
16549
16550 struct type *result = alloc_type (objfile);
16551 result->set_code (TYPE_CODE_STRUCT);
16552
16553 result->set_num_fields (2);
16554 result->set_fields
16555 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16556 * sizeof (struct field))));
16557
16558 /* The names are chosen to coincide with what the compiler does with
16559 -fgnat-encodings=all, which the Ada code in gdb already
16560 understands. */
16561 result->field (0).set_name ("P_ARRAY");
16562 result->field (0).set_type (lookup_pointer_type (type));
16563
16564 result->field (1).set_name ("P_BOUNDS");
16565 result->field (1).set_type (lookup_pointer_type (bounds));
16566 result->field (1).set_loc_bitpos (8 * bounds_offset);
16567
16568 result->set_name (type->name ());
16569 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16570 + TYPE_LENGTH (result->field (1).type ()));
16571
16572 return result;
16573 }
16574
16575 /* Extract all information from a DW_TAG_array_type DIE and put it in
16576 the DIE's type field. For now, this only handles one dimensional
16577 arrays. */
16578
16579 static struct type *
16580 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16581 {
16582 struct objfile *objfile = cu->per_objfile->objfile;
16583 struct die_info *child_die;
16584 struct type *type;
16585 struct type *element_type, *range_type, *index_type;
16586 struct attribute *attr;
16587 const char *name;
16588 struct dynamic_prop *byte_stride_prop = NULL;
16589 unsigned int bit_stride = 0;
16590
16591 element_type = die_type (die, cu);
16592
16593 /* The die_type call above may have already set the type for this DIE. */
16594 type = get_die_type (die, cu);
16595 if (type)
16596 return type;
16597
16598 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16599 if (attr != NULL)
16600 {
16601 int stride_ok;
16602 struct type *prop_type = cu->addr_sized_int_type (false);
16603
16604 byte_stride_prop
16605 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16606 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16607 prop_type);
16608 if (!stride_ok)
16609 {
16610 complaint (_("unable to read array DW_AT_byte_stride "
16611 " - DIE at %s [in module %s]"),
16612 sect_offset_str (die->sect_off),
16613 objfile_name (cu->per_objfile->objfile));
16614 /* Ignore this attribute. We will likely not be able to print
16615 arrays of this type correctly, but there is little we can do
16616 to help if we cannot read the attribute's value. */
16617 byte_stride_prop = NULL;
16618 }
16619 }
16620
16621 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16622 if (attr != NULL)
16623 bit_stride = attr->constant_value (0);
16624
16625 /* Irix 6.2 native cc creates array types without children for
16626 arrays with unspecified length. */
16627 if (die->child == NULL)
16628 {
16629 index_type = objfile_type (objfile)->builtin_int;
16630 range_type = create_static_range_type (NULL, index_type, 0, -1);
16631 type = create_array_type_with_stride (NULL, element_type, range_type,
16632 byte_stride_prop, bit_stride);
16633 return set_die_type (die, type, cu);
16634 }
16635
16636 std::vector<struct type *> range_types;
16637 child_die = die->child;
16638 while (child_die && child_die->tag)
16639 {
16640 if (child_die->tag == DW_TAG_subrange_type)
16641 {
16642 struct type *child_type = read_type_die (child_die, cu);
16643
16644 if (child_type != NULL)
16645 {
16646 /* The range type was succesfully read. Save it for the
16647 array type creation. */
16648 range_types.push_back (child_type);
16649 }
16650 }
16651 child_die = child_die->sibling;
16652 }
16653
16654 if (range_types.empty ())
16655 {
16656 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16657 sect_offset_str (die->sect_off),
16658 objfile_name (cu->per_objfile->objfile));
16659 return NULL;
16660 }
16661
16662 /* Dwarf2 dimensions are output from left to right, create the
16663 necessary array types in backwards order. */
16664
16665 type = element_type;
16666
16667 if (read_array_order (die, cu) == DW_ORD_col_major)
16668 {
16669 int i = 0;
16670
16671 while (i < range_types.size ())
16672 {
16673 type = create_array_type_with_stride (NULL, type, range_types[i++],
16674 byte_stride_prop, bit_stride);
16675 bit_stride = 0;
16676 byte_stride_prop = nullptr;
16677 }
16678 }
16679 else
16680 {
16681 size_t ndim = range_types.size ();
16682 while (ndim-- > 0)
16683 {
16684 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16685 byte_stride_prop, bit_stride);
16686 bit_stride = 0;
16687 byte_stride_prop = nullptr;
16688 }
16689 }
16690
16691 gdb_assert (type != element_type);
16692
16693 /* Understand Dwarf2 support for vector types (like they occur on
16694 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16695 array type. This is not part of the Dwarf2/3 standard yet, but a
16696 custom vendor extension. The main difference between a regular
16697 array and the vector variant is that vectors are passed by value
16698 to functions. */
16699 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16700 if (attr != nullptr)
16701 make_vector_type (type);
16702
16703 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16704 implementation may choose to implement triple vectors using this
16705 attribute. */
16706 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16707 if (attr != nullptr && attr->form_is_unsigned ())
16708 {
16709 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16710 TYPE_LENGTH (type) = attr->as_unsigned ();
16711 else
16712 complaint (_("DW_AT_byte_size for array type smaller "
16713 "than the total size of elements"));
16714 }
16715
16716 name = dwarf2_name (die, cu);
16717 if (name)
16718 type->set_name (name);
16719
16720 maybe_set_alignment (cu, die, type);
16721
16722 struct type *replacement_type = nullptr;
16723 if (cu->per_cu->lang == language_ada)
16724 {
16725 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16726 if (replacement_type != nullptr)
16727 type = replacement_type;
16728 }
16729
16730 /* Install the type in the die. */
16731 set_die_type (die, type, cu, replacement_type != nullptr);
16732
16733 /* set_die_type should be already done. */
16734 set_descriptive_type (type, die, cu);
16735
16736 return type;
16737 }
16738
16739 static enum dwarf_array_dim_ordering
16740 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16741 {
16742 struct attribute *attr;
16743
16744 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16745
16746 if (attr != nullptr)
16747 {
16748 LONGEST val = attr->constant_value (-1);
16749 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16750 return (enum dwarf_array_dim_ordering) val;
16751 }
16752
16753 /* GNU F77 is a special case, as at 08/2004 array type info is the
16754 opposite order to the dwarf2 specification, but data is still
16755 laid out as per normal fortran.
16756
16757 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16758 version checking. */
16759
16760 if (cu->per_cu->lang == language_fortran
16761 && cu->producer && strstr (cu->producer, "GNU F77"))
16762 {
16763 return DW_ORD_row_major;
16764 }
16765
16766 switch (cu->language_defn->array_ordering ())
16767 {
16768 case array_column_major:
16769 return DW_ORD_col_major;
16770 case array_row_major:
16771 default:
16772 return DW_ORD_row_major;
16773 };
16774 }
16775
16776 /* Extract all information from a DW_TAG_set_type DIE and put it in
16777 the DIE's type field. */
16778
16779 static struct type *
16780 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16781 {
16782 struct type *domain_type, *set_type;
16783 struct attribute *attr;
16784
16785 domain_type = die_type (die, cu);
16786
16787 /* The die_type call above may have already set the type for this DIE. */
16788 set_type = get_die_type (die, cu);
16789 if (set_type)
16790 return set_type;
16791
16792 set_type = create_set_type (NULL, domain_type);
16793
16794 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16795 if (attr != nullptr && attr->form_is_unsigned ())
16796 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16797
16798 maybe_set_alignment (cu, die, set_type);
16799
16800 return set_die_type (die, set_type, cu);
16801 }
16802
16803 /* A helper for read_common_block that creates a locexpr baton.
16804 SYM is the symbol which we are marking as computed.
16805 COMMON_DIE is the DIE for the common block.
16806 COMMON_LOC is the location expression attribute for the common
16807 block itself.
16808 MEMBER_LOC is the location expression attribute for the particular
16809 member of the common block that we are processing.
16810 CU is the CU from which the above come. */
16811
16812 static void
16813 mark_common_block_symbol_computed (struct symbol *sym,
16814 struct die_info *common_die,
16815 struct attribute *common_loc,
16816 struct attribute *member_loc,
16817 struct dwarf2_cu *cu)
16818 {
16819 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16820 struct objfile *objfile = per_objfile->objfile;
16821 struct dwarf2_locexpr_baton *baton;
16822 gdb_byte *ptr;
16823 unsigned int cu_off;
16824 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16825 LONGEST offset = 0;
16826
16827 gdb_assert (common_loc && member_loc);
16828 gdb_assert (common_loc->form_is_block ());
16829 gdb_assert (member_loc->form_is_block ()
16830 || member_loc->form_is_constant ());
16831
16832 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16833 baton->per_objfile = per_objfile;
16834 baton->per_cu = cu->per_cu;
16835 gdb_assert (baton->per_cu);
16836
16837 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16838
16839 if (member_loc->form_is_constant ())
16840 {
16841 offset = member_loc->constant_value (0);
16842 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16843 }
16844 else
16845 baton->size += member_loc->as_block ()->size;
16846
16847 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16848 baton->data = ptr;
16849
16850 *ptr++ = DW_OP_call4;
16851 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16852 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16853 ptr += 4;
16854
16855 if (member_loc->form_is_constant ())
16856 {
16857 *ptr++ = DW_OP_addr;
16858 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16859 ptr += cu->header.addr_size;
16860 }
16861 else
16862 {
16863 /* We have to copy the data here, because DW_OP_call4 will only
16864 use a DW_AT_location attribute. */
16865 struct dwarf_block *block = member_loc->as_block ();
16866 memcpy (ptr, block->data, block->size);
16867 ptr += block->size;
16868 }
16869
16870 *ptr++ = DW_OP_plus;
16871 gdb_assert (ptr - baton->data == baton->size);
16872
16873 SYMBOL_LOCATION_BATON (sym) = baton;
16874 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16875 }
16876
16877 /* Create appropriate locally-scoped variables for all the
16878 DW_TAG_common_block entries. Also create a struct common_block
16879 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16880 is used to separate the common blocks name namespace from regular
16881 variable names. */
16882
16883 static void
16884 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16885 {
16886 struct attribute *attr;
16887
16888 attr = dwarf2_attr (die, DW_AT_location, cu);
16889 if (attr != nullptr)
16890 {
16891 /* Support the .debug_loc offsets. */
16892 if (attr->form_is_block ())
16893 {
16894 /* Ok. */
16895 }
16896 else if (attr->form_is_section_offset ())
16897 {
16898 dwarf2_complex_location_expr_complaint ();
16899 attr = NULL;
16900 }
16901 else
16902 {
16903 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16904 "common block member");
16905 attr = NULL;
16906 }
16907 }
16908
16909 if (die->child != NULL)
16910 {
16911 struct objfile *objfile = cu->per_objfile->objfile;
16912 struct die_info *child_die;
16913 size_t n_entries = 0, size;
16914 struct common_block *common_block;
16915 struct symbol *sym;
16916
16917 for (child_die = die->child;
16918 child_die && child_die->tag;
16919 child_die = child_die->sibling)
16920 ++n_entries;
16921
16922 size = (sizeof (struct common_block)
16923 + (n_entries - 1) * sizeof (struct symbol *));
16924 common_block
16925 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16926 size);
16927 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16928 common_block->n_entries = 0;
16929
16930 for (child_die = die->child;
16931 child_die && child_die->tag;
16932 child_die = child_die->sibling)
16933 {
16934 /* Create the symbol in the DW_TAG_common_block block in the current
16935 symbol scope. */
16936 sym = new_symbol (child_die, NULL, cu);
16937 if (sym != NULL)
16938 {
16939 struct attribute *member_loc;
16940
16941 common_block->contents[common_block->n_entries++] = sym;
16942
16943 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16944 cu);
16945 if (member_loc)
16946 {
16947 /* GDB has handled this for a long time, but it is
16948 not specified by DWARF. It seems to have been
16949 emitted by gfortran at least as recently as:
16950 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16951 complaint (_("Variable in common block has "
16952 "DW_AT_data_member_location "
16953 "- DIE at %s [in module %s]"),
16954 sect_offset_str (child_die->sect_off),
16955 objfile_name (objfile));
16956
16957 if (member_loc->form_is_section_offset ())
16958 dwarf2_complex_location_expr_complaint ();
16959 else if (member_loc->form_is_constant ()
16960 || member_loc->form_is_block ())
16961 {
16962 if (attr != nullptr)
16963 mark_common_block_symbol_computed (sym, die, attr,
16964 member_loc, cu);
16965 }
16966 else
16967 dwarf2_complex_location_expr_complaint ();
16968 }
16969 }
16970 }
16971
16972 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16973 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16974 }
16975 }
16976
16977 /* Create a type for a C++ namespace. */
16978
16979 static struct type *
16980 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16981 {
16982 struct objfile *objfile = cu->per_objfile->objfile;
16983 const char *previous_prefix, *name;
16984 int is_anonymous;
16985 struct type *type;
16986
16987 /* For extensions, reuse the type of the original namespace. */
16988 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16989 {
16990 struct die_info *ext_die;
16991 struct dwarf2_cu *ext_cu = cu;
16992
16993 ext_die = dwarf2_extension (die, &ext_cu);
16994 type = read_type_die (ext_die, ext_cu);
16995
16996 /* EXT_CU may not be the same as CU.
16997 Ensure TYPE is recorded with CU in die_type_hash. */
16998 return set_die_type (die, type, cu);
16999 }
17000
17001 name = namespace_name (die, &is_anonymous, cu);
17002
17003 /* Now build the name of the current namespace. */
17004
17005 previous_prefix = determine_prefix (die, cu);
17006 if (previous_prefix[0] != '\0')
17007 name = typename_concat (&objfile->objfile_obstack,
17008 previous_prefix, name, 0, cu);
17009
17010 /* Create the type. */
17011 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17012
17013 return set_die_type (die, type, cu);
17014 }
17015
17016 /* Read a namespace scope. */
17017
17018 static void
17019 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17020 {
17021 struct objfile *objfile = cu->per_objfile->objfile;
17022 int is_anonymous;
17023
17024 /* Add a symbol associated to this if we haven't seen the namespace
17025 before. Also, add a using directive if it's an anonymous
17026 namespace. */
17027
17028 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17029 {
17030 struct type *type;
17031
17032 type = read_type_die (die, cu);
17033 new_symbol (die, type, cu);
17034
17035 namespace_name (die, &is_anonymous, cu);
17036 if (is_anonymous)
17037 {
17038 const char *previous_prefix = determine_prefix (die, cu);
17039
17040 std::vector<const char *> excludes;
17041 add_using_directive (using_directives (cu),
17042 previous_prefix, type->name (), NULL,
17043 NULL, excludes, 0, &objfile->objfile_obstack);
17044 }
17045 }
17046
17047 if (die->child != NULL)
17048 {
17049 struct die_info *child_die = die->child;
17050
17051 while (child_die && child_die->tag)
17052 {
17053 process_die (child_die, cu);
17054 child_die = child_die->sibling;
17055 }
17056 }
17057 }
17058
17059 /* Read a Fortran module as type. This DIE can be only a declaration used for
17060 imported module. Still we need that type as local Fortran "use ... only"
17061 declaration imports depend on the created type in determine_prefix. */
17062
17063 static struct type *
17064 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17065 {
17066 struct objfile *objfile = cu->per_objfile->objfile;
17067 const char *module_name;
17068 struct type *type;
17069
17070 module_name = dwarf2_name (die, cu);
17071 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17072
17073 return set_die_type (die, type, cu);
17074 }
17075
17076 /* Read a Fortran module. */
17077
17078 static void
17079 read_module (struct die_info *die, struct dwarf2_cu *cu)
17080 {
17081 struct die_info *child_die = die->child;
17082 struct type *type;
17083
17084 type = read_type_die (die, cu);
17085 new_symbol (die, type, cu);
17086
17087 while (child_die && child_die->tag)
17088 {
17089 process_die (child_die, cu);
17090 child_die = child_die->sibling;
17091 }
17092 }
17093
17094 /* Return the name of the namespace represented by DIE. Set
17095 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17096 namespace. */
17097
17098 static const char *
17099 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17100 {
17101 struct die_info *current_die;
17102 const char *name = NULL;
17103
17104 /* Loop through the extensions until we find a name. */
17105
17106 for (current_die = die;
17107 current_die != NULL;
17108 current_die = dwarf2_extension (die, &cu))
17109 {
17110 /* We don't use dwarf2_name here so that we can detect the absence
17111 of a name -> anonymous namespace. */
17112 name = dwarf2_string_attr (die, DW_AT_name, cu);
17113
17114 if (name != NULL)
17115 break;
17116 }
17117
17118 /* Is it an anonymous namespace? */
17119
17120 *is_anonymous = (name == NULL);
17121 if (*is_anonymous)
17122 name = CP_ANONYMOUS_NAMESPACE_STR;
17123
17124 return name;
17125 }
17126
17127 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17128 the user defined type vector. */
17129
17130 static struct type *
17131 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17132 {
17133 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17134 struct comp_unit_head *cu_header = &cu->header;
17135 struct type *type;
17136 struct attribute *attr_byte_size;
17137 struct attribute *attr_address_class;
17138 int byte_size, addr_class;
17139 struct type *target_type;
17140
17141 target_type = die_type (die, cu);
17142
17143 /* The die_type call above may have already set the type for this DIE. */
17144 type = get_die_type (die, cu);
17145 if (type)
17146 return type;
17147
17148 type = lookup_pointer_type (target_type);
17149
17150 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17151 if (attr_byte_size)
17152 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17153 else
17154 byte_size = cu_header->addr_size;
17155
17156 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17157 if (attr_address_class)
17158 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17159 else
17160 addr_class = DW_ADDR_none;
17161
17162 ULONGEST alignment = get_alignment (cu, die);
17163
17164 /* If the pointer size, alignment, or address class is different
17165 than the default, create a type variant marked as such and set
17166 the length accordingly. */
17167 if (TYPE_LENGTH (type) != byte_size
17168 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17169 && alignment != TYPE_RAW_ALIGN (type))
17170 || addr_class != DW_ADDR_none)
17171 {
17172 if (gdbarch_address_class_type_flags_p (gdbarch))
17173 {
17174 type_instance_flags type_flags
17175 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17176 addr_class);
17177 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17178 == 0);
17179 type = make_type_with_address_space (type, type_flags);
17180 }
17181 else if (TYPE_LENGTH (type) != byte_size)
17182 {
17183 complaint (_("invalid pointer size %d"), byte_size);
17184 }
17185 else if (TYPE_RAW_ALIGN (type) != alignment)
17186 {
17187 complaint (_("Invalid DW_AT_alignment"
17188 " - DIE at %s [in module %s]"),
17189 sect_offset_str (die->sect_off),
17190 objfile_name (cu->per_objfile->objfile));
17191 }
17192 else
17193 {
17194 /* Should we also complain about unhandled address classes? */
17195 }
17196 }
17197
17198 TYPE_LENGTH (type) = byte_size;
17199 set_type_align (type, alignment);
17200 return set_die_type (die, type, cu);
17201 }
17202
17203 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17204 the user defined type vector. */
17205
17206 static struct type *
17207 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17208 {
17209 struct type *type;
17210 struct type *to_type;
17211 struct type *domain;
17212
17213 to_type = die_type (die, cu);
17214 domain = die_containing_type (die, cu);
17215
17216 /* The calls above may have already set the type for this DIE. */
17217 type = get_die_type (die, cu);
17218 if (type)
17219 return type;
17220
17221 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17222 type = lookup_methodptr_type (to_type);
17223 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17224 {
17225 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17226
17227 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17228 to_type->fields (), to_type->num_fields (),
17229 to_type->has_varargs ());
17230 type = lookup_methodptr_type (new_type);
17231 }
17232 else
17233 type = lookup_memberptr_type (to_type, domain);
17234
17235 return set_die_type (die, type, cu);
17236 }
17237
17238 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17239 the user defined type vector. */
17240
17241 static struct type *
17242 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17243 enum type_code refcode)
17244 {
17245 struct comp_unit_head *cu_header = &cu->header;
17246 struct type *type, *target_type;
17247 struct attribute *attr;
17248
17249 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17250
17251 target_type = die_type (die, cu);
17252
17253 /* The die_type call above may have already set the type for this DIE. */
17254 type = get_die_type (die, cu);
17255 if (type)
17256 return type;
17257
17258 type = lookup_reference_type (target_type, refcode);
17259 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17260 if (attr != nullptr)
17261 {
17262 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17263 }
17264 else
17265 {
17266 TYPE_LENGTH (type) = cu_header->addr_size;
17267 }
17268 maybe_set_alignment (cu, die, type);
17269 return set_die_type (die, type, cu);
17270 }
17271
17272 /* Add the given cv-qualifiers to the element type of the array. GCC
17273 outputs DWARF type qualifiers that apply to an array, not the
17274 element type. But GDB relies on the array element type to carry
17275 the cv-qualifiers. This mimics section 6.7.3 of the C99
17276 specification. */
17277
17278 static struct type *
17279 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17280 struct type *base_type, int cnst, int voltl)
17281 {
17282 struct type *el_type, *inner_array;
17283
17284 base_type = copy_type (base_type);
17285 inner_array = base_type;
17286
17287 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17288 {
17289 TYPE_TARGET_TYPE (inner_array) =
17290 copy_type (TYPE_TARGET_TYPE (inner_array));
17291 inner_array = TYPE_TARGET_TYPE (inner_array);
17292 }
17293
17294 el_type = TYPE_TARGET_TYPE (inner_array);
17295 cnst |= TYPE_CONST (el_type);
17296 voltl |= TYPE_VOLATILE (el_type);
17297 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17298
17299 return set_die_type (die, base_type, cu);
17300 }
17301
17302 static struct type *
17303 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17304 {
17305 struct type *base_type, *cv_type;
17306
17307 base_type = die_type (die, cu);
17308
17309 /* The die_type call above may have already set the type for this DIE. */
17310 cv_type = get_die_type (die, cu);
17311 if (cv_type)
17312 return cv_type;
17313
17314 /* In case the const qualifier is applied to an array type, the element type
17315 is so qualified, not the array type (section 6.7.3 of C99). */
17316 if (base_type->code () == TYPE_CODE_ARRAY)
17317 return add_array_cv_type (die, cu, base_type, 1, 0);
17318
17319 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17320 return set_die_type (die, cv_type, cu);
17321 }
17322
17323 static struct type *
17324 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17325 {
17326 struct type *base_type, *cv_type;
17327
17328 base_type = die_type (die, cu);
17329
17330 /* The die_type call above may have already set the type for this DIE. */
17331 cv_type = get_die_type (die, cu);
17332 if (cv_type)
17333 return cv_type;
17334
17335 /* In case the volatile qualifier is applied to an array type, the
17336 element type is so qualified, not the array type (section 6.7.3
17337 of C99). */
17338 if (base_type->code () == TYPE_CODE_ARRAY)
17339 return add_array_cv_type (die, cu, base_type, 0, 1);
17340
17341 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17342 return set_die_type (die, cv_type, cu);
17343 }
17344
17345 /* Handle DW_TAG_restrict_type. */
17346
17347 static struct type *
17348 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17349 {
17350 struct type *base_type, *cv_type;
17351
17352 base_type = die_type (die, cu);
17353
17354 /* The die_type call above may have already set the type for this DIE. */
17355 cv_type = get_die_type (die, cu);
17356 if (cv_type)
17357 return cv_type;
17358
17359 cv_type = make_restrict_type (base_type);
17360 return set_die_type (die, cv_type, cu);
17361 }
17362
17363 /* Handle DW_TAG_atomic_type. */
17364
17365 static struct type *
17366 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17367 {
17368 struct type *base_type, *cv_type;
17369
17370 base_type = die_type (die, cu);
17371
17372 /* The die_type call above may have already set the type for this DIE. */
17373 cv_type = get_die_type (die, cu);
17374 if (cv_type)
17375 return cv_type;
17376
17377 cv_type = make_atomic_type (base_type);
17378 return set_die_type (die, cv_type, cu);
17379 }
17380
17381 /* Extract all information from a DW_TAG_string_type DIE and add to
17382 the user defined type vector. It isn't really a user defined type,
17383 but it behaves like one, with other DIE's using an AT_user_def_type
17384 attribute to reference it. */
17385
17386 static struct type *
17387 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17388 {
17389 struct objfile *objfile = cu->per_objfile->objfile;
17390 struct gdbarch *gdbarch = objfile->arch ();
17391 struct type *type, *range_type, *index_type, *char_type;
17392 struct attribute *attr;
17393 struct dynamic_prop prop;
17394 bool length_is_constant = true;
17395 LONGEST length;
17396
17397 /* There are a couple of places where bit sizes might be made use of
17398 when parsing a DW_TAG_string_type, however, no producer that we know
17399 of make use of these. Handling bit sizes that are a multiple of the
17400 byte size is easy enough, but what about other bit sizes? Lets deal
17401 with that problem when we have to. Warn about these attributes being
17402 unsupported, then parse the type and ignore them like we always
17403 have. */
17404 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17405 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17406 {
17407 static bool warning_printed = false;
17408 if (!warning_printed)
17409 {
17410 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17411 "currently supported on DW_TAG_string_type."));
17412 warning_printed = true;
17413 }
17414 }
17415
17416 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17417 if (attr != nullptr && !attr->form_is_constant ())
17418 {
17419 /* The string length describes the location at which the length of
17420 the string can be found. The size of the length field can be
17421 specified with one of the attributes below. */
17422 struct type *prop_type;
17423 struct attribute *len
17424 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17425 if (len == nullptr)
17426 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17427 if (len != nullptr && len->form_is_constant ())
17428 {
17429 /* Pass 0 as the default as we know this attribute is constant
17430 and the default value will not be returned. */
17431 LONGEST sz = len->constant_value (0);
17432 prop_type = objfile_int_type (objfile, sz, true);
17433 }
17434 else
17435 {
17436 /* If the size is not specified then we assume it is the size of
17437 an address on this target. */
17438 prop_type = cu->addr_sized_int_type (true);
17439 }
17440
17441 /* Convert the attribute into a dynamic property. */
17442 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17443 length = 1;
17444 else
17445 length_is_constant = false;
17446 }
17447 else if (attr != nullptr)
17448 {
17449 /* This DW_AT_string_length just contains the length with no
17450 indirection. There's no need to create a dynamic property in this
17451 case. Pass 0 for the default value as we know it will not be
17452 returned in this case. */
17453 length = attr->constant_value (0);
17454 }
17455 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17456 {
17457 /* We don't currently support non-constant byte sizes for strings. */
17458 length = attr->constant_value (1);
17459 }
17460 else
17461 {
17462 /* Use 1 as a fallback length if we have nothing else. */
17463 length = 1;
17464 }
17465
17466 index_type = objfile_type (objfile)->builtin_int;
17467 if (length_is_constant)
17468 range_type = create_static_range_type (NULL, index_type, 1, length);
17469 else
17470 {
17471 struct dynamic_prop low_bound;
17472
17473 low_bound.set_const_val (1);
17474 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17475 }
17476 char_type = language_string_char_type (cu->language_defn, gdbarch);
17477 type = create_string_type (NULL, char_type, range_type);
17478
17479 return set_die_type (die, type, cu);
17480 }
17481
17482 /* Assuming that DIE corresponds to a function, returns nonzero
17483 if the function is prototyped. */
17484
17485 static int
17486 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17487 {
17488 struct attribute *attr;
17489
17490 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17491 if (attr && attr->as_boolean ())
17492 return 1;
17493
17494 /* The DWARF standard implies that the DW_AT_prototyped attribute
17495 is only meaningful for C, but the concept also extends to other
17496 languages that allow unprototyped functions (Eg: Objective C).
17497 For all other languages, assume that functions are always
17498 prototyped. */
17499 if (cu->per_cu->lang != language_c
17500 && cu->per_cu->lang != language_objc
17501 && cu->per_cu->lang != language_opencl)
17502 return 1;
17503
17504 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17505 prototyped and unprototyped functions; default to prototyped,
17506 since that is more common in modern code (and RealView warns
17507 about unprototyped functions). */
17508 if (producer_is_realview (cu->producer))
17509 return 1;
17510
17511 return 0;
17512 }
17513
17514 /* Handle DIES due to C code like:
17515
17516 struct foo
17517 {
17518 int (*funcp)(int a, long l);
17519 int b;
17520 };
17521
17522 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17523
17524 static struct type *
17525 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17526 {
17527 struct objfile *objfile = cu->per_objfile->objfile;
17528 struct type *type; /* Type that this function returns. */
17529 struct type *ftype; /* Function that returns above type. */
17530 struct attribute *attr;
17531
17532 type = die_type (die, cu);
17533
17534 /* The die_type call above may have already set the type for this DIE. */
17535 ftype = get_die_type (die, cu);
17536 if (ftype)
17537 return ftype;
17538
17539 ftype = lookup_function_type (type);
17540
17541 if (prototyped_function_p (die, cu))
17542 ftype->set_is_prototyped (true);
17543
17544 /* Store the calling convention in the type if it's available in
17545 the subroutine die. Otherwise set the calling convention to
17546 the default value DW_CC_normal. */
17547 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17548 if (attr != nullptr
17549 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17550 TYPE_CALLING_CONVENTION (ftype)
17551 = (enum dwarf_calling_convention) attr->constant_value (0);
17552 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17553 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17554 else
17555 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17556
17557 /* Record whether the function returns normally to its caller or not
17558 if the DWARF producer set that information. */
17559 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17560 if (attr && attr->as_boolean ())
17561 TYPE_NO_RETURN (ftype) = 1;
17562
17563 /* We need to add the subroutine type to the die immediately so
17564 we don't infinitely recurse when dealing with parameters
17565 declared as the same subroutine type. */
17566 set_die_type (die, ftype, cu);
17567
17568 if (die->child != NULL)
17569 {
17570 struct type *void_type = objfile_type (objfile)->builtin_void;
17571 struct die_info *child_die;
17572 int nparams, iparams;
17573
17574 /* Count the number of parameters.
17575 FIXME: GDB currently ignores vararg functions, but knows about
17576 vararg member functions. */
17577 nparams = 0;
17578 child_die = die->child;
17579 while (child_die && child_die->tag)
17580 {
17581 if (child_die->tag == DW_TAG_formal_parameter)
17582 nparams++;
17583 else if (child_die->tag == DW_TAG_unspecified_parameters)
17584 ftype->set_has_varargs (true);
17585
17586 child_die = child_die->sibling;
17587 }
17588
17589 /* Allocate storage for parameters and fill them in. */
17590 ftype->set_num_fields (nparams);
17591 ftype->set_fields
17592 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17593
17594 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17595 even if we error out during the parameters reading below. */
17596 for (iparams = 0; iparams < nparams; iparams++)
17597 ftype->field (iparams).set_type (void_type);
17598
17599 iparams = 0;
17600 child_die = die->child;
17601 while (child_die && child_die->tag)
17602 {
17603 if (child_die->tag == DW_TAG_formal_parameter)
17604 {
17605 struct type *arg_type;
17606
17607 /* DWARF version 2 has no clean way to discern C++
17608 static and non-static member functions. G++ helps
17609 GDB by marking the first parameter for non-static
17610 member functions (which is the this pointer) as
17611 artificial. We pass this information to
17612 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17613
17614 DWARF version 3 added DW_AT_object_pointer, which GCC
17615 4.5 does not yet generate. */
17616 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17617 if (attr != nullptr)
17618 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17619 else
17620 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17621 arg_type = die_type (child_die, cu);
17622
17623 /* RealView does not mark THIS as const, which the testsuite
17624 expects. GCC marks THIS as const in method definitions,
17625 but not in the class specifications (GCC PR 43053). */
17626 if (cu->per_cu->lang == language_cplus
17627 && !TYPE_CONST (arg_type)
17628 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17629 {
17630 int is_this = 0;
17631 struct dwarf2_cu *arg_cu = cu;
17632 const char *name = dwarf2_name (child_die, cu);
17633
17634 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17635 if (attr != nullptr)
17636 {
17637 /* If the compiler emits this, use it. */
17638 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17639 is_this = 1;
17640 }
17641 else if (name && strcmp (name, "this") == 0)
17642 /* Function definitions will have the argument names. */
17643 is_this = 1;
17644 else if (name == NULL && iparams == 0)
17645 /* Declarations may not have the names, so like
17646 elsewhere in GDB, assume an artificial first
17647 argument is "this". */
17648 is_this = 1;
17649
17650 if (is_this)
17651 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17652 arg_type, 0);
17653 }
17654
17655 ftype->field (iparams).set_type (arg_type);
17656 iparams++;
17657 }
17658 child_die = child_die->sibling;
17659 }
17660 }
17661
17662 return ftype;
17663 }
17664
17665 static struct type *
17666 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17667 {
17668 struct objfile *objfile = cu->per_objfile->objfile;
17669 const char *name = NULL;
17670 struct type *this_type, *target_type;
17671
17672 name = dwarf2_full_name (NULL, die, cu);
17673 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17674 this_type->set_target_is_stub (true);
17675 set_die_type (die, this_type, cu);
17676 target_type = die_type (die, cu);
17677 if (target_type != this_type)
17678 TYPE_TARGET_TYPE (this_type) = target_type;
17679 else
17680 {
17681 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17682 spec and cause infinite loops in GDB. */
17683 complaint (_("Self-referential DW_TAG_typedef "
17684 "- DIE at %s [in module %s]"),
17685 sect_offset_str (die->sect_off), objfile_name (objfile));
17686 TYPE_TARGET_TYPE (this_type) = NULL;
17687 }
17688 if (name == NULL)
17689 {
17690 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17691 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17692 Handle these by just returning the target type, rather than
17693 constructing an anonymous typedef type and trying to handle this
17694 elsewhere. */
17695 set_die_type (die, target_type, cu);
17696 return target_type;
17697 }
17698 return this_type;
17699 }
17700
17701 /* Helper for get_dwarf2_rational_constant that computes the value of
17702 a given gmp_mpz given an attribute. */
17703
17704 static void
17705 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17706 {
17707 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17708 location expression that pushes an implicit value. */
17709 if (attr->form == DW_FORM_exprloc)
17710 {
17711 dwarf_block *blk = attr->as_block ();
17712 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17713 {
17714 uint64_t len;
17715 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17716 blk->data + blk->size,
17717 &len);
17718 if (ptr - blk->data + len <= blk->size)
17719 {
17720 mpz_import (value->val, len,
17721 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17722 1, 0, 0, ptr);
17723 return;
17724 }
17725 }
17726
17727 /* On failure set it to 1. */
17728 *value = gdb_mpz (1);
17729 }
17730 else if (attr->form_is_block ())
17731 {
17732 dwarf_block *blk = attr->as_block ();
17733 mpz_import (value->val, blk->size,
17734 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17735 1, 0, 0, blk->data);
17736 }
17737 else
17738 *value = gdb_mpz (attr->constant_value (1));
17739 }
17740
17741 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17742 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17743
17744 If the numerator and/or numerator attribute is missing,
17745 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17746 untouched. */
17747
17748 static void
17749 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17750 gdb_mpz *numerator, gdb_mpz *denominator)
17751 {
17752 struct attribute *num_attr, *denom_attr;
17753
17754 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17755 if (num_attr == nullptr)
17756 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17757 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17758
17759 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17760 if (denom_attr == nullptr)
17761 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17762 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17763
17764 if (num_attr == nullptr || denom_attr == nullptr)
17765 return;
17766
17767 get_mpz (cu, numerator, num_attr);
17768 get_mpz (cu, denominator, denom_attr);
17769 }
17770
17771 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17772 rational constant, rather than a signed one.
17773
17774 If the rational constant has a negative value, a complaint
17775 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17776
17777 static void
17778 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17779 struct dwarf2_cu *cu,
17780 gdb_mpz *numerator,
17781 gdb_mpz *denominator)
17782 {
17783 gdb_mpz num (1);
17784 gdb_mpz denom (1);
17785
17786 get_dwarf2_rational_constant (die, cu, &num, &denom);
17787 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17788 {
17789 mpz_neg (num.val, num.val);
17790 mpz_neg (denom.val, denom.val);
17791 }
17792 else if (mpz_sgn (num.val) == -1)
17793 {
17794 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17795 " in DIE at %s"),
17796 sect_offset_str (die->sect_off));
17797 return;
17798 }
17799 else if (mpz_sgn (denom.val) == -1)
17800 {
17801 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17802 " in DIE at %s"),
17803 sect_offset_str (die->sect_off));
17804 return;
17805 }
17806
17807 *numerator = std::move (num);
17808 *denominator = std::move (denom);
17809 }
17810
17811 /* Assuming that ENCODING is a string whose contents starting at the
17812 K'th character is "_nn" where "nn" is a decimal number, scan that
17813 number and set RESULT to the value. K is updated to point to the
17814 character immediately following the number.
17815
17816 If the string does not conform to the format described above, false
17817 is returned, and K may or may not be changed. */
17818
17819 static bool
17820 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17821 {
17822 /* The next character should be an underscore ('_') followed
17823 by a digit. */
17824 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17825 return false;
17826
17827 /* Skip the underscore. */
17828 k++;
17829 int start = k;
17830
17831 /* Determine the number of digits for our number. */
17832 while (isdigit (encoding[k]))
17833 k++;
17834 if (k == start)
17835 return false;
17836
17837 std::string copy (&encoding[start], k - start);
17838 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17839 return false;
17840
17841 return true;
17842 }
17843
17844 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17845 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17846 DENOM, update OFFSET, and return true on success. Return false on
17847 failure. */
17848
17849 static bool
17850 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17851 gdb_mpz *num, gdb_mpz *denom)
17852 {
17853 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17854 return false;
17855 return ada_get_gnat_encoded_number (encoding, offset, denom);
17856 }
17857
17858 /* Assuming DIE corresponds to a fixed point type, finish the creation
17859 of the corresponding TYPE by setting its type-specific data. CU is
17860 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17861 encodings. It is nullptr if the GNAT encoding should be
17862 ignored. */
17863
17864 static void
17865 finish_fixed_point_type (struct type *type, const char *suffix,
17866 struct die_info *die, struct dwarf2_cu *cu)
17867 {
17868 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17869 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17870
17871 /* If GNAT encodings are preferred, don't examine the
17872 attributes. */
17873 struct attribute *attr = nullptr;
17874 if (suffix == nullptr)
17875 {
17876 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17877 if (attr == nullptr)
17878 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17879 if (attr == nullptr)
17880 attr = dwarf2_attr (die, DW_AT_small, cu);
17881 }
17882
17883 /* Numerator and denominator of our fixed-point type's scaling factor.
17884 The default is a scaling factor of 1, which we use as a fallback
17885 when we are not able to decode it (problem with the debugging info,
17886 unsupported forms, bug in GDB, etc...). Using that as the default
17887 allows us to at least print the unscaled value, which might still
17888 be useful to a user. */
17889 gdb_mpz scale_num (1);
17890 gdb_mpz scale_denom (1);
17891
17892 if (attr == nullptr)
17893 {
17894 int offset = 0;
17895 if (suffix != nullptr
17896 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17897 &scale_denom)
17898 /* The number might be encoded as _nn_dd_nn_dd, where the
17899 second ratio is the 'small value. In this situation, we
17900 want the second value. */
17901 && (suffix[offset] != '_'
17902 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17903 &scale_denom)))
17904 {
17905 /* Found it. */
17906 }
17907 else
17908 {
17909 /* Scaling factor not found. Assume a scaling factor of 1,
17910 and hope for the best. At least the user will be able to
17911 see the encoded value. */
17912 scale_num = 1;
17913 scale_denom = 1;
17914 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17915 sect_offset_str (die->sect_off));
17916 }
17917 }
17918 else if (attr->name == DW_AT_binary_scale)
17919 {
17920 LONGEST scale_exp = attr->constant_value (0);
17921 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17922
17923 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17924 }
17925 else if (attr->name == DW_AT_decimal_scale)
17926 {
17927 LONGEST scale_exp = attr->constant_value (0);
17928 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17929
17930 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17931 }
17932 else if (attr->name == DW_AT_small)
17933 {
17934 struct die_info *scale_die;
17935 struct dwarf2_cu *scale_cu = cu;
17936
17937 scale_die = follow_die_ref (die, attr, &scale_cu);
17938 if (scale_die->tag == DW_TAG_constant)
17939 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17940 &scale_num, &scale_denom);
17941 else
17942 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17943 " (DIE at %s)"),
17944 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17945 }
17946 else
17947 {
17948 complaint (_("unsupported scale attribute %s for fixed-point type"
17949 " (DIE at %s)"),
17950 dwarf_attr_name (attr->name),
17951 sect_offset_str (die->sect_off));
17952 }
17953
17954 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17955 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17956 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17957 mpq_canonicalize (scaling_factor.val);
17958 }
17959
17960 /* The gnat-encoding suffix for fixed point. */
17961
17962 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17963
17964 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17965 "XF" suffix of the name. The text after this is what encodes the
17966 'small and 'delta information. Otherwise, return nullptr. */
17967
17968 static const char *
17969 gnat_encoded_fixed_point_type_info (const char *name)
17970 {
17971 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17972 }
17973
17974 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17975 (which may be different from NAME) to the architecture back-end to allow
17976 it to guess the correct format if necessary. */
17977
17978 static struct type *
17979 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17980 const char *name_hint, enum bfd_endian byte_order)
17981 {
17982 struct gdbarch *gdbarch = objfile->arch ();
17983 const struct floatformat **format;
17984 struct type *type;
17985
17986 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17987 if (format)
17988 type = init_float_type (objfile, bits, name, format, byte_order);
17989 else
17990 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17991
17992 return type;
17993 }
17994
17995 /* Allocate an integer type of size BITS and name NAME. */
17996
17997 static struct type *
17998 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17999 int bits, int unsigned_p, const char *name)
18000 {
18001 struct type *type;
18002
18003 /* Versions of Intel's C Compiler generate an integer type called "void"
18004 instead of using DW_TAG_unspecified_type. This has been seen on
18005 at least versions 14, 17, and 18. */
18006 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18007 && strcmp (name, "void") == 0)
18008 type = objfile_type (objfile)->builtin_void;
18009 else
18010 type = init_integer_type (objfile, bits, unsigned_p, name);
18011
18012 return type;
18013 }
18014
18015 /* Return true if DIE has a DW_AT_small attribute whose value is
18016 a constant rational, where both the numerator and denominator
18017 are equal to zero.
18018
18019 CU is the DIE's Compilation Unit. */
18020
18021 static bool
18022 has_zero_over_zero_small_attribute (struct die_info *die,
18023 struct dwarf2_cu *cu)
18024 {
18025 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18026 if (attr == nullptr)
18027 return false;
18028
18029 struct dwarf2_cu *scale_cu = cu;
18030 struct die_info *scale_die
18031 = follow_die_ref (die, attr, &scale_cu);
18032
18033 if (scale_die->tag != DW_TAG_constant)
18034 return false;
18035
18036 gdb_mpz num (1), denom (1);
18037 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18038 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18039 }
18040
18041 /* Initialise and return a floating point type of size BITS suitable for
18042 use as a component of a complex number. The NAME_HINT is passed through
18043 when initialising the floating point type and is the name of the complex
18044 type.
18045
18046 As DWARF doesn't currently provide an explicit name for the components
18047 of a complex number, but it can be helpful to have these components
18048 named, we try to select a suitable name based on the size of the
18049 component. */
18050 static struct type *
18051 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18052 struct objfile *objfile,
18053 int bits, const char *name_hint,
18054 enum bfd_endian byte_order)
18055 {
18056 gdbarch *gdbarch = objfile->arch ();
18057 struct type *tt = nullptr;
18058
18059 /* Try to find a suitable floating point builtin type of size BITS.
18060 We're going to use the name of this type as the name for the complex
18061 target type that we are about to create. */
18062 switch (cu->per_cu->lang)
18063 {
18064 case language_fortran:
18065 switch (bits)
18066 {
18067 case 32:
18068 tt = builtin_f_type (gdbarch)->builtin_real;
18069 break;
18070 case 64:
18071 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18072 break;
18073 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18074 case 128:
18075 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18076 break;
18077 }
18078 break;
18079 default:
18080 switch (bits)
18081 {
18082 case 32:
18083 tt = builtin_type (gdbarch)->builtin_float;
18084 break;
18085 case 64:
18086 tt = builtin_type (gdbarch)->builtin_double;
18087 break;
18088 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18089 case 128:
18090 tt = builtin_type (gdbarch)->builtin_long_double;
18091 break;
18092 }
18093 break;
18094 }
18095
18096 /* If the type we found doesn't match the size we were looking for, then
18097 pretend we didn't find a type at all, the complex target type we
18098 create will then be nameless. */
18099 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18100 tt = nullptr;
18101
18102 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18103 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18104 }
18105
18106 /* Find a representation of a given base type and install
18107 it in the TYPE field of the die. */
18108
18109 static struct type *
18110 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18111 {
18112 struct objfile *objfile = cu->per_objfile->objfile;
18113 struct type *type;
18114 struct attribute *attr;
18115 int encoding = 0, bits = 0;
18116 const char *name;
18117 gdbarch *arch;
18118
18119 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18120 if (attr != nullptr && attr->form_is_constant ())
18121 encoding = attr->constant_value (0);
18122 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18123 if (attr != nullptr)
18124 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18125 name = dwarf2_name (die, cu);
18126 if (!name)
18127 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18128
18129 arch = objfile->arch ();
18130 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18131
18132 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18133 if (attr != nullptr && attr->form_is_constant ())
18134 {
18135 int endianity = attr->constant_value (0);
18136
18137 switch (endianity)
18138 {
18139 case DW_END_big:
18140 byte_order = BFD_ENDIAN_BIG;
18141 break;
18142 case DW_END_little:
18143 byte_order = BFD_ENDIAN_LITTLE;
18144 break;
18145 default:
18146 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18147 break;
18148 }
18149 }
18150
18151 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18152 && cu->per_cu->lang == language_ada
18153 && has_zero_over_zero_small_attribute (die, cu))
18154 {
18155 /* brobecker/2018-02-24: This is a fixed point type for which
18156 the scaling factor is represented as fraction whose value
18157 does not make sense (zero divided by zero), so we should
18158 normally never see these. However, there is a small category
18159 of fixed point types for which GNAT is unable to provide
18160 the scaling factor via the standard DWARF mechanisms, and
18161 for which the info is provided via the GNAT encodings instead.
18162 This is likely what this DIE is about. */
18163 encoding = (encoding == DW_ATE_signed_fixed
18164 ? DW_ATE_signed
18165 : DW_ATE_unsigned);
18166 }
18167
18168 /* With GNAT encodings, fixed-point information will be encoded in
18169 the type name. Note that this can also occur with the above
18170 zero-over-zero case, which is why this is a separate "if" rather
18171 than an "else if". */
18172 const char *gnat_encoding_suffix = nullptr;
18173 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18174 && cu->per_cu->lang == language_ada
18175 && name != nullptr)
18176 {
18177 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18178 if (gnat_encoding_suffix != nullptr)
18179 {
18180 gdb_assert (startswith (gnat_encoding_suffix,
18181 GNAT_FIXED_POINT_SUFFIX));
18182 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18183 name, gnat_encoding_suffix - name);
18184 /* Use -1 here so that SUFFIX points at the "_" after the
18185 "XF". */
18186 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18187
18188 encoding = (encoding == DW_ATE_signed
18189 ? DW_ATE_signed_fixed
18190 : DW_ATE_unsigned_fixed);
18191 }
18192 }
18193
18194 switch (encoding)
18195 {
18196 case DW_ATE_address:
18197 /* Turn DW_ATE_address into a void * pointer. */
18198 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18199 type = init_pointer_type (objfile, bits, name, type);
18200 break;
18201 case DW_ATE_boolean:
18202 type = init_boolean_type (objfile, bits, 1, name);
18203 break;
18204 case DW_ATE_complex_float:
18205 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18206 byte_order);
18207 if (type->code () == TYPE_CODE_ERROR)
18208 {
18209 if (name == nullptr)
18210 {
18211 struct obstack *obstack
18212 = &cu->per_objfile->objfile->objfile_obstack;
18213 name = obconcat (obstack, "_Complex ", type->name (),
18214 nullptr);
18215 }
18216 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18217 }
18218 else
18219 type = init_complex_type (name, type);
18220 break;
18221 case DW_ATE_decimal_float:
18222 type = init_decfloat_type (objfile, bits, name);
18223 break;
18224 case DW_ATE_float:
18225 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18226 break;
18227 case DW_ATE_signed:
18228 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18229 break;
18230 case DW_ATE_unsigned:
18231 if (cu->per_cu->lang == language_fortran
18232 && name
18233 && startswith (name, "character("))
18234 type = init_character_type (objfile, bits, 1, name);
18235 else
18236 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18237 break;
18238 case DW_ATE_signed_char:
18239 if (cu->per_cu->lang == language_ada
18240 || cu->per_cu->lang == language_m2
18241 || cu->per_cu->lang == language_pascal
18242 || cu->per_cu->lang == language_fortran)
18243 type = init_character_type (objfile, bits, 0, name);
18244 else
18245 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18246 break;
18247 case DW_ATE_unsigned_char:
18248 if (cu->per_cu->lang == language_ada
18249 || cu->per_cu->lang == language_m2
18250 || cu->per_cu->lang == language_pascal
18251 || cu->per_cu->lang == language_fortran
18252 || cu->per_cu->lang == language_rust)
18253 type = init_character_type (objfile, bits, 1, name);
18254 else
18255 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18256 break;
18257 case DW_ATE_UTF:
18258 {
18259 if (bits == 16)
18260 type = builtin_type (arch)->builtin_char16;
18261 else if (bits == 32)
18262 type = builtin_type (arch)->builtin_char32;
18263 else
18264 {
18265 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18266 bits);
18267 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18268 }
18269 return set_die_type (die, type, cu);
18270 }
18271 break;
18272 case DW_ATE_signed_fixed:
18273 type = init_fixed_point_type (objfile, bits, 0, name);
18274 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18275 break;
18276 case DW_ATE_unsigned_fixed:
18277 type = init_fixed_point_type (objfile, bits, 1, name);
18278 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18279 break;
18280
18281 default:
18282 complaint (_("unsupported DW_AT_encoding: '%s'"),
18283 dwarf_type_encoding_name (encoding));
18284 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18285 break;
18286 }
18287
18288 if (name && strcmp (name, "char") == 0)
18289 type->set_has_no_signedness (true);
18290
18291 maybe_set_alignment (cu, die, type);
18292
18293 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18294
18295 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18296 {
18297 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18298 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18299 {
18300 unsigned real_bit_size = attr->as_unsigned ();
18301 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18302 /* Only use the attributes if they make sense together. */
18303 if (attr == nullptr
18304 || (attr->as_unsigned () + real_bit_size
18305 <= 8 * TYPE_LENGTH (type)))
18306 {
18307 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18308 = real_bit_size;
18309 if (attr != nullptr)
18310 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18311 = attr->as_unsigned ();
18312 }
18313 }
18314 }
18315
18316 return set_die_type (die, type, cu);
18317 }
18318
18319 /* A helper function that returns the name of DIE, if it refers to a
18320 variable declaration. */
18321
18322 static const char *
18323 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18324 {
18325 if (die->tag != DW_TAG_variable)
18326 return nullptr;
18327
18328 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18329 if (attr == nullptr || !attr->as_boolean ())
18330 return nullptr;
18331
18332 attr = dwarf2_attr (die, DW_AT_name, cu);
18333 if (attr == nullptr)
18334 return nullptr;
18335 return attr->as_string ();
18336 }
18337
18338 /* Parse dwarf attribute if it's a block, reference or constant and put the
18339 resulting value of the attribute into struct bound_prop.
18340 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18341
18342 static int
18343 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18344 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18345 struct type *default_type)
18346 {
18347 struct dwarf2_property_baton *baton;
18348 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18349 struct objfile *objfile = per_objfile->objfile;
18350 struct obstack *obstack = &objfile->objfile_obstack;
18351
18352 gdb_assert (default_type != NULL);
18353
18354 if (attr == NULL || prop == NULL)
18355 return 0;
18356
18357 if (attr->form_is_block ())
18358 {
18359 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18360 baton->property_type = default_type;
18361 baton->locexpr.per_cu = cu->per_cu;
18362 baton->locexpr.per_objfile = per_objfile;
18363
18364 struct dwarf_block *block;
18365 if (attr->form == DW_FORM_data16)
18366 {
18367 size_t data_size = 16;
18368 block = XOBNEW (obstack, struct dwarf_block);
18369 block->size = (data_size
18370 + 2 /* Extra bytes for DW_OP and arg. */);
18371 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18372 data[0] = DW_OP_implicit_value;
18373 data[1] = data_size;
18374 memcpy (&data[2], attr->as_block ()->data, data_size);
18375 block->data = data;
18376 }
18377 else
18378 block = attr->as_block ();
18379
18380 baton->locexpr.size = block->size;
18381 baton->locexpr.data = block->data;
18382 switch (attr->name)
18383 {
18384 case DW_AT_string_length:
18385 baton->locexpr.is_reference = true;
18386 break;
18387 default:
18388 baton->locexpr.is_reference = false;
18389 break;
18390 }
18391
18392 prop->set_locexpr (baton);
18393 gdb_assert (prop->baton () != NULL);
18394 }
18395 else if (attr->form_is_ref ())
18396 {
18397 struct dwarf2_cu *target_cu = cu;
18398 struct die_info *target_die;
18399 struct attribute *target_attr;
18400
18401 target_die = follow_die_ref (die, attr, &target_cu);
18402 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18403 if (target_attr == NULL)
18404 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18405 target_cu);
18406 if (target_attr == nullptr)
18407 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
18408 target_cu);
18409 if (target_attr == NULL)
18410 {
18411 const char *name = var_decl_name (target_die, target_cu);
18412 if (name != nullptr)
18413 {
18414 prop->set_variable_name (name);
18415 return 1;
18416 }
18417 return 0;
18418 }
18419
18420 switch (target_attr->name)
18421 {
18422 case DW_AT_location:
18423 if (target_attr->form_is_section_offset ())
18424 {
18425 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18426 baton->property_type = die_type (target_die, target_cu);
18427 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18428 prop->set_loclist (baton);
18429 gdb_assert (prop->baton () != NULL);
18430 }
18431 else if (target_attr->form_is_block ())
18432 {
18433 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18434 baton->property_type = die_type (target_die, target_cu);
18435 baton->locexpr.per_cu = cu->per_cu;
18436 baton->locexpr.per_objfile = per_objfile;
18437 struct dwarf_block *block = target_attr->as_block ();
18438 baton->locexpr.size = block->size;
18439 baton->locexpr.data = block->data;
18440 baton->locexpr.is_reference = true;
18441 prop->set_locexpr (baton);
18442 gdb_assert (prop->baton () != NULL);
18443 }
18444 else
18445 {
18446 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18447 "dynamic property");
18448 return 0;
18449 }
18450 break;
18451 case DW_AT_data_member_location:
18452 case DW_AT_data_bit_offset:
18453 {
18454 LONGEST offset;
18455
18456 if (!handle_member_location (target_die, target_cu, &offset))
18457 return 0;
18458
18459 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18460 baton->property_type = read_type_die (target_die->parent,
18461 target_cu);
18462 baton->offset_info.offset = offset;
18463 baton->offset_info.type = die_type (target_die, target_cu);
18464 prop->set_addr_offset (baton);
18465 break;
18466 }
18467 }
18468 }
18469 else if (attr->form_is_constant ())
18470 prop->set_const_val (attr->constant_value (0));
18471 else if (attr->form_is_section_offset ())
18472 {
18473 switch (attr->name)
18474 {
18475 case DW_AT_string_length:
18476 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18477 baton->property_type = default_type;
18478 fill_in_loclist_baton (cu, &baton->loclist, attr);
18479 prop->set_loclist (baton);
18480 gdb_assert (prop->baton () != NULL);
18481 break;
18482 default:
18483 goto invalid;
18484 }
18485 }
18486 else
18487 goto invalid;
18488
18489 return 1;
18490
18491 invalid:
18492 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18493 dwarf2_name (die, cu));
18494 return 0;
18495 }
18496
18497 /* See read.h. */
18498
18499 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18500 present (which is valid) then compute the default type based on the
18501 compilation units address size. */
18502
18503 static struct type *
18504 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18505 {
18506 struct type *index_type = die_type (die, cu);
18507
18508 /* Dwarf-2 specifications explicitly allows to create subrange types
18509 without specifying a base type.
18510 In that case, the base type must be set to the type of
18511 the lower bound, upper bound or count, in that order, if any of these
18512 three attributes references an object that has a type.
18513 If no base type is found, the Dwarf-2 specifications say that
18514 a signed integer type of size equal to the size of an address should
18515 be used.
18516 For the following C code: `extern char gdb_int [];'
18517 GCC produces an empty range DIE.
18518 FIXME: muller/2010-05-28: Possible references to object for low bound,
18519 high bound or count are not yet handled by this code. */
18520 if (index_type->code () == TYPE_CODE_VOID)
18521 index_type = cu->addr_sized_int_type (false);
18522
18523 return index_type;
18524 }
18525
18526 /* Read the given DW_AT_subrange DIE. */
18527
18528 static struct type *
18529 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18530 {
18531 struct type *base_type, *orig_base_type;
18532 struct type *range_type;
18533 struct attribute *attr;
18534 struct dynamic_prop low, high;
18535 int low_default_is_valid;
18536 int high_bound_is_count = 0;
18537 const char *name;
18538 ULONGEST negative_mask;
18539
18540 orig_base_type = read_subrange_index_type (die, cu);
18541
18542 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18543 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18544 creating the range type, but we use the result of check_typedef
18545 when examining properties of the type. */
18546 base_type = check_typedef (orig_base_type);
18547
18548 /* The die_type call above may have already set the type for this DIE. */
18549 range_type = get_die_type (die, cu);
18550 if (range_type)
18551 return range_type;
18552
18553 high.set_const_val (0);
18554
18555 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18556 omitting DW_AT_lower_bound. */
18557 switch (cu->per_cu->lang)
18558 {
18559 case language_c:
18560 case language_cplus:
18561 low.set_const_val (0);
18562 low_default_is_valid = 1;
18563 break;
18564 case language_fortran:
18565 low.set_const_val (1);
18566 low_default_is_valid = 1;
18567 break;
18568 case language_d:
18569 case language_objc:
18570 case language_rust:
18571 low.set_const_val (0);
18572 low_default_is_valid = (cu->header.version >= 4);
18573 break;
18574 case language_ada:
18575 case language_m2:
18576 case language_pascal:
18577 low.set_const_val (1);
18578 low_default_is_valid = (cu->header.version >= 4);
18579 break;
18580 default:
18581 low.set_const_val (0);
18582 low_default_is_valid = 0;
18583 break;
18584 }
18585
18586 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18587 if (attr != nullptr)
18588 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18589 else if (!low_default_is_valid)
18590 complaint (_("Missing DW_AT_lower_bound "
18591 "- DIE at %s [in module %s]"),
18592 sect_offset_str (die->sect_off),
18593 objfile_name (cu->per_objfile->objfile));
18594
18595 struct attribute *attr_ub, *attr_count;
18596 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18597 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18598 {
18599 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18600 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18601 {
18602 /* If bounds are constant do the final calculation here. */
18603 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18604 high.set_const_val (low.const_val () + high.const_val () - 1);
18605 else
18606 high_bound_is_count = 1;
18607 }
18608 else
18609 {
18610 if (attr_ub != NULL)
18611 complaint (_("Unresolved DW_AT_upper_bound "
18612 "- DIE at %s [in module %s]"),
18613 sect_offset_str (die->sect_off),
18614 objfile_name (cu->per_objfile->objfile));
18615 if (attr_count != NULL)
18616 complaint (_("Unresolved DW_AT_count "
18617 "- DIE at %s [in module %s]"),
18618 sect_offset_str (die->sect_off),
18619 objfile_name (cu->per_objfile->objfile));
18620 }
18621 }
18622
18623 LONGEST bias = 0;
18624 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18625 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18626 bias = bias_attr->constant_value (0);
18627
18628 /* Normally, the DWARF producers are expected to use a signed
18629 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18630 But this is unfortunately not always the case, as witnessed
18631 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18632 is used instead. To work around that ambiguity, we treat
18633 the bounds as signed, and thus sign-extend their values, when
18634 the base type is signed. */
18635 negative_mask =
18636 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18637 if (low.kind () == PROP_CONST
18638 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18639 low.set_const_val (low.const_val () | negative_mask);
18640 if (high.kind () == PROP_CONST
18641 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18642 high.set_const_val (high.const_val () | negative_mask);
18643
18644 /* Check for bit and byte strides. */
18645 struct dynamic_prop byte_stride_prop;
18646 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18647 if (attr_byte_stride != nullptr)
18648 {
18649 struct type *prop_type = cu->addr_sized_int_type (false);
18650 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18651 prop_type);
18652 }
18653
18654 struct dynamic_prop bit_stride_prop;
18655 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18656 if (attr_bit_stride != nullptr)
18657 {
18658 /* It only makes sense to have either a bit or byte stride. */
18659 if (attr_byte_stride != nullptr)
18660 {
18661 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18662 "- DIE at %s [in module %s]"),
18663 sect_offset_str (die->sect_off),
18664 objfile_name (cu->per_objfile->objfile));
18665 attr_bit_stride = nullptr;
18666 }
18667 else
18668 {
18669 struct type *prop_type = cu->addr_sized_int_type (false);
18670 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18671 prop_type);
18672 }
18673 }
18674
18675 if (attr_byte_stride != nullptr
18676 || attr_bit_stride != nullptr)
18677 {
18678 bool byte_stride_p = (attr_byte_stride != nullptr);
18679 struct dynamic_prop *stride
18680 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18681
18682 range_type
18683 = create_range_type_with_stride (NULL, orig_base_type, &low,
18684 &high, bias, stride, byte_stride_p);
18685 }
18686 else
18687 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18688
18689 if (high_bound_is_count)
18690 range_type->bounds ()->flag_upper_bound_is_count = 1;
18691
18692 /* Ada expects an empty array on no boundary attributes. */
18693 if (attr == NULL && cu->per_cu->lang != language_ada)
18694 range_type->bounds ()->high.set_undefined ();
18695
18696 name = dwarf2_name (die, cu);
18697 if (name)
18698 range_type->set_name (name);
18699
18700 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18701 if (attr != nullptr)
18702 TYPE_LENGTH (range_type) = attr->constant_value (0);
18703
18704 maybe_set_alignment (cu, die, range_type);
18705
18706 set_die_type (die, range_type, cu);
18707
18708 /* set_die_type should be already done. */
18709 set_descriptive_type (range_type, die, cu);
18710
18711 return range_type;
18712 }
18713
18714 static struct type *
18715 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18716 {
18717 struct type *type;
18718
18719 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18720 type->set_name (dwarf2_name (die, cu));
18721
18722 /* In Ada, an unspecified type is typically used when the description
18723 of the type is deferred to a different unit. When encountering
18724 such a type, we treat it as a stub, and try to resolve it later on,
18725 when needed. */
18726 if (cu->per_cu->lang == language_ada)
18727 type->set_is_stub (true);
18728
18729 return set_die_type (die, type, cu);
18730 }
18731
18732 /* Read a single die and all its descendents. Set the die's sibling
18733 field to NULL; set other fields in the die correctly, and set all
18734 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18735 location of the info_ptr after reading all of those dies. PARENT
18736 is the parent of the die in question. */
18737
18738 static struct die_info *
18739 read_die_and_children (const struct die_reader_specs *reader,
18740 const gdb_byte *info_ptr,
18741 const gdb_byte **new_info_ptr,
18742 struct die_info *parent)
18743 {
18744 struct die_info *die;
18745 const gdb_byte *cur_ptr;
18746
18747 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18748 if (die == NULL)
18749 {
18750 *new_info_ptr = cur_ptr;
18751 return NULL;
18752 }
18753 store_in_ref_table (die, reader->cu);
18754
18755 if (die->has_children)
18756 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18757 else
18758 {
18759 die->child = NULL;
18760 *new_info_ptr = cur_ptr;
18761 }
18762
18763 die->sibling = NULL;
18764 die->parent = parent;
18765 return die;
18766 }
18767
18768 /* Read a die, all of its descendents, and all of its siblings; set
18769 all of the fields of all of the dies correctly. Arguments are as
18770 in read_die_and_children. */
18771
18772 static struct die_info *
18773 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18774 const gdb_byte *info_ptr,
18775 const gdb_byte **new_info_ptr,
18776 struct die_info *parent)
18777 {
18778 struct die_info *first_die, *last_sibling;
18779 const gdb_byte *cur_ptr;
18780
18781 cur_ptr = info_ptr;
18782 first_die = last_sibling = NULL;
18783
18784 while (1)
18785 {
18786 struct die_info *die
18787 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18788
18789 if (die == NULL)
18790 {
18791 *new_info_ptr = cur_ptr;
18792 return first_die;
18793 }
18794
18795 if (!first_die)
18796 first_die = die;
18797 else
18798 last_sibling->sibling = die;
18799
18800 last_sibling = die;
18801 }
18802 }
18803
18804 /* Read a die, all of its descendents, and all of its siblings; set
18805 all of the fields of all of the dies correctly. Arguments are as
18806 in read_die_and_children.
18807 This the main entry point for reading a DIE and all its children. */
18808
18809 static struct die_info *
18810 read_die_and_siblings (const struct die_reader_specs *reader,
18811 const gdb_byte *info_ptr,
18812 const gdb_byte **new_info_ptr,
18813 struct die_info *parent)
18814 {
18815 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18816 new_info_ptr, parent);
18817
18818 if (dwarf_die_debug)
18819 {
18820 fprintf_unfiltered (gdb_stdlog,
18821 "Read die from %s@0x%x of %s:\n",
18822 reader->die_section->get_name (),
18823 (unsigned) (info_ptr - reader->die_section->buffer),
18824 bfd_get_filename (reader->abfd));
18825 dump_die (die, dwarf_die_debug);
18826 }
18827
18828 return die;
18829 }
18830
18831 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18832 attributes.
18833 The caller is responsible for filling in the extra attributes
18834 and updating (*DIEP)->num_attrs.
18835 Set DIEP to point to a newly allocated die with its information,
18836 except for its child, sibling, and parent fields. */
18837
18838 static const gdb_byte *
18839 read_full_die_1 (const struct die_reader_specs *reader,
18840 struct die_info **diep, const gdb_byte *info_ptr,
18841 int num_extra_attrs)
18842 {
18843 unsigned int abbrev_number, bytes_read, i;
18844 const struct abbrev_info *abbrev;
18845 struct die_info *die;
18846 struct dwarf2_cu *cu = reader->cu;
18847 bfd *abfd = reader->abfd;
18848
18849 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18850 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18851 info_ptr += bytes_read;
18852 if (!abbrev_number)
18853 {
18854 *diep = NULL;
18855 return info_ptr;
18856 }
18857
18858 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18859 if (!abbrev)
18860 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18861 abbrev_number,
18862 bfd_get_filename (abfd));
18863
18864 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18865 die->sect_off = sect_off;
18866 die->tag = abbrev->tag;
18867 die->abbrev = abbrev_number;
18868 die->has_children = abbrev->has_children;
18869
18870 /* Make the result usable.
18871 The caller needs to update num_attrs after adding the extra
18872 attributes. */
18873 die->num_attrs = abbrev->num_attrs;
18874
18875 bool any_need_reprocess = false;
18876 for (i = 0; i < abbrev->num_attrs; ++i)
18877 {
18878 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18879 info_ptr);
18880 if (die->attrs[i].requires_reprocessing_p ())
18881 any_need_reprocess = true;
18882 }
18883
18884 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18885 if (attr != nullptr && attr->form_is_unsigned ())
18886 cu->str_offsets_base = attr->as_unsigned ();
18887
18888 attr = die->attr (DW_AT_loclists_base);
18889 if (attr != nullptr)
18890 cu->loclist_base = attr->as_unsigned ();
18891
18892 auto maybe_addr_base = die->addr_base ();
18893 if (maybe_addr_base.has_value ())
18894 cu->addr_base = *maybe_addr_base;
18895
18896 attr = die->attr (DW_AT_rnglists_base);
18897 if (attr != nullptr)
18898 cu->rnglists_base = attr->as_unsigned ();
18899
18900 if (any_need_reprocess)
18901 {
18902 for (i = 0; i < abbrev->num_attrs; ++i)
18903 {
18904 if (die->attrs[i].requires_reprocessing_p ())
18905 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18906 }
18907 }
18908 *diep = die;
18909 return info_ptr;
18910 }
18911
18912 /* Read a die and all its attributes.
18913 Set DIEP to point to a newly allocated die with its information,
18914 except for its child, sibling, and parent fields. */
18915
18916 static const gdb_byte *
18917 read_full_die (const struct die_reader_specs *reader,
18918 struct die_info **diep, const gdb_byte *info_ptr)
18919 {
18920 const gdb_byte *result;
18921
18922 result = read_full_die_1 (reader, diep, info_ptr, 0);
18923
18924 if (dwarf_die_debug)
18925 {
18926 fprintf_unfiltered (gdb_stdlog,
18927 "Read die from %s@0x%x of %s:\n",
18928 reader->die_section->get_name (),
18929 (unsigned) (info_ptr - reader->die_section->buffer),
18930 bfd_get_filename (reader->abfd));
18931 dump_die (*diep, dwarf_die_debug);
18932 }
18933
18934 return result;
18935 }
18936 \f
18937
18938 /* Returns nonzero if TAG represents a type that we might generate a partial
18939 symbol for. */
18940
18941 static int
18942 is_type_tag_for_partial (int tag, enum language lang)
18943 {
18944 switch (tag)
18945 {
18946 #if 0
18947 /* Some types that would be reasonable to generate partial symbols for,
18948 that we don't at present. Note that normally this does not
18949 matter, mainly because C compilers don't give names to these
18950 types, but instead emit DW_TAG_typedef. */
18951 case DW_TAG_file_type:
18952 case DW_TAG_ptr_to_member_type:
18953 case DW_TAG_set_type:
18954 case DW_TAG_string_type:
18955 case DW_TAG_subroutine_type:
18956 #endif
18957
18958 /* GNAT may emit an array with a name, but no typedef, so we
18959 need to make a symbol in this case. */
18960 case DW_TAG_array_type:
18961 return lang == language_ada;
18962
18963 case DW_TAG_base_type:
18964 case DW_TAG_class_type:
18965 case DW_TAG_interface_type:
18966 case DW_TAG_enumeration_type:
18967 case DW_TAG_structure_type:
18968 case DW_TAG_subrange_type:
18969 case DW_TAG_typedef:
18970 case DW_TAG_union_type:
18971 return 1;
18972 default:
18973 return 0;
18974 }
18975 }
18976
18977 /* Load all DIEs that are interesting for partial symbols into memory. */
18978
18979 static struct partial_die_info *
18980 load_partial_dies (const struct die_reader_specs *reader,
18981 const gdb_byte *info_ptr, int building_psymtab)
18982 {
18983 struct dwarf2_cu *cu = reader->cu;
18984 struct objfile *objfile = cu->per_objfile->objfile;
18985 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18986 unsigned int bytes_read;
18987 unsigned int load_all = 0;
18988 int nesting_level = 1;
18989
18990 parent_die = NULL;
18991 last_die = NULL;
18992
18993 gdb_assert (cu->per_cu != NULL);
18994 if (cu->load_all_dies)
18995 load_all = 1;
18996
18997 cu->partial_dies
18998 = htab_create_alloc_ex (cu->header.length / 12,
18999 partial_die_hash,
19000 partial_die_eq,
19001 NULL,
19002 &cu->comp_unit_obstack,
19003 hashtab_obstack_allocate,
19004 dummy_obstack_deallocate);
19005
19006 while (1)
19007 {
19008 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19009 &bytes_read);
19010
19011 /* A NULL abbrev means the end of a series of children. */
19012 if (abbrev == NULL)
19013 {
19014 if (--nesting_level == 0)
19015 return first_die;
19016
19017 info_ptr += bytes_read;
19018 last_die = parent_die;
19019 parent_die = parent_die->die_parent;
19020 continue;
19021 }
19022
19023 /* Check for template arguments. We never save these; if
19024 they're seen, we just mark the parent, and go on our way. */
19025 if (parent_die != NULL
19026 && cu->per_cu->lang == language_cplus
19027 && (abbrev->tag == DW_TAG_template_type_param
19028 || abbrev->tag == DW_TAG_template_value_param))
19029 {
19030 parent_die->has_template_arguments = 1;
19031
19032 if (!load_all)
19033 {
19034 /* We don't need a partial DIE for the template argument. */
19035 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19036 continue;
19037 }
19038 }
19039
19040 /* We only recurse into c++ subprograms looking for template arguments.
19041 Skip their other children. */
19042 if (!load_all
19043 && cu->per_cu->lang == language_cplus
19044 && parent_die != NULL
19045 && parent_die->tag == DW_TAG_subprogram
19046 && abbrev->tag != DW_TAG_inlined_subroutine)
19047 {
19048 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19049 continue;
19050 }
19051
19052 /* Check whether this DIE is interesting enough to save. Normally
19053 we would not be interested in members here, but there may be
19054 later variables referencing them via DW_AT_specification (for
19055 static members). */
19056 if (!load_all
19057 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
19058 && abbrev->tag != DW_TAG_constant
19059 && abbrev->tag != DW_TAG_enumerator
19060 && abbrev->tag != DW_TAG_subprogram
19061 && abbrev->tag != DW_TAG_inlined_subroutine
19062 && abbrev->tag != DW_TAG_lexical_block
19063 && abbrev->tag != DW_TAG_variable
19064 && abbrev->tag != DW_TAG_namespace
19065 && abbrev->tag != DW_TAG_module
19066 && abbrev->tag != DW_TAG_member
19067 && abbrev->tag != DW_TAG_imported_unit
19068 && abbrev->tag != DW_TAG_imported_declaration)
19069 {
19070 /* Otherwise we skip to the next sibling, if any. */
19071 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19072 continue;
19073 }
19074
19075 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19076 abbrev);
19077
19078 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19079
19080 /* This two-pass algorithm for processing partial symbols has a
19081 high cost in cache pressure. Thus, handle some simple cases
19082 here which cover the majority of C partial symbols. DIEs
19083 which neither have specification tags in them, nor could have
19084 specification tags elsewhere pointing at them, can simply be
19085 processed and discarded.
19086
19087 This segment is also optional; scan_partial_symbols and
19088 add_partial_symbol will handle these DIEs if we chain
19089 them in normally. When compilers which do not emit large
19090 quantities of duplicate debug information are more common,
19091 this code can probably be removed. */
19092
19093 /* Any complete simple types at the top level (pretty much all
19094 of them, for a language without namespaces), can be processed
19095 directly. */
19096 if (parent_die == NULL
19097 && pdi.has_specification == 0
19098 && pdi.is_declaration == 0
19099 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19100 || pdi.tag == DW_TAG_base_type
19101 || pdi.tag == DW_TAG_array_type
19102 || pdi.tag == DW_TAG_subrange_type))
19103 {
19104 if (building_psymtab && pdi.raw_name != NULL)
19105 add_partial_symbol (&pdi, cu);
19106
19107 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19108 continue;
19109 }
19110
19111 /* The exception for DW_TAG_typedef with has_children above is
19112 a workaround of GCC PR debug/47510. In the case of this complaint
19113 type_name_or_error will error on such types later.
19114
19115 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19116 it could not find the child DIEs referenced later, this is checked
19117 above. In correct DWARF DW_TAG_typedef should have no children. */
19118
19119 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19120 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19121 "- DIE at %s [in module %s]"),
19122 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19123
19124 /* If we're at the second level, and we're an enumerator, and
19125 our parent has no specification (meaning possibly lives in a
19126 namespace elsewhere), then we can add the partial symbol now
19127 instead of queueing it. */
19128 if (pdi.tag == DW_TAG_enumerator
19129 && parent_die != NULL
19130 && parent_die->die_parent == NULL
19131 && parent_die->tag == DW_TAG_enumeration_type
19132 && parent_die->has_specification == 0)
19133 {
19134 if (pdi.raw_name == NULL)
19135 complaint (_("malformed enumerator DIE ignored"));
19136 else if (building_psymtab)
19137 add_partial_symbol (&pdi, cu);
19138
19139 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19140 continue;
19141 }
19142
19143 struct partial_die_info *part_die
19144 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19145
19146 /* We'll save this DIE so link it in. */
19147 part_die->die_parent = parent_die;
19148 part_die->die_sibling = NULL;
19149 part_die->die_child = NULL;
19150
19151 if (last_die && last_die == parent_die)
19152 last_die->die_child = part_die;
19153 else if (last_die)
19154 last_die->die_sibling = part_die;
19155
19156 last_die = part_die;
19157
19158 if (first_die == NULL)
19159 first_die = part_die;
19160
19161 /* Maybe add the DIE to the hash table. Not all DIEs that we
19162 find interesting need to be in the hash table, because we
19163 also have the parent/sibling/child chains; only those that we
19164 might refer to by offset later during partial symbol reading.
19165
19166 For now this means things that might have be the target of a
19167 DW_AT_specification, DW_AT_abstract_origin, or
19168 DW_AT_extension. DW_AT_extension will refer only to
19169 namespaces; DW_AT_abstract_origin refers to functions (and
19170 many things under the function DIE, but we do not recurse
19171 into function DIEs during partial symbol reading) and
19172 possibly variables as well; DW_AT_specification refers to
19173 declarations. Declarations ought to have the DW_AT_declaration
19174 flag. It happens that GCC forgets to put it in sometimes, but
19175 only for functions, not for types.
19176
19177 Adding more things than necessary to the hash table is harmless
19178 except for the performance cost. Adding too few will result in
19179 wasted time in find_partial_die, when we reread the compilation
19180 unit with load_all_dies set. */
19181
19182 if (load_all
19183 || abbrev->tag == DW_TAG_constant
19184 || abbrev->tag == DW_TAG_subprogram
19185 || abbrev->tag == DW_TAG_variable
19186 || abbrev->tag == DW_TAG_namespace
19187 || part_die->is_declaration)
19188 {
19189 void **slot;
19190
19191 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19192 to_underlying (part_die->sect_off),
19193 INSERT);
19194 *slot = part_die;
19195 }
19196
19197 /* For some DIEs we want to follow their children (if any). For C
19198 we have no reason to follow the children of structures; for other
19199 languages we have to, so that we can get at method physnames
19200 to infer fully qualified class names, for DW_AT_specification,
19201 and for C++ template arguments. For C++, we also look one level
19202 inside functions to find template arguments (if the name of the
19203 function does not already contain the template arguments).
19204
19205 For Ada and Fortran, we need to scan the children of subprograms
19206 and lexical blocks as well because these languages allow the
19207 definition of nested entities that could be interesting for the
19208 debugger, such as nested subprograms for instance. */
19209 if (last_die->has_children
19210 && (load_all
19211 || last_die->tag == DW_TAG_namespace
19212 || last_die->tag == DW_TAG_module
19213 || last_die->tag == DW_TAG_enumeration_type
19214 || (cu->per_cu->lang == language_cplus
19215 && last_die->tag == DW_TAG_subprogram
19216 && (last_die->raw_name == NULL
19217 || strchr (last_die->raw_name, '<') == NULL))
19218 || (cu->per_cu->lang != language_c
19219 && (last_die->tag == DW_TAG_class_type
19220 || last_die->tag == DW_TAG_interface_type
19221 || last_die->tag == DW_TAG_structure_type
19222 || last_die->tag == DW_TAG_union_type))
19223 || ((cu->per_cu->lang == language_ada
19224 || cu->per_cu->lang == language_fortran)
19225 && (last_die->tag == DW_TAG_subprogram
19226 || last_die->tag == DW_TAG_lexical_block))))
19227 {
19228 nesting_level++;
19229 parent_die = last_die;
19230 continue;
19231 }
19232
19233 /* Otherwise we skip to the next sibling, if any. */
19234 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19235
19236 /* Back to the top, do it again. */
19237 }
19238 }
19239
19240 partial_die_info::partial_die_info (sect_offset sect_off_,
19241 const struct abbrev_info *abbrev)
19242 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19243 {
19244 }
19245
19246 /* See class definition. */
19247
19248 const char *
19249 partial_die_info::name (dwarf2_cu *cu)
19250 {
19251 if (!canonical_name && raw_name != nullptr)
19252 {
19253 struct objfile *objfile = cu->per_objfile->objfile;
19254 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19255 canonical_name = 1;
19256 }
19257
19258 return raw_name;
19259 }
19260
19261 /* Read a minimal amount of information into the minimal die structure.
19262 INFO_PTR should point just after the initial uleb128 of a DIE. */
19263
19264 const gdb_byte *
19265 partial_die_info::read (const struct die_reader_specs *reader,
19266 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19267 {
19268 struct dwarf2_cu *cu = reader->cu;
19269 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19270 unsigned int i;
19271 int has_low_pc_attr = 0;
19272 int has_high_pc_attr = 0;
19273 int high_pc_relative = 0;
19274
19275 for (i = 0; i < abbrev.num_attrs; ++i)
19276 {
19277 attribute attr;
19278 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19279 /* String and address offsets that need to do the reprocessing have
19280 already been read at this point, so there is no need to wait until
19281 the loop terminates to do the reprocessing. */
19282 if (attr.requires_reprocessing_p ())
19283 read_attribute_reprocess (reader, &attr, tag);
19284 /* Store the data if it is of an attribute we want to keep in a
19285 partial symbol table. */
19286 switch (attr.name)
19287 {
19288 case DW_AT_name:
19289 switch (tag)
19290 {
19291 case DW_TAG_compile_unit:
19292 case DW_TAG_partial_unit:
19293 case DW_TAG_type_unit:
19294 /* Compilation units have a DW_AT_name that is a filename, not
19295 a source language identifier. */
19296 case DW_TAG_enumeration_type:
19297 case DW_TAG_enumerator:
19298 /* These tags always have simple identifiers already; no need
19299 to canonicalize them. */
19300 canonical_name = 1;
19301 raw_name = attr.as_string ();
19302 break;
19303 default:
19304 canonical_name = 0;
19305 raw_name = attr.as_string ();
19306 break;
19307 }
19308 break;
19309 case DW_AT_linkage_name:
19310 case DW_AT_MIPS_linkage_name:
19311 /* Note that both forms of linkage name might appear. We
19312 assume they will be the same, and we only store the last
19313 one we see. */
19314 linkage_name = attr.as_string ();
19315 break;
19316 case DW_AT_low_pc:
19317 has_low_pc_attr = 1;
19318 lowpc = attr.as_address ();
19319 break;
19320 case DW_AT_high_pc:
19321 has_high_pc_attr = 1;
19322 highpc = attr.as_address ();
19323 if (cu->header.version >= 4 && attr.form_is_constant ())
19324 high_pc_relative = 1;
19325 break;
19326 case DW_AT_location:
19327 /* Support the .debug_loc offsets. */
19328 if (attr.form_is_block ())
19329 {
19330 d.locdesc = attr.as_block ();
19331 }
19332 else if (attr.form_is_section_offset ())
19333 {
19334 dwarf2_complex_location_expr_complaint ();
19335 }
19336 else
19337 {
19338 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19339 "partial symbol information");
19340 }
19341 break;
19342 case DW_AT_external:
19343 is_external = attr.as_boolean ();
19344 break;
19345 case DW_AT_declaration:
19346 is_declaration = attr.as_boolean ();
19347 break;
19348 case DW_AT_type:
19349 has_type = 1;
19350 break;
19351 case DW_AT_abstract_origin:
19352 case DW_AT_specification:
19353 case DW_AT_extension:
19354 has_specification = 1;
19355 spec_offset = attr.get_ref_die_offset ();
19356 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19357 || cu->per_cu->is_dwz);
19358 break;
19359 case DW_AT_sibling:
19360 /* Ignore absolute siblings, they might point outside of
19361 the current compile unit. */
19362 if (attr.form == DW_FORM_ref_addr)
19363 complaint (_("ignoring absolute DW_AT_sibling"));
19364 else
19365 {
19366 const gdb_byte *buffer = reader->buffer;
19367 sect_offset off = attr.get_ref_die_offset ();
19368 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19369
19370 if (sibling_ptr < info_ptr)
19371 complaint (_("DW_AT_sibling points backwards"));
19372 else if (sibling_ptr > reader->buffer_end)
19373 reader->die_section->overflow_complaint ();
19374 else
19375 sibling = sibling_ptr;
19376 }
19377 break;
19378 case DW_AT_byte_size:
19379 has_byte_size = 1;
19380 break;
19381 case DW_AT_const_value:
19382 has_const_value = 1;
19383 break;
19384 case DW_AT_calling_convention:
19385 /* DWARF doesn't provide a way to identify a program's source-level
19386 entry point. DW_AT_calling_convention attributes are only meant
19387 to describe functions' calling conventions.
19388
19389 However, because it's a necessary piece of information in
19390 Fortran, and before DWARF 4 DW_CC_program was the only
19391 piece of debugging information whose definition refers to
19392 a 'main program' at all, several compilers marked Fortran
19393 main programs with DW_CC_program --- even when those
19394 functions use the standard calling conventions.
19395
19396 Although DWARF now specifies a way to provide this
19397 information, we support this practice for backward
19398 compatibility. */
19399 if (attr.constant_value (0) == DW_CC_program
19400 && cu->per_cu->lang == language_fortran)
19401 main_subprogram = 1;
19402 break;
19403 case DW_AT_inline:
19404 {
19405 LONGEST value = attr.constant_value (-1);
19406 if (value == DW_INL_inlined
19407 || value == DW_INL_declared_inlined)
19408 may_be_inlined = 1;
19409 }
19410 break;
19411
19412 case DW_AT_import:
19413 if (tag == DW_TAG_imported_unit)
19414 {
19415 d.sect_off = attr.get_ref_die_offset ();
19416 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19417 || cu->per_cu->is_dwz);
19418 }
19419 break;
19420
19421 case DW_AT_main_subprogram:
19422 main_subprogram = attr.as_boolean ();
19423 break;
19424
19425 case DW_AT_ranges:
19426 {
19427 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19428 on DWARF version). */
19429 ranges_offset = attr.as_unsigned ();
19430
19431 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19432 this value. */
19433 if (tag != DW_TAG_compile_unit)
19434 ranges_offset += cu->gnu_ranges_base;
19435
19436 has_range_info = 1;
19437 }
19438 break;
19439
19440 default:
19441 break;
19442 }
19443 }
19444
19445 /* For Ada, if both the name and the linkage name appear, we prefer
19446 the latter. This lets "catch exception" work better, regardless
19447 of the order in which the name and linkage name were emitted.
19448 Really, though, this is just a workaround for the fact that gdb
19449 doesn't store both the name and the linkage name. */
19450 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
19451 raw_name = linkage_name;
19452
19453 if (high_pc_relative)
19454 highpc += lowpc;
19455
19456 if (has_low_pc_attr && has_high_pc_attr)
19457 {
19458 /* When using the GNU linker, .gnu.linkonce. sections are used to
19459 eliminate duplicate copies of functions and vtables and such.
19460 The linker will arbitrarily choose one and discard the others.
19461 The AT_*_pc values for such functions refer to local labels in
19462 these sections. If the section from that file was discarded, the
19463 labels are not in the output, so the relocs get a value of 0.
19464 If this is a discarded function, mark the pc bounds as invalid,
19465 so that GDB will ignore it. */
19466 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19467 {
19468 struct objfile *objfile = per_objfile->objfile;
19469 struct gdbarch *gdbarch = objfile->arch ();
19470
19471 complaint (_("DW_AT_low_pc %s is zero "
19472 "for DIE at %s [in module %s]"),
19473 paddress (gdbarch, lowpc),
19474 sect_offset_str (sect_off),
19475 objfile_name (objfile));
19476 }
19477 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19478 else if (lowpc >= highpc)
19479 {
19480 struct objfile *objfile = per_objfile->objfile;
19481 struct gdbarch *gdbarch = objfile->arch ();
19482
19483 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19484 "for DIE at %s [in module %s]"),
19485 paddress (gdbarch, lowpc),
19486 paddress (gdbarch, highpc),
19487 sect_offset_str (sect_off),
19488 objfile_name (objfile));
19489 }
19490 else
19491 has_pc_info = 1;
19492 }
19493
19494 return info_ptr;
19495 }
19496
19497 /* Find a cached partial DIE at OFFSET in CU. */
19498
19499 struct partial_die_info *
19500 dwarf2_cu::find_partial_die (sect_offset sect_off)
19501 {
19502 struct partial_die_info *lookup_die = NULL;
19503 struct partial_die_info part_die (sect_off);
19504
19505 lookup_die = ((struct partial_die_info *)
19506 htab_find_with_hash (partial_dies, &part_die,
19507 to_underlying (sect_off)));
19508
19509 return lookup_die;
19510 }
19511
19512 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19513 except in the case of .debug_types DIEs which do not reference
19514 outside their CU (they do however referencing other types via
19515 DW_FORM_ref_sig8). */
19516
19517 static const struct cu_partial_die_info
19518 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19519 {
19520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19521 struct objfile *objfile = per_objfile->objfile;
19522 struct partial_die_info *pd = NULL;
19523
19524 if (offset_in_dwz == cu->per_cu->is_dwz
19525 && cu->header.offset_in_cu_p (sect_off))
19526 {
19527 pd = cu->find_partial_die (sect_off);
19528 if (pd != NULL)
19529 return { cu, pd };
19530 /* We missed recording what we needed.
19531 Load all dies and try again. */
19532 }
19533 else
19534 {
19535 /* TUs don't reference other CUs/TUs (except via type signatures). */
19536 if (cu->per_cu->is_debug_types)
19537 {
19538 error (_("Dwarf Error: Type Unit at offset %s contains"
19539 " external reference to offset %s [in module %s].\n"),
19540 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19541 bfd_get_filename (objfile->obfd));
19542 }
19543 dwarf2_per_cu_data *per_cu
19544 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19545 per_objfile);
19546
19547 cu = per_objfile->get_cu (per_cu);
19548 if (cu == NULL || cu->partial_dies == NULL)
19549 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19550
19551 cu = per_objfile->get_cu (per_cu);
19552
19553 cu->last_used = 0;
19554 pd = cu->find_partial_die (sect_off);
19555 }
19556
19557 /* If we didn't find it, and not all dies have been loaded,
19558 load them all and try again. */
19559
19560 if (pd == NULL && cu->load_all_dies == 0)
19561 {
19562 cu->load_all_dies = 1;
19563
19564 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19565 THIS_CU->cu may already be in use. So we can't just free it and
19566 replace its DIEs with the ones we read in. Instead, we leave those
19567 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19568 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19569 set. */
19570 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19571
19572 pd = cu->find_partial_die (sect_off);
19573 }
19574
19575 if (pd == NULL)
19576 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19577 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19578 return { cu, pd };
19579 }
19580
19581 /* See if we can figure out if the class lives in a namespace. We do
19582 this by looking for a member function; its demangled name will
19583 contain namespace info, if there is any. */
19584
19585 static void
19586 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19587 struct dwarf2_cu *cu)
19588 {
19589 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19590 what template types look like, because the demangler
19591 frequently doesn't give the same name as the debug info. We
19592 could fix this by only using the demangled name to get the
19593 prefix (but see comment in read_structure_type). */
19594
19595 struct partial_die_info *real_pdi;
19596 struct partial_die_info *child_pdi;
19597
19598 /* If this DIE (this DIE's specification, if any) has a parent, then
19599 we should not do this. We'll prepend the parent's fully qualified
19600 name when we create the partial symbol. */
19601
19602 real_pdi = struct_pdi;
19603 while (real_pdi->has_specification)
19604 {
19605 auto res = find_partial_die (real_pdi->spec_offset,
19606 real_pdi->spec_is_dwz, cu);
19607 real_pdi = res.pdi;
19608 cu = res.cu;
19609 }
19610
19611 if (real_pdi->die_parent != NULL)
19612 return;
19613
19614 for (child_pdi = struct_pdi->die_child;
19615 child_pdi != NULL;
19616 child_pdi = child_pdi->die_sibling)
19617 {
19618 if (child_pdi->tag == DW_TAG_subprogram
19619 && child_pdi->linkage_name != NULL)
19620 {
19621 gdb::unique_xmalloc_ptr<char> actual_class_name
19622 (cu->language_defn->class_name_from_physname
19623 (child_pdi->linkage_name));
19624 if (actual_class_name != NULL)
19625 {
19626 struct objfile *objfile = cu->per_objfile->objfile;
19627 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19628 struct_pdi->canonical_name = 1;
19629 }
19630 break;
19631 }
19632 }
19633 }
19634
19635 /* Return true if a DIE with TAG may have the DW_AT_const_value
19636 attribute. */
19637
19638 static bool
19639 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19640 {
19641 switch (tag)
19642 {
19643 case DW_TAG_constant:
19644 case DW_TAG_enumerator:
19645 case DW_TAG_formal_parameter:
19646 case DW_TAG_template_value_param:
19647 case DW_TAG_variable:
19648 return true;
19649 }
19650
19651 return false;
19652 }
19653
19654 void
19655 partial_die_info::fixup (struct dwarf2_cu *cu)
19656 {
19657 /* Once we've fixed up a die, there's no point in doing so again.
19658 This also avoids a memory leak if we were to call
19659 guess_partial_die_structure_name multiple times. */
19660 if (fixup_called)
19661 return;
19662
19663 /* If we found a reference attribute and the DIE has no name, try
19664 to find a name in the referred to DIE. */
19665
19666 if (raw_name == NULL && has_specification)
19667 {
19668 struct partial_die_info *spec_die;
19669
19670 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19671 spec_die = res.pdi;
19672 cu = res.cu;
19673
19674 spec_die->fixup (cu);
19675
19676 if (spec_die->raw_name)
19677 {
19678 raw_name = spec_die->raw_name;
19679 canonical_name = spec_die->canonical_name;
19680
19681 /* Copy DW_AT_external attribute if it is set. */
19682 if (spec_die->is_external)
19683 is_external = spec_die->is_external;
19684 }
19685 }
19686
19687 if (!has_const_value && has_specification
19688 && can_have_DW_AT_const_value_p (tag))
19689 {
19690 struct partial_die_info *spec_die;
19691
19692 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19693 spec_die = res.pdi;
19694 cu = res.cu;
19695
19696 spec_die->fixup (cu);
19697
19698 if (spec_die->has_const_value)
19699 {
19700 /* Copy DW_AT_const_value attribute if it is set. */
19701 has_const_value = spec_die->has_const_value;
19702 }
19703 }
19704
19705 /* Set default names for some unnamed DIEs. */
19706
19707 if (raw_name == NULL && tag == DW_TAG_namespace)
19708 {
19709 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19710 canonical_name = 1;
19711 }
19712
19713 /* If there is no parent die to provide a namespace, and there are
19714 children, see if we can determine the namespace from their linkage
19715 name. */
19716 if (cu->per_cu->lang == language_cplus
19717 && !cu->per_objfile->per_bfd->types.empty ()
19718 && die_parent == NULL
19719 && has_children
19720 && (tag == DW_TAG_class_type
19721 || tag == DW_TAG_structure_type
19722 || tag == DW_TAG_union_type))
19723 guess_partial_die_structure_name (this, cu);
19724
19725 /* GCC might emit a nameless struct or union that has a linkage
19726 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19727 if (raw_name == NULL
19728 && (tag == DW_TAG_class_type
19729 || tag == DW_TAG_interface_type
19730 || tag == DW_TAG_structure_type
19731 || tag == DW_TAG_union_type)
19732 && linkage_name != NULL)
19733 {
19734 gdb::unique_xmalloc_ptr<char> demangled
19735 (gdb_demangle (linkage_name, DMGL_TYPES));
19736 if (demangled != nullptr)
19737 {
19738 const char *base;
19739
19740 /* Strip any leading namespaces/classes, keep only the base name.
19741 DW_AT_name for named DIEs does not contain the prefixes. */
19742 base = strrchr (demangled.get (), ':');
19743 if (base && base > demangled.get () && base[-1] == ':')
19744 base++;
19745 else
19746 base = demangled.get ();
19747
19748 struct objfile *objfile = cu->per_objfile->objfile;
19749 raw_name = objfile->intern (base);
19750 canonical_name = 1;
19751 }
19752 }
19753
19754 fixup_called = 1;
19755 }
19756
19757 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19758 contents from the given SECTION in the HEADER.
19759
19760 HEADER_OFFSET is the offset of the header in the section. */
19761 static void
19762 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19763 struct dwarf2_section_info *section,
19764 sect_offset header_offset)
19765 {
19766 unsigned int bytes_read;
19767 bfd *abfd = section->get_bfd_owner ();
19768 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19769
19770 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19771 info_ptr += bytes_read;
19772
19773 header->version = read_2_bytes (abfd, info_ptr);
19774 info_ptr += 2;
19775
19776 header->addr_size = read_1_byte (abfd, info_ptr);
19777 info_ptr += 1;
19778
19779 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19780 info_ptr += 1;
19781
19782 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19783 }
19784
19785 /* Return the DW_AT_loclists_base value for the CU. */
19786 static ULONGEST
19787 lookup_loclist_base (struct dwarf2_cu *cu)
19788 {
19789 /* For the .dwo unit, the loclist_base points to the first offset following
19790 the header. The header consists of the following entities-
19791 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19792 bit format)
19793 2. version (2 bytes)
19794 3. address size (1 byte)
19795 4. segment selector size (1 byte)
19796 5. offset entry count (4 bytes)
19797 These sizes are derived as per the DWARFv5 standard. */
19798 if (cu->dwo_unit != nullptr)
19799 {
19800 if (cu->header.initial_length_size == 4)
19801 return LOCLIST_HEADER_SIZE32;
19802 return LOCLIST_HEADER_SIZE64;
19803 }
19804 return cu->loclist_base;
19805 }
19806
19807 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19808 array of offsets in the .debug_loclists section. */
19809
19810 static sect_offset
19811 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19812 {
19813 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19814 struct objfile *objfile = per_objfile->objfile;
19815 bfd *abfd = objfile->obfd;
19816 ULONGEST loclist_header_size =
19817 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19818 : LOCLIST_HEADER_SIZE64);
19819 ULONGEST loclist_base = lookup_loclist_base (cu);
19820
19821 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19822 ULONGEST start_offset =
19823 loclist_base + loclist_index * cu->header.offset_size;
19824
19825 /* Get loclists section. */
19826 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19827
19828 /* Read the loclists section content. */
19829 section->read (objfile);
19830 if (section->buffer == NULL)
19831 error (_("DW_FORM_loclistx used without .debug_loclists "
19832 "section [in module %s]"), objfile_name (objfile));
19833
19834 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19835 so if loclist_base is smaller than the header size, we have a problem. */
19836 if (loclist_base < loclist_header_size)
19837 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19838 objfile_name (objfile));
19839
19840 /* Read the header of the loclists contribution. */
19841 struct loclists_rnglists_header header;
19842 read_loclists_rnglists_header (&header, section,
19843 (sect_offset) (loclist_base - loclist_header_size));
19844
19845 /* Verify the loclist index is valid. */
19846 if (loclist_index >= header.offset_entry_count)
19847 error (_("DW_FORM_loclistx pointing outside of "
19848 ".debug_loclists offset array [in module %s]"),
19849 objfile_name (objfile));
19850
19851 /* Validate that reading won't go beyond the end of the section. */
19852 if (start_offset + cu->header.offset_size > section->size)
19853 error (_("Reading DW_FORM_loclistx index beyond end of"
19854 ".debug_loclists section [in module %s]"),
19855 objfile_name (objfile));
19856
19857 const gdb_byte *info_ptr = section->buffer + start_offset;
19858
19859 if (cu->header.offset_size == 4)
19860 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19861 else
19862 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19863 }
19864
19865 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19866 array of offsets in the .debug_rnglists section. */
19867
19868 static sect_offset
19869 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19870 dwarf_tag tag)
19871 {
19872 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19873 struct objfile *objfile = dwarf2_per_objfile->objfile;
19874 bfd *abfd = objfile->obfd;
19875 ULONGEST rnglist_header_size =
19876 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19877 : RNGLIST_HEADER_SIZE64);
19878
19879 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19880 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19881 doesn't apply. */
19882 ULONGEST rnglist_base =
19883 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19884
19885 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19886 ULONGEST start_offset =
19887 rnglist_base + rnglist_index * cu->header.offset_size;
19888
19889 /* Get rnglists section. */
19890 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19891
19892 /* Read the rnglists section content. */
19893 section->read (objfile);
19894 if (section->buffer == nullptr)
19895 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19896 "[in module %s]"),
19897 objfile_name (objfile));
19898
19899 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19900 so if rnglist_base is smaller than the header size, we have a problem. */
19901 if (rnglist_base < rnglist_header_size)
19902 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19903 objfile_name (objfile));
19904
19905 /* Read the header of the rnglists contribution. */
19906 struct loclists_rnglists_header header;
19907 read_loclists_rnglists_header (&header, section,
19908 (sect_offset) (rnglist_base - rnglist_header_size));
19909
19910 /* Verify the rnglist index is valid. */
19911 if (rnglist_index >= header.offset_entry_count)
19912 error (_("DW_FORM_rnglistx index pointing outside of "
19913 ".debug_rnglists offset array [in module %s]"),
19914 objfile_name (objfile));
19915
19916 /* Validate that reading won't go beyond the end of the section. */
19917 if (start_offset + cu->header.offset_size > section->size)
19918 error (_("Reading DW_FORM_rnglistx index beyond end of"
19919 ".debug_rnglists section [in module %s]"),
19920 objfile_name (objfile));
19921
19922 const gdb_byte *info_ptr = section->buffer + start_offset;
19923
19924 if (cu->header.offset_size == 4)
19925 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19926 else
19927 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19928 }
19929
19930 /* Process the attributes that had to be skipped in the first round. These
19931 attributes are the ones that need str_offsets_base or addr_base attributes.
19932 They could not have been processed in the first round, because at the time
19933 the values of str_offsets_base or addr_base may not have been known. */
19934 static void
19935 read_attribute_reprocess (const struct die_reader_specs *reader,
19936 struct attribute *attr, dwarf_tag tag)
19937 {
19938 struct dwarf2_cu *cu = reader->cu;
19939 switch (attr->form)
19940 {
19941 case DW_FORM_addrx:
19942 case DW_FORM_GNU_addr_index:
19943 attr->set_address (read_addr_index (cu,
19944 attr->as_unsigned_reprocess ()));
19945 break;
19946 case DW_FORM_loclistx:
19947 {
19948 sect_offset loclists_sect_off
19949 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19950
19951 attr->set_unsigned (to_underlying (loclists_sect_off));
19952 }
19953 break;
19954 case DW_FORM_rnglistx:
19955 {
19956 sect_offset rnglists_sect_off
19957 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19958
19959 attr->set_unsigned (to_underlying (rnglists_sect_off));
19960 }
19961 break;
19962 case DW_FORM_strx:
19963 case DW_FORM_strx1:
19964 case DW_FORM_strx2:
19965 case DW_FORM_strx3:
19966 case DW_FORM_strx4:
19967 case DW_FORM_GNU_str_index:
19968 {
19969 unsigned int str_index = attr->as_unsigned_reprocess ();
19970 gdb_assert (!attr->canonical_string_p ());
19971 if (reader->dwo_file != NULL)
19972 attr->set_string_noncanonical (read_dwo_str_index (reader,
19973 str_index));
19974 else
19975 attr->set_string_noncanonical (read_stub_str_index (cu,
19976 str_index));
19977 break;
19978 }
19979 default:
19980 gdb_assert_not_reached (_("Unexpected DWARF form."));
19981 }
19982 }
19983
19984 /* Read an attribute value described by an attribute form. */
19985
19986 static const gdb_byte *
19987 read_attribute_value (const struct die_reader_specs *reader,
19988 struct attribute *attr, unsigned form,
19989 LONGEST implicit_const, const gdb_byte *info_ptr)
19990 {
19991 struct dwarf2_cu *cu = reader->cu;
19992 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19993 struct objfile *objfile = per_objfile->objfile;
19994 bfd *abfd = reader->abfd;
19995 struct comp_unit_head *cu_header = &cu->header;
19996 unsigned int bytes_read;
19997 struct dwarf_block *blk;
19998
19999 attr->form = (enum dwarf_form) form;
20000 switch (form)
20001 {
20002 case DW_FORM_ref_addr:
20003 if (cu_header->version == 2)
20004 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20005 &bytes_read));
20006 else
20007 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20008 &bytes_read));
20009 info_ptr += bytes_read;
20010 break;
20011 case DW_FORM_GNU_ref_alt:
20012 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20013 &bytes_read));
20014 info_ptr += bytes_read;
20015 break;
20016 case DW_FORM_addr:
20017 {
20018 struct gdbarch *gdbarch = objfile->arch ();
20019 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20020 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20021 attr->set_address (addr);
20022 info_ptr += bytes_read;
20023 }
20024 break;
20025 case DW_FORM_block2:
20026 blk = dwarf_alloc_block (cu);
20027 blk->size = read_2_bytes (abfd, info_ptr);
20028 info_ptr += 2;
20029 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20030 info_ptr += blk->size;
20031 attr->set_block (blk);
20032 break;
20033 case DW_FORM_block4:
20034 blk = dwarf_alloc_block (cu);
20035 blk->size = read_4_bytes (abfd, info_ptr);
20036 info_ptr += 4;
20037 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20038 info_ptr += blk->size;
20039 attr->set_block (blk);
20040 break;
20041 case DW_FORM_data2:
20042 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20043 info_ptr += 2;
20044 break;
20045 case DW_FORM_data4:
20046 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20047 info_ptr += 4;
20048 break;
20049 case DW_FORM_data8:
20050 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20051 info_ptr += 8;
20052 break;
20053 case DW_FORM_data16:
20054 blk = dwarf_alloc_block (cu);
20055 blk->size = 16;
20056 blk->data = read_n_bytes (abfd, info_ptr, 16);
20057 info_ptr += 16;
20058 attr->set_block (blk);
20059 break;
20060 case DW_FORM_sec_offset:
20061 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20062 &bytes_read));
20063 info_ptr += bytes_read;
20064 break;
20065 case DW_FORM_loclistx:
20066 {
20067 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20068 &bytes_read));
20069 info_ptr += bytes_read;
20070 }
20071 break;
20072 case DW_FORM_string:
20073 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20074 &bytes_read));
20075 info_ptr += bytes_read;
20076 break;
20077 case DW_FORM_strp:
20078 if (!cu->per_cu->is_dwz)
20079 {
20080 attr->set_string_noncanonical
20081 (read_indirect_string (per_objfile,
20082 abfd, info_ptr, cu_header,
20083 &bytes_read));
20084 info_ptr += bytes_read;
20085 break;
20086 }
20087 /* FALLTHROUGH */
20088 case DW_FORM_line_strp:
20089 if (!cu->per_cu->is_dwz)
20090 {
20091 attr->set_string_noncanonical
20092 (per_objfile->read_line_string (info_ptr, cu_header,
20093 &bytes_read));
20094 info_ptr += bytes_read;
20095 break;
20096 }
20097 /* FALLTHROUGH */
20098 case DW_FORM_GNU_strp_alt:
20099 {
20100 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20101 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20102 &bytes_read);
20103
20104 attr->set_string_noncanonical
20105 (dwz->read_string (objfile, str_offset));
20106 info_ptr += bytes_read;
20107 }
20108 break;
20109 case DW_FORM_exprloc:
20110 case DW_FORM_block:
20111 blk = dwarf_alloc_block (cu);
20112 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20113 info_ptr += bytes_read;
20114 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20115 info_ptr += blk->size;
20116 attr->set_block (blk);
20117 break;
20118 case DW_FORM_block1:
20119 blk = dwarf_alloc_block (cu);
20120 blk->size = read_1_byte (abfd, info_ptr);
20121 info_ptr += 1;
20122 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20123 info_ptr += blk->size;
20124 attr->set_block (blk);
20125 break;
20126 case DW_FORM_data1:
20127 case DW_FORM_flag:
20128 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20129 info_ptr += 1;
20130 break;
20131 case DW_FORM_flag_present:
20132 attr->set_unsigned (1);
20133 break;
20134 case DW_FORM_sdata:
20135 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20136 info_ptr += bytes_read;
20137 break;
20138 case DW_FORM_rnglistx:
20139 {
20140 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20141 &bytes_read));
20142 info_ptr += bytes_read;
20143 }
20144 break;
20145 case DW_FORM_udata:
20146 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20147 info_ptr += bytes_read;
20148 break;
20149 case DW_FORM_ref1:
20150 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20151 + read_1_byte (abfd, info_ptr)));
20152 info_ptr += 1;
20153 break;
20154 case DW_FORM_ref2:
20155 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20156 + read_2_bytes (abfd, info_ptr)));
20157 info_ptr += 2;
20158 break;
20159 case DW_FORM_ref4:
20160 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20161 + read_4_bytes (abfd, info_ptr)));
20162 info_ptr += 4;
20163 break;
20164 case DW_FORM_ref8:
20165 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20166 + read_8_bytes (abfd, info_ptr)));
20167 info_ptr += 8;
20168 break;
20169 case DW_FORM_ref_sig8:
20170 attr->set_signature (read_8_bytes (abfd, info_ptr));
20171 info_ptr += 8;
20172 break;
20173 case DW_FORM_ref_udata:
20174 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20175 + read_unsigned_leb128 (abfd, info_ptr,
20176 &bytes_read)));
20177 info_ptr += bytes_read;
20178 break;
20179 case DW_FORM_indirect:
20180 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20181 info_ptr += bytes_read;
20182 if (form == DW_FORM_implicit_const)
20183 {
20184 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20185 info_ptr += bytes_read;
20186 }
20187 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20188 info_ptr);
20189 break;
20190 case DW_FORM_implicit_const:
20191 attr->set_signed (implicit_const);
20192 break;
20193 case DW_FORM_addrx:
20194 case DW_FORM_GNU_addr_index:
20195 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20196 &bytes_read));
20197 info_ptr += bytes_read;
20198 break;
20199 case DW_FORM_strx:
20200 case DW_FORM_strx1:
20201 case DW_FORM_strx2:
20202 case DW_FORM_strx3:
20203 case DW_FORM_strx4:
20204 case DW_FORM_GNU_str_index:
20205 {
20206 ULONGEST str_index;
20207 if (form == DW_FORM_strx1)
20208 {
20209 str_index = read_1_byte (abfd, info_ptr);
20210 info_ptr += 1;
20211 }
20212 else if (form == DW_FORM_strx2)
20213 {
20214 str_index = read_2_bytes (abfd, info_ptr);
20215 info_ptr += 2;
20216 }
20217 else if (form == DW_FORM_strx3)
20218 {
20219 str_index = read_3_bytes (abfd, info_ptr);
20220 info_ptr += 3;
20221 }
20222 else if (form == DW_FORM_strx4)
20223 {
20224 str_index = read_4_bytes (abfd, info_ptr);
20225 info_ptr += 4;
20226 }
20227 else
20228 {
20229 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20230 info_ptr += bytes_read;
20231 }
20232 attr->set_unsigned_reprocess (str_index);
20233 }
20234 break;
20235 default:
20236 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20237 dwarf_form_name (form),
20238 bfd_get_filename (abfd));
20239 }
20240
20241 /* Super hack. */
20242 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20243 attr->form = DW_FORM_GNU_ref_alt;
20244
20245 /* We have seen instances where the compiler tried to emit a byte
20246 size attribute of -1 which ended up being encoded as an unsigned
20247 0xffffffff. Although 0xffffffff is technically a valid size value,
20248 an object of this size seems pretty unlikely so we can relatively
20249 safely treat these cases as if the size attribute was invalid and
20250 treat them as zero by default. */
20251 if (attr->name == DW_AT_byte_size
20252 && form == DW_FORM_data4
20253 && attr->as_unsigned () >= 0xffffffff)
20254 {
20255 complaint
20256 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20257 hex_string (attr->as_unsigned ()));
20258 attr->set_unsigned (0);
20259 }
20260
20261 return info_ptr;
20262 }
20263
20264 /* Read an attribute described by an abbreviated attribute. */
20265
20266 static const gdb_byte *
20267 read_attribute (const struct die_reader_specs *reader,
20268 struct attribute *attr, const struct attr_abbrev *abbrev,
20269 const gdb_byte *info_ptr)
20270 {
20271 attr->name = abbrev->name;
20272 attr->string_is_canonical = 0;
20273 attr->requires_reprocessing = 0;
20274 return read_attribute_value (reader, attr, abbrev->form,
20275 abbrev->implicit_const, info_ptr);
20276 }
20277
20278 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20279
20280 static const char *
20281 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20282 LONGEST str_offset)
20283 {
20284 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20285 str_offset, "DW_FORM_strp");
20286 }
20287
20288 /* Return pointer to string at .debug_str offset as read from BUF.
20289 BUF is assumed to be in a compilation unit described by CU_HEADER.
20290 Return *BYTES_READ_PTR count of bytes read from BUF. */
20291
20292 static const char *
20293 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20294 const gdb_byte *buf,
20295 const struct comp_unit_head *cu_header,
20296 unsigned int *bytes_read_ptr)
20297 {
20298 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20299
20300 return read_indirect_string_at_offset (per_objfile, str_offset);
20301 }
20302
20303 /* See read.h. */
20304
20305 const char *
20306 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20307 const struct comp_unit_head *cu_header,
20308 unsigned int *bytes_read_ptr)
20309 {
20310 bfd *abfd = objfile->obfd;
20311 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20312
20313 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20314 }
20315
20316 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20317 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20318 ADDR_SIZE is the size of addresses from the CU header. */
20319
20320 static CORE_ADDR
20321 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20322 gdb::optional<ULONGEST> addr_base, int addr_size)
20323 {
20324 struct objfile *objfile = per_objfile->objfile;
20325 bfd *abfd = objfile->obfd;
20326 const gdb_byte *info_ptr;
20327 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20328
20329 per_objfile->per_bfd->addr.read (objfile);
20330 if (per_objfile->per_bfd->addr.buffer == NULL)
20331 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20332 objfile_name (objfile));
20333 if (addr_base_or_zero + addr_index * addr_size
20334 >= per_objfile->per_bfd->addr.size)
20335 error (_("DW_FORM_addr_index pointing outside of "
20336 ".debug_addr section [in module %s]"),
20337 objfile_name (objfile));
20338 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20339 + addr_index * addr_size);
20340 if (addr_size == 4)
20341 return bfd_get_32 (abfd, info_ptr);
20342 else
20343 return bfd_get_64 (abfd, info_ptr);
20344 }
20345
20346 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20347
20348 static CORE_ADDR
20349 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20350 {
20351 return read_addr_index_1 (cu->per_objfile, addr_index,
20352 cu->addr_base, cu->header.addr_size);
20353 }
20354
20355 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20356
20357 static CORE_ADDR
20358 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20359 unsigned int *bytes_read)
20360 {
20361 bfd *abfd = cu->per_objfile->objfile->obfd;
20362 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20363
20364 return read_addr_index (cu, addr_index);
20365 }
20366
20367 /* See read.h. */
20368
20369 CORE_ADDR
20370 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20371 dwarf2_per_objfile *per_objfile,
20372 unsigned int addr_index)
20373 {
20374 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20375 gdb::optional<ULONGEST> addr_base;
20376 int addr_size;
20377
20378 /* We need addr_base and addr_size.
20379 If we don't have PER_CU->cu, we have to get it.
20380 Nasty, but the alternative is storing the needed info in PER_CU,
20381 which at this point doesn't seem justified: it's not clear how frequently
20382 it would get used and it would increase the size of every PER_CU.
20383 Entry points like dwarf2_per_cu_addr_size do a similar thing
20384 so we're not in uncharted territory here.
20385 Alas we need to be a bit more complicated as addr_base is contained
20386 in the DIE.
20387
20388 We don't need to read the entire CU(/TU).
20389 We just need the header and top level die.
20390
20391 IWBN to use the aging mechanism to let us lazily later discard the CU.
20392 For now we skip this optimization. */
20393
20394 if (cu != NULL)
20395 {
20396 addr_base = cu->addr_base;
20397 addr_size = cu->header.addr_size;
20398 }
20399 else
20400 {
20401 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20402 addr_base = reader.cu->addr_base;
20403 addr_size = reader.cu->header.addr_size;
20404 }
20405
20406 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20407 }
20408
20409 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20410 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20411 DWO file. */
20412
20413 static const char *
20414 read_str_index (struct dwarf2_cu *cu,
20415 struct dwarf2_section_info *str_section,
20416 struct dwarf2_section_info *str_offsets_section,
20417 ULONGEST str_offsets_base, ULONGEST str_index)
20418 {
20419 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20420 struct objfile *objfile = per_objfile->objfile;
20421 const char *objf_name = objfile_name (objfile);
20422 bfd *abfd = objfile->obfd;
20423 const gdb_byte *info_ptr;
20424 ULONGEST str_offset;
20425 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20426
20427 str_section->read (objfile);
20428 str_offsets_section->read (objfile);
20429 if (str_section->buffer == NULL)
20430 error (_("%s used without %s section"
20431 " in CU at offset %s [in module %s]"),
20432 form_name, str_section->get_name (),
20433 sect_offset_str (cu->header.sect_off), objf_name);
20434 if (str_offsets_section->buffer == NULL)
20435 error (_("%s used without %s section"
20436 " in CU at offset %s [in module %s]"),
20437 form_name, str_section->get_name (),
20438 sect_offset_str (cu->header.sect_off), objf_name);
20439 info_ptr = (str_offsets_section->buffer
20440 + str_offsets_base
20441 + str_index * cu->header.offset_size);
20442 if (cu->header.offset_size == 4)
20443 str_offset = bfd_get_32 (abfd, info_ptr);
20444 else
20445 str_offset = bfd_get_64 (abfd, info_ptr);
20446 if (str_offset >= str_section->size)
20447 error (_("Offset from %s pointing outside of"
20448 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20449 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20450 return (const char *) (str_section->buffer + str_offset);
20451 }
20452
20453 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20454
20455 static const char *
20456 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20457 {
20458 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20459 ? reader->cu->header.addr_size : 0;
20460 return read_str_index (reader->cu,
20461 &reader->dwo_file->sections.str,
20462 &reader->dwo_file->sections.str_offsets,
20463 str_offsets_base, str_index);
20464 }
20465
20466 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20467
20468 static const char *
20469 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20470 {
20471 struct objfile *objfile = cu->per_objfile->objfile;
20472 const char *objf_name = objfile_name (objfile);
20473 static const char form_name[] = "DW_FORM_GNU_str_index";
20474 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20475
20476 if (!cu->str_offsets_base.has_value ())
20477 error (_("%s used in Fission stub without %s"
20478 " in CU at offset 0x%lx [in module %s]"),
20479 form_name, str_offsets_attr_name,
20480 (long) cu->header.offset_size, objf_name);
20481
20482 return read_str_index (cu,
20483 &cu->per_objfile->per_bfd->str,
20484 &cu->per_objfile->per_bfd->str_offsets,
20485 *cu->str_offsets_base, str_index);
20486 }
20487
20488 /* Return the length of an LEB128 number in BUF. */
20489
20490 static int
20491 leb128_size (const gdb_byte *buf)
20492 {
20493 const gdb_byte *begin = buf;
20494 gdb_byte byte;
20495
20496 while (1)
20497 {
20498 byte = *buf++;
20499 if ((byte & 128) == 0)
20500 return buf - begin;
20501 }
20502 }
20503
20504 static enum language
20505 dwarf_lang_to_enum_language (unsigned int lang)
20506 {
20507 enum language language;
20508
20509 switch (lang)
20510 {
20511 case DW_LANG_C89:
20512 case DW_LANG_C99:
20513 case DW_LANG_C11:
20514 case DW_LANG_C:
20515 case DW_LANG_UPC:
20516 language = language_c;
20517 break;
20518 case DW_LANG_Java:
20519 case DW_LANG_C_plus_plus:
20520 case DW_LANG_C_plus_plus_11:
20521 case DW_LANG_C_plus_plus_14:
20522 language = language_cplus;
20523 break;
20524 case DW_LANG_D:
20525 language = language_d;
20526 break;
20527 case DW_LANG_Fortran77:
20528 case DW_LANG_Fortran90:
20529 case DW_LANG_Fortran95:
20530 case DW_LANG_Fortran03:
20531 case DW_LANG_Fortran08:
20532 language = language_fortran;
20533 break;
20534 case DW_LANG_Go:
20535 language = language_go;
20536 break;
20537 case DW_LANG_Mips_Assembler:
20538 language = language_asm;
20539 break;
20540 case DW_LANG_Ada83:
20541 case DW_LANG_Ada95:
20542 language = language_ada;
20543 break;
20544 case DW_LANG_Modula2:
20545 language = language_m2;
20546 break;
20547 case DW_LANG_Pascal83:
20548 language = language_pascal;
20549 break;
20550 case DW_LANG_ObjC:
20551 language = language_objc;
20552 break;
20553 case DW_LANG_Rust:
20554 case DW_LANG_Rust_old:
20555 language = language_rust;
20556 break;
20557 case DW_LANG_OpenCL:
20558 language = language_opencl;
20559 break;
20560 case DW_LANG_Cobol74:
20561 case DW_LANG_Cobol85:
20562 default:
20563 language = language_minimal;
20564 break;
20565 }
20566
20567 return language;
20568 }
20569
20570 /* Return the named attribute or NULL if not there. */
20571
20572 static struct attribute *
20573 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20574 {
20575 for (;;)
20576 {
20577 unsigned int i;
20578 struct attribute *spec = NULL;
20579
20580 for (i = 0; i < die->num_attrs; ++i)
20581 {
20582 if (die->attrs[i].name == name)
20583 return &die->attrs[i];
20584 if (die->attrs[i].name == DW_AT_specification
20585 || die->attrs[i].name == DW_AT_abstract_origin)
20586 spec = &die->attrs[i];
20587 }
20588
20589 if (!spec)
20590 break;
20591
20592 die = follow_die_ref (die, spec, &cu);
20593 }
20594
20595 return NULL;
20596 }
20597
20598 /* Return the string associated with a string-typed attribute, or NULL if it
20599 is either not found or is of an incorrect type. */
20600
20601 static const char *
20602 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20603 {
20604 struct attribute *attr;
20605 const char *str = NULL;
20606
20607 attr = dwarf2_attr (die, name, cu);
20608
20609 if (attr != NULL)
20610 {
20611 str = attr->as_string ();
20612 if (str == nullptr)
20613 complaint (_("string type expected for attribute %s for "
20614 "DIE at %s in module %s"),
20615 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20616 objfile_name (cu->per_objfile->objfile));
20617 }
20618
20619 return str;
20620 }
20621
20622 /* Return the dwo name or NULL if not present. If present, it is in either
20623 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20624 static const char *
20625 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20626 {
20627 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20628 if (dwo_name == nullptr)
20629 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20630 return dwo_name;
20631 }
20632
20633 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20634 and holds a non-zero value. This function should only be used for
20635 DW_FORM_flag or DW_FORM_flag_present attributes. */
20636
20637 static int
20638 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20639 {
20640 struct attribute *attr = dwarf2_attr (die, name, cu);
20641
20642 return attr != nullptr && attr->as_boolean ();
20643 }
20644
20645 static int
20646 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20647 {
20648 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20649 which value is non-zero. However, we have to be careful with
20650 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20651 (via dwarf2_flag_true_p) follows this attribute. So we may
20652 end up accidently finding a declaration attribute that belongs
20653 to a different DIE referenced by the specification attribute,
20654 even though the given DIE does not have a declaration attribute. */
20655 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20656 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20657 }
20658
20659 /* Return the die giving the specification for DIE, if there is
20660 one. *SPEC_CU is the CU containing DIE on input, and the CU
20661 containing the return value on output. If there is no
20662 specification, but there is an abstract origin, that is
20663 returned. */
20664
20665 static struct die_info *
20666 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20667 {
20668 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20669 *spec_cu);
20670
20671 if (spec_attr == NULL)
20672 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20673
20674 if (spec_attr == NULL)
20675 return NULL;
20676 else
20677 return follow_die_ref (die, spec_attr, spec_cu);
20678 }
20679
20680 /* A convenience function to find the proper .debug_line section for a CU. */
20681
20682 static struct dwarf2_section_info *
20683 get_debug_line_section (struct dwarf2_cu *cu)
20684 {
20685 struct dwarf2_section_info *section;
20686 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20687
20688 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20689 DWO file. */
20690 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20691 section = &cu->dwo_unit->dwo_file->sections.line;
20692 else if (cu->per_cu->is_dwz)
20693 {
20694 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20695
20696 section = &dwz->line;
20697 }
20698 else
20699 section = &per_objfile->per_bfd->line;
20700
20701 return section;
20702 }
20703
20704 /* Read the statement program header starting at OFFSET in
20705 .debug_line, or .debug_line.dwo. Return a pointer
20706 to a struct line_header, allocated using xmalloc.
20707 Returns NULL if there is a problem reading the header, e.g., if it
20708 has a version we don't understand.
20709
20710 NOTE: the strings in the include directory and file name tables of
20711 the returned object point into the dwarf line section buffer,
20712 and must not be freed. */
20713
20714 static line_header_up
20715 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20716 {
20717 struct dwarf2_section_info *section;
20718 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20719
20720 section = get_debug_line_section (cu);
20721 section->read (per_objfile->objfile);
20722 if (section->buffer == NULL)
20723 {
20724 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20725 complaint (_("missing .debug_line.dwo section"));
20726 else
20727 complaint (_("missing .debug_line section"));
20728 return 0;
20729 }
20730
20731 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20732 per_objfile, section, &cu->header);
20733 }
20734
20735 /* Subroutine of dwarf_decode_lines to simplify it.
20736 Return the file name for the given file_entry.
20737 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
20738 If space for the result is malloc'd, *NAME_HOLDER will be set.
20739 Returns NULL if FILE_INDEX should be ignored, i.e., it is
20740 equivalent to CU_INFO. */
20741
20742 static const char *
20743 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
20744 const file_and_directory &cu_info,
20745 gdb::unique_xmalloc_ptr<char> *name_holder)
20746 {
20747 const char *include_name = fe.name;
20748 const char *include_name_to_compare = include_name;
20749
20750 const char *dir_name = fe.include_dir (lh);
20751
20752 gdb::unique_xmalloc_ptr<char> hold_compare;
20753 if (!IS_ABSOLUTE_PATH (include_name)
20754 && (dir_name != NULL || cu_info.comp_dir != NULL))
20755 {
20756 /* Avoid creating a duplicate name for CU_INFO.
20757 We do this by comparing INCLUDE_NAME and CU_INFO.
20758 Before we do the comparison, however, we need to account
20759 for DIR_NAME and COMP_DIR.
20760 First prepend dir_name (if non-NULL). If we still don't
20761 have an absolute path prepend comp_dir (if non-NULL).
20762 However, the directory we record in the include-file's
20763 psymtab does not contain COMP_DIR (to match the
20764 corresponding symtab(s)).
20765
20766 Example:
20767
20768 bash$ cd /tmp
20769 bash$ gcc -g ./hello.c
20770 include_name = "hello.c"
20771 dir_name = "."
20772 DW_AT_comp_dir = comp_dir = "/tmp"
20773 DW_AT_name = "./hello.c"
20774
20775 */
20776
20777 if (dir_name != NULL)
20778 {
20779 name_holder->reset (concat (dir_name, SLASH_STRING,
20780 include_name, (char *) NULL));
20781 include_name = name_holder->get ();
20782 include_name_to_compare = include_name;
20783 }
20784 if (!IS_ABSOLUTE_PATH (include_name) && cu_info.comp_dir != nullptr)
20785 {
20786 hold_compare.reset (concat (cu_info.comp_dir, SLASH_STRING,
20787 include_name, (char *) NULL));
20788 include_name_to_compare = hold_compare.get ();
20789 }
20790 }
20791
20792 gdb::unique_xmalloc_ptr<char> copied_name;
20793 const char *cu_filename = cu_info.name;
20794 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.comp_dir != nullptr)
20795 {
20796 copied_name.reset (concat (cu_info.comp_dir, SLASH_STRING,
20797 cu_filename, (char *) NULL));
20798 cu_filename = copied_name.get ();
20799 }
20800
20801 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
20802 return nullptr;
20803 return include_name;
20804 }
20805
20806 /* State machine to track the state of the line number program. */
20807
20808 class lnp_state_machine
20809 {
20810 public:
20811 /* Initialize a machine state for the start of a line number
20812 program. */
20813 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20814 bool record_lines_p);
20815
20816 file_entry *current_file ()
20817 {
20818 /* lh->file_names is 0-based, but the file name numbers in the
20819 statement program are 1-based. */
20820 return m_line_header->file_name_at (m_file);
20821 }
20822
20823 /* Record the line in the state machine. END_SEQUENCE is true if
20824 we're processing the end of a sequence. */
20825 void record_line (bool end_sequence);
20826
20827 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20828 nop-out rest of the lines in this sequence. */
20829 void check_line_address (struct dwarf2_cu *cu,
20830 const gdb_byte *line_ptr,
20831 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20832
20833 void handle_set_discriminator (unsigned int discriminator)
20834 {
20835 m_discriminator = discriminator;
20836 m_line_has_non_zero_discriminator |= discriminator != 0;
20837 }
20838
20839 /* Handle DW_LNE_set_address. */
20840 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20841 {
20842 m_op_index = 0;
20843 address += baseaddr;
20844 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20845 }
20846
20847 /* Handle DW_LNS_advance_pc. */
20848 void handle_advance_pc (CORE_ADDR adjust);
20849
20850 /* Handle a special opcode. */
20851 void handle_special_opcode (unsigned char op_code);
20852
20853 /* Handle DW_LNS_advance_line. */
20854 void handle_advance_line (int line_delta)
20855 {
20856 advance_line (line_delta);
20857 }
20858
20859 /* Handle DW_LNS_set_file. */
20860 void handle_set_file (file_name_index file);
20861
20862 /* Handle DW_LNS_negate_stmt. */
20863 void handle_negate_stmt ()
20864 {
20865 m_is_stmt = !m_is_stmt;
20866 }
20867
20868 /* Handle DW_LNS_const_add_pc. */
20869 void handle_const_add_pc ();
20870
20871 /* Handle DW_LNS_fixed_advance_pc. */
20872 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20873 {
20874 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20875 m_op_index = 0;
20876 }
20877
20878 /* Handle DW_LNS_copy. */
20879 void handle_copy ()
20880 {
20881 record_line (false);
20882 m_discriminator = 0;
20883 }
20884
20885 /* Handle DW_LNE_end_sequence. */
20886 void handle_end_sequence ()
20887 {
20888 m_currently_recording_lines = true;
20889 }
20890
20891 private:
20892 /* Advance the line by LINE_DELTA. */
20893 void advance_line (int line_delta)
20894 {
20895 m_line += line_delta;
20896
20897 if (line_delta != 0)
20898 m_line_has_non_zero_discriminator = m_discriminator != 0;
20899 }
20900
20901 struct dwarf2_cu *m_cu;
20902
20903 gdbarch *m_gdbarch;
20904
20905 /* True if we're recording lines.
20906 Otherwise we're building partial symtabs and are just interested in
20907 finding include files mentioned by the line number program. */
20908 bool m_record_lines_p;
20909
20910 /* The line number header. */
20911 line_header *m_line_header;
20912
20913 /* These are part of the standard DWARF line number state machine,
20914 and initialized according to the DWARF spec. */
20915
20916 unsigned char m_op_index = 0;
20917 /* The line table index of the current file. */
20918 file_name_index m_file = 1;
20919 unsigned int m_line = 1;
20920
20921 /* These are initialized in the constructor. */
20922
20923 CORE_ADDR m_address;
20924 bool m_is_stmt;
20925 unsigned int m_discriminator;
20926
20927 /* Additional bits of state we need to track. */
20928
20929 /* The last file that we called dwarf2_start_subfile for.
20930 This is only used for TLLs. */
20931 unsigned int m_last_file = 0;
20932 /* The last file a line number was recorded for. */
20933 struct subfile *m_last_subfile = NULL;
20934
20935 /* The address of the last line entry. */
20936 CORE_ADDR m_last_address;
20937
20938 /* Set to true when a previous line at the same address (using
20939 m_last_address) had m_is_stmt true. This is reset to false when a
20940 line entry at a new address (m_address different to m_last_address) is
20941 processed. */
20942 bool m_stmt_at_address = false;
20943
20944 /* When true, record the lines we decode. */
20945 bool m_currently_recording_lines = false;
20946
20947 /* The last line number that was recorded, used to coalesce
20948 consecutive entries for the same line. This can happen, for
20949 example, when discriminators are present. PR 17276. */
20950 unsigned int m_last_line = 0;
20951 bool m_line_has_non_zero_discriminator = false;
20952 };
20953
20954 void
20955 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20956 {
20957 CORE_ADDR addr_adj = (((m_op_index + adjust)
20958 / m_line_header->maximum_ops_per_instruction)
20959 * m_line_header->minimum_instruction_length);
20960 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20961 m_op_index = ((m_op_index + adjust)
20962 % m_line_header->maximum_ops_per_instruction);
20963 }
20964
20965 void
20966 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20967 {
20968 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20969 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20970 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20971 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20972 / m_line_header->maximum_ops_per_instruction)
20973 * m_line_header->minimum_instruction_length);
20974 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20975 m_op_index = ((m_op_index + adj_opcode_d)
20976 % m_line_header->maximum_ops_per_instruction);
20977
20978 int line_delta = m_line_header->line_base + adj_opcode_r;
20979 advance_line (line_delta);
20980 record_line (false);
20981 m_discriminator = 0;
20982 }
20983
20984 void
20985 lnp_state_machine::handle_set_file (file_name_index file)
20986 {
20987 m_file = file;
20988
20989 const file_entry *fe = current_file ();
20990 if (fe == NULL)
20991 dwarf2_debug_line_missing_file_complaint ();
20992 else if (m_record_lines_p)
20993 {
20994 const char *dir = fe->include_dir (m_line_header);
20995
20996 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20997 m_line_has_non_zero_discriminator = m_discriminator != 0;
20998 dwarf2_start_subfile (m_cu, fe->name, dir);
20999 }
21000 }
21001
21002 void
21003 lnp_state_machine::handle_const_add_pc ()
21004 {
21005 CORE_ADDR adjust
21006 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21007
21008 CORE_ADDR addr_adj
21009 = (((m_op_index + adjust)
21010 / m_line_header->maximum_ops_per_instruction)
21011 * m_line_header->minimum_instruction_length);
21012
21013 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21014 m_op_index = ((m_op_index + adjust)
21015 % m_line_header->maximum_ops_per_instruction);
21016 }
21017
21018 /* Return non-zero if we should add LINE to the line number table.
21019 LINE is the line to add, LAST_LINE is the last line that was added,
21020 LAST_SUBFILE is the subfile for LAST_LINE.
21021 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21022 had a non-zero discriminator.
21023
21024 We have to be careful in the presence of discriminators.
21025 E.g., for this line:
21026
21027 for (i = 0; i < 100000; i++);
21028
21029 clang can emit four line number entries for that one line,
21030 each with a different discriminator.
21031 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21032
21033 However, we want gdb to coalesce all four entries into one.
21034 Otherwise the user could stepi into the middle of the line and
21035 gdb would get confused about whether the pc really was in the
21036 middle of the line.
21037
21038 Things are further complicated by the fact that two consecutive
21039 line number entries for the same line is a heuristic used by gcc
21040 to denote the end of the prologue. So we can't just discard duplicate
21041 entries, we have to be selective about it. The heuristic we use is
21042 that we only collapse consecutive entries for the same line if at least
21043 one of those entries has a non-zero discriminator. PR 17276.
21044
21045 Note: Addresses in the line number state machine can never go backwards
21046 within one sequence, thus this coalescing is ok. */
21047
21048 static int
21049 dwarf_record_line_p (struct dwarf2_cu *cu,
21050 unsigned int line, unsigned int last_line,
21051 int line_has_non_zero_discriminator,
21052 struct subfile *last_subfile)
21053 {
21054 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21055 return 1;
21056 if (line != last_line)
21057 return 1;
21058 /* Same line for the same file that we've seen already.
21059 As a last check, for pr 17276, only record the line if the line
21060 has never had a non-zero discriminator. */
21061 if (!line_has_non_zero_discriminator)
21062 return 1;
21063 return 0;
21064 }
21065
21066 /* Use the CU's builder to record line number LINE beginning at
21067 address ADDRESS in the line table of subfile SUBFILE. */
21068
21069 static void
21070 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21071 unsigned int line, CORE_ADDR address, bool is_stmt,
21072 struct dwarf2_cu *cu)
21073 {
21074 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21075
21076 if (dwarf_line_debug)
21077 {
21078 fprintf_unfiltered (gdb_stdlog,
21079 "Recording line %u, file %s, address %s\n",
21080 line, lbasename (subfile->name),
21081 paddress (gdbarch, address));
21082 }
21083
21084 if (cu != nullptr)
21085 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21086 }
21087
21088 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21089 Mark the end of a set of line number records.
21090 The arguments are the same as for dwarf_record_line_1.
21091 If SUBFILE is NULL the request is ignored. */
21092
21093 static void
21094 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21095 CORE_ADDR address, struct dwarf2_cu *cu)
21096 {
21097 if (subfile == NULL)
21098 return;
21099
21100 if (dwarf_line_debug)
21101 {
21102 fprintf_unfiltered (gdb_stdlog,
21103 "Finishing current line, file %s, address %s\n",
21104 lbasename (subfile->name),
21105 paddress (gdbarch, address));
21106 }
21107
21108 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21109 }
21110
21111 void
21112 lnp_state_machine::record_line (bool end_sequence)
21113 {
21114 if (dwarf_line_debug)
21115 {
21116 fprintf_unfiltered (gdb_stdlog,
21117 "Processing actual line %u: file %u,"
21118 " address %s, is_stmt %u, discrim %u%s\n",
21119 m_line, m_file,
21120 paddress (m_gdbarch, m_address),
21121 m_is_stmt, m_discriminator,
21122 (end_sequence ? "\t(end sequence)" : ""));
21123 }
21124
21125 file_entry *fe = current_file ();
21126
21127 if (fe == NULL)
21128 dwarf2_debug_line_missing_file_complaint ();
21129 /* For now we ignore lines not starting on an instruction boundary.
21130 But not when processing end_sequence for compatibility with the
21131 previous version of the code. */
21132 else if (m_op_index == 0 || end_sequence)
21133 {
21134 fe->included_p = true;
21135 if (m_record_lines_p)
21136 {
21137 /* When we switch files we insert an end maker in the first file,
21138 switch to the second file and add a new line entry. The
21139 problem is that the end marker inserted in the first file will
21140 discard any previous line entries at the same address. If the
21141 line entries in the first file are marked as is-stmt, while
21142 the new line in the second file is non-stmt, then this means
21143 the end marker will discard is-stmt lines so we can have a
21144 non-stmt line. This means that there are less addresses at
21145 which the user can insert a breakpoint.
21146
21147 To improve this we track the last address in m_last_address,
21148 and whether we have seen an is-stmt at this address. Then
21149 when switching files, if we have seen a stmt at the current
21150 address, and we are switching to create a non-stmt line, then
21151 discard the new line. */
21152 bool file_changed
21153 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21154 bool ignore_this_line
21155 = ((file_changed && !end_sequence && m_last_address == m_address
21156 && !m_is_stmt && m_stmt_at_address)
21157 || (!end_sequence && m_line == 0));
21158
21159 if ((file_changed && !ignore_this_line) || end_sequence)
21160 {
21161 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21162 m_currently_recording_lines ? m_cu : nullptr);
21163 }
21164
21165 if (!end_sequence && !ignore_this_line)
21166 {
21167 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21168
21169 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21170 m_line_has_non_zero_discriminator,
21171 m_last_subfile))
21172 {
21173 buildsym_compunit *builder = m_cu->get_builder ();
21174 dwarf_record_line_1 (m_gdbarch,
21175 builder->get_current_subfile (),
21176 m_line, m_address, is_stmt,
21177 m_currently_recording_lines ? m_cu : nullptr);
21178 }
21179 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21180 m_last_line = m_line;
21181 }
21182 }
21183 }
21184
21185 /* Track whether we have seen any m_is_stmt true at m_address in case we
21186 have multiple line table entries all at m_address. */
21187 if (m_last_address != m_address)
21188 {
21189 m_stmt_at_address = false;
21190 m_last_address = m_address;
21191 }
21192 m_stmt_at_address |= m_is_stmt;
21193 }
21194
21195 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21196 line_header *lh, bool record_lines_p)
21197 {
21198 m_cu = cu;
21199 m_gdbarch = arch;
21200 m_record_lines_p = record_lines_p;
21201 m_line_header = lh;
21202
21203 m_currently_recording_lines = true;
21204
21205 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21206 was a line entry for it so that the backend has a chance to adjust it
21207 and also record it in case it needs it. This is currently used by MIPS
21208 code, cf. `mips_adjust_dwarf2_line'. */
21209 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21210 m_is_stmt = lh->default_is_stmt;
21211 m_discriminator = 0;
21212
21213 m_last_address = m_address;
21214 m_stmt_at_address = false;
21215 }
21216
21217 void
21218 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21219 const gdb_byte *line_ptr,
21220 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21221 {
21222 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21223 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21224 located at 0x0. In this case, additionally check that if
21225 ADDRESS < UNRELOCATED_LOWPC. */
21226
21227 if ((address == 0 && address < unrelocated_lowpc)
21228 || address == (CORE_ADDR) -1)
21229 {
21230 /* This line table is for a function which has been
21231 GCd by the linker. Ignore it. PR gdb/12528 */
21232
21233 struct objfile *objfile = cu->per_objfile->objfile;
21234 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21235
21236 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21237 line_offset, objfile_name (objfile));
21238 m_currently_recording_lines = false;
21239 /* Note: m_currently_recording_lines is left as false until we see
21240 DW_LNE_end_sequence. */
21241 }
21242 }
21243
21244 /* Subroutine of dwarf_decode_lines to simplify it.
21245 Process the line number information in LH.
21246 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21247 program in order to set included_p for every referenced header. */
21248
21249 static void
21250 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21251 const int decode_for_pst_p, CORE_ADDR lowpc)
21252 {
21253 const gdb_byte *line_ptr, *extended_end;
21254 const gdb_byte *line_end;
21255 unsigned int bytes_read, extended_len;
21256 unsigned char op_code, extended_op;
21257 CORE_ADDR baseaddr;
21258 struct objfile *objfile = cu->per_objfile->objfile;
21259 bfd *abfd = objfile->obfd;
21260 struct gdbarch *gdbarch = objfile->arch ();
21261 /* True if we're recording line info (as opposed to building partial
21262 symtabs and just interested in finding include files mentioned by
21263 the line number program). */
21264 bool record_lines_p = !decode_for_pst_p;
21265
21266 baseaddr = objfile->text_section_offset ();
21267
21268 line_ptr = lh->statement_program_start;
21269 line_end = lh->statement_program_end;
21270
21271 /* Read the statement sequences until there's nothing left. */
21272 while (line_ptr < line_end)
21273 {
21274 /* The DWARF line number program state machine. Reset the state
21275 machine at the start of each sequence. */
21276 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21277 bool end_sequence = false;
21278
21279 if (record_lines_p)
21280 {
21281 /* Start a subfile for the current file of the state
21282 machine. */
21283 const file_entry *fe = state_machine.current_file ();
21284
21285 if (fe != NULL)
21286 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21287 }
21288
21289 /* Decode the table. */
21290 while (line_ptr < line_end && !end_sequence)
21291 {
21292 op_code = read_1_byte (abfd, line_ptr);
21293 line_ptr += 1;
21294
21295 if (op_code >= lh->opcode_base)
21296 {
21297 /* Special opcode. */
21298 state_machine.handle_special_opcode (op_code);
21299 }
21300 else switch (op_code)
21301 {
21302 case DW_LNS_extended_op:
21303 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21304 &bytes_read);
21305 line_ptr += bytes_read;
21306 extended_end = line_ptr + extended_len;
21307 extended_op = read_1_byte (abfd, line_ptr);
21308 line_ptr += 1;
21309 if (DW_LNE_lo_user <= extended_op
21310 && extended_op <= DW_LNE_hi_user)
21311 {
21312 /* Vendor extension, ignore. */
21313 line_ptr = extended_end;
21314 break;
21315 }
21316 switch (extended_op)
21317 {
21318 case DW_LNE_end_sequence:
21319 state_machine.handle_end_sequence ();
21320 end_sequence = true;
21321 break;
21322 case DW_LNE_set_address:
21323 {
21324 CORE_ADDR address
21325 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21326 line_ptr += bytes_read;
21327
21328 state_machine.check_line_address (cu, line_ptr,
21329 lowpc - baseaddr, address);
21330 state_machine.handle_set_address (baseaddr, address);
21331 }
21332 break;
21333 case DW_LNE_define_file:
21334 {
21335 const char *cur_file;
21336 unsigned int mod_time, length;
21337 dir_index dindex;
21338
21339 cur_file = read_direct_string (abfd, line_ptr,
21340 &bytes_read);
21341 line_ptr += bytes_read;
21342 dindex = (dir_index)
21343 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21344 line_ptr += bytes_read;
21345 mod_time =
21346 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21347 line_ptr += bytes_read;
21348 length =
21349 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21350 line_ptr += bytes_read;
21351 lh->add_file_name (cur_file, dindex, mod_time, length);
21352 }
21353 break;
21354 case DW_LNE_set_discriminator:
21355 {
21356 /* The discriminator is not interesting to the
21357 debugger; just ignore it. We still need to
21358 check its value though:
21359 if there are consecutive entries for the same
21360 (non-prologue) line we want to coalesce them.
21361 PR 17276. */
21362 unsigned int discr
21363 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21364 line_ptr += bytes_read;
21365
21366 state_machine.handle_set_discriminator (discr);
21367 }
21368 break;
21369 default:
21370 complaint (_("mangled .debug_line section"));
21371 return;
21372 }
21373 /* Make sure that we parsed the extended op correctly. If e.g.
21374 we expected a different address size than the producer used,
21375 we may have read the wrong number of bytes. */
21376 if (line_ptr != extended_end)
21377 {
21378 complaint (_("mangled .debug_line section"));
21379 return;
21380 }
21381 break;
21382 case DW_LNS_copy:
21383 state_machine.handle_copy ();
21384 break;
21385 case DW_LNS_advance_pc:
21386 {
21387 CORE_ADDR adjust
21388 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21389 line_ptr += bytes_read;
21390
21391 state_machine.handle_advance_pc (adjust);
21392 }
21393 break;
21394 case DW_LNS_advance_line:
21395 {
21396 int line_delta
21397 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21398 line_ptr += bytes_read;
21399
21400 state_machine.handle_advance_line (line_delta);
21401 }
21402 break;
21403 case DW_LNS_set_file:
21404 {
21405 file_name_index file
21406 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21407 &bytes_read);
21408 line_ptr += bytes_read;
21409
21410 state_machine.handle_set_file (file);
21411 }
21412 break;
21413 case DW_LNS_set_column:
21414 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21415 line_ptr += bytes_read;
21416 break;
21417 case DW_LNS_negate_stmt:
21418 state_machine.handle_negate_stmt ();
21419 break;
21420 case DW_LNS_set_basic_block:
21421 break;
21422 /* Add to the address register of the state machine the
21423 address increment value corresponding to special opcode
21424 255. I.e., this value is scaled by the minimum
21425 instruction length since special opcode 255 would have
21426 scaled the increment. */
21427 case DW_LNS_const_add_pc:
21428 state_machine.handle_const_add_pc ();
21429 break;
21430 case DW_LNS_fixed_advance_pc:
21431 {
21432 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21433 line_ptr += 2;
21434
21435 state_machine.handle_fixed_advance_pc (addr_adj);
21436 }
21437 break;
21438 default:
21439 {
21440 /* Unknown standard opcode, ignore it. */
21441 int i;
21442
21443 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21444 {
21445 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21446 line_ptr += bytes_read;
21447 }
21448 }
21449 }
21450 }
21451
21452 if (!end_sequence)
21453 dwarf2_debug_line_missing_end_sequence_complaint ();
21454
21455 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21456 in which case we still finish recording the last line). */
21457 state_machine.record_line (true);
21458 }
21459 }
21460
21461 /* Decode the Line Number Program (LNP) for the given line_header
21462 structure and CU. The actual information extracted and the type
21463 of structures created from the LNP depends on the value of PST.
21464
21465 1. If PST is NULL, then this procedure uses the data from the program
21466 to create all necessary symbol tables, and their linetables.
21467
21468 2. If PST is not NULL, this procedure reads the program to determine
21469 the list of files included by the unit represented by PST, and
21470 builds all the associated partial symbol tables.
21471
21472 FND holds the CU file name and directory, if known.
21473 It is used for relative paths in the line table.
21474
21475 NOTE: It is important that psymtabs have the same file name (via
21476 strcmp) as the corresponding symtab. Since the directory is not
21477 used in the name of the symtab we don't use it in the name of the
21478 psymtabs we create. E.g. expand_line_sal requires this when
21479 finding psymtabs to expand. A good testcase for this is
21480 mb-inline.exp.
21481
21482 LOWPC is the lowest address in CU (or 0 if not known).
21483
21484 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21485 for its PC<->lines mapping information. Otherwise only the filename
21486 table is read in. */
21487
21488 static void
21489 dwarf_decode_lines (struct line_header *lh, const file_and_directory &fnd,
21490 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21491 CORE_ADDR lowpc, int decode_mapping)
21492 {
21493 struct objfile *objfile = cu->per_objfile->objfile;
21494 const int decode_for_pst_p = (pst != NULL);
21495
21496 if (decode_mapping)
21497 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21498
21499 if (decode_for_pst_p)
21500 {
21501 /* Now that we're done scanning the Line Header Program, we can
21502 create the psymtab of each included file. */
21503 for (auto &file_entry : lh->file_names ())
21504 if (file_entry.included_p)
21505 {
21506 gdb::unique_xmalloc_ptr<char> name_holder;
21507 const char *include_name =
21508 compute_include_file_name (lh, file_entry, fnd, &name_holder);
21509 if (include_name != NULL)
21510 dwarf2_create_include_psymtab
21511 (cu->per_objfile->per_bfd, include_name, pst,
21512 cu->per_objfile->per_bfd->partial_symtabs.get (),
21513 objfile->per_bfd);
21514 }
21515 }
21516 else
21517 {
21518 /* Make sure a symtab is created for every file, even files
21519 which contain only variables (i.e. no code with associated
21520 line numbers). */
21521 buildsym_compunit *builder = cu->get_builder ();
21522 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21523
21524 for (auto &fe : lh->file_names ())
21525 {
21526 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21527 if (builder->get_current_subfile ()->symtab == NULL)
21528 {
21529 builder->get_current_subfile ()->symtab
21530 = allocate_symtab (cust,
21531 builder->get_current_subfile ()->name);
21532 }
21533 fe.symtab = builder->get_current_subfile ()->symtab;
21534 }
21535 }
21536 }
21537
21538 /* Start a subfile for DWARF. FILENAME is the name of the file and
21539 DIRNAME the name of the source directory which contains FILENAME
21540 or NULL if not known.
21541 This routine tries to keep line numbers from identical absolute and
21542 relative file names in a common subfile.
21543
21544 Using the `list' example from the GDB testsuite, which resides in
21545 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21546 of /srcdir/list0.c yields the following debugging information for list0.c:
21547
21548 DW_AT_name: /srcdir/list0.c
21549 DW_AT_comp_dir: /compdir
21550 files.files[0].name: list0.h
21551 files.files[0].dir: /srcdir
21552 files.files[1].name: list0.c
21553 files.files[1].dir: /srcdir
21554
21555 The line number information for list0.c has to end up in a single
21556 subfile, so that `break /srcdir/list0.c:1' works as expected.
21557 start_subfile will ensure that this happens provided that we pass the
21558 concatenation of files.files[1].dir and files.files[1].name as the
21559 subfile's name. */
21560
21561 static void
21562 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21563 const char *dirname)
21564 {
21565 gdb::unique_xmalloc_ptr<char> copy;
21566
21567 /* In order not to lose the line information directory,
21568 we concatenate it to the filename when it makes sense.
21569 Note that the Dwarf3 standard says (speaking of filenames in line
21570 information): ``The directory index is ignored for file names
21571 that represent full path names''. Thus ignoring dirname in the
21572 `else' branch below isn't an issue. */
21573
21574 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21575 {
21576 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21577 filename = copy.get ();
21578 }
21579
21580 cu->get_builder ()->start_subfile (filename);
21581 }
21582
21583 static void
21584 var_decode_location (struct attribute *attr, struct symbol *sym,
21585 struct dwarf2_cu *cu)
21586 {
21587 struct objfile *objfile = cu->per_objfile->objfile;
21588 struct comp_unit_head *cu_header = &cu->header;
21589
21590 /* NOTE drow/2003-01-30: There used to be a comment and some special
21591 code here to turn a symbol with DW_AT_external and a
21592 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21593 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21594 with some versions of binutils) where shared libraries could have
21595 relocations against symbols in their debug information - the
21596 minimal symbol would have the right address, but the debug info
21597 would not. It's no longer necessary, because we will explicitly
21598 apply relocations when we read in the debug information now. */
21599
21600 /* A DW_AT_location attribute with no contents indicates that a
21601 variable has been optimized away. */
21602 if (attr->form_is_block () && attr->as_block ()->size == 0)
21603 {
21604 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21605 return;
21606 }
21607
21608 /* Handle one degenerate form of location expression specially, to
21609 preserve GDB's previous behavior when section offsets are
21610 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21611 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21612
21613 if (attr->form_is_block ())
21614 {
21615 struct dwarf_block *block = attr->as_block ();
21616
21617 if ((block->data[0] == DW_OP_addr
21618 && block->size == 1 + cu_header->addr_size)
21619 || ((block->data[0] == DW_OP_GNU_addr_index
21620 || block->data[0] == DW_OP_addrx)
21621 && (block->size
21622 == 1 + leb128_size (&block->data[1]))))
21623 {
21624 unsigned int dummy;
21625
21626 if (block->data[0] == DW_OP_addr)
21627 SET_SYMBOL_VALUE_ADDRESS
21628 (sym, cu->header.read_address (objfile->obfd,
21629 block->data + 1,
21630 &dummy));
21631 else
21632 SET_SYMBOL_VALUE_ADDRESS
21633 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21634 &dummy));
21635 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21636 fixup_symbol_section (sym, objfile);
21637 SET_SYMBOL_VALUE_ADDRESS
21638 (sym,
21639 SYMBOL_VALUE_ADDRESS (sym)
21640 + objfile->section_offsets[sym->section_index ()]);
21641 return;
21642 }
21643 }
21644
21645 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21646 expression evaluator, and use LOC_COMPUTED only when necessary
21647 (i.e. when the value of a register or memory location is
21648 referenced, or a thread-local block, etc.). Then again, it might
21649 not be worthwhile. I'm assuming that it isn't unless performance
21650 or memory numbers show me otherwise. */
21651
21652 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21653
21654 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21655 cu->has_loclist = true;
21656 }
21657
21658 /* Given a pointer to a DWARF information entry, figure out if we need
21659 to make a symbol table entry for it, and if so, create a new entry
21660 and return a pointer to it.
21661 If TYPE is NULL, determine symbol type from the die, otherwise
21662 used the passed type.
21663 If SPACE is not NULL, use it to hold the new symbol. If it is
21664 NULL, allocate a new symbol on the objfile's obstack. */
21665
21666 static struct symbol *
21667 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21668 struct symbol *space)
21669 {
21670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21671 struct objfile *objfile = per_objfile->objfile;
21672 struct gdbarch *gdbarch = objfile->arch ();
21673 struct symbol *sym = NULL;
21674 const char *name;
21675 struct attribute *attr = NULL;
21676 struct attribute *attr2 = NULL;
21677 CORE_ADDR baseaddr;
21678 struct pending **list_to_add = NULL;
21679
21680 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21681
21682 baseaddr = objfile->text_section_offset ();
21683
21684 name = dwarf2_name (die, cu);
21685 if (name)
21686 {
21687 int suppress_add = 0;
21688
21689 if (space)
21690 sym = space;
21691 else
21692 sym = new (&objfile->objfile_obstack) symbol;
21693 OBJSTAT (objfile, n_syms++);
21694
21695 /* Cache this symbol's name and the name's demangled form (if any). */
21696 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
21697 /* Fortran does not have mangling standard and the mangling does differ
21698 between gfortran, iFort etc. */
21699 const char *physname
21700 = (cu->per_cu->lang == language_fortran
21701 ? dwarf2_full_name (name, die, cu)
21702 : dwarf2_physname (name, die, cu));
21703 const char *linkagename = dw2_linkage_name (die, cu);
21704
21705 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
21706 sym->set_linkage_name (physname);
21707 else
21708 {
21709 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21710 sym->set_linkage_name (linkagename);
21711 }
21712
21713 /* Handle DW_AT_artificial. */
21714 attr = dwarf2_attr (die, DW_AT_artificial, cu);
21715 if (attr != nullptr)
21716 sym->artificial = attr->as_boolean ();
21717
21718 /* Default assumptions.
21719 Use the passed type or decode it from the die. */
21720 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21721 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21722 if (type != NULL)
21723 SYMBOL_TYPE (sym) = type;
21724 else
21725 SYMBOL_TYPE (sym) = die_type (die, cu);
21726 attr = dwarf2_attr (die,
21727 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21728 cu);
21729 if (attr != nullptr)
21730 SYMBOL_LINE (sym) = attr->constant_value (0);
21731
21732 attr = dwarf2_attr (die,
21733 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21734 cu);
21735 if (attr != nullptr && attr->is_nonnegative ())
21736 {
21737 file_name_index file_index
21738 = (file_name_index) attr->as_nonnegative ();
21739 struct file_entry *fe;
21740
21741 if (cu->line_header != NULL)
21742 fe = cu->line_header->file_name_at (file_index);
21743 else
21744 fe = NULL;
21745
21746 if (fe == NULL)
21747 complaint (_("file index out of range"));
21748 else
21749 symbol_set_symtab (sym, fe->symtab);
21750 }
21751
21752 switch (die->tag)
21753 {
21754 case DW_TAG_label:
21755 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21756 if (attr != nullptr)
21757 {
21758 CORE_ADDR addr;
21759
21760 addr = attr->as_address ();
21761 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21762 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21763 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21764 }
21765 else
21766 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21767 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21768 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21769 add_symbol_to_list (sym, cu->list_in_scope);
21770 break;
21771 case DW_TAG_subprogram:
21772 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21773 finish_block. */
21774 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21775 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21776 if ((attr2 != nullptr && attr2->as_boolean ())
21777 || cu->per_cu->lang == language_ada
21778 || cu->per_cu->lang == language_fortran)
21779 {
21780 /* Subprograms marked external are stored as a global symbol.
21781 Ada and Fortran subprograms, whether marked external or
21782 not, are always stored as a global symbol, because we want
21783 to be able to access them globally. For instance, we want
21784 to be able to break on a nested subprogram without having
21785 to specify the context. */
21786 list_to_add = cu->get_builder ()->get_global_symbols ();
21787 }
21788 else
21789 {
21790 list_to_add = cu->list_in_scope;
21791 }
21792 break;
21793 case DW_TAG_inlined_subroutine:
21794 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21795 finish_block. */
21796 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21797 SYMBOL_INLINED (sym) = 1;
21798 list_to_add = cu->list_in_scope;
21799 break;
21800 case DW_TAG_template_value_param:
21801 suppress_add = 1;
21802 /* Fall through. */
21803 case DW_TAG_constant:
21804 case DW_TAG_variable:
21805 case DW_TAG_member:
21806 /* Compilation with minimal debug info may result in
21807 variables with missing type entries. Change the
21808 misleading `void' type to something sensible. */
21809 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21810 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21811
21812 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21813 /* In the case of DW_TAG_member, we should only be called for
21814 static const members. */
21815 if (die->tag == DW_TAG_member)
21816 {
21817 /* dwarf2_add_field uses die_is_declaration,
21818 so we do the same. */
21819 gdb_assert (die_is_declaration (die, cu));
21820 gdb_assert (attr);
21821 }
21822 if (attr != nullptr)
21823 {
21824 dwarf2_const_value (attr, sym, cu);
21825 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21826 if (!suppress_add)
21827 {
21828 if (attr2 != nullptr && attr2->as_boolean ())
21829 list_to_add = cu->get_builder ()->get_global_symbols ();
21830 else
21831 list_to_add = cu->list_in_scope;
21832 }
21833 break;
21834 }
21835 attr = dwarf2_attr (die, DW_AT_location, cu);
21836 if (attr != nullptr)
21837 {
21838 var_decode_location (attr, sym, cu);
21839 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21840
21841 /* Fortran explicitly imports any global symbols to the local
21842 scope by DW_TAG_common_block. */
21843 if (cu->per_cu->lang == language_fortran && die->parent
21844 && die->parent->tag == DW_TAG_common_block)
21845 attr2 = NULL;
21846
21847 if (SYMBOL_CLASS (sym) == LOC_STATIC
21848 && SYMBOL_VALUE_ADDRESS (sym) == 0
21849 && !per_objfile->per_bfd->has_section_at_zero)
21850 {
21851 /* When a static variable is eliminated by the linker,
21852 the corresponding debug information is not stripped
21853 out, but the variable address is set to null;
21854 do not add such variables into symbol table. */
21855 }
21856 else if (attr2 != nullptr && attr2->as_boolean ())
21857 {
21858 if (SYMBOL_CLASS (sym) == LOC_STATIC
21859 && (objfile->flags & OBJF_MAINLINE) == 0
21860 && per_objfile->per_bfd->can_copy)
21861 {
21862 /* A global static variable might be subject to
21863 copy relocation. We first check for a local
21864 minsym, though, because maybe the symbol was
21865 marked hidden, in which case this would not
21866 apply. */
21867 bound_minimal_symbol found
21868 = (lookup_minimal_symbol_linkage
21869 (sym->linkage_name (), objfile));
21870 if (found.minsym != nullptr)
21871 sym->maybe_copied = 1;
21872 }
21873
21874 /* A variable with DW_AT_external is never static,
21875 but it may be block-scoped. */
21876 list_to_add
21877 = ((cu->list_in_scope
21878 == cu->get_builder ()->get_file_symbols ())
21879 ? cu->get_builder ()->get_global_symbols ()
21880 : cu->list_in_scope);
21881 }
21882 else
21883 list_to_add = cu->list_in_scope;
21884 }
21885 else
21886 {
21887 /* We do not know the address of this symbol.
21888 If it is an external symbol and we have type information
21889 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21890 The address of the variable will then be determined from
21891 the minimal symbol table whenever the variable is
21892 referenced. */
21893 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21894
21895 /* Fortran explicitly imports any global symbols to the local
21896 scope by DW_TAG_common_block. */
21897 if (cu->per_cu->lang == language_fortran && die->parent
21898 && die->parent->tag == DW_TAG_common_block)
21899 {
21900 /* SYMBOL_CLASS doesn't matter here because
21901 read_common_block is going to reset it. */
21902 if (!suppress_add)
21903 list_to_add = cu->list_in_scope;
21904 }
21905 else if (attr2 != nullptr && attr2->as_boolean ()
21906 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21907 {
21908 /* A variable with DW_AT_external is never static, but it
21909 may be block-scoped. */
21910 list_to_add
21911 = ((cu->list_in_scope
21912 == cu->get_builder ()->get_file_symbols ())
21913 ? cu->get_builder ()->get_global_symbols ()
21914 : cu->list_in_scope);
21915
21916 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21917 }
21918 else if (!die_is_declaration (die, cu))
21919 {
21920 /* Use the default LOC_OPTIMIZED_OUT class. */
21921 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21922 if (!suppress_add)
21923 list_to_add = cu->list_in_scope;
21924 }
21925 }
21926 break;
21927 case DW_TAG_formal_parameter:
21928 {
21929 /* If we are inside a function, mark this as an argument. If
21930 not, we might be looking at an argument to an inlined function
21931 when we do not have enough information to show inlined frames;
21932 pretend it's a local variable in that case so that the user can
21933 still see it. */
21934 struct context_stack *curr
21935 = cu->get_builder ()->get_current_context_stack ();
21936 if (curr != nullptr && curr->name != nullptr)
21937 SYMBOL_IS_ARGUMENT (sym) = 1;
21938 attr = dwarf2_attr (die, DW_AT_location, cu);
21939 if (attr != nullptr)
21940 {
21941 var_decode_location (attr, sym, cu);
21942 }
21943 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21944 if (attr != nullptr)
21945 {
21946 dwarf2_const_value (attr, sym, cu);
21947 }
21948
21949 list_to_add = cu->list_in_scope;
21950 }
21951 break;
21952 case DW_TAG_unspecified_parameters:
21953 /* From varargs functions; gdb doesn't seem to have any
21954 interest in this information, so just ignore it for now.
21955 (FIXME?) */
21956 break;
21957 case DW_TAG_template_type_param:
21958 suppress_add = 1;
21959 /* Fall through. */
21960 case DW_TAG_class_type:
21961 case DW_TAG_interface_type:
21962 case DW_TAG_structure_type:
21963 case DW_TAG_union_type:
21964 case DW_TAG_set_type:
21965 case DW_TAG_enumeration_type:
21966 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21967 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21968
21969 {
21970 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21971 really ever be static objects: otherwise, if you try
21972 to, say, break of a class's method and you're in a file
21973 which doesn't mention that class, it won't work unless
21974 the check for all static symbols in lookup_symbol_aux
21975 saves you. See the OtherFileClass tests in
21976 gdb.c++/namespace.exp. */
21977
21978 if (!suppress_add)
21979 {
21980 buildsym_compunit *builder = cu->get_builder ();
21981 list_to_add
21982 = (cu->list_in_scope == builder->get_file_symbols ()
21983 && cu->per_cu->lang == language_cplus
21984 ? builder->get_global_symbols ()
21985 : cu->list_in_scope);
21986
21987 /* The semantics of C++ state that "struct foo {
21988 ... }" also defines a typedef for "foo". */
21989 if (cu->per_cu->lang == language_cplus
21990 || cu->per_cu->lang == language_ada
21991 || cu->per_cu->lang == language_d
21992 || cu->per_cu->lang == language_rust)
21993 {
21994 /* The symbol's name is already allocated along
21995 with this objfile, so we don't need to
21996 duplicate it for the type. */
21997 if (SYMBOL_TYPE (sym)->name () == 0)
21998 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21999 }
22000 }
22001 }
22002 break;
22003 case DW_TAG_typedef:
22004 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22005 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22006 list_to_add = cu->list_in_scope;
22007 break;
22008 case DW_TAG_array_type:
22009 case DW_TAG_base_type:
22010 case DW_TAG_subrange_type:
22011 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22012 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22013 list_to_add = cu->list_in_scope;
22014 break;
22015 case DW_TAG_enumerator:
22016 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22017 if (attr != nullptr)
22018 {
22019 dwarf2_const_value (attr, sym, cu);
22020 }
22021 {
22022 /* NOTE: carlton/2003-11-10: See comment above in the
22023 DW_TAG_class_type, etc. block. */
22024
22025 list_to_add
22026 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22027 && cu->per_cu->lang == language_cplus
22028 ? cu->get_builder ()->get_global_symbols ()
22029 : cu->list_in_scope);
22030 }
22031 break;
22032 case DW_TAG_imported_declaration:
22033 case DW_TAG_namespace:
22034 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22035 list_to_add = cu->get_builder ()->get_global_symbols ();
22036 break;
22037 case DW_TAG_module:
22038 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22039 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22040 list_to_add = cu->get_builder ()->get_global_symbols ();
22041 break;
22042 case DW_TAG_common_block:
22043 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22044 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22045 add_symbol_to_list (sym, cu->list_in_scope);
22046 break;
22047 default:
22048 /* Not a tag we recognize. Hopefully we aren't processing
22049 trash data, but since we must specifically ignore things
22050 we don't recognize, there is nothing else we should do at
22051 this point. */
22052 complaint (_("unsupported tag: '%s'"),
22053 dwarf_tag_name (die->tag));
22054 break;
22055 }
22056
22057 if (suppress_add)
22058 {
22059 sym->hash_next = objfile->template_symbols;
22060 objfile->template_symbols = sym;
22061 list_to_add = NULL;
22062 }
22063
22064 if (list_to_add != NULL)
22065 add_symbol_to_list (sym, list_to_add);
22066
22067 /* For the benefit of old versions of GCC, check for anonymous
22068 namespaces based on the demangled name. */
22069 if (!cu->processing_has_namespace_info
22070 && cu->per_cu->lang == language_cplus)
22071 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22072 }
22073 return (sym);
22074 }
22075
22076 /* Given an attr with a DW_FORM_dataN value in host byte order,
22077 zero-extend it as appropriate for the symbol's type. The DWARF
22078 standard (v4) is not entirely clear about the meaning of using
22079 DW_FORM_dataN for a constant with a signed type, where the type is
22080 wider than the data. The conclusion of a discussion on the DWARF
22081 list was that this is unspecified. We choose to always zero-extend
22082 because that is the interpretation long in use by GCC. */
22083
22084 static gdb_byte *
22085 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22086 struct dwarf2_cu *cu, LONGEST *value, int bits)
22087 {
22088 struct objfile *objfile = cu->per_objfile->objfile;
22089 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22090 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22091 LONGEST l = attr->constant_value (0);
22092
22093 if (bits < sizeof (*value) * 8)
22094 {
22095 l &= ((LONGEST) 1 << bits) - 1;
22096 *value = l;
22097 }
22098 else if (bits == sizeof (*value) * 8)
22099 *value = l;
22100 else
22101 {
22102 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22103 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22104 return bytes;
22105 }
22106
22107 return NULL;
22108 }
22109
22110 /* Read a constant value from an attribute. Either set *VALUE, or if
22111 the value does not fit in *VALUE, set *BYTES - either already
22112 allocated on the objfile obstack, or newly allocated on OBSTACK,
22113 or, set *BATON, if we translated the constant to a location
22114 expression. */
22115
22116 static void
22117 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22118 const char *name, struct obstack *obstack,
22119 struct dwarf2_cu *cu,
22120 LONGEST *value, const gdb_byte **bytes,
22121 struct dwarf2_locexpr_baton **baton)
22122 {
22123 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22124 struct objfile *objfile = per_objfile->objfile;
22125 struct comp_unit_head *cu_header = &cu->header;
22126 struct dwarf_block *blk;
22127 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22128 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22129
22130 *value = 0;
22131 *bytes = NULL;
22132 *baton = NULL;
22133
22134 switch (attr->form)
22135 {
22136 case DW_FORM_addr:
22137 case DW_FORM_addrx:
22138 case DW_FORM_GNU_addr_index:
22139 {
22140 gdb_byte *data;
22141
22142 if (TYPE_LENGTH (type) != cu_header->addr_size)
22143 dwarf2_const_value_length_mismatch_complaint (name,
22144 cu_header->addr_size,
22145 TYPE_LENGTH (type));
22146 /* Symbols of this form are reasonably rare, so we just
22147 piggyback on the existing location code rather than writing
22148 a new implementation of symbol_computed_ops. */
22149 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22150 (*baton)->per_objfile = per_objfile;
22151 (*baton)->per_cu = cu->per_cu;
22152 gdb_assert ((*baton)->per_cu);
22153
22154 (*baton)->size = 2 + cu_header->addr_size;
22155 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22156 (*baton)->data = data;
22157
22158 data[0] = DW_OP_addr;
22159 store_unsigned_integer (&data[1], cu_header->addr_size,
22160 byte_order, attr->as_address ());
22161 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22162 }
22163 break;
22164 case DW_FORM_string:
22165 case DW_FORM_strp:
22166 case DW_FORM_strx:
22167 case DW_FORM_GNU_str_index:
22168 case DW_FORM_GNU_strp_alt:
22169 /* The string is already allocated on the objfile obstack, point
22170 directly to it. */
22171 *bytes = (const gdb_byte *) attr->as_string ();
22172 break;
22173 case DW_FORM_block1:
22174 case DW_FORM_block2:
22175 case DW_FORM_block4:
22176 case DW_FORM_block:
22177 case DW_FORM_exprloc:
22178 case DW_FORM_data16:
22179 blk = attr->as_block ();
22180 if (TYPE_LENGTH (type) != blk->size)
22181 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22182 TYPE_LENGTH (type));
22183 *bytes = blk->data;
22184 break;
22185
22186 /* The DW_AT_const_value attributes are supposed to carry the
22187 symbol's value "represented as it would be on the target
22188 architecture." By the time we get here, it's already been
22189 converted to host endianness, so we just need to sign- or
22190 zero-extend it as appropriate. */
22191 case DW_FORM_data1:
22192 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22193 break;
22194 case DW_FORM_data2:
22195 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22196 break;
22197 case DW_FORM_data4:
22198 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22199 break;
22200 case DW_FORM_data8:
22201 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22202 break;
22203
22204 case DW_FORM_sdata:
22205 case DW_FORM_implicit_const:
22206 *value = attr->as_signed ();
22207 break;
22208
22209 case DW_FORM_udata:
22210 *value = attr->as_unsigned ();
22211 break;
22212
22213 default:
22214 complaint (_("unsupported const value attribute form: '%s'"),
22215 dwarf_form_name (attr->form));
22216 *value = 0;
22217 break;
22218 }
22219 }
22220
22221
22222 /* Copy constant value from an attribute to a symbol. */
22223
22224 static void
22225 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22226 struct dwarf2_cu *cu)
22227 {
22228 struct objfile *objfile = cu->per_objfile->objfile;
22229 LONGEST value;
22230 const gdb_byte *bytes;
22231 struct dwarf2_locexpr_baton *baton;
22232
22233 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22234 sym->print_name (),
22235 &objfile->objfile_obstack, cu,
22236 &value, &bytes, &baton);
22237
22238 if (baton != NULL)
22239 {
22240 SYMBOL_LOCATION_BATON (sym) = baton;
22241 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22242 }
22243 else if (bytes != NULL)
22244 {
22245 SYMBOL_VALUE_BYTES (sym) = bytes;
22246 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22247 }
22248 else
22249 {
22250 SYMBOL_VALUE (sym) = value;
22251 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22252 }
22253 }
22254
22255 /* Return the type of the die in question using its DW_AT_type attribute. */
22256
22257 static struct type *
22258 die_type (struct die_info *die, struct dwarf2_cu *cu)
22259 {
22260 struct attribute *type_attr;
22261
22262 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22263 if (!type_attr)
22264 {
22265 struct objfile *objfile = cu->per_objfile->objfile;
22266 /* A missing DW_AT_type represents a void type. */
22267 return objfile_type (objfile)->builtin_void;
22268 }
22269
22270 return lookup_die_type (die, type_attr, cu);
22271 }
22272
22273 /* True iff CU's producer generates GNAT Ada auxiliary information
22274 that allows to find parallel types through that information instead
22275 of having to do expensive parallel lookups by type name. */
22276
22277 static int
22278 need_gnat_info (struct dwarf2_cu *cu)
22279 {
22280 /* Assume that the Ada compiler was GNAT, which always produces
22281 the auxiliary information. */
22282 return (cu->per_cu->lang == language_ada);
22283 }
22284
22285 /* Return the auxiliary type of the die in question using its
22286 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22287 attribute is not present. */
22288
22289 static struct type *
22290 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22291 {
22292 struct attribute *type_attr;
22293
22294 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22295 if (!type_attr)
22296 return NULL;
22297
22298 return lookup_die_type (die, type_attr, cu);
22299 }
22300
22301 /* If DIE has a descriptive_type attribute, then set the TYPE's
22302 descriptive type accordingly. */
22303
22304 static void
22305 set_descriptive_type (struct type *type, struct die_info *die,
22306 struct dwarf2_cu *cu)
22307 {
22308 struct type *descriptive_type = die_descriptive_type (die, cu);
22309
22310 if (descriptive_type)
22311 {
22312 ALLOCATE_GNAT_AUX_TYPE (type);
22313 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22314 }
22315 }
22316
22317 /* Return the containing type of the die in question using its
22318 DW_AT_containing_type attribute. */
22319
22320 static struct type *
22321 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22322 {
22323 struct attribute *type_attr;
22324 struct objfile *objfile = cu->per_objfile->objfile;
22325
22326 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22327 if (!type_attr)
22328 error (_("Dwarf Error: Problem turning containing type into gdb type "
22329 "[in module %s]"), objfile_name (objfile));
22330
22331 return lookup_die_type (die, type_attr, cu);
22332 }
22333
22334 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22335
22336 static struct type *
22337 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22338 {
22339 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22340 struct objfile *objfile = per_objfile->objfile;
22341 char *saved;
22342
22343 std::string message
22344 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22345 objfile_name (objfile),
22346 sect_offset_str (cu->header.sect_off),
22347 sect_offset_str (die->sect_off));
22348 saved = obstack_strdup (&objfile->objfile_obstack, message);
22349
22350 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22351 }
22352
22353 /* Look up the type of DIE in CU using its type attribute ATTR.
22354 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22355 DW_AT_containing_type.
22356 If there is no type substitute an error marker. */
22357
22358 static struct type *
22359 lookup_die_type (struct die_info *die, const struct attribute *attr,
22360 struct dwarf2_cu *cu)
22361 {
22362 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22363 struct objfile *objfile = per_objfile->objfile;
22364 struct type *this_type;
22365
22366 gdb_assert (attr->name == DW_AT_type
22367 || attr->name == DW_AT_GNAT_descriptive_type
22368 || attr->name == DW_AT_containing_type);
22369
22370 /* First see if we have it cached. */
22371
22372 if (attr->form == DW_FORM_GNU_ref_alt)
22373 {
22374 struct dwarf2_per_cu_data *per_cu;
22375 sect_offset sect_off = attr->get_ref_die_offset ();
22376
22377 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22378 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22379 }
22380 else if (attr->form_is_ref ())
22381 {
22382 sect_offset sect_off = attr->get_ref_die_offset ();
22383
22384 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22385 }
22386 else if (attr->form == DW_FORM_ref_sig8)
22387 {
22388 ULONGEST signature = attr->as_signature ();
22389
22390 return get_signatured_type (die, signature, cu);
22391 }
22392 else
22393 {
22394 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22395 " at %s [in module %s]"),
22396 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22397 objfile_name (objfile));
22398 return build_error_marker_type (cu, die);
22399 }
22400
22401 /* If not cached we need to read it in. */
22402
22403 if (this_type == NULL)
22404 {
22405 struct die_info *type_die = NULL;
22406 struct dwarf2_cu *type_cu = cu;
22407
22408 if (attr->form_is_ref ())
22409 type_die = follow_die_ref (die, attr, &type_cu);
22410 if (type_die == NULL)
22411 return build_error_marker_type (cu, die);
22412 /* If we find the type now, it's probably because the type came
22413 from an inter-CU reference and the type's CU got expanded before
22414 ours. */
22415 this_type = read_type_die (type_die, type_cu);
22416 }
22417
22418 /* If we still don't have a type use an error marker. */
22419
22420 if (this_type == NULL)
22421 return build_error_marker_type (cu, die);
22422
22423 return this_type;
22424 }
22425
22426 /* Return the type in DIE, CU.
22427 Returns NULL for invalid types.
22428
22429 This first does a lookup in die_type_hash,
22430 and only reads the die in if necessary.
22431
22432 NOTE: This can be called when reading in partial or full symbols. */
22433
22434 static struct type *
22435 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22436 {
22437 struct type *this_type;
22438
22439 this_type = get_die_type (die, cu);
22440 if (this_type)
22441 return this_type;
22442
22443 return read_type_die_1 (die, cu);
22444 }
22445
22446 /* Read the type in DIE, CU.
22447 Returns NULL for invalid types. */
22448
22449 static struct type *
22450 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22451 {
22452 struct type *this_type = NULL;
22453
22454 switch (die->tag)
22455 {
22456 case DW_TAG_class_type:
22457 case DW_TAG_interface_type:
22458 case DW_TAG_structure_type:
22459 case DW_TAG_union_type:
22460 this_type = read_structure_type (die, cu);
22461 break;
22462 case DW_TAG_enumeration_type:
22463 this_type = read_enumeration_type (die, cu);
22464 break;
22465 case DW_TAG_subprogram:
22466 case DW_TAG_subroutine_type:
22467 case DW_TAG_inlined_subroutine:
22468 this_type = read_subroutine_type (die, cu);
22469 break;
22470 case DW_TAG_array_type:
22471 this_type = read_array_type (die, cu);
22472 break;
22473 case DW_TAG_set_type:
22474 this_type = read_set_type (die, cu);
22475 break;
22476 case DW_TAG_pointer_type:
22477 this_type = read_tag_pointer_type (die, cu);
22478 break;
22479 case DW_TAG_ptr_to_member_type:
22480 this_type = read_tag_ptr_to_member_type (die, cu);
22481 break;
22482 case DW_TAG_reference_type:
22483 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22484 break;
22485 case DW_TAG_rvalue_reference_type:
22486 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22487 break;
22488 case DW_TAG_const_type:
22489 this_type = read_tag_const_type (die, cu);
22490 break;
22491 case DW_TAG_volatile_type:
22492 this_type = read_tag_volatile_type (die, cu);
22493 break;
22494 case DW_TAG_restrict_type:
22495 this_type = read_tag_restrict_type (die, cu);
22496 break;
22497 case DW_TAG_string_type:
22498 this_type = read_tag_string_type (die, cu);
22499 break;
22500 case DW_TAG_typedef:
22501 this_type = read_typedef (die, cu);
22502 break;
22503 case DW_TAG_subrange_type:
22504 this_type = read_subrange_type (die, cu);
22505 break;
22506 case DW_TAG_base_type:
22507 this_type = read_base_type (die, cu);
22508 break;
22509 case DW_TAG_unspecified_type:
22510 this_type = read_unspecified_type (die, cu);
22511 break;
22512 case DW_TAG_namespace:
22513 this_type = read_namespace_type (die, cu);
22514 break;
22515 case DW_TAG_module:
22516 this_type = read_module_type (die, cu);
22517 break;
22518 case DW_TAG_atomic_type:
22519 this_type = read_tag_atomic_type (die, cu);
22520 break;
22521 default:
22522 complaint (_("unexpected tag in read_type_die: '%s'"),
22523 dwarf_tag_name (die->tag));
22524 break;
22525 }
22526
22527 return this_type;
22528 }
22529
22530 /* See if we can figure out if the class lives in a namespace. We do
22531 this by looking for a member function; its demangled name will
22532 contain namespace info, if there is any.
22533 Return the computed name or NULL.
22534 Space for the result is allocated on the objfile's obstack.
22535 This is the full-die version of guess_partial_die_structure_name.
22536 In this case we know DIE has no useful parent. */
22537
22538 static const char *
22539 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22540 {
22541 struct die_info *spec_die;
22542 struct dwarf2_cu *spec_cu;
22543 struct die_info *child;
22544 struct objfile *objfile = cu->per_objfile->objfile;
22545
22546 spec_cu = cu;
22547 spec_die = die_specification (die, &spec_cu);
22548 if (spec_die != NULL)
22549 {
22550 die = spec_die;
22551 cu = spec_cu;
22552 }
22553
22554 for (child = die->child;
22555 child != NULL;
22556 child = child->sibling)
22557 {
22558 if (child->tag == DW_TAG_subprogram)
22559 {
22560 const char *linkage_name = dw2_linkage_name (child, cu);
22561
22562 if (linkage_name != NULL)
22563 {
22564 gdb::unique_xmalloc_ptr<char> actual_name
22565 (cu->language_defn->class_name_from_physname (linkage_name));
22566 const char *name = NULL;
22567
22568 if (actual_name != NULL)
22569 {
22570 const char *die_name = dwarf2_name (die, cu);
22571
22572 if (die_name != NULL
22573 && strcmp (die_name, actual_name.get ()) != 0)
22574 {
22575 /* Strip off the class name from the full name.
22576 We want the prefix. */
22577 int die_name_len = strlen (die_name);
22578 int actual_name_len = strlen (actual_name.get ());
22579 const char *ptr = actual_name.get ();
22580
22581 /* Test for '::' as a sanity check. */
22582 if (actual_name_len > die_name_len + 2
22583 && ptr[actual_name_len - die_name_len - 1] == ':')
22584 name = obstack_strndup (
22585 &objfile->per_bfd->storage_obstack,
22586 ptr, actual_name_len - die_name_len - 2);
22587 }
22588 }
22589 return name;
22590 }
22591 }
22592 }
22593
22594 return NULL;
22595 }
22596
22597 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22598 prefix part in such case. See
22599 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22600
22601 static const char *
22602 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22603 {
22604 struct attribute *attr;
22605 const char *base;
22606
22607 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22608 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22609 return NULL;
22610
22611 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22612 return NULL;
22613
22614 attr = dw2_linkage_name_attr (die, cu);
22615 const char *attr_name = attr->as_string ();
22616 if (attr == NULL || attr_name == NULL)
22617 return NULL;
22618
22619 /* dwarf2_name had to be already called. */
22620 gdb_assert (attr->canonical_string_p ());
22621
22622 /* Strip the base name, keep any leading namespaces/classes. */
22623 base = strrchr (attr_name, ':');
22624 if (base == NULL || base == attr_name || base[-1] != ':')
22625 return "";
22626
22627 struct objfile *objfile = cu->per_objfile->objfile;
22628 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22629 attr_name,
22630 &base[-1] - attr_name);
22631 }
22632
22633 /* Return the name of the namespace/class that DIE is defined within,
22634 or "" if we can't tell. The caller should not xfree the result.
22635
22636 For example, if we're within the method foo() in the following
22637 code:
22638
22639 namespace N {
22640 class C {
22641 void foo () {
22642 }
22643 };
22644 }
22645
22646 then determine_prefix on foo's die will return "N::C". */
22647
22648 static const char *
22649 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22650 {
22651 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22652 struct die_info *parent, *spec_die;
22653 struct dwarf2_cu *spec_cu;
22654 struct type *parent_type;
22655 const char *retval;
22656
22657 if (cu->per_cu->lang != language_cplus
22658 && cu->per_cu->lang != language_fortran
22659 && cu->per_cu->lang != language_d
22660 && cu->per_cu->lang != language_rust)
22661 return "";
22662
22663 retval = anonymous_struct_prefix (die, cu);
22664 if (retval)
22665 return retval;
22666
22667 /* We have to be careful in the presence of DW_AT_specification.
22668 For example, with GCC 3.4, given the code
22669
22670 namespace N {
22671 void foo() {
22672 // Definition of N::foo.
22673 }
22674 }
22675
22676 then we'll have a tree of DIEs like this:
22677
22678 1: DW_TAG_compile_unit
22679 2: DW_TAG_namespace // N
22680 3: DW_TAG_subprogram // declaration of N::foo
22681 4: DW_TAG_subprogram // definition of N::foo
22682 DW_AT_specification // refers to die #3
22683
22684 Thus, when processing die #4, we have to pretend that we're in
22685 the context of its DW_AT_specification, namely the contex of die
22686 #3. */
22687 spec_cu = cu;
22688 spec_die = die_specification (die, &spec_cu);
22689 if (spec_die == NULL)
22690 parent = die->parent;
22691 else
22692 {
22693 parent = spec_die->parent;
22694 cu = spec_cu;
22695 }
22696
22697 if (parent == NULL)
22698 return "";
22699 else if (parent->building_fullname)
22700 {
22701 const char *name;
22702 const char *parent_name;
22703
22704 /* It has been seen on RealView 2.2 built binaries,
22705 DW_TAG_template_type_param types actually _defined_ as
22706 children of the parent class:
22707
22708 enum E {};
22709 template class <class Enum> Class{};
22710 Class<enum E> class_e;
22711
22712 1: DW_TAG_class_type (Class)
22713 2: DW_TAG_enumeration_type (E)
22714 3: DW_TAG_enumerator (enum1:0)
22715 3: DW_TAG_enumerator (enum2:1)
22716 ...
22717 2: DW_TAG_template_type_param
22718 DW_AT_type DW_FORM_ref_udata (E)
22719
22720 Besides being broken debug info, it can put GDB into an
22721 infinite loop. Consider:
22722
22723 When we're building the full name for Class<E>, we'll start
22724 at Class, and go look over its template type parameters,
22725 finding E. We'll then try to build the full name of E, and
22726 reach here. We're now trying to build the full name of E,
22727 and look over the parent DIE for containing scope. In the
22728 broken case, if we followed the parent DIE of E, we'd again
22729 find Class, and once again go look at its template type
22730 arguments, etc., etc. Simply don't consider such parent die
22731 as source-level parent of this die (it can't be, the language
22732 doesn't allow it), and break the loop here. */
22733 name = dwarf2_name (die, cu);
22734 parent_name = dwarf2_name (parent, cu);
22735 complaint (_("template param type '%s' defined within parent '%s'"),
22736 name ? name : "<unknown>",
22737 parent_name ? parent_name : "<unknown>");
22738 return "";
22739 }
22740 else
22741 switch (parent->tag)
22742 {
22743 case DW_TAG_namespace:
22744 parent_type = read_type_die (parent, cu);
22745 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22746 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22747 Work around this problem here. */
22748 if (cu->per_cu->lang == language_cplus
22749 && strcmp (parent_type->name (), "::") == 0)
22750 return "";
22751 /* We give a name to even anonymous namespaces. */
22752 return parent_type->name ();
22753 case DW_TAG_class_type:
22754 case DW_TAG_interface_type:
22755 case DW_TAG_structure_type:
22756 case DW_TAG_union_type:
22757 case DW_TAG_module:
22758 parent_type = read_type_die (parent, cu);
22759 if (parent_type->name () != NULL)
22760 return parent_type->name ();
22761 else
22762 /* An anonymous structure is only allowed non-static data
22763 members; no typedefs, no member functions, et cetera.
22764 So it does not need a prefix. */
22765 return "";
22766 case DW_TAG_compile_unit:
22767 case DW_TAG_partial_unit:
22768 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22769 if (cu->per_cu->lang == language_cplus
22770 && !per_objfile->per_bfd->types.empty ()
22771 && die->child != NULL
22772 && (die->tag == DW_TAG_class_type
22773 || die->tag == DW_TAG_structure_type
22774 || die->tag == DW_TAG_union_type))
22775 {
22776 const char *name = guess_full_die_structure_name (die, cu);
22777 if (name != NULL)
22778 return name;
22779 }
22780 return "";
22781 case DW_TAG_subprogram:
22782 /* Nested subroutines in Fortran get a prefix with the name
22783 of the parent's subroutine. */
22784 if (cu->per_cu->lang == language_fortran)
22785 {
22786 if ((die->tag == DW_TAG_subprogram)
22787 && (dwarf2_name (parent, cu) != NULL))
22788 return dwarf2_name (parent, cu);
22789 }
22790 return "";
22791 case DW_TAG_enumeration_type:
22792 parent_type = read_type_die (parent, cu);
22793 if (parent_type->is_declared_class ())
22794 {
22795 if (parent_type->name () != NULL)
22796 return parent_type->name ();
22797 return "";
22798 }
22799 /* Fall through. */
22800 default:
22801 return determine_prefix (parent, cu);
22802 }
22803 }
22804
22805 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22806 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22807 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22808 an obconcat, otherwise allocate storage for the result. The CU argument is
22809 used to determine the language and hence, the appropriate separator. */
22810
22811 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22812
22813 static char *
22814 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22815 int physname, struct dwarf2_cu *cu)
22816 {
22817 const char *lead = "";
22818 const char *sep;
22819
22820 if (suffix == NULL || suffix[0] == '\0'
22821 || prefix == NULL || prefix[0] == '\0')
22822 sep = "";
22823 else if (cu->per_cu->lang == language_d)
22824 {
22825 /* For D, the 'main' function could be defined in any module, but it
22826 should never be prefixed. */
22827 if (strcmp (suffix, "D main") == 0)
22828 {
22829 prefix = "";
22830 sep = "";
22831 }
22832 else
22833 sep = ".";
22834 }
22835 else if (cu->per_cu->lang == language_fortran && physname)
22836 {
22837 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22838 DW_AT_MIPS_linkage_name is preferred and used instead. */
22839
22840 lead = "__";
22841 sep = "_MOD_";
22842 }
22843 else
22844 sep = "::";
22845
22846 if (prefix == NULL)
22847 prefix = "";
22848 if (suffix == NULL)
22849 suffix = "";
22850
22851 if (obs == NULL)
22852 {
22853 char *retval
22854 = ((char *)
22855 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22856
22857 strcpy (retval, lead);
22858 strcat (retval, prefix);
22859 strcat (retval, sep);
22860 strcat (retval, suffix);
22861 return retval;
22862 }
22863 else
22864 {
22865 /* We have an obstack. */
22866 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22867 }
22868 }
22869
22870 /* Get name of a die, return NULL if not found. */
22871
22872 static const char *
22873 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22874 struct objfile *objfile)
22875 {
22876 if (name && cu->per_cu->lang == language_cplus)
22877 {
22878 gdb::unique_xmalloc_ptr<char> canon_name
22879 = cp_canonicalize_string (name);
22880
22881 if (canon_name != nullptr)
22882 name = objfile->intern (canon_name.get ());
22883 }
22884
22885 return name;
22886 }
22887
22888 /* Get name of a die, return NULL if not found.
22889 Anonymous namespaces are converted to their magic string. */
22890
22891 static const char *
22892 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22893 {
22894 struct attribute *attr;
22895 struct objfile *objfile = cu->per_objfile->objfile;
22896
22897 attr = dwarf2_attr (die, DW_AT_name, cu);
22898 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22899 if (attr_name == nullptr
22900 && die->tag != DW_TAG_namespace
22901 && die->tag != DW_TAG_class_type
22902 && die->tag != DW_TAG_interface_type
22903 && die->tag != DW_TAG_structure_type
22904 && die->tag != DW_TAG_union_type)
22905 return NULL;
22906
22907 switch (die->tag)
22908 {
22909 case DW_TAG_compile_unit:
22910 case DW_TAG_partial_unit:
22911 /* Compilation units have a DW_AT_name that is a filename, not
22912 a source language identifier. */
22913 case DW_TAG_enumeration_type:
22914 case DW_TAG_enumerator:
22915 /* These tags always have simple identifiers already; no need
22916 to canonicalize them. */
22917 return attr_name;
22918
22919 case DW_TAG_namespace:
22920 if (attr_name != nullptr)
22921 return attr_name;
22922 return CP_ANONYMOUS_NAMESPACE_STR;
22923
22924 case DW_TAG_class_type:
22925 case DW_TAG_interface_type:
22926 case DW_TAG_structure_type:
22927 case DW_TAG_union_type:
22928 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22929 structures or unions. These were of the form "._%d" in GCC 4.1,
22930 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22931 and GCC 4.4. We work around this problem by ignoring these. */
22932 if (attr_name != nullptr
22933 && (startswith (attr_name, "._")
22934 || startswith (attr_name, "<anonymous")))
22935 return NULL;
22936
22937 /* GCC might emit a nameless typedef that has a linkage name. See
22938 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22939 if (!attr || attr_name == NULL)
22940 {
22941 attr = dw2_linkage_name_attr (die, cu);
22942 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22943 if (attr == NULL || attr_name == NULL)
22944 return NULL;
22945
22946 /* Avoid demangling attr_name the second time on a second
22947 call for the same DIE. */
22948 if (!attr->canonical_string_p ())
22949 {
22950 gdb::unique_xmalloc_ptr<char> demangled
22951 (gdb_demangle (attr_name, DMGL_TYPES));
22952 if (demangled == nullptr)
22953 return nullptr;
22954
22955 attr->set_string_canonical (objfile->intern (demangled.get ()));
22956 attr_name = attr->as_string ();
22957 }
22958
22959 /* Strip any leading namespaces/classes, keep only the
22960 base name. DW_AT_name for named DIEs does not
22961 contain the prefixes. */
22962 const char *base = strrchr (attr_name, ':');
22963 if (base && base > attr_name && base[-1] == ':')
22964 return &base[1];
22965 else
22966 return attr_name;
22967 }
22968 break;
22969
22970 default:
22971 break;
22972 }
22973
22974 if (!attr->canonical_string_p ())
22975 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22976 objfile));
22977 return attr->as_string ();
22978 }
22979
22980 /* Return the die that this die in an extension of, or NULL if there
22981 is none. *EXT_CU is the CU containing DIE on input, and the CU
22982 containing the return value on output. */
22983
22984 static struct die_info *
22985 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22986 {
22987 struct attribute *attr;
22988
22989 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22990 if (attr == NULL)
22991 return NULL;
22992
22993 return follow_die_ref (die, attr, ext_cu);
22994 }
22995
22996 static void
22997 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22998 {
22999 unsigned int i;
23000
23001 print_spaces (indent, f);
23002 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23003 dwarf_tag_name (die->tag), die->abbrev,
23004 sect_offset_str (die->sect_off));
23005
23006 if (die->parent != NULL)
23007 {
23008 print_spaces (indent, f);
23009 fprintf_unfiltered (f, " parent at offset: %s\n",
23010 sect_offset_str (die->parent->sect_off));
23011 }
23012
23013 print_spaces (indent, f);
23014 fprintf_unfiltered (f, " has children: %s\n",
23015 dwarf_bool_name (die->child != NULL));
23016
23017 print_spaces (indent, f);
23018 fprintf_unfiltered (f, " attributes:\n");
23019
23020 for (i = 0; i < die->num_attrs; ++i)
23021 {
23022 print_spaces (indent, f);
23023 fprintf_unfiltered (f, " %s (%s) ",
23024 dwarf_attr_name (die->attrs[i].name),
23025 dwarf_form_name (die->attrs[i].form));
23026
23027 switch (die->attrs[i].form)
23028 {
23029 case DW_FORM_addr:
23030 case DW_FORM_addrx:
23031 case DW_FORM_GNU_addr_index:
23032 fprintf_unfiltered (f, "address: ");
23033 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23034 break;
23035 case DW_FORM_block2:
23036 case DW_FORM_block4:
23037 case DW_FORM_block:
23038 case DW_FORM_block1:
23039 fprintf_unfiltered (f, "block: size %s",
23040 pulongest (die->attrs[i].as_block ()->size));
23041 break;
23042 case DW_FORM_exprloc:
23043 fprintf_unfiltered (f, "expression: size %s",
23044 pulongest (die->attrs[i].as_block ()->size));
23045 break;
23046 case DW_FORM_data16:
23047 fprintf_unfiltered (f, "constant of 16 bytes");
23048 break;
23049 case DW_FORM_ref_addr:
23050 fprintf_unfiltered (f, "ref address: ");
23051 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23052 break;
23053 case DW_FORM_GNU_ref_alt:
23054 fprintf_unfiltered (f, "alt ref address: ");
23055 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23056 break;
23057 case DW_FORM_ref1:
23058 case DW_FORM_ref2:
23059 case DW_FORM_ref4:
23060 case DW_FORM_ref8:
23061 case DW_FORM_ref_udata:
23062 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23063 (long) (die->attrs[i].as_unsigned ()));
23064 break;
23065 case DW_FORM_data1:
23066 case DW_FORM_data2:
23067 case DW_FORM_data4:
23068 case DW_FORM_data8:
23069 case DW_FORM_udata:
23070 fprintf_unfiltered (f, "constant: %s",
23071 pulongest (die->attrs[i].as_unsigned ()));
23072 break;
23073 case DW_FORM_sec_offset:
23074 fprintf_unfiltered (f, "section offset: %s",
23075 pulongest (die->attrs[i].as_unsigned ()));
23076 break;
23077 case DW_FORM_ref_sig8:
23078 fprintf_unfiltered (f, "signature: %s",
23079 hex_string (die->attrs[i].as_signature ()));
23080 break;
23081 case DW_FORM_string:
23082 case DW_FORM_strp:
23083 case DW_FORM_line_strp:
23084 case DW_FORM_strx:
23085 case DW_FORM_GNU_str_index:
23086 case DW_FORM_GNU_strp_alt:
23087 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23088 die->attrs[i].as_string ()
23089 ? die->attrs[i].as_string () : "",
23090 die->attrs[i].canonical_string_p () ? "is" : "not");
23091 break;
23092 case DW_FORM_flag:
23093 if (die->attrs[i].as_boolean ())
23094 fprintf_unfiltered (f, "flag: TRUE");
23095 else
23096 fprintf_unfiltered (f, "flag: FALSE");
23097 break;
23098 case DW_FORM_flag_present:
23099 fprintf_unfiltered (f, "flag: TRUE");
23100 break;
23101 case DW_FORM_indirect:
23102 /* The reader will have reduced the indirect form to
23103 the "base form" so this form should not occur. */
23104 fprintf_unfiltered (f,
23105 "unexpected attribute form: DW_FORM_indirect");
23106 break;
23107 case DW_FORM_sdata:
23108 case DW_FORM_implicit_const:
23109 fprintf_unfiltered (f, "constant: %s",
23110 plongest (die->attrs[i].as_signed ()));
23111 break;
23112 default:
23113 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23114 die->attrs[i].form);
23115 break;
23116 }
23117 fprintf_unfiltered (f, "\n");
23118 }
23119 }
23120
23121 static void
23122 dump_die_for_error (struct die_info *die)
23123 {
23124 dump_die_shallow (gdb_stderr, 0, die);
23125 }
23126
23127 static void
23128 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23129 {
23130 int indent = level * 4;
23131
23132 gdb_assert (die != NULL);
23133
23134 if (level >= max_level)
23135 return;
23136
23137 dump_die_shallow (f, indent, die);
23138
23139 if (die->child != NULL)
23140 {
23141 print_spaces (indent, f);
23142 fprintf_unfiltered (f, " Children:");
23143 if (level + 1 < max_level)
23144 {
23145 fprintf_unfiltered (f, "\n");
23146 dump_die_1 (f, level + 1, max_level, die->child);
23147 }
23148 else
23149 {
23150 fprintf_unfiltered (f,
23151 " [not printed, max nesting level reached]\n");
23152 }
23153 }
23154
23155 if (die->sibling != NULL && level > 0)
23156 {
23157 dump_die_1 (f, level, max_level, die->sibling);
23158 }
23159 }
23160
23161 /* This is called from the pdie macro in gdbinit.in.
23162 It's not static so gcc will keep a copy callable from gdb. */
23163
23164 void
23165 dump_die (struct die_info *die, int max_level)
23166 {
23167 dump_die_1 (gdb_stdlog, 0, max_level, die);
23168 }
23169
23170 static void
23171 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23172 {
23173 void **slot;
23174
23175 slot = htab_find_slot_with_hash (cu->die_hash, die,
23176 to_underlying (die->sect_off),
23177 INSERT);
23178
23179 *slot = die;
23180 }
23181
23182 /* Follow reference or signature attribute ATTR of SRC_DIE.
23183 On entry *REF_CU is the CU of SRC_DIE.
23184 On exit *REF_CU is the CU of the result. */
23185
23186 static struct die_info *
23187 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23188 struct dwarf2_cu **ref_cu)
23189 {
23190 struct die_info *die;
23191
23192 if (attr->form_is_ref ())
23193 die = follow_die_ref (src_die, attr, ref_cu);
23194 else if (attr->form == DW_FORM_ref_sig8)
23195 die = follow_die_sig (src_die, attr, ref_cu);
23196 else
23197 {
23198 dump_die_for_error (src_die);
23199 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23200 objfile_name ((*ref_cu)->per_objfile->objfile));
23201 }
23202
23203 return die;
23204 }
23205
23206 /* Follow reference OFFSET.
23207 On entry *REF_CU is the CU of the source die referencing OFFSET.
23208 On exit *REF_CU is the CU of the result.
23209 Returns NULL if OFFSET is invalid. */
23210
23211 static struct die_info *
23212 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23213 struct dwarf2_cu **ref_cu)
23214 {
23215 struct die_info temp_die;
23216 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23217 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23218
23219 gdb_assert (cu->per_cu != NULL);
23220
23221 target_cu = cu;
23222
23223 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23224 "source CU contains target offset: %d",
23225 sect_offset_str (cu->per_cu->sect_off),
23226 sect_offset_str (sect_off),
23227 cu->header.offset_in_cu_p (sect_off));
23228
23229 if (cu->per_cu->is_debug_types)
23230 {
23231 /* .debug_types CUs cannot reference anything outside their CU.
23232 If they need to, they have to reference a signatured type via
23233 DW_FORM_ref_sig8. */
23234 if (!cu->header.offset_in_cu_p (sect_off))
23235 return NULL;
23236 }
23237 else if (offset_in_dwz != cu->per_cu->is_dwz
23238 || !cu->header.offset_in_cu_p (sect_off))
23239 {
23240 struct dwarf2_per_cu_data *per_cu;
23241
23242 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23243 per_objfile);
23244
23245 dwarf_read_debug_printf_v ("target CU offset: %s, "
23246 "target CU DIEs loaded: %d",
23247 sect_offset_str (per_cu->sect_off),
23248 per_objfile->get_cu (per_cu) != nullptr);
23249
23250 /* If necessary, add it to the queue and load its DIEs.
23251
23252 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23253 it doesn't mean they are currently loaded. Since we require them
23254 to be loaded, we must check for ourselves. */
23255 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
23256 || per_objfile->get_cu (per_cu) == nullptr)
23257 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23258 false, cu->per_cu->lang);
23259
23260 target_cu = per_objfile->get_cu (per_cu);
23261 gdb_assert (target_cu != nullptr);
23262 }
23263 else if (cu->dies == NULL)
23264 {
23265 /* We're loading full DIEs during partial symbol reading. */
23266 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23267 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23268 language_minimal);
23269 }
23270
23271 *ref_cu = target_cu;
23272 temp_die.sect_off = sect_off;
23273
23274 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23275 &temp_die,
23276 to_underlying (sect_off));
23277 }
23278
23279 /* Follow reference attribute ATTR of SRC_DIE.
23280 On entry *REF_CU is the CU of SRC_DIE.
23281 On exit *REF_CU is the CU of the result. */
23282
23283 static struct die_info *
23284 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23285 struct dwarf2_cu **ref_cu)
23286 {
23287 sect_offset sect_off = attr->get_ref_die_offset ();
23288 struct dwarf2_cu *cu = *ref_cu;
23289 struct die_info *die;
23290
23291 die = follow_die_offset (sect_off,
23292 (attr->form == DW_FORM_GNU_ref_alt
23293 || cu->per_cu->is_dwz),
23294 ref_cu);
23295 if (!die)
23296 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23297 "at %s [in module %s]"),
23298 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23299 objfile_name (cu->per_objfile->objfile));
23300
23301 return die;
23302 }
23303
23304 /* See read.h. */
23305
23306 struct dwarf2_locexpr_baton
23307 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23308 dwarf2_per_cu_data *per_cu,
23309 dwarf2_per_objfile *per_objfile,
23310 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23311 bool resolve_abstract_p)
23312 {
23313 struct die_info *die;
23314 struct attribute *attr;
23315 struct dwarf2_locexpr_baton retval;
23316 struct objfile *objfile = per_objfile->objfile;
23317
23318 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23319 if (cu == nullptr)
23320 cu = load_cu (per_cu, per_objfile, false);
23321
23322 if (cu == nullptr)
23323 {
23324 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23325 Instead just throw an error, not much else we can do. */
23326 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23327 sect_offset_str (sect_off), objfile_name (objfile));
23328 }
23329
23330 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23331 if (!die)
23332 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23333 sect_offset_str (sect_off), objfile_name (objfile));
23334
23335 attr = dwarf2_attr (die, DW_AT_location, cu);
23336 if (!attr && resolve_abstract_p
23337 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23338 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23339 {
23340 CORE_ADDR pc = get_frame_pc ();
23341 CORE_ADDR baseaddr = objfile->text_section_offset ();
23342 struct gdbarch *gdbarch = objfile->arch ();
23343
23344 for (const auto &cand_off
23345 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23346 {
23347 struct dwarf2_cu *cand_cu = cu;
23348 struct die_info *cand
23349 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23350 if (!cand
23351 || !cand->parent
23352 || cand->parent->tag != DW_TAG_subprogram)
23353 continue;
23354
23355 CORE_ADDR pc_low, pc_high;
23356 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23357 if (pc_low == ((CORE_ADDR) -1))
23358 continue;
23359 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23360 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23361 if (!(pc_low <= pc && pc < pc_high))
23362 continue;
23363
23364 die = cand;
23365 attr = dwarf2_attr (die, DW_AT_location, cu);
23366 break;
23367 }
23368 }
23369
23370 if (!attr)
23371 {
23372 /* DWARF: "If there is no such attribute, then there is no effect.".
23373 DATA is ignored if SIZE is 0. */
23374
23375 retval.data = NULL;
23376 retval.size = 0;
23377 }
23378 else if (attr->form_is_section_offset ())
23379 {
23380 struct dwarf2_loclist_baton loclist_baton;
23381 CORE_ADDR pc = get_frame_pc ();
23382 size_t size;
23383
23384 fill_in_loclist_baton (cu, &loclist_baton, attr);
23385
23386 retval.data = dwarf2_find_location_expression (&loclist_baton,
23387 &size, pc);
23388 retval.size = size;
23389 }
23390 else
23391 {
23392 if (!attr->form_is_block ())
23393 error (_("Dwarf Error: DIE at %s referenced in module %s "
23394 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23395 sect_offset_str (sect_off), objfile_name (objfile));
23396
23397 struct dwarf_block *block = attr->as_block ();
23398 retval.data = block->data;
23399 retval.size = block->size;
23400 }
23401 retval.per_objfile = per_objfile;
23402 retval.per_cu = cu->per_cu;
23403
23404 per_objfile->age_comp_units ();
23405
23406 return retval;
23407 }
23408
23409 /* See read.h. */
23410
23411 struct dwarf2_locexpr_baton
23412 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23413 dwarf2_per_cu_data *per_cu,
23414 dwarf2_per_objfile *per_objfile,
23415 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23416 {
23417 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23418
23419 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23420 get_frame_pc);
23421 }
23422
23423 /* Write a constant of a given type as target-ordered bytes into
23424 OBSTACK. */
23425
23426 static const gdb_byte *
23427 write_constant_as_bytes (struct obstack *obstack,
23428 enum bfd_endian byte_order,
23429 struct type *type,
23430 ULONGEST value,
23431 LONGEST *len)
23432 {
23433 gdb_byte *result;
23434
23435 *len = TYPE_LENGTH (type);
23436 result = (gdb_byte *) obstack_alloc (obstack, *len);
23437 store_unsigned_integer (result, *len, byte_order, value);
23438
23439 return result;
23440 }
23441
23442 /* See read.h. */
23443
23444 const gdb_byte *
23445 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23446 dwarf2_per_cu_data *per_cu,
23447 dwarf2_per_objfile *per_objfile,
23448 obstack *obstack,
23449 LONGEST *len)
23450 {
23451 struct die_info *die;
23452 struct attribute *attr;
23453 const gdb_byte *result = NULL;
23454 struct type *type;
23455 LONGEST value;
23456 enum bfd_endian byte_order;
23457 struct objfile *objfile = per_objfile->objfile;
23458
23459 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23460 if (cu == nullptr)
23461 cu = load_cu (per_cu, per_objfile, false);
23462
23463 if (cu == nullptr)
23464 {
23465 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23466 Instead just throw an error, not much else we can do. */
23467 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23468 sect_offset_str (sect_off), objfile_name (objfile));
23469 }
23470
23471 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23472 if (!die)
23473 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23474 sect_offset_str (sect_off), objfile_name (objfile));
23475
23476 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23477 if (attr == NULL)
23478 return NULL;
23479
23480 byte_order = (bfd_big_endian (objfile->obfd)
23481 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23482
23483 switch (attr->form)
23484 {
23485 case DW_FORM_addr:
23486 case DW_FORM_addrx:
23487 case DW_FORM_GNU_addr_index:
23488 {
23489 gdb_byte *tem;
23490
23491 *len = cu->header.addr_size;
23492 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23493 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23494 result = tem;
23495 }
23496 break;
23497 case DW_FORM_string:
23498 case DW_FORM_strp:
23499 case DW_FORM_strx:
23500 case DW_FORM_GNU_str_index:
23501 case DW_FORM_GNU_strp_alt:
23502 /* The string is already allocated on the objfile obstack, point
23503 directly to it. */
23504 {
23505 const char *attr_name = attr->as_string ();
23506 result = (const gdb_byte *) attr_name;
23507 *len = strlen (attr_name);
23508 }
23509 break;
23510 case DW_FORM_block1:
23511 case DW_FORM_block2:
23512 case DW_FORM_block4:
23513 case DW_FORM_block:
23514 case DW_FORM_exprloc:
23515 case DW_FORM_data16:
23516 {
23517 struct dwarf_block *block = attr->as_block ();
23518 result = block->data;
23519 *len = block->size;
23520 }
23521 break;
23522
23523 /* The DW_AT_const_value attributes are supposed to carry the
23524 symbol's value "represented as it would be on the target
23525 architecture." By the time we get here, it's already been
23526 converted to host endianness, so we just need to sign- or
23527 zero-extend it as appropriate. */
23528 case DW_FORM_data1:
23529 type = die_type (die, cu);
23530 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23531 if (result == NULL)
23532 result = write_constant_as_bytes (obstack, byte_order,
23533 type, value, len);
23534 break;
23535 case DW_FORM_data2:
23536 type = die_type (die, cu);
23537 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23538 if (result == NULL)
23539 result = write_constant_as_bytes (obstack, byte_order,
23540 type, value, len);
23541 break;
23542 case DW_FORM_data4:
23543 type = die_type (die, cu);
23544 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23545 if (result == NULL)
23546 result = write_constant_as_bytes (obstack, byte_order,
23547 type, value, len);
23548 break;
23549 case DW_FORM_data8:
23550 type = die_type (die, cu);
23551 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23552 if (result == NULL)
23553 result = write_constant_as_bytes (obstack, byte_order,
23554 type, value, len);
23555 break;
23556
23557 case DW_FORM_sdata:
23558 case DW_FORM_implicit_const:
23559 type = die_type (die, cu);
23560 result = write_constant_as_bytes (obstack, byte_order,
23561 type, attr->as_signed (), len);
23562 break;
23563
23564 case DW_FORM_udata:
23565 type = die_type (die, cu);
23566 result = write_constant_as_bytes (obstack, byte_order,
23567 type, attr->as_unsigned (), len);
23568 break;
23569
23570 default:
23571 complaint (_("unsupported const value attribute form: '%s'"),
23572 dwarf_form_name (attr->form));
23573 break;
23574 }
23575
23576 return result;
23577 }
23578
23579 /* See read.h. */
23580
23581 struct type *
23582 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23583 dwarf2_per_cu_data *per_cu,
23584 dwarf2_per_objfile *per_objfile,
23585 const char **var_name)
23586 {
23587 struct die_info *die;
23588
23589 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23590 if (cu == nullptr)
23591 cu = load_cu (per_cu, per_objfile, false);
23592
23593 if (cu == nullptr)
23594 return nullptr;
23595
23596 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23597 if (!die)
23598 return NULL;
23599
23600 if (var_name != nullptr)
23601 *var_name = var_decl_name (die, cu);
23602 return die_type (die, cu);
23603 }
23604
23605 /* See read.h. */
23606
23607 struct type *
23608 dwarf2_get_die_type (cu_offset die_offset,
23609 dwarf2_per_cu_data *per_cu,
23610 dwarf2_per_objfile *per_objfile)
23611 {
23612 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23613 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23614 }
23615
23616 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23617 On entry *REF_CU is the CU of SRC_DIE.
23618 On exit *REF_CU is the CU of the result.
23619 Returns NULL if the referenced DIE isn't found. */
23620
23621 static struct die_info *
23622 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23623 struct dwarf2_cu **ref_cu)
23624 {
23625 struct die_info temp_die;
23626 struct dwarf2_cu *sig_cu;
23627 struct die_info *die;
23628 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23629
23630
23631 /* While it might be nice to assert sig_type->type == NULL here,
23632 we can get here for DW_AT_imported_declaration where we need
23633 the DIE not the type. */
23634
23635 /* If necessary, add it to the queue and load its DIEs.
23636
23637 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23638 it doesn't mean they are currently loaded. Since we require them
23639 to be loaded, we must check for ourselves. */
23640 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23641 language_minimal)
23642 || per_objfile->get_cu (sig_type) == nullptr)
23643 read_signatured_type (sig_type, per_objfile);
23644
23645 sig_cu = per_objfile->get_cu (sig_type);
23646 gdb_assert (sig_cu != NULL);
23647 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23648 temp_die.sect_off = sig_type->type_offset_in_section;
23649 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23650 to_underlying (temp_die.sect_off));
23651 if (die)
23652 {
23653 /* For .gdb_index version 7 keep track of included TUs.
23654 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23655 if (per_objfile->per_bfd->index_table != NULL
23656 && per_objfile->per_bfd->index_table->version <= 7)
23657 {
23658 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23659 }
23660
23661 *ref_cu = sig_cu;
23662 return die;
23663 }
23664
23665 return NULL;
23666 }
23667
23668 /* Follow signatured type referenced by ATTR in SRC_DIE.
23669 On entry *REF_CU is the CU of SRC_DIE.
23670 On exit *REF_CU is the CU of the result.
23671 The result is the DIE of the type.
23672 If the referenced type cannot be found an error is thrown. */
23673
23674 static struct die_info *
23675 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23676 struct dwarf2_cu **ref_cu)
23677 {
23678 ULONGEST signature = attr->as_signature ();
23679 struct signatured_type *sig_type;
23680 struct die_info *die;
23681
23682 gdb_assert (attr->form == DW_FORM_ref_sig8);
23683
23684 sig_type = lookup_signatured_type (*ref_cu, signature);
23685 /* sig_type will be NULL if the signatured type is missing from
23686 the debug info. */
23687 if (sig_type == NULL)
23688 {
23689 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23690 " from DIE at %s [in module %s]"),
23691 hex_string (signature), sect_offset_str (src_die->sect_off),
23692 objfile_name ((*ref_cu)->per_objfile->objfile));
23693 }
23694
23695 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23696 if (die == NULL)
23697 {
23698 dump_die_for_error (src_die);
23699 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23700 " from DIE at %s [in module %s]"),
23701 hex_string (signature), sect_offset_str (src_die->sect_off),
23702 objfile_name ((*ref_cu)->per_objfile->objfile));
23703 }
23704
23705 return die;
23706 }
23707
23708 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23709 reading in and processing the type unit if necessary. */
23710
23711 static struct type *
23712 get_signatured_type (struct die_info *die, ULONGEST signature,
23713 struct dwarf2_cu *cu)
23714 {
23715 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23716 struct signatured_type *sig_type;
23717 struct dwarf2_cu *type_cu;
23718 struct die_info *type_die;
23719 struct type *type;
23720
23721 sig_type = lookup_signatured_type (cu, signature);
23722 /* sig_type will be NULL if the signatured type is missing from
23723 the debug info. */
23724 if (sig_type == NULL)
23725 {
23726 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23727 " from DIE at %s [in module %s]"),
23728 hex_string (signature), sect_offset_str (die->sect_off),
23729 objfile_name (per_objfile->objfile));
23730 return build_error_marker_type (cu, die);
23731 }
23732
23733 /* If we already know the type we're done. */
23734 type = per_objfile->get_type_for_signatured_type (sig_type);
23735 if (type != nullptr)
23736 return type;
23737
23738 type_cu = cu;
23739 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23740 if (type_die != NULL)
23741 {
23742 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23743 is created. This is important, for example, because for c++ classes
23744 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23745 type = read_type_die (type_die, type_cu);
23746 if (type == NULL)
23747 {
23748 complaint (_("Dwarf Error: Cannot build signatured type %s"
23749 " referenced from DIE at %s [in module %s]"),
23750 hex_string (signature), sect_offset_str (die->sect_off),
23751 objfile_name (per_objfile->objfile));
23752 type = build_error_marker_type (cu, die);
23753 }
23754 }
23755 else
23756 {
23757 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23758 " from DIE at %s [in module %s]"),
23759 hex_string (signature), sect_offset_str (die->sect_off),
23760 objfile_name (per_objfile->objfile));
23761 type = build_error_marker_type (cu, die);
23762 }
23763
23764 per_objfile->set_type_for_signatured_type (sig_type, type);
23765
23766 return type;
23767 }
23768
23769 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23770 reading in and processing the type unit if necessary. */
23771
23772 static struct type *
23773 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23774 struct dwarf2_cu *cu) /* ARI: editCase function */
23775 {
23776 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23777 if (attr->form_is_ref ())
23778 {
23779 struct dwarf2_cu *type_cu = cu;
23780 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23781
23782 return read_type_die (type_die, type_cu);
23783 }
23784 else if (attr->form == DW_FORM_ref_sig8)
23785 {
23786 return get_signatured_type (die, attr->as_signature (), cu);
23787 }
23788 else
23789 {
23790 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23791
23792 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23793 " at %s [in module %s]"),
23794 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23795 objfile_name (per_objfile->objfile));
23796 return build_error_marker_type (cu, die);
23797 }
23798 }
23799
23800 /* Load the DIEs associated with type unit PER_CU into memory. */
23801
23802 static void
23803 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23804 dwarf2_per_objfile *per_objfile)
23805 {
23806 struct signatured_type *sig_type;
23807
23808 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23809 gdb_assert (! per_cu->type_unit_group_p ());
23810
23811 /* We have the per_cu, but we need the signatured_type.
23812 Fortunately this is an easy translation. */
23813 gdb_assert (per_cu->is_debug_types);
23814 sig_type = (struct signatured_type *) per_cu;
23815
23816 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23817
23818 read_signatured_type (sig_type, per_objfile);
23819
23820 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23821 }
23822
23823 /* Read in a signatured type and build its CU and DIEs.
23824 If the type is a stub for the real type in a DWO file,
23825 read in the real type from the DWO file as well. */
23826
23827 static void
23828 read_signatured_type (signatured_type *sig_type,
23829 dwarf2_per_objfile *per_objfile)
23830 {
23831 gdb_assert (sig_type->is_debug_types);
23832 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23833
23834 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23835
23836 if (!reader.dummy_p)
23837 {
23838 struct dwarf2_cu *cu = reader.cu;
23839 const gdb_byte *info_ptr = reader.info_ptr;
23840
23841 gdb_assert (cu->die_hash == NULL);
23842 cu->die_hash =
23843 htab_create_alloc_ex (cu->header.length / 12,
23844 die_hash,
23845 die_eq,
23846 NULL,
23847 &cu->comp_unit_obstack,
23848 hashtab_obstack_allocate,
23849 dummy_obstack_deallocate);
23850
23851 if (reader.comp_unit_die->has_children)
23852 reader.comp_unit_die->child
23853 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23854 reader.comp_unit_die);
23855 cu->dies = reader.comp_unit_die;
23856 /* comp_unit_die is not stored in die_hash, no need. */
23857
23858 /* We try not to read any attributes in this function, because
23859 not all CUs needed for references have been loaded yet, and
23860 symbol table processing isn't initialized. But we have to
23861 set the CU language, or we won't be able to build types
23862 correctly. Similarly, if we do not read the producer, we can
23863 not apply producer-specific interpretation. */
23864 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23865
23866 reader.keep ();
23867 }
23868
23869 sig_type->tu_read = 1;
23870 }
23871
23872 /* Decode simple location descriptions.
23873 Given a pointer to a dwarf block that defines a location, compute
23874 the location and return the value. If COMPUTED is non-null, it is
23875 set to true to indicate that decoding was successful, and false
23876 otherwise. If COMPUTED is null, then this function may emit a
23877 complaint. */
23878
23879 static CORE_ADDR
23880 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23881 {
23882 struct objfile *objfile = cu->per_objfile->objfile;
23883 size_t i;
23884 size_t size = blk->size;
23885 const gdb_byte *data = blk->data;
23886 CORE_ADDR stack[64];
23887 int stacki;
23888 unsigned int bytes_read, unsnd;
23889 gdb_byte op;
23890
23891 if (computed != nullptr)
23892 *computed = false;
23893
23894 i = 0;
23895 stacki = 0;
23896 stack[stacki] = 0;
23897 stack[++stacki] = 0;
23898
23899 while (i < size)
23900 {
23901 op = data[i++];
23902 switch (op)
23903 {
23904 case DW_OP_lit0:
23905 case DW_OP_lit1:
23906 case DW_OP_lit2:
23907 case DW_OP_lit3:
23908 case DW_OP_lit4:
23909 case DW_OP_lit5:
23910 case DW_OP_lit6:
23911 case DW_OP_lit7:
23912 case DW_OP_lit8:
23913 case DW_OP_lit9:
23914 case DW_OP_lit10:
23915 case DW_OP_lit11:
23916 case DW_OP_lit12:
23917 case DW_OP_lit13:
23918 case DW_OP_lit14:
23919 case DW_OP_lit15:
23920 case DW_OP_lit16:
23921 case DW_OP_lit17:
23922 case DW_OP_lit18:
23923 case DW_OP_lit19:
23924 case DW_OP_lit20:
23925 case DW_OP_lit21:
23926 case DW_OP_lit22:
23927 case DW_OP_lit23:
23928 case DW_OP_lit24:
23929 case DW_OP_lit25:
23930 case DW_OP_lit26:
23931 case DW_OP_lit27:
23932 case DW_OP_lit28:
23933 case DW_OP_lit29:
23934 case DW_OP_lit30:
23935 case DW_OP_lit31:
23936 stack[++stacki] = op - DW_OP_lit0;
23937 break;
23938
23939 case DW_OP_reg0:
23940 case DW_OP_reg1:
23941 case DW_OP_reg2:
23942 case DW_OP_reg3:
23943 case DW_OP_reg4:
23944 case DW_OP_reg5:
23945 case DW_OP_reg6:
23946 case DW_OP_reg7:
23947 case DW_OP_reg8:
23948 case DW_OP_reg9:
23949 case DW_OP_reg10:
23950 case DW_OP_reg11:
23951 case DW_OP_reg12:
23952 case DW_OP_reg13:
23953 case DW_OP_reg14:
23954 case DW_OP_reg15:
23955 case DW_OP_reg16:
23956 case DW_OP_reg17:
23957 case DW_OP_reg18:
23958 case DW_OP_reg19:
23959 case DW_OP_reg20:
23960 case DW_OP_reg21:
23961 case DW_OP_reg22:
23962 case DW_OP_reg23:
23963 case DW_OP_reg24:
23964 case DW_OP_reg25:
23965 case DW_OP_reg26:
23966 case DW_OP_reg27:
23967 case DW_OP_reg28:
23968 case DW_OP_reg29:
23969 case DW_OP_reg30:
23970 case DW_OP_reg31:
23971 stack[++stacki] = op - DW_OP_reg0;
23972 if (i < size)
23973 {
23974 if (computed == nullptr)
23975 dwarf2_complex_location_expr_complaint ();
23976 else
23977 return 0;
23978 }
23979 break;
23980
23981 case DW_OP_regx:
23982 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23983 i += bytes_read;
23984 stack[++stacki] = unsnd;
23985 if (i < size)
23986 {
23987 if (computed == nullptr)
23988 dwarf2_complex_location_expr_complaint ();
23989 else
23990 return 0;
23991 }
23992 break;
23993
23994 case DW_OP_addr:
23995 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23996 &bytes_read);
23997 i += bytes_read;
23998 break;
23999
24000 case DW_OP_const1u:
24001 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24002 i += 1;
24003 break;
24004
24005 case DW_OP_const1s:
24006 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24007 i += 1;
24008 break;
24009
24010 case DW_OP_const2u:
24011 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24012 i += 2;
24013 break;
24014
24015 case DW_OP_const2s:
24016 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24017 i += 2;
24018 break;
24019
24020 case DW_OP_const4u:
24021 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24022 i += 4;
24023 break;
24024
24025 case DW_OP_const4s:
24026 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24027 i += 4;
24028 break;
24029
24030 case DW_OP_const8u:
24031 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24032 i += 8;
24033 break;
24034
24035 case DW_OP_constu:
24036 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24037 &bytes_read);
24038 i += bytes_read;
24039 break;
24040
24041 case DW_OP_consts:
24042 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24043 i += bytes_read;
24044 break;
24045
24046 case DW_OP_dup:
24047 stack[stacki + 1] = stack[stacki];
24048 stacki++;
24049 break;
24050
24051 case DW_OP_plus:
24052 stack[stacki - 1] += stack[stacki];
24053 stacki--;
24054 break;
24055
24056 case DW_OP_plus_uconst:
24057 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24058 &bytes_read);
24059 i += bytes_read;
24060 break;
24061
24062 case DW_OP_minus:
24063 stack[stacki - 1] -= stack[stacki];
24064 stacki--;
24065 break;
24066
24067 case DW_OP_deref:
24068 /* If we're not the last op, then we definitely can't encode
24069 this using GDB's address_class enum. This is valid for partial
24070 global symbols, although the variable's address will be bogus
24071 in the psymtab. */
24072 if (i < size)
24073 {
24074 if (computed == nullptr)
24075 dwarf2_complex_location_expr_complaint ();
24076 else
24077 return 0;
24078 }
24079 break;
24080
24081 case DW_OP_GNU_push_tls_address:
24082 case DW_OP_form_tls_address:
24083 /* The top of the stack has the offset from the beginning
24084 of the thread control block at which the variable is located. */
24085 /* Nothing should follow this operator, so the top of stack would
24086 be returned. */
24087 /* This is valid for partial global symbols, but the variable's
24088 address will be bogus in the psymtab. Make it always at least
24089 non-zero to not look as a variable garbage collected by linker
24090 which have DW_OP_addr 0. */
24091 if (i < size)
24092 {
24093 if (computed == nullptr)
24094 dwarf2_complex_location_expr_complaint ();
24095 else
24096 return 0;
24097 }
24098 stack[stacki]++;
24099 break;
24100
24101 case DW_OP_GNU_uninit:
24102 if (computed != nullptr)
24103 return 0;
24104 break;
24105
24106 case DW_OP_addrx:
24107 case DW_OP_GNU_addr_index:
24108 case DW_OP_GNU_const_index:
24109 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24110 &bytes_read);
24111 i += bytes_read;
24112 break;
24113
24114 default:
24115 if (computed == nullptr)
24116 {
24117 const char *name = get_DW_OP_name (op);
24118
24119 if (name)
24120 complaint (_("unsupported stack op: '%s'"),
24121 name);
24122 else
24123 complaint (_("unsupported stack op: '%02x'"),
24124 op);
24125 }
24126
24127 return (stack[stacki]);
24128 }
24129
24130 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24131 outside of the allocated space. Also enforce minimum>0. */
24132 if (stacki >= ARRAY_SIZE (stack) - 1)
24133 {
24134 if (computed == nullptr)
24135 complaint (_("location description stack overflow"));
24136 return 0;
24137 }
24138
24139 if (stacki <= 0)
24140 {
24141 if (computed == nullptr)
24142 complaint (_("location description stack underflow"));
24143 return 0;
24144 }
24145 }
24146
24147 if (computed != nullptr)
24148 *computed = true;
24149 return (stack[stacki]);
24150 }
24151
24152 /* memory allocation interface */
24153
24154 static struct dwarf_block *
24155 dwarf_alloc_block (struct dwarf2_cu *cu)
24156 {
24157 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24158 }
24159
24160 static struct die_info *
24161 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24162 {
24163 struct die_info *die;
24164 size_t size = sizeof (struct die_info);
24165
24166 if (num_attrs > 1)
24167 size += (num_attrs - 1) * sizeof (struct attribute);
24168
24169 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24170 memset (die, 0, sizeof (struct die_info));
24171 return (die);
24172 }
24173
24174 \f
24175
24176 /* Macro support. */
24177
24178 /* An overload of dwarf_decode_macros that finds the correct section
24179 and ensures it is read in before calling the other overload. */
24180
24181 static void
24182 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24183 int section_is_gnu)
24184 {
24185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24186 struct objfile *objfile = per_objfile->objfile;
24187 const struct line_header *lh = cu->line_header;
24188 unsigned int offset_size = cu->header.offset_size;
24189 struct dwarf2_section_info *section;
24190 const char *section_name;
24191
24192 if (cu->dwo_unit != nullptr)
24193 {
24194 if (section_is_gnu)
24195 {
24196 section = &cu->dwo_unit->dwo_file->sections.macro;
24197 section_name = ".debug_macro.dwo";
24198 }
24199 else
24200 {
24201 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24202 section_name = ".debug_macinfo.dwo";
24203 }
24204 }
24205 else
24206 {
24207 if (section_is_gnu)
24208 {
24209 section = &per_objfile->per_bfd->macro;
24210 section_name = ".debug_macro";
24211 }
24212 else
24213 {
24214 section = &per_objfile->per_bfd->macinfo;
24215 section_name = ".debug_macinfo";
24216 }
24217 }
24218
24219 section->read (objfile);
24220 if (section->buffer == nullptr)
24221 {
24222 complaint (_("missing %s section"), section_name);
24223 return;
24224 }
24225
24226 buildsym_compunit *builder = cu->get_builder ();
24227
24228 struct dwarf2_section_info *str_offsets_section;
24229 struct dwarf2_section_info *str_section;
24230 gdb::optional<ULONGEST> str_offsets_base;
24231
24232 if (cu->dwo_unit != nullptr)
24233 {
24234 str_offsets_section = &cu->dwo_unit->dwo_file
24235 ->sections.str_offsets;
24236 str_section = &cu->dwo_unit->dwo_file->sections.str;
24237 str_offsets_base = cu->header.addr_size;
24238 }
24239 else
24240 {
24241 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24242 str_section = &per_objfile->per_bfd->str;
24243 str_offsets_base = cu->str_offsets_base;
24244 }
24245
24246 dwarf_decode_macros (per_objfile, builder, section, lh,
24247 offset_size, offset, str_section, str_offsets_section,
24248 str_offsets_base, section_is_gnu);
24249 }
24250
24251 /* Return the .debug_loc section to use for CU.
24252 For DWO files use .debug_loc.dwo. */
24253
24254 static struct dwarf2_section_info *
24255 cu_debug_loc_section (struct dwarf2_cu *cu)
24256 {
24257 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24258
24259 if (cu->dwo_unit)
24260 {
24261 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24262
24263 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24264 }
24265 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24266 : &per_objfile->per_bfd->loc);
24267 }
24268
24269 /* Return the .debug_rnglists section to use for CU. */
24270 static struct dwarf2_section_info *
24271 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24272 {
24273 if (cu->header.version < 5)
24274 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24275 cu->header.version);
24276 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24277
24278 /* Make sure we read the .debug_rnglists section from the file that
24279 contains the DW_AT_ranges attribute we are reading. Normally that
24280 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24281 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24282 program. */
24283 if (cu->dwo_unit != nullptr
24284 && tag != DW_TAG_compile_unit
24285 && tag != DW_TAG_skeleton_unit)
24286 {
24287 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24288
24289 if (sections->rnglists.size > 0)
24290 return &sections->rnglists;
24291 else
24292 error (_(".debug_rnglists section is missing from .dwo file."));
24293 }
24294 return &dwarf2_per_objfile->per_bfd->rnglists;
24295 }
24296
24297 /* A helper function that fills in a dwarf2_loclist_baton. */
24298
24299 static void
24300 fill_in_loclist_baton (struct dwarf2_cu *cu,
24301 struct dwarf2_loclist_baton *baton,
24302 const struct attribute *attr)
24303 {
24304 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24305 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24306
24307 section->read (per_objfile->objfile);
24308
24309 baton->per_objfile = per_objfile;
24310 baton->per_cu = cu->per_cu;
24311 gdb_assert (baton->per_cu);
24312 /* We don't know how long the location list is, but make sure we
24313 don't run off the edge of the section. */
24314 baton->size = section->size - attr->as_unsigned ();
24315 baton->data = section->buffer + attr->as_unsigned ();
24316 if (cu->base_address.has_value ())
24317 baton->base_address = *cu->base_address;
24318 else
24319 baton->base_address = 0;
24320 baton->from_dwo = cu->dwo_unit != NULL;
24321 }
24322
24323 static void
24324 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24325 struct dwarf2_cu *cu, int is_block)
24326 {
24327 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24328 struct objfile *objfile = per_objfile->objfile;
24329 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24330
24331 if (attr->form_is_section_offset ()
24332 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24333 the section. If so, fall through to the complaint in the
24334 other branch. */
24335 && attr->as_unsigned () < section->get_size (objfile))
24336 {
24337 struct dwarf2_loclist_baton *baton;
24338
24339 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24340
24341 fill_in_loclist_baton (cu, baton, attr);
24342
24343 if (!cu->base_address.has_value ())
24344 complaint (_("Location list used without "
24345 "specifying the CU base address."));
24346
24347 SYMBOL_ACLASS_INDEX (sym) = (is_block
24348 ? dwarf2_loclist_block_index
24349 : dwarf2_loclist_index);
24350 SYMBOL_LOCATION_BATON (sym) = baton;
24351 }
24352 else
24353 {
24354 struct dwarf2_locexpr_baton *baton;
24355
24356 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24357 baton->per_objfile = per_objfile;
24358 baton->per_cu = cu->per_cu;
24359 gdb_assert (baton->per_cu);
24360
24361 if (attr->form_is_block ())
24362 {
24363 /* Note that we're just copying the block's data pointer
24364 here, not the actual data. We're still pointing into the
24365 info_buffer for SYM's objfile; right now we never release
24366 that buffer, but when we do clean up properly this may
24367 need to change. */
24368 struct dwarf_block *block = attr->as_block ();
24369 baton->size = block->size;
24370 baton->data = block->data;
24371 }
24372 else
24373 {
24374 dwarf2_invalid_attrib_class_complaint ("location description",
24375 sym->natural_name ());
24376 baton->size = 0;
24377 }
24378
24379 SYMBOL_ACLASS_INDEX (sym) = (is_block
24380 ? dwarf2_locexpr_block_index
24381 : dwarf2_locexpr_index);
24382 SYMBOL_LOCATION_BATON (sym) = baton;
24383 }
24384 }
24385
24386 /* See read.h. */
24387
24388 const comp_unit_head *
24389 dwarf2_per_cu_data::get_header () const
24390 {
24391 if (!m_header_read_in)
24392 {
24393 const gdb_byte *info_ptr
24394 = this->section->buffer + to_underlying (this->sect_off);
24395
24396 memset (&m_header, 0, sizeof (m_header));
24397
24398 read_comp_unit_head (&m_header, info_ptr, this->section,
24399 rcuh_kind::COMPILE);
24400
24401 m_header_read_in = true;
24402 }
24403
24404 return &m_header;
24405 }
24406
24407 /* See read.h. */
24408
24409 int
24410 dwarf2_per_cu_data::addr_size () const
24411 {
24412 return this->get_header ()->addr_size;
24413 }
24414
24415 /* See read.h. */
24416
24417 int
24418 dwarf2_per_cu_data::offset_size () const
24419 {
24420 return this->get_header ()->offset_size;
24421 }
24422
24423 /* See read.h. */
24424
24425 int
24426 dwarf2_per_cu_data::ref_addr_size () const
24427 {
24428 const comp_unit_head *header = this->get_header ();
24429
24430 if (header->version == 2)
24431 return header->addr_size;
24432 else
24433 return header->offset_size;
24434 }
24435
24436 /* A helper function for dwarf2_find_containing_comp_unit that returns
24437 the index of the result, and that searches a vector. It will
24438 return a result even if the offset in question does not actually
24439 occur in any CU. This is separate so that it can be unit
24440 tested. */
24441
24442 static int
24443 dwarf2_find_containing_comp_unit
24444 (sect_offset sect_off,
24445 unsigned int offset_in_dwz,
24446 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24447 {
24448 int low, high;
24449
24450 low = 0;
24451 high = all_comp_units.size () - 1;
24452 while (high > low)
24453 {
24454 struct dwarf2_per_cu_data *mid_cu;
24455 int mid = low + (high - low) / 2;
24456
24457 mid_cu = all_comp_units[mid].get ();
24458 if (mid_cu->is_dwz > offset_in_dwz
24459 || (mid_cu->is_dwz == offset_in_dwz
24460 && mid_cu->sect_off + mid_cu->length > sect_off))
24461 high = mid;
24462 else
24463 low = mid + 1;
24464 }
24465 gdb_assert (low == high);
24466 return low;
24467 }
24468
24469 /* Locate the .debug_info compilation unit from CU's objfile which contains
24470 the DIE at OFFSET. Raises an error on failure. */
24471
24472 static struct dwarf2_per_cu_data *
24473 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24474 unsigned int offset_in_dwz,
24475 dwarf2_per_objfile *per_objfile)
24476 {
24477 int low = dwarf2_find_containing_comp_unit
24478 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24479 dwarf2_per_cu_data *this_cu
24480 = per_objfile->per_bfd->all_comp_units[low].get ();
24481
24482 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24483 {
24484 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24485 error (_("Dwarf Error: could not find partial DIE containing "
24486 "offset %s [in module %s]"),
24487 sect_offset_str (sect_off),
24488 bfd_get_filename (per_objfile->objfile->obfd));
24489
24490 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24491 <= sect_off);
24492 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
24493 }
24494 else
24495 {
24496 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24497 && sect_off >= this_cu->sect_off + this_cu->length)
24498 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24499 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24500 return this_cu;
24501 }
24502 }
24503
24504 #if GDB_SELF_TEST
24505
24506 namespace selftests {
24507 namespace find_containing_comp_unit {
24508
24509 static void
24510 run_test ()
24511 {
24512 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24513 dwarf2_per_cu_data *one_ptr = one.get ();
24514 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24515 dwarf2_per_cu_data *two_ptr = two.get ();
24516 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24517 dwarf2_per_cu_data *three_ptr = three.get ();
24518 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24519 dwarf2_per_cu_data *four_ptr = four.get ();
24520
24521 one->length = 5;
24522 two->sect_off = sect_offset (one->length);
24523 two->length = 7;
24524
24525 three->length = 5;
24526 three->is_dwz = 1;
24527 four->sect_off = sect_offset (three->length);
24528 four->length = 7;
24529 four->is_dwz = 1;
24530
24531 std::vector<dwarf2_per_cu_data_up> units;
24532 units.push_back (std::move (one));
24533 units.push_back (std::move (two));
24534 units.push_back (std::move (three));
24535 units.push_back (std::move (four));
24536
24537 int result;
24538
24539 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24540 SELF_CHECK (units[result].get () == one_ptr);
24541 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24542 SELF_CHECK (units[result].get () == one_ptr);
24543 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24544 SELF_CHECK (units[result].get () == two_ptr);
24545
24546 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24547 SELF_CHECK (units[result].get () == three_ptr);
24548 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24549 SELF_CHECK (units[result].get () == three_ptr);
24550 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24551 SELF_CHECK (units[result].get () == four_ptr);
24552 }
24553
24554 }
24555 }
24556
24557 #endif /* GDB_SELF_TEST */
24558
24559 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24560
24561 static void
24562 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24563 enum language pretend_language)
24564 {
24565 struct attribute *attr;
24566
24567 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24568
24569 /* Set the language we're debugging. */
24570 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24571 if (cu->producer != nullptr
24572 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
24573 {
24574 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24575 attribute is not standardised yet. As a workaround for the
24576 language detection we fall back to the DW_AT_producer
24577 string. */
24578 cu->per_cu->lang = language_opencl;
24579 }
24580 else if (cu->producer != nullptr
24581 && strstr (cu->producer, "GNU Go ") != NULL)
24582 {
24583 /* Similar hack for Go. */
24584 cu->per_cu->lang = language_go;
24585 }
24586 else if (attr != nullptr)
24587 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
24588 else
24589 cu->per_cu->lang = pretend_language;
24590 cu->language_defn = language_def (cu->per_cu->lang);
24591 }
24592
24593 /* See read.h. */
24594
24595 dwarf2_cu *
24596 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24597 {
24598 auto it = m_dwarf2_cus.find (per_cu);
24599 if (it == m_dwarf2_cus.end ())
24600 return nullptr;
24601
24602 return it->second;
24603 }
24604
24605 /* See read.h. */
24606
24607 void
24608 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24609 {
24610 gdb_assert (this->get_cu (per_cu) == nullptr);
24611
24612 m_dwarf2_cus[per_cu] = cu;
24613 }
24614
24615 /* See read.h. */
24616
24617 void
24618 dwarf2_per_objfile::age_comp_units ()
24619 {
24620 dwarf_read_debug_printf_v ("running");
24621
24622 /* This is not expected to be called in the middle of CU expansion. There is
24623 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24624 loaded in memory. Calling age_comp_units while the queue is in use could
24625 make us free the DIEs for a CU that is in the queue and therefore break
24626 that invariant. */
24627 gdb_assert (!this->per_bfd->queue.has_value ());
24628
24629 /* Start by clearing all marks. */
24630 for (auto pair : m_dwarf2_cus)
24631 pair.second->clear_mark ();
24632
24633 /* Traverse all CUs, mark them and their dependencies if used recently
24634 enough. */
24635 for (auto pair : m_dwarf2_cus)
24636 {
24637 dwarf2_cu *cu = pair.second;
24638
24639 cu->last_used++;
24640 if (cu->last_used <= dwarf_max_cache_age)
24641 cu->mark ();
24642 }
24643
24644 /* Delete all CUs still not marked. */
24645 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24646 {
24647 dwarf2_cu *cu = it->second;
24648
24649 if (!cu->is_marked ())
24650 {
24651 dwarf_read_debug_printf_v ("deleting old CU %s",
24652 sect_offset_str (cu->per_cu->sect_off));
24653 delete cu;
24654 it = m_dwarf2_cus.erase (it);
24655 }
24656 else
24657 it++;
24658 }
24659 }
24660
24661 /* See read.h. */
24662
24663 void
24664 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24665 {
24666 auto it = m_dwarf2_cus.find (per_cu);
24667 if (it == m_dwarf2_cus.end ())
24668 return;
24669
24670 delete it->second;
24671
24672 m_dwarf2_cus.erase (it);
24673 }
24674
24675 dwarf2_per_objfile::~dwarf2_per_objfile ()
24676 {
24677 remove_all_cus ();
24678 }
24679
24680 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24681 We store these in a hash table separate from the DIEs, and preserve them
24682 when the DIEs are flushed out of cache.
24683
24684 The CU "per_cu" pointer is needed because offset alone is not enough to
24685 uniquely identify the type. A file may have multiple .debug_types sections,
24686 or the type may come from a DWO file. Furthermore, while it's more logical
24687 to use per_cu->section+offset, with Fission the section with the data is in
24688 the DWO file but we don't know that section at the point we need it.
24689 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24690 because we can enter the lookup routine, get_die_type_at_offset, from
24691 outside this file, and thus won't necessarily have PER_CU->cu.
24692 Fortunately, PER_CU is stable for the life of the objfile. */
24693
24694 struct dwarf2_per_cu_offset_and_type
24695 {
24696 const struct dwarf2_per_cu_data *per_cu;
24697 sect_offset sect_off;
24698 struct type *type;
24699 };
24700
24701 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24702
24703 static hashval_t
24704 per_cu_offset_and_type_hash (const void *item)
24705 {
24706 const struct dwarf2_per_cu_offset_and_type *ofs
24707 = (const struct dwarf2_per_cu_offset_and_type *) item;
24708
24709 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24710 }
24711
24712 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24713
24714 static int
24715 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24716 {
24717 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24718 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24719 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24720 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24721
24722 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24723 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24724 }
24725
24726 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24727 table if necessary. For convenience, return TYPE.
24728
24729 The DIEs reading must have careful ordering to:
24730 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24731 reading current DIE.
24732 * Not trying to dereference contents of still incompletely read in types
24733 while reading in other DIEs.
24734 * Enable referencing still incompletely read in types just by a pointer to
24735 the type without accessing its fields.
24736
24737 Therefore caller should follow these rules:
24738 * Try to fetch any prerequisite types we may need to build this DIE type
24739 before building the type and calling set_die_type.
24740 * After building type call set_die_type for current DIE as soon as
24741 possible before fetching more types to complete the current type.
24742 * Make the type as complete as possible before fetching more types. */
24743
24744 static struct type *
24745 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24746 bool skip_data_location)
24747 {
24748 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24749 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24750 struct objfile *objfile = per_objfile->objfile;
24751 struct attribute *attr;
24752 struct dynamic_prop prop;
24753
24754 /* For Ada types, make sure that the gnat-specific data is always
24755 initialized (if not already set). There are a few types where
24756 we should not be doing so, because the type-specific area is
24757 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24758 where the type-specific area is used to store the floatformat).
24759 But this is not a problem, because the gnat-specific information
24760 is actually not needed for these types. */
24761 if (need_gnat_info (cu)
24762 && type->code () != TYPE_CODE_FUNC
24763 && type->code () != TYPE_CODE_FLT
24764 && type->code () != TYPE_CODE_METHODPTR
24765 && type->code () != TYPE_CODE_MEMBERPTR
24766 && type->code () != TYPE_CODE_METHOD
24767 && type->code () != TYPE_CODE_FIXED_POINT
24768 && !HAVE_GNAT_AUX_INFO (type))
24769 INIT_GNAT_SPECIFIC (type);
24770
24771 /* Read DW_AT_allocated and set in type. */
24772 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24773 if (attr != NULL)
24774 {
24775 struct type *prop_type = cu->addr_sized_int_type (false);
24776 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24777 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24778 }
24779
24780 /* Read DW_AT_associated and set in type. */
24781 attr = dwarf2_attr (die, DW_AT_associated, cu);
24782 if (attr != NULL)
24783 {
24784 struct type *prop_type = cu->addr_sized_int_type (false);
24785 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24786 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24787 }
24788
24789 /* Read DW_AT_data_location and set in type. */
24790 if (!skip_data_location)
24791 {
24792 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24793 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24794 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24795 }
24796
24797 if (per_objfile->die_type_hash == NULL)
24798 per_objfile->die_type_hash
24799 = htab_up (htab_create_alloc (127,
24800 per_cu_offset_and_type_hash,
24801 per_cu_offset_and_type_eq,
24802 NULL, xcalloc, xfree));
24803
24804 ofs.per_cu = cu->per_cu;
24805 ofs.sect_off = die->sect_off;
24806 ofs.type = type;
24807 slot = (struct dwarf2_per_cu_offset_and_type **)
24808 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24809 if (*slot)
24810 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24811 sect_offset_str (die->sect_off));
24812 *slot = XOBNEW (&objfile->objfile_obstack,
24813 struct dwarf2_per_cu_offset_and_type);
24814 **slot = ofs;
24815 return type;
24816 }
24817
24818 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24819 or return NULL if the die does not have a saved type. */
24820
24821 static struct type *
24822 get_die_type_at_offset (sect_offset sect_off,
24823 dwarf2_per_cu_data *per_cu,
24824 dwarf2_per_objfile *per_objfile)
24825 {
24826 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24827
24828 if (per_objfile->die_type_hash == NULL)
24829 return NULL;
24830
24831 ofs.per_cu = per_cu;
24832 ofs.sect_off = sect_off;
24833 slot = ((struct dwarf2_per_cu_offset_and_type *)
24834 htab_find (per_objfile->die_type_hash.get (), &ofs));
24835 if (slot)
24836 return slot->type;
24837 else
24838 return NULL;
24839 }
24840
24841 /* Look up the type for DIE in CU in die_type_hash,
24842 or return NULL if DIE does not have a saved type. */
24843
24844 static struct type *
24845 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24846 {
24847 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24848 }
24849
24850 /* Trivial hash function for partial_die_info: the hash value of a DIE
24851 is its offset in .debug_info for this objfile. */
24852
24853 static hashval_t
24854 partial_die_hash (const void *item)
24855 {
24856 const struct partial_die_info *part_die
24857 = (const struct partial_die_info *) item;
24858
24859 return to_underlying (part_die->sect_off);
24860 }
24861
24862 /* Trivial comparison function for partial_die_info structures: two DIEs
24863 are equal if they have the same offset. */
24864
24865 static int
24866 partial_die_eq (const void *item_lhs, const void *item_rhs)
24867 {
24868 const struct partial_die_info *part_die_lhs
24869 = (const struct partial_die_info *) item_lhs;
24870 const struct partial_die_info *part_die_rhs
24871 = (const struct partial_die_info *) item_rhs;
24872
24873 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24874 }
24875
24876 struct cmd_list_element *set_dwarf_cmdlist;
24877 struct cmd_list_element *show_dwarf_cmdlist;
24878
24879 static void
24880 show_check_physname (struct ui_file *file, int from_tty,
24881 struct cmd_list_element *c, const char *value)
24882 {
24883 fprintf_filtered (file,
24884 _("Whether to check \"physname\" is %s.\n"),
24885 value);
24886 }
24887
24888 void _initialize_dwarf2_read ();
24889 void
24890 _initialize_dwarf2_read ()
24891 {
24892 add_setshow_prefix_cmd ("dwarf", class_maintenance,
24893 _("\
24894 Set DWARF specific variables.\n\
24895 Configure DWARF variables such as the cache size."),
24896 _("\
24897 Show DWARF specific variables.\n\
24898 Show DWARF variables such as the cache size."),
24899 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
24900 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
24901
24902 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24903 &dwarf_max_cache_age, _("\
24904 Set the upper bound on the age of cached DWARF compilation units."), _("\
24905 Show the upper bound on the age of cached DWARF compilation units."), _("\
24906 A higher limit means that cached compilation units will be stored\n\
24907 in memory longer, and more total memory will be used. Zero disables\n\
24908 caching, which can slow down startup."),
24909 NULL,
24910 show_dwarf_max_cache_age,
24911 &set_dwarf_cmdlist,
24912 &show_dwarf_cmdlist);
24913
24914 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24915 Set debugging of the DWARF reader."), _("\
24916 Show debugging of the DWARF reader."), _("\
24917 When enabled (non-zero), debugging messages are printed during DWARF\n\
24918 reading and symtab expansion. A value of 1 (one) provides basic\n\
24919 information. A value greater than 1 provides more verbose information."),
24920 NULL,
24921 NULL,
24922 &setdebuglist, &showdebuglist);
24923
24924 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24925 Set debugging of the DWARF DIE reader."), _("\
24926 Show debugging of the DWARF DIE reader."), _("\
24927 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24928 The value is the maximum depth to print."),
24929 NULL,
24930 NULL,
24931 &setdebuglist, &showdebuglist);
24932
24933 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24934 Set debugging of the dwarf line reader."), _("\
24935 Show debugging of the dwarf line reader."), _("\
24936 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24937 A value of 1 (one) provides basic information.\n\
24938 A value greater than 1 provides more verbose information."),
24939 NULL,
24940 NULL,
24941 &setdebuglist, &showdebuglist);
24942
24943 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24944 Set cross-checking of \"physname\" code against demangler."), _("\
24945 Show cross-checking of \"physname\" code against demangler."), _("\
24946 When enabled, GDB's internal \"physname\" code is checked against\n\
24947 the demangler."),
24948 NULL, show_check_physname,
24949 &setdebuglist, &showdebuglist);
24950
24951 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24952 no_class, &use_deprecated_index_sections, _("\
24953 Set whether to use deprecated gdb_index sections."), _("\
24954 Show whether to use deprecated gdb_index sections."), _("\
24955 When enabled, deprecated .gdb_index sections are used anyway.\n\
24956 Normally they are ignored either because of a missing feature or\n\
24957 performance issue.\n\
24958 Warning: This option must be enabled before gdb reads the file."),
24959 NULL,
24960 NULL,
24961 &setlist, &showlist);
24962
24963 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24964 &dwarf2_locexpr_funcs);
24965 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24966 &dwarf2_loclist_funcs);
24967
24968 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24969 &dwarf2_block_frame_base_locexpr_funcs);
24970 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24971 &dwarf2_block_frame_base_loclist_funcs);
24972
24973 #if GDB_SELF_TEST
24974 selftests::register_test ("dw2_expand_symtabs_matching",
24975 selftests::dw2_expand_symtabs_matching::run_test);
24976 selftests::register_test ("dwarf2_find_containing_comp_unit",
24977 selftests::find_containing_comp_unit::run_test);
24978 #endif
24979 }