Fix handling of DW_AT_data_bit_offset
[binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91 #include <unordered_set>
92
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
97
98 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
99
100 #define dwarf_read_debug_printf(fmt, ...) \
101 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
102 ##__VA_ARGS__)
103
104 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
105
106 #define dwarf_read_debug_printf_v(fmt, ...) \
107 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
109
110 /* When non-zero, dump DIEs after they are read in. */
111 static unsigned int dwarf_die_debug = 0;
112
113 /* When non-zero, dump line number entries as they are read in. */
114 unsigned int dwarf_line_debug = 0;
115
116 /* When true, cross-check physname against demangler. */
117 static bool check_physname = false;
118
119 /* When true, do not reject deprecated .gdb_index sections. */
120 static bool use_deprecated_index_sections = false;
121
122 /* This is used to store the data that is always per objfile. */
123 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125 /* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
135
136 /* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138 static int dwarf2_locexpr_index;
139 static int dwarf2_loclist_index;
140 static int dwarf2_locexpr_block_index;
141 static int dwarf2_loclist_block_index;
142
143 /* Size of .debug_loclists section header for 32-bit DWARF format. */
144 #define LOCLIST_HEADER_SIZE32 12
145
146 /* Size of .debug_loclists section header for 64-bit DWARF format. */
147 #define LOCLIST_HEADER_SIZE64 20
148
149 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
150 #define RNGLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
153 #define RNGLIST_HEADER_SIZE64 20
154
155 /* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168 struct name_component
169 {
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178 };
179
180 /* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183 struct mapped_index_base
184 {
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
211 void build_name_components (dwarf2_per_objfile *per_objfile);
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
218 find_name_components_bounds (const lookup_name_info &ln_no_params,
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
221
222 /* Prevent deleting/destroying via a base class pointer. */
223 protected:
224 ~mapped_index_base() = default;
225 };
226
227 /* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231 class offset_view
232 {
233 public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262 private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265 };
266
267 /* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
269 struct mapped_index final : public mapped_index_base
270 {
271 /* Index data format version. */
272 int version = 0;
273
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
276
277 /* The symbol table, implemented as a hash table. */
278 offset_view symbol_table;
279
280 /* A pointer to the constant pool. */
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
296
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
301 }
302
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size () / 2; }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
363 };
364
365 /* See dwarf2read.h. */
366
367 dwarf2_per_objfile *
368 get_dwarf2_per_objfile (struct objfile *objfile)
369 {
370 return dwarf2_objfile_data_key.get (objfile);
371 }
372
373 /* Default names of the debugging sections. */
374
375 /* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
378 const struct dwarf2_debug_sections dwarf2_elf_names =
379 {
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
384 { ".debug_loclists", ".zdebug_loclists" },
385 { ".debug_macinfo", ".zdebug_macinfo" },
386 { ".debug_macro", ".zdebug_macro" },
387 { ".debug_str", ".zdebug_str" },
388 { ".debug_str_offsets", ".zdebug_str_offsets" },
389 { ".debug_line_str", ".zdebug_line_str" },
390 { ".debug_ranges", ".zdebug_ranges" },
391 { ".debug_rnglists", ".zdebug_rnglists" },
392 { ".debug_types", ".zdebug_types" },
393 { ".debug_addr", ".zdebug_addr" },
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
396 { ".gdb_index", ".zgdb_index" },
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
399 23
400 };
401
402 /* List of DWO/DWP sections. */
403
404 static const struct dwop_section_names
405 {
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
410 struct dwarf2_section_names loclists_dwo;
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
413 struct dwarf2_section_names rnglists_dwo;
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
419 }
420 dwop_section_names =
421 {
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
435 };
436
437 /* local data types */
438
439 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441 struct loclists_rnglists_header
442 {
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461 };
462
463 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466 struct stmt_list_hash
467 {
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
472 sect_offset line_sect_off;
473 };
474
475 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
479
480 struct type_unit_group : public dwarf2_per_cu_data
481 {
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
485 std::vector<signatured_type *> *tus = nullptr;
486
487 /* The data used to construct the hash key. */
488 struct stmt_list_hash hash {};
489 };
490
491 /* These sections are what may appear in a (real or virtual) DWO file. */
492
493 struct dwo_sections
494 {
495 struct dwarf2_section_info abbrev;
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
498 struct dwarf2_section_info loclists;
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
501 struct dwarf2_section_info rnglists;
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
506 std::vector<dwarf2_section_info> types;
507 };
508
509 /* CUs/TUs in DWP/DWO files. */
510
511 struct dwo_unit
512 {
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
522 struct dwarf2_section_info *section;
523
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530 };
531
532 /* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536 enum dwp_v2_section_ids
537 {
538 DW_SECT_MIN = 1
539 };
540
541 /* Data for one DWO file.
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
551
552 struct dwo_file
553 {
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
561 const char *dwo_name = nullptr;
562
563 /* The DW_AT_comp_dir attribute. */
564 const char *comp_dir = nullptr;
565
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
568 gdb_bfd_ref_ptr dbfd;
569
570 /* The sections that make up this DWO file.
571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
572 sections (for lack of a better name). */
573 struct dwo_sections sections {};
574
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
579 htab_up cus;
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
583 htab_up tus;
584 };
585
586 /* These sections are what may appear in a DWP file. */
587
588 struct dwp_sections
589 {
590 /* These are used by all DWP versions (1, 2 and 5). */
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
594
595 /* These are only used by DWP version 2 and version 5 files.
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
607 struct dwarf2_section_info loclists;
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
610 struct dwarf2_section_info rnglists;
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
613 };
614
615 /* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
617
618 struct virtual_v1_dwo_sections
619 {
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
627 That is recorded here, and copied to dwo_unit.section. */
628 struct dwarf2_section_info info_or_types;
629 };
630
631 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
636 struct virtual_v2_or_v5_dwo_sections
637 {
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666 };
667
668 /* Contents of DWP hash tables. */
669
670 struct dwp_hash_table
671 {
672 uint32_t version, nr_columns;
673 uint32_t nr_units, nr_slots;
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685 #define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700 #define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
712 } section_pool;
713 };
714
715 /* Data for one DWP file. */
716
717 struct dwp_file
718 {
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
725 /* Name of the file. */
726 const char *name;
727
728 /* File format version. */
729 int version = 0;
730
731 /* The bfd. */
732 gdb_bfd_ref_ptr dbfd;
733
734 /* Section info for this file. */
735 struct dwp_sections sections {};
736
737 /* Table of CUs in the file. */
738 const struct dwp_hash_table *cus = nullptr;
739
740 /* Table of TUs in the file. */
741 const struct dwp_hash_table *tus = nullptr;
742
743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
744 htab_up loaded_cus;
745 htab_up loaded_tus;
746
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
751 };
752
753 /* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
756 struct exists to abstract away the constant parameters of die reading. */
757
758 struct die_reader_specs
759 {
760 /* The bfd of die_section. */
761 bfd *abfd;
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
767 struct dwo_file *dwo_file;
768
769 /* The section the die comes from.
770 This is either .debug_info or .debug_types, or the .dwo variants. */
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
774 const gdb_byte *buffer;
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
778
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
781 };
782
783 /* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786 class cutu_reader : public die_reader_specs
787 {
788 public:
789
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
792 struct abbrev_table *abbrev_table,
793 dwarf2_cu *existing_cu,
794 bool skip_partial);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
797 dwarf2_per_objfile *per_objfile,
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
805 bool dummy_p = false;
806
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
811 private:
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
814 dwarf2_cu *existing_cu);
815
816 struct dwarf2_per_cu_data *m_this_cu;
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824 };
825
826 /* When we construct a partial symbol table entry we only
827 need this much information. */
828 struct partial_die_info : public allocate_on_obstack
829 {
830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
835 partial_die_info (const partial_die_info &) = default;
836
837 /* Adjust the partial die before generating a symbol for it. This
838 function may set the is_external flag or change the DIE's
839 name. */
840 void fixup (struct dwarf2_cu *cu);
841
842 /* Read a minimal amount of information into the minimal die
843 structure. */
844 const gdb_byte *read (const struct die_reader_specs *reader,
845 const struct abbrev_info &abbrev,
846 const gdb_byte *info_ptr);
847
848 /* Compute the name of this partial DIE. This memoizes the
849 result, so it is safe to call multiple times. */
850 const char *name (dwarf2_cu *cu);
851
852 /* Offset of this DIE. */
853 const sect_offset sect_off;
854
855 /* DWARF-2 tag for this DIE. */
856 const ENUM_BITFIELD(dwarf_tag) tag : 16;
857
858 /* Assorted flags describing the data found in this DIE. */
859 const unsigned int has_children : 1;
860
861 unsigned int is_external : 1;
862 unsigned int is_declaration : 1;
863 unsigned int has_type : 1;
864 unsigned int has_specification : 1;
865 unsigned int has_pc_info : 1;
866 unsigned int has_range_info : 1;
867 unsigned int may_be_inlined : 1;
868
869 /* This DIE has been marked DW_AT_main_subprogram. */
870 unsigned int main_subprogram : 1;
871
872 /* Flag set if the SCOPE field of this structure has been
873 computed. */
874 unsigned int scope_set : 1;
875
876 /* Flag set if the DIE has a byte_size attribute. */
877 unsigned int has_byte_size : 1;
878
879 /* Flag set if the DIE has a DW_AT_const_value attribute. */
880 unsigned int has_const_value : 1;
881
882 /* Flag set if any of the DIE's children are template arguments. */
883 unsigned int has_template_arguments : 1;
884
885 /* Flag set if fixup has been called on this die. */
886 unsigned int fixup_called : 1;
887
888 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
889 unsigned int is_dwz : 1;
890
891 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
892 unsigned int spec_is_dwz : 1;
893
894 unsigned int canonical_name : 1;
895
896 /* The name of this DIE. Normally the value of DW_AT_name, but
897 sometimes a default name for unnamed DIEs. */
898 const char *raw_name = nullptr;
899
900 /* The linkage name, if present. */
901 const char *linkage_name = nullptr;
902
903 /* The scope to prepend to our children. This is generally
904 allocated on the comp_unit_obstack, so will disappear
905 when this compilation unit leaves the cache. */
906 const char *scope = nullptr;
907
908 /* Some data associated with the partial DIE. The tag determines
909 which field is live. */
910 union
911 {
912 /* The location description associated with this DIE, if any. */
913 struct dwarf_block *locdesc;
914 /* The offset of an import, for DW_TAG_imported_unit. */
915 sect_offset sect_off;
916 } d {};
917
918 union
919 {
920 /* If HAS_PC_INFO, the PC range associated with this DIE. */
921 struct
922 {
923 CORE_ADDR lowpc = 0;
924 CORE_ADDR highpc = 0;
925 };
926 /* If HAS_RANGE_INFO, the ranges offset associated with this DIE. */
927 ULONGEST ranges_offset;
928 };
929
930 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
931 DW_AT_sibling, if any. */
932 /* NOTE: This member isn't strictly necessary, partial_die_info::read
933 could return DW_AT_sibling values to its caller load_partial_dies. */
934 const gdb_byte *sibling = nullptr;
935
936 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
937 DW_AT_specification (or DW_AT_abstract_origin or
938 DW_AT_extension). */
939 sect_offset spec_offset {};
940
941 /* Pointers to this DIE's parent, first child, and next sibling,
942 if any. */
943 struct partial_die_info *die_parent = nullptr;
944 struct partial_die_info *die_child = nullptr;
945 struct partial_die_info *die_sibling = nullptr;
946
947 friend struct partial_die_info *
948 dwarf2_cu::find_partial_die (sect_offset sect_off);
949
950 private:
951 /* Only need to do look up in dwarf2_cu::find_partial_die. */
952 partial_die_info (sect_offset sect_off)
953 : partial_die_info (sect_off, DW_TAG_padding, 0)
954 {
955 }
956
957 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
958 int has_children_)
959 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
960 {
961 is_external = 0;
962 is_declaration = 0;
963 has_type = 0;
964 has_specification = 0;
965 has_pc_info = 0;
966 has_range_info = 0;
967 may_be_inlined = 0;
968 main_subprogram = 0;
969 scope_set = 0;
970 has_byte_size = 0;
971 has_const_value = 0;
972 has_template_arguments = 0;
973 fixup_called = 0;
974 is_dwz = 0;
975 spec_is_dwz = 0;
976 canonical_name = 0;
977 }
978 };
979
980 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
981 but this would require a corresponding change in unpack_field_as_long
982 and friends. */
983 static int bits_per_byte = 8;
984
985 struct variant_part_builder;
986
987 /* When reading a variant, we track a bit more information about the
988 field, and store it in an object of this type. */
989
990 struct variant_field
991 {
992 int first_field = -1;
993 int last_field = -1;
994
995 /* A variant can contain other variant parts. */
996 std::vector<variant_part_builder> variant_parts;
997
998 /* If we see a DW_TAG_variant, then this will be set if this is the
999 default branch. */
1000 bool default_branch = false;
1001 /* If we see a DW_AT_discr_value, then this will be the discriminant
1002 value. */
1003 ULONGEST discriminant_value = 0;
1004 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1005 data. */
1006 struct dwarf_block *discr_list_data = nullptr;
1007 };
1008
1009 /* This represents a DW_TAG_variant_part. */
1010
1011 struct variant_part_builder
1012 {
1013 /* The offset of the discriminant field. */
1014 sect_offset discriminant_offset {};
1015
1016 /* Variants that are direct children of this variant part. */
1017 std::vector<variant_field> variants;
1018
1019 /* True if we're currently reading a variant. */
1020 bool processing_variant = false;
1021 };
1022
1023 struct nextfield
1024 {
1025 int accessibility = 0;
1026 int virtuality = 0;
1027 /* Variant parts need to find the discriminant, which is a DIE
1028 reference. We track the section offset of each field to make
1029 this link. */
1030 sect_offset offset;
1031 struct field field {};
1032 };
1033
1034 struct fnfieldlist
1035 {
1036 const char *name = nullptr;
1037 std::vector<struct fn_field> fnfields;
1038 };
1039
1040 /* The routines that read and process dies for a C struct or C++ class
1041 pass lists of data member fields and lists of member function fields
1042 in an instance of a field_info structure, as defined below. */
1043 struct field_info
1044 {
1045 /* List of data member and baseclasses fields. */
1046 std::vector<struct nextfield> fields;
1047 std::vector<struct nextfield> baseclasses;
1048
1049 /* Set if the accessibility of one of the fields is not public. */
1050 bool non_public_fields = false;
1051
1052 /* Member function fieldlist array, contains name of possibly overloaded
1053 member function, number of overloaded member functions and a pointer
1054 to the head of the member function field chain. */
1055 std::vector<struct fnfieldlist> fnfieldlists;
1056
1057 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1058 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1059 std::vector<struct decl_field> typedef_field_list;
1060
1061 /* Nested types defined by this class and the number of elements in this
1062 list. */
1063 std::vector<struct decl_field> nested_types_list;
1064
1065 /* If non-null, this is the variant part we are currently
1066 reading. */
1067 variant_part_builder *current_variant_part = nullptr;
1068 /* This holds all the top-level variant parts attached to the type
1069 we're reading. */
1070 std::vector<variant_part_builder> variant_parts;
1071
1072 /* Return the total number of fields (including baseclasses). */
1073 int nfields () const
1074 {
1075 return fields.size () + baseclasses.size ();
1076 }
1077 };
1078
1079 /* Loaded secondary compilation units are kept in memory until they
1080 have not been referenced for the processing of this many
1081 compilation units. Set this to zero to disable caching. Cache
1082 sizes of up to at least twenty will improve startup time for
1083 typical inter-CU-reference binaries, at an obvious memory cost. */
1084 static int dwarf_max_cache_age = 5;
1085 static void
1086 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1087 struct cmd_list_element *c, const char *value)
1088 {
1089 fprintf_filtered (file, _("The upper bound on the age of cached "
1090 "DWARF compilation units is %s.\n"),
1091 value);
1092 }
1093 \f
1094 /* local function prototypes */
1095
1096 static void dwarf2_find_base_address (struct die_info *die,
1097 struct dwarf2_cu *cu);
1098
1099 static dwarf2_psymtab *create_partial_symtab
1100 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1101 const char *name);
1102
1103 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1104 const gdb_byte *info_ptr,
1105 struct die_info *type_unit_die);
1106
1107 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1108
1109 static void scan_partial_symbols (struct partial_die_info *,
1110 CORE_ADDR *, CORE_ADDR *,
1111 int, struct dwarf2_cu *);
1112
1113 static void add_partial_symbol (struct partial_die_info *,
1114 struct dwarf2_cu *);
1115
1116 static void add_partial_namespace (struct partial_die_info *pdi,
1117 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1118 int set_addrmap, struct dwarf2_cu *cu);
1119
1120 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1121 CORE_ADDR *highpc, int set_addrmap,
1122 struct dwarf2_cu *cu);
1123
1124 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1125 struct dwarf2_cu *cu);
1126
1127 static void add_partial_subprogram (struct partial_die_info *pdi,
1128 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1129 int need_pc, struct dwarf2_cu *cu);
1130
1131 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1132
1133 static struct partial_die_info *load_partial_dies
1134 (const struct die_reader_specs *, const gdb_byte *, int);
1135
1136 /* A pair of partial_die_info and compilation unit. */
1137 struct cu_partial_die_info
1138 {
1139 /* The compilation unit of the partial_die_info. */
1140 struct dwarf2_cu *cu;
1141 /* A partial_die_info. */
1142 struct partial_die_info *pdi;
1143
1144 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1145 : cu (cu),
1146 pdi (pdi)
1147 { /* Nothing. */ }
1148
1149 private:
1150 cu_partial_die_info () = delete;
1151 };
1152
1153 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1154 struct dwarf2_cu *);
1155
1156 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1157 struct attribute *,
1158 const struct attr_abbrev *,
1159 const gdb_byte *);
1160
1161 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1162 struct attribute *attr, dwarf_tag tag);
1163
1164 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1165
1166 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1167 dwarf2_section_info *, sect_offset);
1168
1169 static const char *read_indirect_string
1170 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1171 const struct comp_unit_head *, unsigned int *);
1172
1173 static const char *read_indirect_string_at_offset
1174 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1175
1176 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1177 const gdb_byte *,
1178 unsigned int *);
1179
1180 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1181 ULONGEST str_index);
1182
1183 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1184 ULONGEST str_index);
1185
1186 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1187 struct dwarf2_cu *);
1188
1189 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1190 struct dwarf2_cu *cu);
1191
1192 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1193
1194 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1195 struct dwarf2_cu *cu);
1196
1197 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1198
1199 static struct die_info *die_specification (struct die_info *die,
1200 struct dwarf2_cu **);
1201
1202 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1203 struct dwarf2_cu *cu);
1204
1205 struct file_and_directory;
1206 static void dwarf_decode_lines (struct line_header *,
1207 const file_and_directory &,
1208 struct dwarf2_cu *, dwarf2_psymtab *,
1209 CORE_ADDR, int decode_mapping);
1210
1211 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1212 const char *);
1213
1214 static struct symbol *new_symbol (struct die_info *, struct type *,
1215 struct dwarf2_cu *, struct symbol * = NULL);
1216
1217 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1218 struct dwarf2_cu *);
1219
1220 static void dwarf2_const_value_attr (const struct attribute *attr,
1221 struct type *type,
1222 const char *name,
1223 struct obstack *obstack,
1224 struct dwarf2_cu *cu, LONGEST *value,
1225 const gdb_byte **bytes,
1226 struct dwarf2_locexpr_baton **baton);
1227
1228 static struct type *read_subrange_index_type (struct die_info *die,
1229 struct dwarf2_cu *cu);
1230
1231 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1232
1233 static int need_gnat_info (struct dwarf2_cu *);
1234
1235 static struct type *die_descriptive_type (struct die_info *,
1236 struct dwarf2_cu *);
1237
1238 static void set_descriptive_type (struct type *, struct die_info *,
1239 struct dwarf2_cu *);
1240
1241 static struct type *die_containing_type (struct die_info *,
1242 struct dwarf2_cu *);
1243
1244 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1245 struct dwarf2_cu *);
1246
1247 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1248
1249 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1250
1251 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1252
1253 static char *typename_concat (struct obstack *obs, const char *prefix,
1254 const char *suffix, int physname,
1255 struct dwarf2_cu *cu);
1256
1257 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1258
1259 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1260
1261 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1262
1263 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1264
1265 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1266
1267 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1268
1269 /* Return the .debug_loclists section to use for cu. */
1270 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1271
1272 /* Return the .debug_rnglists section to use for cu. */
1273 static struct dwarf2_section_info *cu_debug_rnglists_section
1274 (struct dwarf2_cu *cu, dwarf_tag tag);
1275
1276 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1277 values. Keep the items ordered with increasing constraints compliance. */
1278 enum pc_bounds_kind
1279 {
1280 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1281 PC_BOUNDS_NOT_PRESENT,
1282
1283 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1284 were present but they do not form a valid range of PC addresses. */
1285 PC_BOUNDS_INVALID,
1286
1287 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1288 PC_BOUNDS_RANGES,
1289
1290 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1291 PC_BOUNDS_HIGH_LOW,
1292 };
1293
1294 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1295 CORE_ADDR *, CORE_ADDR *,
1296 struct dwarf2_cu *,
1297 dwarf2_psymtab *);
1298
1299 static void get_scope_pc_bounds (struct die_info *,
1300 CORE_ADDR *, CORE_ADDR *,
1301 struct dwarf2_cu *);
1302
1303 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1304 CORE_ADDR, struct dwarf2_cu *);
1305
1306 static void dwarf2_add_field (struct field_info *, struct die_info *,
1307 struct dwarf2_cu *);
1308
1309 static void dwarf2_attach_fields_to_type (struct field_info *,
1310 struct type *, struct dwarf2_cu *);
1311
1312 static void dwarf2_add_member_fn (struct field_info *,
1313 struct die_info *, struct type *,
1314 struct dwarf2_cu *);
1315
1316 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1317 struct type *,
1318 struct dwarf2_cu *);
1319
1320 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1321
1322 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1323
1324 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1325
1326 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1327
1328 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1329
1330 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1331
1332 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1333
1334 static struct type *read_module_type (struct die_info *die,
1335 struct dwarf2_cu *cu);
1336
1337 static const char *namespace_name (struct die_info *die,
1338 int *is_anonymous, struct dwarf2_cu *);
1339
1340 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1341
1342 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1343 bool * = nullptr);
1344
1345 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1346 struct dwarf2_cu *);
1347
1348 static struct die_info *read_die_and_siblings_1
1349 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1350 struct die_info *);
1351
1352 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1353 const gdb_byte *info_ptr,
1354 const gdb_byte **new_info_ptr,
1355 struct die_info *parent);
1356
1357 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1358 struct die_info **, const gdb_byte *,
1359 int);
1360
1361 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1362 struct die_info **, const gdb_byte *);
1363
1364 static void process_die (struct die_info *, struct dwarf2_cu *);
1365
1366 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1367 struct objfile *);
1368
1369 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1370
1371 static const char *dwarf2_full_name (const char *name,
1372 struct die_info *die,
1373 struct dwarf2_cu *cu);
1374
1375 static const char *dwarf2_physname (const char *name, struct die_info *die,
1376 struct dwarf2_cu *cu);
1377
1378 static struct die_info *dwarf2_extension (struct die_info *die,
1379 struct dwarf2_cu **);
1380
1381 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1382
1383 static void dump_die_for_error (struct die_info *);
1384
1385 static void dump_die_1 (struct ui_file *, int level, int max_level,
1386 struct die_info *);
1387
1388 /*static*/ void dump_die (struct die_info *, int max_level);
1389
1390 static void store_in_ref_table (struct die_info *,
1391 struct dwarf2_cu *);
1392
1393 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1394 const struct attribute *,
1395 struct dwarf2_cu **);
1396
1397 static struct die_info *follow_die_ref (struct die_info *,
1398 const struct attribute *,
1399 struct dwarf2_cu **);
1400
1401 static struct die_info *follow_die_sig (struct die_info *,
1402 const struct attribute *,
1403 struct dwarf2_cu **);
1404
1405 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1406 struct dwarf2_cu *);
1407
1408 static struct type *get_DW_AT_signature_type (struct die_info *,
1409 const struct attribute *,
1410 struct dwarf2_cu *);
1411
1412 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1413 dwarf2_per_objfile *per_objfile);
1414
1415 static void read_signatured_type (signatured_type *sig_type,
1416 dwarf2_per_objfile *per_objfile);
1417
1418 static int attr_to_dynamic_prop (const struct attribute *attr,
1419 struct die_info *die, struct dwarf2_cu *cu,
1420 struct dynamic_prop *prop, struct type *type);
1421
1422 /* memory allocation interface */
1423
1424 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1425
1426 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1427
1428 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1429
1430 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1431 struct dwarf2_loclist_baton *baton,
1432 const struct attribute *attr);
1433
1434 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1435 struct symbol *sym,
1436 struct dwarf2_cu *cu,
1437 int is_block);
1438
1439 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1440 const gdb_byte *info_ptr,
1441 const struct abbrev_info *abbrev);
1442
1443 static hashval_t partial_die_hash (const void *item);
1444
1445 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1446
1447 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1448 (sect_offset sect_off, unsigned int offset_in_dwz,
1449 dwarf2_per_objfile *per_objfile);
1450
1451 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1452 struct die_info *comp_unit_die,
1453 enum language pretend_language);
1454
1455 static struct type *set_die_type (struct die_info *, struct type *,
1456 struct dwarf2_cu *, bool = false);
1457
1458 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1459
1460 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1461 dwarf2_per_objfile *per_objfile,
1462 dwarf2_cu *existing_cu,
1463 bool skip_partial,
1464 enum language pretend_language);
1465
1466 static void process_full_comp_unit (dwarf2_cu *cu,
1467 enum language pretend_language);
1468
1469 static void process_full_type_unit (dwarf2_cu *cu,
1470 enum language pretend_language);
1471
1472 static struct type *get_die_type_at_offset (sect_offset,
1473 dwarf2_per_cu_data *per_cu,
1474 dwarf2_per_objfile *per_objfile);
1475
1476 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1477
1478 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1479 dwarf2_per_objfile *per_objfile,
1480 enum language pretend_language);
1481
1482 static void process_queue (dwarf2_per_objfile *per_objfile);
1483
1484 /* Class, the destructor of which frees all allocated queue entries. This
1485 will only have work to do if an error was thrown while processing the
1486 dwarf. If no error was thrown then the queue entries should have all
1487 been processed, and freed, as we went along. */
1488
1489 class dwarf2_queue_guard
1490 {
1491 public:
1492 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1493 : m_per_objfile (per_objfile)
1494 {
1495 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1496
1497 m_per_objfile->per_bfd->queue.emplace ();
1498 }
1499
1500 /* Free any entries remaining on the queue. There should only be
1501 entries left if we hit an error while processing the dwarf. */
1502 ~dwarf2_queue_guard ()
1503 {
1504 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1505
1506 m_per_objfile->per_bfd->queue.reset ();
1507 }
1508
1509 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1510
1511 private:
1512 dwarf2_per_objfile *m_per_objfile;
1513 };
1514
1515 dwarf2_queue_item::~dwarf2_queue_item ()
1516 {
1517 /* Anything still marked queued is likely to be in an
1518 inconsistent state, so discard it. */
1519 if (per_cu->queued)
1520 {
1521 per_objfile->remove_cu (per_cu);
1522 per_cu->queued = 0;
1523 }
1524 }
1525
1526 /* See dwarf2/read.h. */
1527
1528 void
1529 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1530 {
1531 if (data->is_debug_types)
1532 delete static_cast<signatured_type *> (data);
1533 else
1534 delete data;
1535 }
1536
1537 /* The return type of find_file_and_directory. Note, the enclosed
1538 string pointers are only valid while this object is valid. */
1539
1540 struct file_and_directory
1541 {
1542 /* The filename. This is never NULL. */
1543 const char *name;
1544
1545 /* The compilation directory. NULL if not known. If we needed to
1546 compute a new string, it will be stored in the per-BFD string
1547 bcache; otherwise, points directly to the DW_AT_comp_dir string
1548 attribute owned by the obstack that owns the DIE. */
1549 const char *comp_dir;
1550 };
1551
1552 static file_and_directory find_file_and_directory (struct die_info *die,
1553 struct dwarf2_cu *cu);
1554
1555 static const char *compute_include_file_name
1556 (const struct line_header *lh,
1557 const file_entry &fe,
1558 const file_and_directory &cu_info,
1559 gdb::unique_xmalloc_ptr<char> *name_holder);
1560
1561 static htab_up allocate_signatured_type_table ();
1562
1563 static htab_up allocate_dwo_unit_table ();
1564
1565 static struct dwo_unit *lookup_dwo_unit_in_dwp
1566 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1567 const char *comp_dir, ULONGEST signature, int is_debug_types);
1568
1569 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1570
1571 static struct dwo_unit *lookup_dwo_comp_unit
1572 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1573 ULONGEST signature);
1574
1575 static struct dwo_unit *lookup_dwo_type_unit
1576 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1577
1578 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1579
1580 /* A unique pointer to a dwo_file. */
1581
1582 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1583
1584 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1585
1586 static void check_producer (struct dwarf2_cu *cu);
1587 \f
1588 /* Various complaints about symbol reading that don't abort the process. */
1589
1590 static void
1591 dwarf2_debug_line_missing_file_complaint (void)
1592 {
1593 complaint (_(".debug_line section has line data without a file"));
1594 }
1595
1596 static void
1597 dwarf2_debug_line_missing_end_sequence_complaint (void)
1598 {
1599 complaint (_(".debug_line section has line "
1600 "program sequence without an end"));
1601 }
1602
1603 static void
1604 dwarf2_complex_location_expr_complaint (void)
1605 {
1606 complaint (_("location expression too complex"));
1607 }
1608
1609 static void
1610 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1611 int arg3)
1612 {
1613 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1614 arg1, arg2, arg3);
1615 }
1616
1617 static void
1618 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1619 {
1620 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1621 arg1, arg2);
1622 }
1623
1624 /* Hash function for line_header_hash. */
1625
1626 static hashval_t
1627 line_header_hash (const struct line_header *ofs)
1628 {
1629 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1630 }
1631
1632 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1633
1634 static hashval_t
1635 line_header_hash_voidp (const void *item)
1636 {
1637 const struct line_header *ofs = (const struct line_header *) item;
1638
1639 return line_header_hash (ofs);
1640 }
1641
1642 /* Equality function for line_header_hash. */
1643
1644 static int
1645 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1646 {
1647 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1648 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1649
1650 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1651 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1652 }
1653
1654 \f
1655
1656 /* An iterator for all_comp_units that is based on index. This
1657 approach makes it possible to iterate over all_comp_units safely,
1658 when some caller in the loop may add new units. */
1659
1660 class all_comp_units_iterator
1661 {
1662 public:
1663
1664 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1665 : m_per_bfd (per_bfd),
1666 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1667 {
1668 }
1669
1670 all_comp_units_iterator &operator++ ()
1671 {
1672 ++m_index;
1673 return *this;
1674 }
1675
1676 dwarf2_per_cu_data *operator* () const
1677 {
1678 return m_per_bfd->get_cu (m_index);
1679 }
1680
1681 bool operator== (const all_comp_units_iterator &other) const
1682 {
1683 return m_index == other.m_index;
1684 }
1685
1686
1687 bool operator!= (const all_comp_units_iterator &other) const
1688 {
1689 return m_index != other.m_index;
1690 }
1691
1692 private:
1693
1694 dwarf2_per_bfd *m_per_bfd;
1695 size_t m_index;
1696 };
1697
1698 /* A range adapter for the all_comp_units_iterator. */
1699 class all_comp_units_range
1700 {
1701 public:
1702
1703 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1704 : m_per_bfd (per_bfd)
1705 {
1706 }
1707
1708 all_comp_units_iterator begin ()
1709 {
1710 return all_comp_units_iterator (m_per_bfd, true);
1711 }
1712
1713 all_comp_units_iterator end ()
1714 {
1715 return all_comp_units_iterator (m_per_bfd, false);
1716 }
1717
1718 private:
1719
1720 dwarf2_per_bfd *m_per_bfd;
1721 };
1722
1723 /* See declaration. */
1724
1725 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1726 bool can_copy_)
1727 : obfd (obfd),
1728 can_copy (can_copy_)
1729 {
1730 if (names == NULL)
1731 names = &dwarf2_elf_names;
1732
1733 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1734 locate_sections (obfd, sec, *names);
1735 }
1736
1737 dwarf2_per_bfd::~dwarf2_per_bfd ()
1738 {
1739 for (auto &per_cu : all_comp_units)
1740 {
1741 per_cu->imported_symtabs_free ();
1742 per_cu->free_cached_file_names ();
1743 }
1744
1745 /* Everything else should be on this->obstack. */
1746 }
1747
1748 /* See read.h. */
1749
1750 void
1751 dwarf2_per_objfile::remove_all_cus ()
1752 {
1753 gdb_assert (!this->per_bfd->queue.has_value ());
1754
1755 for (auto pair : m_dwarf2_cus)
1756 delete pair.second;
1757
1758 m_dwarf2_cus.clear ();
1759 }
1760
1761 /* A helper class that calls free_cached_comp_units on
1762 destruction. */
1763
1764 class free_cached_comp_units
1765 {
1766 public:
1767
1768 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1769 : m_per_objfile (per_objfile)
1770 {
1771 }
1772
1773 ~free_cached_comp_units ()
1774 {
1775 m_per_objfile->remove_all_cus ();
1776 }
1777
1778 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1779
1780 private:
1781
1782 dwarf2_per_objfile *m_per_objfile;
1783 };
1784
1785 /* See read.h. */
1786
1787 bool
1788 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1789 {
1790 if (per_cu->index < this->m_symtabs.size ())
1791 return this->m_symtabs[per_cu->index] != nullptr;
1792 return false;
1793 }
1794
1795 /* See read.h. */
1796
1797 compunit_symtab *
1798 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1799 {
1800 if (per_cu->index < this->m_symtabs.size ())
1801 return this->m_symtabs[per_cu->index];
1802 return nullptr;
1803 }
1804
1805 /* See read.h. */
1806
1807 void
1808 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1809 compunit_symtab *symtab)
1810 {
1811 if (per_cu->index >= this->m_symtabs.size ())
1812 this->m_symtabs.resize (per_cu->index + 1);
1813 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1814 this->m_symtabs[per_cu->index] = symtab;
1815 }
1816
1817 /* Try to locate the sections we need for DWARF 2 debugging
1818 information and return true if we have enough to do something.
1819 NAMES points to the dwarf2 section names, or is NULL if the standard
1820 ELF names are used. CAN_COPY is true for formats where symbol
1821 interposition is possible and so symbol values must follow copy
1822 relocation rules. */
1823
1824 int
1825 dwarf2_has_info (struct objfile *objfile,
1826 const struct dwarf2_debug_sections *names,
1827 bool can_copy)
1828 {
1829 if (objfile->flags & OBJF_READNEVER)
1830 return 0;
1831
1832 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1833
1834 if (per_objfile == NULL)
1835 {
1836 dwarf2_per_bfd *per_bfd;
1837
1838 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1839 BFD doesn't require relocations.
1840
1841 We don't share with objfiles for which -readnow was requested,
1842 because it would complicate things when loading the same BFD with
1843 -readnow and then without -readnow. */
1844 if (!gdb_bfd_requires_relocations (objfile->obfd)
1845 && (objfile->flags & OBJF_READNOW) == 0)
1846 {
1847 /* See if one has been created for this BFD yet. */
1848 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1849
1850 if (per_bfd == nullptr)
1851 {
1852 /* No, create it now. */
1853 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1854 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1855 }
1856 }
1857 else
1858 {
1859 /* No sharing possible, create one specifically for this objfile. */
1860 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1861 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1862 }
1863
1864 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1865 }
1866
1867 return (!per_objfile->per_bfd->info.is_virtual
1868 && per_objfile->per_bfd->info.s.section != NULL
1869 && !per_objfile->per_bfd->abbrev.is_virtual
1870 && per_objfile->per_bfd->abbrev.s.section != NULL);
1871 }
1872
1873 /* See declaration. */
1874
1875 void
1876 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1877 const dwarf2_debug_sections &names)
1878 {
1879 flagword aflag = bfd_section_flags (sectp);
1880
1881 if ((aflag & SEC_HAS_CONTENTS) == 0)
1882 {
1883 }
1884 else if (elf_section_data (sectp)->this_hdr.sh_size
1885 > bfd_get_file_size (abfd))
1886 {
1887 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1888 warning (_("Discarding section %s which has a section size (%s"
1889 ") larger than the file size [in module %s]"),
1890 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1891 bfd_get_filename (abfd));
1892 }
1893 else if (names.info.matches (sectp->name))
1894 {
1895 this->info.s.section = sectp;
1896 this->info.size = bfd_section_size (sectp);
1897 }
1898 else if (names.abbrev.matches (sectp->name))
1899 {
1900 this->abbrev.s.section = sectp;
1901 this->abbrev.size = bfd_section_size (sectp);
1902 }
1903 else if (names.line.matches (sectp->name))
1904 {
1905 this->line.s.section = sectp;
1906 this->line.size = bfd_section_size (sectp);
1907 }
1908 else if (names.loc.matches (sectp->name))
1909 {
1910 this->loc.s.section = sectp;
1911 this->loc.size = bfd_section_size (sectp);
1912 }
1913 else if (names.loclists.matches (sectp->name))
1914 {
1915 this->loclists.s.section = sectp;
1916 this->loclists.size = bfd_section_size (sectp);
1917 }
1918 else if (names.macinfo.matches (sectp->name))
1919 {
1920 this->macinfo.s.section = sectp;
1921 this->macinfo.size = bfd_section_size (sectp);
1922 }
1923 else if (names.macro.matches (sectp->name))
1924 {
1925 this->macro.s.section = sectp;
1926 this->macro.size = bfd_section_size (sectp);
1927 }
1928 else if (names.str.matches (sectp->name))
1929 {
1930 this->str.s.section = sectp;
1931 this->str.size = bfd_section_size (sectp);
1932 }
1933 else if (names.str_offsets.matches (sectp->name))
1934 {
1935 this->str_offsets.s.section = sectp;
1936 this->str_offsets.size = bfd_section_size (sectp);
1937 }
1938 else if (names.line_str.matches (sectp->name))
1939 {
1940 this->line_str.s.section = sectp;
1941 this->line_str.size = bfd_section_size (sectp);
1942 }
1943 else if (names.addr.matches (sectp->name))
1944 {
1945 this->addr.s.section = sectp;
1946 this->addr.size = bfd_section_size (sectp);
1947 }
1948 else if (names.frame.matches (sectp->name))
1949 {
1950 this->frame.s.section = sectp;
1951 this->frame.size = bfd_section_size (sectp);
1952 }
1953 else if (names.eh_frame.matches (sectp->name))
1954 {
1955 this->eh_frame.s.section = sectp;
1956 this->eh_frame.size = bfd_section_size (sectp);
1957 }
1958 else if (names.ranges.matches (sectp->name))
1959 {
1960 this->ranges.s.section = sectp;
1961 this->ranges.size = bfd_section_size (sectp);
1962 }
1963 else if (names.rnglists.matches (sectp->name))
1964 {
1965 this->rnglists.s.section = sectp;
1966 this->rnglists.size = bfd_section_size (sectp);
1967 }
1968 else if (names.types.matches (sectp->name))
1969 {
1970 struct dwarf2_section_info type_section;
1971
1972 memset (&type_section, 0, sizeof (type_section));
1973 type_section.s.section = sectp;
1974 type_section.size = bfd_section_size (sectp);
1975
1976 this->types.push_back (type_section);
1977 }
1978 else if (names.gdb_index.matches (sectp->name))
1979 {
1980 this->gdb_index.s.section = sectp;
1981 this->gdb_index.size = bfd_section_size (sectp);
1982 }
1983 else if (names.debug_names.matches (sectp->name))
1984 {
1985 this->debug_names.s.section = sectp;
1986 this->debug_names.size = bfd_section_size (sectp);
1987 }
1988 else if (names.debug_aranges.matches (sectp->name))
1989 {
1990 this->debug_aranges.s.section = sectp;
1991 this->debug_aranges.size = bfd_section_size (sectp);
1992 }
1993
1994 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1995 && bfd_section_vma (sectp) == 0)
1996 this->has_section_at_zero = true;
1997 }
1998
1999 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2000 SECTION_NAME. */
2001
2002 void
2003 dwarf2_get_section_info (struct objfile *objfile,
2004 enum dwarf2_section_enum sect,
2005 asection **sectp, const gdb_byte **bufp,
2006 bfd_size_type *sizep)
2007 {
2008 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2009 struct dwarf2_section_info *info;
2010
2011 /* We may see an objfile without any DWARF, in which case we just
2012 return nothing. */
2013 if (per_objfile == NULL)
2014 {
2015 *sectp = NULL;
2016 *bufp = NULL;
2017 *sizep = 0;
2018 return;
2019 }
2020 switch (sect)
2021 {
2022 case DWARF2_DEBUG_FRAME:
2023 info = &per_objfile->per_bfd->frame;
2024 break;
2025 case DWARF2_EH_FRAME:
2026 info = &per_objfile->per_bfd->eh_frame;
2027 break;
2028 default:
2029 gdb_assert_not_reached ("unexpected section");
2030 }
2031
2032 info->read (objfile);
2033
2034 *sectp = info->get_bfd_section ();
2035 *bufp = info->buffer;
2036 *sizep = info->size;
2037 }
2038
2039 \f
2040 /* DWARF quick_symbol_functions support. */
2041
2042 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2043 unique line tables, so we maintain a separate table of all .debug_line
2044 derived entries to support the sharing.
2045 All the quick functions need is the list of file names. We discard the
2046 line_header when we're done and don't need to record it here. */
2047 struct quick_file_names
2048 {
2049 /* The data used to construct the hash key. */
2050 struct stmt_list_hash hash;
2051
2052 /* The number of entries in file_names, real_names. */
2053 unsigned int num_file_names;
2054
2055 /* The CU directory, as given by DW_AT_comp_dir. May be
2056 nullptr. */
2057 const char *comp_dir;
2058
2059 /* The file names from the line table, after being run through
2060 file_full_name. */
2061 const char **file_names;
2062
2063 /* The file names from the line table after being run through
2064 gdb_realpath. These are computed lazily. */
2065 const char **real_names;
2066 };
2067
2068 /* When using the index (and thus not using psymtabs), each CU has an
2069 object of this type. This is used to hold information needed by
2070 the various "quick" methods. */
2071 struct dwarf2_per_cu_quick_data
2072 {
2073 /* The file table. This can be NULL if there was no file table
2074 or it's currently not read in.
2075 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2076 struct quick_file_names *file_names;
2077
2078 /* A temporary mark bit used when iterating over all CUs in
2079 expand_symtabs_matching. */
2080 unsigned int mark : 1;
2081
2082 /* True if we've tried to read the file table. There will be no
2083 point in trying to read it again next time. */
2084 bool files_read : 1;
2085 };
2086
2087 /* A subclass of psymbol_functions that arranges to read the DWARF
2088 partial symbols when needed. */
2089 struct lazy_dwarf_reader : public psymbol_functions
2090 {
2091 using psymbol_functions::psymbol_functions;
2092
2093 bool can_lazily_read_symbols () override
2094 {
2095 return true;
2096 }
2097
2098 void read_partial_symbols (struct objfile *objfile) override
2099 {
2100 if (dwarf2_has_info (objfile, nullptr))
2101 dwarf2_build_psymtabs (objfile, this);
2102 }
2103 };
2104
2105 static quick_symbol_functions_up
2106 make_lazy_dwarf_reader ()
2107 {
2108 return quick_symbol_functions_up (new lazy_dwarf_reader);
2109 }
2110
2111 struct dwarf2_base_index_functions : public quick_symbol_functions
2112 {
2113 bool has_symbols (struct objfile *objfile) override;
2114
2115 bool has_unexpanded_symtabs (struct objfile *objfile) override;
2116
2117 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2118
2119 void forget_cached_source_info (struct objfile *objfile) override;
2120
2121 enum language lookup_global_symbol_language (struct objfile *objfile,
2122 const char *name,
2123 domain_enum domain,
2124 bool *symbol_found_p) override
2125 {
2126 *symbol_found_p = false;
2127 return language_unknown;
2128 }
2129
2130 void print_stats (struct objfile *objfile, bool print_bcache) override;
2131
2132 void expand_all_symtabs (struct objfile *objfile) override;
2133
2134 struct compunit_symtab *find_pc_sect_compunit_symtab
2135 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2136 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2137
2138 struct compunit_symtab *find_compunit_symtab_by_address
2139 (struct objfile *objfile, CORE_ADDR address) override
2140 {
2141 return nullptr;
2142 }
2143
2144 void map_symbol_filenames (struct objfile *objfile,
2145 gdb::function_view<symbol_filename_ftype> fun,
2146 bool need_fullname) override;
2147 };
2148
2149 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2150 {
2151 void dump (struct objfile *objfile) override;
2152
2153 void expand_matching_symbols
2154 (struct objfile *,
2155 const lookup_name_info &lookup_name,
2156 domain_enum domain,
2157 int global,
2158 symbol_compare_ftype *ordered_compare) override;
2159
2160 bool expand_symtabs_matching
2161 (struct objfile *objfile,
2162 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2163 const lookup_name_info *lookup_name,
2164 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2165 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2166 block_search_flags search_flags,
2167 domain_enum domain,
2168 enum search_domain kind) override;
2169 };
2170
2171 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2172 {
2173 void dump (struct objfile *objfile) override;
2174
2175 void expand_matching_symbols
2176 (struct objfile *,
2177 const lookup_name_info &lookup_name,
2178 domain_enum domain,
2179 int global,
2180 symbol_compare_ftype *ordered_compare) override;
2181
2182 bool expand_symtabs_matching
2183 (struct objfile *objfile,
2184 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2185 const lookup_name_info *lookup_name,
2186 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2187 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2188 block_search_flags search_flags,
2189 domain_enum domain,
2190 enum search_domain kind) override;
2191 };
2192
2193 static quick_symbol_functions_up
2194 make_dwarf_gdb_index ()
2195 {
2196 return quick_symbol_functions_up (new dwarf2_gdb_index);
2197 }
2198
2199 static quick_symbol_functions_up
2200 make_dwarf_debug_names ()
2201 {
2202 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2203 }
2204
2205 /* Utility hash function for a stmt_list_hash. */
2206
2207 static hashval_t
2208 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2209 {
2210 hashval_t v = 0;
2211
2212 if (stmt_list_hash->dwo_unit != NULL)
2213 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2214 v += to_underlying (stmt_list_hash->line_sect_off);
2215 return v;
2216 }
2217
2218 /* Utility equality function for a stmt_list_hash. */
2219
2220 static int
2221 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2222 const struct stmt_list_hash *rhs)
2223 {
2224 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2225 return 0;
2226 if (lhs->dwo_unit != NULL
2227 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2228 return 0;
2229
2230 return lhs->line_sect_off == rhs->line_sect_off;
2231 }
2232
2233 /* Hash function for a quick_file_names. */
2234
2235 static hashval_t
2236 hash_file_name_entry (const void *e)
2237 {
2238 const struct quick_file_names *file_data
2239 = (const struct quick_file_names *) e;
2240
2241 return hash_stmt_list_entry (&file_data->hash);
2242 }
2243
2244 /* Equality function for a quick_file_names. */
2245
2246 static int
2247 eq_file_name_entry (const void *a, const void *b)
2248 {
2249 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2250 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2251
2252 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2253 }
2254
2255 /* Create a quick_file_names hash table. */
2256
2257 static htab_up
2258 create_quick_file_names_table (unsigned int nr_initial_entries)
2259 {
2260 return htab_up (htab_create_alloc (nr_initial_entries,
2261 hash_file_name_entry, eq_file_name_entry,
2262 nullptr, xcalloc, xfree));
2263 }
2264
2265 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2266 function is unrelated to symtabs, symtab would have to be created afterwards.
2267 You should call age_cached_comp_units after processing the CU. */
2268
2269 static dwarf2_cu *
2270 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2271 bool skip_partial)
2272 {
2273 if (per_cu->is_debug_types)
2274 load_full_type_unit (per_cu, per_objfile);
2275 else
2276 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2277 skip_partial, language_minimal);
2278
2279 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2280 if (cu == nullptr)
2281 return nullptr; /* Dummy CU. */
2282
2283 dwarf2_find_base_address (cu->dies, cu);
2284
2285 return cu;
2286 }
2287
2288 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2289
2290 static void
2291 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2292 dwarf2_per_objfile *per_objfile, bool skip_partial)
2293 {
2294 /* Skip type_unit_groups, reading the type units they contain
2295 is handled elsewhere. */
2296 if (per_cu->type_unit_group_p ())
2297 return;
2298
2299 {
2300 /* The destructor of dwarf2_queue_guard frees any entries left on
2301 the queue. After this point we're guaranteed to leave this function
2302 with the dwarf queue empty. */
2303 dwarf2_queue_guard q_guard (per_objfile);
2304
2305 if (!per_objfile->symtab_set_p (per_cu))
2306 {
2307 queue_comp_unit (per_cu, per_objfile, language_minimal);
2308 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2309
2310 /* If we just loaded a CU from a DWO, and we're working with an index
2311 that may badly handle TUs, load all the TUs in that DWO as well.
2312 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2313 if (!per_cu->is_debug_types
2314 && cu != NULL
2315 && cu->dwo_unit != NULL
2316 && per_objfile->per_bfd->index_table != NULL
2317 && per_objfile->per_bfd->index_table->version <= 7
2318 /* DWP files aren't supported yet. */
2319 && get_dwp_file (per_objfile) == NULL)
2320 queue_and_load_all_dwo_tus (cu);
2321 }
2322
2323 process_queue (per_objfile);
2324 }
2325
2326 /* Age the cache, releasing compilation units that have not
2327 been used recently. */
2328 per_objfile->age_comp_units ();
2329 }
2330
2331 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2332 the per-objfile for which this symtab is instantiated.
2333
2334 Returns the resulting symbol table. */
2335
2336 static struct compunit_symtab *
2337 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2338 dwarf2_per_objfile *per_objfile,
2339 bool skip_partial)
2340 {
2341 gdb_assert (per_objfile->per_bfd->using_index);
2342
2343 if (!per_objfile->symtab_set_p (per_cu))
2344 {
2345 free_cached_comp_units freer (per_objfile);
2346 scoped_restore decrementer = increment_reading_symtab ();
2347 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2348 process_cu_includes (per_objfile);
2349 }
2350
2351 return per_objfile->get_symtab (per_cu);
2352 }
2353
2354 /* See read.h. */
2355
2356 dwarf2_per_cu_data_up
2357 dwarf2_per_bfd::allocate_per_cu ()
2358 {
2359 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2360 result->per_bfd = this;
2361 result->index = all_comp_units.size ();
2362 return result;
2363 }
2364
2365 /* See read.h. */
2366
2367 signatured_type_up
2368 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2369 {
2370 signatured_type_up result (new signatured_type (signature));
2371 result->per_bfd = this;
2372 result->index = all_comp_units.size ();
2373 result->is_debug_types = true;
2374 tu_stats.nr_tus++;
2375 return result;
2376 }
2377
2378 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2379 obstack, and constructed with the specified field values. */
2380
2381 static dwarf2_per_cu_data_up
2382 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2383 struct dwarf2_section_info *section,
2384 int is_dwz,
2385 sect_offset sect_off, ULONGEST length)
2386 {
2387 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2388 the_cu->sect_off = sect_off;
2389 the_cu->length = length;
2390 the_cu->section = section;
2391 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2392 struct dwarf2_per_cu_quick_data);
2393 the_cu->is_dwz = is_dwz;
2394 return the_cu;
2395 }
2396
2397 /* A helper for create_cus_from_index that handles a given list of
2398 CUs. */
2399
2400 static void
2401 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2402 const gdb_byte *cu_list, offset_type n_elements,
2403 struct dwarf2_section_info *section,
2404 int is_dwz)
2405 {
2406 for (offset_type i = 0; i < n_elements; i += 2)
2407 {
2408 gdb_static_assert (sizeof (ULONGEST) >= 8);
2409
2410 sect_offset sect_off
2411 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2412 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2413 cu_list += 2 * 8;
2414
2415 dwarf2_per_cu_data_up per_cu
2416 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2417 length);
2418 per_bfd->all_comp_units.push_back (std::move (per_cu));
2419 }
2420 }
2421
2422 /* Read the CU list from the mapped index, and use it to create all
2423 the CU objects for PER_BFD. */
2424
2425 static void
2426 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2427 const gdb_byte *cu_list, offset_type cu_list_elements,
2428 const gdb_byte *dwz_list, offset_type dwz_elements)
2429 {
2430 gdb_assert (per_bfd->all_comp_units.empty ());
2431 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2432
2433 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2434 &per_bfd->info, 0);
2435
2436 if (dwz_elements == 0)
2437 return;
2438
2439 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2440 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2441 &dwz->info, 1);
2442 }
2443
2444 /* Create the signatured type hash table from the index. */
2445
2446 static void
2447 create_signatured_type_table_from_index
2448 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2449 const gdb_byte *bytes, offset_type elements)
2450 {
2451 htab_up sig_types_hash = allocate_signatured_type_table ();
2452
2453 for (offset_type i = 0; i < elements; i += 3)
2454 {
2455 signatured_type_up sig_type;
2456 ULONGEST signature;
2457 void **slot;
2458 cu_offset type_offset_in_tu;
2459
2460 gdb_static_assert (sizeof (ULONGEST) >= 8);
2461 sect_offset sect_off
2462 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2463 type_offset_in_tu
2464 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2465 BFD_ENDIAN_LITTLE);
2466 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2467 bytes += 3 * 8;
2468
2469 sig_type = per_bfd->allocate_signatured_type (signature);
2470 sig_type->type_offset_in_tu = type_offset_in_tu;
2471 sig_type->section = section;
2472 sig_type->sect_off = sect_off;
2473 sig_type->v.quick
2474 = OBSTACK_ZALLOC (&per_bfd->obstack,
2475 struct dwarf2_per_cu_quick_data);
2476
2477 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2478 *slot = sig_type.get ();
2479
2480 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2481 }
2482
2483 per_bfd->signatured_types = std::move (sig_types_hash);
2484 }
2485
2486 /* Create the signatured type hash table from .debug_names. */
2487
2488 static void
2489 create_signatured_type_table_from_debug_names
2490 (dwarf2_per_objfile *per_objfile,
2491 const mapped_debug_names &map,
2492 struct dwarf2_section_info *section,
2493 struct dwarf2_section_info *abbrev_section)
2494 {
2495 struct objfile *objfile = per_objfile->objfile;
2496
2497 section->read (objfile);
2498 abbrev_section->read (objfile);
2499
2500 htab_up sig_types_hash = allocate_signatured_type_table ();
2501
2502 for (uint32_t i = 0; i < map.tu_count; ++i)
2503 {
2504 signatured_type_up sig_type;
2505 void **slot;
2506
2507 sect_offset sect_off
2508 = (sect_offset) (extract_unsigned_integer
2509 (map.tu_table_reordered + i * map.offset_size,
2510 map.offset_size,
2511 map.dwarf5_byte_order));
2512
2513 comp_unit_head cu_header;
2514 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2515 abbrev_section,
2516 section->buffer + to_underlying (sect_off),
2517 rcuh_kind::TYPE);
2518
2519 sig_type = per_objfile->per_bfd->allocate_signatured_type
2520 (cu_header.signature);
2521 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2522 sig_type->section = section;
2523 sig_type->sect_off = sect_off;
2524 sig_type->v.quick
2525 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2526 struct dwarf2_per_cu_quick_data);
2527
2528 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2529 *slot = sig_type.get ();
2530
2531 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2532 }
2533
2534 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2535 }
2536
2537 /* Read the address map data from the mapped index, and use it to
2538 populate the psymtabs_addrmap. */
2539
2540 static void
2541 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2542 struct mapped_index *index)
2543 {
2544 struct objfile *objfile = per_objfile->objfile;
2545 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2546 struct gdbarch *gdbarch = objfile->arch ();
2547 const gdb_byte *iter, *end;
2548 struct addrmap *mutable_map;
2549 CORE_ADDR baseaddr;
2550
2551 auto_obstack temp_obstack;
2552
2553 mutable_map = addrmap_create_mutable (&temp_obstack);
2554
2555 iter = index->address_table.data ();
2556 end = iter + index->address_table.size ();
2557
2558 baseaddr = objfile->text_section_offset ();
2559
2560 while (iter < end)
2561 {
2562 ULONGEST hi, lo, cu_index;
2563 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2564 iter += 8;
2565 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2566 iter += 8;
2567 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2568 iter += 4;
2569
2570 if (lo > hi)
2571 {
2572 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2573 hex_string (lo), hex_string (hi));
2574 continue;
2575 }
2576
2577 if (cu_index >= per_bfd->all_comp_units.size ())
2578 {
2579 complaint (_(".gdb_index address table has invalid CU number %u"),
2580 (unsigned) cu_index);
2581 continue;
2582 }
2583
2584 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2585 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2586 addrmap_set_empty (mutable_map, lo, hi - 1,
2587 per_bfd->get_cu (cu_index));
2588 }
2589
2590 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2591 &per_bfd->obstack);
2592 }
2593
2594 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2595 populate the psymtabs_addrmap. */
2596
2597 static void
2598 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2599 struct dwarf2_section_info *section)
2600 {
2601 struct objfile *objfile = per_objfile->objfile;
2602 bfd *abfd = objfile->obfd;
2603 struct gdbarch *gdbarch = objfile->arch ();
2604 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2605 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2606
2607 auto_obstack temp_obstack;
2608 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2609
2610 std::unordered_map<sect_offset,
2611 dwarf2_per_cu_data *,
2612 gdb::hash_enum<sect_offset>>
2613 debug_info_offset_to_per_cu;
2614 for (const auto &per_cu : per_bfd->all_comp_units)
2615 {
2616 /* A TU will not need aranges, and skipping them here is an easy
2617 way of ignoring .debug_types -- and possibly seeing a
2618 duplicate section offset -- entirely. */
2619 if (per_cu->is_debug_types)
2620 continue;
2621
2622 const auto insertpair
2623 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2624 per_cu.get ());
2625 if (!insertpair.second)
2626 {
2627 warning (_("Section .debug_aranges in %s has duplicate "
2628 "debug_info_offset %s, ignoring .debug_aranges."),
2629 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2630 return;
2631 }
2632 }
2633
2634 section->read (objfile);
2635
2636 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2637
2638 const gdb_byte *addr = section->buffer;
2639
2640 while (addr < section->buffer + section->size)
2641 {
2642 const gdb_byte *const entry_addr = addr;
2643 unsigned int bytes_read;
2644
2645 const LONGEST entry_length = read_initial_length (abfd, addr,
2646 &bytes_read);
2647 addr += bytes_read;
2648
2649 const gdb_byte *const entry_end = addr + entry_length;
2650 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2651 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2652 if (addr + entry_length > section->buffer + section->size)
2653 {
2654 warning (_("Section .debug_aranges in %s entry at offset %s "
2655 "length %s exceeds section length %s, "
2656 "ignoring .debug_aranges."),
2657 objfile_name (objfile),
2658 plongest (entry_addr - section->buffer),
2659 plongest (bytes_read + entry_length),
2660 pulongest (section->size));
2661 return;
2662 }
2663
2664 /* The version number. */
2665 const uint16_t version = read_2_bytes (abfd, addr);
2666 addr += 2;
2667 if (version != 2)
2668 {
2669 warning (_("Section .debug_aranges in %s entry at offset %s "
2670 "has unsupported version %d, ignoring .debug_aranges."),
2671 objfile_name (objfile),
2672 plongest (entry_addr - section->buffer), version);
2673 return;
2674 }
2675
2676 const uint64_t debug_info_offset
2677 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2678 addr += offset_size;
2679 const auto per_cu_it
2680 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2681 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2682 {
2683 warning (_("Section .debug_aranges in %s entry at offset %s "
2684 "debug_info_offset %s does not exists, "
2685 "ignoring .debug_aranges."),
2686 objfile_name (objfile),
2687 plongest (entry_addr - section->buffer),
2688 pulongest (debug_info_offset));
2689 return;
2690 }
2691 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2692
2693 const uint8_t address_size = *addr++;
2694 if (address_size < 1 || address_size > 8)
2695 {
2696 warning (_("Section .debug_aranges in %s entry at offset %s "
2697 "address_size %u is invalid, ignoring .debug_aranges."),
2698 objfile_name (objfile),
2699 plongest (entry_addr - section->buffer), address_size);
2700 return;
2701 }
2702
2703 const uint8_t segment_selector_size = *addr++;
2704 if (segment_selector_size != 0)
2705 {
2706 warning (_("Section .debug_aranges in %s entry at offset %s "
2707 "segment_selector_size %u is not supported, "
2708 "ignoring .debug_aranges."),
2709 objfile_name (objfile),
2710 plongest (entry_addr - section->buffer),
2711 segment_selector_size);
2712 return;
2713 }
2714
2715 /* Must pad to an alignment boundary that is twice the address
2716 size. It is undocumented by the DWARF standard but GCC does
2717 use it. However, not every compiler does this. We can see
2718 whether it has happened by looking at the total length of the
2719 contents of the aranges for this CU -- it if isn't a multiple
2720 of twice the address size, then we skip any leftover
2721 bytes. */
2722 addr += (entry_end - addr) % (2 * address_size);
2723
2724 for (;;)
2725 {
2726 if (addr + 2 * address_size > entry_end)
2727 {
2728 warning (_("Section .debug_aranges in %s entry at offset %s "
2729 "address list is not properly terminated, "
2730 "ignoring .debug_aranges."),
2731 objfile_name (objfile),
2732 plongest (entry_addr - section->buffer));
2733 return;
2734 }
2735 ULONGEST start = extract_unsigned_integer (addr, address_size,
2736 dwarf5_byte_order);
2737 addr += address_size;
2738 ULONGEST length = extract_unsigned_integer (addr, address_size,
2739 dwarf5_byte_order);
2740 addr += address_size;
2741 if (start == 0 && length == 0)
2742 break;
2743 if (start == 0 && !per_bfd->has_section_at_zero)
2744 {
2745 /* Symbol was eliminated due to a COMDAT group. */
2746 continue;
2747 }
2748 ULONGEST end = start + length;
2749 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2750 - baseaddr);
2751 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2752 - baseaddr);
2753 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2754 }
2755 }
2756
2757 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2758 &per_bfd->obstack);
2759 }
2760
2761 /* A helper function that reads the .gdb_index from BUFFER and fills
2762 in MAP. FILENAME is the name of the file containing the data;
2763 it is used for error reporting. DEPRECATED_OK is true if it is
2764 ok to use deprecated sections.
2765
2766 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2767 out parameters that are filled in with information about the CU and
2768 TU lists in the section.
2769
2770 Returns true if all went well, false otherwise. */
2771
2772 static bool
2773 read_gdb_index_from_buffer (const char *filename,
2774 bool deprecated_ok,
2775 gdb::array_view<const gdb_byte> buffer,
2776 struct mapped_index *map,
2777 const gdb_byte **cu_list,
2778 offset_type *cu_list_elements,
2779 const gdb_byte **types_list,
2780 offset_type *types_list_elements)
2781 {
2782 const gdb_byte *addr = &buffer[0];
2783 offset_view metadata (buffer);
2784
2785 /* Version check. */
2786 offset_type version = metadata[0];
2787 /* Versions earlier than 3 emitted every copy of a psymbol. This
2788 causes the index to behave very poorly for certain requests. Version 3
2789 contained incomplete addrmap. So, it seems better to just ignore such
2790 indices. */
2791 if (version < 4)
2792 {
2793 static int warning_printed = 0;
2794 if (!warning_printed)
2795 {
2796 warning (_("Skipping obsolete .gdb_index section in %s."),
2797 filename);
2798 warning_printed = 1;
2799 }
2800 return 0;
2801 }
2802 /* Index version 4 uses a different hash function than index version
2803 5 and later.
2804
2805 Versions earlier than 6 did not emit psymbols for inlined
2806 functions. Using these files will cause GDB not to be able to
2807 set breakpoints on inlined functions by name, so we ignore these
2808 indices unless the user has done
2809 "set use-deprecated-index-sections on". */
2810 if (version < 6 && !deprecated_ok)
2811 {
2812 static int warning_printed = 0;
2813 if (!warning_printed)
2814 {
2815 warning (_("\
2816 Skipping deprecated .gdb_index section in %s.\n\
2817 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2818 to use the section anyway."),
2819 filename);
2820 warning_printed = 1;
2821 }
2822 return 0;
2823 }
2824 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2825 of the TU (for symbols coming from TUs),
2826 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2827 Plus gold-generated indices can have duplicate entries for global symbols,
2828 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2829 These are just performance bugs, and we can't distinguish gdb-generated
2830 indices from gold-generated ones, so issue no warning here. */
2831
2832 /* Indexes with higher version than the one supported by GDB may be no
2833 longer backward compatible. */
2834 if (version > 8)
2835 return 0;
2836
2837 map->version = version;
2838
2839 int i = 1;
2840 *cu_list = addr + metadata[i];
2841 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2842 ++i;
2843
2844 *types_list = addr + metadata[i];
2845 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2846 ++i;
2847
2848 const gdb_byte *address_table = addr + metadata[i];
2849 const gdb_byte *address_table_end = addr + metadata[i + 1];
2850 map->address_table
2851 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2852 ++i;
2853
2854 const gdb_byte *symbol_table = addr + metadata[i];
2855 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2856 map->symbol_table
2857 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2858 symbol_table_end));
2859
2860 ++i;
2861 map->constant_pool = buffer.slice (metadata[i]);
2862
2863 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2864 {
2865 /* An empty constant pool implies that all symbol table entries are
2866 empty. Make map->symbol_table.empty () == true. */
2867 map->symbol_table
2868 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2869 symbol_table));
2870 }
2871
2872 return 1;
2873 }
2874
2875 /* Callback types for dwarf2_read_gdb_index. */
2876
2877 typedef gdb::function_view
2878 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2879 get_gdb_index_contents_ftype;
2880 typedef gdb::function_view
2881 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2882 get_gdb_index_contents_dwz_ftype;
2883
2884 /* Read .gdb_index. If everything went ok, initialize the "quick"
2885 elements of all the CUs and return 1. Otherwise, return 0. */
2886
2887 static int
2888 dwarf2_read_gdb_index
2889 (dwarf2_per_objfile *per_objfile,
2890 get_gdb_index_contents_ftype get_gdb_index_contents,
2891 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2892 {
2893 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2894 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2895 struct dwz_file *dwz;
2896 struct objfile *objfile = per_objfile->objfile;
2897 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2898
2899 gdb::array_view<const gdb_byte> main_index_contents
2900 = get_gdb_index_contents (objfile, per_bfd);
2901
2902 if (main_index_contents.empty ())
2903 return 0;
2904
2905 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2906 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2907 use_deprecated_index_sections,
2908 main_index_contents, map.get (), &cu_list,
2909 &cu_list_elements, &types_list,
2910 &types_list_elements))
2911 return 0;
2912
2913 /* Don't use the index if it's empty. */
2914 if (map->symbol_table.empty ())
2915 return 0;
2916
2917 /* If there is a .dwz file, read it so we can get its CU list as
2918 well. */
2919 dwz = dwarf2_get_dwz_file (per_bfd);
2920 if (dwz != NULL)
2921 {
2922 struct mapped_index dwz_map;
2923 const gdb_byte *dwz_types_ignore;
2924 offset_type dwz_types_elements_ignore;
2925
2926 gdb::array_view<const gdb_byte> dwz_index_content
2927 = get_gdb_index_contents_dwz (objfile, dwz);
2928
2929 if (dwz_index_content.empty ())
2930 return 0;
2931
2932 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2933 1, dwz_index_content, &dwz_map,
2934 &dwz_list, &dwz_list_elements,
2935 &dwz_types_ignore,
2936 &dwz_types_elements_ignore))
2937 {
2938 warning (_("could not read '.gdb_index' section from %s; skipping"),
2939 bfd_get_filename (dwz->dwz_bfd.get ()));
2940 return 0;
2941 }
2942 }
2943
2944 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2945 dwz_list_elements);
2946
2947 if (types_list_elements)
2948 {
2949 /* We can only handle a single .debug_types when we have an
2950 index. */
2951 if (per_bfd->types.size () != 1)
2952 return 0;
2953
2954 dwarf2_section_info *section = &per_bfd->types[0];
2955
2956 create_signatured_type_table_from_index (per_bfd, section, types_list,
2957 types_list_elements);
2958 }
2959
2960 create_addrmap_from_index (per_objfile, map.get ());
2961
2962 per_bfd->index_table = std::move (map);
2963 per_bfd->using_index = 1;
2964 per_bfd->quick_file_names_table =
2965 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2966
2967 return 1;
2968 }
2969
2970 /* die_reader_func for dw2_get_file_names. */
2971
2972 static void
2973 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2974 struct die_info *comp_unit_die)
2975 {
2976 struct dwarf2_cu *cu = reader->cu;
2977 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2978 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2979 struct dwarf2_per_cu_data *lh_cu;
2980 struct attribute *attr;
2981 void **slot;
2982 struct quick_file_names *qfn;
2983
2984 gdb_assert (! this_cu->is_debug_types);
2985
2986 this_cu->v.quick->files_read = true;
2987 /* Our callers never want to match partial units -- instead they
2988 will match the enclosing full CU. */
2989 if (comp_unit_die->tag == DW_TAG_partial_unit)
2990 return;
2991
2992 lh_cu = this_cu;
2993 slot = NULL;
2994
2995 line_header_up lh;
2996 sect_offset line_offset {};
2997
2998 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2999 if (attr != nullptr && attr->form_is_unsigned ())
3000 {
3001 struct quick_file_names find_entry;
3002
3003 line_offset = (sect_offset) attr->as_unsigned ();
3004
3005 /* We may have already read in this line header (TU line header sharing).
3006 If we have we're done. */
3007 find_entry.hash.dwo_unit = cu->dwo_unit;
3008 find_entry.hash.line_sect_off = line_offset;
3009 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3010 &find_entry, INSERT);
3011 if (*slot != NULL)
3012 {
3013 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3014 return;
3015 }
3016
3017 lh = dwarf_decode_line_header (line_offset, cu);
3018 }
3019
3020 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3021
3022 int offset = 0;
3023 if (strcmp (fnd.name, "<unknown>") != 0)
3024 ++offset;
3025 else if (lh == nullptr)
3026 return;
3027
3028 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3029 qfn->hash.dwo_unit = cu->dwo_unit;
3030 qfn->hash.line_sect_off = line_offset;
3031 /* There may not be a DW_AT_stmt_list. */
3032 if (slot != nullptr)
3033 *slot = qfn;
3034
3035 std::vector<const char *> include_names;
3036 if (lh != nullptr)
3037 {
3038 for (const auto &entry : lh->file_names ())
3039 {
3040 gdb::unique_xmalloc_ptr<char> name_holder;
3041 const char *include_name =
3042 compute_include_file_name (lh.get (), entry, fnd, &name_holder);
3043 if (include_name != nullptr)
3044 {
3045 include_name = per_objfile->objfile->intern (include_name);
3046 include_names.push_back (include_name);
3047 }
3048 }
3049 }
3050
3051 qfn->num_file_names = offset + include_names.size ();
3052 qfn->comp_dir = fnd.comp_dir;
3053 qfn->file_names =
3054 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3055 qfn->num_file_names);
3056 if (offset != 0)
3057 qfn->file_names[0] = xstrdup (fnd.name);
3058
3059 if (!include_names.empty ())
3060 memcpy (&qfn->file_names[offset], include_names.data (),
3061 include_names.size () * sizeof (const char *));
3062
3063 qfn->real_names = NULL;
3064
3065 lh_cu->v.quick->file_names = qfn;
3066 }
3067
3068 /* A helper for the "quick" functions which attempts to read the line
3069 table for THIS_CU. */
3070
3071 static struct quick_file_names *
3072 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3073 dwarf2_per_objfile *per_objfile)
3074 {
3075 /* This should never be called for TUs. */
3076 gdb_assert (! this_cu->is_debug_types);
3077 /* Nor type unit groups. */
3078 gdb_assert (! this_cu->type_unit_group_p ());
3079
3080 if (this_cu->v.quick->files_read)
3081 return this_cu->v.quick->file_names;
3082
3083 cutu_reader reader (this_cu, per_objfile);
3084 if (!reader.dummy_p)
3085 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3086
3087 return this_cu->v.quick->file_names;
3088 }
3089
3090 /* A helper for the "quick" functions which computes and caches the
3091 real path for a given file name from the line table. */
3092
3093 static const char *
3094 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3095 struct quick_file_names *qfn, int index)
3096 {
3097 if (qfn->real_names == NULL)
3098 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3099 qfn->num_file_names, const char *);
3100
3101 if (qfn->real_names[index] == NULL)
3102 {
3103 const char *dirname = nullptr;
3104
3105 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
3106 dirname = qfn->comp_dir;
3107
3108 gdb::unique_xmalloc_ptr<char> fullname;
3109 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
3110
3111 qfn->real_names[index] = fullname.release ();
3112 }
3113
3114 return qfn->real_names[index];
3115 }
3116
3117 struct symtab *
3118 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3119 {
3120 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3121 dwarf2_per_cu_data *dwarf_cu
3122 = per_objfile->per_bfd->all_comp_units.back ().get ();
3123 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3124
3125 if (cust == NULL)
3126 return NULL;
3127
3128 return compunit_primary_filetab (cust);
3129 }
3130
3131 /* See read.h. */
3132
3133 void
3134 dwarf2_per_cu_data::free_cached_file_names ()
3135 {
3136 if (per_bfd == nullptr || !per_bfd->using_index || v.quick == nullptr)
3137 return;
3138
3139 struct quick_file_names *file_data = v.quick->file_names;
3140 if (file_data != nullptr && file_data->real_names != nullptr)
3141 {
3142 for (int i = 0; i < file_data->num_file_names; ++i)
3143 {
3144 xfree ((void *) file_data->real_names[i]);
3145 file_data->real_names[i] = nullptr;
3146 }
3147 }
3148 }
3149
3150 void
3151 dwarf2_base_index_functions::forget_cached_source_info
3152 (struct objfile *objfile)
3153 {
3154 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3155
3156 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
3157 per_cu->free_cached_file_names ();
3158 }
3159
3160 /* Struct used to manage iterating over all CUs looking for a symbol. */
3161
3162 struct dw2_symtab_iterator
3163 {
3164 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3165 dwarf2_per_objfile *per_objfile;
3166 /* If set, only look for symbols that match that block. Valid values are
3167 GLOBAL_BLOCK and STATIC_BLOCK. */
3168 gdb::optional<block_enum> block_index;
3169 /* The kind of symbol we're looking for. */
3170 domain_enum domain;
3171 /* The list of CUs from the index entry of the symbol,
3172 or NULL if not found. */
3173 offset_view vec;
3174 /* The next element in VEC to look at. */
3175 int next;
3176 /* The number of elements in VEC, or zero if there is no match. */
3177 int length;
3178 /* Have we seen a global version of the symbol?
3179 If so we can ignore all further global instances.
3180 This is to work around gold/15646, inefficient gold-generated
3181 indices. */
3182 int global_seen;
3183 };
3184
3185 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3186
3187 static void
3188 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3189 dwarf2_per_objfile *per_objfile,
3190 gdb::optional<block_enum> block_index,
3191 domain_enum domain, offset_type namei)
3192 {
3193 iter->per_objfile = per_objfile;
3194 iter->block_index = block_index;
3195 iter->domain = domain;
3196 iter->next = 0;
3197 iter->global_seen = 0;
3198 iter->vec = {};
3199 iter->length = 0;
3200
3201 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3202 /* index is NULL if OBJF_READNOW. */
3203 if (index == NULL)
3204 return;
3205
3206 gdb_assert (!index->symbol_name_slot_invalid (namei));
3207 offset_type vec_idx = index->symbol_vec_index (namei);
3208
3209 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3210 iter->length = iter->vec[0];
3211 }
3212
3213 /* Return the next matching CU or NULL if there are no more. */
3214
3215 static struct dwarf2_per_cu_data *
3216 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3217 {
3218 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3219
3220 for ( ; iter->next < iter->length; ++iter->next)
3221 {
3222 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3223 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3224 gdb_index_symbol_kind symbol_kind =
3225 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3226 /* Only check the symbol attributes if they're present.
3227 Indices prior to version 7 don't record them,
3228 and indices >= 7 may elide them for certain symbols
3229 (gold does this). */
3230 int attrs_valid =
3231 (per_objfile->per_bfd->index_table->version >= 7
3232 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3233
3234 /* Don't crash on bad data. */
3235 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3236 {
3237 complaint (_(".gdb_index entry has bad CU index"
3238 " [in module %s]"), objfile_name (per_objfile->objfile));
3239 continue;
3240 }
3241
3242 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3243
3244 /* Skip if already read in. */
3245 if (per_objfile->symtab_set_p (per_cu))
3246 continue;
3247
3248 /* Check static vs global. */
3249 if (attrs_valid)
3250 {
3251 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3252
3253 if (iter->block_index.has_value ())
3254 {
3255 bool want_static = *iter->block_index == STATIC_BLOCK;
3256
3257 if (is_static != want_static)
3258 continue;
3259 }
3260
3261 /* Work around gold/15646. */
3262 if (!is_static
3263 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3264 {
3265 if (iter->global_seen)
3266 continue;
3267
3268 iter->global_seen = 1;
3269 }
3270 }
3271
3272 /* Only check the symbol's kind if it has one. */
3273 if (attrs_valid)
3274 {
3275 switch (iter->domain)
3276 {
3277 case VAR_DOMAIN:
3278 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3279 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3280 /* Some types are also in VAR_DOMAIN. */
3281 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3282 continue;
3283 break;
3284 case STRUCT_DOMAIN:
3285 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3286 continue;
3287 break;
3288 case LABEL_DOMAIN:
3289 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3290 continue;
3291 break;
3292 case MODULE_DOMAIN:
3293 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3294 continue;
3295 break;
3296 default:
3297 break;
3298 }
3299 }
3300
3301 ++iter->next;
3302 return per_cu;
3303 }
3304
3305 return NULL;
3306 }
3307
3308 void
3309 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3310 bool print_bcache)
3311 {
3312 if (print_bcache)
3313 return;
3314
3315 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3316 int total = per_objfile->per_bfd->all_comp_units.size ();
3317 int count = 0;
3318
3319 for (int i = 0; i < total; ++i)
3320 {
3321 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3322
3323 if (!per_objfile->symtab_set_p (per_cu))
3324 ++count;
3325 }
3326 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3327 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3328 }
3329
3330 /* This dumps minimal information about the index.
3331 It is called via "mt print objfiles".
3332 One use is to verify .gdb_index has been loaded by the
3333 gdb.dwarf2/gdb-index.exp testcase. */
3334
3335 void
3336 dwarf2_gdb_index::dump (struct objfile *objfile)
3337 {
3338 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3339
3340 gdb_assert (per_objfile->per_bfd->using_index);
3341 printf_filtered (".gdb_index:");
3342 if (per_objfile->per_bfd->index_table != NULL)
3343 {
3344 printf_filtered (" version %d\n",
3345 per_objfile->per_bfd->index_table->version);
3346 }
3347 else
3348 printf_filtered (" faked for \"readnow\"\n");
3349 printf_filtered ("\n");
3350 }
3351
3352 void
3353 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3354 {
3355 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3356 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3357
3358 for (int i = 0; i < total_units; ++i)
3359 {
3360 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3361
3362 /* We don't want to directly expand a partial CU, because if we
3363 read it with the wrong language, then assertion failures can
3364 be triggered later on. See PR symtab/23010. So, tell
3365 dw2_instantiate_symtab to skip partial CUs -- any important
3366 partial CU will be read via DW_TAG_imported_unit anyway. */
3367 dw2_instantiate_symtab (per_cu, per_objfile, true);
3368 }
3369 }
3370
3371 static bool
3372 dw2_expand_symtabs_matching_symbol
3373 (mapped_index_base &index,
3374 const lookup_name_info &lookup_name_in,
3375 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3376 gdb::function_view<bool (offset_type)> match_callback,
3377 dwarf2_per_objfile *per_objfile);
3378
3379 static bool
3380 dw2_expand_symtabs_matching_one
3381 (dwarf2_per_cu_data *per_cu,
3382 dwarf2_per_objfile *per_objfile,
3383 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3384 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3385
3386 void
3387 dwarf2_gdb_index::expand_matching_symbols
3388 (struct objfile *objfile,
3389 const lookup_name_info &name, domain_enum domain,
3390 int global,
3391 symbol_compare_ftype *ordered_compare)
3392 {
3393 /* Used for Ada. */
3394 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3395
3396 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3397
3398 if (per_objfile->per_bfd->index_table != nullptr)
3399 {
3400 mapped_index &index = *per_objfile->per_bfd->index_table;
3401
3402 const char *match_name = name.ada ().lookup_name ().c_str ();
3403 auto matcher = [&] (const char *symname)
3404 {
3405 if (ordered_compare == nullptr)
3406 return true;
3407 return ordered_compare (symname, match_name) == 0;
3408 };
3409
3410 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3411 [&] (offset_type namei)
3412 {
3413 struct dw2_symtab_iterator iter;
3414 struct dwarf2_per_cu_data *per_cu;
3415
3416 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3417 namei);
3418 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3419 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3420 nullptr);
3421 return true;
3422 }, per_objfile);
3423 }
3424 else
3425 {
3426 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3427 proceed assuming all symtabs have been read in. */
3428 }
3429 }
3430
3431 /* Starting from a search name, return the string that finds the upper
3432 bound of all strings that start with SEARCH_NAME in a sorted name
3433 list. Returns the empty string to indicate that the upper bound is
3434 the end of the list. */
3435
3436 static std::string
3437 make_sort_after_prefix_name (const char *search_name)
3438 {
3439 /* When looking to complete "func", we find the upper bound of all
3440 symbols that start with "func" by looking for where we'd insert
3441 the closest string that would follow "func" in lexicographical
3442 order. Usually, that's "func"-with-last-character-incremented,
3443 i.e. "fund". Mind non-ASCII characters, though. Usually those
3444 will be UTF-8 multi-byte sequences, but we can't be certain.
3445 Especially mind the 0xff character, which is a valid character in
3446 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3447 rule out compilers allowing it in identifiers. Note that
3448 conveniently, strcmp/strcasecmp are specified to compare
3449 characters interpreted as unsigned char. So what we do is treat
3450 the whole string as a base 256 number composed of a sequence of
3451 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3452 to 0, and carries 1 to the following more-significant position.
3453 If the very first character in SEARCH_NAME ends up incremented
3454 and carries/overflows, then the upper bound is the end of the
3455 list. The string after the empty string is also the empty
3456 string.
3457
3458 Some examples of this operation:
3459
3460 SEARCH_NAME => "+1" RESULT
3461
3462 "abc" => "abd"
3463 "ab\xff" => "ac"
3464 "\xff" "a" "\xff" => "\xff" "b"
3465 "\xff" => ""
3466 "\xff\xff" => ""
3467 "" => ""
3468
3469 Then, with these symbols for example:
3470
3471 func
3472 func1
3473 fund
3474
3475 completing "func" looks for symbols between "func" and
3476 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3477 which finds "func" and "func1", but not "fund".
3478
3479 And with:
3480
3481 funcÿ (Latin1 'ÿ' [0xff])
3482 funcÿ1
3483 fund
3484
3485 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3486 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3487
3488 And with:
3489
3490 ÿÿ (Latin1 'ÿ' [0xff])
3491 ÿÿ1
3492
3493 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3494 the end of the list.
3495 */
3496 std::string after = search_name;
3497 while (!after.empty () && (unsigned char) after.back () == 0xff)
3498 after.pop_back ();
3499 if (!after.empty ())
3500 after.back () = (unsigned char) after.back () + 1;
3501 return after;
3502 }
3503
3504 /* See declaration. */
3505
3506 std::pair<std::vector<name_component>::const_iterator,
3507 std::vector<name_component>::const_iterator>
3508 mapped_index_base::find_name_components_bounds
3509 (const lookup_name_info &lookup_name_without_params, language lang,
3510 dwarf2_per_objfile *per_objfile) const
3511 {
3512 auto *name_cmp
3513 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3514
3515 const char *lang_name
3516 = lookup_name_without_params.language_lookup_name (lang);
3517
3518 /* Comparison function object for lower_bound that matches against a
3519 given symbol name. */
3520 auto lookup_compare_lower = [&] (const name_component &elem,
3521 const char *name)
3522 {
3523 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3524 const char *elem_name = elem_qualified + elem.name_offset;
3525 return name_cmp (elem_name, name) < 0;
3526 };
3527
3528 /* Comparison function object for upper_bound that matches against a
3529 given symbol name. */
3530 auto lookup_compare_upper = [&] (const char *name,
3531 const name_component &elem)
3532 {
3533 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3534 const char *elem_name = elem_qualified + elem.name_offset;
3535 return name_cmp (name, elem_name) < 0;
3536 };
3537
3538 auto begin = this->name_components.begin ();
3539 auto end = this->name_components.end ();
3540
3541 /* Find the lower bound. */
3542 auto lower = [&] ()
3543 {
3544 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3545 return begin;
3546 else
3547 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3548 } ();
3549
3550 /* Find the upper bound. */
3551 auto upper = [&] ()
3552 {
3553 if (lookup_name_without_params.completion_mode ())
3554 {
3555 /* In completion mode, we want UPPER to point past all
3556 symbols names that have the same prefix. I.e., with
3557 these symbols, and completing "func":
3558
3559 function << lower bound
3560 function1
3561 other_function << upper bound
3562
3563 We find the upper bound by looking for the insertion
3564 point of "func"-with-last-character-incremented,
3565 i.e. "fund". */
3566 std::string after = make_sort_after_prefix_name (lang_name);
3567 if (after.empty ())
3568 return end;
3569 return std::lower_bound (lower, end, after.c_str (),
3570 lookup_compare_lower);
3571 }
3572 else
3573 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3574 } ();
3575
3576 return {lower, upper};
3577 }
3578
3579 /* See declaration. */
3580
3581 void
3582 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3583 {
3584 if (!this->name_components.empty ())
3585 return;
3586
3587 this->name_components_casing = case_sensitivity;
3588 auto *name_cmp
3589 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3590
3591 /* The code below only knows how to break apart components of C++
3592 symbol names (and other languages that use '::' as
3593 namespace/module separator) and Ada symbol names. */
3594 auto count = this->symbol_name_count ();
3595 for (offset_type idx = 0; idx < count; idx++)
3596 {
3597 if (this->symbol_name_slot_invalid (idx))
3598 continue;
3599
3600 const char *name = this->symbol_name_at (idx, per_objfile);
3601
3602 /* Add each name component to the name component table. */
3603 unsigned int previous_len = 0;
3604
3605 if (strstr (name, "::") != nullptr)
3606 {
3607 for (unsigned int current_len = cp_find_first_component (name);
3608 name[current_len] != '\0';
3609 current_len += cp_find_first_component (name + current_len))
3610 {
3611 gdb_assert (name[current_len] == ':');
3612 this->name_components.push_back ({previous_len, idx});
3613 /* Skip the '::'. */
3614 current_len += 2;
3615 previous_len = current_len;
3616 }
3617 }
3618 else
3619 {
3620 /* Handle the Ada encoded (aka mangled) form here. */
3621 for (const char *iter = strstr (name, "__");
3622 iter != nullptr;
3623 iter = strstr (iter, "__"))
3624 {
3625 this->name_components.push_back ({previous_len, idx});
3626 iter += 2;
3627 previous_len = iter - name;
3628 }
3629 }
3630
3631 this->name_components.push_back ({previous_len, idx});
3632 }
3633
3634 /* Sort name_components elements by name. */
3635 auto name_comp_compare = [&] (const name_component &left,
3636 const name_component &right)
3637 {
3638 const char *left_qualified
3639 = this->symbol_name_at (left.idx, per_objfile);
3640 const char *right_qualified
3641 = this->symbol_name_at (right.idx, per_objfile);
3642
3643 const char *left_name = left_qualified + left.name_offset;
3644 const char *right_name = right_qualified + right.name_offset;
3645
3646 return name_cmp (left_name, right_name) < 0;
3647 };
3648
3649 std::sort (this->name_components.begin (),
3650 this->name_components.end (),
3651 name_comp_compare);
3652 }
3653
3654 /* Helper for dw2_expand_symtabs_matching that works with a
3655 mapped_index_base instead of the containing objfile. This is split
3656 to a separate function in order to be able to unit test the
3657 name_components matching using a mock mapped_index_base. For each
3658 symbol name that matches, calls MATCH_CALLBACK, passing it the
3659 symbol's index in the mapped_index_base symbol table. */
3660
3661 static bool
3662 dw2_expand_symtabs_matching_symbol
3663 (mapped_index_base &index,
3664 const lookup_name_info &lookup_name_in,
3665 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3666 gdb::function_view<bool (offset_type)> match_callback,
3667 dwarf2_per_objfile *per_objfile)
3668 {
3669 lookup_name_info lookup_name_without_params
3670 = lookup_name_in.make_ignore_params ();
3671
3672 /* Build the symbol name component sorted vector, if we haven't
3673 yet. */
3674 index.build_name_components (per_objfile);
3675
3676 /* The same symbol may appear more than once in the range though.
3677 E.g., if we're looking for symbols that complete "w", and we have
3678 a symbol named "w1::w2", we'll find the two name components for
3679 that same symbol in the range. To be sure we only call the
3680 callback once per symbol, we first collect the symbol name
3681 indexes that matched in a temporary vector and ignore
3682 duplicates. */
3683 std::vector<offset_type> matches;
3684
3685 struct name_and_matcher
3686 {
3687 symbol_name_matcher_ftype *matcher;
3688 const char *name;
3689
3690 bool operator== (const name_and_matcher &other) const
3691 {
3692 return matcher == other.matcher && strcmp (name, other.name) == 0;
3693 }
3694 };
3695
3696 /* A vector holding all the different symbol name matchers, for all
3697 languages. */
3698 std::vector<name_and_matcher> matchers;
3699
3700 for (int i = 0; i < nr_languages; i++)
3701 {
3702 enum language lang_e = (enum language) i;
3703
3704 const language_defn *lang = language_def (lang_e);
3705 symbol_name_matcher_ftype *name_matcher
3706 = lang->get_symbol_name_matcher (lookup_name_without_params);
3707
3708 name_and_matcher key {
3709 name_matcher,
3710 lookup_name_without_params.language_lookup_name (lang_e)
3711 };
3712
3713 /* Don't insert the same comparison routine more than once.
3714 Note that we do this linear walk. This is not a problem in
3715 practice because the number of supported languages is
3716 low. */
3717 if (std::find (matchers.begin (), matchers.end (), key)
3718 != matchers.end ())
3719 continue;
3720 matchers.push_back (std::move (key));
3721
3722 auto bounds
3723 = index.find_name_components_bounds (lookup_name_without_params,
3724 lang_e, per_objfile);
3725
3726 /* Now for each symbol name in range, check to see if we have a name
3727 match, and if so, call the MATCH_CALLBACK callback. */
3728
3729 for (; bounds.first != bounds.second; ++bounds.first)
3730 {
3731 const char *qualified
3732 = index.symbol_name_at (bounds.first->idx, per_objfile);
3733
3734 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3735 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3736 continue;
3737
3738 matches.push_back (bounds.first->idx);
3739 }
3740 }
3741
3742 std::sort (matches.begin (), matches.end ());
3743
3744 /* Finally call the callback, once per match. */
3745 ULONGEST prev = -1;
3746 bool result = true;
3747 for (offset_type idx : matches)
3748 {
3749 if (prev != idx)
3750 {
3751 if (!match_callback (idx))
3752 {
3753 result = false;
3754 break;
3755 }
3756 prev = idx;
3757 }
3758 }
3759
3760 /* Above we use a type wider than idx's for 'prev', since 0 and
3761 (offset_type)-1 are both possible values. */
3762 static_assert (sizeof (prev) > sizeof (offset_type), "");
3763
3764 return result;
3765 }
3766
3767 #if GDB_SELF_TEST
3768
3769 namespace selftests { namespace dw2_expand_symtabs_matching {
3770
3771 /* A mock .gdb_index/.debug_names-like name index table, enough to
3772 exercise dw2_expand_symtabs_matching_symbol, which works with the
3773 mapped_index_base interface. Builds an index from the symbol list
3774 passed as parameter to the constructor. */
3775 class mock_mapped_index : public mapped_index_base
3776 {
3777 public:
3778 mock_mapped_index (gdb::array_view<const char *> symbols)
3779 : m_symbol_table (symbols)
3780 {}
3781
3782 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3783
3784 /* Return the number of names in the symbol table. */
3785 size_t symbol_name_count () const override
3786 {
3787 return m_symbol_table.size ();
3788 }
3789
3790 /* Get the name of the symbol at IDX in the symbol table. */
3791 const char *symbol_name_at
3792 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3793 {
3794 return m_symbol_table[idx];
3795 }
3796
3797 private:
3798 gdb::array_view<const char *> m_symbol_table;
3799 };
3800
3801 /* Convenience function that converts a NULL pointer to a "<null>"
3802 string, to pass to print routines. */
3803
3804 static const char *
3805 string_or_null (const char *str)
3806 {
3807 return str != NULL ? str : "<null>";
3808 }
3809
3810 /* Check if a lookup_name_info built from
3811 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3812 index. EXPECTED_LIST is the list of expected matches, in expected
3813 matching order. If no match expected, then an empty list is
3814 specified. Returns true on success. On failure prints a warning
3815 indicating the file:line that failed, and returns false. */
3816
3817 static bool
3818 check_match (const char *file, int line,
3819 mock_mapped_index &mock_index,
3820 const char *name, symbol_name_match_type match_type,
3821 bool completion_mode,
3822 std::initializer_list<const char *> expected_list,
3823 dwarf2_per_objfile *per_objfile)
3824 {
3825 lookup_name_info lookup_name (name, match_type, completion_mode);
3826
3827 bool matched = true;
3828
3829 auto mismatch = [&] (const char *expected_str,
3830 const char *got)
3831 {
3832 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3833 "expected=\"%s\", got=\"%s\"\n"),
3834 file, line,
3835 (match_type == symbol_name_match_type::FULL
3836 ? "FULL" : "WILD"),
3837 name, string_or_null (expected_str), string_or_null (got));
3838 matched = false;
3839 };
3840
3841 auto expected_it = expected_list.begin ();
3842 auto expected_end = expected_list.end ();
3843
3844 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3845 nullptr,
3846 [&] (offset_type idx)
3847 {
3848 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3849 const char *expected_str
3850 = expected_it == expected_end ? NULL : *expected_it++;
3851
3852 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3853 mismatch (expected_str, matched_name);
3854 return true;
3855 }, per_objfile);
3856
3857 const char *expected_str
3858 = expected_it == expected_end ? NULL : *expected_it++;
3859 if (expected_str != NULL)
3860 mismatch (expected_str, NULL);
3861
3862 return matched;
3863 }
3864
3865 /* The symbols added to the mock mapped_index for testing (in
3866 canonical form). */
3867 static const char *test_symbols[] = {
3868 "function",
3869 "std::bar",
3870 "std::zfunction",
3871 "std::zfunction2",
3872 "w1::w2",
3873 "ns::foo<char*>",
3874 "ns::foo<int>",
3875 "ns::foo<long>",
3876 "ns2::tmpl<int>::foo2",
3877 "(anonymous namespace)::A::B::C",
3878
3879 /* These are used to check that the increment-last-char in the
3880 matching algorithm for completion doesn't match "t1_fund" when
3881 completing "t1_func". */
3882 "t1_func",
3883 "t1_func1",
3884 "t1_fund",
3885 "t1_fund1",
3886
3887 /* A UTF-8 name with multi-byte sequences to make sure that
3888 cp-name-parser understands this as a single identifier ("função"
3889 is "function" in PT). */
3890 u8"u8função",
3891
3892 /* \377 (0xff) is Latin1 'ÿ'. */
3893 "yfunc\377",
3894
3895 /* \377 (0xff) is Latin1 'ÿ'. */
3896 "\377",
3897 "\377\377123",
3898
3899 /* A name with all sorts of complications. Starts with "z" to make
3900 it easier for the completion tests below. */
3901 #define Z_SYM_NAME \
3902 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3903 "::tuple<(anonymous namespace)::ui*, " \
3904 "std::default_delete<(anonymous namespace)::ui>, void>"
3905
3906 Z_SYM_NAME
3907 };
3908
3909 /* Returns true if the mapped_index_base::find_name_component_bounds
3910 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3911 in completion mode. */
3912
3913 static bool
3914 check_find_bounds_finds (mapped_index_base &index,
3915 const char *search_name,
3916 gdb::array_view<const char *> expected_syms,
3917 dwarf2_per_objfile *per_objfile)
3918 {
3919 lookup_name_info lookup_name (search_name,
3920 symbol_name_match_type::FULL, true);
3921
3922 auto bounds = index.find_name_components_bounds (lookup_name,
3923 language_cplus,
3924 per_objfile);
3925
3926 size_t distance = std::distance (bounds.first, bounds.second);
3927 if (distance != expected_syms.size ())
3928 return false;
3929
3930 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3931 {
3932 auto nc_elem = bounds.first + exp_elem;
3933 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3934 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3935 return false;
3936 }
3937
3938 return true;
3939 }
3940
3941 /* Test the lower-level mapped_index::find_name_component_bounds
3942 method. */
3943
3944 static void
3945 test_mapped_index_find_name_component_bounds ()
3946 {
3947 mock_mapped_index mock_index (test_symbols);
3948
3949 mock_index.build_name_components (NULL /* per_objfile */);
3950
3951 /* Test the lower-level mapped_index::find_name_component_bounds
3952 method in completion mode. */
3953 {
3954 static const char *expected_syms[] = {
3955 "t1_func",
3956 "t1_func1",
3957 };
3958
3959 SELF_CHECK (check_find_bounds_finds
3960 (mock_index, "t1_func", expected_syms,
3961 NULL /* per_objfile */));
3962 }
3963
3964 /* Check that the increment-last-char in the name matching algorithm
3965 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3966 {
3967 static const char *expected_syms1[] = {
3968 "\377",
3969 "\377\377123",
3970 };
3971 SELF_CHECK (check_find_bounds_finds
3972 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3973
3974 static const char *expected_syms2[] = {
3975 "\377\377123",
3976 };
3977 SELF_CHECK (check_find_bounds_finds
3978 (mock_index, "\377\377", expected_syms2,
3979 NULL /* per_objfile */));
3980 }
3981 }
3982
3983 /* Test dw2_expand_symtabs_matching_symbol. */
3984
3985 static void
3986 test_dw2_expand_symtabs_matching_symbol ()
3987 {
3988 mock_mapped_index mock_index (test_symbols);
3989
3990 /* We let all tests run until the end even if some fails, for debug
3991 convenience. */
3992 bool any_mismatch = false;
3993
3994 /* Create the expected symbols list (an initializer_list). Needed
3995 because lists have commas, and we need to pass them to CHECK,
3996 which is a macro. */
3997 #define EXPECT(...) { __VA_ARGS__ }
3998
3999 /* Wrapper for check_match that passes down the current
4000 __FILE__/__LINE__. */
4001 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4002 any_mismatch |= !check_match (__FILE__, __LINE__, \
4003 mock_index, \
4004 NAME, MATCH_TYPE, COMPLETION_MODE, \
4005 EXPECTED_LIST, NULL)
4006
4007 /* Identity checks. */
4008 for (const char *sym : test_symbols)
4009 {
4010 /* Should be able to match all existing symbols. */
4011 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4012 EXPECT (sym));
4013
4014 /* Should be able to match all existing symbols with
4015 parameters. */
4016 std::string with_params = std::string (sym) + "(int)";
4017 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4018 EXPECT (sym));
4019
4020 /* Should be able to match all existing symbols with
4021 parameters and qualifiers. */
4022 with_params = std::string (sym) + " ( int ) const";
4023 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4024 EXPECT (sym));
4025
4026 /* This should really find sym, but cp-name-parser.y doesn't
4027 know about lvalue/rvalue qualifiers yet. */
4028 with_params = std::string (sym) + " ( int ) &&";
4029 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4030 {});
4031 }
4032
4033 /* Check that the name matching algorithm for completion doesn't get
4034 confused with Latin1 'ÿ' / 0xff. */
4035 {
4036 static const char str[] = "\377";
4037 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4038 EXPECT ("\377", "\377\377123"));
4039 }
4040
4041 /* Check that the increment-last-char in the matching algorithm for
4042 completion doesn't match "t1_fund" when completing "t1_func". */
4043 {
4044 static const char str[] = "t1_func";
4045 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4046 EXPECT ("t1_func", "t1_func1"));
4047 }
4048
4049 /* Check that completion mode works at each prefix of the expected
4050 symbol name. */
4051 {
4052 static const char str[] = "function(int)";
4053 size_t len = strlen (str);
4054 std::string lookup;
4055
4056 for (size_t i = 1; i < len; i++)
4057 {
4058 lookup.assign (str, i);
4059 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4060 EXPECT ("function"));
4061 }
4062 }
4063
4064 /* While "w" is a prefix of both components, the match function
4065 should still only be called once. */
4066 {
4067 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4068 EXPECT ("w1::w2"));
4069 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4070 EXPECT ("w1::w2"));
4071 }
4072
4073 /* Same, with a "complicated" symbol. */
4074 {
4075 static const char str[] = Z_SYM_NAME;
4076 size_t len = strlen (str);
4077 std::string lookup;
4078
4079 for (size_t i = 1; i < len; i++)
4080 {
4081 lookup.assign (str, i);
4082 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4083 EXPECT (Z_SYM_NAME));
4084 }
4085 }
4086
4087 /* In FULL mode, an incomplete symbol doesn't match. */
4088 {
4089 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4090 {});
4091 }
4092
4093 /* A complete symbol with parameters matches any overload, since the
4094 index has no overload info. */
4095 {
4096 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4097 EXPECT ("std::zfunction", "std::zfunction2"));
4098 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4099 EXPECT ("std::zfunction", "std::zfunction2"));
4100 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4101 EXPECT ("std::zfunction", "std::zfunction2"));
4102 }
4103
4104 /* Check that whitespace is ignored appropriately. A symbol with a
4105 template argument list. */
4106 {
4107 static const char expected[] = "ns::foo<int>";
4108 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4109 EXPECT (expected));
4110 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4111 EXPECT (expected));
4112 }
4113
4114 /* Check that whitespace is ignored appropriately. A symbol with a
4115 template argument list that includes a pointer. */
4116 {
4117 static const char expected[] = "ns::foo<char*>";
4118 /* Try both completion and non-completion modes. */
4119 static const bool completion_mode[2] = {false, true};
4120 for (size_t i = 0; i < 2; i++)
4121 {
4122 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4123 completion_mode[i], EXPECT (expected));
4124 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4125 completion_mode[i], EXPECT (expected));
4126
4127 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4128 completion_mode[i], EXPECT (expected));
4129 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4130 completion_mode[i], EXPECT (expected));
4131 }
4132 }
4133
4134 {
4135 /* Check method qualifiers are ignored. */
4136 static const char expected[] = "ns::foo<char*>";
4137 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4138 symbol_name_match_type::FULL, true, EXPECT (expected));
4139 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4140 symbol_name_match_type::FULL, true, EXPECT (expected));
4141 CHECK_MATCH ("foo < char * > ( int ) const",
4142 symbol_name_match_type::WILD, true, EXPECT (expected));
4143 CHECK_MATCH ("foo < char * > ( int ) &&",
4144 symbol_name_match_type::WILD, true, EXPECT (expected));
4145 }
4146
4147 /* Test lookup names that don't match anything. */
4148 {
4149 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4150 {});
4151
4152 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4153 {});
4154 }
4155
4156 /* Some wild matching tests, exercising "(anonymous namespace)",
4157 which should not be confused with a parameter list. */
4158 {
4159 static const char *syms[] = {
4160 "A::B::C",
4161 "B::C",
4162 "C",
4163 "A :: B :: C ( int )",
4164 "B :: C ( int )",
4165 "C ( int )",
4166 };
4167
4168 for (const char *s : syms)
4169 {
4170 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4171 EXPECT ("(anonymous namespace)::A::B::C"));
4172 }
4173 }
4174
4175 {
4176 static const char expected[] = "ns2::tmpl<int>::foo2";
4177 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4178 EXPECT (expected));
4179 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4180 EXPECT (expected));
4181 }
4182
4183 SELF_CHECK (!any_mismatch);
4184
4185 #undef EXPECT
4186 #undef CHECK_MATCH
4187 }
4188
4189 static void
4190 run_test ()
4191 {
4192 test_mapped_index_find_name_component_bounds ();
4193 test_dw2_expand_symtabs_matching_symbol ();
4194 }
4195
4196 }} // namespace selftests::dw2_expand_symtabs_matching
4197
4198 #endif /* GDB_SELF_TEST */
4199
4200 /* If FILE_MATCHER is NULL or if PER_CU has
4201 dwarf2_per_cu_quick_data::MARK set (see
4202 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4203 EXPANSION_NOTIFY on it. */
4204
4205 static bool
4206 dw2_expand_symtabs_matching_one
4207 (dwarf2_per_cu_data *per_cu,
4208 dwarf2_per_objfile *per_objfile,
4209 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4210 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4211 {
4212 if (file_matcher == NULL || per_cu->v.quick->mark)
4213 {
4214 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4215
4216 compunit_symtab *symtab
4217 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4218 gdb_assert (symtab != nullptr);
4219
4220 if (expansion_notify != NULL && symtab_was_null)
4221 return expansion_notify (symtab);
4222 }
4223 return true;
4224 }
4225
4226 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4227 matched, to expand corresponding CUs that were marked. IDX is the
4228 index of the symbol name that matched. */
4229
4230 static bool
4231 dw2_expand_marked_cus
4232 (dwarf2_per_objfile *per_objfile, offset_type idx,
4233 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4234 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4235 block_search_flags search_flags,
4236 search_domain kind)
4237 {
4238 offset_type vec_len, vec_idx;
4239 bool global_seen = false;
4240 mapped_index &index = *per_objfile->per_bfd->index_table;
4241
4242 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4243 vec_len = vec[0];
4244 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4245 {
4246 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4247 /* This value is only valid for index versions >= 7. */
4248 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4249 gdb_index_symbol_kind symbol_kind =
4250 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4251 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4252 /* Only check the symbol attributes if they're present.
4253 Indices prior to version 7 don't record them,
4254 and indices >= 7 may elide them for certain symbols
4255 (gold does this). */
4256 int attrs_valid =
4257 (index.version >= 7
4258 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4259
4260 /* Work around gold/15646. */
4261 if (attrs_valid
4262 && !is_static
4263 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4264 {
4265 if (global_seen)
4266 continue;
4267
4268 global_seen = true;
4269 }
4270
4271 /* Only check the symbol's kind if it has one. */
4272 if (attrs_valid)
4273 {
4274 if (is_static)
4275 {
4276 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4277 continue;
4278 }
4279 else
4280 {
4281 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4282 continue;
4283 }
4284
4285 switch (kind)
4286 {
4287 case VARIABLES_DOMAIN:
4288 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4289 continue;
4290 break;
4291 case FUNCTIONS_DOMAIN:
4292 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4293 continue;
4294 break;
4295 case TYPES_DOMAIN:
4296 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4297 continue;
4298 break;
4299 case MODULES_DOMAIN:
4300 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4301 continue;
4302 break;
4303 default:
4304 break;
4305 }
4306 }
4307
4308 /* Don't crash on bad data. */
4309 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4310 {
4311 complaint (_(".gdb_index entry has bad CU index"
4312 " [in module %s]"), objfile_name (per_objfile->objfile));
4313 continue;
4314 }
4315
4316 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4317 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4318 expansion_notify))
4319 return false;
4320 }
4321
4322 return true;
4323 }
4324
4325 /* If FILE_MATCHER is non-NULL, set all the
4326 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4327 that match FILE_MATCHER. */
4328
4329 static void
4330 dw_expand_symtabs_matching_file_matcher
4331 (dwarf2_per_objfile *per_objfile,
4332 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4333 {
4334 if (file_matcher == NULL)
4335 return;
4336
4337 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4338 htab_eq_pointer,
4339 NULL, xcalloc, xfree));
4340 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4341 htab_eq_pointer,
4342 NULL, xcalloc, xfree));
4343
4344 /* The rule is CUs specify all the files, including those used by
4345 any TU, so there's no need to scan TUs here. */
4346
4347 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4348 {
4349 QUIT;
4350
4351 if (per_cu->is_debug_types)
4352 continue;
4353 per_cu->v.quick->mark = 0;
4354
4355 /* We only need to look at symtabs not already expanded. */
4356 if (per_objfile->symtab_set_p (per_cu.get ()))
4357 continue;
4358
4359 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4360 per_objfile);
4361 if (file_data == NULL)
4362 continue;
4363
4364 if (htab_find (visited_not_found.get (), file_data) != NULL)
4365 continue;
4366 else if (htab_find (visited_found.get (), file_data) != NULL)
4367 {
4368 per_cu->v.quick->mark = 1;
4369 continue;
4370 }
4371
4372 for (int j = 0; j < file_data->num_file_names; ++j)
4373 {
4374 const char *this_real_name;
4375
4376 if (file_matcher (file_data->file_names[j], false))
4377 {
4378 per_cu->v.quick->mark = 1;
4379 break;
4380 }
4381
4382 /* Before we invoke realpath, which can get expensive when many
4383 files are involved, do a quick comparison of the basenames. */
4384 if (!basenames_may_differ
4385 && !file_matcher (lbasename (file_data->file_names[j]),
4386 true))
4387 continue;
4388
4389 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4390 if (file_matcher (this_real_name, false))
4391 {
4392 per_cu->v.quick->mark = 1;
4393 break;
4394 }
4395 }
4396
4397 void **slot = htab_find_slot (per_cu->v.quick->mark
4398 ? visited_found.get ()
4399 : visited_not_found.get (),
4400 file_data, INSERT);
4401 *slot = file_data;
4402 }
4403 }
4404
4405 bool
4406 dwarf2_gdb_index::expand_symtabs_matching
4407 (struct objfile *objfile,
4408 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4409 const lookup_name_info *lookup_name,
4410 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4411 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4412 block_search_flags search_flags,
4413 domain_enum domain,
4414 enum search_domain kind)
4415 {
4416 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4417
4418 /* index_table is NULL if OBJF_READNOW. */
4419 if (!per_objfile->per_bfd->index_table)
4420 return true;
4421
4422 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4423
4424 /* This invariant is documented in quick-functions.h. */
4425 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4426 if (lookup_name == nullptr)
4427 {
4428 for (dwarf2_per_cu_data *per_cu
4429 : all_comp_units_range (per_objfile->per_bfd))
4430 {
4431 QUIT;
4432
4433 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4434 file_matcher,
4435 expansion_notify))
4436 return false;
4437 }
4438 return true;
4439 }
4440
4441 mapped_index &index = *per_objfile->per_bfd->index_table;
4442
4443 bool result
4444 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4445 symbol_matcher,
4446 [&] (offset_type idx)
4447 {
4448 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4449 expansion_notify, search_flags, kind))
4450 return false;
4451 return true;
4452 }, per_objfile);
4453
4454 return result;
4455 }
4456
4457 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4458 symtab. */
4459
4460 static struct compunit_symtab *
4461 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4462 CORE_ADDR pc)
4463 {
4464 int i;
4465
4466 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4467 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4468 return cust;
4469
4470 if (cust->includes == NULL)
4471 return NULL;
4472
4473 for (i = 0; cust->includes[i]; ++i)
4474 {
4475 struct compunit_symtab *s = cust->includes[i];
4476
4477 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4478 if (s != NULL)
4479 return s;
4480 }
4481
4482 return NULL;
4483 }
4484
4485 struct compunit_symtab *
4486 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4487 (struct objfile *objfile,
4488 struct bound_minimal_symbol msymbol,
4489 CORE_ADDR pc,
4490 struct obj_section *section,
4491 int warn_if_readin)
4492 {
4493 struct dwarf2_per_cu_data *data;
4494 struct compunit_symtab *result;
4495
4496 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4497 if (per_objfile->per_bfd->index_addrmap == nullptr)
4498 return NULL;
4499
4500 CORE_ADDR baseaddr = objfile->text_section_offset ();
4501 data = ((struct dwarf2_per_cu_data *)
4502 addrmap_find (per_objfile->per_bfd->index_addrmap,
4503 pc - baseaddr));
4504 if (!data)
4505 return NULL;
4506
4507 if (warn_if_readin && per_objfile->symtab_set_p (data))
4508 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4509 paddress (objfile->arch (), pc));
4510
4511 result = recursively_find_pc_sect_compunit_symtab
4512 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4513
4514 gdb_assert (result != NULL);
4515 return result;
4516 }
4517
4518 void
4519 dwarf2_base_index_functions::map_symbol_filenames
4520 (struct objfile *objfile,
4521 gdb::function_view<symbol_filename_ftype> fun,
4522 bool need_fullname)
4523 {
4524 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4525
4526 /* Use caches to ensure we only call FUN once for each filename. */
4527 filename_seen_cache filenames_cache;
4528 std::unordered_set<quick_file_names *> qfn_cache;
4529
4530 /* The rule is CUs specify all the files, including those used by any TU,
4531 so there's no need to scan TUs here. We can ignore file names coming
4532 from already-expanded CUs. It is possible that an expanded CU might
4533 reuse the file names data from a currently unexpanded CU, in this
4534 case we don't want to report the files from the unexpanded CU. */
4535
4536 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4537 {
4538 if (!per_cu->is_debug_types
4539 && per_objfile->symtab_set_p (per_cu.get ()))
4540 {
4541 if (per_cu->v.quick->file_names != nullptr)
4542 qfn_cache.insert (per_cu->v.quick->file_names);
4543 }
4544 }
4545
4546 for (dwarf2_per_cu_data *per_cu
4547 : all_comp_units_range (per_objfile->per_bfd))
4548 {
4549 /* We only need to look at symtabs not already expanded. */
4550 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4551 continue;
4552
4553 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4554 if (file_data == nullptr
4555 || qfn_cache.find (file_data) != qfn_cache.end ())
4556 continue;
4557
4558 for (int j = 0; j < file_data->num_file_names; ++j)
4559 {
4560 const char *filename = file_data->file_names[j];
4561 const char *key = filename;
4562 const char *fullname = nullptr;
4563
4564 if (need_fullname)
4565 {
4566 fullname = dw2_get_real_path (per_objfile, file_data, j);
4567 key = fullname;
4568 }
4569
4570 if (!filenames_cache.seen (key))
4571 fun (filename, fullname);
4572 }
4573 }
4574 }
4575
4576 bool
4577 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4578 {
4579 return true;
4580 }
4581
4582 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4583
4584 bool
4585 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4586 {
4587 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4588
4589 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4590 {
4591 /* Is this already expanded? */
4592 if (per_objfile->symtab_set_p (per_cu.get ()))
4593 continue;
4594
4595 /* It has not yet been expanded. */
4596 return true;
4597 }
4598
4599 return false;
4600 }
4601
4602 /* DWARF-5 debug_names reader. */
4603
4604 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4605 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4606
4607 /* A helper function that reads the .debug_names section in SECTION
4608 and fills in MAP. FILENAME is the name of the file containing the
4609 section; it is used for error reporting.
4610
4611 Returns true if all went well, false otherwise. */
4612
4613 static bool
4614 read_debug_names_from_section (struct objfile *objfile,
4615 const char *filename,
4616 struct dwarf2_section_info *section,
4617 mapped_debug_names &map)
4618 {
4619 if (section->empty ())
4620 return false;
4621
4622 /* Older elfutils strip versions could keep the section in the main
4623 executable while splitting it for the separate debug info file. */
4624 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4625 return false;
4626
4627 section->read (objfile);
4628
4629 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4630
4631 const gdb_byte *addr = section->buffer;
4632
4633 bfd *const abfd = section->get_bfd_owner ();
4634
4635 unsigned int bytes_read;
4636 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4637 addr += bytes_read;
4638
4639 map.dwarf5_is_dwarf64 = bytes_read != 4;
4640 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4641 if (bytes_read + length != section->size)
4642 {
4643 /* There may be multiple per-CU indices. */
4644 warning (_("Section .debug_names in %s length %s does not match "
4645 "section length %s, ignoring .debug_names."),
4646 filename, plongest (bytes_read + length),
4647 pulongest (section->size));
4648 return false;
4649 }
4650
4651 /* The version number. */
4652 uint16_t version = read_2_bytes (abfd, addr);
4653 addr += 2;
4654 if (version != 5)
4655 {
4656 warning (_("Section .debug_names in %s has unsupported version %d, "
4657 "ignoring .debug_names."),
4658 filename, version);
4659 return false;
4660 }
4661
4662 /* Padding. */
4663 uint16_t padding = read_2_bytes (abfd, addr);
4664 addr += 2;
4665 if (padding != 0)
4666 {
4667 warning (_("Section .debug_names in %s has unsupported padding %d, "
4668 "ignoring .debug_names."),
4669 filename, padding);
4670 return false;
4671 }
4672
4673 /* comp_unit_count - The number of CUs in the CU list. */
4674 map.cu_count = read_4_bytes (abfd, addr);
4675 addr += 4;
4676
4677 /* local_type_unit_count - The number of TUs in the local TU
4678 list. */
4679 map.tu_count = read_4_bytes (abfd, addr);
4680 addr += 4;
4681
4682 /* foreign_type_unit_count - The number of TUs in the foreign TU
4683 list. */
4684 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4685 addr += 4;
4686 if (foreign_tu_count != 0)
4687 {
4688 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4689 "ignoring .debug_names."),
4690 filename, static_cast<unsigned long> (foreign_tu_count));
4691 return false;
4692 }
4693
4694 /* bucket_count - The number of hash buckets in the hash lookup
4695 table. */
4696 map.bucket_count = read_4_bytes (abfd, addr);
4697 addr += 4;
4698
4699 /* name_count - The number of unique names in the index. */
4700 map.name_count = read_4_bytes (abfd, addr);
4701 addr += 4;
4702
4703 /* abbrev_table_size - The size in bytes of the abbreviations
4704 table. */
4705 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4706 addr += 4;
4707
4708 /* augmentation_string_size - The size in bytes of the augmentation
4709 string. This value is rounded up to a multiple of 4. */
4710 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4711 addr += 4;
4712 map.augmentation_is_gdb = ((augmentation_string_size
4713 == sizeof (dwarf5_augmentation))
4714 && memcmp (addr, dwarf5_augmentation,
4715 sizeof (dwarf5_augmentation)) == 0);
4716 augmentation_string_size += (-augmentation_string_size) & 3;
4717 addr += augmentation_string_size;
4718
4719 /* List of CUs */
4720 map.cu_table_reordered = addr;
4721 addr += map.cu_count * map.offset_size;
4722
4723 /* List of Local TUs */
4724 map.tu_table_reordered = addr;
4725 addr += map.tu_count * map.offset_size;
4726
4727 /* Hash Lookup Table */
4728 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4729 addr += map.bucket_count * 4;
4730 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4731 addr += map.name_count * 4;
4732
4733 /* Name Table */
4734 map.name_table_string_offs_reordered = addr;
4735 addr += map.name_count * map.offset_size;
4736 map.name_table_entry_offs_reordered = addr;
4737 addr += map.name_count * map.offset_size;
4738
4739 const gdb_byte *abbrev_table_start = addr;
4740 for (;;)
4741 {
4742 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4743 addr += bytes_read;
4744 if (index_num == 0)
4745 break;
4746
4747 const auto insertpair
4748 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4749 if (!insertpair.second)
4750 {
4751 warning (_("Section .debug_names in %s has duplicate index %s, "
4752 "ignoring .debug_names."),
4753 filename, pulongest (index_num));
4754 return false;
4755 }
4756 mapped_debug_names::index_val &indexval = insertpair.first->second;
4757 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4758 addr += bytes_read;
4759
4760 for (;;)
4761 {
4762 mapped_debug_names::index_val::attr attr;
4763 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4764 addr += bytes_read;
4765 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4766 addr += bytes_read;
4767 if (attr.form == DW_FORM_implicit_const)
4768 {
4769 attr.implicit_const = read_signed_leb128 (abfd, addr,
4770 &bytes_read);
4771 addr += bytes_read;
4772 }
4773 if (attr.dw_idx == 0 && attr.form == 0)
4774 break;
4775 indexval.attr_vec.push_back (std::move (attr));
4776 }
4777 }
4778 if (addr != abbrev_table_start + abbrev_table_size)
4779 {
4780 warning (_("Section .debug_names in %s has abbreviation_table "
4781 "of size %s vs. written as %u, ignoring .debug_names."),
4782 filename, plongest (addr - abbrev_table_start),
4783 abbrev_table_size);
4784 return false;
4785 }
4786 map.entry_pool = addr;
4787
4788 return true;
4789 }
4790
4791 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4792 list. */
4793
4794 static void
4795 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4796 const mapped_debug_names &map,
4797 dwarf2_section_info &section,
4798 bool is_dwz)
4799 {
4800 if (!map.augmentation_is_gdb)
4801 {
4802 for (uint32_t i = 0; i < map.cu_count; ++i)
4803 {
4804 sect_offset sect_off
4805 = (sect_offset) (extract_unsigned_integer
4806 (map.cu_table_reordered + i * map.offset_size,
4807 map.offset_size,
4808 map.dwarf5_byte_order));
4809 /* We don't know the length of the CU, because the CU list in a
4810 .debug_names index can be incomplete, so we can't use the start
4811 of the next CU as end of this CU. We create the CUs here with
4812 length 0, and in cutu_reader::cutu_reader we'll fill in the
4813 actual length. */
4814 dwarf2_per_cu_data_up per_cu
4815 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4816 sect_off, 0);
4817 per_bfd->all_comp_units.push_back (std::move (per_cu));
4818 }
4819 return;
4820 }
4821
4822 sect_offset sect_off_prev;
4823 for (uint32_t i = 0; i <= map.cu_count; ++i)
4824 {
4825 sect_offset sect_off_next;
4826 if (i < map.cu_count)
4827 {
4828 sect_off_next
4829 = (sect_offset) (extract_unsigned_integer
4830 (map.cu_table_reordered + i * map.offset_size,
4831 map.offset_size,
4832 map.dwarf5_byte_order));
4833 }
4834 else
4835 sect_off_next = (sect_offset) section.size;
4836 if (i >= 1)
4837 {
4838 const ULONGEST length = sect_off_next - sect_off_prev;
4839 dwarf2_per_cu_data_up per_cu
4840 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4841 sect_off_prev, length);
4842 per_bfd->all_comp_units.push_back (std::move (per_cu));
4843 }
4844 sect_off_prev = sect_off_next;
4845 }
4846 }
4847
4848 /* Read the CU list from the mapped index, and use it to create all
4849 the CU objects for this dwarf2_per_objfile. */
4850
4851 static void
4852 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4853 const mapped_debug_names &map,
4854 const mapped_debug_names &dwz_map)
4855 {
4856 gdb_assert (per_bfd->all_comp_units.empty ());
4857 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4858
4859 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4860 false /* is_dwz */);
4861
4862 if (dwz_map.cu_count == 0)
4863 return;
4864
4865 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4866 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4867 true /* is_dwz */);
4868 }
4869
4870 /* Read .debug_names. If everything went ok, initialize the "quick"
4871 elements of all the CUs and return true. Otherwise, return false. */
4872
4873 static bool
4874 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4875 {
4876 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4877 mapped_debug_names dwz_map;
4878 struct objfile *objfile = per_objfile->objfile;
4879 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4880
4881 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4882 &per_bfd->debug_names, *map))
4883 return false;
4884
4885 /* Don't use the index if it's empty. */
4886 if (map->name_count == 0)
4887 return false;
4888
4889 /* If there is a .dwz file, read it so we can get its CU list as
4890 well. */
4891 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4892 if (dwz != NULL)
4893 {
4894 if (!read_debug_names_from_section (objfile,
4895 bfd_get_filename (dwz->dwz_bfd.get ()),
4896 &dwz->debug_names, dwz_map))
4897 {
4898 warning (_("could not read '.debug_names' section from %s; skipping"),
4899 bfd_get_filename (dwz->dwz_bfd.get ()));
4900 return false;
4901 }
4902 }
4903
4904 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4905
4906 if (map->tu_count != 0)
4907 {
4908 /* We can only handle a single .debug_types when we have an
4909 index. */
4910 if (per_bfd->types.size () != 1)
4911 return false;
4912
4913 dwarf2_section_info *section = &per_bfd->types[0];
4914
4915 create_signatured_type_table_from_debug_names
4916 (per_objfile, *map, section, &per_bfd->abbrev);
4917 }
4918
4919 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4920
4921 per_bfd->debug_names_table = std::move (map);
4922 per_bfd->using_index = 1;
4923 per_bfd->quick_file_names_table =
4924 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4925
4926 return true;
4927 }
4928
4929 /* Type used to manage iterating over all CUs looking for a symbol for
4930 .debug_names. */
4931
4932 class dw2_debug_names_iterator
4933 {
4934 public:
4935 dw2_debug_names_iterator (const mapped_debug_names &map,
4936 block_search_flags block_index,
4937 domain_enum domain,
4938 const char *name, dwarf2_per_objfile *per_objfile)
4939 : m_map (map), m_block_index (block_index), m_domain (domain),
4940 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4941 m_per_objfile (per_objfile)
4942 {}
4943
4944 dw2_debug_names_iterator (const mapped_debug_names &map,
4945 search_domain search, uint32_t namei,
4946 dwarf2_per_objfile *per_objfile,
4947 domain_enum domain = UNDEF_DOMAIN)
4948 : m_map (map),
4949 m_domain (domain),
4950 m_search (search),
4951 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4952 m_per_objfile (per_objfile)
4953 {}
4954
4955 dw2_debug_names_iterator (const mapped_debug_names &map,
4956 block_search_flags block_index, domain_enum domain,
4957 uint32_t namei, dwarf2_per_objfile *per_objfile)
4958 : m_map (map), m_block_index (block_index), m_domain (domain),
4959 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4960 m_per_objfile (per_objfile)
4961 {}
4962
4963 /* Return the next matching CU or NULL if there are no more. */
4964 dwarf2_per_cu_data *next ();
4965
4966 private:
4967 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4968 const char *name,
4969 dwarf2_per_objfile *per_objfile);
4970 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4971 uint32_t namei,
4972 dwarf2_per_objfile *per_objfile);
4973
4974 /* The internalized form of .debug_names. */
4975 const mapped_debug_names &m_map;
4976
4977 /* Restrict the search to these blocks. */
4978 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4979 | SEARCH_STATIC_BLOCK);
4980
4981 /* The kind of symbol we're looking for. */
4982 const domain_enum m_domain = UNDEF_DOMAIN;
4983 const search_domain m_search = ALL_DOMAIN;
4984
4985 /* The list of CUs from the index entry of the symbol, or NULL if
4986 not found. */
4987 const gdb_byte *m_addr;
4988
4989 dwarf2_per_objfile *m_per_objfile;
4990 };
4991
4992 const char *
4993 mapped_debug_names::namei_to_name
4994 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4995 {
4996 const ULONGEST namei_string_offs
4997 = extract_unsigned_integer ((name_table_string_offs_reordered
4998 + namei * offset_size),
4999 offset_size,
5000 dwarf5_byte_order);
5001 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5002 }
5003
5004 /* Find a slot in .debug_names for the object named NAME. If NAME is
5005 found, return pointer to its pool data. If NAME cannot be found,
5006 return NULL. */
5007
5008 const gdb_byte *
5009 dw2_debug_names_iterator::find_vec_in_debug_names
5010 (const mapped_debug_names &map, const char *name,
5011 dwarf2_per_objfile *per_objfile)
5012 {
5013 int (*cmp) (const char *, const char *);
5014
5015 gdb::unique_xmalloc_ptr<char> without_params;
5016 if (current_language->la_language == language_cplus
5017 || current_language->la_language == language_fortran
5018 || current_language->la_language == language_d)
5019 {
5020 /* NAME is already canonical. Drop any qualifiers as
5021 .debug_names does not contain any. */
5022
5023 if (strchr (name, '(') != NULL)
5024 {
5025 without_params = cp_remove_params (name);
5026 if (without_params != NULL)
5027 name = without_params.get ();
5028 }
5029 }
5030
5031 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5032
5033 const uint32_t full_hash = dwarf5_djb_hash (name);
5034 uint32_t namei
5035 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5036 (map.bucket_table_reordered
5037 + (full_hash % map.bucket_count)), 4,
5038 map.dwarf5_byte_order);
5039 if (namei == 0)
5040 return NULL;
5041 --namei;
5042 if (namei >= map.name_count)
5043 {
5044 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5045 "[in module %s]"),
5046 namei, map.name_count,
5047 objfile_name (per_objfile->objfile));
5048 return NULL;
5049 }
5050
5051 for (;;)
5052 {
5053 const uint32_t namei_full_hash
5054 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5055 (map.hash_table_reordered + namei), 4,
5056 map.dwarf5_byte_order);
5057 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5058 return NULL;
5059
5060 if (full_hash == namei_full_hash)
5061 {
5062 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5063
5064 #if 0 /* An expensive sanity check. */
5065 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5066 {
5067 complaint (_("Wrong .debug_names hash for string at index %u "
5068 "[in module %s]"),
5069 namei, objfile_name (dwarf2_per_objfile->objfile));
5070 return NULL;
5071 }
5072 #endif
5073
5074 if (cmp (namei_string, name) == 0)
5075 {
5076 const ULONGEST namei_entry_offs
5077 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5078 + namei * map.offset_size),
5079 map.offset_size, map.dwarf5_byte_order);
5080 return map.entry_pool + namei_entry_offs;
5081 }
5082 }
5083
5084 ++namei;
5085 if (namei >= map.name_count)
5086 return NULL;
5087 }
5088 }
5089
5090 const gdb_byte *
5091 dw2_debug_names_iterator::find_vec_in_debug_names
5092 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5093 {
5094 if (namei >= map.name_count)
5095 {
5096 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5097 "[in module %s]"),
5098 namei, map.name_count,
5099 objfile_name (per_objfile->objfile));
5100 return NULL;
5101 }
5102
5103 const ULONGEST namei_entry_offs
5104 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5105 + namei * map.offset_size),
5106 map.offset_size, map.dwarf5_byte_order);
5107 return map.entry_pool + namei_entry_offs;
5108 }
5109
5110 /* See dw2_debug_names_iterator. */
5111
5112 dwarf2_per_cu_data *
5113 dw2_debug_names_iterator::next ()
5114 {
5115 if (m_addr == NULL)
5116 return NULL;
5117
5118 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5119 struct objfile *objfile = m_per_objfile->objfile;
5120 bfd *const abfd = objfile->obfd;
5121
5122 again:
5123
5124 unsigned int bytes_read;
5125 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5126 m_addr += bytes_read;
5127 if (abbrev == 0)
5128 return NULL;
5129
5130 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5131 if (indexval_it == m_map.abbrev_map.cend ())
5132 {
5133 complaint (_("Wrong .debug_names undefined abbrev code %s "
5134 "[in module %s]"),
5135 pulongest (abbrev), objfile_name (objfile));
5136 return NULL;
5137 }
5138 const mapped_debug_names::index_val &indexval = indexval_it->second;
5139 enum class symbol_linkage {
5140 unknown,
5141 static_,
5142 extern_,
5143 } symbol_linkage_ = symbol_linkage::unknown;
5144 dwarf2_per_cu_data *per_cu = NULL;
5145 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5146 {
5147 ULONGEST ull;
5148 switch (attr.form)
5149 {
5150 case DW_FORM_implicit_const:
5151 ull = attr.implicit_const;
5152 break;
5153 case DW_FORM_flag_present:
5154 ull = 1;
5155 break;
5156 case DW_FORM_udata:
5157 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5158 m_addr += bytes_read;
5159 break;
5160 case DW_FORM_ref4:
5161 ull = read_4_bytes (abfd, m_addr);
5162 m_addr += 4;
5163 break;
5164 case DW_FORM_ref8:
5165 ull = read_8_bytes (abfd, m_addr);
5166 m_addr += 8;
5167 break;
5168 case DW_FORM_ref_sig8:
5169 ull = read_8_bytes (abfd, m_addr);
5170 m_addr += 8;
5171 break;
5172 default:
5173 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5174 dwarf_form_name (attr.form),
5175 objfile_name (objfile));
5176 return NULL;
5177 }
5178 switch (attr.dw_idx)
5179 {
5180 case DW_IDX_compile_unit:
5181 /* Don't crash on bad data. */
5182 if (ull >= per_bfd->all_comp_units.size ())
5183 {
5184 complaint (_(".debug_names entry has bad CU index %s"
5185 " [in module %s]"),
5186 pulongest (ull),
5187 objfile_name (objfile));
5188 continue;
5189 }
5190 per_cu = per_bfd->get_cu (ull);
5191 break;
5192 case DW_IDX_type_unit:
5193 /* Don't crash on bad data. */
5194 if (ull >= per_bfd->tu_stats.nr_tus)
5195 {
5196 complaint (_(".debug_names entry has bad TU index %s"
5197 " [in module %s]"),
5198 pulongest (ull),
5199 objfile_name (objfile));
5200 continue;
5201 }
5202 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5203 break;
5204 case DW_IDX_die_offset:
5205 /* In a per-CU index (as opposed to a per-module index), index
5206 entries without CU attribute implicitly refer to the single CU. */
5207 if (per_cu == NULL)
5208 per_cu = per_bfd->get_cu (0);
5209 break;
5210 case DW_IDX_GNU_internal:
5211 if (!m_map.augmentation_is_gdb)
5212 break;
5213 symbol_linkage_ = symbol_linkage::static_;
5214 break;
5215 case DW_IDX_GNU_external:
5216 if (!m_map.augmentation_is_gdb)
5217 break;
5218 symbol_linkage_ = symbol_linkage::extern_;
5219 break;
5220 }
5221 }
5222
5223 /* Skip if already read in. */
5224 if (m_per_objfile->symtab_set_p (per_cu))
5225 goto again;
5226
5227 /* Check static vs global. */
5228 if (symbol_linkage_ != symbol_linkage::unknown)
5229 {
5230 if (symbol_linkage_ == symbol_linkage::static_)
5231 {
5232 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5233 goto again;
5234 }
5235 else
5236 {
5237 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5238 goto again;
5239 }
5240 }
5241
5242 /* Match dw2_symtab_iter_next, symbol_kind
5243 and debug_names::psymbol_tag. */
5244 switch (m_domain)
5245 {
5246 case VAR_DOMAIN:
5247 switch (indexval.dwarf_tag)
5248 {
5249 case DW_TAG_variable:
5250 case DW_TAG_subprogram:
5251 /* Some types are also in VAR_DOMAIN. */
5252 case DW_TAG_typedef:
5253 case DW_TAG_structure_type:
5254 break;
5255 default:
5256 goto again;
5257 }
5258 break;
5259 case STRUCT_DOMAIN:
5260 switch (indexval.dwarf_tag)
5261 {
5262 case DW_TAG_typedef:
5263 case DW_TAG_structure_type:
5264 break;
5265 default:
5266 goto again;
5267 }
5268 break;
5269 case LABEL_DOMAIN:
5270 switch (indexval.dwarf_tag)
5271 {
5272 case 0:
5273 case DW_TAG_variable:
5274 break;
5275 default:
5276 goto again;
5277 }
5278 break;
5279 case MODULE_DOMAIN:
5280 switch (indexval.dwarf_tag)
5281 {
5282 case DW_TAG_module:
5283 break;
5284 default:
5285 goto again;
5286 }
5287 break;
5288 default:
5289 break;
5290 }
5291
5292 /* Match dw2_expand_symtabs_matching, symbol_kind and
5293 debug_names::psymbol_tag. */
5294 switch (m_search)
5295 {
5296 case VARIABLES_DOMAIN:
5297 switch (indexval.dwarf_tag)
5298 {
5299 case DW_TAG_variable:
5300 break;
5301 default:
5302 goto again;
5303 }
5304 break;
5305 case FUNCTIONS_DOMAIN:
5306 switch (indexval.dwarf_tag)
5307 {
5308 case DW_TAG_subprogram:
5309 break;
5310 default:
5311 goto again;
5312 }
5313 break;
5314 case TYPES_DOMAIN:
5315 switch (indexval.dwarf_tag)
5316 {
5317 case DW_TAG_typedef:
5318 case DW_TAG_structure_type:
5319 break;
5320 default:
5321 goto again;
5322 }
5323 break;
5324 case MODULES_DOMAIN:
5325 switch (indexval.dwarf_tag)
5326 {
5327 case DW_TAG_module:
5328 break;
5329 default:
5330 goto again;
5331 }
5332 default:
5333 break;
5334 }
5335
5336 return per_cu;
5337 }
5338
5339 /* This dumps minimal information about .debug_names. It is called
5340 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5341 uses this to verify that .debug_names has been loaded. */
5342
5343 void
5344 dwarf2_debug_names_index::dump (struct objfile *objfile)
5345 {
5346 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5347
5348 gdb_assert (per_objfile->per_bfd->using_index);
5349 printf_filtered (".debug_names:");
5350 if (per_objfile->per_bfd->debug_names_table)
5351 printf_filtered (" exists\n");
5352 else
5353 printf_filtered (" faked for \"readnow\"\n");
5354 printf_filtered ("\n");
5355 }
5356
5357 void
5358 dwarf2_debug_names_index::expand_matching_symbols
5359 (struct objfile *objfile,
5360 const lookup_name_info &name, domain_enum domain,
5361 int global,
5362 symbol_compare_ftype *ordered_compare)
5363 {
5364 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5365
5366 /* debug_names_table is NULL if OBJF_READNOW. */
5367 if (!per_objfile->per_bfd->debug_names_table)
5368 return;
5369
5370 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5371 const block_search_flags block_flags
5372 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5373
5374 const char *match_name = name.ada ().lookup_name ().c_str ();
5375 auto matcher = [&] (const char *symname)
5376 {
5377 if (ordered_compare == nullptr)
5378 return true;
5379 return ordered_compare (symname, match_name) == 0;
5380 };
5381
5382 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5383 [&] (offset_type namei)
5384 {
5385 /* The name was matched, now expand corresponding CUs that were
5386 marked. */
5387 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5388 per_objfile);
5389
5390 struct dwarf2_per_cu_data *per_cu;
5391 while ((per_cu = iter.next ()) != NULL)
5392 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5393 nullptr);
5394 return true;
5395 }, per_objfile);
5396 }
5397
5398 bool
5399 dwarf2_debug_names_index::expand_symtabs_matching
5400 (struct objfile *objfile,
5401 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5402 const lookup_name_info *lookup_name,
5403 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5404 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5405 block_search_flags search_flags,
5406 domain_enum domain,
5407 enum search_domain kind)
5408 {
5409 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5410
5411 /* debug_names_table is NULL if OBJF_READNOW. */
5412 if (!per_objfile->per_bfd->debug_names_table)
5413 return true;
5414
5415 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5416
5417 /* This invariant is documented in quick-functions.h. */
5418 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5419 if (lookup_name == nullptr)
5420 {
5421 for (dwarf2_per_cu_data *per_cu
5422 : all_comp_units_range (per_objfile->per_bfd))
5423 {
5424 QUIT;
5425
5426 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5427 file_matcher,
5428 expansion_notify))
5429 return false;
5430 }
5431 return true;
5432 }
5433
5434 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5435
5436 bool result
5437 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5438 symbol_matcher,
5439 [&] (offset_type namei)
5440 {
5441 /* The name was matched, now expand corresponding CUs that were
5442 marked. */
5443 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5444
5445 struct dwarf2_per_cu_data *per_cu;
5446 while ((per_cu = iter.next ()) != NULL)
5447 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5448 file_matcher,
5449 expansion_notify))
5450 return false;
5451 return true;
5452 }, per_objfile);
5453
5454 return result;
5455 }
5456
5457 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5458 to either a dwarf2_per_bfd or dwz_file object. */
5459
5460 template <typename T>
5461 static gdb::array_view<const gdb_byte>
5462 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5463 {
5464 dwarf2_section_info *section = &section_owner->gdb_index;
5465
5466 if (section->empty ())
5467 return {};
5468
5469 /* Older elfutils strip versions could keep the section in the main
5470 executable while splitting it for the separate debug info file. */
5471 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5472 return {};
5473
5474 section->read (obj);
5475
5476 /* dwarf2_section_info::size is a bfd_size_type, while
5477 gdb::array_view works with size_t. On 32-bit hosts, with
5478 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5479 is 32-bit. So we need an explicit narrowing conversion here.
5480 This is fine, because it's impossible to allocate or mmap an
5481 array/buffer larger than what size_t can represent. */
5482 return gdb::make_array_view (section->buffer, section->size);
5483 }
5484
5485 /* Lookup the index cache for the contents of the index associated to
5486 DWARF2_OBJ. */
5487
5488 static gdb::array_view<const gdb_byte>
5489 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5490 {
5491 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5492 if (build_id == nullptr)
5493 return {};
5494
5495 return global_index_cache.lookup_gdb_index (build_id,
5496 &dwarf2_per_bfd->index_cache_res);
5497 }
5498
5499 /* Same as the above, but for DWZ. */
5500
5501 static gdb::array_view<const gdb_byte>
5502 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5503 {
5504 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5505 if (build_id == nullptr)
5506 return {};
5507
5508 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5509 }
5510
5511 /* See dwarf2/public.h. */
5512
5513 void
5514 dwarf2_initialize_objfile (struct objfile *objfile)
5515 {
5516 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5517 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5518
5519 dwarf_read_debug_printf ("called");
5520
5521 /* If we're about to read full symbols, don't bother with the
5522 indices. In this case we also don't care if some other debug
5523 format is making psymtabs, because they are all about to be
5524 expanded anyway. */
5525 if ((objfile->flags & OBJF_READNOW))
5526 {
5527 dwarf_read_debug_printf ("readnow requested");
5528
5529 /* When using READNOW, the using_index flag (set below) indicates that
5530 PER_BFD was already initialized, when we loaded some other objfile. */
5531 if (per_bfd->using_index)
5532 {
5533 dwarf_read_debug_printf ("using_index already set");
5534 objfile->qf.push_front (make_dwarf_gdb_index ());
5535 return;
5536 }
5537
5538 per_bfd->using_index = 1;
5539 create_all_comp_units (per_objfile);
5540 per_bfd->quick_file_names_table
5541 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5542
5543 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5544 {
5545 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5546
5547 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5548 struct dwarf2_per_cu_quick_data);
5549 }
5550
5551 /* Arrange for gdb to see the "quick" functions. However, these
5552 functions will be no-ops because we will have expanded all
5553 symtabs. */
5554 objfile->qf.push_front (make_dwarf_gdb_index ());
5555 return;
5556 }
5557
5558 /* Was a debug names index already read when we processed an objfile sharing
5559 PER_BFD? */
5560 if (per_bfd->debug_names_table != nullptr)
5561 {
5562 dwarf_read_debug_printf ("re-using shared debug names table");
5563 objfile->qf.push_front (make_dwarf_debug_names ());
5564 return;
5565 }
5566
5567 /* Was a GDB index already read when we processed an objfile sharing
5568 PER_BFD? */
5569 if (per_bfd->index_table != nullptr)
5570 {
5571 dwarf_read_debug_printf ("re-using shared index table");
5572 objfile->qf.push_front (make_dwarf_gdb_index ());
5573 return;
5574 }
5575
5576 /* There might already be partial symtabs built for this BFD. This happens
5577 when loading the same binary twice with the index-cache enabled. If so,
5578 don't try to read an index. The objfile / per_objfile initialization will
5579 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5580 code path. */
5581 if (per_bfd->partial_symtabs != nullptr)
5582 {
5583 dwarf_read_debug_printf ("re-using shared partial symtabs");
5584 objfile->qf.push_front (make_lazy_dwarf_reader ());
5585 return;
5586 }
5587
5588 if (dwarf2_read_debug_names (per_objfile))
5589 {
5590 dwarf_read_debug_printf ("found debug names");
5591 objfile->qf.push_front (make_dwarf_debug_names ());
5592 return;
5593 }
5594
5595 if (dwarf2_read_gdb_index (per_objfile,
5596 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5597 get_gdb_index_contents_from_section<dwz_file>))
5598 {
5599 dwarf_read_debug_printf ("found gdb index from file");
5600 objfile->qf.push_front (make_dwarf_gdb_index ());
5601 return;
5602 }
5603
5604 /* ... otherwise, try to find the index in the index cache. */
5605 if (dwarf2_read_gdb_index (per_objfile,
5606 get_gdb_index_contents_from_cache,
5607 get_gdb_index_contents_from_cache_dwz))
5608 {
5609 dwarf_read_debug_printf ("found gdb index from cache");
5610 global_index_cache.hit ();
5611 objfile->qf.push_front (make_dwarf_gdb_index ());
5612 return;
5613 }
5614
5615 global_index_cache.miss ();
5616 objfile->qf.push_front (make_lazy_dwarf_reader ());
5617 }
5618
5619 \f
5620
5621 /* Build a partial symbol table. */
5622
5623 void
5624 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5625 {
5626 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5627 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5628
5629 if (per_bfd->partial_symtabs != nullptr)
5630 {
5631 /* Partial symbols were already read, so now we can simply
5632 attach them. */
5633 if (psf == nullptr)
5634 {
5635 psf = new psymbol_functions (per_bfd->partial_symtabs);
5636 objfile->qf.emplace_front (psf);
5637 }
5638 else
5639 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5640 return;
5641 }
5642
5643 if (psf == nullptr)
5644 {
5645 psf = new psymbol_functions;
5646 objfile->qf.emplace_front (psf);
5647 }
5648 const std::shared_ptr<psymtab_storage> &partial_symtabs
5649 = psf->get_partial_symtabs ();
5650
5651 /* Set the local reference to partial symtabs, so that we don't try
5652 to read them again if reading another objfile with the same BFD.
5653 If we can't in fact share, this won't make a difference anyway as
5654 the dwarf2_per_bfd object won't be shared. */
5655 per_bfd->partial_symtabs = partial_symtabs;
5656
5657 try
5658 {
5659 /* This isn't really ideal: all the data we allocate on the
5660 objfile's obstack is still uselessly kept around. However,
5661 freeing it seems unsafe. */
5662 psymtab_discarder psymtabs (partial_symtabs.get ());
5663 dwarf2_build_psymtabs_hard (per_objfile);
5664 psymtabs.keep ();
5665
5666 /* (maybe) store an index in the cache. */
5667 global_index_cache.store (per_objfile);
5668 }
5669 catch (const gdb_exception_error &except)
5670 {
5671 exception_print (gdb_stderr, except);
5672 }
5673 }
5674
5675 /* Find the base address of the compilation unit for range lists and
5676 location lists. It will normally be specified by DW_AT_low_pc.
5677 In DWARF-3 draft 4, the base address could be overridden by
5678 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5679 compilation units with discontinuous ranges. */
5680
5681 static void
5682 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5683 {
5684 struct attribute *attr;
5685
5686 cu->base_address.reset ();
5687
5688 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5689 if (attr != nullptr)
5690 cu->base_address = attr->as_address ();
5691 else
5692 {
5693 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5694 if (attr != nullptr)
5695 cu->base_address = attr->as_address ();
5696 }
5697 }
5698
5699 /* Helper function that returns the proper abbrev section for
5700 THIS_CU. */
5701
5702 static struct dwarf2_section_info *
5703 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5704 {
5705 struct dwarf2_section_info *abbrev;
5706 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5707
5708 if (this_cu->is_dwz)
5709 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5710 else
5711 abbrev = &per_bfd->abbrev;
5712
5713 return abbrev;
5714 }
5715
5716 /* Fetch the abbreviation table offset from a comp or type unit header. */
5717
5718 static sect_offset
5719 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5720 struct dwarf2_section_info *section,
5721 sect_offset sect_off)
5722 {
5723 bfd *abfd = section->get_bfd_owner ();
5724 const gdb_byte *info_ptr;
5725 unsigned int initial_length_size, offset_size;
5726 uint16_t version;
5727
5728 section->read (per_objfile->objfile);
5729 info_ptr = section->buffer + to_underlying (sect_off);
5730 read_initial_length (abfd, info_ptr, &initial_length_size);
5731 offset_size = initial_length_size == 4 ? 4 : 8;
5732 info_ptr += initial_length_size;
5733
5734 version = read_2_bytes (abfd, info_ptr);
5735 info_ptr += 2;
5736 if (version >= 5)
5737 {
5738 /* Skip unit type and address size. */
5739 info_ptr += 2;
5740 }
5741
5742 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5743 }
5744
5745 /* A partial symtab that is used only for include files. */
5746 struct dwarf2_include_psymtab : public partial_symtab
5747 {
5748 dwarf2_include_psymtab (const char *filename,
5749 psymtab_storage *partial_symtabs,
5750 objfile_per_bfd_storage *objfile_per_bfd)
5751 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5752 {
5753 }
5754
5755 void read_symtab (struct objfile *objfile) override
5756 {
5757 /* It's an include file, no symbols to read for it.
5758 Everything is in the includer symtab. */
5759
5760 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5761 expansion of the includer psymtab. We use the dependencies[0] field to
5762 model the includer. But if we go the regular route of calling
5763 expand_psymtab here, and having expand_psymtab call expand_dependencies
5764 to expand the includer, we'll only use expand_psymtab on the includer
5765 (making it a non-toplevel psymtab), while if we expand the includer via
5766 another path, we'll use read_symtab (making it a toplevel psymtab).
5767 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5768 psymtab, and trigger read_symtab on the includer here directly. */
5769 includer ()->read_symtab (objfile);
5770 }
5771
5772 void expand_psymtab (struct objfile *objfile) override
5773 {
5774 /* This is not called by read_symtab, and should not be called by any
5775 expand_dependencies. */
5776 gdb_assert (false);
5777 }
5778
5779 bool readin_p (struct objfile *objfile) const override
5780 {
5781 return includer ()->readin_p (objfile);
5782 }
5783
5784 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5785 {
5786 return nullptr;
5787 }
5788
5789 private:
5790 partial_symtab *includer () const
5791 {
5792 /* An include psymtab has exactly one dependency: the psymtab that
5793 includes it. */
5794 gdb_assert (this->number_of_dependencies == 1);
5795 return this->dependencies[0];
5796 }
5797 };
5798
5799 /* Allocate a new partial symtab for file named NAME and mark this new
5800 partial symtab as being an include of PST. */
5801
5802 static void
5803 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5804 const char *name,
5805 dwarf2_psymtab *pst,
5806 psymtab_storage *partial_symtabs,
5807 objfile_per_bfd_storage *objfile_per_bfd)
5808 {
5809 dwarf2_include_psymtab *subpst
5810 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5811
5812 if (!IS_ABSOLUTE_PATH (subpst->filename))
5813 subpst->dirname = pst->dirname;
5814
5815 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5816 subpst->dependencies[0] = pst;
5817 subpst->number_of_dependencies = 1;
5818 }
5819
5820 /* Read the Line Number Program data and extract the list of files
5821 included by the source file represented by PST. Build an include
5822 partial symtab for each of these included files. */
5823
5824 static void
5825 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5826 struct die_info *die,
5827 const file_and_directory &fnd,
5828 dwarf2_psymtab *pst)
5829 {
5830 line_header_up lh;
5831 struct attribute *attr;
5832
5833 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5834 if (attr != nullptr && attr->form_is_unsigned ())
5835 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5836 if (lh == NULL)
5837 return; /* No linetable, so no includes. */
5838
5839 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5840 that we pass in the raw text_low here; that is ok because we're
5841 only decoding the line table to make include partial symtabs, and
5842 so the addresses aren't really used. */
5843 dwarf_decode_lines (lh.get (), fnd, cu, pst,
5844 pst->raw_text_low (), 1);
5845 }
5846
5847 static hashval_t
5848 hash_signatured_type (const void *item)
5849 {
5850 const struct signatured_type *sig_type
5851 = (const struct signatured_type *) item;
5852
5853 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5854 return sig_type->signature;
5855 }
5856
5857 static int
5858 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5859 {
5860 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5861 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5862
5863 return lhs->signature == rhs->signature;
5864 }
5865
5866 /* Allocate a hash table for signatured types. */
5867
5868 static htab_up
5869 allocate_signatured_type_table ()
5870 {
5871 return htab_up (htab_create_alloc (41,
5872 hash_signatured_type,
5873 eq_signatured_type,
5874 NULL, xcalloc, xfree));
5875 }
5876
5877 /* A helper for create_debug_types_hash_table. Read types from SECTION
5878 and fill them into TYPES_HTAB. It will process only type units,
5879 therefore DW_UT_type. */
5880
5881 static void
5882 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5883 struct dwo_file *dwo_file,
5884 dwarf2_section_info *section, htab_up &types_htab,
5885 rcuh_kind section_kind)
5886 {
5887 struct objfile *objfile = per_objfile->objfile;
5888 struct dwarf2_section_info *abbrev_section;
5889 bfd *abfd;
5890 const gdb_byte *info_ptr, *end_ptr;
5891
5892 abbrev_section = &dwo_file->sections.abbrev;
5893
5894 dwarf_read_debug_printf ("Reading %s for %s",
5895 section->get_name (),
5896 abbrev_section->get_file_name ());
5897
5898 section->read (objfile);
5899 info_ptr = section->buffer;
5900
5901 if (info_ptr == NULL)
5902 return;
5903
5904 /* We can't set abfd until now because the section may be empty or
5905 not present, in which case the bfd is unknown. */
5906 abfd = section->get_bfd_owner ();
5907
5908 /* We don't use cutu_reader here because we don't need to read
5909 any dies: the signature is in the header. */
5910
5911 end_ptr = info_ptr + section->size;
5912 while (info_ptr < end_ptr)
5913 {
5914 signatured_type_up sig_type;
5915 struct dwo_unit *dwo_tu;
5916 void **slot;
5917 const gdb_byte *ptr = info_ptr;
5918 struct comp_unit_head header;
5919 unsigned int length;
5920
5921 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5922
5923 /* Initialize it due to a false compiler warning. */
5924 header.signature = -1;
5925 header.type_cu_offset_in_tu = (cu_offset) -1;
5926
5927 /* We need to read the type's signature in order to build the hash
5928 table, but we don't need anything else just yet. */
5929
5930 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5931 abbrev_section, ptr, section_kind);
5932
5933 length = header.get_length ();
5934
5935 /* Skip dummy type units. */
5936 if (ptr >= info_ptr + length
5937 || peek_abbrev_code (abfd, ptr) == 0
5938 || (header.unit_type != DW_UT_type
5939 && header.unit_type != DW_UT_split_type))
5940 {
5941 info_ptr += length;
5942 continue;
5943 }
5944
5945 if (types_htab == NULL)
5946 types_htab = allocate_dwo_unit_table ();
5947
5948 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5949 dwo_tu->dwo_file = dwo_file;
5950 dwo_tu->signature = header.signature;
5951 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5952 dwo_tu->section = section;
5953 dwo_tu->sect_off = sect_off;
5954 dwo_tu->length = length;
5955
5956 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5957 gdb_assert (slot != NULL);
5958 if (*slot != NULL)
5959 complaint (_("debug type entry at offset %s is duplicate to"
5960 " the entry at offset %s, signature %s"),
5961 sect_offset_str (sect_off),
5962 sect_offset_str (dwo_tu->sect_off),
5963 hex_string (header.signature));
5964 *slot = dwo_tu;
5965
5966 dwarf_read_debug_printf_v (" offset %s, signature %s",
5967 sect_offset_str (sect_off),
5968 hex_string (header.signature));
5969
5970 info_ptr += length;
5971 }
5972 }
5973
5974 /* Create the hash table of all entries in the .debug_types
5975 (or .debug_types.dwo) section(s).
5976 DWO_FILE is a pointer to the DWO file object.
5977
5978 The result is a pointer to the hash table or NULL if there are no types.
5979
5980 Note: This function processes DWO files only, not DWP files. */
5981
5982 static void
5983 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5984 struct dwo_file *dwo_file,
5985 gdb::array_view<dwarf2_section_info> type_sections,
5986 htab_up &types_htab)
5987 {
5988 for (dwarf2_section_info &section : type_sections)
5989 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5990 rcuh_kind::TYPE);
5991 }
5992
5993 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5994 If SLOT is non-NULL, it is the entry to use in the hash table.
5995 Otherwise we find one. */
5996
5997 static struct signatured_type *
5998 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5999 {
6000 if (per_objfile->per_bfd->all_comp_units.size ()
6001 == per_objfile->per_bfd->all_comp_units.capacity ())
6002 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6003
6004 signatured_type_up sig_type_holder
6005 = per_objfile->per_bfd->allocate_signatured_type (sig);
6006 signatured_type *sig_type = sig_type_holder.get ();
6007
6008 per_objfile->per_bfd->all_comp_units.emplace_back
6009 (sig_type_holder.release ());
6010 if (per_objfile->per_bfd->using_index)
6011 {
6012 sig_type->v.quick =
6013 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6014 struct dwarf2_per_cu_quick_data);
6015 }
6016
6017 if (slot == NULL)
6018 {
6019 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6020 sig_type, INSERT);
6021 }
6022 gdb_assert (*slot == NULL);
6023 *slot = sig_type;
6024 /* The rest of sig_type must be filled in by the caller. */
6025 return sig_type;
6026 }
6027
6028 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6029 Fill in SIG_ENTRY with DWO_ENTRY. */
6030
6031 static void
6032 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6033 struct signatured_type *sig_entry,
6034 struct dwo_unit *dwo_entry)
6035 {
6036 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6037
6038 /* Make sure we're not clobbering something we don't expect to. */
6039 gdb_assert (! sig_entry->queued);
6040 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6041 if (per_bfd->using_index)
6042 {
6043 gdb_assert (sig_entry->v.quick != NULL);
6044 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6045 }
6046 else
6047 gdb_assert (sig_entry->v.psymtab == NULL);
6048 gdb_assert (sig_entry->signature == dwo_entry->signature);
6049 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6050 gdb_assert (sig_entry->type_unit_group == NULL);
6051 gdb_assert (sig_entry->dwo_unit == NULL);
6052
6053 sig_entry->section = dwo_entry->section;
6054 sig_entry->sect_off = dwo_entry->sect_off;
6055 sig_entry->length = dwo_entry->length;
6056 sig_entry->reading_dwo_directly = 1;
6057 sig_entry->per_bfd = per_bfd;
6058 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6059 sig_entry->dwo_unit = dwo_entry;
6060 }
6061
6062 /* Subroutine of lookup_signatured_type.
6063 If we haven't read the TU yet, create the signatured_type data structure
6064 for a TU to be read in directly from a DWO file, bypassing the stub.
6065 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6066 using .gdb_index, then when reading a CU we want to stay in the DWO file
6067 containing that CU. Otherwise we could end up reading several other DWO
6068 files (due to comdat folding) to process the transitive closure of all the
6069 mentioned TUs, and that can be slow. The current DWO file will have every
6070 type signature that it needs.
6071 We only do this for .gdb_index because in the psymtab case we already have
6072 to read all the DWOs to build the type unit groups. */
6073
6074 static struct signatured_type *
6075 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6076 {
6077 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6078 struct dwo_file *dwo_file;
6079 struct dwo_unit find_dwo_entry, *dwo_entry;
6080 void **slot;
6081
6082 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6083
6084 /* If TU skeletons have been removed then we may not have read in any
6085 TUs yet. */
6086 if (per_objfile->per_bfd->signatured_types == NULL)
6087 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6088
6089 /* We only ever need to read in one copy of a signatured type.
6090 Use the global signatured_types array to do our own comdat-folding
6091 of types. If this is the first time we're reading this TU, and
6092 the TU has an entry in .gdb_index, replace the recorded data from
6093 .gdb_index with this TU. */
6094
6095 signatured_type find_sig_entry (sig);
6096 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6097 &find_sig_entry, INSERT);
6098 signatured_type *sig_entry = (struct signatured_type *) *slot;
6099
6100 /* We can get here with the TU already read, *or* in the process of being
6101 read. Don't reassign the global entry to point to this DWO if that's
6102 the case. Also note that if the TU is already being read, it may not
6103 have come from a DWO, the program may be a mix of Fission-compiled
6104 code and non-Fission-compiled code. */
6105
6106 /* Have we already tried to read this TU?
6107 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6108 needn't exist in the global table yet). */
6109 if (sig_entry != NULL && sig_entry->tu_read)
6110 return sig_entry;
6111
6112 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6113 dwo_unit of the TU itself. */
6114 dwo_file = cu->dwo_unit->dwo_file;
6115
6116 /* Ok, this is the first time we're reading this TU. */
6117 if (dwo_file->tus == NULL)
6118 return NULL;
6119 find_dwo_entry.signature = sig;
6120 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6121 &find_dwo_entry);
6122 if (dwo_entry == NULL)
6123 return NULL;
6124
6125 /* If the global table doesn't have an entry for this TU, add one. */
6126 if (sig_entry == NULL)
6127 sig_entry = add_type_unit (per_objfile, sig, slot);
6128
6129 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6130 sig_entry->tu_read = 1;
6131 return sig_entry;
6132 }
6133
6134 /* Subroutine of lookup_signatured_type.
6135 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6136 then try the DWP file. If the TU stub (skeleton) has been removed then
6137 it won't be in .gdb_index. */
6138
6139 static struct signatured_type *
6140 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6141 {
6142 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6143 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6144 struct dwo_unit *dwo_entry;
6145 void **slot;
6146
6147 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6148 gdb_assert (dwp_file != NULL);
6149
6150 /* If TU skeletons have been removed then we may not have read in any
6151 TUs yet. */
6152 if (per_objfile->per_bfd->signatured_types == NULL)
6153 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6154
6155 signatured_type find_sig_entry (sig);
6156 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6157 &find_sig_entry, INSERT);
6158 signatured_type *sig_entry = (struct signatured_type *) *slot;
6159
6160 /* Have we already tried to read this TU?
6161 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6162 needn't exist in the global table yet). */
6163 if (sig_entry != NULL)
6164 return sig_entry;
6165
6166 if (dwp_file->tus == NULL)
6167 return NULL;
6168 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6169 1 /* is_debug_types */);
6170 if (dwo_entry == NULL)
6171 return NULL;
6172
6173 sig_entry = add_type_unit (per_objfile, sig, slot);
6174 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6175
6176 return sig_entry;
6177 }
6178
6179 /* Lookup a signature based type for DW_FORM_ref_sig8.
6180 Returns NULL if signature SIG is not present in the table.
6181 It is up to the caller to complain about this. */
6182
6183 static struct signatured_type *
6184 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6185 {
6186 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6187
6188 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6189 {
6190 /* We're in a DWO/DWP file, and we're using .gdb_index.
6191 These cases require special processing. */
6192 if (get_dwp_file (per_objfile) == NULL)
6193 return lookup_dwo_signatured_type (cu, sig);
6194 else
6195 return lookup_dwp_signatured_type (cu, sig);
6196 }
6197 else
6198 {
6199 if (per_objfile->per_bfd->signatured_types == NULL)
6200 return NULL;
6201 signatured_type find_entry (sig);
6202 return ((struct signatured_type *)
6203 htab_find (per_objfile->per_bfd->signatured_types.get (),
6204 &find_entry));
6205 }
6206 }
6207
6208 /* Low level DIE reading support. */
6209
6210 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6211
6212 static void
6213 init_cu_die_reader (struct die_reader_specs *reader,
6214 struct dwarf2_cu *cu,
6215 struct dwarf2_section_info *section,
6216 struct dwo_file *dwo_file,
6217 struct abbrev_table *abbrev_table)
6218 {
6219 gdb_assert (section->readin && section->buffer != NULL);
6220 reader->abfd = section->get_bfd_owner ();
6221 reader->cu = cu;
6222 reader->dwo_file = dwo_file;
6223 reader->die_section = section;
6224 reader->buffer = section->buffer;
6225 reader->buffer_end = section->buffer + section->size;
6226 reader->abbrev_table = abbrev_table;
6227 }
6228
6229 /* Subroutine of cutu_reader to simplify it.
6230 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6231 There's just a lot of work to do, and cutu_reader is big enough
6232 already.
6233
6234 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6235 from it to the DIE in the DWO. If NULL we are skipping the stub.
6236 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6237 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6238 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6239 STUB_COMP_DIR may be non-NULL.
6240 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6241 are filled in with the info of the DIE from the DWO file.
6242 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6243 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6244 kept around for at least as long as *RESULT_READER.
6245
6246 The result is non-zero if a valid (non-dummy) DIE was found. */
6247
6248 static int
6249 read_cutu_die_from_dwo (dwarf2_cu *cu,
6250 struct dwo_unit *dwo_unit,
6251 struct die_info *stub_comp_unit_die,
6252 const char *stub_comp_dir,
6253 struct die_reader_specs *result_reader,
6254 const gdb_byte **result_info_ptr,
6255 struct die_info **result_comp_unit_die,
6256 abbrev_table_up *result_dwo_abbrev_table)
6257 {
6258 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6259 dwarf2_per_cu_data *per_cu = cu->per_cu;
6260 struct objfile *objfile = per_objfile->objfile;
6261 bfd *abfd;
6262 const gdb_byte *begin_info_ptr, *info_ptr;
6263 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6264 int i,num_extra_attrs;
6265 struct dwarf2_section_info *dwo_abbrev_section;
6266 struct die_info *comp_unit_die;
6267
6268 /* At most one of these may be provided. */
6269 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6270
6271 /* These attributes aren't processed until later:
6272 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6273 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6274 referenced later. However, these attributes are found in the stub
6275 which we won't have later. In order to not impose this complication
6276 on the rest of the code, we read them here and copy them to the
6277 DWO CU/TU die. */
6278
6279 stmt_list = NULL;
6280 low_pc = NULL;
6281 high_pc = NULL;
6282 ranges = NULL;
6283 comp_dir = NULL;
6284
6285 if (stub_comp_unit_die != NULL)
6286 {
6287 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6288 DWO file. */
6289 if (!per_cu->is_debug_types)
6290 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6291 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6292 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6293 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6294 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6295
6296 cu->addr_base = stub_comp_unit_die->addr_base ();
6297
6298 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6299 We need the value before we can process DW_AT_ranges values from the
6300 DWO. */
6301 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6302
6303 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6304 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6305 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6306 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6307 section. */
6308 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6309 }
6310 else if (stub_comp_dir != NULL)
6311 {
6312 /* Reconstruct the comp_dir attribute to simplify the code below. */
6313 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6314 comp_dir->name = DW_AT_comp_dir;
6315 comp_dir->form = DW_FORM_string;
6316 comp_dir->set_string_noncanonical (stub_comp_dir);
6317 }
6318
6319 /* Set up for reading the DWO CU/TU. */
6320 cu->dwo_unit = dwo_unit;
6321 dwarf2_section_info *section = dwo_unit->section;
6322 section->read (objfile);
6323 abfd = section->get_bfd_owner ();
6324 begin_info_ptr = info_ptr = (section->buffer
6325 + to_underlying (dwo_unit->sect_off));
6326 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6327
6328 if (per_cu->is_debug_types)
6329 {
6330 signatured_type *sig_type = (struct signatured_type *) per_cu;
6331
6332 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6333 section, dwo_abbrev_section,
6334 info_ptr, rcuh_kind::TYPE);
6335 /* This is not an assert because it can be caused by bad debug info. */
6336 if (sig_type->signature != cu->header.signature)
6337 {
6338 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6339 " TU at offset %s [in module %s]"),
6340 hex_string (sig_type->signature),
6341 hex_string (cu->header.signature),
6342 sect_offset_str (dwo_unit->sect_off),
6343 bfd_get_filename (abfd));
6344 }
6345 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6346 /* For DWOs coming from DWP files, we don't know the CU length
6347 nor the type's offset in the TU until now. */
6348 dwo_unit->length = cu->header.get_length ();
6349 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6350
6351 /* Establish the type offset that can be used to lookup the type.
6352 For DWO files, we don't know it until now. */
6353 sig_type->type_offset_in_section
6354 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6355 }
6356 else
6357 {
6358 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6359 section, dwo_abbrev_section,
6360 info_ptr, rcuh_kind::COMPILE);
6361 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6362 /* For DWOs coming from DWP files, we don't know the CU length
6363 until now. */
6364 dwo_unit->length = cu->header.get_length ();
6365 }
6366
6367 dwo_abbrev_section->read (objfile);
6368 *result_dwo_abbrev_table
6369 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6370 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6371 result_dwo_abbrev_table->get ());
6372
6373 /* Read in the die, but leave space to copy over the attributes
6374 from the stub. This has the benefit of simplifying the rest of
6375 the code - all the work to maintain the illusion of a single
6376 DW_TAG_{compile,type}_unit DIE is done here. */
6377 num_extra_attrs = ((stmt_list != NULL)
6378 + (low_pc != NULL)
6379 + (high_pc != NULL)
6380 + (ranges != NULL)
6381 + (comp_dir != NULL));
6382 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6383 num_extra_attrs);
6384
6385 /* Copy over the attributes from the stub to the DIE we just read in. */
6386 comp_unit_die = *result_comp_unit_die;
6387 i = comp_unit_die->num_attrs;
6388 if (stmt_list != NULL)
6389 comp_unit_die->attrs[i++] = *stmt_list;
6390 if (low_pc != NULL)
6391 comp_unit_die->attrs[i++] = *low_pc;
6392 if (high_pc != NULL)
6393 comp_unit_die->attrs[i++] = *high_pc;
6394 if (ranges != NULL)
6395 comp_unit_die->attrs[i++] = *ranges;
6396 if (comp_dir != NULL)
6397 comp_unit_die->attrs[i++] = *comp_dir;
6398 comp_unit_die->num_attrs += num_extra_attrs;
6399
6400 if (dwarf_die_debug)
6401 {
6402 fprintf_unfiltered (gdb_stdlog,
6403 "Read die from %s@0x%x of %s:\n",
6404 section->get_name (),
6405 (unsigned) (begin_info_ptr - section->buffer),
6406 bfd_get_filename (abfd));
6407 dump_die (comp_unit_die, dwarf_die_debug);
6408 }
6409
6410 /* Skip dummy compilation units. */
6411 if (info_ptr >= begin_info_ptr + dwo_unit->length
6412 || peek_abbrev_code (abfd, info_ptr) == 0)
6413 return 0;
6414
6415 *result_info_ptr = info_ptr;
6416 return 1;
6417 }
6418
6419 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6420 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6421 signature is part of the header. */
6422 static gdb::optional<ULONGEST>
6423 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6424 {
6425 if (cu->header.version >= 5)
6426 return cu->header.signature;
6427 struct attribute *attr;
6428 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6429 if (attr == nullptr || !attr->form_is_unsigned ())
6430 return gdb::optional<ULONGEST> ();
6431 return attr->as_unsigned ();
6432 }
6433
6434 /* Subroutine of cutu_reader to simplify it.
6435 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6436 Returns NULL if the specified DWO unit cannot be found. */
6437
6438 static struct dwo_unit *
6439 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6440 {
6441 dwarf2_per_cu_data *per_cu = cu->per_cu;
6442 struct dwo_unit *dwo_unit;
6443 const char *comp_dir;
6444
6445 gdb_assert (cu != NULL);
6446
6447 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6448 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6449 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6450
6451 if (per_cu->is_debug_types)
6452 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6453 else
6454 {
6455 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6456
6457 if (!signature.has_value ())
6458 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6459 " [in module %s]"),
6460 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6461
6462 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6463 }
6464
6465 return dwo_unit;
6466 }
6467
6468 /* Subroutine of cutu_reader to simplify it.
6469 See it for a description of the parameters.
6470 Read a TU directly from a DWO file, bypassing the stub. */
6471
6472 void
6473 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6474 dwarf2_per_objfile *per_objfile,
6475 dwarf2_cu *existing_cu)
6476 {
6477 struct signatured_type *sig_type;
6478
6479 /* Verify we can do the following downcast, and that we have the
6480 data we need. */
6481 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6482 sig_type = (struct signatured_type *) this_cu;
6483 gdb_assert (sig_type->dwo_unit != NULL);
6484
6485 dwarf2_cu *cu;
6486
6487 if (existing_cu != nullptr)
6488 {
6489 cu = existing_cu;
6490 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6491 /* There's no need to do the rereading_dwo_cu handling that
6492 cutu_reader does since we don't read the stub. */
6493 }
6494 else
6495 {
6496 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6497 in per_objfile yet. */
6498 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6499 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6500 cu = m_new_cu.get ();
6501 }
6502
6503 /* A future optimization, if needed, would be to use an existing
6504 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6505 could share abbrev tables. */
6506
6507 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6508 NULL /* stub_comp_unit_die */,
6509 sig_type->dwo_unit->dwo_file->comp_dir,
6510 this, &info_ptr,
6511 &comp_unit_die,
6512 &m_dwo_abbrev_table) == 0)
6513 {
6514 /* Dummy die. */
6515 dummy_p = true;
6516 }
6517 }
6518
6519 /* Initialize a CU (or TU) and read its DIEs.
6520 If the CU defers to a DWO file, read the DWO file as well.
6521
6522 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6523 Otherwise the table specified in the comp unit header is read in and used.
6524 This is an optimization for when we already have the abbrev table.
6525
6526 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6527 allocated. */
6528
6529 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6530 dwarf2_per_objfile *per_objfile,
6531 struct abbrev_table *abbrev_table,
6532 dwarf2_cu *existing_cu,
6533 bool skip_partial)
6534 : die_reader_specs {},
6535 m_this_cu (this_cu)
6536 {
6537 struct objfile *objfile = per_objfile->objfile;
6538 struct dwarf2_section_info *section = this_cu->section;
6539 bfd *abfd = section->get_bfd_owner ();
6540 const gdb_byte *begin_info_ptr;
6541 struct signatured_type *sig_type = NULL;
6542 struct dwarf2_section_info *abbrev_section;
6543 /* Non-zero if CU currently points to a DWO file and we need to
6544 reread it. When this happens we need to reread the skeleton die
6545 before we can reread the DWO file (this only applies to CUs, not TUs). */
6546 int rereading_dwo_cu = 0;
6547
6548 if (dwarf_die_debug)
6549 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6550 this_cu->is_debug_types ? "type" : "comp",
6551 sect_offset_str (this_cu->sect_off));
6552
6553 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6554 file (instead of going through the stub), short-circuit all of this. */
6555 if (this_cu->reading_dwo_directly)
6556 {
6557 /* Narrow down the scope of possibilities to have to understand. */
6558 gdb_assert (this_cu->is_debug_types);
6559 gdb_assert (abbrev_table == NULL);
6560 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6561 return;
6562 }
6563
6564 /* This is cheap if the section is already read in. */
6565 section->read (objfile);
6566
6567 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6568
6569 abbrev_section = get_abbrev_section_for_cu (this_cu);
6570
6571 dwarf2_cu *cu;
6572
6573 if (existing_cu != nullptr)
6574 {
6575 cu = existing_cu;
6576 /* If this CU is from a DWO file we need to start over, we need to
6577 refetch the attributes from the skeleton CU.
6578 This could be optimized by retrieving those attributes from when we
6579 were here the first time: the previous comp_unit_die was stored in
6580 comp_unit_obstack. But there's no data yet that we need this
6581 optimization. */
6582 if (cu->dwo_unit != NULL)
6583 rereading_dwo_cu = 1;
6584 }
6585 else
6586 {
6587 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6588 in per_objfile yet. */
6589 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6590 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6591 cu = m_new_cu.get ();
6592 }
6593
6594 /* Get the header. */
6595 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6596 {
6597 /* We already have the header, there's no need to read it in again. */
6598 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6599 }
6600 else
6601 {
6602 if (this_cu->is_debug_types)
6603 {
6604 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6605 section, abbrev_section,
6606 info_ptr, rcuh_kind::TYPE);
6607
6608 /* Since per_cu is the first member of struct signatured_type,
6609 we can go from a pointer to one to a pointer to the other. */
6610 sig_type = (struct signatured_type *) this_cu;
6611 gdb_assert (sig_type->signature == cu->header.signature);
6612 gdb_assert (sig_type->type_offset_in_tu
6613 == cu->header.type_cu_offset_in_tu);
6614 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6615
6616 /* LENGTH has not been set yet for type units if we're
6617 using .gdb_index. */
6618 this_cu->length = cu->header.get_length ();
6619
6620 /* Establish the type offset that can be used to lookup the type. */
6621 sig_type->type_offset_in_section =
6622 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6623
6624 this_cu->dwarf_version = cu->header.version;
6625 }
6626 else
6627 {
6628 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6629 section, abbrev_section,
6630 info_ptr,
6631 rcuh_kind::COMPILE);
6632
6633 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6634 if (this_cu->length == 0)
6635 this_cu->length = cu->header.get_length ();
6636 else
6637 gdb_assert (this_cu->length == cu->header.get_length ());
6638 this_cu->dwarf_version = cu->header.version;
6639 }
6640 }
6641
6642 /* Skip dummy compilation units. */
6643 if (info_ptr >= begin_info_ptr + this_cu->length
6644 || peek_abbrev_code (abfd, info_ptr) == 0)
6645 {
6646 dummy_p = true;
6647 return;
6648 }
6649
6650 /* If we don't have them yet, read the abbrevs for this compilation unit.
6651 And if we need to read them now, make sure they're freed when we're
6652 done. */
6653 if (abbrev_table != NULL)
6654 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6655 else
6656 {
6657 abbrev_section->read (objfile);
6658 m_abbrev_table_holder
6659 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6660 abbrev_table = m_abbrev_table_holder.get ();
6661 }
6662
6663 /* Read the top level CU/TU die. */
6664 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6665 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6666
6667 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6668 {
6669 dummy_p = true;
6670 return;
6671 }
6672
6673 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6674 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6675 table from the DWO file and pass the ownership over to us. It will be
6676 referenced from READER, so we must make sure to free it after we're done
6677 with READER.
6678
6679 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6680 DWO CU, that this test will fail (the attribute will not be present). */
6681 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6682 if (dwo_name != nullptr)
6683 {
6684 struct dwo_unit *dwo_unit;
6685 struct die_info *dwo_comp_unit_die;
6686
6687 if (comp_unit_die->has_children)
6688 {
6689 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6690 " has children (offset %s) [in module %s]"),
6691 sect_offset_str (this_cu->sect_off),
6692 bfd_get_filename (abfd));
6693 }
6694 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6695 if (dwo_unit != NULL)
6696 {
6697 if (read_cutu_die_from_dwo (cu, dwo_unit,
6698 comp_unit_die, NULL,
6699 this, &info_ptr,
6700 &dwo_comp_unit_die,
6701 &m_dwo_abbrev_table) == 0)
6702 {
6703 /* Dummy die. */
6704 dummy_p = true;
6705 return;
6706 }
6707 comp_unit_die = dwo_comp_unit_die;
6708 }
6709 else
6710 {
6711 /* Yikes, we couldn't find the rest of the DIE, we only have
6712 the stub. A complaint has already been logged. There's
6713 not much more we can do except pass on the stub DIE to
6714 die_reader_func. We don't want to throw an error on bad
6715 debug info. */
6716 }
6717 }
6718 }
6719
6720 void
6721 cutu_reader::keep ()
6722 {
6723 /* Done, clean up. */
6724 gdb_assert (!dummy_p);
6725 if (m_new_cu != NULL)
6726 {
6727 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6728 now. */
6729 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6730 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6731 }
6732 }
6733
6734 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6735 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6736 assumed to have already done the lookup to find the DWO file).
6737
6738 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6739 THIS_CU->is_debug_types, but nothing else.
6740
6741 We fill in THIS_CU->length.
6742
6743 THIS_CU->cu is always freed when done.
6744 This is done in order to not leave THIS_CU->cu in a state where we have
6745 to care whether it refers to the "main" CU or the DWO CU.
6746
6747 When parent_cu is passed, it is used to provide a default value for
6748 str_offsets_base and addr_base from the parent. */
6749
6750 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6751 dwarf2_per_objfile *per_objfile,
6752 struct dwarf2_cu *parent_cu,
6753 struct dwo_file *dwo_file)
6754 : die_reader_specs {},
6755 m_this_cu (this_cu)
6756 {
6757 struct objfile *objfile = per_objfile->objfile;
6758 struct dwarf2_section_info *section = this_cu->section;
6759 bfd *abfd = section->get_bfd_owner ();
6760 struct dwarf2_section_info *abbrev_section;
6761 const gdb_byte *begin_info_ptr, *info_ptr;
6762
6763 if (dwarf_die_debug)
6764 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6765 this_cu->is_debug_types ? "type" : "comp",
6766 sect_offset_str (this_cu->sect_off));
6767
6768 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6769
6770 abbrev_section = (dwo_file != NULL
6771 ? &dwo_file->sections.abbrev
6772 : get_abbrev_section_for_cu (this_cu));
6773
6774 /* This is cheap if the section is already read in. */
6775 section->read (objfile);
6776
6777 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6778
6779 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6780 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6781 section, abbrev_section, info_ptr,
6782 (this_cu->is_debug_types
6783 ? rcuh_kind::TYPE
6784 : rcuh_kind::COMPILE));
6785
6786 if (parent_cu != nullptr)
6787 {
6788 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6789 m_new_cu->addr_base = parent_cu->addr_base;
6790 }
6791 this_cu->length = m_new_cu->header.get_length ();
6792
6793 /* Skip dummy compilation units. */
6794 if (info_ptr >= begin_info_ptr + this_cu->length
6795 || peek_abbrev_code (abfd, info_ptr) == 0)
6796 {
6797 dummy_p = true;
6798 return;
6799 }
6800
6801 abbrev_section->read (objfile);
6802 m_abbrev_table_holder
6803 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6804
6805 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6806 m_abbrev_table_holder.get ());
6807 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6808 }
6809
6810 \f
6811 /* Type Unit Groups.
6812
6813 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6814 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6815 so that all types coming from the same compilation (.o file) are grouped
6816 together. A future step could be to put the types in the same symtab as
6817 the CU the types ultimately came from. */
6818
6819 static hashval_t
6820 hash_type_unit_group (const void *item)
6821 {
6822 const struct type_unit_group *tu_group
6823 = (const struct type_unit_group *) item;
6824
6825 return hash_stmt_list_entry (&tu_group->hash);
6826 }
6827
6828 static int
6829 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6830 {
6831 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6832 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6833
6834 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6835 }
6836
6837 /* Allocate a hash table for type unit groups. */
6838
6839 static htab_up
6840 allocate_type_unit_groups_table ()
6841 {
6842 return htab_up (htab_create_alloc (3,
6843 hash_type_unit_group,
6844 eq_type_unit_group,
6845 htab_delete_entry<type_unit_group>,
6846 xcalloc, xfree));
6847 }
6848
6849 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6850 partial symtabs. We combine several TUs per psymtab to not let the size
6851 of any one psymtab grow too big. */
6852 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6853 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6854
6855 /* Helper routine for get_type_unit_group.
6856 Create the type_unit_group object used to hold one or more TUs. */
6857
6858 static std::unique_ptr<type_unit_group>
6859 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6860 {
6861 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6862 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6863
6864 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6865 tu_group->per_bfd = per_bfd;
6866
6867 if (per_bfd->using_index)
6868 {
6869 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6870 struct dwarf2_per_cu_quick_data);
6871 }
6872 else
6873 {
6874 unsigned int line_offset = to_underlying (line_offset_struct);
6875 dwarf2_psymtab *pst;
6876 std::string name;
6877
6878 /* Give the symtab a useful name for debug purposes. */
6879 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6880 name = string_printf ("<type_units_%d>",
6881 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6882 else
6883 name = string_printf ("<type_units_at_0x%x>", line_offset);
6884
6885 pst = create_partial_symtab (tu_group.get (), per_objfile,
6886 name.c_str ());
6887 pst->anonymous = true;
6888 }
6889
6890 tu_group->hash.dwo_unit = cu->dwo_unit;
6891 tu_group->hash.line_sect_off = line_offset_struct;
6892
6893 return tu_group;
6894 }
6895
6896 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6897 STMT_LIST is a DW_AT_stmt_list attribute. */
6898
6899 static struct type_unit_group *
6900 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6901 {
6902 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6903 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6904 struct type_unit_group *tu_group;
6905 void **slot;
6906 unsigned int line_offset;
6907 struct type_unit_group type_unit_group_for_lookup;
6908
6909 if (per_objfile->per_bfd->type_unit_groups == NULL)
6910 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6911
6912 /* Do we need to create a new group, or can we use an existing one? */
6913
6914 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6915 {
6916 line_offset = stmt_list->as_unsigned ();
6917 ++tu_stats->nr_symtab_sharers;
6918 }
6919 else
6920 {
6921 /* Ugh, no stmt_list. Rare, but we have to handle it.
6922 We can do various things here like create one group per TU or
6923 spread them over multiple groups to split up the expansion work.
6924 To avoid worst case scenarios (too many groups or too large groups)
6925 we, umm, group them in bunches. */
6926 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6927 | (tu_stats->nr_stmt_less_type_units
6928 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6929 ++tu_stats->nr_stmt_less_type_units;
6930 }
6931
6932 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6933 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6934 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6935 &type_unit_group_for_lookup, INSERT);
6936 if (*slot == nullptr)
6937 {
6938 sect_offset line_offset_struct = (sect_offset) line_offset;
6939 std::unique_ptr<type_unit_group> grp
6940 = create_type_unit_group (cu, line_offset_struct);
6941 *slot = grp.release ();
6942 ++tu_stats->nr_symtabs;
6943 }
6944
6945 tu_group = (struct type_unit_group *) *slot;
6946 gdb_assert (tu_group != nullptr);
6947 return tu_group;
6948 }
6949 \f
6950 /* Partial symbol tables. */
6951
6952 /* Create a psymtab named NAME and assign it to PER_CU.
6953
6954 The caller must fill in the following details:
6955 dirname, textlow, texthigh. */
6956
6957 static dwarf2_psymtab *
6958 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6959 dwarf2_per_objfile *per_objfile,
6960 const char *name)
6961 {
6962 dwarf2_psymtab *pst
6963 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6964 per_objfile->objfile->per_bfd, per_cu);
6965
6966 pst->psymtabs_addrmap_supported = true;
6967
6968 /* This is the glue that links PST into GDB's symbol API. */
6969 per_cu->v.psymtab = pst;
6970
6971 return pst;
6972 }
6973
6974 /* DIE reader function for process_psymtab_comp_unit. */
6975
6976 static void
6977 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6978 const gdb_byte *info_ptr,
6979 struct die_info *comp_unit_die,
6980 enum language pretend_language)
6981 {
6982 struct dwarf2_cu *cu = reader->cu;
6983 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6984 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6985 struct objfile *objfile = per_objfile->objfile;
6986 struct gdbarch *gdbarch = objfile->arch ();
6987 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6988 CORE_ADDR baseaddr;
6989 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6990 dwarf2_psymtab *pst;
6991 enum pc_bounds_kind cu_bounds_kind;
6992
6993 gdb_assert (! per_cu->is_debug_types);
6994
6995 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
6996
6997 /* Allocate a new partial symbol table structure. */
6998 gdb::unique_xmalloc_ptr<char> debug_filename;
6999 static const char artificial[] = "<artificial>";
7000 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
7001 if (strcmp (fnd.name, artificial) == 0)
7002 {
7003 debug_filename.reset (concat (artificial, "@",
7004 sect_offset_str (per_cu->sect_off),
7005 (char *) NULL));
7006 fnd.name = debug_filename.get ();
7007 }
7008
7009 pst = create_partial_symtab (per_cu, per_objfile, fnd.name);
7010
7011 /* This must be done before calling dwarf2_build_include_psymtabs. */
7012 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7013
7014 baseaddr = objfile->text_section_offset ();
7015
7016 dwarf2_find_base_address (comp_unit_die, cu);
7017
7018 /* Possibly set the default values of LOWPC and HIGHPC from
7019 `DW_AT_ranges'. */
7020 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7021 &best_highpc, cu, pst);
7022 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7023 {
7024 CORE_ADDR low
7025 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7026 - baseaddr);
7027 CORE_ADDR high
7028 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7029 - baseaddr - 1);
7030 /* Store the contiguous range if it is not empty; it can be
7031 empty for CUs with no code. */
7032 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7033 low, high, pst);
7034 }
7035
7036 /* Check if comp unit has_children.
7037 If so, read the rest of the partial symbols from this comp unit.
7038 If not, there's no more debug_info for this comp unit. */
7039 if (comp_unit_die->has_children)
7040 {
7041 struct partial_die_info *first_die;
7042 CORE_ADDR lowpc, highpc;
7043
7044 lowpc = ((CORE_ADDR) -1);
7045 highpc = ((CORE_ADDR) 0);
7046
7047 first_die = load_partial_dies (reader, info_ptr, 1);
7048
7049 scan_partial_symbols (first_die, &lowpc, &highpc,
7050 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7051
7052 /* If we didn't find a lowpc, set it to highpc to avoid
7053 complaints from `maint check'. */
7054 if (lowpc == ((CORE_ADDR) -1))
7055 lowpc = highpc;
7056
7057 /* If the compilation unit didn't have an explicit address range,
7058 then use the information extracted from its child dies. */
7059 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7060 {
7061 best_lowpc = lowpc;
7062 best_highpc = highpc;
7063 }
7064 }
7065 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7066 best_lowpc + baseaddr)
7067 - baseaddr);
7068 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7069 best_highpc + baseaddr)
7070 - baseaddr);
7071
7072 pst->end ();
7073
7074 if (!cu->per_cu->imported_symtabs_empty ())
7075 {
7076 int i;
7077 int len = cu->per_cu->imported_symtabs_size ();
7078
7079 /* Fill in 'dependencies' here; we fill in 'users' in a
7080 post-pass. */
7081 pst->number_of_dependencies = len;
7082 pst->dependencies
7083 = per_bfd->partial_symtabs->allocate_dependencies (len);
7084 for (i = 0; i < len; ++i)
7085 {
7086 pst->dependencies[i]
7087 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7088 }
7089
7090 cu->per_cu->imported_symtabs_free ();
7091 }
7092
7093 /* Get the list of files included in the current compilation unit,
7094 and build a psymtab for each of them. */
7095 dwarf2_build_include_psymtabs (cu, comp_unit_die, fnd, pst);
7096
7097 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7098 ", %d global, %d static syms",
7099 per_cu->is_debug_types ? "type" : "comp",
7100 sect_offset_str (per_cu->sect_off),
7101 paddress (gdbarch, pst->text_low (objfile)),
7102 paddress (gdbarch, pst->text_high (objfile)),
7103 (int) pst->global_psymbols.size (),
7104 (int) pst->static_psymbols.size ());
7105 }
7106
7107 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7108 Process compilation unit THIS_CU for a psymtab. */
7109
7110 static void
7111 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7112 dwarf2_per_objfile *per_objfile,
7113 bool want_partial_unit,
7114 enum language pretend_language)
7115 {
7116 /* If this compilation unit was already read in, free the
7117 cached copy in order to read it in again. This is
7118 necessary because we skipped some symbols when we first
7119 read in the compilation unit (see load_partial_dies).
7120 This problem could be avoided, but the benefit is unclear. */
7121 per_objfile->remove_cu (this_cu);
7122
7123 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7124
7125 if (reader.comp_unit_die == nullptr)
7126 return;
7127
7128 switch (reader.comp_unit_die->tag)
7129 {
7130 case DW_TAG_compile_unit:
7131 this_cu->unit_type = DW_UT_compile;
7132 break;
7133 case DW_TAG_partial_unit:
7134 this_cu->unit_type = DW_UT_partial;
7135 break;
7136 case DW_TAG_type_unit:
7137 this_cu->unit_type = DW_UT_type;
7138 break;
7139 default:
7140 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7141 dwarf_tag_name (reader.comp_unit_die->tag),
7142 sect_offset_str (reader.cu->per_cu->sect_off),
7143 objfile_name (per_objfile->objfile));
7144 }
7145
7146 if (reader.dummy_p)
7147 {
7148 /* Nothing. */
7149 }
7150 else if (this_cu->is_debug_types)
7151 build_type_psymtabs_reader (&reader, reader.info_ptr,
7152 reader.comp_unit_die);
7153 else if (want_partial_unit
7154 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7155 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7156 reader.comp_unit_die,
7157 pretend_language);
7158
7159 /* Age out any secondary CUs. */
7160 per_objfile->age_comp_units ();
7161 }
7162
7163 /* Reader function for build_type_psymtabs. */
7164
7165 static void
7166 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7167 const gdb_byte *info_ptr,
7168 struct die_info *type_unit_die)
7169 {
7170 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7171 struct dwarf2_cu *cu = reader->cu;
7172 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7173 struct signatured_type *sig_type;
7174 struct type_unit_group *tu_group;
7175 struct attribute *attr;
7176 struct partial_die_info *first_die;
7177 CORE_ADDR lowpc, highpc;
7178 dwarf2_psymtab *pst;
7179
7180 gdb_assert (per_cu->is_debug_types);
7181 sig_type = (struct signatured_type *) per_cu;
7182
7183 if (! type_unit_die->has_children)
7184 return;
7185
7186 attr = type_unit_die->attr (DW_AT_stmt_list);
7187 tu_group = get_type_unit_group (cu, attr);
7188
7189 if (tu_group->tus == nullptr)
7190 tu_group->tus = new std::vector<signatured_type *>;
7191 tu_group->tus->push_back (sig_type);
7192
7193 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7194 pst = create_partial_symtab (per_cu, per_objfile, "");
7195 pst->anonymous = true;
7196
7197 first_die = load_partial_dies (reader, info_ptr, 1);
7198
7199 lowpc = (CORE_ADDR) -1;
7200 highpc = (CORE_ADDR) 0;
7201 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7202
7203 pst->end ();
7204 }
7205
7206 /* Struct used to sort TUs by their abbreviation table offset. */
7207
7208 struct tu_abbrev_offset
7209 {
7210 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7211 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7212 {}
7213
7214 /* This is used when sorting. */
7215 bool operator< (const tu_abbrev_offset &other) const
7216 {
7217 return abbrev_offset < other.abbrev_offset;
7218 }
7219
7220 signatured_type *sig_type;
7221 sect_offset abbrev_offset;
7222 };
7223
7224 /* Efficiently read all the type units.
7225
7226 The efficiency is because we sort TUs by the abbrev table they use and
7227 only read each abbrev table once. In one program there are 200K TUs
7228 sharing 8K abbrev tables.
7229
7230 The main purpose of this function is to support building the
7231 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7232 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7233 can collapse the search space by grouping them by stmt_list.
7234 The savings can be significant, in the same program from above the 200K TUs
7235 share 8K stmt_list tables.
7236
7237 FUNC is expected to call get_type_unit_group, which will create the
7238 struct type_unit_group if necessary and add it to
7239 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7240
7241 static void
7242 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7243 {
7244 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7245 abbrev_table_up abbrev_table;
7246 sect_offset abbrev_offset;
7247
7248 /* It's up to the caller to not call us multiple times. */
7249 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7250
7251 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7252 return;
7253
7254 /* TUs typically share abbrev tables, and there can be way more TUs than
7255 abbrev tables. Sort by abbrev table to reduce the number of times we
7256 read each abbrev table in.
7257 Alternatives are to punt or to maintain a cache of abbrev tables.
7258 This is simpler and efficient enough for now.
7259
7260 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7261 symtab to use). Typically TUs with the same abbrev offset have the same
7262 stmt_list value too so in practice this should work well.
7263
7264 The basic algorithm here is:
7265
7266 sort TUs by abbrev table
7267 for each TU with same abbrev table:
7268 read abbrev table if first user
7269 read TU top level DIE
7270 [IWBN if DWO skeletons had DW_AT_stmt_list]
7271 call FUNC */
7272
7273 dwarf_read_debug_printf ("Building type unit groups ...");
7274
7275 /* Sort in a separate table to maintain the order of all_comp_units
7276 for .gdb_index: TU indices directly index all_type_units. */
7277 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7278 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7279
7280 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7281 {
7282 if (cu->is_debug_types)
7283 {
7284 auto sig_type = static_cast<signatured_type *> (cu.get ());
7285 sorted_by_abbrev.emplace_back
7286 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7287 sig_type->sect_off));
7288 }
7289 }
7290
7291 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7292
7293 abbrev_offset = (sect_offset) ~(unsigned) 0;
7294
7295 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7296 {
7297 /* Switch to the next abbrev table if necessary. */
7298 if (abbrev_table == NULL
7299 || tu.abbrev_offset != abbrev_offset)
7300 {
7301 abbrev_offset = tu.abbrev_offset;
7302 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7303 abbrev_table =
7304 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7305 ++tu_stats->nr_uniq_abbrev_tables;
7306 }
7307
7308 cutu_reader reader (tu.sig_type, per_objfile,
7309 abbrev_table.get (), nullptr, false);
7310 if (!reader.dummy_p)
7311 build_type_psymtabs_reader (&reader, reader.info_ptr,
7312 reader.comp_unit_die);
7313 }
7314 }
7315
7316 /* Print collected type unit statistics. */
7317
7318 static void
7319 print_tu_stats (dwarf2_per_objfile *per_objfile)
7320 {
7321 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7322
7323 dwarf_read_debug_printf ("Type unit statistics:");
7324 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7325 dwarf_read_debug_printf (" %d uniq abbrev tables",
7326 tu_stats->nr_uniq_abbrev_tables);
7327 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7328 tu_stats->nr_symtabs);
7329 dwarf_read_debug_printf (" %d symtab sharers",
7330 tu_stats->nr_symtab_sharers);
7331 dwarf_read_debug_printf (" %d type units without a stmt_list",
7332 tu_stats->nr_stmt_less_type_units);
7333 dwarf_read_debug_printf (" %d all_type_units reallocs",
7334 tu_stats->nr_all_type_units_reallocs);
7335 }
7336
7337 /* Traversal function for build_type_psymtabs. */
7338
7339 static int
7340 build_type_psymtab_dependencies (void **slot, void *info)
7341 {
7342 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7343 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7344 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7345 dwarf2_psymtab *pst = tu_group->v.psymtab;
7346 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7347 int i;
7348
7349 gdb_assert (len > 0);
7350 gdb_assert (tu_group->type_unit_group_p ());
7351
7352 pst->number_of_dependencies = len;
7353 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7354 for (i = 0; i < len; ++i)
7355 {
7356 struct signatured_type *iter = tu_group->tus->at (i);
7357 gdb_assert (iter->is_debug_types);
7358 pst->dependencies[i] = iter->v.psymtab;
7359 iter->type_unit_group = tu_group;
7360 }
7361
7362 delete tu_group->tus;
7363 tu_group->tus = nullptr;
7364
7365 return 1;
7366 }
7367
7368 /* Traversal function for process_skeletonless_type_unit.
7369 Read a TU in a DWO file and build partial symbols for it. */
7370
7371 static int
7372 process_skeletonless_type_unit (void **slot, void *info)
7373 {
7374 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7375 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7376
7377 /* If this TU doesn't exist in the global table, add it and read it in. */
7378
7379 if (per_objfile->per_bfd->signatured_types == NULL)
7380 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7381
7382 signatured_type find_entry (dwo_unit->signature);
7383 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7384 &find_entry, INSERT);
7385 /* If we've already seen this type there's nothing to do. What's happening
7386 is we're doing our own version of comdat-folding here. */
7387 if (*slot != NULL)
7388 return 1;
7389
7390 /* This does the job that create_all_comp_units would have done for
7391 this TU. */
7392 signatured_type *entry
7393 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7394 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7395 *slot = entry;
7396
7397 /* This does the job that build_type_psymtabs would have done. */
7398 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7399 if (!reader.dummy_p)
7400 build_type_psymtabs_reader (&reader, reader.info_ptr,
7401 reader.comp_unit_die);
7402
7403 return 1;
7404 }
7405
7406 /* Traversal function for process_skeletonless_type_units. */
7407
7408 static int
7409 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7410 {
7411 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7412
7413 if (dwo_file->tus != NULL)
7414 htab_traverse_noresize (dwo_file->tus.get (),
7415 process_skeletonless_type_unit, info);
7416
7417 return 1;
7418 }
7419
7420 /* Scan all TUs of DWO files, verifying we've processed them.
7421 This is needed in case a TU was emitted without its skeleton.
7422 Note: This can't be done until we know what all the DWO files are. */
7423
7424 static void
7425 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7426 {
7427 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7428 if (get_dwp_file (per_objfile) == NULL
7429 && per_objfile->per_bfd->dwo_files != NULL)
7430 {
7431 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7432 process_dwo_file_for_skeletonless_type_units,
7433 per_objfile);
7434 }
7435 }
7436
7437 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7438
7439 static void
7440 set_partial_user (dwarf2_per_objfile *per_objfile)
7441 {
7442 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7443 {
7444 dwarf2_psymtab *pst = per_cu->v.psymtab;
7445
7446 if (pst == NULL)
7447 continue;
7448
7449 for (int j = 0; j < pst->number_of_dependencies; ++j)
7450 {
7451 /* Set the 'user' field only if it is not already set. */
7452 if (pst->dependencies[j]->user == NULL)
7453 pst->dependencies[j]->user = pst;
7454 }
7455 }
7456 }
7457
7458 /* Build the partial symbol table by doing a quick pass through the
7459 .debug_info and .debug_abbrev sections. */
7460
7461 static void
7462 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7463 {
7464 struct objfile *objfile = per_objfile->objfile;
7465 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7466
7467 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7468 objfile_name (objfile));
7469
7470 scoped_restore restore_reading_psyms
7471 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7472
7473 per_bfd->info.read (objfile);
7474
7475 /* Any cached compilation units will be linked by the per-objfile
7476 read_in_chain. Make sure to free them when we're done. */
7477 free_cached_comp_units freer (per_objfile);
7478
7479 create_all_comp_units (per_objfile);
7480 build_type_psymtabs (per_objfile);
7481
7482 /* Create a temporary address map on a temporary obstack. We later
7483 copy this to the final obstack. */
7484 auto_obstack temp_obstack;
7485
7486 scoped_restore save_psymtabs_addrmap
7487 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7488 addrmap_create_mutable (&temp_obstack));
7489
7490 for (const auto &per_cu : per_bfd->all_comp_units)
7491 {
7492 if (per_cu->v.psymtab != NULL)
7493 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7494 continue;
7495 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7496 language_minimal);
7497 }
7498
7499 /* This has to wait until we read the CUs, we need the list of DWOs. */
7500 process_skeletonless_type_units (per_objfile);
7501
7502 /* Now that all TUs have been processed we can fill in the dependencies. */
7503 if (per_bfd->type_unit_groups != NULL)
7504 {
7505 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7506 build_type_psymtab_dependencies, per_objfile);
7507 }
7508
7509 if (dwarf_read_debug > 0)
7510 print_tu_stats (per_objfile);
7511
7512 set_partial_user (per_objfile);
7513
7514 per_bfd->partial_symtabs->psymtabs_addrmap
7515 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7516 per_bfd->partial_symtabs->obstack ());
7517 /* At this point we want to keep the address map. */
7518 save_psymtabs_addrmap.release ();
7519
7520 dwarf_read_debug_printf ("Done building psymtabs of %s",
7521 objfile_name (objfile));
7522 }
7523
7524 /* Load the partial DIEs for a secondary CU into memory.
7525 This is also used when rereading a primary CU with load_all_dies. */
7526
7527 static void
7528 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7529 dwarf2_per_objfile *per_objfile,
7530 dwarf2_cu *existing_cu)
7531 {
7532 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7533
7534 if (!reader.dummy_p)
7535 {
7536 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7537 language_minimal);
7538
7539 /* Check if comp unit has_children.
7540 If so, read the rest of the partial symbols from this comp unit.
7541 If not, there's no more debug_info for this comp unit. */
7542 if (reader.comp_unit_die->has_children)
7543 load_partial_dies (&reader, reader.info_ptr, 0);
7544
7545 reader.keep ();
7546 }
7547 }
7548
7549 static void
7550 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7551 struct dwarf2_section_info *section,
7552 struct dwarf2_section_info *abbrev_section,
7553 unsigned int is_dwz,
7554 htab_up &types_htab,
7555 rcuh_kind section_kind)
7556 {
7557 const gdb_byte *info_ptr;
7558 struct objfile *objfile = per_objfile->objfile;
7559
7560 dwarf_read_debug_printf ("Reading %s for %s",
7561 section->get_name (),
7562 section->get_file_name ());
7563
7564 section->read (objfile);
7565
7566 info_ptr = section->buffer;
7567
7568 while (info_ptr < section->buffer + section->size)
7569 {
7570 dwarf2_per_cu_data_up this_cu;
7571
7572 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7573
7574 comp_unit_head cu_header;
7575 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7576 abbrev_section, info_ptr,
7577 section_kind);
7578
7579 /* Save the compilation unit for later lookup. */
7580 if (cu_header.unit_type != DW_UT_type)
7581 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7582 else
7583 {
7584 if (types_htab == nullptr)
7585 types_htab = allocate_signatured_type_table ();
7586
7587 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7588 (cu_header.signature);
7589 signatured_type *sig_ptr = sig_type.get ();
7590 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7591 this_cu.reset (sig_type.release ());
7592
7593 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7594 gdb_assert (slot != nullptr);
7595 if (*slot != nullptr)
7596 complaint (_("debug type entry at offset %s is duplicate to"
7597 " the entry at offset %s, signature %s"),
7598 sect_offset_str (sect_off),
7599 sect_offset_str (sig_ptr->sect_off),
7600 hex_string (sig_ptr->signature));
7601 *slot = sig_ptr;
7602 }
7603 this_cu->sect_off = sect_off;
7604 this_cu->length = cu_header.length + cu_header.initial_length_size;
7605 this_cu->is_dwz = is_dwz;
7606 this_cu->section = section;
7607
7608 info_ptr = info_ptr + this_cu->length;
7609 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7610 }
7611 }
7612
7613 /* Create a list of all compilation units in OBJFILE.
7614 This is only done for -readnow and building partial symtabs. */
7615
7616 static void
7617 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7618 {
7619 htab_up types_htab;
7620
7621 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7622 &per_objfile->per_bfd->abbrev, 0,
7623 types_htab, rcuh_kind::COMPILE);
7624 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7625 read_comp_units_from_section (per_objfile, &section,
7626 &per_objfile->per_bfd->abbrev, 0,
7627 types_htab, rcuh_kind::TYPE);
7628
7629 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7630 if (dwz != NULL)
7631 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7632 types_htab, rcuh_kind::COMPILE);
7633
7634 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7635 }
7636
7637 /* Process all loaded DIEs for compilation unit CU, starting at
7638 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7639 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7640 DW_AT_ranges). See the comments of add_partial_subprogram on how
7641 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7642
7643 static void
7644 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7645 CORE_ADDR *highpc, int set_addrmap,
7646 struct dwarf2_cu *cu)
7647 {
7648 struct partial_die_info *pdi;
7649
7650 /* Now, march along the PDI's, descending into ones which have
7651 interesting children but skipping the children of the other ones,
7652 until we reach the end of the compilation unit. */
7653
7654 pdi = first_die;
7655
7656 while (pdi != NULL)
7657 {
7658 pdi->fixup (cu);
7659
7660 /* Anonymous namespaces or modules have no name but have interesting
7661 children, so we need to look at them. Ditto for anonymous
7662 enums. */
7663
7664 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7665 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7666 || pdi->tag == DW_TAG_imported_unit
7667 || pdi->tag == DW_TAG_inlined_subroutine)
7668 {
7669 switch (pdi->tag)
7670 {
7671 case DW_TAG_subprogram:
7672 case DW_TAG_inlined_subroutine:
7673 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7674 if (cu->per_cu->lang == language_cplus)
7675 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7676 set_addrmap, cu);
7677 break;
7678 case DW_TAG_constant:
7679 case DW_TAG_variable:
7680 case DW_TAG_typedef:
7681 case DW_TAG_union_type:
7682 if (!pdi->is_declaration
7683 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7684 {
7685 add_partial_symbol (pdi, cu);
7686 }
7687 break;
7688 case DW_TAG_class_type:
7689 case DW_TAG_interface_type:
7690 case DW_TAG_structure_type:
7691 if (!pdi->is_declaration)
7692 {
7693 add_partial_symbol (pdi, cu);
7694 }
7695 if ((cu->per_cu->lang == language_rust
7696 || cu->per_cu->lang == language_cplus)
7697 && pdi->has_children)
7698 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7699 set_addrmap, cu);
7700 break;
7701 case DW_TAG_enumeration_type:
7702 if (!pdi->is_declaration)
7703 add_partial_enumeration (pdi, cu);
7704 break;
7705 case DW_TAG_base_type:
7706 case DW_TAG_subrange_type:
7707 /* File scope base type definitions are added to the partial
7708 symbol table. */
7709 add_partial_symbol (pdi, cu);
7710 break;
7711 case DW_TAG_namespace:
7712 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7713 break;
7714 case DW_TAG_module:
7715 if (!pdi->is_declaration)
7716 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7717 break;
7718 case DW_TAG_imported_unit:
7719 {
7720 struct dwarf2_per_cu_data *per_cu;
7721
7722 /* For now we don't handle imported units in type units. */
7723 if (cu->per_cu->is_debug_types)
7724 {
7725 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7726 " supported in type units [in module %s]"),
7727 objfile_name (cu->per_objfile->objfile));
7728 }
7729
7730 per_cu = dwarf2_find_containing_comp_unit
7731 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
7732
7733 /* Go read the partial unit, if needed. */
7734 if (per_cu->v.psymtab == NULL)
7735 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7736 cu->per_cu->lang);
7737
7738 if (pdi->die_parent == nullptr
7739 && per_cu->unit_type == DW_UT_compile
7740 && per_cu->lang == language_cplus)
7741 /* Regard import as hint. See corresponding code in
7742 process_imported_unit_die. */
7743 break;
7744
7745 cu->per_cu->imported_symtabs_push (per_cu);
7746 }
7747 break;
7748 case DW_TAG_imported_declaration:
7749 add_partial_symbol (pdi, cu);
7750 break;
7751 default:
7752 break;
7753 }
7754 }
7755
7756 /* If the die has a sibling, skip to the sibling. */
7757
7758 pdi = pdi->die_sibling;
7759 }
7760 }
7761
7762 /* Functions used to compute the fully scoped name of a partial DIE.
7763
7764 Normally, this is simple. For C++, the parent DIE's fully scoped
7765 name is concatenated with "::" and the partial DIE's name.
7766 Enumerators are an exception; they use the scope of their parent
7767 enumeration type, i.e. the name of the enumeration type is not
7768 prepended to the enumerator.
7769
7770 There are two complexities. One is DW_AT_specification; in this
7771 case "parent" means the parent of the target of the specification,
7772 instead of the direct parent of the DIE. The other is compilers
7773 which do not emit DW_TAG_namespace; in this case we try to guess
7774 the fully qualified name of structure types from their members'
7775 linkage names. This must be done using the DIE's children rather
7776 than the children of any DW_AT_specification target. We only need
7777 to do this for structures at the top level, i.e. if the target of
7778 any DW_AT_specification (if any; otherwise the DIE itself) does not
7779 have a parent. */
7780
7781 /* Compute the scope prefix associated with PDI's parent, in
7782 compilation unit CU. The result will be allocated on CU's
7783 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7784 field. NULL is returned if no prefix is necessary. */
7785 static const char *
7786 partial_die_parent_scope (struct partial_die_info *pdi,
7787 struct dwarf2_cu *cu)
7788 {
7789 const char *grandparent_scope;
7790 struct partial_die_info *parent, *real_pdi;
7791
7792 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7793 then this means the parent of the specification DIE. */
7794
7795 real_pdi = pdi;
7796 while (real_pdi->has_specification)
7797 {
7798 auto res = find_partial_die (real_pdi->spec_offset,
7799 real_pdi->spec_is_dwz, cu);
7800 real_pdi = res.pdi;
7801 cu = res.cu;
7802 }
7803
7804 parent = real_pdi->die_parent;
7805 if (parent == NULL)
7806 return NULL;
7807
7808 if (parent->scope_set)
7809 return parent->scope;
7810
7811 parent->fixup (cu);
7812
7813 grandparent_scope = partial_die_parent_scope (parent, cu);
7814
7815 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7816 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7817 Work around this problem here. */
7818 if (cu->per_cu->lang == language_cplus
7819 && parent->tag == DW_TAG_namespace
7820 && strcmp (parent->name (cu), "::") == 0
7821 && grandparent_scope == NULL)
7822 {
7823 parent->scope = NULL;
7824 parent->scope_set = 1;
7825 return NULL;
7826 }
7827
7828 /* Nested subroutines in Fortran get a prefix. */
7829 if (pdi->tag == DW_TAG_enumerator)
7830 /* Enumerators should not get the name of the enumeration as a prefix. */
7831 parent->scope = grandparent_scope;
7832 else if (parent->tag == DW_TAG_namespace
7833 || parent->tag == DW_TAG_module
7834 || parent->tag == DW_TAG_structure_type
7835 || parent->tag == DW_TAG_class_type
7836 || parent->tag == DW_TAG_interface_type
7837 || parent->tag == DW_TAG_union_type
7838 || parent->tag == DW_TAG_enumeration_type
7839 || (cu->per_cu->lang == language_fortran
7840 && parent->tag == DW_TAG_subprogram
7841 && pdi->tag == DW_TAG_subprogram))
7842 {
7843 if (grandparent_scope == NULL)
7844 parent->scope = parent->name (cu);
7845 else
7846 parent->scope = typename_concat (&cu->comp_unit_obstack,
7847 grandparent_scope,
7848 parent->name (cu), 0, cu);
7849 }
7850 else
7851 {
7852 /* FIXME drow/2004-04-01: What should we be doing with
7853 function-local names? For partial symbols, we should probably be
7854 ignoring them. */
7855 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7856 dwarf_tag_name (parent->tag),
7857 sect_offset_str (pdi->sect_off));
7858 parent->scope = grandparent_scope;
7859 }
7860
7861 parent->scope_set = 1;
7862 return parent->scope;
7863 }
7864
7865 /* Return the fully scoped name associated with PDI, from compilation unit
7866 CU. The result will be allocated with malloc. */
7867
7868 static gdb::unique_xmalloc_ptr<char>
7869 partial_die_full_name (struct partial_die_info *pdi,
7870 struct dwarf2_cu *cu)
7871 {
7872 const char *parent_scope;
7873
7874 /* If this is a template instantiation, we can not work out the
7875 template arguments from partial DIEs. So, unfortunately, we have
7876 to go through the full DIEs. At least any work we do building
7877 types here will be reused if full symbols are loaded later. */
7878 if (pdi->has_template_arguments)
7879 {
7880 pdi->fixup (cu);
7881
7882 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7883 {
7884 struct die_info *die;
7885 struct attribute attr;
7886 struct dwarf2_cu *ref_cu = cu;
7887
7888 /* DW_FORM_ref_addr is using section offset. */
7889 attr.name = (enum dwarf_attribute) 0;
7890 attr.form = DW_FORM_ref_addr;
7891 attr.u.unsnd = to_underlying (pdi->sect_off);
7892 die = follow_die_ref (NULL, &attr, &ref_cu);
7893
7894 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7895 }
7896 }
7897
7898 parent_scope = partial_die_parent_scope (pdi, cu);
7899 if (parent_scope == NULL)
7900 return NULL;
7901 else
7902 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7903 pdi->name (cu),
7904 0, cu));
7905 }
7906
7907 static void
7908 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7909 {
7910 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7911 struct objfile *objfile = per_objfile->objfile;
7912 struct gdbarch *gdbarch = objfile->arch ();
7913 CORE_ADDR addr = 0;
7914 const char *actual_name = NULL;
7915 CORE_ADDR baseaddr;
7916
7917 baseaddr = objfile->text_section_offset ();
7918
7919 gdb::unique_xmalloc_ptr<char> built_actual_name
7920 = partial_die_full_name (pdi, cu);
7921 if (built_actual_name != NULL)
7922 actual_name = built_actual_name.get ();
7923
7924 if (actual_name == NULL)
7925 actual_name = pdi->name (cu);
7926
7927 partial_symbol psymbol;
7928 memset (&psymbol, 0, sizeof (psymbol));
7929 psymbol.ginfo.set_language (cu->per_cu->lang,
7930 &objfile->objfile_obstack);
7931 psymbol.ginfo.set_section_index (-1);
7932
7933 /* The code below indicates that the psymbol should be installed by
7934 setting this. */
7935 gdb::optional<psymbol_placement> where;
7936
7937 switch (pdi->tag)
7938 {
7939 case DW_TAG_inlined_subroutine:
7940 case DW_TAG_subprogram:
7941 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7942 - baseaddr);
7943 if (pdi->is_external
7944 || cu->per_cu->lang == language_ada
7945 || (cu->per_cu->lang == language_fortran
7946 && pdi->die_parent != NULL
7947 && pdi->die_parent->tag == DW_TAG_subprogram))
7948 {
7949 /* Normally, only "external" DIEs are part of the global scope.
7950 But in Ada and Fortran, we want to be able to access nested
7951 procedures globally. So all Ada and Fortran subprograms are
7952 stored in the global scope. */
7953 where = psymbol_placement::GLOBAL;
7954 }
7955 else
7956 where = psymbol_placement::STATIC;
7957
7958 psymbol.domain = VAR_DOMAIN;
7959 psymbol.aclass = LOC_BLOCK;
7960 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7961 psymbol.ginfo.value.address = addr;
7962
7963 if (pdi->main_subprogram && actual_name != NULL)
7964 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
7965 break;
7966 case DW_TAG_constant:
7967 psymbol.domain = VAR_DOMAIN;
7968 psymbol.aclass = LOC_STATIC;
7969 where = (pdi->is_external
7970 ? psymbol_placement::GLOBAL
7971 : psymbol_placement::STATIC);
7972 break;
7973 case DW_TAG_variable:
7974 if (pdi->d.locdesc)
7975 addr = decode_locdesc (pdi->d.locdesc, cu);
7976
7977 if (pdi->d.locdesc
7978 && addr == 0
7979 && !per_objfile->per_bfd->has_section_at_zero)
7980 {
7981 /* A global or static variable may also have been stripped
7982 out by the linker if unused, in which case its address
7983 will be nullified; do not add such variables into partial
7984 symbol table then. */
7985 }
7986 else if (pdi->is_external)
7987 {
7988 /* Global Variable.
7989 Don't enter into the minimal symbol tables as there is
7990 a minimal symbol table entry from the ELF symbols already.
7991 Enter into partial symbol table if it has a location
7992 descriptor or a type.
7993 If the location descriptor is missing, new_symbol will create
7994 a LOC_UNRESOLVED symbol, the address of the variable will then
7995 be determined from the minimal symbol table whenever the variable
7996 is referenced.
7997 The address for the partial symbol table entry is not
7998 used by GDB, but it comes in handy for debugging partial symbol
7999 table building. */
8000
8001 if (pdi->d.locdesc || pdi->has_type)
8002 {
8003 psymbol.domain = VAR_DOMAIN;
8004 psymbol.aclass = LOC_STATIC;
8005 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8006 psymbol.ginfo.value.address = addr;
8007 where = psymbol_placement::GLOBAL;
8008 }
8009 }
8010 else
8011 {
8012 int has_loc = pdi->d.locdesc != NULL;
8013
8014 /* Static Variable. Skip symbols whose value we cannot know (those
8015 without location descriptors or constant values). */
8016 if (!has_loc && !pdi->has_const_value)
8017 return;
8018
8019 psymbol.domain = VAR_DOMAIN;
8020 psymbol.aclass = LOC_STATIC;
8021 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8022 if (has_loc)
8023 psymbol.ginfo.value.address = addr;
8024 where = psymbol_placement::STATIC;
8025 }
8026 break;
8027 case DW_TAG_array_type:
8028 case DW_TAG_typedef:
8029 case DW_TAG_base_type:
8030 case DW_TAG_subrange_type:
8031 psymbol.domain = VAR_DOMAIN;
8032 psymbol.aclass = LOC_TYPEDEF;
8033 where = psymbol_placement::STATIC;
8034 break;
8035 case DW_TAG_imported_declaration:
8036 case DW_TAG_namespace:
8037 psymbol.domain = VAR_DOMAIN;
8038 psymbol.aclass = LOC_TYPEDEF;
8039 where = psymbol_placement::GLOBAL;
8040 break;
8041 case DW_TAG_module:
8042 /* With Fortran 77 there might be a "BLOCK DATA" module
8043 available without any name. If so, we skip the module as it
8044 doesn't bring any value. */
8045 if (actual_name != nullptr)
8046 {
8047 psymbol.domain = MODULE_DOMAIN;
8048 psymbol.aclass = LOC_TYPEDEF;
8049 where = psymbol_placement::GLOBAL;
8050 }
8051 break;
8052 case DW_TAG_class_type:
8053 case DW_TAG_interface_type:
8054 case DW_TAG_structure_type:
8055 case DW_TAG_union_type:
8056 case DW_TAG_enumeration_type:
8057 /* Skip external references. The DWARF standard says in the section
8058 about "Structure, Union, and Class Type Entries": "An incomplete
8059 structure, union or class type is represented by a structure,
8060 union or class entry that does not have a byte size attribute
8061 and that has a DW_AT_declaration attribute." */
8062 if (!pdi->has_byte_size && pdi->is_declaration)
8063 return;
8064
8065 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8066 static vs. global. */
8067 psymbol.domain = STRUCT_DOMAIN;
8068 psymbol.aclass = LOC_TYPEDEF;
8069 where = (cu->per_cu->lang == language_cplus
8070 ? psymbol_placement::GLOBAL
8071 : psymbol_placement::STATIC);
8072 break;
8073 case DW_TAG_enumerator:
8074 psymbol.domain = VAR_DOMAIN;
8075 psymbol.aclass = LOC_CONST;
8076 where = (cu->per_cu->lang == language_cplus
8077 ? psymbol_placement::GLOBAL
8078 : psymbol_placement::STATIC);
8079 break;
8080 default:
8081 break;
8082 }
8083
8084 if (where.has_value ())
8085 {
8086 if (built_actual_name != nullptr)
8087 actual_name = objfile->intern (actual_name);
8088 if (pdi->linkage_name == nullptr
8089 || cu->per_cu->lang == language_ada)
8090 psymbol.ginfo.set_linkage_name (actual_name);
8091 else
8092 {
8093 psymbol.ginfo.set_demangled_name (actual_name,
8094 &objfile->objfile_obstack);
8095 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8096 }
8097 cu->per_cu->v.psymtab->add_psymbol
8098 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8099 objfile);
8100 }
8101 }
8102
8103 /* Read a partial die corresponding to a namespace; also, add a symbol
8104 corresponding to that namespace to the symbol table. NAMESPACE is
8105 the name of the enclosing namespace. */
8106
8107 static void
8108 add_partial_namespace (struct partial_die_info *pdi,
8109 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8110 int set_addrmap, struct dwarf2_cu *cu)
8111 {
8112 /* Add a symbol for the namespace. */
8113
8114 add_partial_symbol (pdi, cu);
8115
8116 /* Now scan partial symbols in that namespace. */
8117
8118 if (pdi->has_children)
8119 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8120 }
8121
8122 /* Read a partial die corresponding to a Fortran module. */
8123
8124 static void
8125 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8126 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8127 {
8128 /* Add a symbol for the namespace. */
8129
8130 add_partial_symbol (pdi, cu);
8131
8132 /* Now scan partial symbols in that module. */
8133
8134 if (pdi->has_children)
8135 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8136 }
8137
8138 static int
8139 dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
8140 dwarf2_psymtab *, dwarf_tag);
8141
8142 /* Read a partial die corresponding to a subprogram or an inlined
8143 subprogram and create a partial symbol for that subprogram.
8144 When the CU language allows it, this routine also defines a partial
8145 symbol for each nested subprogram that this subprogram contains.
8146 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8147 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8148
8149 PDI may also be a lexical block, in which case we simply search
8150 recursively for subprograms defined inside that lexical block.
8151 Again, this is only performed when the CU language allows this
8152 type of definitions. */
8153
8154 static void
8155 add_partial_subprogram (struct partial_die_info *pdi,
8156 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8157 int set_addrmap, struct dwarf2_cu *cu)
8158 {
8159 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8160 {
8161 if (pdi->has_pc_info)
8162 {
8163 if (pdi->lowpc < *lowpc)
8164 *lowpc = pdi->lowpc;
8165 if (pdi->highpc > *highpc)
8166 *highpc = pdi->highpc;
8167 if (set_addrmap)
8168 {
8169 struct objfile *objfile = cu->per_objfile->objfile;
8170 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8171 struct gdbarch *gdbarch = objfile->arch ();
8172 CORE_ADDR baseaddr;
8173 CORE_ADDR this_highpc;
8174 CORE_ADDR this_lowpc;
8175
8176 baseaddr = objfile->text_section_offset ();
8177 this_lowpc
8178 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8179 pdi->lowpc + baseaddr)
8180 - baseaddr);
8181 this_highpc
8182 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8183 pdi->highpc + baseaddr)
8184 - baseaddr);
8185 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8186 this_lowpc, this_highpc - 1,
8187 cu->per_cu->v.psymtab);
8188 }
8189 }
8190
8191 if (pdi->has_range_info
8192 && dwarf2_ranges_read (pdi->ranges_offset, &pdi->lowpc, &pdi->highpc,
8193 cu,
8194 set_addrmap ? cu->per_cu->v.psymtab : nullptr,
8195 pdi->tag))
8196 {
8197 if (pdi->lowpc < *lowpc)
8198 *lowpc = pdi->lowpc;
8199 if (pdi->highpc > *highpc)
8200 *highpc = pdi->highpc;
8201 }
8202
8203 if (pdi->has_pc_info || pdi->has_range_info
8204 || (!pdi->is_external && pdi->may_be_inlined))
8205 {
8206 if (!pdi->is_declaration)
8207 /* Ignore subprogram DIEs that do not have a name, they are
8208 illegal. Do not emit a complaint at this point, we will
8209 do so when we convert this psymtab into a symtab. */
8210 if (pdi->name (cu))
8211 add_partial_symbol (pdi, cu);
8212 }
8213 }
8214
8215 if (! pdi->has_children)
8216 return;
8217
8218 if (cu->per_cu->lang == language_ada
8219 || cu->per_cu->lang == language_fortran)
8220 {
8221 pdi = pdi->die_child;
8222 while (pdi != NULL)
8223 {
8224 pdi->fixup (cu);
8225 if (pdi->tag == DW_TAG_subprogram
8226 || pdi->tag == DW_TAG_inlined_subroutine
8227 || pdi->tag == DW_TAG_lexical_block)
8228 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8229 pdi = pdi->die_sibling;
8230 }
8231 }
8232 }
8233
8234 /* Read a partial die corresponding to an enumeration type. */
8235
8236 static void
8237 add_partial_enumeration (struct partial_die_info *enum_pdi,
8238 struct dwarf2_cu *cu)
8239 {
8240 struct partial_die_info *pdi;
8241
8242 if (enum_pdi->name (cu) != NULL)
8243 add_partial_symbol (enum_pdi, cu);
8244
8245 pdi = enum_pdi->die_child;
8246 while (pdi)
8247 {
8248 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8249 complaint (_("malformed enumerator DIE ignored"));
8250 else
8251 add_partial_symbol (pdi, cu);
8252 pdi = pdi->die_sibling;
8253 }
8254 }
8255
8256 /* Return the initial uleb128 in the die at INFO_PTR. */
8257
8258 static unsigned int
8259 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8260 {
8261 unsigned int bytes_read;
8262
8263 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8264 }
8265
8266 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8267 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8268
8269 Return the corresponding abbrev, or NULL if the number is zero (indicating
8270 an empty DIE). In either case *BYTES_READ will be set to the length of
8271 the initial number. */
8272
8273 static const struct abbrev_info *
8274 peek_die_abbrev (const die_reader_specs &reader,
8275 const gdb_byte *info_ptr, unsigned int *bytes_read)
8276 {
8277 dwarf2_cu *cu = reader.cu;
8278 bfd *abfd = reader.abfd;
8279 unsigned int abbrev_number
8280 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8281
8282 if (abbrev_number == 0)
8283 return NULL;
8284
8285 const abbrev_info *abbrev
8286 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8287 if (!abbrev)
8288 {
8289 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8290 " at offset %s [in module %s]"),
8291 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8292 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8293 }
8294
8295 return abbrev;
8296 }
8297
8298 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8299 Returns a pointer to the end of a series of DIEs, terminated by an empty
8300 DIE. Any children of the skipped DIEs will also be skipped. */
8301
8302 static const gdb_byte *
8303 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8304 {
8305 while (1)
8306 {
8307 unsigned int bytes_read;
8308 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8309 &bytes_read);
8310
8311 if (abbrev == NULL)
8312 return info_ptr + bytes_read;
8313 else
8314 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8315 }
8316 }
8317
8318 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8319 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8320 abbrev corresponding to that skipped uleb128 should be passed in
8321 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8322 children. */
8323
8324 static const gdb_byte *
8325 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8326 const struct abbrev_info *abbrev)
8327 {
8328 unsigned int bytes_read;
8329 struct attribute attr;
8330 bfd *abfd = reader->abfd;
8331 struct dwarf2_cu *cu = reader->cu;
8332 const gdb_byte *buffer = reader->buffer;
8333 const gdb_byte *buffer_end = reader->buffer_end;
8334 unsigned int form, i;
8335
8336 for (i = 0; i < abbrev->num_attrs; i++)
8337 {
8338 /* The only abbrev we care about is DW_AT_sibling. */
8339 if (abbrev->attrs[i].name == DW_AT_sibling)
8340 {
8341 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8342 if (attr.form == DW_FORM_ref_addr)
8343 complaint (_("ignoring absolute DW_AT_sibling"));
8344 else
8345 {
8346 sect_offset off = attr.get_ref_die_offset ();
8347 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8348
8349 if (sibling_ptr < info_ptr)
8350 complaint (_("DW_AT_sibling points backwards"));
8351 else if (sibling_ptr > reader->buffer_end)
8352 reader->die_section->overflow_complaint ();
8353 else
8354 return sibling_ptr;
8355 }
8356 }
8357
8358 /* If it isn't DW_AT_sibling, skip this attribute. */
8359 form = abbrev->attrs[i].form;
8360 skip_attribute:
8361 switch (form)
8362 {
8363 case DW_FORM_ref_addr:
8364 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8365 and later it is offset sized. */
8366 if (cu->header.version == 2)
8367 info_ptr += cu->header.addr_size;
8368 else
8369 info_ptr += cu->header.offset_size;
8370 break;
8371 case DW_FORM_GNU_ref_alt:
8372 info_ptr += cu->header.offset_size;
8373 break;
8374 case DW_FORM_addr:
8375 info_ptr += cu->header.addr_size;
8376 break;
8377 case DW_FORM_data1:
8378 case DW_FORM_ref1:
8379 case DW_FORM_flag:
8380 case DW_FORM_strx1:
8381 info_ptr += 1;
8382 break;
8383 case DW_FORM_flag_present:
8384 case DW_FORM_implicit_const:
8385 break;
8386 case DW_FORM_data2:
8387 case DW_FORM_ref2:
8388 case DW_FORM_strx2:
8389 info_ptr += 2;
8390 break;
8391 case DW_FORM_strx3:
8392 info_ptr += 3;
8393 break;
8394 case DW_FORM_data4:
8395 case DW_FORM_ref4:
8396 case DW_FORM_strx4:
8397 info_ptr += 4;
8398 break;
8399 case DW_FORM_data8:
8400 case DW_FORM_ref8:
8401 case DW_FORM_ref_sig8:
8402 info_ptr += 8;
8403 break;
8404 case DW_FORM_data16:
8405 info_ptr += 16;
8406 break;
8407 case DW_FORM_string:
8408 read_direct_string (abfd, info_ptr, &bytes_read);
8409 info_ptr += bytes_read;
8410 break;
8411 case DW_FORM_sec_offset:
8412 case DW_FORM_strp:
8413 case DW_FORM_GNU_strp_alt:
8414 info_ptr += cu->header.offset_size;
8415 break;
8416 case DW_FORM_exprloc:
8417 case DW_FORM_block:
8418 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8419 info_ptr += bytes_read;
8420 break;
8421 case DW_FORM_block1:
8422 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8423 break;
8424 case DW_FORM_block2:
8425 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8426 break;
8427 case DW_FORM_block4:
8428 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8429 break;
8430 case DW_FORM_addrx:
8431 case DW_FORM_strx:
8432 case DW_FORM_sdata:
8433 case DW_FORM_udata:
8434 case DW_FORM_ref_udata:
8435 case DW_FORM_GNU_addr_index:
8436 case DW_FORM_GNU_str_index:
8437 case DW_FORM_rnglistx:
8438 case DW_FORM_loclistx:
8439 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8440 break;
8441 case DW_FORM_indirect:
8442 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8443 info_ptr += bytes_read;
8444 /* We need to continue parsing from here, so just go back to
8445 the top. */
8446 goto skip_attribute;
8447
8448 default:
8449 error (_("Dwarf Error: Cannot handle %s "
8450 "in DWARF reader [in module %s]"),
8451 dwarf_form_name (form),
8452 bfd_get_filename (abfd));
8453 }
8454 }
8455
8456 if (abbrev->has_children)
8457 return skip_children (reader, info_ptr);
8458 else
8459 return info_ptr;
8460 }
8461
8462 /* Locate ORIG_PDI's sibling.
8463 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8464
8465 static const gdb_byte *
8466 locate_pdi_sibling (const struct die_reader_specs *reader,
8467 struct partial_die_info *orig_pdi,
8468 const gdb_byte *info_ptr)
8469 {
8470 /* Do we know the sibling already? */
8471
8472 if (orig_pdi->sibling)
8473 return orig_pdi->sibling;
8474
8475 /* Are there any children to deal with? */
8476
8477 if (!orig_pdi->has_children)
8478 return info_ptr;
8479
8480 /* Skip the children the long way. */
8481
8482 return skip_children (reader, info_ptr);
8483 }
8484
8485 /* Expand this partial symbol table into a full symbol table. SELF is
8486 not NULL. */
8487
8488 void
8489 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8490 {
8491 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8492
8493 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8494
8495 /* If this psymtab is constructed from a debug-only objfile, the
8496 has_section_at_zero flag will not necessarily be correct. We
8497 can get the correct value for this flag by looking at the data
8498 associated with the (presumably stripped) associated objfile. */
8499 if (objfile->separate_debug_objfile_backlink)
8500 {
8501 dwarf2_per_objfile *per_objfile_backlink
8502 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8503
8504 per_objfile->per_bfd->has_section_at_zero
8505 = per_objfile_backlink->per_bfd->has_section_at_zero;
8506 }
8507
8508 expand_psymtab (objfile);
8509
8510 process_cu_includes (per_objfile);
8511 }
8512 \f
8513 /* Reading in full CUs. */
8514
8515 /* Add PER_CU to the queue. */
8516
8517 static void
8518 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8519 dwarf2_per_objfile *per_objfile,
8520 enum language pretend_language)
8521 {
8522 per_cu->queued = 1;
8523
8524 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8525 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8526 }
8527
8528 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8529
8530 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8531 dependency.
8532
8533 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8534 DIEs, false otherwise.
8535
8536 Explanation: there is an invariant that if a CU is queued for expansion
8537 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8538 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8539 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8540 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8541 invariant is respected.
8542
8543 The caller is therefore not required to load the CU's DIEs (we return false)
8544 if:
8545
8546 - the CU is already expanded, and therefore does not get enqueued
8547 - the CU gets enqueued for expansion, but its DIEs are already loaded
8548
8549 Note that the caller should not use this function's return value as an
8550 indicator of whether the CU's DIEs are loaded right now, it should check
8551 that by calling `dwarf2_per_objfile::get_cu` instead. */
8552
8553 static int
8554 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8555 dwarf2_per_cu_data *per_cu,
8556 dwarf2_per_objfile *per_objfile,
8557 enum language pretend_language)
8558 {
8559 /* We may arrive here during partial symbol reading, if we need full
8560 DIEs to process an unusual case (e.g. template arguments). Do
8561 not queue PER_CU, just tell our caller to load its DIEs. */
8562 if (per_cu->per_bfd->reading_partial_symbols)
8563 {
8564 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8565
8566 if (cu == NULL || cu->dies == NULL)
8567 return 1;
8568 return 0;
8569 }
8570
8571 /* Mark the dependence relation so that we don't flush PER_CU
8572 too early. */
8573 if (dependent_cu != NULL)
8574 dependent_cu->add_dependence (per_cu);
8575
8576 /* If it's already on the queue, we have nothing to do. */
8577 if (per_cu->queued)
8578 {
8579 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8580 loaded. */
8581 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8582
8583 /* If the CU is queued for expansion, it should not already be
8584 expanded. */
8585 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8586
8587 /* The DIEs are already loaded, the caller doesn't need to do it. */
8588 return 0;
8589 }
8590
8591 bool queued = false;
8592 if (!per_objfile->symtab_set_p (per_cu))
8593 {
8594 /* Add it to the queue. */
8595 queue_comp_unit (per_cu, per_objfile, pretend_language);
8596 queued = true;
8597 }
8598
8599 /* If the compilation unit is already loaded, just mark it as
8600 used. */
8601 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8602 if (cu != nullptr)
8603 cu->last_used = 0;
8604
8605 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8606 and the DIEs are not already loaded. */
8607 return queued && cu == nullptr;
8608 }
8609
8610 /* Process the queue. */
8611
8612 static void
8613 process_queue (dwarf2_per_objfile *per_objfile)
8614 {
8615 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8616 objfile_name (per_objfile->objfile));
8617
8618 /* The queue starts out with one item, but following a DIE reference
8619 may load a new CU, adding it to the end of the queue. */
8620 while (!per_objfile->per_bfd->queue->empty ())
8621 {
8622 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8623 dwarf2_per_cu_data *per_cu = item.per_cu;
8624
8625 if (!per_objfile->symtab_set_p (per_cu))
8626 {
8627 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8628
8629 /* Skip dummy CUs. */
8630 if (cu != nullptr)
8631 {
8632 unsigned int debug_print_threshold;
8633 char buf[100];
8634
8635 if (per_cu->is_debug_types)
8636 {
8637 struct signatured_type *sig_type =
8638 (struct signatured_type *) per_cu;
8639
8640 sprintf (buf, "TU %s at offset %s",
8641 hex_string (sig_type->signature),
8642 sect_offset_str (per_cu->sect_off));
8643 /* There can be 100s of TUs.
8644 Only print them in verbose mode. */
8645 debug_print_threshold = 2;
8646 }
8647 else
8648 {
8649 sprintf (buf, "CU at offset %s",
8650 sect_offset_str (per_cu->sect_off));
8651 debug_print_threshold = 1;
8652 }
8653
8654 if (dwarf_read_debug >= debug_print_threshold)
8655 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8656
8657 if (per_cu->is_debug_types)
8658 process_full_type_unit (cu, item.pretend_language);
8659 else
8660 process_full_comp_unit (cu, item.pretend_language);
8661
8662 if (dwarf_read_debug >= debug_print_threshold)
8663 dwarf_read_debug_printf ("Done expanding %s", buf);
8664 }
8665 }
8666
8667 per_cu->queued = 0;
8668 per_objfile->per_bfd->queue->pop ();
8669 }
8670
8671 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8672 objfile_name (per_objfile->objfile));
8673 }
8674
8675 /* Read in full symbols for PST, and anything it depends on. */
8676
8677 void
8678 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8679 {
8680 gdb_assert (!readin_p (objfile));
8681
8682 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8683 free_cached_comp_units freer (per_objfile);
8684 expand_dependencies (objfile);
8685
8686 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8687 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8688 }
8689
8690 /* See psympriv.h. */
8691
8692 bool
8693 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8694 {
8695 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8696 return per_objfile->symtab_set_p (per_cu_data);
8697 }
8698
8699 /* See psympriv.h. */
8700
8701 compunit_symtab *
8702 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8703 {
8704 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8705 return per_objfile->get_symtab (per_cu_data);
8706 }
8707
8708 /* Trivial hash function for die_info: the hash value of a DIE
8709 is its offset in .debug_info for this objfile. */
8710
8711 static hashval_t
8712 die_hash (const void *item)
8713 {
8714 const struct die_info *die = (const struct die_info *) item;
8715
8716 return to_underlying (die->sect_off);
8717 }
8718
8719 /* Trivial comparison function for die_info structures: two DIEs
8720 are equal if they have the same offset. */
8721
8722 static int
8723 die_eq (const void *item_lhs, const void *item_rhs)
8724 {
8725 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8726 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8727
8728 return die_lhs->sect_off == die_rhs->sect_off;
8729 }
8730
8731 /* Load the DIEs associated with PER_CU into memory.
8732
8733 In some cases, the caller, while reading partial symbols, will need to load
8734 the full symbols for the CU for some reason. It will already have a
8735 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8736 rather than creating a new one. */
8737
8738 static void
8739 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8740 dwarf2_per_objfile *per_objfile,
8741 dwarf2_cu *existing_cu,
8742 bool skip_partial,
8743 enum language pretend_language)
8744 {
8745 gdb_assert (! this_cu->is_debug_types);
8746
8747 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8748 if (reader.dummy_p)
8749 return;
8750
8751 struct dwarf2_cu *cu = reader.cu;
8752 const gdb_byte *info_ptr = reader.info_ptr;
8753
8754 gdb_assert (cu->die_hash == NULL);
8755 cu->die_hash =
8756 htab_create_alloc_ex (cu->header.length / 12,
8757 die_hash,
8758 die_eq,
8759 NULL,
8760 &cu->comp_unit_obstack,
8761 hashtab_obstack_allocate,
8762 dummy_obstack_deallocate);
8763
8764 if (reader.comp_unit_die->has_children)
8765 reader.comp_unit_die->child
8766 = read_die_and_siblings (&reader, reader.info_ptr,
8767 &info_ptr, reader.comp_unit_die);
8768 cu->dies = reader.comp_unit_die;
8769 /* comp_unit_die is not stored in die_hash, no need. */
8770
8771 /* We try not to read any attributes in this function, because not
8772 all CUs needed for references have been loaded yet, and symbol
8773 table processing isn't initialized. But we have to set the CU language,
8774 or we won't be able to build types correctly.
8775 Similarly, if we do not read the producer, we can not apply
8776 producer-specific interpretation. */
8777 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8778
8779 reader.keep ();
8780 }
8781
8782 /* Add a DIE to the delayed physname list. */
8783
8784 static void
8785 add_to_method_list (struct type *type, int fnfield_index, int index,
8786 const char *name, struct die_info *die,
8787 struct dwarf2_cu *cu)
8788 {
8789 struct delayed_method_info mi;
8790 mi.type = type;
8791 mi.fnfield_index = fnfield_index;
8792 mi.index = index;
8793 mi.name = name;
8794 mi.die = die;
8795 cu->method_list.push_back (mi);
8796 }
8797
8798 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8799 "const" / "volatile". If so, decrements LEN by the length of the
8800 modifier and return true. Otherwise return false. */
8801
8802 template<size_t N>
8803 static bool
8804 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8805 {
8806 size_t mod_len = sizeof (mod) - 1;
8807 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8808 {
8809 len -= mod_len;
8810 return true;
8811 }
8812 return false;
8813 }
8814
8815 /* Compute the physnames of any methods on the CU's method list.
8816
8817 The computation of method physnames is delayed in order to avoid the
8818 (bad) condition that one of the method's formal parameters is of an as yet
8819 incomplete type. */
8820
8821 static void
8822 compute_delayed_physnames (struct dwarf2_cu *cu)
8823 {
8824 /* Only C++ delays computing physnames. */
8825 if (cu->method_list.empty ())
8826 return;
8827 gdb_assert (cu->per_cu->lang == language_cplus);
8828
8829 for (const delayed_method_info &mi : cu->method_list)
8830 {
8831 const char *physname;
8832 struct fn_fieldlist *fn_flp
8833 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8834 physname = dwarf2_physname (mi.name, mi.die, cu);
8835 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8836 = physname ? physname : "";
8837
8838 /* Since there's no tag to indicate whether a method is a
8839 const/volatile overload, extract that information out of the
8840 demangled name. */
8841 if (physname != NULL)
8842 {
8843 size_t len = strlen (physname);
8844
8845 while (1)
8846 {
8847 if (physname[len] == ')') /* shortcut */
8848 break;
8849 else if (check_modifier (physname, len, " const"))
8850 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8851 else if (check_modifier (physname, len, " volatile"))
8852 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8853 else
8854 break;
8855 }
8856 }
8857 }
8858
8859 /* The list is no longer needed. */
8860 cu->method_list.clear ();
8861 }
8862
8863 /* Go objects should be embedded in a DW_TAG_module DIE,
8864 and it's not clear if/how imported objects will appear.
8865 To keep Go support simple until that's worked out,
8866 go back through what we've read and create something usable.
8867 We could do this while processing each DIE, and feels kinda cleaner,
8868 but that way is more invasive.
8869 This is to, for example, allow the user to type "p var" or "b main"
8870 without having to specify the package name, and allow lookups
8871 of module.object to work in contexts that use the expression
8872 parser. */
8873
8874 static void
8875 fixup_go_packaging (struct dwarf2_cu *cu)
8876 {
8877 gdb::unique_xmalloc_ptr<char> package_name;
8878 struct pending *list;
8879 int i;
8880
8881 for (list = *cu->get_builder ()->get_global_symbols ();
8882 list != NULL;
8883 list = list->next)
8884 {
8885 for (i = 0; i < list->nsyms; ++i)
8886 {
8887 struct symbol *sym = list->symbol[i];
8888
8889 if (sym->language () == language_go
8890 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8891 {
8892 gdb::unique_xmalloc_ptr<char> this_package_name
8893 (go_symbol_package_name (sym));
8894
8895 if (this_package_name == NULL)
8896 continue;
8897 if (package_name == NULL)
8898 package_name = std::move (this_package_name);
8899 else
8900 {
8901 struct objfile *objfile = cu->per_objfile->objfile;
8902 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8903 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8904 (symbol_symtab (sym) != NULL
8905 ? symtab_to_filename_for_display
8906 (symbol_symtab (sym))
8907 : objfile_name (objfile)),
8908 this_package_name.get (), package_name.get ());
8909 }
8910 }
8911 }
8912 }
8913
8914 if (package_name != NULL)
8915 {
8916 struct objfile *objfile = cu->per_objfile->objfile;
8917 const char *saved_package_name = objfile->intern (package_name.get ());
8918 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8919 saved_package_name);
8920 struct symbol *sym;
8921
8922 sym = new (&objfile->objfile_obstack) symbol;
8923 sym->set_language (language_go, &objfile->objfile_obstack);
8924 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8925 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8926 e.g., "main" finds the "main" module and not C's main(). */
8927 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8928 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8929 SYMBOL_TYPE (sym) = type;
8930
8931 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8932 }
8933 }
8934
8935 /* Allocate a fully-qualified name consisting of the two parts on the
8936 obstack. */
8937
8938 static const char *
8939 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8940 {
8941 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8942 }
8943
8944 /* A helper that allocates a variant part to attach to a Rust enum
8945 type. OBSTACK is where the results should be allocated. TYPE is
8946 the type we're processing. DISCRIMINANT_INDEX is the index of the
8947 discriminant. It must be the index of one of the fields of TYPE,
8948 or -1 to mean there is no discriminant (univariant enum).
8949 DEFAULT_INDEX is the index of the default field; or -1 if there is
8950 no default. RANGES is indexed by "effective" field number (the
8951 field index, but omitting the discriminant and default fields) and
8952 must hold the discriminant values used by the variants. Note that
8953 RANGES must have a lifetime at least as long as OBSTACK -- either
8954 already allocated on it, or static. */
8955
8956 static void
8957 alloc_rust_variant (struct obstack *obstack, struct type *type,
8958 int discriminant_index, int default_index,
8959 gdb::array_view<discriminant_range> ranges)
8960 {
8961 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8962 gdb_assert (discriminant_index == -1
8963 || (discriminant_index >= 0
8964 && discriminant_index < type->num_fields ()));
8965 gdb_assert (default_index == -1
8966 || (default_index >= 0 && default_index < type->num_fields ()));
8967
8968 /* We have one variant for each non-discriminant field. */
8969 int n_variants = type->num_fields ();
8970 if (discriminant_index != -1)
8971 --n_variants;
8972
8973 variant *variants = new (obstack) variant[n_variants];
8974 int var_idx = 0;
8975 int range_idx = 0;
8976 for (int i = 0; i < type->num_fields (); ++i)
8977 {
8978 if (i == discriminant_index)
8979 continue;
8980
8981 variants[var_idx].first_field = i;
8982 variants[var_idx].last_field = i + 1;
8983
8984 /* The default field does not need a range, but other fields do.
8985 We skipped the discriminant above. */
8986 if (i != default_index)
8987 {
8988 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8989 ++range_idx;
8990 }
8991
8992 ++var_idx;
8993 }
8994
8995 gdb_assert (range_idx == ranges.size ());
8996 gdb_assert (var_idx == n_variants);
8997
8998 variant_part *part = new (obstack) variant_part;
8999 part->discriminant_index = discriminant_index;
9000 /* If there is no discriminant, then whether it is signed is of no
9001 consequence. */
9002 part->is_unsigned
9003 = (discriminant_index == -1
9004 ? false
9005 : type->field (discriminant_index).type ()->is_unsigned ());
9006 part->variants = gdb::array_view<variant> (variants, n_variants);
9007
9008 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9009 gdb::array_view<variant_part> *prop_value
9010 = new (storage) gdb::array_view<variant_part> (part, 1);
9011
9012 struct dynamic_prop prop;
9013 prop.set_variant_parts (prop_value);
9014
9015 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9016 }
9017
9018 /* Some versions of rustc emitted enums in an unusual way.
9019
9020 Ordinary enums were emitted as unions. The first element of each
9021 structure in the union was named "RUST$ENUM$DISR". This element
9022 held the discriminant.
9023
9024 These versions of Rust also implemented the "non-zero"
9025 optimization. When the enum had two values, and one is empty and
9026 the other holds a pointer that cannot be zero, the pointer is used
9027 as the discriminant, with a zero value meaning the empty variant.
9028 Here, the union's first member is of the form
9029 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9030 where the fieldnos are the indices of the fields that should be
9031 traversed in order to find the field (which may be several fields deep)
9032 and the variantname is the name of the variant of the case when the
9033 field is zero.
9034
9035 This function recognizes whether TYPE is of one of these forms,
9036 and, if so, smashes it to be a variant type. */
9037
9038 static void
9039 quirk_rust_enum (struct type *type, struct objfile *objfile)
9040 {
9041 gdb_assert (type->code () == TYPE_CODE_UNION);
9042
9043 /* We don't need to deal with empty enums. */
9044 if (type->num_fields () == 0)
9045 return;
9046
9047 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9048 if (type->num_fields () == 1
9049 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9050 {
9051 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9052
9053 /* Decode the field name to find the offset of the
9054 discriminant. */
9055 ULONGEST bit_offset = 0;
9056 struct type *field_type = type->field (0).type ();
9057 while (name[0] >= '0' && name[0] <= '9')
9058 {
9059 char *tail;
9060 unsigned long index = strtoul (name, &tail, 10);
9061 name = tail;
9062 if (*name != '$'
9063 || index >= field_type->num_fields ()
9064 || (TYPE_FIELD_LOC_KIND (field_type, index)
9065 != FIELD_LOC_KIND_BITPOS))
9066 {
9067 complaint (_("Could not parse Rust enum encoding string \"%s\""
9068 "[in module %s]"),
9069 TYPE_FIELD_NAME (type, 0),
9070 objfile_name (objfile));
9071 return;
9072 }
9073 ++name;
9074
9075 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9076 field_type = field_type->field (index).type ();
9077 }
9078
9079 /* Smash this type to be a structure type. We have to do this
9080 because the type has already been recorded. */
9081 type->set_code (TYPE_CODE_STRUCT);
9082 type->set_num_fields (3);
9083 /* Save the field we care about. */
9084 struct field saved_field = type->field (0);
9085 type->set_fields
9086 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9087
9088 /* Put the discriminant at index 0. */
9089 type->field (0).set_type (field_type);
9090 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9091 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9092 SET_FIELD_BITPOS (type->field (0), bit_offset);
9093
9094 /* The order of fields doesn't really matter, so put the real
9095 field at index 1 and the data-less field at index 2. */
9096 type->field (1) = saved_field;
9097 TYPE_FIELD_NAME (type, 1)
9098 = rust_last_path_segment (type->field (1).type ()->name ());
9099 type->field (1).type ()->set_name
9100 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9101 TYPE_FIELD_NAME (type, 1)));
9102
9103 const char *dataless_name
9104 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9105 name);
9106 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9107 dataless_name);
9108 type->field (2).set_type (dataless_type);
9109 /* NAME points into the original discriminant name, which
9110 already has the correct lifetime. */
9111 TYPE_FIELD_NAME (type, 2) = name;
9112 SET_FIELD_BITPOS (type->field (2), 0);
9113
9114 /* Indicate that this is a variant type. */
9115 static discriminant_range ranges[1] = { { 0, 0 } };
9116 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9117 }
9118 /* A union with a single anonymous field is probably an old-style
9119 univariant enum. */
9120 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9121 {
9122 /* Smash this type to be a structure type. We have to do this
9123 because the type has already been recorded. */
9124 type->set_code (TYPE_CODE_STRUCT);
9125
9126 struct type *field_type = type->field (0).type ();
9127 const char *variant_name
9128 = rust_last_path_segment (field_type->name ());
9129 TYPE_FIELD_NAME (type, 0) = variant_name;
9130 field_type->set_name
9131 (rust_fully_qualify (&objfile->objfile_obstack,
9132 type->name (), variant_name));
9133
9134 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9135 }
9136 else
9137 {
9138 struct type *disr_type = nullptr;
9139 for (int i = 0; i < type->num_fields (); ++i)
9140 {
9141 disr_type = type->field (i).type ();
9142
9143 if (disr_type->code () != TYPE_CODE_STRUCT)
9144 {
9145 /* All fields of a true enum will be structs. */
9146 return;
9147 }
9148 else if (disr_type->num_fields () == 0)
9149 {
9150 /* Could be data-less variant, so keep going. */
9151 disr_type = nullptr;
9152 }
9153 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9154 "RUST$ENUM$DISR") != 0)
9155 {
9156 /* Not a Rust enum. */
9157 return;
9158 }
9159 else
9160 {
9161 /* Found one. */
9162 break;
9163 }
9164 }
9165
9166 /* If we got here without a discriminant, then it's probably
9167 just a union. */
9168 if (disr_type == nullptr)
9169 return;
9170
9171 /* Smash this type to be a structure type. We have to do this
9172 because the type has already been recorded. */
9173 type->set_code (TYPE_CODE_STRUCT);
9174
9175 /* Make space for the discriminant field. */
9176 struct field *disr_field = &disr_type->field (0);
9177 field *new_fields
9178 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9179 * sizeof (struct field)));
9180 memcpy (new_fields + 1, type->fields (),
9181 type->num_fields () * sizeof (struct field));
9182 type->set_fields (new_fields);
9183 type->set_num_fields (type->num_fields () + 1);
9184
9185 /* Install the discriminant at index 0 in the union. */
9186 type->field (0) = *disr_field;
9187 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9188 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9189
9190 /* We need a way to find the correct discriminant given a
9191 variant name. For convenience we build a map here. */
9192 struct type *enum_type = disr_field->type ();
9193 std::unordered_map<std::string, ULONGEST> discriminant_map;
9194 for (int i = 0; i < enum_type->num_fields (); ++i)
9195 {
9196 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9197 {
9198 const char *name
9199 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9200 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9201 }
9202 }
9203
9204 int n_fields = type->num_fields ();
9205 /* We don't need a range entry for the discriminant, but we do
9206 need one for every other field, as there is no default
9207 variant. */
9208 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9209 discriminant_range,
9210 n_fields - 1);
9211 /* Skip the discriminant here. */
9212 for (int i = 1; i < n_fields; ++i)
9213 {
9214 /* Find the final word in the name of this variant's type.
9215 That name can be used to look up the correct
9216 discriminant. */
9217 const char *variant_name
9218 = rust_last_path_segment (type->field (i).type ()->name ());
9219
9220 auto iter = discriminant_map.find (variant_name);
9221 if (iter != discriminant_map.end ())
9222 {
9223 ranges[i - 1].low = iter->second;
9224 ranges[i - 1].high = iter->second;
9225 }
9226
9227 /* In Rust, each element should have the size of the
9228 enclosing enum. */
9229 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9230
9231 /* Remove the discriminant field, if it exists. */
9232 struct type *sub_type = type->field (i).type ();
9233 if (sub_type->num_fields () > 0)
9234 {
9235 sub_type->set_num_fields (sub_type->num_fields () - 1);
9236 sub_type->set_fields (sub_type->fields () + 1);
9237 }
9238 TYPE_FIELD_NAME (type, i) = variant_name;
9239 sub_type->set_name
9240 (rust_fully_qualify (&objfile->objfile_obstack,
9241 type->name (), variant_name));
9242 }
9243
9244 /* Indicate that this is a variant type. */
9245 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9246 gdb::array_view<discriminant_range> (ranges,
9247 n_fields - 1));
9248 }
9249 }
9250
9251 /* Rewrite some Rust unions to be structures with variants parts. */
9252
9253 static void
9254 rust_union_quirks (struct dwarf2_cu *cu)
9255 {
9256 gdb_assert (cu->per_cu->lang == language_rust);
9257 for (type *type_ : cu->rust_unions)
9258 quirk_rust_enum (type_, cu->per_objfile->objfile);
9259 /* We don't need this any more. */
9260 cu->rust_unions.clear ();
9261 }
9262
9263 /* See read.h. */
9264
9265 type_unit_group_unshareable *
9266 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9267 {
9268 auto iter = this->m_type_units.find (tu_group);
9269 if (iter != this->m_type_units.end ())
9270 return iter->second.get ();
9271
9272 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9273 type_unit_group_unshareable *result = uniq.get ();
9274 this->m_type_units[tu_group] = std::move (uniq);
9275 return result;
9276 }
9277
9278 struct type *
9279 dwarf2_per_objfile::get_type_for_signatured_type
9280 (signatured_type *sig_type) const
9281 {
9282 auto iter = this->m_type_map.find (sig_type);
9283 if (iter == this->m_type_map.end ())
9284 return nullptr;
9285
9286 return iter->second;
9287 }
9288
9289 void dwarf2_per_objfile::set_type_for_signatured_type
9290 (signatured_type *sig_type, struct type *type)
9291 {
9292 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9293
9294 this->m_type_map[sig_type] = type;
9295 }
9296
9297 /* A helper function for computing the list of all symbol tables
9298 included by PER_CU. */
9299
9300 static void
9301 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9302 htab_t all_children, htab_t all_type_symtabs,
9303 dwarf2_per_cu_data *per_cu,
9304 dwarf2_per_objfile *per_objfile,
9305 struct compunit_symtab *immediate_parent)
9306 {
9307 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9308 if (*slot != NULL)
9309 {
9310 /* This inclusion and its children have been processed. */
9311 return;
9312 }
9313
9314 *slot = per_cu;
9315
9316 /* Only add a CU if it has a symbol table. */
9317 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9318 if (cust != NULL)
9319 {
9320 /* If this is a type unit only add its symbol table if we haven't
9321 seen it yet (type unit per_cu's can share symtabs). */
9322 if (per_cu->is_debug_types)
9323 {
9324 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9325 if (*slot == NULL)
9326 {
9327 *slot = cust;
9328 result->push_back (cust);
9329 if (cust->user == NULL)
9330 cust->user = immediate_parent;
9331 }
9332 }
9333 else
9334 {
9335 result->push_back (cust);
9336 if (cust->user == NULL)
9337 cust->user = immediate_parent;
9338 }
9339 }
9340
9341 if (!per_cu->imported_symtabs_empty ())
9342 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9343 {
9344 recursively_compute_inclusions (result, all_children,
9345 all_type_symtabs, ptr, per_objfile,
9346 cust);
9347 }
9348 }
9349
9350 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9351 PER_CU. */
9352
9353 static void
9354 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9355 dwarf2_per_objfile *per_objfile)
9356 {
9357 gdb_assert (! per_cu->is_debug_types);
9358
9359 if (!per_cu->imported_symtabs_empty ())
9360 {
9361 int len;
9362 std::vector<compunit_symtab *> result_symtabs;
9363 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9364
9365 /* If we don't have a symtab, we can just skip this case. */
9366 if (cust == NULL)
9367 return;
9368
9369 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9370 htab_eq_pointer,
9371 NULL, xcalloc, xfree));
9372 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9373 htab_eq_pointer,
9374 NULL, xcalloc, xfree));
9375
9376 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9377 {
9378 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9379 all_type_symtabs.get (), ptr,
9380 per_objfile, cust);
9381 }
9382
9383 /* Now we have a transitive closure of all the included symtabs. */
9384 len = result_symtabs.size ();
9385 cust->includes
9386 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9387 struct compunit_symtab *, len + 1);
9388 memcpy (cust->includes, result_symtabs.data (),
9389 len * sizeof (compunit_symtab *));
9390 cust->includes[len] = NULL;
9391 }
9392 }
9393
9394 /* Compute the 'includes' field for the symtabs of all the CUs we just
9395 read. */
9396
9397 static void
9398 process_cu_includes (dwarf2_per_objfile *per_objfile)
9399 {
9400 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9401 {
9402 if (! iter->is_debug_types)
9403 compute_compunit_symtab_includes (iter, per_objfile);
9404 }
9405
9406 per_objfile->per_bfd->just_read_cus.clear ();
9407 }
9408
9409 /* Generate full symbol information for CU, whose DIEs have
9410 already been loaded into memory. */
9411
9412 static void
9413 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9414 {
9415 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9416 struct objfile *objfile = per_objfile->objfile;
9417 struct gdbarch *gdbarch = objfile->arch ();
9418 CORE_ADDR lowpc, highpc;
9419 struct compunit_symtab *cust;
9420 CORE_ADDR baseaddr;
9421 struct block *static_block;
9422 CORE_ADDR addr;
9423
9424 baseaddr = objfile->text_section_offset ();
9425
9426 /* Clear the list here in case something was left over. */
9427 cu->method_list.clear ();
9428
9429 dwarf2_find_base_address (cu->dies, cu);
9430
9431 /* Before we start reading the top-level DIE, ensure it has a valid tag
9432 type. */
9433 switch (cu->dies->tag)
9434 {
9435 case DW_TAG_compile_unit:
9436 case DW_TAG_partial_unit:
9437 case DW_TAG_type_unit:
9438 break;
9439 default:
9440 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9441 dwarf_tag_name (cu->dies->tag),
9442 sect_offset_str (cu->per_cu->sect_off),
9443 objfile_name (per_objfile->objfile));
9444 }
9445
9446 /* Do line number decoding in read_file_scope () */
9447 process_die (cu->dies, cu);
9448
9449 /* For now fudge the Go package. */
9450 if (cu->per_cu->lang == language_go)
9451 fixup_go_packaging (cu);
9452
9453 /* Now that we have processed all the DIEs in the CU, all the types
9454 should be complete, and it should now be safe to compute all of the
9455 physnames. */
9456 compute_delayed_physnames (cu);
9457
9458 if (cu->per_cu->lang == language_rust)
9459 rust_union_quirks (cu);
9460
9461 /* Some compilers don't define a DW_AT_high_pc attribute for the
9462 compilation unit. If the DW_AT_high_pc is missing, synthesize
9463 it, by scanning the DIE's below the compilation unit. */
9464 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9465
9466 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9467 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9468
9469 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9470 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9471 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9472 addrmap to help ensure it has an accurate map of pc values belonging to
9473 this comp unit. */
9474 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9475
9476 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9477 SECT_OFF_TEXT (objfile),
9478 0);
9479
9480 if (cust != NULL)
9481 {
9482 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9483
9484 /* Set symtab language to language from DW_AT_language. If the
9485 compilation is from a C file generated by language preprocessors, do
9486 not set the language if it was already deduced by start_subfile. */
9487 if (!(cu->per_cu->lang == language_c
9488 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9489 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
9490
9491 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9492 produce DW_AT_location with location lists but it can be possibly
9493 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9494 there were bugs in prologue debug info, fixed later in GCC-4.5
9495 by "unwind info for epilogues" patch (which is not directly related).
9496
9497 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9498 needed, it would be wrong due to missing DW_AT_producer there.
9499
9500 Still one can confuse GDB by using non-standard GCC compilation
9501 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9502 */
9503 if (cu->has_loclist && gcc_4_minor >= 5)
9504 cust->locations_valid = 1;
9505
9506 if (gcc_4_minor >= 5)
9507 cust->epilogue_unwind_valid = 1;
9508
9509 cust->call_site_htab = cu->call_site_htab;
9510 }
9511
9512 per_objfile->set_symtab (cu->per_cu, cust);
9513
9514 /* Push it for inclusion processing later. */
9515 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9516
9517 /* Not needed any more. */
9518 cu->reset_builder ();
9519 }
9520
9521 /* Generate full symbol information for type unit CU, whose DIEs have
9522 already been loaded into memory. */
9523
9524 static void
9525 process_full_type_unit (dwarf2_cu *cu,
9526 enum language pretend_language)
9527 {
9528 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9529 struct objfile *objfile = per_objfile->objfile;
9530 struct compunit_symtab *cust;
9531 struct signatured_type *sig_type;
9532
9533 gdb_assert (cu->per_cu->is_debug_types);
9534 sig_type = (struct signatured_type *) cu->per_cu;
9535
9536 /* Clear the list here in case something was left over. */
9537 cu->method_list.clear ();
9538
9539 /* The symbol tables are set up in read_type_unit_scope. */
9540 process_die (cu->dies, cu);
9541
9542 /* For now fudge the Go package. */
9543 if (cu->per_cu->lang == language_go)
9544 fixup_go_packaging (cu);
9545
9546 /* Now that we have processed all the DIEs in the CU, all the types
9547 should be complete, and it should now be safe to compute all of the
9548 physnames. */
9549 compute_delayed_physnames (cu);
9550
9551 if (cu->per_cu->lang == language_rust)
9552 rust_union_quirks (cu);
9553
9554 /* TUs share symbol tables.
9555 If this is the first TU to use this symtab, complete the construction
9556 of it with end_expandable_symtab. Otherwise, complete the addition of
9557 this TU's symbols to the existing symtab. */
9558 type_unit_group_unshareable *tug_unshare =
9559 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9560 if (tug_unshare->compunit_symtab == NULL)
9561 {
9562 buildsym_compunit *builder = cu->get_builder ();
9563 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9564 tug_unshare->compunit_symtab = cust;
9565
9566 if (cust != NULL)
9567 {
9568 /* Set symtab language to language from DW_AT_language. If the
9569 compilation is from a C file generated by language preprocessors,
9570 do not set the language if it was already deduced by
9571 start_subfile. */
9572 if (!(cu->per_cu->lang == language_c
9573 && COMPUNIT_FILETABS (cust)->language != language_c))
9574 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
9575 }
9576 }
9577 else
9578 {
9579 cu->get_builder ()->augment_type_symtab ();
9580 cust = tug_unshare->compunit_symtab;
9581 }
9582
9583 per_objfile->set_symtab (cu->per_cu, cust);
9584
9585 /* Not needed any more. */
9586 cu->reset_builder ();
9587 }
9588
9589 /* Process an imported unit DIE. */
9590
9591 static void
9592 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9593 {
9594 struct attribute *attr;
9595
9596 /* For now we don't handle imported units in type units. */
9597 if (cu->per_cu->is_debug_types)
9598 {
9599 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9600 " supported in type units [in module %s]"),
9601 objfile_name (cu->per_objfile->objfile));
9602 }
9603
9604 attr = dwarf2_attr (die, DW_AT_import, cu);
9605 if (attr != NULL)
9606 {
9607 sect_offset sect_off = attr->get_ref_die_offset ();
9608 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9609 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9610 dwarf2_per_cu_data *per_cu
9611 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9612
9613 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9614 into another compilation unit, at root level. Regard this as a hint,
9615 and ignore it. */
9616 if (die->parent && die->parent->parent == NULL
9617 && per_cu->unit_type == DW_UT_compile
9618 && per_cu->lang == language_cplus)
9619 return;
9620
9621 /* If necessary, add it to the queue and load its DIEs. */
9622 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9623 cu->per_cu->lang))
9624 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9625 false, cu->per_cu->lang);
9626
9627 cu->per_cu->imported_symtabs_push (per_cu);
9628 }
9629 }
9630
9631 /* RAII object that represents a process_die scope: i.e.,
9632 starts/finishes processing a DIE. */
9633 class process_die_scope
9634 {
9635 public:
9636 process_die_scope (die_info *die, dwarf2_cu *cu)
9637 : m_die (die), m_cu (cu)
9638 {
9639 /* We should only be processing DIEs not already in process. */
9640 gdb_assert (!m_die->in_process);
9641 m_die->in_process = true;
9642 }
9643
9644 ~process_die_scope ()
9645 {
9646 m_die->in_process = false;
9647
9648 /* If we're done processing the DIE for the CU that owns the line
9649 header, we don't need the line header anymore. */
9650 if (m_cu->line_header_die_owner == m_die)
9651 {
9652 delete m_cu->line_header;
9653 m_cu->line_header = NULL;
9654 m_cu->line_header_die_owner = NULL;
9655 }
9656 }
9657
9658 private:
9659 die_info *m_die;
9660 dwarf2_cu *m_cu;
9661 };
9662
9663 /* Process a die and its children. */
9664
9665 static void
9666 process_die (struct die_info *die, struct dwarf2_cu *cu)
9667 {
9668 process_die_scope scope (die, cu);
9669
9670 switch (die->tag)
9671 {
9672 case DW_TAG_padding:
9673 break;
9674 case DW_TAG_compile_unit:
9675 case DW_TAG_partial_unit:
9676 read_file_scope (die, cu);
9677 break;
9678 case DW_TAG_type_unit:
9679 read_type_unit_scope (die, cu);
9680 break;
9681 case DW_TAG_subprogram:
9682 /* Nested subprograms in Fortran get a prefix. */
9683 if (cu->per_cu->lang == language_fortran
9684 && die->parent != NULL
9685 && die->parent->tag == DW_TAG_subprogram)
9686 cu->processing_has_namespace_info = true;
9687 /* Fall through. */
9688 case DW_TAG_inlined_subroutine:
9689 read_func_scope (die, cu);
9690 break;
9691 case DW_TAG_lexical_block:
9692 case DW_TAG_try_block:
9693 case DW_TAG_catch_block:
9694 read_lexical_block_scope (die, cu);
9695 break;
9696 case DW_TAG_call_site:
9697 case DW_TAG_GNU_call_site:
9698 read_call_site_scope (die, cu);
9699 break;
9700 case DW_TAG_class_type:
9701 case DW_TAG_interface_type:
9702 case DW_TAG_structure_type:
9703 case DW_TAG_union_type:
9704 process_structure_scope (die, cu);
9705 break;
9706 case DW_TAG_enumeration_type:
9707 process_enumeration_scope (die, cu);
9708 break;
9709
9710 /* These dies have a type, but processing them does not create
9711 a symbol or recurse to process the children. Therefore we can
9712 read them on-demand through read_type_die. */
9713 case DW_TAG_subroutine_type:
9714 case DW_TAG_set_type:
9715 case DW_TAG_pointer_type:
9716 case DW_TAG_ptr_to_member_type:
9717 case DW_TAG_reference_type:
9718 case DW_TAG_rvalue_reference_type:
9719 case DW_TAG_string_type:
9720 break;
9721
9722 case DW_TAG_array_type:
9723 /* We only need to handle this case for Ada -- in other
9724 languages, it's normal for the compiler to emit a typedef
9725 instead. */
9726 if (cu->per_cu->lang != language_ada)
9727 break;
9728 /* FALLTHROUGH */
9729 case DW_TAG_base_type:
9730 case DW_TAG_subrange_type:
9731 case DW_TAG_typedef:
9732 /* Add a typedef symbol for the type definition, if it has a
9733 DW_AT_name. */
9734 new_symbol (die, read_type_die (die, cu), cu);
9735 break;
9736 case DW_TAG_common_block:
9737 read_common_block (die, cu);
9738 break;
9739 case DW_TAG_common_inclusion:
9740 break;
9741 case DW_TAG_namespace:
9742 cu->processing_has_namespace_info = true;
9743 read_namespace (die, cu);
9744 break;
9745 case DW_TAG_module:
9746 cu->processing_has_namespace_info = true;
9747 read_module (die, cu);
9748 break;
9749 case DW_TAG_imported_declaration:
9750 cu->processing_has_namespace_info = true;
9751 if (read_namespace_alias (die, cu))
9752 break;
9753 /* The declaration is not a global namespace alias. */
9754 /* Fall through. */
9755 case DW_TAG_imported_module:
9756 cu->processing_has_namespace_info = true;
9757 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9758 || cu->per_cu->lang != language_fortran))
9759 complaint (_("Tag '%s' has unexpected children"),
9760 dwarf_tag_name (die->tag));
9761 read_import_statement (die, cu);
9762 break;
9763
9764 case DW_TAG_imported_unit:
9765 process_imported_unit_die (die, cu);
9766 break;
9767
9768 case DW_TAG_variable:
9769 read_variable (die, cu);
9770 break;
9771
9772 default:
9773 new_symbol (die, NULL, cu);
9774 break;
9775 }
9776 }
9777 \f
9778 /* DWARF name computation. */
9779
9780 /* A helper function for dwarf2_compute_name which determines whether DIE
9781 needs to have the name of the scope prepended to the name listed in the
9782 die. */
9783
9784 static int
9785 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9786 {
9787 struct attribute *attr;
9788
9789 switch (die->tag)
9790 {
9791 case DW_TAG_namespace:
9792 case DW_TAG_typedef:
9793 case DW_TAG_class_type:
9794 case DW_TAG_interface_type:
9795 case DW_TAG_structure_type:
9796 case DW_TAG_union_type:
9797 case DW_TAG_enumeration_type:
9798 case DW_TAG_enumerator:
9799 case DW_TAG_subprogram:
9800 case DW_TAG_inlined_subroutine:
9801 case DW_TAG_member:
9802 case DW_TAG_imported_declaration:
9803 return 1;
9804
9805 case DW_TAG_variable:
9806 case DW_TAG_constant:
9807 /* We only need to prefix "globally" visible variables. These include
9808 any variable marked with DW_AT_external or any variable that
9809 lives in a namespace. [Variables in anonymous namespaces
9810 require prefixing, but they are not DW_AT_external.] */
9811
9812 if (dwarf2_attr (die, DW_AT_specification, cu))
9813 {
9814 struct dwarf2_cu *spec_cu = cu;
9815
9816 return die_needs_namespace (die_specification (die, &spec_cu),
9817 spec_cu);
9818 }
9819
9820 attr = dwarf2_attr (die, DW_AT_external, cu);
9821 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9822 && die->parent->tag != DW_TAG_module)
9823 return 0;
9824 /* A variable in a lexical block of some kind does not need a
9825 namespace, even though in C++ such variables may be external
9826 and have a mangled name. */
9827 if (die->parent->tag == DW_TAG_lexical_block
9828 || die->parent->tag == DW_TAG_try_block
9829 || die->parent->tag == DW_TAG_catch_block
9830 || die->parent->tag == DW_TAG_subprogram)
9831 return 0;
9832 return 1;
9833
9834 default:
9835 return 0;
9836 }
9837 }
9838
9839 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9840 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9841 defined for the given DIE. */
9842
9843 static struct attribute *
9844 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9845 {
9846 struct attribute *attr;
9847
9848 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9849 if (attr == NULL)
9850 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9851
9852 return attr;
9853 }
9854
9855 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9856 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9857 defined for the given DIE. */
9858
9859 static const char *
9860 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9861 {
9862 const char *linkage_name;
9863
9864 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9865 if (linkage_name == NULL)
9866 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9867
9868 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9869 See https://github.com/rust-lang/rust/issues/32925. */
9870 if (cu->per_cu->lang == language_rust && linkage_name != NULL
9871 && strchr (linkage_name, '{') != NULL)
9872 linkage_name = NULL;
9873
9874 return linkage_name;
9875 }
9876
9877 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9878 compute the physname for the object, which include a method's:
9879 - formal parameters (C++),
9880 - receiver type (Go),
9881
9882 The term "physname" is a bit confusing.
9883 For C++, for example, it is the demangled name.
9884 For Go, for example, it's the mangled name.
9885
9886 For Ada, return the DIE's linkage name rather than the fully qualified
9887 name. PHYSNAME is ignored..
9888
9889 The result is allocated on the objfile->per_bfd's obstack and
9890 canonicalized. */
9891
9892 static const char *
9893 dwarf2_compute_name (const char *name,
9894 struct die_info *die, struct dwarf2_cu *cu,
9895 int physname)
9896 {
9897 struct objfile *objfile = cu->per_objfile->objfile;
9898
9899 if (name == NULL)
9900 name = dwarf2_name (die, cu);
9901
9902 enum language lang = cu->per_cu->lang;
9903
9904 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9905 but otherwise compute it by typename_concat inside GDB.
9906 FIXME: Actually this is not really true, or at least not always true.
9907 It's all very confusing. compute_and_set_names doesn't try to demangle
9908 Fortran names because there is no mangling standard. So new_symbol
9909 will set the demangled name to the result of dwarf2_full_name, and it is
9910 the demangled name that GDB uses if it exists. */
9911 if (lang == language_ada
9912 || (lang == language_fortran && physname))
9913 {
9914 /* For Ada unit, we prefer the linkage name over the name, as
9915 the former contains the exported name, which the user expects
9916 to be able to reference. Ideally, we want the user to be able
9917 to reference this entity using either natural or linkage name,
9918 but we haven't started looking at this enhancement yet. */
9919 const char *linkage_name = dw2_linkage_name (die, cu);
9920
9921 if (linkage_name != NULL)
9922 return linkage_name;
9923 }
9924
9925 /* These are the only languages we know how to qualify names in. */
9926 if (name != NULL
9927 && (lang == language_cplus
9928 || lang == language_fortran || lang == language_d
9929 || lang == language_rust))
9930 {
9931 if (die_needs_namespace (die, cu))
9932 {
9933 const char *prefix;
9934 const char *canonical_name = NULL;
9935
9936 string_file buf;
9937
9938 prefix = determine_prefix (die, cu);
9939 if (*prefix != '\0')
9940 {
9941 gdb::unique_xmalloc_ptr<char> prefixed_name
9942 (typename_concat (NULL, prefix, name, physname, cu));
9943
9944 buf.puts (prefixed_name.get ());
9945 }
9946 else
9947 buf.puts (name);
9948
9949 /* Template parameters may be specified in the DIE's DW_AT_name, or
9950 as children with DW_TAG_template_type_param or
9951 DW_TAG_value_type_param. If the latter, add them to the name
9952 here. If the name already has template parameters, then
9953 skip this step; some versions of GCC emit both, and
9954 it is more efficient to use the pre-computed name.
9955
9956 Something to keep in mind about this process: it is very
9957 unlikely, or in some cases downright impossible, to produce
9958 something that will match the mangled name of a function.
9959 If the definition of the function has the same debug info,
9960 we should be able to match up with it anyway. But fallbacks
9961 using the minimal symbol, for instance to find a method
9962 implemented in a stripped copy of libstdc++, will not work.
9963 If we do not have debug info for the definition, we will have to
9964 match them up some other way.
9965
9966 When we do name matching there is a related problem with function
9967 templates; two instantiated function templates are allowed to
9968 differ only by their return types, which we do not add here. */
9969
9970 if (lang == language_cplus && strchr (name, '<') == NULL)
9971 {
9972 struct attribute *attr;
9973 struct die_info *child;
9974 int first = 1;
9975
9976 die->building_fullname = 1;
9977
9978 for (child = die->child; child != NULL; child = child->sibling)
9979 {
9980 struct type *type;
9981 LONGEST value;
9982 const gdb_byte *bytes;
9983 struct dwarf2_locexpr_baton *baton;
9984 struct value *v;
9985
9986 if (child->tag != DW_TAG_template_type_param
9987 && child->tag != DW_TAG_template_value_param)
9988 continue;
9989
9990 if (first)
9991 {
9992 buf.puts ("<");
9993 first = 0;
9994 }
9995 else
9996 buf.puts (", ");
9997
9998 attr = dwarf2_attr (child, DW_AT_type, cu);
9999 if (attr == NULL)
10000 {
10001 complaint (_("template parameter missing DW_AT_type"));
10002 buf.puts ("UNKNOWN_TYPE");
10003 continue;
10004 }
10005 type = die_type (child, cu);
10006
10007 if (child->tag == DW_TAG_template_type_param)
10008 {
10009 cu->language_defn->print_type (type, "", &buf, -1, 0,
10010 &type_print_raw_options);
10011 continue;
10012 }
10013
10014 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10015 if (attr == NULL)
10016 {
10017 complaint (_("template parameter missing "
10018 "DW_AT_const_value"));
10019 buf.puts ("UNKNOWN_VALUE");
10020 continue;
10021 }
10022
10023 dwarf2_const_value_attr (attr, type, name,
10024 &cu->comp_unit_obstack, cu,
10025 &value, &bytes, &baton);
10026
10027 if (type->has_no_signedness ())
10028 /* GDB prints characters as NUMBER 'CHAR'. If that's
10029 changed, this can use value_print instead. */
10030 cu->language_defn->printchar (value, type, &buf);
10031 else
10032 {
10033 struct value_print_options opts;
10034
10035 if (baton != NULL)
10036 v = dwarf2_evaluate_loc_desc (type, NULL,
10037 baton->data,
10038 baton->size,
10039 baton->per_cu,
10040 baton->per_objfile);
10041 else if (bytes != NULL)
10042 {
10043 v = allocate_value (type);
10044 memcpy (value_contents_writeable (v), bytes,
10045 TYPE_LENGTH (type));
10046 }
10047 else
10048 v = value_from_longest (type, value);
10049
10050 /* Specify decimal so that we do not depend on
10051 the radix. */
10052 get_formatted_print_options (&opts, 'd');
10053 opts.raw = 1;
10054 value_print (v, &buf, &opts);
10055 release_value (v);
10056 }
10057 }
10058
10059 die->building_fullname = 0;
10060
10061 if (!first)
10062 {
10063 /* Close the argument list, with a space if necessary
10064 (nested templates). */
10065 if (!buf.empty () && buf.string ().back () == '>')
10066 buf.puts (" >");
10067 else
10068 buf.puts (">");
10069 }
10070 }
10071
10072 /* For C++ methods, append formal parameter type
10073 information, if PHYSNAME. */
10074
10075 if (physname && die->tag == DW_TAG_subprogram
10076 && lang == language_cplus)
10077 {
10078 struct type *type = read_type_die (die, cu);
10079
10080 c_type_print_args (type, &buf, 1, lang,
10081 &type_print_raw_options);
10082
10083 if (lang == language_cplus)
10084 {
10085 /* Assume that an artificial first parameter is
10086 "this", but do not crash if it is not. RealView
10087 marks unnamed (and thus unused) parameters as
10088 artificial; there is no way to differentiate
10089 the two cases. */
10090 if (type->num_fields () > 0
10091 && TYPE_FIELD_ARTIFICIAL (type, 0)
10092 && type->field (0).type ()->code () == TYPE_CODE_PTR
10093 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10094 buf.puts (" const");
10095 }
10096 }
10097
10098 const std::string &intermediate_name = buf.string ();
10099
10100 if (lang == language_cplus)
10101 canonical_name
10102 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10103 objfile);
10104
10105 /* If we only computed INTERMEDIATE_NAME, or if
10106 INTERMEDIATE_NAME is already canonical, then we need to
10107 intern it. */
10108 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10109 name = objfile->intern (intermediate_name);
10110 else
10111 name = canonical_name;
10112 }
10113 }
10114
10115 return name;
10116 }
10117
10118 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10119 If scope qualifiers are appropriate they will be added. The result
10120 will be allocated on the storage_obstack, or NULL if the DIE does
10121 not have a name. NAME may either be from a previous call to
10122 dwarf2_name or NULL.
10123
10124 The output string will be canonicalized (if C++). */
10125
10126 static const char *
10127 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10128 {
10129 return dwarf2_compute_name (name, die, cu, 0);
10130 }
10131
10132 /* Construct a physname for the given DIE in CU. NAME may either be
10133 from a previous call to dwarf2_name or NULL. The result will be
10134 allocated on the objfile_objstack or NULL if the DIE does not have a
10135 name.
10136
10137 The output string will be canonicalized (if C++). */
10138
10139 static const char *
10140 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10141 {
10142 struct objfile *objfile = cu->per_objfile->objfile;
10143 const char *retval, *mangled = NULL, *canon = NULL;
10144 int need_copy = 1;
10145
10146 /* In this case dwarf2_compute_name is just a shortcut not building anything
10147 on its own. */
10148 if (!die_needs_namespace (die, cu))
10149 return dwarf2_compute_name (name, die, cu, 1);
10150
10151 if (cu->per_cu->lang != language_rust)
10152 mangled = dw2_linkage_name (die, cu);
10153
10154 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10155 has computed. */
10156 gdb::unique_xmalloc_ptr<char> demangled;
10157 if (mangled != NULL)
10158 {
10159 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10160 {
10161 /* Do nothing (do not demangle the symbol name). */
10162 }
10163 else
10164 {
10165 /* Use DMGL_RET_DROP for C++ template functions to suppress
10166 their return type. It is easier for GDB users to search
10167 for such functions as `name(params)' than `long name(params)'.
10168 In such case the minimal symbol names do not match the full
10169 symbol names but for template functions there is never a need
10170 to look up their definition from their declaration so
10171 the only disadvantage remains the minimal symbol variant
10172 `long name(params)' does not have the proper inferior type. */
10173 demangled.reset (gdb_demangle (mangled,
10174 (DMGL_PARAMS | DMGL_ANSI
10175 | DMGL_RET_DROP)));
10176 }
10177 if (demangled)
10178 canon = demangled.get ();
10179 else
10180 {
10181 canon = mangled;
10182 need_copy = 0;
10183 }
10184 }
10185
10186 if (canon == NULL || check_physname)
10187 {
10188 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10189
10190 if (canon != NULL && strcmp (physname, canon) != 0)
10191 {
10192 /* It may not mean a bug in GDB. The compiler could also
10193 compute DW_AT_linkage_name incorrectly. But in such case
10194 GDB would need to be bug-to-bug compatible. */
10195
10196 complaint (_("Computed physname <%s> does not match demangled <%s> "
10197 "(from linkage <%s>) - DIE at %s [in module %s]"),
10198 physname, canon, mangled, sect_offset_str (die->sect_off),
10199 objfile_name (objfile));
10200
10201 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10202 is available here - over computed PHYSNAME. It is safer
10203 against both buggy GDB and buggy compilers. */
10204
10205 retval = canon;
10206 }
10207 else
10208 {
10209 retval = physname;
10210 need_copy = 0;
10211 }
10212 }
10213 else
10214 retval = canon;
10215
10216 if (need_copy)
10217 retval = objfile->intern (retval);
10218
10219 return retval;
10220 }
10221
10222 /* Inspect DIE in CU for a namespace alias. If one exists, record
10223 a new symbol for it.
10224
10225 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10226
10227 static int
10228 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10229 {
10230 struct attribute *attr;
10231
10232 /* If the die does not have a name, this is not a namespace
10233 alias. */
10234 attr = dwarf2_attr (die, DW_AT_name, cu);
10235 if (attr != NULL)
10236 {
10237 int num;
10238 struct die_info *d = die;
10239 struct dwarf2_cu *imported_cu = cu;
10240
10241 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10242 keep inspecting DIEs until we hit the underlying import. */
10243 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10244 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10245 {
10246 attr = dwarf2_attr (d, DW_AT_import, cu);
10247 if (attr == NULL)
10248 break;
10249
10250 d = follow_die_ref (d, attr, &imported_cu);
10251 if (d->tag != DW_TAG_imported_declaration)
10252 break;
10253 }
10254
10255 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10256 {
10257 complaint (_("DIE at %s has too many recursively imported "
10258 "declarations"), sect_offset_str (d->sect_off));
10259 return 0;
10260 }
10261
10262 if (attr != NULL)
10263 {
10264 struct type *type;
10265 sect_offset sect_off = attr->get_ref_die_offset ();
10266
10267 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10268 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10269 {
10270 /* This declaration is a global namespace alias. Add
10271 a symbol for it whose type is the aliased namespace. */
10272 new_symbol (die, type, cu);
10273 return 1;
10274 }
10275 }
10276 }
10277
10278 return 0;
10279 }
10280
10281 /* Return the using directives repository (global or local?) to use in the
10282 current context for CU.
10283
10284 For Ada, imported declarations can materialize renamings, which *may* be
10285 global. However it is impossible (for now?) in DWARF to distinguish
10286 "external" imported declarations and "static" ones. As all imported
10287 declarations seem to be static in all other languages, make them all CU-wide
10288 global only in Ada. */
10289
10290 static struct using_direct **
10291 using_directives (struct dwarf2_cu *cu)
10292 {
10293 if (cu->per_cu->lang == language_ada
10294 && cu->get_builder ()->outermost_context_p ())
10295 return cu->get_builder ()->get_global_using_directives ();
10296 else
10297 return cu->get_builder ()->get_local_using_directives ();
10298 }
10299
10300 /* Read the import statement specified by the given die and record it. */
10301
10302 static void
10303 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10304 {
10305 struct objfile *objfile = cu->per_objfile->objfile;
10306 struct attribute *import_attr;
10307 struct die_info *imported_die, *child_die;
10308 struct dwarf2_cu *imported_cu;
10309 const char *imported_name;
10310 const char *imported_name_prefix;
10311 const char *canonical_name;
10312 const char *import_alias;
10313 const char *imported_declaration = NULL;
10314 const char *import_prefix;
10315 std::vector<const char *> excludes;
10316
10317 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10318 if (import_attr == NULL)
10319 {
10320 complaint (_("Tag '%s' has no DW_AT_import"),
10321 dwarf_tag_name (die->tag));
10322 return;
10323 }
10324
10325 imported_cu = cu;
10326 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10327 imported_name = dwarf2_name (imported_die, imported_cu);
10328 if (imported_name == NULL)
10329 {
10330 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10331
10332 The import in the following code:
10333 namespace A
10334 {
10335 typedef int B;
10336 }
10337
10338 int main ()
10339 {
10340 using A::B;
10341 B b;
10342 return b;
10343 }
10344
10345 ...
10346 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10347 <52> DW_AT_decl_file : 1
10348 <53> DW_AT_decl_line : 6
10349 <54> DW_AT_import : <0x75>
10350 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10351 <59> DW_AT_name : B
10352 <5b> DW_AT_decl_file : 1
10353 <5c> DW_AT_decl_line : 2
10354 <5d> DW_AT_type : <0x6e>
10355 ...
10356 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10357 <76> DW_AT_byte_size : 4
10358 <77> DW_AT_encoding : 5 (signed)
10359
10360 imports the wrong die ( 0x75 instead of 0x58 ).
10361 This case will be ignored until the gcc bug is fixed. */
10362 return;
10363 }
10364
10365 /* Figure out the local name after import. */
10366 import_alias = dwarf2_name (die, cu);
10367
10368 /* Figure out where the statement is being imported to. */
10369 import_prefix = determine_prefix (die, cu);
10370
10371 /* Figure out what the scope of the imported die is and prepend it
10372 to the name of the imported die. */
10373 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10374
10375 if (imported_die->tag != DW_TAG_namespace
10376 && imported_die->tag != DW_TAG_module)
10377 {
10378 imported_declaration = imported_name;
10379 canonical_name = imported_name_prefix;
10380 }
10381 else if (strlen (imported_name_prefix) > 0)
10382 canonical_name = obconcat (&objfile->objfile_obstack,
10383 imported_name_prefix,
10384 (cu->per_cu->lang == language_d
10385 ? "."
10386 : "::"),
10387 imported_name, (char *) NULL);
10388 else
10389 canonical_name = imported_name;
10390
10391 if (die->tag == DW_TAG_imported_module
10392 && cu->per_cu->lang == language_fortran)
10393 for (child_die = die->child; child_die && child_die->tag;
10394 child_die = child_die->sibling)
10395 {
10396 /* DWARF-4: A Fortran use statement with a “rename list” may be
10397 represented by an imported module entry with an import attribute
10398 referring to the module and owned entries corresponding to those
10399 entities that are renamed as part of being imported. */
10400
10401 if (child_die->tag != DW_TAG_imported_declaration)
10402 {
10403 complaint (_("child DW_TAG_imported_declaration expected "
10404 "- DIE at %s [in module %s]"),
10405 sect_offset_str (child_die->sect_off),
10406 objfile_name (objfile));
10407 continue;
10408 }
10409
10410 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10411 if (import_attr == NULL)
10412 {
10413 complaint (_("Tag '%s' has no DW_AT_import"),
10414 dwarf_tag_name (child_die->tag));
10415 continue;
10416 }
10417
10418 imported_cu = cu;
10419 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10420 &imported_cu);
10421 imported_name = dwarf2_name (imported_die, imported_cu);
10422 if (imported_name == NULL)
10423 {
10424 complaint (_("child DW_TAG_imported_declaration has unknown "
10425 "imported name - DIE at %s [in module %s]"),
10426 sect_offset_str (child_die->sect_off),
10427 objfile_name (objfile));
10428 continue;
10429 }
10430
10431 excludes.push_back (imported_name);
10432
10433 process_die (child_die, cu);
10434 }
10435
10436 add_using_directive (using_directives (cu),
10437 import_prefix,
10438 canonical_name,
10439 import_alias,
10440 imported_declaration,
10441 excludes,
10442 0,
10443 &objfile->objfile_obstack);
10444 }
10445
10446 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10447 types, but gives them a size of zero. Starting with version 14,
10448 ICC is compatible with GCC. */
10449
10450 static bool
10451 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10452 {
10453 if (!cu->checked_producer)
10454 check_producer (cu);
10455
10456 return cu->producer_is_icc_lt_14;
10457 }
10458
10459 /* ICC generates a DW_AT_type for C void functions. This was observed on
10460 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10461 which says that void functions should not have a DW_AT_type. */
10462
10463 static bool
10464 producer_is_icc (struct dwarf2_cu *cu)
10465 {
10466 if (!cu->checked_producer)
10467 check_producer (cu);
10468
10469 return cu->producer_is_icc;
10470 }
10471
10472 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10473 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10474 this, it was first present in GCC release 4.3.0. */
10475
10476 static bool
10477 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10478 {
10479 if (!cu->checked_producer)
10480 check_producer (cu);
10481
10482 return cu->producer_is_gcc_lt_4_3;
10483 }
10484
10485 static file_and_directory
10486 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10487 {
10488 file_and_directory res;
10489
10490 /* Find the filename. Do not use dwarf2_name here, since the filename
10491 is not a source language identifier. */
10492 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10493 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10494
10495 if (res.comp_dir == NULL
10496 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10497 && IS_ABSOLUTE_PATH (res.name))
10498 {
10499 std::string comp_dir_storage = ldirname (res.name);
10500 if (!comp_dir_storage.empty ())
10501 res.comp_dir
10502 = cu->per_objfile->objfile->intern (comp_dir_storage.c_str ());
10503 }
10504 if (res.comp_dir != NULL)
10505 {
10506 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10507 directory, get rid of it. */
10508 const char *cp = strchr (res.comp_dir, ':');
10509
10510 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10511 res.comp_dir = cp + 1;
10512 }
10513
10514 if (res.name == NULL)
10515 res.name = "<unknown>";
10516
10517 return res;
10518 }
10519
10520 /* Handle DW_AT_stmt_list for a compilation unit.
10521 DIE is the DW_TAG_compile_unit die for CU.
10522 COMP_DIR is the compilation directory. LOWPC is passed to
10523 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10524
10525 static void
10526 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10527 const file_and_directory &fnd, CORE_ADDR lowpc) /* ARI: editCase function */
10528 {
10529 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10530 struct attribute *attr;
10531 struct line_header line_header_local;
10532 hashval_t line_header_local_hash;
10533 void **slot;
10534 int decode_mapping;
10535
10536 gdb_assert (! cu->per_cu->is_debug_types);
10537
10538 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10539 if (attr == NULL || !attr->form_is_unsigned ())
10540 return;
10541
10542 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10543
10544 /* The line header hash table is only created if needed (it exists to
10545 prevent redundant reading of the line table for partial_units).
10546 If we're given a partial_unit, we'll need it. If we're given a
10547 compile_unit, then use the line header hash table if it's already
10548 created, but don't create one just yet. */
10549
10550 if (per_objfile->line_header_hash == NULL
10551 && die->tag == DW_TAG_partial_unit)
10552 {
10553 per_objfile->line_header_hash
10554 .reset (htab_create_alloc (127, line_header_hash_voidp,
10555 line_header_eq_voidp,
10556 htab_delete_entry<line_header>,
10557 xcalloc, xfree));
10558 }
10559
10560 line_header_local.sect_off = line_offset;
10561 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10562 line_header_local_hash = line_header_hash (&line_header_local);
10563 if (per_objfile->line_header_hash != NULL)
10564 {
10565 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10566 &line_header_local,
10567 line_header_local_hash, NO_INSERT);
10568
10569 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10570 is not present in *SLOT (since if there is something in *SLOT then
10571 it will be for a partial_unit). */
10572 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10573 {
10574 gdb_assert (*slot != NULL);
10575 cu->line_header = (struct line_header *) *slot;
10576 return;
10577 }
10578 }
10579
10580 /* dwarf_decode_line_header does not yet provide sufficient information.
10581 We always have to call also dwarf_decode_lines for it. */
10582 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10583 if (lh == NULL)
10584 return;
10585
10586 cu->line_header = lh.release ();
10587 cu->line_header_die_owner = die;
10588
10589 if (per_objfile->line_header_hash == NULL)
10590 slot = NULL;
10591 else
10592 {
10593 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10594 &line_header_local,
10595 line_header_local_hash, INSERT);
10596 gdb_assert (slot != NULL);
10597 }
10598 if (slot != NULL && *slot == NULL)
10599 {
10600 /* This newly decoded line number information unit will be owned
10601 by line_header_hash hash table. */
10602 *slot = cu->line_header;
10603 cu->line_header_die_owner = NULL;
10604 }
10605 else
10606 {
10607 /* We cannot free any current entry in (*slot) as that struct line_header
10608 may be already used by multiple CUs. Create only temporary decoded
10609 line_header for this CU - it may happen at most once for each line
10610 number information unit. And if we're not using line_header_hash
10611 then this is what we want as well. */
10612 gdb_assert (die->tag != DW_TAG_partial_unit);
10613 }
10614 decode_mapping = (die->tag != DW_TAG_partial_unit);
10615 dwarf_decode_lines (cu->line_header, fnd, cu, nullptr, lowpc,
10616 decode_mapping);
10617
10618 }
10619
10620 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10621
10622 static void
10623 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10624 {
10625 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10626 struct objfile *objfile = per_objfile->objfile;
10627 struct gdbarch *gdbarch = objfile->arch ();
10628 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10629 CORE_ADDR highpc = ((CORE_ADDR) 0);
10630 struct attribute *attr;
10631 struct die_info *child_die;
10632 CORE_ADDR baseaddr;
10633
10634 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
10635 baseaddr = objfile->text_section_offset ();
10636
10637 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10638
10639 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10640 from finish_block. */
10641 if (lowpc == ((CORE_ADDR) -1))
10642 lowpc = highpc;
10643 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10644
10645 file_and_directory fnd = find_file_and_directory (die, cu);
10646
10647 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10648
10649 gdb_assert (per_objfile->sym_cu == nullptr);
10650 scoped_restore restore_sym_cu
10651 = make_scoped_restore (&per_objfile->sym_cu, cu);
10652
10653 /* Decode line number information if present. We do this before
10654 processing child DIEs, so that the line header table is available
10655 for DW_AT_decl_file. */
10656 handle_DW_AT_stmt_list (die, cu, fnd, lowpc);
10657
10658 /* Process all dies in compilation unit. */
10659 if (die->child != NULL)
10660 {
10661 child_die = die->child;
10662 while (child_die && child_die->tag)
10663 {
10664 process_die (child_die, cu);
10665 child_die = child_die->sibling;
10666 }
10667 }
10668 per_objfile->sym_cu = nullptr;
10669
10670 /* Decode macro information, if present. Dwarf 2 macro information
10671 refers to information in the line number info statement program
10672 header, so we can only read it if we've read the header
10673 successfully. */
10674 attr = dwarf2_attr (die, DW_AT_macros, cu);
10675 if (attr == NULL)
10676 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10677 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10678 {
10679 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10680 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10681
10682 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10683 }
10684 else
10685 {
10686 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10687 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10688 {
10689 unsigned int macro_offset = attr->as_unsigned ();
10690
10691 dwarf_decode_macros (cu, macro_offset, 0);
10692 }
10693 }
10694 }
10695
10696 void
10697 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10698 {
10699 struct type_unit_group *tu_group;
10700 int first_time;
10701 struct attribute *attr;
10702 unsigned int i;
10703 struct signatured_type *sig_type;
10704
10705 gdb_assert (per_cu->is_debug_types);
10706 sig_type = (struct signatured_type *) per_cu;
10707
10708 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10709
10710 /* If we're using .gdb_index (includes -readnow) then
10711 per_cu->type_unit_group may not have been set up yet. */
10712 if (sig_type->type_unit_group == NULL)
10713 sig_type->type_unit_group = get_type_unit_group (this, attr);
10714 tu_group = sig_type->type_unit_group;
10715
10716 /* If we've already processed this stmt_list there's no real need to
10717 do it again, we could fake it and just recreate the part we need
10718 (file name,index -> symtab mapping). If data shows this optimization
10719 is useful we can do it then. */
10720 type_unit_group_unshareable *tug_unshare
10721 = per_objfile->get_type_unit_group_unshareable (tu_group);
10722 first_time = tug_unshare->compunit_symtab == NULL;
10723
10724 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10725 debug info. */
10726 line_header_up lh;
10727 if (attr != NULL && attr->form_is_unsigned ())
10728 {
10729 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10730 lh = dwarf_decode_line_header (line_offset, this);
10731 }
10732 if (lh == NULL)
10733 {
10734 if (first_time)
10735 start_symtab ("", NULL, 0);
10736 else
10737 {
10738 gdb_assert (tug_unshare->symtabs == NULL);
10739 gdb_assert (m_builder == nullptr);
10740 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10741 m_builder.reset (new struct buildsym_compunit
10742 (COMPUNIT_OBJFILE (cust), "",
10743 COMPUNIT_DIRNAME (cust),
10744 compunit_language (cust),
10745 0, cust));
10746 list_in_scope = get_builder ()->get_file_symbols ();
10747 }
10748 return;
10749 }
10750
10751 line_header = lh.release ();
10752 line_header_die_owner = die;
10753
10754 if (first_time)
10755 {
10756 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10757
10758 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10759 still initializing it, and our caller (a few levels up)
10760 process_full_type_unit still needs to know if this is the first
10761 time. */
10762
10763 tug_unshare->symtabs
10764 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10765 struct symtab *, line_header->file_names_size ());
10766
10767 auto &file_names = line_header->file_names ();
10768 for (i = 0; i < file_names.size (); ++i)
10769 {
10770 file_entry &fe = file_names[i];
10771 dwarf2_start_subfile (this, fe.name,
10772 fe.include_dir (line_header));
10773 buildsym_compunit *b = get_builder ();
10774 if (b->get_current_subfile ()->symtab == NULL)
10775 {
10776 /* NOTE: start_subfile will recognize when it's been
10777 passed a file it has already seen. So we can't
10778 assume there's a simple mapping from
10779 cu->line_header->file_names to subfiles, plus
10780 cu->line_header->file_names may contain dups. */
10781 b->get_current_subfile ()->symtab
10782 = allocate_symtab (cust, b->get_current_subfile ()->name);
10783 }
10784
10785 fe.symtab = b->get_current_subfile ()->symtab;
10786 tug_unshare->symtabs[i] = fe.symtab;
10787 }
10788 }
10789 else
10790 {
10791 gdb_assert (m_builder == nullptr);
10792 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10793 m_builder.reset (new struct buildsym_compunit
10794 (COMPUNIT_OBJFILE (cust), "",
10795 COMPUNIT_DIRNAME (cust),
10796 compunit_language (cust),
10797 0, cust));
10798 list_in_scope = get_builder ()->get_file_symbols ();
10799
10800 auto &file_names = line_header->file_names ();
10801 for (i = 0; i < file_names.size (); ++i)
10802 {
10803 file_entry &fe = file_names[i];
10804 fe.symtab = tug_unshare->symtabs[i];
10805 }
10806 }
10807
10808 /* The main symtab is allocated last. Type units don't have DW_AT_name
10809 so they don't have a "real" (so to speak) symtab anyway.
10810 There is later code that will assign the main symtab to all symbols
10811 that don't have one. We need to handle the case of a symbol with a
10812 missing symtab (DW_AT_decl_file) anyway. */
10813 }
10814
10815 /* Process DW_TAG_type_unit.
10816 For TUs we want to skip the first top level sibling if it's not the
10817 actual type being defined by this TU. In this case the first top
10818 level sibling is there to provide context only. */
10819
10820 static void
10821 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10822 {
10823 struct die_info *child_die;
10824
10825 prepare_one_comp_unit (cu, die, language_minimal);
10826
10827 /* Initialize (or reinitialize) the machinery for building symtabs.
10828 We do this before processing child DIEs, so that the line header table
10829 is available for DW_AT_decl_file. */
10830 cu->setup_type_unit_groups (die);
10831
10832 if (die->child != NULL)
10833 {
10834 child_die = die->child;
10835 while (child_die && child_die->tag)
10836 {
10837 process_die (child_die, cu);
10838 child_die = child_die->sibling;
10839 }
10840 }
10841 }
10842 \f
10843 /* DWO/DWP files.
10844
10845 http://gcc.gnu.org/wiki/DebugFission
10846 http://gcc.gnu.org/wiki/DebugFissionDWP
10847
10848 To simplify handling of both DWO files ("object" files with the DWARF info)
10849 and DWP files (a file with the DWOs packaged up into one file), we treat
10850 DWP files as having a collection of virtual DWO files. */
10851
10852 static hashval_t
10853 hash_dwo_file (const void *item)
10854 {
10855 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10856 hashval_t hash;
10857
10858 hash = htab_hash_string (dwo_file->dwo_name);
10859 if (dwo_file->comp_dir != NULL)
10860 hash += htab_hash_string (dwo_file->comp_dir);
10861 return hash;
10862 }
10863
10864 static int
10865 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10866 {
10867 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10868 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10869
10870 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10871 return 0;
10872 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10873 return lhs->comp_dir == rhs->comp_dir;
10874 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10875 }
10876
10877 /* Allocate a hash table for DWO files. */
10878
10879 static htab_up
10880 allocate_dwo_file_hash_table ()
10881 {
10882 return htab_up (htab_create_alloc (41,
10883 hash_dwo_file,
10884 eq_dwo_file,
10885 htab_delete_entry<dwo_file>,
10886 xcalloc, xfree));
10887 }
10888
10889 /* Lookup DWO file DWO_NAME. */
10890
10891 static void **
10892 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10893 const char *dwo_name,
10894 const char *comp_dir)
10895 {
10896 struct dwo_file find_entry;
10897 void **slot;
10898
10899 if (per_objfile->per_bfd->dwo_files == NULL)
10900 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10901
10902 find_entry.dwo_name = dwo_name;
10903 find_entry.comp_dir = comp_dir;
10904 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10905 INSERT);
10906
10907 return slot;
10908 }
10909
10910 static hashval_t
10911 hash_dwo_unit (const void *item)
10912 {
10913 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10914
10915 /* This drops the top 32 bits of the id, but is ok for a hash. */
10916 return dwo_unit->signature;
10917 }
10918
10919 static int
10920 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10921 {
10922 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10923 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10924
10925 /* The signature is assumed to be unique within the DWO file.
10926 So while object file CU dwo_id's always have the value zero,
10927 that's OK, assuming each object file DWO file has only one CU,
10928 and that's the rule for now. */
10929 return lhs->signature == rhs->signature;
10930 }
10931
10932 /* Allocate a hash table for DWO CUs,TUs.
10933 There is one of these tables for each of CUs,TUs for each DWO file. */
10934
10935 static htab_up
10936 allocate_dwo_unit_table ()
10937 {
10938 /* Start out with a pretty small number.
10939 Generally DWO files contain only one CU and maybe some TUs. */
10940 return htab_up (htab_create_alloc (3,
10941 hash_dwo_unit,
10942 eq_dwo_unit,
10943 NULL, xcalloc, xfree));
10944 }
10945
10946 /* die_reader_func for create_dwo_cu. */
10947
10948 static void
10949 create_dwo_cu_reader (const struct die_reader_specs *reader,
10950 const gdb_byte *info_ptr,
10951 struct die_info *comp_unit_die,
10952 struct dwo_file *dwo_file,
10953 struct dwo_unit *dwo_unit)
10954 {
10955 struct dwarf2_cu *cu = reader->cu;
10956 sect_offset sect_off = cu->per_cu->sect_off;
10957 struct dwarf2_section_info *section = cu->per_cu->section;
10958
10959 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10960 if (!signature.has_value ())
10961 {
10962 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10963 " its dwo_id [in module %s]"),
10964 sect_offset_str (sect_off), dwo_file->dwo_name);
10965 return;
10966 }
10967
10968 dwo_unit->dwo_file = dwo_file;
10969 dwo_unit->signature = *signature;
10970 dwo_unit->section = section;
10971 dwo_unit->sect_off = sect_off;
10972 dwo_unit->length = cu->per_cu->length;
10973
10974 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10975 sect_offset_str (sect_off),
10976 hex_string (dwo_unit->signature));
10977 }
10978
10979 /* Create the dwo_units for the CUs in a DWO_FILE.
10980 Note: This function processes DWO files only, not DWP files. */
10981
10982 static void
10983 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10984 dwarf2_cu *cu, struct dwo_file &dwo_file,
10985 dwarf2_section_info &section, htab_up &cus_htab)
10986 {
10987 struct objfile *objfile = per_objfile->objfile;
10988 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10989 const gdb_byte *info_ptr, *end_ptr;
10990
10991 section.read (objfile);
10992 info_ptr = section.buffer;
10993
10994 if (info_ptr == NULL)
10995 return;
10996
10997 dwarf_read_debug_printf ("Reading %s for %s:",
10998 section.get_name (),
10999 section.get_file_name ());
11000
11001 end_ptr = info_ptr + section.size;
11002 while (info_ptr < end_ptr)
11003 {
11004 struct dwarf2_per_cu_data per_cu;
11005 struct dwo_unit read_unit {};
11006 struct dwo_unit *dwo_unit;
11007 void **slot;
11008 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11009
11010 per_cu.per_bfd = per_bfd;
11011 per_cu.is_debug_types = 0;
11012 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11013 per_cu.section = &section;
11014
11015 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11016 if (!reader.dummy_p)
11017 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11018 &dwo_file, &read_unit);
11019 info_ptr += per_cu.length;
11020
11021 // If the unit could not be parsed, skip it.
11022 if (read_unit.dwo_file == NULL)
11023 continue;
11024
11025 if (cus_htab == NULL)
11026 cus_htab = allocate_dwo_unit_table ();
11027
11028 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11029 struct dwo_unit);
11030 *dwo_unit = read_unit;
11031 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11032 gdb_assert (slot != NULL);
11033 if (*slot != NULL)
11034 {
11035 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11036 sect_offset dup_sect_off = dup_cu->sect_off;
11037
11038 complaint (_("debug cu entry at offset %s is duplicate to"
11039 " the entry at offset %s, signature %s"),
11040 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11041 hex_string (dwo_unit->signature));
11042 }
11043 *slot = (void *)dwo_unit;
11044 }
11045 }
11046
11047 /* DWP file .debug_{cu,tu}_index section format:
11048 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11049 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11050
11051 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11052 officially standard DWP format was published with DWARF v5 and is called
11053 Version 5. There are no versions 3 or 4.
11054
11055 DWP Version 1:
11056
11057 Both index sections have the same format, and serve to map a 64-bit
11058 signature to a set of section numbers. Each section begins with a header,
11059 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11060 indexes, and a pool of 32-bit section numbers. The index sections will be
11061 aligned at 8-byte boundaries in the file.
11062
11063 The index section header consists of:
11064
11065 V, 32 bit version number
11066 -, 32 bits unused
11067 N, 32 bit number of compilation units or type units in the index
11068 M, 32 bit number of slots in the hash table
11069
11070 Numbers are recorded using the byte order of the application binary.
11071
11072 The hash table begins at offset 16 in the section, and consists of an array
11073 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11074 order of the application binary). Unused slots in the hash table are 0.
11075 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11076
11077 The parallel table begins immediately after the hash table
11078 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11079 array of 32-bit indexes (using the byte order of the application binary),
11080 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11081 table contains a 32-bit index into the pool of section numbers. For unused
11082 hash table slots, the corresponding entry in the parallel table will be 0.
11083
11084 The pool of section numbers begins immediately following the hash table
11085 (at offset 16 + 12 * M from the beginning of the section). The pool of
11086 section numbers consists of an array of 32-bit words (using the byte order
11087 of the application binary). Each item in the array is indexed starting
11088 from 0. The hash table entry provides the index of the first section
11089 number in the set. Additional section numbers in the set follow, and the
11090 set is terminated by a 0 entry (section number 0 is not used in ELF).
11091
11092 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11093 section must be the first entry in the set, and the .debug_abbrev.dwo must
11094 be the second entry. Other members of the set may follow in any order.
11095
11096 ---
11097
11098 DWP Versions 2 and 5:
11099
11100 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11101 and the entries in the index tables are now offsets into these sections.
11102 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11103 section.
11104
11105 Index Section Contents:
11106 Header
11107 Hash Table of Signatures dwp_hash_table.hash_table
11108 Parallel Table of Indices dwp_hash_table.unit_table
11109 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11110 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11111
11112 The index section header consists of:
11113
11114 V, 32 bit version number
11115 L, 32 bit number of columns in the table of section offsets
11116 N, 32 bit number of compilation units or type units in the index
11117 M, 32 bit number of slots in the hash table
11118
11119 Numbers are recorded using the byte order of the application binary.
11120
11121 The hash table has the same format as version 1.
11122 The parallel table of indices has the same format as version 1,
11123 except that the entries are origin-1 indices into the table of sections
11124 offsets and the table of section sizes.
11125
11126 The table of offsets begins immediately following the parallel table
11127 (at offset 16 + 12 * M from the beginning of the section). The table is
11128 a two-dimensional array of 32-bit words (using the byte order of the
11129 application binary), with L columns and N+1 rows, in row-major order.
11130 Each row in the array is indexed starting from 0. The first row provides
11131 a key to the remaining rows: each column in this row provides an identifier
11132 for a debug section, and the offsets in the same column of subsequent rows
11133 refer to that section. The section identifiers for Version 2 are:
11134
11135 DW_SECT_INFO 1 .debug_info.dwo
11136 DW_SECT_TYPES 2 .debug_types.dwo
11137 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11138 DW_SECT_LINE 4 .debug_line.dwo
11139 DW_SECT_LOC 5 .debug_loc.dwo
11140 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11141 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11142 DW_SECT_MACRO 8 .debug_macro.dwo
11143
11144 The section identifiers for Version 5 are:
11145
11146 DW_SECT_INFO_V5 1 .debug_info.dwo
11147 DW_SECT_RESERVED_V5 2 --
11148 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11149 DW_SECT_LINE_V5 4 .debug_line.dwo
11150 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11151 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11152 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11153 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11154
11155 The offsets provided by the CU and TU index sections are the base offsets
11156 for the contributions made by each CU or TU to the corresponding section
11157 in the package file. Each CU and TU header contains an abbrev_offset
11158 field, used to find the abbreviations table for that CU or TU within the
11159 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11160 be interpreted as relative to the base offset given in the index section.
11161 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11162 should be interpreted as relative to the base offset for .debug_line.dwo,
11163 and offsets into other debug sections obtained from DWARF attributes should
11164 also be interpreted as relative to the corresponding base offset.
11165
11166 The table of sizes begins immediately following the table of offsets.
11167 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11168 with L columns and N rows, in row-major order. Each row in the array is
11169 indexed starting from 1 (row 0 is shared by the two tables).
11170
11171 ---
11172
11173 Hash table lookup is handled the same in version 1 and 2:
11174
11175 We assume that N and M will not exceed 2^32 - 1.
11176 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11177
11178 Given a 64-bit compilation unit signature or a type signature S, an entry
11179 in the hash table is located as follows:
11180
11181 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11182 the low-order k bits all set to 1.
11183
11184 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11185
11186 3) If the hash table entry at index H matches the signature, use that
11187 entry. If the hash table entry at index H is unused (all zeroes),
11188 terminate the search: the signature is not present in the table.
11189
11190 4) Let H = (H + H') modulo M. Repeat at Step 3.
11191
11192 Because M > N and H' and M are relatively prime, the search is guaranteed
11193 to stop at an unused slot or find the match. */
11194
11195 /* Create a hash table to map DWO IDs to their CU/TU entry in
11196 .debug_{info,types}.dwo in DWP_FILE.
11197 Returns NULL if there isn't one.
11198 Note: This function processes DWP files only, not DWO files. */
11199
11200 static struct dwp_hash_table *
11201 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11202 struct dwp_file *dwp_file, int is_debug_types)
11203 {
11204 struct objfile *objfile = per_objfile->objfile;
11205 bfd *dbfd = dwp_file->dbfd.get ();
11206 const gdb_byte *index_ptr, *index_end;
11207 struct dwarf2_section_info *index;
11208 uint32_t version, nr_columns, nr_units, nr_slots;
11209 struct dwp_hash_table *htab;
11210
11211 if (is_debug_types)
11212 index = &dwp_file->sections.tu_index;
11213 else
11214 index = &dwp_file->sections.cu_index;
11215
11216 if (index->empty ())
11217 return NULL;
11218 index->read (objfile);
11219
11220 index_ptr = index->buffer;
11221 index_end = index_ptr + index->size;
11222
11223 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11224 For now it's safe to just read 4 bytes (particularly as it's difficult to
11225 tell if you're dealing with Version 5 before you've read the version). */
11226 version = read_4_bytes (dbfd, index_ptr);
11227 index_ptr += 4;
11228 if (version == 2 || version == 5)
11229 nr_columns = read_4_bytes (dbfd, index_ptr);
11230 else
11231 nr_columns = 0;
11232 index_ptr += 4;
11233 nr_units = read_4_bytes (dbfd, index_ptr);
11234 index_ptr += 4;
11235 nr_slots = read_4_bytes (dbfd, index_ptr);
11236 index_ptr += 4;
11237
11238 if (version != 1 && version != 2 && version != 5)
11239 {
11240 error (_("Dwarf Error: unsupported DWP file version (%s)"
11241 " [in module %s]"),
11242 pulongest (version), dwp_file->name);
11243 }
11244 if (nr_slots != (nr_slots & -nr_slots))
11245 {
11246 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11247 " is not power of 2 [in module %s]"),
11248 pulongest (nr_slots), dwp_file->name);
11249 }
11250
11251 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11252 htab->version = version;
11253 htab->nr_columns = nr_columns;
11254 htab->nr_units = nr_units;
11255 htab->nr_slots = nr_slots;
11256 htab->hash_table = index_ptr;
11257 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11258
11259 /* Exit early if the table is empty. */
11260 if (nr_slots == 0 || nr_units == 0
11261 || (version == 2 && nr_columns == 0)
11262 || (version == 5 && nr_columns == 0))
11263 {
11264 /* All must be zero. */
11265 if (nr_slots != 0 || nr_units != 0
11266 || (version == 2 && nr_columns != 0)
11267 || (version == 5 && nr_columns != 0))
11268 {
11269 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11270 " all zero [in modules %s]"),
11271 dwp_file->name);
11272 }
11273 return htab;
11274 }
11275
11276 if (version == 1)
11277 {
11278 htab->section_pool.v1.indices =
11279 htab->unit_table + sizeof (uint32_t) * nr_slots;
11280 /* It's harder to decide whether the section is too small in v1.
11281 V1 is deprecated anyway so we punt. */
11282 }
11283 else if (version == 2)
11284 {
11285 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11286 int *ids = htab->section_pool.v2.section_ids;
11287 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11288 /* Reverse map for error checking. */
11289 int ids_seen[DW_SECT_MAX + 1];
11290 int i;
11291
11292 if (nr_columns < 2)
11293 {
11294 error (_("Dwarf Error: bad DWP hash table, too few columns"
11295 " in section table [in module %s]"),
11296 dwp_file->name);
11297 }
11298 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11299 {
11300 error (_("Dwarf Error: bad DWP hash table, too many columns"
11301 " in section table [in module %s]"),
11302 dwp_file->name);
11303 }
11304 memset (ids, 255, sizeof_ids);
11305 memset (ids_seen, 255, sizeof (ids_seen));
11306 for (i = 0; i < nr_columns; ++i)
11307 {
11308 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11309
11310 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11311 {
11312 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11313 " in section table [in module %s]"),
11314 id, dwp_file->name);
11315 }
11316 if (ids_seen[id] != -1)
11317 {
11318 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11319 " id %d in section table [in module %s]"),
11320 id, dwp_file->name);
11321 }
11322 ids_seen[id] = i;
11323 ids[i] = id;
11324 }
11325 /* Must have exactly one info or types section. */
11326 if (((ids_seen[DW_SECT_INFO] != -1)
11327 + (ids_seen[DW_SECT_TYPES] != -1))
11328 != 1)
11329 {
11330 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11331 " DWO info/types section [in module %s]"),
11332 dwp_file->name);
11333 }
11334 /* Must have an abbrev section. */
11335 if (ids_seen[DW_SECT_ABBREV] == -1)
11336 {
11337 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11338 " section [in module %s]"),
11339 dwp_file->name);
11340 }
11341 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11342 htab->section_pool.v2.sizes =
11343 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11344 * nr_units * nr_columns);
11345 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11346 * nr_units * nr_columns))
11347 > index_end)
11348 {
11349 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11350 " [in module %s]"),
11351 dwp_file->name);
11352 }
11353 }
11354 else /* version == 5 */
11355 {
11356 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11357 int *ids = htab->section_pool.v5.section_ids;
11358 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11359 /* Reverse map for error checking. */
11360 int ids_seen[DW_SECT_MAX_V5 + 1];
11361
11362 if (nr_columns < 2)
11363 {
11364 error (_("Dwarf Error: bad DWP hash table, too few columns"
11365 " in section table [in module %s]"),
11366 dwp_file->name);
11367 }
11368 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11369 {
11370 error (_("Dwarf Error: bad DWP hash table, too many columns"
11371 " in section table [in module %s]"),
11372 dwp_file->name);
11373 }
11374 memset (ids, 255, sizeof_ids);
11375 memset (ids_seen, 255, sizeof (ids_seen));
11376 for (int i = 0; i < nr_columns; ++i)
11377 {
11378 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11379
11380 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11381 {
11382 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11383 " in section table [in module %s]"),
11384 id, dwp_file->name);
11385 }
11386 if (ids_seen[id] != -1)
11387 {
11388 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11389 " id %d in section table [in module %s]"),
11390 id, dwp_file->name);
11391 }
11392 ids_seen[id] = i;
11393 ids[i] = id;
11394 }
11395 /* Must have seen an info section. */
11396 if (ids_seen[DW_SECT_INFO_V5] == -1)
11397 {
11398 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11399 " DWO info/types section [in module %s]"),
11400 dwp_file->name);
11401 }
11402 /* Must have an abbrev section. */
11403 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11404 {
11405 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11406 " section [in module %s]"),
11407 dwp_file->name);
11408 }
11409 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11410 htab->section_pool.v5.sizes
11411 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11412 * nr_units * nr_columns);
11413 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11414 * nr_units * nr_columns))
11415 > index_end)
11416 {
11417 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11418 " [in module %s]"),
11419 dwp_file->name);
11420 }
11421 }
11422
11423 return htab;
11424 }
11425
11426 /* Update SECTIONS with the data from SECTP.
11427
11428 This function is like the other "locate" section routines, but in
11429 this context the sections to read comes from the DWP V1 hash table,
11430 not the full ELF section table.
11431
11432 The result is non-zero for success, or zero if an error was found. */
11433
11434 static int
11435 locate_v1_virtual_dwo_sections (asection *sectp,
11436 struct virtual_v1_dwo_sections *sections)
11437 {
11438 const struct dwop_section_names *names = &dwop_section_names;
11439
11440 if (names->abbrev_dwo.matches (sectp->name))
11441 {
11442 /* There can be only one. */
11443 if (sections->abbrev.s.section != NULL)
11444 return 0;
11445 sections->abbrev.s.section = sectp;
11446 sections->abbrev.size = bfd_section_size (sectp);
11447 }
11448 else if (names->info_dwo.matches (sectp->name)
11449 || names->types_dwo.matches (sectp->name))
11450 {
11451 /* There can be only one. */
11452 if (sections->info_or_types.s.section != NULL)
11453 return 0;
11454 sections->info_or_types.s.section = sectp;
11455 sections->info_or_types.size = bfd_section_size (sectp);
11456 }
11457 else if (names->line_dwo.matches (sectp->name))
11458 {
11459 /* There can be only one. */
11460 if (sections->line.s.section != NULL)
11461 return 0;
11462 sections->line.s.section = sectp;
11463 sections->line.size = bfd_section_size (sectp);
11464 }
11465 else if (names->loc_dwo.matches (sectp->name))
11466 {
11467 /* There can be only one. */
11468 if (sections->loc.s.section != NULL)
11469 return 0;
11470 sections->loc.s.section = sectp;
11471 sections->loc.size = bfd_section_size (sectp);
11472 }
11473 else if (names->macinfo_dwo.matches (sectp->name))
11474 {
11475 /* There can be only one. */
11476 if (sections->macinfo.s.section != NULL)
11477 return 0;
11478 sections->macinfo.s.section = sectp;
11479 sections->macinfo.size = bfd_section_size (sectp);
11480 }
11481 else if (names->macro_dwo.matches (sectp->name))
11482 {
11483 /* There can be only one. */
11484 if (sections->macro.s.section != NULL)
11485 return 0;
11486 sections->macro.s.section = sectp;
11487 sections->macro.size = bfd_section_size (sectp);
11488 }
11489 else if (names->str_offsets_dwo.matches (sectp->name))
11490 {
11491 /* There can be only one. */
11492 if (sections->str_offsets.s.section != NULL)
11493 return 0;
11494 sections->str_offsets.s.section = sectp;
11495 sections->str_offsets.size = bfd_section_size (sectp);
11496 }
11497 else
11498 {
11499 /* No other kind of section is valid. */
11500 return 0;
11501 }
11502
11503 return 1;
11504 }
11505
11506 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11507 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11508 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11509 This is for DWP version 1 files. */
11510
11511 static struct dwo_unit *
11512 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11513 struct dwp_file *dwp_file,
11514 uint32_t unit_index,
11515 const char *comp_dir,
11516 ULONGEST signature, int is_debug_types)
11517 {
11518 const struct dwp_hash_table *dwp_htab =
11519 is_debug_types ? dwp_file->tus : dwp_file->cus;
11520 bfd *dbfd = dwp_file->dbfd.get ();
11521 const char *kind = is_debug_types ? "TU" : "CU";
11522 struct dwo_file *dwo_file;
11523 struct dwo_unit *dwo_unit;
11524 struct virtual_v1_dwo_sections sections;
11525 void **dwo_file_slot;
11526 int i;
11527
11528 gdb_assert (dwp_file->version == 1);
11529
11530 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11531 kind, pulongest (unit_index), hex_string (signature),
11532 dwp_file->name);
11533
11534 /* Fetch the sections of this DWO unit.
11535 Put a limit on the number of sections we look for so that bad data
11536 doesn't cause us to loop forever. */
11537
11538 #define MAX_NR_V1_DWO_SECTIONS \
11539 (1 /* .debug_info or .debug_types */ \
11540 + 1 /* .debug_abbrev */ \
11541 + 1 /* .debug_line */ \
11542 + 1 /* .debug_loc */ \
11543 + 1 /* .debug_str_offsets */ \
11544 + 1 /* .debug_macro or .debug_macinfo */ \
11545 + 1 /* trailing zero */)
11546
11547 memset (&sections, 0, sizeof (sections));
11548
11549 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11550 {
11551 asection *sectp;
11552 uint32_t section_nr =
11553 read_4_bytes (dbfd,
11554 dwp_htab->section_pool.v1.indices
11555 + (unit_index + i) * sizeof (uint32_t));
11556
11557 if (section_nr == 0)
11558 break;
11559 if (section_nr >= dwp_file->num_sections)
11560 {
11561 error (_("Dwarf Error: bad DWP hash table, section number too large"
11562 " [in module %s]"),
11563 dwp_file->name);
11564 }
11565
11566 sectp = dwp_file->elf_sections[section_nr];
11567 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11568 {
11569 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11570 " [in module %s]"),
11571 dwp_file->name);
11572 }
11573 }
11574
11575 if (i < 2
11576 || sections.info_or_types.empty ()
11577 || sections.abbrev.empty ())
11578 {
11579 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11580 " [in module %s]"),
11581 dwp_file->name);
11582 }
11583 if (i == MAX_NR_V1_DWO_SECTIONS)
11584 {
11585 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11586 " [in module %s]"),
11587 dwp_file->name);
11588 }
11589
11590 /* It's easier for the rest of the code if we fake a struct dwo_file and
11591 have dwo_unit "live" in that. At least for now.
11592
11593 The DWP file can be made up of a random collection of CUs and TUs.
11594 However, for each CU + set of TUs that came from the same original DWO
11595 file, we can combine them back into a virtual DWO file to save space
11596 (fewer struct dwo_file objects to allocate). Remember that for really
11597 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11598
11599 std::string virtual_dwo_name =
11600 string_printf ("virtual-dwo/%d-%d-%d-%d",
11601 sections.abbrev.get_id (),
11602 sections.line.get_id (),
11603 sections.loc.get_id (),
11604 sections.str_offsets.get_id ());
11605 /* Can we use an existing virtual DWO file? */
11606 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11607 comp_dir);
11608 /* Create one if necessary. */
11609 if (*dwo_file_slot == NULL)
11610 {
11611 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11612 virtual_dwo_name.c_str ());
11613
11614 dwo_file = new struct dwo_file;
11615 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11616 dwo_file->comp_dir = comp_dir;
11617 dwo_file->sections.abbrev = sections.abbrev;
11618 dwo_file->sections.line = sections.line;
11619 dwo_file->sections.loc = sections.loc;
11620 dwo_file->sections.macinfo = sections.macinfo;
11621 dwo_file->sections.macro = sections.macro;
11622 dwo_file->sections.str_offsets = sections.str_offsets;
11623 /* The "str" section is global to the entire DWP file. */
11624 dwo_file->sections.str = dwp_file->sections.str;
11625 /* The info or types section is assigned below to dwo_unit,
11626 there's no need to record it in dwo_file.
11627 Also, we can't simply record type sections in dwo_file because
11628 we record a pointer into the vector in dwo_unit. As we collect more
11629 types we'll grow the vector and eventually have to reallocate space
11630 for it, invalidating all copies of pointers into the previous
11631 contents. */
11632 *dwo_file_slot = dwo_file;
11633 }
11634 else
11635 {
11636 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11637 virtual_dwo_name.c_str ());
11638
11639 dwo_file = (struct dwo_file *) *dwo_file_slot;
11640 }
11641
11642 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11643 dwo_unit->dwo_file = dwo_file;
11644 dwo_unit->signature = signature;
11645 dwo_unit->section =
11646 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11647 *dwo_unit->section = sections.info_or_types;
11648 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11649
11650 return dwo_unit;
11651 }
11652
11653 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11654 simplify them. Given a pointer to the containing section SECTION, and
11655 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11656 virtual section of just that piece. */
11657
11658 static struct dwarf2_section_info
11659 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11660 struct dwarf2_section_info *section,
11661 bfd_size_type offset, bfd_size_type size)
11662 {
11663 struct dwarf2_section_info result;
11664 asection *sectp;
11665
11666 gdb_assert (section != NULL);
11667 gdb_assert (!section->is_virtual);
11668
11669 memset (&result, 0, sizeof (result));
11670 result.s.containing_section = section;
11671 result.is_virtual = true;
11672
11673 if (size == 0)
11674 return result;
11675
11676 sectp = section->get_bfd_section ();
11677
11678 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11679 bounds of the real section. This is a pretty-rare event, so just
11680 flag an error (easier) instead of a warning and trying to cope. */
11681 if (sectp == NULL
11682 || offset + size > bfd_section_size (sectp))
11683 {
11684 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11685 " in section %s [in module %s]"),
11686 sectp ? bfd_section_name (sectp) : "<unknown>",
11687 objfile_name (per_objfile->objfile));
11688 }
11689
11690 result.virtual_offset = offset;
11691 result.size = size;
11692 return result;
11693 }
11694
11695 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11696 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11697 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11698 This is for DWP version 2 files. */
11699
11700 static struct dwo_unit *
11701 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11702 struct dwp_file *dwp_file,
11703 uint32_t unit_index,
11704 const char *comp_dir,
11705 ULONGEST signature, int is_debug_types)
11706 {
11707 const struct dwp_hash_table *dwp_htab =
11708 is_debug_types ? dwp_file->tus : dwp_file->cus;
11709 bfd *dbfd = dwp_file->dbfd.get ();
11710 const char *kind = is_debug_types ? "TU" : "CU";
11711 struct dwo_file *dwo_file;
11712 struct dwo_unit *dwo_unit;
11713 struct virtual_v2_or_v5_dwo_sections sections;
11714 void **dwo_file_slot;
11715 int i;
11716
11717 gdb_assert (dwp_file->version == 2);
11718
11719 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11720 kind, pulongest (unit_index), hex_string (signature),
11721 dwp_file->name);
11722
11723 /* Fetch the section offsets of this DWO unit. */
11724
11725 memset (&sections, 0, sizeof (sections));
11726
11727 for (i = 0; i < dwp_htab->nr_columns; ++i)
11728 {
11729 uint32_t offset = read_4_bytes (dbfd,
11730 dwp_htab->section_pool.v2.offsets
11731 + (((unit_index - 1) * dwp_htab->nr_columns
11732 + i)
11733 * sizeof (uint32_t)));
11734 uint32_t size = read_4_bytes (dbfd,
11735 dwp_htab->section_pool.v2.sizes
11736 + (((unit_index - 1) * dwp_htab->nr_columns
11737 + i)
11738 * sizeof (uint32_t)));
11739
11740 switch (dwp_htab->section_pool.v2.section_ids[i])
11741 {
11742 case DW_SECT_INFO:
11743 case DW_SECT_TYPES:
11744 sections.info_or_types_offset = offset;
11745 sections.info_or_types_size = size;
11746 break;
11747 case DW_SECT_ABBREV:
11748 sections.abbrev_offset = offset;
11749 sections.abbrev_size = size;
11750 break;
11751 case DW_SECT_LINE:
11752 sections.line_offset = offset;
11753 sections.line_size = size;
11754 break;
11755 case DW_SECT_LOC:
11756 sections.loc_offset = offset;
11757 sections.loc_size = size;
11758 break;
11759 case DW_SECT_STR_OFFSETS:
11760 sections.str_offsets_offset = offset;
11761 sections.str_offsets_size = size;
11762 break;
11763 case DW_SECT_MACINFO:
11764 sections.macinfo_offset = offset;
11765 sections.macinfo_size = size;
11766 break;
11767 case DW_SECT_MACRO:
11768 sections.macro_offset = offset;
11769 sections.macro_size = size;
11770 break;
11771 }
11772 }
11773
11774 /* It's easier for the rest of the code if we fake a struct dwo_file and
11775 have dwo_unit "live" in that. At least for now.
11776
11777 The DWP file can be made up of a random collection of CUs and TUs.
11778 However, for each CU + set of TUs that came from the same original DWO
11779 file, we can combine them back into a virtual DWO file to save space
11780 (fewer struct dwo_file objects to allocate). Remember that for really
11781 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11782
11783 std::string virtual_dwo_name =
11784 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11785 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11786 (long) (sections.line_size ? sections.line_offset : 0),
11787 (long) (sections.loc_size ? sections.loc_offset : 0),
11788 (long) (sections.str_offsets_size
11789 ? sections.str_offsets_offset : 0));
11790 /* Can we use an existing virtual DWO file? */
11791 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11792 comp_dir);
11793 /* Create one if necessary. */
11794 if (*dwo_file_slot == NULL)
11795 {
11796 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11797 virtual_dwo_name.c_str ());
11798
11799 dwo_file = new struct dwo_file;
11800 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11801 dwo_file->comp_dir = comp_dir;
11802 dwo_file->sections.abbrev =
11803 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11804 sections.abbrev_offset,
11805 sections.abbrev_size);
11806 dwo_file->sections.line =
11807 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11808 sections.line_offset,
11809 sections.line_size);
11810 dwo_file->sections.loc =
11811 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11812 sections.loc_offset, sections.loc_size);
11813 dwo_file->sections.macinfo =
11814 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11815 sections.macinfo_offset,
11816 sections.macinfo_size);
11817 dwo_file->sections.macro =
11818 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11819 sections.macro_offset,
11820 sections.macro_size);
11821 dwo_file->sections.str_offsets =
11822 create_dwp_v2_or_v5_section (per_objfile,
11823 &dwp_file->sections.str_offsets,
11824 sections.str_offsets_offset,
11825 sections.str_offsets_size);
11826 /* The "str" section is global to the entire DWP file. */
11827 dwo_file->sections.str = dwp_file->sections.str;
11828 /* The info or types section is assigned below to dwo_unit,
11829 there's no need to record it in dwo_file.
11830 Also, we can't simply record type sections in dwo_file because
11831 we record a pointer into the vector in dwo_unit. As we collect more
11832 types we'll grow the vector and eventually have to reallocate space
11833 for it, invalidating all copies of pointers into the previous
11834 contents. */
11835 *dwo_file_slot = dwo_file;
11836 }
11837 else
11838 {
11839 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11840 virtual_dwo_name.c_str ());
11841
11842 dwo_file = (struct dwo_file *) *dwo_file_slot;
11843 }
11844
11845 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11846 dwo_unit->dwo_file = dwo_file;
11847 dwo_unit->signature = signature;
11848 dwo_unit->section =
11849 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11850 *dwo_unit->section = create_dwp_v2_or_v5_section
11851 (per_objfile,
11852 is_debug_types
11853 ? &dwp_file->sections.types
11854 : &dwp_file->sections.info,
11855 sections.info_or_types_offset,
11856 sections.info_or_types_size);
11857 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11858
11859 return dwo_unit;
11860 }
11861
11862 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11863 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11864 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11865 This is for DWP version 5 files. */
11866
11867 static struct dwo_unit *
11868 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11869 struct dwp_file *dwp_file,
11870 uint32_t unit_index,
11871 const char *comp_dir,
11872 ULONGEST signature, int is_debug_types)
11873 {
11874 const struct dwp_hash_table *dwp_htab
11875 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11876 bfd *dbfd = dwp_file->dbfd.get ();
11877 const char *kind = is_debug_types ? "TU" : "CU";
11878 struct dwo_file *dwo_file;
11879 struct dwo_unit *dwo_unit;
11880 struct virtual_v2_or_v5_dwo_sections sections {};
11881 void **dwo_file_slot;
11882
11883 gdb_assert (dwp_file->version == 5);
11884
11885 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11886 kind, pulongest (unit_index), hex_string (signature),
11887 dwp_file->name);
11888
11889 /* Fetch the section offsets of this DWO unit. */
11890
11891 /* memset (&sections, 0, sizeof (sections)); */
11892
11893 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11894 {
11895 uint32_t offset = read_4_bytes (dbfd,
11896 dwp_htab->section_pool.v5.offsets
11897 + (((unit_index - 1)
11898 * dwp_htab->nr_columns
11899 + i)
11900 * sizeof (uint32_t)));
11901 uint32_t size = read_4_bytes (dbfd,
11902 dwp_htab->section_pool.v5.sizes
11903 + (((unit_index - 1) * dwp_htab->nr_columns
11904 + i)
11905 * sizeof (uint32_t)));
11906
11907 switch (dwp_htab->section_pool.v5.section_ids[i])
11908 {
11909 case DW_SECT_ABBREV_V5:
11910 sections.abbrev_offset = offset;
11911 sections.abbrev_size = size;
11912 break;
11913 case DW_SECT_INFO_V5:
11914 sections.info_or_types_offset = offset;
11915 sections.info_or_types_size = size;
11916 break;
11917 case DW_SECT_LINE_V5:
11918 sections.line_offset = offset;
11919 sections.line_size = size;
11920 break;
11921 case DW_SECT_LOCLISTS_V5:
11922 sections.loclists_offset = offset;
11923 sections.loclists_size = size;
11924 break;
11925 case DW_SECT_MACRO_V5:
11926 sections.macro_offset = offset;
11927 sections.macro_size = size;
11928 break;
11929 case DW_SECT_RNGLISTS_V5:
11930 sections.rnglists_offset = offset;
11931 sections.rnglists_size = size;
11932 break;
11933 case DW_SECT_STR_OFFSETS_V5:
11934 sections.str_offsets_offset = offset;
11935 sections.str_offsets_size = size;
11936 break;
11937 case DW_SECT_RESERVED_V5:
11938 default:
11939 break;
11940 }
11941 }
11942
11943 /* It's easier for the rest of the code if we fake a struct dwo_file and
11944 have dwo_unit "live" in that. At least for now.
11945
11946 The DWP file can be made up of a random collection of CUs and TUs.
11947 However, for each CU + set of TUs that came from the same original DWO
11948 file, we can combine them back into a virtual DWO file to save space
11949 (fewer struct dwo_file objects to allocate). Remember that for really
11950 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11951
11952 std::string virtual_dwo_name =
11953 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11954 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11955 (long) (sections.line_size ? sections.line_offset : 0),
11956 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11957 (long) (sections.str_offsets_size
11958 ? sections.str_offsets_offset : 0),
11959 (long) (sections.macro_size ? sections.macro_offset : 0),
11960 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11961 /* Can we use an existing virtual DWO file? */
11962 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11963 virtual_dwo_name.c_str (),
11964 comp_dir);
11965 /* Create one if necessary. */
11966 if (*dwo_file_slot == NULL)
11967 {
11968 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11969 virtual_dwo_name.c_str ());
11970
11971 dwo_file = new struct dwo_file;
11972 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11973 dwo_file->comp_dir = comp_dir;
11974 dwo_file->sections.abbrev =
11975 create_dwp_v2_or_v5_section (per_objfile,
11976 &dwp_file->sections.abbrev,
11977 sections.abbrev_offset,
11978 sections.abbrev_size);
11979 dwo_file->sections.line =
11980 create_dwp_v2_or_v5_section (per_objfile,
11981 &dwp_file->sections.line,
11982 sections.line_offset, sections.line_size);
11983 dwo_file->sections.macro =
11984 create_dwp_v2_or_v5_section (per_objfile,
11985 &dwp_file->sections.macro,
11986 sections.macro_offset,
11987 sections.macro_size);
11988 dwo_file->sections.loclists =
11989 create_dwp_v2_or_v5_section (per_objfile,
11990 &dwp_file->sections.loclists,
11991 sections.loclists_offset,
11992 sections.loclists_size);
11993 dwo_file->sections.rnglists =
11994 create_dwp_v2_or_v5_section (per_objfile,
11995 &dwp_file->sections.rnglists,
11996 sections.rnglists_offset,
11997 sections.rnglists_size);
11998 dwo_file->sections.str_offsets =
11999 create_dwp_v2_or_v5_section (per_objfile,
12000 &dwp_file->sections.str_offsets,
12001 sections.str_offsets_offset,
12002 sections.str_offsets_size);
12003 /* The "str" section is global to the entire DWP file. */
12004 dwo_file->sections.str = dwp_file->sections.str;
12005 /* The info or types section is assigned below to dwo_unit,
12006 there's no need to record it in dwo_file.
12007 Also, we can't simply record type sections in dwo_file because
12008 we record a pointer into the vector in dwo_unit. As we collect more
12009 types we'll grow the vector and eventually have to reallocate space
12010 for it, invalidating all copies of pointers into the previous
12011 contents. */
12012 *dwo_file_slot = dwo_file;
12013 }
12014 else
12015 {
12016 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12017 virtual_dwo_name.c_str ());
12018
12019 dwo_file = (struct dwo_file *) *dwo_file_slot;
12020 }
12021
12022 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12023 dwo_unit->dwo_file = dwo_file;
12024 dwo_unit->signature = signature;
12025 dwo_unit->section
12026 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12027 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12028 &dwp_file->sections.info,
12029 sections.info_or_types_offset,
12030 sections.info_or_types_size);
12031 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12032
12033 return dwo_unit;
12034 }
12035
12036 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12037 Returns NULL if the signature isn't found. */
12038
12039 static struct dwo_unit *
12040 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12041 struct dwp_file *dwp_file, const char *comp_dir,
12042 ULONGEST signature, int is_debug_types)
12043 {
12044 const struct dwp_hash_table *dwp_htab =
12045 is_debug_types ? dwp_file->tus : dwp_file->cus;
12046 bfd *dbfd = dwp_file->dbfd.get ();
12047 uint32_t mask = dwp_htab->nr_slots - 1;
12048 uint32_t hash = signature & mask;
12049 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12050 unsigned int i;
12051 void **slot;
12052 struct dwo_unit find_dwo_cu;
12053
12054 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12055 find_dwo_cu.signature = signature;
12056 slot = htab_find_slot (is_debug_types
12057 ? dwp_file->loaded_tus.get ()
12058 : dwp_file->loaded_cus.get (),
12059 &find_dwo_cu, INSERT);
12060
12061 if (*slot != NULL)
12062 return (struct dwo_unit *) *slot;
12063
12064 /* Use a for loop so that we don't loop forever on bad debug info. */
12065 for (i = 0; i < dwp_htab->nr_slots; ++i)
12066 {
12067 ULONGEST signature_in_table;
12068
12069 signature_in_table =
12070 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12071 if (signature_in_table == signature)
12072 {
12073 uint32_t unit_index =
12074 read_4_bytes (dbfd,
12075 dwp_htab->unit_table + hash * sizeof (uint32_t));
12076
12077 if (dwp_file->version == 1)
12078 {
12079 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12080 unit_index, comp_dir,
12081 signature, is_debug_types);
12082 }
12083 else if (dwp_file->version == 2)
12084 {
12085 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12086 unit_index, comp_dir,
12087 signature, is_debug_types);
12088 }
12089 else /* version == 5 */
12090 {
12091 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12092 unit_index, comp_dir,
12093 signature, is_debug_types);
12094 }
12095 return (struct dwo_unit *) *slot;
12096 }
12097 if (signature_in_table == 0)
12098 return NULL;
12099 hash = (hash + hash2) & mask;
12100 }
12101
12102 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12103 " [in module %s]"),
12104 dwp_file->name);
12105 }
12106
12107 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12108 Open the file specified by FILE_NAME and hand it off to BFD for
12109 preliminary analysis. Return a newly initialized bfd *, which
12110 includes a canonicalized copy of FILE_NAME.
12111 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12112 SEARCH_CWD is true if the current directory is to be searched.
12113 It will be searched before debug-file-directory.
12114 If successful, the file is added to the bfd include table of the
12115 objfile's bfd (see gdb_bfd_record_inclusion).
12116 If unable to find/open the file, return NULL.
12117 NOTE: This function is derived from symfile_bfd_open. */
12118
12119 static gdb_bfd_ref_ptr
12120 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12121 const char *file_name, int is_dwp, int search_cwd)
12122 {
12123 int desc;
12124 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12125 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12126 to debug_file_directory. */
12127 const char *search_path;
12128 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12129
12130 gdb::unique_xmalloc_ptr<char> search_path_holder;
12131 if (search_cwd)
12132 {
12133 if (*debug_file_directory != '\0')
12134 {
12135 search_path_holder.reset (concat (".", dirname_separator_string,
12136 debug_file_directory,
12137 (char *) NULL));
12138 search_path = search_path_holder.get ();
12139 }
12140 else
12141 search_path = ".";
12142 }
12143 else
12144 search_path = debug_file_directory;
12145
12146 /* Add the path for the executable binary to the list of search paths. */
12147 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12148 search_path_holder.reset (concat (objfile_dir.c_str (),
12149 dirname_separator_string,
12150 search_path, nullptr));
12151 search_path = search_path_holder.get ();
12152
12153 openp_flags flags = OPF_RETURN_REALPATH;
12154 if (is_dwp)
12155 flags |= OPF_SEARCH_IN_PATH;
12156
12157 gdb::unique_xmalloc_ptr<char> absolute_name;
12158 desc = openp (search_path, flags, file_name,
12159 O_RDONLY | O_BINARY, &absolute_name);
12160 if (desc < 0)
12161 return NULL;
12162
12163 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12164 gnutarget, desc));
12165 if (sym_bfd == NULL)
12166 return NULL;
12167 bfd_set_cacheable (sym_bfd.get (), 1);
12168
12169 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12170 return NULL;
12171
12172 /* Success. Record the bfd as having been included by the objfile's bfd.
12173 This is important because things like demangled_names_hash lives in the
12174 objfile's per_bfd space and may have references to things like symbol
12175 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12176 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12177
12178 return sym_bfd;
12179 }
12180
12181 /* Try to open DWO file FILE_NAME.
12182 COMP_DIR is the DW_AT_comp_dir attribute.
12183 The result is the bfd handle of the file.
12184 If there is a problem finding or opening the file, return NULL.
12185 Upon success, the canonicalized path of the file is stored in the bfd,
12186 same as symfile_bfd_open. */
12187
12188 static gdb_bfd_ref_ptr
12189 open_dwo_file (dwarf2_per_objfile *per_objfile,
12190 const char *file_name, const char *comp_dir)
12191 {
12192 if (IS_ABSOLUTE_PATH (file_name))
12193 return try_open_dwop_file (per_objfile, file_name,
12194 0 /*is_dwp*/, 0 /*search_cwd*/);
12195
12196 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12197
12198 if (comp_dir != NULL)
12199 {
12200 gdb::unique_xmalloc_ptr<char> path_to_try
12201 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12202
12203 /* NOTE: If comp_dir is a relative path, this will also try the
12204 search path, which seems useful. */
12205 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12206 0 /*is_dwp*/,
12207 1 /*search_cwd*/));
12208 if (abfd != NULL)
12209 return abfd;
12210 }
12211
12212 /* That didn't work, try debug-file-directory, which, despite its name,
12213 is a list of paths. */
12214
12215 if (*debug_file_directory == '\0')
12216 return NULL;
12217
12218 return try_open_dwop_file (per_objfile, file_name,
12219 0 /*is_dwp*/, 1 /*search_cwd*/);
12220 }
12221
12222 /* This function is mapped across the sections and remembers the offset and
12223 size of each of the DWO debugging sections we are interested in. */
12224
12225 static void
12226 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12227 dwo_sections *dwo_sections)
12228 {
12229 const struct dwop_section_names *names = &dwop_section_names;
12230
12231 if (names->abbrev_dwo.matches (sectp->name))
12232 {
12233 dwo_sections->abbrev.s.section = sectp;
12234 dwo_sections->abbrev.size = bfd_section_size (sectp);
12235 }
12236 else if (names->info_dwo.matches (sectp->name))
12237 {
12238 dwo_sections->info.s.section = sectp;
12239 dwo_sections->info.size = bfd_section_size (sectp);
12240 }
12241 else if (names->line_dwo.matches (sectp->name))
12242 {
12243 dwo_sections->line.s.section = sectp;
12244 dwo_sections->line.size = bfd_section_size (sectp);
12245 }
12246 else if (names->loc_dwo.matches (sectp->name))
12247 {
12248 dwo_sections->loc.s.section = sectp;
12249 dwo_sections->loc.size = bfd_section_size (sectp);
12250 }
12251 else if (names->loclists_dwo.matches (sectp->name))
12252 {
12253 dwo_sections->loclists.s.section = sectp;
12254 dwo_sections->loclists.size = bfd_section_size (sectp);
12255 }
12256 else if (names->macinfo_dwo.matches (sectp->name))
12257 {
12258 dwo_sections->macinfo.s.section = sectp;
12259 dwo_sections->macinfo.size = bfd_section_size (sectp);
12260 }
12261 else if (names->macro_dwo.matches (sectp->name))
12262 {
12263 dwo_sections->macro.s.section = sectp;
12264 dwo_sections->macro.size = bfd_section_size (sectp);
12265 }
12266 else if (names->rnglists_dwo.matches (sectp->name))
12267 {
12268 dwo_sections->rnglists.s.section = sectp;
12269 dwo_sections->rnglists.size = bfd_section_size (sectp);
12270 }
12271 else if (names->str_dwo.matches (sectp->name))
12272 {
12273 dwo_sections->str.s.section = sectp;
12274 dwo_sections->str.size = bfd_section_size (sectp);
12275 }
12276 else if (names->str_offsets_dwo.matches (sectp->name))
12277 {
12278 dwo_sections->str_offsets.s.section = sectp;
12279 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12280 }
12281 else if (names->types_dwo.matches (sectp->name))
12282 {
12283 struct dwarf2_section_info type_section;
12284
12285 memset (&type_section, 0, sizeof (type_section));
12286 type_section.s.section = sectp;
12287 type_section.size = bfd_section_size (sectp);
12288 dwo_sections->types.push_back (type_section);
12289 }
12290 }
12291
12292 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12293 by PER_CU. This is for the non-DWP case.
12294 The result is NULL if DWO_NAME can't be found. */
12295
12296 static struct dwo_file *
12297 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12298 const char *comp_dir)
12299 {
12300 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12301
12302 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12303 if (dbfd == NULL)
12304 {
12305 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12306
12307 return NULL;
12308 }
12309
12310 dwo_file_up dwo_file (new struct dwo_file);
12311 dwo_file->dwo_name = dwo_name;
12312 dwo_file->comp_dir = comp_dir;
12313 dwo_file->dbfd = std::move (dbfd);
12314
12315 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12316 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12317 &dwo_file->sections);
12318
12319 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12320 dwo_file->cus);
12321
12322 if (cu->per_cu->dwarf_version < 5)
12323 {
12324 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12325 dwo_file->sections.types, dwo_file->tus);
12326 }
12327 else
12328 {
12329 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12330 &dwo_file->sections.info, dwo_file->tus,
12331 rcuh_kind::COMPILE);
12332 }
12333
12334 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12335
12336 return dwo_file.release ();
12337 }
12338
12339 /* This function is mapped across the sections and remembers the offset and
12340 size of each of the DWP debugging sections common to version 1 and 2 that
12341 we are interested in. */
12342
12343 static void
12344 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12345 dwp_file *dwp_file)
12346 {
12347 const struct dwop_section_names *names = &dwop_section_names;
12348 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12349
12350 /* Record the ELF section number for later lookup: this is what the
12351 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12352 gdb_assert (elf_section_nr < dwp_file->num_sections);
12353 dwp_file->elf_sections[elf_section_nr] = sectp;
12354
12355 /* Look for specific sections that we need. */
12356 if (names->str_dwo.matches (sectp->name))
12357 {
12358 dwp_file->sections.str.s.section = sectp;
12359 dwp_file->sections.str.size = bfd_section_size (sectp);
12360 }
12361 else if (names->cu_index.matches (sectp->name))
12362 {
12363 dwp_file->sections.cu_index.s.section = sectp;
12364 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12365 }
12366 else if (names->tu_index.matches (sectp->name))
12367 {
12368 dwp_file->sections.tu_index.s.section = sectp;
12369 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12370 }
12371 }
12372
12373 /* This function is mapped across the sections and remembers the offset and
12374 size of each of the DWP version 2 debugging sections that we are interested
12375 in. This is split into a separate function because we don't know if we
12376 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12377
12378 static void
12379 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12380 {
12381 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12382 const struct dwop_section_names *names = &dwop_section_names;
12383 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12384
12385 /* Record the ELF section number for later lookup: this is what the
12386 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12387 gdb_assert (elf_section_nr < dwp_file->num_sections);
12388 dwp_file->elf_sections[elf_section_nr] = sectp;
12389
12390 /* Look for specific sections that we need. */
12391 if (names->abbrev_dwo.matches (sectp->name))
12392 {
12393 dwp_file->sections.abbrev.s.section = sectp;
12394 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12395 }
12396 else if (names->info_dwo.matches (sectp->name))
12397 {
12398 dwp_file->sections.info.s.section = sectp;
12399 dwp_file->sections.info.size = bfd_section_size (sectp);
12400 }
12401 else if (names->line_dwo.matches (sectp->name))
12402 {
12403 dwp_file->sections.line.s.section = sectp;
12404 dwp_file->sections.line.size = bfd_section_size (sectp);
12405 }
12406 else if (names->loc_dwo.matches (sectp->name))
12407 {
12408 dwp_file->sections.loc.s.section = sectp;
12409 dwp_file->sections.loc.size = bfd_section_size (sectp);
12410 }
12411 else if (names->macinfo_dwo.matches (sectp->name))
12412 {
12413 dwp_file->sections.macinfo.s.section = sectp;
12414 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12415 }
12416 else if (names->macro_dwo.matches (sectp->name))
12417 {
12418 dwp_file->sections.macro.s.section = sectp;
12419 dwp_file->sections.macro.size = bfd_section_size (sectp);
12420 }
12421 else if (names->str_offsets_dwo.matches (sectp->name))
12422 {
12423 dwp_file->sections.str_offsets.s.section = sectp;
12424 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12425 }
12426 else if (names->types_dwo.matches (sectp->name))
12427 {
12428 dwp_file->sections.types.s.section = sectp;
12429 dwp_file->sections.types.size = bfd_section_size (sectp);
12430 }
12431 }
12432
12433 /* This function is mapped across the sections and remembers the offset and
12434 size of each of the DWP version 5 debugging sections that we are interested
12435 in. This is split into a separate function because we don't know if we
12436 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12437
12438 static void
12439 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12440 {
12441 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12442 const struct dwop_section_names *names = &dwop_section_names;
12443 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12444
12445 /* Record the ELF section number for later lookup: this is what the
12446 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12447 gdb_assert (elf_section_nr < dwp_file->num_sections);
12448 dwp_file->elf_sections[elf_section_nr] = sectp;
12449
12450 /* Look for specific sections that we need. */
12451 if (names->abbrev_dwo.matches (sectp->name))
12452 {
12453 dwp_file->sections.abbrev.s.section = sectp;
12454 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12455 }
12456 else if (names->info_dwo.matches (sectp->name))
12457 {
12458 dwp_file->sections.info.s.section = sectp;
12459 dwp_file->sections.info.size = bfd_section_size (sectp);
12460 }
12461 else if (names->line_dwo.matches (sectp->name))
12462 {
12463 dwp_file->sections.line.s.section = sectp;
12464 dwp_file->sections.line.size = bfd_section_size (sectp);
12465 }
12466 else if (names->loclists_dwo.matches (sectp->name))
12467 {
12468 dwp_file->sections.loclists.s.section = sectp;
12469 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12470 }
12471 else if (names->macro_dwo.matches (sectp->name))
12472 {
12473 dwp_file->sections.macro.s.section = sectp;
12474 dwp_file->sections.macro.size = bfd_section_size (sectp);
12475 }
12476 else if (names->rnglists_dwo.matches (sectp->name))
12477 {
12478 dwp_file->sections.rnglists.s.section = sectp;
12479 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12480 }
12481 else if (names->str_offsets_dwo.matches (sectp->name))
12482 {
12483 dwp_file->sections.str_offsets.s.section = sectp;
12484 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12485 }
12486 }
12487
12488 /* Hash function for dwp_file loaded CUs/TUs. */
12489
12490 static hashval_t
12491 hash_dwp_loaded_cutus (const void *item)
12492 {
12493 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12494
12495 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12496 return dwo_unit->signature;
12497 }
12498
12499 /* Equality function for dwp_file loaded CUs/TUs. */
12500
12501 static int
12502 eq_dwp_loaded_cutus (const void *a, const void *b)
12503 {
12504 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12505 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12506
12507 return dua->signature == dub->signature;
12508 }
12509
12510 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12511
12512 static htab_up
12513 allocate_dwp_loaded_cutus_table ()
12514 {
12515 return htab_up (htab_create_alloc (3,
12516 hash_dwp_loaded_cutus,
12517 eq_dwp_loaded_cutus,
12518 NULL, xcalloc, xfree));
12519 }
12520
12521 /* Try to open DWP file FILE_NAME.
12522 The result is the bfd handle of the file.
12523 If there is a problem finding or opening the file, return NULL.
12524 Upon success, the canonicalized path of the file is stored in the bfd,
12525 same as symfile_bfd_open. */
12526
12527 static gdb_bfd_ref_ptr
12528 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12529 {
12530 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12531 1 /*is_dwp*/,
12532 1 /*search_cwd*/));
12533 if (abfd != NULL)
12534 return abfd;
12535
12536 /* Work around upstream bug 15652.
12537 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12538 [Whether that's a "bug" is debatable, but it is getting in our way.]
12539 We have no real idea where the dwp file is, because gdb's realpath-ing
12540 of the executable's path may have discarded the needed info.
12541 [IWBN if the dwp file name was recorded in the executable, akin to
12542 .gnu_debuglink, but that doesn't exist yet.]
12543 Strip the directory from FILE_NAME and search again. */
12544 if (*debug_file_directory != '\0')
12545 {
12546 /* Don't implicitly search the current directory here.
12547 If the user wants to search "." to handle this case,
12548 it must be added to debug-file-directory. */
12549 return try_open_dwop_file (per_objfile, lbasename (file_name),
12550 1 /*is_dwp*/,
12551 0 /*search_cwd*/);
12552 }
12553
12554 return NULL;
12555 }
12556
12557 /* Initialize the use of the DWP file for the current objfile.
12558 By convention the name of the DWP file is ${objfile}.dwp.
12559 The result is NULL if it can't be found. */
12560
12561 static std::unique_ptr<struct dwp_file>
12562 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12563 {
12564 struct objfile *objfile = per_objfile->objfile;
12565
12566 /* Try to find first .dwp for the binary file before any symbolic links
12567 resolving. */
12568
12569 /* If the objfile is a debug file, find the name of the real binary
12570 file and get the name of dwp file from there. */
12571 std::string dwp_name;
12572 if (objfile->separate_debug_objfile_backlink != NULL)
12573 {
12574 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12575 const char *backlink_basename = lbasename (backlink->original_name);
12576
12577 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12578 }
12579 else
12580 dwp_name = objfile->original_name;
12581
12582 dwp_name += ".dwp";
12583
12584 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12585 if (dbfd == NULL
12586 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12587 {
12588 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12589 dwp_name = objfile_name (objfile);
12590 dwp_name += ".dwp";
12591 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12592 }
12593
12594 if (dbfd == NULL)
12595 {
12596 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12597
12598 return std::unique_ptr<dwp_file> ();
12599 }
12600
12601 const char *name = bfd_get_filename (dbfd.get ());
12602 std::unique_ptr<struct dwp_file> dwp_file
12603 (new struct dwp_file (name, std::move (dbfd)));
12604
12605 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12606 dwp_file->elf_sections =
12607 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12608 dwp_file->num_sections, asection *);
12609
12610 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12611 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12612 dwp_file.get ());
12613
12614 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12615
12616 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12617
12618 /* The DWP file version is stored in the hash table. Oh well. */
12619 if (dwp_file->cus && dwp_file->tus
12620 && dwp_file->cus->version != dwp_file->tus->version)
12621 {
12622 /* Technically speaking, we should try to limp along, but this is
12623 pretty bizarre. We use pulongest here because that's the established
12624 portability solution (e.g, we cannot use %u for uint32_t). */
12625 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12626 " TU version %s [in DWP file %s]"),
12627 pulongest (dwp_file->cus->version),
12628 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12629 }
12630
12631 if (dwp_file->cus)
12632 dwp_file->version = dwp_file->cus->version;
12633 else if (dwp_file->tus)
12634 dwp_file->version = dwp_file->tus->version;
12635 else
12636 dwp_file->version = 2;
12637
12638 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12639 {
12640 if (dwp_file->version == 2)
12641 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12642 dwp_file.get ());
12643 else
12644 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12645 dwp_file.get ());
12646 }
12647
12648 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12649 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12650
12651 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12652 dwarf_read_debug_printf (" %s CUs, %s TUs",
12653 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12654 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12655
12656 return dwp_file;
12657 }
12658
12659 /* Wrapper around open_and_init_dwp_file, only open it once. */
12660
12661 static struct dwp_file *
12662 get_dwp_file (dwarf2_per_objfile *per_objfile)
12663 {
12664 if (!per_objfile->per_bfd->dwp_checked)
12665 {
12666 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12667 per_objfile->per_bfd->dwp_checked = 1;
12668 }
12669 return per_objfile->per_bfd->dwp_file.get ();
12670 }
12671
12672 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12673 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12674 or in the DWP file for the objfile, referenced by THIS_UNIT.
12675 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12676 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12677
12678 This is called, for example, when wanting to read a variable with a
12679 complex location. Therefore we don't want to do file i/o for every call.
12680 Therefore we don't want to look for a DWO file on every call.
12681 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12682 then we check if we've already seen DWO_NAME, and only THEN do we check
12683 for a DWO file.
12684
12685 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12686 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12687
12688 static struct dwo_unit *
12689 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12690 ULONGEST signature, int is_debug_types)
12691 {
12692 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12693 struct objfile *objfile = per_objfile->objfile;
12694 const char *kind = is_debug_types ? "TU" : "CU";
12695 void **dwo_file_slot;
12696 struct dwo_file *dwo_file;
12697 struct dwp_file *dwp_file;
12698
12699 /* First see if there's a DWP file.
12700 If we have a DWP file but didn't find the DWO inside it, don't
12701 look for the original DWO file. It makes gdb behave differently
12702 depending on whether one is debugging in the build tree. */
12703
12704 dwp_file = get_dwp_file (per_objfile);
12705 if (dwp_file != NULL)
12706 {
12707 const struct dwp_hash_table *dwp_htab =
12708 is_debug_types ? dwp_file->tus : dwp_file->cus;
12709
12710 if (dwp_htab != NULL)
12711 {
12712 struct dwo_unit *dwo_cutu =
12713 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12714 is_debug_types);
12715
12716 if (dwo_cutu != NULL)
12717 {
12718 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12719 kind, hex_string (signature),
12720 host_address_to_string (dwo_cutu));
12721
12722 return dwo_cutu;
12723 }
12724 }
12725 }
12726 else
12727 {
12728 /* No DWP file, look for the DWO file. */
12729
12730 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12731 if (*dwo_file_slot == NULL)
12732 {
12733 /* Read in the file and build a table of the CUs/TUs it contains. */
12734 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12735 }
12736 /* NOTE: This will be NULL if unable to open the file. */
12737 dwo_file = (struct dwo_file *) *dwo_file_slot;
12738
12739 if (dwo_file != NULL)
12740 {
12741 struct dwo_unit *dwo_cutu = NULL;
12742
12743 if (is_debug_types && dwo_file->tus)
12744 {
12745 struct dwo_unit find_dwo_cutu;
12746
12747 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12748 find_dwo_cutu.signature = signature;
12749 dwo_cutu
12750 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12751 &find_dwo_cutu);
12752 }
12753 else if (!is_debug_types && dwo_file->cus)
12754 {
12755 struct dwo_unit find_dwo_cutu;
12756
12757 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12758 find_dwo_cutu.signature = signature;
12759 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12760 &find_dwo_cutu);
12761 }
12762
12763 if (dwo_cutu != NULL)
12764 {
12765 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12766 kind, dwo_name, hex_string (signature),
12767 host_address_to_string (dwo_cutu));
12768
12769 return dwo_cutu;
12770 }
12771 }
12772 }
12773
12774 /* We didn't find it. This could mean a dwo_id mismatch, or
12775 someone deleted the DWO/DWP file, or the search path isn't set up
12776 correctly to find the file. */
12777
12778 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12779 kind, dwo_name, hex_string (signature));
12780
12781 /* This is a warning and not a complaint because it can be caused by
12782 pilot error (e.g., user accidentally deleting the DWO). */
12783 {
12784 /* Print the name of the DWP file if we looked there, helps the user
12785 better diagnose the problem. */
12786 std::string dwp_text;
12787
12788 if (dwp_file != NULL)
12789 dwp_text = string_printf (" [in DWP file %s]",
12790 lbasename (dwp_file->name));
12791
12792 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12793 " [in module %s]"),
12794 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12795 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12796 }
12797 return NULL;
12798 }
12799
12800 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12801 See lookup_dwo_cutu_unit for details. */
12802
12803 static struct dwo_unit *
12804 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12805 ULONGEST signature)
12806 {
12807 gdb_assert (!cu->per_cu->is_debug_types);
12808
12809 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12810 }
12811
12812 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12813 See lookup_dwo_cutu_unit for details. */
12814
12815 static struct dwo_unit *
12816 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12817 {
12818 gdb_assert (cu->per_cu->is_debug_types);
12819
12820 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12821
12822 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12823 }
12824
12825 /* Traversal function for queue_and_load_all_dwo_tus. */
12826
12827 static int
12828 queue_and_load_dwo_tu (void **slot, void *info)
12829 {
12830 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12831 dwarf2_cu *cu = (dwarf2_cu *) info;
12832 ULONGEST signature = dwo_unit->signature;
12833 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12834
12835 if (sig_type != NULL)
12836 {
12837 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12838 a real dependency of PER_CU on SIG_TYPE. That is detected later
12839 while processing PER_CU. */
12840 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12841 cu->per_cu->lang))
12842 load_full_type_unit (sig_type, cu->per_objfile);
12843 cu->per_cu->imported_symtabs_push (sig_type);
12844 }
12845
12846 return 1;
12847 }
12848
12849 /* Queue all TUs contained in the DWO of CU to be read in.
12850 The DWO may have the only definition of the type, though it may not be
12851 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12852 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12853
12854 static void
12855 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12856 {
12857 struct dwo_unit *dwo_unit;
12858 struct dwo_file *dwo_file;
12859
12860 gdb_assert (cu != nullptr);
12861 gdb_assert (!cu->per_cu->is_debug_types);
12862 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12863
12864 dwo_unit = cu->dwo_unit;
12865 gdb_assert (dwo_unit != NULL);
12866
12867 dwo_file = dwo_unit->dwo_file;
12868 if (dwo_file->tus != NULL)
12869 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12870 }
12871
12872 /* Read in various DIEs. */
12873
12874 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12875 Inherit only the children of the DW_AT_abstract_origin DIE not being
12876 already referenced by DW_AT_abstract_origin from the children of the
12877 current DIE. */
12878
12879 static void
12880 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12881 {
12882 struct die_info *child_die;
12883 sect_offset *offsetp;
12884 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12885 struct die_info *origin_die;
12886 /* Iterator of the ORIGIN_DIE children. */
12887 struct die_info *origin_child_die;
12888 struct attribute *attr;
12889 struct dwarf2_cu *origin_cu;
12890 struct pending **origin_previous_list_in_scope;
12891
12892 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12893 if (!attr)
12894 return;
12895
12896 /* Note that following die references may follow to a die in a
12897 different cu. */
12898
12899 origin_cu = cu;
12900 origin_die = follow_die_ref (die, attr, &origin_cu);
12901
12902 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12903 symbols in. */
12904 origin_previous_list_in_scope = origin_cu->list_in_scope;
12905 origin_cu->list_in_scope = cu->list_in_scope;
12906
12907 if (die->tag != origin_die->tag
12908 && !(die->tag == DW_TAG_inlined_subroutine
12909 && origin_die->tag == DW_TAG_subprogram))
12910 complaint (_("DIE %s and its abstract origin %s have different tags"),
12911 sect_offset_str (die->sect_off),
12912 sect_offset_str (origin_die->sect_off));
12913
12914 /* Find if the concrete and abstract trees are structurally the
12915 same. This is a shallow traversal and it is not bullet-proof;
12916 the compiler can trick the debugger into believing that the trees
12917 are isomorphic, whereas they actually are not. However, the
12918 likelyhood of this happening is pretty low, and a full-fledged
12919 check would be an overkill. */
12920 bool are_isomorphic = true;
12921 die_info *concrete_child = die->child;
12922 die_info *abstract_child = origin_die->child;
12923 while (concrete_child != nullptr || abstract_child != nullptr)
12924 {
12925 if (concrete_child == nullptr
12926 || abstract_child == nullptr
12927 || concrete_child->tag != abstract_child->tag)
12928 {
12929 are_isomorphic = false;
12930 break;
12931 }
12932
12933 concrete_child = concrete_child->sibling;
12934 abstract_child = abstract_child->sibling;
12935 }
12936
12937 /* Walk the origin's children in parallel to the concrete children.
12938 This helps match an origin child in case the debug info misses
12939 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12940 origin tree may not have the same tree structure as the concrete
12941 DIE, though. */
12942 die_info *corresponding_abstract_child
12943 = are_isomorphic ? origin_die->child : nullptr;
12944
12945 std::vector<sect_offset> offsets;
12946
12947 for (child_die = die->child;
12948 child_die && child_die->tag;
12949 child_die = child_die->sibling)
12950 {
12951 struct die_info *child_origin_die;
12952 struct dwarf2_cu *child_origin_cu;
12953
12954 /* We are trying to process concrete instance entries:
12955 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12956 it's not relevant to our analysis here. i.e. detecting DIEs that are
12957 present in the abstract instance but not referenced in the concrete
12958 one. */
12959 if (child_die->tag == DW_TAG_call_site
12960 || child_die->tag == DW_TAG_GNU_call_site)
12961 {
12962 if (are_isomorphic)
12963 corresponding_abstract_child
12964 = corresponding_abstract_child->sibling;
12965 continue;
12966 }
12967
12968 /* For each CHILD_DIE, find the corresponding child of
12969 ORIGIN_DIE. If there is more than one layer of
12970 DW_AT_abstract_origin, follow them all; there shouldn't be,
12971 but GCC versions at least through 4.4 generate this (GCC PR
12972 40573). */
12973 child_origin_die = child_die;
12974 child_origin_cu = cu;
12975 while (1)
12976 {
12977 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12978 child_origin_cu);
12979 if (attr == NULL)
12980 break;
12981 child_origin_die = follow_die_ref (child_origin_die, attr,
12982 &child_origin_cu);
12983 }
12984
12985 /* If missing DW_AT_abstract_origin, try the corresponding child
12986 of the origin. Clang emits such lexical scopes. */
12987 if (child_origin_die == child_die
12988 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12989 && are_isomorphic
12990 && child_die->tag == DW_TAG_lexical_block)
12991 child_origin_die = corresponding_abstract_child;
12992
12993 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12994 counterpart may exist. */
12995 if (child_origin_die != child_die)
12996 {
12997 if (child_die->tag != child_origin_die->tag
12998 && !(child_die->tag == DW_TAG_inlined_subroutine
12999 && child_origin_die->tag == DW_TAG_subprogram))
13000 complaint (_("Child DIE %s and its abstract origin %s have "
13001 "different tags"),
13002 sect_offset_str (child_die->sect_off),
13003 sect_offset_str (child_origin_die->sect_off));
13004 if (child_origin_die->parent != origin_die)
13005 complaint (_("Child DIE %s and its abstract origin %s have "
13006 "different parents"),
13007 sect_offset_str (child_die->sect_off),
13008 sect_offset_str (child_origin_die->sect_off));
13009 else
13010 offsets.push_back (child_origin_die->sect_off);
13011 }
13012
13013 if (are_isomorphic)
13014 corresponding_abstract_child = corresponding_abstract_child->sibling;
13015 }
13016 std::sort (offsets.begin (), offsets.end ());
13017 sect_offset *offsets_end = offsets.data () + offsets.size ();
13018 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13019 if (offsetp[-1] == *offsetp)
13020 complaint (_("Multiple children of DIE %s refer "
13021 "to DIE %s as their abstract origin"),
13022 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13023
13024 offsetp = offsets.data ();
13025 origin_child_die = origin_die->child;
13026 while (origin_child_die && origin_child_die->tag)
13027 {
13028 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13029 while (offsetp < offsets_end
13030 && *offsetp < origin_child_die->sect_off)
13031 offsetp++;
13032 if (offsetp >= offsets_end
13033 || *offsetp > origin_child_die->sect_off)
13034 {
13035 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13036 Check whether we're already processing ORIGIN_CHILD_DIE.
13037 This can happen with mutually referenced abstract_origins.
13038 PR 16581. */
13039 if (!origin_child_die->in_process)
13040 process_die (origin_child_die, origin_cu);
13041 }
13042 origin_child_die = origin_child_die->sibling;
13043 }
13044 origin_cu->list_in_scope = origin_previous_list_in_scope;
13045
13046 if (cu != origin_cu)
13047 compute_delayed_physnames (origin_cu);
13048 }
13049
13050 static void
13051 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13052 {
13053 struct objfile *objfile = cu->per_objfile->objfile;
13054 struct gdbarch *gdbarch = objfile->arch ();
13055 struct context_stack *newobj;
13056 CORE_ADDR lowpc;
13057 CORE_ADDR highpc;
13058 struct die_info *child_die;
13059 struct attribute *attr, *call_line, *call_file;
13060 const char *name;
13061 CORE_ADDR baseaddr;
13062 struct block *block;
13063 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13064 std::vector<struct symbol *> template_args;
13065 struct template_symbol *templ_func = NULL;
13066
13067 if (inlined_func)
13068 {
13069 /* If we do not have call site information, we can't show the
13070 caller of this inlined function. That's too confusing, so
13071 only use the scope for local variables. */
13072 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13073 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13074 if (call_line == NULL || call_file == NULL)
13075 {
13076 read_lexical_block_scope (die, cu);
13077 return;
13078 }
13079 }
13080
13081 baseaddr = objfile->text_section_offset ();
13082
13083 name = dwarf2_name (die, cu);
13084
13085 /* Ignore functions with missing or empty names. These are actually
13086 illegal according to the DWARF standard. */
13087 if (name == NULL)
13088 {
13089 complaint (_("missing name for subprogram DIE at %s"),
13090 sect_offset_str (die->sect_off));
13091 return;
13092 }
13093
13094 /* Ignore functions with missing or invalid low and high pc attributes. */
13095 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13096 <= PC_BOUNDS_INVALID)
13097 {
13098 attr = dwarf2_attr (die, DW_AT_external, cu);
13099 if (attr == nullptr || !attr->as_boolean ())
13100 complaint (_("cannot get low and high bounds "
13101 "for subprogram DIE at %s"),
13102 sect_offset_str (die->sect_off));
13103 return;
13104 }
13105
13106 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13107 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13108
13109 /* If we have any template arguments, then we must allocate a
13110 different sort of symbol. */
13111 for (child_die = die->child; child_die; child_die = child_die->sibling)
13112 {
13113 if (child_die->tag == DW_TAG_template_type_param
13114 || child_die->tag == DW_TAG_template_value_param)
13115 {
13116 templ_func = new (&objfile->objfile_obstack) template_symbol;
13117 templ_func->subclass = SYMBOL_TEMPLATE;
13118 break;
13119 }
13120 }
13121
13122 gdb_assert (cu->get_builder () != nullptr);
13123 newobj = cu->get_builder ()->push_context (0, lowpc);
13124 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13125 (struct symbol *) templ_func);
13126
13127 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13128 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13129 cu->per_cu->lang);
13130
13131 /* If there is a location expression for DW_AT_frame_base, record
13132 it. */
13133 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13134 if (attr != nullptr)
13135 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13136
13137 /* If there is a location for the static link, record it. */
13138 newobj->static_link = NULL;
13139 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13140 if (attr != nullptr)
13141 {
13142 newobj->static_link
13143 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13144 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13145 cu->addr_type ());
13146 }
13147
13148 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13149
13150 if (die->child != NULL)
13151 {
13152 child_die = die->child;
13153 while (child_die && child_die->tag)
13154 {
13155 if (child_die->tag == DW_TAG_template_type_param
13156 || child_die->tag == DW_TAG_template_value_param)
13157 {
13158 struct symbol *arg = new_symbol (child_die, NULL, cu);
13159
13160 if (arg != NULL)
13161 template_args.push_back (arg);
13162 }
13163 else
13164 process_die (child_die, cu);
13165 child_die = child_die->sibling;
13166 }
13167 }
13168
13169 inherit_abstract_dies (die, cu);
13170
13171 /* If we have a DW_AT_specification, we might need to import using
13172 directives from the context of the specification DIE. See the
13173 comment in determine_prefix. */
13174 if (cu->per_cu->lang == language_cplus
13175 && dwarf2_attr (die, DW_AT_specification, cu))
13176 {
13177 struct dwarf2_cu *spec_cu = cu;
13178 struct die_info *spec_die = die_specification (die, &spec_cu);
13179
13180 while (spec_die)
13181 {
13182 child_die = spec_die->child;
13183 while (child_die && child_die->tag)
13184 {
13185 if (child_die->tag == DW_TAG_imported_module)
13186 process_die (child_die, spec_cu);
13187 child_die = child_die->sibling;
13188 }
13189
13190 /* In some cases, GCC generates specification DIEs that
13191 themselves contain DW_AT_specification attributes. */
13192 spec_die = die_specification (spec_die, &spec_cu);
13193 }
13194 }
13195
13196 struct context_stack cstk = cu->get_builder ()->pop_context ();
13197 /* Make a block for the local symbols within. */
13198 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13199 cstk.static_link, lowpc, highpc);
13200
13201 /* For C++, set the block's scope. */
13202 if ((cu->per_cu->lang == language_cplus
13203 || cu->per_cu->lang == language_fortran
13204 || cu->per_cu->lang == language_d
13205 || cu->per_cu->lang == language_rust)
13206 && cu->processing_has_namespace_info)
13207 block_set_scope (block, determine_prefix (die, cu),
13208 &objfile->objfile_obstack);
13209
13210 /* If we have address ranges, record them. */
13211 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13212
13213 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13214
13215 /* Attach template arguments to function. */
13216 if (!template_args.empty ())
13217 {
13218 gdb_assert (templ_func != NULL);
13219
13220 templ_func->n_template_arguments = template_args.size ();
13221 templ_func->template_arguments
13222 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13223 templ_func->n_template_arguments);
13224 memcpy (templ_func->template_arguments,
13225 template_args.data (),
13226 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13227
13228 /* Make sure that the symtab is set on the new symbols. Even
13229 though they don't appear in this symtab directly, other parts
13230 of gdb assume that symbols do, and this is reasonably
13231 true. */
13232 for (symbol *sym : template_args)
13233 symbol_set_symtab (sym, symbol_symtab (templ_func));
13234 }
13235
13236 /* In C++, we can have functions nested inside functions (e.g., when
13237 a function declares a class that has methods). This means that
13238 when we finish processing a function scope, we may need to go
13239 back to building a containing block's symbol lists. */
13240 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13241 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13242
13243 /* If we've finished processing a top-level function, subsequent
13244 symbols go in the file symbol list. */
13245 if (cu->get_builder ()->outermost_context_p ())
13246 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13247 }
13248
13249 /* Process all the DIES contained within a lexical block scope. Start
13250 a new scope, process the dies, and then close the scope. */
13251
13252 static void
13253 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13254 {
13255 struct objfile *objfile = cu->per_objfile->objfile;
13256 struct gdbarch *gdbarch = objfile->arch ();
13257 CORE_ADDR lowpc, highpc;
13258 struct die_info *child_die;
13259 CORE_ADDR baseaddr;
13260
13261 baseaddr = objfile->text_section_offset ();
13262
13263 /* Ignore blocks with missing or invalid low and high pc attributes. */
13264 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13265 as multiple lexical blocks? Handling children in a sane way would
13266 be nasty. Might be easier to properly extend generic blocks to
13267 describe ranges. */
13268 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13269 {
13270 case PC_BOUNDS_NOT_PRESENT:
13271 /* DW_TAG_lexical_block has no attributes, process its children as if
13272 there was no wrapping by that DW_TAG_lexical_block.
13273 GCC does no longer produces such DWARF since GCC r224161. */
13274 for (child_die = die->child;
13275 child_die != NULL && child_die->tag;
13276 child_die = child_die->sibling)
13277 {
13278 /* We might already be processing this DIE. This can happen
13279 in an unusual circumstance -- where a subroutine A
13280 appears lexically in another subroutine B, but A actually
13281 inlines B. The recursion is broken here, rather than in
13282 inherit_abstract_dies, because it seems better to simply
13283 drop concrete children here. */
13284 if (!child_die->in_process)
13285 process_die (child_die, cu);
13286 }
13287 return;
13288 case PC_BOUNDS_INVALID:
13289 return;
13290 }
13291 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13292 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13293
13294 cu->get_builder ()->push_context (0, lowpc);
13295 if (die->child != NULL)
13296 {
13297 child_die = die->child;
13298 while (child_die && child_die->tag)
13299 {
13300 process_die (child_die, cu);
13301 child_die = child_die->sibling;
13302 }
13303 }
13304 inherit_abstract_dies (die, cu);
13305 struct context_stack cstk = cu->get_builder ()->pop_context ();
13306
13307 if (*cu->get_builder ()->get_local_symbols () != NULL
13308 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13309 {
13310 struct block *block
13311 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13312 cstk.start_addr, highpc);
13313
13314 /* Note that recording ranges after traversing children, as we
13315 do here, means that recording a parent's ranges entails
13316 walking across all its children's ranges as they appear in
13317 the address map, which is quadratic behavior.
13318
13319 It would be nicer to record the parent's ranges before
13320 traversing its children, simply overriding whatever you find
13321 there. But since we don't even decide whether to create a
13322 block until after we've traversed its children, that's hard
13323 to do. */
13324 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13325 }
13326 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13327 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13328 }
13329
13330 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13331
13332 static void
13333 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13334 {
13335 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13336 struct objfile *objfile = per_objfile->objfile;
13337 struct gdbarch *gdbarch = objfile->arch ();
13338 CORE_ADDR pc, baseaddr;
13339 struct attribute *attr;
13340 struct call_site *call_site, call_site_local;
13341 void **slot;
13342 int nparams;
13343 struct die_info *child_die;
13344
13345 baseaddr = objfile->text_section_offset ();
13346
13347 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13348 if (attr == NULL)
13349 {
13350 /* This was a pre-DWARF-5 GNU extension alias
13351 for DW_AT_call_return_pc. */
13352 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13353 }
13354 if (!attr)
13355 {
13356 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13357 "DIE %s [in module %s]"),
13358 sect_offset_str (die->sect_off), objfile_name (objfile));
13359 return;
13360 }
13361 pc = attr->as_address () + baseaddr;
13362 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13363
13364 if (cu->call_site_htab == NULL)
13365 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13366 NULL, &objfile->objfile_obstack,
13367 hashtab_obstack_allocate, NULL);
13368 call_site_local.pc = pc;
13369 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13370 if (*slot != NULL)
13371 {
13372 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13373 "DIE %s [in module %s]"),
13374 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13375 objfile_name (objfile));
13376 return;
13377 }
13378
13379 /* Count parameters at the caller. */
13380
13381 nparams = 0;
13382 for (child_die = die->child; child_die && child_die->tag;
13383 child_die = child_die->sibling)
13384 {
13385 if (child_die->tag != DW_TAG_call_site_parameter
13386 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13387 {
13388 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13389 "DW_TAG_call_site child DIE %s [in module %s]"),
13390 child_die->tag, sect_offset_str (child_die->sect_off),
13391 objfile_name (objfile));
13392 continue;
13393 }
13394
13395 nparams++;
13396 }
13397
13398 call_site
13399 = ((struct call_site *)
13400 obstack_alloc (&objfile->objfile_obstack,
13401 sizeof (*call_site)
13402 + (sizeof (*call_site->parameter) * (nparams - 1))));
13403 *slot = call_site;
13404 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13405 call_site->pc = pc;
13406
13407 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13408 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13409 {
13410 struct die_info *func_die;
13411
13412 /* Skip also over DW_TAG_inlined_subroutine. */
13413 for (func_die = die->parent;
13414 func_die && func_die->tag != DW_TAG_subprogram
13415 && func_die->tag != DW_TAG_subroutine_type;
13416 func_die = func_die->parent);
13417
13418 /* DW_AT_call_all_calls is a superset
13419 of DW_AT_call_all_tail_calls. */
13420 if (func_die
13421 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13422 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13423 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13424 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13425 {
13426 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13427 not complete. But keep CALL_SITE for look ups via call_site_htab,
13428 both the initial caller containing the real return address PC and
13429 the final callee containing the current PC of a chain of tail
13430 calls do not need to have the tail call list complete. But any
13431 function candidate for a virtual tail call frame searched via
13432 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13433 determined unambiguously. */
13434 }
13435 else
13436 {
13437 struct type *func_type = NULL;
13438
13439 if (func_die)
13440 func_type = get_die_type (func_die, cu);
13441 if (func_type != NULL)
13442 {
13443 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13444
13445 /* Enlist this call site to the function. */
13446 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13447 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13448 }
13449 else
13450 complaint (_("Cannot find function owning DW_TAG_call_site "
13451 "DIE %s [in module %s]"),
13452 sect_offset_str (die->sect_off), objfile_name (objfile));
13453 }
13454 }
13455
13456 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13457 if (attr == NULL)
13458 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13459 if (attr == NULL)
13460 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13461 if (attr == NULL)
13462 {
13463 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13464 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13465 }
13466 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13467 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13468 /* Keep NULL DWARF_BLOCK. */;
13469 else if (attr->form_is_block ())
13470 {
13471 struct dwarf2_locexpr_baton *dlbaton;
13472 struct dwarf_block *block = attr->as_block ();
13473
13474 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13475 dlbaton->data = block->data;
13476 dlbaton->size = block->size;
13477 dlbaton->per_objfile = per_objfile;
13478 dlbaton->per_cu = cu->per_cu;
13479
13480 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13481 }
13482 else if (attr->form_is_ref ())
13483 {
13484 struct dwarf2_cu *target_cu = cu;
13485 struct die_info *target_die;
13486
13487 target_die = follow_die_ref (die, attr, &target_cu);
13488 gdb_assert (target_cu->per_objfile->objfile == objfile);
13489 if (die_is_declaration (target_die, target_cu))
13490 {
13491 const char *target_physname;
13492
13493 /* Prefer the mangled name; otherwise compute the demangled one. */
13494 target_physname = dw2_linkage_name (target_die, target_cu);
13495 if (target_physname == NULL)
13496 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13497 if (target_physname == NULL)
13498 complaint (_("DW_AT_call_target target DIE has invalid "
13499 "physname, for referencing DIE %s [in module %s]"),
13500 sect_offset_str (die->sect_off), objfile_name (objfile));
13501 else
13502 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13503 }
13504 else
13505 {
13506 CORE_ADDR lowpc;
13507
13508 /* DW_AT_entry_pc should be preferred. */
13509 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13510 <= PC_BOUNDS_INVALID)
13511 complaint (_("DW_AT_call_target target DIE has invalid "
13512 "low pc, for referencing DIE %s [in module %s]"),
13513 sect_offset_str (die->sect_off), objfile_name (objfile));
13514 else
13515 {
13516 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13517 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13518 }
13519 }
13520 }
13521 else
13522 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13523 "block nor reference, for DIE %s [in module %s]"),
13524 sect_offset_str (die->sect_off), objfile_name (objfile));
13525
13526 call_site->per_cu = cu->per_cu;
13527 call_site->per_objfile = per_objfile;
13528
13529 for (child_die = die->child;
13530 child_die && child_die->tag;
13531 child_die = child_die->sibling)
13532 {
13533 struct call_site_parameter *parameter;
13534 struct attribute *loc, *origin;
13535
13536 if (child_die->tag != DW_TAG_call_site_parameter
13537 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13538 {
13539 /* Already printed the complaint above. */
13540 continue;
13541 }
13542
13543 gdb_assert (call_site->parameter_count < nparams);
13544 parameter = &call_site->parameter[call_site->parameter_count];
13545
13546 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13547 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13548 register is contained in DW_AT_call_value. */
13549
13550 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13551 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13552 if (origin == NULL)
13553 {
13554 /* This was a pre-DWARF-5 GNU extension alias
13555 for DW_AT_call_parameter. */
13556 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13557 }
13558 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13559 {
13560 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13561
13562 sect_offset sect_off = origin->get_ref_die_offset ();
13563 if (!cu->header.offset_in_cu_p (sect_off))
13564 {
13565 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13566 binding can be done only inside one CU. Such referenced DIE
13567 therefore cannot be even moved to DW_TAG_partial_unit. */
13568 complaint (_("DW_AT_call_parameter offset is not in CU for "
13569 "DW_TAG_call_site child DIE %s [in module %s]"),
13570 sect_offset_str (child_die->sect_off),
13571 objfile_name (objfile));
13572 continue;
13573 }
13574 parameter->u.param_cu_off
13575 = (cu_offset) (sect_off - cu->header.sect_off);
13576 }
13577 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13578 {
13579 complaint (_("No DW_FORM_block* DW_AT_location for "
13580 "DW_TAG_call_site child DIE %s [in module %s]"),
13581 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13582 continue;
13583 }
13584 else
13585 {
13586 struct dwarf_block *block = loc->as_block ();
13587
13588 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13589 (block->data, &block->data[block->size]);
13590 if (parameter->u.dwarf_reg != -1)
13591 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13592 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13593 &block->data[block->size],
13594 &parameter->u.fb_offset))
13595 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13596 else
13597 {
13598 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13599 "for DW_FORM_block* DW_AT_location is supported for "
13600 "DW_TAG_call_site child DIE %s "
13601 "[in module %s]"),
13602 sect_offset_str (child_die->sect_off),
13603 objfile_name (objfile));
13604 continue;
13605 }
13606 }
13607
13608 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13609 if (attr == NULL)
13610 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13611 if (attr == NULL || !attr->form_is_block ())
13612 {
13613 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13614 "DW_TAG_call_site child DIE %s [in module %s]"),
13615 sect_offset_str (child_die->sect_off),
13616 objfile_name (objfile));
13617 continue;
13618 }
13619
13620 struct dwarf_block *block = attr->as_block ();
13621 parameter->value = block->data;
13622 parameter->value_size = block->size;
13623
13624 /* Parameters are not pre-cleared by memset above. */
13625 parameter->data_value = NULL;
13626 parameter->data_value_size = 0;
13627 call_site->parameter_count++;
13628
13629 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13630 if (attr == NULL)
13631 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13632 if (attr != nullptr)
13633 {
13634 if (!attr->form_is_block ())
13635 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13636 "DW_TAG_call_site child DIE %s [in module %s]"),
13637 sect_offset_str (child_die->sect_off),
13638 objfile_name (objfile));
13639 else
13640 {
13641 block = attr->as_block ();
13642 parameter->data_value = block->data;
13643 parameter->data_value_size = block->size;
13644 }
13645 }
13646 }
13647 }
13648
13649 /* Helper function for read_variable. If DIE represents a virtual
13650 table, then return the type of the concrete object that is
13651 associated with the virtual table. Otherwise, return NULL. */
13652
13653 static struct type *
13654 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13655 {
13656 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13657 if (attr == NULL)
13658 return NULL;
13659
13660 /* Find the type DIE. */
13661 struct die_info *type_die = NULL;
13662 struct dwarf2_cu *type_cu = cu;
13663
13664 if (attr->form_is_ref ())
13665 type_die = follow_die_ref (die, attr, &type_cu);
13666 if (type_die == NULL)
13667 return NULL;
13668
13669 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13670 return NULL;
13671 return die_containing_type (type_die, type_cu);
13672 }
13673
13674 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13675
13676 static void
13677 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13678 {
13679 struct rust_vtable_symbol *storage = NULL;
13680
13681 if (cu->per_cu->lang == language_rust)
13682 {
13683 struct type *containing_type = rust_containing_type (die, cu);
13684
13685 if (containing_type != NULL)
13686 {
13687 struct objfile *objfile = cu->per_objfile->objfile;
13688
13689 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13690 storage->concrete_type = containing_type;
13691 storage->subclass = SYMBOL_RUST_VTABLE;
13692 }
13693 }
13694
13695 struct symbol *res = new_symbol (die, NULL, cu, storage);
13696 struct attribute *abstract_origin
13697 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13698 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13699 if (res == NULL && loc && abstract_origin)
13700 {
13701 /* We have a variable without a name, but with a location and an abstract
13702 origin. This may be a concrete instance of an abstract variable
13703 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13704 later. */
13705 struct dwarf2_cu *origin_cu = cu;
13706 struct die_info *origin_die
13707 = follow_die_ref (die, abstract_origin, &origin_cu);
13708 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13709 per_objfile->per_bfd->abstract_to_concrete
13710 [origin_die->sect_off].push_back (die->sect_off);
13711 }
13712 }
13713
13714 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13715 reading .debug_rnglists.
13716 Callback's type should be:
13717 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13718 Return true if the attributes are present and valid, otherwise,
13719 return false. */
13720
13721 template <typename Callback>
13722 static bool
13723 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13724 dwarf_tag tag, Callback &&callback)
13725 {
13726 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13727 struct objfile *objfile = per_objfile->objfile;
13728 bfd *obfd = objfile->obfd;
13729 /* Base address selection entry. */
13730 gdb::optional<CORE_ADDR> base;
13731 const gdb_byte *buffer;
13732 bool overflow = false;
13733 ULONGEST addr_index;
13734 struct dwarf2_section_info *rnglists_section;
13735
13736 base = cu->base_address;
13737 rnglists_section = cu_debug_rnglists_section (cu, tag);
13738 rnglists_section->read (objfile);
13739
13740 if (offset >= rnglists_section->size)
13741 {
13742 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13743 offset);
13744 return false;
13745 }
13746 buffer = rnglists_section->buffer + offset;
13747
13748 while (1)
13749 {
13750 /* Initialize it due to a false compiler warning. */
13751 CORE_ADDR range_beginning = 0, range_end = 0;
13752 const gdb_byte *buf_end = (rnglists_section->buffer
13753 + rnglists_section->size);
13754 unsigned int bytes_read;
13755
13756 if (buffer == buf_end)
13757 {
13758 overflow = true;
13759 break;
13760 }
13761 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13762 switch (rlet)
13763 {
13764 case DW_RLE_end_of_list:
13765 break;
13766 case DW_RLE_base_address:
13767 if (buffer + cu->header.addr_size > buf_end)
13768 {
13769 overflow = true;
13770 break;
13771 }
13772 base = cu->header.read_address (obfd, buffer, &bytes_read);
13773 buffer += bytes_read;
13774 break;
13775 case DW_RLE_base_addressx:
13776 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13777 buffer += bytes_read;
13778 base = read_addr_index (cu, addr_index);
13779 break;
13780 case DW_RLE_start_length:
13781 if (buffer + cu->header.addr_size > buf_end)
13782 {
13783 overflow = true;
13784 break;
13785 }
13786 range_beginning = cu->header.read_address (obfd, buffer,
13787 &bytes_read);
13788 buffer += bytes_read;
13789 range_end = (range_beginning
13790 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13791 buffer += bytes_read;
13792 if (buffer > buf_end)
13793 {
13794 overflow = true;
13795 break;
13796 }
13797 break;
13798 case DW_RLE_startx_length:
13799 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13800 buffer += bytes_read;
13801 range_beginning = read_addr_index (cu, addr_index);
13802 if (buffer > buf_end)
13803 {
13804 overflow = true;
13805 break;
13806 }
13807 range_end = (range_beginning
13808 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13809 buffer += bytes_read;
13810 break;
13811 case DW_RLE_offset_pair:
13812 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13813 buffer += bytes_read;
13814 if (buffer > buf_end)
13815 {
13816 overflow = true;
13817 break;
13818 }
13819 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13820 buffer += bytes_read;
13821 if (buffer > buf_end)
13822 {
13823 overflow = true;
13824 break;
13825 }
13826 break;
13827 case DW_RLE_start_end:
13828 if (buffer + 2 * cu->header.addr_size > buf_end)
13829 {
13830 overflow = true;
13831 break;
13832 }
13833 range_beginning = cu->header.read_address (obfd, buffer,
13834 &bytes_read);
13835 buffer += bytes_read;
13836 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13837 buffer += bytes_read;
13838 break;
13839 case DW_RLE_startx_endx:
13840 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13841 buffer += bytes_read;
13842 range_beginning = read_addr_index (cu, addr_index);
13843 if (buffer > buf_end)
13844 {
13845 overflow = true;
13846 break;
13847 }
13848 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13849 buffer += bytes_read;
13850 range_end = read_addr_index (cu, addr_index);
13851 break;
13852 default:
13853 complaint (_("Invalid .debug_rnglists data (no base address)"));
13854 return false;
13855 }
13856 if (rlet == DW_RLE_end_of_list || overflow)
13857 break;
13858 if (rlet == DW_RLE_base_address)
13859 continue;
13860
13861 if (range_beginning > range_end)
13862 {
13863 /* Inverted range entries are invalid. */
13864 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13865 return false;
13866 }
13867
13868 /* Empty range entries have no effect. */
13869 if (range_beginning == range_end)
13870 continue;
13871
13872 /* Only DW_RLE_offset_pair needs the base address added. */
13873 if (rlet == DW_RLE_offset_pair)
13874 {
13875 if (!base.has_value ())
13876 {
13877 /* We have no valid base address for the DW_RLE_offset_pair. */
13878 complaint (_("Invalid .debug_rnglists data (no base address for "
13879 "DW_RLE_offset_pair)"));
13880 return false;
13881 }
13882
13883 range_beginning += *base;
13884 range_end += *base;
13885 }
13886
13887 /* A not-uncommon case of bad debug info.
13888 Don't pollute the addrmap with bad data. */
13889 if (range_beginning == 0
13890 && !per_objfile->per_bfd->has_section_at_zero)
13891 {
13892 complaint (_(".debug_rnglists entry has start address of zero"
13893 " [in module %s]"), objfile_name (objfile));
13894 continue;
13895 }
13896
13897 callback (range_beginning, range_end);
13898 }
13899
13900 if (overflow)
13901 {
13902 complaint (_("Offset %d is not terminated "
13903 "for DW_AT_ranges attribute"),
13904 offset);
13905 return false;
13906 }
13907
13908 return true;
13909 }
13910
13911 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13912 Callback's type should be:
13913 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13914 Return 1 if the attributes are present and valid, otherwise, return 0. */
13915
13916 template <typename Callback>
13917 static int
13918 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13919 Callback &&callback)
13920 {
13921 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13922 struct objfile *objfile = per_objfile->objfile;
13923 struct comp_unit_head *cu_header = &cu->header;
13924 bfd *obfd = objfile->obfd;
13925 unsigned int addr_size = cu_header->addr_size;
13926 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13927 /* Base address selection entry. */
13928 gdb::optional<CORE_ADDR> base;
13929 unsigned int dummy;
13930 const gdb_byte *buffer;
13931
13932 if (cu_header->version >= 5)
13933 return dwarf2_rnglists_process (offset, cu, tag, callback);
13934
13935 base = cu->base_address;
13936
13937 per_objfile->per_bfd->ranges.read (objfile);
13938 if (offset >= per_objfile->per_bfd->ranges.size)
13939 {
13940 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13941 offset);
13942 return 0;
13943 }
13944 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13945
13946 while (1)
13947 {
13948 CORE_ADDR range_beginning, range_end;
13949
13950 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13951 buffer += addr_size;
13952 range_end = cu->header.read_address (obfd, buffer, &dummy);
13953 buffer += addr_size;
13954 offset += 2 * addr_size;
13955
13956 /* An end of list marker is a pair of zero addresses. */
13957 if (range_beginning == 0 && range_end == 0)
13958 /* Found the end of list entry. */
13959 break;
13960
13961 /* Each base address selection entry is a pair of 2 values.
13962 The first is the largest possible address, the second is
13963 the base address. Check for a base address here. */
13964 if ((range_beginning & mask) == mask)
13965 {
13966 /* If we found the largest possible address, then we already
13967 have the base address in range_end. */
13968 base = range_end;
13969 continue;
13970 }
13971
13972 if (!base.has_value ())
13973 {
13974 /* We have no valid base address for the ranges
13975 data. */
13976 complaint (_("Invalid .debug_ranges data (no base address)"));
13977 return 0;
13978 }
13979
13980 if (range_beginning > range_end)
13981 {
13982 /* Inverted range entries are invalid. */
13983 complaint (_("Invalid .debug_ranges data (inverted range)"));
13984 return 0;
13985 }
13986
13987 /* Empty range entries have no effect. */
13988 if (range_beginning == range_end)
13989 continue;
13990
13991 range_beginning += *base;
13992 range_end += *base;
13993
13994 /* A not-uncommon case of bad debug info.
13995 Don't pollute the addrmap with bad data. */
13996 if (range_beginning == 0
13997 && !per_objfile->per_bfd->has_section_at_zero)
13998 {
13999 complaint (_(".debug_ranges entry has start address of zero"
14000 " [in module %s]"), objfile_name (objfile));
14001 continue;
14002 }
14003
14004 callback (range_beginning, range_end);
14005 }
14006
14007 return 1;
14008 }
14009
14010 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14011 Return 1 if the attributes are present and valid, otherwise, return 0.
14012 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14013
14014 static int
14015 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14016 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14017 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14018 {
14019 struct objfile *objfile = cu->per_objfile->objfile;
14020 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14021 struct gdbarch *gdbarch = objfile->arch ();
14022 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14023 int low_set = 0;
14024 CORE_ADDR low = 0;
14025 CORE_ADDR high = 0;
14026 int retval;
14027
14028 retval = dwarf2_ranges_process (offset, cu, tag,
14029 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14030 {
14031 if (ranges_pst != NULL)
14032 {
14033 CORE_ADDR lowpc;
14034 CORE_ADDR highpc;
14035
14036 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14037 range_beginning + baseaddr)
14038 - baseaddr);
14039 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14040 range_end + baseaddr)
14041 - baseaddr);
14042 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14043 lowpc, highpc - 1, ranges_pst);
14044 }
14045
14046 /* FIXME: This is recording everything as a low-high
14047 segment of consecutive addresses. We should have a
14048 data structure for discontiguous block ranges
14049 instead. */
14050 if (! low_set)
14051 {
14052 low = range_beginning;
14053 high = range_end;
14054 low_set = 1;
14055 }
14056 else
14057 {
14058 if (range_beginning < low)
14059 low = range_beginning;
14060 if (range_end > high)
14061 high = range_end;
14062 }
14063 });
14064 if (!retval)
14065 return 0;
14066
14067 if (! low_set)
14068 /* If the first entry is an end-of-list marker, the range
14069 describes an empty scope, i.e. no instructions. */
14070 return 0;
14071
14072 if (low_return)
14073 *low_return = low;
14074 if (high_return)
14075 *high_return = high;
14076 return 1;
14077 }
14078
14079 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14080 definition for the return value. *LOWPC and *HIGHPC are set iff
14081 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14082
14083 static enum pc_bounds_kind
14084 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14085 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14086 dwarf2_psymtab *pst)
14087 {
14088 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14089 struct attribute *attr;
14090 struct attribute *attr_high;
14091 CORE_ADDR low = 0;
14092 CORE_ADDR high = 0;
14093 enum pc_bounds_kind ret;
14094
14095 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14096 if (attr_high)
14097 {
14098 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14099 if (attr != nullptr)
14100 {
14101 low = attr->as_address ();
14102 high = attr_high->as_address ();
14103 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14104 high += low;
14105 }
14106 else
14107 /* Found high w/o low attribute. */
14108 return PC_BOUNDS_INVALID;
14109
14110 /* Found consecutive range of addresses. */
14111 ret = PC_BOUNDS_HIGH_LOW;
14112 }
14113 else
14114 {
14115 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14116 if (attr != nullptr && attr->form_is_unsigned ())
14117 {
14118 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14119 on DWARF version). */
14120 ULONGEST ranges_offset = attr->as_unsigned ();
14121
14122 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14123 this value. */
14124 if (die->tag != DW_TAG_compile_unit)
14125 ranges_offset += cu->gnu_ranges_base;
14126
14127 /* Value of the DW_AT_ranges attribute is the offset in the
14128 .debug_ranges section. */
14129 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14130 die->tag))
14131 return PC_BOUNDS_INVALID;
14132 /* Found discontinuous range of addresses. */
14133 ret = PC_BOUNDS_RANGES;
14134 }
14135 else
14136 return PC_BOUNDS_NOT_PRESENT;
14137 }
14138
14139 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14140 if (high <= low)
14141 return PC_BOUNDS_INVALID;
14142
14143 /* When using the GNU linker, .gnu.linkonce. sections are used to
14144 eliminate duplicate copies of functions and vtables and such.
14145 The linker will arbitrarily choose one and discard the others.
14146 The AT_*_pc values for such functions refer to local labels in
14147 these sections. If the section from that file was discarded, the
14148 labels are not in the output, so the relocs get a value of 0.
14149 If this is a discarded function, mark the pc bounds as invalid,
14150 so that GDB will ignore it. */
14151 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14152 return PC_BOUNDS_INVALID;
14153
14154 *lowpc = low;
14155 if (highpc)
14156 *highpc = high;
14157 return ret;
14158 }
14159
14160 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14161 its low and high PC addresses. Do nothing if these addresses could not
14162 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14163 and HIGHPC to the high address if greater than HIGHPC. */
14164
14165 static void
14166 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14167 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14168 struct dwarf2_cu *cu)
14169 {
14170 CORE_ADDR low, high;
14171 struct die_info *child = die->child;
14172
14173 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14174 {
14175 *lowpc = std::min (*lowpc, low);
14176 *highpc = std::max (*highpc, high);
14177 }
14178
14179 /* If the language does not allow nested subprograms (either inside
14180 subprograms or lexical blocks), we're done. */
14181 if (cu->per_cu->lang != language_ada)
14182 return;
14183
14184 /* Check all the children of the given DIE. If it contains nested
14185 subprograms, then check their pc bounds. Likewise, we need to
14186 check lexical blocks as well, as they may also contain subprogram
14187 definitions. */
14188 while (child && child->tag)
14189 {
14190 if (child->tag == DW_TAG_subprogram
14191 || child->tag == DW_TAG_lexical_block)
14192 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14193 child = child->sibling;
14194 }
14195 }
14196
14197 /* Get the low and high pc's represented by the scope DIE, and store
14198 them in *LOWPC and *HIGHPC. If the correct values can't be
14199 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14200
14201 static void
14202 get_scope_pc_bounds (struct die_info *die,
14203 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14204 struct dwarf2_cu *cu)
14205 {
14206 CORE_ADDR best_low = (CORE_ADDR) -1;
14207 CORE_ADDR best_high = (CORE_ADDR) 0;
14208 CORE_ADDR current_low, current_high;
14209
14210 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14211 >= PC_BOUNDS_RANGES)
14212 {
14213 best_low = current_low;
14214 best_high = current_high;
14215 }
14216 else
14217 {
14218 struct die_info *child = die->child;
14219
14220 while (child && child->tag)
14221 {
14222 switch (child->tag) {
14223 case DW_TAG_subprogram:
14224 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14225 break;
14226 case DW_TAG_namespace:
14227 case DW_TAG_module:
14228 /* FIXME: carlton/2004-01-16: Should we do this for
14229 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14230 that current GCC's always emit the DIEs corresponding
14231 to definitions of methods of classes as children of a
14232 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14233 the DIEs giving the declarations, which could be
14234 anywhere). But I don't see any reason why the
14235 standards says that they have to be there. */
14236 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14237
14238 if (current_low != ((CORE_ADDR) -1))
14239 {
14240 best_low = std::min (best_low, current_low);
14241 best_high = std::max (best_high, current_high);
14242 }
14243 break;
14244 default:
14245 /* Ignore. */
14246 break;
14247 }
14248
14249 child = child->sibling;
14250 }
14251 }
14252
14253 *lowpc = best_low;
14254 *highpc = best_high;
14255 }
14256
14257 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14258 in DIE. */
14259
14260 static void
14261 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14262 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14263 {
14264 struct objfile *objfile = cu->per_objfile->objfile;
14265 struct gdbarch *gdbarch = objfile->arch ();
14266 struct attribute *attr;
14267 struct attribute *attr_high;
14268
14269 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14270 if (attr_high)
14271 {
14272 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14273 if (attr != nullptr)
14274 {
14275 CORE_ADDR low = attr->as_address ();
14276 CORE_ADDR high = attr_high->as_address ();
14277
14278 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14279 high += low;
14280
14281 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14282 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14283 cu->get_builder ()->record_block_range (block, low, high - 1);
14284 }
14285 }
14286
14287 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14288 if (attr != nullptr && attr->form_is_unsigned ())
14289 {
14290 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14291 on DWARF version). */
14292 ULONGEST ranges_offset = attr->as_unsigned ();
14293
14294 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14295 this value. */
14296 if (die->tag != DW_TAG_compile_unit)
14297 ranges_offset += cu->gnu_ranges_base;
14298
14299 std::vector<blockrange> blockvec;
14300 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14301 [&] (CORE_ADDR start, CORE_ADDR end)
14302 {
14303 start += baseaddr;
14304 end += baseaddr;
14305 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14306 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14307 cu->get_builder ()->record_block_range (block, start, end - 1);
14308 blockvec.emplace_back (start, end);
14309 });
14310
14311 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14312 }
14313 }
14314
14315 /* Check whether the producer field indicates either of GCC < 4.6, or the
14316 Intel C/C++ compiler, and cache the result in CU. */
14317
14318 static void
14319 check_producer (struct dwarf2_cu *cu)
14320 {
14321 int major, minor;
14322
14323 if (cu->producer == NULL)
14324 {
14325 /* For unknown compilers expect their behavior is DWARF version
14326 compliant.
14327
14328 GCC started to support .debug_types sections by -gdwarf-4 since
14329 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14330 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14331 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14332 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14333 }
14334 else if (producer_is_gcc (cu->producer, &major, &minor))
14335 {
14336 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14337 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14338 }
14339 else if (producer_is_icc (cu->producer, &major, &minor))
14340 {
14341 cu->producer_is_icc = true;
14342 cu->producer_is_icc_lt_14 = major < 14;
14343 }
14344 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14345 cu->producer_is_codewarrior = true;
14346 else
14347 {
14348 /* For other non-GCC compilers, expect their behavior is DWARF version
14349 compliant. */
14350 }
14351
14352 cu->checked_producer = true;
14353 }
14354
14355 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14356 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14357 during 4.6.0 experimental. */
14358
14359 static bool
14360 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14361 {
14362 if (!cu->checked_producer)
14363 check_producer (cu);
14364
14365 return cu->producer_is_gxx_lt_4_6;
14366 }
14367
14368
14369 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14370 with incorrect is_stmt attributes. */
14371
14372 static bool
14373 producer_is_codewarrior (struct dwarf2_cu *cu)
14374 {
14375 if (!cu->checked_producer)
14376 check_producer (cu);
14377
14378 return cu->producer_is_codewarrior;
14379 }
14380
14381 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14382 If that attribute is not available, return the appropriate
14383 default. */
14384
14385 static enum dwarf_access_attribute
14386 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14387 {
14388 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14389 if (attr != nullptr)
14390 {
14391 LONGEST value = attr->constant_value (-1);
14392 if (value == DW_ACCESS_public
14393 || value == DW_ACCESS_protected
14394 || value == DW_ACCESS_private)
14395 return (dwarf_access_attribute) value;
14396 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14397 plongest (value));
14398 }
14399
14400 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14401 {
14402 /* The default DWARF 2 accessibility for members is public, the default
14403 accessibility for inheritance is private. */
14404
14405 if (die->tag != DW_TAG_inheritance)
14406 return DW_ACCESS_public;
14407 else
14408 return DW_ACCESS_private;
14409 }
14410 else
14411 {
14412 /* DWARF 3+ defines the default accessibility a different way. The same
14413 rules apply now for DW_TAG_inheritance as for the members and it only
14414 depends on the container kind. */
14415
14416 if (die->parent->tag == DW_TAG_class_type)
14417 return DW_ACCESS_private;
14418 else
14419 return DW_ACCESS_public;
14420 }
14421 }
14422
14423 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
14424 *OFFSET to the byte offset. If the attribute was not found return
14425 0, otherwise return 1. If it was found but could not properly be
14426 handled, set *OFFSET to 0. */
14427
14428 static int
14429 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14430 LONGEST *offset)
14431 {
14432 struct attribute *attr;
14433
14434 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14435 if (attr != NULL)
14436 {
14437 *offset = 0;
14438
14439 /* Note that we do not check for a section offset first here.
14440 This is because DW_AT_data_member_location is new in DWARF 4,
14441 so if we see it, we can assume that a constant form is really
14442 a constant and not a section offset. */
14443 if (attr->form_is_constant ())
14444 *offset = attr->constant_value (0);
14445 else if (attr->form_is_section_offset ())
14446 dwarf2_complex_location_expr_complaint ();
14447 else if (attr->form_is_block ())
14448 *offset = decode_locdesc (attr->as_block (), cu);
14449 else
14450 dwarf2_complex_location_expr_complaint ();
14451
14452 return 1;
14453 }
14454 else
14455 {
14456 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14457 if (attr != nullptr)
14458 {
14459 *offset = attr->constant_value (0);
14460 return 1;
14461 }
14462 }
14463
14464 return 0;
14465 }
14466
14467 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
14468 store the results in FIELD. */
14469
14470 static void
14471 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14472 struct field *field)
14473 {
14474 struct attribute *attr;
14475
14476 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14477 if (attr != NULL)
14478 {
14479 if (attr->form_is_constant ())
14480 {
14481 LONGEST offset = attr->constant_value (0);
14482 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14483 }
14484 else if (attr->form_is_section_offset ())
14485 dwarf2_complex_location_expr_complaint ();
14486 else if (attr->form_is_block ())
14487 {
14488 bool handled;
14489 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14490 if (handled)
14491 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14492 else
14493 {
14494 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14495 struct objfile *objfile = per_objfile->objfile;
14496 struct dwarf2_locexpr_baton *dlbaton
14497 = XOBNEW (&objfile->objfile_obstack,
14498 struct dwarf2_locexpr_baton);
14499 dlbaton->data = attr->as_block ()->data;
14500 dlbaton->size = attr->as_block ()->size;
14501 /* When using this baton, we want to compute the address
14502 of the field, not the value. This is why
14503 is_reference is set to false here. */
14504 dlbaton->is_reference = false;
14505 dlbaton->per_objfile = per_objfile;
14506 dlbaton->per_cu = cu->per_cu;
14507
14508 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14509 }
14510 }
14511 else
14512 dwarf2_complex_location_expr_complaint ();
14513 }
14514 else
14515 {
14516 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14517 if (attr != nullptr)
14518 SET_FIELD_BITPOS (*field, attr->constant_value (0));
14519 }
14520 }
14521
14522 /* Add an aggregate field to the field list. */
14523
14524 static void
14525 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14526 struct dwarf2_cu *cu)
14527 {
14528 struct objfile *objfile = cu->per_objfile->objfile;
14529 struct gdbarch *gdbarch = objfile->arch ();
14530 struct nextfield *new_field;
14531 struct attribute *attr;
14532 struct field *fp;
14533 const char *fieldname = "";
14534
14535 if (die->tag == DW_TAG_inheritance)
14536 {
14537 fip->baseclasses.emplace_back ();
14538 new_field = &fip->baseclasses.back ();
14539 }
14540 else
14541 {
14542 fip->fields.emplace_back ();
14543 new_field = &fip->fields.back ();
14544 }
14545
14546 new_field->offset = die->sect_off;
14547
14548 new_field->accessibility = dwarf2_access_attribute (die, cu);
14549 if (new_field->accessibility != DW_ACCESS_public)
14550 fip->non_public_fields = true;
14551
14552 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14553 if (attr != nullptr)
14554 new_field->virtuality = attr->as_virtuality ();
14555 else
14556 new_field->virtuality = DW_VIRTUALITY_none;
14557
14558 fp = &new_field->field;
14559
14560 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14561 {
14562 /* Data member other than a C++ static data member. */
14563
14564 /* Get type of field. */
14565 fp->set_type (die_type (die, cu));
14566
14567 SET_FIELD_BITPOS (*fp, 0);
14568
14569 /* Get bit size of field (zero if none). */
14570 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14571 if (attr != nullptr)
14572 {
14573 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14574 }
14575 else
14576 {
14577 FIELD_BITSIZE (*fp) = 0;
14578 }
14579
14580 /* Get bit offset of field. */
14581 handle_member_location (die, cu, fp);
14582 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14583 if (attr != nullptr && attr->form_is_constant ())
14584 {
14585 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14586 {
14587 /* For big endian bits, the DW_AT_bit_offset gives the
14588 additional bit offset from the MSB of the containing
14589 anonymous object to the MSB of the field. We don't
14590 have to do anything special since we don't need to
14591 know the size of the anonymous object. */
14592 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14593 + attr->constant_value (0)));
14594 }
14595 else
14596 {
14597 /* For little endian bits, compute the bit offset to the
14598 MSB of the anonymous object, subtract off the number of
14599 bits from the MSB of the field to the MSB of the
14600 object, and then subtract off the number of bits of
14601 the field itself. The result is the bit offset of
14602 the LSB of the field. */
14603 int anonymous_size;
14604 int bit_offset = attr->constant_value (0);
14605
14606 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14607 if (attr != nullptr && attr->form_is_constant ())
14608 {
14609 /* The size of the anonymous object containing
14610 the bit field is explicit, so use the
14611 indicated size (in bytes). */
14612 anonymous_size = attr->constant_value (0);
14613 }
14614 else
14615 {
14616 /* The size of the anonymous object containing
14617 the bit field must be inferred from the type
14618 attribute of the data member containing the
14619 bit field. */
14620 anonymous_size = TYPE_LENGTH (fp->type ());
14621 }
14622 SET_FIELD_BITPOS (*fp,
14623 (FIELD_BITPOS (*fp)
14624 + anonymous_size * bits_per_byte
14625 - bit_offset - FIELD_BITSIZE (*fp)));
14626 }
14627 }
14628
14629 /* Get name of field. */
14630 fieldname = dwarf2_name (die, cu);
14631 if (fieldname == NULL)
14632 fieldname = "";
14633
14634 /* The name is already allocated along with this objfile, so we don't
14635 need to duplicate it for the type. */
14636 fp->name = fieldname;
14637
14638 /* Change accessibility for artificial fields (e.g. virtual table
14639 pointer or virtual base class pointer) to private. */
14640 if (dwarf2_attr (die, DW_AT_artificial, cu))
14641 {
14642 FIELD_ARTIFICIAL (*fp) = 1;
14643 new_field->accessibility = DW_ACCESS_private;
14644 fip->non_public_fields = true;
14645 }
14646 }
14647 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14648 {
14649 /* C++ static member. */
14650
14651 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14652 is a declaration, but all versions of G++ as of this writing
14653 (so through at least 3.2.1) incorrectly generate
14654 DW_TAG_variable tags. */
14655
14656 const char *physname;
14657
14658 /* Get name of field. */
14659 fieldname = dwarf2_name (die, cu);
14660 if (fieldname == NULL)
14661 return;
14662
14663 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14664 if (attr
14665 /* Only create a symbol if this is an external value.
14666 new_symbol checks this and puts the value in the global symbol
14667 table, which we want. If it is not external, new_symbol
14668 will try to put the value in cu->list_in_scope which is wrong. */
14669 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14670 {
14671 /* A static const member, not much different than an enum as far as
14672 we're concerned, except that we can support more types. */
14673 new_symbol (die, NULL, cu);
14674 }
14675
14676 /* Get physical name. */
14677 physname = dwarf2_physname (fieldname, die, cu);
14678
14679 /* The name is already allocated along with this objfile, so we don't
14680 need to duplicate it for the type. */
14681 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14682 fp->set_type (die_type (die, cu));
14683 FIELD_NAME (*fp) = fieldname;
14684 }
14685 else if (die->tag == DW_TAG_inheritance)
14686 {
14687 /* C++ base class field. */
14688 handle_member_location (die, cu, fp);
14689 FIELD_BITSIZE (*fp) = 0;
14690 fp->set_type (die_type (die, cu));
14691 FIELD_NAME (*fp) = fp->type ()->name ();
14692 }
14693 else
14694 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14695 }
14696
14697 /* Can the type given by DIE define another type? */
14698
14699 static bool
14700 type_can_define_types (const struct die_info *die)
14701 {
14702 switch (die->tag)
14703 {
14704 case DW_TAG_typedef:
14705 case DW_TAG_class_type:
14706 case DW_TAG_structure_type:
14707 case DW_TAG_union_type:
14708 case DW_TAG_enumeration_type:
14709 return true;
14710
14711 default:
14712 return false;
14713 }
14714 }
14715
14716 /* Add a type definition defined in the scope of the FIP's class. */
14717
14718 static void
14719 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14720 struct dwarf2_cu *cu)
14721 {
14722 struct decl_field fp;
14723 memset (&fp, 0, sizeof (fp));
14724
14725 gdb_assert (type_can_define_types (die));
14726
14727 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14728 fp.name = dwarf2_name (die, cu);
14729 fp.type = read_type_die (die, cu);
14730
14731 /* Save accessibility. */
14732 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14733 switch (accessibility)
14734 {
14735 case DW_ACCESS_public:
14736 /* The assumed value if neither private nor protected. */
14737 break;
14738 case DW_ACCESS_private:
14739 fp.is_private = 1;
14740 break;
14741 case DW_ACCESS_protected:
14742 fp.is_protected = 1;
14743 break;
14744 }
14745
14746 if (die->tag == DW_TAG_typedef)
14747 fip->typedef_field_list.push_back (fp);
14748 else
14749 fip->nested_types_list.push_back (fp);
14750 }
14751
14752 /* A convenience typedef that's used when finding the discriminant
14753 field for a variant part. */
14754 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14755 offset_map_type;
14756
14757 /* Compute the discriminant range for a given variant. OBSTACK is
14758 where the results will be stored. VARIANT is the variant to
14759 process. IS_UNSIGNED indicates whether the discriminant is signed
14760 or unsigned. */
14761
14762 static const gdb::array_view<discriminant_range>
14763 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14764 bool is_unsigned)
14765 {
14766 std::vector<discriminant_range> ranges;
14767
14768 if (variant.default_branch)
14769 return {};
14770
14771 if (variant.discr_list_data == nullptr)
14772 {
14773 discriminant_range r
14774 = {variant.discriminant_value, variant.discriminant_value};
14775 ranges.push_back (r);
14776 }
14777 else
14778 {
14779 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14780 variant.discr_list_data->size);
14781 while (!data.empty ())
14782 {
14783 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14784 {
14785 complaint (_("invalid discriminant marker: %d"), data[0]);
14786 break;
14787 }
14788 bool is_range = data[0] == DW_DSC_range;
14789 data = data.slice (1);
14790
14791 ULONGEST low, high;
14792 unsigned int bytes_read;
14793
14794 if (data.empty ())
14795 {
14796 complaint (_("DW_AT_discr_list missing low value"));
14797 break;
14798 }
14799 if (is_unsigned)
14800 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14801 else
14802 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14803 &bytes_read);
14804 data = data.slice (bytes_read);
14805
14806 if (is_range)
14807 {
14808 if (data.empty ())
14809 {
14810 complaint (_("DW_AT_discr_list missing high value"));
14811 break;
14812 }
14813 if (is_unsigned)
14814 high = read_unsigned_leb128 (nullptr, data.data (),
14815 &bytes_read);
14816 else
14817 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14818 &bytes_read);
14819 data = data.slice (bytes_read);
14820 }
14821 else
14822 high = low;
14823
14824 ranges.push_back ({ low, high });
14825 }
14826 }
14827
14828 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14829 ranges.size ());
14830 std::copy (ranges.begin (), ranges.end (), result);
14831 return gdb::array_view<discriminant_range> (result, ranges.size ());
14832 }
14833
14834 static const gdb::array_view<variant_part> create_variant_parts
14835 (struct obstack *obstack,
14836 const offset_map_type &offset_map,
14837 struct field_info *fi,
14838 const std::vector<variant_part_builder> &variant_parts);
14839
14840 /* Fill in a "struct variant" for a given variant field. RESULT is
14841 the variant to fill in. OBSTACK is where any needed allocations
14842 will be done. OFFSET_MAP holds the mapping from section offsets to
14843 fields for the type. FI describes the fields of the type we're
14844 processing. FIELD is the variant field we're converting. */
14845
14846 static void
14847 create_one_variant (variant &result, struct obstack *obstack,
14848 const offset_map_type &offset_map,
14849 struct field_info *fi, const variant_field &field)
14850 {
14851 result.discriminants = convert_variant_range (obstack, field, false);
14852 result.first_field = field.first_field + fi->baseclasses.size ();
14853 result.last_field = field.last_field + fi->baseclasses.size ();
14854 result.parts = create_variant_parts (obstack, offset_map, fi,
14855 field.variant_parts);
14856 }
14857
14858 /* Fill in a "struct variant_part" for a given variant part. RESULT
14859 is the variant part to fill in. OBSTACK is where any needed
14860 allocations will be done. OFFSET_MAP holds the mapping from
14861 section offsets to fields for the type. FI describes the fields of
14862 the type we're processing. BUILDER is the variant part to be
14863 converted. */
14864
14865 static void
14866 create_one_variant_part (variant_part &result,
14867 struct obstack *obstack,
14868 const offset_map_type &offset_map,
14869 struct field_info *fi,
14870 const variant_part_builder &builder)
14871 {
14872 auto iter = offset_map.find (builder.discriminant_offset);
14873 if (iter == offset_map.end ())
14874 {
14875 result.discriminant_index = -1;
14876 /* Doesn't matter. */
14877 result.is_unsigned = false;
14878 }
14879 else
14880 {
14881 result.discriminant_index = iter->second;
14882 result.is_unsigned
14883 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14884 }
14885
14886 size_t n = builder.variants.size ();
14887 variant *output = new (obstack) variant[n];
14888 for (size_t i = 0; i < n; ++i)
14889 create_one_variant (output[i], obstack, offset_map, fi,
14890 builder.variants[i]);
14891
14892 result.variants = gdb::array_view<variant> (output, n);
14893 }
14894
14895 /* Create a vector of variant parts that can be attached to a type.
14896 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14897 holds the mapping from section offsets to fields for the type. FI
14898 describes the fields of the type we're processing. VARIANT_PARTS
14899 is the vector to convert. */
14900
14901 static const gdb::array_view<variant_part>
14902 create_variant_parts (struct obstack *obstack,
14903 const offset_map_type &offset_map,
14904 struct field_info *fi,
14905 const std::vector<variant_part_builder> &variant_parts)
14906 {
14907 if (variant_parts.empty ())
14908 return {};
14909
14910 size_t n = variant_parts.size ();
14911 variant_part *result = new (obstack) variant_part[n];
14912 for (size_t i = 0; i < n; ++i)
14913 create_one_variant_part (result[i], obstack, offset_map, fi,
14914 variant_parts[i]);
14915
14916 return gdb::array_view<variant_part> (result, n);
14917 }
14918
14919 /* Compute the variant part vector for FIP, attaching it to TYPE when
14920 done. */
14921
14922 static void
14923 add_variant_property (struct field_info *fip, struct type *type,
14924 struct dwarf2_cu *cu)
14925 {
14926 /* Map section offsets of fields to their field index. Note the
14927 field index here does not take the number of baseclasses into
14928 account. */
14929 offset_map_type offset_map;
14930 for (int i = 0; i < fip->fields.size (); ++i)
14931 offset_map[fip->fields[i].offset] = i;
14932
14933 struct objfile *objfile = cu->per_objfile->objfile;
14934 gdb::array_view<variant_part> parts
14935 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14936 fip->variant_parts);
14937
14938 struct dynamic_prop prop;
14939 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14940 obstack_copy (&objfile->objfile_obstack, &parts,
14941 sizeof (parts)));
14942
14943 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14944 }
14945
14946 /* Create the vector of fields, and attach it to the type. */
14947
14948 static void
14949 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14950 struct dwarf2_cu *cu)
14951 {
14952 int nfields = fip->nfields ();
14953
14954 /* Record the field count, allocate space for the array of fields,
14955 and create blank accessibility bitfields if necessary. */
14956 type->set_num_fields (nfields);
14957 type->set_fields
14958 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14959
14960 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
14961 {
14962 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14963
14964 TYPE_FIELD_PRIVATE_BITS (type) =
14965 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14966 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14967
14968 TYPE_FIELD_PROTECTED_BITS (type) =
14969 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14970 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14971
14972 TYPE_FIELD_IGNORE_BITS (type) =
14973 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14974 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14975 }
14976
14977 /* If the type has baseclasses, allocate and clear a bit vector for
14978 TYPE_FIELD_VIRTUAL_BITS. */
14979 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
14980 {
14981 int num_bytes = B_BYTES (fip->baseclasses.size ());
14982 unsigned char *pointer;
14983
14984 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14985 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14986 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14987 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14988 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14989 }
14990
14991 if (!fip->variant_parts.empty ())
14992 add_variant_property (fip, type, cu);
14993
14994 /* Copy the saved-up fields into the field vector. */
14995 for (int i = 0; i < nfields; ++i)
14996 {
14997 struct nextfield &field
14998 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14999 : fip->fields[i - fip->baseclasses.size ()]);
15000
15001 type->field (i) = field.field;
15002 switch (field.accessibility)
15003 {
15004 case DW_ACCESS_private:
15005 if (cu->per_cu->lang != language_ada)
15006 SET_TYPE_FIELD_PRIVATE (type, i);
15007 break;
15008
15009 case DW_ACCESS_protected:
15010 if (cu->per_cu->lang != language_ada)
15011 SET_TYPE_FIELD_PROTECTED (type, i);
15012 break;
15013
15014 case DW_ACCESS_public:
15015 break;
15016
15017 default:
15018 /* Unknown accessibility. Complain and treat it as public. */
15019 {
15020 complaint (_("unsupported accessibility %d"),
15021 field.accessibility);
15022 }
15023 break;
15024 }
15025 if (i < fip->baseclasses.size ())
15026 {
15027 switch (field.virtuality)
15028 {
15029 case DW_VIRTUALITY_virtual:
15030 case DW_VIRTUALITY_pure_virtual:
15031 if (cu->per_cu->lang == language_ada)
15032 error (_("unexpected virtuality in component of Ada type"));
15033 SET_TYPE_FIELD_VIRTUAL (type, i);
15034 break;
15035 }
15036 }
15037 }
15038 }
15039
15040 /* Return true if this member function is a constructor, false
15041 otherwise. */
15042
15043 static int
15044 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15045 {
15046 const char *fieldname;
15047 const char *type_name;
15048 int len;
15049
15050 if (die->parent == NULL)
15051 return 0;
15052
15053 if (die->parent->tag != DW_TAG_structure_type
15054 && die->parent->tag != DW_TAG_union_type
15055 && die->parent->tag != DW_TAG_class_type)
15056 return 0;
15057
15058 fieldname = dwarf2_name (die, cu);
15059 type_name = dwarf2_name (die->parent, cu);
15060 if (fieldname == NULL || type_name == NULL)
15061 return 0;
15062
15063 len = strlen (fieldname);
15064 return (strncmp (fieldname, type_name, len) == 0
15065 && (type_name[len] == '\0' || type_name[len] == '<'));
15066 }
15067
15068 /* Add a member function to the proper fieldlist. */
15069
15070 static void
15071 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15072 struct type *type, struct dwarf2_cu *cu)
15073 {
15074 struct objfile *objfile = cu->per_objfile->objfile;
15075 struct attribute *attr;
15076 int i;
15077 struct fnfieldlist *flp = nullptr;
15078 struct fn_field *fnp;
15079 const char *fieldname;
15080 struct type *this_type;
15081
15082 if (cu->per_cu->lang == language_ada)
15083 error (_("unexpected member function in Ada type"));
15084
15085 /* Get name of member function. */
15086 fieldname = dwarf2_name (die, cu);
15087 if (fieldname == NULL)
15088 return;
15089
15090 /* Look up member function name in fieldlist. */
15091 for (i = 0; i < fip->fnfieldlists.size (); i++)
15092 {
15093 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15094 {
15095 flp = &fip->fnfieldlists[i];
15096 break;
15097 }
15098 }
15099
15100 /* Create a new fnfieldlist if necessary. */
15101 if (flp == nullptr)
15102 {
15103 fip->fnfieldlists.emplace_back ();
15104 flp = &fip->fnfieldlists.back ();
15105 flp->name = fieldname;
15106 i = fip->fnfieldlists.size () - 1;
15107 }
15108
15109 /* Create a new member function field and add it to the vector of
15110 fnfieldlists. */
15111 flp->fnfields.emplace_back ();
15112 fnp = &flp->fnfields.back ();
15113
15114 /* Delay processing of the physname until later. */
15115 if (cu->per_cu->lang == language_cplus)
15116 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15117 die, cu);
15118 else
15119 {
15120 const char *physname = dwarf2_physname (fieldname, die, cu);
15121 fnp->physname = physname ? physname : "";
15122 }
15123
15124 fnp->type = alloc_type (objfile);
15125 this_type = read_type_die (die, cu);
15126 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15127 {
15128 int nparams = this_type->num_fields ();
15129
15130 /* TYPE is the domain of this method, and THIS_TYPE is the type
15131 of the method itself (TYPE_CODE_METHOD). */
15132 smash_to_method_type (fnp->type, type,
15133 TYPE_TARGET_TYPE (this_type),
15134 this_type->fields (),
15135 this_type->num_fields (),
15136 this_type->has_varargs ());
15137
15138 /* Handle static member functions.
15139 Dwarf2 has no clean way to discern C++ static and non-static
15140 member functions. G++ helps GDB by marking the first
15141 parameter for non-static member functions (which is the this
15142 pointer) as artificial. We obtain this information from
15143 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15144 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15145 fnp->voffset = VOFFSET_STATIC;
15146 }
15147 else
15148 complaint (_("member function type missing for '%s'"),
15149 dwarf2_full_name (fieldname, die, cu));
15150
15151 /* Get fcontext from DW_AT_containing_type if present. */
15152 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15153 fnp->fcontext = die_containing_type (die, cu);
15154
15155 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15156 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15157
15158 /* Get accessibility. */
15159 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15160 switch (accessibility)
15161 {
15162 case DW_ACCESS_private:
15163 fnp->is_private = 1;
15164 break;
15165 case DW_ACCESS_protected:
15166 fnp->is_protected = 1;
15167 break;
15168 }
15169
15170 /* Check for artificial methods. */
15171 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15172 if (attr && attr->as_boolean ())
15173 fnp->is_artificial = 1;
15174
15175 /* Check for defaulted methods. */
15176 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15177 if (attr != nullptr)
15178 fnp->defaulted = attr->defaulted ();
15179
15180 /* Check for deleted methods. */
15181 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15182 if (attr != nullptr && attr->as_boolean ())
15183 fnp->is_deleted = 1;
15184
15185 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15186
15187 /* Get index in virtual function table if it is a virtual member
15188 function. For older versions of GCC, this is an offset in the
15189 appropriate virtual table, as specified by DW_AT_containing_type.
15190 For everyone else, it is an expression to be evaluated relative
15191 to the object address. */
15192
15193 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15194 if (attr != nullptr)
15195 {
15196 if (attr->form_is_block () && attr->as_block ()->size > 0)
15197 {
15198 struct dwarf_block *block = attr->as_block ();
15199
15200 if (block->data[0] == DW_OP_constu)
15201 {
15202 /* Old-style GCC. */
15203 fnp->voffset = decode_locdesc (block, cu) + 2;
15204 }
15205 else if (block->data[0] == DW_OP_deref
15206 || (block->size > 1
15207 && block->data[0] == DW_OP_deref_size
15208 && block->data[1] == cu->header.addr_size))
15209 {
15210 fnp->voffset = decode_locdesc (block, cu);
15211 if ((fnp->voffset % cu->header.addr_size) != 0)
15212 dwarf2_complex_location_expr_complaint ();
15213 else
15214 fnp->voffset /= cu->header.addr_size;
15215 fnp->voffset += 2;
15216 }
15217 else
15218 dwarf2_complex_location_expr_complaint ();
15219
15220 if (!fnp->fcontext)
15221 {
15222 /* If there is no `this' field and no DW_AT_containing_type,
15223 we cannot actually find a base class context for the
15224 vtable! */
15225 if (this_type->num_fields () == 0
15226 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15227 {
15228 complaint (_("cannot determine context for virtual member "
15229 "function \"%s\" (offset %s)"),
15230 fieldname, sect_offset_str (die->sect_off));
15231 }
15232 else
15233 {
15234 fnp->fcontext
15235 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15236 }
15237 }
15238 }
15239 else if (attr->form_is_section_offset ())
15240 {
15241 dwarf2_complex_location_expr_complaint ();
15242 }
15243 else
15244 {
15245 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15246 fieldname);
15247 }
15248 }
15249 else
15250 {
15251 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15252 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15253 {
15254 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15255 complaint (_("Member function \"%s\" (offset %s) is virtual "
15256 "but the vtable offset is not specified"),
15257 fieldname, sect_offset_str (die->sect_off));
15258 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15259 TYPE_CPLUS_DYNAMIC (type) = 1;
15260 }
15261 }
15262 }
15263
15264 /* Create the vector of member function fields, and attach it to the type. */
15265
15266 static void
15267 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15268 struct dwarf2_cu *cu)
15269 {
15270 if (cu->per_cu->lang == language_ada)
15271 error (_("unexpected member functions in Ada type"));
15272
15273 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15274 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15275 TYPE_ALLOC (type,
15276 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15277
15278 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15279 {
15280 struct fnfieldlist &nf = fip->fnfieldlists[i];
15281 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15282
15283 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15284 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15285 fn_flp->fn_fields = (struct fn_field *)
15286 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15287
15288 for (int k = 0; k < nf.fnfields.size (); ++k)
15289 fn_flp->fn_fields[k] = nf.fnfields[k];
15290 }
15291
15292 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15293 }
15294
15295 /* Returns non-zero if NAME is the name of a vtable member in CU's
15296 language, zero otherwise. */
15297 static int
15298 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15299 {
15300 static const char vptr[] = "_vptr";
15301
15302 /* Look for the C++ form of the vtable. */
15303 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15304 return 1;
15305
15306 return 0;
15307 }
15308
15309 /* GCC outputs unnamed structures that are really pointers to member
15310 functions, with the ABI-specified layout. If TYPE describes
15311 such a structure, smash it into a member function type.
15312
15313 GCC shouldn't do this; it should just output pointer to member DIEs.
15314 This is GCC PR debug/28767. */
15315
15316 static void
15317 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15318 {
15319 struct type *pfn_type, *self_type, *new_type;
15320
15321 /* Check for a structure with no name and two children. */
15322 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15323 return;
15324
15325 /* Check for __pfn and __delta members. */
15326 if (TYPE_FIELD_NAME (type, 0) == NULL
15327 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15328 || TYPE_FIELD_NAME (type, 1) == NULL
15329 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15330 return;
15331
15332 /* Find the type of the method. */
15333 pfn_type = type->field (0).type ();
15334 if (pfn_type == NULL
15335 || pfn_type->code () != TYPE_CODE_PTR
15336 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15337 return;
15338
15339 /* Look for the "this" argument. */
15340 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15341 if (pfn_type->num_fields () == 0
15342 /* || pfn_type->field (0).type () == NULL */
15343 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15344 return;
15345
15346 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15347 new_type = alloc_type (objfile);
15348 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15349 pfn_type->fields (), pfn_type->num_fields (),
15350 pfn_type->has_varargs ());
15351 smash_to_methodptr_type (type, new_type);
15352 }
15353
15354 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15355 requires rewriting, then copy it and return the updated copy.
15356 Otherwise return nullptr. */
15357
15358 static struct type *
15359 rewrite_array_type (struct type *type)
15360 {
15361 if (type->code () != TYPE_CODE_ARRAY)
15362 return nullptr;
15363
15364 struct type *index_type = type->index_type ();
15365 range_bounds *current_bounds = index_type->bounds ();
15366
15367 /* Handle multi-dimensional arrays. */
15368 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15369 if (new_target == nullptr)
15370 {
15371 /* Maybe we don't need to rewrite this array. */
15372 if (current_bounds->low.kind () == PROP_CONST
15373 && current_bounds->high.kind () == PROP_CONST)
15374 return nullptr;
15375 }
15376
15377 /* Either the target type was rewritten, or the bounds have to be
15378 updated. Either way we want to copy the type and update
15379 everything. */
15380 struct type *copy = copy_type (type);
15381 int nfields = copy->num_fields ();
15382 field *new_fields
15383 = ((struct field *) TYPE_ZALLOC (copy,
15384 nfields * sizeof (struct field)));
15385 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15386 copy->set_fields (new_fields);
15387 if (new_target != nullptr)
15388 TYPE_TARGET_TYPE (copy) = new_target;
15389
15390 struct type *index_copy = copy_type (index_type);
15391 range_bounds *bounds
15392 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15393 sizeof (range_bounds));
15394 *bounds = *current_bounds;
15395 bounds->low.set_const_val (1);
15396 bounds->high.set_const_val (0);
15397 index_copy->set_bounds (bounds);
15398 copy->set_index_type (index_copy);
15399
15400 return copy;
15401 }
15402
15403 /* While some versions of GCC will generate complicated DWARF for an
15404 array (see quirk_ada_thick_pointer), more recent versions were
15405 modified to emit an explicit thick pointer structure. However, in
15406 this case, the array still has DWARF expressions for its ranges,
15407 and these must be ignored. */
15408
15409 static void
15410 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15411 struct type *type)
15412 {
15413 gdb_assert (cu->per_cu->lang == language_ada);
15414
15415 /* Check for a structure with two children. */
15416 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15417 return;
15418
15419 /* Check for P_ARRAY and P_BOUNDS members. */
15420 if (TYPE_FIELD_NAME (type, 0) == NULL
15421 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15422 || TYPE_FIELD_NAME (type, 1) == NULL
15423 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15424 return;
15425
15426 /* Make sure we're looking at a pointer to an array. */
15427 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15428 return;
15429
15430 /* The Ada code already knows how to handle these types, so all that
15431 we need to do is turn the bounds into static bounds. However, we
15432 don't want to rewrite existing array or index types in-place,
15433 because those may be referenced in other contexts where this
15434 rewriting is undesirable. */
15435 struct type *new_ary_type
15436 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15437 if (new_ary_type != nullptr)
15438 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15439 }
15440
15441 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15442 appropriate error checking and issuing complaints if there is a
15443 problem. */
15444
15445 static ULONGEST
15446 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15447 {
15448 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15449
15450 if (attr == nullptr)
15451 return 0;
15452
15453 if (!attr->form_is_constant ())
15454 {
15455 complaint (_("DW_AT_alignment must have constant form"
15456 " - DIE at %s [in module %s]"),
15457 sect_offset_str (die->sect_off),
15458 objfile_name (cu->per_objfile->objfile));
15459 return 0;
15460 }
15461
15462 LONGEST val = attr->constant_value (0);
15463 if (val < 0)
15464 {
15465 complaint (_("DW_AT_alignment value must not be negative"
15466 " - DIE at %s [in module %s]"),
15467 sect_offset_str (die->sect_off),
15468 objfile_name (cu->per_objfile->objfile));
15469 return 0;
15470 }
15471 ULONGEST align = val;
15472
15473 if (align == 0)
15474 {
15475 complaint (_("DW_AT_alignment value must not be zero"
15476 " - DIE at %s [in module %s]"),
15477 sect_offset_str (die->sect_off),
15478 objfile_name (cu->per_objfile->objfile));
15479 return 0;
15480 }
15481 if ((align & (align - 1)) != 0)
15482 {
15483 complaint (_("DW_AT_alignment value must be a power of 2"
15484 " - DIE at %s [in module %s]"),
15485 sect_offset_str (die->sect_off),
15486 objfile_name (cu->per_objfile->objfile));
15487 return 0;
15488 }
15489
15490 return align;
15491 }
15492
15493 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15494 the alignment for TYPE. */
15495
15496 static void
15497 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15498 struct type *type)
15499 {
15500 if (!set_type_align (type, get_alignment (cu, die)))
15501 complaint (_("DW_AT_alignment value too large"
15502 " - DIE at %s [in module %s]"),
15503 sect_offset_str (die->sect_off),
15504 objfile_name (cu->per_objfile->objfile));
15505 }
15506
15507 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15508 constant for a type, according to DWARF5 spec, Table 5.5. */
15509
15510 static bool
15511 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15512 {
15513 switch (value)
15514 {
15515 case DW_CC_normal:
15516 case DW_CC_pass_by_reference:
15517 case DW_CC_pass_by_value:
15518 return true;
15519
15520 default:
15521 complaint (_("unrecognized DW_AT_calling_convention value "
15522 "(%s) for a type"), pulongest (value));
15523 return false;
15524 }
15525 }
15526
15527 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15528 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15529 also according to GNU-specific values (see include/dwarf2.h). */
15530
15531 static bool
15532 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15533 {
15534 switch (value)
15535 {
15536 case DW_CC_normal:
15537 case DW_CC_program:
15538 case DW_CC_nocall:
15539 return true;
15540
15541 case DW_CC_GNU_renesas_sh:
15542 case DW_CC_GNU_borland_fastcall_i386:
15543 case DW_CC_GDB_IBM_OpenCL:
15544 return true;
15545
15546 default:
15547 complaint (_("unrecognized DW_AT_calling_convention value "
15548 "(%s) for a subroutine"), pulongest (value));
15549 return false;
15550 }
15551 }
15552
15553 /* Called when we find the DIE that starts a structure or union scope
15554 (definition) to create a type for the structure or union. Fill in
15555 the type's name and general properties; the members will not be
15556 processed until process_structure_scope. A symbol table entry for
15557 the type will also not be done until process_structure_scope (assuming
15558 the type has a name).
15559
15560 NOTE: we need to call these functions regardless of whether or not the
15561 DIE has a DW_AT_name attribute, since it might be an anonymous
15562 structure or union. This gets the type entered into our set of
15563 user defined types. */
15564
15565 static struct type *
15566 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15567 {
15568 struct objfile *objfile = cu->per_objfile->objfile;
15569 struct type *type;
15570 struct attribute *attr;
15571 const char *name;
15572
15573 /* If the definition of this type lives in .debug_types, read that type.
15574 Don't follow DW_AT_specification though, that will take us back up
15575 the chain and we want to go down. */
15576 attr = die->attr (DW_AT_signature);
15577 if (attr != nullptr)
15578 {
15579 type = get_DW_AT_signature_type (die, attr, cu);
15580
15581 /* The type's CU may not be the same as CU.
15582 Ensure TYPE is recorded with CU in die_type_hash. */
15583 return set_die_type (die, type, cu);
15584 }
15585
15586 type = alloc_type (objfile);
15587 INIT_CPLUS_SPECIFIC (type);
15588
15589 name = dwarf2_name (die, cu);
15590 if (name != NULL)
15591 {
15592 if (cu->per_cu->lang == language_cplus
15593 || cu->per_cu->lang == language_d
15594 || cu->per_cu->lang == language_rust)
15595 {
15596 const char *full_name = dwarf2_full_name (name, die, cu);
15597
15598 /* dwarf2_full_name might have already finished building the DIE's
15599 type. If so, there is no need to continue. */
15600 if (get_die_type (die, cu) != NULL)
15601 return get_die_type (die, cu);
15602
15603 type->set_name (full_name);
15604 }
15605 else
15606 {
15607 /* The name is already allocated along with this objfile, so
15608 we don't need to duplicate it for the type. */
15609 type->set_name (name);
15610 }
15611 }
15612
15613 if (die->tag == DW_TAG_structure_type)
15614 {
15615 type->set_code (TYPE_CODE_STRUCT);
15616 }
15617 else if (die->tag == DW_TAG_union_type)
15618 {
15619 type->set_code (TYPE_CODE_UNION);
15620 }
15621 else
15622 {
15623 type->set_code (TYPE_CODE_STRUCT);
15624 }
15625
15626 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
15627 type->set_is_declared_class (true);
15628
15629 /* Store the calling convention in the type if it's available in
15630 the die. Otherwise the calling convention remains set to
15631 the default value DW_CC_normal. */
15632 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15633 if (attr != nullptr
15634 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15635 {
15636 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15637 TYPE_CPLUS_CALLING_CONVENTION (type)
15638 = (enum dwarf_calling_convention) (attr->constant_value (0));
15639 }
15640
15641 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15642 if (attr != nullptr)
15643 {
15644 if (attr->form_is_constant ())
15645 TYPE_LENGTH (type) = attr->constant_value (0);
15646 else
15647 {
15648 struct dynamic_prop prop;
15649 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15650 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15651 TYPE_LENGTH (type) = 0;
15652 }
15653 }
15654 else
15655 {
15656 TYPE_LENGTH (type) = 0;
15657 }
15658
15659 maybe_set_alignment (cu, die, type);
15660
15661 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15662 {
15663 /* ICC<14 does not output the required DW_AT_declaration on
15664 incomplete types, but gives them a size of zero. */
15665 type->set_is_stub (true);
15666 }
15667 else
15668 type->set_stub_is_supported (true);
15669
15670 if (die_is_declaration (die, cu))
15671 type->set_is_stub (true);
15672 else if (attr == NULL && die->child == NULL
15673 && producer_is_realview (cu->producer))
15674 /* RealView does not output the required DW_AT_declaration
15675 on incomplete types. */
15676 type->set_is_stub (true);
15677
15678 /* We need to add the type field to the die immediately so we don't
15679 infinitely recurse when dealing with pointers to the structure
15680 type within the structure itself. */
15681 set_die_type (die, type, cu);
15682
15683 /* set_die_type should be already done. */
15684 set_descriptive_type (type, die, cu);
15685
15686 return type;
15687 }
15688
15689 static void handle_struct_member_die
15690 (struct die_info *child_die,
15691 struct type *type,
15692 struct field_info *fi,
15693 std::vector<struct symbol *> *template_args,
15694 struct dwarf2_cu *cu);
15695
15696 /* A helper for handle_struct_member_die that handles
15697 DW_TAG_variant_part. */
15698
15699 static void
15700 handle_variant_part (struct die_info *die, struct type *type,
15701 struct field_info *fi,
15702 std::vector<struct symbol *> *template_args,
15703 struct dwarf2_cu *cu)
15704 {
15705 variant_part_builder *new_part;
15706 if (fi->current_variant_part == nullptr)
15707 {
15708 fi->variant_parts.emplace_back ();
15709 new_part = &fi->variant_parts.back ();
15710 }
15711 else if (!fi->current_variant_part->processing_variant)
15712 {
15713 complaint (_("nested DW_TAG_variant_part seen "
15714 "- DIE at %s [in module %s]"),
15715 sect_offset_str (die->sect_off),
15716 objfile_name (cu->per_objfile->objfile));
15717 return;
15718 }
15719 else
15720 {
15721 variant_field &current = fi->current_variant_part->variants.back ();
15722 current.variant_parts.emplace_back ();
15723 new_part = &current.variant_parts.back ();
15724 }
15725
15726 /* When we recurse, we want callees to add to this new variant
15727 part. */
15728 scoped_restore save_current_variant_part
15729 = make_scoped_restore (&fi->current_variant_part, new_part);
15730
15731 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15732 if (discr == NULL)
15733 {
15734 /* It's a univariant form, an extension we support. */
15735 }
15736 else if (discr->form_is_ref ())
15737 {
15738 struct dwarf2_cu *target_cu = cu;
15739 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15740
15741 new_part->discriminant_offset = target_die->sect_off;
15742 }
15743 else
15744 {
15745 complaint (_("DW_AT_discr does not have DIE reference form"
15746 " - DIE at %s [in module %s]"),
15747 sect_offset_str (die->sect_off),
15748 objfile_name (cu->per_objfile->objfile));
15749 }
15750
15751 for (die_info *child_die = die->child;
15752 child_die != NULL;
15753 child_die = child_die->sibling)
15754 handle_struct_member_die (child_die, type, fi, template_args, cu);
15755 }
15756
15757 /* A helper for handle_struct_member_die that handles
15758 DW_TAG_variant. */
15759
15760 static void
15761 handle_variant (struct die_info *die, struct type *type,
15762 struct field_info *fi,
15763 std::vector<struct symbol *> *template_args,
15764 struct dwarf2_cu *cu)
15765 {
15766 if (fi->current_variant_part == nullptr)
15767 {
15768 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15769 "- DIE at %s [in module %s]"),
15770 sect_offset_str (die->sect_off),
15771 objfile_name (cu->per_objfile->objfile));
15772 return;
15773 }
15774 if (fi->current_variant_part->processing_variant)
15775 {
15776 complaint (_("nested DW_TAG_variant seen "
15777 "- DIE at %s [in module %s]"),
15778 sect_offset_str (die->sect_off),
15779 objfile_name (cu->per_objfile->objfile));
15780 return;
15781 }
15782
15783 scoped_restore save_processing_variant
15784 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15785 true);
15786
15787 fi->current_variant_part->variants.emplace_back ();
15788 variant_field &variant = fi->current_variant_part->variants.back ();
15789 variant.first_field = fi->fields.size ();
15790
15791 /* In a variant we want to get the discriminant and also add a
15792 field for our sole member child. */
15793 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15794 if (discr == nullptr || !discr->form_is_constant ())
15795 {
15796 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15797 if (discr == nullptr || discr->as_block ()->size == 0)
15798 variant.default_branch = true;
15799 else
15800 variant.discr_list_data = discr->as_block ();
15801 }
15802 else
15803 variant.discriminant_value = discr->constant_value (0);
15804
15805 for (die_info *variant_child = die->child;
15806 variant_child != NULL;
15807 variant_child = variant_child->sibling)
15808 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15809
15810 variant.last_field = fi->fields.size ();
15811 }
15812
15813 /* A helper for process_structure_scope that handles a single member
15814 DIE. */
15815
15816 static void
15817 handle_struct_member_die (struct die_info *child_die, struct type *type,
15818 struct field_info *fi,
15819 std::vector<struct symbol *> *template_args,
15820 struct dwarf2_cu *cu)
15821 {
15822 if (child_die->tag == DW_TAG_member
15823 || child_die->tag == DW_TAG_variable)
15824 {
15825 /* NOTE: carlton/2002-11-05: A C++ static data member
15826 should be a DW_TAG_member that is a declaration, but
15827 all versions of G++ as of this writing (so through at
15828 least 3.2.1) incorrectly generate DW_TAG_variable
15829 tags for them instead. */
15830 dwarf2_add_field (fi, child_die, cu);
15831 }
15832 else if (child_die->tag == DW_TAG_subprogram)
15833 {
15834 /* Rust doesn't have member functions in the C++ sense.
15835 However, it does emit ordinary functions as children
15836 of a struct DIE. */
15837 if (cu->per_cu->lang == language_rust)
15838 read_func_scope (child_die, cu);
15839 else
15840 {
15841 /* C++ member function. */
15842 dwarf2_add_member_fn (fi, child_die, type, cu);
15843 }
15844 }
15845 else if (child_die->tag == DW_TAG_inheritance)
15846 {
15847 /* C++ base class field. */
15848 dwarf2_add_field (fi, child_die, cu);
15849 }
15850 else if (type_can_define_types (child_die))
15851 dwarf2_add_type_defn (fi, child_die, cu);
15852 else if (child_die->tag == DW_TAG_template_type_param
15853 || child_die->tag == DW_TAG_template_value_param)
15854 {
15855 struct symbol *arg = new_symbol (child_die, NULL, cu);
15856
15857 if (arg != NULL)
15858 template_args->push_back (arg);
15859 }
15860 else if (child_die->tag == DW_TAG_variant_part)
15861 handle_variant_part (child_die, type, fi, template_args, cu);
15862 else if (child_die->tag == DW_TAG_variant)
15863 handle_variant (child_die, type, fi, template_args, cu);
15864 }
15865
15866 /* Finish creating a structure or union type, including filling in
15867 its members and creating a symbol for it. */
15868
15869 static void
15870 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15871 {
15872 struct objfile *objfile = cu->per_objfile->objfile;
15873 struct die_info *child_die;
15874 struct type *type;
15875
15876 type = get_die_type (die, cu);
15877 if (type == NULL)
15878 type = read_structure_type (die, cu);
15879
15880 bool has_template_parameters = false;
15881 if (die->child != NULL && ! die_is_declaration (die, cu))
15882 {
15883 struct field_info fi;
15884 std::vector<struct symbol *> template_args;
15885
15886 child_die = die->child;
15887
15888 while (child_die && child_die->tag)
15889 {
15890 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15891 child_die = child_die->sibling;
15892 }
15893
15894 /* Attach template arguments to type. */
15895 if (!template_args.empty ())
15896 {
15897 has_template_parameters = true;
15898 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15899 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15900 TYPE_TEMPLATE_ARGUMENTS (type)
15901 = XOBNEWVEC (&objfile->objfile_obstack,
15902 struct symbol *,
15903 TYPE_N_TEMPLATE_ARGUMENTS (type));
15904 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15905 template_args.data (),
15906 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15907 * sizeof (struct symbol *)));
15908 }
15909
15910 /* Attach fields and member functions to the type. */
15911 if (fi.nfields () > 0)
15912 dwarf2_attach_fields_to_type (&fi, type, cu);
15913 if (!fi.fnfieldlists.empty ())
15914 {
15915 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15916
15917 /* Get the type which refers to the base class (possibly this
15918 class itself) which contains the vtable pointer for the current
15919 class from the DW_AT_containing_type attribute. This use of
15920 DW_AT_containing_type is a GNU extension. */
15921
15922 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15923 {
15924 struct type *t = die_containing_type (die, cu);
15925
15926 set_type_vptr_basetype (type, t);
15927 if (type == t)
15928 {
15929 int i;
15930
15931 /* Our own class provides vtbl ptr. */
15932 for (i = t->num_fields () - 1;
15933 i >= TYPE_N_BASECLASSES (t);
15934 --i)
15935 {
15936 const char *fieldname = TYPE_FIELD_NAME (t, i);
15937
15938 if (is_vtable_name (fieldname, cu))
15939 {
15940 set_type_vptr_fieldno (type, i);
15941 break;
15942 }
15943 }
15944
15945 /* Complain if virtual function table field not found. */
15946 if (i < TYPE_N_BASECLASSES (t))
15947 complaint (_("virtual function table pointer "
15948 "not found when defining class '%s'"),
15949 type->name () ? type->name () : "");
15950 }
15951 else
15952 {
15953 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15954 }
15955 }
15956 else if (cu->producer
15957 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15958 {
15959 /* The IBM XLC compiler does not provide direct indication
15960 of the containing type, but the vtable pointer is
15961 always named __vfp. */
15962
15963 int i;
15964
15965 for (i = type->num_fields () - 1;
15966 i >= TYPE_N_BASECLASSES (type);
15967 --i)
15968 {
15969 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15970 {
15971 set_type_vptr_fieldno (type, i);
15972 set_type_vptr_basetype (type, type);
15973 break;
15974 }
15975 }
15976 }
15977 }
15978
15979 /* Copy fi.typedef_field_list linked list elements content into the
15980 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15981 if (!fi.typedef_field_list.empty ())
15982 {
15983 int count = fi.typedef_field_list.size ();
15984
15985 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15986 TYPE_TYPEDEF_FIELD_ARRAY (type)
15987 = ((struct decl_field *)
15988 TYPE_ALLOC (type,
15989 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15990 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15991
15992 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15993 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15994 }
15995
15996 /* Copy fi.nested_types_list linked list elements content into the
15997 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15998 if (!fi.nested_types_list.empty ()
15999 && cu->per_cu->lang != language_ada)
16000 {
16001 int count = fi.nested_types_list.size ();
16002
16003 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16004 TYPE_NESTED_TYPES_ARRAY (type)
16005 = ((struct decl_field *)
16006 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16007 TYPE_NESTED_TYPES_COUNT (type) = count;
16008
16009 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16010 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16011 }
16012 }
16013
16014 quirk_gcc_member_function_pointer (type, objfile);
16015 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
16016 cu->rust_unions.push_back (type);
16017 else if (cu->per_cu->lang == language_ada)
16018 quirk_ada_thick_pointer_struct (die, cu, type);
16019
16020 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16021 snapshots) has been known to create a die giving a declaration
16022 for a class that has, as a child, a die giving a definition for a
16023 nested class. So we have to process our children even if the
16024 current die is a declaration. Normally, of course, a declaration
16025 won't have any children at all. */
16026
16027 child_die = die->child;
16028
16029 while (child_die != NULL && child_die->tag)
16030 {
16031 if (child_die->tag == DW_TAG_member
16032 || child_die->tag == DW_TAG_variable
16033 || child_die->tag == DW_TAG_inheritance
16034 || child_die->tag == DW_TAG_template_value_param
16035 || child_die->tag == DW_TAG_template_type_param)
16036 {
16037 /* Do nothing. */
16038 }
16039 else
16040 process_die (child_die, cu);
16041
16042 child_die = child_die->sibling;
16043 }
16044
16045 /* Do not consider external references. According to the DWARF standard,
16046 these DIEs are identified by the fact that they have no byte_size
16047 attribute, and a declaration attribute. */
16048 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16049 || !die_is_declaration (die, cu)
16050 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16051 {
16052 struct symbol *sym = new_symbol (die, type, cu);
16053
16054 if (has_template_parameters)
16055 {
16056 struct symtab *symtab;
16057 if (sym != nullptr)
16058 symtab = symbol_symtab (sym);
16059 else if (cu->line_header != nullptr)
16060 {
16061 /* Any related symtab will do. */
16062 symtab
16063 = cu->line_header->file_names ()[0].symtab;
16064 }
16065 else
16066 {
16067 symtab = nullptr;
16068 complaint (_("could not find suitable "
16069 "symtab for template parameter"
16070 " - DIE at %s [in module %s]"),
16071 sect_offset_str (die->sect_off),
16072 objfile_name (objfile));
16073 }
16074
16075 if (symtab != nullptr)
16076 {
16077 /* Make sure that the symtab is set on the new symbols.
16078 Even though they don't appear in this symtab directly,
16079 other parts of gdb assume that symbols do, and this is
16080 reasonably true. */
16081 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16082 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16083 }
16084 }
16085 }
16086 }
16087
16088 /* Assuming DIE is an enumeration type, and TYPE is its associated
16089 type, update TYPE using some information only available in DIE's
16090 children. In particular, the fields are computed. */
16091
16092 static void
16093 update_enumeration_type_from_children (struct die_info *die,
16094 struct type *type,
16095 struct dwarf2_cu *cu)
16096 {
16097 struct die_info *child_die;
16098 int unsigned_enum = 1;
16099 int flag_enum = 1;
16100
16101 auto_obstack obstack;
16102 std::vector<struct field> fields;
16103
16104 for (child_die = die->child;
16105 child_die != NULL && child_die->tag;
16106 child_die = child_die->sibling)
16107 {
16108 struct attribute *attr;
16109 LONGEST value;
16110 const gdb_byte *bytes;
16111 struct dwarf2_locexpr_baton *baton;
16112 const char *name;
16113
16114 if (child_die->tag != DW_TAG_enumerator)
16115 continue;
16116
16117 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16118 if (attr == NULL)
16119 continue;
16120
16121 name = dwarf2_name (child_die, cu);
16122 if (name == NULL)
16123 name = "<anonymous enumerator>";
16124
16125 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16126 &value, &bytes, &baton);
16127 if (value < 0)
16128 {
16129 unsigned_enum = 0;
16130 flag_enum = 0;
16131 }
16132 else
16133 {
16134 if (count_one_bits_ll (value) >= 2)
16135 flag_enum = 0;
16136 }
16137
16138 fields.emplace_back ();
16139 struct field &field = fields.back ();
16140 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16141 SET_FIELD_ENUMVAL (field, value);
16142 }
16143
16144 if (!fields.empty ())
16145 {
16146 type->set_num_fields (fields.size ());
16147 type->set_fields
16148 ((struct field *)
16149 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16150 memcpy (type->fields (), fields.data (),
16151 sizeof (struct field) * fields.size ());
16152 }
16153
16154 if (unsigned_enum)
16155 type->set_is_unsigned (true);
16156
16157 if (flag_enum)
16158 type->set_is_flag_enum (true);
16159 }
16160
16161 /* Given a DW_AT_enumeration_type die, set its type. We do not
16162 complete the type's fields yet, or create any symbols. */
16163
16164 static struct type *
16165 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16166 {
16167 struct objfile *objfile = cu->per_objfile->objfile;
16168 struct type *type;
16169 struct attribute *attr;
16170 const char *name;
16171
16172 /* If the definition of this type lives in .debug_types, read that type.
16173 Don't follow DW_AT_specification though, that will take us back up
16174 the chain and we want to go down. */
16175 attr = die->attr (DW_AT_signature);
16176 if (attr != nullptr)
16177 {
16178 type = get_DW_AT_signature_type (die, attr, cu);
16179
16180 /* The type's CU may not be the same as CU.
16181 Ensure TYPE is recorded with CU in die_type_hash. */
16182 return set_die_type (die, type, cu);
16183 }
16184
16185 type = alloc_type (objfile);
16186
16187 type->set_code (TYPE_CODE_ENUM);
16188 name = dwarf2_full_name (NULL, die, cu);
16189 if (name != NULL)
16190 type->set_name (name);
16191
16192 attr = dwarf2_attr (die, DW_AT_type, cu);
16193 if (attr != NULL)
16194 {
16195 struct type *underlying_type = die_type (die, cu);
16196
16197 TYPE_TARGET_TYPE (type) = underlying_type;
16198 }
16199
16200 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16201 if (attr != nullptr)
16202 {
16203 TYPE_LENGTH (type) = attr->constant_value (0);
16204 }
16205 else
16206 {
16207 TYPE_LENGTH (type) = 0;
16208 }
16209
16210 maybe_set_alignment (cu, die, type);
16211
16212 /* The enumeration DIE can be incomplete. In Ada, any type can be
16213 declared as private in the package spec, and then defined only
16214 inside the package body. Such types are known as Taft Amendment
16215 Types. When another package uses such a type, an incomplete DIE
16216 may be generated by the compiler. */
16217 if (die_is_declaration (die, cu))
16218 type->set_is_stub (true);
16219
16220 /* If this type has an underlying type that is not a stub, then we
16221 may use its attributes. We always use the "unsigned" attribute
16222 in this situation, because ordinarily we guess whether the type
16223 is unsigned -- but the guess can be wrong and the underlying type
16224 can tell us the reality. However, we defer to a local size
16225 attribute if one exists, because this lets the compiler override
16226 the underlying type if needed. */
16227 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16228 {
16229 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16230 underlying_type = check_typedef (underlying_type);
16231
16232 type->set_is_unsigned (underlying_type->is_unsigned ());
16233
16234 if (TYPE_LENGTH (type) == 0)
16235 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16236
16237 if (TYPE_RAW_ALIGN (type) == 0
16238 && TYPE_RAW_ALIGN (underlying_type) != 0)
16239 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16240 }
16241
16242 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16243
16244 set_die_type (die, type, cu);
16245
16246 /* Finish the creation of this type by using the enum's children.
16247 Note that, as usual, this must come after set_die_type to avoid
16248 infinite recursion when trying to compute the names of the
16249 enumerators. */
16250 update_enumeration_type_from_children (die, type, cu);
16251
16252 return type;
16253 }
16254
16255 /* Given a pointer to a die which begins an enumeration, process all
16256 the dies that define the members of the enumeration, and create the
16257 symbol for the enumeration type.
16258
16259 NOTE: We reverse the order of the element list. */
16260
16261 static void
16262 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16263 {
16264 struct type *this_type;
16265
16266 this_type = get_die_type (die, cu);
16267 if (this_type == NULL)
16268 this_type = read_enumeration_type (die, cu);
16269
16270 if (die->child != NULL)
16271 {
16272 struct die_info *child_die;
16273 const char *name;
16274
16275 child_die = die->child;
16276 while (child_die && child_die->tag)
16277 {
16278 if (child_die->tag != DW_TAG_enumerator)
16279 {
16280 process_die (child_die, cu);
16281 }
16282 else
16283 {
16284 name = dwarf2_name (child_die, cu);
16285 if (name)
16286 new_symbol (child_die, this_type, cu);
16287 }
16288
16289 child_die = child_die->sibling;
16290 }
16291 }
16292
16293 /* If we are reading an enum from a .debug_types unit, and the enum
16294 is a declaration, and the enum is not the signatured type in the
16295 unit, then we do not want to add a symbol for it. Adding a
16296 symbol would in some cases obscure the true definition of the
16297 enum, giving users an incomplete type when the definition is
16298 actually available. Note that we do not want to do this for all
16299 enums which are just declarations, because C++0x allows forward
16300 enum declarations. */
16301 if (cu->per_cu->is_debug_types
16302 && die_is_declaration (die, cu))
16303 {
16304 struct signatured_type *sig_type;
16305
16306 sig_type = (struct signatured_type *) cu->per_cu;
16307 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16308 if (sig_type->type_offset_in_section != die->sect_off)
16309 return;
16310 }
16311
16312 new_symbol (die, this_type, cu);
16313 }
16314
16315 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16316 expression for an index type and finds the corresponding field
16317 offset in the hidden "P_BOUNDS" structure. Returns true on success
16318 and updates *FIELD, false if it fails to recognize an
16319 expression. */
16320
16321 static bool
16322 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16323 int *bounds_offset, struct field *field,
16324 struct dwarf2_cu *cu)
16325 {
16326 struct attribute *attr = dwarf2_attr (die, name, cu);
16327 if (attr == nullptr || !attr->form_is_block ())
16328 return false;
16329
16330 const struct dwarf_block *block = attr->as_block ();
16331 const gdb_byte *start = block->data;
16332 const gdb_byte *end = block->data + block->size;
16333
16334 /* The expression to recognize generally looks like:
16335
16336 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16337 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16338
16339 However, the second "plus_uconst" may be missing:
16340
16341 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16342 DW_OP_deref_size: 4)
16343
16344 This happens when the field is at the start of the structure.
16345
16346 Also, the final deref may not be sized:
16347
16348 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16349 DW_OP_deref)
16350
16351 This happens when the size of the index type happens to be the
16352 same as the architecture's word size. This can occur with or
16353 without the second plus_uconst. */
16354
16355 if (end - start < 2)
16356 return false;
16357 if (*start++ != DW_OP_push_object_address)
16358 return false;
16359 if (*start++ != DW_OP_plus_uconst)
16360 return false;
16361
16362 uint64_t this_bound_off;
16363 start = gdb_read_uleb128 (start, end, &this_bound_off);
16364 if (start == nullptr || (int) this_bound_off != this_bound_off)
16365 return false;
16366 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16367 is consistent among all bounds. */
16368 if (*bounds_offset == -1)
16369 *bounds_offset = this_bound_off;
16370 else if (*bounds_offset != this_bound_off)
16371 return false;
16372
16373 if (start == end || *start++ != DW_OP_deref)
16374 return false;
16375
16376 int offset = 0;
16377 if (start ==end)
16378 return false;
16379 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16380 {
16381 /* This means an offset of 0. */
16382 }
16383 else if (*start++ != DW_OP_plus_uconst)
16384 return false;
16385 else
16386 {
16387 /* The size is the parameter to DW_OP_plus_uconst. */
16388 uint64_t val;
16389 start = gdb_read_uleb128 (start, end, &val);
16390 if (start == nullptr)
16391 return false;
16392 if ((int) val != val)
16393 return false;
16394 offset = val;
16395 }
16396
16397 if (start == end)
16398 return false;
16399
16400 uint64_t size;
16401 if (*start == DW_OP_deref_size)
16402 {
16403 start = gdb_read_uleb128 (start + 1, end, &size);
16404 if (start == nullptr)
16405 return false;
16406 }
16407 else if (*start == DW_OP_deref)
16408 {
16409 size = cu->header.addr_size;
16410 ++start;
16411 }
16412 else
16413 return false;
16414
16415 SET_FIELD_BITPOS (*field, 8 * offset);
16416 if (size != TYPE_LENGTH (field->type ()))
16417 FIELD_BITSIZE (*field) = 8 * size;
16418
16419 return true;
16420 }
16421
16422 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16423 some kinds of Ada arrays:
16424
16425 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16426 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16427 <11e0> DW_AT_data_location: 2 byte block: 97 6
16428 (DW_OP_push_object_address; DW_OP_deref)
16429 <11e3> DW_AT_type : <0x1173>
16430 <11e7> DW_AT_sibling : <0x1201>
16431 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16432 <11ec> DW_AT_type : <0x1206>
16433 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16434 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16435 DW_OP_deref_size: 4)
16436 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16437 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16438 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16439
16440 This actually represents a "thick pointer", which is a structure
16441 with two elements: one that is a pointer to the array data, and one
16442 that is a pointer to another structure; this second structure holds
16443 the array bounds.
16444
16445 This returns a new type on success, or nullptr if this didn't
16446 recognize the type. */
16447
16448 static struct type *
16449 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16450 struct type *type)
16451 {
16452 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16453 /* So far we've only seen this with block form. */
16454 if (attr == nullptr || !attr->form_is_block ())
16455 return nullptr;
16456
16457 /* Note that this will fail if the structure layout is changed by
16458 the compiler. However, we have no good way to recognize some
16459 other layout, because we don't know what expression the compiler
16460 might choose to emit should this happen. */
16461 struct dwarf_block *blk = attr->as_block ();
16462 if (blk->size != 2
16463 || blk->data[0] != DW_OP_push_object_address
16464 || blk->data[1] != DW_OP_deref)
16465 return nullptr;
16466
16467 int bounds_offset = -1;
16468 int max_align = -1;
16469 std::vector<struct field> range_fields;
16470 for (struct die_info *child_die = die->child;
16471 child_die;
16472 child_die = child_die->sibling)
16473 {
16474 if (child_die->tag == DW_TAG_subrange_type)
16475 {
16476 struct type *underlying = read_subrange_index_type (child_die, cu);
16477
16478 int this_align = type_align (underlying);
16479 if (this_align > max_align)
16480 max_align = this_align;
16481
16482 range_fields.emplace_back ();
16483 range_fields.emplace_back ();
16484
16485 struct field &lower = range_fields[range_fields.size () - 2];
16486 struct field &upper = range_fields[range_fields.size () - 1];
16487
16488 lower.set_type (underlying);
16489 FIELD_ARTIFICIAL (lower) = 1;
16490
16491 upper.set_type (underlying);
16492 FIELD_ARTIFICIAL (upper) = 1;
16493
16494 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16495 &bounds_offset, &lower, cu)
16496 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16497 &bounds_offset, &upper, cu))
16498 return nullptr;
16499 }
16500 }
16501
16502 /* This shouldn't really happen, but double-check that we found
16503 where the bounds are stored. */
16504 if (bounds_offset == -1)
16505 return nullptr;
16506
16507 struct objfile *objfile = cu->per_objfile->objfile;
16508 for (int i = 0; i < range_fields.size (); i += 2)
16509 {
16510 char name[20];
16511
16512 /* Set the name of each field in the bounds. */
16513 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16514 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16515 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16516 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16517 }
16518
16519 struct type *bounds = alloc_type (objfile);
16520 bounds->set_code (TYPE_CODE_STRUCT);
16521
16522 bounds->set_num_fields (range_fields.size ());
16523 bounds->set_fields
16524 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16525 * sizeof (struct field))));
16526 memcpy (bounds->fields (), range_fields.data (),
16527 bounds->num_fields () * sizeof (struct field));
16528
16529 int last_fieldno = range_fields.size () - 1;
16530 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16531 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16532 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16533
16534 /* Rewrite the existing array type in place. Specifically, we
16535 remove any dynamic properties we might have read, and we replace
16536 the index types. */
16537 struct type *iter = type;
16538 for (int i = 0; i < range_fields.size (); i += 2)
16539 {
16540 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16541 iter->main_type->dyn_prop_list = nullptr;
16542 iter->set_index_type
16543 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16544 iter = TYPE_TARGET_TYPE (iter);
16545 }
16546
16547 struct type *result = alloc_type (objfile);
16548 result->set_code (TYPE_CODE_STRUCT);
16549
16550 result->set_num_fields (2);
16551 result->set_fields
16552 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16553 * sizeof (struct field))));
16554
16555 /* The names are chosen to coincide with what the compiler does with
16556 -fgnat-encodings=all, which the Ada code in gdb already
16557 understands. */
16558 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16559 result->field (0).set_type (lookup_pointer_type (type));
16560
16561 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16562 result->field (1).set_type (lookup_pointer_type (bounds));
16563 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16564
16565 result->set_name (type->name ());
16566 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16567 + TYPE_LENGTH (result->field (1).type ()));
16568
16569 return result;
16570 }
16571
16572 /* Extract all information from a DW_TAG_array_type DIE and put it in
16573 the DIE's type field. For now, this only handles one dimensional
16574 arrays. */
16575
16576 static struct type *
16577 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16578 {
16579 struct objfile *objfile = cu->per_objfile->objfile;
16580 struct die_info *child_die;
16581 struct type *type;
16582 struct type *element_type, *range_type, *index_type;
16583 struct attribute *attr;
16584 const char *name;
16585 struct dynamic_prop *byte_stride_prop = NULL;
16586 unsigned int bit_stride = 0;
16587
16588 element_type = die_type (die, cu);
16589
16590 /* The die_type call above may have already set the type for this DIE. */
16591 type = get_die_type (die, cu);
16592 if (type)
16593 return type;
16594
16595 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16596 if (attr != NULL)
16597 {
16598 int stride_ok;
16599 struct type *prop_type = cu->addr_sized_int_type (false);
16600
16601 byte_stride_prop
16602 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16603 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16604 prop_type);
16605 if (!stride_ok)
16606 {
16607 complaint (_("unable to read array DW_AT_byte_stride "
16608 " - DIE at %s [in module %s]"),
16609 sect_offset_str (die->sect_off),
16610 objfile_name (cu->per_objfile->objfile));
16611 /* Ignore this attribute. We will likely not be able to print
16612 arrays of this type correctly, but there is little we can do
16613 to help if we cannot read the attribute's value. */
16614 byte_stride_prop = NULL;
16615 }
16616 }
16617
16618 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16619 if (attr != NULL)
16620 bit_stride = attr->constant_value (0);
16621
16622 /* Irix 6.2 native cc creates array types without children for
16623 arrays with unspecified length. */
16624 if (die->child == NULL)
16625 {
16626 index_type = objfile_type (objfile)->builtin_int;
16627 range_type = create_static_range_type (NULL, index_type, 0, -1);
16628 type = create_array_type_with_stride (NULL, element_type, range_type,
16629 byte_stride_prop, bit_stride);
16630 return set_die_type (die, type, cu);
16631 }
16632
16633 std::vector<struct type *> range_types;
16634 child_die = die->child;
16635 while (child_die && child_die->tag)
16636 {
16637 if (child_die->tag == DW_TAG_subrange_type)
16638 {
16639 struct type *child_type = read_type_die (child_die, cu);
16640
16641 if (child_type != NULL)
16642 {
16643 /* The range type was succesfully read. Save it for the
16644 array type creation. */
16645 range_types.push_back (child_type);
16646 }
16647 }
16648 child_die = child_die->sibling;
16649 }
16650
16651 if (range_types.empty ())
16652 {
16653 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16654 sect_offset_str (die->sect_off),
16655 objfile_name (cu->per_objfile->objfile));
16656 return NULL;
16657 }
16658
16659 /* Dwarf2 dimensions are output from left to right, create the
16660 necessary array types in backwards order. */
16661
16662 type = element_type;
16663
16664 if (read_array_order (die, cu) == DW_ORD_col_major)
16665 {
16666 int i = 0;
16667
16668 while (i < range_types.size ())
16669 {
16670 type = create_array_type_with_stride (NULL, type, range_types[i++],
16671 byte_stride_prop, bit_stride);
16672 bit_stride = 0;
16673 byte_stride_prop = nullptr;
16674 }
16675 }
16676 else
16677 {
16678 size_t ndim = range_types.size ();
16679 while (ndim-- > 0)
16680 {
16681 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16682 byte_stride_prop, bit_stride);
16683 bit_stride = 0;
16684 byte_stride_prop = nullptr;
16685 }
16686 }
16687
16688 gdb_assert (type != element_type);
16689
16690 /* Understand Dwarf2 support for vector types (like they occur on
16691 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16692 array type. This is not part of the Dwarf2/3 standard yet, but a
16693 custom vendor extension. The main difference between a regular
16694 array and the vector variant is that vectors are passed by value
16695 to functions. */
16696 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16697 if (attr != nullptr)
16698 make_vector_type (type);
16699
16700 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16701 implementation may choose to implement triple vectors using this
16702 attribute. */
16703 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16704 if (attr != nullptr && attr->form_is_unsigned ())
16705 {
16706 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16707 TYPE_LENGTH (type) = attr->as_unsigned ();
16708 else
16709 complaint (_("DW_AT_byte_size for array type smaller "
16710 "than the total size of elements"));
16711 }
16712
16713 name = dwarf2_name (die, cu);
16714 if (name)
16715 type->set_name (name);
16716
16717 maybe_set_alignment (cu, die, type);
16718
16719 struct type *replacement_type = nullptr;
16720 if (cu->per_cu->lang == language_ada)
16721 {
16722 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16723 if (replacement_type != nullptr)
16724 type = replacement_type;
16725 }
16726
16727 /* Install the type in the die. */
16728 set_die_type (die, type, cu, replacement_type != nullptr);
16729
16730 /* set_die_type should be already done. */
16731 set_descriptive_type (type, die, cu);
16732
16733 return type;
16734 }
16735
16736 static enum dwarf_array_dim_ordering
16737 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16738 {
16739 struct attribute *attr;
16740
16741 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16742
16743 if (attr != nullptr)
16744 {
16745 LONGEST val = attr->constant_value (-1);
16746 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16747 return (enum dwarf_array_dim_ordering) val;
16748 }
16749
16750 /* GNU F77 is a special case, as at 08/2004 array type info is the
16751 opposite order to the dwarf2 specification, but data is still
16752 laid out as per normal fortran.
16753
16754 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16755 version checking. */
16756
16757 if (cu->per_cu->lang == language_fortran
16758 && cu->producer && strstr (cu->producer, "GNU F77"))
16759 {
16760 return DW_ORD_row_major;
16761 }
16762
16763 switch (cu->language_defn->array_ordering ())
16764 {
16765 case array_column_major:
16766 return DW_ORD_col_major;
16767 case array_row_major:
16768 default:
16769 return DW_ORD_row_major;
16770 };
16771 }
16772
16773 /* Extract all information from a DW_TAG_set_type DIE and put it in
16774 the DIE's type field. */
16775
16776 static struct type *
16777 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16778 {
16779 struct type *domain_type, *set_type;
16780 struct attribute *attr;
16781
16782 domain_type = die_type (die, cu);
16783
16784 /* The die_type call above may have already set the type for this DIE. */
16785 set_type = get_die_type (die, cu);
16786 if (set_type)
16787 return set_type;
16788
16789 set_type = create_set_type (NULL, domain_type);
16790
16791 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16792 if (attr != nullptr && attr->form_is_unsigned ())
16793 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16794
16795 maybe_set_alignment (cu, die, set_type);
16796
16797 return set_die_type (die, set_type, cu);
16798 }
16799
16800 /* A helper for read_common_block that creates a locexpr baton.
16801 SYM is the symbol which we are marking as computed.
16802 COMMON_DIE is the DIE for the common block.
16803 COMMON_LOC is the location expression attribute for the common
16804 block itself.
16805 MEMBER_LOC is the location expression attribute for the particular
16806 member of the common block that we are processing.
16807 CU is the CU from which the above come. */
16808
16809 static void
16810 mark_common_block_symbol_computed (struct symbol *sym,
16811 struct die_info *common_die,
16812 struct attribute *common_loc,
16813 struct attribute *member_loc,
16814 struct dwarf2_cu *cu)
16815 {
16816 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16817 struct objfile *objfile = per_objfile->objfile;
16818 struct dwarf2_locexpr_baton *baton;
16819 gdb_byte *ptr;
16820 unsigned int cu_off;
16821 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16822 LONGEST offset = 0;
16823
16824 gdb_assert (common_loc && member_loc);
16825 gdb_assert (common_loc->form_is_block ());
16826 gdb_assert (member_loc->form_is_block ()
16827 || member_loc->form_is_constant ());
16828
16829 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16830 baton->per_objfile = per_objfile;
16831 baton->per_cu = cu->per_cu;
16832 gdb_assert (baton->per_cu);
16833
16834 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16835
16836 if (member_loc->form_is_constant ())
16837 {
16838 offset = member_loc->constant_value (0);
16839 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16840 }
16841 else
16842 baton->size += member_loc->as_block ()->size;
16843
16844 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16845 baton->data = ptr;
16846
16847 *ptr++ = DW_OP_call4;
16848 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16849 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16850 ptr += 4;
16851
16852 if (member_loc->form_is_constant ())
16853 {
16854 *ptr++ = DW_OP_addr;
16855 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16856 ptr += cu->header.addr_size;
16857 }
16858 else
16859 {
16860 /* We have to copy the data here, because DW_OP_call4 will only
16861 use a DW_AT_location attribute. */
16862 struct dwarf_block *block = member_loc->as_block ();
16863 memcpy (ptr, block->data, block->size);
16864 ptr += block->size;
16865 }
16866
16867 *ptr++ = DW_OP_plus;
16868 gdb_assert (ptr - baton->data == baton->size);
16869
16870 SYMBOL_LOCATION_BATON (sym) = baton;
16871 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16872 }
16873
16874 /* Create appropriate locally-scoped variables for all the
16875 DW_TAG_common_block entries. Also create a struct common_block
16876 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16877 is used to separate the common blocks name namespace from regular
16878 variable names. */
16879
16880 static void
16881 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16882 {
16883 struct attribute *attr;
16884
16885 attr = dwarf2_attr (die, DW_AT_location, cu);
16886 if (attr != nullptr)
16887 {
16888 /* Support the .debug_loc offsets. */
16889 if (attr->form_is_block ())
16890 {
16891 /* Ok. */
16892 }
16893 else if (attr->form_is_section_offset ())
16894 {
16895 dwarf2_complex_location_expr_complaint ();
16896 attr = NULL;
16897 }
16898 else
16899 {
16900 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16901 "common block member");
16902 attr = NULL;
16903 }
16904 }
16905
16906 if (die->child != NULL)
16907 {
16908 struct objfile *objfile = cu->per_objfile->objfile;
16909 struct die_info *child_die;
16910 size_t n_entries = 0, size;
16911 struct common_block *common_block;
16912 struct symbol *sym;
16913
16914 for (child_die = die->child;
16915 child_die && child_die->tag;
16916 child_die = child_die->sibling)
16917 ++n_entries;
16918
16919 size = (sizeof (struct common_block)
16920 + (n_entries - 1) * sizeof (struct symbol *));
16921 common_block
16922 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16923 size);
16924 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16925 common_block->n_entries = 0;
16926
16927 for (child_die = die->child;
16928 child_die && child_die->tag;
16929 child_die = child_die->sibling)
16930 {
16931 /* Create the symbol in the DW_TAG_common_block block in the current
16932 symbol scope. */
16933 sym = new_symbol (child_die, NULL, cu);
16934 if (sym != NULL)
16935 {
16936 struct attribute *member_loc;
16937
16938 common_block->contents[common_block->n_entries++] = sym;
16939
16940 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16941 cu);
16942 if (member_loc)
16943 {
16944 /* GDB has handled this for a long time, but it is
16945 not specified by DWARF. It seems to have been
16946 emitted by gfortran at least as recently as:
16947 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16948 complaint (_("Variable in common block has "
16949 "DW_AT_data_member_location "
16950 "- DIE at %s [in module %s]"),
16951 sect_offset_str (child_die->sect_off),
16952 objfile_name (objfile));
16953
16954 if (member_loc->form_is_section_offset ())
16955 dwarf2_complex_location_expr_complaint ();
16956 else if (member_loc->form_is_constant ()
16957 || member_loc->form_is_block ())
16958 {
16959 if (attr != nullptr)
16960 mark_common_block_symbol_computed (sym, die, attr,
16961 member_loc, cu);
16962 }
16963 else
16964 dwarf2_complex_location_expr_complaint ();
16965 }
16966 }
16967 }
16968
16969 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16970 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16971 }
16972 }
16973
16974 /* Create a type for a C++ namespace. */
16975
16976 static struct type *
16977 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16978 {
16979 struct objfile *objfile = cu->per_objfile->objfile;
16980 const char *previous_prefix, *name;
16981 int is_anonymous;
16982 struct type *type;
16983
16984 /* For extensions, reuse the type of the original namespace. */
16985 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16986 {
16987 struct die_info *ext_die;
16988 struct dwarf2_cu *ext_cu = cu;
16989
16990 ext_die = dwarf2_extension (die, &ext_cu);
16991 type = read_type_die (ext_die, ext_cu);
16992
16993 /* EXT_CU may not be the same as CU.
16994 Ensure TYPE is recorded with CU in die_type_hash. */
16995 return set_die_type (die, type, cu);
16996 }
16997
16998 name = namespace_name (die, &is_anonymous, cu);
16999
17000 /* Now build the name of the current namespace. */
17001
17002 previous_prefix = determine_prefix (die, cu);
17003 if (previous_prefix[0] != '\0')
17004 name = typename_concat (&objfile->objfile_obstack,
17005 previous_prefix, name, 0, cu);
17006
17007 /* Create the type. */
17008 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17009
17010 return set_die_type (die, type, cu);
17011 }
17012
17013 /* Read a namespace scope. */
17014
17015 static void
17016 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17017 {
17018 struct objfile *objfile = cu->per_objfile->objfile;
17019 int is_anonymous;
17020
17021 /* Add a symbol associated to this if we haven't seen the namespace
17022 before. Also, add a using directive if it's an anonymous
17023 namespace. */
17024
17025 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17026 {
17027 struct type *type;
17028
17029 type = read_type_die (die, cu);
17030 new_symbol (die, type, cu);
17031
17032 namespace_name (die, &is_anonymous, cu);
17033 if (is_anonymous)
17034 {
17035 const char *previous_prefix = determine_prefix (die, cu);
17036
17037 std::vector<const char *> excludes;
17038 add_using_directive (using_directives (cu),
17039 previous_prefix, type->name (), NULL,
17040 NULL, excludes, 0, &objfile->objfile_obstack);
17041 }
17042 }
17043
17044 if (die->child != NULL)
17045 {
17046 struct die_info *child_die = die->child;
17047
17048 while (child_die && child_die->tag)
17049 {
17050 process_die (child_die, cu);
17051 child_die = child_die->sibling;
17052 }
17053 }
17054 }
17055
17056 /* Read a Fortran module as type. This DIE can be only a declaration used for
17057 imported module. Still we need that type as local Fortran "use ... only"
17058 declaration imports depend on the created type in determine_prefix. */
17059
17060 static struct type *
17061 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17062 {
17063 struct objfile *objfile = cu->per_objfile->objfile;
17064 const char *module_name;
17065 struct type *type;
17066
17067 module_name = dwarf2_name (die, cu);
17068 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17069
17070 return set_die_type (die, type, cu);
17071 }
17072
17073 /* Read a Fortran module. */
17074
17075 static void
17076 read_module (struct die_info *die, struct dwarf2_cu *cu)
17077 {
17078 struct die_info *child_die = die->child;
17079 struct type *type;
17080
17081 type = read_type_die (die, cu);
17082 new_symbol (die, type, cu);
17083
17084 while (child_die && child_die->tag)
17085 {
17086 process_die (child_die, cu);
17087 child_die = child_die->sibling;
17088 }
17089 }
17090
17091 /* Return the name of the namespace represented by DIE. Set
17092 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17093 namespace. */
17094
17095 static const char *
17096 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17097 {
17098 struct die_info *current_die;
17099 const char *name = NULL;
17100
17101 /* Loop through the extensions until we find a name. */
17102
17103 for (current_die = die;
17104 current_die != NULL;
17105 current_die = dwarf2_extension (die, &cu))
17106 {
17107 /* We don't use dwarf2_name here so that we can detect the absence
17108 of a name -> anonymous namespace. */
17109 name = dwarf2_string_attr (die, DW_AT_name, cu);
17110
17111 if (name != NULL)
17112 break;
17113 }
17114
17115 /* Is it an anonymous namespace? */
17116
17117 *is_anonymous = (name == NULL);
17118 if (*is_anonymous)
17119 name = CP_ANONYMOUS_NAMESPACE_STR;
17120
17121 return name;
17122 }
17123
17124 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17125 the user defined type vector. */
17126
17127 static struct type *
17128 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17129 {
17130 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17131 struct comp_unit_head *cu_header = &cu->header;
17132 struct type *type;
17133 struct attribute *attr_byte_size;
17134 struct attribute *attr_address_class;
17135 int byte_size, addr_class;
17136 struct type *target_type;
17137
17138 target_type = die_type (die, cu);
17139
17140 /* The die_type call above may have already set the type for this DIE. */
17141 type = get_die_type (die, cu);
17142 if (type)
17143 return type;
17144
17145 type = lookup_pointer_type (target_type);
17146
17147 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17148 if (attr_byte_size)
17149 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17150 else
17151 byte_size = cu_header->addr_size;
17152
17153 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17154 if (attr_address_class)
17155 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17156 else
17157 addr_class = DW_ADDR_none;
17158
17159 ULONGEST alignment = get_alignment (cu, die);
17160
17161 /* If the pointer size, alignment, or address class is different
17162 than the default, create a type variant marked as such and set
17163 the length accordingly. */
17164 if (TYPE_LENGTH (type) != byte_size
17165 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17166 && alignment != TYPE_RAW_ALIGN (type))
17167 || addr_class != DW_ADDR_none)
17168 {
17169 if (gdbarch_address_class_type_flags_p (gdbarch))
17170 {
17171 type_instance_flags type_flags
17172 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17173 addr_class);
17174 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17175 == 0);
17176 type = make_type_with_address_space (type, type_flags);
17177 }
17178 else if (TYPE_LENGTH (type) != byte_size)
17179 {
17180 complaint (_("invalid pointer size %d"), byte_size);
17181 }
17182 else if (TYPE_RAW_ALIGN (type) != alignment)
17183 {
17184 complaint (_("Invalid DW_AT_alignment"
17185 " - DIE at %s [in module %s]"),
17186 sect_offset_str (die->sect_off),
17187 objfile_name (cu->per_objfile->objfile));
17188 }
17189 else
17190 {
17191 /* Should we also complain about unhandled address classes? */
17192 }
17193 }
17194
17195 TYPE_LENGTH (type) = byte_size;
17196 set_type_align (type, alignment);
17197 return set_die_type (die, type, cu);
17198 }
17199
17200 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17201 the user defined type vector. */
17202
17203 static struct type *
17204 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17205 {
17206 struct type *type;
17207 struct type *to_type;
17208 struct type *domain;
17209
17210 to_type = die_type (die, cu);
17211 domain = die_containing_type (die, cu);
17212
17213 /* The calls above may have already set the type for this DIE. */
17214 type = get_die_type (die, cu);
17215 if (type)
17216 return type;
17217
17218 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17219 type = lookup_methodptr_type (to_type);
17220 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17221 {
17222 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17223
17224 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17225 to_type->fields (), to_type->num_fields (),
17226 to_type->has_varargs ());
17227 type = lookup_methodptr_type (new_type);
17228 }
17229 else
17230 type = lookup_memberptr_type (to_type, domain);
17231
17232 return set_die_type (die, type, cu);
17233 }
17234
17235 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17236 the user defined type vector. */
17237
17238 static struct type *
17239 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17240 enum type_code refcode)
17241 {
17242 struct comp_unit_head *cu_header = &cu->header;
17243 struct type *type, *target_type;
17244 struct attribute *attr;
17245
17246 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17247
17248 target_type = die_type (die, cu);
17249
17250 /* The die_type call above may have already set the type for this DIE. */
17251 type = get_die_type (die, cu);
17252 if (type)
17253 return type;
17254
17255 type = lookup_reference_type (target_type, refcode);
17256 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17257 if (attr != nullptr)
17258 {
17259 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17260 }
17261 else
17262 {
17263 TYPE_LENGTH (type) = cu_header->addr_size;
17264 }
17265 maybe_set_alignment (cu, die, type);
17266 return set_die_type (die, type, cu);
17267 }
17268
17269 /* Add the given cv-qualifiers to the element type of the array. GCC
17270 outputs DWARF type qualifiers that apply to an array, not the
17271 element type. But GDB relies on the array element type to carry
17272 the cv-qualifiers. This mimics section 6.7.3 of the C99
17273 specification. */
17274
17275 static struct type *
17276 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17277 struct type *base_type, int cnst, int voltl)
17278 {
17279 struct type *el_type, *inner_array;
17280
17281 base_type = copy_type (base_type);
17282 inner_array = base_type;
17283
17284 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17285 {
17286 TYPE_TARGET_TYPE (inner_array) =
17287 copy_type (TYPE_TARGET_TYPE (inner_array));
17288 inner_array = TYPE_TARGET_TYPE (inner_array);
17289 }
17290
17291 el_type = TYPE_TARGET_TYPE (inner_array);
17292 cnst |= TYPE_CONST (el_type);
17293 voltl |= TYPE_VOLATILE (el_type);
17294 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17295
17296 return set_die_type (die, base_type, cu);
17297 }
17298
17299 static struct type *
17300 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17301 {
17302 struct type *base_type, *cv_type;
17303
17304 base_type = die_type (die, cu);
17305
17306 /* The die_type call above may have already set the type for this DIE. */
17307 cv_type = get_die_type (die, cu);
17308 if (cv_type)
17309 return cv_type;
17310
17311 /* In case the const qualifier is applied to an array type, the element type
17312 is so qualified, not the array type (section 6.7.3 of C99). */
17313 if (base_type->code () == TYPE_CODE_ARRAY)
17314 return add_array_cv_type (die, cu, base_type, 1, 0);
17315
17316 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17317 return set_die_type (die, cv_type, cu);
17318 }
17319
17320 static struct type *
17321 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17322 {
17323 struct type *base_type, *cv_type;
17324
17325 base_type = die_type (die, cu);
17326
17327 /* The die_type call above may have already set the type for this DIE. */
17328 cv_type = get_die_type (die, cu);
17329 if (cv_type)
17330 return cv_type;
17331
17332 /* In case the volatile qualifier is applied to an array type, the
17333 element type is so qualified, not the array type (section 6.7.3
17334 of C99). */
17335 if (base_type->code () == TYPE_CODE_ARRAY)
17336 return add_array_cv_type (die, cu, base_type, 0, 1);
17337
17338 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17339 return set_die_type (die, cv_type, cu);
17340 }
17341
17342 /* Handle DW_TAG_restrict_type. */
17343
17344 static struct type *
17345 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17346 {
17347 struct type *base_type, *cv_type;
17348
17349 base_type = die_type (die, cu);
17350
17351 /* The die_type call above may have already set the type for this DIE. */
17352 cv_type = get_die_type (die, cu);
17353 if (cv_type)
17354 return cv_type;
17355
17356 cv_type = make_restrict_type (base_type);
17357 return set_die_type (die, cv_type, cu);
17358 }
17359
17360 /* Handle DW_TAG_atomic_type. */
17361
17362 static struct type *
17363 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17364 {
17365 struct type *base_type, *cv_type;
17366
17367 base_type = die_type (die, cu);
17368
17369 /* The die_type call above may have already set the type for this DIE. */
17370 cv_type = get_die_type (die, cu);
17371 if (cv_type)
17372 return cv_type;
17373
17374 cv_type = make_atomic_type (base_type);
17375 return set_die_type (die, cv_type, cu);
17376 }
17377
17378 /* Extract all information from a DW_TAG_string_type DIE and add to
17379 the user defined type vector. It isn't really a user defined type,
17380 but it behaves like one, with other DIE's using an AT_user_def_type
17381 attribute to reference it. */
17382
17383 static struct type *
17384 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17385 {
17386 struct objfile *objfile = cu->per_objfile->objfile;
17387 struct gdbarch *gdbarch = objfile->arch ();
17388 struct type *type, *range_type, *index_type, *char_type;
17389 struct attribute *attr;
17390 struct dynamic_prop prop;
17391 bool length_is_constant = true;
17392 LONGEST length;
17393
17394 /* There are a couple of places where bit sizes might be made use of
17395 when parsing a DW_TAG_string_type, however, no producer that we know
17396 of make use of these. Handling bit sizes that are a multiple of the
17397 byte size is easy enough, but what about other bit sizes? Lets deal
17398 with that problem when we have to. Warn about these attributes being
17399 unsupported, then parse the type and ignore them like we always
17400 have. */
17401 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17402 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17403 {
17404 static bool warning_printed = false;
17405 if (!warning_printed)
17406 {
17407 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17408 "currently supported on DW_TAG_string_type."));
17409 warning_printed = true;
17410 }
17411 }
17412
17413 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17414 if (attr != nullptr && !attr->form_is_constant ())
17415 {
17416 /* The string length describes the location at which the length of
17417 the string can be found. The size of the length field can be
17418 specified with one of the attributes below. */
17419 struct type *prop_type;
17420 struct attribute *len
17421 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17422 if (len == nullptr)
17423 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17424 if (len != nullptr && len->form_is_constant ())
17425 {
17426 /* Pass 0 as the default as we know this attribute is constant
17427 and the default value will not be returned. */
17428 LONGEST sz = len->constant_value (0);
17429 prop_type = cu->per_objfile->int_type (sz, true);
17430 }
17431 else
17432 {
17433 /* If the size is not specified then we assume it is the size of
17434 an address on this target. */
17435 prop_type = cu->addr_sized_int_type (true);
17436 }
17437
17438 /* Convert the attribute into a dynamic property. */
17439 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17440 length = 1;
17441 else
17442 length_is_constant = false;
17443 }
17444 else if (attr != nullptr)
17445 {
17446 /* This DW_AT_string_length just contains the length with no
17447 indirection. There's no need to create a dynamic property in this
17448 case. Pass 0 for the default value as we know it will not be
17449 returned in this case. */
17450 length = attr->constant_value (0);
17451 }
17452 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17453 {
17454 /* We don't currently support non-constant byte sizes for strings. */
17455 length = attr->constant_value (1);
17456 }
17457 else
17458 {
17459 /* Use 1 as a fallback length if we have nothing else. */
17460 length = 1;
17461 }
17462
17463 index_type = objfile_type (objfile)->builtin_int;
17464 if (length_is_constant)
17465 range_type = create_static_range_type (NULL, index_type, 1, length);
17466 else
17467 {
17468 struct dynamic_prop low_bound;
17469
17470 low_bound.set_const_val (1);
17471 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17472 }
17473 char_type = language_string_char_type (cu->language_defn, gdbarch);
17474 type = create_string_type (NULL, char_type, range_type);
17475
17476 return set_die_type (die, type, cu);
17477 }
17478
17479 /* Assuming that DIE corresponds to a function, returns nonzero
17480 if the function is prototyped. */
17481
17482 static int
17483 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17484 {
17485 struct attribute *attr;
17486
17487 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17488 if (attr && attr->as_boolean ())
17489 return 1;
17490
17491 /* The DWARF standard implies that the DW_AT_prototyped attribute
17492 is only meaningful for C, but the concept also extends to other
17493 languages that allow unprototyped functions (Eg: Objective C).
17494 For all other languages, assume that functions are always
17495 prototyped. */
17496 if (cu->per_cu->lang != language_c
17497 && cu->per_cu->lang != language_objc
17498 && cu->per_cu->lang != language_opencl)
17499 return 1;
17500
17501 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17502 prototyped and unprototyped functions; default to prototyped,
17503 since that is more common in modern code (and RealView warns
17504 about unprototyped functions). */
17505 if (producer_is_realview (cu->producer))
17506 return 1;
17507
17508 return 0;
17509 }
17510
17511 /* Handle DIES due to C code like:
17512
17513 struct foo
17514 {
17515 int (*funcp)(int a, long l);
17516 int b;
17517 };
17518
17519 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17520
17521 static struct type *
17522 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17523 {
17524 struct objfile *objfile = cu->per_objfile->objfile;
17525 struct type *type; /* Type that this function returns. */
17526 struct type *ftype; /* Function that returns above type. */
17527 struct attribute *attr;
17528
17529 type = die_type (die, cu);
17530
17531 /* The die_type call above may have already set the type for this DIE. */
17532 ftype = get_die_type (die, cu);
17533 if (ftype)
17534 return ftype;
17535
17536 ftype = lookup_function_type (type);
17537
17538 if (prototyped_function_p (die, cu))
17539 ftype->set_is_prototyped (true);
17540
17541 /* Store the calling convention in the type if it's available in
17542 the subroutine die. Otherwise set the calling convention to
17543 the default value DW_CC_normal. */
17544 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17545 if (attr != nullptr
17546 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17547 TYPE_CALLING_CONVENTION (ftype)
17548 = (enum dwarf_calling_convention) attr->constant_value (0);
17549 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17550 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17551 else
17552 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17553
17554 /* Record whether the function returns normally to its caller or not
17555 if the DWARF producer set that information. */
17556 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17557 if (attr && attr->as_boolean ())
17558 TYPE_NO_RETURN (ftype) = 1;
17559
17560 /* We need to add the subroutine type to the die immediately so
17561 we don't infinitely recurse when dealing with parameters
17562 declared as the same subroutine type. */
17563 set_die_type (die, ftype, cu);
17564
17565 if (die->child != NULL)
17566 {
17567 struct type *void_type = objfile_type (objfile)->builtin_void;
17568 struct die_info *child_die;
17569 int nparams, iparams;
17570
17571 /* Count the number of parameters.
17572 FIXME: GDB currently ignores vararg functions, but knows about
17573 vararg member functions. */
17574 nparams = 0;
17575 child_die = die->child;
17576 while (child_die && child_die->tag)
17577 {
17578 if (child_die->tag == DW_TAG_formal_parameter)
17579 nparams++;
17580 else if (child_die->tag == DW_TAG_unspecified_parameters)
17581 ftype->set_has_varargs (true);
17582
17583 child_die = child_die->sibling;
17584 }
17585
17586 /* Allocate storage for parameters and fill them in. */
17587 ftype->set_num_fields (nparams);
17588 ftype->set_fields
17589 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17590
17591 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17592 even if we error out during the parameters reading below. */
17593 for (iparams = 0; iparams < nparams; iparams++)
17594 ftype->field (iparams).set_type (void_type);
17595
17596 iparams = 0;
17597 child_die = die->child;
17598 while (child_die && child_die->tag)
17599 {
17600 if (child_die->tag == DW_TAG_formal_parameter)
17601 {
17602 struct type *arg_type;
17603
17604 /* DWARF version 2 has no clean way to discern C++
17605 static and non-static member functions. G++ helps
17606 GDB by marking the first parameter for non-static
17607 member functions (which is the this pointer) as
17608 artificial. We pass this information to
17609 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17610
17611 DWARF version 3 added DW_AT_object_pointer, which GCC
17612 4.5 does not yet generate. */
17613 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17614 if (attr != nullptr)
17615 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17616 else
17617 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17618 arg_type = die_type (child_die, cu);
17619
17620 /* RealView does not mark THIS as const, which the testsuite
17621 expects. GCC marks THIS as const in method definitions,
17622 but not in the class specifications (GCC PR 43053). */
17623 if (cu->per_cu->lang == language_cplus
17624 && !TYPE_CONST (arg_type)
17625 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17626 {
17627 int is_this = 0;
17628 struct dwarf2_cu *arg_cu = cu;
17629 const char *name = dwarf2_name (child_die, cu);
17630
17631 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17632 if (attr != nullptr)
17633 {
17634 /* If the compiler emits this, use it. */
17635 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17636 is_this = 1;
17637 }
17638 else if (name && strcmp (name, "this") == 0)
17639 /* Function definitions will have the argument names. */
17640 is_this = 1;
17641 else if (name == NULL && iparams == 0)
17642 /* Declarations may not have the names, so like
17643 elsewhere in GDB, assume an artificial first
17644 argument is "this". */
17645 is_this = 1;
17646
17647 if (is_this)
17648 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17649 arg_type, 0);
17650 }
17651
17652 ftype->field (iparams).set_type (arg_type);
17653 iparams++;
17654 }
17655 child_die = child_die->sibling;
17656 }
17657 }
17658
17659 return ftype;
17660 }
17661
17662 static struct type *
17663 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17664 {
17665 struct objfile *objfile = cu->per_objfile->objfile;
17666 const char *name = NULL;
17667 struct type *this_type, *target_type;
17668
17669 name = dwarf2_full_name (NULL, die, cu);
17670 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17671 this_type->set_target_is_stub (true);
17672 set_die_type (die, this_type, cu);
17673 target_type = die_type (die, cu);
17674 if (target_type != this_type)
17675 TYPE_TARGET_TYPE (this_type) = target_type;
17676 else
17677 {
17678 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17679 spec and cause infinite loops in GDB. */
17680 complaint (_("Self-referential DW_TAG_typedef "
17681 "- DIE at %s [in module %s]"),
17682 sect_offset_str (die->sect_off), objfile_name (objfile));
17683 TYPE_TARGET_TYPE (this_type) = NULL;
17684 }
17685 if (name == NULL)
17686 {
17687 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17688 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17689 Handle these by just returning the target type, rather than
17690 constructing an anonymous typedef type and trying to handle this
17691 elsewhere. */
17692 set_die_type (die, target_type, cu);
17693 return target_type;
17694 }
17695 return this_type;
17696 }
17697
17698 /* Helper for get_dwarf2_rational_constant that computes the value of
17699 a given gmp_mpz given an attribute. */
17700
17701 static void
17702 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17703 {
17704 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17705 location expression that pushes an implicit value. */
17706 if (attr->form == DW_FORM_exprloc)
17707 {
17708 dwarf_block *blk = attr->as_block ();
17709 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17710 {
17711 uint64_t len;
17712 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17713 blk->data + blk->size,
17714 &len);
17715 if (ptr - blk->data + len <= blk->size)
17716 {
17717 mpz_import (value->val, len,
17718 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17719 1, 0, 0, ptr);
17720 return;
17721 }
17722 }
17723
17724 /* On failure set it to 1. */
17725 *value = gdb_mpz (1);
17726 }
17727 else if (attr->form_is_block ())
17728 {
17729 dwarf_block *blk = attr->as_block ();
17730 mpz_import (value->val, blk->size,
17731 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17732 1, 0, 0, blk->data);
17733 }
17734 else
17735 *value = gdb_mpz (attr->constant_value (1));
17736 }
17737
17738 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17739 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17740
17741 If the numerator and/or numerator attribute is missing,
17742 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17743 untouched. */
17744
17745 static void
17746 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17747 gdb_mpz *numerator, gdb_mpz *denominator)
17748 {
17749 struct attribute *num_attr, *denom_attr;
17750
17751 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17752 if (num_attr == nullptr)
17753 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17754 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17755
17756 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17757 if (denom_attr == nullptr)
17758 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17759 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17760
17761 if (num_attr == nullptr || denom_attr == nullptr)
17762 return;
17763
17764 get_mpz (cu, numerator, num_attr);
17765 get_mpz (cu, denominator, denom_attr);
17766 }
17767
17768 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17769 rational constant, rather than a signed one.
17770
17771 If the rational constant has a negative value, a complaint
17772 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17773
17774 static void
17775 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17776 struct dwarf2_cu *cu,
17777 gdb_mpz *numerator,
17778 gdb_mpz *denominator)
17779 {
17780 gdb_mpz num (1);
17781 gdb_mpz denom (1);
17782
17783 get_dwarf2_rational_constant (die, cu, &num, &denom);
17784 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17785 {
17786 mpz_neg (num.val, num.val);
17787 mpz_neg (denom.val, denom.val);
17788 }
17789 else if (mpz_sgn (num.val) == -1)
17790 {
17791 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17792 " in DIE at %s"),
17793 sect_offset_str (die->sect_off));
17794 return;
17795 }
17796 else if (mpz_sgn (denom.val) == -1)
17797 {
17798 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17799 " in DIE at %s"),
17800 sect_offset_str (die->sect_off));
17801 return;
17802 }
17803
17804 *numerator = std::move (num);
17805 *denominator = std::move (denom);
17806 }
17807
17808 /* Assuming that ENCODING is a string whose contents starting at the
17809 K'th character is "_nn" where "nn" is a decimal number, scan that
17810 number and set RESULT to the value. K is updated to point to the
17811 character immediately following the number.
17812
17813 If the string does not conform to the format described above, false
17814 is returned, and K may or may not be changed. */
17815
17816 static bool
17817 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17818 {
17819 /* The next character should be an underscore ('_') followed
17820 by a digit. */
17821 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17822 return false;
17823
17824 /* Skip the underscore. */
17825 k++;
17826 int start = k;
17827
17828 /* Determine the number of digits for our number. */
17829 while (isdigit (encoding[k]))
17830 k++;
17831 if (k == start)
17832 return false;
17833
17834 std::string copy (&encoding[start], k - start);
17835 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17836 return false;
17837
17838 return true;
17839 }
17840
17841 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17842 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17843 DENOM, update OFFSET, and return true on success. Return false on
17844 failure. */
17845
17846 static bool
17847 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17848 gdb_mpz *num, gdb_mpz *denom)
17849 {
17850 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17851 return false;
17852 return ada_get_gnat_encoded_number (encoding, offset, denom);
17853 }
17854
17855 /* Assuming DIE corresponds to a fixed point type, finish the creation
17856 of the corresponding TYPE by setting its type-specific data. CU is
17857 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17858 encodings. It is nullptr if the GNAT encoding should be
17859 ignored. */
17860
17861 static void
17862 finish_fixed_point_type (struct type *type, const char *suffix,
17863 struct die_info *die, struct dwarf2_cu *cu)
17864 {
17865 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17866 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17867
17868 /* If GNAT encodings are preferred, don't examine the
17869 attributes. */
17870 struct attribute *attr = nullptr;
17871 if (suffix == nullptr)
17872 {
17873 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17874 if (attr == nullptr)
17875 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17876 if (attr == nullptr)
17877 attr = dwarf2_attr (die, DW_AT_small, cu);
17878 }
17879
17880 /* Numerator and denominator of our fixed-point type's scaling factor.
17881 The default is a scaling factor of 1, which we use as a fallback
17882 when we are not able to decode it (problem with the debugging info,
17883 unsupported forms, bug in GDB, etc...). Using that as the default
17884 allows us to at least print the unscaled value, which might still
17885 be useful to a user. */
17886 gdb_mpz scale_num (1);
17887 gdb_mpz scale_denom (1);
17888
17889 if (attr == nullptr)
17890 {
17891 int offset = 0;
17892 if (suffix != nullptr
17893 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17894 &scale_denom)
17895 /* The number might be encoded as _nn_dd_nn_dd, where the
17896 second ratio is the 'small value. In this situation, we
17897 want the second value. */
17898 && (suffix[offset] != '_'
17899 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17900 &scale_denom)))
17901 {
17902 /* Found it. */
17903 }
17904 else
17905 {
17906 /* Scaling factor not found. Assume a scaling factor of 1,
17907 and hope for the best. At least the user will be able to
17908 see the encoded value. */
17909 scale_num = 1;
17910 scale_denom = 1;
17911 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17912 sect_offset_str (die->sect_off));
17913 }
17914 }
17915 else if (attr->name == DW_AT_binary_scale)
17916 {
17917 LONGEST scale_exp = attr->constant_value (0);
17918 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17919
17920 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17921 }
17922 else if (attr->name == DW_AT_decimal_scale)
17923 {
17924 LONGEST scale_exp = attr->constant_value (0);
17925 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17926
17927 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17928 }
17929 else if (attr->name == DW_AT_small)
17930 {
17931 struct die_info *scale_die;
17932 struct dwarf2_cu *scale_cu = cu;
17933
17934 scale_die = follow_die_ref (die, attr, &scale_cu);
17935 if (scale_die->tag == DW_TAG_constant)
17936 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17937 &scale_num, &scale_denom);
17938 else
17939 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17940 " (DIE at %s)"),
17941 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17942 }
17943 else
17944 {
17945 complaint (_("unsupported scale attribute %s for fixed-point type"
17946 " (DIE at %s)"),
17947 dwarf_attr_name (attr->name),
17948 sect_offset_str (die->sect_off));
17949 }
17950
17951 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17952 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17953 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17954 mpq_canonicalize (scaling_factor.val);
17955 }
17956
17957 /* The gnat-encoding suffix for fixed point. */
17958
17959 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17960
17961 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17962 "XF" suffix of the name. The text after this is what encodes the
17963 'small and 'delta information. Otherwise, return nullptr. */
17964
17965 static const char *
17966 gnat_encoded_fixed_point_type_info (const char *name)
17967 {
17968 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17969 }
17970
17971 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17972 (which may be different from NAME) to the architecture back-end to allow
17973 it to guess the correct format if necessary. */
17974
17975 static struct type *
17976 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17977 const char *name_hint, enum bfd_endian byte_order)
17978 {
17979 struct gdbarch *gdbarch = objfile->arch ();
17980 const struct floatformat **format;
17981 struct type *type;
17982
17983 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17984 if (format)
17985 type = init_float_type (objfile, bits, name, format, byte_order);
17986 else
17987 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17988
17989 return type;
17990 }
17991
17992 /* Allocate an integer type of size BITS and name NAME. */
17993
17994 static struct type *
17995 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17996 int bits, int unsigned_p, const char *name)
17997 {
17998 struct type *type;
17999
18000 /* Versions of Intel's C Compiler generate an integer type called "void"
18001 instead of using DW_TAG_unspecified_type. This has been seen on
18002 at least versions 14, 17, and 18. */
18003 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18004 && strcmp (name, "void") == 0)
18005 type = objfile_type (objfile)->builtin_void;
18006 else
18007 type = init_integer_type (objfile, bits, unsigned_p, name);
18008
18009 return type;
18010 }
18011
18012 /* Return true if DIE has a DW_AT_small attribute whose value is
18013 a constant rational, where both the numerator and denominator
18014 are equal to zero.
18015
18016 CU is the DIE's Compilation Unit. */
18017
18018 static bool
18019 has_zero_over_zero_small_attribute (struct die_info *die,
18020 struct dwarf2_cu *cu)
18021 {
18022 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18023 if (attr == nullptr)
18024 return false;
18025
18026 struct dwarf2_cu *scale_cu = cu;
18027 struct die_info *scale_die
18028 = follow_die_ref (die, attr, &scale_cu);
18029
18030 if (scale_die->tag != DW_TAG_constant)
18031 return false;
18032
18033 gdb_mpz num (1), denom (1);
18034 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18035 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18036 }
18037
18038 /* Initialise and return a floating point type of size BITS suitable for
18039 use as a component of a complex number. The NAME_HINT is passed through
18040 when initialising the floating point type and is the name of the complex
18041 type.
18042
18043 As DWARF doesn't currently provide an explicit name for the components
18044 of a complex number, but it can be helpful to have these components
18045 named, we try to select a suitable name based on the size of the
18046 component. */
18047 static struct type *
18048 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18049 struct objfile *objfile,
18050 int bits, const char *name_hint,
18051 enum bfd_endian byte_order)
18052 {
18053 gdbarch *gdbarch = objfile->arch ();
18054 struct type *tt = nullptr;
18055
18056 /* Try to find a suitable floating point builtin type of size BITS.
18057 We're going to use the name of this type as the name for the complex
18058 target type that we are about to create. */
18059 switch (cu->per_cu->lang)
18060 {
18061 case language_fortran:
18062 switch (bits)
18063 {
18064 case 32:
18065 tt = builtin_f_type (gdbarch)->builtin_real;
18066 break;
18067 case 64:
18068 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18069 break;
18070 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18071 case 128:
18072 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18073 break;
18074 }
18075 break;
18076 default:
18077 switch (bits)
18078 {
18079 case 32:
18080 tt = builtin_type (gdbarch)->builtin_float;
18081 break;
18082 case 64:
18083 tt = builtin_type (gdbarch)->builtin_double;
18084 break;
18085 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18086 case 128:
18087 tt = builtin_type (gdbarch)->builtin_long_double;
18088 break;
18089 }
18090 break;
18091 }
18092
18093 /* If the type we found doesn't match the size we were looking for, then
18094 pretend we didn't find a type at all, the complex target type we
18095 create will then be nameless. */
18096 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18097 tt = nullptr;
18098
18099 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18100 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18101 }
18102
18103 /* Find a representation of a given base type and install
18104 it in the TYPE field of the die. */
18105
18106 static struct type *
18107 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18108 {
18109 struct objfile *objfile = cu->per_objfile->objfile;
18110 struct type *type;
18111 struct attribute *attr;
18112 int encoding = 0, bits = 0;
18113 const char *name;
18114 gdbarch *arch;
18115
18116 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18117 if (attr != nullptr && attr->form_is_constant ())
18118 encoding = attr->constant_value (0);
18119 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18120 if (attr != nullptr)
18121 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18122 name = dwarf2_name (die, cu);
18123 if (!name)
18124 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18125
18126 arch = objfile->arch ();
18127 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18128
18129 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18130 if (attr != nullptr && attr->form_is_constant ())
18131 {
18132 int endianity = attr->constant_value (0);
18133
18134 switch (endianity)
18135 {
18136 case DW_END_big:
18137 byte_order = BFD_ENDIAN_BIG;
18138 break;
18139 case DW_END_little:
18140 byte_order = BFD_ENDIAN_LITTLE;
18141 break;
18142 default:
18143 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18144 break;
18145 }
18146 }
18147
18148 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18149 && cu->per_cu->lang == language_ada
18150 && has_zero_over_zero_small_attribute (die, cu))
18151 {
18152 /* brobecker/2018-02-24: This is a fixed point type for which
18153 the scaling factor is represented as fraction whose value
18154 does not make sense (zero divided by zero), so we should
18155 normally never see these. However, there is a small category
18156 of fixed point types for which GNAT is unable to provide
18157 the scaling factor via the standard DWARF mechanisms, and
18158 for which the info is provided via the GNAT encodings instead.
18159 This is likely what this DIE is about. */
18160 encoding = (encoding == DW_ATE_signed_fixed
18161 ? DW_ATE_signed
18162 : DW_ATE_unsigned);
18163 }
18164
18165 /* With GNAT encodings, fixed-point information will be encoded in
18166 the type name. Note that this can also occur with the above
18167 zero-over-zero case, which is why this is a separate "if" rather
18168 than an "else if". */
18169 const char *gnat_encoding_suffix = nullptr;
18170 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18171 && cu->per_cu->lang == language_ada
18172 && name != nullptr)
18173 {
18174 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18175 if (gnat_encoding_suffix != nullptr)
18176 {
18177 gdb_assert (startswith (gnat_encoding_suffix,
18178 GNAT_FIXED_POINT_SUFFIX));
18179 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18180 name, gnat_encoding_suffix - name);
18181 /* Use -1 here so that SUFFIX points at the "_" after the
18182 "XF". */
18183 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18184
18185 encoding = (encoding == DW_ATE_signed
18186 ? DW_ATE_signed_fixed
18187 : DW_ATE_unsigned_fixed);
18188 }
18189 }
18190
18191 switch (encoding)
18192 {
18193 case DW_ATE_address:
18194 /* Turn DW_ATE_address into a void * pointer. */
18195 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18196 type = init_pointer_type (objfile, bits, name, type);
18197 break;
18198 case DW_ATE_boolean:
18199 type = init_boolean_type (objfile, bits, 1, name);
18200 break;
18201 case DW_ATE_complex_float:
18202 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18203 byte_order);
18204 if (type->code () == TYPE_CODE_ERROR)
18205 {
18206 if (name == nullptr)
18207 {
18208 struct obstack *obstack
18209 = &cu->per_objfile->objfile->objfile_obstack;
18210 name = obconcat (obstack, "_Complex ", type->name (),
18211 nullptr);
18212 }
18213 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18214 }
18215 else
18216 type = init_complex_type (name, type);
18217 break;
18218 case DW_ATE_decimal_float:
18219 type = init_decfloat_type (objfile, bits, name);
18220 break;
18221 case DW_ATE_float:
18222 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18223 break;
18224 case DW_ATE_signed:
18225 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18226 break;
18227 case DW_ATE_unsigned:
18228 if (cu->per_cu->lang == language_fortran
18229 && name
18230 && startswith (name, "character("))
18231 type = init_character_type (objfile, bits, 1, name);
18232 else
18233 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18234 break;
18235 case DW_ATE_signed_char:
18236 if (cu->per_cu->lang == language_ada
18237 || cu->per_cu->lang == language_m2
18238 || cu->per_cu->lang == language_pascal
18239 || cu->per_cu->lang == language_fortran)
18240 type = init_character_type (objfile, bits, 0, name);
18241 else
18242 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18243 break;
18244 case DW_ATE_unsigned_char:
18245 if (cu->per_cu->lang == language_ada
18246 || cu->per_cu->lang == language_m2
18247 || cu->per_cu->lang == language_pascal
18248 || cu->per_cu->lang == language_fortran
18249 || cu->per_cu->lang == language_rust)
18250 type = init_character_type (objfile, bits, 1, name);
18251 else
18252 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18253 break;
18254 case DW_ATE_UTF:
18255 {
18256 if (bits == 16)
18257 type = builtin_type (arch)->builtin_char16;
18258 else if (bits == 32)
18259 type = builtin_type (arch)->builtin_char32;
18260 else
18261 {
18262 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18263 bits);
18264 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18265 }
18266 return set_die_type (die, type, cu);
18267 }
18268 break;
18269 case DW_ATE_signed_fixed:
18270 type = init_fixed_point_type (objfile, bits, 0, name);
18271 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18272 break;
18273 case DW_ATE_unsigned_fixed:
18274 type = init_fixed_point_type (objfile, bits, 1, name);
18275 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18276 break;
18277
18278 default:
18279 complaint (_("unsupported DW_AT_encoding: '%s'"),
18280 dwarf_type_encoding_name (encoding));
18281 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18282 break;
18283 }
18284
18285 if (name && strcmp (name, "char") == 0)
18286 type->set_has_no_signedness (true);
18287
18288 maybe_set_alignment (cu, die, type);
18289
18290 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18291
18292 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18293 {
18294 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18295 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18296 {
18297 unsigned real_bit_size = attr->as_unsigned ();
18298 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18299 /* Only use the attributes if they make sense together. */
18300 if (attr == nullptr
18301 || (attr->as_unsigned () + real_bit_size
18302 <= 8 * TYPE_LENGTH (type)))
18303 {
18304 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18305 = real_bit_size;
18306 if (attr != nullptr)
18307 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18308 = attr->as_unsigned ();
18309 }
18310 }
18311 }
18312
18313 return set_die_type (die, type, cu);
18314 }
18315
18316 /* A helper function that returns the name of DIE, if it refers to a
18317 variable declaration. */
18318
18319 static const char *
18320 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18321 {
18322 if (die->tag != DW_TAG_variable)
18323 return nullptr;
18324
18325 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18326 if (attr == nullptr || !attr->as_boolean ())
18327 return nullptr;
18328
18329 attr = dwarf2_attr (die, DW_AT_name, cu);
18330 if (attr == nullptr)
18331 return nullptr;
18332 return attr->as_string ();
18333 }
18334
18335 /* Parse dwarf attribute if it's a block, reference or constant and put the
18336 resulting value of the attribute into struct bound_prop.
18337 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18338
18339 static int
18340 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18341 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18342 struct type *default_type)
18343 {
18344 struct dwarf2_property_baton *baton;
18345 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18346 struct objfile *objfile = per_objfile->objfile;
18347 struct obstack *obstack = &objfile->objfile_obstack;
18348
18349 gdb_assert (default_type != NULL);
18350
18351 if (attr == NULL || prop == NULL)
18352 return 0;
18353
18354 if (attr->form_is_block ())
18355 {
18356 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18357 baton->property_type = default_type;
18358 baton->locexpr.per_cu = cu->per_cu;
18359 baton->locexpr.per_objfile = per_objfile;
18360
18361 struct dwarf_block *block;
18362 if (attr->form == DW_FORM_data16)
18363 {
18364 size_t data_size = 16;
18365 block = XOBNEW (obstack, struct dwarf_block);
18366 block->size = (data_size
18367 + 2 /* Extra bytes for DW_OP and arg. */);
18368 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18369 data[0] = DW_OP_implicit_value;
18370 data[1] = data_size;
18371 memcpy (&data[2], attr->as_block ()->data, data_size);
18372 block->data = data;
18373 }
18374 else
18375 block = attr->as_block ();
18376
18377 baton->locexpr.size = block->size;
18378 baton->locexpr.data = block->data;
18379 switch (attr->name)
18380 {
18381 case DW_AT_string_length:
18382 baton->locexpr.is_reference = true;
18383 break;
18384 default:
18385 baton->locexpr.is_reference = false;
18386 break;
18387 }
18388
18389 prop->set_locexpr (baton);
18390 gdb_assert (prop->baton () != NULL);
18391 }
18392 else if (attr->form_is_ref ())
18393 {
18394 struct dwarf2_cu *target_cu = cu;
18395 struct die_info *target_die;
18396 struct attribute *target_attr;
18397
18398 target_die = follow_die_ref (die, attr, &target_cu);
18399 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18400 if (target_attr == NULL)
18401 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18402 target_cu);
18403 if (target_attr == nullptr)
18404 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
18405 target_cu);
18406 if (target_attr == NULL)
18407 {
18408 const char *name = var_decl_name (target_die, target_cu);
18409 if (name != nullptr)
18410 {
18411 prop->set_variable_name (name);
18412 return 1;
18413 }
18414 return 0;
18415 }
18416
18417 switch (target_attr->name)
18418 {
18419 case DW_AT_location:
18420 if (target_attr->form_is_section_offset ())
18421 {
18422 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18423 baton->property_type = die_type (target_die, target_cu);
18424 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18425 prop->set_loclist (baton);
18426 gdb_assert (prop->baton () != NULL);
18427 }
18428 else if (target_attr->form_is_block ())
18429 {
18430 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18431 baton->property_type = die_type (target_die, target_cu);
18432 baton->locexpr.per_cu = cu->per_cu;
18433 baton->locexpr.per_objfile = per_objfile;
18434 struct dwarf_block *block = target_attr->as_block ();
18435 baton->locexpr.size = block->size;
18436 baton->locexpr.data = block->data;
18437 baton->locexpr.is_reference = true;
18438 prop->set_locexpr (baton);
18439 gdb_assert (prop->baton () != NULL);
18440 }
18441 else
18442 {
18443 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18444 "dynamic property");
18445 return 0;
18446 }
18447 break;
18448 case DW_AT_data_member_location:
18449 case DW_AT_data_bit_offset:
18450 {
18451 LONGEST offset;
18452
18453 if (!handle_member_location (target_die, target_cu, &offset))
18454 return 0;
18455
18456 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18457 baton->property_type = read_type_die (target_die->parent,
18458 target_cu);
18459 baton->offset_info.offset = offset;
18460 baton->offset_info.type = die_type (target_die, target_cu);
18461 prop->set_addr_offset (baton);
18462 break;
18463 }
18464 }
18465 }
18466 else if (attr->form_is_constant ())
18467 prop->set_const_val (attr->constant_value (0));
18468 else
18469 {
18470 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18471 dwarf2_name (die, cu));
18472 return 0;
18473 }
18474
18475 return 1;
18476 }
18477
18478 /* See read.h. */
18479
18480 struct type *
18481 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18482 {
18483 struct type *int_type;
18484
18485 /* Helper macro to examine the various builtin types. */
18486 #define TRY_TYPE(F) \
18487 int_type = (unsigned_p \
18488 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18489 : objfile_type (objfile)->builtin_ ## F); \
18490 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18491 return int_type
18492
18493 TRY_TYPE (char);
18494 TRY_TYPE (short);
18495 TRY_TYPE (int);
18496 TRY_TYPE (long);
18497 TRY_TYPE (long_long);
18498
18499 #undef TRY_TYPE
18500
18501 gdb_assert_not_reached ("unable to find suitable integer type");
18502 }
18503
18504 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18505 present (which is valid) then compute the default type based on the
18506 compilation units address size. */
18507
18508 static struct type *
18509 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18510 {
18511 struct type *index_type = die_type (die, cu);
18512
18513 /* Dwarf-2 specifications explicitly allows to create subrange types
18514 without specifying a base type.
18515 In that case, the base type must be set to the type of
18516 the lower bound, upper bound or count, in that order, if any of these
18517 three attributes references an object that has a type.
18518 If no base type is found, the Dwarf-2 specifications say that
18519 a signed integer type of size equal to the size of an address should
18520 be used.
18521 For the following C code: `extern char gdb_int [];'
18522 GCC produces an empty range DIE.
18523 FIXME: muller/2010-05-28: Possible references to object for low bound,
18524 high bound or count are not yet handled by this code. */
18525 if (index_type->code () == TYPE_CODE_VOID)
18526 index_type = cu->addr_sized_int_type (false);
18527
18528 return index_type;
18529 }
18530
18531 /* Read the given DW_AT_subrange DIE. */
18532
18533 static struct type *
18534 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18535 {
18536 struct type *base_type, *orig_base_type;
18537 struct type *range_type;
18538 struct attribute *attr;
18539 struct dynamic_prop low, high;
18540 int low_default_is_valid;
18541 int high_bound_is_count = 0;
18542 const char *name;
18543 ULONGEST negative_mask;
18544
18545 orig_base_type = read_subrange_index_type (die, cu);
18546
18547 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18548 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18549 creating the range type, but we use the result of check_typedef
18550 when examining properties of the type. */
18551 base_type = check_typedef (orig_base_type);
18552
18553 /* The die_type call above may have already set the type for this DIE. */
18554 range_type = get_die_type (die, cu);
18555 if (range_type)
18556 return range_type;
18557
18558 high.set_const_val (0);
18559
18560 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18561 omitting DW_AT_lower_bound. */
18562 switch (cu->per_cu->lang)
18563 {
18564 case language_c:
18565 case language_cplus:
18566 low.set_const_val (0);
18567 low_default_is_valid = 1;
18568 break;
18569 case language_fortran:
18570 low.set_const_val (1);
18571 low_default_is_valid = 1;
18572 break;
18573 case language_d:
18574 case language_objc:
18575 case language_rust:
18576 low.set_const_val (0);
18577 low_default_is_valid = (cu->header.version >= 4);
18578 break;
18579 case language_ada:
18580 case language_m2:
18581 case language_pascal:
18582 low.set_const_val (1);
18583 low_default_is_valid = (cu->header.version >= 4);
18584 break;
18585 default:
18586 low.set_const_val (0);
18587 low_default_is_valid = 0;
18588 break;
18589 }
18590
18591 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18592 if (attr != nullptr)
18593 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18594 else if (!low_default_is_valid)
18595 complaint (_("Missing DW_AT_lower_bound "
18596 "- DIE at %s [in module %s]"),
18597 sect_offset_str (die->sect_off),
18598 objfile_name (cu->per_objfile->objfile));
18599
18600 struct attribute *attr_ub, *attr_count;
18601 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18602 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18603 {
18604 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18605 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18606 {
18607 /* If bounds are constant do the final calculation here. */
18608 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18609 high.set_const_val (low.const_val () + high.const_val () - 1);
18610 else
18611 high_bound_is_count = 1;
18612 }
18613 else
18614 {
18615 if (attr_ub != NULL)
18616 complaint (_("Unresolved DW_AT_upper_bound "
18617 "- DIE at %s [in module %s]"),
18618 sect_offset_str (die->sect_off),
18619 objfile_name (cu->per_objfile->objfile));
18620 if (attr_count != NULL)
18621 complaint (_("Unresolved DW_AT_count "
18622 "- DIE at %s [in module %s]"),
18623 sect_offset_str (die->sect_off),
18624 objfile_name (cu->per_objfile->objfile));
18625 }
18626 }
18627
18628 LONGEST bias = 0;
18629 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18630 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18631 bias = bias_attr->constant_value (0);
18632
18633 /* Normally, the DWARF producers are expected to use a signed
18634 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18635 But this is unfortunately not always the case, as witnessed
18636 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18637 is used instead. To work around that ambiguity, we treat
18638 the bounds as signed, and thus sign-extend their values, when
18639 the base type is signed. */
18640 negative_mask =
18641 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18642 if (low.kind () == PROP_CONST
18643 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18644 low.set_const_val (low.const_val () | negative_mask);
18645 if (high.kind () == PROP_CONST
18646 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18647 high.set_const_val (high.const_val () | negative_mask);
18648
18649 /* Check for bit and byte strides. */
18650 struct dynamic_prop byte_stride_prop;
18651 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18652 if (attr_byte_stride != nullptr)
18653 {
18654 struct type *prop_type = cu->addr_sized_int_type (false);
18655 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18656 prop_type);
18657 }
18658
18659 struct dynamic_prop bit_stride_prop;
18660 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18661 if (attr_bit_stride != nullptr)
18662 {
18663 /* It only makes sense to have either a bit or byte stride. */
18664 if (attr_byte_stride != nullptr)
18665 {
18666 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18667 "- DIE at %s [in module %s]"),
18668 sect_offset_str (die->sect_off),
18669 objfile_name (cu->per_objfile->objfile));
18670 attr_bit_stride = nullptr;
18671 }
18672 else
18673 {
18674 struct type *prop_type = cu->addr_sized_int_type (false);
18675 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18676 prop_type);
18677 }
18678 }
18679
18680 if (attr_byte_stride != nullptr
18681 || attr_bit_stride != nullptr)
18682 {
18683 bool byte_stride_p = (attr_byte_stride != nullptr);
18684 struct dynamic_prop *stride
18685 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18686
18687 range_type
18688 = create_range_type_with_stride (NULL, orig_base_type, &low,
18689 &high, bias, stride, byte_stride_p);
18690 }
18691 else
18692 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18693
18694 if (high_bound_is_count)
18695 range_type->bounds ()->flag_upper_bound_is_count = 1;
18696
18697 /* Ada expects an empty array on no boundary attributes. */
18698 if (attr == NULL && cu->per_cu->lang != language_ada)
18699 range_type->bounds ()->high.set_undefined ();
18700
18701 name = dwarf2_name (die, cu);
18702 if (name)
18703 range_type->set_name (name);
18704
18705 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18706 if (attr != nullptr)
18707 TYPE_LENGTH (range_type) = attr->constant_value (0);
18708
18709 maybe_set_alignment (cu, die, range_type);
18710
18711 set_die_type (die, range_type, cu);
18712
18713 /* set_die_type should be already done. */
18714 set_descriptive_type (range_type, die, cu);
18715
18716 return range_type;
18717 }
18718
18719 static struct type *
18720 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18721 {
18722 struct type *type;
18723
18724 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18725 type->set_name (dwarf2_name (die, cu));
18726
18727 /* In Ada, an unspecified type is typically used when the description
18728 of the type is deferred to a different unit. When encountering
18729 such a type, we treat it as a stub, and try to resolve it later on,
18730 when needed. */
18731 if (cu->per_cu->lang == language_ada)
18732 type->set_is_stub (true);
18733
18734 return set_die_type (die, type, cu);
18735 }
18736
18737 /* Read a single die and all its descendents. Set the die's sibling
18738 field to NULL; set other fields in the die correctly, and set all
18739 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18740 location of the info_ptr after reading all of those dies. PARENT
18741 is the parent of the die in question. */
18742
18743 static struct die_info *
18744 read_die_and_children (const struct die_reader_specs *reader,
18745 const gdb_byte *info_ptr,
18746 const gdb_byte **new_info_ptr,
18747 struct die_info *parent)
18748 {
18749 struct die_info *die;
18750 const gdb_byte *cur_ptr;
18751
18752 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18753 if (die == NULL)
18754 {
18755 *new_info_ptr = cur_ptr;
18756 return NULL;
18757 }
18758 store_in_ref_table (die, reader->cu);
18759
18760 if (die->has_children)
18761 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18762 else
18763 {
18764 die->child = NULL;
18765 *new_info_ptr = cur_ptr;
18766 }
18767
18768 die->sibling = NULL;
18769 die->parent = parent;
18770 return die;
18771 }
18772
18773 /* Read a die, all of its descendents, and all of its siblings; set
18774 all of the fields of all of the dies correctly. Arguments are as
18775 in read_die_and_children. */
18776
18777 static struct die_info *
18778 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18779 const gdb_byte *info_ptr,
18780 const gdb_byte **new_info_ptr,
18781 struct die_info *parent)
18782 {
18783 struct die_info *first_die, *last_sibling;
18784 const gdb_byte *cur_ptr;
18785
18786 cur_ptr = info_ptr;
18787 first_die = last_sibling = NULL;
18788
18789 while (1)
18790 {
18791 struct die_info *die
18792 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18793
18794 if (die == NULL)
18795 {
18796 *new_info_ptr = cur_ptr;
18797 return first_die;
18798 }
18799
18800 if (!first_die)
18801 first_die = die;
18802 else
18803 last_sibling->sibling = die;
18804
18805 last_sibling = die;
18806 }
18807 }
18808
18809 /* Read a die, all of its descendents, and all of its siblings; set
18810 all of the fields of all of the dies correctly. Arguments are as
18811 in read_die_and_children.
18812 This the main entry point for reading a DIE and all its children. */
18813
18814 static struct die_info *
18815 read_die_and_siblings (const struct die_reader_specs *reader,
18816 const gdb_byte *info_ptr,
18817 const gdb_byte **new_info_ptr,
18818 struct die_info *parent)
18819 {
18820 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18821 new_info_ptr, parent);
18822
18823 if (dwarf_die_debug)
18824 {
18825 fprintf_unfiltered (gdb_stdlog,
18826 "Read die from %s@0x%x of %s:\n",
18827 reader->die_section->get_name (),
18828 (unsigned) (info_ptr - reader->die_section->buffer),
18829 bfd_get_filename (reader->abfd));
18830 dump_die (die, dwarf_die_debug);
18831 }
18832
18833 return die;
18834 }
18835
18836 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18837 attributes.
18838 The caller is responsible for filling in the extra attributes
18839 and updating (*DIEP)->num_attrs.
18840 Set DIEP to point to a newly allocated die with its information,
18841 except for its child, sibling, and parent fields. */
18842
18843 static const gdb_byte *
18844 read_full_die_1 (const struct die_reader_specs *reader,
18845 struct die_info **diep, const gdb_byte *info_ptr,
18846 int num_extra_attrs)
18847 {
18848 unsigned int abbrev_number, bytes_read, i;
18849 const struct abbrev_info *abbrev;
18850 struct die_info *die;
18851 struct dwarf2_cu *cu = reader->cu;
18852 bfd *abfd = reader->abfd;
18853
18854 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18855 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18856 info_ptr += bytes_read;
18857 if (!abbrev_number)
18858 {
18859 *diep = NULL;
18860 return info_ptr;
18861 }
18862
18863 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18864 if (!abbrev)
18865 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18866 abbrev_number,
18867 bfd_get_filename (abfd));
18868
18869 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18870 die->sect_off = sect_off;
18871 die->tag = abbrev->tag;
18872 die->abbrev = abbrev_number;
18873 die->has_children = abbrev->has_children;
18874
18875 /* Make the result usable.
18876 The caller needs to update num_attrs after adding the extra
18877 attributes. */
18878 die->num_attrs = abbrev->num_attrs;
18879
18880 bool any_need_reprocess = false;
18881 for (i = 0; i < abbrev->num_attrs; ++i)
18882 {
18883 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18884 info_ptr);
18885 if (die->attrs[i].requires_reprocessing_p ())
18886 any_need_reprocess = true;
18887 }
18888
18889 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18890 if (attr != nullptr && attr->form_is_unsigned ())
18891 cu->str_offsets_base = attr->as_unsigned ();
18892
18893 attr = die->attr (DW_AT_loclists_base);
18894 if (attr != nullptr)
18895 cu->loclist_base = attr->as_unsigned ();
18896
18897 auto maybe_addr_base = die->addr_base ();
18898 if (maybe_addr_base.has_value ())
18899 cu->addr_base = *maybe_addr_base;
18900
18901 attr = die->attr (DW_AT_rnglists_base);
18902 if (attr != nullptr)
18903 cu->rnglists_base = attr->as_unsigned ();
18904
18905 if (any_need_reprocess)
18906 {
18907 for (i = 0; i < abbrev->num_attrs; ++i)
18908 {
18909 if (die->attrs[i].requires_reprocessing_p ())
18910 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18911 }
18912 }
18913 *diep = die;
18914 return info_ptr;
18915 }
18916
18917 /* Read a die and all its attributes.
18918 Set DIEP to point to a newly allocated die with its information,
18919 except for its child, sibling, and parent fields. */
18920
18921 static const gdb_byte *
18922 read_full_die (const struct die_reader_specs *reader,
18923 struct die_info **diep, const gdb_byte *info_ptr)
18924 {
18925 const gdb_byte *result;
18926
18927 result = read_full_die_1 (reader, diep, info_ptr, 0);
18928
18929 if (dwarf_die_debug)
18930 {
18931 fprintf_unfiltered (gdb_stdlog,
18932 "Read die from %s@0x%x of %s:\n",
18933 reader->die_section->get_name (),
18934 (unsigned) (info_ptr - reader->die_section->buffer),
18935 bfd_get_filename (reader->abfd));
18936 dump_die (*diep, dwarf_die_debug);
18937 }
18938
18939 return result;
18940 }
18941 \f
18942
18943 /* Returns nonzero if TAG represents a type that we might generate a partial
18944 symbol for. */
18945
18946 static int
18947 is_type_tag_for_partial (int tag, enum language lang)
18948 {
18949 switch (tag)
18950 {
18951 #if 0
18952 /* Some types that would be reasonable to generate partial symbols for,
18953 that we don't at present. Note that normally this does not
18954 matter, mainly because C compilers don't give names to these
18955 types, but instead emit DW_TAG_typedef. */
18956 case DW_TAG_file_type:
18957 case DW_TAG_ptr_to_member_type:
18958 case DW_TAG_set_type:
18959 case DW_TAG_string_type:
18960 case DW_TAG_subroutine_type:
18961 #endif
18962
18963 /* GNAT may emit an array with a name, but no typedef, so we
18964 need to make a symbol in this case. */
18965 case DW_TAG_array_type:
18966 return lang == language_ada;
18967
18968 case DW_TAG_base_type:
18969 case DW_TAG_class_type:
18970 case DW_TAG_interface_type:
18971 case DW_TAG_enumeration_type:
18972 case DW_TAG_structure_type:
18973 case DW_TAG_subrange_type:
18974 case DW_TAG_typedef:
18975 case DW_TAG_union_type:
18976 return 1;
18977 default:
18978 return 0;
18979 }
18980 }
18981
18982 /* Load all DIEs that are interesting for partial symbols into memory. */
18983
18984 static struct partial_die_info *
18985 load_partial_dies (const struct die_reader_specs *reader,
18986 const gdb_byte *info_ptr, int building_psymtab)
18987 {
18988 struct dwarf2_cu *cu = reader->cu;
18989 struct objfile *objfile = cu->per_objfile->objfile;
18990 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18991 unsigned int bytes_read;
18992 unsigned int load_all = 0;
18993 int nesting_level = 1;
18994
18995 parent_die = NULL;
18996 last_die = NULL;
18997
18998 gdb_assert (cu->per_cu != NULL);
18999 if (cu->load_all_dies)
19000 load_all = 1;
19001
19002 cu->partial_dies
19003 = htab_create_alloc_ex (cu->header.length / 12,
19004 partial_die_hash,
19005 partial_die_eq,
19006 NULL,
19007 &cu->comp_unit_obstack,
19008 hashtab_obstack_allocate,
19009 dummy_obstack_deallocate);
19010
19011 while (1)
19012 {
19013 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19014 &bytes_read);
19015
19016 /* A NULL abbrev means the end of a series of children. */
19017 if (abbrev == NULL)
19018 {
19019 if (--nesting_level == 0)
19020 return first_die;
19021
19022 info_ptr += bytes_read;
19023 last_die = parent_die;
19024 parent_die = parent_die->die_parent;
19025 continue;
19026 }
19027
19028 /* Check for template arguments. We never save these; if
19029 they're seen, we just mark the parent, and go on our way. */
19030 if (parent_die != NULL
19031 && cu->per_cu->lang == language_cplus
19032 && (abbrev->tag == DW_TAG_template_type_param
19033 || abbrev->tag == DW_TAG_template_value_param))
19034 {
19035 parent_die->has_template_arguments = 1;
19036
19037 if (!load_all)
19038 {
19039 /* We don't need a partial DIE for the template argument. */
19040 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19041 continue;
19042 }
19043 }
19044
19045 /* We only recurse into c++ subprograms looking for template arguments.
19046 Skip their other children. */
19047 if (!load_all
19048 && cu->per_cu->lang == language_cplus
19049 && parent_die != NULL
19050 && parent_die->tag == DW_TAG_subprogram
19051 && abbrev->tag != DW_TAG_inlined_subroutine)
19052 {
19053 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19054 continue;
19055 }
19056
19057 /* Check whether this DIE is interesting enough to save. Normally
19058 we would not be interested in members here, but there may be
19059 later variables referencing them via DW_AT_specification (for
19060 static members). */
19061 if (!load_all
19062 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
19063 && abbrev->tag != DW_TAG_constant
19064 && abbrev->tag != DW_TAG_enumerator
19065 && abbrev->tag != DW_TAG_subprogram
19066 && abbrev->tag != DW_TAG_inlined_subroutine
19067 && abbrev->tag != DW_TAG_lexical_block
19068 && abbrev->tag != DW_TAG_variable
19069 && abbrev->tag != DW_TAG_namespace
19070 && abbrev->tag != DW_TAG_module
19071 && abbrev->tag != DW_TAG_member
19072 && abbrev->tag != DW_TAG_imported_unit
19073 && abbrev->tag != DW_TAG_imported_declaration)
19074 {
19075 /* Otherwise we skip to the next sibling, if any. */
19076 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19077 continue;
19078 }
19079
19080 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19081 abbrev);
19082
19083 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19084
19085 /* This two-pass algorithm for processing partial symbols has a
19086 high cost in cache pressure. Thus, handle some simple cases
19087 here which cover the majority of C partial symbols. DIEs
19088 which neither have specification tags in them, nor could have
19089 specification tags elsewhere pointing at them, can simply be
19090 processed and discarded.
19091
19092 This segment is also optional; scan_partial_symbols and
19093 add_partial_symbol will handle these DIEs if we chain
19094 them in normally. When compilers which do not emit large
19095 quantities of duplicate debug information are more common,
19096 this code can probably be removed. */
19097
19098 /* Any complete simple types at the top level (pretty much all
19099 of them, for a language without namespaces), can be processed
19100 directly. */
19101 if (parent_die == NULL
19102 && pdi.has_specification == 0
19103 && pdi.is_declaration == 0
19104 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19105 || pdi.tag == DW_TAG_base_type
19106 || pdi.tag == DW_TAG_array_type
19107 || pdi.tag == DW_TAG_subrange_type))
19108 {
19109 if (building_psymtab && pdi.raw_name != NULL)
19110 add_partial_symbol (&pdi, cu);
19111
19112 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19113 continue;
19114 }
19115
19116 /* The exception for DW_TAG_typedef with has_children above is
19117 a workaround of GCC PR debug/47510. In the case of this complaint
19118 type_name_or_error will error on such types later.
19119
19120 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19121 it could not find the child DIEs referenced later, this is checked
19122 above. In correct DWARF DW_TAG_typedef should have no children. */
19123
19124 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19125 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19126 "- DIE at %s [in module %s]"),
19127 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19128
19129 /* If we're at the second level, and we're an enumerator, and
19130 our parent has no specification (meaning possibly lives in a
19131 namespace elsewhere), then we can add the partial symbol now
19132 instead of queueing it. */
19133 if (pdi.tag == DW_TAG_enumerator
19134 && parent_die != NULL
19135 && parent_die->die_parent == NULL
19136 && parent_die->tag == DW_TAG_enumeration_type
19137 && parent_die->has_specification == 0)
19138 {
19139 if (pdi.raw_name == NULL)
19140 complaint (_("malformed enumerator DIE ignored"));
19141 else if (building_psymtab)
19142 add_partial_symbol (&pdi, cu);
19143
19144 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19145 continue;
19146 }
19147
19148 struct partial_die_info *part_die
19149 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19150
19151 /* We'll save this DIE so link it in. */
19152 part_die->die_parent = parent_die;
19153 part_die->die_sibling = NULL;
19154 part_die->die_child = NULL;
19155
19156 if (last_die && last_die == parent_die)
19157 last_die->die_child = part_die;
19158 else if (last_die)
19159 last_die->die_sibling = part_die;
19160
19161 last_die = part_die;
19162
19163 if (first_die == NULL)
19164 first_die = part_die;
19165
19166 /* Maybe add the DIE to the hash table. Not all DIEs that we
19167 find interesting need to be in the hash table, because we
19168 also have the parent/sibling/child chains; only those that we
19169 might refer to by offset later during partial symbol reading.
19170
19171 For now this means things that might have be the target of a
19172 DW_AT_specification, DW_AT_abstract_origin, or
19173 DW_AT_extension. DW_AT_extension will refer only to
19174 namespaces; DW_AT_abstract_origin refers to functions (and
19175 many things under the function DIE, but we do not recurse
19176 into function DIEs during partial symbol reading) and
19177 possibly variables as well; DW_AT_specification refers to
19178 declarations. Declarations ought to have the DW_AT_declaration
19179 flag. It happens that GCC forgets to put it in sometimes, but
19180 only for functions, not for types.
19181
19182 Adding more things than necessary to the hash table is harmless
19183 except for the performance cost. Adding too few will result in
19184 wasted time in find_partial_die, when we reread the compilation
19185 unit with load_all_dies set. */
19186
19187 if (load_all
19188 || abbrev->tag == DW_TAG_constant
19189 || abbrev->tag == DW_TAG_subprogram
19190 || abbrev->tag == DW_TAG_variable
19191 || abbrev->tag == DW_TAG_namespace
19192 || part_die->is_declaration)
19193 {
19194 void **slot;
19195
19196 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19197 to_underlying (part_die->sect_off),
19198 INSERT);
19199 *slot = part_die;
19200 }
19201
19202 /* For some DIEs we want to follow their children (if any). For C
19203 we have no reason to follow the children of structures; for other
19204 languages we have to, so that we can get at method physnames
19205 to infer fully qualified class names, for DW_AT_specification,
19206 and for C++ template arguments. For C++, we also look one level
19207 inside functions to find template arguments (if the name of the
19208 function does not already contain the template arguments).
19209
19210 For Ada and Fortran, we need to scan the children of subprograms
19211 and lexical blocks as well because these languages allow the
19212 definition of nested entities that could be interesting for the
19213 debugger, such as nested subprograms for instance. */
19214 if (last_die->has_children
19215 && (load_all
19216 || last_die->tag == DW_TAG_namespace
19217 || last_die->tag == DW_TAG_module
19218 || last_die->tag == DW_TAG_enumeration_type
19219 || (cu->per_cu->lang == language_cplus
19220 && last_die->tag == DW_TAG_subprogram
19221 && (last_die->raw_name == NULL
19222 || strchr (last_die->raw_name, '<') == NULL))
19223 || (cu->per_cu->lang != language_c
19224 && (last_die->tag == DW_TAG_class_type
19225 || last_die->tag == DW_TAG_interface_type
19226 || last_die->tag == DW_TAG_structure_type
19227 || last_die->tag == DW_TAG_union_type))
19228 || ((cu->per_cu->lang == language_ada
19229 || cu->per_cu->lang == language_fortran)
19230 && (last_die->tag == DW_TAG_subprogram
19231 || last_die->tag == DW_TAG_lexical_block))))
19232 {
19233 nesting_level++;
19234 parent_die = last_die;
19235 continue;
19236 }
19237
19238 /* Otherwise we skip to the next sibling, if any. */
19239 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19240
19241 /* Back to the top, do it again. */
19242 }
19243 }
19244
19245 partial_die_info::partial_die_info (sect_offset sect_off_,
19246 const struct abbrev_info *abbrev)
19247 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19248 {
19249 }
19250
19251 /* See class definition. */
19252
19253 const char *
19254 partial_die_info::name (dwarf2_cu *cu)
19255 {
19256 if (!canonical_name && raw_name != nullptr)
19257 {
19258 struct objfile *objfile = cu->per_objfile->objfile;
19259 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19260 canonical_name = 1;
19261 }
19262
19263 return raw_name;
19264 }
19265
19266 /* Read a minimal amount of information into the minimal die structure.
19267 INFO_PTR should point just after the initial uleb128 of a DIE. */
19268
19269 const gdb_byte *
19270 partial_die_info::read (const struct die_reader_specs *reader,
19271 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19272 {
19273 struct dwarf2_cu *cu = reader->cu;
19274 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19275 unsigned int i;
19276 int has_low_pc_attr = 0;
19277 int has_high_pc_attr = 0;
19278 int high_pc_relative = 0;
19279
19280 for (i = 0; i < abbrev.num_attrs; ++i)
19281 {
19282 attribute attr;
19283 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19284 /* String and address offsets that need to do the reprocessing have
19285 already been read at this point, so there is no need to wait until
19286 the loop terminates to do the reprocessing. */
19287 if (attr.requires_reprocessing_p ())
19288 read_attribute_reprocess (reader, &attr, tag);
19289 /* Store the data if it is of an attribute we want to keep in a
19290 partial symbol table. */
19291 switch (attr.name)
19292 {
19293 case DW_AT_name:
19294 switch (tag)
19295 {
19296 case DW_TAG_compile_unit:
19297 case DW_TAG_partial_unit:
19298 case DW_TAG_type_unit:
19299 /* Compilation units have a DW_AT_name that is a filename, not
19300 a source language identifier. */
19301 case DW_TAG_enumeration_type:
19302 case DW_TAG_enumerator:
19303 /* These tags always have simple identifiers already; no need
19304 to canonicalize them. */
19305 canonical_name = 1;
19306 raw_name = attr.as_string ();
19307 break;
19308 default:
19309 canonical_name = 0;
19310 raw_name = attr.as_string ();
19311 break;
19312 }
19313 break;
19314 case DW_AT_linkage_name:
19315 case DW_AT_MIPS_linkage_name:
19316 /* Note that both forms of linkage name might appear. We
19317 assume they will be the same, and we only store the last
19318 one we see. */
19319 linkage_name = attr.as_string ();
19320 break;
19321 case DW_AT_low_pc:
19322 has_low_pc_attr = 1;
19323 lowpc = attr.as_address ();
19324 break;
19325 case DW_AT_high_pc:
19326 has_high_pc_attr = 1;
19327 highpc = attr.as_address ();
19328 if (cu->header.version >= 4 && attr.form_is_constant ())
19329 high_pc_relative = 1;
19330 break;
19331 case DW_AT_location:
19332 /* Support the .debug_loc offsets. */
19333 if (attr.form_is_block ())
19334 {
19335 d.locdesc = attr.as_block ();
19336 }
19337 else if (attr.form_is_section_offset ())
19338 {
19339 dwarf2_complex_location_expr_complaint ();
19340 }
19341 else
19342 {
19343 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19344 "partial symbol information");
19345 }
19346 break;
19347 case DW_AT_external:
19348 is_external = attr.as_boolean ();
19349 break;
19350 case DW_AT_declaration:
19351 is_declaration = attr.as_boolean ();
19352 break;
19353 case DW_AT_type:
19354 has_type = 1;
19355 break;
19356 case DW_AT_abstract_origin:
19357 case DW_AT_specification:
19358 case DW_AT_extension:
19359 has_specification = 1;
19360 spec_offset = attr.get_ref_die_offset ();
19361 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19362 || cu->per_cu->is_dwz);
19363 break;
19364 case DW_AT_sibling:
19365 /* Ignore absolute siblings, they might point outside of
19366 the current compile unit. */
19367 if (attr.form == DW_FORM_ref_addr)
19368 complaint (_("ignoring absolute DW_AT_sibling"));
19369 else
19370 {
19371 const gdb_byte *buffer = reader->buffer;
19372 sect_offset off = attr.get_ref_die_offset ();
19373 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19374
19375 if (sibling_ptr < info_ptr)
19376 complaint (_("DW_AT_sibling points backwards"));
19377 else if (sibling_ptr > reader->buffer_end)
19378 reader->die_section->overflow_complaint ();
19379 else
19380 sibling = sibling_ptr;
19381 }
19382 break;
19383 case DW_AT_byte_size:
19384 has_byte_size = 1;
19385 break;
19386 case DW_AT_const_value:
19387 has_const_value = 1;
19388 break;
19389 case DW_AT_calling_convention:
19390 /* DWARF doesn't provide a way to identify a program's source-level
19391 entry point. DW_AT_calling_convention attributes are only meant
19392 to describe functions' calling conventions.
19393
19394 However, because it's a necessary piece of information in
19395 Fortran, and before DWARF 4 DW_CC_program was the only
19396 piece of debugging information whose definition refers to
19397 a 'main program' at all, several compilers marked Fortran
19398 main programs with DW_CC_program --- even when those
19399 functions use the standard calling conventions.
19400
19401 Although DWARF now specifies a way to provide this
19402 information, we support this practice for backward
19403 compatibility. */
19404 if (attr.constant_value (0) == DW_CC_program
19405 && cu->per_cu->lang == language_fortran)
19406 main_subprogram = 1;
19407 break;
19408 case DW_AT_inline:
19409 {
19410 LONGEST value = attr.constant_value (-1);
19411 if (value == DW_INL_inlined
19412 || value == DW_INL_declared_inlined)
19413 may_be_inlined = 1;
19414 }
19415 break;
19416
19417 case DW_AT_import:
19418 if (tag == DW_TAG_imported_unit)
19419 {
19420 d.sect_off = attr.get_ref_die_offset ();
19421 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19422 || cu->per_cu->is_dwz);
19423 }
19424 break;
19425
19426 case DW_AT_main_subprogram:
19427 main_subprogram = attr.as_boolean ();
19428 break;
19429
19430 case DW_AT_ranges:
19431 {
19432 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19433 on DWARF version). */
19434 ranges_offset = attr.as_unsigned ();
19435
19436 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19437 this value. */
19438 if (tag != DW_TAG_compile_unit)
19439 ranges_offset += cu->gnu_ranges_base;
19440
19441 has_range_info = 1;
19442 }
19443 break;
19444
19445 default:
19446 break;
19447 }
19448 }
19449
19450 /* For Ada, if both the name and the linkage name appear, we prefer
19451 the latter. This lets "catch exception" work better, regardless
19452 of the order in which the name and linkage name were emitted.
19453 Really, though, this is just a workaround for the fact that gdb
19454 doesn't store both the name and the linkage name. */
19455 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
19456 raw_name = linkage_name;
19457
19458 if (high_pc_relative)
19459 highpc += lowpc;
19460
19461 if (has_low_pc_attr && has_high_pc_attr)
19462 {
19463 /* When using the GNU linker, .gnu.linkonce. sections are used to
19464 eliminate duplicate copies of functions and vtables and such.
19465 The linker will arbitrarily choose one and discard the others.
19466 The AT_*_pc values for such functions refer to local labels in
19467 these sections. If the section from that file was discarded, the
19468 labels are not in the output, so the relocs get a value of 0.
19469 If this is a discarded function, mark the pc bounds as invalid,
19470 so that GDB will ignore it. */
19471 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19472 {
19473 struct objfile *objfile = per_objfile->objfile;
19474 struct gdbarch *gdbarch = objfile->arch ();
19475
19476 complaint (_("DW_AT_low_pc %s is zero "
19477 "for DIE at %s [in module %s]"),
19478 paddress (gdbarch, lowpc),
19479 sect_offset_str (sect_off),
19480 objfile_name (objfile));
19481 }
19482 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19483 else if (lowpc >= highpc)
19484 {
19485 struct objfile *objfile = per_objfile->objfile;
19486 struct gdbarch *gdbarch = objfile->arch ();
19487
19488 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19489 "for DIE at %s [in module %s]"),
19490 paddress (gdbarch, lowpc),
19491 paddress (gdbarch, highpc),
19492 sect_offset_str (sect_off),
19493 objfile_name (objfile));
19494 }
19495 else
19496 has_pc_info = 1;
19497 }
19498
19499 return info_ptr;
19500 }
19501
19502 /* Find a cached partial DIE at OFFSET in CU. */
19503
19504 struct partial_die_info *
19505 dwarf2_cu::find_partial_die (sect_offset sect_off)
19506 {
19507 struct partial_die_info *lookup_die = NULL;
19508 struct partial_die_info part_die (sect_off);
19509
19510 lookup_die = ((struct partial_die_info *)
19511 htab_find_with_hash (partial_dies, &part_die,
19512 to_underlying (sect_off)));
19513
19514 return lookup_die;
19515 }
19516
19517 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19518 except in the case of .debug_types DIEs which do not reference
19519 outside their CU (they do however referencing other types via
19520 DW_FORM_ref_sig8). */
19521
19522 static const struct cu_partial_die_info
19523 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19524 {
19525 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19526 struct objfile *objfile = per_objfile->objfile;
19527 struct partial_die_info *pd = NULL;
19528
19529 if (offset_in_dwz == cu->per_cu->is_dwz
19530 && cu->header.offset_in_cu_p (sect_off))
19531 {
19532 pd = cu->find_partial_die (sect_off);
19533 if (pd != NULL)
19534 return { cu, pd };
19535 /* We missed recording what we needed.
19536 Load all dies and try again. */
19537 }
19538 else
19539 {
19540 /* TUs don't reference other CUs/TUs (except via type signatures). */
19541 if (cu->per_cu->is_debug_types)
19542 {
19543 error (_("Dwarf Error: Type Unit at offset %s contains"
19544 " external reference to offset %s [in module %s].\n"),
19545 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19546 bfd_get_filename (objfile->obfd));
19547 }
19548 dwarf2_per_cu_data *per_cu
19549 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19550 per_objfile);
19551
19552 cu = per_objfile->get_cu (per_cu);
19553 if (cu == NULL || cu->partial_dies == NULL)
19554 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19555
19556 cu = per_objfile->get_cu (per_cu);
19557
19558 cu->last_used = 0;
19559 pd = cu->find_partial_die (sect_off);
19560 }
19561
19562 /* If we didn't find it, and not all dies have been loaded,
19563 load them all and try again. */
19564
19565 if (pd == NULL && cu->load_all_dies == 0)
19566 {
19567 cu->load_all_dies = 1;
19568
19569 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19570 THIS_CU->cu may already be in use. So we can't just free it and
19571 replace its DIEs with the ones we read in. Instead, we leave those
19572 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19573 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19574 set. */
19575 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19576
19577 pd = cu->find_partial_die (sect_off);
19578 }
19579
19580 if (pd == NULL)
19581 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19582 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19583 return { cu, pd };
19584 }
19585
19586 /* See if we can figure out if the class lives in a namespace. We do
19587 this by looking for a member function; its demangled name will
19588 contain namespace info, if there is any. */
19589
19590 static void
19591 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19592 struct dwarf2_cu *cu)
19593 {
19594 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19595 what template types look like, because the demangler
19596 frequently doesn't give the same name as the debug info. We
19597 could fix this by only using the demangled name to get the
19598 prefix (but see comment in read_structure_type). */
19599
19600 struct partial_die_info *real_pdi;
19601 struct partial_die_info *child_pdi;
19602
19603 /* If this DIE (this DIE's specification, if any) has a parent, then
19604 we should not do this. We'll prepend the parent's fully qualified
19605 name when we create the partial symbol. */
19606
19607 real_pdi = struct_pdi;
19608 while (real_pdi->has_specification)
19609 {
19610 auto res = find_partial_die (real_pdi->spec_offset,
19611 real_pdi->spec_is_dwz, cu);
19612 real_pdi = res.pdi;
19613 cu = res.cu;
19614 }
19615
19616 if (real_pdi->die_parent != NULL)
19617 return;
19618
19619 for (child_pdi = struct_pdi->die_child;
19620 child_pdi != NULL;
19621 child_pdi = child_pdi->die_sibling)
19622 {
19623 if (child_pdi->tag == DW_TAG_subprogram
19624 && child_pdi->linkage_name != NULL)
19625 {
19626 gdb::unique_xmalloc_ptr<char> actual_class_name
19627 (cu->language_defn->class_name_from_physname
19628 (child_pdi->linkage_name));
19629 if (actual_class_name != NULL)
19630 {
19631 struct objfile *objfile = cu->per_objfile->objfile;
19632 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19633 struct_pdi->canonical_name = 1;
19634 }
19635 break;
19636 }
19637 }
19638 }
19639
19640 /* Return true if a DIE with TAG may have the DW_AT_const_value
19641 attribute. */
19642
19643 static bool
19644 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19645 {
19646 switch (tag)
19647 {
19648 case DW_TAG_constant:
19649 case DW_TAG_enumerator:
19650 case DW_TAG_formal_parameter:
19651 case DW_TAG_template_value_param:
19652 case DW_TAG_variable:
19653 return true;
19654 }
19655
19656 return false;
19657 }
19658
19659 void
19660 partial_die_info::fixup (struct dwarf2_cu *cu)
19661 {
19662 /* Once we've fixed up a die, there's no point in doing so again.
19663 This also avoids a memory leak if we were to call
19664 guess_partial_die_structure_name multiple times. */
19665 if (fixup_called)
19666 return;
19667
19668 /* If we found a reference attribute and the DIE has no name, try
19669 to find a name in the referred to DIE. */
19670
19671 if (raw_name == NULL && has_specification)
19672 {
19673 struct partial_die_info *spec_die;
19674
19675 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19676 spec_die = res.pdi;
19677 cu = res.cu;
19678
19679 spec_die->fixup (cu);
19680
19681 if (spec_die->raw_name)
19682 {
19683 raw_name = spec_die->raw_name;
19684 canonical_name = spec_die->canonical_name;
19685
19686 /* Copy DW_AT_external attribute if it is set. */
19687 if (spec_die->is_external)
19688 is_external = spec_die->is_external;
19689 }
19690 }
19691
19692 if (!has_const_value && has_specification
19693 && can_have_DW_AT_const_value_p (tag))
19694 {
19695 struct partial_die_info *spec_die;
19696
19697 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19698 spec_die = res.pdi;
19699 cu = res.cu;
19700
19701 spec_die->fixup (cu);
19702
19703 if (spec_die->has_const_value)
19704 {
19705 /* Copy DW_AT_const_value attribute if it is set. */
19706 has_const_value = spec_die->has_const_value;
19707 }
19708 }
19709
19710 /* Set default names for some unnamed DIEs. */
19711
19712 if (raw_name == NULL && tag == DW_TAG_namespace)
19713 {
19714 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19715 canonical_name = 1;
19716 }
19717
19718 /* If there is no parent die to provide a namespace, and there are
19719 children, see if we can determine the namespace from their linkage
19720 name. */
19721 if (cu->per_cu->lang == language_cplus
19722 && !cu->per_objfile->per_bfd->types.empty ()
19723 && die_parent == NULL
19724 && has_children
19725 && (tag == DW_TAG_class_type
19726 || tag == DW_TAG_structure_type
19727 || tag == DW_TAG_union_type))
19728 guess_partial_die_structure_name (this, cu);
19729
19730 /* GCC might emit a nameless struct or union that has a linkage
19731 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19732 if (raw_name == NULL
19733 && (tag == DW_TAG_class_type
19734 || tag == DW_TAG_interface_type
19735 || tag == DW_TAG_structure_type
19736 || tag == DW_TAG_union_type)
19737 && linkage_name != NULL)
19738 {
19739 gdb::unique_xmalloc_ptr<char> demangled
19740 (gdb_demangle (linkage_name, DMGL_TYPES));
19741 if (demangled != nullptr)
19742 {
19743 const char *base;
19744
19745 /* Strip any leading namespaces/classes, keep only the base name.
19746 DW_AT_name for named DIEs does not contain the prefixes. */
19747 base = strrchr (demangled.get (), ':');
19748 if (base && base > demangled.get () && base[-1] == ':')
19749 base++;
19750 else
19751 base = demangled.get ();
19752
19753 struct objfile *objfile = cu->per_objfile->objfile;
19754 raw_name = objfile->intern (base);
19755 canonical_name = 1;
19756 }
19757 }
19758
19759 fixup_called = 1;
19760 }
19761
19762 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19763 contents from the given SECTION in the HEADER.
19764
19765 HEADER_OFFSET is the offset of the header in the section. */
19766 static void
19767 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19768 struct dwarf2_section_info *section,
19769 sect_offset header_offset)
19770 {
19771 unsigned int bytes_read;
19772 bfd *abfd = section->get_bfd_owner ();
19773 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19774
19775 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19776 info_ptr += bytes_read;
19777
19778 header->version = read_2_bytes (abfd, info_ptr);
19779 info_ptr += 2;
19780
19781 header->addr_size = read_1_byte (abfd, info_ptr);
19782 info_ptr += 1;
19783
19784 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19785 info_ptr += 1;
19786
19787 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19788 }
19789
19790 /* Return the DW_AT_loclists_base value for the CU. */
19791 static ULONGEST
19792 lookup_loclist_base (struct dwarf2_cu *cu)
19793 {
19794 /* For the .dwo unit, the loclist_base points to the first offset following
19795 the header. The header consists of the following entities-
19796 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19797 bit format)
19798 2. version (2 bytes)
19799 3. address size (1 byte)
19800 4. segment selector size (1 byte)
19801 5. offset entry count (4 bytes)
19802 These sizes are derived as per the DWARFv5 standard. */
19803 if (cu->dwo_unit != nullptr)
19804 {
19805 if (cu->header.initial_length_size == 4)
19806 return LOCLIST_HEADER_SIZE32;
19807 return LOCLIST_HEADER_SIZE64;
19808 }
19809 return cu->loclist_base;
19810 }
19811
19812 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19813 array of offsets in the .debug_loclists section. */
19814
19815 static sect_offset
19816 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19817 {
19818 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19819 struct objfile *objfile = per_objfile->objfile;
19820 bfd *abfd = objfile->obfd;
19821 ULONGEST loclist_header_size =
19822 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19823 : LOCLIST_HEADER_SIZE64);
19824 ULONGEST loclist_base = lookup_loclist_base (cu);
19825
19826 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19827 ULONGEST start_offset =
19828 loclist_base + loclist_index * cu->header.offset_size;
19829
19830 /* Get loclists section. */
19831 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19832
19833 /* Read the loclists section content. */
19834 section->read (objfile);
19835 if (section->buffer == NULL)
19836 error (_("DW_FORM_loclistx used without .debug_loclists "
19837 "section [in module %s]"), objfile_name (objfile));
19838
19839 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19840 so if loclist_base is smaller than the header size, we have a problem. */
19841 if (loclist_base < loclist_header_size)
19842 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19843 objfile_name (objfile));
19844
19845 /* Read the header of the loclists contribution. */
19846 struct loclists_rnglists_header header;
19847 read_loclists_rnglists_header (&header, section,
19848 (sect_offset) (loclist_base - loclist_header_size));
19849
19850 /* Verify the loclist index is valid. */
19851 if (loclist_index >= header.offset_entry_count)
19852 error (_("DW_FORM_loclistx pointing outside of "
19853 ".debug_loclists offset array [in module %s]"),
19854 objfile_name (objfile));
19855
19856 /* Validate that reading won't go beyond the end of the section. */
19857 if (start_offset + cu->header.offset_size > section->size)
19858 error (_("Reading DW_FORM_loclistx index beyond end of"
19859 ".debug_loclists section [in module %s]"),
19860 objfile_name (objfile));
19861
19862 const gdb_byte *info_ptr = section->buffer + start_offset;
19863
19864 if (cu->header.offset_size == 4)
19865 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19866 else
19867 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19868 }
19869
19870 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19871 array of offsets in the .debug_rnglists section. */
19872
19873 static sect_offset
19874 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19875 dwarf_tag tag)
19876 {
19877 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19878 struct objfile *objfile = dwarf2_per_objfile->objfile;
19879 bfd *abfd = objfile->obfd;
19880 ULONGEST rnglist_header_size =
19881 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19882 : RNGLIST_HEADER_SIZE64);
19883
19884 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19885 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19886 doesn't apply. */
19887 ULONGEST rnglist_base =
19888 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19889
19890 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19891 ULONGEST start_offset =
19892 rnglist_base + rnglist_index * cu->header.offset_size;
19893
19894 /* Get rnglists section. */
19895 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19896
19897 /* Read the rnglists section content. */
19898 section->read (objfile);
19899 if (section->buffer == nullptr)
19900 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19901 "[in module %s]"),
19902 objfile_name (objfile));
19903
19904 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19905 so if rnglist_base is smaller than the header size, we have a problem. */
19906 if (rnglist_base < rnglist_header_size)
19907 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19908 objfile_name (objfile));
19909
19910 /* Read the header of the rnglists contribution. */
19911 struct loclists_rnglists_header header;
19912 read_loclists_rnglists_header (&header, section,
19913 (sect_offset) (rnglist_base - rnglist_header_size));
19914
19915 /* Verify the rnglist index is valid. */
19916 if (rnglist_index >= header.offset_entry_count)
19917 error (_("DW_FORM_rnglistx index pointing outside of "
19918 ".debug_rnglists offset array [in module %s]"),
19919 objfile_name (objfile));
19920
19921 /* Validate that reading won't go beyond the end of the section. */
19922 if (start_offset + cu->header.offset_size > section->size)
19923 error (_("Reading DW_FORM_rnglistx index beyond end of"
19924 ".debug_rnglists section [in module %s]"),
19925 objfile_name (objfile));
19926
19927 const gdb_byte *info_ptr = section->buffer + start_offset;
19928
19929 if (cu->header.offset_size == 4)
19930 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19931 else
19932 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19933 }
19934
19935 /* Process the attributes that had to be skipped in the first round. These
19936 attributes are the ones that need str_offsets_base or addr_base attributes.
19937 They could not have been processed in the first round, because at the time
19938 the values of str_offsets_base or addr_base may not have been known. */
19939 static void
19940 read_attribute_reprocess (const struct die_reader_specs *reader,
19941 struct attribute *attr, dwarf_tag tag)
19942 {
19943 struct dwarf2_cu *cu = reader->cu;
19944 switch (attr->form)
19945 {
19946 case DW_FORM_addrx:
19947 case DW_FORM_GNU_addr_index:
19948 attr->set_address (read_addr_index (cu,
19949 attr->as_unsigned_reprocess ()));
19950 break;
19951 case DW_FORM_loclistx:
19952 {
19953 sect_offset loclists_sect_off
19954 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19955
19956 attr->set_unsigned (to_underlying (loclists_sect_off));
19957 }
19958 break;
19959 case DW_FORM_rnglistx:
19960 {
19961 sect_offset rnglists_sect_off
19962 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19963
19964 attr->set_unsigned (to_underlying (rnglists_sect_off));
19965 }
19966 break;
19967 case DW_FORM_strx:
19968 case DW_FORM_strx1:
19969 case DW_FORM_strx2:
19970 case DW_FORM_strx3:
19971 case DW_FORM_strx4:
19972 case DW_FORM_GNU_str_index:
19973 {
19974 unsigned int str_index = attr->as_unsigned_reprocess ();
19975 gdb_assert (!attr->canonical_string_p ());
19976 if (reader->dwo_file != NULL)
19977 attr->set_string_noncanonical (read_dwo_str_index (reader,
19978 str_index));
19979 else
19980 attr->set_string_noncanonical (read_stub_str_index (cu,
19981 str_index));
19982 break;
19983 }
19984 default:
19985 gdb_assert_not_reached (_("Unexpected DWARF form."));
19986 }
19987 }
19988
19989 /* Read an attribute value described by an attribute form. */
19990
19991 static const gdb_byte *
19992 read_attribute_value (const struct die_reader_specs *reader,
19993 struct attribute *attr, unsigned form,
19994 LONGEST implicit_const, const gdb_byte *info_ptr)
19995 {
19996 struct dwarf2_cu *cu = reader->cu;
19997 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19998 struct objfile *objfile = per_objfile->objfile;
19999 bfd *abfd = reader->abfd;
20000 struct comp_unit_head *cu_header = &cu->header;
20001 unsigned int bytes_read;
20002 struct dwarf_block *blk;
20003
20004 attr->form = (enum dwarf_form) form;
20005 switch (form)
20006 {
20007 case DW_FORM_ref_addr:
20008 if (cu_header->version == 2)
20009 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20010 &bytes_read));
20011 else
20012 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20013 &bytes_read));
20014 info_ptr += bytes_read;
20015 break;
20016 case DW_FORM_GNU_ref_alt:
20017 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20018 &bytes_read));
20019 info_ptr += bytes_read;
20020 break;
20021 case DW_FORM_addr:
20022 {
20023 struct gdbarch *gdbarch = objfile->arch ();
20024 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20025 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20026 attr->set_address (addr);
20027 info_ptr += bytes_read;
20028 }
20029 break;
20030 case DW_FORM_block2:
20031 blk = dwarf_alloc_block (cu);
20032 blk->size = read_2_bytes (abfd, info_ptr);
20033 info_ptr += 2;
20034 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20035 info_ptr += blk->size;
20036 attr->set_block (blk);
20037 break;
20038 case DW_FORM_block4:
20039 blk = dwarf_alloc_block (cu);
20040 blk->size = read_4_bytes (abfd, info_ptr);
20041 info_ptr += 4;
20042 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20043 info_ptr += blk->size;
20044 attr->set_block (blk);
20045 break;
20046 case DW_FORM_data2:
20047 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20048 info_ptr += 2;
20049 break;
20050 case DW_FORM_data4:
20051 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20052 info_ptr += 4;
20053 break;
20054 case DW_FORM_data8:
20055 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20056 info_ptr += 8;
20057 break;
20058 case DW_FORM_data16:
20059 blk = dwarf_alloc_block (cu);
20060 blk->size = 16;
20061 blk->data = read_n_bytes (abfd, info_ptr, 16);
20062 info_ptr += 16;
20063 attr->set_block (blk);
20064 break;
20065 case DW_FORM_sec_offset:
20066 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20067 &bytes_read));
20068 info_ptr += bytes_read;
20069 break;
20070 case DW_FORM_loclistx:
20071 {
20072 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20073 &bytes_read));
20074 info_ptr += bytes_read;
20075 }
20076 break;
20077 case DW_FORM_string:
20078 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20079 &bytes_read));
20080 info_ptr += bytes_read;
20081 break;
20082 case DW_FORM_strp:
20083 if (!cu->per_cu->is_dwz)
20084 {
20085 attr->set_string_noncanonical
20086 (read_indirect_string (per_objfile,
20087 abfd, info_ptr, cu_header,
20088 &bytes_read));
20089 info_ptr += bytes_read;
20090 break;
20091 }
20092 /* FALLTHROUGH */
20093 case DW_FORM_line_strp:
20094 if (!cu->per_cu->is_dwz)
20095 {
20096 attr->set_string_noncanonical
20097 (per_objfile->read_line_string (info_ptr, cu_header,
20098 &bytes_read));
20099 info_ptr += bytes_read;
20100 break;
20101 }
20102 /* FALLTHROUGH */
20103 case DW_FORM_GNU_strp_alt:
20104 {
20105 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20106 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20107 &bytes_read);
20108
20109 attr->set_string_noncanonical
20110 (dwz->read_string (objfile, str_offset));
20111 info_ptr += bytes_read;
20112 }
20113 break;
20114 case DW_FORM_exprloc:
20115 case DW_FORM_block:
20116 blk = dwarf_alloc_block (cu);
20117 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20118 info_ptr += bytes_read;
20119 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20120 info_ptr += blk->size;
20121 attr->set_block (blk);
20122 break;
20123 case DW_FORM_block1:
20124 blk = dwarf_alloc_block (cu);
20125 blk->size = read_1_byte (abfd, info_ptr);
20126 info_ptr += 1;
20127 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20128 info_ptr += blk->size;
20129 attr->set_block (blk);
20130 break;
20131 case DW_FORM_data1:
20132 case DW_FORM_flag:
20133 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20134 info_ptr += 1;
20135 break;
20136 case DW_FORM_flag_present:
20137 attr->set_unsigned (1);
20138 break;
20139 case DW_FORM_sdata:
20140 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20141 info_ptr += bytes_read;
20142 break;
20143 case DW_FORM_rnglistx:
20144 {
20145 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20146 &bytes_read));
20147 info_ptr += bytes_read;
20148 }
20149 break;
20150 case DW_FORM_udata:
20151 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20152 info_ptr += bytes_read;
20153 break;
20154 case DW_FORM_ref1:
20155 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20156 + read_1_byte (abfd, info_ptr)));
20157 info_ptr += 1;
20158 break;
20159 case DW_FORM_ref2:
20160 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20161 + read_2_bytes (abfd, info_ptr)));
20162 info_ptr += 2;
20163 break;
20164 case DW_FORM_ref4:
20165 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20166 + read_4_bytes (abfd, info_ptr)));
20167 info_ptr += 4;
20168 break;
20169 case DW_FORM_ref8:
20170 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20171 + read_8_bytes (abfd, info_ptr)));
20172 info_ptr += 8;
20173 break;
20174 case DW_FORM_ref_sig8:
20175 attr->set_signature (read_8_bytes (abfd, info_ptr));
20176 info_ptr += 8;
20177 break;
20178 case DW_FORM_ref_udata:
20179 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20180 + read_unsigned_leb128 (abfd, info_ptr,
20181 &bytes_read)));
20182 info_ptr += bytes_read;
20183 break;
20184 case DW_FORM_indirect:
20185 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20186 info_ptr += bytes_read;
20187 if (form == DW_FORM_implicit_const)
20188 {
20189 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20190 info_ptr += bytes_read;
20191 }
20192 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20193 info_ptr);
20194 break;
20195 case DW_FORM_implicit_const:
20196 attr->set_signed (implicit_const);
20197 break;
20198 case DW_FORM_addrx:
20199 case DW_FORM_GNU_addr_index:
20200 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20201 &bytes_read));
20202 info_ptr += bytes_read;
20203 break;
20204 case DW_FORM_strx:
20205 case DW_FORM_strx1:
20206 case DW_FORM_strx2:
20207 case DW_FORM_strx3:
20208 case DW_FORM_strx4:
20209 case DW_FORM_GNU_str_index:
20210 {
20211 ULONGEST str_index;
20212 if (form == DW_FORM_strx1)
20213 {
20214 str_index = read_1_byte (abfd, info_ptr);
20215 info_ptr += 1;
20216 }
20217 else if (form == DW_FORM_strx2)
20218 {
20219 str_index = read_2_bytes (abfd, info_ptr);
20220 info_ptr += 2;
20221 }
20222 else if (form == DW_FORM_strx3)
20223 {
20224 str_index = read_3_bytes (abfd, info_ptr);
20225 info_ptr += 3;
20226 }
20227 else if (form == DW_FORM_strx4)
20228 {
20229 str_index = read_4_bytes (abfd, info_ptr);
20230 info_ptr += 4;
20231 }
20232 else
20233 {
20234 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20235 info_ptr += bytes_read;
20236 }
20237 attr->set_unsigned_reprocess (str_index);
20238 }
20239 break;
20240 default:
20241 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20242 dwarf_form_name (form),
20243 bfd_get_filename (abfd));
20244 }
20245
20246 /* Super hack. */
20247 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20248 attr->form = DW_FORM_GNU_ref_alt;
20249
20250 /* We have seen instances where the compiler tried to emit a byte
20251 size attribute of -1 which ended up being encoded as an unsigned
20252 0xffffffff. Although 0xffffffff is technically a valid size value,
20253 an object of this size seems pretty unlikely so we can relatively
20254 safely treat these cases as if the size attribute was invalid and
20255 treat them as zero by default. */
20256 if (attr->name == DW_AT_byte_size
20257 && form == DW_FORM_data4
20258 && attr->as_unsigned () >= 0xffffffff)
20259 {
20260 complaint
20261 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20262 hex_string (attr->as_unsigned ()));
20263 attr->set_unsigned (0);
20264 }
20265
20266 return info_ptr;
20267 }
20268
20269 /* Read an attribute described by an abbreviated attribute. */
20270
20271 static const gdb_byte *
20272 read_attribute (const struct die_reader_specs *reader,
20273 struct attribute *attr, const struct attr_abbrev *abbrev,
20274 const gdb_byte *info_ptr)
20275 {
20276 attr->name = abbrev->name;
20277 attr->string_is_canonical = 0;
20278 attr->requires_reprocessing = 0;
20279 return read_attribute_value (reader, attr, abbrev->form,
20280 abbrev->implicit_const, info_ptr);
20281 }
20282
20283 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20284
20285 static const char *
20286 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20287 LONGEST str_offset)
20288 {
20289 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20290 str_offset, "DW_FORM_strp");
20291 }
20292
20293 /* Return pointer to string at .debug_str offset as read from BUF.
20294 BUF is assumed to be in a compilation unit described by CU_HEADER.
20295 Return *BYTES_READ_PTR count of bytes read from BUF. */
20296
20297 static const char *
20298 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20299 const gdb_byte *buf,
20300 const struct comp_unit_head *cu_header,
20301 unsigned int *bytes_read_ptr)
20302 {
20303 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20304
20305 return read_indirect_string_at_offset (per_objfile, str_offset);
20306 }
20307
20308 /* See read.h. */
20309
20310 const char *
20311 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20312 const struct comp_unit_head *cu_header,
20313 unsigned int *bytes_read_ptr)
20314 {
20315 bfd *abfd = objfile->obfd;
20316 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20317
20318 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20319 }
20320
20321 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20322 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20323 ADDR_SIZE is the size of addresses from the CU header. */
20324
20325 static CORE_ADDR
20326 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20327 gdb::optional<ULONGEST> addr_base, int addr_size)
20328 {
20329 struct objfile *objfile = per_objfile->objfile;
20330 bfd *abfd = objfile->obfd;
20331 const gdb_byte *info_ptr;
20332 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20333
20334 per_objfile->per_bfd->addr.read (objfile);
20335 if (per_objfile->per_bfd->addr.buffer == NULL)
20336 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20337 objfile_name (objfile));
20338 if (addr_base_or_zero + addr_index * addr_size
20339 >= per_objfile->per_bfd->addr.size)
20340 error (_("DW_FORM_addr_index pointing outside of "
20341 ".debug_addr section [in module %s]"),
20342 objfile_name (objfile));
20343 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20344 + addr_index * addr_size);
20345 if (addr_size == 4)
20346 return bfd_get_32 (abfd, info_ptr);
20347 else
20348 return bfd_get_64 (abfd, info_ptr);
20349 }
20350
20351 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20352
20353 static CORE_ADDR
20354 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20355 {
20356 return read_addr_index_1 (cu->per_objfile, addr_index,
20357 cu->addr_base, cu->header.addr_size);
20358 }
20359
20360 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20361
20362 static CORE_ADDR
20363 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20364 unsigned int *bytes_read)
20365 {
20366 bfd *abfd = cu->per_objfile->objfile->obfd;
20367 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20368
20369 return read_addr_index (cu, addr_index);
20370 }
20371
20372 /* See read.h. */
20373
20374 CORE_ADDR
20375 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20376 dwarf2_per_objfile *per_objfile,
20377 unsigned int addr_index)
20378 {
20379 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20380 gdb::optional<ULONGEST> addr_base;
20381 int addr_size;
20382
20383 /* We need addr_base and addr_size.
20384 If we don't have PER_CU->cu, we have to get it.
20385 Nasty, but the alternative is storing the needed info in PER_CU,
20386 which at this point doesn't seem justified: it's not clear how frequently
20387 it would get used and it would increase the size of every PER_CU.
20388 Entry points like dwarf2_per_cu_addr_size do a similar thing
20389 so we're not in uncharted territory here.
20390 Alas we need to be a bit more complicated as addr_base is contained
20391 in the DIE.
20392
20393 We don't need to read the entire CU(/TU).
20394 We just need the header and top level die.
20395
20396 IWBN to use the aging mechanism to let us lazily later discard the CU.
20397 For now we skip this optimization. */
20398
20399 if (cu != NULL)
20400 {
20401 addr_base = cu->addr_base;
20402 addr_size = cu->header.addr_size;
20403 }
20404 else
20405 {
20406 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20407 addr_base = reader.cu->addr_base;
20408 addr_size = reader.cu->header.addr_size;
20409 }
20410
20411 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20412 }
20413
20414 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20415 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20416 DWO file. */
20417
20418 static const char *
20419 read_str_index (struct dwarf2_cu *cu,
20420 struct dwarf2_section_info *str_section,
20421 struct dwarf2_section_info *str_offsets_section,
20422 ULONGEST str_offsets_base, ULONGEST str_index)
20423 {
20424 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20425 struct objfile *objfile = per_objfile->objfile;
20426 const char *objf_name = objfile_name (objfile);
20427 bfd *abfd = objfile->obfd;
20428 const gdb_byte *info_ptr;
20429 ULONGEST str_offset;
20430 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20431
20432 str_section->read (objfile);
20433 str_offsets_section->read (objfile);
20434 if (str_section->buffer == NULL)
20435 error (_("%s used without %s section"
20436 " in CU at offset %s [in module %s]"),
20437 form_name, str_section->get_name (),
20438 sect_offset_str (cu->header.sect_off), objf_name);
20439 if (str_offsets_section->buffer == NULL)
20440 error (_("%s used without %s section"
20441 " in CU at offset %s [in module %s]"),
20442 form_name, str_section->get_name (),
20443 sect_offset_str (cu->header.sect_off), objf_name);
20444 info_ptr = (str_offsets_section->buffer
20445 + str_offsets_base
20446 + str_index * cu->header.offset_size);
20447 if (cu->header.offset_size == 4)
20448 str_offset = bfd_get_32 (abfd, info_ptr);
20449 else
20450 str_offset = bfd_get_64 (abfd, info_ptr);
20451 if (str_offset >= str_section->size)
20452 error (_("Offset from %s pointing outside of"
20453 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20454 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20455 return (const char *) (str_section->buffer + str_offset);
20456 }
20457
20458 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20459
20460 static const char *
20461 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20462 {
20463 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20464 ? reader->cu->header.addr_size : 0;
20465 return read_str_index (reader->cu,
20466 &reader->dwo_file->sections.str,
20467 &reader->dwo_file->sections.str_offsets,
20468 str_offsets_base, str_index);
20469 }
20470
20471 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20472
20473 static const char *
20474 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20475 {
20476 struct objfile *objfile = cu->per_objfile->objfile;
20477 const char *objf_name = objfile_name (objfile);
20478 static const char form_name[] = "DW_FORM_GNU_str_index";
20479 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20480
20481 if (!cu->str_offsets_base.has_value ())
20482 error (_("%s used in Fission stub without %s"
20483 " in CU at offset 0x%lx [in module %s]"),
20484 form_name, str_offsets_attr_name,
20485 (long) cu->header.offset_size, objf_name);
20486
20487 return read_str_index (cu,
20488 &cu->per_objfile->per_bfd->str,
20489 &cu->per_objfile->per_bfd->str_offsets,
20490 *cu->str_offsets_base, str_index);
20491 }
20492
20493 /* Return the length of an LEB128 number in BUF. */
20494
20495 static int
20496 leb128_size (const gdb_byte *buf)
20497 {
20498 const gdb_byte *begin = buf;
20499 gdb_byte byte;
20500
20501 while (1)
20502 {
20503 byte = *buf++;
20504 if ((byte & 128) == 0)
20505 return buf - begin;
20506 }
20507 }
20508
20509 static enum language
20510 dwarf_lang_to_enum_language (unsigned int lang)
20511 {
20512 enum language language;
20513
20514 switch (lang)
20515 {
20516 case DW_LANG_C89:
20517 case DW_LANG_C99:
20518 case DW_LANG_C11:
20519 case DW_LANG_C:
20520 case DW_LANG_UPC:
20521 language = language_c;
20522 break;
20523 case DW_LANG_Java:
20524 case DW_LANG_C_plus_plus:
20525 case DW_LANG_C_plus_plus_11:
20526 case DW_LANG_C_plus_plus_14:
20527 language = language_cplus;
20528 break;
20529 case DW_LANG_D:
20530 language = language_d;
20531 break;
20532 case DW_LANG_Fortran77:
20533 case DW_LANG_Fortran90:
20534 case DW_LANG_Fortran95:
20535 case DW_LANG_Fortran03:
20536 case DW_LANG_Fortran08:
20537 language = language_fortran;
20538 break;
20539 case DW_LANG_Go:
20540 language = language_go;
20541 break;
20542 case DW_LANG_Mips_Assembler:
20543 language = language_asm;
20544 break;
20545 case DW_LANG_Ada83:
20546 case DW_LANG_Ada95:
20547 language = language_ada;
20548 break;
20549 case DW_LANG_Modula2:
20550 language = language_m2;
20551 break;
20552 case DW_LANG_Pascal83:
20553 language = language_pascal;
20554 break;
20555 case DW_LANG_ObjC:
20556 language = language_objc;
20557 break;
20558 case DW_LANG_Rust:
20559 case DW_LANG_Rust_old:
20560 language = language_rust;
20561 break;
20562 case DW_LANG_OpenCL:
20563 language = language_opencl;
20564 break;
20565 case DW_LANG_Cobol74:
20566 case DW_LANG_Cobol85:
20567 default:
20568 language = language_minimal;
20569 break;
20570 }
20571
20572 return language;
20573 }
20574
20575 /* Return the named attribute or NULL if not there. */
20576
20577 static struct attribute *
20578 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20579 {
20580 for (;;)
20581 {
20582 unsigned int i;
20583 struct attribute *spec = NULL;
20584
20585 for (i = 0; i < die->num_attrs; ++i)
20586 {
20587 if (die->attrs[i].name == name)
20588 return &die->attrs[i];
20589 if (die->attrs[i].name == DW_AT_specification
20590 || die->attrs[i].name == DW_AT_abstract_origin)
20591 spec = &die->attrs[i];
20592 }
20593
20594 if (!spec)
20595 break;
20596
20597 die = follow_die_ref (die, spec, &cu);
20598 }
20599
20600 return NULL;
20601 }
20602
20603 /* Return the string associated with a string-typed attribute, or NULL if it
20604 is either not found or is of an incorrect type. */
20605
20606 static const char *
20607 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20608 {
20609 struct attribute *attr;
20610 const char *str = NULL;
20611
20612 attr = dwarf2_attr (die, name, cu);
20613
20614 if (attr != NULL)
20615 {
20616 str = attr->as_string ();
20617 if (str == nullptr)
20618 complaint (_("string type expected for attribute %s for "
20619 "DIE at %s in module %s"),
20620 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20621 objfile_name (cu->per_objfile->objfile));
20622 }
20623
20624 return str;
20625 }
20626
20627 /* Return the dwo name or NULL if not present. If present, it is in either
20628 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20629 static const char *
20630 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20631 {
20632 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20633 if (dwo_name == nullptr)
20634 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20635 return dwo_name;
20636 }
20637
20638 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20639 and holds a non-zero value. This function should only be used for
20640 DW_FORM_flag or DW_FORM_flag_present attributes. */
20641
20642 static int
20643 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20644 {
20645 struct attribute *attr = dwarf2_attr (die, name, cu);
20646
20647 return attr != nullptr && attr->as_boolean ();
20648 }
20649
20650 static int
20651 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20652 {
20653 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20654 which value is non-zero. However, we have to be careful with
20655 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20656 (via dwarf2_flag_true_p) follows this attribute. So we may
20657 end up accidently finding a declaration attribute that belongs
20658 to a different DIE referenced by the specification attribute,
20659 even though the given DIE does not have a declaration attribute. */
20660 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20661 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20662 }
20663
20664 /* Return the die giving the specification for DIE, if there is
20665 one. *SPEC_CU is the CU containing DIE on input, and the CU
20666 containing the return value on output. If there is no
20667 specification, but there is an abstract origin, that is
20668 returned. */
20669
20670 static struct die_info *
20671 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20672 {
20673 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20674 *spec_cu);
20675
20676 if (spec_attr == NULL)
20677 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20678
20679 if (spec_attr == NULL)
20680 return NULL;
20681 else
20682 return follow_die_ref (die, spec_attr, spec_cu);
20683 }
20684
20685 /* A convenience function to find the proper .debug_line section for a CU. */
20686
20687 static struct dwarf2_section_info *
20688 get_debug_line_section (struct dwarf2_cu *cu)
20689 {
20690 struct dwarf2_section_info *section;
20691 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20692
20693 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20694 DWO file. */
20695 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20696 section = &cu->dwo_unit->dwo_file->sections.line;
20697 else if (cu->per_cu->is_dwz)
20698 {
20699 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20700
20701 section = &dwz->line;
20702 }
20703 else
20704 section = &per_objfile->per_bfd->line;
20705
20706 return section;
20707 }
20708
20709 /* Read the statement program header starting at OFFSET in
20710 .debug_line, or .debug_line.dwo. Return a pointer
20711 to a struct line_header, allocated using xmalloc.
20712 Returns NULL if there is a problem reading the header, e.g., if it
20713 has a version we don't understand.
20714
20715 NOTE: the strings in the include directory and file name tables of
20716 the returned object point into the dwarf line section buffer,
20717 and must not be freed. */
20718
20719 static line_header_up
20720 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20721 {
20722 struct dwarf2_section_info *section;
20723 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20724
20725 section = get_debug_line_section (cu);
20726 section->read (per_objfile->objfile);
20727 if (section->buffer == NULL)
20728 {
20729 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20730 complaint (_("missing .debug_line.dwo section"));
20731 else
20732 complaint (_("missing .debug_line section"));
20733 return 0;
20734 }
20735
20736 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20737 per_objfile, section, &cu->header);
20738 }
20739
20740 /* Subroutine of dwarf_decode_lines to simplify it.
20741 Return the file name for the given file_entry.
20742 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
20743 If space for the result is malloc'd, *NAME_HOLDER will be set.
20744 Returns NULL if FILE_INDEX should be ignored, i.e., it is
20745 equivalent to CU_INFO. */
20746
20747 static const char *
20748 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
20749 const file_and_directory &cu_info,
20750 gdb::unique_xmalloc_ptr<char> *name_holder)
20751 {
20752 const char *include_name = fe.name;
20753 const char *include_name_to_compare = include_name;
20754
20755 const char *dir_name = fe.include_dir (lh);
20756
20757 gdb::unique_xmalloc_ptr<char> hold_compare;
20758 if (!IS_ABSOLUTE_PATH (include_name)
20759 && (dir_name != NULL || cu_info.comp_dir != NULL))
20760 {
20761 /* Avoid creating a duplicate name for CU_INFO.
20762 We do this by comparing INCLUDE_NAME and CU_INFO.
20763 Before we do the comparison, however, we need to account
20764 for DIR_NAME and COMP_DIR.
20765 First prepend dir_name (if non-NULL). If we still don't
20766 have an absolute path prepend comp_dir (if non-NULL).
20767 However, the directory we record in the include-file's
20768 psymtab does not contain COMP_DIR (to match the
20769 corresponding symtab(s)).
20770
20771 Example:
20772
20773 bash$ cd /tmp
20774 bash$ gcc -g ./hello.c
20775 include_name = "hello.c"
20776 dir_name = "."
20777 DW_AT_comp_dir = comp_dir = "/tmp"
20778 DW_AT_name = "./hello.c"
20779
20780 */
20781
20782 if (dir_name != NULL)
20783 {
20784 name_holder->reset (concat (dir_name, SLASH_STRING,
20785 include_name, (char *) NULL));
20786 include_name = name_holder->get ();
20787 include_name_to_compare = include_name;
20788 }
20789 if (!IS_ABSOLUTE_PATH (include_name) && cu_info.comp_dir != nullptr)
20790 {
20791 hold_compare.reset (concat (cu_info.comp_dir, SLASH_STRING,
20792 include_name, (char *) NULL));
20793 include_name_to_compare = hold_compare.get ();
20794 }
20795 }
20796
20797 gdb::unique_xmalloc_ptr<char> copied_name;
20798 const char *cu_filename = cu_info.name;
20799 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.comp_dir != nullptr)
20800 {
20801 copied_name.reset (concat (cu_info.comp_dir, SLASH_STRING,
20802 cu_filename, (char *) NULL));
20803 cu_filename = copied_name.get ();
20804 }
20805
20806 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
20807 return nullptr;
20808 return include_name;
20809 }
20810
20811 /* State machine to track the state of the line number program. */
20812
20813 class lnp_state_machine
20814 {
20815 public:
20816 /* Initialize a machine state for the start of a line number
20817 program. */
20818 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20819 bool record_lines_p);
20820
20821 file_entry *current_file ()
20822 {
20823 /* lh->file_names is 0-based, but the file name numbers in the
20824 statement program are 1-based. */
20825 return m_line_header->file_name_at (m_file);
20826 }
20827
20828 /* Record the line in the state machine. END_SEQUENCE is true if
20829 we're processing the end of a sequence. */
20830 void record_line (bool end_sequence);
20831
20832 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20833 nop-out rest of the lines in this sequence. */
20834 void check_line_address (struct dwarf2_cu *cu,
20835 const gdb_byte *line_ptr,
20836 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20837
20838 void handle_set_discriminator (unsigned int discriminator)
20839 {
20840 m_discriminator = discriminator;
20841 m_line_has_non_zero_discriminator |= discriminator != 0;
20842 }
20843
20844 /* Handle DW_LNE_set_address. */
20845 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20846 {
20847 m_op_index = 0;
20848 address += baseaddr;
20849 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20850 }
20851
20852 /* Handle DW_LNS_advance_pc. */
20853 void handle_advance_pc (CORE_ADDR adjust);
20854
20855 /* Handle a special opcode. */
20856 void handle_special_opcode (unsigned char op_code);
20857
20858 /* Handle DW_LNS_advance_line. */
20859 void handle_advance_line (int line_delta)
20860 {
20861 advance_line (line_delta);
20862 }
20863
20864 /* Handle DW_LNS_set_file. */
20865 void handle_set_file (file_name_index file);
20866
20867 /* Handle DW_LNS_negate_stmt. */
20868 void handle_negate_stmt ()
20869 {
20870 m_is_stmt = !m_is_stmt;
20871 }
20872
20873 /* Handle DW_LNS_const_add_pc. */
20874 void handle_const_add_pc ();
20875
20876 /* Handle DW_LNS_fixed_advance_pc. */
20877 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20878 {
20879 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20880 m_op_index = 0;
20881 }
20882
20883 /* Handle DW_LNS_copy. */
20884 void handle_copy ()
20885 {
20886 record_line (false);
20887 m_discriminator = 0;
20888 }
20889
20890 /* Handle DW_LNE_end_sequence. */
20891 void handle_end_sequence ()
20892 {
20893 m_currently_recording_lines = true;
20894 }
20895
20896 private:
20897 /* Advance the line by LINE_DELTA. */
20898 void advance_line (int line_delta)
20899 {
20900 m_line += line_delta;
20901
20902 if (line_delta != 0)
20903 m_line_has_non_zero_discriminator = m_discriminator != 0;
20904 }
20905
20906 struct dwarf2_cu *m_cu;
20907
20908 gdbarch *m_gdbarch;
20909
20910 /* True if we're recording lines.
20911 Otherwise we're building partial symtabs and are just interested in
20912 finding include files mentioned by the line number program. */
20913 bool m_record_lines_p;
20914
20915 /* The line number header. */
20916 line_header *m_line_header;
20917
20918 /* These are part of the standard DWARF line number state machine,
20919 and initialized according to the DWARF spec. */
20920
20921 unsigned char m_op_index = 0;
20922 /* The line table index of the current file. */
20923 file_name_index m_file = 1;
20924 unsigned int m_line = 1;
20925
20926 /* These are initialized in the constructor. */
20927
20928 CORE_ADDR m_address;
20929 bool m_is_stmt;
20930 unsigned int m_discriminator;
20931
20932 /* Additional bits of state we need to track. */
20933
20934 /* The last file that we called dwarf2_start_subfile for.
20935 This is only used for TLLs. */
20936 unsigned int m_last_file = 0;
20937 /* The last file a line number was recorded for. */
20938 struct subfile *m_last_subfile = NULL;
20939
20940 /* The address of the last line entry. */
20941 CORE_ADDR m_last_address;
20942
20943 /* Set to true when a previous line at the same address (using
20944 m_last_address) had m_is_stmt true. This is reset to false when a
20945 line entry at a new address (m_address different to m_last_address) is
20946 processed. */
20947 bool m_stmt_at_address = false;
20948
20949 /* When true, record the lines we decode. */
20950 bool m_currently_recording_lines = false;
20951
20952 /* The last line number that was recorded, used to coalesce
20953 consecutive entries for the same line. This can happen, for
20954 example, when discriminators are present. PR 17276. */
20955 unsigned int m_last_line = 0;
20956 bool m_line_has_non_zero_discriminator = false;
20957 };
20958
20959 void
20960 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20961 {
20962 CORE_ADDR addr_adj = (((m_op_index + adjust)
20963 / m_line_header->maximum_ops_per_instruction)
20964 * m_line_header->minimum_instruction_length);
20965 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20966 m_op_index = ((m_op_index + adjust)
20967 % m_line_header->maximum_ops_per_instruction);
20968 }
20969
20970 void
20971 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20972 {
20973 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20974 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20975 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20976 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20977 / m_line_header->maximum_ops_per_instruction)
20978 * m_line_header->minimum_instruction_length);
20979 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20980 m_op_index = ((m_op_index + adj_opcode_d)
20981 % m_line_header->maximum_ops_per_instruction);
20982
20983 int line_delta = m_line_header->line_base + adj_opcode_r;
20984 advance_line (line_delta);
20985 record_line (false);
20986 m_discriminator = 0;
20987 }
20988
20989 void
20990 lnp_state_machine::handle_set_file (file_name_index file)
20991 {
20992 m_file = file;
20993
20994 const file_entry *fe = current_file ();
20995 if (fe == NULL)
20996 dwarf2_debug_line_missing_file_complaint ();
20997 else if (m_record_lines_p)
20998 {
20999 const char *dir = fe->include_dir (m_line_header);
21000
21001 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21002 m_line_has_non_zero_discriminator = m_discriminator != 0;
21003 dwarf2_start_subfile (m_cu, fe->name, dir);
21004 }
21005 }
21006
21007 void
21008 lnp_state_machine::handle_const_add_pc ()
21009 {
21010 CORE_ADDR adjust
21011 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21012
21013 CORE_ADDR addr_adj
21014 = (((m_op_index + adjust)
21015 / m_line_header->maximum_ops_per_instruction)
21016 * m_line_header->minimum_instruction_length);
21017
21018 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21019 m_op_index = ((m_op_index + adjust)
21020 % m_line_header->maximum_ops_per_instruction);
21021 }
21022
21023 /* Return non-zero if we should add LINE to the line number table.
21024 LINE is the line to add, LAST_LINE is the last line that was added,
21025 LAST_SUBFILE is the subfile for LAST_LINE.
21026 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21027 had a non-zero discriminator.
21028
21029 We have to be careful in the presence of discriminators.
21030 E.g., for this line:
21031
21032 for (i = 0; i < 100000; i++);
21033
21034 clang can emit four line number entries for that one line,
21035 each with a different discriminator.
21036 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21037
21038 However, we want gdb to coalesce all four entries into one.
21039 Otherwise the user could stepi into the middle of the line and
21040 gdb would get confused about whether the pc really was in the
21041 middle of the line.
21042
21043 Things are further complicated by the fact that two consecutive
21044 line number entries for the same line is a heuristic used by gcc
21045 to denote the end of the prologue. So we can't just discard duplicate
21046 entries, we have to be selective about it. The heuristic we use is
21047 that we only collapse consecutive entries for the same line if at least
21048 one of those entries has a non-zero discriminator. PR 17276.
21049
21050 Note: Addresses in the line number state machine can never go backwards
21051 within one sequence, thus this coalescing is ok. */
21052
21053 static int
21054 dwarf_record_line_p (struct dwarf2_cu *cu,
21055 unsigned int line, unsigned int last_line,
21056 int line_has_non_zero_discriminator,
21057 struct subfile *last_subfile)
21058 {
21059 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21060 return 1;
21061 if (line != last_line)
21062 return 1;
21063 /* Same line for the same file that we've seen already.
21064 As a last check, for pr 17276, only record the line if the line
21065 has never had a non-zero discriminator. */
21066 if (!line_has_non_zero_discriminator)
21067 return 1;
21068 return 0;
21069 }
21070
21071 /* Use the CU's builder to record line number LINE beginning at
21072 address ADDRESS in the line table of subfile SUBFILE. */
21073
21074 static void
21075 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21076 unsigned int line, CORE_ADDR address, bool is_stmt,
21077 struct dwarf2_cu *cu)
21078 {
21079 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21080
21081 if (dwarf_line_debug)
21082 {
21083 fprintf_unfiltered (gdb_stdlog,
21084 "Recording line %u, file %s, address %s\n",
21085 line, lbasename (subfile->name),
21086 paddress (gdbarch, address));
21087 }
21088
21089 if (cu != nullptr)
21090 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21091 }
21092
21093 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21094 Mark the end of a set of line number records.
21095 The arguments are the same as for dwarf_record_line_1.
21096 If SUBFILE is NULL the request is ignored. */
21097
21098 static void
21099 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21100 CORE_ADDR address, struct dwarf2_cu *cu)
21101 {
21102 if (subfile == NULL)
21103 return;
21104
21105 if (dwarf_line_debug)
21106 {
21107 fprintf_unfiltered (gdb_stdlog,
21108 "Finishing current line, file %s, address %s\n",
21109 lbasename (subfile->name),
21110 paddress (gdbarch, address));
21111 }
21112
21113 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21114 }
21115
21116 void
21117 lnp_state_machine::record_line (bool end_sequence)
21118 {
21119 if (dwarf_line_debug)
21120 {
21121 fprintf_unfiltered (gdb_stdlog,
21122 "Processing actual line %u: file %u,"
21123 " address %s, is_stmt %u, discrim %u%s\n",
21124 m_line, m_file,
21125 paddress (m_gdbarch, m_address),
21126 m_is_stmt, m_discriminator,
21127 (end_sequence ? "\t(end sequence)" : ""));
21128 }
21129
21130 file_entry *fe = current_file ();
21131
21132 if (fe == NULL)
21133 dwarf2_debug_line_missing_file_complaint ();
21134 /* For now we ignore lines not starting on an instruction boundary.
21135 But not when processing end_sequence for compatibility with the
21136 previous version of the code. */
21137 else if (m_op_index == 0 || end_sequence)
21138 {
21139 fe->included_p = true;
21140 if (m_record_lines_p)
21141 {
21142 /* When we switch files we insert an end maker in the first file,
21143 switch to the second file and add a new line entry. The
21144 problem is that the end marker inserted in the first file will
21145 discard any previous line entries at the same address. If the
21146 line entries in the first file are marked as is-stmt, while
21147 the new line in the second file is non-stmt, then this means
21148 the end marker will discard is-stmt lines so we can have a
21149 non-stmt line. This means that there are less addresses at
21150 which the user can insert a breakpoint.
21151
21152 To improve this we track the last address in m_last_address,
21153 and whether we have seen an is-stmt at this address. Then
21154 when switching files, if we have seen a stmt at the current
21155 address, and we are switching to create a non-stmt line, then
21156 discard the new line. */
21157 bool file_changed
21158 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21159 bool ignore_this_line
21160 = ((file_changed && !end_sequence && m_last_address == m_address
21161 && !m_is_stmt && m_stmt_at_address)
21162 || (!end_sequence && m_line == 0));
21163
21164 if ((file_changed && !ignore_this_line) || end_sequence)
21165 {
21166 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21167 m_currently_recording_lines ? m_cu : nullptr);
21168 }
21169
21170 if (!end_sequence && !ignore_this_line)
21171 {
21172 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21173
21174 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21175 m_line_has_non_zero_discriminator,
21176 m_last_subfile))
21177 {
21178 buildsym_compunit *builder = m_cu->get_builder ();
21179 dwarf_record_line_1 (m_gdbarch,
21180 builder->get_current_subfile (),
21181 m_line, m_address, is_stmt,
21182 m_currently_recording_lines ? m_cu : nullptr);
21183 }
21184 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21185 m_last_line = m_line;
21186 }
21187 }
21188 }
21189
21190 /* Track whether we have seen any m_is_stmt true at m_address in case we
21191 have multiple line table entries all at m_address. */
21192 if (m_last_address != m_address)
21193 {
21194 m_stmt_at_address = false;
21195 m_last_address = m_address;
21196 }
21197 m_stmt_at_address |= m_is_stmt;
21198 }
21199
21200 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21201 line_header *lh, bool record_lines_p)
21202 {
21203 m_cu = cu;
21204 m_gdbarch = arch;
21205 m_record_lines_p = record_lines_p;
21206 m_line_header = lh;
21207
21208 m_currently_recording_lines = true;
21209
21210 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21211 was a line entry for it so that the backend has a chance to adjust it
21212 and also record it in case it needs it. This is currently used by MIPS
21213 code, cf. `mips_adjust_dwarf2_line'. */
21214 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21215 m_is_stmt = lh->default_is_stmt;
21216 m_discriminator = 0;
21217
21218 m_last_address = m_address;
21219 m_stmt_at_address = false;
21220 }
21221
21222 void
21223 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21224 const gdb_byte *line_ptr,
21225 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21226 {
21227 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21228 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21229 located at 0x0. In this case, additionally check that if
21230 ADDRESS < UNRELOCATED_LOWPC. */
21231
21232 if ((address == 0 && address < unrelocated_lowpc)
21233 || address == (CORE_ADDR) -1)
21234 {
21235 /* This line table is for a function which has been
21236 GCd by the linker. Ignore it. PR gdb/12528 */
21237
21238 struct objfile *objfile = cu->per_objfile->objfile;
21239 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21240
21241 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21242 line_offset, objfile_name (objfile));
21243 m_currently_recording_lines = false;
21244 /* Note: m_currently_recording_lines is left as false until we see
21245 DW_LNE_end_sequence. */
21246 }
21247 }
21248
21249 /* Subroutine of dwarf_decode_lines to simplify it.
21250 Process the line number information in LH.
21251 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21252 program in order to set included_p for every referenced header. */
21253
21254 static void
21255 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21256 const int decode_for_pst_p, CORE_ADDR lowpc)
21257 {
21258 const gdb_byte *line_ptr, *extended_end;
21259 const gdb_byte *line_end;
21260 unsigned int bytes_read, extended_len;
21261 unsigned char op_code, extended_op;
21262 CORE_ADDR baseaddr;
21263 struct objfile *objfile = cu->per_objfile->objfile;
21264 bfd *abfd = objfile->obfd;
21265 struct gdbarch *gdbarch = objfile->arch ();
21266 /* True if we're recording line info (as opposed to building partial
21267 symtabs and just interested in finding include files mentioned by
21268 the line number program). */
21269 bool record_lines_p = !decode_for_pst_p;
21270
21271 baseaddr = objfile->text_section_offset ();
21272
21273 line_ptr = lh->statement_program_start;
21274 line_end = lh->statement_program_end;
21275
21276 /* Read the statement sequences until there's nothing left. */
21277 while (line_ptr < line_end)
21278 {
21279 /* The DWARF line number program state machine. Reset the state
21280 machine at the start of each sequence. */
21281 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21282 bool end_sequence = false;
21283
21284 if (record_lines_p)
21285 {
21286 /* Start a subfile for the current file of the state
21287 machine. */
21288 const file_entry *fe = state_machine.current_file ();
21289
21290 if (fe != NULL)
21291 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21292 }
21293
21294 /* Decode the table. */
21295 while (line_ptr < line_end && !end_sequence)
21296 {
21297 op_code = read_1_byte (abfd, line_ptr);
21298 line_ptr += 1;
21299
21300 if (op_code >= lh->opcode_base)
21301 {
21302 /* Special opcode. */
21303 state_machine.handle_special_opcode (op_code);
21304 }
21305 else switch (op_code)
21306 {
21307 case DW_LNS_extended_op:
21308 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21309 &bytes_read);
21310 line_ptr += bytes_read;
21311 extended_end = line_ptr + extended_len;
21312 extended_op = read_1_byte (abfd, line_ptr);
21313 line_ptr += 1;
21314 if (DW_LNE_lo_user <= extended_op
21315 && extended_op <= DW_LNE_hi_user)
21316 {
21317 /* Vendor extension, ignore. */
21318 line_ptr = extended_end;
21319 break;
21320 }
21321 switch (extended_op)
21322 {
21323 case DW_LNE_end_sequence:
21324 state_machine.handle_end_sequence ();
21325 end_sequence = true;
21326 break;
21327 case DW_LNE_set_address:
21328 {
21329 CORE_ADDR address
21330 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21331 line_ptr += bytes_read;
21332
21333 state_machine.check_line_address (cu, line_ptr,
21334 lowpc - baseaddr, address);
21335 state_machine.handle_set_address (baseaddr, address);
21336 }
21337 break;
21338 case DW_LNE_define_file:
21339 {
21340 const char *cur_file;
21341 unsigned int mod_time, length;
21342 dir_index dindex;
21343
21344 cur_file = read_direct_string (abfd, line_ptr,
21345 &bytes_read);
21346 line_ptr += bytes_read;
21347 dindex = (dir_index)
21348 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21349 line_ptr += bytes_read;
21350 mod_time =
21351 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21352 line_ptr += bytes_read;
21353 length =
21354 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21355 line_ptr += bytes_read;
21356 lh->add_file_name (cur_file, dindex, mod_time, length);
21357 }
21358 break;
21359 case DW_LNE_set_discriminator:
21360 {
21361 /* The discriminator is not interesting to the
21362 debugger; just ignore it. We still need to
21363 check its value though:
21364 if there are consecutive entries for the same
21365 (non-prologue) line we want to coalesce them.
21366 PR 17276. */
21367 unsigned int discr
21368 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21369 line_ptr += bytes_read;
21370
21371 state_machine.handle_set_discriminator (discr);
21372 }
21373 break;
21374 default:
21375 complaint (_("mangled .debug_line section"));
21376 return;
21377 }
21378 /* Make sure that we parsed the extended op correctly. If e.g.
21379 we expected a different address size than the producer used,
21380 we may have read the wrong number of bytes. */
21381 if (line_ptr != extended_end)
21382 {
21383 complaint (_("mangled .debug_line section"));
21384 return;
21385 }
21386 break;
21387 case DW_LNS_copy:
21388 state_machine.handle_copy ();
21389 break;
21390 case DW_LNS_advance_pc:
21391 {
21392 CORE_ADDR adjust
21393 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21394 line_ptr += bytes_read;
21395
21396 state_machine.handle_advance_pc (adjust);
21397 }
21398 break;
21399 case DW_LNS_advance_line:
21400 {
21401 int line_delta
21402 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21403 line_ptr += bytes_read;
21404
21405 state_machine.handle_advance_line (line_delta);
21406 }
21407 break;
21408 case DW_LNS_set_file:
21409 {
21410 file_name_index file
21411 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21412 &bytes_read);
21413 line_ptr += bytes_read;
21414
21415 state_machine.handle_set_file (file);
21416 }
21417 break;
21418 case DW_LNS_set_column:
21419 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21420 line_ptr += bytes_read;
21421 break;
21422 case DW_LNS_negate_stmt:
21423 state_machine.handle_negate_stmt ();
21424 break;
21425 case DW_LNS_set_basic_block:
21426 break;
21427 /* Add to the address register of the state machine the
21428 address increment value corresponding to special opcode
21429 255. I.e., this value is scaled by the minimum
21430 instruction length since special opcode 255 would have
21431 scaled the increment. */
21432 case DW_LNS_const_add_pc:
21433 state_machine.handle_const_add_pc ();
21434 break;
21435 case DW_LNS_fixed_advance_pc:
21436 {
21437 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21438 line_ptr += 2;
21439
21440 state_machine.handle_fixed_advance_pc (addr_adj);
21441 }
21442 break;
21443 default:
21444 {
21445 /* Unknown standard opcode, ignore it. */
21446 int i;
21447
21448 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21449 {
21450 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21451 line_ptr += bytes_read;
21452 }
21453 }
21454 }
21455 }
21456
21457 if (!end_sequence)
21458 dwarf2_debug_line_missing_end_sequence_complaint ();
21459
21460 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21461 in which case we still finish recording the last line). */
21462 state_machine.record_line (true);
21463 }
21464 }
21465
21466 /* Decode the Line Number Program (LNP) for the given line_header
21467 structure and CU. The actual information extracted and the type
21468 of structures created from the LNP depends on the value of PST.
21469
21470 1. If PST is NULL, then this procedure uses the data from the program
21471 to create all necessary symbol tables, and their linetables.
21472
21473 2. If PST is not NULL, this procedure reads the program to determine
21474 the list of files included by the unit represented by PST, and
21475 builds all the associated partial symbol tables.
21476
21477 FND holds the CU file name and directory, if known.
21478 It is used for relative paths in the line table.
21479
21480 NOTE: It is important that psymtabs have the same file name (via
21481 strcmp) as the corresponding symtab. Since the directory is not
21482 used in the name of the symtab we don't use it in the name of the
21483 psymtabs we create. E.g. expand_line_sal requires this when
21484 finding psymtabs to expand. A good testcase for this is
21485 mb-inline.exp.
21486
21487 LOWPC is the lowest address in CU (or 0 if not known).
21488
21489 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21490 for its PC<->lines mapping information. Otherwise only the filename
21491 table is read in. */
21492
21493 static void
21494 dwarf_decode_lines (struct line_header *lh, const file_and_directory &fnd,
21495 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21496 CORE_ADDR lowpc, int decode_mapping)
21497 {
21498 struct objfile *objfile = cu->per_objfile->objfile;
21499 const int decode_for_pst_p = (pst != NULL);
21500
21501 if (decode_mapping)
21502 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21503
21504 if (decode_for_pst_p)
21505 {
21506 /* Now that we're done scanning the Line Header Program, we can
21507 create the psymtab of each included file. */
21508 for (auto &file_entry : lh->file_names ())
21509 if (file_entry.included_p)
21510 {
21511 gdb::unique_xmalloc_ptr<char> name_holder;
21512 const char *include_name =
21513 compute_include_file_name (lh, file_entry, fnd, &name_holder);
21514 if (include_name != NULL)
21515 dwarf2_create_include_psymtab
21516 (cu->per_objfile->per_bfd, include_name, pst,
21517 cu->per_objfile->per_bfd->partial_symtabs.get (),
21518 objfile->per_bfd);
21519 }
21520 }
21521 else
21522 {
21523 /* Make sure a symtab is created for every file, even files
21524 which contain only variables (i.e. no code with associated
21525 line numbers). */
21526 buildsym_compunit *builder = cu->get_builder ();
21527 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21528
21529 for (auto &fe : lh->file_names ())
21530 {
21531 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21532 if (builder->get_current_subfile ()->symtab == NULL)
21533 {
21534 builder->get_current_subfile ()->symtab
21535 = allocate_symtab (cust,
21536 builder->get_current_subfile ()->name);
21537 }
21538 fe.symtab = builder->get_current_subfile ()->symtab;
21539 }
21540 }
21541 }
21542
21543 /* Start a subfile for DWARF. FILENAME is the name of the file and
21544 DIRNAME the name of the source directory which contains FILENAME
21545 or NULL if not known.
21546 This routine tries to keep line numbers from identical absolute and
21547 relative file names in a common subfile.
21548
21549 Using the `list' example from the GDB testsuite, which resides in
21550 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21551 of /srcdir/list0.c yields the following debugging information for list0.c:
21552
21553 DW_AT_name: /srcdir/list0.c
21554 DW_AT_comp_dir: /compdir
21555 files.files[0].name: list0.h
21556 files.files[0].dir: /srcdir
21557 files.files[1].name: list0.c
21558 files.files[1].dir: /srcdir
21559
21560 The line number information for list0.c has to end up in a single
21561 subfile, so that `break /srcdir/list0.c:1' works as expected.
21562 start_subfile will ensure that this happens provided that we pass the
21563 concatenation of files.files[1].dir and files.files[1].name as the
21564 subfile's name. */
21565
21566 static void
21567 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21568 const char *dirname)
21569 {
21570 gdb::unique_xmalloc_ptr<char> copy;
21571
21572 /* In order not to lose the line information directory,
21573 we concatenate it to the filename when it makes sense.
21574 Note that the Dwarf3 standard says (speaking of filenames in line
21575 information): ``The directory index is ignored for file names
21576 that represent full path names''. Thus ignoring dirname in the
21577 `else' branch below isn't an issue. */
21578
21579 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21580 {
21581 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21582 filename = copy.get ();
21583 }
21584
21585 cu->get_builder ()->start_subfile (filename);
21586 }
21587
21588 static void
21589 var_decode_location (struct attribute *attr, struct symbol *sym,
21590 struct dwarf2_cu *cu)
21591 {
21592 struct objfile *objfile = cu->per_objfile->objfile;
21593 struct comp_unit_head *cu_header = &cu->header;
21594
21595 /* NOTE drow/2003-01-30: There used to be a comment and some special
21596 code here to turn a symbol with DW_AT_external and a
21597 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21598 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21599 with some versions of binutils) where shared libraries could have
21600 relocations against symbols in their debug information - the
21601 minimal symbol would have the right address, but the debug info
21602 would not. It's no longer necessary, because we will explicitly
21603 apply relocations when we read in the debug information now. */
21604
21605 /* A DW_AT_location attribute with no contents indicates that a
21606 variable has been optimized away. */
21607 if (attr->form_is_block () && attr->as_block ()->size == 0)
21608 {
21609 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21610 return;
21611 }
21612
21613 /* Handle one degenerate form of location expression specially, to
21614 preserve GDB's previous behavior when section offsets are
21615 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21616 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21617
21618 if (attr->form_is_block ())
21619 {
21620 struct dwarf_block *block = attr->as_block ();
21621
21622 if ((block->data[0] == DW_OP_addr
21623 && block->size == 1 + cu_header->addr_size)
21624 || ((block->data[0] == DW_OP_GNU_addr_index
21625 || block->data[0] == DW_OP_addrx)
21626 && (block->size
21627 == 1 + leb128_size (&block->data[1]))))
21628 {
21629 unsigned int dummy;
21630
21631 if (block->data[0] == DW_OP_addr)
21632 SET_SYMBOL_VALUE_ADDRESS
21633 (sym, cu->header.read_address (objfile->obfd,
21634 block->data + 1,
21635 &dummy));
21636 else
21637 SET_SYMBOL_VALUE_ADDRESS
21638 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21639 &dummy));
21640 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21641 fixup_symbol_section (sym, objfile);
21642 SET_SYMBOL_VALUE_ADDRESS
21643 (sym,
21644 SYMBOL_VALUE_ADDRESS (sym)
21645 + objfile->section_offsets[sym->section_index ()]);
21646 return;
21647 }
21648 }
21649
21650 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21651 expression evaluator, and use LOC_COMPUTED only when necessary
21652 (i.e. when the value of a register or memory location is
21653 referenced, or a thread-local block, etc.). Then again, it might
21654 not be worthwhile. I'm assuming that it isn't unless performance
21655 or memory numbers show me otherwise. */
21656
21657 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21658
21659 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21660 cu->has_loclist = true;
21661 }
21662
21663 /* Given a pointer to a DWARF information entry, figure out if we need
21664 to make a symbol table entry for it, and if so, create a new entry
21665 and return a pointer to it.
21666 If TYPE is NULL, determine symbol type from the die, otherwise
21667 used the passed type.
21668 If SPACE is not NULL, use it to hold the new symbol. If it is
21669 NULL, allocate a new symbol on the objfile's obstack. */
21670
21671 static struct symbol *
21672 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21673 struct symbol *space)
21674 {
21675 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21676 struct objfile *objfile = per_objfile->objfile;
21677 struct gdbarch *gdbarch = objfile->arch ();
21678 struct symbol *sym = NULL;
21679 const char *name;
21680 struct attribute *attr = NULL;
21681 struct attribute *attr2 = NULL;
21682 CORE_ADDR baseaddr;
21683 struct pending **list_to_add = NULL;
21684
21685 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21686
21687 baseaddr = objfile->text_section_offset ();
21688
21689 name = dwarf2_name (die, cu);
21690 if (name)
21691 {
21692 int suppress_add = 0;
21693
21694 if (space)
21695 sym = space;
21696 else
21697 sym = new (&objfile->objfile_obstack) symbol;
21698 OBJSTAT (objfile, n_syms++);
21699
21700 /* Cache this symbol's name and the name's demangled form (if any). */
21701 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
21702 /* Fortran does not have mangling standard and the mangling does differ
21703 between gfortran, iFort etc. */
21704 const char *physname
21705 = (cu->per_cu->lang == language_fortran
21706 ? dwarf2_full_name (name, die, cu)
21707 : dwarf2_physname (name, die, cu));
21708 const char *linkagename = dw2_linkage_name (die, cu);
21709
21710 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
21711 sym->set_linkage_name (physname);
21712 else
21713 {
21714 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21715 sym->set_linkage_name (linkagename);
21716 }
21717
21718 /* Handle DW_AT_artificial. */
21719 attr = dwarf2_attr (die, DW_AT_artificial, cu);
21720 if (attr != nullptr)
21721 sym->artificial = attr->as_boolean ();
21722
21723 /* Default assumptions.
21724 Use the passed type or decode it from the die. */
21725 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21726 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21727 if (type != NULL)
21728 SYMBOL_TYPE (sym) = type;
21729 else
21730 SYMBOL_TYPE (sym) = die_type (die, cu);
21731 attr = dwarf2_attr (die,
21732 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21733 cu);
21734 if (attr != nullptr)
21735 SYMBOL_LINE (sym) = attr->constant_value (0);
21736
21737 attr = dwarf2_attr (die,
21738 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21739 cu);
21740 if (attr != nullptr && attr->is_nonnegative ())
21741 {
21742 file_name_index file_index
21743 = (file_name_index) attr->as_nonnegative ();
21744 struct file_entry *fe;
21745
21746 if (cu->line_header != NULL)
21747 fe = cu->line_header->file_name_at (file_index);
21748 else
21749 fe = NULL;
21750
21751 if (fe == NULL)
21752 complaint (_("file index out of range"));
21753 else
21754 symbol_set_symtab (sym, fe->symtab);
21755 }
21756
21757 switch (die->tag)
21758 {
21759 case DW_TAG_label:
21760 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21761 if (attr != nullptr)
21762 {
21763 CORE_ADDR addr;
21764
21765 addr = attr->as_address ();
21766 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21767 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21768 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21769 }
21770 else
21771 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21772 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21773 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21774 add_symbol_to_list (sym, cu->list_in_scope);
21775 break;
21776 case DW_TAG_subprogram:
21777 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21778 finish_block. */
21779 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21780 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21781 if ((attr2 != nullptr && attr2->as_boolean ())
21782 || cu->per_cu->lang == language_ada
21783 || cu->per_cu->lang == language_fortran)
21784 {
21785 /* Subprograms marked external are stored as a global symbol.
21786 Ada and Fortran subprograms, whether marked external or
21787 not, are always stored as a global symbol, because we want
21788 to be able to access them globally. For instance, we want
21789 to be able to break on a nested subprogram without having
21790 to specify the context. */
21791 list_to_add = cu->get_builder ()->get_global_symbols ();
21792 }
21793 else
21794 {
21795 list_to_add = cu->list_in_scope;
21796 }
21797 break;
21798 case DW_TAG_inlined_subroutine:
21799 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21800 finish_block. */
21801 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21802 SYMBOL_INLINED (sym) = 1;
21803 list_to_add = cu->list_in_scope;
21804 break;
21805 case DW_TAG_template_value_param:
21806 suppress_add = 1;
21807 /* Fall through. */
21808 case DW_TAG_constant:
21809 case DW_TAG_variable:
21810 case DW_TAG_member:
21811 /* Compilation with minimal debug info may result in
21812 variables with missing type entries. Change the
21813 misleading `void' type to something sensible. */
21814 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21815 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21816
21817 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21818 /* In the case of DW_TAG_member, we should only be called for
21819 static const members. */
21820 if (die->tag == DW_TAG_member)
21821 {
21822 /* dwarf2_add_field uses die_is_declaration,
21823 so we do the same. */
21824 gdb_assert (die_is_declaration (die, cu));
21825 gdb_assert (attr);
21826 }
21827 if (attr != nullptr)
21828 {
21829 dwarf2_const_value (attr, sym, cu);
21830 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21831 if (!suppress_add)
21832 {
21833 if (attr2 != nullptr && attr2->as_boolean ())
21834 list_to_add = cu->get_builder ()->get_global_symbols ();
21835 else
21836 list_to_add = cu->list_in_scope;
21837 }
21838 break;
21839 }
21840 attr = dwarf2_attr (die, DW_AT_location, cu);
21841 if (attr != nullptr)
21842 {
21843 var_decode_location (attr, sym, cu);
21844 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21845
21846 /* Fortran explicitly imports any global symbols to the local
21847 scope by DW_TAG_common_block. */
21848 if (cu->per_cu->lang == language_fortran && die->parent
21849 && die->parent->tag == DW_TAG_common_block)
21850 attr2 = NULL;
21851
21852 if (SYMBOL_CLASS (sym) == LOC_STATIC
21853 && SYMBOL_VALUE_ADDRESS (sym) == 0
21854 && !per_objfile->per_bfd->has_section_at_zero)
21855 {
21856 /* When a static variable is eliminated by the linker,
21857 the corresponding debug information is not stripped
21858 out, but the variable address is set to null;
21859 do not add such variables into symbol table. */
21860 }
21861 else if (attr2 != nullptr && attr2->as_boolean ())
21862 {
21863 if (SYMBOL_CLASS (sym) == LOC_STATIC
21864 && (objfile->flags & OBJF_MAINLINE) == 0
21865 && per_objfile->per_bfd->can_copy)
21866 {
21867 /* A global static variable might be subject to
21868 copy relocation. We first check for a local
21869 minsym, though, because maybe the symbol was
21870 marked hidden, in which case this would not
21871 apply. */
21872 bound_minimal_symbol found
21873 = (lookup_minimal_symbol_linkage
21874 (sym->linkage_name (), objfile));
21875 if (found.minsym != nullptr)
21876 sym->maybe_copied = 1;
21877 }
21878
21879 /* A variable with DW_AT_external is never static,
21880 but it may be block-scoped. */
21881 list_to_add
21882 = ((cu->list_in_scope
21883 == cu->get_builder ()->get_file_symbols ())
21884 ? cu->get_builder ()->get_global_symbols ()
21885 : cu->list_in_scope);
21886 }
21887 else
21888 list_to_add = cu->list_in_scope;
21889 }
21890 else
21891 {
21892 /* We do not know the address of this symbol.
21893 If it is an external symbol and we have type information
21894 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21895 The address of the variable will then be determined from
21896 the minimal symbol table whenever the variable is
21897 referenced. */
21898 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21899
21900 /* Fortran explicitly imports any global symbols to the local
21901 scope by DW_TAG_common_block. */
21902 if (cu->per_cu->lang == language_fortran && die->parent
21903 && die->parent->tag == DW_TAG_common_block)
21904 {
21905 /* SYMBOL_CLASS doesn't matter here because
21906 read_common_block is going to reset it. */
21907 if (!suppress_add)
21908 list_to_add = cu->list_in_scope;
21909 }
21910 else if (attr2 != nullptr && attr2->as_boolean ()
21911 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21912 {
21913 /* A variable with DW_AT_external is never static, but it
21914 may be block-scoped. */
21915 list_to_add
21916 = ((cu->list_in_scope
21917 == cu->get_builder ()->get_file_symbols ())
21918 ? cu->get_builder ()->get_global_symbols ()
21919 : cu->list_in_scope);
21920
21921 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21922 }
21923 else if (!die_is_declaration (die, cu))
21924 {
21925 /* Use the default LOC_OPTIMIZED_OUT class. */
21926 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21927 if (!suppress_add)
21928 list_to_add = cu->list_in_scope;
21929 }
21930 }
21931 break;
21932 case DW_TAG_formal_parameter:
21933 {
21934 /* If we are inside a function, mark this as an argument. If
21935 not, we might be looking at an argument to an inlined function
21936 when we do not have enough information to show inlined frames;
21937 pretend it's a local variable in that case so that the user can
21938 still see it. */
21939 struct context_stack *curr
21940 = cu->get_builder ()->get_current_context_stack ();
21941 if (curr != nullptr && curr->name != nullptr)
21942 SYMBOL_IS_ARGUMENT (sym) = 1;
21943 attr = dwarf2_attr (die, DW_AT_location, cu);
21944 if (attr != nullptr)
21945 {
21946 var_decode_location (attr, sym, cu);
21947 }
21948 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21949 if (attr != nullptr)
21950 {
21951 dwarf2_const_value (attr, sym, cu);
21952 }
21953
21954 list_to_add = cu->list_in_scope;
21955 }
21956 break;
21957 case DW_TAG_unspecified_parameters:
21958 /* From varargs functions; gdb doesn't seem to have any
21959 interest in this information, so just ignore it for now.
21960 (FIXME?) */
21961 break;
21962 case DW_TAG_template_type_param:
21963 suppress_add = 1;
21964 /* Fall through. */
21965 case DW_TAG_class_type:
21966 case DW_TAG_interface_type:
21967 case DW_TAG_structure_type:
21968 case DW_TAG_union_type:
21969 case DW_TAG_set_type:
21970 case DW_TAG_enumeration_type:
21971 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21972 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21973
21974 {
21975 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21976 really ever be static objects: otherwise, if you try
21977 to, say, break of a class's method and you're in a file
21978 which doesn't mention that class, it won't work unless
21979 the check for all static symbols in lookup_symbol_aux
21980 saves you. See the OtherFileClass tests in
21981 gdb.c++/namespace.exp. */
21982
21983 if (!suppress_add)
21984 {
21985 buildsym_compunit *builder = cu->get_builder ();
21986 list_to_add
21987 = (cu->list_in_scope == builder->get_file_symbols ()
21988 && cu->per_cu->lang == language_cplus
21989 ? builder->get_global_symbols ()
21990 : cu->list_in_scope);
21991
21992 /* The semantics of C++ state that "struct foo {
21993 ... }" also defines a typedef for "foo". */
21994 if (cu->per_cu->lang == language_cplus
21995 || cu->per_cu->lang == language_ada
21996 || cu->per_cu->lang == language_d
21997 || cu->per_cu->lang == language_rust)
21998 {
21999 /* The symbol's name is already allocated along
22000 with this objfile, so we don't need to
22001 duplicate it for the type. */
22002 if (SYMBOL_TYPE (sym)->name () == 0)
22003 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22004 }
22005 }
22006 }
22007 break;
22008 case DW_TAG_typedef:
22009 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22010 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22011 list_to_add = cu->list_in_scope;
22012 break;
22013 case DW_TAG_array_type:
22014 case DW_TAG_base_type:
22015 case DW_TAG_subrange_type:
22016 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22017 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22018 list_to_add = cu->list_in_scope;
22019 break;
22020 case DW_TAG_enumerator:
22021 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22022 if (attr != nullptr)
22023 {
22024 dwarf2_const_value (attr, sym, cu);
22025 }
22026 {
22027 /* NOTE: carlton/2003-11-10: See comment above in the
22028 DW_TAG_class_type, etc. block. */
22029
22030 list_to_add
22031 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22032 && cu->per_cu->lang == language_cplus
22033 ? cu->get_builder ()->get_global_symbols ()
22034 : cu->list_in_scope);
22035 }
22036 break;
22037 case DW_TAG_imported_declaration:
22038 case DW_TAG_namespace:
22039 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22040 list_to_add = cu->get_builder ()->get_global_symbols ();
22041 break;
22042 case DW_TAG_module:
22043 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22044 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22045 list_to_add = cu->get_builder ()->get_global_symbols ();
22046 break;
22047 case DW_TAG_common_block:
22048 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22049 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22050 add_symbol_to_list (sym, cu->list_in_scope);
22051 break;
22052 default:
22053 /* Not a tag we recognize. Hopefully we aren't processing
22054 trash data, but since we must specifically ignore things
22055 we don't recognize, there is nothing else we should do at
22056 this point. */
22057 complaint (_("unsupported tag: '%s'"),
22058 dwarf_tag_name (die->tag));
22059 break;
22060 }
22061
22062 if (suppress_add)
22063 {
22064 sym->hash_next = objfile->template_symbols;
22065 objfile->template_symbols = sym;
22066 list_to_add = NULL;
22067 }
22068
22069 if (list_to_add != NULL)
22070 add_symbol_to_list (sym, list_to_add);
22071
22072 /* For the benefit of old versions of GCC, check for anonymous
22073 namespaces based on the demangled name. */
22074 if (!cu->processing_has_namespace_info
22075 && cu->per_cu->lang == language_cplus)
22076 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22077 }
22078 return (sym);
22079 }
22080
22081 /* Given an attr with a DW_FORM_dataN value in host byte order,
22082 zero-extend it as appropriate for the symbol's type. The DWARF
22083 standard (v4) is not entirely clear about the meaning of using
22084 DW_FORM_dataN for a constant with a signed type, where the type is
22085 wider than the data. The conclusion of a discussion on the DWARF
22086 list was that this is unspecified. We choose to always zero-extend
22087 because that is the interpretation long in use by GCC. */
22088
22089 static gdb_byte *
22090 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22091 struct dwarf2_cu *cu, LONGEST *value, int bits)
22092 {
22093 struct objfile *objfile = cu->per_objfile->objfile;
22094 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22095 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22096 LONGEST l = attr->constant_value (0);
22097
22098 if (bits < sizeof (*value) * 8)
22099 {
22100 l &= ((LONGEST) 1 << bits) - 1;
22101 *value = l;
22102 }
22103 else if (bits == sizeof (*value) * 8)
22104 *value = l;
22105 else
22106 {
22107 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22108 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22109 return bytes;
22110 }
22111
22112 return NULL;
22113 }
22114
22115 /* Read a constant value from an attribute. Either set *VALUE, or if
22116 the value does not fit in *VALUE, set *BYTES - either already
22117 allocated on the objfile obstack, or newly allocated on OBSTACK,
22118 or, set *BATON, if we translated the constant to a location
22119 expression. */
22120
22121 static void
22122 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22123 const char *name, struct obstack *obstack,
22124 struct dwarf2_cu *cu,
22125 LONGEST *value, const gdb_byte **bytes,
22126 struct dwarf2_locexpr_baton **baton)
22127 {
22128 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22129 struct objfile *objfile = per_objfile->objfile;
22130 struct comp_unit_head *cu_header = &cu->header;
22131 struct dwarf_block *blk;
22132 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22133 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22134
22135 *value = 0;
22136 *bytes = NULL;
22137 *baton = NULL;
22138
22139 switch (attr->form)
22140 {
22141 case DW_FORM_addr:
22142 case DW_FORM_addrx:
22143 case DW_FORM_GNU_addr_index:
22144 {
22145 gdb_byte *data;
22146
22147 if (TYPE_LENGTH (type) != cu_header->addr_size)
22148 dwarf2_const_value_length_mismatch_complaint (name,
22149 cu_header->addr_size,
22150 TYPE_LENGTH (type));
22151 /* Symbols of this form are reasonably rare, so we just
22152 piggyback on the existing location code rather than writing
22153 a new implementation of symbol_computed_ops. */
22154 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22155 (*baton)->per_objfile = per_objfile;
22156 (*baton)->per_cu = cu->per_cu;
22157 gdb_assert ((*baton)->per_cu);
22158
22159 (*baton)->size = 2 + cu_header->addr_size;
22160 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22161 (*baton)->data = data;
22162
22163 data[0] = DW_OP_addr;
22164 store_unsigned_integer (&data[1], cu_header->addr_size,
22165 byte_order, attr->as_address ());
22166 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22167 }
22168 break;
22169 case DW_FORM_string:
22170 case DW_FORM_strp:
22171 case DW_FORM_strx:
22172 case DW_FORM_GNU_str_index:
22173 case DW_FORM_GNU_strp_alt:
22174 /* The string is already allocated on the objfile obstack, point
22175 directly to it. */
22176 *bytes = (const gdb_byte *) attr->as_string ();
22177 break;
22178 case DW_FORM_block1:
22179 case DW_FORM_block2:
22180 case DW_FORM_block4:
22181 case DW_FORM_block:
22182 case DW_FORM_exprloc:
22183 case DW_FORM_data16:
22184 blk = attr->as_block ();
22185 if (TYPE_LENGTH (type) != blk->size)
22186 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22187 TYPE_LENGTH (type));
22188 *bytes = blk->data;
22189 break;
22190
22191 /* The DW_AT_const_value attributes are supposed to carry the
22192 symbol's value "represented as it would be on the target
22193 architecture." By the time we get here, it's already been
22194 converted to host endianness, so we just need to sign- or
22195 zero-extend it as appropriate. */
22196 case DW_FORM_data1:
22197 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22198 break;
22199 case DW_FORM_data2:
22200 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22201 break;
22202 case DW_FORM_data4:
22203 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22204 break;
22205 case DW_FORM_data8:
22206 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22207 break;
22208
22209 case DW_FORM_sdata:
22210 case DW_FORM_implicit_const:
22211 *value = attr->as_signed ();
22212 break;
22213
22214 case DW_FORM_udata:
22215 *value = attr->as_unsigned ();
22216 break;
22217
22218 default:
22219 complaint (_("unsupported const value attribute form: '%s'"),
22220 dwarf_form_name (attr->form));
22221 *value = 0;
22222 break;
22223 }
22224 }
22225
22226
22227 /* Copy constant value from an attribute to a symbol. */
22228
22229 static void
22230 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22231 struct dwarf2_cu *cu)
22232 {
22233 struct objfile *objfile = cu->per_objfile->objfile;
22234 LONGEST value;
22235 const gdb_byte *bytes;
22236 struct dwarf2_locexpr_baton *baton;
22237
22238 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22239 sym->print_name (),
22240 &objfile->objfile_obstack, cu,
22241 &value, &bytes, &baton);
22242
22243 if (baton != NULL)
22244 {
22245 SYMBOL_LOCATION_BATON (sym) = baton;
22246 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22247 }
22248 else if (bytes != NULL)
22249 {
22250 SYMBOL_VALUE_BYTES (sym) = bytes;
22251 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22252 }
22253 else
22254 {
22255 SYMBOL_VALUE (sym) = value;
22256 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22257 }
22258 }
22259
22260 /* Return the type of the die in question using its DW_AT_type attribute. */
22261
22262 static struct type *
22263 die_type (struct die_info *die, struct dwarf2_cu *cu)
22264 {
22265 struct attribute *type_attr;
22266
22267 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22268 if (!type_attr)
22269 {
22270 struct objfile *objfile = cu->per_objfile->objfile;
22271 /* A missing DW_AT_type represents a void type. */
22272 return objfile_type (objfile)->builtin_void;
22273 }
22274
22275 return lookup_die_type (die, type_attr, cu);
22276 }
22277
22278 /* True iff CU's producer generates GNAT Ada auxiliary information
22279 that allows to find parallel types through that information instead
22280 of having to do expensive parallel lookups by type name. */
22281
22282 static int
22283 need_gnat_info (struct dwarf2_cu *cu)
22284 {
22285 /* Assume that the Ada compiler was GNAT, which always produces
22286 the auxiliary information. */
22287 return (cu->per_cu->lang == language_ada);
22288 }
22289
22290 /* Return the auxiliary type of the die in question using its
22291 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22292 attribute is not present. */
22293
22294 static struct type *
22295 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22296 {
22297 struct attribute *type_attr;
22298
22299 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22300 if (!type_attr)
22301 return NULL;
22302
22303 return lookup_die_type (die, type_attr, cu);
22304 }
22305
22306 /* If DIE has a descriptive_type attribute, then set the TYPE's
22307 descriptive type accordingly. */
22308
22309 static void
22310 set_descriptive_type (struct type *type, struct die_info *die,
22311 struct dwarf2_cu *cu)
22312 {
22313 struct type *descriptive_type = die_descriptive_type (die, cu);
22314
22315 if (descriptive_type)
22316 {
22317 ALLOCATE_GNAT_AUX_TYPE (type);
22318 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22319 }
22320 }
22321
22322 /* Return the containing type of the die in question using its
22323 DW_AT_containing_type attribute. */
22324
22325 static struct type *
22326 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22327 {
22328 struct attribute *type_attr;
22329 struct objfile *objfile = cu->per_objfile->objfile;
22330
22331 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22332 if (!type_attr)
22333 error (_("Dwarf Error: Problem turning containing type into gdb type "
22334 "[in module %s]"), objfile_name (objfile));
22335
22336 return lookup_die_type (die, type_attr, cu);
22337 }
22338
22339 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22340
22341 static struct type *
22342 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22343 {
22344 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22345 struct objfile *objfile = per_objfile->objfile;
22346 char *saved;
22347
22348 std::string message
22349 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22350 objfile_name (objfile),
22351 sect_offset_str (cu->header.sect_off),
22352 sect_offset_str (die->sect_off));
22353 saved = obstack_strdup (&objfile->objfile_obstack, message);
22354
22355 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22356 }
22357
22358 /* Look up the type of DIE in CU using its type attribute ATTR.
22359 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22360 DW_AT_containing_type.
22361 If there is no type substitute an error marker. */
22362
22363 static struct type *
22364 lookup_die_type (struct die_info *die, const struct attribute *attr,
22365 struct dwarf2_cu *cu)
22366 {
22367 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22368 struct objfile *objfile = per_objfile->objfile;
22369 struct type *this_type;
22370
22371 gdb_assert (attr->name == DW_AT_type
22372 || attr->name == DW_AT_GNAT_descriptive_type
22373 || attr->name == DW_AT_containing_type);
22374
22375 /* First see if we have it cached. */
22376
22377 if (attr->form == DW_FORM_GNU_ref_alt)
22378 {
22379 struct dwarf2_per_cu_data *per_cu;
22380 sect_offset sect_off = attr->get_ref_die_offset ();
22381
22382 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22383 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22384 }
22385 else if (attr->form_is_ref ())
22386 {
22387 sect_offset sect_off = attr->get_ref_die_offset ();
22388
22389 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22390 }
22391 else if (attr->form == DW_FORM_ref_sig8)
22392 {
22393 ULONGEST signature = attr->as_signature ();
22394
22395 return get_signatured_type (die, signature, cu);
22396 }
22397 else
22398 {
22399 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22400 " at %s [in module %s]"),
22401 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22402 objfile_name (objfile));
22403 return build_error_marker_type (cu, die);
22404 }
22405
22406 /* If not cached we need to read it in. */
22407
22408 if (this_type == NULL)
22409 {
22410 struct die_info *type_die = NULL;
22411 struct dwarf2_cu *type_cu = cu;
22412
22413 if (attr->form_is_ref ())
22414 type_die = follow_die_ref (die, attr, &type_cu);
22415 if (type_die == NULL)
22416 return build_error_marker_type (cu, die);
22417 /* If we find the type now, it's probably because the type came
22418 from an inter-CU reference and the type's CU got expanded before
22419 ours. */
22420 this_type = read_type_die (type_die, type_cu);
22421 }
22422
22423 /* If we still don't have a type use an error marker. */
22424
22425 if (this_type == NULL)
22426 return build_error_marker_type (cu, die);
22427
22428 return this_type;
22429 }
22430
22431 /* Return the type in DIE, CU.
22432 Returns NULL for invalid types.
22433
22434 This first does a lookup in die_type_hash,
22435 and only reads the die in if necessary.
22436
22437 NOTE: This can be called when reading in partial or full symbols. */
22438
22439 static struct type *
22440 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22441 {
22442 struct type *this_type;
22443
22444 this_type = get_die_type (die, cu);
22445 if (this_type)
22446 return this_type;
22447
22448 return read_type_die_1 (die, cu);
22449 }
22450
22451 /* Read the type in DIE, CU.
22452 Returns NULL for invalid types. */
22453
22454 static struct type *
22455 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22456 {
22457 struct type *this_type = NULL;
22458
22459 switch (die->tag)
22460 {
22461 case DW_TAG_class_type:
22462 case DW_TAG_interface_type:
22463 case DW_TAG_structure_type:
22464 case DW_TAG_union_type:
22465 this_type = read_structure_type (die, cu);
22466 break;
22467 case DW_TAG_enumeration_type:
22468 this_type = read_enumeration_type (die, cu);
22469 break;
22470 case DW_TAG_subprogram:
22471 case DW_TAG_subroutine_type:
22472 case DW_TAG_inlined_subroutine:
22473 this_type = read_subroutine_type (die, cu);
22474 break;
22475 case DW_TAG_array_type:
22476 this_type = read_array_type (die, cu);
22477 break;
22478 case DW_TAG_set_type:
22479 this_type = read_set_type (die, cu);
22480 break;
22481 case DW_TAG_pointer_type:
22482 this_type = read_tag_pointer_type (die, cu);
22483 break;
22484 case DW_TAG_ptr_to_member_type:
22485 this_type = read_tag_ptr_to_member_type (die, cu);
22486 break;
22487 case DW_TAG_reference_type:
22488 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22489 break;
22490 case DW_TAG_rvalue_reference_type:
22491 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22492 break;
22493 case DW_TAG_const_type:
22494 this_type = read_tag_const_type (die, cu);
22495 break;
22496 case DW_TAG_volatile_type:
22497 this_type = read_tag_volatile_type (die, cu);
22498 break;
22499 case DW_TAG_restrict_type:
22500 this_type = read_tag_restrict_type (die, cu);
22501 break;
22502 case DW_TAG_string_type:
22503 this_type = read_tag_string_type (die, cu);
22504 break;
22505 case DW_TAG_typedef:
22506 this_type = read_typedef (die, cu);
22507 break;
22508 case DW_TAG_subrange_type:
22509 this_type = read_subrange_type (die, cu);
22510 break;
22511 case DW_TAG_base_type:
22512 this_type = read_base_type (die, cu);
22513 break;
22514 case DW_TAG_unspecified_type:
22515 this_type = read_unspecified_type (die, cu);
22516 break;
22517 case DW_TAG_namespace:
22518 this_type = read_namespace_type (die, cu);
22519 break;
22520 case DW_TAG_module:
22521 this_type = read_module_type (die, cu);
22522 break;
22523 case DW_TAG_atomic_type:
22524 this_type = read_tag_atomic_type (die, cu);
22525 break;
22526 default:
22527 complaint (_("unexpected tag in read_type_die: '%s'"),
22528 dwarf_tag_name (die->tag));
22529 break;
22530 }
22531
22532 return this_type;
22533 }
22534
22535 /* See if we can figure out if the class lives in a namespace. We do
22536 this by looking for a member function; its demangled name will
22537 contain namespace info, if there is any.
22538 Return the computed name or NULL.
22539 Space for the result is allocated on the objfile's obstack.
22540 This is the full-die version of guess_partial_die_structure_name.
22541 In this case we know DIE has no useful parent. */
22542
22543 static const char *
22544 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22545 {
22546 struct die_info *spec_die;
22547 struct dwarf2_cu *spec_cu;
22548 struct die_info *child;
22549 struct objfile *objfile = cu->per_objfile->objfile;
22550
22551 spec_cu = cu;
22552 spec_die = die_specification (die, &spec_cu);
22553 if (spec_die != NULL)
22554 {
22555 die = spec_die;
22556 cu = spec_cu;
22557 }
22558
22559 for (child = die->child;
22560 child != NULL;
22561 child = child->sibling)
22562 {
22563 if (child->tag == DW_TAG_subprogram)
22564 {
22565 const char *linkage_name = dw2_linkage_name (child, cu);
22566
22567 if (linkage_name != NULL)
22568 {
22569 gdb::unique_xmalloc_ptr<char> actual_name
22570 (cu->language_defn->class_name_from_physname (linkage_name));
22571 const char *name = NULL;
22572
22573 if (actual_name != NULL)
22574 {
22575 const char *die_name = dwarf2_name (die, cu);
22576
22577 if (die_name != NULL
22578 && strcmp (die_name, actual_name.get ()) != 0)
22579 {
22580 /* Strip off the class name from the full name.
22581 We want the prefix. */
22582 int die_name_len = strlen (die_name);
22583 int actual_name_len = strlen (actual_name.get ());
22584 const char *ptr = actual_name.get ();
22585
22586 /* Test for '::' as a sanity check. */
22587 if (actual_name_len > die_name_len + 2
22588 && ptr[actual_name_len - die_name_len - 1] == ':')
22589 name = obstack_strndup (
22590 &objfile->per_bfd->storage_obstack,
22591 ptr, actual_name_len - die_name_len - 2);
22592 }
22593 }
22594 return name;
22595 }
22596 }
22597 }
22598
22599 return NULL;
22600 }
22601
22602 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22603 prefix part in such case. See
22604 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22605
22606 static const char *
22607 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22608 {
22609 struct attribute *attr;
22610 const char *base;
22611
22612 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22613 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22614 return NULL;
22615
22616 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22617 return NULL;
22618
22619 attr = dw2_linkage_name_attr (die, cu);
22620 const char *attr_name = attr->as_string ();
22621 if (attr == NULL || attr_name == NULL)
22622 return NULL;
22623
22624 /* dwarf2_name had to be already called. */
22625 gdb_assert (attr->canonical_string_p ());
22626
22627 /* Strip the base name, keep any leading namespaces/classes. */
22628 base = strrchr (attr_name, ':');
22629 if (base == NULL || base == attr_name || base[-1] != ':')
22630 return "";
22631
22632 struct objfile *objfile = cu->per_objfile->objfile;
22633 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22634 attr_name,
22635 &base[-1] - attr_name);
22636 }
22637
22638 /* Return the name of the namespace/class that DIE is defined within,
22639 or "" if we can't tell. The caller should not xfree the result.
22640
22641 For example, if we're within the method foo() in the following
22642 code:
22643
22644 namespace N {
22645 class C {
22646 void foo () {
22647 }
22648 };
22649 }
22650
22651 then determine_prefix on foo's die will return "N::C". */
22652
22653 static const char *
22654 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22655 {
22656 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22657 struct die_info *parent, *spec_die;
22658 struct dwarf2_cu *spec_cu;
22659 struct type *parent_type;
22660 const char *retval;
22661
22662 if (cu->per_cu->lang != language_cplus
22663 && cu->per_cu->lang != language_fortran
22664 && cu->per_cu->lang != language_d
22665 && cu->per_cu->lang != language_rust)
22666 return "";
22667
22668 retval = anonymous_struct_prefix (die, cu);
22669 if (retval)
22670 return retval;
22671
22672 /* We have to be careful in the presence of DW_AT_specification.
22673 For example, with GCC 3.4, given the code
22674
22675 namespace N {
22676 void foo() {
22677 // Definition of N::foo.
22678 }
22679 }
22680
22681 then we'll have a tree of DIEs like this:
22682
22683 1: DW_TAG_compile_unit
22684 2: DW_TAG_namespace // N
22685 3: DW_TAG_subprogram // declaration of N::foo
22686 4: DW_TAG_subprogram // definition of N::foo
22687 DW_AT_specification // refers to die #3
22688
22689 Thus, when processing die #4, we have to pretend that we're in
22690 the context of its DW_AT_specification, namely the contex of die
22691 #3. */
22692 spec_cu = cu;
22693 spec_die = die_specification (die, &spec_cu);
22694 if (spec_die == NULL)
22695 parent = die->parent;
22696 else
22697 {
22698 parent = spec_die->parent;
22699 cu = spec_cu;
22700 }
22701
22702 if (parent == NULL)
22703 return "";
22704 else if (parent->building_fullname)
22705 {
22706 const char *name;
22707 const char *parent_name;
22708
22709 /* It has been seen on RealView 2.2 built binaries,
22710 DW_TAG_template_type_param types actually _defined_ as
22711 children of the parent class:
22712
22713 enum E {};
22714 template class <class Enum> Class{};
22715 Class<enum E> class_e;
22716
22717 1: DW_TAG_class_type (Class)
22718 2: DW_TAG_enumeration_type (E)
22719 3: DW_TAG_enumerator (enum1:0)
22720 3: DW_TAG_enumerator (enum2:1)
22721 ...
22722 2: DW_TAG_template_type_param
22723 DW_AT_type DW_FORM_ref_udata (E)
22724
22725 Besides being broken debug info, it can put GDB into an
22726 infinite loop. Consider:
22727
22728 When we're building the full name for Class<E>, we'll start
22729 at Class, and go look over its template type parameters,
22730 finding E. We'll then try to build the full name of E, and
22731 reach here. We're now trying to build the full name of E,
22732 and look over the parent DIE for containing scope. In the
22733 broken case, if we followed the parent DIE of E, we'd again
22734 find Class, and once again go look at its template type
22735 arguments, etc., etc. Simply don't consider such parent die
22736 as source-level parent of this die (it can't be, the language
22737 doesn't allow it), and break the loop here. */
22738 name = dwarf2_name (die, cu);
22739 parent_name = dwarf2_name (parent, cu);
22740 complaint (_("template param type '%s' defined within parent '%s'"),
22741 name ? name : "<unknown>",
22742 parent_name ? parent_name : "<unknown>");
22743 return "";
22744 }
22745 else
22746 switch (parent->tag)
22747 {
22748 case DW_TAG_namespace:
22749 parent_type = read_type_die (parent, cu);
22750 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22751 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22752 Work around this problem here. */
22753 if (cu->per_cu->lang == language_cplus
22754 && strcmp (parent_type->name (), "::") == 0)
22755 return "";
22756 /* We give a name to even anonymous namespaces. */
22757 return parent_type->name ();
22758 case DW_TAG_class_type:
22759 case DW_TAG_interface_type:
22760 case DW_TAG_structure_type:
22761 case DW_TAG_union_type:
22762 case DW_TAG_module:
22763 parent_type = read_type_die (parent, cu);
22764 if (parent_type->name () != NULL)
22765 return parent_type->name ();
22766 else
22767 /* An anonymous structure is only allowed non-static data
22768 members; no typedefs, no member functions, et cetera.
22769 So it does not need a prefix. */
22770 return "";
22771 case DW_TAG_compile_unit:
22772 case DW_TAG_partial_unit:
22773 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22774 if (cu->per_cu->lang == language_cplus
22775 && !per_objfile->per_bfd->types.empty ()
22776 && die->child != NULL
22777 && (die->tag == DW_TAG_class_type
22778 || die->tag == DW_TAG_structure_type
22779 || die->tag == DW_TAG_union_type))
22780 {
22781 const char *name = guess_full_die_structure_name (die, cu);
22782 if (name != NULL)
22783 return name;
22784 }
22785 return "";
22786 case DW_TAG_subprogram:
22787 /* Nested subroutines in Fortran get a prefix with the name
22788 of the parent's subroutine. */
22789 if (cu->per_cu->lang == language_fortran)
22790 {
22791 if ((die->tag == DW_TAG_subprogram)
22792 && (dwarf2_name (parent, cu) != NULL))
22793 return dwarf2_name (parent, cu);
22794 }
22795 return "";
22796 case DW_TAG_enumeration_type:
22797 parent_type = read_type_die (parent, cu);
22798 if (parent_type->is_declared_class ())
22799 {
22800 if (parent_type->name () != NULL)
22801 return parent_type->name ();
22802 return "";
22803 }
22804 /* Fall through. */
22805 default:
22806 return determine_prefix (parent, cu);
22807 }
22808 }
22809
22810 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22811 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22812 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22813 an obconcat, otherwise allocate storage for the result. The CU argument is
22814 used to determine the language and hence, the appropriate separator. */
22815
22816 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22817
22818 static char *
22819 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22820 int physname, struct dwarf2_cu *cu)
22821 {
22822 const char *lead = "";
22823 const char *sep;
22824
22825 if (suffix == NULL || suffix[0] == '\0'
22826 || prefix == NULL || prefix[0] == '\0')
22827 sep = "";
22828 else if (cu->per_cu->lang == language_d)
22829 {
22830 /* For D, the 'main' function could be defined in any module, but it
22831 should never be prefixed. */
22832 if (strcmp (suffix, "D main") == 0)
22833 {
22834 prefix = "";
22835 sep = "";
22836 }
22837 else
22838 sep = ".";
22839 }
22840 else if (cu->per_cu->lang == language_fortran && physname)
22841 {
22842 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22843 DW_AT_MIPS_linkage_name is preferred and used instead. */
22844
22845 lead = "__";
22846 sep = "_MOD_";
22847 }
22848 else
22849 sep = "::";
22850
22851 if (prefix == NULL)
22852 prefix = "";
22853 if (suffix == NULL)
22854 suffix = "";
22855
22856 if (obs == NULL)
22857 {
22858 char *retval
22859 = ((char *)
22860 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22861
22862 strcpy (retval, lead);
22863 strcat (retval, prefix);
22864 strcat (retval, sep);
22865 strcat (retval, suffix);
22866 return retval;
22867 }
22868 else
22869 {
22870 /* We have an obstack. */
22871 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22872 }
22873 }
22874
22875 /* Get name of a die, return NULL if not found. */
22876
22877 static const char *
22878 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22879 struct objfile *objfile)
22880 {
22881 if (name && cu->per_cu->lang == language_cplus)
22882 {
22883 gdb::unique_xmalloc_ptr<char> canon_name
22884 = cp_canonicalize_string (name);
22885
22886 if (canon_name != nullptr)
22887 name = objfile->intern (canon_name.get ());
22888 }
22889
22890 return name;
22891 }
22892
22893 /* Get name of a die, return NULL if not found.
22894 Anonymous namespaces are converted to their magic string. */
22895
22896 static const char *
22897 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22898 {
22899 struct attribute *attr;
22900 struct objfile *objfile = cu->per_objfile->objfile;
22901
22902 attr = dwarf2_attr (die, DW_AT_name, cu);
22903 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22904 if (attr_name == nullptr
22905 && die->tag != DW_TAG_namespace
22906 && die->tag != DW_TAG_class_type
22907 && die->tag != DW_TAG_interface_type
22908 && die->tag != DW_TAG_structure_type
22909 && die->tag != DW_TAG_union_type)
22910 return NULL;
22911
22912 switch (die->tag)
22913 {
22914 case DW_TAG_compile_unit:
22915 case DW_TAG_partial_unit:
22916 /* Compilation units have a DW_AT_name that is a filename, not
22917 a source language identifier. */
22918 case DW_TAG_enumeration_type:
22919 case DW_TAG_enumerator:
22920 /* These tags always have simple identifiers already; no need
22921 to canonicalize them. */
22922 return attr_name;
22923
22924 case DW_TAG_namespace:
22925 if (attr_name != nullptr)
22926 return attr_name;
22927 return CP_ANONYMOUS_NAMESPACE_STR;
22928
22929 case DW_TAG_class_type:
22930 case DW_TAG_interface_type:
22931 case DW_TAG_structure_type:
22932 case DW_TAG_union_type:
22933 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22934 structures or unions. These were of the form "._%d" in GCC 4.1,
22935 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22936 and GCC 4.4. We work around this problem by ignoring these. */
22937 if (attr_name != nullptr
22938 && (startswith (attr_name, "._")
22939 || startswith (attr_name, "<anonymous")))
22940 return NULL;
22941
22942 /* GCC might emit a nameless typedef that has a linkage name. See
22943 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22944 if (!attr || attr_name == NULL)
22945 {
22946 attr = dw2_linkage_name_attr (die, cu);
22947 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22948 if (attr == NULL || attr_name == NULL)
22949 return NULL;
22950
22951 /* Avoid demangling attr_name the second time on a second
22952 call for the same DIE. */
22953 if (!attr->canonical_string_p ())
22954 {
22955 gdb::unique_xmalloc_ptr<char> demangled
22956 (gdb_demangle (attr_name, DMGL_TYPES));
22957 if (demangled == nullptr)
22958 return nullptr;
22959
22960 attr->set_string_canonical (objfile->intern (demangled.get ()));
22961 attr_name = attr->as_string ();
22962 }
22963
22964 /* Strip any leading namespaces/classes, keep only the
22965 base name. DW_AT_name for named DIEs does not
22966 contain the prefixes. */
22967 const char *base = strrchr (attr_name, ':');
22968 if (base && base > attr_name && base[-1] == ':')
22969 return &base[1];
22970 else
22971 return attr_name;
22972 }
22973 break;
22974
22975 default:
22976 break;
22977 }
22978
22979 if (!attr->canonical_string_p ())
22980 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22981 objfile));
22982 return attr->as_string ();
22983 }
22984
22985 /* Return the die that this die in an extension of, or NULL if there
22986 is none. *EXT_CU is the CU containing DIE on input, and the CU
22987 containing the return value on output. */
22988
22989 static struct die_info *
22990 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22991 {
22992 struct attribute *attr;
22993
22994 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22995 if (attr == NULL)
22996 return NULL;
22997
22998 return follow_die_ref (die, attr, ext_cu);
22999 }
23000
23001 static void
23002 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23003 {
23004 unsigned int i;
23005
23006 print_spaces (indent, f);
23007 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23008 dwarf_tag_name (die->tag), die->abbrev,
23009 sect_offset_str (die->sect_off));
23010
23011 if (die->parent != NULL)
23012 {
23013 print_spaces (indent, f);
23014 fprintf_unfiltered (f, " parent at offset: %s\n",
23015 sect_offset_str (die->parent->sect_off));
23016 }
23017
23018 print_spaces (indent, f);
23019 fprintf_unfiltered (f, " has children: %s\n",
23020 dwarf_bool_name (die->child != NULL));
23021
23022 print_spaces (indent, f);
23023 fprintf_unfiltered (f, " attributes:\n");
23024
23025 for (i = 0; i < die->num_attrs; ++i)
23026 {
23027 print_spaces (indent, f);
23028 fprintf_unfiltered (f, " %s (%s) ",
23029 dwarf_attr_name (die->attrs[i].name),
23030 dwarf_form_name (die->attrs[i].form));
23031
23032 switch (die->attrs[i].form)
23033 {
23034 case DW_FORM_addr:
23035 case DW_FORM_addrx:
23036 case DW_FORM_GNU_addr_index:
23037 fprintf_unfiltered (f, "address: ");
23038 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23039 break;
23040 case DW_FORM_block2:
23041 case DW_FORM_block4:
23042 case DW_FORM_block:
23043 case DW_FORM_block1:
23044 fprintf_unfiltered (f, "block: size %s",
23045 pulongest (die->attrs[i].as_block ()->size));
23046 break;
23047 case DW_FORM_exprloc:
23048 fprintf_unfiltered (f, "expression: size %s",
23049 pulongest (die->attrs[i].as_block ()->size));
23050 break;
23051 case DW_FORM_data16:
23052 fprintf_unfiltered (f, "constant of 16 bytes");
23053 break;
23054 case DW_FORM_ref_addr:
23055 fprintf_unfiltered (f, "ref address: ");
23056 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23057 break;
23058 case DW_FORM_GNU_ref_alt:
23059 fprintf_unfiltered (f, "alt ref address: ");
23060 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23061 break;
23062 case DW_FORM_ref1:
23063 case DW_FORM_ref2:
23064 case DW_FORM_ref4:
23065 case DW_FORM_ref8:
23066 case DW_FORM_ref_udata:
23067 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23068 (long) (die->attrs[i].as_unsigned ()));
23069 break;
23070 case DW_FORM_data1:
23071 case DW_FORM_data2:
23072 case DW_FORM_data4:
23073 case DW_FORM_data8:
23074 case DW_FORM_udata:
23075 fprintf_unfiltered (f, "constant: %s",
23076 pulongest (die->attrs[i].as_unsigned ()));
23077 break;
23078 case DW_FORM_sec_offset:
23079 fprintf_unfiltered (f, "section offset: %s",
23080 pulongest (die->attrs[i].as_unsigned ()));
23081 break;
23082 case DW_FORM_ref_sig8:
23083 fprintf_unfiltered (f, "signature: %s",
23084 hex_string (die->attrs[i].as_signature ()));
23085 break;
23086 case DW_FORM_string:
23087 case DW_FORM_strp:
23088 case DW_FORM_line_strp:
23089 case DW_FORM_strx:
23090 case DW_FORM_GNU_str_index:
23091 case DW_FORM_GNU_strp_alt:
23092 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23093 die->attrs[i].as_string ()
23094 ? die->attrs[i].as_string () : "",
23095 die->attrs[i].canonical_string_p () ? "is" : "not");
23096 break;
23097 case DW_FORM_flag:
23098 if (die->attrs[i].as_boolean ())
23099 fprintf_unfiltered (f, "flag: TRUE");
23100 else
23101 fprintf_unfiltered (f, "flag: FALSE");
23102 break;
23103 case DW_FORM_flag_present:
23104 fprintf_unfiltered (f, "flag: TRUE");
23105 break;
23106 case DW_FORM_indirect:
23107 /* The reader will have reduced the indirect form to
23108 the "base form" so this form should not occur. */
23109 fprintf_unfiltered (f,
23110 "unexpected attribute form: DW_FORM_indirect");
23111 break;
23112 case DW_FORM_sdata:
23113 case DW_FORM_implicit_const:
23114 fprintf_unfiltered (f, "constant: %s",
23115 plongest (die->attrs[i].as_signed ()));
23116 break;
23117 default:
23118 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23119 die->attrs[i].form);
23120 break;
23121 }
23122 fprintf_unfiltered (f, "\n");
23123 }
23124 }
23125
23126 static void
23127 dump_die_for_error (struct die_info *die)
23128 {
23129 dump_die_shallow (gdb_stderr, 0, die);
23130 }
23131
23132 static void
23133 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23134 {
23135 int indent = level * 4;
23136
23137 gdb_assert (die != NULL);
23138
23139 if (level >= max_level)
23140 return;
23141
23142 dump_die_shallow (f, indent, die);
23143
23144 if (die->child != NULL)
23145 {
23146 print_spaces (indent, f);
23147 fprintf_unfiltered (f, " Children:");
23148 if (level + 1 < max_level)
23149 {
23150 fprintf_unfiltered (f, "\n");
23151 dump_die_1 (f, level + 1, max_level, die->child);
23152 }
23153 else
23154 {
23155 fprintf_unfiltered (f,
23156 " [not printed, max nesting level reached]\n");
23157 }
23158 }
23159
23160 if (die->sibling != NULL && level > 0)
23161 {
23162 dump_die_1 (f, level, max_level, die->sibling);
23163 }
23164 }
23165
23166 /* This is called from the pdie macro in gdbinit.in.
23167 It's not static so gcc will keep a copy callable from gdb. */
23168
23169 void
23170 dump_die (struct die_info *die, int max_level)
23171 {
23172 dump_die_1 (gdb_stdlog, 0, max_level, die);
23173 }
23174
23175 static void
23176 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23177 {
23178 void **slot;
23179
23180 slot = htab_find_slot_with_hash (cu->die_hash, die,
23181 to_underlying (die->sect_off),
23182 INSERT);
23183
23184 *slot = die;
23185 }
23186
23187 /* Follow reference or signature attribute ATTR of SRC_DIE.
23188 On entry *REF_CU is the CU of SRC_DIE.
23189 On exit *REF_CU is the CU of the result. */
23190
23191 static struct die_info *
23192 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23193 struct dwarf2_cu **ref_cu)
23194 {
23195 struct die_info *die;
23196
23197 if (attr->form_is_ref ())
23198 die = follow_die_ref (src_die, attr, ref_cu);
23199 else if (attr->form == DW_FORM_ref_sig8)
23200 die = follow_die_sig (src_die, attr, ref_cu);
23201 else
23202 {
23203 dump_die_for_error (src_die);
23204 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23205 objfile_name ((*ref_cu)->per_objfile->objfile));
23206 }
23207
23208 return die;
23209 }
23210
23211 /* Follow reference OFFSET.
23212 On entry *REF_CU is the CU of the source die referencing OFFSET.
23213 On exit *REF_CU is the CU of the result.
23214 Returns NULL if OFFSET is invalid. */
23215
23216 static struct die_info *
23217 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23218 struct dwarf2_cu **ref_cu)
23219 {
23220 struct die_info temp_die;
23221 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23222 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23223
23224 gdb_assert (cu->per_cu != NULL);
23225
23226 target_cu = cu;
23227
23228 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23229 "source CU contains target offset: %d",
23230 sect_offset_str (cu->per_cu->sect_off),
23231 sect_offset_str (sect_off),
23232 cu->header.offset_in_cu_p (sect_off));
23233
23234 if (cu->per_cu->is_debug_types)
23235 {
23236 /* .debug_types CUs cannot reference anything outside their CU.
23237 If they need to, they have to reference a signatured type via
23238 DW_FORM_ref_sig8. */
23239 if (!cu->header.offset_in_cu_p (sect_off))
23240 return NULL;
23241 }
23242 else if (offset_in_dwz != cu->per_cu->is_dwz
23243 || !cu->header.offset_in_cu_p (sect_off))
23244 {
23245 struct dwarf2_per_cu_data *per_cu;
23246
23247 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23248 per_objfile);
23249
23250 dwarf_read_debug_printf_v ("target CU offset: %s, "
23251 "target CU DIEs loaded: %d",
23252 sect_offset_str (per_cu->sect_off),
23253 per_objfile->get_cu (per_cu) != nullptr);
23254
23255 /* If necessary, add it to the queue and load its DIEs.
23256
23257 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23258 it doesn't mean they are currently loaded. Since we require them
23259 to be loaded, we must check for ourselves. */
23260 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
23261 || per_objfile->get_cu (per_cu) == nullptr)
23262 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23263 false, cu->per_cu->lang);
23264
23265 target_cu = per_objfile->get_cu (per_cu);
23266 gdb_assert (target_cu != nullptr);
23267 }
23268 else if (cu->dies == NULL)
23269 {
23270 /* We're loading full DIEs during partial symbol reading. */
23271 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23272 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23273 language_minimal);
23274 }
23275
23276 *ref_cu = target_cu;
23277 temp_die.sect_off = sect_off;
23278
23279 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23280 &temp_die,
23281 to_underlying (sect_off));
23282 }
23283
23284 /* Follow reference attribute ATTR of SRC_DIE.
23285 On entry *REF_CU is the CU of SRC_DIE.
23286 On exit *REF_CU is the CU of the result. */
23287
23288 static struct die_info *
23289 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23290 struct dwarf2_cu **ref_cu)
23291 {
23292 sect_offset sect_off = attr->get_ref_die_offset ();
23293 struct dwarf2_cu *cu = *ref_cu;
23294 struct die_info *die;
23295
23296 die = follow_die_offset (sect_off,
23297 (attr->form == DW_FORM_GNU_ref_alt
23298 || cu->per_cu->is_dwz),
23299 ref_cu);
23300 if (!die)
23301 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23302 "at %s [in module %s]"),
23303 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23304 objfile_name (cu->per_objfile->objfile));
23305
23306 return die;
23307 }
23308
23309 /* See read.h. */
23310
23311 struct dwarf2_locexpr_baton
23312 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23313 dwarf2_per_cu_data *per_cu,
23314 dwarf2_per_objfile *per_objfile,
23315 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23316 bool resolve_abstract_p)
23317 {
23318 struct die_info *die;
23319 struct attribute *attr;
23320 struct dwarf2_locexpr_baton retval;
23321 struct objfile *objfile = per_objfile->objfile;
23322
23323 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23324 if (cu == nullptr)
23325 cu = load_cu (per_cu, per_objfile, false);
23326
23327 if (cu == nullptr)
23328 {
23329 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23330 Instead just throw an error, not much else we can do. */
23331 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23332 sect_offset_str (sect_off), objfile_name (objfile));
23333 }
23334
23335 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23336 if (!die)
23337 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23338 sect_offset_str (sect_off), objfile_name (objfile));
23339
23340 attr = dwarf2_attr (die, DW_AT_location, cu);
23341 if (!attr && resolve_abstract_p
23342 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23343 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23344 {
23345 CORE_ADDR pc = get_frame_pc ();
23346 CORE_ADDR baseaddr = objfile->text_section_offset ();
23347 struct gdbarch *gdbarch = objfile->arch ();
23348
23349 for (const auto &cand_off
23350 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23351 {
23352 struct dwarf2_cu *cand_cu = cu;
23353 struct die_info *cand
23354 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23355 if (!cand
23356 || !cand->parent
23357 || cand->parent->tag != DW_TAG_subprogram)
23358 continue;
23359
23360 CORE_ADDR pc_low, pc_high;
23361 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23362 if (pc_low == ((CORE_ADDR) -1))
23363 continue;
23364 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23365 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23366 if (!(pc_low <= pc && pc < pc_high))
23367 continue;
23368
23369 die = cand;
23370 attr = dwarf2_attr (die, DW_AT_location, cu);
23371 break;
23372 }
23373 }
23374
23375 if (!attr)
23376 {
23377 /* DWARF: "If there is no such attribute, then there is no effect.".
23378 DATA is ignored if SIZE is 0. */
23379
23380 retval.data = NULL;
23381 retval.size = 0;
23382 }
23383 else if (attr->form_is_section_offset ())
23384 {
23385 struct dwarf2_loclist_baton loclist_baton;
23386 CORE_ADDR pc = get_frame_pc ();
23387 size_t size;
23388
23389 fill_in_loclist_baton (cu, &loclist_baton, attr);
23390
23391 retval.data = dwarf2_find_location_expression (&loclist_baton,
23392 &size, pc);
23393 retval.size = size;
23394 }
23395 else
23396 {
23397 if (!attr->form_is_block ())
23398 error (_("Dwarf Error: DIE at %s referenced in module %s "
23399 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23400 sect_offset_str (sect_off), objfile_name (objfile));
23401
23402 struct dwarf_block *block = attr->as_block ();
23403 retval.data = block->data;
23404 retval.size = block->size;
23405 }
23406 retval.per_objfile = per_objfile;
23407 retval.per_cu = cu->per_cu;
23408
23409 per_objfile->age_comp_units ();
23410
23411 return retval;
23412 }
23413
23414 /* See read.h. */
23415
23416 struct dwarf2_locexpr_baton
23417 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23418 dwarf2_per_cu_data *per_cu,
23419 dwarf2_per_objfile *per_objfile,
23420 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23421 {
23422 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23423
23424 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23425 get_frame_pc);
23426 }
23427
23428 /* Write a constant of a given type as target-ordered bytes into
23429 OBSTACK. */
23430
23431 static const gdb_byte *
23432 write_constant_as_bytes (struct obstack *obstack,
23433 enum bfd_endian byte_order,
23434 struct type *type,
23435 ULONGEST value,
23436 LONGEST *len)
23437 {
23438 gdb_byte *result;
23439
23440 *len = TYPE_LENGTH (type);
23441 result = (gdb_byte *) obstack_alloc (obstack, *len);
23442 store_unsigned_integer (result, *len, byte_order, value);
23443
23444 return result;
23445 }
23446
23447 /* See read.h. */
23448
23449 const gdb_byte *
23450 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23451 dwarf2_per_cu_data *per_cu,
23452 dwarf2_per_objfile *per_objfile,
23453 obstack *obstack,
23454 LONGEST *len)
23455 {
23456 struct die_info *die;
23457 struct attribute *attr;
23458 const gdb_byte *result = NULL;
23459 struct type *type;
23460 LONGEST value;
23461 enum bfd_endian byte_order;
23462 struct objfile *objfile = per_objfile->objfile;
23463
23464 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23465 if (cu == nullptr)
23466 cu = load_cu (per_cu, per_objfile, false);
23467
23468 if (cu == nullptr)
23469 {
23470 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23471 Instead just throw an error, not much else we can do. */
23472 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23473 sect_offset_str (sect_off), objfile_name (objfile));
23474 }
23475
23476 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23477 if (!die)
23478 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23479 sect_offset_str (sect_off), objfile_name (objfile));
23480
23481 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23482 if (attr == NULL)
23483 return NULL;
23484
23485 byte_order = (bfd_big_endian (objfile->obfd)
23486 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23487
23488 switch (attr->form)
23489 {
23490 case DW_FORM_addr:
23491 case DW_FORM_addrx:
23492 case DW_FORM_GNU_addr_index:
23493 {
23494 gdb_byte *tem;
23495
23496 *len = cu->header.addr_size;
23497 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23498 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23499 result = tem;
23500 }
23501 break;
23502 case DW_FORM_string:
23503 case DW_FORM_strp:
23504 case DW_FORM_strx:
23505 case DW_FORM_GNU_str_index:
23506 case DW_FORM_GNU_strp_alt:
23507 /* The string is already allocated on the objfile obstack, point
23508 directly to it. */
23509 {
23510 const char *attr_name = attr->as_string ();
23511 result = (const gdb_byte *) attr_name;
23512 *len = strlen (attr_name);
23513 }
23514 break;
23515 case DW_FORM_block1:
23516 case DW_FORM_block2:
23517 case DW_FORM_block4:
23518 case DW_FORM_block:
23519 case DW_FORM_exprloc:
23520 case DW_FORM_data16:
23521 {
23522 struct dwarf_block *block = attr->as_block ();
23523 result = block->data;
23524 *len = block->size;
23525 }
23526 break;
23527
23528 /* The DW_AT_const_value attributes are supposed to carry the
23529 symbol's value "represented as it would be on the target
23530 architecture." By the time we get here, it's already been
23531 converted to host endianness, so we just need to sign- or
23532 zero-extend it as appropriate. */
23533 case DW_FORM_data1:
23534 type = die_type (die, cu);
23535 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23536 if (result == NULL)
23537 result = write_constant_as_bytes (obstack, byte_order,
23538 type, value, len);
23539 break;
23540 case DW_FORM_data2:
23541 type = die_type (die, cu);
23542 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23543 if (result == NULL)
23544 result = write_constant_as_bytes (obstack, byte_order,
23545 type, value, len);
23546 break;
23547 case DW_FORM_data4:
23548 type = die_type (die, cu);
23549 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23550 if (result == NULL)
23551 result = write_constant_as_bytes (obstack, byte_order,
23552 type, value, len);
23553 break;
23554 case DW_FORM_data8:
23555 type = die_type (die, cu);
23556 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23557 if (result == NULL)
23558 result = write_constant_as_bytes (obstack, byte_order,
23559 type, value, len);
23560 break;
23561
23562 case DW_FORM_sdata:
23563 case DW_FORM_implicit_const:
23564 type = die_type (die, cu);
23565 result = write_constant_as_bytes (obstack, byte_order,
23566 type, attr->as_signed (), len);
23567 break;
23568
23569 case DW_FORM_udata:
23570 type = die_type (die, cu);
23571 result = write_constant_as_bytes (obstack, byte_order,
23572 type, attr->as_unsigned (), len);
23573 break;
23574
23575 default:
23576 complaint (_("unsupported const value attribute form: '%s'"),
23577 dwarf_form_name (attr->form));
23578 break;
23579 }
23580
23581 return result;
23582 }
23583
23584 /* See read.h. */
23585
23586 struct type *
23587 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23588 dwarf2_per_cu_data *per_cu,
23589 dwarf2_per_objfile *per_objfile,
23590 const char **var_name)
23591 {
23592 struct die_info *die;
23593
23594 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23595 if (cu == nullptr)
23596 cu = load_cu (per_cu, per_objfile, false);
23597
23598 if (cu == nullptr)
23599 return nullptr;
23600
23601 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23602 if (!die)
23603 return NULL;
23604
23605 if (var_name != nullptr)
23606 *var_name = var_decl_name (die, cu);
23607 return die_type (die, cu);
23608 }
23609
23610 /* See read.h. */
23611
23612 struct type *
23613 dwarf2_get_die_type (cu_offset die_offset,
23614 dwarf2_per_cu_data *per_cu,
23615 dwarf2_per_objfile *per_objfile)
23616 {
23617 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23618 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23619 }
23620
23621 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23622 On entry *REF_CU is the CU of SRC_DIE.
23623 On exit *REF_CU is the CU of the result.
23624 Returns NULL if the referenced DIE isn't found. */
23625
23626 static struct die_info *
23627 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23628 struct dwarf2_cu **ref_cu)
23629 {
23630 struct die_info temp_die;
23631 struct dwarf2_cu *sig_cu;
23632 struct die_info *die;
23633 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23634
23635
23636 /* While it might be nice to assert sig_type->type == NULL here,
23637 we can get here for DW_AT_imported_declaration where we need
23638 the DIE not the type. */
23639
23640 /* If necessary, add it to the queue and load its DIEs.
23641
23642 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23643 it doesn't mean they are currently loaded. Since we require them
23644 to be loaded, we must check for ourselves. */
23645 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23646 language_minimal)
23647 || per_objfile->get_cu (sig_type) == nullptr)
23648 read_signatured_type (sig_type, per_objfile);
23649
23650 sig_cu = per_objfile->get_cu (sig_type);
23651 gdb_assert (sig_cu != NULL);
23652 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23653 temp_die.sect_off = sig_type->type_offset_in_section;
23654 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23655 to_underlying (temp_die.sect_off));
23656 if (die)
23657 {
23658 /* For .gdb_index version 7 keep track of included TUs.
23659 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23660 if (per_objfile->per_bfd->index_table != NULL
23661 && per_objfile->per_bfd->index_table->version <= 7)
23662 {
23663 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23664 }
23665
23666 *ref_cu = sig_cu;
23667 return die;
23668 }
23669
23670 return NULL;
23671 }
23672
23673 /* Follow signatured type referenced by ATTR in SRC_DIE.
23674 On entry *REF_CU is the CU of SRC_DIE.
23675 On exit *REF_CU is the CU of the result.
23676 The result is the DIE of the type.
23677 If the referenced type cannot be found an error is thrown. */
23678
23679 static struct die_info *
23680 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23681 struct dwarf2_cu **ref_cu)
23682 {
23683 ULONGEST signature = attr->as_signature ();
23684 struct signatured_type *sig_type;
23685 struct die_info *die;
23686
23687 gdb_assert (attr->form == DW_FORM_ref_sig8);
23688
23689 sig_type = lookup_signatured_type (*ref_cu, signature);
23690 /* sig_type will be NULL if the signatured type is missing from
23691 the debug info. */
23692 if (sig_type == NULL)
23693 {
23694 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23695 " from DIE at %s [in module %s]"),
23696 hex_string (signature), sect_offset_str (src_die->sect_off),
23697 objfile_name ((*ref_cu)->per_objfile->objfile));
23698 }
23699
23700 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23701 if (die == NULL)
23702 {
23703 dump_die_for_error (src_die);
23704 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23705 " from DIE at %s [in module %s]"),
23706 hex_string (signature), sect_offset_str (src_die->sect_off),
23707 objfile_name ((*ref_cu)->per_objfile->objfile));
23708 }
23709
23710 return die;
23711 }
23712
23713 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23714 reading in and processing the type unit if necessary. */
23715
23716 static struct type *
23717 get_signatured_type (struct die_info *die, ULONGEST signature,
23718 struct dwarf2_cu *cu)
23719 {
23720 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23721 struct signatured_type *sig_type;
23722 struct dwarf2_cu *type_cu;
23723 struct die_info *type_die;
23724 struct type *type;
23725
23726 sig_type = lookup_signatured_type (cu, signature);
23727 /* sig_type will be NULL if the signatured type is missing from
23728 the debug info. */
23729 if (sig_type == NULL)
23730 {
23731 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23732 " from DIE at %s [in module %s]"),
23733 hex_string (signature), sect_offset_str (die->sect_off),
23734 objfile_name (per_objfile->objfile));
23735 return build_error_marker_type (cu, die);
23736 }
23737
23738 /* If we already know the type we're done. */
23739 type = per_objfile->get_type_for_signatured_type (sig_type);
23740 if (type != nullptr)
23741 return type;
23742
23743 type_cu = cu;
23744 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23745 if (type_die != NULL)
23746 {
23747 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23748 is created. This is important, for example, because for c++ classes
23749 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23750 type = read_type_die (type_die, type_cu);
23751 if (type == NULL)
23752 {
23753 complaint (_("Dwarf Error: Cannot build signatured type %s"
23754 " referenced from DIE at %s [in module %s]"),
23755 hex_string (signature), sect_offset_str (die->sect_off),
23756 objfile_name (per_objfile->objfile));
23757 type = build_error_marker_type (cu, die);
23758 }
23759 }
23760 else
23761 {
23762 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23763 " from DIE at %s [in module %s]"),
23764 hex_string (signature), sect_offset_str (die->sect_off),
23765 objfile_name (per_objfile->objfile));
23766 type = build_error_marker_type (cu, die);
23767 }
23768
23769 per_objfile->set_type_for_signatured_type (sig_type, type);
23770
23771 return type;
23772 }
23773
23774 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23775 reading in and processing the type unit if necessary. */
23776
23777 static struct type *
23778 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23779 struct dwarf2_cu *cu) /* ARI: editCase function */
23780 {
23781 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23782 if (attr->form_is_ref ())
23783 {
23784 struct dwarf2_cu *type_cu = cu;
23785 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23786
23787 return read_type_die (type_die, type_cu);
23788 }
23789 else if (attr->form == DW_FORM_ref_sig8)
23790 {
23791 return get_signatured_type (die, attr->as_signature (), cu);
23792 }
23793 else
23794 {
23795 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23796
23797 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23798 " at %s [in module %s]"),
23799 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23800 objfile_name (per_objfile->objfile));
23801 return build_error_marker_type (cu, die);
23802 }
23803 }
23804
23805 /* Load the DIEs associated with type unit PER_CU into memory. */
23806
23807 static void
23808 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23809 dwarf2_per_objfile *per_objfile)
23810 {
23811 struct signatured_type *sig_type;
23812
23813 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23814 gdb_assert (! per_cu->type_unit_group_p ());
23815
23816 /* We have the per_cu, but we need the signatured_type.
23817 Fortunately this is an easy translation. */
23818 gdb_assert (per_cu->is_debug_types);
23819 sig_type = (struct signatured_type *) per_cu;
23820
23821 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23822
23823 read_signatured_type (sig_type, per_objfile);
23824
23825 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23826 }
23827
23828 /* Read in a signatured type and build its CU and DIEs.
23829 If the type is a stub for the real type in a DWO file,
23830 read in the real type from the DWO file as well. */
23831
23832 static void
23833 read_signatured_type (signatured_type *sig_type,
23834 dwarf2_per_objfile *per_objfile)
23835 {
23836 gdb_assert (sig_type->is_debug_types);
23837 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23838
23839 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23840
23841 if (!reader.dummy_p)
23842 {
23843 struct dwarf2_cu *cu = reader.cu;
23844 const gdb_byte *info_ptr = reader.info_ptr;
23845
23846 gdb_assert (cu->die_hash == NULL);
23847 cu->die_hash =
23848 htab_create_alloc_ex (cu->header.length / 12,
23849 die_hash,
23850 die_eq,
23851 NULL,
23852 &cu->comp_unit_obstack,
23853 hashtab_obstack_allocate,
23854 dummy_obstack_deallocate);
23855
23856 if (reader.comp_unit_die->has_children)
23857 reader.comp_unit_die->child
23858 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23859 reader.comp_unit_die);
23860 cu->dies = reader.comp_unit_die;
23861 /* comp_unit_die is not stored in die_hash, no need. */
23862
23863 /* We try not to read any attributes in this function, because
23864 not all CUs needed for references have been loaded yet, and
23865 symbol table processing isn't initialized. But we have to
23866 set the CU language, or we won't be able to build types
23867 correctly. Similarly, if we do not read the producer, we can
23868 not apply producer-specific interpretation. */
23869 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23870
23871 reader.keep ();
23872 }
23873
23874 sig_type->tu_read = 1;
23875 }
23876
23877 /* Decode simple location descriptions.
23878 Given a pointer to a dwarf block that defines a location, compute
23879 the location and return the value. If COMPUTED is non-null, it is
23880 set to true to indicate that decoding was successful, and false
23881 otherwise. If COMPUTED is null, then this function may emit a
23882 complaint. */
23883
23884 static CORE_ADDR
23885 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23886 {
23887 struct objfile *objfile = cu->per_objfile->objfile;
23888 size_t i;
23889 size_t size = blk->size;
23890 const gdb_byte *data = blk->data;
23891 CORE_ADDR stack[64];
23892 int stacki;
23893 unsigned int bytes_read, unsnd;
23894 gdb_byte op;
23895
23896 if (computed != nullptr)
23897 *computed = false;
23898
23899 i = 0;
23900 stacki = 0;
23901 stack[stacki] = 0;
23902 stack[++stacki] = 0;
23903
23904 while (i < size)
23905 {
23906 op = data[i++];
23907 switch (op)
23908 {
23909 case DW_OP_lit0:
23910 case DW_OP_lit1:
23911 case DW_OP_lit2:
23912 case DW_OP_lit3:
23913 case DW_OP_lit4:
23914 case DW_OP_lit5:
23915 case DW_OP_lit6:
23916 case DW_OP_lit7:
23917 case DW_OP_lit8:
23918 case DW_OP_lit9:
23919 case DW_OP_lit10:
23920 case DW_OP_lit11:
23921 case DW_OP_lit12:
23922 case DW_OP_lit13:
23923 case DW_OP_lit14:
23924 case DW_OP_lit15:
23925 case DW_OP_lit16:
23926 case DW_OP_lit17:
23927 case DW_OP_lit18:
23928 case DW_OP_lit19:
23929 case DW_OP_lit20:
23930 case DW_OP_lit21:
23931 case DW_OP_lit22:
23932 case DW_OP_lit23:
23933 case DW_OP_lit24:
23934 case DW_OP_lit25:
23935 case DW_OP_lit26:
23936 case DW_OP_lit27:
23937 case DW_OP_lit28:
23938 case DW_OP_lit29:
23939 case DW_OP_lit30:
23940 case DW_OP_lit31:
23941 stack[++stacki] = op - DW_OP_lit0;
23942 break;
23943
23944 case DW_OP_reg0:
23945 case DW_OP_reg1:
23946 case DW_OP_reg2:
23947 case DW_OP_reg3:
23948 case DW_OP_reg4:
23949 case DW_OP_reg5:
23950 case DW_OP_reg6:
23951 case DW_OP_reg7:
23952 case DW_OP_reg8:
23953 case DW_OP_reg9:
23954 case DW_OP_reg10:
23955 case DW_OP_reg11:
23956 case DW_OP_reg12:
23957 case DW_OP_reg13:
23958 case DW_OP_reg14:
23959 case DW_OP_reg15:
23960 case DW_OP_reg16:
23961 case DW_OP_reg17:
23962 case DW_OP_reg18:
23963 case DW_OP_reg19:
23964 case DW_OP_reg20:
23965 case DW_OP_reg21:
23966 case DW_OP_reg22:
23967 case DW_OP_reg23:
23968 case DW_OP_reg24:
23969 case DW_OP_reg25:
23970 case DW_OP_reg26:
23971 case DW_OP_reg27:
23972 case DW_OP_reg28:
23973 case DW_OP_reg29:
23974 case DW_OP_reg30:
23975 case DW_OP_reg31:
23976 stack[++stacki] = op - DW_OP_reg0;
23977 if (i < size)
23978 {
23979 if (computed == nullptr)
23980 dwarf2_complex_location_expr_complaint ();
23981 else
23982 return 0;
23983 }
23984 break;
23985
23986 case DW_OP_regx:
23987 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23988 i += bytes_read;
23989 stack[++stacki] = unsnd;
23990 if (i < size)
23991 {
23992 if (computed == nullptr)
23993 dwarf2_complex_location_expr_complaint ();
23994 else
23995 return 0;
23996 }
23997 break;
23998
23999 case DW_OP_addr:
24000 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24001 &bytes_read);
24002 i += bytes_read;
24003 break;
24004
24005 case DW_OP_const1u:
24006 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24007 i += 1;
24008 break;
24009
24010 case DW_OP_const1s:
24011 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24012 i += 1;
24013 break;
24014
24015 case DW_OP_const2u:
24016 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24017 i += 2;
24018 break;
24019
24020 case DW_OP_const2s:
24021 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24022 i += 2;
24023 break;
24024
24025 case DW_OP_const4u:
24026 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24027 i += 4;
24028 break;
24029
24030 case DW_OP_const4s:
24031 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24032 i += 4;
24033 break;
24034
24035 case DW_OP_const8u:
24036 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24037 i += 8;
24038 break;
24039
24040 case DW_OP_constu:
24041 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24042 &bytes_read);
24043 i += bytes_read;
24044 break;
24045
24046 case DW_OP_consts:
24047 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24048 i += bytes_read;
24049 break;
24050
24051 case DW_OP_dup:
24052 stack[stacki + 1] = stack[stacki];
24053 stacki++;
24054 break;
24055
24056 case DW_OP_plus:
24057 stack[stacki - 1] += stack[stacki];
24058 stacki--;
24059 break;
24060
24061 case DW_OP_plus_uconst:
24062 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24063 &bytes_read);
24064 i += bytes_read;
24065 break;
24066
24067 case DW_OP_minus:
24068 stack[stacki - 1] -= stack[stacki];
24069 stacki--;
24070 break;
24071
24072 case DW_OP_deref:
24073 /* If we're not the last op, then we definitely can't encode
24074 this using GDB's address_class enum. This is valid for partial
24075 global symbols, although the variable's address will be bogus
24076 in the psymtab. */
24077 if (i < size)
24078 {
24079 if (computed == nullptr)
24080 dwarf2_complex_location_expr_complaint ();
24081 else
24082 return 0;
24083 }
24084 break;
24085
24086 case DW_OP_GNU_push_tls_address:
24087 case DW_OP_form_tls_address:
24088 /* The top of the stack has the offset from the beginning
24089 of the thread control block at which the variable is located. */
24090 /* Nothing should follow this operator, so the top of stack would
24091 be returned. */
24092 /* This is valid for partial global symbols, but the variable's
24093 address will be bogus in the psymtab. Make it always at least
24094 non-zero to not look as a variable garbage collected by linker
24095 which have DW_OP_addr 0. */
24096 if (i < size)
24097 {
24098 if (computed == nullptr)
24099 dwarf2_complex_location_expr_complaint ();
24100 else
24101 return 0;
24102 }
24103 stack[stacki]++;
24104 break;
24105
24106 case DW_OP_GNU_uninit:
24107 if (computed != nullptr)
24108 return 0;
24109 break;
24110
24111 case DW_OP_addrx:
24112 case DW_OP_GNU_addr_index:
24113 case DW_OP_GNU_const_index:
24114 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24115 &bytes_read);
24116 i += bytes_read;
24117 break;
24118
24119 default:
24120 if (computed == nullptr)
24121 {
24122 const char *name = get_DW_OP_name (op);
24123
24124 if (name)
24125 complaint (_("unsupported stack op: '%s'"),
24126 name);
24127 else
24128 complaint (_("unsupported stack op: '%02x'"),
24129 op);
24130 }
24131
24132 return (stack[stacki]);
24133 }
24134
24135 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24136 outside of the allocated space. Also enforce minimum>0. */
24137 if (stacki >= ARRAY_SIZE (stack) - 1)
24138 {
24139 if (computed == nullptr)
24140 complaint (_("location description stack overflow"));
24141 return 0;
24142 }
24143
24144 if (stacki <= 0)
24145 {
24146 if (computed == nullptr)
24147 complaint (_("location description stack underflow"));
24148 return 0;
24149 }
24150 }
24151
24152 if (computed != nullptr)
24153 *computed = true;
24154 return (stack[stacki]);
24155 }
24156
24157 /* memory allocation interface */
24158
24159 static struct dwarf_block *
24160 dwarf_alloc_block (struct dwarf2_cu *cu)
24161 {
24162 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24163 }
24164
24165 static struct die_info *
24166 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24167 {
24168 struct die_info *die;
24169 size_t size = sizeof (struct die_info);
24170
24171 if (num_attrs > 1)
24172 size += (num_attrs - 1) * sizeof (struct attribute);
24173
24174 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24175 memset (die, 0, sizeof (struct die_info));
24176 return (die);
24177 }
24178
24179 \f
24180
24181 /* Macro support. */
24182
24183 /* An overload of dwarf_decode_macros that finds the correct section
24184 and ensures it is read in before calling the other overload. */
24185
24186 static void
24187 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24188 int section_is_gnu)
24189 {
24190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24191 struct objfile *objfile = per_objfile->objfile;
24192 const struct line_header *lh = cu->line_header;
24193 unsigned int offset_size = cu->header.offset_size;
24194 struct dwarf2_section_info *section;
24195 const char *section_name;
24196
24197 if (cu->dwo_unit != nullptr)
24198 {
24199 if (section_is_gnu)
24200 {
24201 section = &cu->dwo_unit->dwo_file->sections.macro;
24202 section_name = ".debug_macro.dwo";
24203 }
24204 else
24205 {
24206 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24207 section_name = ".debug_macinfo.dwo";
24208 }
24209 }
24210 else
24211 {
24212 if (section_is_gnu)
24213 {
24214 section = &per_objfile->per_bfd->macro;
24215 section_name = ".debug_macro";
24216 }
24217 else
24218 {
24219 section = &per_objfile->per_bfd->macinfo;
24220 section_name = ".debug_macinfo";
24221 }
24222 }
24223
24224 section->read (objfile);
24225 if (section->buffer == nullptr)
24226 {
24227 complaint (_("missing %s section"), section_name);
24228 return;
24229 }
24230
24231 buildsym_compunit *builder = cu->get_builder ();
24232
24233 struct dwarf2_section_info *str_offsets_section;
24234 struct dwarf2_section_info *str_section;
24235 gdb::optional<ULONGEST> str_offsets_base;
24236
24237 if (cu->dwo_unit != nullptr)
24238 {
24239 str_offsets_section = &cu->dwo_unit->dwo_file
24240 ->sections.str_offsets;
24241 str_section = &cu->dwo_unit->dwo_file->sections.str;
24242 str_offsets_base = cu->header.addr_size;
24243 }
24244 else
24245 {
24246 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24247 str_section = &per_objfile->per_bfd->str;
24248 str_offsets_base = cu->str_offsets_base;
24249 }
24250
24251 dwarf_decode_macros (per_objfile, builder, section, lh,
24252 offset_size, offset, str_section, str_offsets_section,
24253 str_offsets_base, section_is_gnu);
24254 }
24255
24256 /* Return the .debug_loc section to use for CU.
24257 For DWO files use .debug_loc.dwo. */
24258
24259 static struct dwarf2_section_info *
24260 cu_debug_loc_section (struct dwarf2_cu *cu)
24261 {
24262 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24263
24264 if (cu->dwo_unit)
24265 {
24266 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24267
24268 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24269 }
24270 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24271 : &per_objfile->per_bfd->loc);
24272 }
24273
24274 /* Return the .debug_rnglists section to use for CU. */
24275 static struct dwarf2_section_info *
24276 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24277 {
24278 if (cu->header.version < 5)
24279 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24280 cu->header.version);
24281 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24282
24283 /* Make sure we read the .debug_rnglists section from the file that
24284 contains the DW_AT_ranges attribute we are reading. Normally that
24285 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24286 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24287 program. */
24288 if (cu->dwo_unit != nullptr
24289 && tag != DW_TAG_compile_unit
24290 && tag != DW_TAG_skeleton_unit)
24291 {
24292 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24293
24294 if (sections->rnglists.size > 0)
24295 return &sections->rnglists;
24296 else
24297 error (_(".debug_rnglists section is missing from .dwo file."));
24298 }
24299 return &dwarf2_per_objfile->per_bfd->rnglists;
24300 }
24301
24302 /* A helper function that fills in a dwarf2_loclist_baton. */
24303
24304 static void
24305 fill_in_loclist_baton (struct dwarf2_cu *cu,
24306 struct dwarf2_loclist_baton *baton,
24307 const struct attribute *attr)
24308 {
24309 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24310 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24311
24312 section->read (per_objfile->objfile);
24313
24314 baton->per_objfile = per_objfile;
24315 baton->per_cu = cu->per_cu;
24316 gdb_assert (baton->per_cu);
24317 /* We don't know how long the location list is, but make sure we
24318 don't run off the edge of the section. */
24319 baton->size = section->size - attr->as_unsigned ();
24320 baton->data = section->buffer + attr->as_unsigned ();
24321 if (cu->base_address.has_value ())
24322 baton->base_address = *cu->base_address;
24323 else
24324 baton->base_address = 0;
24325 baton->from_dwo = cu->dwo_unit != NULL;
24326 }
24327
24328 static void
24329 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24330 struct dwarf2_cu *cu, int is_block)
24331 {
24332 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24333 struct objfile *objfile = per_objfile->objfile;
24334 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24335
24336 if (attr->form_is_section_offset ()
24337 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24338 the section. If so, fall through to the complaint in the
24339 other branch. */
24340 && attr->as_unsigned () < section->get_size (objfile))
24341 {
24342 struct dwarf2_loclist_baton *baton;
24343
24344 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24345
24346 fill_in_loclist_baton (cu, baton, attr);
24347
24348 if (!cu->base_address.has_value ())
24349 complaint (_("Location list used without "
24350 "specifying the CU base address."));
24351
24352 SYMBOL_ACLASS_INDEX (sym) = (is_block
24353 ? dwarf2_loclist_block_index
24354 : dwarf2_loclist_index);
24355 SYMBOL_LOCATION_BATON (sym) = baton;
24356 }
24357 else
24358 {
24359 struct dwarf2_locexpr_baton *baton;
24360
24361 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24362 baton->per_objfile = per_objfile;
24363 baton->per_cu = cu->per_cu;
24364 gdb_assert (baton->per_cu);
24365
24366 if (attr->form_is_block ())
24367 {
24368 /* Note that we're just copying the block's data pointer
24369 here, not the actual data. We're still pointing into the
24370 info_buffer for SYM's objfile; right now we never release
24371 that buffer, but when we do clean up properly this may
24372 need to change. */
24373 struct dwarf_block *block = attr->as_block ();
24374 baton->size = block->size;
24375 baton->data = block->data;
24376 }
24377 else
24378 {
24379 dwarf2_invalid_attrib_class_complaint ("location description",
24380 sym->natural_name ());
24381 baton->size = 0;
24382 }
24383
24384 SYMBOL_ACLASS_INDEX (sym) = (is_block
24385 ? dwarf2_locexpr_block_index
24386 : dwarf2_locexpr_index);
24387 SYMBOL_LOCATION_BATON (sym) = baton;
24388 }
24389 }
24390
24391 /* See read.h. */
24392
24393 const comp_unit_head *
24394 dwarf2_per_cu_data::get_header () const
24395 {
24396 if (!m_header_read_in)
24397 {
24398 const gdb_byte *info_ptr
24399 = this->section->buffer + to_underlying (this->sect_off);
24400
24401 memset (&m_header, 0, sizeof (m_header));
24402
24403 read_comp_unit_head (&m_header, info_ptr, this->section,
24404 rcuh_kind::COMPILE);
24405
24406 m_header_read_in = true;
24407 }
24408
24409 return &m_header;
24410 }
24411
24412 /* See read.h. */
24413
24414 int
24415 dwarf2_per_cu_data::addr_size () const
24416 {
24417 return this->get_header ()->addr_size;
24418 }
24419
24420 /* See read.h. */
24421
24422 int
24423 dwarf2_per_cu_data::offset_size () const
24424 {
24425 return this->get_header ()->offset_size;
24426 }
24427
24428 /* See read.h. */
24429
24430 int
24431 dwarf2_per_cu_data::ref_addr_size () const
24432 {
24433 const comp_unit_head *header = this->get_header ();
24434
24435 if (header->version == 2)
24436 return header->addr_size;
24437 else
24438 return header->offset_size;
24439 }
24440
24441 /* A helper function for dwarf2_find_containing_comp_unit that returns
24442 the index of the result, and that searches a vector. It will
24443 return a result even if the offset in question does not actually
24444 occur in any CU. This is separate so that it can be unit
24445 tested. */
24446
24447 static int
24448 dwarf2_find_containing_comp_unit
24449 (sect_offset sect_off,
24450 unsigned int offset_in_dwz,
24451 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24452 {
24453 int low, high;
24454
24455 low = 0;
24456 high = all_comp_units.size () - 1;
24457 while (high > low)
24458 {
24459 struct dwarf2_per_cu_data *mid_cu;
24460 int mid = low + (high - low) / 2;
24461
24462 mid_cu = all_comp_units[mid].get ();
24463 if (mid_cu->is_dwz > offset_in_dwz
24464 || (mid_cu->is_dwz == offset_in_dwz
24465 && mid_cu->sect_off + mid_cu->length > sect_off))
24466 high = mid;
24467 else
24468 low = mid + 1;
24469 }
24470 gdb_assert (low == high);
24471 return low;
24472 }
24473
24474 /* Locate the .debug_info compilation unit from CU's objfile which contains
24475 the DIE at OFFSET. Raises an error on failure. */
24476
24477 static struct dwarf2_per_cu_data *
24478 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24479 unsigned int offset_in_dwz,
24480 dwarf2_per_objfile *per_objfile)
24481 {
24482 int low = dwarf2_find_containing_comp_unit
24483 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24484 dwarf2_per_cu_data *this_cu
24485 = per_objfile->per_bfd->all_comp_units[low].get ();
24486
24487 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24488 {
24489 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24490 error (_("Dwarf Error: could not find partial DIE containing "
24491 "offset %s [in module %s]"),
24492 sect_offset_str (sect_off),
24493 bfd_get_filename (per_objfile->objfile->obfd));
24494
24495 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24496 <= sect_off);
24497 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
24498 }
24499 else
24500 {
24501 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24502 && sect_off >= this_cu->sect_off + this_cu->length)
24503 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24504 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24505 return this_cu;
24506 }
24507 }
24508
24509 #if GDB_SELF_TEST
24510
24511 namespace selftests {
24512 namespace find_containing_comp_unit {
24513
24514 static void
24515 run_test ()
24516 {
24517 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24518 dwarf2_per_cu_data *one_ptr = one.get ();
24519 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24520 dwarf2_per_cu_data *two_ptr = two.get ();
24521 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24522 dwarf2_per_cu_data *three_ptr = three.get ();
24523 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24524 dwarf2_per_cu_data *four_ptr = four.get ();
24525
24526 one->length = 5;
24527 two->sect_off = sect_offset (one->length);
24528 two->length = 7;
24529
24530 three->length = 5;
24531 three->is_dwz = 1;
24532 four->sect_off = sect_offset (three->length);
24533 four->length = 7;
24534 four->is_dwz = 1;
24535
24536 std::vector<dwarf2_per_cu_data_up> units;
24537 units.push_back (std::move (one));
24538 units.push_back (std::move (two));
24539 units.push_back (std::move (three));
24540 units.push_back (std::move (four));
24541
24542 int result;
24543
24544 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24545 SELF_CHECK (units[result].get () == one_ptr);
24546 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24547 SELF_CHECK (units[result].get () == one_ptr);
24548 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24549 SELF_CHECK (units[result].get () == two_ptr);
24550
24551 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24552 SELF_CHECK (units[result].get () == three_ptr);
24553 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24554 SELF_CHECK (units[result].get () == three_ptr);
24555 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24556 SELF_CHECK (units[result].get () == four_ptr);
24557 }
24558
24559 }
24560 }
24561
24562 #endif /* GDB_SELF_TEST */
24563
24564 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24565
24566 static void
24567 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24568 enum language pretend_language)
24569 {
24570 struct attribute *attr;
24571
24572 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24573
24574 /* Set the language we're debugging. */
24575 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24576 if (cu->producer != nullptr
24577 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
24578 {
24579 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24580 attribute is not standardised yet. As a workaround for the
24581 language detection we fall back to the DW_AT_producer
24582 string. */
24583 cu->per_cu->lang = language_opencl;
24584 }
24585 else if (cu->producer != nullptr
24586 && strstr (cu->producer, "GNU Go ") != NULL)
24587 {
24588 /* Similar hack for Go. */
24589 cu->per_cu->lang = language_go;
24590 }
24591 else if (attr != nullptr)
24592 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
24593 else
24594 cu->per_cu->lang = pretend_language;
24595 cu->language_defn = language_def (cu->per_cu->lang);
24596 }
24597
24598 /* See read.h. */
24599
24600 dwarf2_cu *
24601 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24602 {
24603 auto it = m_dwarf2_cus.find (per_cu);
24604 if (it == m_dwarf2_cus.end ())
24605 return nullptr;
24606
24607 return it->second;
24608 }
24609
24610 /* See read.h. */
24611
24612 void
24613 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24614 {
24615 gdb_assert (this->get_cu (per_cu) == nullptr);
24616
24617 m_dwarf2_cus[per_cu] = cu;
24618 }
24619
24620 /* See read.h. */
24621
24622 void
24623 dwarf2_per_objfile::age_comp_units ()
24624 {
24625 dwarf_read_debug_printf_v ("running");
24626
24627 /* This is not expected to be called in the middle of CU expansion. There is
24628 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24629 loaded in memory. Calling age_comp_units while the queue is in use could
24630 make us free the DIEs for a CU that is in the queue and therefore break
24631 that invariant. */
24632 gdb_assert (!this->per_bfd->queue.has_value ());
24633
24634 /* Start by clearing all marks. */
24635 for (auto pair : m_dwarf2_cus)
24636 pair.second->clear_mark ();
24637
24638 /* Traverse all CUs, mark them and their dependencies if used recently
24639 enough. */
24640 for (auto pair : m_dwarf2_cus)
24641 {
24642 dwarf2_cu *cu = pair.second;
24643
24644 cu->last_used++;
24645 if (cu->last_used <= dwarf_max_cache_age)
24646 cu->mark ();
24647 }
24648
24649 /* Delete all CUs still not marked. */
24650 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24651 {
24652 dwarf2_cu *cu = it->second;
24653
24654 if (!cu->is_marked ())
24655 {
24656 dwarf_read_debug_printf_v ("deleting old CU %s",
24657 sect_offset_str (cu->per_cu->sect_off));
24658 delete cu;
24659 it = m_dwarf2_cus.erase (it);
24660 }
24661 else
24662 it++;
24663 }
24664 }
24665
24666 /* See read.h. */
24667
24668 void
24669 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24670 {
24671 auto it = m_dwarf2_cus.find (per_cu);
24672 if (it == m_dwarf2_cus.end ())
24673 return;
24674
24675 delete it->second;
24676
24677 m_dwarf2_cus.erase (it);
24678 }
24679
24680 dwarf2_per_objfile::~dwarf2_per_objfile ()
24681 {
24682 remove_all_cus ();
24683 }
24684
24685 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24686 We store these in a hash table separate from the DIEs, and preserve them
24687 when the DIEs are flushed out of cache.
24688
24689 The CU "per_cu" pointer is needed because offset alone is not enough to
24690 uniquely identify the type. A file may have multiple .debug_types sections,
24691 or the type may come from a DWO file. Furthermore, while it's more logical
24692 to use per_cu->section+offset, with Fission the section with the data is in
24693 the DWO file but we don't know that section at the point we need it.
24694 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24695 because we can enter the lookup routine, get_die_type_at_offset, from
24696 outside this file, and thus won't necessarily have PER_CU->cu.
24697 Fortunately, PER_CU is stable for the life of the objfile. */
24698
24699 struct dwarf2_per_cu_offset_and_type
24700 {
24701 const struct dwarf2_per_cu_data *per_cu;
24702 sect_offset sect_off;
24703 struct type *type;
24704 };
24705
24706 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24707
24708 static hashval_t
24709 per_cu_offset_and_type_hash (const void *item)
24710 {
24711 const struct dwarf2_per_cu_offset_and_type *ofs
24712 = (const struct dwarf2_per_cu_offset_and_type *) item;
24713
24714 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24715 }
24716
24717 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24718
24719 static int
24720 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24721 {
24722 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24723 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24724 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24725 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24726
24727 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24728 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24729 }
24730
24731 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24732 table if necessary. For convenience, return TYPE.
24733
24734 The DIEs reading must have careful ordering to:
24735 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24736 reading current DIE.
24737 * Not trying to dereference contents of still incompletely read in types
24738 while reading in other DIEs.
24739 * Enable referencing still incompletely read in types just by a pointer to
24740 the type without accessing its fields.
24741
24742 Therefore caller should follow these rules:
24743 * Try to fetch any prerequisite types we may need to build this DIE type
24744 before building the type and calling set_die_type.
24745 * After building type call set_die_type for current DIE as soon as
24746 possible before fetching more types to complete the current type.
24747 * Make the type as complete as possible before fetching more types. */
24748
24749 static struct type *
24750 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24751 bool skip_data_location)
24752 {
24753 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24754 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24755 struct objfile *objfile = per_objfile->objfile;
24756 struct attribute *attr;
24757 struct dynamic_prop prop;
24758
24759 /* For Ada types, make sure that the gnat-specific data is always
24760 initialized (if not already set). There are a few types where
24761 we should not be doing so, because the type-specific area is
24762 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24763 where the type-specific area is used to store the floatformat).
24764 But this is not a problem, because the gnat-specific information
24765 is actually not needed for these types. */
24766 if (need_gnat_info (cu)
24767 && type->code () != TYPE_CODE_FUNC
24768 && type->code () != TYPE_CODE_FLT
24769 && type->code () != TYPE_CODE_METHODPTR
24770 && type->code () != TYPE_CODE_MEMBERPTR
24771 && type->code () != TYPE_CODE_METHOD
24772 && type->code () != TYPE_CODE_FIXED_POINT
24773 && !HAVE_GNAT_AUX_INFO (type))
24774 INIT_GNAT_SPECIFIC (type);
24775
24776 /* Read DW_AT_allocated and set in type. */
24777 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24778 if (attr != NULL)
24779 {
24780 struct type *prop_type = cu->addr_sized_int_type (false);
24781 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24782 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24783 }
24784
24785 /* Read DW_AT_associated and set in type. */
24786 attr = dwarf2_attr (die, DW_AT_associated, cu);
24787 if (attr != NULL)
24788 {
24789 struct type *prop_type = cu->addr_sized_int_type (false);
24790 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24791 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24792 }
24793
24794 /* Read DW_AT_data_location and set in type. */
24795 if (!skip_data_location)
24796 {
24797 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24798 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24799 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24800 }
24801
24802 if (per_objfile->die_type_hash == NULL)
24803 per_objfile->die_type_hash
24804 = htab_up (htab_create_alloc (127,
24805 per_cu_offset_and_type_hash,
24806 per_cu_offset_and_type_eq,
24807 NULL, xcalloc, xfree));
24808
24809 ofs.per_cu = cu->per_cu;
24810 ofs.sect_off = die->sect_off;
24811 ofs.type = type;
24812 slot = (struct dwarf2_per_cu_offset_and_type **)
24813 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24814 if (*slot)
24815 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24816 sect_offset_str (die->sect_off));
24817 *slot = XOBNEW (&objfile->objfile_obstack,
24818 struct dwarf2_per_cu_offset_and_type);
24819 **slot = ofs;
24820 return type;
24821 }
24822
24823 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24824 or return NULL if the die does not have a saved type. */
24825
24826 static struct type *
24827 get_die_type_at_offset (sect_offset sect_off,
24828 dwarf2_per_cu_data *per_cu,
24829 dwarf2_per_objfile *per_objfile)
24830 {
24831 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24832
24833 if (per_objfile->die_type_hash == NULL)
24834 return NULL;
24835
24836 ofs.per_cu = per_cu;
24837 ofs.sect_off = sect_off;
24838 slot = ((struct dwarf2_per_cu_offset_and_type *)
24839 htab_find (per_objfile->die_type_hash.get (), &ofs));
24840 if (slot)
24841 return slot->type;
24842 else
24843 return NULL;
24844 }
24845
24846 /* Look up the type for DIE in CU in die_type_hash,
24847 or return NULL if DIE does not have a saved type. */
24848
24849 static struct type *
24850 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24851 {
24852 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24853 }
24854
24855 /* Trivial hash function for partial_die_info: the hash value of a DIE
24856 is its offset in .debug_info for this objfile. */
24857
24858 static hashval_t
24859 partial_die_hash (const void *item)
24860 {
24861 const struct partial_die_info *part_die
24862 = (const struct partial_die_info *) item;
24863
24864 return to_underlying (part_die->sect_off);
24865 }
24866
24867 /* Trivial comparison function for partial_die_info structures: two DIEs
24868 are equal if they have the same offset. */
24869
24870 static int
24871 partial_die_eq (const void *item_lhs, const void *item_rhs)
24872 {
24873 const struct partial_die_info *part_die_lhs
24874 = (const struct partial_die_info *) item_lhs;
24875 const struct partial_die_info *part_die_rhs
24876 = (const struct partial_die_info *) item_rhs;
24877
24878 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24879 }
24880
24881 struct cmd_list_element *set_dwarf_cmdlist;
24882 struct cmd_list_element *show_dwarf_cmdlist;
24883
24884 static void
24885 show_check_physname (struct ui_file *file, int from_tty,
24886 struct cmd_list_element *c, const char *value)
24887 {
24888 fprintf_filtered (file,
24889 _("Whether to check \"physname\" is %s.\n"),
24890 value);
24891 }
24892
24893 void _initialize_dwarf2_read ();
24894 void
24895 _initialize_dwarf2_read ()
24896 {
24897 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24898 Set DWARF specific variables.\n\
24899 Configure DWARF variables such as the cache size."),
24900 &set_dwarf_cmdlist,
24901 0/*allow-unknown*/, &maintenance_set_cmdlist);
24902
24903 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24904 Show DWARF specific variables.\n\
24905 Show DWARF variables such as the cache size."),
24906 &show_dwarf_cmdlist,
24907 0/*allow-unknown*/, &maintenance_show_cmdlist);
24908
24909 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24910 &dwarf_max_cache_age, _("\
24911 Set the upper bound on the age of cached DWARF compilation units."), _("\
24912 Show the upper bound on the age of cached DWARF compilation units."), _("\
24913 A higher limit means that cached compilation units will be stored\n\
24914 in memory longer, and more total memory will be used. Zero disables\n\
24915 caching, which can slow down startup."),
24916 NULL,
24917 show_dwarf_max_cache_age,
24918 &set_dwarf_cmdlist,
24919 &show_dwarf_cmdlist);
24920
24921 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24922 Set debugging of the DWARF reader."), _("\
24923 Show debugging of the DWARF reader."), _("\
24924 When enabled (non-zero), debugging messages are printed during DWARF\n\
24925 reading and symtab expansion. A value of 1 (one) provides basic\n\
24926 information. A value greater than 1 provides more verbose information."),
24927 NULL,
24928 NULL,
24929 &setdebuglist, &showdebuglist);
24930
24931 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24932 Set debugging of the DWARF DIE reader."), _("\
24933 Show debugging of the DWARF DIE reader."), _("\
24934 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24935 The value is the maximum depth to print."),
24936 NULL,
24937 NULL,
24938 &setdebuglist, &showdebuglist);
24939
24940 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24941 Set debugging of the dwarf line reader."), _("\
24942 Show debugging of the dwarf line reader."), _("\
24943 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24944 A value of 1 (one) provides basic information.\n\
24945 A value greater than 1 provides more verbose information."),
24946 NULL,
24947 NULL,
24948 &setdebuglist, &showdebuglist);
24949
24950 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24951 Set cross-checking of \"physname\" code against demangler."), _("\
24952 Show cross-checking of \"physname\" code against demangler."), _("\
24953 When enabled, GDB's internal \"physname\" code is checked against\n\
24954 the demangler."),
24955 NULL, show_check_physname,
24956 &setdebuglist, &showdebuglist);
24957
24958 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24959 no_class, &use_deprecated_index_sections, _("\
24960 Set whether to use deprecated gdb_index sections."), _("\
24961 Show whether to use deprecated gdb_index sections."), _("\
24962 When enabled, deprecated .gdb_index sections are used anyway.\n\
24963 Normally they are ignored either because of a missing feature or\n\
24964 performance issue.\n\
24965 Warning: This option must be enabled before gdb reads the file."),
24966 NULL,
24967 NULL,
24968 &setlist, &showlist);
24969
24970 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24971 &dwarf2_locexpr_funcs);
24972 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24973 &dwarf2_loclist_funcs);
24974
24975 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24976 &dwarf2_block_frame_base_locexpr_funcs);
24977 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24978 &dwarf2_block_frame_base_loclist_funcs);
24979
24980 #if GDB_SELF_TEST
24981 selftests::register_test ("dw2_expand_symtabs_matching",
24982 selftests::dw2_expand_symtabs_matching::run_test);
24983 selftests::register_test ("dwarf2_find_containing_comp_unit",
24984 selftests::find_containing_comp_unit::run_test);
24985 #endif
24986 }