Rename dwarf2/comp-unit.h
[binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91 #include <unordered_set>
92
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
97
98 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
99
100 #define dwarf_read_debug_printf(fmt, ...) \
101 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
102 ##__VA_ARGS__)
103
104 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
105
106 #define dwarf_read_debug_printf_v(fmt, ...) \
107 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
109
110 /* When non-zero, dump DIEs after they are read in. */
111 static unsigned int dwarf_die_debug = 0;
112
113 /* When non-zero, dump line number entries as they are read in. */
114 unsigned int dwarf_line_debug = 0;
115
116 /* When true, cross-check physname against demangler. */
117 static bool check_physname = false;
118
119 /* When true, do not reject deprecated .gdb_index sections. */
120 static bool use_deprecated_index_sections = false;
121
122 /* This is used to store the data that is always per objfile. */
123 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125 /* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
135
136 /* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138 static int dwarf2_locexpr_index;
139 static int dwarf2_loclist_index;
140 static int dwarf2_locexpr_block_index;
141 static int dwarf2_loclist_block_index;
142
143 /* Size of .debug_loclists section header for 32-bit DWARF format. */
144 #define LOCLIST_HEADER_SIZE32 12
145
146 /* Size of .debug_loclists section header for 64-bit DWARF format. */
147 #define LOCLIST_HEADER_SIZE64 20
148
149 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
150 #define RNGLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
153 #define RNGLIST_HEADER_SIZE64 20
154
155 /* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168 struct name_component
169 {
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178 };
179
180 /* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183 struct mapped_index_base
184 {
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
211 void build_name_components (dwarf2_per_objfile *per_objfile);
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
218 find_name_components_bounds (const lookup_name_info &ln_no_params,
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
221
222 /* Prevent deleting/destroying via a base class pointer. */
223 protected:
224 ~mapped_index_base() = default;
225 };
226
227 /* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231 class offset_view
232 {
233 public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262 private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265 };
266
267 /* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
269 struct mapped_index final : public mapped_index_base
270 {
271 /* Index data format version. */
272 int version = 0;
273
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
276
277 /* The symbol table, implemented as a hash table. */
278 offset_view symbol_table;
279
280 /* A pointer to the constant pool. */
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
296
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
301 }
302
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size () / 2; }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
363 };
364
365 /* See dwarf2read.h. */
366
367 dwarf2_per_objfile *
368 get_dwarf2_per_objfile (struct objfile *objfile)
369 {
370 return dwarf2_objfile_data_key.get (objfile);
371 }
372
373 /* Default names of the debugging sections. */
374
375 /* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
378 const struct dwarf2_debug_sections dwarf2_elf_names =
379 {
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
384 { ".debug_loclists", ".zdebug_loclists" },
385 { ".debug_macinfo", ".zdebug_macinfo" },
386 { ".debug_macro", ".zdebug_macro" },
387 { ".debug_str", ".zdebug_str" },
388 { ".debug_str_offsets", ".zdebug_str_offsets" },
389 { ".debug_line_str", ".zdebug_line_str" },
390 { ".debug_ranges", ".zdebug_ranges" },
391 { ".debug_rnglists", ".zdebug_rnglists" },
392 { ".debug_types", ".zdebug_types" },
393 { ".debug_addr", ".zdebug_addr" },
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
396 { ".gdb_index", ".zgdb_index" },
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
399 23
400 };
401
402 /* List of DWO/DWP sections. */
403
404 static const struct dwop_section_names
405 {
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
410 struct dwarf2_section_names loclists_dwo;
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
413 struct dwarf2_section_names rnglists_dwo;
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
419 }
420 dwop_section_names =
421 {
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
435 };
436
437 /* local data types */
438
439 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441 struct loclists_rnglists_header
442 {
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461 };
462
463 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466 struct stmt_list_hash
467 {
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
472 sect_offset line_sect_off;
473 };
474
475 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
479
480 struct type_unit_group : public dwarf2_per_cu_data
481 {
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
485 std::vector<signatured_type *> *tus = nullptr;
486
487 /* The data used to construct the hash key. */
488 struct stmt_list_hash hash {};
489 };
490
491 /* These sections are what may appear in a (real or virtual) DWO file. */
492
493 struct dwo_sections
494 {
495 struct dwarf2_section_info abbrev;
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
498 struct dwarf2_section_info loclists;
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
501 struct dwarf2_section_info rnglists;
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
506 std::vector<dwarf2_section_info> types;
507 };
508
509 /* CUs/TUs in DWP/DWO files. */
510
511 struct dwo_unit
512 {
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
522 struct dwarf2_section_info *section;
523
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530 };
531
532 /* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536 enum dwp_v2_section_ids
537 {
538 DW_SECT_MIN = 1
539 };
540
541 /* Data for one DWO file.
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
551
552 struct dwo_file
553 {
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
561 const char *dwo_name = nullptr;
562
563 /* The DW_AT_comp_dir attribute. */
564 const char *comp_dir = nullptr;
565
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
568 gdb_bfd_ref_ptr dbfd;
569
570 /* The sections that make up this DWO file.
571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
572 sections (for lack of a better name). */
573 struct dwo_sections sections {};
574
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
579 htab_up cus;
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
583 htab_up tus;
584 };
585
586 /* These sections are what may appear in a DWP file. */
587
588 struct dwp_sections
589 {
590 /* These are used by all DWP versions (1, 2 and 5). */
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
594
595 /* These are only used by DWP version 2 and version 5 files.
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
607 struct dwarf2_section_info loclists;
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
610 struct dwarf2_section_info rnglists;
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
613 };
614
615 /* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
617
618 struct virtual_v1_dwo_sections
619 {
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
627 That is recorded here, and copied to dwo_unit.section. */
628 struct dwarf2_section_info info_or_types;
629 };
630
631 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
636 struct virtual_v2_or_v5_dwo_sections
637 {
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666 };
667
668 /* Contents of DWP hash tables. */
669
670 struct dwp_hash_table
671 {
672 uint32_t version, nr_columns;
673 uint32_t nr_units, nr_slots;
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685 #define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700 #define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
712 } section_pool;
713 };
714
715 /* Data for one DWP file. */
716
717 struct dwp_file
718 {
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
725 /* Name of the file. */
726 const char *name;
727
728 /* File format version. */
729 int version = 0;
730
731 /* The bfd. */
732 gdb_bfd_ref_ptr dbfd;
733
734 /* Section info for this file. */
735 struct dwp_sections sections {};
736
737 /* Table of CUs in the file. */
738 const struct dwp_hash_table *cus = nullptr;
739
740 /* Table of TUs in the file. */
741 const struct dwp_hash_table *tus = nullptr;
742
743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
744 htab_up loaded_cus;
745 htab_up loaded_tus;
746
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
751 };
752
753 /* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
756 struct exists to abstract away the constant parameters of die reading. */
757
758 struct die_reader_specs
759 {
760 /* The bfd of die_section. */
761 bfd *abfd;
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
767 struct dwo_file *dwo_file;
768
769 /* The section the die comes from.
770 This is either .debug_info or .debug_types, or the .dwo variants. */
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
774 const gdb_byte *buffer;
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
778
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
781 };
782
783 /* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786 class cutu_reader : public die_reader_specs
787 {
788 public:
789
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
792 struct abbrev_table *abbrev_table,
793 dwarf2_cu *existing_cu,
794 bool skip_partial);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
797 dwarf2_per_objfile *per_objfile,
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
805 bool dummy_p = false;
806
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
811 private:
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
814 dwarf2_cu *existing_cu);
815
816 struct dwarf2_per_cu_data *m_this_cu;
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824 };
825
826 /* When we construct a partial symbol table entry we only
827 need this much information. */
828 struct partial_die_info : public allocate_on_obstack
829 {
830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
835
836 /* Adjust the partial die before generating a symbol for it. This
837 function may set the is_external flag or change the DIE's
838 name. */
839 void fixup (struct dwarf2_cu *cu);
840
841 /* Read a minimal amount of information into the minimal die
842 structure. */
843 const gdb_byte *read (const struct die_reader_specs *reader,
844 const struct abbrev_info &abbrev,
845 const gdb_byte *info_ptr);
846
847 /* Compute the name of this partial DIE. This memoizes the
848 result, so it is safe to call multiple times. */
849 const char *name (dwarf2_cu *cu);
850
851 /* Offset of this DIE. */
852 const sect_offset sect_off;
853
854 /* DWARF-2 tag for this DIE. */
855 const ENUM_BITFIELD(dwarf_tag) tag : 16;
856
857 /* Assorted flags describing the data found in this DIE. */
858 const unsigned int has_children : 1;
859
860 unsigned int is_external : 1;
861 unsigned int is_declaration : 1;
862 unsigned int has_type : 1;
863 unsigned int has_specification : 1;
864 unsigned int has_pc_info : 1;
865 unsigned int may_be_inlined : 1;
866
867 /* This DIE has been marked DW_AT_main_subprogram. */
868 unsigned int main_subprogram : 1;
869
870 /* Flag set if the SCOPE field of this structure has been
871 computed. */
872 unsigned int scope_set : 1;
873
874 /* Flag set if the DIE has a byte_size attribute. */
875 unsigned int has_byte_size : 1;
876
877 /* Flag set if the DIE has a DW_AT_const_value attribute. */
878 unsigned int has_const_value : 1;
879
880 /* Flag set if any of the DIE's children are template arguments. */
881 unsigned int has_template_arguments : 1;
882
883 /* Flag set if fixup has been called on this die. */
884 unsigned int fixup_called : 1;
885
886 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
887 unsigned int is_dwz : 1;
888
889 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
890 unsigned int spec_is_dwz : 1;
891
892 unsigned int canonical_name : 1;
893
894 /* The name of this DIE. Normally the value of DW_AT_name, but
895 sometimes a default name for unnamed DIEs. */
896 const char *raw_name = nullptr;
897
898 /* The linkage name, if present. */
899 const char *linkage_name = nullptr;
900
901 /* The scope to prepend to our children. This is generally
902 allocated on the comp_unit_obstack, so will disappear
903 when this compilation unit leaves the cache. */
904 const char *scope = nullptr;
905
906 /* Some data associated with the partial DIE. The tag determines
907 which field is live. */
908 union
909 {
910 /* The location description associated with this DIE, if any. */
911 struct dwarf_block *locdesc;
912 /* The offset of an import, for DW_TAG_imported_unit. */
913 sect_offset sect_off;
914 } d {};
915
916 /* If HAS_PC_INFO, the PC range associated with this DIE. */
917 CORE_ADDR lowpc = 0;
918 CORE_ADDR highpc = 0;
919
920 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
921 DW_AT_sibling, if any. */
922 /* NOTE: This member isn't strictly necessary, partial_die_info::read
923 could return DW_AT_sibling values to its caller load_partial_dies. */
924 const gdb_byte *sibling = nullptr;
925
926 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
927 DW_AT_specification (or DW_AT_abstract_origin or
928 DW_AT_extension). */
929 sect_offset spec_offset {};
930
931 /* Pointers to this DIE's parent, first child, and next sibling,
932 if any. */
933 struct partial_die_info *die_parent = nullptr;
934 struct partial_die_info *die_child = nullptr;
935 struct partial_die_info *die_sibling = nullptr;
936
937 friend struct partial_die_info *
938 dwarf2_cu::find_partial_die (sect_offset sect_off);
939
940 private:
941 /* Only need to do look up in dwarf2_cu::find_partial_die. */
942 partial_die_info (sect_offset sect_off)
943 : partial_die_info (sect_off, DW_TAG_padding, 0)
944 {
945 }
946
947 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
948 int has_children_)
949 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
950 {
951 is_external = 0;
952 is_declaration = 0;
953 has_type = 0;
954 has_specification = 0;
955 has_pc_info = 0;
956 may_be_inlined = 0;
957 main_subprogram = 0;
958 scope_set = 0;
959 has_byte_size = 0;
960 has_const_value = 0;
961 has_template_arguments = 0;
962 fixup_called = 0;
963 is_dwz = 0;
964 spec_is_dwz = 0;
965 canonical_name = 0;
966 }
967 };
968
969 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
970 but this would require a corresponding change in unpack_field_as_long
971 and friends. */
972 static int bits_per_byte = 8;
973
974 struct variant_part_builder;
975
976 /* When reading a variant, we track a bit more information about the
977 field, and store it in an object of this type. */
978
979 struct variant_field
980 {
981 int first_field = -1;
982 int last_field = -1;
983
984 /* A variant can contain other variant parts. */
985 std::vector<variant_part_builder> variant_parts;
986
987 /* If we see a DW_TAG_variant, then this will be set if this is the
988 default branch. */
989 bool default_branch = false;
990 /* If we see a DW_AT_discr_value, then this will be the discriminant
991 value. */
992 ULONGEST discriminant_value = 0;
993 /* If we see a DW_AT_discr_list, then this is a pointer to the list
994 data. */
995 struct dwarf_block *discr_list_data = nullptr;
996 };
997
998 /* This represents a DW_TAG_variant_part. */
999
1000 struct variant_part_builder
1001 {
1002 /* The offset of the discriminant field. */
1003 sect_offset discriminant_offset {};
1004
1005 /* Variants that are direct children of this variant part. */
1006 std::vector<variant_field> variants;
1007
1008 /* True if we're currently reading a variant. */
1009 bool processing_variant = false;
1010 };
1011
1012 struct nextfield
1013 {
1014 int accessibility = 0;
1015 int virtuality = 0;
1016 /* Variant parts need to find the discriminant, which is a DIE
1017 reference. We track the section offset of each field to make
1018 this link. */
1019 sect_offset offset;
1020 struct field field {};
1021 };
1022
1023 struct fnfieldlist
1024 {
1025 const char *name = nullptr;
1026 std::vector<struct fn_field> fnfields;
1027 };
1028
1029 /* The routines that read and process dies for a C struct or C++ class
1030 pass lists of data member fields and lists of member function fields
1031 in an instance of a field_info structure, as defined below. */
1032 struct field_info
1033 {
1034 /* List of data member and baseclasses fields. */
1035 std::vector<struct nextfield> fields;
1036 std::vector<struct nextfield> baseclasses;
1037
1038 /* Set if the accessibility of one of the fields is not public. */
1039 bool non_public_fields = false;
1040
1041 /* Member function fieldlist array, contains name of possibly overloaded
1042 member function, number of overloaded member functions and a pointer
1043 to the head of the member function field chain. */
1044 std::vector<struct fnfieldlist> fnfieldlists;
1045
1046 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1047 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1048 std::vector<struct decl_field> typedef_field_list;
1049
1050 /* Nested types defined by this class and the number of elements in this
1051 list. */
1052 std::vector<struct decl_field> nested_types_list;
1053
1054 /* If non-null, this is the variant part we are currently
1055 reading. */
1056 variant_part_builder *current_variant_part = nullptr;
1057 /* This holds all the top-level variant parts attached to the type
1058 we're reading. */
1059 std::vector<variant_part_builder> variant_parts;
1060
1061 /* Return the total number of fields (including baseclasses). */
1062 int nfields () const
1063 {
1064 return fields.size () + baseclasses.size ();
1065 }
1066 };
1067
1068 /* Loaded secondary compilation units are kept in memory until they
1069 have not been referenced for the processing of this many
1070 compilation units. Set this to zero to disable caching. Cache
1071 sizes of up to at least twenty will improve startup time for
1072 typical inter-CU-reference binaries, at an obvious memory cost. */
1073 static int dwarf_max_cache_age = 5;
1074 static void
1075 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1076 struct cmd_list_element *c, const char *value)
1077 {
1078 fprintf_filtered (file, _("The upper bound on the age of cached "
1079 "DWARF compilation units is %s.\n"),
1080 value);
1081 }
1082 \f
1083 /* local function prototypes */
1084
1085 static void dwarf2_find_base_address (struct die_info *die,
1086 struct dwarf2_cu *cu);
1087
1088 static dwarf2_psymtab *create_partial_symtab
1089 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1090 const char *name);
1091
1092 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1093 const gdb_byte *info_ptr,
1094 struct die_info *type_unit_die);
1095
1096 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1097
1098 static void scan_partial_symbols (struct partial_die_info *,
1099 CORE_ADDR *, CORE_ADDR *,
1100 int, struct dwarf2_cu *);
1101
1102 static void add_partial_symbol (struct partial_die_info *,
1103 struct dwarf2_cu *);
1104
1105 static void add_partial_namespace (struct partial_die_info *pdi,
1106 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1107 int set_addrmap, struct dwarf2_cu *cu);
1108
1109 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1110 CORE_ADDR *highpc, int set_addrmap,
1111 struct dwarf2_cu *cu);
1112
1113 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1114 struct dwarf2_cu *cu);
1115
1116 static void add_partial_subprogram (struct partial_die_info *pdi,
1117 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1118 int need_pc, struct dwarf2_cu *cu);
1119
1120 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1121
1122 static struct partial_die_info *load_partial_dies
1123 (const struct die_reader_specs *, const gdb_byte *, int);
1124
1125 /* A pair of partial_die_info and compilation unit. */
1126 struct cu_partial_die_info
1127 {
1128 /* The compilation unit of the partial_die_info. */
1129 struct dwarf2_cu *cu;
1130 /* A partial_die_info. */
1131 struct partial_die_info *pdi;
1132
1133 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1134 : cu (cu),
1135 pdi (pdi)
1136 { /* Nothing. */ }
1137
1138 private:
1139 cu_partial_die_info () = delete;
1140 };
1141
1142 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1143 struct dwarf2_cu *);
1144
1145 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1146 struct attribute *,
1147 const struct attr_abbrev *,
1148 const gdb_byte *);
1149
1150 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1151 struct attribute *attr, dwarf_tag tag);
1152
1153 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1154
1155 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1156 dwarf2_section_info *, sect_offset);
1157
1158 static const char *read_indirect_string
1159 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1160 const struct comp_unit_head *, unsigned int *);
1161
1162 static const char *read_indirect_string_at_offset
1163 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1164
1165 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1166 const gdb_byte *,
1167 unsigned int *);
1168
1169 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1170 ULONGEST str_index);
1171
1172 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1173 ULONGEST str_index);
1174
1175 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1176
1177 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1178 struct dwarf2_cu *);
1179
1180 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1181 struct dwarf2_cu *cu);
1182
1183 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1184
1185 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1186 struct dwarf2_cu *cu);
1187
1188 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1189
1190 static struct die_info *die_specification (struct die_info *die,
1191 struct dwarf2_cu **);
1192
1193 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1194 struct dwarf2_cu *cu);
1195
1196 static void dwarf_decode_lines (struct line_header *, const char *,
1197 struct dwarf2_cu *, dwarf2_psymtab *,
1198 CORE_ADDR, int decode_mapping);
1199
1200 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1201 const char *);
1202
1203 static struct symbol *new_symbol (struct die_info *, struct type *,
1204 struct dwarf2_cu *, struct symbol * = NULL);
1205
1206 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1207 struct dwarf2_cu *);
1208
1209 static void dwarf2_const_value_attr (const struct attribute *attr,
1210 struct type *type,
1211 const char *name,
1212 struct obstack *obstack,
1213 struct dwarf2_cu *cu, LONGEST *value,
1214 const gdb_byte **bytes,
1215 struct dwarf2_locexpr_baton **baton);
1216
1217 static struct type *read_subrange_index_type (struct die_info *die,
1218 struct dwarf2_cu *cu);
1219
1220 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1221
1222 static int need_gnat_info (struct dwarf2_cu *);
1223
1224 static struct type *die_descriptive_type (struct die_info *,
1225 struct dwarf2_cu *);
1226
1227 static void set_descriptive_type (struct type *, struct die_info *,
1228 struct dwarf2_cu *);
1229
1230 static struct type *die_containing_type (struct die_info *,
1231 struct dwarf2_cu *);
1232
1233 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1234 struct dwarf2_cu *);
1235
1236 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1237
1238 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1239
1240 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1241
1242 static char *typename_concat (struct obstack *obs, const char *prefix,
1243 const char *suffix, int physname,
1244 struct dwarf2_cu *cu);
1245
1246 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1247
1248 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1249
1250 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1251
1252 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1253
1254 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1255
1256 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1257
1258 /* Return the .debug_loclists section to use for cu. */
1259 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1260
1261 /* Return the .debug_rnglists section to use for cu. */
1262 static struct dwarf2_section_info *cu_debug_rnglists_section
1263 (struct dwarf2_cu *cu, dwarf_tag tag);
1264
1265 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1266 values. Keep the items ordered with increasing constraints compliance. */
1267 enum pc_bounds_kind
1268 {
1269 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1270 PC_BOUNDS_NOT_PRESENT,
1271
1272 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1273 were present but they do not form a valid range of PC addresses. */
1274 PC_BOUNDS_INVALID,
1275
1276 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1277 PC_BOUNDS_RANGES,
1278
1279 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1280 PC_BOUNDS_HIGH_LOW,
1281 };
1282
1283 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1284 CORE_ADDR *, CORE_ADDR *,
1285 struct dwarf2_cu *,
1286 dwarf2_psymtab *);
1287
1288 static void get_scope_pc_bounds (struct die_info *,
1289 CORE_ADDR *, CORE_ADDR *,
1290 struct dwarf2_cu *);
1291
1292 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1293 CORE_ADDR, struct dwarf2_cu *);
1294
1295 static void dwarf2_add_field (struct field_info *, struct die_info *,
1296 struct dwarf2_cu *);
1297
1298 static void dwarf2_attach_fields_to_type (struct field_info *,
1299 struct type *, struct dwarf2_cu *);
1300
1301 static void dwarf2_add_member_fn (struct field_info *,
1302 struct die_info *, struct type *,
1303 struct dwarf2_cu *);
1304
1305 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1306 struct type *,
1307 struct dwarf2_cu *);
1308
1309 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1310
1311 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1312
1313 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1314
1315 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1316
1317 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1318
1319 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1320
1321 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1322
1323 static struct type *read_module_type (struct die_info *die,
1324 struct dwarf2_cu *cu);
1325
1326 static const char *namespace_name (struct die_info *die,
1327 int *is_anonymous, struct dwarf2_cu *);
1328
1329 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1330
1331 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1332 bool * = nullptr);
1333
1334 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1335 struct dwarf2_cu *);
1336
1337 static struct die_info *read_die_and_siblings_1
1338 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1339 struct die_info *);
1340
1341 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1342 const gdb_byte *info_ptr,
1343 const gdb_byte **new_info_ptr,
1344 struct die_info *parent);
1345
1346 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1347 struct die_info **, const gdb_byte *,
1348 int);
1349
1350 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1351 struct die_info **, const gdb_byte *);
1352
1353 static void process_die (struct die_info *, struct dwarf2_cu *);
1354
1355 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1356 struct objfile *);
1357
1358 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1359
1360 static const char *dwarf2_full_name (const char *name,
1361 struct die_info *die,
1362 struct dwarf2_cu *cu);
1363
1364 static const char *dwarf2_physname (const char *name, struct die_info *die,
1365 struct dwarf2_cu *cu);
1366
1367 static struct die_info *dwarf2_extension (struct die_info *die,
1368 struct dwarf2_cu **);
1369
1370 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1371
1372 static void dump_die_for_error (struct die_info *);
1373
1374 static void dump_die_1 (struct ui_file *, int level, int max_level,
1375 struct die_info *);
1376
1377 /*static*/ void dump_die (struct die_info *, int max_level);
1378
1379 static void store_in_ref_table (struct die_info *,
1380 struct dwarf2_cu *);
1381
1382 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1383 const struct attribute *,
1384 struct dwarf2_cu **);
1385
1386 static struct die_info *follow_die_ref (struct die_info *,
1387 const struct attribute *,
1388 struct dwarf2_cu **);
1389
1390 static struct die_info *follow_die_sig (struct die_info *,
1391 const struct attribute *,
1392 struct dwarf2_cu **);
1393
1394 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1395 struct dwarf2_cu *);
1396
1397 static struct type *get_DW_AT_signature_type (struct die_info *,
1398 const struct attribute *,
1399 struct dwarf2_cu *);
1400
1401 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1402 dwarf2_per_objfile *per_objfile);
1403
1404 static void read_signatured_type (signatured_type *sig_type,
1405 dwarf2_per_objfile *per_objfile);
1406
1407 static int attr_to_dynamic_prop (const struct attribute *attr,
1408 struct die_info *die, struct dwarf2_cu *cu,
1409 struct dynamic_prop *prop, struct type *type);
1410
1411 /* memory allocation interface */
1412
1413 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1414
1415 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1416
1417 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1418
1419 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1420 struct dwarf2_loclist_baton *baton,
1421 const struct attribute *attr);
1422
1423 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1424 struct symbol *sym,
1425 struct dwarf2_cu *cu,
1426 int is_block);
1427
1428 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1429 const gdb_byte *info_ptr,
1430 const struct abbrev_info *abbrev);
1431
1432 static hashval_t partial_die_hash (const void *item);
1433
1434 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1435
1436 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1437 (sect_offset sect_off, unsigned int offset_in_dwz,
1438 dwarf2_per_objfile *per_objfile);
1439
1440 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1441 struct die_info *comp_unit_die,
1442 enum language pretend_language);
1443
1444 static struct type *set_die_type (struct die_info *, struct type *,
1445 struct dwarf2_cu *, bool = false);
1446
1447 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1448
1449 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1450 dwarf2_per_objfile *per_objfile,
1451 dwarf2_cu *existing_cu,
1452 bool skip_partial,
1453 enum language pretend_language);
1454
1455 static void process_full_comp_unit (dwarf2_cu *cu,
1456 enum language pretend_language);
1457
1458 static void process_full_type_unit (dwarf2_cu *cu,
1459 enum language pretend_language);
1460
1461 static struct type *get_die_type_at_offset (sect_offset,
1462 dwarf2_per_cu_data *per_cu,
1463 dwarf2_per_objfile *per_objfile);
1464
1465 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1466
1467 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1468 dwarf2_per_objfile *per_objfile,
1469 enum language pretend_language);
1470
1471 static void process_queue (dwarf2_per_objfile *per_objfile);
1472
1473 /* Class, the destructor of which frees all allocated queue entries. This
1474 will only have work to do if an error was thrown while processing the
1475 dwarf. If no error was thrown then the queue entries should have all
1476 been processed, and freed, as we went along. */
1477
1478 class dwarf2_queue_guard
1479 {
1480 public:
1481 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1482 : m_per_objfile (per_objfile)
1483 {
1484 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1485
1486 m_per_objfile->per_bfd->queue.emplace ();
1487 }
1488
1489 /* Free any entries remaining on the queue. There should only be
1490 entries left if we hit an error while processing the dwarf. */
1491 ~dwarf2_queue_guard ()
1492 {
1493 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1494
1495 m_per_objfile->per_bfd->queue.reset ();
1496 }
1497
1498 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1499
1500 private:
1501 dwarf2_per_objfile *m_per_objfile;
1502 };
1503
1504 dwarf2_queue_item::~dwarf2_queue_item ()
1505 {
1506 /* Anything still marked queued is likely to be in an
1507 inconsistent state, so discard it. */
1508 if (per_cu->queued)
1509 {
1510 per_objfile->remove_cu (per_cu);
1511 per_cu->queued = 0;
1512 }
1513 }
1514
1515 /* See dwarf2/read.h. */
1516
1517 void
1518 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1519 {
1520 if (data->is_debug_types)
1521 delete static_cast<signatured_type *> (data);
1522 else
1523 delete data;
1524 }
1525
1526 /* The return type of find_file_and_directory. Note, the enclosed
1527 string pointers are only valid while this object is valid. */
1528
1529 struct file_and_directory
1530 {
1531 /* The filename. This is never NULL. */
1532 const char *name;
1533
1534 /* The compilation directory. NULL if not known. If we needed to
1535 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1536 points directly to the DW_AT_comp_dir string attribute owned by
1537 the obstack that owns the DIE. */
1538 const char *comp_dir;
1539
1540 /* If we needed to build a new string for comp_dir, this is what
1541 owns the storage. */
1542 std::string comp_dir_storage;
1543 };
1544
1545 static file_and_directory find_file_and_directory (struct die_info *die,
1546 struct dwarf2_cu *cu);
1547
1548 static htab_up allocate_signatured_type_table ();
1549
1550 static htab_up allocate_dwo_unit_table ();
1551
1552 static struct dwo_unit *lookup_dwo_unit_in_dwp
1553 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1554 const char *comp_dir, ULONGEST signature, int is_debug_types);
1555
1556 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1557
1558 static struct dwo_unit *lookup_dwo_comp_unit
1559 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1560 ULONGEST signature);
1561
1562 static struct dwo_unit *lookup_dwo_type_unit
1563 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1564
1565 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1566
1567 /* A unique pointer to a dwo_file. */
1568
1569 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1570
1571 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1572
1573 static void check_producer (struct dwarf2_cu *cu);
1574
1575 static void free_line_header_voidp (void *arg);
1576 \f
1577 /* Various complaints about symbol reading that don't abort the process. */
1578
1579 static void
1580 dwarf2_debug_line_missing_file_complaint (void)
1581 {
1582 complaint (_(".debug_line section has line data without a file"));
1583 }
1584
1585 static void
1586 dwarf2_debug_line_missing_end_sequence_complaint (void)
1587 {
1588 complaint (_(".debug_line section has line "
1589 "program sequence without an end"));
1590 }
1591
1592 static void
1593 dwarf2_complex_location_expr_complaint (void)
1594 {
1595 complaint (_("location expression too complex"));
1596 }
1597
1598 static void
1599 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1600 int arg3)
1601 {
1602 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1603 arg1, arg2, arg3);
1604 }
1605
1606 static void
1607 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1608 {
1609 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1610 arg1, arg2);
1611 }
1612
1613 /* Hash function for line_header_hash. */
1614
1615 static hashval_t
1616 line_header_hash (const struct line_header *ofs)
1617 {
1618 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1619 }
1620
1621 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1622
1623 static hashval_t
1624 line_header_hash_voidp (const void *item)
1625 {
1626 const struct line_header *ofs = (const struct line_header *) item;
1627
1628 return line_header_hash (ofs);
1629 }
1630
1631 /* Equality function for line_header_hash. */
1632
1633 static int
1634 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1635 {
1636 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1637 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1638
1639 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1640 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1641 }
1642
1643 \f
1644
1645 /* See declaration. */
1646
1647 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1648 bool can_copy_)
1649 : obfd (obfd),
1650 can_copy (can_copy_)
1651 {
1652 if (names == NULL)
1653 names = &dwarf2_elf_names;
1654
1655 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1656 locate_sections (obfd, sec, *names);
1657 }
1658
1659 dwarf2_per_bfd::~dwarf2_per_bfd ()
1660 {
1661 for (auto &per_cu : all_comp_units)
1662 per_cu->imported_symtabs_free ();
1663
1664 /* Everything else should be on this->obstack. */
1665 }
1666
1667 /* See read.h. */
1668
1669 void
1670 dwarf2_per_objfile::remove_all_cus ()
1671 {
1672 gdb_assert (!this->per_bfd->queue.has_value ());
1673
1674 for (auto pair : m_dwarf2_cus)
1675 delete pair.second;
1676
1677 m_dwarf2_cus.clear ();
1678 }
1679
1680 /* A helper class that calls free_cached_comp_units on
1681 destruction. */
1682
1683 class free_cached_comp_units
1684 {
1685 public:
1686
1687 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1688 : m_per_objfile (per_objfile)
1689 {
1690 }
1691
1692 ~free_cached_comp_units ()
1693 {
1694 m_per_objfile->remove_all_cus ();
1695 }
1696
1697 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1698
1699 private:
1700
1701 dwarf2_per_objfile *m_per_objfile;
1702 };
1703
1704 /* See read.h. */
1705
1706 bool
1707 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1708 {
1709 gdb_assert (per_cu->index < this->m_symtabs.size ());
1710
1711 return this->m_symtabs[per_cu->index] != nullptr;
1712 }
1713
1714 /* See read.h. */
1715
1716 compunit_symtab *
1717 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1718 {
1719 gdb_assert (per_cu->index < this->m_symtabs.size ());
1720
1721 return this->m_symtabs[per_cu->index];
1722 }
1723
1724 /* See read.h. */
1725
1726 void
1727 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1728 compunit_symtab *symtab)
1729 {
1730 gdb_assert (per_cu->index < this->m_symtabs.size ());
1731 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1732
1733 this->m_symtabs[per_cu->index] = symtab;
1734 }
1735
1736 /* Try to locate the sections we need for DWARF 2 debugging
1737 information and return true if we have enough to do something.
1738 NAMES points to the dwarf2 section names, or is NULL if the standard
1739 ELF names are used. CAN_COPY is true for formats where symbol
1740 interposition is possible and so symbol values must follow copy
1741 relocation rules. */
1742
1743 int
1744 dwarf2_has_info (struct objfile *objfile,
1745 const struct dwarf2_debug_sections *names,
1746 bool can_copy)
1747 {
1748 if (objfile->flags & OBJF_READNEVER)
1749 return 0;
1750
1751 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1752
1753 if (per_objfile == NULL)
1754 {
1755 dwarf2_per_bfd *per_bfd;
1756
1757 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1758 BFD doesn't require relocations.
1759
1760 We don't share with objfiles for which -readnow was requested,
1761 because it would complicate things when loading the same BFD with
1762 -readnow and then without -readnow. */
1763 if (!gdb_bfd_requires_relocations (objfile->obfd)
1764 && (objfile->flags & OBJF_READNOW) == 0)
1765 {
1766 /* See if one has been created for this BFD yet. */
1767 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1768
1769 if (per_bfd == nullptr)
1770 {
1771 /* No, create it now. */
1772 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1773 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1774 }
1775 }
1776 else
1777 {
1778 /* No sharing possible, create one specifically for this objfile. */
1779 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1780 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1781 }
1782
1783 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1784 }
1785
1786 return (!per_objfile->per_bfd->info.is_virtual
1787 && per_objfile->per_bfd->info.s.section != NULL
1788 && !per_objfile->per_bfd->abbrev.is_virtual
1789 && per_objfile->per_bfd->abbrev.s.section != NULL);
1790 }
1791
1792 /* See declaration. */
1793
1794 void
1795 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1796 const dwarf2_debug_sections &names)
1797 {
1798 flagword aflag = bfd_section_flags (sectp);
1799
1800 if ((aflag & SEC_HAS_CONTENTS) == 0)
1801 {
1802 }
1803 else if (elf_section_data (sectp)->this_hdr.sh_size
1804 > bfd_get_file_size (abfd))
1805 {
1806 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1807 warning (_("Discarding section %s which has a section size (%s"
1808 ") larger than the file size [in module %s]"),
1809 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1810 bfd_get_filename (abfd));
1811 }
1812 else if (names.info.matches (sectp->name))
1813 {
1814 this->info.s.section = sectp;
1815 this->info.size = bfd_section_size (sectp);
1816 }
1817 else if (names.abbrev.matches (sectp->name))
1818 {
1819 this->abbrev.s.section = sectp;
1820 this->abbrev.size = bfd_section_size (sectp);
1821 }
1822 else if (names.line.matches (sectp->name))
1823 {
1824 this->line.s.section = sectp;
1825 this->line.size = bfd_section_size (sectp);
1826 }
1827 else if (names.loc.matches (sectp->name))
1828 {
1829 this->loc.s.section = sectp;
1830 this->loc.size = bfd_section_size (sectp);
1831 }
1832 else if (names.loclists.matches (sectp->name))
1833 {
1834 this->loclists.s.section = sectp;
1835 this->loclists.size = bfd_section_size (sectp);
1836 }
1837 else if (names.macinfo.matches (sectp->name))
1838 {
1839 this->macinfo.s.section = sectp;
1840 this->macinfo.size = bfd_section_size (sectp);
1841 }
1842 else if (names.macro.matches (sectp->name))
1843 {
1844 this->macro.s.section = sectp;
1845 this->macro.size = bfd_section_size (sectp);
1846 }
1847 else if (names.str.matches (sectp->name))
1848 {
1849 this->str.s.section = sectp;
1850 this->str.size = bfd_section_size (sectp);
1851 }
1852 else if (names.str_offsets.matches (sectp->name))
1853 {
1854 this->str_offsets.s.section = sectp;
1855 this->str_offsets.size = bfd_section_size (sectp);
1856 }
1857 else if (names.line_str.matches (sectp->name))
1858 {
1859 this->line_str.s.section = sectp;
1860 this->line_str.size = bfd_section_size (sectp);
1861 }
1862 else if (names.addr.matches (sectp->name))
1863 {
1864 this->addr.s.section = sectp;
1865 this->addr.size = bfd_section_size (sectp);
1866 }
1867 else if (names.frame.matches (sectp->name))
1868 {
1869 this->frame.s.section = sectp;
1870 this->frame.size = bfd_section_size (sectp);
1871 }
1872 else if (names.eh_frame.matches (sectp->name))
1873 {
1874 this->eh_frame.s.section = sectp;
1875 this->eh_frame.size = bfd_section_size (sectp);
1876 }
1877 else if (names.ranges.matches (sectp->name))
1878 {
1879 this->ranges.s.section = sectp;
1880 this->ranges.size = bfd_section_size (sectp);
1881 }
1882 else if (names.rnglists.matches (sectp->name))
1883 {
1884 this->rnglists.s.section = sectp;
1885 this->rnglists.size = bfd_section_size (sectp);
1886 }
1887 else if (names.types.matches (sectp->name))
1888 {
1889 struct dwarf2_section_info type_section;
1890
1891 memset (&type_section, 0, sizeof (type_section));
1892 type_section.s.section = sectp;
1893 type_section.size = bfd_section_size (sectp);
1894
1895 this->types.push_back (type_section);
1896 }
1897 else if (names.gdb_index.matches (sectp->name))
1898 {
1899 this->gdb_index.s.section = sectp;
1900 this->gdb_index.size = bfd_section_size (sectp);
1901 }
1902 else if (names.debug_names.matches (sectp->name))
1903 {
1904 this->debug_names.s.section = sectp;
1905 this->debug_names.size = bfd_section_size (sectp);
1906 }
1907 else if (names.debug_aranges.matches (sectp->name))
1908 {
1909 this->debug_aranges.s.section = sectp;
1910 this->debug_aranges.size = bfd_section_size (sectp);
1911 }
1912
1913 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1914 && bfd_section_vma (sectp) == 0)
1915 this->has_section_at_zero = true;
1916 }
1917
1918 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1919 SECTION_NAME. */
1920
1921 void
1922 dwarf2_get_section_info (struct objfile *objfile,
1923 enum dwarf2_section_enum sect,
1924 asection **sectp, const gdb_byte **bufp,
1925 bfd_size_type *sizep)
1926 {
1927 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1928 struct dwarf2_section_info *info;
1929
1930 /* We may see an objfile without any DWARF, in which case we just
1931 return nothing. */
1932 if (per_objfile == NULL)
1933 {
1934 *sectp = NULL;
1935 *bufp = NULL;
1936 *sizep = 0;
1937 return;
1938 }
1939 switch (sect)
1940 {
1941 case DWARF2_DEBUG_FRAME:
1942 info = &per_objfile->per_bfd->frame;
1943 break;
1944 case DWARF2_EH_FRAME:
1945 info = &per_objfile->per_bfd->eh_frame;
1946 break;
1947 default:
1948 gdb_assert_not_reached ("unexpected section");
1949 }
1950
1951 info->read (objfile);
1952
1953 *sectp = info->get_bfd_section ();
1954 *bufp = info->buffer;
1955 *sizep = info->size;
1956 }
1957
1958 \f
1959 /* DWARF quick_symbol_functions support. */
1960
1961 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1962 unique line tables, so we maintain a separate table of all .debug_line
1963 derived entries to support the sharing.
1964 All the quick functions need is the list of file names. We discard the
1965 line_header when we're done and don't need to record it here. */
1966 struct quick_file_names
1967 {
1968 /* The data used to construct the hash key. */
1969 struct stmt_list_hash hash;
1970
1971 /* The number of entries in file_names, real_names. */
1972 unsigned int num_file_names;
1973
1974 /* The file names from the line table, after being run through
1975 file_full_name. */
1976 const char **file_names;
1977
1978 /* The file names from the line table after being run through
1979 gdb_realpath. These are computed lazily. */
1980 const char **real_names;
1981 };
1982
1983 /* When using the index (and thus not using psymtabs), each CU has an
1984 object of this type. This is used to hold information needed by
1985 the various "quick" methods. */
1986 struct dwarf2_per_cu_quick_data
1987 {
1988 /* The file table. This can be NULL if there was no file table
1989 or it's currently not read in.
1990 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
1991 struct quick_file_names *file_names;
1992
1993 /* A temporary mark bit used when iterating over all CUs in
1994 expand_symtabs_matching. */
1995 unsigned int mark : 1;
1996
1997 /* True if we've tried to read the file table and found there isn't one.
1998 There will be no point in trying to read it again next time. */
1999 unsigned int no_file_data : 1;
2000 };
2001
2002 /* A subclass of psymbol_functions that arranges to read the DWARF
2003 partial symbols when needed. */
2004 struct lazy_dwarf_reader : public psymbol_functions
2005 {
2006 using psymbol_functions::psymbol_functions;
2007
2008 bool can_lazily_read_symbols () override
2009 {
2010 return true;
2011 }
2012
2013 void read_partial_symbols (struct objfile *objfile) override
2014 {
2015 if (dwarf2_has_info (objfile, nullptr))
2016 dwarf2_build_psymtabs (objfile, this);
2017 }
2018 };
2019
2020 static quick_symbol_functions_up
2021 make_lazy_dwarf_reader ()
2022 {
2023 return quick_symbol_functions_up (new lazy_dwarf_reader);
2024 }
2025
2026 struct dwarf2_base_index_functions : public quick_symbol_functions
2027 {
2028 bool has_symbols (struct objfile *objfile) override;
2029
2030 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2031
2032 void forget_cached_source_info (struct objfile *objfile) override;
2033
2034 enum language lookup_global_symbol_language (struct objfile *objfile,
2035 const char *name,
2036 domain_enum domain,
2037 bool *symbol_found_p) override
2038 {
2039 *symbol_found_p = false;
2040 return language_unknown;
2041 }
2042
2043 void print_stats (struct objfile *objfile, bool print_bcache) override;
2044
2045 void expand_all_symtabs (struct objfile *objfile) override;
2046
2047 struct compunit_symtab *find_pc_sect_compunit_symtab
2048 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2049 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2050
2051 struct compunit_symtab *find_compunit_symtab_by_address
2052 (struct objfile *objfile, CORE_ADDR address) override
2053 {
2054 return nullptr;
2055 }
2056
2057 void map_symbol_filenames (struct objfile *objfile,
2058 gdb::function_view<symbol_filename_ftype> fun,
2059 bool need_fullname) override;
2060 };
2061
2062 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2063 {
2064 void dump (struct objfile *objfile) override;
2065
2066 void expand_matching_symbols
2067 (struct objfile *,
2068 const lookup_name_info &lookup_name,
2069 domain_enum domain,
2070 int global,
2071 symbol_compare_ftype *ordered_compare) override;
2072
2073 bool expand_symtabs_matching
2074 (struct objfile *objfile,
2075 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2076 const lookup_name_info *lookup_name,
2077 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2078 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2079 block_search_flags search_flags,
2080 domain_enum domain,
2081 enum search_domain kind) override;
2082 };
2083
2084 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2085 {
2086 void dump (struct objfile *objfile) override;
2087
2088 void expand_matching_symbols
2089 (struct objfile *,
2090 const lookup_name_info &lookup_name,
2091 domain_enum domain,
2092 int global,
2093 symbol_compare_ftype *ordered_compare) override;
2094
2095 bool expand_symtabs_matching
2096 (struct objfile *objfile,
2097 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2098 const lookup_name_info *lookup_name,
2099 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2100 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2101 block_search_flags search_flags,
2102 domain_enum domain,
2103 enum search_domain kind) override;
2104 };
2105
2106 static quick_symbol_functions_up
2107 make_dwarf_gdb_index ()
2108 {
2109 return quick_symbol_functions_up (new dwarf2_gdb_index);
2110 }
2111
2112 static quick_symbol_functions_up
2113 make_dwarf_debug_names ()
2114 {
2115 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2116 }
2117
2118 /* Utility hash function for a stmt_list_hash. */
2119
2120 static hashval_t
2121 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2122 {
2123 hashval_t v = 0;
2124
2125 if (stmt_list_hash->dwo_unit != NULL)
2126 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2127 v += to_underlying (stmt_list_hash->line_sect_off);
2128 return v;
2129 }
2130
2131 /* Utility equality function for a stmt_list_hash. */
2132
2133 static int
2134 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2135 const struct stmt_list_hash *rhs)
2136 {
2137 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2138 return 0;
2139 if (lhs->dwo_unit != NULL
2140 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2141 return 0;
2142
2143 return lhs->line_sect_off == rhs->line_sect_off;
2144 }
2145
2146 /* Hash function for a quick_file_names. */
2147
2148 static hashval_t
2149 hash_file_name_entry (const void *e)
2150 {
2151 const struct quick_file_names *file_data
2152 = (const struct quick_file_names *) e;
2153
2154 return hash_stmt_list_entry (&file_data->hash);
2155 }
2156
2157 /* Equality function for a quick_file_names. */
2158
2159 static int
2160 eq_file_name_entry (const void *a, const void *b)
2161 {
2162 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2163 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2164
2165 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2166 }
2167
2168 /* Delete function for a quick_file_names. */
2169
2170 static void
2171 delete_file_name_entry (void *e)
2172 {
2173 struct quick_file_names *file_data = (struct quick_file_names *) e;
2174 int i;
2175
2176 for (i = 0; i < file_data->num_file_names; ++i)
2177 {
2178 xfree ((void*) file_data->file_names[i]);
2179 if (file_data->real_names)
2180 xfree ((void*) file_data->real_names[i]);
2181 }
2182
2183 /* The space for the struct itself lives on the obstack, so we don't
2184 free it here. */
2185 }
2186
2187 /* Create a quick_file_names hash table. */
2188
2189 static htab_up
2190 create_quick_file_names_table (unsigned int nr_initial_entries)
2191 {
2192 return htab_up (htab_create_alloc (nr_initial_entries,
2193 hash_file_name_entry, eq_file_name_entry,
2194 delete_file_name_entry, xcalloc, xfree));
2195 }
2196
2197 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2198 function is unrelated to symtabs, symtab would have to be created afterwards.
2199 You should call age_cached_comp_units after processing the CU. */
2200
2201 static dwarf2_cu *
2202 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2203 bool skip_partial)
2204 {
2205 if (per_cu->is_debug_types)
2206 load_full_type_unit (per_cu, per_objfile);
2207 else
2208 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2209 skip_partial, language_minimal);
2210
2211 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2212 if (cu == nullptr)
2213 return nullptr; /* Dummy CU. */
2214
2215 dwarf2_find_base_address (cu->dies, cu);
2216
2217 return cu;
2218 }
2219
2220 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2221
2222 static void
2223 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2224 dwarf2_per_objfile *per_objfile, bool skip_partial)
2225 {
2226 /* Skip type_unit_groups, reading the type units they contain
2227 is handled elsewhere. */
2228 if (per_cu->type_unit_group_p ())
2229 return;
2230
2231 {
2232 /* The destructor of dwarf2_queue_guard frees any entries left on
2233 the queue. After this point we're guaranteed to leave this function
2234 with the dwarf queue empty. */
2235 dwarf2_queue_guard q_guard (per_objfile);
2236
2237 if (!per_objfile->symtab_set_p (per_cu))
2238 {
2239 queue_comp_unit (per_cu, per_objfile, language_minimal);
2240 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2241
2242 /* If we just loaded a CU from a DWO, and we're working with an index
2243 that may badly handle TUs, load all the TUs in that DWO as well.
2244 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2245 if (!per_cu->is_debug_types
2246 && cu != NULL
2247 && cu->dwo_unit != NULL
2248 && per_objfile->per_bfd->index_table != NULL
2249 && per_objfile->per_bfd->index_table->version <= 7
2250 /* DWP files aren't supported yet. */
2251 && get_dwp_file (per_objfile) == NULL)
2252 queue_and_load_all_dwo_tus (cu);
2253 }
2254
2255 process_queue (per_objfile);
2256 }
2257
2258 /* Age the cache, releasing compilation units that have not
2259 been used recently. */
2260 per_objfile->age_comp_units ();
2261 }
2262
2263 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2264 the per-objfile for which this symtab is instantiated.
2265
2266 Returns the resulting symbol table. */
2267
2268 static struct compunit_symtab *
2269 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2270 dwarf2_per_objfile *per_objfile,
2271 bool skip_partial)
2272 {
2273 gdb_assert (per_objfile->per_bfd->using_index);
2274
2275 if (!per_objfile->symtab_set_p (per_cu))
2276 {
2277 free_cached_comp_units freer (per_objfile);
2278 scoped_restore decrementer = increment_reading_symtab ();
2279 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2280 process_cu_includes (per_objfile);
2281 }
2282
2283 return per_objfile->get_symtab (per_cu);
2284 }
2285
2286 /* See read.h. */
2287
2288 dwarf2_per_cu_data_up
2289 dwarf2_per_bfd::allocate_per_cu ()
2290 {
2291 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2292 result->per_bfd = this;
2293 result->index = m_num_psymtabs++;
2294 return result;
2295 }
2296
2297 /* See read.h. */
2298
2299 std::unique_ptr<signatured_type>
2300 dwarf2_per_bfd::allocate_signatured_type ()
2301 {
2302 std::unique_ptr<signatured_type> result (new signatured_type);
2303 result->per_bfd = this;
2304 result->index = m_num_psymtabs++;
2305 tu_stats.nr_tus++;
2306 return result;
2307 }
2308
2309 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2310 obstack, and constructed with the specified field values. */
2311
2312 static dwarf2_per_cu_data_up
2313 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2314 struct dwarf2_section_info *section,
2315 int is_dwz,
2316 sect_offset sect_off, ULONGEST length)
2317 {
2318 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2319 the_cu->sect_off = sect_off;
2320 the_cu->length = length;
2321 the_cu->section = section;
2322 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2323 struct dwarf2_per_cu_quick_data);
2324 the_cu->is_dwz = is_dwz;
2325 return the_cu;
2326 }
2327
2328 /* A helper for create_cus_from_index that handles a given list of
2329 CUs. */
2330
2331 static void
2332 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2333 const gdb_byte *cu_list, offset_type n_elements,
2334 struct dwarf2_section_info *section,
2335 int is_dwz)
2336 {
2337 for (offset_type i = 0; i < n_elements; i += 2)
2338 {
2339 gdb_static_assert (sizeof (ULONGEST) >= 8);
2340
2341 sect_offset sect_off
2342 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2343 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2344 cu_list += 2 * 8;
2345
2346 dwarf2_per_cu_data_up per_cu
2347 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2348 length);
2349 per_bfd->all_comp_units.push_back (std::move (per_cu));
2350 }
2351 }
2352
2353 /* Read the CU list from the mapped index, and use it to create all
2354 the CU objects for PER_BFD. */
2355
2356 static void
2357 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2358 const gdb_byte *cu_list, offset_type cu_list_elements,
2359 const gdb_byte *dwz_list, offset_type dwz_elements)
2360 {
2361 gdb_assert (per_bfd->all_comp_units.empty ());
2362 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2363
2364 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2365 &per_bfd->info, 0);
2366
2367 if (dwz_elements == 0)
2368 return;
2369
2370 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2371 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2372 &dwz->info, 1);
2373 }
2374
2375 /* Create the signatured type hash table from the index. */
2376
2377 static void
2378 create_signatured_type_table_from_index
2379 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2380 const gdb_byte *bytes, offset_type elements)
2381 {
2382 htab_up sig_types_hash = allocate_signatured_type_table ();
2383
2384 for (offset_type i = 0; i < elements; i += 3)
2385 {
2386 std::unique_ptr<signatured_type> sig_type;
2387 ULONGEST signature;
2388 void **slot;
2389 cu_offset type_offset_in_tu;
2390
2391 gdb_static_assert (sizeof (ULONGEST) >= 8);
2392 sect_offset sect_off
2393 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2394 type_offset_in_tu
2395 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2396 BFD_ENDIAN_LITTLE);
2397 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2398 bytes += 3 * 8;
2399
2400 sig_type = per_bfd->allocate_signatured_type ();
2401 sig_type->signature = signature;
2402 sig_type->type_offset_in_tu = type_offset_in_tu;
2403 sig_type->is_debug_types = 1;
2404 sig_type->section = section;
2405 sig_type->sect_off = sect_off;
2406 sig_type->v.quick
2407 = OBSTACK_ZALLOC (&per_bfd->obstack,
2408 struct dwarf2_per_cu_quick_data);
2409
2410 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2411 *slot = sig_type.get ();
2412
2413 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2414 }
2415
2416 per_bfd->signatured_types = std::move (sig_types_hash);
2417 }
2418
2419 /* Create the signatured type hash table from .debug_names. */
2420
2421 static void
2422 create_signatured_type_table_from_debug_names
2423 (dwarf2_per_objfile *per_objfile,
2424 const mapped_debug_names &map,
2425 struct dwarf2_section_info *section,
2426 struct dwarf2_section_info *abbrev_section)
2427 {
2428 struct objfile *objfile = per_objfile->objfile;
2429
2430 section->read (objfile);
2431 abbrev_section->read (objfile);
2432
2433 htab_up sig_types_hash = allocate_signatured_type_table ();
2434
2435 for (uint32_t i = 0; i < map.tu_count; ++i)
2436 {
2437 std::unique_ptr<signatured_type> sig_type;
2438 void **slot;
2439
2440 sect_offset sect_off
2441 = (sect_offset) (extract_unsigned_integer
2442 (map.tu_table_reordered + i * map.offset_size,
2443 map.offset_size,
2444 map.dwarf5_byte_order));
2445
2446 comp_unit_head cu_header;
2447 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2448 abbrev_section,
2449 section->buffer + to_underlying (sect_off),
2450 rcuh_kind::TYPE);
2451
2452 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2453 sig_type->signature = cu_header.signature;
2454 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2455 sig_type->is_debug_types = 1;
2456 sig_type->section = section;
2457 sig_type->sect_off = sect_off;
2458 sig_type->v.quick
2459 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2460 struct dwarf2_per_cu_quick_data);
2461
2462 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2463 *slot = sig_type.get ();
2464
2465 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2466 }
2467
2468 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2469 }
2470
2471 /* Read the address map data from the mapped index, and use it to
2472 populate the psymtabs_addrmap. */
2473
2474 static void
2475 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2476 struct mapped_index *index)
2477 {
2478 struct objfile *objfile = per_objfile->objfile;
2479 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2480 struct gdbarch *gdbarch = objfile->arch ();
2481 const gdb_byte *iter, *end;
2482 struct addrmap *mutable_map;
2483 CORE_ADDR baseaddr;
2484
2485 auto_obstack temp_obstack;
2486
2487 mutable_map = addrmap_create_mutable (&temp_obstack);
2488
2489 iter = index->address_table.data ();
2490 end = iter + index->address_table.size ();
2491
2492 baseaddr = objfile->text_section_offset ();
2493
2494 while (iter < end)
2495 {
2496 ULONGEST hi, lo, cu_index;
2497 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2498 iter += 8;
2499 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2500 iter += 8;
2501 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2502 iter += 4;
2503
2504 if (lo > hi)
2505 {
2506 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2507 hex_string (lo), hex_string (hi));
2508 continue;
2509 }
2510
2511 if (cu_index >= per_bfd->all_comp_units.size ())
2512 {
2513 complaint (_(".gdb_index address table has invalid CU number %u"),
2514 (unsigned) cu_index);
2515 continue;
2516 }
2517
2518 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2519 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2520 addrmap_set_empty (mutable_map, lo, hi - 1,
2521 per_bfd->get_cu (cu_index));
2522 }
2523
2524 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2525 &per_bfd->obstack);
2526 }
2527
2528 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2529 populate the psymtabs_addrmap. */
2530
2531 static void
2532 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2533 struct dwarf2_section_info *section)
2534 {
2535 struct objfile *objfile = per_objfile->objfile;
2536 bfd *abfd = objfile->obfd;
2537 struct gdbarch *gdbarch = objfile->arch ();
2538 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2539 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2540
2541 auto_obstack temp_obstack;
2542 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2543
2544 std::unordered_map<sect_offset,
2545 dwarf2_per_cu_data *,
2546 gdb::hash_enum<sect_offset>>
2547 debug_info_offset_to_per_cu;
2548 for (const auto &per_cu : per_bfd->all_comp_units)
2549 {
2550 const auto insertpair
2551 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2552 per_cu.get ());
2553 if (!insertpair.second)
2554 {
2555 warning (_("Section .debug_aranges in %s has duplicate "
2556 "debug_info_offset %s, ignoring .debug_aranges."),
2557 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2558 return;
2559 }
2560 }
2561
2562 section->read (objfile);
2563
2564 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2565
2566 const gdb_byte *addr = section->buffer;
2567
2568 while (addr < section->buffer + section->size)
2569 {
2570 const gdb_byte *const entry_addr = addr;
2571 unsigned int bytes_read;
2572
2573 const LONGEST entry_length = read_initial_length (abfd, addr,
2574 &bytes_read);
2575 addr += bytes_read;
2576
2577 const gdb_byte *const entry_end = addr + entry_length;
2578 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2579 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2580 if (addr + entry_length > section->buffer + section->size)
2581 {
2582 warning (_("Section .debug_aranges in %s entry at offset %s "
2583 "length %s exceeds section length %s, "
2584 "ignoring .debug_aranges."),
2585 objfile_name (objfile),
2586 plongest (entry_addr - section->buffer),
2587 plongest (bytes_read + entry_length),
2588 pulongest (section->size));
2589 return;
2590 }
2591
2592 /* The version number. */
2593 const uint16_t version = read_2_bytes (abfd, addr);
2594 addr += 2;
2595 if (version != 2)
2596 {
2597 warning (_("Section .debug_aranges in %s entry at offset %s "
2598 "has unsupported version %d, ignoring .debug_aranges."),
2599 objfile_name (objfile),
2600 plongest (entry_addr - section->buffer), version);
2601 return;
2602 }
2603
2604 const uint64_t debug_info_offset
2605 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2606 addr += offset_size;
2607 const auto per_cu_it
2608 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2609 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2610 {
2611 warning (_("Section .debug_aranges in %s entry at offset %s "
2612 "debug_info_offset %s does not exists, "
2613 "ignoring .debug_aranges."),
2614 objfile_name (objfile),
2615 plongest (entry_addr - section->buffer),
2616 pulongest (debug_info_offset));
2617 return;
2618 }
2619 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2620
2621 const uint8_t address_size = *addr++;
2622 if (address_size < 1 || address_size > 8)
2623 {
2624 warning (_("Section .debug_aranges in %s entry at offset %s "
2625 "address_size %u is invalid, ignoring .debug_aranges."),
2626 objfile_name (objfile),
2627 plongest (entry_addr - section->buffer), address_size);
2628 return;
2629 }
2630
2631 const uint8_t segment_selector_size = *addr++;
2632 if (segment_selector_size != 0)
2633 {
2634 warning (_("Section .debug_aranges in %s entry at offset %s "
2635 "segment_selector_size %u is not supported, "
2636 "ignoring .debug_aranges."),
2637 objfile_name (objfile),
2638 plongest (entry_addr - section->buffer),
2639 segment_selector_size);
2640 return;
2641 }
2642
2643 /* Must pad to an alignment boundary that is twice the address
2644 size. It is undocumented by the DWARF standard but GCC does
2645 use it. */
2646 for (size_t padding = ((-(addr - section->buffer))
2647 & (2 * address_size - 1));
2648 padding > 0; padding--)
2649 if (*addr++ != 0)
2650 {
2651 warning (_("Section .debug_aranges in %s entry at offset %s "
2652 "padding is not zero, ignoring .debug_aranges."),
2653 objfile_name (objfile),
2654 plongest (entry_addr - section->buffer));
2655 return;
2656 }
2657
2658 for (;;)
2659 {
2660 if (addr + 2 * address_size > entry_end)
2661 {
2662 warning (_("Section .debug_aranges in %s entry at offset %s "
2663 "address list is not properly terminated, "
2664 "ignoring .debug_aranges."),
2665 objfile_name (objfile),
2666 plongest (entry_addr - section->buffer));
2667 return;
2668 }
2669 ULONGEST start = extract_unsigned_integer (addr, address_size,
2670 dwarf5_byte_order);
2671 addr += address_size;
2672 ULONGEST length = extract_unsigned_integer (addr, address_size,
2673 dwarf5_byte_order);
2674 addr += address_size;
2675 if (start == 0 && length == 0)
2676 break;
2677 if (start == 0 && !per_bfd->has_section_at_zero)
2678 {
2679 /* Symbol was eliminated due to a COMDAT group. */
2680 continue;
2681 }
2682 ULONGEST end = start + length;
2683 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2684 - baseaddr);
2685 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2686 - baseaddr);
2687 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2688 }
2689 }
2690
2691 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2692 &per_bfd->obstack);
2693 }
2694
2695 /* A helper function that reads the .gdb_index from BUFFER and fills
2696 in MAP. FILENAME is the name of the file containing the data;
2697 it is used for error reporting. DEPRECATED_OK is true if it is
2698 ok to use deprecated sections.
2699
2700 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2701 out parameters that are filled in with information about the CU and
2702 TU lists in the section.
2703
2704 Returns true if all went well, false otherwise. */
2705
2706 static bool
2707 read_gdb_index_from_buffer (const char *filename,
2708 bool deprecated_ok,
2709 gdb::array_view<const gdb_byte> buffer,
2710 struct mapped_index *map,
2711 const gdb_byte **cu_list,
2712 offset_type *cu_list_elements,
2713 const gdb_byte **types_list,
2714 offset_type *types_list_elements)
2715 {
2716 const gdb_byte *addr = &buffer[0];
2717 offset_view metadata (buffer);
2718
2719 /* Version check. */
2720 offset_type version = metadata[0];
2721 /* Versions earlier than 3 emitted every copy of a psymbol. This
2722 causes the index to behave very poorly for certain requests. Version 3
2723 contained incomplete addrmap. So, it seems better to just ignore such
2724 indices. */
2725 if (version < 4)
2726 {
2727 static int warning_printed = 0;
2728 if (!warning_printed)
2729 {
2730 warning (_("Skipping obsolete .gdb_index section in %s."),
2731 filename);
2732 warning_printed = 1;
2733 }
2734 return 0;
2735 }
2736 /* Index version 4 uses a different hash function than index version
2737 5 and later.
2738
2739 Versions earlier than 6 did not emit psymbols for inlined
2740 functions. Using these files will cause GDB not to be able to
2741 set breakpoints on inlined functions by name, so we ignore these
2742 indices unless the user has done
2743 "set use-deprecated-index-sections on". */
2744 if (version < 6 && !deprecated_ok)
2745 {
2746 static int warning_printed = 0;
2747 if (!warning_printed)
2748 {
2749 warning (_("\
2750 Skipping deprecated .gdb_index section in %s.\n\
2751 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2752 to use the section anyway."),
2753 filename);
2754 warning_printed = 1;
2755 }
2756 return 0;
2757 }
2758 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2759 of the TU (for symbols coming from TUs),
2760 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2761 Plus gold-generated indices can have duplicate entries for global symbols,
2762 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2763 These are just performance bugs, and we can't distinguish gdb-generated
2764 indices from gold-generated ones, so issue no warning here. */
2765
2766 /* Indexes with higher version than the one supported by GDB may be no
2767 longer backward compatible. */
2768 if (version > 8)
2769 return 0;
2770
2771 map->version = version;
2772
2773 int i = 1;
2774 *cu_list = addr + metadata[i];
2775 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2776 ++i;
2777
2778 *types_list = addr + metadata[i];
2779 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2780 ++i;
2781
2782 const gdb_byte *address_table = addr + metadata[i];
2783 const gdb_byte *address_table_end = addr + metadata[i + 1];
2784 map->address_table
2785 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2786 ++i;
2787
2788 const gdb_byte *symbol_table = addr + metadata[i];
2789 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2790 map->symbol_table
2791 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2792 symbol_table_end));
2793
2794 ++i;
2795 map->constant_pool = buffer.slice (metadata[i]);
2796
2797 return 1;
2798 }
2799
2800 /* Callback types for dwarf2_read_gdb_index. */
2801
2802 typedef gdb::function_view
2803 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2804 get_gdb_index_contents_ftype;
2805 typedef gdb::function_view
2806 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2807 get_gdb_index_contents_dwz_ftype;
2808
2809 /* Read .gdb_index. If everything went ok, initialize the "quick"
2810 elements of all the CUs and return 1. Otherwise, return 0. */
2811
2812 static int
2813 dwarf2_read_gdb_index
2814 (dwarf2_per_objfile *per_objfile,
2815 get_gdb_index_contents_ftype get_gdb_index_contents,
2816 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2817 {
2818 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2819 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2820 struct dwz_file *dwz;
2821 struct objfile *objfile = per_objfile->objfile;
2822 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2823
2824 gdb::array_view<const gdb_byte> main_index_contents
2825 = get_gdb_index_contents (objfile, per_bfd);
2826
2827 if (main_index_contents.empty ())
2828 return 0;
2829
2830 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2831 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2832 use_deprecated_index_sections,
2833 main_index_contents, map.get (), &cu_list,
2834 &cu_list_elements, &types_list,
2835 &types_list_elements))
2836 return 0;
2837
2838 /* Don't use the index if it's empty. */
2839 if (map->symbol_table.empty ())
2840 return 0;
2841
2842 /* If there is a .dwz file, read it so we can get its CU list as
2843 well. */
2844 dwz = dwarf2_get_dwz_file (per_bfd);
2845 if (dwz != NULL)
2846 {
2847 struct mapped_index dwz_map;
2848 const gdb_byte *dwz_types_ignore;
2849 offset_type dwz_types_elements_ignore;
2850
2851 gdb::array_view<const gdb_byte> dwz_index_content
2852 = get_gdb_index_contents_dwz (objfile, dwz);
2853
2854 if (dwz_index_content.empty ())
2855 return 0;
2856
2857 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2858 1, dwz_index_content, &dwz_map,
2859 &dwz_list, &dwz_list_elements,
2860 &dwz_types_ignore,
2861 &dwz_types_elements_ignore))
2862 {
2863 warning (_("could not read '.gdb_index' section from %s; skipping"),
2864 bfd_get_filename (dwz->dwz_bfd.get ()));
2865 return 0;
2866 }
2867 }
2868
2869 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2870 dwz_list_elements);
2871
2872 if (types_list_elements)
2873 {
2874 /* We can only handle a single .debug_types when we have an
2875 index. */
2876 if (per_bfd->types.size () != 1)
2877 return 0;
2878
2879 dwarf2_section_info *section = &per_bfd->types[0];
2880
2881 create_signatured_type_table_from_index (per_bfd, section, types_list,
2882 types_list_elements);
2883 }
2884
2885 create_addrmap_from_index (per_objfile, map.get ());
2886
2887 per_bfd->index_table = std::move (map);
2888 per_bfd->using_index = 1;
2889 per_bfd->quick_file_names_table =
2890 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2891
2892 return 1;
2893 }
2894
2895 /* die_reader_func for dw2_get_file_names. */
2896
2897 static void
2898 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2899 struct die_info *comp_unit_die)
2900 {
2901 struct dwarf2_cu *cu = reader->cu;
2902 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2903 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2904 struct dwarf2_per_cu_data *lh_cu;
2905 struct attribute *attr;
2906 void **slot;
2907 struct quick_file_names *qfn;
2908
2909 gdb_assert (! this_cu->is_debug_types);
2910
2911 /* Our callers never want to match partial units -- instead they
2912 will match the enclosing full CU. */
2913 if (comp_unit_die->tag == DW_TAG_partial_unit)
2914 {
2915 this_cu->v.quick->no_file_data = 1;
2916 return;
2917 }
2918
2919 lh_cu = this_cu;
2920 slot = NULL;
2921
2922 line_header_up lh;
2923 sect_offset line_offset {};
2924
2925 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2926 if (attr != nullptr && attr->form_is_unsigned ())
2927 {
2928 struct quick_file_names find_entry;
2929
2930 line_offset = (sect_offset) attr->as_unsigned ();
2931
2932 /* We may have already read in this line header (TU line header sharing).
2933 If we have we're done. */
2934 find_entry.hash.dwo_unit = cu->dwo_unit;
2935 find_entry.hash.line_sect_off = line_offset;
2936 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2937 &find_entry, INSERT);
2938 if (*slot != NULL)
2939 {
2940 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
2941 return;
2942 }
2943
2944 lh = dwarf_decode_line_header (line_offset, cu);
2945 }
2946 if (lh == NULL)
2947 {
2948 lh_cu->v.quick->no_file_data = 1;
2949 return;
2950 }
2951
2952 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2953 qfn->hash.dwo_unit = cu->dwo_unit;
2954 qfn->hash.line_sect_off = line_offset;
2955 gdb_assert (slot != NULL);
2956 *slot = qfn;
2957
2958 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
2959
2960 int offset = 0;
2961 if (strcmp (fnd.name, "<unknown>") != 0)
2962 ++offset;
2963
2964 qfn->num_file_names = offset + lh->file_names_size ();
2965 qfn->file_names =
2966 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2967 qfn->num_file_names);
2968 if (offset != 0)
2969 qfn->file_names[0] = xstrdup (fnd.name);
2970 for (int i = 0; i < lh->file_names_size (); ++i)
2971 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
2972 fnd.comp_dir).release ();
2973 qfn->real_names = NULL;
2974
2975 lh_cu->v.quick->file_names = qfn;
2976 }
2977
2978 /* A helper for the "quick" functions which attempts to read the line
2979 table for THIS_CU. */
2980
2981 static struct quick_file_names *
2982 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2983 dwarf2_per_objfile *per_objfile)
2984 {
2985 /* This should never be called for TUs. */
2986 gdb_assert (! this_cu->is_debug_types);
2987 /* Nor type unit groups. */
2988 gdb_assert (! this_cu->type_unit_group_p ());
2989
2990 if (this_cu->v.quick->file_names != NULL)
2991 return this_cu->v.quick->file_names;
2992 /* If we know there is no line data, no point in looking again. */
2993 if (this_cu->v.quick->no_file_data)
2994 return NULL;
2995
2996 cutu_reader reader (this_cu, per_objfile);
2997 if (!reader.dummy_p)
2998 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2999
3000 if (this_cu->v.quick->no_file_data)
3001 return NULL;
3002 return this_cu->v.quick->file_names;
3003 }
3004
3005 /* A helper for the "quick" functions which computes and caches the
3006 real path for a given file name from the line table. */
3007
3008 static const char *
3009 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3010 struct quick_file_names *qfn, int index)
3011 {
3012 if (qfn->real_names == NULL)
3013 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3014 qfn->num_file_names, const char *);
3015
3016 if (qfn->real_names[index] == NULL)
3017 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3018
3019 return qfn->real_names[index];
3020 }
3021
3022 struct symtab *
3023 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3024 {
3025 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3026 dwarf2_per_cu_data *dwarf_cu
3027 = per_objfile->per_bfd->all_comp_units.back ().get ();
3028 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3029
3030 if (cust == NULL)
3031 return NULL;
3032
3033 return compunit_primary_filetab (cust);
3034 }
3035
3036 /* Traversal function for dw2_forget_cached_source_info. */
3037
3038 static int
3039 dw2_free_cached_file_names (void **slot, void *info)
3040 {
3041 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3042
3043 if (file_data->real_names)
3044 {
3045 int i;
3046
3047 for (i = 0; i < file_data->num_file_names; ++i)
3048 {
3049 xfree ((void*) file_data->real_names[i]);
3050 file_data->real_names[i] = NULL;
3051 }
3052 }
3053
3054 return 1;
3055 }
3056
3057 void
3058 dwarf2_base_index_functions::forget_cached_source_info
3059 (struct objfile *objfile)
3060 {
3061 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3062
3063 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3064 dw2_free_cached_file_names, NULL);
3065 }
3066
3067 /* Struct used to manage iterating over all CUs looking for a symbol. */
3068
3069 struct dw2_symtab_iterator
3070 {
3071 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3072 dwarf2_per_objfile *per_objfile;
3073 /* If set, only look for symbols that match that block. Valid values are
3074 GLOBAL_BLOCK and STATIC_BLOCK. */
3075 gdb::optional<block_enum> block_index;
3076 /* The kind of symbol we're looking for. */
3077 domain_enum domain;
3078 /* The list of CUs from the index entry of the symbol,
3079 or NULL if not found. */
3080 offset_view vec;
3081 /* The next element in VEC to look at. */
3082 int next;
3083 /* The number of elements in VEC, or zero if there is no match. */
3084 int length;
3085 /* Have we seen a global version of the symbol?
3086 If so we can ignore all further global instances.
3087 This is to work around gold/15646, inefficient gold-generated
3088 indices. */
3089 int global_seen;
3090 };
3091
3092 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3093
3094 static void
3095 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3096 dwarf2_per_objfile *per_objfile,
3097 gdb::optional<block_enum> block_index,
3098 domain_enum domain, offset_type namei)
3099 {
3100 iter->per_objfile = per_objfile;
3101 iter->block_index = block_index;
3102 iter->domain = domain;
3103 iter->next = 0;
3104 iter->global_seen = 0;
3105 iter->vec = {};
3106 iter->length = 0;
3107
3108 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3109 /* index is NULL if OBJF_READNOW. */
3110 if (index == NULL)
3111 return;
3112
3113 gdb_assert (!index->symbol_name_slot_invalid (namei));
3114 offset_type vec_idx = index->symbol_vec_index (namei);
3115
3116 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3117 iter->length = iter->vec[0];
3118 }
3119
3120 /* Return the next matching CU or NULL if there are no more. */
3121
3122 static struct dwarf2_per_cu_data *
3123 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3124 {
3125 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3126
3127 for ( ; iter->next < iter->length; ++iter->next)
3128 {
3129 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3130 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3131 gdb_index_symbol_kind symbol_kind =
3132 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3133 /* Only check the symbol attributes if they're present.
3134 Indices prior to version 7 don't record them,
3135 and indices >= 7 may elide them for certain symbols
3136 (gold does this). */
3137 int attrs_valid =
3138 (per_objfile->per_bfd->index_table->version >= 7
3139 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3140
3141 /* Don't crash on bad data. */
3142 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3143 {
3144 complaint (_(".gdb_index entry has bad CU index"
3145 " [in module %s]"), objfile_name (per_objfile->objfile));
3146 continue;
3147 }
3148
3149 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3150
3151 /* Skip if already read in. */
3152 if (per_objfile->symtab_set_p (per_cu))
3153 continue;
3154
3155 /* Check static vs global. */
3156 if (attrs_valid)
3157 {
3158 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3159
3160 if (iter->block_index.has_value ())
3161 {
3162 bool want_static = *iter->block_index == STATIC_BLOCK;
3163
3164 if (is_static != want_static)
3165 continue;
3166 }
3167
3168 /* Work around gold/15646. */
3169 if (!is_static
3170 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3171 {
3172 if (iter->global_seen)
3173 continue;
3174
3175 iter->global_seen = 1;
3176 }
3177 }
3178
3179 /* Only check the symbol's kind if it has one. */
3180 if (attrs_valid)
3181 {
3182 switch (iter->domain)
3183 {
3184 case VAR_DOMAIN:
3185 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3186 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3187 /* Some types are also in VAR_DOMAIN. */
3188 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3189 continue;
3190 break;
3191 case STRUCT_DOMAIN:
3192 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3193 continue;
3194 break;
3195 case LABEL_DOMAIN:
3196 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3197 continue;
3198 break;
3199 case MODULE_DOMAIN:
3200 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3201 continue;
3202 break;
3203 default:
3204 break;
3205 }
3206 }
3207
3208 ++iter->next;
3209 return per_cu;
3210 }
3211
3212 return NULL;
3213 }
3214
3215 void
3216 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3217 bool print_bcache)
3218 {
3219 if (print_bcache)
3220 return;
3221
3222 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3223 int total = per_objfile->per_bfd->all_comp_units.size ();
3224 int count = 0;
3225
3226 for (int i = 0; i < total; ++i)
3227 {
3228 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3229
3230 if (!per_objfile->symtab_set_p (per_cu))
3231 ++count;
3232 }
3233 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3234 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3235 }
3236
3237 /* This dumps minimal information about the index.
3238 It is called via "mt print objfiles".
3239 One use is to verify .gdb_index has been loaded by the
3240 gdb.dwarf2/gdb-index.exp testcase. */
3241
3242 void
3243 dwarf2_gdb_index::dump (struct objfile *objfile)
3244 {
3245 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3246
3247 gdb_assert (per_objfile->per_bfd->using_index);
3248 printf_filtered (".gdb_index:");
3249 if (per_objfile->per_bfd->index_table != NULL)
3250 {
3251 printf_filtered (" version %d\n",
3252 per_objfile->per_bfd->index_table->version);
3253 }
3254 else
3255 printf_filtered (" faked for \"readnow\"\n");
3256 printf_filtered ("\n");
3257 }
3258
3259 void
3260 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3261 {
3262 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3263 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3264
3265 for (int i = 0; i < total_units; ++i)
3266 {
3267 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3268
3269 /* We don't want to directly expand a partial CU, because if we
3270 read it with the wrong language, then assertion failures can
3271 be triggered later on. See PR symtab/23010. So, tell
3272 dw2_instantiate_symtab to skip partial CUs -- any important
3273 partial CU will be read via DW_TAG_imported_unit anyway. */
3274 dw2_instantiate_symtab (per_cu, per_objfile, true);
3275 }
3276 }
3277
3278 static bool
3279 dw2_expand_symtabs_matching_symbol
3280 (mapped_index_base &index,
3281 const lookup_name_info &lookup_name_in,
3282 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3283 gdb::function_view<bool (offset_type)> match_callback,
3284 dwarf2_per_objfile *per_objfile);
3285
3286 static bool
3287 dw2_expand_symtabs_matching_one
3288 (dwarf2_per_cu_data *per_cu,
3289 dwarf2_per_objfile *per_objfile,
3290 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3291 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3292
3293 void
3294 dwarf2_gdb_index::expand_matching_symbols
3295 (struct objfile *objfile,
3296 const lookup_name_info &name, domain_enum domain,
3297 int global,
3298 symbol_compare_ftype *ordered_compare)
3299 {
3300 /* Used for Ada. */
3301 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3302
3303 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3304
3305 if (per_objfile->per_bfd->index_table != nullptr)
3306 {
3307 mapped_index &index = *per_objfile->per_bfd->index_table;
3308
3309 const char *match_name = name.ada ().lookup_name ().c_str ();
3310 auto matcher = [&] (const char *symname)
3311 {
3312 if (ordered_compare == nullptr)
3313 return true;
3314 return ordered_compare (symname, match_name) == 0;
3315 };
3316
3317 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3318 [&] (offset_type namei)
3319 {
3320 struct dw2_symtab_iterator iter;
3321 struct dwarf2_per_cu_data *per_cu;
3322
3323 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3324 namei);
3325 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3326 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3327 nullptr);
3328 return true;
3329 }, per_objfile);
3330 }
3331 else
3332 {
3333 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3334 proceed assuming all symtabs have been read in. */
3335 }
3336 }
3337
3338 /* Starting from a search name, return the string that finds the upper
3339 bound of all strings that start with SEARCH_NAME in a sorted name
3340 list. Returns the empty string to indicate that the upper bound is
3341 the end of the list. */
3342
3343 static std::string
3344 make_sort_after_prefix_name (const char *search_name)
3345 {
3346 /* When looking to complete "func", we find the upper bound of all
3347 symbols that start with "func" by looking for where we'd insert
3348 the closest string that would follow "func" in lexicographical
3349 order. Usually, that's "func"-with-last-character-incremented,
3350 i.e. "fund". Mind non-ASCII characters, though. Usually those
3351 will be UTF-8 multi-byte sequences, but we can't be certain.
3352 Especially mind the 0xff character, which is a valid character in
3353 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3354 rule out compilers allowing it in identifiers. Note that
3355 conveniently, strcmp/strcasecmp are specified to compare
3356 characters interpreted as unsigned char. So what we do is treat
3357 the whole string as a base 256 number composed of a sequence of
3358 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3359 to 0, and carries 1 to the following more-significant position.
3360 If the very first character in SEARCH_NAME ends up incremented
3361 and carries/overflows, then the upper bound is the end of the
3362 list. The string after the empty string is also the empty
3363 string.
3364
3365 Some examples of this operation:
3366
3367 SEARCH_NAME => "+1" RESULT
3368
3369 "abc" => "abd"
3370 "ab\xff" => "ac"
3371 "\xff" "a" "\xff" => "\xff" "b"
3372 "\xff" => ""
3373 "\xff\xff" => ""
3374 "" => ""
3375
3376 Then, with these symbols for example:
3377
3378 func
3379 func1
3380 fund
3381
3382 completing "func" looks for symbols between "func" and
3383 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3384 which finds "func" and "func1", but not "fund".
3385
3386 And with:
3387
3388 funcÿ (Latin1 'ÿ' [0xff])
3389 funcÿ1
3390 fund
3391
3392 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3393 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3394
3395 And with:
3396
3397 ÿÿ (Latin1 'ÿ' [0xff])
3398 ÿÿ1
3399
3400 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3401 the end of the list.
3402 */
3403 std::string after = search_name;
3404 while (!after.empty () && (unsigned char) after.back () == 0xff)
3405 after.pop_back ();
3406 if (!after.empty ())
3407 after.back () = (unsigned char) after.back () + 1;
3408 return after;
3409 }
3410
3411 /* See declaration. */
3412
3413 std::pair<std::vector<name_component>::const_iterator,
3414 std::vector<name_component>::const_iterator>
3415 mapped_index_base::find_name_components_bounds
3416 (const lookup_name_info &lookup_name_without_params, language lang,
3417 dwarf2_per_objfile *per_objfile) const
3418 {
3419 auto *name_cmp
3420 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3421
3422 const char *lang_name
3423 = lookup_name_without_params.language_lookup_name (lang);
3424
3425 /* Comparison function object for lower_bound that matches against a
3426 given symbol name. */
3427 auto lookup_compare_lower = [&] (const name_component &elem,
3428 const char *name)
3429 {
3430 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3431 const char *elem_name = elem_qualified + elem.name_offset;
3432 return name_cmp (elem_name, name) < 0;
3433 };
3434
3435 /* Comparison function object for upper_bound that matches against a
3436 given symbol name. */
3437 auto lookup_compare_upper = [&] (const char *name,
3438 const name_component &elem)
3439 {
3440 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3441 const char *elem_name = elem_qualified + elem.name_offset;
3442 return name_cmp (name, elem_name) < 0;
3443 };
3444
3445 auto begin = this->name_components.begin ();
3446 auto end = this->name_components.end ();
3447
3448 /* Find the lower bound. */
3449 auto lower = [&] ()
3450 {
3451 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3452 return begin;
3453 else
3454 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3455 } ();
3456
3457 /* Find the upper bound. */
3458 auto upper = [&] ()
3459 {
3460 if (lookup_name_without_params.completion_mode ())
3461 {
3462 /* In completion mode, we want UPPER to point past all
3463 symbols names that have the same prefix. I.e., with
3464 these symbols, and completing "func":
3465
3466 function << lower bound
3467 function1
3468 other_function << upper bound
3469
3470 We find the upper bound by looking for the insertion
3471 point of "func"-with-last-character-incremented,
3472 i.e. "fund". */
3473 std::string after = make_sort_after_prefix_name (lang_name);
3474 if (after.empty ())
3475 return end;
3476 return std::lower_bound (lower, end, after.c_str (),
3477 lookup_compare_lower);
3478 }
3479 else
3480 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3481 } ();
3482
3483 return {lower, upper};
3484 }
3485
3486 /* See declaration. */
3487
3488 void
3489 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3490 {
3491 if (!this->name_components.empty ())
3492 return;
3493
3494 this->name_components_casing = case_sensitivity;
3495 auto *name_cmp
3496 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3497
3498 /* The code below only knows how to break apart components of C++
3499 symbol names (and other languages that use '::' as
3500 namespace/module separator) and Ada symbol names. */
3501 auto count = this->symbol_name_count ();
3502 for (offset_type idx = 0; idx < count; idx++)
3503 {
3504 if (this->symbol_name_slot_invalid (idx))
3505 continue;
3506
3507 const char *name = this->symbol_name_at (idx, per_objfile);
3508
3509 /* Add each name component to the name component table. */
3510 unsigned int previous_len = 0;
3511
3512 if (strstr (name, "::") != nullptr)
3513 {
3514 for (unsigned int current_len = cp_find_first_component (name);
3515 name[current_len] != '\0';
3516 current_len += cp_find_first_component (name + current_len))
3517 {
3518 gdb_assert (name[current_len] == ':');
3519 this->name_components.push_back ({previous_len, idx});
3520 /* Skip the '::'. */
3521 current_len += 2;
3522 previous_len = current_len;
3523 }
3524 }
3525 else
3526 {
3527 /* Handle the Ada encoded (aka mangled) form here. */
3528 for (const char *iter = strstr (name, "__");
3529 iter != nullptr;
3530 iter = strstr (iter, "__"))
3531 {
3532 this->name_components.push_back ({previous_len, idx});
3533 iter += 2;
3534 previous_len = iter - name;
3535 }
3536 }
3537
3538 this->name_components.push_back ({previous_len, idx});
3539 }
3540
3541 /* Sort name_components elements by name. */
3542 auto name_comp_compare = [&] (const name_component &left,
3543 const name_component &right)
3544 {
3545 const char *left_qualified
3546 = this->symbol_name_at (left.idx, per_objfile);
3547 const char *right_qualified
3548 = this->symbol_name_at (right.idx, per_objfile);
3549
3550 const char *left_name = left_qualified + left.name_offset;
3551 const char *right_name = right_qualified + right.name_offset;
3552
3553 return name_cmp (left_name, right_name) < 0;
3554 };
3555
3556 std::sort (this->name_components.begin (),
3557 this->name_components.end (),
3558 name_comp_compare);
3559 }
3560
3561 /* Helper for dw2_expand_symtabs_matching that works with a
3562 mapped_index_base instead of the containing objfile. This is split
3563 to a separate function in order to be able to unit test the
3564 name_components matching using a mock mapped_index_base. For each
3565 symbol name that matches, calls MATCH_CALLBACK, passing it the
3566 symbol's index in the mapped_index_base symbol table. */
3567
3568 static bool
3569 dw2_expand_symtabs_matching_symbol
3570 (mapped_index_base &index,
3571 const lookup_name_info &lookup_name_in,
3572 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3573 gdb::function_view<bool (offset_type)> match_callback,
3574 dwarf2_per_objfile *per_objfile)
3575 {
3576 lookup_name_info lookup_name_without_params
3577 = lookup_name_in.make_ignore_params ();
3578
3579 /* Build the symbol name component sorted vector, if we haven't
3580 yet. */
3581 index.build_name_components (per_objfile);
3582
3583 /* The same symbol may appear more than once in the range though.
3584 E.g., if we're looking for symbols that complete "w", and we have
3585 a symbol named "w1::w2", we'll find the two name components for
3586 that same symbol in the range. To be sure we only call the
3587 callback once per symbol, we first collect the symbol name
3588 indexes that matched in a temporary vector and ignore
3589 duplicates. */
3590 std::vector<offset_type> matches;
3591
3592 struct name_and_matcher
3593 {
3594 symbol_name_matcher_ftype *matcher;
3595 const char *name;
3596
3597 bool operator== (const name_and_matcher &other) const
3598 {
3599 return matcher == other.matcher && strcmp (name, other.name) == 0;
3600 }
3601 };
3602
3603 /* A vector holding all the different symbol name matchers, for all
3604 languages. */
3605 std::vector<name_and_matcher> matchers;
3606
3607 for (int i = 0; i < nr_languages; i++)
3608 {
3609 enum language lang_e = (enum language) i;
3610
3611 const language_defn *lang = language_def (lang_e);
3612 symbol_name_matcher_ftype *name_matcher
3613 = lang->get_symbol_name_matcher (lookup_name_without_params);
3614
3615 name_and_matcher key {
3616 name_matcher,
3617 lookup_name_without_params.language_lookup_name (lang_e)
3618 };
3619
3620 /* Don't insert the same comparison routine more than once.
3621 Note that we do this linear walk. This is not a problem in
3622 practice because the number of supported languages is
3623 low. */
3624 if (std::find (matchers.begin (), matchers.end (), key)
3625 != matchers.end ())
3626 continue;
3627 matchers.push_back (std::move (key));
3628
3629 auto bounds
3630 = index.find_name_components_bounds (lookup_name_without_params,
3631 lang_e, per_objfile);
3632
3633 /* Now for each symbol name in range, check to see if we have a name
3634 match, and if so, call the MATCH_CALLBACK callback. */
3635
3636 for (; bounds.first != bounds.second; ++bounds.first)
3637 {
3638 const char *qualified
3639 = index.symbol_name_at (bounds.first->idx, per_objfile);
3640
3641 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3642 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3643 continue;
3644
3645 matches.push_back (bounds.first->idx);
3646 }
3647 }
3648
3649 std::sort (matches.begin (), matches.end ());
3650
3651 /* Finally call the callback, once per match. */
3652 ULONGEST prev = -1;
3653 bool result = true;
3654 for (offset_type idx : matches)
3655 {
3656 if (prev != idx)
3657 {
3658 if (!match_callback (idx))
3659 {
3660 result = false;
3661 break;
3662 }
3663 prev = idx;
3664 }
3665 }
3666
3667 /* Above we use a type wider than idx's for 'prev', since 0 and
3668 (offset_type)-1 are both possible values. */
3669 static_assert (sizeof (prev) > sizeof (offset_type), "");
3670
3671 return result;
3672 }
3673
3674 #if GDB_SELF_TEST
3675
3676 namespace selftests { namespace dw2_expand_symtabs_matching {
3677
3678 /* A mock .gdb_index/.debug_names-like name index table, enough to
3679 exercise dw2_expand_symtabs_matching_symbol, which works with the
3680 mapped_index_base interface. Builds an index from the symbol list
3681 passed as parameter to the constructor. */
3682 class mock_mapped_index : public mapped_index_base
3683 {
3684 public:
3685 mock_mapped_index (gdb::array_view<const char *> symbols)
3686 : m_symbol_table (symbols)
3687 {}
3688
3689 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3690
3691 /* Return the number of names in the symbol table. */
3692 size_t symbol_name_count () const override
3693 {
3694 return m_symbol_table.size ();
3695 }
3696
3697 /* Get the name of the symbol at IDX in the symbol table. */
3698 const char *symbol_name_at
3699 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3700 {
3701 return m_symbol_table[idx];
3702 }
3703
3704 private:
3705 gdb::array_view<const char *> m_symbol_table;
3706 };
3707
3708 /* Convenience function that converts a NULL pointer to a "<null>"
3709 string, to pass to print routines. */
3710
3711 static const char *
3712 string_or_null (const char *str)
3713 {
3714 return str != NULL ? str : "<null>";
3715 }
3716
3717 /* Check if a lookup_name_info built from
3718 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3719 index. EXPECTED_LIST is the list of expected matches, in expected
3720 matching order. If no match expected, then an empty list is
3721 specified. Returns true on success. On failure prints a warning
3722 indicating the file:line that failed, and returns false. */
3723
3724 static bool
3725 check_match (const char *file, int line,
3726 mock_mapped_index &mock_index,
3727 const char *name, symbol_name_match_type match_type,
3728 bool completion_mode,
3729 std::initializer_list<const char *> expected_list,
3730 dwarf2_per_objfile *per_objfile)
3731 {
3732 lookup_name_info lookup_name (name, match_type, completion_mode);
3733
3734 bool matched = true;
3735
3736 auto mismatch = [&] (const char *expected_str,
3737 const char *got)
3738 {
3739 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3740 "expected=\"%s\", got=\"%s\"\n"),
3741 file, line,
3742 (match_type == symbol_name_match_type::FULL
3743 ? "FULL" : "WILD"),
3744 name, string_or_null (expected_str), string_or_null (got));
3745 matched = false;
3746 };
3747
3748 auto expected_it = expected_list.begin ();
3749 auto expected_end = expected_list.end ();
3750
3751 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3752 nullptr,
3753 [&] (offset_type idx)
3754 {
3755 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3756 const char *expected_str
3757 = expected_it == expected_end ? NULL : *expected_it++;
3758
3759 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3760 mismatch (expected_str, matched_name);
3761 return true;
3762 }, per_objfile);
3763
3764 const char *expected_str
3765 = expected_it == expected_end ? NULL : *expected_it++;
3766 if (expected_str != NULL)
3767 mismatch (expected_str, NULL);
3768
3769 return matched;
3770 }
3771
3772 /* The symbols added to the mock mapped_index for testing (in
3773 canonical form). */
3774 static const char *test_symbols[] = {
3775 "function",
3776 "std::bar",
3777 "std::zfunction",
3778 "std::zfunction2",
3779 "w1::w2",
3780 "ns::foo<char*>",
3781 "ns::foo<int>",
3782 "ns::foo<long>",
3783 "ns2::tmpl<int>::foo2",
3784 "(anonymous namespace)::A::B::C",
3785
3786 /* These are used to check that the increment-last-char in the
3787 matching algorithm for completion doesn't match "t1_fund" when
3788 completing "t1_func". */
3789 "t1_func",
3790 "t1_func1",
3791 "t1_fund",
3792 "t1_fund1",
3793
3794 /* A UTF-8 name with multi-byte sequences to make sure that
3795 cp-name-parser understands this as a single identifier ("função"
3796 is "function" in PT). */
3797 u8"u8função",
3798
3799 /* \377 (0xff) is Latin1 'ÿ'. */
3800 "yfunc\377",
3801
3802 /* \377 (0xff) is Latin1 'ÿ'. */
3803 "\377",
3804 "\377\377123",
3805
3806 /* A name with all sorts of complications. Starts with "z" to make
3807 it easier for the completion tests below. */
3808 #define Z_SYM_NAME \
3809 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3810 "::tuple<(anonymous namespace)::ui*, " \
3811 "std::default_delete<(anonymous namespace)::ui>, void>"
3812
3813 Z_SYM_NAME
3814 };
3815
3816 /* Returns true if the mapped_index_base::find_name_component_bounds
3817 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3818 in completion mode. */
3819
3820 static bool
3821 check_find_bounds_finds (mapped_index_base &index,
3822 const char *search_name,
3823 gdb::array_view<const char *> expected_syms,
3824 dwarf2_per_objfile *per_objfile)
3825 {
3826 lookup_name_info lookup_name (search_name,
3827 symbol_name_match_type::FULL, true);
3828
3829 auto bounds = index.find_name_components_bounds (lookup_name,
3830 language_cplus,
3831 per_objfile);
3832
3833 size_t distance = std::distance (bounds.first, bounds.second);
3834 if (distance != expected_syms.size ())
3835 return false;
3836
3837 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3838 {
3839 auto nc_elem = bounds.first + exp_elem;
3840 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3841 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3842 return false;
3843 }
3844
3845 return true;
3846 }
3847
3848 /* Test the lower-level mapped_index::find_name_component_bounds
3849 method. */
3850
3851 static void
3852 test_mapped_index_find_name_component_bounds ()
3853 {
3854 mock_mapped_index mock_index (test_symbols);
3855
3856 mock_index.build_name_components (NULL /* per_objfile */);
3857
3858 /* Test the lower-level mapped_index::find_name_component_bounds
3859 method in completion mode. */
3860 {
3861 static const char *expected_syms[] = {
3862 "t1_func",
3863 "t1_func1",
3864 };
3865
3866 SELF_CHECK (check_find_bounds_finds
3867 (mock_index, "t1_func", expected_syms,
3868 NULL /* per_objfile */));
3869 }
3870
3871 /* Check that the increment-last-char in the name matching algorithm
3872 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3873 {
3874 static const char *expected_syms1[] = {
3875 "\377",
3876 "\377\377123",
3877 };
3878 SELF_CHECK (check_find_bounds_finds
3879 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3880
3881 static const char *expected_syms2[] = {
3882 "\377\377123",
3883 };
3884 SELF_CHECK (check_find_bounds_finds
3885 (mock_index, "\377\377", expected_syms2,
3886 NULL /* per_objfile */));
3887 }
3888 }
3889
3890 /* Test dw2_expand_symtabs_matching_symbol. */
3891
3892 static void
3893 test_dw2_expand_symtabs_matching_symbol ()
3894 {
3895 mock_mapped_index mock_index (test_symbols);
3896
3897 /* We let all tests run until the end even if some fails, for debug
3898 convenience. */
3899 bool any_mismatch = false;
3900
3901 /* Create the expected symbols list (an initializer_list). Needed
3902 because lists have commas, and we need to pass them to CHECK,
3903 which is a macro. */
3904 #define EXPECT(...) { __VA_ARGS__ }
3905
3906 /* Wrapper for check_match that passes down the current
3907 __FILE__/__LINE__. */
3908 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3909 any_mismatch |= !check_match (__FILE__, __LINE__, \
3910 mock_index, \
3911 NAME, MATCH_TYPE, COMPLETION_MODE, \
3912 EXPECTED_LIST, NULL)
3913
3914 /* Identity checks. */
3915 for (const char *sym : test_symbols)
3916 {
3917 /* Should be able to match all existing symbols. */
3918 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3919 EXPECT (sym));
3920
3921 /* Should be able to match all existing symbols with
3922 parameters. */
3923 std::string with_params = std::string (sym) + "(int)";
3924 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3925 EXPECT (sym));
3926
3927 /* Should be able to match all existing symbols with
3928 parameters and qualifiers. */
3929 with_params = std::string (sym) + " ( int ) const";
3930 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3931 EXPECT (sym));
3932
3933 /* This should really find sym, but cp-name-parser.y doesn't
3934 know about lvalue/rvalue qualifiers yet. */
3935 with_params = std::string (sym) + " ( int ) &&";
3936 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3937 {});
3938 }
3939
3940 /* Check that the name matching algorithm for completion doesn't get
3941 confused with Latin1 'ÿ' / 0xff. */
3942 {
3943 static const char str[] = "\377";
3944 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3945 EXPECT ("\377", "\377\377123"));
3946 }
3947
3948 /* Check that the increment-last-char in the matching algorithm for
3949 completion doesn't match "t1_fund" when completing "t1_func". */
3950 {
3951 static const char str[] = "t1_func";
3952 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3953 EXPECT ("t1_func", "t1_func1"));
3954 }
3955
3956 /* Check that completion mode works at each prefix of the expected
3957 symbol name. */
3958 {
3959 static const char str[] = "function(int)";
3960 size_t len = strlen (str);
3961 std::string lookup;
3962
3963 for (size_t i = 1; i < len; i++)
3964 {
3965 lookup.assign (str, i);
3966 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3967 EXPECT ("function"));
3968 }
3969 }
3970
3971 /* While "w" is a prefix of both components, the match function
3972 should still only be called once. */
3973 {
3974 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3975 EXPECT ("w1::w2"));
3976 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3977 EXPECT ("w1::w2"));
3978 }
3979
3980 /* Same, with a "complicated" symbol. */
3981 {
3982 static const char str[] = Z_SYM_NAME;
3983 size_t len = strlen (str);
3984 std::string lookup;
3985
3986 for (size_t i = 1; i < len; i++)
3987 {
3988 lookup.assign (str, i);
3989 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3990 EXPECT (Z_SYM_NAME));
3991 }
3992 }
3993
3994 /* In FULL mode, an incomplete symbol doesn't match. */
3995 {
3996 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3997 {});
3998 }
3999
4000 /* A complete symbol with parameters matches any overload, since the
4001 index has no overload info. */
4002 {
4003 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4004 EXPECT ("std::zfunction", "std::zfunction2"));
4005 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4006 EXPECT ("std::zfunction", "std::zfunction2"));
4007 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4008 EXPECT ("std::zfunction", "std::zfunction2"));
4009 }
4010
4011 /* Check that whitespace is ignored appropriately. A symbol with a
4012 template argument list. */
4013 {
4014 static const char expected[] = "ns::foo<int>";
4015 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4016 EXPECT (expected));
4017 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4018 EXPECT (expected));
4019 }
4020
4021 /* Check that whitespace is ignored appropriately. A symbol with a
4022 template argument list that includes a pointer. */
4023 {
4024 static const char expected[] = "ns::foo<char*>";
4025 /* Try both completion and non-completion modes. */
4026 static const bool completion_mode[2] = {false, true};
4027 for (size_t i = 0; i < 2; i++)
4028 {
4029 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4030 completion_mode[i], EXPECT (expected));
4031 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4032 completion_mode[i], EXPECT (expected));
4033
4034 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4035 completion_mode[i], EXPECT (expected));
4036 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4037 completion_mode[i], EXPECT (expected));
4038 }
4039 }
4040
4041 {
4042 /* Check method qualifiers are ignored. */
4043 static const char expected[] = "ns::foo<char*>";
4044 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4045 symbol_name_match_type::FULL, true, EXPECT (expected));
4046 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4047 symbol_name_match_type::FULL, true, EXPECT (expected));
4048 CHECK_MATCH ("foo < char * > ( int ) const",
4049 symbol_name_match_type::WILD, true, EXPECT (expected));
4050 CHECK_MATCH ("foo < char * > ( int ) &&",
4051 symbol_name_match_type::WILD, true, EXPECT (expected));
4052 }
4053
4054 /* Test lookup names that don't match anything. */
4055 {
4056 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4057 {});
4058
4059 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4060 {});
4061 }
4062
4063 /* Some wild matching tests, exercising "(anonymous namespace)",
4064 which should not be confused with a parameter list. */
4065 {
4066 static const char *syms[] = {
4067 "A::B::C",
4068 "B::C",
4069 "C",
4070 "A :: B :: C ( int )",
4071 "B :: C ( int )",
4072 "C ( int )",
4073 };
4074
4075 for (const char *s : syms)
4076 {
4077 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4078 EXPECT ("(anonymous namespace)::A::B::C"));
4079 }
4080 }
4081
4082 {
4083 static const char expected[] = "ns2::tmpl<int>::foo2";
4084 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4085 EXPECT (expected));
4086 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4087 EXPECT (expected));
4088 }
4089
4090 SELF_CHECK (!any_mismatch);
4091
4092 #undef EXPECT
4093 #undef CHECK_MATCH
4094 }
4095
4096 static void
4097 run_test ()
4098 {
4099 test_mapped_index_find_name_component_bounds ();
4100 test_dw2_expand_symtabs_matching_symbol ();
4101 }
4102
4103 }} // namespace selftests::dw2_expand_symtabs_matching
4104
4105 #endif /* GDB_SELF_TEST */
4106
4107 /* If FILE_MATCHER is NULL or if PER_CU has
4108 dwarf2_per_cu_quick_data::MARK set (see
4109 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4110 EXPANSION_NOTIFY on it. */
4111
4112 static bool
4113 dw2_expand_symtabs_matching_one
4114 (dwarf2_per_cu_data *per_cu,
4115 dwarf2_per_objfile *per_objfile,
4116 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4117 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4118 {
4119 if (file_matcher == NULL || per_cu->v.quick->mark)
4120 {
4121 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4122
4123 compunit_symtab *symtab
4124 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4125 gdb_assert (symtab != nullptr);
4126
4127 if (expansion_notify != NULL && symtab_was_null)
4128 return expansion_notify (symtab);
4129 }
4130 return true;
4131 }
4132
4133 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4134 matched, to expand corresponding CUs that were marked. IDX is the
4135 index of the symbol name that matched. */
4136
4137 static bool
4138 dw2_expand_marked_cus
4139 (dwarf2_per_objfile *per_objfile, offset_type idx,
4140 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4141 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4142 block_search_flags search_flags,
4143 search_domain kind)
4144 {
4145 offset_type vec_len, vec_idx;
4146 bool global_seen = false;
4147 mapped_index &index = *per_objfile->per_bfd->index_table;
4148
4149 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4150 vec_len = vec[0];
4151 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4152 {
4153 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4154 /* This value is only valid for index versions >= 7. */
4155 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4156 gdb_index_symbol_kind symbol_kind =
4157 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4158 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4159 /* Only check the symbol attributes if they're present.
4160 Indices prior to version 7 don't record them,
4161 and indices >= 7 may elide them for certain symbols
4162 (gold does this). */
4163 int attrs_valid =
4164 (index.version >= 7
4165 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4166
4167 /* Work around gold/15646. */
4168 if (attrs_valid
4169 && !is_static
4170 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4171 {
4172 if (global_seen)
4173 continue;
4174
4175 global_seen = true;
4176 }
4177
4178 /* Only check the symbol's kind if it has one. */
4179 if (attrs_valid)
4180 {
4181 if (is_static)
4182 {
4183 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4184 continue;
4185 }
4186 else
4187 {
4188 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4189 continue;
4190 }
4191
4192 switch (kind)
4193 {
4194 case VARIABLES_DOMAIN:
4195 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4196 continue;
4197 break;
4198 case FUNCTIONS_DOMAIN:
4199 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4200 continue;
4201 break;
4202 case TYPES_DOMAIN:
4203 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4204 continue;
4205 break;
4206 case MODULES_DOMAIN:
4207 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4208 continue;
4209 break;
4210 default:
4211 break;
4212 }
4213 }
4214
4215 /* Don't crash on bad data. */
4216 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4217 {
4218 complaint (_(".gdb_index entry has bad CU index"
4219 " [in module %s]"), objfile_name (per_objfile->objfile));
4220 continue;
4221 }
4222
4223 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4224 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4225 expansion_notify))
4226 return false;
4227 }
4228
4229 return true;
4230 }
4231
4232 /* If FILE_MATCHER is non-NULL, set all the
4233 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4234 that match FILE_MATCHER. */
4235
4236 static void
4237 dw_expand_symtabs_matching_file_matcher
4238 (dwarf2_per_objfile *per_objfile,
4239 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4240 {
4241 if (file_matcher == NULL)
4242 return;
4243
4244 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4245 htab_eq_pointer,
4246 NULL, xcalloc, xfree));
4247 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4248 htab_eq_pointer,
4249 NULL, xcalloc, xfree));
4250
4251 /* The rule is CUs specify all the files, including those used by
4252 any TU, so there's no need to scan TUs here. */
4253
4254 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4255 {
4256 QUIT;
4257
4258 if (per_cu->is_debug_types)
4259 continue;
4260 per_cu->v.quick->mark = 0;
4261
4262 /* We only need to look at symtabs not already expanded. */
4263 if (per_objfile->symtab_set_p (per_cu.get ()))
4264 continue;
4265
4266 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4267 per_objfile);
4268 if (file_data == NULL)
4269 continue;
4270
4271 if (htab_find (visited_not_found.get (), file_data) != NULL)
4272 continue;
4273 else if (htab_find (visited_found.get (), file_data) != NULL)
4274 {
4275 per_cu->v.quick->mark = 1;
4276 continue;
4277 }
4278
4279 for (int j = 0; j < file_data->num_file_names; ++j)
4280 {
4281 const char *this_real_name;
4282
4283 if (file_matcher (file_data->file_names[j], false))
4284 {
4285 per_cu->v.quick->mark = 1;
4286 break;
4287 }
4288
4289 /* Before we invoke realpath, which can get expensive when many
4290 files are involved, do a quick comparison of the basenames. */
4291 if (!basenames_may_differ
4292 && !file_matcher (lbasename (file_data->file_names[j]),
4293 true))
4294 continue;
4295
4296 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4297 if (file_matcher (this_real_name, false))
4298 {
4299 per_cu->v.quick->mark = 1;
4300 break;
4301 }
4302 }
4303
4304 void **slot = htab_find_slot (per_cu->v.quick->mark
4305 ? visited_found.get ()
4306 : visited_not_found.get (),
4307 file_data, INSERT);
4308 *slot = file_data;
4309 }
4310 }
4311
4312 bool
4313 dwarf2_gdb_index::expand_symtabs_matching
4314 (struct objfile *objfile,
4315 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4316 const lookup_name_info *lookup_name,
4317 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4318 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4319 block_search_flags search_flags,
4320 domain_enum domain,
4321 enum search_domain kind)
4322 {
4323 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4324
4325 /* index_table is NULL if OBJF_READNOW. */
4326 if (!per_objfile->per_bfd->index_table)
4327 return true;
4328
4329 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4330
4331 if (symbol_matcher == NULL && lookup_name == NULL)
4332 {
4333 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4334 {
4335 QUIT;
4336
4337 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
4338 file_matcher,
4339 expansion_notify))
4340 return false;
4341 }
4342 return true;
4343 }
4344
4345 mapped_index &index = *per_objfile->per_bfd->index_table;
4346
4347 bool result
4348 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4349 symbol_matcher,
4350 [&] (offset_type idx)
4351 {
4352 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4353 expansion_notify, search_flags, kind))
4354 return false;
4355 return true;
4356 }, per_objfile);
4357
4358 return result;
4359 }
4360
4361 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4362 symtab. */
4363
4364 static struct compunit_symtab *
4365 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4366 CORE_ADDR pc)
4367 {
4368 int i;
4369
4370 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4371 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4372 return cust;
4373
4374 if (cust->includes == NULL)
4375 return NULL;
4376
4377 for (i = 0; cust->includes[i]; ++i)
4378 {
4379 struct compunit_symtab *s = cust->includes[i];
4380
4381 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4382 if (s != NULL)
4383 return s;
4384 }
4385
4386 return NULL;
4387 }
4388
4389 struct compunit_symtab *
4390 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4391 (struct objfile *objfile,
4392 struct bound_minimal_symbol msymbol,
4393 CORE_ADDR pc,
4394 struct obj_section *section,
4395 int warn_if_readin)
4396 {
4397 struct dwarf2_per_cu_data *data;
4398 struct compunit_symtab *result;
4399
4400 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4401 if (per_objfile->per_bfd->index_addrmap == nullptr)
4402 return NULL;
4403
4404 CORE_ADDR baseaddr = objfile->text_section_offset ();
4405 data = ((struct dwarf2_per_cu_data *)
4406 addrmap_find (per_objfile->per_bfd->index_addrmap,
4407 pc - baseaddr));
4408 if (!data)
4409 return NULL;
4410
4411 if (warn_if_readin && per_objfile->symtab_set_p (data))
4412 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4413 paddress (objfile->arch (), pc));
4414
4415 result = recursively_find_pc_sect_compunit_symtab
4416 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4417
4418 gdb_assert (result != NULL);
4419 return result;
4420 }
4421
4422 void
4423 dwarf2_base_index_functions::map_symbol_filenames
4424 (struct objfile *objfile,
4425 gdb::function_view<symbol_filename_ftype> fun,
4426 bool need_fullname)
4427 {
4428 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4429
4430 /* Use caches to ensure we only call FUN once for each filename. */
4431 filename_seen_cache filenames_cache;
4432 std::unordered_set<quick_file_names *> qfn_cache;
4433
4434 /* The rule is CUs specify all the files, including those used by any TU,
4435 so there's no need to scan TUs here. We can ignore file names coming
4436 from already-expanded CUs. It is possible that an expanded CU might
4437 reuse the file names data from a currently unexpanded CU, in this
4438 case we don't want to report the files from the unexpanded CU. */
4439
4440 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4441 {
4442 if (per_objfile->symtab_set_p (per_cu.get ()))
4443 {
4444 if (per_cu->v.quick->file_names != nullptr)
4445 qfn_cache.insert (per_cu->v.quick->file_names);
4446 }
4447 }
4448
4449 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4450 {
4451 /* We only need to look at symtabs not already expanded. */
4452 if (per_objfile->symtab_set_p (per_cu.get ()))
4453 continue;
4454
4455 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4456 per_objfile);
4457 if (file_data == nullptr
4458 || qfn_cache.find (file_data) != qfn_cache.end ())
4459 continue;
4460
4461 for (int j = 0; j < file_data->num_file_names; ++j)
4462 {
4463 const char *filename = file_data->file_names[j];
4464 filenames_cache.seen (filename);
4465 }
4466 }
4467
4468 filenames_cache.traverse ([&] (const char *filename)
4469 {
4470 gdb::unique_xmalloc_ptr<char> this_real_name;
4471
4472 if (need_fullname)
4473 this_real_name = gdb_realpath (filename);
4474 fun (filename, this_real_name.get ());
4475 });
4476 }
4477
4478 bool
4479 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4480 {
4481 return true;
4482 }
4483
4484 /* DWARF-5 debug_names reader. */
4485
4486 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4487 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4488
4489 /* A helper function that reads the .debug_names section in SECTION
4490 and fills in MAP. FILENAME is the name of the file containing the
4491 section; it is used for error reporting.
4492
4493 Returns true if all went well, false otherwise. */
4494
4495 static bool
4496 read_debug_names_from_section (struct objfile *objfile,
4497 const char *filename,
4498 struct dwarf2_section_info *section,
4499 mapped_debug_names &map)
4500 {
4501 if (section->empty ())
4502 return false;
4503
4504 /* Older elfutils strip versions could keep the section in the main
4505 executable while splitting it for the separate debug info file. */
4506 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4507 return false;
4508
4509 section->read (objfile);
4510
4511 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4512
4513 const gdb_byte *addr = section->buffer;
4514
4515 bfd *const abfd = section->get_bfd_owner ();
4516
4517 unsigned int bytes_read;
4518 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4519 addr += bytes_read;
4520
4521 map.dwarf5_is_dwarf64 = bytes_read != 4;
4522 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4523 if (bytes_read + length != section->size)
4524 {
4525 /* There may be multiple per-CU indices. */
4526 warning (_("Section .debug_names in %s length %s does not match "
4527 "section length %s, ignoring .debug_names."),
4528 filename, plongest (bytes_read + length),
4529 pulongest (section->size));
4530 return false;
4531 }
4532
4533 /* The version number. */
4534 uint16_t version = read_2_bytes (abfd, addr);
4535 addr += 2;
4536 if (version != 5)
4537 {
4538 warning (_("Section .debug_names in %s has unsupported version %d, "
4539 "ignoring .debug_names."),
4540 filename, version);
4541 return false;
4542 }
4543
4544 /* Padding. */
4545 uint16_t padding = read_2_bytes (abfd, addr);
4546 addr += 2;
4547 if (padding != 0)
4548 {
4549 warning (_("Section .debug_names in %s has unsupported padding %d, "
4550 "ignoring .debug_names."),
4551 filename, padding);
4552 return false;
4553 }
4554
4555 /* comp_unit_count - The number of CUs in the CU list. */
4556 map.cu_count = read_4_bytes (abfd, addr);
4557 addr += 4;
4558
4559 /* local_type_unit_count - The number of TUs in the local TU
4560 list. */
4561 map.tu_count = read_4_bytes (abfd, addr);
4562 addr += 4;
4563
4564 /* foreign_type_unit_count - The number of TUs in the foreign TU
4565 list. */
4566 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4567 addr += 4;
4568 if (foreign_tu_count != 0)
4569 {
4570 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4571 "ignoring .debug_names."),
4572 filename, static_cast<unsigned long> (foreign_tu_count));
4573 return false;
4574 }
4575
4576 /* bucket_count - The number of hash buckets in the hash lookup
4577 table. */
4578 map.bucket_count = read_4_bytes (abfd, addr);
4579 addr += 4;
4580
4581 /* name_count - The number of unique names in the index. */
4582 map.name_count = read_4_bytes (abfd, addr);
4583 addr += 4;
4584
4585 /* abbrev_table_size - The size in bytes of the abbreviations
4586 table. */
4587 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4588 addr += 4;
4589
4590 /* augmentation_string_size - The size in bytes of the augmentation
4591 string. This value is rounded up to a multiple of 4. */
4592 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4593 addr += 4;
4594 map.augmentation_is_gdb = ((augmentation_string_size
4595 == sizeof (dwarf5_augmentation))
4596 && memcmp (addr, dwarf5_augmentation,
4597 sizeof (dwarf5_augmentation)) == 0);
4598 augmentation_string_size += (-augmentation_string_size) & 3;
4599 addr += augmentation_string_size;
4600
4601 /* List of CUs */
4602 map.cu_table_reordered = addr;
4603 addr += map.cu_count * map.offset_size;
4604
4605 /* List of Local TUs */
4606 map.tu_table_reordered = addr;
4607 addr += map.tu_count * map.offset_size;
4608
4609 /* Hash Lookup Table */
4610 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4611 addr += map.bucket_count * 4;
4612 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4613 addr += map.name_count * 4;
4614
4615 /* Name Table */
4616 map.name_table_string_offs_reordered = addr;
4617 addr += map.name_count * map.offset_size;
4618 map.name_table_entry_offs_reordered = addr;
4619 addr += map.name_count * map.offset_size;
4620
4621 const gdb_byte *abbrev_table_start = addr;
4622 for (;;)
4623 {
4624 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4625 addr += bytes_read;
4626 if (index_num == 0)
4627 break;
4628
4629 const auto insertpair
4630 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4631 if (!insertpair.second)
4632 {
4633 warning (_("Section .debug_names in %s has duplicate index %s, "
4634 "ignoring .debug_names."),
4635 filename, pulongest (index_num));
4636 return false;
4637 }
4638 mapped_debug_names::index_val &indexval = insertpair.first->second;
4639 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4640 addr += bytes_read;
4641
4642 for (;;)
4643 {
4644 mapped_debug_names::index_val::attr attr;
4645 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4646 addr += bytes_read;
4647 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4648 addr += bytes_read;
4649 if (attr.form == DW_FORM_implicit_const)
4650 {
4651 attr.implicit_const = read_signed_leb128 (abfd, addr,
4652 &bytes_read);
4653 addr += bytes_read;
4654 }
4655 if (attr.dw_idx == 0 && attr.form == 0)
4656 break;
4657 indexval.attr_vec.push_back (std::move (attr));
4658 }
4659 }
4660 if (addr != abbrev_table_start + abbrev_table_size)
4661 {
4662 warning (_("Section .debug_names in %s has abbreviation_table "
4663 "of size %s vs. written as %u, ignoring .debug_names."),
4664 filename, plongest (addr - abbrev_table_start),
4665 abbrev_table_size);
4666 return false;
4667 }
4668 map.entry_pool = addr;
4669
4670 return true;
4671 }
4672
4673 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4674 list. */
4675
4676 static void
4677 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4678 const mapped_debug_names &map,
4679 dwarf2_section_info &section,
4680 bool is_dwz)
4681 {
4682 if (!map.augmentation_is_gdb)
4683 {
4684 for (uint32_t i = 0; i < map.cu_count; ++i)
4685 {
4686 sect_offset sect_off
4687 = (sect_offset) (extract_unsigned_integer
4688 (map.cu_table_reordered + i * map.offset_size,
4689 map.offset_size,
4690 map.dwarf5_byte_order));
4691 /* We don't know the length of the CU, because the CU list in a
4692 .debug_names index can be incomplete, so we can't use the start
4693 of the next CU as end of this CU. We create the CUs here with
4694 length 0, and in cutu_reader::cutu_reader we'll fill in the
4695 actual length. */
4696 dwarf2_per_cu_data_up per_cu
4697 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4698 sect_off, 0);
4699 per_bfd->all_comp_units.push_back (std::move (per_cu));
4700 }
4701 return;
4702 }
4703
4704 sect_offset sect_off_prev;
4705 for (uint32_t i = 0; i <= map.cu_count; ++i)
4706 {
4707 sect_offset sect_off_next;
4708 if (i < map.cu_count)
4709 {
4710 sect_off_next
4711 = (sect_offset) (extract_unsigned_integer
4712 (map.cu_table_reordered + i * map.offset_size,
4713 map.offset_size,
4714 map.dwarf5_byte_order));
4715 }
4716 else
4717 sect_off_next = (sect_offset) section.size;
4718 if (i >= 1)
4719 {
4720 const ULONGEST length = sect_off_next - sect_off_prev;
4721 dwarf2_per_cu_data_up per_cu
4722 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4723 sect_off_prev, length);
4724 per_bfd->all_comp_units.push_back (std::move (per_cu));
4725 }
4726 sect_off_prev = sect_off_next;
4727 }
4728 }
4729
4730 /* Read the CU list from the mapped index, and use it to create all
4731 the CU objects for this dwarf2_per_objfile. */
4732
4733 static void
4734 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4735 const mapped_debug_names &map,
4736 const mapped_debug_names &dwz_map)
4737 {
4738 gdb_assert (per_bfd->all_comp_units.empty ());
4739 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4740
4741 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4742 false /* is_dwz */);
4743
4744 if (dwz_map.cu_count == 0)
4745 return;
4746
4747 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4748 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4749 true /* is_dwz */);
4750 }
4751
4752 /* Read .debug_names. If everything went ok, initialize the "quick"
4753 elements of all the CUs and return true. Otherwise, return false. */
4754
4755 static bool
4756 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4757 {
4758 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4759 mapped_debug_names dwz_map;
4760 struct objfile *objfile = per_objfile->objfile;
4761 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4762
4763 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4764 &per_bfd->debug_names, *map))
4765 return false;
4766
4767 /* Don't use the index if it's empty. */
4768 if (map->name_count == 0)
4769 return false;
4770
4771 /* If there is a .dwz file, read it so we can get its CU list as
4772 well. */
4773 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4774 if (dwz != NULL)
4775 {
4776 if (!read_debug_names_from_section (objfile,
4777 bfd_get_filename (dwz->dwz_bfd.get ()),
4778 &dwz->debug_names, dwz_map))
4779 {
4780 warning (_("could not read '.debug_names' section from %s; skipping"),
4781 bfd_get_filename (dwz->dwz_bfd.get ()));
4782 return false;
4783 }
4784 }
4785
4786 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4787
4788 if (map->tu_count != 0)
4789 {
4790 /* We can only handle a single .debug_types when we have an
4791 index. */
4792 if (per_bfd->types.size () != 1)
4793 return false;
4794
4795 dwarf2_section_info *section = &per_bfd->types[0];
4796
4797 create_signatured_type_table_from_debug_names
4798 (per_objfile, *map, section, &per_bfd->abbrev);
4799 }
4800
4801 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4802
4803 per_bfd->debug_names_table = std::move (map);
4804 per_bfd->using_index = 1;
4805 per_bfd->quick_file_names_table =
4806 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4807
4808 return true;
4809 }
4810
4811 /* Type used to manage iterating over all CUs looking for a symbol for
4812 .debug_names. */
4813
4814 class dw2_debug_names_iterator
4815 {
4816 public:
4817 dw2_debug_names_iterator (const mapped_debug_names &map,
4818 block_search_flags block_index,
4819 domain_enum domain,
4820 const char *name, dwarf2_per_objfile *per_objfile)
4821 : m_map (map), m_block_index (block_index), m_domain (domain),
4822 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4823 m_per_objfile (per_objfile)
4824 {}
4825
4826 dw2_debug_names_iterator (const mapped_debug_names &map,
4827 search_domain search, uint32_t namei,
4828 dwarf2_per_objfile *per_objfile,
4829 domain_enum domain = UNDEF_DOMAIN)
4830 : m_map (map),
4831 m_domain (domain),
4832 m_search (search),
4833 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4834 m_per_objfile (per_objfile)
4835 {}
4836
4837 dw2_debug_names_iterator (const mapped_debug_names &map,
4838 block_search_flags block_index, domain_enum domain,
4839 uint32_t namei, dwarf2_per_objfile *per_objfile)
4840 : m_map (map), m_block_index (block_index), m_domain (domain),
4841 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4842 m_per_objfile (per_objfile)
4843 {}
4844
4845 /* Return the next matching CU or NULL if there are no more. */
4846 dwarf2_per_cu_data *next ();
4847
4848 private:
4849 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4850 const char *name,
4851 dwarf2_per_objfile *per_objfile);
4852 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4853 uint32_t namei,
4854 dwarf2_per_objfile *per_objfile);
4855
4856 /* The internalized form of .debug_names. */
4857 const mapped_debug_names &m_map;
4858
4859 /* Restrict the search to these blocks. */
4860 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4861 | SEARCH_STATIC_BLOCK);
4862
4863 /* The kind of symbol we're looking for. */
4864 const domain_enum m_domain = UNDEF_DOMAIN;
4865 const search_domain m_search = ALL_DOMAIN;
4866
4867 /* The list of CUs from the index entry of the symbol, or NULL if
4868 not found. */
4869 const gdb_byte *m_addr;
4870
4871 dwarf2_per_objfile *m_per_objfile;
4872 };
4873
4874 const char *
4875 mapped_debug_names::namei_to_name
4876 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4877 {
4878 const ULONGEST namei_string_offs
4879 = extract_unsigned_integer ((name_table_string_offs_reordered
4880 + namei * offset_size),
4881 offset_size,
4882 dwarf5_byte_order);
4883 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4884 }
4885
4886 /* Find a slot in .debug_names for the object named NAME. If NAME is
4887 found, return pointer to its pool data. If NAME cannot be found,
4888 return NULL. */
4889
4890 const gdb_byte *
4891 dw2_debug_names_iterator::find_vec_in_debug_names
4892 (const mapped_debug_names &map, const char *name,
4893 dwarf2_per_objfile *per_objfile)
4894 {
4895 int (*cmp) (const char *, const char *);
4896
4897 gdb::unique_xmalloc_ptr<char> without_params;
4898 if (current_language->la_language == language_cplus
4899 || current_language->la_language == language_fortran
4900 || current_language->la_language == language_d)
4901 {
4902 /* NAME is already canonical. Drop any qualifiers as
4903 .debug_names does not contain any. */
4904
4905 if (strchr (name, '(') != NULL)
4906 {
4907 without_params = cp_remove_params (name);
4908 if (without_params != NULL)
4909 name = without_params.get ();
4910 }
4911 }
4912
4913 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4914
4915 const uint32_t full_hash = dwarf5_djb_hash (name);
4916 uint32_t namei
4917 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4918 (map.bucket_table_reordered
4919 + (full_hash % map.bucket_count)), 4,
4920 map.dwarf5_byte_order);
4921 if (namei == 0)
4922 return NULL;
4923 --namei;
4924 if (namei >= map.name_count)
4925 {
4926 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4927 "[in module %s]"),
4928 namei, map.name_count,
4929 objfile_name (per_objfile->objfile));
4930 return NULL;
4931 }
4932
4933 for (;;)
4934 {
4935 const uint32_t namei_full_hash
4936 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4937 (map.hash_table_reordered + namei), 4,
4938 map.dwarf5_byte_order);
4939 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4940 return NULL;
4941
4942 if (full_hash == namei_full_hash)
4943 {
4944 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4945
4946 #if 0 /* An expensive sanity check. */
4947 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4948 {
4949 complaint (_("Wrong .debug_names hash for string at index %u "
4950 "[in module %s]"),
4951 namei, objfile_name (dwarf2_per_objfile->objfile));
4952 return NULL;
4953 }
4954 #endif
4955
4956 if (cmp (namei_string, name) == 0)
4957 {
4958 const ULONGEST namei_entry_offs
4959 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4960 + namei * map.offset_size),
4961 map.offset_size, map.dwarf5_byte_order);
4962 return map.entry_pool + namei_entry_offs;
4963 }
4964 }
4965
4966 ++namei;
4967 if (namei >= map.name_count)
4968 return NULL;
4969 }
4970 }
4971
4972 const gdb_byte *
4973 dw2_debug_names_iterator::find_vec_in_debug_names
4974 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4975 {
4976 if (namei >= map.name_count)
4977 {
4978 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4979 "[in module %s]"),
4980 namei, map.name_count,
4981 objfile_name (per_objfile->objfile));
4982 return NULL;
4983 }
4984
4985 const ULONGEST namei_entry_offs
4986 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4987 + namei * map.offset_size),
4988 map.offset_size, map.dwarf5_byte_order);
4989 return map.entry_pool + namei_entry_offs;
4990 }
4991
4992 /* See dw2_debug_names_iterator. */
4993
4994 dwarf2_per_cu_data *
4995 dw2_debug_names_iterator::next ()
4996 {
4997 if (m_addr == NULL)
4998 return NULL;
4999
5000 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5001 struct objfile *objfile = m_per_objfile->objfile;
5002 bfd *const abfd = objfile->obfd;
5003
5004 again:
5005
5006 unsigned int bytes_read;
5007 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5008 m_addr += bytes_read;
5009 if (abbrev == 0)
5010 return NULL;
5011
5012 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5013 if (indexval_it == m_map.abbrev_map.cend ())
5014 {
5015 complaint (_("Wrong .debug_names undefined abbrev code %s "
5016 "[in module %s]"),
5017 pulongest (abbrev), objfile_name (objfile));
5018 return NULL;
5019 }
5020 const mapped_debug_names::index_val &indexval = indexval_it->second;
5021 enum class symbol_linkage {
5022 unknown,
5023 static_,
5024 extern_,
5025 } symbol_linkage_ = symbol_linkage::unknown;
5026 dwarf2_per_cu_data *per_cu = NULL;
5027 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5028 {
5029 ULONGEST ull;
5030 switch (attr.form)
5031 {
5032 case DW_FORM_implicit_const:
5033 ull = attr.implicit_const;
5034 break;
5035 case DW_FORM_flag_present:
5036 ull = 1;
5037 break;
5038 case DW_FORM_udata:
5039 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5040 m_addr += bytes_read;
5041 break;
5042 case DW_FORM_ref4:
5043 ull = read_4_bytes (abfd, m_addr);
5044 m_addr += 4;
5045 break;
5046 case DW_FORM_ref8:
5047 ull = read_8_bytes (abfd, m_addr);
5048 m_addr += 8;
5049 break;
5050 case DW_FORM_ref_sig8:
5051 ull = read_8_bytes (abfd, m_addr);
5052 m_addr += 8;
5053 break;
5054 default:
5055 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5056 dwarf_form_name (attr.form),
5057 objfile_name (objfile));
5058 return NULL;
5059 }
5060 switch (attr.dw_idx)
5061 {
5062 case DW_IDX_compile_unit:
5063 /* Don't crash on bad data. */
5064 if (ull >= per_bfd->all_comp_units.size ())
5065 {
5066 complaint (_(".debug_names entry has bad CU index %s"
5067 " [in module %s]"),
5068 pulongest (ull),
5069 objfile_name (objfile));
5070 continue;
5071 }
5072 per_cu = per_bfd->get_cu (ull);
5073 break;
5074 case DW_IDX_type_unit:
5075 /* Don't crash on bad data. */
5076 if (ull >= per_bfd->tu_stats.nr_tus)
5077 {
5078 complaint (_(".debug_names entry has bad TU index %s"
5079 " [in module %s]"),
5080 pulongest (ull),
5081 objfile_name (objfile));
5082 continue;
5083 }
5084 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5085 break;
5086 case DW_IDX_die_offset:
5087 /* In a per-CU index (as opposed to a per-module index), index
5088 entries without CU attribute implicitly refer to the single CU. */
5089 if (per_cu == NULL)
5090 per_cu = per_bfd->get_cu (0);
5091 break;
5092 case DW_IDX_GNU_internal:
5093 if (!m_map.augmentation_is_gdb)
5094 break;
5095 symbol_linkage_ = symbol_linkage::static_;
5096 break;
5097 case DW_IDX_GNU_external:
5098 if (!m_map.augmentation_is_gdb)
5099 break;
5100 symbol_linkage_ = symbol_linkage::extern_;
5101 break;
5102 }
5103 }
5104
5105 /* Skip if already read in. */
5106 if (m_per_objfile->symtab_set_p (per_cu))
5107 goto again;
5108
5109 /* Check static vs global. */
5110 if (symbol_linkage_ != symbol_linkage::unknown)
5111 {
5112 if (symbol_linkage_ == symbol_linkage::static_)
5113 {
5114 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5115 goto again;
5116 }
5117 else
5118 {
5119 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5120 goto again;
5121 }
5122 }
5123
5124 /* Match dw2_symtab_iter_next, symbol_kind
5125 and debug_names::psymbol_tag. */
5126 switch (m_domain)
5127 {
5128 case VAR_DOMAIN:
5129 switch (indexval.dwarf_tag)
5130 {
5131 case DW_TAG_variable:
5132 case DW_TAG_subprogram:
5133 /* Some types are also in VAR_DOMAIN. */
5134 case DW_TAG_typedef:
5135 case DW_TAG_structure_type:
5136 break;
5137 default:
5138 goto again;
5139 }
5140 break;
5141 case STRUCT_DOMAIN:
5142 switch (indexval.dwarf_tag)
5143 {
5144 case DW_TAG_typedef:
5145 case DW_TAG_structure_type:
5146 break;
5147 default:
5148 goto again;
5149 }
5150 break;
5151 case LABEL_DOMAIN:
5152 switch (indexval.dwarf_tag)
5153 {
5154 case 0:
5155 case DW_TAG_variable:
5156 break;
5157 default:
5158 goto again;
5159 }
5160 break;
5161 case MODULE_DOMAIN:
5162 switch (indexval.dwarf_tag)
5163 {
5164 case DW_TAG_module:
5165 break;
5166 default:
5167 goto again;
5168 }
5169 break;
5170 default:
5171 break;
5172 }
5173
5174 /* Match dw2_expand_symtabs_matching, symbol_kind and
5175 debug_names::psymbol_tag. */
5176 switch (m_search)
5177 {
5178 case VARIABLES_DOMAIN:
5179 switch (indexval.dwarf_tag)
5180 {
5181 case DW_TAG_variable:
5182 break;
5183 default:
5184 goto again;
5185 }
5186 break;
5187 case FUNCTIONS_DOMAIN:
5188 switch (indexval.dwarf_tag)
5189 {
5190 case DW_TAG_subprogram:
5191 break;
5192 default:
5193 goto again;
5194 }
5195 break;
5196 case TYPES_DOMAIN:
5197 switch (indexval.dwarf_tag)
5198 {
5199 case DW_TAG_typedef:
5200 case DW_TAG_structure_type:
5201 break;
5202 default:
5203 goto again;
5204 }
5205 break;
5206 case MODULES_DOMAIN:
5207 switch (indexval.dwarf_tag)
5208 {
5209 case DW_TAG_module:
5210 break;
5211 default:
5212 goto again;
5213 }
5214 default:
5215 break;
5216 }
5217
5218 return per_cu;
5219 }
5220
5221 /* This dumps minimal information about .debug_names. It is called
5222 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5223 uses this to verify that .debug_names has been loaded. */
5224
5225 void
5226 dwarf2_debug_names_index::dump (struct objfile *objfile)
5227 {
5228 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5229
5230 gdb_assert (per_objfile->per_bfd->using_index);
5231 printf_filtered (".debug_names:");
5232 if (per_objfile->per_bfd->debug_names_table)
5233 printf_filtered (" exists\n");
5234 else
5235 printf_filtered (" faked for \"readnow\"\n");
5236 printf_filtered ("\n");
5237 }
5238
5239 void
5240 dwarf2_debug_names_index::expand_matching_symbols
5241 (struct objfile *objfile,
5242 const lookup_name_info &name, domain_enum domain,
5243 int global,
5244 symbol_compare_ftype *ordered_compare)
5245 {
5246 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5247
5248 /* debug_names_table is NULL if OBJF_READNOW. */
5249 if (!per_objfile->per_bfd->debug_names_table)
5250 return;
5251
5252 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5253 const block_search_flags block_flags
5254 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5255
5256 const char *match_name = name.ada ().lookup_name ().c_str ();
5257 auto matcher = [&] (const char *symname)
5258 {
5259 if (ordered_compare == nullptr)
5260 return true;
5261 return ordered_compare (symname, match_name) == 0;
5262 };
5263
5264 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5265 [&] (offset_type namei)
5266 {
5267 /* The name was matched, now expand corresponding CUs that were
5268 marked. */
5269 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5270 per_objfile);
5271
5272 struct dwarf2_per_cu_data *per_cu;
5273 while ((per_cu = iter.next ()) != NULL)
5274 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5275 nullptr);
5276 return true;
5277 }, per_objfile);
5278 }
5279
5280 bool
5281 dwarf2_debug_names_index::expand_symtabs_matching
5282 (struct objfile *objfile,
5283 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5284 const lookup_name_info *lookup_name,
5285 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5286 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5287 block_search_flags search_flags,
5288 domain_enum domain,
5289 enum search_domain kind)
5290 {
5291 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5292
5293 /* debug_names_table is NULL if OBJF_READNOW. */
5294 if (!per_objfile->per_bfd->debug_names_table)
5295 return true;
5296
5297 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5298
5299 if (symbol_matcher == NULL && lookup_name == NULL)
5300 {
5301 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
5302 {
5303 QUIT;
5304
5305 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
5306 file_matcher,
5307 expansion_notify))
5308 return false;
5309 }
5310 return true;
5311 }
5312
5313 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5314
5315 bool result
5316 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5317 symbol_matcher,
5318 [&] (offset_type namei)
5319 {
5320 /* The name was matched, now expand corresponding CUs that were
5321 marked. */
5322 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5323
5324 struct dwarf2_per_cu_data *per_cu;
5325 while ((per_cu = iter.next ()) != NULL)
5326 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5327 file_matcher,
5328 expansion_notify))
5329 return false;
5330 return true;
5331 }, per_objfile);
5332
5333 return result;
5334 }
5335
5336 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5337 to either a dwarf2_per_bfd or dwz_file object. */
5338
5339 template <typename T>
5340 static gdb::array_view<const gdb_byte>
5341 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5342 {
5343 dwarf2_section_info *section = &section_owner->gdb_index;
5344
5345 if (section->empty ())
5346 return {};
5347
5348 /* Older elfutils strip versions could keep the section in the main
5349 executable while splitting it for the separate debug info file. */
5350 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5351 return {};
5352
5353 section->read (obj);
5354
5355 /* dwarf2_section_info::size is a bfd_size_type, while
5356 gdb::array_view works with size_t. On 32-bit hosts, with
5357 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5358 is 32-bit. So we need an explicit narrowing conversion here.
5359 This is fine, because it's impossible to allocate or mmap an
5360 array/buffer larger than what size_t can represent. */
5361 return gdb::make_array_view (section->buffer, section->size);
5362 }
5363
5364 /* Lookup the index cache for the contents of the index associated to
5365 DWARF2_OBJ. */
5366
5367 static gdb::array_view<const gdb_byte>
5368 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5369 {
5370 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5371 if (build_id == nullptr)
5372 return {};
5373
5374 return global_index_cache.lookup_gdb_index (build_id,
5375 &dwarf2_per_bfd->index_cache_res);
5376 }
5377
5378 /* Same as the above, but for DWZ. */
5379
5380 static gdb::array_view<const gdb_byte>
5381 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5382 {
5383 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5384 if (build_id == nullptr)
5385 return {};
5386
5387 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5388 }
5389
5390 /* See dwarf2/public.h. */
5391
5392 void
5393 dwarf2_initialize_objfile (struct objfile *objfile)
5394 {
5395 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5396 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5397
5398 dwarf_read_debug_printf ("called");
5399
5400 /* If we're about to read full symbols, don't bother with the
5401 indices. In this case we also don't care if some other debug
5402 format is making psymtabs, because they are all about to be
5403 expanded anyway. */
5404 if ((objfile->flags & OBJF_READNOW))
5405 {
5406 dwarf_read_debug_printf ("readnow requested");
5407
5408 /* When using READNOW, the using_index flag (set below) indicates that
5409 PER_BFD was already initialized, when we loaded some other objfile. */
5410 if (per_bfd->using_index)
5411 {
5412 dwarf_read_debug_printf ("using_index already set");
5413 per_objfile->resize_symtabs ();
5414 objfile->qf.push_front (make_dwarf_gdb_index ());
5415 return;
5416 }
5417
5418 per_bfd->using_index = 1;
5419 create_all_comp_units (per_objfile);
5420 per_bfd->quick_file_names_table
5421 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5422 per_objfile->resize_symtabs ();
5423
5424 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5425 {
5426 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5427
5428 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5429 struct dwarf2_per_cu_quick_data);
5430 }
5431
5432 /* Arrange for gdb to see the "quick" functions. However, these
5433 functions will be no-ops because we will have expanded all
5434 symtabs. */
5435 objfile->qf.push_front (make_dwarf_gdb_index ());
5436 return;
5437 }
5438
5439 /* Was a debug names index already read when we processed an objfile sharing
5440 PER_BFD? */
5441 if (per_bfd->debug_names_table != nullptr)
5442 {
5443 dwarf_read_debug_printf ("re-using shared debug names table");
5444 per_objfile->resize_symtabs ();
5445 objfile->qf.push_front (make_dwarf_debug_names ());
5446 return;
5447 }
5448
5449 /* Was a GDB index already read when we processed an objfile sharing
5450 PER_BFD? */
5451 if (per_bfd->index_table != nullptr)
5452 {
5453 dwarf_read_debug_printf ("re-using shared index table");
5454 per_objfile->resize_symtabs ();
5455 objfile->qf.push_front (make_dwarf_gdb_index ());
5456 return;
5457 }
5458
5459 /* There might already be partial symtabs built for this BFD. This happens
5460 when loading the same binary twice with the index-cache enabled. If so,
5461 don't try to read an index. The objfile / per_objfile initialization will
5462 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5463 code path. */
5464 if (per_bfd->partial_symtabs != nullptr)
5465 {
5466 dwarf_read_debug_printf ("re-using shared partial symtabs");
5467 objfile->qf.push_front (make_lazy_dwarf_reader ());
5468 return;
5469 }
5470
5471 if (dwarf2_read_debug_names (per_objfile))
5472 {
5473 dwarf_read_debug_printf ("found debug names");
5474 per_objfile->resize_symtabs ();
5475 objfile->qf.push_front (make_dwarf_debug_names ());
5476 return;
5477 }
5478
5479 if (dwarf2_read_gdb_index (per_objfile,
5480 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5481 get_gdb_index_contents_from_section<dwz_file>))
5482 {
5483 dwarf_read_debug_printf ("found gdb index from file");
5484 per_objfile->resize_symtabs ();
5485 objfile->qf.push_front (make_dwarf_gdb_index ());
5486 return;
5487 }
5488
5489 /* ... otherwise, try to find the index in the index cache. */
5490 if (dwarf2_read_gdb_index (per_objfile,
5491 get_gdb_index_contents_from_cache,
5492 get_gdb_index_contents_from_cache_dwz))
5493 {
5494 dwarf_read_debug_printf ("found gdb index from cache");
5495 global_index_cache.hit ();
5496 per_objfile->resize_symtabs ();
5497 objfile->qf.push_front (make_dwarf_gdb_index ());
5498 return;
5499 }
5500
5501 global_index_cache.miss ();
5502 objfile->qf.push_front (make_lazy_dwarf_reader ());
5503 }
5504
5505 \f
5506
5507 /* Build a partial symbol table. */
5508
5509 void
5510 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5511 {
5512 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5513 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5514
5515 if (per_bfd->partial_symtabs != nullptr)
5516 {
5517 /* Partial symbols were already read, so now we can simply
5518 attach them. */
5519 if (psf == nullptr)
5520 {
5521 psf = new psymbol_functions (per_bfd->partial_symtabs);
5522 objfile->qf.emplace_front (psf);
5523 }
5524 else
5525 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5526 per_objfile->resize_symtabs ();
5527 return;
5528 }
5529
5530 if (psf == nullptr)
5531 {
5532 psf = new psymbol_functions;
5533 objfile->qf.emplace_front (psf);
5534 }
5535 const std::shared_ptr<psymtab_storage> &partial_symtabs
5536 = psf->get_partial_symtabs ();
5537
5538 /* Set the local reference to partial symtabs, so that we don't try
5539 to read them again if reading another objfile with the same BFD.
5540 If we can't in fact share, this won't make a difference anyway as
5541 the dwarf2_per_bfd object won't be shared. */
5542 per_bfd->partial_symtabs = partial_symtabs;
5543
5544 try
5545 {
5546 /* This isn't really ideal: all the data we allocate on the
5547 objfile's obstack is still uselessly kept around. However,
5548 freeing it seems unsafe. */
5549 psymtab_discarder psymtabs (partial_symtabs.get ());
5550 dwarf2_build_psymtabs_hard (per_objfile);
5551 psymtabs.keep ();
5552
5553 per_objfile->resize_symtabs ();
5554
5555 /* (maybe) store an index in the cache. */
5556 global_index_cache.store (per_objfile);
5557 }
5558 catch (const gdb_exception_error &except)
5559 {
5560 exception_print (gdb_stderr, except);
5561 }
5562 }
5563
5564 /* Find the base address of the compilation unit for range lists and
5565 location lists. It will normally be specified by DW_AT_low_pc.
5566 In DWARF-3 draft 4, the base address could be overridden by
5567 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5568 compilation units with discontinuous ranges. */
5569
5570 static void
5571 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5572 {
5573 struct attribute *attr;
5574
5575 cu->base_address.reset ();
5576
5577 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5578 if (attr != nullptr)
5579 cu->base_address = attr->as_address ();
5580 else
5581 {
5582 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5583 if (attr != nullptr)
5584 cu->base_address = attr->as_address ();
5585 }
5586 }
5587
5588 /* Helper function that returns the proper abbrev section for
5589 THIS_CU. */
5590
5591 static struct dwarf2_section_info *
5592 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5593 {
5594 struct dwarf2_section_info *abbrev;
5595 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5596
5597 if (this_cu->is_dwz)
5598 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5599 else
5600 abbrev = &per_bfd->abbrev;
5601
5602 return abbrev;
5603 }
5604
5605 /* Fetch the abbreviation table offset from a comp or type unit header. */
5606
5607 static sect_offset
5608 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5609 struct dwarf2_section_info *section,
5610 sect_offset sect_off)
5611 {
5612 bfd *abfd = section->get_bfd_owner ();
5613 const gdb_byte *info_ptr;
5614 unsigned int initial_length_size, offset_size;
5615 uint16_t version;
5616
5617 section->read (per_objfile->objfile);
5618 info_ptr = section->buffer + to_underlying (sect_off);
5619 read_initial_length (abfd, info_ptr, &initial_length_size);
5620 offset_size = initial_length_size == 4 ? 4 : 8;
5621 info_ptr += initial_length_size;
5622
5623 version = read_2_bytes (abfd, info_ptr);
5624 info_ptr += 2;
5625 if (version >= 5)
5626 {
5627 /* Skip unit type and address size. */
5628 info_ptr += 2;
5629 }
5630
5631 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5632 }
5633
5634 /* A partial symtab that is used only for include files. */
5635 struct dwarf2_include_psymtab : public partial_symtab
5636 {
5637 dwarf2_include_psymtab (const char *filename,
5638 psymtab_storage *partial_symtabs,
5639 objfile_per_bfd_storage *objfile_per_bfd)
5640 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5641 {
5642 }
5643
5644 void read_symtab (struct objfile *objfile) override
5645 {
5646 /* It's an include file, no symbols to read for it.
5647 Everything is in the includer symtab. */
5648
5649 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5650 expansion of the includer psymtab. We use the dependencies[0] field to
5651 model the includer. But if we go the regular route of calling
5652 expand_psymtab here, and having expand_psymtab call expand_dependencies
5653 to expand the includer, we'll only use expand_psymtab on the includer
5654 (making it a non-toplevel psymtab), while if we expand the includer via
5655 another path, we'll use read_symtab (making it a toplevel psymtab).
5656 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5657 psymtab, and trigger read_symtab on the includer here directly. */
5658 includer ()->read_symtab (objfile);
5659 }
5660
5661 void expand_psymtab (struct objfile *objfile) override
5662 {
5663 /* This is not called by read_symtab, and should not be called by any
5664 expand_dependencies. */
5665 gdb_assert (false);
5666 }
5667
5668 bool readin_p (struct objfile *objfile) const override
5669 {
5670 return includer ()->readin_p (objfile);
5671 }
5672
5673 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5674 {
5675 return nullptr;
5676 }
5677
5678 private:
5679 partial_symtab *includer () const
5680 {
5681 /* An include psymtab has exactly one dependency: the psymtab that
5682 includes it. */
5683 gdb_assert (this->number_of_dependencies == 1);
5684 return this->dependencies[0];
5685 }
5686 };
5687
5688 /* Allocate a new partial symtab for file named NAME and mark this new
5689 partial symtab as being an include of PST. */
5690
5691 static void
5692 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5693 const char *name,
5694 dwarf2_psymtab *pst,
5695 psymtab_storage *partial_symtabs,
5696 objfile_per_bfd_storage *objfile_per_bfd)
5697 {
5698 dwarf2_include_psymtab *subpst
5699 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5700
5701 if (!IS_ABSOLUTE_PATH (subpst->filename))
5702 subpst->dirname = pst->dirname;
5703
5704 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5705 subpst->dependencies[0] = pst;
5706 subpst->number_of_dependencies = 1;
5707 }
5708
5709 /* Read the Line Number Program data and extract the list of files
5710 included by the source file represented by PST. Build an include
5711 partial symtab for each of these included files. */
5712
5713 static void
5714 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5715 struct die_info *die,
5716 dwarf2_psymtab *pst)
5717 {
5718 line_header_up lh;
5719 struct attribute *attr;
5720
5721 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5722 if (attr != nullptr && attr->form_is_unsigned ())
5723 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5724 if (lh == NULL)
5725 return; /* No linetable, so no includes. */
5726
5727 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5728 that we pass in the raw text_low here; that is ok because we're
5729 only decoding the line table to make include partial symtabs, and
5730 so the addresses aren't really used. */
5731 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5732 pst->raw_text_low (), 1);
5733 }
5734
5735 static hashval_t
5736 hash_signatured_type (const void *item)
5737 {
5738 const struct signatured_type *sig_type
5739 = (const struct signatured_type *) item;
5740
5741 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5742 return sig_type->signature;
5743 }
5744
5745 static int
5746 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5747 {
5748 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5749 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5750
5751 return lhs->signature == rhs->signature;
5752 }
5753
5754 /* Allocate a hash table for signatured types. */
5755
5756 static htab_up
5757 allocate_signatured_type_table ()
5758 {
5759 return htab_up (htab_create_alloc (41,
5760 hash_signatured_type,
5761 eq_signatured_type,
5762 NULL, xcalloc, xfree));
5763 }
5764
5765 /* A helper for create_debug_types_hash_table. Read types from SECTION
5766 and fill them into TYPES_HTAB. It will process only type units,
5767 therefore DW_UT_type. */
5768
5769 static void
5770 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5771 struct dwo_file *dwo_file,
5772 dwarf2_section_info *section, htab_up &types_htab,
5773 rcuh_kind section_kind)
5774 {
5775 struct objfile *objfile = per_objfile->objfile;
5776 struct dwarf2_section_info *abbrev_section;
5777 bfd *abfd;
5778 const gdb_byte *info_ptr, *end_ptr;
5779
5780 abbrev_section = &dwo_file->sections.abbrev;
5781
5782 dwarf_read_debug_printf ("Reading %s for %s",
5783 section->get_name (),
5784 abbrev_section->get_file_name ());
5785
5786 section->read (objfile);
5787 info_ptr = section->buffer;
5788
5789 if (info_ptr == NULL)
5790 return;
5791
5792 /* We can't set abfd until now because the section may be empty or
5793 not present, in which case the bfd is unknown. */
5794 abfd = section->get_bfd_owner ();
5795
5796 /* We don't use cutu_reader here because we don't need to read
5797 any dies: the signature is in the header. */
5798
5799 end_ptr = info_ptr + section->size;
5800 while (info_ptr < end_ptr)
5801 {
5802 std::unique_ptr<signatured_type> sig_type;
5803 struct dwo_unit *dwo_tu;
5804 void **slot;
5805 const gdb_byte *ptr = info_ptr;
5806 struct comp_unit_head header;
5807 unsigned int length;
5808
5809 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5810
5811 /* Initialize it due to a false compiler warning. */
5812 header.signature = -1;
5813 header.type_cu_offset_in_tu = (cu_offset) -1;
5814
5815 /* We need to read the type's signature in order to build the hash
5816 table, but we don't need anything else just yet. */
5817
5818 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5819 abbrev_section, ptr, section_kind);
5820
5821 length = header.get_length ();
5822
5823 /* Skip dummy type units. */
5824 if (ptr >= info_ptr + length
5825 || peek_abbrev_code (abfd, ptr) == 0
5826 || (header.unit_type != DW_UT_type
5827 && header.unit_type != DW_UT_split_type))
5828 {
5829 info_ptr += length;
5830 continue;
5831 }
5832
5833 if (types_htab == NULL)
5834 types_htab = allocate_dwo_unit_table ();
5835
5836 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5837 dwo_tu->dwo_file = dwo_file;
5838 dwo_tu->signature = header.signature;
5839 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5840 dwo_tu->section = section;
5841 dwo_tu->sect_off = sect_off;
5842 dwo_tu->length = length;
5843
5844 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5845 gdb_assert (slot != NULL);
5846 if (*slot != NULL)
5847 complaint (_("debug type entry at offset %s is duplicate to"
5848 " the entry at offset %s, signature %s"),
5849 sect_offset_str (sect_off),
5850 sect_offset_str (dwo_tu->sect_off),
5851 hex_string (header.signature));
5852 *slot = dwo_tu;
5853
5854 dwarf_read_debug_printf_v (" offset %s, signature %s",
5855 sect_offset_str (sect_off),
5856 hex_string (header.signature));
5857
5858 info_ptr += length;
5859 }
5860 }
5861
5862 /* Create the hash table of all entries in the .debug_types
5863 (or .debug_types.dwo) section(s).
5864 DWO_FILE is a pointer to the DWO file object.
5865
5866 The result is a pointer to the hash table or NULL if there are no types.
5867
5868 Note: This function processes DWO files only, not DWP files. */
5869
5870 static void
5871 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5872 struct dwo_file *dwo_file,
5873 gdb::array_view<dwarf2_section_info> type_sections,
5874 htab_up &types_htab)
5875 {
5876 for (dwarf2_section_info &section : type_sections)
5877 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5878 rcuh_kind::TYPE);
5879 }
5880
5881 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5882 If SLOT is non-NULL, it is the entry to use in the hash table.
5883 Otherwise we find one. */
5884
5885 static struct signatured_type *
5886 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5887 {
5888 if (per_objfile->per_bfd->all_comp_units.size ()
5889 == per_objfile->per_bfd->all_comp_units.capacity ())
5890 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5891
5892 std::unique_ptr<signatured_type> sig_type_holder
5893 = per_objfile->per_bfd->allocate_signatured_type ();
5894 signatured_type *sig_type = sig_type_holder.get ();
5895
5896 per_objfile->resize_symtabs ();
5897
5898 per_objfile->per_bfd->all_comp_units.emplace_back
5899 (sig_type_holder.release ());
5900 sig_type->signature = sig;
5901 sig_type->is_debug_types = 1;
5902 if (per_objfile->per_bfd->using_index)
5903 {
5904 sig_type->v.quick =
5905 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
5906 struct dwarf2_per_cu_quick_data);
5907 }
5908
5909 if (slot == NULL)
5910 {
5911 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5912 sig_type, INSERT);
5913 }
5914 gdb_assert (*slot == NULL);
5915 *slot = sig_type;
5916 /* The rest of sig_type must be filled in by the caller. */
5917 return sig_type;
5918 }
5919
5920 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5921 Fill in SIG_ENTRY with DWO_ENTRY. */
5922
5923 static void
5924 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5925 struct signatured_type *sig_entry,
5926 struct dwo_unit *dwo_entry)
5927 {
5928 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5929
5930 /* Make sure we're not clobbering something we don't expect to. */
5931 gdb_assert (! sig_entry->queued);
5932 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5933 if (per_bfd->using_index)
5934 {
5935 gdb_assert (sig_entry->v.quick != NULL);
5936 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5937 }
5938 else
5939 gdb_assert (sig_entry->v.psymtab == NULL);
5940 gdb_assert (sig_entry->signature == dwo_entry->signature);
5941 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5942 gdb_assert (sig_entry->type_unit_group == NULL);
5943 gdb_assert (sig_entry->dwo_unit == NULL);
5944
5945 sig_entry->section = dwo_entry->section;
5946 sig_entry->sect_off = dwo_entry->sect_off;
5947 sig_entry->length = dwo_entry->length;
5948 sig_entry->reading_dwo_directly = 1;
5949 sig_entry->per_bfd = per_bfd;
5950 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5951 sig_entry->dwo_unit = dwo_entry;
5952 }
5953
5954 /* Subroutine of lookup_signatured_type.
5955 If we haven't read the TU yet, create the signatured_type data structure
5956 for a TU to be read in directly from a DWO file, bypassing the stub.
5957 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5958 using .gdb_index, then when reading a CU we want to stay in the DWO file
5959 containing that CU. Otherwise we could end up reading several other DWO
5960 files (due to comdat folding) to process the transitive closure of all the
5961 mentioned TUs, and that can be slow. The current DWO file will have every
5962 type signature that it needs.
5963 We only do this for .gdb_index because in the psymtab case we already have
5964 to read all the DWOs to build the type unit groups. */
5965
5966 static struct signatured_type *
5967 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5968 {
5969 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5970 struct dwo_file *dwo_file;
5971 struct dwo_unit find_dwo_entry, *dwo_entry;
5972 struct signatured_type find_sig_entry, *sig_entry;
5973 void **slot;
5974
5975 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
5976
5977 /* If TU skeletons have been removed then we may not have read in any
5978 TUs yet. */
5979 if (per_objfile->per_bfd->signatured_types == NULL)
5980 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5981
5982 /* We only ever need to read in one copy of a signatured type.
5983 Use the global signatured_types array to do our own comdat-folding
5984 of types. If this is the first time we're reading this TU, and
5985 the TU has an entry in .gdb_index, replace the recorded data from
5986 .gdb_index with this TU. */
5987
5988 find_sig_entry.signature = sig;
5989 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5990 &find_sig_entry, INSERT);
5991 sig_entry = (struct signatured_type *) *slot;
5992
5993 /* We can get here with the TU already read, *or* in the process of being
5994 read. Don't reassign the global entry to point to this DWO if that's
5995 the case. Also note that if the TU is already being read, it may not
5996 have come from a DWO, the program may be a mix of Fission-compiled
5997 code and non-Fission-compiled code. */
5998
5999 /* Have we already tried to read this TU?
6000 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6001 needn't exist in the global table yet). */
6002 if (sig_entry != NULL && sig_entry->tu_read)
6003 return sig_entry;
6004
6005 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6006 dwo_unit of the TU itself. */
6007 dwo_file = cu->dwo_unit->dwo_file;
6008
6009 /* Ok, this is the first time we're reading this TU. */
6010 if (dwo_file->tus == NULL)
6011 return NULL;
6012 find_dwo_entry.signature = sig;
6013 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6014 &find_dwo_entry);
6015 if (dwo_entry == NULL)
6016 return NULL;
6017
6018 /* If the global table doesn't have an entry for this TU, add one. */
6019 if (sig_entry == NULL)
6020 sig_entry = add_type_unit (per_objfile, sig, slot);
6021
6022 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6023 sig_entry->tu_read = 1;
6024 return sig_entry;
6025 }
6026
6027 /* Subroutine of lookup_signatured_type.
6028 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6029 then try the DWP file. If the TU stub (skeleton) has been removed then
6030 it won't be in .gdb_index. */
6031
6032 static struct signatured_type *
6033 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6034 {
6035 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6036 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6037 struct dwo_unit *dwo_entry;
6038 struct signatured_type find_sig_entry, *sig_entry;
6039 void **slot;
6040
6041 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6042 gdb_assert (dwp_file != NULL);
6043
6044 /* If TU skeletons have been removed then we may not have read in any
6045 TUs yet. */
6046 if (per_objfile->per_bfd->signatured_types == NULL)
6047 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6048
6049 find_sig_entry.signature = sig;
6050 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6051 &find_sig_entry, INSERT);
6052 sig_entry = (struct signatured_type *) *slot;
6053
6054 /* Have we already tried to read this TU?
6055 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6056 needn't exist in the global table yet). */
6057 if (sig_entry != NULL)
6058 return sig_entry;
6059
6060 if (dwp_file->tus == NULL)
6061 return NULL;
6062 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6063 1 /* is_debug_types */);
6064 if (dwo_entry == NULL)
6065 return NULL;
6066
6067 sig_entry = add_type_unit (per_objfile, sig, slot);
6068 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6069
6070 return sig_entry;
6071 }
6072
6073 /* Lookup a signature based type for DW_FORM_ref_sig8.
6074 Returns NULL if signature SIG is not present in the table.
6075 It is up to the caller to complain about this. */
6076
6077 static struct signatured_type *
6078 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6079 {
6080 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6081
6082 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6083 {
6084 /* We're in a DWO/DWP file, and we're using .gdb_index.
6085 These cases require special processing. */
6086 if (get_dwp_file (per_objfile) == NULL)
6087 return lookup_dwo_signatured_type (cu, sig);
6088 else
6089 return lookup_dwp_signatured_type (cu, sig);
6090 }
6091 else
6092 {
6093 struct signatured_type find_entry, *entry;
6094
6095 if (per_objfile->per_bfd->signatured_types == NULL)
6096 return NULL;
6097 find_entry.signature = sig;
6098 entry = ((struct signatured_type *)
6099 htab_find (per_objfile->per_bfd->signatured_types.get (),
6100 &find_entry));
6101 return entry;
6102 }
6103 }
6104
6105 /* Low level DIE reading support. */
6106
6107 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6108
6109 static void
6110 init_cu_die_reader (struct die_reader_specs *reader,
6111 struct dwarf2_cu *cu,
6112 struct dwarf2_section_info *section,
6113 struct dwo_file *dwo_file,
6114 struct abbrev_table *abbrev_table)
6115 {
6116 gdb_assert (section->readin && section->buffer != NULL);
6117 reader->abfd = section->get_bfd_owner ();
6118 reader->cu = cu;
6119 reader->dwo_file = dwo_file;
6120 reader->die_section = section;
6121 reader->buffer = section->buffer;
6122 reader->buffer_end = section->buffer + section->size;
6123 reader->abbrev_table = abbrev_table;
6124 }
6125
6126 /* Subroutine of cutu_reader to simplify it.
6127 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6128 There's just a lot of work to do, and cutu_reader is big enough
6129 already.
6130
6131 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6132 from it to the DIE in the DWO. If NULL we are skipping the stub.
6133 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6134 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6135 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6136 STUB_COMP_DIR may be non-NULL.
6137 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6138 are filled in with the info of the DIE from the DWO file.
6139 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6140 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6141 kept around for at least as long as *RESULT_READER.
6142
6143 The result is non-zero if a valid (non-dummy) DIE was found. */
6144
6145 static int
6146 read_cutu_die_from_dwo (dwarf2_cu *cu,
6147 struct dwo_unit *dwo_unit,
6148 struct die_info *stub_comp_unit_die,
6149 const char *stub_comp_dir,
6150 struct die_reader_specs *result_reader,
6151 const gdb_byte **result_info_ptr,
6152 struct die_info **result_comp_unit_die,
6153 abbrev_table_up *result_dwo_abbrev_table)
6154 {
6155 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6156 dwarf2_per_cu_data *per_cu = cu->per_cu;
6157 struct objfile *objfile = per_objfile->objfile;
6158 bfd *abfd;
6159 const gdb_byte *begin_info_ptr, *info_ptr;
6160 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6161 int i,num_extra_attrs;
6162 struct dwarf2_section_info *dwo_abbrev_section;
6163 struct die_info *comp_unit_die;
6164
6165 /* At most one of these may be provided. */
6166 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6167
6168 /* These attributes aren't processed until later:
6169 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6170 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6171 referenced later. However, these attributes are found in the stub
6172 which we won't have later. In order to not impose this complication
6173 on the rest of the code, we read them here and copy them to the
6174 DWO CU/TU die. */
6175
6176 stmt_list = NULL;
6177 low_pc = NULL;
6178 high_pc = NULL;
6179 ranges = NULL;
6180 comp_dir = NULL;
6181
6182 if (stub_comp_unit_die != NULL)
6183 {
6184 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6185 DWO file. */
6186 if (!per_cu->is_debug_types)
6187 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6188 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6189 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6190 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6191 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6192
6193 cu->addr_base = stub_comp_unit_die->addr_base ();
6194
6195 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6196 We need the value before we can process DW_AT_ranges values from the
6197 DWO. */
6198 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6199
6200 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6201 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6202 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6203 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6204 section. */
6205 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6206 }
6207 else if (stub_comp_dir != NULL)
6208 {
6209 /* Reconstruct the comp_dir attribute to simplify the code below. */
6210 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6211 comp_dir->name = DW_AT_comp_dir;
6212 comp_dir->form = DW_FORM_string;
6213 comp_dir->set_string_noncanonical (stub_comp_dir);
6214 }
6215
6216 /* Set up for reading the DWO CU/TU. */
6217 cu->dwo_unit = dwo_unit;
6218 dwarf2_section_info *section = dwo_unit->section;
6219 section->read (objfile);
6220 abfd = section->get_bfd_owner ();
6221 begin_info_ptr = info_ptr = (section->buffer
6222 + to_underlying (dwo_unit->sect_off));
6223 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6224
6225 if (per_cu->is_debug_types)
6226 {
6227 signatured_type *sig_type = (struct signatured_type *) per_cu;
6228
6229 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6230 section, dwo_abbrev_section,
6231 info_ptr, rcuh_kind::TYPE);
6232 /* This is not an assert because it can be caused by bad debug info. */
6233 if (sig_type->signature != cu->header.signature)
6234 {
6235 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6236 " TU at offset %s [in module %s]"),
6237 hex_string (sig_type->signature),
6238 hex_string (cu->header.signature),
6239 sect_offset_str (dwo_unit->sect_off),
6240 bfd_get_filename (abfd));
6241 }
6242 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6243 /* For DWOs coming from DWP files, we don't know the CU length
6244 nor the type's offset in the TU until now. */
6245 dwo_unit->length = cu->header.get_length ();
6246 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6247
6248 /* Establish the type offset that can be used to lookup the type.
6249 For DWO files, we don't know it until now. */
6250 sig_type->type_offset_in_section
6251 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6252 }
6253 else
6254 {
6255 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6256 section, dwo_abbrev_section,
6257 info_ptr, rcuh_kind::COMPILE);
6258 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6259 /* For DWOs coming from DWP files, we don't know the CU length
6260 until now. */
6261 dwo_unit->length = cu->header.get_length ();
6262 }
6263
6264 dwo_abbrev_section->read (objfile);
6265 *result_dwo_abbrev_table
6266 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6267 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6268 result_dwo_abbrev_table->get ());
6269
6270 /* Read in the die, but leave space to copy over the attributes
6271 from the stub. This has the benefit of simplifying the rest of
6272 the code - all the work to maintain the illusion of a single
6273 DW_TAG_{compile,type}_unit DIE is done here. */
6274 num_extra_attrs = ((stmt_list != NULL)
6275 + (low_pc != NULL)
6276 + (high_pc != NULL)
6277 + (ranges != NULL)
6278 + (comp_dir != NULL));
6279 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6280 num_extra_attrs);
6281
6282 /* Copy over the attributes from the stub to the DIE we just read in. */
6283 comp_unit_die = *result_comp_unit_die;
6284 i = comp_unit_die->num_attrs;
6285 if (stmt_list != NULL)
6286 comp_unit_die->attrs[i++] = *stmt_list;
6287 if (low_pc != NULL)
6288 comp_unit_die->attrs[i++] = *low_pc;
6289 if (high_pc != NULL)
6290 comp_unit_die->attrs[i++] = *high_pc;
6291 if (ranges != NULL)
6292 comp_unit_die->attrs[i++] = *ranges;
6293 if (comp_dir != NULL)
6294 comp_unit_die->attrs[i++] = *comp_dir;
6295 comp_unit_die->num_attrs += num_extra_attrs;
6296
6297 if (dwarf_die_debug)
6298 {
6299 fprintf_unfiltered (gdb_stdlog,
6300 "Read die from %s@0x%x of %s:\n",
6301 section->get_name (),
6302 (unsigned) (begin_info_ptr - section->buffer),
6303 bfd_get_filename (abfd));
6304 dump_die (comp_unit_die, dwarf_die_debug);
6305 }
6306
6307 /* Skip dummy compilation units. */
6308 if (info_ptr >= begin_info_ptr + dwo_unit->length
6309 || peek_abbrev_code (abfd, info_ptr) == 0)
6310 return 0;
6311
6312 *result_info_ptr = info_ptr;
6313 return 1;
6314 }
6315
6316 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6317 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6318 signature is part of the header. */
6319 static gdb::optional<ULONGEST>
6320 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6321 {
6322 if (cu->header.version >= 5)
6323 return cu->header.signature;
6324 struct attribute *attr;
6325 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6326 if (attr == nullptr || !attr->form_is_unsigned ())
6327 return gdb::optional<ULONGEST> ();
6328 return attr->as_unsigned ();
6329 }
6330
6331 /* Subroutine of cutu_reader to simplify it.
6332 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6333 Returns NULL if the specified DWO unit cannot be found. */
6334
6335 static struct dwo_unit *
6336 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6337 {
6338 dwarf2_per_cu_data *per_cu = cu->per_cu;
6339 struct dwo_unit *dwo_unit;
6340 const char *comp_dir;
6341
6342 gdb_assert (cu != NULL);
6343
6344 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6345 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6346 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6347
6348 if (per_cu->is_debug_types)
6349 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6350 else
6351 {
6352 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6353
6354 if (!signature.has_value ())
6355 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6356 " [in module %s]"),
6357 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6358
6359 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6360 }
6361
6362 return dwo_unit;
6363 }
6364
6365 /* Subroutine of cutu_reader to simplify it.
6366 See it for a description of the parameters.
6367 Read a TU directly from a DWO file, bypassing the stub. */
6368
6369 void
6370 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6371 dwarf2_per_objfile *per_objfile,
6372 dwarf2_cu *existing_cu)
6373 {
6374 struct signatured_type *sig_type;
6375
6376 /* Verify we can do the following downcast, and that we have the
6377 data we need. */
6378 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6379 sig_type = (struct signatured_type *) this_cu;
6380 gdb_assert (sig_type->dwo_unit != NULL);
6381
6382 dwarf2_cu *cu;
6383
6384 if (existing_cu != nullptr)
6385 {
6386 cu = existing_cu;
6387 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6388 /* There's no need to do the rereading_dwo_cu handling that
6389 cutu_reader does since we don't read the stub. */
6390 }
6391 else
6392 {
6393 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6394 in per_objfile yet. */
6395 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6396 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6397 cu = m_new_cu.get ();
6398 }
6399
6400 /* A future optimization, if needed, would be to use an existing
6401 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6402 could share abbrev tables. */
6403
6404 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6405 NULL /* stub_comp_unit_die */,
6406 sig_type->dwo_unit->dwo_file->comp_dir,
6407 this, &info_ptr,
6408 &comp_unit_die,
6409 &m_dwo_abbrev_table) == 0)
6410 {
6411 /* Dummy die. */
6412 dummy_p = true;
6413 }
6414 }
6415
6416 /* Initialize a CU (or TU) and read its DIEs.
6417 If the CU defers to a DWO file, read the DWO file as well.
6418
6419 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6420 Otherwise the table specified in the comp unit header is read in and used.
6421 This is an optimization for when we already have the abbrev table.
6422
6423 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6424 allocated. */
6425
6426 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6427 dwarf2_per_objfile *per_objfile,
6428 struct abbrev_table *abbrev_table,
6429 dwarf2_cu *existing_cu,
6430 bool skip_partial)
6431 : die_reader_specs {},
6432 m_this_cu (this_cu)
6433 {
6434 struct objfile *objfile = per_objfile->objfile;
6435 struct dwarf2_section_info *section = this_cu->section;
6436 bfd *abfd = section->get_bfd_owner ();
6437 const gdb_byte *begin_info_ptr;
6438 struct signatured_type *sig_type = NULL;
6439 struct dwarf2_section_info *abbrev_section;
6440 /* Non-zero if CU currently points to a DWO file and we need to
6441 reread it. When this happens we need to reread the skeleton die
6442 before we can reread the DWO file (this only applies to CUs, not TUs). */
6443 int rereading_dwo_cu = 0;
6444
6445 if (dwarf_die_debug)
6446 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6447 this_cu->is_debug_types ? "type" : "comp",
6448 sect_offset_str (this_cu->sect_off));
6449
6450 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6451 file (instead of going through the stub), short-circuit all of this. */
6452 if (this_cu->reading_dwo_directly)
6453 {
6454 /* Narrow down the scope of possibilities to have to understand. */
6455 gdb_assert (this_cu->is_debug_types);
6456 gdb_assert (abbrev_table == NULL);
6457 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6458 return;
6459 }
6460
6461 /* This is cheap if the section is already read in. */
6462 section->read (objfile);
6463
6464 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6465
6466 abbrev_section = get_abbrev_section_for_cu (this_cu);
6467
6468 dwarf2_cu *cu;
6469
6470 if (existing_cu != nullptr)
6471 {
6472 cu = existing_cu;
6473 /* If this CU is from a DWO file we need to start over, we need to
6474 refetch the attributes from the skeleton CU.
6475 This could be optimized by retrieving those attributes from when we
6476 were here the first time: the previous comp_unit_die was stored in
6477 comp_unit_obstack. But there's no data yet that we need this
6478 optimization. */
6479 if (cu->dwo_unit != NULL)
6480 rereading_dwo_cu = 1;
6481 }
6482 else
6483 {
6484 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6485 in per_objfile yet. */
6486 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6487 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6488 cu = m_new_cu.get ();
6489 }
6490
6491 /* Get the header. */
6492 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6493 {
6494 /* We already have the header, there's no need to read it in again. */
6495 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6496 }
6497 else
6498 {
6499 if (this_cu->is_debug_types)
6500 {
6501 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6502 section, abbrev_section,
6503 info_ptr, rcuh_kind::TYPE);
6504
6505 /* Since per_cu is the first member of struct signatured_type,
6506 we can go from a pointer to one to a pointer to the other. */
6507 sig_type = (struct signatured_type *) this_cu;
6508 gdb_assert (sig_type->signature == cu->header.signature);
6509 gdb_assert (sig_type->type_offset_in_tu
6510 == cu->header.type_cu_offset_in_tu);
6511 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6512
6513 /* LENGTH has not been set yet for type units if we're
6514 using .gdb_index. */
6515 this_cu->length = cu->header.get_length ();
6516
6517 /* Establish the type offset that can be used to lookup the type. */
6518 sig_type->type_offset_in_section =
6519 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6520
6521 this_cu->dwarf_version = cu->header.version;
6522 }
6523 else
6524 {
6525 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6526 section, abbrev_section,
6527 info_ptr,
6528 rcuh_kind::COMPILE);
6529
6530 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6531 if (this_cu->length == 0)
6532 this_cu->length = cu->header.get_length ();
6533 else
6534 gdb_assert (this_cu->length == cu->header.get_length ());
6535 this_cu->dwarf_version = cu->header.version;
6536 }
6537 }
6538
6539 /* Skip dummy compilation units. */
6540 if (info_ptr >= begin_info_ptr + this_cu->length
6541 || peek_abbrev_code (abfd, info_ptr) == 0)
6542 {
6543 dummy_p = true;
6544 return;
6545 }
6546
6547 /* If we don't have them yet, read the abbrevs for this compilation unit.
6548 And if we need to read them now, make sure they're freed when we're
6549 done. */
6550 if (abbrev_table != NULL)
6551 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6552 else
6553 {
6554 abbrev_section->read (objfile);
6555 m_abbrev_table_holder
6556 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6557 abbrev_table = m_abbrev_table_holder.get ();
6558 }
6559
6560 /* Read the top level CU/TU die. */
6561 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6562 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6563
6564 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6565 {
6566 dummy_p = true;
6567 return;
6568 }
6569
6570 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6571 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6572 table from the DWO file and pass the ownership over to us. It will be
6573 referenced from READER, so we must make sure to free it after we're done
6574 with READER.
6575
6576 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6577 DWO CU, that this test will fail (the attribute will not be present). */
6578 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6579 if (dwo_name != nullptr)
6580 {
6581 struct dwo_unit *dwo_unit;
6582 struct die_info *dwo_comp_unit_die;
6583
6584 if (comp_unit_die->has_children)
6585 {
6586 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6587 " has children (offset %s) [in module %s]"),
6588 sect_offset_str (this_cu->sect_off),
6589 bfd_get_filename (abfd));
6590 }
6591 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6592 if (dwo_unit != NULL)
6593 {
6594 if (read_cutu_die_from_dwo (cu, dwo_unit,
6595 comp_unit_die, NULL,
6596 this, &info_ptr,
6597 &dwo_comp_unit_die,
6598 &m_dwo_abbrev_table) == 0)
6599 {
6600 /* Dummy die. */
6601 dummy_p = true;
6602 return;
6603 }
6604 comp_unit_die = dwo_comp_unit_die;
6605 }
6606 else
6607 {
6608 /* Yikes, we couldn't find the rest of the DIE, we only have
6609 the stub. A complaint has already been logged. There's
6610 not much more we can do except pass on the stub DIE to
6611 die_reader_func. We don't want to throw an error on bad
6612 debug info. */
6613 }
6614 }
6615 }
6616
6617 void
6618 cutu_reader::keep ()
6619 {
6620 /* Done, clean up. */
6621 gdb_assert (!dummy_p);
6622 if (m_new_cu != NULL)
6623 {
6624 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6625 now. */
6626 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6627 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6628 }
6629 }
6630
6631 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6632 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6633 assumed to have already done the lookup to find the DWO file).
6634
6635 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6636 THIS_CU->is_debug_types, but nothing else.
6637
6638 We fill in THIS_CU->length.
6639
6640 THIS_CU->cu is always freed when done.
6641 This is done in order to not leave THIS_CU->cu in a state where we have
6642 to care whether it refers to the "main" CU or the DWO CU.
6643
6644 When parent_cu is passed, it is used to provide a default value for
6645 str_offsets_base and addr_base from the parent. */
6646
6647 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6648 dwarf2_per_objfile *per_objfile,
6649 struct dwarf2_cu *parent_cu,
6650 struct dwo_file *dwo_file)
6651 : die_reader_specs {},
6652 m_this_cu (this_cu)
6653 {
6654 struct objfile *objfile = per_objfile->objfile;
6655 struct dwarf2_section_info *section = this_cu->section;
6656 bfd *abfd = section->get_bfd_owner ();
6657 struct dwarf2_section_info *abbrev_section;
6658 const gdb_byte *begin_info_ptr, *info_ptr;
6659
6660 if (dwarf_die_debug)
6661 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6662 this_cu->is_debug_types ? "type" : "comp",
6663 sect_offset_str (this_cu->sect_off));
6664
6665 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6666
6667 abbrev_section = (dwo_file != NULL
6668 ? &dwo_file->sections.abbrev
6669 : get_abbrev_section_for_cu (this_cu));
6670
6671 /* This is cheap if the section is already read in. */
6672 section->read (objfile);
6673
6674 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6675
6676 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6677 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6678 section, abbrev_section, info_ptr,
6679 (this_cu->is_debug_types
6680 ? rcuh_kind::TYPE
6681 : rcuh_kind::COMPILE));
6682
6683 if (parent_cu != nullptr)
6684 {
6685 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6686 m_new_cu->addr_base = parent_cu->addr_base;
6687 }
6688 this_cu->length = m_new_cu->header.get_length ();
6689
6690 /* Skip dummy compilation units. */
6691 if (info_ptr >= begin_info_ptr + this_cu->length
6692 || peek_abbrev_code (abfd, info_ptr) == 0)
6693 {
6694 dummy_p = true;
6695 return;
6696 }
6697
6698 abbrev_section->read (objfile);
6699 m_abbrev_table_holder
6700 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6701
6702 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6703 m_abbrev_table_holder.get ());
6704 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6705 }
6706
6707 \f
6708 /* Type Unit Groups.
6709
6710 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6711 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6712 so that all types coming from the same compilation (.o file) are grouped
6713 together. A future step could be to put the types in the same symtab as
6714 the CU the types ultimately came from. */
6715
6716 static hashval_t
6717 hash_type_unit_group (const void *item)
6718 {
6719 const struct type_unit_group *tu_group
6720 = (const struct type_unit_group *) item;
6721
6722 return hash_stmt_list_entry (&tu_group->hash);
6723 }
6724
6725 static int
6726 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6727 {
6728 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6729 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6730
6731 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6732 }
6733
6734 /* Allocate a hash table for type unit groups. */
6735
6736 static htab_up
6737 allocate_type_unit_groups_table ()
6738 {
6739 return htab_up (htab_create_alloc (3,
6740 hash_type_unit_group,
6741 eq_type_unit_group,
6742 [] (void *arg)
6743 {
6744 type_unit_group *grp
6745 = (type_unit_group *) arg;
6746 delete grp;
6747 },
6748 xcalloc, xfree));
6749 }
6750
6751 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6752 partial symtabs. We combine several TUs per psymtab to not let the size
6753 of any one psymtab grow too big. */
6754 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6755 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6756
6757 /* Helper routine for get_type_unit_group.
6758 Create the type_unit_group object used to hold one or more TUs. */
6759
6760 static std::unique_ptr<type_unit_group>
6761 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6762 {
6763 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6764 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6765
6766 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6767 tu_group->per_bfd = per_bfd;
6768
6769 if (per_bfd->using_index)
6770 {
6771 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6772 struct dwarf2_per_cu_quick_data);
6773 }
6774 else
6775 {
6776 unsigned int line_offset = to_underlying (line_offset_struct);
6777 dwarf2_psymtab *pst;
6778 std::string name;
6779
6780 /* Give the symtab a useful name for debug purposes. */
6781 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6782 name = string_printf ("<type_units_%d>",
6783 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6784 else
6785 name = string_printf ("<type_units_at_0x%x>", line_offset);
6786
6787 pst = create_partial_symtab (tu_group.get (), per_objfile,
6788 name.c_str ());
6789 pst->anonymous = true;
6790 }
6791
6792 tu_group->hash.dwo_unit = cu->dwo_unit;
6793 tu_group->hash.line_sect_off = line_offset_struct;
6794
6795 return tu_group;
6796 }
6797
6798 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6799 STMT_LIST is a DW_AT_stmt_list attribute. */
6800
6801 static struct type_unit_group *
6802 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6803 {
6804 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6805 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6806 struct type_unit_group *tu_group;
6807 void **slot;
6808 unsigned int line_offset;
6809 struct type_unit_group type_unit_group_for_lookup;
6810
6811 if (per_objfile->per_bfd->type_unit_groups == NULL)
6812 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6813
6814 /* Do we need to create a new group, or can we use an existing one? */
6815
6816 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6817 {
6818 line_offset = stmt_list->as_unsigned ();
6819 ++tu_stats->nr_symtab_sharers;
6820 }
6821 else
6822 {
6823 /* Ugh, no stmt_list. Rare, but we have to handle it.
6824 We can do various things here like create one group per TU or
6825 spread them over multiple groups to split up the expansion work.
6826 To avoid worst case scenarios (too many groups or too large groups)
6827 we, umm, group them in bunches. */
6828 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6829 | (tu_stats->nr_stmt_less_type_units
6830 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6831 ++tu_stats->nr_stmt_less_type_units;
6832 }
6833
6834 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6835 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6836 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6837 &type_unit_group_for_lookup, INSERT);
6838 if (*slot == nullptr)
6839 {
6840 sect_offset line_offset_struct = (sect_offset) line_offset;
6841 std::unique_ptr<type_unit_group> grp
6842 = create_type_unit_group (cu, line_offset_struct);
6843 *slot = grp.release ();
6844 ++tu_stats->nr_symtabs;
6845 }
6846
6847 tu_group = (struct type_unit_group *) *slot;
6848 gdb_assert (tu_group != nullptr);
6849 return tu_group;
6850 }
6851 \f
6852 /* Partial symbol tables. */
6853
6854 /* Create a psymtab named NAME and assign it to PER_CU.
6855
6856 The caller must fill in the following details:
6857 dirname, textlow, texthigh. */
6858
6859 static dwarf2_psymtab *
6860 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6861 dwarf2_per_objfile *per_objfile,
6862 const char *name)
6863 {
6864 dwarf2_psymtab *pst
6865 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6866 per_objfile->objfile->per_bfd, per_cu);
6867
6868 pst->psymtabs_addrmap_supported = true;
6869
6870 /* This is the glue that links PST into GDB's symbol API. */
6871 per_cu->v.psymtab = pst;
6872
6873 return pst;
6874 }
6875
6876 /* DIE reader function for process_psymtab_comp_unit. */
6877
6878 static void
6879 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6880 const gdb_byte *info_ptr,
6881 struct die_info *comp_unit_die,
6882 enum language pretend_language)
6883 {
6884 struct dwarf2_cu *cu = reader->cu;
6885 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6886 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6887 struct objfile *objfile = per_objfile->objfile;
6888 struct gdbarch *gdbarch = objfile->arch ();
6889 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6890 CORE_ADDR baseaddr;
6891 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6892 dwarf2_psymtab *pst;
6893 enum pc_bounds_kind cu_bounds_kind;
6894 const char *filename;
6895
6896 gdb_assert (! per_cu->is_debug_types);
6897
6898 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
6899
6900 /* Allocate a new partial symbol table structure. */
6901 gdb::unique_xmalloc_ptr<char> debug_filename;
6902 static const char artificial[] = "<artificial>";
6903 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6904 if (filename == NULL)
6905 filename = "";
6906 else if (strcmp (filename, artificial) == 0)
6907 {
6908 debug_filename.reset (concat (artificial, "@",
6909 sect_offset_str (per_cu->sect_off),
6910 (char *) NULL));
6911 filename = debug_filename.get ();
6912 }
6913
6914 pst = create_partial_symtab (per_cu, per_objfile, filename);
6915
6916 /* This must be done before calling dwarf2_build_include_psymtabs. */
6917 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6918
6919 baseaddr = objfile->text_section_offset ();
6920
6921 dwarf2_find_base_address (comp_unit_die, cu);
6922
6923 /* Possibly set the default values of LOWPC and HIGHPC from
6924 `DW_AT_ranges'. */
6925 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6926 &best_highpc, cu, pst);
6927 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6928 {
6929 CORE_ADDR low
6930 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
6931 - baseaddr);
6932 CORE_ADDR high
6933 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
6934 - baseaddr - 1);
6935 /* Store the contiguous range if it is not empty; it can be
6936 empty for CUs with no code. */
6937 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
6938 low, high, pst);
6939 }
6940
6941 /* Check if comp unit has_children.
6942 If so, read the rest of the partial symbols from this comp unit.
6943 If not, there's no more debug_info for this comp unit. */
6944 if (comp_unit_die->has_children)
6945 {
6946 struct partial_die_info *first_die;
6947 CORE_ADDR lowpc, highpc;
6948
6949 lowpc = ((CORE_ADDR) -1);
6950 highpc = ((CORE_ADDR) 0);
6951
6952 first_die = load_partial_dies (reader, info_ptr, 1);
6953
6954 scan_partial_symbols (first_die, &lowpc, &highpc,
6955 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6956
6957 /* If we didn't find a lowpc, set it to highpc to avoid
6958 complaints from `maint check'. */
6959 if (lowpc == ((CORE_ADDR) -1))
6960 lowpc = highpc;
6961
6962 /* If the compilation unit didn't have an explicit address range,
6963 then use the information extracted from its child dies. */
6964 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6965 {
6966 best_lowpc = lowpc;
6967 best_highpc = highpc;
6968 }
6969 }
6970 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
6971 best_lowpc + baseaddr)
6972 - baseaddr);
6973 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
6974 best_highpc + baseaddr)
6975 - baseaddr);
6976
6977 pst->end ();
6978
6979 if (!cu->per_cu->imported_symtabs_empty ())
6980 {
6981 int i;
6982 int len = cu->per_cu->imported_symtabs_size ();
6983
6984 /* Fill in 'dependencies' here; we fill in 'users' in a
6985 post-pass. */
6986 pst->number_of_dependencies = len;
6987 pst->dependencies
6988 = per_bfd->partial_symtabs->allocate_dependencies (len);
6989 for (i = 0; i < len; ++i)
6990 {
6991 pst->dependencies[i]
6992 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
6993 }
6994
6995 cu->per_cu->imported_symtabs_free ();
6996 }
6997
6998 /* Get the list of files included in the current compilation unit,
6999 and build a psymtab for each of them. */
7000 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7001
7002 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7003 ", %d global, %d static syms",
7004 per_cu->is_debug_types ? "type" : "comp",
7005 sect_offset_str (per_cu->sect_off),
7006 paddress (gdbarch, pst->text_low (objfile)),
7007 paddress (gdbarch, pst->text_high (objfile)),
7008 (int) pst->global_psymbols.size (),
7009 (int) pst->static_psymbols.size ());
7010 }
7011
7012 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7013 Process compilation unit THIS_CU for a psymtab. */
7014
7015 static void
7016 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7017 dwarf2_per_objfile *per_objfile,
7018 bool want_partial_unit,
7019 enum language pretend_language)
7020 {
7021 /* If this compilation unit was already read in, free the
7022 cached copy in order to read it in again. This is
7023 necessary because we skipped some symbols when we first
7024 read in the compilation unit (see load_partial_dies).
7025 This problem could be avoided, but the benefit is unclear. */
7026 per_objfile->remove_cu (this_cu);
7027
7028 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7029
7030 switch (reader.comp_unit_die->tag)
7031 {
7032 case DW_TAG_compile_unit:
7033 this_cu->unit_type = DW_UT_compile;
7034 break;
7035 case DW_TAG_partial_unit:
7036 this_cu->unit_type = DW_UT_partial;
7037 break;
7038 case DW_TAG_type_unit:
7039 this_cu->unit_type = DW_UT_type;
7040 break;
7041 default:
7042 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7043 dwarf_tag_name (reader.comp_unit_die->tag),
7044 sect_offset_str (reader.cu->per_cu->sect_off),
7045 objfile_name (per_objfile->objfile));
7046 }
7047
7048 if (reader.dummy_p)
7049 {
7050 /* Nothing. */
7051 }
7052 else if (this_cu->is_debug_types)
7053 build_type_psymtabs_reader (&reader, reader.info_ptr,
7054 reader.comp_unit_die);
7055 else if (want_partial_unit
7056 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7057 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7058 reader.comp_unit_die,
7059 pretend_language);
7060
7061 this_cu->lang = reader.cu->language;
7062
7063 /* Age out any secondary CUs. */
7064 per_objfile->age_comp_units ();
7065 }
7066
7067 /* Reader function for build_type_psymtabs. */
7068
7069 static void
7070 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7071 const gdb_byte *info_ptr,
7072 struct die_info *type_unit_die)
7073 {
7074 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7075 struct dwarf2_cu *cu = reader->cu;
7076 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7077 struct signatured_type *sig_type;
7078 struct type_unit_group *tu_group;
7079 struct attribute *attr;
7080 struct partial_die_info *first_die;
7081 CORE_ADDR lowpc, highpc;
7082 dwarf2_psymtab *pst;
7083
7084 gdb_assert (per_cu->is_debug_types);
7085 sig_type = (struct signatured_type *) per_cu;
7086
7087 if (! type_unit_die->has_children)
7088 return;
7089
7090 attr = type_unit_die->attr (DW_AT_stmt_list);
7091 tu_group = get_type_unit_group (cu, attr);
7092
7093 if (tu_group->tus == nullptr)
7094 tu_group->tus = new std::vector<signatured_type *>;
7095 tu_group->tus->push_back (sig_type);
7096
7097 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7098 pst = create_partial_symtab (per_cu, per_objfile, "");
7099 pst->anonymous = true;
7100
7101 first_die = load_partial_dies (reader, info_ptr, 1);
7102
7103 lowpc = (CORE_ADDR) -1;
7104 highpc = (CORE_ADDR) 0;
7105 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7106
7107 pst->end ();
7108 }
7109
7110 /* Struct used to sort TUs by their abbreviation table offset. */
7111
7112 struct tu_abbrev_offset
7113 {
7114 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7115 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7116 {}
7117
7118 /* This is used when sorting. */
7119 bool operator< (const tu_abbrev_offset &other)
7120 {
7121 return abbrev_offset < other.abbrev_offset;
7122 }
7123
7124 signatured_type *sig_type;
7125 sect_offset abbrev_offset;
7126 };
7127
7128 /* Efficiently read all the type units.
7129
7130 The efficiency is because we sort TUs by the abbrev table they use and
7131 only read each abbrev table once. In one program there are 200K TUs
7132 sharing 8K abbrev tables.
7133
7134 The main purpose of this function is to support building the
7135 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7136 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7137 can collapse the search space by grouping them by stmt_list.
7138 The savings can be significant, in the same program from above the 200K TUs
7139 share 8K stmt_list tables.
7140
7141 FUNC is expected to call get_type_unit_group, which will create the
7142 struct type_unit_group if necessary and add it to
7143 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7144
7145 static void
7146 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7147 {
7148 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7149 abbrev_table_up abbrev_table;
7150 sect_offset abbrev_offset;
7151
7152 /* It's up to the caller to not call us multiple times. */
7153 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7154
7155 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7156 return;
7157
7158 /* TUs typically share abbrev tables, and there can be way more TUs than
7159 abbrev tables. Sort by abbrev table to reduce the number of times we
7160 read each abbrev table in.
7161 Alternatives are to punt or to maintain a cache of abbrev tables.
7162 This is simpler and efficient enough for now.
7163
7164 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7165 symtab to use). Typically TUs with the same abbrev offset have the same
7166 stmt_list value too so in practice this should work well.
7167
7168 The basic algorithm here is:
7169
7170 sort TUs by abbrev table
7171 for each TU with same abbrev table:
7172 read abbrev table if first user
7173 read TU top level DIE
7174 [IWBN if DWO skeletons had DW_AT_stmt_list]
7175 call FUNC */
7176
7177 dwarf_read_debug_printf ("Building type unit groups ...");
7178
7179 /* Sort in a separate table to maintain the order of all_comp_units
7180 for .gdb_index: TU indices directly index all_type_units. */
7181 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7182 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7183
7184 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7185 {
7186 if (cu->is_debug_types)
7187 {
7188 auto sig_type = static_cast<signatured_type *> (cu.get ());
7189 sorted_by_abbrev.emplace_back
7190 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7191 sig_type->sect_off));
7192 }
7193 }
7194
7195 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7196
7197 abbrev_offset = (sect_offset) ~(unsigned) 0;
7198
7199 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7200 {
7201 /* Switch to the next abbrev table if necessary. */
7202 if (abbrev_table == NULL
7203 || tu.abbrev_offset != abbrev_offset)
7204 {
7205 abbrev_offset = tu.abbrev_offset;
7206 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7207 abbrev_table =
7208 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7209 ++tu_stats->nr_uniq_abbrev_tables;
7210 }
7211
7212 cutu_reader reader (tu.sig_type, per_objfile,
7213 abbrev_table.get (), nullptr, false);
7214 if (!reader.dummy_p)
7215 build_type_psymtabs_reader (&reader, reader.info_ptr,
7216 reader.comp_unit_die);
7217 }
7218 }
7219
7220 /* Print collected type unit statistics. */
7221
7222 static void
7223 print_tu_stats (dwarf2_per_objfile *per_objfile)
7224 {
7225 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7226
7227 dwarf_read_debug_printf ("Type unit statistics:");
7228 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7229 dwarf_read_debug_printf (" %d uniq abbrev tables",
7230 tu_stats->nr_uniq_abbrev_tables);
7231 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7232 tu_stats->nr_symtabs);
7233 dwarf_read_debug_printf (" %d symtab sharers",
7234 tu_stats->nr_symtab_sharers);
7235 dwarf_read_debug_printf (" %d type units without a stmt_list",
7236 tu_stats->nr_stmt_less_type_units);
7237 dwarf_read_debug_printf (" %d all_type_units reallocs",
7238 tu_stats->nr_all_type_units_reallocs);
7239 }
7240
7241 /* Traversal function for build_type_psymtabs. */
7242
7243 static int
7244 build_type_psymtab_dependencies (void **slot, void *info)
7245 {
7246 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7247 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7248 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7249 dwarf2_psymtab *pst = tu_group->v.psymtab;
7250 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7251 int i;
7252
7253 gdb_assert (len > 0);
7254 gdb_assert (tu_group->type_unit_group_p ());
7255
7256 pst->number_of_dependencies = len;
7257 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7258 for (i = 0; i < len; ++i)
7259 {
7260 struct signatured_type *iter = tu_group->tus->at (i);
7261 gdb_assert (iter->is_debug_types);
7262 pst->dependencies[i] = iter->v.psymtab;
7263 iter->type_unit_group = tu_group;
7264 }
7265
7266 delete tu_group->tus;
7267 tu_group->tus = nullptr;
7268
7269 return 1;
7270 }
7271
7272 /* Traversal function for process_skeletonless_type_unit.
7273 Read a TU in a DWO file and build partial symbols for it. */
7274
7275 static int
7276 process_skeletonless_type_unit (void **slot, void *info)
7277 {
7278 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7279 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7280 struct signatured_type find_entry, *entry;
7281
7282 /* If this TU doesn't exist in the global table, add it and read it in. */
7283
7284 if (per_objfile->per_bfd->signatured_types == NULL)
7285 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7286
7287 find_entry.signature = dwo_unit->signature;
7288 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7289 &find_entry, INSERT);
7290 /* If we've already seen this type there's nothing to do. What's happening
7291 is we're doing our own version of comdat-folding here. */
7292 if (*slot != NULL)
7293 return 1;
7294
7295 /* This does the job that create_all_comp_units would have done for
7296 this TU. */
7297 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7298 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7299 *slot = entry;
7300
7301 /* This does the job that build_type_psymtabs would have done. */
7302 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7303 if (!reader.dummy_p)
7304 build_type_psymtabs_reader (&reader, reader.info_ptr,
7305 reader.comp_unit_die);
7306
7307 return 1;
7308 }
7309
7310 /* Traversal function for process_skeletonless_type_units. */
7311
7312 static int
7313 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7314 {
7315 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7316
7317 if (dwo_file->tus != NULL)
7318 htab_traverse_noresize (dwo_file->tus.get (),
7319 process_skeletonless_type_unit, info);
7320
7321 return 1;
7322 }
7323
7324 /* Scan all TUs of DWO files, verifying we've processed them.
7325 This is needed in case a TU was emitted without its skeleton.
7326 Note: This can't be done until we know what all the DWO files are. */
7327
7328 static void
7329 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7330 {
7331 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7332 if (get_dwp_file (per_objfile) == NULL
7333 && per_objfile->per_bfd->dwo_files != NULL)
7334 {
7335 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7336 process_dwo_file_for_skeletonless_type_units,
7337 per_objfile);
7338 }
7339 }
7340
7341 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7342
7343 static void
7344 set_partial_user (dwarf2_per_objfile *per_objfile)
7345 {
7346 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7347 {
7348 dwarf2_psymtab *pst = per_cu->v.psymtab;
7349
7350 if (pst == NULL)
7351 continue;
7352
7353 for (int j = 0; j < pst->number_of_dependencies; ++j)
7354 {
7355 /* Set the 'user' field only if it is not already set. */
7356 if (pst->dependencies[j]->user == NULL)
7357 pst->dependencies[j]->user = pst;
7358 }
7359 }
7360 }
7361
7362 /* Build the partial symbol table by doing a quick pass through the
7363 .debug_info and .debug_abbrev sections. */
7364
7365 static void
7366 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7367 {
7368 struct objfile *objfile = per_objfile->objfile;
7369 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7370
7371 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7372 objfile_name (objfile));
7373
7374 scoped_restore restore_reading_psyms
7375 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7376
7377 per_bfd->info.read (objfile);
7378
7379 /* Any cached compilation units will be linked by the per-objfile
7380 read_in_chain. Make sure to free them when we're done. */
7381 free_cached_comp_units freer (per_objfile);
7382
7383 create_all_comp_units (per_objfile);
7384 build_type_psymtabs (per_objfile);
7385
7386 /* Create a temporary address map on a temporary obstack. We later
7387 copy this to the final obstack. */
7388 auto_obstack temp_obstack;
7389
7390 scoped_restore save_psymtabs_addrmap
7391 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7392 addrmap_create_mutable (&temp_obstack));
7393
7394 for (const auto &per_cu : per_bfd->all_comp_units)
7395 {
7396 if (per_cu->v.psymtab != NULL)
7397 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7398 continue;
7399 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7400 language_minimal);
7401 }
7402
7403 /* This has to wait until we read the CUs, we need the list of DWOs. */
7404 process_skeletonless_type_units (per_objfile);
7405
7406 /* Now that all TUs have been processed we can fill in the dependencies. */
7407 if (per_bfd->type_unit_groups != NULL)
7408 {
7409 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7410 build_type_psymtab_dependencies, per_objfile);
7411 }
7412
7413 if (dwarf_read_debug > 0)
7414 print_tu_stats (per_objfile);
7415
7416 set_partial_user (per_objfile);
7417
7418 per_bfd->partial_symtabs->psymtabs_addrmap
7419 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7420 per_bfd->partial_symtabs->obstack ());
7421 /* At this point we want to keep the address map. */
7422 save_psymtabs_addrmap.release ();
7423
7424 dwarf_read_debug_printf ("Done building psymtabs of %s",
7425 objfile_name (objfile));
7426 }
7427
7428 /* Load the partial DIEs for a secondary CU into memory.
7429 This is also used when rereading a primary CU with load_all_dies. */
7430
7431 static void
7432 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7433 dwarf2_per_objfile *per_objfile,
7434 dwarf2_cu *existing_cu)
7435 {
7436 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7437
7438 if (!reader.dummy_p)
7439 {
7440 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7441 language_minimal);
7442
7443 /* Check if comp unit has_children.
7444 If so, read the rest of the partial symbols from this comp unit.
7445 If not, there's no more debug_info for this comp unit. */
7446 if (reader.comp_unit_die->has_children)
7447 load_partial_dies (&reader, reader.info_ptr, 0);
7448
7449 reader.keep ();
7450 }
7451 }
7452
7453 static void
7454 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7455 struct dwarf2_section_info *section,
7456 struct dwarf2_section_info *abbrev_section,
7457 unsigned int is_dwz,
7458 htab_up &types_htab,
7459 rcuh_kind section_kind)
7460 {
7461 const gdb_byte *info_ptr;
7462 struct objfile *objfile = per_objfile->objfile;
7463
7464 dwarf_read_debug_printf ("Reading %s for %s",
7465 section->get_name (),
7466 section->get_file_name ());
7467
7468 section->read (objfile);
7469
7470 info_ptr = section->buffer;
7471
7472 while (info_ptr < section->buffer + section->size)
7473 {
7474 dwarf2_per_cu_data_up this_cu;
7475
7476 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7477
7478 comp_unit_head cu_header;
7479 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7480 abbrev_section, info_ptr,
7481 section_kind);
7482
7483 /* Save the compilation unit for later lookup. */
7484 if (cu_header.unit_type != DW_UT_type)
7485 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7486 else
7487 {
7488 if (types_htab == nullptr)
7489 types_htab = allocate_signatured_type_table ();
7490
7491 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
7492 signatured_type *sig_ptr = sig_type.get ();
7493 sig_type->signature = cu_header.signature;
7494 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7495 this_cu.reset (sig_type.release ());
7496
7497 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7498 gdb_assert (slot != nullptr);
7499 if (*slot != nullptr)
7500 complaint (_("debug type entry at offset %s is duplicate to"
7501 " the entry at offset %s, signature %s"),
7502 sect_offset_str (sect_off),
7503 sect_offset_str (sig_ptr->sect_off),
7504 hex_string (sig_ptr->signature));
7505 *slot = sig_ptr;
7506 }
7507 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7508 this_cu->sect_off = sect_off;
7509 this_cu->length = cu_header.length + cu_header.initial_length_size;
7510 this_cu->is_dwz = is_dwz;
7511 this_cu->section = section;
7512
7513 info_ptr = info_ptr + this_cu->length;
7514 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7515 }
7516 }
7517
7518 /* Create a list of all compilation units in OBJFILE.
7519 This is only done for -readnow and building partial symtabs. */
7520
7521 static void
7522 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7523 {
7524 htab_up types_htab;
7525
7526 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7527 &per_objfile->per_bfd->abbrev, 0,
7528 types_htab, rcuh_kind::COMPILE);
7529 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7530 read_comp_units_from_section (per_objfile, &section,
7531 &per_objfile->per_bfd->abbrev, 0,
7532 types_htab, rcuh_kind::TYPE);
7533
7534 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7535 if (dwz != NULL)
7536 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7537 types_htab, rcuh_kind::COMPILE);
7538
7539 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7540 }
7541
7542 /* Process all loaded DIEs for compilation unit CU, starting at
7543 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7544 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7545 DW_AT_ranges). See the comments of add_partial_subprogram on how
7546 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7547
7548 static void
7549 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7550 CORE_ADDR *highpc, int set_addrmap,
7551 struct dwarf2_cu *cu)
7552 {
7553 struct partial_die_info *pdi;
7554
7555 /* Now, march along the PDI's, descending into ones which have
7556 interesting children but skipping the children of the other ones,
7557 until we reach the end of the compilation unit. */
7558
7559 pdi = first_die;
7560
7561 while (pdi != NULL)
7562 {
7563 pdi->fixup (cu);
7564
7565 /* Anonymous namespaces or modules have no name but have interesting
7566 children, so we need to look at them. Ditto for anonymous
7567 enums. */
7568
7569 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7570 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7571 || pdi->tag == DW_TAG_imported_unit
7572 || pdi->tag == DW_TAG_inlined_subroutine)
7573 {
7574 switch (pdi->tag)
7575 {
7576 case DW_TAG_subprogram:
7577 case DW_TAG_inlined_subroutine:
7578 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7579 if (cu->language == language_cplus)
7580 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7581 set_addrmap, cu);
7582 break;
7583 case DW_TAG_constant:
7584 case DW_TAG_variable:
7585 case DW_TAG_typedef:
7586 case DW_TAG_union_type:
7587 if (!pdi->is_declaration
7588 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7589 {
7590 add_partial_symbol (pdi, cu);
7591 }
7592 break;
7593 case DW_TAG_class_type:
7594 case DW_TAG_interface_type:
7595 case DW_TAG_structure_type:
7596 if (!pdi->is_declaration)
7597 {
7598 add_partial_symbol (pdi, cu);
7599 }
7600 if ((cu->language == language_rust
7601 || cu->language == language_cplus) && pdi->has_children)
7602 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7603 set_addrmap, cu);
7604 break;
7605 case DW_TAG_enumeration_type:
7606 if (!pdi->is_declaration)
7607 add_partial_enumeration (pdi, cu);
7608 break;
7609 case DW_TAG_base_type:
7610 case DW_TAG_subrange_type:
7611 /* File scope base type definitions are added to the partial
7612 symbol table. */
7613 add_partial_symbol (pdi, cu);
7614 break;
7615 case DW_TAG_namespace:
7616 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7617 break;
7618 case DW_TAG_module:
7619 if (!pdi->is_declaration)
7620 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7621 break;
7622 case DW_TAG_imported_unit:
7623 {
7624 struct dwarf2_per_cu_data *per_cu;
7625
7626 /* For now we don't handle imported units in type units. */
7627 if (cu->per_cu->is_debug_types)
7628 {
7629 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7630 " supported in type units [in module %s]"),
7631 objfile_name (cu->per_objfile->objfile));
7632 }
7633
7634 per_cu = dwarf2_find_containing_comp_unit
7635 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
7636
7637 /* Go read the partial unit, if needed. */
7638 if (per_cu->v.psymtab == NULL)
7639 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7640 cu->language);
7641
7642 cu->per_cu->imported_symtabs_push (per_cu);
7643 }
7644 break;
7645 case DW_TAG_imported_declaration:
7646 add_partial_symbol (pdi, cu);
7647 break;
7648 default:
7649 break;
7650 }
7651 }
7652
7653 /* If the die has a sibling, skip to the sibling. */
7654
7655 pdi = pdi->die_sibling;
7656 }
7657 }
7658
7659 /* Functions used to compute the fully scoped name of a partial DIE.
7660
7661 Normally, this is simple. For C++, the parent DIE's fully scoped
7662 name is concatenated with "::" and the partial DIE's name.
7663 Enumerators are an exception; they use the scope of their parent
7664 enumeration type, i.e. the name of the enumeration type is not
7665 prepended to the enumerator.
7666
7667 There are two complexities. One is DW_AT_specification; in this
7668 case "parent" means the parent of the target of the specification,
7669 instead of the direct parent of the DIE. The other is compilers
7670 which do not emit DW_TAG_namespace; in this case we try to guess
7671 the fully qualified name of structure types from their members'
7672 linkage names. This must be done using the DIE's children rather
7673 than the children of any DW_AT_specification target. We only need
7674 to do this for structures at the top level, i.e. if the target of
7675 any DW_AT_specification (if any; otherwise the DIE itself) does not
7676 have a parent. */
7677
7678 /* Compute the scope prefix associated with PDI's parent, in
7679 compilation unit CU. The result will be allocated on CU's
7680 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7681 field. NULL is returned if no prefix is necessary. */
7682 static const char *
7683 partial_die_parent_scope (struct partial_die_info *pdi,
7684 struct dwarf2_cu *cu)
7685 {
7686 const char *grandparent_scope;
7687 struct partial_die_info *parent, *real_pdi;
7688
7689 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7690 then this means the parent of the specification DIE. */
7691
7692 real_pdi = pdi;
7693 while (real_pdi->has_specification)
7694 {
7695 auto res = find_partial_die (real_pdi->spec_offset,
7696 real_pdi->spec_is_dwz, cu);
7697 real_pdi = res.pdi;
7698 cu = res.cu;
7699 }
7700
7701 parent = real_pdi->die_parent;
7702 if (parent == NULL)
7703 return NULL;
7704
7705 if (parent->scope_set)
7706 return parent->scope;
7707
7708 parent->fixup (cu);
7709
7710 grandparent_scope = partial_die_parent_scope (parent, cu);
7711
7712 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7713 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7714 Work around this problem here. */
7715 if (cu->language == language_cplus
7716 && parent->tag == DW_TAG_namespace
7717 && strcmp (parent->name (cu), "::") == 0
7718 && grandparent_scope == NULL)
7719 {
7720 parent->scope = NULL;
7721 parent->scope_set = 1;
7722 return NULL;
7723 }
7724
7725 /* Nested subroutines in Fortran get a prefix. */
7726 if (pdi->tag == DW_TAG_enumerator)
7727 /* Enumerators should not get the name of the enumeration as a prefix. */
7728 parent->scope = grandparent_scope;
7729 else if (parent->tag == DW_TAG_namespace
7730 || parent->tag == DW_TAG_module
7731 || parent->tag == DW_TAG_structure_type
7732 || parent->tag == DW_TAG_class_type
7733 || parent->tag == DW_TAG_interface_type
7734 || parent->tag == DW_TAG_union_type
7735 || parent->tag == DW_TAG_enumeration_type
7736 || (cu->language == language_fortran
7737 && parent->tag == DW_TAG_subprogram
7738 && pdi->tag == DW_TAG_subprogram))
7739 {
7740 if (grandparent_scope == NULL)
7741 parent->scope = parent->name (cu);
7742 else
7743 parent->scope = typename_concat (&cu->comp_unit_obstack,
7744 grandparent_scope,
7745 parent->name (cu), 0, cu);
7746 }
7747 else
7748 {
7749 /* FIXME drow/2004-04-01: What should we be doing with
7750 function-local names? For partial symbols, we should probably be
7751 ignoring them. */
7752 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7753 dwarf_tag_name (parent->tag),
7754 sect_offset_str (pdi->sect_off));
7755 parent->scope = grandparent_scope;
7756 }
7757
7758 parent->scope_set = 1;
7759 return parent->scope;
7760 }
7761
7762 /* Return the fully scoped name associated with PDI, from compilation unit
7763 CU. The result will be allocated with malloc. */
7764
7765 static gdb::unique_xmalloc_ptr<char>
7766 partial_die_full_name (struct partial_die_info *pdi,
7767 struct dwarf2_cu *cu)
7768 {
7769 const char *parent_scope;
7770
7771 /* If this is a template instantiation, we can not work out the
7772 template arguments from partial DIEs. So, unfortunately, we have
7773 to go through the full DIEs. At least any work we do building
7774 types here will be reused if full symbols are loaded later. */
7775 if (pdi->has_template_arguments)
7776 {
7777 pdi->fixup (cu);
7778
7779 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7780 {
7781 struct die_info *die;
7782 struct attribute attr;
7783 struct dwarf2_cu *ref_cu = cu;
7784
7785 /* DW_FORM_ref_addr is using section offset. */
7786 attr.name = (enum dwarf_attribute) 0;
7787 attr.form = DW_FORM_ref_addr;
7788 attr.u.unsnd = to_underlying (pdi->sect_off);
7789 die = follow_die_ref (NULL, &attr, &ref_cu);
7790
7791 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7792 }
7793 }
7794
7795 parent_scope = partial_die_parent_scope (pdi, cu);
7796 if (parent_scope == NULL)
7797 return NULL;
7798 else
7799 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7800 pdi->name (cu),
7801 0, cu));
7802 }
7803
7804 static void
7805 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7806 {
7807 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7808 struct objfile *objfile = per_objfile->objfile;
7809 struct gdbarch *gdbarch = objfile->arch ();
7810 CORE_ADDR addr = 0;
7811 const char *actual_name = NULL;
7812 CORE_ADDR baseaddr;
7813
7814 baseaddr = objfile->text_section_offset ();
7815
7816 gdb::unique_xmalloc_ptr<char> built_actual_name
7817 = partial_die_full_name (pdi, cu);
7818 if (built_actual_name != NULL)
7819 actual_name = built_actual_name.get ();
7820
7821 if (actual_name == NULL)
7822 actual_name = pdi->name (cu);
7823
7824 partial_symbol psymbol;
7825 memset (&psymbol, 0, sizeof (psymbol));
7826 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
7827 psymbol.ginfo.set_section_index (-1);
7828
7829 /* The code below indicates that the psymbol should be installed by
7830 setting this. */
7831 gdb::optional<psymbol_placement> where;
7832
7833 switch (pdi->tag)
7834 {
7835 case DW_TAG_inlined_subroutine:
7836 case DW_TAG_subprogram:
7837 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7838 - baseaddr);
7839 if (pdi->is_external
7840 || cu->language == language_ada
7841 || (cu->language == language_fortran
7842 && pdi->die_parent != NULL
7843 && pdi->die_parent->tag == DW_TAG_subprogram))
7844 {
7845 /* Normally, only "external" DIEs are part of the global scope.
7846 But in Ada and Fortran, we want to be able to access nested
7847 procedures globally. So all Ada and Fortran subprograms are
7848 stored in the global scope. */
7849 where = psymbol_placement::GLOBAL;
7850 }
7851 else
7852 where = psymbol_placement::STATIC;
7853
7854 psymbol.domain = VAR_DOMAIN;
7855 psymbol.aclass = LOC_BLOCK;
7856 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7857 psymbol.ginfo.value.address = addr;
7858
7859 if (pdi->main_subprogram && actual_name != NULL)
7860 set_objfile_main_name (objfile, actual_name, cu->language);
7861 break;
7862 case DW_TAG_constant:
7863 psymbol.domain = VAR_DOMAIN;
7864 psymbol.aclass = LOC_STATIC;
7865 where = (pdi->is_external
7866 ? psymbol_placement::GLOBAL
7867 : psymbol_placement::STATIC);
7868 break;
7869 case DW_TAG_variable:
7870 if (pdi->d.locdesc)
7871 addr = decode_locdesc (pdi->d.locdesc, cu);
7872
7873 if (pdi->d.locdesc
7874 && addr == 0
7875 && !per_objfile->per_bfd->has_section_at_zero)
7876 {
7877 /* A global or static variable may also have been stripped
7878 out by the linker if unused, in which case its address
7879 will be nullified; do not add such variables into partial
7880 symbol table then. */
7881 }
7882 else if (pdi->is_external)
7883 {
7884 /* Global Variable.
7885 Don't enter into the minimal symbol tables as there is
7886 a minimal symbol table entry from the ELF symbols already.
7887 Enter into partial symbol table if it has a location
7888 descriptor or a type.
7889 If the location descriptor is missing, new_symbol will create
7890 a LOC_UNRESOLVED symbol, the address of the variable will then
7891 be determined from the minimal symbol table whenever the variable
7892 is referenced.
7893 The address for the partial symbol table entry is not
7894 used by GDB, but it comes in handy for debugging partial symbol
7895 table building. */
7896
7897 if (pdi->d.locdesc || pdi->has_type)
7898 {
7899 psymbol.domain = VAR_DOMAIN;
7900 psymbol.aclass = LOC_STATIC;
7901 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7902 psymbol.ginfo.value.address = addr;
7903 where = psymbol_placement::GLOBAL;
7904 }
7905 }
7906 else
7907 {
7908 int has_loc = pdi->d.locdesc != NULL;
7909
7910 /* Static Variable. Skip symbols whose value we cannot know (those
7911 without location descriptors or constant values). */
7912 if (!has_loc && !pdi->has_const_value)
7913 return;
7914
7915 psymbol.domain = VAR_DOMAIN;
7916 psymbol.aclass = LOC_STATIC;
7917 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7918 if (has_loc)
7919 psymbol.ginfo.value.address = addr;
7920 where = psymbol_placement::STATIC;
7921 }
7922 break;
7923 case DW_TAG_array_type:
7924 case DW_TAG_typedef:
7925 case DW_TAG_base_type:
7926 case DW_TAG_subrange_type:
7927 psymbol.domain = VAR_DOMAIN;
7928 psymbol.aclass = LOC_TYPEDEF;
7929 where = psymbol_placement::STATIC;
7930 break;
7931 case DW_TAG_imported_declaration:
7932 case DW_TAG_namespace:
7933 psymbol.domain = VAR_DOMAIN;
7934 psymbol.aclass = LOC_TYPEDEF;
7935 where = psymbol_placement::GLOBAL;
7936 break;
7937 case DW_TAG_module:
7938 /* With Fortran 77 there might be a "BLOCK DATA" module
7939 available without any name. If so, we skip the module as it
7940 doesn't bring any value. */
7941 if (actual_name != nullptr)
7942 {
7943 psymbol.domain = MODULE_DOMAIN;
7944 psymbol.aclass = LOC_TYPEDEF;
7945 where = psymbol_placement::GLOBAL;
7946 }
7947 break;
7948 case DW_TAG_class_type:
7949 case DW_TAG_interface_type:
7950 case DW_TAG_structure_type:
7951 case DW_TAG_union_type:
7952 case DW_TAG_enumeration_type:
7953 /* Skip external references. The DWARF standard says in the section
7954 about "Structure, Union, and Class Type Entries": "An incomplete
7955 structure, union or class type is represented by a structure,
7956 union or class entry that does not have a byte size attribute
7957 and that has a DW_AT_declaration attribute." */
7958 if (!pdi->has_byte_size && pdi->is_declaration)
7959 return;
7960
7961 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7962 static vs. global. */
7963 psymbol.domain = STRUCT_DOMAIN;
7964 psymbol.aclass = LOC_TYPEDEF;
7965 where = (cu->language == language_cplus
7966 ? psymbol_placement::GLOBAL
7967 : psymbol_placement::STATIC);
7968 break;
7969 case DW_TAG_enumerator:
7970 psymbol.domain = VAR_DOMAIN;
7971 psymbol.aclass = LOC_CONST;
7972 where = (cu->language == language_cplus
7973 ? psymbol_placement::GLOBAL
7974 : psymbol_placement::STATIC);
7975 break;
7976 default:
7977 break;
7978 }
7979
7980 if (where.has_value ())
7981 {
7982 if (built_actual_name != nullptr)
7983 actual_name = objfile->intern (actual_name);
7984 if (pdi->linkage_name == nullptr || cu->language == language_ada)
7985 psymbol.ginfo.set_linkage_name (actual_name);
7986 else
7987 {
7988 psymbol.ginfo.set_demangled_name (actual_name,
7989 &objfile->objfile_obstack);
7990 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
7991 }
7992 cu->per_cu->v.psymtab->add_psymbol
7993 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
7994 objfile);
7995 }
7996 }
7997
7998 /* Read a partial die corresponding to a namespace; also, add a symbol
7999 corresponding to that namespace to the symbol table. NAMESPACE is
8000 the name of the enclosing namespace. */
8001
8002 static void
8003 add_partial_namespace (struct partial_die_info *pdi,
8004 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8005 int set_addrmap, struct dwarf2_cu *cu)
8006 {
8007 /* Add a symbol for the namespace. */
8008
8009 add_partial_symbol (pdi, cu);
8010
8011 /* Now scan partial symbols in that namespace. */
8012
8013 if (pdi->has_children)
8014 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8015 }
8016
8017 /* Read a partial die corresponding to a Fortran module. */
8018
8019 static void
8020 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8021 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8022 {
8023 /* Add a symbol for the namespace. */
8024
8025 add_partial_symbol (pdi, cu);
8026
8027 /* Now scan partial symbols in that module. */
8028
8029 if (pdi->has_children)
8030 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8031 }
8032
8033 /* Read a partial die corresponding to a subprogram or an inlined
8034 subprogram and create a partial symbol for that subprogram.
8035 When the CU language allows it, this routine also defines a partial
8036 symbol for each nested subprogram that this subprogram contains.
8037 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8038 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8039
8040 PDI may also be a lexical block, in which case we simply search
8041 recursively for subprograms defined inside that lexical block.
8042 Again, this is only performed when the CU language allows this
8043 type of definitions. */
8044
8045 static void
8046 add_partial_subprogram (struct partial_die_info *pdi,
8047 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8048 int set_addrmap, struct dwarf2_cu *cu)
8049 {
8050 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8051 {
8052 if (pdi->has_pc_info)
8053 {
8054 if (pdi->lowpc < *lowpc)
8055 *lowpc = pdi->lowpc;
8056 if (pdi->highpc > *highpc)
8057 *highpc = pdi->highpc;
8058 if (set_addrmap)
8059 {
8060 struct objfile *objfile = cu->per_objfile->objfile;
8061 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8062 struct gdbarch *gdbarch = objfile->arch ();
8063 CORE_ADDR baseaddr;
8064 CORE_ADDR this_highpc;
8065 CORE_ADDR this_lowpc;
8066
8067 baseaddr = objfile->text_section_offset ();
8068 this_lowpc
8069 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8070 pdi->lowpc + baseaddr)
8071 - baseaddr);
8072 this_highpc
8073 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8074 pdi->highpc + baseaddr)
8075 - baseaddr);
8076 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8077 this_lowpc, this_highpc - 1,
8078 cu->per_cu->v.psymtab);
8079 }
8080 }
8081
8082 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8083 {
8084 if (!pdi->is_declaration)
8085 /* Ignore subprogram DIEs that do not have a name, they are
8086 illegal. Do not emit a complaint at this point, we will
8087 do so when we convert this psymtab into a symtab. */
8088 if (pdi->name (cu))
8089 add_partial_symbol (pdi, cu);
8090 }
8091 }
8092
8093 if (! pdi->has_children)
8094 return;
8095
8096 if (cu->language == language_ada || cu->language == language_fortran)
8097 {
8098 pdi = pdi->die_child;
8099 while (pdi != NULL)
8100 {
8101 pdi->fixup (cu);
8102 if (pdi->tag == DW_TAG_subprogram
8103 || pdi->tag == DW_TAG_inlined_subroutine
8104 || pdi->tag == DW_TAG_lexical_block)
8105 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8106 pdi = pdi->die_sibling;
8107 }
8108 }
8109 }
8110
8111 /* Read a partial die corresponding to an enumeration type. */
8112
8113 static void
8114 add_partial_enumeration (struct partial_die_info *enum_pdi,
8115 struct dwarf2_cu *cu)
8116 {
8117 struct partial_die_info *pdi;
8118
8119 if (enum_pdi->name (cu) != NULL)
8120 add_partial_symbol (enum_pdi, cu);
8121
8122 pdi = enum_pdi->die_child;
8123 while (pdi)
8124 {
8125 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8126 complaint (_("malformed enumerator DIE ignored"));
8127 else
8128 add_partial_symbol (pdi, cu);
8129 pdi = pdi->die_sibling;
8130 }
8131 }
8132
8133 /* Return the initial uleb128 in the die at INFO_PTR. */
8134
8135 static unsigned int
8136 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8137 {
8138 unsigned int bytes_read;
8139
8140 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8141 }
8142
8143 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8144 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8145
8146 Return the corresponding abbrev, or NULL if the number is zero (indicating
8147 an empty DIE). In either case *BYTES_READ will be set to the length of
8148 the initial number. */
8149
8150 static const struct abbrev_info *
8151 peek_die_abbrev (const die_reader_specs &reader,
8152 const gdb_byte *info_ptr, unsigned int *bytes_read)
8153 {
8154 dwarf2_cu *cu = reader.cu;
8155 bfd *abfd = reader.abfd;
8156 unsigned int abbrev_number
8157 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8158
8159 if (abbrev_number == 0)
8160 return NULL;
8161
8162 const abbrev_info *abbrev
8163 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8164 if (!abbrev)
8165 {
8166 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8167 " at offset %s [in module %s]"),
8168 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8169 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8170 }
8171
8172 return abbrev;
8173 }
8174
8175 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8176 Returns a pointer to the end of a series of DIEs, terminated by an empty
8177 DIE. Any children of the skipped DIEs will also be skipped. */
8178
8179 static const gdb_byte *
8180 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8181 {
8182 while (1)
8183 {
8184 unsigned int bytes_read;
8185 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8186 &bytes_read);
8187
8188 if (abbrev == NULL)
8189 return info_ptr + bytes_read;
8190 else
8191 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8192 }
8193 }
8194
8195 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8196 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8197 abbrev corresponding to that skipped uleb128 should be passed in
8198 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8199 children. */
8200
8201 static const gdb_byte *
8202 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8203 const struct abbrev_info *abbrev)
8204 {
8205 unsigned int bytes_read;
8206 struct attribute attr;
8207 bfd *abfd = reader->abfd;
8208 struct dwarf2_cu *cu = reader->cu;
8209 const gdb_byte *buffer = reader->buffer;
8210 const gdb_byte *buffer_end = reader->buffer_end;
8211 unsigned int form, i;
8212
8213 for (i = 0; i < abbrev->num_attrs; i++)
8214 {
8215 /* The only abbrev we care about is DW_AT_sibling. */
8216 if (abbrev->attrs[i].name == DW_AT_sibling)
8217 {
8218 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8219 if (attr.form == DW_FORM_ref_addr)
8220 complaint (_("ignoring absolute DW_AT_sibling"));
8221 else
8222 {
8223 sect_offset off = attr.get_ref_die_offset ();
8224 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8225
8226 if (sibling_ptr < info_ptr)
8227 complaint (_("DW_AT_sibling points backwards"));
8228 else if (sibling_ptr > reader->buffer_end)
8229 reader->die_section->overflow_complaint ();
8230 else
8231 return sibling_ptr;
8232 }
8233 }
8234
8235 /* If it isn't DW_AT_sibling, skip this attribute. */
8236 form = abbrev->attrs[i].form;
8237 skip_attribute:
8238 switch (form)
8239 {
8240 case DW_FORM_ref_addr:
8241 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8242 and later it is offset sized. */
8243 if (cu->header.version == 2)
8244 info_ptr += cu->header.addr_size;
8245 else
8246 info_ptr += cu->header.offset_size;
8247 break;
8248 case DW_FORM_GNU_ref_alt:
8249 info_ptr += cu->header.offset_size;
8250 break;
8251 case DW_FORM_addr:
8252 info_ptr += cu->header.addr_size;
8253 break;
8254 case DW_FORM_data1:
8255 case DW_FORM_ref1:
8256 case DW_FORM_flag:
8257 case DW_FORM_strx1:
8258 info_ptr += 1;
8259 break;
8260 case DW_FORM_flag_present:
8261 case DW_FORM_implicit_const:
8262 break;
8263 case DW_FORM_data2:
8264 case DW_FORM_ref2:
8265 case DW_FORM_strx2:
8266 info_ptr += 2;
8267 break;
8268 case DW_FORM_strx3:
8269 info_ptr += 3;
8270 break;
8271 case DW_FORM_data4:
8272 case DW_FORM_ref4:
8273 case DW_FORM_strx4:
8274 info_ptr += 4;
8275 break;
8276 case DW_FORM_data8:
8277 case DW_FORM_ref8:
8278 case DW_FORM_ref_sig8:
8279 info_ptr += 8;
8280 break;
8281 case DW_FORM_data16:
8282 info_ptr += 16;
8283 break;
8284 case DW_FORM_string:
8285 read_direct_string (abfd, info_ptr, &bytes_read);
8286 info_ptr += bytes_read;
8287 break;
8288 case DW_FORM_sec_offset:
8289 case DW_FORM_strp:
8290 case DW_FORM_GNU_strp_alt:
8291 info_ptr += cu->header.offset_size;
8292 break;
8293 case DW_FORM_exprloc:
8294 case DW_FORM_block:
8295 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8296 info_ptr += bytes_read;
8297 break;
8298 case DW_FORM_block1:
8299 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8300 break;
8301 case DW_FORM_block2:
8302 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8303 break;
8304 case DW_FORM_block4:
8305 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8306 break;
8307 case DW_FORM_addrx:
8308 case DW_FORM_strx:
8309 case DW_FORM_sdata:
8310 case DW_FORM_udata:
8311 case DW_FORM_ref_udata:
8312 case DW_FORM_GNU_addr_index:
8313 case DW_FORM_GNU_str_index:
8314 case DW_FORM_rnglistx:
8315 case DW_FORM_loclistx:
8316 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8317 break;
8318 case DW_FORM_indirect:
8319 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8320 info_ptr += bytes_read;
8321 /* We need to continue parsing from here, so just go back to
8322 the top. */
8323 goto skip_attribute;
8324
8325 default:
8326 error (_("Dwarf Error: Cannot handle %s "
8327 "in DWARF reader [in module %s]"),
8328 dwarf_form_name (form),
8329 bfd_get_filename (abfd));
8330 }
8331 }
8332
8333 if (abbrev->has_children)
8334 return skip_children (reader, info_ptr);
8335 else
8336 return info_ptr;
8337 }
8338
8339 /* Locate ORIG_PDI's sibling.
8340 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8341
8342 static const gdb_byte *
8343 locate_pdi_sibling (const struct die_reader_specs *reader,
8344 struct partial_die_info *orig_pdi,
8345 const gdb_byte *info_ptr)
8346 {
8347 /* Do we know the sibling already? */
8348
8349 if (orig_pdi->sibling)
8350 return orig_pdi->sibling;
8351
8352 /* Are there any children to deal with? */
8353
8354 if (!orig_pdi->has_children)
8355 return info_ptr;
8356
8357 /* Skip the children the long way. */
8358
8359 return skip_children (reader, info_ptr);
8360 }
8361
8362 /* Expand this partial symbol table into a full symbol table. SELF is
8363 not NULL. */
8364
8365 void
8366 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8367 {
8368 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8369
8370 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8371
8372 /* If this psymtab is constructed from a debug-only objfile, the
8373 has_section_at_zero flag will not necessarily be correct. We
8374 can get the correct value for this flag by looking at the data
8375 associated with the (presumably stripped) associated objfile. */
8376 if (objfile->separate_debug_objfile_backlink)
8377 {
8378 dwarf2_per_objfile *per_objfile_backlink
8379 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8380
8381 per_objfile->per_bfd->has_section_at_zero
8382 = per_objfile_backlink->per_bfd->has_section_at_zero;
8383 }
8384
8385 expand_psymtab (objfile);
8386
8387 process_cu_includes (per_objfile);
8388 }
8389 \f
8390 /* Reading in full CUs. */
8391
8392 /* Add PER_CU to the queue. */
8393
8394 static void
8395 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8396 dwarf2_per_objfile *per_objfile,
8397 enum language pretend_language)
8398 {
8399 per_cu->queued = 1;
8400
8401 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8402 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8403 }
8404
8405 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8406
8407 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8408 dependency.
8409
8410 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8411 DIEs, false otherwise.
8412
8413 Explanation: there is an invariant that if a CU is queued for expansion
8414 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8415 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8416 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8417 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8418 invariant is respected.
8419
8420 The caller is therefore not required to load the CU's DIEs (we return false)
8421 if:
8422
8423 - the CU is already expanded, and therefore does not get enqueued
8424 - the CU gets enqueued for expansion, but its DIEs are already loaded
8425
8426 Note that the caller should not use this function's return value as an
8427 indicator of whether the CU's DIEs are loaded right now, it should check
8428 that by calling `dwarf2_per_objfile::get_cu` instead. */
8429
8430 static int
8431 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8432 dwarf2_per_cu_data *per_cu,
8433 dwarf2_per_objfile *per_objfile,
8434 enum language pretend_language)
8435 {
8436 /* We may arrive here during partial symbol reading, if we need full
8437 DIEs to process an unusual case (e.g. template arguments). Do
8438 not queue PER_CU, just tell our caller to load its DIEs. */
8439 if (per_cu->per_bfd->reading_partial_symbols)
8440 {
8441 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8442
8443 if (cu == NULL || cu->dies == NULL)
8444 return 1;
8445 return 0;
8446 }
8447
8448 /* Mark the dependence relation so that we don't flush PER_CU
8449 too early. */
8450 if (dependent_cu != NULL)
8451 dependent_cu->add_dependence (per_cu);
8452
8453 /* If it's already on the queue, we have nothing to do. */
8454 if (per_cu->queued)
8455 {
8456 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8457 loaded. */
8458 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8459
8460 /* If the CU is queued for expansion, it should not already be
8461 expanded. */
8462 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8463
8464 /* The DIEs are already loaded, the caller doesn't need to do it. */
8465 return 0;
8466 }
8467
8468 bool queued = false;
8469 if (!per_objfile->symtab_set_p (per_cu))
8470 {
8471 /* Add it to the queue. */
8472 queue_comp_unit (per_cu, per_objfile, pretend_language);
8473 queued = true;
8474 }
8475
8476 /* If the compilation unit is already loaded, just mark it as
8477 used. */
8478 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8479 if (cu != nullptr)
8480 cu->last_used = 0;
8481
8482 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8483 and the DIEs are not already loaded. */
8484 return queued && cu == nullptr;
8485 }
8486
8487 /* Process the queue. */
8488
8489 static void
8490 process_queue (dwarf2_per_objfile *per_objfile)
8491 {
8492 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8493 objfile_name (per_objfile->objfile));
8494
8495 /* The queue starts out with one item, but following a DIE reference
8496 may load a new CU, adding it to the end of the queue. */
8497 while (!per_objfile->per_bfd->queue->empty ())
8498 {
8499 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8500 dwarf2_per_cu_data *per_cu = item.per_cu;
8501
8502 if (!per_objfile->symtab_set_p (per_cu))
8503 {
8504 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8505
8506 /* Skip dummy CUs. */
8507 if (cu != nullptr)
8508 {
8509 unsigned int debug_print_threshold;
8510 char buf[100];
8511
8512 if (per_cu->is_debug_types)
8513 {
8514 struct signatured_type *sig_type =
8515 (struct signatured_type *) per_cu;
8516
8517 sprintf (buf, "TU %s at offset %s",
8518 hex_string (sig_type->signature),
8519 sect_offset_str (per_cu->sect_off));
8520 /* There can be 100s of TUs.
8521 Only print them in verbose mode. */
8522 debug_print_threshold = 2;
8523 }
8524 else
8525 {
8526 sprintf (buf, "CU at offset %s",
8527 sect_offset_str (per_cu->sect_off));
8528 debug_print_threshold = 1;
8529 }
8530
8531 if (dwarf_read_debug >= debug_print_threshold)
8532 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8533
8534 if (per_cu->is_debug_types)
8535 process_full_type_unit (cu, item.pretend_language);
8536 else
8537 process_full_comp_unit (cu, item.pretend_language);
8538
8539 if (dwarf_read_debug >= debug_print_threshold)
8540 dwarf_read_debug_printf ("Done expanding %s", buf);
8541 }
8542 }
8543
8544 per_cu->queued = 0;
8545 per_objfile->per_bfd->queue->pop ();
8546 }
8547
8548 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8549 objfile_name (per_objfile->objfile));
8550 }
8551
8552 /* Read in full symbols for PST, and anything it depends on. */
8553
8554 void
8555 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8556 {
8557 gdb_assert (!readin_p (objfile));
8558
8559 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8560 free_cached_comp_units freer (per_objfile);
8561 expand_dependencies (objfile);
8562
8563 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8564 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8565 }
8566
8567 /* See psympriv.h. */
8568
8569 bool
8570 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8571 {
8572 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8573 return per_objfile->symtab_set_p (per_cu_data);
8574 }
8575
8576 /* See psympriv.h. */
8577
8578 compunit_symtab *
8579 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8580 {
8581 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8582 return per_objfile->get_symtab (per_cu_data);
8583 }
8584
8585 /* Trivial hash function for die_info: the hash value of a DIE
8586 is its offset in .debug_info for this objfile. */
8587
8588 static hashval_t
8589 die_hash (const void *item)
8590 {
8591 const struct die_info *die = (const struct die_info *) item;
8592
8593 return to_underlying (die->sect_off);
8594 }
8595
8596 /* Trivial comparison function for die_info structures: two DIEs
8597 are equal if they have the same offset. */
8598
8599 static int
8600 die_eq (const void *item_lhs, const void *item_rhs)
8601 {
8602 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8603 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8604
8605 return die_lhs->sect_off == die_rhs->sect_off;
8606 }
8607
8608 /* Load the DIEs associated with PER_CU into memory.
8609
8610 In some cases, the caller, while reading partial symbols, will need to load
8611 the full symbols for the CU for some reason. It will already have a
8612 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8613 rather than creating a new one. */
8614
8615 static void
8616 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8617 dwarf2_per_objfile *per_objfile,
8618 dwarf2_cu *existing_cu,
8619 bool skip_partial,
8620 enum language pretend_language)
8621 {
8622 gdb_assert (! this_cu->is_debug_types);
8623
8624 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8625 if (reader.dummy_p)
8626 return;
8627
8628 struct dwarf2_cu *cu = reader.cu;
8629 const gdb_byte *info_ptr = reader.info_ptr;
8630
8631 gdb_assert (cu->die_hash == NULL);
8632 cu->die_hash =
8633 htab_create_alloc_ex (cu->header.length / 12,
8634 die_hash,
8635 die_eq,
8636 NULL,
8637 &cu->comp_unit_obstack,
8638 hashtab_obstack_allocate,
8639 dummy_obstack_deallocate);
8640
8641 if (reader.comp_unit_die->has_children)
8642 reader.comp_unit_die->child
8643 = read_die_and_siblings (&reader, reader.info_ptr,
8644 &info_ptr, reader.comp_unit_die);
8645 cu->dies = reader.comp_unit_die;
8646 /* comp_unit_die is not stored in die_hash, no need. */
8647
8648 /* We try not to read any attributes in this function, because not
8649 all CUs needed for references have been loaded yet, and symbol
8650 table processing isn't initialized. But we have to set the CU language,
8651 or we won't be able to build types correctly.
8652 Similarly, if we do not read the producer, we can not apply
8653 producer-specific interpretation. */
8654 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8655
8656 reader.keep ();
8657 }
8658
8659 /* Add a DIE to the delayed physname list. */
8660
8661 static void
8662 add_to_method_list (struct type *type, int fnfield_index, int index,
8663 const char *name, struct die_info *die,
8664 struct dwarf2_cu *cu)
8665 {
8666 struct delayed_method_info mi;
8667 mi.type = type;
8668 mi.fnfield_index = fnfield_index;
8669 mi.index = index;
8670 mi.name = name;
8671 mi.die = die;
8672 cu->method_list.push_back (mi);
8673 }
8674
8675 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8676 "const" / "volatile". If so, decrements LEN by the length of the
8677 modifier and return true. Otherwise return false. */
8678
8679 template<size_t N>
8680 static bool
8681 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8682 {
8683 size_t mod_len = sizeof (mod) - 1;
8684 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8685 {
8686 len -= mod_len;
8687 return true;
8688 }
8689 return false;
8690 }
8691
8692 /* Compute the physnames of any methods on the CU's method list.
8693
8694 The computation of method physnames is delayed in order to avoid the
8695 (bad) condition that one of the method's formal parameters is of an as yet
8696 incomplete type. */
8697
8698 static void
8699 compute_delayed_physnames (struct dwarf2_cu *cu)
8700 {
8701 /* Only C++ delays computing physnames. */
8702 if (cu->method_list.empty ())
8703 return;
8704 gdb_assert (cu->language == language_cplus);
8705
8706 for (const delayed_method_info &mi : cu->method_list)
8707 {
8708 const char *physname;
8709 struct fn_fieldlist *fn_flp
8710 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8711 physname = dwarf2_physname (mi.name, mi.die, cu);
8712 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8713 = physname ? physname : "";
8714
8715 /* Since there's no tag to indicate whether a method is a
8716 const/volatile overload, extract that information out of the
8717 demangled name. */
8718 if (physname != NULL)
8719 {
8720 size_t len = strlen (physname);
8721
8722 while (1)
8723 {
8724 if (physname[len] == ')') /* shortcut */
8725 break;
8726 else if (check_modifier (physname, len, " const"))
8727 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8728 else if (check_modifier (physname, len, " volatile"))
8729 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8730 else
8731 break;
8732 }
8733 }
8734 }
8735
8736 /* The list is no longer needed. */
8737 cu->method_list.clear ();
8738 }
8739
8740 /* Go objects should be embedded in a DW_TAG_module DIE,
8741 and it's not clear if/how imported objects will appear.
8742 To keep Go support simple until that's worked out,
8743 go back through what we've read and create something usable.
8744 We could do this while processing each DIE, and feels kinda cleaner,
8745 but that way is more invasive.
8746 This is to, for example, allow the user to type "p var" or "b main"
8747 without having to specify the package name, and allow lookups
8748 of module.object to work in contexts that use the expression
8749 parser. */
8750
8751 static void
8752 fixup_go_packaging (struct dwarf2_cu *cu)
8753 {
8754 gdb::unique_xmalloc_ptr<char> package_name;
8755 struct pending *list;
8756 int i;
8757
8758 for (list = *cu->get_builder ()->get_global_symbols ();
8759 list != NULL;
8760 list = list->next)
8761 {
8762 for (i = 0; i < list->nsyms; ++i)
8763 {
8764 struct symbol *sym = list->symbol[i];
8765
8766 if (sym->language () == language_go
8767 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8768 {
8769 gdb::unique_xmalloc_ptr<char> this_package_name
8770 (go_symbol_package_name (sym));
8771
8772 if (this_package_name == NULL)
8773 continue;
8774 if (package_name == NULL)
8775 package_name = std::move (this_package_name);
8776 else
8777 {
8778 struct objfile *objfile = cu->per_objfile->objfile;
8779 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8780 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8781 (symbol_symtab (sym) != NULL
8782 ? symtab_to_filename_for_display
8783 (symbol_symtab (sym))
8784 : objfile_name (objfile)),
8785 this_package_name.get (), package_name.get ());
8786 }
8787 }
8788 }
8789 }
8790
8791 if (package_name != NULL)
8792 {
8793 struct objfile *objfile = cu->per_objfile->objfile;
8794 const char *saved_package_name = objfile->intern (package_name.get ());
8795 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8796 saved_package_name);
8797 struct symbol *sym;
8798
8799 sym = new (&objfile->objfile_obstack) symbol;
8800 sym->set_language (language_go, &objfile->objfile_obstack);
8801 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8802 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8803 e.g., "main" finds the "main" module and not C's main(). */
8804 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8805 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8806 SYMBOL_TYPE (sym) = type;
8807
8808 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8809 }
8810 }
8811
8812 /* Allocate a fully-qualified name consisting of the two parts on the
8813 obstack. */
8814
8815 static const char *
8816 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8817 {
8818 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8819 }
8820
8821 /* A helper that allocates a variant part to attach to a Rust enum
8822 type. OBSTACK is where the results should be allocated. TYPE is
8823 the type we're processing. DISCRIMINANT_INDEX is the index of the
8824 discriminant. It must be the index of one of the fields of TYPE,
8825 or -1 to mean there is no discriminant (univariant enum).
8826 DEFAULT_INDEX is the index of the default field; or -1 if there is
8827 no default. RANGES is indexed by "effective" field number (the
8828 field index, but omitting the discriminant and default fields) and
8829 must hold the discriminant values used by the variants. Note that
8830 RANGES must have a lifetime at least as long as OBSTACK -- either
8831 already allocated on it, or static. */
8832
8833 static void
8834 alloc_rust_variant (struct obstack *obstack, struct type *type,
8835 int discriminant_index, int default_index,
8836 gdb::array_view<discriminant_range> ranges)
8837 {
8838 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8839 gdb_assert (discriminant_index == -1
8840 || (discriminant_index >= 0
8841 && discriminant_index < type->num_fields ()));
8842 gdb_assert (default_index == -1
8843 || (default_index >= 0 && default_index < type->num_fields ()));
8844
8845 /* We have one variant for each non-discriminant field. */
8846 int n_variants = type->num_fields ();
8847 if (discriminant_index != -1)
8848 --n_variants;
8849
8850 variant *variants = new (obstack) variant[n_variants];
8851 int var_idx = 0;
8852 int range_idx = 0;
8853 for (int i = 0; i < type->num_fields (); ++i)
8854 {
8855 if (i == discriminant_index)
8856 continue;
8857
8858 variants[var_idx].first_field = i;
8859 variants[var_idx].last_field = i + 1;
8860
8861 /* The default field does not need a range, but other fields do.
8862 We skipped the discriminant above. */
8863 if (i != default_index)
8864 {
8865 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8866 ++range_idx;
8867 }
8868
8869 ++var_idx;
8870 }
8871
8872 gdb_assert (range_idx == ranges.size ());
8873 gdb_assert (var_idx == n_variants);
8874
8875 variant_part *part = new (obstack) variant_part;
8876 part->discriminant_index = discriminant_index;
8877 /* If there is no discriminant, then whether it is signed is of no
8878 consequence. */
8879 part->is_unsigned
8880 = (discriminant_index == -1
8881 ? false
8882 : type->field (discriminant_index).type ()->is_unsigned ());
8883 part->variants = gdb::array_view<variant> (variants, n_variants);
8884
8885 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
8886 gdb::array_view<variant_part> *prop_value
8887 = new (storage) gdb::array_view<variant_part> (part, 1);
8888
8889 struct dynamic_prop prop;
8890 prop.set_variant_parts (prop_value);
8891
8892 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
8893 }
8894
8895 /* Some versions of rustc emitted enums in an unusual way.
8896
8897 Ordinary enums were emitted as unions. The first element of each
8898 structure in the union was named "RUST$ENUM$DISR". This element
8899 held the discriminant.
8900
8901 These versions of Rust also implemented the "non-zero"
8902 optimization. When the enum had two values, and one is empty and
8903 the other holds a pointer that cannot be zero, the pointer is used
8904 as the discriminant, with a zero value meaning the empty variant.
8905 Here, the union's first member is of the form
8906 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8907 where the fieldnos are the indices of the fields that should be
8908 traversed in order to find the field (which may be several fields deep)
8909 and the variantname is the name of the variant of the case when the
8910 field is zero.
8911
8912 This function recognizes whether TYPE is of one of these forms,
8913 and, if so, smashes it to be a variant type. */
8914
8915 static void
8916 quirk_rust_enum (struct type *type, struct objfile *objfile)
8917 {
8918 gdb_assert (type->code () == TYPE_CODE_UNION);
8919
8920 /* We don't need to deal with empty enums. */
8921 if (type->num_fields () == 0)
8922 return;
8923
8924 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
8925 if (type->num_fields () == 1
8926 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
8927 {
8928 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
8929
8930 /* Decode the field name to find the offset of the
8931 discriminant. */
8932 ULONGEST bit_offset = 0;
8933 struct type *field_type = type->field (0).type ();
8934 while (name[0] >= '0' && name[0] <= '9')
8935 {
8936 char *tail;
8937 unsigned long index = strtoul (name, &tail, 10);
8938 name = tail;
8939 if (*name != '$'
8940 || index >= field_type->num_fields ()
8941 || (TYPE_FIELD_LOC_KIND (field_type, index)
8942 != FIELD_LOC_KIND_BITPOS))
8943 {
8944 complaint (_("Could not parse Rust enum encoding string \"%s\""
8945 "[in module %s]"),
8946 TYPE_FIELD_NAME (type, 0),
8947 objfile_name (objfile));
8948 return;
8949 }
8950 ++name;
8951
8952 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
8953 field_type = field_type->field (index).type ();
8954 }
8955
8956 /* Smash this type to be a structure type. We have to do this
8957 because the type has already been recorded. */
8958 type->set_code (TYPE_CODE_STRUCT);
8959 type->set_num_fields (3);
8960 /* Save the field we care about. */
8961 struct field saved_field = type->field (0);
8962 type->set_fields
8963 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
8964
8965 /* Put the discriminant at index 0. */
8966 type->field (0).set_type (field_type);
8967 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8968 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
8969 SET_FIELD_BITPOS (type->field (0), bit_offset);
8970
8971 /* The order of fields doesn't really matter, so put the real
8972 field at index 1 and the data-less field at index 2. */
8973 type->field (1) = saved_field;
8974 TYPE_FIELD_NAME (type, 1)
8975 = rust_last_path_segment (type->field (1).type ()->name ());
8976 type->field (1).type ()->set_name
8977 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8978 TYPE_FIELD_NAME (type, 1)));
8979
8980 const char *dataless_name
8981 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8982 name);
8983 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8984 dataless_name);
8985 type->field (2).set_type (dataless_type);
8986 /* NAME points into the original discriminant name, which
8987 already has the correct lifetime. */
8988 TYPE_FIELD_NAME (type, 2) = name;
8989 SET_FIELD_BITPOS (type->field (2), 0);
8990
8991 /* Indicate that this is a variant type. */
8992 static discriminant_range ranges[1] = { { 0, 0 } };
8993 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8994 }
8995 /* A union with a single anonymous field is probably an old-style
8996 univariant enum. */
8997 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
8998 {
8999 /* Smash this type to be a structure type. We have to do this
9000 because the type has already been recorded. */
9001 type->set_code (TYPE_CODE_STRUCT);
9002
9003 struct type *field_type = type->field (0).type ();
9004 const char *variant_name
9005 = rust_last_path_segment (field_type->name ());
9006 TYPE_FIELD_NAME (type, 0) = variant_name;
9007 field_type->set_name
9008 (rust_fully_qualify (&objfile->objfile_obstack,
9009 type->name (), variant_name));
9010
9011 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9012 }
9013 else
9014 {
9015 struct type *disr_type = nullptr;
9016 for (int i = 0; i < type->num_fields (); ++i)
9017 {
9018 disr_type = type->field (i).type ();
9019
9020 if (disr_type->code () != TYPE_CODE_STRUCT)
9021 {
9022 /* All fields of a true enum will be structs. */
9023 return;
9024 }
9025 else if (disr_type->num_fields () == 0)
9026 {
9027 /* Could be data-less variant, so keep going. */
9028 disr_type = nullptr;
9029 }
9030 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9031 "RUST$ENUM$DISR") != 0)
9032 {
9033 /* Not a Rust enum. */
9034 return;
9035 }
9036 else
9037 {
9038 /* Found one. */
9039 break;
9040 }
9041 }
9042
9043 /* If we got here without a discriminant, then it's probably
9044 just a union. */
9045 if (disr_type == nullptr)
9046 return;
9047
9048 /* Smash this type to be a structure type. We have to do this
9049 because the type has already been recorded. */
9050 type->set_code (TYPE_CODE_STRUCT);
9051
9052 /* Make space for the discriminant field. */
9053 struct field *disr_field = &disr_type->field (0);
9054 field *new_fields
9055 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9056 * sizeof (struct field)));
9057 memcpy (new_fields + 1, type->fields (),
9058 type->num_fields () * sizeof (struct field));
9059 type->set_fields (new_fields);
9060 type->set_num_fields (type->num_fields () + 1);
9061
9062 /* Install the discriminant at index 0 in the union. */
9063 type->field (0) = *disr_field;
9064 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9065 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9066
9067 /* We need a way to find the correct discriminant given a
9068 variant name. For convenience we build a map here. */
9069 struct type *enum_type = disr_field->type ();
9070 std::unordered_map<std::string, ULONGEST> discriminant_map;
9071 for (int i = 0; i < enum_type->num_fields (); ++i)
9072 {
9073 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9074 {
9075 const char *name
9076 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9077 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9078 }
9079 }
9080
9081 int n_fields = type->num_fields ();
9082 /* We don't need a range entry for the discriminant, but we do
9083 need one for every other field, as there is no default
9084 variant. */
9085 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9086 discriminant_range,
9087 n_fields - 1);
9088 /* Skip the discriminant here. */
9089 for (int i = 1; i < n_fields; ++i)
9090 {
9091 /* Find the final word in the name of this variant's type.
9092 That name can be used to look up the correct
9093 discriminant. */
9094 const char *variant_name
9095 = rust_last_path_segment (type->field (i).type ()->name ());
9096
9097 auto iter = discriminant_map.find (variant_name);
9098 if (iter != discriminant_map.end ())
9099 {
9100 ranges[i - 1].low = iter->second;
9101 ranges[i - 1].high = iter->second;
9102 }
9103
9104 /* In Rust, each element should have the size of the
9105 enclosing enum. */
9106 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9107
9108 /* Remove the discriminant field, if it exists. */
9109 struct type *sub_type = type->field (i).type ();
9110 if (sub_type->num_fields () > 0)
9111 {
9112 sub_type->set_num_fields (sub_type->num_fields () - 1);
9113 sub_type->set_fields (sub_type->fields () + 1);
9114 }
9115 TYPE_FIELD_NAME (type, i) = variant_name;
9116 sub_type->set_name
9117 (rust_fully_qualify (&objfile->objfile_obstack,
9118 type->name (), variant_name));
9119 }
9120
9121 /* Indicate that this is a variant type. */
9122 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9123 gdb::array_view<discriminant_range> (ranges,
9124 n_fields - 1));
9125 }
9126 }
9127
9128 /* Rewrite some Rust unions to be structures with variants parts. */
9129
9130 static void
9131 rust_union_quirks (struct dwarf2_cu *cu)
9132 {
9133 gdb_assert (cu->language == language_rust);
9134 for (type *type_ : cu->rust_unions)
9135 quirk_rust_enum (type_, cu->per_objfile->objfile);
9136 /* We don't need this any more. */
9137 cu->rust_unions.clear ();
9138 }
9139
9140 /* See read.h. */
9141
9142 type_unit_group_unshareable *
9143 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9144 {
9145 auto iter = this->m_type_units.find (tu_group);
9146 if (iter != this->m_type_units.end ())
9147 return iter->second.get ();
9148
9149 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9150 type_unit_group_unshareable *result = uniq.get ();
9151 this->m_type_units[tu_group] = std::move (uniq);
9152 return result;
9153 }
9154
9155 struct type *
9156 dwarf2_per_objfile::get_type_for_signatured_type
9157 (signatured_type *sig_type) const
9158 {
9159 auto iter = this->m_type_map.find (sig_type);
9160 if (iter == this->m_type_map.end ())
9161 return nullptr;
9162
9163 return iter->second;
9164 }
9165
9166 void dwarf2_per_objfile::set_type_for_signatured_type
9167 (signatured_type *sig_type, struct type *type)
9168 {
9169 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9170
9171 this->m_type_map[sig_type] = type;
9172 }
9173
9174 /* A helper function for computing the list of all symbol tables
9175 included by PER_CU. */
9176
9177 static void
9178 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9179 htab_t all_children, htab_t all_type_symtabs,
9180 dwarf2_per_cu_data *per_cu,
9181 dwarf2_per_objfile *per_objfile,
9182 struct compunit_symtab *immediate_parent)
9183 {
9184 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9185 if (*slot != NULL)
9186 {
9187 /* This inclusion and its children have been processed. */
9188 return;
9189 }
9190
9191 *slot = per_cu;
9192
9193 /* Only add a CU if it has a symbol table. */
9194 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9195 if (cust != NULL)
9196 {
9197 /* If this is a type unit only add its symbol table if we haven't
9198 seen it yet (type unit per_cu's can share symtabs). */
9199 if (per_cu->is_debug_types)
9200 {
9201 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9202 if (*slot == NULL)
9203 {
9204 *slot = cust;
9205 result->push_back (cust);
9206 if (cust->user == NULL)
9207 cust->user = immediate_parent;
9208 }
9209 }
9210 else
9211 {
9212 result->push_back (cust);
9213 if (cust->user == NULL)
9214 cust->user = immediate_parent;
9215 }
9216 }
9217
9218 if (!per_cu->imported_symtabs_empty ())
9219 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9220 {
9221 recursively_compute_inclusions (result, all_children,
9222 all_type_symtabs, ptr, per_objfile,
9223 cust);
9224 }
9225 }
9226
9227 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9228 PER_CU. */
9229
9230 static void
9231 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9232 dwarf2_per_objfile *per_objfile)
9233 {
9234 gdb_assert (! per_cu->is_debug_types);
9235
9236 if (!per_cu->imported_symtabs_empty ())
9237 {
9238 int len;
9239 std::vector<compunit_symtab *> result_symtabs;
9240 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9241
9242 /* If we don't have a symtab, we can just skip this case. */
9243 if (cust == NULL)
9244 return;
9245
9246 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9247 htab_eq_pointer,
9248 NULL, xcalloc, xfree));
9249 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9250 htab_eq_pointer,
9251 NULL, xcalloc, xfree));
9252
9253 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9254 {
9255 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9256 all_type_symtabs.get (), ptr,
9257 per_objfile, cust);
9258 }
9259
9260 /* Now we have a transitive closure of all the included symtabs. */
9261 len = result_symtabs.size ();
9262 cust->includes
9263 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9264 struct compunit_symtab *, len + 1);
9265 memcpy (cust->includes, result_symtabs.data (),
9266 len * sizeof (compunit_symtab *));
9267 cust->includes[len] = NULL;
9268 }
9269 }
9270
9271 /* Compute the 'includes' field for the symtabs of all the CUs we just
9272 read. */
9273
9274 static void
9275 process_cu_includes (dwarf2_per_objfile *per_objfile)
9276 {
9277 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9278 {
9279 if (! iter->is_debug_types)
9280 compute_compunit_symtab_includes (iter, per_objfile);
9281 }
9282
9283 per_objfile->per_bfd->just_read_cus.clear ();
9284 }
9285
9286 /* Generate full symbol information for CU, whose DIEs have
9287 already been loaded into memory. */
9288
9289 static void
9290 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9291 {
9292 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9293 struct objfile *objfile = per_objfile->objfile;
9294 struct gdbarch *gdbarch = objfile->arch ();
9295 CORE_ADDR lowpc, highpc;
9296 struct compunit_symtab *cust;
9297 CORE_ADDR baseaddr;
9298 struct block *static_block;
9299 CORE_ADDR addr;
9300
9301 baseaddr = objfile->text_section_offset ();
9302
9303 /* Clear the list here in case something was left over. */
9304 cu->method_list.clear ();
9305
9306 cu->language = pretend_language;
9307 cu->language_defn = language_def (cu->language);
9308
9309 dwarf2_find_base_address (cu->dies, cu);
9310
9311 /* Before we start reading the top-level DIE, ensure it has a valid tag
9312 type. */
9313 switch (cu->dies->tag)
9314 {
9315 case DW_TAG_compile_unit:
9316 case DW_TAG_partial_unit:
9317 case DW_TAG_type_unit:
9318 break;
9319 default:
9320 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9321 dwarf_tag_name (cu->dies->tag),
9322 sect_offset_str (cu->per_cu->sect_off),
9323 objfile_name (per_objfile->objfile));
9324 }
9325
9326 /* Do line number decoding in read_file_scope () */
9327 process_die (cu->dies, cu);
9328
9329 /* For now fudge the Go package. */
9330 if (cu->language == language_go)
9331 fixup_go_packaging (cu);
9332
9333 /* Now that we have processed all the DIEs in the CU, all the types
9334 should be complete, and it should now be safe to compute all of the
9335 physnames. */
9336 compute_delayed_physnames (cu);
9337
9338 if (cu->language == language_rust)
9339 rust_union_quirks (cu);
9340
9341 /* Some compilers don't define a DW_AT_high_pc attribute for the
9342 compilation unit. If the DW_AT_high_pc is missing, synthesize
9343 it, by scanning the DIE's below the compilation unit. */
9344 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9345
9346 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9347 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9348
9349 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9350 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9351 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9352 addrmap to help ensure it has an accurate map of pc values belonging to
9353 this comp unit. */
9354 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9355
9356 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9357 SECT_OFF_TEXT (objfile),
9358 0);
9359
9360 if (cust != NULL)
9361 {
9362 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9363
9364 /* Set symtab language to language from DW_AT_language. If the
9365 compilation is from a C file generated by language preprocessors, do
9366 not set the language if it was already deduced by start_subfile. */
9367 if (!(cu->language == language_c
9368 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9369 COMPUNIT_FILETABS (cust)->language = cu->language;
9370
9371 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9372 produce DW_AT_location with location lists but it can be possibly
9373 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9374 there were bugs in prologue debug info, fixed later in GCC-4.5
9375 by "unwind info for epilogues" patch (which is not directly related).
9376
9377 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9378 needed, it would be wrong due to missing DW_AT_producer there.
9379
9380 Still one can confuse GDB by using non-standard GCC compilation
9381 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9382 */
9383 if (cu->has_loclist && gcc_4_minor >= 5)
9384 cust->locations_valid = 1;
9385
9386 if (gcc_4_minor >= 5)
9387 cust->epilogue_unwind_valid = 1;
9388
9389 cust->call_site_htab = cu->call_site_htab;
9390 }
9391
9392 per_objfile->set_symtab (cu->per_cu, cust);
9393
9394 /* Push it for inclusion processing later. */
9395 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9396
9397 /* Not needed any more. */
9398 cu->reset_builder ();
9399 }
9400
9401 /* Generate full symbol information for type unit CU, whose DIEs have
9402 already been loaded into memory. */
9403
9404 static void
9405 process_full_type_unit (dwarf2_cu *cu,
9406 enum language pretend_language)
9407 {
9408 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9409 struct objfile *objfile = per_objfile->objfile;
9410 struct compunit_symtab *cust;
9411 struct signatured_type *sig_type;
9412
9413 gdb_assert (cu->per_cu->is_debug_types);
9414 sig_type = (struct signatured_type *) cu->per_cu;
9415
9416 /* Clear the list here in case something was left over. */
9417 cu->method_list.clear ();
9418
9419 cu->language = pretend_language;
9420 cu->language_defn = language_def (cu->language);
9421
9422 /* The symbol tables are set up in read_type_unit_scope. */
9423 process_die (cu->dies, cu);
9424
9425 /* For now fudge the Go package. */
9426 if (cu->language == language_go)
9427 fixup_go_packaging (cu);
9428
9429 /* Now that we have processed all the DIEs in the CU, all the types
9430 should be complete, and it should now be safe to compute all of the
9431 physnames. */
9432 compute_delayed_physnames (cu);
9433
9434 if (cu->language == language_rust)
9435 rust_union_quirks (cu);
9436
9437 /* TUs share symbol tables.
9438 If this is the first TU to use this symtab, complete the construction
9439 of it with end_expandable_symtab. Otherwise, complete the addition of
9440 this TU's symbols to the existing symtab. */
9441 type_unit_group_unshareable *tug_unshare =
9442 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9443 if (tug_unshare->compunit_symtab == NULL)
9444 {
9445 buildsym_compunit *builder = cu->get_builder ();
9446 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9447 tug_unshare->compunit_symtab = cust;
9448
9449 if (cust != NULL)
9450 {
9451 /* Set symtab language to language from DW_AT_language. If the
9452 compilation is from a C file generated by language preprocessors,
9453 do not set the language if it was already deduced by
9454 start_subfile. */
9455 if (!(cu->language == language_c
9456 && COMPUNIT_FILETABS (cust)->language != language_c))
9457 COMPUNIT_FILETABS (cust)->language = cu->language;
9458 }
9459 }
9460 else
9461 {
9462 cu->get_builder ()->augment_type_symtab ();
9463 cust = tug_unshare->compunit_symtab;
9464 }
9465
9466 per_objfile->set_symtab (cu->per_cu, cust);
9467
9468 /* Not needed any more. */
9469 cu->reset_builder ();
9470 }
9471
9472 /* Process an imported unit DIE. */
9473
9474 static void
9475 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9476 {
9477 struct attribute *attr;
9478
9479 /* For now we don't handle imported units in type units. */
9480 if (cu->per_cu->is_debug_types)
9481 {
9482 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9483 " supported in type units [in module %s]"),
9484 objfile_name (cu->per_objfile->objfile));
9485 }
9486
9487 attr = dwarf2_attr (die, DW_AT_import, cu);
9488 if (attr != NULL)
9489 {
9490 sect_offset sect_off = attr->get_ref_die_offset ();
9491 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9492 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9493 dwarf2_per_cu_data *per_cu
9494 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9495
9496 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9497 into another compilation unit, at root level. Regard this as a hint,
9498 and ignore it. */
9499 if (die->parent && die->parent->parent == NULL
9500 && per_cu->unit_type == DW_UT_compile
9501 && per_cu->lang == language_cplus)
9502 return;
9503
9504 /* If necessary, add it to the queue and load its DIEs. */
9505 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9506 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9507 false, cu->language);
9508
9509 cu->per_cu->imported_symtabs_push (per_cu);
9510 }
9511 }
9512
9513 /* RAII object that represents a process_die scope: i.e.,
9514 starts/finishes processing a DIE. */
9515 class process_die_scope
9516 {
9517 public:
9518 process_die_scope (die_info *die, dwarf2_cu *cu)
9519 : m_die (die), m_cu (cu)
9520 {
9521 /* We should only be processing DIEs not already in process. */
9522 gdb_assert (!m_die->in_process);
9523 m_die->in_process = true;
9524 }
9525
9526 ~process_die_scope ()
9527 {
9528 m_die->in_process = false;
9529
9530 /* If we're done processing the DIE for the CU that owns the line
9531 header, we don't need the line header anymore. */
9532 if (m_cu->line_header_die_owner == m_die)
9533 {
9534 delete m_cu->line_header;
9535 m_cu->line_header = NULL;
9536 m_cu->line_header_die_owner = NULL;
9537 }
9538 }
9539
9540 private:
9541 die_info *m_die;
9542 dwarf2_cu *m_cu;
9543 };
9544
9545 /* Process a die and its children. */
9546
9547 static void
9548 process_die (struct die_info *die, struct dwarf2_cu *cu)
9549 {
9550 process_die_scope scope (die, cu);
9551
9552 switch (die->tag)
9553 {
9554 case DW_TAG_padding:
9555 break;
9556 case DW_TAG_compile_unit:
9557 case DW_TAG_partial_unit:
9558 read_file_scope (die, cu);
9559 break;
9560 case DW_TAG_type_unit:
9561 read_type_unit_scope (die, cu);
9562 break;
9563 case DW_TAG_subprogram:
9564 /* Nested subprograms in Fortran get a prefix. */
9565 if (cu->language == language_fortran
9566 && die->parent != NULL
9567 && die->parent->tag == DW_TAG_subprogram)
9568 cu->processing_has_namespace_info = true;
9569 /* Fall through. */
9570 case DW_TAG_inlined_subroutine:
9571 read_func_scope (die, cu);
9572 break;
9573 case DW_TAG_lexical_block:
9574 case DW_TAG_try_block:
9575 case DW_TAG_catch_block:
9576 read_lexical_block_scope (die, cu);
9577 break;
9578 case DW_TAG_call_site:
9579 case DW_TAG_GNU_call_site:
9580 read_call_site_scope (die, cu);
9581 break;
9582 case DW_TAG_class_type:
9583 case DW_TAG_interface_type:
9584 case DW_TAG_structure_type:
9585 case DW_TAG_union_type:
9586 process_structure_scope (die, cu);
9587 break;
9588 case DW_TAG_enumeration_type:
9589 process_enumeration_scope (die, cu);
9590 break;
9591
9592 /* These dies have a type, but processing them does not create
9593 a symbol or recurse to process the children. Therefore we can
9594 read them on-demand through read_type_die. */
9595 case DW_TAG_subroutine_type:
9596 case DW_TAG_set_type:
9597 case DW_TAG_pointer_type:
9598 case DW_TAG_ptr_to_member_type:
9599 case DW_TAG_reference_type:
9600 case DW_TAG_rvalue_reference_type:
9601 case DW_TAG_string_type:
9602 break;
9603
9604 case DW_TAG_array_type:
9605 /* We only need to handle this case for Ada -- in other
9606 languages, it's normal for the compiler to emit a typedef
9607 instead. */
9608 if (cu->language != language_ada)
9609 break;
9610 /* FALLTHROUGH */
9611 case DW_TAG_base_type:
9612 case DW_TAG_subrange_type:
9613 case DW_TAG_typedef:
9614 /* Add a typedef symbol for the type definition, if it has a
9615 DW_AT_name. */
9616 new_symbol (die, read_type_die (die, cu), cu);
9617 break;
9618 case DW_TAG_common_block:
9619 read_common_block (die, cu);
9620 break;
9621 case DW_TAG_common_inclusion:
9622 break;
9623 case DW_TAG_namespace:
9624 cu->processing_has_namespace_info = true;
9625 read_namespace (die, cu);
9626 break;
9627 case DW_TAG_module:
9628 cu->processing_has_namespace_info = true;
9629 read_module (die, cu);
9630 break;
9631 case DW_TAG_imported_declaration:
9632 cu->processing_has_namespace_info = true;
9633 if (read_namespace_alias (die, cu))
9634 break;
9635 /* The declaration is not a global namespace alias. */
9636 /* Fall through. */
9637 case DW_TAG_imported_module:
9638 cu->processing_has_namespace_info = true;
9639 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9640 || cu->language != language_fortran))
9641 complaint (_("Tag '%s' has unexpected children"),
9642 dwarf_tag_name (die->tag));
9643 read_import_statement (die, cu);
9644 break;
9645
9646 case DW_TAG_imported_unit:
9647 process_imported_unit_die (die, cu);
9648 break;
9649
9650 case DW_TAG_variable:
9651 read_variable (die, cu);
9652 break;
9653
9654 default:
9655 new_symbol (die, NULL, cu);
9656 break;
9657 }
9658 }
9659 \f
9660 /* DWARF name computation. */
9661
9662 /* A helper function for dwarf2_compute_name which determines whether DIE
9663 needs to have the name of the scope prepended to the name listed in the
9664 die. */
9665
9666 static int
9667 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9668 {
9669 struct attribute *attr;
9670
9671 switch (die->tag)
9672 {
9673 case DW_TAG_namespace:
9674 case DW_TAG_typedef:
9675 case DW_TAG_class_type:
9676 case DW_TAG_interface_type:
9677 case DW_TAG_structure_type:
9678 case DW_TAG_union_type:
9679 case DW_TAG_enumeration_type:
9680 case DW_TAG_enumerator:
9681 case DW_TAG_subprogram:
9682 case DW_TAG_inlined_subroutine:
9683 case DW_TAG_member:
9684 case DW_TAG_imported_declaration:
9685 return 1;
9686
9687 case DW_TAG_variable:
9688 case DW_TAG_constant:
9689 /* We only need to prefix "globally" visible variables. These include
9690 any variable marked with DW_AT_external or any variable that
9691 lives in a namespace. [Variables in anonymous namespaces
9692 require prefixing, but they are not DW_AT_external.] */
9693
9694 if (dwarf2_attr (die, DW_AT_specification, cu))
9695 {
9696 struct dwarf2_cu *spec_cu = cu;
9697
9698 return die_needs_namespace (die_specification (die, &spec_cu),
9699 spec_cu);
9700 }
9701
9702 attr = dwarf2_attr (die, DW_AT_external, cu);
9703 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9704 && die->parent->tag != DW_TAG_module)
9705 return 0;
9706 /* A variable in a lexical block of some kind does not need a
9707 namespace, even though in C++ such variables may be external
9708 and have a mangled name. */
9709 if (die->parent->tag == DW_TAG_lexical_block
9710 || die->parent->tag == DW_TAG_try_block
9711 || die->parent->tag == DW_TAG_catch_block
9712 || die->parent->tag == DW_TAG_subprogram)
9713 return 0;
9714 return 1;
9715
9716 default:
9717 return 0;
9718 }
9719 }
9720
9721 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9722 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9723 defined for the given DIE. */
9724
9725 static struct attribute *
9726 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9727 {
9728 struct attribute *attr;
9729
9730 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9731 if (attr == NULL)
9732 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9733
9734 return attr;
9735 }
9736
9737 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9738 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9739 defined for the given DIE. */
9740
9741 static const char *
9742 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9743 {
9744 const char *linkage_name;
9745
9746 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9747 if (linkage_name == NULL)
9748 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9749
9750 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9751 See https://github.com/rust-lang/rust/issues/32925. */
9752 if (cu->language == language_rust && linkage_name != NULL
9753 && strchr (linkage_name, '{') != NULL)
9754 linkage_name = NULL;
9755
9756 return linkage_name;
9757 }
9758
9759 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9760 compute the physname for the object, which include a method's:
9761 - formal parameters (C++),
9762 - receiver type (Go),
9763
9764 The term "physname" is a bit confusing.
9765 For C++, for example, it is the demangled name.
9766 For Go, for example, it's the mangled name.
9767
9768 For Ada, return the DIE's linkage name rather than the fully qualified
9769 name. PHYSNAME is ignored..
9770
9771 The result is allocated on the objfile->per_bfd's obstack and
9772 canonicalized. */
9773
9774 static const char *
9775 dwarf2_compute_name (const char *name,
9776 struct die_info *die, struct dwarf2_cu *cu,
9777 int physname)
9778 {
9779 struct objfile *objfile = cu->per_objfile->objfile;
9780
9781 if (name == NULL)
9782 name = dwarf2_name (die, cu);
9783
9784 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9785 but otherwise compute it by typename_concat inside GDB.
9786 FIXME: Actually this is not really true, or at least not always true.
9787 It's all very confusing. compute_and_set_names doesn't try to demangle
9788 Fortran names because there is no mangling standard. So new_symbol
9789 will set the demangled name to the result of dwarf2_full_name, and it is
9790 the demangled name that GDB uses if it exists. */
9791 if (cu->language == language_ada
9792 || (cu->language == language_fortran && physname))
9793 {
9794 /* For Ada unit, we prefer the linkage name over the name, as
9795 the former contains the exported name, which the user expects
9796 to be able to reference. Ideally, we want the user to be able
9797 to reference this entity using either natural or linkage name,
9798 but we haven't started looking at this enhancement yet. */
9799 const char *linkage_name = dw2_linkage_name (die, cu);
9800
9801 if (linkage_name != NULL)
9802 return linkage_name;
9803 }
9804
9805 /* These are the only languages we know how to qualify names in. */
9806 if (name != NULL
9807 && (cu->language == language_cplus
9808 || cu->language == language_fortran || cu->language == language_d
9809 || cu->language == language_rust))
9810 {
9811 if (die_needs_namespace (die, cu))
9812 {
9813 const char *prefix;
9814 const char *canonical_name = NULL;
9815
9816 string_file buf;
9817
9818 prefix = determine_prefix (die, cu);
9819 if (*prefix != '\0')
9820 {
9821 gdb::unique_xmalloc_ptr<char> prefixed_name
9822 (typename_concat (NULL, prefix, name, physname, cu));
9823
9824 buf.puts (prefixed_name.get ());
9825 }
9826 else
9827 buf.puts (name);
9828
9829 /* Template parameters may be specified in the DIE's DW_AT_name, or
9830 as children with DW_TAG_template_type_param or
9831 DW_TAG_value_type_param. If the latter, add them to the name
9832 here. If the name already has template parameters, then
9833 skip this step; some versions of GCC emit both, and
9834 it is more efficient to use the pre-computed name.
9835
9836 Something to keep in mind about this process: it is very
9837 unlikely, or in some cases downright impossible, to produce
9838 something that will match the mangled name of a function.
9839 If the definition of the function has the same debug info,
9840 we should be able to match up with it anyway. But fallbacks
9841 using the minimal symbol, for instance to find a method
9842 implemented in a stripped copy of libstdc++, will not work.
9843 If we do not have debug info for the definition, we will have to
9844 match them up some other way.
9845
9846 When we do name matching there is a related problem with function
9847 templates; two instantiated function templates are allowed to
9848 differ only by their return types, which we do not add here. */
9849
9850 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9851 {
9852 struct attribute *attr;
9853 struct die_info *child;
9854 int first = 1;
9855 const language_defn *cplus_lang = language_def (cu->language);
9856
9857 die->building_fullname = 1;
9858
9859 for (child = die->child; child != NULL; child = child->sibling)
9860 {
9861 struct type *type;
9862 LONGEST value;
9863 const gdb_byte *bytes;
9864 struct dwarf2_locexpr_baton *baton;
9865 struct value *v;
9866
9867 if (child->tag != DW_TAG_template_type_param
9868 && child->tag != DW_TAG_template_value_param)
9869 continue;
9870
9871 if (first)
9872 {
9873 buf.puts ("<");
9874 first = 0;
9875 }
9876 else
9877 buf.puts (", ");
9878
9879 attr = dwarf2_attr (child, DW_AT_type, cu);
9880 if (attr == NULL)
9881 {
9882 complaint (_("template parameter missing DW_AT_type"));
9883 buf.puts ("UNKNOWN_TYPE");
9884 continue;
9885 }
9886 type = die_type (child, cu);
9887
9888 if (child->tag == DW_TAG_template_type_param)
9889 {
9890 cplus_lang->print_type (type, "", &buf, -1, 0,
9891 &type_print_raw_options);
9892 continue;
9893 }
9894
9895 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9896 if (attr == NULL)
9897 {
9898 complaint (_("template parameter missing "
9899 "DW_AT_const_value"));
9900 buf.puts ("UNKNOWN_VALUE");
9901 continue;
9902 }
9903
9904 dwarf2_const_value_attr (attr, type, name,
9905 &cu->comp_unit_obstack, cu,
9906 &value, &bytes, &baton);
9907
9908 if (type->has_no_signedness ())
9909 /* GDB prints characters as NUMBER 'CHAR'. If that's
9910 changed, this can use value_print instead. */
9911 cplus_lang->printchar (value, type, &buf);
9912 else
9913 {
9914 struct value_print_options opts;
9915
9916 if (baton != NULL)
9917 v = dwarf2_evaluate_loc_desc (type, NULL,
9918 baton->data,
9919 baton->size,
9920 baton->per_cu,
9921 baton->per_objfile);
9922 else if (bytes != NULL)
9923 {
9924 v = allocate_value (type);
9925 memcpy (value_contents_writeable (v), bytes,
9926 TYPE_LENGTH (type));
9927 }
9928 else
9929 v = value_from_longest (type, value);
9930
9931 /* Specify decimal so that we do not depend on
9932 the radix. */
9933 get_formatted_print_options (&opts, 'd');
9934 opts.raw = 1;
9935 value_print (v, &buf, &opts);
9936 release_value (v);
9937 }
9938 }
9939
9940 die->building_fullname = 0;
9941
9942 if (!first)
9943 {
9944 /* Close the argument list, with a space if necessary
9945 (nested templates). */
9946 if (!buf.empty () && buf.string ().back () == '>')
9947 buf.puts (" >");
9948 else
9949 buf.puts (">");
9950 }
9951 }
9952
9953 /* For C++ methods, append formal parameter type
9954 information, if PHYSNAME. */
9955
9956 if (physname && die->tag == DW_TAG_subprogram
9957 && cu->language == language_cplus)
9958 {
9959 struct type *type = read_type_die (die, cu);
9960
9961 c_type_print_args (type, &buf, 1, cu->language,
9962 &type_print_raw_options);
9963
9964 if (cu->language == language_cplus)
9965 {
9966 /* Assume that an artificial first parameter is
9967 "this", but do not crash if it is not. RealView
9968 marks unnamed (and thus unused) parameters as
9969 artificial; there is no way to differentiate
9970 the two cases. */
9971 if (type->num_fields () > 0
9972 && TYPE_FIELD_ARTIFICIAL (type, 0)
9973 && type->field (0).type ()->code () == TYPE_CODE_PTR
9974 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
9975 buf.puts (" const");
9976 }
9977 }
9978
9979 const std::string &intermediate_name = buf.string ();
9980
9981 if (cu->language == language_cplus)
9982 canonical_name
9983 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9984 objfile);
9985
9986 /* If we only computed INTERMEDIATE_NAME, or if
9987 INTERMEDIATE_NAME is already canonical, then we need to
9988 intern it. */
9989 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9990 name = objfile->intern (intermediate_name);
9991 else
9992 name = canonical_name;
9993 }
9994 }
9995
9996 return name;
9997 }
9998
9999 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10000 If scope qualifiers are appropriate they will be added. The result
10001 will be allocated on the storage_obstack, or NULL if the DIE does
10002 not have a name. NAME may either be from a previous call to
10003 dwarf2_name or NULL.
10004
10005 The output string will be canonicalized (if C++). */
10006
10007 static const char *
10008 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10009 {
10010 return dwarf2_compute_name (name, die, cu, 0);
10011 }
10012
10013 /* Construct a physname for the given DIE in CU. NAME may either be
10014 from a previous call to dwarf2_name or NULL. The result will be
10015 allocated on the objfile_objstack or NULL if the DIE does not have a
10016 name.
10017
10018 The output string will be canonicalized (if C++). */
10019
10020 static const char *
10021 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10022 {
10023 struct objfile *objfile = cu->per_objfile->objfile;
10024 const char *retval, *mangled = NULL, *canon = NULL;
10025 int need_copy = 1;
10026
10027 /* In this case dwarf2_compute_name is just a shortcut not building anything
10028 on its own. */
10029 if (!die_needs_namespace (die, cu))
10030 return dwarf2_compute_name (name, die, cu, 1);
10031
10032 if (cu->language != language_rust)
10033 mangled = dw2_linkage_name (die, cu);
10034
10035 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10036 has computed. */
10037 gdb::unique_xmalloc_ptr<char> demangled;
10038 if (mangled != NULL)
10039 {
10040
10041 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10042 {
10043 /* Do nothing (do not demangle the symbol name). */
10044 }
10045 else
10046 {
10047 /* Use DMGL_RET_DROP for C++ template functions to suppress
10048 their return type. It is easier for GDB users to search
10049 for such functions as `name(params)' than `long name(params)'.
10050 In such case the minimal symbol names do not match the full
10051 symbol names but for template functions there is never a need
10052 to look up their definition from their declaration so
10053 the only disadvantage remains the minimal symbol variant
10054 `long name(params)' does not have the proper inferior type. */
10055 demangled.reset (gdb_demangle (mangled,
10056 (DMGL_PARAMS | DMGL_ANSI
10057 | DMGL_RET_DROP)));
10058 }
10059 if (demangled)
10060 canon = demangled.get ();
10061 else
10062 {
10063 canon = mangled;
10064 need_copy = 0;
10065 }
10066 }
10067
10068 if (canon == NULL || check_physname)
10069 {
10070 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10071
10072 if (canon != NULL && strcmp (physname, canon) != 0)
10073 {
10074 /* It may not mean a bug in GDB. The compiler could also
10075 compute DW_AT_linkage_name incorrectly. But in such case
10076 GDB would need to be bug-to-bug compatible. */
10077
10078 complaint (_("Computed physname <%s> does not match demangled <%s> "
10079 "(from linkage <%s>) - DIE at %s [in module %s]"),
10080 physname, canon, mangled, sect_offset_str (die->sect_off),
10081 objfile_name (objfile));
10082
10083 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10084 is available here - over computed PHYSNAME. It is safer
10085 against both buggy GDB and buggy compilers. */
10086
10087 retval = canon;
10088 }
10089 else
10090 {
10091 retval = physname;
10092 need_copy = 0;
10093 }
10094 }
10095 else
10096 retval = canon;
10097
10098 if (need_copy)
10099 retval = objfile->intern (retval);
10100
10101 return retval;
10102 }
10103
10104 /* Inspect DIE in CU for a namespace alias. If one exists, record
10105 a new symbol for it.
10106
10107 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10108
10109 static int
10110 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10111 {
10112 struct attribute *attr;
10113
10114 /* If the die does not have a name, this is not a namespace
10115 alias. */
10116 attr = dwarf2_attr (die, DW_AT_name, cu);
10117 if (attr != NULL)
10118 {
10119 int num;
10120 struct die_info *d = die;
10121 struct dwarf2_cu *imported_cu = cu;
10122
10123 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10124 keep inspecting DIEs until we hit the underlying import. */
10125 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10126 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10127 {
10128 attr = dwarf2_attr (d, DW_AT_import, cu);
10129 if (attr == NULL)
10130 break;
10131
10132 d = follow_die_ref (d, attr, &imported_cu);
10133 if (d->tag != DW_TAG_imported_declaration)
10134 break;
10135 }
10136
10137 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10138 {
10139 complaint (_("DIE at %s has too many recursively imported "
10140 "declarations"), sect_offset_str (d->sect_off));
10141 return 0;
10142 }
10143
10144 if (attr != NULL)
10145 {
10146 struct type *type;
10147 sect_offset sect_off = attr->get_ref_die_offset ();
10148
10149 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10150 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10151 {
10152 /* This declaration is a global namespace alias. Add
10153 a symbol for it whose type is the aliased namespace. */
10154 new_symbol (die, type, cu);
10155 return 1;
10156 }
10157 }
10158 }
10159
10160 return 0;
10161 }
10162
10163 /* Return the using directives repository (global or local?) to use in the
10164 current context for CU.
10165
10166 For Ada, imported declarations can materialize renamings, which *may* be
10167 global. However it is impossible (for now?) in DWARF to distinguish
10168 "external" imported declarations and "static" ones. As all imported
10169 declarations seem to be static in all other languages, make them all CU-wide
10170 global only in Ada. */
10171
10172 static struct using_direct **
10173 using_directives (struct dwarf2_cu *cu)
10174 {
10175 if (cu->language == language_ada
10176 && cu->get_builder ()->outermost_context_p ())
10177 return cu->get_builder ()->get_global_using_directives ();
10178 else
10179 return cu->get_builder ()->get_local_using_directives ();
10180 }
10181
10182 /* Read the import statement specified by the given die and record it. */
10183
10184 static void
10185 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10186 {
10187 struct objfile *objfile = cu->per_objfile->objfile;
10188 struct attribute *import_attr;
10189 struct die_info *imported_die, *child_die;
10190 struct dwarf2_cu *imported_cu;
10191 const char *imported_name;
10192 const char *imported_name_prefix;
10193 const char *canonical_name;
10194 const char *import_alias;
10195 const char *imported_declaration = NULL;
10196 const char *import_prefix;
10197 std::vector<const char *> excludes;
10198
10199 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10200 if (import_attr == NULL)
10201 {
10202 complaint (_("Tag '%s' has no DW_AT_import"),
10203 dwarf_tag_name (die->tag));
10204 return;
10205 }
10206
10207 imported_cu = cu;
10208 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10209 imported_name = dwarf2_name (imported_die, imported_cu);
10210 if (imported_name == NULL)
10211 {
10212 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10213
10214 The import in the following code:
10215 namespace A
10216 {
10217 typedef int B;
10218 }
10219
10220 int main ()
10221 {
10222 using A::B;
10223 B b;
10224 return b;
10225 }
10226
10227 ...
10228 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10229 <52> DW_AT_decl_file : 1
10230 <53> DW_AT_decl_line : 6
10231 <54> DW_AT_import : <0x75>
10232 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10233 <59> DW_AT_name : B
10234 <5b> DW_AT_decl_file : 1
10235 <5c> DW_AT_decl_line : 2
10236 <5d> DW_AT_type : <0x6e>
10237 ...
10238 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10239 <76> DW_AT_byte_size : 4
10240 <77> DW_AT_encoding : 5 (signed)
10241
10242 imports the wrong die ( 0x75 instead of 0x58 ).
10243 This case will be ignored until the gcc bug is fixed. */
10244 return;
10245 }
10246
10247 /* Figure out the local name after import. */
10248 import_alias = dwarf2_name (die, cu);
10249
10250 /* Figure out where the statement is being imported to. */
10251 import_prefix = determine_prefix (die, cu);
10252
10253 /* Figure out what the scope of the imported die is and prepend it
10254 to the name of the imported die. */
10255 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10256
10257 if (imported_die->tag != DW_TAG_namespace
10258 && imported_die->tag != DW_TAG_module)
10259 {
10260 imported_declaration = imported_name;
10261 canonical_name = imported_name_prefix;
10262 }
10263 else if (strlen (imported_name_prefix) > 0)
10264 canonical_name = obconcat (&objfile->objfile_obstack,
10265 imported_name_prefix,
10266 (cu->language == language_d ? "." : "::"),
10267 imported_name, (char *) NULL);
10268 else
10269 canonical_name = imported_name;
10270
10271 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10272 for (child_die = die->child; child_die && child_die->tag;
10273 child_die = child_die->sibling)
10274 {
10275 /* DWARF-4: A Fortran use statement with a “rename list” may be
10276 represented by an imported module entry with an import attribute
10277 referring to the module and owned entries corresponding to those
10278 entities that are renamed as part of being imported. */
10279
10280 if (child_die->tag != DW_TAG_imported_declaration)
10281 {
10282 complaint (_("child DW_TAG_imported_declaration expected "
10283 "- DIE at %s [in module %s]"),
10284 sect_offset_str (child_die->sect_off),
10285 objfile_name (objfile));
10286 continue;
10287 }
10288
10289 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10290 if (import_attr == NULL)
10291 {
10292 complaint (_("Tag '%s' has no DW_AT_import"),
10293 dwarf_tag_name (child_die->tag));
10294 continue;
10295 }
10296
10297 imported_cu = cu;
10298 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10299 &imported_cu);
10300 imported_name = dwarf2_name (imported_die, imported_cu);
10301 if (imported_name == NULL)
10302 {
10303 complaint (_("child DW_TAG_imported_declaration has unknown "
10304 "imported name - DIE at %s [in module %s]"),
10305 sect_offset_str (child_die->sect_off),
10306 objfile_name (objfile));
10307 continue;
10308 }
10309
10310 excludes.push_back (imported_name);
10311
10312 process_die (child_die, cu);
10313 }
10314
10315 add_using_directive (using_directives (cu),
10316 import_prefix,
10317 canonical_name,
10318 import_alias,
10319 imported_declaration,
10320 excludes,
10321 0,
10322 &objfile->objfile_obstack);
10323 }
10324
10325 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10326 types, but gives them a size of zero. Starting with version 14,
10327 ICC is compatible with GCC. */
10328
10329 static bool
10330 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10331 {
10332 if (!cu->checked_producer)
10333 check_producer (cu);
10334
10335 return cu->producer_is_icc_lt_14;
10336 }
10337
10338 /* ICC generates a DW_AT_type for C void functions. This was observed on
10339 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10340 which says that void functions should not have a DW_AT_type. */
10341
10342 static bool
10343 producer_is_icc (struct dwarf2_cu *cu)
10344 {
10345 if (!cu->checked_producer)
10346 check_producer (cu);
10347
10348 return cu->producer_is_icc;
10349 }
10350
10351 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10352 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10353 this, it was first present in GCC release 4.3.0. */
10354
10355 static bool
10356 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10357 {
10358 if (!cu->checked_producer)
10359 check_producer (cu);
10360
10361 return cu->producer_is_gcc_lt_4_3;
10362 }
10363
10364 static file_and_directory
10365 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10366 {
10367 file_and_directory res;
10368
10369 /* Find the filename. Do not use dwarf2_name here, since the filename
10370 is not a source language identifier. */
10371 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10372 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10373
10374 if (res.comp_dir == NULL
10375 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10376 && IS_ABSOLUTE_PATH (res.name))
10377 {
10378 res.comp_dir_storage = ldirname (res.name);
10379 if (!res.comp_dir_storage.empty ())
10380 res.comp_dir = res.comp_dir_storage.c_str ();
10381 }
10382 if (res.comp_dir != NULL)
10383 {
10384 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10385 directory, get rid of it. */
10386 const char *cp = strchr (res.comp_dir, ':');
10387
10388 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10389 res.comp_dir = cp + 1;
10390 }
10391
10392 if (res.name == NULL)
10393 res.name = "<unknown>";
10394
10395 return res;
10396 }
10397
10398 /* Handle DW_AT_stmt_list for a compilation unit.
10399 DIE is the DW_TAG_compile_unit die for CU.
10400 COMP_DIR is the compilation directory. LOWPC is passed to
10401 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10402
10403 static void
10404 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10405 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10406 {
10407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10408 struct attribute *attr;
10409 struct line_header line_header_local;
10410 hashval_t line_header_local_hash;
10411 void **slot;
10412 int decode_mapping;
10413
10414 gdb_assert (! cu->per_cu->is_debug_types);
10415
10416 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10417 if (attr == NULL || !attr->form_is_unsigned ())
10418 return;
10419
10420 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10421
10422 /* The line header hash table is only created if needed (it exists to
10423 prevent redundant reading of the line table for partial_units).
10424 If we're given a partial_unit, we'll need it. If we're given a
10425 compile_unit, then use the line header hash table if it's already
10426 created, but don't create one just yet. */
10427
10428 if (per_objfile->line_header_hash == NULL
10429 && die->tag == DW_TAG_partial_unit)
10430 {
10431 per_objfile->line_header_hash
10432 .reset (htab_create_alloc (127, line_header_hash_voidp,
10433 line_header_eq_voidp,
10434 free_line_header_voidp,
10435 xcalloc, xfree));
10436 }
10437
10438 line_header_local.sect_off = line_offset;
10439 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10440 line_header_local_hash = line_header_hash (&line_header_local);
10441 if (per_objfile->line_header_hash != NULL)
10442 {
10443 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10444 &line_header_local,
10445 line_header_local_hash, NO_INSERT);
10446
10447 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10448 is not present in *SLOT (since if there is something in *SLOT then
10449 it will be for a partial_unit). */
10450 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10451 {
10452 gdb_assert (*slot != NULL);
10453 cu->line_header = (struct line_header *) *slot;
10454 return;
10455 }
10456 }
10457
10458 /* dwarf_decode_line_header does not yet provide sufficient information.
10459 We always have to call also dwarf_decode_lines for it. */
10460 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10461 if (lh == NULL)
10462 return;
10463
10464 cu->line_header = lh.release ();
10465 cu->line_header_die_owner = die;
10466
10467 if (per_objfile->line_header_hash == NULL)
10468 slot = NULL;
10469 else
10470 {
10471 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10472 &line_header_local,
10473 line_header_local_hash, INSERT);
10474 gdb_assert (slot != NULL);
10475 }
10476 if (slot != NULL && *slot == NULL)
10477 {
10478 /* This newly decoded line number information unit will be owned
10479 by line_header_hash hash table. */
10480 *slot = cu->line_header;
10481 cu->line_header_die_owner = NULL;
10482 }
10483 else
10484 {
10485 /* We cannot free any current entry in (*slot) as that struct line_header
10486 may be already used by multiple CUs. Create only temporary decoded
10487 line_header for this CU - it may happen at most once for each line
10488 number information unit. And if we're not using line_header_hash
10489 then this is what we want as well. */
10490 gdb_assert (die->tag != DW_TAG_partial_unit);
10491 }
10492 decode_mapping = (die->tag != DW_TAG_partial_unit);
10493 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10494 decode_mapping);
10495
10496 }
10497
10498 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10499
10500 static void
10501 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10502 {
10503 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10504 struct objfile *objfile = per_objfile->objfile;
10505 struct gdbarch *gdbarch = objfile->arch ();
10506 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10507 CORE_ADDR highpc = ((CORE_ADDR) 0);
10508 struct attribute *attr;
10509 struct die_info *child_die;
10510 CORE_ADDR baseaddr;
10511
10512 prepare_one_comp_unit (cu, die, cu->language);
10513 baseaddr = objfile->text_section_offset ();
10514
10515 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10516
10517 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10518 from finish_block. */
10519 if (lowpc == ((CORE_ADDR) -1))
10520 lowpc = highpc;
10521 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10522
10523 file_and_directory fnd = find_file_and_directory (die, cu);
10524
10525 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10526 standardised yet. As a workaround for the language detection we fall
10527 back to the DW_AT_producer string. */
10528 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10529 cu->language = language_opencl;
10530
10531 /* Similar hack for Go. */
10532 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10533 set_cu_language (DW_LANG_Go, cu);
10534
10535 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10536
10537 /* Decode line number information if present. We do this before
10538 processing child DIEs, so that the line header table is available
10539 for DW_AT_decl_file. */
10540 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10541
10542 /* Process all dies in compilation unit. */
10543 if (die->child != NULL)
10544 {
10545 child_die = die->child;
10546 while (child_die && child_die->tag)
10547 {
10548 process_die (child_die, cu);
10549 child_die = child_die->sibling;
10550 }
10551 }
10552
10553 /* Decode macro information, if present. Dwarf 2 macro information
10554 refers to information in the line number info statement program
10555 header, so we can only read it if we've read the header
10556 successfully. */
10557 attr = dwarf2_attr (die, DW_AT_macros, cu);
10558 if (attr == NULL)
10559 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10560 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10561 {
10562 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10563 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10564
10565 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10566 }
10567 else
10568 {
10569 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10570 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10571 {
10572 unsigned int macro_offset = attr->as_unsigned ();
10573
10574 dwarf_decode_macros (cu, macro_offset, 0);
10575 }
10576 }
10577 }
10578
10579 void
10580 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10581 {
10582 struct type_unit_group *tu_group;
10583 int first_time;
10584 struct attribute *attr;
10585 unsigned int i;
10586 struct signatured_type *sig_type;
10587
10588 gdb_assert (per_cu->is_debug_types);
10589 sig_type = (struct signatured_type *) per_cu;
10590
10591 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10592
10593 /* If we're using .gdb_index (includes -readnow) then
10594 per_cu->type_unit_group may not have been set up yet. */
10595 if (sig_type->type_unit_group == NULL)
10596 sig_type->type_unit_group = get_type_unit_group (this, attr);
10597 tu_group = sig_type->type_unit_group;
10598
10599 /* If we've already processed this stmt_list there's no real need to
10600 do it again, we could fake it and just recreate the part we need
10601 (file name,index -> symtab mapping). If data shows this optimization
10602 is useful we can do it then. */
10603 type_unit_group_unshareable *tug_unshare
10604 = per_objfile->get_type_unit_group_unshareable (tu_group);
10605 first_time = tug_unshare->compunit_symtab == NULL;
10606
10607 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10608 debug info. */
10609 line_header_up lh;
10610 if (attr != NULL && attr->form_is_unsigned ())
10611 {
10612 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10613 lh = dwarf_decode_line_header (line_offset, this);
10614 }
10615 if (lh == NULL)
10616 {
10617 if (first_time)
10618 start_symtab ("", NULL, 0);
10619 else
10620 {
10621 gdb_assert (tug_unshare->symtabs == NULL);
10622 gdb_assert (m_builder == nullptr);
10623 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10624 m_builder.reset (new struct buildsym_compunit
10625 (COMPUNIT_OBJFILE (cust), "",
10626 COMPUNIT_DIRNAME (cust),
10627 compunit_language (cust),
10628 0, cust));
10629 list_in_scope = get_builder ()->get_file_symbols ();
10630 }
10631 return;
10632 }
10633
10634 line_header = lh.release ();
10635 line_header_die_owner = die;
10636
10637 if (first_time)
10638 {
10639 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10640
10641 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10642 still initializing it, and our caller (a few levels up)
10643 process_full_type_unit still needs to know if this is the first
10644 time. */
10645
10646 tug_unshare->symtabs
10647 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10648 struct symtab *, line_header->file_names_size ());
10649
10650 auto &file_names = line_header->file_names ();
10651 for (i = 0; i < file_names.size (); ++i)
10652 {
10653 file_entry &fe = file_names[i];
10654 dwarf2_start_subfile (this, fe.name,
10655 fe.include_dir (line_header));
10656 buildsym_compunit *b = get_builder ();
10657 if (b->get_current_subfile ()->symtab == NULL)
10658 {
10659 /* NOTE: start_subfile will recognize when it's been
10660 passed a file it has already seen. So we can't
10661 assume there's a simple mapping from
10662 cu->line_header->file_names to subfiles, plus
10663 cu->line_header->file_names may contain dups. */
10664 b->get_current_subfile ()->symtab
10665 = allocate_symtab (cust, b->get_current_subfile ()->name);
10666 }
10667
10668 fe.symtab = b->get_current_subfile ()->symtab;
10669 tug_unshare->symtabs[i] = fe.symtab;
10670 }
10671 }
10672 else
10673 {
10674 gdb_assert (m_builder == nullptr);
10675 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10676 m_builder.reset (new struct buildsym_compunit
10677 (COMPUNIT_OBJFILE (cust), "",
10678 COMPUNIT_DIRNAME (cust),
10679 compunit_language (cust),
10680 0, cust));
10681 list_in_scope = get_builder ()->get_file_symbols ();
10682
10683 auto &file_names = line_header->file_names ();
10684 for (i = 0; i < file_names.size (); ++i)
10685 {
10686 file_entry &fe = file_names[i];
10687 fe.symtab = tug_unshare->symtabs[i];
10688 }
10689 }
10690
10691 /* The main symtab is allocated last. Type units don't have DW_AT_name
10692 so they don't have a "real" (so to speak) symtab anyway.
10693 There is later code that will assign the main symtab to all symbols
10694 that don't have one. We need to handle the case of a symbol with a
10695 missing symtab (DW_AT_decl_file) anyway. */
10696 }
10697
10698 /* Process DW_TAG_type_unit.
10699 For TUs we want to skip the first top level sibling if it's not the
10700 actual type being defined by this TU. In this case the first top
10701 level sibling is there to provide context only. */
10702
10703 static void
10704 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10705 {
10706 struct die_info *child_die;
10707
10708 prepare_one_comp_unit (cu, die, language_minimal);
10709
10710 /* Initialize (or reinitialize) the machinery for building symtabs.
10711 We do this before processing child DIEs, so that the line header table
10712 is available for DW_AT_decl_file. */
10713 cu->setup_type_unit_groups (die);
10714
10715 if (die->child != NULL)
10716 {
10717 child_die = die->child;
10718 while (child_die && child_die->tag)
10719 {
10720 process_die (child_die, cu);
10721 child_die = child_die->sibling;
10722 }
10723 }
10724 }
10725 \f
10726 /* DWO/DWP files.
10727
10728 http://gcc.gnu.org/wiki/DebugFission
10729 http://gcc.gnu.org/wiki/DebugFissionDWP
10730
10731 To simplify handling of both DWO files ("object" files with the DWARF info)
10732 and DWP files (a file with the DWOs packaged up into one file), we treat
10733 DWP files as having a collection of virtual DWO files. */
10734
10735 static hashval_t
10736 hash_dwo_file (const void *item)
10737 {
10738 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10739 hashval_t hash;
10740
10741 hash = htab_hash_string (dwo_file->dwo_name);
10742 if (dwo_file->comp_dir != NULL)
10743 hash += htab_hash_string (dwo_file->comp_dir);
10744 return hash;
10745 }
10746
10747 static int
10748 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10749 {
10750 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10751 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10752
10753 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10754 return 0;
10755 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10756 return lhs->comp_dir == rhs->comp_dir;
10757 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10758 }
10759
10760 /* Allocate a hash table for DWO files. */
10761
10762 static htab_up
10763 allocate_dwo_file_hash_table ()
10764 {
10765 auto delete_dwo_file = [] (void *item)
10766 {
10767 struct dwo_file *dwo_file = (struct dwo_file *) item;
10768
10769 delete dwo_file;
10770 };
10771
10772 return htab_up (htab_create_alloc (41,
10773 hash_dwo_file,
10774 eq_dwo_file,
10775 delete_dwo_file,
10776 xcalloc, xfree));
10777 }
10778
10779 /* Lookup DWO file DWO_NAME. */
10780
10781 static void **
10782 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10783 const char *dwo_name,
10784 const char *comp_dir)
10785 {
10786 struct dwo_file find_entry;
10787 void **slot;
10788
10789 if (per_objfile->per_bfd->dwo_files == NULL)
10790 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10791
10792 find_entry.dwo_name = dwo_name;
10793 find_entry.comp_dir = comp_dir;
10794 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10795 INSERT);
10796
10797 return slot;
10798 }
10799
10800 static hashval_t
10801 hash_dwo_unit (const void *item)
10802 {
10803 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10804
10805 /* This drops the top 32 bits of the id, but is ok for a hash. */
10806 return dwo_unit->signature;
10807 }
10808
10809 static int
10810 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10811 {
10812 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10813 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10814
10815 /* The signature is assumed to be unique within the DWO file.
10816 So while object file CU dwo_id's always have the value zero,
10817 that's OK, assuming each object file DWO file has only one CU,
10818 and that's the rule for now. */
10819 return lhs->signature == rhs->signature;
10820 }
10821
10822 /* Allocate a hash table for DWO CUs,TUs.
10823 There is one of these tables for each of CUs,TUs for each DWO file. */
10824
10825 static htab_up
10826 allocate_dwo_unit_table ()
10827 {
10828 /* Start out with a pretty small number.
10829 Generally DWO files contain only one CU and maybe some TUs. */
10830 return htab_up (htab_create_alloc (3,
10831 hash_dwo_unit,
10832 eq_dwo_unit,
10833 NULL, xcalloc, xfree));
10834 }
10835
10836 /* die_reader_func for create_dwo_cu. */
10837
10838 static void
10839 create_dwo_cu_reader (const struct die_reader_specs *reader,
10840 const gdb_byte *info_ptr,
10841 struct die_info *comp_unit_die,
10842 struct dwo_file *dwo_file,
10843 struct dwo_unit *dwo_unit)
10844 {
10845 struct dwarf2_cu *cu = reader->cu;
10846 sect_offset sect_off = cu->per_cu->sect_off;
10847 struct dwarf2_section_info *section = cu->per_cu->section;
10848
10849 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10850 if (!signature.has_value ())
10851 {
10852 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10853 " its dwo_id [in module %s]"),
10854 sect_offset_str (sect_off), dwo_file->dwo_name);
10855 return;
10856 }
10857
10858 dwo_unit->dwo_file = dwo_file;
10859 dwo_unit->signature = *signature;
10860 dwo_unit->section = section;
10861 dwo_unit->sect_off = sect_off;
10862 dwo_unit->length = cu->per_cu->length;
10863
10864 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10865 sect_offset_str (sect_off),
10866 hex_string (dwo_unit->signature));
10867 }
10868
10869 /* Create the dwo_units for the CUs in a DWO_FILE.
10870 Note: This function processes DWO files only, not DWP files. */
10871
10872 static void
10873 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10874 dwarf2_cu *cu, struct dwo_file &dwo_file,
10875 dwarf2_section_info &section, htab_up &cus_htab)
10876 {
10877 struct objfile *objfile = per_objfile->objfile;
10878 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10879 const gdb_byte *info_ptr, *end_ptr;
10880
10881 section.read (objfile);
10882 info_ptr = section.buffer;
10883
10884 if (info_ptr == NULL)
10885 return;
10886
10887 dwarf_read_debug_printf ("Reading %s for %s:",
10888 section.get_name (),
10889 section.get_file_name ());
10890
10891 end_ptr = info_ptr + section.size;
10892 while (info_ptr < end_ptr)
10893 {
10894 struct dwarf2_per_cu_data per_cu;
10895 struct dwo_unit read_unit {};
10896 struct dwo_unit *dwo_unit;
10897 void **slot;
10898 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10899
10900 per_cu.per_bfd = per_bfd;
10901 per_cu.is_debug_types = 0;
10902 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10903 per_cu.section = &section;
10904
10905 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
10906 if (!reader.dummy_p)
10907 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
10908 &dwo_file, &read_unit);
10909 info_ptr += per_cu.length;
10910
10911 // If the unit could not be parsed, skip it.
10912 if (read_unit.dwo_file == NULL)
10913 continue;
10914
10915 if (cus_htab == NULL)
10916 cus_htab = allocate_dwo_unit_table ();
10917
10918 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
10919 struct dwo_unit);
10920 *dwo_unit = read_unit;
10921 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
10922 gdb_assert (slot != NULL);
10923 if (*slot != NULL)
10924 {
10925 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10926 sect_offset dup_sect_off = dup_cu->sect_off;
10927
10928 complaint (_("debug cu entry at offset %s is duplicate to"
10929 " the entry at offset %s, signature %s"),
10930 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
10931 hex_string (dwo_unit->signature));
10932 }
10933 *slot = (void *)dwo_unit;
10934 }
10935 }
10936
10937 /* DWP file .debug_{cu,tu}_index section format:
10938 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10939 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10940
10941 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10942 officially standard DWP format was published with DWARF v5 and is called
10943 Version 5. There are no versions 3 or 4.
10944
10945 DWP Version 1:
10946
10947 Both index sections have the same format, and serve to map a 64-bit
10948 signature to a set of section numbers. Each section begins with a header,
10949 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10950 indexes, and a pool of 32-bit section numbers. The index sections will be
10951 aligned at 8-byte boundaries in the file.
10952
10953 The index section header consists of:
10954
10955 V, 32 bit version number
10956 -, 32 bits unused
10957 N, 32 bit number of compilation units or type units in the index
10958 M, 32 bit number of slots in the hash table
10959
10960 Numbers are recorded using the byte order of the application binary.
10961
10962 The hash table begins at offset 16 in the section, and consists of an array
10963 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10964 order of the application binary). Unused slots in the hash table are 0.
10965 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10966
10967 The parallel table begins immediately after the hash table
10968 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10969 array of 32-bit indexes (using the byte order of the application binary),
10970 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10971 table contains a 32-bit index into the pool of section numbers. For unused
10972 hash table slots, the corresponding entry in the parallel table will be 0.
10973
10974 The pool of section numbers begins immediately following the hash table
10975 (at offset 16 + 12 * M from the beginning of the section). The pool of
10976 section numbers consists of an array of 32-bit words (using the byte order
10977 of the application binary). Each item in the array is indexed starting
10978 from 0. The hash table entry provides the index of the first section
10979 number in the set. Additional section numbers in the set follow, and the
10980 set is terminated by a 0 entry (section number 0 is not used in ELF).
10981
10982 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10983 section must be the first entry in the set, and the .debug_abbrev.dwo must
10984 be the second entry. Other members of the set may follow in any order.
10985
10986 ---
10987
10988 DWP Versions 2 and 5:
10989
10990 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10991 and the entries in the index tables are now offsets into these sections.
10992 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10993 section.
10994
10995 Index Section Contents:
10996 Header
10997 Hash Table of Signatures dwp_hash_table.hash_table
10998 Parallel Table of Indices dwp_hash_table.unit_table
10999 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11000 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11001
11002 The index section header consists of:
11003
11004 V, 32 bit version number
11005 L, 32 bit number of columns in the table of section offsets
11006 N, 32 bit number of compilation units or type units in the index
11007 M, 32 bit number of slots in the hash table
11008
11009 Numbers are recorded using the byte order of the application binary.
11010
11011 The hash table has the same format as version 1.
11012 The parallel table of indices has the same format as version 1,
11013 except that the entries are origin-1 indices into the table of sections
11014 offsets and the table of section sizes.
11015
11016 The table of offsets begins immediately following the parallel table
11017 (at offset 16 + 12 * M from the beginning of the section). The table is
11018 a two-dimensional array of 32-bit words (using the byte order of the
11019 application binary), with L columns and N+1 rows, in row-major order.
11020 Each row in the array is indexed starting from 0. The first row provides
11021 a key to the remaining rows: each column in this row provides an identifier
11022 for a debug section, and the offsets in the same column of subsequent rows
11023 refer to that section. The section identifiers for Version 2 are:
11024
11025 DW_SECT_INFO 1 .debug_info.dwo
11026 DW_SECT_TYPES 2 .debug_types.dwo
11027 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11028 DW_SECT_LINE 4 .debug_line.dwo
11029 DW_SECT_LOC 5 .debug_loc.dwo
11030 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11031 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11032 DW_SECT_MACRO 8 .debug_macro.dwo
11033
11034 The section identifiers for Version 5 are:
11035
11036 DW_SECT_INFO_V5 1 .debug_info.dwo
11037 DW_SECT_RESERVED_V5 2 --
11038 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11039 DW_SECT_LINE_V5 4 .debug_line.dwo
11040 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11041 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11042 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11043 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11044
11045 The offsets provided by the CU and TU index sections are the base offsets
11046 for the contributions made by each CU or TU to the corresponding section
11047 in the package file. Each CU and TU header contains an abbrev_offset
11048 field, used to find the abbreviations table for that CU or TU within the
11049 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11050 be interpreted as relative to the base offset given in the index section.
11051 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11052 should be interpreted as relative to the base offset for .debug_line.dwo,
11053 and offsets into other debug sections obtained from DWARF attributes should
11054 also be interpreted as relative to the corresponding base offset.
11055
11056 The table of sizes begins immediately following the table of offsets.
11057 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11058 with L columns and N rows, in row-major order. Each row in the array is
11059 indexed starting from 1 (row 0 is shared by the two tables).
11060
11061 ---
11062
11063 Hash table lookup is handled the same in version 1 and 2:
11064
11065 We assume that N and M will not exceed 2^32 - 1.
11066 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11067
11068 Given a 64-bit compilation unit signature or a type signature S, an entry
11069 in the hash table is located as follows:
11070
11071 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11072 the low-order k bits all set to 1.
11073
11074 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11075
11076 3) If the hash table entry at index H matches the signature, use that
11077 entry. If the hash table entry at index H is unused (all zeroes),
11078 terminate the search: the signature is not present in the table.
11079
11080 4) Let H = (H + H') modulo M. Repeat at Step 3.
11081
11082 Because M > N and H' and M are relatively prime, the search is guaranteed
11083 to stop at an unused slot or find the match. */
11084
11085 /* Create a hash table to map DWO IDs to their CU/TU entry in
11086 .debug_{info,types}.dwo in DWP_FILE.
11087 Returns NULL if there isn't one.
11088 Note: This function processes DWP files only, not DWO files. */
11089
11090 static struct dwp_hash_table *
11091 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11092 struct dwp_file *dwp_file, int is_debug_types)
11093 {
11094 struct objfile *objfile = per_objfile->objfile;
11095 bfd *dbfd = dwp_file->dbfd.get ();
11096 const gdb_byte *index_ptr, *index_end;
11097 struct dwarf2_section_info *index;
11098 uint32_t version, nr_columns, nr_units, nr_slots;
11099 struct dwp_hash_table *htab;
11100
11101 if (is_debug_types)
11102 index = &dwp_file->sections.tu_index;
11103 else
11104 index = &dwp_file->sections.cu_index;
11105
11106 if (index->empty ())
11107 return NULL;
11108 index->read (objfile);
11109
11110 index_ptr = index->buffer;
11111 index_end = index_ptr + index->size;
11112
11113 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11114 For now it's safe to just read 4 bytes (particularly as it's difficult to
11115 tell if you're dealing with Version 5 before you've read the version). */
11116 version = read_4_bytes (dbfd, index_ptr);
11117 index_ptr += 4;
11118 if (version == 2 || version == 5)
11119 nr_columns = read_4_bytes (dbfd, index_ptr);
11120 else
11121 nr_columns = 0;
11122 index_ptr += 4;
11123 nr_units = read_4_bytes (dbfd, index_ptr);
11124 index_ptr += 4;
11125 nr_slots = read_4_bytes (dbfd, index_ptr);
11126 index_ptr += 4;
11127
11128 if (version != 1 && version != 2 && version != 5)
11129 {
11130 error (_("Dwarf Error: unsupported DWP file version (%s)"
11131 " [in module %s]"),
11132 pulongest (version), dwp_file->name);
11133 }
11134 if (nr_slots != (nr_slots & -nr_slots))
11135 {
11136 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11137 " is not power of 2 [in module %s]"),
11138 pulongest (nr_slots), dwp_file->name);
11139 }
11140
11141 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11142 htab->version = version;
11143 htab->nr_columns = nr_columns;
11144 htab->nr_units = nr_units;
11145 htab->nr_slots = nr_slots;
11146 htab->hash_table = index_ptr;
11147 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11148
11149 /* Exit early if the table is empty. */
11150 if (nr_slots == 0 || nr_units == 0
11151 || (version == 2 && nr_columns == 0)
11152 || (version == 5 && nr_columns == 0))
11153 {
11154 /* All must be zero. */
11155 if (nr_slots != 0 || nr_units != 0
11156 || (version == 2 && nr_columns != 0)
11157 || (version == 5 && nr_columns != 0))
11158 {
11159 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11160 " all zero [in modules %s]"),
11161 dwp_file->name);
11162 }
11163 return htab;
11164 }
11165
11166 if (version == 1)
11167 {
11168 htab->section_pool.v1.indices =
11169 htab->unit_table + sizeof (uint32_t) * nr_slots;
11170 /* It's harder to decide whether the section is too small in v1.
11171 V1 is deprecated anyway so we punt. */
11172 }
11173 else if (version == 2)
11174 {
11175 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11176 int *ids = htab->section_pool.v2.section_ids;
11177 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11178 /* Reverse map for error checking. */
11179 int ids_seen[DW_SECT_MAX + 1];
11180 int i;
11181
11182 if (nr_columns < 2)
11183 {
11184 error (_("Dwarf Error: bad DWP hash table, too few columns"
11185 " in section table [in module %s]"),
11186 dwp_file->name);
11187 }
11188 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11189 {
11190 error (_("Dwarf Error: bad DWP hash table, too many columns"
11191 " in section table [in module %s]"),
11192 dwp_file->name);
11193 }
11194 memset (ids, 255, sizeof_ids);
11195 memset (ids_seen, 255, sizeof (ids_seen));
11196 for (i = 0; i < nr_columns; ++i)
11197 {
11198 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11199
11200 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11201 {
11202 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11203 " in section table [in module %s]"),
11204 id, dwp_file->name);
11205 }
11206 if (ids_seen[id] != -1)
11207 {
11208 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11209 " id %d in section table [in module %s]"),
11210 id, dwp_file->name);
11211 }
11212 ids_seen[id] = i;
11213 ids[i] = id;
11214 }
11215 /* Must have exactly one info or types section. */
11216 if (((ids_seen[DW_SECT_INFO] != -1)
11217 + (ids_seen[DW_SECT_TYPES] != -1))
11218 != 1)
11219 {
11220 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11221 " DWO info/types section [in module %s]"),
11222 dwp_file->name);
11223 }
11224 /* Must have an abbrev section. */
11225 if (ids_seen[DW_SECT_ABBREV] == -1)
11226 {
11227 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11228 " section [in module %s]"),
11229 dwp_file->name);
11230 }
11231 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11232 htab->section_pool.v2.sizes =
11233 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11234 * nr_units * nr_columns);
11235 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11236 * nr_units * nr_columns))
11237 > index_end)
11238 {
11239 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11240 " [in module %s]"),
11241 dwp_file->name);
11242 }
11243 }
11244 else /* version == 5 */
11245 {
11246 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11247 int *ids = htab->section_pool.v5.section_ids;
11248 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11249 /* Reverse map for error checking. */
11250 int ids_seen[DW_SECT_MAX_V5 + 1];
11251
11252 if (nr_columns < 2)
11253 {
11254 error (_("Dwarf Error: bad DWP hash table, too few columns"
11255 " in section table [in module %s]"),
11256 dwp_file->name);
11257 }
11258 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11259 {
11260 error (_("Dwarf Error: bad DWP hash table, too many columns"
11261 " in section table [in module %s]"),
11262 dwp_file->name);
11263 }
11264 memset (ids, 255, sizeof_ids);
11265 memset (ids_seen, 255, sizeof (ids_seen));
11266 for (int i = 0; i < nr_columns; ++i)
11267 {
11268 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11269
11270 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11271 {
11272 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11273 " in section table [in module %s]"),
11274 id, dwp_file->name);
11275 }
11276 if (ids_seen[id] != -1)
11277 {
11278 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11279 " id %d in section table [in module %s]"),
11280 id, dwp_file->name);
11281 }
11282 ids_seen[id] = i;
11283 ids[i] = id;
11284 }
11285 /* Must have seen an info section. */
11286 if (ids_seen[DW_SECT_INFO_V5] == -1)
11287 {
11288 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11289 " DWO info/types section [in module %s]"),
11290 dwp_file->name);
11291 }
11292 /* Must have an abbrev section. */
11293 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11294 {
11295 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11296 " section [in module %s]"),
11297 dwp_file->name);
11298 }
11299 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11300 htab->section_pool.v5.sizes
11301 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11302 * nr_units * nr_columns);
11303 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11304 * nr_units * nr_columns))
11305 > index_end)
11306 {
11307 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11308 " [in module %s]"),
11309 dwp_file->name);
11310 }
11311 }
11312
11313 return htab;
11314 }
11315
11316 /* Update SECTIONS with the data from SECTP.
11317
11318 This function is like the other "locate" section routines, but in
11319 this context the sections to read comes from the DWP V1 hash table,
11320 not the full ELF section table.
11321
11322 The result is non-zero for success, or zero if an error was found. */
11323
11324 static int
11325 locate_v1_virtual_dwo_sections (asection *sectp,
11326 struct virtual_v1_dwo_sections *sections)
11327 {
11328 const struct dwop_section_names *names = &dwop_section_names;
11329
11330 if (names->abbrev_dwo.matches (sectp->name))
11331 {
11332 /* There can be only one. */
11333 if (sections->abbrev.s.section != NULL)
11334 return 0;
11335 sections->abbrev.s.section = sectp;
11336 sections->abbrev.size = bfd_section_size (sectp);
11337 }
11338 else if (names->info_dwo.matches (sectp->name)
11339 || names->types_dwo.matches (sectp->name))
11340 {
11341 /* There can be only one. */
11342 if (sections->info_or_types.s.section != NULL)
11343 return 0;
11344 sections->info_or_types.s.section = sectp;
11345 sections->info_or_types.size = bfd_section_size (sectp);
11346 }
11347 else if (names->line_dwo.matches (sectp->name))
11348 {
11349 /* There can be only one. */
11350 if (sections->line.s.section != NULL)
11351 return 0;
11352 sections->line.s.section = sectp;
11353 sections->line.size = bfd_section_size (sectp);
11354 }
11355 else if (names->loc_dwo.matches (sectp->name))
11356 {
11357 /* There can be only one. */
11358 if (sections->loc.s.section != NULL)
11359 return 0;
11360 sections->loc.s.section = sectp;
11361 sections->loc.size = bfd_section_size (sectp);
11362 }
11363 else if (names->macinfo_dwo.matches (sectp->name))
11364 {
11365 /* There can be only one. */
11366 if (sections->macinfo.s.section != NULL)
11367 return 0;
11368 sections->macinfo.s.section = sectp;
11369 sections->macinfo.size = bfd_section_size (sectp);
11370 }
11371 else if (names->macro_dwo.matches (sectp->name))
11372 {
11373 /* There can be only one. */
11374 if (sections->macro.s.section != NULL)
11375 return 0;
11376 sections->macro.s.section = sectp;
11377 sections->macro.size = bfd_section_size (sectp);
11378 }
11379 else if (names->str_offsets_dwo.matches (sectp->name))
11380 {
11381 /* There can be only one. */
11382 if (sections->str_offsets.s.section != NULL)
11383 return 0;
11384 sections->str_offsets.s.section = sectp;
11385 sections->str_offsets.size = bfd_section_size (sectp);
11386 }
11387 else
11388 {
11389 /* No other kind of section is valid. */
11390 return 0;
11391 }
11392
11393 return 1;
11394 }
11395
11396 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11397 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11398 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11399 This is for DWP version 1 files. */
11400
11401 static struct dwo_unit *
11402 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11403 struct dwp_file *dwp_file,
11404 uint32_t unit_index,
11405 const char *comp_dir,
11406 ULONGEST signature, int is_debug_types)
11407 {
11408 const struct dwp_hash_table *dwp_htab =
11409 is_debug_types ? dwp_file->tus : dwp_file->cus;
11410 bfd *dbfd = dwp_file->dbfd.get ();
11411 const char *kind = is_debug_types ? "TU" : "CU";
11412 struct dwo_file *dwo_file;
11413 struct dwo_unit *dwo_unit;
11414 struct virtual_v1_dwo_sections sections;
11415 void **dwo_file_slot;
11416 int i;
11417
11418 gdb_assert (dwp_file->version == 1);
11419
11420 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11421 kind, pulongest (unit_index), hex_string (signature),
11422 dwp_file->name);
11423
11424 /* Fetch the sections of this DWO unit.
11425 Put a limit on the number of sections we look for so that bad data
11426 doesn't cause us to loop forever. */
11427
11428 #define MAX_NR_V1_DWO_SECTIONS \
11429 (1 /* .debug_info or .debug_types */ \
11430 + 1 /* .debug_abbrev */ \
11431 + 1 /* .debug_line */ \
11432 + 1 /* .debug_loc */ \
11433 + 1 /* .debug_str_offsets */ \
11434 + 1 /* .debug_macro or .debug_macinfo */ \
11435 + 1 /* trailing zero */)
11436
11437 memset (&sections, 0, sizeof (sections));
11438
11439 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11440 {
11441 asection *sectp;
11442 uint32_t section_nr =
11443 read_4_bytes (dbfd,
11444 dwp_htab->section_pool.v1.indices
11445 + (unit_index + i) * sizeof (uint32_t));
11446
11447 if (section_nr == 0)
11448 break;
11449 if (section_nr >= dwp_file->num_sections)
11450 {
11451 error (_("Dwarf Error: bad DWP hash table, section number too large"
11452 " [in module %s]"),
11453 dwp_file->name);
11454 }
11455
11456 sectp = dwp_file->elf_sections[section_nr];
11457 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11458 {
11459 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11460 " [in module %s]"),
11461 dwp_file->name);
11462 }
11463 }
11464
11465 if (i < 2
11466 || sections.info_or_types.empty ()
11467 || sections.abbrev.empty ())
11468 {
11469 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11470 " [in module %s]"),
11471 dwp_file->name);
11472 }
11473 if (i == MAX_NR_V1_DWO_SECTIONS)
11474 {
11475 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11476 " [in module %s]"),
11477 dwp_file->name);
11478 }
11479
11480 /* It's easier for the rest of the code if we fake a struct dwo_file and
11481 have dwo_unit "live" in that. At least for now.
11482
11483 The DWP file can be made up of a random collection of CUs and TUs.
11484 However, for each CU + set of TUs that came from the same original DWO
11485 file, we can combine them back into a virtual DWO file to save space
11486 (fewer struct dwo_file objects to allocate). Remember that for really
11487 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11488
11489 std::string virtual_dwo_name =
11490 string_printf ("virtual-dwo/%d-%d-%d-%d",
11491 sections.abbrev.get_id (),
11492 sections.line.get_id (),
11493 sections.loc.get_id (),
11494 sections.str_offsets.get_id ());
11495 /* Can we use an existing virtual DWO file? */
11496 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11497 comp_dir);
11498 /* Create one if necessary. */
11499 if (*dwo_file_slot == NULL)
11500 {
11501 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11502 virtual_dwo_name.c_str ());
11503
11504 dwo_file = new struct dwo_file;
11505 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11506 dwo_file->comp_dir = comp_dir;
11507 dwo_file->sections.abbrev = sections.abbrev;
11508 dwo_file->sections.line = sections.line;
11509 dwo_file->sections.loc = sections.loc;
11510 dwo_file->sections.macinfo = sections.macinfo;
11511 dwo_file->sections.macro = sections.macro;
11512 dwo_file->sections.str_offsets = sections.str_offsets;
11513 /* The "str" section is global to the entire DWP file. */
11514 dwo_file->sections.str = dwp_file->sections.str;
11515 /* The info or types section is assigned below to dwo_unit,
11516 there's no need to record it in dwo_file.
11517 Also, we can't simply record type sections in dwo_file because
11518 we record a pointer into the vector in dwo_unit. As we collect more
11519 types we'll grow the vector and eventually have to reallocate space
11520 for it, invalidating all copies of pointers into the previous
11521 contents. */
11522 *dwo_file_slot = dwo_file;
11523 }
11524 else
11525 {
11526 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11527 virtual_dwo_name.c_str ());
11528
11529 dwo_file = (struct dwo_file *) *dwo_file_slot;
11530 }
11531
11532 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11533 dwo_unit->dwo_file = dwo_file;
11534 dwo_unit->signature = signature;
11535 dwo_unit->section =
11536 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11537 *dwo_unit->section = sections.info_or_types;
11538 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11539
11540 return dwo_unit;
11541 }
11542
11543 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11544 simplify them. Given a pointer to the containing section SECTION, and
11545 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11546 virtual section of just that piece. */
11547
11548 static struct dwarf2_section_info
11549 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11550 struct dwarf2_section_info *section,
11551 bfd_size_type offset, bfd_size_type size)
11552 {
11553 struct dwarf2_section_info result;
11554 asection *sectp;
11555
11556 gdb_assert (section != NULL);
11557 gdb_assert (!section->is_virtual);
11558
11559 memset (&result, 0, sizeof (result));
11560 result.s.containing_section = section;
11561 result.is_virtual = true;
11562
11563 if (size == 0)
11564 return result;
11565
11566 sectp = section->get_bfd_section ();
11567
11568 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11569 bounds of the real section. This is a pretty-rare event, so just
11570 flag an error (easier) instead of a warning and trying to cope. */
11571 if (sectp == NULL
11572 || offset + size > bfd_section_size (sectp))
11573 {
11574 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11575 " in section %s [in module %s]"),
11576 sectp ? bfd_section_name (sectp) : "<unknown>",
11577 objfile_name (per_objfile->objfile));
11578 }
11579
11580 result.virtual_offset = offset;
11581 result.size = size;
11582 return result;
11583 }
11584
11585 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11586 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11587 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11588 This is for DWP version 2 files. */
11589
11590 static struct dwo_unit *
11591 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11592 struct dwp_file *dwp_file,
11593 uint32_t unit_index,
11594 const char *comp_dir,
11595 ULONGEST signature, int is_debug_types)
11596 {
11597 const struct dwp_hash_table *dwp_htab =
11598 is_debug_types ? dwp_file->tus : dwp_file->cus;
11599 bfd *dbfd = dwp_file->dbfd.get ();
11600 const char *kind = is_debug_types ? "TU" : "CU";
11601 struct dwo_file *dwo_file;
11602 struct dwo_unit *dwo_unit;
11603 struct virtual_v2_or_v5_dwo_sections sections;
11604 void **dwo_file_slot;
11605 int i;
11606
11607 gdb_assert (dwp_file->version == 2);
11608
11609 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11610 kind, pulongest (unit_index), hex_string (signature),
11611 dwp_file->name);
11612
11613 /* Fetch the section offsets of this DWO unit. */
11614
11615 memset (&sections, 0, sizeof (sections));
11616
11617 for (i = 0; i < dwp_htab->nr_columns; ++i)
11618 {
11619 uint32_t offset = read_4_bytes (dbfd,
11620 dwp_htab->section_pool.v2.offsets
11621 + (((unit_index - 1) * dwp_htab->nr_columns
11622 + i)
11623 * sizeof (uint32_t)));
11624 uint32_t size = read_4_bytes (dbfd,
11625 dwp_htab->section_pool.v2.sizes
11626 + (((unit_index - 1) * dwp_htab->nr_columns
11627 + i)
11628 * sizeof (uint32_t)));
11629
11630 switch (dwp_htab->section_pool.v2.section_ids[i])
11631 {
11632 case DW_SECT_INFO:
11633 case DW_SECT_TYPES:
11634 sections.info_or_types_offset = offset;
11635 sections.info_or_types_size = size;
11636 break;
11637 case DW_SECT_ABBREV:
11638 sections.abbrev_offset = offset;
11639 sections.abbrev_size = size;
11640 break;
11641 case DW_SECT_LINE:
11642 sections.line_offset = offset;
11643 sections.line_size = size;
11644 break;
11645 case DW_SECT_LOC:
11646 sections.loc_offset = offset;
11647 sections.loc_size = size;
11648 break;
11649 case DW_SECT_STR_OFFSETS:
11650 sections.str_offsets_offset = offset;
11651 sections.str_offsets_size = size;
11652 break;
11653 case DW_SECT_MACINFO:
11654 sections.macinfo_offset = offset;
11655 sections.macinfo_size = size;
11656 break;
11657 case DW_SECT_MACRO:
11658 sections.macro_offset = offset;
11659 sections.macro_size = size;
11660 break;
11661 }
11662 }
11663
11664 /* It's easier for the rest of the code if we fake a struct dwo_file and
11665 have dwo_unit "live" in that. At least for now.
11666
11667 The DWP file can be made up of a random collection of CUs and TUs.
11668 However, for each CU + set of TUs that came from the same original DWO
11669 file, we can combine them back into a virtual DWO file to save space
11670 (fewer struct dwo_file objects to allocate). Remember that for really
11671 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11672
11673 std::string virtual_dwo_name =
11674 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11675 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11676 (long) (sections.line_size ? sections.line_offset : 0),
11677 (long) (sections.loc_size ? sections.loc_offset : 0),
11678 (long) (sections.str_offsets_size
11679 ? sections.str_offsets_offset : 0));
11680 /* Can we use an existing virtual DWO file? */
11681 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11682 comp_dir);
11683 /* Create one if necessary. */
11684 if (*dwo_file_slot == NULL)
11685 {
11686 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11687 virtual_dwo_name.c_str ());
11688
11689 dwo_file = new struct dwo_file;
11690 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11691 dwo_file->comp_dir = comp_dir;
11692 dwo_file->sections.abbrev =
11693 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11694 sections.abbrev_offset,
11695 sections.abbrev_size);
11696 dwo_file->sections.line =
11697 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11698 sections.line_offset,
11699 sections.line_size);
11700 dwo_file->sections.loc =
11701 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11702 sections.loc_offset, sections.loc_size);
11703 dwo_file->sections.macinfo =
11704 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11705 sections.macinfo_offset,
11706 sections.macinfo_size);
11707 dwo_file->sections.macro =
11708 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11709 sections.macro_offset,
11710 sections.macro_size);
11711 dwo_file->sections.str_offsets =
11712 create_dwp_v2_or_v5_section (per_objfile,
11713 &dwp_file->sections.str_offsets,
11714 sections.str_offsets_offset,
11715 sections.str_offsets_size);
11716 /* The "str" section is global to the entire DWP file. */
11717 dwo_file->sections.str = dwp_file->sections.str;
11718 /* The info or types section is assigned below to dwo_unit,
11719 there's no need to record it in dwo_file.
11720 Also, we can't simply record type sections in dwo_file because
11721 we record a pointer into the vector in dwo_unit. As we collect more
11722 types we'll grow the vector and eventually have to reallocate space
11723 for it, invalidating all copies of pointers into the previous
11724 contents. */
11725 *dwo_file_slot = dwo_file;
11726 }
11727 else
11728 {
11729 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11730 virtual_dwo_name.c_str ());
11731
11732 dwo_file = (struct dwo_file *) *dwo_file_slot;
11733 }
11734
11735 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11736 dwo_unit->dwo_file = dwo_file;
11737 dwo_unit->signature = signature;
11738 dwo_unit->section =
11739 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11740 *dwo_unit->section = create_dwp_v2_or_v5_section
11741 (per_objfile,
11742 is_debug_types
11743 ? &dwp_file->sections.types
11744 : &dwp_file->sections.info,
11745 sections.info_or_types_offset,
11746 sections.info_or_types_size);
11747 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11748
11749 return dwo_unit;
11750 }
11751
11752 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11753 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11754 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11755 This is for DWP version 5 files. */
11756
11757 static struct dwo_unit *
11758 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11759 struct dwp_file *dwp_file,
11760 uint32_t unit_index,
11761 const char *comp_dir,
11762 ULONGEST signature, int is_debug_types)
11763 {
11764 const struct dwp_hash_table *dwp_htab
11765 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11766 bfd *dbfd = dwp_file->dbfd.get ();
11767 const char *kind = is_debug_types ? "TU" : "CU";
11768 struct dwo_file *dwo_file;
11769 struct dwo_unit *dwo_unit;
11770 struct virtual_v2_or_v5_dwo_sections sections {};
11771 void **dwo_file_slot;
11772
11773 gdb_assert (dwp_file->version == 5);
11774
11775 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11776 kind, pulongest (unit_index), hex_string (signature),
11777 dwp_file->name);
11778
11779 /* Fetch the section offsets of this DWO unit. */
11780
11781 /* memset (&sections, 0, sizeof (sections)); */
11782
11783 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11784 {
11785 uint32_t offset = read_4_bytes (dbfd,
11786 dwp_htab->section_pool.v5.offsets
11787 + (((unit_index - 1)
11788 * dwp_htab->nr_columns
11789 + i)
11790 * sizeof (uint32_t)));
11791 uint32_t size = read_4_bytes (dbfd,
11792 dwp_htab->section_pool.v5.sizes
11793 + (((unit_index - 1) * dwp_htab->nr_columns
11794 + i)
11795 * sizeof (uint32_t)));
11796
11797 switch (dwp_htab->section_pool.v5.section_ids[i])
11798 {
11799 case DW_SECT_ABBREV_V5:
11800 sections.abbrev_offset = offset;
11801 sections.abbrev_size = size;
11802 break;
11803 case DW_SECT_INFO_V5:
11804 sections.info_or_types_offset = offset;
11805 sections.info_or_types_size = size;
11806 break;
11807 case DW_SECT_LINE_V5:
11808 sections.line_offset = offset;
11809 sections.line_size = size;
11810 break;
11811 case DW_SECT_LOCLISTS_V5:
11812 sections.loclists_offset = offset;
11813 sections.loclists_size = size;
11814 break;
11815 case DW_SECT_MACRO_V5:
11816 sections.macro_offset = offset;
11817 sections.macro_size = size;
11818 break;
11819 case DW_SECT_RNGLISTS_V5:
11820 sections.rnglists_offset = offset;
11821 sections.rnglists_size = size;
11822 break;
11823 case DW_SECT_STR_OFFSETS_V5:
11824 sections.str_offsets_offset = offset;
11825 sections.str_offsets_size = size;
11826 break;
11827 case DW_SECT_RESERVED_V5:
11828 default:
11829 break;
11830 }
11831 }
11832
11833 /* It's easier for the rest of the code if we fake a struct dwo_file and
11834 have dwo_unit "live" in that. At least for now.
11835
11836 The DWP file can be made up of a random collection of CUs and TUs.
11837 However, for each CU + set of TUs that came from the same original DWO
11838 file, we can combine them back into a virtual DWO file to save space
11839 (fewer struct dwo_file objects to allocate). Remember that for really
11840 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11841
11842 std::string virtual_dwo_name =
11843 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11844 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11845 (long) (sections.line_size ? sections.line_offset : 0),
11846 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11847 (long) (sections.str_offsets_size
11848 ? sections.str_offsets_offset : 0),
11849 (long) (sections.macro_size ? sections.macro_offset : 0),
11850 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11851 /* Can we use an existing virtual DWO file? */
11852 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11853 virtual_dwo_name.c_str (),
11854 comp_dir);
11855 /* Create one if necessary. */
11856 if (*dwo_file_slot == NULL)
11857 {
11858 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11859 virtual_dwo_name.c_str ());
11860
11861 dwo_file = new struct dwo_file;
11862 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11863 dwo_file->comp_dir = comp_dir;
11864 dwo_file->sections.abbrev =
11865 create_dwp_v2_or_v5_section (per_objfile,
11866 &dwp_file->sections.abbrev,
11867 sections.abbrev_offset,
11868 sections.abbrev_size);
11869 dwo_file->sections.line =
11870 create_dwp_v2_or_v5_section (per_objfile,
11871 &dwp_file->sections.line,
11872 sections.line_offset, sections.line_size);
11873 dwo_file->sections.macro =
11874 create_dwp_v2_or_v5_section (per_objfile,
11875 &dwp_file->sections.macro,
11876 sections.macro_offset,
11877 sections.macro_size);
11878 dwo_file->sections.loclists =
11879 create_dwp_v2_or_v5_section (per_objfile,
11880 &dwp_file->sections.loclists,
11881 sections.loclists_offset,
11882 sections.loclists_size);
11883 dwo_file->sections.rnglists =
11884 create_dwp_v2_or_v5_section (per_objfile,
11885 &dwp_file->sections.rnglists,
11886 sections.rnglists_offset,
11887 sections.rnglists_size);
11888 dwo_file->sections.str_offsets =
11889 create_dwp_v2_or_v5_section (per_objfile,
11890 &dwp_file->sections.str_offsets,
11891 sections.str_offsets_offset,
11892 sections.str_offsets_size);
11893 /* The "str" section is global to the entire DWP file. */
11894 dwo_file->sections.str = dwp_file->sections.str;
11895 /* The info or types section is assigned below to dwo_unit,
11896 there's no need to record it in dwo_file.
11897 Also, we can't simply record type sections in dwo_file because
11898 we record a pointer into the vector in dwo_unit. As we collect more
11899 types we'll grow the vector and eventually have to reallocate space
11900 for it, invalidating all copies of pointers into the previous
11901 contents. */
11902 *dwo_file_slot = dwo_file;
11903 }
11904 else
11905 {
11906 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11907 virtual_dwo_name.c_str ());
11908
11909 dwo_file = (struct dwo_file *) *dwo_file_slot;
11910 }
11911
11912 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11913 dwo_unit->dwo_file = dwo_file;
11914 dwo_unit->signature = signature;
11915 dwo_unit->section
11916 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11917 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
11918 &dwp_file->sections.info,
11919 sections.info_or_types_offset,
11920 sections.info_or_types_size);
11921 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11922
11923 return dwo_unit;
11924 }
11925
11926 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11927 Returns NULL if the signature isn't found. */
11928
11929 static struct dwo_unit *
11930 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
11931 struct dwp_file *dwp_file, const char *comp_dir,
11932 ULONGEST signature, int is_debug_types)
11933 {
11934 const struct dwp_hash_table *dwp_htab =
11935 is_debug_types ? dwp_file->tus : dwp_file->cus;
11936 bfd *dbfd = dwp_file->dbfd.get ();
11937 uint32_t mask = dwp_htab->nr_slots - 1;
11938 uint32_t hash = signature & mask;
11939 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11940 unsigned int i;
11941 void **slot;
11942 struct dwo_unit find_dwo_cu;
11943
11944 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11945 find_dwo_cu.signature = signature;
11946 slot = htab_find_slot (is_debug_types
11947 ? dwp_file->loaded_tus.get ()
11948 : dwp_file->loaded_cus.get (),
11949 &find_dwo_cu, INSERT);
11950
11951 if (*slot != NULL)
11952 return (struct dwo_unit *) *slot;
11953
11954 /* Use a for loop so that we don't loop forever on bad debug info. */
11955 for (i = 0; i < dwp_htab->nr_slots; ++i)
11956 {
11957 ULONGEST signature_in_table;
11958
11959 signature_in_table =
11960 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11961 if (signature_in_table == signature)
11962 {
11963 uint32_t unit_index =
11964 read_4_bytes (dbfd,
11965 dwp_htab->unit_table + hash * sizeof (uint32_t));
11966
11967 if (dwp_file->version == 1)
11968 {
11969 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11970 unit_index, comp_dir,
11971 signature, is_debug_types);
11972 }
11973 else if (dwp_file->version == 2)
11974 {
11975 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11976 unit_index, comp_dir,
11977 signature, is_debug_types);
11978 }
11979 else /* version == 5 */
11980 {
11981 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11982 unit_index, comp_dir,
11983 signature, is_debug_types);
11984 }
11985 return (struct dwo_unit *) *slot;
11986 }
11987 if (signature_in_table == 0)
11988 return NULL;
11989 hash = (hash + hash2) & mask;
11990 }
11991
11992 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11993 " [in module %s]"),
11994 dwp_file->name);
11995 }
11996
11997 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11998 Open the file specified by FILE_NAME and hand it off to BFD for
11999 preliminary analysis. Return a newly initialized bfd *, which
12000 includes a canonicalized copy of FILE_NAME.
12001 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12002 SEARCH_CWD is true if the current directory is to be searched.
12003 It will be searched before debug-file-directory.
12004 If successful, the file is added to the bfd include table of the
12005 objfile's bfd (see gdb_bfd_record_inclusion).
12006 If unable to find/open the file, return NULL.
12007 NOTE: This function is derived from symfile_bfd_open. */
12008
12009 static gdb_bfd_ref_ptr
12010 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12011 const char *file_name, int is_dwp, int search_cwd)
12012 {
12013 int desc;
12014 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12015 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12016 to debug_file_directory. */
12017 const char *search_path;
12018 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12019
12020 gdb::unique_xmalloc_ptr<char> search_path_holder;
12021 if (search_cwd)
12022 {
12023 if (*debug_file_directory != '\0')
12024 {
12025 search_path_holder.reset (concat (".", dirname_separator_string,
12026 debug_file_directory,
12027 (char *) NULL));
12028 search_path = search_path_holder.get ();
12029 }
12030 else
12031 search_path = ".";
12032 }
12033 else
12034 search_path = debug_file_directory;
12035
12036 /* Add the path for the executable binary to the list of search paths. */
12037 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12038 search_path_holder.reset (concat (objfile_dir.c_str (),
12039 dirname_separator_string,
12040 search_path, nullptr));
12041 search_path = search_path_holder.get ();
12042
12043 openp_flags flags = OPF_RETURN_REALPATH;
12044 if (is_dwp)
12045 flags |= OPF_SEARCH_IN_PATH;
12046
12047 gdb::unique_xmalloc_ptr<char> absolute_name;
12048 desc = openp (search_path, flags, file_name,
12049 O_RDONLY | O_BINARY, &absolute_name);
12050 if (desc < 0)
12051 return NULL;
12052
12053 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12054 gnutarget, desc));
12055 if (sym_bfd == NULL)
12056 return NULL;
12057 bfd_set_cacheable (sym_bfd.get (), 1);
12058
12059 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12060 return NULL;
12061
12062 /* Success. Record the bfd as having been included by the objfile's bfd.
12063 This is important because things like demangled_names_hash lives in the
12064 objfile's per_bfd space and may have references to things like symbol
12065 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12066 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12067
12068 return sym_bfd;
12069 }
12070
12071 /* Try to open DWO file FILE_NAME.
12072 COMP_DIR is the DW_AT_comp_dir attribute.
12073 The result is the bfd handle of the file.
12074 If there is a problem finding or opening the file, return NULL.
12075 Upon success, the canonicalized path of the file is stored in the bfd,
12076 same as symfile_bfd_open. */
12077
12078 static gdb_bfd_ref_ptr
12079 open_dwo_file (dwarf2_per_objfile *per_objfile,
12080 const char *file_name, const char *comp_dir)
12081 {
12082 if (IS_ABSOLUTE_PATH (file_name))
12083 return try_open_dwop_file (per_objfile, file_name,
12084 0 /*is_dwp*/, 0 /*search_cwd*/);
12085
12086 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12087
12088 if (comp_dir != NULL)
12089 {
12090 gdb::unique_xmalloc_ptr<char> path_to_try
12091 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12092
12093 /* NOTE: If comp_dir is a relative path, this will also try the
12094 search path, which seems useful. */
12095 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12096 0 /*is_dwp*/,
12097 1 /*search_cwd*/));
12098 if (abfd != NULL)
12099 return abfd;
12100 }
12101
12102 /* That didn't work, try debug-file-directory, which, despite its name,
12103 is a list of paths. */
12104
12105 if (*debug_file_directory == '\0')
12106 return NULL;
12107
12108 return try_open_dwop_file (per_objfile, file_name,
12109 0 /*is_dwp*/, 1 /*search_cwd*/);
12110 }
12111
12112 /* This function is mapped across the sections and remembers the offset and
12113 size of each of the DWO debugging sections we are interested in. */
12114
12115 static void
12116 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12117 dwo_sections *dwo_sections)
12118 {
12119 const struct dwop_section_names *names = &dwop_section_names;
12120
12121 if (names->abbrev_dwo.matches (sectp->name))
12122 {
12123 dwo_sections->abbrev.s.section = sectp;
12124 dwo_sections->abbrev.size = bfd_section_size (sectp);
12125 }
12126 else if (names->info_dwo.matches (sectp->name))
12127 {
12128 dwo_sections->info.s.section = sectp;
12129 dwo_sections->info.size = bfd_section_size (sectp);
12130 }
12131 else if (names->line_dwo.matches (sectp->name))
12132 {
12133 dwo_sections->line.s.section = sectp;
12134 dwo_sections->line.size = bfd_section_size (sectp);
12135 }
12136 else if (names->loc_dwo.matches (sectp->name))
12137 {
12138 dwo_sections->loc.s.section = sectp;
12139 dwo_sections->loc.size = bfd_section_size (sectp);
12140 }
12141 else if (names->loclists_dwo.matches (sectp->name))
12142 {
12143 dwo_sections->loclists.s.section = sectp;
12144 dwo_sections->loclists.size = bfd_section_size (sectp);
12145 }
12146 else if (names->macinfo_dwo.matches (sectp->name))
12147 {
12148 dwo_sections->macinfo.s.section = sectp;
12149 dwo_sections->macinfo.size = bfd_section_size (sectp);
12150 }
12151 else if (names->macro_dwo.matches (sectp->name))
12152 {
12153 dwo_sections->macro.s.section = sectp;
12154 dwo_sections->macro.size = bfd_section_size (sectp);
12155 }
12156 else if (names->rnglists_dwo.matches (sectp->name))
12157 {
12158 dwo_sections->rnglists.s.section = sectp;
12159 dwo_sections->rnglists.size = bfd_section_size (sectp);
12160 }
12161 else if (names->str_dwo.matches (sectp->name))
12162 {
12163 dwo_sections->str.s.section = sectp;
12164 dwo_sections->str.size = bfd_section_size (sectp);
12165 }
12166 else if (names->str_offsets_dwo.matches (sectp->name))
12167 {
12168 dwo_sections->str_offsets.s.section = sectp;
12169 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12170 }
12171 else if (names->types_dwo.matches (sectp->name))
12172 {
12173 struct dwarf2_section_info type_section;
12174
12175 memset (&type_section, 0, sizeof (type_section));
12176 type_section.s.section = sectp;
12177 type_section.size = bfd_section_size (sectp);
12178 dwo_sections->types.push_back (type_section);
12179 }
12180 }
12181
12182 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12183 by PER_CU. This is for the non-DWP case.
12184 The result is NULL if DWO_NAME can't be found. */
12185
12186 static struct dwo_file *
12187 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12188 const char *comp_dir)
12189 {
12190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12191
12192 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12193 if (dbfd == NULL)
12194 {
12195 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12196
12197 return NULL;
12198 }
12199
12200 dwo_file_up dwo_file (new struct dwo_file);
12201 dwo_file->dwo_name = dwo_name;
12202 dwo_file->comp_dir = comp_dir;
12203 dwo_file->dbfd = std::move (dbfd);
12204
12205 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12206 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12207 &dwo_file->sections);
12208
12209 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12210 dwo_file->cus);
12211
12212 if (cu->per_cu->dwarf_version < 5)
12213 {
12214 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12215 dwo_file->sections.types, dwo_file->tus);
12216 }
12217 else
12218 {
12219 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12220 &dwo_file->sections.info, dwo_file->tus,
12221 rcuh_kind::COMPILE);
12222 }
12223
12224 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12225
12226 return dwo_file.release ();
12227 }
12228
12229 /* This function is mapped across the sections and remembers the offset and
12230 size of each of the DWP debugging sections common to version 1 and 2 that
12231 we are interested in. */
12232
12233 static void
12234 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12235 dwp_file *dwp_file)
12236 {
12237 const struct dwop_section_names *names = &dwop_section_names;
12238 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12239
12240 /* Record the ELF section number for later lookup: this is what the
12241 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12242 gdb_assert (elf_section_nr < dwp_file->num_sections);
12243 dwp_file->elf_sections[elf_section_nr] = sectp;
12244
12245 /* Look for specific sections that we need. */
12246 if (names->str_dwo.matches (sectp->name))
12247 {
12248 dwp_file->sections.str.s.section = sectp;
12249 dwp_file->sections.str.size = bfd_section_size (sectp);
12250 }
12251 else if (names->cu_index.matches (sectp->name))
12252 {
12253 dwp_file->sections.cu_index.s.section = sectp;
12254 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12255 }
12256 else if (names->tu_index.matches (sectp->name))
12257 {
12258 dwp_file->sections.tu_index.s.section = sectp;
12259 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12260 }
12261 }
12262
12263 /* This function is mapped across the sections and remembers the offset and
12264 size of each of the DWP version 2 debugging sections that we are interested
12265 in. This is split into a separate function because we don't know if we
12266 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12267
12268 static void
12269 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12270 {
12271 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12272 const struct dwop_section_names *names = &dwop_section_names;
12273 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12274
12275 /* Record the ELF section number for later lookup: this is what the
12276 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12277 gdb_assert (elf_section_nr < dwp_file->num_sections);
12278 dwp_file->elf_sections[elf_section_nr] = sectp;
12279
12280 /* Look for specific sections that we need. */
12281 if (names->abbrev_dwo.matches (sectp->name))
12282 {
12283 dwp_file->sections.abbrev.s.section = sectp;
12284 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12285 }
12286 else if (names->info_dwo.matches (sectp->name))
12287 {
12288 dwp_file->sections.info.s.section = sectp;
12289 dwp_file->sections.info.size = bfd_section_size (sectp);
12290 }
12291 else if (names->line_dwo.matches (sectp->name))
12292 {
12293 dwp_file->sections.line.s.section = sectp;
12294 dwp_file->sections.line.size = bfd_section_size (sectp);
12295 }
12296 else if (names->loc_dwo.matches (sectp->name))
12297 {
12298 dwp_file->sections.loc.s.section = sectp;
12299 dwp_file->sections.loc.size = bfd_section_size (sectp);
12300 }
12301 else if (names->macinfo_dwo.matches (sectp->name))
12302 {
12303 dwp_file->sections.macinfo.s.section = sectp;
12304 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12305 }
12306 else if (names->macro_dwo.matches (sectp->name))
12307 {
12308 dwp_file->sections.macro.s.section = sectp;
12309 dwp_file->sections.macro.size = bfd_section_size (sectp);
12310 }
12311 else if (names->str_offsets_dwo.matches (sectp->name))
12312 {
12313 dwp_file->sections.str_offsets.s.section = sectp;
12314 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12315 }
12316 else if (names->types_dwo.matches (sectp->name))
12317 {
12318 dwp_file->sections.types.s.section = sectp;
12319 dwp_file->sections.types.size = bfd_section_size (sectp);
12320 }
12321 }
12322
12323 /* This function is mapped across the sections and remembers the offset and
12324 size of each of the DWP version 5 debugging sections that we are interested
12325 in. This is split into a separate function because we don't know if we
12326 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12327
12328 static void
12329 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12330 {
12331 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12332 const struct dwop_section_names *names = &dwop_section_names;
12333 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12334
12335 /* Record the ELF section number for later lookup: this is what the
12336 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12337 gdb_assert (elf_section_nr < dwp_file->num_sections);
12338 dwp_file->elf_sections[elf_section_nr] = sectp;
12339
12340 /* Look for specific sections that we need. */
12341 if (names->abbrev_dwo.matches (sectp->name))
12342 {
12343 dwp_file->sections.abbrev.s.section = sectp;
12344 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12345 }
12346 else if (names->info_dwo.matches (sectp->name))
12347 {
12348 dwp_file->sections.info.s.section = sectp;
12349 dwp_file->sections.info.size = bfd_section_size (sectp);
12350 }
12351 else if (names->line_dwo.matches (sectp->name))
12352 {
12353 dwp_file->sections.line.s.section = sectp;
12354 dwp_file->sections.line.size = bfd_section_size (sectp);
12355 }
12356 else if (names->loclists_dwo.matches (sectp->name))
12357 {
12358 dwp_file->sections.loclists.s.section = sectp;
12359 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12360 }
12361 else if (names->macro_dwo.matches (sectp->name))
12362 {
12363 dwp_file->sections.macro.s.section = sectp;
12364 dwp_file->sections.macro.size = bfd_section_size (sectp);
12365 }
12366 else if (names->rnglists_dwo.matches (sectp->name))
12367 {
12368 dwp_file->sections.rnglists.s.section = sectp;
12369 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12370 }
12371 else if (names->str_offsets_dwo.matches (sectp->name))
12372 {
12373 dwp_file->sections.str_offsets.s.section = sectp;
12374 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12375 }
12376 }
12377
12378 /* Hash function for dwp_file loaded CUs/TUs. */
12379
12380 static hashval_t
12381 hash_dwp_loaded_cutus (const void *item)
12382 {
12383 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12384
12385 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12386 return dwo_unit->signature;
12387 }
12388
12389 /* Equality function for dwp_file loaded CUs/TUs. */
12390
12391 static int
12392 eq_dwp_loaded_cutus (const void *a, const void *b)
12393 {
12394 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12395 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12396
12397 return dua->signature == dub->signature;
12398 }
12399
12400 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12401
12402 static htab_up
12403 allocate_dwp_loaded_cutus_table ()
12404 {
12405 return htab_up (htab_create_alloc (3,
12406 hash_dwp_loaded_cutus,
12407 eq_dwp_loaded_cutus,
12408 NULL, xcalloc, xfree));
12409 }
12410
12411 /* Try to open DWP file FILE_NAME.
12412 The result is the bfd handle of the file.
12413 If there is a problem finding or opening the file, return NULL.
12414 Upon success, the canonicalized path of the file is stored in the bfd,
12415 same as symfile_bfd_open. */
12416
12417 static gdb_bfd_ref_ptr
12418 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12419 {
12420 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12421 1 /*is_dwp*/,
12422 1 /*search_cwd*/));
12423 if (abfd != NULL)
12424 return abfd;
12425
12426 /* Work around upstream bug 15652.
12427 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12428 [Whether that's a "bug" is debatable, but it is getting in our way.]
12429 We have no real idea where the dwp file is, because gdb's realpath-ing
12430 of the executable's path may have discarded the needed info.
12431 [IWBN if the dwp file name was recorded in the executable, akin to
12432 .gnu_debuglink, but that doesn't exist yet.]
12433 Strip the directory from FILE_NAME and search again. */
12434 if (*debug_file_directory != '\0')
12435 {
12436 /* Don't implicitly search the current directory here.
12437 If the user wants to search "." to handle this case,
12438 it must be added to debug-file-directory. */
12439 return try_open_dwop_file (per_objfile, lbasename (file_name),
12440 1 /*is_dwp*/,
12441 0 /*search_cwd*/);
12442 }
12443
12444 return NULL;
12445 }
12446
12447 /* Initialize the use of the DWP file for the current objfile.
12448 By convention the name of the DWP file is ${objfile}.dwp.
12449 The result is NULL if it can't be found. */
12450
12451 static std::unique_ptr<struct dwp_file>
12452 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12453 {
12454 struct objfile *objfile = per_objfile->objfile;
12455
12456 /* Try to find first .dwp for the binary file before any symbolic links
12457 resolving. */
12458
12459 /* If the objfile is a debug file, find the name of the real binary
12460 file and get the name of dwp file from there. */
12461 std::string dwp_name;
12462 if (objfile->separate_debug_objfile_backlink != NULL)
12463 {
12464 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12465 const char *backlink_basename = lbasename (backlink->original_name);
12466
12467 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12468 }
12469 else
12470 dwp_name = objfile->original_name;
12471
12472 dwp_name += ".dwp";
12473
12474 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12475 if (dbfd == NULL
12476 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12477 {
12478 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12479 dwp_name = objfile_name (objfile);
12480 dwp_name += ".dwp";
12481 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12482 }
12483
12484 if (dbfd == NULL)
12485 {
12486 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12487
12488 return std::unique_ptr<dwp_file> ();
12489 }
12490
12491 const char *name = bfd_get_filename (dbfd.get ());
12492 std::unique_ptr<struct dwp_file> dwp_file
12493 (new struct dwp_file (name, std::move (dbfd)));
12494
12495 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12496 dwp_file->elf_sections =
12497 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12498 dwp_file->num_sections, asection *);
12499
12500 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12501 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12502 dwp_file.get ());
12503
12504 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12505
12506 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12507
12508 /* The DWP file version is stored in the hash table. Oh well. */
12509 if (dwp_file->cus && dwp_file->tus
12510 && dwp_file->cus->version != dwp_file->tus->version)
12511 {
12512 /* Technically speaking, we should try to limp along, but this is
12513 pretty bizarre. We use pulongest here because that's the established
12514 portability solution (e.g, we cannot use %u for uint32_t). */
12515 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12516 " TU version %s [in DWP file %s]"),
12517 pulongest (dwp_file->cus->version),
12518 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12519 }
12520
12521 if (dwp_file->cus)
12522 dwp_file->version = dwp_file->cus->version;
12523 else if (dwp_file->tus)
12524 dwp_file->version = dwp_file->tus->version;
12525 else
12526 dwp_file->version = 2;
12527
12528 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12529 {
12530 if (dwp_file->version == 2)
12531 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12532 dwp_file.get ());
12533 else
12534 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12535 dwp_file.get ());
12536 }
12537
12538 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12539 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12540
12541 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12542 dwarf_read_debug_printf (" %s CUs, %s TUs",
12543 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12544 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12545
12546 return dwp_file;
12547 }
12548
12549 /* Wrapper around open_and_init_dwp_file, only open it once. */
12550
12551 static struct dwp_file *
12552 get_dwp_file (dwarf2_per_objfile *per_objfile)
12553 {
12554 if (!per_objfile->per_bfd->dwp_checked)
12555 {
12556 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12557 per_objfile->per_bfd->dwp_checked = 1;
12558 }
12559 return per_objfile->per_bfd->dwp_file.get ();
12560 }
12561
12562 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12563 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12564 or in the DWP file for the objfile, referenced by THIS_UNIT.
12565 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12566 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12567
12568 This is called, for example, when wanting to read a variable with a
12569 complex location. Therefore we don't want to do file i/o for every call.
12570 Therefore we don't want to look for a DWO file on every call.
12571 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12572 then we check if we've already seen DWO_NAME, and only THEN do we check
12573 for a DWO file.
12574
12575 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12576 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12577
12578 static struct dwo_unit *
12579 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12580 ULONGEST signature, int is_debug_types)
12581 {
12582 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12583 struct objfile *objfile = per_objfile->objfile;
12584 const char *kind = is_debug_types ? "TU" : "CU";
12585 void **dwo_file_slot;
12586 struct dwo_file *dwo_file;
12587 struct dwp_file *dwp_file;
12588
12589 /* First see if there's a DWP file.
12590 If we have a DWP file but didn't find the DWO inside it, don't
12591 look for the original DWO file. It makes gdb behave differently
12592 depending on whether one is debugging in the build tree. */
12593
12594 dwp_file = get_dwp_file (per_objfile);
12595 if (dwp_file != NULL)
12596 {
12597 const struct dwp_hash_table *dwp_htab =
12598 is_debug_types ? dwp_file->tus : dwp_file->cus;
12599
12600 if (dwp_htab != NULL)
12601 {
12602 struct dwo_unit *dwo_cutu =
12603 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12604 is_debug_types);
12605
12606 if (dwo_cutu != NULL)
12607 {
12608 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12609 kind, hex_string (signature),
12610 host_address_to_string (dwo_cutu));
12611
12612 return dwo_cutu;
12613 }
12614 }
12615 }
12616 else
12617 {
12618 /* No DWP file, look for the DWO file. */
12619
12620 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12621 if (*dwo_file_slot == NULL)
12622 {
12623 /* Read in the file and build a table of the CUs/TUs it contains. */
12624 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12625 }
12626 /* NOTE: This will be NULL if unable to open the file. */
12627 dwo_file = (struct dwo_file *) *dwo_file_slot;
12628
12629 if (dwo_file != NULL)
12630 {
12631 struct dwo_unit *dwo_cutu = NULL;
12632
12633 if (is_debug_types && dwo_file->tus)
12634 {
12635 struct dwo_unit find_dwo_cutu;
12636
12637 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12638 find_dwo_cutu.signature = signature;
12639 dwo_cutu
12640 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12641 &find_dwo_cutu);
12642 }
12643 else if (!is_debug_types && dwo_file->cus)
12644 {
12645 struct dwo_unit find_dwo_cutu;
12646
12647 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12648 find_dwo_cutu.signature = signature;
12649 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12650 &find_dwo_cutu);
12651 }
12652
12653 if (dwo_cutu != NULL)
12654 {
12655 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12656 kind, dwo_name, hex_string (signature),
12657 host_address_to_string (dwo_cutu));
12658
12659 return dwo_cutu;
12660 }
12661 }
12662 }
12663
12664 /* We didn't find it. This could mean a dwo_id mismatch, or
12665 someone deleted the DWO/DWP file, or the search path isn't set up
12666 correctly to find the file. */
12667
12668 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12669 kind, dwo_name, hex_string (signature));
12670
12671 /* This is a warning and not a complaint because it can be caused by
12672 pilot error (e.g., user accidentally deleting the DWO). */
12673 {
12674 /* Print the name of the DWP file if we looked there, helps the user
12675 better diagnose the problem. */
12676 std::string dwp_text;
12677
12678 if (dwp_file != NULL)
12679 dwp_text = string_printf (" [in DWP file %s]",
12680 lbasename (dwp_file->name));
12681
12682 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12683 " [in module %s]"),
12684 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12685 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12686 }
12687 return NULL;
12688 }
12689
12690 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12691 See lookup_dwo_cutu_unit for details. */
12692
12693 static struct dwo_unit *
12694 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12695 ULONGEST signature)
12696 {
12697 gdb_assert (!cu->per_cu->is_debug_types);
12698
12699 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12700 }
12701
12702 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12703 See lookup_dwo_cutu_unit for details. */
12704
12705 static struct dwo_unit *
12706 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12707 {
12708 gdb_assert (cu->per_cu->is_debug_types);
12709
12710 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12711
12712 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12713 }
12714
12715 /* Traversal function for queue_and_load_all_dwo_tus. */
12716
12717 static int
12718 queue_and_load_dwo_tu (void **slot, void *info)
12719 {
12720 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12721 dwarf2_cu *cu = (dwarf2_cu *) info;
12722 ULONGEST signature = dwo_unit->signature;
12723 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12724
12725 if (sig_type != NULL)
12726 {
12727 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12728 a real dependency of PER_CU on SIG_TYPE. That is detected later
12729 while processing PER_CU. */
12730 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12731 cu->language))
12732 load_full_type_unit (sig_type, cu->per_objfile);
12733 cu->per_cu->imported_symtabs_push (sig_type);
12734 }
12735
12736 return 1;
12737 }
12738
12739 /* Queue all TUs contained in the DWO of CU to be read in.
12740 The DWO may have the only definition of the type, though it may not be
12741 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12742 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12743
12744 static void
12745 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12746 {
12747 struct dwo_unit *dwo_unit;
12748 struct dwo_file *dwo_file;
12749
12750 gdb_assert (cu != nullptr);
12751 gdb_assert (!cu->per_cu->is_debug_types);
12752 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12753
12754 dwo_unit = cu->dwo_unit;
12755 gdb_assert (dwo_unit != NULL);
12756
12757 dwo_file = dwo_unit->dwo_file;
12758 if (dwo_file->tus != NULL)
12759 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12760 }
12761
12762 /* Read in various DIEs. */
12763
12764 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12765 Inherit only the children of the DW_AT_abstract_origin DIE not being
12766 already referenced by DW_AT_abstract_origin from the children of the
12767 current DIE. */
12768
12769 static void
12770 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12771 {
12772 struct die_info *child_die;
12773 sect_offset *offsetp;
12774 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12775 struct die_info *origin_die;
12776 /* Iterator of the ORIGIN_DIE children. */
12777 struct die_info *origin_child_die;
12778 struct attribute *attr;
12779 struct dwarf2_cu *origin_cu;
12780 struct pending **origin_previous_list_in_scope;
12781
12782 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12783 if (!attr)
12784 return;
12785
12786 /* Note that following die references may follow to a die in a
12787 different cu. */
12788
12789 origin_cu = cu;
12790 origin_die = follow_die_ref (die, attr, &origin_cu);
12791
12792 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12793 symbols in. */
12794 origin_previous_list_in_scope = origin_cu->list_in_scope;
12795 origin_cu->list_in_scope = cu->list_in_scope;
12796
12797 if (die->tag != origin_die->tag
12798 && !(die->tag == DW_TAG_inlined_subroutine
12799 && origin_die->tag == DW_TAG_subprogram))
12800 complaint (_("DIE %s and its abstract origin %s have different tags"),
12801 sect_offset_str (die->sect_off),
12802 sect_offset_str (origin_die->sect_off));
12803
12804 /* Find if the concrete and abstract trees are structurally the
12805 same. This is a shallow traversal and it is not bullet-proof;
12806 the compiler can trick the debugger into believing that the trees
12807 are isomorphic, whereas they actually are not. However, the
12808 likelyhood of this happening is pretty low, and a full-fledged
12809 check would be an overkill. */
12810 bool are_isomorphic = true;
12811 die_info *concrete_child = die->child;
12812 die_info *abstract_child = origin_die->child;
12813 while (concrete_child != nullptr || abstract_child != nullptr)
12814 {
12815 if (concrete_child == nullptr
12816 || abstract_child == nullptr
12817 || concrete_child->tag != abstract_child->tag)
12818 {
12819 are_isomorphic = false;
12820 break;
12821 }
12822
12823 concrete_child = concrete_child->sibling;
12824 abstract_child = abstract_child->sibling;
12825 }
12826
12827 /* Walk the origin's children in parallel to the concrete children.
12828 This helps match an origin child in case the debug info misses
12829 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12830 origin tree may not have the same tree structure as the concrete
12831 DIE, though. */
12832 die_info *corresponding_abstract_child
12833 = are_isomorphic ? origin_die->child : nullptr;
12834
12835 std::vector<sect_offset> offsets;
12836
12837 for (child_die = die->child;
12838 child_die && child_die->tag;
12839 child_die = child_die->sibling)
12840 {
12841 struct die_info *child_origin_die;
12842 struct dwarf2_cu *child_origin_cu;
12843
12844 /* We are trying to process concrete instance entries:
12845 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12846 it's not relevant to our analysis here. i.e. detecting DIEs that are
12847 present in the abstract instance but not referenced in the concrete
12848 one. */
12849 if (child_die->tag == DW_TAG_call_site
12850 || child_die->tag == DW_TAG_GNU_call_site)
12851 {
12852 if (are_isomorphic)
12853 corresponding_abstract_child
12854 = corresponding_abstract_child->sibling;
12855 continue;
12856 }
12857
12858 /* For each CHILD_DIE, find the corresponding child of
12859 ORIGIN_DIE. If there is more than one layer of
12860 DW_AT_abstract_origin, follow them all; there shouldn't be,
12861 but GCC versions at least through 4.4 generate this (GCC PR
12862 40573). */
12863 child_origin_die = child_die;
12864 child_origin_cu = cu;
12865 while (1)
12866 {
12867 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12868 child_origin_cu);
12869 if (attr == NULL)
12870 break;
12871 child_origin_die = follow_die_ref (child_origin_die, attr,
12872 &child_origin_cu);
12873 }
12874
12875 /* If missing DW_AT_abstract_origin, try the corresponding child
12876 of the origin. Clang emits such lexical scopes. */
12877 if (child_origin_die == child_die
12878 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12879 && are_isomorphic
12880 && child_die->tag == DW_TAG_lexical_block)
12881 child_origin_die = corresponding_abstract_child;
12882
12883 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12884 counterpart may exist. */
12885 if (child_origin_die != child_die)
12886 {
12887 if (child_die->tag != child_origin_die->tag
12888 && !(child_die->tag == DW_TAG_inlined_subroutine
12889 && child_origin_die->tag == DW_TAG_subprogram))
12890 complaint (_("Child DIE %s and its abstract origin %s have "
12891 "different tags"),
12892 sect_offset_str (child_die->sect_off),
12893 sect_offset_str (child_origin_die->sect_off));
12894 if (child_origin_die->parent != origin_die)
12895 complaint (_("Child DIE %s and its abstract origin %s have "
12896 "different parents"),
12897 sect_offset_str (child_die->sect_off),
12898 sect_offset_str (child_origin_die->sect_off));
12899 else
12900 offsets.push_back (child_origin_die->sect_off);
12901 }
12902
12903 if (are_isomorphic)
12904 corresponding_abstract_child = corresponding_abstract_child->sibling;
12905 }
12906 std::sort (offsets.begin (), offsets.end ());
12907 sect_offset *offsets_end = offsets.data () + offsets.size ();
12908 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12909 if (offsetp[-1] == *offsetp)
12910 complaint (_("Multiple children of DIE %s refer "
12911 "to DIE %s as their abstract origin"),
12912 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12913
12914 offsetp = offsets.data ();
12915 origin_child_die = origin_die->child;
12916 while (origin_child_die && origin_child_die->tag)
12917 {
12918 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12919 while (offsetp < offsets_end
12920 && *offsetp < origin_child_die->sect_off)
12921 offsetp++;
12922 if (offsetp >= offsets_end
12923 || *offsetp > origin_child_die->sect_off)
12924 {
12925 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12926 Check whether we're already processing ORIGIN_CHILD_DIE.
12927 This can happen with mutually referenced abstract_origins.
12928 PR 16581. */
12929 if (!origin_child_die->in_process)
12930 process_die (origin_child_die, origin_cu);
12931 }
12932 origin_child_die = origin_child_die->sibling;
12933 }
12934 origin_cu->list_in_scope = origin_previous_list_in_scope;
12935
12936 if (cu != origin_cu)
12937 compute_delayed_physnames (origin_cu);
12938 }
12939
12940 static void
12941 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12942 {
12943 struct objfile *objfile = cu->per_objfile->objfile;
12944 struct gdbarch *gdbarch = objfile->arch ();
12945 struct context_stack *newobj;
12946 CORE_ADDR lowpc;
12947 CORE_ADDR highpc;
12948 struct die_info *child_die;
12949 struct attribute *attr, *call_line, *call_file;
12950 const char *name;
12951 CORE_ADDR baseaddr;
12952 struct block *block;
12953 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12954 std::vector<struct symbol *> template_args;
12955 struct template_symbol *templ_func = NULL;
12956
12957 if (inlined_func)
12958 {
12959 /* If we do not have call site information, we can't show the
12960 caller of this inlined function. That's too confusing, so
12961 only use the scope for local variables. */
12962 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12963 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12964 if (call_line == NULL || call_file == NULL)
12965 {
12966 read_lexical_block_scope (die, cu);
12967 return;
12968 }
12969 }
12970
12971 baseaddr = objfile->text_section_offset ();
12972
12973 name = dwarf2_name (die, cu);
12974
12975 /* Ignore functions with missing or empty names. These are actually
12976 illegal according to the DWARF standard. */
12977 if (name == NULL)
12978 {
12979 complaint (_("missing name for subprogram DIE at %s"),
12980 sect_offset_str (die->sect_off));
12981 return;
12982 }
12983
12984 /* Ignore functions with missing or invalid low and high pc attributes. */
12985 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12986 <= PC_BOUNDS_INVALID)
12987 {
12988 attr = dwarf2_attr (die, DW_AT_external, cu);
12989 if (attr == nullptr || !attr->as_boolean ())
12990 complaint (_("cannot get low and high bounds "
12991 "for subprogram DIE at %s"),
12992 sect_offset_str (die->sect_off));
12993 return;
12994 }
12995
12996 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12997 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12998
12999 /* If we have any template arguments, then we must allocate a
13000 different sort of symbol. */
13001 for (child_die = die->child; child_die; child_die = child_die->sibling)
13002 {
13003 if (child_die->tag == DW_TAG_template_type_param
13004 || child_die->tag == DW_TAG_template_value_param)
13005 {
13006 templ_func = new (&objfile->objfile_obstack) template_symbol;
13007 templ_func->subclass = SYMBOL_TEMPLATE;
13008 break;
13009 }
13010 }
13011
13012 gdb_assert (cu->get_builder () != nullptr);
13013 newobj = cu->get_builder ()->push_context (0, lowpc);
13014 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13015 (struct symbol *) templ_func);
13016
13017 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13018 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13019 cu->language);
13020
13021 /* If there is a location expression for DW_AT_frame_base, record
13022 it. */
13023 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13024 if (attr != nullptr)
13025 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13026
13027 /* If there is a location for the static link, record it. */
13028 newobj->static_link = NULL;
13029 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13030 if (attr != nullptr)
13031 {
13032 newobj->static_link
13033 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13034 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13035 cu->addr_type ());
13036 }
13037
13038 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13039
13040 if (die->child != NULL)
13041 {
13042 child_die = die->child;
13043 while (child_die && child_die->tag)
13044 {
13045 if (child_die->tag == DW_TAG_template_type_param
13046 || child_die->tag == DW_TAG_template_value_param)
13047 {
13048 struct symbol *arg = new_symbol (child_die, NULL, cu);
13049
13050 if (arg != NULL)
13051 template_args.push_back (arg);
13052 }
13053 else
13054 process_die (child_die, cu);
13055 child_die = child_die->sibling;
13056 }
13057 }
13058
13059 inherit_abstract_dies (die, cu);
13060
13061 /* If we have a DW_AT_specification, we might need to import using
13062 directives from the context of the specification DIE. See the
13063 comment in determine_prefix. */
13064 if (cu->language == language_cplus
13065 && dwarf2_attr (die, DW_AT_specification, cu))
13066 {
13067 struct dwarf2_cu *spec_cu = cu;
13068 struct die_info *spec_die = die_specification (die, &spec_cu);
13069
13070 while (spec_die)
13071 {
13072 child_die = spec_die->child;
13073 while (child_die && child_die->tag)
13074 {
13075 if (child_die->tag == DW_TAG_imported_module)
13076 process_die (child_die, spec_cu);
13077 child_die = child_die->sibling;
13078 }
13079
13080 /* In some cases, GCC generates specification DIEs that
13081 themselves contain DW_AT_specification attributes. */
13082 spec_die = die_specification (spec_die, &spec_cu);
13083 }
13084 }
13085
13086 struct context_stack cstk = cu->get_builder ()->pop_context ();
13087 /* Make a block for the local symbols within. */
13088 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13089 cstk.static_link, lowpc, highpc);
13090
13091 /* For C++, set the block's scope. */
13092 if ((cu->language == language_cplus
13093 || cu->language == language_fortran
13094 || cu->language == language_d
13095 || cu->language == language_rust)
13096 && cu->processing_has_namespace_info)
13097 block_set_scope (block, determine_prefix (die, cu),
13098 &objfile->objfile_obstack);
13099
13100 /* If we have address ranges, record them. */
13101 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13102
13103 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13104
13105 /* Attach template arguments to function. */
13106 if (!template_args.empty ())
13107 {
13108 gdb_assert (templ_func != NULL);
13109
13110 templ_func->n_template_arguments = template_args.size ();
13111 templ_func->template_arguments
13112 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13113 templ_func->n_template_arguments);
13114 memcpy (templ_func->template_arguments,
13115 template_args.data (),
13116 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13117
13118 /* Make sure that the symtab is set on the new symbols. Even
13119 though they don't appear in this symtab directly, other parts
13120 of gdb assume that symbols do, and this is reasonably
13121 true. */
13122 for (symbol *sym : template_args)
13123 symbol_set_symtab (sym, symbol_symtab (templ_func));
13124 }
13125
13126 /* In C++, we can have functions nested inside functions (e.g., when
13127 a function declares a class that has methods). This means that
13128 when we finish processing a function scope, we may need to go
13129 back to building a containing block's symbol lists. */
13130 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13131 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13132
13133 /* If we've finished processing a top-level function, subsequent
13134 symbols go in the file symbol list. */
13135 if (cu->get_builder ()->outermost_context_p ())
13136 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13137 }
13138
13139 /* Process all the DIES contained within a lexical block scope. Start
13140 a new scope, process the dies, and then close the scope. */
13141
13142 static void
13143 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13144 {
13145 struct objfile *objfile = cu->per_objfile->objfile;
13146 struct gdbarch *gdbarch = objfile->arch ();
13147 CORE_ADDR lowpc, highpc;
13148 struct die_info *child_die;
13149 CORE_ADDR baseaddr;
13150
13151 baseaddr = objfile->text_section_offset ();
13152
13153 /* Ignore blocks with missing or invalid low and high pc attributes. */
13154 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13155 as multiple lexical blocks? Handling children in a sane way would
13156 be nasty. Might be easier to properly extend generic blocks to
13157 describe ranges. */
13158 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13159 {
13160 case PC_BOUNDS_NOT_PRESENT:
13161 /* DW_TAG_lexical_block has no attributes, process its children as if
13162 there was no wrapping by that DW_TAG_lexical_block.
13163 GCC does no longer produces such DWARF since GCC r224161. */
13164 for (child_die = die->child;
13165 child_die != NULL && child_die->tag;
13166 child_die = child_die->sibling)
13167 {
13168 /* We might already be processing this DIE. This can happen
13169 in an unusual circumstance -- where a subroutine A
13170 appears lexically in another subroutine B, but A actually
13171 inlines B. The recursion is broken here, rather than in
13172 inherit_abstract_dies, because it seems better to simply
13173 drop concrete children here. */
13174 if (!child_die->in_process)
13175 process_die (child_die, cu);
13176 }
13177 return;
13178 case PC_BOUNDS_INVALID:
13179 return;
13180 }
13181 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13182 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13183
13184 cu->get_builder ()->push_context (0, lowpc);
13185 if (die->child != NULL)
13186 {
13187 child_die = die->child;
13188 while (child_die && child_die->tag)
13189 {
13190 process_die (child_die, cu);
13191 child_die = child_die->sibling;
13192 }
13193 }
13194 inherit_abstract_dies (die, cu);
13195 struct context_stack cstk = cu->get_builder ()->pop_context ();
13196
13197 if (*cu->get_builder ()->get_local_symbols () != NULL
13198 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13199 {
13200 struct block *block
13201 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13202 cstk.start_addr, highpc);
13203
13204 /* Note that recording ranges after traversing children, as we
13205 do here, means that recording a parent's ranges entails
13206 walking across all its children's ranges as they appear in
13207 the address map, which is quadratic behavior.
13208
13209 It would be nicer to record the parent's ranges before
13210 traversing its children, simply overriding whatever you find
13211 there. But since we don't even decide whether to create a
13212 block until after we've traversed its children, that's hard
13213 to do. */
13214 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13215 }
13216 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13217 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13218 }
13219
13220 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13221
13222 static void
13223 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13224 {
13225 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13226 struct objfile *objfile = per_objfile->objfile;
13227 struct gdbarch *gdbarch = objfile->arch ();
13228 CORE_ADDR pc, baseaddr;
13229 struct attribute *attr;
13230 struct call_site *call_site, call_site_local;
13231 void **slot;
13232 int nparams;
13233 struct die_info *child_die;
13234
13235 baseaddr = objfile->text_section_offset ();
13236
13237 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13238 if (attr == NULL)
13239 {
13240 /* This was a pre-DWARF-5 GNU extension alias
13241 for DW_AT_call_return_pc. */
13242 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13243 }
13244 if (!attr)
13245 {
13246 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13247 "DIE %s [in module %s]"),
13248 sect_offset_str (die->sect_off), objfile_name (objfile));
13249 return;
13250 }
13251 pc = attr->as_address () + baseaddr;
13252 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13253
13254 if (cu->call_site_htab == NULL)
13255 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13256 NULL, &objfile->objfile_obstack,
13257 hashtab_obstack_allocate, NULL);
13258 call_site_local.pc = pc;
13259 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13260 if (*slot != NULL)
13261 {
13262 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13263 "DIE %s [in module %s]"),
13264 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13265 objfile_name (objfile));
13266 return;
13267 }
13268
13269 /* Count parameters at the caller. */
13270
13271 nparams = 0;
13272 for (child_die = die->child; child_die && child_die->tag;
13273 child_die = child_die->sibling)
13274 {
13275 if (child_die->tag != DW_TAG_call_site_parameter
13276 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13277 {
13278 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13279 "DW_TAG_call_site child DIE %s [in module %s]"),
13280 child_die->tag, sect_offset_str (child_die->sect_off),
13281 objfile_name (objfile));
13282 continue;
13283 }
13284
13285 nparams++;
13286 }
13287
13288 call_site
13289 = ((struct call_site *)
13290 obstack_alloc (&objfile->objfile_obstack,
13291 sizeof (*call_site)
13292 + (sizeof (*call_site->parameter) * (nparams - 1))));
13293 *slot = call_site;
13294 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13295 call_site->pc = pc;
13296
13297 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13298 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13299 {
13300 struct die_info *func_die;
13301
13302 /* Skip also over DW_TAG_inlined_subroutine. */
13303 for (func_die = die->parent;
13304 func_die && func_die->tag != DW_TAG_subprogram
13305 && func_die->tag != DW_TAG_subroutine_type;
13306 func_die = func_die->parent);
13307
13308 /* DW_AT_call_all_calls is a superset
13309 of DW_AT_call_all_tail_calls. */
13310 if (func_die
13311 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13312 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13313 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13314 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13315 {
13316 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13317 not complete. But keep CALL_SITE for look ups via call_site_htab,
13318 both the initial caller containing the real return address PC and
13319 the final callee containing the current PC of a chain of tail
13320 calls do not need to have the tail call list complete. But any
13321 function candidate for a virtual tail call frame searched via
13322 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13323 determined unambiguously. */
13324 }
13325 else
13326 {
13327 struct type *func_type = NULL;
13328
13329 if (func_die)
13330 func_type = get_die_type (func_die, cu);
13331 if (func_type != NULL)
13332 {
13333 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13334
13335 /* Enlist this call site to the function. */
13336 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13337 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13338 }
13339 else
13340 complaint (_("Cannot find function owning DW_TAG_call_site "
13341 "DIE %s [in module %s]"),
13342 sect_offset_str (die->sect_off), objfile_name (objfile));
13343 }
13344 }
13345
13346 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13347 if (attr == NULL)
13348 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13349 if (attr == NULL)
13350 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13351 if (attr == NULL)
13352 {
13353 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13354 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13355 }
13356 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13357 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13358 /* Keep NULL DWARF_BLOCK. */;
13359 else if (attr->form_is_block ())
13360 {
13361 struct dwarf2_locexpr_baton *dlbaton;
13362 struct dwarf_block *block = attr->as_block ();
13363
13364 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13365 dlbaton->data = block->data;
13366 dlbaton->size = block->size;
13367 dlbaton->per_objfile = per_objfile;
13368 dlbaton->per_cu = cu->per_cu;
13369
13370 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13371 }
13372 else if (attr->form_is_ref ())
13373 {
13374 struct dwarf2_cu *target_cu = cu;
13375 struct die_info *target_die;
13376
13377 target_die = follow_die_ref (die, attr, &target_cu);
13378 gdb_assert (target_cu->per_objfile->objfile == objfile);
13379 if (die_is_declaration (target_die, target_cu))
13380 {
13381 const char *target_physname;
13382
13383 /* Prefer the mangled name; otherwise compute the demangled one. */
13384 target_physname = dw2_linkage_name (target_die, target_cu);
13385 if (target_physname == NULL)
13386 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13387 if (target_physname == NULL)
13388 complaint (_("DW_AT_call_target target DIE has invalid "
13389 "physname, for referencing DIE %s [in module %s]"),
13390 sect_offset_str (die->sect_off), objfile_name (objfile));
13391 else
13392 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13393 }
13394 else
13395 {
13396 CORE_ADDR lowpc;
13397
13398 /* DW_AT_entry_pc should be preferred. */
13399 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13400 <= PC_BOUNDS_INVALID)
13401 complaint (_("DW_AT_call_target target DIE has invalid "
13402 "low pc, for referencing DIE %s [in module %s]"),
13403 sect_offset_str (die->sect_off), objfile_name (objfile));
13404 else
13405 {
13406 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13407 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13408 }
13409 }
13410 }
13411 else
13412 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13413 "block nor reference, for DIE %s [in module %s]"),
13414 sect_offset_str (die->sect_off), objfile_name (objfile));
13415
13416 call_site->per_cu = cu->per_cu;
13417 call_site->per_objfile = per_objfile;
13418
13419 for (child_die = die->child;
13420 child_die && child_die->tag;
13421 child_die = child_die->sibling)
13422 {
13423 struct call_site_parameter *parameter;
13424 struct attribute *loc, *origin;
13425
13426 if (child_die->tag != DW_TAG_call_site_parameter
13427 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13428 {
13429 /* Already printed the complaint above. */
13430 continue;
13431 }
13432
13433 gdb_assert (call_site->parameter_count < nparams);
13434 parameter = &call_site->parameter[call_site->parameter_count];
13435
13436 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13437 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13438 register is contained in DW_AT_call_value. */
13439
13440 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13441 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13442 if (origin == NULL)
13443 {
13444 /* This was a pre-DWARF-5 GNU extension alias
13445 for DW_AT_call_parameter. */
13446 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13447 }
13448 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13449 {
13450 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13451
13452 sect_offset sect_off = origin->get_ref_die_offset ();
13453 if (!cu->header.offset_in_cu_p (sect_off))
13454 {
13455 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13456 binding can be done only inside one CU. Such referenced DIE
13457 therefore cannot be even moved to DW_TAG_partial_unit. */
13458 complaint (_("DW_AT_call_parameter offset is not in CU for "
13459 "DW_TAG_call_site child DIE %s [in module %s]"),
13460 sect_offset_str (child_die->sect_off),
13461 objfile_name (objfile));
13462 continue;
13463 }
13464 parameter->u.param_cu_off
13465 = (cu_offset) (sect_off - cu->header.sect_off);
13466 }
13467 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13468 {
13469 complaint (_("No DW_FORM_block* DW_AT_location for "
13470 "DW_TAG_call_site child DIE %s [in module %s]"),
13471 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13472 continue;
13473 }
13474 else
13475 {
13476 struct dwarf_block *block = loc->as_block ();
13477
13478 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13479 (block->data, &block->data[block->size]);
13480 if (parameter->u.dwarf_reg != -1)
13481 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13482 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13483 &block->data[block->size],
13484 &parameter->u.fb_offset))
13485 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13486 else
13487 {
13488 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13489 "for DW_FORM_block* DW_AT_location is supported for "
13490 "DW_TAG_call_site child DIE %s "
13491 "[in module %s]"),
13492 sect_offset_str (child_die->sect_off),
13493 objfile_name (objfile));
13494 continue;
13495 }
13496 }
13497
13498 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13499 if (attr == NULL)
13500 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13501 if (attr == NULL || !attr->form_is_block ())
13502 {
13503 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13504 "DW_TAG_call_site child DIE %s [in module %s]"),
13505 sect_offset_str (child_die->sect_off),
13506 objfile_name (objfile));
13507 continue;
13508 }
13509
13510 struct dwarf_block *block = attr->as_block ();
13511 parameter->value = block->data;
13512 parameter->value_size = block->size;
13513
13514 /* Parameters are not pre-cleared by memset above. */
13515 parameter->data_value = NULL;
13516 parameter->data_value_size = 0;
13517 call_site->parameter_count++;
13518
13519 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13520 if (attr == NULL)
13521 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13522 if (attr != nullptr)
13523 {
13524 if (!attr->form_is_block ())
13525 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13526 "DW_TAG_call_site child DIE %s [in module %s]"),
13527 sect_offset_str (child_die->sect_off),
13528 objfile_name (objfile));
13529 else
13530 {
13531 block = attr->as_block ();
13532 parameter->data_value = block->data;
13533 parameter->data_value_size = block->size;
13534 }
13535 }
13536 }
13537 }
13538
13539 /* Helper function for read_variable. If DIE represents a virtual
13540 table, then return the type of the concrete object that is
13541 associated with the virtual table. Otherwise, return NULL. */
13542
13543 static struct type *
13544 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13545 {
13546 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13547 if (attr == NULL)
13548 return NULL;
13549
13550 /* Find the type DIE. */
13551 struct die_info *type_die = NULL;
13552 struct dwarf2_cu *type_cu = cu;
13553
13554 if (attr->form_is_ref ())
13555 type_die = follow_die_ref (die, attr, &type_cu);
13556 if (type_die == NULL)
13557 return NULL;
13558
13559 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13560 return NULL;
13561 return die_containing_type (type_die, type_cu);
13562 }
13563
13564 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13565
13566 static void
13567 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13568 {
13569 struct rust_vtable_symbol *storage = NULL;
13570
13571 if (cu->language == language_rust)
13572 {
13573 struct type *containing_type = rust_containing_type (die, cu);
13574
13575 if (containing_type != NULL)
13576 {
13577 struct objfile *objfile = cu->per_objfile->objfile;
13578
13579 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13580 storage->concrete_type = containing_type;
13581 storage->subclass = SYMBOL_RUST_VTABLE;
13582 }
13583 }
13584
13585 struct symbol *res = new_symbol (die, NULL, cu, storage);
13586 struct attribute *abstract_origin
13587 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13588 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13589 if (res == NULL && loc && abstract_origin)
13590 {
13591 /* We have a variable without a name, but with a location and an abstract
13592 origin. This may be a concrete instance of an abstract variable
13593 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13594 later. */
13595 struct dwarf2_cu *origin_cu = cu;
13596 struct die_info *origin_die
13597 = follow_die_ref (die, abstract_origin, &origin_cu);
13598 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13599 per_objfile->per_bfd->abstract_to_concrete
13600 [origin_die->sect_off].push_back (die->sect_off);
13601 }
13602 }
13603
13604 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13605 reading .debug_rnglists.
13606 Callback's type should be:
13607 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13608 Return true if the attributes are present and valid, otherwise,
13609 return false. */
13610
13611 template <typename Callback>
13612 static bool
13613 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13614 dwarf_tag tag, Callback &&callback)
13615 {
13616 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13617 struct objfile *objfile = per_objfile->objfile;
13618 bfd *obfd = objfile->obfd;
13619 /* Base address selection entry. */
13620 gdb::optional<CORE_ADDR> base;
13621 const gdb_byte *buffer;
13622 CORE_ADDR baseaddr;
13623 bool overflow = false;
13624 ULONGEST addr_index;
13625 struct dwarf2_section_info *rnglists_section;
13626
13627 base = cu->base_address;
13628 rnglists_section = cu_debug_rnglists_section (cu, tag);
13629 rnglists_section->read (objfile);
13630
13631 if (offset >= rnglists_section->size)
13632 {
13633 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13634 offset);
13635 return false;
13636 }
13637 buffer = rnglists_section->buffer + offset;
13638
13639 baseaddr = objfile->text_section_offset ();
13640
13641 while (1)
13642 {
13643 /* Initialize it due to a false compiler warning. */
13644 CORE_ADDR range_beginning = 0, range_end = 0;
13645 const gdb_byte *buf_end = (rnglists_section->buffer
13646 + rnglists_section->size);
13647 unsigned int bytes_read;
13648
13649 if (buffer == buf_end)
13650 {
13651 overflow = true;
13652 break;
13653 }
13654 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13655 switch (rlet)
13656 {
13657 case DW_RLE_end_of_list:
13658 break;
13659 case DW_RLE_base_address:
13660 if (buffer + cu->header.addr_size > buf_end)
13661 {
13662 overflow = true;
13663 break;
13664 }
13665 base = cu->header.read_address (obfd, buffer, &bytes_read);
13666 buffer += bytes_read;
13667 break;
13668 case DW_RLE_base_addressx:
13669 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13670 buffer += bytes_read;
13671 base = read_addr_index (cu, addr_index);
13672 break;
13673 case DW_RLE_start_length:
13674 if (buffer + cu->header.addr_size > buf_end)
13675 {
13676 overflow = true;
13677 break;
13678 }
13679 range_beginning = cu->header.read_address (obfd, buffer,
13680 &bytes_read);
13681 buffer += bytes_read;
13682 range_end = (range_beginning
13683 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13684 buffer += bytes_read;
13685 if (buffer > buf_end)
13686 {
13687 overflow = true;
13688 break;
13689 }
13690 break;
13691 case DW_RLE_startx_length:
13692 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13693 buffer += bytes_read;
13694 range_beginning = read_addr_index (cu, addr_index);
13695 if (buffer > buf_end)
13696 {
13697 overflow = true;
13698 break;
13699 }
13700 range_end = (range_beginning
13701 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13702 buffer += bytes_read;
13703 break;
13704 case DW_RLE_offset_pair:
13705 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13706 buffer += bytes_read;
13707 if (buffer > buf_end)
13708 {
13709 overflow = true;
13710 break;
13711 }
13712 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13713 buffer += bytes_read;
13714 if (buffer > buf_end)
13715 {
13716 overflow = true;
13717 break;
13718 }
13719 break;
13720 case DW_RLE_start_end:
13721 if (buffer + 2 * cu->header.addr_size > buf_end)
13722 {
13723 overflow = true;
13724 break;
13725 }
13726 range_beginning = cu->header.read_address (obfd, buffer,
13727 &bytes_read);
13728 buffer += bytes_read;
13729 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13730 buffer += bytes_read;
13731 break;
13732 case DW_RLE_startx_endx:
13733 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13734 buffer += bytes_read;
13735 range_beginning = read_addr_index (cu, addr_index);
13736 if (buffer > buf_end)
13737 {
13738 overflow = true;
13739 break;
13740 }
13741 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13742 buffer += bytes_read;
13743 range_end = read_addr_index (cu, addr_index);
13744 break;
13745 default:
13746 complaint (_("Invalid .debug_rnglists data (no base address)"));
13747 return false;
13748 }
13749 if (rlet == DW_RLE_end_of_list || overflow)
13750 break;
13751 if (rlet == DW_RLE_base_address)
13752 continue;
13753
13754 if (range_beginning > range_end)
13755 {
13756 /* Inverted range entries are invalid. */
13757 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13758 return false;
13759 }
13760
13761 /* Empty range entries have no effect. */
13762 if (range_beginning == range_end)
13763 continue;
13764
13765 /* Only DW_RLE_offset_pair needs the base address added. */
13766 if (rlet == DW_RLE_offset_pair)
13767 {
13768 if (!base.has_value ())
13769 {
13770 /* We have no valid base address for the DW_RLE_offset_pair. */
13771 complaint (_("Invalid .debug_rnglists data (no base address for "
13772 "DW_RLE_offset_pair)"));
13773 return false;
13774 }
13775
13776 range_beginning += *base;
13777 range_end += *base;
13778 }
13779
13780 /* A not-uncommon case of bad debug info.
13781 Don't pollute the addrmap with bad data. */
13782 if (range_beginning + baseaddr == 0
13783 && !per_objfile->per_bfd->has_section_at_zero)
13784 {
13785 complaint (_(".debug_rnglists entry has start address of zero"
13786 " [in module %s]"), objfile_name (objfile));
13787 continue;
13788 }
13789
13790 callback (range_beginning, range_end);
13791 }
13792
13793 if (overflow)
13794 {
13795 complaint (_("Offset %d is not terminated "
13796 "for DW_AT_ranges attribute"),
13797 offset);
13798 return false;
13799 }
13800
13801 return true;
13802 }
13803
13804 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13805 Callback's type should be:
13806 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13807 Return 1 if the attributes are present and valid, otherwise, return 0. */
13808
13809 template <typename Callback>
13810 static int
13811 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13812 Callback &&callback)
13813 {
13814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13815 struct objfile *objfile = per_objfile->objfile;
13816 struct comp_unit_head *cu_header = &cu->header;
13817 bfd *obfd = objfile->obfd;
13818 unsigned int addr_size = cu_header->addr_size;
13819 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13820 /* Base address selection entry. */
13821 gdb::optional<CORE_ADDR> base;
13822 unsigned int dummy;
13823 const gdb_byte *buffer;
13824 CORE_ADDR baseaddr;
13825
13826 if (cu_header->version >= 5)
13827 return dwarf2_rnglists_process (offset, cu, tag, callback);
13828
13829 base = cu->base_address;
13830
13831 per_objfile->per_bfd->ranges.read (objfile);
13832 if (offset >= per_objfile->per_bfd->ranges.size)
13833 {
13834 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13835 offset);
13836 return 0;
13837 }
13838 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13839
13840 baseaddr = objfile->text_section_offset ();
13841
13842 while (1)
13843 {
13844 CORE_ADDR range_beginning, range_end;
13845
13846 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13847 buffer += addr_size;
13848 range_end = cu->header.read_address (obfd, buffer, &dummy);
13849 buffer += addr_size;
13850 offset += 2 * addr_size;
13851
13852 /* An end of list marker is a pair of zero addresses. */
13853 if (range_beginning == 0 && range_end == 0)
13854 /* Found the end of list entry. */
13855 break;
13856
13857 /* Each base address selection entry is a pair of 2 values.
13858 The first is the largest possible address, the second is
13859 the base address. Check for a base address here. */
13860 if ((range_beginning & mask) == mask)
13861 {
13862 /* If we found the largest possible address, then we already
13863 have the base address in range_end. */
13864 base = range_end;
13865 continue;
13866 }
13867
13868 if (!base.has_value ())
13869 {
13870 /* We have no valid base address for the ranges
13871 data. */
13872 complaint (_("Invalid .debug_ranges data (no base address)"));
13873 return 0;
13874 }
13875
13876 if (range_beginning > range_end)
13877 {
13878 /* Inverted range entries are invalid. */
13879 complaint (_("Invalid .debug_ranges data (inverted range)"));
13880 return 0;
13881 }
13882
13883 /* Empty range entries have no effect. */
13884 if (range_beginning == range_end)
13885 continue;
13886
13887 range_beginning += *base;
13888 range_end += *base;
13889
13890 /* A not-uncommon case of bad debug info.
13891 Don't pollute the addrmap with bad data. */
13892 if (range_beginning + baseaddr == 0
13893 && !per_objfile->per_bfd->has_section_at_zero)
13894 {
13895 complaint (_(".debug_ranges entry has start address of zero"
13896 " [in module %s]"), objfile_name (objfile));
13897 continue;
13898 }
13899
13900 callback (range_beginning, range_end);
13901 }
13902
13903 return 1;
13904 }
13905
13906 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13907 Return 1 if the attributes are present and valid, otherwise, return 0.
13908 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
13909
13910 static int
13911 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13912 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13913 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
13914 {
13915 struct objfile *objfile = cu->per_objfile->objfile;
13916 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
13917 struct gdbarch *gdbarch = objfile->arch ();
13918 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13919 int low_set = 0;
13920 CORE_ADDR low = 0;
13921 CORE_ADDR high = 0;
13922 int retval;
13923
13924 retval = dwarf2_ranges_process (offset, cu, tag,
13925 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13926 {
13927 if (ranges_pst != NULL)
13928 {
13929 CORE_ADDR lowpc;
13930 CORE_ADDR highpc;
13931
13932 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13933 range_beginning + baseaddr)
13934 - baseaddr);
13935 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13936 range_end + baseaddr)
13937 - baseaddr);
13938 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
13939 lowpc, highpc - 1, ranges_pst);
13940 }
13941
13942 /* FIXME: This is recording everything as a low-high
13943 segment of consecutive addresses. We should have a
13944 data structure for discontiguous block ranges
13945 instead. */
13946 if (! low_set)
13947 {
13948 low = range_beginning;
13949 high = range_end;
13950 low_set = 1;
13951 }
13952 else
13953 {
13954 if (range_beginning < low)
13955 low = range_beginning;
13956 if (range_end > high)
13957 high = range_end;
13958 }
13959 });
13960 if (!retval)
13961 return 0;
13962
13963 if (! low_set)
13964 /* If the first entry is an end-of-list marker, the range
13965 describes an empty scope, i.e. no instructions. */
13966 return 0;
13967
13968 if (low_return)
13969 *low_return = low;
13970 if (high_return)
13971 *high_return = high;
13972 return 1;
13973 }
13974
13975 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13976 definition for the return value. *LOWPC and *HIGHPC are set iff
13977 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13978
13979 static enum pc_bounds_kind
13980 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13981 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13982 dwarf2_psymtab *pst)
13983 {
13984 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13985 struct attribute *attr;
13986 struct attribute *attr_high;
13987 CORE_ADDR low = 0;
13988 CORE_ADDR high = 0;
13989 enum pc_bounds_kind ret;
13990
13991 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13992 if (attr_high)
13993 {
13994 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13995 if (attr != nullptr)
13996 {
13997 low = attr->as_address ();
13998 high = attr_high->as_address ();
13999 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14000 high += low;
14001 }
14002 else
14003 /* Found high w/o low attribute. */
14004 return PC_BOUNDS_INVALID;
14005
14006 /* Found consecutive range of addresses. */
14007 ret = PC_BOUNDS_HIGH_LOW;
14008 }
14009 else
14010 {
14011 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14012 if (attr != nullptr && attr->form_is_unsigned ())
14013 {
14014 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14015 on DWARF version). */
14016 ULONGEST ranges_offset = attr->as_unsigned ();
14017
14018 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14019 this value. */
14020 if (die->tag != DW_TAG_compile_unit)
14021 ranges_offset += cu->gnu_ranges_base;
14022
14023 /* Value of the DW_AT_ranges attribute is the offset in the
14024 .debug_ranges section. */
14025 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14026 die->tag))
14027 return PC_BOUNDS_INVALID;
14028 /* Found discontinuous range of addresses. */
14029 ret = PC_BOUNDS_RANGES;
14030 }
14031 else
14032 return PC_BOUNDS_NOT_PRESENT;
14033 }
14034
14035 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14036 if (high <= low)
14037 return PC_BOUNDS_INVALID;
14038
14039 /* When using the GNU linker, .gnu.linkonce. sections are used to
14040 eliminate duplicate copies of functions and vtables and such.
14041 The linker will arbitrarily choose one and discard the others.
14042 The AT_*_pc values for such functions refer to local labels in
14043 these sections. If the section from that file was discarded, the
14044 labels are not in the output, so the relocs get a value of 0.
14045 If this is a discarded function, mark the pc bounds as invalid,
14046 so that GDB will ignore it. */
14047 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14048 return PC_BOUNDS_INVALID;
14049
14050 *lowpc = low;
14051 if (highpc)
14052 *highpc = high;
14053 return ret;
14054 }
14055
14056 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14057 its low and high PC addresses. Do nothing if these addresses could not
14058 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14059 and HIGHPC to the high address if greater than HIGHPC. */
14060
14061 static void
14062 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14063 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14064 struct dwarf2_cu *cu)
14065 {
14066 CORE_ADDR low, high;
14067 struct die_info *child = die->child;
14068
14069 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14070 {
14071 *lowpc = std::min (*lowpc, low);
14072 *highpc = std::max (*highpc, high);
14073 }
14074
14075 /* If the language does not allow nested subprograms (either inside
14076 subprograms or lexical blocks), we're done. */
14077 if (cu->language != language_ada)
14078 return;
14079
14080 /* Check all the children of the given DIE. If it contains nested
14081 subprograms, then check their pc bounds. Likewise, we need to
14082 check lexical blocks as well, as they may also contain subprogram
14083 definitions. */
14084 while (child && child->tag)
14085 {
14086 if (child->tag == DW_TAG_subprogram
14087 || child->tag == DW_TAG_lexical_block)
14088 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14089 child = child->sibling;
14090 }
14091 }
14092
14093 /* Get the low and high pc's represented by the scope DIE, and store
14094 them in *LOWPC and *HIGHPC. If the correct values can't be
14095 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14096
14097 static void
14098 get_scope_pc_bounds (struct die_info *die,
14099 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14100 struct dwarf2_cu *cu)
14101 {
14102 CORE_ADDR best_low = (CORE_ADDR) -1;
14103 CORE_ADDR best_high = (CORE_ADDR) 0;
14104 CORE_ADDR current_low, current_high;
14105
14106 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14107 >= PC_BOUNDS_RANGES)
14108 {
14109 best_low = current_low;
14110 best_high = current_high;
14111 }
14112 else
14113 {
14114 struct die_info *child = die->child;
14115
14116 while (child && child->tag)
14117 {
14118 switch (child->tag) {
14119 case DW_TAG_subprogram:
14120 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14121 break;
14122 case DW_TAG_namespace:
14123 case DW_TAG_module:
14124 /* FIXME: carlton/2004-01-16: Should we do this for
14125 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14126 that current GCC's always emit the DIEs corresponding
14127 to definitions of methods of classes as children of a
14128 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14129 the DIEs giving the declarations, which could be
14130 anywhere). But I don't see any reason why the
14131 standards says that they have to be there. */
14132 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14133
14134 if (current_low != ((CORE_ADDR) -1))
14135 {
14136 best_low = std::min (best_low, current_low);
14137 best_high = std::max (best_high, current_high);
14138 }
14139 break;
14140 default:
14141 /* Ignore. */
14142 break;
14143 }
14144
14145 child = child->sibling;
14146 }
14147 }
14148
14149 *lowpc = best_low;
14150 *highpc = best_high;
14151 }
14152
14153 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14154 in DIE. */
14155
14156 static void
14157 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14158 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14159 {
14160 struct objfile *objfile = cu->per_objfile->objfile;
14161 struct gdbarch *gdbarch = objfile->arch ();
14162 struct attribute *attr;
14163 struct attribute *attr_high;
14164
14165 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14166 if (attr_high)
14167 {
14168 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14169 if (attr != nullptr)
14170 {
14171 CORE_ADDR low = attr->as_address ();
14172 CORE_ADDR high = attr_high->as_address ();
14173
14174 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14175 high += low;
14176
14177 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14178 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14179 cu->get_builder ()->record_block_range (block, low, high - 1);
14180 }
14181 }
14182
14183 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14184 if (attr != nullptr && attr->form_is_unsigned ())
14185 {
14186 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14187 on DWARF version). */
14188 ULONGEST ranges_offset = attr->as_unsigned ();
14189
14190 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14191 this value. */
14192 if (die->tag != DW_TAG_compile_unit)
14193 ranges_offset += cu->gnu_ranges_base;
14194
14195 std::vector<blockrange> blockvec;
14196 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14197 [&] (CORE_ADDR start, CORE_ADDR end)
14198 {
14199 start += baseaddr;
14200 end += baseaddr;
14201 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14202 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14203 cu->get_builder ()->record_block_range (block, start, end - 1);
14204 blockvec.emplace_back (start, end);
14205 });
14206
14207 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14208 }
14209 }
14210
14211 /* Check whether the producer field indicates either of GCC < 4.6, or the
14212 Intel C/C++ compiler, and cache the result in CU. */
14213
14214 static void
14215 check_producer (struct dwarf2_cu *cu)
14216 {
14217 int major, minor;
14218
14219 if (cu->producer == NULL)
14220 {
14221 /* For unknown compilers expect their behavior is DWARF version
14222 compliant.
14223
14224 GCC started to support .debug_types sections by -gdwarf-4 since
14225 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14226 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14227 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14228 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14229 }
14230 else if (producer_is_gcc (cu->producer, &major, &minor))
14231 {
14232 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14233 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14234 }
14235 else if (producer_is_icc (cu->producer, &major, &minor))
14236 {
14237 cu->producer_is_icc = true;
14238 cu->producer_is_icc_lt_14 = major < 14;
14239 }
14240 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14241 cu->producer_is_codewarrior = true;
14242 else
14243 {
14244 /* For other non-GCC compilers, expect their behavior is DWARF version
14245 compliant. */
14246 }
14247
14248 cu->checked_producer = true;
14249 }
14250
14251 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14252 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14253 during 4.6.0 experimental. */
14254
14255 static bool
14256 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14257 {
14258 if (!cu->checked_producer)
14259 check_producer (cu);
14260
14261 return cu->producer_is_gxx_lt_4_6;
14262 }
14263
14264
14265 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14266 with incorrect is_stmt attributes. */
14267
14268 static bool
14269 producer_is_codewarrior (struct dwarf2_cu *cu)
14270 {
14271 if (!cu->checked_producer)
14272 check_producer (cu);
14273
14274 return cu->producer_is_codewarrior;
14275 }
14276
14277 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14278 If that attribute is not available, return the appropriate
14279 default. */
14280
14281 static enum dwarf_access_attribute
14282 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14283 {
14284 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14285 if (attr != nullptr)
14286 {
14287 LONGEST value = attr->constant_value (-1);
14288 if (value == DW_ACCESS_public
14289 || value == DW_ACCESS_protected
14290 || value == DW_ACCESS_private)
14291 return (dwarf_access_attribute) value;
14292 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14293 plongest (value));
14294 }
14295
14296 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14297 {
14298 /* The default DWARF 2 accessibility for members is public, the default
14299 accessibility for inheritance is private. */
14300
14301 if (die->tag != DW_TAG_inheritance)
14302 return DW_ACCESS_public;
14303 else
14304 return DW_ACCESS_private;
14305 }
14306 else
14307 {
14308 /* DWARF 3+ defines the default accessibility a different way. The same
14309 rules apply now for DW_TAG_inheritance as for the members and it only
14310 depends on the container kind. */
14311
14312 if (die->parent->tag == DW_TAG_class_type)
14313 return DW_ACCESS_private;
14314 else
14315 return DW_ACCESS_public;
14316 }
14317 }
14318
14319 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14320 offset. If the attribute was not found return 0, otherwise return
14321 1. If it was found but could not properly be handled, set *OFFSET
14322 to 0. */
14323
14324 static int
14325 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14326 LONGEST *offset)
14327 {
14328 struct attribute *attr;
14329
14330 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14331 if (attr != NULL)
14332 {
14333 *offset = 0;
14334
14335 /* Note that we do not check for a section offset first here.
14336 This is because DW_AT_data_member_location is new in DWARF 4,
14337 so if we see it, we can assume that a constant form is really
14338 a constant and not a section offset. */
14339 if (attr->form_is_constant ())
14340 *offset = attr->constant_value (0);
14341 else if (attr->form_is_section_offset ())
14342 dwarf2_complex_location_expr_complaint ();
14343 else if (attr->form_is_block ())
14344 *offset = decode_locdesc (attr->as_block (), cu);
14345 else
14346 dwarf2_complex_location_expr_complaint ();
14347
14348 return 1;
14349 }
14350
14351 return 0;
14352 }
14353
14354 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14355
14356 static void
14357 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14358 struct field *field)
14359 {
14360 struct attribute *attr;
14361
14362 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14363 if (attr != NULL)
14364 {
14365 if (attr->form_is_constant ())
14366 {
14367 LONGEST offset = attr->constant_value (0);
14368 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14369 }
14370 else if (attr->form_is_section_offset ())
14371 dwarf2_complex_location_expr_complaint ();
14372 else if (attr->form_is_block ())
14373 {
14374 bool handled;
14375 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14376 if (handled)
14377 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14378 else
14379 {
14380 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14381 struct objfile *objfile = per_objfile->objfile;
14382 struct dwarf2_locexpr_baton *dlbaton
14383 = XOBNEW (&objfile->objfile_obstack,
14384 struct dwarf2_locexpr_baton);
14385 dlbaton->data = attr->as_block ()->data;
14386 dlbaton->size = attr->as_block ()->size;
14387 /* When using this baton, we want to compute the address
14388 of the field, not the value. This is why
14389 is_reference is set to false here. */
14390 dlbaton->is_reference = false;
14391 dlbaton->per_objfile = per_objfile;
14392 dlbaton->per_cu = cu->per_cu;
14393
14394 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14395 }
14396 }
14397 else
14398 dwarf2_complex_location_expr_complaint ();
14399 }
14400 }
14401
14402 /* Add an aggregate field to the field list. */
14403
14404 static void
14405 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14406 struct dwarf2_cu *cu)
14407 {
14408 struct objfile *objfile = cu->per_objfile->objfile;
14409 struct gdbarch *gdbarch = objfile->arch ();
14410 struct nextfield *new_field;
14411 struct attribute *attr;
14412 struct field *fp;
14413 const char *fieldname = "";
14414
14415 if (die->tag == DW_TAG_inheritance)
14416 {
14417 fip->baseclasses.emplace_back ();
14418 new_field = &fip->baseclasses.back ();
14419 }
14420 else
14421 {
14422 fip->fields.emplace_back ();
14423 new_field = &fip->fields.back ();
14424 }
14425
14426 new_field->offset = die->sect_off;
14427
14428 new_field->accessibility = dwarf2_access_attribute (die, cu);
14429 if (new_field->accessibility != DW_ACCESS_public)
14430 fip->non_public_fields = true;
14431
14432 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14433 if (attr != nullptr)
14434 new_field->virtuality = attr->as_virtuality ();
14435 else
14436 new_field->virtuality = DW_VIRTUALITY_none;
14437
14438 fp = &new_field->field;
14439
14440 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14441 {
14442 /* Data member other than a C++ static data member. */
14443
14444 /* Get type of field. */
14445 fp->set_type (die_type (die, cu));
14446
14447 SET_FIELD_BITPOS (*fp, 0);
14448
14449 /* Get bit size of field (zero if none). */
14450 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14451 if (attr != nullptr)
14452 {
14453 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14454 }
14455 else
14456 {
14457 FIELD_BITSIZE (*fp) = 0;
14458 }
14459
14460 /* Get bit offset of field. */
14461 handle_data_member_location (die, cu, fp);
14462 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14463 if (attr != nullptr && attr->form_is_constant ())
14464 {
14465 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14466 {
14467 /* For big endian bits, the DW_AT_bit_offset gives the
14468 additional bit offset from the MSB of the containing
14469 anonymous object to the MSB of the field. We don't
14470 have to do anything special since we don't need to
14471 know the size of the anonymous object. */
14472 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14473 + attr->constant_value (0)));
14474 }
14475 else
14476 {
14477 /* For little endian bits, compute the bit offset to the
14478 MSB of the anonymous object, subtract off the number of
14479 bits from the MSB of the field to the MSB of the
14480 object, and then subtract off the number of bits of
14481 the field itself. The result is the bit offset of
14482 the LSB of the field. */
14483 int anonymous_size;
14484 int bit_offset = attr->constant_value (0);
14485
14486 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14487 if (attr != nullptr && attr->form_is_constant ())
14488 {
14489 /* The size of the anonymous object containing
14490 the bit field is explicit, so use the
14491 indicated size (in bytes). */
14492 anonymous_size = attr->constant_value (0);
14493 }
14494 else
14495 {
14496 /* The size of the anonymous object containing
14497 the bit field must be inferred from the type
14498 attribute of the data member containing the
14499 bit field. */
14500 anonymous_size = TYPE_LENGTH (fp->type ());
14501 }
14502 SET_FIELD_BITPOS (*fp,
14503 (FIELD_BITPOS (*fp)
14504 + anonymous_size * bits_per_byte
14505 - bit_offset - FIELD_BITSIZE (*fp)));
14506 }
14507 }
14508 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14509 if (attr != NULL)
14510 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14511 + attr->constant_value (0)));
14512
14513 /* Get name of field. */
14514 fieldname = dwarf2_name (die, cu);
14515 if (fieldname == NULL)
14516 fieldname = "";
14517
14518 /* The name is already allocated along with this objfile, so we don't
14519 need to duplicate it for the type. */
14520 fp->name = fieldname;
14521
14522 /* Change accessibility for artificial fields (e.g. virtual table
14523 pointer or virtual base class pointer) to private. */
14524 if (dwarf2_attr (die, DW_AT_artificial, cu))
14525 {
14526 FIELD_ARTIFICIAL (*fp) = 1;
14527 new_field->accessibility = DW_ACCESS_private;
14528 fip->non_public_fields = true;
14529 }
14530 }
14531 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14532 {
14533 /* C++ static member. */
14534
14535 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14536 is a declaration, but all versions of G++ as of this writing
14537 (so through at least 3.2.1) incorrectly generate
14538 DW_TAG_variable tags. */
14539
14540 const char *physname;
14541
14542 /* Get name of field. */
14543 fieldname = dwarf2_name (die, cu);
14544 if (fieldname == NULL)
14545 return;
14546
14547 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14548 if (attr
14549 /* Only create a symbol if this is an external value.
14550 new_symbol checks this and puts the value in the global symbol
14551 table, which we want. If it is not external, new_symbol
14552 will try to put the value in cu->list_in_scope which is wrong. */
14553 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14554 {
14555 /* A static const member, not much different than an enum as far as
14556 we're concerned, except that we can support more types. */
14557 new_symbol (die, NULL, cu);
14558 }
14559
14560 /* Get physical name. */
14561 physname = dwarf2_physname (fieldname, die, cu);
14562
14563 /* The name is already allocated along with this objfile, so we don't
14564 need to duplicate it for the type. */
14565 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14566 fp->set_type (die_type (die, cu));
14567 FIELD_NAME (*fp) = fieldname;
14568 }
14569 else if (die->tag == DW_TAG_inheritance)
14570 {
14571 /* C++ base class field. */
14572 handle_data_member_location (die, cu, fp);
14573 FIELD_BITSIZE (*fp) = 0;
14574 fp->set_type (die_type (die, cu));
14575 FIELD_NAME (*fp) = fp->type ()->name ();
14576 }
14577 else
14578 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14579 }
14580
14581 /* Can the type given by DIE define another type? */
14582
14583 static bool
14584 type_can_define_types (const struct die_info *die)
14585 {
14586 switch (die->tag)
14587 {
14588 case DW_TAG_typedef:
14589 case DW_TAG_class_type:
14590 case DW_TAG_structure_type:
14591 case DW_TAG_union_type:
14592 case DW_TAG_enumeration_type:
14593 return true;
14594
14595 default:
14596 return false;
14597 }
14598 }
14599
14600 /* Add a type definition defined in the scope of the FIP's class. */
14601
14602 static void
14603 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14604 struct dwarf2_cu *cu)
14605 {
14606 struct decl_field fp;
14607 memset (&fp, 0, sizeof (fp));
14608
14609 gdb_assert (type_can_define_types (die));
14610
14611 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14612 fp.name = dwarf2_name (die, cu);
14613 fp.type = read_type_die (die, cu);
14614
14615 /* Save accessibility. */
14616 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14617 switch (accessibility)
14618 {
14619 case DW_ACCESS_public:
14620 /* The assumed value if neither private nor protected. */
14621 break;
14622 case DW_ACCESS_private:
14623 fp.is_private = 1;
14624 break;
14625 case DW_ACCESS_protected:
14626 fp.is_protected = 1;
14627 break;
14628 }
14629
14630 if (die->tag == DW_TAG_typedef)
14631 fip->typedef_field_list.push_back (fp);
14632 else
14633 fip->nested_types_list.push_back (fp);
14634 }
14635
14636 /* A convenience typedef that's used when finding the discriminant
14637 field for a variant part. */
14638 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14639 offset_map_type;
14640
14641 /* Compute the discriminant range for a given variant. OBSTACK is
14642 where the results will be stored. VARIANT is the variant to
14643 process. IS_UNSIGNED indicates whether the discriminant is signed
14644 or unsigned. */
14645
14646 static const gdb::array_view<discriminant_range>
14647 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14648 bool is_unsigned)
14649 {
14650 std::vector<discriminant_range> ranges;
14651
14652 if (variant.default_branch)
14653 return {};
14654
14655 if (variant.discr_list_data == nullptr)
14656 {
14657 discriminant_range r
14658 = {variant.discriminant_value, variant.discriminant_value};
14659 ranges.push_back (r);
14660 }
14661 else
14662 {
14663 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14664 variant.discr_list_data->size);
14665 while (!data.empty ())
14666 {
14667 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14668 {
14669 complaint (_("invalid discriminant marker: %d"), data[0]);
14670 break;
14671 }
14672 bool is_range = data[0] == DW_DSC_range;
14673 data = data.slice (1);
14674
14675 ULONGEST low, high;
14676 unsigned int bytes_read;
14677
14678 if (data.empty ())
14679 {
14680 complaint (_("DW_AT_discr_list missing low value"));
14681 break;
14682 }
14683 if (is_unsigned)
14684 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14685 else
14686 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14687 &bytes_read);
14688 data = data.slice (bytes_read);
14689
14690 if (is_range)
14691 {
14692 if (data.empty ())
14693 {
14694 complaint (_("DW_AT_discr_list missing high value"));
14695 break;
14696 }
14697 if (is_unsigned)
14698 high = read_unsigned_leb128 (nullptr, data.data (),
14699 &bytes_read);
14700 else
14701 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14702 &bytes_read);
14703 data = data.slice (bytes_read);
14704 }
14705 else
14706 high = low;
14707
14708 ranges.push_back ({ low, high });
14709 }
14710 }
14711
14712 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14713 ranges.size ());
14714 std::copy (ranges.begin (), ranges.end (), result);
14715 return gdb::array_view<discriminant_range> (result, ranges.size ());
14716 }
14717
14718 static const gdb::array_view<variant_part> create_variant_parts
14719 (struct obstack *obstack,
14720 const offset_map_type &offset_map,
14721 struct field_info *fi,
14722 const std::vector<variant_part_builder> &variant_parts);
14723
14724 /* Fill in a "struct variant" for a given variant field. RESULT is
14725 the variant to fill in. OBSTACK is where any needed allocations
14726 will be done. OFFSET_MAP holds the mapping from section offsets to
14727 fields for the type. FI describes the fields of the type we're
14728 processing. FIELD is the variant field we're converting. */
14729
14730 static void
14731 create_one_variant (variant &result, struct obstack *obstack,
14732 const offset_map_type &offset_map,
14733 struct field_info *fi, const variant_field &field)
14734 {
14735 result.discriminants = convert_variant_range (obstack, field, false);
14736 result.first_field = field.first_field + fi->baseclasses.size ();
14737 result.last_field = field.last_field + fi->baseclasses.size ();
14738 result.parts = create_variant_parts (obstack, offset_map, fi,
14739 field.variant_parts);
14740 }
14741
14742 /* Fill in a "struct variant_part" for a given variant part. RESULT
14743 is the variant part to fill in. OBSTACK is where any needed
14744 allocations will be done. OFFSET_MAP holds the mapping from
14745 section offsets to fields for the type. FI describes the fields of
14746 the type we're processing. BUILDER is the variant part to be
14747 converted. */
14748
14749 static void
14750 create_one_variant_part (variant_part &result,
14751 struct obstack *obstack,
14752 const offset_map_type &offset_map,
14753 struct field_info *fi,
14754 const variant_part_builder &builder)
14755 {
14756 auto iter = offset_map.find (builder.discriminant_offset);
14757 if (iter == offset_map.end ())
14758 {
14759 result.discriminant_index = -1;
14760 /* Doesn't matter. */
14761 result.is_unsigned = false;
14762 }
14763 else
14764 {
14765 result.discriminant_index = iter->second;
14766 result.is_unsigned
14767 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14768 }
14769
14770 size_t n = builder.variants.size ();
14771 variant *output = new (obstack) variant[n];
14772 for (size_t i = 0; i < n; ++i)
14773 create_one_variant (output[i], obstack, offset_map, fi,
14774 builder.variants[i]);
14775
14776 result.variants = gdb::array_view<variant> (output, n);
14777 }
14778
14779 /* Create a vector of variant parts that can be attached to a type.
14780 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14781 holds the mapping from section offsets to fields for the type. FI
14782 describes the fields of the type we're processing. VARIANT_PARTS
14783 is the vector to convert. */
14784
14785 static const gdb::array_view<variant_part>
14786 create_variant_parts (struct obstack *obstack,
14787 const offset_map_type &offset_map,
14788 struct field_info *fi,
14789 const std::vector<variant_part_builder> &variant_parts)
14790 {
14791 if (variant_parts.empty ())
14792 return {};
14793
14794 size_t n = variant_parts.size ();
14795 variant_part *result = new (obstack) variant_part[n];
14796 for (size_t i = 0; i < n; ++i)
14797 create_one_variant_part (result[i], obstack, offset_map, fi,
14798 variant_parts[i]);
14799
14800 return gdb::array_view<variant_part> (result, n);
14801 }
14802
14803 /* Compute the variant part vector for FIP, attaching it to TYPE when
14804 done. */
14805
14806 static void
14807 add_variant_property (struct field_info *fip, struct type *type,
14808 struct dwarf2_cu *cu)
14809 {
14810 /* Map section offsets of fields to their field index. Note the
14811 field index here does not take the number of baseclasses into
14812 account. */
14813 offset_map_type offset_map;
14814 for (int i = 0; i < fip->fields.size (); ++i)
14815 offset_map[fip->fields[i].offset] = i;
14816
14817 struct objfile *objfile = cu->per_objfile->objfile;
14818 gdb::array_view<variant_part> parts
14819 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14820 fip->variant_parts);
14821
14822 struct dynamic_prop prop;
14823 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14824 obstack_copy (&objfile->objfile_obstack, &parts,
14825 sizeof (parts)));
14826
14827 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14828 }
14829
14830 /* Create the vector of fields, and attach it to the type. */
14831
14832 static void
14833 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14834 struct dwarf2_cu *cu)
14835 {
14836 int nfields = fip->nfields ();
14837
14838 /* Record the field count, allocate space for the array of fields,
14839 and create blank accessibility bitfields if necessary. */
14840 type->set_num_fields (nfields);
14841 type->set_fields
14842 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14843
14844 if (fip->non_public_fields && cu->language != language_ada)
14845 {
14846 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14847
14848 TYPE_FIELD_PRIVATE_BITS (type) =
14849 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14850 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14851
14852 TYPE_FIELD_PROTECTED_BITS (type) =
14853 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14854 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14855
14856 TYPE_FIELD_IGNORE_BITS (type) =
14857 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14858 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14859 }
14860
14861 /* If the type has baseclasses, allocate and clear a bit vector for
14862 TYPE_FIELD_VIRTUAL_BITS. */
14863 if (!fip->baseclasses.empty () && cu->language != language_ada)
14864 {
14865 int num_bytes = B_BYTES (fip->baseclasses.size ());
14866 unsigned char *pointer;
14867
14868 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14869 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14870 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14871 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14872 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14873 }
14874
14875 if (!fip->variant_parts.empty ())
14876 add_variant_property (fip, type, cu);
14877
14878 /* Copy the saved-up fields into the field vector. */
14879 for (int i = 0; i < nfields; ++i)
14880 {
14881 struct nextfield &field
14882 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14883 : fip->fields[i - fip->baseclasses.size ()]);
14884
14885 type->field (i) = field.field;
14886 switch (field.accessibility)
14887 {
14888 case DW_ACCESS_private:
14889 if (cu->language != language_ada)
14890 SET_TYPE_FIELD_PRIVATE (type, i);
14891 break;
14892
14893 case DW_ACCESS_protected:
14894 if (cu->language != language_ada)
14895 SET_TYPE_FIELD_PROTECTED (type, i);
14896 break;
14897
14898 case DW_ACCESS_public:
14899 break;
14900
14901 default:
14902 /* Unknown accessibility. Complain and treat it as public. */
14903 {
14904 complaint (_("unsupported accessibility %d"),
14905 field.accessibility);
14906 }
14907 break;
14908 }
14909 if (i < fip->baseclasses.size ())
14910 {
14911 switch (field.virtuality)
14912 {
14913 case DW_VIRTUALITY_virtual:
14914 case DW_VIRTUALITY_pure_virtual:
14915 if (cu->language == language_ada)
14916 error (_("unexpected virtuality in component of Ada type"));
14917 SET_TYPE_FIELD_VIRTUAL (type, i);
14918 break;
14919 }
14920 }
14921 }
14922 }
14923
14924 /* Return true if this member function is a constructor, false
14925 otherwise. */
14926
14927 static int
14928 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14929 {
14930 const char *fieldname;
14931 const char *type_name;
14932 int len;
14933
14934 if (die->parent == NULL)
14935 return 0;
14936
14937 if (die->parent->tag != DW_TAG_structure_type
14938 && die->parent->tag != DW_TAG_union_type
14939 && die->parent->tag != DW_TAG_class_type)
14940 return 0;
14941
14942 fieldname = dwarf2_name (die, cu);
14943 type_name = dwarf2_name (die->parent, cu);
14944 if (fieldname == NULL || type_name == NULL)
14945 return 0;
14946
14947 len = strlen (fieldname);
14948 return (strncmp (fieldname, type_name, len) == 0
14949 && (type_name[len] == '\0' || type_name[len] == '<'));
14950 }
14951
14952 /* Add a member function to the proper fieldlist. */
14953
14954 static void
14955 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14956 struct type *type, struct dwarf2_cu *cu)
14957 {
14958 struct objfile *objfile = cu->per_objfile->objfile;
14959 struct attribute *attr;
14960 int i;
14961 struct fnfieldlist *flp = nullptr;
14962 struct fn_field *fnp;
14963 const char *fieldname;
14964 struct type *this_type;
14965
14966 if (cu->language == language_ada)
14967 error (_("unexpected member function in Ada type"));
14968
14969 /* Get name of member function. */
14970 fieldname = dwarf2_name (die, cu);
14971 if (fieldname == NULL)
14972 return;
14973
14974 /* Look up member function name in fieldlist. */
14975 for (i = 0; i < fip->fnfieldlists.size (); i++)
14976 {
14977 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14978 {
14979 flp = &fip->fnfieldlists[i];
14980 break;
14981 }
14982 }
14983
14984 /* Create a new fnfieldlist if necessary. */
14985 if (flp == nullptr)
14986 {
14987 fip->fnfieldlists.emplace_back ();
14988 flp = &fip->fnfieldlists.back ();
14989 flp->name = fieldname;
14990 i = fip->fnfieldlists.size () - 1;
14991 }
14992
14993 /* Create a new member function field and add it to the vector of
14994 fnfieldlists. */
14995 flp->fnfields.emplace_back ();
14996 fnp = &flp->fnfields.back ();
14997
14998 /* Delay processing of the physname until later. */
14999 if (cu->language == language_cplus)
15000 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15001 die, cu);
15002 else
15003 {
15004 const char *physname = dwarf2_physname (fieldname, die, cu);
15005 fnp->physname = physname ? physname : "";
15006 }
15007
15008 fnp->type = alloc_type (objfile);
15009 this_type = read_type_die (die, cu);
15010 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15011 {
15012 int nparams = this_type->num_fields ();
15013
15014 /* TYPE is the domain of this method, and THIS_TYPE is the type
15015 of the method itself (TYPE_CODE_METHOD). */
15016 smash_to_method_type (fnp->type, type,
15017 TYPE_TARGET_TYPE (this_type),
15018 this_type->fields (),
15019 this_type->num_fields (),
15020 this_type->has_varargs ());
15021
15022 /* Handle static member functions.
15023 Dwarf2 has no clean way to discern C++ static and non-static
15024 member functions. G++ helps GDB by marking the first
15025 parameter for non-static member functions (which is the this
15026 pointer) as artificial. We obtain this information from
15027 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15028 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15029 fnp->voffset = VOFFSET_STATIC;
15030 }
15031 else
15032 complaint (_("member function type missing for '%s'"),
15033 dwarf2_full_name (fieldname, die, cu));
15034
15035 /* Get fcontext from DW_AT_containing_type if present. */
15036 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15037 fnp->fcontext = die_containing_type (die, cu);
15038
15039 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15040 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15041
15042 /* Get accessibility. */
15043 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15044 switch (accessibility)
15045 {
15046 case DW_ACCESS_private:
15047 fnp->is_private = 1;
15048 break;
15049 case DW_ACCESS_protected:
15050 fnp->is_protected = 1;
15051 break;
15052 }
15053
15054 /* Check for artificial methods. */
15055 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15056 if (attr && attr->as_boolean ())
15057 fnp->is_artificial = 1;
15058
15059 /* Check for defaulted methods. */
15060 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15061 if (attr != nullptr)
15062 fnp->defaulted = attr->defaulted ();
15063
15064 /* Check for deleted methods. */
15065 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15066 if (attr != nullptr && attr->as_boolean ())
15067 fnp->is_deleted = 1;
15068
15069 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15070
15071 /* Get index in virtual function table if it is a virtual member
15072 function. For older versions of GCC, this is an offset in the
15073 appropriate virtual table, as specified by DW_AT_containing_type.
15074 For everyone else, it is an expression to be evaluated relative
15075 to the object address. */
15076
15077 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15078 if (attr != nullptr)
15079 {
15080 if (attr->form_is_block () && attr->as_block ()->size > 0)
15081 {
15082 struct dwarf_block *block = attr->as_block ();
15083
15084 if (block->data[0] == DW_OP_constu)
15085 {
15086 /* Old-style GCC. */
15087 fnp->voffset = decode_locdesc (block, cu) + 2;
15088 }
15089 else if (block->data[0] == DW_OP_deref
15090 || (block->size > 1
15091 && block->data[0] == DW_OP_deref_size
15092 && block->data[1] == cu->header.addr_size))
15093 {
15094 fnp->voffset = decode_locdesc (block, cu);
15095 if ((fnp->voffset % cu->header.addr_size) != 0)
15096 dwarf2_complex_location_expr_complaint ();
15097 else
15098 fnp->voffset /= cu->header.addr_size;
15099 fnp->voffset += 2;
15100 }
15101 else
15102 dwarf2_complex_location_expr_complaint ();
15103
15104 if (!fnp->fcontext)
15105 {
15106 /* If there is no `this' field and no DW_AT_containing_type,
15107 we cannot actually find a base class context for the
15108 vtable! */
15109 if (this_type->num_fields () == 0
15110 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15111 {
15112 complaint (_("cannot determine context for virtual member "
15113 "function \"%s\" (offset %s)"),
15114 fieldname, sect_offset_str (die->sect_off));
15115 }
15116 else
15117 {
15118 fnp->fcontext
15119 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15120 }
15121 }
15122 }
15123 else if (attr->form_is_section_offset ())
15124 {
15125 dwarf2_complex_location_expr_complaint ();
15126 }
15127 else
15128 {
15129 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15130 fieldname);
15131 }
15132 }
15133 else
15134 {
15135 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15136 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15137 {
15138 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15139 complaint (_("Member function \"%s\" (offset %s) is virtual "
15140 "but the vtable offset is not specified"),
15141 fieldname, sect_offset_str (die->sect_off));
15142 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15143 TYPE_CPLUS_DYNAMIC (type) = 1;
15144 }
15145 }
15146 }
15147
15148 /* Create the vector of member function fields, and attach it to the type. */
15149
15150 static void
15151 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15152 struct dwarf2_cu *cu)
15153 {
15154 if (cu->language == language_ada)
15155 error (_("unexpected member functions in Ada type"));
15156
15157 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15158 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15159 TYPE_ALLOC (type,
15160 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15161
15162 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15163 {
15164 struct fnfieldlist &nf = fip->fnfieldlists[i];
15165 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15166
15167 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15168 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15169 fn_flp->fn_fields = (struct fn_field *)
15170 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15171
15172 for (int k = 0; k < nf.fnfields.size (); ++k)
15173 fn_flp->fn_fields[k] = nf.fnfields[k];
15174 }
15175
15176 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15177 }
15178
15179 /* Returns non-zero if NAME is the name of a vtable member in CU's
15180 language, zero otherwise. */
15181 static int
15182 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15183 {
15184 static const char vptr[] = "_vptr";
15185
15186 /* Look for the C++ form of the vtable. */
15187 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15188 return 1;
15189
15190 return 0;
15191 }
15192
15193 /* GCC outputs unnamed structures that are really pointers to member
15194 functions, with the ABI-specified layout. If TYPE describes
15195 such a structure, smash it into a member function type.
15196
15197 GCC shouldn't do this; it should just output pointer to member DIEs.
15198 This is GCC PR debug/28767. */
15199
15200 static void
15201 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15202 {
15203 struct type *pfn_type, *self_type, *new_type;
15204
15205 /* Check for a structure with no name and two children. */
15206 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15207 return;
15208
15209 /* Check for __pfn and __delta members. */
15210 if (TYPE_FIELD_NAME (type, 0) == NULL
15211 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15212 || TYPE_FIELD_NAME (type, 1) == NULL
15213 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15214 return;
15215
15216 /* Find the type of the method. */
15217 pfn_type = type->field (0).type ();
15218 if (pfn_type == NULL
15219 || pfn_type->code () != TYPE_CODE_PTR
15220 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15221 return;
15222
15223 /* Look for the "this" argument. */
15224 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15225 if (pfn_type->num_fields () == 0
15226 /* || pfn_type->field (0).type () == NULL */
15227 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15228 return;
15229
15230 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15231 new_type = alloc_type (objfile);
15232 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15233 pfn_type->fields (), pfn_type->num_fields (),
15234 pfn_type->has_varargs ());
15235 smash_to_methodptr_type (type, new_type);
15236 }
15237
15238 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15239 requires rewriting, then copy it and return the updated copy.
15240 Otherwise return nullptr. */
15241
15242 static struct type *
15243 rewrite_array_type (struct type *type)
15244 {
15245 if (type->code () != TYPE_CODE_ARRAY)
15246 return nullptr;
15247
15248 struct type *index_type = type->index_type ();
15249 range_bounds *current_bounds = index_type->bounds ();
15250
15251 /* Handle multi-dimensional arrays. */
15252 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15253 if (new_target == nullptr)
15254 {
15255 /* Maybe we don't need to rewrite this array. */
15256 if (current_bounds->low.kind () == PROP_CONST
15257 && current_bounds->high.kind () == PROP_CONST)
15258 return nullptr;
15259 }
15260
15261 /* Either the target type was rewritten, or the bounds have to be
15262 updated. Either way we want to copy the type and update
15263 everything. */
15264 struct type *copy = copy_type (type);
15265 int nfields = copy->num_fields ();
15266 field *new_fields
15267 = ((struct field *) TYPE_ZALLOC (copy,
15268 nfields * sizeof (struct field)));
15269 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15270 copy->set_fields (new_fields);
15271 if (new_target != nullptr)
15272 TYPE_TARGET_TYPE (copy) = new_target;
15273
15274 struct type *index_copy = copy_type (index_type);
15275 range_bounds *bounds
15276 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15277 sizeof (range_bounds));
15278 *bounds = *current_bounds;
15279 bounds->low.set_const_val (1);
15280 bounds->high.set_const_val (0);
15281 index_copy->set_bounds (bounds);
15282 copy->set_index_type (index_copy);
15283
15284 return copy;
15285 }
15286
15287 /* While some versions of GCC will generate complicated DWARF for an
15288 array (see quirk_ada_thick_pointer), more recent versions were
15289 modified to emit an explicit thick pointer structure. However, in
15290 this case, the array still has DWARF expressions for its ranges,
15291 and these must be ignored. */
15292
15293 static void
15294 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15295 struct type *type)
15296 {
15297 gdb_assert (cu->language == language_ada);
15298
15299 /* Check for a structure with two children. */
15300 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15301 return;
15302
15303 /* Check for P_ARRAY and P_BOUNDS members. */
15304 if (TYPE_FIELD_NAME (type, 0) == NULL
15305 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15306 || TYPE_FIELD_NAME (type, 1) == NULL
15307 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15308 return;
15309
15310 /* Make sure we're looking at a pointer to an array. */
15311 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15312 return;
15313
15314 /* The Ada code already knows how to handle these types, so all that
15315 we need to do is turn the bounds into static bounds. However, we
15316 don't want to rewrite existing array or index types in-place,
15317 because those may be referenced in other contexts where this
15318 rewriting is undesirable. */
15319 struct type *new_ary_type
15320 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15321 if (new_ary_type != nullptr)
15322 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15323 }
15324
15325 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15326 appropriate error checking and issuing complaints if there is a
15327 problem. */
15328
15329 static ULONGEST
15330 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15331 {
15332 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15333
15334 if (attr == nullptr)
15335 return 0;
15336
15337 if (!attr->form_is_constant ())
15338 {
15339 complaint (_("DW_AT_alignment must have constant form"
15340 " - DIE at %s [in module %s]"),
15341 sect_offset_str (die->sect_off),
15342 objfile_name (cu->per_objfile->objfile));
15343 return 0;
15344 }
15345
15346 LONGEST val = attr->constant_value (0);
15347 if (val < 0)
15348 {
15349 complaint (_("DW_AT_alignment value must not be negative"
15350 " - DIE at %s [in module %s]"),
15351 sect_offset_str (die->sect_off),
15352 objfile_name (cu->per_objfile->objfile));
15353 return 0;
15354 }
15355 ULONGEST align = val;
15356
15357 if (align == 0)
15358 {
15359 complaint (_("DW_AT_alignment value must not be zero"
15360 " - DIE at %s [in module %s]"),
15361 sect_offset_str (die->sect_off),
15362 objfile_name (cu->per_objfile->objfile));
15363 return 0;
15364 }
15365 if ((align & (align - 1)) != 0)
15366 {
15367 complaint (_("DW_AT_alignment value must be a power of 2"
15368 " - DIE at %s [in module %s]"),
15369 sect_offset_str (die->sect_off),
15370 objfile_name (cu->per_objfile->objfile));
15371 return 0;
15372 }
15373
15374 return align;
15375 }
15376
15377 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15378 the alignment for TYPE. */
15379
15380 static void
15381 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15382 struct type *type)
15383 {
15384 if (!set_type_align (type, get_alignment (cu, die)))
15385 complaint (_("DW_AT_alignment value too large"
15386 " - DIE at %s [in module %s]"),
15387 sect_offset_str (die->sect_off),
15388 objfile_name (cu->per_objfile->objfile));
15389 }
15390
15391 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15392 constant for a type, according to DWARF5 spec, Table 5.5. */
15393
15394 static bool
15395 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15396 {
15397 switch (value)
15398 {
15399 case DW_CC_normal:
15400 case DW_CC_pass_by_reference:
15401 case DW_CC_pass_by_value:
15402 return true;
15403
15404 default:
15405 complaint (_("unrecognized DW_AT_calling_convention value "
15406 "(%s) for a type"), pulongest (value));
15407 return false;
15408 }
15409 }
15410
15411 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15412 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15413 also according to GNU-specific values (see include/dwarf2.h). */
15414
15415 static bool
15416 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15417 {
15418 switch (value)
15419 {
15420 case DW_CC_normal:
15421 case DW_CC_program:
15422 case DW_CC_nocall:
15423 return true;
15424
15425 case DW_CC_GNU_renesas_sh:
15426 case DW_CC_GNU_borland_fastcall_i386:
15427 case DW_CC_GDB_IBM_OpenCL:
15428 return true;
15429
15430 default:
15431 complaint (_("unrecognized DW_AT_calling_convention value "
15432 "(%s) for a subroutine"), pulongest (value));
15433 return false;
15434 }
15435 }
15436
15437 /* Called when we find the DIE that starts a structure or union scope
15438 (definition) to create a type for the structure or union. Fill in
15439 the type's name and general properties; the members will not be
15440 processed until process_structure_scope. A symbol table entry for
15441 the type will also not be done until process_structure_scope (assuming
15442 the type has a name).
15443
15444 NOTE: we need to call these functions regardless of whether or not the
15445 DIE has a DW_AT_name attribute, since it might be an anonymous
15446 structure or union. This gets the type entered into our set of
15447 user defined types. */
15448
15449 static struct type *
15450 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15451 {
15452 struct objfile *objfile = cu->per_objfile->objfile;
15453 struct type *type;
15454 struct attribute *attr;
15455 const char *name;
15456
15457 /* If the definition of this type lives in .debug_types, read that type.
15458 Don't follow DW_AT_specification though, that will take us back up
15459 the chain and we want to go down. */
15460 attr = die->attr (DW_AT_signature);
15461 if (attr != nullptr)
15462 {
15463 type = get_DW_AT_signature_type (die, attr, cu);
15464
15465 /* The type's CU may not be the same as CU.
15466 Ensure TYPE is recorded with CU in die_type_hash. */
15467 return set_die_type (die, type, cu);
15468 }
15469
15470 type = alloc_type (objfile);
15471 INIT_CPLUS_SPECIFIC (type);
15472
15473 name = dwarf2_name (die, cu);
15474 if (name != NULL)
15475 {
15476 if (cu->language == language_cplus
15477 || cu->language == language_d
15478 || cu->language == language_rust)
15479 {
15480 const char *full_name = dwarf2_full_name (name, die, cu);
15481
15482 /* dwarf2_full_name might have already finished building the DIE's
15483 type. If so, there is no need to continue. */
15484 if (get_die_type (die, cu) != NULL)
15485 return get_die_type (die, cu);
15486
15487 type->set_name (full_name);
15488 }
15489 else
15490 {
15491 /* The name is already allocated along with this objfile, so
15492 we don't need to duplicate it for the type. */
15493 type->set_name (name);
15494 }
15495 }
15496
15497 if (die->tag == DW_TAG_structure_type)
15498 {
15499 type->set_code (TYPE_CODE_STRUCT);
15500 }
15501 else if (die->tag == DW_TAG_union_type)
15502 {
15503 type->set_code (TYPE_CODE_UNION);
15504 }
15505 else
15506 {
15507 type->set_code (TYPE_CODE_STRUCT);
15508 }
15509
15510 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15511 type->set_is_declared_class (true);
15512
15513 /* Store the calling convention in the type if it's available in
15514 the die. Otherwise the calling convention remains set to
15515 the default value DW_CC_normal. */
15516 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15517 if (attr != nullptr
15518 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15519 {
15520 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15521 TYPE_CPLUS_CALLING_CONVENTION (type)
15522 = (enum dwarf_calling_convention) (attr->constant_value (0));
15523 }
15524
15525 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15526 if (attr != nullptr)
15527 {
15528 if (attr->form_is_constant ())
15529 TYPE_LENGTH (type) = attr->constant_value (0);
15530 else
15531 {
15532 struct dynamic_prop prop;
15533 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15534 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15535 TYPE_LENGTH (type) = 0;
15536 }
15537 }
15538 else
15539 {
15540 TYPE_LENGTH (type) = 0;
15541 }
15542
15543 maybe_set_alignment (cu, die, type);
15544
15545 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15546 {
15547 /* ICC<14 does not output the required DW_AT_declaration on
15548 incomplete types, but gives them a size of zero. */
15549 type->set_is_stub (true);
15550 }
15551 else
15552 type->set_stub_is_supported (true);
15553
15554 if (die_is_declaration (die, cu))
15555 type->set_is_stub (true);
15556 else if (attr == NULL && die->child == NULL
15557 && producer_is_realview (cu->producer))
15558 /* RealView does not output the required DW_AT_declaration
15559 on incomplete types. */
15560 type->set_is_stub (true);
15561
15562 /* We need to add the type field to the die immediately so we don't
15563 infinitely recurse when dealing with pointers to the structure
15564 type within the structure itself. */
15565 set_die_type (die, type, cu);
15566
15567 /* set_die_type should be already done. */
15568 set_descriptive_type (type, die, cu);
15569
15570 return type;
15571 }
15572
15573 static void handle_struct_member_die
15574 (struct die_info *child_die,
15575 struct type *type,
15576 struct field_info *fi,
15577 std::vector<struct symbol *> *template_args,
15578 struct dwarf2_cu *cu);
15579
15580 /* A helper for handle_struct_member_die that handles
15581 DW_TAG_variant_part. */
15582
15583 static void
15584 handle_variant_part (struct die_info *die, struct type *type,
15585 struct field_info *fi,
15586 std::vector<struct symbol *> *template_args,
15587 struct dwarf2_cu *cu)
15588 {
15589 variant_part_builder *new_part;
15590 if (fi->current_variant_part == nullptr)
15591 {
15592 fi->variant_parts.emplace_back ();
15593 new_part = &fi->variant_parts.back ();
15594 }
15595 else if (!fi->current_variant_part->processing_variant)
15596 {
15597 complaint (_("nested DW_TAG_variant_part seen "
15598 "- DIE at %s [in module %s]"),
15599 sect_offset_str (die->sect_off),
15600 objfile_name (cu->per_objfile->objfile));
15601 return;
15602 }
15603 else
15604 {
15605 variant_field &current = fi->current_variant_part->variants.back ();
15606 current.variant_parts.emplace_back ();
15607 new_part = &current.variant_parts.back ();
15608 }
15609
15610 /* When we recurse, we want callees to add to this new variant
15611 part. */
15612 scoped_restore save_current_variant_part
15613 = make_scoped_restore (&fi->current_variant_part, new_part);
15614
15615 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15616 if (discr == NULL)
15617 {
15618 /* It's a univariant form, an extension we support. */
15619 }
15620 else if (discr->form_is_ref ())
15621 {
15622 struct dwarf2_cu *target_cu = cu;
15623 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15624
15625 new_part->discriminant_offset = target_die->sect_off;
15626 }
15627 else
15628 {
15629 complaint (_("DW_AT_discr does not have DIE reference form"
15630 " - DIE at %s [in module %s]"),
15631 sect_offset_str (die->sect_off),
15632 objfile_name (cu->per_objfile->objfile));
15633 }
15634
15635 for (die_info *child_die = die->child;
15636 child_die != NULL;
15637 child_die = child_die->sibling)
15638 handle_struct_member_die (child_die, type, fi, template_args, cu);
15639 }
15640
15641 /* A helper for handle_struct_member_die that handles
15642 DW_TAG_variant. */
15643
15644 static void
15645 handle_variant (struct die_info *die, struct type *type,
15646 struct field_info *fi,
15647 std::vector<struct symbol *> *template_args,
15648 struct dwarf2_cu *cu)
15649 {
15650 if (fi->current_variant_part == nullptr)
15651 {
15652 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15653 "- DIE at %s [in module %s]"),
15654 sect_offset_str (die->sect_off),
15655 objfile_name (cu->per_objfile->objfile));
15656 return;
15657 }
15658 if (fi->current_variant_part->processing_variant)
15659 {
15660 complaint (_("nested DW_TAG_variant seen "
15661 "- DIE at %s [in module %s]"),
15662 sect_offset_str (die->sect_off),
15663 objfile_name (cu->per_objfile->objfile));
15664 return;
15665 }
15666
15667 scoped_restore save_processing_variant
15668 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15669 true);
15670
15671 fi->current_variant_part->variants.emplace_back ();
15672 variant_field &variant = fi->current_variant_part->variants.back ();
15673 variant.first_field = fi->fields.size ();
15674
15675 /* In a variant we want to get the discriminant and also add a
15676 field for our sole member child. */
15677 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15678 if (discr == nullptr || !discr->form_is_constant ())
15679 {
15680 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15681 if (discr == nullptr || discr->as_block ()->size == 0)
15682 variant.default_branch = true;
15683 else
15684 variant.discr_list_data = discr->as_block ();
15685 }
15686 else
15687 variant.discriminant_value = discr->constant_value (0);
15688
15689 for (die_info *variant_child = die->child;
15690 variant_child != NULL;
15691 variant_child = variant_child->sibling)
15692 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15693
15694 variant.last_field = fi->fields.size ();
15695 }
15696
15697 /* A helper for process_structure_scope that handles a single member
15698 DIE. */
15699
15700 static void
15701 handle_struct_member_die (struct die_info *child_die, struct type *type,
15702 struct field_info *fi,
15703 std::vector<struct symbol *> *template_args,
15704 struct dwarf2_cu *cu)
15705 {
15706 if (child_die->tag == DW_TAG_member
15707 || child_die->tag == DW_TAG_variable)
15708 {
15709 /* NOTE: carlton/2002-11-05: A C++ static data member
15710 should be a DW_TAG_member that is a declaration, but
15711 all versions of G++ as of this writing (so through at
15712 least 3.2.1) incorrectly generate DW_TAG_variable
15713 tags for them instead. */
15714 dwarf2_add_field (fi, child_die, cu);
15715 }
15716 else if (child_die->tag == DW_TAG_subprogram)
15717 {
15718 /* Rust doesn't have member functions in the C++ sense.
15719 However, it does emit ordinary functions as children
15720 of a struct DIE. */
15721 if (cu->language == language_rust)
15722 read_func_scope (child_die, cu);
15723 else
15724 {
15725 /* C++ member function. */
15726 dwarf2_add_member_fn (fi, child_die, type, cu);
15727 }
15728 }
15729 else if (child_die->tag == DW_TAG_inheritance)
15730 {
15731 /* C++ base class field. */
15732 dwarf2_add_field (fi, child_die, cu);
15733 }
15734 else if (type_can_define_types (child_die))
15735 dwarf2_add_type_defn (fi, child_die, cu);
15736 else if (child_die->tag == DW_TAG_template_type_param
15737 || child_die->tag == DW_TAG_template_value_param)
15738 {
15739 struct symbol *arg = new_symbol (child_die, NULL, cu);
15740
15741 if (arg != NULL)
15742 template_args->push_back (arg);
15743 }
15744 else if (child_die->tag == DW_TAG_variant_part)
15745 handle_variant_part (child_die, type, fi, template_args, cu);
15746 else if (child_die->tag == DW_TAG_variant)
15747 handle_variant (child_die, type, fi, template_args, cu);
15748 }
15749
15750 /* Finish creating a structure or union type, including filling in
15751 its members and creating a symbol for it. */
15752
15753 static void
15754 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15755 {
15756 struct objfile *objfile = cu->per_objfile->objfile;
15757 struct die_info *child_die;
15758 struct type *type;
15759
15760 type = get_die_type (die, cu);
15761 if (type == NULL)
15762 type = read_structure_type (die, cu);
15763
15764 bool has_template_parameters = false;
15765 if (die->child != NULL && ! die_is_declaration (die, cu))
15766 {
15767 struct field_info fi;
15768 std::vector<struct symbol *> template_args;
15769
15770 child_die = die->child;
15771
15772 while (child_die && child_die->tag)
15773 {
15774 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15775 child_die = child_die->sibling;
15776 }
15777
15778 /* Attach template arguments to type. */
15779 if (!template_args.empty ())
15780 {
15781 has_template_parameters = true;
15782 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15783 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15784 TYPE_TEMPLATE_ARGUMENTS (type)
15785 = XOBNEWVEC (&objfile->objfile_obstack,
15786 struct symbol *,
15787 TYPE_N_TEMPLATE_ARGUMENTS (type));
15788 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15789 template_args.data (),
15790 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15791 * sizeof (struct symbol *)));
15792 }
15793
15794 /* Attach fields and member functions to the type. */
15795 if (fi.nfields () > 0)
15796 dwarf2_attach_fields_to_type (&fi, type, cu);
15797 if (!fi.fnfieldlists.empty ())
15798 {
15799 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15800
15801 /* Get the type which refers to the base class (possibly this
15802 class itself) which contains the vtable pointer for the current
15803 class from the DW_AT_containing_type attribute. This use of
15804 DW_AT_containing_type is a GNU extension. */
15805
15806 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15807 {
15808 struct type *t = die_containing_type (die, cu);
15809
15810 set_type_vptr_basetype (type, t);
15811 if (type == t)
15812 {
15813 int i;
15814
15815 /* Our own class provides vtbl ptr. */
15816 for (i = t->num_fields () - 1;
15817 i >= TYPE_N_BASECLASSES (t);
15818 --i)
15819 {
15820 const char *fieldname = TYPE_FIELD_NAME (t, i);
15821
15822 if (is_vtable_name (fieldname, cu))
15823 {
15824 set_type_vptr_fieldno (type, i);
15825 break;
15826 }
15827 }
15828
15829 /* Complain if virtual function table field not found. */
15830 if (i < TYPE_N_BASECLASSES (t))
15831 complaint (_("virtual function table pointer "
15832 "not found when defining class '%s'"),
15833 type->name () ? type->name () : "");
15834 }
15835 else
15836 {
15837 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15838 }
15839 }
15840 else if (cu->producer
15841 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15842 {
15843 /* The IBM XLC compiler does not provide direct indication
15844 of the containing type, but the vtable pointer is
15845 always named __vfp. */
15846
15847 int i;
15848
15849 for (i = type->num_fields () - 1;
15850 i >= TYPE_N_BASECLASSES (type);
15851 --i)
15852 {
15853 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15854 {
15855 set_type_vptr_fieldno (type, i);
15856 set_type_vptr_basetype (type, type);
15857 break;
15858 }
15859 }
15860 }
15861 }
15862
15863 /* Copy fi.typedef_field_list linked list elements content into the
15864 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15865 if (!fi.typedef_field_list.empty ())
15866 {
15867 int count = fi.typedef_field_list.size ();
15868
15869 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15870 TYPE_TYPEDEF_FIELD_ARRAY (type)
15871 = ((struct decl_field *)
15872 TYPE_ALLOC (type,
15873 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15874 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15875
15876 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15877 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15878 }
15879
15880 /* Copy fi.nested_types_list linked list elements content into the
15881 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15882 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15883 {
15884 int count = fi.nested_types_list.size ();
15885
15886 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15887 TYPE_NESTED_TYPES_ARRAY (type)
15888 = ((struct decl_field *)
15889 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15890 TYPE_NESTED_TYPES_COUNT (type) = count;
15891
15892 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15893 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15894 }
15895 }
15896
15897 quirk_gcc_member_function_pointer (type, objfile);
15898 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15899 cu->rust_unions.push_back (type);
15900 else if (cu->language == language_ada)
15901 quirk_ada_thick_pointer_struct (die, cu, type);
15902
15903 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15904 snapshots) has been known to create a die giving a declaration
15905 for a class that has, as a child, a die giving a definition for a
15906 nested class. So we have to process our children even if the
15907 current die is a declaration. Normally, of course, a declaration
15908 won't have any children at all. */
15909
15910 child_die = die->child;
15911
15912 while (child_die != NULL && child_die->tag)
15913 {
15914 if (child_die->tag == DW_TAG_member
15915 || child_die->tag == DW_TAG_variable
15916 || child_die->tag == DW_TAG_inheritance
15917 || child_die->tag == DW_TAG_template_value_param
15918 || child_die->tag == DW_TAG_template_type_param)
15919 {
15920 /* Do nothing. */
15921 }
15922 else
15923 process_die (child_die, cu);
15924
15925 child_die = child_die->sibling;
15926 }
15927
15928 /* Do not consider external references. According to the DWARF standard,
15929 these DIEs are identified by the fact that they have no byte_size
15930 attribute, and a declaration attribute. */
15931 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15932 || !die_is_declaration (die, cu)
15933 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15934 {
15935 struct symbol *sym = new_symbol (die, type, cu);
15936
15937 if (has_template_parameters)
15938 {
15939 struct symtab *symtab;
15940 if (sym != nullptr)
15941 symtab = symbol_symtab (sym);
15942 else if (cu->line_header != nullptr)
15943 {
15944 /* Any related symtab will do. */
15945 symtab
15946 = cu->line_header->file_names ()[0].symtab;
15947 }
15948 else
15949 {
15950 symtab = nullptr;
15951 complaint (_("could not find suitable "
15952 "symtab for template parameter"
15953 " - DIE at %s [in module %s]"),
15954 sect_offset_str (die->sect_off),
15955 objfile_name (objfile));
15956 }
15957
15958 if (symtab != nullptr)
15959 {
15960 /* Make sure that the symtab is set on the new symbols.
15961 Even though they don't appear in this symtab directly,
15962 other parts of gdb assume that symbols do, and this is
15963 reasonably true. */
15964 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15965 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15966 }
15967 }
15968 }
15969 }
15970
15971 /* Assuming DIE is an enumeration type, and TYPE is its associated
15972 type, update TYPE using some information only available in DIE's
15973 children. In particular, the fields are computed. */
15974
15975 static void
15976 update_enumeration_type_from_children (struct die_info *die,
15977 struct type *type,
15978 struct dwarf2_cu *cu)
15979 {
15980 struct die_info *child_die;
15981 int unsigned_enum = 1;
15982 int flag_enum = 1;
15983
15984 auto_obstack obstack;
15985 std::vector<struct field> fields;
15986
15987 for (child_die = die->child;
15988 child_die != NULL && child_die->tag;
15989 child_die = child_die->sibling)
15990 {
15991 struct attribute *attr;
15992 LONGEST value;
15993 const gdb_byte *bytes;
15994 struct dwarf2_locexpr_baton *baton;
15995 const char *name;
15996
15997 if (child_die->tag != DW_TAG_enumerator)
15998 continue;
15999
16000 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16001 if (attr == NULL)
16002 continue;
16003
16004 name = dwarf2_name (child_die, cu);
16005 if (name == NULL)
16006 name = "<anonymous enumerator>";
16007
16008 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16009 &value, &bytes, &baton);
16010 if (value < 0)
16011 {
16012 unsigned_enum = 0;
16013 flag_enum = 0;
16014 }
16015 else
16016 {
16017 if (count_one_bits_ll (value) >= 2)
16018 flag_enum = 0;
16019 }
16020
16021 fields.emplace_back ();
16022 struct field &field = fields.back ();
16023 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16024 SET_FIELD_ENUMVAL (field, value);
16025 }
16026
16027 if (!fields.empty ())
16028 {
16029 type->set_num_fields (fields.size ());
16030 type->set_fields
16031 ((struct field *)
16032 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16033 memcpy (type->fields (), fields.data (),
16034 sizeof (struct field) * fields.size ());
16035 }
16036
16037 if (unsigned_enum)
16038 type->set_is_unsigned (true);
16039
16040 if (flag_enum)
16041 type->set_is_flag_enum (true);
16042 }
16043
16044 /* Given a DW_AT_enumeration_type die, set its type. We do not
16045 complete the type's fields yet, or create any symbols. */
16046
16047 static struct type *
16048 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16049 {
16050 struct objfile *objfile = cu->per_objfile->objfile;
16051 struct type *type;
16052 struct attribute *attr;
16053 const char *name;
16054
16055 /* If the definition of this type lives in .debug_types, read that type.
16056 Don't follow DW_AT_specification though, that will take us back up
16057 the chain and we want to go down. */
16058 attr = die->attr (DW_AT_signature);
16059 if (attr != nullptr)
16060 {
16061 type = get_DW_AT_signature_type (die, attr, cu);
16062
16063 /* The type's CU may not be the same as CU.
16064 Ensure TYPE is recorded with CU in die_type_hash. */
16065 return set_die_type (die, type, cu);
16066 }
16067
16068 type = alloc_type (objfile);
16069
16070 type->set_code (TYPE_CODE_ENUM);
16071 name = dwarf2_full_name (NULL, die, cu);
16072 if (name != NULL)
16073 type->set_name (name);
16074
16075 attr = dwarf2_attr (die, DW_AT_type, cu);
16076 if (attr != NULL)
16077 {
16078 struct type *underlying_type = die_type (die, cu);
16079
16080 TYPE_TARGET_TYPE (type) = underlying_type;
16081 }
16082
16083 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16084 if (attr != nullptr)
16085 {
16086 TYPE_LENGTH (type) = attr->constant_value (0);
16087 }
16088 else
16089 {
16090 TYPE_LENGTH (type) = 0;
16091 }
16092
16093 maybe_set_alignment (cu, die, type);
16094
16095 /* The enumeration DIE can be incomplete. In Ada, any type can be
16096 declared as private in the package spec, and then defined only
16097 inside the package body. Such types are known as Taft Amendment
16098 Types. When another package uses such a type, an incomplete DIE
16099 may be generated by the compiler. */
16100 if (die_is_declaration (die, cu))
16101 type->set_is_stub (true);
16102
16103 /* If this type has an underlying type that is not a stub, then we
16104 may use its attributes. We always use the "unsigned" attribute
16105 in this situation, because ordinarily we guess whether the type
16106 is unsigned -- but the guess can be wrong and the underlying type
16107 can tell us the reality. However, we defer to a local size
16108 attribute if one exists, because this lets the compiler override
16109 the underlying type if needed. */
16110 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16111 {
16112 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16113 underlying_type = check_typedef (underlying_type);
16114
16115 type->set_is_unsigned (underlying_type->is_unsigned ());
16116
16117 if (TYPE_LENGTH (type) == 0)
16118 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16119
16120 if (TYPE_RAW_ALIGN (type) == 0
16121 && TYPE_RAW_ALIGN (underlying_type) != 0)
16122 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16123 }
16124
16125 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16126
16127 set_die_type (die, type, cu);
16128
16129 /* Finish the creation of this type by using the enum's children.
16130 Note that, as usual, this must come after set_die_type to avoid
16131 infinite recursion when trying to compute the names of the
16132 enumerators. */
16133 update_enumeration_type_from_children (die, type, cu);
16134
16135 return type;
16136 }
16137
16138 /* Given a pointer to a die which begins an enumeration, process all
16139 the dies that define the members of the enumeration, and create the
16140 symbol for the enumeration type.
16141
16142 NOTE: We reverse the order of the element list. */
16143
16144 static void
16145 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16146 {
16147 struct type *this_type;
16148
16149 this_type = get_die_type (die, cu);
16150 if (this_type == NULL)
16151 this_type = read_enumeration_type (die, cu);
16152
16153 if (die->child != NULL)
16154 {
16155 struct die_info *child_die;
16156 const char *name;
16157
16158 child_die = die->child;
16159 while (child_die && child_die->tag)
16160 {
16161 if (child_die->tag != DW_TAG_enumerator)
16162 {
16163 process_die (child_die, cu);
16164 }
16165 else
16166 {
16167 name = dwarf2_name (child_die, cu);
16168 if (name)
16169 new_symbol (child_die, this_type, cu);
16170 }
16171
16172 child_die = child_die->sibling;
16173 }
16174 }
16175
16176 /* If we are reading an enum from a .debug_types unit, and the enum
16177 is a declaration, and the enum is not the signatured type in the
16178 unit, then we do not want to add a symbol for it. Adding a
16179 symbol would in some cases obscure the true definition of the
16180 enum, giving users an incomplete type when the definition is
16181 actually available. Note that we do not want to do this for all
16182 enums which are just declarations, because C++0x allows forward
16183 enum declarations. */
16184 if (cu->per_cu->is_debug_types
16185 && die_is_declaration (die, cu))
16186 {
16187 struct signatured_type *sig_type;
16188
16189 sig_type = (struct signatured_type *) cu->per_cu;
16190 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16191 if (sig_type->type_offset_in_section != die->sect_off)
16192 return;
16193 }
16194
16195 new_symbol (die, this_type, cu);
16196 }
16197
16198 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16199 expression for an index type and finds the corresponding field
16200 offset in the hidden "P_BOUNDS" structure. Returns true on success
16201 and updates *FIELD, false if it fails to recognize an
16202 expression. */
16203
16204 static bool
16205 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16206 int *bounds_offset, struct field *field,
16207 struct dwarf2_cu *cu)
16208 {
16209 struct attribute *attr = dwarf2_attr (die, name, cu);
16210 if (attr == nullptr || !attr->form_is_block ())
16211 return false;
16212
16213 const struct dwarf_block *block = attr->as_block ();
16214 const gdb_byte *start = block->data;
16215 const gdb_byte *end = block->data + block->size;
16216
16217 /* The expression to recognize generally looks like:
16218
16219 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16220 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16221
16222 However, the second "plus_uconst" may be missing:
16223
16224 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16225 DW_OP_deref_size: 4)
16226
16227 This happens when the field is at the start of the structure.
16228
16229 Also, the final deref may not be sized:
16230
16231 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16232 DW_OP_deref)
16233
16234 This happens when the size of the index type happens to be the
16235 same as the architecture's word size. This can occur with or
16236 without the second plus_uconst. */
16237
16238 if (end - start < 2)
16239 return false;
16240 if (*start++ != DW_OP_push_object_address)
16241 return false;
16242 if (*start++ != DW_OP_plus_uconst)
16243 return false;
16244
16245 uint64_t this_bound_off;
16246 start = gdb_read_uleb128 (start, end, &this_bound_off);
16247 if (start == nullptr || (int) this_bound_off != this_bound_off)
16248 return false;
16249 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16250 is consistent among all bounds. */
16251 if (*bounds_offset == -1)
16252 *bounds_offset = this_bound_off;
16253 else if (*bounds_offset != this_bound_off)
16254 return false;
16255
16256 if (start == end || *start++ != DW_OP_deref)
16257 return false;
16258
16259 int offset = 0;
16260 if (start ==end)
16261 return false;
16262 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16263 {
16264 /* This means an offset of 0. */
16265 }
16266 else if (*start++ != DW_OP_plus_uconst)
16267 return false;
16268 else
16269 {
16270 /* The size is the parameter to DW_OP_plus_uconst. */
16271 uint64_t val;
16272 start = gdb_read_uleb128 (start, end, &val);
16273 if (start == nullptr)
16274 return false;
16275 if ((int) val != val)
16276 return false;
16277 offset = val;
16278 }
16279
16280 if (start == end)
16281 return false;
16282
16283 uint64_t size;
16284 if (*start == DW_OP_deref_size)
16285 {
16286 start = gdb_read_uleb128 (start + 1, end, &size);
16287 if (start == nullptr)
16288 return false;
16289 }
16290 else if (*start == DW_OP_deref)
16291 {
16292 size = cu->header.addr_size;
16293 ++start;
16294 }
16295 else
16296 return false;
16297
16298 SET_FIELD_BITPOS (*field, 8 * offset);
16299 if (size != TYPE_LENGTH (field->type ()))
16300 FIELD_BITSIZE (*field) = 8 * size;
16301
16302 return true;
16303 }
16304
16305 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16306 some kinds of Ada arrays:
16307
16308 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16309 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16310 <11e0> DW_AT_data_location: 2 byte block: 97 6
16311 (DW_OP_push_object_address; DW_OP_deref)
16312 <11e3> DW_AT_type : <0x1173>
16313 <11e7> DW_AT_sibling : <0x1201>
16314 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16315 <11ec> DW_AT_type : <0x1206>
16316 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16317 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16318 DW_OP_deref_size: 4)
16319 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16320 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16321 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16322
16323 This actually represents a "thick pointer", which is a structure
16324 with two elements: one that is a pointer to the array data, and one
16325 that is a pointer to another structure; this second structure holds
16326 the array bounds.
16327
16328 This returns a new type on success, or nullptr if this didn't
16329 recognize the type. */
16330
16331 static struct type *
16332 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16333 struct type *type)
16334 {
16335 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16336 /* So far we've only seen this with block form. */
16337 if (attr == nullptr || !attr->form_is_block ())
16338 return nullptr;
16339
16340 /* Note that this will fail if the structure layout is changed by
16341 the compiler. However, we have no good way to recognize some
16342 other layout, because we don't know what expression the compiler
16343 might choose to emit should this happen. */
16344 struct dwarf_block *blk = attr->as_block ();
16345 if (blk->size != 2
16346 || blk->data[0] != DW_OP_push_object_address
16347 || blk->data[1] != DW_OP_deref)
16348 return nullptr;
16349
16350 int bounds_offset = -1;
16351 int max_align = -1;
16352 std::vector<struct field> range_fields;
16353 for (struct die_info *child_die = die->child;
16354 child_die;
16355 child_die = child_die->sibling)
16356 {
16357 if (child_die->tag == DW_TAG_subrange_type)
16358 {
16359 struct type *underlying = read_subrange_index_type (child_die, cu);
16360
16361 int this_align = type_align (underlying);
16362 if (this_align > max_align)
16363 max_align = this_align;
16364
16365 range_fields.emplace_back ();
16366 range_fields.emplace_back ();
16367
16368 struct field &lower = range_fields[range_fields.size () - 2];
16369 struct field &upper = range_fields[range_fields.size () - 1];
16370
16371 lower.set_type (underlying);
16372 FIELD_ARTIFICIAL (lower) = 1;
16373
16374 upper.set_type (underlying);
16375 FIELD_ARTIFICIAL (upper) = 1;
16376
16377 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16378 &bounds_offset, &lower, cu)
16379 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16380 &bounds_offset, &upper, cu))
16381 return nullptr;
16382 }
16383 }
16384
16385 /* This shouldn't really happen, but double-check that we found
16386 where the bounds are stored. */
16387 if (bounds_offset == -1)
16388 return nullptr;
16389
16390 struct objfile *objfile = cu->per_objfile->objfile;
16391 for (int i = 0; i < range_fields.size (); i += 2)
16392 {
16393 char name[20];
16394
16395 /* Set the name of each field in the bounds. */
16396 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16397 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16398 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16399 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16400 }
16401
16402 struct type *bounds = alloc_type (objfile);
16403 bounds->set_code (TYPE_CODE_STRUCT);
16404
16405 bounds->set_num_fields (range_fields.size ());
16406 bounds->set_fields
16407 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16408 * sizeof (struct field))));
16409 memcpy (bounds->fields (), range_fields.data (),
16410 bounds->num_fields () * sizeof (struct field));
16411
16412 int last_fieldno = range_fields.size () - 1;
16413 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16414 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16415 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16416
16417 /* Rewrite the existing array type in place. Specifically, we
16418 remove any dynamic properties we might have read, and we replace
16419 the index types. */
16420 struct type *iter = type;
16421 for (int i = 0; i < range_fields.size (); i += 2)
16422 {
16423 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16424 iter->main_type->dyn_prop_list = nullptr;
16425 iter->set_index_type
16426 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16427 iter = TYPE_TARGET_TYPE (iter);
16428 }
16429
16430 struct type *result = alloc_type (objfile);
16431 result->set_code (TYPE_CODE_STRUCT);
16432
16433 result->set_num_fields (2);
16434 result->set_fields
16435 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16436 * sizeof (struct field))));
16437
16438 /* The names are chosen to coincide with what the compiler does with
16439 -fgnat-encodings=all, which the Ada code in gdb already
16440 understands. */
16441 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16442 result->field (0).set_type (lookup_pointer_type (type));
16443
16444 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16445 result->field (1).set_type (lookup_pointer_type (bounds));
16446 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16447
16448 result->set_name (type->name ());
16449 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16450 + TYPE_LENGTH (result->field (1).type ()));
16451
16452 return result;
16453 }
16454
16455 /* Extract all information from a DW_TAG_array_type DIE and put it in
16456 the DIE's type field. For now, this only handles one dimensional
16457 arrays. */
16458
16459 static struct type *
16460 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16461 {
16462 struct objfile *objfile = cu->per_objfile->objfile;
16463 struct die_info *child_die;
16464 struct type *type;
16465 struct type *element_type, *range_type, *index_type;
16466 struct attribute *attr;
16467 const char *name;
16468 struct dynamic_prop *byte_stride_prop = NULL;
16469 unsigned int bit_stride = 0;
16470
16471 element_type = die_type (die, cu);
16472
16473 /* The die_type call above may have already set the type for this DIE. */
16474 type = get_die_type (die, cu);
16475 if (type)
16476 return type;
16477
16478 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16479 if (attr != NULL)
16480 {
16481 int stride_ok;
16482 struct type *prop_type = cu->addr_sized_int_type (false);
16483
16484 byte_stride_prop
16485 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16486 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16487 prop_type);
16488 if (!stride_ok)
16489 {
16490 complaint (_("unable to read array DW_AT_byte_stride "
16491 " - DIE at %s [in module %s]"),
16492 sect_offset_str (die->sect_off),
16493 objfile_name (cu->per_objfile->objfile));
16494 /* Ignore this attribute. We will likely not be able to print
16495 arrays of this type correctly, but there is little we can do
16496 to help if we cannot read the attribute's value. */
16497 byte_stride_prop = NULL;
16498 }
16499 }
16500
16501 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16502 if (attr != NULL)
16503 bit_stride = attr->constant_value (0);
16504
16505 /* Irix 6.2 native cc creates array types without children for
16506 arrays with unspecified length. */
16507 if (die->child == NULL)
16508 {
16509 index_type = objfile_type (objfile)->builtin_int;
16510 range_type = create_static_range_type (NULL, index_type, 0, -1);
16511 type = create_array_type_with_stride (NULL, element_type, range_type,
16512 byte_stride_prop, bit_stride);
16513 return set_die_type (die, type, cu);
16514 }
16515
16516 std::vector<struct type *> range_types;
16517 child_die = die->child;
16518 while (child_die && child_die->tag)
16519 {
16520 if (child_die->tag == DW_TAG_subrange_type)
16521 {
16522 struct type *child_type = read_type_die (child_die, cu);
16523
16524 if (child_type != NULL)
16525 {
16526 /* The range type was succesfully read. Save it for the
16527 array type creation. */
16528 range_types.push_back (child_type);
16529 }
16530 }
16531 child_die = child_die->sibling;
16532 }
16533
16534 if (range_types.empty ())
16535 {
16536 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16537 sect_offset_str (die->sect_off),
16538 objfile_name (cu->per_objfile->objfile));
16539 return NULL;
16540 }
16541
16542 /* Dwarf2 dimensions are output from left to right, create the
16543 necessary array types in backwards order. */
16544
16545 type = element_type;
16546
16547 if (read_array_order (die, cu) == DW_ORD_col_major)
16548 {
16549 int i = 0;
16550
16551 while (i < range_types.size ())
16552 {
16553 type = create_array_type_with_stride (NULL, type, range_types[i++],
16554 byte_stride_prop, bit_stride);
16555 bit_stride = 0;
16556 byte_stride_prop = nullptr;
16557 }
16558 }
16559 else
16560 {
16561 size_t ndim = range_types.size ();
16562 while (ndim-- > 0)
16563 {
16564 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16565 byte_stride_prop, bit_stride);
16566 bit_stride = 0;
16567 byte_stride_prop = nullptr;
16568 }
16569 }
16570
16571 gdb_assert (type != element_type);
16572
16573 /* Understand Dwarf2 support for vector types (like they occur on
16574 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16575 array type. This is not part of the Dwarf2/3 standard yet, but a
16576 custom vendor extension. The main difference between a regular
16577 array and the vector variant is that vectors are passed by value
16578 to functions. */
16579 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16580 if (attr != nullptr)
16581 make_vector_type (type);
16582
16583 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16584 implementation may choose to implement triple vectors using this
16585 attribute. */
16586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16587 if (attr != nullptr && attr->form_is_unsigned ())
16588 {
16589 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16590 TYPE_LENGTH (type) = attr->as_unsigned ();
16591 else
16592 complaint (_("DW_AT_byte_size for array type smaller "
16593 "than the total size of elements"));
16594 }
16595
16596 name = dwarf2_name (die, cu);
16597 if (name)
16598 type->set_name (name);
16599
16600 maybe_set_alignment (cu, die, type);
16601
16602 struct type *replacement_type = nullptr;
16603 if (cu->language == language_ada)
16604 {
16605 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16606 if (replacement_type != nullptr)
16607 type = replacement_type;
16608 }
16609
16610 /* Install the type in the die. */
16611 set_die_type (die, type, cu, replacement_type != nullptr);
16612
16613 /* set_die_type should be already done. */
16614 set_descriptive_type (type, die, cu);
16615
16616 return type;
16617 }
16618
16619 static enum dwarf_array_dim_ordering
16620 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16621 {
16622 struct attribute *attr;
16623
16624 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16625
16626 if (attr != nullptr)
16627 {
16628 LONGEST val = attr->constant_value (-1);
16629 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16630 return (enum dwarf_array_dim_ordering) val;
16631 }
16632
16633 /* GNU F77 is a special case, as at 08/2004 array type info is the
16634 opposite order to the dwarf2 specification, but data is still
16635 laid out as per normal fortran.
16636
16637 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16638 version checking. */
16639
16640 if (cu->language == language_fortran
16641 && cu->producer && strstr (cu->producer, "GNU F77"))
16642 {
16643 return DW_ORD_row_major;
16644 }
16645
16646 switch (cu->language_defn->array_ordering ())
16647 {
16648 case array_column_major:
16649 return DW_ORD_col_major;
16650 case array_row_major:
16651 default:
16652 return DW_ORD_row_major;
16653 };
16654 }
16655
16656 /* Extract all information from a DW_TAG_set_type DIE and put it in
16657 the DIE's type field. */
16658
16659 static struct type *
16660 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16661 {
16662 struct type *domain_type, *set_type;
16663 struct attribute *attr;
16664
16665 domain_type = die_type (die, cu);
16666
16667 /* The die_type call above may have already set the type for this DIE. */
16668 set_type = get_die_type (die, cu);
16669 if (set_type)
16670 return set_type;
16671
16672 set_type = create_set_type (NULL, domain_type);
16673
16674 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16675 if (attr != nullptr && attr->form_is_unsigned ())
16676 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16677
16678 maybe_set_alignment (cu, die, set_type);
16679
16680 return set_die_type (die, set_type, cu);
16681 }
16682
16683 /* A helper for read_common_block that creates a locexpr baton.
16684 SYM is the symbol which we are marking as computed.
16685 COMMON_DIE is the DIE for the common block.
16686 COMMON_LOC is the location expression attribute for the common
16687 block itself.
16688 MEMBER_LOC is the location expression attribute for the particular
16689 member of the common block that we are processing.
16690 CU is the CU from which the above come. */
16691
16692 static void
16693 mark_common_block_symbol_computed (struct symbol *sym,
16694 struct die_info *common_die,
16695 struct attribute *common_loc,
16696 struct attribute *member_loc,
16697 struct dwarf2_cu *cu)
16698 {
16699 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16700 struct objfile *objfile = per_objfile->objfile;
16701 struct dwarf2_locexpr_baton *baton;
16702 gdb_byte *ptr;
16703 unsigned int cu_off;
16704 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16705 LONGEST offset = 0;
16706
16707 gdb_assert (common_loc && member_loc);
16708 gdb_assert (common_loc->form_is_block ());
16709 gdb_assert (member_loc->form_is_block ()
16710 || member_loc->form_is_constant ());
16711
16712 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16713 baton->per_objfile = per_objfile;
16714 baton->per_cu = cu->per_cu;
16715 gdb_assert (baton->per_cu);
16716
16717 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16718
16719 if (member_loc->form_is_constant ())
16720 {
16721 offset = member_loc->constant_value (0);
16722 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16723 }
16724 else
16725 baton->size += member_loc->as_block ()->size;
16726
16727 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16728 baton->data = ptr;
16729
16730 *ptr++ = DW_OP_call4;
16731 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16732 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16733 ptr += 4;
16734
16735 if (member_loc->form_is_constant ())
16736 {
16737 *ptr++ = DW_OP_addr;
16738 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16739 ptr += cu->header.addr_size;
16740 }
16741 else
16742 {
16743 /* We have to copy the data here, because DW_OP_call4 will only
16744 use a DW_AT_location attribute. */
16745 struct dwarf_block *block = member_loc->as_block ();
16746 memcpy (ptr, block->data, block->size);
16747 ptr += block->size;
16748 }
16749
16750 *ptr++ = DW_OP_plus;
16751 gdb_assert (ptr - baton->data == baton->size);
16752
16753 SYMBOL_LOCATION_BATON (sym) = baton;
16754 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16755 }
16756
16757 /* Create appropriate locally-scoped variables for all the
16758 DW_TAG_common_block entries. Also create a struct common_block
16759 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16760 is used to separate the common blocks name namespace from regular
16761 variable names. */
16762
16763 static void
16764 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16765 {
16766 struct attribute *attr;
16767
16768 attr = dwarf2_attr (die, DW_AT_location, cu);
16769 if (attr != nullptr)
16770 {
16771 /* Support the .debug_loc offsets. */
16772 if (attr->form_is_block ())
16773 {
16774 /* Ok. */
16775 }
16776 else if (attr->form_is_section_offset ())
16777 {
16778 dwarf2_complex_location_expr_complaint ();
16779 attr = NULL;
16780 }
16781 else
16782 {
16783 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16784 "common block member");
16785 attr = NULL;
16786 }
16787 }
16788
16789 if (die->child != NULL)
16790 {
16791 struct objfile *objfile = cu->per_objfile->objfile;
16792 struct die_info *child_die;
16793 size_t n_entries = 0, size;
16794 struct common_block *common_block;
16795 struct symbol *sym;
16796
16797 for (child_die = die->child;
16798 child_die && child_die->tag;
16799 child_die = child_die->sibling)
16800 ++n_entries;
16801
16802 size = (sizeof (struct common_block)
16803 + (n_entries - 1) * sizeof (struct symbol *));
16804 common_block
16805 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16806 size);
16807 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16808 common_block->n_entries = 0;
16809
16810 for (child_die = die->child;
16811 child_die && child_die->tag;
16812 child_die = child_die->sibling)
16813 {
16814 /* Create the symbol in the DW_TAG_common_block block in the current
16815 symbol scope. */
16816 sym = new_symbol (child_die, NULL, cu);
16817 if (sym != NULL)
16818 {
16819 struct attribute *member_loc;
16820
16821 common_block->contents[common_block->n_entries++] = sym;
16822
16823 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16824 cu);
16825 if (member_loc)
16826 {
16827 /* GDB has handled this for a long time, but it is
16828 not specified by DWARF. It seems to have been
16829 emitted by gfortran at least as recently as:
16830 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16831 complaint (_("Variable in common block has "
16832 "DW_AT_data_member_location "
16833 "- DIE at %s [in module %s]"),
16834 sect_offset_str (child_die->sect_off),
16835 objfile_name (objfile));
16836
16837 if (member_loc->form_is_section_offset ())
16838 dwarf2_complex_location_expr_complaint ();
16839 else if (member_loc->form_is_constant ()
16840 || member_loc->form_is_block ())
16841 {
16842 if (attr != nullptr)
16843 mark_common_block_symbol_computed (sym, die, attr,
16844 member_loc, cu);
16845 }
16846 else
16847 dwarf2_complex_location_expr_complaint ();
16848 }
16849 }
16850 }
16851
16852 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16853 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16854 }
16855 }
16856
16857 /* Create a type for a C++ namespace. */
16858
16859 static struct type *
16860 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16861 {
16862 struct objfile *objfile = cu->per_objfile->objfile;
16863 const char *previous_prefix, *name;
16864 int is_anonymous;
16865 struct type *type;
16866
16867 /* For extensions, reuse the type of the original namespace. */
16868 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16869 {
16870 struct die_info *ext_die;
16871 struct dwarf2_cu *ext_cu = cu;
16872
16873 ext_die = dwarf2_extension (die, &ext_cu);
16874 type = read_type_die (ext_die, ext_cu);
16875
16876 /* EXT_CU may not be the same as CU.
16877 Ensure TYPE is recorded with CU in die_type_hash. */
16878 return set_die_type (die, type, cu);
16879 }
16880
16881 name = namespace_name (die, &is_anonymous, cu);
16882
16883 /* Now build the name of the current namespace. */
16884
16885 previous_prefix = determine_prefix (die, cu);
16886 if (previous_prefix[0] != '\0')
16887 name = typename_concat (&objfile->objfile_obstack,
16888 previous_prefix, name, 0, cu);
16889
16890 /* Create the type. */
16891 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16892
16893 return set_die_type (die, type, cu);
16894 }
16895
16896 /* Read a namespace scope. */
16897
16898 static void
16899 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16900 {
16901 struct objfile *objfile = cu->per_objfile->objfile;
16902 int is_anonymous;
16903
16904 /* Add a symbol associated to this if we haven't seen the namespace
16905 before. Also, add a using directive if it's an anonymous
16906 namespace. */
16907
16908 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16909 {
16910 struct type *type;
16911
16912 type = read_type_die (die, cu);
16913 new_symbol (die, type, cu);
16914
16915 namespace_name (die, &is_anonymous, cu);
16916 if (is_anonymous)
16917 {
16918 const char *previous_prefix = determine_prefix (die, cu);
16919
16920 std::vector<const char *> excludes;
16921 add_using_directive (using_directives (cu),
16922 previous_prefix, type->name (), NULL,
16923 NULL, excludes, 0, &objfile->objfile_obstack);
16924 }
16925 }
16926
16927 if (die->child != NULL)
16928 {
16929 struct die_info *child_die = die->child;
16930
16931 while (child_die && child_die->tag)
16932 {
16933 process_die (child_die, cu);
16934 child_die = child_die->sibling;
16935 }
16936 }
16937 }
16938
16939 /* Read a Fortran module as type. This DIE can be only a declaration used for
16940 imported module. Still we need that type as local Fortran "use ... only"
16941 declaration imports depend on the created type in determine_prefix. */
16942
16943 static struct type *
16944 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16945 {
16946 struct objfile *objfile = cu->per_objfile->objfile;
16947 const char *module_name;
16948 struct type *type;
16949
16950 module_name = dwarf2_name (die, cu);
16951 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16952
16953 return set_die_type (die, type, cu);
16954 }
16955
16956 /* Read a Fortran module. */
16957
16958 static void
16959 read_module (struct die_info *die, struct dwarf2_cu *cu)
16960 {
16961 struct die_info *child_die = die->child;
16962 struct type *type;
16963
16964 type = read_type_die (die, cu);
16965 new_symbol (die, type, cu);
16966
16967 while (child_die && child_die->tag)
16968 {
16969 process_die (child_die, cu);
16970 child_die = child_die->sibling;
16971 }
16972 }
16973
16974 /* Return the name of the namespace represented by DIE. Set
16975 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16976 namespace. */
16977
16978 static const char *
16979 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16980 {
16981 struct die_info *current_die;
16982 const char *name = NULL;
16983
16984 /* Loop through the extensions until we find a name. */
16985
16986 for (current_die = die;
16987 current_die != NULL;
16988 current_die = dwarf2_extension (die, &cu))
16989 {
16990 /* We don't use dwarf2_name here so that we can detect the absence
16991 of a name -> anonymous namespace. */
16992 name = dwarf2_string_attr (die, DW_AT_name, cu);
16993
16994 if (name != NULL)
16995 break;
16996 }
16997
16998 /* Is it an anonymous namespace? */
16999
17000 *is_anonymous = (name == NULL);
17001 if (*is_anonymous)
17002 name = CP_ANONYMOUS_NAMESPACE_STR;
17003
17004 return name;
17005 }
17006
17007 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17008 the user defined type vector. */
17009
17010 static struct type *
17011 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17012 {
17013 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17014 struct comp_unit_head *cu_header = &cu->header;
17015 struct type *type;
17016 struct attribute *attr_byte_size;
17017 struct attribute *attr_address_class;
17018 int byte_size, addr_class;
17019 struct type *target_type;
17020
17021 target_type = die_type (die, cu);
17022
17023 /* The die_type call above may have already set the type for this DIE. */
17024 type = get_die_type (die, cu);
17025 if (type)
17026 return type;
17027
17028 type = lookup_pointer_type (target_type);
17029
17030 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17031 if (attr_byte_size)
17032 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17033 else
17034 byte_size = cu_header->addr_size;
17035
17036 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17037 if (attr_address_class)
17038 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17039 else
17040 addr_class = DW_ADDR_none;
17041
17042 ULONGEST alignment = get_alignment (cu, die);
17043
17044 /* If the pointer size, alignment, or address class is different
17045 than the default, create a type variant marked as such and set
17046 the length accordingly. */
17047 if (TYPE_LENGTH (type) != byte_size
17048 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17049 && alignment != TYPE_RAW_ALIGN (type))
17050 || addr_class != DW_ADDR_none)
17051 {
17052 if (gdbarch_address_class_type_flags_p (gdbarch))
17053 {
17054 type_instance_flags type_flags
17055 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17056 addr_class);
17057 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17058 == 0);
17059 type = make_type_with_address_space (type, type_flags);
17060 }
17061 else if (TYPE_LENGTH (type) != byte_size)
17062 {
17063 complaint (_("invalid pointer size %d"), byte_size);
17064 }
17065 else if (TYPE_RAW_ALIGN (type) != alignment)
17066 {
17067 complaint (_("Invalid DW_AT_alignment"
17068 " - DIE at %s [in module %s]"),
17069 sect_offset_str (die->sect_off),
17070 objfile_name (cu->per_objfile->objfile));
17071 }
17072 else
17073 {
17074 /* Should we also complain about unhandled address classes? */
17075 }
17076 }
17077
17078 TYPE_LENGTH (type) = byte_size;
17079 set_type_align (type, alignment);
17080 return set_die_type (die, type, cu);
17081 }
17082
17083 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17084 the user defined type vector. */
17085
17086 static struct type *
17087 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17088 {
17089 struct type *type;
17090 struct type *to_type;
17091 struct type *domain;
17092
17093 to_type = die_type (die, cu);
17094 domain = die_containing_type (die, cu);
17095
17096 /* The calls above may have already set the type for this DIE. */
17097 type = get_die_type (die, cu);
17098 if (type)
17099 return type;
17100
17101 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17102 type = lookup_methodptr_type (to_type);
17103 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17104 {
17105 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17106
17107 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17108 to_type->fields (), to_type->num_fields (),
17109 to_type->has_varargs ());
17110 type = lookup_methodptr_type (new_type);
17111 }
17112 else
17113 type = lookup_memberptr_type (to_type, domain);
17114
17115 return set_die_type (die, type, cu);
17116 }
17117
17118 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17119 the user defined type vector. */
17120
17121 static struct type *
17122 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17123 enum type_code refcode)
17124 {
17125 struct comp_unit_head *cu_header = &cu->header;
17126 struct type *type, *target_type;
17127 struct attribute *attr;
17128
17129 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17130
17131 target_type = die_type (die, cu);
17132
17133 /* The die_type call above may have already set the type for this DIE. */
17134 type = get_die_type (die, cu);
17135 if (type)
17136 return type;
17137
17138 type = lookup_reference_type (target_type, refcode);
17139 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17140 if (attr != nullptr)
17141 {
17142 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17143 }
17144 else
17145 {
17146 TYPE_LENGTH (type) = cu_header->addr_size;
17147 }
17148 maybe_set_alignment (cu, die, type);
17149 return set_die_type (die, type, cu);
17150 }
17151
17152 /* Add the given cv-qualifiers to the element type of the array. GCC
17153 outputs DWARF type qualifiers that apply to an array, not the
17154 element type. But GDB relies on the array element type to carry
17155 the cv-qualifiers. This mimics section 6.7.3 of the C99
17156 specification. */
17157
17158 static struct type *
17159 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17160 struct type *base_type, int cnst, int voltl)
17161 {
17162 struct type *el_type, *inner_array;
17163
17164 base_type = copy_type (base_type);
17165 inner_array = base_type;
17166
17167 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17168 {
17169 TYPE_TARGET_TYPE (inner_array) =
17170 copy_type (TYPE_TARGET_TYPE (inner_array));
17171 inner_array = TYPE_TARGET_TYPE (inner_array);
17172 }
17173
17174 el_type = TYPE_TARGET_TYPE (inner_array);
17175 cnst |= TYPE_CONST (el_type);
17176 voltl |= TYPE_VOLATILE (el_type);
17177 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17178
17179 return set_die_type (die, base_type, cu);
17180 }
17181
17182 static struct type *
17183 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17184 {
17185 struct type *base_type, *cv_type;
17186
17187 base_type = die_type (die, cu);
17188
17189 /* The die_type call above may have already set the type for this DIE. */
17190 cv_type = get_die_type (die, cu);
17191 if (cv_type)
17192 return cv_type;
17193
17194 /* In case the const qualifier is applied to an array type, the element type
17195 is so qualified, not the array type (section 6.7.3 of C99). */
17196 if (base_type->code () == TYPE_CODE_ARRAY)
17197 return add_array_cv_type (die, cu, base_type, 1, 0);
17198
17199 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17200 return set_die_type (die, cv_type, cu);
17201 }
17202
17203 static struct type *
17204 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17205 {
17206 struct type *base_type, *cv_type;
17207
17208 base_type = die_type (die, cu);
17209
17210 /* The die_type call above may have already set the type for this DIE. */
17211 cv_type = get_die_type (die, cu);
17212 if (cv_type)
17213 return cv_type;
17214
17215 /* In case the volatile qualifier is applied to an array type, the
17216 element type is so qualified, not the array type (section 6.7.3
17217 of C99). */
17218 if (base_type->code () == TYPE_CODE_ARRAY)
17219 return add_array_cv_type (die, cu, base_type, 0, 1);
17220
17221 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17222 return set_die_type (die, cv_type, cu);
17223 }
17224
17225 /* Handle DW_TAG_restrict_type. */
17226
17227 static struct type *
17228 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17229 {
17230 struct type *base_type, *cv_type;
17231
17232 base_type = die_type (die, cu);
17233
17234 /* The die_type call above may have already set the type for this DIE. */
17235 cv_type = get_die_type (die, cu);
17236 if (cv_type)
17237 return cv_type;
17238
17239 cv_type = make_restrict_type (base_type);
17240 return set_die_type (die, cv_type, cu);
17241 }
17242
17243 /* Handle DW_TAG_atomic_type. */
17244
17245 static struct type *
17246 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17247 {
17248 struct type *base_type, *cv_type;
17249
17250 base_type = die_type (die, cu);
17251
17252 /* The die_type call above may have already set the type for this DIE. */
17253 cv_type = get_die_type (die, cu);
17254 if (cv_type)
17255 return cv_type;
17256
17257 cv_type = make_atomic_type (base_type);
17258 return set_die_type (die, cv_type, cu);
17259 }
17260
17261 /* Extract all information from a DW_TAG_string_type DIE and add to
17262 the user defined type vector. It isn't really a user defined type,
17263 but it behaves like one, with other DIE's using an AT_user_def_type
17264 attribute to reference it. */
17265
17266 static struct type *
17267 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17268 {
17269 struct objfile *objfile = cu->per_objfile->objfile;
17270 struct gdbarch *gdbarch = objfile->arch ();
17271 struct type *type, *range_type, *index_type, *char_type;
17272 struct attribute *attr;
17273 struct dynamic_prop prop;
17274 bool length_is_constant = true;
17275 LONGEST length;
17276
17277 /* There are a couple of places where bit sizes might be made use of
17278 when parsing a DW_TAG_string_type, however, no producer that we know
17279 of make use of these. Handling bit sizes that are a multiple of the
17280 byte size is easy enough, but what about other bit sizes? Lets deal
17281 with that problem when we have to. Warn about these attributes being
17282 unsupported, then parse the type and ignore them like we always
17283 have. */
17284 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17285 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17286 {
17287 static bool warning_printed = false;
17288 if (!warning_printed)
17289 {
17290 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17291 "currently supported on DW_TAG_string_type."));
17292 warning_printed = true;
17293 }
17294 }
17295
17296 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17297 if (attr != nullptr && !attr->form_is_constant ())
17298 {
17299 /* The string length describes the location at which the length of
17300 the string can be found. The size of the length field can be
17301 specified with one of the attributes below. */
17302 struct type *prop_type;
17303 struct attribute *len
17304 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17305 if (len == nullptr)
17306 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17307 if (len != nullptr && len->form_is_constant ())
17308 {
17309 /* Pass 0 as the default as we know this attribute is constant
17310 and the default value will not be returned. */
17311 LONGEST sz = len->constant_value (0);
17312 prop_type = cu->per_objfile->int_type (sz, true);
17313 }
17314 else
17315 {
17316 /* If the size is not specified then we assume it is the size of
17317 an address on this target. */
17318 prop_type = cu->addr_sized_int_type (true);
17319 }
17320
17321 /* Convert the attribute into a dynamic property. */
17322 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17323 length = 1;
17324 else
17325 length_is_constant = false;
17326 }
17327 else if (attr != nullptr)
17328 {
17329 /* This DW_AT_string_length just contains the length with no
17330 indirection. There's no need to create a dynamic property in this
17331 case. Pass 0 for the default value as we know it will not be
17332 returned in this case. */
17333 length = attr->constant_value (0);
17334 }
17335 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17336 {
17337 /* We don't currently support non-constant byte sizes for strings. */
17338 length = attr->constant_value (1);
17339 }
17340 else
17341 {
17342 /* Use 1 as a fallback length if we have nothing else. */
17343 length = 1;
17344 }
17345
17346 index_type = objfile_type (objfile)->builtin_int;
17347 if (length_is_constant)
17348 range_type = create_static_range_type (NULL, index_type, 1, length);
17349 else
17350 {
17351 struct dynamic_prop low_bound;
17352
17353 low_bound.set_const_val (1);
17354 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17355 }
17356 char_type = language_string_char_type (cu->language_defn, gdbarch);
17357 type = create_string_type (NULL, char_type, range_type);
17358
17359 return set_die_type (die, type, cu);
17360 }
17361
17362 /* Assuming that DIE corresponds to a function, returns nonzero
17363 if the function is prototyped. */
17364
17365 static int
17366 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17367 {
17368 struct attribute *attr;
17369
17370 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17371 if (attr && attr->as_boolean ())
17372 return 1;
17373
17374 /* The DWARF standard implies that the DW_AT_prototyped attribute
17375 is only meaningful for C, but the concept also extends to other
17376 languages that allow unprototyped functions (Eg: Objective C).
17377 For all other languages, assume that functions are always
17378 prototyped. */
17379 if (cu->language != language_c
17380 && cu->language != language_objc
17381 && cu->language != language_opencl)
17382 return 1;
17383
17384 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17385 prototyped and unprototyped functions; default to prototyped,
17386 since that is more common in modern code (and RealView warns
17387 about unprototyped functions). */
17388 if (producer_is_realview (cu->producer))
17389 return 1;
17390
17391 return 0;
17392 }
17393
17394 /* Handle DIES due to C code like:
17395
17396 struct foo
17397 {
17398 int (*funcp)(int a, long l);
17399 int b;
17400 };
17401
17402 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17403
17404 static struct type *
17405 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17406 {
17407 struct objfile *objfile = cu->per_objfile->objfile;
17408 struct type *type; /* Type that this function returns. */
17409 struct type *ftype; /* Function that returns above type. */
17410 struct attribute *attr;
17411
17412 type = die_type (die, cu);
17413
17414 /* The die_type call above may have already set the type for this DIE. */
17415 ftype = get_die_type (die, cu);
17416 if (ftype)
17417 return ftype;
17418
17419 ftype = lookup_function_type (type);
17420
17421 if (prototyped_function_p (die, cu))
17422 ftype->set_is_prototyped (true);
17423
17424 /* Store the calling convention in the type if it's available in
17425 the subroutine die. Otherwise set the calling convention to
17426 the default value DW_CC_normal. */
17427 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17428 if (attr != nullptr
17429 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17430 TYPE_CALLING_CONVENTION (ftype)
17431 = (enum dwarf_calling_convention) attr->constant_value (0);
17432 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17433 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17434 else
17435 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17436
17437 /* Record whether the function returns normally to its caller or not
17438 if the DWARF producer set that information. */
17439 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17440 if (attr && attr->as_boolean ())
17441 TYPE_NO_RETURN (ftype) = 1;
17442
17443 /* We need to add the subroutine type to the die immediately so
17444 we don't infinitely recurse when dealing with parameters
17445 declared as the same subroutine type. */
17446 set_die_type (die, ftype, cu);
17447
17448 if (die->child != NULL)
17449 {
17450 struct type *void_type = objfile_type (objfile)->builtin_void;
17451 struct die_info *child_die;
17452 int nparams, iparams;
17453
17454 /* Count the number of parameters.
17455 FIXME: GDB currently ignores vararg functions, but knows about
17456 vararg member functions. */
17457 nparams = 0;
17458 child_die = die->child;
17459 while (child_die && child_die->tag)
17460 {
17461 if (child_die->tag == DW_TAG_formal_parameter)
17462 nparams++;
17463 else if (child_die->tag == DW_TAG_unspecified_parameters)
17464 ftype->set_has_varargs (true);
17465
17466 child_die = child_die->sibling;
17467 }
17468
17469 /* Allocate storage for parameters and fill them in. */
17470 ftype->set_num_fields (nparams);
17471 ftype->set_fields
17472 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17473
17474 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17475 even if we error out during the parameters reading below. */
17476 for (iparams = 0; iparams < nparams; iparams++)
17477 ftype->field (iparams).set_type (void_type);
17478
17479 iparams = 0;
17480 child_die = die->child;
17481 while (child_die && child_die->tag)
17482 {
17483 if (child_die->tag == DW_TAG_formal_parameter)
17484 {
17485 struct type *arg_type;
17486
17487 /* DWARF version 2 has no clean way to discern C++
17488 static and non-static member functions. G++ helps
17489 GDB by marking the first parameter for non-static
17490 member functions (which is the this pointer) as
17491 artificial. We pass this information to
17492 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17493
17494 DWARF version 3 added DW_AT_object_pointer, which GCC
17495 4.5 does not yet generate. */
17496 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17497 if (attr != nullptr)
17498 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17499 else
17500 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17501 arg_type = die_type (child_die, cu);
17502
17503 /* RealView does not mark THIS as const, which the testsuite
17504 expects. GCC marks THIS as const in method definitions,
17505 but not in the class specifications (GCC PR 43053). */
17506 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17507 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17508 {
17509 int is_this = 0;
17510 struct dwarf2_cu *arg_cu = cu;
17511 const char *name = dwarf2_name (child_die, cu);
17512
17513 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17514 if (attr != nullptr)
17515 {
17516 /* If the compiler emits this, use it. */
17517 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17518 is_this = 1;
17519 }
17520 else if (name && strcmp (name, "this") == 0)
17521 /* Function definitions will have the argument names. */
17522 is_this = 1;
17523 else if (name == NULL && iparams == 0)
17524 /* Declarations may not have the names, so like
17525 elsewhere in GDB, assume an artificial first
17526 argument is "this". */
17527 is_this = 1;
17528
17529 if (is_this)
17530 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17531 arg_type, 0);
17532 }
17533
17534 ftype->field (iparams).set_type (arg_type);
17535 iparams++;
17536 }
17537 child_die = child_die->sibling;
17538 }
17539 }
17540
17541 return ftype;
17542 }
17543
17544 static struct type *
17545 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17546 {
17547 struct objfile *objfile = cu->per_objfile->objfile;
17548 const char *name = NULL;
17549 struct type *this_type, *target_type;
17550
17551 name = dwarf2_full_name (NULL, die, cu);
17552 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17553 this_type->set_target_is_stub (true);
17554 set_die_type (die, this_type, cu);
17555 target_type = die_type (die, cu);
17556 if (target_type != this_type)
17557 TYPE_TARGET_TYPE (this_type) = target_type;
17558 else
17559 {
17560 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17561 spec and cause infinite loops in GDB. */
17562 complaint (_("Self-referential DW_TAG_typedef "
17563 "- DIE at %s [in module %s]"),
17564 sect_offset_str (die->sect_off), objfile_name (objfile));
17565 TYPE_TARGET_TYPE (this_type) = NULL;
17566 }
17567 if (name == NULL)
17568 {
17569 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17570 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17571 Handle these by just returning the target type, rather than
17572 constructing an anonymous typedef type and trying to handle this
17573 elsewhere. */
17574 set_die_type (die, target_type, cu);
17575 return target_type;
17576 }
17577 return this_type;
17578 }
17579
17580 /* Helper for get_dwarf2_rational_constant that computes the value of
17581 a given gmp_mpz given an attribute. */
17582
17583 static void
17584 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17585 {
17586 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17587 location expression that pushes an implicit value. */
17588 if (attr->form == DW_FORM_exprloc)
17589 {
17590 dwarf_block *blk = attr->as_block ();
17591 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17592 {
17593 uint64_t len;
17594 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17595 blk->data + blk->size,
17596 &len);
17597 if (ptr - blk->data + len <= blk->size)
17598 {
17599 mpz_import (value->val, len,
17600 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17601 1, 0, 0, ptr);
17602 return;
17603 }
17604 }
17605
17606 /* On failure set it to 1. */
17607 *value = gdb_mpz (1);
17608 }
17609 else if (attr->form_is_block ())
17610 {
17611 dwarf_block *blk = attr->as_block ();
17612 mpz_import (value->val, blk->size,
17613 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17614 1, 0, 0, blk->data);
17615 }
17616 else
17617 *value = gdb_mpz (attr->constant_value (1));
17618 }
17619
17620 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17621 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17622
17623 If the numerator and/or numerator attribute is missing,
17624 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17625 untouched. */
17626
17627 static void
17628 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17629 gdb_mpz *numerator, gdb_mpz *denominator)
17630 {
17631 struct attribute *num_attr, *denom_attr;
17632
17633 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17634 if (num_attr == nullptr)
17635 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17636 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17637
17638 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17639 if (denom_attr == nullptr)
17640 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17641 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17642
17643 if (num_attr == nullptr || denom_attr == nullptr)
17644 return;
17645
17646 get_mpz (cu, numerator, num_attr);
17647 get_mpz (cu, denominator, denom_attr);
17648 }
17649
17650 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17651 rational constant, rather than a signed one.
17652
17653 If the rational constant has a negative value, a complaint
17654 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17655
17656 static void
17657 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17658 struct dwarf2_cu *cu,
17659 gdb_mpz *numerator,
17660 gdb_mpz *denominator)
17661 {
17662 gdb_mpz num (1);
17663 gdb_mpz denom (1);
17664
17665 get_dwarf2_rational_constant (die, cu, &num, &denom);
17666 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17667 {
17668 mpz_neg (num.val, num.val);
17669 mpz_neg (denom.val, denom.val);
17670 }
17671 else if (mpz_sgn (num.val) == -1)
17672 {
17673 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17674 " in DIE at %s"),
17675 sect_offset_str (die->sect_off));
17676 return;
17677 }
17678 else if (mpz_sgn (denom.val) == -1)
17679 {
17680 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17681 " in DIE at %s"),
17682 sect_offset_str (die->sect_off));
17683 return;
17684 }
17685
17686 *numerator = std::move (num);
17687 *denominator = std::move (denom);
17688 }
17689
17690 /* Assuming that ENCODING is a string whose contents starting at the
17691 K'th character is "_nn" where "nn" is a decimal number, scan that
17692 number and set RESULT to the value. K is updated to point to the
17693 character immediately following the number.
17694
17695 If the string does not conform to the format described above, false
17696 is returned, and K may or may not be changed. */
17697
17698 static bool
17699 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17700 {
17701 /* The next character should be an underscore ('_') followed
17702 by a digit. */
17703 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17704 return false;
17705
17706 /* Skip the underscore. */
17707 k++;
17708 int start = k;
17709
17710 /* Determine the number of digits for our number. */
17711 while (isdigit (encoding[k]))
17712 k++;
17713 if (k == start)
17714 return false;
17715
17716 std::string copy (&encoding[start], k - start);
17717 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17718 return false;
17719
17720 return true;
17721 }
17722
17723 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17724 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17725 DENOM, update OFFSET, and return true on success. Return false on
17726 failure. */
17727
17728 static bool
17729 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17730 gdb_mpz *num, gdb_mpz *denom)
17731 {
17732 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17733 return false;
17734 return ada_get_gnat_encoded_number (encoding, offset, denom);
17735 }
17736
17737 /* Assuming DIE corresponds to a fixed point type, finish the creation
17738 of the corresponding TYPE by setting its type-specific data. CU is
17739 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17740 encodings. It is nullptr if the GNAT encoding should be
17741 ignored. */
17742
17743 static void
17744 finish_fixed_point_type (struct type *type, const char *suffix,
17745 struct die_info *die, struct dwarf2_cu *cu)
17746 {
17747 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17748 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17749
17750 /* If GNAT encodings are preferred, don't examine the
17751 attributes. */
17752 struct attribute *attr = nullptr;
17753 if (suffix == nullptr)
17754 {
17755 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17756 if (attr == nullptr)
17757 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17758 if (attr == nullptr)
17759 attr = dwarf2_attr (die, DW_AT_small, cu);
17760 }
17761
17762 /* Numerator and denominator of our fixed-point type's scaling factor.
17763 The default is a scaling factor of 1, which we use as a fallback
17764 when we are not able to decode it (problem with the debugging info,
17765 unsupported forms, bug in GDB, etc...). Using that as the default
17766 allows us to at least print the unscaled value, which might still
17767 be useful to a user. */
17768 gdb_mpz scale_num (1);
17769 gdb_mpz scale_denom (1);
17770
17771 if (attr == nullptr)
17772 {
17773 int offset = 0;
17774 if (suffix != nullptr
17775 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17776 &scale_denom)
17777 /* The number might be encoded as _nn_dd_nn_dd, where the
17778 second ratio is the 'small value. In this situation, we
17779 want the second value. */
17780 && (suffix[offset] != '_'
17781 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17782 &scale_denom)))
17783 {
17784 /* Found it. */
17785 }
17786 else
17787 {
17788 /* Scaling factor not found. Assume a scaling factor of 1,
17789 and hope for the best. At least the user will be able to
17790 see the encoded value. */
17791 scale_num = 1;
17792 scale_denom = 1;
17793 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17794 sect_offset_str (die->sect_off));
17795 }
17796 }
17797 else if (attr->name == DW_AT_binary_scale)
17798 {
17799 LONGEST scale_exp = attr->constant_value (0);
17800 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17801
17802 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17803 }
17804 else if (attr->name == DW_AT_decimal_scale)
17805 {
17806 LONGEST scale_exp = attr->constant_value (0);
17807 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17808
17809 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17810 }
17811 else if (attr->name == DW_AT_small)
17812 {
17813 struct die_info *scale_die;
17814 struct dwarf2_cu *scale_cu = cu;
17815
17816 scale_die = follow_die_ref (die, attr, &scale_cu);
17817 if (scale_die->tag == DW_TAG_constant)
17818 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17819 &scale_num, &scale_denom);
17820 else
17821 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17822 " (DIE at %s)"),
17823 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17824 }
17825 else
17826 {
17827 complaint (_("unsupported scale attribute %s for fixed-point type"
17828 " (DIE at %s)"),
17829 dwarf_attr_name (attr->name),
17830 sect_offset_str (die->sect_off));
17831 }
17832
17833 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17834 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17835 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17836 mpq_canonicalize (scaling_factor.val);
17837 }
17838
17839 /* The gnat-encoding suffix for fixed point. */
17840
17841 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17842
17843 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17844 "XF" suffix of the name. The text after this is what encodes the
17845 'small and 'delta information. Otherwise, return nullptr. */
17846
17847 static const char *
17848 gnat_encoded_fixed_point_type_info (const char *name)
17849 {
17850 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17851 }
17852
17853 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17854 (which may be different from NAME) to the architecture back-end to allow
17855 it to guess the correct format if necessary. */
17856
17857 static struct type *
17858 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17859 const char *name_hint, enum bfd_endian byte_order)
17860 {
17861 struct gdbarch *gdbarch = objfile->arch ();
17862 const struct floatformat **format;
17863 struct type *type;
17864
17865 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17866 if (format)
17867 type = init_float_type (objfile, bits, name, format, byte_order);
17868 else
17869 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17870
17871 return type;
17872 }
17873
17874 /* Allocate an integer type of size BITS and name NAME. */
17875
17876 static struct type *
17877 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17878 int bits, int unsigned_p, const char *name)
17879 {
17880 struct type *type;
17881
17882 /* Versions of Intel's C Compiler generate an integer type called "void"
17883 instead of using DW_TAG_unspecified_type. This has been seen on
17884 at least versions 14, 17, and 18. */
17885 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17886 && strcmp (name, "void") == 0)
17887 type = objfile_type (objfile)->builtin_void;
17888 else
17889 type = init_integer_type (objfile, bits, unsigned_p, name);
17890
17891 return type;
17892 }
17893
17894 /* Return true if DIE has a DW_AT_small attribute whose value is
17895 a constant rational, where both the numerator and denominator
17896 are equal to zero.
17897
17898 CU is the DIE's Compilation Unit. */
17899
17900 static bool
17901 has_zero_over_zero_small_attribute (struct die_info *die,
17902 struct dwarf2_cu *cu)
17903 {
17904 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17905 if (attr == nullptr)
17906 return false;
17907
17908 struct dwarf2_cu *scale_cu = cu;
17909 struct die_info *scale_die
17910 = follow_die_ref (die, attr, &scale_cu);
17911
17912 if (scale_die->tag != DW_TAG_constant)
17913 return false;
17914
17915 gdb_mpz num (1), denom (1);
17916 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
17917 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
17918 }
17919
17920 /* Initialise and return a floating point type of size BITS suitable for
17921 use as a component of a complex number. The NAME_HINT is passed through
17922 when initialising the floating point type and is the name of the complex
17923 type.
17924
17925 As DWARF doesn't currently provide an explicit name for the components
17926 of a complex number, but it can be helpful to have these components
17927 named, we try to select a suitable name based on the size of the
17928 component. */
17929 static struct type *
17930 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17931 struct objfile *objfile,
17932 int bits, const char *name_hint,
17933 enum bfd_endian byte_order)
17934 {
17935 gdbarch *gdbarch = objfile->arch ();
17936 struct type *tt = nullptr;
17937
17938 /* Try to find a suitable floating point builtin type of size BITS.
17939 We're going to use the name of this type as the name for the complex
17940 target type that we are about to create. */
17941 switch (cu->language)
17942 {
17943 case language_fortran:
17944 switch (bits)
17945 {
17946 case 32:
17947 tt = builtin_f_type (gdbarch)->builtin_real;
17948 break;
17949 case 64:
17950 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17951 break;
17952 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17953 case 128:
17954 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17955 break;
17956 }
17957 break;
17958 default:
17959 switch (bits)
17960 {
17961 case 32:
17962 tt = builtin_type (gdbarch)->builtin_float;
17963 break;
17964 case 64:
17965 tt = builtin_type (gdbarch)->builtin_double;
17966 break;
17967 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17968 case 128:
17969 tt = builtin_type (gdbarch)->builtin_long_double;
17970 break;
17971 }
17972 break;
17973 }
17974
17975 /* If the type we found doesn't match the size we were looking for, then
17976 pretend we didn't find a type at all, the complex target type we
17977 create will then be nameless. */
17978 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17979 tt = nullptr;
17980
17981 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17982 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17983 }
17984
17985 /* Find a representation of a given base type and install
17986 it in the TYPE field of the die. */
17987
17988 static struct type *
17989 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17990 {
17991 struct objfile *objfile = cu->per_objfile->objfile;
17992 struct type *type;
17993 struct attribute *attr;
17994 int encoding = 0, bits = 0;
17995 const char *name;
17996 gdbarch *arch;
17997
17998 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17999 if (attr != nullptr && attr->form_is_constant ())
18000 encoding = attr->constant_value (0);
18001 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18002 if (attr != nullptr)
18003 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18004 name = dwarf2_name (die, cu);
18005 if (!name)
18006 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18007
18008 arch = objfile->arch ();
18009 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18010
18011 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18012 if (attr != nullptr && attr->form_is_constant ())
18013 {
18014 int endianity = attr->constant_value (0);
18015
18016 switch (endianity)
18017 {
18018 case DW_END_big:
18019 byte_order = BFD_ENDIAN_BIG;
18020 break;
18021 case DW_END_little:
18022 byte_order = BFD_ENDIAN_LITTLE;
18023 break;
18024 default:
18025 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18026 break;
18027 }
18028 }
18029
18030 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18031 && cu->language == language_ada
18032 && has_zero_over_zero_small_attribute (die, cu))
18033 {
18034 /* brobecker/2018-02-24: This is a fixed point type for which
18035 the scaling factor is represented as fraction whose value
18036 does not make sense (zero divided by zero), so we should
18037 normally never see these. However, there is a small category
18038 of fixed point types for which GNAT is unable to provide
18039 the scaling factor via the standard DWARF mechanisms, and
18040 for which the info is provided via the GNAT encodings instead.
18041 This is likely what this DIE is about. */
18042 encoding = (encoding == DW_ATE_signed_fixed
18043 ? DW_ATE_signed
18044 : DW_ATE_unsigned);
18045 }
18046
18047 /* With GNAT encodings, fixed-point information will be encoded in
18048 the type name. Note that this can also occur with the above
18049 zero-over-zero case, which is why this is a separate "if" rather
18050 than an "else if". */
18051 const char *gnat_encoding_suffix = nullptr;
18052 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18053 && cu->language == language_ada
18054 && name != nullptr)
18055 {
18056 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18057 if (gnat_encoding_suffix != nullptr)
18058 {
18059 gdb_assert (startswith (gnat_encoding_suffix,
18060 GNAT_FIXED_POINT_SUFFIX));
18061 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18062 name, gnat_encoding_suffix - name);
18063 /* Use -1 here so that SUFFIX points at the "_" after the
18064 "XF". */
18065 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18066
18067 encoding = (encoding == DW_ATE_signed
18068 ? DW_ATE_signed_fixed
18069 : DW_ATE_unsigned_fixed);
18070 }
18071 }
18072
18073 switch (encoding)
18074 {
18075 case DW_ATE_address:
18076 /* Turn DW_ATE_address into a void * pointer. */
18077 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18078 type = init_pointer_type (objfile, bits, name, type);
18079 break;
18080 case DW_ATE_boolean:
18081 type = init_boolean_type (objfile, bits, 1, name);
18082 break;
18083 case DW_ATE_complex_float:
18084 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18085 byte_order);
18086 if (type->code () == TYPE_CODE_ERROR)
18087 {
18088 if (name == nullptr)
18089 {
18090 struct obstack *obstack
18091 = &cu->per_objfile->objfile->objfile_obstack;
18092 name = obconcat (obstack, "_Complex ", type->name (),
18093 nullptr);
18094 }
18095 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18096 }
18097 else
18098 type = init_complex_type (name, type);
18099 break;
18100 case DW_ATE_decimal_float:
18101 type = init_decfloat_type (objfile, bits, name);
18102 break;
18103 case DW_ATE_float:
18104 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18105 break;
18106 case DW_ATE_signed:
18107 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18108 break;
18109 case DW_ATE_unsigned:
18110 if (cu->language == language_fortran
18111 && name
18112 && startswith (name, "character("))
18113 type = init_character_type (objfile, bits, 1, name);
18114 else
18115 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18116 break;
18117 case DW_ATE_signed_char:
18118 if (cu->language == language_ada || cu->language == language_m2
18119 || cu->language == language_pascal
18120 || cu->language == language_fortran)
18121 type = init_character_type (objfile, bits, 0, name);
18122 else
18123 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18124 break;
18125 case DW_ATE_unsigned_char:
18126 if (cu->language == language_ada || cu->language == language_m2
18127 || cu->language == language_pascal
18128 || cu->language == language_fortran
18129 || cu->language == language_rust)
18130 type = init_character_type (objfile, bits, 1, name);
18131 else
18132 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18133 break;
18134 case DW_ATE_UTF:
18135 {
18136 if (bits == 16)
18137 type = builtin_type (arch)->builtin_char16;
18138 else if (bits == 32)
18139 type = builtin_type (arch)->builtin_char32;
18140 else
18141 {
18142 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18143 bits);
18144 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18145 }
18146 return set_die_type (die, type, cu);
18147 }
18148 break;
18149 case DW_ATE_signed_fixed:
18150 type = init_fixed_point_type (objfile, bits, 0, name);
18151 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18152 break;
18153 case DW_ATE_unsigned_fixed:
18154 type = init_fixed_point_type (objfile, bits, 1, name);
18155 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18156 break;
18157
18158 default:
18159 complaint (_("unsupported DW_AT_encoding: '%s'"),
18160 dwarf_type_encoding_name (encoding));
18161 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18162 break;
18163 }
18164
18165 if (name && strcmp (name, "char") == 0)
18166 type->set_has_no_signedness (true);
18167
18168 maybe_set_alignment (cu, die, type);
18169
18170 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18171
18172 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18173 {
18174 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18175 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18176 {
18177 unsigned real_bit_size = attr->as_unsigned ();
18178 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18179 /* Only use the attributes if they make sense together. */
18180 if (attr == nullptr
18181 || (attr->as_unsigned () + real_bit_size
18182 <= 8 * TYPE_LENGTH (type)))
18183 {
18184 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18185 = real_bit_size;
18186 if (attr != nullptr)
18187 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18188 = attr->as_unsigned ();
18189 }
18190 }
18191 }
18192
18193 return set_die_type (die, type, cu);
18194 }
18195
18196 /* Parse dwarf attribute if it's a block, reference or constant and put the
18197 resulting value of the attribute into struct bound_prop.
18198 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18199
18200 static int
18201 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18202 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18203 struct type *default_type)
18204 {
18205 struct dwarf2_property_baton *baton;
18206 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18207 struct objfile *objfile = per_objfile->objfile;
18208 struct obstack *obstack = &objfile->objfile_obstack;
18209
18210 gdb_assert (default_type != NULL);
18211
18212 if (attr == NULL || prop == NULL)
18213 return 0;
18214
18215 if (attr->form_is_block ())
18216 {
18217 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18218 baton->property_type = default_type;
18219 baton->locexpr.per_cu = cu->per_cu;
18220 baton->locexpr.per_objfile = per_objfile;
18221
18222 struct dwarf_block *block = attr->as_block ();
18223 baton->locexpr.size = block->size;
18224 baton->locexpr.data = block->data;
18225 switch (attr->name)
18226 {
18227 case DW_AT_string_length:
18228 baton->locexpr.is_reference = true;
18229 break;
18230 default:
18231 baton->locexpr.is_reference = false;
18232 break;
18233 }
18234
18235 prop->set_locexpr (baton);
18236 gdb_assert (prop->baton () != NULL);
18237 }
18238 else if (attr->form_is_ref ())
18239 {
18240 struct dwarf2_cu *target_cu = cu;
18241 struct die_info *target_die;
18242 struct attribute *target_attr;
18243
18244 target_die = follow_die_ref (die, attr, &target_cu);
18245 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18246 if (target_attr == NULL)
18247 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18248 target_cu);
18249 if (target_attr == NULL)
18250 return 0;
18251
18252 switch (target_attr->name)
18253 {
18254 case DW_AT_location:
18255 if (target_attr->form_is_section_offset ())
18256 {
18257 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18258 baton->property_type = die_type (target_die, target_cu);
18259 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18260 prop->set_loclist (baton);
18261 gdb_assert (prop->baton () != NULL);
18262 }
18263 else if (target_attr->form_is_block ())
18264 {
18265 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18266 baton->property_type = die_type (target_die, target_cu);
18267 baton->locexpr.per_cu = cu->per_cu;
18268 baton->locexpr.per_objfile = per_objfile;
18269 struct dwarf_block *block = target_attr->as_block ();
18270 baton->locexpr.size = block->size;
18271 baton->locexpr.data = block->data;
18272 baton->locexpr.is_reference = true;
18273 prop->set_locexpr (baton);
18274 gdb_assert (prop->baton () != NULL);
18275 }
18276 else
18277 {
18278 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18279 "dynamic property");
18280 return 0;
18281 }
18282 break;
18283 case DW_AT_data_member_location:
18284 {
18285 LONGEST offset;
18286
18287 if (!handle_data_member_location (target_die, target_cu,
18288 &offset))
18289 return 0;
18290
18291 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18292 baton->property_type = read_type_die (target_die->parent,
18293 target_cu);
18294 baton->offset_info.offset = offset;
18295 baton->offset_info.type = die_type (target_die, target_cu);
18296 prop->set_addr_offset (baton);
18297 break;
18298 }
18299 }
18300 }
18301 else if (attr->form_is_constant ())
18302 prop->set_const_val (attr->constant_value (0));
18303 else
18304 {
18305 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18306 dwarf2_name (die, cu));
18307 return 0;
18308 }
18309
18310 return 1;
18311 }
18312
18313 /* See read.h. */
18314
18315 struct type *
18316 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18317 {
18318 struct type *int_type;
18319
18320 /* Helper macro to examine the various builtin types. */
18321 #define TRY_TYPE(F) \
18322 int_type = (unsigned_p \
18323 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18324 : objfile_type (objfile)->builtin_ ## F); \
18325 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18326 return int_type
18327
18328 TRY_TYPE (char);
18329 TRY_TYPE (short);
18330 TRY_TYPE (int);
18331 TRY_TYPE (long);
18332 TRY_TYPE (long_long);
18333
18334 #undef TRY_TYPE
18335
18336 gdb_assert_not_reached ("unable to find suitable integer type");
18337 }
18338
18339 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18340 present (which is valid) then compute the default type based on the
18341 compilation units address size. */
18342
18343 static struct type *
18344 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18345 {
18346 struct type *index_type = die_type (die, cu);
18347
18348 /* Dwarf-2 specifications explicitly allows to create subrange types
18349 without specifying a base type.
18350 In that case, the base type must be set to the type of
18351 the lower bound, upper bound or count, in that order, if any of these
18352 three attributes references an object that has a type.
18353 If no base type is found, the Dwarf-2 specifications say that
18354 a signed integer type of size equal to the size of an address should
18355 be used.
18356 For the following C code: `extern char gdb_int [];'
18357 GCC produces an empty range DIE.
18358 FIXME: muller/2010-05-28: Possible references to object for low bound,
18359 high bound or count are not yet handled by this code. */
18360 if (index_type->code () == TYPE_CODE_VOID)
18361 index_type = cu->addr_sized_int_type (false);
18362
18363 return index_type;
18364 }
18365
18366 /* Read the given DW_AT_subrange DIE. */
18367
18368 static struct type *
18369 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18370 {
18371 struct type *base_type, *orig_base_type;
18372 struct type *range_type;
18373 struct attribute *attr;
18374 struct dynamic_prop low, high;
18375 int low_default_is_valid;
18376 int high_bound_is_count = 0;
18377 const char *name;
18378 ULONGEST negative_mask;
18379
18380 orig_base_type = read_subrange_index_type (die, cu);
18381
18382 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18383 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18384 creating the range type, but we use the result of check_typedef
18385 when examining properties of the type. */
18386 base_type = check_typedef (orig_base_type);
18387
18388 /* The die_type call above may have already set the type for this DIE. */
18389 range_type = get_die_type (die, cu);
18390 if (range_type)
18391 return range_type;
18392
18393 high.set_const_val (0);
18394
18395 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18396 omitting DW_AT_lower_bound. */
18397 switch (cu->language)
18398 {
18399 case language_c:
18400 case language_cplus:
18401 low.set_const_val (0);
18402 low_default_is_valid = 1;
18403 break;
18404 case language_fortran:
18405 low.set_const_val (1);
18406 low_default_is_valid = 1;
18407 break;
18408 case language_d:
18409 case language_objc:
18410 case language_rust:
18411 low.set_const_val (0);
18412 low_default_is_valid = (cu->header.version >= 4);
18413 break;
18414 case language_ada:
18415 case language_m2:
18416 case language_pascal:
18417 low.set_const_val (1);
18418 low_default_is_valid = (cu->header.version >= 4);
18419 break;
18420 default:
18421 low.set_const_val (0);
18422 low_default_is_valid = 0;
18423 break;
18424 }
18425
18426 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18427 if (attr != nullptr)
18428 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18429 else if (!low_default_is_valid)
18430 complaint (_("Missing DW_AT_lower_bound "
18431 "- DIE at %s [in module %s]"),
18432 sect_offset_str (die->sect_off),
18433 objfile_name (cu->per_objfile->objfile));
18434
18435 struct attribute *attr_ub, *attr_count;
18436 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18437 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18438 {
18439 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18440 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18441 {
18442 /* If bounds are constant do the final calculation here. */
18443 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18444 high.set_const_val (low.const_val () + high.const_val () - 1);
18445 else
18446 high_bound_is_count = 1;
18447 }
18448 else
18449 {
18450 if (attr_ub != NULL)
18451 complaint (_("Unresolved DW_AT_upper_bound "
18452 "- DIE at %s [in module %s]"),
18453 sect_offset_str (die->sect_off),
18454 objfile_name (cu->per_objfile->objfile));
18455 if (attr_count != NULL)
18456 complaint (_("Unresolved DW_AT_count "
18457 "- DIE at %s [in module %s]"),
18458 sect_offset_str (die->sect_off),
18459 objfile_name (cu->per_objfile->objfile));
18460 }
18461 }
18462
18463 LONGEST bias = 0;
18464 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18465 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18466 bias = bias_attr->constant_value (0);
18467
18468 /* Normally, the DWARF producers are expected to use a signed
18469 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18470 But this is unfortunately not always the case, as witnessed
18471 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18472 is used instead. To work around that ambiguity, we treat
18473 the bounds as signed, and thus sign-extend their values, when
18474 the base type is signed. */
18475 negative_mask =
18476 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18477 if (low.kind () == PROP_CONST
18478 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18479 low.set_const_val (low.const_val () | negative_mask);
18480 if (high.kind () == PROP_CONST
18481 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18482 high.set_const_val (high.const_val () | negative_mask);
18483
18484 /* Check for bit and byte strides. */
18485 struct dynamic_prop byte_stride_prop;
18486 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18487 if (attr_byte_stride != nullptr)
18488 {
18489 struct type *prop_type = cu->addr_sized_int_type (false);
18490 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18491 prop_type);
18492 }
18493
18494 struct dynamic_prop bit_stride_prop;
18495 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18496 if (attr_bit_stride != nullptr)
18497 {
18498 /* It only makes sense to have either a bit or byte stride. */
18499 if (attr_byte_stride != nullptr)
18500 {
18501 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18502 "- DIE at %s [in module %s]"),
18503 sect_offset_str (die->sect_off),
18504 objfile_name (cu->per_objfile->objfile));
18505 attr_bit_stride = nullptr;
18506 }
18507 else
18508 {
18509 struct type *prop_type = cu->addr_sized_int_type (false);
18510 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18511 prop_type);
18512 }
18513 }
18514
18515 if (attr_byte_stride != nullptr
18516 || attr_bit_stride != nullptr)
18517 {
18518 bool byte_stride_p = (attr_byte_stride != nullptr);
18519 struct dynamic_prop *stride
18520 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18521
18522 range_type
18523 = create_range_type_with_stride (NULL, orig_base_type, &low,
18524 &high, bias, stride, byte_stride_p);
18525 }
18526 else
18527 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18528
18529 if (high_bound_is_count)
18530 range_type->bounds ()->flag_upper_bound_is_count = 1;
18531
18532 /* Ada expects an empty array on no boundary attributes. */
18533 if (attr == NULL && cu->language != language_ada)
18534 range_type->bounds ()->high.set_undefined ();
18535
18536 name = dwarf2_name (die, cu);
18537 if (name)
18538 range_type->set_name (name);
18539
18540 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18541 if (attr != nullptr)
18542 TYPE_LENGTH (range_type) = attr->constant_value (0);
18543
18544 maybe_set_alignment (cu, die, range_type);
18545
18546 set_die_type (die, range_type, cu);
18547
18548 /* set_die_type should be already done. */
18549 set_descriptive_type (range_type, die, cu);
18550
18551 return range_type;
18552 }
18553
18554 static struct type *
18555 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18556 {
18557 struct type *type;
18558
18559 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18560 type->set_name (dwarf2_name (die, cu));
18561
18562 /* In Ada, an unspecified type is typically used when the description
18563 of the type is deferred to a different unit. When encountering
18564 such a type, we treat it as a stub, and try to resolve it later on,
18565 when needed. */
18566 if (cu->language == language_ada)
18567 type->set_is_stub (true);
18568
18569 return set_die_type (die, type, cu);
18570 }
18571
18572 /* Read a single die and all its descendents. Set the die's sibling
18573 field to NULL; set other fields in the die correctly, and set all
18574 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18575 location of the info_ptr after reading all of those dies. PARENT
18576 is the parent of the die in question. */
18577
18578 static struct die_info *
18579 read_die_and_children (const struct die_reader_specs *reader,
18580 const gdb_byte *info_ptr,
18581 const gdb_byte **new_info_ptr,
18582 struct die_info *parent)
18583 {
18584 struct die_info *die;
18585 const gdb_byte *cur_ptr;
18586
18587 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18588 if (die == NULL)
18589 {
18590 *new_info_ptr = cur_ptr;
18591 return NULL;
18592 }
18593 store_in_ref_table (die, reader->cu);
18594
18595 if (die->has_children)
18596 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18597 else
18598 {
18599 die->child = NULL;
18600 *new_info_ptr = cur_ptr;
18601 }
18602
18603 die->sibling = NULL;
18604 die->parent = parent;
18605 return die;
18606 }
18607
18608 /* Read a die, all of its descendents, and all of its siblings; set
18609 all of the fields of all of the dies correctly. Arguments are as
18610 in read_die_and_children. */
18611
18612 static struct die_info *
18613 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18614 const gdb_byte *info_ptr,
18615 const gdb_byte **new_info_ptr,
18616 struct die_info *parent)
18617 {
18618 struct die_info *first_die, *last_sibling;
18619 const gdb_byte *cur_ptr;
18620
18621 cur_ptr = info_ptr;
18622 first_die = last_sibling = NULL;
18623
18624 while (1)
18625 {
18626 struct die_info *die
18627 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18628
18629 if (die == NULL)
18630 {
18631 *new_info_ptr = cur_ptr;
18632 return first_die;
18633 }
18634
18635 if (!first_die)
18636 first_die = die;
18637 else
18638 last_sibling->sibling = die;
18639
18640 last_sibling = die;
18641 }
18642 }
18643
18644 /* Read a die, all of its descendents, and all of its siblings; set
18645 all of the fields of all of the dies correctly. Arguments are as
18646 in read_die_and_children.
18647 This the main entry point for reading a DIE and all its children. */
18648
18649 static struct die_info *
18650 read_die_and_siblings (const struct die_reader_specs *reader,
18651 const gdb_byte *info_ptr,
18652 const gdb_byte **new_info_ptr,
18653 struct die_info *parent)
18654 {
18655 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18656 new_info_ptr, parent);
18657
18658 if (dwarf_die_debug)
18659 {
18660 fprintf_unfiltered (gdb_stdlog,
18661 "Read die from %s@0x%x of %s:\n",
18662 reader->die_section->get_name (),
18663 (unsigned) (info_ptr - reader->die_section->buffer),
18664 bfd_get_filename (reader->abfd));
18665 dump_die (die, dwarf_die_debug);
18666 }
18667
18668 return die;
18669 }
18670
18671 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18672 attributes.
18673 The caller is responsible for filling in the extra attributes
18674 and updating (*DIEP)->num_attrs.
18675 Set DIEP to point to a newly allocated die with its information,
18676 except for its child, sibling, and parent fields. */
18677
18678 static const gdb_byte *
18679 read_full_die_1 (const struct die_reader_specs *reader,
18680 struct die_info **diep, const gdb_byte *info_ptr,
18681 int num_extra_attrs)
18682 {
18683 unsigned int abbrev_number, bytes_read, i;
18684 const struct abbrev_info *abbrev;
18685 struct die_info *die;
18686 struct dwarf2_cu *cu = reader->cu;
18687 bfd *abfd = reader->abfd;
18688
18689 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18690 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18691 info_ptr += bytes_read;
18692 if (!abbrev_number)
18693 {
18694 *diep = NULL;
18695 return info_ptr;
18696 }
18697
18698 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18699 if (!abbrev)
18700 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18701 abbrev_number,
18702 bfd_get_filename (abfd));
18703
18704 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18705 die->sect_off = sect_off;
18706 die->tag = abbrev->tag;
18707 die->abbrev = abbrev_number;
18708 die->has_children = abbrev->has_children;
18709
18710 /* Make the result usable.
18711 The caller needs to update num_attrs after adding the extra
18712 attributes. */
18713 die->num_attrs = abbrev->num_attrs;
18714
18715 bool any_need_reprocess = false;
18716 for (i = 0; i < abbrev->num_attrs; ++i)
18717 {
18718 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18719 info_ptr);
18720 if (die->attrs[i].requires_reprocessing_p ())
18721 any_need_reprocess = true;
18722 }
18723
18724 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18725 if (attr != nullptr && attr->form_is_unsigned ())
18726 cu->str_offsets_base = attr->as_unsigned ();
18727
18728 attr = die->attr (DW_AT_loclists_base);
18729 if (attr != nullptr)
18730 cu->loclist_base = attr->as_unsigned ();
18731
18732 auto maybe_addr_base = die->addr_base ();
18733 if (maybe_addr_base.has_value ())
18734 cu->addr_base = *maybe_addr_base;
18735
18736 attr = die->attr (DW_AT_rnglists_base);
18737 if (attr != nullptr)
18738 cu->rnglists_base = attr->as_unsigned ();
18739
18740 if (any_need_reprocess)
18741 {
18742 for (i = 0; i < abbrev->num_attrs; ++i)
18743 {
18744 if (die->attrs[i].requires_reprocessing_p ())
18745 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18746 }
18747 }
18748 *diep = die;
18749 return info_ptr;
18750 }
18751
18752 /* Read a die and all its attributes.
18753 Set DIEP to point to a newly allocated die with its information,
18754 except for its child, sibling, and parent fields. */
18755
18756 static const gdb_byte *
18757 read_full_die (const struct die_reader_specs *reader,
18758 struct die_info **diep, const gdb_byte *info_ptr)
18759 {
18760 const gdb_byte *result;
18761
18762 result = read_full_die_1 (reader, diep, info_ptr, 0);
18763
18764 if (dwarf_die_debug)
18765 {
18766 fprintf_unfiltered (gdb_stdlog,
18767 "Read die from %s@0x%x of %s:\n",
18768 reader->die_section->get_name (),
18769 (unsigned) (info_ptr - reader->die_section->buffer),
18770 bfd_get_filename (reader->abfd));
18771 dump_die (*diep, dwarf_die_debug);
18772 }
18773
18774 return result;
18775 }
18776 \f
18777
18778 /* Returns nonzero if TAG represents a type that we might generate a partial
18779 symbol for. */
18780
18781 static int
18782 is_type_tag_for_partial (int tag, enum language lang)
18783 {
18784 switch (tag)
18785 {
18786 #if 0
18787 /* Some types that would be reasonable to generate partial symbols for,
18788 that we don't at present. Note that normally this does not
18789 matter, mainly because C compilers don't give names to these
18790 types, but instead emit DW_TAG_typedef. */
18791 case DW_TAG_file_type:
18792 case DW_TAG_ptr_to_member_type:
18793 case DW_TAG_set_type:
18794 case DW_TAG_string_type:
18795 case DW_TAG_subroutine_type:
18796 #endif
18797
18798 /* GNAT may emit an array with a name, but no typedef, so we
18799 need to make a symbol in this case. */
18800 case DW_TAG_array_type:
18801 return lang == language_ada;
18802
18803 case DW_TAG_base_type:
18804 case DW_TAG_class_type:
18805 case DW_TAG_interface_type:
18806 case DW_TAG_enumeration_type:
18807 case DW_TAG_structure_type:
18808 case DW_TAG_subrange_type:
18809 case DW_TAG_typedef:
18810 case DW_TAG_union_type:
18811 return 1;
18812 default:
18813 return 0;
18814 }
18815 }
18816
18817 /* Load all DIEs that are interesting for partial symbols into memory. */
18818
18819 static struct partial_die_info *
18820 load_partial_dies (const struct die_reader_specs *reader,
18821 const gdb_byte *info_ptr, int building_psymtab)
18822 {
18823 struct dwarf2_cu *cu = reader->cu;
18824 struct objfile *objfile = cu->per_objfile->objfile;
18825 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18826 unsigned int bytes_read;
18827 unsigned int load_all = 0;
18828 int nesting_level = 1;
18829
18830 parent_die = NULL;
18831 last_die = NULL;
18832
18833 gdb_assert (cu->per_cu != NULL);
18834 if (cu->per_cu->load_all_dies)
18835 load_all = 1;
18836
18837 cu->partial_dies
18838 = htab_create_alloc_ex (cu->header.length / 12,
18839 partial_die_hash,
18840 partial_die_eq,
18841 NULL,
18842 &cu->comp_unit_obstack,
18843 hashtab_obstack_allocate,
18844 dummy_obstack_deallocate);
18845
18846 while (1)
18847 {
18848 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18849 &bytes_read);
18850
18851 /* A NULL abbrev means the end of a series of children. */
18852 if (abbrev == NULL)
18853 {
18854 if (--nesting_level == 0)
18855 return first_die;
18856
18857 info_ptr += bytes_read;
18858 last_die = parent_die;
18859 parent_die = parent_die->die_parent;
18860 continue;
18861 }
18862
18863 /* Check for template arguments. We never save these; if
18864 they're seen, we just mark the parent, and go on our way. */
18865 if (parent_die != NULL
18866 && cu->language == language_cplus
18867 && (abbrev->tag == DW_TAG_template_type_param
18868 || abbrev->tag == DW_TAG_template_value_param))
18869 {
18870 parent_die->has_template_arguments = 1;
18871
18872 if (!load_all)
18873 {
18874 /* We don't need a partial DIE for the template argument. */
18875 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18876 continue;
18877 }
18878 }
18879
18880 /* We only recurse into c++ subprograms looking for template arguments.
18881 Skip their other children. */
18882 if (!load_all
18883 && cu->language == language_cplus
18884 && parent_die != NULL
18885 && parent_die->tag == DW_TAG_subprogram
18886 && abbrev->tag != DW_TAG_inlined_subroutine)
18887 {
18888 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18889 continue;
18890 }
18891
18892 /* Check whether this DIE is interesting enough to save. Normally
18893 we would not be interested in members here, but there may be
18894 later variables referencing them via DW_AT_specification (for
18895 static members). */
18896 if (!load_all
18897 && !is_type_tag_for_partial (abbrev->tag, cu->language)
18898 && abbrev->tag != DW_TAG_constant
18899 && abbrev->tag != DW_TAG_enumerator
18900 && abbrev->tag != DW_TAG_subprogram
18901 && abbrev->tag != DW_TAG_inlined_subroutine
18902 && abbrev->tag != DW_TAG_lexical_block
18903 && abbrev->tag != DW_TAG_variable
18904 && abbrev->tag != DW_TAG_namespace
18905 && abbrev->tag != DW_TAG_module
18906 && abbrev->tag != DW_TAG_member
18907 && abbrev->tag != DW_TAG_imported_unit
18908 && abbrev->tag != DW_TAG_imported_declaration)
18909 {
18910 /* Otherwise we skip to the next sibling, if any. */
18911 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18912 continue;
18913 }
18914
18915 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18916 abbrev);
18917
18918 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18919
18920 /* This two-pass algorithm for processing partial symbols has a
18921 high cost in cache pressure. Thus, handle some simple cases
18922 here which cover the majority of C partial symbols. DIEs
18923 which neither have specification tags in them, nor could have
18924 specification tags elsewhere pointing at them, can simply be
18925 processed and discarded.
18926
18927 This segment is also optional; scan_partial_symbols and
18928 add_partial_symbol will handle these DIEs if we chain
18929 them in normally. When compilers which do not emit large
18930 quantities of duplicate debug information are more common,
18931 this code can probably be removed. */
18932
18933 /* Any complete simple types at the top level (pretty much all
18934 of them, for a language without namespaces), can be processed
18935 directly. */
18936 if (parent_die == NULL
18937 && pdi.has_specification == 0
18938 && pdi.is_declaration == 0
18939 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18940 || pdi.tag == DW_TAG_base_type
18941 || pdi.tag == DW_TAG_array_type
18942 || pdi.tag == DW_TAG_subrange_type))
18943 {
18944 if (building_psymtab && pdi.raw_name != NULL)
18945 add_partial_symbol (&pdi, cu);
18946
18947 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18948 continue;
18949 }
18950
18951 /* The exception for DW_TAG_typedef with has_children above is
18952 a workaround of GCC PR debug/47510. In the case of this complaint
18953 type_name_or_error will error on such types later.
18954
18955 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18956 it could not find the child DIEs referenced later, this is checked
18957 above. In correct DWARF DW_TAG_typedef should have no children. */
18958
18959 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18960 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18961 "- DIE at %s [in module %s]"),
18962 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18963
18964 /* If we're at the second level, and we're an enumerator, and
18965 our parent has no specification (meaning possibly lives in a
18966 namespace elsewhere), then we can add the partial symbol now
18967 instead of queueing it. */
18968 if (pdi.tag == DW_TAG_enumerator
18969 && parent_die != NULL
18970 && parent_die->die_parent == NULL
18971 && parent_die->tag == DW_TAG_enumeration_type
18972 && parent_die->has_specification == 0)
18973 {
18974 if (pdi.raw_name == NULL)
18975 complaint (_("malformed enumerator DIE ignored"));
18976 else if (building_psymtab)
18977 add_partial_symbol (&pdi, cu);
18978
18979 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18980 continue;
18981 }
18982
18983 struct partial_die_info *part_die
18984 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18985
18986 /* We'll save this DIE so link it in. */
18987 part_die->die_parent = parent_die;
18988 part_die->die_sibling = NULL;
18989 part_die->die_child = NULL;
18990
18991 if (last_die && last_die == parent_die)
18992 last_die->die_child = part_die;
18993 else if (last_die)
18994 last_die->die_sibling = part_die;
18995
18996 last_die = part_die;
18997
18998 if (first_die == NULL)
18999 first_die = part_die;
19000
19001 /* Maybe add the DIE to the hash table. Not all DIEs that we
19002 find interesting need to be in the hash table, because we
19003 also have the parent/sibling/child chains; only those that we
19004 might refer to by offset later during partial symbol reading.
19005
19006 For now this means things that might have be the target of a
19007 DW_AT_specification, DW_AT_abstract_origin, or
19008 DW_AT_extension. DW_AT_extension will refer only to
19009 namespaces; DW_AT_abstract_origin refers to functions (and
19010 many things under the function DIE, but we do not recurse
19011 into function DIEs during partial symbol reading) and
19012 possibly variables as well; DW_AT_specification refers to
19013 declarations. Declarations ought to have the DW_AT_declaration
19014 flag. It happens that GCC forgets to put it in sometimes, but
19015 only for functions, not for types.
19016
19017 Adding more things than necessary to the hash table is harmless
19018 except for the performance cost. Adding too few will result in
19019 wasted time in find_partial_die, when we reread the compilation
19020 unit with load_all_dies set. */
19021
19022 if (load_all
19023 || abbrev->tag == DW_TAG_constant
19024 || abbrev->tag == DW_TAG_subprogram
19025 || abbrev->tag == DW_TAG_variable
19026 || abbrev->tag == DW_TAG_namespace
19027 || part_die->is_declaration)
19028 {
19029 void **slot;
19030
19031 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19032 to_underlying (part_die->sect_off),
19033 INSERT);
19034 *slot = part_die;
19035 }
19036
19037 /* For some DIEs we want to follow their children (if any). For C
19038 we have no reason to follow the children of structures; for other
19039 languages we have to, so that we can get at method physnames
19040 to infer fully qualified class names, for DW_AT_specification,
19041 and for C++ template arguments. For C++, we also look one level
19042 inside functions to find template arguments (if the name of the
19043 function does not already contain the template arguments).
19044
19045 For Ada and Fortran, we need to scan the children of subprograms
19046 and lexical blocks as well because these languages allow the
19047 definition of nested entities that could be interesting for the
19048 debugger, such as nested subprograms for instance. */
19049 if (last_die->has_children
19050 && (load_all
19051 || last_die->tag == DW_TAG_namespace
19052 || last_die->tag == DW_TAG_module
19053 || last_die->tag == DW_TAG_enumeration_type
19054 || (cu->language == language_cplus
19055 && last_die->tag == DW_TAG_subprogram
19056 && (last_die->raw_name == NULL
19057 || strchr (last_die->raw_name, '<') == NULL))
19058 || (cu->language != language_c
19059 && (last_die->tag == DW_TAG_class_type
19060 || last_die->tag == DW_TAG_interface_type
19061 || last_die->tag == DW_TAG_structure_type
19062 || last_die->tag == DW_TAG_union_type))
19063 || ((cu->language == language_ada
19064 || cu->language == language_fortran)
19065 && (last_die->tag == DW_TAG_subprogram
19066 || last_die->tag == DW_TAG_lexical_block))))
19067 {
19068 nesting_level++;
19069 parent_die = last_die;
19070 continue;
19071 }
19072
19073 /* Otherwise we skip to the next sibling, if any. */
19074 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19075
19076 /* Back to the top, do it again. */
19077 }
19078 }
19079
19080 partial_die_info::partial_die_info (sect_offset sect_off_,
19081 const struct abbrev_info *abbrev)
19082 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19083 {
19084 }
19085
19086 /* See class definition. */
19087
19088 const char *
19089 partial_die_info::name (dwarf2_cu *cu)
19090 {
19091 if (!canonical_name && raw_name != nullptr)
19092 {
19093 struct objfile *objfile = cu->per_objfile->objfile;
19094 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19095 canonical_name = 1;
19096 }
19097
19098 return raw_name;
19099 }
19100
19101 /* Read a minimal amount of information into the minimal die structure.
19102 INFO_PTR should point just after the initial uleb128 of a DIE. */
19103
19104 const gdb_byte *
19105 partial_die_info::read (const struct die_reader_specs *reader,
19106 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19107 {
19108 struct dwarf2_cu *cu = reader->cu;
19109 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19110 unsigned int i;
19111 int has_low_pc_attr = 0;
19112 int has_high_pc_attr = 0;
19113 int high_pc_relative = 0;
19114
19115 for (i = 0; i < abbrev.num_attrs; ++i)
19116 {
19117 attribute attr;
19118 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19119 /* String and address offsets that need to do the reprocessing have
19120 already been read at this point, so there is no need to wait until
19121 the loop terminates to do the reprocessing. */
19122 if (attr.requires_reprocessing_p ())
19123 read_attribute_reprocess (reader, &attr, tag);
19124 /* Store the data if it is of an attribute we want to keep in a
19125 partial symbol table. */
19126 switch (attr.name)
19127 {
19128 case DW_AT_name:
19129 switch (tag)
19130 {
19131 case DW_TAG_compile_unit:
19132 case DW_TAG_partial_unit:
19133 case DW_TAG_type_unit:
19134 /* Compilation units have a DW_AT_name that is a filename, not
19135 a source language identifier. */
19136 case DW_TAG_enumeration_type:
19137 case DW_TAG_enumerator:
19138 /* These tags always have simple identifiers already; no need
19139 to canonicalize them. */
19140 canonical_name = 1;
19141 raw_name = attr.as_string ();
19142 break;
19143 default:
19144 canonical_name = 0;
19145 raw_name = attr.as_string ();
19146 break;
19147 }
19148 break;
19149 case DW_AT_linkage_name:
19150 case DW_AT_MIPS_linkage_name:
19151 /* Note that both forms of linkage name might appear. We
19152 assume they will be the same, and we only store the last
19153 one we see. */
19154 linkage_name = attr.as_string ();
19155 break;
19156 case DW_AT_low_pc:
19157 has_low_pc_attr = 1;
19158 lowpc = attr.as_address ();
19159 break;
19160 case DW_AT_high_pc:
19161 has_high_pc_attr = 1;
19162 highpc = attr.as_address ();
19163 if (cu->header.version >= 4 && attr.form_is_constant ())
19164 high_pc_relative = 1;
19165 break;
19166 case DW_AT_location:
19167 /* Support the .debug_loc offsets. */
19168 if (attr.form_is_block ())
19169 {
19170 d.locdesc = attr.as_block ();
19171 }
19172 else if (attr.form_is_section_offset ())
19173 {
19174 dwarf2_complex_location_expr_complaint ();
19175 }
19176 else
19177 {
19178 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19179 "partial symbol information");
19180 }
19181 break;
19182 case DW_AT_external:
19183 is_external = attr.as_boolean ();
19184 break;
19185 case DW_AT_declaration:
19186 is_declaration = attr.as_boolean ();
19187 break;
19188 case DW_AT_type:
19189 has_type = 1;
19190 break;
19191 case DW_AT_abstract_origin:
19192 case DW_AT_specification:
19193 case DW_AT_extension:
19194 has_specification = 1;
19195 spec_offset = attr.get_ref_die_offset ();
19196 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19197 || cu->per_cu->is_dwz);
19198 break;
19199 case DW_AT_sibling:
19200 /* Ignore absolute siblings, they might point outside of
19201 the current compile unit. */
19202 if (attr.form == DW_FORM_ref_addr)
19203 complaint (_("ignoring absolute DW_AT_sibling"));
19204 else
19205 {
19206 const gdb_byte *buffer = reader->buffer;
19207 sect_offset off = attr.get_ref_die_offset ();
19208 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19209
19210 if (sibling_ptr < info_ptr)
19211 complaint (_("DW_AT_sibling points backwards"));
19212 else if (sibling_ptr > reader->buffer_end)
19213 reader->die_section->overflow_complaint ();
19214 else
19215 sibling = sibling_ptr;
19216 }
19217 break;
19218 case DW_AT_byte_size:
19219 has_byte_size = 1;
19220 break;
19221 case DW_AT_const_value:
19222 has_const_value = 1;
19223 break;
19224 case DW_AT_calling_convention:
19225 /* DWARF doesn't provide a way to identify a program's source-level
19226 entry point. DW_AT_calling_convention attributes are only meant
19227 to describe functions' calling conventions.
19228
19229 However, because it's a necessary piece of information in
19230 Fortran, and before DWARF 4 DW_CC_program was the only
19231 piece of debugging information whose definition refers to
19232 a 'main program' at all, several compilers marked Fortran
19233 main programs with DW_CC_program --- even when those
19234 functions use the standard calling conventions.
19235
19236 Although DWARF now specifies a way to provide this
19237 information, we support this practice for backward
19238 compatibility. */
19239 if (attr.constant_value (0) == DW_CC_program
19240 && cu->language == language_fortran)
19241 main_subprogram = 1;
19242 break;
19243 case DW_AT_inline:
19244 {
19245 LONGEST value = attr.constant_value (-1);
19246 if (value == DW_INL_inlined
19247 || value == DW_INL_declared_inlined)
19248 may_be_inlined = 1;
19249 }
19250 break;
19251
19252 case DW_AT_import:
19253 if (tag == DW_TAG_imported_unit)
19254 {
19255 d.sect_off = attr.get_ref_die_offset ();
19256 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19257 || cu->per_cu->is_dwz);
19258 }
19259 break;
19260
19261 case DW_AT_main_subprogram:
19262 main_subprogram = attr.as_boolean ();
19263 break;
19264
19265 case DW_AT_ranges:
19266 {
19267 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19268 on DWARF version). */
19269 ULONGEST ranges_offset = attr.as_unsigned ();
19270
19271 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19272 this value. */
19273 if (tag != DW_TAG_compile_unit)
19274 ranges_offset += cu->gnu_ranges_base;
19275
19276 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19277 nullptr, tag))
19278 has_pc_info = 1;
19279 }
19280 break;
19281
19282 default:
19283 break;
19284 }
19285 }
19286
19287 /* For Ada, if both the name and the linkage name appear, we prefer
19288 the latter. This lets "catch exception" work better, regardless
19289 of the order in which the name and linkage name were emitted.
19290 Really, though, this is just a workaround for the fact that gdb
19291 doesn't store both the name and the linkage name. */
19292 if (cu->language == language_ada && linkage_name != nullptr)
19293 raw_name = linkage_name;
19294
19295 if (high_pc_relative)
19296 highpc += lowpc;
19297
19298 if (has_low_pc_attr && has_high_pc_attr)
19299 {
19300 /* When using the GNU linker, .gnu.linkonce. sections are used to
19301 eliminate duplicate copies of functions and vtables and such.
19302 The linker will arbitrarily choose one and discard the others.
19303 The AT_*_pc values for such functions refer to local labels in
19304 these sections. If the section from that file was discarded, the
19305 labels are not in the output, so the relocs get a value of 0.
19306 If this is a discarded function, mark the pc bounds as invalid,
19307 so that GDB will ignore it. */
19308 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19309 {
19310 struct objfile *objfile = per_objfile->objfile;
19311 struct gdbarch *gdbarch = objfile->arch ();
19312
19313 complaint (_("DW_AT_low_pc %s is zero "
19314 "for DIE at %s [in module %s]"),
19315 paddress (gdbarch, lowpc),
19316 sect_offset_str (sect_off),
19317 objfile_name (objfile));
19318 }
19319 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19320 else if (lowpc >= highpc)
19321 {
19322 struct objfile *objfile = per_objfile->objfile;
19323 struct gdbarch *gdbarch = objfile->arch ();
19324
19325 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19326 "for DIE at %s [in module %s]"),
19327 paddress (gdbarch, lowpc),
19328 paddress (gdbarch, highpc),
19329 sect_offset_str (sect_off),
19330 objfile_name (objfile));
19331 }
19332 else
19333 has_pc_info = 1;
19334 }
19335
19336 return info_ptr;
19337 }
19338
19339 /* Find a cached partial DIE at OFFSET in CU. */
19340
19341 struct partial_die_info *
19342 dwarf2_cu::find_partial_die (sect_offset sect_off)
19343 {
19344 struct partial_die_info *lookup_die = NULL;
19345 struct partial_die_info part_die (sect_off);
19346
19347 lookup_die = ((struct partial_die_info *)
19348 htab_find_with_hash (partial_dies, &part_die,
19349 to_underlying (sect_off)));
19350
19351 return lookup_die;
19352 }
19353
19354 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19355 except in the case of .debug_types DIEs which do not reference
19356 outside their CU (they do however referencing other types via
19357 DW_FORM_ref_sig8). */
19358
19359 static const struct cu_partial_die_info
19360 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19361 {
19362 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19363 struct objfile *objfile = per_objfile->objfile;
19364 struct partial_die_info *pd = NULL;
19365
19366 if (offset_in_dwz == cu->per_cu->is_dwz
19367 && cu->header.offset_in_cu_p (sect_off))
19368 {
19369 pd = cu->find_partial_die (sect_off);
19370 if (pd != NULL)
19371 return { cu, pd };
19372 /* We missed recording what we needed.
19373 Load all dies and try again. */
19374 }
19375 else
19376 {
19377 /* TUs don't reference other CUs/TUs (except via type signatures). */
19378 if (cu->per_cu->is_debug_types)
19379 {
19380 error (_("Dwarf Error: Type Unit at offset %s contains"
19381 " external reference to offset %s [in module %s].\n"),
19382 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19383 bfd_get_filename (objfile->obfd));
19384 }
19385 dwarf2_per_cu_data *per_cu
19386 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19387 per_objfile);
19388
19389 cu = per_objfile->get_cu (per_cu);
19390 if (cu == NULL || cu->partial_dies == NULL)
19391 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19392
19393 cu = per_objfile->get_cu (per_cu);
19394
19395 cu->last_used = 0;
19396 pd = cu->find_partial_die (sect_off);
19397 }
19398
19399 /* If we didn't find it, and not all dies have been loaded,
19400 load them all and try again. */
19401
19402 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19403 {
19404 cu->per_cu->load_all_dies = 1;
19405
19406 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19407 THIS_CU->cu may already be in use. So we can't just free it and
19408 replace its DIEs with the ones we read in. Instead, we leave those
19409 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19410 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19411 set. */
19412 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19413
19414 pd = cu->find_partial_die (sect_off);
19415 }
19416
19417 if (pd == NULL)
19418 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19419 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19420 return { cu, pd };
19421 }
19422
19423 /* See if we can figure out if the class lives in a namespace. We do
19424 this by looking for a member function; its demangled name will
19425 contain namespace info, if there is any. */
19426
19427 static void
19428 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19429 struct dwarf2_cu *cu)
19430 {
19431 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19432 what template types look like, because the demangler
19433 frequently doesn't give the same name as the debug info. We
19434 could fix this by only using the demangled name to get the
19435 prefix (but see comment in read_structure_type). */
19436
19437 struct partial_die_info *real_pdi;
19438 struct partial_die_info *child_pdi;
19439
19440 /* If this DIE (this DIE's specification, if any) has a parent, then
19441 we should not do this. We'll prepend the parent's fully qualified
19442 name when we create the partial symbol. */
19443
19444 real_pdi = struct_pdi;
19445 while (real_pdi->has_specification)
19446 {
19447 auto res = find_partial_die (real_pdi->spec_offset,
19448 real_pdi->spec_is_dwz, cu);
19449 real_pdi = res.pdi;
19450 cu = res.cu;
19451 }
19452
19453 if (real_pdi->die_parent != NULL)
19454 return;
19455
19456 for (child_pdi = struct_pdi->die_child;
19457 child_pdi != NULL;
19458 child_pdi = child_pdi->die_sibling)
19459 {
19460 if (child_pdi->tag == DW_TAG_subprogram
19461 && child_pdi->linkage_name != NULL)
19462 {
19463 gdb::unique_xmalloc_ptr<char> actual_class_name
19464 (cu->language_defn->class_name_from_physname
19465 (child_pdi->linkage_name));
19466 if (actual_class_name != NULL)
19467 {
19468 struct objfile *objfile = cu->per_objfile->objfile;
19469 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19470 struct_pdi->canonical_name = 1;
19471 }
19472 break;
19473 }
19474 }
19475 }
19476
19477 /* Return true if a DIE with TAG may have the DW_AT_const_value
19478 attribute. */
19479
19480 static bool
19481 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19482 {
19483 switch (tag)
19484 {
19485 case DW_TAG_constant:
19486 case DW_TAG_enumerator:
19487 case DW_TAG_formal_parameter:
19488 case DW_TAG_template_value_param:
19489 case DW_TAG_variable:
19490 return true;
19491 }
19492
19493 return false;
19494 }
19495
19496 void
19497 partial_die_info::fixup (struct dwarf2_cu *cu)
19498 {
19499 /* Once we've fixed up a die, there's no point in doing so again.
19500 This also avoids a memory leak if we were to call
19501 guess_partial_die_structure_name multiple times. */
19502 if (fixup_called)
19503 return;
19504
19505 /* If we found a reference attribute and the DIE has no name, try
19506 to find a name in the referred to DIE. */
19507
19508 if (raw_name == NULL && has_specification)
19509 {
19510 struct partial_die_info *spec_die;
19511
19512 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19513 spec_die = res.pdi;
19514 cu = res.cu;
19515
19516 spec_die->fixup (cu);
19517
19518 if (spec_die->raw_name)
19519 {
19520 raw_name = spec_die->raw_name;
19521 canonical_name = spec_die->canonical_name;
19522
19523 /* Copy DW_AT_external attribute if it is set. */
19524 if (spec_die->is_external)
19525 is_external = spec_die->is_external;
19526 }
19527 }
19528
19529 if (!has_const_value && has_specification
19530 && can_have_DW_AT_const_value_p (tag))
19531 {
19532 struct partial_die_info *spec_die;
19533
19534 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19535 spec_die = res.pdi;
19536 cu = res.cu;
19537
19538 spec_die->fixup (cu);
19539
19540 if (spec_die->has_const_value)
19541 {
19542 /* Copy DW_AT_const_value attribute if it is set. */
19543 has_const_value = spec_die->has_const_value;
19544 }
19545 }
19546
19547 /* Set default names for some unnamed DIEs. */
19548
19549 if (raw_name == NULL && tag == DW_TAG_namespace)
19550 {
19551 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19552 canonical_name = 1;
19553 }
19554
19555 /* If there is no parent die to provide a namespace, and there are
19556 children, see if we can determine the namespace from their linkage
19557 name. */
19558 if (cu->language == language_cplus
19559 && !cu->per_objfile->per_bfd->types.empty ()
19560 && die_parent == NULL
19561 && has_children
19562 && (tag == DW_TAG_class_type
19563 || tag == DW_TAG_structure_type
19564 || tag == DW_TAG_union_type))
19565 guess_partial_die_structure_name (this, cu);
19566
19567 /* GCC might emit a nameless struct or union that has a linkage
19568 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19569 if (raw_name == NULL
19570 && (tag == DW_TAG_class_type
19571 || tag == DW_TAG_interface_type
19572 || tag == DW_TAG_structure_type
19573 || tag == DW_TAG_union_type)
19574 && linkage_name != NULL)
19575 {
19576 gdb::unique_xmalloc_ptr<char> demangled
19577 (gdb_demangle (linkage_name, DMGL_TYPES));
19578 if (demangled != nullptr)
19579 {
19580 const char *base;
19581
19582 /* Strip any leading namespaces/classes, keep only the base name.
19583 DW_AT_name for named DIEs does not contain the prefixes. */
19584 base = strrchr (demangled.get (), ':');
19585 if (base && base > demangled.get () && base[-1] == ':')
19586 base++;
19587 else
19588 base = demangled.get ();
19589
19590 struct objfile *objfile = cu->per_objfile->objfile;
19591 raw_name = objfile->intern (base);
19592 canonical_name = 1;
19593 }
19594 }
19595
19596 fixup_called = 1;
19597 }
19598
19599 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19600 contents from the given SECTION in the HEADER.
19601
19602 HEADER_OFFSET is the offset of the header in the section. */
19603 static void
19604 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19605 struct dwarf2_section_info *section,
19606 sect_offset header_offset)
19607 {
19608 unsigned int bytes_read;
19609 bfd *abfd = section->get_bfd_owner ();
19610 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19611
19612 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19613 info_ptr += bytes_read;
19614
19615 header->version = read_2_bytes (abfd, info_ptr);
19616 info_ptr += 2;
19617
19618 header->addr_size = read_1_byte (abfd, info_ptr);
19619 info_ptr += 1;
19620
19621 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19622 info_ptr += 1;
19623
19624 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19625 }
19626
19627 /* Return the DW_AT_loclists_base value for the CU. */
19628 static ULONGEST
19629 lookup_loclist_base (struct dwarf2_cu *cu)
19630 {
19631 /* For the .dwo unit, the loclist_base points to the first offset following
19632 the header. The header consists of the following entities-
19633 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19634 bit format)
19635 2. version (2 bytes)
19636 3. address size (1 byte)
19637 4. segment selector size (1 byte)
19638 5. offset entry count (4 bytes)
19639 These sizes are derived as per the DWARFv5 standard. */
19640 if (cu->dwo_unit != nullptr)
19641 {
19642 if (cu->header.initial_length_size == 4)
19643 return LOCLIST_HEADER_SIZE32;
19644 return LOCLIST_HEADER_SIZE64;
19645 }
19646 return cu->loclist_base;
19647 }
19648
19649 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19650 array of offsets in the .debug_loclists section. */
19651
19652 static sect_offset
19653 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19654 {
19655 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19656 struct objfile *objfile = per_objfile->objfile;
19657 bfd *abfd = objfile->obfd;
19658 ULONGEST loclist_header_size =
19659 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19660 : LOCLIST_HEADER_SIZE64);
19661 ULONGEST loclist_base = lookup_loclist_base (cu);
19662
19663 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19664 ULONGEST start_offset =
19665 loclist_base + loclist_index * cu->header.offset_size;
19666
19667 /* Get loclists section. */
19668 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19669
19670 /* Read the loclists section content. */
19671 section->read (objfile);
19672 if (section->buffer == NULL)
19673 error (_("DW_FORM_loclistx used without .debug_loclists "
19674 "section [in module %s]"), objfile_name (objfile));
19675
19676 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19677 so if loclist_base is smaller than the header size, we have a problem. */
19678 if (loclist_base < loclist_header_size)
19679 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19680 objfile_name (objfile));
19681
19682 /* Read the header of the loclists contribution. */
19683 struct loclists_rnglists_header header;
19684 read_loclists_rnglists_header (&header, section,
19685 (sect_offset) (loclist_base - loclist_header_size));
19686
19687 /* Verify the loclist index is valid. */
19688 if (loclist_index >= header.offset_entry_count)
19689 error (_("DW_FORM_loclistx pointing outside of "
19690 ".debug_loclists offset array [in module %s]"),
19691 objfile_name (objfile));
19692
19693 /* Validate that reading won't go beyond the end of the section. */
19694 if (start_offset + cu->header.offset_size > section->size)
19695 error (_("Reading DW_FORM_loclistx index beyond end of"
19696 ".debug_loclists section [in module %s]"),
19697 objfile_name (objfile));
19698
19699 const gdb_byte *info_ptr = section->buffer + start_offset;
19700
19701 if (cu->header.offset_size == 4)
19702 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19703 else
19704 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19705 }
19706
19707 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19708 array of offsets in the .debug_rnglists section. */
19709
19710 static sect_offset
19711 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19712 dwarf_tag tag)
19713 {
19714 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19715 struct objfile *objfile = dwarf2_per_objfile->objfile;
19716 bfd *abfd = objfile->obfd;
19717 ULONGEST rnglist_header_size =
19718 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19719 : RNGLIST_HEADER_SIZE64);
19720
19721 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19722 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19723 doesn't apply. */
19724 ULONGEST rnglist_base =
19725 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19726
19727 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19728 ULONGEST start_offset =
19729 rnglist_base + rnglist_index * cu->header.offset_size;
19730
19731 /* Get rnglists section. */
19732 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19733
19734 /* Read the rnglists section content. */
19735 section->read (objfile);
19736 if (section->buffer == nullptr)
19737 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19738 "[in module %s]"),
19739 objfile_name (objfile));
19740
19741 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19742 so if rnglist_base is smaller than the header size, we have a problem. */
19743 if (rnglist_base < rnglist_header_size)
19744 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19745 objfile_name (objfile));
19746
19747 /* Read the header of the rnglists contribution. */
19748 struct loclists_rnglists_header header;
19749 read_loclists_rnglists_header (&header, section,
19750 (sect_offset) (rnglist_base - rnglist_header_size));
19751
19752 /* Verify the rnglist index is valid. */
19753 if (rnglist_index >= header.offset_entry_count)
19754 error (_("DW_FORM_rnglistx index pointing outside of "
19755 ".debug_rnglists offset array [in module %s]"),
19756 objfile_name (objfile));
19757
19758 /* Validate that reading won't go beyond the end of the section. */
19759 if (start_offset + cu->header.offset_size > section->size)
19760 error (_("Reading DW_FORM_rnglistx index beyond end of"
19761 ".debug_rnglists section [in module %s]"),
19762 objfile_name (objfile));
19763
19764 const gdb_byte *info_ptr = section->buffer + start_offset;
19765
19766 if (cu->header.offset_size == 4)
19767 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19768 else
19769 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19770 }
19771
19772 /* Process the attributes that had to be skipped in the first round. These
19773 attributes are the ones that need str_offsets_base or addr_base attributes.
19774 They could not have been processed in the first round, because at the time
19775 the values of str_offsets_base or addr_base may not have been known. */
19776 static void
19777 read_attribute_reprocess (const struct die_reader_specs *reader,
19778 struct attribute *attr, dwarf_tag tag)
19779 {
19780 struct dwarf2_cu *cu = reader->cu;
19781 switch (attr->form)
19782 {
19783 case DW_FORM_addrx:
19784 case DW_FORM_GNU_addr_index:
19785 attr->set_address (read_addr_index (cu,
19786 attr->as_unsigned_reprocess ()));
19787 break;
19788 case DW_FORM_loclistx:
19789 {
19790 sect_offset loclists_sect_off
19791 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19792
19793 attr->set_unsigned (to_underlying (loclists_sect_off));
19794 }
19795 break;
19796 case DW_FORM_rnglistx:
19797 {
19798 sect_offset rnglists_sect_off
19799 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19800
19801 attr->set_unsigned (to_underlying (rnglists_sect_off));
19802 }
19803 break;
19804 case DW_FORM_strx:
19805 case DW_FORM_strx1:
19806 case DW_FORM_strx2:
19807 case DW_FORM_strx3:
19808 case DW_FORM_strx4:
19809 case DW_FORM_GNU_str_index:
19810 {
19811 unsigned int str_index = attr->as_unsigned_reprocess ();
19812 gdb_assert (!attr->canonical_string_p ());
19813 if (reader->dwo_file != NULL)
19814 attr->set_string_noncanonical (read_dwo_str_index (reader,
19815 str_index));
19816 else
19817 attr->set_string_noncanonical (read_stub_str_index (cu,
19818 str_index));
19819 break;
19820 }
19821 default:
19822 gdb_assert_not_reached (_("Unexpected DWARF form."));
19823 }
19824 }
19825
19826 /* Read an attribute value described by an attribute form. */
19827
19828 static const gdb_byte *
19829 read_attribute_value (const struct die_reader_specs *reader,
19830 struct attribute *attr, unsigned form,
19831 LONGEST implicit_const, const gdb_byte *info_ptr)
19832 {
19833 struct dwarf2_cu *cu = reader->cu;
19834 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19835 struct objfile *objfile = per_objfile->objfile;
19836 bfd *abfd = reader->abfd;
19837 struct comp_unit_head *cu_header = &cu->header;
19838 unsigned int bytes_read;
19839 struct dwarf_block *blk;
19840
19841 attr->form = (enum dwarf_form) form;
19842 switch (form)
19843 {
19844 case DW_FORM_ref_addr:
19845 if (cu_header->version == 2)
19846 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
19847 &bytes_read));
19848 else
19849 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19850 &bytes_read));
19851 info_ptr += bytes_read;
19852 break;
19853 case DW_FORM_GNU_ref_alt:
19854 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19855 &bytes_read));
19856 info_ptr += bytes_read;
19857 break;
19858 case DW_FORM_addr:
19859 {
19860 struct gdbarch *gdbarch = objfile->arch ();
19861 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
19862 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19863 attr->set_address (addr);
19864 info_ptr += bytes_read;
19865 }
19866 break;
19867 case DW_FORM_block2:
19868 blk = dwarf_alloc_block (cu);
19869 blk->size = read_2_bytes (abfd, info_ptr);
19870 info_ptr += 2;
19871 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19872 info_ptr += blk->size;
19873 attr->set_block (blk);
19874 break;
19875 case DW_FORM_block4:
19876 blk = dwarf_alloc_block (cu);
19877 blk->size = read_4_bytes (abfd, info_ptr);
19878 info_ptr += 4;
19879 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19880 info_ptr += blk->size;
19881 attr->set_block (blk);
19882 break;
19883 case DW_FORM_data2:
19884 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19885 info_ptr += 2;
19886 break;
19887 case DW_FORM_data4:
19888 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19889 info_ptr += 4;
19890 break;
19891 case DW_FORM_data8:
19892 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19893 info_ptr += 8;
19894 break;
19895 case DW_FORM_data16:
19896 blk = dwarf_alloc_block (cu);
19897 blk->size = 16;
19898 blk->data = read_n_bytes (abfd, info_ptr, 16);
19899 info_ptr += 16;
19900 attr->set_block (blk);
19901 break;
19902 case DW_FORM_sec_offset:
19903 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19904 &bytes_read));
19905 info_ptr += bytes_read;
19906 break;
19907 case DW_FORM_loclistx:
19908 {
19909 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19910 &bytes_read));
19911 info_ptr += bytes_read;
19912 }
19913 break;
19914 case DW_FORM_string:
19915 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19916 &bytes_read));
19917 info_ptr += bytes_read;
19918 break;
19919 case DW_FORM_strp:
19920 if (!cu->per_cu->is_dwz)
19921 {
19922 attr->set_string_noncanonical
19923 (read_indirect_string (per_objfile,
19924 abfd, info_ptr, cu_header,
19925 &bytes_read));
19926 info_ptr += bytes_read;
19927 break;
19928 }
19929 /* FALLTHROUGH */
19930 case DW_FORM_line_strp:
19931 if (!cu->per_cu->is_dwz)
19932 {
19933 attr->set_string_noncanonical
19934 (per_objfile->read_line_string (info_ptr, cu_header,
19935 &bytes_read));
19936 info_ptr += bytes_read;
19937 break;
19938 }
19939 /* FALLTHROUGH */
19940 case DW_FORM_GNU_strp_alt:
19941 {
19942 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19943 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19944 &bytes_read);
19945
19946 attr->set_string_noncanonical
19947 (dwz->read_string (objfile, str_offset));
19948 info_ptr += bytes_read;
19949 }
19950 break;
19951 case DW_FORM_exprloc:
19952 case DW_FORM_block:
19953 blk = dwarf_alloc_block (cu);
19954 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19955 info_ptr += bytes_read;
19956 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19957 info_ptr += blk->size;
19958 attr->set_block (blk);
19959 break;
19960 case DW_FORM_block1:
19961 blk = dwarf_alloc_block (cu);
19962 blk->size = read_1_byte (abfd, info_ptr);
19963 info_ptr += 1;
19964 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19965 info_ptr += blk->size;
19966 attr->set_block (blk);
19967 break;
19968 case DW_FORM_data1:
19969 case DW_FORM_flag:
19970 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19971 info_ptr += 1;
19972 break;
19973 case DW_FORM_flag_present:
19974 attr->set_unsigned (1);
19975 break;
19976 case DW_FORM_sdata:
19977 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19978 info_ptr += bytes_read;
19979 break;
19980 case DW_FORM_rnglistx:
19981 {
19982 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19983 &bytes_read));
19984 info_ptr += bytes_read;
19985 }
19986 break;
19987 case DW_FORM_udata:
19988 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19989 info_ptr += bytes_read;
19990 break;
19991 case DW_FORM_ref1:
19992 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19993 + read_1_byte (abfd, info_ptr)));
19994 info_ptr += 1;
19995 break;
19996 case DW_FORM_ref2:
19997 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19998 + read_2_bytes (abfd, info_ptr)));
19999 info_ptr += 2;
20000 break;
20001 case DW_FORM_ref4:
20002 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20003 + read_4_bytes (abfd, info_ptr)));
20004 info_ptr += 4;
20005 break;
20006 case DW_FORM_ref8:
20007 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20008 + read_8_bytes (abfd, info_ptr)));
20009 info_ptr += 8;
20010 break;
20011 case DW_FORM_ref_sig8:
20012 attr->set_signature (read_8_bytes (abfd, info_ptr));
20013 info_ptr += 8;
20014 break;
20015 case DW_FORM_ref_udata:
20016 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20017 + read_unsigned_leb128 (abfd, info_ptr,
20018 &bytes_read)));
20019 info_ptr += bytes_read;
20020 break;
20021 case DW_FORM_indirect:
20022 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20023 info_ptr += bytes_read;
20024 if (form == DW_FORM_implicit_const)
20025 {
20026 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20027 info_ptr += bytes_read;
20028 }
20029 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20030 info_ptr);
20031 break;
20032 case DW_FORM_implicit_const:
20033 attr->set_signed (implicit_const);
20034 break;
20035 case DW_FORM_addrx:
20036 case DW_FORM_GNU_addr_index:
20037 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20038 &bytes_read));
20039 info_ptr += bytes_read;
20040 break;
20041 case DW_FORM_strx:
20042 case DW_FORM_strx1:
20043 case DW_FORM_strx2:
20044 case DW_FORM_strx3:
20045 case DW_FORM_strx4:
20046 case DW_FORM_GNU_str_index:
20047 {
20048 ULONGEST str_index;
20049 if (form == DW_FORM_strx1)
20050 {
20051 str_index = read_1_byte (abfd, info_ptr);
20052 info_ptr += 1;
20053 }
20054 else if (form == DW_FORM_strx2)
20055 {
20056 str_index = read_2_bytes (abfd, info_ptr);
20057 info_ptr += 2;
20058 }
20059 else if (form == DW_FORM_strx3)
20060 {
20061 str_index = read_3_bytes (abfd, info_ptr);
20062 info_ptr += 3;
20063 }
20064 else if (form == DW_FORM_strx4)
20065 {
20066 str_index = read_4_bytes (abfd, info_ptr);
20067 info_ptr += 4;
20068 }
20069 else
20070 {
20071 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20072 info_ptr += bytes_read;
20073 }
20074 attr->set_unsigned_reprocess (str_index);
20075 }
20076 break;
20077 default:
20078 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20079 dwarf_form_name (form),
20080 bfd_get_filename (abfd));
20081 }
20082
20083 /* Super hack. */
20084 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20085 attr->form = DW_FORM_GNU_ref_alt;
20086
20087 /* We have seen instances where the compiler tried to emit a byte
20088 size attribute of -1 which ended up being encoded as an unsigned
20089 0xffffffff. Although 0xffffffff is technically a valid size value,
20090 an object of this size seems pretty unlikely so we can relatively
20091 safely treat these cases as if the size attribute was invalid and
20092 treat them as zero by default. */
20093 if (attr->name == DW_AT_byte_size
20094 && form == DW_FORM_data4
20095 && attr->as_unsigned () >= 0xffffffff)
20096 {
20097 complaint
20098 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20099 hex_string (attr->as_unsigned ()));
20100 attr->set_unsigned (0);
20101 }
20102
20103 return info_ptr;
20104 }
20105
20106 /* Read an attribute described by an abbreviated attribute. */
20107
20108 static const gdb_byte *
20109 read_attribute (const struct die_reader_specs *reader,
20110 struct attribute *attr, const struct attr_abbrev *abbrev,
20111 const gdb_byte *info_ptr)
20112 {
20113 attr->name = abbrev->name;
20114 attr->string_is_canonical = 0;
20115 attr->requires_reprocessing = 0;
20116 return read_attribute_value (reader, attr, abbrev->form,
20117 abbrev->implicit_const, info_ptr);
20118 }
20119
20120 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20121
20122 static const char *
20123 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20124 LONGEST str_offset)
20125 {
20126 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20127 str_offset, "DW_FORM_strp");
20128 }
20129
20130 /* Return pointer to string at .debug_str offset as read from BUF.
20131 BUF is assumed to be in a compilation unit described by CU_HEADER.
20132 Return *BYTES_READ_PTR count of bytes read from BUF. */
20133
20134 static const char *
20135 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20136 const gdb_byte *buf,
20137 const struct comp_unit_head *cu_header,
20138 unsigned int *bytes_read_ptr)
20139 {
20140 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20141
20142 return read_indirect_string_at_offset (per_objfile, str_offset);
20143 }
20144
20145 /* See read.h. */
20146
20147 const char *
20148 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20149 const struct comp_unit_head *cu_header,
20150 unsigned int *bytes_read_ptr)
20151 {
20152 bfd *abfd = objfile->obfd;
20153 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20154
20155 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20156 }
20157
20158 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20159 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20160 ADDR_SIZE is the size of addresses from the CU header. */
20161
20162 static CORE_ADDR
20163 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20164 gdb::optional<ULONGEST> addr_base, int addr_size)
20165 {
20166 struct objfile *objfile = per_objfile->objfile;
20167 bfd *abfd = objfile->obfd;
20168 const gdb_byte *info_ptr;
20169 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20170
20171 per_objfile->per_bfd->addr.read (objfile);
20172 if (per_objfile->per_bfd->addr.buffer == NULL)
20173 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20174 objfile_name (objfile));
20175 if (addr_base_or_zero + addr_index * addr_size
20176 >= per_objfile->per_bfd->addr.size)
20177 error (_("DW_FORM_addr_index pointing outside of "
20178 ".debug_addr section [in module %s]"),
20179 objfile_name (objfile));
20180 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20181 + addr_index * addr_size);
20182 if (addr_size == 4)
20183 return bfd_get_32 (abfd, info_ptr);
20184 else
20185 return bfd_get_64 (abfd, info_ptr);
20186 }
20187
20188 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20189
20190 static CORE_ADDR
20191 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20192 {
20193 return read_addr_index_1 (cu->per_objfile, addr_index,
20194 cu->addr_base, cu->header.addr_size);
20195 }
20196
20197 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20198
20199 static CORE_ADDR
20200 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20201 unsigned int *bytes_read)
20202 {
20203 bfd *abfd = cu->per_objfile->objfile->obfd;
20204 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20205
20206 return read_addr_index (cu, addr_index);
20207 }
20208
20209 /* See read.h. */
20210
20211 CORE_ADDR
20212 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20213 dwarf2_per_objfile *per_objfile,
20214 unsigned int addr_index)
20215 {
20216 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20217 gdb::optional<ULONGEST> addr_base;
20218 int addr_size;
20219
20220 /* We need addr_base and addr_size.
20221 If we don't have PER_CU->cu, we have to get it.
20222 Nasty, but the alternative is storing the needed info in PER_CU,
20223 which at this point doesn't seem justified: it's not clear how frequently
20224 it would get used and it would increase the size of every PER_CU.
20225 Entry points like dwarf2_per_cu_addr_size do a similar thing
20226 so we're not in uncharted territory here.
20227 Alas we need to be a bit more complicated as addr_base is contained
20228 in the DIE.
20229
20230 We don't need to read the entire CU(/TU).
20231 We just need the header and top level die.
20232
20233 IWBN to use the aging mechanism to let us lazily later discard the CU.
20234 For now we skip this optimization. */
20235
20236 if (cu != NULL)
20237 {
20238 addr_base = cu->addr_base;
20239 addr_size = cu->header.addr_size;
20240 }
20241 else
20242 {
20243 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20244 addr_base = reader.cu->addr_base;
20245 addr_size = reader.cu->header.addr_size;
20246 }
20247
20248 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20249 }
20250
20251 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20252 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20253 DWO file. */
20254
20255 static const char *
20256 read_str_index (struct dwarf2_cu *cu,
20257 struct dwarf2_section_info *str_section,
20258 struct dwarf2_section_info *str_offsets_section,
20259 ULONGEST str_offsets_base, ULONGEST str_index)
20260 {
20261 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20262 struct objfile *objfile = per_objfile->objfile;
20263 const char *objf_name = objfile_name (objfile);
20264 bfd *abfd = objfile->obfd;
20265 const gdb_byte *info_ptr;
20266 ULONGEST str_offset;
20267 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20268
20269 str_section->read (objfile);
20270 str_offsets_section->read (objfile);
20271 if (str_section->buffer == NULL)
20272 error (_("%s used without %s section"
20273 " in CU at offset %s [in module %s]"),
20274 form_name, str_section->get_name (),
20275 sect_offset_str (cu->header.sect_off), objf_name);
20276 if (str_offsets_section->buffer == NULL)
20277 error (_("%s used without %s section"
20278 " in CU at offset %s [in module %s]"),
20279 form_name, str_section->get_name (),
20280 sect_offset_str (cu->header.sect_off), objf_name);
20281 info_ptr = (str_offsets_section->buffer
20282 + str_offsets_base
20283 + str_index * cu->header.offset_size);
20284 if (cu->header.offset_size == 4)
20285 str_offset = bfd_get_32 (abfd, info_ptr);
20286 else
20287 str_offset = bfd_get_64 (abfd, info_ptr);
20288 if (str_offset >= str_section->size)
20289 error (_("Offset from %s pointing outside of"
20290 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20291 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20292 return (const char *) (str_section->buffer + str_offset);
20293 }
20294
20295 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20296
20297 static const char *
20298 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20299 {
20300 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20301 ? reader->cu->header.addr_size : 0;
20302 return read_str_index (reader->cu,
20303 &reader->dwo_file->sections.str,
20304 &reader->dwo_file->sections.str_offsets,
20305 str_offsets_base, str_index);
20306 }
20307
20308 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20309
20310 static const char *
20311 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20312 {
20313 struct objfile *objfile = cu->per_objfile->objfile;
20314 const char *objf_name = objfile_name (objfile);
20315 static const char form_name[] = "DW_FORM_GNU_str_index";
20316 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20317
20318 if (!cu->str_offsets_base.has_value ())
20319 error (_("%s used in Fission stub without %s"
20320 " in CU at offset 0x%lx [in module %s]"),
20321 form_name, str_offsets_attr_name,
20322 (long) cu->header.offset_size, objf_name);
20323
20324 return read_str_index (cu,
20325 &cu->per_objfile->per_bfd->str,
20326 &cu->per_objfile->per_bfd->str_offsets,
20327 *cu->str_offsets_base, str_index);
20328 }
20329
20330 /* Return the length of an LEB128 number in BUF. */
20331
20332 static int
20333 leb128_size (const gdb_byte *buf)
20334 {
20335 const gdb_byte *begin = buf;
20336 gdb_byte byte;
20337
20338 while (1)
20339 {
20340 byte = *buf++;
20341 if ((byte & 128) == 0)
20342 return buf - begin;
20343 }
20344 }
20345
20346 static void
20347 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20348 {
20349 switch (lang)
20350 {
20351 case DW_LANG_C89:
20352 case DW_LANG_C99:
20353 case DW_LANG_C11:
20354 case DW_LANG_C:
20355 case DW_LANG_UPC:
20356 cu->language = language_c;
20357 break;
20358 case DW_LANG_Java:
20359 case DW_LANG_C_plus_plus:
20360 case DW_LANG_C_plus_plus_11:
20361 case DW_LANG_C_plus_plus_14:
20362 cu->language = language_cplus;
20363 break;
20364 case DW_LANG_D:
20365 cu->language = language_d;
20366 break;
20367 case DW_LANG_Fortran77:
20368 case DW_LANG_Fortran90:
20369 case DW_LANG_Fortran95:
20370 case DW_LANG_Fortran03:
20371 case DW_LANG_Fortran08:
20372 cu->language = language_fortran;
20373 break;
20374 case DW_LANG_Go:
20375 cu->language = language_go;
20376 break;
20377 case DW_LANG_Mips_Assembler:
20378 cu->language = language_asm;
20379 break;
20380 case DW_LANG_Ada83:
20381 case DW_LANG_Ada95:
20382 cu->language = language_ada;
20383 break;
20384 case DW_LANG_Modula2:
20385 cu->language = language_m2;
20386 break;
20387 case DW_LANG_Pascal83:
20388 cu->language = language_pascal;
20389 break;
20390 case DW_LANG_ObjC:
20391 cu->language = language_objc;
20392 break;
20393 case DW_LANG_Rust:
20394 case DW_LANG_Rust_old:
20395 cu->language = language_rust;
20396 break;
20397 case DW_LANG_Cobol74:
20398 case DW_LANG_Cobol85:
20399 default:
20400 cu->language = language_minimal;
20401 break;
20402 }
20403 cu->language_defn = language_def (cu->language);
20404 }
20405
20406 /* Return the named attribute or NULL if not there. */
20407
20408 static struct attribute *
20409 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20410 {
20411 for (;;)
20412 {
20413 unsigned int i;
20414 struct attribute *spec = NULL;
20415
20416 for (i = 0; i < die->num_attrs; ++i)
20417 {
20418 if (die->attrs[i].name == name)
20419 return &die->attrs[i];
20420 if (die->attrs[i].name == DW_AT_specification
20421 || die->attrs[i].name == DW_AT_abstract_origin)
20422 spec = &die->attrs[i];
20423 }
20424
20425 if (!spec)
20426 break;
20427
20428 die = follow_die_ref (die, spec, &cu);
20429 }
20430
20431 return NULL;
20432 }
20433
20434 /* Return the string associated with a string-typed attribute, or NULL if it
20435 is either not found or is of an incorrect type. */
20436
20437 static const char *
20438 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20439 {
20440 struct attribute *attr;
20441 const char *str = NULL;
20442
20443 attr = dwarf2_attr (die, name, cu);
20444
20445 if (attr != NULL)
20446 {
20447 str = attr->as_string ();
20448 if (str == nullptr)
20449 complaint (_("string type expected for attribute %s for "
20450 "DIE at %s in module %s"),
20451 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20452 objfile_name (cu->per_objfile->objfile));
20453 }
20454
20455 return str;
20456 }
20457
20458 /* Return the dwo name or NULL if not present. If present, it is in either
20459 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20460 static const char *
20461 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20462 {
20463 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20464 if (dwo_name == nullptr)
20465 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20466 return dwo_name;
20467 }
20468
20469 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20470 and holds a non-zero value. This function should only be used for
20471 DW_FORM_flag or DW_FORM_flag_present attributes. */
20472
20473 static int
20474 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20475 {
20476 struct attribute *attr = dwarf2_attr (die, name, cu);
20477
20478 return attr != nullptr && attr->as_boolean ();
20479 }
20480
20481 static int
20482 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20483 {
20484 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20485 which value is non-zero. However, we have to be careful with
20486 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20487 (via dwarf2_flag_true_p) follows this attribute. So we may
20488 end up accidently finding a declaration attribute that belongs
20489 to a different DIE referenced by the specification attribute,
20490 even though the given DIE does not have a declaration attribute. */
20491 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20492 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20493 }
20494
20495 /* Return the die giving the specification for DIE, if there is
20496 one. *SPEC_CU is the CU containing DIE on input, and the CU
20497 containing the return value on output. If there is no
20498 specification, but there is an abstract origin, that is
20499 returned. */
20500
20501 static struct die_info *
20502 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20503 {
20504 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20505 *spec_cu);
20506
20507 if (spec_attr == NULL)
20508 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20509
20510 if (spec_attr == NULL)
20511 return NULL;
20512 else
20513 return follow_die_ref (die, spec_attr, spec_cu);
20514 }
20515
20516 /* Stub for free_line_header to match void * callback types. */
20517
20518 static void
20519 free_line_header_voidp (void *arg)
20520 {
20521 struct line_header *lh = (struct line_header *) arg;
20522
20523 delete lh;
20524 }
20525
20526 /* A convenience function to find the proper .debug_line section for a CU. */
20527
20528 static struct dwarf2_section_info *
20529 get_debug_line_section (struct dwarf2_cu *cu)
20530 {
20531 struct dwarf2_section_info *section;
20532 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20533
20534 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20535 DWO file. */
20536 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20537 section = &cu->dwo_unit->dwo_file->sections.line;
20538 else if (cu->per_cu->is_dwz)
20539 {
20540 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20541
20542 section = &dwz->line;
20543 }
20544 else
20545 section = &per_objfile->per_bfd->line;
20546
20547 return section;
20548 }
20549
20550 /* Read the statement program header starting at OFFSET in
20551 .debug_line, or .debug_line.dwo. Return a pointer
20552 to a struct line_header, allocated using xmalloc.
20553 Returns NULL if there is a problem reading the header, e.g., if it
20554 has a version we don't understand.
20555
20556 NOTE: the strings in the include directory and file name tables of
20557 the returned object point into the dwarf line section buffer,
20558 and must not be freed. */
20559
20560 static line_header_up
20561 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20562 {
20563 struct dwarf2_section_info *section;
20564 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20565
20566 section = get_debug_line_section (cu);
20567 section->read (per_objfile->objfile);
20568 if (section->buffer == NULL)
20569 {
20570 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20571 complaint (_("missing .debug_line.dwo section"));
20572 else
20573 complaint (_("missing .debug_line section"));
20574 return 0;
20575 }
20576
20577 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20578 per_objfile, section, &cu->header);
20579 }
20580
20581 /* Subroutine of dwarf_decode_lines to simplify it.
20582 Return the file name of the psymtab for the given file_entry.
20583 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20584 If space for the result is malloc'd, *NAME_HOLDER will be set.
20585 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20586
20587 static const char *
20588 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20589 const dwarf2_psymtab *pst,
20590 const char *comp_dir,
20591 gdb::unique_xmalloc_ptr<char> *name_holder)
20592 {
20593 const char *include_name = fe.name;
20594 const char *include_name_to_compare = include_name;
20595 const char *pst_filename;
20596 int file_is_pst;
20597
20598 const char *dir_name = fe.include_dir (lh);
20599
20600 gdb::unique_xmalloc_ptr<char> hold_compare;
20601 if (!IS_ABSOLUTE_PATH (include_name)
20602 && (dir_name != NULL || comp_dir != NULL))
20603 {
20604 /* Avoid creating a duplicate psymtab for PST.
20605 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20606 Before we do the comparison, however, we need to account
20607 for DIR_NAME and COMP_DIR.
20608 First prepend dir_name (if non-NULL). If we still don't
20609 have an absolute path prepend comp_dir (if non-NULL).
20610 However, the directory we record in the include-file's
20611 psymtab does not contain COMP_DIR (to match the
20612 corresponding symtab(s)).
20613
20614 Example:
20615
20616 bash$ cd /tmp
20617 bash$ gcc -g ./hello.c
20618 include_name = "hello.c"
20619 dir_name = "."
20620 DW_AT_comp_dir = comp_dir = "/tmp"
20621 DW_AT_name = "./hello.c"
20622
20623 */
20624
20625 if (dir_name != NULL)
20626 {
20627 name_holder->reset (concat (dir_name, SLASH_STRING,
20628 include_name, (char *) NULL));
20629 include_name = name_holder->get ();
20630 include_name_to_compare = include_name;
20631 }
20632 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20633 {
20634 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20635 include_name, (char *) NULL));
20636 include_name_to_compare = hold_compare.get ();
20637 }
20638 }
20639
20640 pst_filename = pst->filename;
20641 gdb::unique_xmalloc_ptr<char> copied_name;
20642 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20643 {
20644 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20645 pst_filename, (char *) NULL));
20646 pst_filename = copied_name.get ();
20647 }
20648
20649 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20650
20651 if (file_is_pst)
20652 return NULL;
20653 return include_name;
20654 }
20655
20656 /* State machine to track the state of the line number program. */
20657
20658 class lnp_state_machine
20659 {
20660 public:
20661 /* Initialize a machine state for the start of a line number
20662 program. */
20663 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20664 bool record_lines_p);
20665
20666 file_entry *current_file ()
20667 {
20668 /* lh->file_names is 0-based, but the file name numbers in the
20669 statement program are 1-based. */
20670 return m_line_header->file_name_at (m_file);
20671 }
20672
20673 /* Record the line in the state machine. END_SEQUENCE is true if
20674 we're processing the end of a sequence. */
20675 void record_line (bool end_sequence);
20676
20677 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20678 nop-out rest of the lines in this sequence. */
20679 void check_line_address (struct dwarf2_cu *cu,
20680 const gdb_byte *line_ptr,
20681 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20682
20683 void handle_set_discriminator (unsigned int discriminator)
20684 {
20685 m_discriminator = discriminator;
20686 m_line_has_non_zero_discriminator |= discriminator != 0;
20687 }
20688
20689 /* Handle DW_LNE_set_address. */
20690 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20691 {
20692 m_op_index = 0;
20693 address += baseaddr;
20694 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20695 }
20696
20697 /* Handle DW_LNS_advance_pc. */
20698 void handle_advance_pc (CORE_ADDR adjust);
20699
20700 /* Handle a special opcode. */
20701 void handle_special_opcode (unsigned char op_code);
20702
20703 /* Handle DW_LNS_advance_line. */
20704 void handle_advance_line (int line_delta)
20705 {
20706 advance_line (line_delta);
20707 }
20708
20709 /* Handle DW_LNS_set_file. */
20710 void handle_set_file (file_name_index file);
20711
20712 /* Handle DW_LNS_negate_stmt. */
20713 void handle_negate_stmt ()
20714 {
20715 m_is_stmt = !m_is_stmt;
20716 }
20717
20718 /* Handle DW_LNS_const_add_pc. */
20719 void handle_const_add_pc ();
20720
20721 /* Handle DW_LNS_fixed_advance_pc. */
20722 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20723 {
20724 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20725 m_op_index = 0;
20726 }
20727
20728 /* Handle DW_LNS_copy. */
20729 void handle_copy ()
20730 {
20731 record_line (false);
20732 m_discriminator = 0;
20733 }
20734
20735 /* Handle DW_LNE_end_sequence. */
20736 void handle_end_sequence ()
20737 {
20738 m_currently_recording_lines = true;
20739 }
20740
20741 private:
20742 /* Advance the line by LINE_DELTA. */
20743 void advance_line (int line_delta)
20744 {
20745 m_line += line_delta;
20746
20747 if (line_delta != 0)
20748 m_line_has_non_zero_discriminator = m_discriminator != 0;
20749 }
20750
20751 struct dwarf2_cu *m_cu;
20752
20753 gdbarch *m_gdbarch;
20754
20755 /* True if we're recording lines.
20756 Otherwise we're building partial symtabs and are just interested in
20757 finding include files mentioned by the line number program. */
20758 bool m_record_lines_p;
20759
20760 /* The line number header. */
20761 line_header *m_line_header;
20762
20763 /* These are part of the standard DWARF line number state machine,
20764 and initialized according to the DWARF spec. */
20765
20766 unsigned char m_op_index = 0;
20767 /* The line table index of the current file. */
20768 file_name_index m_file = 1;
20769 unsigned int m_line = 1;
20770
20771 /* These are initialized in the constructor. */
20772
20773 CORE_ADDR m_address;
20774 bool m_is_stmt;
20775 unsigned int m_discriminator;
20776
20777 /* Additional bits of state we need to track. */
20778
20779 /* The last file that we called dwarf2_start_subfile for.
20780 This is only used for TLLs. */
20781 unsigned int m_last_file = 0;
20782 /* The last file a line number was recorded for. */
20783 struct subfile *m_last_subfile = NULL;
20784
20785 /* The address of the last line entry. */
20786 CORE_ADDR m_last_address;
20787
20788 /* Set to true when a previous line at the same address (using
20789 m_last_address) had m_is_stmt true. This is reset to false when a
20790 line entry at a new address (m_address different to m_last_address) is
20791 processed. */
20792 bool m_stmt_at_address = false;
20793
20794 /* When true, record the lines we decode. */
20795 bool m_currently_recording_lines = false;
20796
20797 /* The last line number that was recorded, used to coalesce
20798 consecutive entries for the same line. This can happen, for
20799 example, when discriminators are present. PR 17276. */
20800 unsigned int m_last_line = 0;
20801 bool m_line_has_non_zero_discriminator = false;
20802 };
20803
20804 void
20805 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20806 {
20807 CORE_ADDR addr_adj = (((m_op_index + adjust)
20808 / m_line_header->maximum_ops_per_instruction)
20809 * m_line_header->minimum_instruction_length);
20810 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20811 m_op_index = ((m_op_index + adjust)
20812 % m_line_header->maximum_ops_per_instruction);
20813 }
20814
20815 void
20816 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20817 {
20818 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20819 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20820 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20821 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20822 / m_line_header->maximum_ops_per_instruction)
20823 * m_line_header->minimum_instruction_length);
20824 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20825 m_op_index = ((m_op_index + adj_opcode_d)
20826 % m_line_header->maximum_ops_per_instruction);
20827
20828 int line_delta = m_line_header->line_base + adj_opcode_r;
20829 advance_line (line_delta);
20830 record_line (false);
20831 m_discriminator = 0;
20832 }
20833
20834 void
20835 lnp_state_machine::handle_set_file (file_name_index file)
20836 {
20837 m_file = file;
20838
20839 const file_entry *fe = current_file ();
20840 if (fe == NULL)
20841 dwarf2_debug_line_missing_file_complaint ();
20842 else if (m_record_lines_p)
20843 {
20844 const char *dir = fe->include_dir (m_line_header);
20845
20846 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20847 m_line_has_non_zero_discriminator = m_discriminator != 0;
20848 dwarf2_start_subfile (m_cu, fe->name, dir);
20849 }
20850 }
20851
20852 void
20853 lnp_state_machine::handle_const_add_pc ()
20854 {
20855 CORE_ADDR adjust
20856 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20857
20858 CORE_ADDR addr_adj
20859 = (((m_op_index + adjust)
20860 / m_line_header->maximum_ops_per_instruction)
20861 * m_line_header->minimum_instruction_length);
20862
20863 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20864 m_op_index = ((m_op_index + adjust)
20865 % m_line_header->maximum_ops_per_instruction);
20866 }
20867
20868 /* Return non-zero if we should add LINE to the line number table.
20869 LINE is the line to add, LAST_LINE is the last line that was added,
20870 LAST_SUBFILE is the subfile for LAST_LINE.
20871 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20872 had a non-zero discriminator.
20873
20874 We have to be careful in the presence of discriminators.
20875 E.g., for this line:
20876
20877 for (i = 0; i < 100000; i++);
20878
20879 clang can emit four line number entries for that one line,
20880 each with a different discriminator.
20881 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20882
20883 However, we want gdb to coalesce all four entries into one.
20884 Otherwise the user could stepi into the middle of the line and
20885 gdb would get confused about whether the pc really was in the
20886 middle of the line.
20887
20888 Things are further complicated by the fact that two consecutive
20889 line number entries for the same line is a heuristic used by gcc
20890 to denote the end of the prologue. So we can't just discard duplicate
20891 entries, we have to be selective about it. The heuristic we use is
20892 that we only collapse consecutive entries for the same line if at least
20893 one of those entries has a non-zero discriminator. PR 17276.
20894
20895 Note: Addresses in the line number state machine can never go backwards
20896 within one sequence, thus this coalescing is ok. */
20897
20898 static int
20899 dwarf_record_line_p (struct dwarf2_cu *cu,
20900 unsigned int line, unsigned int last_line,
20901 int line_has_non_zero_discriminator,
20902 struct subfile *last_subfile)
20903 {
20904 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20905 return 1;
20906 if (line != last_line)
20907 return 1;
20908 /* Same line for the same file that we've seen already.
20909 As a last check, for pr 17276, only record the line if the line
20910 has never had a non-zero discriminator. */
20911 if (!line_has_non_zero_discriminator)
20912 return 1;
20913 return 0;
20914 }
20915
20916 /* Use the CU's builder to record line number LINE beginning at
20917 address ADDRESS in the line table of subfile SUBFILE. */
20918
20919 static void
20920 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20921 unsigned int line, CORE_ADDR address, bool is_stmt,
20922 struct dwarf2_cu *cu)
20923 {
20924 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20925
20926 if (dwarf_line_debug)
20927 {
20928 fprintf_unfiltered (gdb_stdlog,
20929 "Recording line %u, file %s, address %s\n",
20930 line, lbasename (subfile->name),
20931 paddress (gdbarch, address));
20932 }
20933
20934 if (cu != nullptr)
20935 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20936 }
20937
20938 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20939 Mark the end of a set of line number records.
20940 The arguments are the same as for dwarf_record_line_1.
20941 If SUBFILE is NULL the request is ignored. */
20942
20943 static void
20944 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20945 CORE_ADDR address, struct dwarf2_cu *cu)
20946 {
20947 if (subfile == NULL)
20948 return;
20949
20950 if (dwarf_line_debug)
20951 {
20952 fprintf_unfiltered (gdb_stdlog,
20953 "Finishing current line, file %s, address %s\n",
20954 lbasename (subfile->name),
20955 paddress (gdbarch, address));
20956 }
20957
20958 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20959 }
20960
20961 void
20962 lnp_state_machine::record_line (bool end_sequence)
20963 {
20964 if (dwarf_line_debug)
20965 {
20966 fprintf_unfiltered (gdb_stdlog,
20967 "Processing actual line %u: file %u,"
20968 " address %s, is_stmt %u, discrim %u%s\n",
20969 m_line, m_file,
20970 paddress (m_gdbarch, m_address),
20971 m_is_stmt, m_discriminator,
20972 (end_sequence ? "\t(end sequence)" : ""));
20973 }
20974
20975 file_entry *fe = current_file ();
20976
20977 if (fe == NULL)
20978 dwarf2_debug_line_missing_file_complaint ();
20979 /* For now we ignore lines not starting on an instruction boundary.
20980 But not when processing end_sequence for compatibility with the
20981 previous version of the code. */
20982 else if (m_op_index == 0 || end_sequence)
20983 {
20984 fe->included_p = 1;
20985 if (m_record_lines_p)
20986 {
20987 /* When we switch files we insert an end maker in the first file,
20988 switch to the second file and add a new line entry. The
20989 problem is that the end marker inserted in the first file will
20990 discard any previous line entries at the same address. If the
20991 line entries in the first file are marked as is-stmt, while
20992 the new line in the second file is non-stmt, then this means
20993 the end marker will discard is-stmt lines so we can have a
20994 non-stmt line. This means that there are less addresses at
20995 which the user can insert a breakpoint.
20996
20997 To improve this we track the last address in m_last_address,
20998 and whether we have seen an is-stmt at this address. Then
20999 when switching files, if we have seen a stmt at the current
21000 address, and we are switching to create a non-stmt line, then
21001 discard the new line. */
21002 bool file_changed
21003 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21004 bool ignore_this_line
21005 = ((file_changed && !end_sequence && m_last_address == m_address
21006 && !m_is_stmt && m_stmt_at_address)
21007 || (!end_sequence && m_line == 0));
21008
21009 if ((file_changed && !ignore_this_line) || end_sequence)
21010 {
21011 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21012 m_currently_recording_lines ? m_cu : nullptr);
21013 }
21014
21015 if (!end_sequence && !ignore_this_line)
21016 {
21017 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21018
21019 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21020 m_line_has_non_zero_discriminator,
21021 m_last_subfile))
21022 {
21023 buildsym_compunit *builder = m_cu->get_builder ();
21024 dwarf_record_line_1 (m_gdbarch,
21025 builder->get_current_subfile (),
21026 m_line, m_address, is_stmt,
21027 m_currently_recording_lines ? m_cu : nullptr);
21028 }
21029 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21030 m_last_line = m_line;
21031 }
21032 }
21033 }
21034
21035 /* Track whether we have seen any m_is_stmt true at m_address in case we
21036 have multiple line table entries all at m_address. */
21037 if (m_last_address != m_address)
21038 {
21039 m_stmt_at_address = false;
21040 m_last_address = m_address;
21041 }
21042 m_stmt_at_address |= m_is_stmt;
21043 }
21044
21045 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21046 line_header *lh, bool record_lines_p)
21047 {
21048 m_cu = cu;
21049 m_gdbarch = arch;
21050 m_record_lines_p = record_lines_p;
21051 m_line_header = lh;
21052
21053 m_currently_recording_lines = true;
21054
21055 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21056 was a line entry for it so that the backend has a chance to adjust it
21057 and also record it in case it needs it. This is currently used by MIPS
21058 code, cf. `mips_adjust_dwarf2_line'. */
21059 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21060 m_is_stmt = lh->default_is_stmt;
21061 m_discriminator = 0;
21062
21063 m_last_address = m_address;
21064 m_stmt_at_address = false;
21065 }
21066
21067 void
21068 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21069 const gdb_byte *line_ptr,
21070 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21071 {
21072 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21073 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21074 located at 0x0. In this case, additionally check that if
21075 ADDRESS < UNRELOCATED_LOWPC. */
21076
21077 if ((address == 0 && address < unrelocated_lowpc)
21078 || address == (CORE_ADDR) -1)
21079 {
21080 /* This line table is for a function which has been
21081 GCd by the linker. Ignore it. PR gdb/12528 */
21082
21083 struct objfile *objfile = cu->per_objfile->objfile;
21084 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21085
21086 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21087 line_offset, objfile_name (objfile));
21088 m_currently_recording_lines = false;
21089 /* Note: m_currently_recording_lines is left as false until we see
21090 DW_LNE_end_sequence. */
21091 }
21092 }
21093
21094 /* Subroutine of dwarf_decode_lines to simplify it.
21095 Process the line number information in LH.
21096 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21097 program in order to set included_p for every referenced header. */
21098
21099 static void
21100 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21101 const int decode_for_pst_p, CORE_ADDR lowpc)
21102 {
21103 const gdb_byte *line_ptr, *extended_end;
21104 const gdb_byte *line_end;
21105 unsigned int bytes_read, extended_len;
21106 unsigned char op_code, extended_op;
21107 CORE_ADDR baseaddr;
21108 struct objfile *objfile = cu->per_objfile->objfile;
21109 bfd *abfd = objfile->obfd;
21110 struct gdbarch *gdbarch = objfile->arch ();
21111 /* True if we're recording line info (as opposed to building partial
21112 symtabs and just interested in finding include files mentioned by
21113 the line number program). */
21114 bool record_lines_p = !decode_for_pst_p;
21115
21116 baseaddr = objfile->text_section_offset ();
21117
21118 line_ptr = lh->statement_program_start;
21119 line_end = lh->statement_program_end;
21120
21121 /* Read the statement sequences until there's nothing left. */
21122 while (line_ptr < line_end)
21123 {
21124 /* The DWARF line number program state machine. Reset the state
21125 machine at the start of each sequence. */
21126 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21127 bool end_sequence = false;
21128
21129 if (record_lines_p)
21130 {
21131 /* Start a subfile for the current file of the state
21132 machine. */
21133 const file_entry *fe = state_machine.current_file ();
21134
21135 if (fe != NULL)
21136 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21137 }
21138
21139 /* Decode the table. */
21140 while (line_ptr < line_end && !end_sequence)
21141 {
21142 op_code = read_1_byte (abfd, line_ptr);
21143 line_ptr += 1;
21144
21145 if (op_code >= lh->opcode_base)
21146 {
21147 /* Special opcode. */
21148 state_machine.handle_special_opcode (op_code);
21149 }
21150 else switch (op_code)
21151 {
21152 case DW_LNS_extended_op:
21153 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21154 &bytes_read);
21155 line_ptr += bytes_read;
21156 extended_end = line_ptr + extended_len;
21157 extended_op = read_1_byte (abfd, line_ptr);
21158 line_ptr += 1;
21159 if (DW_LNE_lo_user <= extended_op
21160 && extended_op <= DW_LNE_hi_user)
21161 {
21162 /* Vendor extension, ignore. */
21163 line_ptr = extended_end;
21164 break;
21165 }
21166 switch (extended_op)
21167 {
21168 case DW_LNE_end_sequence:
21169 state_machine.handle_end_sequence ();
21170 end_sequence = true;
21171 break;
21172 case DW_LNE_set_address:
21173 {
21174 CORE_ADDR address
21175 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21176 line_ptr += bytes_read;
21177
21178 state_machine.check_line_address (cu, line_ptr,
21179 lowpc - baseaddr, address);
21180 state_machine.handle_set_address (baseaddr, address);
21181 }
21182 break;
21183 case DW_LNE_define_file:
21184 {
21185 const char *cur_file;
21186 unsigned int mod_time, length;
21187 dir_index dindex;
21188
21189 cur_file = read_direct_string (abfd, line_ptr,
21190 &bytes_read);
21191 line_ptr += bytes_read;
21192 dindex = (dir_index)
21193 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21194 line_ptr += bytes_read;
21195 mod_time =
21196 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21197 line_ptr += bytes_read;
21198 length =
21199 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21200 line_ptr += bytes_read;
21201 lh->add_file_name (cur_file, dindex, mod_time, length);
21202 }
21203 break;
21204 case DW_LNE_set_discriminator:
21205 {
21206 /* The discriminator is not interesting to the
21207 debugger; just ignore it. We still need to
21208 check its value though:
21209 if there are consecutive entries for the same
21210 (non-prologue) line we want to coalesce them.
21211 PR 17276. */
21212 unsigned int discr
21213 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21214 line_ptr += bytes_read;
21215
21216 state_machine.handle_set_discriminator (discr);
21217 }
21218 break;
21219 default:
21220 complaint (_("mangled .debug_line section"));
21221 return;
21222 }
21223 /* Make sure that we parsed the extended op correctly. If e.g.
21224 we expected a different address size than the producer used,
21225 we may have read the wrong number of bytes. */
21226 if (line_ptr != extended_end)
21227 {
21228 complaint (_("mangled .debug_line section"));
21229 return;
21230 }
21231 break;
21232 case DW_LNS_copy:
21233 state_machine.handle_copy ();
21234 break;
21235 case DW_LNS_advance_pc:
21236 {
21237 CORE_ADDR adjust
21238 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21239 line_ptr += bytes_read;
21240
21241 state_machine.handle_advance_pc (adjust);
21242 }
21243 break;
21244 case DW_LNS_advance_line:
21245 {
21246 int line_delta
21247 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21248 line_ptr += bytes_read;
21249
21250 state_machine.handle_advance_line (line_delta);
21251 }
21252 break;
21253 case DW_LNS_set_file:
21254 {
21255 file_name_index file
21256 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21257 &bytes_read);
21258 line_ptr += bytes_read;
21259
21260 state_machine.handle_set_file (file);
21261 }
21262 break;
21263 case DW_LNS_set_column:
21264 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21265 line_ptr += bytes_read;
21266 break;
21267 case DW_LNS_negate_stmt:
21268 state_machine.handle_negate_stmt ();
21269 break;
21270 case DW_LNS_set_basic_block:
21271 break;
21272 /* Add to the address register of the state machine the
21273 address increment value corresponding to special opcode
21274 255. I.e., this value is scaled by the minimum
21275 instruction length since special opcode 255 would have
21276 scaled the increment. */
21277 case DW_LNS_const_add_pc:
21278 state_machine.handle_const_add_pc ();
21279 break;
21280 case DW_LNS_fixed_advance_pc:
21281 {
21282 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21283 line_ptr += 2;
21284
21285 state_machine.handle_fixed_advance_pc (addr_adj);
21286 }
21287 break;
21288 default:
21289 {
21290 /* Unknown standard opcode, ignore it. */
21291 int i;
21292
21293 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21294 {
21295 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21296 line_ptr += bytes_read;
21297 }
21298 }
21299 }
21300 }
21301
21302 if (!end_sequence)
21303 dwarf2_debug_line_missing_end_sequence_complaint ();
21304
21305 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21306 in which case we still finish recording the last line). */
21307 state_machine.record_line (true);
21308 }
21309 }
21310
21311 /* Decode the Line Number Program (LNP) for the given line_header
21312 structure and CU. The actual information extracted and the type
21313 of structures created from the LNP depends on the value of PST.
21314
21315 1. If PST is NULL, then this procedure uses the data from the program
21316 to create all necessary symbol tables, and their linetables.
21317
21318 2. If PST is not NULL, this procedure reads the program to determine
21319 the list of files included by the unit represented by PST, and
21320 builds all the associated partial symbol tables.
21321
21322 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21323 It is used for relative paths in the line table.
21324 NOTE: When processing partial symtabs (pst != NULL),
21325 comp_dir == pst->dirname.
21326
21327 NOTE: It is important that psymtabs have the same file name (via strcmp)
21328 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21329 symtab we don't use it in the name of the psymtabs we create.
21330 E.g. expand_line_sal requires this when finding psymtabs to expand.
21331 A good testcase for this is mb-inline.exp.
21332
21333 LOWPC is the lowest address in CU (or 0 if not known).
21334
21335 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21336 for its PC<->lines mapping information. Otherwise only the filename
21337 table is read in. */
21338
21339 static void
21340 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21341 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21342 CORE_ADDR lowpc, int decode_mapping)
21343 {
21344 struct objfile *objfile = cu->per_objfile->objfile;
21345 const int decode_for_pst_p = (pst != NULL);
21346
21347 if (decode_mapping)
21348 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21349
21350 if (decode_for_pst_p)
21351 {
21352 /* Now that we're done scanning the Line Header Program, we can
21353 create the psymtab of each included file. */
21354 for (auto &file_entry : lh->file_names ())
21355 if (file_entry.included_p == 1)
21356 {
21357 gdb::unique_xmalloc_ptr<char> name_holder;
21358 const char *include_name =
21359 psymtab_include_file_name (lh, file_entry, pst,
21360 comp_dir, &name_holder);
21361 if (include_name != NULL)
21362 dwarf2_create_include_psymtab
21363 (cu->per_objfile->per_bfd, include_name, pst,
21364 cu->per_objfile->per_bfd->partial_symtabs.get (),
21365 objfile->per_bfd);
21366 }
21367 }
21368 else
21369 {
21370 /* Make sure a symtab is created for every file, even files
21371 which contain only variables (i.e. no code with associated
21372 line numbers). */
21373 buildsym_compunit *builder = cu->get_builder ();
21374 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21375
21376 for (auto &fe : lh->file_names ())
21377 {
21378 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21379 if (builder->get_current_subfile ()->symtab == NULL)
21380 {
21381 builder->get_current_subfile ()->symtab
21382 = allocate_symtab (cust,
21383 builder->get_current_subfile ()->name);
21384 }
21385 fe.symtab = builder->get_current_subfile ()->symtab;
21386 }
21387 }
21388 }
21389
21390 /* Start a subfile for DWARF. FILENAME is the name of the file and
21391 DIRNAME the name of the source directory which contains FILENAME
21392 or NULL if not known.
21393 This routine tries to keep line numbers from identical absolute and
21394 relative file names in a common subfile.
21395
21396 Using the `list' example from the GDB testsuite, which resides in
21397 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21398 of /srcdir/list0.c yields the following debugging information for list0.c:
21399
21400 DW_AT_name: /srcdir/list0.c
21401 DW_AT_comp_dir: /compdir
21402 files.files[0].name: list0.h
21403 files.files[0].dir: /srcdir
21404 files.files[1].name: list0.c
21405 files.files[1].dir: /srcdir
21406
21407 The line number information for list0.c has to end up in a single
21408 subfile, so that `break /srcdir/list0.c:1' works as expected.
21409 start_subfile will ensure that this happens provided that we pass the
21410 concatenation of files.files[1].dir and files.files[1].name as the
21411 subfile's name. */
21412
21413 static void
21414 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21415 const char *dirname)
21416 {
21417 gdb::unique_xmalloc_ptr<char> copy;
21418
21419 /* In order not to lose the line information directory,
21420 we concatenate it to the filename when it makes sense.
21421 Note that the Dwarf3 standard says (speaking of filenames in line
21422 information): ``The directory index is ignored for file names
21423 that represent full path names''. Thus ignoring dirname in the
21424 `else' branch below isn't an issue. */
21425
21426 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21427 {
21428 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21429 filename = copy.get ();
21430 }
21431
21432 cu->get_builder ()->start_subfile (filename);
21433 }
21434
21435 static void
21436 var_decode_location (struct attribute *attr, struct symbol *sym,
21437 struct dwarf2_cu *cu)
21438 {
21439 struct objfile *objfile = cu->per_objfile->objfile;
21440 struct comp_unit_head *cu_header = &cu->header;
21441
21442 /* NOTE drow/2003-01-30: There used to be a comment and some special
21443 code here to turn a symbol with DW_AT_external and a
21444 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21445 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21446 with some versions of binutils) where shared libraries could have
21447 relocations against symbols in their debug information - the
21448 minimal symbol would have the right address, but the debug info
21449 would not. It's no longer necessary, because we will explicitly
21450 apply relocations when we read in the debug information now. */
21451
21452 /* A DW_AT_location attribute with no contents indicates that a
21453 variable has been optimized away. */
21454 if (attr->form_is_block () && attr->as_block ()->size == 0)
21455 {
21456 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21457 return;
21458 }
21459
21460 /* Handle one degenerate form of location expression specially, to
21461 preserve GDB's previous behavior when section offsets are
21462 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21463 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21464
21465 if (attr->form_is_block ())
21466 {
21467 struct dwarf_block *block = attr->as_block ();
21468
21469 if ((block->data[0] == DW_OP_addr
21470 && block->size == 1 + cu_header->addr_size)
21471 || ((block->data[0] == DW_OP_GNU_addr_index
21472 || block->data[0] == DW_OP_addrx)
21473 && (block->size
21474 == 1 + leb128_size (&block->data[1]))))
21475 {
21476 unsigned int dummy;
21477
21478 if (block->data[0] == DW_OP_addr)
21479 SET_SYMBOL_VALUE_ADDRESS
21480 (sym, cu->header.read_address (objfile->obfd,
21481 block->data + 1,
21482 &dummy));
21483 else
21484 SET_SYMBOL_VALUE_ADDRESS
21485 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21486 &dummy));
21487 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21488 fixup_symbol_section (sym, objfile);
21489 SET_SYMBOL_VALUE_ADDRESS
21490 (sym,
21491 SYMBOL_VALUE_ADDRESS (sym)
21492 + objfile->section_offsets[sym->section_index ()]);
21493 return;
21494 }
21495 }
21496
21497 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21498 expression evaluator, and use LOC_COMPUTED only when necessary
21499 (i.e. when the value of a register or memory location is
21500 referenced, or a thread-local block, etc.). Then again, it might
21501 not be worthwhile. I'm assuming that it isn't unless performance
21502 or memory numbers show me otherwise. */
21503
21504 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21505
21506 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21507 cu->has_loclist = true;
21508 }
21509
21510 /* Given a pointer to a DWARF information entry, figure out if we need
21511 to make a symbol table entry for it, and if so, create a new entry
21512 and return a pointer to it.
21513 If TYPE is NULL, determine symbol type from the die, otherwise
21514 used the passed type.
21515 If SPACE is not NULL, use it to hold the new symbol. If it is
21516 NULL, allocate a new symbol on the objfile's obstack. */
21517
21518 static struct symbol *
21519 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21520 struct symbol *space)
21521 {
21522 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21523 struct objfile *objfile = per_objfile->objfile;
21524 struct gdbarch *gdbarch = objfile->arch ();
21525 struct symbol *sym = NULL;
21526 const char *name;
21527 struct attribute *attr = NULL;
21528 struct attribute *attr2 = NULL;
21529 CORE_ADDR baseaddr;
21530 struct pending **list_to_add = NULL;
21531
21532 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21533
21534 baseaddr = objfile->text_section_offset ();
21535
21536 name = dwarf2_name (die, cu);
21537 if (name)
21538 {
21539 int suppress_add = 0;
21540
21541 if (space)
21542 sym = space;
21543 else
21544 sym = new (&objfile->objfile_obstack) symbol;
21545 OBJSTAT (objfile, n_syms++);
21546
21547 /* Cache this symbol's name and the name's demangled form (if any). */
21548 sym->set_language (cu->language, &objfile->objfile_obstack);
21549 /* Fortran does not have mangling standard and the mangling does differ
21550 between gfortran, iFort etc. */
21551 const char *physname
21552 = (cu->language == language_fortran
21553 ? dwarf2_full_name (name, die, cu)
21554 : dwarf2_physname (name, die, cu));
21555 const char *linkagename = dw2_linkage_name (die, cu);
21556
21557 if (linkagename == nullptr || cu->language == language_ada)
21558 sym->set_linkage_name (physname);
21559 else
21560 {
21561 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21562 sym->set_linkage_name (linkagename);
21563 }
21564
21565 /* Default assumptions.
21566 Use the passed type or decode it from the die. */
21567 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21568 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21569 if (type != NULL)
21570 SYMBOL_TYPE (sym) = type;
21571 else
21572 SYMBOL_TYPE (sym) = die_type (die, cu);
21573 attr = dwarf2_attr (die,
21574 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21575 cu);
21576 if (attr != nullptr)
21577 SYMBOL_LINE (sym) = attr->constant_value (0);
21578
21579 attr = dwarf2_attr (die,
21580 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21581 cu);
21582 if (attr != nullptr && attr->is_nonnegative ())
21583 {
21584 file_name_index file_index
21585 = (file_name_index) attr->as_nonnegative ();
21586 struct file_entry *fe;
21587
21588 if (cu->line_header != NULL)
21589 fe = cu->line_header->file_name_at (file_index);
21590 else
21591 fe = NULL;
21592
21593 if (fe == NULL)
21594 complaint (_("file index out of range"));
21595 else
21596 symbol_set_symtab (sym, fe->symtab);
21597 }
21598
21599 switch (die->tag)
21600 {
21601 case DW_TAG_label:
21602 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21603 if (attr != nullptr)
21604 {
21605 CORE_ADDR addr;
21606
21607 addr = attr->as_address ();
21608 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21609 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21610 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21611 }
21612 else
21613 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21614 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21615 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21616 add_symbol_to_list (sym, cu->list_in_scope);
21617 break;
21618 case DW_TAG_subprogram:
21619 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21620 finish_block. */
21621 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21622 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21623 if ((attr2 != nullptr && attr2->as_boolean ())
21624 || cu->language == language_ada
21625 || cu->language == language_fortran)
21626 {
21627 /* Subprograms marked external are stored as a global symbol.
21628 Ada and Fortran subprograms, whether marked external or
21629 not, are always stored as a global symbol, because we want
21630 to be able to access them globally. For instance, we want
21631 to be able to break on a nested subprogram without having
21632 to specify the context. */
21633 list_to_add = cu->get_builder ()->get_global_symbols ();
21634 }
21635 else
21636 {
21637 list_to_add = cu->list_in_scope;
21638 }
21639 break;
21640 case DW_TAG_inlined_subroutine:
21641 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21642 finish_block. */
21643 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21644 SYMBOL_INLINED (sym) = 1;
21645 list_to_add = cu->list_in_scope;
21646 break;
21647 case DW_TAG_template_value_param:
21648 suppress_add = 1;
21649 /* Fall through. */
21650 case DW_TAG_constant:
21651 case DW_TAG_variable:
21652 case DW_TAG_member:
21653 /* Compilation with minimal debug info may result in
21654 variables with missing type entries. Change the
21655 misleading `void' type to something sensible. */
21656 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21657 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21658
21659 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21660 /* In the case of DW_TAG_member, we should only be called for
21661 static const members. */
21662 if (die->tag == DW_TAG_member)
21663 {
21664 /* dwarf2_add_field uses die_is_declaration,
21665 so we do the same. */
21666 gdb_assert (die_is_declaration (die, cu));
21667 gdb_assert (attr);
21668 }
21669 if (attr != nullptr)
21670 {
21671 dwarf2_const_value (attr, sym, cu);
21672 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21673 if (!suppress_add)
21674 {
21675 if (attr2 != nullptr && attr2->as_boolean ())
21676 list_to_add = cu->get_builder ()->get_global_symbols ();
21677 else
21678 list_to_add = cu->list_in_scope;
21679 }
21680 break;
21681 }
21682 attr = dwarf2_attr (die, DW_AT_location, cu);
21683 if (attr != nullptr)
21684 {
21685 var_decode_location (attr, sym, cu);
21686 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21687
21688 /* Fortran explicitly imports any global symbols to the local
21689 scope by DW_TAG_common_block. */
21690 if (cu->language == language_fortran && die->parent
21691 && die->parent->tag == DW_TAG_common_block)
21692 attr2 = NULL;
21693
21694 if (SYMBOL_CLASS (sym) == LOC_STATIC
21695 && SYMBOL_VALUE_ADDRESS (sym) == 0
21696 && !per_objfile->per_bfd->has_section_at_zero)
21697 {
21698 /* When a static variable is eliminated by the linker,
21699 the corresponding debug information is not stripped
21700 out, but the variable address is set to null;
21701 do not add such variables into symbol table. */
21702 }
21703 else if (attr2 != nullptr && attr2->as_boolean ())
21704 {
21705 if (SYMBOL_CLASS (sym) == LOC_STATIC
21706 && (objfile->flags & OBJF_MAINLINE) == 0
21707 && per_objfile->per_bfd->can_copy)
21708 {
21709 /* A global static variable might be subject to
21710 copy relocation. We first check for a local
21711 minsym, though, because maybe the symbol was
21712 marked hidden, in which case this would not
21713 apply. */
21714 bound_minimal_symbol found
21715 = (lookup_minimal_symbol_linkage
21716 (sym->linkage_name (), objfile));
21717 if (found.minsym != nullptr)
21718 sym->maybe_copied = 1;
21719 }
21720
21721 /* A variable with DW_AT_external is never static,
21722 but it may be block-scoped. */
21723 list_to_add
21724 = ((cu->list_in_scope
21725 == cu->get_builder ()->get_file_symbols ())
21726 ? cu->get_builder ()->get_global_symbols ()
21727 : cu->list_in_scope);
21728 }
21729 else
21730 list_to_add = cu->list_in_scope;
21731 }
21732 else
21733 {
21734 /* We do not know the address of this symbol.
21735 If it is an external symbol and we have type information
21736 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21737 The address of the variable will then be determined from
21738 the minimal symbol table whenever the variable is
21739 referenced. */
21740 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21741
21742 /* Fortran explicitly imports any global symbols to the local
21743 scope by DW_TAG_common_block. */
21744 if (cu->language == language_fortran && die->parent
21745 && die->parent->tag == DW_TAG_common_block)
21746 {
21747 /* SYMBOL_CLASS doesn't matter here because
21748 read_common_block is going to reset it. */
21749 if (!suppress_add)
21750 list_to_add = cu->list_in_scope;
21751 }
21752 else if (attr2 != nullptr && attr2->as_boolean ()
21753 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21754 {
21755 /* A variable with DW_AT_external is never static, but it
21756 may be block-scoped. */
21757 list_to_add
21758 = ((cu->list_in_scope
21759 == cu->get_builder ()->get_file_symbols ())
21760 ? cu->get_builder ()->get_global_symbols ()
21761 : cu->list_in_scope);
21762
21763 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21764 }
21765 else if (!die_is_declaration (die, cu))
21766 {
21767 /* Use the default LOC_OPTIMIZED_OUT class. */
21768 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21769 if (!suppress_add)
21770 list_to_add = cu->list_in_scope;
21771 }
21772 }
21773 break;
21774 case DW_TAG_formal_parameter:
21775 {
21776 /* If we are inside a function, mark this as an argument. If
21777 not, we might be looking at an argument to an inlined function
21778 when we do not have enough information to show inlined frames;
21779 pretend it's a local variable in that case so that the user can
21780 still see it. */
21781 struct context_stack *curr
21782 = cu->get_builder ()->get_current_context_stack ();
21783 if (curr != nullptr && curr->name != nullptr)
21784 SYMBOL_IS_ARGUMENT (sym) = 1;
21785 attr = dwarf2_attr (die, DW_AT_location, cu);
21786 if (attr != nullptr)
21787 {
21788 var_decode_location (attr, sym, cu);
21789 }
21790 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21791 if (attr != nullptr)
21792 {
21793 dwarf2_const_value (attr, sym, cu);
21794 }
21795
21796 list_to_add = cu->list_in_scope;
21797 }
21798 break;
21799 case DW_TAG_unspecified_parameters:
21800 /* From varargs functions; gdb doesn't seem to have any
21801 interest in this information, so just ignore it for now.
21802 (FIXME?) */
21803 break;
21804 case DW_TAG_template_type_param:
21805 suppress_add = 1;
21806 /* Fall through. */
21807 case DW_TAG_class_type:
21808 case DW_TAG_interface_type:
21809 case DW_TAG_structure_type:
21810 case DW_TAG_union_type:
21811 case DW_TAG_set_type:
21812 case DW_TAG_enumeration_type:
21813 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21814 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21815
21816 {
21817 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21818 really ever be static objects: otherwise, if you try
21819 to, say, break of a class's method and you're in a file
21820 which doesn't mention that class, it won't work unless
21821 the check for all static symbols in lookup_symbol_aux
21822 saves you. See the OtherFileClass tests in
21823 gdb.c++/namespace.exp. */
21824
21825 if (!suppress_add)
21826 {
21827 buildsym_compunit *builder = cu->get_builder ();
21828 list_to_add
21829 = (cu->list_in_scope == builder->get_file_symbols ()
21830 && cu->language == language_cplus
21831 ? builder->get_global_symbols ()
21832 : cu->list_in_scope);
21833
21834 /* The semantics of C++ state that "struct foo {
21835 ... }" also defines a typedef for "foo". */
21836 if (cu->language == language_cplus
21837 || cu->language == language_ada
21838 || cu->language == language_d
21839 || cu->language == language_rust)
21840 {
21841 /* The symbol's name is already allocated along
21842 with this objfile, so we don't need to
21843 duplicate it for the type. */
21844 if (SYMBOL_TYPE (sym)->name () == 0)
21845 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21846 }
21847 }
21848 }
21849 break;
21850 case DW_TAG_typedef:
21851 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21852 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21853 list_to_add = cu->list_in_scope;
21854 break;
21855 case DW_TAG_array_type:
21856 case DW_TAG_base_type:
21857 case DW_TAG_subrange_type:
21858 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21859 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21860 list_to_add = cu->list_in_scope;
21861 break;
21862 case DW_TAG_enumerator:
21863 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21864 if (attr != nullptr)
21865 {
21866 dwarf2_const_value (attr, sym, cu);
21867 }
21868 {
21869 /* NOTE: carlton/2003-11-10: See comment above in the
21870 DW_TAG_class_type, etc. block. */
21871
21872 list_to_add
21873 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21874 && cu->language == language_cplus
21875 ? cu->get_builder ()->get_global_symbols ()
21876 : cu->list_in_scope);
21877 }
21878 break;
21879 case DW_TAG_imported_declaration:
21880 case DW_TAG_namespace:
21881 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21882 list_to_add = cu->get_builder ()->get_global_symbols ();
21883 break;
21884 case DW_TAG_module:
21885 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21886 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21887 list_to_add = cu->get_builder ()->get_global_symbols ();
21888 break;
21889 case DW_TAG_common_block:
21890 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21891 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21892 add_symbol_to_list (sym, cu->list_in_scope);
21893 break;
21894 default:
21895 /* Not a tag we recognize. Hopefully we aren't processing
21896 trash data, but since we must specifically ignore things
21897 we don't recognize, there is nothing else we should do at
21898 this point. */
21899 complaint (_("unsupported tag: '%s'"),
21900 dwarf_tag_name (die->tag));
21901 break;
21902 }
21903
21904 if (suppress_add)
21905 {
21906 sym->hash_next = objfile->template_symbols;
21907 objfile->template_symbols = sym;
21908 list_to_add = NULL;
21909 }
21910
21911 if (list_to_add != NULL)
21912 add_symbol_to_list (sym, list_to_add);
21913
21914 /* For the benefit of old versions of GCC, check for anonymous
21915 namespaces based on the demangled name. */
21916 if (!cu->processing_has_namespace_info
21917 && cu->language == language_cplus)
21918 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21919 }
21920 return (sym);
21921 }
21922
21923 /* Given an attr with a DW_FORM_dataN value in host byte order,
21924 zero-extend it as appropriate for the symbol's type. The DWARF
21925 standard (v4) is not entirely clear about the meaning of using
21926 DW_FORM_dataN for a constant with a signed type, where the type is
21927 wider than the data. The conclusion of a discussion on the DWARF
21928 list was that this is unspecified. We choose to always zero-extend
21929 because that is the interpretation long in use by GCC. */
21930
21931 static gdb_byte *
21932 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21933 struct dwarf2_cu *cu, LONGEST *value, int bits)
21934 {
21935 struct objfile *objfile = cu->per_objfile->objfile;
21936 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21937 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21938 LONGEST l = attr->constant_value (0);
21939
21940 if (bits < sizeof (*value) * 8)
21941 {
21942 l &= ((LONGEST) 1 << bits) - 1;
21943 *value = l;
21944 }
21945 else if (bits == sizeof (*value) * 8)
21946 *value = l;
21947 else
21948 {
21949 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21950 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21951 return bytes;
21952 }
21953
21954 return NULL;
21955 }
21956
21957 /* Read a constant value from an attribute. Either set *VALUE, or if
21958 the value does not fit in *VALUE, set *BYTES - either already
21959 allocated on the objfile obstack, or newly allocated on OBSTACK,
21960 or, set *BATON, if we translated the constant to a location
21961 expression. */
21962
21963 static void
21964 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21965 const char *name, struct obstack *obstack,
21966 struct dwarf2_cu *cu,
21967 LONGEST *value, const gdb_byte **bytes,
21968 struct dwarf2_locexpr_baton **baton)
21969 {
21970 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21971 struct objfile *objfile = per_objfile->objfile;
21972 struct comp_unit_head *cu_header = &cu->header;
21973 struct dwarf_block *blk;
21974 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21975 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21976
21977 *value = 0;
21978 *bytes = NULL;
21979 *baton = NULL;
21980
21981 switch (attr->form)
21982 {
21983 case DW_FORM_addr:
21984 case DW_FORM_addrx:
21985 case DW_FORM_GNU_addr_index:
21986 {
21987 gdb_byte *data;
21988
21989 if (TYPE_LENGTH (type) != cu_header->addr_size)
21990 dwarf2_const_value_length_mismatch_complaint (name,
21991 cu_header->addr_size,
21992 TYPE_LENGTH (type));
21993 /* Symbols of this form are reasonably rare, so we just
21994 piggyback on the existing location code rather than writing
21995 a new implementation of symbol_computed_ops. */
21996 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21997 (*baton)->per_objfile = per_objfile;
21998 (*baton)->per_cu = cu->per_cu;
21999 gdb_assert ((*baton)->per_cu);
22000
22001 (*baton)->size = 2 + cu_header->addr_size;
22002 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22003 (*baton)->data = data;
22004
22005 data[0] = DW_OP_addr;
22006 store_unsigned_integer (&data[1], cu_header->addr_size,
22007 byte_order, attr->as_address ());
22008 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22009 }
22010 break;
22011 case DW_FORM_string:
22012 case DW_FORM_strp:
22013 case DW_FORM_strx:
22014 case DW_FORM_GNU_str_index:
22015 case DW_FORM_GNU_strp_alt:
22016 /* The string is already allocated on the objfile obstack, point
22017 directly to it. */
22018 *bytes = (const gdb_byte *) attr->as_string ();
22019 break;
22020 case DW_FORM_block1:
22021 case DW_FORM_block2:
22022 case DW_FORM_block4:
22023 case DW_FORM_block:
22024 case DW_FORM_exprloc:
22025 case DW_FORM_data16:
22026 blk = attr->as_block ();
22027 if (TYPE_LENGTH (type) != blk->size)
22028 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22029 TYPE_LENGTH (type));
22030 *bytes = blk->data;
22031 break;
22032
22033 /* The DW_AT_const_value attributes are supposed to carry the
22034 symbol's value "represented as it would be on the target
22035 architecture." By the time we get here, it's already been
22036 converted to host endianness, so we just need to sign- or
22037 zero-extend it as appropriate. */
22038 case DW_FORM_data1:
22039 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22040 break;
22041 case DW_FORM_data2:
22042 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22043 break;
22044 case DW_FORM_data4:
22045 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22046 break;
22047 case DW_FORM_data8:
22048 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22049 break;
22050
22051 case DW_FORM_sdata:
22052 case DW_FORM_implicit_const:
22053 *value = attr->as_signed ();
22054 break;
22055
22056 case DW_FORM_udata:
22057 *value = attr->as_unsigned ();
22058 break;
22059
22060 default:
22061 complaint (_("unsupported const value attribute form: '%s'"),
22062 dwarf_form_name (attr->form));
22063 *value = 0;
22064 break;
22065 }
22066 }
22067
22068
22069 /* Copy constant value from an attribute to a symbol. */
22070
22071 static void
22072 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22073 struct dwarf2_cu *cu)
22074 {
22075 struct objfile *objfile = cu->per_objfile->objfile;
22076 LONGEST value;
22077 const gdb_byte *bytes;
22078 struct dwarf2_locexpr_baton *baton;
22079
22080 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22081 sym->print_name (),
22082 &objfile->objfile_obstack, cu,
22083 &value, &bytes, &baton);
22084
22085 if (baton != NULL)
22086 {
22087 SYMBOL_LOCATION_BATON (sym) = baton;
22088 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22089 }
22090 else if (bytes != NULL)
22091 {
22092 SYMBOL_VALUE_BYTES (sym) = bytes;
22093 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22094 }
22095 else
22096 {
22097 SYMBOL_VALUE (sym) = value;
22098 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22099 }
22100 }
22101
22102 /* Return the type of the die in question using its DW_AT_type attribute. */
22103
22104 static struct type *
22105 die_type (struct die_info *die, struct dwarf2_cu *cu)
22106 {
22107 struct attribute *type_attr;
22108
22109 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22110 if (!type_attr)
22111 {
22112 struct objfile *objfile = cu->per_objfile->objfile;
22113 /* A missing DW_AT_type represents a void type. */
22114 return objfile_type (objfile)->builtin_void;
22115 }
22116
22117 return lookup_die_type (die, type_attr, cu);
22118 }
22119
22120 /* True iff CU's producer generates GNAT Ada auxiliary information
22121 that allows to find parallel types through that information instead
22122 of having to do expensive parallel lookups by type name. */
22123
22124 static int
22125 need_gnat_info (struct dwarf2_cu *cu)
22126 {
22127 /* Assume that the Ada compiler was GNAT, which always produces
22128 the auxiliary information. */
22129 return (cu->language == language_ada);
22130 }
22131
22132 /* Return the auxiliary type of the die in question using its
22133 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22134 attribute is not present. */
22135
22136 static struct type *
22137 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22138 {
22139 struct attribute *type_attr;
22140
22141 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22142 if (!type_attr)
22143 return NULL;
22144
22145 return lookup_die_type (die, type_attr, cu);
22146 }
22147
22148 /* If DIE has a descriptive_type attribute, then set the TYPE's
22149 descriptive type accordingly. */
22150
22151 static void
22152 set_descriptive_type (struct type *type, struct die_info *die,
22153 struct dwarf2_cu *cu)
22154 {
22155 struct type *descriptive_type = die_descriptive_type (die, cu);
22156
22157 if (descriptive_type)
22158 {
22159 ALLOCATE_GNAT_AUX_TYPE (type);
22160 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22161 }
22162 }
22163
22164 /* Return the containing type of the die in question using its
22165 DW_AT_containing_type attribute. */
22166
22167 static struct type *
22168 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22169 {
22170 struct attribute *type_attr;
22171 struct objfile *objfile = cu->per_objfile->objfile;
22172
22173 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22174 if (!type_attr)
22175 error (_("Dwarf Error: Problem turning containing type into gdb type "
22176 "[in module %s]"), objfile_name (objfile));
22177
22178 return lookup_die_type (die, type_attr, cu);
22179 }
22180
22181 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22182
22183 static struct type *
22184 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22185 {
22186 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22187 struct objfile *objfile = per_objfile->objfile;
22188 char *saved;
22189
22190 std::string message
22191 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22192 objfile_name (objfile),
22193 sect_offset_str (cu->header.sect_off),
22194 sect_offset_str (die->sect_off));
22195 saved = obstack_strdup (&objfile->objfile_obstack, message);
22196
22197 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22198 }
22199
22200 /* Look up the type of DIE in CU using its type attribute ATTR.
22201 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22202 DW_AT_containing_type.
22203 If there is no type substitute an error marker. */
22204
22205 static struct type *
22206 lookup_die_type (struct die_info *die, const struct attribute *attr,
22207 struct dwarf2_cu *cu)
22208 {
22209 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22210 struct objfile *objfile = per_objfile->objfile;
22211 struct type *this_type;
22212
22213 gdb_assert (attr->name == DW_AT_type
22214 || attr->name == DW_AT_GNAT_descriptive_type
22215 || attr->name == DW_AT_containing_type);
22216
22217 /* First see if we have it cached. */
22218
22219 if (attr->form == DW_FORM_GNU_ref_alt)
22220 {
22221 struct dwarf2_per_cu_data *per_cu;
22222 sect_offset sect_off = attr->get_ref_die_offset ();
22223
22224 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22225 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22226 }
22227 else if (attr->form_is_ref ())
22228 {
22229 sect_offset sect_off = attr->get_ref_die_offset ();
22230
22231 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22232 }
22233 else if (attr->form == DW_FORM_ref_sig8)
22234 {
22235 ULONGEST signature = attr->as_signature ();
22236
22237 return get_signatured_type (die, signature, cu);
22238 }
22239 else
22240 {
22241 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22242 " at %s [in module %s]"),
22243 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22244 objfile_name (objfile));
22245 return build_error_marker_type (cu, die);
22246 }
22247
22248 /* If not cached we need to read it in. */
22249
22250 if (this_type == NULL)
22251 {
22252 struct die_info *type_die = NULL;
22253 struct dwarf2_cu *type_cu = cu;
22254
22255 if (attr->form_is_ref ())
22256 type_die = follow_die_ref (die, attr, &type_cu);
22257 if (type_die == NULL)
22258 return build_error_marker_type (cu, die);
22259 /* If we find the type now, it's probably because the type came
22260 from an inter-CU reference and the type's CU got expanded before
22261 ours. */
22262 this_type = read_type_die (type_die, type_cu);
22263 }
22264
22265 /* If we still don't have a type use an error marker. */
22266
22267 if (this_type == NULL)
22268 return build_error_marker_type (cu, die);
22269
22270 return this_type;
22271 }
22272
22273 /* Return the type in DIE, CU.
22274 Returns NULL for invalid types.
22275
22276 This first does a lookup in die_type_hash,
22277 and only reads the die in if necessary.
22278
22279 NOTE: This can be called when reading in partial or full symbols. */
22280
22281 static struct type *
22282 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22283 {
22284 struct type *this_type;
22285
22286 this_type = get_die_type (die, cu);
22287 if (this_type)
22288 return this_type;
22289
22290 return read_type_die_1 (die, cu);
22291 }
22292
22293 /* Read the type in DIE, CU.
22294 Returns NULL for invalid types. */
22295
22296 static struct type *
22297 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22298 {
22299 struct type *this_type = NULL;
22300
22301 switch (die->tag)
22302 {
22303 case DW_TAG_class_type:
22304 case DW_TAG_interface_type:
22305 case DW_TAG_structure_type:
22306 case DW_TAG_union_type:
22307 this_type = read_structure_type (die, cu);
22308 break;
22309 case DW_TAG_enumeration_type:
22310 this_type = read_enumeration_type (die, cu);
22311 break;
22312 case DW_TAG_subprogram:
22313 case DW_TAG_subroutine_type:
22314 case DW_TAG_inlined_subroutine:
22315 this_type = read_subroutine_type (die, cu);
22316 break;
22317 case DW_TAG_array_type:
22318 this_type = read_array_type (die, cu);
22319 break;
22320 case DW_TAG_set_type:
22321 this_type = read_set_type (die, cu);
22322 break;
22323 case DW_TAG_pointer_type:
22324 this_type = read_tag_pointer_type (die, cu);
22325 break;
22326 case DW_TAG_ptr_to_member_type:
22327 this_type = read_tag_ptr_to_member_type (die, cu);
22328 break;
22329 case DW_TAG_reference_type:
22330 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22331 break;
22332 case DW_TAG_rvalue_reference_type:
22333 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22334 break;
22335 case DW_TAG_const_type:
22336 this_type = read_tag_const_type (die, cu);
22337 break;
22338 case DW_TAG_volatile_type:
22339 this_type = read_tag_volatile_type (die, cu);
22340 break;
22341 case DW_TAG_restrict_type:
22342 this_type = read_tag_restrict_type (die, cu);
22343 break;
22344 case DW_TAG_string_type:
22345 this_type = read_tag_string_type (die, cu);
22346 break;
22347 case DW_TAG_typedef:
22348 this_type = read_typedef (die, cu);
22349 break;
22350 case DW_TAG_subrange_type:
22351 this_type = read_subrange_type (die, cu);
22352 break;
22353 case DW_TAG_base_type:
22354 this_type = read_base_type (die, cu);
22355 break;
22356 case DW_TAG_unspecified_type:
22357 this_type = read_unspecified_type (die, cu);
22358 break;
22359 case DW_TAG_namespace:
22360 this_type = read_namespace_type (die, cu);
22361 break;
22362 case DW_TAG_module:
22363 this_type = read_module_type (die, cu);
22364 break;
22365 case DW_TAG_atomic_type:
22366 this_type = read_tag_atomic_type (die, cu);
22367 break;
22368 default:
22369 complaint (_("unexpected tag in read_type_die: '%s'"),
22370 dwarf_tag_name (die->tag));
22371 break;
22372 }
22373
22374 return this_type;
22375 }
22376
22377 /* See if we can figure out if the class lives in a namespace. We do
22378 this by looking for a member function; its demangled name will
22379 contain namespace info, if there is any.
22380 Return the computed name or NULL.
22381 Space for the result is allocated on the objfile's obstack.
22382 This is the full-die version of guess_partial_die_structure_name.
22383 In this case we know DIE has no useful parent. */
22384
22385 static const char *
22386 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22387 {
22388 struct die_info *spec_die;
22389 struct dwarf2_cu *spec_cu;
22390 struct die_info *child;
22391 struct objfile *objfile = cu->per_objfile->objfile;
22392
22393 spec_cu = cu;
22394 spec_die = die_specification (die, &spec_cu);
22395 if (spec_die != NULL)
22396 {
22397 die = spec_die;
22398 cu = spec_cu;
22399 }
22400
22401 for (child = die->child;
22402 child != NULL;
22403 child = child->sibling)
22404 {
22405 if (child->tag == DW_TAG_subprogram)
22406 {
22407 const char *linkage_name = dw2_linkage_name (child, cu);
22408
22409 if (linkage_name != NULL)
22410 {
22411 gdb::unique_xmalloc_ptr<char> actual_name
22412 (cu->language_defn->class_name_from_physname (linkage_name));
22413 const char *name = NULL;
22414
22415 if (actual_name != NULL)
22416 {
22417 const char *die_name = dwarf2_name (die, cu);
22418
22419 if (die_name != NULL
22420 && strcmp (die_name, actual_name.get ()) != 0)
22421 {
22422 /* Strip off the class name from the full name.
22423 We want the prefix. */
22424 int die_name_len = strlen (die_name);
22425 int actual_name_len = strlen (actual_name.get ());
22426 const char *ptr = actual_name.get ();
22427
22428 /* Test for '::' as a sanity check. */
22429 if (actual_name_len > die_name_len + 2
22430 && ptr[actual_name_len - die_name_len - 1] == ':')
22431 name = obstack_strndup (
22432 &objfile->per_bfd->storage_obstack,
22433 ptr, actual_name_len - die_name_len - 2);
22434 }
22435 }
22436 return name;
22437 }
22438 }
22439 }
22440
22441 return NULL;
22442 }
22443
22444 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22445 prefix part in such case. See
22446 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22447
22448 static const char *
22449 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22450 {
22451 struct attribute *attr;
22452 const char *base;
22453
22454 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22455 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22456 return NULL;
22457
22458 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22459 return NULL;
22460
22461 attr = dw2_linkage_name_attr (die, cu);
22462 const char *attr_name = attr->as_string ();
22463 if (attr == NULL || attr_name == NULL)
22464 return NULL;
22465
22466 /* dwarf2_name had to be already called. */
22467 gdb_assert (attr->canonical_string_p ());
22468
22469 /* Strip the base name, keep any leading namespaces/classes. */
22470 base = strrchr (attr_name, ':');
22471 if (base == NULL || base == attr_name || base[-1] != ':')
22472 return "";
22473
22474 struct objfile *objfile = cu->per_objfile->objfile;
22475 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22476 attr_name,
22477 &base[-1] - attr_name);
22478 }
22479
22480 /* Return the name of the namespace/class that DIE is defined within,
22481 or "" if we can't tell. The caller should not xfree the result.
22482
22483 For example, if we're within the method foo() in the following
22484 code:
22485
22486 namespace N {
22487 class C {
22488 void foo () {
22489 }
22490 };
22491 }
22492
22493 then determine_prefix on foo's die will return "N::C". */
22494
22495 static const char *
22496 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22497 {
22498 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22499 struct die_info *parent, *spec_die;
22500 struct dwarf2_cu *spec_cu;
22501 struct type *parent_type;
22502 const char *retval;
22503
22504 if (cu->language != language_cplus
22505 && cu->language != language_fortran && cu->language != language_d
22506 && cu->language != language_rust)
22507 return "";
22508
22509 retval = anonymous_struct_prefix (die, cu);
22510 if (retval)
22511 return retval;
22512
22513 /* We have to be careful in the presence of DW_AT_specification.
22514 For example, with GCC 3.4, given the code
22515
22516 namespace N {
22517 void foo() {
22518 // Definition of N::foo.
22519 }
22520 }
22521
22522 then we'll have a tree of DIEs like this:
22523
22524 1: DW_TAG_compile_unit
22525 2: DW_TAG_namespace // N
22526 3: DW_TAG_subprogram // declaration of N::foo
22527 4: DW_TAG_subprogram // definition of N::foo
22528 DW_AT_specification // refers to die #3
22529
22530 Thus, when processing die #4, we have to pretend that we're in
22531 the context of its DW_AT_specification, namely the contex of die
22532 #3. */
22533 spec_cu = cu;
22534 spec_die = die_specification (die, &spec_cu);
22535 if (spec_die == NULL)
22536 parent = die->parent;
22537 else
22538 {
22539 parent = spec_die->parent;
22540 cu = spec_cu;
22541 }
22542
22543 if (parent == NULL)
22544 return "";
22545 else if (parent->building_fullname)
22546 {
22547 const char *name;
22548 const char *parent_name;
22549
22550 /* It has been seen on RealView 2.2 built binaries,
22551 DW_TAG_template_type_param types actually _defined_ as
22552 children of the parent class:
22553
22554 enum E {};
22555 template class <class Enum> Class{};
22556 Class<enum E> class_e;
22557
22558 1: DW_TAG_class_type (Class)
22559 2: DW_TAG_enumeration_type (E)
22560 3: DW_TAG_enumerator (enum1:0)
22561 3: DW_TAG_enumerator (enum2:1)
22562 ...
22563 2: DW_TAG_template_type_param
22564 DW_AT_type DW_FORM_ref_udata (E)
22565
22566 Besides being broken debug info, it can put GDB into an
22567 infinite loop. Consider:
22568
22569 When we're building the full name for Class<E>, we'll start
22570 at Class, and go look over its template type parameters,
22571 finding E. We'll then try to build the full name of E, and
22572 reach here. We're now trying to build the full name of E,
22573 and look over the parent DIE for containing scope. In the
22574 broken case, if we followed the parent DIE of E, we'd again
22575 find Class, and once again go look at its template type
22576 arguments, etc., etc. Simply don't consider such parent die
22577 as source-level parent of this die (it can't be, the language
22578 doesn't allow it), and break the loop here. */
22579 name = dwarf2_name (die, cu);
22580 parent_name = dwarf2_name (parent, cu);
22581 complaint (_("template param type '%s' defined within parent '%s'"),
22582 name ? name : "<unknown>",
22583 parent_name ? parent_name : "<unknown>");
22584 return "";
22585 }
22586 else
22587 switch (parent->tag)
22588 {
22589 case DW_TAG_namespace:
22590 parent_type = read_type_die (parent, cu);
22591 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22592 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22593 Work around this problem here. */
22594 if (cu->language == language_cplus
22595 && strcmp (parent_type->name (), "::") == 0)
22596 return "";
22597 /* We give a name to even anonymous namespaces. */
22598 return parent_type->name ();
22599 case DW_TAG_class_type:
22600 case DW_TAG_interface_type:
22601 case DW_TAG_structure_type:
22602 case DW_TAG_union_type:
22603 case DW_TAG_module:
22604 parent_type = read_type_die (parent, cu);
22605 if (parent_type->name () != NULL)
22606 return parent_type->name ();
22607 else
22608 /* An anonymous structure is only allowed non-static data
22609 members; no typedefs, no member functions, et cetera.
22610 So it does not need a prefix. */
22611 return "";
22612 case DW_TAG_compile_unit:
22613 case DW_TAG_partial_unit:
22614 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22615 if (cu->language == language_cplus
22616 && !per_objfile->per_bfd->types.empty ()
22617 && die->child != NULL
22618 && (die->tag == DW_TAG_class_type
22619 || die->tag == DW_TAG_structure_type
22620 || die->tag == DW_TAG_union_type))
22621 {
22622 const char *name = guess_full_die_structure_name (die, cu);
22623 if (name != NULL)
22624 return name;
22625 }
22626 return "";
22627 case DW_TAG_subprogram:
22628 /* Nested subroutines in Fortran get a prefix with the name
22629 of the parent's subroutine. */
22630 if (cu->language == language_fortran)
22631 {
22632 if ((die->tag == DW_TAG_subprogram)
22633 && (dwarf2_name (parent, cu) != NULL))
22634 return dwarf2_name (parent, cu);
22635 }
22636 return determine_prefix (parent, cu);
22637 case DW_TAG_enumeration_type:
22638 parent_type = read_type_die (parent, cu);
22639 if (parent_type->is_declared_class ())
22640 {
22641 if (parent_type->name () != NULL)
22642 return parent_type->name ();
22643 return "";
22644 }
22645 /* Fall through. */
22646 default:
22647 return determine_prefix (parent, cu);
22648 }
22649 }
22650
22651 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22652 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22653 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22654 an obconcat, otherwise allocate storage for the result. The CU argument is
22655 used to determine the language and hence, the appropriate separator. */
22656
22657 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22658
22659 static char *
22660 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22661 int physname, struct dwarf2_cu *cu)
22662 {
22663 const char *lead = "";
22664 const char *sep;
22665
22666 if (suffix == NULL || suffix[0] == '\0'
22667 || prefix == NULL || prefix[0] == '\0')
22668 sep = "";
22669 else if (cu->language == language_d)
22670 {
22671 /* For D, the 'main' function could be defined in any module, but it
22672 should never be prefixed. */
22673 if (strcmp (suffix, "D main") == 0)
22674 {
22675 prefix = "";
22676 sep = "";
22677 }
22678 else
22679 sep = ".";
22680 }
22681 else if (cu->language == language_fortran && physname)
22682 {
22683 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22684 DW_AT_MIPS_linkage_name is preferred and used instead. */
22685
22686 lead = "__";
22687 sep = "_MOD_";
22688 }
22689 else
22690 sep = "::";
22691
22692 if (prefix == NULL)
22693 prefix = "";
22694 if (suffix == NULL)
22695 suffix = "";
22696
22697 if (obs == NULL)
22698 {
22699 char *retval
22700 = ((char *)
22701 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22702
22703 strcpy (retval, lead);
22704 strcat (retval, prefix);
22705 strcat (retval, sep);
22706 strcat (retval, suffix);
22707 return retval;
22708 }
22709 else
22710 {
22711 /* We have an obstack. */
22712 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22713 }
22714 }
22715
22716 /* Get name of a die, return NULL if not found. */
22717
22718 static const char *
22719 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22720 struct objfile *objfile)
22721 {
22722 if (name && cu->language == language_cplus)
22723 {
22724 gdb::unique_xmalloc_ptr<char> canon_name
22725 = cp_canonicalize_string (name);
22726
22727 if (canon_name != nullptr)
22728 name = objfile->intern (canon_name.get ());
22729 }
22730
22731 return name;
22732 }
22733
22734 /* Get name of a die, return NULL if not found.
22735 Anonymous namespaces are converted to their magic string. */
22736
22737 static const char *
22738 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22739 {
22740 struct attribute *attr;
22741 struct objfile *objfile = cu->per_objfile->objfile;
22742
22743 attr = dwarf2_attr (die, DW_AT_name, cu);
22744 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22745 if (attr_name == nullptr
22746 && die->tag != DW_TAG_namespace
22747 && die->tag != DW_TAG_class_type
22748 && die->tag != DW_TAG_interface_type
22749 && die->tag != DW_TAG_structure_type
22750 && die->tag != DW_TAG_union_type)
22751 return NULL;
22752
22753 switch (die->tag)
22754 {
22755 case DW_TAG_compile_unit:
22756 case DW_TAG_partial_unit:
22757 /* Compilation units have a DW_AT_name that is a filename, not
22758 a source language identifier. */
22759 case DW_TAG_enumeration_type:
22760 case DW_TAG_enumerator:
22761 /* These tags always have simple identifiers already; no need
22762 to canonicalize them. */
22763 return attr_name;
22764
22765 case DW_TAG_namespace:
22766 if (attr_name != nullptr)
22767 return attr_name;
22768 return CP_ANONYMOUS_NAMESPACE_STR;
22769
22770 case DW_TAG_class_type:
22771 case DW_TAG_interface_type:
22772 case DW_TAG_structure_type:
22773 case DW_TAG_union_type:
22774 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22775 structures or unions. These were of the form "._%d" in GCC 4.1,
22776 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22777 and GCC 4.4. We work around this problem by ignoring these. */
22778 if (attr_name != nullptr
22779 && (startswith (attr_name, "._")
22780 || startswith (attr_name, "<anonymous")))
22781 return NULL;
22782
22783 /* GCC might emit a nameless typedef that has a linkage name. See
22784 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22785 if (!attr || attr_name == NULL)
22786 {
22787 attr = dw2_linkage_name_attr (die, cu);
22788 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22789 if (attr == NULL || attr_name == NULL)
22790 return NULL;
22791
22792 /* Avoid demangling attr_name the second time on a second
22793 call for the same DIE. */
22794 if (!attr->canonical_string_p ())
22795 {
22796 gdb::unique_xmalloc_ptr<char> demangled
22797 (gdb_demangle (attr_name, DMGL_TYPES));
22798 if (demangled == nullptr)
22799 return nullptr;
22800
22801 attr->set_string_canonical (objfile->intern (demangled.get ()));
22802 attr_name = attr->as_string ();
22803 }
22804
22805 /* Strip any leading namespaces/classes, keep only the
22806 base name. DW_AT_name for named DIEs does not
22807 contain the prefixes. */
22808 const char *base = strrchr (attr_name, ':');
22809 if (base && base > attr_name && base[-1] == ':')
22810 return &base[1];
22811 else
22812 return attr_name;
22813 }
22814 break;
22815
22816 default:
22817 break;
22818 }
22819
22820 if (!attr->canonical_string_p ())
22821 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22822 objfile));
22823 return attr->as_string ();
22824 }
22825
22826 /* Return the die that this die in an extension of, or NULL if there
22827 is none. *EXT_CU is the CU containing DIE on input, and the CU
22828 containing the return value on output. */
22829
22830 static struct die_info *
22831 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22832 {
22833 struct attribute *attr;
22834
22835 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22836 if (attr == NULL)
22837 return NULL;
22838
22839 return follow_die_ref (die, attr, ext_cu);
22840 }
22841
22842 static void
22843 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22844 {
22845 unsigned int i;
22846
22847 print_spaces (indent, f);
22848 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22849 dwarf_tag_name (die->tag), die->abbrev,
22850 sect_offset_str (die->sect_off));
22851
22852 if (die->parent != NULL)
22853 {
22854 print_spaces (indent, f);
22855 fprintf_unfiltered (f, " parent at offset: %s\n",
22856 sect_offset_str (die->parent->sect_off));
22857 }
22858
22859 print_spaces (indent, f);
22860 fprintf_unfiltered (f, " has children: %s\n",
22861 dwarf_bool_name (die->child != NULL));
22862
22863 print_spaces (indent, f);
22864 fprintf_unfiltered (f, " attributes:\n");
22865
22866 for (i = 0; i < die->num_attrs; ++i)
22867 {
22868 print_spaces (indent, f);
22869 fprintf_unfiltered (f, " %s (%s) ",
22870 dwarf_attr_name (die->attrs[i].name),
22871 dwarf_form_name (die->attrs[i].form));
22872
22873 switch (die->attrs[i].form)
22874 {
22875 case DW_FORM_addr:
22876 case DW_FORM_addrx:
22877 case DW_FORM_GNU_addr_index:
22878 fprintf_unfiltered (f, "address: ");
22879 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
22880 break;
22881 case DW_FORM_block2:
22882 case DW_FORM_block4:
22883 case DW_FORM_block:
22884 case DW_FORM_block1:
22885 fprintf_unfiltered (f, "block: size %s",
22886 pulongest (die->attrs[i].as_block ()->size));
22887 break;
22888 case DW_FORM_exprloc:
22889 fprintf_unfiltered (f, "expression: size %s",
22890 pulongest (die->attrs[i].as_block ()->size));
22891 break;
22892 case DW_FORM_data16:
22893 fprintf_unfiltered (f, "constant of 16 bytes");
22894 break;
22895 case DW_FORM_ref_addr:
22896 fprintf_unfiltered (f, "ref address: ");
22897 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22898 break;
22899 case DW_FORM_GNU_ref_alt:
22900 fprintf_unfiltered (f, "alt ref address: ");
22901 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22902 break;
22903 case DW_FORM_ref1:
22904 case DW_FORM_ref2:
22905 case DW_FORM_ref4:
22906 case DW_FORM_ref8:
22907 case DW_FORM_ref_udata:
22908 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22909 (long) (die->attrs[i].as_unsigned ()));
22910 break;
22911 case DW_FORM_data1:
22912 case DW_FORM_data2:
22913 case DW_FORM_data4:
22914 case DW_FORM_data8:
22915 case DW_FORM_udata:
22916 fprintf_unfiltered (f, "constant: %s",
22917 pulongest (die->attrs[i].as_unsigned ()));
22918 break;
22919 case DW_FORM_sec_offset:
22920 fprintf_unfiltered (f, "section offset: %s",
22921 pulongest (die->attrs[i].as_unsigned ()));
22922 break;
22923 case DW_FORM_ref_sig8:
22924 fprintf_unfiltered (f, "signature: %s",
22925 hex_string (die->attrs[i].as_signature ()));
22926 break;
22927 case DW_FORM_string:
22928 case DW_FORM_strp:
22929 case DW_FORM_line_strp:
22930 case DW_FORM_strx:
22931 case DW_FORM_GNU_str_index:
22932 case DW_FORM_GNU_strp_alt:
22933 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22934 die->attrs[i].as_string ()
22935 ? die->attrs[i].as_string () : "",
22936 die->attrs[i].canonical_string_p () ? "is" : "not");
22937 break;
22938 case DW_FORM_flag:
22939 if (die->attrs[i].as_boolean ())
22940 fprintf_unfiltered (f, "flag: TRUE");
22941 else
22942 fprintf_unfiltered (f, "flag: FALSE");
22943 break;
22944 case DW_FORM_flag_present:
22945 fprintf_unfiltered (f, "flag: TRUE");
22946 break;
22947 case DW_FORM_indirect:
22948 /* The reader will have reduced the indirect form to
22949 the "base form" so this form should not occur. */
22950 fprintf_unfiltered (f,
22951 "unexpected attribute form: DW_FORM_indirect");
22952 break;
22953 case DW_FORM_sdata:
22954 case DW_FORM_implicit_const:
22955 fprintf_unfiltered (f, "constant: %s",
22956 plongest (die->attrs[i].as_signed ()));
22957 break;
22958 default:
22959 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22960 die->attrs[i].form);
22961 break;
22962 }
22963 fprintf_unfiltered (f, "\n");
22964 }
22965 }
22966
22967 static void
22968 dump_die_for_error (struct die_info *die)
22969 {
22970 dump_die_shallow (gdb_stderr, 0, die);
22971 }
22972
22973 static void
22974 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22975 {
22976 int indent = level * 4;
22977
22978 gdb_assert (die != NULL);
22979
22980 if (level >= max_level)
22981 return;
22982
22983 dump_die_shallow (f, indent, die);
22984
22985 if (die->child != NULL)
22986 {
22987 print_spaces (indent, f);
22988 fprintf_unfiltered (f, " Children:");
22989 if (level + 1 < max_level)
22990 {
22991 fprintf_unfiltered (f, "\n");
22992 dump_die_1 (f, level + 1, max_level, die->child);
22993 }
22994 else
22995 {
22996 fprintf_unfiltered (f,
22997 " [not printed, max nesting level reached]\n");
22998 }
22999 }
23000
23001 if (die->sibling != NULL && level > 0)
23002 {
23003 dump_die_1 (f, level, max_level, die->sibling);
23004 }
23005 }
23006
23007 /* This is called from the pdie macro in gdbinit.in.
23008 It's not static so gcc will keep a copy callable from gdb. */
23009
23010 void
23011 dump_die (struct die_info *die, int max_level)
23012 {
23013 dump_die_1 (gdb_stdlog, 0, max_level, die);
23014 }
23015
23016 static void
23017 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23018 {
23019 void **slot;
23020
23021 slot = htab_find_slot_with_hash (cu->die_hash, die,
23022 to_underlying (die->sect_off),
23023 INSERT);
23024
23025 *slot = die;
23026 }
23027
23028 /* Follow reference or signature attribute ATTR of SRC_DIE.
23029 On entry *REF_CU is the CU of SRC_DIE.
23030 On exit *REF_CU is the CU of the result. */
23031
23032 static struct die_info *
23033 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23034 struct dwarf2_cu **ref_cu)
23035 {
23036 struct die_info *die;
23037
23038 if (attr->form_is_ref ())
23039 die = follow_die_ref (src_die, attr, ref_cu);
23040 else if (attr->form == DW_FORM_ref_sig8)
23041 die = follow_die_sig (src_die, attr, ref_cu);
23042 else
23043 {
23044 dump_die_for_error (src_die);
23045 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23046 objfile_name ((*ref_cu)->per_objfile->objfile));
23047 }
23048
23049 return die;
23050 }
23051
23052 /* Follow reference OFFSET.
23053 On entry *REF_CU is the CU of the source die referencing OFFSET.
23054 On exit *REF_CU is the CU of the result.
23055 Returns NULL if OFFSET is invalid. */
23056
23057 static struct die_info *
23058 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23059 struct dwarf2_cu **ref_cu)
23060 {
23061 struct die_info temp_die;
23062 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23063 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23064
23065 gdb_assert (cu->per_cu != NULL);
23066
23067 target_cu = cu;
23068
23069 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23070 "source CU contains target offset: %d",
23071 sect_offset_str (cu->per_cu->sect_off),
23072 sect_offset_str (sect_off),
23073 cu->header.offset_in_cu_p (sect_off));
23074
23075 if (cu->per_cu->is_debug_types)
23076 {
23077 /* .debug_types CUs cannot reference anything outside their CU.
23078 If they need to, they have to reference a signatured type via
23079 DW_FORM_ref_sig8. */
23080 if (!cu->header.offset_in_cu_p (sect_off))
23081 return NULL;
23082 }
23083 else if (offset_in_dwz != cu->per_cu->is_dwz
23084 || !cu->header.offset_in_cu_p (sect_off))
23085 {
23086 struct dwarf2_per_cu_data *per_cu;
23087
23088 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23089 per_objfile);
23090
23091 dwarf_read_debug_printf_v ("target CU offset: %s, "
23092 "target CU DIEs loaded: %d",
23093 sect_offset_str (per_cu->sect_off),
23094 per_objfile->get_cu (per_cu) != nullptr);
23095
23096 /* If necessary, add it to the queue and load its DIEs.
23097
23098 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23099 it doesn't mean they are currently loaded. Since we require them
23100 to be loaded, we must check for ourselves. */
23101 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23102 || per_objfile->get_cu (per_cu) == nullptr)
23103 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23104 false, cu->language);
23105
23106 target_cu = per_objfile->get_cu (per_cu);
23107 gdb_assert (target_cu != nullptr);
23108 }
23109 else if (cu->dies == NULL)
23110 {
23111 /* We're loading full DIEs during partial symbol reading. */
23112 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23113 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23114 language_minimal);
23115 }
23116
23117 *ref_cu = target_cu;
23118 temp_die.sect_off = sect_off;
23119
23120 if (target_cu != cu)
23121 target_cu->ancestor = cu;
23122
23123 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23124 &temp_die,
23125 to_underlying (sect_off));
23126 }
23127
23128 /* Follow reference attribute ATTR of SRC_DIE.
23129 On entry *REF_CU is the CU of SRC_DIE.
23130 On exit *REF_CU is the CU of the result. */
23131
23132 static struct die_info *
23133 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23134 struct dwarf2_cu **ref_cu)
23135 {
23136 sect_offset sect_off = attr->get_ref_die_offset ();
23137 struct dwarf2_cu *cu = *ref_cu;
23138 struct die_info *die;
23139
23140 die = follow_die_offset (sect_off,
23141 (attr->form == DW_FORM_GNU_ref_alt
23142 || cu->per_cu->is_dwz),
23143 ref_cu);
23144 if (!die)
23145 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23146 "at %s [in module %s]"),
23147 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23148 objfile_name (cu->per_objfile->objfile));
23149
23150 return die;
23151 }
23152
23153 /* See read.h. */
23154
23155 struct dwarf2_locexpr_baton
23156 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23157 dwarf2_per_cu_data *per_cu,
23158 dwarf2_per_objfile *per_objfile,
23159 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23160 bool resolve_abstract_p)
23161 {
23162 struct die_info *die;
23163 struct attribute *attr;
23164 struct dwarf2_locexpr_baton retval;
23165 struct objfile *objfile = per_objfile->objfile;
23166
23167 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23168 if (cu == nullptr)
23169 cu = load_cu (per_cu, per_objfile, false);
23170
23171 if (cu == nullptr)
23172 {
23173 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23174 Instead just throw an error, not much else we can do. */
23175 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23176 sect_offset_str (sect_off), objfile_name (objfile));
23177 }
23178
23179 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23180 if (!die)
23181 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23182 sect_offset_str (sect_off), objfile_name (objfile));
23183
23184 attr = dwarf2_attr (die, DW_AT_location, cu);
23185 if (!attr && resolve_abstract_p
23186 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23187 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23188 {
23189 CORE_ADDR pc = get_frame_pc ();
23190 CORE_ADDR baseaddr = objfile->text_section_offset ();
23191 struct gdbarch *gdbarch = objfile->arch ();
23192
23193 for (const auto &cand_off
23194 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23195 {
23196 struct dwarf2_cu *cand_cu = cu;
23197 struct die_info *cand
23198 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23199 if (!cand
23200 || !cand->parent
23201 || cand->parent->tag != DW_TAG_subprogram)
23202 continue;
23203
23204 CORE_ADDR pc_low, pc_high;
23205 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23206 if (pc_low == ((CORE_ADDR) -1))
23207 continue;
23208 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23209 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23210 if (!(pc_low <= pc && pc < pc_high))
23211 continue;
23212
23213 die = cand;
23214 attr = dwarf2_attr (die, DW_AT_location, cu);
23215 break;
23216 }
23217 }
23218
23219 if (!attr)
23220 {
23221 /* DWARF: "If there is no such attribute, then there is no effect.".
23222 DATA is ignored if SIZE is 0. */
23223
23224 retval.data = NULL;
23225 retval.size = 0;
23226 }
23227 else if (attr->form_is_section_offset ())
23228 {
23229 struct dwarf2_loclist_baton loclist_baton;
23230 CORE_ADDR pc = get_frame_pc ();
23231 size_t size;
23232
23233 fill_in_loclist_baton (cu, &loclist_baton, attr);
23234
23235 retval.data = dwarf2_find_location_expression (&loclist_baton,
23236 &size, pc);
23237 retval.size = size;
23238 }
23239 else
23240 {
23241 if (!attr->form_is_block ())
23242 error (_("Dwarf Error: DIE at %s referenced in module %s "
23243 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23244 sect_offset_str (sect_off), objfile_name (objfile));
23245
23246 struct dwarf_block *block = attr->as_block ();
23247 retval.data = block->data;
23248 retval.size = block->size;
23249 }
23250 retval.per_objfile = per_objfile;
23251 retval.per_cu = cu->per_cu;
23252
23253 per_objfile->age_comp_units ();
23254
23255 return retval;
23256 }
23257
23258 /* See read.h. */
23259
23260 struct dwarf2_locexpr_baton
23261 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23262 dwarf2_per_cu_data *per_cu,
23263 dwarf2_per_objfile *per_objfile,
23264 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23265 {
23266 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23267
23268 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23269 get_frame_pc);
23270 }
23271
23272 /* Write a constant of a given type as target-ordered bytes into
23273 OBSTACK. */
23274
23275 static const gdb_byte *
23276 write_constant_as_bytes (struct obstack *obstack,
23277 enum bfd_endian byte_order,
23278 struct type *type,
23279 ULONGEST value,
23280 LONGEST *len)
23281 {
23282 gdb_byte *result;
23283
23284 *len = TYPE_LENGTH (type);
23285 result = (gdb_byte *) obstack_alloc (obstack, *len);
23286 store_unsigned_integer (result, *len, byte_order, value);
23287
23288 return result;
23289 }
23290
23291 /* See read.h. */
23292
23293 const gdb_byte *
23294 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23295 dwarf2_per_cu_data *per_cu,
23296 dwarf2_per_objfile *per_objfile,
23297 obstack *obstack,
23298 LONGEST *len)
23299 {
23300 struct die_info *die;
23301 struct attribute *attr;
23302 const gdb_byte *result = NULL;
23303 struct type *type;
23304 LONGEST value;
23305 enum bfd_endian byte_order;
23306 struct objfile *objfile = per_objfile->objfile;
23307
23308 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23309 if (cu == nullptr)
23310 cu = load_cu (per_cu, per_objfile, false);
23311
23312 if (cu == nullptr)
23313 {
23314 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23315 Instead just throw an error, not much else we can do. */
23316 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23317 sect_offset_str (sect_off), objfile_name (objfile));
23318 }
23319
23320 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23321 if (!die)
23322 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23323 sect_offset_str (sect_off), objfile_name (objfile));
23324
23325 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23326 if (attr == NULL)
23327 return NULL;
23328
23329 byte_order = (bfd_big_endian (objfile->obfd)
23330 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23331
23332 switch (attr->form)
23333 {
23334 case DW_FORM_addr:
23335 case DW_FORM_addrx:
23336 case DW_FORM_GNU_addr_index:
23337 {
23338 gdb_byte *tem;
23339
23340 *len = cu->header.addr_size;
23341 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23342 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23343 result = tem;
23344 }
23345 break;
23346 case DW_FORM_string:
23347 case DW_FORM_strp:
23348 case DW_FORM_strx:
23349 case DW_FORM_GNU_str_index:
23350 case DW_FORM_GNU_strp_alt:
23351 /* The string is already allocated on the objfile obstack, point
23352 directly to it. */
23353 {
23354 const char *attr_name = attr->as_string ();
23355 result = (const gdb_byte *) attr_name;
23356 *len = strlen (attr_name);
23357 }
23358 break;
23359 case DW_FORM_block1:
23360 case DW_FORM_block2:
23361 case DW_FORM_block4:
23362 case DW_FORM_block:
23363 case DW_FORM_exprloc:
23364 case DW_FORM_data16:
23365 {
23366 struct dwarf_block *block = attr->as_block ();
23367 result = block->data;
23368 *len = block->size;
23369 }
23370 break;
23371
23372 /* The DW_AT_const_value attributes are supposed to carry the
23373 symbol's value "represented as it would be on the target
23374 architecture." By the time we get here, it's already been
23375 converted to host endianness, so we just need to sign- or
23376 zero-extend it as appropriate. */
23377 case DW_FORM_data1:
23378 type = die_type (die, cu);
23379 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23380 if (result == NULL)
23381 result = write_constant_as_bytes (obstack, byte_order,
23382 type, value, len);
23383 break;
23384 case DW_FORM_data2:
23385 type = die_type (die, cu);
23386 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23387 if (result == NULL)
23388 result = write_constant_as_bytes (obstack, byte_order,
23389 type, value, len);
23390 break;
23391 case DW_FORM_data4:
23392 type = die_type (die, cu);
23393 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23394 if (result == NULL)
23395 result = write_constant_as_bytes (obstack, byte_order,
23396 type, value, len);
23397 break;
23398 case DW_FORM_data8:
23399 type = die_type (die, cu);
23400 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23401 if (result == NULL)
23402 result = write_constant_as_bytes (obstack, byte_order,
23403 type, value, len);
23404 break;
23405
23406 case DW_FORM_sdata:
23407 case DW_FORM_implicit_const:
23408 type = die_type (die, cu);
23409 result = write_constant_as_bytes (obstack, byte_order,
23410 type, attr->as_signed (), len);
23411 break;
23412
23413 case DW_FORM_udata:
23414 type = die_type (die, cu);
23415 result = write_constant_as_bytes (obstack, byte_order,
23416 type, attr->as_unsigned (), len);
23417 break;
23418
23419 default:
23420 complaint (_("unsupported const value attribute form: '%s'"),
23421 dwarf_form_name (attr->form));
23422 break;
23423 }
23424
23425 return result;
23426 }
23427
23428 /* See read.h. */
23429
23430 struct type *
23431 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23432 dwarf2_per_cu_data *per_cu,
23433 dwarf2_per_objfile *per_objfile)
23434 {
23435 struct die_info *die;
23436
23437 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23438 if (cu == nullptr)
23439 cu = load_cu (per_cu, per_objfile, false);
23440
23441 if (cu == nullptr)
23442 return nullptr;
23443
23444 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23445 if (!die)
23446 return NULL;
23447
23448 return die_type (die, cu);
23449 }
23450
23451 /* See read.h. */
23452
23453 struct type *
23454 dwarf2_get_die_type (cu_offset die_offset,
23455 dwarf2_per_cu_data *per_cu,
23456 dwarf2_per_objfile *per_objfile)
23457 {
23458 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23459 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23460 }
23461
23462 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23463 On entry *REF_CU is the CU of SRC_DIE.
23464 On exit *REF_CU is the CU of the result.
23465 Returns NULL if the referenced DIE isn't found. */
23466
23467 static struct die_info *
23468 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23469 struct dwarf2_cu **ref_cu)
23470 {
23471 struct die_info temp_die;
23472 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23473 struct die_info *die;
23474 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23475
23476
23477 /* While it might be nice to assert sig_type->type == NULL here,
23478 we can get here for DW_AT_imported_declaration where we need
23479 the DIE not the type. */
23480
23481 /* If necessary, add it to the queue and load its DIEs.
23482
23483 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23484 it doesn't mean they are currently loaded. Since we require them
23485 to be loaded, we must check for ourselves. */
23486 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23487 language_minimal)
23488 || per_objfile->get_cu (sig_type) == nullptr)
23489 read_signatured_type (sig_type, per_objfile);
23490
23491 sig_cu = per_objfile->get_cu (sig_type);
23492 gdb_assert (sig_cu != NULL);
23493 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23494 temp_die.sect_off = sig_type->type_offset_in_section;
23495 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23496 to_underlying (temp_die.sect_off));
23497 if (die)
23498 {
23499 /* For .gdb_index version 7 keep track of included TUs.
23500 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23501 if (per_objfile->per_bfd->index_table != NULL
23502 && per_objfile->per_bfd->index_table->version <= 7)
23503 {
23504 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23505 }
23506
23507 *ref_cu = sig_cu;
23508 if (sig_cu != cu)
23509 sig_cu->ancestor = cu;
23510
23511 return die;
23512 }
23513
23514 return NULL;
23515 }
23516
23517 /* Follow signatured type referenced by ATTR in SRC_DIE.
23518 On entry *REF_CU is the CU of SRC_DIE.
23519 On exit *REF_CU is the CU of the result.
23520 The result is the DIE of the type.
23521 If the referenced type cannot be found an error is thrown. */
23522
23523 static struct die_info *
23524 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23525 struct dwarf2_cu **ref_cu)
23526 {
23527 ULONGEST signature = attr->as_signature ();
23528 struct signatured_type *sig_type;
23529 struct die_info *die;
23530
23531 gdb_assert (attr->form == DW_FORM_ref_sig8);
23532
23533 sig_type = lookup_signatured_type (*ref_cu, signature);
23534 /* sig_type will be NULL if the signatured type is missing from
23535 the debug info. */
23536 if (sig_type == NULL)
23537 {
23538 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23539 " from DIE at %s [in module %s]"),
23540 hex_string (signature), sect_offset_str (src_die->sect_off),
23541 objfile_name ((*ref_cu)->per_objfile->objfile));
23542 }
23543
23544 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23545 if (die == NULL)
23546 {
23547 dump_die_for_error (src_die);
23548 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23549 " from DIE at %s [in module %s]"),
23550 hex_string (signature), sect_offset_str (src_die->sect_off),
23551 objfile_name ((*ref_cu)->per_objfile->objfile));
23552 }
23553
23554 return die;
23555 }
23556
23557 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23558 reading in and processing the type unit if necessary. */
23559
23560 static struct type *
23561 get_signatured_type (struct die_info *die, ULONGEST signature,
23562 struct dwarf2_cu *cu)
23563 {
23564 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23565 struct signatured_type *sig_type;
23566 struct dwarf2_cu *type_cu;
23567 struct die_info *type_die;
23568 struct type *type;
23569
23570 sig_type = lookup_signatured_type (cu, signature);
23571 /* sig_type will be NULL if the signatured type is missing from
23572 the debug info. */
23573 if (sig_type == NULL)
23574 {
23575 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23576 " from DIE at %s [in module %s]"),
23577 hex_string (signature), sect_offset_str (die->sect_off),
23578 objfile_name (per_objfile->objfile));
23579 return build_error_marker_type (cu, die);
23580 }
23581
23582 /* If we already know the type we're done. */
23583 type = per_objfile->get_type_for_signatured_type (sig_type);
23584 if (type != nullptr)
23585 return type;
23586
23587 type_cu = cu;
23588 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23589 if (type_die != NULL)
23590 {
23591 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23592 is created. This is important, for example, because for c++ classes
23593 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23594 type = read_type_die (type_die, type_cu);
23595 if (type == NULL)
23596 {
23597 complaint (_("Dwarf Error: Cannot build signatured type %s"
23598 " referenced from DIE at %s [in module %s]"),
23599 hex_string (signature), sect_offset_str (die->sect_off),
23600 objfile_name (per_objfile->objfile));
23601 type = build_error_marker_type (cu, die);
23602 }
23603 }
23604 else
23605 {
23606 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23607 " from DIE at %s [in module %s]"),
23608 hex_string (signature), sect_offset_str (die->sect_off),
23609 objfile_name (per_objfile->objfile));
23610 type = build_error_marker_type (cu, die);
23611 }
23612
23613 per_objfile->set_type_for_signatured_type (sig_type, type);
23614
23615 return type;
23616 }
23617
23618 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23619 reading in and processing the type unit if necessary. */
23620
23621 static struct type *
23622 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23623 struct dwarf2_cu *cu) /* ARI: editCase function */
23624 {
23625 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23626 if (attr->form_is_ref ())
23627 {
23628 struct dwarf2_cu *type_cu = cu;
23629 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23630
23631 return read_type_die (type_die, type_cu);
23632 }
23633 else if (attr->form == DW_FORM_ref_sig8)
23634 {
23635 return get_signatured_type (die, attr->as_signature (), cu);
23636 }
23637 else
23638 {
23639 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23640
23641 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23642 " at %s [in module %s]"),
23643 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23644 objfile_name (per_objfile->objfile));
23645 return build_error_marker_type (cu, die);
23646 }
23647 }
23648
23649 /* Load the DIEs associated with type unit PER_CU into memory. */
23650
23651 static void
23652 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23653 dwarf2_per_objfile *per_objfile)
23654 {
23655 struct signatured_type *sig_type;
23656
23657 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23658 gdb_assert (! per_cu->type_unit_group_p ());
23659
23660 /* We have the per_cu, but we need the signatured_type.
23661 Fortunately this is an easy translation. */
23662 gdb_assert (per_cu->is_debug_types);
23663 sig_type = (struct signatured_type *) per_cu;
23664
23665 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23666
23667 read_signatured_type (sig_type, per_objfile);
23668
23669 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23670 }
23671
23672 /* Read in a signatured type and build its CU and DIEs.
23673 If the type is a stub for the real type in a DWO file,
23674 read in the real type from the DWO file as well. */
23675
23676 static void
23677 read_signatured_type (signatured_type *sig_type,
23678 dwarf2_per_objfile *per_objfile)
23679 {
23680 gdb_assert (sig_type->is_debug_types);
23681 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23682
23683 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23684
23685 if (!reader.dummy_p)
23686 {
23687 struct dwarf2_cu *cu = reader.cu;
23688 const gdb_byte *info_ptr = reader.info_ptr;
23689
23690 gdb_assert (cu->die_hash == NULL);
23691 cu->die_hash =
23692 htab_create_alloc_ex (cu->header.length / 12,
23693 die_hash,
23694 die_eq,
23695 NULL,
23696 &cu->comp_unit_obstack,
23697 hashtab_obstack_allocate,
23698 dummy_obstack_deallocate);
23699
23700 if (reader.comp_unit_die->has_children)
23701 reader.comp_unit_die->child
23702 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23703 reader.comp_unit_die);
23704 cu->dies = reader.comp_unit_die;
23705 /* comp_unit_die is not stored in die_hash, no need. */
23706
23707 /* We try not to read any attributes in this function, because
23708 not all CUs needed for references have been loaded yet, and
23709 symbol table processing isn't initialized. But we have to
23710 set the CU language, or we won't be able to build types
23711 correctly. Similarly, if we do not read the producer, we can
23712 not apply producer-specific interpretation. */
23713 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23714
23715 reader.keep ();
23716 }
23717
23718 sig_type->tu_read = 1;
23719 }
23720
23721 /* Decode simple location descriptions.
23722 Given a pointer to a dwarf block that defines a location, compute
23723 the location and return the value. If COMPUTED is non-null, it is
23724 set to true to indicate that decoding was successful, and false
23725 otherwise. If COMPUTED is null, then this function may emit a
23726 complaint. */
23727
23728 static CORE_ADDR
23729 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23730 {
23731 struct objfile *objfile = cu->per_objfile->objfile;
23732 size_t i;
23733 size_t size = blk->size;
23734 const gdb_byte *data = blk->data;
23735 CORE_ADDR stack[64];
23736 int stacki;
23737 unsigned int bytes_read, unsnd;
23738 gdb_byte op;
23739
23740 if (computed != nullptr)
23741 *computed = false;
23742
23743 i = 0;
23744 stacki = 0;
23745 stack[stacki] = 0;
23746 stack[++stacki] = 0;
23747
23748 while (i < size)
23749 {
23750 op = data[i++];
23751 switch (op)
23752 {
23753 case DW_OP_lit0:
23754 case DW_OP_lit1:
23755 case DW_OP_lit2:
23756 case DW_OP_lit3:
23757 case DW_OP_lit4:
23758 case DW_OP_lit5:
23759 case DW_OP_lit6:
23760 case DW_OP_lit7:
23761 case DW_OP_lit8:
23762 case DW_OP_lit9:
23763 case DW_OP_lit10:
23764 case DW_OP_lit11:
23765 case DW_OP_lit12:
23766 case DW_OP_lit13:
23767 case DW_OP_lit14:
23768 case DW_OP_lit15:
23769 case DW_OP_lit16:
23770 case DW_OP_lit17:
23771 case DW_OP_lit18:
23772 case DW_OP_lit19:
23773 case DW_OP_lit20:
23774 case DW_OP_lit21:
23775 case DW_OP_lit22:
23776 case DW_OP_lit23:
23777 case DW_OP_lit24:
23778 case DW_OP_lit25:
23779 case DW_OP_lit26:
23780 case DW_OP_lit27:
23781 case DW_OP_lit28:
23782 case DW_OP_lit29:
23783 case DW_OP_lit30:
23784 case DW_OP_lit31:
23785 stack[++stacki] = op - DW_OP_lit0;
23786 break;
23787
23788 case DW_OP_reg0:
23789 case DW_OP_reg1:
23790 case DW_OP_reg2:
23791 case DW_OP_reg3:
23792 case DW_OP_reg4:
23793 case DW_OP_reg5:
23794 case DW_OP_reg6:
23795 case DW_OP_reg7:
23796 case DW_OP_reg8:
23797 case DW_OP_reg9:
23798 case DW_OP_reg10:
23799 case DW_OP_reg11:
23800 case DW_OP_reg12:
23801 case DW_OP_reg13:
23802 case DW_OP_reg14:
23803 case DW_OP_reg15:
23804 case DW_OP_reg16:
23805 case DW_OP_reg17:
23806 case DW_OP_reg18:
23807 case DW_OP_reg19:
23808 case DW_OP_reg20:
23809 case DW_OP_reg21:
23810 case DW_OP_reg22:
23811 case DW_OP_reg23:
23812 case DW_OP_reg24:
23813 case DW_OP_reg25:
23814 case DW_OP_reg26:
23815 case DW_OP_reg27:
23816 case DW_OP_reg28:
23817 case DW_OP_reg29:
23818 case DW_OP_reg30:
23819 case DW_OP_reg31:
23820 stack[++stacki] = op - DW_OP_reg0;
23821 if (i < size)
23822 {
23823 if (computed == nullptr)
23824 dwarf2_complex_location_expr_complaint ();
23825 else
23826 return 0;
23827 }
23828 break;
23829
23830 case DW_OP_regx:
23831 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23832 i += bytes_read;
23833 stack[++stacki] = unsnd;
23834 if (i < size)
23835 {
23836 if (computed == nullptr)
23837 dwarf2_complex_location_expr_complaint ();
23838 else
23839 return 0;
23840 }
23841 break;
23842
23843 case DW_OP_addr:
23844 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23845 &bytes_read);
23846 i += bytes_read;
23847 break;
23848
23849 case DW_OP_const1u:
23850 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23851 i += 1;
23852 break;
23853
23854 case DW_OP_const1s:
23855 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23856 i += 1;
23857 break;
23858
23859 case DW_OP_const2u:
23860 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23861 i += 2;
23862 break;
23863
23864 case DW_OP_const2s:
23865 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23866 i += 2;
23867 break;
23868
23869 case DW_OP_const4u:
23870 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23871 i += 4;
23872 break;
23873
23874 case DW_OP_const4s:
23875 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23876 i += 4;
23877 break;
23878
23879 case DW_OP_const8u:
23880 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23881 i += 8;
23882 break;
23883
23884 case DW_OP_constu:
23885 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23886 &bytes_read);
23887 i += bytes_read;
23888 break;
23889
23890 case DW_OP_consts:
23891 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23892 i += bytes_read;
23893 break;
23894
23895 case DW_OP_dup:
23896 stack[stacki + 1] = stack[stacki];
23897 stacki++;
23898 break;
23899
23900 case DW_OP_plus:
23901 stack[stacki - 1] += stack[stacki];
23902 stacki--;
23903 break;
23904
23905 case DW_OP_plus_uconst:
23906 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23907 &bytes_read);
23908 i += bytes_read;
23909 break;
23910
23911 case DW_OP_minus:
23912 stack[stacki - 1] -= stack[stacki];
23913 stacki--;
23914 break;
23915
23916 case DW_OP_deref:
23917 /* If we're not the last op, then we definitely can't encode
23918 this using GDB's address_class enum. This is valid for partial
23919 global symbols, although the variable's address will be bogus
23920 in the psymtab. */
23921 if (i < size)
23922 {
23923 if (computed == nullptr)
23924 dwarf2_complex_location_expr_complaint ();
23925 else
23926 return 0;
23927 }
23928 break;
23929
23930 case DW_OP_GNU_push_tls_address:
23931 case DW_OP_form_tls_address:
23932 /* The top of the stack has the offset from the beginning
23933 of the thread control block at which the variable is located. */
23934 /* Nothing should follow this operator, so the top of stack would
23935 be returned. */
23936 /* This is valid for partial global symbols, but the variable's
23937 address will be bogus in the psymtab. Make it always at least
23938 non-zero to not look as a variable garbage collected by linker
23939 which have DW_OP_addr 0. */
23940 if (i < size)
23941 {
23942 if (computed == nullptr)
23943 dwarf2_complex_location_expr_complaint ();
23944 else
23945 return 0;
23946 }
23947 stack[stacki]++;
23948 break;
23949
23950 case DW_OP_GNU_uninit:
23951 if (computed != nullptr)
23952 return 0;
23953 break;
23954
23955 case DW_OP_addrx:
23956 case DW_OP_GNU_addr_index:
23957 case DW_OP_GNU_const_index:
23958 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23959 &bytes_read);
23960 i += bytes_read;
23961 break;
23962
23963 default:
23964 if (computed == nullptr)
23965 {
23966 const char *name = get_DW_OP_name (op);
23967
23968 if (name)
23969 complaint (_("unsupported stack op: '%s'"),
23970 name);
23971 else
23972 complaint (_("unsupported stack op: '%02x'"),
23973 op);
23974 }
23975
23976 return (stack[stacki]);
23977 }
23978
23979 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23980 outside of the allocated space. Also enforce minimum>0. */
23981 if (stacki >= ARRAY_SIZE (stack) - 1)
23982 {
23983 if (computed == nullptr)
23984 complaint (_("location description stack overflow"));
23985 return 0;
23986 }
23987
23988 if (stacki <= 0)
23989 {
23990 if (computed == nullptr)
23991 complaint (_("location description stack underflow"));
23992 return 0;
23993 }
23994 }
23995
23996 if (computed != nullptr)
23997 *computed = true;
23998 return (stack[stacki]);
23999 }
24000
24001 /* memory allocation interface */
24002
24003 static struct dwarf_block *
24004 dwarf_alloc_block (struct dwarf2_cu *cu)
24005 {
24006 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24007 }
24008
24009 static struct die_info *
24010 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24011 {
24012 struct die_info *die;
24013 size_t size = sizeof (struct die_info);
24014
24015 if (num_attrs > 1)
24016 size += (num_attrs - 1) * sizeof (struct attribute);
24017
24018 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24019 memset (die, 0, sizeof (struct die_info));
24020 return (die);
24021 }
24022
24023 \f
24024
24025 /* Macro support. */
24026
24027 /* An overload of dwarf_decode_macros that finds the correct section
24028 and ensures it is read in before calling the other overload. */
24029
24030 static void
24031 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24032 int section_is_gnu)
24033 {
24034 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24035 struct objfile *objfile = per_objfile->objfile;
24036 const struct line_header *lh = cu->line_header;
24037 unsigned int offset_size = cu->header.offset_size;
24038 struct dwarf2_section_info *section;
24039 const char *section_name;
24040
24041 if (cu->dwo_unit != nullptr)
24042 {
24043 if (section_is_gnu)
24044 {
24045 section = &cu->dwo_unit->dwo_file->sections.macro;
24046 section_name = ".debug_macro.dwo";
24047 }
24048 else
24049 {
24050 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24051 section_name = ".debug_macinfo.dwo";
24052 }
24053 }
24054 else
24055 {
24056 if (section_is_gnu)
24057 {
24058 section = &per_objfile->per_bfd->macro;
24059 section_name = ".debug_macro";
24060 }
24061 else
24062 {
24063 section = &per_objfile->per_bfd->macinfo;
24064 section_name = ".debug_macinfo";
24065 }
24066 }
24067
24068 section->read (objfile);
24069 if (section->buffer == nullptr)
24070 {
24071 complaint (_("missing %s section"), section_name);
24072 return;
24073 }
24074
24075 buildsym_compunit *builder = cu->get_builder ();
24076
24077 struct dwarf2_section_info *str_offsets_section;
24078 struct dwarf2_section_info *str_section;
24079 ULONGEST str_offsets_base;
24080
24081 if (cu->dwo_unit != nullptr)
24082 {
24083 str_offsets_section = &cu->dwo_unit->dwo_file
24084 ->sections.str_offsets;
24085 str_section = &cu->dwo_unit->dwo_file->sections.str;
24086 str_offsets_base = cu->header.addr_size;
24087 }
24088 else
24089 {
24090 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24091 str_section = &per_objfile->per_bfd->str;
24092 str_offsets_base = *cu->str_offsets_base;
24093 }
24094
24095 dwarf_decode_macros (per_objfile, builder, section, lh,
24096 offset_size, offset, str_section, str_offsets_section,
24097 str_offsets_base, section_is_gnu);
24098 }
24099
24100 /* Return the .debug_loc section to use for CU.
24101 For DWO files use .debug_loc.dwo. */
24102
24103 static struct dwarf2_section_info *
24104 cu_debug_loc_section (struct dwarf2_cu *cu)
24105 {
24106 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24107
24108 if (cu->dwo_unit)
24109 {
24110 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24111
24112 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24113 }
24114 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24115 : &per_objfile->per_bfd->loc);
24116 }
24117
24118 /* Return the .debug_rnglists section to use for CU. */
24119 static struct dwarf2_section_info *
24120 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24121 {
24122 if (cu->header.version < 5)
24123 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24124 cu->header.version);
24125 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24126
24127 /* Make sure we read the .debug_rnglists section from the file that
24128 contains the DW_AT_ranges attribute we are reading. Normally that
24129 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24130 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24131 program. */
24132 if (cu->dwo_unit != nullptr
24133 && tag != DW_TAG_compile_unit
24134 && tag != DW_TAG_skeleton_unit)
24135 {
24136 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24137
24138 if (sections->rnglists.size > 0)
24139 return &sections->rnglists;
24140 else
24141 error (_(".debug_rnglists section is missing from .dwo file."));
24142 }
24143 return &dwarf2_per_objfile->per_bfd->rnglists;
24144 }
24145
24146 /* A helper function that fills in a dwarf2_loclist_baton. */
24147
24148 static void
24149 fill_in_loclist_baton (struct dwarf2_cu *cu,
24150 struct dwarf2_loclist_baton *baton,
24151 const struct attribute *attr)
24152 {
24153 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24154 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24155
24156 section->read (per_objfile->objfile);
24157
24158 baton->per_objfile = per_objfile;
24159 baton->per_cu = cu->per_cu;
24160 gdb_assert (baton->per_cu);
24161 /* We don't know how long the location list is, but make sure we
24162 don't run off the edge of the section. */
24163 baton->size = section->size - attr->as_unsigned ();
24164 baton->data = section->buffer + attr->as_unsigned ();
24165 if (cu->base_address.has_value ())
24166 baton->base_address = *cu->base_address;
24167 else
24168 baton->base_address = 0;
24169 baton->from_dwo = cu->dwo_unit != NULL;
24170 }
24171
24172 static void
24173 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24174 struct dwarf2_cu *cu, int is_block)
24175 {
24176 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24177 struct objfile *objfile = per_objfile->objfile;
24178 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24179
24180 if (attr->form_is_section_offset ()
24181 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24182 the section. If so, fall through to the complaint in the
24183 other branch. */
24184 && attr->as_unsigned () < section->get_size (objfile))
24185 {
24186 struct dwarf2_loclist_baton *baton;
24187
24188 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24189
24190 fill_in_loclist_baton (cu, baton, attr);
24191
24192 if (!cu->base_address.has_value ())
24193 complaint (_("Location list used without "
24194 "specifying the CU base address."));
24195
24196 SYMBOL_ACLASS_INDEX (sym) = (is_block
24197 ? dwarf2_loclist_block_index
24198 : dwarf2_loclist_index);
24199 SYMBOL_LOCATION_BATON (sym) = baton;
24200 }
24201 else
24202 {
24203 struct dwarf2_locexpr_baton *baton;
24204
24205 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24206 baton->per_objfile = per_objfile;
24207 baton->per_cu = cu->per_cu;
24208 gdb_assert (baton->per_cu);
24209
24210 if (attr->form_is_block ())
24211 {
24212 /* Note that we're just copying the block's data pointer
24213 here, not the actual data. We're still pointing into the
24214 info_buffer for SYM's objfile; right now we never release
24215 that buffer, but when we do clean up properly this may
24216 need to change. */
24217 struct dwarf_block *block = attr->as_block ();
24218 baton->size = block->size;
24219 baton->data = block->data;
24220 }
24221 else
24222 {
24223 dwarf2_invalid_attrib_class_complaint ("location description",
24224 sym->natural_name ());
24225 baton->size = 0;
24226 }
24227
24228 SYMBOL_ACLASS_INDEX (sym) = (is_block
24229 ? dwarf2_locexpr_block_index
24230 : dwarf2_locexpr_index);
24231 SYMBOL_LOCATION_BATON (sym) = baton;
24232 }
24233 }
24234
24235 /* See read.h. */
24236
24237 const comp_unit_head *
24238 dwarf2_per_cu_data::get_header () const
24239 {
24240 if (!m_header_read_in)
24241 {
24242 const gdb_byte *info_ptr
24243 = this->section->buffer + to_underlying (this->sect_off);
24244
24245 memset (&m_header, 0, sizeof (m_header));
24246
24247 read_comp_unit_head (&m_header, info_ptr, this->section,
24248 rcuh_kind::COMPILE);
24249
24250 m_header_read_in = true;
24251 }
24252
24253 return &m_header;
24254 }
24255
24256 /* See read.h. */
24257
24258 int
24259 dwarf2_per_cu_data::addr_size () const
24260 {
24261 return this->get_header ()->addr_size;
24262 }
24263
24264 /* See read.h. */
24265
24266 int
24267 dwarf2_per_cu_data::offset_size () const
24268 {
24269 return this->get_header ()->offset_size;
24270 }
24271
24272 /* See read.h. */
24273
24274 int
24275 dwarf2_per_cu_data::ref_addr_size () const
24276 {
24277 const comp_unit_head *header = this->get_header ();
24278
24279 if (header->version == 2)
24280 return header->addr_size;
24281 else
24282 return header->offset_size;
24283 }
24284
24285 /* A helper function for dwarf2_find_containing_comp_unit that returns
24286 the index of the result, and that searches a vector. It will
24287 return a result even if the offset in question does not actually
24288 occur in any CU. This is separate so that it can be unit
24289 tested. */
24290
24291 static int
24292 dwarf2_find_containing_comp_unit
24293 (sect_offset sect_off,
24294 unsigned int offset_in_dwz,
24295 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24296 {
24297 int low, high;
24298
24299 low = 0;
24300 high = all_comp_units.size () - 1;
24301 while (high > low)
24302 {
24303 struct dwarf2_per_cu_data *mid_cu;
24304 int mid = low + (high - low) / 2;
24305
24306 mid_cu = all_comp_units[mid].get ();
24307 if (mid_cu->is_dwz > offset_in_dwz
24308 || (mid_cu->is_dwz == offset_in_dwz
24309 && mid_cu->sect_off + mid_cu->length > sect_off))
24310 high = mid;
24311 else
24312 low = mid + 1;
24313 }
24314 gdb_assert (low == high);
24315 return low;
24316 }
24317
24318 /* Locate the .debug_info compilation unit from CU's objfile which contains
24319 the DIE at OFFSET. Raises an error on failure. */
24320
24321 static struct dwarf2_per_cu_data *
24322 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24323 unsigned int offset_in_dwz,
24324 dwarf2_per_objfile *per_objfile)
24325 {
24326 int low = dwarf2_find_containing_comp_unit
24327 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24328 dwarf2_per_cu_data *this_cu
24329 = per_objfile->per_bfd->all_comp_units[low].get ();
24330
24331 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24332 {
24333 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24334 error (_("Dwarf Error: could not find partial DIE containing "
24335 "offset %s [in module %s]"),
24336 sect_offset_str (sect_off),
24337 bfd_get_filename (per_objfile->objfile->obfd));
24338
24339 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24340 <= sect_off);
24341 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
24342 }
24343 else
24344 {
24345 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24346 && sect_off >= this_cu->sect_off + this_cu->length)
24347 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24348 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24349 return this_cu;
24350 }
24351 }
24352
24353 #if GDB_SELF_TEST
24354
24355 namespace selftests {
24356 namespace find_containing_comp_unit {
24357
24358 static void
24359 run_test ()
24360 {
24361 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24362 dwarf2_per_cu_data *one_ptr = one.get ();
24363 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24364 dwarf2_per_cu_data *two_ptr = two.get ();
24365 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24366 dwarf2_per_cu_data *three_ptr = three.get ();
24367 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24368 dwarf2_per_cu_data *four_ptr = four.get ();
24369
24370 one->length = 5;
24371 two->sect_off = sect_offset (one->length);
24372 two->length = 7;
24373
24374 three->length = 5;
24375 three->is_dwz = 1;
24376 four->sect_off = sect_offset (three->length);
24377 four->length = 7;
24378 four->is_dwz = 1;
24379
24380 std::vector<dwarf2_per_cu_data_up> units;
24381 units.push_back (std::move (one));
24382 units.push_back (std::move (two));
24383 units.push_back (std::move (three));
24384 units.push_back (std::move (four));
24385
24386 int result;
24387
24388 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24389 SELF_CHECK (units[result].get () == one_ptr);
24390 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24391 SELF_CHECK (units[result].get () == one_ptr);
24392 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24393 SELF_CHECK (units[result].get () == two_ptr);
24394
24395 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24396 SELF_CHECK (units[result].get () == three_ptr);
24397 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24398 SELF_CHECK (units[result].get () == three_ptr);
24399 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24400 SELF_CHECK (units[result].get () == four_ptr);
24401 }
24402
24403 }
24404 }
24405
24406 #endif /* GDB_SELF_TEST */
24407
24408 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24409
24410 static void
24411 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24412 enum language pretend_language)
24413 {
24414 struct attribute *attr;
24415
24416 /* Set the language we're debugging. */
24417 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24418 if (attr != nullptr)
24419 set_cu_language (attr->constant_value (0), cu);
24420 else
24421 {
24422 cu->language = pretend_language;
24423 cu->language_defn = language_def (cu->language);
24424 }
24425
24426 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24427 }
24428
24429 /* See read.h. */
24430
24431 dwarf2_cu *
24432 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24433 {
24434 auto it = m_dwarf2_cus.find (per_cu);
24435 if (it == m_dwarf2_cus.end ())
24436 return nullptr;
24437
24438 return it->second;
24439 }
24440
24441 /* See read.h. */
24442
24443 void
24444 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24445 {
24446 gdb_assert (this->get_cu (per_cu) == nullptr);
24447
24448 m_dwarf2_cus[per_cu] = cu;
24449 }
24450
24451 /* See read.h. */
24452
24453 void
24454 dwarf2_per_objfile::age_comp_units ()
24455 {
24456 dwarf_read_debug_printf_v ("running");
24457
24458 /* This is not expected to be called in the middle of CU expansion. There is
24459 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24460 loaded in memory. Calling age_comp_units while the queue is in use could
24461 make us free the DIEs for a CU that is in the queue and therefore break
24462 that invariant. */
24463 gdb_assert (!this->per_bfd->queue.has_value ());
24464
24465 /* Start by clearing all marks. */
24466 for (auto pair : m_dwarf2_cus)
24467 pair.second->clear_mark ();
24468
24469 /* Traverse all CUs, mark them and their dependencies if used recently
24470 enough. */
24471 for (auto pair : m_dwarf2_cus)
24472 {
24473 dwarf2_cu *cu = pair.second;
24474
24475 cu->last_used++;
24476 if (cu->last_used <= dwarf_max_cache_age)
24477 cu->mark ();
24478 }
24479
24480 /* Delete all CUs still not marked. */
24481 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24482 {
24483 dwarf2_cu *cu = it->second;
24484
24485 if (!cu->is_marked ())
24486 {
24487 dwarf_read_debug_printf_v ("deleting old CU %s",
24488 sect_offset_str (cu->per_cu->sect_off));
24489 delete cu;
24490 it = m_dwarf2_cus.erase (it);
24491 }
24492 else
24493 it++;
24494 }
24495 }
24496
24497 /* See read.h. */
24498
24499 void
24500 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24501 {
24502 auto it = m_dwarf2_cus.find (per_cu);
24503 if (it == m_dwarf2_cus.end ())
24504 return;
24505
24506 delete it->second;
24507
24508 m_dwarf2_cus.erase (it);
24509 }
24510
24511 dwarf2_per_objfile::~dwarf2_per_objfile ()
24512 {
24513 remove_all_cus ();
24514 }
24515
24516 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24517 We store these in a hash table separate from the DIEs, and preserve them
24518 when the DIEs are flushed out of cache.
24519
24520 The CU "per_cu" pointer is needed because offset alone is not enough to
24521 uniquely identify the type. A file may have multiple .debug_types sections,
24522 or the type may come from a DWO file. Furthermore, while it's more logical
24523 to use per_cu->section+offset, with Fission the section with the data is in
24524 the DWO file but we don't know that section at the point we need it.
24525 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24526 because we can enter the lookup routine, get_die_type_at_offset, from
24527 outside this file, and thus won't necessarily have PER_CU->cu.
24528 Fortunately, PER_CU is stable for the life of the objfile. */
24529
24530 struct dwarf2_per_cu_offset_and_type
24531 {
24532 const struct dwarf2_per_cu_data *per_cu;
24533 sect_offset sect_off;
24534 struct type *type;
24535 };
24536
24537 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24538
24539 static hashval_t
24540 per_cu_offset_and_type_hash (const void *item)
24541 {
24542 const struct dwarf2_per_cu_offset_and_type *ofs
24543 = (const struct dwarf2_per_cu_offset_and_type *) item;
24544
24545 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24546 }
24547
24548 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24549
24550 static int
24551 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24552 {
24553 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24554 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24555 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24556 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24557
24558 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24559 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24560 }
24561
24562 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24563 table if necessary. For convenience, return TYPE.
24564
24565 The DIEs reading must have careful ordering to:
24566 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24567 reading current DIE.
24568 * Not trying to dereference contents of still incompletely read in types
24569 while reading in other DIEs.
24570 * Enable referencing still incompletely read in types just by a pointer to
24571 the type without accessing its fields.
24572
24573 Therefore caller should follow these rules:
24574 * Try to fetch any prerequisite types we may need to build this DIE type
24575 before building the type and calling set_die_type.
24576 * After building type call set_die_type for current DIE as soon as
24577 possible before fetching more types to complete the current type.
24578 * Make the type as complete as possible before fetching more types. */
24579
24580 static struct type *
24581 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24582 bool skip_data_location)
24583 {
24584 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24585 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24586 struct objfile *objfile = per_objfile->objfile;
24587 struct attribute *attr;
24588 struct dynamic_prop prop;
24589
24590 /* For Ada types, make sure that the gnat-specific data is always
24591 initialized (if not already set). There are a few types where
24592 we should not be doing so, because the type-specific area is
24593 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24594 where the type-specific area is used to store the floatformat).
24595 But this is not a problem, because the gnat-specific information
24596 is actually not needed for these types. */
24597 if (need_gnat_info (cu)
24598 && type->code () != TYPE_CODE_FUNC
24599 && type->code () != TYPE_CODE_FLT
24600 && type->code () != TYPE_CODE_METHODPTR
24601 && type->code () != TYPE_CODE_MEMBERPTR
24602 && type->code () != TYPE_CODE_METHOD
24603 && type->code () != TYPE_CODE_FIXED_POINT
24604 && !HAVE_GNAT_AUX_INFO (type))
24605 INIT_GNAT_SPECIFIC (type);
24606
24607 /* Read DW_AT_allocated and set in type. */
24608 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24609 if (attr != NULL)
24610 {
24611 struct type *prop_type = cu->addr_sized_int_type (false);
24612 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24613 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24614 }
24615
24616 /* Read DW_AT_associated and set in type. */
24617 attr = dwarf2_attr (die, DW_AT_associated, cu);
24618 if (attr != NULL)
24619 {
24620 struct type *prop_type = cu->addr_sized_int_type (false);
24621 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24622 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24623 }
24624
24625 /* Read DW_AT_data_location and set in type. */
24626 if (!skip_data_location)
24627 {
24628 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24629 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24630 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24631 }
24632
24633 if (per_objfile->die_type_hash == NULL)
24634 per_objfile->die_type_hash
24635 = htab_up (htab_create_alloc (127,
24636 per_cu_offset_and_type_hash,
24637 per_cu_offset_and_type_eq,
24638 NULL, xcalloc, xfree));
24639
24640 ofs.per_cu = cu->per_cu;
24641 ofs.sect_off = die->sect_off;
24642 ofs.type = type;
24643 slot = (struct dwarf2_per_cu_offset_and_type **)
24644 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24645 if (*slot)
24646 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24647 sect_offset_str (die->sect_off));
24648 *slot = XOBNEW (&objfile->objfile_obstack,
24649 struct dwarf2_per_cu_offset_and_type);
24650 **slot = ofs;
24651 return type;
24652 }
24653
24654 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24655 or return NULL if the die does not have a saved type. */
24656
24657 static struct type *
24658 get_die_type_at_offset (sect_offset sect_off,
24659 dwarf2_per_cu_data *per_cu,
24660 dwarf2_per_objfile *per_objfile)
24661 {
24662 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24663
24664 if (per_objfile->die_type_hash == NULL)
24665 return NULL;
24666
24667 ofs.per_cu = per_cu;
24668 ofs.sect_off = sect_off;
24669 slot = ((struct dwarf2_per_cu_offset_and_type *)
24670 htab_find (per_objfile->die_type_hash.get (), &ofs));
24671 if (slot)
24672 return slot->type;
24673 else
24674 return NULL;
24675 }
24676
24677 /* Look up the type for DIE in CU in die_type_hash,
24678 or return NULL if DIE does not have a saved type. */
24679
24680 static struct type *
24681 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24682 {
24683 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24684 }
24685
24686 /* Trivial hash function for partial_die_info: the hash value of a DIE
24687 is its offset in .debug_info for this objfile. */
24688
24689 static hashval_t
24690 partial_die_hash (const void *item)
24691 {
24692 const struct partial_die_info *part_die
24693 = (const struct partial_die_info *) item;
24694
24695 return to_underlying (part_die->sect_off);
24696 }
24697
24698 /* Trivial comparison function for partial_die_info structures: two DIEs
24699 are equal if they have the same offset. */
24700
24701 static int
24702 partial_die_eq (const void *item_lhs, const void *item_rhs)
24703 {
24704 const struct partial_die_info *part_die_lhs
24705 = (const struct partial_die_info *) item_lhs;
24706 const struct partial_die_info *part_die_rhs
24707 = (const struct partial_die_info *) item_rhs;
24708
24709 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24710 }
24711
24712 struct cmd_list_element *set_dwarf_cmdlist;
24713 struct cmd_list_element *show_dwarf_cmdlist;
24714
24715 static void
24716 show_check_physname (struct ui_file *file, int from_tty,
24717 struct cmd_list_element *c, const char *value)
24718 {
24719 fprintf_filtered (file,
24720 _("Whether to check \"physname\" is %s.\n"),
24721 value);
24722 }
24723
24724 void _initialize_dwarf2_read ();
24725 void
24726 _initialize_dwarf2_read ()
24727 {
24728 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24729 Set DWARF specific variables.\n\
24730 Configure DWARF variables such as the cache size."),
24731 &set_dwarf_cmdlist,
24732 0/*allow-unknown*/, &maintenance_set_cmdlist);
24733
24734 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24735 Show DWARF specific variables.\n\
24736 Show DWARF variables such as the cache size."),
24737 &show_dwarf_cmdlist,
24738 0/*allow-unknown*/, &maintenance_show_cmdlist);
24739
24740 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24741 &dwarf_max_cache_age, _("\
24742 Set the upper bound on the age of cached DWARF compilation units."), _("\
24743 Show the upper bound on the age of cached DWARF compilation units."), _("\
24744 A higher limit means that cached compilation units will be stored\n\
24745 in memory longer, and more total memory will be used. Zero disables\n\
24746 caching, which can slow down startup."),
24747 NULL,
24748 show_dwarf_max_cache_age,
24749 &set_dwarf_cmdlist,
24750 &show_dwarf_cmdlist);
24751
24752 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24753 Set debugging of the DWARF reader."), _("\
24754 Show debugging of the DWARF reader."), _("\
24755 When enabled (non-zero), debugging messages are printed during DWARF\n\
24756 reading and symtab expansion. A value of 1 (one) provides basic\n\
24757 information. A value greater than 1 provides more verbose information."),
24758 NULL,
24759 NULL,
24760 &setdebuglist, &showdebuglist);
24761
24762 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24763 Set debugging of the DWARF DIE reader."), _("\
24764 Show debugging of the DWARF DIE reader."), _("\
24765 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24766 The value is the maximum depth to print."),
24767 NULL,
24768 NULL,
24769 &setdebuglist, &showdebuglist);
24770
24771 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24772 Set debugging of the dwarf line reader."), _("\
24773 Show debugging of the dwarf line reader."), _("\
24774 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24775 A value of 1 (one) provides basic information.\n\
24776 A value greater than 1 provides more verbose information."),
24777 NULL,
24778 NULL,
24779 &setdebuglist, &showdebuglist);
24780
24781 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24782 Set cross-checking of \"physname\" code against demangler."), _("\
24783 Show cross-checking of \"physname\" code against demangler."), _("\
24784 When enabled, GDB's internal \"physname\" code is checked against\n\
24785 the demangler."),
24786 NULL, show_check_physname,
24787 &setdebuglist, &showdebuglist);
24788
24789 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24790 no_class, &use_deprecated_index_sections, _("\
24791 Set whether to use deprecated gdb_index sections."), _("\
24792 Show whether to use deprecated gdb_index sections."), _("\
24793 When enabled, deprecated .gdb_index sections are used anyway.\n\
24794 Normally they are ignored either because of a missing feature or\n\
24795 performance issue.\n\
24796 Warning: This option must be enabled before gdb reads the file."),
24797 NULL,
24798 NULL,
24799 &setlist, &showlist);
24800
24801 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24802 &dwarf2_locexpr_funcs);
24803 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24804 &dwarf2_loclist_funcs);
24805
24806 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24807 &dwarf2_block_frame_base_locexpr_funcs);
24808 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24809 &dwarf2_block_frame_base_loclist_funcs);
24810
24811 #if GDB_SELF_TEST
24812 selftests::register_test ("dw2_expand_symtabs_matching",
24813 selftests::dw2_expand_symtabs_matching::run_test);
24814 selftests::register_test ("dwarf2_find_containing_comp_unit",
24815 selftests::find_containing_comp_unit::run_test);
24816 #endif
24817 }