]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
gdb: remove SYMBOL_ACLASS_INDEX macro, add getter/setter
[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 dwarf2read.h. */
366
367 dwarf2_per_objfile *
368 get_dwarf2_per_objfile (struct objfile *objfile)
369 {
370 return dwarf2_objfile_data_key.get (objfile);
371 }
372
373 /* Default names of the debugging sections. */
374
375 /* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
378 const struct dwarf2_debug_sections dwarf2_elf_names =
379 {
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
384 { ".debug_loclists", ".zdebug_loclists" },
385 { ".debug_macinfo", ".zdebug_macinfo" },
386 { ".debug_macro", ".zdebug_macro" },
387 { ".debug_str", ".zdebug_str" },
388 { ".debug_str_offsets", ".zdebug_str_offsets" },
389 { ".debug_line_str", ".zdebug_line_str" },
390 { ".debug_ranges", ".zdebug_ranges" },
391 { ".debug_rnglists", ".zdebug_rnglists" },
392 { ".debug_types", ".zdebug_types" },
393 { ".debug_addr", ".zdebug_addr" },
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
396 { ".gdb_index", ".zgdb_index" },
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
399 23
400 };
401
402 /* List of DWO/DWP sections. */
403
404 static const struct dwop_section_names
405 {
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
410 struct dwarf2_section_names loclists_dwo;
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
413 struct dwarf2_section_names rnglists_dwo;
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
419 }
420 dwop_section_names =
421 {
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
435 };
436
437 /* local data types */
438
439 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441 struct loclists_rnglists_header
442 {
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461 };
462
463 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466 struct stmt_list_hash
467 {
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
472 sect_offset line_sect_off;
473 };
474
475 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
479
480 struct type_unit_group : public dwarf2_per_cu_data
481 {
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
485 std::vector<signatured_type *> *tus = nullptr;
486
487 /* The data used to construct the hash key. */
488 struct stmt_list_hash hash {};
489 };
490
491 /* These sections are what may appear in a (real or virtual) DWO file. */
492
493 struct dwo_sections
494 {
495 struct dwarf2_section_info abbrev;
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
498 struct dwarf2_section_info loclists;
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
501 struct dwarf2_section_info rnglists;
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
506 std::vector<dwarf2_section_info> types;
507 };
508
509 /* CUs/TUs in DWP/DWO files. */
510
511 struct dwo_unit
512 {
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
522 struct dwarf2_section_info *section;
523
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530 };
531
532 /* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536 enum dwp_v2_section_ids
537 {
538 DW_SECT_MIN = 1
539 };
540
541 /* Data for one DWO file.
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
551
552 struct dwo_file
553 {
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
561 const char *dwo_name = nullptr;
562
563 /* The DW_AT_comp_dir attribute. */
564 const char *comp_dir = nullptr;
565
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
568 gdb_bfd_ref_ptr dbfd;
569
570 /* The sections that make up this DWO file.
571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
572 sections (for lack of a better name). */
573 struct dwo_sections sections {};
574
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
579 htab_up cus;
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
583 htab_up tus;
584 };
585
586 /* These sections are what may appear in a DWP file. */
587
588 struct dwp_sections
589 {
590 /* These are used by all DWP versions (1, 2 and 5). */
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
594
595 /* These are only used by DWP version 2 and version 5 files.
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
607 struct dwarf2_section_info loclists;
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
610 struct dwarf2_section_info rnglists;
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
613 };
614
615 /* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
617
618 struct virtual_v1_dwo_sections
619 {
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
627 That is recorded here, and copied to dwo_unit.section. */
628 struct dwarf2_section_info info_or_types;
629 };
630
631 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
636 struct virtual_v2_or_v5_dwo_sections
637 {
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666 };
667
668 /* Contents of DWP hash tables. */
669
670 struct dwp_hash_table
671 {
672 uint32_t version, nr_columns;
673 uint32_t nr_units, nr_slots;
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685 #define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700 #define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
712 } section_pool;
713 };
714
715 /* Data for one DWP file. */
716
717 struct dwp_file
718 {
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
725 /* Name of the file. */
726 const char *name;
727
728 /* File format version. */
729 int version = 0;
730
731 /* The bfd. */
732 gdb_bfd_ref_ptr dbfd;
733
734 /* Section info for this file. */
735 struct dwp_sections sections {};
736
737 /* Table of CUs in the file. */
738 const struct dwp_hash_table *cus = nullptr;
739
740 /* Table of TUs in the file. */
741 const struct dwp_hash_table *tus = nullptr;
742
743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
744 htab_up loaded_cus;
745 htab_up loaded_tus;
746
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
751 };
752
753 /* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
756 struct exists to abstract away the constant parameters of die reading. */
757
758 struct die_reader_specs
759 {
760 /* The bfd of die_section. */
761 bfd *abfd;
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
767 struct dwo_file *dwo_file;
768
769 /* The section the die comes from.
770 This is either .debug_info or .debug_types, or the .dwo variants. */
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
774 const gdb_byte *buffer;
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
778
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
781 };
782
783 /* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786 class cutu_reader : public die_reader_specs
787 {
788 public:
789
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
792 struct abbrev_table *abbrev_table,
793 dwarf2_cu *existing_cu,
794 bool skip_partial);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
797 dwarf2_per_objfile *per_objfile,
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
805 bool dummy_p = false;
806
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
811 private:
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
814 dwarf2_cu *existing_cu);
815
816 struct dwarf2_per_cu_data *m_this_cu;
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824 };
825
826 /* When we construct a partial symbol table entry we only
827 need this much information. */
828 struct partial_die_info : public allocate_on_obstack
829 {
830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
835 partial_die_info (const partial_die_info &) = default;
836
837 /* Adjust the partial die before generating a symbol for it. This
838 function may set the is_external flag or change the DIE's
839 name. */
840 void fixup (struct dwarf2_cu *cu);
841
842 /* Read a minimal amount of information into the minimal die
843 structure. */
844 const gdb_byte *read (const struct die_reader_specs *reader,
845 const struct abbrev_info &abbrev,
846 const gdb_byte *info_ptr);
847
848 /* Compute the name of this partial DIE. This memoizes the
849 result, so it is safe to call multiple times. */
850 const char *name (dwarf2_cu *cu);
851
852 /* Offset of this DIE. */
853 const sect_offset sect_off;
854
855 /* DWARF-2 tag for this DIE. */
856 const ENUM_BITFIELD(dwarf_tag) tag : 16;
857
858 /* Assorted flags describing the data found in this DIE. */
859 const unsigned int has_children : 1;
860
861 unsigned int is_external : 1;
862 unsigned int is_declaration : 1;
863 unsigned int has_type : 1;
864 unsigned int has_specification : 1;
865 unsigned int has_pc_info : 1;
866 unsigned int has_range_info : 1;
867 unsigned int may_be_inlined : 1;
868
869 /* This DIE has been marked DW_AT_main_subprogram. */
870 unsigned int main_subprogram : 1;
871
872 /* Flag set if the SCOPE field of this structure has been
873 computed. */
874 unsigned int scope_set : 1;
875
876 /* Flag set if the DIE has a byte_size attribute. */
877 unsigned int has_byte_size : 1;
878
879 /* Flag set if the DIE has a DW_AT_const_value attribute. */
880 unsigned int has_const_value : 1;
881
882 /* Flag set if any of the DIE's children are template arguments. */
883 unsigned int has_template_arguments : 1;
884
885 /* Flag set if fixup has been called on this die. */
886 unsigned int fixup_called : 1;
887
888 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
889 unsigned int is_dwz : 1;
890
891 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
892 unsigned int spec_is_dwz : 1;
893
894 unsigned int canonical_name : 1;
895
896 /* The name of this DIE. Normally the value of DW_AT_name, but
897 sometimes a default name for unnamed DIEs. */
898 const char *raw_name = nullptr;
899
900 /* The linkage name, if present. */
901 const char *linkage_name = nullptr;
902
903 /* The scope to prepend to our children. This is generally
904 allocated on the comp_unit_obstack, so will disappear
905 when this compilation unit leaves the cache. */
906 const char *scope = nullptr;
907
908 /* Some data associated with the partial DIE. The tag determines
909 which field is live. */
910 union
911 {
912 /* The location description associated with this DIE, if any. */
913 struct dwarf_block *locdesc;
914 /* The offset of an import, for DW_TAG_imported_unit. */
915 sect_offset sect_off;
916 } d {};
917
918 union
919 {
920 /* If HAS_PC_INFO, the PC range associated with this DIE. */
921 struct
922 {
923 CORE_ADDR lowpc;
924 CORE_ADDR highpc;
925 };
926 /* If HAS_RANGE_INFO, the ranges offset associated with this DIE. */
927 ULONGEST ranges_offset;
928 };
929
930 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
931 DW_AT_sibling, if any. */
932 /* NOTE: This member isn't strictly necessary, partial_die_info::read
933 could return DW_AT_sibling values to its caller load_partial_dies. */
934 const gdb_byte *sibling = nullptr;
935
936 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
937 DW_AT_specification (or DW_AT_abstract_origin or
938 DW_AT_extension). */
939 sect_offset spec_offset {};
940
941 /* Pointers to this DIE's parent, first child, and next sibling,
942 if any. */
943 struct partial_die_info *die_parent = nullptr;
944 struct partial_die_info *die_child = nullptr;
945 struct partial_die_info *die_sibling = nullptr;
946
947 friend struct partial_die_info *
948 dwarf2_cu::find_partial_die (sect_offset sect_off);
949
950 private:
951 /* Only need to do look up in dwarf2_cu::find_partial_die. */
952 partial_die_info (sect_offset sect_off)
953 : partial_die_info (sect_off, DW_TAG_padding, 0)
954 {
955 }
956
957 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
958 int has_children_)
959 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
960 {
961 is_external = 0;
962 is_declaration = 0;
963 has_type = 0;
964 has_specification = 0;
965 has_pc_info = 0;
966 has_range_info = 0;
967 may_be_inlined = 0;
968 main_subprogram = 0;
969 scope_set = 0;
970 has_byte_size = 0;
971 has_const_value = 0;
972 has_template_arguments = 0;
973 fixup_called = 0;
974 is_dwz = 0;
975 spec_is_dwz = 0;
976 canonical_name = 0;
977 /* Don't set these using NSDMI (Non-static data member initialisation),
978 because g++-4.8 will error out. */
979 lowpc = 0;
980 highpc = 0;
981 }
982 };
983
984 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
985 but this would require a corresponding change in unpack_field_as_long
986 and friends. */
987 static int bits_per_byte = 8;
988
989 struct variant_part_builder;
990
991 /* When reading a variant, we track a bit more information about the
992 field, and store it in an object of this type. */
993
994 struct variant_field
995 {
996 int first_field = -1;
997 int last_field = -1;
998
999 /* A variant can contain other variant parts. */
1000 std::vector<variant_part_builder> variant_parts;
1001
1002 /* If we see a DW_TAG_variant, then this will be set if this is the
1003 default branch. */
1004 bool default_branch = false;
1005 /* If we see a DW_AT_discr_value, then this will be the discriminant
1006 value. */
1007 ULONGEST discriminant_value = 0;
1008 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1009 data. */
1010 struct dwarf_block *discr_list_data = nullptr;
1011 };
1012
1013 /* This represents a DW_TAG_variant_part. */
1014
1015 struct variant_part_builder
1016 {
1017 /* The offset of the discriminant field. */
1018 sect_offset discriminant_offset {};
1019
1020 /* Variants that are direct children of this variant part. */
1021 std::vector<variant_field> variants;
1022
1023 /* True if we're currently reading a variant. */
1024 bool processing_variant = false;
1025 };
1026
1027 struct nextfield
1028 {
1029 int accessibility = 0;
1030 int virtuality = 0;
1031 /* Variant parts need to find the discriminant, which is a DIE
1032 reference. We track the section offset of each field to make
1033 this link. */
1034 sect_offset offset;
1035 struct field field {};
1036 };
1037
1038 struct fnfieldlist
1039 {
1040 const char *name = nullptr;
1041 std::vector<struct fn_field> fnfields;
1042 };
1043
1044 /* The routines that read and process dies for a C struct or C++ class
1045 pass lists of data member fields and lists of member function fields
1046 in an instance of a field_info structure, as defined below. */
1047 struct field_info
1048 {
1049 /* List of data member and baseclasses fields. */
1050 std::vector<struct nextfield> fields;
1051 std::vector<struct nextfield> baseclasses;
1052
1053 /* Set if the accessibility of one of the fields is not public. */
1054 bool non_public_fields = false;
1055
1056 /* Member function fieldlist array, contains name of possibly overloaded
1057 member function, number of overloaded member functions and a pointer
1058 to the head of the member function field chain. */
1059 std::vector<struct fnfieldlist> fnfieldlists;
1060
1061 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1062 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1063 std::vector<struct decl_field> typedef_field_list;
1064
1065 /* Nested types defined by this class and the number of elements in this
1066 list. */
1067 std::vector<struct decl_field> nested_types_list;
1068
1069 /* If non-null, this is the variant part we are currently
1070 reading. */
1071 variant_part_builder *current_variant_part = nullptr;
1072 /* This holds all the top-level variant parts attached to the type
1073 we're reading. */
1074 std::vector<variant_part_builder> variant_parts;
1075
1076 /* Return the total number of fields (including baseclasses). */
1077 int nfields () const
1078 {
1079 return fields.size () + baseclasses.size ();
1080 }
1081 };
1082
1083 /* Loaded secondary compilation units are kept in memory until they
1084 have not been referenced for the processing of this many
1085 compilation units. Set this to zero to disable caching. Cache
1086 sizes of up to at least twenty will improve startup time for
1087 typical inter-CU-reference binaries, at an obvious memory cost. */
1088 static int dwarf_max_cache_age = 5;
1089 static void
1090 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1091 struct cmd_list_element *c, const char *value)
1092 {
1093 fprintf_filtered (file, _("The upper bound on the age of cached "
1094 "DWARF compilation units is %s.\n"),
1095 value);
1096 }
1097 \f
1098 /* local function prototypes */
1099
1100 static void dwarf2_find_base_address (struct die_info *die,
1101 struct dwarf2_cu *cu);
1102
1103 static dwarf2_psymtab *create_partial_symtab
1104 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1105 const char *name);
1106
1107 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1108 const gdb_byte *info_ptr,
1109 struct die_info *type_unit_die);
1110
1111 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1112
1113 static void scan_partial_symbols (struct partial_die_info *,
1114 CORE_ADDR *, CORE_ADDR *,
1115 int, struct dwarf2_cu *);
1116
1117 static void add_partial_symbol (struct partial_die_info *,
1118 struct dwarf2_cu *);
1119
1120 static void add_partial_namespace (struct partial_die_info *pdi,
1121 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1122 int set_addrmap, struct dwarf2_cu *cu);
1123
1124 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1125 CORE_ADDR *highpc, int set_addrmap,
1126 struct dwarf2_cu *cu);
1127
1128 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1129 struct dwarf2_cu *cu);
1130
1131 static void add_partial_subprogram (struct partial_die_info *pdi,
1132 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1133 int need_pc, struct dwarf2_cu *cu);
1134
1135 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1136
1137 static struct partial_die_info *load_partial_dies
1138 (const struct die_reader_specs *, const gdb_byte *, int);
1139
1140 /* A pair of partial_die_info and compilation unit. */
1141 struct cu_partial_die_info
1142 {
1143 /* The compilation unit of the partial_die_info. */
1144 struct dwarf2_cu *cu;
1145 /* A partial_die_info. */
1146 struct partial_die_info *pdi;
1147
1148 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1149 : cu (cu),
1150 pdi (pdi)
1151 { /* Nothing. */ }
1152
1153 private:
1154 cu_partial_die_info () = delete;
1155 };
1156
1157 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1158 struct dwarf2_cu *);
1159
1160 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1161 struct attribute *,
1162 const struct attr_abbrev *,
1163 const gdb_byte *);
1164
1165 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1166 struct attribute *attr, dwarf_tag tag);
1167
1168 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1169
1170 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1171 dwarf2_section_info *, sect_offset);
1172
1173 static const char *read_indirect_string
1174 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1175 const struct comp_unit_head *, unsigned int *);
1176
1177 static const char *read_indirect_string_at_offset
1178 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1179
1180 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1181 const gdb_byte *,
1182 unsigned int *);
1183
1184 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1185 ULONGEST str_index);
1186
1187 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1188 ULONGEST str_index);
1189
1190 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1191 struct dwarf2_cu *);
1192
1193 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1194 struct dwarf2_cu *cu);
1195
1196 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1197
1198 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1199 struct dwarf2_cu *cu);
1200
1201 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1202
1203 static struct die_info *die_specification (struct die_info *die,
1204 struct dwarf2_cu **);
1205
1206 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1207 struct dwarf2_cu *cu);
1208
1209 static void dwarf_decode_lines (struct line_header *,
1210 const file_and_directory &,
1211 struct dwarf2_cu *, dwarf2_psymtab *,
1212 CORE_ADDR, int decode_mapping);
1213
1214 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1215 const char *);
1216
1217 static struct symbol *new_symbol (struct die_info *, struct type *,
1218 struct dwarf2_cu *, struct symbol * = NULL);
1219
1220 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1221 struct dwarf2_cu *);
1222
1223 static void dwarf2_const_value_attr (const struct attribute *attr,
1224 struct type *type,
1225 const char *name,
1226 struct obstack *obstack,
1227 struct dwarf2_cu *cu, LONGEST *value,
1228 const gdb_byte **bytes,
1229 struct dwarf2_locexpr_baton **baton);
1230
1231 static struct type *read_subrange_index_type (struct die_info *die,
1232 struct dwarf2_cu *cu);
1233
1234 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1235
1236 static int need_gnat_info (struct dwarf2_cu *);
1237
1238 static struct type *die_descriptive_type (struct die_info *,
1239 struct dwarf2_cu *);
1240
1241 static void set_descriptive_type (struct type *, struct die_info *,
1242 struct dwarf2_cu *);
1243
1244 static struct type *die_containing_type (struct die_info *,
1245 struct dwarf2_cu *);
1246
1247 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1248 struct dwarf2_cu *);
1249
1250 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1251
1252 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1253
1254 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1255
1256 static char *typename_concat (struct obstack *obs, const char *prefix,
1257 const char *suffix, int physname,
1258 struct dwarf2_cu *cu);
1259
1260 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1261
1262 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1263
1264 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1265
1266 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1267
1268 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1269
1270 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1271
1272 /* Return the .debug_loclists section to use for cu. */
1273 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1274
1275 /* Return the .debug_rnglists section to use for cu. */
1276 static struct dwarf2_section_info *cu_debug_rnglists_section
1277 (struct dwarf2_cu *cu, dwarf_tag tag);
1278
1279 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1280 values. Keep the items ordered with increasing constraints compliance. */
1281 enum pc_bounds_kind
1282 {
1283 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1284 PC_BOUNDS_NOT_PRESENT,
1285
1286 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1287 were present but they do not form a valid range of PC addresses. */
1288 PC_BOUNDS_INVALID,
1289
1290 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1291 PC_BOUNDS_RANGES,
1292
1293 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1294 PC_BOUNDS_HIGH_LOW,
1295 };
1296
1297 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1298 CORE_ADDR *, CORE_ADDR *,
1299 struct dwarf2_cu *,
1300 dwarf2_psymtab *);
1301
1302 static void get_scope_pc_bounds (struct die_info *,
1303 CORE_ADDR *, CORE_ADDR *,
1304 struct dwarf2_cu *);
1305
1306 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1307 CORE_ADDR, struct dwarf2_cu *);
1308
1309 static void dwarf2_add_field (struct field_info *, struct die_info *,
1310 struct dwarf2_cu *);
1311
1312 static void dwarf2_attach_fields_to_type (struct field_info *,
1313 struct type *, struct dwarf2_cu *);
1314
1315 static void dwarf2_add_member_fn (struct field_info *,
1316 struct die_info *, struct type *,
1317 struct dwarf2_cu *);
1318
1319 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1320 struct type *,
1321 struct dwarf2_cu *);
1322
1323 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1324
1325 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1326
1327 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1328
1329 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1330
1331 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1332
1333 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1334
1335 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1336
1337 static struct type *read_module_type (struct die_info *die,
1338 struct dwarf2_cu *cu);
1339
1340 static const char *namespace_name (struct die_info *die,
1341 int *is_anonymous, struct dwarf2_cu *);
1342
1343 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1344
1345 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1346 bool * = nullptr);
1347
1348 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1349 struct dwarf2_cu *);
1350
1351 static struct die_info *read_die_and_siblings_1
1352 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1353 struct die_info *);
1354
1355 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1356 const gdb_byte *info_ptr,
1357 const gdb_byte **new_info_ptr,
1358 struct die_info *parent);
1359
1360 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1361 struct die_info **, const gdb_byte *,
1362 int);
1363
1364 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1365 struct die_info **, const gdb_byte *);
1366
1367 static void process_die (struct die_info *, struct dwarf2_cu *);
1368
1369 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1370 struct objfile *);
1371
1372 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1373
1374 static const char *dwarf2_full_name (const char *name,
1375 struct die_info *die,
1376 struct dwarf2_cu *cu);
1377
1378 static const char *dwarf2_physname (const char *name, struct die_info *die,
1379 struct dwarf2_cu *cu);
1380
1381 static struct die_info *dwarf2_extension (struct die_info *die,
1382 struct dwarf2_cu **);
1383
1384 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1385
1386 static void dump_die_for_error (struct die_info *);
1387
1388 static void dump_die_1 (struct ui_file *, int level, int max_level,
1389 struct die_info *);
1390
1391 /*static*/ void dump_die (struct die_info *, int max_level);
1392
1393 static void store_in_ref_table (struct die_info *,
1394 struct dwarf2_cu *);
1395
1396 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1397 const struct attribute *,
1398 struct dwarf2_cu **);
1399
1400 static struct die_info *follow_die_ref (struct die_info *,
1401 const struct attribute *,
1402 struct dwarf2_cu **);
1403
1404 static struct die_info *follow_die_sig (struct die_info *,
1405 const struct attribute *,
1406 struct dwarf2_cu **);
1407
1408 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1409 struct dwarf2_cu *);
1410
1411 static struct type *get_DW_AT_signature_type (struct die_info *,
1412 const struct attribute *,
1413 struct dwarf2_cu *);
1414
1415 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1416 dwarf2_per_objfile *per_objfile);
1417
1418 static void read_signatured_type (signatured_type *sig_type,
1419 dwarf2_per_objfile *per_objfile);
1420
1421 static int attr_to_dynamic_prop (const struct attribute *attr,
1422 struct die_info *die, struct dwarf2_cu *cu,
1423 struct dynamic_prop *prop, struct type *type);
1424
1425 /* memory allocation interface */
1426
1427 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1428
1429 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1430
1431 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1432
1433 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1434 struct dwarf2_loclist_baton *baton,
1435 const struct attribute *attr);
1436
1437 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1438 struct symbol *sym,
1439 struct dwarf2_cu *cu,
1440 int is_block);
1441
1442 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1443 const gdb_byte *info_ptr,
1444 const struct abbrev_info *abbrev);
1445
1446 static hashval_t partial_die_hash (const void *item);
1447
1448 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1449
1450 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1451 (sect_offset sect_off, unsigned int offset_in_dwz,
1452 dwarf2_per_bfd *per_bfd);
1453
1454 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1455 struct die_info *comp_unit_die,
1456 enum language pretend_language);
1457
1458 static struct type *set_die_type (struct die_info *, struct type *,
1459 struct dwarf2_cu *, bool = false);
1460
1461 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1462
1463 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1464 dwarf2_per_objfile *per_objfile,
1465 dwarf2_cu *existing_cu,
1466 bool skip_partial,
1467 enum language pretend_language);
1468
1469 static void process_full_comp_unit (dwarf2_cu *cu,
1470 enum language pretend_language);
1471
1472 static void process_full_type_unit (dwarf2_cu *cu,
1473 enum language pretend_language);
1474
1475 static struct type *get_die_type_at_offset (sect_offset,
1476 dwarf2_per_cu_data *per_cu,
1477 dwarf2_per_objfile *per_objfile);
1478
1479 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1480
1481 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1482 dwarf2_per_objfile *per_objfile,
1483 enum language pretend_language);
1484
1485 static void process_queue (dwarf2_per_objfile *per_objfile);
1486
1487 /* Class, the destructor of which frees all allocated queue entries. This
1488 will only have work to do if an error was thrown while processing the
1489 dwarf. If no error was thrown then the queue entries should have all
1490 been processed, and freed, as we went along. */
1491
1492 class dwarf2_queue_guard
1493 {
1494 public:
1495 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1496 : m_per_objfile (per_objfile)
1497 {
1498 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1499
1500 m_per_objfile->per_bfd->queue.emplace ();
1501 }
1502
1503 /* Free any entries remaining on the queue. There should only be
1504 entries left if we hit an error while processing the dwarf. */
1505 ~dwarf2_queue_guard ()
1506 {
1507 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1508
1509 m_per_objfile->per_bfd->queue.reset ();
1510 }
1511
1512 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1513
1514 private:
1515 dwarf2_per_objfile *m_per_objfile;
1516 };
1517
1518 dwarf2_queue_item::~dwarf2_queue_item ()
1519 {
1520 /* Anything still marked queued is likely to be in an
1521 inconsistent state, so discard it. */
1522 if (per_cu->queued)
1523 {
1524 per_objfile->remove_cu (per_cu);
1525 per_cu->queued = 0;
1526 }
1527 }
1528
1529 /* See dwarf2/read.h. */
1530
1531 void
1532 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1533 {
1534 if (data->is_debug_types)
1535 delete static_cast<signatured_type *> (data);
1536 else
1537 delete data;
1538 }
1539
1540 static file_and_directory &find_file_and_directory
1541 (struct die_info *die, struct dwarf2_cu *cu);
1542
1543 static const char *compute_include_file_name
1544 (const struct line_header *lh,
1545 const file_entry &fe,
1546 const file_and_directory &cu_info,
1547 gdb::unique_xmalloc_ptr<char> *name_holder);
1548
1549 static htab_up allocate_signatured_type_table ();
1550
1551 static htab_up allocate_dwo_unit_table ();
1552
1553 static struct dwo_unit *lookup_dwo_unit_in_dwp
1554 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1555 const char *comp_dir, ULONGEST signature, int is_debug_types);
1556
1557 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1558
1559 static struct dwo_unit *lookup_dwo_comp_unit
1560 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1561 ULONGEST signature);
1562
1563 static struct dwo_unit *lookup_dwo_type_unit
1564 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1565
1566 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1567
1568 /* A unique pointer to a dwo_file. */
1569
1570 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1571
1572 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1573
1574 static void check_producer (struct dwarf2_cu *cu);
1575 \f
1576 /* Various complaints about symbol reading that don't abort the process. */
1577
1578 static void
1579 dwarf2_debug_line_missing_file_complaint (void)
1580 {
1581 complaint (_(".debug_line section has line data without a file"));
1582 }
1583
1584 static void
1585 dwarf2_debug_line_missing_end_sequence_complaint (void)
1586 {
1587 complaint (_(".debug_line section has line "
1588 "program sequence without an end"));
1589 }
1590
1591 static void
1592 dwarf2_complex_location_expr_complaint (void)
1593 {
1594 complaint (_("location expression too complex"));
1595 }
1596
1597 static void
1598 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1599 int arg3)
1600 {
1601 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1602 arg1, arg2, arg3);
1603 }
1604
1605 static void
1606 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1607 {
1608 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1609 arg1, arg2);
1610 }
1611
1612 /* Hash function for line_header_hash. */
1613
1614 static hashval_t
1615 line_header_hash (const struct line_header *ofs)
1616 {
1617 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1618 }
1619
1620 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1621
1622 static hashval_t
1623 line_header_hash_voidp (const void *item)
1624 {
1625 const struct line_header *ofs = (const struct line_header *) item;
1626
1627 return line_header_hash (ofs);
1628 }
1629
1630 /* Equality function for line_header_hash. */
1631
1632 static int
1633 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1634 {
1635 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1636 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1637
1638 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1639 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1640 }
1641
1642 \f
1643
1644 /* An iterator for all_comp_units that is based on index. This
1645 approach makes it possible to iterate over all_comp_units safely,
1646 when some caller in the loop may add new units. */
1647
1648 class all_comp_units_iterator
1649 {
1650 public:
1651
1652 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1653 : m_per_bfd (per_bfd),
1654 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1655 {
1656 }
1657
1658 all_comp_units_iterator &operator++ ()
1659 {
1660 ++m_index;
1661 return *this;
1662 }
1663
1664 dwarf2_per_cu_data *operator* () const
1665 {
1666 return m_per_bfd->get_cu (m_index);
1667 }
1668
1669 bool operator== (const all_comp_units_iterator &other) const
1670 {
1671 return m_index == other.m_index;
1672 }
1673
1674
1675 bool operator!= (const all_comp_units_iterator &other) const
1676 {
1677 return m_index != other.m_index;
1678 }
1679
1680 private:
1681
1682 dwarf2_per_bfd *m_per_bfd;
1683 size_t m_index;
1684 };
1685
1686 /* A range adapter for the all_comp_units_iterator. */
1687 class all_comp_units_range
1688 {
1689 public:
1690
1691 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1692 : m_per_bfd (per_bfd)
1693 {
1694 }
1695
1696 all_comp_units_iterator begin ()
1697 {
1698 return all_comp_units_iterator (m_per_bfd, true);
1699 }
1700
1701 all_comp_units_iterator end ()
1702 {
1703 return all_comp_units_iterator (m_per_bfd, false);
1704 }
1705
1706 private:
1707
1708 dwarf2_per_bfd *m_per_bfd;
1709 };
1710
1711 /* See declaration. */
1712
1713 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1714 bool can_copy_)
1715 : obfd (obfd),
1716 can_copy (can_copy_)
1717 {
1718 if (names == NULL)
1719 names = &dwarf2_elf_names;
1720
1721 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1722 locate_sections (obfd, sec, *names);
1723 }
1724
1725 dwarf2_per_bfd::~dwarf2_per_bfd ()
1726 {
1727 for (auto &per_cu : all_comp_units)
1728 {
1729 per_cu->imported_symtabs_free ();
1730 per_cu->free_cached_file_names ();
1731 }
1732
1733 /* Everything else should be on this->obstack. */
1734 }
1735
1736 /* See read.h. */
1737
1738 void
1739 dwarf2_per_objfile::remove_all_cus ()
1740 {
1741 gdb_assert (!this->per_bfd->queue.has_value ());
1742
1743 for (auto pair : m_dwarf2_cus)
1744 delete pair.second;
1745
1746 m_dwarf2_cus.clear ();
1747 }
1748
1749 /* A helper class that calls free_cached_comp_units on
1750 destruction. */
1751
1752 class free_cached_comp_units
1753 {
1754 public:
1755
1756 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1757 : m_per_objfile (per_objfile)
1758 {
1759 }
1760
1761 ~free_cached_comp_units ()
1762 {
1763 m_per_objfile->remove_all_cus ();
1764 }
1765
1766 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1767
1768 private:
1769
1770 dwarf2_per_objfile *m_per_objfile;
1771 };
1772
1773 /* See read.h. */
1774
1775 bool
1776 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1777 {
1778 if (per_cu->index < this->m_symtabs.size ())
1779 return this->m_symtabs[per_cu->index] != nullptr;
1780 return false;
1781 }
1782
1783 /* See read.h. */
1784
1785 compunit_symtab *
1786 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1787 {
1788 if (per_cu->index < this->m_symtabs.size ())
1789 return this->m_symtabs[per_cu->index];
1790 return nullptr;
1791 }
1792
1793 /* See read.h. */
1794
1795 void
1796 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1797 compunit_symtab *symtab)
1798 {
1799 if (per_cu->index >= this->m_symtabs.size ())
1800 this->m_symtabs.resize (per_cu->index + 1);
1801 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1802 this->m_symtabs[per_cu->index] = symtab;
1803 }
1804
1805 /* Try to locate the sections we need for DWARF 2 debugging
1806 information and return true if we have enough to do something.
1807 NAMES points to the dwarf2 section names, or is NULL if the standard
1808 ELF names are used. CAN_COPY is true for formats where symbol
1809 interposition is possible and so symbol values must follow copy
1810 relocation rules. */
1811
1812 int
1813 dwarf2_has_info (struct objfile *objfile,
1814 const struct dwarf2_debug_sections *names,
1815 bool can_copy)
1816 {
1817 if (objfile->flags & OBJF_READNEVER)
1818 return 0;
1819
1820 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1821
1822 if (per_objfile == NULL)
1823 {
1824 dwarf2_per_bfd *per_bfd;
1825
1826 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1827 BFD doesn't require relocations.
1828
1829 We don't share with objfiles for which -readnow was requested,
1830 because it would complicate things when loading the same BFD with
1831 -readnow and then without -readnow. */
1832 if (!gdb_bfd_requires_relocations (objfile->obfd)
1833 && (objfile->flags & OBJF_READNOW) == 0)
1834 {
1835 /* See if one has been created for this BFD yet. */
1836 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1837
1838 if (per_bfd == nullptr)
1839 {
1840 /* No, create it now. */
1841 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1842 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1843 }
1844 }
1845 else
1846 {
1847 /* No sharing possible, create one specifically for this objfile. */
1848 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1849 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1850 }
1851
1852 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1853 }
1854
1855 return (!per_objfile->per_bfd->info.is_virtual
1856 && per_objfile->per_bfd->info.s.section != NULL
1857 && !per_objfile->per_bfd->abbrev.is_virtual
1858 && per_objfile->per_bfd->abbrev.s.section != NULL);
1859 }
1860
1861 /* See declaration. */
1862
1863 void
1864 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1865 const dwarf2_debug_sections &names)
1866 {
1867 flagword aflag = bfd_section_flags (sectp);
1868
1869 if ((aflag & SEC_HAS_CONTENTS) == 0)
1870 {
1871 }
1872 else if (elf_section_data (sectp)->this_hdr.sh_size
1873 > bfd_get_file_size (abfd))
1874 {
1875 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1876 warning (_("Discarding section %s which has a section size (%s"
1877 ") larger than the file size [in module %s]"),
1878 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1879 bfd_get_filename (abfd));
1880 }
1881 else if (names.info.matches (sectp->name))
1882 {
1883 this->info.s.section = sectp;
1884 this->info.size = bfd_section_size (sectp);
1885 }
1886 else if (names.abbrev.matches (sectp->name))
1887 {
1888 this->abbrev.s.section = sectp;
1889 this->abbrev.size = bfd_section_size (sectp);
1890 }
1891 else if (names.line.matches (sectp->name))
1892 {
1893 this->line.s.section = sectp;
1894 this->line.size = bfd_section_size (sectp);
1895 }
1896 else if (names.loc.matches (sectp->name))
1897 {
1898 this->loc.s.section = sectp;
1899 this->loc.size = bfd_section_size (sectp);
1900 }
1901 else if (names.loclists.matches (sectp->name))
1902 {
1903 this->loclists.s.section = sectp;
1904 this->loclists.size = bfd_section_size (sectp);
1905 }
1906 else if (names.macinfo.matches (sectp->name))
1907 {
1908 this->macinfo.s.section = sectp;
1909 this->macinfo.size = bfd_section_size (sectp);
1910 }
1911 else if (names.macro.matches (sectp->name))
1912 {
1913 this->macro.s.section = sectp;
1914 this->macro.size = bfd_section_size (sectp);
1915 }
1916 else if (names.str.matches (sectp->name))
1917 {
1918 this->str.s.section = sectp;
1919 this->str.size = bfd_section_size (sectp);
1920 }
1921 else if (names.str_offsets.matches (sectp->name))
1922 {
1923 this->str_offsets.s.section = sectp;
1924 this->str_offsets.size = bfd_section_size (sectp);
1925 }
1926 else if (names.line_str.matches (sectp->name))
1927 {
1928 this->line_str.s.section = sectp;
1929 this->line_str.size = bfd_section_size (sectp);
1930 }
1931 else if (names.addr.matches (sectp->name))
1932 {
1933 this->addr.s.section = sectp;
1934 this->addr.size = bfd_section_size (sectp);
1935 }
1936 else if (names.frame.matches (sectp->name))
1937 {
1938 this->frame.s.section = sectp;
1939 this->frame.size = bfd_section_size (sectp);
1940 }
1941 else if (names.eh_frame.matches (sectp->name))
1942 {
1943 this->eh_frame.s.section = sectp;
1944 this->eh_frame.size = bfd_section_size (sectp);
1945 }
1946 else if (names.ranges.matches (sectp->name))
1947 {
1948 this->ranges.s.section = sectp;
1949 this->ranges.size = bfd_section_size (sectp);
1950 }
1951 else if (names.rnglists.matches (sectp->name))
1952 {
1953 this->rnglists.s.section = sectp;
1954 this->rnglists.size = bfd_section_size (sectp);
1955 }
1956 else if (names.types.matches (sectp->name))
1957 {
1958 struct dwarf2_section_info type_section;
1959
1960 memset (&type_section, 0, sizeof (type_section));
1961 type_section.s.section = sectp;
1962 type_section.size = bfd_section_size (sectp);
1963
1964 this->types.push_back (type_section);
1965 }
1966 else if (names.gdb_index.matches (sectp->name))
1967 {
1968 this->gdb_index.s.section = sectp;
1969 this->gdb_index.size = bfd_section_size (sectp);
1970 }
1971 else if (names.debug_names.matches (sectp->name))
1972 {
1973 this->debug_names.s.section = sectp;
1974 this->debug_names.size = bfd_section_size (sectp);
1975 }
1976 else if (names.debug_aranges.matches (sectp->name))
1977 {
1978 this->debug_aranges.s.section = sectp;
1979 this->debug_aranges.size = bfd_section_size (sectp);
1980 }
1981
1982 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1983 && bfd_section_vma (sectp) == 0)
1984 this->has_section_at_zero = true;
1985 }
1986
1987 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1988 SECTION_NAME. */
1989
1990 void
1991 dwarf2_get_section_info (struct objfile *objfile,
1992 enum dwarf2_section_enum sect,
1993 asection **sectp, const gdb_byte **bufp,
1994 bfd_size_type *sizep)
1995 {
1996 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1997 struct dwarf2_section_info *info;
1998
1999 /* We may see an objfile without any DWARF, in which case we just
2000 return nothing. */
2001 if (per_objfile == NULL)
2002 {
2003 *sectp = NULL;
2004 *bufp = NULL;
2005 *sizep = 0;
2006 return;
2007 }
2008 switch (sect)
2009 {
2010 case DWARF2_DEBUG_FRAME:
2011 info = &per_objfile->per_bfd->frame;
2012 break;
2013 case DWARF2_EH_FRAME:
2014 info = &per_objfile->per_bfd->eh_frame;
2015 break;
2016 default:
2017 gdb_assert_not_reached ("unexpected section");
2018 }
2019
2020 info->read (objfile);
2021
2022 *sectp = info->get_bfd_section ();
2023 *bufp = info->buffer;
2024 *sizep = info->size;
2025 }
2026
2027 \f
2028 /* DWARF quick_symbol_functions support. */
2029
2030 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2031 unique line tables, so we maintain a separate table of all .debug_line
2032 derived entries to support the sharing.
2033 All the quick functions need is the list of file names. We discard the
2034 line_header when we're done and don't need to record it here. */
2035 struct quick_file_names
2036 {
2037 /* The data used to construct the hash key. */
2038 struct stmt_list_hash hash;
2039
2040 /* The number of entries in file_names, real_names. */
2041 unsigned int num_file_names;
2042
2043 /* The CU directory, as given by DW_AT_comp_dir. May be
2044 nullptr. */
2045 const char *comp_dir;
2046
2047 /* The file names from the line table, after being run through
2048 file_full_name. */
2049 const char **file_names;
2050
2051 /* The file names from the line table after being run through
2052 gdb_realpath. These are computed lazily. */
2053 const char **real_names;
2054 };
2055
2056 /* When using the index (and thus not using psymtabs), each CU has an
2057 object of this type. This is used to hold information needed by
2058 the various "quick" methods. */
2059 struct dwarf2_per_cu_quick_data
2060 {
2061 /* The file table. This can be NULL if there was no file table
2062 or it's currently not read in.
2063 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2064 struct quick_file_names *file_names;
2065
2066 /* A temporary mark bit used when iterating over all CUs in
2067 expand_symtabs_matching. */
2068 unsigned int mark : 1;
2069
2070 /* True if we've tried to read the file table. There will be no
2071 point in trying to read it again next time. */
2072 bool files_read : 1;
2073 };
2074
2075 /* A subclass of psymbol_functions that arranges to read the DWARF
2076 partial symbols when needed. */
2077 struct lazy_dwarf_reader : public psymbol_functions
2078 {
2079 using psymbol_functions::psymbol_functions;
2080
2081 bool can_lazily_read_symbols () override
2082 {
2083 return true;
2084 }
2085
2086 void read_partial_symbols (struct objfile *objfile) override
2087 {
2088 if (dwarf2_has_info (objfile, nullptr))
2089 dwarf2_build_psymtabs (objfile, this);
2090 }
2091 };
2092
2093 static quick_symbol_functions_up
2094 make_lazy_dwarf_reader ()
2095 {
2096 return quick_symbol_functions_up (new lazy_dwarf_reader);
2097 }
2098
2099 struct dwarf2_base_index_functions : public quick_symbol_functions
2100 {
2101 bool has_symbols (struct objfile *objfile) override;
2102
2103 bool has_unexpanded_symtabs (struct objfile *objfile) override;
2104
2105 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2106
2107 void forget_cached_source_info (struct objfile *objfile) override;
2108
2109 enum language lookup_global_symbol_language (struct objfile *objfile,
2110 const char *name,
2111 domain_enum domain,
2112 bool *symbol_found_p) override
2113 {
2114 *symbol_found_p = false;
2115 return language_unknown;
2116 }
2117
2118 void print_stats (struct objfile *objfile, bool print_bcache) override;
2119
2120 void expand_all_symtabs (struct objfile *objfile) override;
2121
2122 struct compunit_symtab *find_pc_sect_compunit_symtab
2123 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2124 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2125
2126 struct compunit_symtab *find_compunit_symtab_by_address
2127 (struct objfile *objfile, CORE_ADDR address) override
2128 {
2129 return nullptr;
2130 }
2131
2132 void map_symbol_filenames (struct objfile *objfile,
2133 gdb::function_view<symbol_filename_ftype> fun,
2134 bool need_fullname) override;
2135 };
2136
2137 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2138 {
2139 void dump (struct objfile *objfile) override;
2140
2141 void expand_matching_symbols
2142 (struct objfile *,
2143 const lookup_name_info &lookup_name,
2144 domain_enum domain,
2145 int global,
2146 symbol_compare_ftype *ordered_compare) override;
2147
2148 bool expand_symtabs_matching
2149 (struct objfile *objfile,
2150 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2151 const lookup_name_info *lookup_name,
2152 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2153 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2154 block_search_flags search_flags,
2155 domain_enum domain,
2156 enum search_domain kind) override;
2157 };
2158
2159 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2160 {
2161 void dump (struct objfile *objfile) override;
2162
2163 void expand_matching_symbols
2164 (struct objfile *,
2165 const lookup_name_info &lookup_name,
2166 domain_enum domain,
2167 int global,
2168 symbol_compare_ftype *ordered_compare) override;
2169
2170 bool expand_symtabs_matching
2171 (struct objfile *objfile,
2172 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2173 const lookup_name_info *lookup_name,
2174 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2176 block_search_flags search_flags,
2177 domain_enum domain,
2178 enum search_domain kind) override;
2179 };
2180
2181 static quick_symbol_functions_up
2182 make_dwarf_gdb_index ()
2183 {
2184 return quick_symbol_functions_up (new dwarf2_gdb_index);
2185 }
2186
2187 static quick_symbol_functions_up
2188 make_dwarf_debug_names ()
2189 {
2190 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2191 }
2192
2193 /* Utility hash function for a stmt_list_hash. */
2194
2195 static hashval_t
2196 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2197 {
2198 hashval_t v = 0;
2199
2200 if (stmt_list_hash->dwo_unit != NULL)
2201 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2202 v += to_underlying (stmt_list_hash->line_sect_off);
2203 return v;
2204 }
2205
2206 /* Utility equality function for a stmt_list_hash. */
2207
2208 static int
2209 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2210 const struct stmt_list_hash *rhs)
2211 {
2212 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2213 return 0;
2214 if (lhs->dwo_unit != NULL
2215 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2216 return 0;
2217
2218 return lhs->line_sect_off == rhs->line_sect_off;
2219 }
2220
2221 /* Hash function for a quick_file_names. */
2222
2223 static hashval_t
2224 hash_file_name_entry (const void *e)
2225 {
2226 const struct quick_file_names *file_data
2227 = (const struct quick_file_names *) e;
2228
2229 return hash_stmt_list_entry (&file_data->hash);
2230 }
2231
2232 /* Equality function for a quick_file_names. */
2233
2234 static int
2235 eq_file_name_entry (const void *a, const void *b)
2236 {
2237 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2238 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2239
2240 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2241 }
2242
2243 /* Create a quick_file_names hash table. */
2244
2245 static htab_up
2246 create_quick_file_names_table (unsigned int nr_initial_entries)
2247 {
2248 return htab_up (htab_create_alloc (nr_initial_entries,
2249 hash_file_name_entry, eq_file_name_entry,
2250 nullptr, xcalloc, xfree));
2251 }
2252
2253 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2254 function is unrelated to symtabs, symtab would have to be created afterwards.
2255 You should call age_cached_comp_units after processing the CU. */
2256
2257 static dwarf2_cu *
2258 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2259 bool skip_partial)
2260 {
2261 if (per_cu->is_debug_types)
2262 load_full_type_unit (per_cu, per_objfile);
2263 else
2264 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2265 skip_partial, language_minimal);
2266
2267 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2268 if (cu == nullptr)
2269 return nullptr; /* Dummy CU. */
2270
2271 dwarf2_find_base_address (cu->dies, cu);
2272
2273 return cu;
2274 }
2275
2276 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2277
2278 static void
2279 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2280 dwarf2_per_objfile *per_objfile, bool skip_partial)
2281 {
2282 /* Skip type_unit_groups, reading the type units they contain
2283 is handled elsewhere. */
2284 if (per_cu->type_unit_group_p ())
2285 return;
2286
2287 {
2288 /* The destructor of dwarf2_queue_guard frees any entries left on
2289 the queue. After this point we're guaranteed to leave this function
2290 with the dwarf queue empty. */
2291 dwarf2_queue_guard q_guard (per_objfile);
2292
2293 if (!per_objfile->symtab_set_p (per_cu))
2294 {
2295 queue_comp_unit (per_cu, per_objfile, language_minimal);
2296 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2297
2298 /* If we just loaded a CU from a DWO, and we're working with an index
2299 that may badly handle TUs, load all the TUs in that DWO as well.
2300 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2301 if (!per_cu->is_debug_types
2302 && cu != NULL
2303 && cu->dwo_unit != NULL
2304 && per_objfile->per_bfd->index_table != NULL
2305 && per_objfile->per_bfd->index_table->version <= 7
2306 /* DWP files aren't supported yet. */
2307 && get_dwp_file (per_objfile) == NULL)
2308 queue_and_load_all_dwo_tus (cu);
2309 }
2310
2311 process_queue (per_objfile);
2312 }
2313
2314 /* Age the cache, releasing compilation units that have not
2315 been used recently. */
2316 per_objfile->age_comp_units ();
2317 }
2318
2319 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2320 the per-objfile for which this symtab is instantiated.
2321
2322 Returns the resulting symbol table. */
2323
2324 static struct compunit_symtab *
2325 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2326 dwarf2_per_objfile *per_objfile,
2327 bool skip_partial)
2328 {
2329 gdb_assert (per_objfile->per_bfd->using_index);
2330
2331 if (!per_objfile->symtab_set_p (per_cu))
2332 {
2333 free_cached_comp_units freer (per_objfile);
2334 scoped_restore decrementer = increment_reading_symtab ();
2335 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2336 process_cu_includes (per_objfile);
2337 }
2338
2339 return per_objfile->get_symtab (per_cu);
2340 }
2341
2342 /* See read.h. */
2343
2344 dwarf2_per_cu_data_up
2345 dwarf2_per_bfd::allocate_per_cu ()
2346 {
2347 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2348 result->per_bfd = this;
2349 result->index = all_comp_units.size ();
2350 return result;
2351 }
2352
2353 /* See read.h. */
2354
2355 signatured_type_up
2356 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2357 {
2358 signatured_type_up result (new signatured_type (signature));
2359 result->per_bfd = this;
2360 result->index = all_comp_units.size ();
2361 result->is_debug_types = true;
2362 tu_stats.nr_tus++;
2363 return result;
2364 }
2365
2366 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2367 obstack, and constructed with the specified field values. */
2368
2369 static dwarf2_per_cu_data_up
2370 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2371 struct dwarf2_section_info *section,
2372 int is_dwz,
2373 sect_offset sect_off, ULONGEST length)
2374 {
2375 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2376 the_cu->sect_off = sect_off;
2377 the_cu->length = length;
2378 the_cu->section = section;
2379 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2380 struct dwarf2_per_cu_quick_data);
2381 the_cu->is_dwz = is_dwz;
2382 return the_cu;
2383 }
2384
2385 /* A helper for create_cus_from_index that handles a given list of
2386 CUs. */
2387
2388 static void
2389 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2390 const gdb_byte *cu_list, offset_type n_elements,
2391 struct dwarf2_section_info *section,
2392 int is_dwz)
2393 {
2394 for (offset_type i = 0; i < n_elements; i += 2)
2395 {
2396 gdb_static_assert (sizeof (ULONGEST) >= 8);
2397
2398 sect_offset sect_off
2399 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2400 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2401 cu_list += 2 * 8;
2402
2403 dwarf2_per_cu_data_up per_cu
2404 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2405 length);
2406 per_bfd->all_comp_units.push_back (std::move (per_cu));
2407 }
2408 }
2409
2410 /* Read the CU list from the mapped index, and use it to create all
2411 the CU objects for PER_BFD. */
2412
2413 static void
2414 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2415 const gdb_byte *cu_list, offset_type cu_list_elements,
2416 const gdb_byte *dwz_list, offset_type dwz_elements)
2417 {
2418 gdb_assert (per_bfd->all_comp_units.empty ());
2419 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2420
2421 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2422 &per_bfd->info, 0);
2423
2424 if (dwz_elements == 0)
2425 return;
2426
2427 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2428 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2429 &dwz->info, 1);
2430 }
2431
2432 /* Create the signatured type hash table from the index. */
2433
2434 static void
2435 create_signatured_type_table_from_index
2436 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2437 const gdb_byte *bytes, offset_type elements)
2438 {
2439 htab_up sig_types_hash = allocate_signatured_type_table ();
2440
2441 for (offset_type i = 0; i < elements; i += 3)
2442 {
2443 signatured_type_up sig_type;
2444 ULONGEST signature;
2445 void **slot;
2446 cu_offset type_offset_in_tu;
2447
2448 gdb_static_assert (sizeof (ULONGEST) >= 8);
2449 sect_offset sect_off
2450 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2451 type_offset_in_tu
2452 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2453 BFD_ENDIAN_LITTLE);
2454 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2455 bytes += 3 * 8;
2456
2457 sig_type = per_bfd->allocate_signatured_type (signature);
2458 sig_type->type_offset_in_tu = type_offset_in_tu;
2459 sig_type->section = section;
2460 sig_type->sect_off = sect_off;
2461 sig_type->v.quick
2462 = OBSTACK_ZALLOC (&per_bfd->obstack,
2463 struct dwarf2_per_cu_quick_data);
2464
2465 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2466 *slot = sig_type.get ();
2467
2468 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2469 }
2470
2471 per_bfd->signatured_types = std::move (sig_types_hash);
2472 }
2473
2474 /* Create the signatured type hash table from .debug_names. */
2475
2476 static void
2477 create_signatured_type_table_from_debug_names
2478 (dwarf2_per_objfile *per_objfile,
2479 const mapped_debug_names &map,
2480 struct dwarf2_section_info *section,
2481 struct dwarf2_section_info *abbrev_section)
2482 {
2483 struct objfile *objfile = per_objfile->objfile;
2484
2485 section->read (objfile);
2486 abbrev_section->read (objfile);
2487
2488 htab_up sig_types_hash = allocate_signatured_type_table ();
2489
2490 for (uint32_t i = 0; i < map.tu_count; ++i)
2491 {
2492 signatured_type_up sig_type;
2493 void **slot;
2494
2495 sect_offset sect_off
2496 = (sect_offset) (extract_unsigned_integer
2497 (map.tu_table_reordered + i * map.offset_size,
2498 map.offset_size,
2499 map.dwarf5_byte_order));
2500
2501 comp_unit_head cu_header;
2502 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2503 abbrev_section,
2504 section->buffer + to_underlying (sect_off),
2505 rcuh_kind::TYPE);
2506
2507 sig_type = per_objfile->per_bfd->allocate_signatured_type
2508 (cu_header.signature);
2509 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2510 sig_type->section = section;
2511 sig_type->sect_off = sect_off;
2512 sig_type->v.quick
2513 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2514 struct dwarf2_per_cu_quick_data);
2515
2516 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2517 *slot = sig_type.get ();
2518
2519 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2520 }
2521
2522 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2523 }
2524
2525 /* Read the address map data from the mapped index, and use it to
2526 populate the psymtabs_addrmap. */
2527
2528 static void
2529 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2530 struct mapped_index *index)
2531 {
2532 struct objfile *objfile = per_objfile->objfile;
2533 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2534 struct gdbarch *gdbarch = objfile->arch ();
2535 const gdb_byte *iter, *end;
2536 struct addrmap *mutable_map;
2537 CORE_ADDR baseaddr;
2538
2539 auto_obstack temp_obstack;
2540
2541 mutable_map = addrmap_create_mutable (&temp_obstack);
2542
2543 iter = index->address_table.data ();
2544 end = iter + index->address_table.size ();
2545
2546 baseaddr = objfile->text_section_offset ();
2547
2548 while (iter < end)
2549 {
2550 ULONGEST hi, lo, cu_index;
2551 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2552 iter += 8;
2553 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2554 iter += 8;
2555 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2556 iter += 4;
2557
2558 if (lo > hi)
2559 {
2560 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2561 hex_string (lo), hex_string (hi));
2562 continue;
2563 }
2564
2565 if (cu_index >= per_bfd->all_comp_units.size ())
2566 {
2567 complaint (_(".gdb_index address table has invalid CU number %u"),
2568 (unsigned) cu_index);
2569 continue;
2570 }
2571
2572 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2573 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2574 addrmap_set_empty (mutable_map, lo, hi - 1,
2575 per_bfd->get_cu (cu_index));
2576 }
2577
2578 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2579 &per_bfd->obstack);
2580 }
2581
2582 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2583 populate the psymtabs_addrmap. */
2584
2585 static void
2586 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2587 struct dwarf2_section_info *section)
2588 {
2589 struct objfile *objfile = per_objfile->objfile;
2590 bfd *abfd = objfile->obfd;
2591 struct gdbarch *gdbarch = objfile->arch ();
2592 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2593 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2594
2595 auto_obstack temp_obstack;
2596 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2597
2598 std::unordered_map<sect_offset,
2599 dwarf2_per_cu_data *,
2600 gdb::hash_enum<sect_offset>>
2601 debug_info_offset_to_per_cu;
2602 for (const auto &per_cu : per_bfd->all_comp_units)
2603 {
2604 /* A TU will not need aranges, and skipping them here is an easy
2605 way of ignoring .debug_types -- and possibly seeing a
2606 duplicate section offset -- entirely. */
2607 if (per_cu->is_debug_types)
2608 continue;
2609
2610 const auto insertpair
2611 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2612 per_cu.get ());
2613 if (!insertpair.second)
2614 {
2615 warning (_("Section .debug_aranges in %s has duplicate "
2616 "debug_info_offset %s, ignoring .debug_aranges."),
2617 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2618 return;
2619 }
2620 }
2621
2622 section->read (objfile);
2623
2624 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2625
2626 const gdb_byte *addr = section->buffer;
2627
2628 while (addr < section->buffer + section->size)
2629 {
2630 const gdb_byte *const entry_addr = addr;
2631 unsigned int bytes_read;
2632
2633 const LONGEST entry_length = read_initial_length (abfd, addr,
2634 &bytes_read);
2635 addr += bytes_read;
2636
2637 const gdb_byte *const entry_end = addr + entry_length;
2638 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2639 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2640 if (addr + entry_length > section->buffer + section->size)
2641 {
2642 warning (_("Section .debug_aranges in %s entry at offset %s "
2643 "length %s exceeds section length %s, "
2644 "ignoring .debug_aranges."),
2645 objfile_name (objfile),
2646 plongest (entry_addr - section->buffer),
2647 plongest (bytes_read + entry_length),
2648 pulongest (section->size));
2649 return;
2650 }
2651
2652 /* The version number. */
2653 const uint16_t version = read_2_bytes (abfd, addr);
2654 addr += 2;
2655 if (version != 2)
2656 {
2657 warning (_("Section .debug_aranges in %s entry at offset %s "
2658 "has unsupported version %d, ignoring .debug_aranges."),
2659 objfile_name (objfile),
2660 plongest (entry_addr - section->buffer), version);
2661 return;
2662 }
2663
2664 const uint64_t debug_info_offset
2665 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2666 addr += offset_size;
2667 const auto per_cu_it
2668 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2669 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2670 {
2671 warning (_("Section .debug_aranges in %s entry at offset %s "
2672 "debug_info_offset %s does not exists, "
2673 "ignoring .debug_aranges."),
2674 objfile_name (objfile),
2675 plongest (entry_addr - section->buffer),
2676 pulongest (debug_info_offset));
2677 return;
2678 }
2679 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2680
2681 const uint8_t address_size = *addr++;
2682 if (address_size < 1 || address_size > 8)
2683 {
2684 warning (_("Section .debug_aranges in %s entry at offset %s "
2685 "address_size %u is invalid, ignoring .debug_aranges."),
2686 objfile_name (objfile),
2687 plongest (entry_addr - section->buffer), address_size);
2688 return;
2689 }
2690
2691 const uint8_t segment_selector_size = *addr++;
2692 if (segment_selector_size != 0)
2693 {
2694 warning (_("Section .debug_aranges in %s entry at offset %s "
2695 "segment_selector_size %u is not supported, "
2696 "ignoring .debug_aranges."),
2697 objfile_name (objfile),
2698 plongest (entry_addr - section->buffer),
2699 segment_selector_size);
2700 return;
2701 }
2702
2703 /* Must pad to an alignment boundary that is twice the address
2704 size. It is undocumented by the DWARF standard but GCC does
2705 use it. However, not every compiler does this. We can see
2706 whether it has happened by looking at the total length of the
2707 contents of the aranges for this CU -- it if isn't a multiple
2708 of twice the address size, then we skip any leftover
2709 bytes. */
2710 addr += (entry_end - addr) % (2 * address_size);
2711
2712 for (;;)
2713 {
2714 if (addr + 2 * address_size > entry_end)
2715 {
2716 warning (_("Section .debug_aranges in %s entry at offset %s "
2717 "address list is not properly terminated, "
2718 "ignoring .debug_aranges."),
2719 objfile_name (objfile),
2720 plongest (entry_addr - section->buffer));
2721 return;
2722 }
2723 ULONGEST start = extract_unsigned_integer (addr, address_size,
2724 dwarf5_byte_order);
2725 addr += address_size;
2726 ULONGEST length = extract_unsigned_integer (addr, address_size,
2727 dwarf5_byte_order);
2728 addr += address_size;
2729 if (start == 0 && length == 0)
2730 break;
2731 if (start == 0 && !per_bfd->has_section_at_zero)
2732 {
2733 /* Symbol was eliminated due to a COMDAT group. */
2734 continue;
2735 }
2736 ULONGEST end = start + length;
2737 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2738 - baseaddr);
2739 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2740 - baseaddr);
2741 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2742 }
2743 }
2744
2745 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2746 &per_bfd->obstack);
2747 }
2748
2749 /* A helper function that reads the .gdb_index from BUFFER and fills
2750 in MAP. FILENAME is the name of the file containing the data;
2751 it is used for error reporting. DEPRECATED_OK is true if it is
2752 ok to use deprecated sections.
2753
2754 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2755 out parameters that are filled in with information about the CU and
2756 TU lists in the section.
2757
2758 Returns true if all went well, false otherwise. */
2759
2760 static bool
2761 read_gdb_index_from_buffer (const char *filename,
2762 bool deprecated_ok,
2763 gdb::array_view<const gdb_byte> buffer,
2764 struct mapped_index *map,
2765 const gdb_byte **cu_list,
2766 offset_type *cu_list_elements,
2767 const gdb_byte **types_list,
2768 offset_type *types_list_elements)
2769 {
2770 const gdb_byte *addr = &buffer[0];
2771 offset_view metadata (buffer);
2772
2773 /* Version check. */
2774 offset_type version = metadata[0];
2775 /* Versions earlier than 3 emitted every copy of a psymbol. This
2776 causes the index to behave very poorly for certain requests. Version 3
2777 contained incomplete addrmap. So, it seems better to just ignore such
2778 indices. */
2779 if (version < 4)
2780 {
2781 static int warning_printed = 0;
2782 if (!warning_printed)
2783 {
2784 warning (_("Skipping obsolete .gdb_index section in %s."),
2785 filename);
2786 warning_printed = 1;
2787 }
2788 return 0;
2789 }
2790 /* Index version 4 uses a different hash function than index version
2791 5 and later.
2792
2793 Versions earlier than 6 did not emit psymbols for inlined
2794 functions. Using these files will cause GDB not to be able to
2795 set breakpoints on inlined functions by name, so we ignore these
2796 indices unless the user has done
2797 "set use-deprecated-index-sections on". */
2798 if (version < 6 && !deprecated_ok)
2799 {
2800 static int warning_printed = 0;
2801 if (!warning_printed)
2802 {
2803 warning (_("\
2804 Skipping deprecated .gdb_index section in %s.\n\
2805 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2806 to use the section anyway."),
2807 filename);
2808 warning_printed = 1;
2809 }
2810 return 0;
2811 }
2812 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2813 of the TU (for symbols coming from TUs),
2814 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2815 Plus gold-generated indices can have duplicate entries for global symbols,
2816 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2817 These are just performance bugs, and we can't distinguish gdb-generated
2818 indices from gold-generated ones, so issue no warning here. */
2819
2820 /* Indexes with higher version than the one supported by GDB may be no
2821 longer backward compatible. */
2822 if (version > 8)
2823 return 0;
2824
2825 map->version = version;
2826
2827 int i = 1;
2828 *cu_list = addr + metadata[i];
2829 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2830 ++i;
2831
2832 *types_list = addr + metadata[i];
2833 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2834 ++i;
2835
2836 const gdb_byte *address_table = addr + metadata[i];
2837 const gdb_byte *address_table_end = addr + metadata[i + 1];
2838 map->address_table
2839 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2840 ++i;
2841
2842 const gdb_byte *symbol_table = addr + metadata[i];
2843 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2844 map->symbol_table
2845 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2846 symbol_table_end));
2847
2848 ++i;
2849 map->constant_pool = buffer.slice (metadata[i]);
2850
2851 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2852 {
2853 /* An empty constant pool implies that all symbol table entries are
2854 empty. Make map->symbol_table.empty () == true. */
2855 map->symbol_table
2856 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2857 symbol_table));
2858 }
2859
2860 return 1;
2861 }
2862
2863 /* Callback types for dwarf2_read_gdb_index. */
2864
2865 typedef gdb::function_view
2866 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2867 get_gdb_index_contents_ftype;
2868 typedef gdb::function_view
2869 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2870 get_gdb_index_contents_dwz_ftype;
2871
2872 /* Read .gdb_index. If everything went ok, initialize the "quick"
2873 elements of all the CUs and return 1. Otherwise, return 0. */
2874
2875 static int
2876 dwarf2_read_gdb_index
2877 (dwarf2_per_objfile *per_objfile,
2878 get_gdb_index_contents_ftype get_gdb_index_contents,
2879 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2880 {
2881 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2882 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2883 struct dwz_file *dwz;
2884 struct objfile *objfile = per_objfile->objfile;
2885 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2886
2887 gdb::array_view<const gdb_byte> main_index_contents
2888 = get_gdb_index_contents (objfile, per_bfd);
2889
2890 if (main_index_contents.empty ())
2891 return 0;
2892
2893 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2894 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2895 use_deprecated_index_sections,
2896 main_index_contents, map.get (), &cu_list,
2897 &cu_list_elements, &types_list,
2898 &types_list_elements))
2899 return 0;
2900
2901 /* Don't use the index if it's empty. */
2902 if (map->symbol_table.empty ())
2903 return 0;
2904
2905 /* If there is a .dwz file, read it so we can get its CU list as
2906 well. */
2907 dwz = dwarf2_get_dwz_file (per_bfd);
2908 if (dwz != NULL)
2909 {
2910 struct mapped_index dwz_map;
2911 const gdb_byte *dwz_types_ignore;
2912 offset_type dwz_types_elements_ignore;
2913
2914 gdb::array_view<const gdb_byte> dwz_index_content
2915 = get_gdb_index_contents_dwz (objfile, dwz);
2916
2917 if (dwz_index_content.empty ())
2918 return 0;
2919
2920 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2921 1, dwz_index_content, &dwz_map,
2922 &dwz_list, &dwz_list_elements,
2923 &dwz_types_ignore,
2924 &dwz_types_elements_ignore))
2925 {
2926 warning (_("could not read '.gdb_index' section from %s; skipping"),
2927 bfd_get_filename (dwz->dwz_bfd.get ()));
2928 return 0;
2929 }
2930 }
2931
2932 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2933 dwz_list_elements);
2934
2935 if (types_list_elements)
2936 {
2937 /* We can only handle a single .debug_types when we have an
2938 index. */
2939 if (per_bfd->types.size () != 1)
2940 return 0;
2941
2942 dwarf2_section_info *section = &per_bfd->types[0];
2943
2944 create_signatured_type_table_from_index (per_bfd, section, types_list,
2945 types_list_elements);
2946 }
2947
2948 create_addrmap_from_index (per_objfile, map.get ());
2949
2950 per_bfd->index_table = std::move (map);
2951 per_bfd->using_index = 1;
2952 per_bfd->quick_file_names_table =
2953 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2954
2955 return 1;
2956 }
2957
2958 /* die_reader_func for dw2_get_file_names. */
2959
2960 static void
2961 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2962 struct die_info *comp_unit_die)
2963 {
2964 struct dwarf2_cu *cu = reader->cu;
2965 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2966 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2967 struct dwarf2_per_cu_data *lh_cu;
2968 struct attribute *attr;
2969 void **slot;
2970 struct quick_file_names *qfn;
2971
2972 gdb_assert (! this_cu->is_debug_types);
2973
2974 this_cu->v.quick->files_read = true;
2975 /* Our callers never want to match partial units -- instead they
2976 will match the enclosing full CU. */
2977 if (comp_unit_die->tag == DW_TAG_partial_unit)
2978 return;
2979
2980 lh_cu = this_cu;
2981 slot = NULL;
2982
2983 line_header_up lh;
2984 sect_offset line_offset {};
2985
2986 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2987 if (attr != nullptr && attr->form_is_unsigned ())
2988 {
2989 struct quick_file_names find_entry;
2990
2991 line_offset = (sect_offset) attr->as_unsigned ();
2992
2993 /* We may have already read in this line header (TU line header sharing).
2994 If we have we're done. */
2995 find_entry.hash.dwo_unit = cu->dwo_unit;
2996 find_entry.hash.line_sect_off = line_offset;
2997 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2998 &find_entry, INSERT);
2999 if (*slot != NULL)
3000 {
3001 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3002 return;
3003 }
3004
3005 lh = dwarf_decode_line_header (line_offset, cu);
3006 }
3007
3008 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
3009
3010 int offset = 0;
3011 if (!fnd.is_unknown ())
3012 ++offset;
3013 else if (lh == nullptr)
3014 return;
3015
3016 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3017 qfn->hash.dwo_unit = cu->dwo_unit;
3018 qfn->hash.line_sect_off = line_offset;
3019 /* There may not be a DW_AT_stmt_list. */
3020 if (slot != nullptr)
3021 *slot = qfn;
3022
3023 std::vector<const char *> include_names;
3024 if (lh != nullptr)
3025 {
3026 for (const auto &entry : lh->file_names ())
3027 {
3028 gdb::unique_xmalloc_ptr<char> name_holder;
3029 const char *include_name =
3030 compute_include_file_name (lh.get (), entry, fnd, &name_holder);
3031 if (include_name != nullptr)
3032 {
3033 include_name = per_objfile->objfile->intern (include_name);
3034 include_names.push_back (include_name);
3035 }
3036 }
3037 }
3038
3039 qfn->num_file_names = offset + include_names.size ();
3040 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
3041 qfn->file_names =
3042 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3043 qfn->num_file_names);
3044 if (offset != 0)
3045 qfn->file_names[0] = xstrdup (fnd.get_name ());
3046
3047 if (!include_names.empty ())
3048 memcpy (&qfn->file_names[offset], include_names.data (),
3049 include_names.size () * sizeof (const char *));
3050
3051 qfn->real_names = NULL;
3052
3053 lh_cu->v.quick->file_names = qfn;
3054 }
3055
3056 /* A helper for the "quick" functions which attempts to read the line
3057 table for THIS_CU. */
3058
3059 static struct quick_file_names *
3060 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3061 dwarf2_per_objfile *per_objfile)
3062 {
3063 /* This should never be called for TUs. */
3064 gdb_assert (! this_cu->is_debug_types);
3065 /* Nor type unit groups. */
3066 gdb_assert (! this_cu->type_unit_group_p ());
3067
3068 if (this_cu->v.quick->files_read)
3069 return this_cu->v.quick->file_names;
3070
3071 cutu_reader reader (this_cu, per_objfile);
3072 if (!reader.dummy_p)
3073 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3074
3075 return this_cu->v.quick->file_names;
3076 }
3077
3078 /* A helper for the "quick" functions which computes and caches the
3079 real path for a given file name from the line table. */
3080
3081 static const char *
3082 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3083 struct quick_file_names *qfn, int index)
3084 {
3085 if (qfn->real_names == NULL)
3086 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3087 qfn->num_file_names, const char *);
3088
3089 if (qfn->real_names[index] == NULL)
3090 {
3091 const char *dirname = nullptr;
3092
3093 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
3094 dirname = qfn->comp_dir;
3095
3096 gdb::unique_xmalloc_ptr<char> fullname;
3097 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
3098
3099 qfn->real_names[index] = fullname.release ();
3100 }
3101
3102 return qfn->real_names[index];
3103 }
3104
3105 struct symtab *
3106 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3107 {
3108 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3109 dwarf2_per_cu_data *dwarf_cu
3110 = per_objfile->per_bfd->all_comp_units.back ().get ();
3111 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3112
3113 if (cust == NULL)
3114 return NULL;
3115
3116 return cust->primary_filetab ();
3117 }
3118
3119 /* See read.h. */
3120
3121 void
3122 dwarf2_per_cu_data::free_cached_file_names ()
3123 {
3124 if (per_bfd == nullptr || !per_bfd->using_index || v.quick == nullptr)
3125 return;
3126
3127 struct quick_file_names *file_data = v.quick->file_names;
3128 if (file_data != nullptr && file_data->real_names != nullptr)
3129 {
3130 for (int i = 0; i < file_data->num_file_names; ++i)
3131 {
3132 xfree ((void *) file_data->real_names[i]);
3133 file_data->real_names[i] = nullptr;
3134 }
3135 }
3136 }
3137
3138 void
3139 dwarf2_base_index_functions::forget_cached_source_info
3140 (struct objfile *objfile)
3141 {
3142 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3143
3144 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
3145 per_cu->free_cached_file_names ();
3146 }
3147
3148 /* Struct used to manage iterating over all CUs looking for a symbol. */
3149
3150 struct dw2_symtab_iterator
3151 {
3152 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3153 dwarf2_per_objfile *per_objfile;
3154 /* If set, only look for symbols that match that block. Valid values are
3155 GLOBAL_BLOCK and STATIC_BLOCK. */
3156 gdb::optional<block_enum> block_index;
3157 /* The kind of symbol we're looking for. */
3158 domain_enum domain;
3159 /* The list of CUs from the index entry of the symbol,
3160 or NULL if not found. */
3161 offset_view vec;
3162 /* The next element in VEC to look at. */
3163 int next;
3164 /* The number of elements in VEC, or zero if there is no match. */
3165 int length;
3166 /* Have we seen a global version of the symbol?
3167 If so we can ignore all further global instances.
3168 This is to work around gold/15646, inefficient gold-generated
3169 indices. */
3170 int global_seen;
3171 };
3172
3173 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3174
3175 static void
3176 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3177 dwarf2_per_objfile *per_objfile,
3178 gdb::optional<block_enum> block_index,
3179 domain_enum domain, offset_type namei)
3180 {
3181 iter->per_objfile = per_objfile;
3182 iter->block_index = block_index;
3183 iter->domain = domain;
3184 iter->next = 0;
3185 iter->global_seen = 0;
3186 iter->vec = {};
3187 iter->length = 0;
3188
3189 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3190 /* index is NULL if OBJF_READNOW. */
3191 if (index == NULL)
3192 return;
3193
3194 gdb_assert (!index->symbol_name_slot_invalid (namei));
3195 offset_type vec_idx = index->symbol_vec_index (namei);
3196
3197 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3198 iter->length = iter->vec[0];
3199 }
3200
3201 /* Return the next matching CU or NULL if there are no more. */
3202
3203 static struct dwarf2_per_cu_data *
3204 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3205 {
3206 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3207
3208 for ( ; iter->next < iter->length; ++iter->next)
3209 {
3210 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3211 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3212 gdb_index_symbol_kind symbol_kind =
3213 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3214 /* Only check the symbol attributes if they're present.
3215 Indices prior to version 7 don't record them,
3216 and indices >= 7 may elide them for certain symbols
3217 (gold does this). */
3218 int attrs_valid =
3219 (per_objfile->per_bfd->index_table->version >= 7
3220 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3221
3222 /* Don't crash on bad data. */
3223 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3224 {
3225 complaint (_(".gdb_index entry has bad CU index"
3226 " [in module %s]"), objfile_name (per_objfile->objfile));
3227 continue;
3228 }
3229
3230 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3231
3232 /* Skip if already read in. */
3233 if (per_objfile->symtab_set_p (per_cu))
3234 continue;
3235
3236 /* Check static vs global. */
3237 if (attrs_valid)
3238 {
3239 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3240
3241 if (iter->block_index.has_value ())
3242 {
3243 bool want_static = *iter->block_index == STATIC_BLOCK;
3244
3245 if (is_static != want_static)
3246 continue;
3247 }
3248
3249 /* Work around gold/15646. */
3250 if (!is_static
3251 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3252 {
3253 if (iter->global_seen)
3254 continue;
3255
3256 iter->global_seen = 1;
3257 }
3258 }
3259
3260 /* Only check the symbol's kind if it has one. */
3261 if (attrs_valid)
3262 {
3263 switch (iter->domain)
3264 {
3265 case VAR_DOMAIN:
3266 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3267 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3268 /* Some types are also in VAR_DOMAIN. */
3269 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3270 continue;
3271 break;
3272 case STRUCT_DOMAIN:
3273 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3274 continue;
3275 break;
3276 case LABEL_DOMAIN:
3277 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3278 continue;
3279 break;
3280 case MODULE_DOMAIN:
3281 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3282 continue;
3283 break;
3284 default:
3285 break;
3286 }
3287 }
3288
3289 ++iter->next;
3290 return per_cu;
3291 }
3292
3293 return NULL;
3294 }
3295
3296 void
3297 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3298 bool print_bcache)
3299 {
3300 if (print_bcache)
3301 return;
3302
3303 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3304 int total = per_objfile->per_bfd->all_comp_units.size ();
3305 int count = 0;
3306
3307 for (int i = 0; i < total; ++i)
3308 {
3309 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3310
3311 if (!per_objfile->symtab_set_p (per_cu))
3312 ++count;
3313 }
3314 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3315 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3316 }
3317
3318 /* This dumps minimal information about the index.
3319 It is called via "mt print objfiles".
3320 One use is to verify .gdb_index has been loaded by the
3321 gdb.dwarf2/gdb-index.exp testcase. */
3322
3323 void
3324 dwarf2_gdb_index::dump (struct objfile *objfile)
3325 {
3326 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3327
3328 gdb_assert (per_objfile->per_bfd->using_index);
3329 printf_filtered (".gdb_index:");
3330 if (per_objfile->per_bfd->index_table != NULL)
3331 {
3332 printf_filtered (" version %d\n",
3333 per_objfile->per_bfd->index_table->version);
3334 }
3335 else
3336 printf_filtered (" faked for \"readnow\"\n");
3337 printf_filtered ("\n");
3338 }
3339
3340 void
3341 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3342 {
3343 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3344 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3345
3346 for (int i = 0; i < total_units; ++i)
3347 {
3348 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3349
3350 /* We don't want to directly expand a partial CU, because if we
3351 read it with the wrong language, then assertion failures can
3352 be triggered later on. See PR symtab/23010. So, tell
3353 dw2_instantiate_symtab to skip partial CUs -- any important
3354 partial CU will be read via DW_TAG_imported_unit anyway. */
3355 dw2_instantiate_symtab (per_cu, per_objfile, true);
3356 }
3357 }
3358
3359 static bool
3360 dw2_expand_symtabs_matching_symbol
3361 (mapped_index_base &index,
3362 const lookup_name_info &lookup_name_in,
3363 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3364 gdb::function_view<bool (offset_type)> match_callback,
3365 dwarf2_per_objfile *per_objfile);
3366
3367 static bool
3368 dw2_expand_symtabs_matching_one
3369 (dwarf2_per_cu_data *per_cu,
3370 dwarf2_per_objfile *per_objfile,
3371 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3372 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3373
3374 void
3375 dwarf2_gdb_index::expand_matching_symbols
3376 (struct objfile *objfile,
3377 const lookup_name_info &name, domain_enum domain,
3378 int global,
3379 symbol_compare_ftype *ordered_compare)
3380 {
3381 /* Used for Ada. */
3382 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3383
3384 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3385
3386 if (per_objfile->per_bfd->index_table != nullptr)
3387 {
3388 mapped_index &index = *per_objfile->per_bfd->index_table;
3389
3390 const char *match_name = name.ada ().lookup_name ().c_str ();
3391 auto matcher = [&] (const char *symname)
3392 {
3393 if (ordered_compare == nullptr)
3394 return true;
3395 return ordered_compare (symname, match_name) == 0;
3396 };
3397
3398 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3399 [&] (offset_type namei)
3400 {
3401 struct dw2_symtab_iterator iter;
3402 struct dwarf2_per_cu_data *per_cu;
3403
3404 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3405 namei);
3406 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3407 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3408 nullptr);
3409 return true;
3410 }, per_objfile);
3411 }
3412 else
3413 {
3414 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3415 proceed assuming all symtabs have been read in. */
3416 }
3417 }
3418
3419 /* Starting from a search name, return the string that finds the upper
3420 bound of all strings that start with SEARCH_NAME in a sorted name
3421 list. Returns the empty string to indicate that the upper bound is
3422 the end of the list. */
3423
3424 static std::string
3425 make_sort_after_prefix_name (const char *search_name)
3426 {
3427 /* When looking to complete "func", we find the upper bound of all
3428 symbols that start with "func" by looking for where we'd insert
3429 the closest string that would follow "func" in lexicographical
3430 order. Usually, that's "func"-with-last-character-incremented,
3431 i.e. "fund". Mind non-ASCII characters, though. Usually those
3432 will be UTF-8 multi-byte sequences, but we can't be certain.
3433 Especially mind the 0xff character, which is a valid character in
3434 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3435 rule out compilers allowing it in identifiers. Note that
3436 conveniently, strcmp/strcasecmp are specified to compare
3437 characters interpreted as unsigned char. So what we do is treat
3438 the whole string as a base 256 number composed of a sequence of
3439 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3440 to 0, and carries 1 to the following more-significant position.
3441 If the very first character in SEARCH_NAME ends up incremented
3442 and carries/overflows, then the upper bound is the end of the
3443 list. The string after the empty string is also the empty
3444 string.
3445
3446 Some examples of this operation:
3447
3448 SEARCH_NAME => "+1" RESULT
3449
3450 "abc" => "abd"
3451 "ab\xff" => "ac"
3452 "\xff" "a" "\xff" => "\xff" "b"
3453 "\xff" => ""
3454 "\xff\xff" => ""
3455 "" => ""
3456
3457 Then, with these symbols for example:
3458
3459 func
3460 func1
3461 fund
3462
3463 completing "func" looks for symbols between "func" and
3464 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3465 which finds "func" and "func1", but not "fund".
3466
3467 And with:
3468
3469 funcÿ (Latin1 'ÿ' [0xff])
3470 funcÿ1
3471 fund
3472
3473 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3474 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3475
3476 And with:
3477
3478 ÿÿ (Latin1 'ÿ' [0xff])
3479 ÿÿ1
3480
3481 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3482 the end of the list.
3483 */
3484 std::string after = search_name;
3485 while (!after.empty () && (unsigned char) after.back () == 0xff)
3486 after.pop_back ();
3487 if (!after.empty ())
3488 after.back () = (unsigned char) after.back () + 1;
3489 return after;
3490 }
3491
3492 /* See declaration. */
3493
3494 std::pair<std::vector<name_component>::const_iterator,
3495 std::vector<name_component>::const_iterator>
3496 mapped_index_base::find_name_components_bounds
3497 (const lookup_name_info &lookup_name_without_params, language lang,
3498 dwarf2_per_objfile *per_objfile) const
3499 {
3500 auto *name_cmp
3501 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3502
3503 const char *lang_name
3504 = lookup_name_without_params.language_lookup_name (lang);
3505
3506 /* Comparison function object for lower_bound that matches against a
3507 given symbol name. */
3508 auto lookup_compare_lower = [&] (const name_component &elem,
3509 const char *name)
3510 {
3511 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3512 const char *elem_name = elem_qualified + elem.name_offset;
3513 return name_cmp (elem_name, name) < 0;
3514 };
3515
3516 /* Comparison function object for upper_bound that matches against a
3517 given symbol name. */
3518 auto lookup_compare_upper = [&] (const char *name,
3519 const name_component &elem)
3520 {
3521 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3522 const char *elem_name = elem_qualified + elem.name_offset;
3523 return name_cmp (name, elem_name) < 0;
3524 };
3525
3526 auto begin = this->name_components.begin ();
3527 auto end = this->name_components.end ();
3528
3529 /* Find the lower bound. */
3530 auto lower = [&] ()
3531 {
3532 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3533 return begin;
3534 else
3535 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3536 } ();
3537
3538 /* Find the upper bound. */
3539 auto upper = [&] ()
3540 {
3541 if (lookup_name_without_params.completion_mode ())
3542 {
3543 /* In completion mode, we want UPPER to point past all
3544 symbols names that have the same prefix. I.e., with
3545 these symbols, and completing "func":
3546
3547 function << lower bound
3548 function1
3549 other_function << upper bound
3550
3551 We find the upper bound by looking for the insertion
3552 point of "func"-with-last-character-incremented,
3553 i.e. "fund". */
3554 std::string after = make_sort_after_prefix_name (lang_name);
3555 if (after.empty ())
3556 return end;
3557 return std::lower_bound (lower, end, after.c_str (),
3558 lookup_compare_lower);
3559 }
3560 else
3561 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3562 } ();
3563
3564 return {lower, upper};
3565 }
3566
3567 /* See declaration. */
3568
3569 void
3570 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3571 {
3572 if (!this->name_components.empty ())
3573 return;
3574
3575 this->name_components_casing = case_sensitivity;
3576 auto *name_cmp
3577 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3578
3579 /* The code below only knows how to break apart components of C++
3580 symbol names (and other languages that use '::' as
3581 namespace/module separator) and Ada symbol names. */
3582 auto count = this->symbol_name_count ();
3583 for (offset_type idx = 0; idx < count; idx++)
3584 {
3585 if (this->symbol_name_slot_invalid (idx))
3586 continue;
3587
3588 const char *name = this->symbol_name_at (idx, per_objfile);
3589
3590 /* Add each name component to the name component table. */
3591 unsigned int previous_len = 0;
3592
3593 if (strstr (name, "::") != nullptr)
3594 {
3595 for (unsigned int current_len = cp_find_first_component (name);
3596 name[current_len] != '\0';
3597 current_len += cp_find_first_component (name + current_len))
3598 {
3599 gdb_assert (name[current_len] == ':');
3600 this->name_components.push_back ({previous_len, idx});
3601 /* Skip the '::'. */
3602 current_len += 2;
3603 previous_len = current_len;
3604 }
3605 }
3606 else
3607 {
3608 /* Handle the Ada encoded (aka mangled) form here. */
3609 for (const char *iter = strstr (name, "__");
3610 iter != nullptr;
3611 iter = strstr (iter, "__"))
3612 {
3613 this->name_components.push_back ({previous_len, idx});
3614 iter += 2;
3615 previous_len = iter - name;
3616 }
3617 }
3618
3619 this->name_components.push_back ({previous_len, idx});
3620 }
3621
3622 /* Sort name_components elements by name. */
3623 auto name_comp_compare = [&] (const name_component &left,
3624 const name_component &right)
3625 {
3626 const char *left_qualified
3627 = this->symbol_name_at (left.idx, per_objfile);
3628 const char *right_qualified
3629 = this->symbol_name_at (right.idx, per_objfile);
3630
3631 const char *left_name = left_qualified + left.name_offset;
3632 const char *right_name = right_qualified + right.name_offset;
3633
3634 return name_cmp (left_name, right_name) < 0;
3635 };
3636
3637 std::sort (this->name_components.begin (),
3638 this->name_components.end (),
3639 name_comp_compare);
3640 }
3641
3642 /* Helper for dw2_expand_symtabs_matching that works with a
3643 mapped_index_base instead of the containing objfile. This is split
3644 to a separate function in order to be able to unit test the
3645 name_components matching using a mock mapped_index_base. For each
3646 symbol name that matches, calls MATCH_CALLBACK, passing it the
3647 symbol's index in the mapped_index_base symbol table. */
3648
3649 static bool
3650 dw2_expand_symtabs_matching_symbol
3651 (mapped_index_base &index,
3652 const lookup_name_info &lookup_name_in,
3653 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3654 gdb::function_view<bool (offset_type)> match_callback,
3655 dwarf2_per_objfile *per_objfile)
3656 {
3657 lookup_name_info lookup_name_without_params
3658 = lookup_name_in.make_ignore_params ();
3659
3660 /* Build the symbol name component sorted vector, if we haven't
3661 yet. */
3662 index.build_name_components (per_objfile);
3663
3664 /* The same symbol may appear more than once in the range though.
3665 E.g., if we're looking for symbols that complete "w", and we have
3666 a symbol named "w1::w2", we'll find the two name components for
3667 that same symbol in the range. To be sure we only call the
3668 callback once per symbol, we first collect the symbol name
3669 indexes that matched in a temporary vector and ignore
3670 duplicates. */
3671 std::vector<offset_type> matches;
3672
3673 struct name_and_matcher
3674 {
3675 symbol_name_matcher_ftype *matcher;
3676 const char *name;
3677
3678 bool operator== (const name_and_matcher &other) const
3679 {
3680 return matcher == other.matcher && strcmp (name, other.name) == 0;
3681 }
3682 };
3683
3684 /* A vector holding all the different symbol name matchers, for all
3685 languages. */
3686 std::vector<name_and_matcher> matchers;
3687
3688 for (int i = 0; i < nr_languages; i++)
3689 {
3690 enum language lang_e = (enum language) i;
3691
3692 const language_defn *lang = language_def (lang_e);
3693 symbol_name_matcher_ftype *name_matcher
3694 = lang->get_symbol_name_matcher (lookup_name_without_params);
3695
3696 name_and_matcher key {
3697 name_matcher,
3698 lookup_name_without_params.language_lookup_name (lang_e)
3699 };
3700
3701 /* Don't insert the same comparison routine more than once.
3702 Note that we do this linear walk. This is not a problem in
3703 practice because the number of supported languages is
3704 low. */
3705 if (std::find (matchers.begin (), matchers.end (), key)
3706 != matchers.end ())
3707 continue;
3708 matchers.push_back (std::move (key));
3709
3710 auto bounds
3711 = index.find_name_components_bounds (lookup_name_without_params,
3712 lang_e, per_objfile);
3713
3714 /* Now for each symbol name in range, check to see if we have a name
3715 match, and if so, call the MATCH_CALLBACK callback. */
3716
3717 for (; bounds.first != bounds.second; ++bounds.first)
3718 {
3719 const char *qualified
3720 = index.symbol_name_at (bounds.first->idx, per_objfile);
3721
3722 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3723 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3724 continue;
3725
3726 matches.push_back (bounds.first->idx);
3727 }
3728 }
3729
3730 std::sort (matches.begin (), matches.end ());
3731
3732 /* Finally call the callback, once per match. */
3733 ULONGEST prev = -1;
3734 bool result = true;
3735 for (offset_type idx : matches)
3736 {
3737 if (prev != idx)
3738 {
3739 if (!match_callback (idx))
3740 {
3741 result = false;
3742 break;
3743 }
3744 prev = idx;
3745 }
3746 }
3747
3748 /* Above we use a type wider than idx's for 'prev', since 0 and
3749 (offset_type)-1 are both possible values. */
3750 static_assert (sizeof (prev) > sizeof (offset_type), "");
3751
3752 return result;
3753 }
3754
3755 #if GDB_SELF_TEST
3756
3757 namespace selftests { namespace dw2_expand_symtabs_matching {
3758
3759 /* A mock .gdb_index/.debug_names-like name index table, enough to
3760 exercise dw2_expand_symtabs_matching_symbol, which works with the
3761 mapped_index_base interface. Builds an index from the symbol list
3762 passed as parameter to the constructor. */
3763 class mock_mapped_index : public mapped_index_base
3764 {
3765 public:
3766 mock_mapped_index (gdb::array_view<const char *> symbols)
3767 : m_symbol_table (symbols)
3768 {}
3769
3770 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3771
3772 /* Return the number of names in the symbol table. */
3773 size_t symbol_name_count () const override
3774 {
3775 return m_symbol_table.size ();
3776 }
3777
3778 /* Get the name of the symbol at IDX in the symbol table. */
3779 const char *symbol_name_at
3780 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3781 {
3782 return m_symbol_table[idx];
3783 }
3784
3785 private:
3786 gdb::array_view<const char *> m_symbol_table;
3787 };
3788
3789 /* Convenience function that converts a NULL pointer to a "<null>"
3790 string, to pass to print routines. */
3791
3792 static const char *
3793 string_or_null (const char *str)
3794 {
3795 return str != NULL ? str : "<null>";
3796 }
3797
3798 /* Check if a lookup_name_info built from
3799 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3800 index. EXPECTED_LIST is the list of expected matches, in expected
3801 matching order. If no match expected, then an empty list is
3802 specified. Returns true on success. On failure prints a warning
3803 indicating the file:line that failed, and returns false. */
3804
3805 static bool
3806 check_match (const char *file, int line,
3807 mock_mapped_index &mock_index,
3808 const char *name, symbol_name_match_type match_type,
3809 bool completion_mode,
3810 std::initializer_list<const char *> expected_list,
3811 dwarf2_per_objfile *per_objfile)
3812 {
3813 lookup_name_info lookup_name (name, match_type, completion_mode);
3814
3815 bool matched = true;
3816
3817 auto mismatch = [&] (const char *expected_str,
3818 const char *got)
3819 {
3820 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3821 "expected=\"%s\", got=\"%s\"\n"),
3822 file, line,
3823 (match_type == symbol_name_match_type::FULL
3824 ? "FULL" : "WILD"),
3825 name, string_or_null (expected_str), string_or_null (got));
3826 matched = false;
3827 };
3828
3829 auto expected_it = expected_list.begin ();
3830 auto expected_end = expected_list.end ();
3831
3832 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3833 nullptr,
3834 [&] (offset_type idx)
3835 {
3836 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3837 const char *expected_str
3838 = expected_it == expected_end ? NULL : *expected_it++;
3839
3840 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3841 mismatch (expected_str, matched_name);
3842 return true;
3843 }, per_objfile);
3844
3845 const char *expected_str
3846 = expected_it == expected_end ? NULL : *expected_it++;
3847 if (expected_str != NULL)
3848 mismatch (expected_str, NULL);
3849
3850 return matched;
3851 }
3852
3853 /* The symbols added to the mock mapped_index for testing (in
3854 canonical form). */
3855 static const char *test_symbols[] = {
3856 "function",
3857 "std::bar",
3858 "std::zfunction",
3859 "std::zfunction2",
3860 "w1::w2",
3861 "ns::foo<char*>",
3862 "ns::foo<int>",
3863 "ns::foo<long>",
3864 "ns2::tmpl<int>::foo2",
3865 "(anonymous namespace)::A::B::C",
3866
3867 /* These are used to check that the increment-last-char in the
3868 matching algorithm for completion doesn't match "t1_fund" when
3869 completing "t1_func". */
3870 "t1_func",
3871 "t1_func1",
3872 "t1_fund",
3873 "t1_fund1",
3874
3875 /* A UTF-8 name with multi-byte sequences to make sure that
3876 cp-name-parser understands this as a single identifier ("função"
3877 is "function" in PT). */
3878 u8"u8função",
3879
3880 /* \377 (0xff) is Latin1 'ÿ'. */
3881 "yfunc\377",
3882
3883 /* \377 (0xff) is Latin1 'ÿ'. */
3884 "\377",
3885 "\377\377123",
3886
3887 /* A name with all sorts of complications. Starts with "z" to make
3888 it easier for the completion tests below. */
3889 #define Z_SYM_NAME \
3890 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3891 "::tuple<(anonymous namespace)::ui*, " \
3892 "std::default_delete<(anonymous namespace)::ui>, void>"
3893
3894 Z_SYM_NAME
3895 };
3896
3897 /* Returns true if the mapped_index_base::find_name_component_bounds
3898 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3899 in completion mode. */
3900
3901 static bool
3902 check_find_bounds_finds (mapped_index_base &index,
3903 const char *search_name,
3904 gdb::array_view<const char *> expected_syms,
3905 dwarf2_per_objfile *per_objfile)
3906 {
3907 lookup_name_info lookup_name (search_name,
3908 symbol_name_match_type::FULL, true);
3909
3910 auto bounds = index.find_name_components_bounds (lookup_name,
3911 language_cplus,
3912 per_objfile);
3913
3914 size_t distance = std::distance (bounds.first, bounds.second);
3915 if (distance != expected_syms.size ())
3916 return false;
3917
3918 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3919 {
3920 auto nc_elem = bounds.first + exp_elem;
3921 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3922 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3923 return false;
3924 }
3925
3926 return true;
3927 }
3928
3929 /* Test the lower-level mapped_index::find_name_component_bounds
3930 method. */
3931
3932 static void
3933 test_mapped_index_find_name_component_bounds ()
3934 {
3935 mock_mapped_index mock_index (test_symbols);
3936
3937 mock_index.build_name_components (NULL /* per_objfile */);
3938
3939 /* Test the lower-level mapped_index::find_name_component_bounds
3940 method in completion mode. */
3941 {
3942 static const char *expected_syms[] = {
3943 "t1_func",
3944 "t1_func1",
3945 };
3946
3947 SELF_CHECK (check_find_bounds_finds
3948 (mock_index, "t1_func", expected_syms,
3949 NULL /* per_objfile */));
3950 }
3951
3952 /* Check that the increment-last-char in the name matching algorithm
3953 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3954 {
3955 static const char *expected_syms1[] = {
3956 "\377",
3957 "\377\377123",
3958 };
3959 SELF_CHECK (check_find_bounds_finds
3960 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3961
3962 static const char *expected_syms2[] = {
3963 "\377\377123",
3964 };
3965 SELF_CHECK (check_find_bounds_finds
3966 (mock_index, "\377\377", expected_syms2,
3967 NULL /* per_objfile */));
3968 }
3969 }
3970
3971 /* Test dw2_expand_symtabs_matching_symbol. */
3972
3973 static void
3974 test_dw2_expand_symtabs_matching_symbol ()
3975 {
3976 mock_mapped_index mock_index (test_symbols);
3977
3978 /* We let all tests run until the end even if some fails, for debug
3979 convenience. */
3980 bool any_mismatch = false;
3981
3982 /* Create the expected symbols list (an initializer_list). Needed
3983 because lists have commas, and we need to pass them to CHECK,
3984 which is a macro. */
3985 #define EXPECT(...) { __VA_ARGS__ }
3986
3987 /* Wrapper for check_match that passes down the current
3988 __FILE__/__LINE__. */
3989 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3990 any_mismatch |= !check_match (__FILE__, __LINE__, \
3991 mock_index, \
3992 NAME, MATCH_TYPE, COMPLETION_MODE, \
3993 EXPECTED_LIST, NULL)
3994
3995 /* Identity checks. */
3996 for (const char *sym : test_symbols)
3997 {
3998 /* Should be able to match all existing symbols. */
3999 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4000 EXPECT (sym));
4001
4002 /* Should be able to match all existing symbols with
4003 parameters. */
4004 std::string with_params = std::string (sym) + "(int)";
4005 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4006 EXPECT (sym));
4007
4008 /* Should be able to match all existing symbols with
4009 parameters and qualifiers. */
4010 with_params = std::string (sym) + " ( int ) const";
4011 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4012 EXPECT (sym));
4013
4014 /* This should really find sym, but cp-name-parser.y doesn't
4015 know about lvalue/rvalue qualifiers yet. */
4016 with_params = std::string (sym) + " ( int ) &&";
4017 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4018 {});
4019 }
4020
4021 /* Check that the name matching algorithm for completion doesn't get
4022 confused with Latin1 'ÿ' / 0xff. */
4023 {
4024 static const char str[] = "\377";
4025 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4026 EXPECT ("\377", "\377\377123"));
4027 }
4028
4029 /* Check that the increment-last-char in the matching algorithm for
4030 completion doesn't match "t1_fund" when completing "t1_func". */
4031 {
4032 static const char str[] = "t1_func";
4033 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4034 EXPECT ("t1_func", "t1_func1"));
4035 }
4036
4037 /* Check that completion mode works at each prefix of the expected
4038 symbol name. */
4039 {
4040 static const char str[] = "function(int)";
4041 size_t len = strlen (str);
4042 std::string lookup;
4043
4044 for (size_t i = 1; i < len; i++)
4045 {
4046 lookup.assign (str, i);
4047 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4048 EXPECT ("function"));
4049 }
4050 }
4051
4052 /* While "w" is a prefix of both components, the match function
4053 should still only be called once. */
4054 {
4055 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4056 EXPECT ("w1::w2"));
4057 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4058 EXPECT ("w1::w2"));
4059 }
4060
4061 /* Same, with a "complicated" symbol. */
4062 {
4063 static const char str[] = Z_SYM_NAME;
4064 size_t len = strlen (str);
4065 std::string lookup;
4066
4067 for (size_t i = 1; i < len; i++)
4068 {
4069 lookup.assign (str, i);
4070 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4071 EXPECT (Z_SYM_NAME));
4072 }
4073 }
4074
4075 /* In FULL mode, an incomplete symbol doesn't match. */
4076 {
4077 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4078 {});
4079 }
4080
4081 /* A complete symbol with parameters matches any overload, since the
4082 index has no overload info. */
4083 {
4084 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4085 EXPECT ("std::zfunction", "std::zfunction2"));
4086 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4087 EXPECT ("std::zfunction", "std::zfunction2"));
4088 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4089 EXPECT ("std::zfunction", "std::zfunction2"));
4090 }
4091
4092 /* Check that whitespace is ignored appropriately. A symbol with a
4093 template argument list. */
4094 {
4095 static const char expected[] = "ns::foo<int>";
4096 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4097 EXPECT (expected));
4098 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4099 EXPECT (expected));
4100 }
4101
4102 /* Check that whitespace is ignored appropriately. A symbol with a
4103 template argument list that includes a pointer. */
4104 {
4105 static const char expected[] = "ns::foo<char*>";
4106 /* Try both completion and non-completion modes. */
4107 static const bool completion_mode[2] = {false, true};
4108 for (size_t i = 0; i < 2; i++)
4109 {
4110 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4111 completion_mode[i], EXPECT (expected));
4112 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4113 completion_mode[i], EXPECT (expected));
4114
4115 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4116 completion_mode[i], EXPECT (expected));
4117 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4118 completion_mode[i], EXPECT (expected));
4119 }
4120 }
4121
4122 {
4123 /* Check method qualifiers are ignored. */
4124 static const char expected[] = "ns::foo<char*>";
4125 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4126 symbol_name_match_type::FULL, true, EXPECT (expected));
4127 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4128 symbol_name_match_type::FULL, true, EXPECT (expected));
4129 CHECK_MATCH ("foo < char * > ( int ) const",
4130 symbol_name_match_type::WILD, true, EXPECT (expected));
4131 CHECK_MATCH ("foo < char * > ( int ) &&",
4132 symbol_name_match_type::WILD, true, EXPECT (expected));
4133 }
4134
4135 /* Test lookup names that don't match anything. */
4136 {
4137 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4138 {});
4139
4140 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4141 {});
4142 }
4143
4144 /* Some wild matching tests, exercising "(anonymous namespace)",
4145 which should not be confused with a parameter list. */
4146 {
4147 static const char *syms[] = {
4148 "A::B::C",
4149 "B::C",
4150 "C",
4151 "A :: B :: C ( int )",
4152 "B :: C ( int )",
4153 "C ( int )",
4154 };
4155
4156 for (const char *s : syms)
4157 {
4158 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4159 EXPECT ("(anonymous namespace)::A::B::C"));
4160 }
4161 }
4162
4163 {
4164 static const char expected[] = "ns2::tmpl<int>::foo2";
4165 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4166 EXPECT (expected));
4167 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4168 EXPECT (expected));
4169 }
4170
4171 SELF_CHECK (!any_mismatch);
4172
4173 #undef EXPECT
4174 #undef CHECK_MATCH
4175 }
4176
4177 static void
4178 run_test ()
4179 {
4180 test_mapped_index_find_name_component_bounds ();
4181 test_dw2_expand_symtabs_matching_symbol ();
4182 }
4183
4184 }} // namespace selftests::dw2_expand_symtabs_matching
4185
4186 #endif /* GDB_SELF_TEST */
4187
4188 /* If FILE_MATCHER is NULL or if PER_CU has
4189 dwarf2_per_cu_quick_data::MARK set (see
4190 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4191 EXPANSION_NOTIFY on it. */
4192
4193 static bool
4194 dw2_expand_symtabs_matching_one
4195 (dwarf2_per_cu_data *per_cu,
4196 dwarf2_per_objfile *per_objfile,
4197 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4198 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4199 {
4200 if (file_matcher == NULL || per_cu->v.quick->mark)
4201 {
4202 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4203
4204 compunit_symtab *symtab
4205 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4206 gdb_assert (symtab != nullptr);
4207
4208 if (expansion_notify != NULL && symtab_was_null)
4209 return expansion_notify (symtab);
4210 }
4211 return true;
4212 }
4213
4214 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4215 matched, to expand corresponding CUs that were marked. IDX is the
4216 index of the symbol name that matched. */
4217
4218 static bool
4219 dw2_expand_marked_cus
4220 (dwarf2_per_objfile *per_objfile, offset_type idx,
4221 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4222 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4223 block_search_flags search_flags,
4224 search_domain kind)
4225 {
4226 offset_type vec_len, vec_idx;
4227 bool global_seen = false;
4228 mapped_index &index = *per_objfile->per_bfd->index_table;
4229
4230 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4231 vec_len = vec[0];
4232 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4233 {
4234 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4235 /* This value is only valid for index versions >= 7. */
4236 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4237 gdb_index_symbol_kind symbol_kind =
4238 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4239 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4240 /* Only check the symbol attributes if they're present.
4241 Indices prior to version 7 don't record them,
4242 and indices >= 7 may elide them for certain symbols
4243 (gold does this). */
4244 int attrs_valid =
4245 (index.version >= 7
4246 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4247
4248 /* Work around gold/15646. */
4249 if (attrs_valid
4250 && !is_static
4251 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4252 {
4253 if (global_seen)
4254 continue;
4255
4256 global_seen = true;
4257 }
4258
4259 /* Only check the symbol's kind if it has one. */
4260 if (attrs_valid)
4261 {
4262 if (is_static)
4263 {
4264 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4265 continue;
4266 }
4267 else
4268 {
4269 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4270 continue;
4271 }
4272
4273 switch (kind)
4274 {
4275 case VARIABLES_DOMAIN:
4276 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4277 continue;
4278 break;
4279 case FUNCTIONS_DOMAIN:
4280 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4281 continue;
4282 break;
4283 case TYPES_DOMAIN:
4284 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4285 continue;
4286 break;
4287 case MODULES_DOMAIN:
4288 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4289 continue;
4290 break;
4291 default:
4292 break;
4293 }
4294 }
4295
4296 /* Don't crash on bad data. */
4297 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4298 {
4299 complaint (_(".gdb_index entry has bad CU index"
4300 " [in module %s]"), objfile_name (per_objfile->objfile));
4301 continue;
4302 }
4303
4304 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4305 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4306 expansion_notify))
4307 return false;
4308 }
4309
4310 return true;
4311 }
4312
4313 /* If FILE_MATCHER is non-NULL, set all the
4314 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4315 that match FILE_MATCHER. */
4316
4317 static void
4318 dw_expand_symtabs_matching_file_matcher
4319 (dwarf2_per_objfile *per_objfile,
4320 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4321 {
4322 if (file_matcher == NULL)
4323 return;
4324
4325 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4326 htab_eq_pointer,
4327 NULL, xcalloc, xfree));
4328 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4329 htab_eq_pointer,
4330 NULL, xcalloc, xfree));
4331
4332 /* The rule is CUs specify all the files, including those used by
4333 any TU, so there's no need to scan TUs here. */
4334
4335 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4336 {
4337 QUIT;
4338
4339 if (per_cu->is_debug_types)
4340 continue;
4341 per_cu->v.quick->mark = 0;
4342
4343 /* We only need to look at symtabs not already expanded. */
4344 if (per_objfile->symtab_set_p (per_cu.get ()))
4345 continue;
4346
4347 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4348 per_objfile);
4349 if (file_data == NULL)
4350 continue;
4351
4352 if (htab_find (visited_not_found.get (), file_data) != NULL)
4353 continue;
4354 else if (htab_find (visited_found.get (), file_data) != NULL)
4355 {
4356 per_cu->v.quick->mark = 1;
4357 continue;
4358 }
4359
4360 for (int j = 0; j < file_data->num_file_names; ++j)
4361 {
4362 const char *this_real_name;
4363
4364 if (file_matcher (file_data->file_names[j], false))
4365 {
4366 per_cu->v.quick->mark = 1;
4367 break;
4368 }
4369
4370 /* Before we invoke realpath, which can get expensive when many
4371 files are involved, do a quick comparison of the basenames. */
4372 if (!basenames_may_differ
4373 && !file_matcher (lbasename (file_data->file_names[j]),
4374 true))
4375 continue;
4376
4377 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4378 if (file_matcher (this_real_name, false))
4379 {
4380 per_cu->v.quick->mark = 1;
4381 break;
4382 }
4383 }
4384
4385 void **slot = htab_find_slot (per_cu->v.quick->mark
4386 ? visited_found.get ()
4387 : visited_not_found.get (),
4388 file_data, INSERT);
4389 *slot = file_data;
4390 }
4391 }
4392
4393 bool
4394 dwarf2_gdb_index::expand_symtabs_matching
4395 (struct objfile *objfile,
4396 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4397 const lookup_name_info *lookup_name,
4398 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4399 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4400 block_search_flags search_flags,
4401 domain_enum domain,
4402 enum search_domain kind)
4403 {
4404 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4405
4406 /* index_table is NULL if OBJF_READNOW. */
4407 if (!per_objfile->per_bfd->index_table)
4408 return true;
4409
4410 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4411
4412 /* This invariant is documented in quick-functions.h. */
4413 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4414 if (lookup_name == nullptr)
4415 {
4416 for (dwarf2_per_cu_data *per_cu
4417 : all_comp_units_range (per_objfile->per_bfd))
4418 {
4419 QUIT;
4420
4421 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4422 file_matcher,
4423 expansion_notify))
4424 return false;
4425 }
4426 return true;
4427 }
4428
4429 mapped_index &index = *per_objfile->per_bfd->index_table;
4430
4431 bool result
4432 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4433 symbol_matcher,
4434 [&] (offset_type idx)
4435 {
4436 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4437 expansion_notify, search_flags, kind))
4438 return false;
4439 return true;
4440 }, per_objfile);
4441
4442 return result;
4443 }
4444
4445 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4446 symtab. */
4447
4448 static struct compunit_symtab *
4449 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4450 CORE_ADDR pc)
4451 {
4452 int i;
4453
4454 if (cust->blockvector () != nullptr
4455 && blockvector_contains_pc (cust->blockvector (), pc))
4456 return cust;
4457
4458 if (cust->includes == NULL)
4459 return NULL;
4460
4461 for (i = 0; cust->includes[i]; ++i)
4462 {
4463 struct compunit_symtab *s = cust->includes[i];
4464
4465 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4466 if (s != NULL)
4467 return s;
4468 }
4469
4470 return NULL;
4471 }
4472
4473 struct compunit_symtab *
4474 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4475 (struct objfile *objfile,
4476 struct bound_minimal_symbol msymbol,
4477 CORE_ADDR pc,
4478 struct obj_section *section,
4479 int warn_if_readin)
4480 {
4481 struct dwarf2_per_cu_data *data;
4482 struct compunit_symtab *result;
4483
4484 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4485 if (per_objfile->per_bfd->index_addrmap == nullptr)
4486 return NULL;
4487
4488 CORE_ADDR baseaddr = objfile->text_section_offset ();
4489 data = ((struct dwarf2_per_cu_data *)
4490 addrmap_find (per_objfile->per_bfd->index_addrmap,
4491 pc - baseaddr));
4492 if (!data)
4493 return NULL;
4494
4495 if (warn_if_readin && per_objfile->symtab_set_p (data))
4496 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4497 paddress (objfile->arch (), pc));
4498
4499 result = recursively_find_pc_sect_compunit_symtab
4500 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4501
4502 gdb_assert (result != NULL);
4503 return result;
4504 }
4505
4506 void
4507 dwarf2_base_index_functions::map_symbol_filenames
4508 (struct objfile *objfile,
4509 gdb::function_view<symbol_filename_ftype> fun,
4510 bool need_fullname)
4511 {
4512 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4513
4514 /* Use caches to ensure we only call FUN once for each filename. */
4515 filename_seen_cache filenames_cache;
4516 std::unordered_set<quick_file_names *> qfn_cache;
4517
4518 /* The rule is CUs specify all the files, including those used by any TU,
4519 so there's no need to scan TUs here. We can ignore file names coming
4520 from already-expanded CUs. It is possible that an expanded CU might
4521 reuse the file names data from a currently unexpanded CU, in this
4522 case we don't want to report the files from the unexpanded CU. */
4523
4524 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4525 {
4526 if (!per_cu->is_debug_types
4527 && per_objfile->symtab_set_p (per_cu.get ()))
4528 {
4529 if (per_cu->v.quick->file_names != nullptr)
4530 qfn_cache.insert (per_cu->v.quick->file_names);
4531 }
4532 }
4533
4534 for (dwarf2_per_cu_data *per_cu
4535 : all_comp_units_range (per_objfile->per_bfd))
4536 {
4537 /* We only need to look at symtabs not already expanded. */
4538 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4539 continue;
4540
4541 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4542 if (file_data == nullptr
4543 || qfn_cache.find (file_data) != qfn_cache.end ())
4544 continue;
4545
4546 for (int j = 0; j < file_data->num_file_names; ++j)
4547 {
4548 const char *filename = file_data->file_names[j];
4549 const char *key = filename;
4550 const char *fullname = nullptr;
4551
4552 if (need_fullname)
4553 {
4554 fullname = dw2_get_real_path (per_objfile, file_data, j);
4555 key = fullname;
4556 }
4557
4558 if (!filenames_cache.seen (key))
4559 fun (filename, fullname);
4560 }
4561 }
4562 }
4563
4564 bool
4565 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4566 {
4567 return true;
4568 }
4569
4570 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4571
4572 bool
4573 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4574 {
4575 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4576
4577 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4578 {
4579 /* Is this already expanded? */
4580 if (per_objfile->symtab_set_p (per_cu.get ()))
4581 continue;
4582
4583 /* It has not yet been expanded. */
4584 return true;
4585 }
4586
4587 return false;
4588 }
4589
4590 /* DWARF-5 debug_names reader. */
4591
4592 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4593 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4594
4595 /* A helper function that reads the .debug_names section in SECTION
4596 and fills in MAP. FILENAME is the name of the file containing the
4597 section; it is used for error reporting.
4598
4599 Returns true if all went well, false otherwise. */
4600
4601 static bool
4602 read_debug_names_from_section (struct objfile *objfile,
4603 const char *filename,
4604 struct dwarf2_section_info *section,
4605 mapped_debug_names &map)
4606 {
4607 if (section->empty ())
4608 return false;
4609
4610 /* Older elfutils strip versions could keep the section in the main
4611 executable while splitting it for the separate debug info file. */
4612 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4613 return false;
4614
4615 section->read (objfile);
4616
4617 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4618
4619 const gdb_byte *addr = section->buffer;
4620
4621 bfd *const abfd = section->get_bfd_owner ();
4622
4623 unsigned int bytes_read;
4624 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4625 addr += bytes_read;
4626
4627 map.dwarf5_is_dwarf64 = bytes_read != 4;
4628 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4629 if (bytes_read + length != section->size)
4630 {
4631 /* There may be multiple per-CU indices. */
4632 warning (_("Section .debug_names in %s length %s does not match "
4633 "section length %s, ignoring .debug_names."),
4634 filename, plongest (bytes_read + length),
4635 pulongest (section->size));
4636 return false;
4637 }
4638
4639 /* The version number. */
4640 uint16_t version = read_2_bytes (abfd, addr);
4641 addr += 2;
4642 if (version != 5)
4643 {
4644 warning (_("Section .debug_names in %s has unsupported version %d, "
4645 "ignoring .debug_names."),
4646 filename, version);
4647 return false;
4648 }
4649
4650 /* Padding. */
4651 uint16_t padding = read_2_bytes (abfd, addr);
4652 addr += 2;
4653 if (padding != 0)
4654 {
4655 warning (_("Section .debug_names in %s has unsupported padding %d, "
4656 "ignoring .debug_names."),
4657 filename, padding);
4658 return false;
4659 }
4660
4661 /* comp_unit_count - The number of CUs in the CU list. */
4662 map.cu_count = read_4_bytes (abfd, addr);
4663 addr += 4;
4664
4665 /* local_type_unit_count - The number of TUs in the local TU
4666 list. */
4667 map.tu_count = read_4_bytes (abfd, addr);
4668 addr += 4;
4669
4670 /* foreign_type_unit_count - The number of TUs in the foreign TU
4671 list. */
4672 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4673 addr += 4;
4674 if (foreign_tu_count != 0)
4675 {
4676 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4677 "ignoring .debug_names."),
4678 filename, static_cast<unsigned long> (foreign_tu_count));
4679 return false;
4680 }
4681
4682 /* bucket_count - The number of hash buckets in the hash lookup
4683 table. */
4684 map.bucket_count = read_4_bytes (abfd, addr);
4685 addr += 4;
4686
4687 /* name_count - The number of unique names in the index. */
4688 map.name_count = read_4_bytes (abfd, addr);
4689 addr += 4;
4690
4691 /* abbrev_table_size - The size in bytes of the abbreviations
4692 table. */
4693 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4694 addr += 4;
4695
4696 /* augmentation_string_size - The size in bytes of the augmentation
4697 string. This value is rounded up to a multiple of 4. */
4698 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4699 addr += 4;
4700 map.augmentation_is_gdb = ((augmentation_string_size
4701 == sizeof (dwarf5_augmentation))
4702 && memcmp (addr, dwarf5_augmentation,
4703 sizeof (dwarf5_augmentation)) == 0);
4704 augmentation_string_size += (-augmentation_string_size) & 3;
4705 addr += augmentation_string_size;
4706
4707 /* List of CUs */
4708 map.cu_table_reordered = addr;
4709 addr += map.cu_count * map.offset_size;
4710
4711 /* List of Local TUs */
4712 map.tu_table_reordered = addr;
4713 addr += map.tu_count * map.offset_size;
4714
4715 /* Hash Lookup Table */
4716 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4717 addr += map.bucket_count * 4;
4718 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4719 addr += map.name_count * 4;
4720
4721 /* Name Table */
4722 map.name_table_string_offs_reordered = addr;
4723 addr += map.name_count * map.offset_size;
4724 map.name_table_entry_offs_reordered = addr;
4725 addr += map.name_count * map.offset_size;
4726
4727 const gdb_byte *abbrev_table_start = addr;
4728 for (;;)
4729 {
4730 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4731 addr += bytes_read;
4732 if (index_num == 0)
4733 break;
4734
4735 const auto insertpair
4736 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4737 if (!insertpair.second)
4738 {
4739 warning (_("Section .debug_names in %s has duplicate index %s, "
4740 "ignoring .debug_names."),
4741 filename, pulongest (index_num));
4742 return false;
4743 }
4744 mapped_debug_names::index_val &indexval = insertpair.first->second;
4745 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4746 addr += bytes_read;
4747
4748 for (;;)
4749 {
4750 mapped_debug_names::index_val::attr attr;
4751 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4752 addr += bytes_read;
4753 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4754 addr += bytes_read;
4755 if (attr.form == DW_FORM_implicit_const)
4756 {
4757 attr.implicit_const = read_signed_leb128 (abfd, addr,
4758 &bytes_read);
4759 addr += bytes_read;
4760 }
4761 if (attr.dw_idx == 0 && attr.form == 0)
4762 break;
4763 indexval.attr_vec.push_back (std::move (attr));
4764 }
4765 }
4766 if (addr != abbrev_table_start + abbrev_table_size)
4767 {
4768 warning (_("Section .debug_names in %s has abbreviation_table "
4769 "of size %s vs. written as %u, ignoring .debug_names."),
4770 filename, plongest (addr - abbrev_table_start),
4771 abbrev_table_size);
4772 return false;
4773 }
4774 map.entry_pool = addr;
4775
4776 return true;
4777 }
4778
4779 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4780 list. */
4781
4782 static void
4783 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4784 const mapped_debug_names &map,
4785 dwarf2_section_info &section,
4786 bool is_dwz)
4787 {
4788 if (!map.augmentation_is_gdb)
4789 {
4790 for (uint32_t i = 0; i < map.cu_count; ++i)
4791 {
4792 sect_offset sect_off
4793 = (sect_offset) (extract_unsigned_integer
4794 (map.cu_table_reordered + i * map.offset_size,
4795 map.offset_size,
4796 map.dwarf5_byte_order));
4797 /* We don't know the length of the CU, because the CU list in a
4798 .debug_names index can be incomplete, so we can't use the start
4799 of the next CU as end of this CU. We create the CUs here with
4800 length 0, and in cutu_reader::cutu_reader we'll fill in the
4801 actual length. */
4802 dwarf2_per_cu_data_up per_cu
4803 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4804 sect_off, 0);
4805 per_bfd->all_comp_units.push_back (std::move (per_cu));
4806 }
4807 return;
4808 }
4809
4810 sect_offset sect_off_prev;
4811 for (uint32_t i = 0; i <= map.cu_count; ++i)
4812 {
4813 sect_offset sect_off_next;
4814 if (i < map.cu_count)
4815 {
4816 sect_off_next
4817 = (sect_offset) (extract_unsigned_integer
4818 (map.cu_table_reordered + i * map.offset_size,
4819 map.offset_size,
4820 map.dwarf5_byte_order));
4821 }
4822 else
4823 sect_off_next = (sect_offset) section.size;
4824 if (i >= 1)
4825 {
4826 const ULONGEST length = sect_off_next - sect_off_prev;
4827 dwarf2_per_cu_data_up per_cu
4828 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4829 sect_off_prev, length);
4830 per_bfd->all_comp_units.push_back (std::move (per_cu));
4831 }
4832 sect_off_prev = sect_off_next;
4833 }
4834 }
4835
4836 /* Read the CU list from the mapped index, and use it to create all
4837 the CU objects for this dwarf2_per_objfile. */
4838
4839 static void
4840 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4841 const mapped_debug_names &map,
4842 const mapped_debug_names &dwz_map)
4843 {
4844 gdb_assert (per_bfd->all_comp_units.empty ());
4845 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4846
4847 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4848 false /* is_dwz */);
4849
4850 if (dwz_map.cu_count == 0)
4851 return;
4852
4853 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4854 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4855 true /* is_dwz */);
4856 }
4857
4858 /* Read .debug_names. If everything went ok, initialize the "quick"
4859 elements of all the CUs and return true. Otherwise, return false. */
4860
4861 static bool
4862 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4863 {
4864 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4865 mapped_debug_names dwz_map;
4866 struct objfile *objfile = per_objfile->objfile;
4867 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4868
4869 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4870 &per_bfd->debug_names, *map))
4871 return false;
4872
4873 /* Don't use the index if it's empty. */
4874 if (map->name_count == 0)
4875 return false;
4876
4877 /* If there is a .dwz file, read it so we can get its CU list as
4878 well. */
4879 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4880 if (dwz != NULL)
4881 {
4882 if (!read_debug_names_from_section (objfile,
4883 bfd_get_filename (dwz->dwz_bfd.get ()),
4884 &dwz->debug_names, dwz_map))
4885 {
4886 warning (_("could not read '.debug_names' section from %s; skipping"),
4887 bfd_get_filename (dwz->dwz_bfd.get ()));
4888 return false;
4889 }
4890 }
4891
4892 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4893
4894 if (map->tu_count != 0)
4895 {
4896 /* We can only handle a single .debug_types when we have an
4897 index. */
4898 if (per_bfd->types.size () != 1)
4899 return false;
4900
4901 dwarf2_section_info *section = &per_bfd->types[0];
4902
4903 create_signatured_type_table_from_debug_names
4904 (per_objfile, *map, section, &per_bfd->abbrev);
4905 }
4906
4907 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4908
4909 per_bfd->debug_names_table = std::move (map);
4910 per_bfd->using_index = 1;
4911 per_bfd->quick_file_names_table =
4912 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4913
4914 return true;
4915 }
4916
4917 /* Type used to manage iterating over all CUs looking for a symbol for
4918 .debug_names. */
4919
4920 class dw2_debug_names_iterator
4921 {
4922 public:
4923 dw2_debug_names_iterator (const mapped_debug_names &map,
4924 block_search_flags block_index,
4925 domain_enum domain,
4926 const char *name, dwarf2_per_objfile *per_objfile)
4927 : m_map (map), m_block_index (block_index), m_domain (domain),
4928 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4929 m_per_objfile (per_objfile)
4930 {}
4931
4932 dw2_debug_names_iterator (const mapped_debug_names &map,
4933 search_domain search, uint32_t namei,
4934 dwarf2_per_objfile *per_objfile,
4935 domain_enum domain = UNDEF_DOMAIN)
4936 : m_map (map),
4937 m_domain (domain),
4938 m_search (search),
4939 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4940 m_per_objfile (per_objfile)
4941 {}
4942
4943 dw2_debug_names_iterator (const mapped_debug_names &map,
4944 block_search_flags block_index, domain_enum domain,
4945 uint32_t namei, dwarf2_per_objfile *per_objfile)
4946 : m_map (map), m_block_index (block_index), m_domain (domain),
4947 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4948 m_per_objfile (per_objfile)
4949 {}
4950
4951 /* Return the next matching CU or NULL if there are no more. */
4952 dwarf2_per_cu_data *next ();
4953
4954 private:
4955 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4956 const char *name,
4957 dwarf2_per_objfile *per_objfile);
4958 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4959 uint32_t namei,
4960 dwarf2_per_objfile *per_objfile);
4961
4962 /* The internalized form of .debug_names. */
4963 const mapped_debug_names &m_map;
4964
4965 /* Restrict the search to these blocks. */
4966 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4967 | SEARCH_STATIC_BLOCK);
4968
4969 /* The kind of symbol we're looking for. */
4970 const domain_enum m_domain = UNDEF_DOMAIN;
4971 const search_domain m_search = ALL_DOMAIN;
4972
4973 /* The list of CUs from the index entry of the symbol, or NULL if
4974 not found. */
4975 const gdb_byte *m_addr;
4976
4977 dwarf2_per_objfile *m_per_objfile;
4978 };
4979
4980 const char *
4981 mapped_debug_names::namei_to_name
4982 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4983 {
4984 const ULONGEST namei_string_offs
4985 = extract_unsigned_integer ((name_table_string_offs_reordered
4986 + namei * offset_size),
4987 offset_size,
4988 dwarf5_byte_order);
4989 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4990 }
4991
4992 /* Find a slot in .debug_names for the object named NAME. If NAME is
4993 found, return pointer to its pool data. If NAME cannot be found,
4994 return NULL. */
4995
4996 const gdb_byte *
4997 dw2_debug_names_iterator::find_vec_in_debug_names
4998 (const mapped_debug_names &map, const char *name,
4999 dwarf2_per_objfile *per_objfile)
5000 {
5001 int (*cmp) (const char *, const char *);
5002
5003 gdb::unique_xmalloc_ptr<char> without_params;
5004 if (current_language->la_language == language_cplus
5005 || current_language->la_language == language_fortran
5006 || current_language->la_language == language_d)
5007 {
5008 /* NAME is already canonical. Drop any qualifiers as
5009 .debug_names does not contain any. */
5010
5011 if (strchr (name, '(') != NULL)
5012 {
5013 without_params = cp_remove_params (name);
5014 if (without_params != NULL)
5015 name = without_params.get ();
5016 }
5017 }
5018
5019 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5020
5021 const uint32_t full_hash = dwarf5_djb_hash (name);
5022 uint32_t namei
5023 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5024 (map.bucket_table_reordered
5025 + (full_hash % map.bucket_count)), 4,
5026 map.dwarf5_byte_order);
5027 if (namei == 0)
5028 return NULL;
5029 --namei;
5030 if (namei >= map.name_count)
5031 {
5032 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5033 "[in module %s]"),
5034 namei, map.name_count,
5035 objfile_name (per_objfile->objfile));
5036 return NULL;
5037 }
5038
5039 for (;;)
5040 {
5041 const uint32_t namei_full_hash
5042 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5043 (map.hash_table_reordered + namei), 4,
5044 map.dwarf5_byte_order);
5045 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5046 return NULL;
5047
5048 if (full_hash == namei_full_hash)
5049 {
5050 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5051
5052 #if 0 /* An expensive sanity check. */
5053 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5054 {
5055 complaint (_("Wrong .debug_names hash for string at index %u "
5056 "[in module %s]"),
5057 namei, objfile_name (dwarf2_per_objfile->objfile));
5058 return NULL;
5059 }
5060 #endif
5061
5062 if (cmp (namei_string, name) == 0)
5063 {
5064 const ULONGEST namei_entry_offs
5065 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5066 + namei * map.offset_size),
5067 map.offset_size, map.dwarf5_byte_order);
5068 return map.entry_pool + namei_entry_offs;
5069 }
5070 }
5071
5072 ++namei;
5073 if (namei >= map.name_count)
5074 return NULL;
5075 }
5076 }
5077
5078 const gdb_byte *
5079 dw2_debug_names_iterator::find_vec_in_debug_names
5080 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5081 {
5082 if (namei >= map.name_count)
5083 {
5084 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5085 "[in module %s]"),
5086 namei, map.name_count,
5087 objfile_name (per_objfile->objfile));
5088 return NULL;
5089 }
5090
5091 const ULONGEST namei_entry_offs
5092 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5093 + namei * map.offset_size),
5094 map.offset_size, map.dwarf5_byte_order);
5095 return map.entry_pool + namei_entry_offs;
5096 }
5097
5098 /* See dw2_debug_names_iterator. */
5099
5100 dwarf2_per_cu_data *
5101 dw2_debug_names_iterator::next ()
5102 {
5103 if (m_addr == NULL)
5104 return NULL;
5105
5106 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5107 struct objfile *objfile = m_per_objfile->objfile;
5108 bfd *const abfd = objfile->obfd;
5109
5110 again:
5111
5112 unsigned int bytes_read;
5113 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5114 m_addr += bytes_read;
5115 if (abbrev == 0)
5116 return NULL;
5117
5118 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5119 if (indexval_it == m_map.abbrev_map.cend ())
5120 {
5121 complaint (_("Wrong .debug_names undefined abbrev code %s "
5122 "[in module %s]"),
5123 pulongest (abbrev), objfile_name (objfile));
5124 return NULL;
5125 }
5126 const mapped_debug_names::index_val &indexval = indexval_it->second;
5127 enum class symbol_linkage {
5128 unknown,
5129 static_,
5130 extern_,
5131 } symbol_linkage_ = symbol_linkage::unknown;
5132 dwarf2_per_cu_data *per_cu = NULL;
5133 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5134 {
5135 ULONGEST ull;
5136 switch (attr.form)
5137 {
5138 case DW_FORM_implicit_const:
5139 ull = attr.implicit_const;
5140 break;
5141 case DW_FORM_flag_present:
5142 ull = 1;
5143 break;
5144 case DW_FORM_udata:
5145 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5146 m_addr += bytes_read;
5147 break;
5148 case DW_FORM_ref4:
5149 ull = read_4_bytes (abfd, m_addr);
5150 m_addr += 4;
5151 break;
5152 case DW_FORM_ref8:
5153 ull = read_8_bytes (abfd, m_addr);
5154 m_addr += 8;
5155 break;
5156 case DW_FORM_ref_sig8:
5157 ull = read_8_bytes (abfd, m_addr);
5158 m_addr += 8;
5159 break;
5160 default:
5161 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5162 dwarf_form_name (attr.form),
5163 objfile_name (objfile));
5164 return NULL;
5165 }
5166 switch (attr.dw_idx)
5167 {
5168 case DW_IDX_compile_unit:
5169 /* Don't crash on bad data. */
5170 if (ull >= per_bfd->all_comp_units.size ())
5171 {
5172 complaint (_(".debug_names entry has bad CU index %s"
5173 " [in module %s]"),
5174 pulongest (ull),
5175 objfile_name (objfile));
5176 continue;
5177 }
5178 per_cu = per_bfd->get_cu (ull);
5179 break;
5180 case DW_IDX_type_unit:
5181 /* Don't crash on bad data. */
5182 if (ull >= per_bfd->tu_stats.nr_tus)
5183 {
5184 complaint (_(".debug_names entry has bad TU index %s"
5185 " [in module %s]"),
5186 pulongest (ull),
5187 objfile_name (objfile));
5188 continue;
5189 }
5190 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5191 break;
5192 case DW_IDX_die_offset:
5193 /* In a per-CU index (as opposed to a per-module index), index
5194 entries without CU attribute implicitly refer to the single CU. */
5195 if (per_cu == NULL)
5196 per_cu = per_bfd->get_cu (0);
5197 break;
5198 case DW_IDX_GNU_internal:
5199 if (!m_map.augmentation_is_gdb)
5200 break;
5201 symbol_linkage_ = symbol_linkage::static_;
5202 break;
5203 case DW_IDX_GNU_external:
5204 if (!m_map.augmentation_is_gdb)
5205 break;
5206 symbol_linkage_ = symbol_linkage::extern_;
5207 break;
5208 }
5209 }
5210
5211 /* Skip if already read in. */
5212 if (m_per_objfile->symtab_set_p (per_cu))
5213 goto again;
5214
5215 /* Check static vs global. */
5216 if (symbol_linkage_ != symbol_linkage::unknown)
5217 {
5218 if (symbol_linkage_ == symbol_linkage::static_)
5219 {
5220 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5221 goto again;
5222 }
5223 else
5224 {
5225 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5226 goto again;
5227 }
5228 }
5229
5230 /* Match dw2_symtab_iter_next, symbol_kind
5231 and debug_names::psymbol_tag. */
5232 switch (m_domain)
5233 {
5234 case VAR_DOMAIN:
5235 switch (indexval.dwarf_tag)
5236 {
5237 case DW_TAG_variable:
5238 case DW_TAG_subprogram:
5239 /* Some types are also in VAR_DOMAIN. */
5240 case DW_TAG_typedef:
5241 case DW_TAG_structure_type:
5242 break;
5243 default:
5244 goto again;
5245 }
5246 break;
5247 case STRUCT_DOMAIN:
5248 switch (indexval.dwarf_tag)
5249 {
5250 case DW_TAG_typedef:
5251 case DW_TAG_structure_type:
5252 break;
5253 default:
5254 goto again;
5255 }
5256 break;
5257 case LABEL_DOMAIN:
5258 switch (indexval.dwarf_tag)
5259 {
5260 case 0:
5261 case DW_TAG_variable:
5262 break;
5263 default:
5264 goto again;
5265 }
5266 break;
5267 case MODULE_DOMAIN:
5268 switch (indexval.dwarf_tag)
5269 {
5270 case DW_TAG_module:
5271 break;
5272 default:
5273 goto again;
5274 }
5275 break;
5276 default:
5277 break;
5278 }
5279
5280 /* Match dw2_expand_symtabs_matching, symbol_kind and
5281 debug_names::psymbol_tag. */
5282 switch (m_search)
5283 {
5284 case VARIABLES_DOMAIN:
5285 switch (indexval.dwarf_tag)
5286 {
5287 case DW_TAG_variable:
5288 break;
5289 default:
5290 goto again;
5291 }
5292 break;
5293 case FUNCTIONS_DOMAIN:
5294 switch (indexval.dwarf_tag)
5295 {
5296 case DW_TAG_subprogram:
5297 break;
5298 default:
5299 goto again;
5300 }
5301 break;
5302 case TYPES_DOMAIN:
5303 switch (indexval.dwarf_tag)
5304 {
5305 case DW_TAG_typedef:
5306 case DW_TAG_structure_type:
5307 break;
5308 default:
5309 goto again;
5310 }
5311 break;
5312 case MODULES_DOMAIN:
5313 switch (indexval.dwarf_tag)
5314 {
5315 case DW_TAG_module:
5316 break;
5317 default:
5318 goto again;
5319 }
5320 default:
5321 break;
5322 }
5323
5324 return per_cu;
5325 }
5326
5327 /* This dumps minimal information about .debug_names. It is called
5328 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5329 uses this to verify that .debug_names has been loaded. */
5330
5331 void
5332 dwarf2_debug_names_index::dump (struct objfile *objfile)
5333 {
5334 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5335
5336 gdb_assert (per_objfile->per_bfd->using_index);
5337 printf_filtered (".debug_names:");
5338 if (per_objfile->per_bfd->debug_names_table)
5339 printf_filtered (" exists\n");
5340 else
5341 printf_filtered (" faked for \"readnow\"\n");
5342 printf_filtered ("\n");
5343 }
5344
5345 void
5346 dwarf2_debug_names_index::expand_matching_symbols
5347 (struct objfile *objfile,
5348 const lookup_name_info &name, domain_enum domain,
5349 int global,
5350 symbol_compare_ftype *ordered_compare)
5351 {
5352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5353
5354 /* debug_names_table is NULL if OBJF_READNOW. */
5355 if (!per_objfile->per_bfd->debug_names_table)
5356 return;
5357
5358 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5359 const block_search_flags block_flags
5360 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5361
5362 const char *match_name = name.ada ().lookup_name ().c_str ();
5363 auto matcher = [&] (const char *symname)
5364 {
5365 if (ordered_compare == nullptr)
5366 return true;
5367 return ordered_compare (symname, match_name) == 0;
5368 };
5369
5370 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5371 [&] (offset_type namei)
5372 {
5373 /* The name was matched, now expand corresponding CUs that were
5374 marked. */
5375 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5376 per_objfile);
5377
5378 struct dwarf2_per_cu_data *per_cu;
5379 while ((per_cu = iter.next ()) != NULL)
5380 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5381 nullptr);
5382 return true;
5383 }, per_objfile);
5384 }
5385
5386 bool
5387 dwarf2_debug_names_index::expand_symtabs_matching
5388 (struct objfile *objfile,
5389 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5390 const lookup_name_info *lookup_name,
5391 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5392 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5393 block_search_flags search_flags,
5394 domain_enum domain,
5395 enum search_domain kind)
5396 {
5397 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5398
5399 /* debug_names_table is NULL if OBJF_READNOW. */
5400 if (!per_objfile->per_bfd->debug_names_table)
5401 return true;
5402
5403 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5404
5405 /* This invariant is documented in quick-functions.h. */
5406 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5407 if (lookup_name == nullptr)
5408 {
5409 for (dwarf2_per_cu_data *per_cu
5410 : all_comp_units_range (per_objfile->per_bfd))
5411 {
5412 QUIT;
5413
5414 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5415 file_matcher,
5416 expansion_notify))
5417 return false;
5418 }
5419 return true;
5420 }
5421
5422 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5423
5424 bool result
5425 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5426 symbol_matcher,
5427 [&] (offset_type namei)
5428 {
5429 /* The name was matched, now expand corresponding CUs that were
5430 marked. */
5431 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5432
5433 struct dwarf2_per_cu_data *per_cu;
5434 while ((per_cu = iter.next ()) != NULL)
5435 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5436 file_matcher,
5437 expansion_notify))
5438 return false;
5439 return true;
5440 }, per_objfile);
5441
5442 return result;
5443 }
5444
5445 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5446 to either a dwarf2_per_bfd or dwz_file object. */
5447
5448 template <typename T>
5449 static gdb::array_view<const gdb_byte>
5450 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5451 {
5452 dwarf2_section_info *section = &section_owner->gdb_index;
5453
5454 if (section->empty ())
5455 return {};
5456
5457 /* Older elfutils strip versions could keep the section in the main
5458 executable while splitting it for the separate debug info file. */
5459 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5460 return {};
5461
5462 section->read (obj);
5463
5464 /* dwarf2_section_info::size is a bfd_size_type, while
5465 gdb::array_view works with size_t. On 32-bit hosts, with
5466 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5467 is 32-bit. So we need an explicit narrowing conversion here.
5468 This is fine, because it's impossible to allocate or mmap an
5469 array/buffer larger than what size_t can represent. */
5470 return gdb::make_array_view (section->buffer, section->size);
5471 }
5472
5473 /* Lookup the index cache for the contents of the index associated to
5474 DWARF2_OBJ. */
5475
5476 static gdb::array_view<const gdb_byte>
5477 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5478 {
5479 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5480 if (build_id == nullptr)
5481 return {};
5482
5483 return global_index_cache.lookup_gdb_index (build_id,
5484 &dwarf2_per_bfd->index_cache_res);
5485 }
5486
5487 /* Same as the above, but for DWZ. */
5488
5489 static gdb::array_view<const gdb_byte>
5490 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5491 {
5492 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5493 if (build_id == nullptr)
5494 return {};
5495
5496 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5497 }
5498
5499 /* See dwarf2/public.h. */
5500
5501 void
5502 dwarf2_initialize_objfile (struct objfile *objfile)
5503 {
5504 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5505 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5506
5507 dwarf_read_debug_printf ("called");
5508
5509 /* If we're about to read full symbols, don't bother with the
5510 indices. In this case we also don't care if some other debug
5511 format is making psymtabs, because they are all about to be
5512 expanded anyway. */
5513 if ((objfile->flags & OBJF_READNOW))
5514 {
5515 dwarf_read_debug_printf ("readnow requested");
5516
5517 /* When using READNOW, the using_index flag (set below) indicates that
5518 PER_BFD was already initialized, when we loaded some other objfile. */
5519 if (per_bfd->using_index)
5520 {
5521 dwarf_read_debug_printf ("using_index already set");
5522 objfile->qf.push_front (make_dwarf_gdb_index ());
5523 return;
5524 }
5525
5526 per_bfd->using_index = 1;
5527 create_all_comp_units (per_objfile);
5528 per_bfd->quick_file_names_table
5529 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5530
5531 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5532 {
5533 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5534
5535 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5536 struct dwarf2_per_cu_quick_data);
5537 }
5538
5539 /* Arrange for gdb to see the "quick" functions. However, these
5540 functions will be no-ops because we will have expanded all
5541 symtabs. */
5542 objfile->qf.push_front (make_dwarf_gdb_index ());
5543 return;
5544 }
5545
5546 /* Was a debug names index already read when we processed an objfile sharing
5547 PER_BFD? */
5548 if (per_bfd->debug_names_table != nullptr)
5549 {
5550 dwarf_read_debug_printf ("re-using shared debug names table");
5551 objfile->qf.push_front (make_dwarf_debug_names ());
5552 return;
5553 }
5554
5555 /* Was a GDB index already read when we processed an objfile sharing
5556 PER_BFD? */
5557 if (per_bfd->index_table != nullptr)
5558 {
5559 dwarf_read_debug_printf ("re-using shared index table");
5560 objfile->qf.push_front (make_dwarf_gdb_index ());
5561 return;
5562 }
5563
5564 /* There might already be partial symtabs built for this BFD. This happens
5565 when loading the same binary twice with the index-cache enabled. If so,
5566 don't try to read an index. The objfile / per_objfile initialization will
5567 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5568 code path. */
5569 if (per_bfd->partial_symtabs != nullptr)
5570 {
5571 dwarf_read_debug_printf ("re-using shared partial symtabs");
5572 objfile->qf.push_front (make_lazy_dwarf_reader ());
5573 return;
5574 }
5575
5576 if (dwarf2_read_debug_names (per_objfile))
5577 {
5578 dwarf_read_debug_printf ("found debug names");
5579 objfile->qf.push_front (make_dwarf_debug_names ());
5580 return;
5581 }
5582
5583 if (dwarf2_read_gdb_index (per_objfile,
5584 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5585 get_gdb_index_contents_from_section<dwz_file>))
5586 {
5587 dwarf_read_debug_printf ("found gdb index from file");
5588 objfile->qf.push_front (make_dwarf_gdb_index ());
5589 return;
5590 }
5591
5592 /* ... otherwise, try to find the index in the index cache. */
5593 if (dwarf2_read_gdb_index (per_objfile,
5594 get_gdb_index_contents_from_cache,
5595 get_gdb_index_contents_from_cache_dwz))
5596 {
5597 dwarf_read_debug_printf ("found gdb index from cache");
5598 global_index_cache.hit ();
5599 objfile->qf.push_front (make_dwarf_gdb_index ());
5600 return;
5601 }
5602
5603 global_index_cache.miss ();
5604 objfile->qf.push_front (make_lazy_dwarf_reader ());
5605 }
5606
5607 \f
5608
5609 /* Build a partial symbol table. */
5610
5611 void
5612 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5613 {
5614 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5615 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5616
5617 if (per_bfd->partial_symtabs != nullptr)
5618 {
5619 /* Partial symbols were already read, so now we can simply
5620 attach them. */
5621 if (psf == nullptr)
5622 {
5623 psf = new psymbol_functions (per_bfd->partial_symtabs);
5624 objfile->qf.emplace_front (psf);
5625 }
5626 else
5627 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5628 return;
5629 }
5630
5631 if (psf == nullptr)
5632 {
5633 psf = new psymbol_functions;
5634 objfile->qf.emplace_front (psf);
5635 }
5636 const std::shared_ptr<psymtab_storage> &partial_symtabs
5637 = psf->get_partial_symtabs ();
5638
5639 /* Set the local reference to partial symtabs, so that we don't try
5640 to read them again if reading another objfile with the same BFD.
5641 If we can't in fact share, this won't make a difference anyway as
5642 the dwarf2_per_bfd object won't be shared. */
5643 per_bfd->partial_symtabs = partial_symtabs;
5644
5645 try
5646 {
5647 /* This isn't really ideal: all the data we allocate on the
5648 objfile's obstack is still uselessly kept around. However,
5649 freeing it seems unsafe. */
5650 psymtab_discarder psymtabs (partial_symtabs.get ());
5651 dwarf2_build_psymtabs_hard (per_objfile);
5652 psymtabs.keep ();
5653
5654 /* (maybe) store an index in the cache. */
5655 global_index_cache.store (per_objfile);
5656 }
5657 catch (const gdb_exception_error &except)
5658 {
5659 exception_print (gdb_stderr, except);
5660 }
5661 }
5662
5663 /* Find the base address of the compilation unit for range lists and
5664 location lists. It will normally be specified by DW_AT_low_pc.
5665 In DWARF-3 draft 4, the base address could be overridden by
5666 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5667 compilation units with discontinuous ranges. */
5668
5669 static void
5670 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5671 {
5672 struct attribute *attr;
5673
5674 cu->base_address.reset ();
5675
5676 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5677 if (attr != nullptr)
5678 cu->base_address = attr->as_address ();
5679 else
5680 {
5681 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5682 if (attr != nullptr)
5683 cu->base_address = attr->as_address ();
5684 }
5685 }
5686
5687 /* Helper function that returns the proper abbrev section for
5688 THIS_CU. */
5689
5690 static struct dwarf2_section_info *
5691 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5692 {
5693 struct dwarf2_section_info *abbrev;
5694 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5695
5696 if (this_cu->is_dwz)
5697 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5698 else
5699 abbrev = &per_bfd->abbrev;
5700
5701 return abbrev;
5702 }
5703
5704 /* Fetch the abbreviation table offset from a comp or type unit header. */
5705
5706 static sect_offset
5707 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5708 struct dwarf2_section_info *section,
5709 sect_offset sect_off)
5710 {
5711 bfd *abfd = section->get_bfd_owner ();
5712 const gdb_byte *info_ptr;
5713 unsigned int initial_length_size, offset_size;
5714 uint16_t version;
5715
5716 section->read (per_objfile->objfile);
5717 info_ptr = section->buffer + to_underlying (sect_off);
5718 read_initial_length (abfd, info_ptr, &initial_length_size);
5719 offset_size = initial_length_size == 4 ? 4 : 8;
5720 info_ptr += initial_length_size;
5721
5722 version = read_2_bytes (abfd, info_ptr);
5723 info_ptr += 2;
5724 if (version >= 5)
5725 {
5726 /* Skip unit type and address size. */
5727 info_ptr += 2;
5728 }
5729
5730 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5731 }
5732
5733 /* A partial symtab that is used only for include files. */
5734 struct dwarf2_include_psymtab : public partial_symtab
5735 {
5736 dwarf2_include_psymtab (const char *filename,
5737 psymtab_storage *partial_symtabs,
5738 objfile_per_bfd_storage *objfile_per_bfd)
5739 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5740 {
5741 }
5742
5743 void read_symtab (struct objfile *objfile) override
5744 {
5745 /* It's an include file, no symbols to read for it.
5746 Everything is in the includer symtab. */
5747
5748 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5749 expansion of the includer psymtab. We use the dependencies[0] field to
5750 model the includer. But if we go the regular route of calling
5751 expand_psymtab here, and having expand_psymtab call expand_dependencies
5752 to expand the includer, we'll only use expand_psymtab on the includer
5753 (making it a non-toplevel psymtab), while if we expand the includer via
5754 another path, we'll use read_symtab (making it a toplevel psymtab).
5755 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5756 psymtab, and trigger read_symtab on the includer here directly. */
5757 includer ()->read_symtab (objfile);
5758 }
5759
5760 void expand_psymtab (struct objfile *objfile) override
5761 {
5762 /* This is not called by read_symtab, and should not be called by any
5763 expand_dependencies. */
5764 gdb_assert (false);
5765 }
5766
5767 bool readin_p (struct objfile *objfile) const override
5768 {
5769 return includer ()->readin_p (objfile);
5770 }
5771
5772 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5773 {
5774 compunit_symtab *cust = includer ()->get_compunit_symtab (objfile);
5775 while (cust != nullptr && cust->user != nullptr)
5776 cust = cust->user;
5777 return cust;
5778 }
5779
5780 private:
5781 partial_symtab *includer () const
5782 {
5783 /* An include psymtab has exactly one dependency: the psymtab that
5784 includes it. */
5785 gdb_assert (this->number_of_dependencies == 1);
5786 return this->dependencies[0];
5787 }
5788 };
5789
5790 /* Allocate a new partial symtab for file named NAME and mark this new
5791 partial symtab as being an include of PST. */
5792
5793 static void
5794 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5795 const char *name,
5796 dwarf2_psymtab *pst,
5797 psymtab_storage *partial_symtabs,
5798 objfile_per_bfd_storage *objfile_per_bfd)
5799 {
5800 dwarf2_include_psymtab *subpst
5801 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5802
5803 if (!IS_ABSOLUTE_PATH (subpst->filename))
5804 subpst->dirname = pst->dirname;
5805
5806 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5807 subpst->dependencies[0] = pst;
5808 subpst->number_of_dependencies = 1;
5809 }
5810
5811 /* Read the Line Number Program data and extract the list of files
5812 included by the source file represented by PST. Build an include
5813 partial symtab for each of these included files. */
5814
5815 static void
5816 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5817 struct die_info *die,
5818 const file_and_directory &fnd,
5819 dwarf2_psymtab *pst)
5820 {
5821 line_header_up lh;
5822 struct attribute *attr;
5823
5824 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5825 if (attr != nullptr && attr->form_is_unsigned ())
5826 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5827 if (lh == NULL)
5828 return; /* No linetable, so no includes. */
5829
5830 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5831 that we pass in the raw text_low here; that is ok because we're
5832 only decoding the line table to make include partial symtabs, and
5833 so the addresses aren't really used. */
5834 dwarf_decode_lines (lh.get (), fnd, cu, pst,
5835 pst->raw_text_low (), 1);
5836 }
5837
5838 static hashval_t
5839 hash_signatured_type (const void *item)
5840 {
5841 const struct signatured_type *sig_type
5842 = (const struct signatured_type *) item;
5843
5844 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5845 return sig_type->signature;
5846 }
5847
5848 static int
5849 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5850 {
5851 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5852 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5853
5854 return lhs->signature == rhs->signature;
5855 }
5856
5857 /* Allocate a hash table for signatured types. */
5858
5859 static htab_up
5860 allocate_signatured_type_table ()
5861 {
5862 return htab_up (htab_create_alloc (41,
5863 hash_signatured_type,
5864 eq_signatured_type,
5865 NULL, xcalloc, xfree));
5866 }
5867
5868 /* A helper for create_debug_types_hash_table. Read types from SECTION
5869 and fill them into TYPES_HTAB. It will process only type units,
5870 therefore DW_UT_type. */
5871
5872 static void
5873 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5874 struct dwo_file *dwo_file,
5875 dwarf2_section_info *section, htab_up &types_htab,
5876 rcuh_kind section_kind)
5877 {
5878 struct objfile *objfile = per_objfile->objfile;
5879 struct dwarf2_section_info *abbrev_section;
5880 bfd *abfd;
5881 const gdb_byte *info_ptr, *end_ptr;
5882
5883 abbrev_section = &dwo_file->sections.abbrev;
5884
5885 dwarf_read_debug_printf ("Reading %s for %s",
5886 section->get_name (),
5887 abbrev_section->get_file_name ());
5888
5889 section->read (objfile);
5890 info_ptr = section->buffer;
5891
5892 if (info_ptr == NULL)
5893 return;
5894
5895 /* We can't set abfd until now because the section may be empty or
5896 not present, in which case the bfd is unknown. */
5897 abfd = section->get_bfd_owner ();
5898
5899 /* We don't use cutu_reader here because we don't need to read
5900 any dies: the signature is in the header. */
5901
5902 end_ptr = info_ptr + section->size;
5903 while (info_ptr < end_ptr)
5904 {
5905 signatured_type_up sig_type;
5906 struct dwo_unit *dwo_tu;
5907 void **slot;
5908 const gdb_byte *ptr = info_ptr;
5909 struct comp_unit_head header;
5910 unsigned int length;
5911
5912 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5913
5914 /* Initialize it due to a false compiler warning. */
5915 header.signature = -1;
5916 header.type_cu_offset_in_tu = (cu_offset) -1;
5917
5918 /* We need to read the type's signature in order to build the hash
5919 table, but we don't need anything else just yet. */
5920
5921 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5922 abbrev_section, ptr, section_kind);
5923
5924 length = header.get_length ();
5925
5926 /* Skip dummy type units. */
5927 if (ptr >= info_ptr + length
5928 || peek_abbrev_code (abfd, ptr) == 0
5929 || (header.unit_type != DW_UT_type
5930 && header.unit_type != DW_UT_split_type))
5931 {
5932 info_ptr += length;
5933 continue;
5934 }
5935
5936 if (types_htab == NULL)
5937 types_htab = allocate_dwo_unit_table ();
5938
5939 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5940 dwo_tu->dwo_file = dwo_file;
5941 dwo_tu->signature = header.signature;
5942 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5943 dwo_tu->section = section;
5944 dwo_tu->sect_off = sect_off;
5945 dwo_tu->length = length;
5946
5947 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5948 gdb_assert (slot != NULL);
5949 if (*slot != NULL)
5950 complaint (_("debug type entry at offset %s is duplicate to"
5951 " the entry at offset %s, signature %s"),
5952 sect_offset_str (sect_off),
5953 sect_offset_str (dwo_tu->sect_off),
5954 hex_string (header.signature));
5955 *slot = dwo_tu;
5956
5957 dwarf_read_debug_printf_v (" offset %s, signature %s",
5958 sect_offset_str (sect_off),
5959 hex_string (header.signature));
5960
5961 info_ptr += length;
5962 }
5963 }
5964
5965 /* Create the hash table of all entries in the .debug_types
5966 (or .debug_types.dwo) section(s).
5967 DWO_FILE is a pointer to the DWO file object.
5968
5969 The result is a pointer to the hash table or NULL if there are no types.
5970
5971 Note: This function processes DWO files only, not DWP files. */
5972
5973 static void
5974 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5975 struct dwo_file *dwo_file,
5976 gdb::array_view<dwarf2_section_info> type_sections,
5977 htab_up &types_htab)
5978 {
5979 for (dwarf2_section_info &section : type_sections)
5980 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5981 rcuh_kind::TYPE);
5982 }
5983
5984 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5985 If SLOT is non-NULL, it is the entry to use in the hash table.
5986 Otherwise we find one. */
5987
5988 static struct signatured_type *
5989 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5990 {
5991 if (per_objfile->per_bfd->all_comp_units.size ()
5992 == per_objfile->per_bfd->all_comp_units.capacity ())
5993 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5994
5995 signatured_type_up sig_type_holder
5996 = per_objfile->per_bfd->allocate_signatured_type (sig);
5997 signatured_type *sig_type = sig_type_holder.get ();
5998
5999 per_objfile->per_bfd->all_comp_units.emplace_back
6000 (sig_type_holder.release ());
6001 if (per_objfile->per_bfd->using_index)
6002 {
6003 sig_type->v.quick =
6004 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6005 struct dwarf2_per_cu_quick_data);
6006 }
6007
6008 if (slot == NULL)
6009 {
6010 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6011 sig_type, INSERT);
6012 }
6013 gdb_assert (*slot == NULL);
6014 *slot = sig_type;
6015 /* The rest of sig_type must be filled in by the caller. */
6016 return sig_type;
6017 }
6018
6019 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6020 Fill in SIG_ENTRY with DWO_ENTRY. */
6021
6022 static void
6023 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6024 struct signatured_type *sig_entry,
6025 struct dwo_unit *dwo_entry)
6026 {
6027 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6028
6029 /* Make sure we're not clobbering something we don't expect to. */
6030 gdb_assert (! sig_entry->queued);
6031 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6032 if (per_bfd->using_index)
6033 {
6034 gdb_assert (sig_entry->v.quick != NULL);
6035 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6036 }
6037 else
6038 gdb_assert (sig_entry->v.psymtab == NULL);
6039 gdb_assert (sig_entry->signature == dwo_entry->signature);
6040 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6041 gdb_assert (sig_entry->type_unit_group == NULL);
6042 gdb_assert (sig_entry->dwo_unit == NULL);
6043
6044 sig_entry->section = dwo_entry->section;
6045 sig_entry->sect_off = dwo_entry->sect_off;
6046 sig_entry->length = dwo_entry->length;
6047 sig_entry->reading_dwo_directly = 1;
6048 sig_entry->per_bfd = per_bfd;
6049 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6050 sig_entry->dwo_unit = dwo_entry;
6051 }
6052
6053 /* Subroutine of lookup_signatured_type.
6054 If we haven't read the TU yet, create the signatured_type data structure
6055 for a TU to be read in directly from a DWO file, bypassing the stub.
6056 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6057 using .gdb_index, then when reading a CU we want to stay in the DWO file
6058 containing that CU. Otherwise we could end up reading several other DWO
6059 files (due to comdat folding) to process the transitive closure of all the
6060 mentioned TUs, and that can be slow. The current DWO file will have every
6061 type signature that it needs.
6062 We only do this for .gdb_index because in the psymtab case we already have
6063 to read all the DWOs to build the type unit groups. */
6064
6065 static struct signatured_type *
6066 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6067 {
6068 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6069 struct dwo_file *dwo_file;
6070 struct dwo_unit find_dwo_entry, *dwo_entry;
6071 void **slot;
6072
6073 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6074
6075 /* If TU skeletons have been removed then we may not have read in any
6076 TUs yet. */
6077 if (per_objfile->per_bfd->signatured_types == NULL)
6078 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6079
6080 /* We only ever need to read in one copy of a signatured type.
6081 Use the global signatured_types array to do our own comdat-folding
6082 of types. If this is the first time we're reading this TU, and
6083 the TU has an entry in .gdb_index, replace the recorded data from
6084 .gdb_index with this TU. */
6085
6086 signatured_type find_sig_entry (sig);
6087 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6088 &find_sig_entry, INSERT);
6089 signatured_type *sig_entry = (struct signatured_type *) *slot;
6090
6091 /* We can get here with the TU already read, *or* in the process of being
6092 read. Don't reassign the global entry to point to this DWO if that's
6093 the case. Also note that if the TU is already being read, it may not
6094 have come from a DWO, the program may be a mix of Fission-compiled
6095 code and non-Fission-compiled code. */
6096
6097 /* Have we already tried to read this TU?
6098 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6099 needn't exist in the global table yet). */
6100 if (sig_entry != NULL && sig_entry->tu_read)
6101 return sig_entry;
6102
6103 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6104 dwo_unit of the TU itself. */
6105 dwo_file = cu->dwo_unit->dwo_file;
6106
6107 /* Ok, this is the first time we're reading this TU. */
6108 if (dwo_file->tus == NULL)
6109 return NULL;
6110 find_dwo_entry.signature = sig;
6111 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6112 &find_dwo_entry);
6113 if (dwo_entry == NULL)
6114 return NULL;
6115
6116 /* If the global table doesn't have an entry for this TU, add one. */
6117 if (sig_entry == NULL)
6118 sig_entry = add_type_unit (per_objfile, sig, slot);
6119
6120 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6121 sig_entry->tu_read = 1;
6122 return sig_entry;
6123 }
6124
6125 /* Subroutine of lookup_signatured_type.
6126 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6127 then try the DWP file. If the TU stub (skeleton) has been removed then
6128 it won't be in .gdb_index. */
6129
6130 static struct signatured_type *
6131 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6132 {
6133 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6134 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6135 struct dwo_unit *dwo_entry;
6136 void **slot;
6137
6138 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6139 gdb_assert (dwp_file != NULL);
6140
6141 /* If TU skeletons have been removed then we may not have read in any
6142 TUs yet. */
6143 if (per_objfile->per_bfd->signatured_types == NULL)
6144 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6145
6146 signatured_type find_sig_entry (sig);
6147 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6148 &find_sig_entry, INSERT);
6149 signatured_type *sig_entry = (struct signatured_type *) *slot;
6150
6151 /* Have we already tried to read this TU?
6152 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6153 needn't exist in the global table yet). */
6154 if (sig_entry != NULL)
6155 return sig_entry;
6156
6157 if (dwp_file->tus == NULL)
6158 return NULL;
6159 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6160 1 /* is_debug_types */);
6161 if (dwo_entry == NULL)
6162 return NULL;
6163
6164 sig_entry = add_type_unit (per_objfile, sig, slot);
6165 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6166
6167 return sig_entry;
6168 }
6169
6170 /* Lookup a signature based type for DW_FORM_ref_sig8.
6171 Returns NULL if signature SIG is not present in the table.
6172 It is up to the caller to complain about this. */
6173
6174 static struct signatured_type *
6175 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6176 {
6177 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6178
6179 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6180 {
6181 /* We're in a DWO/DWP file, and we're using .gdb_index.
6182 These cases require special processing. */
6183 if (get_dwp_file (per_objfile) == NULL)
6184 return lookup_dwo_signatured_type (cu, sig);
6185 else
6186 return lookup_dwp_signatured_type (cu, sig);
6187 }
6188 else
6189 {
6190 if (per_objfile->per_bfd->signatured_types == NULL)
6191 return NULL;
6192 signatured_type find_entry (sig);
6193 return ((struct signatured_type *)
6194 htab_find (per_objfile->per_bfd->signatured_types.get (),
6195 &find_entry));
6196 }
6197 }
6198
6199 /* Low level DIE reading support. */
6200
6201 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6202
6203 static void
6204 init_cu_die_reader (struct die_reader_specs *reader,
6205 struct dwarf2_cu *cu,
6206 struct dwarf2_section_info *section,
6207 struct dwo_file *dwo_file,
6208 struct abbrev_table *abbrev_table)
6209 {
6210 gdb_assert (section->readin && section->buffer != NULL);
6211 reader->abfd = section->get_bfd_owner ();
6212 reader->cu = cu;
6213 reader->dwo_file = dwo_file;
6214 reader->die_section = section;
6215 reader->buffer = section->buffer;
6216 reader->buffer_end = section->buffer + section->size;
6217 reader->abbrev_table = abbrev_table;
6218 }
6219
6220 /* Subroutine of cutu_reader to simplify it.
6221 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6222 There's just a lot of work to do, and cutu_reader is big enough
6223 already.
6224
6225 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6226 from it to the DIE in the DWO. If NULL we are skipping the stub.
6227 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6228 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6229 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6230 STUB_COMP_DIR may be non-NULL.
6231 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6232 are filled in with the info of the DIE from the DWO file.
6233 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6234 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6235 kept around for at least as long as *RESULT_READER.
6236
6237 The result is non-zero if a valid (non-dummy) DIE was found. */
6238
6239 static int
6240 read_cutu_die_from_dwo (dwarf2_cu *cu,
6241 struct dwo_unit *dwo_unit,
6242 struct die_info *stub_comp_unit_die,
6243 const char *stub_comp_dir,
6244 struct die_reader_specs *result_reader,
6245 const gdb_byte **result_info_ptr,
6246 struct die_info **result_comp_unit_die,
6247 abbrev_table_up *result_dwo_abbrev_table)
6248 {
6249 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6250 dwarf2_per_cu_data *per_cu = cu->per_cu;
6251 struct objfile *objfile = per_objfile->objfile;
6252 bfd *abfd;
6253 const gdb_byte *begin_info_ptr, *info_ptr;
6254 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6255 int i,num_extra_attrs;
6256 struct dwarf2_section_info *dwo_abbrev_section;
6257 struct die_info *comp_unit_die;
6258
6259 /* At most one of these may be provided. */
6260 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6261
6262 /* These attributes aren't processed until later:
6263 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6264 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6265 referenced later. However, these attributes are found in the stub
6266 which we won't have later. In order to not impose this complication
6267 on the rest of the code, we read them here and copy them to the
6268 DWO CU/TU die. */
6269
6270 stmt_list = NULL;
6271 low_pc = NULL;
6272 high_pc = NULL;
6273 ranges = NULL;
6274 comp_dir = NULL;
6275
6276 if (stub_comp_unit_die != NULL)
6277 {
6278 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6279 DWO file. */
6280 if (!per_cu->is_debug_types)
6281 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6282 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6283 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6284 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6285 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6286
6287 cu->addr_base = stub_comp_unit_die->addr_base ();
6288
6289 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6290 We need the value before we can process DW_AT_ranges values from the
6291 DWO. */
6292 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6293
6294 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6295 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6296 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6297 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6298 section. */
6299 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6300 }
6301 else if (stub_comp_dir != NULL)
6302 {
6303 /* Reconstruct the comp_dir attribute to simplify the code below. */
6304 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6305 comp_dir->name = DW_AT_comp_dir;
6306 comp_dir->form = DW_FORM_string;
6307 comp_dir->set_string_noncanonical (stub_comp_dir);
6308 }
6309
6310 /* Set up for reading the DWO CU/TU. */
6311 cu->dwo_unit = dwo_unit;
6312 dwarf2_section_info *section = dwo_unit->section;
6313 section->read (objfile);
6314 abfd = section->get_bfd_owner ();
6315 begin_info_ptr = info_ptr = (section->buffer
6316 + to_underlying (dwo_unit->sect_off));
6317 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6318
6319 if (per_cu->is_debug_types)
6320 {
6321 signatured_type *sig_type = (struct signatured_type *) per_cu;
6322
6323 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6324 section, dwo_abbrev_section,
6325 info_ptr, rcuh_kind::TYPE);
6326 /* This is not an assert because it can be caused by bad debug info. */
6327 if (sig_type->signature != cu->header.signature)
6328 {
6329 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6330 " TU at offset %s [in module %s]"),
6331 hex_string (sig_type->signature),
6332 hex_string (cu->header.signature),
6333 sect_offset_str (dwo_unit->sect_off),
6334 bfd_get_filename (abfd));
6335 }
6336 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6337 /* For DWOs coming from DWP files, we don't know the CU length
6338 nor the type's offset in the TU until now. */
6339 dwo_unit->length = cu->header.get_length ();
6340 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6341
6342 /* Establish the type offset that can be used to lookup the type.
6343 For DWO files, we don't know it until now. */
6344 sig_type->type_offset_in_section
6345 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6346 }
6347 else
6348 {
6349 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6350 section, dwo_abbrev_section,
6351 info_ptr, rcuh_kind::COMPILE);
6352 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6353 /* For DWOs coming from DWP files, we don't know the CU length
6354 until now. */
6355 dwo_unit->length = cu->header.get_length ();
6356 }
6357
6358 dwo_abbrev_section->read (objfile);
6359 *result_dwo_abbrev_table
6360 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6361 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6362 result_dwo_abbrev_table->get ());
6363
6364 /* Read in the die, but leave space to copy over the attributes
6365 from the stub. This has the benefit of simplifying the rest of
6366 the code - all the work to maintain the illusion of a single
6367 DW_TAG_{compile,type}_unit DIE is done here. */
6368 num_extra_attrs = ((stmt_list != NULL)
6369 + (low_pc != NULL)
6370 + (high_pc != NULL)
6371 + (ranges != NULL)
6372 + (comp_dir != NULL));
6373 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6374 num_extra_attrs);
6375
6376 /* Copy over the attributes from the stub to the DIE we just read in. */
6377 comp_unit_die = *result_comp_unit_die;
6378 i = comp_unit_die->num_attrs;
6379 if (stmt_list != NULL)
6380 comp_unit_die->attrs[i++] = *stmt_list;
6381 if (low_pc != NULL)
6382 comp_unit_die->attrs[i++] = *low_pc;
6383 if (high_pc != NULL)
6384 comp_unit_die->attrs[i++] = *high_pc;
6385 if (ranges != NULL)
6386 comp_unit_die->attrs[i++] = *ranges;
6387 if (comp_dir != NULL)
6388 comp_unit_die->attrs[i++] = *comp_dir;
6389 comp_unit_die->num_attrs += num_extra_attrs;
6390
6391 if (dwarf_die_debug)
6392 {
6393 fprintf_unfiltered (gdb_stdlog,
6394 "Read die from %s@0x%x of %s:\n",
6395 section->get_name (),
6396 (unsigned) (begin_info_ptr - section->buffer),
6397 bfd_get_filename (abfd));
6398 dump_die (comp_unit_die, dwarf_die_debug);
6399 }
6400
6401 /* Skip dummy compilation units. */
6402 if (info_ptr >= begin_info_ptr + dwo_unit->length
6403 || peek_abbrev_code (abfd, info_ptr) == 0)
6404 return 0;
6405
6406 *result_info_ptr = info_ptr;
6407 return 1;
6408 }
6409
6410 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6411 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6412 signature is part of the header. */
6413 static gdb::optional<ULONGEST>
6414 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6415 {
6416 if (cu->header.version >= 5)
6417 return cu->header.signature;
6418 struct attribute *attr;
6419 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6420 if (attr == nullptr || !attr->form_is_unsigned ())
6421 return gdb::optional<ULONGEST> ();
6422 return attr->as_unsigned ();
6423 }
6424
6425 /* Subroutine of cutu_reader to simplify it.
6426 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6427 Returns NULL if the specified DWO unit cannot be found. */
6428
6429 static struct dwo_unit *
6430 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6431 {
6432 dwarf2_per_cu_data *per_cu = cu->per_cu;
6433 struct dwo_unit *dwo_unit;
6434 const char *comp_dir;
6435
6436 gdb_assert (cu != NULL);
6437
6438 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6439 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6440 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6441
6442 if (per_cu->is_debug_types)
6443 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6444 else
6445 {
6446 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6447
6448 if (!signature.has_value ())
6449 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6450 " [in module %s]"),
6451 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6452
6453 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6454 }
6455
6456 return dwo_unit;
6457 }
6458
6459 /* Subroutine of cutu_reader to simplify it.
6460 See it for a description of the parameters.
6461 Read a TU directly from a DWO file, bypassing the stub. */
6462
6463 void
6464 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6465 dwarf2_per_objfile *per_objfile,
6466 dwarf2_cu *existing_cu)
6467 {
6468 struct signatured_type *sig_type;
6469
6470 /* Verify we can do the following downcast, and that we have the
6471 data we need. */
6472 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6473 sig_type = (struct signatured_type *) this_cu;
6474 gdb_assert (sig_type->dwo_unit != NULL);
6475
6476 dwarf2_cu *cu;
6477
6478 if (existing_cu != nullptr)
6479 {
6480 cu = existing_cu;
6481 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6482 /* There's no need to do the rereading_dwo_cu handling that
6483 cutu_reader does since we don't read the stub. */
6484 }
6485 else
6486 {
6487 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6488 in per_objfile yet. */
6489 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6490 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6491 cu = m_new_cu.get ();
6492 }
6493
6494 /* A future optimization, if needed, would be to use an existing
6495 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6496 could share abbrev tables. */
6497
6498 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6499 NULL /* stub_comp_unit_die */,
6500 sig_type->dwo_unit->dwo_file->comp_dir,
6501 this, &info_ptr,
6502 &comp_unit_die,
6503 &m_dwo_abbrev_table) == 0)
6504 {
6505 /* Dummy die. */
6506 dummy_p = true;
6507 }
6508 }
6509
6510 /* Initialize a CU (or TU) and read its DIEs.
6511 If the CU defers to a DWO file, read the DWO file as well.
6512
6513 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6514 Otherwise the table specified in the comp unit header is read in and used.
6515 This is an optimization for when we already have the abbrev table.
6516
6517 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6518 allocated. */
6519
6520 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6521 dwarf2_per_objfile *per_objfile,
6522 struct abbrev_table *abbrev_table,
6523 dwarf2_cu *existing_cu,
6524 bool skip_partial)
6525 : die_reader_specs {},
6526 m_this_cu (this_cu)
6527 {
6528 struct objfile *objfile = per_objfile->objfile;
6529 struct dwarf2_section_info *section = this_cu->section;
6530 bfd *abfd = section->get_bfd_owner ();
6531 const gdb_byte *begin_info_ptr;
6532 struct signatured_type *sig_type = NULL;
6533 struct dwarf2_section_info *abbrev_section;
6534 /* Non-zero if CU currently points to a DWO file and we need to
6535 reread it. When this happens we need to reread the skeleton die
6536 before we can reread the DWO file (this only applies to CUs, not TUs). */
6537 int rereading_dwo_cu = 0;
6538
6539 if (dwarf_die_debug)
6540 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6541 this_cu->is_debug_types ? "type" : "comp",
6542 sect_offset_str (this_cu->sect_off));
6543
6544 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6545 file (instead of going through the stub), short-circuit all of this. */
6546 if (this_cu->reading_dwo_directly)
6547 {
6548 /* Narrow down the scope of possibilities to have to understand. */
6549 gdb_assert (this_cu->is_debug_types);
6550 gdb_assert (abbrev_table == NULL);
6551 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6552 return;
6553 }
6554
6555 /* This is cheap if the section is already read in. */
6556 section->read (objfile);
6557
6558 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6559
6560 abbrev_section = get_abbrev_section_for_cu (this_cu);
6561
6562 dwarf2_cu *cu;
6563
6564 if (existing_cu != nullptr)
6565 {
6566 cu = existing_cu;
6567 /* If this CU is from a DWO file we need to start over, we need to
6568 refetch the attributes from the skeleton CU.
6569 This could be optimized by retrieving those attributes from when we
6570 were here the first time: the previous comp_unit_die was stored in
6571 comp_unit_obstack. But there's no data yet that we need this
6572 optimization. */
6573 if (cu->dwo_unit != NULL)
6574 rereading_dwo_cu = 1;
6575 }
6576 else
6577 {
6578 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6579 in per_objfile yet. */
6580 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6581 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6582 cu = m_new_cu.get ();
6583 }
6584
6585 /* Get the header. */
6586 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6587 {
6588 /* We already have the header, there's no need to read it in again. */
6589 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6590 }
6591 else
6592 {
6593 if (this_cu->is_debug_types)
6594 {
6595 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6596 section, abbrev_section,
6597 info_ptr, rcuh_kind::TYPE);
6598
6599 /* Since per_cu is the first member of struct signatured_type,
6600 we can go from a pointer to one to a pointer to the other. */
6601 sig_type = (struct signatured_type *) this_cu;
6602 gdb_assert (sig_type->signature == cu->header.signature);
6603 gdb_assert (sig_type->type_offset_in_tu
6604 == cu->header.type_cu_offset_in_tu);
6605 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6606
6607 /* LENGTH has not been set yet for type units if we're
6608 using .gdb_index. */
6609 this_cu->length = cu->header.get_length ();
6610
6611 /* Establish the type offset that can be used to lookup the type. */
6612 sig_type->type_offset_in_section =
6613 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6614
6615 this_cu->dwarf_version = cu->header.version;
6616 }
6617 else
6618 {
6619 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6620 section, abbrev_section,
6621 info_ptr,
6622 rcuh_kind::COMPILE);
6623
6624 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6625 if (this_cu->length == 0)
6626 this_cu->length = cu->header.get_length ();
6627 else
6628 gdb_assert (this_cu->length == cu->header.get_length ());
6629 this_cu->dwarf_version = cu->header.version;
6630 }
6631 }
6632
6633 /* Skip dummy compilation units. */
6634 if (info_ptr >= begin_info_ptr + this_cu->length
6635 || peek_abbrev_code (abfd, info_ptr) == 0)
6636 {
6637 dummy_p = true;
6638 return;
6639 }
6640
6641 /* If we don't have them yet, read the abbrevs for this compilation unit.
6642 And if we need to read them now, make sure they're freed when we're
6643 done. */
6644 if (abbrev_table != NULL)
6645 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6646 else
6647 {
6648 abbrev_section->read (objfile);
6649 m_abbrev_table_holder
6650 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6651 abbrev_table = m_abbrev_table_holder.get ();
6652 }
6653
6654 /* Read the top level CU/TU die. */
6655 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6656 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6657
6658 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6659 {
6660 dummy_p = true;
6661 return;
6662 }
6663
6664 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6665 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6666 table from the DWO file and pass the ownership over to us. It will be
6667 referenced from READER, so we must make sure to free it after we're done
6668 with READER.
6669
6670 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6671 DWO CU, that this test will fail (the attribute will not be present). */
6672 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6673 if (dwo_name != nullptr)
6674 {
6675 struct dwo_unit *dwo_unit;
6676 struct die_info *dwo_comp_unit_die;
6677
6678 if (comp_unit_die->has_children)
6679 {
6680 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6681 " has children (offset %s) [in module %s]"),
6682 sect_offset_str (this_cu->sect_off),
6683 bfd_get_filename (abfd));
6684 }
6685 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6686 if (dwo_unit != NULL)
6687 {
6688 if (read_cutu_die_from_dwo (cu, dwo_unit,
6689 comp_unit_die, NULL,
6690 this, &info_ptr,
6691 &dwo_comp_unit_die,
6692 &m_dwo_abbrev_table) == 0)
6693 {
6694 /* Dummy die. */
6695 dummy_p = true;
6696 return;
6697 }
6698 comp_unit_die = dwo_comp_unit_die;
6699 }
6700 else
6701 {
6702 /* Yikes, we couldn't find the rest of the DIE, we only have
6703 the stub. A complaint has already been logged. There's
6704 not much more we can do except pass on the stub DIE to
6705 die_reader_func. We don't want to throw an error on bad
6706 debug info. */
6707 }
6708 }
6709 }
6710
6711 void
6712 cutu_reader::keep ()
6713 {
6714 /* Done, clean up. */
6715 gdb_assert (!dummy_p);
6716 if (m_new_cu != NULL)
6717 {
6718 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6719 now. */
6720 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6721 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6722 }
6723 }
6724
6725 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6726 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6727 assumed to have already done the lookup to find the DWO file).
6728
6729 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6730 THIS_CU->is_debug_types, but nothing else.
6731
6732 We fill in THIS_CU->length.
6733
6734 THIS_CU->cu is always freed when done.
6735 This is done in order to not leave THIS_CU->cu in a state where we have
6736 to care whether it refers to the "main" CU or the DWO CU.
6737
6738 When parent_cu is passed, it is used to provide a default value for
6739 str_offsets_base and addr_base from the parent. */
6740
6741 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6742 dwarf2_per_objfile *per_objfile,
6743 struct dwarf2_cu *parent_cu,
6744 struct dwo_file *dwo_file)
6745 : die_reader_specs {},
6746 m_this_cu (this_cu)
6747 {
6748 struct objfile *objfile = per_objfile->objfile;
6749 struct dwarf2_section_info *section = this_cu->section;
6750 bfd *abfd = section->get_bfd_owner ();
6751 struct dwarf2_section_info *abbrev_section;
6752 const gdb_byte *begin_info_ptr, *info_ptr;
6753
6754 if (dwarf_die_debug)
6755 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6756 this_cu->is_debug_types ? "type" : "comp",
6757 sect_offset_str (this_cu->sect_off));
6758
6759 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6760
6761 abbrev_section = (dwo_file != NULL
6762 ? &dwo_file->sections.abbrev
6763 : get_abbrev_section_for_cu (this_cu));
6764
6765 /* This is cheap if the section is already read in. */
6766 section->read (objfile);
6767
6768 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6769
6770 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6771 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6772 section, abbrev_section, info_ptr,
6773 (this_cu->is_debug_types
6774 ? rcuh_kind::TYPE
6775 : rcuh_kind::COMPILE));
6776
6777 if (parent_cu != nullptr)
6778 {
6779 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6780 m_new_cu->addr_base = parent_cu->addr_base;
6781 }
6782 this_cu->length = m_new_cu->header.get_length ();
6783
6784 /* Skip dummy compilation units. */
6785 if (info_ptr >= begin_info_ptr + this_cu->length
6786 || peek_abbrev_code (abfd, info_ptr) == 0)
6787 {
6788 dummy_p = true;
6789 return;
6790 }
6791
6792 abbrev_section->read (objfile);
6793 m_abbrev_table_holder
6794 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6795
6796 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6797 m_abbrev_table_holder.get ());
6798 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6799 }
6800
6801 \f
6802 /* Type Unit Groups.
6803
6804 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6805 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6806 so that all types coming from the same compilation (.o file) are grouped
6807 together. A future step could be to put the types in the same symtab as
6808 the CU the types ultimately came from. */
6809
6810 static hashval_t
6811 hash_type_unit_group (const void *item)
6812 {
6813 const struct type_unit_group *tu_group
6814 = (const struct type_unit_group *) item;
6815
6816 return hash_stmt_list_entry (&tu_group->hash);
6817 }
6818
6819 static int
6820 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6821 {
6822 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6823 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6824
6825 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6826 }
6827
6828 /* Allocate a hash table for type unit groups. */
6829
6830 static htab_up
6831 allocate_type_unit_groups_table ()
6832 {
6833 return htab_up (htab_create_alloc (3,
6834 hash_type_unit_group,
6835 eq_type_unit_group,
6836 htab_delete_entry<type_unit_group>,
6837 xcalloc, xfree));
6838 }
6839
6840 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6841 partial symtabs. We combine several TUs per psymtab to not let the size
6842 of any one psymtab grow too big. */
6843 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6844 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6845
6846 /* Helper routine for get_type_unit_group.
6847 Create the type_unit_group object used to hold one or more TUs. */
6848
6849 static std::unique_ptr<type_unit_group>
6850 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6851 {
6852 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6853 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6854
6855 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6856 tu_group->per_bfd = per_bfd;
6857
6858 if (per_bfd->using_index)
6859 {
6860 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6861 struct dwarf2_per_cu_quick_data);
6862 }
6863 else
6864 {
6865 unsigned int line_offset = to_underlying (line_offset_struct);
6866 dwarf2_psymtab *pst;
6867 std::string name;
6868
6869 /* Give the symtab a useful name for debug purposes. */
6870 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6871 name = string_printf ("<type_units_%d>",
6872 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6873 else
6874 name = string_printf ("<type_units_at_0x%x>", line_offset);
6875
6876 pst = create_partial_symtab (tu_group.get (), per_objfile,
6877 name.c_str ());
6878 pst->anonymous = true;
6879 }
6880
6881 tu_group->hash.dwo_unit = cu->dwo_unit;
6882 tu_group->hash.line_sect_off = line_offset_struct;
6883
6884 return tu_group;
6885 }
6886
6887 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6888 STMT_LIST is a DW_AT_stmt_list attribute. */
6889
6890 static struct type_unit_group *
6891 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6892 {
6893 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6894 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6895 struct type_unit_group *tu_group;
6896 void **slot;
6897 unsigned int line_offset;
6898 struct type_unit_group type_unit_group_for_lookup;
6899
6900 if (per_objfile->per_bfd->type_unit_groups == NULL)
6901 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6902
6903 /* Do we need to create a new group, or can we use an existing one? */
6904
6905 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6906 {
6907 line_offset = stmt_list->as_unsigned ();
6908 ++tu_stats->nr_symtab_sharers;
6909 }
6910 else
6911 {
6912 /* Ugh, no stmt_list. Rare, but we have to handle it.
6913 We can do various things here like create one group per TU or
6914 spread them over multiple groups to split up the expansion work.
6915 To avoid worst case scenarios (too many groups or too large groups)
6916 we, umm, group them in bunches. */
6917 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6918 | (tu_stats->nr_stmt_less_type_units
6919 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6920 ++tu_stats->nr_stmt_less_type_units;
6921 }
6922
6923 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6924 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6925 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6926 &type_unit_group_for_lookup, INSERT);
6927 if (*slot == nullptr)
6928 {
6929 sect_offset line_offset_struct = (sect_offset) line_offset;
6930 std::unique_ptr<type_unit_group> grp
6931 = create_type_unit_group (cu, line_offset_struct);
6932 *slot = grp.release ();
6933 ++tu_stats->nr_symtabs;
6934 }
6935
6936 tu_group = (struct type_unit_group *) *slot;
6937 gdb_assert (tu_group != nullptr);
6938 return tu_group;
6939 }
6940 \f
6941 /* Partial symbol tables. */
6942
6943 /* Create a psymtab named NAME and assign it to PER_CU.
6944
6945 The caller must fill in the following details:
6946 dirname, textlow, texthigh. */
6947
6948 static dwarf2_psymtab *
6949 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6950 dwarf2_per_objfile *per_objfile,
6951 const char *name)
6952 {
6953 dwarf2_psymtab *pst
6954 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6955 per_objfile->objfile->per_bfd, per_cu);
6956
6957 pst->psymtabs_addrmap_supported = true;
6958
6959 /* This is the glue that links PST into GDB's symbol API. */
6960 per_cu->v.psymtab = pst;
6961
6962 return pst;
6963 }
6964
6965 /* DIE reader function for process_psymtab_comp_unit. */
6966
6967 static void
6968 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6969 const gdb_byte *info_ptr,
6970 struct die_info *comp_unit_die,
6971 enum language pretend_language)
6972 {
6973 struct dwarf2_cu *cu = reader->cu;
6974 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6975 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6976 struct objfile *objfile = per_objfile->objfile;
6977 struct gdbarch *gdbarch = objfile->arch ();
6978 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6979 CORE_ADDR baseaddr;
6980 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6981 dwarf2_psymtab *pst;
6982 enum pc_bounds_kind cu_bounds_kind;
6983
6984 gdb_assert (! per_cu->is_debug_types);
6985
6986 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
6987
6988 /* Allocate a new partial symbol table structure. */
6989 static const char artificial[] = "<artificial>";
6990 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
6991 if (strcmp (fnd.get_name (), artificial) == 0)
6992 {
6993 gdb::unique_xmalloc_ptr<char> debug_filename
6994 (concat (artificial, "@",
6995 sect_offset_str (per_cu->sect_off),
6996 (char *) NULL));
6997 fnd.set_name (std::move (debug_filename));
6998 }
6999
7000 pst = create_partial_symtab (per_cu, per_objfile, fnd.get_name ());
7001
7002 /* This must be done before calling dwarf2_build_include_psymtabs. */
7003 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7004
7005 baseaddr = objfile->text_section_offset ();
7006
7007 dwarf2_find_base_address (comp_unit_die, cu);
7008
7009 /* Possibly set the default values of LOWPC and HIGHPC from
7010 `DW_AT_ranges'. */
7011 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7012 &best_highpc, cu, pst);
7013 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7014 {
7015 CORE_ADDR low
7016 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7017 - baseaddr);
7018 CORE_ADDR high
7019 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7020 - baseaddr - 1);
7021 /* Store the contiguous range if it is not empty; it can be
7022 empty for CUs with no code. */
7023 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7024 low, high, pst);
7025 }
7026
7027 /* Check if comp unit has_children.
7028 If so, read the rest of the partial symbols from this comp unit.
7029 If not, there's no more debug_info for this comp unit. */
7030 if (comp_unit_die->has_children)
7031 {
7032 struct partial_die_info *first_die;
7033 CORE_ADDR lowpc, highpc;
7034
7035 lowpc = ((CORE_ADDR) -1);
7036 highpc = ((CORE_ADDR) 0);
7037
7038 first_die = load_partial_dies (reader, info_ptr, 1);
7039
7040 scan_partial_symbols (first_die, &lowpc, &highpc,
7041 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7042
7043 /* If we didn't find a lowpc, set it to highpc to avoid
7044 complaints from `maint check'. */
7045 if (lowpc == ((CORE_ADDR) -1))
7046 lowpc = highpc;
7047
7048 /* If the compilation unit didn't have an explicit address range,
7049 then use the information extracted from its child dies. */
7050 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7051 {
7052 best_lowpc = lowpc;
7053 best_highpc = highpc;
7054 }
7055 }
7056 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7057 best_lowpc + baseaddr)
7058 - baseaddr);
7059 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7060 best_highpc + baseaddr)
7061 - baseaddr);
7062
7063 pst->end ();
7064
7065 if (!cu->per_cu->imported_symtabs_empty ())
7066 {
7067 int i;
7068 int len = cu->per_cu->imported_symtabs_size ();
7069
7070 /* Fill in 'dependencies' here; we fill in 'users' in a
7071 post-pass. */
7072 pst->number_of_dependencies = len;
7073 pst->dependencies
7074 = per_bfd->partial_symtabs->allocate_dependencies (len);
7075 for (i = 0; i < len; ++i)
7076 {
7077 pst->dependencies[i]
7078 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7079 }
7080
7081 cu->per_cu->imported_symtabs_free ();
7082 }
7083
7084 /* Get the list of files included in the current compilation unit,
7085 and build a psymtab for each of them. */
7086 dwarf2_build_include_psymtabs (cu, comp_unit_die, fnd, pst);
7087
7088 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7089 ", %d global, %d static syms",
7090 per_cu->is_debug_types ? "type" : "comp",
7091 sect_offset_str (per_cu->sect_off),
7092 paddress (gdbarch, pst->text_low (objfile)),
7093 paddress (gdbarch, pst->text_high (objfile)),
7094 (int) pst->global_psymbols.size (),
7095 (int) pst->static_psymbols.size ());
7096 }
7097
7098 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7099 Process compilation unit THIS_CU for a psymtab. */
7100
7101 static void
7102 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7103 dwarf2_per_objfile *per_objfile,
7104 bool want_partial_unit,
7105 enum language pretend_language)
7106 {
7107 /* If this compilation unit was already read in, free the
7108 cached copy in order to read it in again. This is
7109 necessary because we skipped some symbols when we first
7110 read in the compilation unit (see load_partial_dies).
7111 This problem could be avoided, but the benefit is unclear. */
7112 per_objfile->remove_cu (this_cu);
7113
7114 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7115
7116 if (reader.comp_unit_die == nullptr)
7117 return;
7118
7119 switch (reader.comp_unit_die->tag)
7120 {
7121 case DW_TAG_compile_unit:
7122 this_cu->unit_type = DW_UT_compile;
7123 break;
7124 case DW_TAG_partial_unit:
7125 this_cu->unit_type = DW_UT_partial;
7126 break;
7127 case DW_TAG_type_unit:
7128 this_cu->unit_type = DW_UT_type;
7129 break;
7130 default:
7131 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7132 dwarf_tag_name (reader.comp_unit_die->tag),
7133 sect_offset_str (reader.cu->per_cu->sect_off),
7134 objfile_name (per_objfile->objfile));
7135 }
7136
7137 if (reader.dummy_p)
7138 {
7139 /* Nothing. */
7140 }
7141 else if (this_cu->is_debug_types)
7142 build_type_psymtabs_reader (&reader, reader.info_ptr,
7143 reader.comp_unit_die);
7144 else if (want_partial_unit
7145 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7146 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7147 reader.comp_unit_die,
7148 pretend_language);
7149
7150 /* Age out any secondary CUs. */
7151 per_objfile->age_comp_units ();
7152 }
7153
7154 /* Reader function for build_type_psymtabs. */
7155
7156 static void
7157 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7158 const gdb_byte *info_ptr,
7159 struct die_info *type_unit_die)
7160 {
7161 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7162 struct dwarf2_cu *cu = reader->cu;
7163 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7164 struct signatured_type *sig_type;
7165 struct type_unit_group *tu_group;
7166 struct attribute *attr;
7167 struct partial_die_info *first_die;
7168 CORE_ADDR lowpc, highpc;
7169 dwarf2_psymtab *pst;
7170
7171 gdb_assert (per_cu->is_debug_types);
7172 sig_type = (struct signatured_type *) per_cu;
7173
7174 if (! type_unit_die->has_children)
7175 return;
7176
7177 attr = type_unit_die->attr (DW_AT_stmt_list);
7178 tu_group = get_type_unit_group (cu, attr);
7179
7180 if (tu_group->tus == nullptr)
7181 tu_group->tus = new std::vector<signatured_type *>;
7182 tu_group->tus->push_back (sig_type);
7183
7184 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7185 pst = create_partial_symtab (per_cu, per_objfile, "");
7186 pst->anonymous = true;
7187
7188 first_die = load_partial_dies (reader, info_ptr, 1);
7189
7190 lowpc = (CORE_ADDR) -1;
7191 highpc = (CORE_ADDR) 0;
7192 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7193
7194 pst->end ();
7195 }
7196
7197 /* Struct used to sort TUs by their abbreviation table offset. */
7198
7199 struct tu_abbrev_offset
7200 {
7201 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7202 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7203 {}
7204
7205 /* This is used when sorting. */
7206 bool operator< (const tu_abbrev_offset &other) const
7207 {
7208 return abbrev_offset < other.abbrev_offset;
7209 }
7210
7211 signatured_type *sig_type;
7212 sect_offset abbrev_offset;
7213 };
7214
7215 /* Efficiently read all the type units.
7216
7217 The efficiency is because we sort TUs by the abbrev table they use and
7218 only read each abbrev table once. In one program there are 200K TUs
7219 sharing 8K abbrev tables.
7220
7221 The main purpose of this function is to support building the
7222 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7223 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7224 can collapse the search space by grouping them by stmt_list.
7225 The savings can be significant, in the same program from above the 200K TUs
7226 share 8K stmt_list tables.
7227
7228 FUNC is expected to call get_type_unit_group, which will create the
7229 struct type_unit_group if necessary and add it to
7230 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7231
7232 static void
7233 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7234 {
7235 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7236 abbrev_table_up abbrev_table;
7237 sect_offset abbrev_offset;
7238
7239 /* It's up to the caller to not call us multiple times. */
7240 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7241
7242 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7243 return;
7244
7245 /* TUs typically share abbrev tables, and there can be way more TUs than
7246 abbrev tables. Sort by abbrev table to reduce the number of times we
7247 read each abbrev table in.
7248 Alternatives are to punt or to maintain a cache of abbrev tables.
7249 This is simpler and efficient enough for now.
7250
7251 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7252 symtab to use). Typically TUs with the same abbrev offset have the same
7253 stmt_list value too so in practice this should work well.
7254
7255 The basic algorithm here is:
7256
7257 sort TUs by abbrev table
7258 for each TU with same abbrev table:
7259 read abbrev table if first user
7260 read TU top level DIE
7261 [IWBN if DWO skeletons had DW_AT_stmt_list]
7262 call FUNC */
7263
7264 dwarf_read_debug_printf ("Building type unit groups ...");
7265
7266 /* Sort in a separate table to maintain the order of all_comp_units
7267 for .gdb_index: TU indices directly index all_type_units. */
7268 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7269 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7270
7271 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7272 {
7273 if (cu->is_debug_types)
7274 {
7275 auto sig_type = static_cast<signatured_type *> (cu.get ());
7276 sorted_by_abbrev.emplace_back
7277 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7278 sig_type->sect_off));
7279 }
7280 }
7281
7282 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7283
7284 abbrev_offset = (sect_offset) ~(unsigned) 0;
7285
7286 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7287 {
7288 /* Switch to the next abbrev table if necessary. */
7289 if (abbrev_table == NULL
7290 || tu.abbrev_offset != abbrev_offset)
7291 {
7292 abbrev_offset = tu.abbrev_offset;
7293 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7294 abbrev_table =
7295 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7296 ++tu_stats->nr_uniq_abbrev_tables;
7297 }
7298
7299 cutu_reader reader (tu.sig_type, per_objfile,
7300 abbrev_table.get (), nullptr, false);
7301 if (!reader.dummy_p)
7302 build_type_psymtabs_reader (&reader, reader.info_ptr,
7303 reader.comp_unit_die);
7304 }
7305 }
7306
7307 /* Print collected type unit statistics. */
7308
7309 static void
7310 print_tu_stats (dwarf2_per_objfile *per_objfile)
7311 {
7312 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7313
7314 dwarf_read_debug_printf ("Type unit statistics:");
7315 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7316 dwarf_read_debug_printf (" %d uniq abbrev tables",
7317 tu_stats->nr_uniq_abbrev_tables);
7318 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7319 tu_stats->nr_symtabs);
7320 dwarf_read_debug_printf (" %d symtab sharers",
7321 tu_stats->nr_symtab_sharers);
7322 dwarf_read_debug_printf (" %d type units without a stmt_list",
7323 tu_stats->nr_stmt_less_type_units);
7324 dwarf_read_debug_printf (" %d all_type_units reallocs",
7325 tu_stats->nr_all_type_units_reallocs);
7326 }
7327
7328 /* Traversal function for build_type_psymtabs. */
7329
7330 static int
7331 build_type_psymtab_dependencies (void **slot, void *info)
7332 {
7333 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7334 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7335 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7336 dwarf2_psymtab *pst = tu_group->v.psymtab;
7337 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7338 int i;
7339
7340 gdb_assert (len > 0);
7341 gdb_assert (tu_group->type_unit_group_p ());
7342
7343 pst->number_of_dependencies = len;
7344 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7345 for (i = 0; i < len; ++i)
7346 {
7347 struct signatured_type *iter = tu_group->tus->at (i);
7348 gdb_assert (iter->is_debug_types);
7349 pst->dependencies[i] = iter->v.psymtab;
7350 iter->type_unit_group = tu_group;
7351 }
7352
7353 delete tu_group->tus;
7354 tu_group->tus = nullptr;
7355
7356 return 1;
7357 }
7358
7359 /* Traversal function for process_skeletonless_type_unit.
7360 Read a TU in a DWO file and build partial symbols for it. */
7361
7362 static int
7363 process_skeletonless_type_unit (void **slot, void *info)
7364 {
7365 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7366 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7367
7368 /* If this TU doesn't exist in the global table, add it and read it in. */
7369
7370 if (per_objfile->per_bfd->signatured_types == NULL)
7371 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7372
7373 signatured_type find_entry (dwo_unit->signature);
7374 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7375 &find_entry, INSERT);
7376 /* If we've already seen this type there's nothing to do. What's happening
7377 is we're doing our own version of comdat-folding here. */
7378 if (*slot != NULL)
7379 return 1;
7380
7381 /* This does the job that create_all_comp_units would have done for
7382 this TU. */
7383 signatured_type *entry
7384 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7385 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7386 *slot = entry;
7387
7388 /* This does the job that build_type_psymtabs would have done. */
7389 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7390 if (!reader.dummy_p)
7391 build_type_psymtabs_reader (&reader, reader.info_ptr,
7392 reader.comp_unit_die);
7393
7394 return 1;
7395 }
7396
7397 /* Traversal function for process_skeletonless_type_units. */
7398
7399 static int
7400 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7401 {
7402 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7403
7404 if (dwo_file->tus != NULL)
7405 htab_traverse_noresize (dwo_file->tus.get (),
7406 process_skeletonless_type_unit, info);
7407
7408 return 1;
7409 }
7410
7411 /* Scan all TUs of DWO files, verifying we've processed them.
7412 This is needed in case a TU was emitted without its skeleton.
7413 Note: This can't be done until we know what all the DWO files are. */
7414
7415 static void
7416 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7417 {
7418 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7419 if (get_dwp_file (per_objfile) == NULL
7420 && per_objfile->per_bfd->dwo_files != NULL)
7421 {
7422 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7423 process_dwo_file_for_skeletonless_type_units,
7424 per_objfile);
7425 }
7426 }
7427
7428 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7429
7430 static void
7431 set_partial_user (dwarf2_per_objfile *per_objfile)
7432 {
7433 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7434 {
7435 dwarf2_psymtab *pst = per_cu->v.psymtab;
7436
7437 if (pst == NULL)
7438 continue;
7439
7440 for (int j = 0; j < pst->number_of_dependencies; ++j)
7441 {
7442 /* Set the 'user' field only if it is not already set. */
7443 if (pst->dependencies[j]->user == NULL)
7444 pst->dependencies[j]->user = pst;
7445 }
7446 }
7447 }
7448
7449 /* Build the partial symbol table by doing a quick pass through the
7450 .debug_info and .debug_abbrev sections. */
7451
7452 static void
7453 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7454 {
7455 struct objfile *objfile = per_objfile->objfile;
7456 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7457
7458 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7459 objfile_name (objfile));
7460
7461 scoped_restore restore_reading_psyms
7462 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7463
7464 per_bfd->info.read (objfile);
7465
7466 /* Any cached compilation units will be linked by the per-objfile
7467 read_in_chain. Make sure to free them when we're done. */
7468 free_cached_comp_units freer (per_objfile);
7469
7470 create_all_comp_units (per_objfile);
7471 build_type_psymtabs (per_objfile);
7472
7473 /* Create a temporary address map on a temporary obstack. We later
7474 copy this to the final obstack. */
7475 auto_obstack temp_obstack;
7476
7477 scoped_restore save_psymtabs_addrmap
7478 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7479 addrmap_create_mutable (&temp_obstack));
7480
7481 for (const auto &per_cu : per_bfd->all_comp_units)
7482 {
7483 if (per_cu->v.psymtab != NULL)
7484 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7485 continue;
7486 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7487 language_minimal);
7488 }
7489
7490 /* This has to wait until we read the CUs, we need the list of DWOs. */
7491 process_skeletonless_type_units (per_objfile);
7492
7493 /* Now that all TUs have been processed we can fill in the dependencies. */
7494 if (per_bfd->type_unit_groups != NULL)
7495 {
7496 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7497 build_type_psymtab_dependencies, per_objfile);
7498 }
7499
7500 if (dwarf_read_debug > 0)
7501 print_tu_stats (per_objfile);
7502
7503 set_partial_user (per_objfile);
7504
7505 per_bfd->partial_symtabs->psymtabs_addrmap
7506 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7507 per_bfd->partial_symtabs->obstack ());
7508 /* At this point we want to keep the address map. */
7509 save_psymtabs_addrmap.release ();
7510
7511 dwarf_read_debug_printf ("Done building psymtabs of %s",
7512 objfile_name (objfile));
7513 }
7514
7515 /* Load the partial DIEs for a secondary CU into memory.
7516 This is also used when rereading a primary CU with load_all_dies. */
7517
7518 static void
7519 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7520 dwarf2_per_objfile *per_objfile,
7521 dwarf2_cu *existing_cu)
7522 {
7523 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7524
7525 if (!reader.dummy_p)
7526 {
7527 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7528 language_minimal);
7529
7530 /* Check if comp unit has_children.
7531 If so, read the rest of the partial symbols from this comp unit.
7532 If not, there's no more debug_info for this comp unit. */
7533 if (reader.comp_unit_die->has_children)
7534 load_partial_dies (&reader, reader.info_ptr, 0);
7535
7536 reader.keep ();
7537 }
7538 }
7539
7540 static void
7541 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7542 struct dwarf2_section_info *section,
7543 struct dwarf2_section_info *abbrev_section,
7544 unsigned int is_dwz,
7545 htab_up &types_htab,
7546 rcuh_kind section_kind)
7547 {
7548 const gdb_byte *info_ptr;
7549 struct objfile *objfile = per_objfile->objfile;
7550
7551 dwarf_read_debug_printf ("Reading %s for %s",
7552 section->get_name (),
7553 section->get_file_name ());
7554
7555 section->read (objfile);
7556
7557 info_ptr = section->buffer;
7558
7559 while (info_ptr < section->buffer + section->size)
7560 {
7561 dwarf2_per_cu_data_up this_cu;
7562
7563 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7564
7565 comp_unit_head cu_header;
7566 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7567 abbrev_section, info_ptr,
7568 section_kind);
7569
7570 /* Save the compilation unit for later lookup. */
7571 if (cu_header.unit_type != DW_UT_type)
7572 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7573 else
7574 {
7575 if (types_htab == nullptr)
7576 types_htab = allocate_signatured_type_table ();
7577
7578 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7579 (cu_header.signature);
7580 signatured_type *sig_ptr = sig_type.get ();
7581 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7582 this_cu.reset (sig_type.release ());
7583
7584 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7585 gdb_assert (slot != nullptr);
7586 if (*slot != nullptr)
7587 complaint (_("debug type entry at offset %s is duplicate to"
7588 " the entry at offset %s, signature %s"),
7589 sect_offset_str (sect_off),
7590 sect_offset_str (sig_ptr->sect_off),
7591 hex_string (sig_ptr->signature));
7592 *slot = sig_ptr;
7593 }
7594 this_cu->sect_off = sect_off;
7595 this_cu->length = cu_header.length + cu_header.initial_length_size;
7596 this_cu->is_dwz = is_dwz;
7597 this_cu->section = section;
7598
7599 info_ptr = info_ptr + this_cu->length;
7600 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7601 }
7602 }
7603
7604 /* Create a list of all compilation units in OBJFILE.
7605 This is only done for -readnow and building partial symtabs. */
7606
7607 static void
7608 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7609 {
7610 htab_up types_htab;
7611
7612 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7613 &per_objfile->per_bfd->abbrev, 0,
7614 types_htab, rcuh_kind::COMPILE);
7615 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7616 read_comp_units_from_section (per_objfile, &section,
7617 &per_objfile->per_bfd->abbrev, 0,
7618 types_htab, rcuh_kind::TYPE);
7619
7620 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7621 if (dwz != NULL)
7622 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7623 types_htab, rcuh_kind::COMPILE);
7624
7625 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7626 }
7627
7628 /* Process all loaded DIEs for compilation unit CU, starting at
7629 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7630 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7631 DW_AT_ranges). See the comments of add_partial_subprogram on how
7632 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7633
7634 static void
7635 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7636 CORE_ADDR *highpc, int set_addrmap,
7637 struct dwarf2_cu *cu)
7638 {
7639 struct partial_die_info *pdi;
7640
7641 /* Now, march along the PDI's, descending into ones which have
7642 interesting children but skipping the children of the other ones,
7643 until we reach the end of the compilation unit. */
7644
7645 pdi = first_die;
7646
7647 while (pdi != NULL)
7648 {
7649 pdi->fixup (cu);
7650
7651 /* Anonymous namespaces or modules have no name but have interesting
7652 children, so we need to look at them. Ditto for anonymous
7653 enums. */
7654
7655 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7656 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7657 || pdi->tag == DW_TAG_imported_unit
7658 || pdi->tag == DW_TAG_inlined_subroutine)
7659 {
7660 switch (pdi->tag)
7661 {
7662 case DW_TAG_subprogram:
7663 case DW_TAG_inlined_subroutine:
7664 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7665 if (cu->per_cu->lang == language_cplus)
7666 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7667 set_addrmap, cu);
7668 break;
7669 case DW_TAG_constant:
7670 case DW_TAG_variable:
7671 case DW_TAG_typedef:
7672 case DW_TAG_union_type:
7673 if (!pdi->is_declaration
7674 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7675 {
7676 add_partial_symbol (pdi, cu);
7677 }
7678 break;
7679 case DW_TAG_class_type:
7680 case DW_TAG_interface_type:
7681 case DW_TAG_structure_type:
7682 if (!pdi->is_declaration)
7683 {
7684 add_partial_symbol (pdi, cu);
7685 }
7686 if ((cu->per_cu->lang == language_rust
7687 || cu->per_cu->lang == language_cplus)
7688 && pdi->has_children)
7689 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7690 set_addrmap, cu);
7691 break;
7692 case DW_TAG_enumeration_type:
7693 if (!pdi->is_declaration)
7694 add_partial_enumeration (pdi, cu);
7695 break;
7696 case DW_TAG_base_type:
7697 case DW_TAG_subrange_type:
7698 /* File scope base type definitions are added to the partial
7699 symbol table. */
7700 add_partial_symbol (pdi, cu);
7701 break;
7702 case DW_TAG_namespace:
7703 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7704 break;
7705 case DW_TAG_module:
7706 if (!pdi->is_declaration)
7707 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7708 break;
7709 case DW_TAG_imported_unit:
7710 {
7711 struct dwarf2_per_cu_data *per_cu;
7712
7713 /* For now we don't handle imported units in type units. */
7714 if (cu->per_cu->is_debug_types)
7715 {
7716 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7717 " supported in type units [in module %s]"),
7718 objfile_name (cu->per_objfile->objfile));
7719 }
7720
7721 per_cu = dwarf2_find_containing_comp_unit
7722 (pdi->d.sect_off, pdi->is_dwz,
7723 cu->per_objfile->per_bfd);
7724
7725 /* Go read the partial unit, if needed. */
7726 if (per_cu->v.psymtab == NULL)
7727 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7728 cu->per_cu->lang);
7729
7730 if (pdi->die_parent == nullptr
7731 && per_cu->unit_type == DW_UT_compile
7732 && per_cu->lang == language_cplus)
7733 /* Regard import as hint. See corresponding code in
7734 process_imported_unit_die. */
7735 break;
7736
7737 cu->per_cu->imported_symtabs_push (per_cu);
7738 }
7739 break;
7740 case DW_TAG_imported_declaration:
7741 add_partial_symbol (pdi, cu);
7742 break;
7743 default:
7744 break;
7745 }
7746 }
7747
7748 /* If the die has a sibling, skip to the sibling. */
7749
7750 pdi = pdi->die_sibling;
7751 }
7752 }
7753
7754 /* Functions used to compute the fully scoped name of a partial DIE.
7755
7756 Normally, this is simple. For C++, the parent DIE's fully scoped
7757 name is concatenated with "::" and the partial DIE's name.
7758 Enumerators are an exception; they use the scope of their parent
7759 enumeration type, i.e. the name of the enumeration type is not
7760 prepended to the enumerator.
7761
7762 There are two complexities. One is DW_AT_specification; in this
7763 case "parent" means the parent of the target of the specification,
7764 instead of the direct parent of the DIE. The other is compilers
7765 which do not emit DW_TAG_namespace; in this case we try to guess
7766 the fully qualified name of structure types from their members'
7767 linkage names. This must be done using the DIE's children rather
7768 than the children of any DW_AT_specification target. We only need
7769 to do this for structures at the top level, i.e. if the target of
7770 any DW_AT_specification (if any; otherwise the DIE itself) does not
7771 have a parent. */
7772
7773 /* Compute the scope prefix associated with PDI's parent, in
7774 compilation unit CU. The result will be allocated on CU's
7775 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7776 field. NULL is returned if no prefix is necessary. */
7777 static const char *
7778 partial_die_parent_scope (struct partial_die_info *pdi,
7779 struct dwarf2_cu *cu)
7780 {
7781 const char *grandparent_scope;
7782 struct partial_die_info *parent, *real_pdi;
7783
7784 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7785 then this means the parent of the specification DIE. */
7786
7787 real_pdi = pdi;
7788 while (real_pdi->has_specification)
7789 {
7790 auto res = find_partial_die (real_pdi->spec_offset,
7791 real_pdi->spec_is_dwz, cu);
7792 real_pdi = res.pdi;
7793 cu = res.cu;
7794 }
7795
7796 parent = real_pdi->die_parent;
7797 if (parent == NULL)
7798 return NULL;
7799
7800 if (parent->scope_set)
7801 return parent->scope;
7802
7803 parent->fixup (cu);
7804
7805 grandparent_scope = partial_die_parent_scope (parent, cu);
7806
7807 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7808 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7809 Work around this problem here. */
7810 if (cu->per_cu->lang == language_cplus
7811 && parent->tag == DW_TAG_namespace
7812 && strcmp (parent->name (cu), "::") == 0
7813 && grandparent_scope == NULL)
7814 {
7815 parent->scope = NULL;
7816 parent->scope_set = 1;
7817 return NULL;
7818 }
7819
7820 /* Nested subroutines in Fortran get a prefix. */
7821 if (pdi->tag == DW_TAG_enumerator)
7822 /* Enumerators should not get the name of the enumeration as a prefix. */
7823 parent->scope = grandparent_scope;
7824 else if (parent->tag == DW_TAG_namespace
7825 || parent->tag == DW_TAG_module
7826 || parent->tag == DW_TAG_structure_type
7827 || parent->tag == DW_TAG_class_type
7828 || parent->tag == DW_TAG_interface_type
7829 || parent->tag == DW_TAG_union_type
7830 || parent->tag == DW_TAG_enumeration_type
7831 || (cu->per_cu->lang == language_fortran
7832 && parent->tag == DW_TAG_subprogram
7833 && pdi->tag == DW_TAG_subprogram))
7834 {
7835 if (grandparent_scope == NULL)
7836 parent->scope = parent->name (cu);
7837 else
7838 parent->scope = typename_concat (&cu->comp_unit_obstack,
7839 grandparent_scope,
7840 parent->name (cu), 0, cu);
7841 }
7842 else
7843 {
7844 /* FIXME drow/2004-04-01: What should we be doing with
7845 function-local names? For partial symbols, we should probably be
7846 ignoring them. */
7847 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7848 dwarf_tag_name (parent->tag),
7849 sect_offset_str (pdi->sect_off));
7850 parent->scope = grandparent_scope;
7851 }
7852
7853 parent->scope_set = 1;
7854 return parent->scope;
7855 }
7856
7857 /* Return the fully scoped name associated with PDI, from compilation unit
7858 CU. The result will be allocated with malloc. */
7859
7860 static gdb::unique_xmalloc_ptr<char>
7861 partial_die_full_name (struct partial_die_info *pdi,
7862 struct dwarf2_cu *cu)
7863 {
7864 const char *parent_scope;
7865
7866 /* If this is a template instantiation, we can not work out the
7867 template arguments from partial DIEs. So, unfortunately, we have
7868 to go through the full DIEs. At least any work we do building
7869 types here will be reused if full symbols are loaded later. */
7870 if (pdi->has_template_arguments)
7871 {
7872 pdi->fixup (cu);
7873
7874 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7875 {
7876 struct die_info *die;
7877 struct attribute attr;
7878 struct dwarf2_cu *ref_cu = cu;
7879
7880 /* DW_FORM_ref_addr is using section offset. */
7881 attr.name = (enum dwarf_attribute) 0;
7882 attr.form = DW_FORM_ref_addr;
7883 attr.u.unsnd = to_underlying (pdi->sect_off);
7884 die = follow_die_ref (NULL, &attr, &ref_cu);
7885
7886 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7887 }
7888 }
7889
7890 parent_scope = partial_die_parent_scope (pdi, cu);
7891 if (parent_scope == NULL)
7892 return NULL;
7893 else
7894 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7895 pdi->name (cu),
7896 0, cu));
7897 }
7898
7899 static void
7900 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7901 {
7902 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7903 struct objfile *objfile = per_objfile->objfile;
7904 struct gdbarch *gdbarch = objfile->arch ();
7905 CORE_ADDR addr = 0;
7906 const char *actual_name = NULL;
7907 CORE_ADDR baseaddr;
7908
7909 baseaddr = objfile->text_section_offset ();
7910
7911 gdb::unique_xmalloc_ptr<char> built_actual_name
7912 = partial_die_full_name (pdi, cu);
7913 if (built_actual_name != NULL)
7914 actual_name = built_actual_name.get ();
7915
7916 if (actual_name == NULL)
7917 actual_name = pdi->name (cu);
7918
7919 partial_symbol psymbol;
7920 memset (&psymbol, 0, sizeof (psymbol));
7921 psymbol.ginfo.set_language (cu->per_cu->lang,
7922 &objfile->objfile_obstack);
7923 psymbol.ginfo.set_section_index (-1);
7924
7925 /* The code below indicates that the psymbol should be installed by
7926 setting this. */
7927 gdb::optional<psymbol_placement> where;
7928
7929 switch (pdi->tag)
7930 {
7931 case DW_TAG_inlined_subroutine:
7932 case DW_TAG_subprogram:
7933 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7934 - baseaddr);
7935 if (pdi->is_external
7936 || cu->per_cu->lang == language_ada
7937 || (cu->per_cu->lang == language_fortran
7938 && pdi->die_parent != NULL
7939 && pdi->die_parent->tag == DW_TAG_subprogram))
7940 {
7941 /* Normally, only "external" DIEs are part of the global scope.
7942 But in Ada and Fortran, we want to be able to access nested
7943 procedures globally. So all Ada and Fortran subprograms are
7944 stored in the global scope. */
7945 where = psymbol_placement::GLOBAL;
7946 }
7947 else
7948 where = psymbol_placement::STATIC;
7949
7950 psymbol.domain = VAR_DOMAIN;
7951 psymbol.aclass = LOC_BLOCK;
7952 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7953 psymbol.ginfo.value.address = addr;
7954
7955 if (pdi->main_subprogram && actual_name != NULL)
7956 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
7957 break;
7958 case DW_TAG_constant:
7959 psymbol.domain = VAR_DOMAIN;
7960 psymbol.aclass = LOC_STATIC;
7961 where = (pdi->is_external
7962 ? psymbol_placement::GLOBAL
7963 : psymbol_placement::STATIC);
7964 break;
7965 case DW_TAG_variable:
7966 if (pdi->d.locdesc)
7967 addr = decode_locdesc (pdi->d.locdesc, cu);
7968
7969 if (pdi->d.locdesc
7970 && addr == 0
7971 && !per_objfile->per_bfd->has_section_at_zero)
7972 {
7973 /* A global or static variable may also have been stripped
7974 out by the linker if unused, in which case its address
7975 will be nullified; do not add such variables into partial
7976 symbol table then. */
7977 }
7978 else if (pdi->is_external)
7979 {
7980 /* Global Variable.
7981 Don't enter into the minimal symbol tables as there is
7982 a minimal symbol table entry from the ELF symbols already.
7983 Enter into partial symbol table if it has a location
7984 descriptor or a type.
7985 If the location descriptor is missing, new_symbol will create
7986 a LOC_UNRESOLVED symbol, the address of the variable will then
7987 be determined from the minimal symbol table whenever the variable
7988 is referenced.
7989 The address for the partial symbol table entry is not
7990 used by GDB, but it comes in handy for debugging partial symbol
7991 table building. */
7992
7993 if (pdi->d.locdesc || pdi->has_type)
7994 {
7995 psymbol.domain = VAR_DOMAIN;
7996 psymbol.aclass = LOC_STATIC;
7997 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7998 psymbol.ginfo.value.address = addr;
7999 where = psymbol_placement::GLOBAL;
8000 }
8001 }
8002 else
8003 {
8004 int has_loc = pdi->d.locdesc != NULL;
8005
8006 /* Static Variable. Skip symbols whose value we cannot know (those
8007 without location descriptors or constant values). */
8008 if (!has_loc && !pdi->has_const_value)
8009 return;
8010
8011 psymbol.domain = VAR_DOMAIN;
8012 psymbol.aclass = LOC_STATIC;
8013 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8014 if (has_loc)
8015 psymbol.ginfo.value.address = addr;
8016 where = psymbol_placement::STATIC;
8017 }
8018 break;
8019 case DW_TAG_array_type:
8020 case DW_TAG_typedef:
8021 case DW_TAG_base_type:
8022 case DW_TAG_subrange_type:
8023 psymbol.domain = VAR_DOMAIN;
8024 psymbol.aclass = LOC_TYPEDEF;
8025 where = psymbol_placement::STATIC;
8026 break;
8027 case DW_TAG_imported_declaration:
8028 case DW_TAG_namespace:
8029 psymbol.domain = VAR_DOMAIN;
8030 psymbol.aclass = LOC_TYPEDEF;
8031 where = psymbol_placement::GLOBAL;
8032 break;
8033 case DW_TAG_module:
8034 /* With Fortran 77 there might be a "BLOCK DATA" module
8035 available without any name. If so, we skip the module as it
8036 doesn't bring any value. */
8037 if (actual_name != nullptr)
8038 {
8039 psymbol.domain = MODULE_DOMAIN;
8040 psymbol.aclass = LOC_TYPEDEF;
8041 where = psymbol_placement::GLOBAL;
8042 }
8043 break;
8044 case DW_TAG_class_type:
8045 case DW_TAG_interface_type:
8046 case DW_TAG_structure_type:
8047 case DW_TAG_union_type:
8048 case DW_TAG_enumeration_type:
8049 /* Skip external references. The DWARF standard says in the section
8050 about "Structure, Union, and Class Type Entries": "An incomplete
8051 structure, union or class type is represented by a structure,
8052 union or class entry that does not have a byte size attribute
8053 and that has a DW_AT_declaration attribute." */
8054 if (!pdi->has_byte_size && pdi->is_declaration)
8055 return;
8056
8057 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8058 static vs. global. */
8059 psymbol.domain = STRUCT_DOMAIN;
8060 psymbol.aclass = LOC_TYPEDEF;
8061 where = (cu->per_cu->lang == language_cplus
8062 ? psymbol_placement::GLOBAL
8063 : psymbol_placement::STATIC);
8064 break;
8065 case DW_TAG_enumerator:
8066 psymbol.domain = VAR_DOMAIN;
8067 psymbol.aclass = LOC_CONST;
8068 where = (cu->per_cu->lang == language_cplus
8069 ? psymbol_placement::GLOBAL
8070 : psymbol_placement::STATIC);
8071 break;
8072 default:
8073 break;
8074 }
8075
8076 if (where.has_value ())
8077 {
8078 if (built_actual_name != nullptr)
8079 actual_name = objfile->intern (actual_name);
8080 if (pdi->linkage_name == nullptr
8081 || cu->per_cu->lang == language_ada)
8082 psymbol.ginfo.set_linkage_name (actual_name);
8083 else
8084 {
8085 psymbol.ginfo.set_demangled_name (actual_name,
8086 &objfile->objfile_obstack);
8087 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8088 }
8089 cu->per_cu->v.psymtab->add_psymbol
8090 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8091 objfile);
8092 }
8093 }
8094
8095 /* Read a partial die corresponding to a namespace; also, add a symbol
8096 corresponding to that namespace to the symbol table. NAMESPACE is
8097 the name of the enclosing namespace. */
8098
8099 static void
8100 add_partial_namespace (struct partial_die_info *pdi,
8101 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8102 int set_addrmap, struct dwarf2_cu *cu)
8103 {
8104 /* Add a symbol for the namespace. */
8105
8106 add_partial_symbol (pdi, cu);
8107
8108 /* Now scan partial symbols in that namespace. */
8109
8110 if (pdi->has_children)
8111 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8112 }
8113
8114 /* Read a partial die corresponding to a Fortran module. */
8115
8116 static void
8117 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8118 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8119 {
8120 /* Add a symbol for the namespace. */
8121
8122 add_partial_symbol (pdi, cu);
8123
8124 /* Now scan partial symbols in that module. */
8125
8126 if (pdi->has_children)
8127 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8128 }
8129
8130 static int
8131 dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
8132 dwarf2_psymtab *, dwarf_tag);
8133
8134 /* Read a partial die corresponding to a subprogram or an inlined
8135 subprogram and create a partial symbol for that subprogram.
8136 When the CU language allows it, this routine also defines a partial
8137 symbol for each nested subprogram that this subprogram contains.
8138 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8139 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8140
8141 PDI may also be a lexical block, in which case we simply search
8142 recursively for subprograms defined inside that lexical block.
8143 Again, this is only performed when the CU language allows this
8144 type of definitions. */
8145
8146 static void
8147 add_partial_subprogram (struct partial_die_info *pdi,
8148 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8149 int set_addrmap, struct dwarf2_cu *cu)
8150 {
8151 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8152 {
8153 if (pdi->has_pc_info)
8154 {
8155 if (pdi->lowpc < *lowpc)
8156 *lowpc = pdi->lowpc;
8157 if (pdi->highpc > *highpc)
8158 *highpc = pdi->highpc;
8159 if (set_addrmap)
8160 {
8161 struct objfile *objfile = cu->per_objfile->objfile;
8162 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8163 struct gdbarch *gdbarch = objfile->arch ();
8164 CORE_ADDR baseaddr;
8165 CORE_ADDR this_highpc;
8166 CORE_ADDR this_lowpc;
8167
8168 baseaddr = objfile->text_section_offset ();
8169 this_lowpc
8170 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8171 pdi->lowpc + baseaddr)
8172 - baseaddr);
8173 this_highpc
8174 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8175 pdi->highpc + baseaddr)
8176 - baseaddr);
8177 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8178 this_lowpc, this_highpc - 1,
8179 cu->per_cu->v.psymtab);
8180 }
8181 }
8182
8183 if (pdi->has_range_info
8184 && dwarf2_ranges_read (pdi->ranges_offset, &pdi->lowpc, &pdi->highpc,
8185 cu,
8186 set_addrmap ? cu->per_cu->v.psymtab : nullptr,
8187 pdi->tag))
8188 {
8189 if (pdi->lowpc < *lowpc)
8190 *lowpc = pdi->lowpc;
8191 if (pdi->highpc > *highpc)
8192 *highpc = pdi->highpc;
8193 }
8194
8195 if (pdi->has_pc_info || pdi->has_range_info
8196 || (!pdi->is_external && pdi->may_be_inlined))
8197 {
8198 if (!pdi->is_declaration)
8199 /* Ignore subprogram DIEs that do not have a name, they are
8200 illegal. Do not emit a complaint at this point, we will
8201 do so when we convert this psymtab into a symtab. */
8202 if (pdi->name (cu))
8203 add_partial_symbol (pdi, cu);
8204 }
8205 }
8206
8207 if (! pdi->has_children)
8208 return;
8209
8210 if (cu->per_cu->lang == language_ada
8211 || cu->per_cu->lang == language_fortran)
8212 {
8213 pdi = pdi->die_child;
8214 while (pdi != NULL)
8215 {
8216 pdi->fixup (cu);
8217 if (pdi->tag == DW_TAG_subprogram
8218 || pdi->tag == DW_TAG_inlined_subroutine
8219 || pdi->tag == DW_TAG_lexical_block)
8220 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8221 pdi = pdi->die_sibling;
8222 }
8223 }
8224 }
8225
8226 /* Read a partial die corresponding to an enumeration type. */
8227
8228 static void
8229 add_partial_enumeration (struct partial_die_info *enum_pdi,
8230 struct dwarf2_cu *cu)
8231 {
8232 struct partial_die_info *pdi;
8233
8234 if (enum_pdi->name (cu) != NULL)
8235 add_partial_symbol (enum_pdi, cu);
8236
8237 pdi = enum_pdi->die_child;
8238 while (pdi)
8239 {
8240 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8241 complaint (_("malformed enumerator DIE ignored"));
8242 else
8243 add_partial_symbol (pdi, cu);
8244 pdi = pdi->die_sibling;
8245 }
8246 }
8247
8248 /* Return the initial uleb128 in the die at INFO_PTR. */
8249
8250 static unsigned int
8251 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8252 {
8253 unsigned int bytes_read;
8254
8255 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8256 }
8257
8258 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8259 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8260
8261 Return the corresponding abbrev, or NULL if the number is zero (indicating
8262 an empty DIE). In either case *BYTES_READ will be set to the length of
8263 the initial number. */
8264
8265 static const struct abbrev_info *
8266 peek_die_abbrev (const die_reader_specs &reader,
8267 const gdb_byte *info_ptr, unsigned int *bytes_read)
8268 {
8269 dwarf2_cu *cu = reader.cu;
8270 bfd *abfd = reader.abfd;
8271 unsigned int abbrev_number
8272 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8273
8274 if (abbrev_number == 0)
8275 return NULL;
8276
8277 const abbrev_info *abbrev
8278 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8279 if (!abbrev)
8280 {
8281 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8282 " at offset %s [in module %s]"),
8283 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8284 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8285 }
8286
8287 return abbrev;
8288 }
8289
8290 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8291 Returns a pointer to the end of a series of DIEs, terminated by an empty
8292 DIE. Any children of the skipped DIEs will also be skipped. */
8293
8294 static const gdb_byte *
8295 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8296 {
8297 while (1)
8298 {
8299 unsigned int bytes_read;
8300 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8301 &bytes_read);
8302
8303 if (abbrev == NULL)
8304 return info_ptr + bytes_read;
8305 else
8306 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8307 }
8308 }
8309
8310 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8311 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8312 abbrev corresponding to that skipped uleb128 should be passed in
8313 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8314 children. */
8315
8316 static const gdb_byte *
8317 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8318 const struct abbrev_info *abbrev)
8319 {
8320 unsigned int bytes_read;
8321 struct attribute attr;
8322 bfd *abfd = reader->abfd;
8323 struct dwarf2_cu *cu = reader->cu;
8324 const gdb_byte *buffer = reader->buffer;
8325 const gdb_byte *buffer_end = reader->buffer_end;
8326 unsigned int form, i;
8327
8328 for (i = 0; i < abbrev->num_attrs; i++)
8329 {
8330 /* The only abbrev we care about is DW_AT_sibling. */
8331 if (abbrev->attrs[i].name == DW_AT_sibling)
8332 {
8333 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8334 if (attr.form == DW_FORM_ref_addr)
8335 complaint (_("ignoring absolute DW_AT_sibling"));
8336 else
8337 {
8338 sect_offset off = attr.get_ref_die_offset ();
8339 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8340
8341 if (sibling_ptr < info_ptr)
8342 complaint (_("DW_AT_sibling points backwards"));
8343 else if (sibling_ptr > reader->buffer_end)
8344 reader->die_section->overflow_complaint ();
8345 else
8346 return sibling_ptr;
8347 }
8348 }
8349
8350 /* If it isn't DW_AT_sibling, skip this attribute. */
8351 form = abbrev->attrs[i].form;
8352 skip_attribute:
8353 switch (form)
8354 {
8355 case DW_FORM_ref_addr:
8356 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8357 and later it is offset sized. */
8358 if (cu->header.version == 2)
8359 info_ptr += cu->header.addr_size;
8360 else
8361 info_ptr += cu->header.offset_size;
8362 break;
8363 case DW_FORM_GNU_ref_alt:
8364 info_ptr += cu->header.offset_size;
8365 break;
8366 case DW_FORM_addr:
8367 info_ptr += cu->header.addr_size;
8368 break;
8369 case DW_FORM_data1:
8370 case DW_FORM_ref1:
8371 case DW_FORM_flag:
8372 case DW_FORM_strx1:
8373 info_ptr += 1;
8374 break;
8375 case DW_FORM_flag_present:
8376 case DW_FORM_implicit_const:
8377 break;
8378 case DW_FORM_data2:
8379 case DW_FORM_ref2:
8380 case DW_FORM_strx2:
8381 info_ptr += 2;
8382 break;
8383 case DW_FORM_strx3:
8384 info_ptr += 3;
8385 break;
8386 case DW_FORM_data4:
8387 case DW_FORM_ref4:
8388 case DW_FORM_strx4:
8389 info_ptr += 4;
8390 break;
8391 case DW_FORM_data8:
8392 case DW_FORM_ref8:
8393 case DW_FORM_ref_sig8:
8394 info_ptr += 8;
8395 break;
8396 case DW_FORM_data16:
8397 info_ptr += 16;
8398 break;
8399 case DW_FORM_string:
8400 read_direct_string (abfd, info_ptr, &bytes_read);
8401 info_ptr += bytes_read;
8402 break;
8403 case DW_FORM_sec_offset:
8404 case DW_FORM_strp:
8405 case DW_FORM_GNU_strp_alt:
8406 info_ptr += cu->header.offset_size;
8407 break;
8408 case DW_FORM_exprloc:
8409 case DW_FORM_block:
8410 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8411 info_ptr += bytes_read;
8412 break;
8413 case DW_FORM_block1:
8414 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8415 break;
8416 case DW_FORM_block2:
8417 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8418 break;
8419 case DW_FORM_block4:
8420 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8421 break;
8422 case DW_FORM_addrx:
8423 case DW_FORM_strx:
8424 case DW_FORM_sdata:
8425 case DW_FORM_udata:
8426 case DW_FORM_ref_udata:
8427 case DW_FORM_GNU_addr_index:
8428 case DW_FORM_GNU_str_index:
8429 case DW_FORM_rnglistx:
8430 case DW_FORM_loclistx:
8431 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8432 break;
8433 case DW_FORM_indirect:
8434 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8435 info_ptr += bytes_read;
8436 /* We need to continue parsing from here, so just go back to
8437 the top. */
8438 goto skip_attribute;
8439
8440 default:
8441 error (_("Dwarf Error: Cannot handle %s "
8442 "in DWARF reader [in module %s]"),
8443 dwarf_form_name (form),
8444 bfd_get_filename (abfd));
8445 }
8446 }
8447
8448 if (abbrev->has_children)
8449 return skip_children (reader, info_ptr);
8450 else
8451 return info_ptr;
8452 }
8453
8454 /* Locate ORIG_PDI's sibling.
8455 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8456
8457 static const gdb_byte *
8458 locate_pdi_sibling (const struct die_reader_specs *reader,
8459 struct partial_die_info *orig_pdi,
8460 const gdb_byte *info_ptr)
8461 {
8462 /* Do we know the sibling already? */
8463
8464 if (orig_pdi->sibling)
8465 return orig_pdi->sibling;
8466
8467 /* Are there any children to deal with? */
8468
8469 if (!orig_pdi->has_children)
8470 return info_ptr;
8471
8472 /* Skip the children the long way. */
8473
8474 return skip_children (reader, info_ptr);
8475 }
8476
8477 /* Expand this partial symbol table into a full symbol table. SELF is
8478 not NULL. */
8479
8480 void
8481 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8482 {
8483 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8484
8485 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8486
8487 /* If this psymtab is constructed from a debug-only objfile, the
8488 has_section_at_zero flag will not necessarily be correct. We
8489 can get the correct value for this flag by looking at the data
8490 associated with the (presumably stripped) associated objfile. */
8491 if (objfile->separate_debug_objfile_backlink)
8492 {
8493 dwarf2_per_objfile *per_objfile_backlink
8494 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8495
8496 per_objfile->per_bfd->has_section_at_zero
8497 = per_objfile_backlink->per_bfd->has_section_at_zero;
8498 }
8499
8500 expand_psymtab (objfile);
8501
8502 process_cu_includes (per_objfile);
8503 }
8504 \f
8505 /* Reading in full CUs. */
8506
8507 /* Add PER_CU to the queue. */
8508
8509 static void
8510 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8511 dwarf2_per_objfile *per_objfile,
8512 enum language pretend_language)
8513 {
8514 per_cu->queued = 1;
8515
8516 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8517 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8518 }
8519
8520 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8521
8522 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8523 dependency.
8524
8525 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8526 DIEs, false otherwise.
8527
8528 Explanation: there is an invariant that if a CU is queued for expansion
8529 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8530 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8531 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8532 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8533 invariant is respected.
8534
8535 The caller is therefore not required to load the CU's DIEs (we return false)
8536 if:
8537
8538 - the CU is already expanded, and therefore does not get enqueued
8539 - the CU gets enqueued for expansion, but its DIEs are already loaded
8540
8541 Note that the caller should not use this function's return value as an
8542 indicator of whether the CU's DIEs are loaded right now, it should check
8543 that by calling `dwarf2_per_objfile::get_cu` instead. */
8544
8545 static int
8546 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8547 dwarf2_per_cu_data *per_cu,
8548 dwarf2_per_objfile *per_objfile,
8549 enum language pretend_language)
8550 {
8551 /* We may arrive here during partial symbol reading, if we need full
8552 DIEs to process an unusual case (e.g. template arguments). Do
8553 not queue PER_CU, just tell our caller to load its DIEs. */
8554 if (per_cu->per_bfd->reading_partial_symbols)
8555 {
8556 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8557
8558 if (cu == NULL || cu->dies == NULL)
8559 return 1;
8560 return 0;
8561 }
8562
8563 /* Mark the dependence relation so that we don't flush PER_CU
8564 too early. */
8565 if (dependent_cu != NULL)
8566 dependent_cu->add_dependence (per_cu);
8567
8568 /* If it's already on the queue, we have nothing to do. */
8569 if (per_cu->queued)
8570 {
8571 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8572 loaded. */
8573 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8574
8575 /* If the CU is queued for expansion, it should not already be
8576 expanded. */
8577 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8578
8579 /* The DIEs are already loaded, the caller doesn't need to do it. */
8580 return 0;
8581 }
8582
8583 bool queued = false;
8584 if (!per_objfile->symtab_set_p (per_cu))
8585 {
8586 /* Add it to the queue. */
8587 queue_comp_unit (per_cu, per_objfile, pretend_language);
8588 queued = true;
8589 }
8590
8591 /* If the compilation unit is already loaded, just mark it as
8592 used. */
8593 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8594 if (cu != nullptr)
8595 cu->last_used = 0;
8596
8597 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8598 and the DIEs are not already loaded. */
8599 return queued && cu == nullptr;
8600 }
8601
8602 /* Process the queue. */
8603
8604 static void
8605 process_queue (dwarf2_per_objfile *per_objfile)
8606 {
8607 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8608 objfile_name (per_objfile->objfile));
8609
8610 /* The queue starts out with one item, but following a DIE reference
8611 may load a new CU, adding it to the end of the queue. */
8612 while (!per_objfile->per_bfd->queue->empty ())
8613 {
8614 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8615 dwarf2_per_cu_data *per_cu = item.per_cu;
8616
8617 if (!per_objfile->symtab_set_p (per_cu))
8618 {
8619 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8620
8621 /* Skip dummy CUs. */
8622 if (cu != nullptr)
8623 {
8624 unsigned int debug_print_threshold;
8625 char buf[100];
8626
8627 if (per_cu->is_debug_types)
8628 {
8629 struct signatured_type *sig_type =
8630 (struct signatured_type *) per_cu;
8631
8632 sprintf (buf, "TU %s at offset %s",
8633 hex_string (sig_type->signature),
8634 sect_offset_str (per_cu->sect_off));
8635 /* There can be 100s of TUs.
8636 Only print them in verbose mode. */
8637 debug_print_threshold = 2;
8638 }
8639 else
8640 {
8641 sprintf (buf, "CU at offset %s",
8642 sect_offset_str (per_cu->sect_off));
8643 debug_print_threshold = 1;
8644 }
8645
8646 if (dwarf_read_debug >= debug_print_threshold)
8647 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8648
8649 if (per_cu->is_debug_types)
8650 process_full_type_unit (cu, item.pretend_language);
8651 else
8652 process_full_comp_unit (cu, item.pretend_language);
8653
8654 if (dwarf_read_debug >= debug_print_threshold)
8655 dwarf_read_debug_printf ("Done expanding %s", buf);
8656 }
8657 }
8658
8659 per_cu->queued = 0;
8660 per_objfile->per_bfd->queue->pop ();
8661 }
8662
8663 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8664 objfile_name (per_objfile->objfile));
8665 }
8666
8667 /* Read in full symbols for PST, and anything it depends on. */
8668
8669 void
8670 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8671 {
8672 gdb_assert (!readin_p (objfile));
8673
8674 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8675 free_cached_comp_units freer (per_objfile);
8676 expand_dependencies (objfile);
8677
8678 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8679 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8680 }
8681
8682 /* See psympriv.h. */
8683
8684 bool
8685 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8686 {
8687 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8688 return per_objfile->symtab_set_p (per_cu_data);
8689 }
8690
8691 /* See psympriv.h. */
8692
8693 compunit_symtab *
8694 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8695 {
8696 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8697 return per_objfile->get_symtab (per_cu_data);
8698 }
8699
8700 /* Trivial hash function for die_info: the hash value of a DIE
8701 is its offset in .debug_info for this objfile. */
8702
8703 static hashval_t
8704 die_hash (const void *item)
8705 {
8706 const struct die_info *die = (const struct die_info *) item;
8707
8708 return to_underlying (die->sect_off);
8709 }
8710
8711 /* Trivial comparison function for die_info structures: two DIEs
8712 are equal if they have the same offset. */
8713
8714 static int
8715 die_eq (const void *item_lhs, const void *item_rhs)
8716 {
8717 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8718 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8719
8720 return die_lhs->sect_off == die_rhs->sect_off;
8721 }
8722
8723 /* Load the DIEs associated with PER_CU into memory.
8724
8725 In some cases, the caller, while reading partial symbols, will need to load
8726 the full symbols for the CU for some reason. It will already have a
8727 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8728 rather than creating a new one. */
8729
8730 static void
8731 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8732 dwarf2_per_objfile *per_objfile,
8733 dwarf2_cu *existing_cu,
8734 bool skip_partial,
8735 enum language pretend_language)
8736 {
8737 gdb_assert (! this_cu->is_debug_types);
8738
8739 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8740 if (reader.dummy_p)
8741 return;
8742
8743 struct dwarf2_cu *cu = reader.cu;
8744 const gdb_byte *info_ptr = reader.info_ptr;
8745
8746 gdb_assert (cu->die_hash == NULL);
8747 cu->die_hash =
8748 htab_create_alloc_ex (cu->header.length / 12,
8749 die_hash,
8750 die_eq,
8751 NULL,
8752 &cu->comp_unit_obstack,
8753 hashtab_obstack_allocate,
8754 dummy_obstack_deallocate);
8755
8756 if (reader.comp_unit_die->has_children)
8757 reader.comp_unit_die->child
8758 = read_die_and_siblings (&reader, reader.info_ptr,
8759 &info_ptr, reader.comp_unit_die);
8760 cu->dies = reader.comp_unit_die;
8761 /* comp_unit_die is not stored in die_hash, no need. */
8762
8763 /* We try not to read any attributes in this function, because not
8764 all CUs needed for references have been loaded yet, and symbol
8765 table processing isn't initialized. But we have to set the CU language,
8766 or we won't be able to build types correctly.
8767 Similarly, if we do not read the producer, we can not apply
8768 producer-specific interpretation. */
8769 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8770
8771 reader.keep ();
8772 }
8773
8774 /* Add a DIE to the delayed physname list. */
8775
8776 static void
8777 add_to_method_list (struct type *type, int fnfield_index, int index,
8778 const char *name, struct die_info *die,
8779 struct dwarf2_cu *cu)
8780 {
8781 struct delayed_method_info mi;
8782 mi.type = type;
8783 mi.fnfield_index = fnfield_index;
8784 mi.index = index;
8785 mi.name = name;
8786 mi.die = die;
8787 cu->method_list.push_back (mi);
8788 }
8789
8790 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8791 "const" / "volatile". If so, decrements LEN by the length of the
8792 modifier and return true. Otherwise return false. */
8793
8794 template<size_t N>
8795 static bool
8796 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8797 {
8798 size_t mod_len = sizeof (mod) - 1;
8799 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8800 {
8801 len -= mod_len;
8802 return true;
8803 }
8804 return false;
8805 }
8806
8807 /* Compute the physnames of any methods on the CU's method list.
8808
8809 The computation of method physnames is delayed in order to avoid the
8810 (bad) condition that one of the method's formal parameters is of an as yet
8811 incomplete type. */
8812
8813 static void
8814 compute_delayed_physnames (struct dwarf2_cu *cu)
8815 {
8816 /* Only C++ delays computing physnames. */
8817 if (cu->method_list.empty ())
8818 return;
8819 gdb_assert (cu->per_cu->lang == language_cplus);
8820
8821 for (const delayed_method_info &mi : cu->method_list)
8822 {
8823 const char *physname;
8824 struct fn_fieldlist *fn_flp
8825 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8826 physname = dwarf2_physname (mi.name, mi.die, cu);
8827 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8828 = physname ? physname : "";
8829
8830 /* Since there's no tag to indicate whether a method is a
8831 const/volatile overload, extract that information out of the
8832 demangled name. */
8833 if (physname != NULL)
8834 {
8835 size_t len = strlen (physname);
8836
8837 while (1)
8838 {
8839 if (physname[len] == ')') /* shortcut */
8840 break;
8841 else if (check_modifier (physname, len, " const"))
8842 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8843 else if (check_modifier (physname, len, " volatile"))
8844 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8845 else
8846 break;
8847 }
8848 }
8849 }
8850
8851 /* The list is no longer needed. */
8852 cu->method_list.clear ();
8853 }
8854
8855 /* Go objects should be embedded in a DW_TAG_module DIE,
8856 and it's not clear if/how imported objects will appear.
8857 To keep Go support simple until that's worked out,
8858 go back through what we've read and create something usable.
8859 We could do this while processing each DIE, and feels kinda cleaner,
8860 but that way is more invasive.
8861 This is to, for example, allow the user to type "p var" or "b main"
8862 without having to specify the package name, and allow lookups
8863 of module.object to work in contexts that use the expression
8864 parser. */
8865
8866 static void
8867 fixup_go_packaging (struct dwarf2_cu *cu)
8868 {
8869 gdb::unique_xmalloc_ptr<char> package_name;
8870 struct pending *list;
8871 int i;
8872
8873 for (list = *cu->get_builder ()->get_global_symbols ();
8874 list != NULL;
8875 list = list->next)
8876 {
8877 for (i = 0; i < list->nsyms; ++i)
8878 {
8879 struct symbol *sym = list->symbol[i];
8880
8881 if (sym->language () == language_go
8882 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8883 {
8884 gdb::unique_xmalloc_ptr<char> this_package_name
8885 (go_symbol_package_name (sym));
8886
8887 if (this_package_name == NULL)
8888 continue;
8889 if (package_name == NULL)
8890 package_name = std::move (this_package_name);
8891 else
8892 {
8893 struct objfile *objfile = cu->per_objfile->objfile;
8894 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8895 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8896 (symbol_symtab (sym) != NULL
8897 ? symtab_to_filename_for_display
8898 (symbol_symtab (sym))
8899 : objfile_name (objfile)),
8900 this_package_name.get (), package_name.get ());
8901 }
8902 }
8903 }
8904 }
8905
8906 if (package_name != NULL)
8907 {
8908 struct objfile *objfile = cu->per_objfile->objfile;
8909 const char *saved_package_name = objfile->intern (package_name.get ());
8910 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8911 saved_package_name);
8912 struct symbol *sym;
8913
8914 sym = new (&objfile->objfile_obstack) symbol;
8915 sym->set_language (language_go, &objfile->objfile_obstack);
8916 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8917 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8918 e.g., "main" finds the "main" module and not C's main(). */
8919 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8920 sym->set_aclass_index (LOC_TYPEDEF);
8921 SYMBOL_TYPE (sym) = type;
8922
8923 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8924 }
8925 }
8926
8927 /* Allocate a fully-qualified name consisting of the two parts on the
8928 obstack. */
8929
8930 static const char *
8931 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8932 {
8933 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8934 }
8935
8936 /* A helper that allocates a variant part to attach to a Rust enum
8937 type. OBSTACK is where the results should be allocated. TYPE is
8938 the type we're processing. DISCRIMINANT_INDEX is the index of the
8939 discriminant. It must be the index of one of the fields of TYPE,
8940 or -1 to mean there is no discriminant (univariant enum).
8941 DEFAULT_INDEX is the index of the default field; or -1 if there is
8942 no default. RANGES is indexed by "effective" field number (the
8943 field index, but omitting the discriminant and default fields) and
8944 must hold the discriminant values used by the variants. Note that
8945 RANGES must have a lifetime at least as long as OBSTACK -- either
8946 already allocated on it, or static. */
8947
8948 static void
8949 alloc_rust_variant (struct obstack *obstack, struct type *type,
8950 int discriminant_index, int default_index,
8951 gdb::array_view<discriminant_range> ranges)
8952 {
8953 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8954 gdb_assert (discriminant_index == -1
8955 || (discriminant_index >= 0
8956 && discriminant_index < type->num_fields ()));
8957 gdb_assert (default_index == -1
8958 || (default_index >= 0 && default_index < type->num_fields ()));
8959
8960 /* We have one variant for each non-discriminant field. */
8961 int n_variants = type->num_fields ();
8962 if (discriminant_index != -1)
8963 --n_variants;
8964
8965 variant *variants = new (obstack) variant[n_variants];
8966 int var_idx = 0;
8967 int range_idx = 0;
8968 for (int i = 0; i < type->num_fields (); ++i)
8969 {
8970 if (i == discriminant_index)
8971 continue;
8972
8973 variants[var_idx].first_field = i;
8974 variants[var_idx].last_field = i + 1;
8975
8976 /* The default field does not need a range, but other fields do.
8977 We skipped the discriminant above. */
8978 if (i != default_index)
8979 {
8980 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8981 ++range_idx;
8982 }
8983
8984 ++var_idx;
8985 }
8986
8987 gdb_assert (range_idx == ranges.size ());
8988 gdb_assert (var_idx == n_variants);
8989
8990 variant_part *part = new (obstack) variant_part;
8991 part->discriminant_index = discriminant_index;
8992 /* If there is no discriminant, then whether it is signed is of no
8993 consequence. */
8994 part->is_unsigned
8995 = (discriminant_index == -1
8996 ? false
8997 : type->field (discriminant_index).type ()->is_unsigned ());
8998 part->variants = gdb::array_view<variant> (variants, n_variants);
8999
9000 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9001 gdb::array_view<variant_part> *prop_value
9002 = new (storage) gdb::array_view<variant_part> (part, 1);
9003
9004 struct dynamic_prop prop;
9005 prop.set_variant_parts (prop_value);
9006
9007 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9008 }
9009
9010 /* Some versions of rustc emitted enums in an unusual way.
9011
9012 Ordinary enums were emitted as unions. The first element of each
9013 structure in the union was named "RUST$ENUM$DISR". This element
9014 held the discriminant.
9015
9016 These versions of Rust also implemented the "non-zero"
9017 optimization. When the enum had two values, and one is empty and
9018 the other holds a pointer that cannot be zero, the pointer is used
9019 as the discriminant, with a zero value meaning the empty variant.
9020 Here, the union's first member is of the form
9021 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9022 where the fieldnos are the indices of the fields that should be
9023 traversed in order to find the field (which may be several fields deep)
9024 and the variantname is the name of the variant of the case when the
9025 field is zero.
9026
9027 This function recognizes whether TYPE is of one of these forms,
9028 and, if so, smashes it to be a variant type. */
9029
9030 static void
9031 quirk_rust_enum (struct type *type, struct objfile *objfile)
9032 {
9033 gdb_assert (type->code () == TYPE_CODE_UNION);
9034
9035 /* We don't need to deal with empty enums. */
9036 if (type->num_fields () == 0)
9037 return;
9038
9039 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9040 if (type->num_fields () == 1
9041 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
9042 {
9043 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
9044
9045 /* Decode the field name to find the offset of the
9046 discriminant. */
9047 ULONGEST bit_offset = 0;
9048 struct type *field_type = type->field (0).type ();
9049 while (name[0] >= '0' && name[0] <= '9')
9050 {
9051 char *tail;
9052 unsigned long index = strtoul (name, &tail, 10);
9053 name = tail;
9054 if (*name != '$'
9055 || index >= field_type->num_fields ()
9056 || (field_type->field (index).loc_kind ()
9057 != FIELD_LOC_KIND_BITPOS))
9058 {
9059 complaint (_("Could not parse Rust enum encoding string \"%s\""
9060 "[in module %s]"),
9061 type->field (0).name (),
9062 objfile_name (objfile));
9063 return;
9064 }
9065 ++name;
9066
9067 bit_offset += field_type->field (index).loc_bitpos ();
9068 field_type = field_type->field (index).type ();
9069 }
9070
9071 /* Smash this type to be a structure type. We have to do this
9072 because the type has already been recorded. */
9073 type->set_code (TYPE_CODE_STRUCT);
9074 type->set_num_fields (3);
9075 /* Save the field we care about. */
9076 struct field saved_field = type->field (0);
9077 type->set_fields
9078 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9079
9080 /* Put the discriminant at index 0. */
9081 type->field (0).set_type (field_type);
9082 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9083 type->field (0).set_name ("<<discriminant>>");
9084 type->field (0).set_loc_bitpos (bit_offset);
9085
9086 /* The order of fields doesn't really matter, so put the real
9087 field at index 1 and the data-less field at index 2. */
9088 type->field (1) = saved_field;
9089 type->field (1).set_name
9090 (rust_last_path_segment (type->field (1).type ()->name ()));
9091 type->field (1).type ()->set_name
9092 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9093 type->field (1).name ()));
9094
9095 const char *dataless_name
9096 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9097 name);
9098 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9099 dataless_name);
9100 type->field (2).set_type (dataless_type);
9101 /* NAME points into the original discriminant name, which
9102 already has the correct lifetime. */
9103 type->field (2).set_name (name);
9104 type->field (2).set_loc_bitpos (0);
9105
9106 /* Indicate that this is a variant type. */
9107 static discriminant_range ranges[1] = { { 0, 0 } };
9108 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9109 }
9110 /* A union with a single anonymous field is probably an old-style
9111 univariant enum. */
9112 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
9113 {
9114 /* Smash this type to be a structure type. We have to do this
9115 because the type has already been recorded. */
9116 type->set_code (TYPE_CODE_STRUCT);
9117
9118 struct type *field_type = type->field (0).type ();
9119 const char *variant_name
9120 = rust_last_path_segment (field_type->name ());
9121 type->field (0).set_name (variant_name);
9122 field_type->set_name
9123 (rust_fully_qualify (&objfile->objfile_obstack,
9124 type->name (), variant_name));
9125
9126 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9127 }
9128 else
9129 {
9130 struct type *disr_type = nullptr;
9131 for (int i = 0; i < type->num_fields (); ++i)
9132 {
9133 disr_type = type->field (i).type ();
9134
9135 if (disr_type->code () != TYPE_CODE_STRUCT)
9136 {
9137 /* All fields of a true enum will be structs. */
9138 return;
9139 }
9140 else if (disr_type->num_fields () == 0)
9141 {
9142 /* Could be data-less variant, so keep going. */
9143 disr_type = nullptr;
9144 }
9145 else if (strcmp (disr_type->field (0).name (),
9146 "RUST$ENUM$DISR") != 0)
9147 {
9148 /* Not a Rust enum. */
9149 return;
9150 }
9151 else
9152 {
9153 /* Found one. */
9154 break;
9155 }
9156 }
9157
9158 /* If we got here without a discriminant, then it's probably
9159 just a union. */
9160 if (disr_type == nullptr)
9161 return;
9162
9163 /* Smash this type to be a structure type. We have to do this
9164 because the type has already been recorded. */
9165 type->set_code (TYPE_CODE_STRUCT);
9166
9167 /* Make space for the discriminant field. */
9168 struct field *disr_field = &disr_type->field (0);
9169 field *new_fields
9170 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9171 * sizeof (struct field)));
9172 memcpy (new_fields + 1, type->fields (),
9173 type->num_fields () * sizeof (struct field));
9174 type->set_fields (new_fields);
9175 type->set_num_fields (type->num_fields () + 1);
9176
9177 /* Install the discriminant at index 0 in the union. */
9178 type->field (0) = *disr_field;
9179 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9180 type->field (0).set_name ("<<discriminant>>");
9181
9182 /* We need a way to find the correct discriminant given a
9183 variant name. For convenience we build a map here. */
9184 struct type *enum_type = disr_field->type ();
9185 std::unordered_map<std::string, ULONGEST> discriminant_map;
9186 for (int i = 0; i < enum_type->num_fields (); ++i)
9187 {
9188 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
9189 {
9190 const char *name
9191 = rust_last_path_segment (enum_type->field (i).name ());
9192 discriminant_map[name] = enum_type->field (i).loc_enumval ();
9193 }
9194 }
9195
9196 int n_fields = type->num_fields ();
9197 /* We don't need a range entry for the discriminant, but we do
9198 need one for every other field, as there is no default
9199 variant. */
9200 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9201 discriminant_range,
9202 n_fields - 1);
9203 /* Skip the discriminant here. */
9204 for (int i = 1; i < n_fields; ++i)
9205 {
9206 /* Find the final word in the name of this variant's type.
9207 That name can be used to look up the correct
9208 discriminant. */
9209 const char *variant_name
9210 = rust_last_path_segment (type->field (i).type ()->name ());
9211
9212 auto iter = discriminant_map.find (variant_name);
9213 if (iter != discriminant_map.end ())
9214 {
9215 ranges[i - 1].low = iter->second;
9216 ranges[i - 1].high = iter->second;
9217 }
9218
9219 /* In Rust, each element should have the size of the
9220 enclosing enum. */
9221 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9222
9223 /* Remove the discriminant field, if it exists. */
9224 struct type *sub_type = type->field (i).type ();
9225 if (sub_type->num_fields () > 0)
9226 {
9227 sub_type->set_num_fields (sub_type->num_fields () - 1);
9228 sub_type->set_fields (sub_type->fields () + 1);
9229 }
9230 type->field (i).set_name (variant_name);
9231 sub_type->set_name
9232 (rust_fully_qualify (&objfile->objfile_obstack,
9233 type->name (), variant_name));
9234 }
9235
9236 /* Indicate that this is a variant type. */
9237 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9238 gdb::array_view<discriminant_range> (ranges,
9239 n_fields - 1));
9240 }
9241 }
9242
9243 /* Rewrite some Rust unions to be structures with variants parts. */
9244
9245 static void
9246 rust_union_quirks (struct dwarf2_cu *cu)
9247 {
9248 gdb_assert (cu->per_cu->lang == language_rust);
9249 for (type *type_ : cu->rust_unions)
9250 quirk_rust_enum (type_, cu->per_objfile->objfile);
9251 /* We don't need this any more. */
9252 cu->rust_unions.clear ();
9253 }
9254
9255 /* See read.h. */
9256
9257 type_unit_group_unshareable *
9258 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9259 {
9260 auto iter = this->m_type_units.find (tu_group);
9261 if (iter != this->m_type_units.end ())
9262 return iter->second.get ();
9263
9264 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9265 type_unit_group_unshareable *result = uniq.get ();
9266 this->m_type_units[tu_group] = std::move (uniq);
9267 return result;
9268 }
9269
9270 struct type *
9271 dwarf2_per_objfile::get_type_for_signatured_type
9272 (signatured_type *sig_type) const
9273 {
9274 auto iter = this->m_type_map.find (sig_type);
9275 if (iter == this->m_type_map.end ())
9276 return nullptr;
9277
9278 return iter->second;
9279 }
9280
9281 void dwarf2_per_objfile::set_type_for_signatured_type
9282 (signatured_type *sig_type, struct type *type)
9283 {
9284 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9285
9286 this->m_type_map[sig_type] = type;
9287 }
9288
9289 /* A helper function for computing the list of all symbol tables
9290 included by PER_CU. */
9291
9292 static void
9293 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9294 htab_t all_children, htab_t all_type_symtabs,
9295 dwarf2_per_cu_data *per_cu,
9296 dwarf2_per_objfile *per_objfile,
9297 struct compunit_symtab *immediate_parent)
9298 {
9299 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9300 if (*slot != NULL)
9301 {
9302 /* This inclusion and its children have been processed. */
9303 return;
9304 }
9305
9306 *slot = per_cu;
9307
9308 /* Only add a CU if it has a symbol table. */
9309 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9310 if (cust != NULL)
9311 {
9312 /* If this is a type unit only add its symbol table if we haven't
9313 seen it yet (type unit per_cu's can share symtabs). */
9314 if (per_cu->is_debug_types)
9315 {
9316 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9317 if (*slot == NULL)
9318 {
9319 *slot = cust;
9320 result->push_back (cust);
9321 if (cust->user == NULL)
9322 cust->user = immediate_parent;
9323 }
9324 }
9325 else
9326 {
9327 result->push_back (cust);
9328 if (cust->user == NULL)
9329 cust->user = immediate_parent;
9330 }
9331 }
9332
9333 if (!per_cu->imported_symtabs_empty ())
9334 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9335 {
9336 recursively_compute_inclusions (result, all_children,
9337 all_type_symtabs, ptr, per_objfile,
9338 cust);
9339 }
9340 }
9341
9342 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9343 PER_CU. */
9344
9345 static void
9346 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9347 dwarf2_per_objfile *per_objfile)
9348 {
9349 gdb_assert (! per_cu->is_debug_types);
9350
9351 if (!per_cu->imported_symtabs_empty ())
9352 {
9353 int len;
9354 std::vector<compunit_symtab *> result_symtabs;
9355 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9356
9357 /* If we don't have a symtab, we can just skip this case. */
9358 if (cust == NULL)
9359 return;
9360
9361 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9362 htab_eq_pointer,
9363 NULL, xcalloc, xfree));
9364 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9365 htab_eq_pointer,
9366 NULL, xcalloc, xfree));
9367
9368 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9369 {
9370 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9371 all_type_symtabs.get (), ptr,
9372 per_objfile, cust);
9373 }
9374
9375 /* Now we have a transitive closure of all the included symtabs. */
9376 len = result_symtabs.size ();
9377 cust->includes
9378 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9379 struct compunit_symtab *, len + 1);
9380 memcpy (cust->includes, result_symtabs.data (),
9381 len * sizeof (compunit_symtab *));
9382 cust->includes[len] = NULL;
9383 }
9384 }
9385
9386 /* Compute the 'includes' field for the symtabs of all the CUs we just
9387 read. */
9388
9389 static void
9390 process_cu_includes (dwarf2_per_objfile *per_objfile)
9391 {
9392 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9393 {
9394 if (! iter->is_debug_types)
9395 compute_compunit_symtab_includes (iter, per_objfile);
9396 }
9397
9398 per_objfile->per_bfd->just_read_cus.clear ();
9399 }
9400
9401 /* Generate full symbol information for CU, whose DIEs have
9402 already been loaded into memory. */
9403
9404 static void
9405 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9406 {
9407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9408 struct objfile *objfile = per_objfile->objfile;
9409 struct gdbarch *gdbarch = objfile->arch ();
9410 CORE_ADDR lowpc, highpc;
9411 struct compunit_symtab *cust;
9412 CORE_ADDR baseaddr;
9413 struct block *static_block;
9414 CORE_ADDR addr;
9415
9416 baseaddr = objfile->text_section_offset ();
9417
9418 /* Clear the list here in case something was left over. */
9419 cu->method_list.clear ();
9420
9421 dwarf2_find_base_address (cu->dies, cu);
9422
9423 /* Before we start reading the top-level DIE, ensure it has a valid tag
9424 type. */
9425 switch (cu->dies->tag)
9426 {
9427 case DW_TAG_compile_unit:
9428 case DW_TAG_partial_unit:
9429 case DW_TAG_type_unit:
9430 break;
9431 default:
9432 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9433 dwarf_tag_name (cu->dies->tag),
9434 sect_offset_str (cu->per_cu->sect_off),
9435 objfile_name (per_objfile->objfile));
9436 }
9437
9438 /* Do line number decoding in read_file_scope () */
9439 process_die (cu->dies, cu);
9440
9441 /* For now fudge the Go package. */
9442 if (cu->per_cu->lang == language_go)
9443 fixup_go_packaging (cu);
9444
9445 /* Now that we have processed all the DIEs in the CU, all the types
9446 should be complete, and it should now be safe to compute all of the
9447 physnames. */
9448 compute_delayed_physnames (cu);
9449
9450 if (cu->per_cu->lang == language_rust)
9451 rust_union_quirks (cu);
9452
9453 /* Some compilers don't define a DW_AT_high_pc attribute for the
9454 compilation unit. If the DW_AT_high_pc is missing, synthesize
9455 it, by scanning the DIE's below the compilation unit. */
9456 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9457
9458 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9459 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9460
9461 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9462 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9463 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9464 addrmap to help ensure it has an accurate map of pc values belonging to
9465 this comp unit. */
9466 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9467
9468 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9469 SECT_OFF_TEXT (objfile),
9470 0);
9471
9472 if (cust != NULL)
9473 {
9474 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9475
9476 /* Set symtab language to language from DW_AT_language. If the
9477 compilation is from a C file generated by language preprocessors, do
9478 not set the language if it was already deduced by start_subfile. */
9479 if (!(cu->per_cu->lang == language_c
9480 && cust->primary_filetab ()->language () != language_unknown))
9481 cust->primary_filetab ()->set_language (cu->per_cu->lang);
9482
9483 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9484 produce DW_AT_location with location lists but it can be possibly
9485 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9486 there were bugs in prologue debug info, fixed later in GCC-4.5
9487 by "unwind info for epilogues" patch (which is not directly related).
9488
9489 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9490 needed, it would be wrong due to missing DW_AT_producer there.
9491
9492 Still one can confuse GDB by using non-standard GCC compilation
9493 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9494 */
9495 if (cu->has_loclist && gcc_4_minor >= 5)
9496 cust->set_locations_valid (true);
9497
9498 if (gcc_4_minor >= 5)
9499 cust->set_epilogue_unwind_valid (true);
9500
9501 cust->set_call_site_htab (cu->call_site_htab);
9502 }
9503
9504 per_objfile->set_symtab (cu->per_cu, cust);
9505
9506 /* Push it for inclusion processing later. */
9507 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9508
9509 /* Not needed any more. */
9510 cu->reset_builder ();
9511 }
9512
9513 /* Generate full symbol information for type unit CU, whose DIEs have
9514 already been loaded into memory. */
9515
9516 static void
9517 process_full_type_unit (dwarf2_cu *cu,
9518 enum language pretend_language)
9519 {
9520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9521 struct objfile *objfile = per_objfile->objfile;
9522 struct compunit_symtab *cust;
9523 struct signatured_type *sig_type;
9524
9525 gdb_assert (cu->per_cu->is_debug_types);
9526 sig_type = (struct signatured_type *) cu->per_cu;
9527
9528 /* Clear the list here in case something was left over. */
9529 cu->method_list.clear ();
9530
9531 /* The symbol tables are set up in read_type_unit_scope. */
9532 process_die (cu->dies, cu);
9533
9534 /* For now fudge the Go package. */
9535 if (cu->per_cu->lang == language_go)
9536 fixup_go_packaging (cu);
9537
9538 /* Now that we have processed all the DIEs in the CU, all the types
9539 should be complete, and it should now be safe to compute all of the
9540 physnames. */
9541 compute_delayed_physnames (cu);
9542
9543 if (cu->per_cu->lang == language_rust)
9544 rust_union_quirks (cu);
9545
9546 /* TUs share symbol tables.
9547 If this is the first TU to use this symtab, complete the construction
9548 of it with end_expandable_symtab. Otherwise, complete the addition of
9549 this TU's symbols to the existing symtab. */
9550 type_unit_group_unshareable *tug_unshare =
9551 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9552 if (tug_unshare->compunit_symtab == NULL)
9553 {
9554 buildsym_compunit *builder = cu->get_builder ();
9555 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9556 tug_unshare->compunit_symtab = cust;
9557
9558 if (cust != NULL)
9559 {
9560 /* Set symtab language to language from DW_AT_language. If the
9561 compilation is from a C file generated by language preprocessors,
9562 do not set the language if it was already deduced by
9563 start_subfile. */
9564 if (!(cu->per_cu->lang == language_c
9565 && cust->primary_filetab ()->language () != language_c))
9566 cust->primary_filetab ()->set_language (cu->per_cu->lang);
9567 }
9568 }
9569 else
9570 {
9571 cu->get_builder ()->augment_type_symtab ();
9572 cust = tug_unshare->compunit_symtab;
9573 }
9574
9575 per_objfile->set_symtab (cu->per_cu, cust);
9576
9577 /* Not needed any more. */
9578 cu->reset_builder ();
9579 }
9580
9581 /* Process an imported unit DIE. */
9582
9583 static void
9584 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9585 {
9586 struct attribute *attr;
9587
9588 /* For now we don't handle imported units in type units. */
9589 if (cu->per_cu->is_debug_types)
9590 {
9591 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9592 " supported in type units [in module %s]"),
9593 objfile_name (cu->per_objfile->objfile));
9594 }
9595
9596 attr = dwarf2_attr (die, DW_AT_import, cu);
9597 if (attr != NULL)
9598 {
9599 sect_offset sect_off = attr->get_ref_die_offset ();
9600 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9601 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9602 dwarf2_per_cu_data *per_cu
9603 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9604 per_objfile->per_bfd);
9605
9606 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9607 into another compilation unit, at root level. Regard this as a hint,
9608 and ignore it. */
9609 if (die->parent && die->parent->parent == NULL
9610 && per_cu->unit_type == DW_UT_compile
9611 && per_cu->lang == language_cplus)
9612 return;
9613
9614 /* If necessary, add it to the queue and load its DIEs. */
9615 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9616 cu->per_cu->lang))
9617 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9618 false, cu->per_cu->lang);
9619
9620 cu->per_cu->imported_symtabs_push (per_cu);
9621 }
9622 }
9623
9624 /* RAII object that represents a process_die scope: i.e.,
9625 starts/finishes processing a DIE. */
9626 class process_die_scope
9627 {
9628 public:
9629 process_die_scope (die_info *die, dwarf2_cu *cu)
9630 : m_die (die), m_cu (cu)
9631 {
9632 /* We should only be processing DIEs not already in process. */
9633 gdb_assert (!m_die->in_process);
9634 m_die->in_process = true;
9635 }
9636
9637 ~process_die_scope ()
9638 {
9639 m_die->in_process = false;
9640
9641 /* If we're done processing the DIE for the CU that owns the line
9642 header, we don't need the line header anymore. */
9643 if (m_cu->line_header_die_owner == m_die)
9644 {
9645 delete m_cu->line_header;
9646 m_cu->line_header = NULL;
9647 m_cu->line_header_die_owner = NULL;
9648 }
9649 }
9650
9651 private:
9652 die_info *m_die;
9653 dwarf2_cu *m_cu;
9654 };
9655
9656 /* Process a die and its children. */
9657
9658 static void
9659 process_die (struct die_info *die, struct dwarf2_cu *cu)
9660 {
9661 process_die_scope scope (die, cu);
9662
9663 switch (die->tag)
9664 {
9665 case DW_TAG_padding:
9666 break;
9667 case DW_TAG_compile_unit:
9668 case DW_TAG_partial_unit:
9669 read_file_scope (die, cu);
9670 break;
9671 case DW_TAG_type_unit:
9672 read_type_unit_scope (die, cu);
9673 break;
9674 case DW_TAG_subprogram:
9675 /* Nested subprograms in Fortran get a prefix. */
9676 if (cu->per_cu->lang == language_fortran
9677 && die->parent != NULL
9678 && die->parent->tag == DW_TAG_subprogram)
9679 cu->processing_has_namespace_info = true;
9680 /* Fall through. */
9681 case DW_TAG_inlined_subroutine:
9682 read_func_scope (die, cu);
9683 break;
9684 case DW_TAG_lexical_block:
9685 case DW_TAG_try_block:
9686 case DW_TAG_catch_block:
9687 read_lexical_block_scope (die, cu);
9688 break;
9689 case DW_TAG_call_site:
9690 case DW_TAG_GNU_call_site:
9691 read_call_site_scope (die, cu);
9692 break;
9693 case DW_TAG_class_type:
9694 case DW_TAG_interface_type:
9695 case DW_TAG_structure_type:
9696 case DW_TAG_union_type:
9697 process_structure_scope (die, cu);
9698 break;
9699 case DW_TAG_enumeration_type:
9700 process_enumeration_scope (die, cu);
9701 break;
9702
9703 /* These dies have a type, but processing them does not create
9704 a symbol or recurse to process the children. Therefore we can
9705 read them on-demand through read_type_die. */
9706 case DW_TAG_subroutine_type:
9707 case DW_TAG_set_type:
9708 case DW_TAG_pointer_type:
9709 case DW_TAG_ptr_to_member_type:
9710 case DW_TAG_reference_type:
9711 case DW_TAG_rvalue_reference_type:
9712 case DW_TAG_string_type:
9713 break;
9714
9715 case DW_TAG_array_type:
9716 /* We only need to handle this case for Ada -- in other
9717 languages, it's normal for the compiler to emit a typedef
9718 instead. */
9719 if (cu->per_cu->lang != language_ada)
9720 break;
9721 /* FALLTHROUGH */
9722 case DW_TAG_base_type:
9723 case DW_TAG_subrange_type:
9724 case DW_TAG_typedef:
9725 /* Add a typedef symbol for the type definition, if it has a
9726 DW_AT_name. */
9727 new_symbol (die, read_type_die (die, cu), cu);
9728 break;
9729 case DW_TAG_common_block:
9730 read_common_block (die, cu);
9731 break;
9732 case DW_TAG_common_inclusion:
9733 break;
9734 case DW_TAG_namespace:
9735 cu->processing_has_namespace_info = true;
9736 read_namespace (die, cu);
9737 break;
9738 case DW_TAG_module:
9739 cu->processing_has_namespace_info = true;
9740 read_module (die, cu);
9741 break;
9742 case DW_TAG_imported_declaration:
9743 cu->processing_has_namespace_info = true;
9744 if (read_namespace_alias (die, cu))
9745 break;
9746 /* The declaration is not a global namespace alias. */
9747 /* Fall through. */
9748 case DW_TAG_imported_module:
9749 cu->processing_has_namespace_info = true;
9750 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9751 || cu->per_cu->lang != language_fortran))
9752 complaint (_("Tag '%s' has unexpected children"),
9753 dwarf_tag_name (die->tag));
9754 read_import_statement (die, cu);
9755 break;
9756
9757 case DW_TAG_imported_unit:
9758 process_imported_unit_die (die, cu);
9759 break;
9760
9761 case DW_TAG_variable:
9762 read_variable (die, cu);
9763 break;
9764
9765 default:
9766 new_symbol (die, NULL, cu);
9767 break;
9768 }
9769 }
9770 \f
9771 /* DWARF name computation. */
9772
9773 /* A helper function for dwarf2_compute_name which determines whether DIE
9774 needs to have the name of the scope prepended to the name listed in the
9775 die. */
9776
9777 static int
9778 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9779 {
9780 struct attribute *attr;
9781
9782 switch (die->tag)
9783 {
9784 case DW_TAG_namespace:
9785 case DW_TAG_typedef:
9786 case DW_TAG_class_type:
9787 case DW_TAG_interface_type:
9788 case DW_TAG_structure_type:
9789 case DW_TAG_union_type:
9790 case DW_TAG_enumeration_type:
9791 case DW_TAG_enumerator:
9792 case DW_TAG_subprogram:
9793 case DW_TAG_inlined_subroutine:
9794 case DW_TAG_member:
9795 case DW_TAG_imported_declaration:
9796 return 1;
9797
9798 case DW_TAG_variable:
9799 case DW_TAG_constant:
9800 /* We only need to prefix "globally" visible variables. These include
9801 any variable marked with DW_AT_external or any variable that
9802 lives in a namespace. [Variables in anonymous namespaces
9803 require prefixing, but they are not DW_AT_external.] */
9804
9805 if (dwarf2_attr (die, DW_AT_specification, cu))
9806 {
9807 struct dwarf2_cu *spec_cu = cu;
9808
9809 return die_needs_namespace (die_specification (die, &spec_cu),
9810 spec_cu);
9811 }
9812
9813 attr = dwarf2_attr (die, DW_AT_external, cu);
9814 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9815 && die->parent->tag != DW_TAG_module)
9816 return 0;
9817 /* A variable in a lexical block of some kind does not need a
9818 namespace, even though in C++ such variables may be external
9819 and have a mangled name. */
9820 if (die->parent->tag == DW_TAG_lexical_block
9821 || die->parent->tag == DW_TAG_try_block
9822 || die->parent->tag == DW_TAG_catch_block
9823 || die->parent->tag == DW_TAG_subprogram)
9824 return 0;
9825 return 1;
9826
9827 default:
9828 return 0;
9829 }
9830 }
9831
9832 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9833 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9834 defined for the given DIE. */
9835
9836 static struct attribute *
9837 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9838 {
9839 struct attribute *attr;
9840
9841 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9842 if (attr == NULL)
9843 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9844
9845 return attr;
9846 }
9847
9848 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9849 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9850 defined for the given DIE. */
9851
9852 static const char *
9853 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9854 {
9855 const char *linkage_name;
9856
9857 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9858 if (linkage_name == NULL)
9859 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9860
9861 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9862 See https://github.com/rust-lang/rust/issues/32925. */
9863 if (cu->per_cu->lang == language_rust && linkage_name != NULL
9864 && strchr (linkage_name, '{') != NULL)
9865 linkage_name = NULL;
9866
9867 return linkage_name;
9868 }
9869
9870 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9871 compute the physname for the object, which include a method's:
9872 - formal parameters (C++),
9873 - receiver type (Go),
9874
9875 The term "physname" is a bit confusing.
9876 For C++, for example, it is the demangled name.
9877 For Go, for example, it's the mangled name.
9878
9879 For Ada, return the DIE's linkage name rather than the fully qualified
9880 name. PHYSNAME is ignored..
9881
9882 The result is allocated on the objfile->per_bfd's obstack and
9883 canonicalized. */
9884
9885 static const char *
9886 dwarf2_compute_name (const char *name,
9887 struct die_info *die, struct dwarf2_cu *cu,
9888 int physname)
9889 {
9890 struct objfile *objfile = cu->per_objfile->objfile;
9891
9892 if (name == NULL)
9893 name = dwarf2_name (die, cu);
9894
9895 enum language lang = cu->per_cu->lang;
9896
9897 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9898 but otherwise compute it by typename_concat inside GDB.
9899 FIXME: Actually this is not really true, or at least not always true.
9900 It's all very confusing. compute_and_set_names doesn't try to demangle
9901 Fortran names because there is no mangling standard. So new_symbol
9902 will set the demangled name to the result of dwarf2_full_name, and it is
9903 the demangled name that GDB uses if it exists. */
9904 if (lang == language_ada
9905 || (lang == language_fortran && physname))
9906 {
9907 /* For Ada unit, we prefer the linkage name over the name, as
9908 the former contains the exported name, which the user expects
9909 to be able to reference. Ideally, we want the user to be able
9910 to reference this entity using either natural or linkage name,
9911 but we haven't started looking at this enhancement yet. */
9912 const char *linkage_name = dw2_linkage_name (die, cu);
9913
9914 if (linkage_name != NULL)
9915 return linkage_name;
9916 }
9917
9918 /* These are the only languages we know how to qualify names in. */
9919 if (name != NULL
9920 && (lang == language_cplus
9921 || lang == language_fortran || lang == language_d
9922 || lang == language_rust))
9923 {
9924 if (die_needs_namespace (die, cu))
9925 {
9926 const char *prefix;
9927 const char *canonical_name = NULL;
9928
9929 string_file buf;
9930
9931 prefix = determine_prefix (die, cu);
9932 if (*prefix != '\0')
9933 {
9934 gdb::unique_xmalloc_ptr<char> prefixed_name
9935 (typename_concat (NULL, prefix, name, physname, cu));
9936
9937 buf.puts (prefixed_name.get ());
9938 }
9939 else
9940 buf.puts (name);
9941
9942 /* Template parameters may be specified in the DIE's DW_AT_name, or
9943 as children with DW_TAG_template_type_param or
9944 DW_TAG_value_type_param. If the latter, add them to the name
9945 here. If the name already has template parameters, then
9946 skip this step; some versions of GCC emit both, and
9947 it is more efficient to use the pre-computed name.
9948
9949 Something to keep in mind about this process: it is very
9950 unlikely, or in some cases downright impossible, to produce
9951 something that will match the mangled name of a function.
9952 If the definition of the function has the same debug info,
9953 we should be able to match up with it anyway. But fallbacks
9954 using the minimal symbol, for instance to find a method
9955 implemented in a stripped copy of libstdc++, will not work.
9956 If we do not have debug info for the definition, we will have to
9957 match them up some other way.
9958
9959 When we do name matching there is a related problem with function
9960 templates; two instantiated function templates are allowed to
9961 differ only by their return types, which we do not add here. */
9962
9963 if (lang == language_cplus && strchr (name, '<') == NULL)
9964 {
9965 struct attribute *attr;
9966 struct die_info *child;
9967 int first = 1;
9968
9969 die->building_fullname = 1;
9970
9971 for (child = die->child; child != NULL; child = child->sibling)
9972 {
9973 struct type *type;
9974 LONGEST value;
9975 const gdb_byte *bytes;
9976 struct dwarf2_locexpr_baton *baton;
9977 struct value *v;
9978
9979 if (child->tag != DW_TAG_template_type_param
9980 && child->tag != DW_TAG_template_value_param)
9981 continue;
9982
9983 if (first)
9984 {
9985 buf.puts ("<");
9986 first = 0;
9987 }
9988 else
9989 buf.puts (", ");
9990
9991 attr = dwarf2_attr (child, DW_AT_type, cu);
9992 if (attr == NULL)
9993 {
9994 complaint (_("template parameter missing DW_AT_type"));
9995 buf.puts ("UNKNOWN_TYPE");
9996 continue;
9997 }
9998 type = die_type (child, cu);
9999
10000 if (child->tag == DW_TAG_template_type_param)
10001 {
10002 cu->language_defn->print_type (type, "", &buf, -1, 0,
10003 &type_print_raw_options);
10004 continue;
10005 }
10006
10007 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10008 if (attr == NULL)
10009 {
10010 complaint (_("template parameter missing "
10011 "DW_AT_const_value"));
10012 buf.puts ("UNKNOWN_VALUE");
10013 continue;
10014 }
10015
10016 dwarf2_const_value_attr (attr, type, name,
10017 &cu->comp_unit_obstack, cu,
10018 &value, &bytes, &baton);
10019
10020 if (type->has_no_signedness ())
10021 /* GDB prints characters as NUMBER 'CHAR'. If that's
10022 changed, this can use value_print instead. */
10023 cu->language_defn->printchar (value, type, &buf);
10024 else
10025 {
10026 struct value_print_options opts;
10027
10028 if (baton != NULL)
10029 v = dwarf2_evaluate_loc_desc (type, NULL,
10030 baton->data,
10031 baton->size,
10032 baton->per_cu,
10033 baton->per_objfile);
10034 else if (bytes != NULL)
10035 {
10036 v = allocate_value (type);
10037 memcpy (value_contents_writeable (v).data (), bytes,
10038 TYPE_LENGTH (type));
10039 }
10040 else
10041 v = value_from_longest (type, value);
10042
10043 /* Specify decimal so that we do not depend on
10044 the radix. */
10045 get_formatted_print_options (&opts, 'd');
10046 opts.raw = 1;
10047 value_print (v, &buf, &opts);
10048 release_value (v);
10049 }
10050 }
10051
10052 die->building_fullname = 0;
10053
10054 if (!first)
10055 {
10056 /* Close the argument list, with a space if necessary
10057 (nested templates). */
10058 if (!buf.empty () && buf.string ().back () == '>')
10059 buf.puts (" >");
10060 else
10061 buf.puts (">");
10062 }
10063 }
10064
10065 /* For C++ methods, append formal parameter type
10066 information, if PHYSNAME. */
10067
10068 if (physname && die->tag == DW_TAG_subprogram
10069 && lang == language_cplus)
10070 {
10071 struct type *type = read_type_die (die, cu);
10072
10073 c_type_print_args (type, &buf, 1, lang,
10074 &type_print_raw_options);
10075
10076 if (lang == language_cplus)
10077 {
10078 /* Assume that an artificial first parameter is
10079 "this", but do not crash if it is not. RealView
10080 marks unnamed (and thus unused) parameters as
10081 artificial; there is no way to differentiate
10082 the two cases. */
10083 if (type->num_fields () > 0
10084 && TYPE_FIELD_ARTIFICIAL (type, 0)
10085 && type->field (0).type ()->code () == TYPE_CODE_PTR
10086 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10087 buf.puts (" const");
10088 }
10089 }
10090
10091 const std::string &intermediate_name = buf.string ();
10092
10093 if (lang == language_cplus)
10094 canonical_name
10095 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10096 objfile);
10097
10098 /* If we only computed INTERMEDIATE_NAME, or if
10099 INTERMEDIATE_NAME is already canonical, then we need to
10100 intern it. */
10101 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10102 name = objfile->intern (intermediate_name);
10103 else
10104 name = canonical_name;
10105 }
10106 }
10107
10108 return name;
10109 }
10110
10111 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10112 If scope qualifiers are appropriate they will be added. The result
10113 will be allocated on the storage_obstack, or NULL if the DIE does
10114 not have a name. NAME may either be from a previous call to
10115 dwarf2_name or NULL.
10116
10117 The output string will be canonicalized (if C++). */
10118
10119 static const char *
10120 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10121 {
10122 return dwarf2_compute_name (name, die, cu, 0);
10123 }
10124
10125 /* Construct a physname for the given DIE in CU. NAME may either be
10126 from a previous call to dwarf2_name or NULL. The result will be
10127 allocated on the objfile_objstack or NULL if the DIE does not have a
10128 name.
10129
10130 The output string will be canonicalized (if C++). */
10131
10132 static const char *
10133 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10134 {
10135 struct objfile *objfile = cu->per_objfile->objfile;
10136 const char *retval, *mangled = NULL, *canon = NULL;
10137 int need_copy = 1;
10138
10139 /* In this case dwarf2_compute_name is just a shortcut not building anything
10140 on its own. */
10141 if (!die_needs_namespace (die, cu))
10142 return dwarf2_compute_name (name, die, cu, 1);
10143
10144 if (cu->per_cu->lang != language_rust)
10145 mangled = dw2_linkage_name (die, cu);
10146
10147 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10148 has computed. */
10149 gdb::unique_xmalloc_ptr<char> demangled;
10150 if (mangled != NULL)
10151 {
10152 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10153 {
10154 /* Do nothing (do not demangle the symbol name). */
10155 }
10156 else
10157 {
10158 /* Use DMGL_RET_DROP for C++ template functions to suppress
10159 their return type. It is easier for GDB users to search
10160 for such functions as `name(params)' than `long name(params)'.
10161 In such case the minimal symbol names do not match the full
10162 symbol names but for template functions there is never a need
10163 to look up their definition from their declaration so
10164 the only disadvantage remains the minimal symbol variant
10165 `long name(params)' does not have the proper inferior type. */
10166 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
10167 | DMGL_RET_DROP));
10168 }
10169 if (demangled)
10170 canon = demangled.get ();
10171 else
10172 {
10173 canon = mangled;
10174 need_copy = 0;
10175 }
10176 }
10177
10178 if (canon == NULL || check_physname)
10179 {
10180 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10181
10182 if (canon != NULL && strcmp (physname, canon) != 0)
10183 {
10184 /* It may not mean a bug in GDB. The compiler could also
10185 compute DW_AT_linkage_name incorrectly. But in such case
10186 GDB would need to be bug-to-bug compatible. */
10187
10188 complaint (_("Computed physname <%s> does not match demangled <%s> "
10189 "(from linkage <%s>) - DIE at %s [in module %s]"),
10190 physname, canon, mangled, sect_offset_str (die->sect_off),
10191 objfile_name (objfile));
10192
10193 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10194 is available here - over computed PHYSNAME. It is safer
10195 against both buggy GDB and buggy compilers. */
10196
10197 retval = canon;
10198 }
10199 else
10200 {
10201 retval = physname;
10202 need_copy = 0;
10203 }
10204 }
10205 else
10206 retval = canon;
10207
10208 if (need_copy)
10209 retval = objfile->intern (retval);
10210
10211 return retval;
10212 }
10213
10214 /* Inspect DIE in CU for a namespace alias. If one exists, record
10215 a new symbol for it.
10216
10217 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10218
10219 static int
10220 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10221 {
10222 struct attribute *attr;
10223
10224 /* If the die does not have a name, this is not a namespace
10225 alias. */
10226 attr = dwarf2_attr (die, DW_AT_name, cu);
10227 if (attr != NULL)
10228 {
10229 int num;
10230 struct die_info *d = die;
10231 struct dwarf2_cu *imported_cu = cu;
10232
10233 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10234 keep inspecting DIEs until we hit the underlying import. */
10235 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10236 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10237 {
10238 attr = dwarf2_attr (d, DW_AT_import, cu);
10239 if (attr == NULL)
10240 break;
10241
10242 d = follow_die_ref (d, attr, &imported_cu);
10243 if (d->tag != DW_TAG_imported_declaration)
10244 break;
10245 }
10246
10247 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10248 {
10249 complaint (_("DIE at %s has too many recursively imported "
10250 "declarations"), sect_offset_str (d->sect_off));
10251 return 0;
10252 }
10253
10254 if (attr != NULL)
10255 {
10256 struct type *type;
10257 sect_offset sect_off = attr->get_ref_die_offset ();
10258
10259 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10260 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10261 {
10262 /* This declaration is a global namespace alias. Add
10263 a symbol for it whose type is the aliased namespace. */
10264 new_symbol (die, type, cu);
10265 return 1;
10266 }
10267 }
10268 }
10269
10270 return 0;
10271 }
10272
10273 /* Return the using directives repository (global or local?) to use in the
10274 current context for CU.
10275
10276 For Ada, imported declarations can materialize renamings, which *may* be
10277 global. However it is impossible (for now?) in DWARF to distinguish
10278 "external" imported declarations and "static" ones. As all imported
10279 declarations seem to be static in all other languages, make them all CU-wide
10280 global only in Ada. */
10281
10282 static struct using_direct **
10283 using_directives (struct dwarf2_cu *cu)
10284 {
10285 if (cu->per_cu->lang == language_ada
10286 && cu->get_builder ()->outermost_context_p ())
10287 return cu->get_builder ()->get_global_using_directives ();
10288 else
10289 return cu->get_builder ()->get_local_using_directives ();
10290 }
10291
10292 /* Read the import statement specified by the given die and record it. */
10293
10294 static void
10295 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10296 {
10297 struct objfile *objfile = cu->per_objfile->objfile;
10298 struct attribute *import_attr;
10299 struct die_info *imported_die, *child_die;
10300 struct dwarf2_cu *imported_cu;
10301 const char *imported_name;
10302 const char *imported_name_prefix;
10303 const char *canonical_name;
10304 const char *import_alias;
10305 const char *imported_declaration = NULL;
10306 const char *import_prefix;
10307 std::vector<const char *> excludes;
10308
10309 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10310 if (import_attr == NULL)
10311 {
10312 complaint (_("Tag '%s' has no DW_AT_import"),
10313 dwarf_tag_name (die->tag));
10314 return;
10315 }
10316
10317 imported_cu = cu;
10318 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10319 imported_name = dwarf2_name (imported_die, imported_cu);
10320 if (imported_name == NULL)
10321 {
10322 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10323
10324 The import in the following code:
10325 namespace A
10326 {
10327 typedef int B;
10328 }
10329
10330 int main ()
10331 {
10332 using A::B;
10333 B b;
10334 return b;
10335 }
10336
10337 ...
10338 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10339 <52> DW_AT_decl_file : 1
10340 <53> DW_AT_decl_line : 6
10341 <54> DW_AT_import : <0x75>
10342 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10343 <59> DW_AT_name : B
10344 <5b> DW_AT_decl_file : 1
10345 <5c> DW_AT_decl_line : 2
10346 <5d> DW_AT_type : <0x6e>
10347 ...
10348 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10349 <76> DW_AT_byte_size : 4
10350 <77> DW_AT_encoding : 5 (signed)
10351
10352 imports the wrong die ( 0x75 instead of 0x58 ).
10353 This case will be ignored until the gcc bug is fixed. */
10354 return;
10355 }
10356
10357 /* Figure out the local name after import. */
10358 import_alias = dwarf2_name (die, cu);
10359
10360 /* Figure out where the statement is being imported to. */
10361 import_prefix = determine_prefix (die, cu);
10362
10363 /* Figure out what the scope of the imported die is and prepend it
10364 to the name of the imported die. */
10365 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10366
10367 if (imported_die->tag != DW_TAG_namespace
10368 && imported_die->tag != DW_TAG_module)
10369 {
10370 imported_declaration = imported_name;
10371 canonical_name = imported_name_prefix;
10372 }
10373 else if (strlen (imported_name_prefix) > 0)
10374 canonical_name = obconcat (&objfile->objfile_obstack,
10375 imported_name_prefix,
10376 (cu->per_cu->lang == language_d
10377 ? "."
10378 : "::"),
10379 imported_name, (char *) NULL);
10380 else
10381 canonical_name = imported_name;
10382
10383 if (die->tag == DW_TAG_imported_module
10384 && cu->per_cu->lang == language_fortran)
10385 for (child_die = die->child; child_die && child_die->tag;
10386 child_die = child_die->sibling)
10387 {
10388 /* DWARF-4: A Fortran use statement with a “rename list” may be
10389 represented by an imported module entry with an import attribute
10390 referring to the module and owned entries corresponding to those
10391 entities that are renamed as part of being imported. */
10392
10393 if (child_die->tag != DW_TAG_imported_declaration)
10394 {
10395 complaint (_("child DW_TAG_imported_declaration expected "
10396 "- DIE at %s [in module %s]"),
10397 sect_offset_str (child_die->sect_off),
10398 objfile_name (objfile));
10399 continue;
10400 }
10401
10402 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10403 if (import_attr == NULL)
10404 {
10405 complaint (_("Tag '%s' has no DW_AT_import"),
10406 dwarf_tag_name (child_die->tag));
10407 continue;
10408 }
10409
10410 imported_cu = cu;
10411 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10412 &imported_cu);
10413 imported_name = dwarf2_name (imported_die, imported_cu);
10414 if (imported_name == NULL)
10415 {
10416 complaint (_("child DW_TAG_imported_declaration has unknown "
10417 "imported name - DIE at %s [in module %s]"),
10418 sect_offset_str (child_die->sect_off),
10419 objfile_name (objfile));
10420 continue;
10421 }
10422
10423 excludes.push_back (imported_name);
10424
10425 process_die (child_die, cu);
10426 }
10427
10428 add_using_directive (using_directives (cu),
10429 import_prefix,
10430 canonical_name,
10431 import_alias,
10432 imported_declaration,
10433 excludes,
10434 0,
10435 &objfile->objfile_obstack);
10436 }
10437
10438 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10439 types, but gives them a size of zero. Starting with version 14,
10440 ICC is compatible with GCC. */
10441
10442 static bool
10443 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10444 {
10445 if (!cu->checked_producer)
10446 check_producer (cu);
10447
10448 return cu->producer_is_icc_lt_14;
10449 }
10450
10451 /* ICC generates a DW_AT_type for C void functions. This was observed on
10452 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10453 which says that void functions should not have a DW_AT_type. */
10454
10455 static bool
10456 producer_is_icc (struct dwarf2_cu *cu)
10457 {
10458 if (!cu->checked_producer)
10459 check_producer (cu);
10460
10461 return cu->producer_is_icc;
10462 }
10463
10464 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10465 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10466 this, it was first present in GCC release 4.3.0. */
10467
10468 static bool
10469 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10470 {
10471 if (!cu->checked_producer)
10472 check_producer (cu);
10473
10474 return cu->producer_is_gcc_lt_4_3;
10475 }
10476
10477 static file_and_directory &
10478 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10479 {
10480 if (cu->per_cu->fnd != nullptr)
10481 return *cu->per_cu->fnd;
10482
10483 /* Find the filename. Do not use dwarf2_name here, since the filename
10484 is not a source language identifier. */
10485 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
10486 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
10487
10488 if (res.get_comp_dir () == nullptr
10489 && producer_is_gcc_lt_4_3 (cu)
10490 && res.get_name () != nullptr
10491 && IS_ABSOLUTE_PATH (res.get_name ()))
10492 res.set_comp_dir (ldirname (res.get_name ()));
10493
10494 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
10495 return *cu->per_cu->fnd;
10496 }
10497
10498 /* Handle DW_AT_stmt_list for a compilation unit.
10499 DIE is the DW_TAG_compile_unit die for CU.
10500 COMP_DIR is the compilation directory. LOWPC is passed to
10501 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10502
10503 static void
10504 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10505 const file_and_directory &fnd, CORE_ADDR lowpc) /* ARI: editCase function */
10506 {
10507 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10508 struct attribute *attr;
10509 struct line_header line_header_local;
10510 hashval_t line_header_local_hash;
10511 void **slot;
10512 int decode_mapping;
10513
10514 gdb_assert (! cu->per_cu->is_debug_types);
10515
10516 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10517 if (attr == NULL || !attr->form_is_unsigned ())
10518 return;
10519
10520 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10521
10522 /* The line header hash table is only created if needed (it exists to
10523 prevent redundant reading of the line table for partial_units).
10524 If we're given a partial_unit, we'll need it. If we're given a
10525 compile_unit, then use the line header hash table if it's already
10526 created, but don't create one just yet. */
10527
10528 if (per_objfile->line_header_hash == NULL
10529 && die->tag == DW_TAG_partial_unit)
10530 {
10531 per_objfile->line_header_hash
10532 .reset (htab_create_alloc (127, line_header_hash_voidp,
10533 line_header_eq_voidp,
10534 htab_delete_entry<line_header>,
10535 xcalloc, xfree));
10536 }
10537
10538 line_header_local.sect_off = line_offset;
10539 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10540 line_header_local_hash = line_header_hash (&line_header_local);
10541 if (per_objfile->line_header_hash != NULL)
10542 {
10543 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10544 &line_header_local,
10545 line_header_local_hash, NO_INSERT);
10546
10547 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10548 is not present in *SLOT (since if there is something in *SLOT then
10549 it will be for a partial_unit). */
10550 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10551 {
10552 gdb_assert (*slot != NULL);
10553 cu->line_header = (struct line_header *) *slot;
10554 return;
10555 }
10556 }
10557
10558 /* dwarf_decode_line_header does not yet provide sufficient information.
10559 We always have to call also dwarf_decode_lines for it. */
10560 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10561 if (lh == NULL)
10562 return;
10563
10564 cu->line_header = lh.release ();
10565 cu->line_header_die_owner = die;
10566
10567 if (per_objfile->line_header_hash == NULL)
10568 slot = NULL;
10569 else
10570 {
10571 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10572 &line_header_local,
10573 line_header_local_hash, INSERT);
10574 gdb_assert (slot != NULL);
10575 }
10576 if (slot != NULL && *slot == NULL)
10577 {
10578 /* This newly decoded line number information unit will be owned
10579 by line_header_hash hash table. */
10580 *slot = cu->line_header;
10581 cu->line_header_die_owner = NULL;
10582 }
10583 else
10584 {
10585 /* We cannot free any current entry in (*slot) as that struct line_header
10586 may be already used by multiple CUs. Create only temporary decoded
10587 line_header for this CU - it may happen at most once for each line
10588 number information unit. And if we're not using line_header_hash
10589 then this is what we want as well. */
10590 gdb_assert (die->tag != DW_TAG_partial_unit);
10591 }
10592 decode_mapping = (die->tag != DW_TAG_partial_unit);
10593 dwarf_decode_lines (cu->line_header, fnd, cu, nullptr, lowpc,
10594 decode_mapping);
10595
10596 }
10597
10598 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10599
10600 static void
10601 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10602 {
10603 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10604 struct objfile *objfile = per_objfile->objfile;
10605 struct gdbarch *gdbarch = objfile->arch ();
10606 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10607 CORE_ADDR highpc = ((CORE_ADDR) 0);
10608 struct attribute *attr;
10609 struct die_info *child_die;
10610 CORE_ADDR baseaddr;
10611
10612 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
10613 baseaddr = objfile->text_section_offset ();
10614
10615 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10616
10617 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10618 from finish_block. */
10619 if (lowpc == ((CORE_ADDR) -1))
10620 lowpc = highpc;
10621 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10622
10623 file_and_directory &fnd = find_file_and_directory (die, cu);
10624
10625 cu->start_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile), lowpc);
10626
10627 gdb_assert (per_objfile->sym_cu == nullptr);
10628 scoped_restore restore_sym_cu
10629 = make_scoped_restore (&per_objfile->sym_cu, cu);
10630
10631 /* Decode line number information if present. We do this before
10632 processing child DIEs, so that the line header table is available
10633 for DW_AT_decl_file. The PC check is here because, if LOWPC and
10634 HIGHPC are both 0x0, then there won't be any interesting code in
10635 the CU, but a check later on (in
10636 lnp_state_machine::check_line_address) will fail to properly
10637 exclude an entry that was removed via --gc-sections. */
10638 if (lowpc != highpc)
10639 handle_DW_AT_stmt_list (die, cu, fnd, lowpc);
10640
10641 /* Process all dies in compilation unit. */
10642 if (die->child != NULL)
10643 {
10644 child_die = die->child;
10645 while (child_die && child_die->tag)
10646 {
10647 process_die (child_die, cu);
10648 child_die = child_die->sibling;
10649 }
10650 }
10651 per_objfile->sym_cu = nullptr;
10652
10653 /* Decode macro information, if present. Dwarf 2 macro information
10654 refers to information in the line number info statement program
10655 header, so we can only read it if we've read the header
10656 successfully. */
10657 attr = dwarf2_attr (die, DW_AT_macros, cu);
10658 if (attr == NULL)
10659 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10660 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10661 {
10662 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10663 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10664
10665 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10666 }
10667 else
10668 {
10669 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10670 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10671 {
10672 unsigned int macro_offset = attr->as_unsigned ();
10673
10674 dwarf_decode_macros (cu, macro_offset, 0);
10675 }
10676 }
10677 }
10678
10679 void
10680 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10681 {
10682 struct type_unit_group *tu_group;
10683 int first_time;
10684 struct attribute *attr;
10685 unsigned int i;
10686 struct signatured_type *sig_type;
10687
10688 gdb_assert (per_cu->is_debug_types);
10689 sig_type = (struct signatured_type *) per_cu;
10690
10691 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10692
10693 /* If we're using .gdb_index (includes -readnow) then
10694 per_cu->type_unit_group may not have been set up yet. */
10695 if (sig_type->type_unit_group == NULL)
10696 sig_type->type_unit_group = get_type_unit_group (this, attr);
10697 tu_group = sig_type->type_unit_group;
10698
10699 /* If we've already processed this stmt_list there's no real need to
10700 do it again, we could fake it and just recreate the part we need
10701 (file name,index -> symtab mapping). If data shows this optimization
10702 is useful we can do it then. */
10703 type_unit_group_unshareable *tug_unshare
10704 = per_objfile->get_type_unit_group_unshareable (tu_group);
10705 first_time = tug_unshare->compunit_symtab == NULL;
10706
10707 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10708 debug info. */
10709 line_header_up lh;
10710 if (attr != NULL && attr->form_is_unsigned ())
10711 {
10712 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10713 lh = dwarf_decode_line_header (line_offset, this);
10714 }
10715 if (lh == NULL)
10716 {
10717 if (first_time)
10718 start_symtab ("", NULL, 0);
10719 else
10720 {
10721 gdb_assert (tug_unshare->symtabs == NULL);
10722 gdb_assert (m_builder == nullptr);
10723 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10724 m_builder.reset (new struct buildsym_compunit
10725 (cust->objfile (), "",
10726 cust->dirname (),
10727 compunit_language (cust),
10728 0, cust));
10729 list_in_scope = get_builder ()->get_file_symbols ();
10730 }
10731 return;
10732 }
10733
10734 line_header = lh.release ();
10735 line_header_die_owner = die;
10736
10737 if (first_time)
10738 {
10739 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10740
10741 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10742 still initializing it, and our caller (a few levels up)
10743 process_full_type_unit still needs to know if this is the first
10744 time. */
10745
10746 tug_unshare->symtabs
10747 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
10748 struct symtab *, line_header->file_names_size ());
10749
10750 auto &file_names = line_header->file_names ();
10751 for (i = 0; i < file_names.size (); ++i)
10752 {
10753 file_entry &fe = file_names[i];
10754 dwarf2_start_subfile (this, fe.name,
10755 fe.include_dir (line_header));
10756 buildsym_compunit *b = get_builder ();
10757 if (b->get_current_subfile ()->symtab == NULL)
10758 {
10759 /* NOTE: start_subfile will recognize when it's been
10760 passed a file it has already seen. So we can't
10761 assume there's a simple mapping from
10762 cu->line_header->file_names to subfiles, plus
10763 cu->line_header->file_names may contain dups. */
10764 b->get_current_subfile ()->symtab
10765 = allocate_symtab (cust, b->get_current_subfile ()->name);
10766 }
10767
10768 fe.symtab = b->get_current_subfile ()->symtab;
10769 tug_unshare->symtabs[i] = fe.symtab;
10770 }
10771 }
10772 else
10773 {
10774 gdb_assert (m_builder == nullptr);
10775 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10776 m_builder.reset (new struct buildsym_compunit
10777 (cust->objfile (), "",
10778 cust->dirname (),
10779 compunit_language (cust),
10780 0, cust));
10781 list_in_scope = get_builder ()->get_file_symbols ();
10782
10783 auto &file_names = line_header->file_names ();
10784 for (i = 0; i < file_names.size (); ++i)
10785 {
10786 file_entry &fe = file_names[i];
10787 fe.symtab = tug_unshare->symtabs[i];
10788 }
10789 }
10790
10791 /* The main symtab is allocated last. Type units don't have DW_AT_name
10792 so they don't have a "real" (so to speak) symtab anyway.
10793 There is later code that will assign the main symtab to all symbols
10794 that don't have one. We need to handle the case of a symbol with a
10795 missing symtab (DW_AT_decl_file) anyway. */
10796 }
10797
10798 /* Process DW_TAG_type_unit.
10799 For TUs we want to skip the first top level sibling if it's not the
10800 actual type being defined by this TU. In this case the first top
10801 level sibling is there to provide context only. */
10802
10803 static void
10804 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10805 {
10806 struct die_info *child_die;
10807
10808 prepare_one_comp_unit (cu, die, language_minimal);
10809
10810 /* Initialize (or reinitialize) the machinery for building symtabs.
10811 We do this before processing child DIEs, so that the line header table
10812 is available for DW_AT_decl_file. */
10813 cu->setup_type_unit_groups (die);
10814
10815 if (die->child != NULL)
10816 {
10817 child_die = die->child;
10818 while (child_die && child_die->tag)
10819 {
10820 process_die (child_die, cu);
10821 child_die = child_die->sibling;
10822 }
10823 }
10824 }
10825 \f
10826 /* DWO/DWP files.
10827
10828 http://gcc.gnu.org/wiki/DebugFission
10829 http://gcc.gnu.org/wiki/DebugFissionDWP
10830
10831 To simplify handling of both DWO files ("object" files with the DWARF info)
10832 and DWP files (a file with the DWOs packaged up into one file), we treat
10833 DWP files as having a collection of virtual DWO files. */
10834
10835 static hashval_t
10836 hash_dwo_file (const void *item)
10837 {
10838 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10839 hashval_t hash;
10840
10841 hash = htab_hash_string (dwo_file->dwo_name);
10842 if (dwo_file->comp_dir != NULL)
10843 hash += htab_hash_string (dwo_file->comp_dir);
10844 return hash;
10845 }
10846
10847 static int
10848 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10849 {
10850 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10851 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10852
10853 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10854 return 0;
10855 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10856 return lhs->comp_dir == rhs->comp_dir;
10857 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10858 }
10859
10860 /* Allocate a hash table for DWO files. */
10861
10862 static htab_up
10863 allocate_dwo_file_hash_table ()
10864 {
10865 return htab_up (htab_create_alloc (41,
10866 hash_dwo_file,
10867 eq_dwo_file,
10868 htab_delete_entry<dwo_file>,
10869 xcalloc, xfree));
10870 }
10871
10872 /* Lookup DWO file DWO_NAME. */
10873
10874 static void **
10875 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10876 const char *dwo_name,
10877 const char *comp_dir)
10878 {
10879 struct dwo_file find_entry;
10880 void **slot;
10881
10882 if (per_objfile->per_bfd->dwo_files == NULL)
10883 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10884
10885 find_entry.dwo_name = dwo_name;
10886 find_entry.comp_dir = comp_dir;
10887 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10888 INSERT);
10889
10890 return slot;
10891 }
10892
10893 static hashval_t
10894 hash_dwo_unit (const void *item)
10895 {
10896 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10897
10898 /* This drops the top 32 bits of the id, but is ok for a hash. */
10899 return dwo_unit->signature;
10900 }
10901
10902 static int
10903 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10904 {
10905 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10906 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10907
10908 /* The signature is assumed to be unique within the DWO file.
10909 So while object file CU dwo_id's always have the value zero,
10910 that's OK, assuming each object file DWO file has only one CU,
10911 and that's the rule for now. */
10912 return lhs->signature == rhs->signature;
10913 }
10914
10915 /* Allocate a hash table for DWO CUs,TUs.
10916 There is one of these tables for each of CUs,TUs for each DWO file. */
10917
10918 static htab_up
10919 allocate_dwo_unit_table ()
10920 {
10921 /* Start out with a pretty small number.
10922 Generally DWO files contain only one CU and maybe some TUs. */
10923 return htab_up (htab_create_alloc (3,
10924 hash_dwo_unit,
10925 eq_dwo_unit,
10926 NULL, xcalloc, xfree));
10927 }
10928
10929 /* die_reader_func for create_dwo_cu. */
10930
10931 static void
10932 create_dwo_cu_reader (const struct die_reader_specs *reader,
10933 const gdb_byte *info_ptr,
10934 struct die_info *comp_unit_die,
10935 struct dwo_file *dwo_file,
10936 struct dwo_unit *dwo_unit)
10937 {
10938 struct dwarf2_cu *cu = reader->cu;
10939 sect_offset sect_off = cu->per_cu->sect_off;
10940 struct dwarf2_section_info *section = cu->per_cu->section;
10941
10942 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10943 if (!signature.has_value ())
10944 {
10945 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10946 " its dwo_id [in module %s]"),
10947 sect_offset_str (sect_off), dwo_file->dwo_name);
10948 return;
10949 }
10950
10951 dwo_unit->dwo_file = dwo_file;
10952 dwo_unit->signature = *signature;
10953 dwo_unit->section = section;
10954 dwo_unit->sect_off = sect_off;
10955 dwo_unit->length = cu->per_cu->length;
10956
10957 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10958 sect_offset_str (sect_off),
10959 hex_string (dwo_unit->signature));
10960 }
10961
10962 /* Create the dwo_units for the CUs in a DWO_FILE.
10963 Note: This function processes DWO files only, not DWP files. */
10964
10965 static void
10966 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10967 dwarf2_cu *cu, struct dwo_file &dwo_file,
10968 dwarf2_section_info &section, htab_up &cus_htab)
10969 {
10970 struct objfile *objfile = per_objfile->objfile;
10971 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10972 const gdb_byte *info_ptr, *end_ptr;
10973
10974 section.read (objfile);
10975 info_ptr = section.buffer;
10976
10977 if (info_ptr == NULL)
10978 return;
10979
10980 dwarf_read_debug_printf ("Reading %s for %s:",
10981 section.get_name (),
10982 section.get_file_name ());
10983
10984 end_ptr = info_ptr + section.size;
10985 while (info_ptr < end_ptr)
10986 {
10987 struct dwarf2_per_cu_data per_cu;
10988 struct dwo_unit read_unit {};
10989 struct dwo_unit *dwo_unit;
10990 void **slot;
10991 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10992
10993 per_cu.per_bfd = per_bfd;
10994 per_cu.is_debug_types = 0;
10995 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10996 per_cu.section = &section;
10997
10998 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
10999 if (!reader.dummy_p)
11000 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11001 &dwo_file, &read_unit);
11002 info_ptr += per_cu.length;
11003
11004 // If the unit could not be parsed, skip it.
11005 if (read_unit.dwo_file == NULL)
11006 continue;
11007
11008 if (cus_htab == NULL)
11009 cus_htab = allocate_dwo_unit_table ();
11010
11011 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11012 struct dwo_unit);
11013 *dwo_unit = read_unit;
11014 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11015 gdb_assert (slot != NULL);
11016 if (*slot != NULL)
11017 {
11018 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11019 sect_offset dup_sect_off = dup_cu->sect_off;
11020
11021 complaint (_("debug cu entry at offset %s is duplicate to"
11022 " the entry at offset %s, signature %s"),
11023 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11024 hex_string (dwo_unit->signature));
11025 }
11026 *slot = (void *)dwo_unit;
11027 }
11028 }
11029
11030 /* DWP file .debug_{cu,tu}_index section format:
11031 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11032 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11033
11034 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11035 officially standard DWP format was published with DWARF v5 and is called
11036 Version 5. There are no versions 3 or 4.
11037
11038 DWP Version 1:
11039
11040 Both index sections have the same format, and serve to map a 64-bit
11041 signature to a set of section numbers. Each section begins with a header,
11042 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11043 indexes, and a pool of 32-bit section numbers. The index sections will be
11044 aligned at 8-byte boundaries in the file.
11045
11046 The index section header consists of:
11047
11048 V, 32 bit version number
11049 -, 32 bits unused
11050 N, 32 bit number of compilation units or type units in the index
11051 M, 32 bit number of slots in the hash table
11052
11053 Numbers are recorded using the byte order of the application binary.
11054
11055 The hash table begins at offset 16 in the section, and consists of an array
11056 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11057 order of the application binary). Unused slots in the hash table are 0.
11058 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11059
11060 The parallel table begins immediately after the hash table
11061 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11062 array of 32-bit indexes (using the byte order of the application binary),
11063 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11064 table contains a 32-bit index into the pool of section numbers. For unused
11065 hash table slots, the corresponding entry in the parallel table will be 0.
11066
11067 The pool of section numbers begins immediately following the hash table
11068 (at offset 16 + 12 * M from the beginning of the section). The pool of
11069 section numbers consists of an array of 32-bit words (using the byte order
11070 of the application binary). Each item in the array is indexed starting
11071 from 0. The hash table entry provides the index of the first section
11072 number in the set. Additional section numbers in the set follow, and the
11073 set is terminated by a 0 entry (section number 0 is not used in ELF).
11074
11075 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11076 section must be the first entry in the set, and the .debug_abbrev.dwo must
11077 be the second entry. Other members of the set may follow in any order.
11078
11079 ---
11080
11081 DWP Versions 2 and 5:
11082
11083 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11084 and the entries in the index tables are now offsets into these sections.
11085 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11086 section.
11087
11088 Index Section Contents:
11089 Header
11090 Hash Table of Signatures dwp_hash_table.hash_table
11091 Parallel Table of Indices dwp_hash_table.unit_table
11092 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11093 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11094
11095 The index section header consists of:
11096
11097 V, 32 bit version number
11098 L, 32 bit number of columns in the table of section offsets
11099 N, 32 bit number of compilation units or type units in the index
11100 M, 32 bit number of slots in the hash table
11101
11102 Numbers are recorded using the byte order of the application binary.
11103
11104 The hash table has the same format as version 1.
11105 The parallel table of indices has the same format as version 1,
11106 except that the entries are origin-1 indices into the table of sections
11107 offsets and the table of section sizes.
11108
11109 The table of offsets begins immediately following the parallel table
11110 (at offset 16 + 12 * M from the beginning of the section). The table is
11111 a two-dimensional array of 32-bit words (using the byte order of the
11112 application binary), with L columns and N+1 rows, in row-major order.
11113 Each row in the array is indexed starting from 0. The first row provides
11114 a key to the remaining rows: each column in this row provides an identifier
11115 for a debug section, and the offsets in the same column of subsequent rows
11116 refer to that section. The section identifiers for Version 2 are:
11117
11118 DW_SECT_INFO 1 .debug_info.dwo
11119 DW_SECT_TYPES 2 .debug_types.dwo
11120 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11121 DW_SECT_LINE 4 .debug_line.dwo
11122 DW_SECT_LOC 5 .debug_loc.dwo
11123 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11124 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11125 DW_SECT_MACRO 8 .debug_macro.dwo
11126
11127 The section identifiers for Version 5 are:
11128
11129 DW_SECT_INFO_V5 1 .debug_info.dwo
11130 DW_SECT_RESERVED_V5 2 --
11131 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11132 DW_SECT_LINE_V5 4 .debug_line.dwo
11133 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11134 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11135 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11136 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11137
11138 The offsets provided by the CU and TU index sections are the base offsets
11139 for the contributions made by each CU or TU to the corresponding section
11140 in the package file. Each CU and TU header contains an abbrev_offset
11141 field, used to find the abbreviations table for that CU or TU within the
11142 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11143 be interpreted as relative to the base offset given in the index section.
11144 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11145 should be interpreted as relative to the base offset for .debug_line.dwo,
11146 and offsets into other debug sections obtained from DWARF attributes should
11147 also be interpreted as relative to the corresponding base offset.
11148
11149 The table of sizes begins immediately following the table of offsets.
11150 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11151 with L columns and N rows, in row-major order. Each row in the array is
11152 indexed starting from 1 (row 0 is shared by the two tables).
11153
11154 ---
11155
11156 Hash table lookup is handled the same in version 1 and 2:
11157
11158 We assume that N and M will not exceed 2^32 - 1.
11159 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11160
11161 Given a 64-bit compilation unit signature or a type signature S, an entry
11162 in the hash table is located as follows:
11163
11164 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11165 the low-order k bits all set to 1.
11166
11167 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11168
11169 3) If the hash table entry at index H matches the signature, use that
11170 entry. If the hash table entry at index H is unused (all zeroes),
11171 terminate the search: the signature is not present in the table.
11172
11173 4) Let H = (H + H') modulo M. Repeat at Step 3.
11174
11175 Because M > N and H' and M are relatively prime, the search is guaranteed
11176 to stop at an unused slot or find the match. */
11177
11178 /* Create a hash table to map DWO IDs to their CU/TU entry in
11179 .debug_{info,types}.dwo in DWP_FILE.
11180 Returns NULL if there isn't one.
11181 Note: This function processes DWP files only, not DWO files. */
11182
11183 static struct dwp_hash_table *
11184 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11185 struct dwp_file *dwp_file, int is_debug_types)
11186 {
11187 struct objfile *objfile = per_objfile->objfile;
11188 bfd *dbfd = dwp_file->dbfd.get ();
11189 const gdb_byte *index_ptr, *index_end;
11190 struct dwarf2_section_info *index;
11191 uint32_t version, nr_columns, nr_units, nr_slots;
11192 struct dwp_hash_table *htab;
11193
11194 if (is_debug_types)
11195 index = &dwp_file->sections.tu_index;
11196 else
11197 index = &dwp_file->sections.cu_index;
11198
11199 if (index->empty ())
11200 return NULL;
11201 index->read (objfile);
11202
11203 index_ptr = index->buffer;
11204 index_end = index_ptr + index->size;
11205
11206 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11207 For now it's safe to just read 4 bytes (particularly as it's difficult to
11208 tell if you're dealing with Version 5 before you've read the version). */
11209 version = read_4_bytes (dbfd, index_ptr);
11210 index_ptr += 4;
11211 if (version == 2 || version == 5)
11212 nr_columns = read_4_bytes (dbfd, index_ptr);
11213 else
11214 nr_columns = 0;
11215 index_ptr += 4;
11216 nr_units = read_4_bytes (dbfd, index_ptr);
11217 index_ptr += 4;
11218 nr_slots = read_4_bytes (dbfd, index_ptr);
11219 index_ptr += 4;
11220
11221 if (version != 1 && version != 2 && version != 5)
11222 {
11223 error (_("Dwarf Error: unsupported DWP file version (%s)"
11224 " [in module %s]"),
11225 pulongest (version), dwp_file->name);
11226 }
11227 if (nr_slots != (nr_slots & -nr_slots))
11228 {
11229 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11230 " is not power of 2 [in module %s]"),
11231 pulongest (nr_slots), dwp_file->name);
11232 }
11233
11234 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11235 htab->version = version;
11236 htab->nr_columns = nr_columns;
11237 htab->nr_units = nr_units;
11238 htab->nr_slots = nr_slots;
11239 htab->hash_table = index_ptr;
11240 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11241
11242 /* Exit early if the table is empty. */
11243 if (nr_slots == 0 || nr_units == 0
11244 || (version == 2 && nr_columns == 0)
11245 || (version == 5 && nr_columns == 0))
11246 {
11247 /* All must be zero. */
11248 if (nr_slots != 0 || nr_units != 0
11249 || (version == 2 && nr_columns != 0)
11250 || (version == 5 && nr_columns != 0))
11251 {
11252 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11253 " all zero [in modules %s]"),
11254 dwp_file->name);
11255 }
11256 return htab;
11257 }
11258
11259 if (version == 1)
11260 {
11261 htab->section_pool.v1.indices =
11262 htab->unit_table + sizeof (uint32_t) * nr_slots;
11263 /* It's harder to decide whether the section is too small in v1.
11264 V1 is deprecated anyway so we punt. */
11265 }
11266 else if (version == 2)
11267 {
11268 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11269 int *ids = htab->section_pool.v2.section_ids;
11270 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11271 /* Reverse map for error checking. */
11272 int ids_seen[DW_SECT_MAX + 1];
11273 int i;
11274
11275 if (nr_columns < 2)
11276 {
11277 error (_("Dwarf Error: bad DWP hash table, too few columns"
11278 " in section table [in module %s]"),
11279 dwp_file->name);
11280 }
11281 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11282 {
11283 error (_("Dwarf Error: bad DWP hash table, too many columns"
11284 " in section table [in module %s]"),
11285 dwp_file->name);
11286 }
11287 memset (ids, 255, sizeof_ids);
11288 memset (ids_seen, 255, sizeof (ids_seen));
11289 for (i = 0; i < nr_columns; ++i)
11290 {
11291 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11292
11293 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11294 {
11295 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11296 " in section table [in module %s]"),
11297 id, dwp_file->name);
11298 }
11299 if (ids_seen[id] != -1)
11300 {
11301 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11302 " id %d in section table [in module %s]"),
11303 id, dwp_file->name);
11304 }
11305 ids_seen[id] = i;
11306 ids[i] = id;
11307 }
11308 /* Must have exactly one info or types section. */
11309 if (((ids_seen[DW_SECT_INFO] != -1)
11310 + (ids_seen[DW_SECT_TYPES] != -1))
11311 != 1)
11312 {
11313 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11314 " DWO info/types section [in module %s]"),
11315 dwp_file->name);
11316 }
11317 /* Must have an abbrev section. */
11318 if (ids_seen[DW_SECT_ABBREV] == -1)
11319 {
11320 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11321 " section [in module %s]"),
11322 dwp_file->name);
11323 }
11324 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11325 htab->section_pool.v2.sizes =
11326 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11327 * nr_units * nr_columns);
11328 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11329 * nr_units * nr_columns))
11330 > index_end)
11331 {
11332 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11333 " [in module %s]"),
11334 dwp_file->name);
11335 }
11336 }
11337 else /* version == 5 */
11338 {
11339 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11340 int *ids = htab->section_pool.v5.section_ids;
11341 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11342 /* Reverse map for error checking. */
11343 int ids_seen[DW_SECT_MAX_V5 + 1];
11344
11345 if (nr_columns < 2)
11346 {
11347 error (_("Dwarf Error: bad DWP hash table, too few columns"
11348 " in section table [in module %s]"),
11349 dwp_file->name);
11350 }
11351 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11352 {
11353 error (_("Dwarf Error: bad DWP hash table, too many columns"
11354 " in section table [in module %s]"),
11355 dwp_file->name);
11356 }
11357 memset (ids, 255, sizeof_ids);
11358 memset (ids_seen, 255, sizeof (ids_seen));
11359 for (int i = 0; i < nr_columns; ++i)
11360 {
11361 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11362
11363 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11364 {
11365 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11366 " in section table [in module %s]"),
11367 id, dwp_file->name);
11368 }
11369 if (ids_seen[id] != -1)
11370 {
11371 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11372 " id %d in section table [in module %s]"),
11373 id, dwp_file->name);
11374 }
11375 ids_seen[id] = i;
11376 ids[i] = id;
11377 }
11378 /* Must have seen an info section. */
11379 if (ids_seen[DW_SECT_INFO_V5] == -1)
11380 {
11381 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11382 " DWO info/types section [in module %s]"),
11383 dwp_file->name);
11384 }
11385 /* Must have an abbrev section. */
11386 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11387 {
11388 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11389 " section [in module %s]"),
11390 dwp_file->name);
11391 }
11392 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11393 htab->section_pool.v5.sizes
11394 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11395 * nr_units * nr_columns);
11396 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11397 * nr_units * nr_columns))
11398 > index_end)
11399 {
11400 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11401 " [in module %s]"),
11402 dwp_file->name);
11403 }
11404 }
11405
11406 return htab;
11407 }
11408
11409 /* Update SECTIONS with the data from SECTP.
11410
11411 This function is like the other "locate" section routines, but in
11412 this context the sections to read comes from the DWP V1 hash table,
11413 not the full ELF section table.
11414
11415 The result is non-zero for success, or zero if an error was found. */
11416
11417 static int
11418 locate_v1_virtual_dwo_sections (asection *sectp,
11419 struct virtual_v1_dwo_sections *sections)
11420 {
11421 const struct dwop_section_names *names = &dwop_section_names;
11422
11423 if (names->abbrev_dwo.matches (sectp->name))
11424 {
11425 /* There can be only one. */
11426 if (sections->abbrev.s.section != NULL)
11427 return 0;
11428 sections->abbrev.s.section = sectp;
11429 sections->abbrev.size = bfd_section_size (sectp);
11430 }
11431 else if (names->info_dwo.matches (sectp->name)
11432 || names->types_dwo.matches (sectp->name))
11433 {
11434 /* There can be only one. */
11435 if (sections->info_or_types.s.section != NULL)
11436 return 0;
11437 sections->info_or_types.s.section = sectp;
11438 sections->info_or_types.size = bfd_section_size (sectp);
11439 }
11440 else if (names->line_dwo.matches (sectp->name))
11441 {
11442 /* There can be only one. */
11443 if (sections->line.s.section != NULL)
11444 return 0;
11445 sections->line.s.section = sectp;
11446 sections->line.size = bfd_section_size (sectp);
11447 }
11448 else if (names->loc_dwo.matches (sectp->name))
11449 {
11450 /* There can be only one. */
11451 if (sections->loc.s.section != NULL)
11452 return 0;
11453 sections->loc.s.section = sectp;
11454 sections->loc.size = bfd_section_size (sectp);
11455 }
11456 else if (names->macinfo_dwo.matches (sectp->name))
11457 {
11458 /* There can be only one. */
11459 if (sections->macinfo.s.section != NULL)
11460 return 0;
11461 sections->macinfo.s.section = sectp;
11462 sections->macinfo.size = bfd_section_size (sectp);
11463 }
11464 else if (names->macro_dwo.matches (sectp->name))
11465 {
11466 /* There can be only one. */
11467 if (sections->macro.s.section != NULL)
11468 return 0;
11469 sections->macro.s.section = sectp;
11470 sections->macro.size = bfd_section_size (sectp);
11471 }
11472 else if (names->str_offsets_dwo.matches (sectp->name))
11473 {
11474 /* There can be only one. */
11475 if (sections->str_offsets.s.section != NULL)
11476 return 0;
11477 sections->str_offsets.s.section = sectp;
11478 sections->str_offsets.size = bfd_section_size (sectp);
11479 }
11480 else
11481 {
11482 /* No other kind of section is valid. */
11483 return 0;
11484 }
11485
11486 return 1;
11487 }
11488
11489 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11490 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11491 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11492 This is for DWP version 1 files. */
11493
11494 static struct dwo_unit *
11495 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11496 struct dwp_file *dwp_file,
11497 uint32_t unit_index,
11498 const char *comp_dir,
11499 ULONGEST signature, int is_debug_types)
11500 {
11501 const struct dwp_hash_table *dwp_htab =
11502 is_debug_types ? dwp_file->tus : dwp_file->cus;
11503 bfd *dbfd = dwp_file->dbfd.get ();
11504 const char *kind = is_debug_types ? "TU" : "CU";
11505 struct dwo_file *dwo_file;
11506 struct dwo_unit *dwo_unit;
11507 struct virtual_v1_dwo_sections sections;
11508 void **dwo_file_slot;
11509 int i;
11510
11511 gdb_assert (dwp_file->version == 1);
11512
11513 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11514 kind, pulongest (unit_index), hex_string (signature),
11515 dwp_file->name);
11516
11517 /* Fetch the sections of this DWO unit.
11518 Put a limit on the number of sections we look for so that bad data
11519 doesn't cause us to loop forever. */
11520
11521 #define MAX_NR_V1_DWO_SECTIONS \
11522 (1 /* .debug_info or .debug_types */ \
11523 + 1 /* .debug_abbrev */ \
11524 + 1 /* .debug_line */ \
11525 + 1 /* .debug_loc */ \
11526 + 1 /* .debug_str_offsets */ \
11527 + 1 /* .debug_macro or .debug_macinfo */ \
11528 + 1 /* trailing zero */)
11529
11530 memset (&sections, 0, sizeof (sections));
11531
11532 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11533 {
11534 asection *sectp;
11535 uint32_t section_nr =
11536 read_4_bytes (dbfd,
11537 dwp_htab->section_pool.v1.indices
11538 + (unit_index + i) * sizeof (uint32_t));
11539
11540 if (section_nr == 0)
11541 break;
11542 if (section_nr >= dwp_file->num_sections)
11543 {
11544 error (_("Dwarf Error: bad DWP hash table, section number too large"
11545 " [in module %s]"),
11546 dwp_file->name);
11547 }
11548
11549 sectp = dwp_file->elf_sections[section_nr];
11550 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11551 {
11552 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11553 " [in module %s]"),
11554 dwp_file->name);
11555 }
11556 }
11557
11558 if (i < 2
11559 || sections.info_or_types.empty ()
11560 || sections.abbrev.empty ())
11561 {
11562 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11563 " [in module %s]"),
11564 dwp_file->name);
11565 }
11566 if (i == MAX_NR_V1_DWO_SECTIONS)
11567 {
11568 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11569 " [in module %s]"),
11570 dwp_file->name);
11571 }
11572
11573 /* It's easier for the rest of the code if we fake a struct dwo_file and
11574 have dwo_unit "live" in that. At least for now.
11575
11576 The DWP file can be made up of a random collection of CUs and TUs.
11577 However, for each CU + set of TUs that came from the same original DWO
11578 file, we can combine them back into a virtual DWO file to save space
11579 (fewer struct dwo_file objects to allocate). Remember that for really
11580 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11581
11582 std::string virtual_dwo_name =
11583 string_printf ("virtual-dwo/%d-%d-%d-%d",
11584 sections.abbrev.get_id (),
11585 sections.line.get_id (),
11586 sections.loc.get_id (),
11587 sections.str_offsets.get_id ());
11588 /* Can we use an existing virtual DWO file? */
11589 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11590 comp_dir);
11591 /* Create one if necessary. */
11592 if (*dwo_file_slot == NULL)
11593 {
11594 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11595 virtual_dwo_name.c_str ());
11596
11597 dwo_file = new struct dwo_file;
11598 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11599 dwo_file->comp_dir = comp_dir;
11600 dwo_file->sections.abbrev = sections.abbrev;
11601 dwo_file->sections.line = sections.line;
11602 dwo_file->sections.loc = sections.loc;
11603 dwo_file->sections.macinfo = sections.macinfo;
11604 dwo_file->sections.macro = sections.macro;
11605 dwo_file->sections.str_offsets = sections.str_offsets;
11606 /* The "str" section is global to the entire DWP file. */
11607 dwo_file->sections.str = dwp_file->sections.str;
11608 /* The info or types section is assigned below to dwo_unit,
11609 there's no need to record it in dwo_file.
11610 Also, we can't simply record type sections in dwo_file because
11611 we record a pointer into the vector in dwo_unit. As we collect more
11612 types we'll grow the vector and eventually have to reallocate space
11613 for it, invalidating all copies of pointers into the previous
11614 contents. */
11615 *dwo_file_slot = dwo_file;
11616 }
11617 else
11618 {
11619 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11620 virtual_dwo_name.c_str ());
11621
11622 dwo_file = (struct dwo_file *) *dwo_file_slot;
11623 }
11624
11625 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11626 dwo_unit->dwo_file = dwo_file;
11627 dwo_unit->signature = signature;
11628 dwo_unit->section =
11629 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11630 *dwo_unit->section = sections.info_or_types;
11631 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11632
11633 return dwo_unit;
11634 }
11635
11636 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11637 simplify them. Given a pointer to the containing section SECTION, and
11638 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11639 virtual section of just that piece. */
11640
11641 static struct dwarf2_section_info
11642 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11643 struct dwarf2_section_info *section,
11644 bfd_size_type offset, bfd_size_type size)
11645 {
11646 struct dwarf2_section_info result;
11647 asection *sectp;
11648
11649 gdb_assert (section != NULL);
11650 gdb_assert (!section->is_virtual);
11651
11652 memset (&result, 0, sizeof (result));
11653 result.s.containing_section = section;
11654 result.is_virtual = true;
11655
11656 if (size == 0)
11657 return result;
11658
11659 sectp = section->get_bfd_section ();
11660
11661 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11662 bounds of the real section. This is a pretty-rare event, so just
11663 flag an error (easier) instead of a warning and trying to cope. */
11664 if (sectp == NULL
11665 || offset + size > bfd_section_size (sectp))
11666 {
11667 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11668 " in section %s [in module %s]"),
11669 sectp ? bfd_section_name (sectp) : "<unknown>",
11670 objfile_name (per_objfile->objfile));
11671 }
11672
11673 result.virtual_offset = offset;
11674 result.size = size;
11675 return result;
11676 }
11677
11678 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11679 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11680 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11681 This is for DWP version 2 files. */
11682
11683 static struct dwo_unit *
11684 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11685 struct dwp_file *dwp_file,
11686 uint32_t unit_index,
11687 const char *comp_dir,
11688 ULONGEST signature, int is_debug_types)
11689 {
11690 const struct dwp_hash_table *dwp_htab =
11691 is_debug_types ? dwp_file->tus : dwp_file->cus;
11692 bfd *dbfd = dwp_file->dbfd.get ();
11693 const char *kind = is_debug_types ? "TU" : "CU";
11694 struct dwo_file *dwo_file;
11695 struct dwo_unit *dwo_unit;
11696 struct virtual_v2_or_v5_dwo_sections sections;
11697 void **dwo_file_slot;
11698 int i;
11699
11700 gdb_assert (dwp_file->version == 2);
11701
11702 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11703 kind, pulongest (unit_index), hex_string (signature),
11704 dwp_file->name);
11705
11706 /* Fetch the section offsets of this DWO unit. */
11707
11708 memset (&sections, 0, sizeof (sections));
11709
11710 for (i = 0; i < dwp_htab->nr_columns; ++i)
11711 {
11712 uint32_t offset = read_4_bytes (dbfd,
11713 dwp_htab->section_pool.v2.offsets
11714 + (((unit_index - 1) * dwp_htab->nr_columns
11715 + i)
11716 * sizeof (uint32_t)));
11717 uint32_t size = read_4_bytes (dbfd,
11718 dwp_htab->section_pool.v2.sizes
11719 + (((unit_index - 1) * dwp_htab->nr_columns
11720 + i)
11721 * sizeof (uint32_t)));
11722
11723 switch (dwp_htab->section_pool.v2.section_ids[i])
11724 {
11725 case DW_SECT_INFO:
11726 case DW_SECT_TYPES:
11727 sections.info_or_types_offset = offset;
11728 sections.info_or_types_size = size;
11729 break;
11730 case DW_SECT_ABBREV:
11731 sections.abbrev_offset = offset;
11732 sections.abbrev_size = size;
11733 break;
11734 case DW_SECT_LINE:
11735 sections.line_offset = offset;
11736 sections.line_size = size;
11737 break;
11738 case DW_SECT_LOC:
11739 sections.loc_offset = offset;
11740 sections.loc_size = size;
11741 break;
11742 case DW_SECT_STR_OFFSETS:
11743 sections.str_offsets_offset = offset;
11744 sections.str_offsets_size = size;
11745 break;
11746 case DW_SECT_MACINFO:
11747 sections.macinfo_offset = offset;
11748 sections.macinfo_size = size;
11749 break;
11750 case DW_SECT_MACRO:
11751 sections.macro_offset = offset;
11752 sections.macro_size = size;
11753 break;
11754 }
11755 }
11756
11757 /* It's easier for the rest of the code if we fake a struct dwo_file and
11758 have dwo_unit "live" in that. At least for now.
11759
11760 The DWP file can be made up of a random collection of CUs and TUs.
11761 However, for each CU + set of TUs that came from the same original DWO
11762 file, we can combine them back into a virtual DWO file to save space
11763 (fewer struct dwo_file objects to allocate). Remember that for really
11764 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11765
11766 std::string virtual_dwo_name =
11767 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11768 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11769 (long) (sections.line_size ? sections.line_offset : 0),
11770 (long) (sections.loc_size ? sections.loc_offset : 0),
11771 (long) (sections.str_offsets_size
11772 ? sections.str_offsets_offset : 0));
11773 /* Can we use an existing virtual DWO file? */
11774 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11775 comp_dir);
11776 /* Create one if necessary. */
11777 if (*dwo_file_slot == NULL)
11778 {
11779 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11780 virtual_dwo_name.c_str ());
11781
11782 dwo_file = new struct dwo_file;
11783 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11784 dwo_file->comp_dir = comp_dir;
11785 dwo_file->sections.abbrev =
11786 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11787 sections.abbrev_offset,
11788 sections.abbrev_size);
11789 dwo_file->sections.line =
11790 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11791 sections.line_offset,
11792 sections.line_size);
11793 dwo_file->sections.loc =
11794 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11795 sections.loc_offset, sections.loc_size);
11796 dwo_file->sections.macinfo =
11797 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11798 sections.macinfo_offset,
11799 sections.macinfo_size);
11800 dwo_file->sections.macro =
11801 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11802 sections.macro_offset,
11803 sections.macro_size);
11804 dwo_file->sections.str_offsets =
11805 create_dwp_v2_or_v5_section (per_objfile,
11806 &dwp_file->sections.str_offsets,
11807 sections.str_offsets_offset,
11808 sections.str_offsets_size);
11809 /* The "str" section is global to the entire DWP file. */
11810 dwo_file->sections.str = dwp_file->sections.str;
11811 /* The info or types section is assigned below to dwo_unit,
11812 there's no need to record it in dwo_file.
11813 Also, we can't simply record type sections in dwo_file because
11814 we record a pointer into the vector in dwo_unit. As we collect more
11815 types we'll grow the vector and eventually have to reallocate space
11816 for it, invalidating all copies of pointers into the previous
11817 contents. */
11818 *dwo_file_slot = dwo_file;
11819 }
11820 else
11821 {
11822 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11823 virtual_dwo_name.c_str ());
11824
11825 dwo_file = (struct dwo_file *) *dwo_file_slot;
11826 }
11827
11828 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11829 dwo_unit->dwo_file = dwo_file;
11830 dwo_unit->signature = signature;
11831 dwo_unit->section =
11832 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11833 *dwo_unit->section = create_dwp_v2_or_v5_section
11834 (per_objfile,
11835 is_debug_types
11836 ? &dwp_file->sections.types
11837 : &dwp_file->sections.info,
11838 sections.info_or_types_offset,
11839 sections.info_or_types_size);
11840 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11841
11842 return dwo_unit;
11843 }
11844
11845 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11846 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11847 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11848 This is for DWP version 5 files. */
11849
11850 static struct dwo_unit *
11851 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11852 struct dwp_file *dwp_file,
11853 uint32_t unit_index,
11854 const char *comp_dir,
11855 ULONGEST signature, int is_debug_types)
11856 {
11857 const struct dwp_hash_table *dwp_htab
11858 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11859 bfd *dbfd = dwp_file->dbfd.get ();
11860 const char *kind = is_debug_types ? "TU" : "CU";
11861 struct dwo_file *dwo_file;
11862 struct dwo_unit *dwo_unit;
11863 struct virtual_v2_or_v5_dwo_sections sections {};
11864 void **dwo_file_slot;
11865
11866 gdb_assert (dwp_file->version == 5);
11867
11868 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11869 kind, pulongest (unit_index), hex_string (signature),
11870 dwp_file->name);
11871
11872 /* Fetch the section offsets of this DWO unit. */
11873
11874 /* memset (&sections, 0, sizeof (sections)); */
11875
11876 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11877 {
11878 uint32_t offset = read_4_bytes (dbfd,
11879 dwp_htab->section_pool.v5.offsets
11880 + (((unit_index - 1)
11881 * dwp_htab->nr_columns
11882 + i)
11883 * sizeof (uint32_t)));
11884 uint32_t size = read_4_bytes (dbfd,
11885 dwp_htab->section_pool.v5.sizes
11886 + (((unit_index - 1) * dwp_htab->nr_columns
11887 + i)
11888 * sizeof (uint32_t)));
11889
11890 switch (dwp_htab->section_pool.v5.section_ids[i])
11891 {
11892 case DW_SECT_ABBREV_V5:
11893 sections.abbrev_offset = offset;
11894 sections.abbrev_size = size;
11895 break;
11896 case DW_SECT_INFO_V5:
11897 sections.info_or_types_offset = offset;
11898 sections.info_or_types_size = size;
11899 break;
11900 case DW_SECT_LINE_V5:
11901 sections.line_offset = offset;
11902 sections.line_size = size;
11903 break;
11904 case DW_SECT_LOCLISTS_V5:
11905 sections.loclists_offset = offset;
11906 sections.loclists_size = size;
11907 break;
11908 case DW_SECT_MACRO_V5:
11909 sections.macro_offset = offset;
11910 sections.macro_size = size;
11911 break;
11912 case DW_SECT_RNGLISTS_V5:
11913 sections.rnglists_offset = offset;
11914 sections.rnglists_size = size;
11915 break;
11916 case DW_SECT_STR_OFFSETS_V5:
11917 sections.str_offsets_offset = offset;
11918 sections.str_offsets_size = size;
11919 break;
11920 case DW_SECT_RESERVED_V5:
11921 default:
11922 break;
11923 }
11924 }
11925
11926 /* It's easier for the rest of the code if we fake a struct dwo_file and
11927 have dwo_unit "live" in that. At least for now.
11928
11929 The DWP file can be made up of a random collection of CUs and TUs.
11930 However, for each CU + set of TUs that came from the same original DWO
11931 file, we can combine them back into a virtual DWO file to save space
11932 (fewer struct dwo_file objects to allocate). Remember that for really
11933 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11934
11935 std::string virtual_dwo_name =
11936 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11937 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11938 (long) (sections.line_size ? sections.line_offset : 0),
11939 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11940 (long) (sections.str_offsets_size
11941 ? sections.str_offsets_offset : 0),
11942 (long) (sections.macro_size ? sections.macro_offset : 0),
11943 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11944 /* Can we use an existing virtual DWO file? */
11945 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11946 virtual_dwo_name.c_str (),
11947 comp_dir);
11948 /* Create one if necessary. */
11949 if (*dwo_file_slot == NULL)
11950 {
11951 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11952 virtual_dwo_name.c_str ());
11953
11954 dwo_file = new struct dwo_file;
11955 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11956 dwo_file->comp_dir = comp_dir;
11957 dwo_file->sections.abbrev =
11958 create_dwp_v2_or_v5_section (per_objfile,
11959 &dwp_file->sections.abbrev,
11960 sections.abbrev_offset,
11961 sections.abbrev_size);
11962 dwo_file->sections.line =
11963 create_dwp_v2_or_v5_section (per_objfile,
11964 &dwp_file->sections.line,
11965 sections.line_offset, sections.line_size);
11966 dwo_file->sections.macro =
11967 create_dwp_v2_or_v5_section (per_objfile,
11968 &dwp_file->sections.macro,
11969 sections.macro_offset,
11970 sections.macro_size);
11971 dwo_file->sections.loclists =
11972 create_dwp_v2_or_v5_section (per_objfile,
11973 &dwp_file->sections.loclists,
11974 sections.loclists_offset,
11975 sections.loclists_size);
11976 dwo_file->sections.rnglists =
11977 create_dwp_v2_or_v5_section (per_objfile,
11978 &dwp_file->sections.rnglists,
11979 sections.rnglists_offset,
11980 sections.rnglists_size);
11981 dwo_file->sections.str_offsets =
11982 create_dwp_v2_or_v5_section (per_objfile,
11983 &dwp_file->sections.str_offsets,
11984 sections.str_offsets_offset,
11985 sections.str_offsets_size);
11986 /* The "str" section is global to the entire DWP file. */
11987 dwo_file->sections.str = dwp_file->sections.str;
11988 /* The info or types section is assigned below to dwo_unit,
11989 there's no need to record it in dwo_file.
11990 Also, we can't simply record type sections in dwo_file because
11991 we record a pointer into the vector in dwo_unit. As we collect more
11992 types we'll grow the vector and eventually have to reallocate space
11993 for it, invalidating all copies of pointers into the previous
11994 contents. */
11995 *dwo_file_slot = dwo_file;
11996 }
11997 else
11998 {
11999 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12000 virtual_dwo_name.c_str ());
12001
12002 dwo_file = (struct dwo_file *) *dwo_file_slot;
12003 }
12004
12005 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12006 dwo_unit->dwo_file = dwo_file;
12007 dwo_unit->signature = signature;
12008 dwo_unit->section
12009 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12010 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12011 &dwp_file->sections.info,
12012 sections.info_or_types_offset,
12013 sections.info_or_types_size);
12014 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12015
12016 return dwo_unit;
12017 }
12018
12019 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12020 Returns NULL if the signature isn't found. */
12021
12022 static struct dwo_unit *
12023 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12024 struct dwp_file *dwp_file, const char *comp_dir,
12025 ULONGEST signature, int is_debug_types)
12026 {
12027 const struct dwp_hash_table *dwp_htab =
12028 is_debug_types ? dwp_file->tus : dwp_file->cus;
12029 bfd *dbfd = dwp_file->dbfd.get ();
12030 uint32_t mask = dwp_htab->nr_slots - 1;
12031 uint32_t hash = signature & mask;
12032 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12033 unsigned int i;
12034 void **slot;
12035 struct dwo_unit find_dwo_cu;
12036
12037 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12038 find_dwo_cu.signature = signature;
12039 slot = htab_find_slot (is_debug_types
12040 ? dwp_file->loaded_tus.get ()
12041 : dwp_file->loaded_cus.get (),
12042 &find_dwo_cu, INSERT);
12043
12044 if (*slot != NULL)
12045 return (struct dwo_unit *) *slot;
12046
12047 /* Use a for loop so that we don't loop forever on bad debug info. */
12048 for (i = 0; i < dwp_htab->nr_slots; ++i)
12049 {
12050 ULONGEST signature_in_table;
12051
12052 signature_in_table =
12053 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12054 if (signature_in_table == signature)
12055 {
12056 uint32_t unit_index =
12057 read_4_bytes (dbfd,
12058 dwp_htab->unit_table + hash * sizeof (uint32_t));
12059
12060 if (dwp_file->version == 1)
12061 {
12062 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12063 unit_index, comp_dir,
12064 signature, is_debug_types);
12065 }
12066 else if (dwp_file->version == 2)
12067 {
12068 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12069 unit_index, comp_dir,
12070 signature, is_debug_types);
12071 }
12072 else /* version == 5 */
12073 {
12074 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12075 unit_index, comp_dir,
12076 signature, is_debug_types);
12077 }
12078 return (struct dwo_unit *) *slot;
12079 }
12080 if (signature_in_table == 0)
12081 return NULL;
12082 hash = (hash + hash2) & mask;
12083 }
12084
12085 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12086 " [in module %s]"),
12087 dwp_file->name);
12088 }
12089
12090 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12091 Open the file specified by FILE_NAME and hand it off to BFD for
12092 preliminary analysis. Return a newly initialized bfd *, which
12093 includes a canonicalized copy of FILE_NAME.
12094 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12095 SEARCH_CWD is true if the current directory is to be searched.
12096 It will be searched before debug-file-directory.
12097 If successful, the file is added to the bfd include table of the
12098 objfile's bfd (see gdb_bfd_record_inclusion).
12099 If unable to find/open the file, return NULL.
12100 NOTE: This function is derived from symfile_bfd_open. */
12101
12102 static gdb_bfd_ref_ptr
12103 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12104 const char *file_name, int is_dwp, int search_cwd)
12105 {
12106 int desc;
12107 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12108 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12109 to debug_file_directory. */
12110 const char *search_path;
12111 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12112
12113 gdb::unique_xmalloc_ptr<char> search_path_holder;
12114 if (search_cwd)
12115 {
12116 if (!debug_file_directory.empty ())
12117 {
12118 search_path_holder.reset (concat (".", dirname_separator_string,
12119 debug_file_directory.c_str (),
12120 (char *) NULL));
12121 search_path = search_path_holder.get ();
12122 }
12123 else
12124 search_path = ".";
12125 }
12126 else
12127 search_path = debug_file_directory.c_str ();
12128
12129 /* Add the path for the executable binary to the list of search paths. */
12130 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12131 search_path_holder.reset (concat (objfile_dir.c_str (),
12132 dirname_separator_string,
12133 search_path, nullptr));
12134 search_path = search_path_holder.get ();
12135
12136 openp_flags flags = OPF_RETURN_REALPATH;
12137 if (is_dwp)
12138 flags |= OPF_SEARCH_IN_PATH;
12139
12140 gdb::unique_xmalloc_ptr<char> absolute_name;
12141 desc = openp (search_path, flags, file_name,
12142 O_RDONLY | O_BINARY, &absolute_name);
12143 if (desc < 0)
12144 return NULL;
12145
12146 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12147 gnutarget, desc));
12148 if (sym_bfd == NULL)
12149 return NULL;
12150 bfd_set_cacheable (sym_bfd.get (), 1);
12151
12152 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12153 return NULL;
12154
12155 /* Success. Record the bfd as having been included by the objfile's bfd.
12156 This is important because things like demangled_names_hash lives in the
12157 objfile's per_bfd space and may have references to things like symbol
12158 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12159 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12160
12161 return sym_bfd;
12162 }
12163
12164 /* Try to open DWO file FILE_NAME.
12165 COMP_DIR is the DW_AT_comp_dir attribute.
12166 The result is the bfd handle of the file.
12167 If there is a problem finding or opening the file, return NULL.
12168 Upon success, the canonicalized path of the file is stored in the bfd,
12169 same as symfile_bfd_open. */
12170
12171 static gdb_bfd_ref_ptr
12172 open_dwo_file (dwarf2_per_objfile *per_objfile,
12173 const char *file_name, const char *comp_dir)
12174 {
12175 if (IS_ABSOLUTE_PATH (file_name))
12176 return try_open_dwop_file (per_objfile, file_name,
12177 0 /*is_dwp*/, 0 /*search_cwd*/);
12178
12179 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12180
12181 if (comp_dir != NULL)
12182 {
12183 gdb::unique_xmalloc_ptr<char> path_to_try
12184 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12185
12186 /* NOTE: If comp_dir is a relative path, this will also try the
12187 search path, which seems useful. */
12188 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12189 0 /*is_dwp*/,
12190 1 /*search_cwd*/));
12191 if (abfd != NULL)
12192 return abfd;
12193 }
12194
12195 /* That didn't work, try debug-file-directory, which, despite its name,
12196 is a list of paths. */
12197
12198 if (debug_file_directory.empty ())
12199 return NULL;
12200
12201 return try_open_dwop_file (per_objfile, file_name,
12202 0 /*is_dwp*/, 1 /*search_cwd*/);
12203 }
12204
12205 /* This function is mapped across the sections and remembers the offset and
12206 size of each of the DWO debugging sections we are interested in. */
12207
12208 static void
12209 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12210 dwo_sections *dwo_sections)
12211 {
12212 const struct dwop_section_names *names = &dwop_section_names;
12213
12214 if (names->abbrev_dwo.matches (sectp->name))
12215 {
12216 dwo_sections->abbrev.s.section = sectp;
12217 dwo_sections->abbrev.size = bfd_section_size (sectp);
12218 }
12219 else if (names->info_dwo.matches (sectp->name))
12220 {
12221 dwo_sections->info.s.section = sectp;
12222 dwo_sections->info.size = bfd_section_size (sectp);
12223 }
12224 else if (names->line_dwo.matches (sectp->name))
12225 {
12226 dwo_sections->line.s.section = sectp;
12227 dwo_sections->line.size = bfd_section_size (sectp);
12228 }
12229 else if (names->loc_dwo.matches (sectp->name))
12230 {
12231 dwo_sections->loc.s.section = sectp;
12232 dwo_sections->loc.size = bfd_section_size (sectp);
12233 }
12234 else if (names->loclists_dwo.matches (sectp->name))
12235 {
12236 dwo_sections->loclists.s.section = sectp;
12237 dwo_sections->loclists.size = bfd_section_size (sectp);
12238 }
12239 else if (names->macinfo_dwo.matches (sectp->name))
12240 {
12241 dwo_sections->macinfo.s.section = sectp;
12242 dwo_sections->macinfo.size = bfd_section_size (sectp);
12243 }
12244 else if (names->macro_dwo.matches (sectp->name))
12245 {
12246 dwo_sections->macro.s.section = sectp;
12247 dwo_sections->macro.size = bfd_section_size (sectp);
12248 }
12249 else if (names->rnglists_dwo.matches (sectp->name))
12250 {
12251 dwo_sections->rnglists.s.section = sectp;
12252 dwo_sections->rnglists.size = bfd_section_size (sectp);
12253 }
12254 else if (names->str_dwo.matches (sectp->name))
12255 {
12256 dwo_sections->str.s.section = sectp;
12257 dwo_sections->str.size = bfd_section_size (sectp);
12258 }
12259 else if (names->str_offsets_dwo.matches (sectp->name))
12260 {
12261 dwo_sections->str_offsets.s.section = sectp;
12262 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12263 }
12264 else if (names->types_dwo.matches (sectp->name))
12265 {
12266 struct dwarf2_section_info type_section;
12267
12268 memset (&type_section, 0, sizeof (type_section));
12269 type_section.s.section = sectp;
12270 type_section.size = bfd_section_size (sectp);
12271 dwo_sections->types.push_back (type_section);
12272 }
12273 }
12274
12275 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12276 by PER_CU. This is for the non-DWP case.
12277 The result is NULL if DWO_NAME can't be found. */
12278
12279 static struct dwo_file *
12280 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12281 const char *comp_dir)
12282 {
12283 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12284
12285 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12286 if (dbfd == NULL)
12287 {
12288 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12289
12290 return NULL;
12291 }
12292
12293 dwo_file_up dwo_file (new struct dwo_file);
12294 dwo_file->dwo_name = dwo_name;
12295 dwo_file->comp_dir = comp_dir;
12296 dwo_file->dbfd = std::move (dbfd);
12297
12298 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12299 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12300 &dwo_file->sections);
12301
12302 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12303 dwo_file->cus);
12304
12305 if (cu->per_cu->dwarf_version < 5)
12306 {
12307 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12308 dwo_file->sections.types, dwo_file->tus);
12309 }
12310 else
12311 {
12312 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12313 &dwo_file->sections.info, dwo_file->tus,
12314 rcuh_kind::COMPILE);
12315 }
12316
12317 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12318
12319 return dwo_file.release ();
12320 }
12321
12322 /* This function is mapped across the sections and remembers the offset and
12323 size of each of the DWP debugging sections common to version 1 and 2 that
12324 we are interested in. */
12325
12326 static void
12327 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12328 dwp_file *dwp_file)
12329 {
12330 const struct dwop_section_names *names = &dwop_section_names;
12331 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12332
12333 /* Record the ELF section number for later lookup: this is what the
12334 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12335 gdb_assert (elf_section_nr < dwp_file->num_sections);
12336 dwp_file->elf_sections[elf_section_nr] = sectp;
12337
12338 /* Look for specific sections that we need. */
12339 if (names->str_dwo.matches (sectp->name))
12340 {
12341 dwp_file->sections.str.s.section = sectp;
12342 dwp_file->sections.str.size = bfd_section_size (sectp);
12343 }
12344 else if (names->cu_index.matches (sectp->name))
12345 {
12346 dwp_file->sections.cu_index.s.section = sectp;
12347 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12348 }
12349 else if (names->tu_index.matches (sectp->name))
12350 {
12351 dwp_file->sections.tu_index.s.section = sectp;
12352 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12353 }
12354 }
12355
12356 /* This function is mapped across the sections and remembers the offset and
12357 size of each of the DWP version 2 debugging sections that we are interested
12358 in. This is split into a separate function because we don't know if we
12359 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12360
12361 static void
12362 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12363 {
12364 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12365 const struct dwop_section_names *names = &dwop_section_names;
12366 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12367
12368 /* Record the ELF section number for later lookup: this is what the
12369 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12370 gdb_assert (elf_section_nr < dwp_file->num_sections);
12371 dwp_file->elf_sections[elf_section_nr] = sectp;
12372
12373 /* Look for specific sections that we need. */
12374 if (names->abbrev_dwo.matches (sectp->name))
12375 {
12376 dwp_file->sections.abbrev.s.section = sectp;
12377 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12378 }
12379 else if (names->info_dwo.matches (sectp->name))
12380 {
12381 dwp_file->sections.info.s.section = sectp;
12382 dwp_file->sections.info.size = bfd_section_size (sectp);
12383 }
12384 else if (names->line_dwo.matches (sectp->name))
12385 {
12386 dwp_file->sections.line.s.section = sectp;
12387 dwp_file->sections.line.size = bfd_section_size (sectp);
12388 }
12389 else if (names->loc_dwo.matches (sectp->name))
12390 {
12391 dwp_file->sections.loc.s.section = sectp;
12392 dwp_file->sections.loc.size = bfd_section_size (sectp);
12393 }
12394 else if (names->macinfo_dwo.matches (sectp->name))
12395 {
12396 dwp_file->sections.macinfo.s.section = sectp;
12397 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12398 }
12399 else if (names->macro_dwo.matches (sectp->name))
12400 {
12401 dwp_file->sections.macro.s.section = sectp;
12402 dwp_file->sections.macro.size = bfd_section_size (sectp);
12403 }
12404 else if (names->str_offsets_dwo.matches (sectp->name))
12405 {
12406 dwp_file->sections.str_offsets.s.section = sectp;
12407 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12408 }
12409 else if (names->types_dwo.matches (sectp->name))
12410 {
12411 dwp_file->sections.types.s.section = sectp;
12412 dwp_file->sections.types.size = bfd_section_size (sectp);
12413 }
12414 }
12415
12416 /* This function is mapped across the sections and remembers the offset and
12417 size of each of the DWP version 5 debugging sections that we are interested
12418 in. This is split into a separate function because we don't know if we
12419 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12420
12421 static void
12422 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12423 {
12424 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12425 const struct dwop_section_names *names = &dwop_section_names;
12426 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12427
12428 /* Record the ELF section number for later lookup: this is what the
12429 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12430 gdb_assert (elf_section_nr < dwp_file->num_sections);
12431 dwp_file->elf_sections[elf_section_nr] = sectp;
12432
12433 /* Look for specific sections that we need. */
12434 if (names->abbrev_dwo.matches (sectp->name))
12435 {
12436 dwp_file->sections.abbrev.s.section = sectp;
12437 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12438 }
12439 else if (names->info_dwo.matches (sectp->name))
12440 {
12441 dwp_file->sections.info.s.section = sectp;
12442 dwp_file->sections.info.size = bfd_section_size (sectp);
12443 }
12444 else if (names->line_dwo.matches (sectp->name))
12445 {
12446 dwp_file->sections.line.s.section = sectp;
12447 dwp_file->sections.line.size = bfd_section_size (sectp);
12448 }
12449 else if (names->loclists_dwo.matches (sectp->name))
12450 {
12451 dwp_file->sections.loclists.s.section = sectp;
12452 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12453 }
12454 else if (names->macro_dwo.matches (sectp->name))
12455 {
12456 dwp_file->sections.macro.s.section = sectp;
12457 dwp_file->sections.macro.size = bfd_section_size (sectp);
12458 }
12459 else if (names->rnglists_dwo.matches (sectp->name))
12460 {
12461 dwp_file->sections.rnglists.s.section = sectp;
12462 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12463 }
12464 else if (names->str_offsets_dwo.matches (sectp->name))
12465 {
12466 dwp_file->sections.str_offsets.s.section = sectp;
12467 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12468 }
12469 }
12470
12471 /* Hash function for dwp_file loaded CUs/TUs. */
12472
12473 static hashval_t
12474 hash_dwp_loaded_cutus (const void *item)
12475 {
12476 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12477
12478 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12479 return dwo_unit->signature;
12480 }
12481
12482 /* Equality function for dwp_file loaded CUs/TUs. */
12483
12484 static int
12485 eq_dwp_loaded_cutus (const void *a, const void *b)
12486 {
12487 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12488 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12489
12490 return dua->signature == dub->signature;
12491 }
12492
12493 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12494
12495 static htab_up
12496 allocate_dwp_loaded_cutus_table ()
12497 {
12498 return htab_up (htab_create_alloc (3,
12499 hash_dwp_loaded_cutus,
12500 eq_dwp_loaded_cutus,
12501 NULL, xcalloc, xfree));
12502 }
12503
12504 /* Try to open DWP file FILE_NAME.
12505 The result is the bfd handle of the file.
12506 If there is a problem finding or opening the file, return NULL.
12507 Upon success, the canonicalized path of the file is stored in the bfd,
12508 same as symfile_bfd_open. */
12509
12510 static gdb_bfd_ref_ptr
12511 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12512 {
12513 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12514 1 /*is_dwp*/,
12515 1 /*search_cwd*/));
12516 if (abfd != NULL)
12517 return abfd;
12518
12519 /* Work around upstream bug 15652.
12520 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12521 [Whether that's a "bug" is debatable, but it is getting in our way.]
12522 We have no real idea where the dwp file is, because gdb's realpath-ing
12523 of the executable's path may have discarded the needed info.
12524 [IWBN if the dwp file name was recorded in the executable, akin to
12525 .gnu_debuglink, but that doesn't exist yet.]
12526 Strip the directory from FILE_NAME and search again. */
12527 if (!debug_file_directory.empty ())
12528 {
12529 /* Don't implicitly search the current directory here.
12530 If the user wants to search "." to handle this case,
12531 it must be added to debug-file-directory. */
12532 return try_open_dwop_file (per_objfile, lbasename (file_name),
12533 1 /*is_dwp*/,
12534 0 /*search_cwd*/);
12535 }
12536
12537 return NULL;
12538 }
12539
12540 /* Initialize the use of the DWP file for the current objfile.
12541 By convention the name of the DWP file is ${objfile}.dwp.
12542 The result is NULL if it can't be found. */
12543
12544 static std::unique_ptr<struct dwp_file>
12545 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12546 {
12547 struct objfile *objfile = per_objfile->objfile;
12548
12549 /* Try to find first .dwp for the binary file before any symbolic links
12550 resolving. */
12551
12552 /* If the objfile is a debug file, find the name of the real binary
12553 file and get the name of dwp file from there. */
12554 std::string dwp_name;
12555 if (objfile->separate_debug_objfile_backlink != NULL)
12556 {
12557 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12558 const char *backlink_basename = lbasename (backlink->original_name);
12559
12560 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12561 }
12562 else
12563 dwp_name = objfile->original_name;
12564
12565 dwp_name += ".dwp";
12566
12567 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12568 if (dbfd == NULL
12569 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12570 {
12571 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12572 dwp_name = objfile_name (objfile);
12573 dwp_name += ".dwp";
12574 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12575 }
12576
12577 if (dbfd == NULL)
12578 {
12579 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12580
12581 return std::unique_ptr<dwp_file> ();
12582 }
12583
12584 const char *name = bfd_get_filename (dbfd.get ());
12585 std::unique_ptr<struct dwp_file> dwp_file
12586 (new struct dwp_file (name, std::move (dbfd)));
12587
12588 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12589 dwp_file->elf_sections =
12590 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12591 dwp_file->num_sections, asection *);
12592
12593 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12594 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12595 dwp_file.get ());
12596
12597 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12598
12599 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12600
12601 /* The DWP file version is stored in the hash table. Oh well. */
12602 if (dwp_file->cus && dwp_file->tus
12603 && dwp_file->cus->version != dwp_file->tus->version)
12604 {
12605 /* Technically speaking, we should try to limp along, but this is
12606 pretty bizarre. We use pulongest here because that's the established
12607 portability solution (e.g, we cannot use %u for uint32_t). */
12608 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12609 " TU version %s [in DWP file %s]"),
12610 pulongest (dwp_file->cus->version),
12611 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12612 }
12613
12614 if (dwp_file->cus)
12615 dwp_file->version = dwp_file->cus->version;
12616 else if (dwp_file->tus)
12617 dwp_file->version = dwp_file->tus->version;
12618 else
12619 dwp_file->version = 2;
12620
12621 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12622 {
12623 if (dwp_file->version == 2)
12624 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12625 dwp_file.get ());
12626 else
12627 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12628 dwp_file.get ());
12629 }
12630
12631 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12632 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12633
12634 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12635 dwarf_read_debug_printf (" %s CUs, %s TUs",
12636 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12637 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12638
12639 return dwp_file;
12640 }
12641
12642 /* Wrapper around open_and_init_dwp_file, only open it once. */
12643
12644 static struct dwp_file *
12645 get_dwp_file (dwarf2_per_objfile *per_objfile)
12646 {
12647 if (!per_objfile->per_bfd->dwp_checked)
12648 {
12649 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12650 per_objfile->per_bfd->dwp_checked = 1;
12651 }
12652 return per_objfile->per_bfd->dwp_file.get ();
12653 }
12654
12655 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12656 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12657 or in the DWP file for the objfile, referenced by THIS_UNIT.
12658 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12659 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12660
12661 This is called, for example, when wanting to read a variable with a
12662 complex location. Therefore we don't want to do file i/o for every call.
12663 Therefore we don't want to look for a DWO file on every call.
12664 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12665 then we check if we've already seen DWO_NAME, and only THEN do we check
12666 for a DWO file.
12667
12668 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12669 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12670
12671 static struct dwo_unit *
12672 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12673 ULONGEST signature, int is_debug_types)
12674 {
12675 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12676 struct objfile *objfile = per_objfile->objfile;
12677 const char *kind = is_debug_types ? "TU" : "CU";
12678 void **dwo_file_slot;
12679 struct dwo_file *dwo_file;
12680 struct dwp_file *dwp_file;
12681
12682 /* First see if there's a DWP file.
12683 If we have a DWP file but didn't find the DWO inside it, don't
12684 look for the original DWO file. It makes gdb behave differently
12685 depending on whether one is debugging in the build tree. */
12686
12687 dwp_file = get_dwp_file (per_objfile);
12688 if (dwp_file != NULL)
12689 {
12690 const struct dwp_hash_table *dwp_htab =
12691 is_debug_types ? dwp_file->tus : dwp_file->cus;
12692
12693 if (dwp_htab != NULL)
12694 {
12695 struct dwo_unit *dwo_cutu =
12696 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12697 is_debug_types);
12698
12699 if (dwo_cutu != NULL)
12700 {
12701 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12702 kind, hex_string (signature),
12703 host_address_to_string (dwo_cutu));
12704
12705 return dwo_cutu;
12706 }
12707 }
12708 }
12709 else
12710 {
12711 /* No DWP file, look for the DWO file. */
12712
12713 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12714 if (*dwo_file_slot == NULL)
12715 {
12716 /* Read in the file and build a table of the CUs/TUs it contains. */
12717 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12718 }
12719 /* NOTE: This will be NULL if unable to open the file. */
12720 dwo_file = (struct dwo_file *) *dwo_file_slot;
12721
12722 if (dwo_file != NULL)
12723 {
12724 struct dwo_unit *dwo_cutu = NULL;
12725
12726 if (is_debug_types && dwo_file->tus)
12727 {
12728 struct dwo_unit find_dwo_cutu;
12729
12730 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12731 find_dwo_cutu.signature = signature;
12732 dwo_cutu
12733 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12734 &find_dwo_cutu);
12735 }
12736 else if (!is_debug_types && dwo_file->cus)
12737 {
12738 struct dwo_unit find_dwo_cutu;
12739
12740 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12741 find_dwo_cutu.signature = signature;
12742 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12743 &find_dwo_cutu);
12744 }
12745
12746 if (dwo_cutu != NULL)
12747 {
12748 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12749 kind, dwo_name, hex_string (signature),
12750 host_address_to_string (dwo_cutu));
12751
12752 return dwo_cutu;
12753 }
12754 }
12755 }
12756
12757 /* We didn't find it. This could mean a dwo_id mismatch, or
12758 someone deleted the DWO/DWP file, or the search path isn't set up
12759 correctly to find the file. */
12760
12761 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12762 kind, dwo_name, hex_string (signature));
12763
12764 /* This is a warning and not a complaint because it can be caused by
12765 pilot error (e.g., user accidentally deleting the DWO). */
12766 {
12767 /* Print the name of the DWP file if we looked there, helps the user
12768 better diagnose the problem. */
12769 std::string dwp_text;
12770
12771 if (dwp_file != NULL)
12772 dwp_text = string_printf (" [in DWP file %s]",
12773 lbasename (dwp_file->name));
12774
12775 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12776 " [in module %s]"),
12777 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12778 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12779 }
12780 return NULL;
12781 }
12782
12783 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12784 See lookup_dwo_cutu_unit for details. */
12785
12786 static struct dwo_unit *
12787 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12788 ULONGEST signature)
12789 {
12790 gdb_assert (!cu->per_cu->is_debug_types);
12791
12792 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12793 }
12794
12795 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12796 See lookup_dwo_cutu_unit for details. */
12797
12798 static struct dwo_unit *
12799 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12800 {
12801 gdb_assert (cu->per_cu->is_debug_types);
12802
12803 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12804
12805 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12806 }
12807
12808 /* Traversal function for queue_and_load_all_dwo_tus. */
12809
12810 static int
12811 queue_and_load_dwo_tu (void **slot, void *info)
12812 {
12813 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12814 dwarf2_cu *cu = (dwarf2_cu *) info;
12815 ULONGEST signature = dwo_unit->signature;
12816 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12817
12818 if (sig_type != NULL)
12819 {
12820 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12821 a real dependency of PER_CU on SIG_TYPE. That is detected later
12822 while processing PER_CU. */
12823 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12824 cu->per_cu->lang))
12825 load_full_type_unit (sig_type, cu->per_objfile);
12826 cu->per_cu->imported_symtabs_push (sig_type);
12827 }
12828
12829 return 1;
12830 }
12831
12832 /* Queue all TUs contained in the DWO of CU to be read in.
12833 The DWO may have the only definition of the type, though it may not be
12834 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12835 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12836
12837 static void
12838 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12839 {
12840 struct dwo_unit *dwo_unit;
12841 struct dwo_file *dwo_file;
12842
12843 gdb_assert (cu != nullptr);
12844 gdb_assert (!cu->per_cu->is_debug_types);
12845 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12846
12847 dwo_unit = cu->dwo_unit;
12848 gdb_assert (dwo_unit != NULL);
12849
12850 dwo_file = dwo_unit->dwo_file;
12851 if (dwo_file->tus != NULL)
12852 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12853 }
12854
12855 /* Read in various DIEs. */
12856
12857 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12858 Inherit only the children of the DW_AT_abstract_origin DIE not being
12859 already referenced by DW_AT_abstract_origin from the children of the
12860 current DIE. */
12861
12862 static void
12863 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12864 {
12865 struct die_info *child_die;
12866 sect_offset *offsetp;
12867 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12868 struct die_info *origin_die;
12869 /* Iterator of the ORIGIN_DIE children. */
12870 struct die_info *origin_child_die;
12871 struct attribute *attr;
12872 struct dwarf2_cu *origin_cu;
12873 struct pending **origin_previous_list_in_scope;
12874
12875 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12876 if (!attr)
12877 return;
12878
12879 /* Note that following die references may follow to a die in a
12880 different cu. */
12881
12882 origin_cu = cu;
12883 origin_die = follow_die_ref (die, attr, &origin_cu);
12884
12885 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12886 symbols in. */
12887 origin_previous_list_in_scope = origin_cu->list_in_scope;
12888 origin_cu->list_in_scope = cu->list_in_scope;
12889
12890 if (die->tag != origin_die->tag
12891 && !(die->tag == DW_TAG_inlined_subroutine
12892 && origin_die->tag == DW_TAG_subprogram))
12893 complaint (_("DIE %s and its abstract origin %s have different tags"),
12894 sect_offset_str (die->sect_off),
12895 sect_offset_str (origin_die->sect_off));
12896
12897 /* Find if the concrete and abstract trees are structurally the
12898 same. This is a shallow traversal and it is not bullet-proof;
12899 the compiler can trick the debugger into believing that the trees
12900 are isomorphic, whereas they actually are not. However, the
12901 likelyhood of this happening is pretty low, and a full-fledged
12902 check would be an overkill. */
12903 bool are_isomorphic = true;
12904 die_info *concrete_child = die->child;
12905 die_info *abstract_child = origin_die->child;
12906 while (concrete_child != nullptr || abstract_child != nullptr)
12907 {
12908 if (concrete_child == nullptr
12909 || abstract_child == nullptr
12910 || concrete_child->tag != abstract_child->tag)
12911 {
12912 are_isomorphic = false;
12913 break;
12914 }
12915
12916 concrete_child = concrete_child->sibling;
12917 abstract_child = abstract_child->sibling;
12918 }
12919
12920 /* Walk the origin's children in parallel to the concrete children.
12921 This helps match an origin child in case the debug info misses
12922 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12923 origin tree may not have the same tree structure as the concrete
12924 DIE, though. */
12925 die_info *corresponding_abstract_child
12926 = are_isomorphic ? origin_die->child : nullptr;
12927
12928 std::vector<sect_offset> offsets;
12929
12930 for (child_die = die->child;
12931 child_die && child_die->tag;
12932 child_die = child_die->sibling)
12933 {
12934 struct die_info *child_origin_die;
12935 struct dwarf2_cu *child_origin_cu;
12936
12937 /* We are trying to process concrete instance entries:
12938 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12939 it's not relevant to our analysis here. i.e. detecting DIEs that are
12940 present in the abstract instance but not referenced in the concrete
12941 one. */
12942 if (child_die->tag == DW_TAG_call_site
12943 || child_die->tag == DW_TAG_GNU_call_site)
12944 {
12945 if (are_isomorphic)
12946 corresponding_abstract_child
12947 = corresponding_abstract_child->sibling;
12948 continue;
12949 }
12950
12951 /* For each CHILD_DIE, find the corresponding child of
12952 ORIGIN_DIE. If there is more than one layer of
12953 DW_AT_abstract_origin, follow them all; there shouldn't be,
12954 but GCC versions at least through 4.4 generate this (GCC PR
12955 40573). */
12956 child_origin_die = child_die;
12957 child_origin_cu = cu;
12958 while (1)
12959 {
12960 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12961 child_origin_cu);
12962 if (attr == NULL)
12963 break;
12964 child_origin_die = follow_die_ref (child_origin_die, attr,
12965 &child_origin_cu);
12966 }
12967
12968 /* If missing DW_AT_abstract_origin, try the corresponding child
12969 of the origin. Clang emits such lexical scopes. */
12970 if (child_origin_die == child_die
12971 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12972 && are_isomorphic
12973 && child_die->tag == DW_TAG_lexical_block)
12974 child_origin_die = corresponding_abstract_child;
12975
12976 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12977 counterpart may exist. */
12978 if (child_origin_die != child_die)
12979 {
12980 if (child_die->tag != child_origin_die->tag
12981 && !(child_die->tag == DW_TAG_inlined_subroutine
12982 && child_origin_die->tag == DW_TAG_subprogram))
12983 complaint (_("Child DIE %s and its abstract origin %s have "
12984 "different tags"),
12985 sect_offset_str (child_die->sect_off),
12986 sect_offset_str (child_origin_die->sect_off));
12987 if (child_origin_die->parent != origin_die)
12988 complaint (_("Child DIE %s and its abstract origin %s have "
12989 "different parents"),
12990 sect_offset_str (child_die->sect_off),
12991 sect_offset_str (child_origin_die->sect_off));
12992 else
12993 offsets.push_back (child_origin_die->sect_off);
12994 }
12995
12996 if (are_isomorphic)
12997 corresponding_abstract_child = corresponding_abstract_child->sibling;
12998 }
12999 std::sort (offsets.begin (), offsets.end ());
13000 sect_offset *offsets_end = offsets.data () + offsets.size ();
13001 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13002 if (offsetp[-1] == *offsetp)
13003 complaint (_("Multiple children of DIE %s refer "
13004 "to DIE %s as their abstract origin"),
13005 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13006
13007 offsetp = offsets.data ();
13008 origin_child_die = origin_die->child;
13009 while (origin_child_die && origin_child_die->tag)
13010 {
13011 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13012 while (offsetp < offsets_end
13013 && *offsetp < origin_child_die->sect_off)
13014 offsetp++;
13015 if (offsetp >= offsets_end
13016 || *offsetp > origin_child_die->sect_off)
13017 {
13018 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13019 Check whether we're already processing ORIGIN_CHILD_DIE.
13020 This can happen with mutually referenced abstract_origins.
13021 PR 16581. */
13022 if (!origin_child_die->in_process)
13023 process_die (origin_child_die, origin_cu);
13024 }
13025 origin_child_die = origin_child_die->sibling;
13026 }
13027 origin_cu->list_in_scope = origin_previous_list_in_scope;
13028
13029 if (cu != origin_cu)
13030 compute_delayed_physnames (origin_cu);
13031 }
13032
13033 static void
13034 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13035 {
13036 struct objfile *objfile = cu->per_objfile->objfile;
13037 struct gdbarch *gdbarch = objfile->arch ();
13038 struct context_stack *newobj;
13039 CORE_ADDR lowpc;
13040 CORE_ADDR highpc;
13041 struct die_info *child_die;
13042 struct attribute *attr, *call_line, *call_file;
13043 const char *name;
13044 CORE_ADDR baseaddr;
13045 struct block *block;
13046 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13047 std::vector<struct symbol *> template_args;
13048 struct template_symbol *templ_func = NULL;
13049
13050 if (inlined_func)
13051 {
13052 /* If we do not have call site information, we can't show the
13053 caller of this inlined function. That's too confusing, so
13054 only use the scope for local variables. */
13055 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13056 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13057 if (call_line == NULL || call_file == NULL)
13058 {
13059 read_lexical_block_scope (die, cu);
13060 return;
13061 }
13062 }
13063
13064 baseaddr = objfile->text_section_offset ();
13065
13066 name = dwarf2_name (die, cu);
13067
13068 /* Ignore functions with missing or empty names. These are actually
13069 illegal according to the DWARF standard. */
13070 if (name == NULL)
13071 {
13072 complaint (_("missing name for subprogram DIE at %s"),
13073 sect_offset_str (die->sect_off));
13074 return;
13075 }
13076
13077 /* Ignore functions with missing or invalid low and high pc attributes. */
13078 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13079 <= PC_BOUNDS_INVALID)
13080 {
13081 attr = dwarf2_attr (die, DW_AT_external, cu);
13082 if (attr == nullptr || !attr->as_boolean ())
13083 complaint (_("cannot get low and high bounds "
13084 "for subprogram DIE at %s"),
13085 sect_offset_str (die->sect_off));
13086 return;
13087 }
13088
13089 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13090 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13091
13092 /* If we have any template arguments, then we must allocate a
13093 different sort of symbol. */
13094 for (child_die = die->child; child_die; child_die = child_die->sibling)
13095 {
13096 if (child_die->tag == DW_TAG_template_type_param
13097 || child_die->tag == DW_TAG_template_value_param)
13098 {
13099 templ_func = new (&objfile->objfile_obstack) template_symbol;
13100 templ_func->subclass = SYMBOL_TEMPLATE;
13101 break;
13102 }
13103 }
13104
13105 gdb_assert (cu->get_builder () != nullptr);
13106 newobj = cu->get_builder ()->push_context (0, lowpc);
13107 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13108 (struct symbol *) templ_func);
13109
13110 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13111 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13112 cu->per_cu->lang);
13113
13114 /* If there is a location expression for DW_AT_frame_base, record
13115 it. */
13116 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13117 if (attr != nullptr)
13118 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13119
13120 /* If there is a location for the static link, record it. */
13121 newobj->static_link = NULL;
13122 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13123 if (attr != nullptr)
13124 {
13125 newobj->static_link
13126 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13127 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13128 cu->addr_type ());
13129 }
13130
13131 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13132
13133 if (die->child != NULL)
13134 {
13135 child_die = die->child;
13136 while (child_die && child_die->tag)
13137 {
13138 if (child_die->tag == DW_TAG_template_type_param
13139 || child_die->tag == DW_TAG_template_value_param)
13140 {
13141 struct symbol *arg = new_symbol (child_die, NULL, cu);
13142
13143 if (arg != NULL)
13144 template_args.push_back (arg);
13145 }
13146 else
13147 process_die (child_die, cu);
13148 child_die = child_die->sibling;
13149 }
13150 }
13151
13152 inherit_abstract_dies (die, cu);
13153
13154 /* If we have a DW_AT_specification, we might need to import using
13155 directives from the context of the specification DIE. See the
13156 comment in determine_prefix. */
13157 if (cu->per_cu->lang == language_cplus
13158 && dwarf2_attr (die, DW_AT_specification, cu))
13159 {
13160 struct dwarf2_cu *spec_cu = cu;
13161 struct die_info *spec_die = die_specification (die, &spec_cu);
13162
13163 while (spec_die)
13164 {
13165 child_die = spec_die->child;
13166 while (child_die && child_die->tag)
13167 {
13168 if (child_die->tag == DW_TAG_imported_module)
13169 process_die (child_die, spec_cu);
13170 child_die = child_die->sibling;
13171 }
13172
13173 /* In some cases, GCC generates specification DIEs that
13174 themselves contain DW_AT_specification attributes. */
13175 spec_die = die_specification (spec_die, &spec_cu);
13176 }
13177 }
13178
13179 struct context_stack cstk = cu->get_builder ()->pop_context ();
13180 /* Make a block for the local symbols within. */
13181 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13182 cstk.static_link, lowpc, highpc);
13183
13184 /* For C++, set the block's scope. */
13185 if ((cu->per_cu->lang == language_cplus
13186 || cu->per_cu->lang == language_fortran
13187 || cu->per_cu->lang == language_d
13188 || cu->per_cu->lang == language_rust)
13189 && cu->processing_has_namespace_info)
13190 block_set_scope (block, determine_prefix (die, cu),
13191 &objfile->objfile_obstack);
13192
13193 /* If we have address ranges, record them. */
13194 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13195
13196 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13197
13198 /* Attach template arguments to function. */
13199 if (!template_args.empty ())
13200 {
13201 gdb_assert (templ_func != NULL);
13202
13203 templ_func->n_template_arguments = template_args.size ();
13204 templ_func->template_arguments
13205 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13206 templ_func->n_template_arguments);
13207 memcpy (templ_func->template_arguments,
13208 template_args.data (),
13209 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13210
13211 /* Make sure that the symtab is set on the new symbols. Even
13212 though they don't appear in this symtab directly, other parts
13213 of gdb assume that symbols do, and this is reasonably
13214 true. */
13215 for (symbol *sym : template_args)
13216 symbol_set_symtab (sym, symbol_symtab (templ_func));
13217 }
13218
13219 /* In C++, we can have functions nested inside functions (e.g., when
13220 a function declares a class that has methods). This means that
13221 when we finish processing a function scope, we may need to go
13222 back to building a containing block's symbol lists. */
13223 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13224 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13225
13226 /* If we've finished processing a top-level function, subsequent
13227 symbols go in the file symbol list. */
13228 if (cu->get_builder ()->outermost_context_p ())
13229 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13230 }
13231
13232 /* Process all the DIES contained within a lexical block scope. Start
13233 a new scope, process the dies, and then close the scope. */
13234
13235 static void
13236 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13237 {
13238 struct objfile *objfile = cu->per_objfile->objfile;
13239 struct gdbarch *gdbarch = objfile->arch ();
13240 CORE_ADDR lowpc, highpc;
13241 struct die_info *child_die;
13242 CORE_ADDR baseaddr;
13243
13244 baseaddr = objfile->text_section_offset ();
13245
13246 /* Ignore blocks with missing or invalid low and high pc attributes. */
13247 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13248 as multiple lexical blocks? Handling children in a sane way would
13249 be nasty. Might be easier to properly extend generic blocks to
13250 describe ranges. */
13251 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13252 {
13253 case PC_BOUNDS_NOT_PRESENT:
13254 /* DW_TAG_lexical_block has no attributes, process its children as if
13255 there was no wrapping by that DW_TAG_lexical_block.
13256 GCC does no longer produces such DWARF since GCC r224161. */
13257 for (child_die = die->child;
13258 child_die != NULL && child_die->tag;
13259 child_die = child_die->sibling)
13260 {
13261 /* We might already be processing this DIE. This can happen
13262 in an unusual circumstance -- where a subroutine A
13263 appears lexically in another subroutine B, but A actually
13264 inlines B. The recursion is broken here, rather than in
13265 inherit_abstract_dies, because it seems better to simply
13266 drop concrete children here. */
13267 if (!child_die->in_process)
13268 process_die (child_die, cu);
13269 }
13270 return;
13271 case PC_BOUNDS_INVALID:
13272 return;
13273 }
13274 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13275 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13276
13277 cu->get_builder ()->push_context (0, lowpc);
13278 if (die->child != NULL)
13279 {
13280 child_die = die->child;
13281 while (child_die && child_die->tag)
13282 {
13283 process_die (child_die, cu);
13284 child_die = child_die->sibling;
13285 }
13286 }
13287 inherit_abstract_dies (die, cu);
13288 struct context_stack cstk = cu->get_builder ()->pop_context ();
13289
13290 if (*cu->get_builder ()->get_local_symbols () != NULL
13291 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13292 {
13293 struct block *block
13294 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13295 cstk.start_addr, highpc);
13296
13297 /* Note that recording ranges after traversing children, as we
13298 do here, means that recording a parent's ranges entails
13299 walking across all its children's ranges as they appear in
13300 the address map, which is quadratic behavior.
13301
13302 It would be nicer to record the parent's ranges before
13303 traversing its children, simply overriding whatever you find
13304 there. But since we don't even decide whether to create a
13305 block until after we've traversed its children, that's hard
13306 to do. */
13307 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13308 }
13309 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13310 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13311 }
13312
13313 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13314
13315 static void
13316 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13317 {
13318 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13319 struct objfile *objfile = per_objfile->objfile;
13320 struct gdbarch *gdbarch = objfile->arch ();
13321 CORE_ADDR pc, baseaddr;
13322 struct attribute *attr;
13323 void **slot;
13324 int nparams;
13325 struct die_info *child_die;
13326
13327 baseaddr = objfile->text_section_offset ();
13328
13329 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13330 if (attr == NULL)
13331 {
13332 /* This was a pre-DWARF-5 GNU extension alias
13333 for DW_AT_call_return_pc. */
13334 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13335 }
13336 if (!attr)
13337 {
13338 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13339 "DIE %s [in module %s]"),
13340 sect_offset_str (die->sect_off), objfile_name (objfile));
13341 return;
13342 }
13343 pc = attr->as_address () + baseaddr;
13344 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13345 pc -= baseaddr;
13346
13347 if (cu->call_site_htab == NULL)
13348 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
13349 call_site::eq, NULL,
13350 &objfile->objfile_obstack,
13351 hashtab_obstack_allocate, NULL);
13352 struct call_site call_site_local (pc, nullptr, nullptr);
13353 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13354 if (*slot != NULL)
13355 {
13356 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13357 "DIE %s [in module %s]"),
13358 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13359 objfile_name (objfile));
13360 return;
13361 }
13362
13363 /* Count parameters at the caller. */
13364
13365 nparams = 0;
13366 for (child_die = die->child; child_die && child_die->tag;
13367 child_die = child_die->sibling)
13368 {
13369 if (child_die->tag != DW_TAG_call_site_parameter
13370 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13371 {
13372 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13373 "DW_TAG_call_site child DIE %s [in module %s]"),
13374 child_die->tag, sect_offset_str (child_die->sect_off),
13375 objfile_name (objfile));
13376 continue;
13377 }
13378
13379 nparams++;
13380 }
13381
13382 struct call_site *call_site
13383 = new (XOBNEWVAR (&objfile->objfile_obstack,
13384 struct call_site,
13385 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
13386 struct call_site (pc, cu->per_cu, per_objfile);
13387 *slot = call_site;
13388
13389 /* We never call the destructor of call_site, so we must ensure it is
13390 trivially destructible. */
13391 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
13392
13393 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13394 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13395 {
13396 struct die_info *func_die;
13397
13398 /* Skip also over DW_TAG_inlined_subroutine. */
13399 for (func_die = die->parent;
13400 func_die && func_die->tag != DW_TAG_subprogram
13401 && func_die->tag != DW_TAG_subroutine_type;
13402 func_die = func_die->parent);
13403
13404 /* DW_AT_call_all_calls is a superset
13405 of DW_AT_call_all_tail_calls. */
13406 if (func_die
13407 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13408 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13409 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13410 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13411 {
13412 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13413 not complete. But keep CALL_SITE for look ups via call_site_htab,
13414 both the initial caller containing the real return address PC and
13415 the final callee containing the current PC of a chain of tail
13416 calls do not need to have the tail call list complete. But any
13417 function candidate for a virtual tail call frame searched via
13418 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13419 determined unambiguously. */
13420 }
13421 else
13422 {
13423 struct type *func_type = NULL;
13424
13425 if (func_die)
13426 func_type = get_die_type (func_die, cu);
13427 if (func_type != NULL)
13428 {
13429 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13430
13431 /* Enlist this call site to the function. */
13432 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13433 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13434 }
13435 else
13436 complaint (_("Cannot find function owning DW_TAG_call_site "
13437 "DIE %s [in module %s]"),
13438 sect_offset_str (die->sect_off), objfile_name (objfile));
13439 }
13440 }
13441
13442 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13443 if (attr == NULL)
13444 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13445 if (attr == NULL)
13446 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13447 if (attr == NULL)
13448 {
13449 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13450 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13451 }
13452
13453 call_site->target.set_loc_dwarf_block (nullptr);
13454 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13455 /* Keep NULL DWARF_BLOCK. */;
13456 else if (attr->form_is_block ())
13457 {
13458 struct dwarf2_locexpr_baton *dlbaton;
13459 struct dwarf_block *block = attr->as_block ();
13460
13461 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13462 dlbaton->data = block->data;
13463 dlbaton->size = block->size;
13464 dlbaton->per_objfile = per_objfile;
13465 dlbaton->per_cu = cu->per_cu;
13466
13467 call_site->target.set_loc_dwarf_block (dlbaton);
13468 }
13469 else if (attr->form_is_ref ())
13470 {
13471 struct dwarf2_cu *target_cu = cu;
13472 struct die_info *target_die;
13473
13474 target_die = follow_die_ref (die, attr, &target_cu);
13475 gdb_assert (target_cu->per_objfile->objfile == objfile);
13476 if (die_is_declaration (target_die, target_cu))
13477 {
13478 const char *target_physname;
13479
13480 /* Prefer the mangled name; otherwise compute the demangled one. */
13481 target_physname = dw2_linkage_name (target_die, target_cu);
13482 if (target_physname == NULL)
13483 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13484 if (target_physname == NULL)
13485 complaint (_("DW_AT_call_target target DIE has invalid "
13486 "physname, for referencing DIE %s [in module %s]"),
13487 sect_offset_str (die->sect_off), objfile_name (objfile));
13488 else
13489 call_site->target.set_loc_physname (target_physname);
13490 }
13491 else
13492 {
13493 CORE_ADDR lowpc;
13494
13495 /* DW_AT_entry_pc should be preferred. */
13496 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13497 <= PC_BOUNDS_INVALID)
13498 complaint (_("DW_AT_call_target target DIE has invalid "
13499 "low pc, for referencing DIE %s [in module %s]"),
13500 sect_offset_str (die->sect_off), objfile_name (objfile));
13501 else
13502 {
13503 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
13504 - baseaddr);
13505 call_site->target.set_loc_physaddr (lowpc);
13506 }
13507 }
13508 }
13509 else
13510 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13511 "block nor reference, for DIE %s [in module %s]"),
13512 sect_offset_str (die->sect_off), objfile_name (objfile));
13513
13514 for (child_die = die->child;
13515 child_die && child_die->tag;
13516 child_die = child_die->sibling)
13517 {
13518 struct call_site_parameter *parameter;
13519 struct attribute *loc, *origin;
13520
13521 if (child_die->tag != DW_TAG_call_site_parameter
13522 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13523 {
13524 /* Already printed the complaint above. */
13525 continue;
13526 }
13527
13528 gdb_assert (call_site->parameter_count < nparams);
13529 parameter = &call_site->parameter[call_site->parameter_count];
13530
13531 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13532 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13533 register is contained in DW_AT_call_value. */
13534
13535 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13536 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13537 if (origin == NULL)
13538 {
13539 /* This was a pre-DWARF-5 GNU extension alias
13540 for DW_AT_call_parameter. */
13541 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13542 }
13543 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13544 {
13545 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13546
13547 sect_offset sect_off = origin->get_ref_die_offset ();
13548 if (!cu->header.offset_in_cu_p (sect_off))
13549 {
13550 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13551 binding can be done only inside one CU. Such referenced DIE
13552 therefore cannot be even moved to DW_TAG_partial_unit. */
13553 complaint (_("DW_AT_call_parameter offset is not in CU for "
13554 "DW_TAG_call_site child DIE %s [in module %s]"),
13555 sect_offset_str (child_die->sect_off),
13556 objfile_name (objfile));
13557 continue;
13558 }
13559 parameter->u.param_cu_off
13560 = (cu_offset) (sect_off - cu->header.sect_off);
13561 }
13562 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13563 {
13564 complaint (_("No DW_FORM_block* DW_AT_location for "
13565 "DW_TAG_call_site child DIE %s [in module %s]"),
13566 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13567 continue;
13568 }
13569 else
13570 {
13571 struct dwarf_block *block = loc->as_block ();
13572
13573 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13574 (block->data, &block->data[block->size]);
13575 if (parameter->u.dwarf_reg != -1)
13576 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13577 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13578 &block->data[block->size],
13579 &parameter->u.fb_offset))
13580 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13581 else
13582 {
13583 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13584 "for DW_FORM_block* DW_AT_location is supported for "
13585 "DW_TAG_call_site child DIE %s "
13586 "[in module %s]"),
13587 sect_offset_str (child_die->sect_off),
13588 objfile_name (objfile));
13589 continue;
13590 }
13591 }
13592
13593 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13594 if (attr == NULL)
13595 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13596 if (attr == NULL || !attr->form_is_block ())
13597 {
13598 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13599 "DW_TAG_call_site child DIE %s [in module %s]"),
13600 sect_offset_str (child_die->sect_off),
13601 objfile_name (objfile));
13602 continue;
13603 }
13604
13605 struct dwarf_block *block = attr->as_block ();
13606 parameter->value = block->data;
13607 parameter->value_size = block->size;
13608
13609 /* Parameters are not pre-cleared by memset above. */
13610 parameter->data_value = NULL;
13611 parameter->data_value_size = 0;
13612 call_site->parameter_count++;
13613
13614 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13615 if (attr == NULL)
13616 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13617 if (attr != nullptr)
13618 {
13619 if (!attr->form_is_block ())
13620 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13621 "DW_TAG_call_site child DIE %s [in module %s]"),
13622 sect_offset_str (child_die->sect_off),
13623 objfile_name (objfile));
13624 else
13625 {
13626 block = attr->as_block ();
13627 parameter->data_value = block->data;
13628 parameter->data_value_size = block->size;
13629 }
13630 }
13631 }
13632 }
13633
13634 /* Helper function for read_variable. If DIE represents a virtual
13635 table, then return the type of the concrete object that is
13636 associated with the virtual table. Otherwise, return NULL. */
13637
13638 static struct type *
13639 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13640 {
13641 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13642 if (attr == NULL)
13643 return NULL;
13644
13645 /* Find the type DIE. */
13646 struct die_info *type_die = NULL;
13647 struct dwarf2_cu *type_cu = cu;
13648
13649 if (attr->form_is_ref ())
13650 type_die = follow_die_ref (die, attr, &type_cu);
13651 if (type_die == NULL)
13652 return NULL;
13653
13654 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13655 return NULL;
13656 return die_containing_type (type_die, type_cu);
13657 }
13658
13659 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13660
13661 static void
13662 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13663 {
13664 struct rust_vtable_symbol *storage = NULL;
13665
13666 if (cu->per_cu->lang == language_rust)
13667 {
13668 struct type *containing_type = rust_containing_type (die, cu);
13669
13670 if (containing_type != NULL)
13671 {
13672 struct objfile *objfile = cu->per_objfile->objfile;
13673
13674 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13675 storage->concrete_type = containing_type;
13676 storage->subclass = SYMBOL_RUST_VTABLE;
13677 }
13678 }
13679
13680 struct symbol *res = new_symbol (die, NULL, cu, storage);
13681 struct attribute *abstract_origin
13682 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13683 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13684 if (res == NULL && loc && abstract_origin)
13685 {
13686 /* We have a variable without a name, but with a location and an abstract
13687 origin. This may be a concrete instance of an abstract variable
13688 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13689 later. */
13690 struct dwarf2_cu *origin_cu = cu;
13691 struct die_info *origin_die
13692 = follow_die_ref (die, abstract_origin, &origin_cu);
13693 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13694 per_objfile->per_bfd->abstract_to_concrete
13695 [origin_die->sect_off].push_back (die->sect_off);
13696 }
13697 }
13698
13699 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13700 reading .debug_rnglists.
13701 Callback's type should be:
13702 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13703 Return true if the attributes are present and valid, otherwise,
13704 return false. */
13705
13706 template <typename Callback>
13707 static bool
13708 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13709 dwarf_tag tag, Callback &&callback)
13710 {
13711 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13712 struct objfile *objfile = per_objfile->objfile;
13713 bfd *obfd = objfile->obfd;
13714 /* Base address selection entry. */
13715 gdb::optional<CORE_ADDR> base;
13716 const gdb_byte *buffer;
13717 bool overflow = false;
13718 ULONGEST addr_index;
13719 struct dwarf2_section_info *rnglists_section;
13720
13721 base = cu->base_address;
13722 rnglists_section = cu_debug_rnglists_section (cu, tag);
13723 rnglists_section->read (objfile);
13724
13725 if (offset >= rnglists_section->size)
13726 {
13727 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13728 offset);
13729 return false;
13730 }
13731 buffer = rnglists_section->buffer + offset;
13732
13733 while (1)
13734 {
13735 /* Initialize it due to a false compiler warning. */
13736 CORE_ADDR range_beginning = 0, range_end = 0;
13737 const gdb_byte *buf_end = (rnglists_section->buffer
13738 + rnglists_section->size);
13739 unsigned int bytes_read;
13740
13741 if (buffer == buf_end)
13742 {
13743 overflow = true;
13744 break;
13745 }
13746 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13747 switch (rlet)
13748 {
13749 case DW_RLE_end_of_list:
13750 break;
13751 case DW_RLE_base_address:
13752 if (buffer + cu->header.addr_size > buf_end)
13753 {
13754 overflow = true;
13755 break;
13756 }
13757 base = cu->header.read_address (obfd, buffer, &bytes_read);
13758 buffer += bytes_read;
13759 break;
13760 case DW_RLE_base_addressx:
13761 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13762 buffer += bytes_read;
13763 base = read_addr_index (cu, addr_index);
13764 break;
13765 case DW_RLE_start_length:
13766 if (buffer + cu->header.addr_size > buf_end)
13767 {
13768 overflow = true;
13769 break;
13770 }
13771 range_beginning = cu->header.read_address (obfd, buffer,
13772 &bytes_read);
13773 buffer += bytes_read;
13774 range_end = (range_beginning
13775 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13776 buffer += bytes_read;
13777 if (buffer > buf_end)
13778 {
13779 overflow = true;
13780 break;
13781 }
13782 break;
13783 case DW_RLE_startx_length:
13784 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13785 buffer += bytes_read;
13786 range_beginning = read_addr_index (cu, addr_index);
13787 if (buffer > buf_end)
13788 {
13789 overflow = true;
13790 break;
13791 }
13792 range_end = (range_beginning
13793 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13794 buffer += bytes_read;
13795 break;
13796 case DW_RLE_offset_pair:
13797 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13798 buffer += bytes_read;
13799 if (buffer > buf_end)
13800 {
13801 overflow = true;
13802 break;
13803 }
13804 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13805 buffer += bytes_read;
13806 if (buffer > buf_end)
13807 {
13808 overflow = true;
13809 break;
13810 }
13811 break;
13812 case DW_RLE_start_end:
13813 if (buffer + 2 * cu->header.addr_size > buf_end)
13814 {
13815 overflow = true;
13816 break;
13817 }
13818 range_beginning = cu->header.read_address (obfd, buffer,
13819 &bytes_read);
13820 buffer += bytes_read;
13821 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13822 buffer += bytes_read;
13823 break;
13824 case DW_RLE_startx_endx:
13825 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13826 buffer += bytes_read;
13827 range_beginning = read_addr_index (cu, addr_index);
13828 if (buffer > buf_end)
13829 {
13830 overflow = true;
13831 break;
13832 }
13833 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13834 buffer += bytes_read;
13835 range_end = read_addr_index (cu, addr_index);
13836 break;
13837 default:
13838 complaint (_("Invalid .debug_rnglists data (no base address)"));
13839 return false;
13840 }
13841 if (rlet == DW_RLE_end_of_list || overflow)
13842 break;
13843 if (rlet == DW_RLE_base_address)
13844 continue;
13845
13846 if (range_beginning > range_end)
13847 {
13848 /* Inverted range entries are invalid. */
13849 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13850 return false;
13851 }
13852
13853 /* Empty range entries have no effect. */
13854 if (range_beginning == range_end)
13855 continue;
13856
13857 /* Only DW_RLE_offset_pair needs the base address added. */
13858 if (rlet == DW_RLE_offset_pair)
13859 {
13860 if (!base.has_value ())
13861 {
13862 /* We have no valid base address for the DW_RLE_offset_pair. */
13863 complaint (_("Invalid .debug_rnglists data (no base address for "
13864 "DW_RLE_offset_pair)"));
13865 return false;
13866 }
13867
13868 range_beginning += *base;
13869 range_end += *base;
13870 }
13871
13872 /* A not-uncommon case of bad debug info.
13873 Don't pollute the addrmap with bad data. */
13874 if (range_beginning == 0
13875 && !per_objfile->per_bfd->has_section_at_zero)
13876 {
13877 complaint (_(".debug_rnglists entry has start address of zero"
13878 " [in module %s]"), objfile_name (objfile));
13879 continue;
13880 }
13881
13882 callback (range_beginning, range_end);
13883 }
13884
13885 if (overflow)
13886 {
13887 complaint (_("Offset %d is not terminated "
13888 "for DW_AT_ranges attribute"),
13889 offset);
13890 return false;
13891 }
13892
13893 return true;
13894 }
13895
13896 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13897 Callback's type should be:
13898 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13899 Return 1 if the attributes are present and valid, otherwise, return 0. */
13900
13901 template <typename Callback>
13902 static int
13903 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13904 Callback &&callback)
13905 {
13906 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13907 struct objfile *objfile = per_objfile->objfile;
13908 struct comp_unit_head *cu_header = &cu->header;
13909 bfd *obfd = objfile->obfd;
13910 unsigned int addr_size = cu_header->addr_size;
13911 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13912 /* Base address selection entry. */
13913 gdb::optional<CORE_ADDR> base;
13914 unsigned int dummy;
13915 const gdb_byte *buffer;
13916
13917 if (cu_header->version >= 5)
13918 return dwarf2_rnglists_process (offset, cu, tag, callback);
13919
13920 base = cu->base_address;
13921
13922 per_objfile->per_bfd->ranges.read (objfile);
13923 if (offset >= per_objfile->per_bfd->ranges.size)
13924 {
13925 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13926 offset);
13927 return 0;
13928 }
13929 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13930
13931 while (1)
13932 {
13933 CORE_ADDR range_beginning, range_end;
13934
13935 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13936 buffer += addr_size;
13937 range_end = cu->header.read_address (obfd, buffer, &dummy);
13938 buffer += addr_size;
13939 offset += 2 * addr_size;
13940
13941 /* An end of list marker is a pair of zero addresses. */
13942 if (range_beginning == 0 && range_end == 0)
13943 /* Found the end of list entry. */
13944 break;
13945
13946 /* Each base address selection entry is a pair of 2 values.
13947 The first is the largest possible address, the second is
13948 the base address. Check for a base address here. */
13949 if ((range_beginning & mask) == mask)
13950 {
13951 /* If we found the largest possible address, then we already
13952 have the base address in range_end. */
13953 base = range_end;
13954 continue;
13955 }
13956
13957 if (!base.has_value ())
13958 {
13959 /* We have no valid base address for the ranges
13960 data. */
13961 complaint (_("Invalid .debug_ranges data (no base address)"));
13962 return 0;
13963 }
13964
13965 if (range_beginning > range_end)
13966 {
13967 /* Inverted range entries are invalid. */
13968 complaint (_("Invalid .debug_ranges data (inverted range)"));
13969 return 0;
13970 }
13971
13972 /* Empty range entries have no effect. */
13973 if (range_beginning == range_end)
13974 continue;
13975
13976 range_beginning += *base;
13977 range_end += *base;
13978
13979 /* A not-uncommon case of bad debug info.
13980 Don't pollute the addrmap with bad data. */
13981 if (range_beginning == 0
13982 && !per_objfile->per_bfd->has_section_at_zero)
13983 {
13984 complaint (_(".debug_ranges entry has start address of zero"
13985 " [in module %s]"), objfile_name (objfile));
13986 continue;
13987 }
13988
13989 callback (range_beginning, range_end);
13990 }
13991
13992 return 1;
13993 }
13994
13995 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13996 Return 1 if the attributes are present and valid, otherwise, return 0.
13997 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
13998
13999 static int
14000 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14001 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14002 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14003 {
14004 struct objfile *objfile = cu->per_objfile->objfile;
14005 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14006 struct gdbarch *gdbarch = objfile->arch ();
14007 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14008 int low_set = 0;
14009 CORE_ADDR low = 0;
14010 CORE_ADDR high = 0;
14011 int retval;
14012
14013 retval = dwarf2_ranges_process (offset, cu, tag,
14014 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14015 {
14016 if (ranges_pst != NULL)
14017 {
14018 CORE_ADDR lowpc;
14019 CORE_ADDR highpc;
14020
14021 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14022 range_beginning + baseaddr)
14023 - baseaddr);
14024 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14025 range_end + baseaddr)
14026 - baseaddr);
14027 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14028 lowpc, highpc - 1, ranges_pst);
14029 }
14030
14031 /* FIXME: This is recording everything as a low-high
14032 segment of consecutive addresses. We should have a
14033 data structure for discontiguous block ranges
14034 instead. */
14035 if (! low_set)
14036 {
14037 low = range_beginning;
14038 high = range_end;
14039 low_set = 1;
14040 }
14041 else
14042 {
14043 if (range_beginning < low)
14044 low = range_beginning;
14045 if (range_end > high)
14046 high = range_end;
14047 }
14048 });
14049 if (!retval)
14050 return 0;
14051
14052 if (! low_set)
14053 /* If the first entry is an end-of-list marker, the range
14054 describes an empty scope, i.e. no instructions. */
14055 return 0;
14056
14057 if (low_return)
14058 *low_return = low;
14059 if (high_return)
14060 *high_return = high;
14061 return 1;
14062 }
14063
14064 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14065 definition for the return value. *LOWPC and *HIGHPC are set iff
14066 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14067
14068 static enum pc_bounds_kind
14069 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14070 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14071 dwarf2_psymtab *pst)
14072 {
14073 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14074 struct attribute *attr;
14075 struct attribute *attr_high;
14076 CORE_ADDR low = 0;
14077 CORE_ADDR high = 0;
14078 enum pc_bounds_kind ret;
14079
14080 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14081 if (attr_high)
14082 {
14083 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14084 if (attr != nullptr)
14085 {
14086 low = attr->as_address ();
14087 high = attr_high->as_address ();
14088 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14089 high += low;
14090 }
14091 else
14092 /* Found high w/o low attribute. */
14093 return PC_BOUNDS_INVALID;
14094
14095 /* Found consecutive range of addresses. */
14096 ret = PC_BOUNDS_HIGH_LOW;
14097 }
14098 else
14099 {
14100 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14101 if (attr != nullptr && attr->form_is_unsigned ())
14102 {
14103 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14104 on DWARF version). */
14105 ULONGEST ranges_offset = attr->as_unsigned ();
14106
14107 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14108 this value. */
14109 if (die->tag != DW_TAG_compile_unit)
14110 ranges_offset += cu->gnu_ranges_base;
14111
14112 /* Value of the DW_AT_ranges attribute is the offset in the
14113 .debug_ranges section. */
14114 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14115 die->tag))
14116 return PC_BOUNDS_INVALID;
14117 /* Found discontinuous range of addresses. */
14118 ret = PC_BOUNDS_RANGES;
14119 }
14120 else
14121 return PC_BOUNDS_NOT_PRESENT;
14122 }
14123
14124 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14125 if (high <= low)
14126 return PC_BOUNDS_INVALID;
14127
14128 /* When using the GNU linker, .gnu.linkonce. sections are used to
14129 eliminate duplicate copies of functions and vtables and such.
14130 The linker will arbitrarily choose one and discard the others.
14131 The AT_*_pc values for such functions refer to local labels in
14132 these sections. If the section from that file was discarded, the
14133 labels are not in the output, so the relocs get a value of 0.
14134 If this is a discarded function, mark the pc bounds as invalid,
14135 so that GDB will ignore it. */
14136 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14137 return PC_BOUNDS_INVALID;
14138
14139 *lowpc = low;
14140 if (highpc)
14141 *highpc = high;
14142 return ret;
14143 }
14144
14145 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14146 its low and high PC addresses. Do nothing if these addresses could not
14147 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14148 and HIGHPC to the high address if greater than HIGHPC. */
14149
14150 static void
14151 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14152 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14153 struct dwarf2_cu *cu)
14154 {
14155 CORE_ADDR low, high;
14156 struct die_info *child = die->child;
14157
14158 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14159 {
14160 *lowpc = std::min (*lowpc, low);
14161 *highpc = std::max (*highpc, high);
14162 }
14163
14164 /* If the language does not allow nested subprograms (either inside
14165 subprograms or lexical blocks), we're done. */
14166 if (cu->per_cu->lang != language_ada)
14167 return;
14168
14169 /* Check all the children of the given DIE. If it contains nested
14170 subprograms, then check their pc bounds. Likewise, we need to
14171 check lexical blocks as well, as they may also contain subprogram
14172 definitions. */
14173 while (child && child->tag)
14174 {
14175 if (child->tag == DW_TAG_subprogram
14176 || child->tag == DW_TAG_lexical_block)
14177 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14178 child = child->sibling;
14179 }
14180 }
14181
14182 /* Get the low and high pc's represented by the scope DIE, and store
14183 them in *LOWPC and *HIGHPC. If the correct values can't be
14184 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14185
14186 static void
14187 get_scope_pc_bounds (struct die_info *die,
14188 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14189 struct dwarf2_cu *cu)
14190 {
14191 CORE_ADDR best_low = (CORE_ADDR) -1;
14192 CORE_ADDR best_high = (CORE_ADDR) 0;
14193 CORE_ADDR current_low, current_high;
14194
14195 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14196 >= PC_BOUNDS_RANGES)
14197 {
14198 best_low = current_low;
14199 best_high = current_high;
14200 }
14201 else
14202 {
14203 struct die_info *child = die->child;
14204
14205 while (child && child->tag)
14206 {
14207 switch (child->tag) {
14208 case DW_TAG_subprogram:
14209 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14210 break;
14211 case DW_TAG_namespace:
14212 case DW_TAG_module:
14213 /* FIXME: carlton/2004-01-16: Should we do this for
14214 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14215 that current GCC's always emit the DIEs corresponding
14216 to definitions of methods of classes as children of a
14217 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14218 the DIEs giving the declarations, which could be
14219 anywhere). But I don't see any reason why the
14220 standards says that they have to be there. */
14221 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14222
14223 if (current_low != ((CORE_ADDR) -1))
14224 {
14225 best_low = std::min (best_low, current_low);
14226 best_high = std::max (best_high, current_high);
14227 }
14228 break;
14229 default:
14230 /* Ignore. */
14231 break;
14232 }
14233
14234 child = child->sibling;
14235 }
14236 }
14237
14238 *lowpc = best_low;
14239 *highpc = best_high;
14240 }
14241
14242 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14243 in DIE. */
14244
14245 static void
14246 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14247 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14248 {
14249 struct objfile *objfile = cu->per_objfile->objfile;
14250 struct gdbarch *gdbarch = objfile->arch ();
14251 struct attribute *attr;
14252 struct attribute *attr_high;
14253
14254 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14255 if (attr_high)
14256 {
14257 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14258 if (attr != nullptr)
14259 {
14260 CORE_ADDR low = attr->as_address ();
14261 CORE_ADDR high = attr_high->as_address ();
14262
14263 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14264 high += low;
14265
14266 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14267 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14268 cu->get_builder ()->record_block_range (block, low, high - 1);
14269 }
14270 }
14271
14272 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14273 if (attr != nullptr && attr->form_is_unsigned ())
14274 {
14275 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14276 on DWARF version). */
14277 ULONGEST ranges_offset = attr->as_unsigned ();
14278
14279 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14280 this value. */
14281 if (die->tag != DW_TAG_compile_unit)
14282 ranges_offset += cu->gnu_ranges_base;
14283
14284 std::vector<blockrange> blockvec;
14285 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14286 [&] (CORE_ADDR start, CORE_ADDR end)
14287 {
14288 start += baseaddr;
14289 end += baseaddr;
14290 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14291 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14292 cu->get_builder ()->record_block_range (block, start, end - 1);
14293 blockvec.emplace_back (start, end);
14294 });
14295
14296 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14297 }
14298 }
14299
14300 /* Check whether the producer field indicates either of GCC < 4.6, or the
14301 Intel C/C++ compiler, and cache the result in CU. */
14302
14303 static void
14304 check_producer (struct dwarf2_cu *cu)
14305 {
14306 int major, minor;
14307
14308 if (cu->producer == NULL)
14309 {
14310 /* For unknown compilers expect their behavior is DWARF version
14311 compliant.
14312
14313 GCC started to support .debug_types sections by -gdwarf-4 since
14314 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14315 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14316 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14317 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14318 }
14319 else if (producer_is_gcc (cu->producer, &major, &minor))
14320 {
14321 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14322 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14323 cu->producer_is_gcc_11 = major == 11;
14324 }
14325 else if (producer_is_icc (cu->producer, &major, &minor))
14326 {
14327 cu->producer_is_icc = true;
14328 cu->producer_is_icc_lt_14 = major < 14;
14329 }
14330 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14331 cu->producer_is_codewarrior = true;
14332 else
14333 {
14334 /* For other non-GCC compilers, expect their behavior is DWARF version
14335 compliant. */
14336 }
14337
14338 cu->checked_producer = true;
14339 }
14340
14341 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14342 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14343 during 4.6.0 experimental. */
14344
14345 static bool
14346 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14347 {
14348 if (!cu->checked_producer)
14349 check_producer (cu);
14350
14351 return cu->producer_is_gxx_lt_4_6;
14352 }
14353
14354
14355 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14356 with incorrect is_stmt attributes. */
14357
14358 static bool
14359 producer_is_codewarrior (struct dwarf2_cu *cu)
14360 {
14361 if (!cu->checked_producer)
14362 check_producer (cu);
14363
14364 return cu->producer_is_codewarrior;
14365 }
14366
14367 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14368 If that attribute is not available, return the appropriate
14369 default. */
14370
14371 static enum dwarf_access_attribute
14372 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14373 {
14374 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14375 if (attr != nullptr)
14376 {
14377 LONGEST value = attr->constant_value (-1);
14378 if (value == DW_ACCESS_public
14379 || value == DW_ACCESS_protected
14380 || value == DW_ACCESS_private)
14381 return (dwarf_access_attribute) value;
14382 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14383 plongest (value));
14384 }
14385
14386 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14387 {
14388 /* The default DWARF 2 accessibility for members is public, the default
14389 accessibility for inheritance is private. */
14390
14391 if (die->tag != DW_TAG_inheritance)
14392 return DW_ACCESS_public;
14393 else
14394 return DW_ACCESS_private;
14395 }
14396 else
14397 {
14398 /* DWARF 3+ defines the default accessibility a different way. The same
14399 rules apply now for DW_TAG_inheritance as for the members and it only
14400 depends on the container kind. */
14401
14402 if (die->parent->tag == DW_TAG_class_type)
14403 return DW_ACCESS_private;
14404 else
14405 return DW_ACCESS_public;
14406 }
14407 }
14408
14409 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
14410 *OFFSET to the byte offset. If the attribute was not found return
14411 0, otherwise return 1. If it was found but could not properly be
14412 handled, set *OFFSET to 0. */
14413
14414 static int
14415 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14416 LONGEST *offset)
14417 {
14418 struct attribute *attr;
14419
14420 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14421 if (attr != NULL)
14422 {
14423 *offset = 0;
14424
14425 /* Note that we do not check for a section offset first here.
14426 This is because DW_AT_data_member_location is new in DWARF 4,
14427 so if we see it, we can assume that a constant form is really
14428 a constant and not a section offset. */
14429 if (attr->form_is_constant ())
14430 *offset = attr->constant_value (0);
14431 else if (attr->form_is_section_offset ())
14432 dwarf2_complex_location_expr_complaint ();
14433 else if (attr->form_is_block ())
14434 *offset = decode_locdesc (attr->as_block (), cu);
14435 else
14436 dwarf2_complex_location_expr_complaint ();
14437
14438 return 1;
14439 }
14440 else
14441 {
14442 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14443 if (attr != nullptr)
14444 {
14445 *offset = attr->constant_value (0);
14446 return 1;
14447 }
14448 }
14449
14450 return 0;
14451 }
14452
14453 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
14454 store the results in FIELD. */
14455
14456 static void
14457 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14458 struct field *field)
14459 {
14460 struct attribute *attr;
14461
14462 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14463 if (attr != NULL)
14464 {
14465 if (attr->form_is_constant ())
14466 {
14467 LONGEST offset = attr->constant_value (0);
14468
14469 /* Work around this GCC 11 bug, where it would erroneously use -1
14470 data member locations, instead of 0:
14471
14472 Negative DW_AT_data_member_location
14473 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
14474 */
14475 if (offset == -1 && cu->producer_is_gcc_11)
14476 {
14477 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
14478 offset = 0;
14479 }
14480
14481 field->set_loc_bitpos (offset * bits_per_byte);
14482 }
14483 else if (attr->form_is_section_offset ())
14484 dwarf2_complex_location_expr_complaint ();
14485 else if (attr->form_is_block ())
14486 {
14487 bool handled;
14488 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14489 if (handled)
14490 field->set_loc_bitpos (offset * bits_per_byte);
14491 else
14492 {
14493 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14494 struct objfile *objfile = per_objfile->objfile;
14495 struct dwarf2_locexpr_baton *dlbaton
14496 = XOBNEW (&objfile->objfile_obstack,
14497 struct dwarf2_locexpr_baton);
14498 dlbaton->data = attr->as_block ()->data;
14499 dlbaton->size = attr->as_block ()->size;
14500 /* When using this baton, we want to compute the address
14501 of the field, not the value. This is why
14502 is_reference is set to false here. */
14503 dlbaton->is_reference = false;
14504 dlbaton->per_objfile = per_objfile;
14505 dlbaton->per_cu = cu->per_cu;
14506
14507 field->set_loc_dwarf_block (dlbaton);
14508 }
14509 }
14510 else
14511 dwarf2_complex_location_expr_complaint ();
14512 }
14513 else
14514 {
14515 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14516 if (attr != nullptr)
14517 field->set_loc_bitpos (attr->constant_value (0));
14518 }
14519 }
14520
14521 /* Add an aggregate field to the field list. */
14522
14523 static void
14524 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14525 struct dwarf2_cu *cu)
14526 {
14527 struct objfile *objfile = cu->per_objfile->objfile;
14528 struct gdbarch *gdbarch = objfile->arch ();
14529 struct nextfield *new_field;
14530 struct attribute *attr;
14531 struct field *fp;
14532 const char *fieldname = "";
14533
14534 if (die->tag == DW_TAG_inheritance)
14535 {
14536 fip->baseclasses.emplace_back ();
14537 new_field = &fip->baseclasses.back ();
14538 }
14539 else
14540 {
14541 fip->fields.emplace_back ();
14542 new_field = &fip->fields.back ();
14543 }
14544
14545 new_field->offset = die->sect_off;
14546
14547 new_field->accessibility = dwarf2_access_attribute (die, cu);
14548 if (new_field->accessibility != DW_ACCESS_public)
14549 fip->non_public_fields = true;
14550
14551 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14552 if (attr != nullptr)
14553 new_field->virtuality = attr->as_virtuality ();
14554 else
14555 new_field->virtuality = DW_VIRTUALITY_none;
14556
14557 fp = &new_field->field;
14558
14559 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14560 {
14561 /* Data member other than a C++ static data member. */
14562
14563 /* Get type of field. */
14564 fp->set_type (die_type (die, cu));
14565
14566 fp->set_loc_bitpos (0);
14567
14568 /* Get bit size of field (zero if none). */
14569 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14570 if (attr != nullptr)
14571 {
14572 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14573 }
14574 else
14575 {
14576 FIELD_BITSIZE (*fp) = 0;
14577 }
14578
14579 /* Get bit offset of field. */
14580 handle_member_location (die, cu, fp);
14581 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14582 if (attr != nullptr && attr->form_is_constant ())
14583 {
14584 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14585 {
14586 /* For big endian bits, the DW_AT_bit_offset gives the
14587 additional bit offset from the MSB of the containing
14588 anonymous object to the MSB of the field. We don't
14589 have to do anything special since we don't need to
14590 know the size of the anonymous object. */
14591 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
14592 }
14593 else
14594 {
14595 /* For little endian bits, compute the bit offset to the
14596 MSB of the anonymous object, subtract off the number of
14597 bits from the MSB of the field to the MSB of the
14598 object, and then subtract off the number of bits of
14599 the field itself. The result is the bit offset of
14600 the LSB of the field. */
14601 int anonymous_size;
14602 int bit_offset = attr->constant_value (0);
14603
14604 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14605 if (attr != nullptr && attr->form_is_constant ())
14606 {
14607 /* The size of the anonymous object containing
14608 the bit field is explicit, so use the
14609 indicated size (in bytes). */
14610 anonymous_size = attr->constant_value (0);
14611 }
14612 else
14613 {
14614 /* The size of the anonymous object containing
14615 the bit field must be inferred from the type
14616 attribute of the data member containing the
14617 bit field. */
14618 anonymous_size = TYPE_LENGTH (fp->type ());
14619 }
14620 fp->set_loc_bitpos (fp->loc_bitpos ()
14621 + anonymous_size * bits_per_byte
14622 - bit_offset - FIELD_BITSIZE (*fp));
14623 }
14624 }
14625
14626 /* Get name of field. */
14627 fieldname = dwarf2_name (die, cu);
14628 if (fieldname == NULL)
14629 fieldname = "";
14630
14631 /* The name is already allocated along with this objfile, so we don't
14632 need to duplicate it for the type. */
14633 fp->set_name (fieldname);
14634
14635 /* Change accessibility for artificial fields (e.g. virtual table
14636 pointer or virtual base class pointer) to private. */
14637 if (dwarf2_attr (die, DW_AT_artificial, cu))
14638 {
14639 FIELD_ARTIFICIAL (*fp) = 1;
14640 new_field->accessibility = DW_ACCESS_private;
14641 fip->non_public_fields = true;
14642 }
14643 }
14644 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14645 {
14646 /* C++ static member. */
14647
14648 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14649 is a declaration, but all versions of G++ as of this writing
14650 (so through at least 3.2.1) incorrectly generate
14651 DW_TAG_variable tags. */
14652
14653 const char *physname;
14654
14655 /* Get name of field. */
14656 fieldname = dwarf2_name (die, cu);
14657 if (fieldname == NULL)
14658 return;
14659
14660 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14661 if (attr
14662 /* Only create a symbol if this is an external value.
14663 new_symbol checks this and puts the value in the global symbol
14664 table, which we want. If it is not external, new_symbol
14665 will try to put the value in cu->list_in_scope which is wrong. */
14666 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14667 {
14668 /* A static const member, not much different than an enum as far as
14669 we're concerned, except that we can support more types. */
14670 new_symbol (die, NULL, cu);
14671 }
14672
14673 /* Get physical name. */
14674 physname = dwarf2_physname (fieldname, die, cu);
14675
14676 /* The name is already allocated along with this objfile, so we don't
14677 need to duplicate it for the type. */
14678 fp->set_loc_physname (physname ? physname : "");
14679 fp->set_type (die_type (die, cu));
14680 fp->set_name (fieldname);
14681 }
14682 else if (die->tag == DW_TAG_inheritance)
14683 {
14684 /* C++ base class field. */
14685 handle_member_location (die, cu, fp);
14686 FIELD_BITSIZE (*fp) = 0;
14687 fp->set_type (die_type (die, cu));
14688 fp->set_name (fp->type ()->name ());
14689 }
14690 else
14691 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14692 }
14693
14694 /* Can the type given by DIE define another type? */
14695
14696 static bool
14697 type_can_define_types (const struct die_info *die)
14698 {
14699 switch (die->tag)
14700 {
14701 case DW_TAG_typedef:
14702 case DW_TAG_class_type:
14703 case DW_TAG_structure_type:
14704 case DW_TAG_union_type:
14705 case DW_TAG_enumeration_type:
14706 return true;
14707
14708 default:
14709 return false;
14710 }
14711 }
14712
14713 /* Add a type definition defined in the scope of the FIP's class. */
14714
14715 static void
14716 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14717 struct dwarf2_cu *cu)
14718 {
14719 struct decl_field fp;
14720 memset (&fp, 0, sizeof (fp));
14721
14722 gdb_assert (type_can_define_types (die));
14723
14724 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14725 fp.name = dwarf2_name (die, cu);
14726 fp.type = read_type_die (die, cu);
14727
14728 /* Save accessibility. */
14729 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14730 switch (accessibility)
14731 {
14732 case DW_ACCESS_public:
14733 /* The assumed value if neither private nor protected. */
14734 break;
14735 case DW_ACCESS_private:
14736 fp.is_private = 1;
14737 break;
14738 case DW_ACCESS_protected:
14739 fp.is_protected = 1;
14740 break;
14741 }
14742
14743 if (die->tag == DW_TAG_typedef)
14744 fip->typedef_field_list.push_back (fp);
14745 else
14746 fip->nested_types_list.push_back (fp);
14747 }
14748
14749 /* A convenience typedef that's used when finding the discriminant
14750 field for a variant part. */
14751 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14752 offset_map_type;
14753
14754 /* Compute the discriminant range for a given variant. OBSTACK is
14755 where the results will be stored. VARIANT is the variant to
14756 process. IS_UNSIGNED indicates whether the discriminant is signed
14757 or unsigned. */
14758
14759 static const gdb::array_view<discriminant_range>
14760 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14761 bool is_unsigned)
14762 {
14763 std::vector<discriminant_range> ranges;
14764
14765 if (variant.default_branch)
14766 return {};
14767
14768 if (variant.discr_list_data == nullptr)
14769 {
14770 discriminant_range r
14771 = {variant.discriminant_value, variant.discriminant_value};
14772 ranges.push_back (r);
14773 }
14774 else
14775 {
14776 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14777 variant.discr_list_data->size);
14778 while (!data.empty ())
14779 {
14780 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14781 {
14782 complaint (_("invalid discriminant marker: %d"), data[0]);
14783 break;
14784 }
14785 bool is_range = data[0] == DW_DSC_range;
14786 data = data.slice (1);
14787
14788 ULONGEST low, high;
14789 unsigned int bytes_read;
14790
14791 if (data.empty ())
14792 {
14793 complaint (_("DW_AT_discr_list missing low value"));
14794 break;
14795 }
14796 if (is_unsigned)
14797 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14798 else
14799 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14800 &bytes_read);
14801 data = data.slice (bytes_read);
14802
14803 if (is_range)
14804 {
14805 if (data.empty ())
14806 {
14807 complaint (_("DW_AT_discr_list missing high value"));
14808 break;
14809 }
14810 if (is_unsigned)
14811 high = read_unsigned_leb128 (nullptr, data.data (),
14812 &bytes_read);
14813 else
14814 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14815 &bytes_read);
14816 data = data.slice (bytes_read);
14817 }
14818 else
14819 high = low;
14820
14821 ranges.push_back ({ low, high });
14822 }
14823 }
14824
14825 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14826 ranges.size ());
14827 std::copy (ranges.begin (), ranges.end (), result);
14828 return gdb::array_view<discriminant_range> (result, ranges.size ());
14829 }
14830
14831 static const gdb::array_view<variant_part> create_variant_parts
14832 (struct obstack *obstack,
14833 const offset_map_type &offset_map,
14834 struct field_info *fi,
14835 const std::vector<variant_part_builder> &variant_parts);
14836
14837 /* Fill in a "struct variant" for a given variant field. RESULT is
14838 the variant to fill in. OBSTACK is where any needed allocations
14839 will be done. OFFSET_MAP holds the mapping from section offsets to
14840 fields for the type. FI describes the fields of the type we're
14841 processing. FIELD is the variant field we're converting. */
14842
14843 static void
14844 create_one_variant (variant &result, struct obstack *obstack,
14845 const offset_map_type &offset_map,
14846 struct field_info *fi, const variant_field &field)
14847 {
14848 result.discriminants = convert_variant_range (obstack, field, false);
14849 result.first_field = field.first_field + fi->baseclasses.size ();
14850 result.last_field = field.last_field + fi->baseclasses.size ();
14851 result.parts = create_variant_parts (obstack, offset_map, fi,
14852 field.variant_parts);
14853 }
14854
14855 /* Fill in a "struct variant_part" for a given variant part. RESULT
14856 is the variant part to fill in. OBSTACK is where any needed
14857 allocations will be done. OFFSET_MAP holds the mapping from
14858 section offsets to fields for the type. FI describes the fields of
14859 the type we're processing. BUILDER is the variant part to be
14860 converted. */
14861
14862 static void
14863 create_one_variant_part (variant_part &result,
14864 struct obstack *obstack,
14865 const offset_map_type &offset_map,
14866 struct field_info *fi,
14867 const variant_part_builder &builder)
14868 {
14869 auto iter = offset_map.find (builder.discriminant_offset);
14870 if (iter == offset_map.end ())
14871 {
14872 result.discriminant_index = -1;
14873 /* Doesn't matter. */
14874 result.is_unsigned = false;
14875 }
14876 else
14877 {
14878 result.discriminant_index = iter->second;
14879 result.is_unsigned
14880 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14881 }
14882
14883 size_t n = builder.variants.size ();
14884 variant *output = new (obstack) variant[n];
14885 for (size_t i = 0; i < n; ++i)
14886 create_one_variant (output[i], obstack, offset_map, fi,
14887 builder.variants[i]);
14888
14889 result.variants = gdb::array_view<variant> (output, n);
14890 }
14891
14892 /* Create a vector of variant parts that can be attached to a type.
14893 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14894 holds the mapping from section offsets to fields for the type. FI
14895 describes the fields of the type we're processing. VARIANT_PARTS
14896 is the vector to convert. */
14897
14898 static const gdb::array_view<variant_part>
14899 create_variant_parts (struct obstack *obstack,
14900 const offset_map_type &offset_map,
14901 struct field_info *fi,
14902 const std::vector<variant_part_builder> &variant_parts)
14903 {
14904 if (variant_parts.empty ())
14905 return {};
14906
14907 size_t n = variant_parts.size ();
14908 variant_part *result = new (obstack) variant_part[n];
14909 for (size_t i = 0; i < n; ++i)
14910 create_one_variant_part (result[i], obstack, offset_map, fi,
14911 variant_parts[i]);
14912
14913 return gdb::array_view<variant_part> (result, n);
14914 }
14915
14916 /* Compute the variant part vector for FIP, attaching it to TYPE when
14917 done. */
14918
14919 static void
14920 add_variant_property (struct field_info *fip, struct type *type,
14921 struct dwarf2_cu *cu)
14922 {
14923 /* Map section offsets of fields to their field index. Note the
14924 field index here does not take the number of baseclasses into
14925 account. */
14926 offset_map_type offset_map;
14927 for (int i = 0; i < fip->fields.size (); ++i)
14928 offset_map[fip->fields[i].offset] = i;
14929
14930 struct objfile *objfile = cu->per_objfile->objfile;
14931 gdb::array_view<const variant_part> parts
14932 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14933 fip->variant_parts);
14934
14935 struct dynamic_prop prop;
14936 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14937 obstack_copy (&objfile->objfile_obstack, &parts,
14938 sizeof (parts)));
14939
14940 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14941 }
14942
14943 /* Create the vector of fields, and attach it to the type. */
14944
14945 static void
14946 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14947 struct dwarf2_cu *cu)
14948 {
14949 int nfields = fip->nfields ();
14950
14951 /* Record the field count, allocate space for the array of fields,
14952 and create blank accessibility bitfields if necessary. */
14953 type->set_num_fields (nfields);
14954 type->set_fields
14955 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14956
14957 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
14958 {
14959 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14960
14961 TYPE_FIELD_PRIVATE_BITS (type) =
14962 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14963 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14964
14965 TYPE_FIELD_PROTECTED_BITS (type) =
14966 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14967 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14968
14969 TYPE_FIELD_IGNORE_BITS (type) =
14970 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14971 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14972 }
14973
14974 /* If the type has baseclasses, allocate and clear a bit vector for
14975 TYPE_FIELD_VIRTUAL_BITS. */
14976 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
14977 {
14978 int num_bytes = B_BYTES (fip->baseclasses.size ());
14979 unsigned char *pointer;
14980
14981 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14982 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14983 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14984 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14985 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14986 }
14987
14988 if (!fip->variant_parts.empty ())
14989 add_variant_property (fip, type, cu);
14990
14991 /* Copy the saved-up fields into the field vector. */
14992 for (int i = 0; i < nfields; ++i)
14993 {
14994 struct nextfield &field
14995 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14996 : fip->fields[i - fip->baseclasses.size ()]);
14997
14998 type->field (i) = field.field;
14999 switch (field.accessibility)
15000 {
15001 case DW_ACCESS_private:
15002 if (cu->per_cu->lang != language_ada)
15003 SET_TYPE_FIELD_PRIVATE (type, i);
15004 break;
15005
15006 case DW_ACCESS_protected:
15007 if (cu->per_cu->lang != language_ada)
15008 SET_TYPE_FIELD_PROTECTED (type, i);
15009 break;
15010
15011 case DW_ACCESS_public:
15012 break;
15013
15014 default:
15015 /* Unknown accessibility. Complain and treat it as public. */
15016 {
15017 complaint (_("unsupported accessibility %d"),
15018 field.accessibility);
15019 }
15020 break;
15021 }
15022 if (i < fip->baseclasses.size ())
15023 {
15024 switch (field.virtuality)
15025 {
15026 case DW_VIRTUALITY_virtual:
15027 case DW_VIRTUALITY_pure_virtual:
15028 if (cu->per_cu->lang == language_ada)
15029 error (_("unexpected virtuality in component of Ada type"));
15030 SET_TYPE_FIELD_VIRTUAL (type, i);
15031 break;
15032 }
15033 }
15034 }
15035 }
15036
15037 /* Return true if this member function is a constructor, false
15038 otherwise. */
15039
15040 static int
15041 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15042 {
15043 const char *fieldname;
15044 const char *type_name;
15045 int len;
15046
15047 if (die->parent == NULL)
15048 return 0;
15049
15050 if (die->parent->tag != DW_TAG_structure_type
15051 && die->parent->tag != DW_TAG_union_type
15052 && die->parent->tag != DW_TAG_class_type)
15053 return 0;
15054
15055 fieldname = dwarf2_name (die, cu);
15056 type_name = dwarf2_name (die->parent, cu);
15057 if (fieldname == NULL || type_name == NULL)
15058 return 0;
15059
15060 len = strlen (fieldname);
15061 return (strncmp (fieldname, type_name, len) == 0
15062 && (type_name[len] == '\0' || type_name[len] == '<'));
15063 }
15064
15065 /* Add a member function to the proper fieldlist. */
15066
15067 static void
15068 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15069 struct type *type, struct dwarf2_cu *cu)
15070 {
15071 struct objfile *objfile = cu->per_objfile->objfile;
15072 struct attribute *attr;
15073 int i;
15074 struct fnfieldlist *flp = nullptr;
15075 struct fn_field *fnp;
15076 const char *fieldname;
15077 struct type *this_type;
15078
15079 if (cu->per_cu->lang == language_ada)
15080 error (_("unexpected member function in Ada type"));
15081
15082 /* Get name of member function. */
15083 fieldname = dwarf2_name (die, cu);
15084 if (fieldname == NULL)
15085 return;
15086
15087 /* Look up member function name in fieldlist. */
15088 for (i = 0; i < fip->fnfieldlists.size (); i++)
15089 {
15090 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15091 {
15092 flp = &fip->fnfieldlists[i];
15093 break;
15094 }
15095 }
15096
15097 /* Create a new fnfieldlist if necessary. */
15098 if (flp == nullptr)
15099 {
15100 fip->fnfieldlists.emplace_back ();
15101 flp = &fip->fnfieldlists.back ();
15102 flp->name = fieldname;
15103 i = fip->fnfieldlists.size () - 1;
15104 }
15105
15106 /* Create a new member function field and add it to the vector of
15107 fnfieldlists. */
15108 flp->fnfields.emplace_back ();
15109 fnp = &flp->fnfields.back ();
15110
15111 /* Delay processing of the physname until later. */
15112 if (cu->per_cu->lang == language_cplus)
15113 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15114 die, cu);
15115 else
15116 {
15117 const char *physname = dwarf2_physname (fieldname, die, cu);
15118 fnp->physname = physname ? physname : "";
15119 }
15120
15121 fnp->type = alloc_type (objfile);
15122 this_type = read_type_die (die, cu);
15123 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15124 {
15125 int nparams = this_type->num_fields ();
15126
15127 /* TYPE is the domain of this method, and THIS_TYPE is the type
15128 of the method itself (TYPE_CODE_METHOD). */
15129 smash_to_method_type (fnp->type, type,
15130 TYPE_TARGET_TYPE (this_type),
15131 this_type->fields (),
15132 this_type->num_fields (),
15133 this_type->has_varargs ());
15134
15135 /* Handle static member functions.
15136 Dwarf2 has no clean way to discern C++ static and non-static
15137 member functions. G++ helps GDB by marking the first
15138 parameter for non-static member functions (which is the this
15139 pointer) as artificial. We obtain this information from
15140 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15141 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15142 fnp->voffset = VOFFSET_STATIC;
15143 }
15144 else
15145 complaint (_("member function type missing for '%s'"),
15146 dwarf2_full_name (fieldname, die, cu));
15147
15148 /* Get fcontext from DW_AT_containing_type if present. */
15149 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15150 fnp->fcontext = die_containing_type (die, cu);
15151
15152 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15153 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15154
15155 /* Get accessibility. */
15156 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15157 switch (accessibility)
15158 {
15159 case DW_ACCESS_private:
15160 fnp->is_private = 1;
15161 break;
15162 case DW_ACCESS_protected:
15163 fnp->is_protected = 1;
15164 break;
15165 }
15166
15167 /* Check for artificial methods. */
15168 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15169 if (attr && attr->as_boolean ())
15170 fnp->is_artificial = 1;
15171
15172 /* Check for defaulted methods. */
15173 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15174 if (attr != nullptr)
15175 fnp->defaulted = attr->defaulted ();
15176
15177 /* Check for deleted methods. */
15178 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15179 if (attr != nullptr && attr->as_boolean ())
15180 fnp->is_deleted = 1;
15181
15182 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15183
15184 /* Get index in virtual function table if it is a virtual member
15185 function. For older versions of GCC, this is an offset in the
15186 appropriate virtual table, as specified by DW_AT_containing_type.
15187 For everyone else, it is an expression to be evaluated relative
15188 to the object address. */
15189
15190 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15191 if (attr != nullptr)
15192 {
15193 if (attr->form_is_block () && attr->as_block ()->size > 0)
15194 {
15195 struct dwarf_block *block = attr->as_block ();
15196
15197 if (block->data[0] == DW_OP_constu)
15198 {
15199 /* Old-style GCC. */
15200 fnp->voffset = decode_locdesc (block, cu) + 2;
15201 }
15202 else if (block->data[0] == DW_OP_deref
15203 || (block->size > 1
15204 && block->data[0] == DW_OP_deref_size
15205 && block->data[1] == cu->header.addr_size))
15206 {
15207 fnp->voffset = decode_locdesc (block, cu);
15208 if ((fnp->voffset % cu->header.addr_size) != 0)
15209 dwarf2_complex_location_expr_complaint ();
15210 else
15211 fnp->voffset /= cu->header.addr_size;
15212 fnp->voffset += 2;
15213 }
15214 else
15215 dwarf2_complex_location_expr_complaint ();
15216
15217 if (!fnp->fcontext)
15218 {
15219 /* If there is no `this' field and no DW_AT_containing_type,
15220 we cannot actually find a base class context for the
15221 vtable! */
15222 if (this_type->num_fields () == 0
15223 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15224 {
15225 complaint (_("cannot determine context for virtual member "
15226 "function \"%s\" (offset %s)"),
15227 fieldname, sect_offset_str (die->sect_off));
15228 }
15229 else
15230 {
15231 fnp->fcontext
15232 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15233 }
15234 }
15235 }
15236 else if (attr->form_is_section_offset ())
15237 {
15238 dwarf2_complex_location_expr_complaint ();
15239 }
15240 else
15241 {
15242 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15243 fieldname);
15244 }
15245 }
15246 else
15247 {
15248 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15249 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15250 {
15251 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15252 complaint (_("Member function \"%s\" (offset %s) is virtual "
15253 "but the vtable offset is not specified"),
15254 fieldname, sect_offset_str (die->sect_off));
15255 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15256 TYPE_CPLUS_DYNAMIC (type) = 1;
15257 }
15258 }
15259 }
15260
15261 /* Create the vector of member function fields, and attach it to the type. */
15262
15263 static void
15264 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15265 struct dwarf2_cu *cu)
15266 {
15267 if (cu->per_cu->lang == language_ada)
15268 error (_("unexpected member functions in Ada type"));
15269
15270 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15271 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15272 TYPE_ALLOC (type,
15273 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15274
15275 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15276 {
15277 struct fnfieldlist &nf = fip->fnfieldlists[i];
15278 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15279
15280 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15281 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15282 fn_flp->fn_fields = (struct fn_field *)
15283 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15284
15285 for (int k = 0; k < nf.fnfields.size (); ++k)
15286 fn_flp->fn_fields[k] = nf.fnfields[k];
15287 }
15288
15289 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15290 }
15291
15292 /* Returns non-zero if NAME is the name of a vtable member in CU's
15293 language, zero otherwise. */
15294 static int
15295 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15296 {
15297 static const char vptr[] = "_vptr";
15298
15299 /* Look for the C++ form of the vtable. */
15300 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15301 return 1;
15302
15303 return 0;
15304 }
15305
15306 /* GCC outputs unnamed structures that are really pointers to member
15307 functions, with the ABI-specified layout. If TYPE describes
15308 such a structure, smash it into a member function type.
15309
15310 GCC shouldn't do this; it should just output pointer to member DIEs.
15311 This is GCC PR debug/28767. */
15312
15313 static void
15314 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15315 {
15316 struct type *pfn_type, *self_type, *new_type;
15317
15318 /* Check for a structure with no name and two children. */
15319 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15320 return;
15321
15322 /* Check for __pfn and __delta members. */
15323 if (type->field (0).name () == NULL
15324 || strcmp (type->field (0).name (), "__pfn") != 0
15325 || type->field (1).name () == NULL
15326 || strcmp (type->field (1).name (), "__delta") != 0)
15327 return;
15328
15329 /* Find the type of the method. */
15330 pfn_type = type->field (0).type ();
15331 if (pfn_type == NULL
15332 || pfn_type->code () != TYPE_CODE_PTR
15333 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15334 return;
15335
15336 /* Look for the "this" argument. */
15337 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15338 if (pfn_type->num_fields () == 0
15339 /* || pfn_type->field (0).type () == NULL */
15340 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15341 return;
15342
15343 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15344 new_type = alloc_type (objfile);
15345 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15346 pfn_type->fields (), pfn_type->num_fields (),
15347 pfn_type->has_varargs ());
15348 smash_to_methodptr_type (type, new_type);
15349 }
15350
15351 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15352 requires rewriting, then copy it and return the updated copy.
15353 Otherwise return nullptr. */
15354
15355 static struct type *
15356 rewrite_array_type (struct type *type)
15357 {
15358 if (type->code () != TYPE_CODE_ARRAY)
15359 return nullptr;
15360
15361 struct type *index_type = type->index_type ();
15362 range_bounds *current_bounds = index_type->bounds ();
15363
15364 /* Handle multi-dimensional arrays. */
15365 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15366 if (new_target == nullptr)
15367 {
15368 /* Maybe we don't need to rewrite this array. */
15369 if (current_bounds->low.kind () == PROP_CONST
15370 && current_bounds->high.kind () == PROP_CONST)
15371 return nullptr;
15372 }
15373
15374 /* Either the target type was rewritten, or the bounds have to be
15375 updated. Either way we want to copy the type and update
15376 everything. */
15377 struct type *copy = copy_type (type);
15378 int nfields = copy->num_fields ();
15379 field *new_fields
15380 = ((struct field *) TYPE_ZALLOC (copy,
15381 nfields * sizeof (struct field)));
15382 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15383 copy->set_fields (new_fields);
15384 if (new_target != nullptr)
15385 TYPE_TARGET_TYPE (copy) = new_target;
15386
15387 struct type *index_copy = copy_type (index_type);
15388 range_bounds *bounds
15389 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15390 sizeof (range_bounds));
15391 *bounds = *current_bounds;
15392 bounds->low.set_const_val (1);
15393 bounds->high.set_const_val (0);
15394 index_copy->set_bounds (bounds);
15395 copy->set_index_type (index_copy);
15396
15397 return copy;
15398 }
15399
15400 /* While some versions of GCC will generate complicated DWARF for an
15401 array (see quirk_ada_thick_pointer), more recent versions were
15402 modified to emit an explicit thick pointer structure. However, in
15403 this case, the array still has DWARF expressions for its ranges,
15404 and these must be ignored. */
15405
15406 static void
15407 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15408 struct type *type)
15409 {
15410 gdb_assert (cu->per_cu->lang == language_ada);
15411
15412 /* Check for a structure with two children. */
15413 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15414 return;
15415
15416 /* Check for P_ARRAY and P_BOUNDS members. */
15417 if (type->field (0).name () == NULL
15418 || strcmp (type->field (0).name (), "P_ARRAY") != 0
15419 || type->field (1).name () == NULL
15420 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
15421 return;
15422
15423 /* Make sure we're looking at a pointer to an array. */
15424 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15425 return;
15426
15427 /* The Ada code already knows how to handle these types, so all that
15428 we need to do is turn the bounds into static bounds. However, we
15429 don't want to rewrite existing array or index types in-place,
15430 because those may be referenced in other contexts where this
15431 rewriting is undesirable. */
15432 struct type *new_ary_type
15433 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15434 if (new_ary_type != nullptr)
15435 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15436 }
15437
15438 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15439 appropriate error checking and issuing complaints if there is a
15440 problem. */
15441
15442 static ULONGEST
15443 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15444 {
15445 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15446
15447 if (attr == nullptr)
15448 return 0;
15449
15450 if (!attr->form_is_constant ())
15451 {
15452 complaint (_("DW_AT_alignment must have constant form"
15453 " - DIE at %s [in module %s]"),
15454 sect_offset_str (die->sect_off),
15455 objfile_name (cu->per_objfile->objfile));
15456 return 0;
15457 }
15458
15459 LONGEST val = attr->constant_value (0);
15460 if (val < 0)
15461 {
15462 complaint (_("DW_AT_alignment value must not be negative"
15463 " - DIE at %s [in module %s]"),
15464 sect_offset_str (die->sect_off),
15465 objfile_name (cu->per_objfile->objfile));
15466 return 0;
15467 }
15468 ULONGEST align = val;
15469
15470 if (align == 0)
15471 {
15472 complaint (_("DW_AT_alignment value must not be zero"
15473 " - DIE at %s [in module %s]"),
15474 sect_offset_str (die->sect_off),
15475 objfile_name (cu->per_objfile->objfile));
15476 return 0;
15477 }
15478 if ((align & (align - 1)) != 0)
15479 {
15480 complaint (_("DW_AT_alignment value must be a power of 2"
15481 " - DIE at %s [in module %s]"),
15482 sect_offset_str (die->sect_off),
15483 objfile_name (cu->per_objfile->objfile));
15484 return 0;
15485 }
15486
15487 return align;
15488 }
15489
15490 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15491 the alignment for TYPE. */
15492
15493 static void
15494 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15495 struct type *type)
15496 {
15497 if (!set_type_align (type, get_alignment (cu, die)))
15498 complaint (_("DW_AT_alignment value too large"
15499 " - DIE at %s [in module %s]"),
15500 sect_offset_str (die->sect_off),
15501 objfile_name (cu->per_objfile->objfile));
15502 }
15503
15504 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15505 constant for a type, according to DWARF5 spec, Table 5.5. */
15506
15507 static bool
15508 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15509 {
15510 switch (value)
15511 {
15512 case DW_CC_normal:
15513 case DW_CC_pass_by_reference:
15514 case DW_CC_pass_by_value:
15515 return true;
15516
15517 default:
15518 complaint (_("unrecognized DW_AT_calling_convention value "
15519 "(%s) for a type"), pulongest (value));
15520 return false;
15521 }
15522 }
15523
15524 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15525 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15526 also according to GNU-specific values (see include/dwarf2.h). */
15527
15528 static bool
15529 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15530 {
15531 switch (value)
15532 {
15533 case DW_CC_normal:
15534 case DW_CC_program:
15535 case DW_CC_nocall:
15536 return true;
15537
15538 case DW_CC_GNU_renesas_sh:
15539 case DW_CC_GNU_borland_fastcall_i386:
15540 case DW_CC_GDB_IBM_OpenCL:
15541 return true;
15542
15543 default:
15544 complaint (_("unrecognized DW_AT_calling_convention value "
15545 "(%s) for a subroutine"), pulongest (value));
15546 return false;
15547 }
15548 }
15549
15550 /* Called when we find the DIE that starts a structure or union scope
15551 (definition) to create a type for the structure or union. Fill in
15552 the type's name and general properties; the members will not be
15553 processed until process_structure_scope. A symbol table entry for
15554 the type will also not be done until process_structure_scope (assuming
15555 the type has a name).
15556
15557 NOTE: we need to call these functions regardless of whether or not the
15558 DIE has a DW_AT_name attribute, since it might be an anonymous
15559 structure or union. This gets the type entered into our set of
15560 user defined types. */
15561
15562 static struct type *
15563 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15564 {
15565 struct objfile *objfile = cu->per_objfile->objfile;
15566 struct type *type;
15567 struct attribute *attr;
15568 const char *name;
15569
15570 /* If the definition of this type lives in .debug_types, read that type.
15571 Don't follow DW_AT_specification though, that will take us back up
15572 the chain and we want to go down. */
15573 attr = die->attr (DW_AT_signature);
15574 if (attr != nullptr)
15575 {
15576 type = get_DW_AT_signature_type (die, attr, cu);
15577
15578 /* The type's CU may not be the same as CU.
15579 Ensure TYPE is recorded with CU in die_type_hash. */
15580 return set_die_type (die, type, cu);
15581 }
15582
15583 type = alloc_type (objfile);
15584 INIT_CPLUS_SPECIFIC (type);
15585
15586 name = dwarf2_name (die, cu);
15587 if (name != NULL)
15588 {
15589 if (cu->per_cu->lang == language_cplus
15590 || cu->per_cu->lang == language_d
15591 || cu->per_cu->lang == language_rust)
15592 {
15593 const char *full_name = dwarf2_full_name (name, die, cu);
15594
15595 /* dwarf2_full_name might have already finished building the DIE's
15596 type. If so, there is no need to continue. */
15597 if (get_die_type (die, cu) != NULL)
15598 return get_die_type (die, cu);
15599
15600 type->set_name (full_name);
15601 }
15602 else
15603 {
15604 /* The name is already allocated along with this objfile, so
15605 we don't need to duplicate it for the type. */
15606 type->set_name (name);
15607 }
15608 }
15609
15610 if (die->tag == DW_TAG_structure_type)
15611 {
15612 type->set_code (TYPE_CODE_STRUCT);
15613 }
15614 else if (die->tag == DW_TAG_union_type)
15615 {
15616 type->set_code (TYPE_CODE_UNION);
15617 }
15618 else
15619 {
15620 type->set_code (TYPE_CODE_STRUCT);
15621 }
15622
15623 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
15624 type->set_is_declared_class (true);
15625
15626 /* Store the calling convention in the type if it's available in
15627 the die. Otherwise the calling convention remains set to
15628 the default value DW_CC_normal. */
15629 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15630 if (attr != nullptr
15631 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15632 {
15633 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15634 TYPE_CPLUS_CALLING_CONVENTION (type)
15635 = (enum dwarf_calling_convention) (attr->constant_value (0));
15636 }
15637
15638 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15639 if (attr != nullptr)
15640 {
15641 if (attr->form_is_constant ())
15642 TYPE_LENGTH (type) = attr->constant_value (0);
15643 else
15644 {
15645 struct dynamic_prop prop;
15646 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15647 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15648 TYPE_LENGTH (type) = 0;
15649 }
15650 }
15651 else
15652 {
15653 TYPE_LENGTH (type) = 0;
15654 }
15655
15656 maybe_set_alignment (cu, die, type);
15657
15658 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15659 {
15660 /* ICC<14 does not output the required DW_AT_declaration on
15661 incomplete types, but gives them a size of zero. */
15662 type->set_is_stub (true);
15663 }
15664 else
15665 type->set_stub_is_supported (true);
15666
15667 if (die_is_declaration (die, cu))
15668 type->set_is_stub (true);
15669 else if (attr == NULL && die->child == NULL
15670 && producer_is_realview (cu->producer))
15671 /* RealView does not output the required DW_AT_declaration
15672 on incomplete types. */
15673 type->set_is_stub (true);
15674
15675 /* We need to add the type field to the die immediately so we don't
15676 infinitely recurse when dealing with pointers to the structure
15677 type within the structure itself. */
15678 set_die_type (die, type, cu);
15679
15680 /* set_die_type should be already done. */
15681 set_descriptive_type (type, die, cu);
15682
15683 return type;
15684 }
15685
15686 static void handle_struct_member_die
15687 (struct die_info *child_die,
15688 struct type *type,
15689 struct field_info *fi,
15690 std::vector<struct symbol *> *template_args,
15691 struct dwarf2_cu *cu);
15692
15693 /* A helper for handle_struct_member_die that handles
15694 DW_TAG_variant_part. */
15695
15696 static void
15697 handle_variant_part (struct die_info *die, struct type *type,
15698 struct field_info *fi,
15699 std::vector<struct symbol *> *template_args,
15700 struct dwarf2_cu *cu)
15701 {
15702 variant_part_builder *new_part;
15703 if (fi->current_variant_part == nullptr)
15704 {
15705 fi->variant_parts.emplace_back ();
15706 new_part = &fi->variant_parts.back ();
15707 }
15708 else if (!fi->current_variant_part->processing_variant)
15709 {
15710 complaint (_("nested DW_TAG_variant_part seen "
15711 "- DIE at %s [in module %s]"),
15712 sect_offset_str (die->sect_off),
15713 objfile_name (cu->per_objfile->objfile));
15714 return;
15715 }
15716 else
15717 {
15718 variant_field &current = fi->current_variant_part->variants.back ();
15719 current.variant_parts.emplace_back ();
15720 new_part = &current.variant_parts.back ();
15721 }
15722
15723 /* When we recurse, we want callees to add to this new variant
15724 part. */
15725 scoped_restore save_current_variant_part
15726 = make_scoped_restore (&fi->current_variant_part, new_part);
15727
15728 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15729 if (discr == NULL)
15730 {
15731 /* It's a univariant form, an extension we support. */
15732 }
15733 else if (discr->form_is_ref ())
15734 {
15735 struct dwarf2_cu *target_cu = cu;
15736 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15737
15738 new_part->discriminant_offset = target_die->sect_off;
15739 }
15740 else
15741 {
15742 complaint (_("DW_AT_discr does not have DIE reference form"
15743 " - DIE at %s [in module %s]"),
15744 sect_offset_str (die->sect_off),
15745 objfile_name (cu->per_objfile->objfile));
15746 }
15747
15748 for (die_info *child_die = die->child;
15749 child_die != NULL;
15750 child_die = child_die->sibling)
15751 handle_struct_member_die (child_die, type, fi, template_args, cu);
15752 }
15753
15754 /* A helper for handle_struct_member_die that handles
15755 DW_TAG_variant. */
15756
15757 static void
15758 handle_variant (struct die_info *die, struct type *type,
15759 struct field_info *fi,
15760 std::vector<struct symbol *> *template_args,
15761 struct dwarf2_cu *cu)
15762 {
15763 if (fi->current_variant_part == nullptr)
15764 {
15765 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15766 "- DIE at %s [in module %s]"),
15767 sect_offset_str (die->sect_off),
15768 objfile_name (cu->per_objfile->objfile));
15769 return;
15770 }
15771 if (fi->current_variant_part->processing_variant)
15772 {
15773 complaint (_("nested DW_TAG_variant seen "
15774 "- DIE at %s [in module %s]"),
15775 sect_offset_str (die->sect_off),
15776 objfile_name (cu->per_objfile->objfile));
15777 return;
15778 }
15779
15780 scoped_restore save_processing_variant
15781 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15782 true);
15783
15784 fi->current_variant_part->variants.emplace_back ();
15785 variant_field &variant = fi->current_variant_part->variants.back ();
15786 variant.first_field = fi->fields.size ();
15787
15788 /* In a variant we want to get the discriminant and also add a
15789 field for our sole member child. */
15790 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15791 if (discr == nullptr || !discr->form_is_constant ())
15792 {
15793 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15794 if (discr == nullptr || discr->as_block ()->size == 0)
15795 variant.default_branch = true;
15796 else
15797 variant.discr_list_data = discr->as_block ();
15798 }
15799 else
15800 variant.discriminant_value = discr->constant_value (0);
15801
15802 for (die_info *variant_child = die->child;
15803 variant_child != NULL;
15804 variant_child = variant_child->sibling)
15805 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15806
15807 variant.last_field = fi->fields.size ();
15808 }
15809
15810 /* A helper for process_structure_scope that handles a single member
15811 DIE. */
15812
15813 static void
15814 handle_struct_member_die (struct die_info *child_die, struct type *type,
15815 struct field_info *fi,
15816 std::vector<struct symbol *> *template_args,
15817 struct dwarf2_cu *cu)
15818 {
15819 if (child_die->tag == DW_TAG_member
15820 || child_die->tag == DW_TAG_variable)
15821 {
15822 /* NOTE: carlton/2002-11-05: A C++ static data member
15823 should be a DW_TAG_member that is a declaration, but
15824 all versions of G++ as of this writing (so through at
15825 least 3.2.1) incorrectly generate DW_TAG_variable
15826 tags for them instead. */
15827 dwarf2_add_field (fi, child_die, cu);
15828 }
15829 else if (child_die->tag == DW_TAG_subprogram)
15830 {
15831 /* Rust doesn't have member functions in the C++ sense.
15832 However, it does emit ordinary functions as children
15833 of a struct DIE. */
15834 if (cu->per_cu->lang == language_rust)
15835 read_func_scope (child_die, cu);
15836 else
15837 {
15838 /* C++ member function. */
15839 dwarf2_add_member_fn (fi, child_die, type, cu);
15840 }
15841 }
15842 else if (child_die->tag == DW_TAG_inheritance)
15843 {
15844 /* C++ base class field. */
15845 dwarf2_add_field (fi, child_die, cu);
15846 }
15847 else if (type_can_define_types (child_die))
15848 dwarf2_add_type_defn (fi, child_die, cu);
15849 else if (child_die->tag == DW_TAG_template_type_param
15850 || child_die->tag == DW_TAG_template_value_param)
15851 {
15852 struct symbol *arg = new_symbol (child_die, NULL, cu);
15853
15854 if (arg != NULL)
15855 template_args->push_back (arg);
15856 }
15857 else if (child_die->tag == DW_TAG_variant_part)
15858 handle_variant_part (child_die, type, fi, template_args, cu);
15859 else if (child_die->tag == DW_TAG_variant)
15860 handle_variant (child_die, type, fi, template_args, cu);
15861 }
15862
15863 /* Finish creating a structure or union type, including filling in
15864 its members and creating a symbol for it. */
15865
15866 static void
15867 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15868 {
15869 struct objfile *objfile = cu->per_objfile->objfile;
15870 struct die_info *child_die;
15871 struct type *type;
15872
15873 type = get_die_type (die, cu);
15874 if (type == NULL)
15875 type = read_structure_type (die, cu);
15876
15877 bool has_template_parameters = false;
15878 if (die->child != NULL && ! die_is_declaration (die, cu))
15879 {
15880 struct field_info fi;
15881 std::vector<struct symbol *> template_args;
15882
15883 child_die = die->child;
15884
15885 while (child_die && child_die->tag)
15886 {
15887 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15888 child_die = child_die->sibling;
15889 }
15890
15891 /* Attach template arguments to type. */
15892 if (!template_args.empty ())
15893 {
15894 has_template_parameters = true;
15895 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15896 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15897 TYPE_TEMPLATE_ARGUMENTS (type)
15898 = XOBNEWVEC (&objfile->objfile_obstack,
15899 struct symbol *,
15900 TYPE_N_TEMPLATE_ARGUMENTS (type));
15901 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15902 template_args.data (),
15903 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15904 * sizeof (struct symbol *)));
15905 }
15906
15907 /* Attach fields and member functions to the type. */
15908 if (fi.nfields () > 0)
15909 dwarf2_attach_fields_to_type (&fi, type, cu);
15910 if (!fi.fnfieldlists.empty ())
15911 {
15912 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15913
15914 /* Get the type which refers to the base class (possibly this
15915 class itself) which contains the vtable pointer for the current
15916 class from the DW_AT_containing_type attribute. This use of
15917 DW_AT_containing_type is a GNU extension. */
15918
15919 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15920 {
15921 struct type *t = die_containing_type (die, cu);
15922
15923 set_type_vptr_basetype (type, t);
15924 if (type == t)
15925 {
15926 int i;
15927
15928 /* Our own class provides vtbl ptr. */
15929 for (i = t->num_fields () - 1;
15930 i >= TYPE_N_BASECLASSES (t);
15931 --i)
15932 {
15933 const char *fieldname = t->field (i).name ();
15934
15935 if (is_vtable_name (fieldname, cu))
15936 {
15937 set_type_vptr_fieldno (type, i);
15938 break;
15939 }
15940 }
15941
15942 /* Complain if virtual function table field not found. */
15943 if (i < TYPE_N_BASECLASSES (t))
15944 complaint (_("virtual function table pointer "
15945 "not found when defining class '%s'"),
15946 type->name () ? type->name () : "");
15947 }
15948 else
15949 {
15950 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15951 }
15952 }
15953 else if (cu->producer
15954 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15955 {
15956 /* The IBM XLC compiler does not provide direct indication
15957 of the containing type, but the vtable pointer is
15958 always named __vfp. */
15959
15960 int i;
15961
15962 for (i = type->num_fields () - 1;
15963 i >= TYPE_N_BASECLASSES (type);
15964 --i)
15965 {
15966 if (strcmp (type->field (i).name (), "__vfp") == 0)
15967 {
15968 set_type_vptr_fieldno (type, i);
15969 set_type_vptr_basetype (type, type);
15970 break;
15971 }
15972 }
15973 }
15974 }
15975
15976 /* Copy fi.typedef_field_list linked list elements content into the
15977 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15978 if (!fi.typedef_field_list.empty ())
15979 {
15980 int count = fi.typedef_field_list.size ();
15981
15982 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15983 TYPE_TYPEDEF_FIELD_ARRAY (type)
15984 = ((struct decl_field *)
15985 TYPE_ALLOC (type,
15986 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15987 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15988
15989 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15990 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15991 }
15992
15993 /* Copy fi.nested_types_list linked list elements content into the
15994 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15995 if (!fi.nested_types_list.empty ()
15996 && cu->per_cu->lang != language_ada)
15997 {
15998 int count = fi.nested_types_list.size ();
15999
16000 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16001 TYPE_NESTED_TYPES_ARRAY (type)
16002 = ((struct decl_field *)
16003 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16004 TYPE_NESTED_TYPES_COUNT (type) = count;
16005
16006 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16007 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16008 }
16009 }
16010
16011 quirk_gcc_member_function_pointer (type, objfile);
16012 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
16013 cu->rust_unions.push_back (type);
16014 else if (cu->per_cu->lang == language_ada)
16015 quirk_ada_thick_pointer_struct (die, cu, type);
16016
16017 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16018 snapshots) has been known to create a die giving a declaration
16019 for a class that has, as a child, a die giving a definition for a
16020 nested class. So we have to process our children even if the
16021 current die is a declaration. Normally, of course, a declaration
16022 won't have any children at all. */
16023
16024 child_die = die->child;
16025
16026 while (child_die != NULL && child_die->tag)
16027 {
16028 if (child_die->tag == DW_TAG_member
16029 || child_die->tag == DW_TAG_variable
16030 || child_die->tag == DW_TAG_inheritance
16031 || child_die->tag == DW_TAG_template_value_param
16032 || child_die->tag == DW_TAG_template_type_param)
16033 {
16034 /* Do nothing. */
16035 }
16036 else
16037 process_die (child_die, cu);
16038
16039 child_die = child_die->sibling;
16040 }
16041
16042 /* Do not consider external references. According to the DWARF standard,
16043 these DIEs are identified by the fact that they have no byte_size
16044 attribute, and a declaration attribute. */
16045 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16046 || !die_is_declaration (die, cu)
16047 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16048 {
16049 struct symbol *sym = new_symbol (die, type, cu);
16050
16051 if (has_template_parameters)
16052 {
16053 struct symtab *symtab;
16054 if (sym != nullptr)
16055 symtab = symbol_symtab (sym);
16056 else if (cu->line_header != nullptr)
16057 {
16058 /* Any related symtab will do. */
16059 symtab
16060 = cu->line_header->file_names ()[0].symtab;
16061 }
16062 else
16063 {
16064 symtab = nullptr;
16065 complaint (_("could not find suitable "
16066 "symtab for template parameter"
16067 " - DIE at %s [in module %s]"),
16068 sect_offset_str (die->sect_off),
16069 objfile_name (objfile));
16070 }
16071
16072 if (symtab != nullptr)
16073 {
16074 /* Make sure that the symtab is set on the new symbols.
16075 Even though they don't appear in this symtab directly,
16076 other parts of gdb assume that symbols do, and this is
16077 reasonably true. */
16078 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16079 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16080 }
16081 }
16082 }
16083 }
16084
16085 /* Assuming DIE is an enumeration type, and TYPE is its associated
16086 type, update TYPE using some information only available in DIE's
16087 children. In particular, the fields are computed. */
16088
16089 static void
16090 update_enumeration_type_from_children (struct die_info *die,
16091 struct type *type,
16092 struct dwarf2_cu *cu)
16093 {
16094 struct die_info *child_die;
16095 int unsigned_enum = 1;
16096 int flag_enum = 1;
16097
16098 auto_obstack obstack;
16099 std::vector<struct field> fields;
16100
16101 for (child_die = die->child;
16102 child_die != NULL && child_die->tag;
16103 child_die = child_die->sibling)
16104 {
16105 struct attribute *attr;
16106 LONGEST value;
16107 const gdb_byte *bytes;
16108 struct dwarf2_locexpr_baton *baton;
16109 const char *name;
16110
16111 if (child_die->tag != DW_TAG_enumerator)
16112 continue;
16113
16114 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16115 if (attr == NULL)
16116 continue;
16117
16118 name = dwarf2_name (child_die, cu);
16119 if (name == NULL)
16120 name = "<anonymous enumerator>";
16121
16122 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16123 &value, &bytes, &baton);
16124 if (value < 0)
16125 {
16126 unsigned_enum = 0;
16127 flag_enum = 0;
16128 }
16129 else
16130 {
16131 if (count_one_bits_ll (value) >= 2)
16132 flag_enum = 0;
16133 }
16134
16135 fields.emplace_back ();
16136 struct field &field = fields.back ();
16137 field.set_name (dwarf2_physname (name, child_die, cu));
16138 field.set_loc_enumval (value);
16139 }
16140
16141 if (!fields.empty ())
16142 {
16143 type->set_num_fields (fields.size ());
16144 type->set_fields
16145 ((struct field *)
16146 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16147 memcpy (type->fields (), fields.data (),
16148 sizeof (struct field) * fields.size ());
16149 }
16150
16151 if (unsigned_enum)
16152 type->set_is_unsigned (true);
16153
16154 if (flag_enum)
16155 type->set_is_flag_enum (true);
16156 }
16157
16158 /* Given a DW_AT_enumeration_type die, set its type. We do not
16159 complete the type's fields yet, or create any symbols. */
16160
16161 static struct type *
16162 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16163 {
16164 struct objfile *objfile = cu->per_objfile->objfile;
16165 struct type *type;
16166 struct attribute *attr;
16167 const char *name;
16168
16169 /* If the definition of this type lives in .debug_types, read that type.
16170 Don't follow DW_AT_specification though, that will take us back up
16171 the chain and we want to go down. */
16172 attr = die->attr (DW_AT_signature);
16173 if (attr != nullptr)
16174 {
16175 type = get_DW_AT_signature_type (die, attr, cu);
16176
16177 /* The type's CU may not be the same as CU.
16178 Ensure TYPE is recorded with CU in die_type_hash. */
16179 return set_die_type (die, type, cu);
16180 }
16181
16182 type = alloc_type (objfile);
16183
16184 type->set_code (TYPE_CODE_ENUM);
16185 name = dwarf2_full_name (NULL, die, cu);
16186 if (name != NULL)
16187 type->set_name (name);
16188
16189 attr = dwarf2_attr (die, DW_AT_type, cu);
16190 if (attr != NULL)
16191 {
16192 struct type *underlying_type = die_type (die, cu);
16193
16194 TYPE_TARGET_TYPE (type) = underlying_type;
16195 }
16196
16197 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16198 if (attr != nullptr)
16199 {
16200 TYPE_LENGTH (type) = attr->constant_value (0);
16201 }
16202 else
16203 {
16204 TYPE_LENGTH (type) = 0;
16205 }
16206
16207 maybe_set_alignment (cu, die, type);
16208
16209 /* The enumeration DIE can be incomplete. In Ada, any type can be
16210 declared as private in the package spec, and then defined only
16211 inside the package body. Such types are known as Taft Amendment
16212 Types. When another package uses such a type, an incomplete DIE
16213 may be generated by the compiler. */
16214 if (die_is_declaration (die, cu))
16215 type->set_is_stub (true);
16216
16217 /* If this type has an underlying type that is not a stub, then we
16218 may use its attributes. We always use the "unsigned" attribute
16219 in this situation, because ordinarily we guess whether the type
16220 is unsigned -- but the guess can be wrong and the underlying type
16221 can tell us the reality. However, we defer to a local size
16222 attribute if one exists, because this lets the compiler override
16223 the underlying type if needed. */
16224 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16225 {
16226 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16227 underlying_type = check_typedef (underlying_type);
16228
16229 type->set_is_unsigned (underlying_type->is_unsigned ());
16230
16231 if (TYPE_LENGTH (type) == 0)
16232 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16233
16234 if (TYPE_RAW_ALIGN (type) == 0
16235 && TYPE_RAW_ALIGN (underlying_type) != 0)
16236 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16237 }
16238
16239 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16240
16241 set_die_type (die, type, cu);
16242
16243 /* Finish the creation of this type by using the enum's children.
16244 Note that, as usual, this must come after set_die_type to avoid
16245 infinite recursion when trying to compute the names of the
16246 enumerators. */
16247 update_enumeration_type_from_children (die, type, cu);
16248
16249 return type;
16250 }
16251
16252 /* Given a pointer to a die which begins an enumeration, process all
16253 the dies that define the members of the enumeration, and create the
16254 symbol for the enumeration type.
16255
16256 NOTE: We reverse the order of the element list. */
16257
16258 static void
16259 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16260 {
16261 struct type *this_type;
16262
16263 this_type = get_die_type (die, cu);
16264 if (this_type == NULL)
16265 this_type = read_enumeration_type (die, cu);
16266
16267 if (die->child != NULL)
16268 {
16269 struct die_info *child_die;
16270 const char *name;
16271
16272 child_die = die->child;
16273 while (child_die && child_die->tag)
16274 {
16275 if (child_die->tag != DW_TAG_enumerator)
16276 {
16277 process_die (child_die, cu);
16278 }
16279 else
16280 {
16281 name = dwarf2_name (child_die, cu);
16282 if (name)
16283 new_symbol (child_die, this_type, cu);
16284 }
16285
16286 child_die = child_die->sibling;
16287 }
16288 }
16289
16290 /* If we are reading an enum from a .debug_types unit, and the enum
16291 is a declaration, and the enum is not the signatured type in the
16292 unit, then we do not want to add a symbol for it. Adding a
16293 symbol would in some cases obscure the true definition of the
16294 enum, giving users an incomplete type when the definition is
16295 actually available. Note that we do not want to do this for all
16296 enums which are just declarations, because C++0x allows forward
16297 enum declarations. */
16298 if (cu->per_cu->is_debug_types
16299 && die_is_declaration (die, cu))
16300 {
16301 struct signatured_type *sig_type;
16302
16303 sig_type = (struct signatured_type *) cu->per_cu;
16304 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16305 if (sig_type->type_offset_in_section != die->sect_off)
16306 return;
16307 }
16308
16309 new_symbol (die, this_type, cu);
16310 }
16311
16312 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16313 expression for an index type and finds the corresponding field
16314 offset in the hidden "P_BOUNDS" structure. Returns true on success
16315 and updates *FIELD, false if it fails to recognize an
16316 expression. */
16317
16318 static bool
16319 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16320 int *bounds_offset, struct field *field,
16321 struct dwarf2_cu *cu)
16322 {
16323 struct attribute *attr = dwarf2_attr (die, name, cu);
16324 if (attr == nullptr || !attr->form_is_block ())
16325 return false;
16326
16327 const struct dwarf_block *block = attr->as_block ();
16328 const gdb_byte *start = block->data;
16329 const gdb_byte *end = block->data + block->size;
16330
16331 /* The expression to recognize generally looks like:
16332
16333 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16334 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16335
16336 However, the second "plus_uconst" may be missing:
16337
16338 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16339 DW_OP_deref_size: 4)
16340
16341 This happens when the field is at the start of the structure.
16342
16343 Also, the final deref may not be sized:
16344
16345 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16346 DW_OP_deref)
16347
16348 This happens when the size of the index type happens to be the
16349 same as the architecture's word size. This can occur with or
16350 without the second plus_uconst. */
16351
16352 if (end - start < 2)
16353 return false;
16354 if (*start++ != DW_OP_push_object_address)
16355 return false;
16356 if (*start++ != DW_OP_plus_uconst)
16357 return false;
16358
16359 uint64_t this_bound_off;
16360 start = gdb_read_uleb128 (start, end, &this_bound_off);
16361 if (start == nullptr || (int) this_bound_off != this_bound_off)
16362 return false;
16363 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16364 is consistent among all bounds. */
16365 if (*bounds_offset == -1)
16366 *bounds_offset = this_bound_off;
16367 else if (*bounds_offset != this_bound_off)
16368 return false;
16369
16370 if (start == end || *start++ != DW_OP_deref)
16371 return false;
16372
16373 int offset = 0;
16374 if (start ==end)
16375 return false;
16376 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16377 {
16378 /* This means an offset of 0. */
16379 }
16380 else if (*start++ != DW_OP_plus_uconst)
16381 return false;
16382 else
16383 {
16384 /* The size is the parameter to DW_OP_plus_uconst. */
16385 uint64_t val;
16386 start = gdb_read_uleb128 (start, end, &val);
16387 if (start == nullptr)
16388 return false;
16389 if ((int) val != val)
16390 return false;
16391 offset = val;
16392 }
16393
16394 if (start == end)
16395 return false;
16396
16397 uint64_t size;
16398 if (*start == DW_OP_deref_size)
16399 {
16400 start = gdb_read_uleb128 (start + 1, end, &size);
16401 if (start == nullptr)
16402 return false;
16403 }
16404 else if (*start == DW_OP_deref)
16405 {
16406 size = cu->header.addr_size;
16407 ++start;
16408 }
16409 else
16410 return false;
16411
16412 field->set_loc_bitpos (8 * offset);
16413 if (size != TYPE_LENGTH (field->type ()))
16414 FIELD_BITSIZE (*field) = 8 * size;
16415
16416 return true;
16417 }
16418
16419 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16420 some kinds of Ada arrays:
16421
16422 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16423 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16424 <11e0> DW_AT_data_location: 2 byte block: 97 6
16425 (DW_OP_push_object_address; DW_OP_deref)
16426 <11e3> DW_AT_type : <0x1173>
16427 <11e7> DW_AT_sibling : <0x1201>
16428 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16429 <11ec> DW_AT_type : <0x1206>
16430 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16431 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16432 DW_OP_deref_size: 4)
16433 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16434 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16435 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16436
16437 This actually represents a "thick pointer", which is a structure
16438 with two elements: one that is a pointer to the array data, and one
16439 that is a pointer to another structure; this second structure holds
16440 the array bounds.
16441
16442 This returns a new type on success, or nullptr if this didn't
16443 recognize the type. */
16444
16445 static struct type *
16446 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16447 struct type *type)
16448 {
16449 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16450 /* So far we've only seen this with block form. */
16451 if (attr == nullptr || !attr->form_is_block ())
16452 return nullptr;
16453
16454 /* Note that this will fail if the structure layout is changed by
16455 the compiler. However, we have no good way to recognize some
16456 other layout, because we don't know what expression the compiler
16457 might choose to emit should this happen. */
16458 struct dwarf_block *blk = attr->as_block ();
16459 if (blk->size != 2
16460 || blk->data[0] != DW_OP_push_object_address
16461 || blk->data[1] != DW_OP_deref)
16462 return nullptr;
16463
16464 int bounds_offset = -1;
16465 int max_align = -1;
16466 std::vector<struct field> range_fields;
16467 for (struct die_info *child_die = die->child;
16468 child_die;
16469 child_die = child_die->sibling)
16470 {
16471 if (child_die->tag == DW_TAG_subrange_type)
16472 {
16473 struct type *underlying = read_subrange_index_type (child_die, cu);
16474
16475 int this_align = type_align (underlying);
16476 if (this_align > max_align)
16477 max_align = this_align;
16478
16479 range_fields.emplace_back ();
16480 range_fields.emplace_back ();
16481
16482 struct field &lower = range_fields[range_fields.size () - 2];
16483 struct field &upper = range_fields[range_fields.size () - 1];
16484
16485 lower.set_type (underlying);
16486 FIELD_ARTIFICIAL (lower) = 1;
16487
16488 upper.set_type (underlying);
16489 FIELD_ARTIFICIAL (upper) = 1;
16490
16491 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16492 &bounds_offset, &lower, cu)
16493 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16494 &bounds_offset, &upper, cu))
16495 return nullptr;
16496 }
16497 }
16498
16499 /* This shouldn't really happen, but double-check that we found
16500 where the bounds are stored. */
16501 if (bounds_offset == -1)
16502 return nullptr;
16503
16504 struct objfile *objfile = cu->per_objfile->objfile;
16505 for (int i = 0; i < range_fields.size (); i += 2)
16506 {
16507 char name[20];
16508
16509 /* Set the name of each field in the bounds. */
16510 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16511 range_fields[i].set_name (objfile->intern (name));
16512 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16513 range_fields[i + 1].set_name (objfile->intern (name));
16514 }
16515
16516 struct type *bounds = alloc_type (objfile);
16517 bounds->set_code (TYPE_CODE_STRUCT);
16518
16519 bounds->set_num_fields (range_fields.size ());
16520 bounds->set_fields
16521 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16522 * sizeof (struct field))));
16523 memcpy (bounds->fields (), range_fields.data (),
16524 bounds->num_fields () * sizeof (struct field));
16525
16526 int last_fieldno = range_fields.size () - 1;
16527 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
16528 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16529 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16530
16531 /* Rewrite the existing array type in place. Specifically, we
16532 remove any dynamic properties we might have read, and we replace
16533 the index types. */
16534 struct type *iter = type;
16535 for (int i = 0; i < range_fields.size (); i += 2)
16536 {
16537 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16538 iter->main_type->dyn_prop_list = nullptr;
16539 iter->set_index_type
16540 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16541 iter = TYPE_TARGET_TYPE (iter);
16542 }
16543
16544 struct type *result = alloc_type (objfile);
16545 result->set_code (TYPE_CODE_STRUCT);
16546
16547 result->set_num_fields (2);
16548 result->set_fields
16549 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16550 * sizeof (struct field))));
16551
16552 /* The names are chosen to coincide with what the compiler does with
16553 -fgnat-encodings=all, which the Ada code in gdb already
16554 understands. */
16555 result->field (0).set_name ("P_ARRAY");
16556 result->field (0).set_type (lookup_pointer_type (type));
16557
16558 result->field (1).set_name ("P_BOUNDS");
16559 result->field (1).set_type (lookup_pointer_type (bounds));
16560 result->field (1).set_loc_bitpos (8 * bounds_offset);
16561
16562 result->set_name (type->name ());
16563 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16564 + TYPE_LENGTH (result->field (1).type ()));
16565
16566 return result;
16567 }
16568
16569 /* Extract all information from a DW_TAG_array_type DIE and put it in
16570 the DIE's type field. For now, this only handles one dimensional
16571 arrays. */
16572
16573 static struct type *
16574 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16575 {
16576 struct objfile *objfile = cu->per_objfile->objfile;
16577 struct die_info *child_die;
16578 struct type *type;
16579 struct type *element_type, *range_type, *index_type;
16580 struct attribute *attr;
16581 const char *name;
16582 struct dynamic_prop *byte_stride_prop = NULL;
16583 unsigned int bit_stride = 0;
16584
16585 element_type = die_type (die, cu);
16586
16587 /* The die_type call above may have already set the type for this DIE. */
16588 type = get_die_type (die, cu);
16589 if (type)
16590 return type;
16591
16592 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16593 if (attr != NULL)
16594 {
16595 int stride_ok;
16596 struct type *prop_type = cu->addr_sized_int_type (false);
16597
16598 byte_stride_prop
16599 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16600 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16601 prop_type);
16602 if (!stride_ok)
16603 {
16604 complaint (_("unable to read array DW_AT_byte_stride "
16605 " - DIE at %s [in module %s]"),
16606 sect_offset_str (die->sect_off),
16607 objfile_name (cu->per_objfile->objfile));
16608 /* Ignore this attribute. We will likely not be able to print
16609 arrays of this type correctly, but there is little we can do
16610 to help if we cannot read the attribute's value. */
16611 byte_stride_prop = NULL;
16612 }
16613 }
16614
16615 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16616 if (attr != NULL)
16617 bit_stride = attr->constant_value (0);
16618
16619 /* Irix 6.2 native cc creates array types without children for
16620 arrays with unspecified length. */
16621 if (die->child == NULL)
16622 {
16623 index_type = objfile_type (objfile)->builtin_int;
16624 range_type = create_static_range_type (NULL, index_type, 0, -1);
16625 type = create_array_type_with_stride (NULL, element_type, range_type,
16626 byte_stride_prop, bit_stride);
16627 return set_die_type (die, type, cu);
16628 }
16629
16630 std::vector<struct type *> range_types;
16631 child_die = die->child;
16632 while (child_die && child_die->tag)
16633 {
16634 if (child_die->tag == DW_TAG_subrange_type)
16635 {
16636 struct type *child_type = read_type_die (child_die, cu);
16637
16638 if (child_type != NULL)
16639 {
16640 /* The range type was succesfully read. Save it for the
16641 array type creation. */
16642 range_types.push_back (child_type);
16643 }
16644 }
16645 child_die = child_die->sibling;
16646 }
16647
16648 if (range_types.empty ())
16649 {
16650 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16651 sect_offset_str (die->sect_off),
16652 objfile_name (cu->per_objfile->objfile));
16653 return NULL;
16654 }
16655
16656 /* Dwarf2 dimensions are output from left to right, create the
16657 necessary array types in backwards order. */
16658
16659 type = element_type;
16660
16661 if (read_array_order (die, cu) == DW_ORD_col_major)
16662 {
16663 int i = 0;
16664
16665 while (i < range_types.size ())
16666 {
16667 type = create_array_type_with_stride (NULL, type, range_types[i++],
16668 byte_stride_prop, bit_stride);
16669 bit_stride = 0;
16670 byte_stride_prop = nullptr;
16671 }
16672 }
16673 else
16674 {
16675 size_t ndim = range_types.size ();
16676 while (ndim-- > 0)
16677 {
16678 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16679 byte_stride_prop, bit_stride);
16680 bit_stride = 0;
16681 byte_stride_prop = nullptr;
16682 }
16683 }
16684
16685 gdb_assert (type != element_type);
16686
16687 /* Understand Dwarf2 support for vector types (like they occur on
16688 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16689 array type. This is not part of the Dwarf2/3 standard yet, but a
16690 custom vendor extension. The main difference between a regular
16691 array and the vector variant is that vectors are passed by value
16692 to functions. */
16693 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16694 if (attr != nullptr)
16695 make_vector_type (type);
16696
16697 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16698 implementation may choose to implement triple vectors using this
16699 attribute. */
16700 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16701 if (attr != nullptr && attr->form_is_unsigned ())
16702 {
16703 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16704 TYPE_LENGTH (type) = attr->as_unsigned ();
16705 else
16706 complaint (_("DW_AT_byte_size for array type smaller "
16707 "than the total size of elements"));
16708 }
16709
16710 name = dwarf2_name (die, cu);
16711 if (name)
16712 type->set_name (name);
16713
16714 maybe_set_alignment (cu, die, type);
16715
16716 struct type *replacement_type = nullptr;
16717 if (cu->per_cu->lang == language_ada)
16718 {
16719 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16720 if (replacement_type != nullptr)
16721 type = replacement_type;
16722 }
16723
16724 /* Install the type in the die. */
16725 set_die_type (die, type, cu, replacement_type != nullptr);
16726
16727 /* set_die_type should be already done. */
16728 set_descriptive_type (type, die, cu);
16729
16730 return type;
16731 }
16732
16733 static enum dwarf_array_dim_ordering
16734 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16735 {
16736 struct attribute *attr;
16737
16738 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16739
16740 if (attr != nullptr)
16741 {
16742 LONGEST val = attr->constant_value (-1);
16743 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16744 return (enum dwarf_array_dim_ordering) val;
16745 }
16746
16747 /* GNU F77 is a special case, as at 08/2004 array type info is the
16748 opposite order to the dwarf2 specification, but data is still
16749 laid out as per normal fortran.
16750
16751 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16752 version checking. */
16753
16754 if (cu->per_cu->lang == language_fortran
16755 && cu->producer && strstr (cu->producer, "GNU F77"))
16756 {
16757 return DW_ORD_row_major;
16758 }
16759
16760 switch (cu->language_defn->array_ordering ())
16761 {
16762 case array_column_major:
16763 return DW_ORD_col_major;
16764 case array_row_major:
16765 default:
16766 return DW_ORD_row_major;
16767 };
16768 }
16769
16770 /* Extract all information from a DW_TAG_set_type DIE and put it in
16771 the DIE's type field. */
16772
16773 static struct type *
16774 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16775 {
16776 struct type *domain_type, *set_type;
16777 struct attribute *attr;
16778
16779 domain_type = die_type (die, cu);
16780
16781 /* The die_type call above may have already set the type for this DIE. */
16782 set_type = get_die_type (die, cu);
16783 if (set_type)
16784 return set_type;
16785
16786 set_type = create_set_type (NULL, domain_type);
16787
16788 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16789 if (attr != nullptr && attr->form_is_unsigned ())
16790 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16791
16792 maybe_set_alignment (cu, die, set_type);
16793
16794 return set_die_type (die, set_type, cu);
16795 }
16796
16797 /* A helper for read_common_block that creates a locexpr baton.
16798 SYM is the symbol which we are marking as computed.
16799 COMMON_DIE is the DIE for the common block.
16800 COMMON_LOC is the location expression attribute for the common
16801 block itself.
16802 MEMBER_LOC is the location expression attribute for the particular
16803 member of the common block that we are processing.
16804 CU is the CU from which the above come. */
16805
16806 static void
16807 mark_common_block_symbol_computed (struct symbol *sym,
16808 struct die_info *common_die,
16809 struct attribute *common_loc,
16810 struct attribute *member_loc,
16811 struct dwarf2_cu *cu)
16812 {
16813 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16814 struct objfile *objfile = per_objfile->objfile;
16815 struct dwarf2_locexpr_baton *baton;
16816 gdb_byte *ptr;
16817 unsigned int cu_off;
16818 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16819 LONGEST offset = 0;
16820
16821 gdb_assert (common_loc && member_loc);
16822 gdb_assert (common_loc->form_is_block ());
16823 gdb_assert (member_loc->form_is_block ()
16824 || member_loc->form_is_constant ());
16825
16826 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16827 baton->per_objfile = per_objfile;
16828 baton->per_cu = cu->per_cu;
16829 gdb_assert (baton->per_cu);
16830
16831 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16832
16833 if (member_loc->form_is_constant ())
16834 {
16835 offset = member_loc->constant_value (0);
16836 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16837 }
16838 else
16839 baton->size += member_loc->as_block ()->size;
16840
16841 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16842 baton->data = ptr;
16843
16844 *ptr++ = DW_OP_call4;
16845 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16846 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16847 ptr += 4;
16848
16849 if (member_loc->form_is_constant ())
16850 {
16851 *ptr++ = DW_OP_addr;
16852 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16853 ptr += cu->header.addr_size;
16854 }
16855 else
16856 {
16857 /* We have to copy the data here, because DW_OP_call4 will only
16858 use a DW_AT_location attribute. */
16859 struct dwarf_block *block = member_loc->as_block ();
16860 memcpy (ptr, block->data, block->size);
16861 ptr += block->size;
16862 }
16863
16864 *ptr++ = DW_OP_plus;
16865 gdb_assert (ptr - baton->data == baton->size);
16866
16867 SYMBOL_LOCATION_BATON (sym) = baton;
16868 sym->set_aclass_index (dwarf2_locexpr_index);
16869 }
16870
16871 /* Create appropriate locally-scoped variables for all the
16872 DW_TAG_common_block entries. Also create a struct common_block
16873 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16874 is used to separate the common blocks name namespace from regular
16875 variable names. */
16876
16877 static void
16878 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16879 {
16880 struct attribute *attr;
16881
16882 attr = dwarf2_attr (die, DW_AT_location, cu);
16883 if (attr != nullptr)
16884 {
16885 /* Support the .debug_loc offsets. */
16886 if (attr->form_is_block ())
16887 {
16888 /* Ok. */
16889 }
16890 else if (attr->form_is_section_offset ())
16891 {
16892 dwarf2_complex_location_expr_complaint ();
16893 attr = NULL;
16894 }
16895 else
16896 {
16897 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16898 "common block member");
16899 attr = NULL;
16900 }
16901 }
16902
16903 if (die->child != NULL)
16904 {
16905 struct objfile *objfile = cu->per_objfile->objfile;
16906 struct die_info *child_die;
16907 size_t n_entries = 0, size;
16908 struct common_block *common_block;
16909 struct symbol *sym;
16910
16911 for (child_die = die->child;
16912 child_die && child_die->tag;
16913 child_die = child_die->sibling)
16914 ++n_entries;
16915
16916 size = (sizeof (struct common_block)
16917 + (n_entries - 1) * sizeof (struct symbol *));
16918 common_block
16919 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16920 size);
16921 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16922 common_block->n_entries = 0;
16923
16924 for (child_die = die->child;
16925 child_die && child_die->tag;
16926 child_die = child_die->sibling)
16927 {
16928 /* Create the symbol in the DW_TAG_common_block block in the current
16929 symbol scope. */
16930 sym = new_symbol (child_die, NULL, cu);
16931 if (sym != NULL)
16932 {
16933 struct attribute *member_loc;
16934
16935 common_block->contents[common_block->n_entries++] = sym;
16936
16937 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16938 cu);
16939 if (member_loc)
16940 {
16941 /* GDB has handled this for a long time, but it is
16942 not specified by DWARF. It seems to have been
16943 emitted by gfortran at least as recently as:
16944 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16945 complaint (_("Variable in common block has "
16946 "DW_AT_data_member_location "
16947 "- DIE at %s [in module %s]"),
16948 sect_offset_str (child_die->sect_off),
16949 objfile_name (objfile));
16950
16951 if (member_loc->form_is_section_offset ())
16952 dwarf2_complex_location_expr_complaint ();
16953 else if (member_loc->form_is_constant ()
16954 || member_loc->form_is_block ())
16955 {
16956 if (attr != nullptr)
16957 mark_common_block_symbol_computed (sym, die, attr,
16958 member_loc, cu);
16959 }
16960 else
16961 dwarf2_complex_location_expr_complaint ();
16962 }
16963 }
16964 }
16965
16966 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16967 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16968 }
16969 }
16970
16971 /* Create a type for a C++ namespace. */
16972
16973 static struct type *
16974 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16975 {
16976 struct objfile *objfile = cu->per_objfile->objfile;
16977 const char *previous_prefix, *name;
16978 int is_anonymous;
16979 struct type *type;
16980
16981 /* For extensions, reuse the type of the original namespace. */
16982 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16983 {
16984 struct die_info *ext_die;
16985 struct dwarf2_cu *ext_cu = cu;
16986
16987 ext_die = dwarf2_extension (die, &ext_cu);
16988 type = read_type_die (ext_die, ext_cu);
16989
16990 /* EXT_CU may not be the same as CU.
16991 Ensure TYPE is recorded with CU in die_type_hash. */
16992 return set_die_type (die, type, cu);
16993 }
16994
16995 name = namespace_name (die, &is_anonymous, cu);
16996
16997 /* Now build the name of the current namespace. */
16998
16999 previous_prefix = determine_prefix (die, cu);
17000 if (previous_prefix[0] != '\0')
17001 name = typename_concat (&objfile->objfile_obstack,
17002 previous_prefix, name, 0, cu);
17003
17004 /* Create the type. */
17005 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17006
17007 return set_die_type (die, type, cu);
17008 }
17009
17010 /* Read a namespace scope. */
17011
17012 static void
17013 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17014 {
17015 struct objfile *objfile = cu->per_objfile->objfile;
17016 int is_anonymous;
17017
17018 /* Add a symbol associated to this if we haven't seen the namespace
17019 before. Also, add a using directive if it's an anonymous
17020 namespace. */
17021
17022 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17023 {
17024 struct type *type;
17025
17026 type = read_type_die (die, cu);
17027 new_symbol (die, type, cu);
17028
17029 namespace_name (die, &is_anonymous, cu);
17030 if (is_anonymous)
17031 {
17032 const char *previous_prefix = determine_prefix (die, cu);
17033
17034 std::vector<const char *> excludes;
17035 add_using_directive (using_directives (cu),
17036 previous_prefix, type->name (), NULL,
17037 NULL, excludes, 0, &objfile->objfile_obstack);
17038 }
17039 }
17040
17041 if (die->child != NULL)
17042 {
17043 struct die_info *child_die = die->child;
17044
17045 while (child_die && child_die->tag)
17046 {
17047 process_die (child_die, cu);
17048 child_die = child_die->sibling;
17049 }
17050 }
17051 }
17052
17053 /* Read a Fortran module as type. This DIE can be only a declaration used for
17054 imported module. Still we need that type as local Fortran "use ... only"
17055 declaration imports depend on the created type in determine_prefix. */
17056
17057 static struct type *
17058 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17059 {
17060 struct objfile *objfile = cu->per_objfile->objfile;
17061 const char *module_name;
17062 struct type *type;
17063
17064 module_name = dwarf2_name (die, cu);
17065 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17066
17067 return set_die_type (die, type, cu);
17068 }
17069
17070 /* Read a Fortran module. */
17071
17072 static void
17073 read_module (struct die_info *die, struct dwarf2_cu *cu)
17074 {
17075 struct die_info *child_die = die->child;
17076 struct type *type;
17077
17078 type = read_type_die (die, cu);
17079 new_symbol (die, type, cu);
17080
17081 while (child_die && child_die->tag)
17082 {
17083 process_die (child_die, cu);
17084 child_die = child_die->sibling;
17085 }
17086 }
17087
17088 /* Return the name of the namespace represented by DIE. Set
17089 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17090 namespace. */
17091
17092 static const char *
17093 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17094 {
17095 struct die_info *current_die;
17096 const char *name = NULL;
17097
17098 /* Loop through the extensions until we find a name. */
17099
17100 for (current_die = die;
17101 current_die != NULL;
17102 current_die = dwarf2_extension (die, &cu))
17103 {
17104 /* We don't use dwarf2_name here so that we can detect the absence
17105 of a name -> anonymous namespace. */
17106 name = dwarf2_string_attr (die, DW_AT_name, cu);
17107
17108 if (name != NULL)
17109 break;
17110 }
17111
17112 /* Is it an anonymous namespace? */
17113
17114 *is_anonymous = (name == NULL);
17115 if (*is_anonymous)
17116 name = CP_ANONYMOUS_NAMESPACE_STR;
17117
17118 return name;
17119 }
17120
17121 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17122 the user defined type vector. */
17123
17124 static struct type *
17125 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17126 {
17127 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17128 struct comp_unit_head *cu_header = &cu->header;
17129 struct type *type;
17130 struct attribute *attr_byte_size;
17131 struct attribute *attr_address_class;
17132 int byte_size, addr_class;
17133 struct type *target_type;
17134
17135 target_type = die_type (die, cu);
17136
17137 /* The die_type call above may have already set the type for this DIE. */
17138 type = get_die_type (die, cu);
17139 if (type)
17140 return type;
17141
17142 type = lookup_pointer_type (target_type);
17143
17144 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17145 if (attr_byte_size)
17146 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17147 else
17148 byte_size = cu_header->addr_size;
17149
17150 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17151 if (attr_address_class)
17152 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17153 else
17154 addr_class = DW_ADDR_none;
17155
17156 ULONGEST alignment = get_alignment (cu, die);
17157
17158 /* If the pointer size, alignment, or address class is different
17159 than the default, create a type variant marked as such and set
17160 the length accordingly. */
17161 if (TYPE_LENGTH (type) != byte_size
17162 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17163 && alignment != TYPE_RAW_ALIGN (type))
17164 || addr_class != DW_ADDR_none)
17165 {
17166 if (gdbarch_address_class_type_flags_p (gdbarch))
17167 {
17168 type_instance_flags type_flags
17169 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17170 addr_class);
17171 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17172 == 0);
17173 type = make_type_with_address_space (type, type_flags);
17174 }
17175 else if (TYPE_LENGTH (type) != byte_size)
17176 {
17177 complaint (_("invalid pointer size %d"), byte_size);
17178 }
17179 else if (TYPE_RAW_ALIGN (type) != alignment)
17180 {
17181 complaint (_("Invalid DW_AT_alignment"
17182 " - DIE at %s [in module %s]"),
17183 sect_offset_str (die->sect_off),
17184 objfile_name (cu->per_objfile->objfile));
17185 }
17186 else
17187 {
17188 /* Should we also complain about unhandled address classes? */
17189 }
17190 }
17191
17192 TYPE_LENGTH (type) = byte_size;
17193 set_type_align (type, alignment);
17194 return set_die_type (die, type, cu);
17195 }
17196
17197 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17198 the user defined type vector. */
17199
17200 static struct type *
17201 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17202 {
17203 struct type *type;
17204 struct type *to_type;
17205 struct type *domain;
17206
17207 to_type = die_type (die, cu);
17208 domain = die_containing_type (die, cu);
17209
17210 /* The calls above may have already set the type for this DIE. */
17211 type = get_die_type (die, cu);
17212 if (type)
17213 return type;
17214
17215 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17216 type = lookup_methodptr_type (to_type);
17217 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17218 {
17219 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17220
17221 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17222 to_type->fields (), to_type->num_fields (),
17223 to_type->has_varargs ());
17224 type = lookup_methodptr_type (new_type);
17225 }
17226 else
17227 type = lookup_memberptr_type (to_type, domain);
17228
17229 return set_die_type (die, type, cu);
17230 }
17231
17232 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17233 the user defined type vector. */
17234
17235 static struct type *
17236 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17237 enum type_code refcode)
17238 {
17239 struct comp_unit_head *cu_header = &cu->header;
17240 struct type *type, *target_type;
17241 struct attribute *attr;
17242
17243 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17244
17245 target_type = die_type (die, cu);
17246
17247 /* The die_type call above may have already set the type for this DIE. */
17248 type = get_die_type (die, cu);
17249 if (type)
17250 return type;
17251
17252 type = lookup_reference_type (target_type, refcode);
17253 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17254 if (attr != nullptr)
17255 {
17256 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17257 }
17258 else
17259 {
17260 TYPE_LENGTH (type) = cu_header->addr_size;
17261 }
17262 maybe_set_alignment (cu, die, type);
17263 return set_die_type (die, type, cu);
17264 }
17265
17266 /* Add the given cv-qualifiers to the element type of the array. GCC
17267 outputs DWARF type qualifiers that apply to an array, not the
17268 element type. But GDB relies on the array element type to carry
17269 the cv-qualifiers. This mimics section 6.7.3 of the C99
17270 specification. */
17271
17272 static struct type *
17273 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17274 struct type *base_type, int cnst, int voltl)
17275 {
17276 struct type *el_type, *inner_array;
17277
17278 base_type = copy_type (base_type);
17279 inner_array = base_type;
17280
17281 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17282 {
17283 TYPE_TARGET_TYPE (inner_array) =
17284 copy_type (TYPE_TARGET_TYPE (inner_array));
17285 inner_array = TYPE_TARGET_TYPE (inner_array);
17286 }
17287
17288 el_type = TYPE_TARGET_TYPE (inner_array);
17289 cnst |= TYPE_CONST (el_type);
17290 voltl |= TYPE_VOLATILE (el_type);
17291 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17292
17293 return set_die_type (die, base_type, cu);
17294 }
17295
17296 static struct type *
17297 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17298 {
17299 struct type *base_type, *cv_type;
17300
17301 base_type = die_type (die, cu);
17302
17303 /* The die_type call above may have already set the type for this DIE. */
17304 cv_type = get_die_type (die, cu);
17305 if (cv_type)
17306 return cv_type;
17307
17308 /* In case the const qualifier is applied to an array type, the element type
17309 is so qualified, not the array type (section 6.7.3 of C99). */
17310 if (base_type->code () == TYPE_CODE_ARRAY)
17311 return add_array_cv_type (die, cu, base_type, 1, 0);
17312
17313 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17314 return set_die_type (die, cv_type, cu);
17315 }
17316
17317 static struct type *
17318 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17319 {
17320 struct type *base_type, *cv_type;
17321
17322 base_type = die_type (die, cu);
17323
17324 /* The die_type call above may have already set the type for this DIE. */
17325 cv_type = get_die_type (die, cu);
17326 if (cv_type)
17327 return cv_type;
17328
17329 /* In case the volatile qualifier is applied to an array type, the
17330 element type is so qualified, not the array type (section 6.7.3
17331 of C99). */
17332 if (base_type->code () == TYPE_CODE_ARRAY)
17333 return add_array_cv_type (die, cu, base_type, 0, 1);
17334
17335 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17336 return set_die_type (die, cv_type, cu);
17337 }
17338
17339 /* Handle DW_TAG_restrict_type. */
17340
17341 static struct type *
17342 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17343 {
17344 struct type *base_type, *cv_type;
17345
17346 base_type = die_type (die, cu);
17347
17348 /* The die_type call above may have already set the type for this DIE. */
17349 cv_type = get_die_type (die, cu);
17350 if (cv_type)
17351 return cv_type;
17352
17353 cv_type = make_restrict_type (base_type);
17354 return set_die_type (die, cv_type, cu);
17355 }
17356
17357 /* Handle DW_TAG_atomic_type. */
17358
17359 static struct type *
17360 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17361 {
17362 struct type *base_type, *cv_type;
17363
17364 base_type = die_type (die, cu);
17365
17366 /* The die_type call above may have already set the type for this DIE. */
17367 cv_type = get_die_type (die, cu);
17368 if (cv_type)
17369 return cv_type;
17370
17371 cv_type = make_atomic_type (base_type);
17372 return set_die_type (die, cv_type, cu);
17373 }
17374
17375 /* Extract all information from a DW_TAG_string_type DIE and add to
17376 the user defined type vector. It isn't really a user defined type,
17377 but it behaves like one, with other DIE's using an AT_user_def_type
17378 attribute to reference it. */
17379
17380 static struct type *
17381 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17382 {
17383 struct objfile *objfile = cu->per_objfile->objfile;
17384 struct gdbarch *gdbarch = objfile->arch ();
17385 struct type *type, *range_type, *index_type, *char_type;
17386 struct attribute *attr;
17387 struct dynamic_prop prop;
17388 bool length_is_constant = true;
17389 LONGEST length;
17390
17391 /* There are a couple of places where bit sizes might be made use of
17392 when parsing a DW_TAG_string_type, however, no producer that we know
17393 of make use of these. Handling bit sizes that are a multiple of the
17394 byte size is easy enough, but what about other bit sizes? Lets deal
17395 with that problem when we have to. Warn about these attributes being
17396 unsupported, then parse the type and ignore them like we always
17397 have. */
17398 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17399 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17400 {
17401 static bool warning_printed = false;
17402 if (!warning_printed)
17403 {
17404 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17405 "currently supported on DW_TAG_string_type."));
17406 warning_printed = true;
17407 }
17408 }
17409
17410 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17411 if (attr != nullptr && !attr->form_is_constant ())
17412 {
17413 /* The string length describes the location at which the length of
17414 the string can be found. The size of the length field can be
17415 specified with one of the attributes below. */
17416 struct type *prop_type;
17417 struct attribute *len
17418 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17419 if (len == nullptr)
17420 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17421 if (len != nullptr && len->form_is_constant ())
17422 {
17423 /* Pass 0 as the default as we know this attribute is constant
17424 and the default value will not be returned. */
17425 LONGEST sz = len->constant_value (0);
17426 prop_type = objfile_int_type (objfile, sz, true);
17427 }
17428 else
17429 {
17430 /* If the size is not specified then we assume it is the size of
17431 an address on this target. */
17432 prop_type = cu->addr_sized_int_type (true);
17433 }
17434
17435 /* Convert the attribute into a dynamic property. */
17436 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17437 length = 1;
17438 else
17439 length_is_constant = false;
17440 }
17441 else if (attr != nullptr)
17442 {
17443 /* This DW_AT_string_length just contains the length with no
17444 indirection. There's no need to create a dynamic property in this
17445 case. Pass 0 for the default value as we know it will not be
17446 returned in this case. */
17447 length = attr->constant_value (0);
17448 }
17449 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17450 {
17451 /* We don't currently support non-constant byte sizes for strings. */
17452 length = attr->constant_value (1);
17453 }
17454 else
17455 {
17456 /* Use 1 as a fallback length if we have nothing else. */
17457 length = 1;
17458 }
17459
17460 index_type = objfile_type (objfile)->builtin_int;
17461 if (length_is_constant)
17462 range_type = create_static_range_type (NULL, index_type, 1, length);
17463 else
17464 {
17465 struct dynamic_prop low_bound;
17466
17467 low_bound.set_const_val (1);
17468 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17469 }
17470 char_type = language_string_char_type (cu->language_defn, gdbarch);
17471 type = create_string_type (NULL, char_type, range_type);
17472
17473 return set_die_type (die, type, cu);
17474 }
17475
17476 /* Assuming that DIE corresponds to a function, returns nonzero
17477 if the function is prototyped. */
17478
17479 static int
17480 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17481 {
17482 struct attribute *attr;
17483
17484 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17485 if (attr && attr->as_boolean ())
17486 return 1;
17487
17488 /* The DWARF standard implies that the DW_AT_prototyped attribute
17489 is only meaningful for C, but the concept also extends to other
17490 languages that allow unprototyped functions (Eg: Objective C).
17491 For all other languages, assume that functions are always
17492 prototyped. */
17493 if (cu->per_cu->lang != language_c
17494 && cu->per_cu->lang != language_objc
17495 && cu->per_cu->lang != language_opencl)
17496 return 1;
17497
17498 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17499 prototyped and unprototyped functions; default to prototyped,
17500 since that is more common in modern code (and RealView warns
17501 about unprototyped functions). */
17502 if (producer_is_realview (cu->producer))
17503 return 1;
17504
17505 return 0;
17506 }
17507
17508 /* Handle DIES due to C code like:
17509
17510 struct foo
17511 {
17512 int (*funcp)(int a, long l);
17513 int b;
17514 };
17515
17516 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17517
17518 static struct type *
17519 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17520 {
17521 struct objfile *objfile = cu->per_objfile->objfile;
17522 struct type *type; /* Type that this function returns. */
17523 struct type *ftype; /* Function that returns above type. */
17524 struct attribute *attr;
17525
17526 type = die_type (die, cu);
17527
17528 /* The die_type call above may have already set the type for this DIE. */
17529 ftype = get_die_type (die, cu);
17530 if (ftype)
17531 return ftype;
17532
17533 ftype = lookup_function_type (type);
17534
17535 if (prototyped_function_p (die, cu))
17536 ftype->set_is_prototyped (true);
17537
17538 /* Store the calling convention in the type if it's available in
17539 the subroutine die. Otherwise set the calling convention to
17540 the default value DW_CC_normal. */
17541 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17542 if (attr != nullptr
17543 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17544 TYPE_CALLING_CONVENTION (ftype)
17545 = (enum dwarf_calling_convention) attr->constant_value (0);
17546 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17547 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17548 else
17549 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17550
17551 /* Record whether the function returns normally to its caller or not
17552 if the DWARF producer set that information. */
17553 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17554 if (attr && attr->as_boolean ())
17555 TYPE_NO_RETURN (ftype) = 1;
17556
17557 /* We need to add the subroutine type to the die immediately so
17558 we don't infinitely recurse when dealing with parameters
17559 declared as the same subroutine type. */
17560 set_die_type (die, ftype, cu);
17561
17562 if (die->child != NULL)
17563 {
17564 struct type *void_type = objfile_type (objfile)->builtin_void;
17565 struct die_info *child_die;
17566 int nparams, iparams;
17567
17568 /* Count the number of parameters.
17569 FIXME: GDB currently ignores vararg functions, but knows about
17570 vararg member functions. */
17571 nparams = 0;
17572 child_die = die->child;
17573 while (child_die && child_die->tag)
17574 {
17575 if (child_die->tag == DW_TAG_formal_parameter)
17576 nparams++;
17577 else if (child_die->tag == DW_TAG_unspecified_parameters)
17578 ftype->set_has_varargs (true);
17579
17580 child_die = child_die->sibling;
17581 }
17582
17583 /* Allocate storage for parameters and fill them in. */
17584 ftype->set_num_fields (nparams);
17585 ftype->set_fields
17586 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17587
17588 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17589 even if we error out during the parameters reading below. */
17590 for (iparams = 0; iparams < nparams; iparams++)
17591 ftype->field (iparams).set_type (void_type);
17592
17593 iparams = 0;
17594 child_die = die->child;
17595 while (child_die && child_die->tag)
17596 {
17597 if (child_die->tag == DW_TAG_formal_parameter)
17598 {
17599 struct type *arg_type;
17600
17601 /* DWARF version 2 has no clean way to discern C++
17602 static and non-static member functions. G++ helps
17603 GDB by marking the first parameter for non-static
17604 member functions (which is the this pointer) as
17605 artificial. We pass this information to
17606 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17607
17608 DWARF version 3 added DW_AT_object_pointer, which GCC
17609 4.5 does not yet generate. */
17610 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17611 if (attr != nullptr)
17612 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17613 else
17614 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17615 arg_type = die_type (child_die, cu);
17616
17617 /* RealView does not mark THIS as const, which the testsuite
17618 expects. GCC marks THIS as const in method definitions,
17619 but not in the class specifications (GCC PR 43053). */
17620 if (cu->per_cu->lang == language_cplus
17621 && !TYPE_CONST (arg_type)
17622 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17623 {
17624 int is_this = 0;
17625 struct dwarf2_cu *arg_cu = cu;
17626 const char *name = dwarf2_name (child_die, cu);
17627
17628 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17629 if (attr != nullptr)
17630 {
17631 /* If the compiler emits this, use it. */
17632 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17633 is_this = 1;
17634 }
17635 else if (name && strcmp (name, "this") == 0)
17636 /* Function definitions will have the argument names. */
17637 is_this = 1;
17638 else if (name == NULL && iparams == 0)
17639 /* Declarations may not have the names, so like
17640 elsewhere in GDB, assume an artificial first
17641 argument is "this". */
17642 is_this = 1;
17643
17644 if (is_this)
17645 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17646 arg_type, 0);
17647 }
17648
17649 ftype->field (iparams).set_type (arg_type);
17650 iparams++;
17651 }
17652 child_die = child_die->sibling;
17653 }
17654 }
17655
17656 return ftype;
17657 }
17658
17659 static struct type *
17660 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17661 {
17662 struct objfile *objfile = cu->per_objfile->objfile;
17663 const char *name = NULL;
17664 struct type *this_type, *target_type;
17665
17666 name = dwarf2_full_name (NULL, die, cu);
17667 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17668 this_type->set_target_is_stub (true);
17669 set_die_type (die, this_type, cu);
17670 target_type = die_type (die, cu);
17671 if (target_type != this_type)
17672 TYPE_TARGET_TYPE (this_type) = target_type;
17673 else
17674 {
17675 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17676 spec and cause infinite loops in GDB. */
17677 complaint (_("Self-referential DW_TAG_typedef "
17678 "- DIE at %s [in module %s]"),
17679 sect_offset_str (die->sect_off), objfile_name (objfile));
17680 TYPE_TARGET_TYPE (this_type) = NULL;
17681 }
17682 if (name == NULL)
17683 {
17684 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17685 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17686 Handle these by just returning the target type, rather than
17687 constructing an anonymous typedef type and trying to handle this
17688 elsewhere. */
17689 set_die_type (die, target_type, cu);
17690 return target_type;
17691 }
17692 return this_type;
17693 }
17694
17695 /* Helper for get_dwarf2_rational_constant that computes the value of
17696 a given gmp_mpz given an attribute. */
17697
17698 static void
17699 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17700 {
17701 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17702 location expression that pushes an implicit value. */
17703 if (attr->form == DW_FORM_exprloc)
17704 {
17705 dwarf_block *blk = attr->as_block ();
17706 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17707 {
17708 uint64_t len;
17709 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17710 blk->data + blk->size,
17711 &len);
17712 if (ptr - blk->data + len <= blk->size)
17713 {
17714 mpz_import (value->val, len,
17715 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17716 1, 0, 0, ptr);
17717 return;
17718 }
17719 }
17720
17721 /* On failure set it to 1. */
17722 *value = gdb_mpz (1);
17723 }
17724 else if (attr->form_is_block ())
17725 {
17726 dwarf_block *blk = attr->as_block ();
17727 mpz_import (value->val, blk->size,
17728 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17729 1, 0, 0, blk->data);
17730 }
17731 else
17732 *value = gdb_mpz (attr->constant_value (1));
17733 }
17734
17735 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17736 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17737
17738 If the numerator and/or numerator attribute is missing,
17739 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17740 untouched. */
17741
17742 static void
17743 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17744 gdb_mpz *numerator, gdb_mpz *denominator)
17745 {
17746 struct attribute *num_attr, *denom_attr;
17747
17748 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17749 if (num_attr == nullptr)
17750 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17751 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17752
17753 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17754 if (denom_attr == nullptr)
17755 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17756 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17757
17758 if (num_attr == nullptr || denom_attr == nullptr)
17759 return;
17760
17761 get_mpz (cu, numerator, num_attr);
17762 get_mpz (cu, denominator, denom_attr);
17763 }
17764
17765 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17766 rational constant, rather than a signed one.
17767
17768 If the rational constant has a negative value, a complaint
17769 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17770
17771 static void
17772 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17773 struct dwarf2_cu *cu,
17774 gdb_mpz *numerator,
17775 gdb_mpz *denominator)
17776 {
17777 gdb_mpz num (1);
17778 gdb_mpz denom (1);
17779
17780 get_dwarf2_rational_constant (die, cu, &num, &denom);
17781 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17782 {
17783 mpz_neg (num.val, num.val);
17784 mpz_neg (denom.val, denom.val);
17785 }
17786 else if (mpz_sgn (num.val) == -1)
17787 {
17788 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17789 " in DIE at %s"),
17790 sect_offset_str (die->sect_off));
17791 return;
17792 }
17793 else if (mpz_sgn (denom.val) == -1)
17794 {
17795 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17796 " in DIE at %s"),
17797 sect_offset_str (die->sect_off));
17798 return;
17799 }
17800
17801 *numerator = std::move (num);
17802 *denominator = std::move (denom);
17803 }
17804
17805 /* Assuming that ENCODING is a string whose contents starting at the
17806 K'th character is "_nn" where "nn" is a decimal number, scan that
17807 number and set RESULT to the value. K is updated to point to the
17808 character immediately following the number.
17809
17810 If the string does not conform to the format described above, false
17811 is returned, and K may or may not be changed. */
17812
17813 static bool
17814 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17815 {
17816 /* The next character should be an underscore ('_') followed
17817 by a digit. */
17818 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17819 return false;
17820
17821 /* Skip the underscore. */
17822 k++;
17823 int start = k;
17824
17825 /* Determine the number of digits for our number. */
17826 while (isdigit (encoding[k]))
17827 k++;
17828 if (k == start)
17829 return false;
17830
17831 std::string copy (&encoding[start], k - start);
17832 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17833 return false;
17834
17835 return true;
17836 }
17837
17838 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17839 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17840 DENOM, update OFFSET, and return true on success. Return false on
17841 failure. */
17842
17843 static bool
17844 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17845 gdb_mpz *num, gdb_mpz *denom)
17846 {
17847 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17848 return false;
17849 return ada_get_gnat_encoded_number (encoding, offset, denom);
17850 }
17851
17852 /* Assuming DIE corresponds to a fixed point type, finish the creation
17853 of the corresponding TYPE by setting its type-specific data. CU is
17854 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17855 encodings. It is nullptr if the GNAT encoding should be
17856 ignored. */
17857
17858 static void
17859 finish_fixed_point_type (struct type *type, const char *suffix,
17860 struct die_info *die, struct dwarf2_cu *cu)
17861 {
17862 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17863 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17864
17865 /* If GNAT encodings are preferred, don't examine the
17866 attributes. */
17867 struct attribute *attr = nullptr;
17868 if (suffix == nullptr)
17869 {
17870 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17871 if (attr == nullptr)
17872 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17873 if (attr == nullptr)
17874 attr = dwarf2_attr (die, DW_AT_small, cu);
17875 }
17876
17877 /* Numerator and denominator of our fixed-point type's scaling factor.
17878 The default is a scaling factor of 1, which we use as a fallback
17879 when we are not able to decode it (problem with the debugging info,
17880 unsupported forms, bug in GDB, etc...). Using that as the default
17881 allows us to at least print the unscaled value, which might still
17882 be useful to a user. */
17883 gdb_mpz scale_num (1);
17884 gdb_mpz scale_denom (1);
17885
17886 if (attr == nullptr)
17887 {
17888 int offset = 0;
17889 if (suffix != nullptr
17890 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17891 &scale_denom)
17892 /* The number might be encoded as _nn_dd_nn_dd, where the
17893 second ratio is the 'small value. In this situation, we
17894 want the second value. */
17895 && (suffix[offset] != '_'
17896 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17897 &scale_denom)))
17898 {
17899 /* Found it. */
17900 }
17901 else
17902 {
17903 /* Scaling factor not found. Assume a scaling factor of 1,
17904 and hope for the best. At least the user will be able to
17905 see the encoded value. */
17906 scale_num = 1;
17907 scale_denom = 1;
17908 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17909 sect_offset_str (die->sect_off));
17910 }
17911 }
17912 else if (attr->name == DW_AT_binary_scale)
17913 {
17914 LONGEST scale_exp = attr->constant_value (0);
17915 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17916
17917 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17918 }
17919 else if (attr->name == DW_AT_decimal_scale)
17920 {
17921 LONGEST scale_exp = attr->constant_value (0);
17922 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17923
17924 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17925 }
17926 else if (attr->name == DW_AT_small)
17927 {
17928 struct die_info *scale_die;
17929 struct dwarf2_cu *scale_cu = cu;
17930
17931 scale_die = follow_die_ref (die, attr, &scale_cu);
17932 if (scale_die->tag == DW_TAG_constant)
17933 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17934 &scale_num, &scale_denom);
17935 else
17936 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17937 " (DIE at %s)"),
17938 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17939 }
17940 else
17941 {
17942 complaint (_("unsupported scale attribute %s for fixed-point type"
17943 " (DIE at %s)"),
17944 dwarf_attr_name (attr->name),
17945 sect_offset_str (die->sect_off));
17946 }
17947
17948 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17949 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17950 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17951 mpq_canonicalize (scaling_factor.val);
17952 }
17953
17954 /* The gnat-encoding suffix for fixed point. */
17955
17956 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17957
17958 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17959 "XF" suffix of the name. The text after this is what encodes the
17960 'small and 'delta information. Otherwise, return nullptr. */
17961
17962 static const char *
17963 gnat_encoded_fixed_point_type_info (const char *name)
17964 {
17965 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17966 }
17967
17968 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17969 (which may be different from NAME) to the architecture back-end to allow
17970 it to guess the correct format if necessary. */
17971
17972 static struct type *
17973 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17974 const char *name_hint, enum bfd_endian byte_order)
17975 {
17976 struct gdbarch *gdbarch = objfile->arch ();
17977 const struct floatformat **format;
17978 struct type *type;
17979
17980 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17981 if (format)
17982 type = init_float_type (objfile, bits, name, format, byte_order);
17983 else
17984 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17985
17986 return type;
17987 }
17988
17989 /* Allocate an integer type of size BITS and name NAME. */
17990
17991 static struct type *
17992 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17993 int bits, int unsigned_p, const char *name)
17994 {
17995 struct type *type;
17996
17997 /* Versions of Intel's C Compiler generate an integer type called "void"
17998 instead of using DW_TAG_unspecified_type. This has been seen on
17999 at least versions 14, 17, and 18. */
18000 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18001 && strcmp (name, "void") == 0)
18002 type = objfile_type (objfile)->builtin_void;
18003 else
18004 type = init_integer_type (objfile, bits, unsigned_p, name);
18005
18006 return type;
18007 }
18008
18009 /* Return true if DIE has a DW_AT_small attribute whose value is
18010 a constant rational, where both the numerator and denominator
18011 are equal to zero.
18012
18013 CU is the DIE's Compilation Unit. */
18014
18015 static bool
18016 has_zero_over_zero_small_attribute (struct die_info *die,
18017 struct dwarf2_cu *cu)
18018 {
18019 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18020 if (attr == nullptr)
18021 return false;
18022
18023 struct dwarf2_cu *scale_cu = cu;
18024 struct die_info *scale_die
18025 = follow_die_ref (die, attr, &scale_cu);
18026
18027 if (scale_die->tag != DW_TAG_constant)
18028 return false;
18029
18030 gdb_mpz num (1), denom (1);
18031 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18032 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18033 }
18034
18035 /* Initialise and return a floating point type of size BITS suitable for
18036 use as a component of a complex number. The NAME_HINT is passed through
18037 when initialising the floating point type and is the name of the complex
18038 type.
18039
18040 As DWARF doesn't currently provide an explicit name for the components
18041 of a complex number, but it can be helpful to have these components
18042 named, we try to select a suitable name based on the size of the
18043 component. */
18044 static struct type *
18045 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18046 struct objfile *objfile,
18047 int bits, const char *name_hint,
18048 enum bfd_endian byte_order)
18049 {
18050 gdbarch *gdbarch = objfile->arch ();
18051 struct type *tt = nullptr;
18052
18053 /* Try to find a suitable floating point builtin type of size BITS.
18054 We're going to use the name of this type as the name for the complex
18055 target type that we are about to create. */
18056 switch (cu->per_cu->lang)
18057 {
18058 case language_fortran:
18059 switch (bits)
18060 {
18061 case 32:
18062 tt = builtin_f_type (gdbarch)->builtin_real;
18063 break;
18064 case 64:
18065 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18066 break;
18067 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18068 case 128:
18069 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18070 break;
18071 }
18072 break;
18073 default:
18074 switch (bits)
18075 {
18076 case 32:
18077 tt = builtin_type (gdbarch)->builtin_float;
18078 break;
18079 case 64:
18080 tt = builtin_type (gdbarch)->builtin_double;
18081 break;
18082 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18083 case 128:
18084 tt = builtin_type (gdbarch)->builtin_long_double;
18085 break;
18086 }
18087 break;
18088 }
18089
18090 /* If the type we found doesn't match the size we were looking for, then
18091 pretend we didn't find a type at all, the complex target type we
18092 create will then be nameless. */
18093 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18094 tt = nullptr;
18095
18096 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18097 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18098 }
18099
18100 /* Find a representation of a given base type and install
18101 it in the TYPE field of the die. */
18102
18103 static struct type *
18104 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18105 {
18106 struct objfile *objfile = cu->per_objfile->objfile;
18107 struct type *type;
18108 struct attribute *attr;
18109 int encoding = 0, bits = 0;
18110 const char *name;
18111 gdbarch *arch;
18112
18113 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18114 if (attr != nullptr && attr->form_is_constant ())
18115 encoding = attr->constant_value (0);
18116 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18117 if (attr != nullptr)
18118 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18119 name = dwarf2_name (die, cu);
18120 if (!name)
18121 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18122
18123 arch = objfile->arch ();
18124 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18125
18126 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18127 if (attr != nullptr && attr->form_is_constant ())
18128 {
18129 int endianity = attr->constant_value (0);
18130
18131 switch (endianity)
18132 {
18133 case DW_END_big:
18134 byte_order = BFD_ENDIAN_BIG;
18135 break;
18136 case DW_END_little:
18137 byte_order = BFD_ENDIAN_LITTLE;
18138 break;
18139 default:
18140 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18141 break;
18142 }
18143 }
18144
18145 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18146 && cu->per_cu->lang == language_ada
18147 && has_zero_over_zero_small_attribute (die, cu))
18148 {
18149 /* brobecker/2018-02-24: This is a fixed point type for which
18150 the scaling factor is represented as fraction whose value
18151 does not make sense (zero divided by zero), so we should
18152 normally never see these. However, there is a small category
18153 of fixed point types for which GNAT is unable to provide
18154 the scaling factor via the standard DWARF mechanisms, and
18155 for which the info is provided via the GNAT encodings instead.
18156 This is likely what this DIE is about. */
18157 encoding = (encoding == DW_ATE_signed_fixed
18158 ? DW_ATE_signed
18159 : DW_ATE_unsigned);
18160 }
18161
18162 /* With GNAT encodings, fixed-point information will be encoded in
18163 the type name. Note that this can also occur with the above
18164 zero-over-zero case, which is why this is a separate "if" rather
18165 than an "else if". */
18166 const char *gnat_encoding_suffix = nullptr;
18167 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18168 && cu->per_cu->lang == language_ada
18169 && name != nullptr)
18170 {
18171 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18172 if (gnat_encoding_suffix != nullptr)
18173 {
18174 gdb_assert (startswith (gnat_encoding_suffix,
18175 GNAT_FIXED_POINT_SUFFIX));
18176 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18177 name, gnat_encoding_suffix - name);
18178 /* Use -1 here so that SUFFIX points at the "_" after the
18179 "XF". */
18180 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18181
18182 encoding = (encoding == DW_ATE_signed
18183 ? DW_ATE_signed_fixed
18184 : DW_ATE_unsigned_fixed);
18185 }
18186 }
18187
18188 switch (encoding)
18189 {
18190 case DW_ATE_address:
18191 /* Turn DW_ATE_address into a void * pointer. */
18192 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18193 type = init_pointer_type (objfile, bits, name, type);
18194 break;
18195 case DW_ATE_boolean:
18196 type = init_boolean_type (objfile, bits, 1, name);
18197 break;
18198 case DW_ATE_complex_float:
18199 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18200 byte_order);
18201 if (type->code () == TYPE_CODE_ERROR)
18202 {
18203 if (name == nullptr)
18204 {
18205 struct obstack *obstack
18206 = &cu->per_objfile->objfile->objfile_obstack;
18207 name = obconcat (obstack, "_Complex ", type->name (),
18208 nullptr);
18209 }
18210 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18211 }
18212 else
18213 type = init_complex_type (name, type);
18214 break;
18215 case DW_ATE_decimal_float:
18216 type = init_decfloat_type (objfile, bits, name);
18217 break;
18218 case DW_ATE_float:
18219 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18220 break;
18221 case DW_ATE_signed:
18222 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18223 break;
18224 case DW_ATE_unsigned:
18225 if (cu->per_cu->lang == language_fortran
18226 && name
18227 && startswith (name, "character("))
18228 type = init_character_type (objfile, bits, 1, name);
18229 else
18230 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18231 break;
18232 case DW_ATE_signed_char:
18233 if (cu->per_cu->lang == language_ada
18234 || cu->per_cu->lang == language_m2
18235 || cu->per_cu->lang == language_pascal
18236 || cu->per_cu->lang == language_fortran)
18237 type = init_character_type (objfile, bits, 0, name);
18238 else
18239 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18240 break;
18241 case DW_ATE_unsigned_char:
18242 if (cu->per_cu->lang == language_ada
18243 || cu->per_cu->lang == language_m2
18244 || cu->per_cu->lang == language_pascal
18245 || cu->per_cu->lang == language_fortran
18246 || cu->per_cu->lang == language_rust)
18247 type = init_character_type (objfile, bits, 1, name);
18248 else
18249 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18250 break;
18251 case DW_ATE_UTF:
18252 {
18253 type = init_character_type (objfile, bits, 1, name);
18254 return set_die_type (die, type, cu);
18255 }
18256 break;
18257 case DW_ATE_signed_fixed:
18258 type = init_fixed_point_type (objfile, bits, 0, name);
18259 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18260 break;
18261 case DW_ATE_unsigned_fixed:
18262 type = init_fixed_point_type (objfile, bits, 1, name);
18263 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18264 break;
18265
18266 default:
18267 complaint (_("unsupported DW_AT_encoding: '%s'"),
18268 dwarf_type_encoding_name (encoding));
18269 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18270 break;
18271 }
18272
18273 if (type->code () == TYPE_CODE_INT
18274 && name != nullptr
18275 && strcmp (name, "char") == 0)
18276 type->set_has_no_signedness (true);
18277
18278 maybe_set_alignment (cu, die, type);
18279
18280 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18281
18282 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18283 {
18284 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18285 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18286 {
18287 unsigned real_bit_size = attr->as_unsigned ();
18288 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18289 /* Only use the attributes if they make sense together. */
18290 if (attr == nullptr
18291 || (attr->as_unsigned () + real_bit_size
18292 <= 8 * TYPE_LENGTH (type)))
18293 {
18294 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18295 = real_bit_size;
18296 if (attr != nullptr)
18297 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18298 = attr->as_unsigned ();
18299 }
18300 }
18301 }
18302
18303 return set_die_type (die, type, cu);
18304 }
18305
18306 /* A helper function that returns the name of DIE, if it refers to a
18307 variable declaration. */
18308
18309 static const char *
18310 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18311 {
18312 if (die->tag != DW_TAG_variable)
18313 return nullptr;
18314
18315 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18316 if (attr == nullptr || !attr->as_boolean ())
18317 return nullptr;
18318
18319 attr = dwarf2_attr (die, DW_AT_name, cu);
18320 if (attr == nullptr)
18321 return nullptr;
18322 return attr->as_string ();
18323 }
18324
18325 /* Parse dwarf attribute if it's a block, reference or constant and put the
18326 resulting value of the attribute into struct bound_prop.
18327 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18328
18329 static int
18330 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18331 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18332 struct type *default_type)
18333 {
18334 struct dwarf2_property_baton *baton;
18335 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18336 struct objfile *objfile = per_objfile->objfile;
18337 struct obstack *obstack = &objfile->objfile_obstack;
18338
18339 gdb_assert (default_type != NULL);
18340
18341 if (attr == NULL || prop == NULL)
18342 return 0;
18343
18344 if (attr->form_is_block ())
18345 {
18346 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18347 baton->property_type = default_type;
18348 baton->locexpr.per_cu = cu->per_cu;
18349 baton->locexpr.per_objfile = per_objfile;
18350
18351 struct dwarf_block *block;
18352 if (attr->form == DW_FORM_data16)
18353 {
18354 size_t data_size = 16;
18355 block = XOBNEW (obstack, struct dwarf_block);
18356 block->size = (data_size
18357 + 2 /* Extra bytes for DW_OP and arg. */);
18358 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18359 data[0] = DW_OP_implicit_value;
18360 data[1] = data_size;
18361 memcpy (&data[2], attr->as_block ()->data, data_size);
18362 block->data = data;
18363 }
18364 else
18365 block = attr->as_block ();
18366
18367 baton->locexpr.size = block->size;
18368 baton->locexpr.data = block->data;
18369 switch (attr->name)
18370 {
18371 case DW_AT_string_length:
18372 baton->locexpr.is_reference = true;
18373 break;
18374 default:
18375 baton->locexpr.is_reference = false;
18376 break;
18377 }
18378
18379 prop->set_locexpr (baton);
18380 gdb_assert (prop->baton () != NULL);
18381 }
18382 else if (attr->form_is_ref ())
18383 {
18384 struct dwarf2_cu *target_cu = cu;
18385 struct die_info *target_die;
18386 struct attribute *target_attr;
18387
18388 target_die = follow_die_ref (die, attr, &target_cu);
18389 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18390 if (target_attr == NULL)
18391 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18392 target_cu);
18393 if (target_attr == nullptr)
18394 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
18395 target_cu);
18396 if (target_attr == NULL)
18397 {
18398 const char *name = var_decl_name (target_die, target_cu);
18399 if (name != nullptr)
18400 {
18401 prop->set_variable_name (name);
18402 return 1;
18403 }
18404 return 0;
18405 }
18406
18407 switch (target_attr->name)
18408 {
18409 case DW_AT_location:
18410 if (target_attr->form_is_section_offset ())
18411 {
18412 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18413 baton->property_type = die_type (target_die, target_cu);
18414 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18415 prop->set_loclist (baton);
18416 gdb_assert (prop->baton () != NULL);
18417 }
18418 else if (target_attr->form_is_block ())
18419 {
18420 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18421 baton->property_type = die_type (target_die, target_cu);
18422 baton->locexpr.per_cu = cu->per_cu;
18423 baton->locexpr.per_objfile = per_objfile;
18424 struct dwarf_block *block = target_attr->as_block ();
18425 baton->locexpr.size = block->size;
18426 baton->locexpr.data = block->data;
18427 baton->locexpr.is_reference = true;
18428 prop->set_locexpr (baton);
18429 gdb_assert (prop->baton () != NULL);
18430 }
18431 else
18432 {
18433 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18434 "dynamic property");
18435 return 0;
18436 }
18437 break;
18438 case DW_AT_data_member_location:
18439 case DW_AT_data_bit_offset:
18440 {
18441 LONGEST offset;
18442
18443 if (!handle_member_location (target_die, target_cu, &offset))
18444 return 0;
18445
18446 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18447 baton->property_type = read_type_die (target_die->parent,
18448 target_cu);
18449 baton->offset_info.offset = offset;
18450 baton->offset_info.type = die_type (target_die, target_cu);
18451 prop->set_addr_offset (baton);
18452 break;
18453 }
18454 }
18455 }
18456 else if (attr->form_is_constant ())
18457 prop->set_const_val (attr->constant_value (0));
18458 else if (attr->form_is_section_offset ())
18459 {
18460 switch (attr->name)
18461 {
18462 case DW_AT_string_length:
18463 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18464 baton->property_type = default_type;
18465 fill_in_loclist_baton (cu, &baton->loclist, attr);
18466 prop->set_loclist (baton);
18467 gdb_assert (prop->baton () != NULL);
18468 break;
18469 default:
18470 goto invalid;
18471 }
18472 }
18473 else
18474 goto invalid;
18475
18476 return 1;
18477
18478 invalid:
18479 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18480 dwarf2_name (die, cu));
18481 return 0;
18482 }
18483
18484 /* See read.h. */
18485
18486 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18487 present (which is valid) then compute the default type based on the
18488 compilation units address size. */
18489
18490 static struct type *
18491 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18492 {
18493 struct type *index_type = die_type (die, cu);
18494
18495 /* Dwarf-2 specifications explicitly allows to create subrange types
18496 without specifying a base type.
18497 In that case, the base type must be set to the type of
18498 the lower bound, upper bound or count, in that order, if any of these
18499 three attributes references an object that has a type.
18500 If no base type is found, the Dwarf-2 specifications say that
18501 a signed integer type of size equal to the size of an address should
18502 be used.
18503 For the following C code: `extern char gdb_int [];'
18504 GCC produces an empty range DIE.
18505 FIXME: muller/2010-05-28: Possible references to object for low bound,
18506 high bound or count are not yet handled by this code. */
18507 if (index_type->code () == TYPE_CODE_VOID)
18508 index_type = cu->addr_sized_int_type (false);
18509
18510 return index_type;
18511 }
18512
18513 /* Read the given DW_AT_subrange DIE. */
18514
18515 static struct type *
18516 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18517 {
18518 struct type *base_type, *orig_base_type;
18519 struct type *range_type;
18520 struct attribute *attr;
18521 struct dynamic_prop low, high;
18522 int low_default_is_valid;
18523 int high_bound_is_count = 0;
18524 const char *name;
18525 ULONGEST negative_mask;
18526
18527 orig_base_type = read_subrange_index_type (die, cu);
18528
18529 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18530 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18531 creating the range type, but we use the result of check_typedef
18532 when examining properties of the type. */
18533 base_type = check_typedef (orig_base_type);
18534
18535 /* The die_type call above may have already set the type for this DIE. */
18536 range_type = get_die_type (die, cu);
18537 if (range_type)
18538 return range_type;
18539
18540 high.set_const_val (0);
18541
18542 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18543 omitting DW_AT_lower_bound. */
18544 switch (cu->per_cu->lang)
18545 {
18546 case language_c:
18547 case language_cplus:
18548 low.set_const_val (0);
18549 low_default_is_valid = 1;
18550 break;
18551 case language_fortran:
18552 low.set_const_val (1);
18553 low_default_is_valid = 1;
18554 break;
18555 case language_d:
18556 case language_objc:
18557 case language_rust:
18558 low.set_const_val (0);
18559 low_default_is_valid = (cu->header.version >= 4);
18560 break;
18561 case language_ada:
18562 case language_m2:
18563 case language_pascal:
18564 low.set_const_val (1);
18565 low_default_is_valid = (cu->header.version >= 4);
18566 break;
18567 default:
18568 low.set_const_val (0);
18569 low_default_is_valid = 0;
18570 break;
18571 }
18572
18573 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18574 if (attr != nullptr)
18575 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18576 else if (!low_default_is_valid)
18577 complaint (_("Missing DW_AT_lower_bound "
18578 "- DIE at %s [in module %s]"),
18579 sect_offset_str (die->sect_off),
18580 objfile_name (cu->per_objfile->objfile));
18581
18582 struct attribute *attr_ub, *attr_count;
18583 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18584 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18585 {
18586 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18587 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18588 {
18589 /* If bounds are constant do the final calculation here. */
18590 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18591 high.set_const_val (low.const_val () + high.const_val () - 1);
18592 else
18593 high_bound_is_count = 1;
18594 }
18595 else
18596 {
18597 if (attr_ub != NULL)
18598 complaint (_("Unresolved DW_AT_upper_bound "
18599 "- DIE at %s [in module %s]"),
18600 sect_offset_str (die->sect_off),
18601 objfile_name (cu->per_objfile->objfile));
18602 if (attr_count != NULL)
18603 complaint (_("Unresolved DW_AT_count "
18604 "- DIE at %s [in module %s]"),
18605 sect_offset_str (die->sect_off),
18606 objfile_name (cu->per_objfile->objfile));
18607 }
18608 }
18609
18610 LONGEST bias = 0;
18611 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18612 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18613 bias = bias_attr->constant_value (0);
18614
18615 /* Normally, the DWARF producers are expected to use a signed
18616 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18617 But this is unfortunately not always the case, as witnessed
18618 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18619 is used instead. To work around that ambiguity, we treat
18620 the bounds as signed, and thus sign-extend their values, when
18621 the base type is signed. */
18622 negative_mask =
18623 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18624 if (low.kind () == PROP_CONST
18625 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18626 low.set_const_val (low.const_val () | negative_mask);
18627 if (high.kind () == PROP_CONST
18628 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18629 high.set_const_val (high.const_val () | negative_mask);
18630
18631 /* Check for bit and byte strides. */
18632 struct dynamic_prop byte_stride_prop;
18633 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18634 if (attr_byte_stride != nullptr)
18635 {
18636 struct type *prop_type = cu->addr_sized_int_type (false);
18637 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18638 prop_type);
18639 }
18640
18641 struct dynamic_prop bit_stride_prop;
18642 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18643 if (attr_bit_stride != nullptr)
18644 {
18645 /* It only makes sense to have either a bit or byte stride. */
18646 if (attr_byte_stride != nullptr)
18647 {
18648 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18649 "- DIE at %s [in module %s]"),
18650 sect_offset_str (die->sect_off),
18651 objfile_name (cu->per_objfile->objfile));
18652 attr_bit_stride = nullptr;
18653 }
18654 else
18655 {
18656 struct type *prop_type = cu->addr_sized_int_type (false);
18657 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18658 prop_type);
18659 }
18660 }
18661
18662 if (attr_byte_stride != nullptr
18663 || attr_bit_stride != nullptr)
18664 {
18665 bool byte_stride_p = (attr_byte_stride != nullptr);
18666 struct dynamic_prop *stride
18667 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18668
18669 range_type
18670 = create_range_type_with_stride (NULL, orig_base_type, &low,
18671 &high, bias, stride, byte_stride_p);
18672 }
18673 else
18674 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18675
18676 if (high_bound_is_count)
18677 range_type->bounds ()->flag_upper_bound_is_count = 1;
18678
18679 /* Ada expects an empty array on no boundary attributes. */
18680 if (attr == NULL && cu->per_cu->lang != language_ada)
18681 range_type->bounds ()->high.set_undefined ();
18682
18683 name = dwarf2_name (die, cu);
18684 if (name)
18685 range_type->set_name (name);
18686
18687 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18688 if (attr != nullptr)
18689 TYPE_LENGTH (range_type) = attr->constant_value (0);
18690
18691 maybe_set_alignment (cu, die, range_type);
18692
18693 set_die_type (die, range_type, cu);
18694
18695 /* set_die_type should be already done. */
18696 set_descriptive_type (range_type, die, cu);
18697
18698 return range_type;
18699 }
18700
18701 static struct type *
18702 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18703 {
18704 struct type *type;
18705
18706 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18707 type->set_name (dwarf2_name (die, cu));
18708
18709 /* In Ada, an unspecified type is typically used when the description
18710 of the type is deferred to a different unit. When encountering
18711 such a type, we treat it as a stub, and try to resolve it later on,
18712 when needed. */
18713 if (cu->per_cu->lang == language_ada)
18714 type->set_is_stub (true);
18715
18716 return set_die_type (die, type, cu);
18717 }
18718
18719 /* Read a single die and all its descendents. Set the die's sibling
18720 field to NULL; set other fields in the die correctly, and set all
18721 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18722 location of the info_ptr after reading all of those dies. PARENT
18723 is the parent of the die in question. */
18724
18725 static struct die_info *
18726 read_die_and_children (const struct die_reader_specs *reader,
18727 const gdb_byte *info_ptr,
18728 const gdb_byte **new_info_ptr,
18729 struct die_info *parent)
18730 {
18731 struct die_info *die;
18732 const gdb_byte *cur_ptr;
18733
18734 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18735 if (die == NULL)
18736 {
18737 *new_info_ptr = cur_ptr;
18738 return NULL;
18739 }
18740 store_in_ref_table (die, reader->cu);
18741
18742 if (die->has_children)
18743 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18744 else
18745 {
18746 die->child = NULL;
18747 *new_info_ptr = cur_ptr;
18748 }
18749
18750 die->sibling = NULL;
18751 die->parent = parent;
18752 return die;
18753 }
18754
18755 /* Read a die, all of its descendents, and all of its siblings; set
18756 all of the fields of all of the dies correctly. Arguments are as
18757 in read_die_and_children. */
18758
18759 static struct die_info *
18760 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18761 const gdb_byte *info_ptr,
18762 const gdb_byte **new_info_ptr,
18763 struct die_info *parent)
18764 {
18765 struct die_info *first_die, *last_sibling;
18766 const gdb_byte *cur_ptr;
18767
18768 cur_ptr = info_ptr;
18769 first_die = last_sibling = NULL;
18770
18771 while (1)
18772 {
18773 struct die_info *die
18774 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18775
18776 if (die == NULL)
18777 {
18778 *new_info_ptr = cur_ptr;
18779 return first_die;
18780 }
18781
18782 if (!first_die)
18783 first_die = die;
18784 else
18785 last_sibling->sibling = die;
18786
18787 last_sibling = die;
18788 }
18789 }
18790
18791 /* Read a die, all of its descendents, and all of its siblings; set
18792 all of the fields of all of the dies correctly. Arguments are as
18793 in read_die_and_children.
18794 This the main entry point for reading a DIE and all its children. */
18795
18796 static struct die_info *
18797 read_die_and_siblings (const struct die_reader_specs *reader,
18798 const gdb_byte *info_ptr,
18799 const gdb_byte **new_info_ptr,
18800 struct die_info *parent)
18801 {
18802 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18803 new_info_ptr, parent);
18804
18805 if (dwarf_die_debug)
18806 {
18807 fprintf_unfiltered (gdb_stdlog,
18808 "Read die from %s@0x%x of %s:\n",
18809 reader->die_section->get_name (),
18810 (unsigned) (info_ptr - reader->die_section->buffer),
18811 bfd_get_filename (reader->abfd));
18812 dump_die (die, dwarf_die_debug);
18813 }
18814
18815 return die;
18816 }
18817
18818 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18819 attributes.
18820 The caller is responsible for filling in the extra attributes
18821 and updating (*DIEP)->num_attrs.
18822 Set DIEP to point to a newly allocated die with its information,
18823 except for its child, sibling, and parent fields. */
18824
18825 static const gdb_byte *
18826 read_full_die_1 (const struct die_reader_specs *reader,
18827 struct die_info **diep, const gdb_byte *info_ptr,
18828 int num_extra_attrs)
18829 {
18830 unsigned int abbrev_number, bytes_read, i;
18831 const struct abbrev_info *abbrev;
18832 struct die_info *die;
18833 struct dwarf2_cu *cu = reader->cu;
18834 bfd *abfd = reader->abfd;
18835
18836 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18837 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18838 info_ptr += bytes_read;
18839 if (!abbrev_number)
18840 {
18841 *diep = NULL;
18842 return info_ptr;
18843 }
18844
18845 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18846 if (!abbrev)
18847 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18848 abbrev_number,
18849 bfd_get_filename (abfd));
18850
18851 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18852 die->sect_off = sect_off;
18853 die->tag = abbrev->tag;
18854 die->abbrev = abbrev_number;
18855 die->has_children = abbrev->has_children;
18856
18857 /* Make the result usable.
18858 The caller needs to update num_attrs after adding the extra
18859 attributes. */
18860 die->num_attrs = abbrev->num_attrs;
18861
18862 bool any_need_reprocess = false;
18863 for (i = 0; i < abbrev->num_attrs; ++i)
18864 {
18865 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18866 info_ptr);
18867 if (die->attrs[i].requires_reprocessing_p ())
18868 any_need_reprocess = true;
18869 }
18870
18871 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18872 if (attr != nullptr && attr->form_is_unsigned ())
18873 cu->str_offsets_base = attr->as_unsigned ();
18874
18875 attr = die->attr (DW_AT_loclists_base);
18876 if (attr != nullptr)
18877 cu->loclist_base = attr->as_unsigned ();
18878
18879 auto maybe_addr_base = die->addr_base ();
18880 if (maybe_addr_base.has_value ())
18881 cu->addr_base = *maybe_addr_base;
18882
18883 attr = die->attr (DW_AT_rnglists_base);
18884 if (attr != nullptr)
18885 cu->rnglists_base = attr->as_unsigned ();
18886
18887 if (any_need_reprocess)
18888 {
18889 for (i = 0; i < abbrev->num_attrs; ++i)
18890 {
18891 if (die->attrs[i].requires_reprocessing_p ())
18892 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18893 }
18894 }
18895 *diep = die;
18896 return info_ptr;
18897 }
18898
18899 /* Read a die and all its attributes.
18900 Set DIEP to point to a newly allocated die with its information,
18901 except for its child, sibling, and parent fields. */
18902
18903 static const gdb_byte *
18904 read_full_die (const struct die_reader_specs *reader,
18905 struct die_info **diep, const gdb_byte *info_ptr)
18906 {
18907 const gdb_byte *result;
18908
18909 result = read_full_die_1 (reader, diep, info_ptr, 0);
18910
18911 if (dwarf_die_debug)
18912 {
18913 fprintf_unfiltered (gdb_stdlog,
18914 "Read die from %s@0x%x of %s:\n",
18915 reader->die_section->get_name (),
18916 (unsigned) (info_ptr - reader->die_section->buffer),
18917 bfd_get_filename (reader->abfd));
18918 dump_die (*diep, dwarf_die_debug);
18919 }
18920
18921 return result;
18922 }
18923 \f
18924
18925 /* Returns nonzero if TAG represents a type that we might generate a partial
18926 symbol for. */
18927
18928 static int
18929 is_type_tag_for_partial (int tag, enum language lang)
18930 {
18931 switch (tag)
18932 {
18933 #if 0
18934 /* Some types that would be reasonable to generate partial symbols for,
18935 that we don't at present. Note that normally this does not
18936 matter, mainly because C compilers don't give names to these
18937 types, but instead emit DW_TAG_typedef. */
18938 case DW_TAG_file_type:
18939 case DW_TAG_ptr_to_member_type:
18940 case DW_TAG_set_type:
18941 case DW_TAG_string_type:
18942 case DW_TAG_subroutine_type:
18943 #endif
18944
18945 /* GNAT may emit an array with a name, but no typedef, so we
18946 need to make a symbol in this case. */
18947 case DW_TAG_array_type:
18948 return lang == language_ada;
18949
18950 case DW_TAG_base_type:
18951 case DW_TAG_class_type:
18952 case DW_TAG_interface_type:
18953 case DW_TAG_enumeration_type:
18954 case DW_TAG_structure_type:
18955 case DW_TAG_subrange_type:
18956 case DW_TAG_typedef:
18957 case DW_TAG_union_type:
18958 return 1;
18959 default:
18960 return 0;
18961 }
18962 }
18963
18964 /* Load all DIEs that are interesting for partial symbols into memory. */
18965
18966 static struct partial_die_info *
18967 load_partial_dies (const struct die_reader_specs *reader,
18968 const gdb_byte *info_ptr, int building_psymtab)
18969 {
18970 struct dwarf2_cu *cu = reader->cu;
18971 struct objfile *objfile = cu->per_objfile->objfile;
18972 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18973 unsigned int bytes_read;
18974 unsigned int load_all = 0;
18975 int nesting_level = 1;
18976
18977 parent_die = NULL;
18978 last_die = NULL;
18979
18980 gdb_assert (cu->per_cu != NULL);
18981 if (cu->load_all_dies)
18982 load_all = 1;
18983
18984 cu->partial_dies
18985 = htab_create_alloc_ex (cu->header.length / 12,
18986 partial_die_hash,
18987 partial_die_eq,
18988 NULL,
18989 &cu->comp_unit_obstack,
18990 hashtab_obstack_allocate,
18991 dummy_obstack_deallocate);
18992
18993 while (1)
18994 {
18995 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18996 &bytes_read);
18997
18998 /* A NULL abbrev means the end of a series of children. */
18999 if (abbrev == NULL)
19000 {
19001 if (--nesting_level == 0)
19002 return first_die;
19003
19004 info_ptr += bytes_read;
19005 last_die = parent_die;
19006 parent_die = parent_die->die_parent;
19007 continue;
19008 }
19009
19010 /* Check for template arguments. We never save these; if
19011 they're seen, we just mark the parent, and go on our way. */
19012 if (parent_die != NULL
19013 && cu->per_cu->lang == language_cplus
19014 && (abbrev->tag == DW_TAG_template_type_param
19015 || abbrev->tag == DW_TAG_template_value_param))
19016 {
19017 parent_die->has_template_arguments = 1;
19018
19019 if (!load_all)
19020 {
19021 /* We don't need a partial DIE for the template argument. */
19022 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19023 continue;
19024 }
19025 }
19026
19027 /* We only recurse into c++ subprograms looking for template arguments.
19028 Skip their other children. */
19029 if (!load_all
19030 && cu->per_cu->lang == language_cplus
19031 && parent_die != NULL
19032 && parent_die->tag == DW_TAG_subprogram
19033 && abbrev->tag != DW_TAG_inlined_subroutine)
19034 {
19035 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19036 continue;
19037 }
19038
19039 /* Check whether this DIE is interesting enough to save. Normally
19040 we would not be interested in members here, but there may be
19041 later variables referencing them via DW_AT_specification (for
19042 static members). */
19043 if (!load_all
19044 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
19045 && abbrev->tag != DW_TAG_constant
19046 && abbrev->tag != DW_TAG_enumerator
19047 && abbrev->tag != DW_TAG_subprogram
19048 && abbrev->tag != DW_TAG_inlined_subroutine
19049 && abbrev->tag != DW_TAG_lexical_block
19050 && abbrev->tag != DW_TAG_variable
19051 && abbrev->tag != DW_TAG_namespace
19052 && abbrev->tag != DW_TAG_module
19053 && abbrev->tag != DW_TAG_member
19054 && abbrev->tag != DW_TAG_imported_unit
19055 && abbrev->tag != DW_TAG_imported_declaration)
19056 {
19057 /* Otherwise we skip to the next sibling, if any. */
19058 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19059 continue;
19060 }
19061
19062 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19063 abbrev);
19064
19065 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19066
19067 /* This two-pass algorithm for processing partial symbols has a
19068 high cost in cache pressure. Thus, handle some simple cases
19069 here which cover the majority of C partial symbols. DIEs
19070 which neither have specification tags in them, nor could have
19071 specification tags elsewhere pointing at them, can simply be
19072 processed and discarded.
19073
19074 This segment is also optional; scan_partial_symbols and
19075 add_partial_symbol will handle these DIEs if we chain
19076 them in normally. When compilers which do not emit large
19077 quantities of duplicate debug information are more common,
19078 this code can probably be removed. */
19079
19080 /* Any complete simple types at the top level (pretty much all
19081 of them, for a language without namespaces), can be processed
19082 directly. */
19083 if (parent_die == NULL
19084 && pdi.has_specification == 0
19085 && pdi.is_declaration == 0
19086 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19087 || pdi.tag == DW_TAG_base_type
19088 || pdi.tag == DW_TAG_array_type
19089 || pdi.tag == DW_TAG_subrange_type))
19090 {
19091 if (building_psymtab && pdi.raw_name != NULL)
19092 add_partial_symbol (&pdi, cu);
19093
19094 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19095 continue;
19096 }
19097
19098 /* The exception for DW_TAG_typedef with has_children above is
19099 a workaround of GCC PR debug/47510. In the case of this complaint
19100 type_name_or_error will error on such types later.
19101
19102 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19103 it could not find the child DIEs referenced later, this is checked
19104 above. In correct DWARF DW_TAG_typedef should have no children. */
19105
19106 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19107 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19108 "- DIE at %s [in module %s]"),
19109 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19110
19111 /* If we're at the second level, and we're an enumerator, and
19112 our parent has no specification (meaning possibly lives in a
19113 namespace elsewhere), then we can add the partial symbol now
19114 instead of queueing it. */
19115 if (pdi.tag == DW_TAG_enumerator
19116 && parent_die != NULL
19117 && parent_die->die_parent == NULL
19118 && parent_die->tag == DW_TAG_enumeration_type
19119 && parent_die->has_specification == 0)
19120 {
19121 if (pdi.raw_name == NULL)
19122 complaint (_("malformed enumerator DIE ignored"));
19123 else if (building_psymtab)
19124 add_partial_symbol (&pdi, cu);
19125
19126 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19127 continue;
19128 }
19129
19130 struct partial_die_info *part_die
19131 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19132
19133 /* We'll save this DIE so link it in. */
19134 part_die->die_parent = parent_die;
19135 part_die->die_sibling = NULL;
19136 part_die->die_child = NULL;
19137
19138 if (last_die && last_die == parent_die)
19139 last_die->die_child = part_die;
19140 else if (last_die)
19141 last_die->die_sibling = part_die;
19142
19143 last_die = part_die;
19144
19145 if (first_die == NULL)
19146 first_die = part_die;
19147
19148 /* Maybe add the DIE to the hash table. Not all DIEs that we
19149 find interesting need to be in the hash table, because we
19150 also have the parent/sibling/child chains; only those that we
19151 might refer to by offset later during partial symbol reading.
19152
19153 For now this means things that might have be the target of a
19154 DW_AT_specification, DW_AT_abstract_origin, or
19155 DW_AT_extension. DW_AT_extension will refer only to
19156 namespaces; DW_AT_abstract_origin refers to functions (and
19157 many things under the function DIE, but we do not recurse
19158 into function DIEs during partial symbol reading) and
19159 possibly variables as well; DW_AT_specification refers to
19160 declarations. Declarations ought to have the DW_AT_declaration
19161 flag. It happens that GCC forgets to put it in sometimes, but
19162 only for functions, not for types.
19163
19164 Adding more things than necessary to the hash table is harmless
19165 except for the performance cost. Adding too few will result in
19166 wasted time in find_partial_die, when we reread the compilation
19167 unit with load_all_dies set. */
19168
19169 if (load_all
19170 || abbrev->tag == DW_TAG_constant
19171 || abbrev->tag == DW_TAG_subprogram
19172 || abbrev->tag == DW_TAG_variable
19173 || abbrev->tag == DW_TAG_namespace
19174 || part_die->is_declaration)
19175 {
19176 void **slot;
19177
19178 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19179 to_underlying (part_die->sect_off),
19180 INSERT);
19181 *slot = part_die;
19182 }
19183
19184 /* For some DIEs we want to follow their children (if any). For C
19185 we have no reason to follow the children of structures; for other
19186 languages we have to, so that we can get at method physnames
19187 to infer fully qualified class names, for DW_AT_specification,
19188 and for C++ template arguments. For C++, we also look one level
19189 inside functions to find template arguments (if the name of the
19190 function does not already contain the template arguments).
19191
19192 For Ada and Fortran, we need to scan the children of subprograms
19193 and lexical blocks as well because these languages allow the
19194 definition of nested entities that could be interesting for the
19195 debugger, such as nested subprograms for instance. */
19196 if (last_die->has_children
19197 && (load_all
19198 || last_die->tag == DW_TAG_namespace
19199 || last_die->tag == DW_TAG_module
19200 || last_die->tag == DW_TAG_enumeration_type
19201 || (cu->per_cu->lang == language_cplus
19202 && last_die->tag == DW_TAG_subprogram
19203 && (last_die->raw_name == NULL
19204 || strchr (last_die->raw_name, '<') == NULL))
19205 || (cu->per_cu->lang != language_c
19206 && (last_die->tag == DW_TAG_class_type
19207 || last_die->tag == DW_TAG_interface_type
19208 || last_die->tag == DW_TAG_structure_type
19209 || last_die->tag == DW_TAG_union_type))
19210 || ((cu->per_cu->lang == language_ada
19211 || cu->per_cu->lang == language_fortran)
19212 && (last_die->tag == DW_TAG_subprogram
19213 || last_die->tag == DW_TAG_lexical_block))))
19214 {
19215 nesting_level++;
19216 parent_die = last_die;
19217 continue;
19218 }
19219
19220 /* Otherwise we skip to the next sibling, if any. */
19221 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19222
19223 /* Back to the top, do it again. */
19224 }
19225 }
19226
19227 partial_die_info::partial_die_info (sect_offset sect_off_,
19228 const struct abbrev_info *abbrev)
19229 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19230 {
19231 }
19232
19233 /* See class definition. */
19234
19235 const char *
19236 partial_die_info::name (dwarf2_cu *cu)
19237 {
19238 if (!canonical_name && raw_name != nullptr)
19239 {
19240 struct objfile *objfile = cu->per_objfile->objfile;
19241 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19242 canonical_name = 1;
19243 }
19244
19245 return raw_name;
19246 }
19247
19248 /* Read a minimal amount of information into the minimal die structure.
19249 INFO_PTR should point just after the initial uleb128 of a DIE. */
19250
19251 const gdb_byte *
19252 partial_die_info::read (const struct die_reader_specs *reader,
19253 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19254 {
19255 struct dwarf2_cu *cu = reader->cu;
19256 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19257 unsigned int i;
19258 int has_low_pc_attr = 0;
19259 int has_high_pc_attr = 0;
19260 int high_pc_relative = 0;
19261
19262 for (i = 0; i < abbrev.num_attrs; ++i)
19263 {
19264 attribute attr;
19265 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19266 /* String and address offsets that need to do the reprocessing have
19267 already been read at this point, so there is no need to wait until
19268 the loop terminates to do the reprocessing. */
19269 if (attr.requires_reprocessing_p ())
19270 read_attribute_reprocess (reader, &attr, tag);
19271 /* Store the data if it is of an attribute we want to keep in a
19272 partial symbol table. */
19273 switch (attr.name)
19274 {
19275 case DW_AT_name:
19276 switch (tag)
19277 {
19278 case DW_TAG_compile_unit:
19279 case DW_TAG_partial_unit:
19280 case DW_TAG_type_unit:
19281 /* Compilation units have a DW_AT_name that is a filename, not
19282 a source language identifier. */
19283 case DW_TAG_enumeration_type:
19284 case DW_TAG_enumerator:
19285 /* These tags always have simple identifiers already; no need
19286 to canonicalize them. */
19287 canonical_name = 1;
19288 raw_name = attr.as_string ();
19289 break;
19290 default:
19291 canonical_name = 0;
19292 raw_name = attr.as_string ();
19293 break;
19294 }
19295 break;
19296 case DW_AT_linkage_name:
19297 case DW_AT_MIPS_linkage_name:
19298 /* Note that both forms of linkage name might appear. We
19299 assume they will be the same, and we only store the last
19300 one we see. */
19301 linkage_name = attr.as_string ();
19302 break;
19303 case DW_AT_low_pc:
19304 has_low_pc_attr = 1;
19305 lowpc = attr.as_address ();
19306 break;
19307 case DW_AT_high_pc:
19308 has_high_pc_attr = 1;
19309 highpc = attr.as_address ();
19310 if (cu->header.version >= 4 && attr.form_is_constant ())
19311 high_pc_relative = 1;
19312 break;
19313 case DW_AT_location:
19314 /* Support the .debug_loc offsets. */
19315 if (attr.form_is_block ())
19316 {
19317 d.locdesc = attr.as_block ();
19318 }
19319 else if (attr.form_is_section_offset ())
19320 {
19321 dwarf2_complex_location_expr_complaint ();
19322 }
19323 else
19324 {
19325 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19326 "partial symbol information");
19327 }
19328 break;
19329 case DW_AT_external:
19330 is_external = attr.as_boolean ();
19331 break;
19332 case DW_AT_declaration:
19333 is_declaration = attr.as_boolean ();
19334 break;
19335 case DW_AT_type:
19336 has_type = 1;
19337 break;
19338 case DW_AT_abstract_origin:
19339 case DW_AT_specification:
19340 case DW_AT_extension:
19341 has_specification = 1;
19342 spec_offset = attr.get_ref_die_offset ();
19343 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19344 || cu->per_cu->is_dwz);
19345 break;
19346 case DW_AT_sibling:
19347 /* Ignore absolute siblings, they might point outside of
19348 the current compile unit. */
19349 if (attr.form == DW_FORM_ref_addr)
19350 complaint (_("ignoring absolute DW_AT_sibling"));
19351 else
19352 {
19353 const gdb_byte *buffer = reader->buffer;
19354 sect_offset off = attr.get_ref_die_offset ();
19355 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19356
19357 if (sibling_ptr < info_ptr)
19358 complaint (_("DW_AT_sibling points backwards"));
19359 else if (sibling_ptr > reader->buffer_end)
19360 reader->die_section->overflow_complaint ();
19361 else
19362 sibling = sibling_ptr;
19363 }
19364 break;
19365 case DW_AT_byte_size:
19366 has_byte_size = 1;
19367 break;
19368 case DW_AT_const_value:
19369 has_const_value = 1;
19370 break;
19371 case DW_AT_calling_convention:
19372 /* DWARF doesn't provide a way to identify a program's source-level
19373 entry point. DW_AT_calling_convention attributes are only meant
19374 to describe functions' calling conventions.
19375
19376 However, because it's a necessary piece of information in
19377 Fortran, and before DWARF 4 DW_CC_program was the only
19378 piece of debugging information whose definition refers to
19379 a 'main program' at all, several compilers marked Fortran
19380 main programs with DW_CC_program --- even when those
19381 functions use the standard calling conventions.
19382
19383 Although DWARF now specifies a way to provide this
19384 information, we support this practice for backward
19385 compatibility. */
19386 if (attr.constant_value (0) == DW_CC_program
19387 && cu->per_cu->lang == language_fortran)
19388 main_subprogram = 1;
19389 break;
19390 case DW_AT_inline:
19391 {
19392 LONGEST value = attr.constant_value (-1);
19393 if (value == DW_INL_inlined
19394 || value == DW_INL_declared_inlined)
19395 may_be_inlined = 1;
19396 }
19397 break;
19398
19399 case DW_AT_import:
19400 if (tag == DW_TAG_imported_unit)
19401 {
19402 d.sect_off = attr.get_ref_die_offset ();
19403 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19404 || cu->per_cu->is_dwz);
19405 }
19406 break;
19407
19408 case DW_AT_main_subprogram:
19409 main_subprogram = attr.as_boolean ();
19410 break;
19411
19412 case DW_AT_ranges:
19413 {
19414 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19415 on DWARF version). */
19416 ranges_offset = attr.as_unsigned ();
19417
19418 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19419 this value. */
19420 if (tag != DW_TAG_compile_unit)
19421 ranges_offset += cu->gnu_ranges_base;
19422
19423 has_range_info = 1;
19424 }
19425 break;
19426
19427 default:
19428 break;
19429 }
19430 }
19431
19432 /* For Ada, if both the name and the linkage name appear, we prefer
19433 the latter. This lets "catch exception" work better, regardless
19434 of the order in which the name and linkage name were emitted.
19435 Really, though, this is just a workaround for the fact that gdb
19436 doesn't store both the name and the linkage name. */
19437 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
19438 raw_name = linkage_name;
19439
19440 if (high_pc_relative)
19441 highpc += lowpc;
19442
19443 if (has_low_pc_attr && has_high_pc_attr)
19444 {
19445 /* When using the GNU linker, .gnu.linkonce. sections are used to
19446 eliminate duplicate copies of functions and vtables and such.
19447 The linker will arbitrarily choose one and discard the others.
19448 The AT_*_pc values for such functions refer to local labels in
19449 these sections. If the section from that file was discarded, the
19450 labels are not in the output, so the relocs get a value of 0.
19451 If this is a discarded function, mark the pc bounds as invalid,
19452 so that GDB will ignore it. */
19453 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19454 {
19455 struct objfile *objfile = per_objfile->objfile;
19456 struct gdbarch *gdbarch = objfile->arch ();
19457
19458 complaint (_("DW_AT_low_pc %s is zero "
19459 "for DIE at %s [in module %s]"),
19460 paddress (gdbarch, lowpc),
19461 sect_offset_str (sect_off),
19462 objfile_name (objfile));
19463 }
19464 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19465 else if (lowpc >= highpc)
19466 {
19467 struct objfile *objfile = per_objfile->objfile;
19468 struct gdbarch *gdbarch = objfile->arch ();
19469
19470 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19471 "for DIE at %s [in module %s]"),
19472 paddress (gdbarch, lowpc),
19473 paddress (gdbarch, highpc),
19474 sect_offset_str (sect_off),
19475 objfile_name (objfile));
19476 }
19477 else
19478 has_pc_info = 1;
19479 }
19480
19481 return info_ptr;
19482 }
19483
19484 /* Find a cached partial DIE at OFFSET in CU. */
19485
19486 struct partial_die_info *
19487 dwarf2_cu::find_partial_die (sect_offset sect_off)
19488 {
19489 struct partial_die_info *lookup_die = NULL;
19490 struct partial_die_info part_die (sect_off);
19491
19492 lookup_die = ((struct partial_die_info *)
19493 htab_find_with_hash (partial_dies, &part_die,
19494 to_underlying (sect_off)));
19495
19496 return lookup_die;
19497 }
19498
19499 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19500 except in the case of .debug_types DIEs which do not reference
19501 outside their CU (they do however referencing other types via
19502 DW_FORM_ref_sig8). */
19503
19504 static const struct cu_partial_die_info
19505 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19506 {
19507 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19508 struct objfile *objfile = per_objfile->objfile;
19509 struct partial_die_info *pd = NULL;
19510
19511 if (offset_in_dwz == cu->per_cu->is_dwz
19512 && cu->header.offset_in_cu_p (sect_off))
19513 {
19514 pd = cu->find_partial_die (sect_off);
19515 if (pd != NULL)
19516 return { cu, pd };
19517 /* We missed recording what we needed.
19518 Load all dies and try again. */
19519 }
19520 else
19521 {
19522 /* TUs don't reference other CUs/TUs (except via type signatures). */
19523 if (cu->per_cu->is_debug_types)
19524 {
19525 error (_("Dwarf Error: Type Unit at offset %s contains"
19526 " external reference to offset %s [in module %s].\n"),
19527 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19528 bfd_get_filename (objfile->obfd));
19529 }
19530 dwarf2_per_cu_data *per_cu
19531 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19532 per_objfile->per_bfd);
19533
19534 cu = per_objfile->get_cu (per_cu);
19535 if (cu == NULL || cu->partial_dies == NULL)
19536 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19537
19538 cu = per_objfile->get_cu (per_cu);
19539
19540 cu->last_used = 0;
19541 pd = cu->find_partial_die (sect_off);
19542 }
19543
19544 /* If we didn't find it, and not all dies have been loaded,
19545 load them all and try again. */
19546
19547 if (pd == NULL && cu->load_all_dies == 0)
19548 {
19549 cu->load_all_dies = 1;
19550
19551 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19552 THIS_CU->cu may already be in use. So we can't just free it and
19553 replace its DIEs with the ones we read in. Instead, we leave those
19554 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19555 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19556 set. */
19557 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19558
19559 pd = cu->find_partial_die (sect_off);
19560 }
19561
19562 if (pd == NULL)
19563 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19564 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19565 return { cu, pd };
19566 }
19567
19568 /* See if we can figure out if the class lives in a namespace. We do
19569 this by looking for a member function; its demangled name will
19570 contain namespace info, if there is any. */
19571
19572 static void
19573 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19574 struct dwarf2_cu *cu)
19575 {
19576 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19577 what template types look like, because the demangler
19578 frequently doesn't give the same name as the debug info. We
19579 could fix this by only using the demangled name to get the
19580 prefix (but see comment in read_structure_type). */
19581
19582 struct partial_die_info *real_pdi;
19583 struct partial_die_info *child_pdi;
19584
19585 /* If this DIE (this DIE's specification, if any) has a parent, then
19586 we should not do this. We'll prepend the parent's fully qualified
19587 name when we create the partial symbol. */
19588
19589 real_pdi = struct_pdi;
19590 while (real_pdi->has_specification)
19591 {
19592 auto res = find_partial_die (real_pdi->spec_offset,
19593 real_pdi->spec_is_dwz, cu);
19594 real_pdi = res.pdi;
19595 cu = res.cu;
19596 }
19597
19598 if (real_pdi->die_parent != NULL)
19599 return;
19600
19601 for (child_pdi = struct_pdi->die_child;
19602 child_pdi != NULL;
19603 child_pdi = child_pdi->die_sibling)
19604 {
19605 if (child_pdi->tag == DW_TAG_subprogram
19606 && child_pdi->linkage_name != NULL)
19607 {
19608 gdb::unique_xmalloc_ptr<char> actual_class_name
19609 (cu->language_defn->class_name_from_physname
19610 (child_pdi->linkage_name));
19611 if (actual_class_name != NULL)
19612 {
19613 struct objfile *objfile = cu->per_objfile->objfile;
19614 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19615 struct_pdi->canonical_name = 1;
19616 }
19617 break;
19618 }
19619 }
19620 }
19621
19622 /* Return true if a DIE with TAG may have the DW_AT_const_value
19623 attribute. */
19624
19625 static bool
19626 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19627 {
19628 switch (tag)
19629 {
19630 case DW_TAG_constant:
19631 case DW_TAG_enumerator:
19632 case DW_TAG_formal_parameter:
19633 case DW_TAG_template_value_param:
19634 case DW_TAG_variable:
19635 return true;
19636 }
19637
19638 return false;
19639 }
19640
19641 void
19642 partial_die_info::fixup (struct dwarf2_cu *cu)
19643 {
19644 /* Once we've fixed up a die, there's no point in doing so again.
19645 This also avoids a memory leak if we were to call
19646 guess_partial_die_structure_name multiple times. */
19647 if (fixup_called)
19648 return;
19649
19650 /* If we found a reference attribute and the DIE has no name, try
19651 to find a name in the referred to DIE. */
19652
19653 if (raw_name == NULL && has_specification)
19654 {
19655 struct partial_die_info *spec_die;
19656
19657 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19658 spec_die = res.pdi;
19659 cu = res.cu;
19660
19661 spec_die->fixup (cu);
19662
19663 if (spec_die->raw_name)
19664 {
19665 raw_name = spec_die->raw_name;
19666 canonical_name = spec_die->canonical_name;
19667
19668 /* Copy DW_AT_external attribute if it is set. */
19669 if (spec_die->is_external)
19670 is_external = spec_die->is_external;
19671 }
19672 }
19673
19674 if (!has_const_value && has_specification
19675 && can_have_DW_AT_const_value_p (tag))
19676 {
19677 struct partial_die_info *spec_die;
19678
19679 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19680 spec_die = res.pdi;
19681 cu = res.cu;
19682
19683 spec_die->fixup (cu);
19684
19685 if (spec_die->has_const_value)
19686 {
19687 /* Copy DW_AT_const_value attribute if it is set. */
19688 has_const_value = spec_die->has_const_value;
19689 }
19690 }
19691
19692 /* Set default names for some unnamed DIEs. */
19693
19694 if (raw_name == NULL && tag == DW_TAG_namespace)
19695 {
19696 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19697 canonical_name = 1;
19698 }
19699
19700 /* If there is no parent die to provide a namespace, and there are
19701 children, see if we can determine the namespace from their linkage
19702 name. */
19703 if (cu->per_cu->lang == language_cplus
19704 && !cu->per_objfile->per_bfd->types.empty ()
19705 && die_parent == NULL
19706 && has_children
19707 && (tag == DW_TAG_class_type
19708 || tag == DW_TAG_structure_type
19709 || tag == DW_TAG_union_type))
19710 guess_partial_die_structure_name (this, cu);
19711
19712 /* GCC might emit a nameless struct or union that has a linkage
19713 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19714 if (raw_name == NULL
19715 && (tag == DW_TAG_class_type
19716 || tag == DW_TAG_interface_type
19717 || tag == DW_TAG_structure_type
19718 || tag == DW_TAG_union_type)
19719 && linkage_name != NULL)
19720 {
19721 gdb::unique_xmalloc_ptr<char> demangled
19722 (gdb_demangle (linkage_name, DMGL_TYPES));
19723 if (demangled != nullptr)
19724 {
19725 const char *base;
19726
19727 /* Strip any leading namespaces/classes, keep only the base name.
19728 DW_AT_name for named DIEs does not contain the prefixes. */
19729 base = strrchr (demangled.get (), ':');
19730 if (base && base > demangled.get () && base[-1] == ':')
19731 base++;
19732 else
19733 base = demangled.get ();
19734
19735 struct objfile *objfile = cu->per_objfile->objfile;
19736 raw_name = objfile->intern (base);
19737 canonical_name = 1;
19738 }
19739 }
19740
19741 fixup_called = 1;
19742 }
19743
19744 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19745 contents from the given SECTION in the HEADER.
19746
19747 HEADER_OFFSET is the offset of the header in the section. */
19748 static void
19749 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19750 struct dwarf2_section_info *section,
19751 sect_offset header_offset)
19752 {
19753 unsigned int bytes_read;
19754 bfd *abfd = section->get_bfd_owner ();
19755 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19756
19757 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19758 info_ptr += bytes_read;
19759
19760 header->version = read_2_bytes (abfd, info_ptr);
19761 info_ptr += 2;
19762
19763 header->addr_size = read_1_byte (abfd, info_ptr);
19764 info_ptr += 1;
19765
19766 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19767 info_ptr += 1;
19768
19769 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19770 }
19771
19772 /* Return the DW_AT_loclists_base value for the CU. */
19773 static ULONGEST
19774 lookup_loclist_base (struct dwarf2_cu *cu)
19775 {
19776 /* For the .dwo unit, the loclist_base points to the first offset following
19777 the header. The header consists of the following entities-
19778 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19779 bit format)
19780 2. version (2 bytes)
19781 3. address size (1 byte)
19782 4. segment selector size (1 byte)
19783 5. offset entry count (4 bytes)
19784 These sizes are derived as per the DWARFv5 standard. */
19785 if (cu->dwo_unit != nullptr)
19786 {
19787 if (cu->header.initial_length_size == 4)
19788 return LOCLIST_HEADER_SIZE32;
19789 return LOCLIST_HEADER_SIZE64;
19790 }
19791 return cu->loclist_base;
19792 }
19793
19794 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19795 array of offsets in the .debug_loclists section. */
19796
19797 static sect_offset
19798 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19799 {
19800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19801 struct objfile *objfile = per_objfile->objfile;
19802 bfd *abfd = objfile->obfd;
19803 ULONGEST loclist_header_size =
19804 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19805 : LOCLIST_HEADER_SIZE64);
19806 ULONGEST loclist_base = lookup_loclist_base (cu);
19807
19808 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19809 ULONGEST start_offset =
19810 loclist_base + loclist_index * cu->header.offset_size;
19811
19812 /* Get loclists section. */
19813 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19814
19815 /* Read the loclists section content. */
19816 section->read (objfile);
19817 if (section->buffer == NULL)
19818 error (_("DW_FORM_loclistx used without .debug_loclists "
19819 "section [in module %s]"), objfile_name (objfile));
19820
19821 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19822 so if loclist_base is smaller than the header size, we have a problem. */
19823 if (loclist_base < loclist_header_size)
19824 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19825 objfile_name (objfile));
19826
19827 /* Read the header of the loclists contribution. */
19828 struct loclists_rnglists_header header;
19829 read_loclists_rnglists_header (&header, section,
19830 (sect_offset) (loclist_base - loclist_header_size));
19831
19832 /* Verify the loclist index is valid. */
19833 if (loclist_index >= header.offset_entry_count)
19834 error (_("DW_FORM_loclistx pointing outside of "
19835 ".debug_loclists offset array [in module %s]"),
19836 objfile_name (objfile));
19837
19838 /* Validate that reading won't go beyond the end of the section. */
19839 if (start_offset + cu->header.offset_size > section->size)
19840 error (_("Reading DW_FORM_loclistx index beyond end of"
19841 ".debug_loclists section [in module %s]"),
19842 objfile_name (objfile));
19843
19844 const gdb_byte *info_ptr = section->buffer + start_offset;
19845
19846 if (cu->header.offset_size == 4)
19847 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19848 else
19849 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19850 }
19851
19852 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19853 array of offsets in the .debug_rnglists section. */
19854
19855 static sect_offset
19856 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19857 dwarf_tag tag)
19858 {
19859 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19860 struct objfile *objfile = dwarf2_per_objfile->objfile;
19861 bfd *abfd = objfile->obfd;
19862 ULONGEST rnglist_header_size =
19863 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19864 : RNGLIST_HEADER_SIZE64);
19865
19866 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19867 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19868 doesn't apply. */
19869 ULONGEST rnglist_base =
19870 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19871
19872 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19873 ULONGEST start_offset =
19874 rnglist_base + rnglist_index * cu->header.offset_size;
19875
19876 /* Get rnglists section. */
19877 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19878
19879 /* Read the rnglists section content. */
19880 section->read (objfile);
19881 if (section->buffer == nullptr)
19882 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19883 "[in module %s]"),
19884 objfile_name (objfile));
19885
19886 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19887 so if rnglist_base is smaller than the header size, we have a problem. */
19888 if (rnglist_base < rnglist_header_size)
19889 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19890 objfile_name (objfile));
19891
19892 /* Read the header of the rnglists contribution. */
19893 struct loclists_rnglists_header header;
19894 read_loclists_rnglists_header (&header, section,
19895 (sect_offset) (rnglist_base - rnglist_header_size));
19896
19897 /* Verify the rnglist index is valid. */
19898 if (rnglist_index >= header.offset_entry_count)
19899 error (_("DW_FORM_rnglistx index pointing outside of "
19900 ".debug_rnglists offset array [in module %s]"),
19901 objfile_name (objfile));
19902
19903 /* Validate that reading won't go beyond the end of the section. */
19904 if (start_offset + cu->header.offset_size > section->size)
19905 error (_("Reading DW_FORM_rnglistx index beyond end of"
19906 ".debug_rnglists section [in module %s]"),
19907 objfile_name (objfile));
19908
19909 const gdb_byte *info_ptr = section->buffer + start_offset;
19910
19911 if (cu->header.offset_size == 4)
19912 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19913 else
19914 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19915 }
19916
19917 /* Process the attributes that had to be skipped in the first round. These
19918 attributes are the ones that need str_offsets_base or addr_base attributes.
19919 They could not have been processed in the first round, because at the time
19920 the values of str_offsets_base or addr_base may not have been known. */
19921 static void
19922 read_attribute_reprocess (const struct die_reader_specs *reader,
19923 struct attribute *attr, dwarf_tag tag)
19924 {
19925 struct dwarf2_cu *cu = reader->cu;
19926 switch (attr->form)
19927 {
19928 case DW_FORM_addrx:
19929 case DW_FORM_GNU_addr_index:
19930 attr->set_address (read_addr_index (cu,
19931 attr->as_unsigned_reprocess ()));
19932 break;
19933 case DW_FORM_loclistx:
19934 {
19935 sect_offset loclists_sect_off
19936 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19937
19938 attr->set_unsigned (to_underlying (loclists_sect_off));
19939 }
19940 break;
19941 case DW_FORM_rnglistx:
19942 {
19943 sect_offset rnglists_sect_off
19944 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19945
19946 attr->set_unsigned (to_underlying (rnglists_sect_off));
19947 }
19948 break;
19949 case DW_FORM_strx:
19950 case DW_FORM_strx1:
19951 case DW_FORM_strx2:
19952 case DW_FORM_strx3:
19953 case DW_FORM_strx4:
19954 case DW_FORM_GNU_str_index:
19955 {
19956 unsigned int str_index = attr->as_unsigned_reprocess ();
19957 gdb_assert (!attr->canonical_string_p ());
19958 if (reader->dwo_file != NULL)
19959 attr->set_string_noncanonical (read_dwo_str_index (reader,
19960 str_index));
19961 else
19962 attr->set_string_noncanonical (read_stub_str_index (cu,
19963 str_index));
19964 break;
19965 }
19966 default:
19967 gdb_assert_not_reached ("Unexpected DWARF form.");
19968 }
19969 }
19970
19971 /* Read an attribute value described by an attribute form. */
19972
19973 static const gdb_byte *
19974 read_attribute_value (const struct die_reader_specs *reader,
19975 struct attribute *attr, unsigned form,
19976 LONGEST implicit_const, const gdb_byte *info_ptr)
19977 {
19978 struct dwarf2_cu *cu = reader->cu;
19979 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19980 struct objfile *objfile = per_objfile->objfile;
19981 bfd *abfd = reader->abfd;
19982 struct comp_unit_head *cu_header = &cu->header;
19983 unsigned int bytes_read;
19984 struct dwarf_block *blk;
19985
19986 attr->form = (enum dwarf_form) form;
19987 switch (form)
19988 {
19989 case DW_FORM_ref_addr:
19990 if (cu_header->version == 2)
19991 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
19992 &bytes_read));
19993 else
19994 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19995 &bytes_read));
19996 info_ptr += bytes_read;
19997 break;
19998 case DW_FORM_GNU_ref_alt:
19999 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20000 &bytes_read));
20001 info_ptr += bytes_read;
20002 break;
20003 case DW_FORM_addr:
20004 {
20005 struct gdbarch *gdbarch = objfile->arch ();
20006 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20007 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20008 attr->set_address (addr);
20009 info_ptr += bytes_read;
20010 }
20011 break;
20012 case DW_FORM_block2:
20013 blk = dwarf_alloc_block (cu);
20014 blk->size = read_2_bytes (abfd, info_ptr);
20015 info_ptr += 2;
20016 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20017 info_ptr += blk->size;
20018 attr->set_block (blk);
20019 break;
20020 case DW_FORM_block4:
20021 blk = dwarf_alloc_block (cu);
20022 blk->size = read_4_bytes (abfd, info_ptr);
20023 info_ptr += 4;
20024 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20025 info_ptr += blk->size;
20026 attr->set_block (blk);
20027 break;
20028 case DW_FORM_data2:
20029 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20030 info_ptr += 2;
20031 break;
20032 case DW_FORM_data4:
20033 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20034 info_ptr += 4;
20035 break;
20036 case DW_FORM_data8:
20037 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20038 info_ptr += 8;
20039 break;
20040 case DW_FORM_data16:
20041 blk = dwarf_alloc_block (cu);
20042 blk->size = 16;
20043 blk->data = read_n_bytes (abfd, info_ptr, 16);
20044 info_ptr += 16;
20045 attr->set_block (blk);
20046 break;
20047 case DW_FORM_sec_offset:
20048 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20049 &bytes_read));
20050 info_ptr += bytes_read;
20051 break;
20052 case DW_FORM_loclistx:
20053 {
20054 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20055 &bytes_read));
20056 info_ptr += bytes_read;
20057 }
20058 break;
20059 case DW_FORM_string:
20060 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20061 &bytes_read));
20062 info_ptr += bytes_read;
20063 break;
20064 case DW_FORM_strp:
20065 if (!cu->per_cu->is_dwz)
20066 {
20067 attr->set_string_noncanonical
20068 (read_indirect_string (per_objfile,
20069 abfd, info_ptr, cu_header,
20070 &bytes_read));
20071 info_ptr += bytes_read;
20072 break;
20073 }
20074 /* FALLTHROUGH */
20075 case DW_FORM_line_strp:
20076 if (!cu->per_cu->is_dwz)
20077 {
20078 attr->set_string_noncanonical
20079 (per_objfile->read_line_string (info_ptr, cu_header,
20080 &bytes_read));
20081 info_ptr += bytes_read;
20082 break;
20083 }
20084 /* FALLTHROUGH */
20085 case DW_FORM_GNU_strp_alt:
20086 {
20087 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20088 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20089 &bytes_read);
20090
20091 attr->set_string_noncanonical
20092 (dwz->read_string (objfile, str_offset));
20093 info_ptr += bytes_read;
20094 }
20095 break;
20096 case DW_FORM_exprloc:
20097 case DW_FORM_block:
20098 blk = dwarf_alloc_block (cu);
20099 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20100 info_ptr += bytes_read;
20101 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20102 info_ptr += blk->size;
20103 attr->set_block (blk);
20104 break;
20105 case DW_FORM_block1:
20106 blk = dwarf_alloc_block (cu);
20107 blk->size = read_1_byte (abfd, info_ptr);
20108 info_ptr += 1;
20109 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20110 info_ptr += blk->size;
20111 attr->set_block (blk);
20112 break;
20113 case DW_FORM_data1:
20114 case DW_FORM_flag:
20115 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20116 info_ptr += 1;
20117 break;
20118 case DW_FORM_flag_present:
20119 attr->set_unsigned (1);
20120 break;
20121 case DW_FORM_sdata:
20122 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20123 info_ptr += bytes_read;
20124 break;
20125 case DW_FORM_rnglistx:
20126 {
20127 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20128 &bytes_read));
20129 info_ptr += bytes_read;
20130 }
20131 break;
20132 case DW_FORM_udata:
20133 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20134 info_ptr += bytes_read;
20135 break;
20136 case DW_FORM_ref1:
20137 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20138 + read_1_byte (abfd, info_ptr)));
20139 info_ptr += 1;
20140 break;
20141 case DW_FORM_ref2:
20142 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20143 + read_2_bytes (abfd, info_ptr)));
20144 info_ptr += 2;
20145 break;
20146 case DW_FORM_ref4:
20147 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20148 + read_4_bytes (abfd, info_ptr)));
20149 info_ptr += 4;
20150 break;
20151 case DW_FORM_ref8:
20152 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20153 + read_8_bytes (abfd, info_ptr)));
20154 info_ptr += 8;
20155 break;
20156 case DW_FORM_ref_sig8:
20157 attr->set_signature (read_8_bytes (abfd, info_ptr));
20158 info_ptr += 8;
20159 break;
20160 case DW_FORM_ref_udata:
20161 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20162 + read_unsigned_leb128 (abfd, info_ptr,
20163 &bytes_read)));
20164 info_ptr += bytes_read;
20165 break;
20166 case DW_FORM_indirect:
20167 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20168 info_ptr += bytes_read;
20169 if (form == DW_FORM_implicit_const)
20170 {
20171 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20172 info_ptr += bytes_read;
20173 }
20174 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20175 info_ptr);
20176 break;
20177 case DW_FORM_implicit_const:
20178 attr->set_signed (implicit_const);
20179 break;
20180 case DW_FORM_addrx:
20181 case DW_FORM_GNU_addr_index:
20182 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20183 &bytes_read));
20184 info_ptr += bytes_read;
20185 break;
20186 case DW_FORM_strx:
20187 case DW_FORM_strx1:
20188 case DW_FORM_strx2:
20189 case DW_FORM_strx3:
20190 case DW_FORM_strx4:
20191 case DW_FORM_GNU_str_index:
20192 {
20193 ULONGEST str_index;
20194 if (form == DW_FORM_strx1)
20195 {
20196 str_index = read_1_byte (abfd, info_ptr);
20197 info_ptr += 1;
20198 }
20199 else if (form == DW_FORM_strx2)
20200 {
20201 str_index = read_2_bytes (abfd, info_ptr);
20202 info_ptr += 2;
20203 }
20204 else if (form == DW_FORM_strx3)
20205 {
20206 str_index = read_3_bytes (abfd, info_ptr);
20207 info_ptr += 3;
20208 }
20209 else if (form == DW_FORM_strx4)
20210 {
20211 str_index = read_4_bytes (abfd, info_ptr);
20212 info_ptr += 4;
20213 }
20214 else
20215 {
20216 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20217 info_ptr += bytes_read;
20218 }
20219 attr->set_unsigned_reprocess (str_index);
20220 }
20221 break;
20222 default:
20223 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20224 dwarf_form_name (form),
20225 bfd_get_filename (abfd));
20226 }
20227
20228 /* Super hack. */
20229 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20230 attr->form = DW_FORM_GNU_ref_alt;
20231
20232 /* We have seen instances where the compiler tried to emit a byte
20233 size attribute of -1 which ended up being encoded as an unsigned
20234 0xffffffff. Although 0xffffffff is technically a valid size value,
20235 an object of this size seems pretty unlikely so we can relatively
20236 safely treat these cases as if the size attribute was invalid and
20237 treat them as zero by default. */
20238 if (attr->name == DW_AT_byte_size
20239 && form == DW_FORM_data4
20240 && attr->as_unsigned () >= 0xffffffff)
20241 {
20242 complaint
20243 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20244 hex_string (attr->as_unsigned ()));
20245 attr->set_unsigned (0);
20246 }
20247
20248 return info_ptr;
20249 }
20250
20251 /* Read an attribute described by an abbreviated attribute. */
20252
20253 static const gdb_byte *
20254 read_attribute (const struct die_reader_specs *reader,
20255 struct attribute *attr, const struct attr_abbrev *abbrev,
20256 const gdb_byte *info_ptr)
20257 {
20258 attr->name = abbrev->name;
20259 attr->string_is_canonical = 0;
20260 attr->requires_reprocessing = 0;
20261 return read_attribute_value (reader, attr, abbrev->form,
20262 abbrev->implicit_const, info_ptr);
20263 }
20264
20265 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20266
20267 static const char *
20268 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20269 LONGEST str_offset)
20270 {
20271 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20272 str_offset, "DW_FORM_strp");
20273 }
20274
20275 /* Return pointer to string at .debug_str offset as read from BUF.
20276 BUF is assumed to be in a compilation unit described by CU_HEADER.
20277 Return *BYTES_READ_PTR count of bytes read from BUF. */
20278
20279 static const char *
20280 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20281 const gdb_byte *buf,
20282 const struct comp_unit_head *cu_header,
20283 unsigned int *bytes_read_ptr)
20284 {
20285 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20286
20287 return read_indirect_string_at_offset (per_objfile, str_offset);
20288 }
20289
20290 /* See read.h. */
20291
20292 const char *
20293 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20294 unsigned int offset_size)
20295 {
20296 bfd *abfd = objfile->obfd;
20297 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
20298
20299 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20300 }
20301
20302 /* See read.h. */
20303
20304 const char *
20305 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20306 const struct comp_unit_head *cu_header,
20307 unsigned int *bytes_read_ptr)
20308 {
20309 bfd *abfd = objfile->obfd;
20310 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20311
20312 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20313 }
20314
20315 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20316 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20317 ADDR_SIZE is the size of addresses from the CU header. */
20318
20319 static CORE_ADDR
20320 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20321 gdb::optional<ULONGEST> addr_base, int addr_size)
20322 {
20323 struct objfile *objfile = per_objfile->objfile;
20324 bfd *abfd = objfile->obfd;
20325 const gdb_byte *info_ptr;
20326 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20327
20328 per_objfile->per_bfd->addr.read (objfile);
20329 if (per_objfile->per_bfd->addr.buffer == NULL)
20330 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20331 objfile_name (objfile));
20332 if (addr_base_or_zero + addr_index * addr_size
20333 >= per_objfile->per_bfd->addr.size)
20334 error (_("DW_FORM_addr_index pointing outside of "
20335 ".debug_addr section [in module %s]"),
20336 objfile_name (objfile));
20337 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20338 + addr_index * addr_size);
20339 if (addr_size == 4)
20340 return bfd_get_32 (abfd, info_ptr);
20341 else
20342 return bfd_get_64 (abfd, info_ptr);
20343 }
20344
20345 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20346
20347 static CORE_ADDR
20348 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20349 {
20350 return read_addr_index_1 (cu->per_objfile, addr_index,
20351 cu->addr_base, cu->header.addr_size);
20352 }
20353
20354 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20355
20356 static CORE_ADDR
20357 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20358 unsigned int *bytes_read)
20359 {
20360 bfd *abfd = cu->per_objfile->objfile->obfd;
20361 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20362
20363 return read_addr_index (cu, addr_index);
20364 }
20365
20366 /* See read.h. */
20367
20368 CORE_ADDR
20369 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20370 dwarf2_per_objfile *per_objfile,
20371 unsigned int addr_index)
20372 {
20373 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20374 gdb::optional<ULONGEST> addr_base;
20375 int addr_size;
20376
20377 /* We need addr_base and addr_size.
20378 If we don't have PER_CU->cu, we have to get it.
20379 Nasty, but the alternative is storing the needed info in PER_CU,
20380 which at this point doesn't seem justified: it's not clear how frequently
20381 it would get used and it would increase the size of every PER_CU.
20382 Entry points like dwarf2_per_cu_addr_size do a similar thing
20383 so we're not in uncharted territory here.
20384 Alas we need to be a bit more complicated as addr_base is contained
20385 in the DIE.
20386
20387 We don't need to read the entire CU(/TU).
20388 We just need the header and top level die.
20389
20390 IWBN to use the aging mechanism to let us lazily later discard the CU.
20391 For now we skip this optimization. */
20392
20393 if (cu != NULL)
20394 {
20395 addr_base = cu->addr_base;
20396 addr_size = cu->header.addr_size;
20397 }
20398 else
20399 {
20400 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20401 addr_base = reader.cu->addr_base;
20402 addr_size = reader.cu->header.addr_size;
20403 }
20404
20405 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20406 }
20407
20408 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20409 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20410 DWO file. */
20411
20412 static const char *
20413 read_str_index (struct dwarf2_cu *cu,
20414 struct dwarf2_section_info *str_section,
20415 struct dwarf2_section_info *str_offsets_section,
20416 ULONGEST str_offsets_base, ULONGEST str_index)
20417 {
20418 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20419 struct objfile *objfile = per_objfile->objfile;
20420 const char *objf_name = objfile_name (objfile);
20421 bfd *abfd = objfile->obfd;
20422 const gdb_byte *info_ptr;
20423 ULONGEST str_offset;
20424 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20425
20426 str_section->read (objfile);
20427 str_offsets_section->read (objfile);
20428 if (str_section->buffer == NULL)
20429 error (_("%s used without %s section"
20430 " in CU at offset %s [in module %s]"),
20431 form_name, str_section->get_name (),
20432 sect_offset_str (cu->header.sect_off), objf_name);
20433 if (str_offsets_section->buffer == NULL)
20434 error (_("%s used without %s section"
20435 " in CU at offset %s [in module %s]"),
20436 form_name, str_section->get_name (),
20437 sect_offset_str (cu->header.sect_off), objf_name);
20438 info_ptr = (str_offsets_section->buffer
20439 + str_offsets_base
20440 + str_index * cu->header.offset_size);
20441 if (cu->header.offset_size == 4)
20442 str_offset = bfd_get_32 (abfd, info_ptr);
20443 else
20444 str_offset = bfd_get_64 (abfd, info_ptr);
20445 if (str_offset >= str_section->size)
20446 error (_("Offset from %s pointing outside of"
20447 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20448 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20449 return (const char *) (str_section->buffer + str_offset);
20450 }
20451
20452 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20453
20454 static const char *
20455 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20456 {
20457 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20458 ? reader->cu->header.addr_size : 0;
20459 return read_str_index (reader->cu,
20460 &reader->dwo_file->sections.str,
20461 &reader->dwo_file->sections.str_offsets,
20462 str_offsets_base, str_index);
20463 }
20464
20465 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20466
20467 static const char *
20468 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20469 {
20470 struct objfile *objfile = cu->per_objfile->objfile;
20471 const char *objf_name = objfile_name (objfile);
20472 static const char form_name[] = "DW_FORM_GNU_str_index";
20473 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20474
20475 if (!cu->str_offsets_base.has_value ())
20476 error (_("%s used in Fission stub without %s"
20477 " in CU at offset 0x%lx [in module %s]"),
20478 form_name, str_offsets_attr_name,
20479 (long) cu->header.offset_size, objf_name);
20480
20481 return read_str_index (cu,
20482 &cu->per_objfile->per_bfd->str,
20483 &cu->per_objfile->per_bfd->str_offsets,
20484 *cu->str_offsets_base, str_index);
20485 }
20486
20487 /* Return the length of an LEB128 number in BUF. */
20488
20489 static int
20490 leb128_size (const gdb_byte *buf)
20491 {
20492 const gdb_byte *begin = buf;
20493 gdb_byte byte;
20494
20495 while (1)
20496 {
20497 byte = *buf++;
20498 if ((byte & 128) == 0)
20499 return buf - begin;
20500 }
20501 }
20502
20503 static enum language
20504 dwarf_lang_to_enum_language (unsigned int lang)
20505 {
20506 enum language language;
20507
20508 switch (lang)
20509 {
20510 case DW_LANG_C89:
20511 case DW_LANG_C99:
20512 case DW_LANG_C11:
20513 case DW_LANG_C:
20514 case DW_LANG_UPC:
20515 language = language_c;
20516 break;
20517 case DW_LANG_Java:
20518 case DW_LANG_C_plus_plus:
20519 case DW_LANG_C_plus_plus_11:
20520 case DW_LANG_C_plus_plus_14:
20521 language = language_cplus;
20522 break;
20523 case DW_LANG_D:
20524 language = language_d;
20525 break;
20526 case DW_LANG_Fortran77:
20527 case DW_LANG_Fortran90:
20528 case DW_LANG_Fortran95:
20529 case DW_LANG_Fortran03:
20530 case DW_LANG_Fortran08:
20531 language = language_fortran;
20532 break;
20533 case DW_LANG_Go:
20534 language = language_go;
20535 break;
20536 case DW_LANG_Mips_Assembler:
20537 language = language_asm;
20538 break;
20539 case DW_LANG_Ada83:
20540 case DW_LANG_Ada95:
20541 language = language_ada;
20542 break;
20543 case DW_LANG_Modula2:
20544 language = language_m2;
20545 break;
20546 case DW_LANG_Pascal83:
20547 language = language_pascal;
20548 break;
20549 case DW_LANG_ObjC:
20550 language = language_objc;
20551 break;
20552 case DW_LANG_Rust:
20553 case DW_LANG_Rust_old:
20554 language = language_rust;
20555 break;
20556 case DW_LANG_OpenCL:
20557 language = language_opencl;
20558 break;
20559 case DW_LANG_Cobol74:
20560 case DW_LANG_Cobol85:
20561 default:
20562 language = language_minimal;
20563 break;
20564 }
20565
20566 return language;
20567 }
20568
20569 /* Return the named attribute or NULL if not there. */
20570
20571 static struct attribute *
20572 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20573 {
20574 for (;;)
20575 {
20576 unsigned int i;
20577 struct attribute *spec = NULL;
20578
20579 for (i = 0; i < die->num_attrs; ++i)
20580 {
20581 if (die->attrs[i].name == name)
20582 return &die->attrs[i];
20583 if (die->attrs[i].name == DW_AT_specification
20584 || die->attrs[i].name == DW_AT_abstract_origin)
20585 spec = &die->attrs[i];
20586 }
20587
20588 if (!spec)
20589 break;
20590
20591 die = follow_die_ref (die, spec, &cu);
20592 }
20593
20594 return NULL;
20595 }
20596
20597 /* Return the string associated with a string-typed attribute, or NULL if it
20598 is either not found or is of an incorrect type. */
20599
20600 static const char *
20601 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20602 {
20603 struct attribute *attr;
20604 const char *str = NULL;
20605
20606 attr = dwarf2_attr (die, name, cu);
20607
20608 if (attr != NULL)
20609 {
20610 str = attr->as_string ();
20611 if (str == nullptr)
20612 complaint (_("string type expected for attribute %s for "
20613 "DIE at %s in module %s"),
20614 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20615 objfile_name (cu->per_objfile->objfile));
20616 }
20617
20618 return str;
20619 }
20620
20621 /* Return the dwo name or NULL if not present. If present, it is in either
20622 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20623 static const char *
20624 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20625 {
20626 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20627 if (dwo_name == nullptr)
20628 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20629 return dwo_name;
20630 }
20631
20632 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20633 and holds a non-zero value. This function should only be used for
20634 DW_FORM_flag or DW_FORM_flag_present attributes. */
20635
20636 static int
20637 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20638 {
20639 struct attribute *attr = dwarf2_attr (die, name, cu);
20640
20641 return attr != nullptr && attr->as_boolean ();
20642 }
20643
20644 static int
20645 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20646 {
20647 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20648 which value is non-zero. However, we have to be careful with
20649 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20650 (via dwarf2_flag_true_p) follows this attribute. So we may
20651 end up accidently finding a declaration attribute that belongs
20652 to a different DIE referenced by the specification attribute,
20653 even though the given DIE does not have a declaration attribute. */
20654 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20655 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20656 }
20657
20658 /* Return the die giving the specification for DIE, if there is
20659 one. *SPEC_CU is the CU containing DIE on input, and the CU
20660 containing the return value on output. If there is no
20661 specification, but there is an abstract origin, that is
20662 returned. */
20663
20664 static struct die_info *
20665 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20666 {
20667 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20668 *spec_cu);
20669
20670 if (spec_attr == NULL)
20671 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20672
20673 if (spec_attr == NULL)
20674 return NULL;
20675 else
20676 return follow_die_ref (die, spec_attr, spec_cu);
20677 }
20678
20679 /* A convenience function to find the proper .debug_line section for a CU. */
20680
20681 static struct dwarf2_section_info *
20682 get_debug_line_section (struct dwarf2_cu *cu)
20683 {
20684 struct dwarf2_section_info *section;
20685 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20686
20687 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20688 DWO file. */
20689 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20690 section = &cu->dwo_unit->dwo_file->sections.line;
20691 else if (cu->per_cu->is_dwz)
20692 {
20693 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20694
20695 section = &dwz->line;
20696 }
20697 else
20698 section = &per_objfile->per_bfd->line;
20699
20700 return section;
20701 }
20702
20703 /* Read the statement program header starting at OFFSET in
20704 .debug_line, or .debug_line.dwo. Return a pointer
20705 to a struct line_header, allocated using xmalloc.
20706 Returns NULL if there is a problem reading the header, e.g., if it
20707 has a version we don't understand.
20708
20709 NOTE: the strings in the include directory and file name tables of
20710 the returned object point into the dwarf line section buffer,
20711 and must not be freed. */
20712
20713 static line_header_up
20714 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20715 {
20716 struct dwarf2_section_info *section;
20717 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20718
20719 section = get_debug_line_section (cu);
20720 section->read (per_objfile->objfile);
20721 if (section->buffer == NULL)
20722 {
20723 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20724 complaint (_("missing .debug_line.dwo section"));
20725 else
20726 complaint (_("missing .debug_line section"));
20727 return 0;
20728 }
20729
20730 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20731 per_objfile, section, &cu->header);
20732 }
20733
20734 /* Subroutine of dwarf_decode_lines to simplify it.
20735 Return the file name for the given file_entry.
20736 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
20737 If space for the result is malloc'd, *NAME_HOLDER will be set.
20738 Returns NULL if FILE_INDEX should be ignored, i.e., it is
20739 equivalent to CU_INFO. */
20740
20741 static const char *
20742 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
20743 const file_and_directory &cu_info,
20744 gdb::unique_xmalloc_ptr<char> *name_holder)
20745 {
20746 const char *include_name = fe.name;
20747 const char *include_name_to_compare = include_name;
20748
20749 const char *dir_name = fe.include_dir (lh);
20750
20751 gdb::unique_xmalloc_ptr<char> hold_compare;
20752 if (!IS_ABSOLUTE_PATH (include_name)
20753 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
20754 {
20755 /* Avoid creating a duplicate name for CU_INFO.
20756 We do this by comparing INCLUDE_NAME and CU_INFO.
20757 Before we do the comparison, however, we need to account
20758 for DIR_NAME and COMP_DIR.
20759 First prepend dir_name (if non-NULL). If we still don't
20760 have an absolute path prepend comp_dir (if non-NULL).
20761 However, the directory we record in the include-file's
20762 psymtab does not contain COMP_DIR (to match the
20763 corresponding symtab(s)).
20764
20765 Example:
20766
20767 bash$ cd /tmp
20768 bash$ gcc -g ./hello.c
20769 include_name = "hello.c"
20770 dir_name = "."
20771 DW_AT_comp_dir = comp_dir = "/tmp"
20772 DW_AT_name = "./hello.c"
20773
20774 */
20775
20776 if (dir_name != NULL)
20777 {
20778 name_holder->reset (concat (dir_name, SLASH_STRING,
20779 include_name, (char *) NULL));
20780 include_name = name_holder->get ();
20781 include_name_to_compare = include_name;
20782 }
20783 if (!IS_ABSOLUTE_PATH (include_name)
20784 && cu_info.get_comp_dir () != nullptr)
20785 {
20786 hold_compare.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20787 include_name, (char *) NULL));
20788 include_name_to_compare = hold_compare.get ();
20789 }
20790 }
20791
20792 gdb::unique_xmalloc_ptr<char> copied_name;
20793 const char *cu_filename = cu_info.get_name ();
20794 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
20795 {
20796 copied_name.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20797 cu_filename, (char *) NULL));
20798 cu_filename = copied_name.get ();
20799 }
20800
20801 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
20802 return nullptr;
20803 return include_name;
20804 }
20805
20806 /* State machine to track the state of the line number program. */
20807
20808 class lnp_state_machine
20809 {
20810 public:
20811 /* Initialize a machine state for the start of a line number
20812 program. */
20813 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20814 bool record_lines_p);
20815
20816 file_entry *current_file ()
20817 {
20818 /* lh->file_names is 0-based, but the file name numbers in the
20819 statement program are 1-based. */
20820 return m_line_header->file_name_at (m_file);
20821 }
20822
20823 /* Record the line in the state machine. END_SEQUENCE is true if
20824 we're processing the end of a sequence. */
20825 void record_line (bool end_sequence);
20826
20827 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20828 nop-out rest of the lines in this sequence. */
20829 void check_line_address (struct dwarf2_cu *cu,
20830 const gdb_byte *line_ptr,
20831 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20832
20833 void handle_set_discriminator (unsigned int discriminator)
20834 {
20835 m_discriminator = discriminator;
20836 m_line_has_non_zero_discriminator |= discriminator != 0;
20837 }
20838
20839 /* Handle DW_LNE_set_address. */
20840 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20841 {
20842 m_op_index = 0;
20843 address += baseaddr;
20844 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20845 }
20846
20847 /* Handle DW_LNS_advance_pc. */
20848 void handle_advance_pc (CORE_ADDR adjust);
20849
20850 /* Handle a special opcode. */
20851 void handle_special_opcode (unsigned char op_code);
20852
20853 /* Handle DW_LNS_advance_line. */
20854 void handle_advance_line (int line_delta)
20855 {
20856 advance_line (line_delta);
20857 }
20858
20859 /* Handle DW_LNS_set_file. */
20860 void handle_set_file (file_name_index file);
20861
20862 /* Handle DW_LNS_negate_stmt. */
20863 void handle_negate_stmt ()
20864 {
20865 m_is_stmt = !m_is_stmt;
20866 }
20867
20868 /* Handle DW_LNS_const_add_pc. */
20869 void handle_const_add_pc ();
20870
20871 /* Handle DW_LNS_fixed_advance_pc. */
20872 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20873 {
20874 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20875 m_op_index = 0;
20876 }
20877
20878 /* Handle DW_LNS_copy. */
20879 void handle_copy ()
20880 {
20881 record_line (false);
20882 m_discriminator = 0;
20883 }
20884
20885 /* Handle DW_LNE_end_sequence. */
20886 void handle_end_sequence ()
20887 {
20888 m_currently_recording_lines = true;
20889 }
20890
20891 private:
20892 /* Advance the line by LINE_DELTA. */
20893 void advance_line (int line_delta)
20894 {
20895 m_line += line_delta;
20896
20897 if (line_delta != 0)
20898 m_line_has_non_zero_discriminator = m_discriminator != 0;
20899 }
20900
20901 struct dwarf2_cu *m_cu;
20902
20903 gdbarch *m_gdbarch;
20904
20905 /* True if we're recording lines.
20906 Otherwise we're building partial symtabs and are just interested in
20907 finding include files mentioned by the line number program. */
20908 bool m_record_lines_p;
20909
20910 /* The line number header. */
20911 line_header *m_line_header;
20912
20913 /* These are part of the standard DWARF line number state machine,
20914 and initialized according to the DWARF spec. */
20915
20916 unsigned char m_op_index = 0;
20917 /* The line table index of the current file. */
20918 file_name_index m_file = 1;
20919 unsigned int m_line = 1;
20920
20921 /* These are initialized in the constructor. */
20922
20923 CORE_ADDR m_address;
20924 bool m_is_stmt;
20925 unsigned int m_discriminator;
20926
20927 /* Additional bits of state we need to track. */
20928
20929 /* The last file that we called dwarf2_start_subfile for.
20930 This is only used for TLLs. */
20931 unsigned int m_last_file = 0;
20932 /* The last file a line number was recorded for. */
20933 struct subfile *m_last_subfile = NULL;
20934
20935 /* The address of the last line entry. */
20936 CORE_ADDR m_last_address;
20937
20938 /* Set to true when a previous line at the same address (using
20939 m_last_address) had m_is_stmt true. This is reset to false when a
20940 line entry at a new address (m_address different to m_last_address) is
20941 processed. */
20942 bool m_stmt_at_address = false;
20943
20944 /* When true, record the lines we decode. */
20945 bool m_currently_recording_lines = false;
20946
20947 /* The last line number that was recorded, used to coalesce
20948 consecutive entries for the same line. This can happen, for
20949 example, when discriminators are present. PR 17276. */
20950 unsigned int m_last_line = 0;
20951 bool m_line_has_non_zero_discriminator = false;
20952 };
20953
20954 void
20955 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20956 {
20957 CORE_ADDR addr_adj = (((m_op_index + adjust)
20958 / m_line_header->maximum_ops_per_instruction)
20959 * m_line_header->minimum_instruction_length);
20960 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20961 m_op_index = ((m_op_index + adjust)
20962 % m_line_header->maximum_ops_per_instruction);
20963 }
20964
20965 void
20966 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20967 {
20968 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20969 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20970 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20971 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20972 / m_line_header->maximum_ops_per_instruction)
20973 * m_line_header->minimum_instruction_length);
20974 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20975 m_op_index = ((m_op_index + adj_opcode_d)
20976 % m_line_header->maximum_ops_per_instruction);
20977
20978 int line_delta = m_line_header->line_base + adj_opcode_r;
20979 advance_line (line_delta);
20980 record_line (false);
20981 m_discriminator = 0;
20982 }
20983
20984 void
20985 lnp_state_machine::handle_set_file (file_name_index file)
20986 {
20987 m_file = file;
20988
20989 const file_entry *fe = current_file ();
20990 if (fe == NULL)
20991 dwarf2_debug_line_missing_file_complaint ();
20992 else if (m_record_lines_p)
20993 {
20994 const char *dir = fe->include_dir (m_line_header);
20995
20996 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20997 m_line_has_non_zero_discriminator = m_discriminator != 0;
20998 dwarf2_start_subfile (m_cu, fe->name, dir);
20999 }
21000 }
21001
21002 void
21003 lnp_state_machine::handle_const_add_pc ()
21004 {
21005 CORE_ADDR adjust
21006 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21007
21008 CORE_ADDR addr_adj
21009 = (((m_op_index + adjust)
21010 / m_line_header->maximum_ops_per_instruction)
21011 * m_line_header->minimum_instruction_length);
21012
21013 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21014 m_op_index = ((m_op_index + adjust)
21015 % m_line_header->maximum_ops_per_instruction);
21016 }
21017
21018 /* Return non-zero if we should add LINE to the line number table.
21019 LINE is the line to add, LAST_LINE is the last line that was added,
21020 LAST_SUBFILE is the subfile for LAST_LINE.
21021 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21022 had a non-zero discriminator.
21023
21024 We have to be careful in the presence of discriminators.
21025 E.g., for this line:
21026
21027 for (i = 0; i < 100000; i++);
21028
21029 clang can emit four line number entries for that one line,
21030 each with a different discriminator.
21031 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21032
21033 However, we want gdb to coalesce all four entries into one.
21034 Otherwise the user could stepi into the middle of the line and
21035 gdb would get confused about whether the pc really was in the
21036 middle of the line.
21037
21038 Things are further complicated by the fact that two consecutive
21039 line number entries for the same line is a heuristic used by gcc
21040 to denote the end of the prologue. So we can't just discard duplicate
21041 entries, we have to be selective about it. The heuristic we use is
21042 that we only collapse consecutive entries for the same line if at least
21043 one of those entries has a non-zero discriminator. PR 17276.
21044
21045 Note: Addresses in the line number state machine can never go backwards
21046 within one sequence, thus this coalescing is ok. */
21047
21048 static int
21049 dwarf_record_line_p (struct dwarf2_cu *cu,
21050 unsigned int line, unsigned int last_line,
21051 int line_has_non_zero_discriminator,
21052 struct subfile *last_subfile)
21053 {
21054 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21055 return 1;
21056 if (line != last_line)
21057 return 1;
21058 /* Same line for the same file that we've seen already.
21059 As a last check, for pr 17276, only record the line if the line
21060 has never had a non-zero discriminator. */
21061 if (!line_has_non_zero_discriminator)
21062 return 1;
21063 return 0;
21064 }
21065
21066 /* Use the CU's builder to record line number LINE beginning at
21067 address ADDRESS in the line table of subfile SUBFILE. */
21068
21069 static void
21070 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21071 unsigned int line, CORE_ADDR address, bool is_stmt,
21072 struct dwarf2_cu *cu)
21073 {
21074 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21075
21076 if (dwarf_line_debug)
21077 {
21078 fprintf_unfiltered (gdb_stdlog,
21079 "Recording line %u, file %s, address %s\n",
21080 line, lbasename (subfile->name),
21081 paddress (gdbarch, address));
21082 }
21083
21084 if (cu != nullptr)
21085 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21086 }
21087
21088 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21089 Mark the end of a set of line number records.
21090 The arguments are the same as for dwarf_record_line_1.
21091 If SUBFILE is NULL the request is ignored. */
21092
21093 static void
21094 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21095 CORE_ADDR address, struct dwarf2_cu *cu)
21096 {
21097 if (subfile == NULL)
21098 return;
21099
21100 if (dwarf_line_debug)
21101 {
21102 fprintf_unfiltered (gdb_stdlog,
21103 "Finishing current line, file %s, address %s\n",
21104 lbasename (subfile->name),
21105 paddress (gdbarch, address));
21106 }
21107
21108 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21109 }
21110
21111 void
21112 lnp_state_machine::record_line (bool end_sequence)
21113 {
21114 if (dwarf_line_debug)
21115 {
21116 fprintf_unfiltered (gdb_stdlog,
21117 "Processing actual line %u: file %u,"
21118 " address %s, is_stmt %u, discrim %u%s\n",
21119 m_line, m_file,
21120 paddress (m_gdbarch, m_address),
21121 m_is_stmt, m_discriminator,
21122 (end_sequence ? "\t(end sequence)" : ""));
21123 }
21124
21125 file_entry *fe = current_file ();
21126
21127 if (fe == NULL)
21128 dwarf2_debug_line_missing_file_complaint ();
21129 /* For now we ignore lines not starting on an instruction boundary.
21130 But not when processing end_sequence for compatibility with the
21131 previous version of the code. */
21132 else if (m_op_index == 0 || end_sequence)
21133 {
21134 fe->included_p = true;
21135 if (m_record_lines_p)
21136 {
21137 /* When we switch files we insert an end maker in the first file,
21138 switch to the second file and add a new line entry. The
21139 problem is that the end marker inserted in the first file will
21140 discard any previous line entries at the same address. If the
21141 line entries in the first file are marked as is-stmt, while
21142 the new line in the second file is non-stmt, then this means
21143 the end marker will discard is-stmt lines so we can have a
21144 non-stmt line. This means that there are less addresses at
21145 which the user can insert a breakpoint.
21146
21147 To improve this we track the last address in m_last_address,
21148 and whether we have seen an is-stmt at this address. Then
21149 when switching files, if we have seen a stmt at the current
21150 address, and we are switching to create a non-stmt line, then
21151 discard the new line. */
21152 bool file_changed
21153 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21154 bool ignore_this_line
21155 = ((file_changed && !end_sequence && m_last_address == m_address
21156 && !m_is_stmt && m_stmt_at_address)
21157 || (!end_sequence && m_line == 0));
21158
21159 if ((file_changed && !ignore_this_line) || end_sequence)
21160 {
21161 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21162 m_currently_recording_lines ? m_cu : nullptr);
21163 }
21164
21165 if (!end_sequence && !ignore_this_line)
21166 {
21167 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21168
21169 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21170 m_line_has_non_zero_discriminator,
21171 m_last_subfile))
21172 {
21173 buildsym_compunit *builder = m_cu->get_builder ();
21174 dwarf_record_line_1 (m_gdbarch,
21175 builder->get_current_subfile (),
21176 m_line, m_address, is_stmt,
21177 m_currently_recording_lines ? m_cu : nullptr);
21178 }
21179 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21180 m_last_line = m_line;
21181 }
21182 }
21183 }
21184
21185 /* Track whether we have seen any m_is_stmt true at m_address in case we
21186 have multiple line table entries all at m_address. */
21187 if (m_last_address != m_address)
21188 {
21189 m_stmt_at_address = false;
21190 m_last_address = m_address;
21191 }
21192 m_stmt_at_address |= m_is_stmt;
21193 }
21194
21195 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21196 line_header *lh, bool record_lines_p)
21197 {
21198 m_cu = cu;
21199 m_gdbarch = arch;
21200 m_record_lines_p = record_lines_p;
21201 m_line_header = lh;
21202
21203 m_currently_recording_lines = true;
21204
21205 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21206 was a line entry for it so that the backend has a chance to adjust it
21207 and also record it in case it needs it. This is currently used by MIPS
21208 code, cf. `mips_adjust_dwarf2_line'. */
21209 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21210 m_is_stmt = lh->default_is_stmt;
21211 m_discriminator = 0;
21212
21213 m_last_address = m_address;
21214 m_stmt_at_address = false;
21215 }
21216
21217 void
21218 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21219 const gdb_byte *line_ptr,
21220 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21221 {
21222 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21223 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21224 located at 0x0. In this case, additionally check that if
21225 ADDRESS < UNRELOCATED_LOWPC. */
21226
21227 if ((address == 0 && address < unrelocated_lowpc)
21228 || address == (CORE_ADDR) -1)
21229 {
21230 /* This line table is for a function which has been
21231 GCd by the linker. Ignore it. PR gdb/12528 */
21232
21233 struct objfile *objfile = cu->per_objfile->objfile;
21234 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21235
21236 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21237 line_offset, objfile_name (objfile));
21238 m_currently_recording_lines = false;
21239 /* Note: m_currently_recording_lines is left as false until we see
21240 DW_LNE_end_sequence. */
21241 }
21242 }
21243
21244 /* Subroutine of dwarf_decode_lines to simplify it.
21245 Process the line number information in LH.
21246 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21247 program in order to set included_p for every referenced header. */
21248
21249 static void
21250 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21251 const int decode_for_pst_p, CORE_ADDR lowpc)
21252 {
21253 const gdb_byte *line_ptr, *extended_end;
21254 const gdb_byte *line_end;
21255 unsigned int bytes_read, extended_len;
21256 unsigned char op_code, extended_op;
21257 CORE_ADDR baseaddr;
21258 struct objfile *objfile = cu->per_objfile->objfile;
21259 bfd *abfd = objfile->obfd;
21260 struct gdbarch *gdbarch = objfile->arch ();
21261 /* True if we're recording line info (as opposed to building partial
21262 symtabs and just interested in finding include files mentioned by
21263 the line number program). */
21264 bool record_lines_p = !decode_for_pst_p;
21265
21266 baseaddr = objfile->text_section_offset ();
21267
21268 line_ptr = lh->statement_program_start;
21269 line_end = lh->statement_program_end;
21270
21271 /* Read the statement sequences until there's nothing left. */
21272 while (line_ptr < line_end)
21273 {
21274 /* The DWARF line number program state machine. Reset the state
21275 machine at the start of each sequence. */
21276 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21277 bool end_sequence = false;
21278
21279 if (record_lines_p)
21280 {
21281 /* Start a subfile for the current file of the state
21282 machine. */
21283 const file_entry *fe = state_machine.current_file ();
21284
21285 if (fe != NULL)
21286 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21287 }
21288
21289 /* Decode the table. */
21290 while (line_ptr < line_end && !end_sequence)
21291 {
21292 op_code = read_1_byte (abfd, line_ptr);
21293 line_ptr += 1;
21294
21295 if (op_code >= lh->opcode_base)
21296 {
21297 /* Special opcode. */
21298 state_machine.handle_special_opcode (op_code);
21299 }
21300 else switch (op_code)
21301 {
21302 case DW_LNS_extended_op:
21303 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21304 &bytes_read);
21305 line_ptr += bytes_read;
21306 extended_end = line_ptr + extended_len;
21307 extended_op = read_1_byte (abfd, line_ptr);
21308 line_ptr += 1;
21309 if (DW_LNE_lo_user <= extended_op
21310 && extended_op <= DW_LNE_hi_user)
21311 {
21312 /* Vendor extension, ignore. */
21313 line_ptr = extended_end;
21314 break;
21315 }
21316 switch (extended_op)
21317 {
21318 case DW_LNE_end_sequence:
21319 state_machine.handle_end_sequence ();
21320 end_sequence = true;
21321 break;
21322 case DW_LNE_set_address:
21323 {
21324 CORE_ADDR address
21325 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21326 line_ptr += bytes_read;
21327
21328 state_machine.check_line_address (cu, line_ptr,
21329 lowpc - baseaddr, address);
21330 state_machine.handle_set_address (baseaddr, address);
21331 }
21332 break;
21333 case DW_LNE_define_file:
21334 {
21335 const char *cur_file;
21336 unsigned int mod_time, length;
21337 dir_index dindex;
21338
21339 cur_file = read_direct_string (abfd, line_ptr,
21340 &bytes_read);
21341 line_ptr += bytes_read;
21342 dindex = (dir_index)
21343 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21344 line_ptr += bytes_read;
21345 mod_time =
21346 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21347 line_ptr += bytes_read;
21348 length =
21349 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21350 line_ptr += bytes_read;
21351 lh->add_file_name (cur_file, dindex, mod_time, length);
21352 }
21353 break;
21354 case DW_LNE_set_discriminator:
21355 {
21356 /* The discriminator is not interesting to the
21357 debugger; just ignore it. We still need to
21358 check its value though:
21359 if there are consecutive entries for the same
21360 (non-prologue) line we want to coalesce them.
21361 PR 17276. */
21362 unsigned int discr
21363 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21364 line_ptr += bytes_read;
21365
21366 state_machine.handle_set_discriminator (discr);
21367 }
21368 break;
21369 default:
21370 complaint (_("mangled .debug_line section"));
21371 return;
21372 }
21373 /* Make sure that we parsed the extended op correctly. If e.g.
21374 we expected a different address size than the producer used,
21375 we may have read the wrong number of bytes. */
21376 if (line_ptr != extended_end)
21377 {
21378 complaint (_("mangled .debug_line section"));
21379 return;
21380 }
21381 break;
21382 case DW_LNS_copy:
21383 state_machine.handle_copy ();
21384 break;
21385 case DW_LNS_advance_pc:
21386 {
21387 CORE_ADDR adjust
21388 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21389 line_ptr += bytes_read;
21390
21391 state_machine.handle_advance_pc (adjust);
21392 }
21393 break;
21394 case DW_LNS_advance_line:
21395 {
21396 int line_delta
21397 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21398 line_ptr += bytes_read;
21399
21400 state_machine.handle_advance_line (line_delta);
21401 }
21402 break;
21403 case DW_LNS_set_file:
21404 {
21405 file_name_index file
21406 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21407 &bytes_read);
21408 line_ptr += bytes_read;
21409
21410 state_machine.handle_set_file (file);
21411 }
21412 break;
21413 case DW_LNS_set_column:
21414 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21415 line_ptr += bytes_read;
21416 break;
21417 case DW_LNS_negate_stmt:
21418 state_machine.handle_negate_stmt ();
21419 break;
21420 case DW_LNS_set_basic_block:
21421 break;
21422 /* Add to the address register of the state machine the
21423 address increment value corresponding to special opcode
21424 255. I.e., this value is scaled by the minimum
21425 instruction length since special opcode 255 would have
21426 scaled the increment. */
21427 case DW_LNS_const_add_pc:
21428 state_machine.handle_const_add_pc ();
21429 break;
21430 case DW_LNS_fixed_advance_pc:
21431 {
21432 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21433 line_ptr += 2;
21434
21435 state_machine.handle_fixed_advance_pc (addr_adj);
21436 }
21437 break;
21438 default:
21439 {
21440 /* Unknown standard opcode, ignore it. */
21441 int i;
21442
21443 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21444 {
21445 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21446 line_ptr += bytes_read;
21447 }
21448 }
21449 }
21450 }
21451
21452 if (!end_sequence)
21453 dwarf2_debug_line_missing_end_sequence_complaint ();
21454
21455 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21456 in which case we still finish recording the last line). */
21457 state_machine.record_line (true);
21458 }
21459 }
21460
21461 /* Decode the Line Number Program (LNP) for the given line_header
21462 structure and CU. The actual information extracted and the type
21463 of structures created from the LNP depends on the value of PST.
21464
21465 1. If PST is NULL, then this procedure uses the data from the program
21466 to create all necessary symbol tables, and their linetables.
21467
21468 2. If PST is not NULL, this procedure reads the program to determine
21469 the list of files included by the unit represented by PST, and
21470 builds all the associated partial symbol tables.
21471
21472 FND holds the CU file name and directory, if known.
21473 It is used for relative paths in the line table.
21474
21475 NOTE: It is important that psymtabs have the same file name (via
21476 strcmp) as the corresponding symtab. Since the directory is not
21477 used in the name of the symtab we don't use it in the name of the
21478 psymtabs we create. E.g. expand_line_sal requires this when
21479 finding psymtabs to expand. A good testcase for this is
21480 mb-inline.exp.
21481
21482 LOWPC is the lowest address in CU (or 0 if not known).
21483
21484 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21485 for its PC<->lines mapping information. Otherwise only the filename
21486 table is read in. */
21487
21488 static void
21489 dwarf_decode_lines (struct line_header *lh, const file_and_directory &fnd,
21490 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21491 CORE_ADDR lowpc, int decode_mapping)
21492 {
21493 struct objfile *objfile = cu->per_objfile->objfile;
21494 const int decode_for_pst_p = (pst != NULL);
21495
21496 if (decode_mapping)
21497 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21498
21499 if (decode_for_pst_p)
21500 {
21501 /* Now that we're done scanning the Line Header Program, we can
21502 create the psymtab of each included file. */
21503 for (auto &file_entry : lh->file_names ())
21504 if (file_entry.included_p)
21505 {
21506 gdb::unique_xmalloc_ptr<char> name_holder;
21507 const char *include_name =
21508 compute_include_file_name (lh, file_entry, fnd, &name_holder);
21509 if (include_name != NULL)
21510 dwarf2_create_include_psymtab
21511 (cu->per_objfile->per_bfd, include_name, pst,
21512 cu->per_objfile->per_bfd->partial_symtabs.get (),
21513 objfile->per_bfd);
21514 }
21515 }
21516 else
21517 {
21518 /* Make sure a symtab is created for every file, even files
21519 which contain only variables (i.e. no code with associated
21520 line numbers). */
21521 buildsym_compunit *builder = cu->get_builder ();
21522 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21523
21524 for (auto &fe : lh->file_names ())
21525 {
21526 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21527 if (builder->get_current_subfile ()->symtab == NULL)
21528 {
21529 builder->get_current_subfile ()->symtab
21530 = allocate_symtab (cust,
21531 builder->get_current_subfile ()->name);
21532 }
21533 fe.symtab = builder->get_current_subfile ()->symtab;
21534 }
21535 }
21536 }
21537
21538 /* Start a subfile for DWARF. FILENAME is the name of the file and
21539 DIRNAME the name of the source directory which contains FILENAME
21540 or NULL if not known.
21541 This routine tries to keep line numbers from identical absolute and
21542 relative file names in a common subfile.
21543
21544 Using the `list' example from the GDB testsuite, which resides in
21545 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21546 of /srcdir/list0.c yields the following debugging information for list0.c:
21547
21548 DW_AT_name: /srcdir/list0.c
21549 DW_AT_comp_dir: /compdir
21550 files.files[0].name: list0.h
21551 files.files[0].dir: /srcdir
21552 files.files[1].name: list0.c
21553 files.files[1].dir: /srcdir
21554
21555 The line number information for list0.c has to end up in a single
21556 subfile, so that `break /srcdir/list0.c:1' works as expected.
21557 start_subfile will ensure that this happens provided that we pass the
21558 concatenation of files.files[1].dir and files.files[1].name as the
21559 subfile's name. */
21560
21561 static void
21562 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21563 const char *dirname)
21564 {
21565 gdb::unique_xmalloc_ptr<char> copy;
21566
21567 /* In order not to lose the line information directory,
21568 we concatenate it to the filename when it makes sense.
21569 Note that the Dwarf3 standard says (speaking of filenames in line
21570 information): ``The directory index is ignored for file names
21571 that represent full path names''. Thus ignoring dirname in the
21572 `else' branch below isn't an issue. */
21573
21574 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21575 {
21576 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21577 filename = copy.get ();
21578 }
21579
21580 cu->get_builder ()->start_subfile (filename);
21581 }
21582
21583 static void
21584 var_decode_location (struct attribute *attr, struct symbol *sym,
21585 struct dwarf2_cu *cu)
21586 {
21587 struct objfile *objfile = cu->per_objfile->objfile;
21588 struct comp_unit_head *cu_header = &cu->header;
21589
21590 /* NOTE drow/2003-01-30: There used to be a comment and some special
21591 code here to turn a symbol with DW_AT_external and a
21592 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21593 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21594 with some versions of binutils) where shared libraries could have
21595 relocations against symbols in their debug information - the
21596 minimal symbol would have the right address, but the debug info
21597 would not. It's no longer necessary, because we will explicitly
21598 apply relocations when we read in the debug information now. */
21599
21600 /* A DW_AT_location attribute with no contents indicates that a
21601 variable has been optimized away. */
21602 if (attr->form_is_block () && attr->as_block ()->size == 0)
21603 {
21604 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21605 return;
21606 }
21607
21608 /* Handle one degenerate form of location expression specially, to
21609 preserve GDB's previous behavior when section offsets are
21610 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21611 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21612
21613 if (attr->form_is_block ())
21614 {
21615 struct dwarf_block *block = attr->as_block ();
21616
21617 if ((block->data[0] == DW_OP_addr
21618 && block->size == 1 + cu_header->addr_size)
21619 || ((block->data[0] == DW_OP_GNU_addr_index
21620 || block->data[0] == DW_OP_addrx)
21621 && (block->size
21622 == 1 + leb128_size (&block->data[1]))))
21623 {
21624 unsigned int dummy;
21625
21626 if (block->data[0] == DW_OP_addr)
21627 SET_SYMBOL_VALUE_ADDRESS
21628 (sym, cu->header.read_address (objfile->obfd,
21629 block->data + 1,
21630 &dummy));
21631 else
21632 SET_SYMBOL_VALUE_ADDRESS
21633 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21634 &dummy));
21635 sym->set_aclass_index (LOC_STATIC);
21636 fixup_symbol_section (sym, objfile);
21637 SET_SYMBOL_VALUE_ADDRESS
21638 (sym,
21639 SYMBOL_VALUE_ADDRESS (sym)
21640 + objfile->section_offsets[sym->section_index ()]);
21641 return;
21642 }
21643 }
21644
21645 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21646 expression evaluator, and use LOC_COMPUTED only when necessary
21647 (i.e. when the value of a register or memory location is
21648 referenced, or a thread-local block, etc.). Then again, it might
21649 not be worthwhile. I'm assuming that it isn't unless performance
21650 or memory numbers show me otherwise. */
21651
21652 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21653
21654 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21655 cu->has_loclist = true;
21656 }
21657
21658 /* Given a pointer to a DWARF information entry, figure out if we need
21659 to make a symbol table entry for it, and if so, create a new entry
21660 and return a pointer to it.
21661 If TYPE is NULL, determine symbol type from the die, otherwise
21662 used the passed type.
21663 If SPACE is not NULL, use it to hold the new symbol. If it is
21664 NULL, allocate a new symbol on the objfile's obstack. */
21665
21666 static struct symbol *
21667 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21668 struct symbol *space)
21669 {
21670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21671 struct objfile *objfile = per_objfile->objfile;
21672 struct gdbarch *gdbarch = objfile->arch ();
21673 struct symbol *sym = NULL;
21674 const char *name;
21675 struct attribute *attr = NULL;
21676 struct attribute *attr2 = NULL;
21677 CORE_ADDR baseaddr;
21678 struct pending **list_to_add = NULL;
21679
21680 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21681
21682 baseaddr = objfile->text_section_offset ();
21683
21684 name = dwarf2_name (die, cu);
21685 if (name)
21686 {
21687 int suppress_add = 0;
21688
21689 if (space)
21690 sym = space;
21691 else
21692 sym = new (&objfile->objfile_obstack) symbol;
21693 OBJSTAT (objfile, n_syms++);
21694
21695 /* Cache this symbol's name and the name's demangled form (if any). */
21696 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
21697 /* Fortran does not have mangling standard and the mangling does differ
21698 between gfortran, iFort etc. */
21699 const char *physname
21700 = (cu->per_cu->lang == language_fortran
21701 ? dwarf2_full_name (name, die, cu)
21702 : dwarf2_physname (name, die, cu));
21703 const char *linkagename = dw2_linkage_name (die, cu);
21704
21705 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
21706 sym->set_linkage_name (physname);
21707 else
21708 {
21709 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21710 sym->set_linkage_name (linkagename);
21711 }
21712
21713 /* Handle DW_AT_artificial. */
21714 attr = dwarf2_attr (die, DW_AT_artificial, cu);
21715 if (attr != nullptr)
21716 sym->artificial = attr->as_boolean ();
21717
21718 /* Default assumptions.
21719 Use the passed type or decode it from the die. */
21720 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21721 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21722 if (type != NULL)
21723 SYMBOL_TYPE (sym) = type;
21724 else
21725 SYMBOL_TYPE (sym) = die_type (die, cu);
21726 attr = dwarf2_attr (die,
21727 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21728 cu);
21729 if (attr != nullptr)
21730 SYMBOL_LINE (sym) = attr->constant_value (0);
21731
21732 attr = dwarf2_attr (die,
21733 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21734 cu);
21735 if (attr != nullptr && attr->is_nonnegative ())
21736 {
21737 file_name_index file_index
21738 = (file_name_index) attr->as_nonnegative ();
21739 struct file_entry *fe;
21740
21741 if (cu->line_header != NULL)
21742 fe = cu->line_header->file_name_at (file_index);
21743 else
21744 fe = NULL;
21745
21746 if (fe == NULL)
21747 complaint (_("file index out of range"));
21748 else
21749 symbol_set_symtab (sym, fe->symtab);
21750 }
21751
21752 switch (die->tag)
21753 {
21754 case DW_TAG_label:
21755 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21756 if (attr != nullptr)
21757 {
21758 CORE_ADDR addr;
21759
21760 addr = attr->as_address ();
21761 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21762 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21763 sym->set_aclass_index (LOC_LABEL);
21764 }
21765 else
21766 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21767 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21768 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21769 add_symbol_to_list (sym, cu->list_in_scope);
21770 break;
21771 case DW_TAG_subprogram:
21772 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21773 finish_block. */
21774 sym->set_aclass_index (LOC_BLOCK);
21775 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21776 if ((attr2 != nullptr && attr2->as_boolean ())
21777 || cu->per_cu->lang == language_ada
21778 || cu->per_cu->lang == language_fortran)
21779 {
21780 /* Subprograms marked external are stored as a global symbol.
21781 Ada and Fortran subprograms, whether marked external or
21782 not, are always stored as a global symbol, because we want
21783 to be able to access them globally. For instance, we want
21784 to be able to break on a nested subprogram without having
21785 to specify the context. */
21786 list_to_add = cu->get_builder ()->get_global_symbols ();
21787 }
21788 else
21789 {
21790 list_to_add = cu->list_in_scope;
21791 }
21792 break;
21793 case DW_TAG_inlined_subroutine:
21794 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21795 finish_block. */
21796 sym->set_aclass_index (LOC_BLOCK);
21797 SYMBOL_INLINED (sym) = 1;
21798 list_to_add = cu->list_in_scope;
21799 break;
21800 case DW_TAG_template_value_param:
21801 suppress_add = 1;
21802 /* Fall through. */
21803 case DW_TAG_constant:
21804 case DW_TAG_variable:
21805 case DW_TAG_member:
21806 /* Compilation with minimal debug info may result in
21807 variables with missing type entries. Change the
21808 misleading `void' type to something sensible. */
21809 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21810 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21811
21812 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21813 /* In the case of DW_TAG_member, we should only be called for
21814 static const members. */
21815 if (die->tag == DW_TAG_member)
21816 {
21817 /* dwarf2_add_field uses die_is_declaration,
21818 so we do the same. */
21819 gdb_assert (die_is_declaration (die, cu));
21820 gdb_assert (attr);
21821 }
21822 if (attr != nullptr)
21823 {
21824 dwarf2_const_value (attr, sym, cu);
21825 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21826 if (!suppress_add)
21827 {
21828 if (attr2 != nullptr && attr2->as_boolean ())
21829 list_to_add = cu->get_builder ()->get_global_symbols ();
21830 else
21831 list_to_add = cu->list_in_scope;
21832 }
21833 break;
21834 }
21835 attr = dwarf2_attr (die, DW_AT_location, cu);
21836 if (attr != nullptr)
21837 {
21838 var_decode_location (attr, sym, cu);
21839 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21840
21841 /* Fortran explicitly imports any global symbols to the local
21842 scope by DW_TAG_common_block. */
21843 if (cu->per_cu->lang == language_fortran && die->parent
21844 && die->parent->tag == DW_TAG_common_block)
21845 attr2 = NULL;
21846
21847 if (SYMBOL_CLASS (sym) == LOC_STATIC
21848 && SYMBOL_VALUE_ADDRESS (sym) == 0
21849 && !per_objfile->per_bfd->has_section_at_zero)
21850 {
21851 /* When a static variable is eliminated by the linker,
21852 the corresponding debug information is not stripped
21853 out, but the variable address is set to null;
21854 do not add such variables into symbol table. */
21855 }
21856 else if (attr2 != nullptr && attr2->as_boolean ())
21857 {
21858 if (SYMBOL_CLASS (sym) == LOC_STATIC
21859 && (objfile->flags & OBJF_MAINLINE) == 0
21860 && per_objfile->per_bfd->can_copy)
21861 {
21862 /* A global static variable might be subject to
21863 copy relocation. We first check for a local
21864 minsym, though, because maybe the symbol was
21865 marked hidden, in which case this would not
21866 apply. */
21867 bound_minimal_symbol found
21868 = (lookup_minimal_symbol_linkage
21869 (sym->linkage_name (), objfile));
21870 if (found.minsym != nullptr)
21871 sym->maybe_copied = 1;
21872 }
21873
21874 /* A variable with DW_AT_external is never static,
21875 but it may be block-scoped. */
21876 list_to_add
21877 = ((cu->list_in_scope
21878 == cu->get_builder ()->get_file_symbols ())
21879 ? cu->get_builder ()->get_global_symbols ()
21880 : cu->list_in_scope);
21881 }
21882 else
21883 list_to_add = cu->list_in_scope;
21884 }
21885 else
21886 {
21887 /* We do not know the address of this symbol.
21888 If it is an external symbol and we have type information
21889 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21890 The address of the variable will then be determined from
21891 the minimal symbol table whenever the variable is
21892 referenced. */
21893 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21894
21895 /* Fortran explicitly imports any global symbols to the local
21896 scope by DW_TAG_common_block. */
21897 if (cu->per_cu->lang == language_fortran && die->parent
21898 && die->parent->tag == DW_TAG_common_block)
21899 {
21900 /* SYMBOL_CLASS doesn't matter here because
21901 read_common_block is going to reset it. */
21902 if (!suppress_add)
21903 list_to_add = cu->list_in_scope;
21904 }
21905 else if (attr2 != nullptr && attr2->as_boolean ()
21906 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21907 {
21908 /* A variable with DW_AT_external is never static, but it
21909 may be block-scoped. */
21910 list_to_add
21911 = ((cu->list_in_scope
21912 == cu->get_builder ()->get_file_symbols ())
21913 ? cu->get_builder ()->get_global_symbols ()
21914 : cu->list_in_scope);
21915
21916 sym->set_aclass_index (LOC_UNRESOLVED);
21917 }
21918 else if (!die_is_declaration (die, cu))
21919 {
21920 /* Use the default LOC_OPTIMIZED_OUT class. */
21921 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21922 if (!suppress_add)
21923 list_to_add = cu->list_in_scope;
21924 }
21925 }
21926 break;
21927 case DW_TAG_formal_parameter:
21928 {
21929 /* If we are inside a function, mark this as an argument. If
21930 not, we might be looking at an argument to an inlined function
21931 when we do not have enough information to show inlined frames;
21932 pretend it's a local variable in that case so that the user can
21933 still see it. */
21934 struct context_stack *curr
21935 = cu->get_builder ()->get_current_context_stack ();
21936 if (curr != nullptr && curr->name != nullptr)
21937 SYMBOL_IS_ARGUMENT (sym) = 1;
21938 attr = dwarf2_attr (die, DW_AT_location, cu);
21939 if (attr != nullptr)
21940 {
21941 var_decode_location (attr, sym, cu);
21942 }
21943 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21944 if (attr != nullptr)
21945 {
21946 dwarf2_const_value (attr, sym, cu);
21947 }
21948
21949 list_to_add = cu->list_in_scope;
21950 }
21951 break;
21952 case DW_TAG_unspecified_parameters:
21953 /* From varargs functions; gdb doesn't seem to have any
21954 interest in this information, so just ignore it for now.
21955 (FIXME?) */
21956 break;
21957 case DW_TAG_template_type_param:
21958 suppress_add = 1;
21959 /* Fall through. */
21960 case DW_TAG_class_type:
21961 case DW_TAG_interface_type:
21962 case DW_TAG_structure_type:
21963 case DW_TAG_union_type:
21964 case DW_TAG_set_type:
21965 case DW_TAG_enumeration_type:
21966 sym->set_aclass_index (LOC_TYPEDEF);
21967 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21968
21969 {
21970 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21971 really ever be static objects: otherwise, if you try
21972 to, say, break of a class's method and you're in a file
21973 which doesn't mention that class, it won't work unless
21974 the check for all static symbols in lookup_symbol_aux
21975 saves you. See the OtherFileClass tests in
21976 gdb.c++/namespace.exp. */
21977
21978 if (!suppress_add)
21979 {
21980 buildsym_compunit *builder = cu->get_builder ();
21981 list_to_add
21982 = (cu->list_in_scope == builder->get_file_symbols ()
21983 && cu->per_cu->lang == language_cplus
21984 ? builder->get_global_symbols ()
21985 : cu->list_in_scope);
21986
21987 /* The semantics of C++ state that "struct foo {
21988 ... }" also defines a typedef for "foo". */
21989 if (cu->per_cu->lang == language_cplus
21990 || cu->per_cu->lang == language_ada
21991 || cu->per_cu->lang == language_d
21992 || cu->per_cu->lang == language_rust)
21993 {
21994 /* The symbol's name is already allocated along
21995 with this objfile, so we don't need to
21996 duplicate it for the type. */
21997 if (SYMBOL_TYPE (sym)->name () == 0)
21998 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21999 }
22000 }
22001 }
22002 break;
22003 case DW_TAG_typedef:
22004 sym->set_aclass_index (LOC_TYPEDEF);
22005 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22006 list_to_add = cu->list_in_scope;
22007 break;
22008 case DW_TAG_array_type:
22009 case DW_TAG_base_type:
22010 case DW_TAG_subrange_type:
22011 sym->set_aclass_index (LOC_TYPEDEF);
22012 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22013 list_to_add = cu->list_in_scope;
22014 break;
22015 case DW_TAG_enumerator:
22016 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22017 if (attr != nullptr)
22018 {
22019 dwarf2_const_value (attr, sym, cu);
22020 }
22021 {
22022 /* NOTE: carlton/2003-11-10: See comment above in the
22023 DW_TAG_class_type, etc. block. */
22024
22025 list_to_add
22026 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22027 && cu->per_cu->lang == language_cplus
22028 ? cu->get_builder ()->get_global_symbols ()
22029 : cu->list_in_scope);
22030 }
22031 break;
22032 case DW_TAG_imported_declaration:
22033 case DW_TAG_namespace:
22034 sym->set_aclass_index (LOC_TYPEDEF);
22035 list_to_add = cu->get_builder ()->get_global_symbols ();
22036 break;
22037 case DW_TAG_module:
22038 sym->set_aclass_index (LOC_TYPEDEF);
22039 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22040 list_to_add = cu->get_builder ()->get_global_symbols ();
22041 break;
22042 case DW_TAG_common_block:
22043 sym->set_aclass_index (LOC_COMMON_BLOCK);
22044 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22045 add_symbol_to_list (sym, cu->list_in_scope);
22046 break;
22047 default:
22048 /* Not a tag we recognize. Hopefully we aren't processing
22049 trash data, but since we must specifically ignore things
22050 we don't recognize, there is nothing else we should do at
22051 this point. */
22052 complaint (_("unsupported tag: '%s'"),
22053 dwarf_tag_name (die->tag));
22054 break;
22055 }
22056
22057 if (suppress_add)
22058 {
22059 sym->hash_next = objfile->template_symbols;
22060 objfile->template_symbols = sym;
22061 list_to_add = NULL;
22062 }
22063
22064 if (list_to_add != NULL)
22065 add_symbol_to_list (sym, list_to_add);
22066
22067 /* For the benefit of old versions of GCC, check for anonymous
22068 namespaces based on the demangled name. */
22069 if (!cu->processing_has_namespace_info
22070 && cu->per_cu->lang == language_cplus)
22071 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22072 }
22073 return (sym);
22074 }
22075
22076 /* Given an attr with a DW_FORM_dataN value in host byte order,
22077 zero-extend it as appropriate for the symbol's type. The DWARF
22078 standard (v4) is not entirely clear about the meaning of using
22079 DW_FORM_dataN for a constant with a signed type, where the type is
22080 wider than the data. The conclusion of a discussion on the DWARF
22081 list was that this is unspecified. We choose to always zero-extend
22082 because that is the interpretation long in use by GCC. */
22083
22084 static gdb_byte *
22085 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22086 struct dwarf2_cu *cu, LONGEST *value, int bits)
22087 {
22088 struct objfile *objfile = cu->per_objfile->objfile;
22089 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22090 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22091 LONGEST l = attr->constant_value (0);
22092
22093 if (bits < sizeof (*value) * 8)
22094 {
22095 l &= ((LONGEST) 1 << bits) - 1;
22096 *value = l;
22097 }
22098 else if (bits == sizeof (*value) * 8)
22099 *value = l;
22100 else
22101 {
22102 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22103 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22104 return bytes;
22105 }
22106
22107 return NULL;
22108 }
22109
22110 /* Read a constant value from an attribute. Either set *VALUE, or if
22111 the value does not fit in *VALUE, set *BYTES - either already
22112 allocated on the objfile obstack, or newly allocated on OBSTACK,
22113 or, set *BATON, if we translated the constant to a location
22114 expression. */
22115
22116 static void
22117 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22118 const char *name, struct obstack *obstack,
22119 struct dwarf2_cu *cu,
22120 LONGEST *value, const gdb_byte **bytes,
22121 struct dwarf2_locexpr_baton **baton)
22122 {
22123 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22124 struct objfile *objfile = per_objfile->objfile;
22125 struct comp_unit_head *cu_header = &cu->header;
22126 struct dwarf_block *blk;
22127 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22128 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22129
22130 *value = 0;
22131 *bytes = NULL;
22132 *baton = NULL;
22133
22134 switch (attr->form)
22135 {
22136 case DW_FORM_addr:
22137 case DW_FORM_addrx:
22138 case DW_FORM_GNU_addr_index:
22139 {
22140 gdb_byte *data;
22141
22142 if (TYPE_LENGTH (type) != cu_header->addr_size)
22143 dwarf2_const_value_length_mismatch_complaint (name,
22144 cu_header->addr_size,
22145 TYPE_LENGTH (type));
22146 /* Symbols of this form are reasonably rare, so we just
22147 piggyback on the existing location code rather than writing
22148 a new implementation of symbol_computed_ops. */
22149 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22150 (*baton)->per_objfile = per_objfile;
22151 (*baton)->per_cu = cu->per_cu;
22152 gdb_assert ((*baton)->per_cu);
22153
22154 (*baton)->size = 2 + cu_header->addr_size;
22155 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22156 (*baton)->data = data;
22157
22158 data[0] = DW_OP_addr;
22159 store_unsigned_integer (&data[1], cu_header->addr_size,
22160 byte_order, attr->as_address ());
22161 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22162 }
22163 break;
22164 case DW_FORM_string:
22165 case DW_FORM_strp:
22166 case DW_FORM_strx:
22167 case DW_FORM_GNU_str_index:
22168 case DW_FORM_GNU_strp_alt:
22169 /* The string is already allocated on the objfile obstack, point
22170 directly to it. */
22171 *bytes = (const gdb_byte *) attr->as_string ();
22172 break;
22173 case DW_FORM_block1:
22174 case DW_FORM_block2:
22175 case DW_FORM_block4:
22176 case DW_FORM_block:
22177 case DW_FORM_exprloc:
22178 case DW_FORM_data16:
22179 blk = attr->as_block ();
22180 if (TYPE_LENGTH (type) != blk->size)
22181 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22182 TYPE_LENGTH (type));
22183 *bytes = blk->data;
22184 break;
22185
22186 /* The DW_AT_const_value attributes are supposed to carry the
22187 symbol's value "represented as it would be on the target
22188 architecture." By the time we get here, it's already been
22189 converted to host endianness, so we just need to sign- or
22190 zero-extend it as appropriate. */
22191 case DW_FORM_data1:
22192 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22193 break;
22194 case DW_FORM_data2:
22195 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22196 break;
22197 case DW_FORM_data4:
22198 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22199 break;
22200 case DW_FORM_data8:
22201 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22202 break;
22203
22204 case DW_FORM_sdata:
22205 case DW_FORM_implicit_const:
22206 *value = attr->as_signed ();
22207 break;
22208
22209 case DW_FORM_udata:
22210 *value = attr->as_unsigned ();
22211 break;
22212
22213 default:
22214 complaint (_("unsupported const value attribute form: '%s'"),
22215 dwarf_form_name (attr->form));
22216 *value = 0;
22217 break;
22218 }
22219 }
22220
22221
22222 /* Copy constant value from an attribute to a symbol. */
22223
22224 static void
22225 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22226 struct dwarf2_cu *cu)
22227 {
22228 struct objfile *objfile = cu->per_objfile->objfile;
22229 LONGEST value;
22230 const gdb_byte *bytes;
22231 struct dwarf2_locexpr_baton *baton;
22232
22233 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22234 sym->print_name (),
22235 &objfile->objfile_obstack, cu,
22236 &value, &bytes, &baton);
22237
22238 if (baton != NULL)
22239 {
22240 SYMBOL_LOCATION_BATON (sym) = baton;
22241 sym->set_aclass_index (dwarf2_locexpr_index);
22242 }
22243 else if (bytes != NULL)
22244 {
22245 SYMBOL_VALUE_BYTES (sym) = bytes;
22246 sym->set_aclass_index (LOC_CONST_BYTES);
22247 }
22248 else
22249 {
22250 SYMBOL_VALUE (sym) = value;
22251 sym->set_aclass_index (LOC_CONST);
22252 }
22253 }
22254
22255 /* Return the type of the die in question using its DW_AT_type attribute. */
22256
22257 static struct type *
22258 die_type (struct die_info *die, struct dwarf2_cu *cu)
22259 {
22260 struct attribute *type_attr;
22261
22262 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22263 if (!type_attr)
22264 {
22265 struct objfile *objfile = cu->per_objfile->objfile;
22266 /* A missing DW_AT_type represents a void type. */
22267 return objfile_type (objfile)->builtin_void;
22268 }
22269
22270 return lookup_die_type (die, type_attr, cu);
22271 }
22272
22273 /* True iff CU's producer generates GNAT Ada auxiliary information
22274 that allows to find parallel types through that information instead
22275 of having to do expensive parallel lookups by type name. */
22276
22277 static int
22278 need_gnat_info (struct dwarf2_cu *cu)
22279 {
22280 /* Assume that the Ada compiler was GNAT, which always produces
22281 the auxiliary information. */
22282 return (cu->per_cu->lang == language_ada);
22283 }
22284
22285 /* Return the auxiliary type of the die in question using its
22286 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22287 attribute is not present. */
22288
22289 static struct type *
22290 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22291 {
22292 struct attribute *type_attr;
22293
22294 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22295 if (!type_attr)
22296 return NULL;
22297
22298 return lookup_die_type (die, type_attr, cu);
22299 }
22300
22301 /* If DIE has a descriptive_type attribute, then set the TYPE's
22302 descriptive type accordingly. */
22303
22304 static void
22305 set_descriptive_type (struct type *type, struct die_info *die,
22306 struct dwarf2_cu *cu)
22307 {
22308 struct type *descriptive_type = die_descriptive_type (die, cu);
22309
22310 if (descriptive_type)
22311 {
22312 ALLOCATE_GNAT_AUX_TYPE (type);
22313 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22314 }
22315 }
22316
22317 /* Return the containing type of the die in question using its
22318 DW_AT_containing_type attribute. */
22319
22320 static struct type *
22321 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22322 {
22323 struct attribute *type_attr;
22324 struct objfile *objfile = cu->per_objfile->objfile;
22325
22326 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22327 if (!type_attr)
22328 error (_("Dwarf Error: Problem turning containing type into gdb type "
22329 "[in module %s]"), objfile_name (objfile));
22330
22331 return lookup_die_type (die, type_attr, cu);
22332 }
22333
22334 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22335
22336 static struct type *
22337 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22338 {
22339 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22340 struct objfile *objfile = per_objfile->objfile;
22341 char *saved;
22342
22343 std::string message
22344 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22345 objfile_name (objfile),
22346 sect_offset_str (cu->header.sect_off),
22347 sect_offset_str (die->sect_off));
22348 saved = obstack_strdup (&objfile->objfile_obstack, message);
22349
22350 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22351 }
22352
22353 /* Look up the type of DIE in CU using its type attribute ATTR.
22354 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22355 DW_AT_containing_type.
22356 If there is no type substitute an error marker. */
22357
22358 static struct type *
22359 lookup_die_type (struct die_info *die, const struct attribute *attr,
22360 struct dwarf2_cu *cu)
22361 {
22362 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22363 struct objfile *objfile = per_objfile->objfile;
22364 struct type *this_type;
22365
22366 gdb_assert (attr->name == DW_AT_type
22367 || attr->name == DW_AT_GNAT_descriptive_type
22368 || attr->name == DW_AT_containing_type);
22369
22370 /* First see if we have it cached. */
22371
22372 if (attr->form == DW_FORM_GNU_ref_alt)
22373 {
22374 struct dwarf2_per_cu_data *per_cu;
22375 sect_offset sect_off = attr->get_ref_die_offset ();
22376
22377 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22378 per_objfile->per_bfd);
22379 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22380 }
22381 else if (attr->form_is_ref ())
22382 {
22383 sect_offset sect_off = attr->get_ref_die_offset ();
22384
22385 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22386 }
22387 else if (attr->form == DW_FORM_ref_sig8)
22388 {
22389 ULONGEST signature = attr->as_signature ();
22390
22391 return get_signatured_type (die, signature, cu);
22392 }
22393 else
22394 {
22395 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22396 " at %s [in module %s]"),
22397 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22398 objfile_name (objfile));
22399 return build_error_marker_type (cu, die);
22400 }
22401
22402 /* If not cached we need to read it in. */
22403
22404 if (this_type == NULL)
22405 {
22406 struct die_info *type_die = NULL;
22407 struct dwarf2_cu *type_cu = cu;
22408
22409 if (attr->form_is_ref ())
22410 type_die = follow_die_ref (die, attr, &type_cu);
22411 if (type_die == NULL)
22412 return build_error_marker_type (cu, die);
22413 /* If we find the type now, it's probably because the type came
22414 from an inter-CU reference and the type's CU got expanded before
22415 ours. */
22416 this_type = read_type_die (type_die, type_cu);
22417 }
22418
22419 /* If we still don't have a type use an error marker. */
22420
22421 if (this_type == NULL)
22422 return build_error_marker_type (cu, die);
22423
22424 return this_type;
22425 }
22426
22427 /* Return the type in DIE, CU.
22428 Returns NULL for invalid types.
22429
22430 This first does a lookup in die_type_hash,
22431 and only reads the die in if necessary.
22432
22433 NOTE: This can be called when reading in partial or full symbols. */
22434
22435 static struct type *
22436 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22437 {
22438 struct type *this_type;
22439
22440 this_type = get_die_type (die, cu);
22441 if (this_type)
22442 return this_type;
22443
22444 return read_type_die_1 (die, cu);
22445 }
22446
22447 /* Read the type in DIE, CU.
22448 Returns NULL for invalid types. */
22449
22450 static struct type *
22451 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22452 {
22453 struct type *this_type = NULL;
22454
22455 switch (die->tag)
22456 {
22457 case DW_TAG_class_type:
22458 case DW_TAG_interface_type:
22459 case DW_TAG_structure_type:
22460 case DW_TAG_union_type:
22461 this_type = read_structure_type (die, cu);
22462 break;
22463 case DW_TAG_enumeration_type:
22464 this_type = read_enumeration_type (die, cu);
22465 break;
22466 case DW_TAG_subprogram:
22467 case DW_TAG_subroutine_type:
22468 case DW_TAG_inlined_subroutine:
22469 this_type = read_subroutine_type (die, cu);
22470 break;
22471 case DW_TAG_array_type:
22472 this_type = read_array_type (die, cu);
22473 break;
22474 case DW_TAG_set_type:
22475 this_type = read_set_type (die, cu);
22476 break;
22477 case DW_TAG_pointer_type:
22478 this_type = read_tag_pointer_type (die, cu);
22479 break;
22480 case DW_TAG_ptr_to_member_type:
22481 this_type = read_tag_ptr_to_member_type (die, cu);
22482 break;
22483 case DW_TAG_reference_type:
22484 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22485 break;
22486 case DW_TAG_rvalue_reference_type:
22487 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22488 break;
22489 case DW_TAG_const_type:
22490 this_type = read_tag_const_type (die, cu);
22491 break;
22492 case DW_TAG_volatile_type:
22493 this_type = read_tag_volatile_type (die, cu);
22494 break;
22495 case DW_TAG_restrict_type:
22496 this_type = read_tag_restrict_type (die, cu);
22497 break;
22498 case DW_TAG_string_type:
22499 this_type = read_tag_string_type (die, cu);
22500 break;
22501 case DW_TAG_typedef:
22502 this_type = read_typedef (die, cu);
22503 break;
22504 case DW_TAG_subrange_type:
22505 this_type = read_subrange_type (die, cu);
22506 break;
22507 case DW_TAG_base_type:
22508 this_type = read_base_type (die, cu);
22509 break;
22510 case DW_TAG_unspecified_type:
22511 this_type = read_unspecified_type (die, cu);
22512 break;
22513 case DW_TAG_namespace:
22514 this_type = read_namespace_type (die, cu);
22515 break;
22516 case DW_TAG_module:
22517 this_type = read_module_type (die, cu);
22518 break;
22519 case DW_TAG_atomic_type:
22520 this_type = read_tag_atomic_type (die, cu);
22521 break;
22522 default:
22523 complaint (_("unexpected tag in read_type_die: '%s'"),
22524 dwarf_tag_name (die->tag));
22525 break;
22526 }
22527
22528 return this_type;
22529 }
22530
22531 /* See if we can figure out if the class lives in a namespace. We do
22532 this by looking for a member function; its demangled name will
22533 contain namespace info, if there is any.
22534 Return the computed name or NULL.
22535 Space for the result is allocated on the objfile's obstack.
22536 This is the full-die version of guess_partial_die_structure_name.
22537 In this case we know DIE has no useful parent. */
22538
22539 static const char *
22540 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22541 {
22542 struct die_info *spec_die;
22543 struct dwarf2_cu *spec_cu;
22544 struct die_info *child;
22545 struct objfile *objfile = cu->per_objfile->objfile;
22546
22547 spec_cu = cu;
22548 spec_die = die_specification (die, &spec_cu);
22549 if (spec_die != NULL)
22550 {
22551 die = spec_die;
22552 cu = spec_cu;
22553 }
22554
22555 for (child = die->child;
22556 child != NULL;
22557 child = child->sibling)
22558 {
22559 if (child->tag == DW_TAG_subprogram)
22560 {
22561 const char *linkage_name = dw2_linkage_name (child, cu);
22562
22563 if (linkage_name != NULL)
22564 {
22565 gdb::unique_xmalloc_ptr<char> actual_name
22566 (cu->language_defn->class_name_from_physname (linkage_name));
22567 const char *name = NULL;
22568
22569 if (actual_name != NULL)
22570 {
22571 const char *die_name = dwarf2_name (die, cu);
22572
22573 if (die_name != NULL
22574 && strcmp (die_name, actual_name.get ()) != 0)
22575 {
22576 /* Strip off the class name from the full name.
22577 We want the prefix. */
22578 int die_name_len = strlen (die_name);
22579 int actual_name_len = strlen (actual_name.get ());
22580 const char *ptr = actual_name.get ();
22581
22582 /* Test for '::' as a sanity check. */
22583 if (actual_name_len > die_name_len + 2
22584 && ptr[actual_name_len - die_name_len - 1] == ':')
22585 name = obstack_strndup (
22586 &objfile->per_bfd->storage_obstack,
22587 ptr, actual_name_len - die_name_len - 2);
22588 }
22589 }
22590 return name;
22591 }
22592 }
22593 }
22594
22595 return NULL;
22596 }
22597
22598 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22599 prefix part in such case. See
22600 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22601
22602 static const char *
22603 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22604 {
22605 struct attribute *attr;
22606 const char *base;
22607
22608 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22609 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22610 return NULL;
22611
22612 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22613 return NULL;
22614
22615 attr = dw2_linkage_name_attr (die, cu);
22616 const char *attr_name = attr->as_string ();
22617 if (attr == NULL || attr_name == NULL)
22618 return NULL;
22619
22620 /* dwarf2_name had to be already called. */
22621 gdb_assert (attr->canonical_string_p ());
22622
22623 /* Strip the base name, keep any leading namespaces/classes. */
22624 base = strrchr (attr_name, ':');
22625 if (base == NULL || base == attr_name || base[-1] != ':')
22626 return "";
22627
22628 struct objfile *objfile = cu->per_objfile->objfile;
22629 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22630 attr_name,
22631 &base[-1] - attr_name);
22632 }
22633
22634 /* Return the name of the namespace/class that DIE is defined within,
22635 or "" if we can't tell. The caller should not xfree the result.
22636
22637 For example, if we're within the method foo() in the following
22638 code:
22639
22640 namespace N {
22641 class C {
22642 void foo () {
22643 }
22644 };
22645 }
22646
22647 then determine_prefix on foo's die will return "N::C". */
22648
22649 static const char *
22650 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22651 {
22652 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22653 struct die_info *parent, *spec_die;
22654 struct dwarf2_cu *spec_cu;
22655 struct type *parent_type;
22656 const char *retval;
22657
22658 if (cu->per_cu->lang != language_cplus
22659 && cu->per_cu->lang != language_fortran
22660 && cu->per_cu->lang != language_d
22661 && cu->per_cu->lang != language_rust)
22662 return "";
22663
22664 retval = anonymous_struct_prefix (die, cu);
22665 if (retval)
22666 return retval;
22667
22668 /* We have to be careful in the presence of DW_AT_specification.
22669 For example, with GCC 3.4, given the code
22670
22671 namespace N {
22672 void foo() {
22673 // Definition of N::foo.
22674 }
22675 }
22676
22677 then we'll have a tree of DIEs like this:
22678
22679 1: DW_TAG_compile_unit
22680 2: DW_TAG_namespace // N
22681 3: DW_TAG_subprogram // declaration of N::foo
22682 4: DW_TAG_subprogram // definition of N::foo
22683 DW_AT_specification // refers to die #3
22684
22685 Thus, when processing die #4, we have to pretend that we're in
22686 the context of its DW_AT_specification, namely the contex of die
22687 #3. */
22688 spec_cu = cu;
22689 spec_die = die_specification (die, &spec_cu);
22690 if (spec_die == NULL)
22691 parent = die->parent;
22692 else
22693 {
22694 parent = spec_die->parent;
22695 cu = spec_cu;
22696 }
22697
22698 if (parent == NULL)
22699 return "";
22700 else if (parent->building_fullname)
22701 {
22702 const char *name;
22703 const char *parent_name;
22704
22705 /* It has been seen on RealView 2.2 built binaries,
22706 DW_TAG_template_type_param types actually _defined_ as
22707 children of the parent class:
22708
22709 enum E {};
22710 template class <class Enum> Class{};
22711 Class<enum E> class_e;
22712
22713 1: DW_TAG_class_type (Class)
22714 2: DW_TAG_enumeration_type (E)
22715 3: DW_TAG_enumerator (enum1:0)
22716 3: DW_TAG_enumerator (enum2:1)
22717 ...
22718 2: DW_TAG_template_type_param
22719 DW_AT_type DW_FORM_ref_udata (E)
22720
22721 Besides being broken debug info, it can put GDB into an
22722 infinite loop. Consider:
22723
22724 When we're building the full name for Class<E>, we'll start
22725 at Class, and go look over its template type parameters,
22726 finding E. We'll then try to build the full name of E, and
22727 reach here. We're now trying to build the full name of E,
22728 and look over the parent DIE for containing scope. In the
22729 broken case, if we followed the parent DIE of E, we'd again
22730 find Class, and once again go look at its template type
22731 arguments, etc., etc. Simply don't consider such parent die
22732 as source-level parent of this die (it can't be, the language
22733 doesn't allow it), and break the loop here. */
22734 name = dwarf2_name (die, cu);
22735 parent_name = dwarf2_name (parent, cu);
22736 complaint (_("template param type '%s' defined within parent '%s'"),
22737 name ? name : "<unknown>",
22738 parent_name ? parent_name : "<unknown>");
22739 return "";
22740 }
22741 else
22742 switch (parent->tag)
22743 {
22744 case DW_TAG_namespace:
22745 parent_type = read_type_die (parent, cu);
22746 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22747 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22748 Work around this problem here. */
22749 if (cu->per_cu->lang == language_cplus
22750 && strcmp (parent_type->name (), "::") == 0)
22751 return "";
22752 /* We give a name to even anonymous namespaces. */
22753 return parent_type->name ();
22754 case DW_TAG_class_type:
22755 case DW_TAG_interface_type:
22756 case DW_TAG_structure_type:
22757 case DW_TAG_union_type:
22758 case DW_TAG_module:
22759 parent_type = read_type_die (parent, cu);
22760 if (parent_type->name () != NULL)
22761 return parent_type->name ();
22762 else
22763 /* An anonymous structure is only allowed non-static data
22764 members; no typedefs, no member functions, et cetera.
22765 So it does not need a prefix. */
22766 return "";
22767 case DW_TAG_compile_unit:
22768 case DW_TAG_partial_unit:
22769 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22770 if (cu->per_cu->lang == language_cplus
22771 && !per_objfile->per_bfd->types.empty ()
22772 && die->child != NULL
22773 && (die->tag == DW_TAG_class_type
22774 || die->tag == DW_TAG_structure_type
22775 || die->tag == DW_TAG_union_type))
22776 {
22777 const char *name = guess_full_die_structure_name (die, cu);
22778 if (name != NULL)
22779 return name;
22780 }
22781 return "";
22782 case DW_TAG_subprogram:
22783 /* Nested subroutines in Fortran get a prefix with the name
22784 of the parent's subroutine. */
22785 if (cu->per_cu->lang == language_fortran)
22786 {
22787 if ((die->tag == DW_TAG_subprogram)
22788 && (dwarf2_name (parent, cu) != NULL))
22789 return dwarf2_name (parent, cu);
22790 }
22791 return "";
22792 case DW_TAG_enumeration_type:
22793 parent_type = read_type_die (parent, cu);
22794 if (parent_type->is_declared_class ())
22795 {
22796 if (parent_type->name () != NULL)
22797 return parent_type->name ();
22798 return "";
22799 }
22800 /* Fall through. */
22801 default:
22802 return determine_prefix (parent, cu);
22803 }
22804 }
22805
22806 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22807 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22808 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22809 an obconcat, otherwise allocate storage for the result. The CU argument is
22810 used to determine the language and hence, the appropriate separator. */
22811
22812 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22813
22814 static char *
22815 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22816 int physname, struct dwarf2_cu *cu)
22817 {
22818 const char *lead = "";
22819 const char *sep;
22820
22821 if (suffix == NULL || suffix[0] == '\0'
22822 || prefix == NULL || prefix[0] == '\0')
22823 sep = "";
22824 else if (cu->per_cu->lang == language_d)
22825 {
22826 /* For D, the 'main' function could be defined in any module, but it
22827 should never be prefixed. */
22828 if (strcmp (suffix, "D main") == 0)
22829 {
22830 prefix = "";
22831 sep = "";
22832 }
22833 else
22834 sep = ".";
22835 }
22836 else if (cu->per_cu->lang == language_fortran && physname)
22837 {
22838 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22839 DW_AT_MIPS_linkage_name is preferred and used instead. */
22840
22841 lead = "__";
22842 sep = "_MOD_";
22843 }
22844 else
22845 sep = "::";
22846
22847 if (prefix == NULL)
22848 prefix = "";
22849 if (suffix == NULL)
22850 suffix = "";
22851
22852 if (obs == NULL)
22853 {
22854 char *retval
22855 = ((char *)
22856 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22857
22858 strcpy (retval, lead);
22859 strcat (retval, prefix);
22860 strcat (retval, sep);
22861 strcat (retval, suffix);
22862 return retval;
22863 }
22864 else
22865 {
22866 /* We have an obstack. */
22867 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22868 }
22869 }
22870
22871 /* Get name of a die, return NULL if not found. */
22872
22873 static const char *
22874 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22875 struct objfile *objfile)
22876 {
22877 if (name && cu->per_cu->lang == language_cplus)
22878 {
22879 gdb::unique_xmalloc_ptr<char> canon_name
22880 = cp_canonicalize_string (name);
22881
22882 if (canon_name != nullptr)
22883 name = objfile->intern (canon_name.get ());
22884 }
22885
22886 return name;
22887 }
22888
22889 /* Get name of a die, return NULL if not found.
22890 Anonymous namespaces are converted to their magic string. */
22891
22892 static const char *
22893 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22894 {
22895 struct attribute *attr;
22896 struct objfile *objfile = cu->per_objfile->objfile;
22897
22898 attr = dwarf2_attr (die, DW_AT_name, cu);
22899 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22900 if (attr_name == nullptr
22901 && die->tag != DW_TAG_namespace
22902 && die->tag != DW_TAG_class_type
22903 && die->tag != DW_TAG_interface_type
22904 && die->tag != DW_TAG_structure_type
22905 && die->tag != DW_TAG_union_type)
22906 return NULL;
22907
22908 switch (die->tag)
22909 {
22910 case DW_TAG_compile_unit:
22911 case DW_TAG_partial_unit:
22912 /* Compilation units have a DW_AT_name that is a filename, not
22913 a source language identifier. */
22914 case DW_TAG_enumeration_type:
22915 case DW_TAG_enumerator:
22916 /* These tags always have simple identifiers already; no need
22917 to canonicalize them. */
22918 return attr_name;
22919
22920 case DW_TAG_namespace:
22921 if (attr_name != nullptr)
22922 return attr_name;
22923 return CP_ANONYMOUS_NAMESPACE_STR;
22924
22925 case DW_TAG_class_type:
22926 case DW_TAG_interface_type:
22927 case DW_TAG_structure_type:
22928 case DW_TAG_union_type:
22929 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22930 structures or unions. These were of the form "._%d" in GCC 4.1,
22931 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22932 and GCC 4.4. We work around this problem by ignoring these. */
22933 if (attr_name != nullptr
22934 && (startswith (attr_name, "._")
22935 || startswith (attr_name, "<anonymous")))
22936 return NULL;
22937
22938 /* GCC might emit a nameless typedef that has a linkage name. See
22939 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22940 if (!attr || attr_name == NULL)
22941 {
22942 attr = dw2_linkage_name_attr (die, cu);
22943 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22944 if (attr == NULL || attr_name == NULL)
22945 return NULL;
22946
22947 /* Avoid demangling attr_name the second time on a second
22948 call for the same DIE. */
22949 if (!attr->canonical_string_p ())
22950 {
22951 gdb::unique_xmalloc_ptr<char> demangled
22952 (gdb_demangle (attr_name, DMGL_TYPES));
22953 if (demangled == nullptr)
22954 return nullptr;
22955
22956 attr->set_string_canonical (objfile->intern (demangled.get ()));
22957 attr_name = attr->as_string ();
22958 }
22959
22960 /* Strip any leading namespaces/classes, keep only the
22961 base name. DW_AT_name for named DIEs does not
22962 contain the prefixes. */
22963 const char *base = strrchr (attr_name, ':');
22964 if (base && base > attr_name && base[-1] == ':')
22965 return &base[1];
22966 else
22967 return attr_name;
22968 }
22969 break;
22970
22971 default:
22972 break;
22973 }
22974
22975 if (!attr->canonical_string_p ())
22976 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22977 objfile));
22978 return attr->as_string ();
22979 }
22980
22981 /* Return the die that this die in an extension of, or NULL if there
22982 is none. *EXT_CU is the CU containing DIE on input, and the CU
22983 containing the return value on output. */
22984
22985 static struct die_info *
22986 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22987 {
22988 struct attribute *attr;
22989
22990 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22991 if (attr == NULL)
22992 return NULL;
22993
22994 return follow_die_ref (die, attr, ext_cu);
22995 }
22996
22997 static void
22998 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22999 {
23000 unsigned int i;
23001
23002 fprintf_unfiltered (f, "%*sDie: %s (abbrev %d, offset %s)\n",
23003 indent, "",
23004 dwarf_tag_name (die->tag), die->abbrev,
23005 sect_offset_str (die->sect_off));
23006
23007 if (die->parent != NULL)
23008 fprintf_unfiltered (f, "%*s parent at offset: %s\n",
23009 indent, "",
23010 sect_offset_str (die->parent->sect_off));
23011
23012 fprintf_unfiltered (f, "%*s has children: %s\n",
23013 indent, "",
23014 dwarf_bool_name (die->child != NULL));
23015
23016 fprintf_unfiltered (f, "%*s attributes:\n", indent, "");
23017
23018 for (i = 0; i < die->num_attrs; ++i)
23019 {
23020 fprintf_unfiltered (f, "%*s %s (%s) ",
23021 indent, "",
23022 dwarf_attr_name (die->attrs[i].name),
23023 dwarf_form_name (die->attrs[i].form));
23024
23025 switch (die->attrs[i].form)
23026 {
23027 case DW_FORM_addr:
23028 case DW_FORM_addrx:
23029 case DW_FORM_GNU_addr_index:
23030 fprintf_unfiltered (f, "address: ");
23031 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23032 break;
23033 case DW_FORM_block2:
23034 case DW_FORM_block4:
23035 case DW_FORM_block:
23036 case DW_FORM_block1:
23037 fprintf_unfiltered (f, "block: size %s",
23038 pulongest (die->attrs[i].as_block ()->size));
23039 break;
23040 case DW_FORM_exprloc:
23041 fprintf_unfiltered (f, "expression: size %s",
23042 pulongest (die->attrs[i].as_block ()->size));
23043 break;
23044 case DW_FORM_data16:
23045 fprintf_unfiltered (f, "constant of 16 bytes");
23046 break;
23047 case DW_FORM_ref_addr:
23048 fprintf_unfiltered (f, "ref address: ");
23049 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23050 break;
23051 case DW_FORM_GNU_ref_alt:
23052 fprintf_unfiltered (f, "alt ref address: ");
23053 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23054 break;
23055 case DW_FORM_ref1:
23056 case DW_FORM_ref2:
23057 case DW_FORM_ref4:
23058 case DW_FORM_ref8:
23059 case DW_FORM_ref_udata:
23060 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23061 (long) (die->attrs[i].as_unsigned ()));
23062 break;
23063 case DW_FORM_data1:
23064 case DW_FORM_data2:
23065 case DW_FORM_data4:
23066 case DW_FORM_data8:
23067 case DW_FORM_udata:
23068 fprintf_unfiltered (f, "constant: %s",
23069 pulongest (die->attrs[i].as_unsigned ()));
23070 break;
23071 case DW_FORM_sec_offset:
23072 fprintf_unfiltered (f, "section offset: %s",
23073 pulongest (die->attrs[i].as_unsigned ()));
23074 break;
23075 case DW_FORM_ref_sig8:
23076 fprintf_unfiltered (f, "signature: %s",
23077 hex_string (die->attrs[i].as_signature ()));
23078 break;
23079 case DW_FORM_string:
23080 case DW_FORM_strp:
23081 case DW_FORM_line_strp:
23082 case DW_FORM_strx:
23083 case DW_FORM_GNU_str_index:
23084 case DW_FORM_GNU_strp_alt:
23085 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23086 die->attrs[i].as_string ()
23087 ? die->attrs[i].as_string () : "",
23088 die->attrs[i].canonical_string_p () ? "is" : "not");
23089 break;
23090 case DW_FORM_flag:
23091 if (die->attrs[i].as_boolean ())
23092 fprintf_unfiltered (f, "flag: TRUE");
23093 else
23094 fprintf_unfiltered (f, "flag: FALSE");
23095 break;
23096 case DW_FORM_flag_present:
23097 fprintf_unfiltered (f, "flag: TRUE");
23098 break;
23099 case DW_FORM_indirect:
23100 /* The reader will have reduced the indirect form to
23101 the "base form" so this form should not occur. */
23102 fprintf_unfiltered (f,
23103 "unexpected attribute form: DW_FORM_indirect");
23104 break;
23105 case DW_FORM_sdata:
23106 case DW_FORM_implicit_const:
23107 fprintf_unfiltered (f, "constant: %s",
23108 plongest (die->attrs[i].as_signed ()));
23109 break;
23110 default:
23111 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23112 die->attrs[i].form);
23113 break;
23114 }
23115 fprintf_unfiltered (f, "\n");
23116 }
23117 }
23118
23119 static void
23120 dump_die_for_error (struct die_info *die)
23121 {
23122 dump_die_shallow (gdb_stderr, 0, die);
23123 }
23124
23125 static void
23126 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23127 {
23128 int indent = level * 4;
23129
23130 gdb_assert (die != NULL);
23131
23132 if (level >= max_level)
23133 return;
23134
23135 dump_die_shallow (f, indent, die);
23136
23137 if (die->child != NULL)
23138 {
23139 fprintf_unfiltered (f, "%*s Children:", indent, "");
23140 if (level + 1 < max_level)
23141 {
23142 fprintf_unfiltered (f, "\n");
23143 dump_die_1 (f, level + 1, max_level, die->child);
23144 }
23145 else
23146 {
23147 fprintf_unfiltered (f,
23148 " [not printed, max nesting level reached]\n");
23149 }
23150 }
23151
23152 if (die->sibling != NULL && level > 0)
23153 {
23154 dump_die_1 (f, level, max_level, die->sibling);
23155 }
23156 }
23157
23158 /* This is called from the pdie macro in gdbinit.in.
23159 It's not static so gcc will keep a copy callable from gdb. */
23160
23161 void
23162 dump_die (struct die_info *die, int max_level)
23163 {
23164 dump_die_1 (gdb_stdlog, 0, max_level, die);
23165 }
23166
23167 static void
23168 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23169 {
23170 void **slot;
23171
23172 slot = htab_find_slot_with_hash (cu->die_hash, die,
23173 to_underlying (die->sect_off),
23174 INSERT);
23175
23176 *slot = die;
23177 }
23178
23179 /* Follow reference or signature attribute ATTR of SRC_DIE.
23180 On entry *REF_CU is the CU of SRC_DIE.
23181 On exit *REF_CU is the CU of the result. */
23182
23183 static struct die_info *
23184 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23185 struct dwarf2_cu **ref_cu)
23186 {
23187 struct die_info *die;
23188
23189 if (attr->form_is_ref ())
23190 die = follow_die_ref (src_die, attr, ref_cu);
23191 else if (attr->form == DW_FORM_ref_sig8)
23192 die = follow_die_sig (src_die, attr, ref_cu);
23193 else
23194 {
23195 dump_die_for_error (src_die);
23196 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23197 objfile_name ((*ref_cu)->per_objfile->objfile));
23198 }
23199
23200 return die;
23201 }
23202
23203 /* Follow reference OFFSET.
23204 On entry *REF_CU is the CU of the source die referencing OFFSET.
23205 On exit *REF_CU is the CU of the result.
23206 Returns NULL if OFFSET is invalid. */
23207
23208 static struct die_info *
23209 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23210 struct dwarf2_cu **ref_cu)
23211 {
23212 struct die_info temp_die;
23213 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23214 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23215
23216 gdb_assert (cu->per_cu != NULL);
23217
23218 target_cu = cu;
23219
23220 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23221 "source CU contains target offset: %d",
23222 sect_offset_str (cu->per_cu->sect_off),
23223 sect_offset_str (sect_off),
23224 cu->header.offset_in_cu_p (sect_off));
23225
23226 if (cu->per_cu->is_debug_types)
23227 {
23228 /* .debug_types CUs cannot reference anything outside their CU.
23229 If they need to, they have to reference a signatured type via
23230 DW_FORM_ref_sig8. */
23231 if (!cu->header.offset_in_cu_p (sect_off))
23232 return NULL;
23233 }
23234 else if (offset_in_dwz != cu->per_cu->is_dwz
23235 || !cu->header.offset_in_cu_p (sect_off))
23236 {
23237 struct dwarf2_per_cu_data *per_cu;
23238
23239 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23240 per_objfile->per_bfd);
23241
23242 dwarf_read_debug_printf_v ("target CU offset: %s, "
23243 "target CU DIEs loaded: %d",
23244 sect_offset_str (per_cu->sect_off),
23245 per_objfile->get_cu (per_cu) != nullptr);
23246
23247 /* If necessary, add it to the queue and load its DIEs.
23248
23249 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23250 it doesn't mean they are currently loaded. Since we require them
23251 to be loaded, we must check for ourselves. */
23252 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
23253 || per_objfile->get_cu (per_cu) == nullptr)
23254 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23255 false, cu->per_cu->lang);
23256
23257 target_cu = per_objfile->get_cu (per_cu);
23258 gdb_assert (target_cu != nullptr);
23259 }
23260 else if (cu->dies == NULL)
23261 {
23262 /* We're loading full DIEs during partial symbol reading. */
23263 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23264 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23265 language_minimal);
23266 }
23267
23268 *ref_cu = target_cu;
23269 temp_die.sect_off = sect_off;
23270
23271 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23272 &temp_die,
23273 to_underlying (sect_off));
23274 }
23275
23276 /* Follow reference attribute ATTR of SRC_DIE.
23277 On entry *REF_CU is the CU of SRC_DIE.
23278 On exit *REF_CU is the CU of the result. */
23279
23280 static struct die_info *
23281 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23282 struct dwarf2_cu **ref_cu)
23283 {
23284 sect_offset sect_off = attr->get_ref_die_offset ();
23285 struct dwarf2_cu *cu = *ref_cu;
23286 struct die_info *die;
23287
23288 die = follow_die_offset (sect_off,
23289 (attr->form == DW_FORM_GNU_ref_alt
23290 || cu->per_cu->is_dwz),
23291 ref_cu);
23292 if (!die)
23293 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23294 "at %s [in module %s]"),
23295 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23296 objfile_name (cu->per_objfile->objfile));
23297
23298 return die;
23299 }
23300
23301 /* See read.h. */
23302
23303 struct dwarf2_locexpr_baton
23304 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23305 dwarf2_per_cu_data *per_cu,
23306 dwarf2_per_objfile *per_objfile,
23307 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23308 bool resolve_abstract_p)
23309 {
23310 struct die_info *die;
23311 struct attribute *attr;
23312 struct dwarf2_locexpr_baton retval;
23313 struct objfile *objfile = per_objfile->objfile;
23314
23315 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23316 if (cu == nullptr)
23317 cu = load_cu (per_cu, per_objfile, false);
23318
23319 if (cu == nullptr)
23320 {
23321 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23322 Instead just throw an error, not much else we can do. */
23323 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23324 sect_offset_str (sect_off), objfile_name (objfile));
23325 }
23326
23327 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23328 if (!die)
23329 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23330 sect_offset_str (sect_off), objfile_name (objfile));
23331
23332 attr = dwarf2_attr (die, DW_AT_location, cu);
23333 if (!attr && resolve_abstract_p
23334 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23335 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23336 {
23337 CORE_ADDR pc = get_frame_pc ();
23338 CORE_ADDR baseaddr = objfile->text_section_offset ();
23339 struct gdbarch *gdbarch = objfile->arch ();
23340
23341 for (const auto &cand_off
23342 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23343 {
23344 struct dwarf2_cu *cand_cu = cu;
23345 struct die_info *cand
23346 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23347 if (!cand
23348 || !cand->parent
23349 || cand->parent->tag != DW_TAG_subprogram)
23350 continue;
23351
23352 CORE_ADDR pc_low, pc_high;
23353 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23354 if (pc_low == ((CORE_ADDR) -1))
23355 continue;
23356 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23357 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23358 if (!(pc_low <= pc && pc < pc_high))
23359 continue;
23360
23361 die = cand;
23362 attr = dwarf2_attr (die, DW_AT_location, cu);
23363 break;
23364 }
23365 }
23366
23367 if (!attr)
23368 {
23369 /* DWARF: "If there is no such attribute, then there is no effect.".
23370 DATA is ignored if SIZE is 0. */
23371
23372 retval.data = NULL;
23373 retval.size = 0;
23374 }
23375 else if (attr->form_is_section_offset ())
23376 {
23377 struct dwarf2_loclist_baton loclist_baton;
23378 CORE_ADDR pc = get_frame_pc ();
23379 size_t size;
23380
23381 fill_in_loclist_baton (cu, &loclist_baton, attr);
23382
23383 retval.data = dwarf2_find_location_expression (&loclist_baton,
23384 &size, pc);
23385 retval.size = size;
23386 }
23387 else
23388 {
23389 if (!attr->form_is_block ())
23390 error (_("Dwarf Error: DIE at %s referenced in module %s "
23391 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23392 sect_offset_str (sect_off), objfile_name (objfile));
23393
23394 struct dwarf_block *block = attr->as_block ();
23395 retval.data = block->data;
23396 retval.size = block->size;
23397 }
23398 retval.per_objfile = per_objfile;
23399 retval.per_cu = cu->per_cu;
23400
23401 per_objfile->age_comp_units ();
23402
23403 return retval;
23404 }
23405
23406 /* See read.h. */
23407
23408 struct dwarf2_locexpr_baton
23409 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23410 dwarf2_per_cu_data *per_cu,
23411 dwarf2_per_objfile *per_objfile,
23412 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23413 {
23414 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23415
23416 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23417 get_frame_pc);
23418 }
23419
23420 /* Write a constant of a given type as target-ordered bytes into
23421 OBSTACK. */
23422
23423 static const gdb_byte *
23424 write_constant_as_bytes (struct obstack *obstack,
23425 enum bfd_endian byte_order,
23426 struct type *type,
23427 ULONGEST value,
23428 LONGEST *len)
23429 {
23430 gdb_byte *result;
23431
23432 *len = TYPE_LENGTH (type);
23433 result = (gdb_byte *) obstack_alloc (obstack, *len);
23434 store_unsigned_integer (result, *len, byte_order, value);
23435
23436 return result;
23437 }
23438
23439 /* See read.h. */
23440
23441 const gdb_byte *
23442 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23443 dwarf2_per_cu_data *per_cu,
23444 dwarf2_per_objfile *per_objfile,
23445 obstack *obstack,
23446 LONGEST *len)
23447 {
23448 struct die_info *die;
23449 struct attribute *attr;
23450 const gdb_byte *result = NULL;
23451 struct type *type;
23452 LONGEST value;
23453 enum bfd_endian byte_order;
23454 struct objfile *objfile = per_objfile->objfile;
23455
23456 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23457 if (cu == nullptr)
23458 cu = load_cu (per_cu, per_objfile, false);
23459
23460 if (cu == nullptr)
23461 {
23462 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23463 Instead just throw an error, not much else we can do. */
23464 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23465 sect_offset_str (sect_off), objfile_name (objfile));
23466 }
23467
23468 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23469 if (!die)
23470 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23471 sect_offset_str (sect_off), objfile_name (objfile));
23472
23473 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23474 if (attr == NULL)
23475 return NULL;
23476
23477 byte_order = (bfd_big_endian (objfile->obfd)
23478 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23479
23480 switch (attr->form)
23481 {
23482 case DW_FORM_addr:
23483 case DW_FORM_addrx:
23484 case DW_FORM_GNU_addr_index:
23485 {
23486 gdb_byte *tem;
23487
23488 *len = cu->header.addr_size;
23489 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23490 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23491 result = tem;
23492 }
23493 break;
23494 case DW_FORM_string:
23495 case DW_FORM_strp:
23496 case DW_FORM_strx:
23497 case DW_FORM_GNU_str_index:
23498 case DW_FORM_GNU_strp_alt:
23499 /* The string is already allocated on the objfile obstack, point
23500 directly to it. */
23501 {
23502 const char *attr_name = attr->as_string ();
23503 result = (const gdb_byte *) attr_name;
23504 *len = strlen (attr_name);
23505 }
23506 break;
23507 case DW_FORM_block1:
23508 case DW_FORM_block2:
23509 case DW_FORM_block4:
23510 case DW_FORM_block:
23511 case DW_FORM_exprloc:
23512 case DW_FORM_data16:
23513 {
23514 struct dwarf_block *block = attr->as_block ();
23515 result = block->data;
23516 *len = block->size;
23517 }
23518 break;
23519
23520 /* The DW_AT_const_value attributes are supposed to carry the
23521 symbol's value "represented as it would be on the target
23522 architecture." By the time we get here, it's already been
23523 converted to host endianness, so we just need to sign- or
23524 zero-extend it as appropriate. */
23525 case DW_FORM_data1:
23526 type = die_type (die, cu);
23527 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23528 if (result == NULL)
23529 result = write_constant_as_bytes (obstack, byte_order,
23530 type, value, len);
23531 break;
23532 case DW_FORM_data2:
23533 type = die_type (die, cu);
23534 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23535 if (result == NULL)
23536 result = write_constant_as_bytes (obstack, byte_order,
23537 type, value, len);
23538 break;
23539 case DW_FORM_data4:
23540 type = die_type (die, cu);
23541 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23542 if (result == NULL)
23543 result = write_constant_as_bytes (obstack, byte_order,
23544 type, value, len);
23545 break;
23546 case DW_FORM_data8:
23547 type = die_type (die, cu);
23548 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23549 if (result == NULL)
23550 result = write_constant_as_bytes (obstack, byte_order,
23551 type, value, len);
23552 break;
23553
23554 case DW_FORM_sdata:
23555 case DW_FORM_implicit_const:
23556 type = die_type (die, cu);
23557 result = write_constant_as_bytes (obstack, byte_order,
23558 type, attr->as_signed (), len);
23559 break;
23560
23561 case DW_FORM_udata:
23562 type = die_type (die, cu);
23563 result = write_constant_as_bytes (obstack, byte_order,
23564 type, attr->as_unsigned (), len);
23565 break;
23566
23567 default:
23568 complaint (_("unsupported const value attribute form: '%s'"),
23569 dwarf_form_name (attr->form));
23570 break;
23571 }
23572
23573 return result;
23574 }
23575
23576 /* See read.h. */
23577
23578 struct type *
23579 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23580 dwarf2_per_cu_data *per_cu,
23581 dwarf2_per_objfile *per_objfile,
23582 const char **var_name)
23583 {
23584 struct die_info *die;
23585
23586 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23587 if (cu == nullptr)
23588 cu = load_cu (per_cu, per_objfile, false);
23589
23590 if (cu == nullptr)
23591 return nullptr;
23592
23593 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23594 if (!die)
23595 return NULL;
23596
23597 if (var_name != nullptr)
23598 *var_name = var_decl_name (die, cu);
23599 return die_type (die, cu);
23600 }
23601
23602 /* See read.h. */
23603
23604 struct type *
23605 dwarf2_get_die_type (cu_offset die_offset,
23606 dwarf2_per_cu_data *per_cu,
23607 dwarf2_per_objfile *per_objfile)
23608 {
23609 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23610 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23611 }
23612
23613 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23614 On entry *REF_CU is the CU of SRC_DIE.
23615 On exit *REF_CU is the CU of the result.
23616 Returns NULL if the referenced DIE isn't found. */
23617
23618 static struct die_info *
23619 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23620 struct dwarf2_cu **ref_cu)
23621 {
23622 struct die_info temp_die;
23623 struct dwarf2_cu *sig_cu;
23624 struct die_info *die;
23625 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23626
23627
23628 /* While it might be nice to assert sig_type->type == NULL here,
23629 we can get here for DW_AT_imported_declaration where we need
23630 the DIE not the type. */
23631
23632 /* If necessary, add it to the queue and load its DIEs.
23633
23634 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23635 it doesn't mean they are currently loaded. Since we require them
23636 to be loaded, we must check for ourselves. */
23637 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23638 language_minimal)
23639 || per_objfile->get_cu (sig_type) == nullptr)
23640 read_signatured_type (sig_type, per_objfile);
23641
23642 sig_cu = per_objfile->get_cu (sig_type);
23643 gdb_assert (sig_cu != NULL);
23644 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23645 temp_die.sect_off = sig_type->type_offset_in_section;
23646 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23647 to_underlying (temp_die.sect_off));
23648 if (die)
23649 {
23650 /* For .gdb_index version 7 keep track of included TUs.
23651 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23652 if (per_objfile->per_bfd->index_table != NULL
23653 && per_objfile->per_bfd->index_table->version <= 7)
23654 {
23655 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23656 }
23657
23658 *ref_cu = sig_cu;
23659 return die;
23660 }
23661
23662 return NULL;
23663 }
23664
23665 /* Follow signatured type referenced by ATTR in SRC_DIE.
23666 On entry *REF_CU is the CU of SRC_DIE.
23667 On exit *REF_CU is the CU of the result.
23668 The result is the DIE of the type.
23669 If the referenced type cannot be found an error is thrown. */
23670
23671 static struct die_info *
23672 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23673 struct dwarf2_cu **ref_cu)
23674 {
23675 ULONGEST signature = attr->as_signature ();
23676 struct signatured_type *sig_type;
23677 struct die_info *die;
23678
23679 gdb_assert (attr->form == DW_FORM_ref_sig8);
23680
23681 sig_type = lookup_signatured_type (*ref_cu, signature);
23682 /* sig_type will be NULL if the signatured type is missing from
23683 the debug info. */
23684 if (sig_type == NULL)
23685 {
23686 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23687 " from DIE at %s [in module %s]"),
23688 hex_string (signature), sect_offset_str (src_die->sect_off),
23689 objfile_name ((*ref_cu)->per_objfile->objfile));
23690 }
23691
23692 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23693 if (die == NULL)
23694 {
23695 dump_die_for_error (src_die);
23696 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23697 " from DIE at %s [in module %s]"),
23698 hex_string (signature), sect_offset_str (src_die->sect_off),
23699 objfile_name ((*ref_cu)->per_objfile->objfile));
23700 }
23701
23702 return die;
23703 }
23704
23705 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23706 reading in and processing the type unit if necessary. */
23707
23708 static struct type *
23709 get_signatured_type (struct die_info *die, ULONGEST signature,
23710 struct dwarf2_cu *cu)
23711 {
23712 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23713 struct signatured_type *sig_type;
23714 struct dwarf2_cu *type_cu;
23715 struct die_info *type_die;
23716 struct type *type;
23717
23718 sig_type = lookup_signatured_type (cu, signature);
23719 /* sig_type will be NULL if the signatured type is missing from
23720 the debug info. */
23721 if (sig_type == NULL)
23722 {
23723 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23724 " from DIE at %s [in module %s]"),
23725 hex_string (signature), sect_offset_str (die->sect_off),
23726 objfile_name (per_objfile->objfile));
23727 return build_error_marker_type (cu, die);
23728 }
23729
23730 /* If we already know the type we're done. */
23731 type = per_objfile->get_type_for_signatured_type (sig_type);
23732 if (type != nullptr)
23733 return type;
23734
23735 type_cu = cu;
23736 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23737 if (type_die != NULL)
23738 {
23739 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23740 is created. This is important, for example, because for c++ classes
23741 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23742 type = read_type_die (type_die, type_cu);
23743 if (type == NULL)
23744 {
23745 complaint (_("Dwarf Error: Cannot build signatured type %s"
23746 " referenced from DIE at %s [in module %s]"),
23747 hex_string (signature), sect_offset_str (die->sect_off),
23748 objfile_name (per_objfile->objfile));
23749 type = build_error_marker_type (cu, die);
23750 }
23751 }
23752 else
23753 {
23754 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23755 " from DIE at %s [in module %s]"),
23756 hex_string (signature), sect_offset_str (die->sect_off),
23757 objfile_name (per_objfile->objfile));
23758 type = build_error_marker_type (cu, die);
23759 }
23760
23761 per_objfile->set_type_for_signatured_type (sig_type, type);
23762
23763 return type;
23764 }
23765
23766 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23767 reading in and processing the type unit if necessary. */
23768
23769 static struct type *
23770 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23771 struct dwarf2_cu *cu) /* ARI: editCase function */
23772 {
23773 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23774 if (attr->form_is_ref ())
23775 {
23776 struct dwarf2_cu *type_cu = cu;
23777 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23778
23779 return read_type_die (type_die, type_cu);
23780 }
23781 else if (attr->form == DW_FORM_ref_sig8)
23782 {
23783 return get_signatured_type (die, attr->as_signature (), cu);
23784 }
23785 else
23786 {
23787 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23788
23789 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23790 " at %s [in module %s]"),
23791 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23792 objfile_name (per_objfile->objfile));
23793 return build_error_marker_type (cu, die);
23794 }
23795 }
23796
23797 /* Load the DIEs associated with type unit PER_CU into memory. */
23798
23799 static void
23800 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23801 dwarf2_per_objfile *per_objfile)
23802 {
23803 struct signatured_type *sig_type;
23804
23805 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23806 gdb_assert (! per_cu->type_unit_group_p ());
23807
23808 /* We have the per_cu, but we need the signatured_type.
23809 Fortunately this is an easy translation. */
23810 gdb_assert (per_cu->is_debug_types);
23811 sig_type = (struct signatured_type *) per_cu;
23812
23813 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23814
23815 read_signatured_type (sig_type, per_objfile);
23816
23817 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23818 }
23819
23820 /* Read in a signatured type and build its CU and DIEs.
23821 If the type is a stub for the real type in a DWO file,
23822 read in the real type from the DWO file as well. */
23823
23824 static void
23825 read_signatured_type (signatured_type *sig_type,
23826 dwarf2_per_objfile *per_objfile)
23827 {
23828 gdb_assert (sig_type->is_debug_types);
23829 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23830
23831 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23832
23833 if (!reader.dummy_p)
23834 {
23835 struct dwarf2_cu *cu = reader.cu;
23836 const gdb_byte *info_ptr = reader.info_ptr;
23837
23838 gdb_assert (cu->die_hash == NULL);
23839 cu->die_hash =
23840 htab_create_alloc_ex (cu->header.length / 12,
23841 die_hash,
23842 die_eq,
23843 NULL,
23844 &cu->comp_unit_obstack,
23845 hashtab_obstack_allocate,
23846 dummy_obstack_deallocate);
23847
23848 if (reader.comp_unit_die->has_children)
23849 reader.comp_unit_die->child
23850 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23851 reader.comp_unit_die);
23852 cu->dies = reader.comp_unit_die;
23853 /* comp_unit_die is not stored in die_hash, no need. */
23854
23855 /* We try not to read any attributes in this function, because
23856 not all CUs needed for references have been loaded yet, and
23857 symbol table processing isn't initialized. But we have to
23858 set the CU language, or we won't be able to build types
23859 correctly. Similarly, if we do not read the producer, we can
23860 not apply producer-specific interpretation. */
23861 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23862
23863 reader.keep ();
23864 }
23865
23866 sig_type->tu_read = 1;
23867 }
23868
23869 /* Decode simple location descriptions.
23870 Given a pointer to a dwarf block that defines a location, compute
23871 the location and return the value. If COMPUTED is non-null, it is
23872 set to true to indicate that decoding was successful, and false
23873 otherwise. If COMPUTED is null, then this function may emit a
23874 complaint. */
23875
23876 static CORE_ADDR
23877 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23878 {
23879 struct objfile *objfile = cu->per_objfile->objfile;
23880 size_t i;
23881 size_t size = blk->size;
23882 const gdb_byte *data = blk->data;
23883 CORE_ADDR stack[64];
23884 int stacki;
23885 unsigned int bytes_read, unsnd;
23886 gdb_byte op;
23887
23888 if (computed != nullptr)
23889 *computed = false;
23890
23891 i = 0;
23892 stacki = 0;
23893 stack[stacki] = 0;
23894 stack[++stacki] = 0;
23895
23896 while (i < size)
23897 {
23898 op = data[i++];
23899 switch (op)
23900 {
23901 case DW_OP_lit0:
23902 case DW_OP_lit1:
23903 case DW_OP_lit2:
23904 case DW_OP_lit3:
23905 case DW_OP_lit4:
23906 case DW_OP_lit5:
23907 case DW_OP_lit6:
23908 case DW_OP_lit7:
23909 case DW_OP_lit8:
23910 case DW_OP_lit9:
23911 case DW_OP_lit10:
23912 case DW_OP_lit11:
23913 case DW_OP_lit12:
23914 case DW_OP_lit13:
23915 case DW_OP_lit14:
23916 case DW_OP_lit15:
23917 case DW_OP_lit16:
23918 case DW_OP_lit17:
23919 case DW_OP_lit18:
23920 case DW_OP_lit19:
23921 case DW_OP_lit20:
23922 case DW_OP_lit21:
23923 case DW_OP_lit22:
23924 case DW_OP_lit23:
23925 case DW_OP_lit24:
23926 case DW_OP_lit25:
23927 case DW_OP_lit26:
23928 case DW_OP_lit27:
23929 case DW_OP_lit28:
23930 case DW_OP_lit29:
23931 case DW_OP_lit30:
23932 case DW_OP_lit31:
23933 stack[++stacki] = op - DW_OP_lit0;
23934 break;
23935
23936 case DW_OP_reg0:
23937 case DW_OP_reg1:
23938 case DW_OP_reg2:
23939 case DW_OP_reg3:
23940 case DW_OP_reg4:
23941 case DW_OP_reg5:
23942 case DW_OP_reg6:
23943 case DW_OP_reg7:
23944 case DW_OP_reg8:
23945 case DW_OP_reg9:
23946 case DW_OP_reg10:
23947 case DW_OP_reg11:
23948 case DW_OP_reg12:
23949 case DW_OP_reg13:
23950 case DW_OP_reg14:
23951 case DW_OP_reg15:
23952 case DW_OP_reg16:
23953 case DW_OP_reg17:
23954 case DW_OP_reg18:
23955 case DW_OP_reg19:
23956 case DW_OP_reg20:
23957 case DW_OP_reg21:
23958 case DW_OP_reg22:
23959 case DW_OP_reg23:
23960 case DW_OP_reg24:
23961 case DW_OP_reg25:
23962 case DW_OP_reg26:
23963 case DW_OP_reg27:
23964 case DW_OP_reg28:
23965 case DW_OP_reg29:
23966 case DW_OP_reg30:
23967 case DW_OP_reg31:
23968 stack[++stacki] = op - DW_OP_reg0;
23969 if (i < size)
23970 {
23971 if (computed == nullptr)
23972 dwarf2_complex_location_expr_complaint ();
23973 else
23974 return 0;
23975 }
23976 break;
23977
23978 case DW_OP_regx:
23979 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23980 i += bytes_read;
23981 stack[++stacki] = unsnd;
23982 if (i < size)
23983 {
23984 if (computed == nullptr)
23985 dwarf2_complex_location_expr_complaint ();
23986 else
23987 return 0;
23988 }
23989 break;
23990
23991 case DW_OP_addr:
23992 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23993 &bytes_read);
23994 i += bytes_read;
23995 break;
23996
23997 case DW_OP_const1u:
23998 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23999 i += 1;
24000 break;
24001
24002 case DW_OP_const1s:
24003 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24004 i += 1;
24005 break;
24006
24007 case DW_OP_const2u:
24008 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24009 i += 2;
24010 break;
24011
24012 case DW_OP_const2s:
24013 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24014 i += 2;
24015 break;
24016
24017 case DW_OP_const4u:
24018 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24019 i += 4;
24020 break;
24021
24022 case DW_OP_const4s:
24023 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24024 i += 4;
24025 break;
24026
24027 case DW_OP_const8u:
24028 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24029 i += 8;
24030 break;
24031
24032 case DW_OP_constu:
24033 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24034 &bytes_read);
24035 i += bytes_read;
24036 break;
24037
24038 case DW_OP_consts:
24039 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24040 i += bytes_read;
24041 break;
24042
24043 case DW_OP_dup:
24044 stack[stacki + 1] = stack[stacki];
24045 stacki++;
24046 break;
24047
24048 case DW_OP_plus:
24049 stack[stacki - 1] += stack[stacki];
24050 stacki--;
24051 break;
24052
24053 case DW_OP_plus_uconst:
24054 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24055 &bytes_read);
24056 i += bytes_read;
24057 break;
24058
24059 case DW_OP_minus:
24060 stack[stacki - 1] -= stack[stacki];
24061 stacki--;
24062 break;
24063
24064 case DW_OP_deref:
24065 /* If we're not the last op, then we definitely can't encode
24066 this using GDB's address_class enum. This is valid for partial
24067 global symbols, although the variable's address will be bogus
24068 in the psymtab. */
24069 if (i < size)
24070 {
24071 if (computed == nullptr)
24072 dwarf2_complex_location_expr_complaint ();
24073 else
24074 return 0;
24075 }
24076 break;
24077
24078 case DW_OP_GNU_push_tls_address:
24079 case DW_OP_form_tls_address:
24080 /* The top of the stack has the offset from the beginning
24081 of the thread control block at which the variable is located. */
24082 /* Nothing should follow this operator, so the top of stack would
24083 be returned. */
24084 /* This is valid for partial global symbols, but the variable's
24085 address will be bogus in the psymtab. Make it always at least
24086 non-zero to not look as a variable garbage collected by linker
24087 which have DW_OP_addr 0. */
24088 if (i < size)
24089 {
24090 if (computed == nullptr)
24091 dwarf2_complex_location_expr_complaint ();
24092 else
24093 return 0;
24094 }
24095 stack[stacki]++;
24096 break;
24097
24098 case DW_OP_GNU_uninit:
24099 if (computed != nullptr)
24100 return 0;
24101 break;
24102
24103 case DW_OP_addrx:
24104 case DW_OP_GNU_addr_index:
24105 case DW_OP_GNU_const_index:
24106 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24107 &bytes_read);
24108 i += bytes_read;
24109 break;
24110
24111 default:
24112 if (computed == nullptr)
24113 {
24114 const char *name = get_DW_OP_name (op);
24115
24116 if (name)
24117 complaint (_("unsupported stack op: '%s'"),
24118 name);
24119 else
24120 complaint (_("unsupported stack op: '%02x'"),
24121 op);
24122 }
24123
24124 return (stack[stacki]);
24125 }
24126
24127 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24128 outside of the allocated space. Also enforce minimum>0. */
24129 if (stacki >= ARRAY_SIZE (stack) - 1)
24130 {
24131 if (computed == nullptr)
24132 complaint (_("location description stack overflow"));
24133 return 0;
24134 }
24135
24136 if (stacki <= 0)
24137 {
24138 if (computed == nullptr)
24139 complaint (_("location description stack underflow"));
24140 return 0;
24141 }
24142 }
24143
24144 if (computed != nullptr)
24145 *computed = true;
24146 return (stack[stacki]);
24147 }
24148
24149 /* memory allocation interface */
24150
24151 static struct dwarf_block *
24152 dwarf_alloc_block (struct dwarf2_cu *cu)
24153 {
24154 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24155 }
24156
24157 static struct die_info *
24158 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24159 {
24160 struct die_info *die;
24161 size_t size = sizeof (struct die_info);
24162
24163 if (num_attrs > 1)
24164 size += (num_attrs - 1) * sizeof (struct attribute);
24165
24166 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24167 memset (die, 0, sizeof (struct die_info));
24168 return (die);
24169 }
24170
24171 \f
24172
24173 /* Macro support. */
24174
24175 /* An overload of dwarf_decode_macros that finds the correct section
24176 and ensures it is read in before calling the other overload. */
24177
24178 static void
24179 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24180 int section_is_gnu)
24181 {
24182 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24183 struct objfile *objfile = per_objfile->objfile;
24184 const struct line_header *lh = cu->line_header;
24185 unsigned int offset_size = cu->header.offset_size;
24186 struct dwarf2_section_info *section;
24187 const char *section_name;
24188
24189 if (cu->dwo_unit != nullptr)
24190 {
24191 if (section_is_gnu)
24192 {
24193 section = &cu->dwo_unit->dwo_file->sections.macro;
24194 section_name = ".debug_macro.dwo";
24195 }
24196 else
24197 {
24198 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24199 section_name = ".debug_macinfo.dwo";
24200 }
24201 }
24202 else
24203 {
24204 if (section_is_gnu)
24205 {
24206 section = &per_objfile->per_bfd->macro;
24207 section_name = ".debug_macro";
24208 }
24209 else
24210 {
24211 section = &per_objfile->per_bfd->macinfo;
24212 section_name = ".debug_macinfo";
24213 }
24214 }
24215
24216 section->read (objfile);
24217 if (section->buffer == nullptr)
24218 {
24219 complaint (_("missing %s section"), section_name);
24220 return;
24221 }
24222
24223 buildsym_compunit *builder = cu->get_builder ();
24224
24225 struct dwarf2_section_info *str_offsets_section;
24226 struct dwarf2_section_info *str_section;
24227 gdb::optional<ULONGEST> str_offsets_base;
24228
24229 if (cu->dwo_unit != nullptr)
24230 {
24231 str_offsets_section = &cu->dwo_unit->dwo_file
24232 ->sections.str_offsets;
24233 str_section = &cu->dwo_unit->dwo_file->sections.str;
24234 str_offsets_base = cu->header.addr_size;
24235 }
24236 else
24237 {
24238 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24239 str_section = &per_objfile->per_bfd->str;
24240 str_offsets_base = cu->str_offsets_base;
24241 }
24242
24243 dwarf_decode_macros (per_objfile, builder, section, lh,
24244 offset_size, offset, str_section, str_offsets_section,
24245 str_offsets_base, section_is_gnu);
24246 }
24247
24248 /* Return the .debug_loc section to use for CU.
24249 For DWO files use .debug_loc.dwo. */
24250
24251 static struct dwarf2_section_info *
24252 cu_debug_loc_section (struct dwarf2_cu *cu)
24253 {
24254 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24255
24256 if (cu->dwo_unit)
24257 {
24258 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24259
24260 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24261 }
24262 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24263 : &per_objfile->per_bfd->loc);
24264 }
24265
24266 /* Return the .debug_rnglists section to use for CU. */
24267 static struct dwarf2_section_info *
24268 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24269 {
24270 if (cu->header.version < 5)
24271 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24272 cu->header.version);
24273 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24274
24275 /* Make sure we read the .debug_rnglists section from the file that
24276 contains the DW_AT_ranges attribute we are reading. Normally that
24277 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24278 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24279 program. */
24280 if (cu->dwo_unit != nullptr
24281 && tag != DW_TAG_compile_unit
24282 && tag != DW_TAG_skeleton_unit)
24283 {
24284 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24285
24286 if (sections->rnglists.size > 0)
24287 return &sections->rnglists;
24288 else
24289 error (_(".debug_rnglists section is missing from .dwo file."));
24290 }
24291 return &dwarf2_per_objfile->per_bfd->rnglists;
24292 }
24293
24294 /* A helper function that fills in a dwarf2_loclist_baton. */
24295
24296 static void
24297 fill_in_loclist_baton (struct dwarf2_cu *cu,
24298 struct dwarf2_loclist_baton *baton,
24299 const struct attribute *attr)
24300 {
24301 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24302 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24303
24304 section->read (per_objfile->objfile);
24305
24306 baton->per_objfile = per_objfile;
24307 baton->per_cu = cu->per_cu;
24308 gdb_assert (baton->per_cu);
24309 /* We don't know how long the location list is, but make sure we
24310 don't run off the edge of the section. */
24311 baton->size = section->size - attr->as_unsigned ();
24312 baton->data = section->buffer + attr->as_unsigned ();
24313 if (cu->base_address.has_value ())
24314 baton->base_address = *cu->base_address;
24315 else
24316 baton->base_address = 0;
24317 baton->from_dwo = cu->dwo_unit != NULL;
24318 }
24319
24320 static void
24321 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24322 struct dwarf2_cu *cu, int is_block)
24323 {
24324 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24325 struct objfile *objfile = per_objfile->objfile;
24326 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24327
24328 if (attr->form_is_section_offset ()
24329 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24330 the section. If so, fall through to the complaint in the
24331 other branch. */
24332 && attr->as_unsigned () < section->get_size (objfile))
24333 {
24334 struct dwarf2_loclist_baton *baton;
24335
24336 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24337
24338 fill_in_loclist_baton (cu, baton, attr);
24339
24340 if (!cu->base_address.has_value ())
24341 complaint (_("Location list used without "
24342 "specifying the CU base address."));
24343
24344 sym->set_aclass_index ((is_block
24345 ? dwarf2_loclist_block_index
24346 : dwarf2_loclist_index));
24347 SYMBOL_LOCATION_BATON (sym) = baton;
24348 }
24349 else
24350 {
24351 struct dwarf2_locexpr_baton *baton;
24352
24353 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24354 baton->per_objfile = per_objfile;
24355 baton->per_cu = cu->per_cu;
24356 gdb_assert (baton->per_cu);
24357
24358 if (attr->form_is_block ())
24359 {
24360 /* Note that we're just copying the block's data pointer
24361 here, not the actual data. We're still pointing into the
24362 info_buffer for SYM's objfile; right now we never release
24363 that buffer, but when we do clean up properly this may
24364 need to change. */
24365 struct dwarf_block *block = attr->as_block ();
24366 baton->size = block->size;
24367 baton->data = block->data;
24368 }
24369 else
24370 {
24371 dwarf2_invalid_attrib_class_complaint ("location description",
24372 sym->natural_name ());
24373 baton->size = 0;
24374 }
24375
24376 sym->set_aclass_index ((is_block
24377 ? dwarf2_locexpr_block_index
24378 : dwarf2_locexpr_index));
24379 SYMBOL_LOCATION_BATON (sym) = baton;
24380 }
24381 }
24382
24383 /* See read.h. */
24384
24385 const comp_unit_head *
24386 dwarf2_per_cu_data::get_header () const
24387 {
24388 if (!m_header_read_in)
24389 {
24390 const gdb_byte *info_ptr
24391 = this->section->buffer + to_underlying (this->sect_off);
24392
24393 memset (&m_header, 0, sizeof (m_header));
24394
24395 read_comp_unit_head (&m_header, info_ptr, this->section,
24396 rcuh_kind::COMPILE);
24397
24398 m_header_read_in = true;
24399 }
24400
24401 return &m_header;
24402 }
24403
24404 /* See read.h. */
24405
24406 int
24407 dwarf2_per_cu_data::addr_size () const
24408 {
24409 return this->get_header ()->addr_size;
24410 }
24411
24412 /* See read.h. */
24413
24414 int
24415 dwarf2_per_cu_data::offset_size () const
24416 {
24417 return this->get_header ()->offset_size;
24418 }
24419
24420 /* See read.h. */
24421
24422 int
24423 dwarf2_per_cu_data::ref_addr_size () const
24424 {
24425 const comp_unit_head *header = this->get_header ();
24426
24427 if (header->version == 2)
24428 return header->addr_size;
24429 else
24430 return header->offset_size;
24431 }
24432
24433 /* A helper function for dwarf2_find_containing_comp_unit that returns
24434 the index of the result, and that searches a vector. It will
24435 return a result even if the offset in question does not actually
24436 occur in any CU. This is separate so that it can be unit
24437 tested. */
24438
24439 static int
24440 dwarf2_find_containing_comp_unit
24441 (sect_offset sect_off,
24442 unsigned int offset_in_dwz,
24443 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24444 {
24445 int low, high;
24446
24447 low = 0;
24448 high = all_comp_units.size () - 1;
24449 while (high > low)
24450 {
24451 struct dwarf2_per_cu_data *mid_cu;
24452 int mid = low + (high - low) / 2;
24453
24454 mid_cu = all_comp_units[mid].get ();
24455 if (mid_cu->is_dwz > offset_in_dwz
24456 || (mid_cu->is_dwz == offset_in_dwz
24457 && mid_cu->sect_off + mid_cu->length > sect_off))
24458 high = mid;
24459 else
24460 low = mid + 1;
24461 }
24462 gdb_assert (low == high);
24463 return low;
24464 }
24465
24466 /* Locate the .debug_info compilation unit from CU's objfile which contains
24467 the DIE at OFFSET. Raises an error on failure. */
24468
24469 static struct dwarf2_per_cu_data *
24470 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24471 unsigned int offset_in_dwz,
24472 dwarf2_per_bfd *per_bfd)
24473 {
24474 int low = dwarf2_find_containing_comp_unit
24475 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
24476 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
24477
24478 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24479 {
24480 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24481 error (_("Dwarf Error: could not find partial DIE containing "
24482 "offset %s [in module %s]"),
24483 sect_offset_str (sect_off),
24484 bfd_get_filename (per_bfd->obfd));
24485
24486 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
24487 <= sect_off);
24488 return per_bfd->all_comp_units[low - 1].get ();
24489 }
24490 else
24491 {
24492 if (low == per_bfd->all_comp_units.size () - 1
24493 && sect_off >= this_cu->sect_off + this_cu->length)
24494 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24495 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24496 return this_cu;
24497 }
24498 }
24499
24500 #if GDB_SELF_TEST
24501
24502 namespace selftests {
24503 namespace find_containing_comp_unit {
24504
24505 static void
24506 run_test ()
24507 {
24508 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24509 dwarf2_per_cu_data *one_ptr = one.get ();
24510 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24511 dwarf2_per_cu_data *two_ptr = two.get ();
24512 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24513 dwarf2_per_cu_data *three_ptr = three.get ();
24514 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24515 dwarf2_per_cu_data *four_ptr = four.get ();
24516
24517 one->length = 5;
24518 two->sect_off = sect_offset (one->length);
24519 two->length = 7;
24520
24521 three->length = 5;
24522 three->is_dwz = 1;
24523 four->sect_off = sect_offset (three->length);
24524 four->length = 7;
24525 four->is_dwz = 1;
24526
24527 std::vector<dwarf2_per_cu_data_up> units;
24528 units.push_back (std::move (one));
24529 units.push_back (std::move (two));
24530 units.push_back (std::move (three));
24531 units.push_back (std::move (four));
24532
24533 int result;
24534
24535 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24536 SELF_CHECK (units[result].get () == one_ptr);
24537 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24538 SELF_CHECK (units[result].get () == one_ptr);
24539 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24540 SELF_CHECK (units[result].get () == two_ptr);
24541
24542 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24543 SELF_CHECK (units[result].get () == three_ptr);
24544 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24545 SELF_CHECK (units[result].get () == three_ptr);
24546 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24547 SELF_CHECK (units[result].get () == four_ptr);
24548 }
24549
24550 }
24551 }
24552
24553 #endif /* GDB_SELF_TEST */
24554
24555 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24556
24557 static void
24558 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24559 enum language pretend_language)
24560 {
24561 struct attribute *attr;
24562
24563 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24564
24565 /* Set the language we're debugging. */
24566 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24567 if (cu->producer != nullptr
24568 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
24569 {
24570 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24571 attribute is not standardised yet. As a workaround for the
24572 language detection we fall back to the DW_AT_producer
24573 string. */
24574 cu->per_cu->lang = language_opencl;
24575 }
24576 else if (cu->producer != nullptr
24577 && strstr (cu->producer, "GNU Go ") != NULL)
24578 {
24579 /* Similar hack for Go. */
24580 cu->per_cu->lang = language_go;
24581 }
24582 else if (attr != nullptr)
24583 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
24584 else
24585 cu->per_cu->lang = pretend_language;
24586 cu->language_defn = language_def (cu->per_cu->lang);
24587 }
24588
24589 /* See read.h. */
24590
24591 dwarf2_cu *
24592 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24593 {
24594 auto it = m_dwarf2_cus.find (per_cu);
24595 if (it == m_dwarf2_cus.end ())
24596 return nullptr;
24597
24598 return it->second;
24599 }
24600
24601 /* See read.h. */
24602
24603 void
24604 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24605 {
24606 gdb_assert (this->get_cu (per_cu) == nullptr);
24607
24608 m_dwarf2_cus[per_cu] = cu;
24609 }
24610
24611 /* See read.h. */
24612
24613 void
24614 dwarf2_per_objfile::age_comp_units ()
24615 {
24616 dwarf_read_debug_printf_v ("running");
24617
24618 /* This is not expected to be called in the middle of CU expansion. There is
24619 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24620 loaded in memory. Calling age_comp_units while the queue is in use could
24621 make us free the DIEs for a CU that is in the queue and therefore break
24622 that invariant. */
24623 gdb_assert (!this->per_bfd->queue.has_value ());
24624
24625 /* Start by clearing all marks. */
24626 for (auto pair : m_dwarf2_cus)
24627 pair.second->clear_mark ();
24628
24629 /* Traverse all CUs, mark them and their dependencies if used recently
24630 enough. */
24631 for (auto pair : m_dwarf2_cus)
24632 {
24633 dwarf2_cu *cu = pair.second;
24634
24635 cu->last_used++;
24636 if (cu->last_used <= dwarf_max_cache_age)
24637 cu->mark ();
24638 }
24639
24640 /* Delete all CUs still not marked. */
24641 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24642 {
24643 dwarf2_cu *cu = it->second;
24644
24645 if (!cu->is_marked ())
24646 {
24647 dwarf_read_debug_printf_v ("deleting old CU %s",
24648 sect_offset_str (cu->per_cu->sect_off));
24649 delete cu;
24650 it = m_dwarf2_cus.erase (it);
24651 }
24652 else
24653 it++;
24654 }
24655 }
24656
24657 /* See read.h. */
24658
24659 void
24660 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24661 {
24662 auto it = m_dwarf2_cus.find (per_cu);
24663 if (it == m_dwarf2_cus.end ())
24664 return;
24665
24666 delete it->second;
24667
24668 m_dwarf2_cus.erase (it);
24669 }
24670
24671 dwarf2_per_objfile::~dwarf2_per_objfile ()
24672 {
24673 remove_all_cus ();
24674 }
24675
24676 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24677 We store these in a hash table separate from the DIEs, and preserve them
24678 when the DIEs are flushed out of cache.
24679
24680 The CU "per_cu" pointer is needed because offset alone is not enough to
24681 uniquely identify the type. A file may have multiple .debug_types sections,
24682 or the type may come from a DWO file. Furthermore, while it's more logical
24683 to use per_cu->section+offset, with Fission the section with the data is in
24684 the DWO file but we don't know that section at the point we need it.
24685 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24686 because we can enter the lookup routine, get_die_type_at_offset, from
24687 outside this file, and thus won't necessarily have PER_CU->cu.
24688 Fortunately, PER_CU is stable for the life of the objfile. */
24689
24690 struct dwarf2_per_cu_offset_and_type
24691 {
24692 const struct dwarf2_per_cu_data *per_cu;
24693 sect_offset sect_off;
24694 struct type *type;
24695 };
24696
24697 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24698
24699 static hashval_t
24700 per_cu_offset_and_type_hash (const void *item)
24701 {
24702 const struct dwarf2_per_cu_offset_and_type *ofs
24703 = (const struct dwarf2_per_cu_offset_and_type *) item;
24704
24705 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24706 }
24707
24708 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24709
24710 static int
24711 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24712 {
24713 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24714 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24715 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24716 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24717
24718 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24719 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24720 }
24721
24722 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24723 table if necessary. For convenience, return TYPE.
24724
24725 The DIEs reading must have careful ordering to:
24726 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24727 reading current DIE.
24728 * Not trying to dereference contents of still incompletely read in types
24729 while reading in other DIEs.
24730 * Enable referencing still incompletely read in types just by a pointer to
24731 the type without accessing its fields.
24732
24733 Therefore caller should follow these rules:
24734 * Try to fetch any prerequisite types we may need to build this DIE type
24735 before building the type and calling set_die_type.
24736 * After building type call set_die_type for current DIE as soon as
24737 possible before fetching more types to complete the current type.
24738 * Make the type as complete as possible before fetching more types. */
24739
24740 static struct type *
24741 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24742 bool skip_data_location)
24743 {
24744 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24745 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24746 struct objfile *objfile = per_objfile->objfile;
24747 struct attribute *attr;
24748 struct dynamic_prop prop;
24749
24750 /* For Ada types, make sure that the gnat-specific data is always
24751 initialized (if not already set). There are a few types where
24752 we should not be doing so, because the type-specific area is
24753 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24754 where the type-specific area is used to store the floatformat).
24755 But this is not a problem, because the gnat-specific information
24756 is actually not needed for these types. */
24757 if (need_gnat_info (cu)
24758 && type->code () != TYPE_CODE_FUNC
24759 && type->code () != TYPE_CODE_FLT
24760 && type->code () != TYPE_CODE_METHODPTR
24761 && type->code () != TYPE_CODE_MEMBERPTR
24762 && type->code () != TYPE_CODE_METHOD
24763 && type->code () != TYPE_CODE_FIXED_POINT
24764 && !HAVE_GNAT_AUX_INFO (type))
24765 INIT_GNAT_SPECIFIC (type);
24766
24767 /* Read DW_AT_allocated and set in type. */
24768 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24769 if (attr != NULL)
24770 {
24771 struct type *prop_type = cu->addr_sized_int_type (false);
24772 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24773 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24774 }
24775
24776 /* Read DW_AT_associated and set in type. */
24777 attr = dwarf2_attr (die, DW_AT_associated, cu);
24778 if (attr != NULL)
24779 {
24780 struct type *prop_type = cu->addr_sized_int_type (false);
24781 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24782 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24783 }
24784
24785 /* Read DW_AT_data_location and set in type. */
24786 if (!skip_data_location)
24787 {
24788 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24789 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24790 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24791 }
24792
24793 if (per_objfile->die_type_hash == NULL)
24794 per_objfile->die_type_hash
24795 = htab_up (htab_create_alloc (127,
24796 per_cu_offset_and_type_hash,
24797 per_cu_offset_and_type_eq,
24798 NULL, xcalloc, xfree));
24799
24800 ofs.per_cu = cu->per_cu;
24801 ofs.sect_off = die->sect_off;
24802 ofs.type = type;
24803 slot = (struct dwarf2_per_cu_offset_and_type **)
24804 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24805 if (*slot)
24806 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24807 sect_offset_str (die->sect_off));
24808 *slot = XOBNEW (&objfile->objfile_obstack,
24809 struct dwarf2_per_cu_offset_and_type);
24810 **slot = ofs;
24811 return type;
24812 }
24813
24814 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24815 or return NULL if the die does not have a saved type. */
24816
24817 static struct type *
24818 get_die_type_at_offset (sect_offset sect_off,
24819 dwarf2_per_cu_data *per_cu,
24820 dwarf2_per_objfile *per_objfile)
24821 {
24822 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24823
24824 if (per_objfile->die_type_hash == NULL)
24825 return NULL;
24826
24827 ofs.per_cu = per_cu;
24828 ofs.sect_off = sect_off;
24829 slot = ((struct dwarf2_per_cu_offset_and_type *)
24830 htab_find (per_objfile->die_type_hash.get (), &ofs));
24831 if (slot)
24832 return slot->type;
24833 else
24834 return NULL;
24835 }
24836
24837 /* Look up the type for DIE in CU in die_type_hash,
24838 or return NULL if DIE does not have a saved type. */
24839
24840 static struct type *
24841 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24842 {
24843 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24844 }
24845
24846 /* Trivial hash function for partial_die_info: the hash value of a DIE
24847 is its offset in .debug_info for this objfile. */
24848
24849 static hashval_t
24850 partial_die_hash (const void *item)
24851 {
24852 const struct partial_die_info *part_die
24853 = (const struct partial_die_info *) item;
24854
24855 return to_underlying (part_die->sect_off);
24856 }
24857
24858 /* Trivial comparison function for partial_die_info structures: two DIEs
24859 are equal if they have the same offset. */
24860
24861 static int
24862 partial_die_eq (const void *item_lhs, const void *item_rhs)
24863 {
24864 const struct partial_die_info *part_die_lhs
24865 = (const struct partial_die_info *) item_lhs;
24866 const struct partial_die_info *part_die_rhs
24867 = (const struct partial_die_info *) item_rhs;
24868
24869 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24870 }
24871
24872 struct cmd_list_element *set_dwarf_cmdlist;
24873 struct cmd_list_element *show_dwarf_cmdlist;
24874
24875 static void
24876 show_check_physname (struct ui_file *file, int from_tty,
24877 struct cmd_list_element *c, const char *value)
24878 {
24879 fprintf_filtered (file,
24880 _("Whether to check \"physname\" is %s.\n"),
24881 value);
24882 }
24883
24884 void _initialize_dwarf2_read ();
24885 void
24886 _initialize_dwarf2_read ()
24887 {
24888 add_setshow_prefix_cmd ("dwarf", class_maintenance,
24889 _("\
24890 Set DWARF specific variables.\n\
24891 Configure DWARF variables such as the cache size."),
24892 _("\
24893 Show DWARF specific variables.\n\
24894 Show DWARF variables such as the cache size."),
24895 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
24896 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
24897
24898 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24899 &dwarf_max_cache_age, _("\
24900 Set the upper bound on the age of cached DWARF compilation units."), _("\
24901 Show the upper bound on the age of cached DWARF compilation units."), _("\
24902 A higher limit means that cached compilation units will be stored\n\
24903 in memory longer, and more total memory will be used. Zero disables\n\
24904 caching, which can slow down startup."),
24905 NULL,
24906 show_dwarf_max_cache_age,
24907 &set_dwarf_cmdlist,
24908 &show_dwarf_cmdlist);
24909
24910 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24911 Set debugging of the DWARF reader."), _("\
24912 Show debugging of the DWARF reader."), _("\
24913 When enabled (non-zero), debugging messages are printed during DWARF\n\
24914 reading and symtab expansion. A value of 1 (one) provides basic\n\
24915 information. A value greater than 1 provides more verbose information."),
24916 NULL,
24917 NULL,
24918 &setdebuglist, &showdebuglist);
24919
24920 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24921 Set debugging of the DWARF DIE reader."), _("\
24922 Show debugging of the DWARF DIE reader."), _("\
24923 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24924 The value is the maximum depth to print."),
24925 NULL,
24926 NULL,
24927 &setdebuglist, &showdebuglist);
24928
24929 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24930 Set debugging of the dwarf line reader."), _("\
24931 Show debugging of the dwarf line reader."), _("\
24932 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24933 A value of 1 (one) provides basic information.\n\
24934 A value greater than 1 provides more verbose information."),
24935 NULL,
24936 NULL,
24937 &setdebuglist, &showdebuglist);
24938
24939 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24940 Set cross-checking of \"physname\" code against demangler."), _("\
24941 Show cross-checking of \"physname\" code against demangler."), _("\
24942 When enabled, GDB's internal \"physname\" code is checked against\n\
24943 the demangler."),
24944 NULL, show_check_physname,
24945 &setdebuglist, &showdebuglist);
24946
24947 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24948 no_class, &use_deprecated_index_sections, _("\
24949 Set whether to use deprecated gdb_index sections."), _("\
24950 Show whether to use deprecated gdb_index sections."), _("\
24951 When enabled, deprecated .gdb_index sections are used anyway.\n\
24952 Normally they are ignored either because of a missing feature or\n\
24953 performance issue.\n\
24954 Warning: This option must be enabled before gdb reads the file."),
24955 NULL,
24956 NULL,
24957 &setlist, &showlist);
24958
24959 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24960 &dwarf2_locexpr_funcs);
24961 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24962 &dwarf2_loclist_funcs);
24963
24964 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24965 &dwarf2_block_frame_base_locexpr_funcs);
24966 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24967 &dwarf2_block_frame_base_loclist_funcs);
24968
24969 #if GDB_SELF_TEST
24970 selftests::register_test ("dw2_expand_symtabs_matching",
24971 selftests::dw2_expand_symtabs_matching::run_test);
24972 selftests::register_test ("dwarf2_find_containing_comp_unit",
24973 selftests::find_containing_comp_unit::run_test);
24974 #endif
24975 }