]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Refactor build_type_psymtabs_reader
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91 #include <unordered_set>
92
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
97
98 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
99
100 #define dwarf_read_debug_printf(fmt, ...) \
101 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
102 ##__VA_ARGS__)
103
104 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
105
106 #define dwarf_read_debug_printf_v(fmt, ...) \
107 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
109
110 /* When non-zero, dump DIEs after they are read in. */
111 static unsigned int dwarf_die_debug = 0;
112
113 /* When non-zero, dump line number entries as they are read in. */
114 unsigned int dwarf_line_debug = 0;
115
116 /* When true, cross-check physname against demangler. */
117 static bool check_physname = false;
118
119 /* When true, do not reject deprecated .gdb_index sections. */
120 static bool use_deprecated_index_sections = false;
121
122 /* This is used to store the data that is always per objfile. */
123 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125 /* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
135
136 /* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138 static int dwarf2_locexpr_index;
139 static int dwarf2_loclist_index;
140 static int dwarf2_locexpr_block_index;
141 static int dwarf2_loclist_block_index;
142
143 /* Size of .debug_loclists section header for 32-bit DWARF format. */
144 #define LOCLIST_HEADER_SIZE32 12
145
146 /* Size of .debug_loclists section header for 64-bit DWARF format. */
147 #define LOCLIST_HEADER_SIZE64 20
148
149 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
150 #define RNGLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
153 #define RNGLIST_HEADER_SIZE64 20
154
155 /* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168 struct name_component
169 {
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178 };
179
180 /* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183 struct mapped_index_base
184 {
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
211 void build_name_components (dwarf2_per_objfile *per_objfile);
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
218 find_name_components_bounds (const lookup_name_info &ln_no_params,
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
221
222 /* Prevent deleting/destroying via a base class pointer. */
223 protected:
224 ~mapped_index_base() = default;
225 };
226
227 /* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231 class offset_view
232 {
233 public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262 private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265 };
266
267 /* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
269 struct mapped_index final : public mapped_index_base
270 {
271 /* Index data format version. */
272 int version = 0;
273
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
276
277 /* The symbol table, implemented as a hash table. */
278 offset_view symbol_table;
279
280 /* A pointer to the constant pool. */
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
296
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
301 }
302
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size () / 2; }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
363 };
364
365 /* See dwarf2/read.h. */
366
367 dwarf2_per_objfile *
368 get_dwarf2_per_objfile (struct objfile *objfile)
369 {
370 return dwarf2_objfile_data_key.get (objfile);
371 }
372
373 /* Default names of the debugging sections. */
374
375 /* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
378 const struct dwarf2_debug_sections dwarf2_elf_names =
379 {
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
384 { ".debug_loclists", ".zdebug_loclists" },
385 { ".debug_macinfo", ".zdebug_macinfo" },
386 { ".debug_macro", ".zdebug_macro" },
387 { ".debug_str", ".zdebug_str" },
388 { ".debug_str_offsets", ".zdebug_str_offsets" },
389 { ".debug_line_str", ".zdebug_line_str" },
390 { ".debug_ranges", ".zdebug_ranges" },
391 { ".debug_rnglists", ".zdebug_rnglists" },
392 { ".debug_types", ".zdebug_types" },
393 { ".debug_addr", ".zdebug_addr" },
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
396 { ".gdb_index", ".zgdb_index" },
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
399 23
400 };
401
402 /* List of DWO/DWP sections. */
403
404 static const struct dwop_section_names
405 {
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
410 struct dwarf2_section_names loclists_dwo;
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
413 struct dwarf2_section_names rnglists_dwo;
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
419 }
420 dwop_section_names =
421 {
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
435 };
436
437 /* local data types */
438
439 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441 struct loclists_rnglists_header
442 {
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461 };
462
463 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466 struct stmt_list_hash
467 {
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
472 sect_offset line_sect_off;
473 };
474
475 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
479
480 struct type_unit_group : public dwarf2_per_cu_data
481 {
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
485 std::vector<signatured_type *> *tus = nullptr;
486
487 /* The data used to construct the hash key. */
488 struct stmt_list_hash hash {};
489 };
490
491 /* These sections are what may appear in a (real or virtual) DWO file. */
492
493 struct dwo_sections
494 {
495 struct dwarf2_section_info abbrev;
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
498 struct dwarf2_section_info loclists;
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
501 struct dwarf2_section_info rnglists;
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
506 std::vector<dwarf2_section_info> types;
507 };
508
509 /* CUs/TUs in DWP/DWO files. */
510
511 struct dwo_unit
512 {
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
522 struct dwarf2_section_info *section;
523
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530 };
531
532 /* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536 enum dwp_v2_section_ids
537 {
538 DW_SECT_MIN = 1
539 };
540
541 /* Data for one DWO file.
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
551
552 struct dwo_file
553 {
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
561 const char *dwo_name = nullptr;
562
563 /* The DW_AT_comp_dir attribute. */
564 const char *comp_dir = nullptr;
565
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
568 gdb_bfd_ref_ptr dbfd;
569
570 /* The sections that make up this DWO file.
571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
572 sections (for lack of a better name). */
573 struct dwo_sections sections {};
574
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
579 htab_up cus;
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
583 htab_up tus;
584 };
585
586 /* These sections are what may appear in a DWP file. */
587
588 struct dwp_sections
589 {
590 /* These are used by all DWP versions (1, 2 and 5). */
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
594
595 /* These are only used by DWP version 2 and version 5 files.
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
607 struct dwarf2_section_info loclists;
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
610 struct dwarf2_section_info rnglists;
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
613 };
614
615 /* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
617
618 struct virtual_v1_dwo_sections
619 {
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
627 That is recorded here, and copied to dwo_unit.section. */
628 struct dwarf2_section_info info_or_types;
629 };
630
631 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
636 struct virtual_v2_or_v5_dwo_sections
637 {
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666 };
667
668 /* Contents of DWP hash tables. */
669
670 struct dwp_hash_table
671 {
672 uint32_t version, nr_columns;
673 uint32_t nr_units, nr_slots;
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685 #define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700 #define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
712 } section_pool;
713 };
714
715 /* Data for one DWP file. */
716
717 struct dwp_file
718 {
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
725 /* Name of the file. */
726 const char *name;
727
728 /* File format version. */
729 int version = 0;
730
731 /* The bfd. */
732 gdb_bfd_ref_ptr dbfd;
733
734 /* Section info for this file. */
735 struct dwp_sections sections {};
736
737 /* Table of CUs in the file. */
738 const struct dwp_hash_table *cus = nullptr;
739
740 /* Table of TUs in the file. */
741 const struct dwp_hash_table *tus = nullptr;
742
743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
744 htab_up loaded_cus;
745 htab_up loaded_tus;
746
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
751 };
752
753 /* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
756 struct exists to abstract away the constant parameters of die reading. */
757
758 struct die_reader_specs
759 {
760 /* The bfd of die_section. */
761 bfd *abfd;
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
767 struct dwo_file *dwo_file;
768
769 /* The section the die comes from.
770 This is either .debug_info or .debug_types, or the .dwo variants. */
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
774 const gdb_byte *buffer;
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
778
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
781 };
782
783 /* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786 class cutu_reader : public die_reader_specs
787 {
788 public:
789
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
792 struct abbrev_table *abbrev_table,
793 dwarf2_cu *existing_cu,
794 bool skip_partial);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
797 dwarf2_per_objfile *per_objfile,
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
805 bool dummy_p = false;
806
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
811 private:
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
814 dwarf2_cu *existing_cu);
815
816 struct dwarf2_per_cu_data *m_this_cu;
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824 };
825
826 /* When we construct a partial symbol table entry we only
827 need this much information. */
828 struct partial_die_info : public allocate_on_obstack
829 {
830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
835 partial_die_info (const partial_die_info &) = default;
836
837 /* Adjust the partial die before generating a symbol for it. This
838 function may set the is_external flag or change the DIE's
839 name. */
840 void fixup (struct dwarf2_cu *cu);
841
842 /* Read a minimal amount of information into the minimal die
843 structure. */
844 const gdb_byte *read (const struct die_reader_specs *reader,
845 const struct abbrev_info &abbrev,
846 const gdb_byte *info_ptr);
847
848 /* Compute the name of this partial DIE. This memoizes the
849 result, so it is safe to call multiple times. */
850 const char *name (dwarf2_cu *cu);
851
852 /* Offset of this DIE. */
853 const sect_offset sect_off;
854
855 /* DWARF-2 tag for this DIE. */
856 const ENUM_BITFIELD(dwarf_tag) tag : 16;
857
858 /* Assorted flags describing the data found in this DIE. */
859 const unsigned int has_children : 1;
860
861 unsigned int is_external : 1;
862 unsigned int is_declaration : 1;
863 unsigned int has_type : 1;
864 unsigned int has_specification : 1;
865 unsigned int has_pc_info : 1;
866 unsigned int has_range_info : 1;
867 unsigned int may_be_inlined : 1;
868
869 /* This DIE has been marked DW_AT_main_subprogram. */
870 unsigned int main_subprogram : 1;
871
872 /* Flag set if the SCOPE field of this structure has been
873 computed. */
874 unsigned int scope_set : 1;
875
876 /* Flag set if the DIE has a byte_size attribute. */
877 unsigned int has_byte_size : 1;
878
879 /* Flag set if the DIE has a DW_AT_const_value attribute. */
880 unsigned int has_const_value : 1;
881
882 /* Flag set if any of the DIE's children are template arguments. */
883 unsigned int has_template_arguments : 1;
884
885 /* Flag set if fixup has been called on this die. */
886 unsigned int fixup_called : 1;
887
888 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
889 unsigned int is_dwz : 1;
890
891 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
892 unsigned int spec_is_dwz : 1;
893
894 unsigned int canonical_name : 1;
895
896 /* The name of this DIE. Normally the value of DW_AT_name, but
897 sometimes a default name for unnamed DIEs. */
898 const char *raw_name = nullptr;
899
900 /* The linkage name, if present. */
901 const char *linkage_name = nullptr;
902
903 /* The scope to prepend to our children. This is generally
904 allocated on the comp_unit_obstack, so will disappear
905 when this compilation unit leaves the cache. */
906 const char *scope = nullptr;
907
908 /* Some data associated with the partial DIE. The tag determines
909 which field is live. */
910 union
911 {
912 /* The location description associated with this DIE, if any. */
913 struct dwarf_block *locdesc;
914 /* The offset of an import, for DW_TAG_imported_unit. */
915 sect_offset sect_off;
916 } d {};
917
918 union
919 {
920 /* If HAS_PC_INFO, the PC range associated with this DIE. */
921 struct
922 {
923 CORE_ADDR lowpc;
924 CORE_ADDR highpc;
925 };
926 /* If HAS_RANGE_INFO, the ranges offset associated with this DIE. */
927 ULONGEST ranges_offset;
928 };
929
930 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
931 DW_AT_sibling, if any. */
932 /* NOTE: This member isn't strictly necessary, partial_die_info::read
933 could return DW_AT_sibling values to its caller load_partial_dies. */
934 const gdb_byte *sibling = nullptr;
935
936 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
937 DW_AT_specification (or DW_AT_abstract_origin or
938 DW_AT_extension). */
939 sect_offset spec_offset {};
940
941 /* Pointers to this DIE's parent, first child, and next sibling,
942 if any. */
943 struct partial_die_info *die_parent = nullptr;
944 struct partial_die_info *die_child = nullptr;
945 struct partial_die_info *die_sibling = nullptr;
946
947 friend struct partial_die_info *
948 dwarf2_cu::find_partial_die (sect_offset sect_off);
949
950 private:
951 /* Only need to do look up in dwarf2_cu::find_partial_die. */
952 partial_die_info (sect_offset sect_off)
953 : partial_die_info (sect_off, DW_TAG_padding, 0)
954 {
955 }
956
957 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
958 int has_children_)
959 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
960 {
961 is_external = 0;
962 is_declaration = 0;
963 has_type = 0;
964 has_specification = 0;
965 has_pc_info = 0;
966 has_range_info = 0;
967 may_be_inlined = 0;
968 main_subprogram = 0;
969 scope_set = 0;
970 has_byte_size = 0;
971 has_const_value = 0;
972 has_template_arguments = 0;
973 fixup_called = 0;
974 is_dwz = 0;
975 spec_is_dwz = 0;
976 canonical_name = 0;
977 /* Don't set these using NSDMI (Non-static data member initialisation),
978 because g++-4.8 will error out. */
979 lowpc = 0;
980 highpc = 0;
981 }
982 };
983
984 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
985 but this would require a corresponding change in unpack_field_as_long
986 and friends. */
987 static int bits_per_byte = 8;
988
989 struct variant_part_builder;
990
991 /* When reading a variant, we track a bit more information about the
992 field, and store it in an object of this type. */
993
994 struct variant_field
995 {
996 int first_field = -1;
997 int last_field = -1;
998
999 /* A variant can contain other variant parts. */
1000 std::vector<variant_part_builder> variant_parts;
1001
1002 /* If we see a DW_TAG_variant, then this will be set if this is the
1003 default branch. */
1004 bool default_branch = false;
1005 /* If we see a DW_AT_discr_value, then this will be the discriminant
1006 value. */
1007 ULONGEST discriminant_value = 0;
1008 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1009 data. */
1010 struct dwarf_block *discr_list_data = nullptr;
1011 };
1012
1013 /* This represents a DW_TAG_variant_part. */
1014
1015 struct variant_part_builder
1016 {
1017 /* The offset of the discriminant field. */
1018 sect_offset discriminant_offset {};
1019
1020 /* Variants that are direct children of this variant part. */
1021 std::vector<variant_field> variants;
1022
1023 /* True if we're currently reading a variant. */
1024 bool processing_variant = false;
1025 };
1026
1027 struct nextfield
1028 {
1029 int accessibility = 0;
1030 int virtuality = 0;
1031 /* Variant parts need to find the discriminant, which is a DIE
1032 reference. We track the section offset of each field to make
1033 this link. */
1034 sect_offset offset;
1035 struct field field {};
1036 };
1037
1038 struct fnfieldlist
1039 {
1040 const char *name = nullptr;
1041 std::vector<struct fn_field> fnfields;
1042 };
1043
1044 /* The routines that read and process dies for a C struct or C++ class
1045 pass lists of data member fields and lists of member function fields
1046 in an instance of a field_info structure, as defined below. */
1047 struct field_info
1048 {
1049 /* List of data member and baseclasses fields. */
1050 std::vector<struct nextfield> fields;
1051 std::vector<struct nextfield> baseclasses;
1052
1053 /* Set if the accessibility of one of the fields is not public. */
1054 bool non_public_fields = false;
1055
1056 /* Member function fieldlist array, contains name of possibly overloaded
1057 member function, number of overloaded member functions and a pointer
1058 to the head of the member function field chain. */
1059 std::vector<struct fnfieldlist> fnfieldlists;
1060
1061 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1062 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1063 std::vector<struct decl_field> typedef_field_list;
1064
1065 /* Nested types defined by this class and the number of elements in this
1066 list. */
1067 std::vector<struct decl_field> nested_types_list;
1068
1069 /* If non-null, this is the variant part we are currently
1070 reading. */
1071 variant_part_builder *current_variant_part = nullptr;
1072 /* This holds all the top-level variant parts attached to the type
1073 we're reading. */
1074 std::vector<variant_part_builder> variant_parts;
1075
1076 /* Return the total number of fields (including baseclasses). */
1077 int nfields () const
1078 {
1079 return fields.size () + baseclasses.size ();
1080 }
1081 };
1082
1083 /* Loaded secondary compilation units are kept in memory until they
1084 have not been referenced for the processing of this many
1085 compilation units. Set this to zero to disable caching. Cache
1086 sizes of up to at least twenty will improve startup time for
1087 typical inter-CU-reference binaries, at an obvious memory cost. */
1088 static int dwarf_max_cache_age = 5;
1089 static void
1090 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1091 struct cmd_list_element *c, const char *value)
1092 {
1093 gdb_printf (file, _("The upper bound on the age of cached "
1094 "DWARF compilation units is %s.\n"),
1095 value);
1096 }
1097 \f
1098 /* local function prototypes */
1099
1100 static void dwarf2_find_base_address (struct die_info *die,
1101 struct dwarf2_cu *cu);
1102
1103 static dwarf2_psymtab *create_partial_symtab
1104 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1105 const char *name);
1106
1107 static void build_type_psymtabs_reader (cutu_reader *reader);
1108
1109 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1110
1111 static void scan_partial_symbols (struct partial_die_info *,
1112 CORE_ADDR *, CORE_ADDR *,
1113 int, struct dwarf2_cu *);
1114
1115 static void add_partial_symbol (struct partial_die_info *,
1116 struct dwarf2_cu *);
1117
1118 static void add_partial_namespace (struct partial_die_info *pdi,
1119 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1120 int set_addrmap, struct dwarf2_cu *cu);
1121
1122 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1123 CORE_ADDR *highpc, int set_addrmap,
1124 struct dwarf2_cu *cu);
1125
1126 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1127 struct dwarf2_cu *cu);
1128
1129 static void add_partial_subprogram (struct partial_die_info *pdi,
1130 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1131 int need_pc, struct dwarf2_cu *cu);
1132
1133 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1134
1135 static struct partial_die_info *load_partial_dies
1136 (const struct die_reader_specs *, const gdb_byte *, int);
1137
1138 /* A pair of partial_die_info and compilation unit. */
1139 struct cu_partial_die_info
1140 {
1141 /* The compilation unit of the partial_die_info. */
1142 struct dwarf2_cu *cu;
1143 /* A partial_die_info. */
1144 struct partial_die_info *pdi;
1145
1146 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1147 : cu (cu),
1148 pdi (pdi)
1149 { /* Nothing. */ }
1150
1151 private:
1152 cu_partial_die_info () = delete;
1153 };
1154
1155 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1156 struct dwarf2_cu *);
1157
1158 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1159 struct attribute *,
1160 const struct attr_abbrev *,
1161 const gdb_byte *);
1162
1163 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1164 struct attribute *attr, dwarf_tag tag);
1165
1166 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1167
1168 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1169 dwarf2_section_info *, sect_offset);
1170
1171 static const char *read_indirect_string
1172 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1173 const struct comp_unit_head *, unsigned int *);
1174
1175 static const char *read_indirect_string_at_offset
1176 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1177
1178 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1179 const gdb_byte *,
1180 unsigned int *);
1181
1182 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1183 ULONGEST str_index);
1184
1185 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1186 ULONGEST str_index);
1187
1188 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1189 struct dwarf2_cu *);
1190
1191 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1192 struct dwarf2_cu *cu);
1193
1194 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1195
1196 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1197 struct dwarf2_cu *cu);
1198
1199 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1200
1201 static struct die_info *die_specification (struct die_info *die,
1202 struct dwarf2_cu **);
1203
1204 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1205 struct dwarf2_cu *cu);
1206
1207 static void dwarf_decode_lines (struct line_header *,
1208 const file_and_directory &,
1209 struct dwarf2_cu *, dwarf2_psymtab *,
1210 CORE_ADDR, int decode_mapping);
1211
1212 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1213 const char *);
1214
1215 static struct symbol *new_symbol (struct die_info *, struct type *,
1216 struct dwarf2_cu *, struct symbol * = NULL);
1217
1218 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1219 struct dwarf2_cu *);
1220
1221 static void dwarf2_const_value_attr (const struct attribute *attr,
1222 struct type *type,
1223 const char *name,
1224 struct obstack *obstack,
1225 struct dwarf2_cu *cu, LONGEST *value,
1226 const gdb_byte **bytes,
1227 struct dwarf2_locexpr_baton **baton);
1228
1229 static struct type *read_subrange_index_type (struct die_info *die,
1230 struct dwarf2_cu *cu);
1231
1232 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1233
1234 static int need_gnat_info (struct dwarf2_cu *);
1235
1236 static struct type *die_descriptive_type (struct die_info *,
1237 struct dwarf2_cu *);
1238
1239 static void set_descriptive_type (struct type *, struct die_info *,
1240 struct dwarf2_cu *);
1241
1242 static struct type *die_containing_type (struct die_info *,
1243 struct dwarf2_cu *);
1244
1245 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1246 struct dwarf2_cu *);
1247
1248 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1249
1250 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1251
1252 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1253
1254 static char *typename_concat (struct obstack *obs, const char *prefix,
1255 const char *suffix, int physname,
1256 struct dwarf2_cu *cu);
1257
1258 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1259
1260 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1261
1262 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1263
1264 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1265
1266 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1267
1268 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1269
1270 /* Return the .debug_loclists section to use for cu. */
1271 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1272
1273 /* Return the .debug_rnglists section to use for cu. */
1274 static struct dwarf2_section_info *cu_debug_rnglists_section
1275 (struct dwarf2_cu *cu, dwarf_tag tag);
1276
1277 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1278 values. Keep the items ordered with increasing constraints compliance. */
1279 enum pc_bounds_kind
1280 {
1281 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1282 PC_BOUNDS_NOT_PRESENT,
1283
1284 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1285 were present but they do not form a valid range of PC addresses. */
1286 PC_BOUNDS_INVALID,
1287
1288 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1289 PC_BOUNDS_RANGES,
1290
1291 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1292 PC_BOUNDS_HIGH_LOW,
1293 };
1294
1295 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1296 CORE_ADDR *, CORE_ADDR *,
1297 struct dwarf2_cu *,
1298 addrmap *,
1299 void *);
1300
1301 static void get_scope_pc_bounds (struct die_info *,
1302 CORE_ADDR *, CORE_ADDR *,
1303 struct dwarf2_cu *);
1304
1305 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1306 CORE_ADDR, struct dwarf2_cu *);
1307
1308 static void dwarf2_add_field (struct field_info *, struct die_info *,
1309 struct dwarf2_cu *);
1310
1311 static void dwarf2_attach_fields_to_type (struct field_info *,
1312 struct type *, struct dwarf2_cu *);
1313
1314 static void dwarf2_add_member_fn (struct field_info *,
1315 struct die_info *, struct type *,
1316 struct dwarf2_cu *);
1317
1318 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1319 struct type *,
1320 struct dwarf2_cu *);
1321
1322 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1323
1324 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1325
1326 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1327
1328 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1329
1330 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1331
1332 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1333
1334 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1335
1336 static struct type *read_module_type (struct die_info *die,
1337 struct dwarf2_cu *cu);
1338
1339 static const char *namespace_name (struct die_info *die,
1340 int *is_anonymous, struct dwarf2_cu *);
1341
1342 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1343
1344 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1345 bool * = nullptr);
1346
1347 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1348 struct dwarf2_cu *);
1349
1350 static struct die_info *read_die_and_siblings_1
1351 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1352 struct die_info *);
1353
1354 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1355 const gdb_byte *info_ptr,
1356 const gdb_byte **new_info_ptr,
1357 struct die_info *parent);
1358
1359 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1360 struct die_info **, const gdb_byte *,
1361 int);
1362
1363 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1364 struct die_info **, const gdb_byte *);
1365
1366 static void process_die (struct die_info *, struct dwarf2_cu *);
1367
1368 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1369 struct objfile *);
1370
1371 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1372
1373 static const char *dwarf2_full_name (const char *name,
1374 struct die_info *die,
1375 struct dwarf2_cu *cu);
1376
1377 static const char *dwarf2_physname (const char *name, struct die_info *die,
1378 struct dwarf2_cu *cu);
1379
1380 static struct die_info *dwarf2_extension (struct die_info *die,
1381 struct dwarf2_cu **);
1382
1383 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1384
1385 static void dump_die_for_error (struct die_info *);
1386
1387 static void dump_die_1 (struct ui_file *, int level, int max_level,
1388 struct die_info *);
1389
1390 /*static*/ void dump_die (struct die_info *, int max_level);
1391
1392 static void store_in_ref_table (struct die_info *,
1393 struct dwarf2_cu *);
1394
1395 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1396 const struct attribute *,
1397 struct dwarf2_cu **);
1398
1399 static struct die_info *follow_die_ref (struct die_info *,
1400 const struct attribute *,
1401 struct dwarf2_cu **);
1402
1403 static struct die_info *follow_die_sig (struct die_info *,
1404 const struct attribute *,
1405 struct dwarf2_cu **);
1406
1407 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1408 struct dwarf2_cu *);
1409
1410 static struct type *get_DW_AT_signature_type (struct die_info *,
1411 const struct attribute *,
1412 struct dwarf2_cu *);
1413
1414 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1415 dwarf2_per_objfile *per_objfile);
1416
1417 static void read_signatured_type (signatured_type *sig_type,
1418 dwarf2_per_objfile *per_objfile);
1419
1420 static int attr_to_dynamic_prop (const struct attribute *attr,
1421 struct die_info *die, struct dwarf2_cu *cu,
1422 struct dynamic_prop *prop, struct type *type);
1423
1424 /* memory allocation interface */
1425
1426 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1427
1428 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1429
1430 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1431
1432 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1433 struct dwarf2_loclist_baton *baton,
1434 const struct attribute *attr);
1435
1436 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1437 struct symbol *sym,
1438 struct dwarf2_cu *cu,
1439 int is_block);
1440
1441 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1442 const gdb_byte *info_ptr,
1443 const struct abbrev_info *abbrev,
1444 bool do_skip_children = true);
1445
1446 static hashval_t partial_die_hash (const void *item);
1447
1448 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1449
1450 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1451 (sect_offset sect_off, unsigned int offset_in_dwz,
1452 dwarf2_per_bfd *per_bfd);
1453
1454 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1455 struct die_info *comp_unit_die,
1456 enum language pretend_language);
1457
1458 static struct type *set_die_type (struct die_info *, struct type *,
1459 struct dwarf2_cu *, bool = false);
1460
1461 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1462
1463 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1464 dwarf2_per_objfile *per_objfile,
1465 dwarf2_cu *existing_cu,
1466 bool skip_partial,
1467 enum language pretend_language);
1468
1469 static void process_full_comp_unit (dwarf2_cu *cu,
1470 enum language pretend_language);
1471
1472 static void process_full_type_unit (dwarf2_cu *cu,
1473 enum language pretend_language);
1474
1475 static struct type *get_die_type_at_offset (sect_offset,
1476 dwarf2_per_cu_data *per_cu,
1477 dwarf2_per_objfile *per_objfile);
1478
1479 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1480
1481 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1482 dwarf2_per_objfile *per_objfile,
1483 enum language pretend_language);
1484
1485 static void process_queue (dwarf2_per_objfile *per_objfile);
1486
1487 /* Class, the destructor of which frees all allocated queue entries. This
1488 will only have work to do if an error was thrown while processing the
1489 dwarf. If no error was thrown then the queue entries should have all
1490 been processed, and freed, as we went along. */
1491
1492 class dwarf2_queue_guard
1493 {
1494 public:
1495 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1496 : m_per_objfile (per_objfile)
1497 {
1498 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1499
1500 m_per_objfile->per_bfd->queue.emplace ();
1501 }
1502
1503 /* Free any entries remaining on the queue. There should only be
1504 entries left if we hit an error while processing the dwarf. */
1505 ~dwarf2_queue_guard ()
1506 {
1507 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1508
1509 m_per_objfile->per_bfd->queue.reset ();
1510 }
1511
1512 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1513
1514 private:
1515 dwarf2_per_objfile *m_per_objfile;
1516 };
1517
1518 dwarf2_queue_item::~dwarf2_queue_item ()
1519 {
1520 /* Anything still marked queued is likely to be in an
1521 inconsistent state, so discard it. */
1522 if (per_cu->queued)
1523 {
1524 per_objfile->remove_cu (per_cu);
1525 per_cu->queued = 0;
1526 }
1527 }
1528
1529 /* See dwarf2/read.h. */
1530
1531 void
1532 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1533 {
1534 if (data->is_debug_types)
1535 delete static_cast<signatured_type *> (data);
1536 else
1537 delete data;
1538 }
1539
1540 static file_and_directory &find_file_and_directory
1541 (struct die_info *die, struct dwarf2_cu *cu);
1542
1543 static const char *compute_include_file_name
1544 (const struct line_header *lh,
1545 const file_entry &fe,
1546 const file_and_directory &cu_info,
1547 gdb::unique_xmalloc_ptr<char> *name_holder);
1548
1549 static htab_up allocate_signatured_type_table ();
1550
1551 static htab_up allocate_dwo_unit_table ();
1552
1553 static struct dwo_unit *lookup_dwo_unit_in_dwp
1554 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1555 const char *comp_dir, ULONGEST signature, int is_debug_types);
1556
1557 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1558
1559 static struct dwo_unit *lookup_dwo_comp_unit
1560 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1561 ULONGEST signature);
1562
1563 static struct dwo_unit *lookup_dwo_type_unit
1564 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1565
1566 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1567
1568 /* A unique pointer to a dwo_file. */
1569
1570 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1571
1572 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1573
1574 static void check_producer (struct dwarf2_cu *cu);
1575 \f
1576 /* Various complaints about symbol reading that don't abort the process. */
1577
1578 static void
1579 dwarf2_debug_line_missing_file_complaint (void)
1580 {
1581 complaint (_(".debug_line section has line data without a file"));
1582 }
1583
1584 static void
1585 dwarf2_debug_line_missing_end_sequence_complaint (void)
1586 {
1587 complaint (_(".debug_line section has line "
1588 "program sequence without an end"));
1589 }
1590
1591 static void
1592 dwarf2_complex_location_expr_complaint (void)
1593 {
1594 complaint (_("location expression too complex"));
1595 }
1596
1597 static void
1598 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1599 int arg3)
1600 {
1601 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1602 arg1, arg2, arg3);
1603 }
1604
1605 static void
1606 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1607 {
1608 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1609 arg1, arg2);
1610 }
1611
1612 /* Hash function for line_header_hash. */
1613
1614 static hashval_t
1615 line_header_hash (const struct line_header *ofs)
1616 {
1617 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1618 }
1619
1620 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1621
1622 static hashval_t
1623 line_header_hash_voidp (const void *item)
1624 {
1625 const struct line_header *ofs = (const struct line_header *) item;
1626
1627 return line_header_hash (ofs);
1628 }
1629
1630 /* Equality function for line_header_hash. */
1631
1632 static int
1633 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1634 {
1635 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1636 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1637
1638 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1639 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1640 }
1641
1642 \f
1643
1644 /* An iterator for all_comp_units that is based on index. This
1645 approach makes it possible to iterate over all_comp_units safely,
1646 when some caller in the loop may add new units. */
1647
1648 class all_comp_units_iterator
1649 {
1650 public:
1651
1652 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1653 : m_per_bfd (per_bfd),
1654 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1655 {
1656 }
1657
1658 all_comp_units_iterator &operator++ ()
1659 {
1660 ++m_index;
1661 return *this;
1662 }
1663
1664 dwarf2_per_cu_data *operator* () const
1665 {
1666 return m_per_bfd->get_cu (m_index);
1667 }
1668
1669 bool operator== (const all_comp_units_iterator &other) const
1670 {
1671 return m_index == other.m_index;
1672 }
1673
1674
1675 bool operator!= (const all_comp_units_iterator &other) const
1676 {
1677 return m_index != other.m_index;
1678 }
1679
1680 private:
1681
1682 dwarf2_per_bfd *m_per_bfd;
1683 size_t m_index;
1684 };
1685
1686 /* A range adapter for the all_comp_units_iterator. */
1687 class all_comp_units_range
1688 {
1689 public:
1690
1691 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1692 : m_per_bfd (per_bfd)
1693 {
1694 }
1695
1696 all_comp_units_iterator begin ()
1697 {
1698 return all_comp_units_iterator (m_per_bfd, true);
1699 }
1700
1701 all_comp_units_iterator end ()
1702 {
1703 return all_comp_units_iterator (m_per_bfd, false);
1704 }
1705
1706 private:
1707
1708 dwarf2_per_bfd *m_per_bfd;
1709 };
1710
1711 /* See declaration. */
1712
1713 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1714 bool can_copy_)
1715 : obfd (obfd),
1716 can_copy (can_copy_)
1717 {
1718 if (names == NULL)
1719 names = &dwarf2_elf_names;
1720
1721 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1722 locate_sections (obfd, sec, *names);
1723 }
1724
1725 dwarf2_per_bfd::~dwarf2_per_bfd ()
1726 {
1727 for (auto &per_cu : all_comp_units)
1728 {
1729 per_cu->imported_symtabs_free ();
1730 per_cu->free_cached_file_names ();
1731 }
1732
1733 /* Everything else should be on this->obstack. */
1734 }
1735
1736 /* See read.h. */
1737
1738 void
1739 dwarf2_per_objfile::remove_all_cus ()
1740 {
1741 gdb_assert (!this->per_bfd->queue.has_value ());
1742
1743 for (auto pair : m_dwarf2_cus)
1744 delete pair.second;
1745
1746 m_dwarf2_cus.clear ();
1747 }
1748
1749 /* A helper class that calls free_cached_comp_units on
1750 destruction. */
1751
1752 class free_cached_comp_units
1753 {
1754 public:
1755
1756 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1757 : m_per_objfile (per_objfile)
1758 {
1759 }
1760
1761 ~free_cached_comp_units ()
1762 {
1763 m_per_objfile->remove_all_cus ();
1764 }
1765
1766 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1767
1768 private:
1769
1770 dwarf2_per_objfile *m_per_objfile;
1771 };
1772
1773 /* See read.h. */
1774
1775 bool
1776 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1777 {
1778 if (per_cu->index < this->m_symtabs.size ())
1779 return this->m_symtabs[per_cu->index] != nullptr;
1780 return false;
1781 }
1782
1783 /* See read.h. */
1784
1785 compunit_symtab *
1786 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1787 {
1788 if (per_cu->index < this->m_symtabs.size ())
1789 return this->m_symtabs[per_cu->index];
1790 return nullptr;
1791 }
1792
1793 /* See read.h. */
1794
1795 void
1796 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1797 compunit_symtab *symtab)
1798 {
1799 if (per_cu->index >= this->m_symtabs.size ())
1800 this->m_symtabs.resize (per_cu->index + 1);
1801 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1802 this->m_symtabs[per_cu->index] = symtab;
1803 }
1804
1805 /* Try to locate the sections we need for DWARF 2 debugging
1806 information and return true if we have enough to do something.
1807 NAMES points to the dwarf2 section names, or is NULL if the standard
1808 ELF names are used. CAN_COPY is true for formats where symbol
1809 interposition is possible and so symbol values must follow copy
1810 relocation rules. */
1811
1812 int
1813 dwarf2_has_info (struct objfile *objfile,
1814 const struct dwarf2_debug_sections *names,
1815 bool can_copy)
1816 {
1817 if (objfile->flags & OBJF_READNEVER)
1818 return 0;
1819
1820 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1821
1822 if (per_objfile == NULL)
1823 {
1824 dwarf2_per_bfd *per_bfd;
1825
1826 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1827 BFD doesn't require relocations.
1828
1829 We don't share with objfiles for which -readnow was requested,
1830 because it would complicate things when loading the same BFD with
1831 -readnow and then without -readnow. */
1832 if (!gdb_bfd_requires_relocations (objfile->obfd)
1833 && (objfile->flags & OBJF_READNOW) == 0)
1834 {
1835 /* See if one has been created for this BFD yet. */
1836 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1837
1838 if (per_bfd == nullptr)
1839 {
1840 /* No, create it now. */
1841 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1842 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1843 }
1844 }
1845 else
1846 {
1847 /* No sharing possible, create one specifically for this objfile. */
1848 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1849 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1850 }
1851
1852 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1853 }
1854
1855 return (!per_objfile->per_bfd->info.is_virtual
1856 && per_objfile->per_bfd->info.s.section != NULL
1857 && !per_objfile->per_bfd->abbrev.is_virtual
1858 && per_objfile->per_bfd->abbrev.s.section != NULL);
1859 }
1860
1861 /* See declaration. */
1862
1863 void
1864 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1865 const dwarf2_debug_sections &names)
1866 {
1867 flagword aflag = bfd_section_flags (sectp);
1868
1869 if ((aflag & SEC_HAS_CONTENTS) == 0)
1870 {
1871 }
1872 else if (elf_section_data (sectp)->this_hdr.sh_size
1873 > bfd_get_file_size (abfd))
1874 {
1875 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1876 warning (_("Discarding section %s which has a section size (%s"
1877 ") larger than the file size [in module %s]"),
1878 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1879 bfd_get_filename (abfd));
1880 }
1881 else if (names.info.matches (sectp->name))
1882 {
1883 this->info.s.section = sectp;
1884 this->info.size = bfd_section_size (sectp);
1885 }
1886 else if (names.abbrev.matches (sectp->name))
1887 {
1888 this->abbrev.s.section = sectp;
1889 this->abbrev.size = bfd_section_size (sectp);
1890 }
1891 else if (names.line.matches (sectp->name))
1892 {
1893 this->line.s.section = sectp;
1894 this->line.size = bfd_section_size (sectp);
1895 }
1896 else if (names.loc.matches (sectp->name))
1897 {
1898 this->loc.s.section = sectp;
1899 this->loc.size = bfd_section_size (sectp);
1900 }
1901 else if (names.loclists.matches (sectp->name))
1902 {
1903 this->loclists.s.section = sectp;
1904 this->loclists.size = bfd_section_size (sectp);
1905 }
1906 else if (names.macinfo.matches (sectp->name))
1907 {
1908 this->macinfo.s.section = sectp;
1909 this->macinfo.size = bfd_section_size (sectp);
1910 }
1911 else if (names.macro.matches (sectp->name))
1912 {
1913 this->macro.s.section = sectp;
1914 this->macro.size = bfd_section_size (sectp);
1915 }
1916 else if (names.str.matches (sectp->name))
1917 {
1918 this->str.s.section = sectp;
1919 this->str.size = bfd_section_size (sectp);
1920 }
1921 else if (names.str_offsets.matches (sectp->name))
1922 {
1923 this->str_offsets.s.section = sectp;
1924 this->str_offsets.size = bfd_section_size (sectp);
1925 }
1926 else if (names.line_str.matches (sectp->name))
1927 {
1928 this->line_str.s.section = sectp;
1929 this->line_str.size = bfd_section_size (sectp);
1930 }
1931 else if (names.addr.matches (sectp->name))
1932 {
1933 this->addr.s.section = sectp;
1934 this->addr.size = bfd_section_size (sectp);
1935 }
1936 else if (names.frame.matches (sectp->name))
1937 {
1938 this->frame.s.section = sectp;
1939 this->frame.size = bfd_section_size (sectp);
1940 }
1941 else if (names.eh_frame.matches (sectp->name))
1942 {
1943 this->eh_frame.s.section = sectp;
1944 this->eh_frame.size = bfd_section_size (sectp);
1945 }
1946 else if (names.ranges.matches (sectp->name))
1947 {
1948 this->ranges.s.section = sectp;
1949 this->ranges.size = bfd_section_size (sectp);
1950 }
1951 else if (names.rnglists.matches (sectp->name))
1952 {
1953 this->rnglists.s.section = sectp;
1954 this->rnglists.size = bfd_section_size (sectp);
1955 }
1956 else if (names.types.matches (sectp->name))
1957 {
1958 struct dwarf2_section_info type_section;
1959
1960 memset (&type_section, 0, sizeof (type_section));
1961 type_section.s.section = sectp;
1962 type_section.size = bfd_section_size (sectp);
1963
1964 this->types.push_back (type_section);
1965 }
1966 else if (names.gdb_index.matches (sectp->name))
1967 {
1968 this->gdb_index.s.section = sectp;
1969 this->gdb_index.size = bfd_section_size (sectp);
1970 }
1971 else if (names.debug_names.matches (sectp->name))
1972 {
1973 this->debug_names.s.section = sectp;
1974 this->debug_names.size = bfd_section_size (sectp);
1975 }
1976 else if (names.debug_aranges.matches (sectp->name))
1977 {
1978 this->debug_aranges.s.section = sectp;
1979 this->debug_aranges.size = bfd_section_size (sectp);
1980 }
1981
1982 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1983 && bfd_section_vma (sectp) == 0)
1984 this->has_section_at_zero = true;
1985 }
1986
1987 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1988 SECTION_NAME. */
1989
1990 void
1991 dwarf2_get_section_info (struct objfile *objfile,
1992 enum dwarf2_section_enum sect,
1993 asection **sectp, const gdb_byte **bufp,
1994 bfd_size_type *sizep)
1995 {
1996 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1997 struct dwarf2_section_info *info;
1998
1999 /* We may see an objfile without any DWARF, in which case we just
2000 return nothing. */
2001 if (per_objfile == NULL)
2002 {
2003 *sectp = NULL;
2004 *bufp = NULL;
2005 *sizep = 0;
2006 return;
2007 }
2008 switch (sect)
2009 {
2010 case DWARF2_DEBUG_FRAME:
2011 info = &per_objfile->per_bfd->frame;
2012 break;
2013 case DWARF2_EH_FRAME:
2014 info = &per_objfile->per_bfd->eh_frame;
2015 break;
2016 default:
2017 gdb_assert_not_reached ("unexpected section");
2018 }
2019
2020 info->read (objfile);
2021
2022 *sectp = info->get_bfd_section ();
2023 *bufp = info->buffer;
2024 *sizep = info->size;
2025 }
2026
2027 \f
2028 /* DWARF quick_symbol_functions support. */
2029
2030 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2031 unique line tables, so we maintain a separate table of all .debug_line
2032 derived entries to support the sharing.
2033 All the quick functions need is the list of file names. We discard the
2034 line_header when we're done and don't need to record it here. */
2035 struct quick_file_names
2036 {
2037 /* The data used to construct the hash key. */
2038 struct stmt_list_hash hash;
2039
2040 /* The number of entries in file_names, real_names. */
2041 unsigned int num_file_names;
2042
2043 /* The CU directory, as given by DW_AT_comp_dir. May be
2044 nullptr. */
2045 const char *comp_dir;
2046
2047 /* The file names from the line table, after being run through
2048 file_full_name. */
2049 const char **file_names;
2050
2051 /* The file names from the line table after being run through
2052 gdb_realpath. These are computed lazily. */
2053 const char **real_names;
2054 };
2055
2056 /* When using the index (and thus not using psymtabs), each CU has an
2057 object of this type. This is used to hold information needed by
2058 the various "quick" methods. */
2059 struct dwarf2_per_cu_quick_data
2060 {
2061 /* The file table. This can be NULL if there was no file table
2062 or it's currently not read in.
2063 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2064 struct quick_file_names *file_names;
2065
2066 /* A temporary mark bit used when iterating over all CUs in
2067 expand_symtabs_matching. */
2068 unsigned int mark : 1;
2069
2070 /* True if we've tried to read the file table. There will be no
2071 point in trying to read it again next time. */
2072 bool files_read : 1;
2073 };
2074
2075 /* A subclass of psymbol_functions that arranges to read the DWARF
2076 partial symbols when needed. */
2077 struct lazy_dwarf_reader : public psymbol_functions
2078 {
2079 using psymbol_functions::psymbol_functions;
2080
2081 bool can_lazily_read_symbols () override
2082 {
2083 return true;
2084 }
2085
2086 void read_partial_symbols (struct objfile *objfile) override
2087 {
2088 if (dwarf2_has_info (objfile, nullptr))
2089 dwarf2_build_psymtabs (objfile, this);
2090 }
2091 };
2092
2093 static quick_symbol_functions_up
2094 make_lazy_dwarf_reader ()
2095 {
2096 return quick_symbol_functions_up (new lazy_dwarf_reader);
2097 }
2098
2099 struct dwarf2_base_index_functions : public quick_symbol_functions
2100 {
2101 bool has_symbols (struct objfile *objfile) override;
2102
2103 bool has_unexpanded_symtabs (struct objfile *objfile) override;
2104
2105 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2106
2107 void forget_cached_source_info (struct objfile *objfile) override;
2108
2109 enum language lookup_global_symbol_language (struct objfile *objfile,
2110 const char *name,
2111 domain_enum domain,
2112 bool *symbol_found_p) override
2113 {
2114 *symbol_found_p = false;
2115 return language_unknown;
2116 }
2117
2118 void print_stats (struct objfile *objfile, bool print_bcache) override;
2119
2120 void expand_all_symtabs (struct objfile *objfile) override;
2121
2122 struct compunit_symtab *find_pc_sect_compunit_symtab
2123 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2124 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2125
2126 struct compunit_symtab *find_compunit_symtab_by_address
2127 (struct objfile *objfile, CORE_ADDR address) override
2128 {
2129 return nullptr;
2130 }
2131
2132 void map_symbol_filenames (struct objfile *objfile,
2133 gdb::function_view<symbol_filename_ftype> fun,
2134 bool need_fullname) override;
2135 };
2136
2137 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2138 {
2139 void dump (struct objfile *objfile) override;
2140
2141 void expand_matching_symbols
2142 (struct objfile *,
2143 const lookup_name_info &lookup_name,
2144 domain_enum domain,
2145 int global,
2146 symbol_compare_ftype *ordered_compare) override;
2147
2148 bool expand_symtabs_matching
2149 (struct objfile *objfile,
2150 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2151 const lookup_name_info *lookup_name,
2152 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2153 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2154 block_search_flags search_flags,
2155 domain_enum domain,
2156 enum search_domain kind) override;
2157 };
2158
2159 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2160 {
2161 void dump (struct objfile *objfile) override;
2162
2163 void expand_matching_symbols
2164 (struct objfile *,
2165 const lookup_name_info &lookup_name,
2166 domain_enum domain,
2167 int global,
2168 symbol_compare_ftype *ordered_compare) override;
2169
2170 bool expand_symtabs_matching
2171 (struct objfile *objfile,
2172 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2173 const lookup_name_info *lookup_name,
2174 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2176 block_search_flags search_flags,
2177 domain_enum domain,
2178 enum search_domain kind) override;
2179 };
2180
2181 static quick_symbol_functions_up
2182 make_dwarf_gdb_index ()
2183 {
2184 return quick_symbol_functions_up (new dwarf2_gdb_index);
2185 }
2186
2187 static quick_symbol_functions_up
2188 make_dwarf_debug_names ()
2189 {
2190 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2191 }
2192
2193 /* Utility hash function for a stmt_list_hash. */
2194
2195 static hashval_t
2196 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2197 {
2198 hashval_t v = 0;
2199
2200 if (stmt_list_hash->dwo_unit != NULL)
2201 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2202 v += to_underlying (stmt_list_hash->line_sect_off);
2203 return v;
2204 }
2205
2206 /* Utility equality function for a stmt_list_hash. */
2207
2208 static int
2209 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2210 const struct stmt_list_hash *rhs)
2211 {
2212 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2213 return 0;
2214 if (lhs->dwo_unit != NULL
2215 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2216 return 0;
2217
2218 return lhs->line_sect_off == rhs->line_sect_off;
2219 }
2220
2221 /* Hash function for a quick_file_names. */
2222
2223 static hashval_t
2224 hash_file_name_entry (const void *e)
2225 {
2226 const struct quick_file_names *file_data
2227 = (const struct quick_file_names *) e;
2228
2229 return hash_stmt_list_entry (&file_data->hash);
2230 }
2231
2232 /* Equality function for a quick_file_names. */
2233
2234 static int
2235 eq_file_name_entry (const void *a, const void *b)
2236 {
2237 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2238 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2239
2240 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2241 }
2242
2243 /* Create a quick_file_names hash table. */
2244
2245 static htab_up
2246 create_quick_file_names_table (unsigned int nr_initial_entries)
2247 {
2248 return htab_up (htab_create_alloc (nr_initial_entries,
2249 hash_file_name_entry, eq_file_name_entry,
2250 nullptr, xcalloc, xfree));
2251 }
2252
2253 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2254 function is unrelated to symtabs, symtab would have to be created afterwards.
2255 You should call age_cached_comp_units after processing the CU. */
2256
2257 static dwarf2_cu *
2258 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2259 bool skip_partial)
2260 {
2261 if (per_cu->is_debug_types)
2262 load_full_type_unit (per_cu, per_objfile);
2263 else
2264 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2265 skip_partial, language_minimal);
2266
2267 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2268 if (cu == nullptr)
2269 return nullptr; /* Dummy CU. */
2270
2271 dwarf2_find_base_address (cu->dies, cu);
2272
2273 return cu;
2274 }
2275
2276 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2277
2278 static void
2279 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2280 dwarf2_per_objfile *per_objfile, bool skip_partial)
2281 {
2282 /* Skip type_unit_groups, reading the type units they contain
2283 is handled elsewhere. */
2284 if (per_cu->type_unit_group_p ())
2285 return;
2286
2287 {
2288 /* The destructor of dwarf2_queue_guard frees any entries left on
2289 the queue. After this point we're guaranteed to leave this function
2290 with the dwarf queue empty. */
2291 dwarf2_queue_guard q_guard (per_objfile);
2292
2293 if (!per_objfile->symtab_set_p (per_cu))
2294 {
2295 queue_comp_unit (per_cu, per_objfile, language_minimal);
2296 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2297
2298 /* If we just loaded a CU from a DWO, and we're working with an index
2299 that may badly handle TUs, load all the TUs in that DWO as well.
2300 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2301 if (!per_cu->is_debug_types
2302 && cu != NULL
2303 && cu->dwo_unit != NULL
2304 && per_objfile->per_bfd->index_table != NULL
2305 && per_objfile->per_bfd->index_table->version <= 7
2306 /* DWP files aren't supported yet. */
2307 && get_dwp_file (per_objfile) == NULL)
2308 queue_and_load_all_dwo_tus (cu);
2309 }
2310
2311 process_queue (per_objfile);
2312 }
2313
2314 /* Age the cache, releasing compilation units that have not
2315 been used recently. */
2316 per_objfile->age_comp_units ();
2317 }
2318
2319 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2320 the per-objfile for which this symtab is instantiated.
2321
2322 Returns the resulting symbol table. */
2323
2324 static struct compunit_symtab *
2325 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2326 dwarf2_per_objfile *per_objfile,
2327 bool skip_partial)
2328 {
2329 gdb_assert (per_objfile->per_bfd->using_index);
2330
2331 if (!per_objfile->symtab_set_p (per_cu))
2332 {
2333 free_cached_comp_units freer (per_objfile);
2334 scoped_restore decrementer = increment_reading_symtab ();
2335 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2336 process_cu_includes (per_objfile);
2337 }
2338
2339 return per_objfile->get_symtab (per_cu);
2340 }
2341
2342 /* See read.h. */
2343
2344 dwarf2_per_cu_data_up
2345 dwarf2_per_bfd::allocate_per_cu ()
2346 {
2347 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2348 result->per_bfd = this;
2349 result->index = all_comp_units.size ();
2350 return result;
2351 }
2352
2353 /* See read.h. */
2354
2355 signatured_type_up
2356 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2357 {
2358 signatured_type_up result (new signatured_type (signature));
2359 result->per_bfd = this;
2360 result->index = all_comp_units.size ();
2361 result->is_debug_types = true;
2362 tu_stats.nr_tus++;
2363 return result;
2364 }
2365
2366 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2367 obstack, and constructed with the specified field values. */
2368
2369 static dwarf2_per_cu_data_up
2370 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2371 struct dwarf2_section_info *section,
2372 int is_dwz,
2373 sect_offset sect_off, ULONGEST length)
2374 {
2375 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2376 the_cu->sect_off = sect_off;
2377 the_cu->length = length;
2378 the_cu->section = section;
2379 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2380 struct dwarf2_per_cu_quick_data);
2381 the_cu->is_dwz = is_dwz;
2382 return the_cu;
2383 }
2384
2385 /* A helper for create_cus_from_index that handles a given list of
2386 CUs. */
2387
2388 static void
2389 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2390 const gdb_byte *cu_list, offset_type n_elements,
2391 struct dwarf2_section_info *section,
2392 int is_dwz)
2393 {
2394 for (offset_type i = 0; i < n_elements; i += 2)
2395 {
2396 gdb_static_assert (sizeof (ULONGEST) >= 8);
2397
2398 sect_offset sect_off
2399 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2400 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2401 cu_list += 2 * 8;
2402
2403 dwarf2_per_cu_data_up per_cu
2404 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2405 length);
2406 per_bfd->all_comp_units.push_back (std::move (per_cu));
2407 }
2408 }
2409
2410 /* Read the CU list from the mapped index, and use it to create all
2411 the CU objects for PER_BFD. */
2412
2413 static void
2414 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2415 const gdb_byte *cu_list, offset_type cu_list_elements,
2416 const gdb_byte *dwz_list, offset_type dwz_elements)
2417 {
2418 gdb_assert (per_bfd->all_comp_units.empty ());
2419 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2420
2421 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2422 &per_bfd->info, 0);
2423
2424 if (dwz_elements == 0)
2425 return;
2426
2427 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2428 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2429 &dwz->info, 1);
2430 }
2431
2432 /* Create the signatured type hash table from the index. */
2433
2434 static void
2435 create_signatured_type_table_from_index
2436 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2437 const gdb_byte *bytes, offset_type elements)
2438 {
2439 htab_up sig_types_hash = allocate_signatured_type_table ();
2440
2441 for (offset_type i = 0; i < elements; i += 3)
2442 {
2443 signatured_type_up sig_type;
2444 ULONGEST signature;
2445 void **slot;
2446 cu_offset type_offset_in_tu;
2447
2448 gdb_static_assert (sizeof (ULONGEST) >= 8);
2449 sect_offset sect_off
2450 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2451 type_offset_in_tu
2452 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2453 BFD_ENDIAN_LITTLE);
2454 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2455 bytes += 3 * 8;
2456
2457 sig_type = per_bfd->allocate_signatured_type (signature);
2458 sig_type->type_offset_in_tu = type_offset_in_tu;
2459 sig_type->section = section;
2460 sig_type->sect_off = sect_off;
2461 sig_type->v.quick
2462 = OBSTACK_ZALLOC (&per_bfd->obstack,
2463 struct dwarf2_per_cu_quick_data);
2464
2465 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2466 *slot = sig_type.get ();
2467
2468 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2469 }
2470
2471 per_bfd->signatured_types = std::move (sig_types_hash);
2472 }
2473
2474 /* Create the signatured type hash table from .debug_names. */
2475
2476 static void
2477 create_signatured_type_table_from_debug_names
2478 (dwarf2_per_objfile *per_objfile,
2479 const mapped_debug_names &map,
2480 struct dwarf2_section_info *section,
2481 struct dwarf2_section_info *abbrev_section)
2482 {
2483 struct objfile *objfile = per_objfile->objfile;
2484
2485 section->read (objfile);
2486 abbrev_section->read (objfile);
2487
2488 htab_up sig_types_hash = allocate_signatured_type_table ();
2489
2490 for (uint32_t i = 0; i < map.tu_count; ++i)
2491 {
2492 signatured_type_up sig_type;
2493 void **slot;
2494
2495 sect_offset sect_off
2496 = (sect_offset) (extract_unsigned_integer
2497 (map.tu_table_reordered + i * map.offset_size,
2498 map.offset_size,
2499 map.dwarf5_byte_order));
2500
2501 comp_unit_head cu_header;
2502 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2503 abbrev_section,
2504 section->buffer + to_underlying (sect_off),
2505 rcuh_kind::TYPE);
2506
2507 sig_type = per_objfile->per_bfd->allocate_signatured_type
2508 (cu_header.signature);
2509 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2510 sig_type->section = section;
2511 sig_type->sect_off = sect_off;
2512 sig_type->v.quick
2513 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2514 struct dwarf2_per_cu_quick_data);
2515
2516 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2517 *slot = sig_type.get ();
2518
2519 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2520 }
2521
2522 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2523 }
2524
2525 /* Read the address map data from the mapped index, and use it to
2526 populate the psymtabs_addrmap. */
2527
2528 static void
2529 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2530 struct mapped_index *index)
2531 {
2532 struct objfile *objfile = per_objfile->objfile;
2533 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2534 struct gdbarch *gdbarch = objfile->arch ();
2535 const gdb_byte *iter, *end;
2536 struct addrmap *mutable_map;
2537 CORE_ADDR baseaddr;
2538
2539 auto_obstack temp_obstack;
2540
2541 mutable_map = addrmap_create_mutable (&temp_obstack);
2542
2543 iter = index->address_table.data ();
2544 end = iter + index->address_table.size ();
2545
2546 baseaddr = objfile->text_section_offset ();
2547
2548 while (iter < end)
2549 {
2550 ULONGEST hi, lo, cu_index;
2551 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2552 iter += 8;
2553 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2554 iter += 8;
2555 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2556 iter += 4;
2557
2558 if (lo > hi)
2559 {
2560 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2561 hex_string (lo), hex_string (hi));
2562 continue;
2563 }
2564
2565 if (cu_index >= per_bfd->all_comp_units.size ())
2566 {
2567 complaint (_(".gdb_index address table has invalid CU number %u"),
2568 (unsigned) cu_index);
2569 continue;
2570 }
2571
2572 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2573 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2574 addrmap_set_empty (mutable_map, lo, hi - 1,
2575 per_bfd->get_cu (cu_index));
2576 }
2577
2578 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2579 &per_bfd->obstack);
2580 }
2581
2582 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2583 to populate given addrmap. Returns true on success, false on
2584 failure. */
2585
2586 static bool
2587 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2588 struct dwarf2_section_info *section,
2589 addrmap *mutable_map)
2590 {
2591 struct objfile *objfile = per_objfile->objfile;
2592 bfd *abfd = objfile->obfd;
2593 struct gdbarch *gdbarch = objfile->arch ();
2594 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2595 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2596
2597 std::unordered_map<sect_offset,
2598 dwarf2_per_cu_data *,
2599 gdb::hash_enum<sect_offset>>
2600 debug_info_offset_to_per_cu;
2601 for (const auto &per_cu : per_bfd->all_comp_units)
2602 {
2603 /* A TU will not need aranges, and skipping them here is an easy
2604 way of ignoring .debug_types -- and possibly seeing a
2605 duplicate section offset -- entirely. The same applies to
2606 units coming from a dwz file. */
2607 if (per_cu->is_debug_types || per_cu->is_dwz)
2608 continue;
2609
2610 const auto insertpair
2611 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2612 per_cu.get ());
2613 if (!insertpair.second)
2614 {
2615 warning (_("Section .debug_aranges in %s has duplicate "
2616 "debug_info_offset %s, ignoring .debug_aranges."),
2617 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2618 return false;
2619 }
2620 }
2621
2622 section->read (objfile);
2623
2624 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2625
2626 const gdb_byte *addr = section->buffer;
2627
2628 while (addr < section->buffer + section->size)
2629 {
2630 const gdb_byte *const entry_addr = addr;
2631 unsigned int bytes_read;
2632
2633 const LONGEST entry_length = read_initial_length (abfd, addr,
2634 &bytes_read);
2635 addr += bytes_read;
2636
2637 const gdb_byte *const entry_end = addr + entry_length;
2638 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2639 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2640 if (addr + entry_length > section->buffer + section->size)
2641 {
2642 warning (_("Section .debug_aranges in %s entry at offset %s "
2643 "length %s exceeds section length %s, "
2644 "ignoring .debug_aranges."),
2645 objfile_name (objfile),
2646 plongest (entry_addr - section->buffer),
2647 plongest (bytes_read + entry_length),
2648 pulongest (section->size));
2649 return false;
2650 }
2651
2652 /* The version number. */
2653 const uint16_t version = read_2_bytes (abfd, addr);
2654 addr += 2;
2655 if (version != 2)
2656 {
2657 warning (_("Section .debug_aranges in %s entry at offset %s "
2658 "has unsupported version %d, ignoring .debug_aranges."),
2659 objfile_name (objfile),
2660 plongest (entry_addr - section->buffer), version);
2661 return false;
2662 }
2663
2664 const uint64_t debug_info_offset
2665 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2666 addr += offset_size;
2667 const auto per_cu_it
2668 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2669 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2670 {
2671 warning (_("Section .debug_aranges in %s entry at offset %s "
2672 "debug_info_offset %s does not exists, "
2673 "ignoring .debug_aranges."),
2674 objfile_name (objfile),
2675 plongest (entry_addr - section->buffer),
2676 pulongest (debug_info_offset));
2677 return false;
2678 }
2679 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2680
2681 const uint8_t address_size = *addr++;
2682 if (address_size < 1 || address_size > 8)
2683 {
2684 warning (_("Section .debug_aranges in %s entry at offset %s "
2685 "address_size %u is invalid, ignoring .debug_aranges."),
2686 objfile_name (objfile),
2687 plongest (entry_addr - section->buffer), address_size);
2688 return false;
2689 }
2690
2691 const uint8_t segment_selector_size = *addr++;
2692 if (segment_selector_size != 0)
2693 {
2694 warning (_("Section .debug_aranges in %s entry at offset %s "
2695 "segment_selector_size %u is not supported, "
2696 "ignoring .debug_aranges."),
2697 objfile_name (objfile),
2698 plongest (entry_addr - section->buffer),
2699 segment_selector_size);
2700 return false;
2701 }
2702
2703 /* Must pad to an alignment boundary that is twice the address
2704 size. It is undocumented by the DWARF standard but GCC does
2705 use it. However, not every compiler does this. We can see
2706 whether it has happened by looking at the total length of the
2707 contents of the aranges for this CU -- it if isn't a multiple
2708 of twice the address size, then we skip any leftover
2709 bytes. */
2710 addr += (entry_end - addr) % (2 * address_size);
2711
2712 for (;;)
2713 {
2714 if (addr + 2 * address_size > entry_end)
2715 {
2716 warning (_("Section .debug_aranges in %s entry at offset %s "
2717 "address list is not properly terminated, "
2718 "ignoring .debug_aranges."),
2719 objfile_name (objfile),
2720 plongest (entry_addr - section->buffer));
2721 return false;
2722 }
2723 ULONGEST start = extract_unsigned_integer (addr, address_size,
2724 dwarf5_byte_order);
2725 addr += address_size;
2726 ULONGEST length = extract_unsigned_integer (addr, address_size,
2727 dwarf5_byte_order);
2728 addr += address_size;
2729 if (start == 0 && length == 0)
2730 break;
2731 if (start == 0 && !per_bfd->has_section_at_zero)
2732 {
2733 /* Symbol was eliminated due to a COMDAT group. */
2734 continue;
2735 }
2736 ULONGEST end = start + length;
2737 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2738 - baseaddr);
2739 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2740 - baseaddr);
2741 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2742 }
2743
2744 per_cu->addresses_seen = true;
2745 }
2746
2747 return true;
2748 }
2749
2750 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2751 populate the psymtabs_addrmap. */
2752
2753 static void
2754 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2755 struct dwarf2_section_info *section)
2756 {
2757 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2758
2759 auto_obstack temp_obstack;
2760 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2761
2762 if (read_addrmap_from_aranges (per_objfile, section, mutable_map))
2763 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2764 &per_bfd->obstack);
2765 }
2766
2767 /* A helper function that reads the .gdb_index from BUFFER and fills
2768 in MAP. FILENAME is the name of the file containing the data;
2769 it is used for error reporting. DEPRECATED_OK is true if it is
2770 ok to use deprecated sections.
2771
2772 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2773 out parameters that are filled in with information about the CU and
2774 TU lists in the section.
2775
2776 Returns true if all went well, false otherwise. */
2777
2778 static bool
2779 read_gdb_index_from_buffer (const char *filename,
2780 bool deprecated_ok,
2781 gdb::array_view<const gdb_byte> buffer,
2782 struct mapped_index *map,
2783 const gdb_byte **cu_list,
2784 offset_type *cu_list_elements,
2785 const gdb_byte **types_list,
2786 offset_type *types_list_elements)
2787 {
2788 const gdb_byte *addr = &buffer[0];
2789 offset_view metadata (buffer);
2790
2791 /* Version check. */
2792 offset_type version = metadata[0];
2793 /* Versions earlier than 3 emitted every copy of a psymbol. This
2794 causes the index to behave very poorly for certain requests. Version 3
2795 contained incomplete addrmap. So, it seems better to just ignore such
2796 indices. */
2797 if (version < 4)
2798 {
2799 static int warning_printed = 0;
2800 if (!warning_printed)
2801 {
2802 warning (_("Skipping obsolete .gdb_index section in %s."),
2803 filename);
2804 warning_printed = 1;
2805 }
2806 return 0;
2807 }
2808 /* Index version 4 uses a different hash function than index version
2809 5 and later.
2810
2811 Versions earlier than 6 did not emit psymbols for inlined
2812 functions. Using these files will cause GDB not to be able to
2813 set breakpoints on inlined functions by name, so we ignore these
2814 indices unless the user has done
2815 "set use-deprecated-index-sections on". */
2816 if (version < 6 && !deprecated_ok)
2817 {
2818 static int warning_printed = 0;
2819 if (!warning_printed)
2820 {
2821 warning (_("\
2822 Skipping deprecated .gdb_index section in %s.\n\
2823 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2824 to use the section anyway."),
2825 filename);
2826 warning_printed = 1;
2827 }
2828 return 0;
2829 }
2830 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2831 of the TU (for symbols coming from TUs),
2832 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2833 Plus gold-generated indices can have duplicate entries for global symbols,
2834 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2835 These are just performance bugs, and we can't distinguish gdb-generated
2836 indices from gold-generated ones, so issue no warning here. */
2837
2838 /* Indexes with higher version than the one supported by GDB may be no
2839 longer backward compatible. */
2840 if (version > 8)
2841 return 0;
2842
2843 map->version = version;
2844
2845 int i = 1;
2846 *cu_list = addr + metadata[i];
2847 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2848 ++i;
2849
2850 *types_list = addr + metadata[i];
2851 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2852 ++i;
2853
2854 const gdb_byte *address_table = addr + metadata[i];
2855 const gdb_byte *address_table_end = addr + metadata[i + 1];
2856 map->address_table
2857 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2858 ++i;
2859
2860 const gdb_byte *symbol_table = addr + metadata[i];
2861 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2862 map->symbol_table
2863 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2864 symbol_table_end));
2865
2866 ++i;
2867 map->constant_pool = buffer.slice (metadata[i]);
2868
2869 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2870 {
2871 /* An empty constant pool implies that all symbol table entries are
2872 empty. Make map->symbol_table.empty () == true. */
2873 map->symbol_table
2874 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2875 symbol_table));
2876 }
2877
2878 return 1;
2879 }
2880
2881 /* Callback types for dwarf2_read_gdb_index. */
2882
2883 typedef gdb::function_view
2884 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2885 get_gdb_index_contents_ftype;
2886 typedef gdb::function_view
2887 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2888 get_gdb_index_contents_dwz_ftype;
2889
2890 /* Read .gdb_index. If everything went ok, initialize the "quick"
2891 elements of all the CUs and return 1. Otherwise, return 0. */
2892
2893 static int
2894 dwarf2_read_gdb_index
2895 (dwarf2_per_objfile *per_objfile,
2896 get_gdb_index_contents_ftype get_gdb_index_contents,
2897 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2898 {
2899 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2900 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2901 struct dwz_file *dwz;
2902 struct objfile *objfile = per_objfile->objfile;
2903 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2904
2905 gdb::array_view<const gdb_byte> main_index_contents
2906 = get_gdb_index_contents (objfile, per_bfd);
2907
2908 if (main_index_contents.empty ())
2909 return 0;
2910
2911 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2912 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2913 use_deprecated_index_sections,
2914 main_index_contents, map.get (), &cu_list,
2915 &cu_list_elements, &types_list,
2916 &types_list_elements))
2917 return 0;
2918
2919 /* Don't use the index if it's empty. */
2920 if (map->symbol_table.empty ())
2921 return 0;
2922
2923 /* If there is a .dwz file, read it so we can get its CU list as
2924 well. */
2925 dwz = dwarf2_get_dwz_file (per_bfd);
2926 if (dwz != NULL)
2927 {
2928 struct mapped_index dwz_map;
2929 const gdb_byte *dwz_types_ignore;
2930 offset_type dwz_types_elements_ignore;
2931
2932 gdb::array_view<const gdb_byte> dwz_index_content
2933 = get_gdb_index_contents_dwz (objfile, dwz);
2934
2935 if (dwz_index_content.empty ())
2936 return 0;
2937
2938 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2939 1, dwz_index_content, &dwz_map,
2940 &dwz_list, &dwz_list_elements,
2941 &dwz_types_ignore,
2942 &dwz_types_elements_ignore))
2943 {
2944 warning (_("could not read '.gdb_index' section from %s; skipping"),
2945 bfd_get_filename (dwz->dwz_bfd.get ()));
2946 return 0;
2947 }
2948 }
2949
2950 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2951 dwz_list_elements);
2952
2953 if (types_list_elements)
2954 {
2955 /* We can only handle a single .debug_types when we have an
2956 index. */
2957 if (per_bfd->types.size () != 1)
2958 return 0;
2959
2960 dwarf2_section_info *section = &per_bfd->types[0];
2961
2962 create_signatured_type_table_from_index (per_bfd, section, types_list,
2963 types_list_elements);
2964 }
2965
2966 create_addrmap_from_index (per_objfile, map.get ());
2967
2968 per_bfd->index_table = std::move (map);
2969 per_bfd->using_index = 1;
2970 per_bfd->quick_file_names_table =
2971 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2972
2973 return 1;
2974 }
2975
2976 /* die_reader_func for dw2_get_file_names. */
2977
2978 static void
2979 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2980 struct die_info *comp_unit_die)
2981 {
2982 struct dwarf2_cu *cu = reader->cu;
2983 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2984 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2985 struct dwarf2_per_cu_data *lh_cu;
2986 struct attribute *attr;
2987 void **slot;
2988 struct quick_file_names *qfn;
2989
2990 gdb_assert (! this_cu->is_debug_types);
2991
2992 this_cu->v.quick->files_read = true;
2993 /* Our callers never want to match partial units -- instead they
2994 will match the enclosing full CU. */
2995 if (comp_unit_die->tag == DW_TAG_partial_unit)
2996 return;
2997
2998 lh_cu = this_cu;
2999 slot = NULL;
3000
3001 line_header_up lh;
3002 sect_offset line_offset {};
3003
3004 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3005 if (attr != nullptr && attr->form_is_unsigned ())
3006 {
3007 struct quick_file_names find_entry;
3008
3009 line_offset = (sect_offset) attr->as_unsigned ();
3010
3011 /* We may have already read in this line header (TU line header sharing).
3012 If we have we're done. */
3013 find_entry.hash.dwo_unit = cu->dwo_unit;
3014 find_entry.hash.line_sect_off = line_offset;
3015 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3016 &find_entry, INSERT);
3017 if (*slot != NULL)
3018 {
3019 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3020 return;
3021 }
3022
3023 lh = dwarf_decode_line_header (line_offset, cu);
3024 }
3025
3026 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
3027
3028 int offset = 0;
3029 if (!fnd.is_unknown ())
3030 ++offset;
3031 else if (lh == nullptr)
3032 return;
3033
3034 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3035 qfn->hash.dwo_unit = cu->dwo_unit;
3036 qfn->hash.line_sect_off = line_offset;
3037 /* There may not be a DW_AT_stmt_list. */
3038 if (slot != nullptr)
3039 *slot = qfn;
3040
3041 std::vector<const char *> include_names;
3042 if (lh != nullptr)
3043 {
3044 for (const auto &entry : lh->file_names ())
3045 {
3046 gdb::unique_xmalloc_ptr<char> name_holder;
3047 const char *include_name =
3048 compute_include_file_name (lh.get (), entry, fnd, &name_holder);
3049 if (include_name != nullptr)
3050 {
3051 include_name = per_objfile->objfile->intern (include_name);
3052 include_names.push_back (include_name);
3053 }
3054 }
3055 }
3056
3057 qfn->num_file_names = offset + include_names.size ();
3058 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
3059 qfn->file_names =
3060 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3061 qfn->num_file_names);
3062 if (offset != 0)
3063 qfn->file_names[0] = xstrdup (fnd.get_name ());
3064
3065 if (!include_names.empty ())
3066 memcpy (&qfn->file_names[offset], include_names.data (),
3067 include_names.size () * sizeof (const char *));
3068
3069 qfn->real_names = NULL;
3070
3071 lh_cu->v.quick->file_names = qfn;
3072 }
3073
3074 /* A helper for the "quick" functions which attempts to read the line
3075 table for THIS_CU. */
3076
3077 static struct quick_file_names *
3078 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3079 dwarf2_per_objfile *per_objfile)
3080 {
3081 /* This should never be called for TUs. */
3082 gdb_assert (! this_cu->is_debug_types);
3083 /* Nor type unit groups. */
3084 gdb_assert (! this_cu->type_unit_group_p ());
3085
3086 if (this_cu->v.quick->files_read)
3087 return this_cu->v.quick->file_names;
3088
3089 cutu_reader reader (this_cu, per_objfile);
3090 if (!reader.dummy_p)
3091 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3092
3093 return this_cu->v.quick->file_names;
3094 }
3095
3096 /* A helper for the "quick" functions which computes and caches the
3097 real path for a given file name from the line table. */
3098
3099 static const char *
3100 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3101 struct quick_file_names *qfn, int index)
3102 {
3103 if (qfn->real_names == NULL)
3104 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3105 qfn->num_file_names, const char *);
3106
3107 if (qfn->real_names[index] == NULL)
3108 {
3109 const char *dirname = nullptr;
3110
3111 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
3112 dirname = qfn->comp_dir;
3113
3114 gdb::unique_xmalloc_ptr<char> fullname;
3115 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
3116
3117 qfn->real_names[index] = fullname.release ();
3118 }
3119
3120 return qfn->real_names[index];
3121 }
3122
3123 struct symtab *
3124 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3125 {
3126 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3127 dwarf2_per_cu_data *dwarf_cu
3128 = per_objfile->per_bfd->all_comp_units.back ().get ();
3129 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3130
3131 if (cust == NULL)
3132 return NULL;
3133
3134 return cust->primary_filetab ();
3135 }
3136
3137 /* See read.h. */
3138
3139 void
3140 dwarf2_per_cu_data::free_cached_file_names ()
3141 {
3142 if (per_bfd == nullptr || !per_bfd->using_index || v.quick == nullptr)
3143 return;
3144
3145 struct quick_file_names *file_data = v.quick->file_names;
3146 if (file_data != nullptr && file_data->real_names != nullptr)
3147 {
3148 for (int i = 0; i < file_data->num_file_names; ++i)
3149 {
3150 xfree ((void *) file_data->real_names[i]);
3151 file_data->real_names[i] = nullptr;
3152 }
3153 }
3154 }
3155
3156 void
3157 dwarf2_base_index_functions::forget_cached_source_info
3158 (struct objfile *objfile)
3159 {
3160 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3161
3162 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
3163 per_cu->free_cached_file_names ();
3164 }
3165
3166 /* Struct used to manage iterating over all CUs looking for a symbol. */
3167
3168 struct dw2_symtab_iterator
3169 {
3170 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3171 dwarf2_per_objfile *per_objfile;
3172 /* If set, only look for symbols that match that block. Valid values are
3173 GLOBAL_BLOCK and STATIC_BLOCK. */
3174 gdb::optional<block_enum> block_index;
3175 /* The kind of symbol we're looking for. */
3176 domain_enum domain;
3177 /* The list of CUs from the index entry of the symbol,
3178 or NULL if not found. */
3179 offset_view vec;
3180 /* The next element in VEC to look at. */
3181 int next;
3182 /* The number of elements in VEC, or zero if there is no match. */
3183 int length;
3184 /* Have we seen a global version of the symbol?
3185 If so we can ignore all further global instances.
3186 This is to work around gold/15646, inefficient gold-generated
3187 indices. */
3188 int global_seen;
3189 };
3190
3191 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3192
3193 static void
3194 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3195 dwarf2_per_objfile *per_objfile,
3196 gdb::optional<block_enum> block_index,
3197 domain_enum domain, offset_type namei)
3198 {
3199 iter->per_objfile = per_objfile;
3200 iter->block_index = block_index;
3201 iter->domain = domain;
3202 iter->next = 0;
3203 iter->global_seen = 0;
3204 iter->vec = {};
3205 iter->length = 0;
3206
3207 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3208 /* index is NULL if OBJF_READNOW. */
3209 if (index == NULL)
3210 return;
3211
3212 gdb_assert (!index->symbol_name_slot_invalid (namei));
3213 offset_type vec_idx = index->symbol_vec_index (namei);
3214
3215 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3216 iter->length = iter->vec[0];
3217 }
3218
3219 /* Return the next matching CU or NULL if there are no more. */
3220
3221 static struct dwarf2_per_cu_data *
3222 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3223 {
3224 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3225
3226 for ( ; iter->next < iter->length; ++iter->next)
3227 {
3228 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3229 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3230 gdb_index_symbol_kind symbol_kind =
3231 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3232 /* Only check the symbol attributes if they're present.
3233 Indices prior to version 7 don't record them,
3234 and indices >= 7 may elide them for certain symbols
3235 (gold does this). */
3236 int attrs_valid =
3237 (per_objfile->per_bfd->index_table->version >= 7
3238 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3239
3240 /* Don't crash on bad data. */
3241 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3242 {
3243 complaint (_(".gdb_index entry has bad CU index"
3244 " [in module %s]"), objfile_name (per_objfile->objfile));
3245 continue;
3246 }
3247
3248 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3249
3250 /* Skip if already read in. */
3251 if (per_objfile->symtab_set_p (per_cu))
3252 continue;
3253
3254 /* Check static vs global. */
3255 if (attrs_valid)
3256 {
3257 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3258
3259 if (iter->block_index.has_value ())
3260 {
3261 bool want_static = *iter->block_index == STATIC_BLOCK;
3262
3263 if (is_static != want_static)
3264 continue;
3265 }
3266
3267 /* Work around gold/15646. */
3268 if (!is_static
3269 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3270 {
3271 if (iter->global_seen)
3272 continue;
3273
3274 iter->global_seen = 1;
3275 }
3276 }
3277
3278 /* Only check the symbol's kind if it has one. */
3279 if (attrs_valid)
3280 {
3281 switch (iter->domain)
3282 {
3283 case VAR_DOMAIN:
3284 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3285 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3286 /* Some types are also in VAR_DOMAIN. */
3287 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3288 continue;
3289 break;
3290 case STRUCT_DOMAIN:
3291 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3292 continue;
3293 break;
3294 case LABEL_DOMAIN:
3295 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3296 continue;
3297 break;
3298 case MODULE_DOMAIN:
3299 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3300 continue;
3301 break;
3302 default:
3303 break;
3304 }
3305 }
3306
3307 ++iter->next;
3308 return per_cu;
3309 }
3310
3311 return NULL;
3312 }
3313
3314 void
3315 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3316 bool print_bcache)
3317 {
3318 if (print_bcache)
3319 return;
3320
3321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3322 int total = per_objfile->per_bfd->all_comp_units.size ();
3323 int count = 0;
3324
3325 for (int i = 0; i < total; ++i)
3326 {
3327 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3328
3329 if (!per_objfile->symtab_set_p (per_cu))
3330 ++count;
3331 }
3332 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3333 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3334 }
3335
3336 /* This dumps minimal information about the index.
3337 It is called via "mt print objfiles".
3338 One use is to verify .gdb_index has been loaded by the
3339 gdb.dwarf2/gdb-index.exp testcase. */
3340
3341 void
3342 dwarf2_gdb_index::dump (struct objfile *objfile)
3343 {
3344 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3345
3346 gdb_assert (per_objfile->per_bfd->using_index);
3347 gdb_printf (".gdb_index:");
3348 if (per_objfile->per_bfd->index_table != NULL)
3349 {
3350 gdb_printf (" version %d\n",
3351 per_objfile->per_bfd->index_table->version);
3352 }
3353 else
3354 gdb_printf (" faked for \"readnow\"\n");
3355 gdb_printf ("\n");
3356 }
3357
3358 void
3359 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3360 {
3361 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3362 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3363
3364 for (int i = 0; i < total_units; ++i)
3365 {
3366 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3367
3368 /* We don't want to directly expand a partial CU, because if we
3369 read it with the wrong language, then assertion failures can
3370 be triggered later on. See PR symtab/23010. So, tell
3371 dw2_instantiate_symtab to skip partial CUs -- any important
3372 partial CU will be read via DW_TAG_imported_unit anyway. */
3373 dw2_instantiate_symtab (per_cu, per_objfile, true);
3374 }
3375 }
3376
3377 static bool
3378 dw2_expand_symtabs_matching_symbol
3379 (mapped_index_base &index,
3380 const lookup_name_info &lookup_name_in,
3381 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3382 gdb::function_view<bool (offset_type)> match_callback,
3383 dwarf2_per_objfile *per_objfile);
3384
3385 static bool
3386 dw2_expand_symtabs_matching_one
3387 (dwarf2_per_cu_data *per_cu,
3388 dwarf2_per_objfile *per_objfile,
3389 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3390 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3391
3392 void
3393 dwarf2_gdb_index::expand_matching_symbols
3394 (struct objfile *objfile,
3395 const lookup_name_info &name, domain_enum domain,
3396 int global,
3397 symbol_compare_ftype *ordered_compare)
3398 {
3399 /* Used for Ada. */
3400 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3401
3402 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3403
3404 if (per_objfile->per_bfd->index_table != nullptr)
3405 {
3406 mapped_index &index = *per_objfile->per_bfd->index_table;
3407
3408 const char *match_name = name.ada ().lookup_name ().c_str ();
3409 auto matcher = [&] (const char *symname)
3410 {
3411 if (ordered_compare == nullptr)
3412 return true;
3413 return ordered_compare (symname, match_name) == 0;
3414 };
3415
3416 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3417 [&] (offset_type namei)
3418 {
3419 struct dw2_symtab_iterator iter;
3420 struct dwarf2_per_cu_data *per_cu;
3421
3422 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3423 namei);
3424 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3425 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3426 nullptr);
3427 return true;
3428 }, per_objfile);
3429 }
3430 else
3431 {
3432 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3433 proceed assuming all symtabs have been read in. */
3434 }
3435 }
3436
3437 /* Starting from a search name, return the string that finds the upper
3438 bound of all strings that start with SEARCH_NAME in a sorted name
3439 list. Returns the empty string to indicate that the upper bound is
3440 the end of the list. */
3441
3442 static std::string
3443 make_sort_after_prefix_name (const char *search_name)
3444 {
3445 /* When looking to complete "func", we find the upper bound of all
3446 symbols that start with "func" by looking for where we'd insert
3447 the closest string that would follow "func" in lexicographical
3448 order. Usually, that's "func"-with-last-character-incremented,
3449 i.e. "fund". Mind non-ASCII characters, though. Usually those
3450 will be UTF-8 multi-byte sequences, but we can't be certain.
3451 Especially mind the 0xff character, which is a valid character in
3452 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3453 rule out compilers allowing it in identifiers. Note that
3454 conveniently, strcmp/strcasecmp are specified to compare
3455 characters interpreted as unsigned char. So what we do is treat
3456 the whole string as a base 256 number composed of a sequence of
3457 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3458 to 0, and carries 1 to the following more-significant position.
3459 If the very first character in SEARCH_NAME ends up incremented
3460 and carries/overflows, then the upper bound is the end of the
3461 list. The string after the empty string is also the empty
3462 string.
3463
3464 Some examples of this operation:
3465
3466 SEARCH_NAME => "+1" RESULT
3467
3468 "abc" => "abd"
3469 "ab\xff" => "ac"
3470 "\xff" "a" "\xff" => "\xff" "b"
3471 "\xff" => ""
3472 "\xff\xff" => ""
3473 "" => ""
3474
3475 Then, with these symbols for example:
3476
3477 func
3478 func1
3479 fund
3480
3481 completing "func" looks for symbols between "func" and
3482 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3483 which finds "func" and "func1", but not "fund".
3484
3485 And with:
3486
3487 funcÿ (Latin1 'ÿ' [0xff])
3488 funcÿ1
3489 fund
3490
3491 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3492 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3493
3494 And with:
3495
3496 ÿÿ (Latin1 'ÿ' [0xff])
3497 ÿÿ1
3498
3499 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3500 the end of the list.
3501 */
3502 std::string after = search_name;
3503 while (!after.empty () && (unsigned char) after.back () == 0xff)
3504 after.pop_back ();
3505 if (!after.empty ())
3506 after.back () = (unsigned char) after.back () + 1;
3507 return after;
3508 }
3509
3510 /* See declaration. */
3511
3512 std::pair<std::vector<name_component>::const_iterator,
3513 std::vector<name_component>::const_iterator>
3514 mapped_index_base::find_name_components_bounds
3515 (const lookup_name_info &lookup_name_without_params, language lang,
3516 dwarf2_per_objfile *per_objfile) const
3517 {
3518 auto *name_cmp
3519 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3520
3521 const char *lang_name
3522 = lookup_name_without_params.language_lookup_name (lang);
3523
3524 /* Comparison function object for lower_bound that matches against a
3525 given symbol name. */
3526 auto lookup_compare_lower = [&] (const name_component &elem,
3527 const char *name)
3528 {
3529 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3530 const char *elem_name = elem_qualified + elem.name_offset;
3531 return name_cmp (elem_name, name) < 0;
3532 };
3533
3534 /* Comparison function object for upper_bound that matches against a
3535 given symbol name. */
3536 auto lookup_compare_upper = [&] (const char *name,
3537 const name_component &elem)
3538 {
3539 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3540 const char *elem_name = elem_qualified + elem.name_offset;
3541 return name_cmp (name, elem_name) < 0;
3542 };
3543
3544 auto begin = this->name_components.begin ();
3545 auto end = this->name_components.end ();
3546
3547 /* Find the lower bound. */
3548 auto lower = [&] ()
3549 {
3550 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3551 return begin;
3552 else
3553 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3554 } ();
3555
3556 /* Find the upper bound. */
3557 auto upper = [&] ()
3558 {
3559 if (lookup_name_without_params.completion_mode ())
3560 {
3561 /* In completion mode, we want UPPER to point past all
3562 symbols names that have the same prefix. I.e., with
3563 these symbols, and completing "func":
3564
3565 function << lower bound
3566 function1
3567 other_function << upper bound
3568
3569 We find the upper bound by looking for the insertion
3570 point of "func"-with-last-character-incremented,
3571 i.e. "fund". */
3572 std::string after = make_sort_after_prefix_name (lang_name);
3573 if (after.empty ())
3574 return end;
3575 return std::lower_bound (lower, end, after.c_str (),
3576 lookup_compare_lower);
3577 }
3578 else
3579 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3580 } ();
3581
3582 return {lower, upper};
3583 }
3584
3585 /* See declaration. */
3586
3587 void
3588 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3589 {
3590 if (!this->name_components.empty ())
3591 return;
3592
3593 this->name_components_casing = case_sensitivity;
3594 auto *name_cmp
3595 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3596
3597 /* The code below only knows how to break apart components of C++
3598 symbol names (and other languages that use '::' as
3599 namespace/module separator) and Ada symbol names. */
3600 auto count = this->symbol_name_count ();
3601 for (offset_type idx = 0; idx < count; idx++)
3602 {
3603 if (this->symbol_name_slot_invalid (idx))
3604 continue;
3605
3606 const char *name = this->symbol_name_at (idx, per_objfile);
3607
3608 /* Add each name component to the name component table. */
3609 unsigned int previous_len = 0;
3610
3611 if (strstr (name, "::") != nullptr)
3612 {
3613 for (unsigned int current_len = cp_find_first_component (name);
3614 name[current_len] != '\0';
3615 current_len += cp_find_first_component (name + current_len))
3616 {
3617 gdb_assert (name[current_len] == ':');
3618 this->name_components.push_back ({previous_len, idx});
3619 /* Skip the '::'. */
3620 current_len += 2;
3621 previous_len = current_len;
3622 }
3623 }
3624 else
3625 {
3626 /* Handle the Ada encoded (aka mangled) form here. */
3627 for (const char *iter = strstr (name, "__");
3628 iter != nullptr;
3629 iter = strstr (iter, "__"))
3630 {
3631 this->name_components.push_back ({previous_len, idx});
3632 iter += 2;
3633 previous_len = iter - name;
3634 }
3635 }
3636
3637 this->name_components.push_back ({previous_len, idx});
3638 }
3639
3640 /* Sort name_components elements by name. */
3641 auto name_comp_compare = [&] (const name_component &left,
3642 const name_component &right)
3643 {
3644 const char *left_qualified
3645 = this->symbol_name_at (left.idx, per_objfile);
3646 const char *right_qualified
3647 = this->symbol_name_at (right.idx, per_objfile);
3648
3649 const char *left_name = left_qualified + left.name_offset;
3650 const char *right_name = right_qualified + right.name_offset;
3651
3652 return name_cmp (left_name, right_name) < 0;
3653 };
3654
3655 std::sort (this->name_components.begin (),
3656 this->name_components.end (),
3657 name_comp_compare);
3658 }
3659
3660 /* Helper for dw2_expand_symtabs_matching that works with a
3661 mapped_index_base instead of the containing objfile. This is split
3662 to a separate function in order to be able to unit test the
3663 name_components matching using a mock mapped_index_base. For each
3664 symbol name that matches, calls MATCH_CALLBACK, passing it the
3665 symbol's index in the mapped_index_base symbol table. */
3666
3667 static bool
3668 dw2_expand_symtabs_matching_symbol
3669 (mapped_index_base &index,
3670 const lookup_name_info &lookup_name_in,
3671 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3672 gdb::function_view<bool (offset_type)> match_callback,
3673 dwarf2_per_objfile *per_objfile)
3674 {
3675 lookup_name_info lookup_name_without_params
3676 = lookup_name_in.make_ignore_params ();
3677
3678 /* Build the symbol name component sorted vector, if we haven't
3679 yet. */
3680 index.build_name_components (per_objfile);
3681
3682 /* The same symbol may appear more than once in the range though.
3683 E.g., if we're looking for symbols that complete "w", and we have
3684 a symbol named "w1::w2", we'll find the two name components for
3685 that same symbol in the range. To be sure we only call the
3686 callback once per symbol, we first collect the symbol name
3687 indexes that matched in a temporary vector and ignore
3688 duplicates. */
3689 std::vector<offset_type> matches;
3690
3691 struct name_and_matcher
3692 {
3693 symbol_name_matcher_ftype *matcher;
3694 const char *name;
3695
3696 bool operator== (const name_and_matcher &other) const
3697 {
3698 return matcher == other.matcher && strcmp (name, other.name) == 0;
3699 }
3700 };
3701
3702 /* A vector holding all the different symbol name matchers, for all
3703 languages. */
3704 std::vector<name_and_matcher> matchers;
3705
3706 for (int i = 0; i < nr_languages; i++)
3707 {
3708 enum language lang_e = (enum language) i;
3709
3710 const language_defn *lang = language_def (lang_e);
3711 symbol_name_matcher_ftype *name_matcher
3712 = lang->get_symbol_name_matcher (lookup_name_without_params);
3713
3714 name_and_matcher key {
3715 name_matcher,
3716 lookup_name_without_params.language_lookup_name (lang_e)
3717 };
3718
3719 /* Don't insert the same comparison routine more than once.
3720 Note that we do this linear walk. This is not a problem in
3721 practice because the number of supported languages is
3722 low. */
3723 if (std::find (matchers.begin (), matchers.end (), key)
3724 != matchers.end ())
3725 continue;
3726 matchers.push_back (std::move (key));
3727
3728 auto bounds
3729 = index.find_name_components_bounds (lookup_name_without_params,
3730 lang_e, per_objfile);
3731
3732 /* Now for each symbol name in range, check to see if we have a name
3733 match, and if so, call the MATCH_CALLBACK callback. */
3734
3735 for (; bounds.first != bounds.second; ++bounds.first)
3736 {
3737 const char *qualified
3738 = index.symbol_name_at (bounds.first->idx, per_objfile);
3739
3740 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3741 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3742 continue;
3743
3744 matches.push_back (bounds.first->idx);
3745 }
3746 }
3747
3748 std::sort (matches.begin (), matches.end ());
3749
3750 /* Finally call the callback, once per match. */
3751 ULONGEST prev = -1;
3752 bool result = true;
3753 for (offset_type idx : matches)
3754 {
3755 if (prev != idx)
3756 {
3757 if (!match_callback (idx))
3758 {
3759 result = false;
3760 break;
3761 }
3762 prev = idx;
3763 }
3764 }
3765
3766 /* Above we use a type wider than idx's for 'prev', since 0 and
3767 (offset_type)-1 are both possible values. */
3768 static_assert (sizeof (prev) > sizeof (offset_type), "");
3769
3770 return result;
3771 }
3772
3773 #if GDB_SELF_TEST
3774
3775 namespace selftests { namespace dw2_expand_symtabs_matching {
3776
3777 /* A mock .gdb_index/.debug_names-like name index table, enough to
3778 exercise dw2_expand_symtabs_matching_symbol, which works with the
3779 mapped_index_base interface. Builds an index from the symbol list
3780 passed as parameter to the constructor. */
3781 class mock_mapped_index : public mapped_index_base
3782 {
3783 public:
3784 mock_mapped_index (gdb::array_view<const char *> symbols)
3785 : m_symbol_table (symbols)
3786 {}
3787
3788 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3789
3790 /* Return the number of names in the symbol table. */
3791 size_t symbol_name_count () const override
3792 {
3793 return m_symbol_table.size ();
3794 }
3795
3796 /* Get the name of the symbol at IDX in the symbol table. */
3797 const char *symbol_name_at
3798 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3799 {
3800 return m_symbol_table[idx];
3801 }
3802
3803 private:
3804 gdb::array_view<const char *> m_symbol_table;
3805 };
3806
3807 /* Convenience function that converts a NULL pointer to a "<null>"
3808 string, to pass to print routines. */
3809
3810 static const char *
3811 string_or_null (const char *str)
3812 {
3813 return str != NULL ? str : "<null>";
3814 }
3815
3816 /* Check if a lookup_name_info built from
3817 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3818 index. EXPECTED_LIST is the list of expected matches, in expected
3819 matching order. If no match expected, then an empty list is
3820 specified. Returns true on success. On failure prints a warning
3821 indicating the file:line that failed, and returns false. */
3822
3823 static bool
3824 check_match (const char *file, int line,
3825 mock_mapped_index &mock_index,
3826 const char *name, symbol_name_match_type match_type,
3827 bool completion_mode,
3828 std::initializer_list<const char *> expected_list,
3829 dwarf2_per_objfile *per_objfile)
3830 {
3831 lookup_name_info lookup_name (name, match_type, completion_mode);
3832
3833 bool matched = true;
3834
3835 auto mismatch = [&] (const char *expected_str,
3836 const char *got)
3837 {
3838 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3839 "expected=\"%s\", got=\"%s\"\n"),
3840 file, line,
3841 (match_type == symbol_name_match_type::FULL
3842 ? "FULL" : "WILD"),
3843 name, string_or_null (expected_str), string_or_null (got));
3844 matched = false;
3845 };
3846
3847 auto expected_it = expected_list.begin ();
3848 auto expected_end = expected_list.end ();
3849
3850 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3851 nullptr,
3852 [&] (offset_type idx)
3853 {
3854 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3855 const char *expected_str
3856 = expected_it == expected_end ? NULL : *expected_it++;
3857
3858 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3859 mismatch (expected_str, matched_name);
3860 return true;
3861 }, per_objfile);
3862
3863 const char *expected_str
3864 = expected_it == expected_end ? NULL : *expected_it++;
3865 if (expected_str != NULL)
3866 mismatch (expected_str, NULL);
3867
3868 return matched;
3869 }
3870
3871 /* The symbols added to the mock mapped_index for testing (in
3872 canonical form). */
3873 static const char *test_symbols[] = {
3874 "function",
3875 "std::bar",
3876 "std::zfunction",
3877 "std::zfunction2",
3878 "w1::w2",
3879 "ns::foo<char*>",
3880 "ns::foo<int>",
3881 "ns::foo<long>",
3882 "ns2::tmpl<int>::foo2",
3883 "(anonymous namespace)::A::B::C",
3884
3885 /* These are used to check that the increment-last-char in the
3886 matching algorithm for completion doesn't match "t1_fund" when
3887 completing "t1_func". */
3888 "t1_func",
3889 "t1_func1",
3890 "t1_fund",
3891 "t1_fund1",
3892
3893 /* A UTF-8 name with multi-byte sequences to make sure that
3894 cp-name-parser understands this as a single identifier ("função"
3895 is "function" in PT). */
3896 u8"u8função",
3897
3898 /* \377 (0xff) is Latin1 'ÿ'. */
3899 "yfunc\377",
3900
3901 /* \377 (0xff) is Latin1 'ÿ'. */
3902 "\377",
3903 "\377\377123",
3904
3905 /* A name with all sorts of complications. Starts with "z" to make
3906 it easier for the completion tests below. */
3907 #define Z_SYM_NAME \
3908 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3909 "::tuple<(anonymous namespace)::ui*, " \
3910 "std::default_delete<(anonymous namespace)::ui>, void>"
3911
3912 Z_SYM_NAME
3913 };
3914
3915 /* Returns true if the mapped_index_base::find_name_component_bounds
3916 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3917 in completion mode. */
3918
3919 static bool
3920 check_find_bounds_finds (mapped_index_base &index,
3921 const char *search_name,
3922 gdb::array_view<const char *> expected_syms,
3923 dwarf2_per_objfile *per_objfile)
3924 {
3925 lookup_name_info lookup_name (search_name,
3926 symbol_name_match_type::FULL, true);
3927
3928 auto bounds = index.find_name_components_bounds (lookup_name,
3929 language_cplus,
3930 per_objfile);
3931
3932 size_t distance = std::distance (bounds.first, bounds.second);
3933 if (distance != expected_syms.size ())
3934 return false;
3935
3936 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3937 {
3938 auto nc_elem = bounds.first + exp_elem;
3939 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3940 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3941 return false;
3942 }
3943
3944 return true;
3945 }
3946
3947 /* Test the lower-level mapped_index::find_name_component_bounds
3948 method. */
3949
3950 static void
3951 test_mapped_index_find_name_component_bounds ()
3952 {
3953 mock_mapped_index mock_index (test_symbols);
3954
3955 mock_index.build_name_components (NULL /* per_objfile */);
3956
3957 /* Test the lower-level mapped_index::find_name_component_bounds
3958 method in completion mode. */
3959 {
3960 static const char *expected_syms[] = {
3961 "t1_func",
3962 "t1_func1",
3963 };
3964
3965 SELF_CHECK (check_find_bounds_finds
3966 (mock_index, "t1_func", expected_syms,
3967 NULL /* per_objfile */));
3968 }
3969
3970 /* Check that the increment-last-char in the name matching algorithm
3971 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3972 {
3973 static const char *expected_syms1[] = {
3974 "\377",
3975 "\377\377123",
3976 };
3977 SELF_CHECK (check_find_bounds_finds
3978 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3979
3980 static const char *expected_syms2[] = {
3981 "\377\377123",
3982 };
3983 SELF_CHECK (check_find_bounds_finds
3984 (mock_index, "\377\377", expected_syms2,
3985 NULL /* per_objfile */));
3986 }
3987 }
3988
3989 /* Test dw2_expand_symtabs_matching_symbol. */
3990
3991 static void
3992 test_dw2_expand_symtabs_matching_symbol ()
3993 {
3994 mock_mapped_index mock_index (test_symbols);
3995
3996 /* We let all tests run until the end even if some fails, for debug
3997 convenience. */
3998 bool any_mismatch = false;
3999
4000 /* Create the expected symbols list (an initializer_list). Needed
4001 because lists have commas, and we need to pass them to CHECK,
4002 which is a macro. */
4003 #define EXPECT(...) { __VA_ARGS__ }
4004
4005 /* Wrapper for check_match that passes down the current
4006 __FILE__/__LINE__. */
4007 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4008 any_mismatch |= !check_match (__FILE__, __LINE__, \
4009 mock_index, \
4010 NAME, MATCH_TYPE, COMPLETION_MODE, \
4011 EXPECTED_LIST, NULL)
4012
4013 /* Identity checks. */
4014 for (const char *sym : test_symbols)
4015 {
4016 /* Should be able to match all existing symbols. */
4017 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4018 EXPECT (sym));
4019
4020 /* Should be able to match all existing symbols with
4021 parameters. */
4022 std::string with_params = std::string (sym) + "(int)";
4023 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4024 EXPECT (sym));
4025
4026 /* Should be able to match all existing symbols with
4027 parameters and qualifiers. */
4028 with_params = std::string (sym) + " ( int ) const";
4029 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4030 EXPECT (sym));
4031
4032 /* This should really find sym, but cp-name-parser.y doesn't
4033 know about lvalue/rvalue qualifiers yet. */
4034 with_params = std::string (sym) + " ( int ) &&";
4035 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4036 {});
4037 }
4038
4039 /* Check that the name matching algorithm for completion doesn't get
4040 confused with Latin1 'ÿ' / 0xff. */
4041 {
4042 static const char str[] = "\377";
4043 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4044 EXPECT ("\377", "\377\377123"));
4045 }
4046
4047 /* Check that the increment-last-char in the matching algorithm for
4048 completion doesn't match "t1_fund" when completing "t1_func". */
4049 {
4050 static const char str[] = "t1_func";
4051 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4052 EXPECT ("t1_func", "t1_func1"));
4053 }
4054
4055 /* Check that completion mode works at each prefix of the expected
4056 symbol name. */
4057 {
4058 static const char str[] = "function(int)";
4059 size_t len = strlen (str);
4060 std::string lookup;
4061
4062 for (size_t i = 1; i < len; i++)
4063 {
4064 lookup.assign (str, i);
4065 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4066 EXPECT ("function"));
4067 }
4068 }
4069
4070 /* While "w" is a prefix of both components, the match function
4071 should still only be called once. */
4072 {
4073 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4074 EXPECT ("w1::w2"));
4075 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4076 EXPECT ("w1::w2"));
4077 }
4078
4079 /* Same, with a "complicated" symbol. */
4080 {
4081 static const char str[] = Z_SYM_NAME;
4082 size_t len = strlen (str);
4083 std::string lookup;
4084
4085 for (size_t i = 1; i < len; i++)
4086 {
4087 lookup.assign (str, i);
4088 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4089 EXPECT (Z_SYM_NAME));
4090 }
4091 }
4092
4093 /* In FULL mode, an incomplete symbol doesn't match. */
4094 {
4095 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4096 {});
4097 }
4098
4099 /* A complete symbol with parameters matches any overload, since the
4100 index has no overload info. */
4101 {
4102 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4103 EXPECT ("std::zfunction", "std::zfunction2"));
4104 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4105 EXPECT ("std::zfunction", "std::zfunction2"));
4106 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4107 EXPECT ("std::zfunction", "std::zfunction2"));
4108 }
4109
4110 /* Check that whitespace is ignored appropriately. A symbol with a
4111 template argument list. */
4112 {
4113 static const char expected[] = "ns::foo<int>";
4114 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4115 EXPECT (expected));
4116 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4117 EXPECT (expected));
4118 }
4119
4120 /* Check that whitespace is ignored appropriately. A symbol with a
4121 template argument list that includes a pointer. */
4122 {
4123 static const char expected[] = "ns::foo<char*>";
4124 /* Try both completion and non-completion modes. */
4125 static const bool completion_mode[2] = {false, true};
4126 for (size_t i = 0; i < 2; i++)
4127 {
4128 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4129 completion_mode[i], EXPECT (expected));
4130 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4131 completion_mode[i], EXPECT (expected));
4132
4133 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4134 completion_mode[i], EXPECT (expected));
4135 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4136 completion_mode[i], EXPECT (expected));
4137 }
4138 }
4139
4140 {
4141 /* Check method qualifiers are ignored. */
4142 static const char expected[] = "ns::foo<char*>";
4143 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4144 symbol_name_match_type::FULL, true, EXPECT (expected));
4145 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4146 symbol_name_match_type::FULL, true, EXPECT (expected));
4147 CHECK_MATCH ("foo < char * > ( int ) const",
4148 symbol_name_match_type::WILD, true, EXPECT (expected));
4149 CHECK_MATCH ("foo < char * > ( int ) &&",
4150 symbol_name_match_type::WILD, true, EXPECT (expected));
4151 }
4152
4153 /* Test lookup names that don't match anything. */
4154 {
4155 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4156 {});
4157
4158 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4159 {});
4160 }
4161
4162 /* Some wild matching tests, exercising "(anonymous namespace)",
4163 which should not be confused with a parameter list. */
4164 {
4165 static const char *syms[] = {
4166 "A::B::C",
4167 "B::C",
4168 "C",
4169 "A :: B :: C ( int )",
4170 "B :: C ( int )",
4171 "C ( int )",
4172 };
4173
4174 for (const char *s : syms)
4175 {
4176 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4177 EXPECT ("(anonymous namespace)::A::B::C"));
4178 }
4179 }
4180
4181 {
4182 static const char expected[] = "ns2::tmpl<int>::foo2";
4183 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4184 EXPECT (expected));
4185 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4186 EXPECT (expected));
4187 }
4188
4189 SELF_CHECK (!any_mismatch);
4190
4191 #undef EXPECT
4192 #undef CHECK_MATCH
4193 }
4194
4195 static void
4196 run_test ()
4197 {
4198 test_mapped_index_find_name_component_bounds ();
4199 test_dw2_expand_symtabs_matching_symbol ();
4200 }
4201
4202 }} // namespace selftests::dw2_expand_symtabs_matching
4203
4204 #endif /* GDB_SELF_TEST */
4205
4206 /* If FILE_MATCHER is NULL or if PER_CU has
4207 dwarf2_per_cu_quick_data::MARK set (see
4208 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4209 EXPANSION_NOTIFY on it. */
4210
4211 static bool
4212 dw2_expand_symtabs_matching_one
4213 (dwarf2_per_cu_data *per_cu,
4214 dwarf2_per_objfile *per_objfile,
4215 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4216 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4217 {
4218 if (file_matcher == NULL || per_cu->v.quick->mark)
4219 {
4220 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4221
4222 compunit_symtab *symtab
4223 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4224 gdb_assert (symtab != nullptr);
4225
4226 if (expansion_notify != NULL && symtab_was_null)
4227 return expansion_notify (symtab);
4228 }
4229 return true;
4230 }
4231
4232 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4233 matched, to expand corresponding CUs that were marked. IDX is the
4234 index of the symbol name that matched. */
4235
4236 static bool
4237 dw2_expand_marked_cus
4238 (dwarf2_per_objfile *per_objfile, offset_type idx,
4239 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4240 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4241 block_search_flags search_flags,
4242 search_domain kind)
4243 {
4244 offset_type vec_len, vec_idx;
4245 bool global_seen = false;
4246 mapped_index &index = *per_objfile->per_bfd->index_table;
4247
4248 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4249 vec_len = vec[0];
4250 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4251 {
4252 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4253 /* This value is only valid for index versions >= 7. */
4254 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4255 gdb_index_symbol_kind symbol_kind =
4256 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4257 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4258 /* Only check the symbol attributes if they're present.
4259 Indices prior to version 7 don't record them,
4260 and indices >= 7 may elide them for certain symbols
4261 (gold does this). */
4262 int attrs_valid =
4263 (index.version >= 7
4264 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4265
4266 /* Work around gold/15646. */
4267 if (attrs_valid
4268 && !is_static
4269 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4270 {
4271 if (global_seen)
4272 continue;
4273
4274 global_seen = true;
4275 }
4276
4277 /* Only check the symbol's kind if it has one. */
4278 if (attrs_valid)
4279 {
4280 if (is_static)
4281 {
4282 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4283 continue;
4284 }
4285 else
4286 {
4287 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4288 continue;
4289 }
4290
4291 switch (kind)
4292 {
4293 case VARIABLES_DOMAIN:
4294 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4295 continue;
4296 break;
4297 case FUNCTIONS_DOMAIN:
4298 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4299 continue;
4300 break;
4301 case TYPES_DOMAIN:
4302 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4303 continue;
4304 break;
4305 case MODULES_DOMAIN:
4306 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4307 continue;
4308 break;
4309 default:
4310 break;
4311 }
4312 }
4313
4314 /* Don't crash on bad data. */
4315 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4316 {
4317 complaint (_(".gdb_index entry has bad CU index"
4318 " [in module %s]"), objfile_name (per_objfile->objfile));
4319 continue;
4320 }
4321
4322 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4323 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4324 expansion_notify))
4325 return false;
4326 }
4327
4328 return true;
4329 }
4330
4331 /* If FILE_MATCHER is non-NULL, set all the
4332 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4333 that match FILE_MATCHER. */
4334
4335 static void
4336 dw_expand_symtabs_matching_file_matcher
4337 (dwarf2_per_objfile *per_objfile,
4338 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4339 {
4340 if (file_matcher == NULL)
4341 return;
4342
4343 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4344 htab_eq_pointer,
4345 NULL, xcalloc, xfree));
4346 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4347 htab_eq_pointer,
4348 NULL, xcalloc, xfree));
4349
4350 /* The rule is CUs specify all the files, including those used by
4351 any TU, so there's no need to scan TUs here. */
4352
4353 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4354 {
4355 QUIT;
4356
4357 if (per_cu->is_debug_types)
4358 continue;
4359 per_cu->v.quick->mark = 0;
4360
4361 /* We only need to look at symtabs not already expanded. */
4362 if (per_objfile->symtab_set_p (per_cu.get ()))
4363 continue;
4364
4365 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4366 per_objfile);
4367 if (file_data == NULL)
4368 continue;
4369
4370 if (htab_find (visited_not_found.get (), file_data) != NULL)
4371 continue;
4372 else if (htab_find (visited_found.get (), file_data) != NULL)
4373 {
4374 per_cu->v.quick->mark = 1;
4375 continue;
4376 }
4377
4378 for (int j = 0; j < file_data->num_file_names; ++j)
4379 {
4380 const char *this_real_name;
4381
4382 if (file_matcher (file_data->file_names[j], false))
4383 {
4384 per_cu->v.quick->mark = 1;
4385 break;
4386 }
4387
4388 /* Before we invoke realpath, which can get expensive when many
4389 files are involved, do a quick comparison of the basenames. */
4390 if (!basenames_may_differ
4391 && !file_matcher (lbasename (file_data->file_names[j]),
4392 true))
4393 continue;
4394
4395 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4396 if (file_matcher (this_real_name, false))
4397 {
4398 per_cu->v.quick->mark = 1;
4399 break;
4400 }
4401 }
4402
4403 void **slot = htab_find_slot (per_cu->v.quick->mark
4404 ? visited_found.get ()
4405 : visited_not_found.get (),
4406 file_data, INSERT);
4407 *slot = file_data;
4408 }
4409 }
4410
4411 bool
4412 dwarf2_gdb_index::expand_symtabs_matching
4413 (struct objfile *objfile,
4414 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4415 const lookup_name_info *lookup_name,
4416 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4417 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4418 block_search_flags search_flags,
4419 domain_enum domain,
4420 enum search_domain kind)
4421 {
4422 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4423
4424 /* index_table is NULL if OBJF_READNOW. */
4425 if (!per_objfile->per_bfd->index_table)
4426 return true;
4427
4428 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4429
4430 /* This invariant is documented in quick-functions.h. */
4431 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4432 if (lookup_name == nullptr)
4433 {
4434 for (dwarf2_per_cu_data *per_cu
4435 : all_comp_units_range (per_objfile->per_bfd))
4436 {
4437 QUIT;
4438
4439 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4440 file_matcher,
4441 expansion_notify))
4442 return false;
4443 }
4444 return true;
4445 }
4446
4447 mapped_index &index = *per_objfile->per_bfd->index_table;
4448
4449 bool result
4450 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4451 symbol_matcher,
4452 [&] (offset_type idx)
4453 {
4454 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4455 expansion_notify, search_flags, kind))
4456 return false;
4457 return true;
4458 }, per_objfile);
4459
4460 return result;
4461 }
4462
4463 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4464 symtab. */
4465
4466 static struct compunit_symtab *
4467 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4468 CORE_ADDR pc)
4469 {
4470 int i;
4471
4472 if (cust->blockvector () != nullptr
4473 && blockvector_contains_pc (cust->blockvector (), pc))
4474 return cust;
4475
4476 if (cust->includes == NULL)
4477 return NULL;
4478
4479 for (i = 0; cust->includes[i]; ++i)
4480 {
4481 struct compunit_symtab *s = cust->includes[i];
4482
4483 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4484 if (s != NULL)
4485 return s;
4486 }
4487
4488 return NULL;
4489 }
4490
4491 struct compunit_symtab *
4492 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4493 (struct objfile *objfile,
4494 struct bound_minimal_symbol msymbol,
4495 CORE_ADDR pc,
4496 struct obj_section *section,
4497 int warn_if_readin)
4498 {
4499 struct dwarf2_per_cu_data *data;
4500 struct compunit_symtab *result;
4501
4502 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4503 if (per_objfile->per_bfd->index_addrmap == nullptr)
4504 return NULL;
4505
4506 CORE_ADDR baseaddr = objfile->text_section_offset ();
4507 data = ((struct dwarf2_per_cu_data *)
4508 addrmap_find (per_objfile->per_bfd->index_addrmap,
4509 pc - baseaddr));
4510 if (!data)
4511 return NULL;
4512
4513 if (warn_if_readin && per_objfile->symtab_set_p (data))
4514 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4515 paddress (objfile->arch (), pc));
4516
4517 result = recursively_find_pc_sect_compunit_symtab
4518 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4519
4520 gdb_assert (result != NULL);
4521 return result;
4522 }
4523
4524 void
4525 dwarf2_base_index_functions::map_symbol_filenames
4526 (struct objfile *objfile,
4527 gdb::function_view<symbol_filename_ftype> fun,
4528 bool need_fullname)
4529 {
4530 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4531
4532 /* Use caches to ensure we only call FUN once for each filename. */
4533 filename_seen_cache filenames_cache;
4534 std::unordered_set<quick_file_names *> qfn_cache;
4535
4536 /* The rule is CUs specify all the files, including those used by any TU,
4537 so there's no need to scan TUs here. We can ignore file names coming
4538 from already-expanded CUs. It is possible that an expanded CU might
4539 reuse the file names data from a currently unexpanded CU, in this
4540 case we don't want to report the files from the unexpanded CU. */
4541
4542 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4543 {
4544 if (!per_cu->is_debug_types
4545 && per_objfile->symtab_set_p (per_cu.get ()))
4546 {
4547 if (per_cu->v.quick->file_names != nullptr)
4548 qfn_cache.insert (per_cu->v.quick->file_names);
4549 }
4550 }
4551
4552 for (dwarf2_per_cu_data *per_cu
4553 : all_comp_units_range (per_objfile->per_bfd))
4554 {
4555 /* We only need to look at symtabs not already expanded. */
4556 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4557 continue;
4558
4559 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4560 if (file_data == nullptr
4561 || qfn_cache.find (file_data) != qfn_cache.end ())
4562 continue;
4563
4564 for (int j = 0; j < file_data->num_file_names; ++j)
4565 {
4566 const char *filename = file_data->file_names[j];
4567 const char *key = filename;
4568 const char *fullname = nullptr;
4569
4570 if (need_fullname)
4571 {
4572 fullname = dw2_get_real_path (per_objfile, file_data, j);
4573 key = fullname;
4574 }
4575
4576 if (!filenames_cache.seen (key))
4577 fun (filename, fullname);
4578 }
4579 }
4580 }
4581
4582 bool
4583 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4584 {
4585 return true;
4586 }
4587
4588 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4589
4590 bool
4591 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4592 {
4593 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4594
4595 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4596 {
4597 /* Is this already expanded? */
4598 if (per_objfile->symtab_set_p (per_cu.get ()))
4599 continue;
4600
4601 /* It has not yet been expanded. */
4602 return true;
4603 }
4604
4605 return false;
4606 }
4607
4608 /* DWARF-5 debug_names reader. */
4609
4610 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4611 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4612
4613 /* A helper function that reads the .debug_names section in SECTION
4614 and fills in MAP. FILENAME is the name of the file containing the
4615 section; it is used for error reporting.
4616
4617 Returns true if all went well, false otherwise. */
4618
4619 static bool
4620 read_debug_names_from_section (struct objfile *objfile,
4621 const char *filename,
4622 struct dwarf2_section_info *section,
4623 mapped_debug_names &map)
4624 {
4625 if (section->empty ())
4626 return false;
4627
4628 /* Older elfutils strip versions could keep the section in the main
4629 executable while splitting it for the separate debug info file. */
4630 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4631 return false;
4632
4633 section->read (objfile);
4634
4635 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4636
4637 const gdb_byte *addr = section->buffer;
4638
4639 bfd *const abfd = section->get_bfd_owner ();
4640
4641 unsigned int bytes_read;
4642 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4643 addr += bytes_read;
4644
4645 map.dwarf5_is_dwarf64 = bytes_read != 4;
4646 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4647 if (bytes_read + length != section->size)
4648 {
4649 /* There may be multiple per-CU indices. */
4650 warning (_("Section .debug_names in %s length %s does not match "
4651 "section length %s, ignoring .debug_names."),
4652 filename, plongest (bytes_read + length),
4653 pulongest (section->size));
4654 return false;
4655 }
4656
4657 /* The version number. */
4658 uint16_t version = read_2_bytes (abfd, addr);
4659 addr += 2;
4660 if (version != 5)
4661 {
4662 warning (_("Section .debug_names in %s has unsupported version %d, "
4663 "ignoring .debug_names."),
4664 filename, version);
4665 return false;
4666 }
4667
4668 /* Padding. */
4669 uint16_t padding = read_2_bytes (abfd, addr);
4670 addr += 2;
4671 if (padding != 0)
4672 {
4673 warning (_("Section .debug_names in %s has unsupported padding %d, "
4674 "ignoring .debug_names."),
4675 filename, padding);
4676 return false;
4677 }
4678
4679 /* comp_unit_count - The number of CUs in the CU list. */
4680 map.cu_count = read_4_bytes (abfd, addr);
4681 addr += 4;
4682
4683 /* local_type_unit_count - The number of TUs in the local TU
4684 list. */
4685 map.tu_count = read_4_bytes (abfd, addr);
4686 addr += 4;
4687
4688 /* foreign_type_unit_count - The number of TUs in the foreign TU
4689 list. */
4690 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4691 addr += 4;
4692 if (foreign_tu_count != 0)
4693 {
4694 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4695 "ignoring .debug_names."),
4696 filename, static_cast<unsigned long> (foreign_tu_count));
4697 return false;
4698 }
4699
4700 /* bucket_count - The number of hash buckets in the hash lookup
4701 table. */
4702 map.bucket_count = read_4_bytes (abfd, addr);
4703 addr += 4;
4704
4705 /* name_count - The number of unique names in the index. */
4706 map.name_count = read_4_bytes (abfd, addr);
4707 addr += 4;
4708
4709 /* abbrev_table_size - The size in bytes of the abbreviations
4710 table. */
4711 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4712 addr += 4;
4713
4714 /* augmentation_string_size - The size in bytes of the augmentation
4715 string. This value is rounded up to a multiple of 4. */
4716 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4717 addr += 4;
4718 map.augmentation_is_gdb = ((augmentation_string_size
4719 == sizeof (dwarf5_augmentation))
4720 && memcmp (addr, dwarf5_augmentation,
4721 sizeof (dwarf5_augmentation)) == 0);
4722 augmentation_string_size += (-augmentation_string_size) & 3;
4723 addr += augmentation_string_size;
4724
4725 /* List of CUs */
4726 map.cu_table_reordered = addr;
4727 addr += map.cu_count * map.offset_size;
4728
4729 /* List of Local TUs */
4730 map.tu_table_reordered = addr;
4731 addr += map.tu_count * map.offset_size;
4732
4733 /* Hash Lookup Table */
4734 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4735 addr += map.bucket_count * 4;
4736 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4737 addr += map.name_count * 4;
4738
4739 /* Name Table */
4740 map.name_table_string_offs_reordered = addr;
4741 addr += map.name_count * map.offset_size;
4742 map.name_table_entry_offs_reordered = addr;
4743 addr += map.name_count * map.offset_size;
4744
4745 const gdb_byte *abbrev_table_start = addr;
4746 for (;;)
4747 {
4748 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4749 addr += bytes_read;
4750 if (index_num == 0)
4751 break;
4752
4753 const auto insertpair
4754 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4755 if (!insertpair.second)
4756 {
4757 warning (_("Section .debug_names in %s has duplicate index %s, "
4758 "ignoring .debug_names."),
4759 filename, pulongest (index_num));
4760 return false;
4761 }
4762 mapped_debug_names::index_val &indexval = insertpair.first->second;
4763 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4764 addr += bytes_read;
4765
4766 for (;;)
4767 {
4768 mapped_debug_names::index_val::attr attr;
4769 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4770 addr += bytes_read;
4771 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4772 addr += bytes_read;
4773 if (attr.form == DW_FORM_implicit_const)
4774 {
4775 attr.implicit_const = read_signed_leb128 (abfd, addr,
4776 &bytes_read);
4777 addr += bytes_read;
4778 }
4779 if (attr.dw_idx == 0 && attr.form == 0)
4780 break;
4781 indexval.attr_vec.push_back (std::move (attr));
4782 }
4783 }
4784 if (addr != abbrev_table_start + abbrev_table_size)
4785 {
4786 warning (_("Section .debug_names in %s has abbreviation_table "
4787 "of size %s vs. written as %u, ignoring .debug_names."),
4788 filename, plongest (addr - abbrev_table_start),
4789 abbrev_table_size);
4790 return false;
4791 }
4792 map.entry_pool = addr;
4793
4794 return true;
4795 }
4796
4797 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4798 list. */
4799
4800 static void
4801 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4802 const mapped_debug_names &map,
4803 dwarf2_section_info &section,
4804 bool is_dwz)
4805 {
4806 if (!map.augmentation_is_gdb)
4807 {
4808 for (uint32_t i = 0; i < map.cu_count; ++i)
4809 {
4810 sect_offset sect_off
4811 = (sect_offset) (extract_unsigned_integer
4812 (map.cu_table_reordered + i * map.offset_size,
4813 map.offset_size,
4814 map.dwarf5_byte_order));
4815 /* We don't know the length of the CU, because the CU list in a
4816 .debug_names index can be incomplete, so we can't use the start
4817 of the next CU as end of this CU. We create the CUs here with
4818 length 0, and in cutu_reader::cutu_reader we'll fill in the
4819 actual length. */
4820 dwarf2_per_cu_data_up per_cu
4821 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4822 sect_off, 0);
4823 per_bfd->all_comp_units.push_back (std::move (per_cu));
4824 }
4825 return;
4826 }
4827
4828 sect_offset sect_off_prev;
4829 for (uint32_t i = 0; i <= map.cu_count; ++i)
4830 {
4831 sect_offset sect_off_next;
4832 if (i < map.cu_count)
4833 {
4834 sect_off_next
4835 = (sect_offset) (extract_unsigned_integer
4836 (map.cu_table_reordered + i * map.offset_size,
4837 map.offset_size,
4838 map.dwarf5_byte_order));
4839 }
4840 else
4841 sect_off_next = (sect_offset) section.size;
4842 if (i >= 1)
4843 {
4844 const ULONGEST length = sect_off_next - sect_off_prev;
4845 dwarf2_per_cu_data_up per_cu
4846 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4847 sect_off_prev, length);
4848 per_bfd->all_comp_units.push_back (std::move (per_cu));
4849 }
4850 sect_off_prev = sect_off_next;
4851 }
4852 }
4853
4854 /* Read the CU list from the mapped index, and use it to create all
4855 the CU objects for this dwarf2_per_objfile. */
4856
4857 static void
4858 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4859 const mapped_debug_names &map,
4860 const mapped_debug_names &dwz_map)
4861 {
4862 gdb_assert (per_bfd->all_comp_units.empty ());
4863 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4864
4865 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4866 false /* is_dwz */);
4867
4868 if (dwz_map.cu_count == 0)
4869 return;
4870
4871 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4872 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4873 true /* is_dwz */);
4874 }
4875
4876 /* Read .debug_names. If everything went ok, initialize the "quick"
4877 elements of all the CUs and return true. Otherwise, return false. */
4878
4879 static bool
4880 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4881 {
4882 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4883 mapped_debug_names dwz_map;
4884 struct objfile *objfile = per_objfile->objfile;
4885 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4886
4887 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4888 &per_bfd->debug_names, *map))
4889 return false;
4890
4891 /* Don't use the index if it's empty. */
4892 if (map->name_count == 0)
4893 return false;
4894
4895 /* If there is a .dwz file, read it so we can get its CU list as
4896 well. */
4897 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4898 if (dwz != NULL)
4899 {
4900 if (!read_debug_names_from_section (objfile,
4901 bfd_get_filename (dwz->dwz_bfd.get ()),
4902 &dwz->debug_names, dwz_map))
4903 {
4904 warning (_("could not read '.debug_names' section from %s; skipping"),
4905 bfd_get_filename (dwz->dwz_bfd.get ()));
4906 return false;
4907 }
4908 }
4909
4910 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4911
4912 if (map->tu_count != 0)
4913 {
4914 /* We can only handle a single .debug_types when we have an
4915 index. */
4916 if (per_bfd->types.size () != 1)
4917 return false;
4918
4919 dwarf2_section_info *section = &per_bfd->types[0];
4920
4921 create_signatured_type_table_from_debug_names
4922 (per_objfile, *map, section, &per_bfd->abbrev);
4923 }
4924
4925 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4926
4927 per_bfd->debug_names_table = std::move (map);
4928 per_bfd->using_index = 1;
4929 per_bfd->quick_file_names_table =
4930 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4931
4932 return true;
4933 }
4934
4935 /* Type used to manage iterating over all CUs looking for a symbol for
4936 .debug_names. */
4937
4938 class dw2_debug_names_iterator
4939 {
4940 public:
4941 dw2_debug_names_iterator (const mapped_debug_names &map,
4942 block_search_flags block_index,
4943 domain_enum domain,
4944 const char *name, dwarf2_per_objfile *per_objfile)
4945 : m_map (map), m_block_index (block_index), m_domain (domain),
4946 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4947 m_per_objfile (per_objfile)
4948 {}
4949
4950 dw2_debug_names_iterator (const mapped_debug_names &map,
4951 search_domain search, uint32_t namei,
4952 dwarf2_per_objfile *per_objfile,
4953 domain_enum domain = UNDEF_DOMAIN)
4954 : m_map (map),
4955 m_domain (domain),
4956 m_search (search),
4957 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4958 m_per_objfile (per_objfile)
4959 {}
4960
4961 dw2_debug_names_iterator (const mapped_debug_names &map,
4962 block_search_flags block_index, domain_enum domain,
4963 uint32_t namei, dwarf2_per_objfile *per_objfile)
4964 : m_map (map), m_block_index (block_index), m_domain (domain),
4965 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4966 m_per_objfile (per_objfile)
4967 {}
4968
4969 /* Return the next matching CU or NULL if there are no more. */
4970 dwarf2_per_cu_data *next ();
4971
4972 private:
4973 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4974 const char *name,
4975 dwarf2_per_objfile *per_objfile);
4976 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4977 uint32_t namei,
4978 dwarf2_per_objfile *per_objfile);
4979
4980 /* The internalized form of .debug_names. */
4981 const mapped_debug_names &m_map;
4982
4983 /* Restrict the search to these blocks. */
4984 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4985 | SEARCH_STATIC_BLOCK);
4986
4987 /* The kind of symbol we're looking for. */
4988 const domain_enum m_domain = UNDEF_DOMAIN;
4989 const search_domain m_search = ALL_DOMAIN;
4990
4991 /* The list of CUs from the index entry of the symbol, or NULL if
4992 not found. */
4993 const gdb_byte *m_addr;
4994
4995 dwarf2_per_objfile *m_per_objfile;
4996 };
4997
4998 const char *
4999 mapped_debug_names::namei_to_name
5000 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5001 {
5002 const ULONGEST namei_string_offs
5003 = extract_unsigned_integer ((name_table_string_offs_reordered
5004 + namei * offset_size),
5005 offset_size,
5006 dwarf5_byte_order);
5007 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5008 }
5009
5010 /* Find a slot in .debug_names for the object named NAME. If NAME is
5011 found, return pointer to its pool data. If NAME cannot be found,
5012 return NULL. */
5013
5014 const gdb_byte *
5015 dw2_debug_names_iterator::find_vec_in_debug_names
5016 (const mapped_debug_names &map, const char *name,
5017 dwarf2_per_objfile *per_objfile)
5018 {
5019 int (*cmp) (const char *, const char *);
5020
5021 gdb::unique_xmalloc_ptr<char> without_params;
5022 if (current_language->la_language == language_cplus
5023 || current_language->la_language == language_fortran
5024 || current_language->la_language == language_d)
5025 {
5026 /* NAME is already canonical. Drop any qualifiers as
5027 .debug_names does not contain any. */
5028
5029 if (strchr (name, '(') != NULL)
5030 {
5031 without_params = cp_remove_params (name);
5032 if (without_params != NULL)
5033 name = without_params.get ();
5034 }
5035 }
5036
5037 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5038
5039 const uint32_t full_hash = dwarf5_djb_hash (name);
5040 uint32_t namei
5041 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5042 (map.bucket_table_reordered
5043 + (full_hash % map.bucket_count)), 4,
5044 map.dwarf5_byte_order);
5045 if (namei == 0)
5046 return NULL;
5047 --namei;
5048 if (namei >= map.name_count)
5049 {
5050 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5051 "[in module %s]"),
5052 namei, map.name_count,
5053 objfile_name (per_objfile->objfile));
5054 return NULL;
5055 }
5056
5057 for (;;)
5058 {
5059 const uint32_t namei_full_hash
5060 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5061 (map.hash_table_reordered + namei), 4,
5062 map.dwarf5_byte_order);
5063 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5064 return NULL;
5065
5066 if (full_hash == namei_full_hash)
5067 {
5068 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5069
5070 #if 0 /* An expensive sanity check. */
5071 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5072 {
5073 complaint (_("Wrong .debug_names hash for string at index %u "
5074 "[in module %s]"),
5075 namei, objfile_name (dwarf2_per_objfile->objfile));
5076 return NULL;
5077 }
5078 #endif
5079
5080 if (cmp (namei_string, name) == 0)
5081 {
5082 const ULONGEST namei_entry_offs
5083 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5084 + namei * map.offset_size),
5085 map.offset_size, map.dwarf5_byte_order);
5086 return map.entry_pool + namei_entry_offs;
5087 }
5088 }
5089
5090 ++namei;
5091 if (namei >= map.name_count)
5092 return NULL;
5093 }
5094 }
5095
5096 const gdb_byte *
5097 dw2_debug_names_iterator::find_vec_in_debug_names
5098 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5099 {
5100 if (namei >= map.name_count)
5101 {
5102 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5103 "[in module %s]"),
5104 namei, map.name_count,
5105 objfile_name (per_objfile->objfile));
5106 return NULL;
5107 }
5108
5109 const ULONGEST namei_entry_offs
5110 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5111 + namei * map.offset_size),
5112 map.offset_size, map.dwarf5_byte_order);
5113 return map.entry_pool + namei_entry_offs;
5114 }
5115
5116 /* See dw2_debug_names_iterator. */
5117
5118 dwarf2_per_cu_data *
5119 dw2_debug_names_iterator::next ()
5120 {
5121 if (m_addr == NULL)
5122 return NULL;
5123
5124 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5125 struct objfile *objfile = m_per_objfile->objfile;
5126 bfd *const abfd = objfile->obfd;
5127
5128 again:
5129
5130 unsigned int bytes_read;
5131 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5132 m_addr += bytes_read;
5133 if (abbrev == 0)
5134 return NULL;
5135
5136 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5137 if (indexval_it == m_map.abbrev_map.cend ())
5138 {
5139 complaint (_("Wrong .debug_names undefined abbrev code %s "
5140 "[in module %s]"),
5141 pulongest (abbrev), objfile_name (objfile));
5142 return NULL;
5143 }
5144 const mapped_debug_names::index_val &indexval = indexval_it->second;
5145 enum class symbol_linkage {
5146 unknown,
5147 static_,
5148 extern_,
5149 } symbol_linkage_ = symbol_linkage::unknown;
5150 dwarf2_per_cu_data *per_cu = NULL;
5151 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5152 {
5153 ULONGEST ull;
5154 switch (attr.form)
5155 {
5156 case DW_FORM_implicit_const:
5157 ull = attr.implicit_const;
5158 break;
5159 case DW_FORM_flag_present:
5160 ull = 1;
5161 break;
5162 case DW_FORM_udata:
5163 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5164 m_addr += bytes_read;
5165 break;
5166 case DW_FORM_ref4:
5167 ull = read_4_bytes (abfd, m_addr);
5168 m_addr += 4;
5169 break;
5170 case DW_FORM_ref8:
5171 ull = read_8_bytes (abfd, m_addr);
5172 m_addr += 8;
5173 break;
5174 case DW_FORM_ref_sig8:
5175 ull = read_8_bytes (abfd, m_addr);
5176 m_addr += 8;
5177 break;
5178 default:
5179 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5180 dwarf_form_name (attr.form),
5181 objfile_name (objfile));
5182 return NULL;
5183 }
5184 switch (attr.dw_idx)
5185 {
5186 case DW_IDX_compile_unit:
5187 /* Don't crash on bad data. */
5188 if (ull >= per_bfd->all_comp_units.size ())
5189 {
5190 complaint (_(".debug_names entry has bad CU index %s"
5191 " [in module %s]"),
5192 pulongest (ull),
5193 objfile_name (objfile));
5194 continue;
5195 }
5196 per_cu = per_bfd->get_cu (ull);
5197 break;
5198 case DW_IDX_type_unit:
5199 /* Don't crash on bad data. */
5200 if (ull >= per_bfd->tu_stats.nr_tus)
5201 {
5202 complaint (_(".debug_names entry has bad TU index %s"
5203 " [in module %s]"),
5204 pulongest (ull),
5205 objfile_name (objfile));
5206 continue;
5207 }
5208 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5209 break;
5210 case DW_IDX_die_offset:
5211 /* In a per-CU index (as opposed to a per-module index), index
5212 entries without CU attribute implicitly refer to the single CU. */
5213 if (per_cu == NULL)
5214 per_cu = per_bfd->get_cu (0);
5215 break;
5216 case DW_IDX_GNU_internal:
5217 if (!m_map.augmentation_is_gdb)
5218 break;
5219 symbol_linkage_ = symbol_linkage::static_;
5220 break;
5221 case DW_IDX_GNU_external:
5222 if (!m_map.augmentation_is_gdb)
5223 break;
5224 symbol_linkage_ = symbol_linkage::extern_;
5225 break;
5226 }
5227 }
5228
5229 /* Skip if already read in. */
5230 if (m_per_objfile->symtab_set_p (per_cu))
5231 goto again;
5232
5233 /* Check static vs global. */
5234 if (symbol_linkage_ != symbol_linkage::unknown)
5235 {
5236 if (symbol_linkage_ == symbol_linkage::static_)
5237 {
5238 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5239 goto again;
5240 }
5241 else
5242 {
5243 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5244 goto again;
5245 }
5246 }
5247
5248 /* Match dw2_symtab_iter_next, symbol_kind
5249 and debug_names::psymbol_tag. */
5250 switch (m_domain)
5251 {
5252 case VAR_DOMAIN:
5253 switch (indexval.dwarf_tag)
5254 {
5255 case DW_TAG_variable:
5256 case DW_TAG_subprogram:
5257 /* Some types are also in VAR_DOMAIN. */
5258 case DW_TAG_typedef:
5259 case DW_TAG_structure_type:
5260 break;
5261 default:
5262 goto again;
5263 }
5264 break;
5265 case STRUCT_DOMAIN:
5266 switch (indexval.dwarf_tag)
5267 {
5268 case DW_TAG_typedef:
5269 case DW_TAG_structure_type:
5270 break;
5271 default:
5272 goto again;
5273 }
5274 break;
5275 case LABEL_DOMAIN:
5276 switch (indexval.dwarf_tag)
5277 {
5278 case 0:
5279 case DW_TAG_variable:
5280 break;
5281 default:
5282 goto again;
5283 }
5284 break;
5285 case MODULE_DOMAIN:
5286 switch (indexval.dwarf_tag)
5287 {
5288 case DW_TAG_module:
5289 break;
5290 default:
5291 goto again;
5292 }
5293 break;
5294 default:
5295 break;
5296 }
5297
5298 /* Match dw2_expand_symtabs_matching, symbol_kind and
5299 debug_names::psymbol_tag. */
5300 switch (m_search)
5301 {
5302 case VARIABLES_DOMAIN:
5303 switch (indexval.dwarf_tag)
5304 {
5305 case DW_TAG_variable:
5306 break;
5307 default:
5308 goto again;
5309 }
5310 break;
5311 case FUNCTIONS_DOMAIN:
5312 switch (indexval.dwarf_tag)
5313 {
5314 case DW_TAG_subprogram:
5315 break;
5316 default:
5317 goto again;
5318 }
5319 break;
5320 case TYPES_DOMAIN:
5321 switch (indexval.dwarf_tag)
5322 {
5323 case DW_TAG_typedef:
5324 case DW_TAG_structure_type:
5325 break;
5326 default:
5327 goto again;
5328 }
5329 break;
5330 case MODULES_DOMAIN:
5331 switch (indexval.dwarf_tag)
5332 {
5333 case DW_TAG_module:
5334 break;
5335 default:
5336 goto again;
5337 }
5338 default:
5339 break;
5340 }
5341
5342 return per_cu;
5343 }
5344
5345 /* This dumps minimal information about .debug_names. It is called
5346 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5347 uses this to verify that .debug_names has been loaded. */
5348
5349 void
5350 dwarf2_debug_names_index::dump (struct objfile *objfile)
5351 {
5352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5353
5354 gdb_assert (per_objfile->per_bfd->using_index);
5355 gdb_printf (".debug_names:");
5356 if (per_objfile->per_bfd->debug_names_table)
5357 gdb_printf (" exists\n");
5358 else
5359 gdb_printf (" faked for \"readnow\"\n");
5360 gdb_printf ("\n");
5361 }
5362
5363 void
5364 dwarf2_debug_names_index::expand_matching_symbols
5365 (struct objfile *objfile,
5366 const lookup_name_info &name, domain_enum domain,
5367 int global,
5368 symbol_compare_ftype *ordered_compare)
5369 {
5370 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5371
5372 /* debug_names_table is NULL if OBJF_READNOW. */
5373 if (!per_objfile->per_bfd->debug_names_table)
5374 return;
5375
5376 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5377 const block_search_flags block_flags
5378 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5379
5380 const char *match_name = name.ada ().lookup_name ().c_str ();
5381 auto matcher = [&] (const char *symname)
5382 {
5383 if (ordered_compare == nullptr)
5384 return true;
5385 return ordered_compare (symname, match_name) == 0;
5386 };
5387
5388 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5389 [&] (offset_type namei)
5390 {
5391 /* The name was matched, now expand corresponding CUs that were
5392 marked. */
5393 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5394 per_objfile);
5395
5396 struct dwarf2_per_cu_data *per_cu;
5397 while ((per_cu = iter.next ()) != NULL)
5398 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5399 nullptr);
5400 return true;
5401 }, per_objfile);
5402 }
5403
5404 bool
5405 dwarf2_debug_names_index::expand_symtabs_matching
5406 (struct objfile *objfile,
5407 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5408 const lookup_name_info *lookup_name,
5409 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5410 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5411 block_search_flags search_flags,
5412 domain_enum domain,
5413 enum search_domain kind)
5414 {
5415 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5416
5417 /* debug_names_table is NULL if OBJF_READNOW. */
5418 if (!per_objfile->per_bfd->debug_names_table)
5419 return true;
5420
5421 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5422
5423 /* This invariant is documented in quick-functions.h. */
5424 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5425 if (lookup_name == nullptr)
5426 {
5427 for (dwarf2_per_cu_data *per_cu
5428 : all_comp_units_range (per_objfile->per_bfd))
5429 {
5430 QUIT;
5431
5432 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5433 file_matcher,
5434 expansion_notify))
5435 return false;
5436 }
5437 return true;
5438 }
5439
5440 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5441
5442 bool result
5443 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5444 symbol_matcher,
5445 [&] (offset_type namei)
5446 {
5447 /* The name was matched, now expand corresponding CUs that were
5448 marked. */
5449 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5450
5451 struct dwarf2_per_cu_data *per_cu;
5452 while ((per_cu = iter.next ()) != NULL)
5453 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5454 file_matcher,
5455 expansion_notify))
5456 return false;
5457 return true;
5458 }, per_objfile);
5459
5460 return result;
5461 }
5462
5463 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5464 to either a dwarf2_per_bfd or dwz_file object. */
5465
5466 template <typename T>
5467 static gdb::array_view<const gdb_byte>
5468 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5469 {
5470 dwarf2_section_info *section = &section_owner->gdb_index;
5471
5472 if (section->empty ())
5473 return {};
5474
5475 /* Older elfutils strip versions could keep the section in the main
5476 executable while splitting it for the separate debug info file. */
5477 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5478 return {};
5479
5480 section->read (obj);
5481
5482 /* dwarf2_section_info::size is a bfd_size_type, while
5483 gdb::array_view works with size_t. On 32-bit hosts, with
5484 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5485 is 32-bit. So we need an explicit narrowing conversion here.
5486 This is fine, because it's impossible to allocate or mmap an
5487 array/buffer larger than what size_t can represent. */
5488 return gdb::make_array_view (section->buffer, section->size);
5489 }
5490
5491 /* Lookup the index cache for the contents of the index associated to
5492 DWARF2_OBJ. */
5493
5494 static gdb::array_view<const gdb_byte>
5495 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5496 {
5497 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5498 if (build_id == nullptr)
5499 return {};
5500
5501 return global_index_cache.lookup_gdb_index (build_id,
5502 &dwarf2_per_bfd->index_cache_res);
5503 }
5504
5505 /* Same as the above, but for DWZ. */
5506
5507 static gdb::array_view<const gdb_byte>
5508 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5509 {
5510 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5511 if (build_id == nullptr)
5512 return {};
5513
5514 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5515 }
5516
5517 /* See dwarf2/public.h. */
5518
5519 void
5520 dwarf2_initialize_objfile (struct objfile *objfile)
5521 {
5522 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5523 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5524
5525 dwarf_read_debug_printf ("called");
5526
5527 /* If we're about to read full symbols, don't bother with the
5528 indices. In this case we also don't care if some other debug
5529 format is making psymtabs, because they are all about to be
5530 expanded anyway. */
5531 if ((objfile->flags & OBJF_READNOW))
5532 {
5533 dwarf_read_debug_printf ("readnow requested");
5534
5535 /* When using READNOW, the using_index flag (set below) indicates that
5536 PER_BFD was already initialized, when we loaded some other objfile. */
5537 if (per_bfd->using_index)
5538 {
5539 dwarf_read_debug_printf ("using_index already set");
5540 objfile->qf.push_front (make_dwarf_gdb_index ());
5541 return;
5542 }
5543
5544 per_bfd->using_index = 1;
5545 create_all_comp_units (per_objfile);
5546 per_bfd->quick_file_names_table
5547 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5548
5549 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5550 {
5551 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5552
5553 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5554 struct dwarf2_per_cu_quick_data);
5555 }
5556
5557 /* Arrange for gdb to see the "quick" functions. However, these
5558 functions will be no-ops because we will have expanded all
5559 symtabs. */
5560 objfile->qf.push_front (make_dwarf_gdb_index ());
5561 return;
5562 }
5563
5564 /* Was a debug names index already read when we processed an objfile sharing
5565 PER_BFD? */
5566 if (per_bfd->debug_names_table != nullptr)
5567 {
5568 dwarf_read_debug_printf ("re-using shared debug names table");
5569 objfile->qf.push_front (make_dwarf_debug_names ());
5570 return;
5571 }
5572
5573 /* Was a GDB index already read when we processed an objfile sharing
5574 PER_BFD? */
5575 if (per_bfd->index_table != nullptr)
5576 {
5577 dwarf_read_debug_printf ("re-using shared index table");
5578 objfile->qf.push_front (make_dwarf_gdb_index ());
5579 return;
5580 }
5581
5582 /* There might already be partial symtabs built for this BFD. This happens
5583 when loading the same binary twice with the index-cache enabled. If so,
5584 don't try to read an index. The objfile / per_objfile initialization will
5585 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5586 code path. */
5587 if (per_bfd->partial_symtabs != nullptr)
5588 {
5589 dwarf_read_debug_printf ("re-using shared partial symtabs");
5590 objfile->qf.push_front (make_lazy_dwarf_reader ());
5591 return;
5592 }
5593
5594 if (dwarf2_read_debug_names (per_objfile))
5595 {
5596 dwarf_read_debug_printf ("found debug names");
5597 objfile->qf.push_front (make_dwarf_debug_names ());
5598 return;
5599 }
5600
5601 if (dwarf2_read_gdb_index (per_objfile,
5602 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5603 get_gdb_index_contents_from_section<dwz_file>))
5604 {
5605 dwarf_read_debug_printf ("found gdb index from file");
5606 objfile->qf.push_front (make_dwarf_gdb_index ());
5607 return;
5608 }
5609
5610 /* ... otherwise, try to find the index in the index cache. */
5611 if (dwarf2_read_gdb_index (per_objfile,
5612 get_gdb_index_contents_from_cache,
5613 get_gdb_index_contents_from_cache_dwz))
5614 {
5615 dwarf_read_debug_printf ("found gdb index from cache");
5616 global_index_cache.hit ();
5617 objfile->qf.push_front (make_dwarf_gdb_index ());
5618 return;
5619 }
5620
5621 global_index_cache.miss ();
5622 objfile->qf.push_front (make_lazy_dwarf_reader ());
5623 }
5624
5625 \f
5626
5627 /* Build a partial symbol table. */
5628
5629 void
5630 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5631 {
5632 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5633 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5634
5635 if (per_bfd->partial_symtabs != nullptr)
5636 {
5637 /* Partial symbols were already read, so now we can simply
5638 attach them. */
5639 if (psf == nullptr)
5640 {
5641 psf = new psymbol_functions (per_bfd->partial_symtabs);
5642 objfile->qf.emplace_front (psf);
5643 }
5644 else
5645 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5646 return;
5647 }
5648
5649 if (psf == nullptr)
5650 {
5651 psf = new psymbol_functions;
5652 objfile->qf.emplace_front (psf);
5653 }
5654 const std::shared_ptr<psymtab_storage> &partial_symtabs
5655 = psf->get_partial_symtabs ();
5656
5657 /* Set the local reference to partial symtabs, so that we don't try
5658 to read them again if reading another objfile with the same BFD.
5659 If we can't in fact share, this won't make a difference anyway as
5660 the dwarf2_per_bfd object won't be shared. */
5661 per_bfd->partial_symtabs = partial_symtabs;
5662
5663 try
5664 {
5665 /* This isn't really ideal: all the data we allocate on the
5666 objfile's obstack is still uselessly kept around. However,
5667 freeing it seems unsafe. */
5668 psymtab_discarder psymtabs (partial_symtabs.get ());
5669 dwarf2_build_psymtabs_hard (per_objfile);
5670 psymtabs.keep ();
5671
5672 /* (maybe) store an index in the cache. */
5673 global_index_cache.store (per_objfile);
5674 }
5675 catch (const gdb_exception_error &except)
5676 {
5677 exception_print (gdb_stderr, except);
5678 }
5679 }
5680
5681 /* Find the base address of the compilation unit for range lists and
5682 location lists. It will normally be specified by DW_AT_low_pc.
5683 In DWARF-3 draft 4, the base address could be overridden by
5684 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5685 compilation units with discontinuous ranges. */
5686
5687 static void
5688 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5689 {
5690 struct attribute *attr;
5691
5692 cu->base_address.reset ();
5693
5694 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5695 if (attr != nullptr)
5696 cu->base_address = attr->as_address ();
5697 else
5698 {
5699 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5700 if (attr != nullptr)
5701 cu->base_address = attr->as_address ();
5702 }
5703 }
5704
5705 /* Helper function that returns the proper abbrev section for
5706 THIS_CU. */
5707
5708 static struct dwarf2_section_info *
5709 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5710 {
5711 struct dwarf2_section_info *abbrev;
5712 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5713
5714 if (this_cu->is_dwz)
5715 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5716 else
5717 abbrev = &per_bfd->abbrev;
5718
5719 return abbrev;
5720 }
5721
5722 /* Fetch the abbreviation table offset from a comp or type unit header. */
5723
5724 static sect_offset
5725 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5726 struct dwarf2_section_info *section,
5727 sect_offset sect_off)
5728 {
5729 bfd *abfd = section->get_bfd_owner ();
5730 const gdb_byte *info_ptr;
5731 unsigned int initial_length_size, offset_size;
5732 uint16_t version;
5733
5734 section->read (per_objfile->objfile);
5735 info_ptr = section->buffer + to_underlying (sect_off);
5736 read_initial_length (abfd, info_ptr, &initial_length_size);
5737 offset_size = initial_length_size == 4 ? 4 : 8;
5738 info_ptr += initial_length_size;
5739
5740 version = read_2_bytes (abfd, info_ptr);
5741 info_ptr += 2;
5742 if (version >= 5)
5743 {
5744 /* Skip unit type and address size. */
5745 info_ptr += 2;
5746 }
5747
5748 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5749 }
5750
5751 /* A partial symtab that is used only for include files. */
5752 struct dwarf2_include_psymtab : public partial_symtab
5753 {
5754 dwarf2_include_psymtab (const char *filename,
5755 psymtab_storage *partial_symtabs,
5756 objfile_per_bfd_storage *objfile_per_bfd)
5757 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5758 {
5759 }
5760
5761 void read_symtab (struct objfile *objfile) override
5762 {
5763 /* It's an include file, no symbols to read for it.
5764 Everything is in the includer symtab. */
5765
5766 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5767 expansion of the includer psymtab. We use the dependencies[0] field to
5768 model the includer. But if we go the regular route of calling
5769 expand_psymtab here, and having expand_psymtab call expand_dependencies
5770 to expand the includer, we'll only use expand_psymtab on the includer
5771 (making it a non-toplevel psymtab), while if we expand the includer via
5772 another path, we'll use read_symtab (making it a toplevel psymtab).
5773 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5774 psymtab, and trigger read_symtab on the includer here directly. */
5775 includer ()->read_symtab (objfile);
5776 }
5777
5778 void expand_psymtab (struct objfile *objfile) override
5779 {
5780 /* This is not called by read_symtab, and should not be called by any
5781 expand_dependencies. */
5782 gdb_assert (false);
5783 }
5784
5785 bool readin_p (struct objfile *objfile) const override
5786 {
5787 return includer ()->readin_p (objfile);
5788 }
5789
5790 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5791 {
5792 compunit_symtab *cust = includer ()->get_compunit_symtab (objfile);
5793 while (cust != nullptr && cust->user != nullptr)
5794 cust = cust->user;
5795 return cust;
5796 }
5797
5798 private:
5799 partial_symtab *includer () const
5800 {
5801 /* An include psymtab has exactly one dependency: the psymtab that
5802 includes it. */
5803 gdb_assert (this->number_of_dependencies == 1);
5804 return this->dependencies[0];
5805 }
5806 };
5807
5808 /* Allocate a new partial symtab for file named NAME and mark this new
5809 partial symtab as being an include of PST. */
5810
5811 static void
5812 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5813 const char *name,
5814 dwarf2_psymtab *pst,
5815 psymtab_storage *partial_symtabs,
5816 objfile_per_bfd_storage *objfile_per_bfd)
5817 {
5818 dwarf2_include_psymtab *subpst
5819 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5820
5821 if (!IS_ABSOLUTE_PATH (subpst->filename))
5822 subpst->dirname = pst->dirname;
5823
5824 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5825 subpst->dependencies[0] = pst;
5826 subpst->number_of_dependencies = 1;
5827 }
5828
5829 /* Read the Line Number Program data and extract the list of files
5830 included by the source file represented by PST. Build an include
5831 partial symtab for each of these included files. */
5832
5833 static void
5834 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5835 struct die_info *die,
5836 const file_and_directory &fnd,
5837 dwarf2_psymtab *pst)
5838 {
5839 line_header_up lh;
5840 struct attribute *attr;
5841
5842 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5843 if (attr != nullptr && attr->form_is_unsigned ())
5844 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5845 if (lh == NULL)
5846 return; /* No linetable, so no includes. */
5847
5848 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5849 that we pass in the raw text_low here; that is ok because we're
5850 only decoding the line table to make include partial symtabs, and
5851 so the addresses aren't really used. */
5852 dwarf_decode_lines (lh.get (), fnd, cu, pst,
5853 pst->raw_text_low (), 1);
5854 }
5855
5856 static hashval_t
5857 hash_signatured_type (const void *item)
5858 {
5859 const struct signatured_type *sig_type
5860 = (const struct signatured_type *) item;
5861
5862 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5863 return sig_type->signature;
5864 }
5865
5866 static int
5867 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5868 {
5869 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5870 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5871
5872 return lhs->signature == rhs->signature;
5873 }
5874
5875 /* Allocate a hash table for signatured types. */
5876
5877 static htab_up
5878 allocate_signatured_type_table ()
5879 {
5880 return htab_up (htab_create_alloc (41,
5881 hash_signatured_type,
5882 eq_signatured_type,
5883 NULL, xcalloc, xfree));
5884 }
5885
5886 /* A helper for create_debug_types_hash_table. Read types from SECTION
5887 and fill them into TYPES_HTAB. It will process only type units,
5888 therefore DW_UT_type. */
5889
5890 static void
5891 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5892 struct dwo_file *dwo_file,
5893 dwarf2_section_info *section, htab_up &types_htab,
5894 rcuh_kind section_kind)
5895 {
5896 struct objfile *objfile = per_objfile->objfile;
5897 struct dwarf2_section_info *abbrev_section;
5898 bfd *abfd;
5899 const gdb_byte *info_ptr, *end_ptr;
5900
5901 abbrev_section = &dwo_file->sections.abbrev;
5902
5903 dwarf_read_debug_printf ("Reading %s for %s",
5904 section->get_name (),
5905 abbrev_section->get_file_name ());
5906
5907 section->read (objfile);
5908 info_ptr = section->buffer;
5909
5910 if (info_ptr == NULL)
5911 return;
5912
5913 /* We can't set abfd until now because the section may be empty or
5914 not present, in which case the bfd is unknown. */
5915 abfd = section->get_bfd_owner ();
5916
5917 /* We don't use cutu_reader here because we don't need to read
5918 any dies: the signature is in the header. */
5919
5920 end_ptr = info_ptr + section->size;
5921 while (info_ptr < end_ptr)
5922 {
5923 signatured_type_up sig_type;
5924 struct dwo_unit *dwo_tu;
5925 void **slot;
5926 const gdb_byte *ptr = info_ptr;
5927 struct comp_unit_head header;
5928 unsigned int length;
5929
5930 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5931
5932 /* Initialize it due to a false compiler warning. */
5933 header.signature = -1;
5934 header.type_cu_offset_in_tu = (cu_offset) -1;
5935
5936 /* We need to read the type's signature in order to build the hash
5937 table, but we don't need anything else just yet. */
5938
5939 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5940 abbrev_section, ptr, section_kind);
5941
5942 length = header.get_length ();
5943
5944 /* Skip dummy type units. */
5945 if (ptr >= info_ptr + length
5946 || peek_abbrev_code (abfd, ptr) == 0
5947 || (header.unit_type != DW_UT_type
5948 && header.unit_type != DW_UT_split_type))
5949 {
5950 info_ptr += length;
5951 continue;
5952 }
5953
5954 if (types_htab == NULL)
5955 types_htab = allocate_dwo_unit_table ();
5956
5957 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5958 dwo_tu->dwo_file = dwo_file;
5959 dwo_tu->signature = header.signature;
5960 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5961 dwo_tu->section = section;
5962 dwo_tu->sect_off = sect_off;
5963 dwo_tu->length = length;
5964
5965 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5966 gdb_assert (slot != NULL);
5967 if (*slot != NULL)
5968 complaint (_("debug type entry at offset %s is duplicate to"
5969 " the entry at offset %s, signature %s"),
5970 sect_offset_str (sect_off),
5971 sect_offset_str (dwo_tu->sect_off),
5972 hex_string (header.signature));
5973 *slot = dwo_tu;
5974
5975 dwarf_read_debug_printf_v (" offset %s, signature %s",
5976 sect_offset_str (sect_off),
5977 hex_string (header.signature));
5978
5979 info_ptr += length;
5980 }
5981 }
5982
5983 /* Create the hash table of all entries in the .debug_types
5984 (or .debug_types.dwo) section(s).
5985 DWO_FILE is a pointer to the DWO file object.
5986
5987 The result is a pointer to the hash table or NULL if there are no types.
5988
5989 Note: This function processes DWO files only, not DWP files. */
5990
5991 static void
5992 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5993 struct dwo_file *dwo_file,
5994 gdb::array_view<dwarf2_section_info> type_sections,
5995 htab_up &types_htab)
5996 {
5997 for (dwarf2_section_info &section : type_sections)
5998 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5999 rcuh_kind::TYPE);
6000 }
6001
6002 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6003 If SLOT is non-NULL, it is the entry to use in the hash table.
6004 Otherwise we find one. */
6005
6006 static struct signatured_type *
6007 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6008 {
6009 if (per_objfile->per_bfd->all_comp_units.size ()
6010 == per_objfile->per_bfd->all_comp_units.capacity ())
6011 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6012
6013 signatured_type_up sig_type_holder
6014 = per_objfile->per_bfd->allocate_signatured_type (sig);
6015 signatured_type *sig_type = sig_type_holder.get ();
6016
6017 per_objfile->per_bfd->all_comp_units.emplace_back
6018 (sig_type_holder.release ());
6019 if (per_objfile->per_bfd->using_index)
6020 {
6021 sig_type->v.quick =
6022 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6023 struct dwarf2_per_cu_quick_data);
6024 }
6025
6026 if (slot == NULL)
6027 {
6028 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6029 sig_type, INSERT);
6030 }
6031 gdb_assert (*slot == NULL);
6032 *slot = sig_type;
6033 /* The rest of sig_type must be filled in by the caller. */
6034 return sig_type;
6035 }
6036
6037 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6038 Fill in SIG_ENTRY with DWO_ENTRY. */
6039
6040 static void
6041 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6042 struct signatured_type *sig_entry,
6043 struct dwo_unit *dwo_entry)
6044 {
6045 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6046
6047 /* Make sure we're not clobbering something we don't expect to. */
6048 gdb_assert (! sig_entry->queued);
6049 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6050 if (per_bfd->using_index)
6051 {
6052 gdb_assert (sig_entry->v.quick != NULL);
6053 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6054 }
6055 else
6056 gdb_assert (sig_entry->v.psymtab == NULL);
6057 gdb_assert (sig_entry->signature == dwo_entry->signature);
6058 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6059 gdb_assert (sig_entry->type_unit_group == NULL);
6060 gdb_assert (sig_entry->dwo_unit == NULL);
6061
6062 sig_entry->section = dwo_entry->section;
6063 sig_entry->sect_off = dwo_entry->sect_off;
6064 sig_entry->length = dwo_entry->length;
6065 sig_entry->reading_dwo_directly = 1;
6066 sig_entry->per_bfd = per_bfd;
6067 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6068 sig_entry->dwo_unit = dwo_entry;
6069 }
6070
6071 /* Subroutine of lookup_signatured_type.
6072 If we haven't read the TU yet, create the signatured_type data structure
6073 for a TU to be read in directly from a DWO file, bypassing the stub.
6074 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6075 using .gdb_index, then when reading a CU we want to stay in the DWO file
6076 containing that CU. Otherwise we could end up reading several other DWO
6077 files (due to comdat folding) to process the transitive closure of all the
6078 mentioned TUs, and that can be slow. The current DWO file will have every
6079 type signature that it needs.
6080 We only do this for .gdb_index because in the psymtab case we already have
6081 to read all the DWOs to build the type unit groups. */
6082
6083 static struct signatured_type *
6084 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6085 {
6086 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6087 struct dwo_file *dwo_file;
6088 struct dwo_unit find_dwo_entry, *dwo_entry;
6089 void **slot;
6090
6091 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6092
6093 /* If TU skeletons have been removed then we may not have read in any
6094 TUs yet. */
6095 if (per_objfile->per_bfd->signatured_types == NULL)
6096 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6097
6098 /* We only ever need to read in one copy of a signatured type.
6099 Use the global signatured_types array to do our own comdat-folding
6100 of types. If this is the first time we're reading this TU, and
6101 the TU has an entry in .gdb_index, replace the recorded data from
6102 .gdb_index with this TU. */
6103
6104 signatured_type find_sig_entry (sig);
6105 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6106 &find_sig_entry, INSERT);
6107 signatured_type *sig_entry = (struct signatured_type *) *slot;
6108
6109 /* We can get here with the TU already read, *or* in the process of being
6110 read. Don't reassign the global entry to point to this DWO if that's
6111 the case. Also note that if the TU is already being read, it may not
6112 have come from a DWO, the program may be a mix of Fission-compiled
6113 code and non-Fission-compiled code. */
6114
6115 /* Have we already tried to read this TU?
6116 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6117 needn't exist in the global table yet). */
6118 if (sig_entry != NULL && sig_entry->tu_read)
6119 return sig_entry;
6120
6121 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6122 dwo_unit of the TU itself. */
6123 dwo_file = cu->dwo_unit->dwo_file;
6124
6125 /* Ok, this is the first time we're reading this TU. */
6126 if (dwo_file->tus == NULL)
6127 return NULL;
6128 find_dwo_entry.signature = sig;
6129 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6130 &find_dwo_entry);
6131 if (dwo_entry == NULL)
6132 return NULL;
6133
6134 /* If the global table doesn't have an entry for this TU, add one. */
6135 if (sig_entry == NULL)
6136 sig_entry = add_type_unit (per_objfile, sig, slot);
6137
6138 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6139 sig_entry->tu_read = 1;
6140 return sig_entry;
6141 }
6142
6143 /* Subroutine of lookup_signatured_type.
6144 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6145 then try the DWP file. If the TU stub (skeleton) has been removed then
6146 it won't be in .gdb_index. */
6147
6148 static struct signatured_type *
6149 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6150 {
6151 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6152 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6153 struct dwo_unit *dwo_entry;
6154 void **slot;
6155
6156 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6157 gdb_assert (dwp_file != NULL);
6158
6159 /* If TU skeletons have been removed then we may not have read in any
6160 TUs yet. */
6161 if (per_objfile->per_bfd->signatured_types == NULL)
6162 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6163
6164 signatured_type find_sig_entry (sig);
6165 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6166 &find_sig_entry, INSERT);
6167 signatured_type *sig_entry = (struct signatured_type *) *slot;
6168
6169 /* Have we already tried to read this TU?
6170 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6171 needn't exist in the global table yet). */
6172 if (sig_entry != NULL)
6173 return sig_entry;
6174
6175 if (dwp_file->tus == NULL)
6176 return NULL;
6177 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6178 1 /* is_debug_types */);
6179 if (dwo_entry == NULL)
6180 return NULL;
6181
6182 sig_entry = add_type_unit (per_objfile, sig, slot);
6183 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6184
6185 return sig_entry;
6186 }
6187
6188 /* Lookup a signature based type for DW_FORM_ref_sig8.
6189 Returns NULL if signature SIG is not present in the table.
6190 It is up to the caller to complain about this. */
6191
6192 static struct signatured_type *
6193 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6194 {
6195 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6196
6197 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6198 {
6199 /* We're in a DWO/DWP file, and we're using .gdb_index.
6200 These cases require special processing. */
6201 if (get_dwp_file (per_objfile) == NULL)
6202 return lookup_dwo_signatured_type (cu, sig);
6203 else
6204 return lookup_dwp_signatured_type (cu, sig);
6205 }
6206 else
6207 {
6208 if (per_objfile->per_bfd->signatured_types == NULL)
6209 return NULL;
6210 signatured_type find_entry (sig);
6211 return ((struct signatured_type *)
6212 htab_find (per_objfile->per_bfd->signatured_types.get (),
6213 &find_entry));
6214 }
6215 }
6216
6217 /* Low level DIE reading support. */
6218
6219 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6220
6221 static void
6222 init_cu_die_reader (struct die_reader_specs *reader,
6223 struct dwarf2_cu *cu,
6224 struct dwarf2_section_info *section,
6225 struct dwo_file *dwo_file,
6226 struct abbrev_table *abbrev_table)
6227 {
6228 gdb_assert (section->readin && section->buffer != NULL);
6229 reader->abfd = section->get_bfd_owner ();
6230 reader->cu = cu;
6231 reader->dwo_file = dwo_file;
6232 reader->die_section = section;
6233 reader->buffer = section->buffer;
6234 reader->buffer_end = section->buffer + section->size;
6235 reader->abbrev_table = abbrev_table;
6236 }
6237
6238 /* Subroutine of cutu_reader to simplify it.
6239 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6240 There's just a lot of work to do, and cutu_reader is big enough
6241 already.
6242
6243 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6244 from it to the DIE in the DWO. If NULL we are skipping the stub.
6245 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6246 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6247 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6248 STUB_COMP_DIR may be non-NULL.
6249 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6250 are filled in with the info of the DIE from the DWO file.
6251 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6252 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6253 kept around for at least as long as *RESULT_READER.
6254
6255 The result is non-zero if a valid (non-dummy) DIE was found. */
6256
6257 static int
6258 read_cutu_die_from_dwo (dwarf2_cu *cu,
6259 struct dwo_unit *dwo_unit,
6260 struct die_info *stub_comp_unit_die,
6261 const char *stub_comp_dir,
6262 struct die_reader_specs *result_reader,
6263 const gdb_byte **result_info_ptr,
6264 struct die_info **result_comp_unit_die,
6265 abbrev_table_up *result_dwo_abbrev_table)
6266 {
6267 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6268 dwarf2_per_cu_data *per_cu = cu->per_cu;
6269 struct objfile *objfile = per_objfile->objfile;
6270 bfd *abfd;
6271 const gdb_byte *begin_info_ptr, *info_ptr;
6272 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6273 int i,num_extra_attrs;
6274 struct dwarf2_section_info *dwo_abbrev_section;
6275 struct die_info *comp_unit_die;
6276
6277 /* At most one of these may be provided. */
6278 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6279
6280 /* These attributes aren't processed until later:
6281 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6282 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6283 referenced later. However, these attributes are found in the stub
6284 which we won't have later. In order to not impose this complication
6285 on the rest of the code, we read them here and copy them to the
6286 DWO CU/TU die. */
6287
6288 stmt_list = NULL;
6289 low_pc = NULL;
6290 high_pc = NULL;
6291 ranges = NULL;
6292 comp_dir = NULL;
6293
6294 if (stub_comp_unit_die != NULL)
6295 {
6296 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6297 DWO file. */
6298 if (!per_cu->is_debug_types)
6299 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6300 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6301 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6302 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6303 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6304
6305 cu->addr_base = stub_comp_unit_die->addr_base ();
6306
6307 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6308 We need the value before we can process DW_AT_ranges values from the
6309 DWO. */
6310 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6311
6312 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6313 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6314 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6315 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6316 section. */
6317 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6318 }
6319 else if (stub_comp_dir != NULL)
6320 {
6321 /* Reconstruct the comp_dir attribute to simplify the code below. */
6322 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6323 comp_dir->name = DW_AT_comp_dir;
6324 comp_dir->form = DW_FORM_string;
6325 comp_dir->set_string_noncanonical (stub_comp_dir);
6326 }
6327
6328 /* Set up for reading the DWO CU/TU. */
6329 cu->dwo_unit = dwo_unit;
6330 dwarf2_section_info *section = dwo_unit->section;
6331 section->read (objfile);
6332 abfd = section->get_bfd_owner ();
6333 begin_info_ptr = info_ptr = (section->buffer
6334 + to_underlying (dwo_unit->sect_off));
6335 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6336
6337 if (per_cu->is_debug_types)
6338 {
6339 signatured_type *sig_type = (struct signatured_type *) per_cu;
6340
6341 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6342 section, dwo_abbrev_section,
6343 info_ptr, rcuh_kind::TYPE);
6344 /* This is not an assert because it can be caused by bad debug info. */
6345 if (sig_type->signature != cu->header.signature)
6346 {
6347 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6348 " TU at offset %s [in module %s]"),
6349 hex_string (sig_type->signature),
6350 hex_string (cu->header.signature),
6351 sect_offset_str (dwo_unit->sect_off),
6352 bfd_get_filename (abfd));
6353 }
6354 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6355 /* For DWOs coming from DWP files, we don't know the CU length
6356 nor the type's offset in the TU until now. */
6357 dwo_unit->length = cu->header.get_length ();
6358 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6359
6360 /* Establish the type offset that can be used to lookup the type.
6361 For DWO files, we don't know it until now. */
6362 sig_type->type_offset_in_section
6363 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6364 }
6365 else
6366 {
6367 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6368 section, dwo_abbrev_section,
6369 info_ptr, rcuh_kind::COMPILE);
6370 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6371 /* For DWOs coming from DWP files, we don't know the CU length
6372 until now. */
6373 dwo_unit->length = cu->header.get_length ();
6374 }
6375
6376 dwo_abbrev_section->read (objfile);
6377 *result_dwo_abbrev_table
6378 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6379 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6380 result_dwo_abbrev_table->get ());
6381
6382 /* Read in the die, but leave space to copy over the attributes
6383 from the stub. This has the benefit of simplifying the rest of
6384 the code - all the work to maintain the illusion of a single
6385 DW_TAG_{compile,type}_unit DIE is done here. */
6386 num_extra_attrs = ((stmt_list != NULL)
6387 + (low_pc != NULL)
6388 + (high_pc != NULL)
6389 + (ranges != NULL)
6390 + (comp_dir != NULL));
6391 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6392 num_extra_attrs);
6393
6394 /* Copy over the attributes from the stub to the DIE we just read in. */
6395 comp_unit_die = *result_comp_unit_die;
6396 i = comp_unit_die->num_attrs;
6397 if (stmt_list != NULL)
6398 comp_unit_die->attrs[i++] = *stmt_list;
6399 if (low_pc != NULL)
6400 comp_unit_die->attrs[i++] = *low_pc;
6401 if (high_pc != NULL)
6402 comp_unit_die->attrs[i++] = *high_pc;
6403 if (ranges != NULL)
6404 comp_unit_die->attrs[i++] = *ranges;
6405 if (comp_dir != NULL)
6406 comp_unit_die->attrs[i++] = *comp_dir;
6407 comp_unit_die->num_attrs += num_extra_attrs;
6408
6409 if (dwarf_die_debug)
6410 {
6411 gdb_printf (gdb_stdlog,
6412 "Read die from %s@0x%x of %s:\n",
6413 section->get_name (),
6414 (unsigned) (begin_info_ptr - section->buffer),
6415 bfd_get_filename (abfd));
6416 dump_die (comp_unit_die, dwarf_die_debug);
6417 }
6418
6419 /* Skip dummy compilation units. */
6420 if (info_ptr >= begin_info_ptr + dwo_unit->length
6421 || peek_abbrev_code (abfd, info_ptr) == 0)
6422 return 0;
6423
6424 *result_info_ptr = info_ptr;
6425 return 1;
6426 }
6427
6428 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6429 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6430 signature is part of the header. */
6431 static gdb::optional<ULONGEST>
6432 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6433 {
6434 if (cu->header.version >= 5)
6435 return cu->header.signature;
6436 struct attribute *attr;
6437 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6438 if (attr == nullptr || !attr->form_is_unsigned ())
6439 return gdb::optional<ULONGEST> ();
6440 return attr->as_unsigned ();
6441 }
6442
6443 /* Subroutine of cutu_reader to simplify it.
6444 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6445 Returns NULL if the specified DWO unit cannot be found. */
6446
6447 static struct dwo_unit *
6448 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6449 {
6450 dwarf2_per_cu_data *per_cu = cu->per_cu;
6451 struct dwo_unit *dwo_unit;
6452 const char *comp_dir;
6453
6454 gdb_assert (cu != NULL);
6455
6456 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6457 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6458 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6459
6460 if (per_cu->is_debug_types)
6461 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6462 else
6463 {
6464 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6465
6466 if (!signature.has_value ())
6467 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6468 " [in module %s]"),
6469 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6470
6471 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6472 }
6473
6474 return dwo_unit;
6475 }
6476
6477 /* Subroutine of cutu_reader to simplify it.
6478 See it for a description of the parameters.
6479 Read a TU directly from a DWO file, bypassing the stub. */
6480
6481 void
6482 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6483 dwarf2_per_objfile *per_objfile,
6484 dwarf2_cu *existing_cu)
6485 {
6486 struct signatured_type *sig_type;
6487
6488 /* Verify we can do the following downcast, and that we have the
6489 data we need. */
6490 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6491 sig_type = (struct signatured_type *) this_cu;
6492 gdb_assert (sig_type->dwo_unit != NULL);
6493
6494 dwarf2_cu *cu;
6495
6496 if (existing_cu != nullptr)
6497 {
6498 cu = existing_cu;
6499 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6500 /* There's no need to do the rereading_dwo_cu handling that
6501 cutu_reader does since we don't read the stub. */
6502 }
6503 else
6504 {
6505 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6506 in per_objfile yet. */
6507 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6508 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6509 cu = m_new_cu.get ();
6510 }
6511
6512 /* A future optimization, if needed, would be to use an existing
6513 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6514 could share abbrev tables. */
6515
6516 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6517 NULL /* stub_comp_unit_die */,
6518 sig_type->dwo_unit->dwo_file->comp_dir,
6519 this, &info_ptr,
6520 &comp_unit_die,
6521 &m_dwo_abbrev_table) == 0)
6522 {
6523 /* Dummy die. */
6524 dummy_p = true;
6525 }
6526 }
6527
6528 /* Initialize a CU (or TU) and read its DIEs.
6529 If the CU defers to a DWO file, read the DWO file as well.
6530
6531 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6532 Otherwise the table specified in the comp unit header is read in and used.
6533 This is an optimization for when we already have the abbrev table.
6534
6535 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6536 allocated. */
6537
6538 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6539 dwarf2_per_objfile *per_objfile,
6540 struct abbrev_table *abbrev_table,
6541 dwarf2_cu *existing_cu,
6542 bool skip_partial)
6543 : die_reader_specs {},
6544 m_this_cu (this_cu)
6545 {
6546 struct objfile *objfile = per_objfile->objfile;
6547 struct dwarf2_section_info *section = this_cu->section;
6548 bfd *abfd = section->get_bfd_owner ();
6549 const gdb_byte *begin_info_ptr;
6550 struct signatured_type *sig_type = NULL;
6551 struct dwarf2_section_info *abbrev_section;
6552 /* Non-zero if CU currently points to a DWO file and we need to
6553 reread it. When this happens we need to reread the skeleton die
6554 before we can reread the DWO file (this only applies to CUs, not TUs). */
6555 int rereading_dwo_cu = 0;
6556
6557 if (dwarf_die_debug)
6558 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6559 this_cu->is_debug_types ? "type" : "comp",
6560 sect_offset_str (this_cu->sect_off));
6561
6562 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6563 file (instead of going through the stub), short-circuit all of this. */
6564 if (this_cu->reading_dwo_directly)
6565 {
6566 /* Narrow down the scope of possibilities to have to understand. */
6567 gdb_assert (this_cu->is_debug_types);
6568 gdb_assert (abbrev_table == NULL);
6569 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6570 return;
6571 }
6572
6573 /* This is cheap if the section is already read in. */
6574 section->read (objfile);
6575
6576 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6577
6578 abbrev_section = get_abbrev_section_for_cu (this_cu);
6579
6580 dwarf2_cu *cu;
6581
6582 if (existing_cu != nullptr)
6583 {
6584 cu = existing_cu;
6585 /* If this CU is from a DWO file we need to start over, we need to
6586 refetch the attributes from the skeleton CU.
6587 This could be optimized by retrieving those attributes from when we
6588 were here the first time: the previous comp_unit_die was stored in
6589 comp_unit_obstack. But there's no data yet that we need this
6590 optimization. */
6591 if (cu->dwo_unit != NULL)
6592 rereading_dwo_cu = 1;
6593 }
6594 else
6595 {
6596 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6597 in per_objfile yet. */
6598 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6599 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6600 cu = m_new_cu.get ();
6601 }
6602
6603 /* Get the header. */
6604 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6605 {
6606 /* We already have the header, there's no need to read it in again. */
6607 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6608 }
6609 else
6610 {
6611 if (this_cu->is_debug_types)
6612 {
6613 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6614 section, abbrev_section,
6615 info_ptr, rcuh_kind::TYPE);
6616
6617 /* Since per_cu is the first member of struct signatured_type,
6618 we can go from a pointer to one to a pointer to the other. */
6619 sig_type = (struct signatured_type *) this_cu;
6620 gdb_assert (sig_type->signature == cu->header.signature);
6621 gdb_assert (sig_type->type_offset_in_tu
6622 == cu->header.type_cu_offset_in_tu);
6623 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6624
6625 /* LENGTH has not been set yet for type units if we're
6626 using .gdb_index. */
6627 this_cu->length = cu->header.get_length ();
6628
6629 /* Establish the type offset that can be used to lookup the type. */
6630 sig_type->type_offset_in_section =
6631 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6632
6633 this_cu->dwarf_version = cu->header.version;
6634 }
6635 else
6636 {
6637 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6638 section, abbrev_section,
6639 info_ptr,
6640 rcuh_kind::COMPILE);
6641
6642 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6643 if (this_cu->length == 0)
6644 this_cu->length = cu->header.get_length ();
6645 else
6646 gdb_assert (this_cu->length == cu->header.get_length ());
6647 this_cu->dwarf_version = cu->header.version;
6648 }
6649 }
6650
6651 /* Skip dummy compilation units. */
6652 if (info_ptr >= begin_info_ptr + this_cu->length
6653 || peek_abbrev_code (abfd, info_ptr) == 0)
6654 {
6655 dummy_p = true;
6656 return;
6657 }
6658
6659 /* If we don't have them yet, read the abbrevs for this compilation unit.
6660 And if we need to read them now, make sure they're freed when we're
6661 done. */
6662 if (abbrev_table != NULL)
6663 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6664 else
6665 {
6666 abbrev_section->read (objfile);
6667 m_abbrev_table_holder
6668 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6669 abbrev_table = m_abbrev_table_holder.get ();
6670 }
6671
6672 /* Read the top level CU/TU die. */
6673 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6674 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6675
6676 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6677 {
6678 dummy_p = true;
6679 return;
6680 }
6681
6682 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6683 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6684 table from the DWO file and pass the ownership over to us. It will be
6685 referenced from READER, so we must make sure to free it after we're done
6686 with READER.
6687
6688 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6689 DWO CU, that this test will fail (the attribute will not be present). */
6690 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6691 if (dwo_name != nullptr)
6692 {
6693 struct dwo_unit *dwo_unit;
6694 struct die_info *dwo_comp_unit_die;
6695
6696 if (comp_unit_die->has_children)
6697 {
6698 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6699 " has children (offset %s) [in module %s]"),
6700 sect_offset_str (this_cu->sect_off),
6701 bfd_get_filename (abfd));
6702 }
6703 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6704 if (dwo_unit != NULL)
6705 {
6706 if (read_cutu_die_from_dwo (cu, dwo_unit,
6707 comp_unit_die, NULL,
6708 this, &info_ptr,
6709 &dwo_comp_unit_die,
6710 &m_dwo_abbrev_table) == 0)
6711 {
6712 /* Dummy die. */
6713 dummy_p = true;
6714 return;
6715 }
6716 comp_unit_die = dwo_comp_unit_die;
6717 }
6718 else
6719 {
6720 /* Yikes, we couldn't find the rest of the DIE, we only have
6721 the stub. A complaint has already been logged. There's
6722 not much more we can do except pass on the stub DIE to
6723 die_reader_func. We don't want to throw an error on bad
6724 debug info. */
6725 }
6726 }
6727 }
6728
6729 void
6730 cutu_reader::keep ()
6731 {
6732 /* Done, clean up. */
6733 gdb_assert (!dummy_p);
6734 if (m_new_cu != NULL)
6735 {
6736 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6737 now. */
6738 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6739 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6740 }
6741 }
6742
6743 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6744 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6745 assumed to have already done the lookup to find the DWO file).
6746
6747 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6748 THIS_CU->is_debug_types, but nothing else.
6749
6750 We fill in THIS_CU->length.
6751
6752 THIS_CU->cu is always freed when done.
6753 This is done in order to not leave THIS_CU->cu in a state where we have
6754 to care whether it refers to the "main" CU or the DWO CU.
6755
6756 When parent_cu is passed, it is used to provide a default value for
6757 str_offsets_base and addr_base from the parent. */
6758
6759 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6760 dwarf2_per_objfile *per_objfile,
6761 struct dwarf2_cu *parent_cu,
6762 struct dwo_file *dwo_file)
6763 : die_reader_specs {},
6764 m_this_cu (this_cu)
6765 {
6766 struct objfile *objfile = per_objfile->objfile;
6767 struct dwarf2_section_info *section = this_cu->section;
6768 bfd *abfd = section->get_bfd_owner ();
6769 struct dwarf2_section_info *abbrev_section;
6770 const gdb_byte *begin_info_ptr, *info_ptr;
6771
6772 if (dwarf_die_debug)
6773 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6774 this_cu->is_debug_types ? "type" : "comp",
6775 sect_offset_str (this_cu->sect_off));
6776
6777 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6778
6779 abbrev_section = (dwo_file != NULL
6780 ? &dwo_file->sections.abbrev
6781 : get_abbrev_section_for_cu (this_cu));
6782
6783 /* This is cheap if the section is already read in. */
6784 section->read (objfile);
6785
6786 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6787
6788 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6789 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6790 section, abbrev_section, info_ptr,
6791 (this_cu->is_debug_types
6792 ? rcuh_kind::TYPE
6793 : rcuh_kind::COMPILE));
6794
6795 if (parent_cu != nullptr)
6796 {
6797 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6798 m_new_cu->addr_base = parent_cu->addr_base;
6799 }
6800 this_cu->length = m_new_cu->header.get_length ();
6801
6802 /* Skip dummy compilation units. */
6803 if (info_ptr >= begin_info_ptr + this_cu->length
6804 || peek_abbrev_code (abfd, info_ptr) == 0)
6805 {
6806 dummy_p = true;
6807 return;
6808 }
6809
6810 abbrev_section->read (objfile);
6811 m_abbrev_table_holder
6812 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6813
6814 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6815 m_abbrev_table_holder.get ());
6816 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6817 }
6818
6819 \f
6820 /* Type Unit Groups.
6821
6822 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6823 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6824 so that all types coming from the same compilation (.o file) are grouped
6825 together. A future step could be to put the types in the same symtab as
6826 the CU the types ultimately came from. */
6827
6828 static hashval_t
6829 hash_type_unit_group (const void *item)
6830 {
6831 const struct type_unit_group *tu_group
6832 = (const struct type_unit_group *) item;
6833
6834 return hash_stmt_list_entry (&tu_group->hash);
6835 }
6836
6837 static int
6838 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6839 {
6840 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6841 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6842
6843 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6844 }
6845
6846 /* Allocate a hash table for type unit groups. */
6847
6848 static htab_up
6849 allocate_type_unit_groups_table ()
6850 {
6851 return htab_up (htab_create_alloc (3,
6852 hash_type_unit_group,
6853 eq_type_unit_group,
6854 htab_delete_entry<type_unit_group>,
6855 xcalloc, xfree));
6856 }
6857
6858 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6859 partial symtabs. We combine several TUs per psymtab to not let the size
6860 of any one psymtab grow too big. */
6861 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6862 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6863
6864 /* Helper routine for get_type_unit_group.
6865 Create the type_unit_group object used to hold one or more TUs. */
6866
6867 static std::unique_ptr<type_unit_group>
6868 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6869 {
6870 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6871 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6872
6873 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6874 tu_group->per_bfd = per_bfd;
6875
6876 if (per_bfd->using_index)
6877 {
6878 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6879 struct dwarf2_per_cu_quick_data);
6880 }
6881 else
6882 {
6883 unsigned int line_offset = to_underlying (line_offset_struct);
6884 dwarf2_psymtab *pst;
6885 std::string name;
6886
6887 /* Give the symtab a useful name for debug purposes. */
6888 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6889 name = string_printf ("<type_units_%d>",
6890 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6891 else
6892 name = string_printf ("<type_units_at_0x%x>", line_offset);
6893
6894 pst = create_partial_symtab (tu_group.get (), per_objfile,
6895 name.c_str ());
6896 pst->anonymous = true;
6897 }
6898
6899 tu_group->hash.dwo_unit = cu->dwo_unit;
6900 tu_group->hash.line_sect_off = line_offset_struct;
6901
6902 return tu_group;
6903 }
6904
6905 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6906 STMT_LIST is a DW_AT_stmt_list attribute. */
6907
6908 static struct type_unit_group *
6909 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6910 {
6911 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6912 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6913 struct type_unit_group *tu_group;
6914 void **slot;
6915 unsigned int line_offset;
6916 struct type_unit_group type_unit_group_for_lookup;
6917
6918 if (per_objfile->per_bfd->type_unit_groups == NULL)
6919 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6920
6921 /* Do we need to create a new group, or can we use an existing one? */
6922
6923 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6924 {
6925 line_offset = stmt_list->as_unsigned ();
6926 ++tu_stats->nr_symtab_sharers;
6927 }
6928 else
6929 {
6930 /* Ugh, no stmt_list. Rare, but we have to handle it.
6931 We can do various things here like create one group per TU or
6932 spread them over multiple groups to split up the expansion work.
6933 To avoid worst case scenarios (too many groups or too large groups)
6934 we, umm, group them in bunches. */
6935 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6936 | (tu_stats->nr_stmt_less_type_units
6937 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6938 ++tu_stats->nr_stmt_less_type_units;
6939 }
6940
6941 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6942 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6943 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6944 &type_unit_group_for_lookup, INSERT);
6945 if (*slot == nullptr)
6946 {
6947 sect_offset line_offset_struct = (sect_offset) line_offset;
6948 std::unique_ptr<type_unit_group> grp
6949 = create_type_unit_group (cu, line_offset_struct);
6950 *slot = grp.release ();
6951 ++tu_stats->nr_symtabs;
6952 }
6953
6954 tu_group = (struct type_unit_group *) *slot;
6955 gdb_assert (tu_group != nullptr);
6956 return tu_group;
6957 }
6958 \f
6959 /* Partial symbol tables. */
6960
6961 /* Create a psymtab named NAME and assign it to PER_CU.
6962
6963 The caller must fill in the following details:
6964 dirname, textlow, texthigh. */
6965
6966 static dwarf2_psymtab *
6967 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6968 dwarf2_per_objfile *per_objfile,
6969 const char *name)
6970 {
6971 dwarf2_psymtab *pst
6972 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6973 per_objfile->objfile->per_bfd, per_cu);
6974
6975 pst->psymtabs_addrmap_supported = true;
6976
6977 /* This is the glue that links PST into GDB's symbol API. */
6978 per_cu->v.psymtab = pst;
6979
6980 return pst;
6981 }
6982
6983 /* DIE reader function for process_psymtab_comp_unit. */
6984
6985 static void
6986 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6987 const gdb_byte *info_ptr,
6988 struct die_info *comp_unit_die,
6989 enum language pretend_language)
6990 {
6991 struct dwarf2_cu *cu = reader->cu;
6992 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6993 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6994 struct objfile *objfile = per_objfile->objfile;
6995 struct gdbarch *gdbarch = objfile->arch ();
6996 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6997 CORE_ADDR baseaddr;
6998 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6999 dwarf2_psymtab *pst;
7000 enum pc_bounds_kind cu_bounds_kind;
7001
7002 gdb_assert (! per_cu->is_debug_types);
7003
7004 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7005
7006 /* Allocate a new partial symbol table structure. */
7007 static const char artificial[] = "<artificial>";
7008 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
7009 if (strcmp (fnd.get_name (), artificial) == 0)
7010 {
7011 gdb::unique_xmalloc_ptr<char> debug_filename
7012 (concat (artificial, "@",
7013 sect_offset_str (per_cu->sect_off),
7014 (char *) NULL));
7015 fnd.set_name (std::move (debug_filename));
7016 }
7017
7018 pst = create_partial_symtab (per_cu, per_objfile, fnd.get_name ());
7019
7020 /* This must be done before calling dwarf2_build_include_psymtabs. */
7021 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7022
7023 baseaddr = objfile->text_section_offset ();
7024
7025 dwarf2_find_base_address (comp_unit_die, cu);
7026
7027 /* Possibly set the default values of LOWPC and HIGHPC from
7028 `DW_AT_ranges'. */
7029 cu_bounds_kind
7030 = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7031 &best_highpc, cu,
7032 per_bfd->partial_symtabs->psymtabs_addrmap,
7033 pst);
7034 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7035 {
7036 CORE_ADDR low
7037 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7038 - baseaddr);
7039 CORE_ADDR high
7040 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7041 - baseaddr - 1);
7042 /* Store the contiguous range if it is not empty; it can be
7043 empty for CUs with no code. */
7044 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7045 low, high, pst);
7046 }
7047
7048 /* Check if comp unit has_children.
7049 If so, read the rest of the partial symbols from this comp unit.
7050 If not, there's no more debug_info for this comp unit. */
7051 if (comp_unit_die->has_children)
7052 {
7053 struct partial_die_info *first_die;
7054 CORE_ADDR lowpc, highpc;
7055
7056 lowpc = ((CORE_ADDR) -1);
7057 highpc = ((CORE_ADDR) 0);
7058
7059 first_die = load_partial_dies (reader, info_ptr, 1);
7060
7061 scan_partial_symbols (first_die, &lowpc, &highpc,
7062 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7063
7064 /* If we didn't find a lowpc, set it to highpc to avoid
7065 complaints from `maint check'. */
7066 if (lowpc == ((CORE_ADDR) -1))
7067 lowpc = highpc;
7068
7069 /* If the compilation unit didn't have an explicit address range,
7070 then use the information extracted from its child dies. */
7071 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7072 {
7073 best_lowpc = lowpc;
7074 best_highpc = highpc;
7075 }
7076 }
7077 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7078 best_lowpc + baseaddr)
7079 - baseaddr);
7080 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7081 best_highpc + baseaddr)
7082 - baseaddr);
7083
7084 pst->end ();
7085
7086 if (!cu->per_cu->imported_symtabs_empty ())
7087 {
7088 int i;
7089 int len = cu->per_cu->imported_symtabs_size ();
7090
7091 /* Fill in 'dependencies' here; we fill in 'users' in a
7092 post-pass. */
7093 pst->number_of_dependencies = len;
7094 pst->dependencies
7095 = per_bfd->partial_symtabs->allocate_dependencies (len);
7096 for (i = 0; i < len; ++i)
7097 {
7098 pst->dependencies[i]
7099 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7100 }
7101
7102 cu->per_cu->imported_symtabs_free ();
7103 }
7104
7105 /* Get the list of files included in the current compilation unit,
7106 and build a psymtab for each of them. */
7107 dwarf2_build_include_psymtabs (cu, comp_unit_die, fnd, pst);
7108
7109 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7110 ", %d global, %d static syms",
7111 per_cu->is_debug_types ? "type" : "comp",
7112 sect_offset_str (per_cu->sect_off),
7113 paddress (gdbarch, pst->text_low (objfile)),
7114 paddress (gdbarch, pst->text_high (objfile)),
7115 (int) pst->global_psymbols.size (),
7116 (int) pst->static_psymbols.size ());
7117 }
7118
7119 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7120 Process compilation unit THIS_CU for a psymtab. */
7121
7122 static void
7123 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7124 dwarf2_per_objfile *per_objfile,
7125 bool want_partial_unit,
7126 enum language pretend_language)
7127 {
7128 /* If this compilation unit was already read in, free the
7129 cached copy in order to read it in again. This is
7130 necessary because we skipped some symbols when we first
7131 read in the compilation unit (see load_partial_dies).
7132 This problem could be avoided, but the benefit is unclear. */
7133 per_objfile->remove_cu (this_cu);
7134
7135 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7136
7137 if (reader.comp_unit_die == nullptr)
7138 return;
7139
7140 switch (reader.comp_unit_die->tag)
7141 {
7142 case DW_TAG_compile_unit:
7143 this_cu->unit_type = DW_UT_compile;
7144 break;
7145 case DW_TAG_partial_unit:
7146 this_cu->unit_type = DW_UT_partial;
7147 break;
7148 case DW_TAG_type_unit:
7149 this_cu->unit_type = DW_UT_type;
7150 break;
7151 default:
7152 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7153 dwarf_tag_name (reader.comp_unit_die->tag),
7154 sect_offset_str (reader.cu->per_cu->sect_off),
7155 objfile_name (per_objfile->objfile));
7156 }
7157
7158 if (reader.dummy_p)
7159 {
7160 /* Nothing. */
7161 }
7162 else if (this_cu->is_debug_types)
7163 build_type_psymtabs_reader (&reader);
7164 else if (want_partial_unit
7165 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7166 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7167 reader.comp_unit_die,
7168 pretend_language);
7169
7170 /* Age out any secondary CUs. */
7171 per_objfile->age_comp_units ();
7172 }
7173
7174 /* Reader function for build_type_psymtabs. */
7175
7176 static void
7177 build_type_psymtabs_reader (cutu_reader *reader)
7178 {
7179 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7180 struct dwarf2_cu *cu = reader->cu;
7181 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7182 struct signatured_type *sig_type;
7183 struct type_unit_group *tu_group;
7184 struct attribute *attr;
7185 struct partial_die_info *first_die;
7186 CORE_ADDR lowpc, highpc;
7187 dwarf2_psymtab *pst;
7188 const gdb_byte *info_ptr = reader->info_ptr;
7189 struct die_info *type_unit_die = reader->comp_unit_die;
7190
7191 gdb_assert (per_cu->is_debug_types);
7192 sig_type = (struct signatured_type *) per_cu;
7193
7194 if (! type_unit_die->has_children)
7195 return;
7196
7197 attr = type_unit_die->attr (DW_AT_stmt_list);
7198 tu_group = get_type_unit_group (cu, attr);
7199
7200 if (tu_group->tus == nullptr)
7201 tu_group->tus = new std::vector<signatured_type *>;
7202 tu_group->tus->push_back (sig_type);
7203
7204 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7205 pst = create_partial_symtab (per_cu, per_objfile, "");
7206 pst->anonymous = true;
7207
7208 first_die = load_partial_dies (reader, info_ptr, 1);
7209
7210 lowpc = (CORE_ADDR) -1;
7211 highpc = (CORE_ADDR) 0;
7212 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7213
7214 pst->end ();
7215 }
7216
7217 /* Struct used to sort TUs by their abbreviation table offset. */
7218
7219 struct tu_abbrev_offset
7220 {
7221 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7222 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7223 {}
7224
7225 /* This is used when sorting. */
7226 bool operator< (const tu_abbrev_offset &other) const
7227 {
7228 return abbrev_offset < other.abbrev_offset;
7229 }
7230
7231 signatured_type *sig_type;
7232 sect_offset abbrev_offset;
7233 };
7234
7235 /* Efficiently read all the type units.
7236
7237 The efficiency is because we sort TUs by the abbrev table they use and
7238 only read each abbrev table once. In one program there are 200K TUs
7239 sharing 8K abbrev tables.
7240
7241 The main purpose of this function is to support building the
7242 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7243 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7244 can collapse the search space by grouping them by stmt_list.
7245 The savings can be significant, in the same program from above the 200K TUs
7246 share 8K stmt_list tables.
7247
7248 FUNC is expected to call get_type_unit_group, which will create the
7249 struct type_unit_group if necessary and add it to
7250 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7251
7252 static void
7253 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7254 {
7255 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7256 abbrev_table_up abbrev_table;
7257 sect_offset abbrev_offset;
7258
7259 /* It's up to the caller to not call us multiple times. */
7260 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7261
7262 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7263 return;
7264
7265 /* TUs typically share abbrev tables, and there can be way more TUs than
7266 abbrev tables. Sort by abbrev table to reduce the number of times we
7267 read each abbrev table in.
7268 Alternatives are to punt or to maintain a cache of abbrev tables.
7269 This is simpler and efficient enough for now.
7270
7271 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7272 symtab to use). Typically TUs with the same abbrev offset have the same
7273 stmt_list value too so in practice this should work well.
7274
7275 The basic algorithm here is:
7276
7277 sort TUs by abbrev table
7278 for each TU with same abbrev table:
7279 read abbrev table if first user
7280 read TU top level DIE
7281 [IWBN if DWO skeletons had DW_AT_stmt_list]
7282 call FUNC */
7283
7284 dwarf_read_debug_printf ("Building type unit groups ...");
7285
7286 /* Sort in a separate table to maintain the order of all_comp_units
7287 for .gdb_index: TU indices directly index all_type_units. */
7288 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7289 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7290
7291 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7292 {
7293 if (cu->is_debug_types)
7294 {
7295 auto sig_type = static_cast<signatured_type *> (cu.get ());
7296 sorted_by_abbrev.emplace_back
7297 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7298 sig_type->sect_off));
7299 }
7300 }
7301
7302 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7303
7304 abbrev_offset = (sect_offset) ~(unsigned) 0;
7305
7306 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7307 {
7308 /* Switch to the next abbrev table if necessary. */
7309 if (abbrev_table == NULL
7310 || tu.abbrev_offset != abbrev_offset)
7311 {
7312 abbrev_offset = tu.abbrev_offset;
7313 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7314 abbrev_table =
7315 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7316 ++tu_stats->nr_uniq_abbrev_tables;
7317 }
7318
7319 cutu_reader reader (tu.sig_type, per_objfile,
7320 abbrev_table.get (), nullptr, false);
7321 if (!reader.dummy_p)
7322 build_type_psymtabs_reader (&reader);
7323 }
7324 }
7325
7326 /* Print collected type unit statistics. */
7327
7328 static void
7329 print_tu_stats (dwarf2_per_objfile *per_objfile)
7330 {
7331 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7332
7333 dwarf_read_debug_printf ("Type unit statistics:");
7334 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7335 dwarf_read_debug_printf (" %d uniq abbrev tables",
7336 tu_stats->nr_uniq_abbrev_tables);
7337 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7338 tu_stats->nr_symtabs);
7339 dwarf_read_debug_printf (" %d symtab sharers",
7340 tu_stats->nr_symtab_sharers);
7341 dwarf_read_debug_printf (" %d type units without a stmt_list",
7342 tu_stats->nr_stmt_less_type_units);
7343 dwarf_read_debug_printf (" %d all_type_units reallocs",
7344 tu_stats->nr_all_type_units_reallocs);
7345 }
7346
7347 /* Traversal function for build_type_psymtabs. */
7348
7349 static int
7350 build_type_psymtab_dependencies (void **slot, void *info)
7351 {
7352 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7353 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7354 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7355 dwarf2_psymtab *pst = tu_group->v.psymtab;
7356 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7357 int i;
7358
7359 gdb_assert (len > 0);
7360 gdb_assert (tu_group->type_unit_group_p ());
7361
7362 pst->number_of_dependencies = len;
7363 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7364 for (i = 0; i < len; ++i)
7365 {
7366 struct signatured_type *iter = tu_group->tus->at (i);
7367 gdb_assert (iter->is_debug_types);
7368 pst->dependencies[i] = iter->v.psymtab;
7369 iter->type_unit_group = tu_group;
7370 }
7371
7372 delete tu_group->tus;
7373 tu_group->tus = nullptr;
7374
7375 return 1;
7376 }
7377
7378 /* Traversal function for process_skeletonless_type_unit.
7379 Read a TU in a DWO file and build partial symbols for it. */
7380
7381 static int
7382 process_skeletonless_type_unit (void **slot, void *info)
7383 {
7384 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7385 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7386
7387 /* If this TU doesn't exist in the global table, add it and read it in. */
7388
7389 if (per_objfile->per_bfd->signatured_types == NULL)
7390 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7391
7392 signatured_type find_entry (dwo_unit->signature);
7393 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7394 &find_entry, INSERT);
7395 /* If we've already seen this type there's nothing to do. What's happening
7396 is we're doing our own version of comdat-folding here. */
7397 if (*slot != NULL)
7398 return 1;
7399
7400 /* This does the job that create_all_comp_units would have done for
7401 this TU. */
7402 signatured_type *entry
7403 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7404 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7405 *slot = entry;
7406
7407 /* This does the job that build_type_psymtabs would have done. */
7408 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7409 if (!reader.dummy_p)
7410 build_type_psymtabs_reader (&reader);
7411
7412 return 1;
7413 }
7414
7415 /* Traversal function for process_skeletonless_type_units. */
7416
7417 static int
7418 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7419 {
7420 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7421
7422 if (dwo_file->tus != NULL)
7423 htab_traverse_noresize (dwo_file->tus.get (),
7424 process_skeletonless_type_unit, info);
7425
7426 return 1;
7427 }
7428
7429 /* Scan all TUs of DWO files, verifying we've processed them.
7430 This is needed in case a TU was emitted without its skeleton.
7431 Note: This can't be done until we know what all the DWO files are. */
7432
7433 static void
7434 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7435 {
7436 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7437 if (get_dwp_file (per_objfile) == NULL
7438 && per_objfile->per_bfd->dwo_files != NULL)
7439 {
7440 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7441 process_dwo_file_for_skeletonless_type_units,
7442 per_objfile);
7443 }
7444 }
7445
7446 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7447
7448 static void
7449 set_partial_user (dwarf2_per_objfile *per_objfile)
7450 {
7451 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7452 {
7453 dwarf2_psymtab *pst = per_cu->v.psymtab;
7454
7455 if (pst == NULL)
7456 continue;
7457
7458 for (int j = 0; j < pst->number_of_dependencies; ++j)
7459 {
7460 /* Set the 'user' field only if it is not already set. */
7461 if (pst->dependencies[j]->user == NULL)
7462 pst->dependencies[j]->user = pst;
7463 }
7464 }
7465 }
7466
7467 /* Build the partial symbol table by doing a quick pass through the
7468 .debug_info and .debug_abbrev sections. */
7469
7470 static void
7471 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7472 {
7473 struct objfile *objfile = per_objfile->objfile;
7474 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7475
7476 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7477 objfile_name (objfile));
7478
7479 scoped_restore restore_reading_psyms
7480 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7481
7482 per_bfd->info.read (objfile);
7483
7484 /* Any cached compilation units will be linked by the per-objfile
7485 read_in_chain. Make sure to free them when we're done. */
7486 free_cached_comp_units freer (per_objfile);
7487
7488 create_all_comp_units (per_objfile);
7489 build_type_psymtabs (per_objfile);
7490
7491 /* Create a temporary address map on a temporary obstack. We later
7492 copy this to the final obstack. */
7493 auto_obstack temp_obstack;
7494
7495 scoped_restore save_psymtabs_addrmap
7496 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7497 addrmap_create_mutable (&temp_obstack));
7498
7499 for (const auto &per_cu : per_bfd->all_comp_units)
7500 {
7501 if (per_cu->v.psymtab != NULL)
7502 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7503 continue;
7504 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7505 language_minimal);
7506 }
7507
7508 /* This has to wait until we read the CUs, we need the list of DWOs. */
7509 process_skeletonless_type_units (per_objfile);
7510
7511 /* Now that all TUs have been processed we can fill in the dependencies. */
7512 if (per_bfd->type_unit_groups != NULL)
7513 {
7514 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7515 build_type_psymtab_dependencies, per_objfile);
7516 }
7517
7518 if (dwarf_read_debug > 0)
7519 print_tu_stats (per_objfile);
7520
7521 set_partial_user (per_objfile);
7522
7523 per_bfd->partial_symtabs->psymtabs_addrmap
7524 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7525 per_bfd->partial_symtabs->obstack ());
7526 /* At this point we want to keep the address map. */
7527 save_psymtabs_addrmap.release ();
7528
7529 dwarf_read_debug_printf ("Done building psymtabs of %s",
7530 objfile_name (objfile));
7531 }
7532
7533 /* Load the partial DIEs for a secondary CU into memory.
7534 This is also used when rereading a primary CU with load_all_dies. */
7535
7536 static void
7537 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7538 dwarf2_per_objfile *per_objfile,
7539 dwarf2_cu *existing_cu)
7540 {
7541 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7542
7543 if (!reader.dummy_p)
7544 {
7545 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7546 language_minimal);
7547
7548 /* Check if comp unit has_children.
7549 If so, read the rest of the partial symbols from this comp unit.
7550 If not, there's no more debug_info for this comp unit. */
7551 if (reader.comp_unit_die->has_children)
7552 load_partial_dies (&reader, reader.info_ptr, 0);
7553
7554 reader.keep ();
7555 }
7556 }
7557
7558 static void
7559 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7560 struct dwarf2_section_info *section,
7561 struct dwarf2_section_info *abbrev_section,
7562 unsigned int is_dwz,
7563 htab_up &types_htab,
7564 rcuh_kind section_kind)
7565 {
7566 const gdb_byte *info_ptr;
7567 struct objfile *objfile = per_objfile->objfile;
7568
7569 dwarf_read_debug_printf ("Reading %s for %s",
7570 section->get_name (),
7571 section->get_file_name ());
7572
7573 section->read (objfile);
7574
7575 info_ptr = section->buffer;
7576
7577 while (info_ptr < section->buffer + section->size)
7578 {
7579 dwarf2_per_cu_data_up this_cu;
7580
7581 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7582
7583 comp_unit_head cu_header;
7584 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7585 abbrev_section, info_ptr,
7586 section_kind);
7587
7588 /* Save the compilation unit for later lookup. */
7589 if (cu_header.unit_type != DW_UT_type)
7590 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7591 else
7592 {
7593 if (types_htab == nullptr)
7594 types_htab = allocate_signatured_type_table ();
7595
7596 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7597 (cu_header.signature);
7598 signatured_type *sig_ptr = sig_type.get ();
7599 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7600 this_cu.reset (sig_type.release ());
7601
7602 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7603 gdb_assert (slot != nullptr);
7604 if (*slot != nullptr)
7605 complaint (_("debug type entry at offset %s is duplicate to"
7606 " the entry at offset %s, signature %s"),
7607 sect_offset_str (sect_off),
7608 sect_offset_str (sig_ptr->sect_off),
7609 hex_string (sig_ptr->signature));
7610 *slot = sig_ptr;
7611 }
7612 this_cu->sect_off = sect_off;
7613 this_cu->length = cu_header.length + cu_header.initial_length_size;
7614 this_cu->is_dwz = is_dwz;
7615 this_cu->section = section;
7616
7617 info_ptr = info_ptr + this_cu->length;
7618 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7619 }
7620 }
7621
7622 /* Create a list of all compilation units in OBJFILE.
7623 This is only done for -readnow and building partial symtabs. */
7624
7625 static void
7626 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7627 {
7628 htab_up types_htab;
7629
7630 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7631 &per_objfile->per_bfd->abbrev, 0,
7632 types_htab, rcuh_kind::COMPILE);
7633 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7634 read_comp_units_from_section (per_objfile, &section,
7635 &per_objfile->per_bfd->abbrev, 0,
7636 types_htab, rcuh_kind::TYPE);
7637
7638 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7639 if (dwz != NULL)
7640 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7641 types_htab, rcuh_kind::COMPILE);
7642
7643 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7644 }
7645
7646 /* Process all loaded DIEs for compilation unit CU, starting at
7647 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7648 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7649 DW_AT_ranges). See the comments of add_partial_subprogram on how
7650 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7651
7652 static void
7653 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7654 CORE_ADDR *highpc, int set_addrmap,
7655 struct dwarf2_cu *cu)
7656 {
7657 struct partial_die_info *pdi;
7658
7659 /* Now, march along the PDI's, descending into ones which have
7660 interesting children but skipping the children of the other ones,
7661 until we reach the end of the compilation unit. */
7662
7663 pdi = first_die;
7664
7665 while (pdi != NULL)
7666 {
7667 pdi->fixup (cu);
7668
7669 /* Anonymous namespaces or modules have no name but have interesting
7670 children, so we need to look at them. Ditto for anonymous
7671 enums. */
7672
7673 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7674 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7675 || pdi->tag == DW_TAG_imported_unit
7676 || pdi->tag == DW_TAG_inlined_subroutine)
7677 {
7678 switch (pdi->tag)
7679 {
7680 case DW_TAG_subprogram:
7681 case DW_TAG_inlined_subroutine:
7682 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7683 if (cu->per_cu->lang == language_cplus)
7684 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7685 set_addrmap, cu);
7686 break;
7687 case DW_TAG_constant:
7688 case DW_TAG_variable:
7689 case DW_TAG_typedef:
7690 case DW_TAG_union_type:
7691 if (!pdi->is_declaration
7692 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7693 {
7694 add_partial_symbol (pdi, cu);
7695 }
7696 break;
7697 case DW_TAG_class_type:
7698 case DW_TAG_interface_type:
7699 case DW_TAG_structure_type:
7700 if (!pdi->is_declaration)
7701 {
7702 add_partial_symbol (pdi, cu);
7703 }
7704 if ((cu->per_cu->lang == language_rust
7705 || cu->per_cu->lang == language_cplus)
7706 && pdi->has_children)
7707 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7708 set_addrmap, cu);
7709 break;
7710 case DW_TAG_enumeration_type:
7711 if (!pdi->is_declaration)
7712 add_partial_enumeration (pdi, cu);
7713 break;
7714 case DW_TAG_base_type:
7715 case DW_TAG_generic_subrange:
7716 case DW_TAG_subrange_type:
7717 /* File scope base type definitions are added to the partial
7718 symbol table. */
7719 add_partial_symbol (pdi, cu);
7720 break;
7721 case DW_TAG_namespace:
7722 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7723 break;
7724 case DW_TAG_module:
7725 if (!pdi->is_declaration)
7726 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7727 break;
7728 case DW_TAG_imported_unit:
7729 {
7730 struct dwarf2_per_cu_data *per_cu;
7731
7732 /* For now we don't handle imported units in type units. */
7733 if (cu->per_cu->is_debug_types)
7734 {
7735 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7736 " supported in type units [in module %s]"),
7737 objfile_name (cu->per_objfile->objfile));
7738 }
7739
7740 per_cu = dwarf2_find_containing_comp_unit
7741 (pdi->d.sect_off, pdi->is_dwz,
7742 cu->per_objfile->per_bfd);
7743
7744 /* Go read the partial unit, if needed. */
7745 if (per_cu->v.psymtab == NULL)
7746 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7747 cu->per_cu->lang);
7748
7749 if (pdi->die_parent == nullptr
7750 && per_cu->unit_type == DW_UT_compile
7751 && per_cu->lang == language_cplus)
7752 /* Regard import as hint. See corresponding code in
7753 process_imported_unit_die. */
7754 break;
7755
7756 cu->per_cu->imported_symtabs_push (per_cu);
7757 }
7758 break;
7759 case DW_TAG_imported_declaration:
7760 add_partial_symbol (pdi, cu);
7761 break;
7762 default:
7763 break;
7764 }
7765 }
7766
7767 /* If the die has a sibling, skip to the sibling. */
7768
7769 pdi = pdi->die_sibling;
7770 }
7771 }
7772
7773 /* Functions used to compute the fully scoped name of a partial DIE.
7774
7775 Normally, this is simple. For C++, the parent DIE's fully scoped
7776 name is concatenated with "::" and the partial DIE's name.
7777 Enumerators are an exception; they use the scope of their parent
7778 enumeration type, i.e. the name of the enumeration type is not
7779 prepended to the enumerator.
7780
7781 There are two complexities. One is DW_AT_specification; in this
7782 case "parent" means the parent of the target of the specification,
7783 instead of the direct parent of the DIE. The other is compilers
7784 which do not emit DW_TAG_namespace; in this case we try to guess
7785 the fully qualified name of structure types from their members'
7786 linkage names. This must be done using the DIE's children rather
7787 than the children of any DW_AT_specification target. We only need
7788 to do this for structures at the top level, i.e. if the target of
7789 any DW_AT_specification (if any; otherwise the DIE itself) does not
7790 have a parent. */
7791
7792 /* Compute the scope prefix associated with PDI's parent, in
7793 compilation unit CU. The result will be allocated on CU's
7794 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7795 field. NULL is returned if no prefix is necessary. */
7796 static const char *
7797 partial_die_parent_scope (struct partial_die_info *pdi,
7798 struct dwarf2_cu *cu)
7799 {
7800 const char *grandparent_scope;
7801 struct partial_die_info *parent, *real_pdi;
7802
7803 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7804 then this means the parent of the specification DIE. */
7805
7806 real_pdi = pdi;
7807 while (real_pdi->has_specification)
7808 {
7809 auto res = find_partial_die (real_pdi->spec_offset,
7810 real_pdi->spec_is_dwz, cu);
7811 real_pdi = res.pdi;
7812 cu = res.cu;
7813 }
7814
7815 parent = real_pdi->die_parent;
7816 if (parent == NULL)
7817 return NULL;
7818
7819 if (parent->scope_set)
7820 return parent->scope;
7821
7822 parent->fixup (cu);
7823
7824 grandparent_scope = partial_die_parent_scope (parent, cu);
7825
7826 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7827 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7828 Work around this problem here. */
7829 if (cu->per_cu->lang == language_cplus
7830 && parent->tag == DW_TAG_namespace
7831 && strcmp (parent->name (cu), "::") == 0
7832 && grandparent_scope == NULL)
7833 {
7834 parent->scope = NULL;
7835 parent->scope_set = 1;
7836 return NULL;
7837 }
7838
7839 /* Nested subroutines in Fortran get a prefix. */
7840 if (pdi->tag == DW_TAG_enumerator)
7841 /* Enumerators should not get the name of the enumeration as a prefix. */
7842 parent->scope = grandparent_scope;
7843 else if (parent->tag == DW_TAG_namespace
7844 || parent->tag == DW_TAG_module
7845 || parent->tag == DW_TAG_structure_type
7846 || parent->tag == DW_TAG_class_type
7847 || parent->tag == DW_TAG_interface_type
7848 || parent->tag == DW_TAG_union_type
7849 || parent->tag == DW_TAG_enumeration_type
7850 || (cu->per_cu->lang == language_fortran
7851 && parent->tag == DW_TAG_subprogram
7852 && pdi->tag == DW_TAG_subprogram))
7853 {
7854 if (grandparent_scope == NULL)
7855 parent->scope = parent->name (cu);
7856 else
7857 parent->scope = typename_concat (&cu->comp_unit_obstack,
7858 grandparent_scope,
7859 parent->name (cu), 0, cu);
7860 }
7861 else
7862 {
7863 /* FIXME drow/2004-04-01: What should we be doing with
7864 function-local names? For partial symbols, we should probably be
7865 ignoring them. */
7866 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7867 dwarf_tag_name (parent->tag),
7868 sect_offset_str (pdi->sect_off));
7869 parent->scope = grandparent_scope;
7870 }
7871
7872 parent->scope_set = 1;
7873 return parent->scope;
7874 }
7875
7876 /* Return the fully scoped name associated with PDI, from compilation unit
7877 CU. The result will be allocated with malloc. */
7878
7879 static gdb::unique_xmalloc_ptr<char>
7880 partial_die_full_name (struct partial_die_info *pdi,
7881 struct dwarf2_cu *cu)
7882 {
7883 const char *parent_scope;
7884
7885 /* If this is a template instantiation, we can not work out the
7886 template arguments from partial DIEs. So, unfortunately, we have
7887 to go through the full DIEs. At least any work we do building
7888 types here will be reused if full symbols are loaded later. */
7889 if (pdi->has_template_arguments)
7890 {
7891 pdi->fixup (cu);
7892
7893 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7894 {
7895 struct die_info *die;
7896 struct attribute attr;
7897 struct dwarf2_cu *ref_cu = cu;
7898
7899 /* DW_FORM_ref_addr is using section offset. */
7900 attr.name = (enum dwarf_attribute) 0;
7901 attr.form = DW_FORM_ref_addr;
7902 attr.u.unsnd = to_underlying (pdi->sect_off);
7903 die = follow_die_ref (NULL, &attr, &ref_cu);
7904
7905 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7906 }
7907 }
7908
7909 parent_scope = partial_die_parent_scope (pdi, cu);
7910 if (parent_scope == NULL)
7911 return NULL;
7912 else
7913 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7914 pdi->name (cu),
7915 0, cu));
7916 }
7917
7918 static void
7919 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7920 {
7921 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7922 struct objfile *objfile = per_objfile->objfile;
7923 struct gdbarch *gdbarch = objfile->arch ();
7924 CORE_ADDR addr = 0;
7925 const char *actual_name = NULL;
7926 CORE_ADDR baseaddr;
7927
7928 baseaddr = objfile->text_section_offset ();
7929
7930 gdb::unique_xmalloc_ptr<char> built_actual_name
7931 = partial_die_full_name (pdi, cu);
7932 if (built_actual_name != NULL)
7933 actual_name = built_actual_name.get ();
7934
7935 if (actual_name == NULL)
7936 actual_name = pdi->name (cu);
7937
7938 partial_symbol psymbol;
7939 memset (&psymbol, 0, sizeof (psymbol));
7940 psymbol.ginfo.set_language (cu->per_cu->lang,
7941 &objfile->objfile_obstack);
7942 psymbol.ginfo.set_section_index (-1);
7943
7944 /* The code below indicates that the psymbol should be installed by
7945 setting this. */
7946 gdb::optional<psymbol_placement> where;
7947
7948 switch (pdi->tag)
7949 {
7950 case DW_TAG_inlined_subroutine:
7951 case DW_TAG_subprogram:
7952 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7953 - baseaddr);
7954 if (pdi->is_external
7955 || cu->per_cu->lang == language_ada
7956 || (cu->per_cu->lang == language_fortran
7957 && pdi->die_parent != NULL
7958 && pdi->die_parent->tag == DW_TAG_subprogram))
7959 {
7960 /* Normally, only "external" DIEs are part of the global scope.
7961 But in Ada and Fortran, we want to be able to access nested
7962 procedures globally. So all Ada and Fortran subprograms are
7963 stored in the global scope. */
7964 where = psymbol_placement::GLOBAL;
7965 }
7966 else
7967 where = psymbol_placement::STATIC;
7968
7969 psymbol.domain = VAR_DOMAIN;
7970 psymbol.aclass = LOC_BLOCK;
7971 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7972 psymbol.ginfo.set_value_address (addr);
7973
7974 if (pdi->main_subprogram && actual_name != NULL)
7975 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
7976 break;
7977 case DW_TAG_constant:
7978 psymbol.domain = VAR_DOMAIN;
7979 psymbol.aclass = LOC_STATIC;
7980 where = (pdi->is_external
7981 ? psymbol_placement::GLOBAL
7982 : psymbol_placement::STATIC);
7983 break;
7984 case DW_TAG_variable:
7985 if (pdi->d.locdesc)
7986 addr = decode_locdesc (pdi->d.locdesc, cu);
7987
7988 if (pdi->d.locdesc
7989 && addr == 0
7990 && !per_objfile->per_bfd->has_section_at_zero)
7991 {
7992 /* A global or static variable may also have been stripped
7993 out by the linker if unused, in which case its address
7994 will be nullified; do not add such variables into partial
7995 symbol table then. */
7996 }
7997 else if (pdi->is_external)
7998 {
7999 /* Global Variable.
8000 Don't enter into the minimal symbol tables as there is
8001 a minimal symbol table entry from the ELF symbols already.
8002 Enter into partial symbol table if it has a location
8003 descriptor or a type.
8004 If the location descriptor is missing, new_symbol will create
8005 a LOC_UNRESOLVED symbol, the address of the variable will then
8006 be determined from the minimal symbol table whenever the variable
8007 is referenced.
8008 The address for the partial symbol table entry is not
8009 used by GDB, but it comes in handy for debugging partial symbol
8010 table building. */
8011
8012 if (pdi->d.locdesc || pdi->has_type)
8013 {
8014 psymbol.domain = VAR_DOMAIN;
8015 psymbol.aclass = LOC_STATIC;
8016 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8017 psymbol.ginfo.set_value_address (addr);
8018 where = psymbol_placement::GLOBAL;
8019 }
8020 }
8021 else
8022 {
8023 int has_loc = pdi->d.locdesc != NULL;
8024
8025 /* Static Variable. Skip symbols whose value we cannot know (those
8026 without location descriptors or constant values). */
8027 if (!has_loc && !pdi->has_const_value)
8028 return;
8029
8030 psymbol.domain = VAR_DOMAIN;
8031 psymbol.aclass = LOC_STATIC;
8032 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8033 if (has_loc)
8034 psymbol.ginfo.set_value_address (addr);
8035 where = psymbol_placement::STATIC;
8036 }
8037 break;
8038 case DW_TAG_array_type:
8039 case DW_TAG_typedef:
8040 case DW_TAG_base_type:
8041 case DW_TAG_subrange_type:
8042 case DW_TAG_generic_subrange:
8043 psymbol.domain = VAR_DOMAIN;
8044 psymbol.aclass = LOC_TYPEDEF;
8045 where = psymbol_placement::STATIC;
8046 break;
8047 case DW_TAG_imported_declaration:
8048 case DW_TAG_namespace:
8049 psymbol.domain = VAR_DOMAIN;
8050 psymbol.aclass = LOC_TYPEDEF;
8051 where = psymbol_placement::GLOBAL;
8052 break;
8053 case DW_TAG_module:
8054 /* With Fortran 77 there might be a "BLOCK DATA" module
8055 available without any name. If so, we skip the module as it
8056 doesn't bring any value. */
8057 if (actual_name != nullptr)
8058 {
8059 psymbol.domain = MODULE_DOMAIN;
8060 psymbol.aclass = LOC_TYPEDEF;
8061 where = psymbol_placement::GLOBAL;
8062 }
8063 break;
8064 case DW_TAG_class_type:
8065 case DW_TAG_interface_type:
8066 case DW_TAG_structure_type:
8067 case DW_TAG_union_type:
8068 case DW_TAG_enumeration_type:
8069 /* Skip external references. The DWARF standard says in the section
8070 about "Structure, Union, and Class Type Entries": "An incomplete
8071 structure, union or class type is represented by a structure,
8072 union or class entry that does not have a byte size attribute
8073 and that has a DW_AT_declaration attribute." */
8074 if (!pdi->has_byte_size && pdi->is_declaration)
8075 return;
8076
8077 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8078 static vs. global. */
8079 psymbol.domain = STRUCT_DOMAIN;
8080 psymbol.aclass = LOC_TYPEDEF;
8081 where = (cu->per_cu->lang == language_cplus
8082 ? psymbol_placement::GLOBAL
8083 : psymbol_placement::STATIC);
8084 break;
8085 case DW_TAG_enumerator:
8086 psymbol.domain = VAR_DOMAIN;
8087 psymbol.aclass = LOC_CONST;
8088 where = (cu->per_cu->lang == language_cplus
8089 ? psymbol_placement::GLOBAL
8090 : psymbol_placement::STATIC);
8091 break;
8092 default:
8093 break;
8094 }
8095
8096 if (where.has_value ())
8097 {
8098 if (built_actual_name != nullptr)
8099 actual_name = objfile->intern (actual_name);
8100 if (pdi->linkage_name == nullptr
8101 || cu->per_cu->lang == language_ada)
8102 psymbol.ginfo.set_linkage_name (actual_name);
8103 else
8104 {
8105 psymbol.ginfo.set_demangled_name (actual_name,
8106 &objfile->objfile_obstack);
8107 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8108 }
8109 cu->per_cu->v.psymtab->add_psymbol
8110 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8111 objfile);
8112 }
8113 }
8114
8115 /* Read a partial die corresponding to a namespace; also, add a symbol
8116 corresponding to that namespace to the symbol table. NAMESPACE is
8117 the name of the enclosing namespace. */
8118
8119 static void
8120 add_partial_namespace (struct partial_die_info *pdi,
8121 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8122 int set_addrmap, struct dwarf2_cu *cu)
8123 {
8124 /* Add a symbol for the namespace. */
8125
8126 add_partial_symbol (pdi, cu);
8127
8128 /* Now scan partial symbols in that namespace. */
8129
8130 if (pdi->has_children)
8131 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8132 }
8133
8134 /* Read a partial die corresponding to a Fortran module. */
8135
8136 static void
8137 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8138 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8139 {
8140 /* Add a symbol for the namespace. */
8141
8142 add_partial_symbol (pdi, cu);
8143
8144 /* Now scan partial symbols in that module. */
8145
8146 if (pdi->has_children)
8147 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8148 }
8149
8150 static int dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
8151 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8152 addrmap *map, void *datum, dwarf_tag tag);
8153
8154 /* Read a partial die corresponding to a subprogram or an inlined
8155 subprogram and create a partial symbol for that subprogram.
8156 When the CU language allows it, this routine also defines a partial
8157 symbol for each nested subprogram that this subprogram contains.
8158 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8159 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8160
8161 PDI may also be a lexical block, in which case we simply search
8162 recursively for subprograms defined inside that lexical block.
8163 Again, this is only performed when the CU language allows this
8164 type of definitions. */
8165
8166 static void
8167 add_partial_subprogram (struct partial_die_info *pdi,
8168 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8169 int set_addrmap, struct dwarf2_cu *cu)
8170 {
8171 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8172 {
8173 if (pdi->has_pc_info)
8174 {
8175 if (pdi->lowpc < *lowpc)
8176 *lowpc = pdi->lowpc;
8177 if (pdi->highpc > *highpc)
8178 *highpc = pdi->highpc;
8179 if (set_addrmap)
8180 {
8181 struct objfile *objfile = cu->per_objfile->objfile;
8182 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8183 struct gdbarch *gdbarch = objfile->arch ();
8184 CORE_ADDR baseaddr;
8185 CORE_ADDR this_highpc;
8186 CORE_ADDR this_lowpc;
8187
8188 baseaddr = objfile->text_section_offset ();
8189 this_lowpc
8190 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8191 pdi->lowpc + baseaddr)
8192 - baseaddr);
8193 this_highpc
8194 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8195 pdi->highpc + baseaddr)
8196 - baseaddr);
8197 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8198 this_lowpc, this_highpc - 1,
8199 cu->per_cu->v.psymtab);
8200 }
8201 }
8202
8203 if (pdi->has_range_info
8204 && dwarf2_ranges_read (pdi->ranges_offset, &pdi->lowpc, &pdi->highpc,
8205 cu,
8206 (set_addrmap
8207 ? cu->per_objfile->per_bfd->partial_symtabs->psymtabs_addrmap
8208 : nullptr),
8209 set_addrmap ? cu->per_cu->v.psymtab : nullptr,
8210 pdi->tag))
8211 {
8212 if (pdi->lowpc < *lowpc)
8213 *lowpc = pdi->lowpc;
8214 if (pdi->highpc > *highpc)
8215 *highpc = pdi->highpc;
8216 }
8217
8218 if (pdi->has_pc_info || pdi->has_range_info
8219 || (!pdi->is_external && pdi->may_be_inlined))
8220 {
8221 if (!pdi->is_declaration)
8222 /* Ignore subprogram DIEs that do not have a name, they are
8223 illegal. Do not emit a complaint at this point, we will
8224 do so when we convert this psymtab into a symtab. */
8225 if (pdi->name (cu))
8226 add_partial_symbol (pdi, cu);
8227 }
8228 }
8229
8230 if (! pdi->has_children)
8231 return;
8232
8233 if (cu->per_cu->lang == language_ada
8234 || cu->per_cu->lang == language_fortran)
8235 {
8236 pdi = pdi->die_child;
8237 while (pdi != NULL)
8238 {
8239 pdi->fixup (cu);
8240 if (pdi->tag == DW_TAG_subprogram
8241 || pdi->tag == DW_TAG_inlined_subroutine
8242 || pdi->tag == DW_TAG_lexical_block)
8243 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8244 pdi = pdi->die_sibling;
8245 }
8246 }
8247 }
8248
8249 /* Read a partial die corresponding to an enumeration type. */
8250
8251 static void
8252 add_partial_enumeration (struct partial_die_info *enum_pdi,
8253 struct dwarf2_cu *cu)
8254 {
8255 struct partial_die_info *pdi;
8256
8257 if (enum_pdi->name (cu) != NULL)
8258 add_partial_symbol (enum_pdi, cu);
8259
8260 pdi = enum_pdi->die_child;
8261 while (pdi)
8262 {
8263 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8264 complaint (_("malformed enumerator DIE ignored"));
8265 else
8266 add_partial_symbol (pdi, cu);
8267 pdi = pdi->die_sibling;
8268 }
8269 }
8270
8271 /* Return the initial uleb128 in the die at INFO_PTR. */
8272
8273 static unsigned int
8274 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8275 {
8276 unsigned int bytes_read;
8277
8278 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8279 }
8280
8281 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8282 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8283
8284 Return the corresponding abbrev, or NULL if the number is zero (indicating
8285 an empty DIE). In either case *BYTES_READ will be set to the length of
8286 the initial number. */
8287
8288 static const struct abbrev_info *
8289 peek_die_abbrev (const die_reader_specs &reader,
8290 const gdb_byte *info_ptr, unsigned int *bytes_read)
8291 {
8292 dwarf2_cu *cu = reader.cu;
8293 bfd *abfd = reader.abfd;
8294 unsigned int abbrev_number
8295 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8296
8297 if (abbrev_number == 0)
8298 return NULL;
8299
8300 const abbrev_info *abbrev
8301 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8302 if (!abbrev)
8303 {
8304 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8305 " at offset %s [in module %s]"),
8306 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8307 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8308 }
8309
8310 return abbrev;
8311 }
8312
8313 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8314 Returns a pointer to the end of a series of DIEs, terminated by an empty
8315 DIE. Any children of the skipped DIEs will also be skipped. */
8316
8317 static const gdb_byte *
8318 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8319 {
8320 while (1)
8321 {
8322 unsigned int bytes_read;
8323 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8324 &bytes_read);
8325
8326 if (abbrev == NULL)
8327 return info_ptr + bytes_read;
8328 else
8329 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8330 }
8331 }
8332
8333 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8334 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8335 abbrev corresponding to that skipped uleb128 should be passed in
8336 ABBREV.
8337
8338 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
8339 returns a pointer to this DIE's sibling, skipping any children.
8340 Otherwise, returns a pointer to the DIE's first child. */
8341
8342 static const gdb_byte *
8343 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8344 const struct abbrev_info *abbrev, bool do_skip_children)
8345 {
8346 unsigned int bytes_read;
8347 struct attribute attr;
8348 bfd *abfd = reader->abfd;
8349 struct dwarf2_cu *cu = reader->cu;
8350 const gdb_byte *buffer = reader->buffer;
8351 const gdb_byte *buffer_end = reader->buffer_end;
8352 unsigned int form, i;
8353
8354 for (i = 0; i < abbrev->num_attrs; i++)
8355 {
8356 /* The only abbrev we care about is DW_AT_sibling. */
8357 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
8358 {
8359 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8360 if (attr.form == DW_FORM_ref_addr)
8361 complaint (_("ignoring absolute DW_AT_sibling"));
8362 else
8363 {
8364 sect_offset off = attr.get_ref_die_offset ();
8365 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8366
8367 if (sibling_ptr < info_ptr)
8368 complaint (_("DW_AT_sibling points backwards"));
8369 else if (sibling_ptr > reader->buffer_end)
8370 reader->die_section->overflow_complaint ();
8371 else
8372 return sibling_ptr;
8373 }
8374 }
8375
8376 /* If it isn't DW_AT_sibling, skip this attribute. */
8377 form = abbrev->attrs[i].form;
8378 skip_attribute:
8379 switch (form)
8380 {
8381 case DW_FORM_ref_addr:
8382 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8383 and later it is offset sized. */
8384 if (cu->header.version == 2)
8385 info_ptr += cu->header.addr_size;
8386 else
8387 info_ptr += cu->header.offset_size;
8388 break;
8389 case DW_FORM_GNU_ref_alt:
8390 info_ptr += cu->header.offset_size;
8391 break;
8392 case DW_FORM_addr:
8393 info_ptr += cu->header.addr_size;
8394 break;
8395 case DW_FORM_data1:
8396 case DW_FORM_ref1:
8397 case DW_FORM_flag:
8398 case DW_FORM_strx1:
8399 info_ptr += 1;
8400 break;
8401 case DW_FORM_flag_present:
8402 case DW_FORM_implicit_const:
8403 break;
8404 case DW_FORM_data2:
8405 case DW_FORM_ref2:
8406 case DW_FORM_strx2:
8407 info_ptr += 2;
8408 break;
8409 case DW_FORM_strx3:
8410 info_ptr += 3;
8411 break;
8412 case DW_FORM_data4:
8413 case DW_FORM_ref4:
8414 case DW_FORM_strx4:
8415 info_ptr += 4;
8416 break;
8417 case DW_FORM_data8:
8418 case DW_FORM_ref8:
8419 case DW_FORM_ref_sig8:
8420 info_ptr += 8;
8421 break;
8422 case DW_FORM_data16:
8423 info_ptr += 16;
8424 break;
8425 case DW_FORM_string:
8426 read_direct_string (abfd, info_ptr, &bytes_read);
8427 info_ptr += bytes_read;
8428 break;
8429 case DW_FORM_sec_offset:
8430 case DW_FORM_strp:
8431 case DW_FORM_GNU_strp_alt:
8432 info_ptr += cu->header.offset_size;
8433 break;
8434 case DW_FORM_exprloc:
8435 case DW_FORM_block:
8436 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8437 info_ptr += bytes_read;
8438 break;
8439 case DW_FORM_block1:
8440 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8441 break;
8442 case DW_FORM_block2:
8443 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8444 break;
8445 case DW_FORM_block4:
8446 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8447 break;
8448 case DW_FORM_addrx:
8449 case DW_FORM_strx:
8450 case DW_FORM_sdata:
8451 case DW_FORM_udata:
8452 case DW_FORM_ref_udata:
8453 case DW_FORM_GNU_addr_index:
8454 case DW_FORM_GNU_str_index:
8455 case DW_FORM_rnglistx:
8456 case DW_FORM_loclistx:
8457 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8458 break;
8459 case DW_FORM_indirect:
8460 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8461 info_ptr += bytes_read;
8462 /* We need to continue parsing from here, so just go back to
8463 the top. */
8464 goto skip_attribute;
8465
8466 default:
8467 error (_("Dwarf Error: Cannot handle %s "
8468 "in DWARF reader [in module %s]"),
8469 dwarf_form_name (form),
8470 bfd_get_filename (abfd));
8471 }
8472 }
8473
8474 if (do_skip_children && abbrev->has_children)
8475 return skip_children (reader, info_ptr);
8476 else
8477 return info_ptr;
8478 }
8479
8480 /* Locate ORIG_PDI's sibling.
8481 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8482
8483 static const gdb_byte *
8484 locate_pdi_sibling (const struct die_reader_specs *reader,
8485 struct partial_die_info *orig_pdi,
8486 const gdb_byte *info_ptr)
8487 {
8488 /* Do we know the sibling already? */
8489
8490 if (orig_pdi->sibling)
8491 return orig_pdi->sibling;
8492
8493 /* Are there any children to deal with? */
8494
8495 if (!orig_pdi->has_children)
8496 return info_ptr;
8497
8498 /* Skip the children the long way. */
8499
8500 return skip_children (reader, info_ptr);
8501 }
8502
8503 /* Expand this partial symbol table into a full symbol table. SELF is
8504 not NULL. */
8505
8506 void
8507 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8508 {
8509 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8510
8511 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8512
8513 /* If this psymtab is constructed from a debug-only objfile, the
8514 has_section_at_zero flag will not necessarily be correct. We
8515 can get the correct value for this flag by looking at the data
8516 associated with the (presumably stripped) associated objfile. */
8517 if (objfile->separate_debug_objfile_backlink)
8518 {
8519 dwarf2_per_objfile *per_objfile_backlink
8520 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8521
8522 per_objfile->per_bfd->has_section_at_zero
8523 = per_objfile_backlink->per_bfd->has_section_at_zero;
8524 }
8525
8526 expand_psymtab (objfile);
8527
8528 process_cu_includes (per_objfile);
8529 }
8530 \f
8531 /* Reading in full CUs. */
8532
8533 /* Add PER_CU to the queue. */
8534
8535 static void
8536 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8537 dwarf2_per_objfile *per_objfile,
8538 enum language pretend_language)
8539 {
8540 per_cu->queued = 1;
8541
8542 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8543 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8544 }
8545
8546 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8547
8548 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8549 dependency.
8550
8551 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8552 DIEs, false otherwise.
8553
8554 Explanation: there is an invariant that if a CU is queued for expansion
8555 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8556 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8557 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8558 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8559 invariant is respected.
8560
8561 The caller is therefore not required to load the CU's DIEs (we return false)
8562 if:
8563
8564 - the CU is already expanded, and therefore does not get enqueued
8565 - the CU gets enqueued for expansion, but its DIEs are already loaded
8566
8567 Note that the caller should not use this function's return value as an
8568 indicator of whether the CU's DIEs are loaded right now, it should check
8569 that by calling `dwarf2_per_objfile::get_cu` instead. */
8570
8571 static int
8572 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8573 dwarf2_per_cu_data *per_cu,
8574 dwarf2_per_objfile *per_objfile,
8575 enum language pretend_language)
8576 {
8577 /* We may arrive here during partial symbol reading, if we need full
8578 DIEs to process an unusual case (e.g. template arguments). Do
8579 not queue PER_CU, just tell our caller to load its DIEs. */
8580 if (per_cu->per_bfd->reading_partial_symbols)
8581 {
8582 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8583
8584 if (cu == NULL || cu->dies == NULL)
8585 return 1;
8586 return 0;
8587 }
8588
8589 /* Mark the dependence relation so that we don't flush PER_CU
8590 too early. */
8591 if (dependent_cu != NULL)
8592 dependent_cu->add_dependence (per_cu);
8593
8594 /* If it's already on the queue, we have nothing to do. */
8595 if (per_cu->queued)
8596 {
8597 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8598 loaded. */
8599 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8600
8601 /* If the CU is queued for expansion, it should not already be
8602 expanded. */
8603 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8604
8605 /* The DIEs are already loaded, the caller doesn't need to do it. */
8606 return 0;
8607 }
8608
8609 bool queued = false;
8610 if (!per_objfile->symtab_set_p (per_cu))
8611 {
8612 /* Add it to the queue. */
8613 queue_comp_unit (per_cu, per_objfile, pretend_language);
8614 queued = true;
8615 }
8616
8617 /* If the compilation unit is already loaded, just mark it as
8618 used. */
8619 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8620 if (cu != nullptr)
8621 cu->last_used = 0;
8622
8623 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8624 and the DIEs are not already loaded. */
8625 return queued && cu == nullptr;
8626 }
8627
8628 /* Process the queue. */
8629
8630 static void
8631 process_queue (dwarf2_per_objfile *per_objfile)
8632 {
8633 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8634 objfile_name (per_objfile->objfile));
8635
8636 /* The queue starts out with one item, but following a DIE reference
8637 may load a new CU, adding it to the end of the queue. */
8638 while (!per_objfile->per_bfd->queue->empty ())
8639 {
8640 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8641 dwarf2_per_cu_data *per_cu = item.per_cu;
8642
8643 if (!per_objfile->symtab_set_p (per_cu))
8644 {
8645 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8646
8647 /* Skip dummy CUs. */
8648 if (cu != nullptr)
8649 {
8650 unsigned int debug_print_threshold;
8651 char buf[100];
8652
8653 if (per_cu->is_debug_types)
8654 {
8655 struct signatured_type *sig_type =
8656 (struct signatured_type *) per_cu;
8657
8658 sprintf (buf, "TU %s at offset %s",
8659 hex_string (sig_type->signature),
8660 sect_offset_str (per_cu->sect_off));
8661 /* There can be 100s of TUs.
8662 Only print them in verbose mode. */
8663 debug_print_threshold = 2;
8664 }
8665 else
8666 {
8667 sprintf (buf, "CU at offset %s",
8668 sect_offset_str (per_cu->sect_off));
8669 debug_print_threshold = 1;
8670 }
8671
8672 if (dwarf_read_debug >= debug_print_threshold)
8673 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8674
8675 if (per_cu->is_debug_types)
8676 process_full_type_unit (cu, item.pretend_language);
8677 else
8678 process_full_comp_unit (cu, item.pretend_language);
8679
8680 if (dwarf_read_debug >= debug_print_threshold)
8681 dwarf_read_debug_printf ("Done expanding %s", buf);
8682 }
8683 }
8684
8685 per_cu->queued = 0;
8686 per_objfile->per_bfd->queue->pop ();
8687 }
8688
8689 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8690 objfile_name (per_objfile->objfile));
8691 }
8692
8693 /* Read in full symbols for PST, and anything it depends on. */
8694
8695 void
8696 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8697 {
8698 gdb_assert (!readin_p (objfile));
8699
8700 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8701 free_cached_comp_units freer (per_objfile);
8702 expand_dependencies (objfile);
8703
8704 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8705 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8706 }
8707
8708 /* See psympriv.h. */
8709
8710 bool
8711 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8712 {
8713 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8714 return per_objfile->symtab_set_p (per_cu_data);
8715 }
8716
8717 /* See psympriv.h. */
8718
8719 compunit_symtab *
8720 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8721 {
8722 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8723 return per_objfile->get_symtab (per_cu_data);
8724 }
8725
8726 /* Trivial hash function for die_info: the hash value of a DIE
8727 is its offset in .debug_info for this objfile. */
8728
8729 static hashval_t
8730 die_hash (const void *item)
8731 {
8732 const struct die_info *die = (const struct die_info *) item;
8733
8734 return to_underlying (die->sect_off);
8735 }
8736
8737 /* Trivial comparison function for die_info structures: two DIEs
8738 are equal if they have the same offset. */
8739
8740 static int
8741 die_eq (const void *item_lhs, const void *item_rhs)
8742 {
8743 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8744 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8745
8746 return die_lhs->sect_off == die_rhs->sect_off;
8747 }
8748
8749 /* Load the DIEs associated with PER_CU into memory.
8750
8751 In some cases, the caller, while reading partial symbols, will need to load
8752 the full symbols for the CU for some reason. It will already have a
8753 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8754 rather than creating a new one. */
8755
8756 static void
8757 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8758 dwarf2_per_objfile *per_objfile,
8759 dwarf2_cu *existing_cu,
8760 bool skip_partial,
8761 enum language pretend_language)
8762 {
8763 gdb_assert (! this_cu->is_debug_types);
8764
8765 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8766 if (reader.dummy_p)
8767 return;
8768
8769 struct dwarf2_cu *cu = reader.cu;
8770 const gdb_byte *info_ptr = reader.info_ptr;
8771
8772 gdb_assert (cu->die_hash == NULL);
8773 cu->die_hash =
8774 htab_create_alloc_ex (cu->header.length / 12,
8775 die_hash,
8776 die_eq,
8777 NULL,
8778 &cu->comp_unit_obstack,
8779 hashtab_obstack_allocate,
8780 dummy_obstack_deallocate);
8781
8782 if (reader.comp_unit_die->has_children)
8783 reader.comp_unit_die->child
8784 = read_die_and_siblings (&reader, reader.info_ptr,
8785 &info_ptr, reader.comp_unit_die);
8786 cu->dies = reader.comp_unit_die;
8787 /* comp_unit_die is not stored in die_hash, no need. */
8788
8789 /* We try not to read any attributes in this function, because not
8790 all CUs needed for references have been loaded yet, and symbol
8791 table processing isn't initialized. But we have to set the CU language,
8792 or we won't be able to build types correctly.
8793 Similarly, if we do not read the producer, we can not apply
8794 producer-specific interpretation. */
8795 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8796
8797 reader.keep ();
8798 }
8799
8800 /* Add a DIE to the delayed physname list. */
8801
8802 static void
8803 add_to_method_list (struct type *type, int fnfield_index, int index,
8804 const char *name, struct die_info *die,
8805 struct dwarf2_cu *cu)
8806 {
8807 struct delayed_method_info mi;
8808 mi.type = type;
8809 mi.fnfield_index = fnfield_index;
8810 mi.index = index;
8811 mi.name = name;
8812 mi.die = die;
8813 cu->method_list.push_back (mi);
8814 }
8815
8816 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8817 "const" / "volatile". If so, decrements LEN by the length of the
8818 modifier and return true. Otherwise return false. */
8819
8820 template<size_t N>
8821 static bool
8822 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8823 {
8824 size_t mod_len = sizeof (mod) - 1;
8825 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8826 {
8827 len -= mod_len;
8828 return true;
8829 }
8830 return false;
8831 }
8832
8833 /* Compute the physnames of any methods on the CU's method list.
8834
8835 The computation of method physnames is delayed in order to avoid the
8836 (bad) condition that one of the method's formal parameters is of an as yet
8837 incomplete type. */
8838
8839 static void
8840 compute_delayed_physnames (struct dwarf2_cu *cu)
8841 {
8842 /* Only C++ delays computing physnames. */
8843 if (cu->method_list.empty ())
8844 return;
8845 gdb_assert (cu->per_cu->lang == language_cplus);
8846
8847 for (const delayed_method_info &mi : cu->method_list)
8848 {
8849 const char *physname;
8850 struct fn_fieldlist *fn_flp
8851 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8852 physname = dwarf2_physname (mi.name, mi.die, cu);
8853 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8854 = physname ? physname : "";
8855
8856 /* Since there's no tag to indicate whether a method is a
8857 const/volatile overload, extract that information out of the
8858 demangled name. */
8859 if (physname != NULL)
8860 {
8861 size_t len = strlen (physname);
8862
8863 while (1)
8864 {
8865 if (physname[len] == ')') /* shortcut */
8866 break;
8867 else if (check_modifier (physname, len, " const"))
8868 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8869 else if (check_modifier (physname, len, " volatile"))
8870 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8871 else
8872 break;
8873 }
8874 }
8875 }
8876
8877 /* The list is no longer needed. */
8878 cu->method_list.clear ();
8879 }
8880
8881 /* Go objects should be embedded in a DW_TAG_module DIE,
8882 and it's not clear if/how imported objects will appear.
8883 To keep Go support simple until that's worked out,
8884 go back through what we've read and create something usable.
8885 We could do this while processing each DIE, and feels kinda cleaner,
8886 but that way is more invasive.
8887 This is to, for example, allow the user to type "p var" or "b main"
8888 without having to specify the package name, and allow lookups
8889 of module.object to work in contexts that use the expression
8890 parser. */
8891
8892 static void
8893 fixup_go_packaging (struct dwarf2_cu *cu)
8894 {
8895 gdb::unique_xmalloc_ptr<char> package_name;
8896 struct pending *list;
8897 int i;
8898
8899 for (list = *cu->get_builder ()->get_global_symbols ();
8900 list != NULL;
8901 list = list->next)
8902 {
8903 for (i = 0; i < list->nsyms; ++i)
8904 {
8905 struct symbol *sym = list->symbol[i];
8906
8907 if (sym->language () == language_go
8908 && sym->aclass () == LOC_BLOCK)
8909 {
8910 gdb::unique_xmalloc_ptr<char> this_package_name
8911 (go_symbol_package_name (sym));
8912
8913 if (this_package_name == NULL)
8914 continue;
8915 if (package_name == NULL)
8916 package_name = std::move (this_package_name);
8917 else
8918 {
8919 struct objfile *objfile = cu->per_objfile->objfile;
8920 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8921 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8922 (symbol_symtab (sym) != NULL
8923 ? symtab_to_filename_for_display
8924 (symbol_symtab (sym))
8925 : objfile_name (objfile)),
8926 this_package_name.get (), package_name.get ());
8927 }
8928 }
8929 }
8930 }
8931
8932 if (package_name != NULL)
8933 {
8934 struct objfile *objfile = cu->per_objfile->objfile;
8935 const char *saved_package_name = objfile->intern (package_name.get ());
8936 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8937 saved_package_name);
8938 struct symbol *sym;
8939
8940 sym = new (&objfile->objfile_obstack) symbol;
8941 sym->set_language (language_go, &objfile->objfile_obstack);
8942 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8943 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8944 e.g., "main" finds the "main" module and not C's main(). */
8945 sym->set_domain (STRUCT_DOMAIN);
8946 sym->set_aclass_index (LOC_TYPEDEF);
8947 sym->set_type (type);
8948
8949 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8950 }
8951 }
8952
8953 /* Allocate a fully-qualified name consisting of the two parts on the
8954 obstack. */
8955
8956 static const char *
8957 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8958 {
8959 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8960 }
8961
8962 /* A helper that allocates a variant part to attach to a Rust enum
8963 type. OBSTACK is where the results should be allocated. TYPE is
8964 the type we're processing. DISCRIMINANT_INDEX is the index of the
8965 discriminant. It must be the index of one of the fields of TYPE,
8966 or -1 to mean there is no discriminant (univariant enum).
8967 DEFAULT_INDEX is the index of the default field; or -1 if there is
8968 no default. RANGES is indexed by "effective" field number (the
8969 field index, but omitting the discriminant and default fields) and
8970 must hold the discriminant values used by the variants. Note that
8971 RANGES must have a lifetime at least as long as OBSTACK -- either
8972 already allocated on it, or static. */
8973
8974 static void
8975 alloc_rust_variant (struct obstack *obstack, struct type *type,
8976 int discriminant_index, int default_index,
8977 gdb::array_view<discriminant_range> ranges)
8978 {
8979 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8980 gdb_assert (discriminant_index == -1
8981 || (discriminant_index >= 0
8982 && discriminant_index < type->num_fields ()));
8983 gdb_assert (default_index == -1
8984 || (default_index >= 0 && default_index < type->num_fields ()));
8985
8986 /* We have one variant for each non-discriminant field. */
8987 int n_variants = type->num_fields ();
8988 if (discriminant_index != -1)
8989 --n_variants;
8990
8991 variant *variants = new (obstack) variant[n_variants];
8992 int var_idx = 0;
8993 int range_idx = 0;
8994 for (int i = 0; i < type->num_fields (); ++i)
8995 {
8996 if (i == discriminant_index)
8997 continue;
8998
8999 variants[var_idx].first_field = i;
9000 variants[var_idx].last_field = i + 1;
9001
9002 /* The default field does not need a range, but other fields do.
9003 We skipped the discriminant above. */
9004 if (i != default_index)
9005 {
9006 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9007 ++range_idx;
9008 }
9009
9010 ++var_idx;
9011 }
9012
9013 gdb_assert (range_idx == ranges.size ());
9014 gdb_assert (var_idx == n_variants);
9015
9016 variant_part *part = new (obstack) variant_part;
9017 part->discriminant_index = discriminant_index;
9018 /* If there is no discriminant, then whether it is signed is of no
9019 consequence. */
9020 part->is_unsigned
9021 = (discriminant_index == -1
9022 ? false
9023 : type->field (discriminant_index).type ()->is_unsigned ());
9024 part->variants = gdb::array_view<variant> (variants, n_variants);
9025
9026 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9027 gdb::array_view<variant_part> *prop_value
9028 = new (storage) gdb::array_view<variant_part> (part, 1);
9029
9030 struct dynamic_prop prop;
9031 prop.set_variant_parts (prop_value);
9032
9033 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9034 }
9035
9036 /* Some versions of rustc emitted enums in an unusual way.
9037
9038 Ordinary enums were emitted as unions. The first element of each
9039 structure in the union was named "RUST$ENUM$DISR". This element
9040 held the discriminant.
9041
9042 These versions of Rust also implemented the "non-zero"
9043 optimization. When the enum had two values, and one is empty and
9044 the other holds a pointer that cannot be zero, the pointer is used
9045 as the discriminant, with a zero value meaning the empty variant.
9046 Here, the union's first member is of the form
9047 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9048 where the fieldnos are the indices of the fields that should be
9049 traversed in order to find the field (which may be several fields deep)
9050 and the variantname is the name of the variant of the case when the
9051 field is zero.
9052
9053 This function recognizes whether TYPE is of one of these forms,
9054 and, if so, smashes it to be a variant type. */
9055
9056 static void
9057 quirk_rust_enum (struct type *type, struct objfile *objfile)
9058 {
9059 gdb_assert (type->code () == TYPE_CODE_UNION);
9060
9061 /* We don't need to deal with empty enums. */
9062 if (type->num_fields () == 0)
9063 return;
9064
9065 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9066 if (type->num_fields () == 1
9067 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
9068 {
9069 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
9070
9071 /* Decode the field name to find the offset of the
9072 discriminant. */
9073 ULONGEST bit_offset = 0;
9074 struct type *field_type = type->field (0).type ();
9075 while (name[0] >= '0' && name[0] <= '9')
9076 {
9077 char *tail;
9078 unsigned long index = strtoul (name, &tail, 10);
9079 name = tail;
9080 if (*name != '$'
9081 || index >= field_type->num_fields ()
9082 || (field_type->field (index).loc_kind ()
9083 != FIELD_LOC_KIND_BITPOS))
9084 {
9085 complaint (_("Could not parse Rust enum encoding string \"%s\""
9086 "[in module %s]"),
9087 type->field (0).name (),
9088 objfile_name (objfile));
9089 return;
9090 }
9091 ++name;
9092
9093 bit_offset += field_type->field (index).loc_bitpos ();
9094 field_type = field_type->field (index).type ();
9095 }
9096
9097 /* Smash this type to be a structure type. We have to do this
9098 because the type has already been recorded. */
9099 type->set_code (TYPE_CODE_STRUCT);
9100 type->set_num_fields (3);
9101 /* Save the field we care about. */
9102 struct field saved_field = type->field (0);
9103 type->set_fields
9104 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9105
9106 /* Put the discriminant at index 0. */
9107 type->field (0).set_type (field_type);
9108 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9109 type->field (0).set_name ("<<discriminant>>");
9110 type->field (0).set_loc_bitpos (bit_offset);
9111
9112 /* The order of fields doesn't really matter, so put the real
9113 field at index 1 and the data-less field at index 2. */
9114 type->field (1) = saved_field;
9115 type->field (1).set_name
9116 (rust_last_path_segment (type->field (1).type ()->name ()));
9117 type->field (1).type ()->set_name
9118 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9119 type->field (1).name ()));
9120
9121 const char *dataless_name
9122 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9123 name);
9124 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9125 dataless_name);
9126 type->field (2).set_type (dataless_type);
9127 /* NAME points into the original discriminant name, which
9128 already has the correct lifetime. */
9129 type->field (2).set_name (name);
9130 type->field (2).set_loc_bitpos (0);
9131
9132 /* Indicate that this is a variant type. */
9133 static discriminant_range ranges[1] = { { 0, 0 } };
9134 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9135 }
9136 /* A union with a single anonymous field is probably an old-style
9137 univariant enum. */
9138 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
9139 {
9140 /* Smash this type to be a structure type. We have to do this
9141 because the type has already been recorded. */
9142 type->set_code (TYPE_CODE_STRUCT);
9143
9144 struct type *field_type = type->field (0).type ();
9145 const char *variant_name
9146 = rust_last_path_segment (field_type->name ());
9147 type->field (0).set_name (variant_name);
9148 field_type->set_name
9149 (rust_fully_qualify (&objfile->objfile_obstack,
9150 type->name (), variant_name));
9151
9152 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9153 }
9154 else
9155 {
9156 struct type *disr_type = nullptr;
9157 for (int i = 0; i < type->num_fields (); ++i)
9158 {
9159 disr_type = type->field (i).type ();
9160
9161 if (disr_type->code () != TYPE_CODE_STRUCT)
9162 {
9163 /* All fields of a true enum will be structs. */
9164 return;
9165 }
9166 else if (disr_type->num_fields () == 0)
9167 {
9168 /* Could be data-less variant, so keep going. */
9169 disr_type = nullptr;
9170 }
9171 else if (strcmp (disr_type->field (0).name (),
9172 "RUST$ENUM$DISR") != 0)
9173 {
9174 /* Not a Rust enum. */
9175 return;
9176 }
9177 else
9178 {
9179 /* Found one. */
9180 break;
9181 }
9182 }
9183
9184 /* If we got here without a discriminant, then it's probably
9185 just a union. */
9186 if (disr_type == nullptr)
9187 return;
9188
9189 /* Smash this type to be a structure type. We have to do this
9190 because the type has already been recorded. */
9191 type->set_code (TYPE_CODE_STRUCT);
9192
9193 /* Make space for the discriminant field. */
9194 struct field *disr_field = &disr_type->field (0);
9195 field *new_fields
9196 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9197 * sizeof (struct field)));
9198 memcpy (new_fields + 1, type->fields (),
9199 type->num_fields () * sizeof (struct field));
9200 type->set_fields (new_fields);
9201 type->set_num_fields (type->num_fields () + 1);
9202
9203 /* Install the discriminant at index 0 in the union. */
9204 type->field (0) = *disr_field;
9205 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9206 type->field (0).set_name ("<<discriminant>>");
9207
9208 /* We need a way to find the correct discriminant given a
9209 variant name. For convenience we build a map here. */
9210 struct type *enum_type = disr_field->type ();
9211 std::unordered_map<std::string, ULONGEST> discriminant_map;
9212 for (int i = 0; i < enum_type->num_fields (); ++i)
9213 {
9214 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
9215 {
9216 const char *name
9217 = rust_last_path_segment (enum_type->field (i).name ());
9218 discriminant_map[name] = enum_type->field (i).loc_enumval ();
9219 }
9220 }
9221
9222 int n_fields = type->num_fields ();
9223 /* We don't need a range entry for the discriminant, but we do
9224 need one for every other field, as there is no default
9225 variant. */
9226 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9227 discriminant_range,
9228 n_fields - 1);
9229 /* Skip the discriminant here. */
9230 for (int i = 1; i < n_fields; ++i)
9231 {
9232 /* Find the final word in the name of this variant's type.
9233 That name can be used to look up the correct
9234 discriminant. */
9235 const char *variant_name
9236 = rust_last_path_segment (type->field (i).type ()->name ());
9237
9238 auto iter = discriminant_map.find (variant_name);
9239 if (iter != discriminant_map.end ())
9240 {
9241 ranges[i - 1].low = iter->second;
9242 ranges[i - 1].high = iter->second;
9243 }
9244
9245 /* In Rust, each element should have the size of the
9246 enclosing enum. */
9247 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9248
9249 /* Remove the discriminant field, if it exists. */
9250 struct type *sub_type = type->field (i).type ();
9251 if (sub_type->num_fields () > 0)
9252 {
9253 sub_type->set_num_fields (sub_type->num_fields () - 1);
9254 sub_type->set_fields (sub_type->fields () + 1);
9255 }
9256 type->field (i).set_name (variant_name);
9257 sub_type->set_name
9258 (rust_fully_qualify (&objfile->objfile_obstack,
9259 type->name (), variant_name));
9260 }
9261
9262 /* Indicate that this is a variant type. */
9263 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9264 gdb::array_view<discriminant_range> (ranges,
9265 n_fields - 1));
9266 }
9267 }
9268
9269 /* Rewrite some Rust unions to be structures with variants parts. */
9270
9271 static void
9272 rust_union_quirks (struct dwarf2_cu *cu)
9273 {
9274 gdb_assert (cu->per_cu->lang == language_rust);
9275 for (type *type_ : cu->rust_unions)
9276 quirk_rust_enum (type_, cu->per_objfile->objfile);
9277 /* We don't need this any more. */
9278 cu->rust_unions.clear ();
9279 }
9280
9281 /* See read.h. */
9282
9283 type_unit_group_unshareable *
9284 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9285 {
9286 auto iter = this->m_type_units.find (tu_group);
9287 if (iter != this->m_type_units.end ())
9288 return iter->second.get ();
9289
9290 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9291 type_unit_group_unshareable *result = uniq.get ();
9292 this->m_type_units[tu_group] = std::move (uniq);
9293 return result;
9294 }
9295
9296 struct type *
9297 dwarf2_per_objfile::get_type_for_signatured_type
9298 (signatured_type *sig_type) const
9299 {
9300 auto iter = this->m_type_map.find (sig_type);
9301 if (iter == this->m_type_map.end ())
9302 return nullptr;
9303
9304 return iter->second;
9305 }
9306
9307 void dwarf2_per_objfile::set_type_for_signatured_type
9308 (signatured_type *sig_type, struct type *type)
9309 {
9310 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9311
9312 this->m_type_map[sig_type] = type;
9313 }
9314
9315 /* A helper function for computing the list of all symbol tables
9316 included by PER_CU. */
9317
9318 static void
9319 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9320 htab_t all_children, htab_t all_type_symtabs,
9321 dwarf2_per_cu_data *per_cu,
9322 dwarf2_per_objfile *per_objfile,
9323 struct compunit_symtab *immediate_parent)
9324 {
9325 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9326 if (*slot != NULL)
9327 {
9328 /* This inclusion and its children have been processed. */
9329 return;
9330 }
9331
9332 *slot = per_cu;
9333
9334 /* Only add a CU if it has a symbol table. */
9335 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9336 if (cust != NULL)
9337 {
9338 /* If this is a type unit only add its symbol table if we haven't
9339 seen it yet (type unit per_cu's can share symtabs). */
9340 if (per_cu->is_debug_types)
9341 {
9342 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9343 if (*slot == NULL)
9344 {
9345 *slot = cust;
9346 result->push_back (cust);
9347 if (cust->user == NULL)
9348 cust->user = immediate_parent;
9349 }
9350 }
9351 else
9352 {
9353 result->push_back (cust);
9354 if (cust->user == NULL)
9355 cust->user = immediate_parent;
9356 }
9357 }
9358
9359 if (!per_cu->imported_symtabs_empty ())
9360 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9361 {
9362 recursively_compute_inclusions (result, all_children,
9363 all_type_symtabs, ptr, per_objfile,
9364 cust);
9365 }
9366 }
9367
9368 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9369 PER_CU. */
9370
9371 static void
9372 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9373 dwarf2_per_objfile *per_objfile)
9374 {
9375 gdb_assert (! per_cu->is_debug_types);
9376
9377 if (!per_cu->imported_symtabs_empty ())
9378 {
9379 int len;
9380 std::vector<compunit_symtab *> result_symtabs;
9381 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9382
9383 /* If we don't have a symtab, we can just skip this case. */
9384 if (cust == NULL)
9385 return;
9386
9387 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9388 htab_eq_pointer,
9389 NULL, xcalloc, xfree));
9390 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9391 htab_eq_pointer,
9392 NULL, xcalloc, xfree));
9393
9394 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9395 {
9396 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9397 all_type_symtabs.get (), ptr,
9398 per_objfile, cust);
9399 }
9400
9401 /* Now we have a transitive closure of all the included symtabs. */
9402 len = result_symtabs.size ();
9403 cust->includes
9404 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9405 struct compunit_symtab *, len + 1);
9406 memcpy (cust->includes, result_symtabs.data (),
9407 len * sizeof (compunit_symtab *));
9408 cust->includes[len] = NULL;
9409 }
9410 }
9411
9412 /* Compute the 'includes' field for the symtabs of all the CUs we just
9413 read. */
9414
9415 static void
9416 process_cu_includes (dwarf2_per_objfile *per_objfile)
9417 {
9418 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9419 {
9420 if (! iter->is_debug_types)
9421 compute_compunit_symtab_includes (iter, per_objfile);
9422 }
9423
9424 per_objfile->per_bfd->just_read_cus.clear ();
9425 }
9426
9427 /* Generate full symbol information for CU, whose DIEs have
9428 already been loaded into memory. */
9429
9430 static void
9431 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9432 {
9433 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9434 struct objfile *objfile = per_objfile->objfile;
9435 struct gdbarch *gdbarch = objfile->arch ();
9436 CORE_ADDR lowpc, highpc;
9437 struct compunit_symtab *cust;
9438 CORE_ADDR baseaddr;
9439 struct block *static_block;
9440 CORE_ADDR addr;
9441
9442 baseaddr = objfile->text_section_offset ();
9443
9444 /* Clear the list here in case something was left over. */
9445 cu->method_list.clear ();
9446
9447 dwarf2_find_base_address (cu->dies, cu);
9448
9449 /* Before we start reading the top-level DIE, ensure it has a valid tag
9450 type. */
9451 switch (cu->dies->tag)
9452 {
9453 case DW_TAG_compile_unit:
9454 case DW_TAG_partial_unit:
9455 case DW_TAG_type_unit:
9456 break;
9457 default:
9458 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9459 dwarf_tag_name (cu->dies->tag),
9460 sect_offset_str (cu->per_cu->sect_off),
9461 objfile_name (per_objfile->objfile));
9462 }
9463
9464 /* Do line number decoding in read_file_scope () */
9465 process_die (cu->dies, cu);
9466
9467 /* For now fudge the Go package. */
9468 if (cu->per_cu->lang == language_go)
9469 fixup_go_packaging (cu);
9470
9471 /* Now that we have processed all the DIEs in the CU, all the types
9472 should be complete, and it should now be safe to compute all of the
9473 physnames. */
9474 compute_delayed_physnames (cu);
9475
9476 if (cu->per_cu->lang == language_rust)
9477 rust_union_quirks (cu);
9478
9479 /* Some compilers don't define a DW_AT_high_pc attribute for the
9480 compilation unit. If the DW_AT_high_pc is missing, synthesize
9481 it, by scanning the DIE's below the compilation unit. */
9482 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9483
9484 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9485 static_block
9486 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
9487
9488 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9489 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9490 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9491 addrmap to help ensure it has an accurate map of pc values belonging to
9492 this comp unit. */
9493 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9494
9495 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
9496 (static_block, SECT_OFF_TEXT (objfile), 0);
9497
9498 if (cust != NULL)
9499 {
9500 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9501
9502 /* Set symtab language to language from DW_AT_language. If the
9503 compilation is from a C file generated by language preprocessors, do
9504 not set the language if it was already deduced by start_subfile. */
9505 if (!(cu->per_cu->lang == language_c
9506 && cust->primary_filetab ()->language () != language_unknown))
9507 cust->primary_filetab ()->set_language (cu->per_cu->lang);
9508
9509 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9510 produce DW_AT_location with location lists but it can be possibly
9511 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9512 there were bugs in prologue debug info, fixed later in GCC-4.5
9513 by "unwind info for epilogues" patch (which is not directly related).
9514
9515 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9516 needed, it would be wrong due to missing DW_AT_producer there.
9517
9518 Still one can confuse GDB by using non-standard GCC compilation
9519 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9520 */
9521 if (cu->has_loclist && gcc_4_minor >= 5)
9522 cust->set_locations_valid (true);
9523
9524 if (gcc_4_minor >= 5)
9525 cust->set_epilogue_unwind_valid (true);
9526
9527 cust->set_call_site_htab (cu->call_site_htab);
9528 }
9529
9530 per_objfile->set_symtab (cu->per_cu, cust);
9531
9532 /* Push it for inclusion processing later. */
9533 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9534
9535 /* Not needed any more. */
9536 cu->reset_builder ();
9537 }
9538
9539 /* Generate full symbol information for type unit CU, whose DIEs have
9540 already been loaded into memory. */
9541
9542 static void
9543 process_full_type_unit (dwarf2_cu *cu,
9544 enum language pretend_language)
9545 {
9546 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9547 struct objfile *objfile = per_objfile->objfile;
9548 struct compunit_symtab *cust;
9549 struct signatured_type *sig_type;
9550
9551 gdb_assert (cu->per_cu->is_debug_types);
9552 sig_type = (struct signatured_type *) cu->per_cu;
9553
9554 /* Clear the list here in case something was left over. */
9555 cu->method_list.clear ();
9556
9557 /* The symbol tables are set up in read_type_unit_scope. */
9558 process_die (cu->dies, cu);
9559
9560 /* For now fudge the Go package. */
9561 if (cu->per_cu->lang == language_go)
9562 fixup_go_packaging (cu);
9563
9564 /* Now that we have processed all the DIEs in the CU, all the types
9565 should be complete, and it should now be safe to compute all of the
9566 physnames. */
9567 compute_delayed_physnames (cu);
9568
9569 if (cu->per_cu->lang == language_rust)
9570 rust_union_quirks (cu);
9571
9572 /* TUs share symbol tables.
9573 If this is the first TU to use this symtab, complete the construction
9574 of it with end_expandable_symtab. Otherwise, complete the addition of
9575 this TU's symbols to the existing symtab. */
9576 type_unit_group_unshareable *tug_unshare =
9577 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9578 if (tug_unshare->compunit_symtab == NULL)
9579 {
9580 buildsym_compunit *builder = cu->get_builder ();
9581 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9582 tug_unshare->compunit_symtab = cust;
9583
9584 if (cust != NULL)
9585 {
9586 /* Set symtab language to language from DW_AT_language. If the
9587 compilation is from a C file generated by language preprocessors,
9588 do not set the language if it was already deduced by
9589 start_subfile. */
9590 if (!(cu->per_cu->lang == language_c
9591 && cust->primary_filetab ()->language () != language_c))
9592 cust->primary_filetab ()->set_language (cu->per_cu->lang);
9593 }
9594 }
9595 else
9596 {
9597 cu->get_builder ()->augment_type_symtab ();
9598 cust = tug_unshare->compunit_symtab;
9599 }
9600
9601 per_objfile->set_symtab (cu->per_cu, cust);
9602
9603 /* Not needed any more. */
9604 cu->reset_builder ();
9605 }
9606
9607 /* Process an imported unit DIE. */
9608
9609 static void
9610 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9611 {
9612 struct attribute *attr;
9613
9614 /* For now we don't handle imported units in type units. */
9615 if (cu->per_cu->is_debug_types)
9616 {
9617 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9618 " supported in type units [in module %s]"),
9619 objfile_name (cu->per_objfile->objfile));
9620 }
9621
9622 attr = dwarf2_attr (die, DW_AT_import, cu);
9623 if (attr != NULL)
9624 {
9625 sect_offset sect_off = attr->get_ref_die_offset ();
9626 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9627 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9628 dwarf2_per_cu_data *per_cu
9629 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9630 per_objfile->per_bfd);
9631
9632 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9633 into another compilation unit, at root level. Regard this as a hint,
9634 and ignore it. */
9635 if (die->parent && die->parent->parent == NULL
9636 && per_cu->unit_type == DW_UT_compile
9637 && per_cu->lang == language_cplus)
9638 return;
9639
9640 /* If necessary, add it to the queue and load its DIEs. */
9641 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9642 cu->per_cu->lang))
9643 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9644 false, cu->per_cu->lang);
9645
9646 cu->per_cu->imported_symtabs_push (per_cu);
9647 }
9648 }
9649
9650 /* RAII object that represents a process_die scope: i.e.,
9651 starts/finishes processing a DIE. */
9652 class process_die_scope
9653 {
9654 public:
9655 process_die_scope (die_info *die, dwarf2_cu *cu)
9656 : m_die (die), m_cu (cu)
9657 {
9658 /* We should only be processing DIEs not already in process. */
9659 gdb_assert (!m_die->in_process);
9660 m_die->in_process = true;
9661 }
9662
9663 ~process_die_scope ()
9664 {
9665 m_die->in_process = false;
9666
9667 /* If we're done processing the DIE for the CU that owns the line
9668 header, we don't need the line header anymore. */
9669 if (m_cu->line_header_die_owner == m_die)
9670 {
9671 delete m_cu->line_header;
9672 m_cu->line_header = NULL;
9673 m_cu->line_header_die_owner = NULL;
9674 }
9675 }
9676
9677 private:
9678 die_info *m_die;
9679 dwarf2_cu *m_cu;
9680 };
9681
9682 /* Process a die and its children. */
9683
9684 static void
9685 process_die (struct die_info *die, struct dwarf2_cu *cu)
9686 {
9687 process_die_scope scope (die, cu);
9688
9689 switch (die->tag)
9690 {
9691 case DW_TAG_padding:
9692 break;
9693 case DW_TAG_compile_unit:
9694 case DW_TAG_partial_unit:
9695 read_file_scope (die, cu);
9696 break;
9697 case DW_TAG_type_unit:
9698 read_type_unit_scope (die, cu);
9699 break;
9700 case DW_TAG_subprogram:
9701 /* Nested subprograms in Fortran get a prefix. */
9702 if (cu->per_cu->lang == language_fortran
9703 && die->parent != NULL
9704 && die->parent->tag == DW_TAG_subprogram)
9705 cu->processing_has_namespace_info = true;
9706 /* Fall through. */
9707 case DW_TAG_inlined_subroutine:
9708 read_func_scope (die, cu);
9709 break;
9710 case DW_TAG_lexical_block:
9711 case DW_TAG_try_block:
9712 case DW_TAG_catch_block:
9713 read_lexical_block_scope (die, cu);
9714 break;
9715 case DW_TAG_call_site:
9716 case DW_TAG_GNU_call_site:
9717 read_call_site_scope (die, cu);
9718 break;
9719 case DW_TAG_class_type:
9720 case DW_TAG_interface_type:
9721 case DW_TAG_structure_type:
9722 case DW_TAG_union_type:
9723 case DW_TAG_namelist:
9724 process_structure_scope (die, cu);
9725 break;
9726 case DW_TAG_enumeration_type:
9727 process_enumeration_scope (die, cu);
9728 break;
9729
9730 /* These dies have a type, but processing them does not create
9731 a symbol or recurse to process the children. Therefore we can
9732 read them on-demand through read_type_die. */
9733 case DW_TAG_subroutine_type:
9734 case DW_TAG_set_type:
9735 case DW_TAG_pointer_type:
9736 case DW_TAG_ptr_to_member_type:
9737 case DW_TAG_reference_type:
9738 case DW_TAG_rvalue_reference_type:
9739 case DW_TAG_string_type:
9740 break;
9741
9742 case DW_TAG_array_type:
9743 /* We only need to handle this case for Ada -- in other
9744 languages, it's normal for the compiler to emit a typedef
9745 instead. */
9746 if (cu->per_cu->lang != language_ada)
9747 break;
9748 /* FALLTHROUGH */
9749 case DW_TAG_base_type:
9750 case DW_TAG_subrange_type:
9751 case DW_TAG_generic_subrange:
9752 case DW_TAG_typedef:
9753 /* Add a typedef symbol for the type definition, if it has a
9754 DW_AT_name. */
9755 new_symbol (die, read_type_die (die, cu), cu);
9756 break;
9757 case DW_TAG_common_block:
9758 read_common_block (die, cu);
9759 break;
9760 case DW_TAG_common_inclusion:
9761 break;
9762 case DW_TAG_namespace:
9763 cu->processing_has_namespace_info = true;
9764 read_namespace (die, cu);
9765 break;
9766 case DW_TAG_module:
9767 cu->processing_has_namespace_info = true;
9768 read_module (die, cu);
9769 break;
9770 case DW_TAG_imported_declaration:
9771 cu->processing_has_namespace_info = true;
9772 if (read_namespace_alias (die, cu))
9773 break;
9774 /* The declaration is not a global namespace alias. */
9775 /* Fall through. */
9776 case DW_TAG_imported_module:
9777 cu->processing_has_namespace_info = true;
9778 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9779 || cu->per_cu->lang != language_fortran))
9780 complaint (_("Tag '%s' has unexpected children"),
9781 dwarf_tag_name (die->tag));
9782 read_import_statement (die, cu);
9783 break;
9784
9785 case DW_TAG_imported_unit:
9786 process_imported_unit_die (die, cu);
9787 break;
9788
9789 case DW_TAG_variable:
9790 read_variable (die, cu);
9791 break;
9792
9793 default:
9794 new_symbol (die, NULL, cu);
9795 break;
9796 }
9797 }
9798 \f
9799 /* DWARF name computation. */
9800
9801 /* A helper function for dwarf2_compute_name which determines whether DIE
9802 needs to have the name of the scope prepended to the name listed in the
9803 die. */
9804
9805 static int
9806 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9807 {
9808 struct attribute *attr;
9809
9810 switch (die->tag)
9811 {
9812 case DW_TAG_namespace:
9813 case DW_TAG_typedef:
9814 case DW_TAG_class_type:
9815 case DW_TAG_interface_type:
9816 case DW_TAG_structure_type:
9817 case DW_TAG_union_type:
9818 case DW_TAG_enumeration_type:
9819 case DW_TAG_enumerator:
9820 case DW_TAG_subprogram:
9821 case DW_TAG_inlined_subroutine:
9822 case DW_TAG_member:
9823 case DW_TAG_imported_declaration:
9824 return 1;
9825
9826 case DW_TAG_variable:
9827 case DW_TAG_constant:
9828 /* We only need to prefix "globally" visible variables. These include
9829 any variable marked with DW_AT_external or any variable that
9830 lives in a namespace. [Variables in anonymous namespaces
9831 require prefixing, but they are not DW_AT_external.] */
9832
9833 if (dwarf2_attr (die, DW_AT_specification, cu))
9834 {
9835 struct dwarf2_cu *spec_cu = cu;
9836
9837 return die_needs_namespace (die_specification (die, &spec_cu),
9838 spec_cu);
9839 }
9840
9841 attr = dwarf2_attr (die, DW_AT_external, cu);
9842 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9843 && die->parent->tag != DW_TAG_module)
9844 return 0;
9845 /* A variable in a lexical block of some kind does not need a
9846 namespace, even though in C++ such variables may be external
9847 and have a mangled name. */
9848 if (die->parent->tag == DW_TAG_lexical_block
9849 || die->parent->tag == DW_TAG_try_block
9850 || die->parent->tag == DW_TAG_catch_block
9851 || die->parent->tag == DW_TAG_subprogram)
9852 return 0;
9853 return 1;
9854
9855 default:
9856 return 0;
9857 }
9858 }
9859
9860 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9861 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9862 defined for the given DIE. */
9863
9864 static struct attribute *
9865 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9866 {
9867 struct attribute *attr;
9868
9869 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9870 if (attr == NULL)
9871 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9872
9873 return attr;
9874 }
9875
9876 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9877 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9878 defined for the given DIE. */
9879
9880 static const char *
9881 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9882 {
9883 const char *linkage_name;
9884
9885 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9886 if (linkage_name == NULL)
9887 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9888
9889 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9890 See https://github.com/rust-lang/rust/issues/32925. */
9891 if (cu->per_cu->lang == language_rust && linkage_name != NULL
9892 && strchr (linkage_name, '{') != NULL)
9893 linkage_name = NULL;
9894
9895 return linkage_name;
9896 }
9897
9898 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9899 compute the physname for the object, which include a method's:
9900 - formal parameters (C++),
9901 - receiver type (Go),
9902
9903 The term "physname" is a bit confusing.
9904 For C++, for example, it is the demangled name.
9905 For Go, for example, it's the mangled name.
9906
9907 For Ada, return the DIE's linkage name rather than the fully qualified
9908 name. PHYSNAME is ignored..
9909
9910 The result is allocated on the objfile->per_bfd's obstack and
9911 canonicalized. */
9912
9913 static const char *
9914 dwarf2_compute_name (const char *name,
9915 struct die_info *die, struct dwarf2_cu *cu,
9916 int physname)
9917 {
9918 struct objfile *objfile = cu->per_objfile->objfile;
9919
9920 if (name == NULL)
9921 name = dwarf2_name (die, cu);
9922
9923 enum language lang = cu->per_cu->lang;
9924
9925 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9926 but otherwise compute it by typename_concat inside GDB.
9927 FIXME: Actually this is not really true, or at least not always true.
9928 It's all very confusing. compute_and_set_names doesn't try to demangle
9929 Fortran names because there is no mangling standard. So new_symbol
9930 will set the demangled name to the result of dwarf2_full_name, and it is
9931 the demangled name that GDB uses if it exists. */
9932 if (lang == language_ada
9933 || (lang == language_fortran && physname))
9934 {
9935 /* For Ada unit, we prefer the linkage name over the name, as
9936 the former contains the exported name, which the user expects
9937 to be able to reference. Ideally, we want the user to be able
9938 to reference this entity using either natural or linkage name,
9939 but we haven't started looking at this enhancement yet. */
9940 const char *linkage_name = dw2_linkage_name (die, cu);
9941
9942 if (linkage_name != NULL)
9943 return linkage_name;
9944 }
9945
9946 /* These are the only languages we know how to qualify names in. */
9947 if (name != NULL
9948 && (lang == language_cplus
9949 || lang == language_fortran || lang == language_d
9950 || lang == language_rust))
9951 {
9952 if (die_needs_namespace (die, cu))
9953 {
9954 const char *prefix;
9955 const char *canonical_name = NULL;
9956
9957 string_file buf;
9958
9959 prefix = determine_prefix (die, cu);
9960 if (*prefix != '\0')
9961 {
9962 gdb::unique_xmalloc_ptr<char> prefixed_name
9963 (typename_concat (NULL, prefix, name, physname, cu));
9964
9965 buf.puts (prefixed_name.get ());
9966 }
9967 else
9968 buf.puts (name);
9969
9970 /* Template parameters may be specified in the DIE's DW_AT_name, or
9971 as children with DW_TAG_template_type_param or
9972 DW_TAG_value_type_param. If the latter, add them to the name
9973 here. If the name already has template parameters, then
9974 skip this step; some versions of GCC emit both, and
9975 it is more efficient to use the pre-computed name.
9976
9977 Something to keep in mind about this process: it is very
9978 unlikely, or in some cases downright impossible, to produce
9979 something that will match the mangled name of a function.
9980 If the definition of the function has the same debug info,
9981 we should be able to match up with it anyway. But fallbacks
9982 using the minimal symbol, for instance to find a method
9983 implemented in a stripped copy of libstdc++, will not work.
9984 If we do not have debug info for the definition, we will have to
9985 match them up some other way.
9986
9987 When we do name matching there is a related problem with function
9988 templates; two instantiated function templates are allowed to
9989 differ only by their return types, which we do not add here. */
9990
9991 if (lang == language_cplus && strchr (name, '<') == NULL)
9992 {
9993 struct attribute *attr;
9994 struct die_info *child;
9995 int first = 1;
9996
9997 die->building_fullname = 1;
9998
9999 for (child = die->child; child != NULL; child = child->sibling)
10000 {
10001 struct type *type;
10002 LONGEST value;
10003 const gdb_byte *bytes;
10004 struct dwarf2_locexpr_baton *baton;
10005 struct value *v;
10006
10007 if (child->tag != DW_TAG_template_type_param
10008 && child->tag != DW_TAG_template_value_param)
10009 continue;
10010
10011 if (first)
10012 {
10013 buf.puts ("<");
10014 first = 0;
10015 }
10016 else
10017 buf.puts (", ");
10018
10019 attr = dwarf2_attr (child, DW_AT_type, cu);
10020 if (attr == NULL)
10021 {
10022 complaint (_("template parameter missing DW_AT_type"));
10023 buf.puts ("UNKNOWN_TYPE");
10024 continue;
10025 }
10026 type = die_type (child, cu);
10027
10028 if (child->tag == DW_TAG_template_type_param)
10029 {
10030 cu->language_defn->print_type (type, "", &buf, -1, 0,
10031 &type_print_raw_options);
10032 continue;
10033 }
10034
10035 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10036 if (attr == NULL)
10037 {
10038 complaint (_("template parameter missing "
10039 "DW_AT_const_value"));
10040 buf.puts ("UNKNOWN_VALUE");
10041 continue;
10042 }
10043
10044 dwarf2_const_value_attr (attr, type, name,
10045 &cu->comp_unit_obstack, cu,
10046 &value, &bytes, &baton);
10047
10048 if (type->has_no_signedness ())
10049 /* GDB prints characters as NUMBER 'CHAR'. If that's
10050 changed, this can use value_print instead. */
10051 cu->language_defn->printchar (value, type, &buf);
10052 else
10053 {
10054 struct value_print_options opts;
10055
10056 if (baton != NULL)
10057 v = dwarf2_evaluate_loc_desc (type, NULL,
10058 baton->data,
10059 baton->size,
10060 baton->per_cu,
10061 baton->per_objfile);
10062 else if (bytes != NULL)
10063 {
10064 v = allocate_value (type);
10065 memcpy (value_contents_writeable (v).data (), bytes,
10066 TYPE_LENGTH (type));
10067 }
10068 else
10069 v = value_from_longest (type, value);
10070
10071 /* Specify decimal so that we do not depend on
10072 the radix. */
10073 get_formatted_print_options (&opts, 'd');
10074 opts.raw = 1;
10075 value_print (v, &buf, &opts);
10076 release_value (v);
10077 }
10078 }
10079
10080 die->building_fullname = 0;
10081
10082 if (!first)
10083 {
10084 /* Close the argument list, with a space if necessary
10085 (nested templates). */
10086 if (!buf.empty () && buf.string ().back () == '>')
10087 buf.puts (" >");
10088 else
10089 buf.puts (">");
10090 }
10091 }
10092
10093 /* For C++ methods, append formal parameter type
10094 information, if PHYSNAME. */
10095
10096 if (physname && die->tag == DW_TAG_subprogram
10097 && lang == language_cplus)
10098 {
10099 struct type *type = read_type_die (die, cu);
10100
10101 c_type_print_args (type, &buf, 1, lang,
10102 &type_print_raw_options);
10103
10104 if (lang == language_cplus)
10105 {
10106 /* Assume that an artificial first parameter is
10107 "this", but do not crash if it is not. RealView
10108 marks unnamed (and thus unused) parameters as
10109 artificial; there is no way to differentiate
10110 the two cases. */
10111 if (type->num_fields () > 0
10112 && TYPE_FIELD_ARTIFICIAL (type, 0)
10113 && type->field (0).type ()->code () == TYPE_CODE_PTR
10114 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10115 buf.puts (" const");
10116 }
10117 }
10118
10119 const std::string &intermediate_name = buf.string ();
10120
10121 if (lang == language_cplus)
10122 canonical_name
10123 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10124 objfile);
10125
10126 /* If we only computed INTERMEDIATE_NAME, or if
10127 INTERMEDIATE_NAME is already canonical, then we need to
10128 intern it. */
10129 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10130 name = objfile->intern (intermediate_name);
10131 else
10132 name = canonical_name;
10133 }
10134 }
10135
10136 return name;
10137 }
10138
10139 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10140 If scope qualifiers are appropriate they will be added. The result
10141 will be allocated on the storage_obstack, or NULL if the DIE does
10142 not have a name. NAME may either be from a previous call to
10143 dwarf2_name or NULL.
10144
10145 The output string will be canonicalized (if C++). */
10146
10147 static const char *
10148 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10149 {
10150 return dwarf2_compute_name (name, die, cu, 0);
10151 }
10152
10153 /* Construct a physname for the given DIE in CU. NAME may either be
10154 from a previous call to dwarf2_name or NULL. The result will be
10155 allocated on the objfile_objstack or NULL if the DIE does not have a
10156 name.
10157
10158 The output string will be canonicalized (if C++). */
10159
10160 static const char *
10161 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10162 {
10163 struct objfile *objfile = cu->per_objfile->objfile;
10164 const char *retval, *mangled = NULL, *canon = NULL;
10165 int need_copy = 1;
10166
10167 /* In this case dwarf2_compute_name is just a shortcut not building anything
10168 on its own. */
10169 if (!die_needs_namespace (die, cu))
10170 return dwarf2_compute_name (name, die, cu, 1);
10171
10172 if (cu->per_cu->lang != language_rust)
10173 mangled = dw2_linkage_name (die, cu);
10174
10175 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10176 has computed. */
10177 gdb::unique_xmalloc_ptr<char> demangled;
10178 if (mangled != NULL)
10179 {
10180 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10181 {
10182 /* Do nothing (do not demangle the symbol name). */
10183 }
10184 else
10185 {
10186 /* Use DMGL_RET_DROP for C++ template functions to suppress
10187 their return type. It is easier for GDB users to search
10188 for such functions as `name(params)' than `long name(params)'.
10189 In such case the minimal symbol names do not match the full
10190 symbol names but for template functions there is never a need
10191 to look up their definition from their declaration so
10192 the only disadvantage remains the minimal symbol variant
10193 `long name(params)' does not have the proper inferior type. */
10194 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
10195 | DMGL_RET_DROP));
10196 }
10197 if (demangled)
10198 canon = demangled.get ();
10199 else
10200 {
10201 canon = mangled;
10202 need_copy = 0;
10203 }
10204 }
10205
10206 if (canon == NULL || check_physname)
10207 {
10208 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10209
10210 if (canon != NULL && strcmp (physname, canon) != 0)
10211 {
10212 /* It may not mean a bug in GDB. The compiler could also
10213 compute DW_AT_linkage_name incorrectly. But in such case
10214 GDB would need to be bug-to-bug compatible. */
10215
10216 complaint (_("Computed physname <%s> does not match demangled <%s> "
10217 "(from linkage <%s>) - DIE at %s [in module %s]"),
10218 physname, canon, mangled, sect_offset_str (die->sect_off),
10219 objfile_name (objfile));
10220
10221 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10222 is available here - over computed PHYSNAME. It is safer
10223 against both buggy GDB and buggy compilers. */
10224
10225 retval = canon;
10226 }
10227 else
10228 {
10229 retval = physname;
10230 need_copy = 0;
10231 }
10232 }
10233 else
10234 retval = canon;
10235
10236 if (need_copy)
10237 retval = objfile->intern (retval);
10238
10239 return retval;
10240 }
10241
10242 /* Inspect DIE in CU for a namespace alias. If one exists, record
10243 a new symbol for it.
10244
10245 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10246
10247 static int
10248 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10249 {
10250 struct attribute *attr;
10251
10252 /* If the die does not have a name, this is not a namespace
10253 alias. */
10254 attr = dwarf2_attr (die, DW_AT_name, cu);
10255 if (attr != NULL)
10256 {
10257 int num;
10258 struct die_info *d = die;
10259 struct dwarf2_cu *imported_cu = cu;
10260
10261 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10262 keep inspecting DIEs until we hit the underlying import. */
10263 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10264 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10265 {
10266 attr = dwarf2_attr (d, DW_AT_import, cu);
10267 if (attr == NULL)
10268 break;
10269
10270 d = follow_die_ref (d, attr, &imported_cu);
10271 if (d->tag != DW_TAG_imported_declaration)
10272 break;
10273 }
10274
10275 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10276 {
10277 complaint (_("DIE at %s has too many recursively imported "
10278 "declarations"), sect_offset_str (d->sect_off));
10279 return 0;
10280 }
10281
10282 if (attr != NULL)
10283 {
10284 struct type *type;
10285 sect_offset sect_off = attr->get_ref_die_offset ();
10286
10287 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10288 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10289 {
10290 /* This declaration is a global namespace alias. Add
10291 a symbol for it whose type is the aliased namespace. */
10292 new_symbol (die, type, cu);
10293 return 1;
10294 }
10295 }
10296 }
10297
10298 return 0;
10299 }
10300
10301 /* Return the using directives repository (global or local?) to use in the
10302 current context for CU.
10303
10304 For Ada, imported declarations can materialize renamings, which *may* be
10305 global. However it is impossible (for now?) in DWARF to distinguish
10306 "external" imported declarations and "static" ones. As all imported
10307 declarations seem to be static in all other languages, make them all CU-wide
10308 global only in Ada. */
10309
10310 static struct using_direct **
10311 using_directives (struct dwarf2_cu *cu)
10312 {
10313 if (cu->per_cu->lang == language_ada
10314 && cu->get_builder ()->outermost_context_p ())
10315 return cu->get_builder ()->get_global_using_directives ();
10316 else
10317 return cu->get_builder ()->get_local_using_directives ();
10318 }
10319
10320 /* Read the import statement specified by the given die and record it. */
10321
10322 static void
10323 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10324 {
10325 struct objfile *objfile = cu->per_objfile->objfile;
10326 struct attribute *import_attr;
10327 struct die_info *imported_die, *child_die;
10328 struct dwarf2_cu *imported_cu;
10329 const char *imported_name;
10330 const char *imported_name_prefix;
10331 const char *canonical_name;
10332 const char *import_alias;
10333 const char *imported_declaration = NULL;
10334 const char *import_prefix;
10335 std::vector<const char *> excludes;
10336
10337 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10338 if (import_attr == NULL)
10339 {
10340 complaint (_("Tag '%s' has no DW_AT_import"),
10341 dwarf_tag_name (die->tag));
10342 return;
10343 }
10344
10345 imported_cu = cu;
10346 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10347 imported_name = dwarf2_name (imported_die, imported_cu);
10348 if (imported_name == NULL)
10349 {
10350 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10351
10352 The import in the following code:
10353 namespace A
10354 {
10355 typedef int B;
10356 }
10357
10358 int main ()
10359 {
10360 using A::B;
10361 B b;
10362 return b;
10363 }
10364
10365 ...
10366 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10367 <52> DW_AT_decl_file : 1
10368 <53> DW_AT_decl_line : 6
10369 <54> DW_AT_import : <0x75>
10370 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10371 <59> DW_AT_name : B
10372 <5b> DW_AT_decl_file : 1
10373 <5c> DW_AT_decl_line : 2
10374 <5d> DW_AT_type : <0x6e>
10375 ...
10376 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10377 <76> DW_AT_byte_size : 4
10378 <77> DW_AT_encoding : 5 (signed)
10379
10380 imports the wrong die ( 0x75 instead of 0x58 ).
10381 This case will be ignored until the gcc bug is fixed. */
10382 return;
10383 }
10384
10385 /* Figure out the local name after import. */
10386 import_alias = dwarf2_name (die, cu);
10387
10388 /* Figure out where the statement is being imported to. */
10389 import_prefix = determine_prefix (die, cu);
10390
10391 /* Figure out what the scope of the imported die is and prepend it
10392 to the name of the imported die. */
10393 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10394
10395 if (imported_die->tag != DW_TAG_namespace
10396 && imported_die->tag != DW_TAG_module)
10397 {
10398 imported_declaration = imported_name;
10399 canonical_name = imported_name_prefix;
10400 }
10401 else if (strlen (imported_name_prefix) > 0)
10402 canonical_name = obconcat (&objfile->objfile_obstack,
10403 imported_name_prefix,
10404 (cu->per_cu->lang == language_d
10405 ? "."
10406 : "::"),
10407 imported_name, (char *) NULL);
10408 else
10409 canonical_name = imported_name;
10410
10411 if (die->tag == DW_TAG_imported_module
10412 && cu->per_cu->lang == language_fortran)
10413 for (child_die = die->child; child_die && child_die->tag;
10414 child_die = child_die->sibling)
10415 {
10416 /* DWARF-4: A Fortran use statement with a “rename list” may be
10417 represented by an imported module entry with an import attribute
10418 referring to the module and owned entries corresponding to those
10419 entities that are renamed as part of being imported. */
10420
10421 if (child_die->tag != DW_TAG_imported_declaration)
10422 {
10423 complaint (_("child DW_TAG_imported_declaration expected "
10424 "- DIE at %s [in module %s]"),
10425 sect_offset_str (child_die->sect_off),
10426 objfile_name (objfile));
10427 continue;
10428 }
10429
10430 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10431 if (import_attr == NULL)
10432 {
10433 complaint (_("Tag '%s' has no DW_AT_import"),
10434 dwarf_tag_name (child_die->tag));
10435 continue;
10436 }
10437
10438 imported_cu = cu;
10439 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10440 &imported_cu);
10441 imported_name = dwarf2_name (imported_die, imported_cu);
10442 if (imported_name == NULL)
10443 {
10444 complaint (_("child DW_TAG_imported_declaration has unknown "
10445 "imported name - DIE at %s [in module %s]"),
10446 sect_offset_str (child_die->sect_off),
10447 objfile_name (objfile));
10448 continue;
10449 }
10450
10451 excludes.push_back (imported_name);
10452
10453 process_die (child_die, cu);
10454 }
10455
10456 add_using_directive (using_directives (cu),
10457 import_prefix,
10458 canonical_name,
10459 import_alias,
10460 imported_declaration,
10461 excludes,
10462 0,
10463 &objfile->objfile_obstack);
10464 }
10465
10466 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10467 types, but gives them a size of zero. Starting with version 14,
10468 ICC is compatible with GCC. */
10469
10470 static bool
10471 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10472 {
10473 if (!cu->checked_producer)
10474 check_producer (cu);
10475
10476 return cu->producer_is_icc_lt_14;
10477 }
10478
10479 /* ICC generates a DW_AT_type for C void functions. This was observed on
10480 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10481 which says that void functions should not have a DW_AT_type. */
10482
10483 static bool
10484 producer_is_icc (struct dwarf2_cu *cu)
10485 {
10486 if (!cu->checked_producer)
10487 check_producer (cu);
10488
10489 return cu->producer_is_icc;
10490 }
10491
10492 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10493 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10494 this, it was first present in GCC release 4.3.0. */
10495
10496 static bool
10497 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10498 {
10499 if (!cu->checked_producer)
10500 check_producer (cu);
10501
10502 return cu->producer_is_gcc_lt_4_3;
10503 }
10504
10505 static file_and_directory &
10506 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10507 {
10508 if (cu->per_cu->fnd != nullptr)
10509 return *cu->per_cu->fnd;
10510
10511 /* Find the filename. Do not use dwarf2_name here, since the filename
10512 is not a source language identifier. */
10513 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
10514 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
10515
10516 if (res.get_comp_dir () == nullptr
10517 && producer_is_gcc_lt_4_3 (cu)
10518 && res.get_name () != nullptr
10519 && IS_ABSOLUTE_PATH (res.get_name ()))
10520 res.set_comp_dir (ldirname (res.get_name ()));
10521
10522 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
10523 return *cu->per_cu->fnd;
10524 }
10525
10526 /* Handle DW_AT_stmt_list for a compilation unit.
10527 DIE is the DW_TAG_compile_unit die for CU.
10528 COMP_DIR is the compilation directory. LOWPC is passed to
10529 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10530
10531 static void
10532 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10533 const file_and_directory &fnd, CORE_ADDR lowpc) /* ARI: editCase function */
10534 {
10535 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10536 struct attribute *attr;
10537 struct line_header line_header_local;
10538 hashval_t line_header_local_hash;
10539 void **slot;
10540 int decode_mapping;
10541
10542 gdb_assert (! cu->per_cu->is_debug_types);
10543
10544 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10545 if (attr == NULL || !attr->form_is_unsigned ())
10546 return;
10547
10548 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10549
10550 /* The line header hash table is only created if needed (it exists to
10551 prevent redundant reading of the line table for partial_units).
10552 If we're given a partial_unit, we'll need it. If we're given a
10553 compile_unit, then use the line header hash table if it's already
10554 created, but don't create one just yet. */
10555
10556 if (per_objfile->line_header_hash == NULL
10557 && die->tag == DW_TAG_partial_unit)
10558 {
10559 per_objfile->line_header_hash
10560 .reset (htab_create_alloc (127, line_header_hash_voidp,
10561 line_header_eq_voidp,
10562 htab_delete_entry<line_header>,
10563 xcalloc, xfree));
10564 }
10565
10566 line_header_local.sect_off = line_offset;
10567 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10568 line_header_local_hash = line_header_hash (&line_header_local);
10569 if (per_objfile->line_header_hash != NULL)
10570 {
10571 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10572 &line_header_local,
10573 line_header_local_hash, NO_INSERT);
10574
10575 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10576 is not present in *SLOT (since if there is something in *SLOT then
10577 it will be for a partial_unit). */
10578 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10579 {
10580 gdb_assert (*slot != NULL);
10581 cu->line_header = (struct line_header *) *slot;
10582 return;
10583 }
10584 }
10585
10586 /* dwarf_decode_line_header does not yet provide sufficient information.
10587 We always have to call also dwarf_decode_lines for it. */
10588 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10589 if (lh == NULL)
10590 return;
10591
10592 cu->line_header = lh.release ();
10593 cu->line_header_die_owner = die;
10594
10595 if (per_objfile->line_header_hash == NULL)
10596 slot = NULL;
10597 else
10598 {
10599 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10600 &line_header_local,
10601 line_header_local_hash, INSERT);
10602 gdb_assert (slot != NULL);
10603 }
10604 if (slot != NULL && *slot == NULL)
10605 {
10606 /* This newly decoded line number information unit will be owned
10607 by line_header_hash hash table. */
10608 *slot = cu->line_header;
10609 cu->line_header_die_owner = NULL;
10610 }
10611 else
10612 {
10613 /* We cannot free any current entry in (*slot) as that struct line_header
10614 may be already used by multiple CUs. Create only temporary decoded
10615 line_header for this CU - it may happen at most once for each line
10616 number information unit. And if we're not using line_header_hash
10617 then this is what we want as well. */
10618 gdb_assert (die->tag != DW_TAG_partial_unit);
10619 }
10620 decode_mapping = (die->tag != DW_TAG_partial_unit);
10621 dwarf_decode_lines (cu->line_header, fnd, cu, nullptr, lowpc,
10622 decode_mapping);
10623
10624 }
10625
10626 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10627
10628 static void
10629 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10630 {
10631 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10632 struct objfile *objfile = per_objfile->objfile;
10633 struct gdbarch *gdbarch = objfile->arch ();
10634 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10635 CORE_ADDR highpc = ((CORE_ADDR) 0);
10636 struct attribute *attr;
10637 struct die_info *child_die;
10638 CORE_ADDR baseaddr;
10639
10640 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
10641 baseaddr = objfile->text_section_offset ();
10642
10643 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10644
10645 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10646 from finish_block. */
10647 if (lowpc == ((CORE_ADDR) -1))
10648 lowpc = highpc;
10649 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10650
10651 file_and_directory &fnd = find_file_and_directory (die, cu);
10652
10653 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
10654 lowpc);
10655
10656 gdb_assert (per_objfile->sym_cu == nullptr);
10657 scoped_restore restore_sym_cu
10658 = make_scoped_restore (&per_objfile->sym_cu, cu);
10659
10660 /* Decode line number information if present. We do this before
10661 processing child DIEs, so that the line header table is available
10662 for DW_AT_decl_file. The PC check is here because, if LOWPC and
10663 HIGHPC are both 0x0, then there won't be any interesting code in
10664 the CU, but a check later on (in
10665 lnp_state_machine::check_line_address) will fail to properly
10666 exclude an entry that was removed via --gc-sections. */
10667 if (lowpc != highpc)
10668 handle_DW_AT_stmt_list (die, cu, fnd, lowpc);
10669
10670 /* Process all dies in compilation unit. */
10671 if (die->child != NULL)
10672 {
10673 child_die = die->child;
10674 while (child_die && child_die->tag)
10675 {
10676 process_die (child_die, cu);
10677 child_die = child_die->sibling;
10678 }
10679 }
10680 per_objfile->sym_cu = nullptr;
10681
10682 /* Decode macro information, if present. Dwarf 2 macro information
10683 refers to information in the line number info statement program
10684 header, so we can only read it if we've read the header
10685 successfully. */
10686 attr = dwarf2_attr (die, DW_AT_macros, cu);
10687 if (attr == NULL)
10688 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10689 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10690 {
10691 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10692 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10693
10694 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10695 }
10696 else
10697 {
10698 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10699 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10700 {
10701 unsigned int macro_offset = attr->as_unsigned ();
10702
10703 dwarf_decode_macros (cu, macro_offset, 0);
10704 }
10705 }
10706 }
10707
10708 void
10709 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10710 {
10711 struct type_unit_group *tu_group;
10712 int first_time;
10713 struct attribute *attr;
10714 unsigned int i;
10715 struct signatured_type *sig_type;
10716
10717 gdb_assert (per_cu->is_debug_types);
10718 sig_type = (struct signatured_type *) per_cu;
10719
10720 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10721
10722 /* If we're using .gdb_index (includes -readnow) then
10723 per_cu->type_unit_group may not have been set up yet. */
10724 if (sig_type->type_unit_group == NULL)
10725 sig_type->type_unit_group = get_type_unit_group (this, attr);
10726 tu_group = sig_type->type_unit_group;
10727
10728 /* If we've already processed this stmt_list there's no real need to
10729 do it again, we could fake it and just recreate the part we need
10730 (file name,index -> symtab mapping). If data shows this optimization
10731 is useful we can do it then. */
10732 type_unit_group_unshareable *tug_unshare
10733 = per_objfile->get_type_unit_group_unshareable (tu_group);
10734 first_time = tug_unshare->compunit_symtab == NULL;
10735
10736 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10737 debug info. */
10738 line_header_up lh;
10739 if (attr != NULL && attr->form_is_unsigned ())
10740 {
10741 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10742 lh = dwarf_decode_line_header (line_offset, this);
10743 }
10744 if (lh == NULL)
10745 {
10746 if (first_time)
10747 start_compunit_symtab ("", NULL, 0);
10748 else
10749 {
10750 gdb_assert (tug_unshare->symtabs == NULL);
10751 gdb_assert (m_builder == nullptr);
10752 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10753 m_builder.reset (new struct buildsym_compunit
10754 (cust->objfile (), "",
10755 cust->dirname (),
10756 compunit_language (cust),
10757 0, cust));
10758 list_in_scope = get_builder ()->get_file_symbols ();
10759 }
10760 return;
10761 }
10762
10763 line_header = lh.release ();
10764 line_header_die_owner = die;
10765
10766 if (first_time)
10767 {
10768 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
10769
10770 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10771 still initializing it, and our caller (a few levels up)
10772 process_full_type_unit still needs to know if this is the first
10773 time. */
10774
10775 tug_unshare->symtabs
10776 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
10777 struct symtab *, line_header->file_names_size ());
10778
10779 auto &file_names = line_header->file_names ();
10780 for (i = 0; i < file_names.size (); ++i)
10781 {
10782 file_entry &fe = file_names[i];
10783 dwarf2_start_subfile (this, fe.name,
10784 fe.include_dir (line_header));
10785 buildsym_compunit *b = get_builder ();
10786 if (b->get_current_subfile ()->symtab == NULL)
10787 {
10788 /* NOTE: start_subfile will recognize when it's been
10789 passed a file it has already seen. So we can't
10790 assume there's a simple mapping from
10791 cu->line_header->file_names to subfiles, plus
10792 cu->line_header->file_names may contain dups. */
10793 b->get_current_subfile ()->symtab
10794 = allocate_symtab (cust, b->get_current_subfile ()->name);
10795 }
10796
10797 fe.symtab = b->get_current_subfile ()->symtab;
10798 tug_unshare->symtabs[i] = fe.symtab;
10799 }
10800 }
10801 else
10802 {
10803 gdb_assert (m_builder == nullptr);
10804 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10805 m_builder.reset (new struct buildsym_compunit
10806 (cust->objfile (), "",
10807 cust->dirname (),
10808 compunit_language (cust),
10809 0, cust));
10810 list_in_scope = get_builder ()->get_file_symbols ();
10811
10812 auto &file_names = line_header->file_names ();
10813 for (i = 0; i < file_names.size (); ++i)
10814 {
10815 file_entry &fe = file_names[i];
10816 fe.symtab = tug_unshare->symtabs[i];
10817 }
10818 }
10819
10820 /* The main symtab is allocated last. Type units don't have DW_AT_name
10821 so they don't have a "real" (so to speak) symtab anyway.
10822 There is later code that will assign the main symtab to all symbols
10823 that don't have one. We need to handle the case of a symbol with a
10824 missing symtab (DW_AT_decl_file) anyway. */
10825 }
10826
10827 /* Process DW_TAG_type_unit.
10828 For TUs we want to skip the first top level sibling if it's not the
10829 actual type being defined by this TU. In this case the first top
10830 level sibling is there to provide context only. */
10831
10832 static void
10833 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10834 {
10835 struct die_info *child_die;
10836
10837 prepare_one_comp_unit (cu, die, language_minimal);
10838
10839 /* Initialize (or reinitialize) the machinery for building symtabs.
10840 We do this before processing child DIEs, so that the line header table
10841 is available for DW_AT_decl_file. */
10842 cu->setup_type_unit_groups (die);
10843
10844 if (die->child != NULL)
10845 {
10846 child_die = die->child;
10847 while (child_die && child_die->tag)
10848 {
10849 process_die (child_die, cu);
10850 child_die = child_die->sibling;
10851 }
10852 }
10853 }
10854 \f
10855 /* DWO/DWP files.
10856
10857 http://gcc.gnu.org/wiki/DebugFission
10858 http://gcc.gnu.org/wiki/DebugFissionDWP
10859
10860 To simplify handling of both DWO files ("object" files with the DWARF info)
10861 and DWP files (a file with the DWOs packaged up into one file), we treat
10862 DWP files as having a collection of virtual DWO files. */
10863
10864 static hashval_t
10865 hash_dwo_file (const void *item)
10866 {
10867 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10868 hashval_t hash;
10869
10870 hash = htab_hash_string (dwo_file->dwo_name);
10871 if (dwo_file->comp_dir != NULL)
10872 hash += htab_hash_string (dwo_file->comp_dir);
10873 return hash;
10874 }
10875
10876 static int
10877 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10878 {
10879 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10880 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10881
10882 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10883 return 0;
10884 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10885 return lhs->comp_dir == rhs->comp_dir;
10886 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10887 }
10888
10889 /* Allocate a hash table for DWO files. */
10890
10891 static htab_up
10892 allocate_dwo_file_hash_table ()
10893 {
10894 return htab_up (htab_create_alloc (41,
10895 hash_dwo_file,
10896 eq_dwo_file,
10897 htab_delete_entry<dwo_file>,
10898 xcalloc, xfree));
10899 }
10900
10901 /* Lookup DWO file DWO_NAME. */
10902
10903 static void **
10904 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10905 const char *dwo_name,
10906 const char *comp_dir)
10907 {
10908 struct dwo_file find_entry;
10909 void **slot;
10910
10911 if (per_objfile->per_bfd->dwo_files == NULL)
10912 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10913
10914 find_entry.dwo_name = dwo_name;
10915 find_entry.comp_dir = comp_dir;
10916 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10917 INSERT);
10918
10919 return slot;
10920 }
10921
10922 static hashval_t
10923 hash_dwo_unit (const void *item)
10924 {
10925 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10926
10927 /* This drops the top 32 bits of the id, but is ok for a hash. */
10928 return dwo_unit->signature;
10929 }
10930
10931 static int
10932 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10933 {
10934 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10935 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10936
10937 /* The signature is assumed to be unique within the DWO file.
10938 So while object file CU dwo_id's always have the value zero,
10939 that's OK, assuming each object file DWO file has only one CU,
10940 and that's the rule for now. */
10941 return lhs->signature == rhs->signature;
10942 }
10943
10944 /* Allocate a hash table for DWO CUs,TUs.
10945 There is one of these tables for each of CUs,TUs for each DWO file. */
10946
10947 static htab_up
10948 allocate_dwo_unit_table ()
10949 {
10950 /* Start out with a pretty small number.
10951 Generally DWO files contain only one CU and maybe some TUs. */
10952 return htab_up (htab_create_alloc (3,
10953 hash_dwo_unit,
10954 eq_dwo_unit,
10955 NULL, xcalloc, xfree));
10956 }
10957
10958 /* die_reader_func for create_dwo_cu. */
10959
10960 static void
10961 create_dwo_cu_reader (const struct die_reader_specs *reader,
10962 const gdb_byte *info_ptr,
10963 struct die_info *comp_unit_die,
10964 struct dwo_file *dwo_file,
10965 struct dwo_unit *dwo_unit)
10966 {
10967 struct dwarf2_cu *cu = reader->cu;
10968 sect_offset sect_off = cu->per_cu->sect_off;
10969 struct dwarf2_section_info *section = cu->per_cu->section;
10970
10971 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10972 if (!signature.has_value ())
10973 {
10974 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10975 " its dwo_id [in module %s]"),
10976 sect_offset_str (sect_off), dwo_file->dwo_name);
10977 return;
10978 }
10979
10980 dwo_unit->dwo_file = dwo_file;
10981 dwo_unit->signature = *signature;
10982 dwo_unit->section = section;
10983 dwo_unit->sect_off = sect_off;
10984 dwo_unit->length = cu->per_cu->length;
10985
10986 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10987 sect_offset_str (sect_off),
10988 hex_string (dwo_unit->signature));
10989 }
10990
10991 /* Create the dwo_units for the CUs in a DWO_FILE.
10992 Note: This function processes DWO files only, not DWP files. */
10993
10994 static void
10995 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10996 dwarf2_cu *cu, struct dwo_file &dwo_file,
10997 dwarf2_section_info &section, htab_up &cus_htab)
10998 {
10999 struct objfile *objfile = per_objfile->objfile;
11000 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11001 const gdb_byte *info_ptr, *end_ptr;
11002
11003 section.read (objfile);
11004 info_ptr = section.buffer;
11005
11006 if (info_ptr == NULL)
11007 return;
11008
11009 dwarf_read_debug_printf ("Reading %s for %s:",
11010 section.get_name (),
11011 section.get_file_name ());
11012
11013 end_ptr = info_ptr + section.size;
11014 while (info_ptr < end_ptr)
11015 {
11016 struct dwarf2_per_cu_data per_cu;
11017 struct dwo_unit read_unit {};
11018 struct dwo_unit *dwo_unit;
11019 void **slot;
11020 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11021
11022 per_cu.per_bfd = per_bfd;
11023 per_cu.is_debug_types = 0;
11024 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11025 per_cu.section = &section;
11026
11027 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11028 if (!reader.dummy_p)
11029 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11030 &dwo_file, &read_unit);
11031 info_ptr += per_cu.length;
11032
11033 // If the unit could not be parsed, skip it.
11034 if (read_unit.dwo_file == NULL)
11035 continue;
11036
11037 if (cus_htab == NULL)
11038 cus_htab = allocate_dwo_unit_table ();
11039
11040 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11041 struct dwo_unit);
11042 *dwo_unit = read_unit;
11043 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11044 gdb_assert (slot != NULL);
11045 if (*slot != NULL)
11046 {
11047 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11048 sect_offset dup_sect_off = dup_cu->sect_off;
11049
11050 complaint (_("debug cu entry at offset %s is duplicate to"
11051 " the entry at offset %s, signature %s"),
11052 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11053 hex_string (dwo_unit->signature));
11054 }
11055 *slot = (void *)dwo_unit;
11056 }
11057 }
11058
11059 /* DWP file .debug_{cu,tu}_index section format:
11060 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11061 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11062
11063 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11064 officially standard DWP format was published with DWARF v5 and is called
11065 Version 5. There are no versions 3 or 4.
11066
11067 DWP Version 1:
11068
11069 Both index sections have the same format, and serve to map a 64-bit
11070 signature to a set of section numbers. Each section begins with a header,
11071 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11072 indexes, and a pool of 32-bit section numbers. The index sections will be
11073 aligned at 8-byte boundaries in the file.
11074
11075 The index section header consists of:
11076
11077 V, 32 bit version number
11078 -, 32 bits unused
11079 N, 32 bit number of compilation units or type units in the index
11080 M, 32 bit number of slots in the hash table
11081
11082 Numbers are recorded using the byte order of the application binary.
11083
11084 The hash table begins at offset 16 in the section, and consists of an array
11085 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11086 order of the application binary). Unused slots in the hash table are 0.
11087 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11088
11089 The parallel table begins immediately after the hash table
11090 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11091 array of 32-bit indexes (using the byte order of the application binary),
11092 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11093 table contains a 32-bit index into the pool of section numbers. For unused
11094 hash table slots, the corresponding entry in the parallel table will be 0.
11095
11096 The pool of section numbers begins immediately following the hash table
11097 (at offset 16 + 12 * M from the beginning of the section). The pool of
11098 section numbers consists of an array of 32-bit words (using the byte order
11099 of the application binary). Each item in the array is indexed starting
11100 from 0. The hash table entry provides the index of the first section
11101 number in the set. Additional section numbers in the set follow, and the
11102 set is terminated by a 0 entry (section number 0 is not used in ELF).
11103
11104 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11105 section must be the first entry in the set, and the .debug_abbrev.dwo must
11106 be the second entry. Other members of the set may follow in any order.
11107
11108 ---
11109
11110 DWP Versions 2 and 5:
11111
11112 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11113 and the entries in the index tables are now offsets into these sections.
11114 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11115 section.
11116
11117 Index Section Contents:
11118 Header
11119 Hash Table of Signatures dwp_hash_table.hash_table
11120 Parallel Table of Indices dwp_hash_table.unit_table
11121 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11122 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11123
11124 The index section header consists of:
11125
11126 V, 32 bit version number
11127 L, 32 bit number of columns in the table of section offsets
11128 N, 32 bit number of compilation units or type units in the index
11129 M, 32 bit number of slots in the hash table
11130
11131 Numbers are recorded using the byte order of the application binary.
11132
11133 The hash table has the same format as version 1.
11134 The parallel table of indices has the same format as version 1,
11135 except that the entries are origin-1 indices into the table of sections
11136 offsets and the table of section sizes.
11137
11138 The table of offsets begins immediately following the parallel table
11139 (at offset 16 + 12 * M from the beginning of the section). The table is
11140 a two-dimensional array of 32-bit words (using the byte order of the
11141 application binary), with L columns and N+1 rows, in row-major order.
11142 Each row in the array is indexed starting from 0. The first row provides
11143 a key to the remaining rows: each column in this row provides an identifier
11144 for a debug section, and the offsets in the same column of subsequent rows
11145 refer to that section. The section identifiers for Version 2 are:
11146
11147 DW_SECT_INFO 1 .debug_info.dwo
11148 DW_SECT_TYPES 2 .debug_types.dwo
11149 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11150 DW_SECT_LINE 4 .debug_line.dwo
11151 DW_SECT_LOC 5 .debug_loc.dwo
11152 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11153 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11154 DW_SECT_MACRO 8 .debug_macro.dwo
11155
11156 The section identifiers for Version 5 are:
11157
11158 DW_SECT_INFO_V5 1 .debug_info.dwo
11159 DW_SECT_RESERVED_V5 2 --
11160 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11161 DW_SECT_LINE_V5 4 .debug_line.dwo
11162 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11163 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11164 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11165 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11166
11167 The offsets provided by the CU and TU index sections are the base offsets
11168 for the contributions made by each CU or TU to the corresponding section
11169 in the package file. Each CU and TU header contains an abbrev_offset
11170 field, used to find the abbreviations table for that CU or TU within the
11171 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11172 be interpreted as relative to the base offset given in the index section.
11173 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11174 should be interpreted as relative to the base offset for .debug_line.dwo,
11175 and offsets into other debug sections obtained from DWARF attributes should
11176 also be interpreted as relative to the corresponding base offset.
11177
11178 The table of sizes begins immediately following the table of offsets.
11179 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11180 with L columns and N rows, in row-major order. Each row in the array is
11181 indexed starting from 1 (row 0 is shared by the two tables).
11182
11183 ---
11184
11185 Hash table lookup is handled the same in version 1 and 2:
11186
11187 We assume that N and M will not exceed 2^32 - 1.
11188 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11189
11190 Given a 64-bit compilation unit signature or a type signature S, an entry
11191 in the hash table is located as follows:
11192
11193 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11194 the low-order k bits all set to 1.
11195
11196 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11197
11198 3) If the hash table entry at index H matches the signature, use that
11199 entry. If the hash table entry at index H is unused (all zeroes),
11200 terminate the search: the signature is not present in the table.
11201
11202 4) Let H = (H + H') modulo M. Repeat at Step 3.
11203
11204 Because M > N and H' and M are relatively prime, the search is guaranteed
11205 to stop at an unused slot or find the match. */
11206
11207 /* Create a hash table to map DWO IDs to their CU/TU entry in
11208 .debug_{info,types}.dwo in DWP_FILE.
11209 Returns NULL if there isn't one.
11210 Note: This function processes DWP files only, not DWO files. */
11211
11212 static struct dwp_hash_table *
11213 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11214 struct dwp_file *dwp_file, int is_debug_types)
11215 {
11216 struct objfile *objfile = per_objfile->objfile;
11217 bfd *dbfd = dwp_file->dbfd.get ();
11218 const gdb_byte *index_ptr, *index_end;
11219 struct dwarf2_section_info *index;
11220 uint32_t version, nr_columns, nr_units, nr_slots;
11221 struct dwp_hash_table *htab;
11222
11223 if (is_debug_types)
11224 index = &dwp_file->sections.tu_index;
11225 else
11226 index = &dwp_file->sections.cu_index;
11227
11228 if (index->empty ())
11229 return NULL;
11230 index->read (objfile);
11231
11232 index_ptr = index->buffer;
11233 index_end = index_ptr + index->size;
11234
11235 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11236 For now it's safe to just read 4 bytes (particularly as it's difficult to
11237 tell if you're dealing with Version 5 before you've read the version). */
11238 version = read_4_bytes (dbfd, index_ptr);
11239 index_ptr += 4;
11240 if (version == 2 || version == 5)
11241 nr_columns = read_4_bytes (dbfd, index_ptr);
11242 else
11243 nr_columns = 0;
11244 index_ptr += 4;
11245 nr_units = read_4_bytes (dbfd, index_ptr);
11246 index_ptr += 4;
11247 nr_slots = read_4_bytes (dbfd, index_ptr);
11248 index_ptr += 4;
11249
11250 if (version != 1 && version != 2 && version != 5)
11251 {
11252 error (_("Dwarf Error: unsupported DWP file version (%s)"
11253 " [in module %s]"),
11254 pulongest (version), dwp_file->name);
11255 }
11256 if (nr_slots != (nr_slots & -nr_slots))
11257 {
11258 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11259 " is not power of 2 [in module %s]"),
11260 pulongest (nr_slots), dwp_file->name);
11261 }
11262
11263 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11264 htab->version = version;
11265 htab->nr_columns = nr_columns;
11266 htab->nr_units = nr_units;
11267 htab->nr_slots = nr_slots;
11268 htab->hash_table = index_ptr;
11269 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11270
11271 /* Exit early if the table is empty. */
11272 if (nr_slots == 0 || nr_units == 0
11273 || (version == 2 && nr_columns == 0)
11274 || (version == 5 && nr_columns == 0))
11275 {
11276 /* All must be zero. */
11277 if (nr_slots != 0 || nr_units != 0
11278 || (version == 2 && nr_columns != 0)
11279 || (version == 5 && nr_columns != 0))
11280 {
11281 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11282 " all zero [in modules %s]"),
11283 dwp_file->name);
11284 }
11285 return htab;
11286 }
11287
11288 if (version == 1)
11289 {
11290 htab->section_pool.v1.indices =
11291 htab->unit_table + sizeof (uint32_t) * nr_slots;
11292 /* It's harder to decide whether the section is too small in v1.
11293 V1 is deprecated anyway so we punt. */
11294 }
11295 else if (version == 2)
11296 {
11297 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11298 int *ids = htab->section_pool.v2.section_ids;
11299 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11300 /* Reverse map for error checking. */
11301 int ids_seen[DW_SECT_MAX + 1];
11302 int i;
11303
11304 if (nr_columns < 2)
11305 {
11306 error (_("Dwarf Error: bad DWP hash table, too few columns"
11307 " in section table [in module %s]"),
11308 dwp_file->name);
11309 }
11310 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11311 {
11312 error (_("Dwarf Error: bad DWP hash table, too many columns"
11313 " in section table [in module %s]"),
11314 dwp_file->name);
11315 }
11316 memset (ids, 255, sizeof_ids);
11317 memset (ids_seen, 255, sizeof (ids_seen));
11318 for (i = 0; i < nr_columns; ++i)
11319 {
11320 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11321
11322 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11323 {
11324 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11325 " in section table [in module %s]"),
11326 id, dwp_file->name);
11327 }
11328 if (ids_seen[id] != -1)
11329 {
11330 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11331 " id %d in section table [in module %s]"),
11332 id, dwp_file->name);
11333 }
11334 ids_seen[id] = i;
11335 ids[i] = id;
11336 }
11337 /* Must have exactly one info or types section. */
11338 if (((ids_seen[DW_SECT_INFO] != -1)
11339 + (ids_seen[DW_SECT_TYPES] != -1))
11340 != 1)
11341 {
11342 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11343 " DWO info/types section [in module %s]"),
11344 dwp_file->name);
11345 }
11346 /* Must have an abbrev section. */
11347 if (ids_seen[DW_SECT_ABBREV] == -1)
11348 {
11349 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11350 " section [in module %s]"),
11351 dwp_file->name);
11352 }
11353 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11354 htab->section_pool.v2.sizes =
11355 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11356 * nr_units * nr_columns);
11357 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11358 * nr_units * nr_columns))
11359 > index_end)
11360 {
11361 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11362 " [in module %s]"),
11363 dwp_file->name);
11364 }
11365 }
11366 else /* version == 5 */
11367 {
11368 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11369 int *ids = htab->section_pool.v5.section_ids;
11370 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11371 /* Reverse map for error checking. */
11372 int ids_seen[DW_SECT_MAX_V5 + 1];
11373
11374 if (nr_columns < 2)
11375 {
11376 error (_("Dwarf Error: bad DWP hash table, too few columns"
11377 " in section table [in module %s]"),
11378 dwp_file->name);
11379 }
11380 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11381 {
11382 error (_("Dwarf Error: bad DWP hash table, too many columns"
11383 " in section table [in module %s]"),
11384 dwp_file->name);
11385 }
11386 memset (ids, 255, sizeof_ids);
11387 memset (ids_seen, 255, sizeof (ids_seen));
11388 for (int i = 0; i < nr_columns; ++i)
11389 {
11390 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11391
11392 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11393 {
11394 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11395 " in section table [in module %s]"),
11396 id, dwp_file->name);
11397 }
11398 if (ids_seen[id] != -1)
11399 {
11400 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11401 " id %d in section table [in module %s]"),
11402 id, dwp_file->name);
11403 }
11404 ids_seen[id] = i;
11405 ids[i] = id;
11406 }
11407 /* Must have seen an info section. */
11408 if (ids_seen[DW_SECT_INFO_V5] == -1)
11409 {
11410 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11411 " DWO info/types section [in module %s]"),
11412 dwp_file->name);
11413 }
11414 /* Must have an abbrev section. */
11415 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11416 {
11417 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11418 " section [in module %s]"),
11419 dwp_file->name);
11420 }
11421 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11422 htab->section_pool.v5.sizes
11423 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11424 * nr_units * nr_columns);
11425 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11426 * nr_units * nr_columns))
11427 > index_end)
11428 {
11429 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11430 " [in module %s]"),
11431 dwp_file->name);
11432 }
11433 }
11434
11435 return htab;
11436 }
11437
11438 /* Update SECTIONS with the data from SECTP.
11439
11440 This function is like the other "locate" section routines, but in
11441 this context the sections to read comes from the DWP V1 hash table,
11442 not the full ELF section table.
11443
11444 The result is non-zero for success, or zero if an error was found. */
11445
11446 static int
11447 locate_v1_virtual_dwo_sections (asection *sectp,
11448 struct virtual_v1_dwo_sections *sections)
11449 {
11450 const struct dwop_section_names *names = &dwop_section_names;
11451
11452 if (names->abbrev_dwo.matches (sectp->name))
11453 {
11454 /* There can be only one. */
11455 if (sections->abbrev.s.section != NULL)
11456 return 0;
11457 sections->abbrev.s.section = sectp;
11458 sections->abbrev.size = bfd_section_size (sectp);
11459 }
11460 else if (names->info_dwo.matches (sectp->name)
11461 || names->types_dwo.matches (sectp->name))
11462 {
11463 /* There can be only one. */
11464 if (sections->info_or_types.s.section != NULL)
11465 return 0;
11466 sections->info_or_types.s.section = sectp;
11467 sections->info_or_types.size = bfd_section_size (sectp);
11468 }
11469 else if (names->line_dwo.matches (sectp->name))
11470 {
11471 /* There can be only one. */
11472 if (sections->line.s.section != NULL)
11473 return 0;
11474 sections->line.s.section = sectp;
11475 sections->line.size = bfd_section_size (sectp);
11476 }
11477 else if (names->loc_dwo.matches (sectp->name))
11478 {
11479 /* There can be only one. */
11480 if (sections->loc.s.section != NULL)
11481 return 0;
11482 sections->loc.s.section = sectp;
11483 sections->loc.size = bfd_section_size (sectp);
11484 }
11485 else if (names->macinfo_dwo.matches (sectp->name))
11486 {
11487 /* There can be only one. */
11488 if (sections->macinfo.s.section != NULL)
11489 return 0;
11490 sections->macinfo.s.section = sectp;
11491 sections->macinfo.size = bfd_section_size (sectp);
11492 }
11493 else if (names->macro_dwo.matches (sectp->name))
11494 {
11495 /* There can be only one. */
11496 if (sections->macro.s.section != NULL)
11497 return 0;
11498 sections->macro.s.section = sectp;
11499 sections->macro.size = bfd_section_size (sectp);
11500 }
11501 else if (names->str_offsets_dwo.matches (sectp->name))
11502 {
11503 /* There can be only one. */
11504 if (sections->str_offsets.s.section != NULL)
11505 return 0;
11506 sections->str_offsets.s.section = sectp;
11507 sections->str_offsets.size = bfd_section_size (sectp);
11508 }
11509 else
11510 {
11511 /* No other kind of section is valid. */
11512 return 0;
11513 }
11514
11515 return 1;
11516 }
11517
11518 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11519 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11520 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11521 This is for DWP version 1 files. */
11522
11523 static struct dwo_unit *
11524 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11525 struct dwp_file *dwp_file,
11526 uint32_t unit_index,
11527 const char *comp_dir,
11528 ULONGEST signature, int is_debug_types)
11529 {
11530 const struct dwp_hash_table *dwp_htab =
11531 is_debug_types ? dwp_file->tus : dwp_file->cus;
11532 bfd *dbfd = dwp_file->dbfd.get ();
11533 const char *kind = is_debug_types ? "TU" : "CU";
11534 struct dwo_file *dwo_file;
11535 struct dwo_unit *dwo_unit;
11536 struct virtual_v1_dwo_sections sections;
11537 void **dwo_file_slot;
11538 int i;
11539
11540 gdb_assert (dwp_file->version == 1);
11541
11542 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11543 kind, pulongest (unit_index), hex_string (signature),
11544 dwp_file->name);
11545
11546 /* Fetch the sections of this DWO unit.
11547 Put a limit on the number of sections we look for so that bad data
11548 doesn't cause us to loop forever. */
11549
11550 #define MAX_NR_V1_DWO_SECTIONS \
11551 (1 /* .debug_info or .debug_types */ \
11552 + 1 /* .debug_abbrev */ \
11553 + 1 /* .debug_line */ \
11554 + 1 /* .debug_loc */ \
11555 + 1 /* .debug_str_offsets */ \
11556 + 1 /* .debug_macro or .debug_macinfo */ \
11557 + 1 /* trailing zero */)
11558
11559 memset (&sections, 0, sizeof (sections));
11560
11561 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11562 {
11563 asection *sectp;
11564 uint32_t section_nr =
11565 read_4_bytes (dbfd,
11566 dwp_htab->section_pool.v1.indices
11567 + (unit_index + i) * sizeof (uint32_t));
11568
11569 if (section_nr == 0)
11570 break;
11571 if (section_nr >= dwp_file->num_sections)
11572 {
11573 error (_("Dwarf Error: bad DWP hash table, section number too large"
11574 " [in module %s]"),
11575 dwp_file->name);
11576 }
11577
11578 sectp = dwp_file->elf_sections[section_nr];
11579 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11580 {
11581 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11582 " [in module %s]"),
11583 dwp_file->name);
11584 }
11585 }
11586
11587 if (i < 2
11588 || sections.info_or_types.empty ()
11589 || sections.abbrev.empty ())
11590 {
11591 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11592 " [in module %s]"),
11593 dwp_file->name);
11594 }
11595 if (i == MAX_NR_V1_DWO_SECTIONS)
11596 {
11597 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11598 " [in module %s]"),
11599 dwp_file->name);
11600 }
11601
11602 /* It's easier for the rest of the code if we fake a struct dwo_file and
11603 have dwo_unit "live" in that. At least for now.
11604
11605 The DWP file can be made up of a random collection of CUs and TUs.
11606 However, for each CU + set of TUs that came from the same original DWO
11607 file, we can combine them back into a virtual DWO file to save space
11608 (fewer struct dwo_file objects to allocate). Remember that for really
11609 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11610
11611 std::string virtual_dwo_name =
11612 string_printf ("virtual-dwo/%d-%d-%d-%d",
11613 sections.abbrev.get_id (),
11614 sections.line.get_id (),
11615 sections.loc.get_id (),
11616 sections.str_offsets.get_id ());
11617 /* Can we use an existing virtual DWO file? */
11618 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11619 comp_dir);
11620 /* Create one if necessary. */
11621 if (*dwo_file_slot == NULL)
11622 {
11623 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11624 virtual_dwo_name.c_str ());
11625
11626 dwo_file = new struct dwo_file;
11627 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11628 dwo_file->comp_dir = comp_dir;
11629 dwo_file->sections.abbrev = sections.abbrev;
11630 dwo_file->sections.line = sections.line;
11631 dwo_file->sections.loc = sections.loc;
11632 dwo_file->sections.macinfo = sections.macinfo;
11633 dwo_file->sections.macro = sections.macro;
11634 dwo_file->sections.str_offsets = sections.str_offsets;
11635 /* The "str" section is global to the entire DWP file. */
11636 dwo_file->sections.str = dwp_file->sections.str;
11637 /* The info or types section is assigned below to dwo_unit,
11638 there's no need to record it in dwo_file.
11639 Also, we can't simply record type sections in dwo_file because
11640 we record a pointer into the vector in dwo_unit. As we collect more
11641 types we'll grow the vector and eventually have to reallocate space
11642 for it, invalidating all copies of pointers into the previous
11643 contents. */
11644 *dwo_file_slot = dwo_file;
11645 }
11646 else
11647 {
11648 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11649 virtual_dwo_name.c_str ());
11650
11651 dwo_file = (struct dwo_file *) *dwo_file_slot;
11652 }
11653
11654 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11655 dwo_unit->dwo_file = dwo_file;
11656 dwo_unit->signature = signature;
11657 dwo_unit->section =
11658 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11659 *dwo_unit->section = sections.info_or_types;
11660 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11661
11662 return dwo_unit;
11663 }
11664
11665 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11666 simplify them. Given a pointer to the containing section SECTION, and
11667 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11668 virtual section of just that piece. */
11669
11670 static struct dwarf2_section_info
11671 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11672 struct dwarf2_section_info *section,
11673 bfd_size_type offset, bfd_size_type size)
11674 {
11675 struct dwarf2_section_info result;
11676 asection *sectp;
11677
11678 gdb_assert (section != NULL);
11679 gdb_assert (!section->is_virtual);
11680
11681 memset (&result, 0, sizeof (result));
11682 result.s.containing_section = section;
11683 result.is_virtual = true;
11684
11685 if (size == 0)
11686 return result;
11687
11688 sectp = section->get_bfd_section ();
11689
11690 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11691 bounds of the real section. This is a pretty-rare event, so just
11692 flag an error (easier) instead of a warning and trying to cope. */
11693 if (sectp == NULL
11694 || offset + size > bfd_section_size (sectp))
11695 {
11696 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11697 " in section %s [in module %s]"),
11698 sectp ? bfd_section_name (sectp) : "<unknown>",
11699 objfile_name (per_objfile->objfile));
11700 }
11701
11702 result.virtual_offset = offset;
11703 result.size = size;
11704 return result;
11705 }
11706
11707 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11708 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11709 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11710 This is for DWP version 2 files. */
11711
11712 static struct dwo_unit *
11713 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11714 struct dwp_file *dwp_file,
11715 uint32_t unit_index,
11716 const char *comp_dir,
11717 ULONGEST signature, int is_debug_types)
11718 {
11719 const struct dwp_hash_table *dwp_htab =
11720 is_debug_types ? dwp_file->tus : dwp_file->cus;
11721 bfd *dbfd = dwp_file->dbfd.get ();
11722 const char *kind = is_debug_types ? "TU" : "CU";
11723 struct dwo_file *dwo_file;
11724 struct dwo_unit *dwo_unit;
11725 struct virtual_v2_or_v5_dwo_sections sections;
11726 void **dwo_file_slot;
11727 int i;
11728
11729 gdb_assert (dwp_file->version == 2);
11730
11731 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11732 kind, pulongest (unit_index), hex_string (signature),
11733 dwp_file->name);
11734
11735 /* Fetch the section offsets of this DWO unit. */
11736
11737 memset (&sections, 0, sizeof (sections));
11738
11739 for (i = 0; i < dwp_htab->nr_columns; ++i)
11740 {
11741 uint32_t offset = read_4_bytes (dbfd,
11742 dwp_htab->section_pool.v2.offsets
11743 + (((unit_index - 1) * dwp_htab->nr_columns
11744 + i)
11745 * sizeof (uint32_t)));
11746 uint32_t size = read_4_bytes (dbfd,
11747 dwp_htab->section_pool.v2.sizes
11748 + (((unit_index - 1) * dwp_htab->nr_columns
11749 + i)
11750 * sizeof (uint32_t)));
11751
11752 switch (dwp_htab->section_pool.v2.section_ids[i])
11753 {
11754 case DW_SECT_INFO:
11755 case DW_SECT_TYPES:
11756 sections.info_or_types_offset = offset;
11757 sections.info_or_types_size = size;
11758 break;
11759 case DW_SECT_ABBREV:
11760 sections.abbrev_offset = offset;
11761 sections.abbrev_size = size;
11762 break;
11763 case DW_SECT_LINE:
11764 sections.line_offset = offset;
11765 sections.line_size = size;
11766 break;
11767 case DW_SECT_LOC:
11768 sections.loc_offset = offset;
11769 sections.loc_size = size;
11770 break;
11771 case DW_SECT_STR_OFFSETS:
11772 sections.str_offsets_offset = offset;
11773 sections.str_offsets_size = size;
11774 break;
11775 case DW_SECT_MACINFO:
11776 sections.macinfo_offset = offset;
11777 sections.macinfo_size = size;
11778 break;
11779 case DW_SECT_MACRO:
11780 sections.macro_offset = offset;
11781 sections.macro_size = size;
11782 break;
11783 }
11784 }
11785
11786 /* It's easier for the rest of the code if we fake a struct dwo_file and
11787 have dwo_unit "live" in that. At least for now.
11788
11789 The DWP file can be made up of a random collection of CUs and TUs.
11790 However, for each CU + set of TUs that came from the same original DWO
11791 file, we can combine them back into a virtual DWO file to save space
11792 (fewer struct dwo_file objects to allocate). Remember that for really
11793 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11794
11795 std::string virtual_dwo_name =
11796 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11797 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11798 (long) (sections.line_size ? sections.line_offset : 0),
11799 (long) (sections.loc_size ? sections.loc_offset : 0),
11800 (long) (sections.str_offsets_size
11801 ? sections.str_offsets_offset : 0));
11802 /* Can we use an existing virtual DWO file? */
11803 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11804 comp_dir);
11805 /* Create one if necessary. */
11806 if (*dwo_file_slot == NULL)
11807 {
11808 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11809 virtual_dwo_name.c_str ());
11810
11811 dwo_file = new struct dwo_file;
11812 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11813 dwo_file->comp_dir = comp_dir;
11814 dwo_file->sections.abbrev =
11815 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11816 sections.abbrev_offset,
11817 sections.abbrev_size);
11818 dwo_file->sections.line =
11819 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11820 sections.line_offset,
11821 sections.line_size);
11822 dwo_file->sections.loc =
11823 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11824 sections.loc_offset, sections.loc_size);
11825 dwo_file->sections.macinfo =
11826 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11827 sections.macinfo_offset,
11828 sections.macinfo_size);
11829 dwo_file->sections.macro =
11830 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11831 sections.macro_offset,
11832 sections.macro_size);
11833 dwo_file->sections.str_offsets =
11834 create_dwp_v2_or_v5_section (per_objfile,
11835 &dwp_file->sections.str_offsets,
11836 sections.str_offsets_offset,
11837 sections.str_offsets_size);
11838 /* The "str" section is global to the entire DWP file. */
11839 dwo_file->sections.str = dwp_file->sections.str;
11840 /* The info or types section is assigned below to dwo_unit,
11841 there's no need to record it in dwo_file.
11842 Also, we can't simply record type sections in dwo_file because
11843 we record a pointer into the vector in dwo_unit. As we collect more
11844 types we'll grow the vector and eventually have to reallocate space
11845 for it, invalidating all copies of pointers into the previous
11846 contents. */
11847 *dwo_file_slot = dwo_file;
11848 }
11849 else
11850 {
11851 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11852 virtual_dwo_name.c_str ());
11853
11854 dwo_file = (struct dwo_file *) *dwo_file_slot;
11855 }
11856
11857 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11858 dwo_unit->dwo_file = dwo_file;
11859 dwo_unit->signature = signature;
11860 dwo_unit->section =
11861 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11862 *dwo_unit->section = create_dwp_v2_or_v5_section
11863 (per_objfile,
11864 is_debug_types
11865 ? &dwp_file->sections.types
11866 : &dwp_file->sections.info,
11867 sections.info_or_types_offset,
11868 sections.info_or_types_size);
11869 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11870
11871 return dwo_unit;
11872 }
11873
11874 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11875 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11876 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11877 This is for DWP version 5 files. */
11878
11879 static struct dwo_unit *
11880 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11881 struct dwp_file *dwp_file,
11882 uint32_t unit_index,
11883 const char *comp_dir,
11884 ULONGEST signature, int is_debug_types)
11885 {
11886 const struct dwp_hash_table *dwp_htab
11887 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11888 bfd *dbfd = dwp_file->dbfd.get ();
11889 const char *kind = is_debug_types ? "TU" : "CU";
11890 struct dwo_file *dwo_file;
11891 struct dwo_unit *dwo_unit;
11892 struct virtual_v2_or_v5_dwo_sections sections {};
11893 void **dwo_file_slot;
11894
11895 gdb_assert (dwp_file->version == 5);
11896
11897 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11898 kind, pulongest (unit_index), hex_string (signature),
11899 dwp_file->name);
11900
11901 /* Fetch the section offsets of this DWO unit. */
11902
11903 /* memset (&sections, 0, sizeof (sections)); */
11904
11905 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11906 {
11907 uint32_t offset = read_4_bytes (dbfd,
11908 dwp_htab->section_pool.v5.offsets
11909 + (((unit_index - 1)
11910 * dwp_htab->nr_columns
11911 + i)
11912 * sizeof (uint32_t)));
11913 uint32_t size = read_4_bytes (dbfd,
11914 dwp_htab->section_pool.v5.sizes
11915 + (((unit_index - 1) * dwp_htab->nr_columns
11916 + i)
11917 * sizeof (uint32_t)));
11918
11919 switch (dwp_htab->section_pool.v5.section_ids[i])
11920 {
11921 case DW_SECT_ABBREV_V5:
11922 sections.abbrev_offset = offset;
11923 sections.abbrev_size = size;
11924 break;
11925 case DW_SECT_INFO_V5:
11926 sections.info_or_types_offset = offset;
11927 sections.info_or_types_size = size;
11928 break;
11929 case DW_SECT_LINE_V5:
11930 sections.line_offset = offset;
11931 sections.line_size = size;
11932 break;
11933 case DW_SECT_LOCLISTS_V5:
11934 sections.loclists_offset = offset;
11935 sections.loclists_size = size;
11936 break;
11937 case DW_SECT_MACRO_V5:
11938 sections.macro_offset = offset;
11939 sections.macro_size = size;
11940 break;
11941 case DW_SECT_RNGLISTS_V5:
11942 sections.rnglists_offset = offset;
11943 sections.rnglists_size = size;
11944 break;
11945 case DW_SECT_STR_OFFSETS_V5:
11946 sections.str_offsets_offset = offset;
11947 sections.str_offsets_size = size;
11948 break;
11949 case DW_SECT_RESERVED_V5:
11950 default:
11951 break;
11952 }
11953 }
11954
11955 /* It's easier for the rest of the code if we fake a struct dwo_file and
11956 have dwo_unit "live" in that. At least for now.
11957
11958 The DWP file can be made up of a random collection of CUs and TUs.
11959 However, for each CU + set of TUs that came from the same original DWO
11960 file, we can combine them back into a virtual DWO file to save space
11961 (fewer struct dwo_file objects to allocate). Remember that for really
11962 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11963
11964 std::string virtual_dwo_name =
11965 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11966 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11967 (long) (sections.line_size ? sections.line_offset : 0),
11968 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11969 (long) (sections.str_offsets_size
11970 ? sections.str_offsets_offset : 0),
11971 (long) (sections.macro_size ? sections.macro_offset : 0),
11972 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11973 /* Can we use an existing virtual DWO file? */
11974 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11975 virtual_dwo_name.c_str (),
11976 comp_dir);
11977 /* Create one if necessary. */
11978 if (*dwo_file_slot == NULL)
11979 {
11980 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11981 virtual_dwo_name.c_str ());
11982
11983 dwo_file = new struct dwo_file;
11984 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11985 dwo_file->comp_dir = comp_dir;
11986 dwo_file->sections.abbrev =
11987 create_dwp_v2_or_v5_section (per_objfile,
11988 &dwp_file->sections.abbrev,
11989 sections.abbrev_offset,
11990 sections.abbrev_size);
11991 dwo_file->sections.line =
11992 create_dwp_v2_or_v5_section (per_objfile,
11993 &dwp_file->sections.line,
11994 sections.line_offset, sections.line_size);
11995 dwo_file->sections.macro =
11996 create_dwp_v2_or_v5_section (per_objfile,
11997 &dwp_file->sections.macro,
11998 sections.macro_offset,
11999 sections.macro_size);
12000 dwo_file->sections.loclists =
12001 create_dwp_v2_or_v5_section (per_objfile,
12002 &dwp_file->sections.loclists,
12003 sections.loclists_offset,
12004 sections.loclists_size);
12005 dwo_file->sections.rnglists =
12006 create_dwp_v2_or_v5_section (per_objfile,
12007 &dwp_file->sections.rnglists,
12008 sections.rnglists_offset,
12009 sections.rnglists_size);
12010 dwo_file->sections.str_offsets =
12011 create_dwp_v2_or_v5_section (per_objfile,
12012 &dwp_file->sections.str_offsets,
12013 sections.str_offsets_offset,
12014 sections.str_offsets_size);
12015 /* The "str" section is global to the entire DWP file. */
12016 dwo_file->sections.str = dwp_file->sections.str;
12017 /* The info or types section is assigned below to dwo_unit,
12018 there's no need to record it in dwo_file.
12019 Also, we can't simply record type sections in dwo_file because
12020 we record a pointer into the vector in dwo_unit. As we collect more
12021 types we'll grow the vector and eventually have to reallocate space
12022 for it, invalidating all copies of pointers into the previous
12023 contents. */
12024 *dwo_file_slot = dwo_file;
12025 }
12026 else
12027 {
12028 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12029 virtual_dwo_name.c_str ());
12030
12031 dwo_file = (struct dwo_file *) *dwo_file_slot;
12032 }
12033
12034 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12035 dwo_unit->dwo_file = dwo_file;
12036 dwo_unit->signature = signature;
12037 dwo_unit->section
12038 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12039 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12040 &dwp_file->sections.info,
12041 sections.info_or_types_offset,
12042 sections.info_or_types_size);
12043 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12044
12045 return dwo_unit;
12046 }
12047
12048 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12049 Returns NULL if the signature isn't found. */
12050
12051 static struct dwo_unit *
12052 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12053 struct dwp_file *dwp_file, const char *comp_dir,
12054 ULONGEST signature, int is_debug_types)
12055 {
12056 const struct dwp_hash_table *dwp_htab =
12057 is_debug_types ? dwp_file->tus : dwp_file->cus;
12058 bfd *dbfd = dwp_file->dbfd.get ();
12059 uint32_t mask = dwp_htab->nr_slots - 1;
12060 uint32_t hash = signature & mask;
12061 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12062 unsigned int i;
12063 void **slot;
12064 struct dwo_unit find_dwo_cu;
12065
12066 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12067 find_dwo_cu.signature = signature;
12068 slot = htab_find_slot (is_debug_types
12069 ? dwp_file->loaded_tus.get ()
12070 : dwp_file->loaded_cus.get (),
12071 &find_dwo_cu, INSERT);
12072
12073 if (*slot != NULL)
12074 return (struct dwo_unit *) *slot;
12075
12076 /* Use a for loop so that we don't loop forever on bad debug info. */
12077 for (i = 0; i < dwp_htab->nr_slots; ++i)
12078 {
12079 ULONGEST signature_in_table;
12080
12081 signature_in_table =
12082 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12083 if (signature_in_table == signature)
12084 {
12085 uint32_t unit_index =
12086 read_4_bytes (dbfd,
12087 dwp_htab->unit_table + hash * sizeof (uint32_t));
12088
12089 if (dwp_file->version == 1)
12090 {
12091 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12092 unit_index, comp_dir,
12093 signature, is_debug_types);
12094 }
12095 else if (dwp_file->version == 2)
12096 {
12097 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12098 unit_index, comp_dir,
12099 signature, is_debug_types);
12100 }
12101 else /* version == 5 */
12102 {
12103 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12104 unit_index, comp_dir,
12105 signature, is_debug_types);
12106 }
12107 return (struct dwo_unit *) *slot;
12108 }
12109 if (signature_in_table == 0)
12110 return NULL;
12111 hash = (hash + hash2) & mask;
12112 }
12113
12114 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12115 " [in module %s]"),
12116 dwp_file->name);
12117 }
12118
12119 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12120 Open the file specified by FILE_NAME and hand it off to BFD for
12121 preliminary analysis. Return a newly initialized bfd *, which
12122 includes a canonicalized copy of FILE_NAME.
12123 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12124 SEARCH_CWD is true if the current directory is to be searched.
12125 It will be searched before debug-file-directory.
12126 If successful, the file is added to the bfd include table of the
12127 objfile's bfd (see gdb_bfd_record_inclusion).
12128 If unable to find/open the file, return NULL.
12129 NOTE: This function is derived from symfile_bfd_open. */
12130
12131 static gdb_bfd_ref_ptr
12132 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12133 const char *file_name, int is_dwp, int search_cwd)
12134 {
12135 int desc;
12136 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12137 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12138 to debug_file_directory. */
12139 const char *search_path;
12140 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12141
12142 gdb::unique_xmalloc_ptr<char> search_path_holder;
12143 if (search_cwd)
12144 {
12145 if (!debug_file_directory.empty ())
12146 {
12147 search_path_holder.reset (concat (".", dirname_separator_string,
12148 debug_file_directory.c_str (),
12149 (char *) NULL));
12150 search_path = search_path_holder.get ();
12151 }
12152 else
12153 search_path = ".";
12154 }
12155 else
12156 search_path = debug_file_directory.c_str ();
12157
12158 /* Add the path for the executable binary to the list of search paths. */
12159 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12160 search_path_holder.reset (concat (objfile_dir.c_str (),
12161 dirname_separator_string,
12162 search_path, nullptr));
12163 search_path = search_path_holder.get ();
12164
12165 openp_flags flags = OPF_RETURN_REALPATH;
12166 if (is_dwp)
12167 flags |= OPF_SEARCH_IN_PATH;
12168
12169 gdb::unique_xmalloc_ptr<char> absolute_name;
12170 desc = openp (search_path, flags, file_name,
12171 O_RDONLY | O_BINARY, &absolute_name);
12172 if (desc < 0)
12173 return NULL;
12174
12175 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12176 gnutarget, desc));
12177 if (sym_bfd == NULL)
12178 return NULL;
12179 bfd_set_cacheable (sym_bfd.get (), 1);
12180
12181 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12182 return NULL;
12183
12184 /* Success. Record the bfd as having been included by the objfile's bfd.
12185 This is important because things like demangled_names_hash lives in the
12186 objfile's per_bfd space and may have references to things like symbol
12187 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12188 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12189
12190 return sym_bfd;
12191 }
12192
12193 /* Try to open DWO file FILE_NAME.
12194 COMP_DIR is the DW_AT_comp_dir attribute.
12195 The result is the bfd handle of the file.
12196 If there is a problem finding or opening the file, return NULL.
12197 Upon success, the canonicalized path of the file is stored in the bfd,
12198 same as symfile_bfd_open. */
12199
12200 static gdb_bfd_ref_ptr
12201 open_dwo_file (dwarf2_per_objfile *per_objfile,
12202 const char *file_name, const char *comp_dir)
12203 {
12204 if (IS_ABSOLUTE_PATH (file_name))
12205 return try_open_dwop_file (per_objfile, file_name,
12206 0 /*is_dwp*/, 0 /*search_cwd*/);
12207
12208 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12209
12210 if (comp_dir != NULL)
12211 {
12212 gdb::unique_xmalloc_ptr<char> path_to_try
12213 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12214
12215 /* NOTE: If comp_dir is a relative path, this will also try the
12216 search path, which seems useful. */
12217 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12218 0 /*is_dwp*/,
12219 1 /*search_cwd*/));
12220 if (abfd != NULL)
12221 return abfd;
12222 }
12223
12224 /* That didn't work, try debug-file-directory, which, despite its name,
12225 is a list of paths. */
12226
12227 if (debug_file_directory.empty ())
12228 return NULL;
12229
12230 return try_open_dwop_file (per_objfile, file_name,
12231 0 /*is_dwp*/, 1 /*search_cwd*/);
12232 }
12233
12234 /* This function is mapped across the sections and remembers the offset and
12235 size of each of the DWO debugging sections we are interested in. */
12236
12237 static void
12238 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12239 dwo_sections *dwo_sections)
12240 {
12241 const struct dwop_section_names *names = &dwop_section_names;
12242
12243 if (names->abbrev_dwo.matches (sectp->name))
12244 {
12245 dwo_sections->abbrev.s.section = sectp;
12246 dwo_sections->abbrev.size = bfd_section_size (sectp);
12247 }
12248 else if (names->info_dwo.matches (sectp->name))
12249 {
12250 dwo_sections->info.s.section = sectp;
12251 dwo_sections->info.size = bfd_section_size (sectp);
12252 }
12253 else if (names->line_dwo.matches (sectp->name))
12254 {
12255 dwo_sections->line.s.section = sectp;
12256 dwo_sections->line.size = bfd_section_size (sectp);
12257 }
12258 else if (names->loc_dwo.matches (sectp->name))
12259 {
12260 dwo_sections->loc.s.section = sectp;
12261 dwo_sections->loc.size = bfd_section_size (sectp);
12262 }
12263 else if (names->loclists_dwo.matches (sectp->name))
12264 {
12265 dwo_sections->loclists.s.section = sectp;
12266 dwo_sections->loclists.size = bfd_section_size (sectp);
12267 }
12268 else if (names->macinfo_dwo.matches (sectp->name))
12269 {
12270 dwo_sections->macinfo.s.section = sectp;
12271 dwo_sections->macinfo.size = bfd_section_size (sectp);
12272 }
12273 else if (names->macro_dwo.matches (sectp->name))
12274 {
12275 dwo_sections->macro.s.section = sectp;
12276 dwo_sections->macro.size = bfd_section_size (sectp);
12277 }
12278 else if (names->rnglists_dwo.matches (sectp->name))
12279 {
12280 dwo_sections->rnglists.s.section = sectp;
12281 dwo_sections->rnglists.size = bfd_section_size (sectp);
12282 }
12283 else if (names->str_dwo.matches (sectp->name))
12284 {
12285 dwo_sections->str.s.section = sectp;
12286 dwo_sections->str.size = bfd_section_size (sectp);
12287 }
12288 else if (names->str_offsets_dwo.matches (sectp->name))
12289 {
12290 dwo_sections->str_offsets.s.section = sectp;
12291 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12292 }
12293 else if (names->types_dwo.matches (sectp->name))
12294 {
12295 struct dwarf2_section_info type_section;
12296
12297 memset (&type_section, 0, sizeof (type_section));
12298 type_section.s.section = sectp;
12299 type_section.size = bfd_section_size (sectp);
12300 dwo_sections->types.push_back (type_section);
12301 }
12302 }
12303
12304 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12305 by PER_CU. This is for the non-DWP case.
12306 The result is NULL if DWO_NAME can't be found. */
12307
12308 static struct dwo_file *
12309 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12310 const char *comp_dir)
12311 {
12312 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12313
12314 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12315 if (dbfd == NULL)
12316 {
12317 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12318
12319 return NULL;
12320 }
12321
12322 dwo_file_up dwo_file (new struct dwo_file);
12323 dwo_file->dwo_name = dwo_name;
12324 dwo_file->comp_dir = comp_dir;
12325 dwo_file->dbfd = std::move (dbfd);
12326
12327 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12328 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12329 &dwo_file->sections);
12330
12331 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12332 dwo_file->cus);
12333
12334 if (cu->per_cu->dwarf_version < 5)
12335 {
12336 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12337 dwo_file->sections.types, dwo_file->tus);
12338 }
12339 else
12340 {
12341 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12342 &dwo_file->sections.info, dwo_file->tus,
12343 rcuh_kind::COMPILE);
12344 }
12345
12346 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12347
12348 return dwo_file.release ();
12349 }
12350
12351 /* This function is mapped across the sections and remembers the offset and
12352 size of each of the DWP debugging sections common to version 1 and 2 that
12353 we are interested in. */
12354
12355 static void
12356 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12357 dwp_file *dwp_file)
12358 {
12359 const struct dwop_section_names *names = &dwop_section_names;
12360 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12361
12362 /* Record the ELF section number for later lookup: this is what the
12363 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12364 gdb_assert (elf_section_nr < dwp_file->num_sections);
12365 dwp_file->elf_sections[elf_section_nr] = sectp;
12366
12367 /* Look for specific sections that we need. */
12368 if (names->str_dwo.matches (sectp->name))
12369 {
12370 dwp_file->sections.str.s.section = sectp;
12371 dwp_file->sections.str.size = bfd_section_size (sectp);
12372 }
12373 else if (names->cu_index.matches (sectp->name))
12374 {
12375 dwp_file->sections.cu_index.s.section = sectp;
12376 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12377 }
12378 else if (names->tu_index.matches (sectp->name))
12379 {
12380 dwp_file->sections.tu_index.s.section = sectp;
12381 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12382 }
12383 }
12384
12385 /* This function is mapped across the sections and remembers the offset and
12386 size of each of the DWP version 2 debugging sections that we are interested
12387 in. This is split into a separate function because we don't know if we
12388 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12389
12390 static void
12391 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12392 {
12393 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12394 const struct dwop_section_names *names = &dwop_section_names;
12395 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12396
12397 /* Record the ELF section number for later lookup: this is what the
12398 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12399 gdb_assert (elf_section_nr < dwp_file->num_sections);
12400 dwp_file->elf_sections[elf_section_nr] = sectp;
12401
12402 /* Look for specific sections that we need. */
12403 if (names->abbrev_dwo.matches (sectp->name))
12404 {
12405 dwp_file->sections.abbrev.s.section = sectp;
12406 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12407 }
12408 else if (names->info_dwo.matches (sectp->name))
12409 {
12410 dwp_file->sections.info.s.section = sectp;
12411 dwp_file->sections.info.size = bfd_section_size (sectp);
12412 }
12413 else if (names->line_dwo.matches (sectp->name))
12414 {
12415 dwp_file->sections.line.s.section = sectp;
12416 dwp_file->sections.line.size = bfd_section_size (sectp);
12417 }
12418 else if (names->loc_dwo.matches (sectp->name))
12419 {
12420 dwp_file->sections.loc.s.section = sectp;
12421 dwp_file->sections.loc.size = bfd_section_size (sectp);
12422 }
12423 else if (names->macinfo_dwo.matches (sectp->name))
12424 {
12425 dwp_file->sections.macinfo.s.section = sectp;
12426 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12427 }
12428 else if (names->macro_dwo.matches (sectp->name))
12429 {
12430 dwp_file->sections.macro.s.section = sectp;
12431 dwp_file->sections.macro.size = bfd_section_size (sectp);
12432 }
12433 else if (names->str_offsets_dwo.matches (sectp->name))
12434 {
12435 dwp_file->sections.str_offsets.s.section = sectp;
12436 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12437 }
12438 else if (names->types_dwo.matches (sectp->name))
12439 {
12440 dwp_file->sections.types.s.section = sectp;
12441 dwp_file->sections.types.size = bfd_section_size (sectp);
12442 }
12443 }
12444
12445 /* This function is mapped across the sections and remembers the offset and
12446 size of each of the DWP version 5 debugging sections that we are interested
12447 in. This is split into a separate function because we don't know if we
12448 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12449
12450 static void
12451 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12452 {
12453 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12454 const struct dwop_section_names *names = &dwop_section_names;
12455 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12456
12457 /* Record the ELF section number for later lookup: this is what the
12458 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12459 gdb_assert (elf_section_nr < dwp_file->num_sections);
12460 dwp_file->elf_sections[elf_section_nr] = sectp;
12461
12462 /* Look for specific sections that we need. */
12463 if (names->abbrev_dwo.matches (sectp->name))
12464 {
12465 dwp_file->sections.abbrev.s.section = sectp;
12466 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12467 }
12468 else if (names->info_dwo.matches (sectp->name))
12469 {
12470 dwp_file->sections.info.s.section = sectp;
12471 dwp_file->sections.info.size = bfd_section_size (sectp);
12472 }
12473 else if (names->line_dwo.matches (sectp->name))
12474 {
12475 dwp_file->sections.line.s.section = sectp;
12476 dwp_file->sections.line.size = bfd_section_size (sectp);
12477 }
12478 else if (names->loclists_dwo.matches (sectp->name))
12479 {
12480 dwp_file->sections.loclists.s.section = sectp;
12481 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12482 }
12483 else if (names->macro_dwo.matches (sectp->name))
12484 {
12485 dwp_file->sections.macro.s.section = sectp;
12486 dwp_file->sections.macro.size = bfd_section_size (sectp);
12487 }
12488 else if (names->rnglists_dwo.matches (sectp->name))
12489 {
12490 dwp_file->sections.rnglists.s.section = sectp;
12491 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12492 }
12493 else if (names->str_offsets_dwo.matches (sectp->name))
12494 {
12495 dwp_file->sections.str_offsets.s.section = sectp;
12496 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12497 }
12498 }
12499
12500 /* Hash function for dwp_file loaded CUs/TUs. */
12501
12502 static hashval_t
12503 hash_dwp_loaded_cutus (const void *item)
12504 {
12505 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12506
12507 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12508 return dwo_unit->signature;
12509 }
12510
12511 /* Equality function for dwp_file loaded CUs/TUs. */
12512
12513 static int
12514 eq_dwp_loaded_cutus (const void *a, const void *b)
12515 {
12516 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12517 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12518
12519 return dua->signature == dub->signature;
12520 }
12521
12522 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12523
12524 static htab_up
12525 allocate_dwp_loaded_cutus_table ()
12526 {
12527 return htab_up (htab_create_alloc (3,
12528 hash_dwp_loaded_cutus,
12529 eq_dwp_loaded_cutus,
12530 NULL, xcalloc, xfree));
12531 }
12532
12533 /* Try to open DWP file FILE_NAME.
12534 The result is the bfd handle of the file.
12535 If there is a problem finding or opening the file, return NULL.
12536 Upon success, the canonicalized path of the file is stored in the bfd,
12537 same as symfile_bfd_open. */
12538
12539 static gdb_bfd_ref_ptr
12540 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12541 {
12542 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12543 1 /*is_dwp*/,
12544 1 /*search_cwd*/));
12545 if (abfd != NULL)
12546 return abfd;
12547
12548 /* Work around upstream bug 15652.
12549 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12550 [Whether that's a "bug" is debatable, but it is getting in our way.]
12551 We have no real idea where the dwp file is, because gdb's realpath-ing
12552 of the executable's path may have discarded the needed info.
12553 [IWBN if the dwp file name was recorded in the executable, akin to
12554 .gnu_debuglink, but that doesn't exist yet.]
12555 Strip the directory from FILE_NAME and search again. */
12556 if (!debug_file_directory.empty ())
12557 {
12558 /* Don't implicitly search the current directory here.
12559 If the user wants to search "." to handle this case,
12560 it must be added to debug-file-directory. */
12561 return try_open_dwop_file (per_objfile, lbasename (file_name),
12562 1 /*is_dwp*/,
12563 0 /*search_cwd*/);
12564 }
12565
12566 return NULL;
12567 }
12568
12569 /* Initialize the use of the DWP file for the current objfile.
12570 By convention the name of the DWP file is ${objfile}.dwp.
12571 The result is NULL if it can't be found. */
12572
12573 static std::unique_ptr<struct dwp_file>
12574 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12575 {
12576 struct objfile *objfile = per_objfile->objfile;
12577
12578 /* Try to find first .dwp for the binary file before any symbolic links
12579 resolving. */
12580
12581 /* If the objfile is a debug file, find the name of the real binary
12582 file and get the name of dwp file from there. */
12583 std::string dwp_name;
12584 if (objfile->separate_debug_objfile_backlink != NULL)
12585 {
12586 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12587 const char *backlink_basename = lbasename (backlink->original_name);
12588
12589 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12590 }
12591 else
12592 dwp_name = objfile->original_name;
12593
12594 dwp_name += ".dwp";
12595
12596 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12597 if (dbfd == NULL
12598 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12599 {
12600 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12601 dwp_name = objfile_name (objfile);
12602 dwp_name += ".dwp";
12603 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12604 }
12605
12606 if (dbfd == NULL)
12607 {
12608 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12609
12610 return std::unique_ptr<dwp_file> ();
12611 }
12612
12613 const char *name = bfd_get_filename (dbfd.get ());
12614 std::unique_ptr<struct dwp_file> dwp_file
12615 (new struct dwp_file (name, std::move (dbfd)));
12616
12617 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12618 dwp_file->elf_sections =
12619 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12620 dwp_file->num_sections, asection *);
12621
12622 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12623 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12624 dwp_file.get ());
12625
12626 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12627
12628 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12629
12630 /* The DWP file version is stored in the hash table. Oh well. */
12631 if (dwp_file->cus && dwp_file->tus
12632 && dwp_file->cus->version != dwp_file->tus->version)
12633 {
12634 /* Technically speaking, we should try to limp along, but this is
12635 pretty bizarre. We use pulongest here because that's the established
12636 portability solution (e.g, we cannot use %u for uint32_t). */
12637 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12638 " TU version %s [in DWP file %s]"),
12639 pulongest (dwp_file->cus->version),
12640 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12641 }
12642
12643 if (dwp_file->cus)
12644 dwp_file->version = dwp_file->cus->version;
12645 else if (dwp_file->tus)
12646 dwp_file->version = dwp_file->tus->version;
12647 else
12648 dwp_file->version = 2;
12649
12650 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12651 {
12652 if (dwp_file->version == 2)
12653 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12654 dwp_file.get ());
12655 else
12656 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12657 dwp_file.get ());
12658 }
12659
12660 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12661 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12662
12663 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12664 dwarf_read_debug_printf (" %s CUs, %s TUs",
12665 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12666 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12667
12668 return dwp_file;
12669 }
12670
12671 /* Wrapper around open_and_init_dwp_file, only open it once. */
12672
12673 static struct dwp_file *
12674 get_dwp_file (dwarf2_per_objfile *per_objfile)
12675 {
12676 if (!per_objfile->per_bfd->dwp_checked)
12677 {
12678 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12679 per_objfile->per_bfd->dwp_checked = 1;
12680 }
12681 return per_objfile->per_bfd->dwp_file.get ();
12682 }
12683
12684 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12685 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12686 or in the DWP file for the objfile, referenced by THIS_UNIT.
12687 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12688 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12689
12690 This is called, for example, when wanting to read a variable with a
12691 complex location. Therefore we don't want to do file i/o for every call.
12692 Therefore we don't want to look for a DWO file on every call.
12693 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12694 then we check if we've already seen DWO_NAME, and only THEN do we check
12695 for a DWO file.
12696
12697 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12698 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12699
12700 static struct dwo_unit *
12701 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12702 ULONGEST signature, int is_debug_types)
12703 {
12704 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12705 struct objfile *objfile = per_objfile->objfile;
12706 const char *kind = is_debug_types ? "TU" : "CU";
12707 void **dwo_file_slot;
12708 struct dwo_file *dwo_file;
12709 struct dwp_file *dwp_file;
12710
12711 /* First see if there's a DWP file.
12712 If we have a DWP file but didn't find the DWO inside it, don't
12713 look for the original DWO file. It makes gdb behave differently
12714 depending on whether one is debugging in the build tree. */
12715
12716 dwp_file = get_dwp_file (per_objfile);
12717 if (dwp_file != NULL)
12718 {
12719 const struct dwp_hash_table *dwp_htab =
12720 is_debug_types ? dwp_file->tus : dwp_file->cus;
12721
12722 if (dwp_htab != NULL)
12723 {
12724 struct dwo_unit *dwo_cutu =
12725 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12726 is_debug_types);
12727
12728 if (dwo_cutu != NULL)
12729 {
12730 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12731 kind, hex_string (signature),
12732 host_address_to_string (dwo_cutu));
12733
12734 return dwo_cutu;
12735 }
12736 }
12737 }
12738 else
12739 {
12740 /* No DWP file, look for the DWO file. */
12741
12742 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12743 if (*dwo_file_slot == NULL)
12744 {
12745 /* Read in the file and build a table of the CUs/TUs it contains. */
12746 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12747 }
12748 /* NOTE: This will be NULL if unable to open the file. */
12749 dwo_file = (struct dwo_file *) *dwo_file_slot;
12750
12751 if (dwo_file != NULL)
12752 {
12753 struct dwo_unit *dwo_cutu = NULL;
12754
12755 if (is_debug_types && dwo_file->tus)
12756 {
12757 struct dwo_unit find_dwo_cutu;
12758
12759 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12760 find_dwo_cutu.signature = signature;
12761 dwo_cutu
12762 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12763 &find_dwo_cutu);
12764 }
12765 else if (!is_debug_types && dwo_file->cus)
12766 {
12767 struct dwo_unit find_dwo_cutu;
12768
12769 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12770 find_dwo_cutu.signature = signature;
12771 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12772 &find_dwo_cutu);
12773 }
12774
12775 if (dwo_cutu != NULL)
12776 {
12777 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12778 kind, dwo_name, hex_string (signature),
12779 host_address_to_string (dwo_cutu));
12780
12781 return dwo_cutu;
12782 }
12783 }
12784 }
12785
12786 /* We didn't find it. This could mean a dwo_id mismatch, or
12787 someone deleted the DWO/DWP file, or the search path isn't set up
12788 correctly to find the file. */
12789
12790 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12791 kind, dwo_name, hex_string (signature));
12792
12793 /* This is a warning and not a complaint because it can be caused by
12794 pilot error (e.g., user accidentally deleting the DWO). */
12795 {
12796 /* Print the name of the DWP file if we looked there, helps the user
12797 better diagnose the problem. */
12798 std::string dwp_text;
12799
12800 if (dwp_file != NULL)
12801 dwp_text = string_printf (" [in DWP file %s]",
12802 lbasename (dwp_file->name));
12803
12804 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12805 " [in module %s]"),
12806 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12807 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12808 }
12809 return NULL;
12810 }
12811
12812 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12813 See lookup_dwo_cutu_unit for details. */
12814
12815 static struct dwo_unit *
12816 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12817 ULONGEST signature)
12818 {
12819 gdb_assert (!cu->per_cu->is_debug_types);
12820
12821 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12822 }
12823
12824 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12825 See lookup_dwo_cutu_unit for details. */
12826
12827 static struct dwo_unit *
12828 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12829 {
12830 gdb_assert (cu->per_cu->is_debug_types);
12831
12832 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12833
12834 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12835 }
12836
12837 /* Traversal function for queue_and_load_all_dwo_tus. */
12838
12839 static int
12840 queue_and_load_dwo_tu (void **slot, void *info)
12841 {
12842 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12843 dwarf2_cu *cu = (dwarf2_cu *) info;
12844 ULONGEST signature = dwo_unit->signature;
12845 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12846
12847 if (sig_type != NULL)
12848 {
12849 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12850 a real dependency of PER_CU on SIG_TYPE. That is detected later
12851 while processing PER_CU. */
12852 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12853 cu->per_cu->lang))
12854 load_full_type_unit (sig_type, cu->per_objfile);
12855 cu->per_cu->imported_symtabs_push (sig_type);
12856 }
12857
12858 return 1;
12859 }
12860
12861 /* Queue all TUs contained in the DWO of CU to be read in.
12862 The DWO may have the only definition of the type, though it may not be
12863 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12864 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12865
12866 static void
12867 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12868 {
12869 struct dwo_unit *dwo_unit;
12870 struct dwo_file *dwo_file;
12871
12872 gdb_assert (cu != nullptr);
12873 gdb_assert (!cu->per_cu->is_debug_types);
12874 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12875
12876 dwo_unit = cu->dwo_unit;
12877 gdb_assert (dwo_unit != NULL);
12878
12879 dwo_file = dwo_unit->dwo_file;
12880 if (dwo_file->tus != NULL)
12881 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12882 }
12883
12884 /* Read in various DIEs. */
12885
12886 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12887 Inherit only the children of the DW_AT_abstract_origin DIE not being
12888 already referenced by DW_AT_abstract_origin from the children of the
12889 current DIE. */
12890
12891 static void
12892 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12893 {
12894 struct die_info *child_die;
12895 sect_offset *offsetp;
12896 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12897 struct die_info *origin_die;
12898 /* Iterator of the ORIGIN_DIE children. */
12899 struct die_info *origin_child_die;
12900 struct attribute *attr;
12901 struct dwarf2_cu *origin_cu;
12902 struct pending **origin_previous_list_in_scope;
12903
12904 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12905 if (!attr)
12906 return;
12907
12908 /* Note that following die references may follow to a die in a
12909 different cu. */
12910
12911 origin_cu = cu;
12912 origin_die = follow_die_ref (die, attr, &origin_cu);
12913
12914 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12915 symbols in. */
12916 origin_previous_list_in_scope = origin_cu->list_in_scope;
12917 origin_cu->list_in_scope = cu->list_in_scope;
12918
12919 if (die->tag != origin_die->tag
12920 && !(die->tag == DW_TAG_inlined_subroutine
12921 && origin_die->tag == DW_TAG_subprogram))
12922 complaint (_("DIE %s and its abstract origin %s have different tags"),
12923 sect_offset_str (die->sect_off),
12924 sect_offset_str (origin_die->sect_off));
12925
12926 /* Find if the concrete and abstract trees are structurally the
12927 same. This is a shallow traversal and it is not bullet-proof;
12928 the compiler can trick the debugger into believing that the trees
12929 are isomorphic, whereas they actually are not. However, the
12930 likelyhood of this happening is pretty low, and a full-fledged
12931 check would be an overkill. */
12932 bool are_isomorphic = true;
12933 die_info *concrete_child = die->child;
12934 die_info *abstract_child = origin_die->child;
12935 while (concrete_child != nullptr || abstract_child != nullptr)
12936 {
12937 if (concrete_child == nullptr
12938 || abstract_child == nullptr
12939 || concrete_child->tag != abstract_child->tag)
12940 {
12941 are_isomorphic = false;
12942 break;
12943 }
12944
12945 concrete_child = concrete_child->sibling;
12946 abstract_child = abstract_child->sibling;
12947 }
12948
12949 /* Walk the origin's children in parallel to the concrete children.
12950 This helps match an origin child in case the debug info misses
12951 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12952 origin tree may not have the same tree structure as the concrete
12953 DIE, though. */
12954 die_info *corresponding_abstract_child
12955 = are_isomorphic ? origin_die->child : nullptr;
12956
12957 std::vector<sect_offset> offsets;
12958
12959 for (child_die = die->child;
12960 child_die && child_die->tag;
12961 child_die = child_die->sibling)
12962 {
12963 struct die_info *child_origin_die;
12964 struct dwarf2_cu *child_origin_cu;
12965
12966 /* We are trying to process concrete instance entries:
12967 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12968 it's not relevant to our analysis here. i.e. detecting DIEs that are
12969 present in the abstract instance but not referenced in the concrete
12970 one. */
12971 if (child_die->tag == DW_TAG_call_site
12972 || child_die->tag == DW_TAG_GNU_call_site)
12973 {
12974 if (are_isomorphic)
12975 corresponding_abstract_child
12976 = corresponding_abstract_child->sibling;
12977 continue;
12978 }
12979
12980 /* For each CHILD_DIE, find the corresponding child of
12981 ORIGIN_DIE. If there is more than one layer of
12982 DW_AT_abstract_origin, follow them all; there shouldn't be,
12983 but GCC versions at least through 4.4 generate this (GCC PR
12984 40573). */
12985 child_origin_die = child_die;
12986 child_origin_cu = cu;
12987 while (1)
12988 {
12989 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12990 child_origin_cu);
12991 if (attr == NULL)
12992 break;
12993 child_origin_die = follow_die_ref (child_origin_die, attr,
12994 &child_origin_cu);
12995 }
12996
12997 /* If missing DW_AT_abstract_origin, try the corresponding child
12998 of the origin. Clang emits such lexical scopes. */
12999 if (child_origin_die == child_die
13000 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13001 && are_isomorphic
13002 && child_die->tag == DW_TAG_lexical_block)
13003 child_origin_die = corresponding_abstract_child;
13004
13005 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13006 counterpart may exist. */
13007 if (child_origin_die != child_die)
13008 {
13009 if (child_die->tag != child_origin_die->tag
13010 && !(child_die->tag == DW_TAG_inlined_subroutine
13011 && child_origin_die->tag == DW_TAG_subprogram))
13012 complaint (_("Child DIE %s and its abstract origin %s have "
13013 "different tags"),
13014 sect_offset_str (child_die->sect_off),
13015 sect_offset_str (child_origin_die->sect_off));
13016 if (child_origin_die->parent != origin_die)
13017 complaint (_("Child DIE %s and its abstract origin %s have "
13018 "different parents"),
13019 sect_offset_str (child_die->sect_off),
13020 sect_offset_str (child_origin_die->sect_off));
13021 else
13022 offsets.push_back (child_origin_die->sect_off);
13023 }
13024
13025 if (are_isomorphic)
13026 corresponding_abstract_child = corresponding_abstract_child->sibling;
13027 }
13028 std::sort (offsets.begin (), offsets.end ());
13029 sect_offset *offsets_end = offsets.data () + offsets.size ();
13030 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13031 if (offsetp[-1] == *offsetp)
13032 complaint (_("Multiple children of DIE %s refer "
13033 "to DIE %s as their abstract origin"),
13034 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13035
13036 offsetp = offsets.data ();
13037 origin_child_die = origin_die->child;
13038 while (origin_child_die && origin_child_die->tag)
13039 {
13040 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13041 while (offsetp < offsets_end
13042 && *offsetp < origin_child_die->sect_off)
13043 offsetp++;
13044 if (offsetp >= offsets_end
13045 || *offsetp > origin_child_die->sect_off)
13046 {
13047 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13048 Check whether we're already processing ORIGIN_CHILD_DIE.
13049 This can happen with mutually referenced abstract_origins.
13050 PR 16581. */
13051 if (!origin_child_die->in_process)
13052 process_die (origin_child_die, origin_cu);
13053 }
13054 origin_child_die = origin_child_die->sibling;
13055 }
13056 origin_cu->list_in_scope = origin_previous_list_in_scope;
13057
13058 if (cu != origin_cu)
13059 compute_delayed_physnames (origin_cu);
13060 }
13061
13062 static void
13063 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13064 {
13065 struct objfile *objfile = cu->per_objfile->objfile;
13066 struct gdbarch *gdbarch = objfile->arch ();
13067 struct context_stack *newobj;
13068 CORE_ADDR lowpc;
13069 CORE_ADDR highpc;
13070 struct die_info *child_die;
13071 struct attribute *attr, *call_line, *call_file;
13072 const char *name;
13073 CORE_ADDR baseaddr;
13074 struct block *block;
13075 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13076 std::vector<struct symbol *> template_args;
13077 struct template_symbol *templ_func = NULL;
13078
13079 if (inlined_func)
13080 {
13081 /* If we do not have call site information, we can't show the
13082 caller of this inlined function. That's too confusing, so
13083 only use the scope for local variables. */
13084 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13085 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13086 if (call_line == NULL || call_file == NULL)
13087 {
13088 read_lexical_block_scope (die, cu);
13089 return;
13090 }
13091 }
13092
13093 baseaddr = objfile->text_section_offset ();
13094
13095 name = dwarf2_name (die, cu);
13096
13097 /* Ignore functions with missing or empty names. These are actually
13098 illegal according to the DWARF standard. */
13099 if (name == NULL)
13100 {
13101 complaint (_("missing name for subprogram DIE at %s"),
13102 sect_offset_str (die->sect_off));
13103 return;
13104 }
13105
13106 /* Ignore functions with missing or invalid low and high pc attributes. */
13107 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
13108 <= PC_BOUNDS_INVALID)
13109 {
13110 attr = dwarf2_attr (die, DW_AT_external, cu);
13111 if (attr == nullptr || !attr->as_boolean ())
13112 complaint (_("cannot get low and high bounds "
13113 "for subprogram DIE at %s"),
13114 sect_offset_str (die->sect_off));
13115 return;
13116 }
13117
13118 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13119 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13120
13121 /* If we have any template arguments, then we must allocate a
13122 different sort of symbol. */
13123 for (child_die = die->child; child_die; child_die = child_die->sibling)
13124 {
13125 if (child_die->tag == DW_TAG_template_type_param
13126 || child_die->tag == DW_TAG_template_value_param)
13127 {
13128 templ_func = new (&objfile->objfile_obstack) template_symbol;
13129 templ_func->subclass = SYMBOL_TEMPLATE;
13130 break;
13131 }
13132 }
13133
13134 gdb_assert (cu->get_builder () != nullptr);
13135 newobj = cu->get_builder ()->push_context (0, lowpc);
13136 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13137 (struct symbol *) templ_func);
13138
13139 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13140 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13141 cu->per_cu->lang);
13142
13143 /* If there is a location expression for DW_AT_frame_base, record
13144 it. */
13145 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13146 if (attr != nullptr)
13147 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13148
13149 /* If there is a location for the static link, record it. */
13150 newobj->static_link = NULL;
13151 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13152 if (attr != nullptr)
13153 {
13154 newobj->static_link
13155 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13156 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13157 cu->addr_type ());
13158 }
13159
13160 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13161
13162 if (die->child != NULL)
13163 {
13164 child_die = die->child;
13165 while (child_die && child_die->tag)
13166 {
13167 if (child_die->tag == DW_TAG_template_type_param
13168 || child_die->tag == DW_TAG_template_value_param)
13169 {
13170 struct symbol *arg = new_symbol (child_die, NULL, cu);
13171
13172 if (arg != NULL)
13173 template_args.push_back (arg);
13174 }
13175 else
13176 process_die (child_die, cu);
13177 child_die = child_die->sibling;
13178 }
13179 }
13180
13181 inherit_abstract_dies (die, cu);
13182
13183 /* If we have a DW_AT_specification, we might need to import using
13184 directives from the context of the specification DIE. See the
13185 comment in determine_prefix. */
13186 if (cu->per_cu->lang == language_cplus
13187 && dwarf2_attr (die, DW_AT_specification, cu))
13188 {
13189 struct dwarf2_cu *spec_cu = cu;
13190 struct die_info *spec_die = die_specification (die, &spec_cu);
13191
13192 while (spec_die)
13193 {
13194 child_die = spec_die->child;
13195 while (child_die && child_die->tag)
13196 {
13197 if (child_die->tag == DW_TAG_imported_module)
13198 process_die (child_die, spec_cu);
13199 child_die = child_die->sibling;
13200 }
13201
13202 /* In some cases, GCC generates specification DIEs that
13203 themselves contain DW_AT_specification attributes. */
13204 spec_die = die_specification (spec_die, &spec_cu);
13205 }
13206 }
13207
13208 struct context_stack cstk = cu->get_builder ()->pop_context ();
13209 /* Make a block for the local symbols within. */
13210 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13211 cstk.static_link, lowpc, highpc);
13212
13213 /* For C++, set the block's scope. */
13214 if ((cu->per_cu->lang == language_cplus
13215 || cu->per_cu->lang == language_fortran
13216 || cu->per_cu->lang == language_d
13217 || cu->per_cu->lang == language_rust)
13218 && cu->processing_has_namespace_info)
13219 block_set_scope (block, determine_prefix (die, cu),
13220 &objfile->objfile_obstack);
13221
13222 /* If we have address ranges, record them. */
13223 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13224
13225 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13226
13227 /* Attach template arguments to function. */
13228 if (!template_args.empty ())
13229 {
13230 gdb_assert (templ_func != NULL);
13231
13232 templ_func->n_template_arguments = template_args.size ();
13233 templ_func->template_arguments
13234 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13235 templ_func->n_template_arguments);
13236 memcpy (templ_func->template_arguments,
13237 template_args.data (),
13238 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13239
13240 /* Make sure that the symtab is set on the new symbols. Even
13241 though they don't appear in this symtab directly, other parts
13242 of gdb assume that symbols do, and this is reasonably
13243 true. */
13244 for (symbol *sym : template_args)
13245 symbol_set_symtab (sym, symbol_symtab (templ_func));
13246 }
13247
13248 /* In C++, we can have functions nested inside functions (e.g., when
13249 a function declares a class that has methods). This means that
13250 when we finish processing a function scope, we may need to go
13251 back to building a containing block's symbol lists. */
13252 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13253 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13254
13255 /* If we've finished processing a top-level function, subsequent
13256 symbols go in the file symbol list. */
13257 if (cu->get_builder ()->outermost_context_p ())
13258 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13259 }
13260
13261 /* Process all the DIES contained within a lexical block scope. Start
13262 a new scope, process the dies, and then close the scope. */
13263
13264 static void
13265 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13266 {
13267 struct objfile *objfile = cu->per_objfile->objfile;
13268 struct gdbarch *gdbarch = objfile->arch ();
13269 CORE_ADDR lowpc, highpc;
13270 struct die_info *child_die;
13271 CORE_ADDR baseaddr;
13272
13273 baseaddr = objfile->text_section_offset ();
13274
13275 /* Ignore blocks with missing or invalid low and high pc attributes. */
13276 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13277 as multiple lexical blocks? Handling children in a sane way would
13278 be nasty. Might be easier to properly extend generic blocks to
13279 describe ranges. */
13280 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
13281 {
13282 case PC_BOUNDS_NOT_PRESENT:
13283 /* DW_TAG_lexical_block has no attributes, process its children as if
13284 there was no wrapping by that DW_TAG_lexical_block.
13285 GCC does no longer produces such DWARF since GCC r224161. */
13286 for (child_die = die->child;
13287 child_die != NULL && child_die->tag;
13288 child_die = child_die->sibling)
13289 {
13290 /* We might already be processing this DIE. This can happen
13291 in an unusual circumstance -- where a subroutine A
13292 appears lexically in another subroutine B, but A actually
13293 inlines B. The recursion is broken here, rather than in
13294 inherit_abstract_dies, because it seems better to simply
13295 drop concrete children here. */
13296 if (!child_die->in_process)
13297 process_die (child_die, cu);
13298 }
13299 return;
13300 case PC_BOUNDS_INVALID:
13301 return;
13302 }
13303 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13304 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13305
13306 cu->get_builder ()->push_context (0, lowpc);
13307 if (die->child != NULL)
13308 {
13309 child_die = die->child;
13310 while (child_die && child_die->tag)
13311 {
13312 process_die (child_die, cu);
13313 child_die = child_die->sibling;
13314 }
13315 }
13316 inherit_abstract_dies (die, cu);
13317 struct context_stack cstk = cu->get_builder ()->pop_context ();
13318
13319 if (*cu->get_builder ()->get_local_symbols () != NULL
13320 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13321 {
13322 struct block *block
13323 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13324 cstk.start_addr, highpc);
13325
13326 /* Note that recording ranges after traversing children, as we
13327 do here, means that recording a parent's ranges entails
13328 walking across all its children's ranges as they appear in
13329 the address map, which is quadratic behavior.
13330
13331 It would be nicer to record the parent's ranges before
13332 traversing its children, simply overriding whatever you find
13333 there. But since we don't even decide whether to create a
13334 block until after we've traversed its children, that's hard
13335 to do. */
13336 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13337 }
13338 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13339 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13340 }
13341
13342 static void dwarf2_ranges_read_low_addrs (unsigned offset,
13343 struct dwarf2_cu *cu,
13344 dwarf_tag tag,
13345 std::vector<CORE_ADDR> &result);
13346
13347 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13348
13349 static void
13350 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13351 {
13352 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13353 struct objfile *objfile = per_objfile->objfile;
13354 struct gdbarch *gdbarch = objfile->arch ();
13355 CORE_ADDR pc, baseaddr;
13356 struct attribute *attr;
13357 void **slot;
13358 int nparams;
13359 struct die_info *child_die;
13360
13361 baseaddr = objfile->text_section_offset ();
13362
13363 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13364 if (attr == NULL)
13365 {
13366 /* This was a pre-DWARF-5 GNU extension alias
13367 for DW_AT_call_return_pc. */
13368 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13369 }
13370 if (!attr)
13371 {
13372 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13373 "DIE %s [in module %s]"),
13374 sect_offset_str (die->sect_off), objfile_name (objfile));
13375 return;
13376 }
13377 pc = attr->as_address () + baseaddr;
13378 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13379 pc -= baseaddr;
13380
13381 if (cu->call_site_htab == NULL)
13382 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
13383 call_site::eq, NULL,
13384 &objfile->objfile_obstack,
13385 hashtab_obstack_allocate, NULL);
13386 struct call_site call_site_local (pc, nullptr, nullptr);
13387 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13388 if (*slot != NULL)
13389 {
13390 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13391 "DIE %s [in module %s]"),
13392 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13393 objfile_name (objfile));
13394 return;
13395 }
13396
13397 /* Count parameters at the caller. */
13398
13399 nparams = 0;
13400 for (child_die = die->child; child_die && child_die->tag;
13401 child_die = child_die->sibling)
13402 {
13403 if (child_die->tag != DW_TAG_call_site_parameter
13404 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13405 {
13406 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13407 "DW_TAG_call_site child DIE %s [in module %s]"),
13408 child_die->tag, sect_offset_str (child_die->sect_off),
13409 objfile_name (objfile));
13410 continue;
13411 }
13412
13413 nparams++;
13414 }
13415
13416 struct call_site *call_site
13417 = new (XOBNEWVAR (&objfile->objfile_obstack,
13418 struct call_site,
13419 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
13420 struct call_site (pc, cu->per_cu, per_objfile);
13421 *slot = call_site;
13422
13423 /* We never call the destructor of call_site, so we must ensure it is
13424 trivially destructible. */
13425 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
13426
13427 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13428 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13429 {
13430 struct die_info *func_die;
13431
13432 /* Skip also over DW_TAG_inlined_subroutine. */
13433 for (func_die = die->parent;
13434 func_die && func_die->tag != DW_TAG_subprogram
13435 && func_die->tag != DW_TAG_subroutine_type;
13436 func_die = func_die->parent);
13437
13438 /* DW_AT_call_all_calls is a superset
13439 of DW_AT_call_all_tail_calls. */
13440 if (func_die
13441 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13442 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13443 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13444 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13445 {
13446 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13447 not complete. But keep CALL_SITE for look ups via call_site_htab,
13448 both the initial caller containing the real return address PC and
13449 the final callee containing the current PC of a chain of tail
13450 calls do not need to have the tail call list complete. But any
13451 function candidate for a virtual tail call frame searched via
13452 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13453 determined unambiguously. */
13454 }
13455 else
13456 {
13457 struct type *func_type = NULL;
13458
13459 if (func_die)
13460 func_type = get_die_type (func_die, cu);
13461 if (func_type != NULL)
13462 {
13463 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13464
13465 /* Enlist this call site to the function. */
13466 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13467 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13468 }
13469 else
13470 complaint (_("Cannot find function owning DW_TAG_call_site "
13471 "DIE %s [in module %s]"),
13472 sect_offset_str (die->sect_off), objfile_name (objfile));
13473 }
13474 }
13475
13476 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13477 if (attr == NULL)
13478 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13479 if (attr == NULL)
13480 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13481 if (attr == NULL)
13482 {
13483 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13484 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13485 }
13486
13487 call_site->target.set_loc_dwarf_block (nullptr);
13488 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13489 /* Keep NULL DWARF_BLOCK. */;
13490 else if (attr->form_is_block ())
13491 {
13492 struct dwarf2_locexpr_baton *dlbaton;
13493 struct dwarf_block *block = attr->as_block ();
13494
13495 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13496 dlbaton->data = block->data;
13497 dlbaton->size = block->size;
13498 dlbaton->per_objfile = per_objfile;
13499 dlbaton->per_cu = cu->per_cu;
13500
13501 call_site->target.set_loc_dwarf_block (dlbaton);
13502 }
13503 else if (attr->form_is_ref ())
13504 {
13505 struct dwarf2_cu *target_cu = cu;
13506 struct die_info *target_die;
13507
13508 target_die = follow_die_ref (die, attr, &target_cu);
13509 gdb_assert (target_cu->per_objfile->objfile == objfile);
13510
13511 struct attribute *ranges_attr
13512 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
13513
13514 if (die_is_declaration (target_die, target_cu))
13515 {
13516 const char *target_physname;
13517
13518 /* Prefer the mangled name; otherwise compute the demangled one. */
13519 target_physname = dw2_linkage_name (target_die, target_cu);
13520 if (target_physname == NULL)
13521 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13522 if (target_physname == NULL)
13523 complaint (_("DW_AT_call_target target DIE has invalid "
13524 "physname, for referencing DIE %s [in module %s]"),
13525 sect_offset_str (die->sect_off), objfile_name (objfile));
13526 else
13527 call_site->target.set_loc_physname (target_physname);
13528 }
13529 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
13530 {
13531 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
13532 + target_cu->gnu_ranges_base);
13533 std::vector<CORE_ADDR> addresses;
13534 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
13535 target_die->tag, addresses);
13536 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
13537 addresses.size ());
13538 std::copy (addresses.begin (), addresses.end (), saved);
13539 call_site->target.set_loc_array (addresses.size (), saved);
13540 }
13541 else
13542 {
13543 CORE_ADDR lowpc;
13544
13545 /* DW_AT_entry_pc should be preferred. */
13546 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
13547 nullptr, nullptr)
13548 <= PC_BOUNDS_INVALID)
13549 complaint (_("DW_AT_call_target target DIE has invalid "
13550 "low pc, for referencing DIE %s [in module %s]"),
13551 sect_offset_str (die->sect_off), objfile_name (objfile));
13552 else
13553 {
13554 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
13555 - baseaddr);
13556 call_site->target.set_loc_physaddr (lowpc);
13557 }
13558 }
13559 }
13560 else
13561 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13562 "block nor reference, for DIE %s [in module %s]"),
13563 sect_offset_str (die->sect_off), objfile_name (objfile));
13564
13565 for (child_die = die->child;
13566 child_die && child_die->tag;
13567 child_die = child_die->sibling)
13568 {
13569 struct call_site_parameter *parameter;
13570 struct attribute *loc, *origin;
13571
13572 if (child_die->tag != DW_TAG_call_site_parameter
13573 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13574 {
13575 /* Already printed the complaint above. */
13576 continue;
13577 }
13578
13579 gdb_assert (call_site->parameter_count < nparams);
13580 parameter = &call_site->parameter[call_site->parameter_count];
13581
13582 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13583 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13584 register is contained in DW_AT_call_value. */
13585
13586 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13587 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13588 if (origin == NULL)
13589 {
13590 /* This was a pre-DWARF-5 GNU extension alias
13591 for DW_AT_call_parameter. */
13592 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13593 }
13594 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13595 {
13596 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13597
13598 sect_offset sect_off = origin->get_ref_die_offset ();
13599 if (!cu->header.offset_in_cu_p (sect_off))
13600 {
13601 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13602 binding can be done only inside one CU. Such referenced DIE
13603 therefore cannot be even moved to DW_TAG_partial_unit. */
13604 complaint (_("DW_AT_call_parameter offset is not in CU for "
13605 "DW_TAG_call_site child DIE %s [in module %s]"),
13606 sect_offset_str (child_die->sect_off),
13607 objfile_name (objfile));
13608 continue;
13609 }
13610 parameter->u.param_cu_off
13611 = (cu_offset) (sect_off - cu->header.sect_off);
13612 }
13613 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13614 {
13615 complaint (_("No DW_FORM_block* DW_AT_location for "
13616 "DW_TAG_call_site child DIE %s [in module %s]"),
13617 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13618 continue;
13619 }
13620 else
13621 {
13622 struct dwarf_block *block = loc->as_block ();
13623
13624 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13625 (block->data, &block->data[block->size]);
13626 if (parameter->u.dwarf_reg != -1)
13627 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13628 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13629 &block->data[block->size],
13630 &parameter->u.fb_offset))
13631 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13632 else
13633 {
13634 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13635 "for DW_FORM_block* DW_AT_location is supported for "
13636 "DW_TAG_call_site child DIE %s "
13637 "[in module %s]"),
13638 sect_offset_str (child_die->sect_off),
13639 objfile_name (objfile));
13640 continue;
13641 }
13642 }
13643
13644 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13645 if (attr == NULL)
13646 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13647 if (attr == NULL || !attr->form_is_block ())
13648 {
13649 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13650 "DW_TAG_call_site child DIE %s [in module %s]"),
13651 sect_offset_str (child_die->sect_off),
13652 objfile_name (objfile));
13653 continue;
13654 }
13655
13656 struct dwarf_block *block = attr->as_block ();
13657 parameter->value = block->data;
13658 parameter->value_size = block->size;
13659
13660 /* Parameters are not pre-cleared by memset above. */
13661 parameter->data_value = NULL;
13662 parameter->data_value_size = 0;
13663 call_site->parameter_count++;
13664
13665 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13666 if (attr == NULL)
13667 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13668 if (attr != nullptr)
13669 {
13670 if (!attr->form_is_block ())
13671 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13672 "DW_TAG_call_site child DIE %s [in module %s]"),
13673 sect_offset_str (child_die->sect_off),
13674 objfile_name (objfile));
13675 else
13676 {
13677 block = attr->as_block ();
13678 parameter->data_value = block->data;
13679 parameter->data_value_size = block->size;
13680 }
13681 }
13682 }
13683 }
13684
13685 /* Helper function for read_variable. If DIE represents a virtual
13686 table, then return the type of the concrete object that is
13687 associated with the virtual table. Otherwise, return NULL. */
13688
13689 static struct type *
13690 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13691 {
13692 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13693 if (attr == NULL)
13694 return NULL;
13695
13696 /* Find the type DIE. */
13697 struct die_info *type_die = NULL;
13698 struct dwarf2_cu *type_cu = cu;
13699
13700 if (attr->form_is_ref ())
13701 type_die = follow_die_ref (die, attr, &type_cu);
13702 if (type_die == NULL)
13703 return NULL;
13704
13705 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13706 return NULL;
13707 return die_containing_type (type_die, type_cu);
13708 }
13709
13710 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13711
13712 static void
13713 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13714 {
13715 struct rust_vtable_symbol *storage = NULL;
13716
13717 if (cu->per_cu->lang == language_rust)
13718 {
13719 struct type *containing_type = rust_containing_type (die, cu);
13720
13721 if (containing_type != NULL)
13722 {
13723 struct objfile *objfile = cu->per_objfile->objfile;
13724
13725 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13726 storage->concrete_type = containing_type;
13727 storage->subclass = SYMBOL_RUST_VTABLE;
13728 }
13729 }
13730
13731 struct symbol *res = new_symbol (die, NULL, cu, storage);
13732 struct attribute *abstract_origin
13733 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13734 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13735 if (res == NULL && loc && abstract_origin)
13736 {
13737 /* We have a variable without a name, but with a location and an abstract
13738 origin. This may be a concrete instance of an abstract variable
13739 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13740 later. */
13741 struct dwarf2_cu *origin_cu = cu;
13742 struct die_info *origin_die
13743 = follow_die_ref (die, abstract_origin, &origin_cu);
13744 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13745 per_objfile->per_bfd->abstract_to_concrete
13746 [origin_die->sect_off].push_back (die->sect_off);
13747 }
13748 }
13749
13750 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13751 reading .debug_rnglists.
13752 Callback's type should be:
13753 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13754 Return true if the attributes are present and valid, otherwise,
13755 return false. */
13756
13757 template <typename Callback>
13758 static bool
13759 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13760 dwarf_tag tag, Callback &&callback)
13761 {
13762 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13763 struct objfile *objfile = per_objfile->objfile;
13764 bfd *obfd = objfile->obfd;
13765 /* Base address selection entry. */
13766 gdb::optional<CORE_ADDR> base;
13767 const gdb_byte *buffer;
13768 bool overflow = false;
13769 ULONGEST addr_index;
13770 struct dwarf2_section_info *rnglists_section;
13771
13772 base = cu->base_address;
13773 rnglists_section = cu_debug_rnglists_section (cu, tag);
13774 rnglists_section->read (objfile);
13775
13776 if (offset >= rnglists_section->size)
13777 {
13778 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13779 offset);
13780 return false;
13781 }
13782 buffer = rnglists_section->buffer + offset;
13783
13784 while (1)
13785 {
13786 /* Initialize it due to a false compiler warning. */
13787 CORE_ADDR range_beginning = 0, range_end = 0;
13788 const gdb_byte *buf_end = (rnglists_section->buffer
13789 + rnglists_section->size);
13790 unsigned int bytes_read;
13791
13792 if (buffer == buf_end)
13793 {
13794 overflow = true;
13795 break;
13796 }
13797 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13798 switch (rlet)
13799 {
13800 case DW_RLE_end_of_list:
13801 break;
13802 case DW_RLE_base_address:
13803 if (buffer + cu->header.addr_size > buf_end)
13804 {
13805 overflow = true;
13806 break;
13807 }
13808 base = cu->header.read_address (obfd, buffer, &bytes_read);
13809 buffer += bytes_read;
13810 break;
13811 case DW_RLE_base_addressx:
13812 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13813 buffer += bytes_read;
13814 base = read_addr_index (cu, addr_index);
13815 break;
13816 case DW_RLE_start_length:
13817 if (buffer + cu->header.addr_size > buf_end)
13818 {
13819 overflow = true;
13820 break;
13821 }
13822 range_beginning = cu->header.read_address (obfd, buffer,
13823 &bytes_read);
13824 buffer += bytes_read;
13825 range_end = (range_beginning
13826 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13827 buffer += bytes_read;
13828 if (buffer > buf_end)
13829 {
13830 overflow = true;
13831 break;
13832 }
13833 break;
13834 case DW_RLE_startx_length:
13835 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13836 buffer += bytes_read;
13837 range_beginning = read_addr_index (cu, addr_index);
13838 if (buffer > buf_end)
13839 {
13840 overflow = true;
13841 break;
13842 }
13843 range_end = (range_beginning
13844 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13845 buffer += bytes_read;
13846 break;
13847 case DW_RLE_offset_pair:
13848 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13849 buffer += bytes_read;
13850 if (buffer > buf_end)
13851 {
13852 overflow = true;
13853 break;
13854 }
13855 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13856 buffer += bytes_read;
13857 if (buffer > buf_end)
13858 {
13859 overflow = true;
13860 break;
13861 }
13862 break;
13863 case DW_RLE_start_end:
13864 if (buffer + 2 * cu->header.addr_size > buf_end)
13865 {
13866 overflow = true;
13867 break;
13868 }
13869 range_beginning = cu->header.read_address (obfd, buffer,
13870 &bytes_read);
13871 buffer += bytes_read;
13872 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13873 buffer += bytes_read;
13874 break;
13875 case DW_RLE_startx_endx:
13876 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13877 buffer += bytes_read;
13878 range_beginning = read_addr_index (cu, addr_index);
13879 if (buffer > buf_end)
13880 {
13881 overflow = true;
13882 break;
13883 }
13884 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13885 buffer += bytes_read;
13886 range_end = read_addr_index (cu, addr_index);
13887 break;
13888 default:
13889 complaint (_("Invalid .debug_rnglists data (no base address)"));
13890 return false;
13891 }
13892 if (rlet == DW_RLE_end_of_list || overflow)
13893 break;
13894 if (rlet == DW_RLE_base_address)
13895 continue;
13896
13897 if (range_beginning > range_end)
13898 {
13899 /* Inverted range entries are invalid. */
13900 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13901 return false;
13902 }
13903
13904 /* Empty range entries have no effect. */
13905 if (range_beginning == range_end)
13906 continue;
13907
13908 /* Only DW_RLE_offset_pair needs the base address added. */
13909 if (rlet == DW_RLE_offset_pair)
13910 {
13911 if (!base.has_value ())
13912 {
13913 /* We have no valid base address for the DW_RLE_offset_pair. */
13914 complaint (_("Invalid .debug_rnglists data (no base address for "
13915 "DW_RLE_offset_pair)"));
13916 return false;
13917 }
13918
13919 range_beginning += *base;
13920 range_end += *base;
13921 }
13922
13923 /* A not-uncommon case of bad debug info.
13924 Don't pollute the addrmap with bad data. */
13925 if (range_beginning == 0
13926 && !per_objfile->per_bfd->has_section_at_zero)
13927 {
13928 complaint (_(".debug_rnglists entry has start address of zero"
13929 " [in module %s]"), objfile_name (objfile));
13930 continue;
13931 }
13932
13933 callback (range_beginning, range_end);
13934 }
13935
13936 if (overflow)
13937 {
13938 complaint (_("Offset %d is not terminated "
13939 "for DW_AT_ranges attribute"),
13940 offset);
13941 return false;
13942 }
13943
13944 return true;
13945 }
13946
13947 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13948 Callback's type should be:
13949 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13950 Return 1 if the attributes are present and valid, otherwise, return 0. */
13951
13952 template <typename Callback>
13953 static int
13954 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13955 Callback &&callback)
13956 {
13957 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13958 struct objfile *objfile = per_objfile->objfile;
13959 struct comp_unit_head *cu_header = &cu->header;
13960 bfd *obfd = objfile->obfd;
13961 unsigned int addr_size = cu_header->addr_size;
13962 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13963 /* Base address selection entry. */
13964 gdb::optional<CORE_ADDR> base;
13965 unsigned int dummy;
13966 const gdb_byte *buffer;
13967
13968 if (cu_header->version >= 5)
13969 return dwarf2_rnglists_process (offset, cu, tag, callback);
13970
13971 base = cu->base_address;
13972
13973 per_objfile->per_bfd->ranges.read (objfile);
13974 if (offset >= per_objfile->per_bfd->ranges.size)
13975 {
13976 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13977 offset);
13978 return 0;
13979 }
13980 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13981
13982 while (1)
13983 {
13984 CORE_ADDR range_beginning, range_end;
13985
13986 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13987 buffer += addr_size;
13988 range_end = cu->header.read_address (obfd, buffer, &dummy);
13989 buffer += addr_size;
13990 offset += 2 * addr_size;
13991
13992 /* An end of list marker is a pair of zero addresses. */
13993 if (range_beginning == 0 && range_end == 0)
13994 /* Found the end of list entry. */
13995 break;
13996
13997 /* Each base address selection entry is a pair of 2 values.
13998 The first is the largest possible address, the second is
13999 the base address. Check for a base address here. */
14000 if ((range_beginning & mask) == mask)
14001 {
14002 /* If we found the largest possible address, then we already
14003 have the base address in range_end. */
14004 base = range_end;
14005 continue;
14006 }
14007
14008 if (!base.has_value ())
14009 {
14010 /* We have no valid base address for the ranges
14011 data. */
14012 complaint (_("Invalid .debug_ranges data (no base address)"));
14013 return 0;
14014 }
14015
14016 if (range_beginning > range_end)
14017 {
14018 /* Inverted range entries are invalid. */
14019 complaint (_("Invalid .debug_ranges data (inverted range)"));
14020 return 0;
14021 }
14022
14023 /* Empty range entries have no effect. */
14024 if (range_beginning == range_end)
14025 continue;
14026
14027 range_beginning += *base;
14028 range_end += *base;
14029
14030 /* A not-uncommon case of bad debug info.
14031 Don't pollute the addrmap with bad data. */
14032 if (range_beginning == 0
14033 && !per_objfile->per_bfd->has_section_at_zero)
14034 {
14035 complaint (_(".debug_ranges entry has start address of zero"
14036 " [in module %s]"), objfile_name (objfile));
14037 continue;
14038 }
14039
14040 callback (range_beginning, range_end);
14041 }
14042
14043 return 1;
14044 }
14045
14046 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14047 Return 1 if the attributes are present and valid, otherwise, return 0.
14048 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
14049 ranges in MAP are set, using DATUM as the value. */
14050
14051 static int
14052 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14053 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14054 addrmap *map, void *datum, dwarf_tag tag)
14055 {
14056 struct objfile *objfile = cu->per_objfile->objfile;
14057 struct gdbarch *gdbarch = objfile->arch ();
14058 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14059 int low_set = 0;
14060 CORE_ADDR low = 0;
14061 CORE_ADDR high = 0;
14062 int retval;
14063
14064 retval = dwarf2_ranges_process (offset, cu, tag,
14065 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14066 {
14067 if (map != nullptr)
14068 {
14069 CORE_ADDR lowpc;
14070 CORE_ADDR highpc;
14071
14072 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14073 range_beginning + baseaddr)
14074 - baseaddr);
14075 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14076 range_end + baseaddr)
14077 - baseaddr);
14078 addrmap_set_empty (map, lowpc, highpc - 1, datum);
14079 }
14080
14081 /* FIXME: This is recording everything as a low-high
14082 segment of consecutive addresses. We should have a
14083 data structure for discontiguous block ranges
14084 instead. */
14085 if (! low_set)
14086 {
14087 low = range_beginning;
14088 high = range_end;
14089 low_set = 1;
14090 }
14091 else
14092 {
14093 if (range_beginning < low)
14094 low = range_beginning;
14095 if (range_end > high)
14096 high = range_end;
14097 }
14098 });
14099 if (!retval)
14100 return 0;
14101
14102 if (! low_set)
14103 /* If the first entry is an end-of-list marker, the range
14104 describes an empty scope, i.e. no instructions. */
14105 return 0;
14106
14107 if (low_return)
14108 *low_return = low;
14109 if (high_return)
14110 *high_return = high;
14111 return 1;
14112 }
14113
14114 /* Process ranges and fill in a vector of the low PC values only. */
14115
14116 static void
14117 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
14118 dwarf_tag tag,
14119 std::vector<CORE_ADDR> &result)
14120 {
14121 dwarf2_ranges_process (offset, cu, tag,
14122 [&] (CORE_ADDR start, CORE_ADDR end)
14123 {
14124 result.push_back (start);
14125 });
14126 }
14127
14128 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14129 definition for the return value. *LOWPC and *HIGHPC are set iff
14130 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14131
14132 static enum pc_bounds_kind
14133 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14134 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14135 addrmap *map, void *datum)
14136 {
14137 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14138 struct attribute *attr;
14139 struct attribute *attr_high;
14140 CORE_ADDR low = 0;
14141 CORE_ADDR high = 0;
14142 enum pc_bounds_kind ret;
14143
14144 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14145 if (attr_high)
14146 {
14147 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14148 if (attr != nullptr)
14149 {
14150 low = attr->as_address ();
14151 high = attr_high->as_address ();
14152 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14153 high += low;
14154 }
14155 else
14156 /* Found high w/o low attribute. */
14157 return PC_BOUNDS_INVALID;
14158
14159 /* Found consecutive range of addresses. */
14160 ret = PC_BOUNDS_HIGH_LOW;
14161 }
14162 else
14163 {
14164 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14165 if (attr != nullptr && attr->form_is_unsigned ())
14166 {
14167 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14168 on DWARF version). */
14169 ULONGEST ranges_offset = attr->as_unsigned ();
14170
14171 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14172 this value. */
14173 if (die->tag != DW_TAG_compile_unit)
14174 ranges_offset += cu->gnu_ranges_base;
14175
14176 /* Value of the DW_AT_ranges attribute is the offset in the
14177 .debug_ranges section. */
14178 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
14179 map, datum, die->tag))
14180 return PC_BOUNDS_INVALID;
14181 /* Found discontinuous range of addresses. */
14182 ret = PC_BOUNDS_RANGES;
14183 }
14184 else
14185 return PC_BOUNDS_NOT_PRESENT;
14186 }
14187
14188 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14189 if (high <= low)
14190 return PC_BOUNDS_INVALID;
14191
14192 /* When using the GNU linker, .gnu.linkonce. sections are used to
14193 eliminate duplicate copies of functions and vtables and such.
14194 The linker will arbitrarily choose one and discard the others.
14195 The AT_*_pc values for such functions refer to local labels in
14196 these sections. If the section from that file was discarded, the
14197 labels are not in the output, so the relocs get a value of 0.
14198 If this is a discarded function, mark the pc bounds as invalid,
14199 so that GDB will ignore it. */
14200 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14201 return PC_BOUNDS_INVALID;
14202
14203 *lowpc = low;
14204 if (highpc)
14205 *highpc = high;
14206 return ret;
14207 }
14208
14209 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14210 its low and high PC addresses. Do nothing if these addresses could not
14211 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14212 and HIGHPC to the high address if greater than HIGHPC. */
14213
14214 static void
14215 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14216 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14217 struct dwarf2_cu *cu)
14218 {
14219 CORE_ADDR low, high;
14220 struct die_info *child = die->child;
14221
14222 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
14223 >= PC_BOUNDS_RANGES)
14224 {
14225 *lowpc = std::min (*lowpc, low);
14226 *highpc = std::max (*highpc, high);
14227 }
14228
14229 /* If the language does not allow nested subprograms (either inside
14230 subprograms or lexical blocks), we're done. */
14231 if (cu->per_cu->lang != language_ada)
14232 return;
14233
14234 /* Check all the children of the given DIE. If it contains nested
14235 subprograms, then check their pc bounds. Likewise, we need to
14236 check lexical blocks as well, as they may also contain subprogram
14237 definitions. */
14238 while (child && child->tag)
14239 {
14240 if (child->tag == DW_TAG_subprogram
14241 || child->tag == DW_TAG_lexical_block)
14242 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14243 child = child->sibling;
14244 }
14245 }
14246
14247 /* Get the low and high pc's represented by the scope DIE, and store
14248 them in *LOWPC and *HIGHPC. If the correct values can't be
14249 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14250
14251 static void
14252 get_scope_pc_bounds (struct die_info *die,
14253 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14254 struct dwarf2_cu *cu)
14255 {
14256 CORE_ADDR best_low = (CORE_ADDR) -1;
14257 CORE_ADDR best_high = (CORE_ADDR) 0;
14258 CORE_ADDR current_low, current_high;
14259
14260 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
14261 nullptr, nullptr)
14262 >= PC_BOUNDS_RANGES)
14263 {
14264 best_low = current_low;
14265 best_high = current_high;
14266 }
14267 else
14268 {
14269 struct die_info *child = die->child;
14270
14271 while (child && child->tag)
14272 {
14273 switch (child->tag) {
14274 case DW_TAG_subprogram:
14275 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14276 break;
14277 case DW_TAG_namespace:
14278 case DW_TAG_module:
14279 /* FIXME: carlton/2004-01-16: Should we do this for
14280 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14281 that current GCC's always emit the DIEs corresponding
14282 to definitions of methods of classes as children of a
14283 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14284 the DIEs giving the declarations, which could be
14285 anywhere). But I don't see any reason why the
14286 standards says that they have to be there. */
14287 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14288
14289 if (current_low != ((CORE_ADDR) -1))
14290 {
14291 best_low = std::min (best_low, current_low);
14292 best_high = std::max (best_high, current_high);
14293 }
14294 break;
14295 default:
14296 /* Ignore. */
14297 break;
14298 }
14299
14300 child = child->sibling;
14301 }
14302 }
14303
14304 *lowpc = best_low;
14305 *highpc = best_high;
14306 }
14307
14308 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14309 in DIE. */
14310
14311 static void
14312 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14313 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14314 {
14315 struct objfile *objfile = cu->per_objfile->objfile;
14316 struct gdbarch *gdbarch = objfile->arch ();
14317 struct attribute *attr;
14318 struct attribute *attr_high;
14319
14320 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14321 if (attr_high)
14322 {
14323 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14324 if (attr != nullptr)
14325 {
14326 CORE_ADDR low = attr->as_address ();
14327 CORE_ADDR high = attr_high->as_address ();
14328
14329 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14330 high += low;
14331
14332 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14333 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14334 cu->get_builder ()->record_block_range (block, low, high - 1);
14335 }
14336 }
14337
14338 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14339 if (attr != nullptr && attr->form_is_unsigned ())
14340 {
14341 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14342 on DWARF version). */
14343 ULONGEST ranges_offset = attr->as_unsigned ();
14344
14345 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14346 this value. */
14347 if (die->tag != DW_TAG_compile_unit)
14348 ranges_offset += cu->gnu_ranges_base;
14349
14350 std::vector<blockrange> blockvec;
14351 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14352 [&] (CORE_ADDR start, CORE_ADDR end)
14353 {
14354 start += baseaddr;
14355 end += baseaddr;
14356 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14357 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14358 cu->get_builder ()->record_block_range (block, start, end - 1);
14359 blockvec.emplace_back (start, end);
14360 });
14361
14362 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14363 }
14364 }
14365
14366 /* Check whether the producer field indicates either of GCC < 4.6, or the
14367 Intel C/C++ compiler, and cache the result in CU. */
14368
14369 static void
14370 check_producer (struct dwarf2_cu *cu)
14371 {
14372 int major, minor;
14373
14374 if (cu->producer == NULL)
14375 {
14376 /* For unknown compilers expect their behavior is DWARF version
14377 compliant.
14378
14379 GCC started to support .debug_types sections by -gdwarf-4 since
14380 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14381 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14382 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14383 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14384 }
14385 else if (producer_is_gcc (cu->producer, &major, &minor))
14386 {
14387 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14388 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14389 cu->producer_is_gcc_11 = major == 11;
14390 }
14391 else if (producer_is_icc (cu->producer, &major, &minor))
14392 {
14393 cu->producer_is_icc = true;
14394 cu->producer_is_icc_lt_14 = major < 14;
14395 }
14396 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14397 cu->producer_is_codewarrior = true;
14398 else
14399 {
14400 /* For other non-GCC compilers, expect their behavior is DWARF version
14401 compliant. */
14402 }
14403
14404 cu->checked_producer = true;
14405 }
14406
14407 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14408 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14409 during 4.6.0 experimental. */
14410
14411 static bool
14412 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14413 {
14414 if (!cu->checked_producer)
14415 check_producer (cu);
14416
14417 return cu->producer_is_gxx_lt_4_6;
14418 }
14419
14420
14421 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14422 with incorrect is_stmt attributes. */
14423
14424 static bool
14425 producer_is_codewarrior (struct dwarf2_cu *cu)
14426 {
14427 if (!cu->checked_producer)
14428 check_producer (cu);
14429
14430 return cu->producer_is_codewarrior;
14431 }
14432
14433 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14434 If that attribute is not available, return the appropriate
14435 default. */
14436
14437 static enum dwarf_access_attribute
14438 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14439 {
14440 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14441 if (attr != nullptr)
14442 {
14443 LONGEST value = attr->constant_value (-1);
14444 if (value == DW_ACCESS_public
14445 || value == DW_ACCESS_protected
14446 || value == DW_ACCESS_private)
14447 return (dwarf_access_attribute) value;
14448 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14449 plongest (value));
14450 }
14451
14452 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14453 {
14454 /* The default DWARF 2 accessibility for members is public, the default
14455 accessibility for inheritance is private. */
14456
14457 if (die->tag != DW_TAG_inheritance)
14458 return DW_ACCESS_public;
14459 else
14460 return DW_ACCESS_private;
14461 }
14462 else
14463 {
14464 /* DWARF 3+ defines the default accessibility a different way. The same
14465 rules apply now for DW_TAG_inheritance as for the members and it only
14466 depends on the container kind. */
14467
14468 if (die->parent->tag == DW_TAG_class_type)
14469 return DW_ACCESS_private;
14470 else
14471 return DW_ACCESS_public;
14472 }
14473 }
14474
14475 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
14476 *OFFSET to the byte offset. If the attribute was not found return
14477 0, otherwise return 1. If it was found but could not properly be
14478 handled, set *OFFSET to 0. */
14479
14480 static int
14481 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14482 LONGEST *offset)
14483 {
14484 struct attribute *attr;
14485
14486 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14487 if (attr != NULL)
14488 {
14489 *offset = 0;
14490
14491 /* Note that we do not check for a section offset first here.
14492 This is because DW_AT_data_member_location is new in DWARF 4,
14493 so if we see it, we can assume that a constant form is really
14494 a constant and not a section offset. */
14495 if (attr->form_is_constant ())
14496 *offset = attr->constant_value (0);
14497 else if (attr->form_is_section_offset ())
14498 dwarf2_complex_location_expr_complaint ();
14499 else if (attr->form_is_block ())
14500 *offset = decode_locdesc (attr->as_block (), cu);
14501 else
14502 dwarf2_complex_location_expr_complaint ();
14503
14504 return 1;
14505 }
14506 else
14507 {
14508 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14509 if (attr != nullptr)
14510 {
14511 *offset = attr->constant_value (0);
14512 return 1;
14513 }
14514 }
14515
14516 return 0;
14517 }
14518
14519 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
14520 store the results in FIELD. */
14521
14522 static void
14523 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14524 struct field *field)
14525 {
14526 struct attribute *attr;
14527
14528 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14529 if (attr != NULL)
14530 {
14531 if (attr->form_is_constant ())
14532 {
14533 LONGEST offset = attr->constant_value (0);
14534
14535 /* Work around this GCC 11 bug, where it would erroneously use -1
14536 data member locations, instead of 0:
14537
14538 Negative DW_AT_data_member_location
14539 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
14540 */
14541 if (offset == -1 && cu->producer_is_gcc_11)
14542 {
14543 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
14544 offset = 0;
14545 }
14546
14547 field->set_loc_bitpos (offset * bits_per_byte);
14548 }
14549 else if (attr->form_is_section_offset ())
14550 dwarf2_complex_location_expr_complaint ();
14551 else if (attr->form_is_block ())
14552 {
14553 bool handled;
14554 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14555 if (handled)
14556 field->set_loc_bitpos (offset * bits_per_byte);
14557 else
14558 {
14559 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14560 struct objfile *objfile = per_objfile->objfile;
14561 struct dwarf2_locexpr_baton *dlbaton
14562 = XOBNEW (&objfile->objfile_obstack,
14563 struct dwarf2_locexpr_baton);
14564 dlbaton->data = attr->as_block ()->data;
14565 dlbaton->size = attr->as_block ()->size;
14566 /* When using this baton, we want to compute the address
14567 of the field, not the value. This is why
14568 is_reference is set to false here. */
14569 dlbaton->is_reference = false;
14570 dlbaton->per_objfile = per_objfile;
14571 dlbaton->per_cu = cu->per_cu;
14572
14573 field->set_loc_dwarf_block (dlbaton);
14574 }
14575 }
14576 else
14577 dwarf2_complex_location_expr_complaint ();
14578 }
14579 else
14580 {
14581 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14582 if (attr != nullptr)
14583 field->set_loc_bitpos (attr->constant_value (0));
14584 }
14585 }
14586
14587 /* Add an aggregate field to the field list. */
14588
14589 static void
14590 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14591 struct dwarf2_cu *cu)
14592 {
14593 struct objfile *objfile = cu->per_objfile->objfile;
14594 struct gdbarch *gdbarch = objfile->arch ();
14595 struct nextfield *new_field;
14596 struct attribute *attr;
14597 struct field *fp;
14598 const char *fieldname = "";
14599
14600 if (die->tag == DW_TAG_inheritance)
14601 {
14602 fip->baseclasses.emplace_back ();
14603 new_field = &fip->baseclasses.back ();
14604 }
14605 else
14606 {
14607 fip->fields.emplace_back ();
14608 new_field = &fip->fields.back ();
14609 }
14610
14611 new_field->offset = die->sect_off;
14612
14613 new_field->accessibility = dwarf2_access_attribute (die, cu);
14614 if (new_field->accessibility != DW_ACCESS_public)
14615 fip->non_public_fields = true;
14616
14617 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14618 if (attr != nullptr)
14619 new_field->virtuality = attr->as_virtuality ();
14620 else
14621 new_field->virtuality = DW_VIRTUALITY_none;
14622
14623 fp = &new_field->field;
14624
14625 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
14626 && !die_is_declaration (die, cu))
14627 {
14628 if (die->tag == DW_TAG_namelist_item)
14629 {
14630 /* Typically, DW_TAG_namelist_item are references to namelist items.
14631 If so, follow that reference. */
14632 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
14633 struct die_info *item_die = nullptr;
14634 struct dwarf2_cu *item_cu = cu;
14635 if (attr1->form_is_ref ())
14636 item_die = follow_die_ref (die, attr1, &item_cu);
14637 if (item_die != nullptr)
14638 die = item_die;
14639 }
14640 /* Data member other than a C++ static data member. */
14641
14642 /* Get type of field. */
14643 fp->set_type (die_type (die, cu));
14644
14645 fp->set_loc_bitpos (0);
14646
14647 /* Get bit size of field (zero if none). */
14648 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14649 if (attr != nullptr)
14650 {
14651 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14652 }
14653 else
14654 {
14655 FIELD_BITSIZE (*fp) = 0;
14656 }
14657
14658 /* Get bit offset of field. */
14659 handle_member_location (die, cu, fp);
14660 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14661 if (attr != nullptr && attr->form_is_constant ())
14662 {
14663 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14664 {
14665 /* For big endian bits, the DW_AT_bit_offset gives the
14666 additional bit offset from the MSB of the containing
14667 anonymous object to the MSB of the field. We don't
14668 have to do anything special since we don't need to
14669 know the size of the anonymous object. */
14670 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
14671 }
14672 else
14673 {
14674 /* For little endian bits, compute the bit offset to the
14675 MSB of the anonymous object, subtract off the number of
14676 bits from the MSB of the field to the MSB of the
14677 object, and then subtract off the number of bits of
14678 the field itself. The result is the bit offset of
14679 the LSB of the field. */
14680 int anonymous_size;
14681 int bit_offset = attr->constant_value (0);
14682
14683 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14684 if (attr != nullptr && attr->form_is_constant ())
14685 {
14686 /* The size of the anonymous object containing
14687 the bit field is explicit, so use the
14688 indicated size (in bytes). */
14689 anonymous_size = attr->constant_value (0);
14690 }
14691 else
14692 {
14693 /* The size of the anonymous object containing
14694 the bit field must be inferred from the type
14695 attribute of the data member containing the
14696 bit field. */
14697 anonymous_size = TYPE_LENGTH (fp->type ());
14698 }
14699 fp->set_loc_bitpos (fp->loc_bitpos ()
14700 + anonymous_size * bits_per_byte
14701 - bit_offset - FIELD_BITSIZE (*fp));
14702 }
14703 }
14704
14705 /* Get name of field. */
14706 fieldname = dwarf2_name (die, cu);
14707 if (fieldname == NULL)
14708 fieldname = "";
14709
14710 /* The name is already allocated along with this objfile, so we don't
14711 need to duplicate it for the type. */
14712 fp->set_name (fieldname);
14713
14714 /* Change accessibility for artificial fields (e.g. virtual table
14715 pointer or virtual base class pointer) to private. */
14716 if (dwarf2_attr (die, DW_AT_artificial, cu))
14717 {
14718 FIELD_ARTIFICIAL (*fp) = 1;
14719 new_field->accessibility = DW_ACCESS_private;
14720 fip->non_public_fields = true;
14721 }
14722 }
14723 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14724 {
14725 /* C++ static member. */
14726
14727 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14728 is a declaration, but all versions of G++ as of this writing
14729 (so through at least 3.2.1) incorrectly generate
14730 DW_TAG_variable tags. */
14731
14732 const char *physname;
14733
14734 /* Get name of field. */
14735 fieldname = dwarf2_name (die, cu);
14736 if (fieldname == NULL)
14737 return;
14738
14739 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14740 if (attr
14741 /* Only create a symbol if this is an external value.
14742 new_symbol checks this and puts the value in the global symbol
14743 table, which we want. If it is not external, new_symbol
14744 will try to put the value in cu->list_in_scope which is wrong. */
14745 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14746 {
14747 /* A static const member, not much different than an enum as far as
14748 we're concerned, except that we can support more types. */
14749 new_symbol (die, NULL, cu);
14750 }
14751
14752 /* Get physical name. */
14753 physname = dwarf2_physname (fieldname, die, cu);
14754
14755 /* The name is already allocated along with this objfile, so we don't
14756 need to duplicate it for the type. */
14757 fp->set_loc_physname (physname ? physname : "");
14758 fp->set_type (die_type (die, cu));
14759 fp->set_name (fieldname);
14760 }
14761 else if (die->tag == DW_TAG_inheritance)
14762 {
14763 /* C++ base class field. */
14764 handle_member_location (die, cu, fp);
14765 FIELD_BITSIZE (*fp) = 0;
14766 fp->set_type (die_type (die, cu));
14767 fp->set_name (fp->type ()->name ());
14768 }
14769 else
14770 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14771 }
14772
14773 /* Can the type given by DIE define another type? */
14774
14775 static bool
14776 type_can_define_types (const struct die_info *die)
14777 {
14778 switch (die->tag)
14779 {
14780 case DW_TAG_typedef:
14781 case DW_TAG_class_type:
14782 case DW_TAG_structure_type:
14783 case DW_TAG_union_type:
14784 case DW_TAG_enumeration_type:
14785 return true;
14786
14787 default:
14788 return false;
14789 }
14790 }
14791
14792 /* Add a type definition defined in the scope of the FIP's class. */
14793
14794 static void
14795 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14796 struct dwarf2_cu *cu)
14797 {
14798 struct decl_field fp;
14799 memset (&fp, 0, sizeof (fp));
14800
14801 gdb_assert (type_can_define_types (die));
14802
14803 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14804 fp.name = dwarf2_name (die, cu);
14805 fp.type = read_type_die (die, cu);
14806
14807 /* Save accessibility. */
14808 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14809 switch (accessibility)
14810 {
14811 case DW_ACCESS_public:
14812 /* The assumed value if neither private nor protected. */
14813 break;
14814 case DW_ACCESS_private:
14815 fp.is_private = 1;
14816 break;
14817 case DW_ACCESS_protected:
14818 fp.is_protected = 1;
14819 break;
14820 }
14821
14822 if (die->tag == DW_TAG_typedef)
14823 fip->typedef_field_list.push_back (fp);
14824 else
14825 fip->nested_types_list.push_back (fp);
14826 }
14827
14828 /* A convenience typedef that's used when finding the discriminant
14829 field for a variant part. */
14830 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14831 offset_map_type;
14832
14833 /* Compute the discriminant range for a given variant. OBSTACK is
14834 where the results will be stored. VARIANT is the variant to
14835 process. IS_UNSIGNED indicates whether the discriminant is signed
14836 or unsigned. */
14837
14838 static const gdb::array_view<discriminant_range>
14839 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14840 bool is_unsigned)
14841 {
14842 std::vector<discriminant_range> ranges;
14843
14844 if (variant.default_branch)
14845 return {};
14846
14847 if (variant.discr_list_data == nullptr)
14848 {
14849 discriminant_range r
14850 = {variant.discriminant_value, variant.discriminant_value};
14851 ranges.push_back (r);
14852 }
14853 else
14854 {
14855 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14856 variant.discr_list_data->size);
14857 while (!data.empty ())
14858 {
14859 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14860 {
14861 complaint (_("invalid discriminant marker: %d"), data[0]);
14862 break;
14863 }
14864 bool is_range = data[0] == DW_DSC_range;
14865 data = data.slice (1);
14866
14867 ULONGEST low, high;
14868 unsigned int bytes_read;
14869
14870 if (data.empty ())
14871 {
14872 complaint (_("DW_AT_discr_list missing low value"));
14873 break;
14874 }
14875 if (is_unsigned)
14876 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14877 else
14878 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14879 &bytes_read);
14880 data = data.slice (bytes_read);
14881
14882 if (is_range)
14883 {
14884 if (data.empty ())
14885 {
14886 complaint (_("DW_AT_discr_list missing high value"));
14887 break;
14888 }
14889 if (is_unsigned)
14890 high = read_unsigned_leb128 (nullptr, data.data (),
14891 &bytes_read);
14892 else
14893 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14894 &bytes_read);
14895 data = data.slice (bytes_read);
14896 }
14897 else
14898 high = low;
14899
14900 ranges.push_back ({ low, high });
14901 }
14902 }
14903
14904 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14905 ranges.size ());
14906 std::copy (ranges.begin (), ranges.end (), result);
14907 return gdb::array_view<discriminant_range> (result, ranges.size ());
14908 }
14909
14910 static const gdb::array_view<variant_part> create_variant_parts
14911 (struct obstack *obstack,
14912 const offset_map_type &offset_map,
14913 struct field_info *fi,
14914 const std::vector<variant_part_builder> &variant_parts);
14915
14916 /* Fill in a "struct variant" for a given variant field. RESULT is
14917 the variant to fill in. OBSTACK is where any needed allocations
14918 will be done. OFFSET_MAP holds the mapping from section offsets to
14919 fields for the type. FI describes the fields of the type we're
14920 processing. FIELD is the variant field we're converting. */
14921
14922 static void
14923 create_one_variant (variant &result, struct obstack *obstack,
14924 const offset_map_type &offset_map,
14925 struct field_info *fi, const variant_field &field)
14926 {
14927 result.discriminants = convert_variant_range (obstack, field, false);
14928 result.first_field = field.first_field + fi->baseclasses.size ();
14929 result.last_field = field.last_field + fi->baseclasses.size ();
14930 result.parts = create_variant_parts (obstack, offset_map, fi,
14931 field.variant_parts);
14932 }
14933
14934 /* Fill in a "struct variant_part" for a given variant part. RESULT
14935 is the variant part to fill in. OBSTACK is where any needed
14936 allocations will be done. OFFSET_MAP holds the mapping from
14937 section offsets to fields for the type. FI describes the fields of
14938 the type we're processing. BUILDER is the variant part to be
14939 converted. */
14940
14941 static void
14942 create_one_variant_part (variant_part &result,
14943 struct obstack *obstack,
14944 const offset_map_type &offset_map,
14945 struct field_info *fi,
14946 const variant_part_builder &builder)
14947 {
14948 auto iter = offset_map.find (builder.discriminant_offset);
14949 if (iter == offset_map.end ())
14950 {
14951 result.discriminant_index = -1;
14952 /* Doesn't matter. */
14953 result.is_unsigned = false;
14954 }
14955 else
14956 {
14957 result.discriminant_index = iter->second;
14958 result.is_unsigned
14959 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14960 }
14961
14962 size_t n = builder.variants.size ();
14963 variant *output = new (obstack) variant[n];
14964 for (size_t i = 0; i < n; ++i)
14965 create_one_variant (output[i], obstack, offset_map, fi,
14966 builder.variants[i]);
14967
14968 result.variants = gdb::array_view<variant> (output, n);
14969 }
14970
14971 /* Create a vector of variant parts that can be attached to a type.
14972 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14973 holds the mapping from section offsets to fields for the type. FI
14974 describes the fields of the type we're processing. VARIANT_PARTS
14975 is the vector to convert. */
14976
14977 static const gdb::array_view<variant_part>
14978 create_variant_parts (struct obstack *obstack,
14979 const offset_map_type &offset_map,
14980 struct field_info *fi,
14981 const std::vector<variant_part_builder> &variant_parts)
14982 {
14983 if (variant_parts.empty ())
14984 return {};
14985
14986 size_t n = variant_parts.size ();
14987 variant_part *result = new (obstack) variant_part[n];
14988 for (size_t i = 0; i < n; ++i)
14989 create_one_variant_part (result[i], obstack, offset_map, fi,
14990 variant_parts[i]);
14991
14992 return gdb::array_view<variant_part> (result, n);
14993 }
14994
14995 /* Compute the variant part vector for FIP, attaching it to TYPE when
14996 done. */
14997
14998 static void
14999 add_variant_property (struct field_info *fip, struct type *type,
15000 struct dwarf2_cu *cu)
15001 {
15002 /* Map section offsets of fields to their field index. Note the
15003 field index here does not take the number of baseclasses into
15004 account. */
15005 offset_map_type offset_map;
15006 for (int i = 0; i < fip->fields.size (); ++i)
15007 offset_map[fip->fields[i].offset] = i;
15008
15009 struct objfile *objfile = cu->per_objfile->objfile;
15010 gdb::array_view<const variant_part> parts
15011 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15012 fip->variant_parts);
15013
15014 struct dynamic_prop prop;
15015 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15016 obstack_copy (&objfile->objfile_obstack, &parts,
15017 sizeof (parts)));
15018
15019 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15020 }
15021
15022 /* Create the vector of fields, and attach it to the type. */
15023
15024 static void
15025 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15026 struct dwarf2_cu *cu)
15027 {
15028 int nfields = fip->nfields ();
15029
15030 /* Record the field count, allocate space for the array of fields,
15031 and create blank accessibility bitfields if necessary. */
15032 type->set_num_fields (nfields);
15033 type->set_fields
15034 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15035
15036 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
15037 {
15038 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15039
15040 TYPE_FIELD_PRIVATE_BITS (type) =
15041 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15042 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15043
15044 TYPE_FIELD_PROTECTED_BITS (type) =
15045 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15046 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15047
15048 TYPE_FIELD_IGNORE_BITS (type) =
15049 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15050 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15051 }
15052
15053 /* If the type has baseclasses, allocate and clear a bit vector for
15054 TYPE_FIELD_VIRTUAL_BITS. */
15055 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
15056 {
15057 int num_bytes = B_BYTES (fip->baseclasses.size ());
15058 unsigned char *pointer;
15059
15060 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15061 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15062 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15063 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15064 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15065 }
15066
15067 if (!fip->variant_parts.empty ())
15068 add_variant_property (fip, type, cu);
15069
15070 /* Copy the saved-up fields into the field vector. */
15071 for (int i = 0; i < nfields; ++i)
15072 {
15073 struct nextfield &field
15074 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15075 : fip->fields[i - fip->baseclasses.size ()]);
15076
15077 type->field (i) = field.field;
15078 switch (field.accessibility)
15079 {
15080 case DW_ACCESS_private:
15081 if (cu->per_cu->lang != language_ada)
15082 SET_TYPE_FIELD_PRIVATE (type, i);
15083 break;
15084
15085 case DW_ACCESS_protected:
15086 if (cu->per_cu->lang != language_ada)
15087 SET_TYPE_FIELD_PROTECTED (type, i);
15088 break;
15089
15090 case DW_ACCESS_public:
15091 break;
15092
15093 default:
15094 /* Unknown accessibility. Complain and treat it as public. */
15095 {
15096 complaint (_("unsupported accessibility %d"),
15097 field.accessibility);
15098 }
15099 break;
15100 }
15101 if (i < fip->baseclasses.size ())
15102 {
15103 switch (field.virtuality)
15104 {
15105 case DW_VIRTUALITY_virtual:
15106 case DW_VIRTUALITY_pure_virtual:
15107 if (cu->per_cu->lang == language_ada)
15108 error (_("unexpected virtuality in component of Ada type"));
15109 SET_TYPE_FIELD_VIRTUAL (type, i);
15110 break;
15111 }
15112 }
15113 }
15114 }
15115
15116 /* Return true if this member function is a constructor, false
15117 otherwise. */
15118
15119 static int
15120 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15121 {
15122 const char *fieldname;
15123 const char *type_name;
15124 int len;
15125
15126 if (die->parent == NULL)
15127 return 0;
15128
15129 if (die->parent->tag != DW_TAG_structure_type
15130 && die->parent->tag != DW_TAG_union_type
15131 && die->parent->tag != DW_TAG_class_type)
15132 return 0;
15133
15134 fieldname = dwarf2_name (die, cu);
15135 type_name = dwarf2_name (die->parent, cu);
15136 if (fieldname == NULL || type_name == NULL)
15137 return 0;
15138
15139 len = strlen (fieldname);
15140 return (strncmp (fieldname, type_name, len) == 0
15141 && (type_name[len] == '\0' || type_name[len] == '<'));
15142 }
15143
15144 /* Add a member function to the proper fieldlist. */
15145
15146 static void
15147 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15148 struct type *type, struct dwarf2_cu *cu)
15149 {
15150 struct objfile *objfile = cu->per_objfile->objfile;
15151 struct attribute *attr;
15152 int i;
15153 struct fnfieldlist *flp = nullptr;
15154 struct fn_field *fnp;
15155 const char *fieldname;
15156 struct type *this_type;
15157
15158 if (cu->per_cu->lang == language_ada)
15159 error (_("unexpected member function in Ada type"));
15160
15161 /* Get name of member function. */
15162 fieldname = dwarf2_name (die, cu);
15163 if (fieldname == NULL)
15164 return;
15165
15166 /* Look up member function name in fieldlist. */
15167 for (i = 0; i < fip->fnfieldlists.size (); i++)
15168 {
15169 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15170 {
15171 flp = &fip->fnfieldlists[i];
15172 break;
15173 }
15174 }
15175
15176 /* Create a new fnfieldlist if necessary. */
15177 if (flp == nullptr)
15178 {
15179 fip->fnfieldlists.emplace_back ();
15180 flp = &fip->fnfieldlists.back ();
15181 flp->name = fieldname;
15182 i = fip->fnfieldlists.size () - 1;
15183 }
15184
15185 /* Create a new member function field and add it to the vector of
15186 fnfieldlists. */
15187 flp->fnfields.emplace_back ();
15188 fnp = &flp->fnfields.back ();
15189
15190 /* Delay processing of the physname until later. */
15191 if (cu->per_cu->lang == language_cplus)
15192 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15193 die, cu);
15194 else
15195 {
15196 const char *physname = dwarf2_physname (fieldname, die, cu);
15197 fnp->physname = physname ? physname : "";
15198 }
15199
15200 fnp->type = alloc_type (objfile);
15201 this_type = read_type_die (die, cu);
15202 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15203 {
15204 int nparams = this_type->num_fields ();
15205
15206 /* TYPE is the domain of this method, and THIS_TYPE is the type
15207 of the method itself (TYPE_CODE_METHOD). */
15208 smash_to_method_type (fnp->type, type,
15209 TYPE_TARGET_TYPE (this_type),
15210 this_type->fields (),
15211 this_type->num_fields (),
15212 this_type->has_varargs ());
15213
15214 /* Handle static member functions.
15215 Dwarf2 has no clean way to discern C++ static and non-static
15216 member functions. G++ helps GDB by marking the first
15217 parameter for non-static member functions (which is the this
15218 pointer) as artificial. We obtain this information from
15219 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15220 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15221 fnp->voffset = VOFFSET_STATIC;
15222 }
15223 else
15224 complaint (_("member function type missing for '%s'"),
15225 dwarf2_full_name (fieldname, die, cu));
15226
15227 /* Get fcontext from DW_AT_containing_type if present. */
15228 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15229 fnp->fcontext = die_containing_type (die, cu);
15230
15231 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15232 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15233
15234 /* Get accessibility. */
15235 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15236 switch (accessibility)
15237 {
15238 case DW_ACCESS_private:
15239 fnp->is_private = 1;
15240 break;
15241 case DW_ACCESS_protected:
15242 fnp->is_protected = 1;
15243 break;
15244 }
15245
15246 /* Check for artificial methods. */
15247 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15248 if (attr && attr->as_boolean ())
15249 fnp->is_artificial = 1;
15250
15251 /* Check for defaulted methods. */
15252 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15253 if (attr != nullptr)
15254 fnp->defaulted = attr->defaulted ();
15255
15256 /* Check for deleted methods. */
15257 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15258 if (attr != nullptr && attr->as_boolean ())
15259 fnp->is_deleted = 1;
15260
15261 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15262
15263 /* Get index in virtual function table if it is a virtual member
15264 function. For older versions of GCC, this is an offset in the
15265 appropriate virtual table, as specified by DW_AT_containing_type.
15266 For everyone else, it is an expression to be evaluated relative
15267 to the object address. */
15268
15269 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15270 if (attr != nullptr)
15271 {
15272 if (attr->form_is_block () && attr->as_block ()->size > 0)
15273 {
15274 struct dwarf_block *block = attr->as_block ();
15275
15276 if (block->data[0] == DW_OP_constu)
15277 {
15278 /* Old-style GCC. */
15279 fnp->voffset = decode_locdesc (block, cu) + 2;
15280 }
15281 else if (block->data[0] == DW_OP_deref
15282 || (block->size > 1
15283 && block->data[0] == DW_OP_deref_size
15284 && block->data[1] == cu->header.addr_size))
15285 {
15286 fnp->voffset = decode_locdesc (block, cu);
15287 if ((fnp->voffset % cu->header.addr_size) != 0)
15288 dwarf2_complex_location_expr_complaint ();
15289 else
15290 fnp->voffset /= cu->header.addr_size;
15291 fnp->voffset += 2;
15292 }
15293 else
15294 dwarf2_complex_location_expr_complaint ();
15295
15296 if (!fnp->fcontext)
15297 {
15298 /* If there is no `this' field and no DW_AT_containing_type,
15299 we cannot actually find a base class context for the
15300 vtable! */
15301 if (this_type->num_fields () == 0
15302 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15303 {
15304 complaint (_("cannot determine context for virtual member "
15305 "function \"%s\" (offset %s)"),
15306 fieldname, sect_offset_str (die->sect_off));
15307 }
15308 else
15309 {
15310 fnp->fcontext
15311 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15312 }
15313 }
15314 }
15315 else if (attr->form_is_section_offset ())
15316 {
15317 dwarf2_complex_location_expr_complaint ();
15318 }
15319 else
15320 {
15321 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15322 fieldname);
15323 }
15324 }
15325 else
15326 {
15327 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15328 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15329 {
15330 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15331 complaint (_("Member function \"%s\" (offset %s) is virtual "
15332 "but the vtable offset is not specified"),
15333 fieldname, sect_offset_str (die->sect_off));
15334 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15335 TYPE_CPLUS_DYNAMIC (type) = 1;
15336 }
15337 }
15338 }
15339
15340 /* Create the vector of member function fields, and attach it to the type. */
15341
15342 static void
15343 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15344 struct dwarf2_cu *cu)
15345 {
15346 if (cu->per_cu->lang == language_ada)
15347 error (_("unexpected member functions in Ada type"));
15348
15349 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15350 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15351 TYPE_ALLOC (type,
15352 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15353
15354 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15355 {
15356 struct fnfieldlist &nf = fip->fnfieldlists[i];
15357 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15358
15359 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15360 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15361 fn_flp->fn_fields = (struct fn_field *)
15362 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15363
15364 for (int k = 0; k < nf.fnfields.size (); ++k)
15365 fn_flp->fn_fields[k] = nf.fnfields[k];
15366 }
15367
15368 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15369 }
15370
15371 /* Returns non-zero if NAME is the name of a vtable member in CU's
15372 language, zero otherwise. */
15373 static int
15374 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15375 {
15376 static const char vptr[] = "_vptr";
15377
15378 /* Look for the C++ form of the vtable. */
15379 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15380 return 1;
15381
15382 return 0;
15383 }
15384
15385 /* GCC outputs unnamed structures that are really pointers to member
15386 functions, with the ABI-specified layout. If TYPE describes
15387 such a structure, smash it into a member function type.
15388
15389 GCC shouldn't do this; it should just output pointer to member DIEs.
15390 This is GCC PR debug/28767. */
15391
15392 static void
15393 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15394 {
15395 struct type *pfn_type, *self_type, *new_type;
15396
15397 /* Check for a structure with no name and two children. */
15398 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15399 return;
15400
15401 /* Check for __pfn and __delta members. */
15402 if (type->field (0).name () == NULL
15403 || strcmp (type->field (0).name (), "__pfn") != 0
15404 || type->field (1).name () == NULL
15405 || strcmp (type->field (1).name (), "__delta") != 0)
15406 return;
15407
15408 /* Find the type of the method. */
15409 pfn_type = type->field (0).type ();
15410 if (pfn_type == NULL
15411 || pfn_type->code () != TYPE_CODE_PTR
15412 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15413 return;
15414
15415 /* Look for the "this" argument. */
15416 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15417 if (pfn_type->num_fields () == 0
15418 /* || pfn_type->field (0).type () == NULL */
15419 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15420 return;
15421
15422 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15423 new_type = alloc_type (objfile);
15424 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15425 pfn_type->fields (), pfn_type->num_fields (),
15426 pfn_type->has_varargs ());
15427 smash_to_methodptr_type (type, new_type);
15428 }
15429
15430 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15431 requires rewriting, then copy it and return the updated copy.
15432 Otherwise return nullptr. */
15433
15434 static struct type *
15435 rewrite_array_type (struct type *type)
15436 {
15437 if (type->code () != TYPE_CODE_ARRAY)
15438 return nullptr;
15439
15440 struct type *index_type = type->index_type ();
15441 range_bounds *current_bounds = index_type->bounds ();
15442
15443 /* Handle multi-dimensional arrays. */
15444 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15445 if (new_target == nullptr)
15446 {
15447 /* Maybe we don't need to rewrite this array. */
15448 if (current_bounds->low.kind () == PROP_CONST
15449 && current_bounds->high.kind () == PROP_CONST)
15450 return nullptr;
15451 }
15452
15453 /* Either the target type was rewritten, or the bounds have to be
15454 updated. Either way we want to copy the type and update
15455 everything. */
15456 struct type *copy = copy_type (type);
15457 int nfields = copy->num_fields ();
15458 field *new_fields
15459 = ((struct field *) TYPE_ZALLOC (copy,
15460 nfields * sizeof (struct field)));
15461 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15462 copy->set_fields (new_fields);
15463 if (new_target != nullptr)
15464 TYPE_TARGET_TYPE (copy) = new_target;
15465
15466 struct type *index_copy = copy_type (index_type);
15467 range_bounds *bounds
15468 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15469 sizeof (range_bounds));
15470 *bounds = *current_bounds;
15471 bounds->low.set_const_val (1);
15472 bounds->high.set_const_val (0);
15473 index_copy->set_bounds (bounds);
15474 copy->set_index_type (index_copy);
15475
15476 return copy;
15477 }
15478
15479 /* While some versions of GCC will generate complicated DWARF for an
15480 array (see quirk_ada_thick_pointer), more recent versions were
15481 modified to emit an explicit thick pointer structure. However, in
15482 this case, the array still has DWARF expressions for its ranges,
15483 and these must be ignored. */
15484
15485 static void
15486 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15487 struct type *type)
15488 {
15489 gdb_assert (cu->per_cu->lang == language_ada);
15490
15491 /* Check for a structure with two children. */
15492 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15493 return;
15494
15495 /* Check for P_ARRAY and P_BOUNDS members. */
15496 if (type->field (0).name () == NULL
15497 || strcmp (type->field (0).name (), "P_ARRAY") != 0
15498 || type->field (1).name () == NULL
15499 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
15500 return;
15501
15502 /* Make sure we're looking at a pointer to an array. */
15503 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15504 return;
15505
15506 /* The Ada code already knows how to handle these types, so all that
15507 we need to do is turn the bounds into static bounds. However, we
15508 don't want to rewrite existing array or index types in-place,
15509 because those may be referenced in other contexts where this
15510 rewriting is undesirable. */
15511 struct type *new_ary_type
15512 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15513 if (new_ary_type != nullptr)
15514 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15515 }
15516
15517 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15518 appropriate error checking and issuing complaints if there is a
15519 problem. */
15520
15521 static ULONGEST
15522 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15523 {
15524 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15525
15526 if (attr == nullptr)
15527 return 0;
15528
15529 if (!attr->form_is_constant ())
15530 {
15531 complaint (_("DW_AT_alignment must have constant form"
15532 " - DIE at %s [in module %s]"),
15533 sect_offset_str (die->sect_off),
15534 objfile_name (cu->per_objfile->objfile));
15535 return 0;
15536 }
15537
15538 LONGEST val = attr->constant_value (0);
15539 if (val < 0)
15540 {
15541 complaint (_("DW_AT_alignment value must not be negative"
15542 " - DIE at %s [in module %s]"),
15543 sect_offset_str (die->sect_off),
15544 objfile_name (cu->per_objfile->objfile));
15545 return 0;
15546 }
15547 ULONGEST align = val;
15548
15549 if (align == 0)
15550 {
15551 complaint (_("DW_AT_alignment value must not be zero"
15552 " - DIE at %s [in module %s]"),
15553 sect_offset_str (die->sect_off),
15554 objfile_name (cu->per_objfile->objfile));
15555 return 0;
15556 }
15557 if ((align & (align - 1)) != 0)
15558 {
15559 complaint (_("DW_AT_alignment value must be a power of 2"
15560 " - DIE at %s [in module %s]"),
15561 sect_offset_str (die->sect_off),
15562 objfile_name (cu->per_objfile->objfile));
15563 return 0;
15564 }
15565
15566 return align;
15567 }
15568
15569 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15570 the alignment for TYPE. */
15571
15572 static void
15573 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15574 struct type *type)
15575 {
15576 if (!set_type_align (type, get_alignment (cu, die)))
15577 complaint (_("DW_AT_alignment value too large"
15578 " - DIE at %s [in module %s]"),
15579 sect_offset_str (die->sect_off),
15580 objfile_name (cu->per_objfile->objfile));
15581 }
15582
15583 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15584 constant for a type, according to DWARF5 spec, Table 5.5. */
15585
15586 static bool
15587 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15588 {
15589 switch (value)
15590 {
15591 case DW_CC_normal:
15592 case DW_CC_pass_by_reference:
15593 case DW_CC_pass_by_value:
15594 return true;
15595
15596 default:
15597 complaint (_("unrecognized DW_AT_calling_convention value "
15598 "(%s) for a type"), pulongest (value));
15599 return false;
15600 }
15601 }
15602
15603 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15604 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15605 also according to GNU-specific values (see include/dwarf2.h). */
15606
15607 static bool
15608 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15609 {
15610 switch (value)
15611 {
15612 case DW_CC_normal:
15613 case DW_CC_program:
15614 case DW_CC_nocall:
15615 return true;
15616
15617 case DW_CC_GNU_renesas_sh:
15618 case DW_CC_GNU_borland_fastcall_i386:
15619 case DW_CC_GDB_IBM_OpenCL:
15620 return true;
15621
15622 default:
15623 complaint (_("unrecognized DW_AT_calling_convention value "
15624 "(%s) for a subroutine"), pulongest (value));
15625 return false;
15626 }
15627 }
15628
15629 /* Called when we find the DIE that starts a structure or union scope
15630 (definition) to create a type for the structure or union. Fill in
15631 the type's name and general properties; the members will not be
15632 processed until process_structure_scope. A symbol table entry for
15633 the type will also not be done until process_structure_scope (assuming
15634 the type has a name).
15635
15636 NOTE: we need to call these functions regardless of whether or not the
15637 DIE has a DW_AT_name attribute, since it might be an anonymous
15638 structure or union. This gets the type entered into our set of
15639 user defined types. */
15640
15641 static struct type *
15642 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15643 {
15644 struct objfile *objfile = cu->per_objfile->objfile;
15645 struct type *type;
15646 struct attribute *attr;
15647 const char *name;
15648
15649 /* If the definition of this type lives in .debug_types, read that type.
15650 Don't follow DW_AT_specification though, that will take us back up
15651 the chain and we want to go down. */
15652 attr = die->attr (DW_AT_signature);
15653 if (attr != nullptr)
15654 {
15655 type = get_DW_AT_signature_type (die, attr, cu);
15656
15657 /* The type's CU may not be the same as CU.
15658 Ensure TYPE is recorded with CU in die_type_hash. */
15659 return set_die_type (die, type, cu);
15660 }
15661
15662 type = alloc_type (objfile);
15663 INIT_CPLUS_SPECIFIC (type);
15664
15665 name = dwarf2_name (die, cu);
15666 if (name != NULL)
15667 {
15668 if (cu->per_cu->lang == language_cplus
15669 || cu->per_cu->lang == language_d
15670 || cu->per_cu->lang == language_rust)
15671 {
15672 const char *full_name = dwarf2_full_name (name, die, cu);
15673
15674 /* dwarf2_full_name might have already finished building the DIE's
15675 type. If so, there is no need to continue. */
15676 if (get_die_type (die, cu) != NULL)
15677 return get_die_type (die, cu);
15678
15679 type->set_name (full_name);
15680 }
15681 else
15682 {
15683 /* The name is already allocated along with this objfile, so
15684 we don't need to duplicate it for the type. */
15685 type->set_name (name);
15686 }
15687 }
15688
15689 if (die->tag == DW_TAG_structure_type)
15690 {
15691 type->set_code (TYPE_CODE_STRUCT);
15692 }
15693 else if (die->tag == DW_TAG_union_type)
15694 {
15695 type->set_code (TYPE_CODE_UNION);
15696 }
15697 else if (die->tag == DW_TAG_namelist)
15698 {
15699 type->set_code (TYPE_CODE_NAMELIST);
15700 }
15701 else
15702 {
15703 type->set_code (TYPE_CODE_STRUCT);
15704 }
15705
15706 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
15707 type->set_is_declared_class (true);
15708
15709 /* Store the calling convention in the type if it's available in
15710 the die. Otherwise the calling convention remains set to
15711 the default value DW_CC_normal. */
15712 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15713 if (attr != nullptr
15714 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15715 {
15716 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15717 TYPE_CPLUS_CALLING_CONVENTION (type)
15718 = (enum dwarf_calling_convention) (attr->constant_value (0));
15719 }
15720
15721 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15722 if (attr != nullptr)
15723 {
15724 if (attr->form_is_constant ())
15725 TYPE_LENGTH (type) = attr->constant_value (0);
15726 else
15727 {
15728 struct dynamic_prop prop;
15729 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15730 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15731 TYPE_LENGTH (type) = 0;
15732 }
15733 }
15734 else
15735 {
15736 TYPE_LENGTH (type) = 0;
15737 }
15738
15739 maybe_set_alignment (cu, die, type);
15740
15741 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15742 {
15743 /* ICC<14 does not output the required DW_AT_declaration on
15744 incomplete types, but gives them a size of zero. */
15745 type->set_is_stub (true);
15746 }
15747 else
15748 type->set_stub_is_supported (true);
15749
15750 if (die_is_declaration (die, cu))
15751 type->set_is_stub (true);
15752 else if (attr == NULL && die->child == NULL
15753 && producer_is_realview (cu->producer))
15754 /* RealView does not output the required DW_AT_declaration
15755 on incomplete types. */
15756 type->set_is_stub (true);
15757
15758 /* We need to add the type field to the die immediately so we don't
15759 infinitely recurse when dealing with pointers to the structure
15760 type within the structure itself. */
15761 set_die_type (die, type, cu);
15762
15763 /* set_die_type should be already done. */
15764 set_descriptive_type (type, die, cu);
15765
15766 return type;
15767 }
15768
15769 static void handle_struct_member_die
15770 (struct die_info *child_die,
15771 struct type *type,
15772 struct field_info *fi,
15773 std::vector<struct symbol *> *template_args,
15774 struct dwarf2_cu *cu);
15775
15776 /* A helper for handle_struct_member_die that handles
15777 DW_TAG_variant_part. */
15778
15779 static void
15780 handle_variant_part (struct die_info *die, struct type *type,
15781 struct field_info *fi,
15782 std::vector<struct symbol *> *template_args,
15783 struct dwarf2_cu *cu)
15784 {
15785 variant_part_builder *new_part;
15786 if (fi->current_variant_part == nullptr)
15787 {
15788 fi->variant_parts.emplace_back ();
15789 new_part = &fi->variant_parts.back ();
15790 }
15791 else if (!fi->current_variant_part->processing_variant)
15792 {
15793 complaint (_("nested DW_TAG_variant_part seen "
15794 "- DIE at %s [in module %s]"),
15795 sect_offset_str (die->sect_off),
15796 objfile_name (cu->per_objfile->objfile));
15797 return;
15798 }
15799 else
15800 {
15801 variant_field &current = fi->current_variant_part->variants.back ();
15802 current.variant_parts.emplace_back ();
15803 new_part = &current.variant_parts.back ();
15804 }
15805
15806 /* When we recurse, we want callees to add to this new variant
15807 part. */
15808 scoped_restore save_current_variant_part
15809 = make_scoped_restore (&fi->current_variant_part, new_part);
15810
15811 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15812 if (discr == NULL)
15813 {
15814 /* It's a univariant form, an extension we support. */
15815 }
15816 else if (discr->form_is_ref ())
15817 {
15818 struct dwarf2_cu *target_cu = cu;
15819 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15820
15821 new_part->discriminant_offset = target_die->sect_off;
15822 }
15823 else
15824 {
15825 complaint (_("DW_AT_discr does not have DIE reference form"
15826 " - DIE at %s [in module %s]"),
15827 sect_offset_str (die->sect_off),
15828 objfile_name (cu->per_objfile->objfile));
15829 }
15830
15831 for (die_info *child_die = die->child;
15832 child_die != NULL;
15833 child_die = child_die->sibling)
15834 handle_struct_member_die (child_die, type, fi, template_args, cu);
15835 }
15836
15837 /* A helper for handle_struct_member_die that handles
15838 DW_TAG_variant. */
15839
15840 static void
15841 handle_variant (struct die_info *die, struct type *type,
15842 struct field_info *fi,
15843 std::vector<struct symbol *> *template_args,
15844 struct dwarf2_cu *cu)
15845 {
15846 if (fi->current_variant_part == nullptr)
15847 {
15848 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15849 "- DIE at %s [in module %s]"),
15850 sect_offset_str (die->sect_off),
15851 objfile_name (cu->per_objfile->objfile));
15852 return;
15853 }
15854 if (fi->current_variant_part->processing_variant)
15855 {
15856 complaint (_("nested DW_TAG_variant seen "
15857 "- DIE at %s [in module %s]"),
15858 sect_offset_str (die->sect_off),
15859 objfile_name (cu->per_objfile->objfile));
15860 return;
15861 }
15862
15863 scoped_restore save_processing_variant
15864 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15865 true);
15866
15867 fi->current_variant_part->variants.emplace_back ();
15868 variant_field &variant = fi->current_variant_part->variants.back ();
15869 variant.first_field = fi->fields.size ();
15870
15871 /* In a variant we want to get the discriminant and also add a
15872 field for our sole member child. */
15873 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15874 if (discr == nullptr || !discr->form_is_constant ())
15875 {
15876 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15877 if (discr == nullptr || discr->as_block ()->size == 0)
15878 variant.default_branch = true;
15879 else
15880 variant.discr_list_data = discr->as_block ();
15881 }
15882 else
15883 variant.discriminant_value = discr->constant_value (0);
15884
15885 for (die_info *variant_child = die->child;
15886 variant_child != NULL;
15887 variant_child = variant_child->sibling)
15888 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15889
15890 variant.last_field = fi->fields.size ();
15891 }
15892
15893 /* A helper for process_structure_scope that handles a single member
15894 DIE. */
15895
15896 static void
15897 handle_struct_member_die (struct die_info *child_die, struct type *type,
15898 struct field_info *fi,
15899 std::vector<struct symbol *> *template_args,
15900 struct dwarf2_cu *cu)
15901 {
15902 if (child_die->tag == DW_TAG_member
15903 || child_die->tag == DW_TAG_variable
15904 || child_die->tag == DW_TAG_namelist_item)
15905 {
15906 /* NOTE: carlton/2002-11-05: A C++ static data member
15907 should be a DW_TAG_member that is a declaration, but
15908 all versions of G++ as of this writing (so through at
15909 least 3.2.1) incorrectly generate DW_TAG_variable
15910 tags for them instead. */
15911 dwarf2_add_field (fi, child_die, cu);
15912 }
15913 else if (child_die->tag == DW_TAG_subprogram)
15914 {
15915 /* Rust doesn't have member functions in the C++ sense.
15916 However, it does emit ordinary functions as children
15917 of a struct DIE. */
15918 if (cu->per_cu->lang == language_rust)
15919 read_func_scope (child_die, cu);
15920 else
15921 {
15922 /* C++ member function. */
15923 dwarf2_add_member_fn (fi, child_die, type, cu);
15924 }
15925 }
15926 else if (child_die->tag == DW_TAG_inheritance)
15927 {
15928 /* C++ base class field. */
15929 dwarf2_add_field (fi, child_die, cu);
15930 }
15931 else if (type_can_define_types (child_die))
15932 dwarf2_add_type_defn (fi, child_die, cu);
15933 else if (child_die->tag == DW_TAG_template_type_param
15934 || child_die->tag == DW_TAG_template_value_param)
15935 {
15936 struct symbol *arg = new_symbol (child_die, NULL, cu);
15937
15938 if (arg != NULL)
15939 template_args->push_back (arg);
15940 }
15941 else if (child_die->tag == DW_TAG_variant_part)
15942 handle_variant_part (child_die, type, fi, template_args, cu);
15943 else if (child_die->tag == DW_TAG_variant)
15944 handle_variant (child_die, type, fi, template_args, cu);
15945 }
15946
15947 /* Finish creating a structure or union type, including filling in its
15948 members and creating a symbol for it. This function also handles Fortran
15949 namelist variables, their items or members and creating a symbol for
15950 them. */
15951
15952 static void
15953 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15954 {
15955 struct objfile *objfile = cu->per_objfile->objfile;
15956 struct die_info *child_die;
15957 struct type *type;
15958
15959 type = get_die_type (die, cu);
15960 if (type == NULL)
15961 type = read_structure_type (die, cu);
15962
15963 bool has_template_parameters = false;
15964 if (die->child != NULL && ! die_is_declaration (die, cu))
15965 {
15966 struct field_info fi;
15967 std::vector<struct symbol *> template_args;
15968
15969 child_die = die->child;
15970
15971 while (child_die && child_die->tag)
15972 {
15973 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15974 child_die = child_die->sibling;
15975 }
15976
15977 /* Attach template arguments to type. */
15978 if (!template_args.empty ())
15979 {
15980 has_template_parameters = true;
15981 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15982 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15983 TYPE_TEMPLATE_ARGUMENTS (type)
15984 = XOBNEWVEC (&objfile->objfile_obstack,
15985 struct symbol *,
15986 TYPE_N_TEMPLATE_ARGUMENTS (type));
15987 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15988 template_args.data (),
15989 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15990 * sizeof (struct symbol *)));
15991 }
15992
15993 /* Attach fields and member functions to the type. */
15994 if (fi.nfields () > 0)
15995 dwarf2_attach_fields_to_type (&fi, type, cu);
15996 if (!fi.fnfieldlists.empty ())
15997 {
15998 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15999
16000 /* Get the type which refers to the base class (possibly this
16001 class itself) which contains the vtable pointer for the current
16002 class from the DW_AT_containing_type attribute. This use of
16003 DW_AT_containing_type is a GNU extension. */
16004
16005 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16006 {
16007 struct type *t = die_containing_type (die, cu);
16008
16009 set_type_vptr_basetype (type, t);
16010 if (type == t)
16011 {
16012 int i;
16013
16014 /* Our own class provides vtbl ptr. */
16015 for (i = t->num_fields () - 1;
16016 i >= TYPE_N_BASECLASSES (t);
16017 --i)
16018 {
16019 const char *fieldname = t->field (i).name ();
16020
16021 if (is_vtable_name (fieldname, cu))
16022 {
16023 set_type_vptr_fieldno (type, i);
16024 break;
16025 }
16026 }
16027
16028 /* Complain if virtual function table field not found. */
16029 if (i < TYPE_N_BASECLASSES (t))
16030 complaint (_("virtual function table pointer "
16031 "not found when defining class '%s'"),
16032 type->name () ? type->name () : "");
16033 }
16034 else
16035 {
16036 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16037 }
16038 }
16039 else if (cu->producer
16040 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16041 {
16042 /* The IBM XLC compiler does not provide direct indication
16043 of the containing type, but the vtable pointer is
16044 always named __vfp. */
16045
16046 int i;
16047
16048 for (i = type->num_fields () - 1;
16049 i >= TYPE_N_BASECLASSES (type);
16050 --i)
16051 {
16052 if (strcmp (type->field (i).name (), "__vfp") == 0)
16053 {
16054 set_type_vptr_fieldno (type, i);
16055 set_type_vptr_basetype (type, type);
16056 break;
16057 }
16058 }
16059 }
16060 }
16061
16062 /* Copy fi.typedef_field_list linked list elements content into the
16063 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16064 if (!fi.typedef_field_list.empty ())
16065 {
16066 int count = fi.typedef_field_list.size ();
16067
16068 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16069 TYPE_TYPEDEF_FIELD_ARRAY (type)
16070 = ((struct decl_field *)
16071 TYPE_ALLOC (type,
16072 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16073 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16074
16075 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16076 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16077 }
16078
16079 /* Copy fi.nested_types_list linked list elements content into the
16080 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16081 if (!fi.nested_types_list.empty ()
16082 && cu->per_cu->lang != language_ada)
16083 {
16084 int count = fi.nested_types_list.size ();
16085
16086 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16087 TYPE_NESTED_TYPES_ARRAY (type)
16088 = ((struct decl_field *)
16089 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16090 TYPE_NESTED_TYPES_COUNT (type) = count;
16091
16092 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16093 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16094 }
16095 }
16096
16097 quirk_gcc_member_function_pointer (type, objfile);
16098 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
16099 cu->rust_unions.push_back (type);
16100 else if (cu->per_cu->lang == language_ada)
16101 quirk_ada_thick_pointer_struct (die, cu, type);
16102
16103 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16104 snapshots) has been known to create a die giving a declaration
16105 for a class that has, as a child, a die giving a definition for a
16106 nested class. So we have to process our children even if the
16107 current die is a declaration. Normally, of course, a declaration
16108 won't have any children at all. */
16109
16110 child_die = die->child;
16111
16112 while (child_die != NULL && child_die->tag)
16113 {
16114 if (child_die->tag == DW_TAG_member
16115 || child_die->tag == DW_TAG_variable
16116 || child_die->tag == DW_TAG_inheritance
16117 || child_die->tag == DW_TAG_template_value_param
16118 || child_die->tag == DW_TAG_template_type_param)
16119 {
16120 /* Do nothing. */
16121 }
16122 else
16123 process_die (child_die, cu);
16124
16125 child_die = child_die->sibling;
16126 }
16127
16128 /* Do not consider external references. According to the DWARF standard,
16129 these DIEs are identified by the fact that they have no byte_size
16130 attribute, and a declaration attribute. */
16131 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16132 || !die_is_declaration (die, cu)
16133 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16134 {
16135 struct symbol *sym = new_symbol (die, type, cu);
16136
16137 if (has_template_parameters)
16138 {
16139 struct symtab *symtab;
16140 if (sym != nullptr)
16141 symtab = symbol_symtab (sym);
16142 else if (cu->line_header != nullptr)
16143 {
16144 /* Any related symtab will do. */
16145 symtab
16146 = cu->line_header->file_names ()[0].symtab;
16147 }
16148 else
16149 {
16150 symtab = nullptr;
16151 complaint (_("could not find suitable "
16152 "symtab for template parameter"
16153 " - DIE at %s [in module %s]"),
16154 sect_offset_str (die->sect_off),
16155 objfile_name (objfile));
16156 }
16157
16158 if (symtab != nullptr)
16159 {
16160 /* Make sure that the symtab is set on the new symbols.
16161 Even though they don't appear in this symtab directly,
16162 other parts of gdb assume that symbols do, and this is
16163 reasonably true. */
16164 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16165 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16166 }
16167 }
16168 }
16169 }
16170
16171 /* Assuming DIE is an enumeration type, and TYPE is its associated
16172 type, update TYPE using some information only available in DIE's
16173 children. In particular, the fields are computed. */
16174
16175 static void
16176 update_enumeration_type_from_children (struct die_info *die,
16177 struct type *type,
16178 struct dwarf2_cu *cu)
16179 {
16180 struct die_info *child_die;
16181 int unsigned_enum = 1;
16182 int flag_enum = 1;
16183
16184 auto_obstack obstack;
16185 std::vector<struct field> fields;
16186
16187 for (child_die = die->child;
16188 child_die != NULL && child_die->tag;
16189 child_die = child_die->sibling)
16190 {
16191 struct attribute *attr;
16192 LONGEST value;
16193 const gdb_byte *bytes;
16194 struct dwarf2_locexpr_baton *baton;
16195 const char *name;
16196
16197 if (child_die->tag != DW_TAG_enumerator)
16198 continue;
16199
16200 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16201 if (attr == NULL)
16202 continue;
16203
16204 name = dwarf2_name (child_die, cu);
16205 if (name == NULL)
16206 name = "<anonymous enumerator>";
16207
16208 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16209 &value, &bytes, &baton);
16210 if (value < 0)
16211 {
16212 unsigned_enum = 0;
16213 flag_enum = 0;
16214 }
16215 else
16216 {
16217 if (count_one_bits_ll (value) >= 2)
16218 flag_enum = 0;
16219 }
16220
16221 fields.emplace_back ();
16222 struct field &field = fields.back ();
16223 field.set_name (dwarf2_physname (name, child_die, cu));
16224 field.set_loc_enumval (value);
16225 }
16226
16227 if (!fields.empty ())
16228 {
16229 type->set_num_fields (fields.size ());
16230 type->set_fields
16231 ((struct field *)
16232 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16233 memcpy (type->fields (), fields.data (),
16234 sizeof (struct field) * fields.size ());
16235 }
16236
16237 if (unsigned_enum)
16238 type->set_is_unsigned (true);
16239
16240 if (flag_enum)
16241 type->set_is_flag_enum (true);
16242 }
16243
16244 /* Given a DW_AT_enumeration_type die, set its type. We do not
16245 complete the type's fields yet, or create any symbols. */
16246
16247 static struct type *
16248 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16249 {
16250 struct objfile *objfile = cu->per_objfile->objfile;
16251 struct type *type;
16252 struct attribute *attr;
16253 const char *name;
16254
16255 /* If the definition of this type lives in .debug_types, read that type.
16256 Don't follow DW_AT_specification though, that will take us back up
16257 the chain and we want to go down. */
16258 attr = die->attr (DW_AT_signature);
16259 if (attr != nullptr)
16260 {
16261 type = get_DW_AT_signature_type (die, attr, cu);
16262
16263 /* The type's CU may not be the same as CU.
16264 Ensure TYPE is recorded with CU in die_type_hash. */
16265 return set_die_type (die, type, cu);
16266 }
16267
16268 type = alloc_type (objfile);
16269
16270 type->set_code (TYPE_CODE_ENUM);
16271 name = dwarf2_full_name (NULL, die, cu);
16272 if (name != NULL)
16273 type->set_name (name);
16274
16275 attr = dwarf2_attr (die, DW_AT_type, cu);
16276 if (attr != NULL)
16277 {
16278 struct type *underlying_type = die_type (die, cu);
16279
16280 TYPE_TARGET_TYPE (type) = underlying_type;
16281 }
16282
16283 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16284 if (attr != nullptr)
16285 {
16286 TYPE_LENGTH (type) = attr->constant_value (0);
16287 }
16288 else
16289 {
16290 TYPE_LENGTH (type) = 0;
16291 }
16292
16293 maybe_set_alignment (cu, die, type);
16294
16295 /* The enumeration DIE can be incomplete. In Ada, any type can be
16296 declared as private in the package spec, and then defined only
16297 inside the package body. Such types are known as Taft Amendment
16298 Types. When another package uses such a type, an incomplete DIE
16299 may be generated by the compiler. */
16300 if (die_is_declaration (die, cu))
16301 type->set_is_stub (true);
16302
16303 /* If this type has an underlying type that is not a stub, then we
16304 may use its attributes. We always use the "unsigned" attribute
16305 in this situation, because ordinarily we guess whether the type
16306 is unsigned -- but the guess can be wrong and the underlying type
16307 can tell us the reality. However, we defer to a local size
16308 attribute if one exists, because this lets the compiler override
16309 the underlying type if needed. */
16310 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16311 {
16312 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16313 underlying_type = check_typedef (underlying_type);
16314
16315 type->set_is_unsigned (underlying_type->is_unsigned ());
16316
16317 if (TYPE_LENGTH (type) == 0)
16318 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16319
16320 if (TYPE_RAW_ALIGN (type) == 0
16321 && TYPE_RAW_ALIGN (underlying_type) != 0)
16322 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16323 }
16324
16325 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16326
16327 set_die_type (die, type, cu);
16328
16329 /* Finish the creation of this type by using the enum's children.
16330 Note that, as usual, this must come after set_die_type to avoid
16331 infinite recursion when trying to compute the names of the
16332 enumerators. */
16333 update_enumeration_type_from_children (die, type, cu);
16334
16335 return type;
16336 }
16337
16338 /* Given a pointer to a die which begins an enumeration, process all
16339 the dies that define the members of the enumeration, and create the
16340 symbol for the enumeration type.
16341
16342 NOTE: We reverse the order of the element list. */
16343
16344 static void
16345 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16346 {
16347 struct type *this_type;
16348
16349 this_type = get_die_type (die, cu);
16350 if (this_type == NULL)
16351 this_type = read_enumeration_type (die, cu);
16352
16353 if (die->child != NULL)
16354 {
16355 struct die_info *child_die;
16356 const char *name;
16357
16358 child_die = die->child;
16359 while (child_die && child_die->tag)
16360 {
16361 if (child_die->tag != DW_TAG_enumerator)
16362 {
16363 process_die (child_die, cu);
16364 }
16365 else
16366 {
16367 name = dwarf2_name (child_die, cu);
16368 if (name)
16369 new_symbol (child_die, this_type, cu);
16370 }
16371
16372 child_die = child_die->sibling;
16373 }
16374 }
16375
16376 /* If we are reading an enum from a .debug_types unit, and the enum
16377 is a declaration, and the enum is not the signatured type in the
16378 unit, then we do not want to add a symbol for it. Adding a
16379 symbol would in some cases obscure the true definition of the
16380 enum, giving users an incomplete type when the definition is
16381 actually available. Note that we do not want to do this for all
16382 enums which are just declarations, because C++0x allows forward
16383 enum declarations. */
16384 if (cu->per_cu->is_debug_types
16385 && die_is_declaration (die, cu))
16386 {
16387 struct signatured_type *sig_type;
16388
16389 sig_type = (struct signatured_type *) cu->per_cu;
16390 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16391 if (sig_type->type_offset_in_section != die->sect_off)
16392 return;
16393 }
16394
16395 new_symbol (die, this_type, cu);
16396 }
16397
16398 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16399 expression for an index type and finds the corresponding field
16400 offset in the hidden "P_BOUNDS" structure. Returns true on success
16401 and updates *FIELD, false if it fails to recognize an
16402 expression. */
16403
16404 static bool
16405 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16406 int *bounds_offset, struct field *field,
16407 struct dwarf2_cu *cu)
16408 {
16409 struct attribute *attr = dwarf2_attr (die, name, cu);
16410 if (attr == nullptr || !attr->form_is_block ())
16411 return false;
16412
16413 const struct dwarf_block *block = attr->as_block ();
16414 const gdb_byte *start = block->data;
16415 const gdb_byte *end = block->data + block->size;
16416
16417 /* The expression to recognize generally looks like:
16418
16419 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16420 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16421
16422 However, the second "plus_uconst" may be missing:
16423
16424 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16425 DW_OP_deref_size: 4)
16426
16427 This happens when the field is at the start of the structure.
16428
16429 Also, the final deref may not be sized:
16430
16431 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16432 DW_OP_deref)
16433
16434 This happens when the size of the index type happens to be the
16435 same as the architecture's word size. This can occur with or
16436 without the second plus_uconst. */
16437
16438 if (end - start < 2)
16439 return false;
16440 if (*start++ != DW_OP_push_object_address)
16441 return false;
16442 if (*start++ != DW_OP_plus_uconst)
16443 return false;
16444
16445 uint64_t this_bound_off;
16446 start = gdb_read_uleb128 (start, end, &this_bound_off);
16447 if (start == nullptr || (int) this_bound_off != this_bound_off)
16448 return false;
16449 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16450 is consistent among all bounds. */
16451 if (*bounds_offset == -1)
16452 *bounds_offset = this_bound_off;
16453 else if (*bounds_offset != this_bound_off)
16454 return false;
16455
16456 if (start == end || *start++ != DW_OP_deref)
16457 return false;
16458
16459 int offset = 0;
16460 if (start ==end)
16461 return false;
16462 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16463 {
16464 /* This means an offset of 0. */
16465 }
16466 else if (*start++ != DW_OP_plus_uconst)
16467 return false;
16468 else
16469 {
16470 /* The size is the parameter to DW_OP_plus_uconst. */
16471 uint64_t val;
16472 start = gdb_read_uleb128 (start, end, &val);
16473 if (start == nullptr)
16474 return false;
16475 if ((int) val != val)
16476 return false;
16477 offset = val;
16478 }
16479
16480 if (start == end)
16481 return false;
16482
16483 uint64_t size;
16484 if (*start == DW_OP_deref_size)
16485 {
16486 start = gdb_read_uleb128 (start + 1, end, &size);
16487 if (start == nullptr)
16488 return false;
16489 }
16490 else if (*start == DW_OP_deref)
16491 {
16492 size = cu->header.addr_size;
16493 ++start;
16494 }
16495 else
16496 return false;
16497
16498 field->set_loc_bitpos (8 * offset);
16499 if (size != TYPE_LENGTH (field->type ()))
16500 FIELD_BITSIZE (*field) = 8 * size;
16501
16502 return true;
16503 }
16504
16505 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16506 some kinds of Ada arrays:
16507
16508 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16509 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16510 <11e0> DW_AT_data_location: 2 byte block: 97 6
16511 (DW_OP_push_object_address; DW_OP_deref)
16512 <11e3> DW_AT_type : <0x1173>
16513 <11e7> DW_AT_sibling : <0x1201>
16514 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16515 <11ec> DW_AT_type : <0x1206>
16516 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16517 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16518 DW_OP_deref_size: 4)
16519 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16520 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16521 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16522
16523 This actually represents a "thick pointer", which is a structure
16524 with two elements: one that is a pointer to the array data, and one
16525 that is a pointer to another structure; this second structure holds
16526 the array bounds.
16527
16528 This returns a new type on success, or nullptr if this didn't
16529 recognize the type. */
16530
16531 static struct type *
16532 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16533 struct type *type)
16534 {
16535 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16536 /* So far we've only seen this with block form. */
16537 if (attr == nullptr || !attr->form_is_block ())
16538 return nullptr;
16539
16540 /* Note that this will fail if the structure layout is changed by
16541 the compiler. However, we have no good way to recognize some
16542 other layout, because we don't know what expression the compiler
16543 might choose to emit should this happen. */
16544 struct dwarf_block *blk = attr->as_block ();
16545 if (blk->size != 2
16546 || blk->data[0] != DW_OP_push_object_address
16547 || blk->data[1] != DW_OP_deref)
16548 return nullptr;
16549
16550 int bounds_offset = -1;
16551 int max_align = -1;
16552 std::vector<struct field> range_fields;
16553 for (struct die_info *child_die = die->child;
16554 child_die;
16555 child_die = child_die->sibling)
16556 {
16557 if (child_die->tag == DW_TAG_subrange_type)
16558 {
16559 struct type *underlying = read_subrange_index_type (child_die, cu);
16560
16561 int this_align = type_align (underlying);
16562 if (this_align > max_align)
16563 max_align = this_align;
16564
16565 range_fields.emplace_back ();
16566 range_fields.emplace_back ();
16567
16568 struct field &lower = range_fields[range_fields.size () - 2];
16569 struct field &upper = range_fields[range_fields.size () - 1];
16570
16571 lower.set_type (underlying);
16572 FIELD_ARTIFICIAL (lower) = 1;
16573
16574 upper.set_type (underlying);
16575 FIELD_ARTIFICIAL (upper) = 1;
16576
16577 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16578 &bounds_offset, &lower, cu)
16579 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16580 &bounds_offset, &upper, cu))
16581 return nullptr;
16582 }
16583 }
16584
16585 /* This shouldn't really happen, but double-check that we found
16586 where the bounds are stored. */
16587 if (bounds_offset == -1)
16588 return nullptr;
16589
16590 struct objfile *objfile = cu->per_objfile->objfile;
16591 for (int i = 0; i < range_fields.size (); i += 2)
16592 {
16593 char name[20];
16594
16595 /* Set the name of each field in the bounds. */
16596 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16597 range_fields[i].set_name (objfile->intern (name));
16598 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16599 range_fields[i + 1].set_name (objfile->intern (name));
16600 }
16601
16602 struct type *bounds = alloc_type (objfile);
16603 bounds->set_code (TYPE_CODE_STRUCT);
16604
16605 bounds->set_num_fields (range_fields.size ());
16606 bounds->set_fields
16607 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16608 * sizeof (struct field))));
16609 memcpy (bounds->fields (), range_fields.data (),
16610 bounds->num_fields () * sizeof (struct field));
16611
16612 int last_fieldno = range_fields.size () - 1;
16613 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
16614 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16615 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16616
16617 /* Rewrite the existing array type in place. Specifically, we
16618 remove any dynamic properties we might have read, and we replace
16619 the index types. */
16620 struct type *iter = type;
16621 for (int i = 0; i < range_fields.size (); i += 2)
16622 {
16623 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16624 iter->main_type->dyn_prop_list = nullptr;
16625 iter->set_index_type
16626 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16627 iter = TYPE_TARGET_TYPE (iter);
16628 }
16629
16630 struct type *result = alloc_type (objfile);
16631 result->set_code (TYPE_CODE_STRUCT);
16632
16633 result->set_num_fields (2);
16634 result->set_fields
16635 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16636 * sizeof (struct field))));
16637
16638 /* The names are chosen to coincide with what the compiler does with
16639 -fgnat-encodings=all, which the Ada code in gdb already
16640 understands. */
16641 result->field (0).set_name ("P_ARRAY");
16642 result->field (0).set_type (lookup_pointer_type (type));
16643
16644 result->field (1).set_name ("P_BOUNDS");
16645 result->field (1).set_type (lookup_pointer_type (bounds));
16646 result->field (1).set_loc_bitpos (8 * bounds_offset);
16647
16648 result->set_name (type->name ());
16649 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16650 + TYPE_LENGTH (result->field (1).type ()));
16651
16652 return result;
16653 }
16654
16655 /* Extract all information from a DW_TAG_array_type DIE and put it in
16656 the DIE's type field. For now, this only handles one dimensional
16657 arrays. */
16658
16659 static struct type *
16660 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16661 {
16662 struct objfile *objfile = cu->per_objfile->objfile;
16663 struct die_info *child_die;
16664 struct type *type;
16665 struct type *element_type, *range_type, *index_type;
16666 struct attribute *attr;
16667 const char *name;
16668 struct dynamic_prop *byte_stride_prop = NULL;
16669 unsigned int bit_stride = 0;
16670
16671 element_type = die_type (die, cu);
16672
16673 /* The die_type call above may have already set the type for this DIE. */
16674 type = get_die_type (die, cu);
16675 if (type)
16676 return type;
16677
16678 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16679 if (attr != NULL)
16680 {
16681 int stride_ok;
16682 struct type *prop_type = cu->addr_sized_int_type (false);
16683
16684 byte_stride_prop
16685 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16686 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16687 prop_type);
16688 if (!stride_ok)
16689 {
16690 complaint (_("unable to read array DW_AT_byte_stride "
16691 " - DIE at %s [in module %s]"),
16692 sect_offset_str (die->sect_off),
16693 objfile_name (cu->per_objfile->objfile));
16694 /* Ignore this attribute. We will likely not be able to print
16695 arrays of this type correctly, but there is little we can do
16696 to help if we cannot read the attribute's value. */
16697 byte_stride_prop = NULL;
16698 }
16699 }
16700
16701 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16702 if (attr != NULL)
16703 bit_stride = attr->constant_value (0);
16704
16705 /* Irix 6.2 native cc creates array types without children for
16706 arrays with unspecified length. */
16707 if (die->child == NULL)
16708 {
16709 index_type = objfile_type (objfile)->builtin_int;
16710 range_type = create_static_range_type (NULL, index_type, 0, -1);
16711 type = create_array_type_with_stride (NULL, element_type, range_type,
16712 byte_stride_prop, bit_stride);
16713 return set_die_type (die, type, cu);
16714 }
16715
16716 std::vector<struct type *> range_types;
16717 child_die = die->child;
16718 while (child_die && child_die->tag)
16719 {
16720 if (child_die->tag == DW_TAG_subrange_type
16721 || child_die->tag == DW_TAG_generic_subrange)
16722 {
16723 struct type *child_type = read_type_die (child_die, cu);
16724
16725 if (child_type != NULL)
16726 {
16727 /* The range type was succesfully read. Save it for the
16728 array type creation. */
16729 range_types.push_back (child_type);
16730 }
16731 }
16732 child_die = child_die->sibling;
16733 }
16734
16735 if (range_types.empty ())
16736 {
16737 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16738 sect_offset_str (die->sect_off),
16739 objfile_name (cu->per_objfile->objfile));
16740 return NULL;
16741 }
16742
16743 /* Dwarf2 dimensions are output from left to right, create the
16744 necessary array types in backwards order. */
16745
16746 type = element_type;
16747
16748 if (read_array_order (die, cu) == DW_ORD_col_major)
16749 {
16750 int i = 0;
16751
16752 while (i < range_types.size ())
16753 {
16754 type = create_array_type_with_stride (NULL, type, range_types[i++],
16755 byte_stride_prop, bit_stride);
16756 bit_stride = 0;
16757 byte_stride_prop = nullptr;
16758 }
16759 }
16760 else
16761 {
16762 size_t ndim = range_types.size ();
16763 while (ndim-- > 0)
16764 {
16765 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16766 byte_stride_prop, bit_stride);
16767 bit_stride = 0;
16768 byte_stride_prop = nullptr;
16769 }
16770 }
16771
16772 gdb_assert (type != element_type);
16773
16774 /* Understand Dwarf2 support for vector types (like they occur on
16775 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16776 array type. This is not part of the Dwarf2/3 standard yet, but a
16777 custom vendor extension. The main difference between a regular
16778 array and the vector variant is that vectors are passed by value
16779 to functions. */
16780 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16781 if (attr != nullptr)
16782 make_vector_type (type);
16783
16784 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16785 implementation may choose to implement triple vectors using this
16786 attribute. */
16787 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16788 if (attr != nullptr && attr->form_is_unsigned ())
16789 {
16790 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16791 TYPE_LENGTH (type) = attr->as_unsigned ();
16792 else
16793 complaint (_("DW_AT_byte_size for array type smaller "
16794 "than the total size of elements"));
16795 }
16796
16797 name = dwarf2_name (die, cu);
16798 if (name)
16799 type->set_name (name);
16800
16801 maybe_set_alignment (cu, die, type);
16802
16803 struct type *replacement_type = nullptr;
16804 if (cu->per_cu->lang == language_ada)
16805 {
16806 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16807 if (replacement_type != nullptr)
16808 type = replacement_type;
16809 }
16810
16811 /* Install the type in the die. */
16812 set_die_type (die, type, cu, replacement_type != nullptr);
16813
16814 /* set_die_type should be already done. */
16815 set_descriptive_type (type, die, cu);
16816
16817 return type;
16818 }
16819
16820 static enum dwarf_array_dim_ordering
16821 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16822 {
16823 struct attribute *attr;
16824
16825 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16826
16827 if (attr != nullptr)
16828 {
16829 LONGEST val = attr->constant_value (-1);
16830 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16831 return (enum dwarf_array_dim_ordering) val;
16832 }
16833
16834 /* GNU F77 is a special case, as at 08/2004 array type info is the
16835 opposite order to the dwarf2 specification, but data is still
16836 laid out as per normal fortran.
16837
16838 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16839 version checking. */
16840
16841 if (cu->per_cu->lang == language_fortran
16842 && cu->producer && strstr (cu->producer, "GNU F77"))
16843 {
16844 return DW_ORD_row_major;
16845 }
16846
16847 switch (cu->language_defn->array_ordering ())
16848 {
16849 case array_column_major:
16850 return DW_ORD_col_major;
16851 case array_row_major:
16852 default:
16853 return DW_ORD_row_major;
16854 };
16855 }
16856
16857 /* Extract all information from a DW_TAG_set_type DIE and put it in
16858 the DIE's type field. */
16859
16860 static struct type *
16861 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16862 {
16863 struct type *domain_type, *set_type;
16864 struct attribute *attr;
16865
16866 domain_type = die_type (die, cu);
16867
16868 /* The die_type call above may have already set the type for this DIE. */
16869 set_type = get_die_type (die, cu);
16870 if (set_type)
16871 return set_type;
16872
16873 set_type = create_set_type (NULL, domain_type);
16874
16875 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16876 if (attr != nullptr && attr->form_is_unsigned ())
16877 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16878
16879 maybe_set_alignment (cu, die, set_type);
16880
16881 return set_die_type (die, set_type, cu);
16882 }
16883
16884 /* A helper for read_common_block that creates a locexpr baton.
16885 SYM is the symbol which we are marking as computed.
16886 COMMON_DIE is the DIE for the common block.
16887 COMMON_LOC is the location expression attribute for the common
16888 block itself.
16889 MEMBER_LOC is the location expression attribute for the particular
16890 member of the common block that we are processing.
16891 CU is the CU from which the above come. */
16892
16893 static void
16894 mark_common_block_symbol_computed (struct symbol *sym,
16895 struct die_info *common_die,
16896 struct attribute *common_loc,
16897 struct attribute *member_loc,
16898 struct dwarf2_cu *cu)
16899 {
16900 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16901 struct objfile *objfile = per_objfile->objfile;
16902 struct dwarf2_locexpr_baton *baton;
16903 gdb_byte *ptr;
16904 unsigned int cu_off;
16905 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16906 LONGEST offset = 0;
16907
16908 gdb_assert (common_loc && member_loc);
16909 gdb_assert (common_loc->form_is_block ());
16910 gdb_assert (member_loc->form_is_block ()
16911 || member_loc->form_is_constant ());
16912
16913 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16914 baton->per_objfile = per_objfile;
16915 baton->per_cu = cu->per_cu;
16916 gdb_assert (baton->per_cu);
16917
16918 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16919
16920 if (member_loc->form_is_constant ())
16921 {
16922 offset = member_loc->constant_value (0);
16923 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16924 }
16925 else
16926 baton->size += member_loc->as_block ()->size;
16927
16928 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16929 baton->data = ptr;
16930
16931 *ptr++ = DW_OP_call4;
16932 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16933 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16934 ptr += 4;
16935
16936 if (member_loc->form_is_constant ())
16937 {
16938 *ptr++ = DW_OP_addr;
16939 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16940 ptr += cu->header.addr_size;
16941 }
16942 else
16943 {
16944 /* We have to copy the data here, because DW_OP_call4 will only
16945 use a DW_AT_location attribute. */
16946 struct dwarf_block *block = member_loc->as_block ();
16947 memcpy (ptr, block->data, block->size);
16948 ptr += block->size;
16949 }
16950
16951 *ptr++ = DW_OP_plus;
16952 gdb_assert (ptr - baton->data == baton->size);
16953
16954 SYMBOL_LOCATION_BATON (sym) = baton;
16955 sym->set_aclass_index (dwarf2_locexpr_index);
16956 }
16957
16958 /* Create appropriate locally-scoped variables for all the
16959 DW_TAG_common_block entries. Also create a struct common_block
16960 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16961 is used to separate the common blocks name namespace from regular
16962 variable names. */
16963
16964 static void
16965 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16966 {
16967 struct attribute *attr;
16968
16969 attr = dwarf2_attr (die, DW_AT_location, cu);
16970 if (attr != nullptr)
16971 {
16972 /* Support the .debug_loc offsets. */
16973 if (attr->form_is_block ())
16974 {
16975 /* Ok. */
16976 }
16977 else if (attr->form_is_section_offset ())
16978 {
16979 dwarf2_complex_location_expr_complaint ();
16980 attr = NULL;
16981 }
16982 else
16983 {
16984 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16985 "common block member");
16986 attr = NULL;
16987 }
16988 }
16989
16990 if (die->child != NULL)
16991 {
16992 struct objfile *objfile = cu->per_objfile->objfile;
16993 struct die_info *child_die;
16994 size_t n_entries = 0, size;
16995 struct common_block *common_block;
16996 struct symbol *sym;
16997
16998 for (child_die = die->child;
16999 child_die && child_die->tag;
17000 child_die = child_die->sibling)
17001 ++n_entries;
17002
17003 size = (sizeof (struct common_block)
17004 + (n_entries - 1) * sizeof (struct symbol *));
17005 common_block
17006 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17007 size);
17008 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17009 common_block->n_entries = 0;
17010
17011 for (child_die = die->child;
17012 child_die && child_die->tag;
17013 child_die = child_die->sibling)
17014 {
17015 /* Create the symbol in the DW_TAG_common_block block in the current
17016 symbol scope. */
17017 sym = new_symbol (child_die, NULL, cu);
17018 if (sym != NULL)
17019 {
17020 struct attribute *member_loc;
17021
17022 common_block->contents[common_block->n_entries++] = sym;
17023
17024 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17025 cu);
17026 if (member_loc)
17027 {
17028 /* GDB has handled this for a long time, but it is
17029 not specified by DWARF. It seems to have been
17030 emitted by gfortran at least as recently as:
17031 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17032 complaint (_("Variable in common block has "
17033 "DW_AT_data_member_location "
17034 "- DIE at %s [in module %s]"),
17035 sect_offset_str (child_die->sect_off),
17036 objfile_name (objfile));
17037
17038 if (member_loc->form_is_section_offset ())
17039 dwarf2_complex_location_expr_complaint ();
17040 else if (member_loc->form_is_constant ()
17041 || member_loc->form_is_block ())
17042 {
17043 if (attr != nullptr)
17044 mark_common_block_symbol_computed (sym, die, attr,
17045 member_loc, cu);
17046 }
17047 else
17048 dwarf2_complex_location_expr_complaint ();
17049 }
17050 }
17051 }
17052
17053 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17054 sym->set_value_common_block (common_block);
17055 }
17056 }
17057
17058 /* Create a type for a C++ namespace. */
17059
17060 static struct type *
17061 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17062 {
17063 struct objfile *objfile = cu->per_objfile->objfile;
17064 const char *previous_prefix, *name;
17065 int is_anonymous;
17066 struct type *type;
17067
17068 /* For extensions, reuse the type of the original namespace. */
17069 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17070 {
17071 struct die_info *ext_die;
17072 struct dwarf2_cu *ext_cu = cu;
17073
17074 ext_die = dwarf2_extension (die, &ext_cu);
17075 type = read_type_die (ext_die, ext_cu);
17076
17077 /* EXT_CU may not be the same as CU.
17078 Ensure TYPE is recorded with CU in die_type_hash. */
17079 return set_die_type (die, type, cu);
17080 }
17081
17082 name = namespace_name (die, &is_anonymous, cu);
17083
17084 /* Now build the name of the current namespace. */
17085
17086 previous_prefix = determine_prefix (die, cu);
17087 if (previous_prefix[0] != '\0')
17088 name = typename_concat (&objfile->objfile_obstack,
17089 previous_prefix, name, 0, cu);
17090
17091 /* Create the type. */
17092 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17093
17094 return set_die_type (die, type, cu);
17095 }
17096
17097 /* Read a namespace scope. */
17098
17099 static void
17100 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17101 {
17102 struct objfile *objfile = cu->per_objfile->objfile;
17103 int is_anonymous;
17104
17105 /* Add a symbol associated to this if we haven't seen the namespace
17106 before. Also, add a using directive if it's an anonymous
17107 namespace. */
17108
17109 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17110 {
17111 struct type *type;
17112
17113 type = read_type_die (die, cu);
17114 new_symbol (die, type, cu);
17115
17116 namespace_name (die, &is_anonymous, cu);
17117 if (is_anonymous)
17118 {
17119 const char *previous_prefix = determine_prefix (die, cu);
17120
17121 std::vector<const char *> excludes;
17122 add_using_directive (using_directives (cu),
17123 previous_prefix, type->name (), NULL,
17124 NULL, excludes, 0, &objfile->objfile_obstack);
17125 }
17126 }
17127
17128 if (die->child != NULL)
17129 {
17130 struct die_info *child_die = die->child;
17131
17132 while (child_die && child_die->tag)
17133 {
17134 process_die (child_die, cu);
17135 child_die = child_die->sibling;
17136 }
17137 }
17138 }
17139
17140 /* Read a Fortran module as type. This DIE can be only a declaration used for
17141 imported module. Still we need that type as local Fortran "use ... only"
17142 declaration imports depend on the created type in determine_prefix. */
17143
17144 static struct type *
17145 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17146 {
17147 struct objfile *objfile = cu->per_objfile->objfile;
17148 const char *module_name;
17149 struct type *type;
17150
17151 module_name = dwarf2_name (die, cu);
17152 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17153
17154 return set_die_type (die, type, cu);
17155 }
17156
17157 /* Read a Fortran module. */
17158
17159 static void
17160 read_module (struct die_info *die, struct dwarf2_cu *cu)
17161 {
17162 struct die_info *child_die = die->child;
17163 struct type *type;
17164
17165 type = read_type_die (die, cu);
17166 new_symbol (die, type, cu);
17167
17168 while (child_die && child_die->tag)
17169 {
17170 process_die (child_die, cu);
17171 child_die = child_die->sibling;
17172 }
17173 }
17174
17175 /* Return the name of the namespace represented by DIE. Set
17176 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17177 namespace. */
17178
17179 static const char *
17180 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17181 {
17182 struct die_info *current_die;
17183 const char *name = NULL;
17184
17185 /* Loop through the extensions until we find a name. */
17186
17187 for (current_die = die;
17188 current_die != NULL;
17189 current_die = dwarf2_extension (die, &cu))
17190 {
17191 /* We don't use dwarf2_name here so that we can detect the absence
17192 of a name -> anonymous namespace. */
17193 name = dwarf2_string_attr (die, DW_AT_name, cu);
17194
17195 if (name != NULL)
17196 break;
17197 }
17198
17199 /* Is it an anonymous namespace? */
17200
17201 *is_anonymous = (name == NULL);
17202 if (*is_anonymous)
17203 name = CP_ANONYMOUS_NAMESPACE_STR;
17204
17205 return name;
17206 }
17207
17208 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17209 the user defined type vector. */
17210
17211 static struct type *
17212 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17213 {
17214 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17215 struct comp_unit_head *cu_header = &cu->header;
17216 struct type *type;
17217 struct attribute *attr_byte_size;
17218 struct attribute *attr_address_class;
17219 int byte_size, addr_class;
17220 struct type *target_type;
17221
17222 target_type = die_type (die, cu);
17223
17224 /* The die_type call above may have already set the type for this DIE. */
17225 type = get_die_type (die, cu);
17226 if (type)
17227 return type;
17228
17229 type = lookup_pointer_type (target_type);
17230
17231 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17232 if (attr_byte_size)
17233 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17234 else
17235 byte_size = cu_header->addr_size;
17236
17237 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17238 if (attr_address_class)
17239 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17240 else
17241 addr_class = DW_ADDR_none;
17242
17243 ULONGEST alignment = get_alignment (cu, die);
17244
17245 /* If the pointer size, alignment, or address class is different
17246 than the default, create a type variant marked as such and set
17247 the length accordingly. */
17248 if (TYPE_LENGTH (type) != byte_size
17249 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17250 && alignment != TYPE_RAW_ALIGN (type))
17251 || addr_class != DW_ADDR_none)
17252 {
17253 if (gdbarch_address_class_type_flags_p (gdbarch))
17254 {
17255 type_instance_flags type_flags
17256 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17257 addr_class);
17258 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17259 == 0);
17260 type = make_type_with_address_space (type, type_flags);
17261 }
17262 else if (TYPE_LENGTH (type) != byte_size)
17263 {
17264 complaint (_("invalid pointer size %d"), byte_size);
17265 }
17266 else if (TYPE_RAW_ALIGN (type) != alignment)
17267 {
17268 complaint (_("Invalid DW_AT_alignment"
17269 " - DIE at %s [in module %s]"),
17270 sect_offset_str (die->sect_off),
17271 objfile_name (cu->per_objfile->objfile));
17272 }
17273 else
17274 {
17275 /* Should we also complain about unhandled address classes? */
17276 }
17277 }
17278
17279 TYPE_LENGTH (type) = byte_size;
17280 set_type_align (type, alignment);
17281 return set_die_type (die, type, cu);
17282 }
17283
17284 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17285 the user defined type vector. */
17286
17287 static struct type *
17288 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17289 {
17290 struct type *type;
17291 struct type *to_type;
17292 struct type *domain;
17293
17294 to_type = die_type (die, cu);
17295 domain = die_containing_type (die, cu);
17296
17297 /* The calls above may have already set the type for this DIE. */
17298 type = get_die_type (die, cu);
17299 if (type)
17300 return type;
17301
17302 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17303 type = lookup_methodptr_type (to_type);
17304 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17305 {
17306 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17307
17308 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17309 to_type->fields (), to_type->num_fields (),
17310 to_type->has_varargs ());
17311 type = lookup_methodptr_type (new_type);
17312 }
17313 else
17314 type = lookup_memberptr_type (to_type, domain);
17315
17316 return set_die_type (die, type, cu);
17317 }
17318
17319 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17320 the user defined type vector. */
17321
17322 static struct type *
17323 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17324 enum type_code refcode)
17325 {
17326 struct comp_unit_head *cu_header = &cu->header;
17327 struct type *type, *target_type;
17328 struct attribute *attr;
17329
17330 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17331
17332 target_type = die_type (die, cu);
17333
17334 /* The die_type call above may have already set the type for this DIE. */
17335 type = get_die_type (die, cu);
17336 if (type)
17337 return type;
17338
17339 type = lookup_reference_type (target_type, refcode);
17340 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17341 if (attr != nullptr)
17342 {
17343 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17344 }
17345 else
17346 {
17347 TYPE_LENGTH (type) = cu_header->addr_size;
17348 }
17349 maybe_set_alignment (cu, die, type);
17350 return set_die_type (die, type, cu);
17351 }
17352
17353 /* Add the given cv-qualifiers to the element type of the array. GCC
17354 outputs DWARF type qualifiers that apply to an array, not the
17355 element type. But GDB relies on the array element type to carry
17356 the cv-qualifiers. This mimics section 6.7.3 of the C99
17357 specification. */
17358
17359 static struct type *
17360 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17361 struct type *base_type, int cnst, int voltl)
17362 {
17363 struct type *el_type, *inner_array;
17364
17365 base_type = copy_type (base_type);
17366 inner_array = base_type;
17367
17368 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17369 {
17370 TYPE_TARGET_TYPE (inner_array) =
17371 copy_type (TYPE_TARGET_TYPE (inner_array));
17372 inner_array = TYPE_TARGET_TYPE (inner_array);
17373 }
17374
17375 el_type = TYPE_TARGET_TYPE (inner_array);
17376 cnst |= TYPE_CONST (el_type);
17377 voltl |= TYPE_VOLATILE (el_type);
17378 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17379
17380 return set_die_type (die, base_type, cu);
17381 }
17382
17383 static struct type *
17384 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17385 {
17386 struct type *base_type, *cv_type;
17387
17388 base_type = die_type (die, cu);
17389
17390 /* The die_type call above may have already set the type for this DIE. */
17391 cv_type = get_die_type (die, cu);
17392 if (cv_type)
17393 return cv_type;
17394
17395 /* In case the const qualifier is applied to an array type, the element type
17396 is so qualified, not the array type (section 6.7.3 of C99). */
17397 if (base_type->code () == TYPE_CODE_ARRAY)
17398 return add_array_cv_type (die, cu, base_type, 1, 0);
17399
17400 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17401 return set_die_type (die, cv_type, cu);
17402 }
17403
17404 static struct type *
17405 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17406 {
17407 struct type *base_type, *cv_type;
17408
17409 base_type = die_type (die, cu);
17410
17411 /* The die_type call above may have already set the type for this DIE. */
17412 cv_type = get_die_type (die, cu);
17413 if (cv_type)
17414 return cv_type;
17415
17416 /* In case the volatile qualifier is applied to an array type, the
17417 element type is so qualified, not the array type (section 6.7.3
17418 of C99). */
17419 if (base_type->code () == TYPE_CODE_ARRAY)
17420 return add_array_cv_type (die, cu, base_type, 0, 1);
17421
17422 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17423 return set_die_type (die, cv_type, cu);
17424 }
17425
17426 /* Handle DW_TAG_restrict_type. */
17427
17428 static struct type *
17429 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17430 {
17431 struct type *base_type, *cv_type;
17432
17433 base_type = die_type (die, cu);
17434
17435 /* The die_type call above may have already set the type for this DIE. */
17436 cv_type = get_die_type (die, cu);
17437 if (cv_type)
17438 return cv_type;
17439
17440 cv_type = make_restrict_type (base_type);
17441 return set_die_type (die, cv_type, cu);
17442 }
17443
17444 /* Handle DW_TAG_atomic_type. */
17445
17446 static struct type *
17447 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17448 {
17449 struct type *base_type, *cv_type;
17450
17451 base_type = die_type (die, cu);
17452
17453 /* The die_type call above may have already set the type for this DIE. */
17454 cv_type = get_die_type (die, cu);
17455 if (cv_type)
17456 return cv_type;
17457
17458 cv_type = make_atomic_type (base_type);
17459 return set_die_type (die, cv_type, cu);
17460 }
17461
17462 /* Extract all information from a DW_TAG_string_type DIE and add to
17463 the user defined type vector. It isn't really a user defined type,
17464 but it behaves like one, with other DIE's using an AT_user_def_type
17465 attribute to reference it. */
17466
17467 static struct type *
17468 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17469 {
17470 struct objfile *objfile = cu->per_objfile->objfile;
17471 struct gdbarch *gdbarch = objfile->arch ();
17472 struct type *type, *range_type, *index_type, *char_type;
17473 struct attribute *attr;
17474 struct dynamic_prop prop;
17475 bool length_is_constant = true;
17476 LONGEST length;
17477
17478 /* There are a couple of places where bit sizes might be made use of
17479 when parsing a DW_TAG_string_type, however, no producer that we know
17480 of make use of these. Handling bit sizes that are a multiple of the
17481 byte size is easy enough, but what about other bit sizes? Lets deal
17482 with that problem when we have to. Warn about these attributes being
17483 unsupported, then parse the type and ignore them like we always
17484 have. */
17485 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17486 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17487 {
17488 static bool warning_printed = false;
17489 if (!warning_printed)
17490 {
17491 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17492 "currently supported on DW_TAG_string_type."));
17493 warning_printed = true;
17494 }
17495 }
17496
17497 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17498 if (attr != nullptr && !attr->form_is_constant ())
17499 {
17500 /* The string length describes the location at which the length of
17501 the string can be found. The size of the length field can be
17502 specified with one of the attributes below. */
17503 struct type *prop_type;
17504 struct attribute *len
17505 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17506 if (len == nullptr)
17507 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17508 if (len != nullptr && len->form_is_constant ())
17509 {
17510 /* Pass 0 as the default as we know this attribute is constant
17511 and the default value will not be returned. */
17512 LONGEST sz = len->constant_value (0);
17513 prop_type = objfile_int_type (objfile, sz, true);
17514 }
17515 else
17516 {
17517 /* If the size is not specified then we assume it is the size of
17518 an address on this target. */
17519 prop_type = cu->addr_sized_int_type (true);
17520 }
17521
17522 /* Convert the attribute into a dynamic property. */
17523 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17524 length = 1;
17525 else
17526 length_is_constant = false;
17527 }
17528 else if (attr != nullptr)
17529 {
17530 /* This DW_AT_string_length just contains the length with no
17531 indirection. There's no need to create a dynamic property in this
17532 case. Pass 0 for the default value as we know it will not be
17533 returned in this case. */
17534 length = attr->constant_value (0);
17535 }
17536 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17537 {
17538 /* We don't currently support non-constant byte sizes for strings. */
17539 length = attr->constant_value (1);
17540 }
17541 else
17542 {
17543 /* Use 1 as a fallback length if we have nothing else. */
17544 length = 1;
17545 }
17546
17547 index_type = objfile_type (objfile)->builtin_int;
17548 if (length_is_constant)
17549 range_type = create_static_range_type (NULL, index_type, 1, length);
17550 else
17551 {
17552 struct dynamic_prop low_bound;
17553
17554 low_bound.set_const_val (1);
17555 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17556 }
17557 char_type = language_string_char_type (cu->language_defn, gdbarch);
17558 type = create_string_type (NULL, char_type, range_type);
17559
17560 return set_die_type (die, type, cu);
17561 }
17562
17563 /* Assuming that DIE corresponds to a function, returns nonzero
17564 if the function is prototyped. */
17565
17566 static int
17567 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17568 {
17569 struct attribute *attr;
17570
17571 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17572 if (attr && attr->as_boolean ())
17573 return 1;
17574
17575 /* The DWARF standard implies that the DW_AT_prototyped attribute
17576 is only meaningful for C, but the concept also extends to other
17577 languages that allow unprototyped functions (Eg: Objective C).
17578 For all other languages, assume that functions are always
17579 prototyped. */
17580 if (cu->per_cu->lang != language_c
17581 && cu->per_cu->lang != language_objc
17582 && cu->per_cu->lang != language_opencl)
17583 return 1;
17584
17585 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17586 prototyped and unprototyped functions; default to prototyped,
17587 since that is more common in modern code (and RealView warns
17588 about unprototyped functions). */
17589 if (producer_is_realview (cu->producer))
17590 return 1;
17591
17592 return 0;
17593 }
17594
17595 /* Handle DIES due to C code like:
17596
17597 struct foo
17598 {
17599 int (*funcp)(int a, long l);
17600 int b;
17601 };
17602
17603 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17604
17605 static struct type *
17606 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17607 {
17608 struct objfile *objfile = cu->per_objfile->objfile;
17609 struct type *type; /* Type that this function returns. */
17610 struct type *ftype; /* Function that returns above type. */
17611 struct attribute *attr;
17612
17613 type = die_type (die, cu);
17614
17615 /* The die_type call above may have already set the type for this DIE. */
17616 ftype = get_die_type (die, cu);
17617 if (ftype)
17618 return ftype;
17619
17620 ftype = lookup_function_type (type);
17621
17622 if (prototyped_function_p (die, cu))
17623 ftype->set_is_prototyped (true);
17624
17625 /* Store the calling convention in the type if it's available in
17626 the subroutine die. Otherwise set the calling convention to
17627 the default value DW_CC_normal. */
17628 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17629 if (attr != nullptr
17630 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17631 TYPE_CALLING_CONVENTION (ftype)
17632 = (enum dwarf_calling_convention) attr->constant_value (0);
17633 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17634 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17635 else
17636 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17637
17638 /* Record whether the function returns normally to its caller or not
17639 if the DWARF producer set that information. */
17640 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17641 if (attr && attr->as_boolean ())
17642 TYPE_NO_RETURN (ftype) = 1;
17643
17644 /* We need to add the subroutine type to the die immediately so
17645 we don't infinitely recurse when dealing with parameters
17646 declared as the same subroutine type. */
17647 set_die_type (die, ftype, cu);
17648
17649 if (die->child != NULL)
17650 {
17651 struct type *void_type = objfile_type (objfile)->builtin_void;
17652 struct die_info *child_die;
17653 int nparams, iparams;
17654
17655 /* Count the number of parameters.
17656 FIXME: GDB currently ignores vararg functions, but knows about
17657 vararg member functions. */
17658 nparams = 0;
17659 child_die = die->child;
17660 while (child_die && child_die->tag)
17661 {
17662 if (child_die->tag == DW_TAG_formal_parameter)
17663 nparams++;
17664 else if (child_die->tag == DW_TAG_unspecified_parameters)
17665 ftype->set_has_varargs (true);
17666
17667 child_die = child_die->sibling;
17668 }
17669
17670 /* Allocate storage for parameters and fill them in. */
17671 ftype->set_num_fields (nparams);
17672 ftype->set_fields
17673 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17674
17675 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17676 even if we error out during the parameters reading below. */
17677 for (iparams = 0; iparams < nparams; iparams++)
17678 ftype->field (iparams).set_type (void_type);
17679
17680 iparams = 0;
17681 child_die = die->child;
17682 while (child_die && child_die->tag)
17683 {
17684 if (child_die->tag == DW_TAG_formal_parameter)
17685 {
17686 struct type *arg_type;
17687
17688 /* DWARF version 2 has no clean way to discern C++
17689 static and non-static member functions. G++ helps
17690 GDB by marking the first parameter for non-static
17691 member functions (which is the this pointer) as
17692 artificial. We pass this information to
17693 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17694
17695 DWARF version 3 added DW_AT_object_pointer, which GCC
17696 4.5 does not yet generate. */
17697 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17698 if (attr != nullptr)
17699 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17700 else
17701 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17702 arg_type = die_type (child_die, cu);
17703
17704 /* RealView does not mark THIS as const, which the testsuite
17705 expects. GCC marks THIS as const in method definitions,
17706 but not in the class specifications (GCC PR 43053). */
17707 if (cu->per_cu->lang == language_cplus
17708 && !TYPE_CONST (arg_type)
17709 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17710 {
17711 int is_this = 0;
17712 struct dwarf2_cu *arg_cu = cu;
17713 const char *name = dwarf2_name (child_die, cu);
17714
17715 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17716 if (attr != nullptr)
17717 {
17718 /* If the compiler emits this, use it. */
17719 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17720 is_this = 1;
17721 }
17722 else if (name && strcmp (name, "this") == 0)
17723 /* Function definitions will have the argument names. */
17724 is_this = 1;
17725 else if (name == NULL && iparams == 0)
17726 /* Declarations may not have the names, so like
17727 elsewhere in GDB, assume an artificial first
17728 argument is "this". */
17729 is_this = 1;
17730
17731 if (is_this)
17732 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17733 arg_type, 0);
17734 }
17735
17736 ftype->field (iparams).set_type (arg_type);
17737 iparams++;
17738 }
17739 child_die = child_die->sibling;
17740 }
17741 }
17742
17743 return ftype;
17744 }
17745
17746 static struct type *
17747 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17748 {
17749 struct objfile *objfile = cu->per_objfile->objfile;
17750 const char *name = NULL;
17751 struct type *this_type, *target_type;
17752
17753 name = dwarf2_full_name (NULL, die, cu);
17754 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17755 this_type->set_target_is_stub (true);
17756 set_die_type (die, this_type, cu);
17757 target_type = die_type (die, cu);
17758 if (target_type != this_type)
17759 TYPE_TARGET_TYPE (this_type) = target_type;
17760 else
17761 {
17762 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17763 spec and cause infinite loops in GDB. */
17764 complaint (_("Self-referential DW_TAG_typedef "
17765 "- DIE at %s [in module %s]"),
17766 sect_offset_str (die->sect_off), objfile_name (objfile));
17767 TYPE_TARGET_TYPE (this_type) = NULL;
17768 }
17769 if (name == NULL)
17770 {
17771 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17772 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17773 Handle these by just returning the target type, rather than
17774 constructing an anonymous typedef type and trying to handle this
17775 elsewhere. */
17776 set_die_type (die, target_type, cu);
17777 return target_type;
17778 }
17779 return this_type;
17780 }
17781
17782 /* Helper for get_dwarf2_rational_constant that computes the value of
17783 a given gmp_mpz given an attribute. */
17784
17785 static void
17786 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17787 {
17788 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17789 location expression that pushes an implicit value. */
17790 if (attr->form == DW_FORM_exprloc)
17791 {
17792 dwarf_block *blk = attr->as_block ();
17793 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17794 {
17795 uint64_t len;
17796 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17797 blk->data + blk->size,
17798 &len);
17799 if (ptr - blk->data + len <= blk->size)
17800 {
17801 mpz_import (value->val, len,
17802 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17803 1, 0, 0, ptr);
17804 return;
17805 }
17806 }
17807
17808 /* On failure set it to 1. */
17809 *value = gdb_mpz (1);
17810 }
17811 else if (attr->form_is_block ())
17812 {
17813 dwarf_block *blk = attr->as_block ();
17814 mpz_import (value->val, blk->size,
17815 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17816 1, 0, 0, blk->data);
17817 }
17818 else
17819 *value = gdb_mpz (attr->constant_value (1));
17820 }
17821
17822 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17823 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17824
17825 If the numerator and/or numerator attribute is missing,
17826 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17827 untouched. */
17828
17829 static void
17830 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17831 gdb_mpz *numerator, gdb_mpz *denominator)
17832 {
17833 struct attribute *num_attr, *denom_attr;
17834
17835 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17836 if (num_attr == nullptr)
17837 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17838 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17839
17840 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17841 if (denom_attr == nullptr)
17842 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17843 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17844
17845 if (num_attr == nullptr || denom_attr == nullptr)
17846 return;
17847
17848 get_mpz (cu, numerator, num_attr);
17849 get_mpz (cu, denominator, denom_attr);
17850 }
17851
17852 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17853 rational constant, rather than a signed one.
17854
17855 If the rational constant has a negative value, a complaint
17856 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17857
17858 static void
17859 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17860 struct dwarf2_cu *cu,
17861 gdb_mpz *numerator,
17862 gdb_mpz *denominator)
17863 {
17864 gdb_mpz num (1);
17865 gdb_mpz denom (1);
17866
17867 get_dwarf2_rational_constant (die, cu, &num, &denom);
17868 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17869 {
17870 mpz_neg (num.val, num.val);
17871 mpz_neg (denom.val, denom.val);
17872 }
17873 else if (mpz_sgn (num.val) == -1)
17874 {
17875 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17876 " in DIE at %s"),
17877 sect_offset_str (die->sect_off));
17878 return;
17879 }
17880 else if (mpz_sgn (denom.val) == -1)
17881 {
17882 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17883 " in DIE at %s"),
17884 sect_offset_str (die->sect_off));
17885 return;
17886 }
17887
17888 *numerator = std::move (num);
17889 *denominator = std::move (denom);
17890 }
17891
17892 /* Assuming that ENCODING is a string whose contents starting at the
17893 K'th character is "_nn" where "nn" is a decimal number, scan that
17894 number and set RESULT to the value. K is updated to point to the
17895 character immediately following the number.
17896
17897 If the string does not conform to the format described above, false
17898 is returned, and K may or may not be changed. */
17899
17900 static bool
17901 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17902 {
17903 /* The next character should be an underscore ('_') followed
17904 by a digit. */
17905 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17906 return false;
17907
17908 /* Skip the underscore. */
17909 k++;
17910 int start = k;
17911
17912 /* Determine the number of digits for our number. */
17913 while (isdigit (encoding[k]))
17914 k++;
17915 if (k == start)
17916 return false;
17917
17918 std::string copy (&encoding[start], k - start);
17919 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17920 return false;
17921
17922 return true;
17923 }
17924
17925 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17926 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17927 DENOM, update OFFSET, and return true on success. Return false on
17928 failure. */
17929
17930 static bool
17931 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17932 gdb_mpz *num, gdb_mpz *denom)
17933 {
17934 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17935 return false;
17936 return ada_get_gnat_encoded_number (encoding, offset, denom);
17937 }
17938
17939 /* Assuming DIE corresponds to a fixed point type, finish the creation
17940 of the corresponding TYPE by setting its type-specific data. CU is
17941 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17942 encodings. It is nullptr if the GNAT encoding should be
17943 ignored. */
17944
17945 static void
17946 finish_fixed_point_type (struct type *type, const char *suffix,
17947 struct die_info *die, struct dwarf2_cu *cu)
17948 {
17949 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17950 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17951
17952 /* If GNAT encodings are preferred, don't examine the
17953 attributes. */
17954 struct attribute *attr = nullptr;
17955 if (suffix == nullptr)
17956 {
17957 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17958 if (attr == nullptr)
17959 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17960 if (attr == nullptr)
17961 attr = dwarf2_attr (die, DW_AT_small, cu);
17962 }
17963
17964 /* Numerator and denominator of our fixed-point type's scaling factor.
17965 The default is a scaling factor of 1, which we use as a fallback
17966 when we are not able to decode it (problem with the debugging info,
17967 unsupported forms, bug in GDB, etc...). Using that as the default
17968 allows us to at least print the unscaled value, which might still
17969 be useful to a user. */
17970 gdb_mpz scale_num (1);
17971 gdb_mpz scale_denom (1);
17972
17973 if (attr == nullptr)
17974 {
17975 int offset = 0;
17976 if (suffix != nullptr
17977 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17978 &scale_denom)
17979 /* The number might be encoded as _nn_dd_nn_dd, where the
17980 second ratio is the 'small value. In this situation, we
17981 want the second value. */
17982 && (suffix[offset] != '_'
17983 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17984 &scale_denom)))
17985 {
17986 /* Found it. */
17987 }
17988 else
17989 {
17990 /* Scaling factor not found. Assume a scaling factor of 1,
17991 and hope for the best. At least the user will be able to
17992 see the encoded value. */
17993 scale_num = 1;
17994 scale_denom = 1;
17995 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17996 sect_offset_str (die->sect_off));
17997 }
17998 }
17999 else if (attr->name == DW_AT_binary_scale)
18000 {
18001 LONGEST scale_exp = attr->constant_value (0);
18002 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18003
18004 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18005 }
18006 else if (attr->name == DW_AT_decimal_scale)
18007 {
18008 LONGEST scale_exp = attr->constant_value (0);
18009 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18010
18011 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18012 }
18013 else if (attr->name == DW_AT_small)
18014 {
18015 struct die_info *scale_die;
18016 struct dwarf2_cu *scale_cu = cu;
18017
18018 scale_die = follow_die_ref (die, attr, &scale_cu);
18019 if (scale_die->tag == DW_TAG_constant)
18020 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18021 &scale_num, &scale_denom);
18022 else
18023 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18024 " (DIE at %s)"),
18025 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18026 }
18027 else
18028 {
18029 complaint (_("unsupported scale attribute %s for fixed-point type"
18030 " (DIE at %s)"),
18031 dwarf_attr_name (attr->name),
18032 sect_offset_str (die->sect_off));
18033 }
18034
18035 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18036 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18037 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18038 mpq_canonicalize (scaling_factor.val);
18039 }
18040
18041 /* The gnat-encoding suffix for fixed point. */
18042
18043 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18044
18045 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18046 "XF" suffix of the name. The text after this is what encodes the
18047 'small and 'delta information. Otherwise, return nullptr. */
18048
18049 static const char *
18050 gnat_encoded_fixed_point_type_info (const char *name)
18051 {
18052 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18053 }
18054
18055 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18056 (which may be different from NAME) to the architecture back-end to allow
18057 it to guess the correct format if necessary. */
18058
18059 static struct type *
18060 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18061 const char *name_hint, enum bfd_endian byte_order)
18062 {
18063 struct gdbarch *gdbarch = objfile->arch ();
18064 const struct floatformat **format;
18065 struct type *type;
18066
18067 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18068 if (format)
18069 type = init_float_type (objfile, bits, name, format, byte_order);
18070 else
18071 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18072
18073 return type;
18074 }
18075
18076 /* Allocate an integer type of size BITS and name NAME. */
18077
18078 static struct type *
18079 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18080 int bits, int unsigned_p, const char *name)
18081 {
18082 struct type *type;
18083
18084 /* Versions of Intel's C Compiler generate an integer type called "void"
18085 instead of using DW_TAG_unspecified_type. This has been seen on
18086 at least versions 14, 17, and 18. */
18087 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18088 && strcmp (name, "void") == 0)
18089 type = objfile_type (objfile)->builtin_void;
18090 else
18091 type = init_integer_type (objfile, bits, unsigned_p, name);
18092
18093 return type;
18094 }
18095
18096 /* Return true if DIE has a DW_AT_small attribute whose value is
18097 a constant rational, where both the numerator and denominator
18098 are equal to zero.
18099
18100 CU is the DIE's Compilation Unit. */
18101
18102 static bool
18103 has_zero_over_zero_small_attribute (struct die_info *die,
18104 struct dwarf2_cu *cu)
18105 {
18106 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18107 if (attr == nullptr)
18108 return false;
18109
18110 struct dwarf2_cu *scale_cu = cu;
18111 struct die_info *scale_die
18112 = follow_die_ref (die, attr, &scale_cu);
18113
18114 if (scale_die->tag != DW_TAG_constant)
18115 return false;
18116
18117 gdb_mpz num (1), denom (1);
18118 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18119 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18120 }
18121
18122 /* Initialise and return a floating point type of size BITS suitable for
18123 use as a component of a complex number. The NAME_HINT is passed through
18124 when initialising the floating point type and is the name of the complex
18125 type.
18126
18127 As DWARF doesn't currently provide an explicit name for the components
18128 of a complex number, but it can be helpful to have these components
18129 named, we try to select a suitable name based on the size of the
18130 component. */
18131 static struct type *
18132 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18133 struct objfile *objfile,
18134 int bits, const char *name_hint,
18135 enum bfd_endian byte_order)
18136 {
18137 gdbarch *gdbarch = objfile->arch ();
18138 struct type *tt = nullptr;
18139
18140 /* Try to find a suitable floating point builtin type of size BITS.
18141 We're going to use the name of this type as the name for the complex
18142 target type that we are about to create. */
18143 switch (cu->per_cu->lang)
18144 {
18145 case language_fortran:
18146 switch (bits)
18147 {
18148 case 32:
18149 tt = builtin_f_type (gdbarch)->builtin_real;
18150 break;
18151 case 64:
18152 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18153 break;
18154 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18155 case 128:
18156 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18157 break;
18158 }
18159 break;
18160 default:
18161 switch (bits)
18162 {
18163 case 32:
18164 tt = builtin_type (gdbarch)->builtin_float;
18165 break;
18166 case 64:
18167 tt = builtin_type (gdbarch)->builtin_double;
18168 break;
18169 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18170 case 128:
18171 tt = builtin_type (gdbarch)->builtin_long_double;
18172 break;
18173 }
18174 break;
18175 }
18176
18177 /* If the type we found doesn't match the size we were looking for, then
18178 pretend we didn't find a type at all, the complex target type we
18179 create will then be nameless. */
18180 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18181 tt = nullptr;
18182
18183 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18184 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18185 }
18186
18187 /* Find a representation of a given base type and install
18188 it in the TYPE field of the die. */
18189
18190 static struct type *
18191 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18192 {
18193 struct objfile *objfile = cu->per_objfile->objfile;
18194 struct type *type;
18195 struct attribute *attr;
18196 int encoding = 0, bits = 0;
18197 const char *name;
18198 gdbarch *arch;
18199
18200 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18201 if (attr != nullptr && attr->form_is_constant ())
18202 encoding = attr->constant_value (0);
18203 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18204 if (attr != nullptr)
18205 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18206 name = dwarf2_name (die, cu);
18207 if (!name)
18208 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18209
18210 arch = objfile->arch ();
18211 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18212
18213 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18214 if (attr != nullptr && attr->form_is_constant ())
18215 {
18216 int endianity = attr->constant_value (0);
18217
18218 switch (endianity)
18219 {
18220 case DW_END_big:
18221 byte_order = BFD_ENDIAN_BIG;
18222 break;
18223 case DW_END_little:
18224 byte_order = BFD_ENDIAN_LITTLE;
18225 break;
18226 default:
18227 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18228 break;
18229 }
18230 }
18231
18232 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18233 && cu->per_cu->lang == language_ada
18234 && has_zero_over_zero_small_attribute (die, cu))
18235 {
18236 /* brobecker/2018-02-24: This is a fixed point type for which
18237 the scaling factor is represented as fraction whose value
18238 does not make sense (zero divided by zero), so we should
18239 normally never see these. However, there is a small category
18240 of fixed point types for which GNAT is unable to provide
18241 the scaling factor via the standard DWARF mechanisms, and
18242 for which the info is provided via the GNAT encodings instead.
18243 This is likely what this DIE is about. */
18244 encoding = (encoding == DW_ATE_signed_fixed
18245 ? DW_ATE_signed
18246 : DW_ATE_unsigned);
18247 }
18248
18249 /* With GNAT encodings, fixed-point information will be encoded in
18250 the type name. Note that this can also occur with the above
18251 zero-over-zero case, which is why this is a separate "if" rather
18252 than an "else if". */
18253 const char *gnat_encoding_suffix = nullptr;
18254 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18255 && cu->per_cu->lang == language_ada
18256 && name != nullptr)
18257 {
18258 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18259 if (gnat_encoding_suffix != nullptr)
18260 {
18261 gdb_assert (startswith (gnat_encoding_suffix,
18262 GNAT_FIXED_POINT_SUFFIX));
18263 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18264 name, gnat_encoding_suffix - name);
18265 /* Use -1 here so that SUFFIX points at the "_" after the
18266 "XF". */
18267 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18268
18269 encoding = (encoding == DW_ATE_signed
18270 ? DW_ATE_signed_fixed
18271 : DW_ATE_unsigned_fixed);
18272 }
18273 }
18274
18275 switch (encoding)
18276 {
18277 case DW_ATE_address:
18278 /* Turn DW_ATE_address into a void * pointer. */
18279 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18280 type = init_pointer_type (objfile, bits, name, type);
18281 break;
18282 case DW_ATE_boolean:
18283 type = init_boolean_type (objfile, bits, 1, name);
18284 break;
18285 case DW_ATE_complex_float:
18286 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18287 byte_order);
18288 if (type->code () == TYPE_CODE_ERROR)
18289 {
18290 if (name == nullptr)
18291 {
18292 struct obstack *obstack
18293 = &cu->per_objfile->objfile->objfile_obstack;
18294 name = obconcat (obstack, "_Complex ", type->name (),
18295 nullptr);
18296 }
18297 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18298 }
18299 else
18300 type = init_complex_type (name, type);
18301 break;
18302 case DW_ATE_decimal_float:
18303 type = init_decfloat_type (objfile, bits, name);
18304 break;
18305 case DW_ATE_float:
18306 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18307 break;
18308 case DW_ATE_signed:
18309 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18310 break;
18311 case DW_ATE_unsigned:
18312 if (cu->per_cu->lang == language_fortran
18313 && name
18314 && startswith (name, "character("))
18315 type = init_character_type (objfile, bits, 1, name);
18316 else
18317 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18318 break;
18319 case DW_ATE_signed_char:
18320 if (cu->per_cu->lang == language_ada
18321 || cu->per_cu->lang == language_m2
18322 || cu->per_cu->lang == language_pascal
18323 || cu->per_cu->lang == language_fortran)
18324 type = init_character_type (objfile, bits, 0, name);
18325 else
18326 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18327 break;
18328 case DW_ATE_unsigned_char:
18329 if (cu->per_cu->lang == language_ada
18330 || cu->per_cu->lang == language_m2
18331 || cu->per_cu->lang == language_pascal
18332 || cu->per_cu->lang == language_fortran
18333 || cu->per_cu->lang == language_rust)
18334 type = init_character_type (objfile, bits, 1, name);
18335 else
18336 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18337 break;
18338 case DW_ATE_UTF:
18339 {
18340 type = init_character_type (objfile, bits, 1, name);
18341 return set_die_type (die, type, cu);
18342 }
18343 break;
18344 case DW_ATE_signed_fixed:
18345 type = init_fixed_point_type (objfile, bits, 0, name);
18346 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18347 break;
18348 case DW_ATE_unsigned_fixed:
18349 type = init_fixed_point_type (objfile, bits, 1, name);
18350 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18351 break;
18352
18353 default:
18354 complaint (_("unsupported DW_AT_encoding: '%s'"),
18355 dwarf_type_encoding_name (encoding));
18356 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18357 break;
18358 }
18359
18360 if (type->code () == TYPE_CODE_INT
18361 && name != nullptr
18362 && strcmp (name, "char") == 0)
18363 type->set_has_no_signedness (true);
18364
18365 maybe_set_alignment (cu, die, type);
18366
18367 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18368
18369 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18370 {
18371 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18372 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18373 {
18374 unsigned real_bit_size = attr->as_unsigned ();
18375 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18376 /* Only use the attributes if they make sense together. */
18377 if (attr == nullptr
18378 || (attr->as_unsigned () + real_bit_size
18379 <= 8 * TYPE_LENGTH (type)))
18380 {
18381 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18382 = real_bit_size;
18383 if (attr != nullptr)
18384 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18385 = attr->as_unsigned ();
18386 }
18387 }
18388 }
18389
18390 return set_die_type (die, type, cu);
18391 }
18392
18393 /* A helper function that returns the name of DIE, if it refers to a
18394 variable declaration. */
18395
18396 static const char *
18397 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18398 {
18399 if (die->tag != DW_TAG_variable)
18400 return nullptr;
18401
18402 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18403 if (attr == nullptr || !attr->as_boolean ())
18404 return nullptr;
18405
18406 attr = dwarf2_attr (die, DW_AT_name, cu);
18407 if (attr == nullptr)
18408 return nullptr;
18409 return attr->as_string ();
18410 }
18411
18412 /* Parse dwarf attribute if it's a block, reference or constant and put the
18413 resulting value of the attribute into struct bound_prop.
18414 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18415
18416 static int
18417 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18418 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18419 struct type *default_type)
18420 {
18421 struct dwarf2_property_baton *baton;
18422 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18423 struct objfile *objfile = per_objfile->objfile;
18424 struct obstack *obstack = &objfile->objfile_obstack;
18425
18426 gdb_assert (default_type != NULL);
18427
18428 if (attr == NULL || prop == NULL)
18429 return 0;
18430
18431 if (attr->form_is_block ())
18432 {
18433 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18434 baton->property_type = default_type;
18435 baton->locexpr.per_cu = cu->per_cu;
18436 baton->locexpr.per_objfile = per_objfile;
18437
18438 struct dwarf_block *block;
18439 if (attr->form == DW_FORM_data16)
18440 {
18441 size_t data_size = 16;
18442 block = XOBNEW (obstack, struct dwarf_block);
18443 block->size = (data_size
18444 + 2 /* Extra bytes for DW_OP and arg. */);
18445 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18446 data[0] = DW_OP_implicit_value;
18447 data[1] = data_size;
18448 memcpy (&data[2], attr->as_block ()->data, data_size);
18449 block->data = data;
18450 }
18451 else
18452 block = attr->as_block ();
18453
18454 baton->locexpr.size = block->size;
18455 baton->locexpr.data = block->data;
18456 switch (attr->name)
18457 {
18458 case DW_AT_string_length:
18459 baton->locexpr.is_reference = true;
18460 break;
18461 default:
18462 baton->locexpr.is_reference = false;
18463 break;
18464 }
18465
18466 prop->set_locexpr (baton);
18467 gdb_assert (prop->baton () != NULL);
18468 }
18469 else if (attr->form_is_ref ())
18470 {
18471 struct dwarf2_cu *target_cu = cu;
18472 struct die_info *target_die;
18473 struct attribute *target_attr;
18474
18475 target_die = follow_die_ref (die, attr, &target_cu);
18476 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18477 if (target_attr == NULL)
18478 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18479 target_cu);
18480 if (target_attr == nullptr)
18481 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
18482 target_cu);
18483 if (target_attr == NULL)
18484 {
18485 const char *name = var_decl_name (target_die, target_cu);
18486 if (name != nullptr)
18487 {
18488 prop->set_variable_name (name);
18489 return 1;
18490 }
18491 return 0;
18492 }
18493
18494 switch (target_attr->name)
18495 {
18496 case DW_AT_location:
18497 if (target_attr->form_is_section_offset ())
18498 {
18499 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18500 baton->property_type = die_type (target_die, target_cu);
18501 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18502 prop->set_loclist (baton);
18503 gdb_assert (prop->baton () != NULL);
18504 }
18505 else if (target_attr->form_is_block ())
18506 {
18507 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18508 baton->property_type = die_type (target_die, target_cu);
18509 baton->locexpr.per_cu = cu->per_cu;
18510 baton->locexpr.per_objfile = per_objfile;
18511 struct dwarf_block *block = target_attr->as_block ();
18512 baton->locexpr.size = block->size;
18513 baton->locexpr.data = block->data;
18514 baton->locexpr.is_reference = true;
18515 prop->set_locexpr (baton);
18516 gdb_assert (prop->baton () != NULL);
18517 }
18518 else
18519 {
18520 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18521 "dynamic property");
18522 return 0;
18523 }
18524 break;
18525 case DW_AT_data_member_location:
18526 case DW_AT_data_bit_offset:
18527 {
18528 LONGEST offset;
18529
18530 if (!handle_member_location (target_die, target_cu, &offset))
18531 return 0;
18532
18533 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18534 baton->property_type = read_type_die (target_die->parent,
18535 target_cu);
18536 baton->offset_info.offset = offset;
18537 baton->offset_info.type = die_type (target_die, target_cu);
18538 prop->set_addr_offset (baton);
18539 break;
18540 }
18541 }
18542 }
18543 else if (attr->form_is_constant ())
18544 prop->set_const_val (attr->constant_value (0));
18545 else if (attr->form_is_section_offset ())
18546 {
18547 switch (attr->name)
18548 {
18549 case DW_AT_string_length:
18550 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18551 baton->property_type = default_type;
18552 fill_in_loclist_baton (cu, &baton->loclist, attr);
18553 prop->set_loclist (baton);
18554 gdb_assert (prop->baton () != NULL);
18555 break;
18556 default:
18557 goto invalid;
18558 }
18559 }
18560 else
18561 goto invalid;
18562
18563 return 1;
18564
18565 invalid:
18566 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18567 dwarf2_name (die, cu));
18568 return 0;
18569 }
18570
18571 /* See read.h. */
18572
18573 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18574 present (which is valid) then compute the default type based on the
18575 compilation units address size. */
18576
18577 static struct type *
18578 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18579 {
18580 struct type *index_type = die_type (die, cu);
18581
18582 /* Dwarf-2 specifications explicitly allows to create subrange types
18583 without specifying a base type.
18584 In that case, the base type must be set to the type of
18585 the lower bound, upper bound or count, in that order, if any of these
18586 three attributes references an object that has a type.
18587 If no base type is found, the Dwarf-2 specifications say that
18588 a signed integer type of size equal to the size of an address should
18589 be used.
18590 For the following C code: `extern char gdb_int [];'
18591 GCC produces an empty range DIE.
18592 FIXME: muller/2010-05-28: Possible references to object for low bound,
18593 high bound or count are not yet handled by this code. */
18594 if (index_type->code () == TYPE_CODE_VOID)
18595 index_type = cu->addr_sized_int_type (false);
18596
18597 return index_type;
18598 }
18599
18600 /* Read the given DW_AT_subrange DIE. */
18601
18602 static struct type *
18603 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18604 {
18605 struct type *base_type, *orig_base_type;
18606 struct type *range_type;
18607 struct attribute *attr;
18608 struct dynamic_prop low, high;
18609 int low_default_is_valid;
18610 int high_bound_is_count = 0;
18611 const char *name;
18612 ULONGEST negative_mask;
18613
18614 orig_base_type = read_subrange_index_type (die, cu);
18615
18616 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18617 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18618 creating the range type, but we use the result of check_typedef
18619 when examining properties of the type. */
18620 base_type = check_typedef (orig_base_type);
18621
18622 /* The die_type call above may have already set the type for this DIE. */
18623 range_type = get_die_type (die, cu);
18624 if (range_type)
18625 return range_type;
18626
18627 high.set_const_val (0);
18628
18629 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18630 omitting DW_AT_lower_bound. */
18631 switch (cu->per_cu->lang)
18632 {
18633 case language_c:
18634 case language_cplus:
18635 low.set_const_val (0);
18636 low_default_is_valid = 1;
18637 break;
18638 case language_fortran:
18639 low.set_const_val (1);
18640 low_default_is_valid = 1;
18641 break;
18642 case language_d:
18643 case language_objc:
18644 case language_rust:
18645 low.set_const_val (0);
18646 low_default_is_valid = (cu->header.version >= 4);
18647 break;
18648 case language_ada:
18649 case language_m2:
18650 case language_pascal:
18651 low.set_const_val (1);
18652 low_default_is_valid = (cu->header.version >= 4);
18653 break;
18654 default:
18655 low.set_const_val (0);
18656 low_default_is_valid = 0;
18657 break;
18658 }
18659
18660 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18661 if (attr != nullptr)
18662 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18663 else if (!low_default_is_valid)
18664 complaint (_("Missing DW_AT_lower_bound "
18665 "- DIE at %s [in module %s]"),
18666 sect_offset_str (die->sect_off),
18667 objfile_name (cu->per_objfile->objfile));
18668
18669 struct attribute *attr_ub, *attr_count;
18670 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18671 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18672 {
18673 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18674 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18675 {
18676 /* If bounds are constant do the final calculation here. */
18677 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18678 high.set_const_val (low.const_val () + high.const_val () - 1);
18679 else
18680 high_bound_is_count = 1;
18681 }
18682 else
18683 {
18684 if (attr_ub != NULL)
18685 complaint (_("Unresolved DW_AT_upper_bound "
18686 "- DIE at %s [in module %s]"),
18687 sect_offset_str (die->sect_off),
18688 objfile_name (cu->per_objfile->objfile));
18689 if (attr_count != NULL)
18690 complaint (_("Unresolved DW_AT_count "
18691 "- DIE at %s [in module %s]"),
18692 sect_offset_str (die->sect_off),
18693 objfile_name (cu->per_objfile->objfile));
18694 }
18695 }
18696
18697 LONGEST bias = 0;
18698 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18699 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18700 bias = bias_attr->constant_value (0);
18701
18702 /* Normally, the DWARF producers are expected to use a signed
18703 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18704 But this is unfortunately not always the case, as witnessed
18705 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18706 is used instead. To work around that ambiguity, we treat
18707 the bounds as signed, and thus sign-extend their values, when
18708 the base type is signed. */
18709 negative_mask =
18710 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18711 if (low.kind () == PROP_CONST
18712 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18713 low.set_const_val (low.const_val () | negative_mask);
18714 if (high.kind () == PROP_CONST
18715 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18716 high.set_const_val (high.const_val () | negative_mask);
18717
18718 /* Check for bit and byte strides. */
18719 struct dynamic_prop byte_stride_prop;
18720 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18721 if (attr_byte_stride != nullptr)
18722 {
18723 struct type *prop_type = cu->addr_sized_int_type (false);
18724 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18725 prop_type);
18726 }
18727
18728 struct dynamic_prop bit_stride_prop;
18729 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18730 if (attr_bit_stride != nullptr)
18731 {
18732 /* It only makes sense to have either a bit or byte stride. */
18733 if (attr_byte_stride != nullptr)
18734 {
18735 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18736 "- DIE at %s [in module %s]"),
18737 sect_offset_str (die->sect_off),
18738 objfile_name (cu->per_objfile->objfile));
18739 attr_bit_stride = nullptr;
18740 }
18741 else
18742 {
18743 struct type *prop_type = cu->addr_sized_int_type (false);
18744 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18745 prop_type);
18746 }
18747 }
18748
18749 if (attr_byte_stride != nullptr
18750 || attr_bit_stride != nullptr)
18751 {
18752 bool byte_stride_p = (attr_byte_stride != nullptr);
18753 struct dynamic_prop *stride
18754 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18755
18756 range_type
18757 = create_range_type_with_stride (NULL, orig_base_type, &low,
18758 &high, bias, stride, byte_stride_p);
18759 }
18760 else
18761 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18762
18763 if (high_bound_is_count)
18764 range_type->bounds ()->flag_upper_bound_is_count = 1;
18765
18766 /* Ada expects an empty array on no boundary attributes. */
18767 if (attr == NULL && cu->per_cu->lang != language_ada)
18768 range_type->bounds ()->high.set_undefined ();
18769
18770 name = dwarf2_name (die, cu);
18771 if (name)
18772 range_type->set_name (name);
18773
18774 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18775 if (attr != nullptr)
18776 TYPE_LENGTH (range_type) = attr->constant_value (0);
18777
18778 maybe_set_alignment (cu, die, range_type);
18779
18780 set_die_type (die, range_type, cu);
18781
18782 /* set_die_type should be already done. */
18783 set_descriptive_type (range_type, die, cu);
18784
18785 return range_type;
18786 }
18787
18788 static struct type *
18789 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18790 {
18791 struct type *type;
18792
18793 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18794 type->set_name (dwarf2_name (die, cu));
18795
18796 /* In Ada, an unspecified type is typically used when the description
18797 of the type is deferred to a different unit. When encountering
18798 such a type, we treat it as a stub, and try to resolve it later on,
18799 when needed. */
18800 if (cu->per_cu->lang == language_ada)
18801 type->set_is_stub (true);
18802
18803 return set_die_type (die, type, cu);
18804 }
18805
18806 /* Read a single die and all its descendents. Set the die's sibling
18807 field to NULL; set other fields in the die correctly, and set all
18808 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18809 location of the info_ptr after reading all of those dies. PARENT
18810 is the parent of the die in question. */
18811
18812 static struct die_info *
18813 read_die_and_children (const struct die_reader_specs *reader,
18814 const gdb_byte *info_ptr,
18815 const gdb_byte **new_info_ptr,
18816 struct die_info *parent)
18817 {
18818 struct die_info *die;
18819 const gdb_byte *cur_ptr;
18820
18821 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18822 if (die == NULL)
18823 {
18824 *new_info_ptr = cur_ptr;
18825 return NULL;
18826 }
18827 store_in_ref_table (die, reader->cu);
18828
18829 if (die->has_children)
18830 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18831 else
18832 {
18833 die->child = NULL;
18834 *new_info_ptr = cur_ptr;
18835 }
18836
18837 die->sibling = NULL;
18838 die->parent = parent;
18839 return die;
18840 }
18841
18842 /* Read a die, all of its descendents, and all of its siblings; set
18843 all of the fields of all of the dies correctly. Arguments are as
18844 in read_die_and_children. */
18845
18846 static struct die_info *
18847 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18848 const gdb_byte *info_ptr,
18849 const gdb_byte **new_info_ptr,
18850 struct die_info *parent)
18851 {
18852 struct die_info *first_die, *last_sibling;
18853 const gdb_byte *cur_ptr;
18854
18855 cur_ptr = info_ptr;
18856 first_die = last_sibling = NULL;
18857
18858 while (1)
18859 {
18860 struct die_info *die
18861 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18862
18863 if (die == NULL)
18864 {
18865 *new_info_ptr = cur_ptr;
18866 return first_die;
18867 }
18868
18869 if (!first_die)
18870 first_die = die;
18871 else
18872 last_sibling->sibling = die;
18873
18874 last_sibling = die;
18875 }
18876 }
18877
18878 /* Read a die, all of its descendents, and all of its siblings; set
18879 all of the fields of all of the dies correctly. Arguments are as
18880 in read_die_and_children.
18881 This the main entry point for reading a DIE and all its children. */
18882
18883 static struct die_info *
18884 read_die_and_siblings (const struct die_reader_specs *reader,
18885 const gdb_byte *info_ptr,
18886 const gdb_byte **new_info_ptr,
18887 struct die_info *parent)
18888 {
18889 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18890 new_info_ptr, parent);
18891
18892 if (dwarf_die_debug)
18893 {
18894 gdb_printf (gdb_stdlog,
18895 "Read die from %s@0x%x of %s:\n",
18896 reader->die_section->get_name (),
18897 (unsigned) (info_ptr - reader->die_section->buffer),
18898 bfd_get_filename (reader->abfd));
18899 dump_die (die, dwarf_die_debug);
18900 }
18901
18902 return die;
18903 }
18904
18905 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18906 attributes.
18907 The caller is responsible for filling in the extra attributes
18908 and updating (*DIEP)->num_attrs.
18909 Set DIEP to point to a newly allocated die with its information,
18910 except for its child, sibling, and parent fields. */
18911
18912 static const gdb_byte *
18913 read_full_die_1 (const struct die_reader_specs *reader,
18914 struct die_info **diep, const gdb_byte *info_ptr,
18915 int num_extra_attrs)
18916 {
18917 unsigned int abbrev_number, bytes_read, i;
18918 const struct abbrev_info *abbrev;
18919 struct die_info *die;
18920 struct dwarf2_cu *cu = reader->cu;
18921 bfd *abfd = reader->abfd;
18922
18923 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18924 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18925 info_ptr += bytes_read;
18926 if (!abbrev_number)
18927 {
18928 *diep = NULL;
18929 return info_ptr;
18930 }
18931
18932 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18933 if (!abbrev)
18934 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18935 abbrev_number,
18936 bfd_get_filename (abfd));
18937
18938 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18939 die->sect_off = sect_off;
18940 die->tag = abbrev->tag;
18941 die->abbrev = abbrev_number;
18942 die->has_children = abbrev->has_children;
18943
18944 /* Make the result usable.
18945 The caller needs to update num_attrs after adding the extra
18946 attributes. */
18947 die->num_attrs = abbrev->num_attrs;
18948
18949 bool any_need_reprocess = false;
18950 for (i = 0; i < abbrev->num_attrs; ++i)
18951 {
18952 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18953 info_ptr);
18954 if (die->attrs[i].requires_reprocessing_p ())
18955 any_need_reprocess = true;
18956 }
18957
18958 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18959 if (attr != nullptr && attr->form_is_unsigned ())
18960 cu->str_offsets_base = attr->as_unsigned ();
18961
18962 attr = die->attr (DW_AT_loclists_base);
18963 if (attr != nullptr)
18964 cu->loclist_base = attr->as_unsigned ();
18965
18966 auto maybe_addr_base = die->addr_base ();
18967 if (maybe_addr_base.has_value ())
18968 cu->addr_base = *maybe_addr_base;
18969
18970 attr = die->attr (DW_AT_rnglists_base);
18971 if (attr != nullptr)
18972 cu->rnglists_base = attr->as_unsigned ();
18973
18974 if (any_need_reprocess)
18975 {
18976 for (i = 0; i < abbrev->num_attrs; ++i)
18977 {
18978 if (die->attrs[i].requires_reprocessing_p ())
18979 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18980 }
18981 }
18982 *diep = die;
18983 return info_ptr;
18984 }
18985
18986 /* Read a die and all its attributes.
18987 Set DIEP to point to a newly allocated die with its information,
18988 except for its child, sibling, and parent fields. */
18989
18990 static const gdb_byte *
18991 read_full_die (const struct die_reader_specs *reader,
18992 struct die_info **diep, const gdb_byte *info_ptr)
18993 {
18994 const gdb_byte *result;
18995
18996 result = read_full_die_1 (reader, diep, info_ptr, 0);
18997
18998 if (dwarf_die_debug)
18999 {
19000 gdb_printf (gdb_stdlog,
19001 "Read die from %s@0x%x of %s:\n",
19002 reader->die_section->get_name (),
19003 (unsigned) (info_ptr - reader->die_section->buffer),
19004 bfd_get_filename (reader->abfd));
19005 dump_die (*diep, dwarf_die_debug);
19006 }
19007
19008 return result;
19009 }
19010 \f
19011
19012 /* Returns nonzero if TAG represents a type that we might generate a partial
19013 symbol for. */
19014
19015 static int
19016 is_type_tag_for_partial (int tag, enum language lang)
19017 {
19018 switch (tag)
19019 {
19020 #if 0
19021 /* Some types that would be reasonable to generate partial symbols for,
19022 that we don't at present. Note that normally this does not
19023 matter, mainly because C compilers don't give names to these
19024 types, but instead emit DW_TAG_typedef. */
19025 case DW_TAG_file_type:
19026 case DW_TAG_ptr_to_member_type:
19027 case DW_TAG_set_type:
19028 case DW_TAG_string_type:
19029 case DW_TAG_subroutine_type:
19030 #endif
19031
19032 /* GNAT may emit an array with a name, but no typedef, so we
19033 need to make a symbol in this case. */
19034 case DW_TAG_array_type:
19035 return lang == language_ada;
19036
19037 case DW_TAG_base_type:
19038 case DW_TAG_class_type:
19039 case DW_TAG_interface_type:
19040 case DW_TAG_enumeration_type:
19041 case DW_TAG_structure_type:
19042 case DW_TAG_subrange_type:
19043 case DW_TAG_generic_subrange:
19044 case DW_TAG_typedef:
19045 case DW_TAG_union_type:
19046 return 1;
19047 default:
19048 return 0;
19049 }
19050 }
19051
19052 /* Load all DIEs that are interesting for partial symbols into memory. */
19053
19054 static struct partial_die_info *
19055 load_partial_dies (const struct die_reader_specs *reader,
19056 const gdb_byte *info_ptr, int building_psymtab)
19057 {
19058 struct dwarf2_cu *cu = reader->cu;
19059 struct objfile *objfile = cu->per_objfile->objfile;
19060 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19061 unsigned int bytes_read;
19062 unsigned int load_all = 0;
19063 int nesting_level = 1;
19064
19065 parent_die = NULL;
19066 last_die = NULL;
19067
19068 gdb_assert (cu->per_cu != NULL);
19069 if (cu->load_all_dies)
19070 load_all = 1;
19071
19072 cu->partial_dies
19073 = htab_create_alloc_ex (cu->header.length / 12,
19074 partial_die_hash,
19075 partial_die_eq,
19076 NULL,
19077 &cu->comp_unit_obstack,
19078 hashtab_obstack_allocate,
19079 dummy_obstack_deallocate);
19080
19081 while (1)
19082 {
19083 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19084 &bytes_read);
19085
19086 /* A NULL abbrev means the end of a series of children. */
19087 if (abbrev == NULL)
19088 {
19089 if (--nesting_level == 0)
19090 return first_die;
19091
19092 info_ptr += bytes_read;
19093 last_die = parent_die;
19094 parent_die = parent_die->die_parent;
19095 continue;
19096 }
19097
19098 /* Check for template arguments. We never save these; if
19099 they're seen, we just mark the parent, and go on our way. */
19100 if (parent_die != NULL
19101 && cu->per_cu->lang == language_cplus
19102 && (abbrev->tag == DW_TAG_template_type_param
19103 || abbrev->tag == DW_TAG_template_value_param))
19104 {
19105 parent_die->has_template_arguments = 1;
19106
19107 if (!load_all)
19108 {
19109 /* We don't need a partial DIE for the template argument. */
19110 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19111 continue;
19112 }
19113 }
19114
19115 /* We only recurse into c++ subprograms looking for template arguments.
19116 Skip their other children. */
19117 if (!load_all
19118 && cu->per_cu->lang == language_cplus
19119 && parent_die != NULL
19120 && parent_die->tag == DW_TAG_subprogram
19121 && abbrev->tag != DW_TAG_inlined_subroutine)
19122 {
19123 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19124 continue;
19125 }
19126
19127 /* Check whether this DIE is interesting enough to save. Normally
19128 we would not be interested in members here, but there may be
19129 later variables referencing them via DW_AT_specification (for
19130 static members). */
19131 if (!load_all
19132 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
19133 && abbrev->tag != DW_TAG_constant
19134 && abbrev->tag != DW_TAG_enumerator
19135 && abbrev->tag != DW_TAG_subprogram
19136 && abbrev->tag != DW_TAG_inlined_subroutine
19137 && abbrev->tag != DW_TAG_lexical_block
19138 && abbrev->tag != DW_TAG_variable
19139 && abbrev->tag != DW_TAG_namespace
19140 && abbrev->tag != DW_TAG_module
19141 && abbrev->tag != DW_TAG_member
19142 && abbrev->tag != DW_TAG_imported_unit
19143 && abbrev->tag != DW_TAG_imported_declaration)
19144 {
19145 /* Otherwise we skip to the next sibling, if any. */
19146 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19147 continue;
19148 }
19149
19150 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19151 abbrev);
19152
19153 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19154
19155 /* This two-pass algorithm for processing partial symbols has a
19156 high cost in cache pressure. Thus, handle some simple cases
19157 here which cover the majority of C partial symbols. DIEs
19158 which neither have specification tags in them, nor could have
19159 specification tags elsewhere pointing at them, can simply be
19160 processed and discarded.
19161
19162 This segment is also optional; scan_partial_symbols and
19163 add_partial_symbol will handle these DIEs if we chain
19164 them in normally. When compilers which do not emit large
19165 quantities of duplicate debug information are more common,
19166 this code can probably be removed. */
19167
19168 /* Any complete simple types at the top level (pretty much all
19169 of them, for a language without namespaces), can be processed
19170 directly. */
19171 if (parent_die == NULL
19172 && pdi.has_specification == 0
19173 && pdi.is_declaration == 0
19174 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19175 || pdi.tag == DW_TAG_base_type
19176 || pdi.tag == DW_TAG_array_type
19177 || pdi.tag == DW_TAG_generic_subrange
19178 || pdi.tag == DW_TAG_subrange_type))
19179 {
19180 if (building_psymtab && pdi.raw_name != NULL)
19181 add_partial_symbol (&pdi, cu);
19182
19183 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19184 continue;
19185 }
19186
19187 /* The exception for DW_TAG_typedef with has_children above is
19188 a workaround of GCC PR debug/47510. In the case of this complaint
19189 type_name_or_error will error on such types later.
19190
19191 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19192 it could not find the child DIEs referenced later, this is checked
19193 above. In correct DWARF DW_TAG_typedef should have no children. */
19194
19195 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19196 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19197 "- DIE at %s [in module %s]"),
19198 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19199
19200 /* If we're at the second level, and we're an enumerator, and
19201 our parent has no specification (meaning possibly lives in a
19202 namespace elsewhere), then we can add the partial symbol now
19203 instead of queueing it. */
19204 if (pdi.tag == DW_TAG_enumerator
19205 && parent_die != NULL
19206 && parent_die->die_parent == NULL
19207 && parent_die->tag == DW_TAG_enumeration_type
19208 && parent_die->has_specification == 0)
19209 {
19210 if (pdi.raw_name == NULL)
19211 complaint (_("malformed enumerator DIE ignored"));
19212 else if (building_psymtab)
19213 add_partial_symbol (&pdi, cu);
19214
19215 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19216 continue;
19217 }
19218
19219 struct partial_die_info *part_die
19220 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19221
19222 /* We'll save this DIE so link it in. */
19223 part_die->die_parent = parent_die;
19224 part_die->die_sibling = NULL;
19225 part_die->die_child = NULL;
19226
19227 if (last_die && last_die == parent_die)
19228 last_die->die_child = part_die;
19229 else if (last_die)
19230 last_die->die_sibling = part_die;
19231
19232 last_die = part_die;
19233
19234 if (first_die == NULL)
19235 first_die = part_die;
19236
19237 /* Maybe add the DIE to the hash table. Not all DIEs that we
19238 find interesting need to be in the hash table, because we
19239 also have the parent/sibling/child chains; only those that we
19240 might refer to by offset later during partial symbol reading.
19241
19242 For now this means things that might have be the target of a
19243 DW_AT_specification, DW_AT_abstract_origin, or
19244 DW_AT_extension. DW_AT_extension will refer only to
19245 namespaces; DW_AT_abstract_origin refers to functions (and
19246 many things under the function DIE, but we do not recurse
19247 into function DIEs during partial symbol reading) and
19248 possibly variables as well; DW_AT_specification refers to
19249 declarations. Declarations ought to have the DW_AT_declaration
19250 flag. It happens that GCC forgets to put it in sometimes, but
19251 only for functions, not for types.
19252
19253 Adding more things than necessary to the hash table is harmless
19254 except for the performance cost. Adding too few will result in
19255 wasted time in find_partial_die, when we reread the compilation
19256 unit with load_all_dies set. */
19257
19258 if (load_all
19259 || abbrev->tag == DW_TAG_constant
19260 || abbrev->tag == DW_TAG_subprogram
19261 || abbrev->tag == DW_TAG_variable
19262 || abbrev->tag == DW_TAG_namespace
19263 || part_die->is_declaration)
19264 {
19265 void **slot;
19266
19267 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19268 to_underlying (part_die->sect_off),
19269 INSERT);
19270 *slot = part_die;
19271 }
19272
19273 /* For some DIEs we want to follow their children (if any). For C
19274 we have no reason to follow the children of structures; for other
19275 languages we have to, so that we can get at method physnames
19276 to infer fully qualified class names, for DW_AT_specification,
19277 and for C++ template arguments. For C++, we also look one level
19278 inside functions to find template arguments (if the name of the
19279 function does not already contain the template arguments).
19280
19281 For Ada and Fortran, we need to scan the children of subprograms
19282 and lexical blocks as well because these languages allow the
19283 definition of nested entities that could be interesting for the
19284 debugger, such as nested subprograms for instance. */
19285 if (last_die->has_children
19286 && (load_all
19287 || last_die->tag == DW_TAG_namespace
19288 || last_die->tag == DW_TAG_module
19289 || last_die->tag == DW_TAG_enumeration_type
19290 || (cu->per_cu->lang == language_cplus
19291 && last_die->tag == DW_TAG_subprogram
19292 && (last_die->raw_name == NULL
19293 || strchr (last_die->raw_name, '<') == NULL))
19294 || (cu->per_cu->lang != language_c
19295 && (last_die->tag == DW_TAG_class_type
19296 || last_die->tag == DW_TAG_interface_type
19297 || last_die->tag == DW_TAG_structure_type
19298 || last_die->tag == DW_TAG_union_type))
19299 || ((cu->per_cu->lang == language_ada
19300 || cu->per_cu->lang == language_fortran)
19301 && (last_die->tag == DW_TAG_subprogram
19302 || last_die->tag == DW_TAG_lexical_block))))
19303 {
19304 nesting_level++;
19305 parent_die = last_die;
19306 continue;
19307 }
19308
19309 /* Otherwise we skip to the next sibling, if any. */
19310 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19311
19312 /* Back to the top, do it again. */
19313 }
19314 }
19315
19316 partial_die_info::partial_die_info (sect_offset sect_off_,
19317 const struct abbrev_info *abbrev)
19318 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19319 {
19320 }
19321
19322 /* See class definition. */
19323
19324 const char *
19325 partial_die_info::name (dwarf2_cu *cu)
19326 {
19327 if (!canonical_name && raw_name != nullptr)
19328 {
19329 struct objfile *objfile = cu->per_objfile->objfile;
19330 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19331 canonical_name = 1;
19332 }
19333
19334 return raw_name;
19335 }
19336
19337 /* Read a minimal amount of information into the minimal die structure.
19338 INFO_PTR should point just after the initial uleb128 of a DIE. */
19339
19340 const gdb_byte *
19341 partial_die_info::read (const struct die_reader_specs *reader,
19342 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19343 {
19344 struct dwarf2_cu *cu = reader->cu;
19345 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19346 unsigned int i;
19347 int has_low_pc_attr = 0;
19348 int has_high_pc_attr = 0;
19349 int high_pc_relative = 0;
19350
19351 for (i = 0; i < abbrev.num_attrs; ++i)
19352 {
19353 attribute attr;
19354 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19355 /* String and address offsets that need to do the reprocessing have
19356 already been read at this point, so there is no need to wait until
19357 the loop terminates to do the reprocessing. */
19358 if (attr.requires_reprocessing_p ())
19359 read_attribute_reprocess (reader, &attr, tag);
19360 /* Store the data if it is of an attribute we want to keep in a
19361 partial symbol table. */
19362 switch (attr.name)
19363 {
19364 case DW_AT_name:
19365 switch (tag)
19366 {
19367 case DW_TAG_compile_unit:
19368 case DW_TAG_partial_unit:
19369 case DW_TAG_type_unit:
19370 /* Compilation units have a DW_AT_name that is a filename, not
19371 a source language identifier. */
19372 case DW_TAG_enumeration_type:
19373 case DW_TAG_enumerator:
19374 /* These tags always have simple identifiers already; no need
19375 to canonicalize them. */
19376 canonical_name = 1;
19377 raw_name = attr.as_string ();
19378 break;
19379 default:
19380 canonical_name = 0;
19381 raw_name = attr.as_string ();
19382 break;
19383 }
19384 break;
19385 case DW_AT_linkage_name:
19386 case DW_AT_MIPS_linkage_name:
19387 /* Note that both forms of linkage name might appear. We
19388 assume they will be the same, and we only store the last
19389 one we see. */
19390 linkage_name = attr.as_string ();
19391 break;
19392 case DW_AT_low_pc:
19393 has_low_pc_attr = 1;
19394 lowpc = attr.as_address ();
19395 break;
19396 case DW_AT_high_pc:
19397 has_high_pc_attr = 1;
19398 highpc = attr.as_address ();
19399 if (cu->header.version >= 4 && attr.form_is_constant ())
19400 high_pc_relative = 1;
19401 break;
19402 case DW_AT_location:
19403 /* Support the .debug_loc offsets. */
19404 if (attr.form_is_block ())
19405 {
19406 d.locdesc = attr.as_block ();
19407 }
19408 else if (attr.form_is_section_offset ())
19409 {
19410 dwarf2_complex_location_expr_complaint ();
19411 }
19412 else
19413 {
19414 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19415 "partial symbol information");
19416 }
19417 break;
19418 case DW_AT_external:
19419 is_external = attr.as_boolean ();
19420 break;
19421 case DW_AT_declaration:
19422 is_declaration = attr.as_boolean ();
19423 break;
19424 case DW_AT_type:
19425 has_type = 1;
19426 break;
19427 case DW_AT_abstract_origin:
19428 case DW_AT_specification:
19429 case DW_AT_extension:
19430 has_specification = 1;
19431 spec_offset = attr.get_ref_die_offset ();
19432 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19433 || cu->per_cu->is_dwz);
19434 break;
19435 case DW_AT_sibling:
19436 /* Ignore absolute siblings, they might point outside of
19437 the current compile unit. */
19438 if (attr.form == DW_FORM_ref_addr)
19439 complaint (_("ignoring absolute DW_AT_sibling"));
19440 else
19441 {
19442 const gdb_byte *buffer = reader->buffer;
19443 sect_offset off = attr.get_ref_die_offset ();
19444 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19445
19446 if (sibling_ptr < info_ptr)
19447 complaint (_("DW_AT_sibling points backwards"));
19448 else if (sibling_ptr > reader->buffer_end)
19449 reader->die_section->overflow_complaint ();
19450 else
19451 sibling = sibling_ptr;
19452 }
19453 break;
19454 case DW_AT_byte_size:
19455 has_byte_size = 1;
19456 break;
19457 case DW_AT_const_value:
19458 has_const_value = 1;
19459 break;
19460 case DW_AT_calling_convention:
19461 /* DWARF doesn't provide a way to identify a program's source-level
19462 entry point. DW_AT_calling_convention attributes are only meant
19463 to describe functions' calling conventions.
19464
19465 However, because it's a necessary piece of information in
19466 Fortran, and before DWARF 4 DW_CC_program was the only
19467 piece of debugging information whose definition refers to
19468 a 'main program' at all, several compilers marked Fortran
19469 main programs with DW_CC_program --- even when those
19470 functions use the standard calling conventions.
19471
19472 Although DWARF now specifies a way to provide this
19473 information, we support this practice for backward
19474 compatibility. */
19475 if (attr.constant_value (0) == DW_CC_program
19476 && cu->per_cu->lang == language_fortran)
19477 main_subprogram = 1;
19478 break;
19479 case DW_AT_inline:
19480 {
19481 LONGEST value = attr.constant_value (-1);
19482 if (value == DW_INL_inlined
19483 || value == DW_INL_declared_inlined)
19484 may_be_inlined = 1;
19485 }
19486 break;
19487
19488 case DW_AT_import:
19489 if (tag == DW_TAG_imported_unit)
19490 {
19491 d.sect_off = attr.get_ref_die_offset ();
19492 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19493 || cu->per_cu->is_dwz);
19494 }
19495 break;
19496
19497 case DW_AT_main_subprogram:
19498 main_subprogram = attr.as_boolean ();
19499 break;
19500
19501 case DW_AT_ranges:
19502 {
19503 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19504 on DWARF version). */
19505 ranges_offset = attr.as_unsigned ();
19506
19507 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19508 this value. */
19509 if (tag != DW_TAG_compile_unit)
19510 ranges_offset += cu->gnu_ranges_base;
19511
19512 has_range_info = 1;
19513 }
19514 break;
19515
19516 default:
19517 break;
19518 }
19519 }
19520
19521 /* For Ada, if both the name and the linkage name appear, we prefer
19522 the latter. This lets "catch exception" work better, regardless
19523 of the order in which the name and linkage name were emitted.
19524 Really, though, this is just a workaround for the fact that gdb
19525 doesn't store both the name and the linkage name. */
19526 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
19527 raw_name = linkage_name;
19528
19529 if (high_pc_relative)
19530 highpc += lowpc;
19531
19532 if (has_low_pc_attr && has_high_pc_attr)
19533 {
19534 /* When using the GNU linker, .gnu.linkonce. sections are used to
19535 eliminate duplicate copies of functions and vtables and such.
19536 The linker will arbitrarily choose one and discard the others.
19537 The AT_*_pc values for such functions refer to local labels in
19538 these sections. If the section from that file was discarded, the
19539 labels are not in the output, so the relocs get a value of 0.
19540 If this is a discarded function, mark the pc bounds as invalid,
19541 so that GDB will ignore it. */
19542 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19543 {
19544 struct objfile *objfile = per_objfile->objfile;
19545 struct gdbarch *gdbarch = objfile->arch ();
19546
19547 complaint (_("DW_AT_low_pc %s is zero "
19548 "for DIE at %s [in module %s]"),
19549 paddress (gdbarch, lowpc),
19550 sect_offset_str (sect_off),
19551 objfile_name (objfile));
19552 }
19553 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19554 else if (lowpc >= highpc)
19555 {
19556 struct objfile *objfile = per_objfile->objfile;
19557 struct gdbarch *gdbarch = objfile->arch ();
19558
19559 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19560 "for DIE at %s [in module %s]"),
19561 paddress (gdbarch, lowpc),
19562 paddress (gdbarch, highpc),
19563 sect_offset_str (sect_off),
19564 objfile_name (objfile));
19565 }
19566 else
19567 has_pc_info = 1;
19568 }
19569
19570 return info_ptr;
19571 }
19572
19573 /* Find a cached partial DIE at OFFSET in CU. */
19574
19575 struct partial_die_info *
19576 dwarf2_cu::find_partial_die (sect_offset sect_off)
19577 {
19578 struct partial_die_info *lookup_die = NULL;
19579 struct partial_die_info part_die (sect_off);
19580
19581 lookup_die = ((struct partial_die_info *)
19582 htab_find_with_hash (partial_dies, &part_die,
19583 to_underlying (sect_off)));
19584
19585 return lookup_die;
19586 }
19587
19588 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19589 except in the case of .debug_types DIEs which do not reference
19590 outside their CU (they do however referencing other types via
19591 DW_FORM_ref_sig8). */
19592
19593 static const struct cu_partial_die_info
19594 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19595 {
19596 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19597 struct objfile *objfile = per_objfile->objfile;
19598 struct partial_die_info *pd = NULL;
19599
19600 if (offset_in_dwz == cu->per_cu->is_dwz
19601 && cu->header.offset_in_cu_p (sect_off))
19602 {
19603 pd = cu->find_partial_die (sect_off);
19604 if (pd != NULL)
19605 return { cu, pd };
19606 /* We missed recording what we needed.
19607 Load all dies and try again. */
19608 }
19609 else
19610 {
19611 /* TUs don't reference other CUs/TUs (except via type signatures). */
19612 if (cu->per_cu->is_debug_types)
19613 {
19614 error (_("Dwarf Error: Type Unit at offset %s contains"
19615 " external reference to offset %s [in module %s].\n"),
19616 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19617 bfd_get_filename (objfile->obfd));
19618 }
19619 dwarf2_per_cu_data *per_cu
19620 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19621 per_objfile->per_bfd);
19622
19623 cu = per_objfile->get_cu (per_cu);
19624 if (cu == NULL || cu->partial_dies == NULL)
19625 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19626
19627 cu = per_objfile->get_cu (per_cu);
19628
19629 cu->last_used = 0;
19630 pd = cu->find_partial_die (sect_off);
19631 }
19632
19633 /* If we didn't find it, and not all dies have been loaded,
19634 load them all and try again. */
19635
19636 if (pd == NULL && cu->load_all_dies == 0)
19637 {
19638 cu->load_all_dies = 1;
19639
19640 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19641 THIS_CU->cu may already be in use. So we can't just free it and
19642 replace its DIEs with the ones we read in. Instead, we leave those
19643 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19644 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19645 set. */
19646 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19647
19648 pd = cu->find_partial_die (sect_off);
19649 }
19650
19651 if (pd == NULL)
19652 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19653 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19654 return { cu, pd };
19655 }
19656
19657 /* See if we can figure out if the class lives in a namespace. We do
19658 this by looking for a member function; its demangled name will
19659 contain namespace info, if there is any. */
19660
19661 static void
19662 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19663 struct dwarf2_cu *cu)
19664 {
19665 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19666 what template types look like, because the demangler
19667 frequently doesn't give the same name as the debug info. We
19668 could fix this by only using the demangled name to get the
19669 prefix (but see comment in read_structure_type). */
19670
19671 struct partial_die_info *real_pdi;
19672 struct partial_die_info *child_pdi;
19673
19674 /* If this DIE (this DIE's specification, if any) has a parent, then
19675 we should not do this. We'll prepend the parent's fully qualified
19676 name when we create the partial symbol. */
19677
19678 real_pdi = struct_pdi;
19679 while (real_pdi->has_specification)
19680 {
19681 auto res = find_partial_die (real_pdi->spec_offset,
19682 real_pdi->spec_is_dwz, cu);
19683 real_pdi = res.pdi;
19684 cu = res.cu;
19685 }
19686
19687 if (real_pdi->die_parent != NULL)
19688 return;
19689
19690 for (child_pdi = struct_pdi->die_child;
19691 child_pdi != NULL;
19692 child_pdi = child_pdi->die_sibling)
19693 {
19694 if (child_pdi->tag == DW_TAG_subprogram
19695 && child_pdi->linkage_name != NULL)
19696 {
19697 gdb::unique_xmalloc_ptr<char> actual_class_name
19698 (cu->language_defn->class_name_from_physname
19699 (child_pdi->linkage_name));
19700 if (actual_class_name != NULL)
19701 {
19702 struct objfile *objfile = cu->per_objfile->objfile;
19703 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19704 struct_pdi->canonical_name = 1;
19705 }
19706 break;
19707 }
19708 }
19709 }
19710
19711 /* Return true if a DIE with TAG may have the DW_AT_const_value
19712 attribute. */
19713
19714 static bool
19715 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19716 {
19717 switch (tag)
19718 {
19719 case DW_TAG_constant:
19720 case DW_TAG_enumerator:
19721 case DW_TAG_formal_parameter:
19722 case DW_TAG_template_value_param:
19723 case DW_TAG_variable:
19724 return true;
19725 }
19726
19727 return false;
19728 }
19729
19730 void
19731 partial_die_info::fixup (struct dwarf2_cu *cu)
19732 {
19733 /* Once we've fixed up a die, there's no point in doing so again.
19734 This also avoids a memory leak if we were to call
19735 guess_partial_die_structure_name multiple times. */
19736 if (fixup_called)
19737 return;
19738
19739 /* If we found a reference attribute and the DIE has no name, try
19740 to find a name in the referred to DIE. */
19741
19742 if (raw_name == NULL && has_specification)
19743 {
19744 struct partial_die_info *spec_die;
19745
19746 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19747 spec_die = res.pdi;
19748 cu = res.cu;
19749
19750 spec_die->fixup (cu);
19751
19752 if (spec_die->raw_name)
19753 {
19754 raw_name = spec_die->raw_name;
19755 canonical_name = spec_die->canonical_name;
19756
19757 /* Copy DW_AT_external attribute if it is set. */
19758 if (spec_die->is_external)
19759 is_external = spec_die->is_external;
19760 }
19761 }
19762
19763 if (!has_const_value && has_specification
19764 && can_have_DW_AT_const_value_p (tag))
19765 {
19766 struct partial_die_info *spec_die;
19767
19768 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19769 spec_die = res.pdi;
19770 cu = res.cu;
19771
19772 spec_die->fixup (cu);
19773
19774 if (spec_die->has_const_value)
19775 {
19776 /* Copy DW_AT_const_value attribute if it is set. */
19777 has_const_value = spec_die->has_const_value;
19778 }
19779 }
19780
19781 /* Set default names for some unnamed DIEs. */
19782
19783 if (raw_name == NULL && tag == DW_TAG_namespace)
19784 {
19785 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19786 canonical_name = 1;
19787 }
19788
19789 /* If there is no parent die to provide a namespace, and there are
19790 children, see if we can determine the namespace from their linkage
19791 name. */
19792 if (cu->per_cu->lang == language_cplus
19793 && !cu->per_objfile->per_bfd->types.empty ()
19794 && die_parent == NULL
19795 && has_children
19796 && (tag == DW_TAG_class_type
19797 || tag == DW_TAG_structure_type
19798 || tag == DW_TAG_union_type))
19799 guess_partial_die_structure_name (this, cu);
19800
19801 /* GCC might emit a nameless struct or union that has a linkage
19802 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19803 if (raw_name == NULL
19804 && (tag == DW_TAG_class_type
19805 || tag == DW_TAG_interface_type
19806 || tag == DW_TAG_structure_type
19807 || tag == DW_TAG_union_type)
19808 && linkage_name != NULL)
19809 {
19810 gdb::unique_xmalloc_ptr<char> demangled
19811 (gdb_demangle (linkage_name, DMGL_TYPES));
19812 if (demangled != nullptr)
19813 {
19814 const char *base;
19815
19816 /* Strip any leading namespaces/classes, keep only the base name.
19817 DW_AT_name for named DIEs does not contain the prefixes. */
19818 base = strrchr (demangled.get (), ':');
19819 if (base && base > demangled.get () && base[-1] == ':')
19820 base++;
19821 else
19822 base = demangled.get ();
19823
19824 struct objfile *objfile = cu->per_objfile->objfile;
19825 raw_name = objfile->intern (base);
19826 canonical_name = 1;
19827 }
19828 }
19829
19830 fixup_called = 1;
19831 }
19832
19833 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19834 contents from the given SECTION in the HEADER.
19835
19836 HEADER_OFFSET is the offset of the header in the section. */
19837 static void
19838 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19839 struct dwarf2_section_info *section,
19840 sect_offset header_offset)
19841 {
19842 unsigned int bytes_read;
19843 bfd *abfd = section->get_bfd_owner ();
19844 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19845
19846 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19847 info_ptr += bytes_read;
19848
19849 header->version = read_2_bytes (abfd, info_ptr);
19850 info_ptr += 2;
19851
19852 header->addr_size = read_1_byte (abfd, info_ptr);
19853 info_ptr += 1;
19854
19855 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19856 info_ptr += 1;
19857
19858 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19859 }
19860
19861 /* Return the DW_AT_loclists_base value for the CU. */
19862 static ULONGEST
19863 lookup_loclist_base (struct dwarf2_cu *cu)
19864 {
19865 /* For the .dwo unit, the loclist_base points to the first offset following
19866 the header. The header consists of the following entities-
19867 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19868 bit format)
19869 2. version (2 bytes)
19870 3. address size (1 byte)
19871 4. segment selector size (1 byte)
19872 5. offset entry count (4 bytes)
19873 These sizes are derived as per the DWARFv5 standard. */
19874 if (cu->dwo_unit != nullptr)
19875 {
19876 if (cu->header.initial_length_size == 4)
19877 return LOCLIST_HEADER_SIZE32;
19878 return LOCLIST_HEADER_SIZE64;
19879 }
19880 return cu->loclist_base;
19881 }
19882
19883 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19884 array of offsets in the .debug_loclists section. */
19885
19886 static sect_offset
19887 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19888 {
19889 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19890 struct objfile *objfile = per_objfile->objfile;
19891 bfd *abfd = objfile->obfd;
19892 ULONGEST loclist_header_size =
19893 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19894 : LOCLIST_HEADER_SIZE64);
19895 ULONGEST loclist_base = lookup_loclist_base (cu);
19896
19897 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19898 ULONGEST start_offset =
19899 loclist_base + loclist_index * cu->header.offset_size;
19900
19901 /* Get loclists section. */
19902 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19903
19904 /* Read the loclists section content. */
19905 section->read (objfile);
19906 if (section->buffer == NULL)
19907 error (_("DW_FORM_loclistx used without .debug_loclists "
19908 "section [in module %s]"), objfile_name (objfile));
19909
19910 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19911 so if loclist_base is smaller than the header size, we have a problem. */
19912 if (loclist_base < loclist_header_size)
19913 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19914 objfile_name (objfile));
19915
19916 /* Read the header of the loclists contribution. */
19917 struct loclists_rnglists_header header;
19918 read_loclists_rnglists_header (&header, section,
19919 (sect_offset) (loclist_base - loclist_header_size));
19920
19921 /* Verify the loclist index is valid. */
19922 if (loclist_index >= header.offset_entry_count)
19923 error (_("DW_FORM_loclistx pointing outside of "
19924 ".debug_loclists offset array [in module %s]"),
19925 objfile_name (objfile));
19926
19927 /* Validate that reading won't go beyond the end of the section. */
19928 if (start_offset + cu->header.offset_size > section->size)
19929 error (_("Reading DW_FORM_loclistx index beyond end of"
19930 ".debug_loclists section [in module %s]"),
19931 objfile_name (objfile));
19932
19933 const gdb_byte *info_ptr = section->buffer + start_offset;
19934
19935 if (cu->header.offset_size == 4)
19936 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19937 else
19938 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19939 }
19940
19941 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19942 array of offsets in the .debug_rnglists section. */
19943
19944 static sect_offset
19945 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19946 dwarf_tag tag)
19947 {
19948 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19949 struct objfile *objfile = dwarf2_per_objfile->objfile;
19950 bfd *abfd = objfile->obfd;
19951 ULONGEST rnglist_header_size =
19952 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19953 : RNGLIST_HEADER_SIZE64);
19954
19955 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19956 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19957 doesn't apply. */
19958 ULONGEST rnglist_base =
19959 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19960
19961 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19962 ULONGEST start_offset =
19963 rnglist_base + rnglist_index * cu->header.offset_size;
19964
19965 /* Get rnglists section. */
19966 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19967
19968 /* Read the rnglists section content. */
19969 section->read (objfile);
19970 if (section->buffer == nullptr)
19971 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19972 "[in module %s]"),
19973 objfile_name (objfile));
19974
19975 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19976 so if rnglist_base is smaller than the header size, we have a problem. */
19977 if (rnglist_base < rnglist_header_size)
19978 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19979 objfile_name (objfile));
19980
19981 /* Read the header of the rnglists contribution. */
19982 struct loclists_rnglists_header header;
19983 read_loclists_rnglists_header (&header, section,
19984 (sect_offset) (rnglist_base - rnglist_header_size));
19985
19986 /* Verify the rnglist index is valid. */
19987 if (rnglist_index >= header.offset_entry_count)
19988 error (_("DW_FORM_rnglistx index pointing outside of "
19989 ".debug_rnglists offset array [in module %s]"),
19990 objfile_name (objfile));
19991
19992 /* Validate that reading won't go beyond the end of the section. */
19993 if (start_offset + cu->header.offset_size > section->size)
19994 error (_("Reading DW_FORM_rnglistx index beyond end of"
19995 ".debug_rnglists section [in module %s]"),
19996 objfile_name (objfile));
19997
19998 const gdb_byte *info_ptr = section->buffer + start_offset;
19999
20000 if (cu->header.offset_size == 4)
20001 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20002 else
20003 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20004 }
20005
20006 /* Process the attributes that had to be skipped in the first round. These
20007 attributes are the ones that need str_offsets_base or addr_base attributes.
20008 They could not have been processed in the first round, because at the time
20009 the values of str_offsets_base or addr_base may not have been known. */
20010 static void
20011 read_attribute_reprocess (const struct die_reader_specs *reader,
20012 struct attribute *attr, dwarf_tag tag)
20013 {
20014 struct dwarf2_cu *cu = reader->cu;
20015 switch (attr->form)
20016 {
20017 case DW_FORM_addrx:
20018 case DW_FORM_GNU_addr_index:
20019 attr->set_address (read_addr_index (cu,
20020 attr->as_unsigned_reprocess ()));
20021 break;
20022 case DW_FORM_loclistx:
20023 {
20024 sect_offset loclists_sect_off
20025 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20026
20027 attr->set_unsigned (to_underlying (loclists_sect_off));
20028 }
20029 break;
20030 case DW_FORM_rnglistx:
20031 {
20032 sect_offset rnglists_sect_off
20033 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20034
20035 attr->set_unsigned (to_underlying (rnglists_sect_off));
20036 }
20037 break;
20038 case DW_FORM_strx:
20039 case DW_FORM_strx1:
20040 case DW_FORM_strx2:
20041 case DW_FORM_strx3:
20042 case DW_FORM_strx4:
20043 case DW_FORM_GNU_str_index:
20044 {
20045 unsigned int str_index = attr->as_unsigned_reprocess ();
20046 gdb_assert (!attr->canonical_string_p ());
20047 if (reader->dwo_file != NULL)
20048 attr->set_string_noncanonical (read_dwo_str_index (reader,
20049 str_index));
20050 else
20051 attr->set_string_noncanonical (read_stub_str_index (cu,
20052 str_index));
20053 break;
20054 }
20055 default:
20056 gdb_assert_not_reached ("Unexpected DWARF form.");
20057 }
20058 }
20059
20060 /* Read an attribute value described by an attribute form. */
20061
20062 static const gdb_byte *
20063 read_attribute_value (const struct die_reader_specs *reader,
20064 struct attribute *attr, unsigned form,
20065 LONGEST implicit_const, const gdb_byte *info_ptr)
20066 {
20067 struct dwarf2_cu *cu = reader->cu;
20068 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20069 struct objfile *objfile = per_objfile->objfile;
20070 bfd *abfd = reader->abfd;
20071 struct comp_unit_head *cu_header = &cu->header;
20072 unsigned int bytes_read;
20073 struct dwarf_block *blk;
20074
20075 attr->form = (enum dwarf_form) form;
20076 switch (form)
20077 {
20078 case DW_FORM_ref_addr:
20079 if (cu_header->version == 2)
20080 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20081 &bytes_read));
20082 else
20083 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20084 &bytes_read));
20085 info_ptr += bytes_read;
20086 break;
20087 case DW_FORM_GNU_ref_alt:
20088 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20089 &bytes_read));
20090 info_ptr += bytes_read;
20091 break;
20092 case DW_FORM_addr:
20093 {
20094 struct gdbarch *gdbarch = objfile->arch ();
20095 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20096 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20097 attr->set_address (addr);
20098 info_ptr += bytes_read;
20099 }
20100 break;
20101 case DW_FORM_block2:
20102 blk = dwarf_alloc_block (cu);
20103 blk->size = read_2_bytes (abfd, info_ptr);
20104 info_ptr += 2;
20105 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20106 info_ptr += blk->size;
20107 attr->set_block (blk);
20108 break;
20109 case DW_FORM_block4:
20110 blk = dwarf_alloc_block (cu);
20111 blk->size = read_4_bytes (abfd, info_ptr);
20112 info_ptr += 4;
20113 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20114 info_ptr += blk->size;
20115 attr->set_block (blk);
20116 break;
20117 case DW_FORM_data2:
20118 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20119 info_ptr += 2;
20120 break;
20121 case DW_FORM_data4:
20122 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20123 info_ptr += 4;
20124 break;
20125 case DW_FORM_data8:
20126 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20127 info_ptr += 8;
20128 break;
20129 case DW_FORM_data16:
20130 blk = dwarf_alloc_block (cu);
20131 blk->size = 16;
20132 blk->data = read_n_bytes (abfd, info_ptr, 16);
20133 info_ptr += 16;
20134 attr->set_block (blk);
20135 break;
20136 case DW_FORM_sec_offset:
20137 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20138 &bytes_read));
20139 info_ptr += bytes_read;
20140 break;
20141 case DW_FORM_loclistx:
20142 {
20143 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20144 &bytes_read));
20145 info_ptr += bytes_read;
20146 }
20147 break;
20148 case DW_FORM_string:
20149 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20150 &bytes_read));
20151 info_ptr += bytes_read;
20152 break;
20153 case DW_FORM_strp:
20154 if (!cu->per_cu->is_dwz)
20155 {
20156 attr->set_string_noncanonical
20157 (read_indirect_string (per_objfile,
20158 abfd, info_ptr, cu_header,
20159 &bytes_read));
20160 info_ptr += bytes_read;
20161 break;
20162 }
20163 /* FALLTHROUGH */
20164 case DW_FORM_line_strp:
20165 if (!cu->per_cu->is_dwz)
20166 {
20167 attr->set_string_noncanonical
20168 (per_objfile->read_line_string (info_ptr, cu_header,
20169 &bytes_read));
20170 info_ptr += bytes_read;
20171 break;
20172 }
20173 /* FALLTHROUGH */
20174 case DW_FORM_GNU_strp_alt:
20175 {
20176 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20177 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20178 &bytes_read);
20179
20180 attr->set_string_noncanonical
20181 (dwz->read_string (objfile, str_offset));
20182 info_ptr += bytes_read;
20183 }
20184 break;
20185 case DW_FORM_exprloc:
20186 case DW_FORM_block:
20187 blk = dwarf_alloc_block (cu);
20188 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20189 info_ptr += bytes_read;
20190 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20191 info_ptr += blk->size;
20192 attr->set_block (blk);
20193 break;
20194 case DW_FORM_block1:
20195 blk = dwarf_alloc_block (cu);
20196 blk->size = read_1_byte (abfd, info_ptr);
20197 info_ptr += 1;
20198 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20199 info_ptr += blk->size;
20200 attr->set_block (blk);
20201 break;
20202 case DW_FORM_data1:
20203 case DW_FORM_flag:
20204 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20205 info_ptr += 1;
20206 break;
20207 case DW_FORM_flag_present:
20208 attr->set_unsigned (1);
20209 break;
20210 case DW_FORM_sdata:
20211 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20212 info_ptr += bytes_read;
20213 break;
20214 case DW_FORM_rnglistx:
20215 {
20216 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20217 &bytes_read));
20218 info_ptr += bytes_read;
20219 }
20220 break;
20221 case DW_FORM_udata:
20222 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20223 info_ptr += bytes_read;
20224 break;
20225 case DW_FORM_ref1:
20226 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20227 + read_1_byte (abfd, info_ptr)));
20228 info_ptr += 1;
20229 break;
20230 case DW_FORM_ref2:
20231 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20232 + read_2_bytes (abfd, info_ptr)));
20233 info_ptr += 2;
20234 break;
20235 case DW_FORM_ref4:
20236 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20237 + read_4_bytes (abfd, info_ptr)));
20238 info_ptr += 4;
20239 break;
20240 case DW_FORM_ref8:
20241 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20242 + read_8_bytes (abfd, info_ptr)));
20243 info_ptr += 8;
20244 break;
20245 case DW_FORM_ref_sig8:
20246 attr->set_signature (read_8_bytes (abfd, info_ptr));
20247 info_ptr += 8;
20248 break;
20249 case DW_FORM_ref_udata:
20250 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20251 + read_unsigned_leb128 (abfd, info_ptr,
20252 &bytes_read)));
20253 info_ptr += bytes_read;
20254 break;
20255 case DW_FORM_indirect:
20256 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20257 info_ptr += bytes_read;
20258 if (form == DW_FORM_implicit_const)
20259 {
20260 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20261 info_ptr += bytes_read;
20262 }
20263 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20264 info_ptr);
20265 break;
20266 case DW_FORM_implicit_const:
20267 attr->set_signed (implicit_const);
20268 break;
20269 case DW_FORM_addrx:
20270 case DW_FORM_GNU_addr_index:
20271 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20272 &bytes_read));
20273 info_ptr += bytes_read;
20274 break;
20275 case DW_FORM_strx:
20276 case DW_FORM_strx1:
20277 case DW_FORM_strx2:
20278 case DW_FORM_strx3:
20279 case DW_FORM_strx4:
20280 case DW_FORM_GNU_str_index:
20281 {
20282 ULONGEST str_index;
20283 if (form == DW_FORM_strx1)
20284 {
20285 str_index = read_1_byte (abfd, info_ptr);
20286 info_ptr += 1;
20287 }
20288 else if (form == DW_FORM_strx2)
20289 {
20290 str_index = read_2_bytes (abfd, info_ptr);
20291 info_ptr += 2;
20292 }
20293 else if (form == DW_FORM_strx3)
20294 {
20295 str_index = read_3_bytes (abfd, info_ptr);
20296 info_ptr += 3;
20297 }
20298 else if (form == DW_FORM_strx4)
20299 {
20300 str_index = read_4_bytes (abfd, info_ptr);
20301 info_ptr += 4;
20302 }
20303 else
20304 {
20305 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20306 info_ptr += bytes_read;
20307 }
20308 attr->set_unsigned_reprocess (str_index);
20309 }
20310 break;
20311 default:
20312 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20313 dwarf_form_name (form),
20314 bfd_get_filename (abfd));
20315 }
20316
20317 /* Super hack. */
20318 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20319 attr->form = DW_FORM_GNU_ref_alt;
20320
20321 /* We have seen instances where the compiler tried to emit a byte
20322 size attribute of -1 which ended up being encoded as an unsigned
20323 0xffffffff. Although 0xffffffff is technically a valid size value,
20324 an object of this size seems pretty unlikely so we can relatively
20325 safely treat these cases as if the size attribute was invalid and
20326 treat them as zero by default. */
20327 if (attr->name == DW_AT_byte_size
20328 && form == DW_FORM_data4
20329 && attr->as_unsigned () >= 0xffffffff)
20330 {
20331 complaint
20332 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20333 hex_string (attr->as_unsigned ()));
20334 attr->set_unsigned (0);
20335 }
20336
20337 return info_ptr;
20338 }
20339
20340 /* Read an attribute described by an abbreviated attribute. */
20341
20342 static const gdb_byte *
20343 read_attribute (const struct die_reader_specs *reader,
20344 struct attribute *attr, const struct attr_abbrev *abbrev,
20345 const gdb_byte *info_ptr)
20346 {
20347 attr->name = abbrev->name;
20348 attr->string_is_canonical = 0;
20349 attr->requires_reprocessing = 0;
20350 return read_attribute_value (reader, attr, abbrev->form,
20351 abbrev->implicit_const, info_ptr);
20352 }
20353
20354 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20355
20356 static const char *
20357 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20358 LONGEST str_offset)
20359 {
20360 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20361 str_offset, "DW_FORM_strp");
20362 }
20363
20364 /* Return pointer to string at .debug_str offset as read from BUF.
20365 BUF is assumed to be in a compilation unit described by CU_HEADER.
20366 Return *BYTES_READ_PTR count of bytes read from BUF. */
20367
20368 static const char *
20369 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20370 const gdb_byte *buf,
20371 const struct comp_unit_head *cu_header,
20372 unsigned int *bytes_read_ptr)
20373 {
20374 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20375
20376 return read_indirect_string_at_offset (per_objfile, str_offset);
20377 }
20378
20379 /* See read.h. */
20380
20381 const char *
20382 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20383 unsigned int offset_size)
20384 {
20385 bfd *abfd = objfile->obfd;
20386 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
20387
20388 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20389 }
20390
20391 /* See read.h. */
20392
20393 const char *
20394 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20395 const struct comp_unit_head *cu_header,
20396 unsigned int *bytes_read_ptr)
20397 {
20398 bfd *abfd = objfile->obfd;
20399 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20400
20401 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20402 }
20403
20404 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20405 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20406 ADDR_SIZE is the size of addresses from the CU header. */
20407
20408 static CORE_ADDR
20409 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20410 gdb::optional<ULONGEST> addr_base, int addr_size)
20411 {
20412 struct objfile *objfile = per_objfile->objfile;
20413 bfd *abfd = objfile->obfd;
20414 const gdb_byte *info_ptr;
20415 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20416
20417 per_objfile->per_bfd->addr.read (objfile);
20418 if (per_objfile->per_bfd->addr.buffer == NULL)
20419 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20420 objfile_name (objfile));
20421 if (addr_base_or_zero + addr_index * addr_size
20422 >= per_objfile->per_bfd->addr.size)
20423 error (_("DW_FORM_addr_index pointing outside of "
20424 ".debug_addr section [in module %s]"),
20425 objfile_name (objfile));
20426 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20427 + addr_index * addr_size);
20428 if (addr_size == 4)
20429 return bfd_get_32 (abfd, info_ptr);
20430 else
20431 return bfd_get_64 (abfd, info_ptr);
20432 }
20433
20434 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20435
20436 static CORE_ADDR
20437 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20438 {
20439 return read_addr_index_1 (cu->per_objfile, addr_index,
20440 cu->addr_base, cu->header.addr_size);
20441 }
20442
20443 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20444
20445 static CORE_ADDR
20446 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20447 unsigned int *bytes_read)
20448 {
20449 bfd *abfd = cu->per_objfile->objfile->obfd;
20450 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20451
20452 return read_addr_index (cu, addr_index);
20453 }
20454
20455 /* See read.h. */
20456
20457 CORE_ADDR
20458 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20459 dwarf2_per_objfile *per_objfile,
20460 unsigned int addr_index)
20461 {
20462 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20463 gdb::optional<ULONGEST> addr_base;
20464 int addr_size;
20465
20466 /* We need addr_base and addr_size.
20467 If we don't have PER_CU->cu, we have to get it.
20468 Nasty, but the alternative is storing the needed info in PER_CU,
20469 which at this point doesn't seem justified: it's not clear how frequently
20470 it would get used and it would increase the size of every PER_CU.
20471 Entry points like dwarf2_per_cu_addr_size do a similar thing
20472 so we're not in uncharted territory here.
20473 Alas we need to be a bit more complicated as addr_base is contained
20474 in the DIE.
20475
20476 We don't need to read the entire CU(/TU).
20477 We just need the header and top level die.
20478
20479 IWBN to use the aging mechanism to let us lazily later discard the CU.
20480 For now we skip this optimization. */
20481
20482 if (cu != NULL)
20483 {
20484 addr_base = cu->addr_base;
20485 addr_size = cu->header.addr_size;
20486 }
20487 else
20488 {
20489 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20490 addr_base = reader.cu->addr_base;
20491 addr_size = reader.cu->header.addr_size;
20492 }
20493
20494 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20495 }
20496
20497 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20498 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20499 DWO file. */
20500
20501 static const char *
20502 read_str_index (struct dwarf2_cu *cu,
20503 struct dwarf2_section_info *str_section,
20504 struct dwarf2_section_info *str_offsets_section,
20505 ULONGEST str_offsets_base, ULONGEST str_index)
20506 {
20507 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20508 struct objfile *objfile = per_objfile->objfile;
20509 const char *objf_name = objfile_name (objfile);
20510 bfd *abfd = objfile->obfd;
20511 const gdb_byte *info_ptr;
20512 ULONGEST str_offset;
20513 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20514
20515 str_section->read (objfile);
20516 str_offsets_section->read (objfile);
20517 if (str_section->buffer == NULL)
20518 error (_("%s used without %s section"
20519 " in CU at offset %s [in module %s]"),
20520 form_name, str_section->get_name (),
20521 sect_offset_str (cu->header.sect_off), objf_name);
20522 if (str_offsets_section->buffer == NULL)
20523 error (_("%s used without %s section"
20524 " in CU at offset %s [in module %s]"),
20525 form_name, str_section->get_name (),
20526 sect_offset_str (cu->header.sect_off), objf_name);
20527 info_ptr = (str_offsets_section->buffer
20528 + str_offsets_base
20529 + str_index * cu->header.offset_size);
20530 if (cu->header.offset_size == 4)
20531 str_offset = bfd_get_32 (abfd, info_ptr);
20532 else
20533 str_offset = bfd_get_64 (abfd, info_ptr);
20534 if (str_offset >= str_section->size)
20535 error (_("Offset from %s pointing outside of"
20536 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20537 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20538 return (const char *) (str_section->buffer + str_offset);
20539 }
20540
20541 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20542
20543 static const char *
20544 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20545 {
20546 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20547 ? reader->cu->header.addr_size : 0;
20548 return read_str_index (reader->cu,
20549 &reader->dwo_file->sections.str,
20550 &reader->dwo_file->sections.str_offsets,
20551 str_offsets_base, str_index);
20552 }
20553
20554 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20555
20556 static const char *
20557 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20558 {
20559 struct objfile *objfile = cu->per_objfile->objfile;
20560 const char *objf_name = objfile_name (objfile);
20561 static const char form_name[] = "DW_FORM_GNU_str_index";
20562 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20563
20564 if (!cu->str_offsets_base.has_value ())
20565 error (_("%s used in Fission stub without %s"
20566 " in CU at offset 0x%lx [in module %s]"),
20567 form_name, str_offsets_attr_name,
20568 (long) cu->header.offset_size, objf_name);
20569
20570 return read_str_index (cu,
20571 &cu->per_objfile->per_bfd->str,
20572 &cu->per_objfile->per_bfd->str_offsets,
20573 *cu->str_offsets_base, str_index);
20574 }
20575
20576 /* Return the length of an LEB128 number in BUF. */
20577
20578 static int
20579 leb128_size (const gdb_byte *buf)
20580 {
20581 const gdb_byte *begin = buf;
20582 gdb_byte byte;
20583
20584 while (1)
20585 {
20586 byte = *buf++;
20587 if ((byte & 128) == 0)
20588 return buf - begin;
20589 }
20590 }
20591
20592 static enum language
20593 dwarf_lang_to_enum_language (unsigned int lang)
20594 {
20595 enum language language;
20596
20597 switch (lang)
20598 {
20599 case DW_LANG_C89:
20600 case DW_LANG_C99:
20601 case DW_LANG_C11:
20602 case DW_LANG_C:
20603 case DW_LANG_UPC:
20604 language = language_c;
20605 break;
20606 case DW_LANG_Java:
20607 case DW_LANG_C_plus_plus:
20608 case DW_LANG_C_plus_plus_11:
20609 case DW_LANG_C_plus_plus_14:
20610 language = language_cplus;
20611 break;
20612 case DW_LANG_D:
20613 language = language_d;
20614 break;
20615 case DW_LANG_Fortran77:
20616 case DW_LANG_Fortran90:
20617 case DW_LANG_Fortran95:
20618 case DW_LANG_Fortran03:
20619 case DW_LANG_Fortran08:
20620 language = language_fortran;
20621 break;
20622 case DW_LANG_Go:
20623 language = language_go;
20624 break;
20625 case DW_LANG_Mips_Assembler:
20626 language = language_asm;
20627 break;
20628 case DW_LANG_Ada83:
20629 case DW_LANG_Ada95:
20630 language = language_ada;
20631 break;
20632 case DW_LANG_Modula2:
20633 language = language_m2;
20634 break;
20635 case DW_LANG_Pascal83:
20636 language = language_pascal;
20637 break;
20638 case DW_LANG_ObjC:
20639 language = language_objc;
20640 break;
20641 case DW_LANG_Rust:
20642 case DW_LANG_Rust_old:
20643 language = language_rust;
20644 break;
20645 case DW_LANG_OpenCL:
20646 language = language_opencl;
20647 break;
20648 case DW_LANG_Cobol74:
20649 case DW_LANG_Cobol85:
20650 default:
20651 language = language_minimal;
20652 break;
20653 }
20654
20655 return language;
20656 }
20657
20658 /* Return the named attribute or NULL if not there. */
20659
20660 static struct attribute *
20661 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20662 {
20663 for (;;)
20664 {
20665 unsigned int i;
20666 struct attribute *spec = NULL;
20667
20668 for (i = 0; i < die->num_attrs; ++i)
20669 {
20670 if (die->attrs[i].name == name)
20671 return &die->attrs[i];
20672 if (die->attrs[i].name == DW_AT_specification
20673 || die->attrs[i].name == DW_AT_abstract_origin)
20674 spec = &die->attrs[i];
20675 }
20676
20677 if (!spec)
20678 break;
20679
20680 die = follow_die_ref (die, spec, &cu);
20681 }
20682
20683 return NULL;
20684 }
20685
20686 /* Return the string associated with a string-typed attribute, or NULL if it
20687 is either not found or is of an incorrect type. */
20688
20689 static const char *
20690 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20691 {
20692 struct attribute *attr;
20693 const char *str = NULL;
20694
20695 attr = dwarf2_attr (die, name, cu);
20696
20697 if (attr != NULL)
20698 {
20699 str = attr->as_string ();
20700 if (str == nullptr)
20701 complaint (_("string type expected for attribute %s for "
20702 "DIE at %s in module %s"),
20703 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20704 objfile_name (cu->per_objfile->objfile));
20705 }
20706
20707 return str;
20708 }
20709
20710 /* Return the dwo name or NULL if not present. If present, it is in either
20711 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20712 static const char *
20713 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20714 {
20715 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20716 if (dwo_name == nullptr)
20717 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20718 return dwo_name;
20719 }
20720
20721 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20722 and holds a non-zero value. This function should only be used for
20723 DW_FORM_flag or DW_FORM_flag_present attributes. */
20724
20725 static int
20726 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20727 {
20728 struct attribute *attr = dwarf2_attr (die, name, cu);
20729
20730 return attr != nullptr && attr->as_boolean ();
20731 }
20732
20733 static int
20734 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20735 {
20736 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20737 which value is non-zero. However, we have to be careful with
20738 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20739 (via dwarf2_flag_true_p) follows this attribute. So we may
20740 end up accidently finding a declaration attribute that belongs
20741 to a different DIE referenced by the specification attribute,
20742 even though the given DIE does not have a declaration attribute. */
20743 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20744 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20745 }
20746
20747 /* Return the die giving the specification for DIE, if there is
20748 one. *SPEC_CU is the CU containing DIE on input, and the CU
20749 containing the return value on output. If there is no
20750 specification, but there is an abstract origin, that is
20751 returned. */
20752
20753 static struct die_info *
20754 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20755 {
20756 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20757 *spec_cu);
20758
20759 if (spec_attr == NULL)
20760 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20761
20762 if (spec_attr == NULL)
20763 return NULL;
20764 else
20765 return follow_die_ref (die, spec_attr, spec_cu);
20766 }
20767
20768 /* A convenience function to find the proper .debug_line section for a CU. */
20769
20770 static struct dwarf2_section_info *
20771 get_debug_line_section (struct dwarf2_cu *cu)
20772 {
20773 struct dwarf2_section_info *section;
20774 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20775
20776 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20777 DWO file. */
20778 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20779 section = &cu->dwo_unit->dwo_file->sections.line;
20780 else if (cu->per_cu->is_dwz)
20781 {
20782 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20783
20784 section = &dwz->line;
20785 }
20786 else
20787 section = &per_objfile->per_bfd->line;
20788
20789 return section;
20790 }
20791
20792 /* Read the statement program header starting at OFFSET in
20793 .debug_line, or .debug_line.dwo. Return a pointer
20794 to a struct line_header, allocated using xmalloc.
20795 Returns NULL if there is a problem reading the header, e.g., if it
20796 has a version we don't understand.
20797
20798 NOTE: the strings in the include directory and file name tables of
20799 the returned object point into the dwarf line section buffer,
20800 and must not be freed. */
20801
20802 static line_header_up
20803 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20804 {
20805 struct dwarf2_section_info *section;
20806 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20807
20808 section = get_debug_line_section (cu);
20809 section->read (per_objfile->objfile);
20810 if (section->buffer == NULL)
20811 {
20812 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20813 complaint (_("missing .debug_line.dwo section"));
20814 else
20815 complaint (_("missing .debug_line section"));
20816 return 0;
20817 }
20818
20819 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20820 per_objfile, section, &cu->header);
20821 }
20822
20823 /* Subroutine of dwarf_decode_lines to simplify it.
20824 Return the file name for the given file_entry.
20825 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
20826 If space for the result is malloc'd, *NAME_HOLDER will be set.
20827 Returns NULL if FILE_INDEX should be ignored, i.e., it is
20828 equivalent to CU_INFO. */
20829
20830 static const char *
20831 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
20832 const file_and_directory &cu_info,
20833 gdb::unique_xmalloc_ptr<char> *name_holder)
20834 {
20835 const char *include_name = fe.name;
20836 const char *include_name_to_compare = include_name;
20837
20838 const char *dir_name = fe.include_dir (lh);
20839
20840 gdb::unique_xmalloc_ptr<char> hold_compare;
20841 if (!IS_ABSOLUTE_PATH (include_name)
20842 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
20843 {
20844 /* Avoid creating a duplicate name for CU_INFO.
20845 We do this by comparing INCLUDE_NAME and CU_INFO.
20846 Before we do the comparison, however, we need to account
20847 for DIR_NAME and COMP_DIR.
20848 First prepend dir_name (if non-NULL). If we still don't
20849 have an absolute path prepend comp_dir (if non-NULL).
20850 However, the directory we record in the include-file's
20851 psymtab does not contain COMP_DIR (to match the
20852 corresponding symtab(s)).
20853
20854 Example:
20855
20856 bash$ cd /tmp
20857 bash$ gcc -g ./hello.c
20858 include_name = "hello.c"
20859 dir_name = "."
20860 DW_AT_comp_dir = comp_dir = "/tmp"
20861 DW_AT_name = "./hello.c"
20862
20863 */
20864
20865 if (dir_name != NULL)
20866 {
20867 name_holder->reset (concat (dir_name, SLASH_STRING,
20868 include_name, (char *) NULL));
20869 include_name = name_holder->get ();
20870 include_name_to_compare = include_name;
20871 }
20872 if (!IS_ABSOLUTE_PATH (include_name)
20873 && cu_info.get_comp_dir () != nullptr)
20874 {
20875 hold_compare.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20876 include_name, (char *) NULL));
20877 include_name_to_compare = hold_compare.get ();
20878 }
20879 }
20880
20881 gdb::unique_xmalloc_ptr<char> copied_name;
20882 const char *cu_filename = cu_info.get_name ();
20883 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
20884 {
20885 copied_name.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20886 cu_filename, (char *) NULL));
20887 cu_filename = copied_name.get ();
20888 }
20889
20890 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
20891 return nullptr;
20892 return include_name;
20893 }
20894
20895 /* State machine to track the state of the line number program. */
20896
20897 class lnp_state_machine
20898 {
20899 public:
20900 /* Initialize a machine state for the start of a line number
20901 program. */
20902 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20903 bool record_lines_p);
20904
20905 file_entry *current_file ()
20906 {
20907 /* lh->file_names is 0-based, but the file name numbers in the
20908 statement program are 1-based. */
20909 return m_line_header->file_name_at (m_file);
20910 }
20911
20912 /* Record the line in the state machine. END_SEQUENCE is true if
20913 we're processing the end of a sequence. */
20914 void record_line (bool end_sequence);
20915
20916 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20917 nop-out rest of the lines in this sequence. */
20918 void check_line_address (struct dwarf2_cu *cu,
20919 const gdb_byte *line_ptr,
20920 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20921
20922 void handle_set_discriminator (unsigned int discriminator)
20923 {
20924 m_discriminator = discriminator;
20925 m_line_has_non_zero_discriminator |= discriminator != 0;
20926 }
20927
20928 /* Handle DW_LNE_set_address. */
20929 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20930 {
20931 m_op_index = 0;
20932 address += baseaddr;
20933 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20934 }
20935
20936 /* Handle DW_LNS_advance_pc. */
20937 void handle_advance_pc (CORE_ADDR adjust);
20938
20939 /* Handle a special opcode. */
20940 void handle_special_opcode (unsigned char op_code);
20941
20942 /* Handle DW_LNS_advance_line. */
20943 void handle_advance_line (int line_delta)
20944 {
20945 advance_line (line_delta);
20946 }
20947
20948 /* Handle DW_LNS_set_file. */
20949 void handle_set_file (file_name_index file);
20950
20951 /* Handle DW_LNS_negate_stmt. */
20952 void handle_negate_stmt ()
20953 {
20954 m_flags ^= LEF_IS_STMT;
20955 }
20956
20957 /* Handle DW_LNS_const_add_pc. */
20958 void handle_const_add_pc ();
20959
20960 /* Handle DW_LNS_fixed_advance_pc. */
20961 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20962 {
20963 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20964 m_op_index = 0;
20965 }
20966
20967 /* Handle DW_LNS_copy. */
20968 void handle_copy ()
20969 {
20970 record_line (false);
20971 m_discriminator = 0;
20972 m_flags &= ~LEF_PROLOGUE_END;
20973 }
20974
20975 /* Handle DW_LNE_end_sequence. */
20976 void handle_end_sequence ()
20977 {
20978 m_currently_recording_lines = true;
20979 }
20980
20981 /* Handle DW_LNS_set_prologue_end. */
20982 void handle_set_prologue_end ()
20983 {
20984 m_flags |= LEF_PROLOGUE_END;
20985 }
20986
20987 private:
20988 /* Advance the line by LINE_DELTA. */
20989 void advance_line (int line_delta)
20990 {
20991 m_line += line_delta;
20992
20993 if (line_delta != 0)
20994 m_line_has_non_zero_discriminator = m_discriminator != 0;
20995 }
20996
20997 struct dwarf2_cu *m_cu;
20998
20999 gdbarch *m_gdbarch;
21000
21001 /* True if we're recording lines.
21002 Otherwise we're building partial symtabs and are just interested in
21003 finding include files mentioned by the line number program. */
21004 bool m_record_lines_p;
21005
21006 /* The line number header. */
21007 line_header *m_line_header;
21008
21009 /* These are part of the standard DWARF line number state machine,
21010 and initialized according to the DWARF spec. */
21011
21012 unsigned char m_op_index = 0;
21013 /* The line table index of the current file. */
21014 file_name_index m_file = 1;
21015 unsigned int m_line = 1;
21016
21017 /* These are initialized in the constructor. */
21018
21019 CORE_ADDR m_address;
21020 linetable_entry_flags m_flags;
21021 unsigned int m_discriminator;
21022
21023 /* Additional bits of state we need to track. */
21024
21025 /* The last file that we called dwarf2_start_subfile for.
21026 This is only used for TLLs. */
21027 unsigned int m_last_file = 0;
21028 /* The last file a line number was recorded for. */
21029 struct subfile *m_last_subfile = NULL;
21030
21031 /* The address of the last line entry. */
21032 CORE_ADDR m_last_address;
21033
21034 /* Set to true when a previous line at the same address (using
21035 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
21036 when a line entry at a new address (m_address different to
21037 m_last_address) is processed. */
21038 bool m_stmt_at_address = false;
21039
21040 /* When true, record the lines we decode. */
21041 bool m_currently_recording_lines = false;
21042
21043 /* The last line number that was recorded, used to coalesce
21044 consecutive entries for the same line. This can happen, for
21045 example, when discriminators are present. PR 17276. */
21046 unsigned int m_last_line = 0;
21047 bool m_line_has_non_zero_discriminator = false;
21048 };
21049
21050 void
21051 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21052 {
21053 CORE_ADDR addr_adj = (((m_op_index + adjust)
21054 / m_line_header->maximum_ops_per_instruction)
21055 * m_line_header->minimum_instruction_length);
21056 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21057 m_op_index = ((m_op_index + adjust)
21058 % m_line_header->maximum_ops_per_instruction);
21059 }
21060
21061 void
21062 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21063 {
21064 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21065 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21066 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21067 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21068 / m_line_header->maximum_ops_per_instruction)
21069 * m_line_header->minimum_instruction_length);
21070 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21071 m_op_index = ((m_op_index + adj_opcode_d)
21072 % m_line_header->maximum_ops_per_instruction);
21073
21074 int line_delta = m_line_header->line_base + adj_opcode_r;
21075 advance_line (line_delta);
21076 record_line (false);
21077 m_discriminator = 0;
21078 m_flags &= ~LEF_PROLOGUE_END;
21079 }
21080
21081 void
21082 lnp_state_machine::handle_set_file (file_name_index file)
21083 {
21084 m_file = file;
21085
21086 const file_entry *fe = current_file ();
21087 if (fe == NULL)
21088 dwarf2_debug_line_missing_file_complaint ();
21089 else if (m_record_lines_p)
21090 {
21091 const char *dir = fe->include_dir (m_line_header);
21092
21093 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21094 m_line_has_non_zero_discriminator = m_discriminator != 0;
21095 dwarf2_start_subfile (m_cu, fe->name, dir);
21096 }
21097 }
21098
21099 void
21100 lnp_state_machine::handle_const_add_pc ()
21101 {
21102 CORE_ADDR adjust
21103 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21104
21105 CORE_ADDR addr_adj
21106 = (((m_op_index + adjust)
21107 / m_line_header->maximum_ops_per_instruction)
21108 * m_line_header->minimum_instruction_length);
21109
21110 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21111 m_op_index = ((m_op_index + adjust)
21112 % m_line_header->maximum_ops_per_instruction);
21113 }
21114
21115 /* Return non-zero if we should add LINE to the line number table.
21116 LINE is the line to add, LAST_LINE is the last line that was added,
21117 LAST_SUBFILE is the subfile for LAST_LINE.
21118 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21119 had a non-zero discriminator.
21120
21121 We have to be careful in the presence of discriminators.
21122 E.g., for this line:
21123
21124 for (i = 0; i < 100000; i++);
21125
21126 clang can emit four line number entries for that one line,
21127 each with a different discriminator.
21128 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21129
21130 However, we want gdb to coalesce all four entries into one.
21131 Otherwise the user could stepi into the middle of the line and
21132 gdb would get confused about whether the pc really was in the
21133 middle of the line.
21134
21135 Things are further complicated by the fact that two consecutive
21136 line number entries for the same line is a heuristic used by gcc
21137 to denote the end of the prologue. So we can't just discard duplicate
21138 entries, we have to be selective about it. The heuristic we use is
21139 that we only collapse consecutive entries for the same line if at least
21140 one of those entries has a non-zero discriminator. PR 17276.
21141
21142 Note: Addresses in the line number state machine can never go backwards
21143 within one sequence, thus this coalescing is ok. */
21144
21145 static int
21146 dwarf_record_line_p (struct dwarf2_cu *cu,
21147 unsigned int line, unsigned int last_line,
21148 int line_has_non_zero_discriminator,
21149 struct subfile *last_subfile)
21150 {
21151 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21152 return 1;
21153 if (line != last_line)
21154 return 1;
21155 /* Same line for the same file that we've seen already.
21156 As a last check, for pr 17276, only record the line if the line
21157 has never had a non-zero discriminator. */
21158 if (!line_has_non_zero_discriminator)
21159 return 1;
21160 return 0;
21161 }
21162
21163 /* Use the CU's builder to record line number LINE beginning at
21164 address ADDRESS in the line table of subfile SUBFILE. */
21165
21166 static void
21167 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21168 unsigned int line, CORE_ADDR address,
21169 linetable_entry_flags flags,
21170 struct dwarf2_cu *cu)
21171 {
21172 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21173
21174 if (dwarf_line_debug)
21175 {
21176 gdb_printf (gdb_stdlog,
21177 "Recording line %u, file %s, address %s\n",
21178 line, lbasename (subfile->name),
21179 paddress (gdbarch, address));
21180 }
21181
21182 if (cu != nullptr)
21183 cu->get_builder ()->record_line (subfile, line, addr, flags);
21184 }
21185
21186 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21187 Mark the end of a set of line number records.
21188 The arguments are the same as for dwarf_record_line_1.
21189 If SUBFILE is NULL the request is ignored. */
21190
21191 static void
21192 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21193 CORE_ADDR address, struct dwarf2_cu *cu)
21194 {
21195 if (subfile == NULL)
21196 return;
21197
21198 if (dwarf_line_debug)
21199 {
21200 gdb_printf (gdb_stdlog,
21201 "Finishing current line, file %s, address %s\n",
21202 lbasename (subfile->name),
21203 paddress (gdbarch, address));
21204 }
21205
21206 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
21207 }
21208
21209 void
21210 lnp_state_machine::record_line (bool end_sequence)
21211 {
21212 if (dwarf_line_debug)
21213 {
21214 gdb_printf (gdb_stdlog,
21215 "Processing actual line %u: file %u,"
21216 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
21217 m_line, m_file,
21218 paddress (m_gdbarch, m_address),
21219 (m_flags & LEF_IS_STMT) != 0,
21220 (m_flags & LEF_PROLOGUE_END) != 0,
21221 m_discriminator,
21222 (end_sequence ? "\t(end sequence)" : ""));
21223 }
21224
21225 file_entry *fe = current_file ();
21226
21227 if (fe == NULL)
21228 dwarf2_debug_line_missing_file_complaint ();
21229 /* For now we ignore lines not starting on an instruction boundary.
21230 But not when processing end_sequence for compatibility with the
21231 previous version of the code. */
21232 else if (m_op_index == 0 || end_sequence)
21233 {
21234 fe->included_p = true;
21235 if (m_record_lines_p)
21236 {
21237 /* When we switch files we insert an end maker in the first file,
21238 switch to the second file and add a new line entry. The
21239 problem is that the end marker inserted in the first file will
21240 discard any previous line entries at the same address. If the
21241 line entries in the first file are marked as is-stmt, while
21242 the new line in the second file is non-stmt, then this means
21243 the end marker will discard is-stmt lines so we can have a
21244 non-stmt line. This means that there are less addresses at
21245 which the user can insert a breakpoint.
21246
21247 To improve this we track the last address in m_last_address,
21248 and whether we have seen an is-stmt at this address. Then
21249 when switching files, if we have seen a stmt at the current
21250 address, and we are switching to create a non-stmt line, then
21251 discard the new line. */
21252 bool file_changed
21253 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21254 bool ignore_this_line
21255 = ((file_changed && !end_sequence && m_last_address == m_address
21256 && ((m_flags & LEF_IS_STMT) == 0)
21257 && m_stmt_at_address)
21258 || (!end_sequence && m_line == 0));
21259
21260 if ((file_changed && !ignore_this_line) || end_sequence)
21261 {
21262 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21263 m_currently_recording_lines ? m_cu : nullptr);
21264 }
21265
21266 if (!end_sequence && !ignore_this_line)
21267 {
21268 linetable_entry_flags lte_flags = m_flags;
21269 if (producer_is_codewarrior (m_cu))
21270 lte_flags |= LEF_IS_STMT;
21271
21272 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21273 m_line_has_non_zero_discriminator,
21274 m_last_subfile))
21275 {
21276 buildsym_compunit *builder = m_cu->get_builder ();
21277 dwarf_record_line_1 (m_gdbarch,
21278 builder->get_current_subfile (),
21279 m_line, m_address, lte_flags,
21280 m_currently_recording_lines ? m_cu : nullptr);
21281 }
21282 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21283 m_last_line = m_line;
21284 }
21285 }
21286 }
21287
21288 /* Track whether we have seen any IS_STMT true at m_address in case we
21289 have multiple line table entries all at m_address. */
21290 if (m_last_address != m_address)
21291 {
21292 m_stmt_at_address = false;
21293 m_last_address = m_address;
21294 }
21295 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
21296 }
21297
21298 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21299 line_header *lh, bool record_lines_p)
21300 {
21301 m_cu = cu;
21302 m_gdbarch = arch;
21303 m_record_lines_p = record_lines_p;
21304 m_line_header = lh;
21305
21306 m_currently_recording_lines = true;
21307
21308 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21309 was a line entry for it so that the backend has a chance to adjust it
21310 and also record it in case it needs it. This is currently used by MIPS
21311 code, cf. `mips_adjust_dwarf2_line'. */
21312 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21313 m_flags = 0;
21314 if (lh->default_is_stmt)
21315 m_flags |= LEF_IS_STMT;
21316 m_discriminator = 0;
21317
21318 m_last_address = m_address;
21319 m_stmt_at_address = false;
21320 }
21321
21322 void
21323 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21324 const gdb_byte *line_ptr,
21325 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21326 {
21327 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21328 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21329 located at 0x0. In this case, additionally check that if
21330 ADDRESS < UNRELOCATED_LOWPC. */
21331
21332 if ((address == 0 && address < unrelocated_lowpc)
21333 || address == (CORE_ADDR) -1)
21334 {
21335 /* This line table is for a function which has been
21336 GCd by the linker. Ignore it. PR gdb/12528 */
21337
21338 struct objfile *objfile = cu->per_objfile->objfile;
21339 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21340
21341 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21342 line_offset, objfile_name (objfile));
21343 m_currently_recording_lines = false;
21344 /* Note: m_currently_recording_lines is left as false until we see
21345 DW_LNE_end_sequence. */
21346 }
21347 }
21348
21349 /* Subroutine of dwarf_decode_lines to simplify it.
21350 Process the line number information in LH.
21351 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21352 program in order to set included_p for every referenced header. */
21353
21354 static void
21355 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21356 const int decode_for_pst_p, CORE_ADDR lowpc)
21357 {
21358 const gdb_byte *line_ptr, *extended_end;
21359 const gdb_byte *line_end;
21360 unsigned int bytes_read, extended_len;
21361 unsigned char op_code, extended_op;
21362 CORE_ADDR baseaddr;
21363 struct objfile *objfile = cu->per_objfile->objfile;
21364 bfd *abfd = objfile->obfd;
21365 struct gdbarch *gdbarch = objfile->arch ();
21366 /* True if we're recording line info (as opposed to building partial
21367 symtabs and just interested in finding include files mentioned by
21368 the line number program). */
21369 bool record_lines_p = !decode_for_pst_p;
21370
21371 baseaddr = objfile->text_section_offset ();
21372
21373 line_ptr = lh->statement_program_start;
21374 line_end = lh->statement_program_end;
21375
21376 /* Read the statement sequences until there's nothing left. */
21377 while (line_ptr < line_end)
21378 {
21379 /* The DWARF line number program state machine. Reset the state
21380 machine at the start of each sequence. */
21381 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21382 bool end_sequence = false;
21383
21384 if (record_lines_p)
21385 {
21386 /* Start a subfile for the current file of the state
21387 machine. */
21388 const file_entry *fe = state_machine.current_file ();
21389
21390 if (fe != NULL)
21391 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21392 }
21393
21394 /* Decode the table. */
21395 while (line_ptr < line_end && !end_sequence)
21396 {
21397 op_code = read_1_byte (abfd, line_ptr);
21398 line_ptr += 1;
21399
21400 if (op_code >= lh->opcode_base)
21401 {
21402 /* Special opcode. */
21403 state_machine.handle_special_opcode (op_code);
21404 }
21405 else switch (op_code)
21406 {
21407 case DW_LNS_extended_op:
21408 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21409 &bytes_read);
21410 line_ptr += bytes_read;
21411 extended_end = line_ptr + extended_len;
21412 extended_op = read_1_byte (abfd, line_ptr);
21413 line_ptr += 1;
21414 if (DW_LNE_lo_user <= extended_op
21415 && extended_op <= DW_LNE_hi_user)
21416 {
21417 /* Vendor extension, ignore. */
21418 line_ptr = extended_end;
21419 break;
21420 }
21421 switch (extended_op)
21422 {
21423 case DW_LNE_end_sequence:
21424 state_machine.handle_end_sequence ();
21425 end_sequence = true;
21426 break;
21427 case DW_LNE_set_address:
21428 {
21429 CORE_ADDR address
21430 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21431 line_ptr += bytes_read;
21432
21433 state_machine.check_line_address (cu, line_ptr,
21434 lowpc - baseaddr, address);
21435 state_machine.handle_set_address (baseaddr, address);
21436 }
21437 break;
21438 case DW_LNE_define_file:
21439 {
21440 const char *cur_file;
21441 unsigned int mod_time, length;
21442 dir_index dindex;
21443
21444 cur_file = read_direct_string (abfd, line_ptr,
21445 &bytes_read);
21446 line_ptr += bytes_read;
21447 dindex = (dir_index)
21448 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21449 line_ptr += bytes_read;
21450 mod_time =
21451 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21452 line_ptr += bytes_read;
21453 length =
21454 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21455 line_ptr += bytes_read;
21456 lh->add_file_name (cur_file, dindex, mod_time, length);
21457 }
21458 break;
21459 case DW_LNE_set_discriminator:
21460 {
21461 /* The discriminator is not interesting to the
21462 debugger; just ignore it. We still need to
21463 check its value though:
21464 if there are consecutive entries for the same
21465 (non-prologue) line we want to coalesce them.
21466 PR 17276. */
21467 unsigned int discr
21468 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21469 line_ptr += bytes_read;
21470
21471 state_machine.handle_set_discriminator (discr);
21472 }
21473 break;
21474 default:
21475 complaint (_("mangled .debug_line section"));
21476 return;
21477 }
21478 /* Make sure that we parsed the extended op correctly. If e.g.
21479 we expected a different address size than the producer used,
21480 we may have read the wrong number of bytes. */
21481 if (line_ptr != extended_end)
21482 {
21483 complaint (_("mangled .debug_line section"));
21484 return;
21485 }
21486 break;
21487 case DW_LNS_copy:
21488 state_machine.handle_copy ();
21489 break;
21490 case DW_LNS_advance_pc:
21491 {
21492 CORE_ADDR adjust
21493 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21494 line_ptr += bytes_read;
21495
21496 state_machine.handle_advance_pc (adjust);
21497 }
21498 break;
21499 case DW_LNS_advance_line:
21500 {
21501 int line_delta
21502 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21503 line_ptr += bytes_read;
21504
21505 state_machine.handle_advance_line (line_delta);
21506 }
21507 break;
21508 case DW_LNS_set_file:
21509 {
21510 file_name_index file
21511 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21512 &bytes_read);
21513 line_ptr += bytes_read;
21514
21515 state_machine.handle_set_file (file);
21516 }
21517 break;
21518 case DW_LNS_set_column:
21519 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21520 line_ptr += bytes_read;
21521 break;
21522 case DW_LNS_negate_stmt:
21523 state_machine.handle_negate_stmt ();
21524 break;
21525 case DW_LNS_set_basic_block:
21526 break;
21527 /* Add to the address register of the state machine the
21528 address increment value corresponding to special opcode
21529 255. I.e., this value is scaled by the minimum
21530 instruction length since special opcode 255 would have
21531 scaled the increment. */
21532 case DW_LNS_const_add_pc:
21533 state_machine.handle_const_add_pc ();
21534 break;
21535 case DW_LNS_fixed_advance_pc:
21536 {
21537 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21538 line_ptr += 2;
21539
21540 state_machine.handle_fixed_advance_pc (addr_adj);
21541 }
21542 break;
21543 case DW_LNS_set_prologue_end:
21544 state_machine.handle_set_prologue_end ();
21545 break;
21546 default:
21547 {
21548 /* Unknown standard opcode, ignore it. */
21549 int i;
21550
21551 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21552 {
21553 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21554 line_ptr += bytes_read;
21555 }
21556 }
21557 }
21558 }
21559
21560 if (!end_sequence)
21561 dwarf2_debug_line_missing_end_sequence_complaint ();
21562
21563 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21564 in which case we still finish recording the last line). */
21565 state_machine.record_line (true);
21566 }
21567 }
21568
21569 /* Decode the Line Number Program (LNP) for the given line_header
21570 structure and CU. The actual information extracted and the type
21571 of structures created from the LNP depends on the value of PST.
21572
21573 1. If PST is NULL, then this procedure uses the data from the program
21574 to create all necessary symbol tables, and their linetables.
21575
21576 2. If PST is not NULL, this procedure reads the program to determine
21577 the list of files included by the unit represented by PST, and
21578 builds all the associated partial symbol tables.
21579
21580 FND holds the CU file name and directory, if known.
21581 It is used for relative paths in the line table.
21582
21583 NOTE: It is important that psymtabs have the same file name (via
21584 strcmp) as the corresponding symtab. Since the directory is not
21585 used in the name of the symtab we don't use it in the name of the
21586 psymtabs we create. E.g. expand_line_sal requires this when
21587 finding psymtabs to expand. A good testcase for this is
21588 mb-inline.exp.
21589
21590 LOWPC is the lowest address in CU (or 0 if not known).
21591
21592 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21593 for its PC<->lines mapping information. Otherwise only the filename
21594 table is read in. */
21595
21596 static void
21597 dwarf_decode_lines (struct line_header *lh, const file_and_directory &fnd,
21598 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21599 CORE_ADDR lowpc, int decode_mapping)
21600 {
21601 struct objfile *objfile = cu->per_objfile->objfile;
21602 const int decode_for_pst_p = (pst != NULL);
21603
21604 if (decode_mapping)
21605 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21606
21607 if (decode_for_pst_p)
21608 {
21609 /* Now that we're done scanning the Line Header Program, we can
21610 create the psymtab of each included file. */
21611 for (auto &file_entry : lh->file_names ())
21612 if (file_entry.included_p)
21613 {
21614 gdb::unique_xmalloc_ptr<char> name_holder;
21615 const char *include_name =
21616 compute_include_file_name (lh, file_entry, fnd, &name_holder);
21617 if (include_name != NULL)
21618 dwarf2_create_include_psymtab
21619 (cu->per_objfile->per_bfd, include_name, pst,
21620 cu->per_objfile->per_bfd->partial_symtabs.get (),
21621 objfile->per_bfd);
21622 }
21623 }
21624 else
21625 {
21626 /* Make sure a symtab is created for every file, even files
21627 which contain only variables (i.e. no code with associated
21628 line numbers). */
21629 buildsym_compunit *builder = cu->get_builder ();
21630 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21631
21632 for (auto &fe : lh->file_names ())
21633 {
21634 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21635 if (builder->get_current_subfile ()->symtab == NULL)
21636 {
21637 builder->get_current_subfile ()->symtab
21638 = allocate_symtab (cust,
21639 builder->get_current_subfile ()->name);
21640 }
21641 fe.symtab = builder->get_current_subfile ()->symtab;
21642 }
21643 }
21644 }
21645
21646 /* Start a subfile for DWARF. FILENAME is the name of the file and
21647 DIRNAME the name of the source directory which contains FILENAME
21648 or NULL if not known.
21649 This routine tries to keep line numbers from identical absolute and
21650 relative file names in a common subfile.
21651
21652 Using the `list' example from the GDB testsuite, which resides in
21653 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21654 of /srcdir/list0.c yields the following debugging information for list0.c:
21655
21656 DW_AT_name: /srcdir/list0.c
21657 DW_AT_comp_dir: /compdir
21658 files.files[0].name: list0.h
21659 files.files[0].dir: /srcdir
21660 files.files[1].name: list0.c
21661 files.files[1].dir: /srcdir
21662
21663 The line number information for list0.c has to end up in a single
21664 subfile, so that `break /srcdir/list0.c:1' works as expected.
21665 start_subfile will ensure that this happens provided that we pass the
21666 concatenation of files.files[1].dir and files.files[1].name as the
21667 subfile's name. */
21668
21669 static void
21670 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21671 const char *dirname)
21672 {
21673 gdb::unique_xmalloc_ptr<char> copy;
21674
21675 /* In order not to lose the line information directory,
21676 we concatenate it to the filename when it makes sense.
21677 Note that the Dwarf3 standard says (speaking of filenames in line
21678 information): ``The directory index is ignored for file names
21679 that represent full path names''. Thus ignoring dirname in the
21680 `else' branch below isn't an issue. */
21681
21682 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21683 {
21684 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21685 filename = copy.get ();
21686 }
21687
21688 cu->get_builder ()->start_subfile (filename);
21689 }
21690
21691 static void
21692 var_decode_location (struct attribute *attr, struct symbol *sym,
21693 struct dwarf2_cu *cu)
21694 {
21695 struct objfile *objfile = cu->per_objfile->objfile;
21696 struct comp_unit_head *cu_header = &cu->header;
21697
21698 /* NOTE drow/2003-01-30: There used to be a comment and some special
21699 code here to turn a symbol with DW_AT_external and a
21700 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21701 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21702 with some versions of binutils) where shared libraries could have
21703 relocations against symbols in their debug information - the
21704 minimal symbol would have the right address, but the debug info
21705 would not. It's no longer necessary, because we will explicitly
21706 apply relocations when we read in the debug information now. */
21707
21708 /* A DW_AT_location attribute with no contents indicates that a
21709 variable has been optimized away. */
21710 if (attr->form_is_block () && attr->as_block ()->size == 0)
21711 {
21712 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21713 return;
21714 }
21715
21716 /* Handle one degenerate form of location expression specially, to
21717 preserve GDB's previous behavior when section offsets are
21718 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21719 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21720
21721 if (attr->form_is_block ())
21722 {
21723 struct dwarf_block *block = attr->as_block ();
21724
21725 if ((block->data[0] == DW_OP_addr
21726 && block->size == 1 + cu_header->addr_size)
21727 || ((block->data[0] == DW_OP_GNU_addr_index
21728 || block->data[0] == DW_OP_addrx)
21729 && (block->size
21730 == 1 + leb128_size (&block->data[1]))))
21731 {
21732 unsigned int dummy;
21733
21734 if (block->data[0] == DW_OP_addr)
21735 sym->set_value_address
21736 (cu->header.read_address (objfile->obfd, block->data + 1,
21737 &dummy));
21738 else
21739 sym->set_value_address
21740 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
21741 sym->set_aclass_index (LOC_STATIC);
21742 fixup_symbol_section (sym, objfile);
21743 sym->set_value_address
21744 (sym->value_address ()
21745 + objfile->section_offsets[sym->section_index ()]);
21746 return;
21747 }
21748 }
21749
21750 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21751 expression evaluator, and use LOC_COMPUTED only when necessary
21752 (i.e. when the value of a register or memory location is
21753 referenced, or a thread-local block, etc.). Then again, it might
21754 not be worthwhile. I'm assuming that it isn't unless performance
21755 or memory numbers show me otherwise. */
21756
21757 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21758
21759 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21760 cu->has_loclist = true;
21761 }
21762
21763 /* Given a pointer to a DWARF information entry, figure out if we need
21764 to make a symbol table entry for it, and if so, create a new entry
21765 and return a pointer to it.
21766 If TYPE is NULL, determine symbol type from the die, otherwise
21767 used the passed type.
21768 If SPACE is not NULL, use it to hold the new symbol. If it is
21769 NULL, allocate a new symbol on the objfile's obstack. */
21770
21771 static struct symbol *
21772 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21773 struct symbol *space)
21774 {
21775 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21776 struct objfile *objfile = per_objfile->objfile;
21777 struct gdbarch *gdbarch = objfile->arch ();
21778 struct symbol *sym = NULL;
21779 const char *name;
21780 struct attribute *attr = NULL;
21781 struct attribute *attr2 = NULL;
21782 CORE_ADDR baseaddr;
21783 struct pending **list_to_add = NULL;
21784
21785 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21786
21787 baseaddr = objfile->text_section_offset ();
21788
21789 name = dwarf2_name (die, cu);
21790 if (name)
21791 {
21792 int suppress_add = 0;
21793
21794 if (space)
21795 sym = space;
21796 else
21797 sym = new (&objfile->objfile_obstack) symbol;
21798 OBJSTAT (objfile, n_syms++);
21799
21800 /* Cache this symbol's name and the name's demangled form (if any). */
21801 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
21802 /* Fortran does not have mangling standard and the mangling does differ
21803 between gfortran, iFort etc. */
21804 const char *physname
21805 = (cu->per_cu->lang == language_fortran
21806 ? dwarf2_full_name (name, die, cu)
21807 : dwarf2_physname (name, die, cu));
21808 const char *linkagename = dw2_linkage_name (die, cu);
21809
21810 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
21811 sym->set_linkage_name (physname);
21812 else
21813 {
21814 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21815 sym->set_linkage_name (linkagename);
21816 }
21817
21818 /* Handle DW_AT_artificial. */
21819 attr = dwarf2_attr (die, DW_AT_artificial, cu);
21820 if (attr != nullptr)
21821 sym->artificial = attr->as_boolean ();
21822
21823 /* Default assumptions.
21824 Use the passed type or decode it from the die. */
21825 sym->set_domain (VAR_DOMAIN);
21826 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21827 if (type != NULL)
21828 sym->set_type (type);
21829 else
21830 sym->set_type (die_type (die, cu));
21831 attr = dwarf2_attr (die,
21832 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21833 cu);
21834 if (attr != nullptr)
21835 sym->set_line (attr->constant_value (0));
21836
21837 attr = dwarf2_attr (die,
21838 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21839 cu);
21840 if (attr != nullptr && attr->is_nonnegative ())
21841 {
21842 file_name_index file_index
21843 = (file_name_index) attr->as_nonnegative ();
21844 struct file_entry *fe;
21845
21846 if (cu->line_header != NULL)
21847 fe = cu->line_header->file_name_at (file_index);
21848 else
21849 fe = NULL;
21850
21851 if (fe == NULL)
21852 complaint (_("file index out of range"));
21853 else
21854 symbol_set_symtab (sym, fe->symtab);
21855 }
21856
21857 switch (die->tag)
21858 {
21859 case DW_TAG_label:
21860 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21861 if (attr != nullptr)
21862 {
21863 CORE_ADDR addr;
21864
21865 addr = attr->as_address ();
21866 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21867 sym->set_value_address (addr);
21868 sym->set_aclass_index (LOC_LABEL);
21869 }
21870 else
21871 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21872 sym->set_type (objfile_type (objfile)->builtin_core_addr);
21873 sym->set_domain (LABEL_DOMAIN);
21874 add_symbol_to_list (sym, cu->list_in_scope);
21875 break;
21876 case DW_TAG_subprogram:
21877 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21878 finish_block. */
21879 sym->set_aclass_index (LOC_BLOCK);
21880 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21881 if ((attr2 != nullptr && attr2->as_boolean ())
21882 || cu->per_cu->lang == language_ada
21883 || cu->per_cu->lang == language_fortran)
21884 {
21885 /* Subprograms marked external are stored as a global symbol.
21886 Ada and Fortran subprograms, whether marked external or
21887 not, are always stored as a global symbol, because we want
21888 to be able to access them globally. For instance, we want
21889 to be able to break on a nested subprogram without having
21890 to specify the context. */
21891 list_to_add = cu->get_builder ()->get_global_symbols ();
21892 }
21893 else
21894 {
21895 list_to_add = cu->list_in_scope;
21896 }
21897 break;
21898 case DW_TAG_inlined_subroutine:
21899 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21900 finish_block. */
21901 sym->set_aclass_index (LOC_BLOCK);
21902 sym->set_is_inlined (1);
21903 list_to_add = cu->list_in_scope;
21904 break;
21905 case DW_TAG_template_value_param:
21906 suppress_add = 1;
21907 /* Fall through. */
21908 case DW_TAG_constant:
21909 case DW_TAG_variable:
21910 case DW_TAG_member:
21911 /* Compilation with minimal debug info may result in
21912 variables with missing type entries. Change the
21913 misleading `void' type to something sensible. */
21914 if (sym->type ()->code () == TYPE_CODE_VOID)
21915 sym->set_type (objfile_type (objfile)->builtin_int);
21916
21917 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21918 /* In the case of DW_TAG_member, we should only be called for
21919 static const members. */
21920 if (die->tag == DW_TAG_member)
21921 {
21922 /* dwarf2_add_field uses die_is_declaration,
21923 so we do the same. */
21924 gdb_assert (die_is_declaration (die, cu));
21925 gdb_assert (attr);
21926 }
21927 if (attr != nullptr)
21928 {
21929 dwarf2_const_value (attr, sym, cu);
21930 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21931 if (!suppress_add)
21932 {
21933 if (attr2 != nullptr && attr2->as_boolean ())
21934 list_to_add = cu->get_builder ()->get_global_symbols ();
21935 else
21936 list_to_add = cu->list_in_scope;
21937 }
21938 break;
21939 }
21940 attr = dwarf2_attr (die, DW_AT_location, cu);
21941 if (attr != nullptr)
21942 {
21943 var_decode_location (attr, sym, cu);
21944 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21945
21946 /* Fortran explicitly imports any global symbols to the local
21947 scope by DW_TAG_common_block. */
21948 if (cu->per_cu->lang == language_fortran && die->parent
21949 && die->parent->tag == DW_TAG_common_block)
21950 attr2 = NULL;
21951
21952 if (sym->aclass () == LOC_STATIC
21953 && sym->value_address () == 0
21954 && !per_objfile->per_bfd->has_section_at_zero)
21955 {
21956 /* When a static variable is eliminated by the linker,
21957 the corresponding debug information is not stripped
21958 out, but the variable address is set to null;
21959 do not add such variables into symbol table. */
21960 }
21961 else if (attr2 != nullptr && attr2->as_boolean ())
21962 {
21963 if (sym->aclass () == LOC_STATIC
21964 && (objfile->flags & OBJF_MAINLINE) == 0
21965 && per_objfile->per_bfd->can_copy)
21966 {
21967 /* A global static variable might be subject to
21968 copy relocation. We first check for a local
21969 minsym, though, because maybe the symbol was
21970 marked hidden, in which case this would not
21971 apply. */
21972 bound_minimal_symbol found
21973 = (lookup_minimal_symbol_linkage
21974 (sym->linkage_name (), objfile));
21975 if (found.minsym != nullptr)
21976 sym->maybe_copied = 1;
21977 }
21978
21979 /* A variable with DW_AT_external is never static,
21980 but it may be block-scoped. */
21981 list_to_add
21982 = ((cu->list_in_scope
21983 == cu->get_builder ()->get_file_symbols ())
21984 ? cu->get_builder ()->get_global_symbols ()
21985 : cu->list_in_scope);
21986 }
21987 else
21988 list_to_add = cu->list_in_scope;
21989 }
21990 else
21991 {
21992 /* We do not know the address of this symbol.
21993 If it is an external symbol and we have type information
21994 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21995 The address of the variable will then be determined from
21996 the minimal symbol table whenever the variable is
21997 referenced. */
21998 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21999
22000 /* Fortran explicitly imports any global symbols to the local
22001 scope by DW_TAG_common_block. */
22002 if (cu->per_cu->lang == language_fortran && die->parent
22003 && die->parent->tag == DW_TAG_common_block)
22004 {
22005 /* SYMBOL_CLASS doesn't matter here because
22006 read_common_block is going to reset it. */
22007 if (!suppress_add)
22008 list_to_add = cu->list_in_scope;
22009 }
22010 else if (attr2 != nullptr && attr2->as_boolean ()
22011 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22012 {
22013 /* A variable with DW_AT_external is never static, but it
22014 may be block-scoped. */
22015 list_to_add
22016 = ((cu->list_in_scope
22017 == cu->get_builder ()->get_file_symbols ())
22018 ? cu->get_builder ()->get_global_symbols ()
22019 : cu->list_in_scope);
22020
22021 sym->set_aclass_index (LOC_UNRESOLVED);
22022 }
22023 else if (!die_is_declaration (die, cu))
22024 {
22025 /* Use the default LOC_OPTIMIZED_OUT class. */
22026 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
22027 if (!suppress_add)
22028 list_to_add = cu->list_in_scope;
22029 }
22030 }
22031 break;
22032 case DW_TAG_formal_parameter:
22033 {
22034 /* If we are inside a function, mark this as an argument. If
22035 not, we might be looking at an argument to an inlined function
22036 when we do not have enough information to show inlined frames;
22037 pretend it's a local variable in that case so that the user can
22038 still see it. */
22039 struct context_stack *curr
22040 = cu->get_builder ()->get_current_context_stack ();
22041 if (curr != nullptr && curr->name != nullptr)
22042 sym->set_is_argument (1);
22043 attr = dwarf2_attr (die, DW_AT_location, cu);
22044 if (attr != nullptr)
22045 {
22046 var_decode_location (attr, sym, cu);
22047 }
22048 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22049 if (attr != nullptr)
22050 {
22051 dwarf2_const_value (attr, sym, cu);
22052 }
22053
22054 list_to_add = cu->list_in_scope;
22055 }
22056 break;
22057 case DW_TAG_unspecified_parameters:
22058 /* From varargs functions; gdb doesn't seem to have any
22059 interest in this information, so just ignore it for now.
22060 (FIXME?) */
22061 break;
22062 case DW_TAG_template_type_param:
22063 suppress_add = 1;
22064 /* Fall through. */
22065 case DW_TAG_class_type:
22066 case DW_TAG_interface_type:
22067 case DW_TAG_structure_type:
22068 case DW_TAG_union_type:
22069 case DW_TAG_set_type:
22070 case DW_TAG_enumeration_type:
22071 case DW_TAG_namelist:
22072 if (die->tag == DW_TAG_namelist)
22073 {
22074 sym->set_aclass_index (LOC_STATIC);
22075 sym->set_domain (VAR_DOMAIN);
22076 }
22077 else
22078 {
22079 sym->set_aclass_index (LOC_TYPEDEF);
22080 sym->set_domain (STRUCT_DOMAIN);
22081 }
22082 {
22083 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22084 really ever be static objects: otherwise, if you try
22085 to, say, break of a class's method and you're in a file
22086 which doesn't mention that class, it won't work unless
22087 the check for all static symbols in lookup_symbol_aux
22088 saves you. See the OtherFileClass tests in
22089 gdb.c++/namespace.exp. */
22090
22091 if (!suppress_add)
22092 {
22093 buildsym_compunit *builder = cu->get_builder ();
22094 list_to_add
22095 = (cu->list_in_scope == builder->get_file_symbols ()
22096 && cu->per_cu->lang == language_cplus
22097 ? builder->get_global_symbols ()
22098 : cu->list_in_scope);
22099
22100 /* The semantics of C++ state that "struct foo {
22101 ... }" also defines a typedef for "foo". */
22102 if (cu->per_cu->lang == language_cplus
22103 || cu->per_cu->lang == language_ada
22104 || cu->per_cu->lang == language_d
22105 || cu->per_cu->lang == language_rust)
22106 {
22107 /* The symbol's name is already allocated along
22108 with this objfile, so we don't need to
22109 duplicate it for the type. */
22110 if (sym->type ()->name () == 0)
22111 sym->type ()->set_name (sym->search_name ());
22112 }
22113 }
22114 }
22115 break;
22116 case DW_TAG_typedef:
22117 sym->set_aclass_index (LOC_TYPEDEF);
22118 sym->set_domain (VAR_DOMAIN);
22119 list_to_add = cu->list_in_scope;
22120 break;
22121 case DW_TAG_array_type:
22122 case DW_TAG_base_type:
22123 case DW_TAG_subrange_type:
22124 case DW_TAG_generic_subrange:
22125 sym->set_aclass_index (LOC_TYPEDEF);
22126 sym->set_domain (VAR_DOMAIN);
22127 list_to_add = cu->list_in_scope;
22128 break;
22129 case DW_TAG_enumerator:
22130 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22131 if (attr != nullptr)
22132 {
22133 dwarf2_const_value (attr, sym, cu);
22134 }
22135 {
22136 /* NOTE: carlton/2003-11-10: See comment above in the
22137 DW_TAG_class_type, etc. block. */
22138
22139 list_to_add
22140 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22141 && cu->per_cu->lang == language_cplus
22142 ? cu->get_builder ()->get_global_symbols ()
22143 : cu->list_in_scope);
22144 }
22145 break;
22146 case DW_TAG_imported_declaration:
22147 case DW_TAG_namespace:
22148 sym->set_aclass_index (LOC_TYPEDEF);
22149 list_to_add = cu->get_builder ()->get_global_symbols ();
22150 break;
22151 case DW_TAG_module:
22152 sym->set_aclass_index (LOC_TYPEDEF);
22153 sym->set_domain (MODULE_DOMAIN);
22154 list_to_add = cu->get_builder ()->get_global_symbols ();
22155 break;
22156 case DW_TAG_common_block:
22157 sym->set_aclass_index (LOC_COMMON_BLOCK);
22158 sym->set_domain (COMMON_BLOCK_DOMAIN);
22159 add_symbol_to_list (sym, cu->list_in_scope);
22160 break;
22161 default:
22162 /* Not a tag we recognize. Hopefully we aren't processing
22163 trash data, but since we must specifically ignore things
22164 we don't recognize, there is nothing else we should do at
22165 this point. */
22166 complaint (_("unsupported tag: '%s'"),
22167 dwarf_tag_name (die->tag));
22168 break;
22169 }
22170
22171 if (suppress_add)
22172 {
22173 sym->hash_next = objfile->template_symbols;
22174 objfile->template_symbols = sym;
22175 list_to_add = NULL;
22176 }
22177
22178 if (list_to_add != NULL)
22179 add_symbol_to_list (sym, list_to_add);
22180
22181 /* For the benefit of old versions of GCC, check for anonymous
22182 namespaces based on the demangled name. */
22183 if (!cu->processing_has_namespace_info
22184 && cu->per_cu->lang == language_cplus)
22185 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22186 }
22187 return (sym);
22188 }
22189
22190 /* Given an attr with a DW_FORM_dataN value in host byte order,
22191 zero-extend it as appropriate for the symbol's type. The DWARF
22192 standard (v4) is not entirely clear about the meaning of using
22193 DW_FORM_dataN for a constant with a signed type, where the type is
22194 wider than the data. The conclusion of a discussion on the DWARF
22195 list was that this is unspecified. We choose to always zero-extend
22196 because that is the interpretation long in use by GCC. */
22197
22198 static gdb_byte *
22199 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22200 struct dwarf2_cu *cu, LONGEST *value, int bits)
22201 {
22202 struct objfile *objfile = cu->per_objfile->objfile;
22203 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22204 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22205 LONGEST l = attr->constant_value (0);
22206
22207 if (bits < sizeof (*value) * 8)
22208 {
22209 l &= ((LONGEST) 1 << bits) - 1;
22210 *value = l;
22211 }
22212 else if (bits == sizeof (*value) * 8)
22213 *value = l;
22214 else
22215 {
22216 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22217 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22218 return bytes;
22219 }
22220
22221 return NULL;
22222 }
22223
22224 /* Read a constant value from an attribute. Either set *VALUE, or if
22225 the value does not fit in *VALUE, set *BYTES - either already
22226 allocated on the objfile obstack, or newly allocated on OBSTACK,
22227 or, set *BATON, if we translated the constant to a location
22228 expression. */
22229
22230 static void
22231 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22232 const char *name, struct obstack *obstack,
22233 struct dwarf2_cu *cu,
22234 LONGEST *value, const gdb_byte **bytes,
22235 struct dwarf2_locexpr_baton **baton)
22236 {
22237 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22238 struct objfile *objfile = per_objfile->objfile;
22239 struct comp_unit_head *cu_header = &cu->header;
22240 struct dwarf_block *blk;
22241 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22242 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22243
22244 *value = 0;
22245 *bytes = NULL;
22246 *baton = NULL;
22247
22248 switch (attr->form)
22249 {
22250 case DW_FORM_addr:
22251 case DW_FORM_addrx:
22252 case DW_FORM_GNU_addr_index:
22253 {
22254 gdb_byte *data;
22255
22256 if (TYPE_LENGTH (type) != cu_header->addr_size)
22257 dwarf2_const_value_length_mismatch_complaint (name,
22258 cu_header->addr_size,
22259 TYPE_LENGTH (type));
22260 /* Symbols of this form are reasonably rare, so we just
22261 piggyback on the existing location code rather than writing
22262 a new implementation of symbol_computed_ops. */
22263 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22264 (*baton)->per_objfile = per_objfile;
22265 (*baton)->per_cu = cu->per_cu;
22266 gdb_assert ((*baton)->per_cu);
22267
22268 (*baton)->size = 2 + cu_header->addr_size;
22269 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22270 (*baton)->data = data;
22271
22272 data[0] = DW_OP_addr;
22273 store_unsigned_integer (&data[1], cu_header->addr_size,
22274 byte_order, attr->as_address ());
22275 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22276 }
22277 break;
22278 case DW_FORM_string:
22279 case DW_FORM_strp:
22280 case DW_FORM_strx:
22281 case DW_FORM_GNU_str_index:
22282 case DW_FORM_GNU_strp_alt:
22283 /* The string is already allocated on the objfile obstack, point
22284 directly to it. */
22285 *bytes = (const gdb_byte *) attr->as_string ();
22286 break;
22287 case DW_FORM_block1:
22288 case DW_FORM_block2:
22289 case DW_FORM_block4:
22290 case DW_FORM_block:
22291 case DW_FORM_exprloc:
22292 case DW_FORM_data16:
22293 blk = attr->as_block ();
22294 if (TYPE_LENGTH (type) != blk->size)
22295 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22296 TYPE_LENGTH (type));
22297 *bytes = blk->data;
22298 break;
22299
22300 /* The DW_AT_const_value attributes are supposed to carry the
22301 symbol's value "represented as it would be on the target
22302 architecture." By the time we get here, it's already been
22303 converted to host endianness, so we just need to sign- or
22304 zero-extend it as appropriate. */
22305 case DW_FORM_data1:
22306 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22307 break;
22308 case DW_FORM_data2:
22309 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22310 break;
22311 case DW_FORM_data4:
22312 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22313 break;
22314 case DW_FORM_data8:
22315 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22316 break;
22317
22318 case DW_FORM_sdata:
22319 case DW_FORM_implicit_const:
22320 *value = attr->as_signed ();
22321 break;
22322
22323 case DW_FORM_udata:
22324 *value = attr->as_unsigned ();
22325 break;
22326
22327 default:
22328 complaint (_("unsupported const value attribute form: '%s'"),
22329 dwarf_form_name (attr->form));
22330 *value = 0;
22331 break;
22332 }
22333 }
22334
22335
22336 /* Copy constant value from an attribute to a symbol. */
22337
22338 static void
22339 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22340 struct dwarf2_cu *cu)
22341 {
22342 struct objfile *objfile = cu->per_objfile->objfile;
22343 LONGEST value;
22344 const gdb_byte *bytes;
22345 struct dwarf2_locexpr_baton *baton;
22346
22347 dwarf2_const_value_attr (attr, sym->type (),
22348 sym->print_name (),
22349 &objfile->objfile_obstack, cu,
22350 &value, &bytes, &baton);
22351
22352 if (baton != NULL)
22353 {
22354 SYMBOL_LOCATION_BATON (sym) = baton;
22355 sym->set_aclass_index (dwarf2_locexpr_index);
22356 }
22357 else if (bytes != NULL)
22358 {
22359 sym->set_value_bytes (bytes);
22360 sym->set_aclass_index (LOC_CONST_BYTES);
22361 }
22362 else
22363 {
22364 sym->set_value_longest (value);
22365 sym->set_aclass_index (LOC_CONST);
22366 }
22367 }
22368
22369 /* Return the type of the die in question using its DW_AT_type attribute. */
22370
22371 static struct type *
22372 die_type (struct die_info *die, struct dwarf2_cu *cu)
22373 {
22374 struct attribute *type_attr;
22375
22376 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22377 if (!type_attr)
22378 {
22379 struct objfile *objfile = cu->per_objfile->objfile;
22380 /* A missing DW_AT_type represents a void type. */
22381 return objfile_type (objfile)->builtin_void;
22382 }
22383
22384 return lookup_die_type (die, type_attr, cu);
22385 }
22386
22387 /* True iff CU's producer generates GNAT Ada auxiliary information
22388 that allows to find parallel types through that information instead
22389 of having to do expensive parallel lookups by type name. */
22390
22391 static int
22392 need_gnat_info (struct dwarf2_cu *cu)
22393 {
22394 /* Assume that the Ada compiler was GNAT, which always produces
22395 the auxiliary information. */
22396 return (cu->per_cu->lang == language_ada);
22397 }
22398
22399 /* Return the auxiliary type of the die in question using its
22400 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22401 attribute is not present. */
22402
22403 static struct type *
22404 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22405 {
22406 struct attribute *type_attr;
22407
22408 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22409 if (!type_attr)
22410 return NULL;
22411
22412 return lookup_die_type (die, type_attr, cu);
22413 }
22414
22415 /* If DIE has a descriptive_type attribute, then set the TYPE's
22416 descriptive type accordingly. */
22417
22418 static void
22419 set_descriptive_type (struct type *type, struct die_info *die,
22420 struct dwarf2_cu *cu)
22421 {
22422 struct type *descriptive_type = die_descriptive_type (die, cu);
22423
22424 if (descriptive_type)
22425 {
22426 ALLOCATE_GNAT_AUX_TYPE (type);
22427 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22428 }
22429 }
22430
22431 /* Return the containing type of the die in question using its
22432 DW_AT_containing_type attribute. */
22433
22434 static struct type *
22435 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22436 {
22437 struct attribute *type_attr;
22438 struct objfile *objfile = cu->per_objfile->objfile;
22439
22440 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22441 if (!type_attr)
22442 error (_("Dwarf Error: Problem turning containing type into gdb type "
22443 "[in module %s]"), objfile_name (objfile));
22444
22445 return lookup_die_type (die, type_attr, cu);
22446 }
22447
22448 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22449
22450 static struct type *
22451 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22452 {
22453 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22454 struct objfile *objfile = per_objfile->objfile;
22455 char *saved;
22456
22457 std::string message
22458 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22459 objfile_name (objfile),
22460 sect_offset_str (cu->header.sect_off),
22461 sect_offset_str (die->sect_off));
22462 saved = obstack_strdup (&objfile->objfile_obstack, message);
22463
22464 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22465 }
22466
22467 /* Look up the type of DIE in CU using its type attribute ATTR.
22468 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22469 DW_AT_containing_type.
22470 If there is no type substitute an error marker. */
22471
22472 static struct type *
22473 lookup_die_type (struct die_info *die, const struct attribute *attr,
22474 struct dwarf2_cu *cu)
22475 {
22476 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22477 struct objfile *objfile = per_objfile->objfile;
22478 struct type *this_type;
22479
22480 gdb_assert (attr->name == DW_AT_type
22481 || attr->name == DW_AT_GNAT_descriptive_type
22482 || attr->name == DW_AT_containing_type);
22483
22484 /* First see if we have it cached. */
22485
22486 if (attr->form == DW_FORM_GNU_ref_alt)
22487 {
22488 struct dwarf2_per_cu_data *per_cu;
22489 sect_offset sect_off = attr->get_ref_die_offset ();
22490
22491 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22492 per_objfile->per_bfd);
22493 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22494 }
22495 else if (attr->form_is_ref ())
22496 {
22497 sect_offset sect_off = attr->get_ref_die_offset ();
22498
22499 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22500 }
22501 else if (attr->form == DW_FORM_ref_sig8)
22502 {
22503 ULONGEST signature = attr->as_signature ();
22504
22505 return get_signatured_type (die, signature, cu);
22506 }
22507 else
22508 {
22509 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22510 " at %s [in module %s]"),
22511 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22512 objfile_name (objfile));
22513 return build_error_marker_type (cu, die);
22514 }
22515
22516 /* If not cached we need to read it in. */
22517
22518 if (this_type == NULL)
22519 {
22520 struct die_info *type_die = NULL;
22521 struct dwarf2_cu *type_cu = cu;
22522
22523 if (attr->form_is_ref ())
22524 type_die = follow_die_ref (die, attr, &type_cu);
22525 if (type_die == NULL)
22526 return build_error_marker_type (cu, die);
22527 /* If we find the type now, it's probably because the type came
22528 from an inter-CU reference and the type's CU got expanded before
22529 ours. */
22530 this_type = read_type_die (type_die, type_cu);
22531 }
22532
22533 /* If we still don't have a type use an error marker. */
22534
22535 if (this_type == NULL)
22536 return build_error_marker_type (cu, die);
22537
22538 return this_type;
22539 }
22540
22541 /* Return the type in DIE, CU.
22542 Returns NULL for invalid types.
22543
22544 This first does a lookup in die_type_hash,
22545 and only reads the die in if necessary.
22546
22547 NOTE: This can be called when reading in partial or full symbols. */
22548
22549 static struct type *
22550 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22551 {
22552 struct type *this_type;
22553
22554 this_type = get_die_type (die, cu);
22555 if (this_type)
22556 return this_type;
22557
22558 return read_type_die_1 (die, cu);
22559 }
22560
22561 /* Read the type in DIE, CU.
22562 Returns NULL for invalid types. */
22563
22564 static struct type *
22565 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22566 {
22567 struct type *this_type = NULL;
22568
22569 switch (die->tag)
22570 {
22571 case DW_TAG_class_type:
22572 case DW_TAG_interface_type:
22573 case DW_TAG_structure_type:
22574 case DW_TAG_union_type:
22575 this_type = read_structure_type (die, cu);
22576 break;
22577 case DW_TAG_enumeration_type:
22578 this_type = read_enumeration_type (die, cu);
22579 break;
22580 case DW_TAG_subprogram:
22581 case DW_TAG_subroutine_type:
22582 case DW_TAG_inlined_subroutine:
22583 this_type = read_subroutine_type (die, cu);
22584 break;
22585 case DW_TAG_array_type:
22586 this_type = read_array_type (die, cu);
22587 break;
22588 case DW_TAG_set_type:
22589 this_type = read_set_type (die, cu);
22590 break;
22591 case DW_TAG_pointer_type:
22592 this_type = read_tag_pointer_type (die, cu);
22593 break;
22594 case DW_TAG_ptr_to_member_type:
22595 this_type = read_tag_ptr_to_member_type (die, cu);
22596 break;
22597 case DW_TAG_reference_type:
22598 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22599 break;
22600 case DW_TAG_rvalue_reference_type:
22601 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22602 break;
22603 case DW_TAG_const_type:
22604 this_type = read_tag_const_type (die, cu);
22605 break;
22606 case DW_TAG_volatile_type:
22607 this_type = read_tag_volatile_type (die, cu);
22608 break;
22609 case DW_TAG_restrict_type:
22610 this_type = read_tag_restrict_type (die, cu);
22611 break;
22612 case DW_TAG_string_type:
22613 this_type = read_tag_string_type (die, cu);
22614 break;
22615 case DW_TAG_typedef:
22616 this_type = read_typedef (die, cu);
22617 break;
22618 case DW_TAG_generic_subrange:
22619 case DW_TAG_subrange_type:
22620 this_type = read_subrange_type (die, cu);
22621 break;
22622 case DW_TAG_base_type:
22623 this_type = read_base_type (die, cu);
22624 break;
22625 case DW_TAG_unspecified_type:
22626 this_type = read_unspecified_type (die, cu);
22627 break;
22628 case DW_TAG_namespace:
22629 this_type = read_namespace_type (die, cu);
22630 break;
22631 case DW_TAG_module:
22632 this_type = read_module_type (die, cu);
22633 break;
22634 case DW_TAG_atomic_type:
22635 this_type = read_tag_atomic_type (die, cu);
22636 break;
22637 default:
22638 complaint (_("unexpected tag in read_type_die: '%s'"),
22639 dwarf_tag_name (die->tag));
22640 break;
22641 }
22642
22643 return this_type;
22644 }
22645
22646 /* See if we can figure out if the class lives in a namespace. We do
22647 this by looking for a member function; its demangled name will
22648 contain namespace info, if there is any.
22649 Return the computed name or NULL.
22650 Space for the result is allocated on the objfile's obstack.
22651 This is the full-die version of guess_partial_die_structure_name.
22652 In this case we know DIE has no useful parent. */
22653
22654 static const char *
22655 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22656 {
22657 struct die_info *spec_die;
22658 struct dwarf2_cu *spec_cu;
22659 struct die_info *child;
22660 struct objfile *objfile = cu->per_objfile->objfile;
22661
22662 spec_cu = cu;
22663 spec_die = die_specification (die, &spec_cu);
22664 if (spec_die != NULL)
22665 {
22666 die = spec_die;
22667 cu = spec_cu;
22668 }
22669
22670 for (child = die->child;
22671 child != NULL;
22672 child = child->sibling)
22673 {
22674 if (child->tag == DW_TAG_subprogram)
22675 {
22676 const char *linkage_name = dw2_linkage_name (child, cu);
22677
22678 if (linkage_name != NULL)
22679 {
22680 gdb::unique_xmalloc_ptr<char> actual_name
22681 (cu->language_defn->class_name_from_physname (linkage_name));
22682 const char *name = NULL;
22683
22684 if (actual_name != NULL)
22685 {
22686 const char *die_name = dwarf2_name (die, cu);
22687
22688 if (die_name != NULL
22689 && strcmp (die_name, actual_name.get ()) != 0)
22690 {
22691 /* Strip off the class name from the full name.
22692 We want the prefix. */
22693 int die_name_len = strlen (die_name);
22694 int actual_name_len = strlen (actual_name.get ());
22695 const char *ptr = actual_name.get ();
22696
22697 /* Test for '::' as a sanity check. */
22698 if (actual_name_len > die_name_len + 2
22699 && ptr[actual_name_len - die_name_len - 1] == ':')
22700 name = obstack_strndup (
22701 &objfile->per_bfd->storage_obstack,
22702 ptr, actual_name_len - die_name_len - 2);
22703 }
22704 }
22705 return name;
22706 }
22707 }
22708 }
22709
22710 return NULL;
22711 }
22712
22713 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22714 prefix part in such case. See
22715 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22716
22717 static const char *
22718 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22719 {
22720 struct attribute *attr;
22721 const char *base;
22722
22723 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22724 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22725 return NULL;
22726
22727 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22728 return NULL;
22729
22730 attr = dw2_linkage_name_attr (die, cu);
22731 const char *attr_name = attr->as_string ();
22732 if (attr == NULL || attr_name == NULL)
22733 return NULL;
22734
22735 /* dwarf2_name had to be already called. */
22736 gdb_assert (attr->canonical_string_p ());
22737
22738 /* Strip the base name, keep any leading namespaces/classes. */
22739 base = strrchr (attr_name, ':');
22740 if (base == NULL || base == attr_name || base[-1] != ':')
22741 return "";
22742
22743 struct objfile *objfile = cu->per_objfile->objfile;
22744 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22745 attr_name,
22746 &base[-1] - attr_name);
22747 }
22748
22749 /* Return the name of the namespace/class that DIE is defined within,
22750 or "" if we can't tell. The caller should not xfree the result.
22751
22752 For example, if we're within the method foo() in the following
22753 code:
22754
22755 namespace N {
22756 class C {
22757 void foo () {
22758 }
22759 };
22760 }
22761
22762 then determine_prefix on foo's die will return "N::C". */
22763
22764 static const char *
22765 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22766 {
22767 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22768 struct die_info *parent, *spec_die;
22769 struct dwarf2_cu *spec_cu;
22770 struct type *parent_type;
22771 const char *retval;
22772
22773 if (cu->per_cu->lang != language_cplus
22774 && cu->per_cu->lang != language_fortran
22775 && cu->per_cu->lang != language_d
22776 && cu->per_cu->lang != language_rust)
22777 return "";
22778
22779 retval = anonymous_struct_prefix (die, cu);
22780 if (retval)
22781 return retval;
22782
22783 /* We have to be careful in the presence of DW_AT_specification.
22784 For example, with GCC 3.4, given the code
22785
22786 namespace N {
22787 void foo() {
22788 // Definition of N::foo.
22789 }
22790 }
22791
22792 then we'll have a tree of DIEs like this:
22793
22794 1: DW_TAG_compile_unit
22795 2: DW_TAG_namespace // N
22796 3: DW_TAG_subprogram // declaration of N::foo
22797 4: DW_TAG_subprogram // definition of N::foo
22798 DW_AT_specification // refers to die #3
22799
22800 Thus, when processing die #4, we have to pretend that we're in
22801 the context of its DW_AT_specification, namely the contex of die
22802 #3. */
22803 spec_cu = cu;
22804 spec_die = die_specification (die, &spec_cu);
22805 if (spec_die == NULL)
22806 parent = die->parent;
22807 else
22808 {
22809 parent = spec_die->parent;
22810 cu = spec_cu;
22811 }
22812
22813 if (parent == NULL)
22814 return "";
22815 else if (parent->building_fullname)
22816 {
22817 const char *name;
22818 const char *parent_name;
22819
22820 /* It has been seen on RealView 2.2 built binaries,
22821 DW_TAG_template_type_param types actually _defined_ as
22822 children of the parent class:
22823
22824 enum E {};
22825 template class <class Enum> Class{};
22826 Class<enum E> class_e;
22827
22828 1: DW_TAG_class_type (Class)
22829 2: DW_TAG_enumeration_type (E)
22830 3: DW_TAG_enumerator (enum1:0)
22831 3: DW_TAG_enumerator (enum2:1)
22832 ...
22833 2: DW_TAG_template_type_param
22834 DW_AT_type DW_FORM_ref_udata (E)
22835
22836 Besides being broken debug info, it can put GDB into an
22837 infinite loop. Consider:
22838
22839 When we're building the full name for Class<E>, we'll start
22840 at Class, and go look over its template type parameters,
22841 finding E. We'll then try to build the full name of E, and
22842 reach here. We're now trying to build the full name of E,
22843 and look over the parent DIE for containing scope. In the
22844 broken case, if we followed the parent DIE of E, we'd again
22845 find Class, and once again go look at its template type
22846 arguments, etc., etc. Simply don't consider such parent die
22847 as source-level parent of this die (it can't be, the language
22848 doesn't allow it), and break the loop here. */
22849 name = dwarf2_name (die, cu);
22850 parent_name = dwarf2_name (parent, cu);
22851 complaint (_("template param type '%s' defined within parent '%s'"),
22852 name ? name : "<unknown>",
22853 parent_name ? parent_name : "<unknown>");
22854 return "";
22855 }
22856 else
22857 switch (parent->tag)
22858 {
22859 case DW_TAG_namespace:
22860 parent_type = read_type_die (parent, cu);
22861 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22862 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22863 Work around this problem here. */
22864 if (cu->per_cu->lang == language_cplus
22865 && strcmp (parent_type->name (), "::") == 0)
22866 return "";
22867 /* We give a name to even anonymous namespaces. */
22868 return parent_type->name ();
22869 case DW_TAG_class_type:
22870 case DW_TAG_interface_type:
22871 case DW_TAG_structure_type:
22872 case DW_TAG_union_type:
22873 case DW_TAG_module:
22874 parent_type = read_type_die (parent, cu);
22875 if (parent_type->name () != NULL)
22876 return parent_type->name ();
22877 else
22878 /* An anonymous structure is only allowed non-static data
22879 members; no typedefs, no member functions, et cetera.
22880 So it does not need a prefix. */
22881 return "";
22882 case DW_TAG_compile_unit:
22883 case DW_TAG_partial_unit:
22884 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22885 if (cu->per_cu->lang == language_cplus
22886 && !per_objfile->per_bfd->types.empty ()
22887 && die->child != NULL
22888 && (die->tag == DW_TAG_class_type
22889 || die->tag == DW_TAG_structure_type
22890 || die->tag == DW_TAG_union_type))
22891 {
22892 const char *name = guess_full_die_structure_name (die, cu);
22893 if (name != NULL)
22894 return name;
22895 }
22896 return "";
22897 case DW_TAG_subprogram:
22898 /* Nested subroutines in Fortran get a prefix with the name
22899 of the parent's subroutine. */
22900 if (cu->per_cu->lang == language_fortran)
22901 {
22902 if ((die->tag == DW_TAG_subprogram)
22903 && (dwarf2_name (parent, cu) != NULL))
22904 return dwarf2_name (parent, cu);
22905 }
22906 return "";
22907 case DW_TAG_enumeration_type:
22908 parent_type = read_type_die (parent, cu);
22909 if (parent_type->is_declared_class ())
22910 {
22911 if (parent_type->name () != NULL)
22912 return parent_type->name ();
22913 return "";
22914 }
22915 /* Fall through. */
22916 default:
22917 return determine_prefix (parent, cu);
22918 }
22919 }
22920
22921 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22922 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22923 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22924 an obconcat, otherwise allocate storage for the result. The CU argument is
22925 used to determine the language and hence, the appropriate separator. */
22926
22927 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22928
22929 static char *
22930 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22931 int physname, struct dwarf2_cu *cu)
22932 {
22933 const char *lead = "";
22934 const char *sep;
22935
22936 if (suffix == NULL || suffix[0] == '\0'
22937 || prefix == NULL || prefix[0] == '\0')
22938 sep = "";
22939 else if (cu->per_cu->lang == language_d)
22940 {
22941 /* For D, the 'main' function could be defined in any module, but it
22942 should never be prefixed. */
22943 if (strcmp (suffix, "D main") == 0)
22944 {
22945 prefix = "";
22946 sep = "";
22947 }
22948 else
22949 sep = ".";
22950 }
22951 else if (cu->per_cu->lang == language_fortran && physname)
22952 {
22953 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22954 DW_AT_MIPS_linkage_name is preferred and used instead. */
22955
22956 lead = "__";
22957 sep = "_MOD_";
22958 }
22959 else
22960 sep = "::";
22961
22962 if (prefix == NULL)
22963 prefix = "";
22964 if (suffix == NULL)
22965 suffix = "";
22966
22967 if (obs == NULL)
22968 {
22969 char *retval
22970 = ((char *)
22971 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22972
22973 strcpy (retval, lead);
22974 strcat (retval, prefix);
22975 strcat (retval, sep);
22976 strcat (retval, suffix);
22977 return retval;
22978 }
22979 else
22980 {
22981 /* We have an obstack. */
22982 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22983 }
22984 }
22985
22986 /* Get name of a die, return NULL if not found. */
22987
22988 static const char *
22989 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22990 struct objfile *objfile)
22991 {
22992 if (name && cu->per_cu->lang == language_cplus)
22993 {
22994 gdb::unique_xmalloc_ptr<char> canon_name
22995 = cp_canonicalize_string (name);
22996
22997 if (canon_name != nullptr)
22998 name = objfile->intern (canon_name.get ());
22999 }
23000
23001 return name;
23002 }
23003
23004 /* Get name of a die, return NULL if not found.
23005 Anonymous namespaces are converted to their magic string. */
23006
23007 static const char *
23008 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23009 {
23010 struct attribute *attr;
23011 struct objfile *objfile = cu->per_objfile->objfile;
23012
23013 attr = dwarf2_attr (die, DW_AT_name, cu);
23014 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23015 if (attr_name == nullptr
23016 && die->tag != DW_TAG_namespace
23017 && die->tag != DW_TAG_class_type
23018 && die->tag != DW_TAG_interface_type
23019 && die->tag != DW_TAG_structure_type
23020 && die->tag != DW_TAG_namelist
23021 && die->tag != DW_TAG_union_type)
23022 return NULL;
23023
23024 switch (die->tag)
23025 {
23026 case DW_TAG_compile_unit:
23027 case DW_TAG_partial_unit:
23028 /* Compilation units have a DW_AT_name that is a filename, not
23029 a source language identifier. */
23030 case DW_TAG_enumeration_type:
23031 case DW_TAG_enumerator:
23032 /* These tags always have simple identifiers already; no need
23033 to canonicalize them. */
23034 return attr_name;
23035
23036 case DW_TAG_namespace:
23037 if (attr_name != nullptr)
23038 return attr_name;
23039 return CP_ANONYMOUS_NAMESPACE_STR;
23040
23041 case DW_TAG_class_type:
23042 case DW_TAG_interface_type:
23043 case DW_TAG_structure_type:
23044 case DW_TAG_union_type:
23045 case DW_TAG_namelist:
23046 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23047 structures or unions. These were of the form "._%d" in GCC 4.1,
23048 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23049 and GCC 4.4. We work around this problem by ignoring these. */
23050 if (attr_name != nullptr
23051 && (startswith (attr_name, "._")
23052 || startswith (attr_name, "<anonymous")))
23053 return NULL;
23054
23055 /* GCC might emit a nameless typedef that has a linkage name. See
23056 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23057 if (!attr || attr_name == NULL)
23058 {
23059 attr = dw2_linkage_name_attr (die, cu);
23060 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23061 if (attr == NULL || attr_name == NULL)
23062 return NULL;
23063
23064 /* Avoid demangling attr_name the second time on a second
23065 call for the same DIE. */
23066 if (!attr->canonical_string_p ())
23067 {
23068 gdb::unique_xmalloc_ptr<char> demangled
23069 (gdb_demangle (attr_name, DMGL_TYPES));
23070 if (demangled == nullptr)
23071 return nullptr;
23072
23073 attr->set_string_canonical (objfile->intern (demangled.get ()));
23074 attr_name = attr->as_string ();
23075 }
23076
23077 /* Strip any leading namespaces/classes, keep only the
23078 base name. DW_AT_name for named DIEs does not
23079 contain the prefixes. */
23080 const char *base = strrchr (attr_name, ':');
23081 if (base && base > attr_name && base[-1] == ':')
23082 return &base[1];
23083 else
23084 return attr_name;
23085 }
23086 break;
23087
23088 default:
23089 break;
23090 }
23091
23092 if (!attr->canonical_string_p ())
23093 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23094 objfile));
23095 return attr->as_string ();
23096 }
23097
23098 /* Return the die that this die in an extension of, or NULL if there
23099 is none. *EXT_CU is the CU containing DIE on input, and the CU
23100 containing the return value on output. */
23101
23102 static struct die_info *
23103 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23104 {
23105 struct attribute *attr;
23106
23107 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23108 if (attr == NULL)
23109 return NULL;
23110
23111 return follow_die_ref (die, attr, ext_cu);
23112 }
23113
23114 static void
23115 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23116 {
23117 unsigned int i;
23118
23119 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
23120 indent, "",
23121 dwarf_tag_name (die->tag), die->abbrev,
23122 sect_offset_str (die->sect_off));
23123
23124 if (die->parent != NULL)
23125 gdb_printf (f, "%*s parent at offset: %s\n",
23126 indent, "",
23127 sect_offset_str (die->parent->sect_off));
23128
23129 gdb_printf (f, "%*s has children: %s\n",
23130 indent, "",
23131 dwarf_bool_name (die->child != NULL));
23132
23133 gdb_printf (f, "%*s attributes:\n", indent, "");
23134
23135 for (i = 0; i < die->num_attrs; ++i)
23136 {
23137 gdb_printf (f, "%*s %s (%s) ",
23138 indent, "",
23139 dwarf_attr_name (die->attrs[i].name),
23140 dwarf_form_name (die->attrs[i].form));
23141
23142 switch (die->attrs[i].form)
23143 {
23144 case DW_FORM_addr:
23145 case DW_FORM_addrx:
23146 case DW_FORM_GNU_addr_index:
23147 gdb_printf (f, "address: ");
23148 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
23149 break;
23150 case DW_FORM_block2:
23151 case DW_FORM_block4:
23152 case DW_FORM_block:
23153 case DW_FORM_block1:
23154 gdb_printf (f, "block: size %s",
23155 pulongest (die->attrs[i].as_block ()->size));
23156 break;
23157 case DW_FORM_exprloc:
23158 gdb_printf (f, "expression: size %s",
23159 pulongest (die->attrs[i].as_block ()->size));
23160 break;
23161 case DW_FORM_data16:
23162 gdb_printf (f, "constant of 16 bytes");
23163 break;
23164 case DW_FORM_ref_addr:
23165 gdb_printf (f, "ref address: ");
23166 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
23167 break;
23168 case DW_FORM_GNU_ref_alt:
23169 gdb_printf (f, "alt ref address: ");
23170 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
23171 break;
23172 case DW_FORM_ref1:
23173 case DW_FORM_ref2:
23174 case DW_FORM_ref4:
23175 case DW_FORM_ref8:
23176 case DW_FORM_ref_udata:
23177 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
23178 (long) (die->attrs[i].as_unsigned ()));
23179 break;
23180 case DW_FORM_data1:
23181 case DW_FORM_data2:
23182 case DW_FORM_data4:
23183 case DW_FORM_data8:
23184 case DW_FORM_udata:
23185 gdb_printf (f, "constant: %s",
23186 pulongest (die->attrs[i].as_unsigned ()));
23187 break;
23188 case DW_FORM_sec_offset:
23189 gdb_printf (f, "section offset: %s",
23190 pulongest (die->attrs[i].as_unsigned ()));
23191 break;
23192 case DW_FORM_ref_sig8:
23193 gdb_printf (f, "signature: %s",
23194 hex_string (die->attrs[i].as_signature ()));
23195 break;
23196 case DW_FORM_string:
23197 case DW_FORM_strp:
23198 case DW_FORM_line_strp:
23199 case DW_FORM_strx:
23200 case DW_FORM_GNU_str_index:
23201 case DW_FORM_GNU_strp_alt:
23202 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
23203 die->attrs[i].as_string ()
23204 ? die->attrs[i].as_string () : "",
23205 die->attrs[i].canonical_string_p () ? "is" : "not");
23206 break;
23207 case DW_FORM_flag:
23208 if (die->attrs[i].as_boolean ())
23209 gdb_printf (f, "flag: TRUE");
23210 else
23211 gdb_printf (f, "flag: FALSE");
23212 break;
23213 case DW_FORM_flag_present:
23214 gdb_printf (f, "flag: TRUE");
23215 break;
23216 case DW_FORM_indirect:
23217 /* The reader will have reduced the indirect form to
23218 the "base form" so this form should not occur. */
23219 gdb_printf (f,
23220 "unexpected attribute form: DW_FORM_indirect");
23221 break;
23222 case DW_FORM_sdata:
23223 case DW_FORM_implicit_const:
23224 gdb_printf (f, "constant: %s",
23225 plongest (die->attrs[i].as_signed ()));
23226 break;
23227 default:
23228 gdb_printf (f, "unsupported attribute form: %d.",
23229 die->attrs[i].form);
23230 break;
23231 }
23232 gdb_printf (f, "\n");
23233 }
23234 }
23235
23236 static void
23237 dump_die_for_error (struct die_info *die)
23238 {
23239 dump_die_shallow (gdb_stderr, 0, die);
23240 }
23241
23242 static void
23243 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23244 {
23245 int indent = level * 4;
23246
23247 gdb_assert (die != NULL);
23248
23249 if (level >= max_level)
23250 return;
23251
23252 dump_die_shallow (f, indent, die);
23253
23254 if (die->child != NULL)
23255 {
23256 gdb_printf (f, "%*s Children:", indent, "");
23257 if (level + 1 < max_level)
23258 {
23259 gdb_printf (f, "\n");
23260 dump_die_1 (f, level + 1, max_level, die->child);
23261 }
23262 else
23263 {
23264 gdb_printf (f,
23265 " [not printed, max nesting level reached]\n");
23266 }
23267 }
23268
23269 if (die->sibling != NULL && level > 0)
23270 {
23271 dump_die_1 (f, level, max_level, die->sibling);
23272 }
23273 }
23274
23275 /* This is called from the pdie macro in gdbinit.in.
23276 It's not static so gcc will keep a copy callable from gdb. */
23277
23278 void
23279 dump_die (struct die_info *die, int max_level)
23280 {
23281 dump_die_1 (gdb_stdlog, 0, max_level, die);
23282 }
23283
23284 static void
23285 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23286 {
23287 void **slot;
23288
23289 slot = htab_find_slot_with_hash (cu->die_hash, die,
23290 to_underlying (die->sect_off),
23291 INSERT);
23292
23293 *slot = die;
23294 }
23295
23296 /* Follow reference or signature attribute ATTR of SRC_DIE.
23297 On entry *REF_CU is the CU of SRC_DIE.
23298 On exit *REF_CU is the CU of the result. */
23299
23300 static struct die_info *
23301 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23302 struct dwarf2_cu **ref_cu)
23303 {
23304 struct die_info *die;
23305
23306 if (attr->form_is_ref ())
23307 die = follow_die_ref (src_die, attr, ref_cu);
23308 else if (attr->form == DW_FORM_ref_sig8)
23309 die = follow_die_sig (src_die, attr, ref_cu);
23310 else
23311 {
23312 dump_die_for_error (src_die);
23313 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23314 objfile_name ((*ref_cu)->per_objfile->objfile));
23315 }
23316
23317 return die;
23318 }
23319
23320 /* Follow reference OFFSET.
23321 On entry *REF_CU is the CU of the source die referencing OFFSET.
23322 On exit *REF_CU is the CU of the result.
23323 Returns NULL if OFFSET is invalid. */
23324
23325 static struct die_info *
23326 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23327 struct dwarf2_cu **ref_cu)
23328 {
23329 struct die_info temp_die;
23330 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23331 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23332
23333 gdb_assert (cu->per_cu != NULL);
23334
23335 target_cu = cu;
23336
23337 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23338 "source CU contains target offset: %d",
23339 sect_offset_str (cu->per_cu->sect_off),
23340 sect_offset_str (sect_off),
23341 cu->header.offset_in_cu_p (sect_off));
23342
23343 if (cu->per_cu->is_debug_types)
23344 {
23345 /* .debug_types CUs cannot reference anything outside their CU.
23346 If they need to, they have to reference a signatured type via
23347 DW_FORM_ref_sig8. */
23348 if (!cu->header.offset_in_cu_p (sect_off))
23349 return NULL;
23350 }
23351 else if (offset_in_dwz != cu->per_cu->is_dwz
23352 || !cu->header.offset_in_cu_p (sect_off))
23353 {
23354 struct dwarf2_per_cu_data *per_cu;
23355
23356 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23357 per_objfile->per_bfd);
23358
23359 dwarf_read_debug_printf_v ("target CU offset: %s, "
23360 "target CU DIEs loaded: %d",
23361 sect_offset_str (per_cu->sect_off),
23362 per_objfile->get_cu (per_cu) != nullptr);
23363
23364 /* If necessary, add it to the queue and load its DIEs.
23365
23366 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23367 it doesn't mean they are currently loaded. Since we require them
23368 to be loaded, we must check for ourselves. */
23369 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
23370 || per_objfile->get_cu (per_cu) == nullptr)
23371 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23372 false, cu->per_cu->lang);
23373
23374 target_cu = per_objfile->get_cu (per_cu);
23375 gdb_assert (target_cu != nullptr);
23376 }
23377 else if (cu->dies == NULL)
23378 {
23379 /* We're loading full DIEs during partial symbol reading. */
23380 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23381 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23382 language_minimal);
23383 }
23384
23385 *ref_cu = target_cu;
23386 temp_die.sect_off = sect_off;
23387
23388 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23389 &temp_die,
23390 to_underlying (sect_off));
23391 }
23392
23393 /* Follow reference attribute ATTR of SRC_DIE.
23394 On entry *REF_CU is the CU of SRC_DIE.
23395 On exit *REF_CU is the CU of the result. */
23396
23397 static struct die_info *
23398 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23399 struct dwarf2_cu **ref_cu)
23400 {
23401 sect_offset sect_off = attr->get_ref_die_offset ();
23402 struct dwarf2_cu *cu = *ref_cu;
23403 struct die_info *die;
23404
23405 die = follow_die_offset (sect_off,
23406 (attr->form == DW_FORM_GNU_ref_alt
23407 || cu->per_cu->is_dwz),
23408 ref_cu);
23409 if (!die)
23410 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23411 "at %s [in module %s]"),
23412 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23413 objfile_name (cu->per_objfile->objfile));
23414
23415 return die;
23416 }
23417
23418 /* See read.h. */
23419
23420 struct dwarf2_locexpr_baton
23421 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23422 dwarf2_per_cu_data *per_cu,
23423 dwarf2_per_objfile *per_objfile,
23424 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23425 bool resolve_abstract_p)
23426 {
23427 struct die_info *die;
23428 struct attribute *attr;
23429 struct dwarf2_locexpr_baton retval;
23430 struct objfile *objfile = per_objfile->objfile;
23431
23432 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23433 if (cu == nullptr)
23434 cu = load_cu (per_cu, per_objfile, false);
23435
23436 if (cu == nullptr)
23437 {
23438 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23439 Instead just throw an error, not much else we can do. */
23440 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23441 sect_offset_str (sect_off), objfile_name (objfile));
23442 }
23443
23444 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23445 if (!die)
23446 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23447 sect_offset_str (sect_off), objfile_name (objfile));
23448
23449 attr = dwarf2_attr (die, DW_AT_location, cu);
23450 if (!attr && resolve_abstract_p
23451 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23452 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23453 {
23454 CORE_ADDR pc = get_frame_pc ();
23455 CORE_ADDR baseaddr = objfile->text_section_offset ();
23456 struct gdbarch *gdbarch = objfile->arch ();
23457
23458 for (const auto &cand_off
23459 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23460 {
23461 struct dwarf2_cu *cand_cu = cu;
23462 struct die_info *cand
23463 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23464 if (!cand
23465 || !cand->parent
23466 || cand->parent->tag != DW_TAG_subprogram)
23467 continue;
23468
23469 CORE_ADDR pc_low, pc_high;
23470 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23471 if (pc_low == ((CORE_ADDR) -1))
23472 continue;
23473 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23474 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23475 if (!(pc_low <= pc && pc < pc_high))
23476 continue;
23477
23478 die = cand;
23479 attr = dwarf2_attr (die, DW_AT_location, cu);
23480 break;
23481 }
23482 }
23483
23484 if (!attr)
23485 {
23486 /* DWARF: "If there is no such attribute, then there is no effect.".
23487 DATA is ignored if SIZE is 0. */
23488
23489 retval.data = NULL;
23490 retval.size = 0;
23491 }
23492 else if (attr->form_is_section_offset ())
23493 {
23494 struct dwarf2_loclist_baton loclist_baton;
23495 CORE_ADDR pc = get_frame_pc ();
23496 size_t size;
23497
23498 fill_in_loclist_baton (cu, &loclist_baton, attr);
23499
23500 retval.data = dwarf2_find_location_expression (&loclist_baton,
23501 &size, pc);
23502 retval.size = size;
23503 }
23504 else
23505 {
23506 if (!attr->form_is_block ())
23507 error (_("Dwarf Error: DIE at %s referenced in module %s "
23508 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23509 sect_offset_str (sect_off), objfile_name (objfile));
23510
23511 struct dwarf_block *block = attr->as_block ();
23512 retval.data = block->data;
23513 retval.size = block->size;
23514 }
23515 retval.per_objfile = per_objfile;
23516 retval.per_cu = cu->per_cu;
23517
23518 per_objfile->age_comp_units ();
23519
23520 return retval;
23521 }
23522
23523 /* See read.h. */
23524
23525 struct dwarf2_locexpr_baton
23526 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23527 dwarf2_per_cu_data *per_cu,
23528 dwarf2_per_objfile *per_objfile,
23529 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23530 {
23531 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23532
23533 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23534 get_frame_pc);
23535 }
23536
23537 /* Write a constant of a given type as target-ordered bytes into
23538 OBSTACK. */
23539
23540 static const gdb_byte *
23541 write_constant_as_bytes (struct obstack *obstack,
23542 enum bfd_endian byte_order,
23543 struct type *type,
23544 ULONGEST value,
23545 LONGEST *len)
23546 {
23547 gdb_byte *result;
23548
23549 *len = TYPE_LENGTH (type);
23550 result = (gdb_byte *) obstack_alloc (obstack, *len);
23551 store_unsigned_integer (result, *len, byte_order, value);
23552
23553 return result;
23554 }
23555
23556 /* See read.h. */
23557
23558 const gdb_byte *
23559 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23560 dwarf2_per_cu_data *per_cu,
23561 dwarf2_per_objfile *per_objfile,
23562 obstack *obstack,
23563 LONGEST *len)
23564 {
23565 struct die_info *die;
23566 struct attribute *attr;
23567 const gdb_byte *result = NULL;
23568 struct type *type;
23569 LONGEST value;
23570 enum bfd_endian byte_order;
23571 struct objfile *objfile = per_objfile->objfile;
23572
23573 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23574 if (cu == nullptr)
23575 cu = load_cu (per_cu, per_objfile, false);
23576
23577 if (cu == nullptr)
23578 {
23579 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23580 Instead just throw an error, not much else we can do. */
23581 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23582 sect_offset_str (sect_off), objfile_name (objfile));
23583 }
23584
23585 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23586 if (!die)
23587 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23588 sect_offset_str (sect_off), objfile_name (objfile));
23589
23590 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23591 if (attr == NULL)
23592 return NULL;
23593
23594 byte_order = (bfd_big_endian (objfile->obfd)
23595 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23596
23597 switch (attr->form)
23598 {
23599 case DW_FORM_addr:
23600 case DW_FORM_addrx:
23601 case DW_FORM_GNU_addr_index:
23602 {
23603 gdb_byte *tem;
23604
23605 *len = cu->header.addr_size;
23606 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23607 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23608 result = tem;
23609 }
23610 break;
23611 case DW_FORM_string:
23612 case DW_FORM_strp:
23613 case DW_FORM_strx:
23614 case DW_FORM_GNU_str_index:
23615 case DW_FORM_GNU_strp_alt:
23616 /* The string is already allocated on the objfile obstack, point
23617 directly to it. */
23618 {
23619 const char *attr_name = attr->as_string ();
23620 result = (const gdb_byte *) attr_name;
23621 *len = strlen (attr_name);
23622 }
23623 break;
23624 case DW_FORM_block1:
23625 case DW_FORM_block2:
23626 case DW_FORM_block4:
23627 case DW_FORM_block:
23628 case DW_FORM_exprloc:
23629 case DW_FORM_data16:
23630 {
23631 struct dwarf_block *block = attr->as_block ();
23632 result = block->data;
23633 *len = block->size;
23634 }
23635 break;
23636
23637 /* The DW_AT_const_value attributes are supposed to carry the
23638 symbol's value "represented as it would be on the target
23639 architecture." By the time we get here, it's already been
23640 converted to host endianness, so we just need to sign- or
23641 zero-extend it as appropriate. */
23642 case DW_FORM_data1:
23643 type = die_type (die, cu);
23644 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23645 if (result == NULL)
23646 result = write_constant_as_bytes (obstack, byte_order,
23647 type, value, len);
23648 break;
23649 case DW_FORM_data2:
23650 type = die_type (die, cu);
23651 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23652 if (result == NULL)
23653 result = write_constant_as_bytes (obstack, byte_order,
23654 type, value, len);
23655 break;
23656 case DW_FORM_data4:
23657 type = die_type (die, cu);
23658 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23659 if (result == NULL)
23660 result = write_constant_as_bytes (obstack, byte_order,
23661 type, value, len);
23662 break;
23663 case DW_FORM_data8:
23664 type = die_type (die, cu);
23665 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23666 if (result == NULL)
23667 result = write_constant_as_bytes (obstack, byte_order,
23668 type, value, len);
23669 break;
23670
23671 case DW_FORM_sdata:
23672 case DW_FORM_implicit_const:
23673 type = die_type (die, cu);
23674 result = write_constant_as_bytes (obstack, byte_order,
23675 type, attr->as_signed (), len);
23676 break;
23677
23678 case DW_FORM_udata:
23679 type = die_type (die, cu);
23680 result = write_constant_as_bytes (obstack, byte_order,
23681 type, attr->as_unsigned (), len);
23682 break;
23683
23684 default:
23685 complaint (_("unsupported const value attribute form: '%s'"),
23686 dwarf_form_name (attr->form));
23687 break;
23688 }
23689
23690 return result;
23691 }
23692
23693 /* See read.h. */
23694
23695 struct type *
23696 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23697 dwarf2_per_cu_data *per_cu,
23698 dwarf2_per_objfile *per_objfile,
23699 const char **var_name)
23700 {
23701 struct die_info *die;
23702
23703 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23704 if (cu == nullptr)
23705 cu = load_cu (per_cu, per_objfile, false);
23706
23707 if (cu == nullptr)
23708 return nullptr;
23709
23710 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23711 if (!die)
23712 return NULL;
23713
23714 if (var_name != nullptr)
23715 *var_name = var_decl_name (die, cu);
23716 return die_type (die, cu);
23717 }
23718
23719 /* See read.h. */
23720
23721 struct type *
23722 dwarf2_get_die_type (cu_offset die_offset,
23723 dwarf2_per_cu_data *per_cu,
23724 dwarf2_per_objfile *per_objfile)
23725 {
23726 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23727 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23728 }
23729
23730 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23731 On entry *REF_CU is the CU of SRC_DIE.
23732 On exit *REF_CU is the CU of the result.
23733 Returns NULL if the referenced DIE isn't found. */
23734
23735 static struct die_info *
23736 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23737 struct dwarf2_cu **ref_cu)
23738 {
23739 struct die_info temp_die;
23740 struct dwarf2_cu *sig_cu;
23741 struct die_info *die;
23742 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23743
23744
23745 /* While it might be nice to assert sig_type->type == NULL here,
23746 we can get here for DW_AT_imported_declaration where we need
23747 the DIE not the type. */
23748
23749 /* If necessary, add it to the queue and load its DIEs.
23750
23751 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23752 it doesn't mean they are currently loaded. Since we require them
23753 to be loaded, we must check for ourselves. */
23754 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23755 language_minimal)
23756 || per_objfile->get_cu (sig_type) == nullptr)
23757 read_signatured_type (sig_type, per_objfile);
23758
23759 sig_cu = per_objfile->get_cu (sig_type);
23760 gdb_assert (sig_cu != NULL);
23761 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23762 temp_die.sect_off = sig_type->type_offset_in_section;
23763 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23764 to_underlying (temp_die.sect_off));
23765 if (die)
23766 {
23767 /* For .gdb_index version 7 keep track of included TUs.
23768 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23769 if (per_objfile->per_bfd->index_table != NULL
23770 && per_objfile->per_bfd->index_table->version <= 7)
23771 {
23772 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23773 }
23774
23775 *ref_cu = sig_cu;
23776 return die;
23777 }
23778
23779 return NULL;
23780 }
23781
23782 /* Follow signatured type referenced by ATTR in SRC_DIE.
23783 On entry *REF_CU is the CU of SRC_DIE.
23784 On exit *REF_CU is the CU of the result.
23785 The result is the DIE of the type.
23786 If the referenced type cannot be found an error is thrown. */
23787
23788 static struct die_info *
23789 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23790 struct dwarf2_cu **ref_cu)
23791 {
23792 ULONGEST signature = attr->as_signature ();
23793 struct signatured_type *sig_type;
23794 struct die_info *die;
23795
23796 gdb_assert (attr->form == DW_FORM_ref_sig8);
23797
23798 sig_type = lookup_signatured_type (*ref_cu, signature);
23799 /* sig_type will be NULL if the signatured type is missing from
23800 the debug info. */
23801 if (sig_type == NULL)
23802 {
23803 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23804 " from DIE at %s [in module %s]"),
23805 hex_string (signature), sect_offset_str (src_die->sect_off),
23806 objfile_name ((*ref_cu)->per_objfile->objfile));
23807 }
23808
23809 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23810 if (die == NULL)
23811 {
23812 dump_die_for_error (src_die);
23813 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23814 " from DIE at %s [in module %s]"),
23815 hex_string (signature), sect_offset_str (src_die->sect_off),
23816 objfile_name ((*ref_cu)->per_objfile->objfile));
23817 }
23818
23819 return die;
23820 }
23821
23822 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23823 reading in and processing the type unit if necessary. */
23824
23825 static struct type *
23826 get_signatured_type (struct die_info *die, ULONGEST signature,
23827 struct dwarf2_cu *cu)
23828 {
23829 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23830 struct signatured_type *sig_type;
23831 struct dwarf2_cu *type_cu;
23832 struct die_info *type_die;
23833 struct type *type;
23834
23835 sig_type = lookup_signatured_type (cu, signature);
23836 /* sig_type will be NULL if the signatured type is missing from
23837 the debug info. */
23838 if (sig_type == NULL)
23839 {
23840 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23841 " from DIE at %s [in module %s]"),
23842 hex_string (signature), sect_offset_str (die->sect_off),
23843 objfile_name (per_objfile->objfile));
23844 return build_error_marker_type (cu, die);
23845 }
23846
23847 /* If we already know the type we're done. */
23848 type = per_objfile->get_type_for_signatured_type (sig_type);
23849 if (type != nullptr)
23850 return type;
23851
23852 type_cu = cu;
23853 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23854 if (type_die != NULL)
23855 {
23856 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23857 is created. This is important, for example, because for c++ classes
23858 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23859 type = read_type_die (type_die, type_cu);
23860 if (type == NULL)
23861 {
23862 complaint (_("Dwarf Error: Cannot build signatured type %s"
23863 " referenced from DIE at %s [in module %s]"),
23864 hex_string (signature), sect_offset_str (die->sect_off),
23865 objfile_name (per_objfile->objfile));
23866 type = build_error_marker_type (cu, die);
23867 }
23868 }
23869 else
23870 {
23871 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23872 " from DIE at %s [in module %s]"),
23873 hex_string (signature), sect_offset_str (die->sect_off),
23874 objfile_name (per_objfile->objfile));
23875 type = build_error_marker_type (cu, die);
23876 }
23877
23878 per_objfile->set_type_for_signatured_type (sig_type, type);
23879
23880 return type;
23881 }
23882
23883 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23884 reading in and processing the type unit if necessary. */
23885
23886 static struct type *
23887 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23888 struct dwarf2_cu *cu) /* ARI: editCase function */
23889 {
23890 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23891 if (attr->form_is_ref ())
23892 {
23893 struct dwarf2_cu *type_cu = cu;
23894 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23895
23896 return read_type_die (type_die, type_cu);
23897 }
23898 else if (attr->form == DW_FORM_ref_sig8)
23899 {
23900 return get_signatured_type (die, attr->as_signature (), cu);
23901 }
23902 else
23903 {
23904 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23905
23906 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23907 " at %s [in module %s]"),
23908 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23909 objfile_name (per_objfile->objfile));
23910 return build_error_marker_type (cu, die);
23911 }
23912 }
23913
23914 /* Load the DIEs associated with type unit PER_CU into memory. */
23915
23916 static void
23917 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23918 dwarf2_per_objfile *per_objfile)
23919 {
23920 struct signatured_type *sig_type;
23921
23922 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23923 gdb_assert (! per_cu->type_unit_group_p ());
23924
23925 /* We have the per_cu, but we need the signatured_type.
23926 Fortunately this is an easy translation. */
23927 gdb_assert (per_cu->is_debug_types);
23928 sig_type = (struct signatured_type *) per_cu;
23929
23930 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23931
23932 read_signatured_type (sig_type, per_objfile);
23933
23934 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23935 }
23936
23937 /* Read in a signatured type and build its CU and DIEs.
23938 If the type is a stub for the real type in a DWO file,
23939 read in the real type from the DWO file as well. */
23940
23941 static void
23942 read_signatured_type (signatured_type *sig_type,
23943 dwarf2_per_objfile *per_objfile)
23944 {
23945 gdb_assert (sig_type->is_debug_types);
23946 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23947
23948 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23949
23950 if (!reader.dummy_p)
23951 {
23952 struct dwarf2_cu *cu = reader.cu;
23953 const gdb_byte *info_ptr = reader.info_ptr;
23954
23955 gdb_assert (cu->die_hash == NULL);
23956 cu->die_hash =
23957 htab_create_alloc_ex (cu->header.length / 12,
23958 die_hash,
23959 die_eq,
23960 NULL,
23961 &cu->comp_unit_obstack,
23962 hashtab_obstack_allocate,
23963 dummy_obstack_deallocate);
23964
23965 if (reader.comp_unit_die->has_children)
23966 reader.comp_unit_die->child
23967 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23968 reader.comp_unit_die);
23969 cu->dies = reader.comp_unit_die;
23970 /* comp_unit_die is not stored in die_hash, no need. */
23971
23972 /* We try not to read any attributes in this function, because
23973 not all CUs needed for references have been loaded yet, and
23974 symbol table processing isn't initialized. But we have to
23975 set the CU language, or we won't be able to build types
23976 correctly. Similarly, if we do not read the producer, we can
23977 not apply producer-specific interpretation. */
23978 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23979
23980 reader.keep ();
23981 }
23982
23983 sig_type->tu_read = 1;
23984 }
23985
23986 /* Decode simple location descriptions.
23987 Given a pointer to a dwarf block that defines a location, compute
23988 the location and return the value. If COMPUTED is non-null, it is
23989 set to true to indicate that decoding was successful, and false
23990 otherwise. If COMPUTED is null, then this function may emit a
23991 complaint. */
23992
23993 static CORE_ADDR
23994 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23995 {
23996 struct objfile *objfile = cu->per_objfile->objfile;
23997 size_t i;
23998 size_t size = blk->size;
23999 const gdb_byte *data = blk->data;
24000 CORE_ADDR stack[64];
24001 int stacki;
24002 unsigned int bytes_read, unsnd;
24003 gdb_byte op;
24004
24005 if (computed != nullptr)
24006 *computed = false;
24007
24008 i = 0;
24009 stacki = 0;
24010 stack[stacki] = 0;
24011 stack[++stacki] = 0;
24012
24013 while (i < size)
24014 {
24015 op = data[i++];
24016 switch (op)
24017 {
24018 case DW_OP_lit0:
24019 case DW_OP_lit1:
24020 case DW_OP_lit2:
24021 case DW_OP_lit3:
24022 case DW_OP_lit4:
24023 case DW_OP_lit5:
24024 case DW_OP_lit6:
24025 case DW_OP_lit7:
24026 case DW_OP_lit8:
24027 case DW_OP_lit9:
24028 case DW_OP_lit10:
24029 case DW_OP_lit11:
24030 case DW_OP_lit12:
24031 case DW_OP_lit13:
24032 case DW_OP_lit14:
24033 case DW_OP_lit15:
24034 case DW_OP_lit16:
24035 case DW_OP_lit17:
24036 case DW_OP_lit18:
24037 case DW_OP_lit19:
24038 case DW_OP_lit20:
24039 case DW_OP_lit21:
24040 case DW_OP_lit22:
24041 case DW_OP_lit23:
24042 case DW_OP_lit24:
24043 case DW_OP_lit25:
24044 case DW_OP_lit26:
24045 case DW_OP_lit27:
24046 case DW_OP_lit28:
24047 case DW_OP_lit29:
24048 case DW_OP_lit30:
24049 case DW_OP_lit31:
24050 stack[++stacki] = op - DW_OP_lit0;
24051 break;
24052
24053 case DW_OP_reg0:
24054 case DW_OP_reg1:
24055 case DW_OP_reg2:
24056 case DW_OP_reg3:
24057 case DW_OP_reg4:
24058 case DW_OP_reg5:
24059 case DW_OP_reg6:
24060 case DW_OP_reg7:
24061 case DW_OP_reg8:
24062 case DW_OP_reg9:
24063 case DW_OP_reg10:
24064 case DW_OP_reg11:
24065 case DW_OP_reg12:
24066 case DW_OP_reg13:
24067 case DW_OP_reg14:
24068 case DW_OP_reg15:
24069 case DW_OP_reg16:
24070 case DW_OP_reg17:
24071 case DW_OP_reg18:
24072 case DW_OP_reg19:
24073 case DW_OP_reg20:
24074 case DW_OP_reg21:
24075 case DW_OP_reg22:
24076 case DW_OP_reg23:
24077 case DW_OP_reg24:
24078 case DW_OP_reg25:
24079 case DW_OP_reg26:
24080 case DW_OP_reg27:
24081 case DW_OP_reg28:
24082 case DW_OP_reg29:
24083 case DW_OP_reg30:
24084 case DW_OP_reg31:
24085 stack[++stacki] = op - DW_OP_reg0;
24086 if (i < size)
24087 {
24088 if (computed == nullptr)
24089 dwarf2_complex_location_expr_complaint ();
24090 else
24091 return 0;
24092 }
24093 break;
24094
24095 case DW_OP_regx:
24096 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24097 i += bytes_read;
24098 stack[++stacki] = unsnd;
24099 if (i < size)
24100 {
24101 if (computed == nullptr)
24102 dwarf2_complex_location_expr_complaint ();
24103 else
24104 return 0;
24105 }
24106 break;
24107
24108 case DW_OP_addr:
24109 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24110 &bytes_read);
24111 i += bytes_read;
24112 break;
24113
24114 case DW_OP_const1u:
24115 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24116 i += 1;
24117 break;
24118
24119 case DW_OP_const1s:
24120 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24121 i += 1;
24122 break;
24123
24124 case DW_OP_const2u:
24125 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24126 i += 2;
24127 break;
24128
24129 case DW_OP_const2s:
24130 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24131 i += 2;
24132 break;
24133
24134 case DW_OP_const4u:
24135 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24136 i += 4;
24137 break;
24138
24139 case DW_OP_const4s:
24140 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24141 i += 4;
24142 break;
24143
24144 case DW_OP_const8u:
24145 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24146 i += 8;
24147 break;
24148
24149 case DW_OP_constu:
24150 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24151 &bytes_read);
24152 i += bytes_read;
24153 break;
24154
24155 case DW_OP_consts:
24156 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24157 i += bytes_read;
24158 break;
24159
24160 case DW_OP_dup:
24161 stack[stacki + 1] = stack[stacki];
24162 stacki++;
24163 break;
24164
24165 case DW_OP_plus:
24166 stack[stacki - 1] += stack[stacki];
24167 stacki--;
24168 break;
24169
24170 case DW_OP_plus_uconst:
24171 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24172 &bytes_read);
24173 i += bytes_read;
24174 break;
24175
24176 case DW_OP_minus:
24177 stack[stacki - 1] -= stack[stacki];
24178 stacki--;
24179 break;
24180
24181 case DW_OP_deref:
24182 /* If we're not the last op, then we definitely can't encode
24183 this using GDB's address_class enum. This is valid for partial
24184 global symbols, although the variable's address will be bogus
24185 in the psymtab. */
24186 if (i < size)
24187 {
24188 if (computed == nullptr)
24189 dwarf2_complex_location_expr_complaint ();
24190 else
24191 return 0;
24192 }
24193 break;
24194
24195 case DW_OP_GNU_push_tls_address:
24196 case DW_OP_form_tls_address:
24197 /* The top of the stack has the offset from the beginning
24198 of the thread control block at which the variable is located. */
24199 /* Nothing should follow this operator, so the top of stack would
24200 be returned. */
24201 /* This is valid for partial global symbols, but the variable's
24202 address will be bogus in the psymtab. Make it always at least
24203 non-zero to not look as a variable garbage collected by linker
24204 which have DW_OP_addr 0. */
24205 if (i < size)
24206 {
24207 if (computed == nullptr)
24208 dwarf2_complex_location_expr_complaint ();
24209 else
24210 return 0;
24211 }
24212 stack[stacki]++;
24213 break;
24214
24215 case DW_OP_GNU_uninit:
24216 if (computed != nullptr)
24217 return 0;
24218 break;
24219
24220 case DW_OP_addrx:
24221 case DW_OP_GNU_addr_index:
24222 case DW_OP_GNU_const_index:
24223 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24224 &bytes_read);
24225 i += bytes_read;
24226 break;
24227
24228 default:
24229 if (computed == nullptr)
24230 {
24231 const char *name = get_DW_OP_name (op);
24232
24233 if (name)
24234 complaint (_("unsupported stack op: '%s'"),
24235 name);
24236 else
24237 complaint (_("unsupported stack op: '%02x'"),
24238 op);
24239 }
24240
24241 return (stack[stacki]);
24242 }
24243
24244 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24245 outside of the allocated space. Also enforce minimum>0. */
24246 if (stacki >= ARRAY_SIZE (stack) - 1)
24247 {
24248 if (computed == nullptr)
24249 complaint (_("location description stack overflow"));
24250 return 0;
24251 }
24252
24253 if (stacki <= 0)
24254 {
24255 if (computed == nullptr)
24256 complaint (_("location description stack underflow"));
24257 return 0;
24258 }
24259 }
24260
24261 if (computed != nullptr)
24262 *computed = true;
24263 return (stack[stacki]);
24264 }
24265
24266 /* memory allocation interface */
24267
24268 static struct dwarf_block *
24269 dwarf_alloc_block (struct dwarf2_cu *cu)
24270 {
24271 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24272 }
24273
24274 static struct die_info *
24275 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24276 {
24277 struct die_info *die;
24278 size_t size = sizeof (struct die_info);
24279
24280 if (num_attrs > 1)
24281 size += (num_attrs - 1) * sizeof (struct attribute);
24282
24283 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24284 memset (die, 0, sizeof (struct die_info));
24285 return (die);
24286 }
24287
24288 \f
24289
24290 /* Macro support. */
24291
24292 /* An overload of dwarf_decode_macros that finds the correct section
24293 and ensures it is read in before calling the other overload. */
24294
24295 static void
24296 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24297 int section_is_gnu)
24298 {
24299 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24300 struct objfile *objfile = per_objfile->objfile;
24301 const struct line_header *lh = cu->line_header;
24302 unsigned int offset_size = cu->header.offset_size;
24303 struct dwarf2_section_info *section;
24304 const char *section_name;
24305
24306 if (cu->dwo_unit != nullptr)
24307 {
24308 if (section_is_gnu)
24309 {
24310 section = &cu->dwo_unit->dwo_file->sections.macro;
24311 section_name = ".debug_macro.dwo";
24312 }
24313 else
24314 {
24315 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24316 section_name = ".debug_macinfo.dwo";
24317 }
24318 }
24319 else
24320 {
24321 if (section_is_gnu)
24322 {
24323 section = &per_objfile->per_bfd->macro;
24324 section_name = ".debug_macro";
24325 }
24326 else
24327 {
24328 section = &per_objfile->per_bfd->macinfo;
24329 section_name = ".debug_macinfo";
24330 }
24331 }
24332
24333 section->read (objfile);
24334 if (section->buffer == nullptr)
24335 {
24336 complaint (_("missing %s section"), section_name);
24337 return;
24338 }
24339
24340 buildsym_compunit *builder = cu->get_builder ();
24341
24342 struct dwarf2_section_info *str_offsets_section;
24343 struct dwarf2_section_info *str_section;
24344 gdb::optional<ULONGEST> str_offsets_base;
24345
24346 if (cu->dwo_unit != nullptr)
24347 {
24348 str_offsets_section = &cu->dwo_unit->dwo_file
24349 ->sections.str_offsets;
24350 str_section = &cu->dwo_unit->dwo_file->sections.str;
24351 str_offsets_base = cu->header.addr_size;
24352 }
24353 else
24354 {
24355 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24356 str_section = &per_objfile->per_bfd->str;
24357 str_offsets_base = cu->str_offsets_base;
24358 }
24359
24360 dwarf_decode_macros (per_objfile, builder, section, lh,
24361 offset_size, offset, str_section, str_offsets_section,
24362 str_offsets_base, section_is_gnu);
24363 }
24364
24365 /* Return the .debug_loc section to use for CU.
24366 For DWO files use .debug_loc.dwo. */
24367
24368 static struct dwarf2_section_info *
24369 cu_debug_loc_section (struct dwarf2_cu *cu)
24370 {
24371 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24372
24373 if (cu->dwo_unit)
24374 {
24375 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24376
24377 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24378 }
24379 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24380 : &per_objfile->per_bfd->loc);
24381 }
24382
24383 /* Return the .debug_rnglists section to use for CU. */
24384 static struct dwarf2_section_info *
24385 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24386 {
24387 if (cu->header.version < 5)
24388 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24389 cu->header.version);
24390 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24391
24392 /* Make sure we read the .debug_rnglists section from the file that
24393 contains the DW_AT_ranges attribute we are reading. Normally that
24394 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24395 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24396 program. */
24397 if (cu->dwo_unit != nullptr
24398 && tag != DW_TAG_compile_unit
24399 && tag != DW_TAG_skeleton_unit)
24400 {
24401 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24402
24403 if (sections->rnglists.size > 0)
24404 return &sections->rnglists;
24405 else
24406 error (_(".debug_rnglists section is missing from .dwo file."));
24407 }
24408 return &dwarf2_per_objfile->per_bfd->rnglists;
24409 }
24410
24411 /* A helper function that fills in a dwarf2_loclist_baton. */
24412
24413 static void
24414 fill_in_loclist_baton (struct dwarf2_cu *cu,
24415 struct dwarf2_loclist_baton *baton,
24416 const struct attribute *attr)
24417 {
24418 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24419 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24420
24421 section->read (per_objfile->objfile);
24422
24423 baton->per_objfile = per_objfile;
24424 baton->per_cu = cu->per_cu;
24425 gdb_assert (baton->per_cu);
24426 /* We don't know how long the location list is, but make sure we
24427 don't run off the edge of the section. */
24428 baton->size = section->size - attr->as_unsigned ();
24429 baton->data = section->buffer + attr->as_unsigned ();
24430 if (cu->base_address.has_value ())
24431 baton->base_address = *cu->base_address;
24432 else
24433 baton->base_address = 0;
24434 baton->from_dwo = cu->dwo_unit != NULL;
24435 }
24436
24437 static void
24438 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24439 struct dwarf2_cu *cu, int is_block)
24440 {
24441 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24442 struct objfile *objfile = per_objfile->objfile;
24443 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24444
24445 if (attr->form_is_section_offset ()
24446 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24447 the section. If so, fall through to the complaint in the
24448 other branch. */
24449 && attr->as_unsigned () < section->get_size (objfile))
24450 {
24451 struct dwarf2_loclist_baton *baton;
24452
24453 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24454
24455 fill_in_loclist_baton (cu, baton, attr);
24456
24457 if (!cu->base_address.has_value ())
24458 complaint (_("Location list used without "
24459 "specifying the CU base address."));
24460
24461 sym->set_aclass_index ((is_block
24462 ? dwarf2_loclist_block_index
24463 : dwarf2_loclist_index));
24464 SYMBOL_LOCATION_BATON (sym) = baton;
24465 }
24466 else
24467 {
24468 struct dwarf2_locexpr_baton *baton;
24469
24470 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24471 baton->per_objfile = per_objfile;
24472 baton->per_cu = cu->per_cu;
24473 gdb_assert (baton->per_cu);
24474
24475 if (attr->form_is_block ())
24476 {
24477 /* Note that we're just copying the block's data pointer
24478 here, not the actual data. We're still pointing into the
24479 info_buffer for SYM's objfile; right now we never release
24480 that buffer, but when we do clean up properly this may
24481 need to change. */
24482 struct dwarf_block *block = attr->as_block ();
24483 baton->size = block->size;
24484 baton->data = block->data;
24485 }
24486 else
24487 {
24488 dwarf2_invalid_attrib_class_complaint ("location description",
24489 sym->natural_name ());
24490 baton->size = 0;
24491 }
24492
24493 sym->set_aclass_index ((is_block
24494 ? dwarf2_locexpr_block_index
24495 : dwarf2_locexpr_index));
24496 SYMBOL_LOCATION_BATON (sym) = baton;
24497 }
24498 }
24499
24500 /* See read.h. */
24501
24502 const comp_unit_head *
24503 dwarf2_per_cu_data::get_header () const
24504 {
24505 if (!m_header_read_in)
24506 {
24507 const gdb_byte *info_ptr
24508 = this->section->buffer + to_underlying (this->sect_off);
24509
24510 memset (&m_header, 0, sizeof (m_header));
24511
24512 read_comp_unit_head (&m_header, info_ptr, this->section,
24513 rcuh_kind::COMPILE);
24514
24515 m_header_read_in = true;
24516 }
24517
24518 return &m_header;
24519 }
24520
24521 /* See read.h. */
24522
24523 int
24524 dwarf2_per_cu_data::addr_size () const
24525 {
24526 return this->get_header ()->addr_size;
24527 }
24528
24529 /* See read.h. */
24530
24531 int
24532 dwarf2_per_cu_data::offset_size () const
24533 {
24534 return this->get_header ()->offset_size;
24535 }
24536
24537 /* See read.h. */
24538
24539 int
24540 dwarf2_per_cu_data::ref_addr_size () const
24541 {
24542 const comp_unit_head *header = this->get_header ();
24543
24544 if (header->version == 2)
24545 return header->addr_size;
24546 else
24547 return header->offset_size;
24548 }
24549
24550 /* A helper function for dwarf2_find_containing_comp_unit that returns
24551 the index of the result, and that searches a vector. It will
24552 return a result even if the offset in question does not actually
24553 occur in any CU. This is separate so that it can be unit
24554 tested. */
24555
24556 static int
24557 dwarf2_find_containing_comp_unit
24558 (sect_offset sect_off,
24559 unsigned int offset_in_dwz,
24560 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24561 {
24562 int low, high;
24563
24564 low = 0;
24565 high = all_comp_units.size () - 1;
24566 while (high > low)
24567 {
24568 struct dwarf2_per_cu_data *mid_cu;
24569 int mid = low + (high - low) / 2;
24570
24571 mid_cu = all_comp_units[mid].get ();
24572 if (mid_cu->is_dwz > offset_in_dwz
24573 || (mid_cu->is_dwz == offset_in_dwz
24574 && mid_cu->sect_off + mid_cu->length > sect_off))
24575 high = mid;
24576 else
24577 low = mid + 1;
24578 }
24579 gdb_assert (low == high);
24580 return low;
24581 }
24582
24583 /* Locate the .debug_info compilation unit from CU's objfile which contains
24584 the DIE at OFFSET. Raises an error on failure. */
24585
24586 static struct dwarf2_per_cu_data *
24587 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24588 unsigned int offset_in_dwz,
24589 dwarf2_per_bfd *per_bfd)
24590 {
24591 int low = dwarf2_find_containing_comp_unit
24592 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
24593 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
24594
24595 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24596 {
24597 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24598 error (_("Dwarf Error: could not find partial DIE containing "
24599 "offset %s [in module %s]"),
24600 sect_offset_str (sect_off),
24601 bfd_get_filename (per_bfd->obfd));
24602
24603 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
24604 <= sect_off);
24605 return per_bfd->all_comp_units[low - 1].get ();
24606 }
24607 else
24608 {
24609 if (low == per_bfd->all_comp_units.size () - 1
24610 && sect_off >= this_cu->sect_off + this_cu->length)
24611 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24612 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24613 return this_cu;
24614 }
24615 }
24616
24617 #if GDB_SELF_TEST
24618
24619 namespace selftests {
24620 namespace find_containing_comp_unit {
24621
24622 static void
24623 run_test ()
24624 {
24625 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24626 dwarf2_per_cu_data *one_ptr = one.get ();
24627 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24628 dwarf2_per_cu_data *two_ptr = two.get ();
24629 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24630 dwarf2_per_cu_data *three_ptr = three.get ();
24631 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24632 dwarf2_per_cu_data *four_ptr = four.get ();
24633
24634 one->length = 5;
24635 two->sect_off = sect_offset (one->length);
24636 two->length = 7;
24637
24638 three->length = 5;
24639 three->is_dwz = 1;
24640 four->sect_off = sect_offset (three->length);
24641 four->length = 7;
24642 four->is_dwz = 1;
24643
24644 std::vector<dwarf2_per_cu_data_up> units;
24645 units.push_back (std::move (one));
24646 units.push_back (std::move (two));
24647 units.push_back (std::move (three));
24648 units.push_back (std::move (four));
24649
24650 int result;
24651
24652 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24653 SELF_CHECK (units[result].get () == one_ptr);
24654 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24655 SELF_CHECK (units[result].get () == one_ptr);
24656 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24657 SELF_CHECK (units[result].get () == two_ptr);
24658
24659 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24660 SELF_CHECK (units[result].get () == three_ptr);
24661 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24662 SELF_CHECK (units[result].get () == three_ptr);
24663 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24664 SELF_CHECK (units[result].get () == four_ptr);
24665 }
24666
24667 }
24668 }
24669
24670 #endif /* GDB_SELF_TEST */
24671
24672 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24673
24674 static void
24675 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24676 enum language pretend_language)
24677 {
24678 struct attribute *attr;
24679
24680 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24681
24682 /* Set the language we're debugging. */
24683 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24684 if (cu->producer != nullptr
24685 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
24686 {
24687 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24688 attribute is not standardised yet. As a workaround for the
24689 language detection we fall back to the DW_AT_producer
24690 string. */
24691 cu->per_cu->lang = language_opencl;
24692 }
24693 else if (cu->producer != nullptr
24694 && strstr (cu->producer, "GNU Go ") != NULL)
24695 {
24696 /* Similar hack for Go. */
24697 cu->per_cu->lang = language_go;
24698 }
24699 else if (attr != nullptr)
24700 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
24701 else
24702 cu->per_cu->lang = pretend_language;
24703 cu->language_defn = language_def (cu->per_cu->lang);
24704 }
24705
24706 /* See read.h. */
24707
24708 dwarf2_cu *
24709 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24710 {
24711 auto it = m_dwarf2_cus.find (per_cu);
24712 if (it == m_dwarf2_cus.end ())
24713 return nullptr;
24714
24715 return it->second;
24716 }
24717
24718 /* See read.h. */
24719
24720 void
24721 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24722 {
24723 gdb_assert (this->get_cu (per_cu) == nullptr);
24724
24725 m_dwarf2_cus[per_cu] = cu;
24726 }
24727
24728 /* See read.h. */
24729
24730 void
24731 dwarf2_per_objfile::age_comp_units ()
24732 {
24733 dwarf_read_debug_printf_v ("running");
24734
24735 /* This is not expected to be called in the middle of CU expansion. There is
24736 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24737 loaded in memory. Calling age_comp_units while the queue is in use could
24738 make us free the DIEs for a CU that is in the queue and therefore break
24739 that invariant. */
24740 gdb_assert (!this->per_bfd->queue.has_value ());
24741
24742 /* Start by clearing all marks. */
24743 for (auto pair : m_dwarf2_cus)
24744 pair.second->clear_mark ();
24745
24746 /* Traverse all CUs, mark them and their dependencies if used recently
24747 enough. */
24748 for (auto pair : m_dwarf2_cus)
24749 {
24750 dwarf2_cu *cu = pair.second;
24751
24752 cu->last_used++;
24753 if (cu->last_used <= dwarf_max_cache_age)
24754 cu->mark ();
24755 }
24756
24757 /* Delete all CUs still not marked. */
24758 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24759 {
24760 dwarf2_cu *cu = it->second;
24761
24762 if (!cu->is_marked ())
24763 {
24764 dwarf_read_debug_printf_v ("deleting old CU %s",
24765 sect_offset_str (cu->per_cu->sect_off));
24766 delete cu;
24767 it = m_dwarf2_cus.erase (it);
24768 }
24769 else
24770 it++;
24771 }
24772 }
24773
24774 /* See read.h. */
24775
24776 void
24777 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24778 {
24779 auto it = m_dwarf2_cus.find (per_cu);
24780 if (it == m_dwarf2_cus.end ())
24781 return;
24782
24783 delete it->second;
24784
24785 m_dwarf2_cus.erase (it);
24786 }
24787
24788 dwarf2_per_objfile::~dwarf2_per_objfile ()
24789 {
24790 remove_all_cus ();
24791 }
24792
24793 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24794 We store these in a hash table separate from the DIEs, and preserve them
24795 when the DIEs are flushed out of cache.
24796
24797 The CU "per_cu" pointer is needed because offset alone is not enough to
24798 uniquely identify the type. A file may have multiple .debug_types sections,
24799 or the type may come from a DWO file. Furthermore, while it's more logical
24800 to use per_cu->section+offset, with Fission the section with the data is in
24801 the DWO file but we don't know that section at the point we need it.
24802 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24803 because we can enter the lookup routine, get_die_type_at_offset, from
24804 outside this file, and thus won't necessarily have PER_CU->cu.
24805 Fortunately, PER_CU is stable for the life of the objfile. */
24806
24807 struct dwarf2_per_cu_offset_and_type
24808 {
24809 const struct dwarf2_per_cu_data *per_cu;
24810 sect_offset sect_off;
24811 struct type *type;
24812 };
24813
24814 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24815
24816 static hashval_t
24817 per_cu_offset_and_type_hash (const void *item)
24818 {
24819 const struct dwarf2_per_cu_offset_and_type *ofs
24820 = (const struct dwarf2_per_cu_offset_and_type *) item;
24821
24822 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24823 }
24824
24825 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24826
24827 static int
24828 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24829 {
24830 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24831 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24832 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24833 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24834
24835 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24836 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24837 }
24838
24839 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24840 table if necessary. For convenience, return TYPE.
24841
24842 The DIEs reading must have careful ordering to:
24843 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24844 reading current DIE.
24845 * Not trying to dereference contents of still incompletely read in types
24846 while reading in other DIEs.
24847 * Enable referencing still incompletely read in types just by a pointer to
24848 the type without accessing its fields.
24849
24850 Therefore caller should follow these rules:
24851 * Try to fetch any prerequisite types we may need to build this DIE type
24852 before building the type and calling set_die_type.
24853 * After building type call set_die_type for current DIE as soon as
24854 possible before fetching more types to complete the current type.
24855 * Make the type as complete as possible before fetching more types. */
24856
24857 static struct type *
24858 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24859 bool skip_data_location)
24860 {
24861 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24862 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24863 struct objfile *objfile = per_objfile->objfile;
24864 struct attribute *attr;
24865 struct dynamic_prop prop;
24866
24867 /* For Ada types, make sure that the gnat-specific data is always
24868 initialized (if not already set). There are a few types where
24869 we should not be doing so, because the type-specific area is
24870 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24871 where the type-specific area is used to store the floatformat).
24872 But this is not a problem, because the gnat-specific information
24873 is actually not needed for these types. */
24874 if (need_gnat_info (cu)
24875 && type->code () != TYPE_CODE_FUNC
24876 && type->code () != TYPE_CODE_FLT
24877 && type->code () != TYPE_CODE_METHODPTR
24878 && type->code () != TYPE_CODE_MEMBERPTR
24879 && type->code () != TYPE_CODE_METHOD
24880 && type->code () != TYPE_CODE_FIXED_POINT
24881 && !HAVE_GNAT_AUX_INFO (type))
24882 INIT_GNAT_SPECIFIC (type);
24883
24884 /* Read DW_AT_allocated and set in type. */
24885 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24886 if (attr != NULL)
24887 {
24888 struct type *prop_type = cu->addr_sized_int_type (false);
24889 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24890 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24891 }
24892
24893 /* Read DW_AT_associated and set in type. */
24894 attr = dwarf2_attr (die, DW_AT_associated, cu);
24895 if (attr != NULL)
24896 {
24897 struct type *prop_type = cu->addr_sized_int_type (false);
24898 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24899 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24900 }
24901
24902 /* Read DW_AT_rank and set in type. */
24903 attr = dwarf2_attr (die, DW_AT_rank, cu);
24904 if (attr != NULL)
24905 {
24906 struct type *prop_type = cu->addr_sized_int_type (false);
24907 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24908 type->add_dyn_prop (DYN_PROP_RANK, prop);
24909 }
24910
24911 /* Read DW_AT_data_location and set in type. */
24912 if (!skip_data_location)
24913 {
24914 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24915 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24916 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24917 }
24918
24919 if (per_objfile->die_type_hash == NULL)
24920 per_objfile->die_type_hash
24921 = htab_up (htab_create_alloc (127,
24922 per_cu_offset_and_type_hash,
24923 per_cu_offset_and_type_eq,
24924 NULL, xcalloc, xfree));
24925
24926 ofs.per_cu = cu->per_cu;
24927 ofs.sect_off = die->sect_off;
24928 ofs.type = type;
24929 slot = (struct dwarf2_per_cu_offset_and_type **)
24930 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24931 if (*slot)
24932 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24933 sect_offset_str (die->sect_off));
24934 *slot = XOBNEW (&objfile->objfile_obstack,
24935 struct dwarf2_per_cu_offset_and_type);
24936 **slot = ofs;
24937 return type;
24938 }
24939
24940 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24941 or return NULL if the die does not have a saved type. */
24942
24943 static struct type *
24944 get_die_type_at_offset (sect_offset sect_off,
24945 dwarf2_per_cu_data *per_cu,
24946 dwarf2_per_objfile *per_objfile)
24947 {
24948 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24949
24950 if (per_objfile->die_type_hash == NULL)
24951 return NULL;
24952
24953 ofs.per_cu = per_cu;
24954 ofs.sect_off = sect_off;
24955 slot = ((struct dwarf2_per_cu_offset_and_type *)
24956 htab_find (per_objfile->die_type_hash.get (), &ofs));
24957 if (slot)
24958 return slot->type;
24959 else
24960 return NULL;
24961 }
24962
24963 /* Look up the type for DIE in CU in die_type_hash,
24964 or return NULL if DIE does not have a saved type. */
24965
24966 static struct type *
24967 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24968 {
24969 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24970 }
24971
24972 /* Trivial hash function for partial_die_info: the hash value of a DIE
24973 is its offset in .debug_info for this objfile. */
24974
24975 static hashval_t
24976 partial_die_hash (const void *item)
24977 {
24978 const struct partial_die_info *part_die
24979 = (const struct partial_die_info *) item;
24980
24981 return to_underlying (part_die->sect_off);
24982 }
24983
24984 /* Trivial comparison function for partial_die_info structures: two DIEs
24985 are equal if they have the same offset. */
24986
24987 static int
24988 partial_die_eq (const void *item_lhs, const void *item_rhs)
24989 {
24990 const struct partial_die_info *part_die_lhs
24991 = (const struct partial_die_info *) item_lhs;
24992 const struct partial_die_info *part_die_rhs
24993 = (const struct partial_die_info *) item_rhs;
24994
24995 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24996 }
24997
24998 struct cmd_list_element *set_dwarf_cmdlist;
24999 struct cmd_list_element *show_dwarf_cmdlist;
25000
25001 static void
25002 show_check_physname (struct ui_file *file, int from_tty,
25003 struct cmd_list_element *c, const char *value)
25004 {
25005 gdb_printf (file,
25006 _("Whether to check \"physname\" is %s.\n"),
25007 value);
25008 }
25009
25010 void _initialize_dwarf2_read ();
25011 void
25012 _initialize_dwarf2_read ()
25013 {
25014 add_setshow_prefix_cmd ("dwarf", class_maintenance,
25015 _("\
25016 Set DWARF specific variables.\n\
25017 Configure DWARF variables such as the cache size."),
25018 _("\
25019 Show DWARF specific variables.\n\
25020 Show DWARF variables such as the cache size."),
25021 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
25022 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
25023
25024 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25025 &dwarf_max_cache_age, _("\
25026 Set the upper bound on the age of cached DWARF compilation units."), _("\
25027 Show the upper bound on the age of cached DWARF compilation units."), _("\
25028 A higher limit means that cached compilation units will be stored\n\
25029 in memory longer, and more total memory will be used. Zero disables\n\
25030 caching, which can slow down startup."),
25031 NULL,
25032 show_dwarf_max_cache_age,
25033 &set_dwarf_cmdlist,
25034 &show_dwarf_cmdlist);
25035
25036 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25037 Set debugging of the DWARF reader."), _("\
25038 Show debugging of the DWARF reader."), _("\
25039 When enabled (non-zero), debugging messages are printed during DWARF\n\
25040 reading and symtab expansion. A value of 1 (one) provides basic\n\
25041 information. A value greater than 1 provides more verbose information."),
25042 NULL,
25043 NULL,
25044 &setdebuglist, &showdebuglist);
25045
25046 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25047 Set debugging of the DWARF DIE reader."), _("\
25048 Show debugging of the DWARF DIE reader."), _("\
25049 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25050 The value is the maximum depth to print."),
25051 NULL,
25052 NULL,
25053 &setdebuglist, &showdebuglist);
25054
25055 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25056 Set debugging of the dwarf line reader."), _("\
25057 Show debugging of the dwarf line reader."), _("\
25058 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25059 A value of 1 (one) provides basic information.\n\
25060 A value greater than 1 provides more verbose information."),
25061 NULL,
25062 NULL,
25063 &setdebuglist, &showdebuglist);
25064
25065 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25066 Set cross-checking of \"physname\" code against demangler."), _("\
25067 Show cross-checking of \"physname\" code against demangler."), _("\
25068 When enabled, GDB's internal \"physname\" code is checked against\n\
25069 the demangler."),
25070 NULL, show_check_physname,
25071 &setdebuglist, &showdebuglist);
25072
25073 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25074 no_class, &use_deprecated_index_sections, _("\
25075 Set whether to use deprecated gdb_index sections."), _("\
25076 Show whether to use deprecated gdb_index sections."), _("\
25077 When enabled, deprecated .gdb_index sections are used anyway.\n\
25078 Normally they are ignored either because of a missing feature or\n\
25079 performance issue.\n\
25080 Warning: This option must be enabled before gdb reads the file."),
25081 NULL,
25082 NULL,
25083 &setlist, &showlist);
25084
25085 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25086 &dwarf2_locexpr_funcs);
25087 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25088 &dwarf2_loclist_funcs);
25089
25090 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25091 &dwarf2_block_frame_base_locexpr_funcs);
25092 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25093 &dwarf2_block_frame_base_loclist_funcs);
25094
25095 #if GDB_SELF_TEST
25096 selftests::register_test ("dw2_expand_symtabs_matching",
25097 selftests::dw2_expand_symtabs_matching::run_test);
25098 selftests::register_test ("dwarf2_find_containing_comp_unit",
25099 selftests::find_containing_comp_unit::run_test);
25100 #endif
25101 }