]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Unify gdb printf functions
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "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 dwarf2/read.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 gdb_printf (file, _("The upper bound on the age of cached "
1094 "DWARF compilation units is %s.\n"),
1095 value);
1096 }
1097 \f
1098 /* local function prototypes */
1099
1100 static void dwarf2_find_base_address (struct die_info *die,
1101 struct dwarf2_cu *cu);
1102
1103 static dwarf2_psymtab *create_partial_symtab
1104 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1105 const char *name);
1106
1107 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1108 const gdb_byte *info_ptr,
1109 struct die_info *type_unit_die);
1110
1111 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1112
1113 static void scan_partial_symbols (struct partial_die_info *,
1114 CORE_ADDR *, CORE_ADDR *,
1115 int, struct dwarf2_cu *);
1116
1117 static void add_partial_symbol (struct partial_die_info *,
1118 struct dwarf2_cu *);
1119
1120 static void add_partial_namespace (struct partial_die_info *pdi,
1121 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1122 int set_addrmap, struct dwarf2_cu *cu);
1123
1124 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1125 CORE_ADDR *highpc, int set_addrmap,
1126 struct dwarf2_cu *cu);
1127
1128 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1129 struct dwarf2_cu *cu);
1130
1131 static void add_partial_subprogram (struct partial_die_info *pdi,
1132 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1133 int need_pc, struct dwarf2_cu *cu);
1134
1135 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1136
1137 static struct partial_die_info *load_partial_dies
1138 (const struct die_reader_specs *, const gdb_byte *, int);
1139
1140 /* A pair of partial_die_info and compilation unit. */
1141 struct cu_partial_die_info
1142 {
1143 /* The compilation unit of the partial_die_info. */
1144 struct dwarf2_cu *cu;
1145 /* A partial_die_info. */
1146 struct partial_die_info *pdi;
1147
1148 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1149 : cu (cu),
1150 pdi (pdi)
1151 { /* Nothing. */ }
1152
1153 private:
1154 cu_partial_die_info () = delete;
1155 };
1156
1157 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1158 struct dwarf2_cu *);
1159
1160 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1161 struct attribute *,
1162 const struct attr_abbrev *,
1163 const gdb_byte *);
1164
1165 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1166 struct attribute *attr, dwarf_tag tag);
1167
1168 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1169
1170 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1171 dwarf2_section_info *, sect_offset);
1172
1173 static const char *read_indirect_string
1174 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1175 const struct comp_unit_head *, unsigned int *);
1176
1177 static const char *read_indirect_string_at_offset
1178 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1179
1180 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1181 const gdb_byte *,
1182 unsigned int *);
1183
1184 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1185 ULONGEST str_index);
1186
1187 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1188 ULONGEST str_index);
1189
1190 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1191 struct dwarf2_cu *);
1192
1193 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1194 struct dwarf2_cu *cu);
1195
1196 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1197
1198 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1199 struct dwarf2_cu *cu);
1200
1201 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1202
1203 static struct die_info *die_specification (struct die_info *die,
1204 struct dwarf2_cu **);
1205
1206 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1207 struct dwarf2_cu *cu);
1208
1209 static void dwarf_decode_lines (struct line_header *,
1210 const file_and_directory &,
1211 struct dwarf2_cu *, dwarf2_psymtab *,
1212 CORE_ADDR, int decode_mapping);
1213
1214 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1215 const char *);
1216
1217 static struct symbol *new_symbol (struct die_info *, struct type *,
1218 struct dwarf2_cu *, struct symbol * = NULL);
1219
1220 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1221 struct dwarf2_cu *);
1222
1223 static void dwarf2_const_value_attr (const struct attribute *attr,
1224 struct type *type,
1225 const char *name,
1226 struct obstack *obstack,
1227 struct dwarf2_cu *cu, LONGEST *value,
1228 const gdb_byte **bytes,
1229 struct dwarf2_locexpr_baton **baton);
1230
1231 static struct type *read_subrange_index_type (struct die_info *die,
1232 struct dwarf2_cu *cu);
1233
1234 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1235
1236 static int need_gnat_info (struct dwarf2_cu *);
1237
1238 static struct type *die_descriptive_type (struct die_info *,
1239 struct dwarf2_cu *);
1240
1241 static void set_descriptive_type (struct type *, struct die_info *,
1242 struct dwarf2_cu *);
1243
1244 static struct type *die_containing_type (struct die_info *,
1245 struct dwarf2_cu *);
1246
1247 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1248 struct dwarf2_cu *);
1249
1250 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1251
1252 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1253
1254 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1255
1256 static char *typename_concat (struct obstack *obs, const char *prefix,
1257 const char *suffix, int physname,
1258 struct dwarf2_cu *cu);
1259
1260 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1261
1262 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1263
1264 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1265
1266 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1267
1268 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1269
1270 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1271
1272 /* Return the .debug_loclists section to use for cu. */
1273 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1274
1275 /* Return the .debug_rnglists section to use for cu. */
1276 static struct dwarf2_section_info *cu_debug_rnglists_section
1277 (struct dwarf2_cu *cu, dwarf_tag tag);
1278
1279 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1280 values. Keep the items ordered with increasing constraints compliance. */
1281 enum pc_bounds_kind
1282 {
1283 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1284 PC_BOUNDS_NOT_PRESENT,
1285
1286 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1287 were present but they do not form a valid range of PC addresses. */
1288 PC_BOUNDS_INVALID,
1289
1290 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1291 PC_BOUNDS_RANGES,
1292
1293 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1294 PC_BOUNDS_HIGH_LOW,
1295 };
1296
1297 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1298 CORE_ADDR *, CORE_ADDR *,
1299 struct dwarf2_cu *,
1300 dwarf2_psymtab *);
1301
1302 static void get_scope_pc_bounds (struct die_info *,
1303 CORE_ADDR *, CORE_ADDR *,
1304 struct dwarf2_cu *);
1305
1306 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1307 CORE_ADDR, struct dwarf2_cu *);
1308
1309 static void dwarf2_add_field (struct field_info *, struct die_info *,
1310 struct dwarf2_cu *);
1311
1312 static void dwarf2_attach_fields_to_type (struct field_info *,
1313 struct type *, struct dwarf2_cu *);
1314
1315 static void dwarf2_add_member_fn (struct field_info *,
1316 struct die_info *, struct type *,
1317 struct dwarf2_cu *);
1318
1319 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1320 struct type *,
1321 struct dwarf2_cu *);
1322
1323 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1324
1325 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1326
1327 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1328
1329 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1330
1331 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1332
1333 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1334
1335 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1336
1337 static struct type *read_module_type (struct die_info *die,
1338 struct dwarf2_cu *cu);
1339
1340 static const char *namespace_name (struct die_info *die,
1341 int *is_anonymous, struct dwarf2_cu *);
1342
1343 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1344
1345 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1346 bool * = nullptr);
1347
1348 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1349 struct dwarf2_cu *);
1350
1351 static struct die_info *read_die_and_siblings_1
1352 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1353 struct die_info *);
1354
1355 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1356 const gdb_byte *info_ptr,
1357 const gdb_byte **new_info_ptr,
1358 struct die_info *parent);
1359
1360 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1361 struct die_info **, const gdb_byte *,
1362 int);
1363
1364 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1365 struct die_info **, const gdb_byte *);
1366
1367 static void process_die (struct die_info *, struct dwarf2_cu *);
1368
1369 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1370 struct objfile *);
1371
1372 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1373
1374 static const char *dwarf2_full_name (const char *name,
1375 struct die_info *die,
1376 struct dwarf2_cu *cu);
1377
1378 static const char *dwarf2_physname (const char *name, struct die_info *die,
1379 struct dwarf2_cu *cu);
1380
1381 static struct die_info *dwarf2_extension (struct die_info *die,
1382 struct dwarf2_cu **);
1383
1384 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1385
1386 static void dump_die_for_error (struct die_info *);
1387
1388 static void dump_die_1 (struct ui_file *, int level, int max_level,
1389 struct die_info *);
1390
1391 /*static*/ void dump_die (struct die_info *, int max_level);
1392
1393 static void store_in_ref_table (struct die_info *,
1394 struct dwarf2_cu *);
1395
1396 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1397 const struct attribute *,
1398 struct dwarf2_cu **);
1399
1400 static struct die_info *follow_die_ref (struct die_info *,
1401 const struct attribute *,
1402 struct dwarf2_cu **);
1403
1404 static struct die_info *follow_die_sig (struct die_info *,
1405 const struct attribute *,
1406 struct dwarf2_cu **);
1407
1408 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1409 struct dwarf2_cu *);
1410
1411 static struct type *get_DW_AT_signature_type (struct die_info *,
1412 const struct attribute *,
1413 struct dwarf2_cu *);
1414
1415 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1416 dwarf2_per_objfile *per_objfile);
1417
1418 static void read_signatured_type (signatured_type *sig_type,
1419 dwarf2_per_objfile *per_objfile);
1420
1421 static int attr_to_dynamic_prop (const struct attribute *attr,
1422 struct die_info *die, struct dwarf2_cu *cu,
1423 struct dynamic_prop *prop, struct type *type);
1424
1425 /* memory allocation interface */
1426
1427 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1428
1429 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1430
1431 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1432
1433 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1434 struct dwarf2_loclist_baton *baton,
1435 const struct attribute *attr);
1436
1437 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1438 struct symbol *sym,
1439 struct dwarf2_cu *cu,
1440 int is_block);
1441
1442 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1443 const gdb_byte *info_ptr,
1444 const struct abbrev_info *abbrev);
1445
1446 static hashval_t partial_die_hash (const void *item);
1447
1448 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1449
1450 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1451 (sect_offset sect_off, unsigned int offset_in_dwz,
1452 dwarf2_per_bfd *per_bfd);
1453
1454 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1455 struct die_info *comp_unit_die,
1456 enum language pretend_language);
1457
1458 static struct type *set_die_type (struct die_info *, struct type *,
1459 struct dwarf2_cu *, bool = false);
1460
1461 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1462
1463 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1464 dwarf2_per_objfile *per_objfile,
1465 dwarf2_cu *existing_cu,
1466 bool skip_partial,
1467 enum language pretend_language);
1468
1469 static void process_full_comp_unit (dwarf2_cu *cu,
1470 enum language pretend_language);
1471
1472 static void process_full_type_unit (dwarf2_cu *cu,
1473 enum language pretend_language);
1474
1475 static struct type *get_die_type_at_offset (sect_offset,
1476 dwarf2_per_cu_data *per_cu,
1477 dwarf2_per_objfile *per_objfile);
1478
1479 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1480
1481 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1482 dwarf2_per_objfile *per_objfile,
1483 enum language pretend_language);
1484
1485 static void process_queue (dwarf2_per_objfile *per_objfile);
1486
1487 /* Class, the destructor of which frees all allocated queue entries. This
1488 will only have work to do if an error was thrown while processing the
1489 dwarf. If no error was thrown then the queue entries should have all
1490 been processed, and freed, as we went along. */
1491
1492 class dwarf2_queue_guard
1493 {
1494 public:
1495 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1496 : m_per_objfile (per_objfile)
1497 {
1498 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1499
1500 m_per_objfile->per_bfd->queue.emplace ();
1501 }
1502
1503 /* Free any entries remaining on the queue. There should only be
1504 entries left if we hit an error while processing the dwarf. */
1505 ~dwarf2_queue_guard ()
1506 {
1507 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1508
1509 m_per_objfile->per_bfd->queue.reset ();
1510 }
1511
1512 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1513
1514 private:
1515 dwarf2_per_objfile *m_per_objfile;
1516 };
1517
1518 dwarf2_queue_item::~dwarf2_queue_item ()
1519 {
1520 /* Anything still marked queued is likely to be in an
1521 inconsistent state, so discard it. */
1522 if (per_cu->queued)
1523 {
1524 per_objfile->remove_cu (per_cu);
1525 per_cu->queued = 0;
1526 }
1527 }
1528
1529 /* See dwarf2/read.h. */
1530
1531 void
1532 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1533 {
1534 if (data->is_debug_types)
1535 delete static_cast<signatured_type *> (data);
1536 else
1537 delete data;
1538 }
1539
1540 static file_and_directory &find_file_and_directory
1541 (struct die_info *die, struct dwarf2_cu *cu);
1542
1543 static const char *compute_include_file_name
1544 (const struct line_header *lh,
1545 const file_entry &fe,
1546 const file_and_directory &cu_info,
1547 gdb::unique_xmalloc_ptr<char> *name_holder);
1548
1549 static htab_up allocate_signatured_type_table ();
1550
1551 static htab_up allocate_dwo_unit_table ();
1552
1553 static struct dwo_unit *lookup_dwo_unit_in_dwp
1554 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1555 const char *comp_dir, ULONGEST signature, int is_debug_types);
1556
1557 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1558
1559 static struct dwo_unit *lookup_dwo_comp_unit
1560 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1561 ULONGEST signature);
1562
1563 static struct dwo_unit *lookup_dwo_type_unit
1564 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1565
1566 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1567
1568 /* A unique pointer to a dwo_file. */
1569
1570 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1571
1572 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1573
1574 static void check_producer (struct dwarf2_cu *cu);
1575 \f
1576 /* Various complaints about symbol reading that don't abort the process. */
1577
1578 static void
1579 dwarf2_debug_line_missing_file_complaint (void)
1580 {
1581 complaint (_(".debug_line section has line data without a file"));
1582 }
1583
1584 static void
1585 dwarf2_debug_line_missing_end_sequence_complaint (void)
1586 {
1587 complaint (_(".debug_line section has line "
1588 "program sequence without an end"));
1589 }
1590
1591 static void
1592 dwarf2_complex_location_expr_complaint (void)
1593 {
1594 complaint (_("location expression too complex"));
1595 }
1596
1597 static void
1598 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1599 int arg3)
1600 {
1601 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1602 arg1, arg2, arg3);
1603 }
1604
1605 static void
1606 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1607 {
1608 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1609 arg1, arg2);
1610 }
1611
1612 /* Hash function for line_header_hash. */
1613
1614 static hashval_t
1615 line_header_hash (const struct line_header *ofs)
1616 {
1617 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1618 }
1619
1620 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1621
1622 static hashval_t
1623 line_header_hash_voidp (const void *item)
1624 {
1625 const struct line_header *ofs = (const struct line_header *) item;
1626
1627 return line_header_hash (ofs);
1628 }
1629
1630 /* Equality function for line_header_hash. */
1631
1632 static int
1633 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1634 {
1635 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1636 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1637
1638 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1639 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1640 }
1641
1642 \f
1643
1644 /* An iterator for all_comp_units that is based on index. This
1645 approach makes it possible to iterate over all_comp_units safely,
1646 when some caller in the loop may add new units. */
1647
1648 class all_comp_units_iterator
1649 {
1650 public:
1651
1652 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1653 : m_per_bfd (per_bfd),
1654 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1655 {
1656 }
1657
1658 all_comp_units_iterator &operator++ ()
1659 {
1660 ++m_index;
1661 return *this;
1662 }
1663
1664 dwarf2_per_cu_data *operator* () const
1665 {
1666 return m_per_bfd->get_cu (m_index);
1667 }
1668
1669 bool operator== (const all_comp_units_iterator &other) const
1670 {
1671 return m_index == other.m_index;
1672 }
1673
1674
1675 bool operator!= (const all_comp_units_iterator &other) const
1676 {
1677 return m_index != other.m_index;
1678 }
1679
1680 private:
1681
1682 dwarf2_per_bfd *m_per_bfd;
1683 size_t m_index;
1684 };
1685
1686 /* A range adapter for the all_comp_units_iterator. */
1687 class all_comp_units_range
1688 {
1689 public:
1690
1691 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1692 : m_per_bfd (per_bfd)
1693 {
1694 }
1695
1696 all_comp_units_iterator begin ()
1697 {
1698 return all_comp_units_iterator (m_per_bfd, true);
1699 }
1700
1701 all_comp_units_iterator end ()
1702 {
1703 return all_comp_units_iterator (m_per_bfd, false);
1704 }
1705
1706 private:
1707
1708 dwarf2_per_bfd *m_per_bfd;
1709 };
1710
1711 /* See declaration. */
1712
1713 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1714 bool can_copy_)
1715 : obfd (obfd),
1716 can_copy (can_copy_)
1717 {
1718 if (names == NULL)
1719 names = &dwarf2_elf_names;
1720
1721 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1722 locate_sections (obfd, sec, *names);
1723 }
1724
1725 dwarf2_per_bfd::~dwarf2_per_bfd ()
1726 {
1727 for (auto &per_cu : all_comp_units)
1728 {
1729 per_cu->imported_symtabs_free ();
1730 per_cu->free_cached_file_names ();
1731 }
1732
1733 /* Everything else should be on this->obstack. */
1734 }
1735
1736 /* See read.h. */
1737
1738 void
1739 dwarf2_per_objfile::remove_all_cus ()
1740 {
1741 gdb_assert (!this->per_bfd->queue.has_value ());
1742
1743 for (auto pair : m_dwarf2_cus)
1744 delete pair.second;
1745
1746 m_dwarf2_cus.clear ();
1747 }
1748
1749 /* A helper class that calls free_cached_comp_units on
1750 destruction. */
1751
1752 class free_cached_comp_units
1753 {
1754 public:
1755
1756 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1757 : m_per_objfile (per_objfile)
1758 {
1759 }
1760
1761 ~free_cached_comp_units ()
1762 {
1763 m_per_objfile->remove_all_cus ();
1764 }
1765
1766 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1767
1768 private:
1769
1770 dwarf2_per_objfile *m_per_objfile;
1771 };
1772
1773 /* See read.h. */
1774
1775 bool
1776 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1777 {
1778 if (per_cu->index < this->m_symtabs.size ())
1779 return this->m_symtabs[per_cu->index] != nullptr;
1780 return false;
1781 }
1782
1783 /* See read.h. */
1784
1785 compunit_symtab *
1786 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1787 {
1788 if (per_cu->index < this->m_symtabs.size ())
1789 return this->m_symtabs[per_cu->index];
1790 return nullptr;
1791 }
1792
1793 /* See read.h. */
1794
1795 void
1796 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1797 compunit_symtab *symtab)
1798 {
1799 if (per_cu->index >= this->m_symtabs.size ())
1800 this->m_symtabs.resize (per_cu->index + 1);
1801 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1802 this->m_symtabs[per_cu->index] = symtab;
1803 }
1804
1805 /* Try to locate the sections we need for DWARF 2 debugging
1806 information and return true if we have enough to do something.
1807 NAMES points to the dwarf2 section names, or is NULL if the standard
1808 ELF names are used. CAN_COPY is true for formats where symbol
1809 interposition is possible and so symbol values must follow copy
1810 relocation rules. */
1811
1812 int
1813 dwarf2_has_info (struct objfile *objfile,
1814 const struct dwarf2_debug_sections *names,
1815 bool can_copy)
1816 {
1817 if (objfile->flags & OBJF_READNEVER)
1818 return 0;
1819
1820 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1821
1822 if (per_objfile == NULL)
1823 {
1824 dwarf2_per_bfd *per_bfd;
1825
1826 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1827 BFD doesn't require relocations.
1828
1829 We don't share with objfiles for which -readnow was requested,
1830 because it would complicate things when loading the same BFD with
1831 -readnow and then without -readnow. */
1832 if (!gdb_bfd_requires_relocations (objfile->obfd)
1833 && (objfile->flags & OBJF_READNOW) == 0)
1834 {
1835 /* See if one has been created for this BFD yet. */
1836 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1837
1838 if (per_bfd == nullptr)
1839 {
1840 /* No, create it now. */
1841 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1842 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1843 }
1844 }
1845 else
1846 {
1847 /* No sharing possible, create one specifically for this objfile. */
1848 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1849 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1850 }
1851
1852 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1853 }
1854
1855 return (!per_objfile->per_bfd->info.is_virtual
1856 && per_objfile->per_bfd->info.s.section != NULL
1857 && !per_objfile->per_bfd->abbrev.is_virtual
1858 && per_objfile->per_bfd->abbrev.s.section != NULL);
1859 }
1860
1861 /* See declaration. */
1862
1863 void
1864 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1865 const dwarf2_debug_sections &names)
1866 {
1867 flagword aflag = bfd_section_flags (sectp);
1868
1869 if ((aflag & SEC_HAS_CONTENTS) == 0)
1870 {
1871 }
1872 else if (elf_section_data (sectp)->this_hdr.sh_size
1873 > bfd_get_file_size (abfd))
1874 {
1875 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1876 warning (_("Discarding section %s which has a section size (%s"
1877 ") larger than the file size [in module %s]"),
1878 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1879 bfd_get_filename (abfd));
1880 }
1881 else if (names.info.matches (sectp->name))
1882 {
1883 this->info.s.section = sectp;
1884 this->info.size = bfd_section_size (sectp);
1885 }
1886 else if (names.abbrev.matches (sectp->name))
1887 {
1888 this->abbrev.s.section = sectp;
1889 this->abbrev.size = bfd_section_size (sectp);
1890 }
1891 else if (names.line.matches (sectp->name))
1892 {
1893 this->line.s.section = sectp;
1894 this->line.size = bfd_section_size (sectp);
1895 }
1896 else if (names.loc.matches (sectp->name))
1897 {
1898 this->loc.s.section = sectp;
1899 this->loc.size = bfd_section_size (sectp);
1900 }
1901 else if (names.loclists.matches (sectp->name))
1902 {
1903 this->loclists.s.section = sectp;
1904 this->loclists.size = bfd_section_size (sectp);
1905 }
1906 else if (names.macinfo.matches (sectp->name))
1907 {
1908 this->macinfo.s.section = sectp;
1909 this->macinfo.size = bfd_section_size (sectp);
1910 }
1911 else if (names.macro.matches (sectp->name))
1912 {
1913 this->macro.s.section = sectp;
1914 this->macro.size = bfd_section_size (sectp);
1915 }
1916 else if (names.str.matches (sectp->name))
1917 {
1918 this->str.s.section = sectp;
1919 this->str.size = bfd_section_size (sectp);
1920 }
1921 else if (names.str_offsets.matches (sectp->name))
1922 {
1923 this->str_offsets.s.section = sectp;
1924 this->str_offsets.size = bfd_section_size (sectp);
1925 }
1926 else if (names.line_str.matches (sectp->name))
1927 {
1928 this->line_str.s.section = sectp;
1929 this->line_str.size = bfd_section_size (sectp);
1930 }
1931 else if (names.addr.matches (sectp->name))
1932 {
1933 this->addr.s.section = sectp;
1934 this->addr.size = bfd_section_size (sectp);
1935 }
1936 else if (names.frame.matches (sectp->name))
1937 {
1938 this->frame.s.section = sectp;
1939 this->frame.size = bfd_section_size (sectp);
1940 }
1941 else if (names.eh_frame.matches (sectp->name))
1942 {
1943 this->eh_frame.s.section = sectp;
1944 this->eh_frame.size = bfd_section_size (sectp);
1945 }
1946 else if (names.ranges.matches (sectp->name))
1947 {
1948 this->ranges.s.section = sectp;
1949 this->ranges.size = bfd_section_size (sectp);
1950 }
1951 else if (names.rnglists.matches (sectp->name))
1952 {
1953 this->rnglists.s.section = sectp;
1954 this->rnglists.size = bfd_section_size (sectp);
1955 }
1956 else if (names.types.matches (sectp->name))
1957 {
1958 struct dwarf2_section_info type_section;
1959
1960 memset (&type_section, 0, sizeof (type_section));
1961 type_section.s.section = sectp;
1962 type_section.size = bfd_section_size (sectp);
1963
1964 this->types.push_back (type_section);
1965 }
1966 else if (names.gdb_index.matches (sectp->name))
1967 {
1968 this->gdb_index.s.section = sectp;
1969 this->gdb_index.size = bfd_section_size (sectp);
1970 }
1971 else if (names.debug_names.matches (sectp->name))
1972 {
1973 this->debug_names.s.section = sectp;
1974 this->debug_names.size = bfd_section_size (sectp);
1975 }
1976 else if (names.debug_aranges.matches (sectp->name))
1977 {
1978 this->debug_aranges.s.section = sectp;
1979 this->debug_aranges.size = bfd_section_size (sectp);
1980 }
1981
1982 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1983 && bfd_section_vma (sectp) == 0)
1984 this->has_section_at_zero = true;
1985 }
1986
1987 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1988 SECTION_NAME. */
1989
1990 void
1991 dwarf2_get_section_info (struct objfile *objfile,
1992 enum dwarf2_section_enum sect,
1993 asection **sectp, const gdb_byte **bufp,
1994 bfd_size_type *sizep)
1995 {
1996 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1997 struct dwarf2_section_info *info;
1998
1999 /* We may see an objfile without any DWARF, in which case we just
2000 return nothing. */
2001 if (per_objfile == NULL)
2002 {
2003 *sectp = NULL;
2004 *bufp = NULL;
2005 *sizep = 0;
2006 return;
2007 }
2008 switch (sect)
2009 {
2010 case DWARF2_DEBUG_FRAME:
2011 info = &per_objfile->per_bfd->frame;
2012 break;
2013 case DWARF2_EH_FRAME:
2014 info = &per_objfile->per_bfd->eh_frame;
2015 break;
2016 default:
2017 gdb_assert_not_reached ("unexpected section");
2018 }
2019
2020 info->read (objfile);
2021
2022 *sectp = info->get_bfd_section ();
2023 *bufp = info->buffer;
2024 *sizep = info->size;
2025 }
2026
2027 \f
2028 /* DWARF quick_symbol_functions support. */
2029
2030 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2031 unique line tables, so we maintain a separate table of all .debug_line
2032 derived entries to support the sharing.
2033 All the quick functions need is the list of file names. We discard the
2034 line_header when we're done and don't need to record it here. */
2035 struct quick_file_names
2036 {
2037 /* The data used to construct the hash key. */
2038 struct stmt_list_hash hash;
2039
2040 /* The number of entries in file_names, real_names. */
2041 unsigned int num_file_names;
2042
2043 /* The CU directory, as given by DW_AT_comp_dir. May be
2044 nullptr. */
2045 const char *comp_dir;
2046
2047 /* The file names from the line table, after being run through
2048 file_full_name. */
2049 const char **file_names;
2050
2051 /* The file names from the line table after being run through
2052 gdb_realpath. These are computed lazily. */
2053 const char **real_names;
2054 };
2055
2056 /* When using the index (and thus not using psymtabs), each CU has an
2057 object of this type. This is used to hold information needed by
2058 the various "quick" methods. */
2059 struct dwarf2_per_cu_quick_data
2060 {
2061 /* The file table. This can be NULL if there was no file table
2062 or it's currently not read in.
2063 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2064 struct quick_file_names *file_names;
2065
2066 /* A temporary mark bit used when iterating over all CUs in
2067 expand_symtabs_matching. */
2068 unsigned int mark : 1;
2069
2070 /* True if we've tried to read the file table. There will be no
2071 point in trying to read it again next time. */
2072 bool files_read : 1;
2073 };
2074
2075 /* A subclass of psymbol_functions that arranges to read the DWARF
2076 partial symbols when needed. */
2077 struct lazy_dwarf_reader : public psymbol_functions
2078 {
2079 using psymbol_functions::psymbol_functions;
2080
2081 bool can_lazily_read_symbols () override
2082 {
2083 return true;
2084 }
2085
2086 void read_partial_symbols (struct objfile *objfile) override
2087 {
2088 if (dwarf2_has_info (objfile, nullptr))
2089 dwarf2_build_psymtabs (objfile, this);
2090 }
2091 };
2092
2093 static quick_symbol_functions_up
2094 make_lazy_dwarf_reader ()
2095 {
2096 return quick_symbol_functions_up (new lazy_dwarf_reader);
2097 }
2098
2099 struct dwarf2_base_index_functions : public quick_symbol_functions
2100 {
2101 bool has_symbols (struct objfile *objfile) override;
2102
2103 bool has_unexpanded_symtabs (struct objfile *objfile) override;
2104
2105 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2106
2107 void forget_cached_source_info (struct objfile *objfile) override;
2108
2109 enum language lookup_global_symbol_language (struct objfile *objfile,
2110 const char *name,
2111 domain_enum domain,
2112 bool *symbol_found_p) override
2113 {
2114 *symbol_found_p = false;
2115 return language_unknown;
2116 }
2117
2118 void print_stats (struct objfile *objfile, bool print_bcache) override;
2119
2120 void expand_all_symtabs (struct objfile *objfile) override;
2121
2122 struct compunit_symtab *find_pc_sect_compunit_symtab
2123 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2124 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2125
2126 struct compunit_symtab *find_compunit_symtab_by_address
2127 (struct objfile *objfile, CORE_ADDR address) override
2128 {
2129 return nullptr;
2130 }
2131
2132 void map_symbol_filenames (struct objfile *objfile,
2133 gdb::function_view<symbol_filename_ftype> fun,
2134 bool need_fullname) override;
2135 };
2136
2137 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2138 {
2139 void dump (struct objfile *objfile) override;
2140
2141 void expand_matching_symbols
2142 (struct objfile *,
2143 const lookup_name_info &lookup_name,
2144 domain_enum domain,
2145 int global,
2146 symbol_compare_ftype *ordered_compare) override;
2147
2148 bool expand_symtabs_matching
2149 (struct objfile *objfile,
2150 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2151 const lookup_name_info *lookup_name,
2152 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2153 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2154 block_search_flags search_flags,
2155 domain_enum domain,
2156 enum search_domain kind) override;
2157 };
2158
2159 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2160 {
2161 void dump (struct objfile *objfile) override;
2162
2163 void expand_matching_symbols
2164 (struct objfile *,
2165 const lookup_name_info &lookup_name,
2166 domain_enum domain,
2167 int global,
2168 symbol_compare_ftype *ordered_compare) override;
2169
2170 bool expand_symtabs_matching
2171 (struct objfile *objfile,
2172 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2173 const lookup_name_info *lookup_name,
2174 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2176 block_search_flags search_flags,
2177 domain_enum domain,
2178 enum search_domain kind) override;
2179 };
2180
2181 static quick_symbol_functions_up
2182 make_dwarf_gdb_index ()
2183 {
2184 return quick_symbol_functions_up (new dwarf2_gdb_index);
2185 }
2186
2187 static quick_symbol_functions_up
2188 make_dwarf_debug_names ()
2189 {
2190 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2191 }
2192
2193 /* Utility hash function for a stmt_list_hash. */
2194
2195 static hashval_t
2196 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2197 {
2198 hashval_t v = 0;
2199
2200 if (stmt_list_hash->dwo_unit != NULL)
2201 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2202 v += to_underlying (stmt_list_hash->line_sect_off);
2203 return v;
2204 }
2205
2206 /* Utility equality function for a stmt_list_hash. */
2207
2208 static int
2209 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2210 const struct stmt_list_hash *rhs)
2211 {
2212 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2213 return 0;
2214 if (lhs->dwo_unit != NULL
2215 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2216 return 0;
2217
2218 return lhs->line_sect_off == rhs->line_sect_off;
2219 }
2220
2221 /* Hash function for a quick_file_names. */
2222
2223 static hashval_t
2224 hash_file_name_entry (const void *e)
2225 {
2226 const struct quick_file_names *file_data
2227 = (const struct quick_file_names *) e;
2228
2229 return hash_stmt_list_entry (&file_data->hash);
2230 }
2231
2232 /* Equality function for a quick_file_names. */
2233
2234 static int
2235 eq_file_name_entry (const void *a, const void *b)
2236 {
2237 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2238 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2239
2240 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2241 }
2242
2243 /* Create a quick_file_names hash table. */
2244
2245 static htab_up
2246 create_quick_file_names_table (unsigned int nr_initial_entries)
2247 {
2248 return htab_up (htab_create_alloc (nr_initial_entries,
2249 hash_file_name_entry, eq_file_name_entry,
2250 nullptr, xcalloc, xfree));
2251 }
2252
2253 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2254 function is unrelated to symtabs, symtab would have to be created afterwards.
2255 You should call age_cached_comp_units after processing the CU. */
2256
2257 static dwarf2_cu *
2258 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2259 bool skip_partial)
2260 {
2261 if (per_cu->is_debug_types)
2262 load_full_type_unit (per_cu, per_objfile);
2263 else
2264 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2265 skip_partial, language_minimal);
2266
2267 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2268 if (cu == nullptr)
2269 return nullptr; /* Dummy CU. */
2270
2271 dwarf2_find_base_address (cu->dies, cu);
2272
2273 return cu;
2274 }
2275
2276 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2277
2278 static void
2279 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2280 dwarf2_per_objfile *per_objfile, bool skip_partial)
2281 {
2282 /* Skip type_unit_groups, reading the type units they contain
2283 is handled elsewhere. */
2284 if (per_cu->type_unit_group_p ())
2285 return;
2286
2287 {
2288 /* The destructor of dwarf2_queue_guard frees any entries left on
2289 the queue. After this point we're guaranteed to leave this function
2290 with the dwarf queue empty. */
2291 dwarf2_queue_guard q_guard (per_objfile);
2292
2293 if (!per_objfile->symtab_set_p (per_cu))
2294 {
2295 queue_comp_unit (per_cu, per_objfile, language_minimal);
2296 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2297
2298 /* If we just loaded a CU from a DWO, and we're working with an index
2299 that may badly handle TUs, load all the TUs in that DWO as well.
2300 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2301 if (!per_cu->is_debug_types
2302 && cu != NULL
2303 && cu->dwo_unit != NULL
2304 && per_objfile->per_bfd->index_table != NULL
2305 && per_objfile->per_bfd->index_table->version <= 7
2306 /* DWP files aren't supported yet. */
2307 && get_dwp_file (per_objfile) == NULL)
2308 queue_and_load_all_dwo_tus (cu);
2309 }
2310
2311 process_queue (per_objfile);
2312 }
2313
2314 /* Age the cache, releasing compilation units that have not
2315 been used recently. */
2316 per_objfile->age_comp_units ();
2317 }
2318
2319 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2320 the per-objfile for which this symtab is instantiated.
2321
2322 Returns the resulting symbol table. */
2323
2324 static struct compunit_symtab *
2325 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2326 dwarf2_per_objfile *per_objfile,
2327 bool skip_partial)
2328 {
2329 gdb_assert (per_objfile->per_bfd->using_index);
2330
2331 if (!per_objfile->symtab_set_p (per_cu))
2332 {
2333 free_cached_comp_units freer (per_objfile);
2334 scoped_restore decrementer = increment_reading_symtab ();
2335 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2336 process_cu_includes (per_objfile);
2337 }
2338
2339 return per_objfile->get_symtab (per_cu);
2340 }
2341
2342 /* See read.h. */
2343
2344 dwarf2_per_cu_data_up
2345 dwarf2_per_bfd::allocate_per_cu ()
2346 {
2347 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2348 result->per_bfd = this;
2349 result->index = all_comp_units.size ();
2350 return result;
2351 }
2352
2353 /* See read.h. */
2354
2355 signatured_type_up
2356 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2357 {
2358 signatured_type_up result (new signatured_type (signature));
2359 result->per_bfd = this;
2360 result->index = all_comp_units.size ();
2361 result->is_debug_types = true;
2362 tu_stats.nr_tus++;
2363 return result;
2364 }
2365
2366 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2367 obstack, and constructed with the specified field values. */
2368
2369 static dwarf2_per_cu_data_up
2370 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2371 struct dwarf2_section_info *section,
2372 int is_dwz,
2373 sect_offset sect_off, ULONGEST length)
2374 {
2375 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2376 the_cu->sect_off = sect_off;
2377 the_cu->length = length;
2378 the_cu->section = section;
2379 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2380 struct dwarf2_per_cu_quick_data);
2381 the_cu->is_dwz = is_dwz;
2382 return the_cu;
2383 }
2384
2385 /* A helper for create_cus_from_index that handles a given list of
2386 CUs. */
2387
2388 static void
2389 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2390 const gdb_byte *cu_list, offset_type n_elements,
2391 struct dwarf2_section_info *section,
2392 int is_dwz)
2393 {
2394 for (offset_type i = 0; i < n_elements; i += 2)
2395 {
2396 gdb_static_assert (sizeof (ULONGEST) >= 8);
2397
2398 sect_offset sect_off
2399 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2400 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2401 cu_list += 2 * 8;
2402
2403 dwarf2_per_cu_data_up per_cu
2404 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2405 length);
2406 per_bfd->all_comp_units.push_back (std::move (per_cu));
2407 }
2408 }
2409
2410 /* Read the CU list from the mapped index, and use it to create all
2411 the CU objects for PER_BFD. */
2412
2413 static void
2414 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2415 const gdb_byte *cu_list, offset_type cu_list_elements,
2416 const gdb_byte *dwz_list, offset_type dwz_elements)
2417 {
2418 gdb_assert (per_bfd->all_comp_units.empty ());
2419 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2420
2421 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2422 &per_bfd->info, 0);
2423
2424 if (dwz_elements == 0)
2425 return;
2426
2427 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2428 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2429 &dwz->info, 1);
2430 }
2431
2432 /* Create the signatured type hash table from the index. */
2433
2434 static void
2435 create_signatured_type_table_from_index
2436 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2437 const gdb_byte *bytes, offset_type elements)
2438 {
2439 htab_up sig_types_hash = allocate_signatured_type_table ();
2440
2441 for (offset_type i = 0; i < elements; i += 3)
2442 {
2443 signatured_type_up sig_type;
2444 ULONGEST signature;
2445 void **slot;
2446 cu_offset type_offset_in_tu;
2447
2448 gdb_static_assert (sizeof (ULONGEST) >= 8);
2449 sect_offset sect_off
2450 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2451 type_offset_in_tu
2452 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2453 BFD_ENDIAN_LITTLE);
2454 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2455 bytes += 3 * 8;
2456
2457 sig_type = per_bfd->allocate_signatured_type (signature);
2458 sig_type->type_offset_in_tu = type_offset_in_tu;
2459 sig_type->section = section;
2460 sig_type->sect_off = sect_off;
2461 sig_type->v.quick
2462 = OBSTACK_ZALLOC (&per_bfd->obstack,
2463 struct dwarf2_per_cu_quick_data);
2464
2465 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2466 *slot = sig_type.get ();
2467
2468 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2469 }
2470
2471 per_bfd->signatured_types = std::move (sig_types_hash);
2472 }
2473
2474 /* Create the signatured type hash table from .debug_names. */
2475
2476 static void
2477 create_signatured_type_table_from_debug_names
2478 (dwarf2_per_objfile *per_objfile,
2479 const mapped_debug_names &map,
2480 struct dwarf2_section_info *section,
2481 struct dwarf2_section_info *abbrev_section)
2482 {
2483 struct objfile *objfile = per_objfile->objfile;
2484
2485 section->read (objfile);
2486 abbrev_section->read (objfile);
2487
2488 htab_up sig_types_hash = allocate_signatured_type_table ();
2489
2490 for (uint32_t i = 0; i < map.tu_count; ++i)
2491 {
2492 signatured_type_up sig_type;
2493 void **slot;
2494
2495 sect_offset sect_off
2496 = (sect_offset) (extract_unsigned_integer
2497 (map.tu_table_reordered + i * map.offset_size,
2498 map.offset_size,
2499 map.dwarf5_byte_order));
2500
2501 comp_unit_head cu_header;
2502 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2503 abbrev_section,
2504 section->buffer + to_underlying (sect_off),
2505 rcuh_kind::TYPE);
2506
2507 sig_type = per_objfile->per_bfd->allocate_signatured_type
2508 (cu_header.signature);
2509 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2510 sig_type->section = section;
2511 sig_type->sect_off = sect_off;
2512 sig_type->v.quick
2513 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2514 struct dwarf2_per_cu_quick_data);
2515
2516 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2517 *slot = sig_type.get ();
2518
2519 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2520 }
2521
2522 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2523 }
2524
2525 /* Read the address map data from the mapped index, and use it to
2526 populate the psymtabs_addrmap. */
2527
2528 static void
2529 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2530 struct mapped_index *index)
2531 {
2532 struct objfile *objfile = per_objfile->objfile;
2533 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2534 struct gdbarch *gdbarch = objfile->arch ();
2535 const gdb_byte *iter, *end;
2536 struct addrmap *mutable_map;
2537 CORE_ADDR baseaddr;
2538
2539 auto_obstack temp_obstack;
2540
2541 mutable_map = addrmap_create_mutable (&temp_obstack);
2542
2543 iter = index->address_table.data ();
2544 end = iter + index->address_table.size ();
2545
2546 baseaddr = objfile->text_section_offset ();
2547
2548 while (iter < end)
2549 {
2550 ULONGEST hi, lo, cu_index;
2551 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2552 iter += 8;
2553 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2554 iter += 8;
2555 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2556 iter += 4;
2557
2558 if (lo > hi)
2559 {
2560 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2561 hex_string (lo), hex_string (hi));
2562 continue;
2563 }
2564
2565 if (cu_index >= per_bfd->all_comp_units.size ())
2566 {
2567 complaint (_(".gdb_index address table has invalid CU number %u"),
2568 (unsigned) cu_index);
2569 continue;
2570 }
2571
2572 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2573 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2574 addrmap_set_empty (mutable_map, lo, hi - 1,
2575 per_bfd->get_cu (cu_index));
2576 }
2577
2578 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2579 &per_bfd->obstack);
2580 }
2581
2582 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2583 populate the psymtabs_addrmap. */
2584
2585 static void
2586 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2587 struct dwarf2_section_info *section)
2588 {
2589 struct objfile *objfile = per_objfile->objfile;
2590 bfd *abfd = objfile->obfd;
2591 struct gdbarch *gdbarch = objfile->arch ();
2592 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2593 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2594
2595 auto_obstack temp_obstack;
2596 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2597
2598 std::unordered_map<sect_offset,
2599 dwarf2_per_cu_data *,
2600 gdb::hash_enum<sect_offset>>
2601 debug_info_offset_to_per_cu;
2602 for (const auto &per_cu : per_bfd->all_comp_units)
2603 {
2604 /* A TU will not need aranges, and skipping them here is an easy
2605 way of ignoring .debug_types -- and possibly seeing a
2606 duplicate section offset -- entirely. */
2607 if (per_cu->is_debug_types)
2608 continue;
2609
2610 const auto insertpair
2611 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2612 per_cu.get ());
2613 if (!insertpair.second)
2614 {
2615 warning (_("Section .debug_aranges in %s has duplicate "
2616 "debug_info_offset %s, ignoring .debug_aranges."),
2617 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2618 return;
2619 }
2620 }
2621
2622 section->read (objfile);
2623
2624 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2625
2626 const gdb_byte *addr = section->buffer;
2627
2628 while (addr < section->buffer + section->size)
2629 {
2630 const gdb_byte *const entry_addr = addr;
2631 unsigned int bytes_read;
2632
2633 const LONGEST entry_length = read_initial_length (abfd, addr,
2634 &bytes_read);
2635 addr += bytes_read;
2636
2637 const gdb_byte *const entry_end = addr + entry_length;
2638 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2639 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2640 if (addr + entry_length > section->buffer + section->size)
2641 {
2642 warning (_("Section .debug_aranges in %s entry at offset %s "
2643 "length %s exceeds section length %s, "
2644 "ignoring .debug_aranges."),
2645 objfile_name (objfile),
2646 plongest (entry_addr - section->buffer),
2647 plongest (bytes_read + entry_length),
2648 pulongest (section->size));
2649 return;
2650 }
2651
2652 /* The version number. */
2653 const uint16_t version = read_2_bytes (abfd, addr);
2654 addr += 2;
2655 if (version != 2)
2656 {
2657 warning (_("Section .debug_aranges in %s entry at offset %s "
2658 "has unsupported version %d, ignoring .debug_aranges."),
2659 objfile_name (objfile),
2660 plongest (entry_addr - section->buffer), version);
2661 return;
2662 }
2663
2664 const uint64_t debug_info_offset
2665 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2666 addr += offset_size;
2667 const auto per_cu_it
2668 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2669 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2670 {
2671 warning (_("Section .debug_aranges in %s entry at offset %s "
2672 "debug_info_offset %s does not exists, "
2673 "ignoring .debug_aranges."),
2674 objfile_name (objfile),
2675 plongest (entry_addr - section->buffer),
2676 pulongest (debug_info_offset));
2677 return;
2678 }
2679 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2680
2681 const uint8_t address_size = *addr++;
2682 if (address_size < 1 || address_size > 8)
2683 {
2684 warning (_("Section .debug_aranges in %s entry at offset %s "
2685 "address_size %u is invalid, ignoring .debug_aranges."),
2686 objfile_name (objfile),
2687 plongest (entry_addr - section->buffer), address_size);
2688 return;
2689 }
2690
2691 const uint8_t segment_selector_size = *addr++;
2692 if (segment_selector_size != 0)
2693 {
2694 warning (_("Section .debug_aranges in %s entry at offset %s "
2695 "segment_selector_size %u is not supported, "
2696 "ignoring .debug_aranges."),
2697 objfile_name (objfile),
2698 plongest (entry_addr - section->buffer),
2699 segment_selector_size);
2700 return;
2701 }
2702
2703 /* Must pad to an alignment boundary that is twice the address
2704 size. It is undocumented by the DWARF standard but GCC does
2705 use it. However, not every compiler does this. We can see
2706 whether it has happened by looking at the total length of the
2707 contents of the aranges for this CU -- it if isn't a multiple
2708 of twice the address size, then we skip any leftover
2709 bytes. */
2710 addr += (entry_end - addr) % (2 * address_size);
2711
2712 for (;;)
2713 {
2714 if (addr + 2 * address_size > entry_end)
2715 {
2716 warning (_("Section .debug_aranges in %s entry at offset %s "
2717 "address list is not properly terminated, "
2718 "ignoring .debug_aranges."),
2719 objfile_name (objfile),
2720 plongest (entry_addr - section->buffer));
2721 return;
2722 }
2723 ULONGEST start = extract_unsigned_integer (addr, address_size,
2724 dwarf5_byte_order);
2725 addr += address_size;
2726 ULONGEST length = extract_unsigned_integer (addr, address_size,
2727 dwarf5_byte_order);
2728 addr += address_size;
2729 if (start == 0 && length == 0)
2730 break;
2731 if (start == 0 && !per_bfd->has_section_at_zero)
2732 {
2733 /* Symbol was eliminated due to a COMDAT group. */
2734 continue;
2735 }
2736 ULONGEST end = start + length;
2737 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2738 - baseaddr);
2739 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2740 - baseaddr);
2741 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2742 }
2743 }
2744
2745 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2746 &per_bfd->obstack);
2747 }
2748
2749 /* A helper function that reads the .gdb_index from BUFFER and fills
2750 in MAP. FILENAME is the name of the file containing the data;
2751 it is used for error reporting. DEPRECATED_OK is true if it is
2752 ok to use deprecated sections.
2753
2754 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2755 out parameters that are filled in with information about the CU and
2756 TU lists in the section.
2757
2758 Returns true if all went well, false otherwise. */
2759
2760 static bool
2761 read_gdb_index_from_buffer (const char *filename,
2762 bool deprecated_ok,
2763 gdb::array_view<const gdb_byte> buffer,
2764 struct mapped_index *map,
2765 const gdb_byte **cu_list,
2766 offset_type *cu_list_elements,
2767 const gdb_byte **types_list,
2768 offset_type *types_list_elements)
2769 {
2770 const gdb_byte *addr = &buffer[0];
2771 offset_view metadata (buffer);
2772
2773 /* Version check. */
2774 offset_type version = metadata[0];
2775 /* Versions earlier than 3 emitted every copy of a psymbol. This
2776 causes the index to behave very poorly for certain requests. Version 3
2777 contained incomplete addrmap. So, it seems better to just ignore such
2778 indices. */
2779 if (version < 4)
2780 {
2781 static int warning_printed = 0;
2782 if (!warning_printed)
2783 {
2784 warning (_("Skipping obsolete .gdb_index section in %s."),
2785 filename);
2786 warning_printed = 1;
2787 }
2788 return 0;
2789 }
2790 /* Index version 4 uses a different hash function than index version
2791 5 and later.
2792
2793 Versions earlier than 6 did not emit psymbols for inlined
2794 functions. Using these files will cause GDB not to be able to
2795 set breakpoints on inlined functions by name, so we ignore these
2796 indices unless the user has done
2797 "set use-deprecated-index-sections on". */
2798 if (version < 6 && !deprecated_ok)
2799 {
2800 static int warning_printed = 0;
2801 if (!warning_printed)
2802 {
2803 warning (_("\
2804 Skipping deprecated .gdb_index section in %s.\n\
2805 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2806 to use the section anyway."),
2807 filename);
2808 warning_printed = 1;
2809 }
2810 return 0;
2811 }
2812 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2813 of the TU (for symbols coming from TUs),
2814 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2815 Plus gold-generated indices can have duplicate entries for global symbols,
2816 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2817 These are just performance bugs, and we can't distinguish gdb-generated
2818 indices from gold-generated ones, so issue no warning here. */
2819
2820 /* Indexes with higher version than the one supported by GDB may be no
2821 longer backward compatible. */
2822 if (version > 8)
2823 return 0;
2824
2825 map->version = version;
2826
2827 int i = 1;
2828 *cu_list = addr + metadata[i];
2829 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2830 ++i;
2831
2832 *types_list = addr + metadata[i];
2833 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2834 ++i;
2835
2836 const gdb_byte *address_table = addr + metadata[i];
2837 const gdb_byte *address_table_end = addr + metadata[i + 1];
2838 map->address_table
2839 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2840 ++i;
2841
2842 const gdb_byte *symbol_table = addr + metadata[i];
2843 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2844 map->symbol_table
2845 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2846 symbol_table_end));
2847
2848 ++i;
2849 map->constant_pool = buffer.slice (metadata[i]);
2850
2851 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2852 {
2853 /* An empty constant pool implies that all symbol table entries are
2854 empty. Make map->symbol_table.empty () == true. */
2855 map->symbol_table
2856 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2857 symbol_table));
2858 }
2859
2860 return 1;
2861 }
2862
2863 /* Callback types for dwarf2_read_gdb_index. */
2864
2865 typedef gdb::function_view
2866 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2867 get_gdb_index_contents_ftype;
2868 typedef gdb::function_view
2869 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2870 get_gdb_index_contents_dwz_ftype;
2871
2872 /* Read .gdb_index. If everything went ok, initialize the "quick"
2873 elements of all the CUs and return 1. Otherwise, return 0. */
2874
2875 static int
2876 dwarf2_read_gdb_index
2877 (dwarf2_per_objfile *per_objfile,
2878 get_gdb_index_contents_ftype get_gdb_index_contents,
2879 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2880 {
2881 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2882 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2883 struct dwz_file *dwz;
2884 struct objfile *objfile = per_objfile->objfile;
2885 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2886
2887 gdb::array_view<const gdb_byte> main_index_contents
2888 = get_gdb_index_contents (objfile, per_bfd);
2889
2890 if (main_index_contents.empty ())
2891 return 0;
2892
2893 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2894 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2895 use_deprecated_index_sections,
2896 main_index_contents, map.get (), &cu_list,
2897 &cu_list_elements, &types_list,
2898 &types_list_elements))
2899 return 0;
2900
2901 /* Don't use the index if it's empty. */
2902 if (map->symbol_table.empty ())
2903 return 0;
2904
2905 /* If there is a .dwz file, read it so we can get its CU list as
2906 well. */
2907 dwz = dwarf2_get_dwz_file (per_bfd);
2908 if (dwz != NULL)
2909 {
2910 struct mapped_index dwz_map;
2911 const gdb_byte *dwz_types_ignore;
2912 offset_type dwz_types_elements_ignore;
2913
2914 gdb::array_view<const gdb_byte> dwz_index_content
2915 = get_gdb_index_contents_dwz (objfile, dwz);
2916
2917 if (dwz_index_content.empty ())
2918 return 0;
2919
2920 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2921 1, dwz_index_content, &dwz_map,
2922 &dwz_list, &dwz_list_elements,
2923 &dwz_types_ignore,
2924 &dwz_types_elements_ignore))
2925 {
2926 warning (_("could not read '.gdb_index' section from %s; skipping"),
2927 bfd_get_filename (dwz->dwz_bfd.get ()));
2928 return 0;
2929 }
2930 }
2931
2932 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2933 dwz_list_elements);
2934
2935 if (types_list_elements)
2936 {
2937 /* We can only handle a single .debug_types when we have an
2938 index. */
2939 if (per_bfd->types.size () != 1)
2940 return 0;
2941
2942 dwarf2_section_info *section = &per_bfd->types[0];
2943
2944 create_signatured_type_table_from_index (per_bfd, section, types_list,
2945 types_list_elements);
2946 }
2947
2948 create_addrmap_from_index (per_objfile, map.get ());
2949
2950 per_bfd->index_table = std::move (map);
2951 per_bfd->using_index = 1;
2952 per_bfd->quick_file_names_table =
2953 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2954
2955 return 1;
2956 }
2957
2958 /* die_reader_func for dw2_get_file_names. */
2959
2960 static void
2961 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2962 struct die_info *comp_unit_die)
2963 {
2964 struct dwarf2_cu *cu = reader->cu;
2965 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2966 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2967 struct dwarf2_per_cu_data *lh_cu;
2968 struct attribute *attr;
2969 void **slot;
2970 struct quick_file_names *qfn;
2971
2972 gdb_assert (! this_cu->is_debug_types);
2973
2974 this_cu->v.quick->files_read = true;
2975 /* Our callers never want to match partial units -- instead they
2976 will match the enclosing full CU. */
2977 if (comp_unit_die->tag == DW_TAG_partial_unit)
2978 return;
2979
2980 lh_cu = this_cu;
2981 slot = NULL;
2982
2983 line_header_up lh;
2984 sect_offset line_offset {};
2985
2986 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2987 if (attr != nullptr && attr->form_is_unsigned ())
2988 {
2989 struct quick_file_names find_entry;
2990
2991 line_offset = (sect_offset) attr->as_unsigned ();
2992
2993 /* We may have already read in this line header (TU line header sharing).
2994 If we have we're done. */
2995 find_entry.hash.dwo_unit = cu->dwo_unit;
2996 find_entry.hash.line_sect_off = line_offset;
2997 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2998 &find_entry, INSERT);
2999 if (*slot != NULL)
3000 {
3001 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3002 return;
3003 }
3004
3005 lh = dwarf_decode_line_header (line_offset, cu);
3006 }
3007
3008 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
3009
3010 int offset = 0;
3011 if (!fnd.is_unknown ())
3012 ++offset;
3013 else if (lh == nullptr)
3014 return;
3015
3016 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3017 qfn->hash.dwo_unit = cu->dwo_unit;
3018 qfn->hash.line_sect_off = line_offset;
3019 /* There may not be a DW_AT_stmt_list. */
3020 if (slot != nullptr)
3021 *slot = qfn;
3022
3023 std::vector<const char *> include_names;
3024 if (lh != nullptr)
3025 {
3026 for (const auto &entry : lh->file_names ())
3027 {
3028 gdb::unique_xmalloc_ptr<char> name_holder;
3029 const char *include_name =
3030 compute_include_file_name (lh.get (), entry, fnd, &name_holder);
3031 if (include_name != nullptr)
3032 {
3033 include_name = per_objfile->objfile->intern (include_name);
3034 include_names.push_back (include_name);
3035 }
3036 }
3037 }
3038
3039 qfn->num_file_names = offset + include_names.size ();
3040 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
3041 qfn->file_names =
3042 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3043 qfn->num_file_names);
3044 if (offset != 0)
3045 qfn->file_names[0] = xstrdup (fnd.get_name ());
3046
3047 if (!include_names.empty ())
3048 memcpy (&qfn->file_names[offset], include_names.data (),
3049 include_names.size () * sizeof (const char *));
3050
3051 qfn->real_names = NULL;
3052
3053 lh_cu->v.quick->file_names = qfn;
3054 }
3055
3056 /* A helper for the "quick" functions which attempts to read the line
3057 table for THIS_CU. */
3058
3059 static struct quick_file_names *
3060 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3061 dwarf2_per_objfile *per_objfile)
3062 {
3063 /* This should never be called for TUs. */
3064 gdb_assert (! this_cu->is_debug_types);
3065 /* Nor type unit groups. */
3066 gdb_assert (! this_cu->type_unit_group_p ());
3067
3068 if (this_cu->v.quick->files_read)
3069 return this_cu->v.quick->file_names;
3070
3071 cutu_reader reader (this_cu, per_objfile);
3072 if (!reader.dummy_p)
3073 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3074
3075 return this_cu->v.quick->file_names;
3076 }
3077
3078 /* A helper for the "quick" functions which computes and caches the
3079 real path for a given file name from the line table. */
3080
3081 static const char *
3082 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3083 struct quick_file_names *qfn, int index)
3084 {
3085 if (qfn->real_names == NULL)
3086 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3087 qfn->num_file_names, const char *);
3088
3089 if (qfn->real_names[index] == NULL)
3090 {
3091 const char *dirname = nullptr;
3092
3093 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
3094 dirname = qfn->comp_dir;
3095
3096 gdb::unique_xmalloc_ptr<char> fullname;
3097 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
3098
3099 qfn->real_names[index] = fullname.release ();
3100 }
3101
3102 return qfn->real_names[index];
3103 }
3104
3105 struct symtab *
3106 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3107 {
3108 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3109 dwarf2_per_cu_data *dwarf_cu
3110 = per_objfile->per_bfd->all_comp_units.back ().get ();
3111 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3112
3113 if (cust == NULL)
3114 return NULL;
3115
3116 return cust->primary_filetab ();
3117 }
3118
3119 /* See read.h. */
3120
3121 void
3122 dwarf2_per_cu_data::free_cached_file_names ()
3123 {
3124 if (per_bfd == nullptr || !per_bfd->using_index || v.quick == nullptr)
3125 return;
3126
3127 struct quick_file_names *file_data = v.quick->file_names;
3128 if (file_data != nullptr && file_data->real_names != nullptr)
3129 {
3130 for (int i = 0; i < file_data->num_file_names; ++i)
3131 {
3132 xfree ((void *) file_data->real_names[i]);
3133 file_data->real_names[i] = nullptr;
3134 }
3135 }
3136 }
3137
3138 void
3139 dwarf2_base_index_functions::forget_cached_source_info
3140 (struct objfile *objfile)
3141 {
3142 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3143
3144 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
3145 per_cu->free_cached_file_names ();
3146 }
3147
3148 /* Struct used to manage iterating over all CUs looking for a symbol. */
3149
3150 struct dw2_symtab_iterator
3151 {
3152 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3153 dwarf2_per_objfile *per_objfile;
3154 /* If set, only look for symbols that match that block. Valid values are
3155 GLOBAL_BLOCK and STATIC_BLOCK. */
3156 gdb::optional<block_enum> block_index;
3157 /* The kind of symbol we're looking for. */
3158 domain_enum domain;
3159 /* The list of CUs from the index entry of the symbol,
3160 or NULL if not found. */
3161 offset_view vec;
3162 /* The next element in VEC to look at. */
3163 int next;
3164 /* The number of elements in VEC, or zero if there is no match. */
3165 int length;
3166 /* Have we seen a global version of the symbol?
3167 If so we can ignore all further global instances.
3168 This is to work around gold/15646, inefficient gold-generated
3169 indices. */
3170 int global_seen;
3171 };
3172
3173 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3174
3175 static void
3176 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3177 dwarf2_per_objfile *per_objfile,
3178 gdb::optional<block_enum> block_index,
3179 domain_enum domain, offset_type namei)
3180 {
3181 iter->per_objfile = per_objfile;
3182 iter->block_index = block_index;
3183 iter->domain = domain;
3184 iter->next = 0;
3185 iter->global_seen = 0;
3186 iter->vec = {};
3187 iter->length = 0;
3188
3189 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3190 /* index is NULL if OBJF_READNOW. */
3191 if (index == NULL)
3192 return;
3193
3194 gdb_assert (!index->symbol_name_slot_invalid (namei));
3195 offset_type vec_idx = index->symbol_vec_index (namei);
3196
3197 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3198 iter->length = iter->vec[0];
3199 }
3200
3201 /* Return the next matching CU or NULL if there are no more. */
3202
3203 static struct dwarf2_per_cu_data *
3204 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3205 {
3206 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3207
3208 for ( ; iter->next < iter->length; ++iter->next)
3209 {
3210 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3211 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3212 gdb_index_symbol_kind symbol_kind =
3213 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3214 /* Only check the symbol attributes if they're present.
3215 Indices prior to version 7 don't record them,
3216 and indices >= 7 may elide them for certain symbols
3217 (gold does this). */
3218 int attrs_valid =
3219 (per_objfile->per_bfd->index_table->version >= 7
3220 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3221
3222 /* Don't crash on bad data. */
3223 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3224 {
3225 complaint (_(".gdb_index entry has bad CU index"
3226 " [in module %s]"), objfile_name (per_objfile->objfile));
3227 continue;
3228 }
3229
3230 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3231
3232 /* Skip if already read in. */
3233 if (per_objfile->symtab_set_p (per_cu))
3234 continue;
3235
3236 /* Check static vs global. */
3237 if (attrs_valid)
3238 {
3239 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3240
3241 if (iter->block_index.has_value ())
3242 {
3243 bool want_static = *iter->block_index == STATIC_BLOCK;
3244
3245 if (is_static != want_static)
3246 continue;
3247 }
3248
3249 /* Work around gold/15646. */
3250 if (!is_static
3251 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3252 {
3253 if (iter->global_seen)
3254 continue;
3255
3256 iter->global_seen = 1;
3257 }
3258 }
3259
3260 /* Only check the symbol's kind if it has one. */
3261 if (attrs_valid)
3262 {
3263 switch (iter->domain)
3264 {
3265 case VAR_DOMAIN:
3266 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3267 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3268 /* Some types are also in VAR_DOMAIN. */
3269 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3270 continue;
3271 break;
3272 case STRUCT_DOMAIN:
3273 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3274 continue;
3275 break;
3276 case LABEL_DOMAIN:
3277 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3278 continue;
3279 break;
3280 case MODULE_DOMAIN:
3281 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3282 continue;
3283 break;
3284 default:
3285 break;
3286 }
3287 }
3288
3289 ++iter->next;
3290 return per_cu;
3291 }
3292
3293 return NULL;
3294 }
3295
3296 void
3297 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3298 bool print_bcache)
3299 {
3300 if (print_bcache)
3301 return;
3302
3303 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3304 int total = per_objfile->per_bfd->all_comp_units.size ();
3305 int count = 0;
3306
3307 for (int i = 0; i < total; ++i)
3308 {
3309 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3310
3311 if (!per_objfile->symtab_set_p (per_cu))
3312 ++count;
3313 }
3314 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3315 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3316 }
3317
3318 /* This dumps minimal information about the index.
3319 It is called via "mt print objfiles".
3320 One use is to verify .gdb_index has been loaded by the
3321 gdb.dwarf2/gdb-index.exp testcase. */
3322
3323 void
3324 dwarf2_gdb_index::dump (struct objfile *objfile)
3325 {
3326 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3327
3328 gdb_assert (per_objfile->per_bfd->using_index);
3329 gdb_printf (".gdb_index:");
3330 if (per_objfile->per_bfd->index_table != NULL)
3331 {
3332 gdb_printf (" version %d\n",
3333 per_objfile->per_bfd->index_table->version);
3334 }
3335 else
3336 gdb_printf (" faked for \"readnow\"\n");
3337 gdb_printf ("\n");
3338 }
3339
3340 void
3341 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3342 {
3343 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3344 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3345
3346 for (int i = 0; i < total_units; ++i)
3347 {
3348 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3349
3350 /* We don't want to directly expand a partial CU, because if we
3351 read it with the wrong language, then assertion failures can
3352 be triggered later on. See PR symtab/23010. So, tell
3353 dw2_instantiate_symtab to skip partial CUs -- any important
3354 partial CU will be read via DW_TAG_imported_unit anyway. */
3355 dw2_instantiate_symtab (per_cu, per_objfile, true);
3356 }
3357 }
3358
3359 static bool
3360 dw2_expand_symtabs_matching_symbol
3361 (mapped_index_base &index,
3362 const lookup_name_info &lookup_name_in,
3363 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3364 gdb::function_view<bool (offset_type)> match_callback,
3365 dwarf2_per_objfile *per_objfile);
3366
3367 static bool
3368 dw2_expand_symtabs_matching_one
3369 (dwarf2_per_cu_data *per_cu,
3370 dwarf2_per_objfile *per_objfile,
3371 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3372 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3373
3374 void
3375 dwarf2_gdb_index::expand_matching_symbols
3376 (struct objfile *objfile,
3377 const lookup_name_info &name, domain_enum domain,
3378 int global,
3379 symbol_compare_ftype *ordered_compare)
3380 {
3381 /* Used for Ada. */
3382 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3383
3384 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3385
3386 if (per_objfile->per_bfd->index_table != nullptr)
3387 {
3388 mapped_index &index = *per_objfile->per_bfd->index_table;
3389
3390 const char *match_name = name.ada ().lookup_name ().c_str ();
3391 auto matcher = [&] (const char *symname)
3392 {
3393 if (ordered_compare == nullptr)
3394 return true;
3395 return ordered_compare (symname, match_name) == 0;
3396 };
3397
3398 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3399 [&] (offset_type namei)
3400 {
3401 struct dw2_symtab_iterator iter;
3402 struct dwarf2_per_cu_data *per_cu;
3403
3404 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3405 namei);
3406 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3407 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3408 nullptr);
3409 return true;
3410 }, per_objfile);
3411 }
3412 else
3413 {
3414 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3415 proceed assuming all symtabs have been read in. */
3416 }
3417 }
3418
3419 /* Starting from a search name, return the string that finds the upper
3420 bound of all strings that start with SEARCH_NAME in a sorted name
3421 list. Returns the empty string to indicate that the upper bound is
3422 the end of the list. */
3423
3424 static std::string
3425 make_sort_after_prefix_name (const char *search_name)
3426 {
3427 /* When looking to complete "func", we find the upper bound of all
3428 symbols that start with "func" by looking for where we'd insert
3429 the closest string that would follow "func" in lexicographical
3430 order. Usually, that's "func"-with-last-character-incremented,
3431 i.e. "fund". Mind non-ASCII characters, though. Usually those
3432 will be UTF-8 multi-byte sequences, but we can't be certain.
3433 Especially mind the 0xff character, which is a valid character in
3434 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3435 rule out compilers allowing it in identifiers. Note that
3436 conveniently, strcmp/strcasecmp are specified to compare
3437 characters interpreted as unsigned char. So what we do is treat
3438 the whole string as a base 256 number composed of a sequence of
3439 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3440 to 0, and carries 1 to the following more-significant position.
3441 If the very first character in SEARCH_NAME ends up incremented
3442 and carries/overflows, then the upper bound is the end of the
3443 list. The string after the empty string is also the empty
3444 string.
3445
3446 Some examples of this operation:
3447
3448 SEARCH_NAME => "+1" RESULT
3449
3450 "abc" => "abd"
3451 "ab\xff" => "ac"
3452 "\xff" "a" "\xff" => "\xff" "b"
3453 "\xff" => ""
3454 "\xff\xff" => ""
3455 "" => ""
3456
3457 Then, with these symbols for example:
3458
3459 func
3460 func1
3461 fund
3462
3463 completing "func" looks for symbols between "func" and
3464 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3465 which finds "func" and "func1", but not "fund".
3466
3467 And with:
3468
3469 funcÿ (Latin1 'ÿ' [0xff])
3470 funcÿ1
3471 fund
3472
3473 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3474 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3475
3476 And with:
3477
3478 ÿÿ (Latin1 'ÿ' [0xff])
3479 ÿÿ1
3480
3481 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3482 the end of the list.
3483 */
3484 std::string after = search_name;
3485 while (!after.empty () && (unsigned char) after.back () == 0xff)
3486 after.pop_back ();
3487 if (!after.empty ())
3488 after.back () = (unsigned char) after.back () + 1;
3489 return after;
3490 }
3491
3492 /* See declaration. */
3493
3494 std::pair<std::vector<name_component>::const_iterator,
3495 std::vector<name_component>::const_iterator>
3496 mapped_index_base::find_name_components_bounds
3497 (const lookup_name_info &lookup_name_without_params, language lang,
3498 dwarf2_per_objfile *per_objfile) const
3499 {
3500 auto *name_cmp
3501 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3502
3503 const char *lang_name
3504 = lookup_name_without_params.language_lookup_name (lang);
3505
3506 /* Comparison function object for lower_bound that matches against a
3507 given symbol name. */
3508 auto lookup_compare_lower = [&] (const name_component &elem,
3509 const char *name)
3510 {
3511 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3512 const char *elem_name = elem_qualified + elem.name_offset;
3513 return name_cmp (elem_name, name) < 0;
3514 };
3515
3516 /* Comparison function object for upper_bound that matches against a
3517 given symbol name. */
3518 auto lookup_compare_upper = [&] (const char *name,
3519 const name_component &elem)
3520 {
3521 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3522 const char *elem_name = elem_qualified + elem.name_offset;
3523 return name_cmp (name, elem_name) < 0;
3524 };
3525
3526 auto begin = this->name_components.begin ();
3527 auto end = this->name_components.end ();
3528
3529 /* Find the lower bound. */
3530 auto lower = [&] ()
3531 {
3532 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3533 return begin;
3534 else
3535 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3536 } ();
3537
3538 /* Find the upper bound. */
3539 auto upper = [&] ()
3540 {
3541 if (lookup_name_without_params.completion_mode ())
3542 {
3543 /* In completion mode, we want UPPER to point past all
3544 symbols names that have the same prefix. I.e., with
3545 these symbols, and completing "func":
3546
3547 function << lower bound
3548 function1
3549 other_function << upper bound
3550
3551 We find the upper bound by looking for the insertion
3552 point of "func"-with-last-character-incremented,
3553 i.e. "fund". */
3554 std::string after = make_sort_after_prefix_name (lang_name);
3555 if (after.empty ())
3556 return end;
3557 return std::lower_bound (lower, end, after.c_str (),
3558 lookup_compare_lower);
3559 }
3560 else
3561 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3562 } ();
3563
3564 return {lower, upper};
3565 }
3566
3567 /* See declaration. */
3568
3569 void
3570 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3571 {
3572 if (!this->name_components.empty ())
3573 return;
3574
3575 this->name_components_casing = case_sensitivity;
3576 auto *name_cmp
3577 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3578
3579 /* The code below only knows how to break apart components of C++
3580 symbol names (and other languages that use '::' as
3581 namespace/module separator) and Ada symbol names. */
3582 auto count = this->symbol_name_count ();
3583 for (offset_type idx = 0; idx < count; idx++)
3584 {
3585 if (this->symbol_name_slot_invalid (idx))
3586 continue;
3587
3588 const char *name = this->symbol_name_at (idx, per_objfile);
3589
3590 /* Add each name component to the name component table. */
3591 unsigned int previous_len = 0;
3592
3593 if (strstr (name, "::") != nullptr)
3594 {
3595 for (unsigned int current_len = cp_find_first_component (name);
3596 name[current_len] != '\0';
3597 current_len += cp_find_first_component (name + current_len))
3598 {
3599 gdb_assert (name[current_len] == ':');
3600 this->name_components.push_back ({previous_len, idx});
3601 /* Skip the '::'. */
3602 current_len += 2;
3603 previous_len = current_len;
3604 }
3605 }
3606 else
3607 {
3608 /* Handle the Ada encoded (aka mangled) form here. */
3609 for (const char *iter = strstr (name, "__");
3610 iter != nullptr;
3611 iter = strstr (iter, "__"))
3612 {
3613 this->name_components.push_back ({previous_len, idx});
3614 iter += 2;
3615 previous_len = iter - name;
3616 }
3617 }
3618
3619 this->name_components.push_back ({previous_len, idx});
3620 }
3621
3622 /* Sort name_components elements by name. */
3623 auto name_comp_compare = [&] (const name_component &left,
3624 const name_component &right)
3625 {
3626 const char *left_qualified
3627 = this->symbol_name_at (left.idx, per_objfile);
3628 const char *right_qualified
3629 = this->symbol_name_at (right.idx, per_objfile);
3630
3631 const char *left_name = left_qualified + left.name_offset;
3632 const char *right_name = right_qualified + right.name_offset;
3633
3634 return name_cmp (left_name, right_name) < 0;
3635 };
3636
3637 std::sort (this->name_components.begin (),
3638 this->name_components.end (),
3639 name_comp_compare);
3640 }
3641
3642 /* Helper for dw2_expand_symtabs_matching that works with a
3643 mapped_index_base instead of the containing objfile. This is split
3644 to a separate function in order to be able to unit test the
3645 name_components matching using a mock mapped_index_base. For each
3646 symbol name that matches, calls MATCH_CALLBACK, passing it the
3647 symbol's index in the mapped_index_base symbol table. */
3648
3649 static bool
3650 dw2_expand_symtabs_matching_symbol
3651 (mapped_index_base &index,
3652 const lookup_name_info &lookup_name_in,
3653 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3654 gdb::function_view<bool (offset_type)> match_callback,
3655 dwarf2_per_objfile *per_objfile)
3656 {
3657 lookup_name_info lookup_name_without_params
3658 = lookup_name_in.make_ignore_params ();
3659
3660 /* Build the symbol name component sorted vector, if we haven't
3661 yet. */
3662 index.build_name_components (per_objfile);
3663
3664 /* The same symbol may appear more than once in the range though.
3665 E.g., if we're looking for symbols that complete "w", and we have
3666 a symbol named "w1::w2", we'll find the two name components for
3667 that same symbol in the range. To be sure we only call the
3668 callback once per symbol, we first collect the symbol name
3669 indexes that matched in a temporary vector and ignore
3670 duplicates. */
3671 std::vector<offset_type> matches;
3672
3673 struct name_and_matcher
3674 {
3675 symbol_name_matcher_ftype *matcher;
3676 const char *name;
3677
3678 bool operator== (const name_and_matcher &other) const
3679 {
3680 return matcher == other.matcher && strcmp (name, other.name) == 0;
3681 }
3682 };
3683
3684 /* A vector holding all the different symbol name matchers, for all
3685 languages. */
3686 std::vector<name_and_matcher> matchers;
3687
3688 for (int i = 0; i < nr_languages; i++)
3689 {
3690 enum language lang_e = (enum language) i;
3691
3692 const language_defn *lang = language_def (lang_e);
3693 symbol_name_matcher_ftype *name_matcher
3694 = lang->get_symbol_name_matcher (lookup_name_without_params);
3695
3696 name_and_matcher key {
3697 name_matcher,
3698 lookup_name_without_params.language_lookup_name (lang_e)
3699 };
3700
3701 /* Don't insert the same comparison routine more than once.
3702 Note that we do this linear walk. This is not a problem in
3703 practice because the number of supported languages is
3704 low. */
3705 if (std::find (matchers.begin (), matchers.end (), key)
3706 != matchers.end ())
3707 continue;
3708 matchers.push_back (std::move (key));
3709
3710 auto bounds
3711 = index.find_name_components_bounds (lookup_name_without_params,
3712 lang_e, per_objfile);
3713
3714 /* Now for each symbol name in range, check to see if we have a name
3715 match, and if so, call the MATCH_CALLBACK callback. */
3716
3717 for (; bounds.first != bounds.second; ++bounds.first)
3718 {
3719 const char *qualified
3720 = index.symbol_name_at (bounds.first->idx, per_objfile);
3721
3722 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3723 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3724 continue;
3725
3726 matches.push_back (bounds.first->idx);
3727 }
3728 }
3729
3730 std::sort (matches.begin (), matches.end ());
3731
3732 /* Finally call the callback, once per match. */
3733 ULONGEST prev = -1;
3734 bool result = true;
3735 for (offset_type idx : matches)
3736 {
3737 if (prev != idx)
3738 {
3739 if (!match_callback (idx))
3740 {
3741 result = false;
3742 break;
3743 }
3744 prev = idx;
3745 }
3746 }
3747
3748 /* Above we use a type wider than idx's for 'prev', since 0 and
3749 (offset_type)-1 are both possible values. */
3750 static_assert (sizeof (prev) > sizeof (offset_type), "");
3751
3752 return result;
3753 }
3754
3755 #if GDB_SELF_TEST
3756
3757 namespace selftests { namespace dw2_expand_symtabs_matching {
3758
3759 /* A mock .gdb_index/.debug_names-like name index table, enough to
3760 exercise dw2_expand_symtabs_matching_symbol, which works with the
3761 mapped_index_base interface. Builds an index from the symbol list
3762 passed as parameter to the constructor. */
3763 class mock_mapped_index : public mapped_index_base
3764 {
3765 public:
3766 mock_mapped_index (gdb::array_view<const char *> symbols)
3767 : m_symbol_table (symbols)
3768 {}
3769
3770 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3771
3772 /* Return the number of names in the symbol table. */
3773 size_t symbol_name_count () const override
3774 {
3775 return m_symbol_table.size ();
3776 }
3777
3778 /* Get the name of the symbol at IDX in the symbol table. */
3779 const char *symbol_name_at
3780 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3781 {
3782 return m_symbol_table[idx];
3783 }
3784
3785 private:
3786 gdb::array_view<const char *> m_symbol_table;
3787 };
3788
3789 /* Convenience function that converts a NULL pointer to a "<null>"
3790 string, to pass to print routines. */
3791
3792 static const char *
3793 string_or_null (const char *str)
3794 {
3795 return str != NULL ? str : "<null>";
3796 }
3797
3798 /* Check if a lookup_name_info built from
3799 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3800 index. EXPECTED_LIST is the list of expected matches, in expected
3801 matching order. If no match expected, then an empty list is
3802 specified. Returns true on success. On failure prints a warning
3803 indicating the file:line that failed, and returns false. */
3804
3805 static bool
3806 check_match (const char *file, int line,
3807 mock_mapped_index &mock_index,
3808 const char *name, symbol_name_match_type match_type,
3809 bool completion_mode,
3810 std::initializer_list<const char *> expected_list,
3811 dwarf2_per_objfile *per_objfile)
3812 {
3813 lookup_name_info lookup_name (name, match_type, completion_mode);
3814
3815 bool matched = true;
3816
3817 auto mismatch = [&] (const char *expected_str,
3818 const char *got)
3819 {
3820 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3821 "expected=\"%s\", got=\"%s\"\n"),
3822 file, line,
3823 (match_type == symbol_name_match_type::FULL
3824 ? "FULL" : "WILD"),
3825 name, string_or_null (expected_str), string_or_null (got));
3826 matched = false;
3827 };
3828
3829 auto expected_it = expected_list.begin ();
3830 auto expected_end = expected_list.end ();
3831
3832 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3833 nullptr,
3834 [&] (offset_type idx)
3835 {
3836 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3837 const char *expected_str
3838 = expected_it == expected_end ? NULL : *expected_it++;
3839
3840 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3841 mismatch (expected_str, matched_name);
3842 return true;
3843 }, per_objfile);
3844
3845 const char *expected_str
3846 = expected_it == expected_end ? NULL : *expected_it++;
3847 if (expected_str != NULL)
3848 mismatch (expected_str, NULL);
3849
3850 return matched;
3851 }
3852
3853 /* The symbols added to the mock mapped_index for testing (in
3854 canonical form). */
3855 static const char *test_symbols[] = {
3856 "function",
3857 "std::bar",
3858 "std::zfunction",
3859 "std::zfunction2",
3860 "w1::w2",
3861 "ns::foo<char*>",
3862 "ns::foo<int>",
3863 "ns::foo<long>",
3864 "ns2::tmpl<int>::foo2",
3865 "(anonymous namespace)::A::B::C",
3866
3867 /* These are used to check that the increment-last-char in the
3868 matching algorithm for completion doesn't match "t1_fund" when
3869 completing "t1_func". */
3870 "t1_func",
3871 "t1_func1",
3872 "t1_fund",
3873 "t1_fund1",
3874
3875 /* A UTF-8 name with multi-byte sequences to make sure that
3876 cp-name-parser understands this as a single identifier ("função"
3877 is "function" in PT). */
3878 u8"u8função",
3879
3880 /* \377 (0xff) is Latin1 'ÿ'. */
3881 "yfunc\377",
3882
3883 /* \377 (0xff) is Latin1 'ÿ'. */
3884 "\377",
3885 "\377\377123",
3886
3887 /* A name with all sorts of complications. Starts with "z" to make
3888 it easier for the completion tests below. */
3889 #define Z_SYM_NAME \
3890 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3891 "::tuple<(anonymous namespace)::ui*, " \
3892 "std::default_delete<(anonymous namespace)::ui>, void>"
3893
3894 Z_SYM_NAME
3895 };
3896
3897 /* Returns true if the mapped_index_base::find_name_component_bounds
3898 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3899 in completion mode. */
3900
3901 static bool
3902 check_find_bounds_finds (mapped_index_base &index,
3903 const char *search_name,
3904 gdb::array_view<const char *> expected_syms,
3905 dwarf2_per_objfile *per_objfile)
3906 {
3907 lookup_name_info lookup_name (search_name,
3908 symbol_name_match_type::FULL, true);
3909
3910 auto bounds = index.find_name_components_bounds (lookup_name,
3911 language_cplus,
3912 per_objfile);
3913
3914 size_t distance = std::distance (bounds.first, bounds.second);
3915 if (distance != expected_syms.size ())
3916 return false;
3917
3918 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3919 {
3920 auto nc_elem = bounds.first + exp_elem;
3921 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3922 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3923 return false;
3924 }
3925
3926 return true;
3927 }
3928
3929 /* Test the lower-level mapped_index::find_name_component_bounds
3930 method. */
3931
3932 static void
3933 test_mapped_index_find_name_component_bounds ()
3934 {
3935 mock_mapped_index mock_index (test_symbols);
3936
3937 mock_index.build_name_components (NULL /* per_objfile */);
3938
3939 /* Test the lower-level mapped_index::find_name_component_bounds
3940 method in completion mode. */
3941 {
3942 static const char *expected_syms[] = {
3943 "t1_func",
3944 "t1_func1",
3945 };
3946
3947 SELF_CHECK (check_find_bounds_finds
3948 (mock_index, "t1_func", expected_syms,
3949 NULL /* per_objfile */));
3950 }
3951
3952 /* Check that the increment-last-char in the name matching algorithm
3953 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3954 {
3955 static const char *expected_syms1[] = {
3956 "\377",
3957 "\377\377123",
3958 };
3959 SELF_CHECK (check_find_bounds_finds
3960 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3961
3962 static const char *expected_syms2[] = {
3963 "\377\377123",
3964 };
3965 SELF_CHECK (check_find_bounds_finds
3966 (mock_index, "\377\377", expected_syms2,
3967 NULL /* per_objfile */));
3968 }
3969 }
3970
3971 /* Test dw2_expand_symtabs_matching_symbol. */
3972
3973 static void
3974 test_dw2_expand_symtabs_matching_symbol ()
3975 {
3976 mock_mapped_index mock_index (test_symbols);
3977
3978 /* We let all tests run until the end even if some fails, for debug
3979 convenience. */
3980 bool any_mismatch = false;
3981
3982 /* Create the expected symbols list (an initializer_list). Needed
3983 because lists have commas, and we need to pass them to CHECK,
3984 which is a macro. */
3985 #define EXPECT(...) { __VA_ARGS__ }
3986
3987 /* Wrapper for check_match that passes down the current
3988 __FILE__/__LINE__. */
3989 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3990 any_mismatch |= !check_match (__FILE__, __LINE__, \
3991 mock_index, \
3992 NAME, MATCH_TYPE, COMPLETION_MODE, \
3993 EXPECTED_LIST, NULL)
3994
3995 /* Identity checks. */
3996 for (const char *sym : test_symbols)
3997 {
3998 /* Should be able to match all existing symbols. */
3999 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4000 EXPECT (sym));
4001
4002 /* Should be able to match all existing symbols with
4003 parameters. */
4004 std::string with_params = std::string (sym) + "(int)";
4005 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4006 EXPECT (sym));
4007
4008 /* Should be able to match all existing symbols with
4009 parameters and qualifiers. */
4010 with_params = std::string (sym) + " ( int ) const";
4011 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4012 EXPECT (sym));
4013
4014 /* This should really find sym, but cp-name-parser.y doesn't
4015 know about lvalue/rvalue qualifiers yet. */
4016 with_params = std::string (sym) + " ( int ) &&";
4017 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4018 {});
4019 }
4020
4021 /* Check that the name matching algorithm for completion doesn't get
4022 confused with Latin1 'ÿ' / 0xff. */
4023 {
4024 static const char str[] = "\377";
4025 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4026 EXPECT ("\377", "\377\377123"));
4027 }
4028
4029 /* Check that the increment-last-char in the matching algorithm for
4030 completion doesn't match "t1_fund" when completing "t1_func". */
4031 {
4032 static const char str[] = "t1_func";
4033 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4034 EXPECT ("t1_func", "t1_func1"));
4035 }
4036
4037 /* Check that completion mode works at each prefix of the expected
4038 symbol name. */
4039 {
4040 static const char str[] = "function(int)";
4041 size_t len = strlen (str);
4042 std::string lookup;
4043
4044 for (size_t i = 1; i < len; i++)
4045 {
4046 lookup.assign (str, i);
4047 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4048 EXPECT ("function"));
4049 }
4050 }
4051
4052 /* While "w" is a prefix of both components, the match function
4053 should still only be called once. */
4054 {
4055 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4056 EXPECT ("w1::w2"));
4057 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4058 EXPECT ("w1::w2"));
4059 }
4060
4061 /* Same, with a "complicated" symbol. */
4062 {
4063 static const char str[] = Z_SYM_NAME;
4064 size_t len = strlen (str);
4065 std::string lookup;
4066
4067 for (size_t i = 1; i < len; i++)
4068 {
4069 lookup.assign (str, i);
4070 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4071 EXPECT (Z_SYM_NAME));
4072 }
4073 }
4074
4075 /* In FULL mode, an incomplete symbol doesn't match. */
4076 {
4077 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4078 {});
4079 }
4080
4081 /* A complete symbol with parameters matches any overload, since the
4082 index has no overload info. */
4083 {
4084 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4085 EXPECT ("std::zfunction", "std::zfunction2"));
4086 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4087 EXPECT ("std::zfunction", "std::zfunction2"));
4088 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4089 EXPECT ("std::zfunction", "std::zfunction2"));
4090 }
4091
4092 /* Check that whitespace is ignored appropriately. A symbol with a
4093 template argument list. */
4094 {
4095 static const char expected[] = "ns::foo<int>";
4096 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4097 EXPECT (expected));
4098 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4099 EXPECT (expected));
4100 }
4101
4102 /* Check that whitespace is ignored appropriately. A symbol with a
4103 template argument list that includes a pointer. */
4104 {
4105 static const char expected[] = "ns::foo<char*>";
4106 /* Try both completion and non-completion modes. */
4107 static const bool completion_mode[2] = {false, true};
4108 for (size_t i = 0; i < 2; i++)
4109 {
4110 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4111 completion_mode[i], EXPECT (expected));
4112 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4113 completion_mode[i], EXPECT (expected));
4114
4115 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4116 completion_mode[i], EXPECT (expected));
4117 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4118 completion_mode[i], EXPECT (expected));
4119 }
4120 }
4121
4122 {
4123 /* Check method qualifiers are ignored. */
4124 static const char expected[] = "ns::foo<char*>";
4125 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4126 symbol_name_match_type::FULL, true, EXPECT (expected));
4127 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4128 symbol_name_match_type::FULL, true, EXPECT (expected));
4129 CHECK_MATCH ("foo < char * > ( int ) const",
4130 symbol_name_match_type::WILD, true, EXPECT (expected));
4131 CHECK_MATCH ("foo < char * > ( int ) &&",
4132 symbol_name_match_type::WILD, true, EXPECT (expected));
4133 }
4134
4135 /* Test lookup names that don't match anything. */
4136 {
4137 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4138 {});
4139
4140 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4141 {});
4142 }
4143
4144 /* Some wild matching tests, exercising "(anonymous namespace)",
4145 which should not be confused with a parameter list. */
4146 {
4147 static const char *syms[] = {
4148 "A::B::C",
4149 "B::C",
4150 "C",
4151 "A :: B :: C ( int )",
4152 "B :: C ( int )",
4153 "C ( int )",
4154 };
4155
4156 for (const char *s : syms)
4157 {
4158 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4159 EXPECT ("(anonymous namespace)::A::B::C"));
4160 }
4161 }
4162
4163 {
4164 static const char expected[] = "ns2::tmpl<int>::foo2";
4165 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4166 EXPECT (expected));
4167 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4168 EXPECT (expected));
4169 }
4170
4171 SELF_CHECK (!any_mismatch);
4172
4173 #undef EXPECT
4174 #undef CHECK_MATCH
4175 }
4176
4177 static void
4178 run_test ()
4179 {
4180 test_mapped_index_find_name_component_bounds ();
4181 test_dw2_expand_symtabs_matching_symbol ();
4182 }
4183
4184 }} // namespace selftests::dw2_expand_symtabs_matching
4185
4186 #endif /* GDB_SELF_TEST */
4187
4188 /* If FILE_MATCHER is NULL or if PER_CU has
4189 dwarf2_per_cu_quick_data::MARK set (see
4190 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4191 EXPANSION_NOTIFY on it. */
4192
4193 static bool
4194 dw2_expand_symtabs_matching_one
4195 (dwarf2_per_cu_data *per_cu,
4196 dwarf2_per_objfile *per_objfile,
4197 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4198 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4199 {
4200 if (file_matcher == NULL || per_cu->v.quick->mark)
4201 {
4202 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4203
4204 compunit_symtab *symtab
4205 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4206 gdb_assert (symtab != nullptr);
4207
4208 if (expansion_notify != NULL && symtab_was_null)
4209 return expansion_notify (symtab);
4210 }
4211 return true;
4212 }
4213
4214 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4215 matched, to expand corresponding CUs that were marked. IDX is the
4216 index of the symbol name that matched. */
4217
4218 static bool
4219 dw2_expand_marked_cus
4220 (dwarf2_per_objfile *per_objfile, offset_type idx,
4221 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4222 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4223 block_search_flags search_flags,
4224 search_domain kind)
4225 {
4226 offset_type vec_len, vec_idx;
4227 bool global_seen = false;
4228 mapped_index &index = *per_objfile->per_bfd->index_table;
4229
4230 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4231 vec_len = vec[0];
4232 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4233 {
4234 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4235 /* This value is only valid for index versions >= 7. */
4236 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4237 gdb_index_symbol_kind symbol_kind =
4238 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4239 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4240 /* Only check the symbol attributes if they're present.
4241 Indices prior to version 7 don't record them,
4242 and indices >= 7 may elide them for certain symbols
4243 (gold does this). */
4244 int attrs_valid =
4245 (index.version >= 7
4246 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4247
4248 /* Work around gold/15646. */
4249 if (attrs_valid
4250 && !is_static
4251 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4252 {
4253 if (global_seen)
4254 continue;
4255
4256 global_seen = true;
4257 }
4258
4259 /* Only check the symbol's kind if it has one. */
4260 if (attrs_valid)
4261 {
4262 if (is_static)
4263 {
4264 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4265 continue;
4266 }
4267 else
4268 {
4269 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4270 continue;
4271 }
4272
4273 switch (kind)
4274 {
4275 case VARIABLES_DOMAIN:
4276 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4277 continue;
4278 break;
4279 case FUNCTIONS_DOMAIN:
4280 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4281 continue;
4282 break;
4283 case TYPES_DOMAIN:
4284 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4285 continue;
4286 break;
4287 case MODULES_DOMAIN:
4288 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4289 continue;
4290 break;
4291 default:
4292 break;
4293 }
4294 }
4295
4296 /* Don't crash on bad data. */
4297 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4298 {
4299 complaint (_(".gdb_index entry has bad CU index"
4300 " [in module %s]"), objfile_name (per_objfile->objfile));
4301 continue;
4302 }
4303
4304 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4305 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4306 expansion_notify))
4307 return false;
4308 }
4309
4310 return true;
4311 }
4312
4313 /* If FILE_MATCHER is non-NULL, set all the
4314 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4315 that match FILE_MATCHER. */
4316
4317 static void
4318 dw_expand_symtabs_matching_file_matcher
4319 (dwarf2_per_objfile *per_objfile,
4320 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4321 {
4322 if (file_matcher == NULL)
4323 return;
4324
4325 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4326 htab_eq_pointer,
4327 NULL, xcalloc, xfree));
4328 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4329 htab_eq_pointer,
4330 NULL, xcalloc, xfree));
4331
4332 /* The rule is CUs specify all the files, including those used by
4333 any TU, so there's no need to scan TUs here. */
4334
4335 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4336 {
4337 QUIT;
4338
4339 if (per_cu->is_debug_types)
4340 continue;
4341 per_cu->v.quick->mark = 0;
4342
4343 /* We only need to look at symtabs not already expanded. */
4344 if (per_objfile->symtab_set_p (per_cu.get ()))
4345 continue;
4346
4347 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4348 per_objfile);
4349 if (file_data == NULL)
4350 continue;
4351
4352 if (htab_find (visited_not_found.get (), file_data) != NULL)
4353 continue;
4354 else if (htab_find (visited_found.get (), file_data) != NULL)
4355 {
4356 per_cu->v.quick->mark = 1;
4357 continue;
4358 }
4359
4360 for (int j = 0; j < file_data->num_file_names; ++j)
4361 {
4362 const char *this_real_name;
4363
4364 if (file_matcher (file_data->file_names[j], false))
4365 {
4366 per_cu->v.quick->mark = 1;
4367 break;
4368 }
4369
4370 /* Before we invoke realpath, which can get expensive when many
4371 files are involved, do a quick comparison of the basenames. */
4372 if (!basenames_may_differ
4373 && !file_matcher (lbasename (file_data->file_names[j]),
4374 true))
4375 continue;
4376
4377 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4378 if (file_matcher (this_real_name, false))
4379 {
4380 per_cu->v.quick->mark = 1;
4381 break;
4382 }
4383 }
4384
4385 void **slot = htab_find_slot (per_cu->v.quick->mark
4386 ? visited_found.get ()
4387 : visited_not_found.get (),
4388 file_data, INSERT);
4389 *slot = file_data;
4390 }
4391 }
4392
4393 bool
4394 dwarf2_gdb_index::expand_symtabs_matching
4395 (struct objfile *objfile,
4396 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4397 const lookup_name_info *lookup_name,
4398 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4399 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4400 block_search_flags search_flags,
4401 domain_enum domain,
4402 enum search_domain kind)
4403 {
4404 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4405
4406 /* index_table is NULL if OBJF_READNOW. */
4407 if (!per_objfile->per_bfd->index_table)
4408 return true;
4409
4410 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4411
4412 /* This invariant is documented in quick-functions.h. */
4413 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4414 if (lookup_name == nullptr)
4415 {
4416 for (dwarf2_per_cu_data *per_cu
4417 : all_comp_units_range (per_objfile->per_bfd))
4418 {
4419 QUIT;
4420
4421 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4422 file_matcher,
4423 expansion_notify))
4424 return false;
4425 }
4426 return true;
4427 }
4428
4429 mapped_index &index = *per_objfile->per_bfd->index_table;
4430
4431 bool result
4432 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4433 symbol_matcher,
4434 [&] (offset_type idx)
4435 {
4436 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4437 expansion_notify, search_flags, kind))
4438 return false;
4439 return true;
4440 }, per_objfile);
4441
4442 return result;
4443 }
4444
4445 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4446 symtab. */
4447
4448 static struct compunit_symtab *
4449 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4450 CORE_ADDR pc)
4451 {
4452 int i;
4453
4454 if (cust->blockvector () != nullptr
4455 && blockvector_contains_pc (cust->blockvector (), pc))
4456 return cust;
4457
4458 if (cust->includes == NULL)
4459 return NULL;
4460
4461 for (i = 0; cust->includes[i]; ++i)
4462 {
4463 struct compunit_symtab *s = cust->includes[i];
4464
4465 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4466 if (s != NULL)
4467 return s;
4468 }
4469
4470 return NULL;
4471 }
4472
4473 struct compunit_symtab *
4474 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4475 (struct objfile *objfile,
4476 struct bound_minimal_symbol msymbol,
4477 CORE_ADDR pc,
4478 struct obj_section *section,
4479 int warn_if_readin)
4480 {
4481 struct dwarf2_per_cu_data *data;
4482 struct compunit_symtab *result;
4483
4484 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4485 if (per_objfile->per_bfd->index_addrmap == nullptr)
4486 return NULL;
4487
4488 CORE_ADDR baseaddr = objfile->text_section_offset ();
4489 data = ((struct dwarf2_per_cu_data *)
4490 addrmap_find (per_objfile->per_bfd->index_addrmap,
4491 pc - baseaddr));
4492 if (!data)
4493 return NULL;
4494
4495 if (warn_if_readin && per_objfile->symtab_set_p (data))
4496 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4497 paddress (objfile->arch (), pc));
4498
4499 result = recursively_find_pc_sect_compunit_symtab
4500 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4501
4502 gdb_assert (result != NULL);
4503 return result;
4504 }
4505
4506 void
4507 dwarf2_base_index_functions::map_symbol_filenames
4508 (struct objfile *objfile,
4509 gdb::function_view<symbol_filename_ftype> fun,
4510 bool need_fullname)
4511 {
4512 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4513
4514 /* Use caches to ensure we only call FUN once for each filename. */
4515 filename_seen_cache filenames_cache;
4516 std::unordered_set<quick_file_names *> qfn_cache;
4517
4518 /* The rule is CUs specify all the files, including those used by any TU,
4519 so there's no need to scan TUs here. We can ignore file names coming
4520 from already-expanded CUs. It is possible that an expanded CU might
4521 reuse the file names data from a currently unexpanded CU, in this
4522 case we don't want to report the files from the unexpanded CU. */
4523
4524 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4525 {
4526 if (!per_cu->is_debug_types
4527 && per_objfile->symtab_set_p (per_cu.get ()))
4528 {
4529 if (per_cu->v.quick->file_names != nullptr)
4530 qfn_cache.insert (per_cu->v.quick->file_names);
4531 }
4532 }
4533
4534 for (dwarf2_per_cu_data *per_cu
4535 : all_comp_units_range (per_objfile->per_bfd))
4536 {
4537 /* We only need to look at symtabs not already expanded. */
4538 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4539 continue;
4540
4541 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4542 if (file_data == nullptr
4543 || qfn_cache.find (file_data) != qfn_cache.end ())
4544 continue;
4545
4546 for (int j = 0; j < file_data->num_file_names; ++j)
4547 {
4548 const char *filename = file_data->file_names[j];
4549 const char *key = filename;
4550 const char *fullname = nullptr;
4551
4552 if (need_fullname)
4553 {
4554 fullname = dw2_get_real_path (per_objfile, file_data, j);
4555 key = fullname;
4556 }
4557
4558 if (!filenames_cache.seen (key))
4559 fun (filename, fullname);
4560 }
4561 }
4562 }
4563
4564 bool
4565 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4566 {
4567 return true;
4568 }
4569
4570 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4571
4572 bool
4573 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4574 {
4575 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4576
4577 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4578 {
4579 /* Is this already expanded? */
4580 if (per_objfile->symtab_set_p (per_cu.get ()))
4581 continue;
4582
4583 /* It has not yet been expanded. */
4584 return true;
4585 }
4586
4587 return false;
4588 }
4589
4590 /* DWARF-5 debug_names reader. */
4591
4592 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4593 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4594
4595 /* A helper function that reads the .debug_names section in SECTION
4596 and fills in MAP. FILENAME is the name of the file containing the
4597 section; it is used for error reporting.
4598
4599 Returns true if all went well, false otherwise. */
4600
4601 static bool
4602 read_debug_names_from_section (struct objfile *objfile,
4603 const char *filename,
4604 struct dwarf2_section_info *section,
4605 mapped_debug_names &map)
4606 {
4607 if (section->empty ())
4608 return false;
4609
4610 /* Older elfutils strip versions could keep the section in the main
4611 executable while splitting it for the separate debug info file. */
4612 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4613 return false;
4614
4615 section->read (objfile);
4616
4617 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4618
4619 const gdb_byte *addr = section->buffer;
4620
4621 bfd *const abfd = section->get_bfd_owner ();
4622
4623 unsigned int bytes_read;
4624 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4625 addr += bytes_read;
4626
4627 map.dwarf5_is_dwarf64 = bytes_read != 4;
4628 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4629 if (bytes_read + length != section->size)
4630 {
4631 /* There may be multiple per-CU indices. */
4632 warning (_("Section .debug_names in %s length %s does not match "
4633 "section length %s, ignoring .debug_names."),
4634 filename, plongest (bytes_read + length),
4635 pulongest (section->size));
4636 return false;
4637 }
4638
4639 /* The version number. */
4640 uint16_t version = read_2_bytes (abfd, addr);
4641 addr += 2;
4642 if (version != 5)
4643 {
4644 warning (_("Section .debug_names in %s has unsupported version %d, "
4645 "ignoring .debug_names."),
4646 filename, version);
4647 return false;
4648 }
4649
4650 /* Padding. */
4651 uint16_t padding = read_2_bytes (abfd, addr);
4652 addr += 2;
4653 if (padding != 0)
4654 {
4655 warning (_("Section .debug_names in %s has unsupported padding %d, "
4656 "ignoring .debug_names."),
4657 filename, padding);
4658 return false;
4659 }
4660
4661 /* comp_unit_count - The number of CUs in the CU list. */
4662 map.cu_count = read_4_bytes (abfd, addr);
4663 addr += 4;
4664
4665 /* local_type_unit_count - The number of TUs in the local TU
4666 list. */
4667 map.tu_count = read_4_bytes (abfd, addr);
4668 addr += 4;
4669
4670 /* foreign_type_unit_count - The number of TUs in the foreign TU
4671 list. */
4672 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4673 addr += 4;
4674 if (foreign_tu_count != 0)
4675 {
4676 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4677 "ignoring .debug_names."),
4678 filename, static_cast<unsigned long> (foreign_tu_count));
4679 return false;
4680 }
4681
4682 /* bucket_count - The number of hash buckets in the hash lookup
4683 table. */
4684 map.bucket_count = read_4_bytes (abfd, addr);
4685 addr += 4;
4686
4687 /* name_count - The number of unique names in the index. */
4688 map.name_count = read_4_bytes (abfd, addr);
4689 addr += 4;
4690
4691 /* abbrev_table_size - The size in bytes of the abbreviations
4692 table. */
4693 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4694 addr += 4;
4695
4696 /* augmentation_string_size - The size in bytes of the augmentation
4697 string. This value is rounded up to a multiple of 4. */
4698 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4699 addr += 4;
4700 map.augmentation_is_gdb = ((augmentation_string_size
4701 == sizeof (dwarf5_augmentation))
4702 && memcmp (addr, dwarf5_augmentation,
4703 sizeof (dwarf5_augmentation)) == 0);
4704 augmentation_string_size += (-augmentation_string_size) & 3;
4705 addr += augmentation_string_size;
4706
4707 /* List of CUs */
4708 map.cu_table_reordered = addr;
4709 addr += map.cu_count * map.offset_size;
4710
4711 /* List of Local TUs */
4712 map.tu_table_reordered = addr;
4713 addr += map.tu_count * map.offset_size;
4714
4715 /* Hash Lookup Table */
4716 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4717 addr += map.bucket_count * 4;
4718 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4719 addr += map.name_count * 4;
4720
4721 /* Name Table */
4722 map.name_table_string_offs_reordered = addr;
4723 addr += map.name_count * map.offset_size;
4724 map.name_table_entry_offs_reordered = addr;
4725 addr += map.name_count * map.offset_size;
4726
4727 const gdb_byte *abbrev_table_start = addr;
4728 for (;;)
4729 {
4730 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4731 addr += bytes_read;
4732 if (index_num == 0)
4733 break;
4734
4735 const auto insertpair
4736 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4737 if (!insertpair.second)
4738 {
4739 warning (_("Section .debug_names in %s has duplicate index %s, "
4740 "ignoring .debug_names."),
4741 filename, pulongest (index_num));
4742 return false;
4743 }
4744 mapped_debug_names::index_val &indexval = insertpair.first->second;
4745 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4746 addr += bytes_read;
4747
4748 for (;;)
4749 {
4750 mapped_debug_names::index_val::attr attr;
4751 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4752 addr += bytes_read;
4753 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4754 addr += bytes_read;
4755 if (attr.form == DW_FORM_implicit_const)
4756 {
4757 attr.implicit_const = read_signed_leb128 (abfd, addr,
4758 &bytes_read);
4759 addr += bytes_read;
4760 }
4761 if (attr.dw_idx == 0 && attr.form == 0)
4762 break;
4763 indexval.attr_vec.push_back (std::move (attr));
4764 }
4765 }
4766 if (addr != abbrev_table_start + abbrev_table_size)
4767 {
4768 warning (_("Section .debug_names in %s has abbreviation_table "
4769 "of size %s vs. written as %u, ignoring .debug_names."),
4770 filename, plongest (addr - abbrev_table_start),
4771 abbrev_table_size);
4772 return false;
4773 }
4774 map.entry_pool = addr;
4775
4776 return true;
4777 }
4778
4779 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4780 list. */
4781
4782 static void
4783 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4784 const mapped_debug_names &map,
4785 dwarf2_section_info &section,
4786 bool is_dwz)
4787 {
4788 if (!map.augmentation_is_gdb)
4789 {
4790 for (uint32_t i = 0; i < map.cu_count; ++i)
4791 {
4792 sect_offset sect_off
4793 = (sect_offset) (extract_unsigned_integer
4794 (map.cu_table_reordered + i * map.offset_size,
4795 map.offset_size,
4796 map.dwarf5_byte_order));
4797 /* We don't know the length of the CU, because the CU list in a
4798 .debug_names index can be incomplete, so we can't use the start
4799 of the next CU as end of this CU. We create the CUs here with
4800 length 0, and in cutu_reader::cutu_reader we'll fill in the
4801 actual length. */
4802 dwarf2_per_cu_data_up per_cu
4803 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4804 sect_off, 0);
4805 per_bfd->all_comp_units.push_back (std::move (per_cu));
4806 }
4807 return;
4808 }
4809
4810 sect_offset sect_off_prev;
4811 for (uint32_t i = 0; i <= map.cu_count; ++i)
4812 {
4813 sect_offset sect_off_next;
4814 if (i < map.cu_count)
4815 {
4816 sect_off_next
4817 = (sect_offset) (extract_unsigned_integer
4818 (map.cu_table_reordered + i * map.offset_size,
4819 map.offset_size,
4820 map.dwarf5_byte_order));
4821 }
4822 else
4823 sect_off_next = (sect_offset) section.size;
4824 if (i >= 1)
4825 {
4826 const ULONGEST length = sect_off_next - sect_off_prev;
4827 dwarf2_per_cu_data_up per_cu
4828 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4829 sect_off_prev, length);
4830 per_bfd->all_comp_units.push_back (std::move (per_cu));
4831 }
4832 sect_off_prev = sect_off_next;
4833 }
4834 }
4835
4836 /* Read the CU list from the mapped index, and use it to create all
4837 the CU objects for this dwarf2_per_objfile. */
4838
4839 static void
4840 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4841 const mapped_debug_names &map,
4842 const mapped_debug_names &dwz_map)
4843 {
4844 gdb_assert (per_bfd->all_comp_units.empty ());
4845 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4846
4847 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4848 false /* is_dwz */);
4849
4850 if (dwz_map.cu_count == 0)
4851 return;
4852
4853 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4854 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4855 true /* is_dwz */);
4856 }
4857
4858 /* Read .debug_names. If everything went ok, initialize the "quick"
4859 elements of all the CUs and return true. Otherwise, return false. */
4860
4861 static bool
4862 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4863 {
4864 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4865 mapped_debug_names dwz_map;
4866 struct objfile *objfile = per_objfile->objfile;
4867 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4868
4869 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4870 &per_bfd->debug_names, *map))
4871 return false;
4872
4873 /* Don't use the index if it's empty. */
4874 if (map->name_count == 0)
4875 return false;
4876
4877 /* If there is a .dwz file, read it so we can get its CU list as
4878 well. */
4879 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4880 if (dwz != NULL)
4881 {
4882 if (!read_debug_names_from_section (objfile,
4883 bfd_get_filename (dwz->dwz_bfd.get ()),
4884 &dwz->debug_names, dwz_map))
4885 {
4886 warning (_("could not read '.debug_names' section from %s; skipping"),
4887 bfd_get_filename (dwz->dwz_bfd.get ()));
4888 return false;
4889 }
4890 }
4891
4892 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4893
4894 if (map->tu_count != 0)
4895 {
4896 /* We can only handle a single .debug_types when we have an
4897 index. */
4898 if (per_bfd->types.size () != 1)
4899 return false;
4900
4901 dwarf2_section_info *section = &per_bfd->types[0];
4902
4903 create_signatured_type_table_from_debug_names
4904 (per_objfile, *map, section, &per_bfd->abbrev);
4905 }
4906
4907 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4908
4909 per_bfd->debug_names_table = std::move (map);
4910 per_bfd->using_index = 1;
4911 per_bfd->quick_file_names_table =
4912 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4913
4914 return true;
4915 }
4916
4917 /* Type used to manage iterating over all CUs looking for a symbol for
4918 .debug_names. */
4919
4920 class dw2_debug_names_iterator
4921 {
4922 public:
4923 dw2_debug_names_iterator (const mapped_debug_names &map,
4924 block_search_flags block_index,
4925 domain_enum domain,
4926 const char *name, dwarf2_per_objfile *per_objfile)
4927 : m_map (map), m_block_index (block_index), m_domain (domain),
4928 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4929 m_per_objfile (per_objfile)
4930 {}
4931
4932 dw2_debug_names_iterator (const mapped_debug_names &map,
4933 search_domain search, uint32_t namei,
4934 dwarf2_per_objfile *per_objfile,
4935 domain_enum domain = UNDEF_DOMAIN)
4936 : m_map (map),
4937 m_domain (domain),
4938 m_search (search),
4939 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4940 m_per_objfile (per_objfile)
4941 {}
4942
4943 dw2_debug_names_iterator (const mapped_debug_names &map,
4944 block_search_flags block_index, domain_enum domain,
4945 uint32_t namei, dwarf2_per_objfile *per_objfile)
4946 : m_map (map), m_block_index (block_index), m_domain (domain),
4947 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4948 m_per_objfile (per_objfile)
4949 {}
4950
4951 /* Return the next matching CU or NULL if there are no more. */
4952 dwarf2_per_cu_data *next ();
4953
4954 private:
4955 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4956 const char *name,
4957 dwarf2_per_objfile *per_objfile);
4958 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4959 uint32_t namei,
4960 dwarf2_per_objfile *per_objfile);
4961
4962 /* The internalized form of .debug_names. */
4963 const mapped_debug_names &m_map;
4964
4965 /* Restrict the search to these blocks. */
4966 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4967 | SEARCH_STATIC_BLOCK);
4968
4969 /* The kind of symbol we're looking for. */
4970 const domain_enum m_domain = UNDEF_DOMAIN;
4971 const search_domain m_search = ALL_DOMAIN;
4972
4973 /* The list of CUs from the index entry of the symbol, or NULL if
4974 not found. */
4975 const gdb_byte *m_addr;
4976
4977 dwarf2_per_objfile *m_per_objfile;
4978 };
4979
4980 const char *
4981 mapped_debug_names::namei_to_name
4982 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4983 {
4984 const ULONGEST namei_string_offs
4985 = extract_unsigned_integer ((name_table_string_offs_reordered
4986 + namei * offset_size),
4987 offset_size,
4988 dwarf5_byte_order);
4989 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4990 }
4991
4992 /* Find a slot in .debug_names for the object named NAME. If NAME is
4993 found, return pointer to its pool data. If NAME cannot be found,
4994 return NULL. */
4995
4996 const gdb_byte *
4997 dw2_debug_names_iterator::find_vec_in_debug_names
4998 (const mapped_debug_names &map, const char *name,
4999 dwarf2_per_objfile *per_objfile)
5000 {
5001 int (*cmp) (const char *, const char *);
5002
5003 gdb::unique_xmalloc_ptr<char> without_params;
5004 if (current_language->la_language == language_cplus
5005 || current_language->la_language == language_fortran
5006 || current_language->la_language == language_d)
5007 {
5008 /* NAME is already canonical. Drop any qualifiers as
5009 .debug_names does not contain any. */
5010
5011 if (strchr (name, '(') != NULL)
5012 {
5013 without_params = cp_remove_params (name);
5014 if (without_params != NULL)
5015 name = without_params.get ();
5016 }
5017 }
5018
5019 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5020
5021 const uint32_t full_hash = dwarf5_djb_hash (name);
5022 uint32_t namei
5023 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5024 (map.bucket_table_reordered
5025 + (full_hash % map.bucket_count)), 4,
5026 map.dwarf5_byte_order);
5027 if (namei == 0)
5028 return NULL;
5029 --namei;
5030 if (namei >= map.name_count)
5031 {
5032 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5033 "[in module %s]"),
5034 namei, map.name_count,
5035 objfile_name (per_objfile->objfile));
5036 return NULL;
5037 }
5038
5039 for (;;)
5040 {
5041 const uint32_t namei_full_hash
5042 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5043 (map.hash_table_reordered + namei), 4,
5044 map.dwarf5_byte_order);
5045 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5046 return NULL;
5047
5048 if (full_hash == namei_full_hash)
5049 {
5050 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5051
5052 #if 0 /* An expensive sanity check. */
5053 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5054 {
5055 complaint (_("Wrong .debug_names hash for string at index %u "
5056 "[in module %s]"),
5057 namei, objfile_name (dwarf2_per_objfile->objfile));
5058 return NULL;
5059 }
5060 #endif
5061
5062 if (cmp (namei_string, name) == 0)
5063 {
5064 const ULONGEST namei_entry_offs
5065 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5066 + namei * map.offset_size),
5067 map.offset_size, map.dwarf5_byte_order);
5068 return map.entry_pool + namei_entry_offs;
5069 }
5070 }
5071
5072 ++namei;
5073 if (namei >= map.name_count)
5074 return NULL;
5075 }
5076 }
5077
5078 const gdb_byte *
5079 dw2_debug_names_iterator::find_vec_in_debug_names
5080 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5081 {
5082 if (namei >= map.name_count)
5083 {
5084 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5085 "[in module %s]"),
5086 namei, map.name_count,
5087 objfile_name (per_objfile->objfile));
5088 return NULL;
5089 }
5090
5091 const ULONGEST namei_entry_offs
5092 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5093 + namei * map.offset_size),
5094 map.offset_size, map.dwarf5_byte_order);
5095 return map.entry_pool + namei_entry_offs;
5096 }
5097
5098 /* See dw2_debug_names_iterator. */
5099
5100 dwarf2_per_cu_data *
5101 dw2_debug_names_iterator::next ()
5102 {
5103 if (m_addr == NULL)
5104 return NULL;
5105
5106 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5107 struct objfile *objfile = m_per_objfile->objfile;
5108 bfd *const abfd = objfile->obfd;
5109
5110 again:
5111
5112 unsigned int bytes_read;
5113 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5114 m_addr += bytes_read;
5115 if (abbrev == 0)
5116 return NULL;
5117
5118 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5119 if (indexval_it == m_map.abbrev_map.cend ())
5120 {
5121 complaint (_("Wrong .debug_names undefined abbrev code %s "
5122 "[in module %s]"),
5123 pulongest (abbrev), objfile_name (objfile));
5124 return NULL;
5125 }
5126 const mapped_debug_names::index_val &indexval = indexval_it->second;
5127 enum class symbol_linkage {
5128 unknown,
5129 static_,
5130 extern_,
5131 } symbol_linkage_ = symbol_linkage::unknown;
5132 dwarf2_per_cu_data *per_cu = NULL;
5133 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5134 {
5135 ULONGEST ull;
5136 switch (attr.form)
5137 {
5138 case DW_FORM_implicit_const:
5139 ull = attr.implicit_const;
5140 break;
5141 case DW_FORM_flag_present:
5142 ull = 1;
5143 break;
5144 case DW_FORM_udata:
5145 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5146 m_addr += bytes_read;
5147 break;
5148 case DW_FORM_ref4:
5149 ull = read_4_bytes (abfd, m_addr);
5150 m_addr += 4;
5151 break;
5152 case DW_FORM_ref8:
5153 ull = read_8_bytes (abfd, m_addr);
5154 m_addr += 8;
5155 break;
5156 case DW_FORM_ref_sig8:
5157 ull = read_8_bytes (abfd, m_addr);
5158 m_addr += 8;
5159 break;
5160 default:
5161 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5162 dwarf_form_name (attr.form),
5163 objfile_name (objfile));
5164 return NULL;
5165 }
5166 switch (attr.dw_idx)
5167 {
5168 case DW_IDX_compile_unit:
5169 /* Don't crash on bad data. */
5170 if (ull >= per_bfd->all_comp_units.size ())
5171 {
5172 complaint (_(".debug_names entry has bad CU index %s"
5173 " [in module %s]"),
5174 pulongest (ull),
5175 objfile_name (objfile));
5176 continue;
5177 }
5178 per_cu = per_bfd->get_cu (ull);
5179 break;
5180 case DW_IDX_type_unit:
5181 /* Don't crash on bad data. */
5182 if (ull >= per_bfd->tu_stats.nr_tus)
5183 {
5184 complaint (_(".debug_names entry has bad TU index %s"
5185 " [in module %s]"),
5186 pulongest (ull),
5187 objfile_name (objfile));
5188 continue;
5189 }
5190 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5191 break;
5192 case DW_IDX_die_offset:
5193 /* In a per-CU index (as opposed to a per-module index), index
5194 entries without CU attribute implicitly refer to the single CU. */
5195 if (per_cu == NULL)
5196 per_cu = per_bfd->get_cu (0);
5197 break;
5198 case DW_IDX_GNU_internal:
5199 if (!m_map.augmentation_is_gdb)
5200 break;
5201 symbol_linkage_ = symbol_linkage::static_;
5202 break;
5203 case DW_IDX_GNU_external:
5204 if (!m_map.augmentation_is_gdb)
5205 break;
5206 symbol_linkage_ = symbol_linkage::extern_;
5207 break;
5208 }
5209 }
5210
5211 /* Skip if already read in. */
5212 if (m_per_objfile->symtab_set_p (per_cu))
5213 goto again;
5214
5215 /* Check static vs global. */
5216 if (symbol_linkage_ != symbol_linkage::unknown)
5217 {
5218 if (symbol_linkage_ == symbol_linkage::static_)
5219 {
5220 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5221 goto again;
5222 }
5223 else
5224 {
5225 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5226 goto again;
5227 }
5228 }
5229
5230 /* Match dw2_symtab_iter_next, symbol_kind
5231 and debug_names::psymbol_tag. */
5232 switch (m_domain)
5233 {
5234 case VAR_DOMAIN:
5235 switch (indexval.dwarf_tag)
5236 {
5237 case DW_TAG_variable:
5238 case DW_TAG_subprogram:
5239 /* Some types are also in VAR_DOMAIN. */
5240 case DW_TAG_typedef:
5241 case DW_TAG_structure_type:
5242 break;
5243 default:
5244 goto again;
5245 }
5246 break;
5247 case STRUCT_DOMAIN:
5248 switch (indexval.dwarf_tag)
5249 {
5250 case DW_TAG_typedef:
5251 case DW_TAG_structure_type:
5252 break;
5253 default:
5254 goto again;
5255 }
5256 break;
5257 case LABEL_DOMAIN:
5258 switch (indexval.dwarf_tag)
5259 {
5260 case 0:
5261 case DW_TAG_variable:
5262 break;
5263 default:
5264 goto again;
5265 }
5266 break;
5267 case MODULE_DOMAIN:
5268 switch (indexval.dwarf_tag)
5269 {
5270 case DW_TAG_module:
5271 break;
5272 default:
5273 goto again;
5274 }
5275 break;
5276 default:
5277 break;
5278 }
5279
5280 /* Match dw2_expand_symtabs_matching, symbol_kind and
5281 debug_names::psymbol_tag. */
5282 switch (m_search)
5283 {
5284 case VARIABLES_DOMAIN:
5285 switch (indexval.dwarf_tag)
5286 {
5287 case DW_TAG_variable:
5288 break;
5289 default:
5290 goto again;
5291 }
5292 break;
5293 case FUNCTIONS_DOMAIN:
5294 switch (indexval.dwarf_tag)
5295 {
5296 case DW_TAG_subprogram:
5297 break;
5298 default:
5299 goto again;
5300 }
5301 break;
5302 case TYPES_DOMAIN:
5303 switch (indexval.dwarf_tag)
5304 {
5305 case DW_TAG_typedef:
5306 case DW_TAG_structure_type:
5307 break;
5308 default:
5309 goto again;
5310 }
5311 break;
5312 case MODULES_DOMAIN:
5313 switch (indexval.dwarf_tag)
5314 {
5315 case DW_TAG_module:
5316 break;
5317 default:
5318 goto again;
5319 }
5320 default:
5321 break;
5322 }
5323
5324 return per_cu;
5325 }
5326
5327 /* This dumps minimal information about .debug_names. It is called
5328 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5329 uses this to verify that .debug_names has been loaded. */
5330
5331 void
5332 dwarf2_debug_names_index::dump (struct objfile *objfile)
5333 {
5334 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5335
5336 gdb_assert (per_objfile->per_bfd->using_index);
5337 gdb_printf (".debug_names:");
5338 if (per_objfile->per_bfd->debug_names_table)
5339 gdb_printf (" exists\n");
5340 else
5341 gdb_printf (" faked for \"readnow\"\n");
5342 gdb_printf ("\n");
5343 }
5344
5345 void
5346 dwarf2_debug_names_index::expand_matching_symbols
5347 (struct objfile *objfile,
5348 const lookup_name_info &name, domain_enum domain,
5349 int global,
5350 symbol_compare_ftype *ordered_compare)
5351 {
5352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5353
5354 /* debug_names_table is NULL if OBJF_READNOW. */
5355 if (!per_objfile->per_bfd->debug_names_table)
5356 return;
5357
5358 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5359 const block_search_flags block_flags
5360 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5361
5362 const char *match_name = name.ada ().lookup_name ().c_str ();
5363 auto matcher = [&] (const char *symname)
5364 {
5365 if (ordered_compare == nullptr)
5366 return true;
5367 return ordered_compare (symname, match_name) == 0;
5368 };
5369
5370 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5371 [&] (offset_type namei)
5372 {
5373 /* The name was matched, now expand corresponding CUs that were
5374 marked. */
5375 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5376 per_objfile);
5377
5378 struct dwarf2_per_cu_data *per_cu;
5379 while ((per_cu = iter.next ()) != NULL)
5380 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5381 nullptr);
5382 return true;
5383 }, per_objfile);
5384 }
5385
5386 bool
5387 dwarf2_debug_names_index::expand_symtabs_matching
5388 (struct objfile *objfile,
5389 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5390 const lookup_name_info *lookup_name,
5391 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5392 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5393 block_search_flags search_flags,
5394 domain_enum domain,
5395 enum search_domain kind)
5396 {
5397 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5398
5399 /* debug_names_table is NULL if OBJF_READNOW. */
5400 if (!per_objfile->per_bfd->debug_names_table)
5401 return true;
5402
5403 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5404
5405 /* This invariant is documented in quick-functions.h. */
5406 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5407 if (lookup_name == nullptr)
5408 {
5409 for (dwarf2_per_cu_data *per_cu
5410 : all_comp_units_range (per_objfile->per_bfd))
5411 {
5412 QUIT;
5413
5414 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5415 file_matcher,
5416 expansion_notify))
5417 return false;
5418 }
5419 return true;
5420 }
5421
5422 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5423
5424 bool result
5425 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5426 symbol_matcher,
5427 [&] (offset_type namei)
5428 {
5429 /* The name was matched, now expand corresponding CUs that were
5430 marked. */
5431 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5432
5433 struct dwarf2_per_cu_data *per_cu;
5434 while ((per_cu = iter.next ()) != NULL)
5435 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5436 file_matcher,
5437 expansion_notify))
5438 return false;
5439 return true;
5440 }, per_objfile);
5441
5442 return result;
5443 }
5444
5445 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5446 to either a dwarf2_per_bfd or dwz_file object. */
5447
5448 template <typename T>
5449 static gdb::array_view<const gdb_byte>
5450 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5451 {
5452 dwarf2_section_info *section = &section_owner->gdb_index;
5453
5454 if (section->empty ())
5455 return {};
5456
5457 /* Older elfutils strip versions could keep the section in the main
5458 executable while splitting it for the separate debug info file. */
5459 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5460 return {};
5461
5462 section->read (obj);
5463
5464 /* dwarf2_section_info::size is a bfd_size_type, while
5465 gdb::array_view works with size_t. On 32-bit hosts, with
5466 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5467 is 32-bit. So we need an explicit narrowing conversion here.
5468 This is fine, because it's impossible to allocate or mmap an
5469 array/buffer larger than what size_t can represent. */
5470 return gdb::make_array_view (section->buffer, section->size);
5471 }
5472
5473 /* Lookup the index cache for the contents of the index associated to
5474 DWARF2_OBJ. */
5475
5476 static gdb::array_view<const gdb_byte>
5477 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5478 {
5479 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5480 if (build_id == nullptr)
5481 return {};
5482
5483 return global_index_cache.lookup_gdb_index (build_id,
5484 &dwarf2_per_bfd->index_cache_res);
5485 }
5486
5487 /* Same as the above, but for DWZ. */
5488
5489 static gdb::array_view<const gdb_byte>
5490 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5491 {
5492 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5493 if (build_id == nullptr)
5494 return {};
5495
5496 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5497 }
5498
5499 /* See dwarf2/public.h. */
5500
5501 void
5502 dwarf2_initialize_objfile (struct objfile *objfile)
5503 {
5504 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5505 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5506
5507 dwarf_read_debug_printf ("called");
5508
5509 /* If we're about to read full symbols, don't bother with the
5510 indices. In this case we also don't care if some other debug
5511 format is making psymtabs, because they are all about to be
5512 expanded anyway. */
5513 if ((objfile->flags & OBJF_READNOW))
5514 {
5515 dwarf_read_debug_printf ("readnow requested");
5516
5517 /* When using READNOW, the using_index flag (set below) indicates that
5518 PER_BFD was already initialized, when we loaded some other objfile. */
5519 if (per_bfd->using_index)
5520 {
5521 dwarf_read_debug_printf ("using_index already set");
5522 objfile->qf.push_front (make_dwarf_gdb_index ());
5523 return;
5524 }
5525
5526 per_bfd->using_index = 1;
5527 create_all_comp_units (per_objfile);
5528 per_bfd->quick_file_names_table
5529 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5530
5531 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5532 {
5533 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5534
5535 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5536 struct dwarf2_per_cu_quick_data);
5537 }
5538
5539 /* Arrange for gdb to see the "quick" functions. However, these
5540 functions will be no-ops because we will have expanded all
5541 symtabs. */
5542 objfile->qf.push_front (make_dwarf_gdb_index ());
5543 return;
5544 }
5545
5546 /* Was a debug names index already read when we processed an objfile sharing
5547 PER_BFD? */
5548 if (per_bfd->debug_names_table != nullptr)
5549 {
5550 dwarf_read_debug_printf ("re-using shared debug names table");
5551 objfile->qf.push_front (make_dwarf_debug_names ());
5552 return;
5553 }
5554
5555 /* Was a GDB index already read when we processed an objfile sharing
5556 PER_BFD? */
5557 if (per_bfd->index_table != nullptr)
5558 {
5559 dwarf_read_debug_printf ("re-using shared index table");
5560 objfile->qf.push_front (make_dwarf_gdb_index ());
5561 return;
5562 }
5563
5564 /* There might already be partial symtabs built for this BFD. This happens
5565 when loading the same binary twice with the index-cache enabled. If so,
5566 don't try to read an index. The objfile / per_objfile initialization will
5567 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5568 code path. */
5569 if (per_bfd->partial_symtabs != nullptr)
5570 {
5571 dwarf_read_debug_printf ("re-using shared partial symtabs");
5572 objfile->qf.push_front (make_lazy_dwarf_reader ());
5573 return;
5574 }
5575
5576 if (dwarf2_read_debug_names (per_objfile))
5577 {
5578 dwarf_read_debug_printf ("found debug names");
5579 objfile->qf.push_front (make_dwarf_debug_names ());
5580 return;
5581 }
5582
5583 if (dwarf2_read_gdb_index (per_objfile,
5584 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5585 get_gdb_index_contents_from_section<dwz_file>))
5586 {
5587 dwarf_read_debug_printf ("found gdb index from file");
5588 objfile->qf.push_front (make_dwarf_gdb_index ());
5589 return;
5590 }
5591
5592 /* ... otherwise, try to find the index in the index cache. */
5593 if (dwarf2_read_gdb_index (per_objfile,
5594 get_gdb_index_contents_from_cache,
5595 get_gdb_index_contents_from_cache_dwz))
5596 {
5597 dwarf_read_debug_printf ("found gdb index from cache");
5598 global_index_cache.hit ();
5599 objfile->qf.push_front (make_dwarf_gdb_index ());
5600 return;
5601 }
5602
5603 global_index_cache.miss ();
5604 objfile->qf.push_front (make_lazy_dwarf_reader ());
5605 }
5606
5607 \f
5608
5609 /* Build a partial symbol table. */
5610
5611 void
5612 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5613 {
5614 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5615 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5616
5617 if (per_bfd->partial_symtabs != nullptr)
5618 {
5619 /* Partial symbols were already read, so now we can simply
5620 attach them. */
5621 if (psf == nullptr)
5622 {
5623 psf = new psymbol_functions (per_bfd->partial_symtabs);
5624 objfile->qf.emplace_front (psf);
5625 }
5626 else
5627 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5628 return;
5629 }
5630
5631 if (psf == nullptr)
5632 {
5633 psf = new psymbol_functions;
5634 objfile->qf.emplace_front (psf);
5635 }
5636 const std::shared_ptr<psymtab_storage> &partial_symtabs
5637 = psf->get_partial_symtabs ();
5638
5639 /* Set the local reference to partial symtabs, so that we don't try
5640 to read them again if reading another objfile with the same BFD.
5641 If we can't in fact share, this won't make a difference anyway as
5642 the dwarf2_per_bfd object won't be shared. */
5643 per_bfd->partial_symtabs = partial_symtabs;
5644
5645 try
5646 {
5647 /* This isn't really ideal: all the data we allocate on the
5648 objfile's obstack is still uselessly kept around. However,
5649 freeing it seems unsafe. */
5650 psymtab_discarder psymtabs (partial_symtabs.get ());
5651 dwarf2_build_psymtabs_hard (per_objfile);
5652 psymtabs.keep ();
5653
5654 /* (maybe) store an index in the cache. */
5655 global_index_cache.store (per_objfile);
5656 }
5657 catch (const gdb_exception_error &except)
5658 {
5659 exception_print (gdb_stderr, except);
5660 }
5661 }
5662
5663 /* Find the base address of the compilation unit for range lists and
5664 location lists. It will normally be specified by DW_AT_low_pc.
5665 In DWARF-3 draft 4, the base address could be overridden by
5666 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5667 compilation units with discontinuous ranges. */
5668
5669 static void
5670 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5671 {
5672 struct attribute *attr;
5673
5674 cu->base_address.reset ();
5675
5676 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5677 if (attr != nullptr)
5678 cu->base_address = attr->as_address ();
5679 else
5680 {
5681 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5682 if (attr != nullptr)
5683 cu->base_address = attr->as_address ();
5684 }
5685 }
5686
5687 /* Helper function that returns the proper abbrev section for
5688 THIS_CU. */
5689
5690 static struct dwarf2_section_info *
5691 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5692 {
5693 struct dwarf2_section_info *abbrev;
5694 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5695
5696 if (this_cu->is_dwz)
5697 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5698 else
5699 abbrev = &per_bfd->abbrev;
5700
5701 return abbrev;
5702 }
5703
5704 /* Fetch the abbreviation table offset from a comp or type unit header. */
5705
5706 static sect_offset
5707 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5708 struct dwarf2_section_info *section,
5709 sect_offset sect_off)
5710 {
5711 bfd *abfd = section->get_bfd_owner ();
5712 const gdb_byte *info_ptr;
5713 unsigned int initial_length_size, offset_size;
5714 uint16_t version;
5715
5716 section->read (per_objfile->objfile);
5717 info_ptr = section->buffer + to_underlying (sect_off);
5718 read_initial_length (abfd, info_ptr, &initial_length_size);
5719 offset_size = initial_length_size == 4 ? 4 : 8;
5720 info_ptr += initial_length_size;
5721
5722 version = read_2_bytes (abfd, info_ptr);
5723 info_ptr += 2;
5724 if (version >= 5)
5725 {
5726 /* Skip unit type and address size. */
5727 info_ptr += 2;
5728 }
5729
5730 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5731 }
5732
5733 /* A partial symtab that is used only for include files. */
5734 struct dwarf2_include_psymtab : public partial_symtab
5735 {
5736 dwarf2_include_psymtab (const char *filename,
5737 psymtab_storage *partial_symtabs,
5738 objfile_per_bfd_storage *objfile_per_bfd)
5739 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5740 {
5741 }
5742
5743 void read_symtab (struct objfile *objfile) override
5744 {
5745 /* It's an include file, no symbols to read for it.
5746 Everything is in the includer symtab. */
5747
5748 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5749 expansion of the includer psymtab. We use the dependencies[0] field to
5750 model the includer. But if we go the regular route of calling
5751 expand_psymtab here, and having expand_psymtab call expand_dependencies
5752 to expand the includer, we'll only use expand_psymtab on the includer
5753 (making it a non-toplevel psymtab), while if we expand the includer via
5754 another path, we'll use read_symtab (making it a toplevel psymtab).
5755 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5756 psymtab, and trigger read_symtab on the includer here directly. */
5757 includer ()->read_symtab (objfile);
5758 }
5759
5760 void expand_psymtab (struct objfile *objfile) override
5761 {
5762 /* This is not called by read_symtab, and should not be called by any
5763 expand_dependencies. */
5764 gdb_assert (false);
5765 }
5766
5767 bool readin_p (struct objfile *objfile) const override
5768 {
5769 return includer ()->readin_p (objfile);
5770 }
5771
5772 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5773 {
5774 compunit_symtab *cust = includer ()->get_compunit_symtab (objfile);
5775 while (cust != nullptr && cust->user != nullptr)
5776 cust = cust->user;
5777 return cust;
5778 }
5779
5780 private:
5781 partial_symtab *includer () const
5782 {
5783 /* An include psymtab has exactly one dependency: the psymtab that
5784 includes it. */
5785 gdb_assert (this->number_of_dependencies == 1);
5786 return this->dependencies[0];
5787 }
5788 };
5789
5790 /* Allocate a new partial symtab for file named NAME and mark this new
5791 partial symtab as being an include of PST. */
5792
5793 static void
5794 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5795 const char *name,
5796 dwarf2_psymtab *pst,
5797 psymtab_storage *partial_symtabs,
5798 objfile_per_bfd_storage *objfile_per_bfd)
5799 {
5800 dwarf2_include_psymtab *subpst
5801 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5802
5803 if (!IS_ABSOLUTE_PATH (subpst->filename))
5804 subpst->dirname = pst->dirname;
5805
5806 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5807 subpst->dependencies[0] = pst;
5808 subpst->number_of_dependencies = 1;
5809 }
5810
5811 /* Read the Line Number Program data and extract the list of files
5812 included by the source file represented by PST. Build an include
5813 partial symtab for each of these included files. */
5814
5815 static void
5816 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5817 struct die_info *die,
5818 const file_and_directory &fnd,
5819 dwarf2_psymtab *pst)
5820 {
5821 line_header_up lh;
5822 struct attribute *attr;
5823
5824 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5825 if (attr != nullptr && attr->form_is_unsigned ())
5826 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5827 if (lh == NULL)
5828 return; /* No linetable, so no includes. */
5829
5830 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5831 that we pass in the raw text_low here; that is ok because we're
5832 only decoding the line table to make include partial symtabs, and
5833 so the addresses aren't really used. */
5834 dwarf_decode_lines (lh.get (), fnd, cu, pst,
5835 pst->raw_text_low (), 1);
5836 }
5837
5838 static hashval_t
5839 hash_signatured_type (const void *item)
5840 {
5841 const struct signatured_type *sig_type
5842 = (const struct signatured_type *) item;
5843
5844 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5845 return sig_type->signature;
5846 }
5847
5848 static int
5849 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5850 {
5851 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5852 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5853
5854 return lhs->signature == rhs->signature;
5855 }
5856
5857 /* Allocate a hash table for signatured types. */
5858
5859 static htab_up
5860 allocate_signatured_type_table ()
5861 {
5862 return htab_up (htab_create_alloc (41,
5863 hash_signatured_type,
5864 eq_signatured_type,
5865 NULL, xcalloc, xfree));
5866 }
5867
5868 /* A helper for create_debug_types_hash_table. Read types from SECTION
5869 and fill them into TYPES_HTAB. It will process only type units,
5870 therefore DW_UT_type. */
5871
5872 static void
5873 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5874 struct dwo_file *dwo_file,
5875 dwarf2_section_info *section, htab_up &types_htab,
5876 rcuh_kind section_kind)
5877 {
5878 struct objfile *objfile = per_objfile->objfile;
5879 struct dwarf2_section_info *abbrev_section;
5880 bfd *abfd;
5881 const gdb_byte *info_ptr, *end_ptr;
5882
5883 abbrev_section = &dwo_file->sections.abbrev;
5884
5885 dwarf_read_debug_printf ("Reading %s for %s",
5886 section->get_name (),
5887 abbrev_section->get_file_name ());
5888
5889 section->read (objfile);
5890 info_ptr = section->buffer;
5891
5892 if (info_ptr == NULL)
5893 return;
5894
5895 /* We can't set abfd until now because the section may be empty or
5896 not present, in which case the bfd is unknown. */
5897 abfd = section->get_bfd_owner ();
5898
5899 /* We don't use cutu_reader here because we don't need to read
5900 any dies: the signature is in the header. */
5901
5902 end_ptr = info_ptr + section->size;
5903 while (info_ptr < end_ptr)
5904 {
5905 signatured_type_up sig_type;
5906 struct dwo_unit *dwo_tu;
5907 void **slot;
5908 const gdb_byte *ptr = info_ptr;
5909 struct comp_unit_head header;
5910 unsigned int length;
5911
5912 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5913
5914 /* Initialize it due to a false compiler warning. */
5915 header.signature = -1;
5916 header.type_cu_offset_in_tu = (cu_offset) -1;
5917
5918 /* We need to read the type's signature in order to build the hash
5919 table, but we don't need anything else just yet. */
5920
5921 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5922 abbrev_section, ptr, section_kind);
5923
5924 length = header.get_length ();
5925
5926 /* Skip dummy type units. */
5927 if (ptr >= info_ptr + length
5928 || peek_abbrev_code (abfd, ptr) == 0
5929 || (header.unit_type != DW_UT_type
5930 && header.unit_type != DW_UT_split_type))
5931 {
5932 info_ptr += length;
5933 continue;
5934 }
5935
5936 if (types_htab == NULL)
5937 types_htab = allocate_dwo_unit_table ();
5938
5939 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5940 dwo_tu->dwo_file = dwo_file;
5941 dwo_tu->signature = header.signature;
5942 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5943 dwo_tu->section = section;
5944 dwo_tu->sect_off = sect_off;
5945 dwo_tu->length = length;
5946
5947 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5948 gdb_assert (slot != NULL);
5949 if (*slot != NULL)
5950 complaint (_("debug type entry at offset %s is duplicate to"
5951 " the entry at offset %s, signature %s"),
5952 sect_offset_str (sect_off),
5953 sect_offset_str (dwo_tu->sect_off),
5954 hex_string (header.signature));
5955 *slot = dwo_tu;
5956
5957 dwarf_read_debug_printf_v (" offset %s, signature %s",
5958 sect_offset_str (sect_off),
5959 hex_string (header.signature));
5960
5961 info_ptr += length;
5962 }
5963 }
5964
5965 /* Create the hash table of all entries in the .debug_types
5966 (or .debug_types.dwo) section(s).
5967 DWO_FILE is a pointer to the DWO file object.
5968
5969 The result is a pointer to the hash table or NULL if there are no types.
5970
5971 Note: This function processes DWO files only, not DWP files. */
5972
5973 static void
5974 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5975 struct dwo_file *dwo_file,
5976 gdb::array_view<dwarf2_section_info> type_sections,
5977 htab_up &types_htab)
5978 {
5979 for (dwarf2_section_info &section : type_sections)
5980 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5981 rcuh_kind::TYPE);
5982 }
5983
5984 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5985 If SLOT is non-NULL, it is the entry to use in the hash table.
5986 Otherwise we find one. */
5987
5988 static struct signatured_type *
5989 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5990 {
5991 if (per_objfile->per_bfd->all_comp_units.size ()
5992 == per_objfile->per_bfd->all_comp_units.capacity ())
5993 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5994
5995 signatured_type_up sig_type_holder
5996 = per_objfile->per_bfd->allocate_signatured_type (sig);
5997 signatured_type *sig_type = sig_type_holder.get ();
5998
5999 per_objfile->per_bfd->all_comp_units.emplace_back
6000 (sig_type_holder.release ());
6001 if (per_objfile->per_bfd->using_index)
6002 {
6003 sig_type->v.quick =
6004 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6005 struct dwarf2_per_cu_quick_data);
6006 }
6007
6008 if (slot == NULL)
6009 {
6010 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6011 sig_type, INSERT);
6012 }
6013 gdb_assert (*slot == NULL);
6014 *slot = sig_type;
6015 /* The rest of sig_type must be filled in by the caller. */
6016 return sig_type;
6017 }
6018
6019 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6020 Fill in SIG_ENTRY with DWO_ENTRY. */
6021
6022 static void
6023 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6024 struct signatured_type *sig_entry,
6025 struct dwo_unit *dwo_entry)
6026 {
6027 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6028
6029 /* Make sure we're not clobbering something we don't expect to. */
6030 gdb_assert (! sig_entry->queued);
6031 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6032 if (per_bfd->using_index)
6033 {
6034 gdb_assert (sig_entry->v.quick != NULL);
6035 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6036 }
6037 else
6038 gdb_assert (sig_entry->v.psymtab == NULL);
6039 gdb_assert (sig_entry->signature == dwo_entry->signature);
6040 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6041 gdb_assert (sig_entry->type_unit_group == NULL);
6042 gdb_assert (sig_entry->dwo_unit == NULL);
6043
6044 sig_entry->section = dwo_entry->section;
6045 sig_entry->sect_off = dwo_entry->sect_off;
6046 sig_entry->length = dwo_entry->length;
6047 sig_entry->reading_dwo_directly = 1;
6048 sig_entry->per_bfd = per_bfd;
6049 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6050 sig_entry->dwo_unit = dwo_entry;
6051 }
6052
6053 /* Subroutine of lookup_signatured_type.
6054 If we haven't read the TU yet, create the signatured_type data structure
6055 for a TU to be read in directly from a DWO file, bypassing the stub.
6056 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6057 using .gdb_index, then when reading a CU we want to stay in the DWO file
6058 containing that CU. Otherwise we could end up reading several other DWO
6059 files (due to comdat folding) to process the transitive closure of all the
6060 mentioned TUs, and that can be slow. The current DWO file will have every
6061 type signature that it needs.
6062 We only do this for .gdb_index because in the psymtab case we already have
6063 to read all the DWOs to build the type unit groups. */
6064
6065 static struct signatured_type *
6066 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6067 {
6068 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6069 struct dwo_file *dwo_file;
6070 struct dwo_unit find_dwo_entry, *dwo_entry;
6071 void **slot;
6072
6073 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6074
6075 /* If TU skeletons have been removed then we may not have read in any
6076 TUs yet. */
6077 if (per_objfile->per_bfd->signatured_types == NULL)
6078 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6079
6080 /* We only ever need to read in one copy of a signatured type.
6081 Use the global signatured_types array to do our own comdat-folding
6082 of types. If this is the first time we're reading this TU, and
6083 the TU has an entry in .gdb_index, replace the recorded data from
6084 .gdb_index with this TU. */
6085
6086 signatured_type find_sig_entry (sig);
6087 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6088 &find_sig_entry, INSERT);
6089 signatured_type *sig_entry = (struct signatured_type *) *slot;
6090
6091 /* We can get here with the TU already read, *or* in the process of being
6092 read. Don't reassign the global entry to point to this DWO if that's
6093 the case. Also note that if the TU is already being read, it may not
6094 have come from a DWO, the program may be a mix of Fission-compiled
6095 code and non-Fission-compiled code. */
6096
6097 /* Have we already tried to read this TU?
6098 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6099 needn't exist in the global table yet). */
6100 if (sig_entry != NULL && sig_entry->tu_read)
6101 return sig_entry;
6102
6103 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6104 dwo_unit of the TU itself. */
6105 dwo_file = cu->dwo_unit->dwo_file;
6106
6107 /* Ok, this is the first time we're reading this TU. */
6108 if (dwo_file->tus == NULL)
6109 return NULL;
6110 find_dwo_entry.signature = sig;
6111 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6112 &find_dwo_entry);
6113 if (dwo_entry == NULL)
6114 return NULL;
6115
6116 /* If the global table doesn't have an entry for this TU, add one. */
6117 if (sig_entry == NULL)
6118 sig_entry = add_type_unit (per_objfile, sig, slot);
6119
6120 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6121 sig_entry->tu_read = 1;
6122 return sig_entry;
6123 }
6124
6125 /* Subroutine of lookup_signatured_type.
6126 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6127 then try the DWP file. If the TU stub (skeleton) has been removed then
6128 it won't be in .gdb_index. */
6129
6130 static struct signatured_type *
6131 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6132 {
6133 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6134 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6135 struct dwo_unit *dwo_entry;
6136 void **slot;
6137
6138 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6139 gdb_assert (dwp_file != NULL);
6140
6141 /* If TU skeletons have been removed then we may not have read in any
6142 TUs yet. */
6143 if (per_objfile->per_bfd->signatured_types == NULL)
6144 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6145
6146 signatured_type find_sig_entry (sig);
6147 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6148 &find_sig_entry, INSERT);
6149 signatured_type *sig_entry = (struct signatured_type *) *slot;
6150
6151 /* Have we already tried to read this TU?
6152 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6153 needn't exist in the global table yet). */
6154 if (sig_entry != NULL)
6155 return sig_entry;
6156
6157 if (dwp_file->tus == NULL)
6158 return NULL;
6159 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6160 1 /* is_debug_types */);
6161 if (dwo_entry == NULL)
6162 return NULL;
6163
6164 sig_entry = add_type_unit (per_objfile, sig, slot);
6165 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6166
6167 return sig_entry;
6168 }
6169
6170 /* Lookup a signature based type for DW_FORM_ref_sig8.
6171 Returns NULL if signature SIG is not present in the table.
6172 It is up to the caller to complain about this. */
6173
6174 static struct signatured_type *
6175 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6176 {
6177 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6178
6179 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6180 {
6181 /* We're in a DWO/DWP file, and we're using .gdb_index.
6182 These cases require special processing. */
6183 if (get_dwp_file (per_objfile) == NULL)
6184 return lookup_dwo_signatured_type (cu, sig);
6185 else
6186 return lookup_dwp_signatured_type (cu, sig);
6187 }
6188 else
6189 {
6190 if (per_objfile->per_bfd->signatured_types == NULL)
6191 return NULL;
6192 signatured_type find_entry (sig);
6193 return ((struct signatured_type *)
6194 htab_find (per_objfile->per_bfd->signatured_types.get (),
6195 &find_entry));
6196 }
6197 }
6198
6199 /* Low level DIE reading support. */
6200
6201 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6202
6203 static void
6204 init_cu_die_reader (struct die_reader_specs *reader,
6205 struct dwarf2_cu *cu,
6206 struct dwarf2_section_info *section,
6207 struct dwo_file *dwo_file,
6208 struct abbrev_table *abbrev_table)
6209 {
6210 gdb_assert (section->readin && section->buffer != NULL);
6211 reader->abfd = section->get_bfd_owner ();
6212 reader->cu = cu;
6213 reader->dwo_file = dwo_file;
6214 reader->die_section = section;
6215 reader->buffer = section->buffer;
6216 reader->buffer_end = section->buffer + section->size;
6217 reader->abbrev_table = abbrev_table;
6218 }
6219
6220 /* Subroutine of cutu_reader to simplify it.
6221 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6222 There's just a lot of work to do, and cutu_reader is big enough
6223 already.
6224
6225 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6226 from it to the DIE in the DWO. If NULL we are skipping the stub.
6227 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6228 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6229 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6230 STUB_COMP_DIR may be non-NULL.
6231 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6232 are filled in with the info of the DIE from the DWO file.
6233 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6234 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6235 kept around for at least as long as *RESULT_READER.
6236
6237 The result is non-zero if a valid (non-dummy) DIE was found. */
6238
6239 static int
6240 read_cutu_die_from_dwo (dwarf2_cu *cu,
6241 struct dwo_unit *dwo_unit,
6242 struct die_info *stub_comp_unit_die,
6243 const char *stub_comp_dir,
6244 struct die_reader_specs *result_reader,
6245 const gdb_byte **result_info_ptr,
6246 struct die_info **result_comp_unit_die,
6247 abbrev_table_up *result_dwo_abbrev_table)
6248 {
6249 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6250 dwarf2_per_cu_data *per_cu = cu->per_cu;
6251 struct objfile *objfile = per_objfile->objfile;
6252 bfd *abfd;
6253 const gdb_byte *begin_info_ptr, *info_ptr;
6254 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6255 int i,num_extra_attrs;
6256 struct dwarf2_section_info *dwo_abbrev_section;
6257 struct die_info *comp_unit_die;
6258
6259 /* At most one of these may be provided. */
6260 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6261
6262 /* These attributes aren't processed until later:
6263 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6264 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6265 referenced later. However, these attributes are found in the stub
6266 which we won't have later. In order to not impose this complication
6267 on the rest of the code, we read them here and copy them to the
6268 DWO CU/TU die. */
6269
6270 stmt_list = NULL;
6271 low_pc = NULL;
6272 high_pc = NULL;
6273 ranges = NULL;
6274 comp_dir = NULL;
6275
6276 if (stub_comp_unit_die != NULL)
6277 {
6278 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6279 DWO file. */
6280 if (!per_cu->is_debug_types)
6281 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6282 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6283 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6284 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6285 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6286
6287 cu->addr_base = stub_comp_unit_die->addr_base ();
6288
6289 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6290 We need the value before we can process DW_AT_ranges values from the
6291 DWO. */
6292 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6293
6294 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6295 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6296 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6297 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6298 section. */
6299 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6300 }
6301 else if (stub_comp_dir != NULL)
6302 {
6303 /* Reconstruct the comp_dir attribute to simplify the code below. */
6304 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6305 comp_dir->name = DW_AT_comp_dir;
6306 comp_dir->form = DW_FORM_string;
6307 comp_dir->set_string_noncanonical (stub_comp_dir);
6308 }
6309
6310 /* Set up for reading the DWO CU/TU. */
6311 cu->dwo_unit = dwo_unit;
6312 dwarf2_section_info *section = dwo_unit->section;
6313 section->read (objfile);
6314 abfd = section->get_bfd_owner ();
6315 begin_info_ptr = info_ptr = (section->buffer
6316 + to_underlying (dwo_unit->sect_off));
6317 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6318
6319 if (per_cu->is_debug_types)
6320 {
6321 signatured_type *sig_type = (struct signatured_type *) per_cu;
6322
6323 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6324 section, dwo_abbrev_section,
6325 info_ptr, rcuh_kind::TYPE);
6326 /* This is not an assert because it can be caused by bad debug info. */
6327 if (sig_type->signature != cu->header.signature)
6328 {
6329 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6330 " TU at offset %s [in module %s]"),
6331 hex_string (sig_type->signature),
6332 hex_string (cu->header.signature),
6333 sect_offset_str (dwo_unit->sect_off),
6334 bfd_get_filename (abfd));
6335 }
6336 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6337 /* For DWOs coming from DWP files, we don't know the CU length
6338 nor the type's offset in the TU until now. */
6339 dwo_unit->length = cu->header.get_length ();
6340 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6341
6342 /* Establish the type offset that can be used to lookup the type.
6343 For DWO files, we don't know it until now. */
6344 sig_type->type_offset_in_section
6345 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6346 }
6347 else
6348 {
6349 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6350 section, dwo_abbrev_section,
6351 info_ptr, rcuh_kind::COMPILE);
6352 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6353 /* For DWOs coming from DWP files, we don't know the CU length
6354 until now. */
6355 dwo_unit->length = cu->header.get_length ();
6356 }
6357
6358 dwo_abbrev_section->read (objfile);
6359 *result_dwo_abbrev_table
6360 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6361 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6362 result_dwo_abbrev_table->get ());
6363
6364 /* Read in the die, but leave space to copy over the attributes
6365 from the stub. This has the benefit of simplifying the rest of
6366 the code - all the work to maintain the illusion of a single
6367 DW_TAG_{compile,type}_unit DIE is done here. */
6368 num_extra_attrs = ((stmt_list != NULL)
6369 + (low_pc != NULL)
6370 + (high_pc != NULL)
6371 + (ranges != NULL)
6372 + (comp_dir != NULL));
6373 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6374 num_extra_attrs);
6375
6376 /* Copy over the attributes from the stub to the DIE we just read in. */
6377 comp_unit_die = *result_comp_unit_die;
6378 i = comp_unit_die->num_attrs;
6379 if (stmt_list != NULL)
6380 comp_unit_die->attrs[i++] = *stmt_list;
6381 if (low_pc != NULL)
6382 comp_unit_die->attrs[i++] = *low_pc;
6383 if (high_pc != NULL)
6384 comp_unit_die->attrs[i++] = *high_pc;
6385 if (ranges != NULL)
6386 comp_unit_die->attrs[i++] = *ranges;
6387 if (comp_dir != NULL)
6388 comp_unit_die->attrs[i++] = *comp_dir;
6389 comp_unit_die->num_attrs += num_extra_attrs;
6390
6391 if (dwarf_die_debug)
6392 {
6393 gdb_printf (gdb_stdlog,
6394 "Read die from %s@0x%x of %s:\n",
6395 section->get_name (),
6396 (unsigned) (begin_info_ptr - section->buffer),
6397 bfd_get_filename (abfd));
6398 dump_die (comp_unit_die, dwarf_die_debug);
6399 }
6400
6401 /* Skip dummy compilation units. */
6402 if (info_ptr >= begin_info_ptr + dwo_unit->length
6403 || peek_abbrev_code (abfd, info_ptr) == 0)
6404 return 0;
6405
6406 *result_info_ptr = info_ptr;
6407 return 1;
6408 }
6409
6410 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6411 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6412 signature is part of the header. */
6413 static gdb::optional<ULONGEST>
6414 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6415 {
6416 if (cu->header.version >= 5)
6417 return cu->header.signature;
6418 struct attribute *attr;
6419 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6420 if (attr == nullptr || !attr->form_is_unsigned ())
6421 return gdb::optional<ULONGEST> ();
6422 return attr->as_unsigned ();
6423 }
6424
6425 /* Subroutine of cutu_reader to simplify it.
6426 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6427 Returns NULL if the specified DWO unit cannot be found. */
6428
6429 static struct dwo_unit *
6430 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6431 {
6432 dwarf2_per_cu_data *per_cu = cu->per_cu;
6433 struct dwo_unit *dwo_unit;
6434 const char *comp_dir;
6435
6436 gdb_assert (cu != NULL);
6437
6438 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6439 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6440 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6441
6442 if (per_cu->is_debug_types)
6443 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6444 else
6445 {
6446 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6447
6448 if (!signature.has_value ())
6449 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6450 " [in module %s]"),
6451 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6452
6453 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6454 }
6455
6456 return dwo_unit;
6457 }
6458
6459 /* Subroutine of cutu_reader to simplify it.
6460 See it for a description of the parameters.
6461 Read a TU directly from a DWO file, bypassing the stub. */
6462
6463 void
6464 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6465 dwarf2_per_objfile *per_objfile,
6466 dwarf2_cu *existing_cu)
6467 {
6468 struct signatured_type *sig_type;
6469
6470 /* Verify we can do the following downcast, and that we have the
6471 data we need. */
6472 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6473 sig_type = (struct signatured_type *) this_cu;
6474 gdb_assert (sig_type->dwo_unit != NULL);
6475
6476 dwarf2_cu *cu;
6477
6478 if (existing_cu != nullptr)
6479 {
6480 cu = existing_cu;
6481 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6482 /* There's no need to do the rereading_dwo_cu handling that
6483 cutu_reader does since we don't read the stub. */
6484 }
6485 else
6486 {
6487 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6488 in per_objfile yet. */
6489 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6490 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6491 cu = m_new_cu.get ();
6492 }
6493
6494 /* A future optimization, if needed, would be to use an existing
6495 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6496 could share abbrev tables. */
6497
6498 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6499 NULL /* stub_comp_unit_die */,
6500 sig_type->dwo_unit->dwo_file->comp_dir,
6501 this, &info_ptr,
6502 &comp_unit_die,
6503 &m_dwo_abbrev_table) == 0)
6504 {
6505 /* Dummy die. */
6506 dummy_p = true;
6507 }
6508 }
6509
6510 /* Initialize a CU (or TU) and read its DIEs.
6511 If the CU defers to a DWO file, read the DWO file as well.
6512
6513 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6514 Otherwise the table specified in the comp unit header is read in and used.
6515 This is an optimization for when we already have the abbrev table.
6516
6517 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6518 allocated. */
6519
6520 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6521 dwarf2_per_objfile *per_objfile,
6522 struct abbrev_table *abbrev_table,
6523 dwarf2_cu *existing_cu,
6524 bool skip_partial)
6525 : die_reader_specs {},
6526 m_this_cu (this_cu)
6527 {
6528 struct objfile *objfile = per_objfile->objfile;
6529 struct dwarf2_section_info *section = this_cu->section;
6530 bfd *abfd = section->get_bfd_owner ();
6531 const gdb_byte *begin_info_ptr;
6532 struct signatured_type *sig_type = NULL;
6533 struct dwarf2_section_info *abbrev_section;
6534 /* Non-zero if CU currently points to a DWO file and we need to
6535 reread it. When this happens we need to reread the skeleton die
6536 before we can reread the DWO file (this only applies to CUs, not TUs). */
6537 int rereading_dwo_cu = 0;
6538
6539 if (dwarf_die_debug)
6540 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6541 this_cu->is_debug_types ? "type" : "comp",
6542 sect_offset_str (this_cu->sect_off));
6543
6544 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6545 file (instead of going through the stub), short-circuit all of this. */
6546 if (this_cu->reading_dwo_directly)
6547 {
6548 /* Narrow down the scope of possibilities to have to understand. */
6549 gdb_assert (this_cu->is_debug_types);
6550 gdb_assert (abbrev_table == NULL);
6551 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6552 return;
6553 }
6554
6555 /* This is cheap if the section is already read in. */
6556 section->read (objfile);
6557
6558 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6559
6560 abbrev_section = get_abbrev_section_for_cu (this_cu);
6561
6562 dwarf2_cu *cu;
6563
6564 if (existing_cu != nullptr)
6565 {
6566 cu = existing_cu;
6567 /* If this CU is from a DWO file we need to start over, we need to
6568 refetch the attributes from the skeleton CU.
6569 This could be optimized by retrieving those attributes from when we
6570 were here the first time: the previous comp_unit_die was stored in
6571 comp_unit_obstack. But there's no data yet that we need this
6572 optimization. */
6573 if (cu->dwo_unit != NULL)
6574 rereading_dwo_cu = 1;
6575 }
6576 else
6577 {
6578 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6579 in per_objfile yet. */
6580 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6581 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6582 cu = m_new_cu.get ();
6583 }
6584
6585 /* Get the header. */
6586 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6587 {
6588 /* We already have the header, there's no need to read it in again. */
6589 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6590 }
6591 else
6592 {
6593 if (this_cu->is_debug_types)
6594 {
6595 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6596 section, abbrev_section,
6597 info_ptr, rcuh_kind::TYPE);
6598
6599 /* Since per_cu is the first member of struct signatured_type,
6600 we can go from a pointer to one to a pointer to the other. */
6601 sig_type = (struct signatured_type *) this_cu;
6602 gdb_assert (sig_type->signature == cu->header.signature);
6603 gdb_assert (sig_type->type_offset_in_tu
6604 == cu->header.type_cu_offset_in_tu);
6605 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6606
6607 /* LENGTH has not been set yet for type units if we're
6608 using .gdb_index. */
6609 this_cu->length = cu->header.get_length ();
6610
6611 /* Establish the type offset that can be used to lookup the type. */
6612 sig_type->type_offset_in_section =
6613 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6614
6615 this_cu->dwarf_version = cu->header.version;
6616 }
6617 else
6618 {
6619 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6620 section, abbrev_section,
6621 info_ptr,
6622 rcuh_kind::COMPILE);
6623
6624 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6625 if (this_cu->length == 0)
6626 this_cu->length = cu->header.get_length ();
6627 else
6628 gdb_assert (this_cu->length == cu->header.get_length ());
6629 this_cu->dwarf_version = cu->header.version;
6630 }
6631 }
6632
6633 /* Skip dummy compilation units. */
6634 if (info_ptr >= begin_info_ptr + this_cu->length
6635 || peek_abbrev_code (abfd, info_ptr) == 0)
6636 {
6637 dummy_p = true;
6638 return;
6639 }
6640
6641 /* If we don't have them yet, read the abbrevs for this compilation unit.
6642 And if we need to read them now, make sure they're freed when we're
6643 done. */
6644 if (abbrev_table != NULL)
6645 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6646 else
6647 {
6648 abbrev_section->read (objfile);
6649 m_abbrev_table_holder
6650 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6651 abbrev_table = m_abbrev_table_holder.get ();
6652 }
6653
6654 /* Read the top level CU/TU die. */
6655 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6656 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6657
6658 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6659 {
6660 dummy_p = true;
6661 return;
6662 }
6663
6664 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6665 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6666 table from the DWO file and pass the ownership over to us. It will be
6667 referenced from READER, so we must make sure to free it after we're done
6668 with READER.
6669
6670 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6671 DWO CU, that this test will fail (the attribute will not be present). */
6672 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6673 if (dwo_name != nullptr)
6674 {
6675 struct dwo_unit *dwo_unit;
6676 struct die_info *dwo_comp_unit_die;
6677
6678 if (comp_unit_die->has_children)
6679 {
6680 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6681 " has children (offset %s) [in module %s]"),
6682 sect_offset_str (this_cu->sect_off),
6683 bfd_get_filename (abfd));
6684 }
6685 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6686 if (dwo_unit != NULL)
6687 {
6688 if (read_cutu_die_from_dwo (cu, dwo_unit,
6689 comp_unit_die, NULL,
6690 this, &info_ptr,
6691 &dwo_comp_unit_die,
6692 &m_dwo_abbrev_table) == 0)
6693 {
6694 /* Dummy die. */
6695 dummy_p = true;
6696 return;
6697 }
6698 comp_unit_die = dwo_comp_unit_die;
6699 }
6700 else
6701 {
6702 /* Yikes, we couldn't find the rest of the DIE, we only have
6703 the stub. A complaint has already been logged. There's
6704 not much more we can do except pass on the stub DIE to
6705 die_reader_func. We don't want to throw an error on bad
6706 debug info. */
6707 }
6708 }
6709 }
6710
6711 void
6712 cutu_reader::keep ()
6713 {
6714 /* Done, clean up. */
6715 gdb_assert (!dummy_p);
6716 if (m_new_cu != NULL)
6717 {
6718 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6719 now. */
6720 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6721 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6722 }
6723 }
6724
6725 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6726 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6727 assumed to have already done the lookup to find the DWO file).
6728
6729 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6730 THIS_CU->is_debug_types, but nothing else.
6731
6732 We fill in THIS_CU->length.
6733
6734 THIS_CU->cu is always freed when done.
6735 This is done in order to not leave THIS_CU->cu in a state where we have
6736 to care whether it refers to the "main" CU or the DWO CU.
6737
6738 When parent_cu is passed, it is used to provide a default value for
6739 str_offsets_base and addr_base from the parent. */
6740
6741 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6742 dwarf2_per_objfile *per_objfile,
6743 struct dwarf2_cu *parent_cu,
6744 struct dwo_file *dwo_file)
6745 : die_reader_specs {},
6746 m_this_cu (this_cu)
6747 {
6748 struct objfile *objfile = per_objfile->objfile;
6749 struct dwarf2_section_info *section = this_cu->section;
6750 bfd *abfd = section->get_bfd_owner ();
6751 struct dwarf2_section_info *abbrev_section;
6752 const gdb_byte *begin_info_ptr, *info_ptr;
6753
6754 if (dwarf_die_debug)
6755 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6756 this_cu->is_debug_types ? "type" : "comp",
6757 sect_offset_str (this_cu->sect_off));
6758
6759 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6760
6761 abbrev_section = (dwo_file != NULL
6762 ? &dwo_file->sections.abbrev
6763 : get_abbrev_section_for_cu (this_cu));
6764
6765 /* This is cheap if the section is already read in. */
6766 section->read (objfile);
6767
6768 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6769
6770 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6771 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6772 section, abbrev_section, info_ptr,
6773 (this_cu->is_debug_types
6774 ? rcuh_kind::TYPE
6775 : rcuh_kind::COMPILE));
6776
6777 if (parent_cu != nullptr)
6778 {
6779 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6780 m_new_cu->addr_base = parent_cu->addr_base;
6781 }
6782 this_cu->length = m_new_cu->header.get_length ();
6783
6784 /* Skip dummy compilation units. */
6785 if (info_ptr >= begin_info_ptr + this_cu->length
6786 || peek_abbrev_code (abfd, info_ptr) == 0)
6787 {
6788 dummy_p = true;
6789 return;
6790 }
6791
6792 abbrev_section->read (objfile);
6793 m_abbrev_table_holder
6794 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6795
6796 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6797 m_abbrev_table_holder.get ());
6798 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6799 }
6800
6801 \f
6802 /* Type Unit Groups.
6803
6804 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6805 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6806 so that all types coming from the same compilation (.o file) are grouped
6807 together. A future step could be to put the types in the same symtab as
6808 the CU the types ultimately came from. */
6809
6810 static hashval_t
6811 hash_type_unit_group (const void *item)
6812 {
6813 const struct type_unit_group *tu_group
6814 = (const struct type_unit_group *) item;
6815
6816 return hash_stmt_list_entry (&tu_group->hash);
6817 }
6818
6819 static int
6820 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6821 {
6822 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6823 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6824
6825 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6826 }
6827
6828 /* Allocate a hash table for type unit groups. */
6829
6830 static htab_up
6831 allocate_type_unit_groups_table ()
6832 {
6833 return htab_up (htab_create_alloc (3,
6834 hash_type_unit_group,
6835 eq_type_unit_group,
6836 htab_delete_entry<type_unit_group>,
6837 xcalloc, xfree));
6838 }
6839
6840 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6841 partial symtabs. We combine several TUs per psymtab to not let the size
6842 of any one psymtab grow too big. */
6843 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6844 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6845
6846 /* Helper routine for get_type_unit_group.
6847 Create the type_unit_group object used to hold one or more TUs. */
6848
6849 static std::unique_ptr<type_unit_group>
6850 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6851 {
6852 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6853 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6854
6855 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6856 tu_group->per_bfd = per_bfd;
6857
6858 if (per_bfd->using_index)
6859 {
6860 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6861 struct dwarf2_per_cu_quick_data);
6862 }
6863 else
6864 {
6865 unsigned int line_offset = to_underlying (line_offset_struct);
6866 dwarf2_psymtab *pst;
6867 std::string name;
6868
6869 /* Give the symtab a useful name for debug purposes. */
6870 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6871 name = string_printf ("<type_units_%d>",
6872 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6873 else
6874 name = string_printf ("<type_units_at_0x%x>", line_offset);
6875
6876 pst = create_partial_symtab (tu_group.get (), per_objfile,
6877 name.c_str ());
6878 pst->anonymous = true;
6879 }
6880
6881 tu_group->hash.dwo_unit = cu->dwo_unit;
6882 tu_group->hash.line_sect_off = line_offset_struct;
6883
6884 return tu_group;
6885 }
6886
6887 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6888 STMT_LIST is a DW_AT_stmt_list attribute. */
6889
6890 static struct type_unit_group *
6891 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6892 {
6893 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6894 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6895 struct type_unit_group *tu_group;
6896 void **slot;
6897 unsigned int line_offset;
6898 struct type_unit_group type_unit_group_for_lookup;
6899
6900 if (per_objfile->per_bfd->type_unit_groups == NULL)
6901 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6902
6903 /* Do we need to create a new group, or can we use an existing one? */
6904
6905 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6906 {
6907 line_offset = stmt_list->as_unsigned ();
6908 ++tu_stats->nr_symtab_sharers;
6909 }
6910 else
6911 {
6912 /* Ugh, no stmt_list. Rare, but we have to handle it.
6913 We can do various things here like create one group per TU or
6914 spread them over multiple groups to split up the expansion work.
6915 To avoid worst case scenarios (too many groups or too large groups)
6916 we, umm, group them in bunches. */
6917 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6918 | (tu_stats->nr_stmt_less_type_units
6919 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6920 ++tu_stats->nr_stmt_less_type_units;
6921 }
6922
6923 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6924 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6925 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6926 &type_unit_group_for_lookup, INSERT);
6927 if (*slot == nullptr)
6928 {
6929 sect_offset line_offset_struct = (sect_offset) line_offset;
6930 std::unique_ptr<type_unit_group> grp
6931 = create_type_unit_group (cu, line_offset_struct);
6932 *slot = grp.release ();
6933 ++tu_stats->nr_symtabs;
6934 }
6935
6936 tu_group = (struct type_unit_group *) *slot;
6937 gdb_assert (tu_group != nullptr);
6938 return tu_group;
6939 }
6940 \f
6941 /* Partial symbol tables. */
6942
6943 /* Create a psymtab named NAME and assign it to PER_CU.
6944
6945 The caller must fill in the following details:
6946 dirname, textlow, texthigh. */
6947
6948 static dwarf2_psymtab *
6949 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6950 dwarf2_per_objfile *per_objfile,
6951 const char *name)
6952 {
6953 dwarf2_psymtab *pst
6954 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6955 per_objfile->objfile->per_bfd, per_cu);
6956
6957 pst->psymtabs_addrmap_supported = true;
6958
6959 /* This is the glue that links PST into GDB's symbol API. */
6960 per_cu->v.psymtab = pst;
6961
6962 return pst;
6963 }
6964
6965 /* DIE reader function for process_psymtab_comp_unit. */
6966
6967 static void
6968 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6969 const gdb_byte *info_ptr,
6970 struct die_info *comp_unit_die,
6971 enum language pretend_language)
6972 {
6973 struct dwarf2_cu *cu = reader->cu;
6974 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6975 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6976 struct objfile *objfile = per_objfile->objfile;
6977 struct gdbarch *gdbarch = objfile->arch ();
6978 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6979 CORE_ADDR baseaddr;
6980 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6981 dwarf2_psymtab *pst;
6982 enum pc_bounds_kind cu_bounds_kind;
6983
6984 gdb_assert (! per_cu->is_debug_types);
6985
6986 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
6987
6988 /* Allocate a new partial symbol table structure. */
6989 static const char artificial[] = "<artificial>";
6990 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
6991 if (strcmp (fnd.get_name (), artificial) == 0)
6992 {
6993 gdb::unique_xmalloc_ptr<char> debug_filename
6994 (concat (artificial, "@",
6995 sect_offset_str (per_cu->sect_off),
6996 (char *) NULL));
6997 fnd.set_name (std::move (debug_filename));
6998 }
6999
7000 pst = create_partial_symtab (per_cu, per_objfile, fnd.get_name ());
7001
7002 /* This must be done before calling dwarf2_build_include_psymtabs. */
7003 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7004
7005 baseaddr = objfile->text_section_offset ();
7006
7007 dwarf2_find_base_address (comp_unit_die, cu);
7008
7009 /* Possibly set the default values of LOWPC and HIGHPC from
7010 `DW_AT_ranges'. */
7011 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7012 &best_highpc, cu, pst);
7013 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7014 {
7015 CORE_ADDR low
7016 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7017 - baseaddr);
7018 CORE_ADDR high
7019 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7020 - baseaddr - 1);
7021 /* Store the contiguous range if it is not empty; it can be
7022 empty for CUs with no code. */
7023 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7024 low, high, pst);
7025 }
7026
7027 /* Check if comp unit has_children.
7028 If so, read the rest of the partial symbols from this comp unit.
7029 If not, there's no more debug_info for this comp unit. */
7030 if (comp_unit_die->has_children)
7031 {
7032 struct partial_die_info *first_die;
7033 CORE_ADDR lowpc, highpc;
7034
7035 lowpc = ((CORE_ADDR) -1);
7036 highpc = ((CORE_ADDR) 0);
7037
7038 first_die = load_partial_dies (reader, info_ptr, 1);
7039
7040 scan_partial_symbols (first_die, &lowpc, &highpc,
7041 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7042
7043 /* If we didn't find a lowpc, set it to highpc to avoid
7044 complaints from `maint check'. */
7045 if (lowpc == ((CORE_ADDR) -1))
7046 lowpc = highpc;
7047
7048 /* If the compilation unit didn't have an explicit address range,
7049 then use the information extracted from its child dies. */
7050 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7051 {
7052 best_lowpc = lowpc;
7053 best_highpc = highpc;
7054 }
7055 }
7056 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7057 best_lowpc + baseaddr)
7058 - baseaddr);
7059 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7060 best_highpc + baseaddr)
7061 - baseaddr);
7062
7063 pst->end ();
7064
7065 if (!cu->per_cu->imported_symtabs_empty ())
7066 {
7067 int i;
7068 int len = cu->per_cu->imported_symtabs_size ();
7069
7070 /* Fill in 'dependencies' here; we fill in 'users' in a
7071 post-pass. */
7072 pst->number_of_dependencies = len;
7073 pst->dependencies
7074 = per_bfd->partial_symtabs->allocate_dependencies (len);
7075 for (i = 0; i < len; ++i)
7076 {
7077 pst->dependencies[i]
7078 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7079 }
7080
7081 cu->per_cu->imported_symtabs_free ();
7082 }
7083
7084 /* Get the list of files included in the current compilation unit,
7085 and build a psymtab for each of them. */
7086 dwarf2_build_include_psymtabs (cu, comp_unit_die, fnd, pst);
7087
7088 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7089 ", %d global, %d static syms",
7090 per_cu->is_debug_types ? "type" : "comp",
7091 sect_offset_str (per_cu->sect_off),
7092 paddress (gdbarch, pst->text_low (objfile)),
7093 paddress (gdbarch, pst->text_high (objfile)),
7094 (int) pst->global_psymbols.size (),
7095 (int) pst->static_psymbols.size ());
7096 }
7097
7098 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7099 Process compilation unit THIS_CU for a psymtab. */
7100
7101 static void
7102 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7103 dwarf2_per_objfile *per_objfile,
7104 bool want_partial_unit,
7105 enum language pretend_language)
7106 {
7107 /* If this compilation unit was already read in, free the
7108 cached copy in order to read it in again. This is
7109 necessary because we skipped some symbols when we first
7110 read in the compilation unit (see load_partial_dies).
7111 This problem could be avoided, but the benefit is unclear. */
7112 per_objfile->remove_cu (this_cu);
7113
7114 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7115
7116 if (reader.comp_unit_die == nullptr)
7117 return;
7118
7119 switch (reader.comp_unit_die->tag)
7120 {
7121 case DW_TAG_compile_unit:
7122 this_cu->unit_type = DW_UT_compile;
7123 break;
7124 case DW_TAG_partial_unit:
7125 this_cu->unit_type = DW_UT_partial;
7126 break;
7127 case DW_TAG_type_unit:
7128 this_cu->unit_type = DW_UT_type;
7129 break;
7130 default:
7131 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7132 dwarf_tag_name (reader.comp_unit_die->tag),
7133 sect_offset_str (reader.cu->per_cu->sect_off),
7134 objfile_name (per_objfile->objfile));
7135 }
7136
7137 if (reader.dummy_p)
7138 {
7139 /* Nothing. */
7140 }
7141 else if (this_cu->is_debug_types)
7142 build_type_psymtabs_reader (&reader, reader.info_ptr,
7143 reader.comp_unit_die);
7144 else if (want_partial_unit
7145 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7146 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7147 reader.comp_unit_die,
7148 pretend_language);
7149
7150 /* Age out any secondary CUs. */
7151 per_objfile->age_comp_units ();
7152 }
7153
7154 /* Reader function for build_type_psymtabs. */
7155
7156 static void
7157 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7158 const gdb_byte *info_ptr,
7159 struct die_info *type_unit_die)
7160 {
7161 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7162 struct dwarf2_cu *cu = reader->cu;
7163 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7164 struct signatured_type *sig_type;
7165 struct type_unit_group *tu_group;
7166 struct attribute *attr;
7167 struct partial_die_info *first_die;
7168 CORE_ADDR lowpc, highpc;
7169 dwarf2_psymtab *pst;
7170
7171 gdb_assert (per_cu->is_debug_types);
7172 sig_type = (struct signatured_type *) per_cu;
7173
7174 if (! type_unit_die->has_children)
7175 return;
7176
7177 attr = type_unit_die->attr (DW_AT_stmt_list);
7178 tu_group = get_type_unit_group (cu, attr);
7179
7180 if (tu_group->tus == nullptr)
7181 tu_group->tus = new std::vector<signatured_type *>;
7182 tu_group->tus->push_back (sig_type);
7183
7184 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7185 pst = create_partial_symtab (per_cu, per_objfile, "");
7186 pst->anonymous = true;
7187
7188 first_die = load_partial_dies (reader, info_ptr, 1);
7189
7190 lowpc = (CORE_ADDR) -1;
7191 highpc = (CORE_ADDR) 0;
7192 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7193
7194 pst->end ();
7195 }
7196
7197 /* Struct used to sort TUs by their abbreviation table offset. */
7198
7199 struct tu_abbrev_offset
7200 {
7201 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7202 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7203 {}
7204
7205 /* This is used when sorting. */
7206 bool operator< (const tu_abbrev_offset &other) const
7207 {
7208 return abbrev_offset < other.abbrev_offset;
7209 }
7210
7211 signatured_type *sig_type;
7212 sect_offset abbrev_offset;
7213 };
7214
7215 /* Efficiently read all the type units.
7216
7217 The efficiency is because we sort TUs by the abbrev table they use and
7218 only read each abbrev table once. In one program there are 200K TUs
7219 sharing 8K abbrev tables.
7220
7221 The main purpose of this function is to support building the
7222 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7223 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7224 can collapse the search space by grouping them by stmt_list.
7225 The savings can be significant, in the same program from above the 200K TUs
7226 share 8K stmt_list tables.
7227
7228 FUNC is expected to call get_type_unit_group, which will create the
7229 struct type_unit_group if necessary and add it to
7230 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7231
7232 static void
7233 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7234 {
7235 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7236 abbrev_table_up abbrev_table;
7237 sect_offset abbrev_offset;
7238
7239 /* It's up to the caller to not call us multiple times. */
7240 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7241
7242 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7243 return;
7244
7245 /* TUs typically share abbrev tables, and there can be way more TUs than
7246 abbrev tables. Sort by abbrev table to reduce the number of times we
7247 read each abbrev table in.
7248 Alternatives are to punt or to maintain a cache of abbrev tables.
7249 This is simpler and efficient enough for now.
7250
7251 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7252 symtab to use). Typically TUs with the same abbrev offset have the same
7253 stmt_list value too so in practice this should work well.
7254
7255 The basic algorithm here is:
7256
7257 sort TUs by abbrev table
7258 for each TU with same abbrev table:
7259 read abbrev table if first user
7260 read TU top level DIE
7261 [IWBN if DWO skeletons had DW_AT_stmt_list]
7262 call FUNC */
7263
7264 dwarf_read_debug_printf ("Building type unit groups ...");
7265
7266 /* Sort in a separate table to maintain the order of all_comp_units
7267 for .gdb_index: TU indices directly index all_type_units. */
7268 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7269 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7270
7271 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7272 {
7273 if (cu->is_debug_types)
7274 {
7275 auto sig_type = static_cast<signatured_type *> (cu.get ());
7276 sorted_by_abbrev.emplace_back
7277 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7278 sig_type->sect_off));
7279 }
7280 }
7281
7282 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7283
7284 abbrev_offset = (sect_offset) ~(unsigned) 0;
7285
7286 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7287 {
7288 /* Switch to the next abbrev table if necessary. */
7289 if (abbrev_table == NULL
7290 || tu.abbrev_offset != abbrev_offset)
7291 {
7292 abbrev_offset = tu.abbrev_offset;
7293 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7294 abbrev_table =
7295 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7296 ++tu_stats->nr_uniq_abbrev_tables;
7297 }
7298
7299 cutu_reader reader (tu.sig_type, per_objfile,
7300 abbrev_table.get (), nullptr, false);
7301 if (!reader.dummy_p)
7302 build_type_psymtabs_reader (&reader, reader.info_ptr,
7303 reader.comp_unit_die);
7304 }
7305 }
7306
7307 /* Print collected type unit statistics. */
7308
7309 static void
7310 print_tu_stats (dwarf2_per_objfile *per_objfile)
7311 {
7312 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7313
7314 dwarf_read_debug_printf ("Type unit statistics:");
7315 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7316 dwarf_read_debug_printf (" %d uniq abbrev tables",
7317 tu_stats->nr_uniq_abbrev_tables);
7318 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7319 tu_stats->nr_symtabs);
7320 dwarf_read_debug_printf (" %d symtab sharers",
7321 tu_stats->nr_symtab_sharers);
7322 dwarf_read_debug_printf (" %d type units without a stmt_list",
7323 tu_stats->nr_stmt_less_type_units);
7324 dwarf_read_debug_printf (" %d all_type_units reallocs",
7325 tu_stats->nr_all_type_units_reallocs);
7326 }
7327
7328 /* Traversal function for build_type_psymtabs. */
7329
7330 static int
7331 build_type_psymtab_dependencies (void **slot, void *info)
7332 {
7333 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7334 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7335 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7336 dwarf2_psymtab *pst = tu_group->v.psymtab;
7337 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7338 int i;
7339
7340 gdb_assert (len > 0);
7341 gdb_assert (tu_group->type_unit_group_p ());
7342
7343 pst->number_of_dependencies = len;
7344 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7345 for (i = 0; i < len; ++i)
7346 {
7347 struct signatured_type *iter = tu_group->tus->at (i);
7348 gdb_assert (iter->is_debug_types);
7349 pst->dependencies[i] = iter->v.psymtab;
7350 iter->type_unit_group = tu_group;
7351 }
7352
7353 delete tu_group->tus;
7354 tu_group->tus = nullptr;
7355
7356 return 1;
7357 }
7358
7359 /* Traversal function for process_skeletonless_type_unit.
7360 Read a TU in a DWO file and build partial symbols for it. */
7361
7362 static int
7363 process_skeletonless_type_unit (void **slot, void *info)
7364 {
7365 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7366 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7367
7368 /* If this TU doesn't exist in the global table, add it and read it in. */
7369
7370 if (per_objfile->per_bfd->signatured_types == NULL)
7371 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7372
7373 signatured_type find_entry (dwo_unit->signature);
7374 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7375 &find_entry, INSERT);
7376 /* If we've already seen this type there's nothing to do. What's happening
7377 is we're doing our own version of comdat-folding here. */
7378 if (*slot != NULL)
7379 return 1;
7380
7381 /* This does the job that create_all_comp_units would have done for
7382 this TU. */
7383 signatured_type *entry
7384 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7385 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7386 *slot = entry;
7387
7388 /* This does the job that build_type_psymtabs would have done. */
7389 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7390 if (!reader.dummy_p)
7391 build_type_psymtabs_reader (&reader, reader.info_ptr,
7392 reader.comp_unit_die);
7393
7394 return 1;
7395 }
7396
7397 /* Traversal function for process_skeletonless_type_units. */
7398
7399 static int
7400 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7401 {
7402 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7403
7404 if (dwo_file->tus != NULL)
7405 htab_traverse_noresize (dwo_file->tus.get (),
7406 process_skeletonless_type_unit, info);
7407
7408 return 1;
7409 }
7410
7411 /* Scan all TUs of DWO files, verifying we've processed them.
7412 This is needed in case a TU was emitted without its skeleton.
7413 Note: This can't be done until we know what all the DWO files are. */
7414
7415 static void
7416 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7417 {
7418 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7419 if (get_dwp_file (per_objfile) == NULL
7420 && per_objfile->per_bfd->dwo_files != NULL)
7421 {
7422 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7423 process_dwo_file_for_skeletonless_type_units,
7424 per_objfile);
7425 }
7426 }
7427
7428 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7429
7430 static void
7431 set_partial_user (dwarf2_per_objfile *per_objfile)
7432 {
7433 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7434 {
7435 dwarf2_psymtab *pst = per_cu->v.psymtab;
7436
7437 if (pst == NULL)
7438 continue;
7439
7440 for (int j = 0; j < pst->number_of_dependencies; ++j)
7441 {
7442 /* Set the 'user' field only if it is not already set. */
7443 if (pst->dependencies[j]->user == NULL)
7444 pst->dependencies[j]->user = pst;
7445 }
7446 }
7447 }
7448
7449 /* Build the partial symbol table by doing a quick pass through the
7450 .debug_info and .debug_abbrev sections. */
7451
7452 static void
7453 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7454 {
7455 struct objfile *objfile = per_objfile->objfile;
7456 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7457
7458 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7459 objfile_name (objfile));
7460
7461 scoped_restore restore_reading_psyms
7462 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7463
7464 per_bfd->info.read (objfile);
7465
7466 /* Any cached compilation units will be linked by the per-objfile
7467 read_in_chain. Make sure to free them when we're done. */
7468 free_cached_comp_units freer (per_objfile);
7469
7470 create_all_comp_units (per_objfile);
7471 build_type_psymtabs (per_objfile);
7472
7473 /* Create a temporary address map on a temporary obstack. We later
7474 copy this to the final obstack. */
7475 auto_obstack temp_obstack;
7476
7477 scoped_restore save_psymtabs_addrmap
7478 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7479 addrmap_create_mutable (&temp_obstack));
7480
7481 for (const auto &per_cu : per_bfd->all_comp_units)
7482 {
7483 if (per_cu->v.psymtab != NULL)
7484 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7485 continue;
7486 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7487 language_minimal);
7488 }
7489
7490 /* This has to wait until we read the CUs, we need the list of DWOs. */
7491 process_skeletonless_type_units (per_objfile);
7492
7493 /* Now that all TUs have been processed we can fill in the dependencies. */
7494 if (per_bfd->type_unit_groups != NULL)
7495 {
7496 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7497 build_type_psymtab_dependencies, per_objfile);
7498 }
7499
7500 if (dwarf_read_debug > 0)
7501 print_tu_stats (per_objfile);
7502
7503 set_partial_user (per_objfile);
7504
7505 per_bfd->partial_symtabs->psymtabs_addrmap
7506 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7507 per_bfd->partial_symtabs->obstack ());
7508 /* At this point we want to keep the address map. */
7509 save_psymtabs_addrmap.release ();
7510
7511 dwarf_read_debug_printf ("Done building psymtabs of %s",
7512 objfile_name (objfile));
7513 }
7514
7515 /* Load the partial DIEs for a secondary CU into memory.
7516 This is also used when rereading a primary CU with load_all_dies. */
7517
7518 static void
7519 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7520 dwarf2_per_objfile *per_objfile,
7521 dwarf2_cu *existing_cu)
7522 {
7523 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7524
7525 if (!reader.dummy_p)
7526 {
7527 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7528 language_minimal);
7529
7530 /* Check if comp unit has_children.
7531 If so, read the rest of the partial symbols from this comp unit.
7532 If not, there's no more debug_info for this comp unit. */
7533 if (reader.comp_unit_die->has_children)
7534 load_partial_dies (&reader, reader.info_ptr, 0);
7535
7536 reader.keep ();
7537 }
7538 }
7539
7540 static void
7541 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7542 struct dwarf2_section_info *section,
7543 struct dwarf2_section_info *abbrev_section,
7544 unsigned int is_dwz,
7545 htab_up &types_htab,
7546 rcuh_kind section_kind)
7547 {
7548 const gdb_byte *info_ptr;
7549 struct objfile *objfile = per_objfile->objfile;
7550
7551 dwarf_read_debug_printf ("Reading %s for %s",
7552 section->get_name (),
7553 section->get_file_name ());
7554
7555 section->read (objfile);
7556
7557 info_ptr = section->buffer;
7558
7559 while (info_ptr < section->buffer + section->size)
7560 {
7561 dwarf2_per_cu_data_up this_cu;
7562
7563 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7564
7565 comp_unit_head cu_header;
7566 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7567 abbrev_section, info_ptr,
7568 section_kind);
7569
7570 /* Save the compilation unit for later lookup. */
7571 if (cu_header.unit_type != DW_UT_type)
7572 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7573 else
7574 {
7575 if (types_htab == nullptr)
7576 types_htab = allocate_signatured_type_table ();
7577
7578 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7579 (cu_header.signature);
7580 signatured_type *sig_ptr = sig_type.get ();
7581 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7582 this_cu.reset (sig_type.release ());
7583
7584 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7585 gdb_assert (slot != nullptr);
7586 if (*slot != nullptr)
7587 complaint (_("debug type entry at offset %s is duplicate to"
7588 " the entry at offset %s, signature %s"),
7589 sect_offset_str (sect_off),
7590 sect_offset_str (sig_ptr->sect_off),
7591 hex_string (sig_ptr->signature));
7592 *slot = sig_ptr;
7593 }
7594 this_cu->sect_off = sect_off;
7595 this_cu->length = cu_header.length + cu_header.initial_length_size;
7596 this_cu->is_dwz = is_dwz;
7597 this_cu->section = section;
7598
7599 info_ptr = info_ptr + this_cu->length;
7600 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7601 }
7602 }
7603
7604 /* Create a list of all compilation units in OBJFILE.
7605 This is only done for -readnow and building partial symtabs. */
7606
7607 static void
7608 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7609 {
7610 htab_up types_htab;
7611
7612 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7613 &per_objfile->per_bfd->abbrev, 0,
7614 types_htab, rcuh_kind::COMPILE);
7615 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7616 read_comp_units_from_section (per_objfile, &section,
7617 &per_objfile->per_bfd->abbrev, 0,
7618 types_htab, rcuh_kind::TYPE);
7619
7620 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7621 if (dwz != NULL)
7622 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7623 types_htab, rcuh_kind::COMPILE);
7624
7625 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7626 }
7627
7628 /* Process all loaded DIEs for compilation unit CU, starting at
7629 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7630 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7631 DW_AT_ranges). See the comments of add_partial_subprogram on how
7632 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7633
7634 static void
7635 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7636 CORE_ADDR *highpc, int set_addrmap,
7637 struct dwarf2_cu *cu)
7638 {
7639 struct partial_die_info *pdi;
7640
7641 /* Now, march along the PDI's, descending into ones which have
7642 interesting children but skipping the children of the other ones,
7643 until we reach the end of the compilation unit. */
7644
7645 pdi = first_die;
7646
7647 while (pdi != NULL)
7648 {
7649 pdi->fixup (cu);
7650
7651 /* Anonymous namespaces or modules have no name but have interesting
7652 children, so we need to look at them. Ditto for anonymous
7653 enums. */
7654
7655 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7656 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7657 || pdi->tag == DW_TAG_imported_unit
7658 || pdi->tag == DW_TAG_inlined_subroutine)
7659 {
7660 switch (pdi->tag)
7661 {
7662 case DW_TAG_subprogram:
7663 case DW_TAG_inlined_subroutine:
7664 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7665 if (cu->per_cu->lang == language_cplus)
7666 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7667 set_addrmap, cu);
7668 break;
7669 case DW_TAG_constant:
7670 case DW_TAG_variable:
7671 case DW_TAG_typedef:
7672 case DW_TAG_union_type:
7673 if (!pdi->is_declaration
7674 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7675 {
7676 add_partial_symbol (pdi, cu);
7677 }
7678 break;
7679 case DW_TAG_class_type:
7680 case DW_TAG_interface_type:
7681 case DW_TAG_structure_type:
7682 if (!pdi->is_declaration)
7683 {
7684 add_partial_symbol (pdi, cu);
7685 }
7686 if ((cu->per_cu->lang == language_rust
7687 || cu->per_cu->lang == language_cplus)
7688 && pdi->has_children)
7689 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7690 set_addrmap, cu);
7691 break;
7692 case DW_TAG_enumeration_type:
7693 if (!pdi->is_declaration)
7694 add_partial_enumeration (pdi, cu);
7695 break;
7696 case DW_TAG_base_type:
7697 case DW_TAG_subrange_type:
7698 /* File scope base type definitions are added to the partial
7699 symbol table. */
7700 add_partial_symbol (pdi, cu);
7701 break;
7702 case DW_TAG_namespace:
7703 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7704 break;
7705 case DW_TAG_module:
7706 if (!pdi->is_declaration)
7707 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7708 break;
7709 case DW_TAG_imported_unit:
7710 {
7711 struct dwarf2_per_cu_data *per_cu;
7712
7713 /* For now we don't handle imported units in type units. */
7714 if (cu->per_cu->is_debug_types)
7715 {
7716 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7717 " supported in type units [in module %s]"),
7718 objfile_name (cu->per_objfile->objfile));
7719 }
7720
7721 per_cu = dwarf2_find_containing_comp_unit
7722 (pdi->d.sect_off, pdi->is_dwz,
7723 cu->per_objfile->per_bfd);
7724
7725 /* Go read the partial unit, if needed. */
7726 if (per_cu->v.psymtab == NULL)
7727 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7728 cu->per_cu->lang);
7729
7730 if (pdi->die_parent == nullptr
7731 && per_cu->unit_type == DW_UT_compile
7732 && per_cu->lang == language_cplus)
7733 /* Regard import as hint. See corresponding code in
7734 process_imported_unit_die. */
7735 break;
7736
7737 cu->per_cu->imported_symtabs_push (per_cu);
7738 }
7739 break;
7740 case DW_TAG_imported_declaration:
7741 add_partial_symbol (pdi, cu);
7742 break;
7743 default:
7744 break;
7745 }
7746 }
7747
7748 /* If the die has a sibling, skip to the sibling. */
7749
7750 pdi = pdi->die_sibling;
7751 }
7752 }
7753
7754 /* Functions used to compute the fully scoped name of a partial DIE.
7755
7756 Normally, this is simple. For C++, the parent DIE's fully scoped
7757 name is concatenated with "::" and the partial DIE's name.
7758 Enumerators are an exception; they use the scope of their parent
7759 enumeration type, i.e. the name of the enumeration type is not
7760 prepended to the enumerator.
7761
7762 There are two complexities. One is DW_AT_specification; in this
7763 case "parent" means the parent of the target of the specification,
7764 instead of the direct parent of the DIE. The other is compilers
7765 which do not emit DW_TAG_namespace; in this case we try to guess
7766 the fully qualified name of structure types from their members'
7767 linkage names. This must be done using the DIE's children rather
7768 than the children of any DW_AT_specification target. We only need
7769 to do this for structures at the top level, i.e. if the target of
7770 any DW_AT_specification (if any; otherwise the DIE itself) does not
7771 have a parent. */
7772
7773 /* Compute the scope prefix associated with PDI's parent, in
7774 compilation unit CU. The result will be allocated on CU's
7775 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7776 field. NULL is returned if no prefix is necessary. */
7777 static const char *
7778 partial_die_parent_scope (struct partial_die_info *pdi,
7779 struct dwarf2_cu *cu)
7780 {
7781 const char *grandparent_scope;
7782 struct partial_die_info *parent, *real_pdi;
7783
7784 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7785 then this means the parent of the specification DIE. */
7786
7787 real_pdi = pdi;
7788 while (real_pdi->has_specification)
7789 {
7790 auto res = find_partial_die (real_pdi->spec_offset,
7791 real_pdi->spec_is_dwz, cu);
7792 real_pdi = res.pdi;
7793 cu = res.cu;
7794 }
7795
7796 parent = real_pdi->die_parent;
7797 if (parent == NULL)
7798 return NULL;
7799
7800 if (parent->scope_set)
7801 return parent->scope;
7802
7803 parent->fixup (cu);
7804
7805 grandparent_scope = partial_die_parent_scope (parent, cu);
7806
7807 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7808 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7809 Work around this problem here. */
7810 if (cu->per_cu->lang == language_cplus
7811 && parent->tag == DW_TAG_namespace
7812 && strcmp (parent->name (cu), "::") == 0
7813 && grandparent_scope == NULL)
7814 {
7815 parent->scope = NULL;
7816 parent->scope_set = 1;
7817 return NULL;
7818 }
7819
7820 /* Nested subroutines in Fortran get a prefix. */
7821 if (pdi->tag == DW_TAG_enumerator)
7822 /* Enumerators should not get the name of the enumeration as a prefix. */
7823 parent->scope = grandparent_scope;
7824 else if (parent->tag == DW_TAG_namespace
7825 || parent->tag == DW_TAG_module
7826 || parent->tag == DW_TAG_structure_type
7827 || parent->tag == DW_TAG_class_type
7828 || parent->tag == DW_TAG_interface_type
7829 || parent->tag == DW_TAG_union_type
7830 || parent->tag == DW_TAG_enumeration_type
7831 || (cu->per_cu->lang == language_fortran
7832 && parent->tag == DW_TAG_subprogram
7833 && pdi->tag == DW_TAG_subprogram))
7834 {
7835 if (grandparent_scope == NULL)
7836 parent->scope = parent->name (cu);
7837 else
7838 parent->scope = typename_concat (&cu->comp_unit_obstack,
7839 grandparent_scope,
7840 parent->name (cu), 0, cu);
7841 }
7842 else
7843 {
7844 /* FIXME drow/2004-04-01: What should we be doing with
7845 function-local names? For partial symbols, we should probably be
7846 ignoring them. */
7847 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7848 dwarf_tag_name (parent->tag),
7849 sect_offset_str (pdi->sect_off));
7850 parent->scope = grandparent_scope;
7851 }
7852
7853 parent->scope_set = 1;
7854 return parent->scope;
7855 }
7856
7857 /* Return the fully scoped name associated with PDI, from compilation unit
7858 CU. The result will be allocated with malloc. */
7859
7860 static gdb::unique_xmalloc_ptr<char>
7861 partial_die_full_name (struct partial_die_info *pdi,
7862 struct dwarf2_cu *cu)
7863 {
7864 const char *parent_scope;
7865
7866 /* If this is a template instantiation, we can not work out the
7867 template arguments from partial DIEs. So, unfortunately, we have
7868 to go through the full DIEs. At least any work we do building
7869 types here will be reused if full symbols are loaded later. */
7870 if (pdi->has_template_arguments)
7871 {
7872 pdi->fixup (cu);
7873
7874 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7875 {
7876 struct die_info *die;
7877 struct attribute attr;
7878 struct dwarf2_cu *ref_cu = cu;
7879
7880 /* DW_FORM_ref_addr is using section offset. */
7881 attr.name = (enum dwarf_attribute) 0;
7882 attr.form = DW_FORM_ref_addr;
7883 attr.u.unsnd = to_underlying (pdi->sect_off);
7884 die = follow_die_ref (NULL, &attr, &ref_cu);
7885
7886 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7887 }
7888 }
7889
7890 parent_scope = partial_die_parent_scope (pdi, cu);
7891 if (parent_scope == NULL)
7892 return NULL;
7893 else
7894 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7895 pdi->name (cu),
7896 0, cu));
7897 }
7898
7899 static void
7900 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7901 {
7902 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7903 struct objfile *objfile = per_objfile->objfile;
7904 struct gdbarch *gdbarch = objfile->arch ();
7905 CORE_ADDR addr = 0;
7906 const char *actual_name = NULL;
7907 CORE_ADDR baseaddr;
7908
7909 baseaddr = objfile->text_section_offset ();
7910
7911 gdb::unique_xmalloc_ptr<char> built_actual_name
7912 = partial_die_full_name (pdi, cu);
7913 if (built_actual_name != NULL)
7914 actual_name = built_actual_name.get ();
7915
7916 if (actual_name == NULL)
7917 actual_name = pdi->name (cu);
7918
7919 partial_symbol psymbol;
7920 memset (&psymbol, 0, sizeof (psymbol));
7921 psymbol.ginfo.set_language (cu->per_cu->lang,
7922 &objfile->objfile_obstack);
7923 psymbol.ginfo.set_section_index (-1);
7924
7925 /* The code below indicates that the psymbol should be installed by
7926 setting this. */
7927 gdb::optional<psymbol_placement> where;
7928
7929 switch (pdi->tag)
7930 {
7931 case DW_TAG_inlined_subroutine:
7932 case DW_TAG_subprogram:
7933 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7934 - baseaddr);
7935 if (pdi->is_external
7936 || cu->per_cu->lang == language_ada
7937 || (cu->per_cu->lang == language_fortran
7938 && pdi->die_parent != NULL
7939 && pdi->die_parent->tag == DW_TAG_subprogram))
7940 {
7941 /* Normally, only "external" DIEs are part of the global scope.
7942 But in Ada and Fortran, we want to be able to access nested
7943 procedures globally. So all Ada and Fortran subprograms are
7944 stored in the global scope. */
7945 where = psymbol_placement::GLOBAL;
7946 }
7947 else
7948 where = psymbol_placement::STATIC;
7949
7950 psymbol.domain = VAR_DOMAIN;
7951 psymbol.aclass = LOC_BLOCK;
7952 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7953 psymbol.ginfo.value.address = addr;
7954
7955 if (pdi->main_subprogram && actual_name != NULL)
7956 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
7957 break;
7958 case DW_TAG_constant:
7959 psymbol.domain = VAR_DOMAIN;
7960 psymbol.aclass = LOC_STATIC;
7961 where = (pdi->is_external
7962 ? psymbol_placement::GLOBAL
7963 : psymbol_placement::STATIC);
7964 break;
7965 case DW_TAG_variable:
7966 if (pdi->d.locdesc)
7967 addr = decode_locdesc (pdi->d.locdesc, cu);
7968
7969 if (pdi->d.locdesc
7970 && addr == 0
7971 && !per_objfile->per_bfd->has_section_at_zero)
7972 {
7973 /* A global or static variable may also have been stripped
7974 out by the linker if unused, in which case its address
7975 will be nullified; do not add such variables into partial
7976 symbol table then. */
7977 }
7978 else if (pdi->is_external)
7979 {
7980 /* Global Variable.
7981 Don't enter into the minimal symbol tables as there is
7982 a minimal symbol table entry from the ELF symbols already.
7983 Enter into partial symbol table if it has a location
7984 descriptor or a type.
7985 If the location descriptor is missing, new_symbol will create
7986 a LOC_UNRESOLVED symbol, the address of the variable will then
7987 be determined from the minimal symbol table whenever the variable
7988 is referenced.
7989 The address for the partial symbol table entry is not
7990 used by GDB, but it comes in handy for debugging partial symbol
7991 table building. */
7992
7993 if (pdi->d.locdesc || pdi->has_type)
7994 {
7995 psymbol.domain = VAR_DOMAIN;
7996 psymbol.aclass = LOC_STATIC;
7997 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7998 psymbol.ginfo.value.address = addr;
7999 where = psymbol_placement::GLOBAL;
8000 }
8001 }
8002 else
8003 {
8004 int has_loc = pdi->d.locdesc != NULL;
8005
8006 /* Static Variable. Skip symbols whose value we cannot know (those
8007 without location descriptors or constant values). */
8008 if (!has_loc && !pdi->has_const_value)
8009 return;
8010
8011 psymbol.domain = VAR_DOMAIN;
8012 psymbol.aclass = LOC_STATIC;
8013 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8014 if (has_loc)
8015 psymbol.ginfo.value.address = addr;
8016 where = psymbol_placement::STATIC;
8017 }
8018 break;
8019 case DW_TAG_array_type:
8020 case DW_TAG_typedef:
8021 case DW_TAG_base_type:
8022 case DW_TAG_subrange_type:
8023 psymbol.domain = VAR_DOMAIN;
8024 psymbol.aclass = LOC_TYPEDEF;
8025 where = psymbol_placement::STATIC;
8026 break;
8027 case DW_TAG_imported_declaration:
8028 case DW_TAG_namespace:
8029 psymbol.domain = VAR_DOMAIN;
8030 psymbol.aclass = LOC_TYPEDEF;
8031 where = psymbol_placement::GLOBAL;
8032 break;
8033 case DW_TAG_module:
8034 /* With Fortran 77 there might be a "BLOCK DATA" module
8035 available without any name. If so, we skip the module as it
8036 doesn't bring any value. */
8037 if (actual_name != nullptr)
8038 {
8039 psymbol.domain = MODULE_DOMAIN;
8040 psymbol.aclass = LOC_TYPEDEF;
8041 where = psymbol_placement::GLOBAL;
8042 }
8043 break;
8044 case DW_TAG_class_type:
8045 case DW_TAG_interface_type:
8046 case DW_TAG_structure_type:
8047 case DW_TAG_union_type:
8048 case DW_TAG_enumeration_type:
8049 /* Skip external references. The DWARF standard says in the section
8050 about "Structure, Union, and Class Type Entries": "An incomplete
8051 structure, union or class type is represented by a structure,
8052 union or class entry that does not have a byte size attribute
8053 and that has a DW_AT_declaration attribute." */
8054 if (!pdi->has_byte_size && pdi->is_declaration)
8055 return;
8056
8057 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8058 static vs. global. */
8059 psymbol.domain = STRUCT_DOMAIN;
8060 psymbol.aclass = LOC_TYPEDEF;
8061 where = (cu->per_cu->lang == language_cplus
8062 ? psymbol_placement::GLOBAL
8063 : psymbol_placement::STATIC);
8064 break;
8065 case DW_TAG_enumerator:
8066 psymbol.domain = VAR_DOMAIN;
8067 psymbol.aclass = LOC_CONST;
8068 where = (cu->per_cu->lang == language_cplus
8069 ? psymbol_placement::GLOBAL
8070 : psymbol_placement::STATIC);
8071 break;
8072 default:
8073 break;
8074 }
8075
8076 if (where.has_value ())
8077 {
8078 if (built_actual_name != nullptr)
8079 actual_name = objfile->intern (actual_name);
8080 if (pdi->linkage_name == nullptr
8081 || cu->per_cu->lang == language_ada)
8082 psymbol.ginfo.set_linkage_name (actual_name);
8083 else
8084 {
8085 psymbol.ginfo.set_demangled_name (actual_name,
8086 &objfile->objfile_obstack);
8087 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8088 }
8089 cu->per_cu->v.psymtab->add_psymbol
8090 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8091 objfile);
8092 }
8093 }
8094
8095 /* Read a partial die corresponding to a namespace; also, add a symbol
8096 corresponding to that namespace to the symbol table. NAMESPACE is
8097 the name of the enclosing namespace. */
8098
8099 static void
8100 add_partial_namespace (struct partial_die_info *pdi,
8101 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8102 int set_addrmap, struct dwarf2_cu *cu)
8103 {
8104 /* Add a symbol for the namespace. */
8105
8106 add_partial_symbol (pdi, cu);
8107
8108 /* Now scan partial symbols in that namespace. */
8109
8110 if (pdi->has_children)
8111 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8112 }
8113
8114 /* Read a partial die corresponding to a Fortran module. */
8115
8116 static void
8117 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8118 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8119 {
8120 /* Add a symbol for the namespace. */
8121
8122 add_partial_symbol (pdi, cu);
8123
8124 /* Now scan partial symbols in that module. */
8125
8126 if (pdi->has_children)
8127 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8128 }
8129
8130 static int
8131 dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
8132 dwarf2_psymtab *, dwarf_tag);
8133
8134 /* Read a partial die corresponding to a subprogram or an inlined
8135 subprogram and create a partial symbol for that subprogram.
8136 When the CU language allows it, this routine also defines a partial
8137 symbol for each nested subprogram that this subprogram contains.
8138 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8139 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8140
8141 PDI may also be a lexical block, in which case we simply search
8142 recursively for subprograms defined inside that lexical block.
8143 Again, this is only performed when the CU language allows this
8144 type of definitions. */
8145
8146 static void
8147 add_partial_subprogram (struct partial_die_info *pdi,
8148 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8149 int set_addrmap, struct dwarf2_cu *cu)
8150 {
8151 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8152 {
8153 if (pdi->has_pc_info)
8154 {
8155 if (pdi->lowpc < *lowpc)
8156 *lowpc = pdi->lowpc;
8157 if (pdi->highpc > *highpc)
8158 *highpc = pdi->highpc;
8159 if (set_addrmap)
8160 {
8161 struct objfile *objfile = cu->per_objfile->objfile;
8162 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8163 struct gdbarch *gdbarch = objfile->arch ();
8164 CORE_ADDR baseaddr;
8165 CORE_ADDR this_highpc;
8166 CORE_ADDR this_lowpc;
8167
8168 baseaddr = objfile->text_section_offset ();
8169 this_lowpc
8170 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8171 pdi->lowpc + baseaddr)
8172 - baseaddr);
8173 this_highpc
8174 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8175 pdi->highpc + baseaddr)
8176 - baseaddr);
8177 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8178 this_lowpc, this_highpc - 1,
8179 cu->per_cu->v.psymtab);
8180 }
8181 }
8182
8183 if (pdi->has_range_info
8184 && dwarf2_ranges_read (pdi->ranges_offset, &pdi->lowpc, &pdi->highpc,
8185 cu,
8186 set_addrmap ? cu->per_cu->v.psymtab : nullptr,
8187 pdi->tag))
8188 {
8189 if (pdi->lowpc < *lowpc)
8190 *lowpc = pdi->lowpc;
8191 if (pdi->highpc > *highpc)
8192 *highpc = pdi->highpc;
8193 }
8194
8195 if (pdi->has_pc_info || pdi->has_range_info
8196 || (!pdi->is_external && pdi->may_be_inlined))
8197 {
8198 if (!pdi->is_declaration)
8199 /* Ignore subprogram DIEs that do not have a name, they are
8200 illegal. Do not emit a complaint at this point, we will
8201 do so when we convert this psymtab into a symtab. */
8202 if (pdi->name (cu))
8203 add_partial_symbol (pdi, cu);
8204 }
8205 }
8206
8207 if (! pdi->has_children)
8208 return;
8209
8210 if (cu->per_cu->lang == language_ada
8211 || cu->per_cu->lang == language_fortran)
8212 {
8213 pdi = pdi->die_child;
8214 while (pdi != NULL)
8215 {
8216 pdi->fixup (cu);
8217 if (pdi->tag == DW_TAG_subprogram
8218 || pdi->tag == DW_TAG_inlined_subroutine
8219 || pdi->tag == DW_TAG_lexical_block)
8220 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8221 pdi = pdi->die_sibling;
8222 }
8223 }
8224 }
8225
8226 /* Read a partial die corresponding to an enumeration type. */
8227
8228 static void
8229 add_partial_enumeration (struct partial_die_info *enum_pdi,
8230 struct dwarf2_cu *cu)
8231 {
8232 struct partial_die_info *pdi;
8233
8234 if (enum_pdi->name (cu) != NULL)
8235 add_partial_symbol (enum_pdi, cu);
8236
8237 pdi = enum_pdi->die_child;
8238 while (pdi)
8239 {
8240 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8241 complaint (_("malformed enumerator DIE ignored"));
8242 else
8243 add_partial_symbol (pdi, cu);
8244 pdi = pdi->die_sibling;
8245 }
8246 }
8247
8248 /* Return the initial uleb128 in the die at INFO_PTR. */
8249
8250 static unsigned int
8251 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8252 {
8253 unsigned int bytes_read;
8254
8255 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8256 }
8257
8258 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8259 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8260
8261 Return the corresponding abbrev, or NULL if the number is zero (indicating
8262 an empty DIE). In either case *BYTES_READ will be set to the length of
8263 the initial number. */
8264
8265 static const struct abbrev_info *
8266 peek_die_abbrev (const die_reader_specs &reader,
8267 const gdb_byte *info_ptr, unsigned int *bytes_read)
8268 {
8269 dwarf2_cu *cu = reader.cu;
8270 bfd *abfd = reader.abfd;
8271 unsigned int abbrev_number
8272 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8273
8274 if (abbrev_number == 0)
8275 return NULL;
8276
8277 const abbrev_info *abbrev
8278 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8279 if (!abbrev)
8280 {
8281 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8282 " at offset %s [in module %s]"),
8283 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8284 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8285 }
8286
8287 return abbrev;
8288 }
8289
8290 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8291 Returns a pointer to the end of a series of DIEs, terminated by an empty
8292 DIE. Any children of the skipped DIEs will also be skipped. */
8293
8294 static const gdb_byte *
8295 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8296 {
8297 while (1)
8298 {
8299 unsigned int bytes_read;
8300 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8301 &bytes_read);
8302
8303 if (abbrev == NULL)
8304 return info_ptr + bytes_read;
8305 else
8306 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8307 }
8308 }
8309
8310 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8311 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8312 abbrev corresponding to that skipped uleb128 should be passed in
8313 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8314 children. */
8315
8316 static const gdb_byte *
8317 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8318 const struct abbrev_info *abbrev)
8319 {
8320 unsigned int bytes_read;
8321 struct attribute attr;
8322 bfd *abfd = reader->abfd;
8323 struct dwarf2_cu *cu = reader->cu;
8324 const gdb_byte *buffer = reader->buffer;
8325 const gdb_byte *buffer_end = reader->buffer_end;
8326 unsigned int form, i;
8327
8328 for (i = 0; i < abbrev->num_attrs; i++)
8329 {
8330 /* The only abbrev we care about is DW_AT_sibling. */
8331 if (abbrev->attrs[i].name == DW_AT_sibling)
8332 {
8333 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8334 if (attr.form == DW_FORM_ref_addr)
8335 complaint (_("ignoring absolute DW_AT_sibling"));
8336 else
8337 {
8338 sect_offset off = attr.get_ref_die_offset ();
8339 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8340
8341 if (sibling_ptr < info_ptr)
8342 complaint (_("DW_AT_sibling points backwards"));
8343 else if (sibling_ptr > reader->buffer_end)
8344 reader->die_section->overflow_complaint ();
8345 else
8346 return sibling_ptr;
8347 }
8348 }
8349
8350 /* If it isn't DW_AT_sibling, skip this attribute. */
8351 form = abbrev->attrs[i].form;
8352 skip_attribute:
8353 switch (form)
8354 {
8355 case DW_FORM_ref_addr:
8356 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8357 and later it is offset sized. */
8358 if (cu->header.version == 2)
8359 info_ptr += cu->header.addr_size;
8360 else
8361 info_ptr += cu->header.offset_size;
8362 break;
8363 case DW_FORM_GNU_ref_alt:
8364 info_ptr += cu->header.offset_size;
8365 break;
8366 case DW_FORM_addr:
8367 info_ptr += cu->header.addr_size;
8368 break;
8369 case DW_FORM_data1:
8370 case DW_FORM_ref1:
8371 case DW_FORM_flag:
8372 case DW_FORM_strx1:
8373 info_ptr += 1;
8374 break;
8375 case DW_FORM_flag_present:
8376 case DW_FORM_implicit_const:
8377 break;
8378 case DW_FORM_data2:
8379 case DW_FORM_ref2:
8380 case DW_FORM_strx2:
8381 info_ptr += 2;
8382 break;
8383 case DW_FORM_strx3:
8384 info_ptr += 3;
8385 break;
8386 case DW_FORM_data4:
8387 case DW_FORM_ref4:
8388 case DW_FORM_strx4:
8389 info_ptr += 4;
8390 break;
8391 case DW_FORM_data8:
8392 case DW_FORM_ref8:
8393 case DW_FORM_ref_sig8:
8394 info_ptr += 8;
8395 break;
8396 case DW_FORM_data16:
8397 info_ptr += 16;
8398 break;
8399 case DW_FORM_string:
8400 read_direct_string (abfd, info_ptr, &bytes_read);
8401 info_ptr += bytes_read;
8402 break;
8403 case DW_FORM_sec_offset:
8404 case DW_FORM_strp:
8405 case DW_FORM_GNU_strp_alt:
8406 info_ptr += cu->header.offset_size;
8407 break;
8408 case DW_FORM_exprloc:
8409 case DW_FORM_block:
8410 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8411 info_ptr += bytes_read;
8412 break;
8413 case DW_FORM_block1:
8414 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8415 break;
8416 case DW_FORM_block2:
8417 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8418 break;
8419 case DW_FORM_block4:
8420 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8421 break;
8422 case DW_FORM_addrx:
8423 case DW_FORM_strx:
8424 case DW_FORM_sdata:
8425 case DW_FORM_udata:
8426 case DW_FORM_ref_udata:
8427 case DW_FORM_GNU_addr_index:
8428 case DW_FORM_GNU_str_index:
8429 case DW_FORM_rnglistx:
8430 case DW_FORM_loclistx:
8431 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8432 break;
8433 case DW_FORM_indirect:
8434 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8435 info_ptr += bytes_read;
8436 /* We need to continue parsing from here, so just go back to
8437 the top. */
8438 goto skip_attribute;
8439
8440 default:
8441 error (_("Dwarf Error: Cannot handle %s "
8442 "in DWARF reader [in module %s]"),
8443 dwarf_form_name (form),
8444 bfd_get_filename (abfd));
8445 }
8446 }
8447
8448 if (abbrev->has_children)
8449 return skip_children (reader, info_ptr);
8450 else
8451 return info_ptr;
8452 }
8453
8454 /* Locate ORIG_PDI's sibling.
8455 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8456
8457 static const gdb_byte *
8458 locate_pdi_sibling (const struct die_reader_specs *reader,
8459 struct partial_die_info *orig_pdi,
8460 const gdb_byte *info_ptr)
8461 {
8462 /* Do we know the sibling already? */
8463
8464 if (orig_pdi->sibling)
8465 return orig_pdi->sibling;
8466
8467 /* Are there any children to deal with? */
8468
8469 if (!orig_pdi->has_children)
8470 return info_ptr;
8471
8472 /* Skip the children the long way. */
8473
8474 return skip_children (reader, info_ptr);
8475 }
8476
8477 /* Expand this partial symbol table into a full symbol table. SELF is
8478 not NULL. */
8479
8480 void
8481 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8482 {
8483 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8484
8485 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8486
8487 /* If this psymtab is constructed from a debug-only objfile, the
8488 has_section_at_zero flag will not necessarily be correct. We
8489 can get the correct value for this flag by looking at the data
8490 associated with the (presumably stripped) associated objfile. */
8491 if (objfile->separate_debug_objfile_backlink)
8492 {
8493 dwarf2_per_objfile *per_objfile_backlink
8494 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8495
8496 per_objfile->per_bfd->has_section_at_zero
8497 = per_objfile_backlink->per_bfd->has_section_at_zero;
8498 }
8499
8500 expand_psymtab (objfile);
8501
8502 process_cu_includes (per_objfile);
8503 }
8504 \f
8505 /* Reading in full CUs. */
8506
8507 /* Add PER_CU to the queue. */
8508
8509 static void
8510 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8511 dwarf2_per_objfile *per_objfile,
8512 enum language pretend_language)
8513 {
8514 per_cu->queued = 1;
8515
8516 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8517 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8518 }
8519
8520 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8521
8522 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8523 dependency.
8524
8525 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8526 DIEs, false otherwise.
8527
8528 Explanation: there is an invariant that if a CU is queued for expansion
8529 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8530 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8531 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8532 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8533 invariant is respected.
8534
8535 The caller is therefore not required to load the CU's DIEs (we return false)
8536 if:
8537
8538 - the CU is already expanded, and therefore does not get enqueued
8539 - the CU gets enqueued for expansion, but its DIEs are already loaded
8540
8541 Note that the caller should not use this function's return value as an
8542 indicator of whether the CU's DIEs are loaded right now, it should check
8543 that by calling `dwarf2_per_objfile::get_cu` instead. */
8544
8545 static int
8546 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8547 dwarf2_per_cu_data *per_cu,
8548 dwarf2_per_objfile *per_objfile,
8549 enum language pretend_language)
8550 {
8551 /* We may arrive here during partial symbol reading, if we need full
8552 DIEs to process an unusual case (e.g. template arguments). Do
8553 not queue PER_CU, just tell our caller to load its DIEs. */
8554 if (per_cu->per_bfd->reading_partial_symbols)
8555 {
8556 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8557
8558 if (cu == NULL || cu->dies == NULL)
8559 return 1;
8560 return 0;
8561 }
8562
8563 /* Mark the dependence relation so that we don't flush PER_CU
8564 too early. */
8565 if (dependent_cu != NULL)
8566 dependent_cu->add_dependence (per_cu);
8567
8568 /* If it's already on the queue, we have nothing to do. */
8569 if (per_cu->queued)
8570 {
8571 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8572 loaded. */
8573 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8574
8575 /* If the CU is queued for expansion, it should not already be
8576 expanded. */
8577 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8578
8579 /* The DIEs are already loaded, the caller doesn't need to do it. */
8580 return 0;
8581 }
8582
8583 bool queued = false;
8584 if (!per_objfile->symtab_set_p (per_cu))
8585 {
8586 /* Add it to the queue. */
8587 queue_comp_unit (per_cu, per_objfile, pretend_language);
8588 queued = true;
8589 }
8590
8591 /* If the compilation unit is already loaded, just mark it as
8592 used. */
8593 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8594 if (cu != nullptr)
8595 cu->last_used = 0;
8596
8597 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8598 and the DIEs are not already loaded. */
8599 return queued && cu == nullptr;
8600 }
8601
8602 /* Process the queue. */
8603
8604 static void
8605 process_queue (dwarf2_per_objfile *per_objfile)
8606 {
8607 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8608 objfile_name (per_objfile->objfile));
8609
8610 /* The queue starts out with one item, but following a DIE reference
8611 may load a new CU, adding it to the end of the queue. */
8612 while (!per_objfile->per_bfd->queue->empty ())
8613 {
8614 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8615 dwarf2_per_cu_data *per_cu = item.per_cu;
8616
8617 if (!per_objfile->symtab_set_p (per_cu))
8618 {
8619 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8620
8621 /* Skip dummy CUs. */
8622 if (cu != nullptr)
8623 {
8624 unsigned int debug_print_threshold;
8625 char buf[100];
8626
8627 if (per_cu->is_debug_types)
8628 {
8629 struct signatured_type *sig_type =
8630 (struct signatured_type *) per_cu;
8631
8632 sprintf (buf, "TU %s at offset %s",
8633 hex_string (sig_type->signature),
8634 sect_offset_str (per_cu->sect_off));
8635 /* There can be 100s of TUs.
8636 Only print them in verbose mode. */
8637 debug_print_threshold = 2;
8638 }
8639 else
8640 {
8641 sprintf (buf, "CU at offset %s",
8642 sect_offset_str (per_cu->sect_off));
8643 debug_print_threshold = 1;
8644 }
8645
8646 if (dwarf_read_debug >= debug_print_threshold)
8647 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8648
8649 if (per_cu->is_debug_types)
8650 process_full_type_unit (cu, item.pretend_language);
8651 else
8652 process_full_comp_unit (cu, item.pretend_language);
8653
8654 if (dwarf_read_debug >= debug_print_threshold)
8655 dwarf_read_debug_printf ("Done expanding %s", buf);
8656 }
8657 }
8658
8659 per_cu->queued = 0;
8660 per_objfile->per_bfd->queue->pop ();
8661 }
8662
8663 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8664 objfile_name (per_objfile->objfile));
8665 }
8666
8667 /* Read in full symbols for PST, and anything it depends on. */
8668
8669 void
8670 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8671 {
8672 gdb_assert (!readin_p (objfile));
8673
8674 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8675 free_cached_comp_units freer (per_objfile);
8676 expand_dependencies (objfile);
8677
8678 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8679 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8680 }
8681
8682 /* See psympriv.h. */
8683
8684 bool
8685 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8686 {
8687 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8688 return per_objfile->symtab_set_p (per_cu_data);
8689 }
8690
8691 /* See psympriv.h. */
8692
8693 compunit_symtab *
8694 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8695 {
8696 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8697 return per_objfile->get_symtab (per_cu_data);
8698 }
8699
8700 /* Trivial hash function for die_info: the hash value of a DIE
8701 is its offset in .debug_info for this objfile. */
8702
8703 static hashval_t
8704 die_hash (const void *item)
8705 {
8706 const struct die_info *die = (const struct die_info *) item;
8707
8708 return to_underlying (die->sect_off);
8709 }
8710
8711 /* Trivial comparison function for die_info structures: two DIEs
8712 are equal if they have the same offset. */
8713
8714 static int
8715 die_eq (const void *item_lhs, const void *item_rhs)
8716 {
8717 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8718 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8719
8720 return die_lhs->sect_off == die_rhs->sect_off;
8721 }
8722
8723 /* Load the DIEs associated with PER_CU into memory.
8724
8725 In some cases, the caller, while reading partial symbols, will need to load
8726 the full symbols for the CU for some reason. It will already have a
8727 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8728 rather than creating a new one. */
8729
8730 static void
8731 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8732 dwarf2_per_objfile *per_objfile,
8733 dwarf2_cu *existing_cu,
8734 bool skip_partial,
8735 enum language pretend_language)
8736 {
8737 gdb_assert (! this_cu->is_debug_types);
8738
8739 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8740 if (reader.dummy_p)
8741 return;
8742
8743 struct dwarf2_cu *cu = reader.cu;
8744 const gdb_byte *info_ptr = reader.info_ptr;
8745
8746 gdb_assert (cu->die_hash == NULL);
8747 cu->die_hash =
8748 htab_create_alloc_ex (cu->header.length / 12,
8749 die_hash,
8750 die_eq,
8751 NULL,
8752 &cu->comp_unit_obstack,
8753 hashtab_obstack_allocate,
8754 dummy_obstack_deallocate);
8755
8756 if (reader.comp_unit_die->has_children)
8757 reader.comp_unit_die->child
8758 = read_die_and_siblings (&reader, reader.info_ptr,
8759 &info_ptr, reader.comp_unit_die);
8760 cu->dies = reader.comp_unit_die;
8761 /* comp_unit_die is not stored in die_hash, no need. */
8762
8763 /* We try not to read any attributes in this function, because not
8764 all CUs needed for references have been loaded yet, and symbol
8765 table processing isn't initialized. But we have to set the CU language,
8766 or we won't be able to build types correctly.
8767 Similarly, if we do not read the producer, we can not apply
8768 producer-specific interpretation. */
8769 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8770
8771 reader.keep ();
8772 }
8773
8774 /* Add a DIE to the delayed physname list. */
8775
8776 static void
8777 add_to_method_list (struct type *type, int fnfield_index, int index,
8778 const char *name, struct die_info *die,
8779 struct dwarf2_cu *cu)
8780 {
8781 struct delayed_method_info mi;
8782 mi.type = type;
8783 mi.fnfield_index = fnfield_index;
8784 mi.index = index;
8785 mi.name = name;
8786 mi.die = die;
8787 cu->method_list.push_back (mi);
8788 }
8789
8790 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8791 "const" / "volatile". If so, decrements LEN by the length of the
8792 modifier and return true. Otherwise return false. */
8793
8794 template<size_t N>
8795 static bool
8796 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8797 {
8798 size_t mod_len = sizeof (mod) - 1;
8799 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8800 {
8801 len -= mod_len;
8802 return true;
8803 }
8804 return false;
8805 }
8806
8807 /* Compute the physnames of any methods on the CU's method list.
8808
8809 The computation of method physnames is delayed in order to avoid the
8810 (bad) condition that one of the method's formal parameters is of an as yet
8811 incomplete type. */
8812
8813 static void
8814 compute_delayed_physnames (struct dwarf2_cu *cu)
8815 {
8816 /* Only C++ delays computing physnames. */
8817 if (cu->method_list.empty ())
8818 return;
8819 gdb_assert (cu->per_cu->lang == language_cplus);
8820
8821 for (const delayed_method_info &mi : cu->method_list)
8822 {
8823 const char *physname;
8824 struct fn_fieldlist *fn_flp
8825 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8826 physname = dwarf2_physname (mi.name, mi.die, cu);
8827 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8828 = physname ? physname : "";
8829
8830 /* Since there's no tag to indicate whether a method is a
8831 const/volatile overload, extract that information out of the
8832 demangled name. */
8833 if (physname != NULL)
8834 {
8835 size_t len = strlen (physname);
8836
8837 while (1)
8838 {
8839 if (physname[len] == ')') /* shortcut */
8840 break;
8841 else if (check_modifier (physname, len, " const"))
8842 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8843 else if (check_modifier (physname, len, " volatile"))
8844 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8845 else
8846 break;
8847 }
8848 }
8849 }
8850
8851 /* The list is no longer needed. */
8852 cu->method_list.clear ();
8853 }
8854
8855 /* Go objects should be embedded in a DW_TAG_module DIE,
8856 and it's not clear if/how imported objects will appear.
8857 To keep Go support simple until that's worked out,
8858 go back through what we've read and create something usable.
8859 We could do this while processing each DIE, and feels kinda cleaner,
8860 but that way is more invasive.
8861 This is to, for example, allow the user to type "p var" or "b main"
8862 without having to specify the package name, and allow lookups
8863 of module.object to work in contexts that use the expression
8864 parser. */
8865
8866 static void
8867 fixup_go_packaging (struct dwarf2_cu *cu)
8868 {
8869 gdb::unique_xmalloc_ptr<char> package_name;
8870 struct pending *list;
8871 int i;
8872
8873 for (list = *cu->get_builder ()->get_global_symbols ();
8874 list != NULL;
8875 list = list->next)
8876 {
8877 for (i = 0; i < list->nsyms; ++i)
8878 {
8879 struct symbol *sym = list->symbol[i];
8880
8881 if (sym->language () == language_go
8882 && sym->aclass () == LOC_BLOCK)
8883 {
8884 gdb::unique_xmalloc_ptr<char> this_package_name
8885 (go_symbol_package_name (sym));
8886
8887 if (this_package_name == NULL)
8888 continue;
8889 if (package_name == NULL)
8890 package_name = std::move (this_package_name);
8891 else
8892 {
8893 struct objfile *objfile = cu->per_objfile->objfile;
8894 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8895 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8896 (symbol_symtab (sym) != NULL
8897 ? symtab_to_filename_for_display
8898 (symbol_symtab (sym))
8899 : objfile_name (objfile)),
8900 this_package_name.get (), package_name.get ());
8901 }
8902 }
8903 }
8904 }
8905
8906 if (package_name != NULL)
8907 {
8908 struct objfile *objfile = cu->per_objfile->objfile;
8909 const char *saved_package_name = objfile->intern (package_name.get ());
8910 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8911 saved_package_name);
8912 struct symbol *sym;
8913
8914 sym = new (&objfile->objfile_obstack) symbol;
8915 sym->set_language (language_go, &objfile->objfile_obstack);
8916 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8917 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8918 e.g., "main" finds the "main" module and not C's main(). */
8919 sym->set_domain (STRUCT_DOMAIN);
8920 sym->set_aclass_index (LOC_TYPEDEF);
8921 sym->set_type (type);
8922
8923 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8924 }
8925 }
8926
8927 /* Allocate a fully-qualified name consisting of the two parts on the
8928 obstack. */
8929
8930 static const char *
8931 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8932 {
8933 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8934 }
8935
8936 /* A helper that allocates a variant part to attach to a Rust enum
8937 type. OBSTACK is where the results should be allocated. TYPE is
8938 the type we're processing. DISCRIMINANT_INDEX is the index of the
8939 discriminant. It must be the index of one of the fields of TYPE,
8940 or -1 to mean there is no discriminant (univariant enum).
8941 DEFAULT_INDEX is the index of the default field; or -1 if there is
8942 no default. RANGES is indexed by "effective" field number (the
8943 field index, but omitting the discriminant and default fields) and
8944 must hold the discriminant values used by the variants. Note that
8945 RANGES must have a lifetime at least as long as OBSTACK -- either
8946 already allocated on it, or static. */
8947
8948 static void
8949 alloc_rust_variant (struct obstack *obstack, struct type *type,
8950 int discriminant_index, int default_index,
8951 gdb::array_view<discriminant_range> ranges)
8952 {
8953 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8954 gdb_assert (discriminant_index == -1
8955 || (discriminant_index >= 0
8956 && discriminant_index < type->num_fields ()));
8957 gdb_assert (default_index == -1
8958 || (default_index >= 0 && default_index < type->num_fields ()));
8959
8960 /* We have one variant for each non-discriminant field. */
8961 int n_variants = type->num_fields ();
8962 if (discriminant_index != -1)
8963 --n_variants;
8964
8965 variant *variants = new (obstack) variant[n_variants];
8966 int var_idx = 0;
8967 int range_idx = 0;
8968 for (int i = 0; i < type->num_fields (); ++i)
8969 {
8970 if (i == discriminant_index)
8971 continue;
8972
8973 variants[var_idx].first_field = i;
8974 variants[var_idx].last_field = i + 1;
8975
8976 /* The default field does not need a range, but other fields do.
8977 We skipped the discriminant above. */
8978 if (i != default_index)
8979 {
8980 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8981 ++range_idx;
8982 }
8983
8984 ++var_idx;
8985 }
8986
8987 gdb_assert (range_idx == ranges.size ());
8988 gdb_assert (var_idx == n_variants);
8989
8990 variant_part *part = new (obstack) variant_part;
8991 part->discriminant_index = discriminant_index;
8992 /* If there is no discriminant, then whether it is signed is of no
8993 consequence. */
8994 part->is_unsigned
8995 = (discriminant_index == -1
8996 ? false
8997 : type->field (discriminant_index).type ()->is_unsigned ());
8998 part->variants = gdb::array_view<variant> (variants, n_variants);
8999
9000 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9001 gdb::array_view<variant_part> *prop_value
9002 = new (storage) gdb::array_view<variant_part> (part, 1);
9003
9004 struct dynamic_prop prop;
9005 prop.set_variant_parts (prop_value);
9006
9007 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9008 }
9009
9010 /* Some versions of rustc emitted enums in an unusual way.
9011
9012 Ordinary enums were emitted as unions. The first element of each
9013 structure in the union was named "RUST$ENUM$DISR". This element
9014 held the discriminant.
9015
9016 These versions of Rust also implemented the "non-zero"
9017 optimization. When the enum had two values, and one is empty and
9018 the other holds a pointer that cannot be zero, the pointer is used
9019 as the discriminant, with a zero value meaning the empty variant.
9020 Here, the union's first member is of the form
9021 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9022 where the fieldnos are the indices of the fields that should be
9023 traversed in order to find the field (which may be several fields deep)
9024 and the variantname is the name of the variant of the case when the
9025 field is zero.
9026
9027 This function recognizes whether TYPE is of one of these forms,
9028 and, if so, smashes it to be a variant type. */
9029
9030 static void
9031 quirk_rust_enum (struct type *type, struct objfile *objfile)
9032 {
9033 gdb_assert (type->code () == TYPE_CODE_UNION);
9034
9035 /* We don't need to deal with empty enums. */
9036 if (type->num_fields () == 0)
9037 return;
9038
9039 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9040 if (type->num_fields () == 1
9041 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
9042 {
9043 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
9044
9045 /* Decode the field name to find the offset of the
9046 discriminant. */
9047 ULONGEST bit_offset = 0;
9048 struct type *field_type = type->field (0).type ();
9049 while (name[0] >= '0' && name[0] <= '9')
9050 {
9051 char *tail;
9052 unsigned long index = strtoul (name, &tail, 10);
9053 name = tail;
9054 if (*name != '$'
9055 || index >= field_type->num_fields ()
9056 || (field_type->field (index).loc_kind ()
9057 != FIELD_LOC_KIND_BITPOS))
9058 {
9059 complaint (_("Could not parse Rust enum encoding string \"%s\""
9060 "[in module %s]"),
9061 type->field (0).name (),
9062 objfile_name (objfile));
9063 return;
9064 }
9065 ++name;
9066
9067 bit_offset += field_type->field (index).loc_bitpos ();
9068 field_type = field_type->field (index).type ();
9069 }
9070
9071 /* Smash this type to be a structure type. We have to do this
9072 because the type has already been recorded. */
9073 type->set_code (TYPE_CODE_STRUCT);
9074 type->set_num_fields (3);
9075 /* Save the field we care about. */
9076 struct field saved_field = type->field (0);
9077 type->set_fields
9078 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9079
9080 /* Put the discriminant at index 0. */
9081 type->field (0).set_type (field_type);
9082 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9083 type->field (0).set_name ("<<discriminant>>");
9084 type->field (0).set_loc_bitpos (bit_offset);
9085
9086 /* The order of fields doesn't really matter, so put the real
9087 field at index 1 and the data-less field at index 2. */
9088 type->field (1) = saved_field;
9089 type->field (1).set_name
9090 (rust_last_path_segment (type->field (1).type ()->name ()));
9091 type->field (1).type ()->set_name
9092 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9093 type->field (1).name ()));
9094
9095 const char *dataless_name
9096 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9097 name);
9098 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9099 dataless_name);
9100 type->field (2).set_type (dataless_type);
9101 /* NAME points into the original discriminant name, which
9102 already has the correct lifetime. */
9103 type->field (2).set_name (name);
9104 type->field (2).set_loc_bitpos (0);
9105
9106 /* Indicate that this is a variant type. */
9107 static discriminant_range ranges[1] = { { 0, 0 } };
9108 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9109 }
9110 /* A union with a single anonymous field is probably an old-style
9111 univariant enum. */
9112 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
9113 {
9114 /* Smash this type to be a structure type. We have to do this
9115 because the type has already been recorded. */
9116 type->set_code (TYPE_CODE_STRUCT);
9117
9118 struct type *field_type = type->field (0).type ();
9119 const char *variant_name
9120 = rust_last_path_segment (field_type->name ());
9121 type->field (0).set_name (variant_name);
9122 field_type->set_name
9123 (rust_fully_qualify (&objfile->objfile_obstack,
9124 type->name (), variant_name));
9125
9126 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9127 }
9128 else
9129 {
9130 struct type *disr_type = nullptr;
9131 for (int i = 0; i < type->num_fields (); ++i)
9132 {
9133 disr_type = type->field (i).type ();
9134
9135 if (disr_type->code () != TYPE_CODE_STRUCT)
9136 {
9137 /* All fields of a true enum will be structs. */
9138 return;
9139 }
9140 else if (disr_type->num_fields () == 0)
9141 {
9142 /* Could be data-less variant, so keep going. */
9143 disr_type = nullptr;
9144 }
9145 else if (strcmp (disr_type->field (0).name (),
9146 "RUST$ENUM$DISR") != 0)
9147 {
9148 /* Not a Rust enum. */
9149 return;
9150 }
9151 else
9152 {
9153 /* Found one. */
9154 break;
9155 }
9156 }
9157
9158 /* If we got here without a discriminant, then it's probably
9159 just a union. */
9160 if (disr_type == nullptr)
9161 return;
9162
9163 /* Smash this type to be a structure type. We have to do this
9164 because the type has already been recorded. */
9165 type->set_code (TYPE_CODE_STRUCT);
9166
9167 /* Make space for the discriminant field. */
9168 struct field *disr_field = &disr_type->field (0);
9169 field *new_fields
9170 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9171 * sizeof (struct field)));
9172 memcpy (new_fields + 1, type->fields (),
9173 type->num_fields () * sizeof (struct field));
9174 type->set_fields (new_fields);
9175 type->set_num_fields (type->num_fields () + 1);
9176
9177 /* Install the discriminant at index 0 in the union. */
9178 type->field (0) = *disr_field;
9179 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9180 type->field (0).set_name ("<<discriminant>>");
9181
9182 /* We need a way to find the correct discriminant given a
9183 variant name. For convenience we build a map here. */
9184 struct type *enum_type = disr_field->type ();
9185 std::unordered_map<std::string, ULONGEST> discriminant_map;
9186 for (int i = 0; i < enum_type->num_fields (); ++i)
9187 {
9188 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
9189 {
9190 const char *name
9191 = rust_last_path_segment (enum_type->field (i).name ());
9192 discriminant_map[name] = enum_type->field (i).loc_enumval ();
9193 }
9194 }
9195
9196 int n_fields = type->num_fields ();
9197 /* We don't need a range entry for the discriminant, but we do
9198 need one for every other field, as there is no default
9199 variant. */
9200 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9201 discriminant_range,
9202 n_fields - 1);
9203 /* Skip the discriminant here. */
9204 for (int i = 1; i < n_fields; ++i)
9205 {
9206 /* Find the final word in the name of this variant's type.
9207 That name can be used to look up the correct
9208 discriminant. */
9209 const char *variant_name
9210 = rust_last_path_segment (type->field (i).type ()->name ());
9211
9212 auto iter = discriminant_map.find (variant_name);
9213 if (iter != discriminant_map.end ())
9214 {
9215 ranges[i - 1].low = iter->second;
9216 ranges[i - 1].high = iter->second;
9217 }
9218
9219 /* In Rust, each element should have the size of the
9220 enclosing enum. */
9221 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9222
9223 /* Remove the discriminant field, if it exists. */
9224 struct type *sub_type = type->field (i).type ();
9225 if (sub_type->num_fields () > 0)
9226 {
9227 sub_type->set_num_fields (sub_type->num_fields () - 1);
9228 sub_type->set_fields (sub_type->fields () + 1);
9229 }
9230 type->field (i).set_name (variant_name);
9231 sub_type->set_name
9232 (rust_fully_qualify (&objfile->objfile_obstack,
9233 type->name (), variant_name));
9234 }
9235
9236 /* Indicate that this is a variant type. */
9237 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9238 gdb::array_view<discriminant_range> (ranges,
9239 n_fields - 1));
9240 }
9241 }
9242
9243 /* Rewrite some Rust unions to be structures with variants parts. */
9244
9245 static void
9246 rust_union_quirks (struct dwarf2_cu *cu)
9247 {
9248 gdb_assert (cu->per_cu->lang == language_rust);
9249 for (type *type_ : cu->rust_unions)
9250 quirk_rust_enum (type_, cu->per_objfile->objfile);
9251 /* We don't need this any more. */
9252 cu->rust_unions.clear ();
9253 }
9254
9255 /* See read.h. */
9256
9257 type_unit_group_unshareable *
9258 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9259 {
9260 auto iter = this->m_type_units.find (tu_group);
9261 if (iter != this->m_type_units.end ())
9262 return iter->second.get ();
9263
9264 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9265 type_unit_group_unshareable *result = uniq.get ();
9266 this->m_type_units[tu_group] = std::move (uniq);
9267 return result;
9268 }
9269
9270 struct type *
9271 dwarf2_per_objfile::get_type_for_signatured_type
9272 (signatured_type *sig_type) const
9273 {
9274 auto iter = this->m_type_map.find (sig_type);
9275 if (iter == this->m_type_map.end ())
9276 return nullptr;
9277
9278 return iter->second;
9279 }
9280
9281 void dwarf2_per_objfile::set_type_for_signatured_type
9282 (signatured_type *sig_type, struct type *type)
9283 {
9284 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9285
9286 this->m_type_map[sig_type] = type;
9287 }
9288
9289 /* A helper function for computing the list of all symbol tables
9290 included by PER_CU. */
9291
9292 static void
9293 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9294 htab_t all_children, htab_t all_type_symtabs,
9295 dwarf2_per_cu_data *per_cu,
9296 dwarf2_per_objfile *per_objfile,
9297 struct compunit_symtab *immediate_parent)
9298 {
9299 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9300 if (*slot != NULL)
9301 {
9302 /* This inclusion and its children have been processed. */
9303 return;
9304 }
9305
9306 *slot = per_cu;
9307
9308 /* Only add a CU if it has a symbol table. */
9309 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9310 if (cust != NULL)
9311 {
9312 /* If this is a type unit only add its symbol table if we haven't
9313 seen it yet (type unit per_cu's can share symtabs). */
9314 if (per_cu->is_debug_types)
9315 {
9316 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9317 if (*slot == NULL)
9318 {
9319 *slot = cust;
9320 result->push_back (cust);
9321 if (cust->user == NULL)
9322 cust->user = immediate_parent;
9323 }
9324 }
9325 else
9326 {
9327 result->push_back (cust);
9328 if (cust->user == NULL)
9329 cust->user = immediate_parent;
9330 }
9331 }
9332
9333 if (!per_cu->imported_symtabs_empty ())
9334 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9335 {
9336 recursively_compute_inclusions (result, all_children,
9337 all_type_symtabs, ptr, per_objfile,
9338 cust);
9339 }
9340 }
9341
9342 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9343 PER_CU. */
9344
9345 static void
9346 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9347 dwarf2_per_objfile *per_objfile)
9348 {
9349 gdb_assert (! per_cu->is_debug_types);
9350
9351 if (!per_cu->imported_symtabs_empty ())
9352 {
9353 int len;
9354 std::vector<compunit_symtab *> result_symtabs;
9355 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9356
9357 /* If we don't have a symtab, we can just skip this case. */
9358 if (cust == NULL)
9359 return;
9360
9361 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9362 htab_eq_pointer,
9363 NULL, xcalloc, xfree));
9364 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9365 htab_eq_pointer,
9366 NULL, xcalloc, xfree));
9367
9368 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9369 {
9370 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9371 all_type_symtabs.get (), ptr,
9372 per_objfile, cust);
9373 }
9374
9375 /* Now we have a transitive closure of all the included symtabs. */
9376 len = result_symtabs.size ();
9377 cust->includes
9378 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9379 struct compunit_symtab *, len + 1);
9380 memcpy (cust->includes, result_symtabs.data (),
9381 len * sizeof (compunit_symtab *));
9382 cust->includes[len] = NULL;
9383 }
9384 }
9385
9386 /* Compute the 'includes' field for the symtabs of all the CUs we just
9387 read. */
9388
9389 static void
9390 process_cu_includes (dwarf2_per_objfile *per_objfile)
9391 {
9392 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9393 {
9394 if (! iter->is_debug_types)
9395 compute_compunit_symtab_includes (iter, per_objfile);
9396 }
9397
9398 per_objfile->per_bfd->just_read_cus.clear ();
9399 }
9400
9401 /* Generate full symbol information for CU, whose DIEs have
9402 already been loaded into memory. */
9403
9404 static void
9405 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9406 {
9407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9408 struct objfile *objfile = per_objfile->objfile;
9409 struct gdbarch *gdbarch = objfile->arch ();
9410 CORE_ADDR lowpc, highpc;
9411 struct compunit_symtab *cust;
9412 CORE_ADDR baseaddr;
9413 struct block *static_block;
9414 CORE_ADDR addr;
9415
9416 baseaddr = objfile->text_section_offset ();
9417
9418 /* Clear the list here in case something was left over. */
9419 cu->method_list.clear ();
9420
9421 dwarf2_find_base_address (cu->dies, cu);
9422
9423 /* Before we start reading the top-level DIE, ensure it has a valid tag
9424 type. */
9425 switch (cu->dies->tag)
9426 {
9427 case DW_TAG_compile_unit:
9428 case DW_TAG_partial_unit:
9429 case DW_TAG_type_unit:
9430 break;
9431 default:
9432 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9433 dwarf_tag_name (cu->dies->tag),
9434 sect_offset_str (cu->per_cu->sect_off),
9435 objfile_name (per_objfile->objfile));
9436 }
9437
9438 /* Do line number decoding in read_file_scope () */
9439 process_die (cu->dies, cu);
9440
9441 /* For now fudge the Go package. */
9442 if (cu->per_cu->lang == language_go)
9443 fixup_go_packaging (cu);
9444
9445 /* Now that we have processed all the DIEs in the CU, all the types
9446 should be complete, and it should now be safe to compute all of the
9447 physnames. */
9448 compute_delayed_physnames (cu);
9449
9450 if (cu->per_cu->lang == language_rust)
9451 rust_union_quirks (cu);
9452
9453 /* Some compilers don't define a DW_AT_high_pc attribute for the
9454 compilation unit. If the DW_AT_high_pc is missing, synthesize
9455 it, by scanning the DIE's below the compilation unit. */
9456 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9457
9458 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9459 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9460
9461 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9462 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9463 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9464 addrmap to help ensure it has an accurate map of pc values belonging to
9465 this comp unit. */
9466 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9467
9468 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9469 SECT_OFF_TEXT (objfile),
9470 0);
9471
9472 if (cust != NULL)
9473 {
9474 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9475
9476 /* Set symtab language to language from DW_AT_language. If the
9477 compilation is from a C file generated by language preprocessors, do
9478 not set the language if it was already deduced by start_subfile. */
9479 if (!(cu->per_cu->lang == language_c
9480 && cust->primary_filetab ()->language () != language_unknown))
9481 cust->primary_filetab ()->set_language (cu->per_cu->lang);
9482
9483 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9484 produce DW_AT_location with location lists but it can be possibly
9485 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9486 there were bugs in prologue debug info, fixed later in GCC-4.5
9487 by "unwind info for epilogues" patch (which is not directly related).
9488
9489 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9490 needed, it would be wrong due to missing DW_AT_producer there.
9491
9492 Still one can confuse GDB by using non-standard GCC compilation
9493 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9494 */
9495 if (cu->has_loclist && gcc_4_minor >= 5)
9496 cust->set_locations_valid (true);
9497
9498 if (gcc_4_minor >= 5)
9499 cust->set_epilogue_unwind_valid (true);
9500
9501 cust->set_call_site_htab (cu->call_site_htab);
9502 }
9503
9504 per_objfile->set_symtab (cu->per_cu, cust);
9505
9506 /* Push it for inclusion processing later. */
9507 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9508
9509 /* Not needed any more. */
9510 cu->reset_builder ();
9511 }
9512
9513 /* Generate full symbol information for type unit CU, whose DIEs have
9514 already been loaded into memory. */
9515
9516 static void
9517 process_full_type_unit (dwarf2_cu *cu,
9518 enum language pretend_language)
9519 {
9520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9521 struct objfile *objfile = per_objfile->objfile;
9522 struct compunit_symtab *cust;
9523 struct signatured_type *sig_type;
9524
9525 gdb_assert (cu->per_cu->is_debug_types);
9526 sig_type = (struct signatured_type *) cu->per_cu;
9527
9528 /* Clear the list here in case something was left over. */
9529 cu->method_list.clear ();
9530
9531 /* The symbol tables are set up in read_type_unit_scope. */
9532 process_die (cu->dies, cu);
9533
9534 /* For now fudge the Go package. */
9535 if (cu->per_cu->lang == language_go)
9536 fixup_go_packaging (cu);
9537
9538 /* Now that we have processed all the DIEs in the CU, all the types
9539 should be complete, and it should now be safe to compute all of the
9540 physnames. */
9541 compute_delayed_physnames (cu);
9542
9543 if (cu->per_cu->lang == language_rust)
9544 rust_union_quirks (cu);
9545
9546 /* TUs share symbol tables.
9547 If this is the first TU to use this symtab, complete the construction
9548 of it with end_expandable_symtab. Otherwise, complete the addition of
9549 this TU's symbols to the existing symtab. */
9550 type_unit_group_unshareable *tug_unshare =
9551 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9552 if (tug_unshare->compunit_symtab == NULL)
9553 {
9554 buildsym_compunit *builder = cu->get_builder ();
9555 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9556 tug_unshare->compunit_symtab = cust;
9557
9558 if (cust != NULL)
9559 {
9560 /* Set symtab language to language from DW_AT_language. If the
9561 compilation is from a C file generated by language preprocessors,
9562 do not set the language if it was already deduced by
9563 start_subfile. */
9564 if (!(cu->per_cu->lang == language_c
9565 && cust->primary_filetab ()->language () != language_c))
9566 cust->primary_filetab ()->set_language (cu->per_cu->lang);
9567 }
9568 }
9569 else
9570 {
9571 cu->get_builder ()->augment_type_symtab ();
9572 cust = tug_unshare->compunit_symtab;
9573 }
9574
9575 per_objfile->set_symtab (cu->per_cu, cust);
9576
9577 /* Not needed any more. */
9578 cu->reset_builder ();
9579 }
9580
9581 /* Process an imported unit DIE. */
9582
9583 static void
9584 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9585 {
9586 struct attribute *attr;
9587
9588 /* For now we don't handle imported units in type units. */
9589 if (cu->per_cu->is_debug_types)
9590 {
9591 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9592 " supported in type units [in module %s]"),
9593 objfile_name (cu->per_objfile->objfile));
9594 }
9595
9596 attr = dwarf2_attr (die, DW_AT_import, cu);
9597 if (attr != NULL)
9598 {
9599 sect_offset sect_off = attr->get_ref_die_offset ();
9600 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9601 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9602 dwarf2_per_cu_data *per_cu
9603 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9604 per_objfile->per_bfd);
9605
9606 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9607 into another compilation unit, at root level. Regard this as a hint,
9608 and ignore it. */
9609 if (die->parent && die->parent->parent == NULL
9610 && per_cu->unit_type == DW_UT_compile
9611 && per_cu->lang == language_cplus)
9612 return;
9613
9614 /* If necessary, add it to the queue and load its DIEs. */
9615 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9616 cu->per_cu->lang))
9617 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9618 false, cu->per_cu->lang);
9619
9620 cu->per_cu->imported_symtabs_push (per_cu);
9621 }
9622 }
9623
9624 /* RAII object that represents a process_die scope: i.e.,
9625 starts/finishes processing a DIE. */
9626 class process_die_scope
9627 {
9628 public:
9629 process_die_scope (die_info *die, dwarf2_cu *cu)
9630 : m_die (die), m_cu (cu)
9631 {
9632 /* We should only be processing DIEs not already in process. */
9633 gdb_assert (!m_die->in_process);
9634 m_die->in_process = true;
9635 }
9636
9637 ~process_die_scope ()
9638 {
9639 m_die->in_process = false;
9640
9641 /* If we're done processing the DIE for the CU that owns the line
9642 header, we don't need the line header anymore. */
9643 if (m_cu->line_header_die_owner == m_die)
9644 {
9645 delete m_cu->line_header;
9646 m_cu->line_header = NULL;
9647 m_cu->line_header_die_owner = NULL;
9648 }
9649 }
9650
9651 private:
9652 die_info *m_die;
9653 dwarf2_cu *m_cu;
9654 };
9655
9656 /* Process a die and its children. */
9657
9658 static void
9659 process_die (struct die_info *die, struct dwarf2_cu *cu)
9660 {
9661 process_die_scope scope (die, cu);
9662
9663 switch (die->tag)
9664 {
9665 case DW_TAG_padding:
9666 break;
9667 case DW_TAG_compile_unit:
9668 case DW_TAG_partial_unit:
9669 read_file_scope (die, cu);
9670 break;
9671 case DW_TAG_type_unit:
9672 read_type_unit_scope (die, cu);
9673 break;
9674 case DW_TAG_subprogram:
9675 /* Nested subprograms in Fortran get a prefix. */
9676 if (cu->per_cu->lang == language_fortran
9677 && die->parent != NULL
9678 && die->parent->tag == DW_TAG_subprogram)
9679 cu->processing_has_namespace_info = true;
9680 /* Fall through. */
9681 case DW_TAG_inlined_subroutine:
9682 read_func_scope (die, cu);
9683 break;
9684 case DW_TAG_lexical_block:
9685 case DW_TAG_try_block:
9686 case DW_TAG_catch_block:
9687 read_lexical_block_scope (die, cu);
9688 break;
9689 case DW_TAG_call_site:
9690 case DW_TAG_GNU_call_site:
9691 read_call_site_scope (die, cu);
9692 break;
9693 case DW_TAG_class_type:
9694 case DW_TAG_interface_type:
9695 case DW_TAG_structure_type:
9696 case DW_TAG_union_type:
9697 case DW_TAG_namelist:
9698 process_structure_scope (die, cu);
9699 break;
9700 case DW_TAG_enumeration_type:
9701 process_enumeration_scope (die, cu);
9702 break;
9703
9704 /* These dies have a type, but processing them does not create
9705 a symbol or recurse to process the children. Therefore we can
9706 read them on-demand through read_type_die. */
9707 case DW_TAG_subroutine_type:
9708 case DW_TAG_set_type:
9709 case DW_TAG_pointer_type:
9710 case DW_TAG_ptr_to_member_type:
9711 case DW_TAG_reference_type:
9712 case DW_TAG_rvalue_reference_type:
9713 case DW_TAG_string_type:
9714 break;
9715
9716 case DW_TAG_array_type:
9717 /* We only need to handle this case for Ada -- in other
9718 languages, it's normal for the compiler to emit a typedef
9719 instead. */
9720 if (cu->per_cu->lang != language_ada)
9721 break;
9722 /* FALLTHROUGH */
9723 case DW_TAG_base_type:
9724 case DW_TAG_subrange_type:
9725 case DW_TAG_typedef:
9726 /* Add a typedef symbol for the type definition, if it has a
9727 DW_AT_name. */
9728 new_symbol (die, read_type_die (die, cu), cu);
9729 break;
9730 case DW_TAG_common_block:
9731 read_common_block (die, cu);
9732 break;
9733 case DW_TAG_common_inclusion:
9734 break;
9735 case DW_TAG_namespace:
9736 cu->processing_has_namespace_info = true;
9737 read_namespace (die, cu);
9738 break;
9739 case DW_TAG_module:
9740 cu->processing_has_namespace_info = true;
9741 read_module (die, cu);
9742 break;
9743 case DW_TAG_imported_declaration:
9744 cu->processing_has_namespace_info = true;
9745 if (read_namespace_alias (die, cu))
9746 break;
9747 /* The declaration is not a global namespace alias. */
9748 /* Fall through. */
9749 case DW_TAG_imported_module:
9750 cu->processing_has_namespace_info = true;
9751 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9752 || cu->per_cu->lang != language_fortran))
9753 complaint (_("Tag '%s' has unexpected children"),
9754 dwarf_tag_name (die->tag));
9755 read_import_statement (die, cu);
9756 break;
9757
9758 case DW_TAG_imported_unit:
9759 process_imported_unit_die (die, cu);
9760 break;
9761
9762 case DW_TAG_variable:
9763 read_variable (die, cu);
9764 break;
9765
9766 default:
9767 new_symbol (die, NULL, cu);
9768 break;
9769 }
9770 }
9771 \f
9772 /* DWARF name computation. */
9773
9774 /* A helper function for dwarf2_compute_name which determines whether DIE
9775 needs to have the name of the scope prepended to the name listed in the
9776 die. */
9777
9778 static int
9779 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9780 {
9781 struct attribute *attr;
9782
9783 switch (die->tag)
9784 {
9785 case DW_TAG_namespace:
9786 case DW_TAG_typedef:
9787 case DW_TAG_class_type:
9788 case DW_TAG_interface_type:
9789 case DW_TAG_structure_type:
9790 case DW_TAG_union_type:
9791 case DW_TAG_enumeration_type:
9792 case DW_TAG_enumerator:
9793 case DW_TAG_subprogram:
9794 case DW_TAG_inlined_subroutine:
9795 case DW_TAG_member:
9796 case DW_TAG_imported_declaration:
9797 return 1;
9798
9799 case DW_TAG_variable:
9800 case DW_TAG_constant:
9801 /* We only need to prefix "globally" visible variables. These include
9802 any variable marked with DW_AT_external or any variable that
9803 lives in a namespace. [Variables in anonymous namespaces
9804 require prefixing, but they are not DW_AT_external.] */
9805
9806 if (dwarf2_attr (die, DW_AT_specification, cu))
9807 {
9808 struct dwarf2_cu *spec_cu = cu;
9809
9810 return die_needs_namespace (die_specification (die, &spec_cu),
9811 spec_cu);
9812 }
9813
9814 attr = dwarf2_attr (die, DW_AT_external, cu);
9815 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9816 && die->parent->tag != DW_TAG_module)
9817 return 0;
9818 /* A variable in a lexical block of some kind does not need a
9819 namespace, even though in C++ such variables may be external
9820 and have a mangled name. */
9821 if (die->parent->tag == DW_TAG_lexical_block
9822 || die->parent->tag == DW_TAG_try_block
9823 || die->parent->tag == DW_TAG_catch_block
9824 || die->parent->tag == DW_TAG_subprogram)
9825 return 0;
9826 return 1;
9827
9828 default:
9829 return 0;
9830 }
9831 }
9832
9833 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9834 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9835 defined for the given DIE. */
9836
9837 static struct attribute *
9838 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9839 {
9840 struct attribute *attr;
9841
9842 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9843 if (attr == NULL)
9844 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9845
9846 return attr;
9847 }
9848
9849 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9850 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9851 defined for the given DIE. */
9852
9853 static const char *
9854 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9855 {
9856 const char *linkage_name;
9857
9858 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9859 if (linkage_name == NULL)
9860 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9861
9862 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9863 See https://github.com/rust-lang/rust/issues/32925. */
9864 if (cu->per_cu->lang == language_rust && linkage_name != NULL
9865 && strchr (linkage_name, '{') != NULL)
9866 linkage_name = NULL;
9867
9868 return linkage_name;
9869 }
9870
9871 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9872 compute the physname for the object, which include a method's:
9873 - formal parameters (C++),
9874 - receiver type (Go),
9875
9876 The term "physname" is a bit confusing.
9877 For C++, for example, it is the demangled name.
9878 For Go, for example, it's the mangled name.
9879
9880 For Ada, return the DIE's linkage name rather than the fully qualified
9881 name. PHYSNAME is ignored..
9882
9883 The result is allocated on the objfile->per_bfd's obstack and
9884 canonicalized. */
9885
9886 static const char *
9887 dwarf2_compute_name (const char *name,
9888 struct die_info *die, struct dwarf2_cu *cu,
9889 int physname)
9890 {
9891 struct objfile *objfile = cu->per_objfile->objfile;
9892
9893 if (name == NULL)
9894 name = dwarf2_name (die, cu);
9895
9896 enum language lang = cu->per_cu->lang;
9897
9898 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9899 but otherwise compute it by typename_concat inside GDB.
9900 FIXME: Actually this is not really true, or at least not always true.
9901 It's all very confusing. compute_and_set_names doesn't try to demangle
9902 Fortran names because there is no mangling standard. So new_symbol
9903 will set the demangled name to the result of dwarf2_full_name, and it is
9904 the demangled name that GDB uses if it exists. */
9905 if (lang == language_ada
9906 || (lang == language_fortran && physname))
9907 {
9908 /* For Ada unit, we prefer the linkage name over the name, as
9909 the former contains the exported name, which the user expects
9910 to be able to reference. Ideally, we want the user to be able
9911 to reference this entity using either natural or linkage name,
9912 but we haven't started looking at this enhancement yet. */
9913 const char *linkage_name = dw2_linkage_name (die, cu);
9914
9915 if (linkage_name != NULL)
9916 return linkage_name;
9917 }
9918
9919 /* These are the only languages we know how to qualify names in. */
9920 if (name != NULL
9921 && (lang == language_cplus
9922 || lang == language_fortran || lang == language_d
9923 || lang == language_rust))
9924 {
9925 if (die_needs_namespace (die, cu))
9926 {
9927 const char *prefix;
9928 const char *canonical_name = NULL;
9929
9930 string_file buf;
9931
9932 prefix = determine_prefix (die, cu);
9933 if (*prefix != '\0')
9934 {
9935 gdb::unique_xmalloc_ptr<char> prefixed_name
9936 (typename_concat (NULL, prefix, name, physname, cu));
9937
9938 buf.puts (prefixed_name.get ());
9939 }
9940 else
9941 buf.puts (name);
9942
9943 /* Template parameters may be specified in the DIE's DW_AT_name, or
9944 as children with DW_TAG_template_type_param or
9945 DW_TAG_value_type_param. If the latter, add them to the name
9946 here. If the name already has template parameters, then
9947 skip this step; some versions of GCC emit both, and
9948 it is more efficient to use the pre-computed name.
9949
9950 Something to keep in mind about this process: it is very
9951 unlikely, or in some cases downright impossible, to produce
9952 something that will match the mangled name of a function.
9953 If the definition of the function has the same debug info,
9954 we should be able to match up with it anyway. But fallbacks
9955 using the minimal symbol, for instance to find a method
9956 implemented in a stripped copy of libstdc++, will not work.
9957 If we do not have debug info for the definition, we will have to
9958 match them up some other way.
9959
9960 When we do name matching there is a related problem with function
9961 templates; two instantiated function templates are allowed to
9962 differ only by their return types, which we do not add here. */
9963
9964 if (lang == language_cplus && strchr (name, '<') == NULL)
9965 {
9966 struct attribute *attr;
9967 struct die_info *child;
9968 int first = 1;
9969
9970 die->building_fullname = 1;
9971
9972 for (child = die->child; child != NULL; child = child->sibling)
9973 {
9974 struct type *type;
9975 LONGEST value;
9976 const gdb_byte *bytes;
9977 struct dwarf2_locexpr_baton *baton;
9978 struct value *v;
9979
9980 if (child->tag != DW_TAG_template_type_param
9981 && child->tag != DW_TAG_template_value_param)
9982 continue;
9983
9984 if (first)
9985 {
9986 buf.puts ("<");
9987 first = 0;
9988 }
9989 else
9990 buf.puts (", ");
9991
9992 attr = dwarf2_attr (child, DW_AT_type, cu);
9993 if (attr == NULL)
9994 {
9995 complaint (_("template parameter missing DW_AT_type"));
9996 buf.puts ("UNKNOWN_TYPE");
9997 continue;
9998 }
9999 type = die_type (child, cu);
10000
10001 if (child->tag == DW_TAG_template_type_param)
10002 {
10003 cu->language_defn->print_type (type, "", &buf, -1, 0,
10004 &type_print_raw_options);
10005 continue;
10006 }
10007
10008 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10009 if (attr == NULL)
10010 {
10011 complaint (_("template parameter missing "
10012 "DW_AT_const_value"));
10013 buf.puts ("UNKNOWN_VALUE");
10014 continue;
10015 }
10016
10017 dwarf2_const_value_attr (attr, type, name,
10018 &cu->comp_unit_obstack, cu,
10019 &value, &bytes, &baton);
10020
10021 if (type->has_no_signedness ())
10022 /* GDB prints characters as NUMBER 'CHAR'. If that's
10023 changed, this can use value_print instead. */
10024 cu->language_defn->printchar (value, type, &buf);
10025 else
10026 {
10027 struct value_print_options opts;
10028
10029 if (baton != NULL)
10030 v = dwarf2_evaluate_loc_desc (type, NULL,
10031 baton->data,
10032 baton->size,
10033 baton->per_cu,
10034 baton->per_objfile);
10035 else if (bytes != NULL)
10036 {
10037 v = allocate_value (type);
10038 memcpy (value_contents_writeable (v).data (), bytes,
10039 TYPE_LENGTH (type));
10040 }
10041 else
10042 v = value_from_longest (type, value);
10043
10044 /* Specify decimal so that we do not depend on
10045 the radix. */
10046 get_formatted_print_options (&opts, 'd');
10047 opts.raw = 1;
10048 value_print (v, &buf, &opts);
10049 release_value (v);
10050 }
10051 }
10052
10053 die->building_fullname = 0;
10054
10055 if (!first)
10056 {
10057 /* Close the argument list, with a space if necessary
10058 (nested templates). */
10059 if (!buf.empty () && buf.string ().back () == '>')
10060 buf.puts (" >");
10061 else
10062 buf.puts (">");
10063 }
10064 }
10065
10066 /* For C++ methods, append formal parameter type
10067 information, if PHYSNAME. */
10068
10069 if (physname && die->tag == DW_TAG_subprogram
10070 && lang == language_cplus)
10071 {
10072 struct type *type = read_type_die (die, cu);
10073
10074 c_type_print_args (type, &buf, 1, lang,
10075 &type_print_raw_options);
10076
10077 if (lang == language_cplus)
10078 {
10079 /* Assume that an artificial first parameter is
10080 "this", but do not crash if it is not. RealView
10081 marks unnamed (and thus unused) parameters as
10082 artificial; there is no way to differentiate
10083 the two cases. */
10084 if (type->num_fields () > 0
10085 && TYPE_FIELD_ARTIFICIAL (type, 0)
10086 && type->field (0).type ()->code () == TYPE_CODE_PTR
10087 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10088 buf.puts (" const");
10089 }
10090 }
10091
10092 const std::string &intermediate_name = buf.string ();
10093
10094 if (lang == language_cplus)
10095 canonical_name
10096 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10097 objfile);
10098
10099 /* If we only computed INTERMEDIATE_NAME, or if
10100 INTERMEDIATE_NAME is already canonical, then we need to
10101 intern it. */
10102 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10103 name = objfile->intern (intermediate_name);
10104 else
10105 name = canonical_name;
10106 }
10107 }
10108
10109 return name;
10110 }
10111
10112 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10113 If scope qualifiers are appropriate they will be added. The result
10114 will be allocated on the storage_obstack, or NULL if the DIE does
10115 not have a name. NAME may either be from a previous call to
10116 dwarf2_name or NULL.
10117
10118 The output string will be canonicalized (if C++). */
10119
10120 static const char *
10121 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10122 {
10123 return dwarf2_compute_name (name, die, cu, 0);
10124 }
10125
10126 /* Construct a physname for the given DIE in CU. NAME may either be
10127 from a previous call to dwarf2_name or NULL. The result will be
10128 allocated on the objfile_objstack or NULL if the DIE does not have a
10129 name.
10130
10131 The output string will be canonicalized (if C++). */
10132
10133 static const char *
10134 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10135 {
10136 struct objfile *objfile = cu->per_objfile->objfile;
10137 const char *retval, *mangled = NULL, *canon = NULL;
10138 int need_copy = 1;
10139
10140 /* In this case dwarf2_compute_name is just a shortcut not building anything
10141 on its own. */
10142 if (!die_needs_namespace (die, cu))
10143 return dwarf2_compute_name (name, die, cu, 1);
10144
10145 if (cu->per_cu->lang != language_rust)
10146 mangled = dw2_linkage_name (die, cu);
10147
10148 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10149 has computed. */
10150 gdb::unique_xmalloc_ptr<char> demangled;
10151 if (mangled != NULL)
10152 {
10153 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10154 {
10155 /* Do nothing (do not demangle the symbol name). */
10156 }
10157 else
10158 {
10159 /* Use DMGL_RET_DROP for C++ template functions to suppress
10160 their return type. It is easier for GDB users to search
10161 for such functions as `name(params)' than `long name(params)'.
10162 In such case the minimal symbol names do not match the full
10163 symbol names but for template functions there is never a need
10164 to look up their definition from their declaration so
10165 the only disadvantage remains the minimal symbol variant
10166 `long name(params)' does not have the proper inferior type. */
10167 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
10168 | DMGL_RET_DROP));
10169 }
10170 if (demangled)
10171 canon = demangled.get ();
10172 else
10173 {
10174 canon = mangled;
10175 need_copy = 0;
10176 }
10177 }
10178
10179 if (canon == NULL || check_physname)
10180 {
10181 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10182
10183 if (canon != NULL && strcmp (physname, canon) != 0)
10184 {
10185 /* It may not mean a bug in GDB. The compiler could also
10186 compute DW_AT_linkage_name incorrectly. But in such case
10187 GDB would need to be bug-to-bug compatible. */
10188
10189 complaint (_("Computed physname <%s> does not match demangled <%s> "
10190 "(from linkage <%s>) - DIE at %s [in module %s]"),
10191 physname, canon, mangled, sect_offset_str (die->sect_off),
10192 objfile_name (objfile));
10193
10194 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10195 is available here - over computed PHYSNAME. It is safer
10196 against both buggy GDB and buggy compilers. */
10197
10198 retval = canon;
10199 }
10200 else
10201 {
10202 retval = physname;
10203 need_copy = 0;
10204 }
10205 }
10206 else
10207 retval = canon;
10208
10209 if (need_copy)
10210 retval = objfile->intern (retval);
10211
10212 return retval;
10213 }
10214
10215 /* Inspect DIE in CU for a namespace alias. If one exists, record
10216 a new symbol for it.
10217
10218 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10219
10220 static int
10221 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10222 {
10223 struct attribute *attr;
10224
10225 /* If the die does not have a name, this is not a namespace
10226 alias. */
10227 attr = dwarf2_attr (die, DW_AT_name, cu);
10228 if (attr != NULL)
10229 {
10230 int num;
10231 struct die_info *d = die;
10232 struct dwarf2_cu *imported_cu = cu;
10233
10234 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10235 keep inspecting DIEs until we hit the underlying import. */
10236 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10237 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10238 {
10239 attr = dwarf2_attr (d, DW_AT_import, cu);
10240 if (attr == NULL)
10241 break;
10242
10243 d = follow_die_ref (d, attr, &imported_cu);
10244 if (d->tag != DW_TAG_imported_declaration)
10245 break;
10246 }
10247
10248 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10249 {
10250 complaint (_("DIE at %s has too many recursively imported "
10251 "declarations"), sect_offset_str (d->sect_off));
10252 return 0;
10253 }
10254
10255 if (attr != NULL)
10256 {
10257 struct type *type;
10258 sect_offset sect_off = attr->get_ref_die_offset ();
10259
10260 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10261 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10262 {
10263 /* This declaration is a global namespace alias. Add
10264 a symbol for it whose type is the aliased namespace. */
10265 new_symbol (die, type, cu);
10266 return 1;
10267 }
10268 }
10269 }
10270
10271 return 0;
10272 }
10273
10274 /* Return the using directives repository (global or local?) to use in the
10275 current context for CU.
10276
10277 For Ada, imported declarations can materialize renamings, which *may* be
10278 global. However it is impossible (for now?) in DWARF to distinguish
10279 "external" imported declarations and "static" ones. As all imported
10280 declarations seem to be static in all other languages, make them all CU-wide
10281 global only in Ada. */
10282
10283 static struct using_direct **
10284 using_directives (struct dwarf2_cu *cu)
10285 {
10286 if (cu->per_cu->lang == language_ada
10287 && cu->get_builder ()->outermost_context_p ())
10288 return cu->get_builder ()->get_global_using_directives ();
10289 else
10290 return cu->get_builder ()->get_local_using_directives ();
10291 }
10292
10293 /* Read the import statement specified by the given die and record it. */
10294
10295 static void
10296 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10297 {
10298 struct objfile *objfile = cu->per_objfile->objfile;
10299 struct attribute *import_attr;
10300 struct die_info *imported_die, *child_die;
10301 struct dwarf2_cu *imported_cu;
10302 const char *imported_name;
10303 const char *imported_name_prefix;
10304 const char *canonical_name;
10305 const char *import_alias;
10306 const char *imported_declaration = NULL;
10307 const char *import_prefix;
10308 std::vector<const char *> excludes;
10309
10310 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10311 if (import_attr == NULL)
10312 {
10313 complaint (_("Tag '%s' has no DW_AT_import"),
10314 dwarf_tag_name (die->tag));
10315 return;
10316 }
10317
10318 imported_cu = cu;
10319 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10320 imported_name = dwarf2_name (imported_die, imported_cu);
10321 if (imported_name == NULL)
10322 {
10323 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10324
10325 The import in the following code:
10326 namespace A
10327 {
10328 typedef int B;
10329 }
10330
10331 int main ()
10332 {
10333 using A::B;
10334 B b;
10335 return b;
10336 }
10337
10338 ...
10339 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10340 <52> DW_AT_decl_file : 1
10341 <53> DW_AT_decl_line : 6
10342 <54> DW_AT_import : <0x75>
10343 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10344 <59> DW_AT_name : B
10345 <5b> DW_AT_decl_file : 1
10346 <5c> DW_AT_decl_line : 2
10347 <5d> DW_AT_type : <0x6e>
10348 ...
10349 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10350 <76> DW_AT_byte_size : 4
10351 <77> DW_AT_encoding : 5 (signed)
10352
10353 imports the wrong die ( 0x75 instead of 0x58 ).
10354 This case will be ignored until the gcc bug is fixed. */
10355 return;
10356 }
10357
10358 /* Figure out the local name after import. */
10359 import_alias = dwarf2_name (die, cu);
10360
10361 /* Figure out where the statement is being imported to. */
10362 import_prefix = determine_prefix (die, cu);
10363
10364 /* Figure out what the scope of the imported die is and prepend it
10365 to the name of the imported die. */
10366 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10367
10368 if (imported_die->tag != DW_TAG_namespace
10369 && imported_die->tag != DW_TAG_module)
10370 {
10371 imported_declaration = imported_name;
10372 canonical_name = imported_name_prefix;
10373 }
10374 else if (strlen (imported_name_prefix) > 0)
10375 canonical_name = obconcat (&objfile->objfile_obstack,
10376 imported_name_prefix,
10377 (cu->per_cu->lang == language_d
10378 ? "."
10379 : "::"),
10380 imported_name, (char *) NULL);
10381 else
10382 canonical_name = imported_name;
10383
10384 if (die->tag == DW_TAG_imported_module
10385 && cu->per_cu->lang == language_fortran)
10386 for (child_die = die->child; child_die && child_die->tag;
10387 child_die = child_die->sibling)
10388 {
10389 /* DWARF-4: A Fortran use statement with a “rename list” may be
10390 represented by an imported module entry with an import attribute
10391 referring to the module and owned entries corresponding to those
10392 entities that are renamed as part of being imported. */
10393
10394 if (child_die->tag != DW_TAG_imported_declaration)
10395 {
10396 complaint (_("child DW_TAG_imported_declaration expected "
10397 "- DIE at %s [in module %s]"),
10398 sect_offset_str (child_die->sect_off),
10399 objfile_name (objfile));
10400 continue;
10401 }
10402
10403 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10404 if (import_attr == NULL)
10405 {
10406 complaint (_("Tag '%s' has no DW_AT_import"),
10407 dwarf_tag_name (child_die->tag));
10408 continue;
10409 }
10410
10411 imported_cu = cu;
10412 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10413 &imported_cu);
10414 imported_name = dwarf2_name (imported_die, imported_cu);
10415 if (imported_name == NULL)
10416 {
10417 complaint (_("child DW_TAG_imported_declaration has unknown "
10418 "imported name - DIE at %s [in module %s]"),
10419 sect_offset_str (child_die->sect_off),
10420 objfile_name (objfile));
10421 continue;
10422 }
10423
10424 excludes.push_back (imported_name);
10425
10426 process_die (child_die, cu);
10427 }
10428
10429 add_using_directive (using_directives (cu),
10430 import_prefix,
10431 canonical_name,
10432 import_alias,
10433 imported_declaration,
10434 excludes,
10435 0,
10436 &objfile->objfile_obstack);
10437 }
10438
10439 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10440 types, but gives them a size of zero. Starting with version 14,
10441 ICC is compatible with GCC. */
10442
10443 static bool
10444 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10445 {
10446 if (!cu->checked_producer)
10447 check_producer (cu);
10448
10449 return cu->producer_is_icc_lt_14;
10450 }
10451
10452 /* ICC generates a DW_AT_type for C void functions. This was observed on
10453 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10454 which says that void functions should not have a DW_AT_type. */
10455
10456 static bool
10457 producer_is_icc (struct dwarf2_cu *cu)
10458 {
10459 if (!cu->checked_producer)
10460 check_producer (cu);
10461
10462 return cu->producer_is_icc;
10463 }
10464
10465 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10466 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10467 this, it was first present in GCC release 4.3.0. */
10468
10469 static bool
10470 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10471 {
10472 if (!cu->checked_producer)
10473 check_producer (cu);
10474
10475 return cu->producer_is_gcc_lt_4_3;
10476 }
10477
10478 static file_and_directory &
10479 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10480 {
10481 if (cu->per_cu->fnd != nullptr)
10482 return *cu->per_cu->fnd;
10483
10484 /* Find the filename. Do not use dwarf2_name here, since the filename
10485 is not a source language identifier. */
10486 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
10487 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
10488
10489 if (res.get_comp_dir () == nullptr
10490 && producer_is_gcc_lt_4_3 (cu)
10491 && res.get_name () != nullptr
10492 && IS_ABSOLUTE_PATH (res.get_name ()))
10493 res.set_comp_dir (ldirname (res.get_name ()));
10494
10495 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
10496 return *cu->per_cu->fnd;
10497 }
10498
10499 /* Handle DW_AT_stmt_list for a compilation unit.
10500 DIE is the DW_TAG_compile_unit die for CU.
10501 COMP_DIR is the compilation directory. LOWPC is passed to
10502 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10503
10504 static void
10505 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10506 const file_and_directory &fnd, CORE_ADDR lowpc) /* ARI: editCase function */
10507 {
10508 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10509 struct attribute *attr;
10510 struct line_header line_header_local;
10511 hashval_t line_header_local_hash;
10512 void **slot;
10513 int decode_mapping;
10514
10515 gdb_assert (! cu->per_cu->is_debug_types);
10516
10517 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10518 if (attr == NULL || !attr->form_is_unsigned ())
10519 return;
10520
10521 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10522
10523 /* The line header hash table is only created if needed (it exists to
10524 prevent redundant reading of the line table for partial_units).
10525 If we're given a partial_unit, we'll need it. If we're given a
10526 compile_unit, then use the line header hash table if it's already
10527 created, but don't create one just yet. */
10528
10529 if (per_objfile->line_header_hash == NULL
10530 && die->tag == DW_TAG_partial_unit)
10531 {
10532 per_objfile->line_header_hash
10533 .reset (htab_create_alloc (127, line_header_hash_voidp,
10534 line_header_eq_voidp,
10535 htab_delete_entry<line_header>,
10536 xcalloc, xfree));
10537 }
10538
10539 line_header_local.sect_off = line_offset;
10540 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10541 line_header_local_hash = line_header_hash (&line_header_local);
10542 if (per_objfile->line_header_hash != NULL)
10543 {
10544 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10545 &line_header_local,
10546 line_header_local_hash, NO_INSERT);
10547
10548 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10549 is not present in *SLOT (since if there is something in *SLOT then
10550 it will be for a partial_unit). */
10551 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10552 {
10553 gdb_assert (*slot != NULL);
10554 cu->line_header = (struct line_header *) *slot;
10555 return;
10556 }
10557 }
10558
10559 /* dwarf_decode_line_header does not yet provide sufficient information.
10560 We always have to call also dwarf_decode_lines for it. */
10561 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10562 if (lh == NULL)
10563 return;
10564
10565 cu->line_header = lh.release ();
10566 cu->line_header_die_owner = die;
10567
10568 if (per_objfile->line_header_hash == NULL)
10569 slot = NULL;
10570 else
10571 {
10572 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10573 &line_header_local,
10574 line_header_local_hash, INSERT);
10575 gdb_assert (slot != NULL);
10576 }
10577 if (slot != NULL && *slot == NULL)
10578 {
10579 /* This newly decoded line number information unit will be owned
10580 by line_header_hash hash table. */
10581 *slot = cu->line_header;
10582 cu->line_header_die_owner = NULL;
10583 }
10584 else
10585 {
10586 /* We cannot free any current entry in (*slot) as that struct line_header
10587 may be already used by multiple CUs. Create only temporary decoded
10588 line_header for this CU - it may happen at most once for each line
10589 number information unit. And if we're not using line_header_hash
10590 then this is what we want as well. */
10591 gdb_assert (die->tag != DW_TAG_partial_unit);
10592 }
10593 decode_mapping = (die->tag != DW_TAG_partial_unit);
10594 dwarf_decode_lines (cu->line_header, fnd, cu, nullptr, lowpc,
10595 decode_mapping);
10596
10597 }
10598
10599 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10600
10601 static void
10602 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10603 {
10604 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10605 struct objfile *objfile = per_objfile->objfile;
10606 struct gdbarch *gdbarch = objfile->arch ();
10607 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10608 CORE_ADDR highpc = ((CORE_ADDR) 0);
10609 struct attribute *attr;
10610 struct die_info *child_die;
10611 CORE_ADDR baseaddr;
10612
10613 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
10614 baseaddr = objfile->text_section_offset ();
10615
10616 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10617
10618 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10619 from finish_block. */
10620 if (lowpc == ((CORE_ADDR) -1))
10621 lowpc = highpc;
10622 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10623
10624 file_and_directory &fnd = find_file_and_directory (die, cu);
10625
10626 cu->start_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile), lowpc);
10627
10628 gdb_assert (per_objfile->sym_cu == nullptr);
10629 scoped_restore restore_sym_cu
10630 = make_scoped_restore (&per_objfile->sym_cu, cu);
10631
10632 /* Decode line number information if present. We do this before
10633 processing child DIEs, so that the line header table is available
10634 for DW_AT_decl_file. The PC check is here because, if LOWPC and
10635 HIGHPC are both 0x0, then there won't be any interesting code in
10636 the CU, but a check later on (in
10637 lnp_state_machine::check_line_address) will fail to properly
10638 exclude an entry that was removed via --gc-sections. */
10639 if (lowpc != highpc)
10640 handle_DW_AT_stmt_list (die, cu, fnd, lowpc);
10641
10642 /* Process all dies in compilation unit. */
10643 if (die->child != NULL)
10644 {
10645 child_die = die->child;
10646 while (child_die && child_die->tag)
10647 {
10648 process_die (child_die, cu);
10649 child_die = child_die->sibling;
10650 }
10651 }
10652 per_objfile->sym_cu = nullptr;
10653
10654 /* Decode macro information, if present. Dwarf 2 macro information
10655 refers to information in the line number info statement program
10656 header, so we can only read it if we've read the header
10657 successfully. */
10658 attr = dwarf2_attr (die, DW_AT_macros, cu);
10659 if (attr == NULL)
10660 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10661 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10662 {
10663 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10664 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10665
10666 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10667 }
10668 else
10669 {
10670 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10671 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10672 {
10673 unsigned int macro_offset = attr->as_unsigned ();
10674
10675 dwarf_decode_macros (cu, macro_offset, 0);
10676 }
10677 }
10678 }
10679
10680 void
10681 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10682 {
10683 struct type_unit_group *tu_group;
10684 int first_time;
10685 struct attribute *attr;
10686 unsigned int i;
10687 struct signatured_type *sig_type;
10688
10689 gdb_assert (per_cu->is_debug_types);
10690 sig_type = (struct signatured_type *) per_cu;
10691
10692 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10693
10694 /* If we're using .gdb_index (includes -readnow) then
10695 per_cu->type_unit_group may not have been set up yet. */
10696 if (sig_type->type_unit_group == NULL)
10697 sig_type->type_unit_group = get_type_unit_group (this, attr);
10698 tu_group = sig_type->type_unit_group;
10699
10700 /* If we've already processed this stmt_list there's no real need to
10701 do it again, we could fake it and just recreate the part we need
10702 (file name,index -> symtab mapping). If data shows this optimization
10703 is useful we can do it then. */
10704 type_unit_group_unshareable *tug_unshare
10705 = per_objfile->get_type_unit_group_unshareable (tu_group);
10706 first_time = tug_unshare->compunit_symtab == NULL;
10707
10708 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10709 debug info. */
10710 line_header_up lh;
10711 if (attr != NULL && attr->form_is_unsigned ())
10712 {
10713 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10714 lh = dwarf_decode_line_header (line_offset, this);
10715 }
10716 if (lh == NULL)
10717 {
10718 if (first_time)
10719 start_symtab ("", NULL, 0);
10720 else
10721 {
10722 gdb_assert (tug_unshare->symtabs == NULL);
10723 gdb_assert (m_builder == nullptr);
10724 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10725 m_builder.reset (new struct buildsym_compunit
10726 (cust->objfile (), "",
10727 cust->dirname (),
10728 compunit_language (cust),
10729 0, cust));
10730 list_in_scope = get_builder ()->get_file_symbols ();
10731 }
10732 return;
10733 }
10734
10735 line_header = lh.release ();
10736 line_header_die_owner = die;
10737
10738 if (first_time)
10739 {
10740 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10741
10742 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10743 still initializing it, and our caller (a few levels up)
10744 process_full_type_unit still needs to know if this is the first
10745 time. */
10746
10747 tug_unshare->symtabs
10748 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
10749 struct symtab *, line_header->file_names_size ());
10750
10751 auto &file_names = line_header->file_names ();
10752 for (i = 0; i < file_names.size (); ++i)
10753 {
10754 file_entry &fe = file_names[i];
10755 dwarf2_start_subfile (this, fe.name,
10756 fe.include_dir (line_header));
10757 buildsym_compunit *b = get_builder ();
10758 if (b->get_current_subfile ()->symtab == NULL)
10759 {
10760 /* NOTE: start_subfile will recognize when it's been
10761 passed a file it has already seen. So we can't
10762 assume there's a simple mapping from
10763 cu->line_header->file_names to subfiles, plus
10764 cu->line_header->file_names may contain dups. */
10765 b->get_current_subfile ()->symtab
10766 = allocate_symtab (cust, b->get_current_subfile ()->name);
10767 }
10768
10769 fe.symtab = b->get_current_subfile ()->symtab;
10770 tug_unshare->symtabs[i] = fe.symtab;
10771 }
10772 }
10773 else
10774 {
10775 gdb_assert (m_builder == nullptr);
10776 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10777 m_builder.reset (new struct buildsym_compunit
10778 (cust->objfile (), "",
10779 cust->dirname (),
10780 compunit_language (cust),
10781 0, cust));
10782 list_in_scope = get_builder ()->get_file_symbols ();
10783
10784 auto &file_names = line_header->file_names ();
10785 for (i = 0; i < file_names.size (); ++i)
10786 {
10787 file_entry &fe = file_names[i];
10788 fe.symtab = tug_unshare->symtabs[i];
10789 }
10790 }
10791
10792 /* The main symtab is allocated last. Type units don't have DW_AT_name
10793 so they don't have a "real" (so to speak) symtab anyway.
10794 There is later code that will assign the main symtab to all symbols
10795 that don't have one. We need to handle the case of a symbol with a
10796 missing symtab (DW_AT_decl_file) anyway. */
10797 }
10798
10799 /* Process DW_TAG_type_unit.
10800 For TUs we want to skip the first top level sibling if it's not the
10801 actual type being defined by this TU. In this case the first top
10802 level sibling is there to provide context only. */
10803
10804 static void
10805 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10806 {
10807 struct die_info *child_die;
10808
10809 prepare_one_comp_unit (cu, die, language_minimal);
10810
10811 /* Initialize (or reinitialize) the machinery for building symtabs.
10812 We do this before processing child DIEs, so that the line header table
10813 is available for DW_AT_decl_file. */
10814 cu->setup_type_unit_groups (die);
10815
10816 if (die->child != NULL)
10817 {
10818 child_die = die->child;
10819 while (child_die && child_die->tag)
10820 {
10821 process_die (child_die, cu);
10822 child_die = child_die->sibling;
10823 }
10824 }
10825 }
10826 \f
10827 /* DWO/DWP files.
10828
10829 http://gcc.gnu.org/wiki/DebugFission
10830 http://gcc.gnu.org/wiki/DebugFissionDWP
10831
10832 To simplify handling of both DWO files ("object" files with the DWARF info)
10833 and DWP files (a file with the DWOs packaged up into one file), we treat
10834 DWP files as having a collection of virtual DWO files. */
10835
10836 static hashval_t
10837 hash_dwo_file (const void *item)
10838 {
10839 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10840 hashval_t hash;
10841
10842 hash = htab_hash_string (dwo_file->dwo_name);
10843 if (dwo_file->comp_dir != NULL)
10844 hash += htab_hash_string (dwo_file->comp_dir);
10845 return hash;
10846 }
10847
10848 static int
10849 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10850 {
10851 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10852 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10853
10854 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10855 return 0;
10856 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10857 return lhs->comp_dir == rhs->comp_dir;
10858 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10859 }
10860
10861 /* Allocate a hash table for DWO files. */
10862
10863 static htab_up
10864 allocate_dwo_file_hash_table ()
10865 {
10866 return htab_up (htab_create_alloc (41,
10867 hash_dwo_file,
10868 eq_dwo_file,
10869 htab_delete_entry<dwo_file>,
10870 xcalloc, xfree));
10871 }
10872
10873 /* Lookup DWO file DWO_NAME. */
10874
10875 static void **
10876 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10877 const char *dwo_name,
10878 const char *comp_dir)
10879 {
10880 struct dwo_file find_entry;
10881 void **slot;
10882
10883 if (per_objfile->per_bfd->dwo_files == NULL)
10884 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10885
10886 find_entry.dwo_name = dwo_name;
10887 find_entry.comp_dir = comp_dir;
10888 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10889 INSERT);
10890
10891 return slot;
10892 }
10893
10894 static hashval_t
10895 hash_dwo_unit (const void *item)
10896 {
10897 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10898
10899 /* This drops the top 32 bits of the id, but is ok for a hash. */
10900 return dwo_unit->signature;
10901 }
10902
10903 static int
10904 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10905 {
10906 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10907 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10908
10909 /* The signature is assumed to be unique within the DWO file.
10910 So while object file CU dwo_id's always have the value zero,
10911 that's OK, assuming each object file DWO file has only one CU,
10912 and that's the rule for now. */
10913 return lhs->signature == rhs->signature;
10914 }
10915
10916 /* Allocate a hash table for DWO CUs,TUs.
10917 There is one of these tables for each of CUs,TUs for each DWO file. */
10918
10919 static htab_up
10920 allocate_dwo_unit_table ()
10921 {
10922 /* Start out with a pretty small number.
10923 Generally DWO files contain only one CU and maybe some TUs. */
10924 return htab_up (htab_create_alloc (3,
10925 hash_dwo_unit,
10926 eq_dwo_unit,
10927 NULL, xcalloc, xfree));
10928 }
10929
10930 /* die_reader_func for create_dwo_cu. */
10931
10932 static void
10933 create_dwo_cu_reader (const struct die_reader_specs *reader,
10934 const gdb_byte *info_ptr,
10935 struct die_info *comp_unit_die,
10936 struct dwo_file *dwo_file,
10937 struct dwo_unit *dwo_unit)
10938 {
10939 struct dwarf2_cu *cu = reader->cu;
10940 sect_offset sect_off = cu->per_cu->sect_off;
10941 struct dwarf2_section_info *section = cu->per_cu->section;
10942
10943 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10944 if (!signature.has_value ())
10945 {
10946 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10947 " its dwo_id [in module %s]"),
10948 sect_offset_str (sect_off), dwo_file->dwo_name);
10949 return;
10950 }
10951
10952 dwo_unit->dwo_file = dwo_file;
10953 dwo_unit->signature = *signature;
10954 dwo_unit->section = section;
10955 dwo_unit->sect_off = sect_off;
10956 dwo_unit->length = cu->per_cu->length;
10957
10958 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10959 sect_offset_str (sect_off),
10960 hex_string (dwo_unit->signature));
10961 }
10962
10963 /* Create the dwo_units for the CUs in a DWO_FILE.
10964 Note: This function processes DWO files only, not DWP files. */
10965
10966 static void
10967 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10968 dwarf2_cu *cu, struct dwo_file &dwo_file,
10969 dwarf2_section_info &section, htab_up &cus_htab)
10970 {
10971 struct objfile *objfile = per_objfile->objfile;
10972 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10973 const gdb_byte *info_ptr, *end_ptr;
10974
10975 section.read (objfile);
10976 info_ptr = section.buffer;
10977
10978 if (info_ptr == NULL)
10979 return;
10980
10981 dwarf_read_debug_printf ("Reading %s for %s:",
10982 section.get_name (),
10983 section.get_file_name ());
10984
10985 end_ptr = info_ptr + section.size;
10986 while (info_ptr < end_ptr)
10987 {
10988 struct dwarf2_per_cu_data per_cu;
10989 struct dwo_unit read_unit {};
10990 struct dwo_unit *dwo_unit;
10991 void **slot;
10992 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10993
10994 per_cu.per_bfd = per_bfd;
10995 per_cu.is_debug_types = 0;
10996 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10997 per_cu.section = &section;
10998
10999 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11000 if (!reader.dummy_p)
11001 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11002 &dwo_file, &read_unit);
11003 info_ptr += per_cu.length;
11004
11005 // If the unit could not be parsed, skip it.
11006 if (read_unit.dwo_file == NULL)
11007 continue;
11008
11009 if (cus_htab == NULL)
11010 cus_htab = allocate_dwo_unit_table ();
11011
11012 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11013 struct dwo_unit);
11014 *dwo_unit = read_unit;
11015 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11016 gdb_assert (slot != NULL);
11017 if (*slot != NULL)
11018 {
11019 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11020 sect_offset dup_sect_off = dup_cu->sect_off;
11021
11022 complaint (_("debug cu entry at offset %s is duplicate to"
11023 " the entry at offset %s, signature %s"),
11024 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11025 hex_string (dwo_unit->signature));
11026 }
11027 *slot = (void *)dwo_unit;
11028 }
11029 }
11030
11031 /* DWP file .debug_{cu,tu}_index section format:
11032 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11033 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11034
11035 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11036 officially standard DWP format was published with DWARF v5 and is called
11037 Version 5. There are no versions 3 or 4.
11038
11039 DWP Version 1:
11040
11041 Both index sections have the same format, and serve to map a 64-bit
11042 signature to a set of section numbers. Each section begins with a header,
11043 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11044 indexes, and a pool of 32-bit section numbers. The index sections will be
11045 aligned at 8-byte boundaries in the file.
11046
11047 The index section header consists of:
11048
11049 V, 32 bit version number
11050 -, 32 bits unused
11051 N, 32 bit number of compilation units or type units in the index
11052 M, 32 bit number of slots in the hash table
11053
11054 Numbers are recorded using the byte order of the application binary.
11055
11056 The hash table begins at offset 16 in the section, and consists of an array
11057 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11058 order of the application binary). Unused slots in the hash table are 0.
11059 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11060
11061 The parallel table begins immediately after the hash table
11062 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11063 array of 32-bit indexes (using the byte order of the application binary),
11064 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11065 table contains a 32-bit index into the pool of section numbers. For unused
11066 hash table slots, the corresponding entry in the parallel table will be 0.
11067
11068 The pool of section numbers begins immediately following the hash table
11069 (at offset 16 + 12 * M from the beginning of the section). The pool of
11070 section numbers consists of an array of 32-bit words (using the byte order
11071 of the application binary). Each item in the array is indexed starting
11072 from 0. The hash table entry provides the index of the first section
11073 number in the set. Additional section numbers in the set follow, and the
11074 set is terminated by a 0 entry (section number 0 is not used in ELF).
11075
11076 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11077 section must be the first entry in the set, and the .debug_abbrev.dwo must
11078 be the second entry. Other members of the set may follow in any order.
11079
11080 ---
11081
11082 DWP Versions 2 and 5:
11083
11084 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11085 and the entries in the index tables are now offsets into these sections.
11086 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11087 section.
11088
11089 Index Section Contents:
11090 Header
11091 Hash Table of Signatures dwp_hash_table.hash_table
11092 Parallel Table of Indices dwp_hash_table.unit_table
11093 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11094 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11095
11096 The index section header consists of:
11097
11098 V, 32 bit version number
11099 L, 32 bit number of columns in the table of section offsets
11100 N, 32 bit number of compilation units or type units in the index
11101 M, 32 bit number of slots in the hash table
11102
11103 Numbers are recorded using the byte order of the application binary.
11104
11105 The hash table has the same format as version 1.
11106 The parallel table of indices has the same format as version 1,
11107 except that the entries are origin-1 indices into the table of sections
11108 offsets and the table of section sizes.
11109
11110 The table of offsets begins immediately following the parallel table
11111 (at offset 16 + 12 * M from the beginning of the section). The table is
11112 a two-dimensional array of 32-bit words (using the byte order of the
11113 application binary), with L columns and N+1 rows, in row-major order.
11114 Each row in the array is indexed starting from 0. The first row provides
11115 a key to the remaining rows: each column in this row provides an identifier
11116 for a debug section, and the offsets in the same column of subsequent rows
11117 refer to that section. The section identifiers for Version 2 are:
11118
11119 DW_SECT_INFO 1 .debug_info.dwo
11120 DW_SECT_TYPES 2 .debug_types.dwo
11121 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11122 DW_SECT_LINE 4 .debug_line.dwo
11123 DW_SECT_LOC 5 .debug_loc.dwo
11124 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11125 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11126 DW_SECT_MACRO 8 .debug_macro.dwo
11127
11128 The section identifiers for Version 5 are:
11129
11130 DW_SECT_INFO_V5 1 .debug_info.dwo
11131 DW_SECT_RESERVED_V5 2 --
11132 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11133 DW_SECT_LINE_V5 4 .debug_line.dwo
11134 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11135 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11136 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11137 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11138
11139 The offsets provided by the CU and TU index sections are the base offsets
11140 for the contributions made by each CU or TU to the corresponding section
11141 in the package file. Each CU and TU header contains an abbrev_offset
11142 field, used to find the abbreviations table for that CU or TU within the
11143 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11144 be interpreted as relative to the base offset given in the index section.
11145 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11146 should be interpreted as relative to the base offset for .debug_line.dwo,
11147 and offsets into other debug sections obtained from DWARF attributes should
11148 also be interpreted as relative to the corresponding base offset.
11149
11150 The table of sizes begins immediately following the table of offsets.
11151 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11152 with L columns and N rows, in row-major order. Each row in the array is
11153 indexed starting from 1 (row 0 is shared by the two tables).
11154
11155 ---
11156
11157 Hash table lookup is handled the same in version 1 and 2:
11158
11159 We assume that N and M will not exceed 2^32 - 1.
11160 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11161
11162 Given a 64-bit compilation unit signature or a type signature S, an entry
11163 in the hash table is located as follows:
11164
11165 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11166 the low-order k bits all set to 1.
11167
11168 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11169
11170 3) If the hash table entry at index H matches the signature, use that
11171 entry. If the hash table entry at index H is unused (all zeroes),
11172 terminate the search: the signature is not present in the table.
11173
11174 4) Let H = (H + H') modulo M. Repeat at Step 3.
11175
11176 Because M > N and H' and M are relatively prime, the search is guaranteed
11177 to stop at an unused slot or find the match. */
11178
11179 /* Create a hash table to map DWO IDs to their CU/TU entry in
11180 .debug_{info,types}.dwo in DWP_FILE.
11181 Returns NULL if there isn't one.
11182 Note: This function processes DWP files only, not DWO files. */
11183
11184 static struct dwp_hash_table *
11185 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11186 struct dwp_file *dwp_file, int is_debug_types)
11187 {
11188 struct objfile *objfile = per_objfile->objfile;
11189 bfd *dbfd = dwp_file->dbfd.get ();
11190 const gdb_byte *index_ptr, *index_end;
11191 struct dwarf2_section_info *index;
11192 uint32_t version, nr_columns, nr_units, nr_slots;
11193 struct dwp_hash_table *htab;
11194
11195 if (is_debug_types)
11196 index = &dwp_file->sections.tu_index;
11197 else
11198 index = &dwp_file->sections.cu_index;
11199
11200 if (index->empty ())
11201 return NULL;
11202 index->read (objfile);
11203
11204 index_ptr = index->buffer;
11205 index_end = index_ptr + index->size;
11206
11207 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11208 For now it's safe to just read 4 bytes (particularly as it's difficult to
11209 tell if you're dealing with Version 5 before you've read the version). */
11210 version = read_4_bytes (dbfd, index_ptr);
11211 index_ptr += 4;
11212 if (version == 2 || version == 5)
11213 nr_columns = read_4_bytes (dbfd, index_ptr);
11214 else
11215 nr_columns = 0;
11216 index_ptr += 4;
11217 nr_units = read_4_bytes (dbfd, index_ptr);
11218 index_ptr += 4;
11219 nr_slots = read_4_bytes (dbfd, index_ptr);
11220 index_ptr += 4;
11221
11222 if (version != 1 && version != 2 && version != 5)
11223 {
11224 error (_("Dwarf Error: unsupported DWP file version (%s)"
11225 " [in module %s]"),
11226 pulongest (version), dwp_file->name);
11227 }
11228 if (nr_slots != (nr_slots & -nr_slots))
11229 {
11230 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11231 " is not power of 2 [in module %s]"),
11232 pulongest (nr_slots), dwp_file->name);
11233 }
11234
11235 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11236 htab->version = version;
11237 htab->nr_columns = nr_columns;
11238 htab->nr_units = nr_units;
11239 htab->nr_slots = nr_slots;
11240 htab->hash_table = index_ptr;
11241 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11242
11243 /* Exit early if the table is empty. */
11244 if (nr_slots == 0 || nr_units == 0
11245 || (version == 2 && nr_columns == 0)
11246 || (version == 5 && nr_columns == 0))
11247 {
11248 /* All must be zero. */
11249 if (nr_slots != 0 || nr_units != 0
11250 || (version == 2 && nr_columns != 0)
11251 || (version == 5 && nr_columns != 0))
11252 {
11253 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11254 " all zero [in modules %s]"),
11255 dwp_file->name);
11256 }
11257 return htab;
11258 }
11259
11260 if (version == 1)
11261 {
11262 htab->section_pool.v1.indices =
11263 htab->unit_table + sizeof (uint32_t) * nr_slots;
11264 /* It's harder to decide whether the section is too small in v1.
11265 V1 is deprecated anyway so we punt. */
11266 }
11267 else if (version == 2)
11268 {
11269 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11270 int *ids = htab->section_pool.v2.section_ids;
11271 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11272 /* Reverse map for error checking. */
11273 int ids_seen[DW_SECT_MAX + 1];
11274 int i;
11275
11276 if (nr_columns < 2)
11277 {
11278 error (_("Dwarf Error: bad DWP hash table, too few columns"
11279 " in section table [in module %s]"),
11280 dwp_file->name);
11281 }
11282 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11283 {
11284 error (_("Dwarf Error: bad DWP hash table, too many columns"
11285 " in section table [in module %s]"),
11286 dwp_file->name);
11287 }
11288 memset (ids, 255, sizeof_ids);
11289 memset (ids_seen, 255, sizeof (ids_seen));
11290 for (i = 0; i < nr_columns; ++i)
11291 {
11292 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11293
11294 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11295 {
11296 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11297 " in section table [in module %s]"),
11298 id, dwp_file->name);
11299 }
11300 if (ids_seen[id] != -1)
11301 {
11302 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11303 " id %d in section table [in module %s]"),
11304 id, dwp_file->name);
11305 }
11306 ids_seen[id] = i;
11307 ids[i] = id;
11308 }
11309 /* Must have exactly one info or types section. */
11310 if (((ids_seen[DW_SECT_INFO] != -1)
11311 + (ids_seen[DW_SECT_TYPES] != -1))
11312 != 1)
11313 {
11314 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11315 " DWO info/types section [in module %s]"),
11316 dwp_file->name);
11317 }
11318 /* Must have an abbrev section. */
11319 if (ids_seen[DW_SECT_ABBREV] == -1)
11320 {
11321 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11322 " section [in module %s]"),
11323 dwp_file->name);
11324 }
11325 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11326 htab->section_pool.v2.sizes =
11327 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11328 * nr_units * nr_columns);
11329 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11330 * nr_units * nr_columns))
11331 > index_end)
11332 {
11333 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11334 " [in module %s]"),
11335 dwp_file->name);
11336 }
11337 }
11338 else /* version == 5 */
11339 {
11340 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11341 int *ids = htab->section_pool.v5.section_ids;
11342 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11343 /* Reverse map for error checking. */
11344 int ids_seen[DW_SECT_MAX_V5 + 1];
11345
11346 if (nr_columns < 2)
11347 {
11348 error (_("Dwarf Error: bad DWP hash table, too few columns"
11349 " in section table [in module %s]"),
11350 dwp_file->name);
11351 }
11352 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11353 {
11354 error (_("Dwarf Error: bad DWP hash table, too many columns"
11355 " in section table [in module %s]"),
11356 dwp_file->name);
11357 }
11358 memset (ids, 255, sizeof_ids);
11359 memset (ids_seen, 255, sizeof (ids_seen));
11360 for (int i = 0; i < nr_columns; ++i)
11361 {
11362 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11363
11364 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11365 {
11366 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11367 " in section table [in module %s]"),
11368 id, dwp_file->name);
11369 }
11370 if (ids_seen[id] != -1)
11371 {
11372 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11373 " id %d in section table [in module %s]"),
11374 id, dwp_file->name);
11375 }
11376 ids_seen[id] = i;
11377 ids[i] = id;
11378 }
11379 /* Must have seen an info section. */
11380 if (ids_seen[DW_SECT_INFO_V5] == -1)
11381 {
11382 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11383 " DWO info/types section [in module %s]"),
11384 dwp_file->name);
11385 }
11386 /* Must have an abbrev section. */
11387 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11388 {
11389 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11390 " section [in module %s]"),
11391 dwp_file->name);
11392 }
11393 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11394 htab->section_pool.v5.sizes
11395 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11396 * nr_units * nr_columns);
11397 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11398 * nr_units * nr_columns))
11399 > index_end)
11400 {
11401 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11402 " [in module %s]"),
11403 dwp_file->name);
11404 }
11405 }
11406
11407 return htab;
11408 }
11409
11410 /* Update SECTIONS with the data from SECTP.
11411
11412 This function is like the other "locate" section routines, but in
11413 this context the sections to read comes from the DWP V1 hash table,
11414 not the full ELF section table.
11415
11416 The result is non-zero for success, or zero if an error was found. */
11417
11418 static int
11419 locate_v1_virtual_dwo_sections (asection *sectp,
11420 struct virtual_v1_dwo_sections *sections)
11421 {
11422 const struct dwop_section_names *names = &dwop_section_names;
11423
11424 if (names->abbrev_dwo.matches (sectp->name))
11425 {
11426 /* There can be only one. */
11427 if (sections->abbrev.s.section != NULL)
11428 return 0;
11429 sections->abbrev.s.section = sectp;
11430 sections->abbrev.size = bfd_section_size (sectp);
11431 }
11432 else if (names->info_dwo.matches (sectp->name)
11433 || names->types_dwo.matches (sectp->name))
11434 {
11435 /* There can be only one. */
11436 if (sections->info_or_types.s.section != NULL)
11437 return 0;
11438 sections->info_or_types.s.section = sectp;
11439 sections->info_or_types.size = bfd_section_size (sectp);
11440 }
11441 else if (names->line_dwo.matches (sectp->name))
11442 {
11443 /* There can be only one. */
11444 if (sections->line.s.section != NULL)
11445 return 0;
11446 sections->line.s.section = sectp;
11447 sections->line.size = bfd_section_size (sectp);
11448 }
11449 else if (names->loc_dwo.matches (sectp->name))
11450 {
11451 /* There can be only one. */
11452 if (sections->loc.s.section != NULL)
11453 return 0;
11454 sections->loc.s.section = sectp;
11455 sections->loc.size = bfd_section_size (sectp);
11456 }
11457 else if (names->macinfo_dwo.matches (sectp->name))
11458 {
11459 /* There can be only one. */
11460 if (sections->macinfo.s.section != NULL)
11461 return 0;
11462 sections->macinfo.s.section = sectp;
11463 sections->macinfo.size = bfd_section_size (sectp);
11464 }
11465 else if (names->macro_dwo.matches (sectp->name))
11466 {
11467 /* There can be only one. */
11468 if (sections->macro.s.section != NULL)
11469 return 0;
11470 sections->macro.s.section = sectp;
11471 sections->macro.size = bfd_section_size (sectp);
11472 }
11473 else if (names->str_offsets_dwo.matches (sectp->name))
11474 {
11475 /* There can be only one. */
11476 if (sections->str_offsets.s.section != NULL)
11477 return 0;
11478 sections->str_offsets.s.section = sectp;
11479 sections->str_offsets.size = bfd_section_size (sectp);
11480 }
11481 else
11482 {
11483 /* No other kind of section is valid. */
11484 return 0;
11485 }
11486
11487 return 1;
11488 }
11489
11490 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11491 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11492 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11493 This is for DWP version 1 files. */
11494
11495 static struct dwo_unit *
11496 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11497 struct dwp_file *dwp_file,
11498 uint32_t unit_index,
11499 const char *comp_dir,
11500 ULONGEST signature, int is_debug_types)
11501 {
11502 const struct dwp_hash_table *dwp_htab =
11503 is_debug_types ? dwp_file->tus : dwp_file->cus;
11504 bfd *dbfd = dwp_file->dbfd.get ();
11505 const char *kind = is_debug_types ? "TU" : "CU";
11506 struct dwo_file *dwo_file;
11507 struct dwo_unit *dwo_unit;
11508 struct virtual_v1_dwo_sections sections;
11509 void **dwo_file_slot;
11510 int i;
11511
11512 gdb_assert (dwp_file->version == 1);
11513
11514 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11515 kind, pulongest (unit_index), hex_string (signature),
11516 dwp_file->name);
11517
11518 /* Fetch the sections of this DWO unit.
11519 Put a limit on the number of sections we look for so that bad data
11520 doesn't cause us to loop forever. */
11521
11522 #define MAX_NR_V1_DWO_SECTIONS \
11523 (1 /* .debug_info or .debug_types */ \
11524 + 1 /* .debug_abbrev */ \
11525 + 1 /* .debug_line */ \
11526 + 1 /* .debug_loc */ \
11527 + 1 /* .debug_str_offsets */ \
11528 + 1 /* .debug_macro or .debug_macinfo */ \
11529 + 1 /* trailing zero */)
11530
11531 memset (&sections, 0, sizeof (sections));
11532
11533 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11534 {
11535 asection *sectp;
11536 uint32_t section_nr =
11537 read_4_bytes (dbfd,
11538 dwp_htab->section_pool.v1.indices
11539 + (unit_index + i) * sizeof (uint32_t));
11540
11541 if (section_nr == 0)
11542 break;
11543 if (section_nr >= dwp_file->num_sections)
11544 {
11545 error (_("Dwarf Error: bad DWP hash table, section number too large"
11546 " [in module %s]"),
11547 dwp_file->name);
11548 }
11549
11550 sectp = dwp_file->elf_sections[section_nr];
11551 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11552 {
11553 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11554 " [in module %s]"),
11555 dwp_file->name);
11556 }
11557 }
11558
11559 if (i < 2
11560 || sections.info_or_types.empty ()
11561 || sections.abbrev.empty ())
11562 {
11563 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11564 " [in module %s]"),
11565 dwp_file->name);
11566 }
11567 if (i == MAX_NR_V1_DWO_SECTIONS)
11568 {
11569 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11570 " [in module %s]"),
11571 dwp_file->name);
11572 }
11573
11574 /* It's easier for the rest of the code if we fake a struct dwo_file and
11575 have dwo_unit "live" in that. At least for now.
11576
11577 The DWP file can be made up of a random collection of CUs and TUs.
11578 However, for each CU + set of TUs that came from the same original DWO
11579 file, we can combine them back into a virtual DWO file to save space
11580 (fewer struct dwo_file objects to allocate). Remember that for really
11581 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11582
11583 std::string virtual_dwo_name =
11584 string_printf ("virtual-dwo/%d-%d-%d-%d",
11585 sections.abbrev.get_id (),
11586 sections.line.get_id (),
11587 sections.loc.get_id (),
11588 sections.str_offsets.get_id ());
11589 /* Can we use an existing virtual DWO file? */
11590 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11591 comp_dir);
11592 /* Create one if necessary. */
11593 if (*dwo_file_slot == NULL)
11594 {
11595 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11596 virtual_dwo_name.c_str ());
11597
11598 dwo_file = new struct dwo_file;
11599 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11600 dwo_file->comp_dir = comp_dir;
11601 dwo_file->sections.abbrev = sections.abbrev;
11602 dwo_file->sections.line = sections.line;
11603 dwo_file->sections.loc = sections.loc;
11604 dwo_file->sections.macinfo = sections.macinfo;
11605 dwo_file->sections.macro = sections.macro;
11606 dwo_file->sections.str_offsets = sections.str_offsets;
11607 /* The "str" section is global to the entire DWP file. */
11608 dwo_file->sections.str = dwp_file->sections.str;
11609 /* The info or types section is assigned below to dwo_unit,
11610 there's no need to record it in dwo_file.
11611 Also, we can't simply record type sections in dwo_file because
11612 we record a pointer into the vector in dwo_unit. As we collect more
11613 types we'll grow the vector and eventually have to reallocate space
11614 for it, invalidating all copies of pointers into the previous
11615 contents. */
11616 *dwo_file_slot = dwo_file;
11617 }
11618 else
11619 {
11620 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11621 virtual_dwo_name.c_str ());
11622
11623 dwo_file = (struct dwo_file *) *dwo_file_slot;
11624 }
11625
11626 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11627 dwo_unit->dwo_file = dwo_file;
11628 dwo_unit->signature = signature;
11629 dwo_unit->section =
11630 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11631 *dwo_unit->section = sections.info_or_types;
11632 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11633
11634 return dwo_unit;
11635 }
11636
11637 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11638 simplify them. Given a pointer to the containing section SECTION, and
11639 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11640 virtual section of just that piece. */
11641
11642 static struct dwarf2_section_info
11643 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11644 struct dwarf2_section_info *section,
11645 bfd_size_type offset, bfd_size_type size)
11646 {
11647 struct dwarf2_section_info result;
11648 asection *sectp;
11649
11650 gdb_assert (section != NULL);
11651 gdb_assert (!section->is_virtual);
11652
11653 memset (&result, 0, sizeof (result));
11654 result.s.containing_section = section;
11655 result.is_virtual = true;
11656
11657 if (size == 0)
11658 return result;
11659
11660 sectp = section->get_bfd_section ();
11661
11662 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11663 bounds of the real section. This is a pretty-rare event, so just
11664 flag an error (easier) instead of a warning and trying to cope. */
11665 if (sectp == NULL
11666 || offset + size > bfd_section_size (sectp))
11667 {
11668 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11669 " in section %s [in module %s]"),
11670 sectp ? bfd_section_name (sectp) : "<unknown>",
11671 objfile_name (per_objfile->objfile));
11672 }
11673
11674 result.virtual_offset = offset;
11675 result.size = size;
11676 return result;
11677 }
11678
11679 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11680 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11681 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11682 This is for DWP version 2 files. */
11683
11684 static struct dwo_unit *
11685 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11686 struct dwp_file *dwp_file,
11687 uint32_t unit_index,
11688 const char *comp_dir,
11689 ULONGEST signature, int is_debug_types)
11690 {
11691 const struct dwp_hash_table *dwp_htab =
11692 is_debug_types ? dwp_file->tus : dwp_file->cus;
11693 bfd *dbfd = dwp_file->dbfd.get ();
11694 const char *kind = is_debug_types ? "TU" : "CU";
11695 struct dwo_file *dwo_file;
11696 struct dwo_unit *dwo_unit;
11697 struct virtual_v2_or_v5_dwo_sections sections;
11698 void **dwo_file_slot;
11699 int i;
11700
11701 gdb_assert (dwp_file->version == 2);
11702
11703 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11704 kind, pulongest (unit_index), hex_string (signature),
11705 dwp_file->name);
11706
11707 /* Fetch the section offsets of this DWO unit. */
11708
11709 memset (&sections, 0, sizeof (sections));
11710
11711 for (i = 0; i < dwp_htab->nr_columns; ++i)
11712 {
11713 uint32_t offset = read_4_bytes (dbfd,
11714 dwp_htab->section_pool.v2.offsets
11715 + (((unit_index - 1) * dwp_htab->nr_columns
11716 + i)
11717 * sizeof (uint32_t)));
11718 uint32_t size = read_4_bytes (dbfd,
11719 dwp_htab->section_pool.v2.sizes
11720 + (((unit_index - 1) * dwp_htab->nr_columns
11721 + i)
11722 * sizeof (uint32_t)));
11723
11724 switch (dwp_htab->section_pool.v2.section_ids[i])
11725 {
11726 case DW_SECT_INFO:
11727 case DW_SECT_TYPES:
11728 sections.info_or_types_offset = offset;
11729 sections.info_or_types_size = size;
11730 break;
11731 case DW_SECT_ABBREV:
11732 sections.abbrev_offset = offset;
11733 sections.abbrev_size = size;
11734 break;
11735 case DW_SECT_LINE:
11736 sections.line_offset = offset;
11737 sections.line_size = size;
11738 break;
11739 case DW_SECT_LOC:
11740 sections.loc_offset = offset;
11741 sections.loc_size = size;
11742 break;
11743 case DW_SECT_STR_OFFSETS:
11744 sections.str_offsets_offset = offset;
11745 sections.str_offsets_size = size;
11746 break;
11747 case DW_SECT_MACINFO:
11748 sections.macinfo_offset = offset;
11749 sections.macinfo_size = size;
11750 break;
11751 case DW_SECT_MACRO:
11752 sections.macro_offset = offset;
11753 sections.macro_size = size;
11754 break;
11755 }
11756 }
11757
11758 /* It's easier for the rest of the code if we fake a struct dwo_file and
11759 have dwo_unit "live" in that. At least for now.
11760
11761 The DWP file can be made up of a random collection of CUs and TUs.
11762 However, for each CU + set of TUs that came from the same original DWO
11763 file, we can combine them back into a virtual DWO file to save space
11764 (fewer struct dwo_file objects to allocate). Remember that for really
11765 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11766
11767 std::string virtual_dwo_name =
11768 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11769 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11770 (long) (sections.line_size ? sections.line_offset : 0),
11771 (long) (sections.loc_size ? sections.loc_offset : 0),
11772 (long) (sections.str_offsets_size
11773 ? sections.str_offsets_offset : 0));
11774 /* Can we use an existing virtual DWO file? */
11775 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11776 comp_dir);
11777 /* Create one if necessary. */
11778 if (*dwo_file_slot == NULL)
11779 {
11780 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11781 virtual_dwo_name.c_str ());
11782
11783 dwo_file = new struct dwo_file;
11784 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11785 dwo_file->comp_dir = comp_dir;
11786 dwo_file->sections.abbrev =
11787 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11788 sections.abbrev_offset,
11789 sections.abbrev_size);
11790 dwo_file->sections.line =
11791 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11792 sections.line_offset,
11793 sections.line_size);
11794 dwo_file->sections.loc =
11795 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11796 sections.loc_offset, sections.loc_size);
11797 dwo_file->sections.macinfo =
11798 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11799 sections.macinfo_offset,
11800 sections.macinfo_size);
11801 dwo_file->sections.macro =
11802 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11803 sections.macro_offset,
11804 sections.macro_size);
11805 dwo_file->sections.str_offsets =
11806 create_dwp_v2_or_v5_section (per_objfile,
11807 &dwp_file->sections.str_offsets,
11808 sections.str_offsets_offset,
11809 sections.str_offsets_size);
11810 /* The "str" section is global to the entire DWP file. */
11811 dwo_file->sections.str = dwp_file->sections.str;
11812 /* The info or types section is assigned below to dwo_unit,
11813 there's no need to record it in dwo_file.
11814 Also, we can't simply record type sections in dwo_file because
11815 we record a pointer into the vector in dwo_unit. As we collect more
11816 types we'll grow the vector and eventually have to reallocate space
11817 for it, invalidating all copies of pointers into the previous
11818 contents. */
11819 *dwo_file_slot = dwo_file;
11820 }
11821 else
11822 {
11823 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11824 virtual_dwo_name.c_str ());
11825
11826 dwo_file = (struct dwo_file *) *dwo_file_slot;
11827 }
11828
11829 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11830 dwo_unit->dwo_file = dwo_file;
11831 dwo_unit->signature = signature;
11832 dwo_unit->section =
11833 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11834 *dwo_unit->section = create_dwp_v2_or_v5_section
11835 (per_objfile,
11836 is_debug_types
11837 ? &dwp_file->sections.types
11838 : &dwp_file->sections.info,
11839 sections.info_or_types_offset,
11840 sections.info_or_types_size);
11841 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11842
11843 return dwo_unit;
11844 }
11845
11846 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11847 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11848 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11849 This is for DWP version 5 files. */
11850
11851 static struct dwo_unit *
11852 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11853 struct dwp_file *dwp_file,
11854 uint32_t unit_index,
11855 const char *comp_dir,
11856 ULONGEST signature, int is_debug_types)
11857 {
11858 const struct dwp_hash_table *dwp_htab
11859 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11860 bfd *dbfd = dwp_file->dbfd.get ();
11861 const char *kind = is_debug_types ? "TU" : "CU";
11862 struct dwo_file *dwo_file;
11863 struct dwo_unit *dwo_unit;
11864 struct virtual_v2_or_v5_dwo_sections sections {};
11865 void **dwo_file_slot;
11866
11867 gdb_assert (dwp_file->version == 5);
11868
11869 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11870 kind, pulongest (unit_index), hex_string (signature),
11871 dwp_file->name);
11872
11873 /* Fetch the section offsets of this DWO unit. */
11874
11875 /* memset (&sections, 0, sizeof (sections)); */
11876
11877 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11878 {
11879 uint32_t offset = read_4_bytes (dbfd,
11880 dwp_htab->section_pool.v5.offsets
11881 + (((unit_index - 1)
11882 * dwp_htab->nr_columns
11883 + i)
11884 * sizeof (uint32_t)));
11885 uint32_t size = read_4_bytes (dbfd,
11886 dwp_htab->section_pool.v5.sizes
11887 + (((unit_index - 1) * dwp_htab->nr_columns
11888 + i)
11889 * sizeof (uint32_t)));
11890
11891 switch (dwp_htab->section_pool.v5.section_ids[i])
11892 {
11893 case DW_SECT_ABBREV_V5:
11894 sections.abbrev_offset = offset;
11895 sections.abbrev_size = size;
11896 break;
11897 case DW_SECT_INFO_V5:
11898 sections.info_or_types_offset = offset;
11899 sections.info_or_types_size = size;
11900 break;
11901 case DW_SECT_LINE_V5:
11902 sections.line_offset = offset;
11903 sections.line_size = size;
11904 break;
11905 case DW_SECT_LOCLISTS_V5:
11906 sections.loclists_offset = offset;
11907 sections.loclists_size = size;
11908 break;
11909 case DW_SECT_MACRO_V5:
11910 sections.macro_offset = offset;
11911 sections.macro_size = size;
11912 break;
11913 case DW_SECT_RNGLISTS_V5:
11914 sections.rnglists_offset = offset;
11915 sections.rnglists_size = size;
11916 break;
11917 case DW_SECT_STR_OFFSETS_V5:
11918 sections.str_offsets_offset = offset;
11919 sections.str_offsets_size = size;
11920 break;
11921 case DW_SECT_RESERVED_V5:
11922 default:
11923 break;
11924 }
11925 }
11926
11927 /* It's easier for the rest of the code if we fake a struct dwo_file and
11928 have dwo_unit "live" in that. At least for now.
11929
11930 The DWP file can be made up of a random collection of CUs and TUs.
11931 However, for each CU + set of TUs that came from the same original DWO
11932 file, we can combine them back into a virtual DWO file to save space
11933 (fewer struct dwo_file objects to allocate). Remember that for really
11934 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11935
11936 std::string virtual_dwo_name =
11937 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11938 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11939 (long) (sections.line_size ? sections.line_offset : 0),
11940 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11941 (long) (sections.str_offsets_size
11942 ? sections.str_offsets_offset : 0),
11943 (long) (sections.macro_size ? sections.macro_offset : 0),
11944 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11945 /* Can we use an existing virtual DWO file? */
11946 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11947 virtual_dwo_name.c_str (),
11948 comp_dir);
11949 /* Create one if necessary. */
11950 if (*dwo_file_slot == NULL)
11951 {
11952 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11953 virtual_dwo_name.c_str ());
11954
11955 dwo_file = new struct dwo_file;
11956 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11957 dwo_file->comp_dir = comp_dir;
11958 dwo_file->sections.abbrev =
11959 create_dwp_v2_or_v5_section (per_objfile,
11960 &dwp_file->sections.abbrev,
11961 sections.abbrev_offset,
11962 sections.abbrev_size);
11963 dwo_file->sections.line =
11964 create_dwp_v2_or_v5_section (per_objfile,
11965 &dwp_file->sections.line,
11966 sections.line_offset, sections.line_size);
11967 dwo_file->sections.macro =
11968 create_dwp_v2_or_v5_section (per_objfile,
11969 &dwp_file->sections.macro,
11970 sections.macro_offset,
11971 sections.macro_size);
11972 dwo_file->sections.loclists =
11973 create_dwp_v2_or_v5_section (per_objfile,
11974 &dwp_file->sections.loclists,
11975 sections.loclists_offset,
11976 sections.loclists_size);
11977 dwo_file->sections.rnglists =
11978 create_dwp_v2_or_v5_section (per_objfile,
11979 &dwp_file->sections.rnglists,
11980 sections.rnglists_offset,
11981 sections.rnglists_size);
11982 dwo_file->sections.str_offsets =
11983 create_dwp_v2_or_v5_section (per_objfile,
11984 &dwp_file->sections.str_offsets,
11985 sections.str_offsets_offset,
11986 sections.str_offsets_size);
11987 /* The "str" section is global to the entire DWP file. */
11988 dwo_file->sections.str = dwp_file->sections.str;
11989 /* The info or types section is assigned below to dwo_unit,
11990 there's no need to record it in dwo_file.
11991 Also, we can't simply record type sections in dwo_file because
11992 we record a pointer into the vector in dwo_unit. As we collect more
11993 types we'll grow the vector and eventually have to reallocate space
11994 for it, invalidating all copies of pointers into the previous
11995 contents. */
11996 *dwo_file_slot = dwo_file;
11997 }
11998 else
11999 {
12000 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12001 virtual_dwo_name.c_str ());
12002
12003 dwo_file = (struct dwo_file *) *dwo_file_slot;
12004 }
12005
12006 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12007 dwo_unit->dwo_file = dwo_file;
12008 dwo_unit->signature = signature;
12009 dwo_unit->section
12010 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12011 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12012 &dwp_file->sections.info,
12013 sections.info_or_types_offset,
12014 sections.info_or_types_size);
12015 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12016
12017 return dwo_unit;
12018 }
12019
12020 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12021 Returns NULL if the signature isn't found. */
12022
12023 static struct dwo_unit *
12024 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12025 struct dwp_file *dwp_file, const char *comp_dir,
12026 ULONGEST signature, int is_debug_types)
12027 {
12028 const struct dwp_hash_table *dwp_htab =
12029 is_debug_types ? dwp_file->tus : dwp_file->cus;
12030 bfd *dbfd = dwp_file->dbfd.get ();
12031 uint32_t mask = dwp_htab->nr_slots - 1;
12032 uint32_t hash = signature & mask;
12033 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12034 unsigned int i;
12035 void **slot;
12036 struct dwo_unit find_dwo_cu;
12037
12038 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12039 find_dwo_cu.signature = signature;
12040 slot = htab_find_slot (is_debug_types
12041 ? dwp_file->loaded_tus.get ()
12042 : dwp_file->loaded_cus.get (),
12043 &find_dwo_cu, INSERT);
12044
12045 if (*slot != NULL)
12046 return (struct dwo_unit *) *slot;
12047
12048 /* Use a for loop so that we don't loop forever on bad debug info. */
12049 for (i = 0; i < dwp_htab->nr_slots; ++i)
12050 {
12051 ULONGEST signature_in_table;
12052
12053 signature_in_table =
12054 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12055 if (signature_in_table == signature)
12056 {
12057 uint32_t unit_index =
12058 read_4_bytes (dbfd,
12059 dwp_htab->unit_table + hash * sizeof (uint32_t));
12060
12061 if (dwp_file->version == 1)
12062 {
12063 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12064 unit_index, comp_dir,
12065 signature, is_debug_types);
12066 }
12067 else if (dwp_file->version == 2)
12068 {
12069 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12070 unit_index, comp_dir,
12071 signature, is_debug_types);
12072 }
12073 else /* version == 5 */
12074 {
12075 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12076 unit_index, comp_dir,
12077 signature, is_debug_types);
12078 }
12079 return (struct dwo_unit *) *slot;
12080 }
12081 if (signature_in_table == 0)
12082 return NULL;
12083 hash = (hash + hash2) & mask;
12084 }
12085
12086 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12087 " [in module %s]"),
12088 dwp_file->name);
12089 }
12090
12091 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12092 Open the file specified by FILE_NAME and hand it off to BFD for
12093 preliminary analysis. Return a newly initialized bfd *, which
12094 includes a canonicalized copy of FILE_NAME.
12095 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12096 SEARCH_CWD is true if the current directory is to be searched.
12097 It will be searched before debug-file-directory.
12098 If successful, the file is added to the bfd include table of the
12099 objfile's bfd (see gdb_bfd_record_inclusion).
12100 If unable to find/open the file, return NULL.
12101 NOTE: This function is derived from symfile_bfd_open. */
12102
12103 static gdb_bfd_ref_ptr
12104 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12105 const char *file_name, int is_dwp, int search_cwd)
12106 {
12107 int desc;
12108 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12109 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12110 to debug_file_directory. */
12111 const char *search_path;
12112 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12113
12114 gdb::unique_xmalloc_ptr<char> search_path_holder;
12115 if (search_cwd)
12116 {
12117 if (!debug_file_directory.empty ())
12118 {
12119 search_path_holder.reset (concat (".", dirname_separator_string,
12120 debug_file_directory.c_str (),
12121 (char *) NULL));
12122 search_path = search_path_holder.get ();
12123 }
12124 else
12125 search_path = ".";
12126 }
12127 else
12128 search_path = debug_file_directory.c_str ();
12129
12130 /* Add the path for the executable binary to the list of search paths. */
12131 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12132 search_path_holder.reset (concat (objfile_dir.c_str (),
12133 dirname_separator_string,
12134 search_path, nullptr));
12135 search_path = search_path_holder.get ();
12136
12137 openp_flags flags = OPF_RETURN_REALPATH;
12138 if (is_dwp)
12139 flags |= OPF_SEARCH_IN_PATH;
12140
12141 gdb::unique_xmalloc_ptr<char> absolute_name;
12142 desc = openp (search_path, flags, file_name,
12143 O_RDONLY | O_BINARY, &absolute_name);
12144 if (desc < 0)
12145 return NULL;
12146
12147 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12148 gnutarget, desc));
12149 if (sym_bfd == NULL)
12150 return NULL;
12151 bfd_set_cacheable (sym_bfd.get (), 1);
12152
12153 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12154 return NULL;
12155
12156 /* Success. Record the bfd as having been included by the objfile's bfd.
12157 This is important because things like demangled_names_hash lives in the
12158 objfile's per_bfd space and may have references to things like symbol
12159 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12160 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12161
12162 return sym_bfd;
12163 }
12164
12165 /* Try to open DWO file FILE_NAME.
12166 COMP_DIR is the DW_AT_comp_dir attribute.
12167 The result is the bfd handle of the file.
12168 If there is a problem finding or opening the file, return NULL.
12169 Upon success, the canonicalized path of the file is stored in the bfd,
12170 same as symfile_bfd_open. */
12171
12172 static gdb_bfd_ref_ptr
12173 open_dwo_file (dwarf2_per_objfile *per_objfile,
12174 const char *file_name, const char *comp_dir)
12175 {
12176 if (IS_ABSOLUTE_PATH (file_name))
12177 return try_open_dwop_file (per_objfile, file_name,
12178 0 /*is_dwp*/, 0 /*search_cwd*/);
12179
12180 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12181
12182 if (comp_dir != NULL)
12183 {
12184 gdb::unique_xmalloc_ptr<char> path_to_try
12185 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12186
12187 /* NOTE: If comp_dir is a relative path, this will also try the
12188 search path, which seems useful. */
12189 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12190 0 /*is_dwp*/,
12191 1 /*search_cwd*/));
12192 if (abfd != NULL)
12193 return abfd;
12194 }
12195
12196 /* That didn't work, try debug-file-directory, which, despite its name,
12197 is a list of paths. */
12198
12199 if (debug_file_directory.empty ())
12200 return NULL;
12201
12202 return try_open_dwop_file (per_objfile, file_name,
12203 0 /*is_dwp*/, 1 /*search_cwd*/);
12204 }
12205
12206 /* This function is mapped across the sections and remembers the offset and
12207 size of each of the DWO debugging sections we are interested in. */
12208
12209 static void
12210 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12211 dwo_sections *dwo_sections)
12212 {
12213 const struct dwop_section_names *names = &dwop_section_names;
12214
12215 if (names->abbrev_dwo.matches (sectp->name))
12216 {
12217 dwo_sections->abbrev.s.section = sectp;
12218 dwo_sections->abbrev.size = bfd_section_size (sectp);
12219 }
12220 else if (names->info_dwo.matches (sectp->name))
12221 {
12222 dwo_sections->info.s.section = sectp;
12223 dwo_sections->info.size = bfd_section_size (sectp);
12224 }
12225 else if (names->line_dwo.matches (sectp->name))
12226 {
12227 dwo_sections->line.s.section = sectp;
12228 dwo_sections->line.size = bfd_section_size (sectp);
12229 }
12230 else if (names->loc_dwo.matches (sectp->name))
12231 {
12232 dwo_sections->loc.s.section = sectp;
12233 dwo_sections->loc.size = bfd_section_size (sectp);
12234 }
12235 else if (names->loclists_dwo.matches (sectp->name))
12236 {
12237 dwo_sections->loclists.s.section = sectp;
12238 dwo_sections->loclists.size = bfd_section_size (sectp);
12239 }
12240 else if (names->macinfo_dwo.matches (sectp->name))
12241 {
12242 dwo_sections->macinfo.s.section = sectp;
12243 dwo_sections->macinfo.size = bfd_section_size (sectp);
12244 }
12245 else if (names->macro_dwo.matches (sectp->name))
12246 {
12247 dwo_sections->macro.s.section = sectp;
12248 dwo_sections->macro.size = bfd_section_size (sectp);
12249 }
12250 else if (names->rnglists_dwo.matches (sectp->name))
12251 {
12252 dwo_sections->rnglists.s.section = sectp;
12253 dwo_sections->rnglists.size = bfd_section_size (sectp);
12254 }
12255 else if (names->str_dwo.matches (sectp->name))
12256 {
12257 dwo_sections->str.s.section = sectp;
12258 dwo_sections->str.size = bfd_section_size (sectp);
12259 }
12260 else if (names->str_offsets_dwo.matches (sectp->name))
12261 {
12262 dwo_sections->str_offsets.s.section = sectp;
12263 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12264 }
12265 else if (names->types_dwo.matches (sectp->name))
12266 {
12267 struct dwarf2_section_info type_section;
12268
12269 memset (&type_section, 0, sizeof (type_section));
12270 type_section.s.section = sectp;
12271 type_section.size = bfd_section_size (sectp);
12272 dwo_sections->types.push_back (type_section);
12273 }
12274 }
12275
12276 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12277 by PER_CU. This is for the non-DWP case.
12278 The result is NULL if DWO_NAME can't be found. */
12279
12280 static struct dwo_file *
12281 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12282 const char *comp_dir)
12283 {
12284 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12285
12286 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12287 if (dbfd == NULL)
12288 {
12289 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12290
12291 return NULL;
12292 }
12293
12294 dwo_file_up dwo_file (new struct dwo_file);
12295 dwo_file->dwo_name = dwo_name;
12296 dwo_file->comp_dir = comp_dir;
12297 dwo_file->dbfd = std::move (dbfd);
12298
12299 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12300 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12301 &dwo_file->sections);
12302
12303 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12304 dwo_file->cus);
12305
12306 if (cu->per_cu->dwarf_version < 5)
12307 {
12308 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12309 dwo_file->sections.types, dwo_file->tus);
12310 }
12311 else
12312 {
12313 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12314 &dwo_file->sections.info, dwo_file->tus,
12315 rcuh_kind::COMPILE);
12316 }
12317
12318 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12319
12320 return dwo_file.release ();
12321 }
12322
12323 /* This function is mapped across the sections and remembers the offset and
12324 size of each of the DWP debugging sections common to version 1 and 2 that
12325 we are interested in. */
12326
12327 static void
12328 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12329 dwp_file *dwp_file)
12330 {
12331 const struct dwop_section_names *names = &dwop_section_names;
12332 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12333
12334 /* Record the ELF section number for later lookup: this is what the
12335 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12336 gdb_assert (elf_section_nr < dwp_file->num_sections);
12337 dwp_file->elf_sections[elf_section_nr] = sectp;
12338
12339 /* Look for specific sections that we need. */
12340 if (names->str_dwo.matches (sectp->name))
12341 {
12342 dwp_file->sections.str.s.section = sectp;
12343 dwp_file->sections.str.size = bfd_section_size (sectp);
12344 }
12345 else if (names->cu_index.matches (sectp->name))
12346 {
12347 dwp_file->sections.cu_index.s.section = sectp;
12348 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12349 }
12350 else if (names->tu_index.matches (sectp->name))
12351 {
12352 dwp_file->sections.tu_index.s.section = sectp;
12353 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12354 }
12355 }
12356
12357 /* This function is mapped across the sections and remembers the offset and
12358 size of each of the DWP version 2 debugging sections that we are interested
12359 in. This is split into a separate function because we don't know if we
12360 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12361
12362 static void
12363 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12364 {
12365 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12366 const struct dwop_section_names *names = &dwop_section_names;
12367 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12368
12369 /* Record the ELF section number for later lookup: this is what the
12370 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12371 gdb_assert (elf_section_nr < dwp_file->num_sections);
12372 dwp_file->elf_sections[elf_section_nr] = sectp;
12373
12374 /* Look for specific sections that we need. */
12375 if (names->abbrev_dwo.matches (sectp->name))
12376 {
12377 dwp_file->sections.abbrev.s.section = sectp;
12378 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12379 }
12380 else if (names->info_dwo.matches (sectp->name))
12381 {
12382 dwp_file->sections.info.s.section = sectp;
12383 dwp_file->sections.info.size = bfd_section_size (sectp);
12384 }
12385 else if (names->line_dwo.matches (sectp->name))
12386 {
12387 dwp_file->sections.line.s.section = sectp;
12388 dwp_file->sections.line.size = bfd_section_size (sectp);
12389 }
12390 else if (names->loc_dwo.matches (sectp->name))
12391 {
12392 dwp_file->sections.loc.s.section = sectp;
12393 dwp_file->sections.loc.size = bfd_section_size (sectp);
12394 }
12395 else if (names->macinfo_dwo.matches (sectp->name))
12396 {
12397 dwp_file->sections.macinfo.s.section = sectp;
12398 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12399 }
12400 else if (names->macro_dwo.matches (sectp->name))
12401 {
12402 dwp_file->sections.macro.s.section = sectp;
12403 dwp_file->sections.macro.size = bfd_section_size (sectp);
12404 }
12405 else if (names->str_offsets_dwo.matches (sectp->name))
12406 {
12407 dwp_file->sections.str_offsets.s.section = sectp;
12408 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12409 }
12410 else if (names->types_dwo.matches (sectp->name))
12411 {
12412 dwp_file->sections.types.s.section = sectp;
12413 dwp_file->sections.types.size = bfd_section_size (sectp);
12414 }
12415 }
12416
12417 /* This function is mapped across the sections and remembers the offset and
12418 size of each of the DWP version 5 debugging sections that we are interested
12419 in. This is split into a separate function because we don't know if we
12420 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12421
12422 static void
12423 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12424 {
12425 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12426 const struct dwop_section_names *names = &dwop_section_names;
12427 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12428
12429 /* Record the ELF section number for later lookup: this is what the
12430 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12431 gdb_assert (elf_section_nr < dwp_file->num_sections);
12432 dwp_file->elf_sections[elf_section_nr] = sectp;
12433
12434 /* Look for specific sections that we need. */
12435 if (names->abbrev_dwo.matches (sectp->name))
12436 {
12437 dwp_file->sections.abbrev.s.section = sectp;
12438 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12439 }
12440 else if (names->info_dwo.matches (sectp->name))
12441 {
12442 dwp_file->sections.info.s.section = sectp;
12443 dwp_file->sections.info.size = bfd_section_size (sectp);
12444 }
12445 else if (names->line_dwo.matches (sectp->name))
12446 {
12447 dwp_file->sections.line.s.section = sectp;
12448 dwp_file->sections.line.size = bfd_section_size (sectp);
12449 }
12450 else if (names->loclists_dwo.matches (sectp->name))
12451 {
12452 dwp_file->sections.loclists.s.section = sectp;
12453 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12454 }
12455 else if (names->macro_dwo.matches (sectp->name))
12456 {
12457 dwp_file->sections.macro.s.section = sectp;
12458 dwp_file->sections.macro.size = bfd_section_size (sectp);
12459 }
12460 else if (names->rnglists_dwo.matches (sectp->name))
12461 {
12462 dwp_file->sections.rnglists.s.section = sectp;
12463 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12464 }
12465 else if (names->str_offsets_dwo.matches (sectp->name))
12466 {
12467 dwp_file->sections.str_offsets.s.section = sectp;
12468 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12469 }
12470 }
12471
12472 /* Hash function for dwp_file loaded CUs/TUs. */
12473
12474 static hashval_t
12475 hash_dwp_loaded_cutus (const void *item)
12476 {
12477 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12478
12479 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12480 return dwo_unit->signature;
12481 }
12482
12483 /* Equality function for dwp_file loaded CUs/TUs. */
12484
12485 static int
12486 eq_dwp_loaded_cutus (const void *a, const void *b)
12487 {
12488 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12489 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12490
12491 return dua->signature == dub->signature;
12492 }
12493
12494 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12495
12496 static htab_up
12497 allocate_dwp_loaded_cutus_table ()
12498 {
12499 return htab_up (htab_create_alloc (3,
12500 hash_dwp_loaded_cutus,
12501 eq_dwp_loaded_cutus,
12502 NULL, xcalloc, xfree));
12503 }
12504
12505 /* Try to open DWP file FILE_NAME.
12506 The result is the bfd handle of the file.
12507 If there is a problem finding or opening the file, return NULL.
12508 Upon success, the canonicalized path of the file is stored in the bfd,
12509 same as symfile_bfd_open. */
12510
12511 static gdb_bfd_ref_ptr
12512 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12513 {
12514 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12515 1 /*is_dwp*/,
12516 1 /*search_cwd*/));
12517 if (abfd != NULL)
12518 return abfd;
12519
12520 /* Work around upstream bug 15652.
12521 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12522 [Whether that's a "bug" is debatable, but it is getting in our way.]
12523 We have no real idea where the dwp file is, because gdb's realpath-ing
12524 of the executable's path may have discarded the needed info.
12525 [IWBN if the dwp file name was recorded in the executable, akin to
12526 .gnu_debuglink, but that doesn't exist yet.]
12527 Strip the directory from FILE_NAME and search again. */
12528 if (!debug_file_directory.empty ())
12529 {
12530 /* Don't implicitly search the current directory here.
12531 If the user wants to search "." to handle this case,
12532 it must be added to debug-file-directory. */
12533 return try_open_dwop_file (per_objfile, lbasename (file_name),
12534 1 /*is_dwp*/,
12535 0 /*search_cwd*/);
12536 }
12537
12538 return NULL;
12539 }
12540
12541 /* Initialize the use of the DWP file for the current objfile.
12542 By convention the name of the DWP file is ${objfile}.dwp.
12543 The result is NULL if it can't be found. */
12544
12545 static std::unique_ptr<struct dwp_file>
12546 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12547 {
12548 struct objfile *objfile = per_objfile->objfile;
12549
12550 /* Try to find first .dwp for the binary file before any symbolic links
12551 resolving. */
12552
12553 /* If the objfile is a debug file, find the name of the real binary
12554 file and get the name of dwp file from there. */
12555 std::string dwp_name;
12556 if (objfile->separate_debug_objfile_backlink != NULL)
12557 {
12558 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12559 const char *backlink_basename = lbasename (backlink->original_name);
12560
12561 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12562 }
12563 else
12564 dwp_name = objfile->original_name;
12565
12566 dwp_name += ".dwp";
12567
12568 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12569 if (dbfd == NULL
12570 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12571 {
12572 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12573 dwp_name = objfile_name (objfile);
12574 dwp_name += ".dwp";
12575 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12576 }
12577
12578 if (dbfd == NULL)
12579 {
12580 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12581
12582 return std::unique_ptr<dwp_file> ();
12583 }
12584
12585 const char *name = bfd_get_filename (dbfd.get ());
12586 std::unique_ptr<struct dwp_file> dwp_file
12587 (new struct dwp_file (name, std::move (dbfd)));
12588
12589 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12590 dwp_file->elf_sections =
12591 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12592 dwp_file->num_sections, asection *);
12593
12594 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12595 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12596 dwp_file.get ());
12597
12598 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12599
12600 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12601
12602 /* The DWP file version is stored in the hash table. Oh well. */
12603 if (dwp_file->cus && dwp_file->tus
12604 && dwp_file->cus->version != dwp_file->tus->version)
12605 {
12606 /* Technically speaking, we should try to limp along, but this is
12607 pretty bizarre. We use pulongest here because that's the established
12608 portability solution (e.g, we cannot use %u for uint32_t). */
12609 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12610 " TU version %s [in DWP file %s]"),
12611 pulongest (dwp_file->cus->version),
12612 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12613 }
12614
12615 if (dwp_file->cus)
12616 dwp_file->version = dwp_file->cus->version;
12617 else if (dwp_file->tus)
12618 dwp_file->version = dwp_file->tus->version;
12619 else
12620 dwp_file->version = 2;
12621
12622 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12623 {
12624 if (dwp_file->version == 2)
12625 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12626 dwp_file.get ());
12627 else
12628 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12629 dwp_file.get ());
12630 }
12631
12632 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12633 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12634
12635 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12636 dwarf_read_debug_printf (" %s CUs, %s TUs",
12637 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12638 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12639
12640 return dwp_file;
12641 }
12642
12643 /* Wrapper around open_and_init_dwp_file, only open it once. */
12644
12645 static struct dwp_file *
12646 get_dwp_file (dwarf2_per_objfile *per_objfile)
12647 {
12648 if (!per_objfile->per_bfd->dwp_checked)
12649 {
12650 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12651 per_objfile->per_bfd->dwp_checked = 1;
12652 }
12653 return per_objfile->per_bfd->dwp_file.get ();
12654 }
12655
12656 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12657 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12658 or in the DWP file for the objfile, referenced by THIS_UNIT.
12659 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12660 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12661
12662 This is called, for example, when wanting to read a variable with a
12663 complex location. Therefore we don't want to do file i/o for every call.
12664 Therefore we don't want to look for a DWO file on every call.
12665 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12666 then we check if we've already seen DWO_NAME, and only THEN do we check
12667 for a DWO file.
12668
12669 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12670 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12671
12672 static struct dwo_unit *
12673 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12674 ULONGEST signature, int is_debug_types)
12675 {
12676 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12677 struct objfile *objfile = per_objfile->objfile;
12678 const char *kind = is_debug_types ? "TU" : "CU";
12679 void **dwo_file_slot;
12680 struct dwo_file *dwo_file;
12681 struct dwp_file *dwp_file;
12682
12683 /* First see if there's a DWP file.
12684 If we have a DWP file but didn't find the DWO inside it, don't
12685 look for the original DWO file. It makes gdb behave differently
12686 depending on whether one is debugging in the build tree. */
12687
12688 dwp_file = get_dwp_file (per_objfile);
12689 if (dwp_file != NULL)
12690 {
12691 const struct dwp_hash_table *dwp_htab =
12692 is_debug_types ? dwp_file->tus : dwp_file->cus;
12693
12694 if (dwp_htab != NULL)
12695 {
12696 struct dwo_unit *dwo_cutu =
12697 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12698 is_debug_types);
12699
12700 if (dwo_cutu != NULL)
12701 {
12702 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12703 kind, hex_string (signature),
12704 host_address_to_string (dwo_cutu));
12705
12706 return dwo_cutu;
12707 }
12708 }
12709 }
12710 else
12711 {
12712 /* No DWP file, look for the DWO file. */
12713
12714 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12715 if (*dwo_file_slot == NULL)
12716 {
12717 /* Read in the file and build a table of the CUs/TUs it contains. */
12718 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12719 }
12720 /* NOTE: This will be NULL if unable to open the file. */
12721 dwo_file = (struct dwo_file *) *dwo_file_slot;
12722
12723 if (dwo_file != NULL)
12724 {
12725 struct dwo_unit *dwo_cutu = NULL;
12726
12727 if (is_debug_types && dwo_file->tus)
12728 {
12729 struct dwo_unit find_dwo_cutu;
12730
12731 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12732 find_dwo_cutu.signature = signature;
12733 dwo_cutu
12734 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12735 &find_dwo_cutu);
12736 }
12737 else if (!is_debug_types && dwo_file->cus)
12738 {
12739 struct dwo_unit find_dwo_cutu;
12740
12741 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12742 find_dwo_cutu.signature = signature;
12743 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12744 &find_dwo_cutu);
12745 }
12746
12747 if (dwo_cutu != NULL)
12748 {
12749 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12750 kind, dwo_name, hex_string (signature),
12751 host_address_to_string (dwo_cutu));
12752
12753 return dwo_cutu;
12754 }
12755 }
12756 }
12757
12758 /* We didn't find it. This could mean a dwo_id mismatch, or
12759 someone deleted the DWO/DWP file, or the search path isn't set up
12760 correctly to find the file. */
12761
12762 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12763 kind, dwo_name, hex_string (signature));
12764
12765 /* This is a warning and not a complaint because it can be caused by
12766 pilot error (e.g., user accidentally deleting the DWO). */
12767 {
12768 /* Print the name of the DWP file if we looked there, helps the user
12769 better diagnose the problem. */
12770 std::string dwp_text;
12771
12772 if (dwp_file != NULL)
12773 dwp_text = string_printf (" [in DWP file %s]",
12774 lbasename (dwp_file->name));
12775
12776 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12777 " [in module %s]"),
12778 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12779 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12780 }
12781 return NULL;
12782 }
12783
12784 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12785 See lookup_dwo_cutu_unit for details. */
12786
12787 static struct dwo_unit *
12788 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12789 ULONGEST signature)
12790 {
12791 gdb_assert (!cu->per_cu->is_debug_types);
12792
12793 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12794 }
12795
12796 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12797 See lookup_dwo_cutu_unit for details. */
12798
12799 static struct dwo_unit *
12800 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12801 {
12802 gdb_assert (cu->per_cu->is_debug_types);
12803
12804 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12805
12806 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12807 }
12808
12809 /* Traversal function for queue_and_load_all_dwo_tus. */
12810
12811 static int
12812 queue_and_load_dwo_tu (void **slot, void *info)
12813 {
12814 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12815 dwarf2_cu *cu = (dwarf2_cu *) info;
12816 ULONGEST signature = dwo_unit->signature;
12817 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12818
12819 if (sig_type != NULL)
12820 {
12821 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12822 a real dependency of PER_CU on SIG_TYPE. That is detected later
12823 while processing PER_CU. */
12824 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12825 cu->per_cu->lang))
12826 load_full_type_unit (sig_type, cu->per_objfile);
12827 cu->per_cu->imported_symtabs_push (sig_type);
12828 }
12829
12830 return 1;
12831 }
12832
12833 /* Queue all TUs contained in the DWO of CU to be read in.
12834 The DWO may have the only definition of the type, though it may not be
12835 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12836 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12837
12838 static void
12839 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12840 {
12841 struct dwo_unit *dwo_unit;
12842 struct dwo_file *dwo_file;
12843
12844 gdb_assert (cu != nullptr);
12845 gdb_assert (!cu->per_cu->is_debug_types);
12846 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12847
12848 dwo_unit = cu->dwo_unit;
12849 gdb_assert (dwo_unit != NULL);
12850
12851 dwo_file = dwo_unit->dwo_file;
12852 if (dwo_file->tus != NULL)
12853 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12854 }
12855
12856 /* Read in various DIEs. */
12857
12858 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12859 Inherit only the children of the DW_AT_abstract_origin DIE not being
12860 already referenced by DW_AT_abstract_origin from the children of the
12861 current DIE. */
12862
12863 static void
12864 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12865 {
12866 struct die_info *child_die;
12867 sect_offset *offsetp;
12868 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12869 struct die_info *origin_die;
12870 /* Iterator of the ORIGIN_DIE children. */
12871 struct die_info *origin_child_die;
12872 struct attribute *attr;
12873 struct dwarf2_cu *origin_cu;
12874 struct pending **origin_previous_list_in_scope;
12875
12876 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12877 if (!attr)
12878 return;
12879
12880 /* Note that following die references may follow to a die in a
12881 different cu. */
12882
12883 origin_cu = cu;
12884 origin_die = follow_die_ref (die, attr, &origin_cu);
12885
12886 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12887 symbols in. */
12888 origin_previous_list_in_scope = origin_cu->list_in_scope;
12889 origin_cu->list_in_scope = cu->list_in_scope;
12890
12891 if (die->tag != origin_die->tag
12892 && !(die->tag == DW_TAG_inlined_subroutine
12893 && origin_die->tag == DW_TAG_subprogram))
12894 complaint (_("DIE %s and its abstract origin %s have different tags"),
12895 sect_offset_str (die->sect_off),
12896 sect_offset_str (origin_die->sect_off));
12897
12898 /* Find if the concrete and abstract trees are structurally the
12899 same. This is a shallow traversal and it is not bullet-proof;
12900 the compiler can trick the debugger into believing that the trees
12901 are isomorphic, whereas they actually are not. However, the
12902 likelyhood of this happening is pretty low, and a full-fledged
12903 check would be an overkill. */
12904 bool are_isomorphic = true;
12905 die_info *concrete_child = die->child;
12906 die_info *abstract_child = origin_die->child;
12907 while (concrete_child != nullptr || abstract_child != nullptr)
12908 {
12909 if (concrete_child == nullptr
12910 || abstract_child == nullptr
12911 || concrete_child->tag != abstract_child->tag)
12912 {
12913 are_isomorphic = false;
12914 break;
12915 }
12916
12917 concrete_child = concrete_child->sibling;
12918 abstract_child = abstract_child->sibling;
12919 }
12920
12921 /* Walk the origin's children in parallel to the concrete children.
12922 This helps match an origin child in case the debug info misses
12923 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12924 origin tree may not have the same tree structure as the concrete
12925 DIE, though. */
12926 die_info *corresponding_abstract_child
12927 = are_isomorphic ? origin_die->child : nullptr;
12928
12929 std::vector<sect_offset> offsets;
12930
12931 for (child_die = die->child;
12932 child_die && child_die->tag;
12933 child_die = child_die->sibling)
12934 {
12935 struct die_info *child_origin_die;
12936 struct dwarf2_cu *child_origin_cu;
12937
12938 /* We are trying to process concrete instance entries:
12939 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12940 it's not relevant to our analysis here. i.e. detecting DIEs that are
12941 present in the abstract instance but not referenced in the concrete
12942 one. */
12943 if (child_die->tag == DW_TAG_call_site
12944 || child_die->tag == DW_TAG_GNU_call_site)
12945 {
12946 if (are_isomorphic)
12947 corresponding_abstract_child
12948 = corresponding_abstract_child->sibling;
12949 continue;
12950 }
12951
12952 /* For each CHILD_DIE, find the corresponding child of
12953 ORIGIN_DIE. If there is more than one layer of
12954 DW_AT_abstract_origin, follow them all; there shouldn't be,
12955 but GCC versions at least through 4.4 generate this (GCC PR
12956 40573). */
12957 child_origin_die = child_die;
12958 child_origin_cu = cu;
12959 while (1)
12960 {
12961 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12962 child_origin_cu);
12963 if (attr == NULL)
12964 break;
12965 child_origin_die = follow_die_ref (child_origin_die, attr,
12966 &child_origin_cu);
12967 }
12968
12969 /* If missing DW_AT_abstract_origin, try the corresponding child
12970 of the origin. Clang emits such lexical scopes. */
12971 if (child_origin_die == child_die
12972 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12973 && are_isomorphic
12974 && child_die->tag == DW_TAG_lexical_block)
12975 child_origin_die = corresponding_abstract_child;
12976
12977 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12978 counterpart may exist. */
12979 if (child_origin_die != child_die)
12980 {
12981 if (child_die->tag != child_origin_die->tag
12982 && !(child_die->tag == DW_TAG_inlined_subroutine
12983 && child_origin_die->tag == DW_TAG_subprogram))
12984 complaint (_("Child DIE %s and its abstract origin %s have "
12985 "different tags"),
12986 sect_offset_str (child_die->sect_off),
12987 sect_offset_str (child_origin_die->sect_off));
12988 if (child_origin_die->parent != origin_die)
12989 complaint (_("Child DIE %s and its abstract origin %s have "
12990 "different parents"),
12991 sect_offset_str (child_die->sect_off),
12992 sect_offset_str (child_origin_die->sect_off));
12993 else
12994 offsets.push_back (child_origin_die->sect_off);
12995 }
12996
12997 if (are_isomorphic)
12998 corresponding_abstract_child = corresponding_abstract_child->sibling;
12999 }
13000 std::sort (offsets.begin (), offsets.end ());
13001 sect_offset *offsets_end = offsets.data () + offsets.size ();
13002 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13003 if (offsetp[-1] == *offsetp)
13004 complaint (_("Multiple children of DIE %s refer "
13005 "to DIE %s as their abstract origin"),
13006 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13007
13008 offsetp = offsets.data ();
13009 origin_child_die = origin_die->child;
13010 while (origin_child_die && origin_child_die->tag)
13011 {
13012 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13013 while (offsetp < offsets_end
13014 && *offsetp < origin_child_die->sect_off)
13015 offsetp++;
13016 if (offsetp >= offsets_end
13017 || *offsetp > origin_child_die->sect_off)
13018 {
13019 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13020 Check whether we're already processing ORIGIN_CHILD_DIE.
13021 This can happen with mutually referenced abstract_origins.
13022 PR 16581. */
13023 if (!origin_child_die->in_process)
13024 process_die (origin_child_die, origin_cu);
13025 }
13026 origin_child_die = origin_child_die->sibling;
13027 }
13028 origin_cu->list_in_scope = origin_previous_list_in_scope;
13029
13030 if (cu != origin_cu)
13031 compute_delayed_physnames (origin_cu);
13032 }
13033
13034 static void
13035 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13036 {
13037 struct objfile *objfile = cu->per_objfile->objfile;
13038 struct gdbarch *gdbarch = objfile->arch ();
13039 struct context_stack *newobj;
13040 CORE_ADDR lowpc;
13041 CORE_ADDR highpc;
13042 struct die_info *child_die;
13043 struct attribute *attr, *call_line, *call_file;
13044 const char *name;
13045 CORE_ADDR baseaddr;
13046 struct block *block;
13047 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13048 std::vector<struct symbol *> template_args;
13049 struct template_symbol *templ_func = NULL;
13050
13051 if (inlined_func)
13052 {
13053 /* If we do not have call site information, we can't show the
13054 caller of this inlined function. That's too confusing, so
13055 only use the scope for local variables. */
13056 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13057 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13058 if (call_line == NULL || call_file == NULL)
13059 {
13060 read_lexical_block_scope (die, cu);
13061 return;
13062 }
13063 }
13064
13065 baseaddr = objfile->text_section_offset ();
13066
13067 name = dwarf2_name (die, cu);
13068
13069 /* Ignore functions with missing or empty names. These are actually
13070 illegal according to the DWARF standard. */
13071 if (name == NULL)
13072 {
13073 complaint (_("missing name for subprogram DIE at %s"),
13074 sect_offset_str (die->sect_off));
13075 return;
13076 }
13077
13078 /* Ignore functions with missing or invalid low and high pc attributes. */
13079 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13080 <= PC_BOUNDS_INVALID)
13081 {
13082 attr = dwarf2_attr (die, DW_AT_external, cu);
13083 if (attr == nullptr || !attr->as_boolean ())
13084 complaint (_("cannot get low and high bounds "
13085 "for subprogram DIE at %s"),
13086 sect_offset_str (die->sect_off));
13087 return;
13088 }
13089
13090 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13091 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13092
13093 /* If we have any template arguments, then we must allocate a
13094 different sort of symbol. */
13095 for (child_die = die->child; child_die; child_die = child_die->sibling)
13096 {
13097 if (child_die->tag == DW_TAG_template_type_param
13098 || child_die->tag == DW_TAG_template_value_param)
13099 {
13100 templ_func = new (&objfile->objfile_obstack) template_symbol;
13101 templ_func->subclass = SYMBOL_TEMPLATE;
13102 break;
13103 }
13104 }
13105
13106 gdb_assert (cu->get_builder () != nullptr);
13107 newobj = cu->get_builder ()->push_context (0, lowpc);
13108 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13109 (struct symbol *) templ_func);
13110
13111 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13112 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13113 cu->per_cu->lang);
13114
13115 /* If there is a location expression for DW_AT_frame_base, record
13116 it. */
13117 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13118 if (attr != nullptr)
13119 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13120
13121 /* If there is a location for the static link, record it. */
13122 newobj->static_link = NULL;
13123 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13124 if (attr != nullptr)
13125 {
13126 newobj->static_link
13127 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13128 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13129 cu->addr_type ());
13130 }
13131
13132 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13133
13134 if (die->child != NULL)
13135 {
13136 child_die = die->child;
13137 while (child_die && child_die->tag)
13138 {
13139 if (child_die->tag == DW_TAG_template_type_param
13140 || child_die->tag == DW_TAG_template_value_param)
13141 {
13142 struct symbol *arg = new_symbol (child_die, NULL, cu);
13143
13144 if (arg != NULL)
13145 template_args.push_back (arg);
13146 }
13147 else
13148 process_die (child_die, cu);
13149 child_die = child_die->sibling;
13150 }
13151 }
13152
13153 inherit_abstract_dies (die, cu);
13154
13155 /* If we have a DW_AT_specification, we might need to import using
13156 directives from the context of the specification DIE. See the
13157 comment in determine_prefix. */
13158 if (cu->per_cu->lang == language_cplus
13159 && dwarf2_attr (die, DW_AT_specification, cu))
13160 {
13161 struct dwarf2_cu *spec_cu = cu;
13162 struct die_info *spec_die = die_specification (die, &spec_cu);
13163
13164 while (spec_die)
13165 {
13166 child_die = spec_die->child;
13167 while (child_die && child_die->tag)
13168 {
13169 if (child_die->tag == DW_TAG_imported_module)
13170 process_die (child_die, spec_cu);
13171 child_die = child_die->sibling;
13172 }
13173
13174 /* In some cases, GCC generates specification DIEs that
13175 themselves contain DW_AT_specification attributes. */
13176 spec_die = die_specification (spec_die, &spec_cu);
13177 }
13178 }
13179
13180 struct context_stack cstk = cu->get_builder ()->pop_context ();
13181 /* Make a block for the local symbols within. */
13182 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13183 cstk.static_link, lowpc, highpc);
13184
13185 /* For C++, set the block's scope. */
13186 if ((cu->per_cu->lang == language_cplus
13187 || cu->per_cu->lang == language_fortran
13188 || cu->per_cu->lang == language_d
13189 || cu->per_cu->lang == language_rust)
13190 && cu->processing_has_namespace_info)
13191 block_set_scope (block, determine_prefix (die, cu),
13192 &objfile->objfile_obstack);
13193
13194 /* If we have address ranges, record them. */
13195 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13196
13197 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13198
13199 /* Attach template arguments to function. */
13200 if (!template_args.empty ())
13201 {
13202 gdb_assert (templ_func != NULL);
13203
13204 templ_func->n_template_arguments = template_args.size ();
13205 templ_func->template_arguments
13206 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13207 templ_func->n_template_arguments);
13208 memcpy (templ_func->template_arguments,
13209 template_args.data (),
13210 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13211
13212 /* Make sure that the symtab is set on the new symbols. Even
13213 though they don't appear in this symtab directly, other parts
13214 of gdb assume that symbols do, and this is reasonably
13215 true. */
13216 for (symbol *sym : template_args)
13217 symbol_set_symtab (sym, symbol_symtab (templ_func));
13218 }
13219
13220 /* In C++, we can have functions nested inside functions (e.g., when
13221 a function declares a class that has methods). This means that
13222 when we finish processing a function scope, we may need to go
13223 back to building a containing block's symbol lists. */
13224 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13225 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13226
13227 /* If we've finished processing a top-level function, subsequent
13228 symbols go in the file symbol list. */
13229 if (cu->get_builder ()->outermost_context_p ())
13230 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13231 }
13232
13233 /* Process all the DIES contained within a lexical block scope. Start
13234 a new scope, process the dies, and then close the scope. */
13235
13236 static void
13237 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13238 {
13239 struct objfile *objfile = cu->per_objfile->objfile;
13240 struct gdbarch *gdbarch = objfile->arch ();
13241 CORE_ADDR lowpc, highpc;
13242 struct die_info *child_die;
13243 CORE_ADDR baseaddr;
13244
13245 baseaddr = objfile->text_section_offset ();
13246
13247 /* Ignore blocks with missing or invalid low and high pc attributes. */
13248 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13249 as multiple lexical blocks? Handling children in a sane way would
13250 be nasty. Might be easier to properly extend generic blocks to
13251 describe ranges. */
13252 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13253 {
13254 case PC_BOUNDS_NOT_PRESENT:
13255 /* DW_TAG_lexical_block has no attributes, process its children as if
13256 there was no wrapping by that DW_TAG_lexical_block.
13257 GCC does no longer produces such DWARF since GCC r224161. */
13258 for (child_die = die->child;
13259 child_die != NULL && child_die->tag;
13260 child_die = child_die->sibling)
13261 {
13262 /* We might already be processing this DIE. This can happen
13263 in an unusual circumstance -- where a subroutine A
13264 appears lexically in another subroutine B, but A actually
13265 inlines B. The recursion is broken here, rather than in
13266 inherit_abstract_dies, because it seems better to simply
13267 drop concrete children here. */
13268 if (!child_die->in_process)
13269 process_die (child_die, cu);
13270 }
13271 return;
13272 case PC_BOUNDS_INVALID:
13273 return;
13274 }
13275 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13276 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13277
13278 cu->get_builder ()->push_context (0, lowpc);
13279 if (die->child != NULL)
13280 {
13281 child_die = die->child;
13282 while (child_die && child_die->tag)
13283 {
13284 process_die (child_die, cu);
13285 child_die = child_die->sibling;
13286 }
13287 }
13288 inherit_abstract_dies (die, cu);
13289 struct context_stack cstk = cu->get_builder ()->pop_context ();
13290
13291 if (*cu->get_builder ()->get_local_symbols () != NULL
13292 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13293 {
13294 struct block *block
13295 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13296 cstk.start_addr, highpc);
13297
13298 /* Note that recording ranges after traversing children, as we
13299 do here, means that recording a parent's ranges entails
13300 walking across all its children's ranges as they appear in
13301 the address map, which is quadratic behavior.
13302
13303 It would be nicer to record the parent's ranges before
13304 traversing its children, simply overriding whatever you find
13305 there. But since we don't even decide whether to create a
13306 block until after we've traversed its children, that's hard
13307 to do. */
13308 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13309 }
13310 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13311 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13312 }
13313
13314 static void dwarf2_ranges_read_low_addrs (unsigned offset,
13315 struct dwarf2_cu *cu,
13316 dwarf_tag tag,
13317 std::vector<CORE_ADDR> &result);
13318
13319 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13320
13321 static void
13322 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13323 {
13324 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13325 struct objfile *objfile = per_objfile->objfile;
13326 struct gdbarch *gdbarch = objfile->arch ();
13327 CORE_ADDR pc, baseaddr;
13328 struct attribute *attr;
13329 void **slot;
13330 int nparams;
13331 struct die_info *child_die;
13332
13333 baseaddr = objfile->text_section_offset ();
13334
13335 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13336 if (attr == NULL)
13337 {
13338 /* This was a pre-DWARF-5 GNU extension alias
13339 for DW_AT_call_return_pc. */
13340 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13341 }
13342 if (!attr)
13343 {
13344 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13345 "DIE %s [in module %s]"),
13346 sect_offset_str (die->sect_off), objfile_name (objfile));
13347 return;
13348 }
13349 pc = attr->as_address () + baseaddr;
13350 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13351 pc -= baseaddr;
13352
13353 if (cu->call_site_htab == NULL)
13354 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
13355 call_site::eq, NULL,
13356 &objfile->objfile_obstack,
13357 hashtab_obstack_allocate, NULL);
13358 struct call_site call_site_local (pc, nullptr, nullptr);
13359 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13360 if (*slot != NULL)
13361 {
13362 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13363 "DIE %s [in module %s]"),
13364 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13365 objfile_name (objfile));
13366 return;
13367 }
13368
13369 /* Count parameters at the caller. */
13370
13371 nparams = 0;
13372 for (child_die = die->child; child_die && child_die->tag;
13373 child_die = child_die->sibling)
13374 {
13375 if (child_die->tag != DW_TAG_call_site_parameter
13376 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13377 {
13378 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13379 "DW_TAG_call_site child DIE %s [in module %s]"),
13380 child_die->tag, sect_offset_str (child_die->sect_off),
13381 objfile_name (objfile));
13382 continue;
13383 }
13384
13385 nparams++;
13386 }
13387
13388 struct call_site *call_site
13389 = new (XOBNEWVAR (&objfile->objfile_obstack,
13390 struct call_site,
13391 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
13392 struct call_site (pc, cu->per_cu, per_objfile);
13393 *slot = call_site;
13394
13395 /* We never call the destructor of call_site, so we must ensure it is
13396 trivially destructible. */
13397 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
13398
13399 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13400 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13401 {
13402 struct die_info *func_die;
13403
13404 /* Skip also over DW_TAG_inlined_subroutine. */
13405 for (func_die = die->parent;
13406 func_die && func_die->tag != DW_TAG_subprogram
13407 && func_die->tag != DW_TAG_subroutine_type;
13408 func_die = func_die->parent);
13409
13410 /* DW_AT_call_all_calls is a superset
13411 of DW_AT_call_all_tail_calls. */
13412 if (func_die
13413 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13414 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13415 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13416 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13417 {
13418 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13419 not complete. But keep CALL_SITE for look ups via call_site_htab,
13420 both the initial caller containing the real return address PC and
13421 the final callee containing the current PC of a chain of tail
13422 calls do not need to have the tail call list complete. But any
13423 function candidate for a virtual tail call frame searched via
13424 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13425 determined unambiguously. */
13426 }
13427 else
13428 {
13429 struct type *func_type = NULL;
13430
13431 if (func_die)
13432 func_type = get_die_type (func_die, cu);
13433 if (func_type != NULL)
13434 {
13435 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13436
13437 /* Enlist this call site to the function. */
13438 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13439 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13440 }
13441 else
13442 complaint (_("Cannot find function owning DW_TAG_call_site "
13443 "DIE %s [in module %s]"),
13444 sect_offset_str (die->sect_off), objfile_name (objfile));
13445 }
13446 }
13447
13448 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13449 if (attr == NULL)
13450 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13451 if (attr == NULL)
13452 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13453 if (attr == NULL)
13454 {
13455 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13456 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13457 }
13458
13459 call_site->target.set_loc_dwarf_block (nullptr);
13460 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13461 /* Keep NULL DWARF_BLOCK. */;
13462 else if (attr->form_is_block ())
13463 {
13464 struct dwarf2_locexpr_baton *dlbaton;
13465 struct dwarf_block *block = attr->as_block ();
13466
13467 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13468 dlbaton->data = block->data;
13469 dlbaton->size = block->size;
13470 dlbaton->per_objfile = per_objfile;
13471 dlbaton->per_cu = cu->per_cu;
13472
13473 call_site->target.set_loc_dwarf_block (dlbaton);
13474 }
13475 else if (attr->form_is_ref ())
13476 {
13477 struct dwarf2_cu *target_cu = cu;
13478 struct die_info *target_die;
13479
13480 target_die = follow_die_ref (die, attr, &target_cu);
13481 gdb_assert (target_cu->per_objfile->objfile == objfile);
13482
13483 struct attribute *ranges_attr
13484 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
13485
13486 if (die_is_declaration (target_die, target_cu))
13487 {
13488 const char *target_physname;
13489
13490 /* Prefer the mangled name; otherwise compute the demangled one. */
13491 target_physname = dw2_linkage_name (target_die, target_cu);
13492 if (target_physname == NULL)
13493 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13494 if (target_physname == NULL)
13495 complaint (_("DW_AT_call_target target DIE has invalid "
13496 "physname, for referencing DIE %s [in module %s]"),
13497 sect_offset_str (die->sect_off), objfile_name (objfile));
13498 else
13499 call_site->target.set_loc_physname (target_physname);
13500 }
13501 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
13502 {
13503 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
13504 + target_cu->gnu_ranges_base);
13505 std::vector<CORE_ADDR> addresses;
13506 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
13507 target_die->tag, addresses);
13508 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
13509 addresses.size ());
13510 std::copy (addresses.begin (), addresses.end (), saved);
13511 call_site->target.set_loc_array (addresses.size (), saved);
13512 }
13513 else
13514 {
13515 CORE_ADDR lowpc;
13516
13517 /* DW_AT_entry_pc should be preferred. */
13518 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13519 <= PC_BOUNDS_INVALID)
13520 complaint (_("DW_AT_call_target target DIE has invalid "
13521 "low pc, for referencing DIE %s [in module %s]"),
13522 sect_offset_str (die->sect_off), objfile_name (objfile));
13523 else
13524 {
13525 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
13526 - baseaddr);
13527 call_site->target.set_loc_physaddr (lowpc);
13528 }
13529 }
13530 }
13531 else
13532 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13533 "block nor reference, for DIE %s [in module %s]"),
13534 sect_offset_str (die->sect_off), objfile_name (objfile));
13535
13536 for (child_die = die->child;
13537 child_die && child_die->tag;
13538 child_die = child_die->sibling)
13539 {
13540 struct call_site_parameter *parameter;
13541 struct attribute *loc, *origin;
13542
13543 if (child_die->tag != DW_TAG_call_site_parameter
13544 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13545 {
13546 /* Already printed the complaint above. */
13547 continue;
13548 }
13549
13550 gdb_assert (call_site->parameter_count < nparams);
13551 parameter = &call_site->parameter[call_site->parameter_count];
13552
13553 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13554 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13555 register is contained in DW_AT_call_value. */
13556
13557 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13558 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13559 if (origin == NULL)
13560 {
13561 /* This was a pre-DWARF-5 GNU extension alias
13562 for DW_AT_call_parameter. */
13563 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13564 }
13565 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13566 {
13567 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13568
13569 sect_offset sect_off = origin->get_ref_die_offset ();
13570 if (!cu->header.offset_in_cu_p (sect_off))
13571 {
13572 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13573 binding can be done only inside one CU. Such referenced DIE
13574 therefore cannot be even moved to DW_TAG_partial_unit. */
13575 complaint (_("DW_AT_call_parameter offset is not in CU for "
13576 "DW_TAG_call_site child DIE %s [in module %s]"),
13577 sect_offset_str (child_die->sect_off),
13578 objfile_name (objfile));
13579 continue;
13580 }
13581 parameter->u.param_cu_off
13582 = (cu_offset) (sect_off - cu->header.sect_off);
13583 }
13584 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13585 {
13586 complaint (_("No DW_FORM_block* DW_AT_location for "
13587 "DW_TAG_call_site child DIE %s [in module %s]"),
13588 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13589 continue;
13590 }
13591 else
13592 {
13593 struct dwarf_block *block = loc->as_block ();
13594
13595 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13596 (block->data, &block->data[block->size]);
13597 if (parameter->u.dwarf_reg != -1)
13598 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13599 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13600 &block->data[block->size],
13601 &parameter->u.fb_offset))
13602 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13603 else
13604 {
13605 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13606 "for DW_FORM_block* DW_AT_location is supported for "
13607 "DW_TAG_call_site child DIE %s "
13608 "[in module %s]"),
13609 sect_offset_str (child_die->sect_off),
13610 objfile_name (objfile));
13611 continue;
13612 }
13613 }
13614
13615 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13616 if (attr == NULL)
13617 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13618 if (attr == NULL || !attr->form_is_block ())
13619 {
13620 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13621 "DW_TAG_call_site child DIE %s [in module %s]"),
13622 sect_offset_str (child_die->sect_off),
13623 objfile_name (objfile));
13624 continue;
13625 }
13626
13627 struct dwarf_block *block = attr->as_block ();
13628 parameter->value = block->data;
13629 parameter->value_size = block->size;
13630
13631 /* Parameters are not pre-cleared by memset above. */
13632 parameter->data_value = NULL;
13633 parameter->data_value_size = 0;
13634 call_site->parameter_count++;
13635
13636 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13637 if (attr == NULL)
13638 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13639 if (attr != nullptr)
13640 {
13641 if (!attr->form_is_block ())
13642 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13643 "DW_TAG_call_site child DIE %s [in module %s]"),
13644 sect_offset_str (child_die->sect_off),
13645 objfile_name (objfile));
13646 else
13647 {
13648 block = attr->as_block ();
13649 parameter->data_value = block->data;
13650 parameter->data_value_size = block->size;
13651 }
13652 }
13653 }
13654 }
13655
13656 /* Helper function for read_variable. If DIE represents a virtual
13657 table, then return the type of the concrete object that is
13658 associated with the virtual table. Otherwise, return NULL. */
13659
13660 static struct type *
13661 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13662 {
13663 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13664 if (attr == NULL)
13665 return NULL;
13666
13667 /* Find the type DIE. */
13668 struct die_info *type_die = NULL;
13669 struct dwarf2_cu *type_cu = cu;
13670
13671 if (attr->form_is_ref ())
13672 type_die = follow_die_ref (die, attr, &type_cu);
13673 if (type_die == NULL)
13674 return NULL;
13675
13676 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13677 return NULL;
13678 return die_containing_type (type_die, type_cu);
13679 }
13680
13681 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13682
13683 static void
13684 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13685 {
13686 struct rust_vtable_symbol *storage = NULL;
13687
13688 if (cu->per_cu->lang == language_rust)
13689 {
13690 struct type *containing_type = rust_containing_type (die, cu);
13691
13692 if (containing_type != NULL)
13693 {
13694 struct objfile *objfile = cu->per_objfile->objfile;
13695
13696 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13697 storage->concrete_type = containing_type;
13698 storage->subclass = SYMBOL_RUST_VTABLE;
13699 }
13700 }
13701
13702 struct symbol *res = new_symbol (die, NULL, cu, storage);
13703 struct attribute *abstract_origin
13704 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13705 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13706 if (res == NULL && loc && abstract_origin)
13707 {
13708 /* We have a variable without a name, but with a location and an abstract
13709 origin. This may be a concrete instance of an abstract variable
13710 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13711 later. */
13712 struct dwarf2_cu *origin_cu = cu;
13713 struct die_info *origin_die
13714 = follow_die_ref (die, abstract_origin, &origin_cu);
13715 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13716 per_objfile->per_bfd->abstract_to_concrete
13717 [origin_die->sect_off].push_back (die->sect_off);
13718 }
13719 }
13720
13721 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13722 reading .debug_rnglists.
13723 Callback's type should be:
13724 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13725 Return true if the attributes are present and valid, otherwise,
13726 return false. */
13727
13728 template <typename Callback>
13729 static bool
13730 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13731 dwarf_tag tag, Callback &&callback)
13732 {
13733 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13734 struct objfile *objfile = per_objfile->objfile;
13735 bfd *obfd = objfile->obfd;
13736 /* Base address selection entry. */
13737 gdb::optional<CORE_ADDR> base;
13738 const gdb_byte *buffer;
13739 bool overflow = false;
13740 ULONGEST addr_index;
13741 struct dwarf2_section_info *rnglists_section;
13742
13743 base = cu->base_address;
13744 rnglists_section = cu_debug_rnglists_section (cu, tag);
13745 rnglists_section->read (objfile);
13746
13747 if (offset >= rnglists_section->size)
13748 {
13749 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13750 offset);
13751 return false;
13752 }
13753 buffer = rnglists_section->buffer + offset;
13754
13755 while (1)
13756 {
13757 /* Initialize it due to a false compiler warning. */
13758 CORE_ADDR range_beginning = 0, range_end = 0;
13759 const gdb_byte *buf_end = (rnglists_section->buffer
13760 + rnglists_section->size);
13761 unsigned int bytes_read;
13762
13763 if (buffer == buf_end)
13764 {
13765 overflow = true;
13766 break;
13767 }
13768 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13769 switch (rlet)
13770 {
13771 case DW_RLE_end_of_list:
13772 break;
13773 case DW_RLE_base_address:
13774 if (buffer + cu->header.addr_size > buf_end)
13775 {
13776 overflow = true;
13777 break;
13778 }
13779 base = cu->header.read_address (obfd, buffer, &bytes_read);
13780 buffer += bytes_read;
13781 break;
13782 case DW_RLE_base_addressx:
13783 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13784 buffer += bytes_read;
13785 base = read_addr_index (cu, addr_index);
13786 break;
13787 case DW_RLE_start_length:
13788 if (buffer + cu->header.addr_size > buf_end)
13789 {
13790 overflow = true;
13791 break;
13792 }
13793 range_beginning = cu->header.read_address (obfd, buffer,
13794 &bytes_read);
13795 buffer += bytes_read;
13796 range_end = (range_beginning
13797 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13798 buffer += bytes_read;
13799 if (buffer > buf_end)
13800 {
13801 overflow = true;
13802 break;
13803 }
13804 break;
13805 case DW_RLE_startx_length:
13806 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13807 buffer += bytes_read;
13808 range_beginning = read_addr_index (cu, addr_index);
13809 if (buffer > buf_end)
13810 {
13811 overflow = true;
13812 break;
13813 }
13814 range_end = (range_beginning
13815 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13816 buffer += bytes_read;
13817 break;
13818 case DW_RLE_offset_pair:
13819 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13820 buffer += bytes_read;
13821 if (buffer > buf_end)
13822 {
13823 overflow = true;
13824 break;
13825 }
13826 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13827 buffer += bytes_read;
13828 if (buffer > buf_end)
13829 {
13830 overflow = true;
13831 break;
13832 }
13833 break;
13834 case DW_RLE_start_end:
13835 if (buffer + 2 * cu->header.addr_size > buf_end)
13836 {
13837 overflow = true;
13838 break;
13839 }
13840 range_beginning = cu->header.read_address (obfd, buffer,
13841 &bytes_read);
13842 buffer += bytes_read;
13843 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13844 buffer += bytes_read;
13845 break;
13846 case DW_RLE_startx_endx:
13847 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13848 buffer += bytes_read;
13849 range_beginning = read_addr_index (cu, addr_index);
13850 if (buffer > buf_end)
13851 {
13852 overflow = true;
13853 break;
13854 }
13855 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13856 buffer += bytes_read;
13857 range_end = read_addr_index (cu, addr_index);
13858 break;
13859 default:
13860 complaint (_("Invalid .debug_rnglists data (no base address)"));
13861 return false;
13862 }
13863 if (rlet == DW_RLE_end_of_list || overflow)
13864 break;
13865 if (rlet == DW_RLE_base_address)
13866 continue;
13867
13868 if (range_beginning > range_end)
13869 {
13870 /* Inverted range entries are invalid. */
13871 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13872 return false;
13873 }
13874
13875 /* Empty range entries have no effect. */
13876 if (range_beginning == range_end)
13877 continue;
13878
13879 /* Only DW_RLE_offset_pair needs the base address added. */
13880 if (rlet == DW_RLE_offset_pair)
13881 {
13882 if (!base.has_value ())
13883 {
13884 /* We have no valid base address for the DW_RLE_offset_pair. */
13885 complaint (_("Invalid .debug_rnglists data (no base address for "
13886 "DW_RLE_offset_pair)"));
13887 return false;
13888 }
13889
13890 range_beginning += *base;
13891 range_end += *base;
13892 }
13893
13894 /* A not-uncommon case of bad debug info.
13895 Don't pollute the addrmap with bad data. */
13896 if (range_beginning == 0
13897 && !per_objfile->per_bfd->has_section_at_zero)
13898 {
13899 complaint (_(".debug_rnglists entry has start address of zero"
13900 " [in module %s]"), objfile_name (objfile));
13901 continue;
13902 }
13903
13904 callback (range_beginning, range_end);
13905 }
13906
13907 if (overflow)
13908 {
13909 complaint (_("Offset %d is not terminated "
13910 "for DW_AT_ranges attribute"),
13911 offset);
13912 return false;
13913 }
13914
13915 return true;
13916 }
13917
13918 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13919 Callback's type should be:
13920 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13921 Return 1 if the attributes are present and valid, otherwise, return 0. */
13922
13923 template <typename Callback>
13924 static int
13925 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13926 Callback &&callback)
13927 {
13928 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13929 struct objfile *objfile = per_objfile->objfile;
13930 struct comp_unit_head *cu_header = &cu->header;
13931 bfd *obfd = objfile->obfd;
13932 unsigned int addr_size = cu_header->addr_size;
13933 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13934 /* Base address selection entry. */
13935 gdb::optional<CORE_ADDR> base;
13936 unsigned int dummy;
13937 const gdb_byte *buffer;
13938
13939 if (cu_header->version >= 5)
13940 return dwarf2_rnglists_process (offset, cu, tag, callback);
13941
13942 base = cu->base_address;
13943
13944 per_objfile->per_bfd->ranges.read (objfile);
13945 if (offset >= per_objfile->per_bfd->ranges.size)
13946 {
13947 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13948 offset);
13949 return 0;
13950 }
13951 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13952
13953 while (1)
13954 {
13955 CORE_ADDR range_beginning, range_end;
13956
13957 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13958 buffer += addr_size;
13959 range_end = cu->header.read_address (obfd, buffer, &dummy);
13960 buffer += addr_size;
13961 offset += 2 * addr_size;
13962
13963 /* An end of list marker is a pair of zero addresses. */
13964 if (range_beginning == 0 && range_end == 0)
13965 /* Found the end of list entry. */
13966 break;
13967
13968 /* Each base address selection entry is a pair of 2 values.
13969 The first is the largest possible address, the second is
13970 the base address. Check for a base address here. */
13971 if ((range_beginning & mask) == mask)
13972 {
13973 /* If we found the largest possible address, then we already
13974 have the base address in range_end. */
13975 base = range_end;
13976 continue;
13977 }
13978
13979 if (!base.has_value ())
13980 {
13981 /* We have no valid base address for the ranges
13982 data. */
13983 complaint (_("Invalid .debug_ranges data (no base address)"));
13984 return 0;
13985 }
13986
13987 if (range_beginning > range_end)
13988 {
13989 /* Inverted range entries are invalid. */
13990 complaint (_("Invalid .debug_ranges data (inverted range)"));
13991 return 0;
13992 }
13993
13994 /* Empty range entries have no effect. */
13995 if (range_beginning == range_end)
13996 continue;
13997
13998 range_beginning += *base;
13999 range_end += *base;
14000
14001 /* A not-uncommon case of bad debug info.
14002 Don't pollute the addrmap with bad data. */
14003 if (range_beginning == 0
14004 && !per_objfile->per_bfd->has_section_at_zero)
14005 {
14006 complaint (_(".debug_ranges entry has start address of zero"
14007 " [in module %s]"), objfile_name (objfile));
14008 continue;
14009 }
14010
14011 callback (range_beginning, range_end);
14012 }
14013
14014 return 1;
14015 }
14016
14017 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14018 Return 1 if the attributes are present and valid, otherwise, return 0.
14019 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14020
14021 static int
14022 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14023 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14024 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14025 {
14026 struct objfile *objfile = cu->per_objfile->objfile;
14027 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14028 struct gdbarch *gdbarch = objfile->arch ();
14029 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14030 int low_set = 0;
14031 CORE_ADDR low = 0;
14032 CORE_ADDR high = 0;
14033 int retval;
14034
14035 retval = dwarf2_ranges_process (offset, cu, tag,
14036 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14037 {
14038 if (ranges_pst != NULL)
14039 {
14040 CORE_ADDR lowpc;
14041 CORE_ADDR highpc;
14042
14043 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14044 range_beginning + baseaddr)
14045 - baseaddr);
14046 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14047 range_end + baseaddr)
14048 - baseaddr);
14049 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14050 lowpc, highpc - 1, ranges_pst);
14051 }
14052
14053 /* FIXME: This is recording everything as a low-high
14054 segment of consecutive addresses. We should have a
14055 data structure for discontiguous block ranges
14056 instead. */
14057 if (! low_set)
14058 {
14059 low = range_beginning;
14060 high = range_end;
14061 low_set = 1;
14062 }
14063 else
14064 {
14065 if (range_beginning < low)
14066 low = range_beginning;
14067 if (range_end > high)
14068 high = range_end;
14069 }
14070 });
14071 if (!retval)
14072 return 0;
14073
14074 if (! low_set)
14075 /* If the first entry is an end-of-list marker, the range
14076 describes an empty scope, i.e. no instructions. */
14077 return 0;
14078
14079 if (low_return)
14080 *low_return = low;
14081 if (high_return)
14082 *high_return = high;
14083 return 1;
14084 }
14085
14086 /* Process ranges and fill in a vector of the low PC values only. */
14087
14088 static void
14089 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
14090 dwarf_tag tag,
14091 std::vector<CORE_ADDR> &result)
14092 {
14093 dwarf2_ranges_process (offset, cu, tag,
14094 [&] (CORE_ADDR start, CORE_ADDR end)
14095 {
14096 result.push_back (start);
14097 });
14098 }
14099
14100 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14101 definition for the return value. *LOWPC and *HIGHPC are set iff
14102 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14103
14104 static enum pc_bounds_kind
14105 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14106 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14107 dwarf2_psymtab *pst)
14108 {
14109 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14110 struct attribute *attr;
14111 struct attribute *attr_high;
14112 CORE_ADDR low = 0;
14113 CORE_ADDR high = 0;
14114 enum pc_bounds_kind ret;
14115
14116 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14117 if (attr_high)
14118 {
14119 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14120 if (attr != nullptr)
14121 {
14122 low = attr->as_address ();
14123 high = attr_high->as_address ();
14124 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14125 high += low;
14126 }
14127 else
14128 /* Found high w/o low attribute. */
14129 return PC_BOUNDS_INVALID;
14130
14131 /* Found consecutive range of addresses. */
14132 ret = PC_BOUNDS_HIGH_LOW;
14133 }
14134 else
14135 {
14136 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14137 if (attr != nullptr && attr->form_is_unsigned ())
14138 {
14139 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14140 on DWARF version). */
14141 ULONGEST ranges_offset = attr->as_unsigned ();
14142
14143 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14144 this value. */
14145 if (die->tag != DW_TAG_compile_unit)
14146 ranges_offset += cu->gnu_ranges_base;
14147
14148 /* Value of the DW_AT_ranges attribute is the offset in the
14149 .debug_ranges section. */
14150 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14151 die->tag))
14152 return PC_BOUNDS_INVALID;
14153 /* Found discontinuous range of addresses. */
14154 ret = PC_BOUNDS_RANGES;
14155 }
14156 else
14157 return PC_BOUNDS_NOT_PRESENT;
14158 }
14159
14160 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14161 if (high <= low)
14162 return PC_BOUNDS_INVALID;
14163
14164 /* When using the GNU linker, .gnu.linkonce. sections are used to
14165 eliminate duplicate copies of functions and vtables and such.
14166 The linker will arbitrarily choose one and discard the others.
14167 The AT_*_pc values for such functions refer to local labels in
14168 these sections. If the section from that file was discarded, the
14169 labels are not in the output, so the relocs get a value of 0.
14170 If this is a discarded function, mark the pc bounds as invalid,
14171 so that GDB will ignore it. */
14172 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14173 return PC_BOUNDS_INVALID;
14174
14175 *lowpc = low;
14176 if (highpc)
14177 *highpc = high;
14178 return ret;
14179 }
14180
14181 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14182 its low and high PC addresses. Do nothing if these addresses could not
14183 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14184 and HIGHPC to the high address if greater than HIGHPC. */
14185
14186 static void
14187 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14188 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14189 struct dwarf2_cu *cu)
14190 {
14191 CORE_ADDR low, high;
14192 struct die_info *child = die->child;
14193
14194 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14195 {
14196 *lowpc = std::min (*lowpc, low);
14197 *highpc = std::max (*highpc, high);
14198 }
14199
14200 /* If the language does not allow nested subprograms (either inside
14201 subprograms or lexical blocks), we're done. */
14202 if (cu->per_cu->lang != language_ada)
14203 return;
14204
14205 /* Check all the children of the given DIE. If it contains nested
14206 subprograms, then check their pc bounds. Likewise, we need to
14207 check lexical blocks as well, as they may also contain subprogram
14208 definitions. */
14209 while (child && child->tag)
14210 {
14211 if (child->tag == DW_TAG_subprogram
14212 || child->tag == DW_TAG_lexical_block)
14213 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14214 child = child->sibling;
14215 }
14216 }
14217
14218 /* Get the low and high pc's represented by the scope DIE, and store
14219 them in *LOWPC and *HIGHPC. If the correct values can't be
14220 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14221
14222 static void
14223 get_scope_pc_bounds (struct die_info *die,
14224 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14225 struct dwarf2_cu *cu)
14226 {
14227 CORE_ADDR best_low = (CORE_ADDR) -1;
14228 CORE_ADDR best_high = (CORE_ADDR) 0;
14229 CORE_ADDR current_low, current_high;
14230
14231 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14232 >= PC_BOUNDS_RANGES)
14233 {
14234 best_low = current_low;
14235 best_high = current_high;
14236 }
14237 else
14238 {
14239 struct die_info *child = die->child;
14240
14241 while (child && child->tag)
14242 {
14243 switch (child->tag) {
14244 case DW_TAG_subprogram:
14245 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14246 break;
14247 case DW_TAG_namespace:
14248 case DW_TAG_module:
14249 /* FIXME: carlton/2004-01-16: Should we do this for
14250 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14251 that current GCC's always emit the DIEs corresponding
14252 to definitions of methods of classes as children of a
14253 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14254 the DIEs giving the declarations, which could be
14255 anywhere). But I don't see any reason why the
14256 standards says that they have to be there. */
14257 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14258
14259 if (current_low != ((CORE_ADDR) -1))
14260 {
14261 best_low = std::min (best_low, current_low);
14262 best_high = std::max (best_high, current_high);
14263 }
14264 break;
14265 default:
14266 /* Ignore. */
14267 break;
14268 }
14269
14270 child = child->sibling;
14271 }
14272 }
14273
14274 *lowpc = best_low;
14275 *highpc = best_high;
14276 }
14277
14278 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14279 in DIE. */
14280
14281 static void
14282 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14283 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14284 {
14285 struct objfile *objfile = cu->per_objfile->objfile;
14286 struct gdbarch *gdbarch = objfile->arch ();
14287 struct attribute *attr;
14288 struct attribute *attr_high;
14289
14290 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14291 if (attr_high)
14292 {
14293 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14294 if (attr != nullptr)
14295 {
14296 CORE_ADDR low = attr->as_address ();
14297 CORE_ADDR high = attr_high->as_address ();
14298
14299 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14300 high += low;
14301
14302 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14303 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14304 cu->get_builder ()->record_block_range (block, low, high - 1);
14305 }
14306 }
14307
14308 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14309 if (attr != nullptr && attr->form_is_unsigned ())
14310 {
14311 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14312 on DWARF version). */
14313 ULONGEST ranges_offset = attr->as_unsigned ();
14314
14315 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14316 this value. */
14317 if (die->tag != DW_TAG_compile_unit)
14318 ranges_offset += cu->gnu_ranges_base;
14319
14320 std::vector<blockrange> blockvec;
14321 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14322 [&] (CORE_ADDR start, CORE_ADDR end)
14323 {
14324 start += baseaddr;
14325 end += baseaddr;
14326 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14327 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14328 cu->get_builder ()->record_block_range (block, start, end - 1);
14329 blockvec.emplace_back (start, end);
14330 });
14331
14332 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14333 }
14334 }
14335
14336 /* Check whether the producer field indicates either of GCC < 4.6, or the
14337 Intel C/C++ compiler, and cache the result in CU. */
14338
14339 static void
14340 check_producer (struct dwarf2_cu *cu)
14341 {
14342 int major, minor;
14343
14344 if (cu->producer == NULL)
14345 {
14346 /* For unknown compilers expect their behavior is DWARF version
14347 compliant.
14348
14349 GCC started to support .debug_types sections by -gdwarf-4 since
14350 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14351 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14352 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14353 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14354 }
14355 else if (producer_is_gcc (cu->producer, &major, &minor))
14356 {
14357 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14358 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14359 cu->producer_is_gcc_11 = major == 11;
14360 }
14361 else if (producer_is_icc (cu->producer, &major, &minor))
14362 {
14363 cu->producer_is_icc = true;
14364 cu->producer_is_icc_lt_14 = major < 14;
14365 }
14366 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14367 cu->producer_is_codewarrior = true;
14368 else
14369 {
14370 /* For other non-GCC compilers, expect their behavior is DWARF version
14371 compliant. */
14372 }
14373
14374 cu->checked_producer = true;
14375 }
14376
14377 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14378 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14379 during 4.6.0 experimental. */
14380
14381 static bool
14382 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14383 {
14384 if (!cu->checked_producer)
14385 check_producer (cu);
14386
14387 return cu->producer_is_gxx_lt_4_6;
14388 }
14389
14390
14391 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14392 with incorrect is_stmt attributes. */
14393
14394 static bool
14395 producer_is_codewarrior (struct dwarf2_cu *cu)
14396 {
14397 if (!cu->checked_producer)
14398 check_producer (cu);
14399
14400 return cu->producer_is_codewarrior;
14401 }
14402
14403 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14404 If that attribute is not available, return the appropriate
14405 default. */
14406
14407 static enum dwarf_access_attribute
14408 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14409 {
14410 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14411 if (attr != nullptr)
14412 {
14413 LONGEST value = attr->constant_value (-1);
14414 if (value == DW_ACCESS_public
14415 || value == DW_ACCESS_protected
14416 || value == DW_ACCESS_private)
14417 return (dwarf_access_attribute) value;
14418 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14419 plongest (value));
14420 }
14421
14422 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14423 {
14424 /* The default DWARF 2 accessibility for members is public, the default
14425 accessibility for inheritance is private. */
14426
14427 if (die->tag != DW_TAG_inheritance)
14428 return DW_ACCESS_public;
14429 else
14430 return DW_ACCESS_private;
14431 }
14432 else
14433 {
14434 /* DWARF 3+ defines the default accessibility a different way. The same
14435 rules apply now for DW_TAG_inheritance as for the members and it only
14436 depends on the container kind. */
14437
14438 if (die->parent->tag == DW_TAG_class_type)
14439 return DW_ACCESS_private;
14440 else
14441 return DW_ACCESS_public;
14442 }
14443 }
14444
14445 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
14446 *OFFSET to the byte offset. If the attribute was not found return
14447 0, otherwise return 1. If it was found but could not properly be
14448 handled, set *OFFSET to 0. */
14449
14450 static int
14451 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14452 LONGEST *offset)
14453 {
14454 struct attribute *attr;
14455
14456 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14457 if (attr != NULL)
14458 {
14459 *offset = 0;
14460
14461 /* Note that we do not check for a section offset first here.
14462 This is because DW_AT_data_member_location is new in DWARF 4,
14463 so if we see it, we can assume that a constant form is really
14464 a constant and not a section offset. */
14465 if (attr->form_is_constant ())
14466 *offset = attr->constant_value (0);
14467 else if (attr->form_is_section_offset ())
14468 dwarf2_complex_location_expr_complaint ();
14469 else if (attr->form_is_block ())
14470 *offset = decode_locdesc (attr->as_block (), cu);
14471 else
14472 dwarf2_complex_location_expr_complaint ();
14473
14474 return 1;
14475 }
14476 else
14477 {
14478 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14479 if (attr != nullptr)
14480 {
14481 *offset = attr->constant_value (0);
14482 return 1;
14483 }
14484 }
14485
14486 return 0;
14487 }
14488
14489 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
14490 store the results in FIELD. */
14491
14492 static void
14493 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14494 struct field *field)
14495 {
14496 struct attribute *attr;
14497
14498 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14499 if (attr != NULL)
14500 {
14501 if (attr->form_is_constant ())
14502 {
14503 LONGEST offset = attr->constant_value (0);
14504
14505 /* Work around this GCC 11 bug, where it would erroneously use -1
14506 data member locations, instead of 0:
14507
14508 Negative DW_AT_data_member_location
14509 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
14510 */
14511 if (offset == -1 && cu->producer_is_gcc_11)
14512 {
14513 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
14514 offset = 0;
14515 }
14516
14517 field->set_loc_bitpos (offset * bits_per_byte);
14518 }
14519 else if (attr->form_is_section_offset ())
14520 dwarf2_complex_location_expr_complaint ();
14521 else if (attr->form_is_block ())
14522 {
14523 bool handled;
14524 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14525 if (handled)
14526 field->set_loc_bitpos (offset * bits_per_byte);
14527 else
14528 {
14529 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14530 struct objfile *objfile = per_objfile->objfile;
14531 struct dwarf2_locexpr_baton *dlbaton
14532 = XOBNEW (&objfile->objfile_obstack,
14533 struct dwarf2_locexpr_baton);
14534 dlbaton->data = attr->as_block ()->data;
14535 dlbaton->size = attr->as_block ()->size;
14536 /* When using this baton, we want to compute the address
14537 of the field, not the value. This is why
14538 is_reference is set to false here. */
14539 dlbaton->is_reference = false;
14540 dlbaton->per_objfile = per_objfile;
14541 dlbaton->per_cu = cu->per_cu;
14542
14543 field->set_loc_dwarf_block (dlbaton);
14544 }
14545 }
14546 else
14547 dwarf2_complex_location_expr_complaint ();
14548 }
14549 else
14550 {
14551 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14552 if (attr != nullptr)
14553 field->set_loc_bitpos (attr->constant_value (0));
14554 }
14555 }
14556
14557 /* Add an aggregate field to the field list. */
14558
14559 static void
14560 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14561 struct dwarf2_cu *cu)
14562 {
14563 struct objfile *objfile = cu->per_objfile->objfile;
14564 struct gdbarch *gdbarch = objfile->arch ();
14565 struct nextfield *new_field;
14566 struct attribute *attr;
14567 struct field *fp;
14568 const char *fieldname = "";
14569
14570 if (die->tag == DW_TAG_inheritance)
14571 {
14572 fip->baseclasses.emplace_back ();
14573 new_field = &fip->baseclasses.back ();
14574 }
14575 else
14576 {
14577 fip->fields.emplace_back ();
14578 new_field = &fip->fields.back ();
14579 }
14580
14581 new_field->offset = die->sect_off;
14582
14583 new_field->accessibility = dwarf2_access_attribute (die, cu);
14584 if (new_field->accessibility != DW_ACCESS_public)
14585 fip->non_public_fields = true;
14586
14587 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14588 if (attr != nullptr)
14589 new_field->virtuality = attr->as_virtuality ();
14590 else
14591 new_field->virtuality = DW_VIRTUALITY_none;
14592
14593 fp = &new_field->field;
14594
14595 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
14596 && !die_is_declaration (die, cu))
14597 {
14598 if (die->tag == DW_TAG_namelist_item)
14599 {
14600 /* Typically, DW_TAG_namelist_item are references to namelist items.
14601 If so, follow that reference. */
14602 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
14603 struct die_info *item_die = nullptr;
14604 struct dwarf2_cu *item_cu = cu;
14605 if (attr1->form_is_ref ())
14606 item_die = follow_die_ref (die, attr1, &item_cu);
14607 if (item_die != nullptr)
14608 die = item_die;
14609 }
14610 /* Data member other than a C++ static data member. */
14611
14612 /* Get type of field. */
14613 fp->set_type (die_type (die, cu));
14614
14615 fp->set_loc_bitpos (0);
14616
14617 /* Get bit size of field (zero if none). */
14618 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14619 if (attr != nullptr)
14620 {
14621 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14622 }
14623 else
14624 {
14625 FIELD_BITSIZE (*fp) = 0;
14626 }
14627
14628 /* Get bit offset of field. */
14629 handle_member_location (die, cu, fp);
14630 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14631 if (attr != nullptr && attr->form_is_constant ())
14632 {
14633 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14634 {
14635 /* For big endian bits, the DW_AT_bit_offset gives the
14636 additional bit offset from the MSB of the containing
14637 anonymous object to the MSB of the field. We don't
14638 have to do anything special since we don't need to
14639 know the size of the anonymous object. */
14640 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
14641 }
14642 else
14643 {
14644 /* For little endian bits, compute the bit offset to the
14645 MSB of the anonymous object, subtract off the number of
14646 bits from the MSB of the field to the MSB of the
14647 object, and then subtract off the number of bits of
14648 the field itself. The result is the bit offset of
14649 the LSB of the field. */
14650 int anonymous_size;
14651 int bit_offset = attr->constant_value (0);
14652
14653 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14654 if (attr != nullptr && attr->form_is_constant ())
14655 {
14656 /* The size of the anonymous object containing
14657 the bit field is explicit, so use the
14658 indicated size (in bytes). */
14659 anonymous_size = attr->constant_value (0);
14660 }
14661 else
14662 {
14663 /* The size of the anonymous object containing
14664 the bit field must be inferred from the type
14665 attribute of the data member containing the
14666 bit field. */
14667 anonymous_size = TYPE_LENGTH (fp->type ());
14668 }
14669 fp->set_loc_bitpos (fp->loc_bitpos ()
14670 + anonymous_size * bits_per_byte
14671 - bit_offset - FIELD_BITSIZE (*fp));
14672 }
14673 }
14674
14675 /* Get name of field. */
14676 fieldname = dwarf2_name (die, cu);
14677 if (fieldname == NULL)
14678 fieldname = "";
14679
14680 /* The name is already allocated along with this objfile, so we don't
14681 need to duplicate it for the type. */
14682 fp->set_name (fieldname);
14683
14684 /* Change accessibility for artificial fields (e.g. virtual table
14685 pointer or virtual base class pointer) to private. */
14686 if (dwarf2_attr (die, DW_AT_artificial, cu))
14687 {
14688 FIELD_ARTIFICIAL (*fp) = 1;
14689 new_field->accessibility = DW_ACCESS_private;
14690 fip->non_public_fields = true;
14691 }
14692 }
14693 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14694 {
14695 /* C++ static member. */
14696
14697 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14698 is a declaration, but all versions of G++ as of this writing
14699 (so through at least 3.2.1) incorrectly generate
14700 DW_TAG_variable tags. */
14701
14702 const char *physname;
14703
14704 /* Get name of field. */
14705 fieldname = dwarf2_name (die, cu);
14706 if (fieldname == NULL)
14707 return;
14708
14709 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14710 if (attr
14711 /* Only create a symbol if this is an external value.
14712 new_symbol checks this and puts the value in the global symbol
14713 table, which we want. If it is not external, new_symbol
14714 will try to put the value in cu->list_in_scope which is wrong. */
14715 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14716 {
14717 /* A static const member, not much different than an enum as far as
14718 we're concerned, except that we can support more types. */
14719 new_symbol (die, NULL, cu);
14720 }
14721
14722 /* Get physical name. */
14723 physname = dwarf2_physname (fieldname, die, cu);
14724
14725 /* The name is already allocated along with this objfile, so we don't
14726 need to duplicate it for the type. */
14727 fp->set_loc_physname (physname ? physname : "");
14728 fp->set_type (die_type (die, cu));
14729 fp->set_name (fieldname);
14730 }
14731 else if (die->tag == DW_TAG_inheritance)
14732 {
14733 /* C++ base class field. */
14734 handle_member_location (die, cu, fp);
14735 FIELD_BITSIZE (*fp) = 0;
14736 fp->set_type (die_type (die, cu));
14737 fp->set_name (fp->type ()->name ());
14738 }
14739 else
14740 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14741 }
14742
14743 /* Can the type given by DIE define another type? */
14744
14745 static bool
14746 type_can_define_types (const struct die_info *die)
14747 {
14748 switch (die->tag)
14749 {
14750 case DW_TAG_typedef:
14751 case DW_TAG_class_type:
14752 case DW_TAG_structure_type:
14753 case DW_TAG_union_type:
14754 case DW_TAG_enumeration_type:
14755 return true;
14756
14757 default:
14758 return false;
14759 }
14760 }
14761
14762 /* Add a type definition defined in the scope of the FIP's class. */
14763
14764 static void
14765 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14766 struct dwarf2_cu *cu)
14767 {
14768 struct decl_field fp;
14769 memset (&fp, 0, sizeof (fp));
14770
14771 gdb_assert (type_can_define_types (die));
14772
14773 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14774 fp.name = dwarf2_name (die, cu);
14775 fp.type = read_type_die (die, cu);
14776
14777 /* Save accessibility. */
14778 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14779 switch (accessibility)
14780 {
14781 case DW_ACCESS_public:
14782 /* The assumed value if neither private nor protected. */
14783 break;
14784 case DW_ACCESS_private:
14785 fp.is_private = 1;
14786 break;
14787 case DW_ACCESS_protected:
14788 fp.is_protected = 1;
14789 break;
14790 }
14791
14792 if (die->tag == DW_TAG_typedef)
14793 fip->typedef_field_list.push_back (fp);
14794 else
14795 fip->nested_types_list.push_back (fp);
14796 }
14797
14798 /* A convenience typedef that's used when finding the discriminant
14799 field for a variant part. */
14800 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14801 offset_map_type;
14802
14803 /* Compute the discriminant range for a given variant. OBSTACK is
14804 where the results will be stored. VARIANT is the variant to
14805 process. IS_UNSIGNED indicates whether the discriminant is signed
14806 or unsigned. */
14807
14808 static const gdb::array_view<discriminant_range>
14809 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14810 bool is_unsigned)
14811 {
14812 std::vector<discriminant_range> ranges;
14813
14814 if (variant.default_branch)
14815 return {};
14816
14817 if (variant.discr_list_data == nullptr)
14818 {
14819 discriminant_range r
14820 = {variant.discriminant_value, variant.discriminant_value};
14821 ranges.push_back (r);
14822 }
14823 else
14824 {
14825 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14826 variant.discr_list_data->size);
14827 while (!data.empty ())
14828 {
14829 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14830 {
14831 complaint (_("invalid discriminant marker: %d"), data[0]);
14832 break;
14833 }
14834 bool is_range = data[0] == DW_DSC_range;
14835 data = data.slice (1);
14836
14837 ULONGEST low, high;
14838 unsigned int bytes_read;
14839
14840 if (data.empty ())
14841 {
14842 complaint (_("DW_AT_discr_list missing low value"));
14843 break;
14844 }
14845 if (is_unsigned)
14846 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14847 else
14848 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14849 &bytes_read);
14850 data = data.slice (bytes_read);
14851
14852 if (is_range)
14853 {
14854 if (data.empty ())
14855 {
14856 complaint (_("DW_AT_discr_list missing high value"));
14857 break;
14858 }
14859 if (is_unsigned)
14860 high = read_unsigned_leb128 (nullptr, data.data (),
14861 &bytes_read);
14862 else
14863 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14864 &bytes_read);
14865 data = data.slice (bytes_read);
14866 }
14867 else
14868 high = low;
14869
14870 ranges.push_back ({ low, high });
14871 }
14872 }
14873
14874 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14875 ranges.size ());
14876 std::copy (ranges.begin (), ranges.end (), result);
14877 return gdb::array_view<discriminant_range> (result, ranges.size ());
14878 }
14879
14880 static const gdb::array_view<variant_part> create_variant_parts
14881 (struct obstack *obstack,
14882 const offset_map_type &offset_map,
14883 struct field_info *fi,
14884 const std::vector<variant_part_builder> &variant_parts);
14885
14886 /* Fill in a "struct variant" for a given variant field. RESULT is
14887 the variant to fill in. OBSTACK is where any needed allocations
14888 will be done. OFFSET_MAP holds the mapping from section offsets to
14889 fields for the type. FI describes the fields of the type we're
14890 processing. FIELD is the variant field we're converting. */
14891
14892 static void
14893 create_one_variant (variant &result, struct obstack *obstack,
14894 const offset_map_type &offset_map,
14895 struct field_info *fi, const variant_field &field)
14896 {
14897 result.discriminants = convert_variant_range (obstack, field, false);
14898 result.first_field = field.first_field + fi->baseclasses.size ();
14899 result.last_field = field.last_field + fi->baseclasses.size ();
14900 result.parts = create_variant_parts (obstack, offset_map, fi,
14901 field.variant_parts);
14902 }
14903
14904 /* Fill in a "struct variant_part" for a given variant part. RESULT
14905 is the variant part to fill in. OBSTACK is where any needed
14906 allocations will be done. OFFSET_MAP holds the mapping from
14907 section offsets to fields for the type. FI describes the fields of
14908 the type we're processing. BUILDER is the variant part to be
14909 converted. */
14910
14911 static void
14912 create_one_variant_part (variant_part &result,
14913 struct obstack *obstack,
14914 const offset_map_type &offset_map,
14915 struct field_info *fi,
14916 const variant_part_builder &builder)
14917 {
14918 auto iter = offset_map.find (builder.discriminant_offset);
14919 if (iter == offset_map.end ())
14920 {
14921 result.discriminant_index = -1;
14922 /* Doesn't matter. */
14923 result.is_unsigned = false;
14924 }
14925 else
14926 {
14927 result.discriminant_index = iter->second;
14928 result.is_unsigned
14929 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14930 }
14931
14932 size_t n = builder.variants.size ();
14933 variant *output = new (obstack) variant[n];
14934 for (size_t i = 0; i < n; ++i)
14935 create_one_variant (output[i], obstack, offset_map, fi,
14936 builder.variants[i]);
14937
14938 result.variants = gdb::array_view<variant> (output, n);
14939 }
14940
14941 /* Create a vector of variant parts that can be attached to a type.
14942 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14943 holds the mapping from section offsets to fields for the type. FI
14944 describes the fields of the type we're processing. VARIANT_PARTS
14945 is the vector to convert. */
14946
14947 static const gdb::array_view<variant_part>
14948 create_variant_parts (struct obstack *obstack,
14949 const offset_map_type &offset_map,
14950 struct field_info *fi,
14951 const std::vector<variant_part_builder> &variant_parts)
14952 {
14953 if (variant_parts.empty ())
14954 return {};
14955
14956 size_t n = variant_parts.size ();
14957 variant_part *result = new (obstack) variant_part[n];
14958 for (size_t i = 0; i < n; ++i)
14959 create_one_variant_part (result[i], obstack, offset_map, fi,
14960 variant_parts[i]);
14961
14962 return gdb::array_view<variant_part> (result, n);
14963 }
14964
14965 /* Compute the variant part vector for FIP, attaching it to TYPE when
14966 done. */
14967
14968 static void
14969 add_variant_property (struct field_info *fip, struct type *type,
14970 struct dwarf2_cu *cu)
14971 {
14972 /* Map section offsets of fields to their field index. Note the
14973 field index here does not take the number of baseclasses into
14974 account. */
14975 offset_map_type offset_map;
14976 for (int i = 0; i < fip->fields.size (); ++i)
14977 offset_map[fip->fields[i].offset] = i;
14978
14979 struct objfile *objfile = cu->per_objfile->objfile;
14980 gdb::array_view<const variant_part> parts
14981 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14982 fip->variant_parts);
14983
14984 struct dynamic_prop prop;
14985 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14986 obstack_copy (&objfile->objfile_obstack, &parts,
14987 sizeof (parts)));
14988
14989 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14990 }
14991
14992 /* Create the vector of fields, and attach it to the type. */
14993
14994 static void
14995 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14996 struct dwarf2_cu *cu)
14997 {
14998 int nfields = fip->nfields ();
14999
15000 /* Record the field count, allocate space for the array of fields,
15001 and create blank accessibility bitfields if necessary. */
15002 type->set_num_fields (nfields);
15003 type->set_fields
15004 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15005
15006 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
15007 {
15008 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15009
15010 TYPE_FIELD_PRIVATE_BITS (type) =
15011 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15012 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15013
15014 TYPE_FIELD_PROTECTED_BITS (type) =
15015 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15016 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15017
15018 TYPE_FIELD_IGNORE_BITS (type) =
15019 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15020 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15021 }
15022
15023 /* If the type has baseclasses, allocate and clear a bit vector for
15024 TYPE_FIELD_VIRTUAL_BITS. */
15025 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
15026 {
15027 int num_bytes = B_BYTES (fip->baseclasses.size ());
15028 unsigned char *pointer;
15029
15030 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15031 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15032 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15033 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15034 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15035 }
15036
15037 if (!fip->variant_parts.empty ())
15038 add_variant_property (fip, type, cu);
15039
15040 /* Copy the saved-up fields into the field vector. */
15041 for (int i = 0; i < nfields; ++i)
15042 {
15043 struct nextfield &field
15044 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15045 : fip->fields[i - fip->baseclasses.size ()]);
15046
15047 type->field (i) = field.field;
15048 switch (field.accessibility)
15049 {
15050 case DW_ACCESS_private:
15051 if (cu->per_cu->lang != language_ada)
15052 SET_TYPE_FIELD_PRIVATE (type, i);
15053 break;
15054
15055 case DW_ACCESS_protected:
15056 if (cu->per_cu->lang != language_ada)
15057 SET_TYPE_FIELD_PROTECTED (type, i);
15058 break;
15059
15060 case DW_ACCESS_public:
15061 break;
15062
15063 default:
15064 /* Unknown accessibility. Complain and treat it as public. */
15065 {
15066 complaint (_("unsupported accessibility %d"),
15067 field.accessibility);
15068 }
15069 break;
15070 }
15071 if (i < fip->baseclasses.size ())
15072 {
15073 switch (field.virtuality)
15074 {
15075 case DW_VIRTUALITY_virtual:
15076 case DW_VIRTUALITY_pure_virtual:
15077 if (cu->per_cu->lang == language_ada)
15078 error (_("unexpected virtuality in component of Ada type"));
15079 SET_TYPE_FIELD_VIRTUAL (type, i);
15080 break;
15081 }
15082 }
15083 }
15084 }
15085
15086 /* Return true if this member function is a constructor, false
15087 otherwise. */
15088
15089 static int
15090 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15091 {
15092 const char *fieldname;
15093 const char *type_name;
15094 int len;
15095
15096 if (die->parent == NULL)
15097 return 0;
15098
15099 if (die->parent->tag != DW_TAG_structure_type
15100 && die->parent->tag != DW_TAG_union_type
15101 && die->parent->tag != DW_TAG_class_type)
15102 return 0;
15103
15104 fieldname = dwarf2_name (die, cu);
15105 type_name = dwarf2_name (die->parent, cu);
15106 if (fieldname == NULL || type_name == NULL)
15107 return 0;
15108
15109 len = strlen (fieldname);
15110 return (strncmp (fieldname, type_name, len) == 0
15111 && (type_name[len] == '\0' || type_name[len] == '<'));
15112 }
15113
15114 /* Add a member function to the proper fieldlist. */
15115
15116 static void
15117 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15118 struct type *type, struct dwarf2_cu *cu)
15119 {
15120 struct objfile *objfile = cu->per_objfile->objfile;
15121 struct attribute *attr;
15122 int i;
15123 struct fnfieldlist *flp = nullptr;
15124 struct fn_field *fnp;
15125 const char *fieldname;
15126 struct type *this_type;
15127
15128 if (cu->per_cu->lang == language_ada)
15129 error (_("unexpected member function in Ada type"));
15130
15131 /* Get name of member function. */
15132 fieldname = dwarf2_name (die, cu);
15133 if (fieldname == NULL)
15134 return;
15135
15136 /* Look up member function name in fieldlist. */
15137 for (i = 0; i < fip->fnfieldlists.size (); i++)
15138 {
15139 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15140 {
15141 flp = &fip->fnfieldlists[i];
15142 break;
15143 }
15144 }
15145
15146 /* Create a new fnfieldlist if necessary. */
15147 if (flp == nullptr)
15148 {
15149 fip->fnfieldlists.emplace_back ();
15150 flp = &fip->fnfieldlists.back ();
15151 flp->name = fieldname;
15152 i = fip->fnfieldlists.size () - 1;
15153 }
15154
15155 /* Create a new member function field and add it to the vector of
15156 fnfieldlists. */
15157 flp->fnfields.emplace_back ();
15158 fnp = &flp->fnfields.back ();
15159
15160 /* Delay processing of the physname until later. */
15161 if (cu->per_cu->lang == language_cplus)
15162 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15163 die, cu);
15164 else
15165 {
15166 const char *physname = dwarf2_physname (fieldname, die, cu);
15167 fnp->physname = physname ? physname : "";
15168 }
15169
15170 fnp->type = alloc_type (objfile);
15171 this_type = read_type_die (die, cu);
15172 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15173 {
15174 int nparams = this_type->num_fields ();
15175
15176 /* TYPE is the domain of this method, and THIS_TYPE is the type
15177 of the method itself (TYPE_CODE_METHOD). */
15178 smash_to_method_type (fnp->type, type,
15179 TYPE_TARGET_TYPE (this_type),
15180 this_type->fields (),
15181 this_type->num_fields (),
15182 this_type->has_varargs ());
15183
15184 /* Handle static member functions.
15185 Dwarf2 has no clean way to discern C++ static and non-static
15186 member functions. G++ helps GDB by marking the first
15187 parameter for non-static member functions (which is the this
15188 pointer) as artificial. We obtain this information from
15189 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15190 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15191 fnp->voffset = VOFFSET_STATIC;
15192 }
15193 else
15194 complaint (_("member function type missing for '%s'"),
15195 dwarf2_full_name (fieldname, die, cu));
15196
15197 /* Get fcontext from DW_AT_containing_type if present. */
15198 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15199 fnp->fcontext = die_containing_type (die, cu);
15200
15201 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15202 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15203
15204 /* Get accessibility. */
15205 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15206 switch (accessibility)
15207 {
15208 case DW_ACCESS_private:
15209 fnp->is_private = 1;
15210 break;
15211 case DW_ACCESS_protected:
15212 fnp->is_protected = 1;
15213 break;
15214 }
15215
15216 /* Check for artificial methods. */
15217 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15218 if (attr && attr->as_boolean ())
15219 fnp->is_artificial = 1;
15220
15221 /* Check for defaulted methods. */
15222 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15223 if (attr != nullptr)
15224 fnp->defaulted = attr->defaulted ();
15225
15226 /* Check for deleted methods. */
15227 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15228 if (attr != nullptr && attr->as_boolean ())
15229 fnp->is_deleted = 1;
15230
15231 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15232
15233 /* Get index in virtual function table if it is a virtual member
15234 function. For older versions of GCC, this is an offset in the
15235 appropriate virtual table, as specified by DW_AT_containing_type.
15236 For everyone else, it is an expression to be evaluated relative
15237 to the object address. */
15238
15239 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15240 if (attr != nullptr)
15241 {
15242 if (attr->form_is_block () && attr->as_block ()->size > 0)
15243 {
15244 struct dwarf_block *block = attr->as_block ();
15245
15246 if (block->data[0] == DW_OP_constu)
15247 {
15248 /* Old-style GCC. */
15249 fnp->voffset = decode_locdesc (block, cu) + 2;
15250 }
15251 else if (block->data[0] == DW_OP_deref
15252 || (block->size > 1
15253 && block->data[0] == DW_OP_deref_size
15254 && block->data[1] == cu->header.addr_size))
15255 {
15256 fnp->voffset = decode_locdesc (block, cu);
15257 if ((fnp->voffset % cu->header.addr_size) != 0)
15258 dwarf2_complex_location_expr_complaint ();
15259 else
15260 fnp->voffset /= cu->header.addr_size;
15261 fnp->voffset += 2;
15262 }
15263 else
15264 dwarf2_complex_location_expr_complaint ();
15265
15266 if (!fnp->fcontext)
15267 {
15268 /* If there is no `this' field and no DW_AT_containing_type,
15269 we cannot actually find a base class context for the
15270 vtable! */
15271 if (this_type->num_fields () == 0
15272 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15273 {
15274 complaint (_("cannot determine context for virtual member "
15275 "function \"%s\" (offset %s)"),
15276 fieldname, sect_offset_str (die->sect_off));
15277 }
15278 else
15279 {
15280 fnp->fcontext
15281 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15282 }
15283 }
15284 }
15285 else if (attr->form_is_section_offset ())
15286 {
15287 dwarf2_complex_location_expr_complaint ();
15288 }
15289 else
15290 {
15291 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15292 fieldname);
15293 }
15294 }
15295 else
15296 {
15297 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15298 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15299 {
15300 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15301 complaint (_("Member function \"%s\" (offset %s) is virtual "
15302 "but the vtable offset is not specified"),
15303 fieldname, sect_offset_str (die->sect_off));
15304 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15305 TYPE_CPLUS_DYNAMIC (type) = 1;
15306 }
15307 }
15308 }
15309
15310 /* Create the vector of member function fields, and attach it to the type. */
15311
15312 static void
15313 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15314 struct dwarf2_cu *cu)
15315 {
15316 if (cu->per_cu->lang == language_ada)
15317 error (_("unexpected member functions in Ada type"));
15318
15319 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15320 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15321 TYPE_ALLOC (type,
15322 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15323
15324 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15325 {
15326 struct fnfieldlist &nf = fip->fnfieldlists[i];
15327 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15328
15329 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15330 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15331 fn_flp->fn_fields = (struct fn_field *)
15332 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15333
15334 for (int k = 0; k < nf.fnfields.size (); ++k)
15335 fn_flp->fn_fields[k] = nf.fnfields[k];
15336 }
15337
15338 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15339 }
15340
15341 /* Returns non-zero if NAME is the name of a vtable member in CU's
15342 language, zero otherwise. */
15343 static int
15344 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15345 {
15346 static const char vptr[] = "_vptr";
15347
15348 /* Look for the C++ form of the vtable. */
15349 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15350 return 1;
15351
15352 return 0;
15353 }
15354
15355 /* GCC outputs unnamed structures that are really pointers to member
15356 functions, with the ABI-specified layout. If TYPE describes
15357 such a structure, smash it into a member function type.
15358
15359 GCC shouldn't do this; it should just output pointer to member DIEs.
15360 This is GCC PR debug/28767. */
15361
15362 static void
15363 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15364 {
15365 struct type *pfn_type, *self_type, *new_type;
15366
15367 /* Check for a structure with no name and two children. */
15368 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15369 return;
15370
15371 /* Check for __pfn and __delta members. */
15372 if (type->field (0).name () == NULL
15373 || strcmp (type->field (0).name (), "__pfn") != 0
15374 || type->field (1).name () == NULL
15375 || strcmp (type->field (1).name (), "__delta") != 0)
15376 return;
15377
15378 /* Find the type of the method. */
15379 pfn_type = type->field (0).type ();
15380 if (pfn_type == NULL
15381 || pfn_type->code () != TYPE_CODE_PTR
15382 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15383 return;
15384
15385 /* Look for the "this" argument. */
15386 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15387 if (pfn_type->num_fields () == 0
15388 /* || pfn_type->field (0).type () == NULL */
15389 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15390 return;
15391
15392 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15393 new_type = alloc_type (objfile);
15394 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15395 pfn_type->fields (), pfn_type->num_fields (),
15396 pfn_type->has_varargs ());
15397 smash_to_methodptr_type (type, new_type);
15398 }
15399
15400 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15401 requires rewriting, then copy it and return the updated copy.
15402 Otherwise return nullptr. */
15403
15404 static struct type *
15405 rewrite_array_type (struct type *type)
15406 {
15407 if (type->code () != TYPE_CODE_ARRAY)
15408 return nullptr;
15409
15410 struct type *index_type = type->index_type ();
15411 range_bounds *current_bounds = index_type->bounds ();
15412
15413 /* Handle multi-dimensional arrays. */
15414 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15415 if (new_target == nullptr)
15416 {
15417 /* Maybe we don't need to rewrite this array. */
15418 if (current_bounds->low.kind () == PROP_CONST
15419 && current_bounds->high.kind () == PROP_CONST)
15420 return nullptr;
15421 }
15422
15423 /* Either the target type was rewritten, or the bounds have to be
15424 updated. Either way we want to copy the type and update
15425 everything. */
15426 struct type *copy = copy_type (type);
15427 int nfields = copy->num_fields ();
15428 field *new_fields
15429 = ((struct field *) TYPE_ZALLOC (copy,
15430 nfields * sizeof (struct field)));
15431 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15432 copy->set_fields (new_fields);
15433 if (new_target != nullptr)
15434 TYPE_TARGET_TYPE (copy) = new_target;
15435
15436 struct type *index_copy = copy_type (index_type);
15437 range_bounds *bounds
15438 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15439 sizeof (range_bounds));
15440 *bounds = *current_bounds;
15441 bounds->low.set_const_val (1);
15442 bounds->high.set_const_val (0);
15443 index_copy->set_bounds (bounds);
15444 copy->set_index_type (index_copy);
15445
15446 return copy;
15447 }
15448
15449 /* While some versions of GCC will generate complicated DWARF for an
15450 array (see quirk_ada_thick_pointer), more recent versions were
15451 modified to emit an explicit thick pointer structure. However, in
15452 this case, the array still has DWARF expressions for its ranges,
15453 and these must be ignored. */
15454
15455 static void
15456 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15457 struct type *type)
15458 {
15459 gdb_assert (cu->per_cu->lang == language_ada);
15460
15461 /* Check for a structure with two children. */
15462 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15463 return;
15464
15465 /* Check for P_ARRAY and P_BOUNDS members. */
15466 if (type->field (0).name () == NULL
15467 || strcmp (type->field (0).name (), "P_ARRAY") != 0
15468 || type->field (1).name () == NULL
15469 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
15470 return;
15471
15472 /* Make sure we're looking at a pointer to an array. */
15473 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15474 return;
15475
15476 /* The Ada code already knows how to handle these types, so all that
15477 we need to do is turn the bounds into static bounds. However, we
15478 don't want to rewrite existing array or index types in-place,
15479 because those may be referenced in other contexts where this
15480 rewriting is undesirable. */
15481 struct type *new_ary_type
15482 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15483 if (new_ary_type != nullptr)
15484 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15485 }
15486
15487 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15488 appropriate error checking and issuing complaints if there is a
15489 problem. */
15490
15491 static ULONGEST
15492 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15493 {
15494 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15495
15496 if (attr == nullptr)
15497 return 0;
15498
15499 if (!attr->form_is_constant ())
15500 {
15501 complaint (_("DW_AT_alignment must have constant form"
15502 " - DIE at %s [in module %s]"),
15503 sect_offset_str (die->sect_off),
15504 objfile_name (cu->per_objfile->objfile));
15505 return 0;
15506 }
15507
15508 LONGEST val = attr->constant_value (0);
15509 if (val < 0)
15510 {
15511 complaint (_("DW_AT_alignment value must not be negative"
15512 " - DIE at %s [in module %s]"),
15513 sect_offset_str (die->sect_off),
15514 objfile_name (cu->per_objfile->objfile));
15515 return 0;
15516 }
15517 ULONGEST align = val;
15518
15519 if (align == 0)
15520 {
15521 complaint (_("DW_AT_alignment value must not be zero"
15522 " - DIE at %s [in module %s]"),
15523 sect_offset_str (die->sect_off),
15524 objfile_name (cu->per_objfile->objfile));
15525 return 0;
15526 }
15527 if ((align & (align - 1)) != 0)
15528 {
15529 complaint (_("DW_AT_alignment value must be a power of 2"
15530 " - DIE at %s [in module %s]"),
15531 sect_offset_str (die->sect_off),
15532 objfile_name (cu->per_objfile->objfile));
15533 return 0;
15534 }
15535
15536 return align;
15537 }
15538
15539 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15540 the alignment for TYPE. */
15541
15542 static void
15543 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15544 struct type *type)
15545 {
15546 if (!set_type_align (type, get_alignment (cu, die)))
15547 complaint (_("DW_AT_alignment value too large"
15548 " - DIE at %s [in module %s]"),
15549 sect_offset_str (die->sect_off),
15550 objfile_name (cu->per_objfile->objfile));
15551 }
15552
15553 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15554 constant for a type, according to DWARF5 spec, Table 5.5. */
15555
15556 static bool
15557 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15558 {
15559 switch (value)
15560 {
15561 case DW_CC_normal:
15562 case DW_CC_pass_by_reference:
15563 case DW_CC_pass_by_value:
15564 return true;
15565
15566 default:
15567 complaint (_("unrecognized DW_AT_calling_convention value "
15568 "(%s) for a type"), pulongest (value));
15569 return false;
15570 }
15571 }
15572
15573 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15574 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15575 also according to GNU-specific values (see include/dwarf2.h). */
15576
15577 static bool
15578 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15579 {
15580 switch (value)
15581 {
15582 case DW_CC_normal:
15583 case DW_CC_program:
15584 case DW_CC_nocall:
15585 return true;
15586
15587 case DW_CC_GNU_renesas_sh:
15588 case DW_CC_GNU_borland_fastcall_i386:
15589 case DW_CC_GDB_IBM_OpenCL:
15590 return true;
15591
15592 default:
15593 complaint (_("unrecognized DW_AT_calling_convention value "
15594 "(%s) for a subroutine"), pulongest (value));
15595 return false;
15596 }
15597 }
15598
15599 /* Called when we find the DIE that starts a structure or union scope
15600 (definition) to create a type for the structure or union. Fill in
15601 the type's name and general properties; the members will not be
15602 processed until process_structure_scope. A symbol table entry for
15603 the type will also not be done until process_structure_scope (assuming
15604 the type has a name).
15605
15606 NOTE: we need to call these functions regardless of whether or not the
15607 DIE has a DW_AT_name attribute, since it might be an anonymous
15608 structure or union. This gets the type entered into our set of
15609 user defined types. */
15610
15611 static struct type *
15612 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15613 {
15614 struct objfile *objfile = cu->per_objfile->objfile;
15615 struct type *type;
15616 struct attribute *attr;
15617 const char *name;
15618
15619 /* If the definition of this type lives in .debug_types, read that type.
15620 Don't follow DW_AT_specification though, that will take us back up
15621 the chain and we want to go down. */
15622 attr = die->attr (DW_AT_signature);
15623 if (attr != nullptr)
15624 {
15625 type = get_DW_AT_signature_type (die, attr, cu);
15626
15627 /* The type's CU may not be the same as CU.
15628 Ensure TYPE is recorded with CU in die_type_hash. */
15629 return set_die_type (die, type, cu);
15630 }
15631
15632 type = alloc_type (objfile);
15633 INIT_CPLUS_SPECIFIC (type);
15634
15635 name = dwarf2_name (die, cu);
15636 if (name != NULL)
15637 {
15638 if (cu->per_cu->lang == language_cplus
15639 || cu->per_cu->lang == language_d
15640 || cu->per_cu->lang == language_rust)
15641 {
15642 const char *full_name = dwarf2_full_name (name, die, cu);
15643
15644 /* dwarf2_full_name might have already finished building the DIE's
15645 type. If so, there is no need to continue. */
15646 if (get_die_type (die, cu) != NULL)
15647 return get_die_type (die, cu);
15648
15649 type->set_name (full_name);
15650 }
15651 else
15652 {
15653 /* The name is already allocated along with this objfile, so
15654 we don't need to duplicate it for the type. */
15655 type->set_name (name);
15656 }
15657 }
15658
15659 if (die->tag == DW_TAG_structure_type)
15660 {
15661 type->set_code (TYPE_CODE_STRUCT);
15662 }
15663 else if (die->tag == DW_TAG_union_type)
15664 {
15665 type->set_code (TYPE_CODE_UNION);
15666 }
15667 else if (die->tag == DW_TAG_namelist)
15668 {
15669 type->set_code (TYPE_CODE_NAMELIST);
15670 }
15671 else
15672 {
15673 type->set_code (TYPE_CODE_STRUCT);
15674 }
15675
15676 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
15677 type->set_is_declared_class (true);
15678
15679 /* Store the calling convention in the type if it's available in
15680 the die. Otherwise the calling convention remains set to
15681 the default value DW_CC_normal. */
15682 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15683 if (attr != nullptr
15684 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15685 {
15686 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15687 TYPE_CPLUS_CALLING_CONVENTION (type)
15688 = (enum dwarf_calling_convention) (attr->constant_value (0));
15689 }
15690
15691 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15692 if (attr != nullptr)
15693 {
15694 if (attr->form_is_constant ())
15695 TYPE_LENGTH (type) = attr->constant_value (0);
15696 else
15697 {
15698 struct dynamic_prop prop;
15699 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15700 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15701 TYPE_LENGTH (type) = 0;
15702 }
15703 }
15704 else
15705 {
15706 TYPE_LENGTH (type) = 0;
15707 }
15708
15709 maybe_set_alignment (cu, die, type);
15710
15711 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15712 {
15713 /* ICC<14 does not output the required DW_AT_declaration on
15714 incomplete types, but gives them a size of zero. */
15715 type->set_is_stub (true);
15716 }
15717 else
15718 type->set_stub_is_supported (true);
15719
15720 if (die_is_declaration (die, cu))
15721 type->set_is_stub (true);
15722 else if (attr == NULL && die->child == NULL
15723 && producer_is_realview (cu->producer))
15724 /* RealView does not output the required DW_AT_declaration
15725 on incomplete types. */
15726 type->set_is_stub (true);
15727
15728 /* We need to add the type field to the die immediately so we don't
15729 infinitely recurse when dealing with pointers to the structure
15730 type within the structure itself. */
15731 set_die_type (die, type, cu);
15732
15733 /* set_die_type should be already done. */
15734 set_descriptive_type (type, die, cu);
15735
15736 return type;
15737 }
15738
15739 static void handle_struct_member_die
15740 (struct die_info *child_die,
15741 struct type *type,
15742 struct field_info *fi,
15743 std::vector<struct symbol *> *template_args,
15744 struct dwarf2_cu *cu);
15745
15746 /* A helper for handle_struct_member_die that handles
15747 DW_TAG_variant_part. */
15748
15749 static void
15750 handle_variant_part (struct die_info *die, struct type *type,
15751 struct field_info *fi,
15752 std::vector<struct symbol *> *template_args,
15753 struct dwarf2_cu *cu)
15754 {
15755 variant_part_builder *new_part;
15756 if (fi->current_variant_part == nullptr)
15757 {
15758 fi->variant_parts.emplace_back ();
15759 new_part = &fi->variant_parts.back ();
15760 }
15761 else if (!fi->current_variant_part->processing_variant)
15762 {
15763 complaint (_("nested DW_TAG_variant_part seen "
15764 "- DIE at %s [in module %s]"),
15765 sect_offset_str (die->sect_off),
15766 objfile_name (cu->per_objfile->objfile));
15767 return;
15768 }
15769 else
15770 {
15771 variant_field &current = fi->current_variant_part->variants.back ();
15772 current.variant_parts.emplace_back ();
15773 new_part = &current.variant_parts.back ();
15774 }
15775
15776 /* When we recurse, we want callees to add to this new variant
15777 part. */
15778 scoped_restore save_current_variant_part
15779 = make_scoped_restore (&fi->current_variant_part, new_part);
15780
15781 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15782 if (discr == NULL)
15783 {
15784 /* It's a univariant form, an extension we support. */
15785 }
15786 else if (discr->form_is_ref ())
15787 {
15788 struct dwarf2_cu *target_cu = cu;
15789 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15790
15791 new_part->discriminant_offset = target_die->sect_off;
15792 }
15793 else
15794 {
15795 complaint (_("DW_AT_discr does not have DIE reference form"
15796 " - DIE at %s [in module %s]"),
15797 sect_offset_str (die->sect_off),
15798 objfile_name (cu->per_objfile->objfile));
15799 }
15800
15801 for (die_info *child_die = die->child;
15802 child_die != NULL;
15803 child_die = child_die->sibling)
15804 handle_struct_member_die (child_die, type, fi, template_args, cu);
15805 }
15806
15807 /* A helper for handle_struct_member_die that handles
15808 DW_TAG_variant. */
15809
15810 static void
15811 handle_variant (struct die_info *die, struct type *type,
15812 struct field_info *fi,
15813 std::vector<struct symbol *> *template_args,
15814 struct dwarf2_cu *cu)
15815 {
15816 if (fi->current_variant_part == nullptr)
15817 {
15818 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15819 "- DIE at %s [in module %s]"),
15820 sect_offset_str (die->sect_off),
15821 objfile_name (cu->per_objfile->objfile));
15822 return;
15823 }
15824 if (fi->current_variant_part->processing_variant)
15825 {
15826 complaint (_("nested DW_TAG_variant seen "
15827 "- DIE at %s [in module %s]"),
15828 sect_offset_str (die->sect_off),
15829 objfile_name (cu->per_objfile->objfile));
15830 return;
15831 }
15832
15833 scoped_restore save_processing_variant
15834 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15835 true);
15836
15837 fi->current_variant_part->variants.emplace_back ();
15838 variant_field &variant = fi->current_variant_part->variants.back ();
15839 variant.first_field = fi->fields.size ();
15840
15841 /* In a variant we want to get the discriminant and also add a
15842 field for our sole member child. */
15843 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15844 if (discr == nullptr || !discr->form_is_constant ())
15845 {
15846 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15847 if (discr == nullptr || discr->as_block ()->size == 0)
15848 variant.default_branch = true;
15849 else
15850 variant.discr_list_data = discr->as_block ();
15851 }
15852 else
15853 variant.discriminant_value = discr->constant_value (0);
15854
15855 for (die_info *variant_child = die->child;
15856 variant_child != NULL;
15857 variant_child = variant_child->sibling)
15858 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15859
15860 variant.last_field = fi->fields.size ();
15861 }
15862
15863 /* A helper for process_structure_scope that handles a single member
15864 DIE. */
15865
15866 static void
15867 handle_struct_member_die (struct die_info *child_die, struct type *type,
15868 struct field_info *fi,
15869 std::vector<struct symbol *> *template_args,
15870 struct dwarf2_cu *cu)
15871 {
15872 if (child_die->tag == DW_TAG_member
15873 || child_die->tag == DW_TAG_variable
15874 || child_die->tag == DW_TAG_namelist_item)
15875 {
15876 /* NOTE: carlton/2002-11-05: A C++ static data member
15877 should be a DW_TAG_member that is a declaration, but
15878 all versions of G++ as of this writing (so through at
15879 least 3.2.1) incorrectly generate DW_TAG_variable
15880 tags for them instead. */
15881 dwarf2_add_field (fi, child_die, cu);
15882 }
15883 else if (child_die->tag == DW_TAG_subprogram)
15884 {
15885 /* Rust doesn't have member functions in the C++ sense.
15886 However, it does emit ordinary functions as children
15887 of a struct DIE. */
15888 if (cu->per_cu->lang == language_rust)
15889 read_func_scope (child_die, cu);
15890 else
15891 {
15892 /* C++ member function. */
15893 dwarf2_add_member_fn (fi, child_die, type, cu);
15894 }
15895 }
15896 else if (child_die->tag == DW_TAG_inheritance)
15897 {
15898 /* C++ base class field. */
15899 dwarf2_add_field (fi, child_die, cu);
15900 }
15901 else if (type_can_define_types (child_die))
15902 dwarf2_add_type_defn (fi, child_die, cu);
15903 else if (child_die->tag == DW_TAG_template_type_param
15904 || child_die->tag == DW_TAG_template_value_param)
15905 {
15906 struct symbol *arg = new_symbol (child_die, NULL, cu);
15907
15908 if (arg != NULL)
15909 template_args->push_back (arg);
15910 }
15911 else if (child_die->tag == DW_TAG_variant_part)
15912 handle_variant_part (child_die, type, fi, template_args, cu);
15913 else if (child_die->tag == DW_TAG_variant)
15914 handle_variant (child_die, type, fi, template_args, cu);
15915 }
15916
15917 /* Finish creating a structure or union type, including filling in its
15918 members and creating a symbol for it. This function also handles Fortran
15919 namelist variables, their items or members and creating a symbol for
15920 them. */
15921
15922 static void
15923 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15924 {
15925 struct objfile *objfile = cu->per_objfile->objfile;
15926 struct die_info *child_die;
15927 struct type *type;
15928
15929 type = get_die_type (die, cu);
15930 if (type == NULL)
15931 type = read_structure_type (die, cu);
15932
15933 bool has_template_parameters = false;
15934 if (die->child != NULL && ! die_is_declaration (die, cu))
15935 {
15936 struct field_info fi;
15937 std::vector<struct symbol *> template_args;
15938
15939 child_die = die->child;
15940
15941 while (child_die && child_die->tag)
15942 {
15943 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15944 child_die = child_die->sibling;
15945 }
15946
15947 /* Attach template arguments to type. */
15948 if (!template_args.empty ())
15949 {
15950 has_template_parameters = true;
15951 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15952 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15953 TYPE_TEMPLATE_ARGUMENTS (type)
15954 = XOBNEWVEC (&objfile->objfile_obstack,
15955 struct symbol *,
15956 TYPE_N_TEMPLATE_ARGUMENTS (type));
15957 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15958 template_args.data (),
15959 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15960 * sizeof (struct symbol *)));
15961 }
15962
15963 /* Attach fields and member functions to the type. */
15964 if (fi.nfields () > 0)
15965 dwarf2_attach_fields_to_type (&fi, type, cu);
15966 if (!fi.fnfieldlists.empty ())
15967 {
15968 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15969
15970 /* Get the type which refers to the base class (possibly this
15971 class itself) which contains the vtable pointer for the current
15972 class from the DW_AT_containing_type attribute. This use of
15973 DW_AT_containing_type is a GNU extension. */
15974
15975 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15976 {
15977 struct type *t = die_containing_type (die, cu);
15978
15979 set_type_vptr_basetype (type, t);
15980 if (type == t)
15981 {
15982 int i;
15983
15984 /* Our own class provides vtbl ptr. */
15985 for (i = t->num_fields () - 1;
15986 i >= TYPE_N_BASECLASSES (t);
15987 --i)
15988 {
15989 const char *fieldname = t->field (i).name ();
15990
15991 if (is_vtable_name (fieldname, cu))
15992 {
15993 set_type_vptr_fieldno (type, i);
15994 break;
15995 }
15996 }
15997
15998 /* Complain if virtual function table field not found. */
15999 if (i < TYPE_N_BASECLASSES (t))
16000 complaint (_("virtual function table pointer "
16001 "not found when defining class '%s'"),
16002 type->name () ? type->name () : "");
16003 }
16004 else
16005 {
16006 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16007 }
16008 }
16009 else if (cu->producer
16010 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16011 {
16012 /* The IBM XLC compiler does not provide direct indication
16013 of the containing type, but the vtable pointer is
16014 always named __vfp. */
16015
16016 int i;
16017
16018 for (i = type->num_fields () - 1;
16019 i >= TYPE_N_BASECLASSES (type);
16020 --i)
16021 {
16022 if (strcmp (type->field (i).name (), "__vfp") == 0)
16023 {
16024 set_type_vptr_fieldno (type, i);
16025 set_type_vptr_basetype (type, type);
16026 break;
16027 }
16028 }
16029 }
16030 }
16031
16032 /* Copy fi.typedef_field_list linked list elements content into the
16033 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16034 if (!fi.typedef_field_list.empty ())
16035 {
16036 int count = fi.typedef_field_list.size ();
16037
16038 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16039 TYPE_TYPEDEF_FIELD_ARRAY (type)
16040 = ((struct decl_field *)
16041 TYPE_ALLOC (type,
16042 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16043 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16044
16045 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16046 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16047 }
16048
16049 /* Copy fi.nested_types_list linked list elements content into the
16050 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16051 if (!fi.nested_types_list.empty ()
16052 && cu->per_cu->lang != language_ada)
16053 {
16054 int count = fi.nested_types_list.size ();
16055
16056 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16057 TYPE_NESTED_TYPES_ARRAY (type)
16058 = ((struct decl_field *)
16059 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16060 TYPE_NESTED_TYPES_COUNT (type) = count;
16061
16062 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16063 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16064 }
16065 }
16066
16067 quirk_gcc_member_function_pointer (type, objfile);
16068 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
16069 cu->rust_unions.push_back (type);
16070 else if (cu->per_cu->lang == language_ada)
16071 quirk_ada_thick_pointer_struct (die, cu, type);
16072
16073 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16074 snapshots) has been known to create a die giving a declaration
16075 for a class that has, as a child, a die giving a definition for a
16076 nested class. So we have to process our children even if the
16077 current die is a declaration. Normally, of course, a declaration
16078 won't have any children at all. */
16079
16080 child_die = die->child;
16081
16082 while (child_die != NULL && child_die->tag)
16083 {
16084 if (child_die->tag == DW_TAG_member
16085 || child_die->tag == DW_TAG_variable
16086 || child_die->tag == DW_TAG_inheritance
16087 || child_die->tag == DW_TAG_template_value_param
16088 || child_die->tag == DW_TAG_template_type_param)
16089 {
16090 /* Do nothing. */
16091 }
16092 else
16093 process_die (child_die, cu);
16094
16095 child_die = child_die->sibling;
16096 }
16097
16098 /* Do not consider external references. According to the DWARF standard,
16099 these DIEs are identified by the fact that they have no byte_size
16100 attribute, and a declaration attribute. */
16101 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16102 || !die_is_declaration (die, cu)
16103 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16104 {
16105 struct symbol *sym = new_symbol (die, type, cu);
16106
16107 if (has_template_parameters)
16108 {
16109 struct symtab *symtab;
16110 if (sym != nullptr)
16111 symtab = symbol_symtab (sym);
16112 else if (cu->line_header != nullptr)
16113 {
16114 /* Any related symtab will do. */
16115 symtab
16116 = cu->line_header->file_names ()[0].symtab;
16117 }
16118 else
16119 {
16120 symtab = nullptr;
16121 complaint (_("could not find suitable "
16122 "symtab for template parameter"
16123 " - DIE at %s [in module %s]"),
16124 sect_offset_str (die->sect_off),
16125 objfile_name (objfile));
16126 }
16127
16128 if (symtab != nullptr)
16129 {
16130 /* Make sure that the symtab is set on the new symbols.
16131 Even though they don't appear in this symtab directly,
16132 other parts of gdb assume that symbols do, and this is
16133 reasonably true. */
16134 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16135 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16136 }
16137 }
16138 }
16139 }
16140
16141 /* Assuming DIE is an enumeration type, and TYPE is its associated
16142 type, update TYPE using some information only available in DIE's
16143 children. In particular, the fields are computed. */
16144
16145 static void
16146 update_enumeration_type_from_children (struct die_info *die,
16147 struct type *type,
16148 struct dwarf2_cu *cu)
16149 {
16150 struct die_info *child_die;
16151 int unsigned_enum = 1;
16152 int flag_enum = 1;
16153
16154 auto_obstack obstack;
16155 std::vector<struct field> fields;
16156
16157 for (child_die = die->child;
16158 child_die != NULL && child_die->tag;
16159 child_die = child_die->sibling)
16160 {
16161 struct attribute *attr;
16162 LONGEST value;
16163 const gdb_byte *bytes;
16164 struct dwarf2_locexpr_baton *baton;
16165 const char *name;
16166
16167 if (child_die->tag != DW_TAG_enumerator)
16168 continue;
16169
16170 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16171 if (attr == NULL)
16172 continue;
16173
16174 name = dwarf2_name (child_die, cu);
16175 if (name == NULL)
16176 name = "<anonymous enumerator>";
16177
16178 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16179 &value, &bytes, &baton);
16180 if (value < 0)
16181 {
16182 unsigned_enum = 0;
16183 flag_enum = 0;
16184 }
16185 else
16186 {
16187 if (count_one_bits_ll (value) >= 2)
16188 flag_enum = 0;
16189 }
16190
16191 fields.emplace_back ();
16192 struct field &field = fields.back ();
16193 field.set_name (dwarf2_physname (name, child_die, cu));
16194 field.set_loc_enumval (value);
16195 }
16196
16197 if (!fields.empty ())
16198 {
16199 type->set_num_fields (fields.size ());
16200 type->set_fields
16201 ((struct field *)
16202 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16203 memcpy (type->fields (), fields.data (),
16204 sizeof (struct field) * fields.size ());
16205 }
16206
16207 if (unsigned_enum)
16208 type->set_is_unsigned (true);
16209
16210 if (flag_enum)
16211 type->set_is_flag_enum (true);
16212 }
16213
16214 /* Given a DW_AT_enumeration_type die, set its type. We do not
16215 complete the type's fields yet, or create any symbols. */
16216
16217 static struct type *
16218 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16219 {
16220 struct objfile *objfile = cu->per_objfile->objfile;
16221 struct type *type;
16222 struct attribute *attr;
16223 const char *name;
16224
16225 /* If the definition of this type lives in .debug_types, read that type.
16226 Don't follow DW_AT_specification though, that will take us back up
16227 the chain and we want to go down. */
16228 attr = die->attr (DW_AT_signature);
16229 if (attr != nullptr)
16230 {
16231 type = get_DW_AT_signature_type (die, attr, cu);
16232
16233 /* The type's CU may not be the same as CU.
16234 Ensure TYPE is recorded with CU in die_type_hash. */
16235 return set_die_type (die, type, cu);
16236 }
16237
16238 type = alloc_type (objfile);
16239
16240 type->set_code (TYPE_CODE_ENUM);
16241 name = dwarf2_full_name (NULL, die, cu);
16242 if (name != NULL)
16243 type->set_name (name);
16244
16245 attr = dwarf2_attr (die, DW_AT_type, cu);
16246 if (attr != NULL)
16247 {
16248 struct type *underlying_type = die_type (die, cu);
16249
16250 TYPE_TARGET_TYPE (type) = underlying_type;
16251 }
16252
16253 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16254 if (attr != nullptr)
16255 {
16256 TYPE_LENGTH (type) = attr->constant_value (0);
16257 }
16258 else
16259 {
16260 TYPE_LENGTH (type) = 0;
16261 }
16262
16263 maybe_set_alignment (cu, die, type);
16264
16265 /* The enumeration DIE can be incomplete. In Ada, any type can be
16266 declared as private in the package spec, and then defined only
16267 inside the package body. Such types are known as Taft Amendment
16268 Types. When another package uses such a type, an incomplete DIE
16269 may be generated by the compiler. */
16270 if (die_is_declaration (die, cu))
16271 type->set_is_stub (true);
16272
16273 /* If this type has an underlying type that is not a stub, then we
16274 may use its attributes. We always use the "unsigned" attribute
16275 in this situation, because ordinarily we guess whether the type
16276 is unsigned -- but the guess can be wrong and the underlying type
16277 can tell us the reality. However, we defer to a local size
16278 attribute if one exists, because this lets the compiler override
16279 the underlying type if needed. */
16280 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16281 {
16282 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16283 underlying_type = check_typedef (underlying_type);
16284
16285 type->set_is_unsigned (underlying_type->is_unsigned ());
16286
16287 if (TYPE_LENGTH (type) == 0)
16288 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16289
16290 if (TYPE_RAW_ALIGN (type) == 0
16291 && TYPE_RAW_ALIGN (underlying_type) != 0)
16292 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16293 }
16294
16295 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16296
16297 set_die_type (die, type, cu);
16298
16299 /* Finish the creation of this type by using the enum's children.
16300 Note that, as usual, this must come after set_die_type to avoid
16301 infinite recursion when trying to compute the names of the
16302 enumerators. */
16303 update_enumeration_type_from_children (die, type, cu);
16304
16305 return type;
16306 }
16307
16308 /* Given a pointer to a die which begins an enumeration, process all
16309 the dies that define the members of the enumeration, and create the
16310 symbol for the enumeration type.
16311
16312 NOTE: We reverse the order of the element list. */
16313
16314 static void
16315 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16316 {
16317 struct type *this_type;
16318
16319 this_type = get_die_type (die, cu);
16320 if (this_type == NULL)
16321 this_type = read_enumeration_type (die, cu);
16322
16323 if (die->child != NULL)
16324 {
16325 struct die_info *child_die;
16326 const char *name;
16327
16328 child_die = die->child;
16329 while (child_die && child_die->tag)
16330 {
16331 if (child_die->tag != DW_TAG_enumerator)
16332 {
16333 process_die (child_die, cu);
16334 }
16335 else
16336 {
16337 name = dwarf2_name (child_die, cu);
16338 if (name)
16339 new_symbol (child_die, this_type, cu);
16340 }
16341
16342 child_die = child_die->sibling;
16343 }
16344 }
16345
16346 /* If we are reading an enum from a .debug_types unit, and the enum
16347 is a declaration, and the enum is not the signatured type in the
16348 unit, then we do not want to add a symbol for it. Adding a
16349 symbol would in some cases obscure the true definition of the
16350 enum, giving users an incomplete type when the definition is
16351 actually available. Note that we do not want to do this for all
16352 enums which are just declarations, because C++0x allows forward
16353 enum declarations. */
16354 if (cu->per_cu->is_debug_types
16355 && die_is_declaration (die, cu))
16356 {
16357 struct signatured_type *sig_type;
16358
16359 sig_type = (struct signatured_type *) cu->per_cu;
16360 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16361 if (sig_type->type_offset_in_section != die->sect_off)
16362 return;
16363 }
16364
16365 new_symbol (die, this_type, cu);
16366 }
16367
16368 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16369 expression for an index type and finds the corresponding field
16370 offset in the hidden "P_BOUNDS" structure. Returns true on success
16371 and updates *FIELD, false if it fails to recognize an
16372 expression. */
16373
16374 static bool
16375 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16376 int *bounds_offset, struct field *field,
16377 struct dwarf2_cu *cu)
16378 {
16379 struct attribute *attr = dwarf2_attr (die, name, cu);
16380 if (attr == nullptr || !attr->form_is_block ())
16381 return false;
16382
16383 const struct dwarf_block *block = attr->as_block ();
16384 const gdb_byte *start = block->data;
16385 const gdb_byte *end = block->data + block->size;
16386
16387 /* The expression to recognize generally looks like:
16388
16389 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16390 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16391
16392 However, the second "plus_uconst" may be missing:
16393
16394 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16395 DW_OP_deref_size: 4)
16396
16397 This happens when the field is at the start of the structure.
16398
16399 Also, the final deref may not be sized:
16400
16401 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16402 DW_OP_deref)
16403
16404 This happens when the size of the index type happens to be the
16405 same as the architecture's word size. This can occur with or
16406 without the second plus_uconst. */
16407
16408 if (end - start < 2)
16409 return false;
16410 if (*start++ != DW_OP_push_object_address)
16411 return false;
16412 if (*start++ != DW_OP_plus_uconst)
16413 return false;
16414
16415 uint64_t this_bound_off;
16416 start = gdb_read_uleb128 (start, end, &this_bound_off);
16417 if (start == nullptr || (int) this_bound_off != this_bound_off)
16418 return false;
16419 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16420 is consistent among all bounds. */
16421 if (*bounds_offset == -1)
16422 *bounds_offset = this_bound_off;
16423 else if (*bounds_offset != this_bound_off)
16424 return false;
16425
16426 if (start == end || *start++ != DW_OP_deref)
16427 return false;
16428
16429 int offset = 0;
16430 if (start ==end)
16431 return false;
16432 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16433 {
16434 /* This means an offset of 0. */
16435 }
16436 else if (*start++ != DW_OP_plus_uconst)
16437 return false;
16438 else
16439 {
16440 /* The size is the parameter to DW_OP_plus_uconst. */
16441 uint64_t val;
16442 start = gdb_read_uleb128 (start, end, &val);
16443 if (start == nullptr)
16444 return false;
16445 if ((int) val != val)
16446 return false;
16447 offset = val;
16448 }
16449
16450 if (start == end)
16451 return false;
16452
16453 uint64_t size;
16454 if (*start == DW_OP_deref_size)
16455 {
16456 start = gdb_read_uleb128 (start + 1, end, &size);
16457 if (start == nullptr)
16458 return false;
16459 }
16460 else if (*start == DW_OP_deref)
16461 {
16462 size = cu->header.addr_size;
16463 ++start;
16464 }
16465 else
16466 return false;
16467
16468 field->set_loc_bitpos (8 * offset);
16469 if (size != TYPE_LENGTH (field->type ()))
16470 FIELD_BITSIZE (*field) = 8 * size;
16471
16472 return true;
16473 }
16474
16475 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16476 some kinds of Ada arrays:
16477
16478 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16479 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16480 <11e0> DW_AT_data_location: 2 byte block: 97 6
16481 (DW_OP_push_object_address; DW_OP_deref)
16482 <11e3> DW_AT_type : <0x1173>
16483 <11e7> DW_AT_sibling : <0x1201>
16484 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16485 <11ec> DW_AT_type : <0x1206>
16486 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16487 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16488 DW_OP_deref_size: 4)
16489 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16490 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16491 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16492
16493 This actually represents a "thick pointer", which is a structure
16494 with two elements: one that is a pointer to the array data, and one
16495 that is a pointer to another structure; this second structure holds
16496 the array bounds.
16497
16498 This returns a new type on success, or nullptr if this didn't
16499 recognize the type. */
16500
16501 static struct type *
16502 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16503 struct type *type)
16504 {
16505 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16506 /* So far we've only seen this with block form. */
16507 if (attr == nullptr || !attr->form_is_block ())
16508 return nullptr;
16509
16510 /* Note that this will fail if the structure layout is changed by
16511 the compiler. However, we have no good way to recognize some
16512 other layout, because we don't know what expression the compiler
16513 might choose to emit should this happen. */
16514 struct dwarf_block *blk = attr->as_block ();
16515 if (blk->size != 2
16516 || blk->data[0] != DW_OP_push_object_address
16517 || blk->data[1] != DW_OP_deref)
16518 return nullptr;
16519
16520 int bounds_offset = -1;
16521 int max_align = -1;
16522 std::vector<struct field> range_fields;
16523 for (struct die_info *child_die = die->child;
16524 child_die;
16525 child_die = child_die->sibling)
16526 {
16527 if (child_die->tag == DW_TAG_subrange_type)
16528 {
16529 struct type *underlying = read_subrange_index_type (child_die, cu);
16530
16531 int this_align = type_align (underlying);
16532 if (this_align > max_align)
16533 max_align = this_align;
16534
16535 range_fields.emplace_back ();
16536 range_fields.emplace_back ();
16537
16538 struct field &lower = range_fields[range_fields.size () - 2];
16539 struct field &upper = range_fields[range_fields.size () - 1];
16540
16541 lower.set_type (underlying);
16542 FIELD_ARTIFICIAL (lower) = 1;
16543
16544 upper.set_type (underlying);
16545 FIELD_ARTIFICIAL (upper) = 1;
16546
16547 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16548 &bounds_offset, &lower, cu)
16549 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16550 &bounds_offset, &upper, cu))
16551 return nullptr;
16552 }
16553 }
16554
16555 /* This shouldn't really happen, but double-check that we found
16556 where the bounds are stored. */
16557 if (bounds_offset == -1)
16558 return nullptr;
16559
16560 struct objfile *objfile = cu->per_objfile->objfile;
16561 for (int i = 0; i < range_fields.size (); i += 2)
16562 {
16563 char name[20];
16564
16565 /* Set the name of each field in the bounds. */
16566 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16567 range_fields[i].set_name (objfile->intern (name));
16568 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16569 range_fields[i + 1].set_name (objfile->intern (name));
16570 }
16571
16572 struct type *bounds = alloc_type (objfile);
16573 bounds->set_code (TYPE_CODE_STRUCT);
16574
16575 bounds->set_num_fields (range_fields.size ());
16576 bounds->set_fields
16577 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16578 * sizeof (struct field))));
16579 memcpy (bounds->fields (), range_fields.data (),
16580 bounds->num_fields () * sizeof (struct field));
16581
16582 int last_fieldno = range_fields.size () - 1;
16583 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
16584 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16585 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16586
16587 /* Rewrite the existing array type in place. Specifically, we
16588 remove any dynamic properties we might have read, and we replace
16589 the index types. */
16590 struct type *iter = type;
16591 for (int i = 0; i < range_fields.size (); i += 2)
16592 {
16593 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16594 iter->main_type->dyn_prop_list = nullptr;
16595 iter->set_index_type
16596 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16597 iter = TYPE_TARGET_TYPE (iter);
16598 }
16599
16600 struct type *result = alloc_type (objfile);
16601 result->set_code (TYPE_CODE_STRUCT);
16602
16603 result->set_num_fields (2);
16604 result->set_fields
16605 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16606 * sizeof (struct field))));
16607
16608 /* The names are chosen to coincide with what the compiler does with
16609 -fgnat-encodings=all, which the Ada code in gdb already
16610 understands. */
16611 result->field (0).set_name ("P_ARRAY");
16612 result->field (0).set_type (lookup_pointer_type (type));
16613
16614 result->field (1).set_name ("P_BOUNDS");
16615 result->field (1).set_type (lookup_pointer_type (bounds));
16616 result->field (1).set_loc_bitpos (8 * bounds_offset);
16617
16618 result->set_name (type->name ());
16619 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16620 + TYPE_LENGTH (result->field (1).type ()));
16621
16622 return result;
16623 }
16624
16625 /* Extract all information from a DW_TAG_array_type DIE and put it in
16626 the DIE's type field. For now, this only handles one dimensional
16627 arrays. */
16628
16629 static struct type *
16630 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16631 {
16632 struct objfile *objfile = cu->per_objfile->objfile;
16633 struct die_info *child_die;
16634 struct type *type;
16635 struct type *element_type, *range_type, *index_type;
16636 struct attribute *attr;
16637 const char *name;
16638 struct dynamic_prop *byte_stride_prop = NULL;
16639 unsigned int bit_stride = 0;
16640
16641 element_type = die_type (die, cu);
16642
16643 /* The die_type call above may have already set the type for this DIE. */
16644 type = get_die_type (die, cu);
16645 if (type)
16646 return type;
16647
16648 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16649 if (attr != NULL)
16650 {
16651 int stride_ok;
16652 struct type *prop_type = cu->addr_sized_int_type (false);
16653
16654 byte_stride_prop
16655 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16656 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16657 prop_type);
16658 if (!stride_ok)
16659 {
16660 complaint (_("unable to read array DW_AT_byte_stride "
16661 " - DIE at %s [in module %s]"),
16662 sect_offset_str (die->sect_off),
16663 objfile_name (cu->per_objfile->objfile));
16664 /* Ignore this attribute. We will likely not be able to print
16665 arrays of this type correctly, but there is little we can do
16666 to help if we cannot read the attribute's value. */
16667 byte_stride_prop = NULL;
16668 }
16669 }
16670
16671 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16672 if (attr != NULL)
16673 bit_stride = attr->constant_value (0);
16674
16675 /* Irix 6.2 native cc creates array types without children for
16676 arrays with unspecified length. */
16677 if (die->child == NULL)
16678 {
16679 index_type = objfile_type (objfile)->builtin_int;
16680 range_type = create_static_range_type (NULL, index_type, 0, -1);
16681 type = create_array_type_with_stride (NULL, element_type, range_type,
16682 byte_stride_prop, bit_stride);
16683 return set_die_type (die, type, cu);
16684 }
16685
16686 std::vector<struct type *> range_types;
16687 child_die = die->child;
16688 while (child_die && child_die->tag)
16689 {
16690 if (child_die->tag == DW_TAG_subrange_type)
16691 {
16692 struct type *child_type = read_type_die (child_die, cu);
16693
16694 if (child_type != NULL)
16695 {
16696 /* The range type was succesfully read. Save it for the
16697 array type creation. */
16698 range_types.push_back (child_type);
16699 }
16700 }
16701 child_die = child_die->sibling;
16702 }
16703
16704 if (range_types.empty ())
16705 {
16706 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16707 sect_offset_str (die->sect_off),
16708 objfile_name (cu->per_objfile->objfile));
16709 return NULL;
16710 }
16711
16712 /* Dwarf2 dimensions are output from left to right, create the
16713 necessary array types in backwards order. */
16714
16715 type = element_type;
16716
16717 if (read_array_order (die, cu) == DW_ORD_col_major)
16718 {
16719 int i = 0;
16720
16721 while (i < range_types.size ())
16722 {
16723 type = create_array_type_with_stride (NULL, type, range_types[i++],
16724 byte_stride_prop, bit_stride);
16725 bit_stride = 0;
16726 byte_stride_prop = nullptr;
16727 }
16728 }
16729 else
16730 {
16731 size_t ndim = range_types.size ();
16732 while (ndim-- > 0)
16733 {
16734 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16735 byte_stride_prop, bit_stride);
16736 bit_stride = 0;
16737 byte_stride_prop = nullptr;
16738 }
16739 }
16740
16741 gdb_assert (type != element_type);
16742
16743 /* Understand Dwarf2 support for vector types (like they occur on
16744 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16745 array type. This is not part of the Dwarf2/3 standard yet, but a
16746 custom vendor extension. The main difference between a regular
16747 array and the vector variant is that vectors are passed by value
16748 to functions. */
16749 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16750 if (attr != nullptr)
16751 make_vector_type (type);
16752
16753 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16754 implementation may choose to implement triple vectors using this
16755 attribute. */
16756 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16757 if (attr != nullptr && attr->form_is_unsigned ())
16758 {
16759 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16760 TYPE_LENGTH (type) = attr->as_unsigned ();
16761 else
16762 complaint (_("DW_AT_byte_size for array type smaller "
16763 "than the total size of elements"));
16764 }
16765
16766 name = dwarf2_name (die, cu);
16767 if (name)
16768 type->set_name (name);
16769
16770 maybe_set_alignment (cu, die, type);
16771
16772 struct type *replacement_type = nullptr;
16773 if (cu->per_cu->lang == language_ada)
16774 {
16775 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16776 if (replacement_type != nullptr)
16777 type = replacement_type;
16778 }
16779
16780 /* Install the type in the die. */
16781 set_die_type (die, type, cu, replacement_type != nullptr);
16782
16783 /* set_die_type should be already done. */
16784 set_descriptive_type (type, die, cu);
16785
16786 return type;
16787 }
16788
16789 static enum dwarf_array_dim_ordering
16790 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16791 {
16792 struct attribute *attr;
16793
16794 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16795
16796 if (attr != nullptr)
16797 {
16798 LONGEST val = attr->constant_value (-1);
16799 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16800 return (enum dwarf_array_dim_ordering) val;
16801 }
16802
16803 /* GNU F77 is a special case, as at 08/2004 array type info is the
16804 opposite order to the dwarf2 specification, but data is still
16805 laid out as per normal fortran.
16806
16807 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16808 version checking. */
16809
16810 if (cu->per_cu->lang == language_fortran
16811 && cu->producer && strstr (cu->producer, "GNU F77"))
16812 {
16813 return DW_ORD_row_major;
16814 }
16815
16816 switch (cu->language_defn->array_ordering ())
16817 {
16818 case array_column_major:
16819 return DW_ORD_col_major;
16820 case array_row_major:
16821 default:
16822 return DW_ORD_row_major;
16823 };
16824 }
16825
16826 /* Extract all information from a DW_TAG_set_type DIE and put it in
16827 the DIE's type field. */
16828
16829 static struct type *
16830 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16831 {
16832 struct type *domain_type, *set_type;
16833 struct attribute *attr;
16834
16835 domain_type = die_type (die, cu);
16836
16837 /* The die_type call above may have already set the type for this DIE. */
16838 set_type = get_die_type (die, cu);
16839 if (set_type)
16840 return set_type;
16841
16842 set_type = create_set_type (NULL, domain_type);
16843
16844 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16845 if (attr != nullptr && attr->form_is_unsigned ())
16846 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16847
16848 maybe_set_alignment (cu, die, set_type);
16849
16850 return set_die_type (die, set_type, cu);
16851 }
16852
16853 /* A helper for read_common_block that creates a locexpr baton.
16854 SYM is the symbol which we are marking as computed.
16855 COMMON_DIE is the DIE for the common block.
16856 COMMON_LOC is the location expression attribute for the common
16857 block itself.
16858 MEMBER_LOC is the location expression attribute for the particular
16859 member of the common block that we are processing.
16860 CU is the CU from which the above come. */
16861
16862 static void
16863 mark_common_block_symbol_computed (struct symbol *sym,
16864 struct die_info *common_die,
16865 struct attribute *common_loc,
16866 struct attribute *member_loc,
16867 struct dwarf2_cu *cu)
16868 {
16869 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16870 struct objfile *objfile = per_objfile->objfile;
16871 struct dwarf2_locexpr_baton *baton;
16872 gdb_byte *ptr;
16873 unsigned int cu_off;
16874 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16875 LONGEST offset = 0;
16876
16877 gdb_assert (common_loc && member_loc);
16878 gdb_assert (common_loc->form_is_block ());
16879 gdb_assert (member_loc->form_is_block ()
16880 || member_loc->form_is_constant ());
16881
16882 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16883 baton->per_objfile = per_objfile;
16884 baton->per_cu = cu->per_cu;
16885 gdb_assert (baton->per_cu);
16886
16887 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16888
16889 if (member_loc->form_is_constant ())
16890 {
16891 offset = member_loc->constant_value (0);
16892 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16893 }
16894 else
16895 baton->size += member_loc->as_block ()->size;
16896
16897 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16898 baton->data = ptr;
16899
16900 *ptr++ = DW_OP_call4;
16901 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16902 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16903 ptr += 4;
16904
16905 if (member_loc->form_is_constant ())
16906 {
16907 *ptr++ = DW_OP_addr;
16908 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16909 ptr += cu->header.addr_size;
16910 }
16911 else
16912 {
16913 /* We have to copy the data here, because DW_OP_call4 will only
16914 use a DW_AT_location attribute. */
16915 struct dwarf_block *block = member_loc->as_block ();
16916 memcpy (ptr, block->data, block->size);
16917 ptr += block->size;
16918 }
16919
16920 *ptr++ = DW_OP_plus;
16921 gdb_assert (ptr - baton->data == baton->size);
16922
16923 SYMBOL_LOCATION_BATON (sym) = baton;
16924 sym->set_aclass_index (dwarf2_locexpr_index);
16925 }
16926
16927 /* Create appropriate locally-scoped variables for all the
16928 DW_TAG_common_block entries. Also create a struct common_block
16929 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16930 is used to separate the common blocks name namespace from regular
16931 variable names. */
16932
16933 static void
16934 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16935 {
16936 struct attribute *attr;
16937
16938 attr = dwarf2_attr (die, DW_AT_location, cu);
16939 if (attr != nullptr)
16940 {
16941 /* Support the .debug_loc offsets. */
16942 if (attr->form_is_block ())
16943 {
16944 /* Ok. */
16945 }
16946 else if (attr->form_is_section_offset ())
16947 {
16948 dwarf2_complex_location_expr_complaint ();
16949 attr = NULL;
16950 }
16951 else
16952 {
16953 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16954 "common block member");
16955 attr = NULL;
16956 }
16957 }
16958
16959 if (die->child != NULL)
16960 {
16961 struct objfile *objfile = cu->per_objfile->objfile;
16962 struct die_info *child_die;
16963 size_t n_entries = 0, size;
16964 struct common_block *common_block;
16965 struct symbol *sym;
16966
16967 for (child_die = die->child;
16968 child_die && child_die->tag;
16969 child_die = child_die->sibling)
16970 ++n_entries;
16971
16972 size = (sizeof (struct common_block)
16973 + (n_entries - 1) * sizeof (struct symbol *));
16974 common_block
16975 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16976 size);
16977 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16978 common_block->n_entries = 0;
16979
16980 for (child_die = die->child;
16981 child_die && child_die->tag;
16982 child_die = child_die->sibling)
16983 {
16984 /* Create the symbol in the DW_TAG_common_block block in the current
16985 symbol scope. */
16986 sym = new_symbol (child_die, NULL, cu);
16987 if (sym != NULL)
16988 {
16989 struct attribute *member_loc;
16990
16991 common_block->contents[common_block->n_entries++] = sym;
16992
16993 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16994 cu);
16995 if (member_loc)
16996 {
16997 /* GDB has handled this for a long time, but it is
16998 not specified by DWARF. It seems to have been
16999 emitted by gfortran at least as recently as:
17000 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17001 complaint (_("Variable in common block has "
17002 "DW_AT_data_member_location "
17003 "- DIE at %s [in module %s]"),
17004 sect_offset_str (child_die->sect_off),
17005 objfile_name (objfile));
17006
17007 if (member_loc->form_is_section_offset ())
17008 dwarf2_complex_location_expr_complaint ();
17009 else if (member_loc->form_is_constant ()
17010 || member_loc->form_is_block ())
17011 {
17012 if (attr != nullptr)
17013 mark_common_block_symbol_computed (sym, die, attr,
17014 member_loc, cu);
17015 }
17016 else
17017 dwarf2_complex_location_expr_complaint ();
17018 }
17019 }
17020 }
17021
17022 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17023 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17024 }
17025 }
17026
17027 /* Create a type for a C++ namespace. */
17028
17029 static struct type *
17030 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17031 {
17032 struct objfile *objfile = cu->per_objfile->objfile;
17033 const char *previous_prefix, *name;
17034 int is_anonymous;
17035 struct type *type;
17036
17037 /* For extensions, reuse the type of the original namespace. */
17038 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17039 {
17040 struct die_info *ext_die;
17041 struct dwarf2_cu *ext_cu = cu;
17042
17043 ext_die = dwarf2_extension (die, &ext_cu);
17044 type = read_type_die (ext_die, ext_cu);
17045
17046 /* EXT_CU may not be the same as CU.
17047 Ensure TYPE is recorded with CU in die_type_hash. */
17048 return set_die_type (die, type, cu);
17049 }
17050
17051 name = namespace_name (die, &is_anonymous, cu);
17052
17053 /* Now build the name of the current namespace. */
17054
17055 previous_prefix = determine_prefix (die, cu);
17056 if (previous_prefix[0] != '\0')
17057 name = typename_concat (&objfile->objfile_obstack,
17058 previous_prefix, name, 0, cu);
17059
17060 /* Create the type. */
17061 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17062
17063 return set_die_type (die, type, cu);
17064 }
17065
17066 /* Read a namespace scope. */
17067
17068 static void
17069 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17070 {
17071 struct objfile *objfile = cu->per_objfile->objfile;
17072 int is_anonymous;
17073
17074 /* Add a symbol associated to this if we haven't seen the namespace
17075 before. Also, add a using directive if it's an anonymous
17076 namespace. */
17077
17078 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17079 {
17080 struct type *type;
17081
17082 type = read_type_die (die, cu);
17083 new_symbol (die, type, cu);
17084
17085 namespace_name (die, &is_anonymous, cu);
17086 if (is_anonymous)
17087 {
17088 const char *previous_prefix = determine_prefix (die, cu);
17089
17090 std::vector<const char *> excludes;
17091 add_using_directive (using_directives (cu),
17092 previous_prefix, type->name (), NULL,
17093 NULL, excludes, 0, &objfile->objfile_obstack);
17094 }
17095 }
17096
17097 if (die->child != NULL)
17098 {
17099 struct die_info *child_die = die->child;
17100
17101 while (child_die && child_die->tag)
17102 {
17103 process_die (child_die, cu);
17104 child_die = child_die->sibling;
17105 }
17106 }
17107 }
17108
17109 /* Read a Fortran module as type. This DIE can be only a declaration used for
17110 imported module. Still we need that type as local Fortran "use ... only"
17111 declaration imports depend on the created type in determine_prefix. */
17112
17113 static struct type *
17114 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17115 {
17116 struct objfile *objfile = cu->per_objfile->objfile;
17117 const char *module_name;
17118 struct type *type;
17119
17120 module_name = dwarf2_name (die, cu);
17121 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17122
17123 return set_die_type (die, type, cu);
17124 }
17125
17126 /* Read a Fortran module. */
17127
17128 static void
17129 read_module (struct die_info *die, struct dwarf2_cu *cu)
17130 {
17131 struct die_info *child_die = die->child;
17132 struct type *type;
17133
17134 type = read_type_die (die, cu);
17135 new_symbol (die, type, cu);
17136
17137 while (child_die && child_die->tag)
17138 {
17139 process_die (child_die, cu);
17140 child_die = child_die->sibling;
17141 }
17142 }
17143
17144 /* Return the name of the namespace represented by DIE. Set
17145 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17146 namespace. */
17147
17148 static const char *
17149 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17150 {
17151 struct die_info *current_die;
17152 const char *name = NULL;
17153
17154 /* Loop through the extensions until we find a name. */
17155
17156 for (current_die = die;
17157 current_die != NULL;
17158 current_die = dwarf2_extension (die, &cu))
17159 {
17160 /* We don't use dwarf2_name here so that we can detect the absence
17161 of a name -> anonymous namespace. */
17162 name = dwarf2_string_attr (die, DW_AT_name, cu);
17163
17164 if (name != NULL)
17165 break;
17166 }
17167
17168 /* Is it an anonymous namespace? */
17169
17170 *is_anonymous = (name == NULL);
17171 if (*is_anonymous)
17172 name = CP_ANONYMOUS_NAMESPACE_STR;
17173
17174 return name;
17175 }
17176
17177 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17178 the user defined type vector. */
17179
17180 static struct type *
17181 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17182 {
17183 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17184 struct comp_unit_head *cu_header = &cu->header;
17185 struct type *type;
17186 struct attribute *attr_byte_size;
17187 struct attribute *attr_address_class;
17188 int byte_size, addr_class;
17189 struct type *target_type;
17190
17191 target_type = die_type (die, cu);
17192
17193 /* The die_type call above may have already set the type for this DIE. */
17194 type = get_die_type (die, cu);
17195 if (type)
17196 return type;
17197
17198 type = lookup_pointer_type (target_type);
17199
17200 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17201 if (attr_byte_size)
17202 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17203 else
17204 byte_size = cu_header->addr_size;
17205
17206 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17207 if (attr_address_class)
17208 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17209 else
17210 addr_class = DW_ADDR_none;
17211
17212 ULONGEST alignment = get_alignment (cu, die);
17213
17214 /* If the pointer size, alignment, or address class is different
17215 than the default, create a type variant marked as such and set
17216 the length accordingly. */
17217 if (TYPE_LENGTH (type) != byte_size
17218 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17219 && alignment != TYPE_RAW_ALIGN (type))
17220 || addr_class != DW_ADDR_none)
17221 {
17222 if (gdbarch_address_class_type_flags_p (gdbarch))
17223 {
17224 type_instance_flags type_flags
17225 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17226 addr_class);
17227 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17228 == 0);
17229 type = make_type_with_address_space (type, type_flags);
17230 }
17231 else if (TYPE_LENGTH (type) != byte_size)
17232 {
17233 complaint (_("invalid pointer size %d"), byte_size);
17234 }
17235 else if (TYPE_RAW_ALIGN (type) != alignment)
17236 {
17237 complaint (_("Invalid DW_AT_alignment"
17238 " - DIE at %s [in module %s]"),
17239 sect_offset_str (die->sect_off),
17240 objfile_name (cu->per_objfile->objfile));
17241 }
17242 else
17243 {
17244 /* Should we also complain about unhandled address classes? */
17245 }
17246 }
17247
17248 TYPE_LENGTH (type) = byte_size;
17249 set_type_align (type, alignment);
17250 return set_die_type (die, type, cu);
17251 }
17252
17253 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17254 the user defined type vector. */
17255
17256 static struct type *
17257 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17258 {
17259 struct type *type;
17260 struct type *to_type;
17261 struct type *domain;
17262
17263 to_type = die_type (die, cu);
17264 domain = die_containing_type (die, cu);
17265
17266 /* The calls above may have already set the type for this DIE. */
17267 type = get_die_type (die, cu);
17268 if (type)
17269 return type;
17270
17271 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17272 type = lookup_methodptr_type (to_type);
17273 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17274 {
17275 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17276
17277 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17278 to_type->fields (), to_type->num_fields (),
17279 to_type->has_varargs ());
17280 type = lookup_methodptr_type (new_type);
17281 }
17282 else
17283 type = lookup_memberptr_type (to_type, domain);
17284
17285 return set_die_type (die, type, cu);
17286 }
17287
17288 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17289 the user defined type vector. */
17290
17291 static struct type *
17292 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17293 enum type_code refcode)
17294 {
17295 struct comp_unit_head *cu_header = &cu->header;
17296 struct type *type, *target_type;
17297 struct attribute *attr;
17298
17299 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17300
17301 target_type = die_type (die, cu);
17302
17303 /* The die_type call above may have already set the type for this DIE. */
17304 type = get_die_type (die, cu);
17305 if (type)
17306 return type;
17307
17308 type = lookup_reference_type (target_type, refcode);
17309 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17310 if (attr != nullptr)
17311 {
17312 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17313 }
17314 else
17315 {
17316 TYPE_LENGTH (type) = cu_header->addr_size;
17317 }
17318 maybe_set_alignment (cu, die, type);
17319 return set_die_type (die, type, cu);
17320 }
17321
17322 /* Add the given cv-qualifiers to the element type of the array. GCC
17323 outputs DWARF type qualifiers that apply to an array, not the
17324 element type. But GDB relies on the array element type to carry
17325 the cv-qualifiers. This mimics section 6.7.3 of the C99
17326 specification. */
17327
17328 static struct type *
17329 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17330 struct type *base_type, int cnst, int voltl)
17331 {
17332 struct type *el_type, *inner_array;
17333
17334 base_type = copy_type (base_type);
17335 inner_array = base_type;
17336
17337 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17338 {
17339 TYPE_TARGET_TYPE (inner_array) =
17340 copy_type (TYPE_TARGET_TYPE (inner_array));
17341 inner_array = TYPE_TARGET_TYPE (inner_array);
17342 }
17343
17344 el_type = TYPE_TARGET_TYPE (inner_array);
17345 cnst |= TYPE_CONST (el_type);
17346 voltl |= TYPE_VOLATILE (el_type);
17347 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17348
17349 return set_die_type (die, base_type, cu);
17350 }
17351
17352 static struct type *
17353 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17354 {
17355 struct type *base_type, *cv_type;
17356
17357 base_type = die_type (die, cu);
17358
17359 /* The die_type call above may have already set the type for this DIE. */
17360 cv_type = get_die_type (die, cu);
17361 if (cv_type)
17362 return cv_type;
17363
17364 /* In case the const qualifier is applied to an array type, the element type
17365 is so qualified, not the array type (section 6.7.3 of C99). */
17366 if (base_type->code () == TYPE_CODE_ARRAY)
17367 return add_array_cv_type (die, cu, base_type, 1, 0);
17368
17369 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17370 return set_die_type (die, cv_type, cu);
17371 }
17372
17373 static struct type *
17374 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17375 {
17376 struct type *base_type, *cv_type;
17377
17378 base_type = die_type (die, cu);
17379
17380 /* The die_type call above may have already set the type for this DIE. */
17381 cv_type = get_die_type (die, cu);
17382 if (cv_type)
17383 return cv_type;
17384
17385 /* In case the volatile qualifier is applied to an array type, the
17386 element type is so qualified, not the array type (section 6.7.3
17387 of C99). */
17388 if (base_type->code () == TYPE_CODE_ARRAY)
17389 return add_array_cv_type (die, cu, base_type, 0, 1);
17390
17391 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17392 return set_die_type (die, cv_type, cu);
17393 }
17394
17395 /* Handle DW_TAG_restrict_type. */
17396
17397 static struct type *
17398 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17399 {
17400 struct type *base_type, *cv_type;
17401
17402 base_type = die_type (die, cu);
17403
17404 /* The die_type call above may have already set the type for this DIE. */
17405 cv_type = get_die_type (die, cu);
17406 if (cv_type)
17407 return cv_type;
17408
17409 cv_type = make_restrict_type (base_type);
17410 return set_die_type (die, cv_type, cu);
17411 }
17412
17413 /* Handle DW_TAG_atomic_type. */
17414
17415 static struct type *
17416 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17417 {
17418 struct type *base_type, *cv_type;
17419
17420 base_type = die_type (die, cu);
17421
17422 /* The die_type call above may have already set the type for this DIE. */
17423 cv_type = get_die_type (die, cu);
17424 if (cv_type)
17425 return cv_type;
17426
17427 cv_type = make_atomic_type (base_type);
17428 return set_die_type (die, cv_type, cu);
17429 }
17430
17431 /* Extract all information from a DW_TAG_string_type DIE and add to
17432 the user defined type vector. It isn't really a user defined type,
17433 but it behaves like one, with other DIE's using an AT_user_def_type
17434 attribute to reference it. */
17435
17436 static struct type *
17437 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17438 {
17439 struct objfile *objfile = cu->per_objfile->objfile;
17440 struct gdbarch *gdbarch = objfile->arch ();
17441 struct type *type, *range_type, *index_type, *char_type;
17442 struct attribute *attr;
17443 struct dynamic_prop prop;
17444 bool length_is_constant = true;
17445 LONGEST length;
17446
17447 /* There are a couple of places where bit sizes might be made use of
17448 when parsing a DW_TAG_string_type, however, no producer that we know
17449 of make use of these. Handling bit sizes that are a multiple of the
17450 byte size is easy enough, but what about other bit sizes? Lets deal
17451 with that problem when we have to. Warn about these attributes being
17452 unsupported, then parse the type and ignore them like we always
17453 have. */
17454 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17455 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17456 {
17457 static bool warning_printed = false;
17458 if (!warning_printed)
17459 {
17460 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17461 "currently supported on DW_TAG_string_type."));
17462 warning_printed = true;
17463 }
17464 }
17465
17466 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17467 if (attr != nullptr && !attr->form_is_constant ())
17468 {
17469 /* The string length describes the location at which the length of
17470 the string can be found. The size of the length field can be
17471 specified with one of the attributes below. */
17472 struct type *prop_type;
17473 struct attribute *len
17474 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17475 if (len == nullptr)
17476 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17477 if (len != nullptr && len->form_is_constant ())
17478 {
17479 /* Pass 0 as the default as we know this attribute is constant
17480 and the default value will not be returned. */
17481 LONGEST sz = len->constant_value (0);
17482 prop_type = objfile_int_type (objfile, sz, true);
17483 }
17484 else
17485 {
17486 /* If the size is not specified then we assume it is the size of
17487 an address on this target. */
17488 prop_type = cu->addr_sized_int_type (true);
17489 }
17490
17491 /* Convert the attribute into a dynamic property. */
17492 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17493 length = 1;
17494 else
17495 length_is_constant = false;
17496 }
17497 else if (attr != nullptr)
17498 {
17499 /* This DW_AT_string_length just contains the length with no
17500 indirection. There's no need to create a dynamic property in this
17501 case. Pass 0 for the default value as we know it will not be
17502 returned in this case. */
17503 length = attr->constant_value (0);
17504 }
17505 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17506 {
17507 /* We don't currently support non-constant byte sizes for strings. */
17508 length = attr->constant_value (1);
17509 }
17510 else
17511 {
17512 /* Use 1 as a fallback length if we have nothing else. */
17513 length = 1;
17514 }
17515
17516 index_type = objfile_type (objfile)->builtin_int;
17517 if (length_is_constant)
17518 range_type = create_static_range_type (NULL, index_type, 1, length);
17519 else
17520 {
17521 struct dynamic_prop low_bound;
17522
17523 low_bound.set_const_val (1);
17524 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17525 }
17526 char_type = language_string_char_type (cu->language_defn, gdbarch);
17527 type = create_string_type (NULL, char_type, range_type);
17528
17529 return set_die_type (die, type, cu);
17530 }
17531
17532 /* Assuming that DIE corresponds to a function, returns nonzero
17533 if the function is prototyped. */
17534
17535 static int
17536 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17537 {
17538 struct attribute *attr;
17539
17540 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17541 if (attr && attr->as_boolean ())
17542 return 1;
17543
17544 /* The DWARF standard implies that the DW_AT_prototyped attribute
17545 is only meaningful for C, but the concept also extends to other
17546 languages that allow unprototyped functions (Eg: Objective C).
17547 For all other languages, assume that functions are always
17548 prototyped. */
17549 if (cu->per_cu->lang != language_c
17550 && cu->per_cu->lang != language_objc
17551 && cu->per_cu->lang != language_opencl)
17552 return 1;
17553
17554 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17555 prototyped and unprototyped functions; default to prototyped,
17556 since that is more common in modern code (and RealView warns
17557 about unprototyped functions). */
17558 if (producer_is_realview (cu->producer))
17559 return 1;
17560
17561 return 0;
17562 }
17563
17564 /* Handle DIES due to C code like:
17565
17566 struct foo
17567 {
17568 int (*funcp)(int a, long l);
17569 int b;
17570 };
17571
17572 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17573
17574 static struct type *
17575 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17576 {
17577 struct objfile *objfile = cu->per_objfile->objfile;
17578 struct type *type; /* Type that this function returns. */
17579 struct type *ftype; /* Function that returns above type. */
17580 struct attribute *attr;
17581
17582 type = die_type (die, cu);
17583
17584 /* The die_type call above may have already set the type for this DIE. */
17585 ftype = get_die_type (die, cu);
17586 if (ftype)
17587 return ftype;
17588
17589 ftype = lookup_function_type (type);
17590
17591 if (prototyped_function_p (die, cu))
17592 ftype->set_is_prototyped (true);
17593
17594 /* Store the calling convention in the type if it's available in
17595 the subroutine die. Otherwise set the calling convention to
17596 the default value DW_CC_normal. */
17597 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17598 if (attr != nullptr
17599 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17600 TYPE_CALLING_CONVENTION (ftype)
17601 = (enum dwarf_calling_convention) attr->constant_value (0);
17602 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17603 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17604 else
17605 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17606
17607 /* Record whether the function returns normally to its caller or not
17608 if the DWARF producer set that information. */
17609 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17610 if (attr && attr->as_boolean ())
17611 TYPE_NO_RETURN (ftype) = 1;
17612
17613 /* We need to add the subroutine type to the die immediately so
17614 we don't infinitely recurse when dealing with parameters
17615 declared as the same subroutine type. */
17616 set_die_type (die, ftype, cu);
17617
17618 if (die->child != NULL)
17619 {
17620 struct type *void_type = objfile_type (objfile)->builtin_void;
17621 struct die_info *child_die;
17622 int nparams, iparams;
17623
17624 /* Count the number of parameters.
17625 FIXME: GDB currently ignores vararg functions, but knows about
17626 vararg member functions. */
17627 nparams = 0;
17628 child_die = die->child;
17629 while (child_die && child_die->tag)
17630 {
17631 if (child_die->tag == DW_TAG_formal_parameter)
17632 nparams++;
17633 else if (child_die->tag == DW_TAG_unspecified_parameters)
17634 ftype->set_has_varargs (true);
17635
17636 child_die = child_die->sibling;
17637 }
17638
17639 /* Allocate storage for parameters and fill them in. */
17640 ftype->set_num_fields (nparams);
17641 ftype->set_fields
17642 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17643
17644 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17645 even if we error out during the parameters reading below. */
17646 for (iparams = 0; iparams < nparams; iparams++)
17647 ftype->field (iparams).set_type (void_type);
17648
17649 iparams = 0;
17650 child_die = die->child;
17651 while (child_die && child_die->tag)
17652 {
17653 if (child_die->tag == DW_TAG_formal_parameter)
17654 {
17655 struct type *arg_type;
17656
17657 /* DWARF version 2 has no clean way to discern C++
17658 static and non-static member functions. G++ helps
17659 GDB by marking the first parameter for non-static
17660 member functions (which is the this pointer) as
17661 artificial. We pass this information to
17662 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17663
17664 DWARF version 3 added DW_AT_object_pointer, which GCC
17665 4.5 does not yet generate. */
17666 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17667 if (attr != nullptr)
17668 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17669 else
17670 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17671 arg_type = die_type (child_die, cu);
17672
17673 /* RealView does not mark THIS as const, which the testsuite
17674 expects. GCC marks THIS as const in method definitions,
17675 but not in the class specifications (GCC PR 43053). */
17676 if (cu->per_cu->lang == language_cplus
17677 && !TYPE_CONST (arg_type)
17678 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17679 {
17680 int is_this = 0;
17681 struct dwarf2_cu *arg_cu = cu;
17682 const char *name = dwarf2_name (child_die, cu);
17683
17684 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17685 if (attr != nullptr)
17686 {
17687 /* If the compiler emits this, use it. */
17688 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17689 is_this = 1;
17690 }
17691 else if (name && strcmp (name, "this") == 0)
17692 /* Function definitions will have the argument names. */
17693 is_this = 1;
17694 else if (name == NULL && iparams == 0)
17695 /* Declarations may not have the names, so like
17696 elsewhere in GDB, assume an artificial first
17697 argument is "this". */
17698 is_this = 1;
17699
17700 if (is_this)
17701 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17702 arg_type, 0);
17703 }
17704
17705 ftype->field (iparams).set_type (arg_type);
17706 iparams++;
17707 }
17708 child_die = child_die->sibling;
17709 }
17710 }
17711
17712 return ftype;
17713 }
17714
17715 static struct type *
17716 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17717 {
17718 struct objfile *objfile = cu->per_objfile->objfile;
17719 const char *name = NULL;
17720 struct type *this_type, *target_type;
17721
17722 name = dwarf2_full_name (NULL, die, cu);
17723 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17724 this_type->set_target_is_stub (true);
17725 set_die_type (die, this_type, cu);
17726 target_type = die_type (die, cu);
17727 if (target_type != this_type)
17728 TYPE_TARGET_TYPE (this_type) = target_type;
17729 else
17730 {
17731 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17732 spec and cause infinite loops in GDB. */
17733 complaint (_("Self-referential DW_TAG_typedef "
17734 "- DIE at %s [in module %s]"),
17735 sect_offset_str (die->sect_off), objfile_name (objfile));
17736 TYPE_TARGET_TYPE (this_type) = NULL;
17737 }
17738 if (name == NULL)
17739 {
17740 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17741 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17742 Handle these by just returning the target type, rather than
17743 constructing an anonymous typedef type and trying to handle this
17744 elsewhere. */
17745 set_die_type (die, target_type, cu);
17746 return target_type;
17747 }
17748 return this_type;
17749 }
17750
17751 /* Helper for get_dwarf2_rational_constant that computes the value of
17752 a given gmp_mpz given an attribute. */
17753
17754 static void
17755 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17756 {
17757 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17758 location expression that pushes an implicit value. */
17759 if (attr->form == DW_FORM_exprloc)
17760 {
17761 dwarf_block *blk = attr->as_block ();
17762 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17763 {
17764 uint64_t len;
17765 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17766 blk->data + blk->size,
17767 &len);
17768 if (ptr - blk->data + len <= blk->size)
17769 {
17770 mpz_import (value->val, len,
17771 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17772 1, 0, 0, ptr);
17773 return;
17774 }
17775 }
17776
17777 /* On failure set it to 1. */
17778 *value = gdb_mpz (1);
17779 }
17780 else if (attr->form_is_block ())
17781 {
17782 dwarf_block *blk = attr->as_block ();
17783 mpz_import (value->val, blk->size,
17784 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17785 1, 0, 0, blk->data);
17786 }
17787 else
17788 *value = gdb_mpz (attr->constant_value (1));
17789 }
17790
17791 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17792 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17793
17794 If the numerator and/or numerator attribute is missing,
17795 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17796 untouched. */
17797
17798 static void
17799 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17800 gdb_mpz *numerator, gdb_mpz *denominator)
17801 {
17802 struct attribute *num_attr, *denom_attr;
17803
17804 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17805 if (num_attr == nullptr)
17806 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17807 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17808
17809 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17810 if (denom_attr == nullptr)
17811 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17812 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17813
17814 if (num_attr == nullptr || denom_attr == nullptr)
17815 return;
17816
17817 get_mpz (cu, numerator, num_attr);
17818 get_mpz (cu, denominator, denom_attr);
17819 }
17820
17821 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17822 rational constant, rather than a signed one.
17823
17824 If the rational constant has a negative value, a complaint
17825 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17826
17827 static void
17828 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17829 struct dwarf2_cu *cu,
17830 gdb_mpz *numerator,
17831 gdb_mpz *denominator)
17832 {
17833 gdb_mpz num (1);
17834 gdb_mpz denom (1);
17835
17836 get_dwarf2_rational_constant (die, cu, &num, &denom);
17837 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17838 {
17839 mpz_neg (num.val, num.val);
17840 mpz_neg (denom.val, denom.val);
17841 }
17842 else if (mpz_sgn (num.val) == -1)
17843 {
17844 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17845 " in DIE at %s"),
17846 sect_offset_str (die->sect_off));
17847 return;
17848 }
17849 else if (mpz_sgn (denom.val) == -1)
17850 {
17851 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17852 " in DIE at %s"),
17853 sect_offset_str (die->sect_off));
17854 return;
17855 }
17856
17857 *numerator = std::move (num);
17858 *denominator = std::move (denom);
17859 }
17860
17861 /* Assuming that ENCODING is a string whose contents starting at the
17862 K'th character is "_nn" where "nn" is a decimal number, scan that
17863 number and set RESULT to the value. K is updated to point to the
17864 character immediately following the number.
17865
17866 If the string does not conform to the format described above, false
17867 is returned, and K may or may not be changed. */
17868
17869 static bool
17870 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17871 {
17872 /* The next character should be an underscore ('_') followed
17873 by a digit. */
17874 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17875 return false;
17876
17877 /* Skip the underscore. */
17878 k++;
17879 int start = k;
17880
17881 /* Determine the number of digits for our number. */
17882 while (isdigit (encoding[k]))
17883 k++;
17884 if (k == start)
17885 return false;
17886
17887 std::string copy (&encoding[start], k - start);
17888 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17889 return false;
17890
17891 return true;
17892 }
17893
17894 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17895 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17896 DENOM, update OFFSET, and return true on success. Return false on
17897 failure. */
17898
17899 static bool
17900 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17901 gdb_mpz *num, gdb_mpz *denom)
17902 {
17903 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17904 return false;
17905 return ada_get_gnat_encoded_number (encoding, offset, denom);
17906 }
17907
17908 /* Assuming DIE corresponds to a fixed point type, finish the creation
17909 of the corresponding TYPE by setting its type-specific data. CU is
17910 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17911 encodings. It is nullptr if the GNAT encoding should be
17912 ignored. */
17913
17914 static void
17915 finish_fixed_point_type (struct type *type, const char *suffix,
17916 struct die_info *die, struct dwarf2_cu *cu)
17917 {
17918 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17919 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17920
17921 /* If GNAT encodings are preferred, don't examine the
17922 attributes. */
17923 struct attribute *attr = nullptr;
17924 if (suffix == nullptr)
17925 {
17926 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17927 if (attr == nullptr)
17928 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17929 if (attr == nullptr)
17930 attr = dwarf2_attr (die, DW_AT_small, cu);
17931 }
17932
17933 /* Numerator and denominator of our fixed-point type's scaling factor.
17934 The default is a scaling factor of 1, which we use as a fallback
17935 when we are not able to decode it (problem with the debugging info,
17936 unsupported forms, bug in GDB, etc...). Using that as the default
17937 allows us to at least print the unscaled value, which might still
17938 be useful to a user. */
17939 gdb_mpz scale_num (1);
17940 gdb_mpz scale_denom (1);
17941
17942 if (attr == nullptr)
17943 {
17944 int offset = 0;
17945 if (suffix != nullptr
17946 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17947 &scale_denom)
17948 /* The number might be encoded as _nn_dd_nn_dd, where the
17949 second ratio is the 'small value. In this situation, we
17950 want the second value. */
17951 && (suffix[offset] != '_'
17952 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17953 &scale_denom)))
17954 {
17955 /* Found it. */
17956 }
17957 else
17958 {
17959 /* Scaling factor not found. Assume a scaling factor of 1,
17960 and hope for the best. At least the user will be able to
17961 see the encoded value. */
17962 scale_num = 1;
17963 scale_denom = 1;
17964 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17965 sect_offset_str (die->sect_off));
17966 }
17967 }
17968 else if (attr->name == DW_AT_binary_scale)
17969 {
17970 LONGEST scale_exp = attr->constant_value (0);
17971 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17972
17973 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17974 }
17975 else if (attr->name == DW_AT_decimal_scale)
17976 {
17977 LONGEST scale_exp = attr->constant_value (0);
17978 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17979
17980 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17981 }
17982 else if (attr->name == DW_AT_small)
17983 {
17984 struct die_info *scale_die;
17985 struct dwarf2_cu *scale_cu = cu;
17986
17987 scale_die = follow_die_ref (die, attr, &scale_cu);
17988 if (scale_die->tag == DW_TAG_constant)
17989 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17990 &scale_num, &scale_denom);
17991 else
17992 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17993 " (DIE at %s)"),
17994 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17995 }
17996 else
17997 {
17998 complaint (_("unsupported scale attribute %s for fixed-point type"
17999 " (DIE at %s)"),
18000 dwarf_attr_name (attr->name),
18001 sect_offset_str (die->sect_off));
18002 }
18003
18004 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18005 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18006 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18007 mpq_canonicalize (scaling_factor.val);
18008 }
18009
18010 /* The gnat-encoding suffix for fixed point. */
18011
18012 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18013
18014 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18015 "XF" suffix of the name. The text after this is what encodes the
18016 'small and 'delta information. Otherwise, return nullptr. */
18017
18018 static const char *
18019 gnat_encoded_fixed_point_type_info (const char *name)
18020 {
18021 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18022 }
18023
18024 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18025 (which may be different from NAME) to the architecture back-end to allow
18026 it to guess the correct format if necessary. */
18027
18028 static struct type *
18029 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18030 const char *name_hint, enum bfd_endian byte_order)
18031 {
18032 struct gdbarch *gdbarch = objfile->arch ();
18033 const struct floatformat **format;
18034 struct type *type;
18035
18036 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18037 if (format)
18038 type = init_float_type (objfile, bits, name, format, byte_order);
18039 else
18040 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18041
18042 return type;
18043 }
18044
18045 /* Allocate an integer type of size BITS and name NAME. */
18046
18047 static struct type *
18048 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18049 int bits, int unsigned_p, const char *name)
18050 {
18051 struct type *type;
18052
18053 /* Versions of Intel's C Compiler generate an integer type called "void"
18054 instead of using DW_TAG_unspecified_type. This has been seen on
18055 at least versions 14, 17, and 18. */
18056 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18057 && strcmp (name, "void") == 0)
18058 type = objfile_type (objfile)->builtin_void;
18059 else
18060 type = init_integer_type (objfile, bits, unsigned_p, name);
18061
18062 return type;
18063 }
18064
18065 /* Return true if DIE has a DW_AT_small attribute whose value is
18066 a constant rational, where both the numerator and denominator
18067 are equal to zero.
18068
18069 CU is the DIE's Compilation Unit. */
18070
18071 static bool
18072 has_zero_over_zero_small_attribute (struct die_info *die,
18073 struct dwarf2_cu *cu)
18074 {
18075 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18076 if (attr == nullptr)
18077 return false;
18078
18079 struct dwarf2_cu *scale_cu = cu;
18080 struct die_info *scale_die
18081 = follow_die_ref (die, attr, &scale_cu);
18082
18083 if (scale_die->tag != DW_TAG_constant)
18084 return false;
18085
18086 gdb_mpz num (1), denom (1);
18087 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18088 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18089 }
18090
18091 /* Initialise and return a floating point type of size BITS suitable for
18092 use as a component of a complex number. The NAME_HINT is passed through
18093 when initialising the floating point type and is the name of the complex
18094 type.
18095
18096 As DWARF doesn't currently provide an explicit name for the components
18097 of a complex number, but it can be helpful to have these components
18098 named, we try to select a suitable name based on the size of the
18099 component. */
18100 static struct type *
18101 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18102 struct objfile *objfile,
18103 int bits, const char *name_hint,
18104 enum bfd_endian byte_order)
18105 {
18106 gdbarch *gdbarch = objfile->arch ();
18107 struct type *tt = nullptr;
18108
18109 /* Try to find a suitable floating point builtin type of size BITS.
18110 We're going to use the name of this type as the name for the complex
18111 target type that we are about to create. */
18112 switch (cu->per_cu->lang)
18113 {
18114 case language_fortran:
18115 switch (bits)
18116 {
18117 case 32:
18118 tt = builtin_f_type (gdbarch)->builtin_real;
18119 break;
18120 case 64:
18121 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18122 break;
18123 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18124 case 128:
18125 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18126 break;
18127 }
18128 break;
18129 default:
18130 switch (bits)
18131 {
18132 case 32:
18133 tt = builtin_type (gdbarch)->builtin_float;
18134 break;
18135 case 64:
18136 tt = builtin_type (gdbarch)->builtin_double;
18137 break;
18138 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18139 case 128:
18140 tt = builtin_type (gdbarch)->builtin_long_double;
18141 break;
18142 }
18143 break;
18144 }
18145
18146 /* If the type we found doesn't match the size we were looking for, then
18147 pretend we didn't find a type at all, the complex target type we
18148 create will then be nameless. */
18149 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18150 tt = nullptr;
18151
18152 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18153 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18154 }
18155
18156 /* Find a representation of a given base type and install
18157 it in the TYPE field of the die. */
18158
18159 static struct type *
18160 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18161 {
18162 struct objfile *objfile = cu->per_objfile->objfile;
18163 struct type *type;
18164 struct attribute *attr;
18165 int encoding = 0, bits = 0;
18166 const char *name;
18167 gdbarch *arch;
18168
18169 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18170 if (attr != nullptr && attr->form_is_constant ())
18171 encoding = attr->constant_value (0);
18172 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18173 if (attr != nullptr)
18174 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18175 name = dwarf2_name (die, cu);
18176 if (!name)
18177 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18178
18179 arch = objfile->arch ();
18180 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18181
18182 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18183 if (attr != nullptr && attr->form_is_constant ())
18184 {
18185 int endianity = attr->constant_value (0);
18186
18187 switch (endianity)
18188 {
18189 case DW_END_big:
18190 byte_order = BFD_ENDIAN_BIG;
18191 break;
18192 case DW_END_little:
18193 byte_order = BFD_ENDIAN_LITTLE;
18194 break;
18195 default:
18196 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18197 break;
18198 }
18199 }
18200
18201 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18202 && cu->per_cu->lang == language_ada
18203 && has_zero_over_zero_small_attribute (die, cu))
18204 {
18205 /* brobecker/2018-02-24: This is a fixed point type for which
18206 the scaling factor is represented as fraction whose value
18207 does not make sense (zero divided by zero), so we should
18208 normally never see these. However, there is a small category
18209 of fixed point types for which GNAT is unable to provide
18210 the scaling factor via the standard DWARF mechanisms, and
18211 for which the info is provided via the GNAT encodings instead.
18212 This is likely what this DIE is about. */
18213 encoding = (encoding == DW_ATE_signed_fixed
18214 ? DW_ATE_signed
18215 : DW_ATE_unsigned);
18216 }
18217
18218 /* With GNAT encodings, fixed-point information will be encoded in
18219 the type name. Note that this can also occur with the above
18220 zero-over-zero case, which is why this is a separate "if" rather
18221 than an "else if". */
18222 const char *gnat_encoding_suffix = nullptr;
18223 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18224 && cu->per_cu->lang == language_ada
18225 && name != nullptr)
18226 {
18227 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18228 if (gnat_encoding_suffix != nullptr)
18229 {
18230 gdb_assert (startswith (gnat_encoding_suffix,
18231 GNAT_FIXED_POINT_SUFFIX));
18232 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18233 name, gnat_encoding_suffix - name);
18234 /* Use -1 here so that SUFFIX points at the "_" after the
18235 "XF". */
18236 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18237
18238 encoding = (encoding == DW_ATE_signed
18239 ? DW_ATE_signed_fixed
18240 : DW_ATE_unsigned_fixed);
18241 }
18242 }
18243
18244 switch (encoding)
18245 {
18246 case DW_ATE_address:
18247 /* Turn DW_ATE_address into a void * pointer. */
18248 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18249 type = init_pointer_type (objfile, bits, name, type);
18250 break;
18251 case DW_ATE_boolean:
18252 type = init_boolean_type (objfile, bits, 1, name);
18253 break;
18254 case DW_ATE_complex_float:
18255 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18256 byte_order);
18257 if (type->code () == TYPE_CODE_ERROR)
18258 {
18259 if (name == nullptr)
18260 {
18261 struct obstack *obstack
18262 = &cu->per_objfile->objfile->objfile_obstack;
18263 name = obconcat (obstack, "_Complex ", type->name (),
18264 nullptr);
18265 }
18266 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18267 }
18268 else
18269 type = init_complex_type (name, type);
18270 break;
18271 case DW_ATE_decimal_float:
18272 type = init_decfloat_type (objfile, bits, name);
18273 break;
18274 case DW_ATE_float:
18275 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18276 break;
18277 case DW_ATE_signed:
18278 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18279 break;
18280 case DW_ATE_unsigned:
18281 if (cu->per_cu->lang == language_fortran
18282 && name
18283 && startswith (name, "character("))
18284 type = init_character_type (objfile, bits, 1, name);
18285 else
18286 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18287 break;
18288 case DW_ATE_signed_char:
18289 if (cu->per_cu->lang == language_ada
18290 || cu->per_cu->lang == language_m2
18291 || cu->per_cu->lang == language_pascal
18292 || cu->per_cu->lang == language_fortran)
18293 type = init_character_type (objfile, bits, 0, name);
18294 else
18295 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18296 break;
18297 case DW_ATE_unsigned_char:
18298 if (cu->per_cu->lang == language_ada
18299 || cu->per_cu->lang == language_m2
18300 || cu->per_cu->lang == language_pascal
18301 || cu->per_cu->lang == language_fortran
18302 || cu->per_cu->lang == language_rust)
18303 type = init_character_type (objfile, bits, 1, name);
18304 else
18305 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18306 break;
18307 case DW_ATE_UTF:
18308 {
18309 type = init_character_type (objfile, bits, 1, name);
18310 return set_die_type (die, type, cu);
18311 }
18312 break;
18313 case DW_ATE_signed_fixed:
18314 type = init_fixed_point_type (objfile, bits, 0, name);
18315 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18316 break;
18317 case DW_ATE_unsigned_fixed:
18318 type = init_fixed_point_type (objfile, bits, 1, name);
18319 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18320 break;
18321
18322 default:
18323 complaint (_("unsupported DW_AT_encoding: '%s'"),
18324 dwarf_type_encoding_name (encoding));
18325 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18326 break;
18327 }
18328
18329 if (type->code () == TYPE_CODE_INT
18330 && name != nullptr
18331 && strcmp (name, "char") == 0)
18332 type->set_has_no_signedness (true);
18333
18334 maybe_set_alignment (cu, die, type);
18335
18336 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18337
18338 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18339 {
18340 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18341 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18342 {
18343 unsigned real_bit_size = attr->as_unsigned ();
18344 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18345 /* Only use the attributes if they make sense together. */
18346 if (attr == nullptr
18347 || (attr->as_unsigned () + real_bit_size
18348 <= 8 * TYPE_LENGTH (type)))
18349 {
18350 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18351 = real_bit_size;
18352 if (attr != nullptr)
18353 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18354 = attr->as_unsigned ();
18355 }
18356 }
18357 }
18358
18359 return set_die_type (die, type, cu);
18360 }
18361
18362 /* A helper function that returns the name of DIE, if it refers to a
18363 variable declaration. */
18364
18365 static const char *
18366 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18367 {
18368 if (die->tag != DW_TAG_variable)
18369 return nullptr;
18370
18371 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18372 if (attr == nullptr || !attr->as_boolean ())
18373 return nullptr;
18374
18375 attr = dwarf2_attr (die, DW_AT_name, cu);
18376 if (attr == nullptr)
18377 return nullptr;
18378 return attr->as_string ();
18379 }
18380
18381 /* Parse dwarf attribute if it's a block, reference or constant and put the
18382 resulting value of the attribute into struct bound_prop.
18383 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18384
18385 static int
18386 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18387 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18388 struct type *default_type)
18389 {
18390 struct dwarf2_property_baton *baton;
18391 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18392 struct objfile *objfile = per_objfile->objfile;
18393 struct obstack *obstack = &objfile->objfile_obstack;
18394
18395 gdb_assert (default_type != NULL);
18396
18397 if (attr == NULL || prop == NULL)
18398 return 0;
18399
18400 if (attr->form_is_block ())
18401 {
18402 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18403 baton->property_type = default_type;
18404 baton->locexpr.per_cu = cu->per_cu;
18405 baton->locexpr.per_objfile = per_objfile;
18406
18407 struct dwarf_block *block;
18408 if (attr->form == DW_FORM_data16)
18409 {
18410 size_t data_size = 16;
18411 block = XOBNEW (obstack, struct dwarf_block);
18412 block->size = (data_size
18413 + 2 /* Extra bytes for DW_OP and arg. */);
18414 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18415 data[0] = DW_OP_implicit_value;
18416 data[1] = data_size;
18417 memcpy (&data[2], attr->as_block ()->data, data_size);
18418 block->data = data;
18419 }
18420 else
18421 block = attr->as_block ();
18422
18423 baton->locexpr.size = block->size;
18424 baton->locexpr.data = block->data;
18425 switch (attr->name)
18426 {
18427 case DW_AT_string_length:
18428 baton->locexpr.is_reference = true;
18429 break;
18430 default:
18431 baton->locexpr.is_reference = false;
18432 break;
18433 }
18434
18435 prop->set_locexpr (baton);
18436 gdb_assert (prop->baton () != NULL);
18437 }
18438 else if (attr->form_is_ref ())
18439 {
18440 struct dwarf2_cu *target_cu = cu;
18441 struct die_info *target_die;
18442 struct attribute *target_attr;
18443
18444 target_die = follow_die_ref (die, attr, &target_cu);
18445 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18446 if (target_attr == NULL)
18447 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18448 target_cu);
18449 if (target_attr == nullptr)
18450 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
18451 target_cu);
18452 if (target_attr == NULL)
18453 {
18454 const char *name = var_decl_name (target_die, target_cu);
18455 if (name != nullptr)
18456 {
18457 prop->set_variable_name (name);
18458 return 1;
18459 }
18460 return 0;
18461 }
18462
18463 switch (target_attr->name)
18464 {
18465 case DW_AT_location:
18466 if (target_attr->form_is_section_offset ())
18467 {
18468 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18469 baton->property_type = die_type (target_die, target_cu);
18470 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18471 prop->set_loclist (baton);
18472 gdb_assert (prop->baton () != NULL);
18473 }
18474 else if (target_attr->form_is_block ())
18475 {
18476 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18477 baton->property_type = die_type (target_die, target_cu);
18478 baton->locexpr.per_cu = cu->per_cu;
18479 baton->locexpr.per_objfile = per_objfile;
18480 struct dwarf_block *block = target_attr->as_block ();
18481 baton->locexpr.size = block->size;
18482 baton->locexpr.data = block->data;
18483 baton->locexpr.is_reference = true;
18484 prop->set_locexpr (baton);
18485 gdb_assert (prop->baton () != NULL);
18486 }
18487 else
18488 {
18489 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18490 "dynamic property");
18491 return 0;
18492 }
18493 break;
18494 case DW_AT_data_member_location:
18495 case DW_AT_data_bit_offset:
18496 {
18497 LONGEST offset;
18498
18499 if (!handle_member_location (target_die, target_cu, &offset))
18500 return 0;
18501
18502 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18503 baton->property_type = read_type_die (target_die->parent,
18504 target_cu);
18505 baton->offset_info.offset = offset;
18506 baton->offset_info.type = die_type (target_die, target_cu);
18507 prop->set_addr_offset (baton);
18508 break;
18509 }
18510 }
18511 }
18512 else if (attr->form_is_constant ())
18513 prop->set_const_val (attr->constant_value (0));
18514 else if (attr->form_is_section_offset ())
18515 {
18516 switch (attr->name)
18517 {
18518 case DW_AT_string_length:
18519 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18520 baton->property_type = default_type;
18521 fill_in_loclist_baton (cu, &baton->loclist, attr);
18522 prop->set_loclist (baton);
18523 gdb_assert (prop->baton () != NULL);
18524 break;
18525 default:
18526 goto invalid;
18527 }
18528 }
18529 else
18530 goto invalid;
18531
18532 return 1;
18533
18534 invalid:
18535 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18536 dwarf2_name (die, cu));
18537 return 0;
18538 }
18539
18540 /* See read.h. */
18541
18542 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18543 present (which is valid) then compute the default type based on the
18544 compilation units address size. */
18545
18546 static struct type *
18547 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18548 {
18549 struct type *index_type = die_type (die, cu);
18550
18551 /* Dwarf-2 specifications explicitly allows to create subrange types
18552 without specifying a base type.
18553 In that case, the base type must be set to the type of
18554 the lower bound, upper bound or count, in that order, if any of these
18555 three attributes references an object that has a type.
18556 If no base type is found, the Dwarf-2 specifications say that
18557 a signed integer type of size equal to the size of an address should
18558 be used.
18559 For the following C code: `extern char gdb_int [];'
18560 GCC produces an empty range DIE.
18561 FIXME: muller/2010-05-28: Possible references to object for low bound,
18562 high bound or count are not yet handled by this code. */
18563 if (index_type->code () == TYPE_CODE_VOID)
18564 index_type = cu->addr_sized_int_type (false);
18565
18566 return index_type;
18567 }
18568
18569 /* Read the given DW_AT_subrange DIE. */
18570
18571 static struct type *
18572 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18573 {
18574 struct type *base_type, *orig_base_type;
18575 struct type *range_type;
18576 struct attribute *attr;
18577 struct dynamic_prop low, high;
18578 int low_default_is_valid;
18579 int high_bound_is_count = 0;
18580 const char *name;
18581 ULONGEST negative_mask;
18582
18583 orig_base_type = read_subrange_index_type (die, cu);
18584
18585 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18586 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18587 creating the range type, but we use the result of check_typedef
18588 when examining properties of the type. */
18589 base_type = check_typedef (orig_base_type);
18590
18591 /* The die_type call above may have already set the type for this DIE. */
18592 range_type = get_die_type (die, cu);
18593 if (range_type)
18594 return range_type;
18595
18596 high.set_const_val (0);
18597
18598 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18599 omitting DW_AT_lower_bound. */
18600 switch (cu->per_cu->lang)
18601 {
18602 case language_c:
18603 case language_cplus:
18604 low.set_const_val (0);
18605 low_default_is_valid = 1;
18606 break;
18607 case language_fortran:
18608 low.set_const_val (1);
18609 low_default_is_valid = 1;
18610 break;
18611 case language_d:
18612 case language_objc:
18613 case language_rust:
18614 low.set_const_val (0);
18615 low_default_is_valid = (cu->header.version >= 4);
18616 break;
18617 case language_ada:
18618 case language_m2:
18619 case language_pascal:
18620 low.set_const_val (1);
18621 low_default_is_valid = (cu->header.version >= 4);
18622 break;
18623 default:
18624 low.set_const_val (0);
18625 low_default_is_valid = 0;
18626 break;
18627 }
18628
18629 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18630 if (attr != nullptr)
18631 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18632 else if (!low_default_is_valid)
18633 complaint (_("Missing DW_AT_lower_bound "
18634 "- DIE at %s [in module %s]"),
18635 sect_offset_str (die->sect_off),
18636 objfile_name (cu->per_objfile->objfile));
18637
18638 struct attribute *attr_ub, *attr_count;
18639 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18640 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18641 {
18642 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18643 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18644 {
18645 /* If bounds are constant do the final calculation here. */
18646 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18647 high.set_const_val (low.const_val () + high.const_val () - 1);
18648 else
18649 high_bound_is_count = 1;
18650 }
18651 else
18652 {
18653 if (attr_ub != NULL)
18654 complaint (_("Unresolved DW_AT_upper_bound "
18655 "- DIE at %s [in module %s]"),
18656 sect_offset_str (die->sect_off),
18657 objfile_name (cu->per_objfile->objfile));
18658 if (attr_count != NULL)
18659 complaint (_("Unresolved DW_AT_count "
18660 "- DIE at %s [in module %s]"),
18661 sect_offset_str (die->sect_off),
18662 objfile_name (cu->per_objfile->objfile));
18663 }
18664 }
18665
18666 LONGEST bias = 0;
18667 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18668 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18669 bias = bias_attr->constant_value (0);
18670
18671 /* Normally, the DWARF producers are expected to use a signed
18672 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18673 But this is unfortunately not always the case, as witnessed
18674 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18675 is used instead. To work around that ambiguity, we treat
18676 the bounds as signed, and thus sign-extend their values, when
18677 the base type is signed. */
18678 negative_mask =
18679 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18680 if (low.kind () == PROP_CONST
18681 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18682 low.set_const_val (low.const_val () | negative_mask);
18683 if (high.kind () == PROP_CONST
18684 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18685 high.set_const_val (high.const_val () | negative_mask);
18686
18687 /* Check for bit and byte strides. */
18688 struct dynamic_prop byte_stride_prop;
18689 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18690 if (attr_byte_stride != nullptr)
18691 {
18692 struct type *prop_type = cu->addr_sized_int_type (false);
18693 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18694 prop_type);
18695 }
18696
18697 struct dynamic_prop bit_stride_prop;
18698 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18699 if (attr_bit_stride != nullptr)
18700 {
18701 /* It only makes sense to have either a bit or byte stride. */
18702 if (attr_byte_stride != nullptr)
18703 {
18704 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18705 "- DIE at %s [in module %s]"),
18706 sect_offset_str (die->sect_off),
18707 objfile_name (cu->per_objfile->objfile));
18708 attr_bit_stride = nullptr;
18709 }
18710 else
18711 {
18712 struct type *prop_type = cu->addr_sized_int_type (false);
18713 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18714 prop_type);
18715 }
18716 }
18717
18718 if (attr_byte_stride != nullptr
18719 || attr_bit_stride != nullptr)
18720 {
18721 bool byte_stride_p = (attr_byte_stride != nullptr);
18722 struct dynamic_prop *stride
18723 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18724
18725 range_type
18726 = create_range_type_with_stride (NULL, orig_base_type, &low,
18727 &high, bias, stride, byte_stride_p);
18728 }
18729 else
18730 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18731
18732 if (high_bound_is_count)
18733 range_type->bounds ()->flag_upper_bound_is_count = 1;
18734
18735 /* Ada expects an empty array on no boundary attributes. */
18736 if (attr == NULL && cu->per_cu->lang != language_ada)
18737 range_type->bounds ()->high.set_undefined ();
18738
18739 name = dwarf2_name (die, cu);
18740 if (name)
18741 range_type->set_name (name);
18742
18743 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18744 if (attr != nullptr)
18745 TYPE_LENGTH (range_type) = attr->constant_value (0);
18746
18747 maybe_set_alignment (cu, die, range_type);
18748
18749 set_die_type (die, range_type, cu);
18750
18751 /* set_die_type should be already done. */
18752 set_descriptive_type (range_type, die, cu);
18753
18754 return range_type;
18755 }
18756
18757 static struct type *
18758 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18759 {
18760 struct type *type;
18761
18762 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18763 type->set_name (dwarf2_name (die, cu));
18764
18765 /* In Ada, an unspecified type is typically used when the description
18766 of the type is deferred to a different unit. When encountering
18767 such a type, we treat it as a stub, and try to resolve it later on,
18768 when needed. */
18769 if (cu->per_cu->lang == language_ada)
18770 type->set_is_stub (true);
18771
18772 return set_die_type (die, type, cu);
18773 }
18774
18775 /* Read a single die and all its descendents. Set the die's sibling
18776 field to NULL; set other fields in the die correctly, and set all
18777 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18778 location of the info_ptr after reading all of those dies. PARENT
18779 is the parent of the die in question. */
18780
18781 static struct die_info *
18782 read_die_and_children (const struct die_reader_specs *reader,
18783 const gdb_byte *info_ptr,
18784 const gdb_byte **new_info_ptr,
18785 struct die_info *parent)
18786 {
18787 struct die_info *die;
18788 const gdb_byte *cur_ptr;
18789
18790 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18791 if (die == NULL)
18792 {
18793 *new_info_ptr = cur_ptr;
18794 return NULL;
18795 }
18796 store_in_ref_table (die, reader->cu);
18797
18798 if (die->has_children)
18799 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18800 else
18801 {
18802 die->child = NULL;
18803 *new_info_ptr = cur_ptr;
18804 }
18805
18806 die->sibling = NULL;
18807 die->parent = parent;
18808 return die;
18809 }
18810
18811 /* Read a die, all of its descendents, and all of its siblings; set
18812 all of the fields of all of the dies correctly. Arguments are as
18813 in read_die_and_children. */
18814
18815 static struct die_info *
18816 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18817 const gdb_byte *info_ptr,
18818 const gdb_byte **new_info_ptr,
18819 struct die_info *parent)
18820 {
18821 struct die_info *first_die, *last_sibling;
18822 const gdb_byte *cur_ptr;
18823
18824 cur_ptr = info_ptr;
18825 first_die = last_sibling = NULL;
18826
18827 while (1)
18828 {
18829 struct die_info *die
18830 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18831
18832 if (die == NULL)
18833 {
18834 *new_info_ptr = cur_ptr;
18835 return first_die;
18836 }
18837
18838 if (!first_die)
18839 first_die = die;
18840 else
18841 last_sibling->sibling = die;
18842
18843 last_sibling = die;
18844 }
18845 }
18846
18847 /* Read a die, all of its descendents, and all of its siblings; set
18848 all of the fields of all of the dies correctly. Arguments are as
18849 in read_die_and_children.
18850 This the main entry point for reading a DIE and all its children. */
18851
18852 static struct die_info *
18853 read_die_and_siblings (const struct die_reader_specs *reader,
18854 const gdb_byte *info_ptr,
18855 const gdb_byte **new_info_ptr,
18856 struct die_info *parent)
18857 {
18858 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18859 new_info_ptr, parent);
18860
18861 if (dwarf_die_debug)
18862 {
18863 gdb_printf (gdb_stdlog,
18864 "Read die from %s@0x%x of %s:\n",
18865 reader->die_section->get_name (),
18866 (unsigned) (info_ptr - reader->die_section->buffer),
18867 bfd_get_filename (reader->abfd));
18868 dump_die (die, dwarf_die_debug);
18869 }
18870
18871 return die;
18872 }
18873
18874 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18875 attributes.
18876 The caller is responsible for filling in the extra attributes
18877 and updating (*DIEP)->num_attrs.
18878 Set DIEP to point to a newly allocated die with its information,
18879 except for its child, sibling, and parent fields. */
18880
18881 static const gdb_byte *
18882 read_full_die_1 (const struct die_reader_specs *reader,
18883 struct die_info **diep, const gdb_byte *info_ptr,
18884 int num_extra_attrs)
18885 {
18886 unsigned int abbrev_number, bytes_read, i;
18887 const struct abbrev_info *abbrev;
18888 struct die_info *die;
18889 struct dwarf2_cu *cu = reader->cu;
18890 bfd *abfd = reader->abfd;
18891
18892 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18893 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18894 info_ptr += bytes_read;
18895 if (!abbrev_number)
18896 {
18897 *diep = NULL;
18898 return info_ptr;
18899 }
18900
18901 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18902 if (!abbrev)
18903 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18904 abbrev_number,
18905 bfd_get_filename (abfd));
18906
18907 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18908 die->sect_off = sect_off;
18909 die->tag = abbrev->tag;
18910 die->abbrev = abbrev_number;
18911 die->has_children = abbrev->has_children;
18912
18913 /* Make the result usable.
18914 The caller needs to update num_attrs after adding the extra
18915 attributes. */
18916 die->num_attrs = abbrev->num_attrs;
18917
18918 bool any_need_reprocess = false;
18919 for (i = 0; i < abbrev->num_attrs; ++i)
18920 {
18921 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18922 info_ptr);
18923 if (die->attrs[i].requires_reprocessing_p ())
18924 any_need_reprocess = true;
18925 }
18926
18927 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18928 if (attr != nullptr && attr->form_is_unsigned ())
18929 cu->str_offsets_base = attr->as_unsigned ();
18930
18931 attr = die->attr (DW_AT_loclists_base);
18932 if (attr != nullptr)
18933 cu->loclist_base = attr->as_unsigned ();
18934
18935 auto maybe_addr_base = die->addr_base ();
18936 if (maybe_addr_base.has_value ())
18937 cu->addr_base = *maybe_addr_base;
18938
18939 attr = die->attr (DW_AT_rnglists_base);
18940 if (attr != nullptr)
18941 cu->rnglists_base = attr->as_unsigned ();
18942
18943 if (any_need_reprocess)
18944 {
18945 for (i = 0; i < abbrev->num_attrs; ++i)
18946 {
18947 if (die->attrs[i].requires_reprocessing_p ())
18948 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18949 }
18950 }
18951 *diep = die;
18952 return info_ptr;
18953 }
18954
18955 /* Read a die and all its attributes.
18956 Set DIEP to point to a newly allocated die with its information,
18957 except for its child, sibling, and parent fields. */
18958
18959 static const gdb_byte *
18960 read_full_die (const struct die_reader_specs *reader,
18961 struct die_info **diep, const gdb_byte *info_ptr)
18962 {
18963 const gdb_byte *result;
18964
18965 result = read_full_die_1 (reader, diep, info_ptr, 0);
18966
18967 if (dwarf_die_debug)
18968 {
18969 gdb_printf (gdb_stdlog,
18970 "Read die from %s@0x%x of %s:\n",
18971 reader->die_section->get_name (),
18972 (unsigned) (info_ptr - reader->die_section->buffer),
18973 bfd_get_filename (reader->abfd));
18974 dump_die (*diep, dwarf_die_debug);
18975 }
18976
18977 return result;
18978 }
18979 \f
18980
18981 /* Returns nonzero if TAG represents a type that we might generate a partial
18982 symbol for. */
18983
18984 static int
18985 is_type_tag_for_partial (int tag, enum language lang)
18986 {
18987 switch (tag)
18988 {
18989 #if 0
18990 /* Some types that would be reasonable to generate partial symbols for,
18991 that we don't at present. Note that normally this does not
18992 matter, mainly because C compilers don't give names to these
18993 types, but instead emit DW_TAG_typedef. */
18994 case DW_TAG_file_type:
18995 case DW_TAG_ptr_to_member_type:
18996 case DW_TAG_set_type:
18997 case DW_TAG_string_type:
18998 case DW_TAG_subroutine_type:
18999 #endif
19000
19001 /* GNAT may emit an array with a name, but no typedef, so we
19002 need to make a symbol in this case. */
19003 case DW_TAG_array_type:
19004 return lang == language_ada;
19005
19006 case DW_TAG_base_type:
19007 case DW_TAG_class_type:
19008 case DW_TAG_interface_type:
19009 case DW_TAG_enumeration_type:
19010 case DW_TAG_structure_type:
19011 case DW_TAG_subrange_type:
19012 case DW_TAG_typedef:
19013 case DW_TAG_union_type:
19014 return 1;
19015 default:
19016 return 0;
19017 }
19018 }
19019
19020 /* Load all DIEs that are interesting for partial symbols into memory. */
19021
19022 static struct partial_die_info *
19023 load_partial_dies (const struct die_reader_specs *reader,
19024 const gdb_byte *info_ptr, int building_psymtab)
19025 {
19026 struct dwarf2_cu *cu = reader->cu;
19027 struct objfile *objfile = cu->per_objfile->objfile;
19028 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19029 unsigned int bytes_read;
19030 unsigned int load_all = 0;
19031 int nesting_level = 1;
19032
19033 parent_die = NULL;
19034 last_die = NULL;
19035
19036 gdb_assert (cu->per_cu != NULL);
19037 if (cu->load_all_dies)
19038 load_all = 1;
19039
19040 cu->partial_dies
19041 = htab_create_alloc_ex (cu->header.length / 12,
19042 partial_die_hash,
19043 partial_die_eq,
19044 NULL,
19045 &cu->comp_unit_obstack,
19046 hashtab_obstack_allocate,
19047 dummy_obstack_deallocate);
19048
19049 while (1)
19050 {
19051 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19052 &bytes_read);
19053
19054 /* A NULL abbrev means the end of a series of children. */
19055 if (abbrev == NULL)
19056 {
19057 if (--nesting_level == 0)
19058 return first_die;
19059
19060 info_ptr += bytes_read;
19061 last_die = parent_die;
19062 parent_die = parent_die->die_parent;
19063 continue;
19064 }
19065
19066 /* Check for template arguments. We never save these; if
19067 they're seen, we just mark the parent, and go on our way. */
19068 if (parent_die != NULL
19069 && cu->per_cu->lang == language_cplus
19070 && (abbrev->tag == DW_TAG_template_type_param
19071 || abbrev->tag == DW_TAG_template_value_param))
19072 {
19073 parent_die->has_template_arguments = 1;
19074
19075 if (!load_all)
19076 {
19077 /* We don't need a partial DIE for the template argument. */
19078 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19079 continue;
19080 }
19081 }
19082
19083 /* We only recurse into c++ subprograms looking for template arguments.
19084 Skip their other children. */
19085 if (!load_all
19086 && cu->per_cu->lang == language_cplus
19087 && parent_die != NULL
19088 && parent_die->tag == DW_TAG_subprogram
19089 && abbrev->tag != DW_TAG_inlined_subroutine)
19090 {
19091 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19092 continue;
19093 }
19094
19095 /* Check whether this DIE is interesting enough to save. Normally
19096 we would not be interested in members here, but there may be
19097 later variables referencing them via DW_AT_specification (for
19098 static members). */
19099 if (!load_all
19100 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
19101 && abbrev->tag != DW_TAG_constant
19102 && abbrev->tag != DW_TAG_enumerator
19103 && abbrev->tag != DW_TAG_subprogram
19104 && abbrev->tag != DW_TAG_inlined_subroutine
19105 && abbrev->tag != DW_TAG_lexical_block
19106 && abbrev->tag != DW_TAG_variable
19107 && abbrev->tag != DW_TAG_namespace
19108 && abbrev->tag != DW_TAG_module
19109 && abbrev->tag != DW_TAG_member
19110 && abbrev->tag != DW_TAG_imported_unit
19111 && abbrev->tag != DW_TAG_imported_declaration)
19112 {
19113 /* Otherwise we skip to the next sibling, if any. */
19114 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19115 continue;
19116 }
19117
19118 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19119 abbrev);
19120
19121 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19122
19123 /* This two-pass algorithm for processing partial symbols has a
19124 high cost in cache pressure. Thus, handle some simple cases
19125 here which cover the majority of C partial symbols. DIEs
19126 which neither have specification tags in them, nor could have
19127 specification tags elsewhere pointing at them, can simply be
19128 processed and discarded.
19129
19130 This segment is also optional; scan_partial_symbols and
19131 add_partial_symbol will handle these DIEs if we chain
19132 them in normally. When compilers which do not emit large
19133 quantities of duplicate debug information are more common,
19134 this code can probably be removed. */
19135
19136 /* Any complete simple types at the top level (pretty much all
19137 of them, for a language without namespaces), can be processed
19138 directly. */
19139 if (parent_die == NULL
19140 && pdi.has_specification == 0
19141 && pdi.is_declaration == 0
19142 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19143 || pdi.tag == DW_TAG_base_type
19144 || pdi.tag == DW_TAG_array_type
19145 || pdi.tag == DW_TAG_subrange_type))
19146 {
19147 if (building_psymtab && pdi.raw_name != NULL)
19148 add_partial_symbol (&pdi, cu);
19149
19150 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19151 continue;
19152 }
19153
19154 /* The exception for DW_TAG_typedef with has_children above is
19155 a workaround of GCC PR debug/47510. In the case of this complaint
19156 type_name_or_error will error on such types later.
19157
19158 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19159 it could not find the child DIEs referenced later, this is checked
19160 above. In correct DWARF DW_TAG_typedef should have no children. */
19161
19162 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19163 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19164 "- DIE at %s [in module %s]"),
19165 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19166
19167 /* If we're at the second level, and we're an enumerator, and
19168 our parent has no specification (meaning possibly lives in a
19169 namespace elsewhere), then we can add the partial symbol now
19170 instead of queueing it. */
19171 if (pdi.tag == DW_TAG_enumerator
19172 && parent_die != NULL
19173 && parent_die->die_parent == NULL
19174 && parent_die->tag == DW_TAG_enumeration_type
19175 && parent_die->has_specification == 0)
19176 {
19177 if (pdi.raw_name == NULL)
19178 complaint (_("malformed enumerator DIE ignored"));
19179 else if (building_psymtab)
19180 add_partial_symbol (&pdi, cu);
19181
19182 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19183 continue;
19184 }
19185
19186 struct partial_die_info *part_die
19187 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19188
19189 /* We'll save this DIE so link it in. */
19190 part_die->die_parent = parent_die;
19191 part_die->die_sibling = NULL;
19192 part_die->die_child = NULL;
19193
19194 if (last_die && last_die == parent_die)
19195 last_die->die_child = part_die;
19196 else if (last_die)
19197 last_die->die_sibling = part_die;
19198
19199 last_die = part_die;
19200
19201 if (first_die == NULL)
19202 first_die = part_die;
19203
19204 /* Maybe add the DIE to the hash table. Not all DIEs that we
19205 find interesting need to be in the hash table, because we
19206 also have the parent/sibling/child chains; only those that we
19207 might refer to by offset later during partial symbol reading.
19208
19209 For now this means things that might have be the target of a
19210 DW_AT_specification, DW_AT_abstract_origin, or
19211 DW_AT_extension. DW_AT_extension will refer only to
19212 namespaces; DW_AT_abstract_origin refers to functions (and
19213 many things under the function DIE, but we do not recurse
19214 into function DIEs during partial symbol reading) and
19215 possibly variables as well; DW_AT_specification refers to
19216 declarations. Declarations ought to have the DW_AT_declaration
19217 flag. It happens that GCC forgets to put it in sometimes, but
19218 only for functions, not for types.
19219
19220 Adding more things than necessary to the hash table is harmless
19221 except for the performance cost. Adding too few will result in
19222 wasted time in find_partial_die, when we reread the compilation
19223 unit with load_all_dies set. */
19224
19225 if (load_all
19226 || abbrev->tag == DW_TAG_constant
19227 || abbrev->tag == DW_TAG_subprogram
19228 || abbrev->tag == DW_TAG_variable
19229 || abbrev->tag == DW_TAG_namespace
19230 || part_die->is_declaration)
19231 {
19232 void **slot;
19233
19234 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19235 to_underlying (part_die->sect_off),
19236 INSERT);
19237 *slot = part_die;
19238 }
19239
19240 /* For some DIEs we want to follow their children (if any). For C
19241 we have no reason to follow the children of structures; for other
19242 languages we have to, so that we can get at method physnames
19243 to infer fully qualified class names, for DW_AT_specification,
19244 and for C++ template arguments. For C++, we also look one level
19245 inside functions to find template arguments (if the name of the
19246 function does not already contain the template arguments).
19247
19248 For Ada and Fortran, we need to scan the children of subprograms
19249 and lexical blocks as well because these languages allow the
19250 definition of nested entities that could be interesting for the
19251 debugger, such as nested subprograms for instance. */
19252 if (last_die->has_children
19253 && (load_all
19254 || last_die->tag == DW_TAG_namespace
19255 || last_die->tag == DW_TAG_module
19256 || last_die->tag == DW_TAG_enumeration_type
19257 || (cu->per_cu->lang == language_cplus
19258 && last_die->tag == DW_TAG_subprogram
19259 && (last_die->raw_name == NULL
19260 || strchr (last_die->raw_name, '<') == NULL))
19261 || (cu->per_cu->lang != language_c
19262 && (last_die->tag == DW_TAG_class_type
19263 || last_die->tag == DW_TAG_interface_type
19264 || last_die->tag == DW_TAG_structure_type
19265 || last_die->tag == DW_TAG_union_type))
19266 || ((cu->per_cu->lang == language_ada
19267 || cu->per_cu->lang == language_fortran)
19268 && (last_die->tag == DW_TAG_subprogram
19269 || last_die->tag == DW_TAG_lexical_block))))
19270 {
19271 nesting_level++;
19272 parent_die = last_die;
19273 continue;
19274 }
19275
19276 /* Otherwise we skip to the next sibling, if any. */
19277 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19278
19279 /* Back to the top, do it again. */
19280 }
19281 }
19282
19283 partial_die_info::partial_die_info (sect_offset sect_off_,
19284 const struct abbrev_info *abbrev)
19285 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19286 {
19287 }
19288
19289 /* See class definition. */
19290
19291 const char *
19292 partial_die_info::name (dwarf2_cu *cu)
19293 {
19294 if (!canonical_name && raw_name != nullptr)
19295 {
19296 struct objfile *objfile = cu->per_objfile->objfile;
19297 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19298 canonical_name = 1;
19299 }
19300
19301 return raw_name;
19302 }
19303
19304 /* Read a minimal amount of information into the minimal die structure.
19305 INFO_PTR should point just after the initial uleb128 of a DIE. */
19306
19307 const gdb_byte *
19308 partial_die_info::read (const struct die_reader_specs *reader,
19309 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19310 {
19311 struct dwarf2_cu *cu = reader->cu;
19312 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19313 unsigned int i;
19314 int has_low_pc_attr = 0;
19315 int has_high_pc_attr = 0;
19316 int high_pc_relative = 0;
19317
19318 for (i = 0; i < abbrev.num_attrs; ++i)
19319 {
19320 attribute attr;
19321 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19322 /* String and address offsets that need to do the reprocessing have
19323 already been read at this point, so there is no need to wait until
19324 the loop terminates to do the reprocessing. */
19325 if (attr.requires_reprocessing_p ())
19326 read_attribute_reprocess (reader, &attr, tag);
19327 /* Store the data if it is of an attribute we want to keep in a
19328 partial symbol table. */
19329 switch (attr.name)
19330 {
19331 case DW_AT_name:
19332 switch (tag)
19333 {
19334 case DW_TAG_compile_unit:
19335 case DW_TAG_partial_unit:
19336 case DW_TAG_type_unit:
19337 /* Compilation units have a DW_AT_name that is a filename, not
19338 a source language identifier. */
19339 case DW_TAG_enumeration_type:
19340 case DW_TAG_enumerator:
19341 /* These tags always have simple identifiers already; no need
19342 to canonicalize them. */
19343 canonical_name = 1;
19344 raw_name = attr.as_string ();
19345 break;
19346 default:
19347 canonical_name = 0;
19348 raw_name = attr.as_string ();
19349 break;
19350 }
19351 break;
19352 case DW_AT_linkage_name:
19353 case DW_AT_MIPS_linkage_name:
19354 /* Note that both forms of linkage name might appear. We
19355 assume they will be the same, and we only store the last
19356 one we see. */
19357 linkage_name = attr.as_string ();
19358 break;
19359 case DW_AT_low_pc:
19360 has_low_pc_attr = 1;
19361 lowpc = attr.as_address ();
19362 break;
19363 case DW_AT_high_pc:
19364 has_high_pc_attr = 1;
19365 highpc = attr.as_address ();
19366 if (cu->header.version >= 4 && attr.form_is_constant ())
19367 high_pc_relative = 1;
19368 break;
19369 case DW_AT_location:
19370 /* Support the .debug_loc offsets. */
19371 if (attr.form_is_block ())
19372 {
19373 d.locdesc = attr.as_block ();
19374 }
19375 else if (attr.form_is_section_offset ())
19376 {
19377 dwarf2_complex_location_expr_complaint ();
19378 }
19379 else
19380 {
19381 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19382 "partial symbol information");
19383 }
19384 break;
19385 case DW_AT_external:
19386 is_external = attr.as_boolean ();
19387 break;
19388 case DW_AT_declaration:
19389 is_declaration = attr.as_boolean ();
19390 break;
19391 case DW_AT_type:
19392 has_type = 1;
19393 break;
19394 case DW_AT_abstract_origin:
19395 case DW_AT_specification:
19396 case DW_AT_extension:
19397 has_specification = 1;
19398 spec_offset = attr.get_ref_die_offset ();
19399 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19400 || cu->per_cu->is_dwz);
19401 break;
19402 case DW_AT_sibling:
19403 /* Ignore absolute siblings, they might point outside of
19404 the current compile unit. */
19405 if (attr.form == DW_FORM_ref_addr)
19406 complaint (_("ignoring absolute DW_AT_sibling"));
19407 else
19408 {
19409 const gdb_byte *buffer = reader->buffer;
19410 sect_offset off = attr.get_ref_die_offset ();
19411 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19412
19413 if (sibling_ptr < info_ptr)
19414 complaint (_("DW_AT_sibling points backwards"));
19415 else if (sibling_ptr > reader->buffer_end)
19416 reader->die_section->overflow_complaint ();
19417 else
19418 sibling = sibling_ptr;
19419 }
19420 break;
19421 case DW_AT_byte_size:
19422 has_byte_size = 1;
19423 break;
19424 case DW_AT_const_value:
19425 has_const_value = 1;
19426 break;
19427 case DW_AT_calling_convention:
19428 /* DWARF doesn't provide a way to identify a program's source-level
19429 entry point. DW_AT_calling_convention attributes are only meant
19430 to describe functions' calling conventions.
19431
19432 However, because it's a necessary piece of information in
19433 Fortran, and before DWARF 4 DW_CC_program was the only
19434 piece of debugging information whose definition refers to
19435 a 'main program' at all, several compilers marked Fortran
19436 main programs with DW_CC_program --- even when those
19437 functions use the standard calling conventions.
19438
19439 Although DWARF now specifies a way to provide this
19440 information, we support this practice for backward
19441 compatibility. */
19442 if (attr.constant_value (0) == DW_CC_program
19443 && cu->per_cu->lang == language_fortran)
19444 main_subprogram = 1;
19445 break;
19446 case DW_AT_inline:
19447 {
19448 LONGEST value = attr.constant_value (-1);
19449 if (value == DW_INL_inlined
19450 || value == DW_INL_declared_inlined)
19451 may_be_inlined = 1;
19452 }
19453 break;
19454
19455 case DW_AT_import:
19456 if (tag == DW_TAG_imported_unit)
19457 {
19458 d.sect_off = attr.get_ref_die_offset ();
19459 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19460 || cu->per_cu->is_dwz);
19461 }
19462 break;
19463
19464 case DW_AT_main_subprogram:
19465 main_subprogram = attr.as_boolean ();
19466 break;
19467
19468 case DW_AT_ranges:
19469 {
19470 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19471 on DWARF version). */
19472 ranges_offset = attr.as_unsigned ();
19473
19474 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19475 this value. */
19476 if (tag != DW_TAG_compile_unit)
19477 ranges_offset += cu->gnu_ranges_base;
19478
19479 has_range_info = 1;
19480 }
19481 break;
19482
19483 default:
19484 break;
19485 }
19486 }
19487
19488 /* For Ada, if both the name and the linkage name appear, we prefer
19489 the latter. This lets "catch exception" work better, regardless
19490 of the order in which the name and linkage name were emitted.
19491 Really, though, this is just a workaround for the fact that gdb
19492 doesn't store both the name and the linkage name. */
19493 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
19494 raw_name = linkage_name;
19495
19496 if (high_pc_relative)
19497 highpc += lowpc;
19498
19499 if (has_low_pc_attr && has_high_pc_attr)
19500 {
19501 /* When using the GNU linker, .gnu.linkonce. sections are used to
19502 eliminate duplicate copies of functions and vtables and such.
19503 The linker will arbitrarily choose one and discard the others.
19504 The AT_*_pc values for such functions refer to local labels in
19505 these sections. If the section from that file was discarded, the
19506 labels are not in the output, so the relocs get a value of 0.
19507 If this is a discarded function, mark the pc bounds as invalid,
19508 so that GDB will ignore it. */
19509 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19510 {
19511 struct objfile *objfile = per_objfile->objfile;
19512 struct gdbarch *gdbarch = objfile->arch ();
19513
19514 complaint (_("DW_AT_low_pc %s is zero "
19515 "for DIE at %s [in module %s]"),
19516 paddress (gdbarch, lowpc),
19517 sect_offset_str (sect_off),
19518 objfile_name (objfile));
19519 }
19520 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19521 else if (lowpc >= highpc)
19522 {
19523 struct objfile *objfile = per_objfile->objfile;
19524 struct gdbarch *gdbarch = objfile->arch ();
19525
19526 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19527 "for DIE at %s [in module %s]"),
19528 paddress (gdbarch, lowpc),
19529 paddress (gdbarch, highpc),
19530 sect_offset_str (sect_off),
19531 objfile_name (objfile));
19532 }
19533 else
19534 has_pc_info = 1;
19535 }
19536
19537 return info_ptr;
19538 }
19539
19540 /* Find a cached partial DIE at OFFSET in CU. */
19541
19542 struct partial_die_info *
19543 dwarf2_cu::find_partial_die (sect_offset sect_off)
19544 {
19545 struct partial_die_info *lookup_die = NULL;
19546 struct partial_die_info part_die (sect_off);
19547
19548 lookup_die = ((struct partial_die_info *)
19549 htab_find_with_hash (partial_dies, &part_die,
19550 to_underlying (sect_off)));
19551
19552 return lookup_die;
19553 }
19554
19555 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19556 except in the case of .debug_types DIEs which do not reference
19557 outside their CU (they do however referencing other types via
19558 DW_FORM_ref_sig8). */
19559
19560 static const struct cu_partial_die_info
19561 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19562 {
19563 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19564 struct objfile *objfile = per_objfile->objfile;
19565 struct partial_die_info *pd = NULL;
19566
19567 if (offset_in_dwz == cu->per_cu->is_dwz
19568 && cu->header.offset_in_cu_p (sect_off))
19569 {
19570 pd = cu->find_partial_die (sect_off);
19571 if (pd != NULL)
19572 return { cu, pd };
19573 /* We missed recording what we needed.
19574 Load all dies and try again. */
19575 }
19576 else
19577 {
19578 /* TUs don't reference other CUs/TUs (except via type signatures). */
19579 if (cu->per_cu->is_debug_types)
19580 {
19581 error (_("Dwarf Error: Type Unit at offset %s contains"
19582 " external reference to offset %s [in module %s].\n"),
19583 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19584 bfd_get_filename (objfile->obfd));
19585 }
19586 dwarf2_per_cu_data *per_cu
19587 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19588 per_objfile->per_bfd);
19589
19590 cu = per_objfile->get_cu (per_cu);
19591 if (cu == NULL || cu->partial_dies == NULL)
19592 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19593
19594 cu = per_objfile->get_cu (per_cu);
19595
19596 cu->last_used = 0;
19597 pd = cu->find_partial_die (sect_off);
19598 }
19599
19600 /* If we didn't find it, and not all dies have been loaded,
19601 load them all and try again. */
19602
19603 if (pd == NULL && cu->load_all_dies == 0)
19604 {
19605 cu->load_all_dies = 1;
19606
19607 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19608 THIS_CU->cu may already be in use. So we can't just free it and
19609 replace its DIEs with the ones we read in. Instead, we leave those
19610 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19611 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19612 set. */
19613 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19614
19615 pd = cu->find_partial_die (sect_off);
19616 }
19617
19618 if (pd == NULL)
19619 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19620 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19621 return { cu, pd };
19622 }
19623
19624 /* See if we can figure out if the class lives in a namespace. We do
19625 this by looking for a member function; its demangled name will
19626 contain namespace info, if there is any. */
19627
19628 static void
19629 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19630 struct dwarf2_cu *cu)
19631 {
19632 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19633 what template types look like, because the demangler
19634 frequently doesn't give the same name as the debug info. We
19635 could fix this by only using the demangled name to get the
19636 prefix (but see comment in read_structure_type). */
19637
19638 struct partial_die_info *real_pdi;
19639 struct partial_die_info *child_pdi;
19640
19641 /* If this DIE (this DIE's specification, if any) has a parent, then
19642 we should not do this. We'll prepend the parent's fully qualified
19643 name when we create the partial symbol. */
19644
19645 real_pdi = struct_pdi;
19646 while (real_pdi->has_specification)
19647 {
19648 auto res = find_partial_die (real_pdi->spec_offset,
19649 real_pdi->spec_is_dwz, cu);
19650 real_pdi = res.pdi;
19651 cu = res.cu;
19652 }
19653
19654 if (real_pdi->die_parent != NULL)
19655 return;
19656
19657 for (child_pdi = struct_pdi->die_child;
19658 child_pdi != NULL;
19659 child_pdi = child_pdi->die_sibling)
19660 {
19661 if (child_pdi->tag == DW_TAG_subprogram
19662 && child_pdi->linkage_name != NULL)
19663 {
19664 gdb::unique_xmalloc_ptr<char> actual_class_name
19665 (cu->language_defn->class_name_from_physname
19666 (child_pdi->linkage_name));
19667 if (actual_class_name != NULL)
19668 {
19669 struct objfile *objfile = cu->per_objfile->objfile;
19670 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19671 struct_pdi->canonical_name = 1;
19672 }
19673 break;
19674 }
19675 }
19676 }
19677
19678 /* Return true if a DIE with TAG may have the DW_AT_const_value
19679 attribute. */
19680
19681 static bool
19682 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19683 {
19684 switch (tag)
19685 {
19686 case DW_TAG_constant:
19687 case DW_TAG_enumerator:
19688 case DW_TAG_formal_parameter:
19689 case DW_TAG_template_value_param:
19690 case DW_TAG_variable:
19691 return true;
19692 }
19693
19694 return false;
19695 }
19696
19697 void
19698 partial_die_info::fixup (struct dwarf2_cu *cu)
19699 {
19700 /* Once we've fixed up a die, there's no point in doing so again.
19701 This also avoids a memory leak if we were to call
19702 guess_partial_die_structure_name multiple times. */
19703 if (fixup_called)
19704 return;
19705
19706 /* If we found a reference attribute and the DIE has no name, try
19707 to find a name in the referred to DIE. */
19708
19709 if (raw_name == NULL && has_specification)
19710 {
19711 struct partial_die_info *spec_die;
19712
19713 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19714 spec_die = res.pdi;
19715 cu = res.cu;
19716
19717 spec_die->fixup (cu);
19718
19719 if (spec_die->raw_name)
19720 {
19721 raw_name = spec_die->raw_name;
19722 canonical_name = spec_die->canonical_name;
19723
19724 /* Copy DW_AT_external attribute if it is set. */
19725 if (spec_die->is_external)
19726 is_external = spec_die->is_external;
19727 }
19728 }
19729
19730 if (!has_const_value && has_specification
19731 && can_have_DW_AT_const_value_p (tag))
19732 {
19733 struct partial_die_info *spec_die;
19734
19735 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19736 spec_die = res.pdi;
19737 cu = res.cu;
19738
19739 spec_die->fixup (cu);
19740
19741 if (spec_die->has_const_value)
19742 {
19743 /* Copy DW_AT_const_value attribute if it is set. */
19744 has_const_value = spec_die->has_const_value;
19745 }
19746 }
19747
19748 /* Set default names for some unnamed DIEs. */
19749
19750 if (raw_name == NULL && tag == DW_TAG_namespace)
19751 {
19752 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19753 canonical_name = 1;
19754 }
19755
19756 /* If there is no parent die to provide a namespace, and there are
19757 children, see if we can determine the namespace from their linkage
19758 name. */
19759 if (cu->per_cu->lang == language_cplus
19760 && !cu->per_objfile->per_bfd->types.empty ()
19761 && die_parent == NULL
19762 && has_children
19763 && (tag == DW_TAG_class_type
19764 || tag == DW_TAG_structure_type
19765 || tag == DW_TAG_union_type))
19766 guess_partial_die_structure_name (this, cu);
19767
19768 /* GCC might emit a nameless struct or union that has a linkage
19769 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19770 if (raw_name == NULL
19771 && (tag == DW_TAG_class_type
19772 || tag == DW_TAG_interface_type
19773 || tag == DW_TAG_structure_type
19774 || tag == DW_TAG_union_type)
19775 && linkage_name != NULL)
19776 {
19777 gdb::unique_xmalloc_ptr<char> demangled
19778 (gdb_demangle (linkage_name, DMGL_TYPES));
19779 if (demangled != nullptr)
19780 {
19781 const char *base;
19782
19783 /* Strip any leading namespaces/classes, keep only the base name.
19784 DW_AT_name for named DIEs does not contain the prefixes. */
19785 base = strrchr (demangled.get (), ':');
19786 if (base && base > demangled.get () && base[-1] == ':')
19787 base++;
19788 else
19789 base = demangled.get ();
19790
19791 struct objfile *objfile = cu->per_objfile->objfile;
19792 raw_name = objfile->intern (base);
19793 canonical_name = 1;
19794 }
19795 }
19796
19797 fixup_called = 1;
19798 }
19799
19800 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19801 contents from the given SECTION in the HEADER.
19802
19803 HEADER_OFFSET is the offset of the header in the section. */
19804 static void
19805 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19806 struct dwarf2_section_info *section,
19807 sect_offset header_offset)
19808 {
19809 unsigned int bytes_read;
19810 bfd *abfd = section->get_bfd_owner ();
19811 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19812
19813 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19814 info_ptr += bytes_read;
19815
19816 header->version = read_2_bytes (abfd, info_ptr);
19817 info_ptr += 2;
19818
19819 header->addr_size = read_1_byte (abfd, info_ptr);
19820 info_ptr += 1;
19821
19822 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19823 info_ptr += 1;
19824
19825 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19826 }
19827
19828 /* Return the DW_AT_loclists_base value for the CU. */
19829 static ULONGEST
19830 lookup_loclist_base (struct dwarf2_cu *cu)
19831 {
19832 /* For the .dwo unit, the loclist_base points to the first offset following
19833 the header. The header consists of the following entities-
19834 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19835 bit format)
19836 2. version (2 bytes)
19837 3. address size (1 byte)
19838 4. segment selector size (1 byte)
19839 5. offset entry count (4 bytes)
19840 These sizes are derived as per the DWARFv5 standard. */
19841 if (cu->dwo_unit != nullptr)
19842 {
19843 if (cu->header.initial_length_size == 4)
19844 return LOCLIST_HEADER_SIZE32;
19845 return LOCLIST_HEADER_SIZE64;
19846 }
19847 return cu->loclist_base;
19848 }
19849
19850 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19851 array of offsets in the .debug_loclists section. */
19852
19853 static sect_offset
19854 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19855 {
19856 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19857 struct objfile *objfile = per_objfile->objfile;
19858 bfd *abfd = objfile->obfd;
19859 ULONGEST loclist_header_size =
19860 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19861 : LOCLIST_HEADER_SIZE64);
19862 ULONGEST loclist_base = lookup_loclist_base (cu);
19863
19864 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19865 ULONGEST start_offset =
19866 loclist_base + loclist_index * cu->header.offset_size;
19867
19868 /* Get loclists section. */
19869 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19870
19871 /* Read the loclists section content. */
19872 section->read (objfile);
19873 if (section->buffer == NULL)
19874 error (_("DW_FORM_loclistx used without .debug_loclists "
19875 "section [in module %s]"), objfile_name (objfile));
19876
19877 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19878 so if loclist_base is smaller than the header size, we have a problem. */
19879 if (loclist_base < loclist_header_size)
19880 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19881 objfile_name (objfile));
19882
19883 /* Read the header of the loclists contribution. */
19884 struct loclists_rnglists_header header;
19885 read_loclists_rnglists_header (&header, section,
19886 (sect_offset) (loclist_base - loclist_header_size));
19887
19888 /* Verify the loclist index is valid. */
19889 if (loclist_index >= header.offset_entry_count)
19890 error (_("DW_FORM_loclistx pointing outside of "
19891 ".debug_loclists offset array [in module %s]"),
19892 objfile_name (objfile));
19893
19894 /* Validate that reading won't go beyond the end of the section. */
19895 if (start_offset + cu->header.offset_size > section->size)
19896 error (_("Reading DW_FORM_loclistx index beyond end of"
19897 ".debug_loclists section [in module %s]"),
19898 objfile_name (objfile));
19899
19900 const gdb_byte *info_ptr = section->buffer + start_offset;
19901
19902 if (cu->header.offset_size == 4)
19903 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19904 else
19905 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19906 }
19907
19908 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19909 array of offsets in the .debug_rnglists section. */
19910
19911 static sect_offset
19912 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19913 dwarf_tag tag)
19914 {
19915 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19916 struct objfile *objfile = dwarf2_per_objfile->objfile;
19917 bfd *abfd = objfile->obfd;
19918 ULONGEST rnglist_header_size =
19919 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19920 : RNGLIST_HEADER_SIZE64);
19921
19922 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19923 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19924 doesn't apply. */
19925 ULONGEST rnglist_base =
19926 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19927
19928 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19929 ULONGEST start_offset =
19930 rnglist_base + rnglist_index * cu->header.offset_size;
19931
19932 /* Get rnglists section. */
19933 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19934
19935 /* Read the rnglists section content. */
19936 section->read (objfile);
19937 if (section->buffer == nullptr)
19938 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19939 "[in module %s]"),
19940 objfile_name (objfile));
19941
19942 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19943 so if rnglist_base is smaller than the header size, we have a problem. */
19944 if (rnglist_base < rnglist_header_size)
19945 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19946 objfile_name (objfile));
19947
19948 /* Read the header of the rnglists contribution. */
19949 struct loclists_rnglists_header header;
19950 read_loclists_rnglists_header (&header, section,
19951 (sect_offset) (rnglist_base - rnglist_header_size));
19952
19953 /* Verify the rnglist index is valid. */
19954 if (rnglist_index >= header.offset_entry_count)
19955 error (_("DW_FORM_rnglistx index pointing outside of "
19956 ".debug_rnglists offset array [in module %s]"),
19957 objfile_name (objfile));
19958
19959 /* Validate that reading won't go beyond the end of the section. */
19960 if (start_offset + cu->header.offset_size > section->size)
19961 error (_("Reading DW_FORM_rnglistx index beyond end of"
19962 ".debug_rnglists section [in module %s]"),
19963 objfile_name (objfile));
19964
19965 const gdb_byte *info_ptr = section->buffer + start_offset;
19966
19967 if (cu->header.offset_size == 4)
19968 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19969 else
19970 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19971 }
19972
19973 /* Process the attributes that had to be skipped in the first round. These
19974 attributes are the ones that need str_offsets_base or addr_base attributes.
19975 They could not have been processed in the first round, because at the time
19976 the values of str_offsets_base or addr_base may not have been known. */
19977 static void
19978 read_attribute_reprocess (const struct die_reader_specs *reader,
19979 struct attribute *attr, dwarf_tag tag)
19980 {
19981 struct dwarf2_cu *cu = reader->cu;
19982 switch (attr->form)
19983 {
19984 case DW_FORM_addrx:
19985 case DW_FORM_GNU_addr_index:
19986 attr->set_address (read_addr_index (cu,
19987 attr->as_unsigned_reprocess ()));
19988 break;
19989 case DW_FORM_loclistx:
19990 {
19991 sect_offset loclists_sect_off
19992 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19993
19994 attr->set_unsigned (to_underlying (loclists_sect_off));
19995 }
19996 break;
19997 case DW_FORM_rnglistx:
19998 {
19999 sect_offset rnglists_sect_off
20000 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20001
20002 attr->set_unsigned (to_underlying (rnglists_sect_off));
20003 }
20004 break;
20005 case DW_FORM_strx:
20006 case DW_FORM_strx1:
20007 case DW_FORM_strx2:
20008 case DW_FORM_strx3:
20009 case DW_FORM_strx4:
20010 case DW_FORM_GNU_str_index:
20011 {
20012 unsigned int str_index = attr->as_unsigned_reprocess ();
20013 gdb_assert (!attr->canonical_string_p ());
20014 if (reader->dwo_file != NULL)
20015 attr->set_string_noncanonical (read_dwo_str_index (reader,
20016 str_index));
20017 else
20018 attr->set_string_noncanonical (read_stub_str_index (cu,
20019 str_index));
20020 break;
20021 }
20022 default:
20023 gdb_assert_not_reached ("Unexpected DWARF form.");
20024 }
20025 }
20026
20027 /* Read an attribute value described by an attribute form. */
20028
20029 static const gdb_byte *
20030 read_attribute_value (const struct die_reader_specs *reader,
20031 struct attribute *attr, unsigned form,
20032 LONGEST implicit_const, const gdb_byte *info_ptr)
20033 {
20034 struct dwarf2_cu *cu = reader->cu;
20035 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20036 struct objfile *objfile = per_objfile->objfile;
20037 bfd *abfd = reader->abfd;
20038 struct comp_unit_head *cu_header = &cu->header;
20039 unsigned int bytes_read;
20040 struct dwarf_block *blk;
20041
20042 attr->form = (enum dwarf_form) form;
20043 switch (form)
20044 {
20045 case DW_FORM_ref_addr:
20046 if (cu_header->version == 2)
20047 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20048 &bytes_read));
20049 else
20050 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20051 &bytes_read));
20052 info_ptr += bytes_read;
20053 break;
20054 case DW_FORM_GNU_ref_alt:
20055 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20056 &bytes_read));
20057 info_ptr += bytes_read;
20058 break;
20059 case DW_FORM_addr:
20060 {
20061 struct gdbarch *gdbarch = objfile->arch ();
20062 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20063 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20064 attr->set_address (addr);
20065 info_ptr += bytes_read;
20066 }
20067 break;
20068 case DW_FORM_block2:
20069 blk = dwarf_alloc_block (cu);
20070 blk->size = read_2_bytes (abfd, info_ptr);
20071 info_ptr += 2;
20072 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20073 info_ptr += blk->size;
20074 attr->set_block (blk);
20075 break;
20076 case DW_FORM_block4:
20077 blk = dwarf_alloc_block (cu);
20078 blk->size = read_4_bytes (abfd, info_ptr);
20079 info_ptr += 4;
20080 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20081 info_ptr += blk->size;
20082 attr->set_block (blk);
20083 break;
20084 case DW_FORM_data2:
20085 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20086 info_ptr += 2;
20087 break;
20088 case DW_FORM_data4:
20089 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20090 info_ptr += 4;
20091 break;
20092 case DW_FORM_data8:
20093 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20094 info_ptr += 8;
20095 break;
20096 case DW_FORM_data16:
20097 blk = dwarf_alloc_block (cu);
20098 blk->size = 16;
20099 blk->data = read_n_bytes (abfd, info_ptr, 16);
20100 info_ptr += 16;
20101 attr->set_block (blk);
20102 break;
20103 case DW_FORM_sec_offset:
20104 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20105 &bytes_read));
20106 info_ptr += bytes_read;
20107 break;
20108 case DW_FORM_loclistx:
20109 {
20110 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20111 &bytes_read));
20112 info_ptr += bytes_read;
20113 }
20114 break;
20115 case DW_FORM_string:
20116 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20117 &bytes_read));
20118 info_ptr += bytes_read;
20119 break;
20120 case DW_FORM_strp:
20121 if (!cu->per_cu->is_dwz)
20122 {
20123 attr->set_string_noncanonical
20124 (read_indirect_string (per_objfile,
20125 abfd, info_ptr, cu_header,
20126 &bytes_read));
20127 info_ptr += bytes_read;
20128 break;
20129 }
20130 /* FALLTHROUGH */
20131 case DW_FORM_line_strp:
20132 if (!cu->per_cu->is_dwz)
20133 {
20134 attr->set_string_noncanonical
20135 (per_objfile->read_line_string (info_ptr, cu_header,
20136 &bytes_read));
20137 info_ptr += bytes_read;
20138 break;
20139 }
20140 /* FALLTHROUGH */
20141 case DW_FORM_GNU_strp_alt:
20142 {
20143 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20144 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20145 &bytes_read);
20146
20147 attr->set_string_noncanonical
20148 (dwz->read_string (objfile, str_offset));
20149 info_ptr += bytes_read;
20150 }
20151 break;
20152 case DW_FORM_exprloc:
20153 case DW_FORM_block:
20154 blk = dwarf_alloc_block (cu);
20155 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20156 info_ptr += bytes_read;
20157 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20158 info_ptr += blk->size;
20159 attr->set_block (blk);
20160 break;
20161 case DW_FORM_block1:
20162 blk = dwarf_alloc_block (cu);
20163 blk->size = read_1_byte (abfd, info_ptr);
20164 info_ptr += 1;
20165 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20166 info_ptr += blk->size;
20167 attr->set_block (blk);
20168 break;
20169 case DW_FORM_data1:
20170 case DW_FORM_flag:
20171 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20172 info_ptr += 1;
20173 break;
20174 case DW_FORM_flag_present:
20175 attr->set_unsigned (1);
20176 break;
20177 case DW_FORM_sdata:
20178 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20179 info_ptr += bytes_read;
20180 break;
20181 case DW_FORM_rnglistx:
20182 {
20183 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20184 &bytes_read));
20185 info_ptr += bytes_read;
20186 }
20187 break;
20188 case DW_FORM_udata:
20189 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20190 info_ptr += bytes_read;
20191 break;
20192 case DW_FORM_ref1:
20193 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20194 + read_1_byte (abfd, info_ptr)));
20195 info_ptr += 1;
20196 break;
20197 case DW_FORM_ref2:
20198 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20199 + read_2_bytes (abfd, info_ptr)));
20200 info_ptr += 2;
20201 break;
20202 case DW_FORM_ref4:
20203 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20204 + read_4_bytes (abfd, info_ptr)));
20205 info_ptr += 4;
20206 break;
20207 case DW_FORM_ref8:
20208 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20209 + read_8_bytes (abfd, info_ptr)));
20210 info_ptr += 8;
20211 break;
20212 case DW_FORM_ref_sig8:
20213 attr->set_signature (read_8_bytes (abfd, info_ptr));
20214 info_ptr += 8;
20215 break;
20216 case DW_FORM_ref_udata:
20217 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20218 + read_unsigned_leb128 (abfd, info_ptr,
20219 &bytes_read)));
20220 info_ptr += bytes_read;
20221 break;
20222 case DW_FORM_indirect:
20223 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20224 info_ptr += bytes_read;
20225 if (form == DW_FORM_implicit_const)
20226 {
20227 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20228 info_ptr += bytes_read;
20229 }
20230 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20231 info_ptr);
20232 break;
20233 case DW_FORM_implicit_const:
20234 attr->set_signed (implicit_const);
20235 break;
20236 case DW_FORM_addrx:
20237 case DW_FORM_GNU_addr_index:
20238 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20239 &bytes_read));
20240 info_ptr += bytes_read;
20241 break;
20242 case DW_FORM_strx:
20243 case DW_FORM_strx1:
20244 case DW_FORM_strx2:
20245 case DW_FORM_strx3:
20246 case DW_FORM_strx4:
20247 case DW_FORM_GNU_str_index:
20248 {
20249 ULONGEST str_index;
20250 if (form == DW_FORM_strx1)
20251 {
20252 str_index = read_1_byte (abfd, info_ptr);
20253 info_ptr += 1;
20254 }
20255 else if (form == DW_FORM_strx2)
20256 {
20257 str_index = read_2_bytes (abfd, info_ptr);
20258 info_ptr += 2;
20259 }
20260 else if (form == DW_FORM_strx3)
20261 {
20262 str_index = read_3_bytes (abfd, info_ptr);
20263 info_ptr += 3;
20264 }
20265 else if (form == DW_FORM_strx4)
20266 {
20267 str_index = read_4_bytes (abfd, info_ptr);
20268 info_ptr += 4;
20269 }
20270 else
20271 {
20272 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20273 info_ptr += bytes_read;
20274 }
20275 attr->set_unsigned_reprocess (str_index);
20276 }
20277 break;
20278 default:
20279 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20280 dwarf_form_name (form),
20281 bfd_get_filename (abfd));
20282 }
20283
20284 /* Super hack. */
20285 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20286 attr->form = DW_FORM_GNU_ref_alt;
20287
20288 /* We have seen instances where the compiler tried to emit a byte
20289 size attribute of -1 which ended up being encoded as an unsigned
20290 0xffffffff. Although 0xffffffff is technically a valid size value,
20291 an object of this size seems pretty unlikely so we can relatively
20292 safely treat these cases as if the size attribute was invalid and
20293 treat them as zero by default. */
20294 if (attr->name == DW_AT_byte_size
20295 && form == DW_FORM_data4
20296 && attr->as_unsigned () >= 0xffffffff)
20297 {
20298 complaint
20299 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20300 hex_string (attr->as_unsigned ()));
20301 attr->set_unsigned (0);
20302 }
20303
20304 return info_ptr;
20305 }
20306
20307 /* Read an attribute described by an abbreviated attribute. */
20308
20309 static const gdb_byte *
20310 read_attribute (const struct die_reader_specs *reader,
20311 struct attribute *attr, const struct attr_abbrev *abbrev,
20312 const gdb_byte *info_ptr)
20313 {
20314 attr->name = abbrev->name;
20315 attr->string_is_canonical = 0;
20316 attr->requires_reprocessing = 0;
20317 return read_attribute_value (reader, attr, abbrev->form,
20318 abbrev->implicit_const, info_ptr);
20319 }
20320
20321 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20322
20323 static const char *
20324 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20325 LONGEST str_offset)
20326 {
20327 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20328 str_offset, "DW_FORM_strp");
20329 }
20330
20331 /* Return pointer to string at .debug_str offset as read from BUF.
20332 BUF is assumed to be in a compilation unit described by CU_HEADER.
20333 Return *BYTES_READ_PTR count of bytes read from BUF. */
20334
20335 static const char *
20336 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20337 const gdb_byte *buf,
20338 const struct comp_unit_head *cu_header,
20339 unsigned int *bytes_read_ptr)
20340 {
20341 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20342
20343 return read_indirect_string_at_offset (per_objfile, str_offset);
20344 }
20345
20346 /* See read.h. */
20347
20348 const char *
20349 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20350 unsigned int offset_size)
20351 {
20352 bfd *abfd = objfile->obfd;
20353 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
20354
20355 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20356 }
20357
20358 /* See read.h. */
20359
20360 const char *
20361 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20362 const struct comp_unit_head *cu_header,
20363 unsigned int *bytes_read_ptr)
20364 {
20365 bfd *abfd = objfile->obfd;
20366 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20367
20368 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20369 }
20370
20371 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20372 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20373 ADDR_SIZE is the size of addresses from the CU header. */
20374
20375 static CORE_ADDR
20376 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20377 gdb::optional<ULONGEST> addr_base, int addr_size)
20378 {
20379 struct objfile *objfile = per_objfile->objfile;
20380 bfd *abfd = objfile->obfd;
20381 const gdb_byte *info_ptr;
20382 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20383
20384 per_objfile->per_bfd->addr.read (objfile);
20385 if (per_objfile->per_bfd->addr.buffer == NULL)
20386 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20387 objfile_name (objfile));
20388 if (addr_base_or_zero + addr_index * addr_size
20389 >= per_objfile->per_bfd->addr.size)
20390 error (_("DW_FORM_addr_index pointing outside of "
20391 ".debug_addr section [in module %s]"),
20392 objfile_name (objfile));
20393 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20394 + addr_index * addr_size);
20395 if (addr_size == 4)
20396 return bfd_get_32 (abfd, info_ptr);
20397 else
20398 return bfd_get_64 (abfd, info_ptr);
20399 }
20400
20401 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20402
20403 static CORE_ADDR
20404 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20405 {
20406 return read_addr_index_1 (cu->per_objfile, addr_index,
20407 cu->addr_base, cu->header.addr_size);
20408 }
20409
20410 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20411
20412 static CORE_ADDR
20413 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20414 unsigned int *bytes_read)
20415 {
20416 bfd *abfd = cu->per_objfile->objfile->obfd;
20417 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20418
20419 return read_addr_index (cu, addr_index);
20420 }
20421
20422 /* See read.h. */
20423
20424 CORE_ADDR
20425 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20426 dwarf2_per_objfile *per_objfile,
20427 unsigned int addr_index)
20428 {
20429 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20430 gdb::optional<ULONGEST> addr_base;
20431 int addr_size;
20432
20433 /* We need addr_base and addr_size.
20434 If we don't have PER_CU->cu, we have to get it.
20435 Nasty, but the alternative is storing the needed info in PER_CU,
20436 which at this point doesn't seem justified: it's not clear how frequently
20437 it would get used and it would increase the size of every PER_CU.
20438 Entry points like dwarf2_per_cu_addr_size do a similar thing
20439 so we're not in uncharted territory here.
20440 Alas we need to be a bit more complicated as addr_base is contained
20441 in the DIE.
20442
20443 We don't need to read the entire CU(/TU).
20444 We just need the header and top level die.
20445
20446 IWBN to use the aging mechanism to let us lazily later discard the CU.
20447 For now we skip this optimization. */
20448
20449 if (cu != NULL)
20450 {
20451 addr_base = cu->addr_base;
20452 addr_size = cu->header.addr_size;
20453 }
20454 else
20455 {
20456 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20457 addr_base = reader.cu->addr_base;
20458 addr_size = reader.cu->header.addr_size;
20459 }
20460
20461 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20462 }
20463
20464 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20465 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20466 DWO file. */
20467
20468 static const char *
20469 read_str_index (struct dwarf2_cu *cu,
20470 struct dwarf2_section_info *str_section,
20471 struct dwarf2_section_info *str_offsets_section,
20472 ULONGEST str_offsets_base, ULONGEST str_index)
20473 {
20474 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20475 struct objfile *objfile = per_objfile->objfile;
20476 const char *objf_name = objfile_name (objfile);
20477 bfd *abfd = objfile->obfd;
20478 const gdb_byte *info_ptr;
20479 ULONGEST str_offset;
20480 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20481
20482 str_section->read (objfile);
20483 str_offsets_section->read (objfile);
20484 if (str_section->buffer == NULL)
20485 error (_("%s used without %s section"
20486 " in CU at offset %s [in module %s]"),
20487 form_name, str_section->get_name (),
20488 sect_offset_str (cu->header.sect_off), objf_name);
20489 if (str_offsets_section->buffer == NULL)
20490 error (_("%s used without %s section"
20491 " in CU at offset %s [in module %s]"),
20492 form_name, str_section->get_name (),
20493 sect_offset_str (cu->header.sect_off), objf_name);
20494 info_ptr = (str_offsets_section->buffer
20495 + str_offsets_base
20496 + str_index * cu->header.offset_size);
20497 if (cu->header.offset_size == 4)
20498 str_offset = bfd_get_32 (abfd, info_ptr);
20499 else
20500 str_offset = bfd_get_64 (abfd, info_ptr);
20501 if (str_offset >= str_section->size)
20502 error (_("Offset from %s pointing outside of"
20503 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20504 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20505 return (const char *) (str_section->buffer + str_offset);
20506 }
20507
20508 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20509
20510 static const char *
20511 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20512 {
20513 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20514 ? reader->cu->header.addr_size : 0;
20515 return read_str_index (reader->cu,
20516 &reader->dwo_file->sections.str,
20517 &reader->dwo_file->sections.str_offsets,
20518 str_offsets_base, str_index);
20519 }
20520
20521 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20522
20523 static const char *
20524 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20525 {
20526 struct objfile *objfile = cu->per_objfile->objfile;
20527 const char *objf_name = objfile_name (objfile);
20528 static const char form_name[] = "DW_FORM_GNU_str_index";
20529 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20530
20531 if (!cu->str_offsets_base.has_value ())
20532 error (_("%s used in Fission stub without %s"
20533 " in CU at offset 0x%lx [in module %s]"),
20534 form_name, str_offsets_attr_name,
20535 (long) cu->header.offset_size, objf_name);
20536
20537 return read_str_index (cu,
20538 &cu->per_objfile->per_bfd->str,
20539 &cu->per_objfile->per_bfd->str_offsets,
20540 *cu->str_offsets_base, str_index);
20541 }
20542
20543 /* Return the length of an LEB128 number in BUF. */
20544
20545 static int
20546 leb128_size (const gdb_byte *buf)
20547 {
20548 const gdb_byte *begin = buf;
20549 gdb_byte byte;
20550
20551 while (1)
20552 {
20553 byte = *buf++;
20554 if ((byte & 128) == 0)
20555 return buf - begin;
20556 }
20557 }
20558
20559 static enum language
20560 dwarf_lang_to_enum_language (unsigned int lang)
20561 {
20562 enum language language;
20563
20564 switch (lang)
20565 {
20566 case DW_LANG_C89:
20567 case DW_LANG_C99:
20568 case DW_LANG_C11:
20569 case DW_LANG_C:
20570 case DW_LANG_UPC:
20571 language = language_c;
20572 break;
20573 case DW_LANG_Java:
20574 case DW_LANG_C_plus_plus:
20575 case DW_LANG_C_plus_plus_11:
20576 case DW_LANG_C_plus_plus_14:
20577 language = language_cplus;
20578 break;
20579 case DW_LANG_D:
20580 language = language_d;
20581 break;
20582 case DW_LANG_Fortran77:
20583 case DW_LANG_Fortran90:
20584 case DW_LANG_Fortran95:
20585 case DW_LANG_Fortran03:
20586 case DW_LANG_Fortran08:
20587 language = language_fortran;
20588 break;
20589 case DW_LANG_Go:
20590 language = language_go;
20591 break;
20592 case DW_LANG_Mips_Assembler:
20593 language = language_asm;
20594 break;
20595 case DW_LANG_Ada83:
20596 case DW_LANG_Ada95:
20597 language = language_ada;
20598 break;
20599 case DW_LANG_Modula2:
20600 language = language_m2;
20601 break;
20602 case DW_LANG_Pascal83:
20603 language = language_pascal;
20604 break;
20605 case DW_LANG_ObjC:
20606 language = language_objc;
20607 break;
20608 case DW_LANG_Rust:
20609 case DW_LANG_Rust_old:
20610 language = language_rust;
20611 break;
20612 case DW_LANG_OpenCL:
20613 language = language_opencl;
20614 break;
20615 case DW_LANG_Cobol74:
20616 case DW_LANG_Cobol85:
20617 default:
20618 language = language_minimal;
20619 break;
20620 }
20621
20622 return language;
20623 }
20624
20625 /* Return the named attribute or NULL if not there. */
20626
20627 static struct attribute *
20628 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20629 {
20630 for (;;)
20631 {
20632 unsigned int i;
20633 struct attribute *spec = NULL;
20634
20635 for (i = 0; i < die->num_attrs; ++i)
20636 {
20637 if (die->attrs[i].name == name)
20638 return &die->attrs[i];
20639 if (die->attrs[i].name == DW_AT_specification
20640 || die->attrs[i].name == DW_AT_abstract_origin)
20641 spec = &die->attrs[i];
20642 }
20643
20644 if (!spec)
20645 break;
20646
20647 die = follow_die_ref (die, spec, &cu);
20648 }
20649
20650 return NULL;
20651 }
20652
20653 /* Return the string associated with a string-typed attribute, or NULL if it
20654 is either not found or is of an incorrect type. */
20655
20656 static const char *
20657 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20658 {
20659 struct attribute *attr;
20660 const char *str = NULL;
20661
20662 attr = dwarf2_attr (die, name, cu);
20663
20664 if (attr != NULL)
20665 {
20666 str = attr->as_string ();
20667 if (str == nullptr)
20668 complaint (_("string type expected for attribute %s for "
20669 "DIE at %s in module %s"),
20670 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20671 objfile_name (cu->per_objfile->objfile));
20672 }
20673
20674 return str;
20675 }
20676
20677 /* Return the dwo name or NULL if not present. If present, it is in either
20678 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20679 static const char *
20680 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20681 {
20682 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20683 if (dwo_name == nullptr)
20684 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20685 return dwo_name;
20686 }
20687
20688 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20689 and holds a non-zero value. This function should only be used for
20690 DW_FORM_flag or DW_FORM_flag_present attributes. */
20691
20692 static int
20693 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20694 {
20695 struct attribute *attr = dwarf2_attr (die, name, cu);
20696
20697 return attr != nullptr && attr->as_boolean ();
20698 }
20699
20700 static int
20701 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20702 {
20703 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20704 which value is non-zero. However, we have to be careful with
20705 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20706 (via dwarf2_flag_true_p) follows this attribute. So we may
20707 end up accidently finding a declaration attribute that belongs
20708 to a different DIE referenced by the specification attribute,
20709 even though the given DIE does not have a declaration attribute. */
20710 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20711 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20712 }
20713
20714 /* Return the die giving the specification for DIE, if there is
20715 one. *SPEC_CU is the CU containing DIE on input, and the CU
20716 containing the return value on output. If there is no
20717 specification, but there is an abstract origin, that is
20718 returned. */
20719
20720 static struct die_info *
20721 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20722 {
20723 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20724 *spec_cu);
20725
20726 if (spec_attr == NULL)
20727 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20728
20729 if (spec_attr == NULL)
20730 return NULL;
20731 else
20732 return follow_die_ref (die, spec_attr, spec_cu);
20733 }
20734
20735 /* A convenience function to find the proper .debug_line section for a CU. */
20736
20737 static struct dwarf2_section_info *
20738 get_debug_line_section (struct dwarf2_cu *cu)
20739 {
20740 struct dwarf2_section_info *section;
20741 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20742
20743 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20744 DWO file. */
20745 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20746 section = &cu->dwo_unit->dwo_file->sections.line;
20747 else if (cu->per_cu->is_dwz)
20748 {
20749 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20750
20751 section = &dwz->line;
20752 }
20753 else
20754 section = &per_objfile->per_bfd->line;
20755
20756 return section;
20757 }
20758
20759 /* Read the statement program header starting at OFFSET in
20760 .debug_line, or .debug_line.dwo. Return a pointer
20761 to a struct line_header, allocated using xmalloc.
20762 Returns NULL if there is a problem reading the header, e.g., if it
20763 has a version we don't understand.
20764
20765 NOTE: the strings in the include directory and file name tables of
20766 the returned object point into the dwarf line section buffer,
20767 and must not be freed. */
20768
20769 static line_header_up
20770 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20771 {
20772 struct dwarf2_section_info *section;
20773 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20774
20775 section = get_debug_line_section (cu);
20776 section->read (per_objfile->objfile);
20777 if (section->buffer == NULL)
20778 {
20779 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20780 complaint (_("missing .debug_line.dwo section"));
20781 else
20782 complaint (_("missing .debug_line section"));
20783 return 0;
20784 }
20785
20786 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20787 per_objfile, section, &cu->header);
20788 }
20789
20790 /* Subroutine of dwarf_decode_lines to simplify it.
20791 Return the file name for the given file_entry.
20792 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
20793 If space for the result is malloc'd, *NAME_HOLDER will be set.
20794 Returns NULL if FILE_INDEX should be ignored, i.e., it is
20795 equivalent to CU_INFO. */
20796
20797 static const char *
20798 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
20799 const file_and_directory &cu_info,
20800 gdb::unique_xmalloc_ptr<char> *name_holder)
20801 {
20802 const char *include_name = fe.name;
20803 const char *include_name_to_compare = include_name;
20804
20805 const char *dir_name = fe.include_dir (lh);
20806
20807 gdb::unique_xmalloc_ptr<char> hold_compare;
20808 if (!IS_ABSOLUTE_PATH (include_name)
20809 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
20810 {
20811 /* Avoid creating a duplicate name for CU_INFO.
20812 We do this by comparing INCLUDE_NAME and CU_INFO.
20813 Before we do the comparison, however, we need to account
20814 for DIR_NAME and COMP_DIR.
20815 First prepend dir_name (if non-NULL). If we still don't
20816 have an absolute path prepend comp_dir (if non-NULL).
20817 However, the directory we record in the include-file's
20818 psymtab does not contain COMP_DIR (to match the
20819 corresponding symtab(s)).
20820
20821 Example:
20822
20823 bash$ cd /tmp
20824 bash$ gcc -g ./hello.c
20825 include_name = "hello.c"
20826 dir_name = "."
20827 DW_AT_comp_dir = comp_dir = "/tmp"
20828 DW_AT_name = "./hello.c"
20829
20830 */
20831
20832 if (dir_name != NULL)
20833 {
20834 name_holder->reset (concat (dir_name, SLASH_STRING,
20835 include_name, (char *) NULL));
20836 include_name = name_holder->get ();
20837 include_name_to_compare = include_name;
20838 }
20839 if (!IS_ABSOLUTE_PATH (include_name)
20840 && cu_info.get_comp_dir () != nullptr)
20841 {
20842 hold_compare.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20843 include_name, (char *) NULL));
20844 include_name_to_compare = hold_compare.get ();
20845 }
20846 }
20847
20848 gdb::unique_xmalloc_ptr<char> copied_name;
20849 const char *cu_filename = cu_info.get_name ();
20850 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
20851 {
20852 copied_name.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20853 cu_filename, (char *) NULL));
20854 cu_filename = copied_name.get ();
20855 }
20856
20857 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
20858 return nullptr;
20859 return include_name;
20860 }
20861
20862 /* State machine to track the state of the line number program. */
20863
20864 class lnp_state_machine
20865 {
20866 public:
20867 /* Initialize a machine state for the start of a line number
20868 program. */
20869 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20870 bool record_lines_p);
20871
20872 file_entry *current_file ()
20873 {
20874 /* lh->file_names is 0-based, but the file name numbers in the
20875 statement program are 1-based. */
20876 return m_line_header->file_name_at (m_file);
20877 }
20878
20879 /* Record the line in the state machine. END_SEQUENCE is true if
20880 we're processing the end of a sequence. */
20881 void record_line (bool end_sequence);
20882
20883 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20884 nop-out rest of the lines in this sequence. */
20885 void check_line_address (struct dwarf2_cu *cu,
20886 const gdb_byte *line_ptr,
20887 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20888
20889 void handle_set_discriminator (unsigned int discriminator)
20890 {
20891 m_discriminator = discriminator;
20892 m_line_has_non_zero_discriminator |= discriminator != 0;
20893 }
20894
20895 /* Handle DW_LNE_set_address. */
20896 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20897 {
20898 m_op_index = 0;
20899 address += baseaddr;
20900 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20901 }
20902
20903 /* Handle DW_LNS_advance_pc. */
20904 void handle_advance_pc (CORE_ADDR adjust);
20905
20906 /* Handle a special opcode. */
20907 void handle_special_opcode (unsigned char op_code);
20908
20909 /* Handle DW_LNS_advance_line. */
20910 void handle_advance_line (int line_delta)
20911 {
20912 advance_line (line_delta);
20913 }
20914
20915 /* Handle DW_LNS_set_file. */
20916 void handle_set_file (file_name_index file);
20917
20918 /* Handle DW_LNS_negate_stmt. */
20919 void handle_negate_stmt ()
20920 {
20921 m_is_stmt = !m_is_stmt;
20922 }
20923
20924 /* Handle DW_LNS_const_add_pc. */
20925 void handle_const_add_pc ();
20926
20927 /* Handle DW_LNS_fixed_advance_pc. */
20928 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20929 {
20930 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20931 m_op_index = 0;
20932 }
20933
20934 /* Handle DW_LNS_copy. */
20935 void handle_copy ()
20936 {
20937 record_line (false);
20938 m_discriminator = 0;
20939 }
20940
20941 /* Handle DW_LNE_end_sequence. */
20942 void handle_end_sequence ()
20943 {
20944 m_currently_recording_lines = true;
20945 }
20946
20947 private:
20948 /* Advance the line by LINE_DELTA. */
20949 void advance_line (int line_delta)
20950 {
20951 m_line += line_delta;
20952
20953 if (line_delta != 0)
20954 m_line_has_non_zero_discriminator = m_discriminator != 0;
20955 }
20956
20957 struct dwarf2_cu *m_cu;
20958
20959 gdbarch *m_gdbarch;
20960
20961 /* True if we're recording lines.
20962 Otherwise we're building partial symtabs and are just interested in
20963 finding include files mentioned by the line number program. */
20964 bool m_record_lines_p;
20965
20966 /* The line number header. */
20967 line_header *m_line_header;
20968
20969 /* These are part of the standard DWARF line number state machine,
20970 and initialized according to the DWARF spec. */
20971
20972 unsigned char m_op_index = 0;
20973 /* The line table index of the current file. */
20974 file_name_index m_file = 1;
20975 unsigned int m_line = 1;
20976
20977 /* These are initialized in the constructor. */
20978
20979 CORE_ADDR m_address;
20980 bool m_is_stmt;
20981 unsigned int m_discriminator;
20982
20983 /* Additional bits of state we need to track. */
20984
20985 /* The last file that we called dwarf2_start_subfile for.
20986 This is only used for TLLs. */
20987 unsigned int m_last_file = 0;
20988 /* The last file a line number was recorded for. */
20989 struct subfile *m_last_subfile = NULL;
20990
20991 /* The address of the last line entry. */
20992 CORE_ADDR m_last_address;
20993
20994 /* Set to true when a previous line at the same address (using
20995 m_last_address) had m_is_stmt true. This is reset to false when a
20996 line entry at a new address (m_address different to m_last_address) is
20997 processed. */
20998 bool m_stmt_at_address = false;
20999
21000 /* When true, record the lines we decode. */
21001 bool m_currently_recording_lines = false;
21002
21003 /* The last line number that was recorded, used to coalesce
21004 consecutive entries for the same line. This can happen, for
21005 example, when discriminators are present. PR 17276. */
21006 unsigned int m_last_line = 0;
21007 bool m_line_has_non_zero_discriminator = false;
21008 };
21009
21010 void
21011 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21012 {
21013 CORE_ADDR addr_adj = (((m_op_index + adjust)
21014 / m_line_header->maximum_ops_per_instruction)
21015 * m_line_header->minimum_instruction_length);
21016 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21017 m_op_index = ((m_op_index + adjust)
21018 % m_line_header->maximum_ops_per_instruction);
21019 }
21020
21021 void
21022 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21023 {
21024 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21025 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21026 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21027 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21028 / m_line_header->maximum_ops_per_instruction)
21029 * m_line_header->minimum_instruction_length);
21030 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21031 m_op_index = ((m_op_index + adj_opcode_d)
21032 % m_line_header->maximum_ops_per_instruction);
21033
21034 int line_delta = m_line_header->line_base + adj_opcode_r;
21035 advance_line (line_delta);
21036 record_line (false);
21037 m_discriminator = 0;
21038 }
21039
21040 void
21041 lnp_state_machine::handle_set_file (file_name_index file)
21042 {
21043 m_file = file;
21044
21045 const file_entry *fe = current_file ();
21046 if (fe == NULL)
21047 dwarf2_debug_line_missing_file_complaint ();
21048 else if (m_record_lines_p)
21049 {
21050 const char *dir = fe->include_dir (m_line_header);
21051
21052 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21053 m_line_has_non_zero_discriminator = m_discriminator != 0;
21054 dwarf2_start_subfile (m_cu, fe->name, dir);
21055 }
21056 }
21057
21058 void
21059 lnp_state_machine::handle_const_add_pc ()
21060 {
21061 CORE_ADDR adjust
21062 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21063
21064 CORE_ADDR addr_adj
21065 = (((m_op_index + adjust)
21066 / m_line_header->maximum_ops_per_instruction)
21067 * m_line_header->minimum_instruction_length);
21068
21069 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21070 m_op_index = ((m_op_index + adjust)
21071 % m_line_header->maximum_ops_per_instruction);
21072 }
21073
21074 /* Return non-zero if we should add LINE to the line number table.
21075 LINE is the line to add, LAST_LINE is the last line that was added,
21076 LAST_SUBFILE is the subfile for LAST_LINE.
21077 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21078 had a non-zero discriminator.
21079
21080 We have to be careful in the presence of discriminators.
21081 E.g., for this line:
21082
21083 for (i = 0; i < 100000; i++);
21084
21085 clang can emit four line number entries for that one line,
21086 each with a different discriminator.
21087 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21088
21089 However, we want gdb to coalesce all four entries into one.
21090 Otherwise the user could stepi into the middle of the line and
21091 gdb would get confused about whether the pc really was in the
21092 middle of the line.
21093
21094 Things are further complicated by the fact that two consecutive
21095 line number entries for the same line is a heuristic used by gcc
21096 to denote the end of the prologue. So we can't just discard duplicate
21097 entries, we have to be selective about it. The heuristic we use is
21098 that we only collapse consecutive entries for the same line if at least
21099 one of those entries has a non-zero discriminator. PR 17276.
21100
21101 Note: Addresses in the line number state machine can never go backwards
21102 within one sequence, thus this coalescing is ok. */
21103
21104 static int
21105 dwarf_record_line_p (struct dwarf2_cu *cu,
21106 unsigned int line, unsigned int last_line,
21107 int line_has_non_zero_discriminator,
21108 struct subfile *last_subfile)
21109 {
21110 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21111 return 1;
21112 if (line != last_line)
21113 return 1;
21114 /* Same line for the same file that we've seen already.
21115 As a last check, for pr 17276, only record the line if the line
21116 has never had a non-zero discriminator. */
21117 if (!line_has_non_zero_discriminator)
21118 return 1;
21119 return 0;
21120 }
21121
21122 /* Use the CU's builder to record line number LINE beginning at
21123 address ADDRESS in the line table of subfile SUBFILE. */
21124
21125 static void
21126 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21127 unsigned int line, CORE_ADDR address, bool is_stmt,
21128 struct dwarf2_cu *cu)
21129 {
21130 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21131
21132 if (dwarf_line_debug)
21133 {
21134 gdb_printf (gdb_stdlog,
21135 "Recording line %u, file %s, address %s\n",
21136 line, lbasename (subfile->name),
21137 paddress (gdbarch, address));
21138 }
21139
21140 if (cu != nullptr)
21141 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21142 }
21143
21144 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21145 Mark the end of a set of line number records.
21146 The arguments are the same as for dwarf_record_line_1.
21147 If SUBFILE is NULL the request is ignored. */
21148
21149 static void
21150 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21151 CORE_ADDR address, struct dwarf2_cu *cu)
21152 {
21153 if (subfile == NULL)
21154 return;
21155
21156 if (dwarf_line_debug)
21157 {
21158 gdb_printf (gdb_stdlog,
21159 "Finishing current line, file %s, address %s\n",
21160 lbasename (subfile->name),
21161 paddress (gdbarch, address));
21162 }
21163
21164 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21165 }
21166
21167 void
21168 lnp_state_machine::record_line (bool end_sequence)
21169 {
21170 if (dwarf_line_debug)
21171 {
21172 gdb_printf (gdb_stdlog,
21173 "Processing actual line %u: file %u,"
21174 " address %s, is_stmt %u, discrim %u%s\n",
21175 m_line, m_file,
21176 paddress (m_gdbarch, m_address),
21177 m_is_stmt, m_discriminator,
21178 (end_sequence ? "\t(end sequence)" : ""));
21179 }
21180
21181 file_entry *fe = current_file ();
21182
21183 if (fe == NULL)
21184 dwarf2_debug_line_missing_file_complaint ();
21185 /* For now we ignore lines not starting on an instruction boundary.
21186 But not when processing end_sequence for compatibility with the
21187 previous version of the code. */
21188 else if (m_op_index == 0 || end_sequence)
21189 {
21190 fe->included_p = true;
21191 if (m_record_lines_p)
21192 {
21193 /* When we switch files we insert an end maker in the first file,
21194 switch to the second file and add a new line entry. The
21195 problem is that the end marker inserted in the first file will
21196 discard any previous line entries at the same address. If the
21197 line entries in the first file are marked as is-stmt, while
21198 the new line in the second file is non-stmt, then this means
21199 the end marker will discard is-stmt lines so we can have a
21200 non-stmt line. This means that there are less addresses at
21201 which the user can insert a breakpoint.
21202
21203 To improve this we track the last address in m_last_address,
21204 and whether we have seen an is-stmt at this address. Then
21205 when switching files, if we have seen a stmt at the current
21206 address, and we are switching to create a non-stmt line, then
21207 discard the new line. */
21208 bool file_changed
21209 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21210 bool ignore_this_line
21211 = ((file_changed && !end_sequence && m_last_address == m_address
21212 && !m_is_stmt && m_stmt_at_address)
21213 || (!end_sequence && m_line == 0));
21214
21215 if ((file_changed && !ignore_this_line) || end_sequence)
21216 {
21217 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21218 m_currently_recording_lines ? m_cu : nullptr);
21219 }
21220
21221 if (!end_sequence && !ignore_this_line)
21222 {
21223 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21224
21225 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21226 m_line_has_non_zero_discriminator,
21227 m_last_subfile))
21228 {
21229 buildsym_compunit *builder = m_cu->get_builder ();
21230 dwarf_record_line_1 (m_gdbarch,
21231 builder->get_current_subfile (),
21232 m_line, m_address, is_stmt,
21233 m_currently_recording_lines ? m_cu : nullptr);
21234 }
21235 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21236 m_last_line = m_line;
21237 }
21238 }
21239 }
21240
21241 /* Track whether we have seen any m_is_stmt true at m_address in case we
21242 have multiple line table entries all at m_address. */
21243 if (m_last_address != m_address)
21244 {
21245 m_stmt_at_address = false;
21246 m_last_address = m_address;
21247 }
21248 m_stmt_at_address |= m_is_stmt;
21249 }
21250
21251 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21252 line_header *lh, bool record_lines_p)
21253 {
21254 m_cu = cu;
21255 m_gdbarch = arch;
21256 m_record_lines_p = record_lines_p;
21257 m_line_header = lh;
21258
21259 m_currently_recording_lines = true;
21260
21261 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21262 was a line entry for it so that the backend has a chance to adjust it
21263 and also record it in case it needs it. This is currently used by MIPS
21264 code, cf. `mips_adjust_dwarf2_line'. */
21265 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21266 m_is_stmt = lh->default_is_stmt;
21267 m_discriminator = 0;
21268
21269 m_last_address = m_address;
21270 m_stmt_at_address = false;
21271 }
21272
21273 void
21274 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21275 const gdb_byte *line_ptr,
21276 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21277 {
21278 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21279 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21280 located at 0x0. In this case, additionally check that if
21281 ADDRESS < UNRELOCATED_LOWPC. */
21282
21283 if ((address == 0 && address < unrelocated_lowpc)
21284 || address == (CORE_ADDR) -1)
21285 {
21286 /* This line table is for a function which has been
21287 GCd by the linker. Ignore it. PR gdb/12528 */
21288
21289 struct objfile *objfile = cu->per_objfile->objfile;
21290 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21291
21292 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21293 line_offset, objfile_name (objfile));
21294 m_currently_recording_lines = false;
21295 /* Note: m_currently_recording_lines is left as false until we see
21296 DW_LNE_end_sequence. */
21297 }
21298 }
21299
21300 /* Subroutine of dwarf_decode_lines to simplify it.
21301 Process the line number information in LH.
21302 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21303 program in order to set included_p for every referenced header. */
21304
21305 static void
21306 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21307 const int decode_for_pst_p, CORE_ADDR lowpc)
21308 {
21309 const gdb_byte *line_ptr, *extended_end;
21310 const gdb_byte *line_end;
21311 unsigned int bytes_read, extended_len;
21312 unsigned char op_code, extended_op;
21313 CORE_ADDR baseaddr;
21314 struct objfile *objfile = cu->per_objfile->objfile;
21315 bfd *abfd = objfile->obfd;
21316 struct gdbarch *gdbarch = objfile->arch ();
21317 /* True if we're recording line info (as opposed to building partial
21318 symtabs and just interested in finding include files mentioned by
21319 the line number program). */
21320 bool record_lines_p = !decode_for_pst_p;
21321
21322 baseaddr = objfile->text_section_offset ();
21323
21324 line_ptr = lh->statement_program_start;
21325 line_end = lh->statement_program_end;
21326
21327 /* Read the statement sequences until there's nothing left. */
21328 while (line_ptr < line_end)
21329 {
21330 /* The DWARF line number program state machine. Reset the state
21331 machine at the start of each sequence. */
21332 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21333 bool end_sequence = false;
21334
21335 if (record_lines_p)
21336 {
21337 /* Start a subfile for the current file of the state
21338 machine. */
21339 const file_entry *fe = state_machine.current_file ();
21340
21341 if (fe != NULL)
21342 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21343 }
21344
21345 /* Decode the table. */
21346 while (line_ptr < line_end && !end_sequence)
21347 {
21348 op_code = read_1_byte (abfd, line_ptr);
21349 line_ptr += 1;
21350
21351 if (op_code >= lh->opcode_base)
21352 {
21353 /* Special opcode. */
21354 state_machine.handle_special_opcode (op_code);
21355 }
21356 else switch (op_code)
21357 {
21358 case DW_LNS_extended_op:
21359 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21360 &bytes_read);
21361 line_ptr += bytes_read;
21362 extended_end = line_ptr + extended_len;
21363 extended_op = read_1_byte (abfd, line_ptr);
21364 line_ptr += 1;
21365 if (DW_LNE_lo_user <= extended_op
21366 && extended_op <= DW_LNE_hi_user)
21367 {
21368 /* Vendor extension, ignore. */
21369 line_ptr = extended_end;
21370 break;
21371 }
21372 switch (extended_op)
21373 {
21374 case DW_LNE_end_sequence:
21375 state_machine.handle_end_sequence ();
21376 end_sequence = true;
21377 break;
21378 case DW_LNE_set_address:
21379 {
21380 CORE_ADDR address
21381 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21382 line_ptr += bytes_read;
21383
21384 state_machine.check_line_address (cu, line_ptr,
21385 lowpc - baseaddr, address);
21386 state_machine.handle_set_address (baseaddr, address);
21387 }
21388 break;
21389 case DW_LNE_define_file:
21390 {
21391 const char *cur_file;
21392 unsigned int mod_time, length;
21393 dir_index dindex;
21394
21395 cur_file = read_direct_string (abfd, line_ptr,
21396 &bytes_read);
21397 line_ptr += bytes_read;
21398 dindex = (dir_index)
21399 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21400 line_ptr += bytes_read;
21401 mod_time =
21402 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21403 line_ptr += bytes_read;
21404 length =
21405 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21406 line_ptr += bytes_read;
21407 lh->add_file_name (cur_file, dindex, mod_time, length);
21408 }
21409 break;
21410 case DW_LNE_set_discriminator:
21411 {
21412 /* The discriminator is not interesting to the
21413 debugger; just ignore it. We still need to
21414 check its value though:
21415 if there are consecutive entries for the same
21416 (non-prologue) line we want to coalesce them.
21417 PR 17276. */
21418 unsigned int discr
21419 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21420 line_ptr += bytes_read;
21421
21422 state_machine.handle_set_discriminator (discr);
21423 }
21424 break;
21425 default:
21426 complaint (_("mangled .debug_line section"));
21427 return;
21428 }
21429 /* Make sure that we parsed the extended op correctly. If e.g.
21430 we expected a different address size than the producer used,
21431 we may have read the wrong number of bytes. */
21432 if (line_ptr != extended_end)
21433 {
21434 complaint (_("mangled .debug_line section"));
21435 return;
21436 }
21437 break;
21438 case DW_LNS_copy:
21439 state_machine.handle_copy ();
21440 break;
21441 case DW_LNS_advance_pc:
21442 {
21443 CORE_ADDR adjust
21444 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21445 line_ptr += bytes_read;
21446
21447 state_machine.handle_advance_pc (adjust);
21448 }
21449 break;
21450 case DW_LNS_advance_line:
21451 {
21452 int line_delta
21453 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21454 line_ptr += bytes_read;
21455
21456 state_machine.handle_advance_line (line_delta);
21457 }
21458 break;
21459 case DW_LNS_set_file:
21460 {
21461 file_name_index file
21462 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21463 &bytes_read);
21464 line_ptr += bytes_read;
21465
21466 state_machine.handle_set_file (file);
21467 }
21468 break;
21469 case DW_LNS_set_column:
21470 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21471 line_ptr += bytes_read;
21472 break;
21473 case DW_LNS_negate_stmt:
21474 state_machine.handle_negate_stmt ();
21475 break;
21476 case DW_LNS_set_basic_block:
21477 break;
21478 /* Add to the address register of the state machine the
21479 address increment value corresponding to special opcode
21480 255. I.e., this value is scaled by the minimum
21481 instruction length since special opcode 255 would have
21482 scaled the increment. */
21483 case DW_LNS_const_add_pc:
21484 state_machine.handle_const_add_pc ();
21485 break;
21486 case DW_LNS_fixed_advance_pc:
21487 {
21488 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21489 line_ptr += 2;
21490
21491 state_machine.handle_fixed_advance_pc (addr_adj);
21492 }
21493 break;
21494 default:
21495 {
21496 /* Unknown standard opcode, ignore it. */
21497 int i;
21498
21499 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21500 {
21501 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21502 line_ptr += bytes_read;
21503 }
21504 }
21505 }
21506 }
21507
21508 if (!end_sequence)
21509 dwarf2_debug_line_missing_end_sequence_complaint ();
21510
21511 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21512 in which case we still finish recording the last line). */
21513 state_machine.record_line (true);
21514 }
21515 }
21516
21517 /* Decode the Line Number Program (LNP) for the given line_header
21518 structure and CU. The actual information extracted and the type
21519 of structures created from the LNP depends on the value of PST.
21520
21521 1. If PST is NULL, then this procedure uses the data from the program
21522 to create all necessary symbol tables, and their linetables.
21523
21524 2. If PST is not NULL, this procedure reads the program to determine
21525 the list of files included by the unit represented by PST, and
21526 builds all the associated partial symbol tables.
21527
21528 FND holds the CU file name and directory, if known.
21529 It is used for relative paths in the line table.
21530
21531 NOTE: It is important that psymtabs have the same file name (via
21532 strcmp) as the corresponding symtab. Since the directory is not
21533 used in the name of the symtab we don't use it in the name of the
21534 psymtabs we create. E.g. expand_line_sal requires this when
21535 finding psymtabs to expand. A good testcase for this is
21536 mb-inline.exp.
21537
21538 LOWPC is the lowest address in CU (or 0 if not known).
21539
21540 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21541 for its PC<->lines mapping information. Otherwise only the filename
21542 table is read in. */
21543
21544 static void
21545 dwarf_decode_lines (struct line_header *lh, const file_and_directory &fnd,
21546 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21547 CORE_ADDR lowpc, int decode_mapping)
21548 {
21549 struct objfile *objfile = cu->per_objfile->objfile;
21550 const int decode_for_pst_p = (pst != NULL);
21551
21552 if (decode_mapping)
21553 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21554
21555 if (decode_for_pst_p)
21556 {
21557 /* Now that we're done scanning the Line Header Program, we can
21558 create the psymtab of each included file. */
21559 for (auto &file_entry : lh->file_names ())
21560 if (file_entry.included_p)
21561 {
21562 gdb::unique_xmalloc_ptr<char> name_holder;
21563 const char *include_name =
21564 compute_include_file_name (lh, file_entry, fnd, &name_holder);
21565 if (include_name != NULL)
21566 dwarf2_create_include_psymtab
21567 (cu->per_objfile->per_bfd, include_name, pst,
21568 cu->per_objfile->per_bfd->partial_symtabs.get (),
21569 objfile->per_bfd);
21570 }
21571 }
21572 else
21573 {
21574 /* Make sure a symtab is created for every file, even files
21575 which contain only variables (i.e. no code with associated
21576 line numbers). */
21577 buildsym_compunit *builder = cu->get_builder ();
21578 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21579
21580 for (auto &fe : lh->file_names ())
21581 {
21582 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21583 if (builder->get_current_subfile ()->symtab == NULL)
21584 {
21585 builder->get_current_subfile ()->symtab
21586 = allocate_symtab (cust,
21587 builder->get_current_subfile ()->name);
21588 }
21589 fe.symtab = builder->get_current_subfile ()->symtab;
21590 }
21591 }
21592 }
21593
21594 /* Start a subfile for DWARF. FILENAME is the name of the file and
21595 DIRNAME the name of the source directory which contains FILENAME
21596 or NULL if not known.
21597 This routine tries to keep line numbers from identical absolute and
21598 relative file names in a common subfile.
21599
21600 Using the `list' example from the GDB testsuite, which resides in
21601 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21602 of /srcdir/list0.c yields the following debugging information for list0.c:
21603
21604 DW_AT_name: /srcdir/list0.c
21605 DW_AT_comp_dir: /compdir
21606 files.files[0].name: list0.h
21607 files.files[0].dir: /srcdir
21608 files.files[1].name: list0.c
21609 files.files[1].dir: /srcdir
21610
21611 The line number information for list0.c has to end up in a single
21612 subfile, so that `break /srcdir/list0.c:1' works as expected.
21613 start_subfile will ensure that this happens provided that we pass the
21614 concatenation of files.files[1].dir and files.files[1].name as the
21615 subfile's name. */
21616
21617 static void
21618 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21619 const char *dirname)
21620 {
21621 gdb::unique_xmalloc_ptr<char> copy;
21622
21623 /* In order not to lose the line information directory,
21624 we concatenate it to the filename when it makes sense.
21625 Note that the Dwarf3 standard says (speaking of filenames in line
21626 information): ``The directory index is ignored for file names
21627 that represent full path names''. Thus ignoring dirname in the
21628 `else' branch below isn't an issue. */
21629
21630 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21631 {
21632 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21633 filename = copy.get ();
21634 }
21635
21636 cu->get_builder ()->start_subfile (filename);
21637 }
21638
21639 static void
21640 var_decode_location (struct attribute *attr, struct symbol *sym,
21641 struct dwarf2_cu *cu)
21642 {
21643 struct objfile *objfile = cu->per_objfile->objfile;
21644 struct comp_unit_head *cu_header = &cu->header;
21645
21646 /* NOTE drow/2003-01-30: There used to be a comment and some special
21647 code here to turn a symbol with DW_AT_external and a
21648 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21649 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21650 with some versions of binutils) where shared libraries could have
21651 relocations against symbols in their debug information - the
21652 minimal symbol would have the right address, but the debug info
21653 would not. It's no longer necessary, because we will explicitly
21654 apply relocations when we read in the debug information now. */
21655
21656 /* A DW_AT_location attribute with no contents indicates that a
21657 variable has been optimized away. */
21658 if (attr->form_is_block () && attr->as_block ()->size == 0)
21659 {
21660 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21661 return;
21662 }
21663
21664 /* Handle one degenerate form of location expression specially, to
21665 preserve GDB's previous behavior when section offsets are
21666 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21667 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21668
21669 if (attr->form_is_block ())
21670 {
21671 struct dwarf_block *block = attr->as_block ();
21672
21673 if ((block->data[0] == DW_OP_addr
21674 && block->size == 1 + cu_header->addr_size)
21675 || ((block->data[0] == DW_OP_GNU_addr_index
21676 || block->data[0] == DW_OP_addrx)
21677 && (block->size
21678 == 1 + leb128_size (&block->data[1]))))
21679 {
21680 unsigned int dummy;
21681
21682 if (block->data[0] == DW_OP_addr)
21683 SET_SYMBOL_VALUE_ADDRESS
21684 (sym, cu->header.read_address (objfile->obfd,
21685 block->data + 1,
21686 &dummy));
21687 else
21688 SET_SYMBOL_VALUE_ADDRESS
21689 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21690 &dummy));
21691 sym->set_aclass_index (LOC_STATIC);
21692 fixup_symbol_section (sym, objfile);
21693 SET_SYMBOL_VALUE_ADDRESS
21694 (sym,
21695 SYMBOL_VALUE_ADDRESS (sym)
21696 + objfile->section_offsets[sym->section_index ()]);
21697 return;
21698 }
21699 }
21700
21701 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21702 expression evaluator, and use LOC_COMPUTED only when necessary
21703 (i.e. when the value of a register or memory location is
21704 referenced, or a thread-local block, etc.). Then again, it might
21705 not be worthwhile. I'm assuming that it isn't unless performance
21706 or memory numbers show me otherwise. */
21707
21708 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21709
21710 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21711 cu->has_loclist = true;
21712 }
21713
21714 /* Given a pointer to a DWARF information entry, figure out if we need
21715 to make a symbol table entry for it, and if so, create a new entry
21716 and return a pointer to it.
21717 If TYPE is NULL, determine symbol type from the die, otherwise
21718 used the passed type.
21719 If SPACE is not NULL, use it to hold the new symbol. If it is
21720 NULL, allocate a new symbol on the objfile's obstack. */
21721
21722 static struct symbol *
21723 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21724 struct symbol *space)
21725 {
21726 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21727 struct objfile *objfile = per_objfile->objfile;
21728 struct gdbarch *gdbarch = objfile->arch ();
21729 struct symbol *sym = NULL;
21730 const char *name;
21731 struct attribute *attr = NULL;
21732 struct attribute *attr2 = NULL;
21733 CORE_ADDR baseaddr;
21734 struct pending **list_to_add = NULL;
21735
21736 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21737
21738 baseaddr = objfile->text_section_offset ();
21739
21740 name = dwarf2_name (die, cu);
21741 if (name)
21742 {
21743 int suppress_add = 0;
21744
21745 if (space)
21746 sym = space;
21747 else
21748 sym = new (&objfile->objfile_obstack) symbol;
21749 OBJSTAT (objfile, n_syms++);
21750
21751 /* Cache this symbol's name and the name's demangled form (if any). */
21752 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
21753 /* Fortran does not have mangling standard and the mangling does differ
21754 between gfortran, iFort etc. */
21755 const char *physname
21756 = (cu->per_cu->lang == language_fortran
21757 ? dwarf2_full_name (name, die, cu)
21758 : dwarf2_physname (name, die, cu));
21759 const char *linkagename = dw2_linkage_name (die, cu);
21760
21761 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
21762 sym->set_linkage_name (physname);
21763 else
21764 {
21765 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21766 sym->set_linkage_name (linkagename);
21767 }
21768
21769 /* Handle DW_AT_artificial. */
21770 attr = dwarf2_attr (die, DW_AT_artificial, cu);
21771 if (attr != nullptr)
21772 sym->artificial = attr->as_boolean ();
21773
21774 /* Default assumptions.
21775 Use the passed type or decode it from the die. */
21776 sym->set_domain (VAR_DOMAIN);
21777 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21778 if (type != NULL)
21779 sym->set_type (type);
21780 else
21781 sym->set_type (die_type (die, cu));
21782 attr = dwarf2_attr (die,
21783 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21784 cu);
21785 if (attr != nullptr)
21786 sym->set_line (attr->constant_value (0));
21787
21788 attr = dwarf2_attr (die,
21789 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21790 cu);
21791 if (attr != nullptr && attr->is_nonnegative ())
21792 {
21793 file_name_index file_index
21794 = (file_name_index) attr->as_nonnegative ();
21795 struct file_entry *fe;
21796
21797 if (cu->line_header != NULL)
21798 fe = cu->line_header->file_name_at (file_index);
21799 else
21800 fe = NULL;
21801
21802 if (fe == NULL)
21803 complaint (_("file index out of range"));
21804 else
21805 symbol_set_symtab (sym, fe->symtab);
21806 }
21807
21808 switch (die->tag)
21809 {
21810 case DW_TAG_label:
21811 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21812 if (attr != nullptr)
21813 {
21814 CORE_ADDR addr;
21815
21816 addr = attr->as_address ();
21817 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21818 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21819 sym->set_aclass_index (LOC_LABEL);
21820 }
21821 else
21822 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21823 sym->set_type (objfile_type (objfile)->builtin_core_addr);
21824 sym->set_domain (LABEL_DOMAIN);
21825 add_symbol_to_list (sym, cu->list_in_scope);
21826 break;
21827 case DW_TAG_subprogram:
21828 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21829 finish_block. */
21830 sym->set_aclass_index (LOC_BLOCK);
21831 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21832 if ((attr2 != nullptr && attr2->as_boolean ())
21833 || cu->per_cu->lang == language_ada
21834 || cu->per_cu->lang == language_fortran)
21835 {
21836 /* Subprograms marked external are stored as a global symbol.
21837 Ada and Fortran subprograms, whether marked external or
21838 not, are always stored as a global symbol, because we want
21839 to be able to access them globally. For instance, we want
21840 to be able to break on a nested subprogram without having
21841 to specify the context. */
21842 list_to_add = cu->get_builder ()->get_global_symbols ();
21843 }
21844 else
21845 {
21846 list_to_add = cu->list_in_scope;
21847 }
21848 break;
21849 case DW_TAG_inlined_subroutine:
21850 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21851 finish_block. */
21852 sym->set_aclass_index (LOC_BLOCK);
21853 sym->set_is_inlined (1);
21854 list_to_add = cu->list_in_scope;
21855 break;
21856 case DW_TAG_template_value_param:
21857 suppress_add = 1;
21858 /* Fall through. */
21859 case DW_TAG_constant:
21860 case DW_TAG_variable:
21861 case DW_TAG_member:
21862 /* Compilation with minimal debug info may result in
21863 variables with missing type entries. Change the
21864 misleading `void' type to something sensible. */
21865 if (sym->type ()->code () == TYPE_CODE_VOID)
21866 sym->set_type (objfile_type (objfile)->builtin_int);
21867
21868 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21869 /* In the case of DW_TAG_member, we should only be called for
21870 static const members. */
21871 if (die->tag == DW_TAG_member)
21872 {
21873 /* dwarf2_add_field uses die_is_declaration,
21874 so we do the same. */
21875 gdb_assert (die_is_declaration (die, cu));
21876 gdb_assert (attr);
21877 }
21878 if (attr != nullptr)
21879 {
21880 dwarf2_const_value (attr, sym, cu);
21881 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21882 if (!suppress_add)
21883 {
21884 if (attr2 != nullptr && attr2->as_boolean ())
21885 list_to_add = cu->get_builder ()->get_global_symbols ();
21886 else
21887 list_to_add = cu->list_in_scope;
21888 }
21889 break;
21890 }
21891 attr = dwarf2_attr (die, DW_AT_location, cu);
21892 if (attr != nullptr)
21893 {
21894 var_decode_location (attr, sym, cu);
21895 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21896
21897 /* Fortran explicitly imports any global symbols to the local
21898 scope by DW_TAG_common_block. */
21899 if (cu->per_cu->lang == language_fortran && die->parent
21900 && die->parent->tag == DW_TAG_common_block)
21901 attr2 = NULL;
21902
21903 if (sym->aclass () == LOC_STATIC
21904 && SYMBOL_VALUE_ADDRESS (sym) == 0
21905 && !per_objfile->per_bfd->has_section_at_zero)
21906 {
21907 /* When a static variable is eliminated by the linker,
21908 the corresponding debug information is not stripped
21909 out, but the variable address is set to null;
21910 do not add such variables into symbol table. */
21911 }
21912 else if (attr2 != nullptr && attr2->as_boolean ())
21913 {
21914 if (sym->aclass () == LOC_STATIC
21915 && (objfile->flags & OBJF_MAINLINE) == 0
21916 && per_objfile->per_bfd->can_copy)
21917 {
21918 /* A global static variable might be subject to
21919 copy relocation. We first check for a local
21920 minsym, though, because maybe the symbol was
21921 marked hidden, in which case this would not
21922 apply. */
21923 bound_minimal_symbol found
21924 = (lookup_minimal_symbol_linkage
21925 (sym->linkage_name (), objfile));
21926 if (found.minsym != nullptr)
21927 sym->maybe_copied = 1;
21928 }
21929
21930 /* A variable with DW_AT_external is never static,
21931 but it may be block-scoped. */
21932 list_to_add
21933 = ((cu->list_in_scope
21934 == cu->get_builder ()->get_file_symbols ())
21935 ? cu->get_builder ()->get_global_symbols ()
21936 : cu->list_in_scope);
21937 }
21938 else
21939 list_to_add = cu->list_in_scope;
21940 }
21941 else
21942 {
21943 /* We do not know the address of this symbol.
21944 If it is an external symbol and we have type information
21945 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21946 The address of the variable will then be determined from
21947 the minimal symbol table whenever the variable is
21948 referenced. */
21949 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21950
21951 /* Fortran explicitly imports any global symbols to the local
21952 scope by DW_TAG_common_block. */
21953 if (cu->per_cu->lang == language_fortran && die->parent
21954 && die->parent->tag == DW_TAG_common_block)
21955 {
21956 /* SYMBOL_CLASS doesn't matter here because
21957 read_common_block is going to reset it. */
21958 if (!suppress_add)
21959 list_to_add = cu->list_in_scope;
21960 }
21961 else if (attr2 != nullptr && attr2->as_boolean ()
21962 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21963 {
21964 /* A variable with DW_AT_external is never static, but it
21965 may be block-scoped. */
21966 list_to_add
21967 = ((cu->list_in_scope
21968 == cu->get_builder ()->get_file_symbols ())
21969 ? cu->get_builder ()->get_global_symbols ()
21970 : cu->list_in_scope);
21971
21972 sym->set_aclass_index (LOC_UNRESOLVED);
21973 }
21974 else if (!die_is_declaration (die, cu))
21975 {
21976 /* Use the default LOC_OPTIMIZED_OUT class. */
21977 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
21978 if (!suppress_add)
21979 list_to_add = cu->list_in_scope;
21980 }
21981 }
21982 break;
21983 case DW_TAG_formal_parameter:
21984 {
21985 /* If we are inside a function, mark this as an argument. If
21986 not, we might be looking at an argument to an inlined function
21987 when we do not have enough information to show inlined frames;
21988 pretend it's a local variable in that case so that the user can
21989 still see it. */
21990 struct context_stack *curr
21991 = cu->get_builder ()->get_current_context_stack ();
21992 if (curr != nullptr && curr->name != nullptr)
21993 sym->set_is_argument (1);
21994 attr = dwarf2_attr (die, DW_AT_location, cu);
21995 if (attr != nullptr)
21996 {
21997 var_decode_location (attr, sym, cu);
21998 }
21999 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22000 if (attr != nullptr)
22001 {
22002 dwarf2_const_value (attr, sym, cu);
22003 }
22004
22005 list_to_add = cu->list_in_scope;
22006 }
22007 break;
22008 case DW_TAG_unspecified_parameters:
22009 /* From varargs functions; gdb doesn't seem to have any
22010 interest in this information, so just ignore it for now.
22011 (FIXME?) */
22012 break;
22013 case DW_TAG_template_type_param:
22014 suppress_add = 1;
22015 /* Fall through. */
22016 case DW_TAG_class_type:
22017 case DW_TAG_interface_type:
22018 case DW_TAG_structure_type:
22019 case DW_TAG_union_type:
22020 case DW_TAG_set_type:
22021 case DW_TAG_enumeration_type:
22022 case DW_TAG_namelist:
22023 if (die->tag == DW_TAG_namelist)
22024 {
22025 sym->set_aclass_index (LOC_STATIC);
22026 sym->set_domain (VAR_DOMAIN);
22027 }
22028 else
22029 {
22030 sym->set_aclass_index (LOC_TYPEDEF);
22031 sym->set_domain (STRUCT_DOMAIN);
22032 }
22033 {
22034 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22035 really ever be static objects: otherwise, if you try
22036 to, say, break of a class's method and you're in a file
22037 which doesn't mention that class, it won't work unless
22038 the check for all static symbols in lookup_symbol_aux
22039 saves you. See the OtherFileClass tests in
22040 gdb.c++/namespace.exp. */
22041
22042 if (!suppress_add)
22043 {
22044 buildsym_compunit *builder = cu->get_builder ();
22045 list_to_add
22046 = (cu->list_in_scope == builder->get_file_symbols ()
22047 && cu->per_cu->lang == language_cplus
22048 ? builder->get_global_symbols ()
22049 : cu->list_in_scope);
22050
22051 /* The semantics of C++ state that "struct foo {
22052 ... }" also defines a typedef for "foo". */
22053 if (cu->per_cu->lang == language_cplus
22054 || cu->per_cu->lang == language_ada
22055 || cu->per_cu->lang == language_d
22056 || cu->per_cu->lang == language_rust)
22057 {
22058 /* The symbol's name is already allocated along
22059 with this objfile, so we don't need to
22060 duplicate it for the type. */
22061 if (sym->type ()->name () == 0)
22062 sym->type ()->set_name (sym->search_name ());
22063 }
22064 }
22065 }
22066 break;
22067 case DW_TAG_typedef:
22068 sym->set_aclass_index (LOC_TYPEDEF);
22069 sym->set_domain (VAR_DOMAIN);
22070 list_to_add = cu->list_in_scope;
22071 break;
22072 case DW_TAG_array_type:
22073 case DW_TAG_base_type:
22074 case DW_TAG_subrange_type:
22075 sym->set_aclass_index (LOC_TYPEDEF);
22076 sym->set_domain (VAR_DOMAIN);
22077 list_to_add = cu->list_in_scope;
22078 break;
22079 case DW_TAG_enumerator:
22080 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22081 if (attr != nullptr)
22082 {
22083 dwarf2_const_value (attr, sym, cu);
22084 }
22085 {
22086 /* NOTE: carlton/2003-11-10: See comment above in the
22087 DW_TAG_class_type, etc. block. */
22088
22089 list_to_add
22090 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22091 && cu->per_cu->lang == language_cplus
22092 ? cu->get_builder ()->get_global_symbols ()
22093 : cu->list_in_scope);
22094 }
22095 break;
22096 case DW_TAG_imported_declaration:
22097 case DW_TAG_namespace:
22098 sym->set_aclass_index (LOC_TYPEDEF);
22099 list_to_add = cu->get_builder ()->get_global_symbols ();
22100 break;
22101 case DW_TAG_module:
22102 sym->set_aclass_index (LOC_TYPEDEF);
22103 sym->set_domain (MODULE_DOMAIN);
22104 list_to_add = cu->get_builder ()->get_global_symbols ();
22105 break;
22106 case DW_TAG_common_block:
22107 sym->set_aclass_index (LOC_COMMON_BLOCK);
22108 sym->set_domain (COMMON_BLOCK_DOMAIN);
22109 add_symbol_to_list (sym, cu->list_in_scope);
22110 break;
22111 default:
22112 /* Not a tag we recognize. Hopefully we aren't processing
22113 trash data, but since we must specifically ignore things
22114 we don't recognize, there is nothing else we should do at
22115 this point. */
22116 complaint (_("unsupported tag: '%s'"),
22117 dwarf_tag_name (die->tag));
22118 break;
22119 }
22120
22121 if (suppress_add)
22122 {
22123 sym->hash_next = objfile->template_symbols;
22124 objfile->template_symbols = sym;
22125 list_to_add = NULL;
22126 }
22127
22128 if (list_to_add != NULL)
22129 add_symbol_to_list (sym, list_to_add);
22130
22131 /* For the benefit of old versions of GCC, check for anonymous
22132 namespaces based on the demangled name. */
22133 if (!cu->processing_has_namespace_info
22134 && cu->per_cu->lang == language_cplus)
22135 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22136 }
22137 return (sym);
22138 }
22139
22140 /* Given an attr with a DW_FORM_dataN value in host byte order,
22141 zero-extend it as appropriate for the symbol's type. The DWARF
22142 standard (v4) is not entirely clear about the meaning of using
22143 DW_FORM_dataN for a constant with a signed type, where the type is
22144 wider than the data. The conclusion of a discussion on the DWARF
22145 list was that this is unspecified. We choose to always zero-extend
22146 because that is the interpretation long in use by GCC. */
22147
22148 static gdb_byte *
22149 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22150 struct dwarf2_cu *cu, LONGEST *value, int bits)
22151 {
22152 struct objfile *objfile = cu->per_objfile->objfile;
22153 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22154 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22155 LONGEST l = attr->constant_value (0);
22156
22157 if (bits < sizeof (*value) * 8)
22158 {
22159 l &= ((LONGEST) 1 << bits) - 1;
22160 *value = l;
22161 }
22162 else if (bits == sizeof (*value) * 8)
22163 *value = l;
22164 else
22165 {
22166 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22167 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22168 return bytes;
22169 }
22170
22171 return NULL;
22172 }
22173
22174 /* Read a constant value from an attribute. Either set *VALUE, or if
22175 the value does not fit in *VALUE, set *BYTES - either already
22176 allocated on the objfile obstack, or newly allocated on OBSTACK,
22177 or, set *BATON, if we translated the constant to a location
22178 expression. */
22179
22180 static void
22181 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22182 const char *name, struct obstack *obstack,
22183 struct dwarf2_cu *cu,
22184 LONGEST *value, const gdb_byte **bytes,
22185 struct dwarf2_locexpr_baton **baton)
22186 {
22187 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22188 struct objfile *objfile = per_objfile->objfile;
22189 struct comp_unit_head *cu_header = &cu->header;
22190 struct dwarf_block *blk;
22191 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22192 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22193
22194 *value = 0;
22195 *bytes = NULL;
22196 *baton = NULL;
22197
22198 switch (attr->form)
22199 {
22200 case DW_FORM_addr:
22201 case DW_FORM_addrx:
22202 case DW_FORM_GNU_addr_index:
22203 {
22204 gdb_byte *data;
22205
22206 if (TYPE_LENGTH (type) != cu_header->addr_size)
22207 dwarf2_const_value_length_mismatch_complaint (name,
22208 cu_header->addr_size,
22209 TYPE_LENGTH (type));
22210 /* Symbols of this form are reasonably rare, so we just
22211 piggyback on the existing location code rather than writing
22212 a new implementation of symbol_computed_ops. */
22213 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22214 (*baton)->per_objfile = per_objfile;
22215 (*baton)->per_cu = cu->per_cu;
22216 gdb_assert ((*baton)->per_cu);
22217
22218 (*baton)->size = 2 + cu_header->addr_size;
22219 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22220 (*baton)->data = data;
22221
22222 data[0] = DW_OP_addr;
22223 store_unsigned_integer (&data[1], cu_header->addr_size,
22224 byte_order, attr->as_address ());
22225 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22226 }
22227 break;
22228 case DW_FORM_string:
22229 case DW_FORM_strp:
22230 case DW_FORM_strx:
22231 case DW_FORM_GNU_str_index:
22232 case DW_FORM_GNU_strp_alt:
22233 /* The string is already allocated on the objfile obstack, point
22234 directly to it. */
22235 *bytes = (const gdb_byte *) attr->as_string ();
22236 break;
22237 case DW_FORM_block1:
22238 case DW_FORM_block2:
22239 case DW_FORM_block4:
22240 case DW_FORM_block:
22241 case DW_FORM_exprloc:
22242 case DW_FORM_data16:
22243 blk = attr->as_block ();
22244 if (TYPE_LENGTH (type) != blk->size)
22245 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22246 TYPE_LENGTH (type));
22247 *bytes = blk->data;
22248 break;
22249
22250 /* The DW_AT_const_value attributes are supposed to carry the
22251 symbol's value "represented as it would be on the target
22252 architecture." By the time we get here, it's already been
22253 converted to host endianness, so we just need to sign- or
22254 zero-extend it as appropriate. */
22255 case DW_FORM_data1:
22256 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22257 break;
22258 case DW_FORM_data2:
22259 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22260 break;
22261 case DW_FORM_data4:
22262 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22263 break;
22264 case DW_FORM_data8:
22265 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22266 break;
22267
22268 case DW_FORM_sdata:
22269 case DW_FORM_implicit_const:
22270 *value = attr->as_signed ();
22271 break;
22272
22273 case DW_FORM_udata:
22274 *value = attr->as_unsigned ();
22275 break;
22276
22277 default:
22278 complaint (_("unsupported const value attribute form: '%s'"),
22279 dwarf_form_name (attr->form));
22280 *value = 0;
22281 break;
22282 }
22283 }
22284
22285
22286 /* Copy constant value from an attribute to a symbol. */
22287
22288 static void
22289 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22290 struct dwarf2_cu *cu)
22291 {
22292 struct objfile *objfile = cu->per_objfile->objfile;
22293 LONGEST value;
22294 const gdb_byte *bytes;
22295 struct dwarf2_locexpr_baton *baton;
22296
22297 dwarf2_const_value_attr (attr, sym->type (),
22298 sym->print_name (),
22299 &objfile->objfile_obstack, cu,
22300 &value, &bytes, &baton);
22301
22302 if (baton != NULL)
22303 {
22304 SYMBOL_LOCATION_BATON (sym) = baton;
22305 sym->set_aclass_index (dwarf2_locexpr_index);
22306 }
22307 else if (bytes != NULL)
22308 {
22309 SYMBOL_VALUE_BYTES (sym) = bytes;
22310 sym->set_aclass_index (LOC_CONST_BYTES);
22311 }
22312 else
22313 {
22314 SYMBOL_VALUE (sym) = value;
22315 sym->set_aclass_index (LOC_CONST);
22316 }
22317 }
22318
22319 /* Return the type of the die in question using its DW_AT_type attribute. */
22320
22321 static struct type *
22322 die_type (struct die_info *die, struct dwarf2_cu *cu)
22323 {
22324 struct attribute *type_attr;
22325
22326 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22327 if (!type_attr)
22328 {
22329 struct objfile *objfile = cu->per_objfile->objfile;
22330 /* A missing DW_AT_type represents a void type. */
22331 return objfile_type (objfile)->builtin_void;
22332 }
22333
22334 return lookup_die_type (die, type_attr, cu);
22335 }
22336
22337 /* True iff CU's producer generates GNAT Ada auxiliary information
22338 that allows to find parallel types through that information instead
22339 of having to do expensive parallel lookups by type name. */
22340
22341 static int
22342 need_gnat_info (struct dwarf2_cu *cu)
22343 {
22344 /* Assume that the Ada compiler was GNAT, which always produces
22345 the auxiliary information. */
22346 return (cu->per_cu->lang == language_ada);
22347 }
22348
22349 /* Return the auxiliary type of the die in question using its
22350 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22351 attribute is not present. */
22352
22353 static struct type *
22354 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22355 {
22356 struct attribute *type_attr;
22357
22358 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22359 if (!type_attr)
22360 return NULL;
22361
22362 return lookup_die_type (die, type_attr, cu);
22363 }
22364
22365 /* If DIE has a descriptive_type attribute, then set the TYPE's
22366 descriptive type accordingly. */
22367
22368 static void
22369 set_descriptive_type (struct type *type, struct die_info *die,
22370 struct dwarf2_cu *cu)
22371 {
22372 struct type *descriptive_type = die_descriptive_type (die, cu);
22373
22374 if (descriptive_type)
22375 {
22376 ALLOCATE_GNAT_AUX_TYPE (type);
22377 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22378 }
22379 }
22380
22381 /* Return the containing type of the die in question using its
22382 DW_AT_containing_type attribute. */
22383
22384 static struct type *
22385 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22386 {
22387 struct attribute *type_attr;
22388 struct objfile *objfile = cu->per_objfile->objfile;
22389
22390 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22391 if (!type_attr)
22392 error (_("Dwarf Error: Problem turning containing type into gdb type "
22393 "[in module %s]"), objfile_name (objfile));
22394
22395 return lookup_die_type (die, type_attr, cu);
22396 }
22397
22398 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22399
22400 static struct type *
22401 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22402 {
22403 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22404 struct objfile *objfile = per_objfile->objfile;
22405 char *saved;
22406
22407 std::string message
22408 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22409 objfile_name (objfile),
22410 sect_offset_str (cu->header.sect_off),
22411 sect_offset_str (die->sect_off));
22412 saved = obstack_strdup (&objfile->objfile_obstack, message);
22413
22414 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22415 }
22416
22417 /* Look up the type of DIE in CU using its type attribute ATTR.
22418 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22419 DW_AT_containing_type.
22420 If there is no type substitute an error marker. */
22421
22422 static struct type *
22423 lookup_die_type (struct die_info *die, const struct attribute *attr,
22424 struct dwarf2_cu *cu)
22425 {
22426 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22427 struct objfile *objfile = per_objfile->objfile;
22428 struct type *this_type;
22429
22430 gdb_assert (attr->name == DW_AT_type
22431 || attr->name == DW_AT_GNAT_descriptive_type
22432 || attr->name == DW_AT_containing_type);
22433
22434 /* First see if we have it cached. */
22435
22436 if (attr->form == DW_FORM_GNU_ref_alt)
22437 {
22438 struct dwarf2_per_cu_data *per_cu;
22439 sect_offset sect_off = attr->get_ref_die_offset ();
22440
22441 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22442 per_objfile->per_bfd);
22443 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22444 }
22445 else if (attr->form_is_ref ())
22446 {
22447 sect_offset sect_off = attr->get_ref_die_offset ();
22448
22449 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22450 }
22451 else if (attr->form == DW_FORM_ref_sig8)
22452 {
22453 ULONGEST signature = attr->as_signature ();
22454
22455 return get_signatured_type (die, signature, cu);
22456 }
22457 else
22458 {
22459 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22460 " at %s [in module %s]"),
22461 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22462 objfile_name (objfile));
22463 return build_error_marker_type (cu, die);
22464 }
22465
22466 /* If not cached we need to read it in. */
22467
22468 if (this_type == NULL)
22469 {
22470 struct die_info *type_die = NULL;
22471 struct dwarf2_cu *type_cu = cu;
22472
22473 if (attr->form_is_ref ())
22474 type_die = follow_die_ref (die, attr, &type_cu);
22475 if (type_die == NULL)
22476 return build_error_marker_type (cu, die);
22477 /* If we find the type now, it's probably because the type came
22478 from an inter-CU reference and the type's CU got expanded before
22479 ours. */
22480 this_type = read_type_die (type_die, type_cu);
22481 }
22482
22483 /* If we still don't have a type use an error marker. */
22484
22485 if (this_type == NULL)
22486 return build_error_marker_type (cu, die);
22487
22488 return this_type;
22489 }
22490
22491 /* Return the type in DIE, CU.
22492 Returns NULL for invalid types.
22493
22494 This first does a lookup in die_type_hash,
22495 and only reads the die in if necessary.
22496
22497 NOTE: This can be called when reading in partial or full symbols. */
22498
22499 static struct type *
22500 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22501 {
22502 struct type *this_type;
22503
22504 this_type = get_die_type (die, cu);
22505 if (this_type)
22506 return this_type;
22507
22508 return read_type_die_1 (die, cu);
22509 }
22510
22511 /* Read the type in DIE, CU.
22512 Returns NULL for invalid types. */
22513
22514 static struct type *
22515 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22516 {
22517 struct type *this_type = NULL;
22518
22519 switch (die->tag)
22520 {
22521 case DW_TAG_class_type:
22522 case DW_TAG_interface_type:
22523 case DW_TAG_structure_type:
22524 case DW_TAG_union_type:
22525 this_type = read_structure_type (die, cu);
22526 break;
22527 case DW_TAG_enumeration_type:
22528 this_type = read_enumeration_type (die, cu);
22529 break;
22530 case DW_TAG_subprogram:
22531 case DW_TAG_subroutine_type:
22532 case DW_TAG_inlined_subroutine:
22533 this_type = read_subroutine_type (die, cu);
22534 break;
22535 case DW_TAG_array_type:
22536 this_type = read_array_type (die, cu);
22537 break;
22538 case DW_TAG_set_type:
22539 this_type = read_set_type (die, cu);
22540 break;
22541 case DW_TAG_pointer_type:
22542 this_type = read_tag_pointer_type (die, cu);
22543 break;
22544 case DW_TAG_ptr_to_member_type:
22545 this_type = read_tag_ptr_to_member_type (die, cu);
22546 break;
22547 case DW_TAG_reference_type:
22548 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22549 break;
22550 case DW_TAG_rvalue_reference_type:
22551 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22552 break;
22553 case DW_TAG_const_type:
22554 this_type = read_tag_const_type (die, cu);
22555 break;
22556 case DW_TAG_volatile_type:
22557 this_type = read_tag_volatile_type (die, cu);
22558 break;
22559 case DW_TAG_restrict_type:
22560 this_type = read_tag_restrict_type (die, cu);
22561 break;
22562 case DW_TAG_string_type:
22563 this_type = read_tag_string_type (die, cu);
22564 break;
22565 case DW_TAG_typedef:
22566 this_type = read_typedef (die, cu);
22567 break;
22568 case DW_TAG_subrange_type:
22569 this_type = read_subrange_type (die, cu);
22570 break;
22571 case DW_TAG_base_type:
22572 this_type = read_base_type (die, cu);
22573 break;
22574 case DW_TAG_unspecified_type:
22575 this_type = read_unspecified_type (die, cu);
22576 break;
22577 case DW_TAG_namespace:
22578 this_type = read_namespace_type (die, cu);
22579 break;
22580 case DW_TAG_module:
22581 this_type = read_module_type (die, cu);
22582 break;
22583 case DW_TAG_atomic_type:
22584 this_type = read_tag_atomic_type (die, cu);
22585 break;
22586 default:
22587 complaint (_("unexpected tag in read_type_die: '%s'"),
22588 dwarf_tag_name (die->tag));
22589 break;
22590 }
22591
22592 return this_type;
22593 }
22594
22595 /* See if we can figure out if the class lives in a namespace. We do
22596 this by looking for a member function; its demangled name will
22597 contain namespace info, if there is any.
22598 Return the computed name or NULL.
22599 Space for the result is allocated on the objfile's obstack.
22600 This is the full-die version of guess_partial_die_structure_name.
22601 In this case we know DIE has no useful parent. */
22602
22603 static const char *
22604 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22605 {
22606 struct die_info *spec_die;
22607 struct dwarf2_cu *spec_cu;
22608 struct die_info *child;
22609 struct objfile *objfile = cu->per_objfile->objfile;
22610
22611 spec_cu = cu;
22612 spec_die = die_specification (die, &spec_cu);
22613 if (spec_die != NULL)
22614 {
22615 die = spec_die;
22616 cu = spec_cu;
22617 }
22618
22619 for (child = die->child;
22620 child != NULL;
22621 child = child->sibling)
22622 {
22623 if (child->tag == DW_TAG_subprogram)
22624 {
22625 const char *linkage_name = dw2_linkage_name (child, cu);
22626
22627 if (linkage_name != NULL)
22628 {
22629 gdb::unique_xmalloc_ptr<char> actual_name
22630 (cu->language_defn->class_name_from_physname (linkage_name));
22631 const char *name = NULL;
22632
22633 if (actual_name != NULL)
22634 {
22635 const char *die_name = dwarf2_name (die, cu);
22636
22637 if (die_name != NULL
22638 && strcmp (die_name, actual_name.get ()) != 0)
22639 {
22640 /* Strip off the class name from the full name.
22641 We want the prefix. */
22642 int die_name_len = strlen (die_name);
22643 int actual_name_len = strlen (actual_name.get ());
22644 const char *ptr = actual_name.get ();
22645
22646 /* Test for '::' as a sanity check. */
22647 if (actual_name_len > die_name_len + 2
22648 && ptr[actual_name_len - die_name_len - 1] == ':')
22649 name = obstack_strndup (
22650 &objfile->per_bfd->storage_obstack,
22651 ptr, actual_name_len - die_name_len - 2);
22652 }
22653 }
22654 return name;
22655 }
22656 }
22657 }
22658
22659 return NULL;
22660 }
22661
22662 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22663 prefix part in such case. See
22664 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22665
22666 static const char *
22667 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22668 {
22669 struct attribute *attr;
22670 const char *base;
22671
22672 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22673 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22674 return NULL;
22675
22676 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22677 return NULL;
22678
22679 attr = dw2_linkage_name_attr (die, cu);
22680 const char *attr_name = attr->as_string ();
22681 if (attr == NULL || attr_name == NULL)
22682 return NULL;
22683
22684 /* dwarf2_name had to be already called. */
22685 gdb_assert (attr->canonical_string_p ());
22686
22687 /* Strip the base name, keep any leading namespaces/classes. */
22688 base = strrchr (attr_name, ':');
22689 if (base == NULL || base == attr_name || base[-1] != ':')
22690 return "";
22691
22692 struct objfile *objfile = cu->per_objfile->objfile;
22693 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22694 attr_name,
22695 &base[-1] - attr_name);
22696 }
22697
22698 /* Return the name of the namespace/class that DIE is defined within,
22699 or "" if we can't tell. The caller should not xfree the result.
22700
22701 For example, if we're within the method foo() in the following
22702 code:
22703
22704 namespace N {
22705 class C {
22706 void foo () {
22707 }
22708 };
22709 }
22710
22711 then determine_prefix on foo's die will return "N::C". */
22712
22713 static const char *
22714 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22715 {
22716 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22717 struct die_info *parent, *spec_die;
22718 struct dwarf2_cu *spec_cu;
22719 struct type *parent_type;
22720 const char *retval;
22721
22722 if (cu->per_cu->lang != language_cplus
22723 && cu->per_cu->lang != language_fortran
22724 && cu->per_cu->lang != language_d
22725 && cu->per_cu->lang != language_rust)
22726 return "";
22727
22728 retval = anonymous_struct_prefix (die, cu);
22729 if (retval)
22730 return retval;
22731
22732 /* We have to be careful in the presence of DW_AT_specification.
22733 For example, with GCC 3.4, given the code
22734
22735 namespace N {
22736 void foo() {
22737 // Definition of N::foo.
22738 }
22739 }
22740
22741 then we'll have a tree of DIEs like this:
22742
22743 1: DW_TAG_compile_unit
22744 2: DW_TAG_namespace // N
22745 3: DW_TAG_subprogram // declaration of N::foo
22746 4: DW_TAG_subprogram // definition of N::foo
22747 DW_AT_specification // refers to die #3
22748
22749 Thus, when processing die #4, we have to pretend that we're in
22750 the context of its DW_AT_specification, namely the contex of die
22751 #3. */
22752 spec_cu = cu;
22753 spec_die = die_specification (die, &spec_cu);
22754 if (spec_die == NULL)
22755 parent = die->parent;
22756 else
22757 {
22758 parent = spec_die->parent;
22759 cu = spec_cu;
22760 }
22761
22762 if (parent == NULL)
22763 return "";
22764 else if (parent->building_fullname)
22765 {
22766 const char *name;
22767 const char *parent_name;
22768
22769 /* It has been seen on RealView 2.2 built binaries,
22770 DW_TAG_template_type_param types actually _defined_ as
22771 children of the parent class:
22772
22773 enum E {};
22774 template class <class Enum> Class{};
22775 Class<enum E> class_e;
22776
22777 1: DW_TAG_class_type (Class)
22778 2: DW_TAG_enumeration_type (E)
22779 3: DW_TAG_enumerator (enum1:0)
22780 3: DW_TAG_enumerator (enum2:1)
22781 ...
22782 2: DW_TAG_template_type_param
22783 DW_AT_type DW_FORM_ref_udata (E)
22784
22785 Besides being broken debug info, it can put GDB into an
22786 infinite loop. Consider:
22787
22788 When we're building the full name for Class<E>, we'll start
22789 at Class, and go look over its template type parameters,
22790 finding E. We'll then try to build the full name of E, and
22791 reach here. We're now trying to build the full name of E,
22792 and look over the parent DIE for containing scope. In the
22793 broken case, if we followed the parent DIE of E, we'd again
22794 find Class, and once again go look at its template type
22795 arguments, etc., etc. Simply don't consider such parent die
22796 as source-level parent of this die (it can't be, the language
22797 doesn't allow it), and break the loop here. */
22798 name = dwarf2_name (die, cu);
22799 parent_name = dwarf2_name (parent, cu);
22800 complaint (_("template param type '%s' defined within parent '%s'"),
22801 name ? name : "<unknown>",
22802 parent_name ? parent_name : "<unknown>");
22803 return "";
22804 }
22805 else
22806 switch (parent->tag)
22807 {
22808 case DW_TAG_namespace:
22809 parent_type = read_type_die (parent, cu);
22810 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22811 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22812 Work around this problem here. */
22813 if (cu->per_cu->lang == language_cplus
22814 && strcmp (parent_type->name (), "::") == 0)
22815 return "";
22816 /* We give a name to even anonymous namespaces. */
22817 return parent_type->name ();
22818 case DW_TAG_class_type:
22819 case DW_TAG_interface_type:
22820 case DW_TAG_structure_type:
22821 case DW_TAG_union_type:
22822 case DW_TAG_module:
22823 parent_type = read_type_die (parent, cu);
22824 if (parent_type->name () != NULL)
22825 return parent_type->name ();
22826 else
22827 /* An anonymous structure is only allowed non-static data
22828 members; no typedefs, no member functions, et cetera.
22829 So it does not need a prefix. */
22830 return "";
22831 case DW_TAG_compile_unit:
22832 case DW_TAG_partial_unit:
22833 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22834 if (cu->per_cu->lang == language_cplus
22835 && !per_objfile->per_bfd->types.empty ()
22836 && die->child != NULL
22837 && (die->tag == DW_TAG_class_type
22838 || die->tag == DW_TAG_structure_type
22839 || die->tag == DW_TAG_union_type))
22840 {
22841 const char *name = guess_full_die_structure_name (die, cu);
22842 if (name != NULL)
22843 return name;
22844 }
22845 return "";
22846 case DW_TAG_subprogram:
22847 /* Nested subroutines in Fortran get a prefix with the name
22848 of the parent's subroutine. */
22849 if (cu->per_cu->lang == language_fortran)
22850 {
22851 if ((die->tag == DW_TAG_subprogram)
22852 && (dwarf2_name (parent, cu) != NULL))
22853 return dwarf2_name (parent, cu);
22854 }
22855 return "";
22856 case DW_TAG_enumeration_type:
22857 parent_type = read_type_die (parent, cu);
22858 if (parent_type->is_declared_class ())
22859 {
22860 if (parent_type->name () != NULL)
22861 return parent_type->name ();
22862 return "";
22863 }
22864 /* Fall through. */
22865 default:
22866 return determine_prefix (parent, cu);
22867 }
22868 }
22869
22870 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22871 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22872 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22873 an obconcat, otherwise allocate storage for the result. The CU argument is
22874 used to determine the language and hence, the appropriate separator. */
22875
22876 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22877
22878 static char *
22879 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22880 int physname, struct dwarf2_cu *cu)
22881 {
22882 const char *lead = "";
22883 const char *sep;
22884
22885 if (suffix == NULL || suffix[0] == '\0'
22886 || prefix == NULL || prefix[0] == '\0')
22887 sep = "";
22888 else if (cu->per_cu->lang == language_d)
22889 {
22890 /* For D, the 'main' function could be defined in any module, but it
22891 should never be prefixed. */
22892 if (strcmp (suffix, "D main") == 0)
22893 {
22894 prefix = "";
22895 sep = "";
22896 }
22897 else
22898 sep = ".";
22899 }
22900 else if (cu->per_cu->lang == language_fortran && physname)
22901 {
22902 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22903 DW_AT_MIPS_linkage_name is preferred and used instead. */
22904
22905 lead = "__";
22906 sep = "_MOD_";
22907 }
22908 else
22909 sep = "::";
22910
22911 if (prefix == NULL)
22912 prefix = "";
22913 if (suffix == NULL)
22914 suffix = "";
22915
22916 if (obs == NULL)
22917 {
22918 char *retval
22919 = ((char *)
22920 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22921
22922 strcpy (retval, lead);
22923 strcat (retval, prefix);
22924 strcat (retval, sep);
22925 strcat (retval, suffix);
22926 return retval;
22927 }
22928 else
22929 {
22930 /* We have an obstack. */
22931 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22932 }
22933 }
22934
22935 /* Get name of a die, return NULL if not found. */
22936
22937 static const char *
22938 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22939 struct objfile *objfile)
22940 {
22941 if (name && cu->per_cu->lang == language_cplus)
22942 {
22943 gdb::unique_xmalloc_ptr<char> canon_name
22944 = cp_canonicalize_string (name);
22945
22946 if (canon_name != nullptr)
22947 name = objfile->intern (canon_name.get ());
22948 }
22949
22950 return name;
22951 }
22952
22953 /* Get name of a die, return NULL if not found.
22954 Anonymous namespaces are converted to their magic string. */
22955
22956 static const char *
22957 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22958 {
22959 struct attribute *attr;
22960 struct objfile *objfile = cu->per_objfile->objfile;
22961
22962 attr = dwarf2_attr (die, DW_AT_name, cu);
22963 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22964 if (attr_name == nullptr
22965 && die->tag != DW_TAG_namespace
22966 && die->tag != DW_TAG_class_type
22967 && die->tag != DW_TAG_interface_type
22968 && die->tag != DW_TAG_structure_type
22969 && die->tag != DW_TAG_namelist
22970 && die->tag != DW_TAG_union_type)
22971 return NULL;
22972
22973 switch (die->tag)
22974 {
22975 case DW_TAG_compile_unit:
22976 case DW_TAG_partial_unit:
22977 /* Compilation units have a DW_AT_name that is a filename, not
22978 a source language identifier. */
22979 case DW_TAG_enumeration_type:
22980 case DW_TAG_enumerator:
22981 /* These tags always have simple identifiers already; no need
22982 to canonicalize them. */
22983 return attr_name;
22984
22985 case DW_TAG_namespace:
22986 if (attr_name != nullptr)
22987 return attr_name;
22988 return CP_ANONYMOUS_NAMESPACE_STR;
22989
22990 case DW_TAG_class_type:
22991 case DW_TAG_interface_type:
22992 case DW_TAG_structure_type:
22993 case DW_TAG_union_type:
22994 case DW_TAG_namelist:
22995 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22996 structures or unions. These were of the form "._%d" in GCC 4.1,
22997 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22998 and GCC 4.4. We work around this problem by ignoring these. */
22999 if (attr_name != nullptr
23000 && (startswith (attr_name, "._")
23001 || startswith (attr_name, "<anonymous")))
23002 return NULL;
23003
23004 /* GCC might emit a nameless typedef that has a linkage name. See
23005 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23006 if (!attr || attr_name == NULL)
23007 {
23008 attr = dw2_linkage_name_attr (die, cu);
23009 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23010 if (attr == NULL || attr_name == NULL)
23011 return NULL;
23012
23013 /* Avoid demangling attr_name the second time on a second
23014 call for the same DIE. */
23015 if (!attr->canonical_string_p ())
23016 {
23017 gdb::unique_xmalloc_ptr<char> demangled
23018 (gdb_demangle (attr_name, DMGL_TYPES));
23019 if (demangled == nullptr)
23020 return nullptr;
23021
23022 attr->set_string_canonical (objfile->intern (demangled.get ()));
23023 attr_name = attr->as_string ();
23024 }
23025
23026 /* Strip any leading namespaces/classes, keep only the
23027 base name. DW_AT_name for named DIEs does not
23028 contain the prefixes. */
23029 const char *base = strrchr (attr_name, ':');
23030 if (base && base > attr_name && base[-1] == ':')
23031 return &base[1];
23032 else
23033 return attr_name;
23034 }
23035 break;
23036
23037 default:
23038 break;
23039 }
23040
23041 if (!attr->canonical_string_p ())
23042 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23043 objfile));
23044 return attr->as_string ();
23045 }
23046
23047 /* Return the die that this die in an extension of, or NULL if there
23048 is none. *EXT_CU is the CU containing DIE on input, and the CU
23049 containing the return value on output. */
23050
23051 static struct die_info *
23052 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23053 {
23054 struct attribute *attr;
23055
23056 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23057 if (attr == NULL)
23058 return NULL;
23059
23060 return follow_die_ref (die, attr, ext_cu);
23061 }
23062
23063 static void
23064 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23065 {
23066 unsigned int i;
23067
23068 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
23069 indent, "",
23070 dwarf_tag_name (die->tag), die->abbrev,
23071 sect_offset_str (die->sect_off));
23072
23073 if (die->parent != NULL)
23074 gdb_printf (f, "%*s parent at offset: %s\n",
23075 indent, "",
23076 sect_offset_str (die->parent->sect_off));
23077
23078 gdb_printf (f, "%*s has children: %s\n",
23079 indent, "",
23080 dwarf_bool_name (die->child != NULL));
23081
23082 gdb_printf (f, "%*s attributes:\n", indent, "");
23083
23084 for (i = 0; i < die->num_attrs; ++i)
23085 {
23086 gdb_printf (f, "%*s %s (%s) ",
23087 indent, "",
23088 dwarf_attr_name (die->attrs[i].name),
23089 dwarf_form_name (die->attrs[i].form));
23090
23091 switch (die->attrs[i].form)
23092 {
23093 case DW_FORM_addr:
23094 case DW_FORM_addrx:
23095 case DW_FORM_GNU_addr_index:
23096 gdb_printf (f, "address: ");
23097 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
23098 break;
23099 case DW_FORM_block2:
23100 case DW_FORM_block4:
23101 case DW_FORM_block:
23102 case DW_FORM_block1:
23103 gdb_printf (f, "block: size %s",
23104 pulongest (die->attrs[i].as_block ()->size));
23105 break;
23106 case DW_FORM_exprloc:
23107 gdb_printf (f, "expression: size %s",
23108 pulongest (die->attrs[i].as_block ()->size));
23109 break;
23110 case DW_FORM_data16:
23111 gdb_printf (f, "constant of 16 bytes");
23112 break;
23113 case DW_FORM_ref_addr:
23114 gdb_printf (f, "ref address: ");
23115 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
23116 break;
23117 case DW_FORM_GNU_ref_alt:
23118 gdb_printf (f, "alt ref address: ");
23119 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
23120 break;
23121 case DW_FORM_ref1:
23122 case DW_FORM_ref2:
23123 case DW_FORM_ref4:
23124 case DW_FORM_ref8:
23125 case DW_FORM_ref_udata:
23126 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
23127 (long) (die->attrs[i].as_unsigned ()));
23128 break;
23129 case DW_FORM_data1:
23130 case DW_FORM_data2:
23131 case DW_FORM_data4:
23132 case DW_FORM_data8:
23133 case DW_FORM_udata:
23134 gdb_printf (f, "constant: %s",
23135 pulongest (die->attrs[i].as_unsigned ()));
23136 break;
23137 case DW_FORM_sec_offset:
23138 gdb_printf (f, "section offset: %s",
23139 pulongest (die->attrs[i].as_unsigned ()));
23140 break;
23141 case DW_FORM_ref_sig8:
23142 gdb_printf (f, "signature: %s",
23143 hex_string (die->attrs[i].as_signature ()));
23144 break;
23145 case DW_FORM_string:
23146 case DW_FORM_strp:
23147 case DW_FORM_line_strp:
23148 case DW_FORM_strx:
23149 case DW_FORM_GNU_str_index:
23150 case DW_FORM_GNU_strp_alt:
23151 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
23152 die->attrs[i].as_string ()
23153 ? die->attrs[i].as_string () : "",
23154 die->attrs[i].canonical_string_p () ? "is" : "not");
23155 break;
23156 case DW_FORM_flag:
23157 if (die->attrs[i].as_boolean ())
23158 gdb_printf (f, "flag: TRUE");
23159 else
23160 gdb_printf (f, "flag: FALSE");
23161 break;
23162 case DW_FORM_flag_present:
23163 gdb_printf (f, "flag: TRUE");
23164 break;
23165 case DW_FORM_indirect:
23166 /* The reader will have reduced the indirect form to
23167 the "base form" so this form should not occur. */
23168 gdb_printf (f,
23169 "unexpected attribute form: DW_FORM_indirect");
23170 break;
23171 case DW_FORM_sdata:
23172 case DW_FORM_implicit_const:
23173 gdb_printf (f, "constant: %s",
23174 plongest (die->attrs[i].as_signed ()));
23175 break;
23176 default:
23177 gdb_printf (f, "unsupported attribute form: %d.",
23178 die->attrs[i].form);
23179 break;
23180 }
23181 gdb_printf (f, "\n");
23182 }
23183 }
23184
23185 static void
23186 dump_die_for_error (struct die_info *die)
23187 {
23188 dump_die_shallow (gdb_stderr, 0, die);
23189 }
23190
23191 static void
23192 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23193 {
23194 int indent = level * 4;
23195
23196 gdb_assert (die != NULL);
23197
23198 if (level >= max_level)
23199 return;
23200
23201 dump_die_shallow (f, indent, die);
23202
23203 if (die->child != NULL)
23204 {
23205 gdb_printf (f, "%*s Children:", indent, "");
23206 if (level + 1 < max_level)
23207 {
23208 gdb_printf (f, "\n");
23209 dump_die_1 (f, level + 1, max_level, die->child);
23210 }
23211 else
23212 {
23213 gdb_printf (f,
23214 " [not printed, max nesting level reached]\n");
23215 }
23216 }
23217
23218 if (die->sibling != NULL && level > 0)
23219 {
23220 dump_die_1 (f, level, max_level, die->sibling);
23221 }
23222 }
23223
23224 /* This is called from the pdie macro in gdbinit.in.
23225 It's not static so gcc will keep a copy callable from gdb. */
23226
23227 void
23228 dump_die (struct die_info *die, int max_level)
23229 {
23230 dump_die_1 (gdb_stdlog, 0, max_level, die);
23231 }
23232
23233 static void
23234 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23235 {
23236 void **slot;
23237
23238 slot = htab_find_slot_with_hash (cu->die_hash, die,
23239 to_underlying (die->sect_off),
23240 INSERT);
23241
23242 *slot = die;
23243 }
23244
23245 /* Follow reference or signature attribute ATTR of SRC_DIE.
23246 On entry *REF_CU is the CU of SRC_DIE.
23247 On exit *REF_CU is the CU of the result. */
23248
23249 static struct die_info *
23250 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23251 struct dwarf2_cu **ref_cu)
23252 {
23253 struct die_info *die;
23254
23255 if (attr->form_is_ref ())
23256 die = follow_die_ref (src_die, attr, ref_cu);
23257 else if (attr->form == DW_FORM_ref_sig8)
23258 die = follow_die_sig (src_die, attr, ref_cu);
23259 else
23260 {
23261 dump_die_for_error (src_die);
23262 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23263 objfile_name ((*ref_cu)->per_objfile->objfile));
23264 }
23265
23266 return die;
23267 }
23268
23269 /* Follow reference OFFSET.
23270 On entry *REF_CU is the CU of the source die referencing OFFSET.
23271 On exit *REF_CU is the CU of the result.
23272 Returns NULL if OFFSET is invalid. */
23273
23274 static struct die_info *
23275 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23276 struct dwarf2_cu **ref_cu)
23277 {
23278 struct die_info temp_die;
23279 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23280 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23281
23282 gdb_assert (cu->per_cu != NULL);
23283
23284 target_cu = cu;
23285
23286 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23287 "source CU contains target offset: %d",
23288 sect_offset_str (cu->per_cu->sect_off),
23289 sect_offset_str (sect_off),
23290 cu->header.offset_in_cu_p (sect_off));
23291
23292 if (cu->per_cu->is_debug_types)
23293 {
23294 /* .debug_types CUs cannot reference anything outside their CU.
23295 If they need to, they have to reference a signatured type via
23296 DW_FORM_ref_sig8. */
23297 if (!cu->header.offset_in_cu_p (sect_off))
23298 return NULL;
23299 }
23300 else if (offset_in_dwz != cu->per_cu->is_dwz
23301 || !cu->header.offset_in_cu_p (sect_off))
23302 {
23303 struct dwarf2_per_cu_data *per_cu;
23304
23305 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23306 per_objfile->per_bfd);
23307
23308 dwarf_read_debug_printf_v ("target CU offset: %s, "
23309 "target CU DIEs loaded: %d",
23310 sect_offset_str (per_cu->sect_off),
23311 per_objfile->get_cu (per_cu) != nullptr);
23312
23313 /* If necessary, add it to the queue and load its DIEs.
23314
23315 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23316 it doesn't mean they are currently loaded. Since we require them
23317 to be loaded, we must check for ourselves. */
23318 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
23319 || per_objfile->get_cu (per_cu) == nullptr)
23320 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23321 false, cu->per_cu->lang);
23322
23323 target_cu = per_objfile->get_cu (per_cu);
23324 gdb_assert (target_cu != nullptr);
23325 }
23326 else if (cu->dies == NULL)
23327 {
23328 /* We're loading full DIEs during partial symbol reading. */
23329 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23330 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23331 language_minimal);
23332 }
23333
23334 *ref_cu = target_cu;
23335 temp_die.sect_off = sect_off;
23336
23337 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23338 &temp_die,
23339 to_underlying (sect_off));
23340 }
23341
23342 /* Follow reference attribute ATTR of SRC_DIE.
23343 On entry *REF_CU is the CU of SRC_DIE.
23344 On exit *REF_CU is the CU of the result. */
23345
23346 static struct die_info *
23347 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23348 struct dwarf2_cu **ref_cu)
23349 {
23350 sect_offset sect_off = attr->get_ref_die_offset ();
23351 struct dwarf2_cu *cu = *ref_cu;
23352 struct die_info *die;
23353
23354 die = follow_die_offset (sect_off,
23355 (attr->form == DW_FORM_GNU_ref_alt
23356 || cu->per_cu->is_dwz),
23357 ref_cu);
23358 if (!die)
23359 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23360 "at %s [in module %s]"),
23361 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23362 objfile_name (cu->per_objfile->objfile));
23363
23364 return die;
23365 }
23366
23367 /* See read.h. */
23368
23369 struct dwarf2_locexpr_baton
23370 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23371 dwarf2_per_cu_data *per_cu,
23372 dwarf2_per_objfile *per_objfile,
23373 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23374 bool resolve_abstract_p)
23375 {
23376 struct die_info *die;
23377 struct attribute *attr;
23378 struct dwarf2_locexpr_baton retval;
23379 struct objfile *objfile = per_objfile->objfile;
23380
23381 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23382 if (cu == nullptr)
23383 cu = load_cu (per_cu, per_objfile, false);
23384
23385 if (cu == nullptr)
23386 {
23387 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23388 Instead just throw an error, not much else we can do. */
23389 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23390 sect_offset_str (sect_off), objfile_name (objfile));
23391 }
23392
23393 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23394 if (!die)
23395 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23396 sect_offset_str (sect_off), objfile_name (objfile));
23397
23398 attr = dwarf2_attr (die, DW_AT_location, cu);
23399 if (!attr && resolve_abstract_p
23400 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23401 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23402 {
23403 CORE_ADDR pc = get_frame_pc ();
23404 CORE_ADDR baseaddr = objfile->text_section_offset ();
23405 struct gdbarch *gdbarch = objfile->arch ();
23406
23407 for (const auto &cand_off
23408 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23409 {
23410 struct dwarf2_cu *cand_cu = cu;
23411 struct die_info *cand
23412 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23413 if (!cand
23414 || !cand->parent
23415 || cand->parent->tag != DW_TAG_subprogram)
23416 continue;
23417
23418 CORE_ADDR pc_low, pc_high;
23419 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23420 if (pc_low == ((CORE_ADDR) -1))
23421 continue;
23422 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23423 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23424 if (!(pc_low <= pc && pc < pc_high))
23425 continue;
23426
23427 die = cand;
23428 attr = dwarf2_attr (die, DW_AT_location, cu);
23429 break;
23430 }
23431 }
23432
23433 if (!attr)
23434 {
23435 /* DWARF: "If there is no such attribute, then there is no effect.".
23436 DATA is ignored if SIZE is 0. */
23437
23438 retval.data = NULL;
23439 retval.size = 0;
23440 }
23441 else if (attr->form_is_section_offset ())
23442 {
23443 struct dwarf2_loclist_baton loclist_baton;
23444 CORE_ADDR pc = get_frame_pc ();
23445 size_t size;
23446
23447 fill_in_loclist_baton (cu, &loclist_baton, attr);
23448
23449 retval.data = dwarf2_find_location_expression (&loclist_baton,
23450 &size, pc);
23451 retval.size = size;
23452 }
23453 else
23454 {
23455 if (!attr->form_is_block ())
23456 error (_("Dwarf Error: DIE at %s referenced in module %s "
23457 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23458 sect_offset_str (sect_off), objfile_name (objfile));
23459
23460 struct dwarf_block *block = attr->as_block ();
23461 retval.data = block->data;
23462 retval.size = block->size;
23463 }
23464 retval.per_objfile = per_objfile;
23465 retval.per_cu = cu->per_cu;
23466
23467 per_objfile->age_comp_units ();
23468
23469 return retval;
23470 }
23471
23472 /* See read.h. */
23473
23474 struct dwarf2_locexpr_baton
23475 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23476 dwarf2_per_cu_data *per_cu,
23477 dwarf2_per_objfile *per_objfile,
23478 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23479 {
23480 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23481
23482 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23483 get_frame_pc);
23484 }
23485
23486 /* Write a constant of a given type as target-ordered bytes into
23487 OBSTACK. */
23488
23489 static const gdb_byte *
23490 write_constant_as_bytes (struct obstack *obstack,
23491 enum bfd_endian byte_order,
23492 struct type *type,
23493 ULONGEST value,
23494 LONGEST *len)
23495 {
23496 gdb_byte *result;
23497
23498 *len = TYPE_LENGTH (type);
23499 result = (gdb_byte *) obstack_alloc (obstack, *len);
23500 store_unsigned_integer (result, *len, byte_order, value);
23501
23502 return result;
23503 }
23504
23505 /* See read.h. */
23506
23507 const gdb_byte *
23508 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23509 dwarf2_per_cu_data *per_cu,
23510 dwarf2_per_objfile *per_objfile,
23511 obstack *obstack,
23512 LONGEST *len)
23513 {
23514 struct die_info *die;
23515 struct attribute *attr;
23516 const gdb_byte *result = NULL;
23517 struct type *type;
23518 LONGEST value;
23519 enum bfd_endian byte_order;
23520 struct objfile *objfile = per_objfile->objfile;
23521
23522 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23523 if (cu == nullptr)
23524 cu = load_cu (per_cu, per_objfile, false);
23525
23526 if (cu == nullptr)
23527 {
23528 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23529 Instead just throw an error, not much else we can do. */
23530 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23531 sect_offset_str (sect_off), objfile_name (objfile));
23532 }
23533
23534 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23535 if (!die)
23536 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23537 sect_offset_str (sect_off), objfile_name (objfile));
23538
23539 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23540 if (attr == NULL)
23541 return NULL;
23542
23543 byte_order = (bfd_big_endian (objfile->obfd)
23544 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23545
23546 switch (attr->form)
23547 {
23548 case DW_FORM_addr:
23549 case DW_FORM_addrx:
23550 case DW_FORM_GNU_addr_index:
23551 {
23552 gdb_byte *tem;
23553
23554 *len = cu->header.addr_size;
23555 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23556 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23557 result = tem;
23558 }
23559 break;
23560 case DW_FORM_string:
23561 case DW_FORM_strp:
23562 case DW_FORM_strx:
23563 case DW_FORM_GNU_str_index:
23564 case DW_FORM_GNU_strp_alt:
23565 /* The string is already allocated on the objfile obstack, point
23566 directly to it. */
23567 {
23568 const char *attr_name = attr->as_string ();
23569 result = (const gdb_byte *) attr_name;
23570 *len = strlen (attr_name);
23571 }
23572 break;
23573 case DW_FORM_block1:
23574 case DW_FORM_block2:
23575 case DW_FORM_block4:
23576 case DW_FORM_block:
23577 case DW_FORM_exprloc:
23578 case DW_FORM_data16:
23579 {
23580 struct dwarf_block *block = attr->as_block ();
23581 result = block->data;
23582 *len = block->size;
23583 }
23584 break;
23585
23586 /* The DW_AT_const_value attributes are supposed to carry the
23587 symbol's value "represented as it would be on the target
23588 architecture." By the time we get here, it's already been
23589 converted to host endianness, so we just need to sign- or
23590 zero-extend it as appropriate. */
23591 case DW_FORM_data1:
23592 type = die_type (die, cu);
23593 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23594 if (result == NULL)
23595 result = write_constant_as_bytes (obstack, byte_order,
23596 type, value, len);
23597 break;
23598 case DW_FORM_data2:
23599 type = die_type (die, cu);
23600 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23601 if (result == NULL)
23602 result = write_constant_as_bytes (obstack, byte_order,
23603 type, value, len);
23604 break;
23605 case DW_FORM_data4:
23606 type = die_type (die, cu);
23607 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23608 if (result == NULL)
23609 result = write_constant_as_bytes (obstack, byte_order,
23610 type, value, len);
23611 break;
23612 case DW_FORM_data8:
23613 type = die_type (die, cu);
23614 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23615 if (result == NULL)
23616 result = write_constant_as_bytes (obstack, byte_order,
23617 type, value, len);
23618 break;
23619
23620 case DW_FORM_sdata:
23621 case DW_FORM_implicit_const:
23622 type = die_type (die, cu);
23623 result = write_constant_as_bytes (obstack, byte_order,
23624 type, attr->as_signed (), len);
23625 break;
23626
23627 case DW_FORM_udata:
23628 type = die_type (die, cu);
23629 result = write_constant_as_bytes (obstack, byte_order,
23630 type, attr->as_unsigned (), len);
23631 break;
23632
23633 default:
23634 complaint (_("unsupported const value attribute form: '%s'"),
23635 dwarf_form_name (attr->form));
23636 break;
23637 }
23638
23639 return result;
23640 }
23641
23642 /* See read.h. */
23643
23644 struct type *
23645 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23646 dwarf2_per_cu_data *per_cu,
23647 dwarf2_per_objfile *per_objfile,
23648 const char **var_name)
23649 {
23650 struct die_info *die;
23651
23652 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23653 if (cu == nullptr)
23654 cu = load_cu (per_cu, per_objfile, false);
23655
23656 if (cu == nullptr)
23657 return nullptr;
23658
23659 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23660 if (!die)
23661 return NULL;
23662
23663 if (var_name != nullptr)
23664 *var_name = var_decl_name (die, cu);
23665 return die_type (die, cu);
23666 }
23667
23668 /* See read.h. */
23669
23670 struct type *
23671 dwarf2_get_die_type (cu_offset die_offset,
23672 dwarf2_per_cu_data *per_cu,
23673 dwarf2_per_objfile *per_objfile)
23674 {
23675 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23676 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23677 }
23678
23679 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23680 On entry *REF_CU is the CU of SRC_DIE.
23681 On exit *REF_CU is the CU of the result.
23682 Returns NULL if the referenced DIE isn't found. */
23683
23684 static struct die_info *
23685 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23686 struct dwarf2_cu **ref_cu)
23687 {
23688 struct die_info temp_die;
23689 struct dwarf2_cu *sig_cu;
23690 struct die_info *die;
23691 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23692
23693
23694 /* While it might be nice to assert sig_type->type == NULL here,
23695 we can get here for DW_AT_imported_declaration where we need
23696 the DIE not the type. */
23697
23698 /* If necessary, add it to the queue and load its DIEs.
23699
23700 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23701 it doesn't mean they are currently loaded. Since we require them
23702 to be loaded, we must check for ourselves. */
23703 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23704 language_minimal)
23705 || per_objfile->get_cu (sig_type) == nullptr)
23706 read_signatured_type (sig_type, per_objfile);
23707
23708 sig_cu = per_objfile->get_cu (sig_type);
23709 gdb_assert (sig_cu != NULL);
23710 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23711 temp_die.sect_off = sig_type->type_offset_in_section;
23712 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23713 to_underlying (temp_die.sect_off));
23714 if (die)
23715 {
23716 /* For .gdb_index version 7 keep track of included TUs.
23717 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23718 if (per_objfile->per_bfd->index_table != NULL
23719 && per_objfile->per_bfd->index_table->version <= 7)
23720 {
23721 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23722 }
23723
23724 *ref_cu = sig_cu;
23725 return die;
23726 }
23727
23728 return NULL;
23729 }
23730
23731 /* Follow signatured type referenced by ATTR in SRC_DIE.
23732 On entry *REF_CU is the CU of SRC_DIE.
23733 On exit *REF_CU is the CU of the result.
23734 The result is the DIE of the type.
23735 If the referenced type cannot be found an error is thrown. */
23736
23737 static struct die_info *
23738 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23739 struct dwarf2_cu **ref_cu)
23740 {
23741 ULONGEST signature = attr->as_signature ();
23742 struct signatured_type *sig_type;
23743 struct die_info *die;
23744
23745 gdb_assert (attr->form == DW_FORM_ref_sig8);
23746
23747 sig_type = lookup_signatured_type (*ref_cu, signature);
23748 /* sig_type will be NULL if the signatured type is missing from
23749 the debug info. */
23750 if (sig_type == NULL)
23751 {
23752 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23753 " from DIE at %s [in module %s]"),
23754 hex_string (signature), sect_offset_str (src_die->sect_off),
23755 objfile_name ((*ref_cu)->per_objfile->objfile));
23756 }
23757
23758 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23759 if (die == NULL)
23760 {
23761 dump_die_for_error (src_die);
23762 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23763 " from DIE at %s [in module %s]"),
23764 hex_string (signature), sect_offset_str (src_die->sect_off),
23765 objfile_name ((*ref_cu)->per_objfile->objfile));
23766 }
23767
23768 return die;
23769 }
23770
23771 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23772 reading in and processing the type unit if necessary. */
23773
23774 static struct type *
23775 get_signatured_type (struct die_info *die, ULONGEST signature,
23776 struct dwarf2_cu *cu)
23777 {
23778 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23779 struct signatured_type *sig_type;
23780 struct dwarf2_cu *type_cu;
23781 struct die_info *type_die;
23782 struct type *type;
23783
23784 sig_type = lookup_signatured_type (cu, signature);
23785 /* sig_type will be NULL if the signatured type is missing from
23786 the debug info. */
23787 if (sig_type == NULL)
23788 {
23789 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23790 " from DIE at %s [in module %s]"),
23791 hex_string (signature), sect_offset_str (die->sect_off),
23792 objfile_name (per_objfile->objfile));
23793 return build_error_marker_type (cu, die);
23794 }
23795
23796 /* If we already know the type we're done. */
23797 type = per_objfile->get_type_for_signatured_type (sig_type);
23798 if (type != nullptr)
23799 return type;
23800
23801 type_cu = cu;
23802 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23803 if (type_die != NULL)
23804 {
23805 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23806 is created. This is important, for example, because for c++ classes
23807 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23808 type = read_type_die (type_die, type_cu);
23809 if (type == NULL)
23810 {
23811 complaint (_("Dwarf Error: Cannot build signatured type %s"
23812 " referenced from DIE at %s [in module %s]"),
23813 hex_string (signature), sect_offset_str (die->sect_off),
23814 objfile_name (per_objfile->objfile));
23815 type = build_error_marker_type (cu, die);
23816 }
23817 }
23818 else
23819 {
23820 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23821 " from DIE at %s [in module %s]"),
23822 hex_string (signature), sect_offset_str (die->sect_off),
23823 objfile_name (per_objfile->objfile));
23824 type = build_error_marker_type (cu, die);
23825 }
23826
23827 per_objfile->set_type_for_signatured_type (sig_type, type);
23828
23829 return type;
23830 }
23831
23832 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23833 reading in and processing the type unit if necessary. */
23834
23835 static struct type *
23836 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23837 struct dwarf2_cu *cu) /* ARI: editCase function */
23838 {
23839 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23840 if (attr->form_is_ref ())
23841 {
23842 struct dwarf2_cu *type_cu = cu;
23843 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23844
23845 return read_type_die (type_die, type_cu);
23846 }
23847 else if (attr->form == DW_FORM_ref_sig8)
23848 {
23849 return get_signatured_type (die, attr->as_signature (), cu);
23850 }
23851 else
23852 {
23853 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23854
23855 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23856 " at %s [in module %s]"),
23857 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23858 objfile_name (per_objfile->objfile));
23859 return build_error_marker_type (cu, die);
23860 }
23861 }
23862
23863 /* Load the DIEs associated with type unit PER_CU into memory. */
23864
23865 static void
23866 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23867 dwarf2_per_objfile *per_objfile)
23868 {
23869 struct signatured_type *sig_type;
23870
23871 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23872 gdb_assert (! per_cu->type_unit_group_p ());
23873
23874 /* We have the per_cu, but we need the signatured_type.
23875 Fortunately this is an easy translation. */
23876 gdb_assert (per_cu->is_debug_types);
23877 sig_type = (struct signatured_type *) per_cu;
23878
23879 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23880
23881 read_signatured_type (sig_type, per_objfile);
23882
23883 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23884 }
23885
23886 /* Read in a signatured type and build its CU and DIEs.
23887 If the type is a stub for the real type in a DWO file,
23888 read in the real type from the DWO file as well. */
23889
23890 static void
23891 read_signatured_type (signatured_type *sig_type,
23892 dwarf2_per_objfile *per_objfile)
23893 {
23894 gdb_assert (sig_type->is_debug_types);
23895 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23896
23897 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23898
23899 if (!reader.dummy_p)
23900 {
23901 struct dwarf2_cu *cu = reader.cu;
23902 const gdb_byte *info_ptr = reader.info_ptr;
23903
23904 gdb_assert (cu->die_hash == NULL);
23905 cu->die_hash =
23906 htab_create_alloc_ex (cu->header.length / 12,
23907 die_hash,
23908 die_eq,
23909 NULL,
23910 &cu->comp_unit_obstack,
23911 hashtab_obstack_allocate,
23912 dummy_obstack_deallocate);
23913
23914 if (reader.comp_unit_die->has_children)
23915 reader.comp_unit_die->child
23916 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23917 reader.comp_unit_die);
23918 cu->dies = reader.comp_unit_die;
23919 /* comp_unit_die is not stored in die_hash, no need. */
23920
23921 /* We try not to read any attributes in this function, because
23922 not all CUs needed for references have been loaded yet, and
23923 symbol table processing isn't initialized. But we have to
23924 set the CU language, or we won't be able to build types
23925 correctly. Similarly, if we do not read the producer, we can
23926 not apply producer-specific interpretation. */
23927 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23928
23929 reader.keep ();
23930 }
23931
23932 sig_type->tu_read = 1;
23933 }
23934
23935 /* Decode simple location descriptions.
23936 Given a pointer to a dwarf block that defines a location, compute
23937 the location and return the value. If COMPUTED is non-null, it is
23938 set to true to indicate that decoding was successful, and false
23939 otherwise. If COMPUTED is null, then this function may emit a
23940 complaint. */
23941
23942 static CORE_ADDR
23943 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23944 {
23945 struct objfile *objfile = cu->per_objfile->objfile;
23946 size_t i;
23947 size_t size = blk->size;
23948 const gdb_byte *data = blk->data;
23949 CORE_ADDR stack[64];
23950 int stacki;
23951 unsigned int bytes_read, unsnd;
23952 gdb_byte op;
23953
23954 if (computed != nullptr)
23955 *computed = false;
23956
23957 i = 0;
23958 stacki = 0;
23959 stack[stacki] = 0;
23960 stack[++stacki] = 0;
23961
23962 while (i < size)
23963 {
23964 op = data[i++];
23965 switch (op)
23966 {
23967 case DW_OP_lit0:
23968 case DW_OP_lit1:
23969 case DW_OP_lit2:
23970 case DW_OP_lit3:
23971 case DW_OP_lit4:
23972 case DW_OP_lit5:
23973 case DW_OP_lit6:
23974 case DW_OP_lit7:
23975 case DW_OP_lit8:
23976 case DW_OP_lit9:
23977 case DW_OP_lit10:
23978 case DW_OP_lit11:
23979 case DW_OP_lit12:
23980 case DW_OP_lit13:
23981 case DW_OP_lit14:
23982 case DW_OP_lit15:
23983 case DW_OP_lit16:
23984 case DW_OP_lit17:
23985 case DW_OP_lit18:
23986 case DW_OP_lit19:
23987 case DW_OP_lit20:
23988 case DW_OP_lit21:
23989 case DW_OP_lit22:
23990 case DW_OP_lit23:
23991 case DW_OP_lit24:
23992 case DW_OP_lit25:
23993 case DW_OP_lit26:
23994 case DW_OP_lit27:
23995 case DW_OP_lit28:
23996 case DW_OP_lit29:
23997 case DW_OP_lit30:
23998 case DW_OP_lit31:
23999 stack[++stacki] = op - DW_OP_lit0;
24000 break;
24001
24002 case DW_OP_reg0:
24003 case DW_OP_reg1:
24004 case DW_OP_reg2:
24005 case DW_OP_reg3:
24006 case DW_OP_reg4:
24007 case DW_OP_reg5:
24008 case DW_OP_reg6:
24009 case DW_OP_reg7:
24010 case DW_OP_reg8:
24011 case DW_OP_reg9:
24012 case DW_OP_reg10:
24013 case DW_OP_reg11:
24014 case DW_OP_reg12:
24015 case DW_OP_reg13:
24016 case DW_OP_reg14:
24017 case DW_OP_reg15:
24018 case DW_OP_reg16:
24019 case DW_OP_reg17:
24020 case DW_OP_reg18:
24021 case DW_OP_reg19:
24022 case DW_OP_reg20:
24023 case DW_OP_reg21:
24024 case DW_OP_reg22:
24025 case DW_OP_reg23:
24026 case DW_OP_reg24:
24027 case DW_OP_reg25:
24028 case DW_OP_reg26:
24029 case DW_OP_reg27:
24030 case DW_OP_reg28:
24031 case DW_OP_reg29:
24032 case DW_OP_reg30:
24033 case DW_OP_reg31:
24034 stack[++stacki] = op - DW_OP_reg0;
24035 if (i < size)
24036 {
24037 if (computed == nullptr)
24038 dwarf2_complex_location_expr_complaint ();
24039 else
24040 return 0;
24041 }
24042 break;
24043
24044 case DW_OP_regx:
24045 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24046 i += bytes_read;
24047 stack[++stacki] = unsnd;
24048 if (i < size)
24049 {
24050 if (computed == nullptr)
24051 dwarf2_complex_location_expr_complaint ();
24052 else
24053 return 0;
24054 }
24055 break;
24056
24057 case DW_OP_addr:
24058 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24059 &bytes_read);
24060 i += bytes_read;
24061 break;
24062
24063 case DW_OP_const1u:
24064 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24065 i += 1;
24066 break;
24067
24068 case DW_OP_const1s:
24069 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24070 i += 1;
24071 break;
24072
24073 case DW_OP_const2u:
24074 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24075 i += 2;
24076 break;
24077
24078 case DW_OP_const2s:
24079 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24080 i += 2;
24081 break;
24082
24083 case DW_OP_const4u:
24084 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24085 i += 4;
24086 break;
24087
24088 case DW_OP_const4s:
24089 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24090 i += 4;
24091 break;
24092
24093 case DW_OP_const8u:
24094 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24095 i += 8;
24096 break;
24097
24098 case DW_OP_constu:
24099 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24100 &bytes_read);
24101 i += bytes_read;
24102 break;
24103
24104 case DW_OP_consts:
24105 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24106 i += bytes_read;
24107 break;
24108
24109 case DW_OP_dup:
24110 stack[stacki + 1] = stack[stacki];
24111 stacki++;
24112 break;
24113
24114 case DW_OP_plus:
24115 stack[stacki - 1] += stack[stacki];
24116 stacki--;
24117 break;
24118
24119 case DW_OP_plus_uconst:
24120 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24121 &bytes_read);
24122 i += bytes_read;
24123 break;
24124
24125 case DW_OP_minus:
24126 stack[stacki - 1] -= stack[stacki];
24127 stacki--;
24128 break;
24129
24130 case DW_OP_deref:
24131 /* If we're not the last op, then we definitely can't encode
24132 this using GDB's address_class enum. This is valid for partial
24133 global symbols, although the variable's address will be bogus
24134 in the psymtab. */
24135 if (i < size)
24136 {
24137 if (computed == nullptr)
24138 dwarf2_complex_location_expr_complaint ();
24139 else
24140 return 0;
24141 }
24142 break;
24143
24144 case DW_OP_GNU_push_tls_address:
24145 case DW_OP_form_tls_address:
24146 /* The top of the stack has the offset from the beginning
24147 of the thread control block at which the variable is located. */
24148 /* Nothing should follow this operator, so the top of stack would
24149 be returned. */
24150 /* This is valid for partial global symbols, but the variable's
24151 address will be bogus in the psymtab. Make it always at least
24152 non-zero to not look as a variable garbage collected by linker
24153 which have DW_OP_addr 0. */
24154 if (i < size)
24155 {
24156 if (computed == nullptr)
24157 dwarf2_complex_location_expr_complaint ();
24158 else
24159 return 0;
24160 }
24161 stack[stacki]++;
24162 break;
24163
24164 case DW_OP_GNU_uninit:
24165 if (computed != nullptr)
24166 return 0;
24167 break;
24168
24169 case DW_OP_addrx:
24170 case DW_OP_GNU_addr_index:
24171 case DW_OP_GNU_const_index:
24172 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24173 &bytes_read);
24174 i += bytes_read;
24175 break;
24176
24177 default:
24178 if (computed == nullptr)
24179 {
24180 const char *name = get_DW_OP_name (op);
24181
24182 if (name)
24183 complaint (_("unsupported stack op: '%s'"),
24184 name);
24185 else
24186 complaint (_("unsupported stack op: '%02x'"),
24187 op);
24188 }
24189
24190 return (stack[stacki]);
24191 }
24192
24193 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24194 outside of the allocated space. Also enforce minimum>0. */
24195 if (stacki >= ARRAY_SIZE (stack) - 1)
24196 {
24197 if (computed == nullptr)
24198 complaint (_("location description stack overflow"));
24199 return 0;
24200 }
24201
24202 if (stacki <= 0)
24203 {
24204 if (computed == nullptr)
24205 complaint (_("location description stack underflow"));
24206 return 0;
24207 }
24208 }
24209
24210 if (computed != nullptr)
24211 *computed = true;
24212 return (stack[stacki]);
24213 }
24214
24215 /* memory allocation interface */
24216
24217 static struct dwarf_block *
24218 dwarf_alloc_block (struct dwarf2_cu *cu)
24219 {
24220 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24221 }
24222
24223 static struct die_info *
24224 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24225 {
24226 struct die_info *die;
24227 size_t size = sizeof (struct die_info);
24228
24229 if (num_attrs > 1)
24230 size += (num_attrs - 1) * sizeof (struct attribute);
24231
24232 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24233 memset (die, 0, sizeof (struct die_info));
24234 return (die);
24235 }
24236
24237 \f
24238
24239 /* Macro support. */
24240
24241 /* An overload of dwarf_decode_macros that finds the correct section
24242 and ensures it is read in before calling the other overload. */
24243
24244 static void
24245 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24246 int section_is_gnu)
24247 {
24248 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24249 struct objfile *objfile = per_objfile->objfile;
24250 const struct line_header *lh = cu->line_header;
24251 unsigned int offset_size = cu->header.offset_size;
24252 struct dwarf2_section_info *section;
24253 const char *section_name;
24254
24255 if (cu->dwo_unit != nullptr)
24256 {
24257 if (section_is_gnu)
24258 {
24259 section = &cu->dwo_unit->dwo_file->sections.macro;
24260 section_name = ".debug_macro.dwo";
24261 }
24262 else
24263 {
24264 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24265 section_name = ".debug_macinfo.dwo";
24266 }
24267 }
24268 else
24269 {
24270 if (section_is_gnu)
24271 {
24272 section = &per_objfile->per_bfd->macro;
24273 section_name = ".debug_macro";
24274 }
24275 else
24276 {
24277 section = &per_objfile->per_bfd->macinfo;
24278 section_name = ".debug_macinfo";
24279 }
24280 }
24281
24282 section->read (objfile);
24283 if (section->buffer == nullptr)
24284 {
24285 complaint (_("missing %s section"), section_name);
24286 return;
24287 }
24288
24289 buildsym_compunit *builder = cu->get_builder ();
24290
24291 struct dwarf2_section_info *str_offsets_section;
24292 struct dwarf2_section_info *str_section;
24293 gdb::optional<ULONGEST> str_offsets_base;
24294
24295 if (cu->dwo_unit != nullptr)
24296 {
24297 str_offsets_section = &cu->dwo_unit->dwo_file
24298 ->sections.str_offsets;
24299 str_section = &cu->dwo_unit->dwo_file->sections.str;
24300 str_offsets_base = cu->header.addr_size;
24301 }
24302 else
24303 {
24304 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24305 str_section = &per_objfile->per_bfd->str;
24306 str_offsets_base = cu->str_offsets_base;
24307 }
24308
24309 dwarf_decode_macros (per_objfile, builder, section, lh,
24310 offset_size, offset, str_section, str_offsets_section,
24311 str_offsets_base, section_is_gnu);
24312 }
24313
24314 /* Return the .debug_loc section to use for CU.
24315 For DWO files use .debug_loc.dwo. */
24316
24317 static struct dwarf2_section_info *
24318 cu_debug_loc_section (struct dwarf2_cu *cu)
24319 {
24320 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24321
24322 if (cu->dwo_unit)
24323 {
24324 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24325
24326 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24327 }
24328 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24329 : &per_objfile->per_bfd->loc);
24330 }
24331
24332 /* Return the .debug_rnglists section to use for CU. */
24333 static struct dwarf2_section_info *
24334 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24335 {
24336 if (cu->header.version < 5)
24337 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24338 cu->header.version);
24339 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24340
24341 /* Make sure we read the .debug_rnglists section from the file that
24342 contains the DW_AT_ranges attribute we are reading. Normally that
24343 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24344 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24345 program. */
24346 if (cu->dwo_unit != nullptr
24347 && tag != DW_TAG_compile_unit
24348 && tag != DW_TAG_skeleton_unit)
24349 {
24350 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24351
24352 if (sections->rnglists.size > 0)
24353 return &sections->rnglists;
24354 else
24355 error (_(".debug_rnglists section is missing from .dwo file."));
24356 }
24357 return &dwarf2_per_objfile->per_bfd->rnglists;
24358 }
24359
24360 /* A helper function that fills in a dwarf2_loclist_baton. */
24361
24362 static void
24363 fill_in_loclist_baton (struct dwarf2_cu *cu,
24364 struct dwarf2_loclist_baton *baton,
24365 const struct attribute *attr)
24366 {
24367 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24368 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24369
24370 section->read (per_objfile->objfile);
24371
24372 baton->per_objfile = per_objfile;
24373 baton->per_cu = cu->per_cu;
24374 gdb_assert (baton->per_cu);
24375 /* We don't know how long the location list is, but make sure we
24376 don't run off the edge of the section. */
24377 baton->size = section->size - attr->as_unsigned ();
24378 baton->data = section->buffer + attr->as_unsigned ();
24379 if (cu->base_address.has_value ())
24380 baton->base_address = *cu->base_address;
24381 else
24382 baton->base_address = 0;
24383 baton->from_dwo = cu->dwo_unit != NULL;
24384 }
24385
24386 static void
24387 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24388 struct dwarf2_cu *cu, int is_block)
24389 {
24390 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24391 struct objfile *objfile = per_objfile->objfile;
24392 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24393
24394 if (attr->form_is_section_offset ()
24395 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24396 the section. If so, fall through to the complaint in the
24397 other branch. */
24398 && attr->as_unsigned () < section->get_size (objfile))
24399 {
24400 struct dwarf2_loclist_baton *baton;
24401
24402 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24403
24404 fill_in_loclist_baton (cu, baton, attr);
24405
24406 if (!cu->base_address.has_value ())
24407 complaint (_("Location list used without "
24408 "specifying the CU base address."));
24409
24410 sym->set_aclass_index ((is_block
24411 ? dwarf2_loclist_block_index
24412 : dwarf2_loclist_index));
24413 SYMBOL_LOCATION_BATON (sym) = baton;
24414 }
24415 else
24416 {
24417 struct dwarf2_locexpr_baton *baton;
24418
24419 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24420 baton->per_objfile = per_objfile;
24421 baton->per_cu = cu->per_cu;
24422 gdb_assert (baton->per_cu);
24423
24424 if (attr->form_is_block ())
24425 {
24426 /* Note that we're just copying the block's data pointer
24427 here, not the actual data. We're still pointing into the
24428 info_buffer for SYM's objfile; right now we never release
24429 that buffer, but when we do clean up properly this may
24430 need to change. */
24431 struct dwarf_block *block = attr->as_block ();
24432 baton->size = block->size;
24433 baton->data = block->data;
24434 }
24435 else
24436 {
24437 dwarf2_invalid_attrib_class_complaint ("location description",
24438 sym->natural_name ());
24439 baton->size = 0;
24440 }
24441
24442 sym->set_aclass_index ((is_block
24443 ? dwarf2_locexpr_block_index
24444 : dwarf2_locexpr_index));
24445 SYMBOL_LOCATION_BATON (sym) = baton;
24446 }
24447 }
24448
24449 /* See read.h. */
24450
24451 const comp_unit_head *
24452 dwarf2_per_cu_data::get_header () const
24453 {
24454 if (!m_header_read_in)
24455 {
24456 const gdb_byte *info_ptr
24457 = this->section->buffer + to_underlying (this->sect_off);
24458
24459 memset (&m_header, 0, sizeof (m_header));
24460
24461 read_comp_unit_head (&m_header, info_ptr, this->section,
24462 rcuh_kind::COMPILE);
24463
24464 m_header_read_in = true;
24465 }
24466
24467 return &m_header;
24468 }
24469
24470 /* See read.h. */
24471
24472 int
24473 dwarf2_per_cu_data::addr_size () const
24474 {
24475 return this->get_header ()->addr_size;
24476 }
24477
24478 /* See read.h. */
24479
24480 int
24481 dwarf2_per_cu_data::offset_size () const
24482 {
24483 return this->get_header ()->offset_size;
24484 }
24485
24486 /* See read.h. */
24487
24488 int
24489 dwarf2_per_cu_data::ref_addr_size () const
24490 {
24491 const comp_unit_head *header = this->get_header ();
24492
24493 if (header->version == 2)
24494 return header->addr_size;
24495 else
24496 return header->offset_size;
24497 }
24498
24499 /* A helper function for dwarf2_find_containing_comp_unit that returns
24500 the index of the result, and that searches a vector. It will
24501 return a result even if the offset in question does not actually
24502 occur in any CU. This is separate so that it can be unit
24503 tested. */
24504
24505 static int
24506 dwarf2_find_containing_comp_unit
24507 (sect_offset sect_off,
24508 unsigned int offset_in_dwz,
24509 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24510 {
24511 int low, high;
24512
24513 low = 0;
24514 high = all_comp_units.size () - 1;
24515 while (high > low)
24516 {
24517 struct dwarf2_per_cu_data *mid_cu;
24518 int mid = low + (high - low) / 2;
24519
24520 mid_cu = all_comp_units[mid].get ();
24521 if (mid_cu->is_dwz > offset_in_dwz
24522 || (mid_cu->is_dwz == offset_in_dwz
24523 && mid_cu->sect_off + mid_cu->length > sect_off))
24524 high = mid;
24525 else
24526 low = mid + 1;
24527 }
24528 gdb_assert (low == high);
24529 return low;
24530 }
24531
24532 /* Locate the .debug_info compilation unit from CU's objfile which contains
24533 the DIE at OFFSET. Raises an error on failure. */
24534
24535 static struct dwarf2_per_cu_data *
24536 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24537 unsigned int offset_in_dwz,
24538 dwarf2_per_bfd *per_bfd)
24539 {
24540 int low = dwarf2_find_containing_comp_unit
24541 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
24542 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
24543
24544 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24545 {
24546 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24547 error (_("Dwarf Error: could not find partial DIE containing "
24548 "offset %s [in module %s]"),
24549 sect_offset_str (sect_off),
24550 bfd_get_filename (per_bfd->obfd));
24551
24552 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
24553 <= sect_off);
24554 return per_bfd->all_comp_units[low - 1].get ();
24555 }
24556 else
24557 {
24558 if (low == per_bfd->all_comp_units.size () - 1
24559 && sect_off >= this_cu->sect_off + this_cu->length)
24560 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24561 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24562 return this_cu;
24563 }
24564 }
24565
24566 #if GDB_SELF_TEST
24567
24568 namespace selftests {
24569 namespace find_containing_comp_unit {
24570
24571 static void
24572 run_test ()
24573 {
24574 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24575 dwarf2_per_cu_data *one_ptr = one.get ();
24576 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24577 dwarf2_per_cu_data *two_ptr = two.get ();
24578 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24579 dwarf2_per_cu_data *three_ptr = three.get ();
24580 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24581 dwarf2_per_cu_data *four_ptr = four.get ();
24582
24583 one->length = 5;
24584 two->sect_off = sect_offset (one->length);
24585 two->length = 7;
24586
24587 three->length = 5;
24588 three->is_dwz = 1;
24589 four->sect_off = sect_offset (three->length);
24590 four->length = 7;
24591 four->is_dwz = 1;
24592
24593 std::vector<dwarf2_per_cu_data_up> units;
24594 units.push_back (std::move (one));
24595 units.push_back (std::move (two));
24596 units.push_back (std::move (three));
24597 units.push_back (std::move (four));
24598
24599 int result;
24600
24601 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24602 SELF_CHECK (units[result].get () == one_ptr);
24603 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24604 SELF_CHECK (units[result].get () == one_ptr);
24605 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24606 SELF_CHECK (units[result].get () == two_ptr);
24607
24608 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24609 SELF_CHECK (units[result].get () == three_ptr);
24610 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24611 SELF_CHECK (units[result].get () == three_ptr);
24612 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24613 SELF_CHECK (units[result].get () == four_ptr);
24614 }
24615
24616 }
24617 }
24618
24619 #endif /* GDB_SELF_TEST */
24620
24621 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24622
24623 static void
24624 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24625 enum language pretend_language)
24626 {
24627 struct attribute *attr;
24628
24629 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24630
24631 /* Set the language we're debugging. */
24632 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24633 if (cu->producer != nullptr
24634 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
24635 {
24636 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24637 attribute is not standardised yet. As a workaround for the
24638 language detection we fall back to the DW_AT_producer
24639 string. */
24640 cu->per_cu->lang = language_opencl;
24641 }
24642 else if (cu->producer != nullptr
24643 && strstr (cu->producer, "GNU Go ") != NULL)
24644 {
24645 /* Similar hack for Go. */
24646 cu->per_cu->lang = language_go;
24647 }
24648 else if (attr != nullptr)
24649 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
24650 else
24651 cu->per_cu->lang = pretend_language;
24652 cu->language_defn = language_def (cu->per_cu->lang);
24653 }
24654
24655 /* See read.h. */
24656
24657 dwarf2_cu *
24658 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24659 {
24660 auto it = m_dwarf2_cus.find (per_cu);
24661 if (it == m_dwarf2_cus.end ())
24662 return nullptr;
24663
24664 return it->second;
24665 }
24666
24667 /* See read.h. */
24668
24669 void
24670 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24671 {
24672 gdb_assert (this->get_cu (per_cu) == nullptr);
24673
24674 m_dwarf2_cus[per_cu] = cu;
24675 }
24676
24677 /* See read.h. */
24678
24679 void
24680 dwarf2_per_objfile::age_comp_units ()
24681 {
24682 dwarf_read_debug_printf_v ("running");
24683
24684 /* This is not expected to be called in the middle of CU expansion. There is
24685 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24686 loaded in memory. Calling age_comp_units while the queue is in use could
24687 make us free the DIEs for a CU that is in the queue and therefore break
24688 that invariant. */
24689 gdb_assert (!this->per_bfd->queue.has_value ());
24690
24691 /* Start by clearing all marks. */
24692 for (auto pair : m_dwarf2_cus)
24693 pair.second->clear_mark ();
24694
24695 /* Traverse all CUs, mark them and their dependencies if used recently
24696 enough. */
24697 for (auto pair : m_dwarf2_cus)
24698 {
24699 dwarf2_cu *cu = pair.second;
24700
24701 cu->last_used++;
24702 if (cu->last_used <= dwarf_max_cache_age)
24703 cu->mark ();
24704 }
24705
24706 /* Delete all CUs still not marked. */
24707 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24708 {
24709 dwarf2_cu *cu = it->second;
24710
24711 if (!cu->is_marked ())
24712 {
24713 dwarf_read_debug_printf_v ("deleting old CU %s",
24714 sect_offset_str (cu->per_cu->sect_off));
24715 delete cu;
24716 it = m_dwarf2_cus.erase (it);
24717 }
24718 else
24719 it++;
24720 }
24721 }
24722
24723 /* See read.h. */
24724
24725 void
24726 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24727 {
24728 auto it = m_dwarf2_cus.find (per_cu);
24729 if (it == m_dwarf2_cus.end ())
24730 return;
24731
24732 delete it->second;
24733
24734 m_dwarf2_cus.erase (it);
24735 }
24736
24737 dwarf2_per_objfile::~dwarf2_per_objfile ()
24738 {
24739 remove_all_cus ();
24740 }
24741
24742 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24743 We store these in a hash table separate from the DIEs, and preserve them
24744 when the DIEs are flushed out of cache.
24745
24746 The CU "per_cu" pointer is needed because offset alone is not enough to
24747 uniquely identify the type. A file may have multiple .debug_types sections,
24748 or the type may come from a DWO file. Furthermore, while it's more logical
24749 to use per_cu->section+offset, with Fission the section with the data is in
24750 the DWO file but we don't know that section at the point we need it.
24751 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24752 because we can enter the lookup routine, get_die_type_at_offset, from
24753 outside this file, and thus won't necessarily have PER_CU->cu.
24754 Fortunately, PER_CU is stable for the life of the objfile. */
24755
24756 struct dwarf2_per_cu_offset_and_type
24757 {
24758 const struct dwarf2_per_cu_data *per_cu;
24759 sect_offset sect_off;
24760 struct type *type;
24761 };
24762
24763 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24764
24765 static hashval_t
24766 per_cu_offset_and_type_hash (const void *item)
24767 {
24768 const struct dwarf2_per_cu_offset_and_type *ofs
24769 = (const struct dwarf2_per_cu_offset_and_type *) item;
24770
24771 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24772 }
24773
24774 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24775
24776 static int
24777 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24778 {
24779 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24780 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24781 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24782 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24783
24784 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24785 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24786 }
24787
24788 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24789 table if necessary. For convenience, return TYPE.
24790
24791 The DIEs reading must have careful ordering to:
24792 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24793 reading current DIE.
24794 * Not trying to dereference contents of still incompletely read in types
24795 while reading in other DIEs.
24796 * Enable referencing still incompletely read in types just by a pointer to
24797 the type without accessing its fields.
24798
24799 Therefore caller should follow these rules:
24800 * Try to fetch any prerequisite types we may need to build this DIE type
24801 before building the type and calling set_die_type.
24802 * After building type call set_die_type for current DIE as soon as
24803 possible before fetching more types to complete the current type.
24804 * Make the type as complete as possible before fetching more types. */
24805
24806 static struct type *
24807 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24808 bool skip_data_location)
24809 {
24810 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24811 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24812 struct objfile *objfile = per_objfile->objfile;
24813 struct attribute *attr;
24814 struct dynamic_prop prop;
24815
24816 /* For Ada types, make sure that the gnat-specific data is always
24817 initialized (if not already set). There are a few types where
24818 we should not be doing so, because the type-specific area is
24819 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24820 where the type-specific area is used to store the floatformat).
24821 But this is not a problem, because the gnat-specific information
24822 is actually not needed for these types. */
24823 if (need_gnat_info (cu)
24824 && type->code () != TYPE_CODE_FUNC
24825 && type->code () != TYPE_CODE_FLT
24826 && type->code () != TYPE_CODE_METHODPTR
24827 && type->code () != TYPE_CODE_MEMBERPTR
24828 && type->code () != TYPE_CODE_METHOD
24829 && type->code () != TYPE_CODE_FIXED_POINT
24830 && !HAVE_GNAT_AUX_INFO (type))
24831 INIT_GNAT_SPECIFIC (type);
24832
24833 /* Read DW_AT_allocated and set in type. */
24834 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24835 if (attr != NULL)
24836 {
24837 struct type *prop_type = cu->addr_sized_int_type (false);
24838 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24839 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24840 }
24841
24842 /* Read DW_AT_associated and set in type. */
24843 attr = dwarf2_attr (die, DW_AT_associated, cu);
24844 if (attr != NULL)
24845 {
24846 struct type *prop_type = cu->addr_sized_int_type (false);
24847 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24848 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24849 }
24850
24851 /* Read DW_AT_data_location and set in type. */
24852 if (!skip_data_location)
24853 {
24854 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24855 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24856 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24857 }
24858
24859 if (per_objfile->die_type_hash == NULL)
24860 per_objfile->die_type_hash
24861 = htab_up (htab_create_alloc (127,
24862 per_cu_offset_and_type_hash,
24863 per_cu_offset_and_type_eq,
24864 NULL, xcalloc, xfree));
24865
24866 ofs.per_cu = cu->per_cu;
24867 ofs.sect_off = die->sect_off;
24868 ofs.type = type;
24869 slot = (struct dwarf2_per_cu_offset_and_type **)
24870 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24871 if (*slot)
24872 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24873 sect_offset_str (die->sect_off));
24874 *slot = XOBNEW (&objfile->objfile_obstack,
24875 struct dwarf2_per_cu_offset_and_type);
24876 **slot = ofs;
24877 return type;
24878 }
24879
24880 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24881 or return NULL if the die does not have a saved type. */
24882
24883 static struct type *
24884 get_die_type_at_offset (sect_offset sect_off,
24885 dwarf2_per_cu_data *per_cu,
24886 dwarf2_per_objfile *per_objfile)
24887 {
24888 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24889
24890 if (per_objfile->die_type_hash == NULL)
24891 return NULL;
24892
24893 ofs.per_cu = per_cu;
24894 ofs.sect_off = sect_off;
24895 slot = ((struct dwarf2_per_cu_offset_and_type *)
24896 htab_find (per_objfile->die_type_hash.get (), &ofs));
24897 if (slot)
24898 return slot->type;
24899 else
24900 return NULL;
24901 }
24902
24903 /* Look up the type for DIE in CU in die_type_hash,
24904 or return NULL if DIE does not have a saved type. */
24905
24906 static struct type *
24907 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24908 {
24909 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24910 }
24911
24912 /* Trivial hash function for partial_die_info: the hash value of a DIE
24913 is its offset in .debug_info for this objfile. */
24914
24915 static hashval_t
24916 partial_die_hash (const void *item)
24917 {
24918 const struct partial_die_info *part_die
24919 = (const struct partial_die_info *) item;
24920
24921 return to_underlying (part_die->sect_off);
24922 }
24923
24924 /* Trivial comparison function for partial_die_info structures: two DIEs
24925 are equal if they have the same offset. */
24926
24927 static int
24928 partial_die_eq (const void *item_lhs, const void *item_rhs)
24929 {
24930 const struct partial_die_info *part_die_lhs
24931 = (const struct partial_die_info *) item_lhs;
24932 const struct partial_die_info *part_die_rhs
24933 = (const struct partial_die_info *) item_rhs;
24934
24935 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24936 }
24937
24938 struct cmd_list_element *set_dwarf_cmdlist;
24939 struct cmd_list_element *show_dwarf_cmdlist;
24940
24941 static void
24942 show_check_physname (struct ui_file *file, int from_tty,
24943 struct cmd_list_element *c, const char *value)
24944 {
24945 gdb_printf (file,
24946 _("Whether to check \"physname\" is %s.\n"),
24947 value);
24948 }
24949
24950 void _initialize_dwarf2_read ();
24951 void
24952 _initialize_dwarf2_read ()
24953 {
24954 add_setshow_prefix_cmd ("dwarf", class_maintenance,
24955 _("\
24956 Set DWARF specific variables.\n\
24957 Configure DWARF variables such as the cache size."),
24958 _("\
24959 Show DWARF specific variables.\n\
24960 Show DWARF variables such as the cache size."),
24961 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
24962 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
24963
24964 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24965 &dwarf_max_cache_age, _("\
24966 Set the upper bound on the age of cached DWARF compilation units."), _("\
24967 Show the upper bound on the age of cached DWARF compilation units."), _("\
24968 A higher limit means that cached compilation units will be stored\n\
24969 in memory longer, and more total memory will be used. Zero disables\n\
24970 caching, which can slow down startup."),
24971 NULL,
24972 show_dwarf_max_cache_age,
24973 &set_dwarf_cmdlist,
24974 &show_dwarf_cmdlist);
24975
24976 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24977 Set debugging of the DWARF reader."), _("\
24978 Show debugging of the DWARF reader."), _("\
24979 When enabled (non-zero), debugging messages are printed during DWARF\n\
24980 reading and symtab expansion. A value of 1 (one) provides basic\n\
24981 information. A value greater than 1 provides more verbose information."),
24982 NULL,
24983 NULL,
24984 &setdebuglist, &showdebuglist);
24985
24986 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24987 Set debugging of the DWARF DIE reader."), _("\
24988 Show debugging of the DWARF DIE reader."), _("\
24989 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24990 The value is the maximum depth to print."),
24991 NULL,
24992 NULL,
24993 &setdebuglist, &showdebuglist);
24994
24995 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24996 Set debugging of the dwarf line reader."), _("\
24997 Show debugging of the dwarf line reader."), _("\
24998 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24999 A value of 1 (one) provides basic information.\n\
25000 A value greater than 1 provides more verbose information."),
25001 NULL,
25002 NULL,
25003 &setdebuglist, &showdebuglist);
25004
25005 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25006 Set cross-checking of \"physname\" code against demangler."), _("\
25007 Show cross-checking of \"physname\" code against demangler."), _("\
25008 When enabled, GDB's internal \"physname\" code is checked against\n\
25009 the demangler."),
25010 NULL, show_check_physname,
25011 &setdebuglist, &showdebuglist);
25012
25013 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25014 no_class, &use_deprecated_index_sections, _("\
25015 Set whether to use deprecated gdb_index sections."), _("\
25016 Show whether to use deprecated gdb_index sections."), _("\
25017 When enabled, deprecated .gdb_index sections are used anyway.\n\
25018 Normally they are ignored either because of a missing feature or\n\
25019 performance issue.\n\
25020 Warning: This option must be enabled before gdb reads the file."),
25021 NULL,
25022 NULL,
25023 &setlist, &showlist);
25024
25025 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25026 &dwarf2_locexpr_funcs);
25027 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25028 &dwarf2_loclist_funcs);
25029
25030 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25031 &dwarf2_block_frame_base_locexpr_funcs);
25032 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25033 &dwarf2_block_frame_base_loclist_funcs);
25034
25035 #if GDB_SELF_TEST
25036 selftests::register_test ("dw2_expand_symtabs_matching",
25037 selftests::dw2_expand_symtabs_matching::run_test);
25038 selftests::register_test ("dwarf2_find_containing_comp_unit",
25039 selftests::find_containing_comp_unit::run_test);
25040 #endif
25041 }