]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
gdb: remove symbol value macros
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91 #include <unordered_set>
92
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
97
98 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
99
100 #define dwarf_read_debug_printf(fmt, ...) \
101 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
102 ##__VA_ARGS__)
103
104 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
105
106 #define dwarf_read_debug_printf_v(fmt, ...) \
107 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
109
110 /* When non-zero, dump DIEs after they are read in. */
111 static unsigned int dwarf_die_debug = 0;
112
113 /* When non-zero, dump line number entries as they are read in. */
114 unsigned int dwarf_line_debug = 0;
115
116 /* When true, cross-check physname against demangler. */
117 static bool check_physname = false;
118
119 /* When true, do not reject deprecated .gdb_index sections. */
120 static bool use_deprecated_index_sections = false;
121
122 /* This is used to store the data that is always per objfile. */
123 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125 /* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
135
136 /* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138 static int dwarf2_locexpr_index;
139 static int dwarf2_loclist_index;
140 static int dwarf2_locexpr_block_index;
141 static int dwarf2_loclist_block_index;
142
143 /* Size of .debug_loclists section header for 32-bit DWARF format. */
144 #define LOCLIST_HEADER_SIZE32 12
145
146 /* Size of .debug_loclists section header for 64-bit DWARF format. */
147 #define LOCLIST_HEADER_SIZE64 20
148
149 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
150 #define RNGLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
153 #define RNGLIST_HEADER_SIZE64 20
154
155 /* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168 struct name_component
169 {
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178 };
179
180 /* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183 struct mapped_index_base
184 {
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
211 void build_name_components (dwarf2_per_objfile *per_objfile);
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
218 find_name_components_bounds (const lookup_name_info &ln_no_params,
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
221
222 /* Prevent deleting/destroying via a base class pointer. */
223 protected:
224 ~mapped_index_base() = default;
225 };
226
227 /* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231 class offset_view
232 {
233 public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262 private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265 };
266
267 /* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
269 struct mapped_index final : public mapped_index_base
270 {
271 /* Index data format version. */
272 int version = 0;
273
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
276
277 /* The symbol table, implemented as a hash table. */
278 offset_view symbol_table;
279
280 /* A pointer to the constant pool. */
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
296
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
301 }
302
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size () / 2; }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
363 };
364
365 /* See dwarf2/read.h. */
366
367 dwarf2_per_objfile *
368 get_dwarf2_per_objfile (struct objfile *objfile)
369 {
370 return dwarf2_objfile_data_key.get (objfile);
371 }
372
373 /* Default names of the debugging sections. */
374
375 /* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
378 const struct dwarf2_debug_sections dwarf2_elf_names =
379 {
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
384 { ".debug_loclists", ".zdebug_loclists" },
385 { ".debug_macinfo", ".zdebug_macinfo" },
386 { ".debug_macro", ".zdebug_macro" },
387 { ".debug_str", ".zdebug_str" },
388 { ".debug_str_offsets", ".zdebug_str_offsets" },
389 { ".debug_line_str", ".zdebug_line_str" },
390 { ".debug_ranges", ".zdebug_ranges" },
391 { ".debug_rnglists", ".zdebug_rnglists" },
392 { ".debug_types", ".zdebug_types" },
393 { ".debug_addr", ".zdebug_addr" },
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
396 { ".gdb_index", ".zgdb_index" },
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
399 23
400 };
401
402 /* List of DWO/DWP sections. */
403
404 static const struct dwop_section_names
405 {
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
410 struct dwarf2_section_names loclists_dwo;
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
413 struct dwarf2_section_names rnglists_dwo;
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
419 }
420 dwop_section_names =
421 {
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
435 };
436
437 /* local data types */
438
439 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441 struct loclists_rnglists_header
442 {
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461 };
462
463 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466 struct stmt_list_hash
467 {
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
472 sect_offset line_sect_off;
473 };
474
475 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
479
480 struct type_unit_group : public dwarf2_per_cu_data
481 {
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
485 std::vector<signatured_type *> *tus = nullptr;
486
487 /* The data used to construct the hash key. */
488 struct stmt_list_hash hash {};
489 };
490
491 /* These sections are what may appear in a (real or virtual) DWO file. */
492
493 struct dwo_sections
494 {
495 struct dwarf2_section_info abbrev;
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
498 struct dwarf2_section_info loclists;
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
501 struct dwarf2_section_info rnglists;
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
506 std::vector<dwarf2_section_info> types;
507 };
508
509 /* CUs/TUs in DWP/DWO files. */
510
511 struct dwo_unit
512 {
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
522 struct dwarf2_section_info *section;
523
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530 };
531
532 /* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536 enum dwp_v2_section_ids
537 {
538 DW_SECT_MIN = 1
539 };
540
541 /* Data for one DWO file.
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
551
552 struct dwo_file
553 {
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
561 const char *dwo_name = nullptr;
562
563 /* The DW_AT_comp_dir attribute. */
564 const char *comp_dir = nullptr;
565
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
568 gdb_bfd_ref_ptr dbfd;
569
570 /* The sections that make up this DWO file.
571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
572 sections (for lack of a better name). */
573 struct dwo_sections sections {};
574
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
579 htab_up cus;
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
583 htab_up tus;
584 };
585
586 /* These sections are what may appear in a DWP file. */
587
588 struct dwp_sections
589 {
590 /* These are used by all DWP versions (1, 2 and 5). */
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
594
595 /* These are only used by DWP version 2 and version 5 files.
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
607 struct dwarf2_section_info loclists;
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
610 struct dwarf2_section_info rnglists;
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
613 };
614
615 /* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
617
618 struct virtual_v1_dwo_sections
619 {
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
627 That is recorded here, and copied to dwo_unit.section. */
628 struct dwarf2_section_info info_or_types;
629 };
630
631 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
636 struct virtual_v2_or_v5_dwo_sections
637 {
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666 };
667
668 /* Contents of DWP hash tables. */
669
670 struct dwp_hash_table
671 {
672 uint32_t version, nr_columns;
673 uint32_t nr_units, nr_slots;
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685 #define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700 #define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
712 } section_pool;
713 };
714
715 /* Data for one DWP file. */
716
717 struct dwp_file
718 {
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
725 /* Name of the file. */
726 const char *name;
727
728 /* File format version. */
729 int version = 0;
730
731 /* The bfd. */
732 gdb_bfd_ref_ptr dbfd;
733
734 /* Section info for this file. */
735 struct dwp_sections sections {};
736
737 /* Table of CUs in the file. */
738 const struct dwp_hash_table *cus = nullptr;
739
740 /* Table of TUs in the file. */
741 const struct dwp_hash_table *tus = nullptr;
742
743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
744 htab_up loaded_cus;
745 htab_up loaded_tus;
746
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
751 };
752
753 /* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
756 struct exists to abstract away the constant parameters of die reading. */
757
758 struct die_reader_specs
759 {
760 /* The bfd of die_section. */
761 bfd *abfd;
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
767 struct dwo_file *dwo_file;
768
769 /* The section the die comes from.
770 This is either .debug_info or .debug_types, or the .dwo variants. */
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
774 const gdb_byte *buffer;
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
778
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
781 };
782
783 /* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786 class cutu_reader : public die_reader_specs
787 {
788 public:
789
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
792 struct abbrev_table *abbrev_table,
793 dwarf2_cu *existing_cu,
794 bool skip_partial);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
797 dwarf2_per_objfile *per_objfile,
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
805 bool dummy_p = false;
806
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
811 private:
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
814 dwarf2_cu *existing_cu);
815
816 struct dwarf2_per_cu_data *m_this_cu;
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824 };
825
826 /* When we construct a partial symbol table entry we only
827 need this much information. */
828 struct partial_die_info : public allocate_on_obstack
829 {
830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
835 partial_die_info (const partial_die_info &) = default;
836
837 /* Adjust the partial die before generating a symbol for it. This
838 function may set the is_external flag or change the DIE's
839 name. */
840 void fixup (struct dwarf2_cu *cu);
841
842 /* Read a minimal amount of information into the minimal die
843 structure. */
844 const gdb_byte *read (const struct die_reader_specs *reader,
845 const struct abbrev_info &abbrev,
846 const gdb_byte *info_ptr);
847
848 /* Compute the name of this partial DIE. This memoizes the
849 result, so it is safe to call multiple times. */
850 const char *name (dwarf2_cu *cu);
851
852 /* Offset of this DIE. */
853 const sect_offset sect_off;
854
855 /* DWARF-2 tag for this DIE. */
856 const ENUM_BITFIELD(dwarf_tag) tag : 16;
857
858 /* Assorted flags describing the data found in this DIE. */
859 const unsigned int has_children : 1;
860
861 unsigned int is_external : 1;
862 unsigned int is_declaration : 1;
863 unsigned int has_type : 1;
864 unsigned int has_specification : 1;
865 unsigned int has_pc_info : 1;
866 unsigned int has_range_info : 1;
867 unsigned int may_be_inlined : 1;
868
869 /* This DIE has been marked DW_AT_main_subprogram. */
870 unsigned int main_subprogram : 1;
871
872 /* Flag set if the SCOPE field of this structure has been
873 computed. */
874 unsigned int scope_set : 1;
875
876 /* Flag set if the DIE has a byte_size attribute. */
877 unsigned int has_byte_size : 1;
878
879 /* Flag set if the DIE has a DW_AT_const_value attribute. */
880 unsigned int has_const_value : 1;
881
882 /* Flag set if any of the DIE's children are template arguments. */
883 unsigned int has_template_arguments : 1;
884
885 /* Flag set if fixup has been called on this die. */
886 unsigned int fixup_called : 1;
887
888 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
889 unsigned int is_dwz : 1;
890
891 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
892 unsigned int spec_is_dwz : 1;
893
894 unsigned int canonical_name : 1;
895
896 /* The name of this DIE. Normally the value of DW_AT_name, but
897 sometimes a default name for unnamed DIEs. */
898 const char *raw_name = nullptr;
899
900 /* The linkage name, if present. */
901 const char *linkage_name = nullptr;
902
903 /* The scope to prepend to our children. This is generally
904 allocated on the comp_unit_obstack, so will disappear
905 when this compilation unit leaves the cache. */
906 const char *scope = nullptr;
907
908 /* Some data associated with the partial DIE. The tag determines
909 which field is live. */
910 union
911 {
912 /* The location description associated with this DIE, if any. */
913 struct dwarf_block *locdesc;
914 /* The offset of an import, for DW_TAG_imported_unit. */
915 sect_offset sect_off;
916 } d {};
917
918 union
919 {
920 /* If HAS_PC_INFO, the PC range associated with this DIE. */
921 struct
922 {
923 CORE_ADDR lowpc;
924 CORE_ADDR highpc;
925 };
926 /* If HAS_RANGE_INFO, the ranges offset associated with this DIE. */
927 ULONGEST ranges_offset;
928 };
929
930 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
931 DW_AT_sibling, if any. */
932 /* NOTE: This member isn't strictly necessary, partial_die_info::read
933 could return DW_AT_sibling values to its caller load_partial_dies. */
934 const gdb_byte *sibling = nullptr;
935
936 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
937 DW_AT_specification (or DW_AT_abstract_origin or
938 DW_AT_extension). */
939 sect_offset spec_offset {};
940
941 /* Pointers to this DIE's parent, first child, and next sibling,
942 if any. */
943 struct partial_die_info *die_parent = nullptr;
944 struct partial_die_info *die_child = nullptr;
945 struct partial_die_info *die_sibling = nullptr;
946
947 friend struct partial_die_info *
948 dwarf2_cu::find_partial_die (sect_offset sect_off);
949
950 private:
951 /* Only need to do look up in dwarf2_cu::find_partial_die. */
952 partial_die_info (sect_offset sect_off)
953 : partial_die_info (sect_off, DW_TAG_padding, 0)
954 {
955 }
956
957 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
958 int has_children_)
959 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
960 {
961 is_external = 0;
962 is_declaration = 0;
963 has_type = 0;
964 has_specification = 0;
965 has_pc_info = 0;
966 has_range_info = 0;
967 may_be_inlined = 0;
968 main_subprogram = 0;
969 scope_set = 0;
970 has_byte_size = 0;
971 has_const_value = 0;
972 has_template_arguments = 0;
973 fixup_called = 0;
974 is_dwz = 0;
975 spec_is_dwz = 0;
976 canonical_name = 0;
977 /* Don't set these using NSDMI (Non-static data member initialisation),
978 because g++-4.8 will error out. */
979 lowpc = 0;
980 highpc = 0;
981 }
982 };
983
984 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
985 but this would require a corresponding change in unpack_field_as_long
986 and friends. */
987 static int bits_per_byte = 8;
988
989 struct variant_part_builder;
990
991 /* When reading a variant, we track a bit more information about the
992 field, and store it in an object of this type. */
993
994 struct variant_field
995 {
996 int first_field = -1;
997 int last_field = -1;
998
999 /* A variant can contain other variant parts. */
1000 std::vector<variant_part_builder> variant_parts;
1001
1002 /* If we see a DW_TAG_variant, then this will be set if this is the
1003 default branch. */
1004 bool default_branch = false;
1005 /* If we see a DW_AT_discr_value, then this will be the discriminant
1006 value. */
1007 ULONGEST discriminant_value = 0;
1008 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1009 data. */
1010 struct dwarf_block *discr_list_data = nullptr;
1011 };
1012
1013 /* This represents a DW_TAG_variant_part. */
1014
1015 struct variant_part_builder
1016 {
1017 /* The offset of the discriminant field. */
1018 sect_offset discriminant_offset {};
1019
1020 /* Variants that are direct children of this variant part. */
1021 std::vector<variant_field> variants;
1022
1023 /* True if we're currently reading a variant. */
1024 bool processing_variant = false;
1025 };
1026
1027 struct nextfield
1028 {
1029 int accessibility = 0;
1030 int virtuality = 0;
1031 /* Variant parts need to find the discriminant, which is a DIE
1032 reference. We track the section offset of each field to make
1033 this link. */
1034 sect_offset offset;
1035 struct field field {};
1036 };
1037
1038 struct fnfieldlist
1039 {
1040 const char *name = nullptr;
1041 std::vector<struct fn_field> fnfields;
1042 };
1043
1044 /* The routines that read and process dies for a C struct or C++ class
1045 pass lists of data member fields and lists of member function fields
1046 in an instance of a field_info structure, as defined below. */
1047 struct field_info
1048 {
1049 /* List of data member and baseclasses fields. */
1050 std::vector<struct nextfield> fields;
1051 std::vector<struct nextfield> baseclasses;
1052
1053 /* Set if the accessibility of one of the fields is not public. */
1054 bool non_public_fields = false;
1055
1056 /* Member function fieldlist array, contains name of possibly overloaded
1057 member function, number of overloaded member functions and a pointer
1058 to the head of the member function field chain. */
1059 std::vector<struct fnfieldlist> fnfieldlists;
1060
1061 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1062 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1063 std::vector<struct decl_field> typedef_field_list;
1064
1065 /* Nested types defined by this class and the number of elements in this
1066 list. */
1067 std::vector<struct decl_field> nested_types_list;
1068
1069 /* If non-null, this is the variant part we are currently
1070 reading. */
1071 variant_part_builder *current_variant_part = nullptr;
1072 /* This holds all the top-level variant parts attached to the type
1073 we're reading. */
1074 std::vector<variant_part_builder> variant_parts;
1075
1076 /* Return the total number of fields (including baseclasses). */
1077 int nfields () const
1078 {
1079 return fields.size () + baseclasses.size ();
1080 }
1081 };
1082
1083 /* Loaded secondary compilation units are kept in memory until they
1084 have not been referenced for the processing of this many
1085 compilation units. Set this to zero to disable caching. Cache
1086 sizes of up to at least twenty will improve startup time for
1087 typical inter-CU-reference binaries, at an obvious memory cost. */
1088 static int dwarf_max_cache_age = 5;
1089 static void
1090 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1091 struct cmd_list_element *c, const char *value)
1092 {
1093 gdb_printf (file, _("The upper bound on the age of cached "
1094 "DWARF compilation units is %s.\n"),
1095 value);
1096 }
1097 \f
1098 /* local function prototypes */
1099
1100 static void dwarf2_find_base_address (struct die_info *die,
1101 struct dwarf2_cu *cu);
1102
1103 static dwarf2_psymtab *create_partial_symtab
1104 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1105 const char *name);
1106
1107 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1108 const gdb_byte *info_ptr,
1109 struct die_info *type_unit_die);
1110
1111 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1112
1113 static void scan_partial_symbols (struct partial_die_info *,
1114 CORE_ADDR *, CORE_ADDR *,
1115 int, struct dwarf2_cu *);
1116
1117 static void add_partial_symbol (struct partial_die_info *,
1118 struct dwarf2_cu *);
1119
1120 static void add_partial_namespace (struct partial_die_info *pdi,
1121 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1122 int set_addrmap, struct dwarf2_cu *cu);
1123
1124 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1125 CORE_ADDR *highpc, int set_addrmap,
1126 struct dwarf2_cu *cu);
1127
1128 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1129 struct dwarf2_cu *cu);
1130
1131 static void add_partial_subprogram (struct partial_die_info *pdi,
1132 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1133 int need_pc, struct dwarf2_cu *cu);
1134
1135 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1136
1137 static struct partial_die_info *load_partial_dies
1138 (const struct die_reader_specs *, const gdb_byte *, int);
1139
1140 /* A pair of partial_die_info and compilation unit. */
1141 struct cu_partial_die_info
1142 {
1143 /* The compilation unit of the partial_die_info. */
1144 struct dwarf2_cu *cu;
1145 /* A partial_die_info. */
1146 struct partial_die_info *pdi;
1147
1148 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1149 : cu (cu),
1150 pdi (pdi)
1151 { /* Nothing. */ }
1152
1153 private:
1154 cu_partial_die_info () = delete;
1155 };
1156
1157 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1158 struct dwarf2_cu *);
1159
1160 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1161 struct attribute *,
1162 const struct attr_abbrev *,
1163 const gdb_byte *);
1164
1165 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1166 struct attribute *attr, dwarf_tag tag);
1167
1168 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1169
1170 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1171 dwarf2_section_info *, sect_offset);
1172
1173 static const char *read_indirect_string
1174 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1175 const struct comp_unit_head *, unsigned int *);
1176
1177 static const char *read_indirect_string_at_offset
1178 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1179
1180 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1181 const gdb_byte *,
1182 unsigned int *);
1183
1184 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1185 ULONGEST str_index);
1186
1187 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1188 ULONGEST str_index);
1189
1190 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1191 struct dwarf2_cu *);
1192
1193 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1194 struct dwarf2_cu *cu);
1195
1196 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1197
1198 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1199 struct dwarf2_cu *cu);
1200
1201 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1202
1203 static struct die_info *die_specification (struct die_info *die,
1204 struct dwarf2_cu **);
1205
1206 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1207 struct dwarf2_cu *cu);
1208
1209 static void dwarf_decode_lines (struct line_header *,
1210 const file_and_directory &,
1211 struct dwarf2_cu *, dwarf2_psymtab *,
1212 CORE_ADDR, int decode_mapping);
1213
1214 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1215 const char *);
1216
1217 static struct symbol *new_symbol (struct die_info *, struct type *,
1218 struct dwarf2_cu *, struct symbol * = NULL);
1219
1220 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1221 struct dwarf2_cu *);
1222
1223 static void dwarf2_const_value_attr (const struct attribute *attr,
1224 struct type *type,
1225 const char *name,
1226 struct obstack *obstack,
1227 struct dwarf2_cu *cu, LONGEST *value,
1228 const gdb_byte **bytes,
1229 struct dwarf2_locexpr_baton **baton);
1230
1231 static struct type *read_subrange_index_type (struct die_info *die,
1232 struct dwarf2_cu *cu);
1233
1234 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1235
1236 static int need_gnat_info (struct dwarf2_cu *);
1237
1238 static struct type *die_descriptive_type (struct die_info *,
1239 struct dwarf2_cu *);
1240
1241 static void set_descriptive_type (struct type *, struct die_info *,
1242 struct dwarf2_cu *);
1243
1244 static struct type *die_containing_type (struct die_info *,
1245 struct dwarf2_cu *);
1246
1247 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1248 struct dwarf2_cu *);
1249
1250 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1251
1252 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1253
1254 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1255
1256 static char *typename_concat (struct obstack *obs, const char *prefix,
1257 const char *suffix, int physname,
1258 struct dwarf2_cu *cu);
1259
1260 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1261
1262 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1263
1264 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1265
1266 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1267
1268 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1269
1270 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1271
1272 /* Return the .debug_loclists section to use for cu. */
1273 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1274
1275 /* Return the .debug_rnglists section to use for cu. */
1276 static struct dwarf2_section_info *cu_debug_rnglists_section
1277 (struct dwarf2_cu *cu, dwarf_tag tag);
1278
1279 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1280 values. Keep the items ordered with increasing constraints compliance. */
1281 enum pc_bounds_kind
1282 {
1283 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1284 PC_BOUNDS_NOT_PRESENT,
1285
1286 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1287 were present but they do not form a valid range of PC addresses. */
1288 PC_BOUNDS_INVALID,
1289
1290 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1291 PC_BOUNDS_RANGES,
1292
1293 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1294 PC_BOUNDS_HIGH_LOW,
1295 };
1296
1297 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1298 CORE_ADDR *, CORE_ADDR *,
1299 struct dwarf2_cu *,
1300 dwarf2_psymtab *);
1301
1302 static void get_scope_pc_bounds (struct die_info *,
1303 CORE_ADDR *, CORE_ADDR *,
1304 struct dwarf2_cu *);
1305
1306 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1307 CORE_ADDR, struct dwarf2_cu *);
1308
1309 static void dwarf2_add_field (struct field_info *, struct die_info *,
1310 struct dwarf2_cu *);
1311
1312 static void dwarf2_attach_fields_to_type (struct field_info *,
1313 struct type *, struct dwarf2_cu *);
1314
1315 static void dwarf2_add_member_fn (struct field_info *,
1316 struct die_info *, struct type *,
1317 struct dwarf2_cu *);
1318
1319 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1320 struct type *,
1321 struct dwarf2_cu *);
1322
1323 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1324
1325 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1326
1327 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1328
1329 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1330
1331 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1332
1333 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1334
1335 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1336
1337 static struct type *read_module_type (struct die_info *die,
1338 struct dwarf2_cu *cu);
1339
1340 static const char *namespace_name (struct die_info *die,
1341 int *is_anonymous, struct dwarf2_cu *);
1342
1343 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1344
1345 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1346 bool * = nullptr);
1347
1348 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1349 struct dwarf2_cu *);
1350
1351 static struct die_info *read_die_and_siblings_1
1352 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1353 struct die_info *);
1354
1355 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1356 const gdb_byte *info_ptr,
1357 const gdb_byte **new_info_ptr,
1358 struct die_info *parent);
1359
1360 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1361 struct die_info **, const gdb_byte *,
1362 int);
1363
1364 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1365 struct die_info **, const gdb_byte *);
1366
1367 static void process_die (struct die_info *, struct dwarf2_cu *);
1368
1369 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1370 struct objfile *);
1371
1372 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1373
1374 static const char *dwarf2_full_name (const char *name,
1375 struct die_info *die,
1376 struct dwarf2_cu *cu);
1377
1378 static const char *dwarf2_physname (const char *name, struct die_info *die,
1379 struct dwarf2_cu *cu);
1380
1381 static struct die_info *dwarf2_extension (struct die_info *die,
1382 struct dwarf2_cu **);
1383
1384 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1385
1386 static void dump_die_for_error (struct die_info *);
1387
1388 static void dump_die_1 (struct ui_file *, int level, int max_level,
1389 struct die_info *);
1390
1391 /*static*/ void dump_die (struct die_info *, int max_level);
1392
1393 static void store_in_ref_table (struct die_info *,
1394 struct dwarf2_cu *);
1395
1396 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1397 const struct attribute *,
1398 struct dwarf2_cu **);
1399
1400 static struct die_info *follow_die_ref (struct die_info *,
1401 const struct attribute *,
1402 struct dwarf2_cu **);
1403
1404 static struct die_info *follow_die_sig (struct die_info *,
1405 const struct attribute *,
1406 struct dwarf2_cu **);
1407
1408 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1409 struct dwarf2_cu *);
1410
1411 static struct type *get_DW_AT_signature_type (struct die_info *,
1412 const struct attribute *,
1413 struct dwarf2_cu *);
1414
1415 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1416 dwarf2_per_objfile *per_objfile);
1417
1418 static void read_signatured_type (signatured_type *sig_type,
1419 dwarf2_per_objfile *per_objfile);
1420
1421 static int attr_to_dynamic_prop (const struct attribute *attr,
1422 struct die_info *die, struct dwarf2_cu *cu,
1423 struct dynamic_prop *prop, struct type *type);
1424
1425 /* memory allocation interface */
1426
1427 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1428
1429 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1430
1431 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1432
1433 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1434 struct dwarf2_loclist_baton *baton,
1435 const struct attribute *attr);
1436
1437 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1438 struct symbol *sym,
1439 struct dwarf2_cu *cu,
1440 int is_block);
1441
1442 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1443 const gdb_byte *info_ptr,
1444 const struct abbrev_info *abbrev);
1445
1446 static hashval_t partial_die_hash (const void *item);
1447
1448 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1449
1450 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1451 (sect_offset sect_off, unsigned int offset_in_dwz,
1452 dwarf2_per_bfd *per_bfd);
1453
1454 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1455 struct die_info *comp_unit_die,
1456 enum language pretend_language);
1457
1458 static struct type *set_die_type (struct die_info *, struct type *,
1459 struct dwarf2_cu *, bool = false);
1460
1461 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1462
1463 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1464 dwarf2_per_objfile *per_objfile,
1465 dwarf2_cu *existing_cu,
1466 bool skip_partial,
1467 enum language pretend_language);
1468
1469 static void process_full_comp_unit (dwarf2_cu *cu,
1470 enum language pretend_language);
1471
1472 static void process_full_type_unit (dwarf2_cu *cu,
1473 enum language pretend_language);
1474
1475 static struct type *get_die_type_at_offset (sect_offset,
1476 dwarf2_per_cu_data *per_cu,
1477 dwarf2_per_objfile *per_objfile);
1478
1479 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1480
1481 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1482 dwarf2_per_objfile *per_objfile,
1483 enum language pretend_language);
1484
1485 static void process_queue (dwarf2_per_objfile *per_objfile);
1486
1487 /* Class, the destructor of which frees all allocated queue entries. This
1488 will only have work to do if an error was thrown while processing the
1489 dwarf. If no error was thrown then the queue entries should have all
1490 been processed, and freed, as we went along. */
1491
1492 class dwarf2_queue_guard
1493 {
1494 public:
1495 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1496 : m_per_objfile (per_objfile)
1497 {
1498 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1499
1500 m_per_objfile->per_bfd->queue.emplace ();
1501 }
1502
1503 /* Free any entries remaining on the queue. There should only be
1504 entries left if we hit an error while processing the dwarf. */
1505 ~dwarf2_queue_guard ()
1506 {
1507 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1508
1509 m_per_objfile->per_bfd->queue.reset ();
1510 }
1511
1512 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1513
1514 private:
1515 dwarf2_per_objfile *m_per_objfile;
1516 };
1517
1518 dwarf2_queue_item::~dwarf2_queue_item ()
1519 {
1520 /* Anything still marked queued is likely to be in an
1521 inconsistent state, so discard it. */
1522 if (per_cu->queued)
1523 {
1524 per_objfile->remove_cu (per_cu);
1525 per_cu->queued = 0;
1526 }
1527 }
1528
1529 /* See dwarf2/read.h. */
1530
1531 void
1532 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1533 {
1534 if (data->is_debug_types)
1535 delete static_cast<signatured_type *> (data);
1536 else
1537 delete data;
1538 }
1539
1540 static file_and_directory &find_file_and_directory
1541 (struct die_info *die, struct dwarf2_cu *cu);
1542
1543 static const char *compute_include_file_name
1544 (const struct line_header *lh,
1545 const file_entry &fe,
1546 const file_and_directory &cu_info,
1547 gdb::unique_xmalloc_ptr<char> *name_holder);
1548
1549 static htab_up allocate_signatured_type_table ();
1550
1551 static htab_up allocate_dwo_unit_table ();
1552
1553 static struct dwo_unit *lookup_dwo_unit_in_dwp
1554 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1555 const char *comp_dir, ULONGEST signature, int is_debug_types);
1556
1557 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1558
1559 static struct dwo_unit *lookup_dwo_comp_unit
1560 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1561 ULONGEST signature);
1562
1563 static struct dwo_unit *lookup_dwo_type_unit
1564 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1565
1566 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1567
1568 /* A unique pointer to a dwo_file. */
1569
1570 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1571
1572 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1573
1574 static void check_producer (struct dwarf2_cu *cu);
1575 \f
1576 /* Various complaints about symbol reading that don't abort the process. */
1577
1578 static void
1579 dwarf2_debug_line_missing_file_complaint (void)
1580 {
1581 complaint (_(".debug_line section has line data without a file"));
1582 }
1583
1584 static void
1585 dwarf2_debug_line_missing_end_sequence_complaint (void)
1586 {
1587 complaint (_(".debug_line section has line "
1588 "program sequence without an end"));
1589 }
1590
1591 static void
1592 dwarf2_complex_location_expr_complaint (void)
1593 {
1594 complaint (_("location expression too complex"));
1595 }
1596
1597 static void
1598 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1599 int arg3)
1600 {
1601 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1602 arg1, arg2, arg3);
1603 }
1604
1605 static void
1606 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1607 {
1608 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1609 arg1, arg2);
1610 }
1611
1612 /* Hash function for line_header_hash. */
1613
1614 static hashval_t
1615 line_header_hash (const struct line_header *ofs)
1616 {
1617 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1618 }
1619
1620 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1621
1622 static hashval_t
1623 line_header_hash_voidp (const void *item)
1624 {
1625 const struct line_header *ofs = (const struct line_header *) item;
1626
1627 return line_header_hash (ofs);
1628 }
1629
1630 /* Equality function for line_header_hash. */
1631
1632 static int
1633 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1634 {
1635 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1636 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1637
1638 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1639 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1640 }
1641
1642 \f
1643
1644 /* An iterator for all_comp_units that is based on index. This
1645 approach makes it possible to iterate over all_comp_units safely,
1646 when some caller in the loop may add new units. */
1647
1648 class all_comp_units_iterator
1649 {
1650 public:
1651
1652 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1653 : m_per_bfd (per_bfd),
1654 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1655 {
1656 }
1657
1658 all_comp_units_iterator &operator++ ()
1659 {
1660 ++m_index;
1661 return *this;
1662 }
1663
1664 dwarf2_per_cu_data *operator* () const
1665 {
1666 return m_per_bfd->get_cu (m_index);
1667 }
1668
1669 bool operator== (const all_comp_units_iterator &other) const
1670 {
1671 return m_index == other.m_index;
1672 }
1673
1674
1675 bool operator!= (const all_comp_units_iterator &other) const
1676 {
1677 return m_index != other.m_index;
1678 }
1679
1680 private:
1681
1682 dwarf2_per_bfd *m_per_bfd;
1683 size_t m_index;
1684 };
1685
1686 /* A range adapter for the all_comp_units_iterator. */
1687 class all_comp_units_range
1688 {
1689 public:
1690
1691 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1692 : m_per_bfd (per_bfd)
1693 {
1694 }
1695
1696 all_comp_units_iterator begin ()
1697 {
1698 return all_comp_units_iterator (m_per_bfd, true);
1699 }
1700
1701 all_comp_units_iterator end ()
1702 {
1703 return all_comp_units_iterator (m_per_bfd, false);
1704 }
1705
1706 private:
1707
1708 dwarf2_per_bfd *m_per_bfd;
1709 };
1710
1711 /* See declaration. */
1712
1713 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1714 bool can_copy_)
1715 : obfd (obfd),
1716 can_copy (can_copy_)
1717 {
1718 if (names == NULL)
1719 names = &dwarf2_elf_names;
1720
1721 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1722 locate_sections (obfd, sec, *names);
1723 }
1724
1725 dwarf2_per_bfd::~dwarf2_per_bfd ()
1726 {
1727 for (auto &per_cu : all_comp_units)
1728 {
1729 per_cu->imported_symtabs_free ();
1730 per_cu->free_cached_file_names ();
1731 }
1732
1733 /* Everything else should be on this->obstack. */
1734 }
1735
1736 /* See read.h. */
1737
1738 void
1739 dwarf2_per_objfile::remove_all_cus ()
1740 {
1741 gdb_assert (!this->per_bfd->queue.has_value ());
1742
1743 for (auto pair : m_dwarf2_cus)
1744 delete pair.second;
1745
1746 m_dwarf2_cus.clear ();
1747 }
1748
1749 /* A helper class that calls free_cached_comp_units on
1750 destruction. */
1751
1752 class free_cached_comp_units
1753 {
1754 public:
1755
1756 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1757 : m_per_objfile (per_objfile)
1758 {
1759 }
1760
1761 ~free_cached_comp_units ()
1762 {
1763 m_per_objfile->remove_all_cus ();
1764 }
1765
1766 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1767
1768 private:
1769
1770 dwarf2_per_objfile *m_per_objfile;
1771 };
1772
1773 /* See read.h. */
1774
1775 bool
1776 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1777 {
1778 if (per_cu->index < this->m_symtabs.size ())
1779 return this->m_symtabs[per_cu->index] != nullptr;
1780 return false;
1781 }
1782
1783 /* See read.h. */
1784
1785 compunit_symtab *
1786 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1787 {
1788 if (per_cu->index < this->m_symtabs.size ())
1789 return this->m_symtabs[per_cu->index];
1790 return nullptr;
1791 }
1792
1793 /* See read.h. */
1794
1795 void
1796 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1797 compunit_symtab *symtab)
1798 {
1799 if (per_cu->index >= this->m_symtabs.size ())
1800 this->m_symtabs.resize (per_cu->index + 1);
1801 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1802 this->m_symtabs[per_cu->index] = symtab;
1803 }
1804
1805 /* Try to locate the sections we need for DWARF 2 debugging
1806 information and return true if we have enough to do something.
1807 NAMES points to the dwarf2 section names, or is NULL if the standard
1808 ELF names are used. CAN_COPY is true for formats where symbol
1809 interposition is possible and so symbol values must follow copy
1810 relocation rules. */
1811
1812 int
1813 dwarf2_has_info (struct objfile *objfile,
1814 const struct dwarf2_debug_sections *names,
1815 bool can_copy)
1816 {
1817 if (objfile->flags & OBJF_READNEVER)
1818 return 0;
1819
1820 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1821
1822 if (per_objfile == NULL)
1823 {
1824 dwarf2_per_bfd *per_bfd;
1825
1826 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1827 BFD doesn't require relocations.
1828
1829 We don't share with objfiles for which -readnow was requested,
1830 because it would complicate things when loading the same BFD with
1831 -readnow and then without -readnow. */
1832 if (!gdb_bfd_requires_relocations (objfile->obfd)
1833 && (objfile->flags & OBJF_READNOW) == 0)
1834 {
1835 /* See if one has been created for this BFD yet. */
1836 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1837
1838 if (per_bfd == nullptr)
1839 {
1840 /* No, create it now. */
1841 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1842 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1843 }
1844 }
1845 else
1846 {
1847 /* No sharing possible, create one specifically for this objfile. */
1848 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1849 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1850 }
1851
1852 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1853 }
1854
1855 return (!per_objfile->per_bfd->info.is_virtual
1856 && per_objfile->per_bfd->info.s.section != NULL
1857 && !per_objfile->per_bfd->abbrev.is_virtual
1858 && per_objfile->per_bfd->abbrev.s.section != NULL);
1859 }
1860
1861 /* See declaration. */
1862
1863 void
1864 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1865 const dwarf2_debug_sections &names)
1866 {
1867 flagword aflag = bfd_section_flags (sectp);
1868
1869 if ((aflag & SEC_HAS_CONTENTS) == 0)
1870 {
1871 }
1872 else if (elf_section_data (sectp)->this_hdr.sh_size
1873 > bfd_get_file_size (abfd))
1874 {
1875 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1876 warning (_("Discarding section %s which has a section size (%s"
1877 ") larger than the file size [in module %s]"),
1878 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1879 bfd_get_filename (abfd));
1880 }
1881 else if (names.info.matches (sectp->name))
1882 {
1883 this->info.s.section = sectp;
1884 this->info.size = bfd_section_size (sectp);
1885 }
1886 else if (names.abbrev.matches (sectp->name))
1887 {
1888 this->abbrev.s.section = sectp;
1889 this->abbrev.size = bfd_section_size (sectp);
1890 }
1891 else if (names.line.matches (sectp->name))
1892 {
1893 this->line.s.section = sectp;
1894 this->line.size = bfd_section_size (sectp);
1895 }
1896 else if (names.loc.matches (sectp->name))
1897 {
1898 this->loc.s.section = sectp;
1899 this->loc.size = bfd_section_size (sectp);
1900 }
1901 else if (names.loclists.matches (sectp->name))
1902 {
1903 this->loclists.s.section = sectp;
1904 this->loclists.size = bfd_section_size (sectp);
1905 }
1906 else if (names.macinfo.matches (sectp->name))
1907 {
1908 this->macinfo.s.section = sectp;
1909 this->macinfo.size = bfd_section_size (sectp);
1910 }
1911 else if (names.macro.matches (sectp->name))
1912 {
1913 this->macro.s.section = sectp;
1914 this->macro.size = bfd_section_size (sectp);
1915 }
1916 else if (names.str.matches (sectp->name))
1917 {
1918 this->str.s.section = sectp;
1919 this->str.size = bfd_section_size (sectp);
1920 }
1921 else if (names.str_offsets.matches (sectp->name))
1922 {
1923 this->str_offsets.s.section = sectp;
1924 this->str_offsets.size = bfd_section_size (sectp);
1925 }
1926 else if (names.line_str.matches (sectp->name))
1927 {
1928 this->line_str.s.section = sectp;
1929 this->line_str.size = bfd_section_size (sectp);
1930 }
1931 else if (names.addr.matches (sectp->name))
1932 {
1933 this->addr.s.section = sectp;
1934 this->addr.size = bfd_section_size (sectp);
1935 }
1936 else if (names.frame.matches (sectp->name))
1937 {
1938 this->frame.s.section = sectp;
1939 this->frame.size = bfd_section_size (sectp);
1940 }
1941 else if (names.eh_frame.matches (sectp->name))
1942 {
1943 this->eh_frame.s.section = sectp;
1944 this->eh_frame.size = bfd_section_size (sectp);
1945 }
1946 else if (names.ranges.matches (sectp->name))
1947 {
1948 this->ranges.s.section = sectp;
1949 this->ranges.size = bfd_section_size (sectp);
1950 }
1951 else if (names.rnglists.matches (sectp->name))
1952 {
1953 this->rnglists.s.section = sectp;
1954 this->rnglists.size = bfd_section_size (sectp);
1955 }
1956 else if (names.types.matches (sectp->name))
1957 {
1958 struct dwarf2_section_info type_section;
1959
1960 memset (&type_section, 0, sizeof (type_section));
1961 type_section.s.section = sectp;
1962 type_section.size = bfd_section_size (sectp);
1963
1964 this->types.push_back (type_section);
1965 }
1966 else if (names.gdb_index.matches (sectp->name))
1967 {
1968 this->gdb_index.s.section = sectp;
1969 this->gdb_index.size = bfd_section_size (sectp);
1970 }
1971 else if (names.debug_names.matches (sectp->name))
1972 {
1973 this->debug_names.s.section = sectp;
1974 this->debug_names.size = bfd_section_size (sectp);
1975 }
1976 else if (names.debug_aranges.matches (sectp->name))
1977 {
1978 this->debug_aranges.s.section = sectp;
1979 this->debug_aranges.size = bfd_section_size (sectp);
1980 }
1981
1982 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1983 && bfd_section_vma (sectp) == 0)
1984 this->has_section_at_zero = true;
1985 }
1986
1987 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1988 SECTION_NAME. */
1989
1990 void
1991 dwarf2_get_section_info (struct objfile *objfile,
1992 enum dwarf2_section_enum sect,
1993 asection **sectp, const gdb_byte **bufp,
1994 bfd_size_type *sizep)
1995 {
1996 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1997 struct dwarf2_section_info *info;
1998
1999 /* We may see an objfile without any DWARF, in which case we just
2000 return nothing. */
2001 if (per_objfile == NULL)
2002 {
2003 *sectp = NULL;
2004 *bufp = NULL;
2005 *sizep = 0;
2006 return;
2007 }
2008 switch (sect)
2009 {
2010 case DWARF2_DEBUG_FRAME:
2011 info = &per_objfile->per_bfd->frame;
2012 break;
2013 case DWARF2_EH_FRAME:
2014 info = &per_objfile->per_bfd->eh_frame;
2015 break;
2016 default:
2017 gdb_assert_not_reached ("unexpected section");
2018 }
2019
2020 info->read (objfile);
2021
2022 *sectp = info->get_bfd_section ();
2023 *bufp = info->buffer;
2024 *sizep = info->size;
2025 }
2026
2027 \f
2028 /* DWARF quick_symbol_functions support. */
2029
2030 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2031 unique line tables, so we maintain a separate table of all .debug_line
2032 derived entries to support the sharing.
2033 All the quick functions need is the list of file names. We discard the
2034 line_header when we're done and don't need to record it here. */
2035 struct quick_file_names
2036 {
2037 /* The data used to construct the hash key. */
2038 struct stmt_list_hash hash;
2039
2040 /* The number of entries in file_names, real_names. */
2041 unsigned int num_file_names;
2042
2043 /* The CU directory, as given by DW_AT_comp_dir. May be
2044 nullptr. */
2045 const char *comp_dir;
2046
2047 /* The file names from the line table, after being run through
2048 file_full_name. */
2049 const char **file_names;
2050
2051 /* The file names from the line table after being run through
2052 gdb_realpath. These are computed lazily. */
2053 const char **real_names;
2054 };
2055
2056 /* When using the index (and thus not using psymtabs), each CU has an
2057 object of this type. This is used to hold information needed by
2058 the various "quick" methods. */
2059 struct dwarf2_per_cu_quick_data
2060 {
2061 /* The file table. This can be NULL if there was no file table
2062 or it's currently not read in.
2063 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2064 struct quick_file_names *file_names;
2065
2066 /* A temporary mark bit used when iterating over all CUs in
2067 expand_symtabs_matching. */
2068 unsigned int mark : 1;
2069
2070 /* True if we've tried to read the file table. There will be no
2071 point in trying to read it again next time. */
2072 bool files_read : 1;
2073 };
2074
2075 /* A subclass of psymbol_functions that arranges to read the DWARF
2076 partial symbols when needed. */
2077 struct lazy_dwarf_reader : public psymbol_functions
2078 {
2079 using psymbol_functions::psymbol_functions;
2080
2081 bool can_lazily_read_symbols () override
2082 {
2083 return true;
2084 }
2085
2086 void read_partial_symbols (struct objfile *objfile) override
2087 {
2088 if (dwarf2_has_info (objfile, nullptr))
2089 dwarf2_build_psymtabs (objfile, this);
2090 }
2091 };
2092
2093 static quick_symbol_functions_up
2094 make_lazy_dwarf_reader ()
2095 {
2096 return quick_symbol_functions_up (new lazy_dwarf_reader);
2097 }
2098
2099 struct dwarf2_base_index_functions : public quick_symbol_functions
2100 {
2101 bool has_symbols (struct objfile *objfile) override;
2102
2103 bool has_unexpanded_symtabs (struct objfile *objfile) override;
2104
2105 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2106
2107 void forget_cached_source_info (struct objfile *objfile) override;
2108
2109 enum language lookup_global_symbol_language (struct objfile *objfile,
2110 const char *name,
2111 domain_enum domain,
2112 bool *symbol_found_p) override
2113 {
2114 *symbol_found_p = false;
2115 return language_unknown;
2116 }
2117
2118 void print_stats (struct objfile *objfile, bool print_bcache) override;
2119
2120 void expand_all_symtabs (struct objfile *objfile) override;
2121
2122 struct compunit_symtab *find_pc_sect_compunit_symtab
2123 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2124 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2125
2126 struct compunit_symtab *find_compunit_symtab_by_address
2127 (struct objfile *objfile, CORE_ADDR address) override
2128 {
2129 return nullptr;
2130 }
2131
2132 void map_symbol_filenames (struct objfile *objfile,
2133 gdb::function_view<symbol_filename_ftype> fun,
2134 bool need_fullname) override;
2135 };
2136
2137 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2138 {
2139 void dump (struct objfile *objfile) override;
2140
2141 void expand_matching_symbols
2142 (struct objfile *,
2143 const lookup_name_info &lookup_name,
2144 domain_enum domain,
2145 int global,
2146 symbol_compare_ftype *ordered_compare) override;
2147
2148 bool expand_symtabs_matching
2149 (struct objfile *objfile,
2150 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2151 const lookup_name_info *lookup_name,
2152 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2153 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2154 block_search_flags search_flags,
2155 domain_enum domain,
2156 enum search_domain kind) override;
2157 };
2158
2159 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2160 {
2161 void dump (struct objfile *objfile) override;
2162
2163 void expand_matching_symbols
2164 (struct objfile *,
2165 const lookup_name_info &lookup_name,
2166 domain_enum domain,
2167 int global,
2168 symbol_compare_ftype *ordered_compare) override;
2169
2170 bool expand_symtabs_matching
2171 (struct objfile *objfile,
2172 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2173 const lookup_name_info *lookup_name,
2174 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2176 block_search_flags search_flags,
2177 domain_enum domain,
2178 enum search_domain kind) override;
2179 };
2180
2181 static quick_symbol_functions_up
2182 make_dwarf_gdb_index ()
2183 {
2184 return quick_symbol_functions_up (new dwarf2_gdb_index);
2185 }
2186
2187 static quick_symbol_functions_up
2188 make_dwarf_debug_names ()
2189 {
2190 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2191 }
2192
2193 /* Utility hash function for a stmt_list_hash. */
2194
2195 static hashval_t
2196 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2197 {
2198 hashval_t v = 0;
2199
2200 if (stmt_list_hash->dwo_unit != NULL)
2201 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2202 v += to_underlying (stmt_list_hash->line_sect_off);
2203 return v;
2204 }
2205
2206 /* Utility equality function for a stmt_list_hash. */
2207
2208 static int
2209 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2210 const struct stmt_list_hash *rhs)
2211 {
2212 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2213 return 0;
2214 if (lhs->dwo_unit != NULL
2215 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2216 return 0;
2217
2218 return lhs->line_sect_off == rhs->line_sect_off;
2219 }
2220
2221 /* Hash function for a quick_file_names. */
2222
2223 static hashval_t
2224 hash_file_name_entry (const void *e)
2225 {
2226 const struct quick_file_names *file_data
2227 = (const struct quick_file_names *) e;
2228
2229 return hash_stmt_list_entry (&file_data->hash);
2230 }
2231
2232 /* Equality function for a quick_file_names. */
2233
2234 static int
2235 eq_file_name_entry (const void *a, const void *b)
2236 {
2237 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2238 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2239
2240 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2241 }
2242
2243 /* Create a quick_file_names hash table. */
2244
2245 static htab_up
2246 create_quick_file_names_table (unsigned int nr_initial_entries)
2247 {
2248 return htab_up (htab_create_alloc (nr_initial_entries,
2249 hash_file_name_entry, eq_file_name_entry,
2250 nullptr, xcalloc, xfree));
2251 }
2252
2253 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2254 function is unrelated to symtabs, symtab would have to be created afterwards.
2255 You should call age_cached_comp_units after processing the CU. */
2256
2257 static dwarf2_cu *
2258 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2259 bool skip_partial)
2260 {
2261 if (per_cu->is_debug_types)
2262 load_full_type_unit (per_cu, per_objfile);
2263 else
2264 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2265 skip_partial, language_minimal);
2266
2267 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2268 if (cu == nullptr)
2269 return nullptr; /* Dummy CU. */
2270
2271 dwarf2_find_base_address (cu->dies, cu);
2272
2273 return cu;
2274 }
2275
2276 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2277
2278 static void
2279 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2280 dwarf2_per_objfile *per_objfile, bool skip_partial)
2281 {
2282 /* Skip type_unit_groups, reading the type units they contain
2283 is handled elsewhere. */
2284 if (per_cu->type_unit_group_p ())
2285 return;
2286
2287 {
2288 /* The destructor of dwarf2_queue_guard frees any entries left on
2289 the queue. After this point we're guaranteed to leave this function
2290 with the dwarf queue empty. */
2291 dwarf2_queue_guard q_guard (per_objfile);
2292
2293 if (!per_objfile->symtab_set_p (per_cu))
2294 {
2295 queue_comp_unit (per_cu, per_objfile, language_minimal);
2296 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2297
2298 /* If we just loaded a CU from a DWO, and we're working with an index
2299 that may badly handle TUs, load all the TUs in that DWO as well.
2300 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2301 if (!per_cu->is_debug_types
2302 && cu != NULL
2303 && cu->dwo_unit != NULL
2304 && per_objfile->per_bfd->index_table != NULL
2305 && per_objfile->per_bfd->index_table->version <= 7
2306 /* DWP files aren't supported yet. */
2307 && get_dwp_file (per_objfile) == NULL)
2308 queue_and_load_all_dwo_tus (cu);
2309 }
2310
2311 process_queue (per_objfile);
2312 }
2313
2314 /* Age the cache, releasing compilation units that have not
2315 been used recently. */
2316 per_objfile->age_comp_units ();
2317 }
2318
2319 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2320 the per-objfile for which this symtab is instantiated.
2321
2322 Returns the resulting symbol table. */
2323
2324 static struct compunit_symtab *
2325 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2326 dwarf2_per_objfile *per_objfile,
2327 bool skip_partial)
2328 {
2329 gdb_assert (per_objfile->per_bfd->using_index);
2330
2331 if (!per_objfile->symtab_set_p (per_cu))
2332 {
2333 free_cached_comp_units freer (per_objfile);
2334 scoped_restore decrementer = increment_reading_symtab ();
2335 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2336 process_cu_includes (per_objfile);
2337 }
2338
2339 return per_objfile->get_symtab (per_cu);
2340 }
2341
2342 /* See read.h. */
2343
2344 dwarf2_per_cu_data_up
2345 dwarf2_per_bfd::allocate_per_cu ()
2346 {
2347 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2348 result->per_bfd = this;
2349 result->index = all_comp_units.size ();
2350 return result;
2351 }
2352
2353 /* See read.h. */
2354
2355 signatured_type_up
2356 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2357 {
2358 signatured_type_up result (new signatured_type (signature));
2359 result->per_bfd = this;
2360 result->index = all_comp_units.size ();
2361 result->is_debug_types = true;
2362 tu_stats.nr_tus++;
2363 return result;
2364 }
2365
2366 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2367 obstack, and constructed with the specified field values. */
2368
2369 static dwarf2_per_cu_data_up
2370 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2371 struct dwarf2_section_info *section,
2372 int is_dwz,
2373 sect_offset sect_off, ULONGEST length)
2374 {
2375 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2376 the_cu->sect_off = sect_off;
2377 the_cu->length = length;
2378 the_cu->section = section;
2379 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2380 struct dwarf2_per_cu_quick_data);
2381 the_cu->is_dwz = is_dwz;
2382 return the_cu;
2383 }
2384
2385 /* A helper for create_cus_from_index that handles a given list of
2386 CUs. */
2387
2388 static void
2389 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2390 const gdb_byte *cu_list, offset_type n_elements,
2391 struct dwarf2_section_info *section,
2392 int is_dwz)
2393 {
2394 for (offset_type i = 0; i < n_elements; i += 2)
2395 {
2396 gdb_static_assert (sizeof (ULONGEST) >= 8);
2397
2398 sect_offset sect_off
2399 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2400 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2401 cu_list += 2 * 8;
2402
2403 dwarf2_per_cu_data_up per_cu
2404 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2405 length);
2406 per_bfd->all_comp_units.push_back (std::move (per_cu));
2407 }
2408 }
2409
2410 /* Read the CU list from the mapped index, and use it to create all
2411 the CU objects for PER_BFD. */
2412
2413 static void
2414 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2415 const gdb_byte *cu_list, offset_type cu_list_elements,
2416 const gdb_byte *dwz_list, offset_type dwz_elements)
2417 {
2418 gdb_assert (per_bfd->all_comp_units.empty ());
2419 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2420
2421 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2422 &per_bfd->info, 0);
2423
2424 if (dwz_elements == 0)
2425 return;
2426
2427 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2428 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2429 &dwz->info, 1);
2430 }
2431
2432 /* Create the signatured type hash table from the index. */
2433
2434 static void
2435 create_signatured_type_table_from_index
2436 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2437 const gdb_byte *bytes, offset_type elements)
2438 {
2439 htab_up sig_types_hash = allocate_signatured_type_table ();
2440
2441 for (offset_type i = 0; i < elements; i += 3)
2442 {
2443 signatured_type_up sig_type;
2444 ULONGEST signature;
2445 void **slot;
2446 cu_offset type_offset_in_tu;
2447
2448 gdb_static_assert (sizeof (ULONGEST) >= 8);
2449 sect_offset sect_off
2450 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2451 type_offset_in_tu
2452 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2453 BFD_ENDIAN_LITTLE);
2454 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2455 bytes += 3 * 8;
2456
2457 sig_type = per_bfd->allocate_signatured_type (signature);
2458 sig_type->type_offset_in_tu = type_offset_in_tu;
2459 sig_type->section = section;
2460 sig_type->sect_off = sect_off;
2461 sig_type->v.quick
2462 = OBSTACK_ZALLOC (&per_bfd->obstack,
2463 struct dwarf2_per_cu_quick_data);
2464
2465 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2466 *slot = sig_type.get ();
2467
2468 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2469 }
2470
2471 per_bfd->signatured_types = std::move (sig_types_hash);
2472 }
2473
2474 /* Create the signatured type hash table from .debug_names. */
2475
2476 static void
2477 create_signatured_type_table_from_debug_names
2478 (dwarf2_per_objfile *per_objfile,
2479 const mapped_debug_names &map,
2480 struct dwarf2_section_info *section,
2481 struct dwarf2_section_info *abbrev_section)
2482 {
2483 struct objfile *objfile = per_objfile->objfile;
2484
2485 section->read (objfile);
2486 abbrev_section->read (objfile);
2487
2488 htab_up sig_types_hash = allocate_signatured_type_table ();
2489
2490 for (uint32_t i = 0; i < map.tu_count; ++i)
2491 {
2492 signatured_type_up sig_type;
2493 void **slot;
2494
2495 sect_offset sect_off
2496 = (sect_offset) (extract_unsigned_integer
2497 (map.tu_table_reordered + i * map.offset_size,
2498 map.offset_size,
2499 map.dwarf5_byte_order));
2500
2501 comp_unit_head cu_header;
2502 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2503 abbrev_section,
2504 section->buffer + to_underlying (sect_off),
2505 rcuh_kind::TYPE);
2506
2507 sig_type = per_objfile->per_bfd->allocate_signatured_type
2508 (cu_header.signature);
2509 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2510 sig_type->section = section;
2511 sig_type->sect_off = sect_off;
2512 sig_type->v.quick
2513 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2514 struct dwarf2_per_cu_quick_data);
2515
2516 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2517 *slot = sig_type.get ();
2518
2519 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2520 }
2521
2522 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2523 }
2524
2525 /* Read the address map data from the mapped index, and use it to
2526 populate the psymtabs_addrmap. */
2527
2528 static void
2529 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2530 struct mapped_index *index)
2531 {
2532 struct objfile *objfile = per_objfile->objfile;
2533 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2534 struct gdbarch *gdbarch = objfile->arch ();
2535 const gdb_byte *iter, *end;
2536 struct addrmap *mutable_map;
2537 CORE_ADDR baseaddr;
2538
2539 auto_obstack temp_obstack;
2540
2541 mutable_map = addrmap_create_mutable (&temp_obstack);
2542
2543 iter = index->address_table.data ();
2544 end = iter + index->address_table.size ();
2545
2546 baseaddr = objfile->text_section_offset ();
2547
2548 while (iter < end)
2549 {
2550 ULONGEST hi, lo, cu_index;
2551 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2552 iter += 8;
2553 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2554 iter += 8;
2555 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2556 iter += 4;
2557
2558 if (lo > hi)
2559 {
2560 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2561 hex_string (lo), hex_string (hi));
2562 continue;
2563 }
2564
2565 if (cu_index >= per_bfd->all_comp_units.size ())
2566 {
2567 complaint (_(".gdb_index address table has invalid CU number %u"),
2568 (unsigned) cu_index);
2569 continue;
2570 }
2571
2572 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2573 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2574 addrmap_set_empty (mutable_map, lo, hi - 1,
2575 per_bfd->get_cu (cu_index));
2576 }
2577
2578 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2579 &per_bfd->obstack);
2580 }
2581
2582 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2583 populate the psymtabs_addrmap. */
2584
2585 static void
2586 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2587 struct dwarf2_section_info *section)
2588 {
2589 struct objfile *objfile = per_objfile->objfile;
2590 bfd *abfd = objfile->obfd;
2591 struct gdbarch *gdbarch = objfile->arch ();
2592 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2593 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2594
2595 auto_obstack temp_obstack;
2596 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2597
2598 std::unordered_map<sect_offset,
2599 dwarf2_per_cu_data *,
2600 gdb::hash_enum<sect_offset>>
2601 debug_info_offset_to_per_cu;
2602 for (const auto &per_cu : per_bfd->all_comp_units)
2603 {
2604 /* A TU will not need aranges, and skipping them here is an easy
2605 way of ignoring .debug_types -- and possibly seeing a
2606 duplicate section offset -- entirely. */
2607 if (per_cu->is_debug_types)
2608 continue;
2609
2610 const auto insertpair
2611 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2612 per_cu.get ());
2613 if (!insertpair.second)
2614 {
2615 warning (_("Section .debug_aranges in %s has duplicate "
2616 "debug_info_offset %s, ignoring .debug_aranges."),
2617 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2618 return;
2619 }
2620 }
2621
2622 section->read (objfile);
2623
2624 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2625
2626 const gdb_byte *addr = section->buffer;
2627
2628 while (addr < section->buffer + section->size)
2629 {
2630 const gdb_byte *const entry_addr = addr;
2631 unsigned int bytes_read;
2632
2633 const LONGEST entry_length = read_initial_length (abfd, addr,
2634 &bytes_read);
2635 addr += bytes_read;
2636
2637 const gdb_byte *const entry_end = addr + entry_length;
2638 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2639 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2640 if (addr + entry_length > section->buffer + section->size)
2641 {
2642 warning (_("Section .debug_aranges in %s entry at offset %s "
2643 "length %s exceeds section length %s, "
2644 "ignoring .debug_aranges."),
2645 objfile_name (objfile),
2646 plongest (entry_addr - section->buffer),
2647 plongest (bytes_read + entry_length),
2648 pulongest (section->size));
2649 return;
2650 }
2651
2652 /* The version number. */
2653 const uint16_t version = read_2_bytes (abfd, addr);
2654 addr += 2;
2655 if (version != 2)
2656 {
2657 warning (_("Section .debug_aranges in %s entry at offset %s "
2658 "has unsupported version %d, ignoring .debug_aranges."),
2659 objfile_name (objfile),
2660 plongest (entry_addr - section->buffer), version);
2661 return;
2662 }
2663
2664 const uint64_t debug_info_offset
2665 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2666 addr += offset_size;
2667 const auto per_cu_it
2668 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2669 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2670 {
2671 warning (_("Section .debug_aranges in %s entry at offset %s "
2672 "debug_info_offset %s does not exists, "
2673 "ignoring .debug_aranges."),
2674 objfile_name (objfile),
2675 plongest (entry_addr - section->buffer),
2676 pulongest (debug_info_offset));
2677 return;
2678 }
2679 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2680
2681 const uint8_t address_size = *addr++;
2682 if (address_size < 1 || address_size > 8)
2683 {
2684 warning (_("Section .debug_aranges in %s entry at offset %s "
2685 "address_size %u is invalid, ignoring .debug_aranges."),
2686 objfile_name (objfile),
2687 plongest (entry_addr - section->buffer), address_size);
2688 return;
2689 }
2690
2691 const uint8_t segment_selector_size = *addr++;
2692 if (segment_selector_size != 0)
2693 {
2694 warning (_("Section .debug_aranges in %s entry at offset %s "
2695 "segment_selector_size %u is not supported, "
2696 "ignoring .debug_aranges."),
2697 objfile_name (objfile),
2698 plongest (entry_addr - section->buffer),
2699 segment_selector_size);
2700 return;
2701 }
2702
2703 /* Must pad to an alignment boundary that is twice the address
2704 size. It is undocumented by the DWARF standard but GCC does
2705 use it. However, not every compiler does this. We can see
2706 whether it has happened by looking at the total length of the
2707 contents of the aranges for this CU -- it if isn't a multiple
2708 of twice the address size, then we skip any leftover
2709 bytes. */
2710 addr += (entry_end - addr) % (2 * address_size);
2711
2712 for (;;)
2713 {
2714 if (addr + 2 * address_size > entry_end)
2715 {
2716 warning (_("Section .debug_aranges in %s entry at offset %s "
2717 "address list is not properly terminated, "
2718 "ignoring .debug_aranges."),
2719 objfile_name (objfile),
2720 plongest (entry_addr - section->buffer));
2721 return;
2722 }
2723 ULONGEST start = extract_unsigned_integer (addr, address_size,
2724 dwarf5_byte_order);
2725 addr += address_size;
2726 ULONGEST length = extract_unsigned_integer (addr, address_size,
2727 dwarf5_byte_order);
2728 addr += address_size;
2729 if (start == 0 && length == 0)
2730 break;
2731 if (start == 0 && !per_bfd->has_section_at_zero)
2732 {
2733 /* Symbol was eliminated due to a COMDAT group. */
2734 continue;
2735 }
2736 ULONGEST end = start + length;
2737 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2738 - baseaddr);
2739 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2740 - baseaddr);
2741 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2742 }
2743 }
2744
2745 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2746 &per_bfd->obstack);
2747 }
2748
2749 /* A helper function that reads the .gdb_index from BUFFER and fills
2750 in MAP. FILENAME is the name of the file containing the data;
2751 it is used for error reporting. DEPRECATED_OK is true if it is
2752 ok to use deprecated sections.
2753
2754 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2755 out parameters that are filled in with information about the CU and
2756 TU lists in the section.
2757
2758 Returns true if all went well, false otherwise. */
2759
2760 static bool
2761 read_gdb_index_from_buffer (const char *filename,
2762 bool deprecated_ok,
2763 gdb::array_view<const gdb_byte> buffer,
2764 struct mapped_index *map,
2765 const gdb_byte **cu_list,
2766 offset_type *cu_list_elements,
2767 const gdb_byte **types_list,
2768 offset_type *types_list_elements)
2769 {
2770 const gdb_byte *addr = &buffer[0];
2771 offset_view metadata (buffer);
2772
2773 /* Version check. */
2774 offset_type version = metadata[0];
2775 /* Versions earlier than 3 emitted every copy of a psymbol. This
2776 causes the index to behave very poorly for certain requests. Version 3
2777 contained incomplete addrmap. So, it seems better to just ignore such
2778 indices. */
2779 if (version < 4)
2780 {
2781 static int warning_printed = 0;
2782 if (!warning_printed)
2783 {
2784 warning (_("Skipping obsolete .gdb_index section in %s."),
2785 filename);
2786 warning_printed = 1;
2787 }
2788 return 0;
2789 }
2790 /* Index version 4 uses a different hash function than index version
2791 5 and later.
2792
2793 Versions earlier than 6 did not emit psymbols for inlined
2794 functions. Using these files will cause GDB not to be able to
2795 set breakpoints on inlined functions by name, so we ignore these
2796 indices unless the user has done
2797 "set use-deprecated-index-sections on". */
2798 if (version < 6 && !deprecated_ok)
2799 {
2800 static int warning_printed = 0;
2801 if (!warning_printed)
2802 {
2803 warning (_("\
2804 Skipping deprecated .gdb_index section in %s.\n\
2805 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2806 to use the section anyway."),
2807 filename);
2808 warning_printed = 1;
2809 }
2810 return 0;
2811 }
2812 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2813 of the TU (for symbols coming from TUs),
2814 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2815 Plus gold-generated indices can have duplicate entries for global symbols,
2816 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2817 These are just performance bugs, and we can't distinguish gdb-generated
2818 indices from gold-generated ones, so issue no warning here. */
2819
2820 /* Indexes with higher version than the one supported by GDB may be no
2821 longer backward compatible. */
2822 if (version > 8)
2823 return 0;
2824
2825 map->version = version;
2826
2827 int i = 1;
2828 *cu_list = addr + metadata[i];
2829 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2830 ++i;
2831
2832 *types_list = addr + metadata[i];
2833 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2834 ++i;
2835
2836 const gdb_byte *address_table = addr + metadata[i];
2837 const gdb_byte *address_table_end = addr + metadata[i + 1];
2838 map->address_table
2839 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2840 ++i;
2841
2842 const gdb_byte *symbol_table = addr + metadata[i];
2843 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2844 map->symbol_table
2845 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2846 symbol_table_end));
2847
2848 ++i;
2849 map->constant_pool = buffer.slice (metadata[i]);
2850
2851 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2852 {
2853 /* An empty constant pool implies that all symbol table entries are
2854 empty. Make map->symbol_table.empty () == true. */
2855 map->symbol_table
2856 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2857 symbol_table));
2858 }
2859
2860 return 1;
2861 }
2862
2863 /* Callback types for dwarf2_read_gdb_index. */
2864
2865 typedef gdb::function_view
2866 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2867 get_gdb_index_contents_ftype;
2868 typedef gdb::function_view
2869 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2870 get_gdb_index_contents_dwz_ftype;
2871
2872 /* Read .gdb_index. If everything went ok, initialize the "quick"
2873 elements of all the CUs and return 1. Otherwise, return 0. */
2874
2875 static int
2876 dwarf2_read_gdb_index
2877 (dwarf2_per_objfile *per_objfile,
2878 get_gdb_index_contents_ftype get_gdb_index_contents,
2879 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2880 {
2881 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2882 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2883 struct dwz_file *dwz;
2884 struct objfile *objfile = per_objfile->objfile;
2885 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2886
2887 gdb::array_view<const gdb_byte> main_index_contents
2888 = get_gdb_index_contents (objfile, per_bfd);
2889
2890 if (main_index_contents.empty ())
2891 return 0;
2892
2893 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2894 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2895 use_deprecated_index_sections,
2896 main_index_contents, map.get (), &cu_list,
2897 &cu_list_elements, &types_list,
2898 &types_list_elements))
2899 return 0;
2900
2901 /* Don't use the index if it's empty. */
2902 if (map->symbol_table.empty ())
2903 return 0;
2904
2905 /* If there is a .dwz file, read it so we can get its CU list as
2906 well. */
2907 dwz = dwarf2_get_dwz_file (per_bfd);
2908 if (dwz != NULL)
2909 {
2910 struct mapped_index dwz_map;
2911 const gdb_byte *dwz_types_ignore;
2912 offset_type dwz_types_elements_ignore;
2913
2914 gdb::array_view<const gdb_byte> dwz_index_content
2915 = get_gdb_index_contents_dwz (objfile, dwz);
2916
2917 if (dwz_index_content.empty ())
2918 return 0;
2919
2920 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2921 1, dwz_index_content, &dwz_map,
2922 &dwz_list, &dwz_list_elements,
2923 &dwz_types_ignore,
2924 &dwz_types_elements_ignore))
2925 {
2926 warning (_("could not read '.gdb_index' section from %s; skipping"),
2927 bfd_get_filename (dwz->dwz_bfd.get ()));
2928 return 0;
2929 }
2930 }
2931
2932 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2933 dwz_list_elements);
2934
2935 if (types_list_elements)
2936 {
2937 /* We can only handle a single .debug_types when we have an
2938 index. */
2939 if (per_bfd->types.size () != 1)
2940 return 0;
2941
2942 dwarf2_section_info *section = &per_bfd->types[0];
2943
2944 create_signatured_type_table_from_index (per_bfd, section, types_list,
2945 types_list_elements);
2946 }
2947
2948 create_addrmap_from_index (per_objfile, map.get ());
2949
2950 per_bfd->index_table = std::move (map);
2951 per_bfd->using_index = 1;
2952 per_bfd->quick_file_names_table =
2953 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2954
2955 return 1;
2956 }
2957
2958 /* die_reader_func for dw2_get_file_names. */
2959
2960 static void
2961 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2962 struct die_info *comp_unit_die)
2963 {
2964 struct dwarf2_cu *cu = reader->cu;
2965 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2966 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2967 struct dwarf2_per_cu_data *lh_cu;
2968 struct attribute *attr;
2969 void **slot;
2970 struct quick_file_names *qfn;
2971
2972 gdb_assert (! this_cu->is_debug_types);
2973
2974 this_cu->v.quick->files_read = true;
2975 /* Our callers never want to match partial units -- instead they
2976 will match the enclosing full CU. */
2977 if (comp_unit_die->tag == DW_TAG_partial_unit)
2978 return;
2979
2980 lh_cu = this_cu;
2981 slot = NULL;
2982
2983 line_header_up lh;
2984 sect_offset line_offset {};
2985
2986 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2987 if (attr != nullptr && attr->form_is_unsigned ())
2988 {
2989 struct quick_file_names find_entry;
2990
2991 line_offset = (sect_offset) attr->as_unsigned ();
2992
2993 /* We may have already read in this line header (TU line header sharing).
2994 If we have we're done. */
2995 find_entry.hash.dwo_unit = cu->dwo_unit;
2996 find_entry.hash.line_sect_off = line_offset;
2997 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2998 &find_entry, INSERT);
2999 if (*slot != NULL)
3000 {
3001 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3002 return;
3003 }
3004
3005 lh = dwarf_decode_line_header (line_offset, cu);
3006 }
3007
3008 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
3009
3010 int offset = 0;
3011 if (!fnd.is_unknown ())
3012 ++offset;
3013 else if (lh == nullptr)
3014 return;
3015
3016 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3017 qfn->hash.dwo_unit = cu->dwo_unit;
3018 qfn->hash.line_sect_off = line_offset;
3019 /* There may not be a DW_AT_stmt_list. */
3020 if (slot != nullptr)
3021 *slot = qfn;
3022
3023 std::vector<const char *> include_names;
3024 if (lh != nullptr)
3025 {
3026 for (const auto &entry : lh->file_names ())
3027 {
3028 gdb::unique_xmalloc_ptr<char> name_holder;
3029 const char *include_name =
3030 compute_include_file_name (lh.get (), entry, fnd, &name_holder);
3031 if (include_name != nullptr)
3032 {
3033 include_name = per_objfile->objfile->intern (include_name);
3034 include_names.push_back (include_name);
3035 }
3036 }
3037 }
3038
3039 qfn->num_file_names = offset + include_names.size ();
3040 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
3041 qfn->file_names =
3042 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3043 qfn->num_file_names);
3044 if (offset != 0)
3045 qfn->file_names[0] = xstrdup (fnd.get_name ());
3046
3047 if (!include_names.empty ())
3048 memcpy (&qfn->file_names[offset], include_names.data (),
3049 include_names.size () * sizeof (const char *));
3050
3051 qfn->real_names = NULL;
3052
3053 lh_cu->v.quick->file_names = qfn;
3054 }
3055
3056 /* A helper for the "quick" functions which attempts to read the line
3057 table for THIS_CU. */
3058
3059 static struct quick_file_names *
3060 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3061 dwarf2_per_objfile *per_objfile)
3062 {
3063 /* This should never be called for TUs. */
3064 gdb_assert (! this_cu->is_debug_types);
3065 /* Nor type unit groups. */
3066 gdb_assert (! this_cu->type_unit_group_p ());
3067
3068 if (this_cu->v.quick->files_read)
3069 return this_cu->v.quick->file_names;
3070
3071 cutu_reader reader (this_cu, per_objfile);
3072 if (!reader.dummy_p)
3073 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3074
3075 return this_cu->v.quick->file_names;
3076 }
3077
3078 /* A helper for the "quick" functions which computes and caches the
3079 real path for a given file name from the line table. */
3080
3081 static const char *
3082 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3083 struct quick_file_names *qfn, int index)
3084 {
3085 if (qfn->real_names == NULL)
3086 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3087 qfn->num_file_names, const char *);
3088
3089 if (qfn->real_names[index] == NULL)
3090 {
3091 const char *dirname = nullptr;
3092
3093 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
3094 dirname = qfn->comp_dir;
3095
3096 gdb::unique_xmalloc_ptr<char> fullname;
3097 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
3098
3099 qfn->real_names[index] = fullname.release ();
3100 }
3101
3102 return qfn->real_names[index];
3103 }
3104
3105 struct symtab *
3106 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3107 {
3108 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3109 dwarf2_per_cu_data *dwarf_cu
3110 = per_objfile->per_bfd->all_comp_units.back ().get ();
3111 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3112
3113 if (cust == NULL)
3114 return NULL;
3115
3116 return cust->primary_filetab ();
3117 }
3118
3119 /* See read.h. */
3120
3121 void
3122 dwarf2_per_cu_data::free_cached_file_names ()
3123 {
3124 if (per_bfd == nullptr || !per_bfd->using_index || v.quick == nullptr)
3125 return;
3126
3127 struct quick_file_names *file_data = v.quick->file_names;
3128 if (file_data != nullptr && file_data->real_names != nullptr)
3129 {
3130 for (int i = 0; i < file_data->num_file_names; ++i)
3131 {
3132 xfree ((void *) file_data->real_names[i]);
3133 file_data->real_names[i] = nullptr;
3134 }
3135 }
3136 }
3137
3138 void
3139 dwarf2_base_index_functions::forget_cached_source_info
3140 (struct objfile *objfile)
3141 {
3142 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3143
3144 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
3145 per_cu->free_cached_file_names ();
3146 }
3147
3148 /* Struct used to manage iterating over all CUs looking for a symbol. */
3149
3150 struct dw2_symtab_iterator
3151 {
3152 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3153 dwarf2_per_objfile *per_objfile;
3154 /* If set, only look for symbols that match that block. Valid values are
3155 GLOBAL_BLOCK and STATIC_BLOCK. */
3156 gdb::optional<block_enum> block_index;
3157 /* The kind of symbol we're looking for. */
3158 domain_enum domain;
3159 /* The list of CUs from the index entry of the symbol,
3160 or NULL if not found. */
3161 offset_view vec;
3162 /* The next element in VEC to look at. */
3163 int next;
3164 /* The number of elements in VEC, or zero if there is no match. */
3165 int length;
3166 /* Have we seen a global version of the symbol?
3167 If so we can ignore all further global instances.
3168 This is to work around gold/15646, inefficient gold-generated
3169 indices. */
3170 int global_seen;
3171 };
3172
3173 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3174
3175 static void
3176 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3177 dwarf2_per_objfile *per_objfile,
3178 gdb::optional<block_enum> block_index,
3179 domain_enum domain, offset_type namei)
3180 {
3181 iter->per_objfile = per_objfile;
3182 iter->block_index = block_index;
3183 iter->domain = domain;
3184 iter->next = 0;
3185 iter->global_seen = 0;
3186 iter->vec = {};
3187 iter->length = 0;
3188
3189 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3190 /* index is NULL if OBJF_READNOW. */
3191 if (index == NULL)
3192 return;
3193
3194 gdb_assert (!index->symbol_name_slot_invalid (namei));
3195 offset_type vec_idx = index->symbol_vec_index (namei);
3196
3197 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3198 iter->length = iter->vec[0];
3199 }
3200
3201 /* Return the next matching CU or NULL if there are no more. */
3202
3203 static struct dwarf2_per_cu_data *
3204 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3205 {
3206 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3207
3208 for ( ; iter->next < iter->length; ++iter->next)
3209 {
3210 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3211 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3212 gdb_index_symbol_kind symbol_kind =
3213 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3214 /* Only check the symbol attributes if they're present.
3215 Indices prior to version 7 don't record them,
3216 and indices >= 7 may elide them for certain symbols
3217 (gold does this). */
3218 int attrs_valid =
3219 (per_objfile->per_bfd->index_table->version >= 7
3220 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3221
3222 /* Don't crash on bad data. */
3223 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3224 {
3225 complaint (_(".gdb_index entry has bad CU index"
3226 " [in module %s]"), objfile_name (per_objfile->objfile));
3227 continue;
3228 }
3229
3230 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3231
3232 /* Skip if already read in. */
3233 if (per_objfile->symtab_set_p (per_cu))
3234 continue;
3235
3236 /* Check static vs global. */
3237 if (attrs_valid)
3238 {
3239 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3240
3241 if (iter->block_index.has_value ())
3242 {
3243 bool want_static = *iter->block_index == STATIC_BLOCK;
3244
3245 if (is_static != want_static)
3246 continue;
3247 }
3248
3249 /* Work around gold/15646. */
3250 if (!is_static
3251 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3252 {
3253 if (iter->global_seen)
3254 continue;
3255
3256 iter->global_seen = 1;
3257 }
3258 }
3259
3260 /* Only check the symbol's kind if it has one. */
3261 if (attrs_valid)
3262 {
3263 switch (iter->domain)
3264 {
3265 case VAR_DOMAIN:
3266 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3267 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3268 /* Some types are also in VAR_DOMAIN. */
3269 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3270 continue;
3271 break;
3272 case STRUCT_DOMAIN:
3273 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3274 continue;
3275 break;
3276 case LABEL_DOMAIN:
3277 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3278 continue;
3279 break;
3280 case MODULE_DOMAIN:
3281 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3282 continue;
3283 break;
3284 default:
3285 break;
3286 }
3287 }
3288
3289 ++iter->next;
3290 return per_cu;
3291 }
3292
3293 return NULL;
3294 }
3295
3296 void
3297 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3298 bool print_bcache)
3299 {
3300 if (print_bcache)
3301 return;
3302
3303 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3304 int total = per_objfile->per_bfd->all_comp_units.size ();
3305 int count = 0;
3306
3307 for (int i = 0; i < total; ++i)
3308 {
3309 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3310
3311 if (!per_objfile->symtab_set_p (per_cu))
3312 ++count;
3313 }
3314 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3315 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3316 }
3317
3318 /* This dumps minimal information about the index.
3319 It is called via "mt print objfiles".
3320 One use is to verify .gdb_index has been loaded by the
3321 gdb.dwarf2/gdb-index.exp testcase. */
3322
3323 void
3324 dwarf2_gdb_index::dump (struct objfile *objfile)
3325 {
3326 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3327
3328 gdb_assert (per_objfile->per_bfd->using_index);
3329 gdb_printf (".gdb_index:");
3330 if (per_objfile->per_bfd->index_table != NULL)
3331 {
3332 gdb_printf (" version %d\n",
3333 per_objfile->per_bfd->index_table->version);
3334 }
3335 else
3336 gdb_printf (" faked for \"readnow\"\n");
3337 gdb_printf ("\n");
3338 }
3339
3340 void
3341 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3342 {
3343 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3344 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3345
3346 for (int i = 0; i < total_units; ++i)
3347 {
3348 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3349
3350 /* We don't want to directly expand a partial CU, because if we
3351 read it with the wrong language, then assertion failures can
3352 be triggered later on. See PR symtab/23010. So, tell
3353 dw2_instantiate_symtab to skip partial CUs -- any important
3354 partial CU will be read via DW_TAG_imported_unit anyway. */
3355 dw2_instantiate_symtab (per_cu, per_objfile, true);
3356 }
3357 }
3358
3359 static bool
3360 dw2_expand_symtabs_matching_symbol
3361 (mapped_index_base &index,
3362 const lookup_name_info &lookup_name_in,
3363 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3364 gdb::function_view<bool (offset_type)> match_callback,
3365 dwarf2_per_objfile *per_objfile);
3366
3367 static bool
3368 dw2_expand_symtabs_matching_one
3369 (dwarf2_per_cu_data *per_cu,
3370 dwarf2_per_objfile *per_objfile,
3371 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3372 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3373
3374 void
3375 dwarf2_gdb_index::expand_matching_symbols
3376 (struct objfile *objfile,
3377 const lookup_name_info &name, domain_enum domain,
3378 int global,
3379 symbol_compare_ftype *ordered_compare)
3380 {
3381 /* Used for Ada. */
3382 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3383
3384 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3385
3386 if (per_objfile->per_bfd->index_table != nullptr)
3387 {
3388 mapped_index &index = *per_objfile->per_bfd->index_table;
3389
3390 const char *match_name = name.ada ().lookup_name ().c_str ();
3391 auto matcher = [&] (const char *symname)
3392 {
3393 if (ordered_compare == nullptr)
3394 return true;
3395 return ordered_compare (symname, match_name) == 0;
3396 };
3397
3398 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3399 [&] (offset_type namei)
3400 {
3401 struct dw2_symtab_iterator iter;
3402 struct dwarf2_per_cu_data *per_cu;
3403
3404 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3405 namei);
3406 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3407 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3408 nullptr);
3409 return true;
3410 }, per_objfile);
3411 }
3412 else
3413 {
3414 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3415 proceed assuming all symtabs have been read in. */
3416 }
3417 }
3418
3419 /* Starting from a search name, return the string that finds the upper
3420 bound of all strings that start with SEARCH_NAME in a sorted name
3421 list. Returns the empty string to indicate that the upper bound is
3422 the end of the list. */
3423
3424 static std::string
3425 make_sort_after_prefix_name (const char *search_name)
3426 {
3427 /* When looking to complete "func", we find the upper bound of all
3428 symbols that start with "func" by looking for where we'd insert
3429 the closest string that would follow "func" in lexicographical
3430 order. Usually, that's "func"-with-last-character-incremented,
3431 i.e. "fund". Mind non-ASCII characters, though. Usually those
3432 will be UTF-8 multi-byte sequences, but we can't be certain.
3433 Especially mind the 0xff character, which is a valid character in
3434 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3435 rule out compilers allowing it in identifiers. Note that
3436 conveniently, strcmp/strcasecmp are specified to compare
3437 characters interpreted as unsigned char. So what we do is treat
3438 the whole string as a base 256 number composed of a sequence of
3439 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3440 to 0, and carries 1 to the following more-significant position.
3441 If the very first character in SEARCH_NAME ends up incremented
3442 and carries/overflows, then the upper bound is the end of the
3443 list. The string after the empty string is also the empty
3444 string.
3445
3446 Some examples of this operation:
3447
3448 SEARCH_NAME => "+1" RESULT
3449
3450 "abc" => "abd"
3451 "ab\xff" => "ac"
3452 "\xff" "a" "\xff" => "\xff" "b"
3453 "\xff" => ""
3454 "\xff\xff" => ""
3455 "" => ""
3456
3457 Then, with these symbols for example:
3458
3459 func
3460 func1
3461 fund
3462
3463 completing "func" looks for symbols between "func" and
3464 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3465 which finds "func" and "func1", but not "fund".
3466
3467 And with:
3468
3469 funcÿ (Latin1 'ÿ' [0xff])
3470 funcÿ1
3471 fund
3472
3473 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3474 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3475
3476 And with:
3477
3478 ÿÿ (Latin1 'ÿ' [0xff])
3479 ÿÿ1
3480
3481 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3482 the end of the list.
3483 */
3484 std::string after = search_name;
3485 while (!after.empty () && (unsigned char) after.back () == 0xff)
3486 after.pop_back ();
3487 if (!after.empty ())
3488 after.back () = (unsigned char) after.back () + 1;
3489 return after;
3490 }
3491
3492 /* See declaration. */
3493
3494 std::pair<std::vector<name_component>::const_iterator,
3495 std::vector<name_component>::const_iterator>
3496 mapped_index_base::find_name_components_bounds
3497 (const lookup_name_info &lookup_name_without_params, language lang,
3498 dwarf2_per_objfile *per_objfile) const
3499 {
3500 auto *name_cmp
3501 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3502
3503 const char *lang_name
3504 = lookup_name_without_params.language_lookup_name (lang);
3505
3506 /* Comparison function object for lower_bound that matches against a
3507 given symbol name. */
3508 auto lookup_compare_lower = [&] (const name_component &elem,
3509 const char *name)
3510 {
3511 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3512 const char *elem_name = elem_qualified + elem.name_offset;
3513 return name_cmp (elem_name, name) < 0;
3514 };
3515
3516 /* Comparison function object for upper_bound that matches against a
3517 given symbol name. */
3518 auto lookup_compare_upper = [&] (const char *name,
3519 const name_component &elem)
3520 {
3521 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3522 const char *elem_name = elem_qualified + elem.name_offset;
3523 return name_cmp (name, elem_name) < 0;
3524 };
3525
3526 auto begin = this->name_components.begin ();
3527 auto end = this->name_components.end ();
3528
3529 /* Find the lower bound. */
3530 auto lower = [&] ()
3531 {
3532 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3533 return begin;
3534 else
3535 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3536 } ();
3537
3538 /* Find the upper bound. */
3539 auto upper = [&] ()
3540 {
3541 if (lookup_name_without_params.completion_mode ())
3542 {
3543 /* In completion mode, we want UPPER to point past all
3544 symbols names that have the same prefix. I.e., with
3545 these symbols, and completing "func":
3546
3547 function << lower bound
3548 function1
3549 other_function << upper bound
3550
3551 We find the upper bound by looking for the insertion
3552 point of "func"-with-last-character-incremented,
3553 i.e. "fund". */
3554 std::string after = make_sort_after_prefix_name (lang_name);
3555 if (after.empty ())
3556 return end;
3557 return std::lower_bound (lower, end, after.c_str (),
3558 lookup_compare_lower);
3559 }
3560 else
3561 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3562 } ();
3563
3564 return {lower, upper};
3565 }
3566
3567 /* See declaration. */
3568
3569 void
3570 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3571 {
3572 if (!this->name_components.empty ())
3573 return;
3574
3575 this->name_components_casing = case_sensitivity;
3576 auto *name_cmp
3577 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3578
3579 /* The code below only knows how to break apart components of C++
3580 symbol names (and other languages that use '::' as
3581 namespace/module separator) and Ada symbol names. */
3582 auto count = this->symbol_name_count ();
3583 for (offset_type idx = 0; idx < count; idx++)
3584 {
3585 if (this->symbol_name_slot_invalid (idx))
3586 continue;
3587
3588 const char *name = this->symbol_name_at (idx, per_objfile);
3589
3590 /* Add each name component to the name component table. */
3591 unsigned int previous_len = 0;
3592
3593 if (strstr (name, "::") != nullptr)
3594 {
3595 for (unsigned int current_len = cp_find_first_component (name);
3596 name[current_len] != '\0';
3597 current_len += cp_find_first_component (name + current_len))
3598 {
3599 gdb_assert (name[current_len] == ':');
3600 this->name_components.push_back ({previous_len, idx});
3601 /* Skip the '::'. */
3602 current_len += 2;
3603 previous_len = current_len;
3604 }
3605 }
3606 else
3607 {
3608 /* Handle the Ada encoded (aka mangled) form here. */
3609 for (const char *iter = strstr (name, "__");
3610 iter != nullptr;
3611 iter = strstr (iter, "__"))
3612 {
3613 this->name_components.push_back ({previous_len, idx});
3614 iter += 2;
3615 previous_len = iter - name;
3616 }
3617 }
3618
3619 this->name_components.push_back ({previous_len, idx});
3620 }
3621
3622 /* Sort name_components elements by name. */
3623 auto name_comp_compare = [&] (const name_component &left,
3624 const name_component &right)
3625 {
3626 const char *left_qualified
3627 = this->symbol_name_at (left.idx, per_objfile);
3628 const char *right_qualified
3629 = this->symbol_name_at (right.idx, per_objfile);
3630
3631 const char *left_name = left_qualified + left.name_offset;
3632 const char *right_name = right_qualified + right.name_offset;
3633
3634 return name_cmp (left_name, right_name) < 0;
3635 };
3636
3637 std::sort (this->name_components.begin (),
3638 this->name_components.end (),
3639 name_comp_compare);
3640 }
3641
3642 /* Helper for dw2_expand_symtabs_matching that works with a
3643 mapped_index_base instead of the containing objfile. This is split
3644 to a separate function in order to be able to unit test the
3645 name_components matching using a mock mapped_index_base. For each
3646 symbol name that matches, calls MATCH_CALLBACK, passing it the
3647 symbol's index in the mapped_index_base symbol table. */
3648
3649 static bool
3650 dw2_expand_symtabs_matching_symbol
3651 (mapped_index_base &index,
3652 const lookup_name_info &lookup_name_in,
3653 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3654 gdb::function_view<bool (offset_type)> match_callback,
3655 dwarf2_per_objfile *per_objfile)
3656 {
3657 lookup_name_info lookup_name_without_params
3658 = lookup_name_in.make_ignore_params ();
3659
3660 /* Build the symbol name component sorted vector, if we haven't
3661 yet. */
3662 index.build_name_components (per_objfile);
3663
3664 /* The same symbol may appear more than once in the range though.
3665 E.g., if we're looking for symbols that complete "w", and we have
3666 a symbol named "w1::w2", we'll find the two name components for
3667 that same symbol in the range. To be sure we only call the
3668 callback once per symbol, we first collect the symbol name
3669 indexes that matched in a temporary vector and ignore
3670 duplicates. */
3671 std::vector<offset_type> matches;
3672
3673 struct name_and_matcher
3674 {
3675 symbol_name_matcher_ftype *matcher;
3676 const char *name;
3677
3678 bool operator== (const name_and_matcher &other) const
3679 {
3680 return matcher == other.matcher && strcmp (name, other.name) == 0;
3681 }
3682 };
3683
3684 /* A vector holding all the different symbol name matchers, for all
3685 languages. */
3686 std::vector<name_and_matcher> matchers;
3687
3688 for (int i = 0; i < nr_languages; i++)
3689 {
3690 enum language lang_e = (enum language) i;
3691
3692 const language_defn *lang = language_def (lang_e);
3693 symbol_name_matcher_ftype *name_matcher
3694 = lang->get_symbol_name_matcher (lookup_name_without_params);
3695
3696 name_and_matcher key {
3697 name_matcher,
3698 lookup_name_without_params.language_lookup_name (lang_e)
3699 };
3700
3701 /* Don't insert the same comparison routine more than once.
3702 Note that we do this linear walk. This is not a problem in
3703 practice because the number of supported languages is
3704 low. */
3705 if (std::find (matchers.begin (), matchers.end (), key)
3706 != matchers.end ())
3707 continue;
3708 matchers.push_back (std::move (key));
3709
3710 auto bounds
3711 = index.find_name_components_bounds (lookup_name_without_params,
3712 lang_e, per_objfile);
3713
3714 /* Now for each symbol name in range, check to see if we have a name
3715 match, and if so, call the MATCH_CALLBACK callback. */
3716
3717 for (; bounds.first != bounds.second; ++bounds.first)
3718 {
3719 const char *qualified
3720 = index.symbol_name_at (bounds.first->idx, per_objfile);
3721
3722 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3723 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3724 continue;
3725
3726 matches.push_back (bounds.first->idx);
3727 }
3728 }
3729
3730 std::sort (matches.begin (), matches.end ());
3731
3732 /* Finally call the callback, once per match. */
3733 ULONGEST prev = -1;
3734 bool result = true;
3735 for (offset_type idx : matches)
3736 {
3737 if (prev != idx)
3738 {
3739 if (!match_callback (idx))
3740 {
3741 result = false;
3742 break;
3743 }
3744 prev = idx;
3745 }
3746 }
3747
3748 /* Above we use a type wider than idx's for 'prev', since 0 and
3749 (offset_type)-1 are both possible values. */
3750 static_assert (sizeof (prev) > sizeof (offset_type), "");
3751
3752 return result;
3753 }
3754
3755 #if GDB_SELF_TEST
3756
3757 namespace selftests { namespace dw2_expand_symtabs_matching {
3758
3759 /* A mock .gdb_index/.debug_names-like name index table, enough to
3760 exercise dw2_expand_symtabs_matching_symbol, which works with the
3761 mapped_index_base interface. Builds an index from the symbol list
3762 passed as parameter to the constructor. */
3763 class mock_mapped_index : public mapped_index_base
3764 {
3765 public:
3766 mock_mapped_index (gdb::array_view<const char *> symbols)
3767 : m_symbol_table (symbols)
3768 {}
3769
3770 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3771
3772 /* Return the number of names in the symbol table. */
3773 size_t symbol_name_count () const override
3774 {
3775 return m_symbol_table.size ();
3776 }
3777
3778 /* Get the name of the symbol at IDX in the symbol table. */
3779 const char *symbol_name_at
3780 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3781 {
3782 return m_symbol_table[idx];
3783 }
3784
3785 private:
3786 gdb::array_view<const char *> m_symbol_table;
3787 };
3788
3789 /* Convenience function that converts a NULL pointer to a "<null>"
3790 string, to pass to print routines. */
3791
3792 static const char *
3793 string_or_null (const char *str)
3794 {
3795 return str != NULL ? str : "<null>";
3796 }
3797
3798 /* Check if a lookup_name_info built from
3799 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3800 index. EXPECTED_LIST is the list of expected matches, in expected
3801 matching order. If no match expected, then an empty list is
3802 specified. Returns true on success. On failure prints a warning
3803 indicating the file:line that failed, and returns false. */
3804
3805 static bool
3806 check_match (const char *file, int line,
3807 mock_mapped_index &mock_index,
3808 const char *name, symbol_name_match_type match_type,
3809 bool completion_mode,
3810 std::initializer_list<const char *> expected_list,
3811 dwarf2_per_objfile *per_objfile)
3812 {
3813 lookup_name_info lookup_name (name, match_type, completion_mode);
3814
3815 bool matched = true;
3816
3817 auto mismatch = [&] (const char *expected_str,
3818 const char *got)
3819 {
3820 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3821 "expected=\"%s\", got=\"%s\"\n"),
3822 file, line,
3823 (match_type == symbol_name_match_type::FULL
3824 ? "FULL" : "WILD"),
3825 name, string_or_null (expected_str), string_or_null (got));
3826 matched = false;
3827 };
3828
3829 auto expected_it = expected_list.begin ();
3830 auto expected_end = expected_list.end ();
3831
3832 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3833 nullptr,
3834 [&] (offset_type idx)
3835 {
3836 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3837 const char *expected_str
3838 = expected_it == expected_end ? NULL : *expected_it++;
3839
3840 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3841 mismatch (expected_str, matched_name);
3842 return true;
3843 }, per_objfile);
3844
3845 const char *expected_str
3846 = expected_it == expected_end ? NULL : *expected_it++;
3847 if (expected_str != NULL)
3848 mismatch (expected_str, NULL);
3849
3850 return matched;
3851 }
3852
3853 /* The symbols added to the mock mapped_index for testing (in
3854 canonical form). */
3855 static const char *test_symbols[] = {
3856 "function",
3857 "std::bar",
3858 "std::zfunction",
3859 "std::zfunction2",
3860 "w1::w2",
3861 "ns::foo<char*>",
3862 "ns::foo<int>",
3863 "ns::foo<long>",
3864 "ns2::tmpl<int>::foo2",
3865 "(anonymous namespace)::A::B::C",
3866
3867 /* These are used to check that the increment-last-char in the
3868 matching algorithm for completion doesn't match "t1_fund" when
3869 completing "t1_func". */
3870 "t1_func",
3871 "t1_func1",
3872 "t1_fund",
3873 "t1_fund1",
3874
3875 /* A UTF-8 name with multi-byte sequences to make sure that
3876 cp-name-parser understands this as a single identifier ("função"
3877 is "function" in PT). */
3878 u8"u8função",
3879
3880 /* \377 (0xff) is Latin1 'ÿ'. */
3881 "yfunc\377",
3882
3883 /* \377 (0xff) is Latin1 'ÿ'. */
3884 "\377",
3885 "\377\377123",
3886
3887 /* A name with all sorts of complications. Starts with "z" to make
3888 it easier for the completion tests below. */
3889 #define Z_SYM_NAME \
3890 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3891 "::tuple<(anonymous namespace)::ui*, " \
3892 "std::default_delete<(anonymous namespace)::ui>, void>"
3893
3894 Z_SYM_NAME
3895 };
3896
3897 /* Returns true if the mapped_index_base::find_name_component_bounds
3898 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3899 in completion mode. */
3900
3901 static bool
3902 check_find_bounds_finds (mapped_index_base &index,
3903 const char *search_name,
3904 gdb::array_view<const char *> expected_syms,
3905 dwarf2_per_objfile *per_objfile)
3906 {
3907 lookup_name_info lookup_name (search_name,
3908 symbol_name_match_type::FULL, true);
3909
3910 auto bounds = index.find_name_components_bounds (lookup_name,
3911 language_cplus,
3912 per_objfile);
3913
3914 size_t distance = std::distance (bounds.first, bounds.second);
3915 if (distance != expected_syms.size ())
3916 return false;
3917
3918 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3919 {
3920 auto nc_elem = bounds.first + exp_elem;
3921 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3922 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3923 return false;
3924 }
3925
3926 return true;
3927 }
3928
3929 /* Test the lower-level mapped_index::find_name_component_bounds
3930 method. */
3931
3932 static void
3933 test_mapped_index_find_name_component_bounds ()
3934 {
3935 mock_mapped_index mock_index (test_symbols);
3936
3937 mock_index.build_name_components (NULL /* per_objfile */);
3938
3939 /* Test the lower-level mapped_index::find_name_component_bounds
3940 method in completion mode. */
3941 {
3942 static const char *expected_syms[] = {
3943 "t1_func",
3944 "t1_func1",
3945 };
3946
3947 SELF_CHECK (check_find_bounds_finds
3948 (mock_index, "t1_func", expected_syms,
3949 NULL /* per_objfile */));
3950 }
3951
3952 /* Check that the increment-last-char in the name matching algorithm
3953 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3954 {
3955 static const char *expected_syms1[] = {
3956 "\377",
3957 "\377\377123",
3958 };
3959 SELF_CHECK (check_find_bounds_finds
3960 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3961
3962 static const char *expected_syms2[] = {
3963 "\377\377123",
3964 };
3965 SELF_CHECK (check_find_bounds_finds
3966 (mock_index, "\377\377", expected_syms2,
3967 NULL /* per_objfile */));
3968 }
3969 }
3970
3971 /* Test dw2_expand_symtabs_matching_symbol. */
3972
3973 static void
3974 test_dw2_expand_symtabs_matching_symbol ()
3975 {
3976 mock_mapped_index mock_index (test_symbols);
3977
3978 /* We let all tests run until the end even if some fails, for debug
3979 convenience. */
3980 bool any_mismatch = false;
3981
3982 /* Create the expected symbols list (an initializer_list). Needed
3983 because lists have commas, and we need to pass them to CHECK,
3984 which is a macro. */
3985 #define EXPECT(...) { __VA_ARGS__ }
3986
3987 /* Wrapper for check_match that passes down the current
3988 __FILE__/__LINE__. */
3989 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3990 any_mismatch |= !check_match (__FILE__, __LINE__, \
3991 mock_index, \
3992 NAME, MATCH_TYPE, COMPLETION_MODE, \
3993 EXPECTED_LIST, NULL)
3994
3995 /* Identity checks. */
3996 for (const char *sym : test_symbols)
3997 {
3998 /* Should be able to match all existing symbols. */
3999 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4000 EXPECT (sym));
4001
4002 /* Should be able to match all existing symbols with
4003 parameters. */
4004 std::string with_params = std::string (sym) + "(int)";
4005 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4006 EXPECT (sym));
4007
4008 /* Should be able to match all existing symbols with
4009 parameters and qualifiers. */
4010 with_params = std::string (sym) + " ( int ) const";
4011 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4012 EXPECT (sym));
4013
4014 /* This should really find sym, but cp-name-parser.y doesn't
4015 know about lvalue/rvalue qualifiers yet. */
4016 with_params = std::string (sym) + " ( int ) &&";
4017 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4018 {});
4019 }
4020
4021 /* Check that the name matching algorithm for completion doesn't get
4022 confused with Latin1 'ÿ' / 0xff. */
4023 {
4024 static const char str[] = "\377";
4025 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4026 EXPECT ("\377", "\377\377123"));
4027 }
4028
4029 /* Check that the increment-last-char in the matching algorithm for
4030 completion doesn't match "t1_fund" when completing "t1_func". */
4031 {
4032 static const char str[] = "t1_func";
4033 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4034 EXPECT ("t1_func", "t1_func1"));
4035 }
4036
4037 /* Check that completion mode works at each prefix of the expected
4038 symbol name. */
4039 {
4040 static const char str[] = "function(int)";
4041 size_t len = strlen (str);
4042 std::string lookup;
4043
4044 for (size_t i = 1; i < len; i++)
4045 {
4046 lookup.assign (str, i);
4047 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4048 EXPECT ("function"));
4049 }
4050 }
4051
4052 /* While "w" is a prefix of both components, the match function
4053 should still only be called once. */
4054 {
4055 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4056 EXPECT ("w1::w2"));
4057 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4058 EXPECT ("w1::w2"));
4059 }
4060
4061 /* Same, with a "complicated" symbol. */
4062 {
4063 static const char str[] = Z_SYM_NAME;
4064 size_t len = strlen (str);
4065 std::string lookup;
4066
4067 for (size_t i = 1; i < len; i++)
4068 {
4069 lookup.assign (str, i);
4070 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4071 EXPECT (Z_SYM_NAME));
4072 }
4073 }
4074
4075 /* In FULL mode, an incomplete symbol doesn't match. */
4076 {
4077 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4078 {});
4079 }
4080
4081 /* A complete symbol with parameters matches any overload, since the
4082 index has no overload info. */
4083 {
4084 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4085 EXPECT ("std::zfunction", "std::zfunction2"));
4086 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4087 EXPECT ("std::zfunction", "std::zfunction2"));
4088 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4089 EXPECT ("std::zfunction", "std::zfunction2"));
4090 }
4091
4092 /* Check that whitespace is ignored appropriately. A symbol with a
4093 template argument list. */
4094 {
4095 static const char expected[] = "ns::foo<int>";
4096 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4097 EXPECT (expected));
4098 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4099 EXPECT (expected));
4100 }
4101
4102 /* Check that whitespace is ignored appropriately. A symbol with a
4103 template argument list that includes a pointer. */
4104 {
4105 static const char expected[] = "ns::foo<char*>";
4106 /* Try both completion and non-completion modes. */
4107 static const bool completion_mode[2] = {false, true};
4108 for (size_t i = 0; i < 2; i++)
4109 {
4110 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4111 completion_mode[i], EXPECT (expected));
4112 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4113 completion_mode[i], EXPECT (expected));
4114
4115 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4116 completion_mode[i], EXPECT (expected));
4117 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4118 completion_mode[i], EXPECT (expected));
4119 }
4120 }
4121
4122 {
4123 /* Check method qualifiers are ignored. */
4124 static const char expected[] = "ns::foo<char*>";
4125 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4126 symbol_name_match_type::FULL, true, EXPECT (expected));
4127 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4128 symbol_name_match_type::FULL, true, EXPECT (expected));
4129 CHECK_MATCH ("foo < char * > ( int ) const",
4130 symbol_name_match_type::WILD, true, EXPECT (expected));
4131 CHECK_MATCH ("foo < char * > ( int ) &&",
4132 symbol_name_match_type::WILD, true, EXPECT (expected));
4133 }
4134
4135 /* Test lookup names that don't match anything. */
4136 {
4137 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4138 {});
4139
4140 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4141 {});
4142 }
4143
4144 /* Some wild matching tests, exercising "(anonymous namespace)",
4145 which should not be confused with a parameter list. */
4146 {
4147 static const char *syms[] = {
4148 "A::B::C",
4149 "B::C",
4150 "C",
4151 "A :: B :: C ( int )",
4152 "B :: C ( int )",
4153 "C ( int )",
4154 };
4155
4156 for (const char *s : syms)
4157 {
4158 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4159 EXPECT ("(anonymous namespace)::A::B::C"));
4160 }
4161 }
4162
4163 {
4164 static const char expected[] = "ns2::tmpl<int>::foo2";
4165 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4166 EXPECT (expected));
4167 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4168 EXPECT (expected));
4169 }
4170
4171 SELF_CHECK (!any_mismatch);
4172
4173 #undef EXPECT
4174 #undef CHECK_MATCH
4175 }
4176
4177 static void
4178 run_test ()
4179 {
4180 test_mapped_index_find_name_component_bounds ();
4181 test_dw2_expand_symtabs_matching_symbol ();
4182 }
4183
4184 }} // namespace selftests::dw2_expand_symtabs_matching
4185
4186 #endif /* GDB_SELF_TEST */
4187
4188 /* If FILE_MATCHER is NULL or if PER_CU has
4189 dwarf2_per_cu_quick_data::MARK set (see
4190 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4191 EXPANSION_NOTIFY on it. */
4192
4193 static bool
4194 dw2_expand_symtabs_matching_one
4195 (dwarf2_per_cu_data *per_cu,
4196 dwarf2_per_objfile *per_objfile,
4197 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4198 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4199 {
4200 if (file_matcher == NULL || per_cu->v.quick->mark)
4201 {
4202 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4203
4204 compunit_symtab *symtab
4205 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4206 gdb_assert (symtab != nullptr);
4207
4208 if (expansion_notify != NULL && symtab_was_null)
4209 return expansion_notify (symtab);
4210 }
4211 return true;
4212 }
4213
4214 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4215 matched, to expand corresponding CUs that were marked. IDX is the
4216 index of the symbol name that matched. */
4217
4218 static bool
4219 dw2_expand_marked_cus
4220 (dwarf2_per_objfile *per_objfile, offset_type idx,
4221 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4222 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4223 block_search_flags search_flags,
4224 search_domain kind)
4225 {
4226 offset_type vec_len, vec_idx;
4227 bool global_seen = false;
4228 mapped_index &index = *per_objfile->per_bfd->index_table;
4229
4230 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4231 vec_len = vec[0];
4232 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4233 {
4234 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4235 /* This value is only valid for index versions >= 7. */
4236 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4237 gdb_index_symbol_kind symbol_kind =
4238 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4239 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4240 /* Only check the symbol attributes if they're present.
4241 Indices prior to version 7 don't record them,
4242 and indices >= 7 may elide them for certain symbols
4243 (gold does this). */
4244 int attrs_valid =
4245 (index.version >= 7
4246 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4247
4248 /* Work around gold/15646. */
4249 if (attrs_valid
4250 && !is_static
4251 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4252 {
4253 if (global_seen)
4254 continue;
4255
4256 global_seen = true;
4257 }
4258
4259 /* Only check the symbol's kind if it has one. */
4260 if (attrs_valid)
4261 {
4262 if (is_static)
4263 {
4264 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4265 continue;
4266 }
4267 else
4268 {
4269 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4270 continue;
4271 }
4272
4273 switch (kind)
4274 {
4275 case VARIABLES_DOMAIN:
4276 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4277 continue;
4278 break;
4279 case FUNCTIONS_DOMAIN:
4280 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4281 continue;
4282 break;
4283 case TYPES_DOMAIN:
4284 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4285 continue;
4286 break;
4287 case MODULES_DOMAIN:
4288 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4289 continue;
4290 break;
4291 default:
4292 break;
4293 }
4294 }
4295
4296 /* Don't crash on bad data. */
4297 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4298 {
4299 complaint (_(".gdb_index entry has bad CU index"
4300 " [in module %s]"), objfile_name (per_objfile->objfile));
4301 continue;
4302 }
4303
4304 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4305 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4306 expansion_notify))
4307 return false;
4308 }
4309
4310 return true;
4311 }
4312
4313 /* If FILE_MATCHER is non-NULL, set all the
4314 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4315 that match FILE_MATCHER. */
4316
4317 static void
4318 dw_expand_symtabs_matching_file_matcher
4319 (dwarf2_per_objfile *per_objfile,
4320 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4321 {
4322 if (file_matcher == NULL)
4323 return;
4324
4325 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4326 htab_eq_pointer,
4327 NULL, xcalloc, xfree));
4328 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4329 htab_eq_pointer,
4330 NULL, xcalloc, xfree));
4331
4332 /* The rule is CUs specify all the files, including those used by
4333 any TU, so there's no need to scan TUs here. */
4334
4335 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4336 {
4337 QUIT;
4338
4339 if (per_cu->is_debug_types)
4340 continue;
4341 per_cu->v.quick->mark = 0;
4342
4343 /* We only need to look at symtabs not already expanded. */
4344 if (per_objfile->symtab_set_p (per_cu.get ()))
4345 continue;
4346
4347 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4348 per_objfile);
4349 if (file_data == NULL)
4350 continue;
4351
4352 if (htab_find (visited_not_found.get (), file_data) != NULL)
4353 continue;
4354 else if (htab_find (visited_found.get (), file_data) != NULL)
4355 {
4356 per_cu->v.quick->mark = 1;
4357 continue;
4358 }
4359
4360 for (int j = 0; j < file_data->num_file_names; ++j)
4361 {
4362 const char *this_real_name;
4363
4364 if (file_matcher (file_data->file_names[j], false))
4365 {
4366 per_cu->v.quick->mark = 1;
4367 break;
4368 }
4369
4370 /* Before we invoke realpath, which can get expensive when many
4371 files are involved, do a quick comparison of the basenames. */
4372 if (!basenames_may_differ
4373 && !file_matcher (lbasename (file_data->file_names[j]),
4374 true))
4375 continue;
4376
4377 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4378 if (file_matcher (this_real_name, false))
4379 {
4380 per_cu->v.quick->mark = 1;
4381 break;
4382 }
4383 }
4384
4385 void **slot = htab_find_slot (per_cu->v.quick->mark
4386 ? visited_found.get ()
4387 : visited_not_found.get (),
4388 file_data, INSERT);
4389 *slot = file_data;
4390 }
4391 }
4392
4393 bool
4394 dwarf2_gdb_index::expand_symtabs_matching
4395 (struct objfile *objfile,
4396 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4397 const lookup_name_info *lookup_name,
4398 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4399 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4400 block_search_flags search_flags,
4401 domain_enum domain,
4402 enum search_domain kind)
4403 {
4404 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4405
4406 /* index_table is NULL if OBJF_READNOW. */
4407 if (!per_objfile->per_bfd->index_table)
4408 return true;
4409
4410 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4411
4412 /* This invariant is documented in quick-functions.h. */
4413 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4414 if (lookup_name == nullptr)
4415 {
4416 for (dwarf2_per_cu_data *per_cu
4417 : all_comp_units_range (per_objfile->per_bfd))
4418 {
4419 QUIT;
4420
4421 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4422 file_matcher,
4423 expansion_notify))
4424 return false;
4425 }
4426 return true;
4427 }
4428
4429 mapped_index &index = *per_objfile->per_bfd->index_table;
4430
4431 bool result
4432 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4433 symbol_matcher,
4434 [&] (offset_type idx)
4435 {
4436 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4437 expansion_notify, search_flags, kind))
4438 return false;
4439 return true;
4440 }, per_objfile);
4441
4442 return result;
4443 }
4444
4445 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4446 symtab. */
4447
4448 static struct compunit_symtab *
4449 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4450 CORE_ADDR pc)
4451 {
4452 int i;
4453
4454 if (cust->blockvector () != nullptr
4455 && blockvector_contains_pc (cust->blockvector (), pc))
4456 return cust;
4457
4458 if (cust->includes == NULL)
4459 return NULL;
4460
4461 for (i = 0; cust->includes[i]; ++i)
4462 {
4463 struct compunit_symtab *s = cust->includes[i];
4464
4465 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4466 if (s != NULL)
4467 return s;
4468 }
4469
4470 return NULL;
4471 }
4472
4473 struct compunit_symtab *
4474 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4475 (struct objfile *objfile,
4476 struct bound_minimal_symbol msymbol,
4477 CORE_ADDR pc,
4478 struct obj_section *section,
4479 int warn_if_readin)
4480 {
4481 struct dwarf2_per_cu_data *data;
4482 struct compunit_symtab *result;
4483
4484 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4485 if (per_objfile->per_bfd->index_addrmap == nullptr)
4486 return NULL;
4487
4488 CORE_ADDR baseaddr = objfile->text_section_offset ();
4489 data = ((struct dwarf2_per_cu_data *)
4490 addrmap_find (per_objfile->per_bfd->index_addrmap,
4491 pc - baseaddr));
4492 if (!data)
4493 return NULL;
4494
4495 if (warn_if_readin && per_objfile->symtab_set_p (data))
4496 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4497 paddress (objfile->arch (), pc));
4498
4499 result = recursively_find_pc_sect_compunit_symtab
4500 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4501
4502 gdb_assert (result != NULL);
4503 return result;
4504 }
4505
4506 void
4507 dwarf2_base_index_functions::map_symbol_filenames
4508 (struct objfile *objfile,
4509 gdb::function_view<symbol_filename_ftype> fun,
4510 bool need_fullname)
4511 {
4512 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4513
4514 /* Use caches to ensure we only call FUN once for each filename. */
4515 filename_seen_cache filenames_cache;
4516 std::unordered_set<quick_file_names *> qfn_cache;
4517
4518 /* The rule is CUs specify all the files, including those used by any TU,
4519 so there's no need to scan TUs here. We can ignore file names coming
4520 from already-expanded CUs. It is possible that an expanded CU might
4521 reuse the file names data from a currently unexpanded CU, in this
4522 case we don't want to report the files from the unexpanded CU. */
4523
4524 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4525 {
4526 if (!per_cu->is_debug_types
4527 && per_objfile->symtab_set_p (per_cu.get ()))
4528 {
4529 if (per_cu->v.quick->file_names != nullptr)
4530 qfn_cache.insert (per_cu->v.quick->file_names);
4531 }
4532 }
4533
4534 for (dwarf2_per_cu_data *per_cu
4535 : all_comp_units_range (per_objfile->per_bfd))
4536 {
4537 /* We only need to look at symtabs not already expanded. */
4538 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4539 continue;
4540
4541 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4542 if (file_data == nullptr
4543 || qfn_cache.find (file_data) != qfn_cache.end ())
4544 continue;
4545
4546 for (int j = 0; j < file_data->num_file_names; ++j)
4547 {
4548 const char *filename = file_data->file_names[j];
4549 const char *key = filename;
4550 const char *fullname = nullptr;
4551
4552 if (need_fullname)
4553 {
4554 fullname = dw2_get_real_path (per_objfile, file_data, j);
4555 key = fullname;
4556 }
4557
4558 if (!filenames_cache.seen (key))
4559 fun (filename, fullname);
4560 }
4561 }
4562 }
4563
4564 bool
4565 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4566 {
4567 return true;
4568 }
4569
4570 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4571
4572 bool
4573 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4574 {
4575 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4576
4577 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4578 {
4579 /* Is this already expanded? */
4580 if (per_objfile->symtab_set_p (per_cu.get ()))
4581 continue;
4582
4583 /* It has not yet been expanded. */
4584 return true;
4585 }
4586
4587 return false;
4588 }
4589
4590 /* DWARF-5 debug_names reader. */
4591
4592 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4593 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4594
4595 /* A helper function that reads the .debug_names section in SECTION
4596 and fills in MAP. FILENAME is the name of the file containing the
4597 section; it is used for error reporting.
4598
4599 Returns true if all went well, false otherwise. */
4600
4601 static bool
4602 read_debug_names_from_section (struct objfile *objfile,
4603 const char *filename,
4604 struct dwarf2_section_info *section,
4605 mapped_debug_names &map)
4606 {
4607 if (section->empty ())
4608 return false;
4609
4610 /* Older elfutils strip versions could keep the section in the main
4611 executable while splitting it for the separate debug info file. */
4612 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4613 return false;
4614
4615 section->read (objfile);
4616
4617 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4618
4619 const gdb_byte *addr = section->buffer;
4620
4621 bfd *const abfd = section->get_bfd_owner ();
4622
4623 unsigned int bytes_read;
4624 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4625 addr += bytes_read;
4626
4627 map.dwarf5_is_dwarf64 = bytes_read != 4;
4628 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4629 if (bytes_read + length != section->size)
4630 {
4631 /* There may be multiple per-CU indices. */
4632 warning (_("Section .debug_names in %s length %s does not match "
4633 "section length %s, ignoring .debug_names."),
4634 filename, plongest (bytes_read + length),
4635 pulongest (section->size));
4636 return false;
4637 }
4638
4639 /* The version number. */
4640 uint16_t version = read_2_bytes (abfd, addr);
4641 addr += 2;
4642 if (version != 5)
4643 {
4644 warning (_("Section .debug_names in %s has unsupported version %d, "
4645 "ignoring .debug_names."),
4646 filename, version);
4647 return false;
4648 }
4649
4650 /* Padding. */
4651 uint16_t padding = read_2_bytes (abfd, addr);
4652 addr += 2;
4653 if (padding != 0)
4654 {
4655 warning (_("Section .debug_names in %s has unsupported padding %d, "
4656 "ignoring .debug_names."),
4657 filename, padding);
4658 return false;
4659 }
4660
4661 /* comp_unit_count - The number of CUs in the CU list. */
4662 map.cu_count = read_4_bytes (abfd, addr);
4663 addr += 4;
4664
4665 /* local_type_unit_count - The number of TUs in the local TU
4666 list. */
4667 map.tu_count = read_4_bytes (abfd, addr);
4668 addr += 4;
4669
4670 /* foreign_type_unit_count - The number of TUs in the foreign TU
4671 list. */
4672 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4673 addr += 4;
4674 if (foreign_tu_count != 0)
4675 {
4676 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4677 "ignoring .debug_names."),
4678 filename, static_cast<unsigned long> (foreign_tu_count));
4679 return false;
4680 }
4681
4682 /* bucket_count - The number of hash buckets in the hash lookup
4683 table. */
4684 map.bucket_count = read_4_bytes (abfd, addr);
4685 addr += 4;
4686
4687 /* name_count - The number of unique names in the index. */
4688 map.name_count = read_4_bytes (abfd, addr);
4689 addr += 4;
4690
4691 /* abbrev_table_size - The size in bytes of the abbreviations
4692 table. */
4693 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4694 addr += 4;
4695
4696 /* augmentation_string_size - The size in bytes of the augmentation
4697 string. This value is rounded up to a multiple of 4. */
4698 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4699 addr += 4;
4700 map.augmentation_is_gdb = ((augmentation_string_size
4701 == sizeof (dwarf5_augmentation))
4702 && memcmp (addr, dwarf5_augmentation,
4703 sizeof (dwarf5_augmentation)) == 0);
4704 augmentation_string_size += (-augmentation_string_size) & 3;
4705 addr += augmentation_string_size;
4706
4707 /* List of CUs */
4708 map.cu_table_reordered = addr;
4709 addr += map.cu_count * map.offset_size;
4710
4711 /* List of Local TUs */
4712 map.tu_table_reordered = addr;
4713 addr += map.tu_count * map.offset_size;
4714
4715 /* Hash Lookup Table */
4716 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4717 addr += map.bucket_count * 4;
4718 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4719 addr += map.name_count * 4;
4720
4721 /* Name Table */
4722 map.name_table_string_offs_reordered = addr;
4723 addr += map.name_count * map.offset_size;
4724 map.name_table_entry_offs_reordered = addr;
4725 addr += map.name_count * map.offset_size;
4726
4727 const gdb_byte *abbrev_table_start = addr;
4728 for (;;)
4729 {
4730 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4731 addr += bytes_read;
4732 if (index_num == 0)
4733 break;
4734
4735 const auto insertpair
4736 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4737 if (!insertpair.second)
4738 {
4739 warning (_("Section .debug_names in %s has duplicate index %s, "
4740 "ignoring .debug_names."),
4741 filename, pulongest (index_num));
4742 return false;
4743 }
4744 mapped_debug_names::index_val &indexval = insertpair.first->second;
4745 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4746 addr += bytes_read;
4747
4748 for (;;)
4749 {
4750 mapped_debug_names::index_val::attr attr;
4751 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4752 addr += bytes_read;
4753 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4754 addr += bytes_read;
4755 if (attr.form == DW_FORM_implicit_const)
4756 {
4757 attr.implicit_const = read_signed_leb128 (abfd, addr,
4758 &bytes_read);
4759 addr += bytes_read;
4760 }
4761 if (attr.dw_idx == 0 && attr.form == 0)
4762 break;
4763 indexval.attr_vec.push_back (std::move (attr));
4764 }
4765 }
4766 if (addr != abbrev_table_start + abbrev_table_size)
4767 {
4768 warning (_("Section .debug_names in %s has abbreviation_table "
4769 "of size %s vs. written as %u, ignoring .debug_names."),
4770 filename, plongest (addr - abbrev_table_start),
4771 abbrev_table_size);
4772 return false;
4773 }
4774 map.entry_pool = addr;
4775
4776 return true;
4777 }
4778
4779 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4780 list. */
4781
4782 static void
4783 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4784 const mapped_debug_names &map,
4785 dwarf2_section_info &section,
4786 bool is_dwz)
4787 {
4788 if (!map.augmentation_is_gdb)
4789 {
4790 for (uint32_t i = 0; i < map.cu_count; ++i)
4791 {
4792 sect_offset sect_off
4793 = (sect_offset) (extract_unsigned_integer
4794 (map.cu_table_reordered + i * map.offset_size,
4795 map.offset_size,
4796 map.dwarf5_byte_order));
4797 /* We don't know the length of the CU, because the CU list in a
4798 .debug_names index can be incomplete, so we can't use the start
4799 of the next CU as end of this CU. We create the CUs here with
4800 length 0, and in cutu_reader::cutu_reader we'll fill in the
4801 actual length. */
4802 dwarf2_per_cu_data_up per_cu
4803 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4804 sect_off, 0);
4805 per_bfd->all_comp_units.push_back (std::move (per_cu));
4806 }
4807 return;
4808 }
4809
4810 sect_offset sect_off_prev;
4811 for (uint32_t i = 0; i <= map.cu_count; ++i)
4812 {
4813 sect_offset sect_off_next;
4814 if (i < map.cu_count)
4815 {
4816 sect_off_next
4817 = (sect_offset) (extract_unsigned_integer
4818 (map.cu_table_reordered + i * map.offset_size,
4819 map.offset_size,
4820 map.dwarf5_byte_order));
4821 }
4822 else
4823 sect_off_next = (sect_offset) section.size;
4824 if (i >= 1)
4825 {
4826 const ULONGEST length = sect_off_next - sect_off_prev;
4827 dwarf2_per_cu_data_up per_cu
4828 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4829 sect_off_prev, length);
4830 per_bfd->all_comp_units.push_back (std::move (per_cu));
4831 }
4832 sect_off_prev = sect_off_next;
4833 }
4834 }
4835
4836 /* Read the CU list from the mapped index, and use it to create all
4837 the CU objects for this dwarf2_per_objfile. */
4838
4839 static void
4840 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4841 const mapped_debug_names &map,
4842 const mapped_debug_names &dwz_map)
4843 {
4844 gdb_assert (per_bfd->all_comp_units.empty ());
4845 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4846
4847 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4848 false /* is_dwz */);
4849
4850 if (dwz_map.cu_count == 0)
4851 return;
4852
4853 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4854 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4855 true /* is_dwz */);
4856 }
4857
4858 /* Read .debug_names. If everything went ok, initialize the "quick"
4859 elements of all the CUs and return true. Otherwise, return false. */
4860
4861 static bool
4862 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4863 {
4864 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4865 mapped_debug_names dwz_map;
4866 struct objfile *objfile = per_objfile->objfile;
4867 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4868
4869 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4870 &per_bfd->debug_names, *map))
4871 return false;
4872
4873 /* Don't use the index if it's empty. */
4874 if (map->name_count == 0)
4875 return false;
4876
4877 /* If there is a .dwz file, read it so we can get its CU list as
4878 well. */
4879 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4880 if (dwz != NULL)
4881 {
4882 if (!read_debug_names_from_section (objfile,
4883 bfd_get_filename (dwz->dwz_bfd.get ()),
4884 &dwz->debug_names, dwz_map))
4885 {
4886 warning (_("could not read '.debug_names' section from %s; skipping"),
4887 bfd_get_filename (dwz->dwz_bfd.get ()));
4888 return false;
4889 }
4890 }
4891
4892 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4893
4894 if (map->tu_count != 0)
4895 {
4896 /* We can only handle a single .debug_types when we have an
4897 index. */
4898 if (per_bfd->types.size () != 1)
4899 return false;
4900
4901 dwarf2_section_info *section = &per_bfd->types[0];
4902
4903 create_signatured_type_table_from_debug_names
4904 (per_objfile, *map, section, &per_bfd->abbrev);
4905 }
4906
4907 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4908
4909 per_bfd->debug_names_table = std::move (map);
4910 per_bfd->using_index = 1;
4911 per_bfd->quick_file_names_table =
4912 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4913
4914 return true;
4915 }
4916
4917 /* Type used to manage iterating over all CUs looking for a symbol for
4918 .debug_names. */
4919
4920 class dw2_debug_names_iterator
4921 {
4922 public:
4923 dw2_debug_names_iterator (const mapped_debug_names &map,
4924 block_search_flags block_index,
4925 domain_enum domain,
4926 const char *name, dwarf2_per_objfile *per_objfile)
4927 : m_map (map), m_block_index (block_index), m_domain (domain),
4928 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4929 m_per_objfile (per_objfile)
4930 {}
4931
4932 dw2_debug_names_iterator (const mapped_debug_names &map,
4933 search_domain search, uint32_t namei,
4934 dwarf2_per_objfile *per_objfile,
4935 domain_enum domain = UNDEF_DOMAIN)
4936 : m_map (map),
4937 m_domain (domain),
4938 m_search (search),
4939 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4940 m_per_objfile (per_objfile)
4941 {}
4942
4943 dw2_debug_names_iterator (const mapped_debug_names &map,
4944 block_search_flags block_index, domain_enum domain,
4945 uint32_t namei, dwarf2_per_objfile *per_objfile)
4946 : m_map (map), m_block_index (block_index), m_domain (domain),
4947 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4948 m_per_objfile (per_objfile)
4949 {}
4950
4951 /* Return the next matching CU or NULL if there are no more. */
4952 dwarf2_per_cu_data *next ();
4953
4954 private:
4955 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4956 const char *name,
4957 dwarf2_per_objfile *per_objfile);
4958 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4959 uint32_t namei,
4960 dwarf2_per_objfile *per_objfile);
4961
4962 /* The internalized form of .debug_names. */
4963 const mapped_debug_names &m_map;
4964
4965 /* Restrict the search to these blocks. */
4966 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4967 | SEARCH_STATIC_BLOCK);
4968
4969 /* The kind of symbol we're looking for. */
4970 const domain_enum m_domain = UNDEF_DOMAIN;
4971 const search_domain m_search = ALL_DOMAIN;
4972
4973 /* The list of CUs from the index entry of the symbol, or NULL if
4974 not found. */
4975 const gdb_byte *m_addr;
4976
4977 dwarf2_per_objfile *m_per_objfile;
4978 };
4979
4980 const char *
4981 mapped_debug_names::namei_to_name
4982 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4983 {
4984 const ULONGEST namei_string_offs
4985 = extract_unsigned_integer ((name_table_string_offs_reordered
4986 + namei * offset_size),
4987 offset_size,
4988 dwarf5_byte_order);
4989 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4990 }
4991
4992 /* Find a slot in .debug_names for the object named NAME. If NAME is
4993 found, return pointer to its pool data. If NAME cannot be found,
4994 return NULL. */
4995
4996 const gdb_byte *
4997 dw2_debug_names_iterator::find_vec_in_debug_names
4998 (const mapped_debug_names &map, const char *name,
4999 dwarf2_per_objfile *per_objfile)
5000 {
5001 int (*cmp) (const char *, const char *);
5002
5003 gdb::unique_xmalloc_ptr<char> without_params;
5004 if (current_language->la_language == language_cplus
5005 || current_language->la_language == language_fortran
5006 || current_language->la_language == language_d)
5007 {
5008 /* NAME is already canonical. Drop any qualifiers as
5009 .debug_names does not contain any. */
5010
5011 if (strchr (name, '(') != NULL)
5012 {
5013 without_params = cp_remove_params (name);
5014 if (without_params != NULL)
5015 name = without_params.get ();
5016 }
5017 }
5018
5019 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5020
5021 const uint32_t full_hash = dwarf5_djb_hash (name);
5022 uint32_t namei
5023 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5024 (map.bucket_table_reordered
5025 + (full_hash % map.bucket_count)), 4,
5026 map.dwarf5_byte_order);
5027 if (namei == 0)
5028 return NULL;
5029 --namei;
5030 if (namei >= map.name_count)
5031 {
5032 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5033 "[in module %s]"),
5034 namei, map.name_count,
5035 objfile_name (per_objfile->objfile));
5036 return NULL;
5037 }
5038
5039 for (;;)
5040 {
5041 const uint32_t namei_full_hash
5042 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5043 (map.hash_table_reordered + namei), 4,
5044 map.dwarf5_byte_order);
5045 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5046 return NULL;
5047
5048 if (full_hash == namei_full_hash)
5049 {
5050 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5051
5052 #if 0 /* An expensive sanity check. */
5053 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5054 {
5055 complaint (_("Wrong .debug_names hash for string at index %u "
5056 "[in module %s]"),
5057 namei, objfile_name (dwarf2_per_objfile->objfile));
5058 return NULL;
5059 }
5060 #endif
5061
5062 if (cmp (namei_string, name) == 0)
5063 {
5064 const ULONGEST namei_entry_offs
5065 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5066 + namei * map.offset_size),
5067 map.offset_size, map.dwarf5_byte_order);
5068 return map.entry_pool + namei_entry_offs;
5069 }
5070 }
5071
5072 ++namei;
5073 if (namei >= map.name_count)
5074 return NULL;
5075 }
5076 }
5077
5078 const gdb_byte *
5079 dw2_debug_names_iterator::find_vec_in_debug_names
5080 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5081 {
5082 if (namei >= map.name_count)
5083 {
5084 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5085 "[in module %s]"),
5086 namei, map.name_count,
5087 objfile_name (per_objfile->objfile));
5088 return NULL;
5089 }
5090
5091 const ULONGEST namei_entry_offs
5092 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5093 + namei * map.offset_size),
5094 map.offset_size, map.dwarf5_byte_order);
5095 return map.entry_pool + namei_entry_offs;
5096 }
5097
5098 /* See dw2_debug_names_iterator. */
5099
5100 dwarf2_per_cu_data *
5101 dw2_debug_names_iterator::next ()
5102 {
5103 if (m_addr == NULL)
5104 return NULL;
5105
5106 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5107 struct objfile *objfile = m_per_objfile->objfile;
5108 bfd *const abfd = objfile->obfd;
5109
5110 again:
5111
5112 unsigned int bytes_read;
5113 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5114 m_addr += bytes_read;
5115 if (abbrev == 0)
5116 return NULL;
5117
5118 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5119 if (indexval_it == m_map.abbrev_map.cend ())
5120 {
5121 complaint (_("Wrong .debug_names undefined abbrev code %s "
5122 "[in module %s]"),
5123 pulongest (abbrev), objfile_name (objfile));
5124 return NULL;
5125 }
5126 const mapped_debug_names::index_val &indexval = indexval_it->second;
5127 enum class symbol_linkage {
5128 unknown,
5129 static_,
5130 extern_,
5131 } symbol_linkage_ = symbol_linkage::unknown;
5132 dwarf2_per_cu_data *per_cu = NULL;
5133 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5134 {
5135 ULONGEST ull;
5136 switch (attr.form)
5137 {
5138 case DW_FORM_implicit_const:
5139 ull = attr.implicit_const;
5140 break;
5141 case DW_FORM_flag_present:
5142 ull = 1;
5143 break;
5144 case DW_FORM_udata:
5145 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5146 m_addr += bytes_read;
5147 break;
5148 case DW_FORM_ref4:
5149 ull = read_4_bytes (abfd, m_addr);
5150 m_addr += 4;
5151 break;
5152 case DW_FORM_ref8:
5153 ull = read_8_bytes (abfd, m_addr);
5154 m_addr += 8;
5155 break;
5156 case DW_FORM_ref_sig8:
5157 ull = read_8_bytes (abfd, m_addr);
5158 m_addr += 8;
5159 break;
5160 default:
5161 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5162 dwarf_form_name (attr.form),
5163 objfile_name (objfile));
5164 return NULL;
5165 }
5166 switch (attr.dw_idx)
5167 {
5168 case DW_IDX_compile_unit:
5169 /* Don't crash on bad data. */
5170 if (ull >= per_bfd->all_comp_units.size ())
5171 {
5172 complaint (_(".debug_names entry has bad CU index %s"
5173 " [in module %s]"),
5174 pulongest (ull),
5175 objfile_name (objfile));
5176 continue;
5177 }
5178 per_cu = per_bfd->get_cu (ull);
5179 break;
5180 case DW_IDX_type_unit:
5181 /* Don't crash on bad data. */
5182 if (ull >= per_bfd->tu_stats.nr_tus)
5183 {
5184 complaint (_(".debug_names entry has bad TU index %s"
5185 " [in module %s]"),
5186 pulongest (ull),
5187 objfile_name (objfile));
5188 continue;
5189 }
5190 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5191 break;
5192 case DW_IDX_die_offset:
5193 /* In a per-CU index (as opposed to a per-module index), index
5194 entries without CU attribute implicitly refer to the single CU. */
5195 if (per_cu == NULL)
5196 per_cu = per_bfd->get_cu (0);
5197 break;
5198 case DW_IDX_GNU_internal:
5199 if (!m_map.augmentation_is_gdb)
5200 break;
5201 symbol_linkage_ = symbol_linkage::static_;
5202 break;
5203 case DW_IDX_GNU_external:
5204 if (!m_map.augmentation_is_gdb)
5205 break;
5206 symbol_linkage_ = symbol_linkage::extern_;
5207 break;
5208 }
5209 }
5210
5211 /* Skip if already read in. */
5212 if (m_per_objfile->symtab_set_p (per_cu))
5213 goto again;
5214
5215 /* Check static vs global. */
5216 if (symbol_linkage_ != symbol_linkage::unknown)
5217 {
5218 if (symbol_linkage_ == symbol_linkage::static_)
5219 {
5220 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5221 goto again;
5222 }
5223 else
5224 {
5225 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5226 goto again;
5227 }
5228 }
5229
5230 /* Match dw2_symtab_iter_next, symbol_kind
5231 and debug_names::psymbol_tag. */
5232 switch (m_domain)
5233 {
5234 case VAR_DOMAIN:
5235 switch (indexval.dwarf_tag)
5236 {
5237 case DW_TAG_variable:
5238 case DW_TAG_subprogram:
5239 /* Some types are also in VAR_DOMAIN. */
5240 case DW_TAG_typedef:
5241 case DW_TAG_structure_type:
5242 break;
5243 default:
5244 goto again;
5245 }
5246 break;
5247 case STRUCT_DOMAIN:
5248 switch (indexval.dwarf_tag)
5249 {
5250 case DW_TAG_typedef:
5251 case DW_TAG_structure_type:
5252 break;
5253 default:
5254 goto again;
5255 }
5256 break;
5257 case LABEL_DOMAIN:
5258 switch (indexval.dwarf_tag)
5259 {
5260 case 0:
5261 case DW_TAG_variable:
5262 break;
5263 default:
5264 goto again;
5265 }
5266 break;
5267 case MODULE_DOMAIN:
5268 switch (indexval.dwarf_tag)
5269 {
5270 case DW_TAG_module:
5271 break;
5272 default:
5273 goto again;
5274 }
5275 break;
5276 default:
5277 break;
5278 }
5279
5280 /* Match dw2_expand_symtabs_matching, symbol_kind and
5281 debug_names::psymbol_tag. */
5282 switch (m_search)
5283 {
5284 case VARIABLES_DOMAIN:
5285 switch (indexval.dwarf_tag)
5286 {
5287 case DW_TAG_variable:
5288 break;
5289 default:
5290 goto again;
5291 }
5292 break;
5293 case FUNCTIONS_DOMAIN:
5294 switch (indexval.dwarf_tag)
5295 {
5296 case DW_TAG_subprogram:
5297 break;
5298 default:
5299 goto again;
5300 }
5301 break;
5302 case TYPES_DOMAIN:
5303 switch (indexval.dwarf_tag)
5304 {
5305 case DW_TAG_typedef:
5306 case DW_TAG_structure_type:
5307 break;
5308 default:
5309 goto again;
5310 }
5311 break;
5312 case MODULES_DOMAIN:
5313 switch (indexval.dwarf_tag)
5314 {
5315 case DW_TAG_module:
5316 break;
5317 default:
5318 goto again;
5319 }
5320 default:
5321 break;
5322 }
5323
5324 return per_cu;
5325 }
5326
5327 /* This dumps minimal information about .debug_names. It is called
5328 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5329 uses this to verify that .debug_names has been loaded. */
5330
5331 void
5332 dwarf2_debug_names_index::dump (struct objfile *objfile)
5333 {
5334 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5335
5336 gdb_assert (per_objfile->per_bfd->using_index);
5337 gdb_printf (".debug_names:");
5338 if (per_objfile->per_bfd->debug_names_table)
5339 gdb_printf (" exists\n");
5340 else
5341 gdb_printf (" faked for \"readnow\"\n");
5342 gdb_printf ("\n");
5343 }
5344
5345 void
5346 dwarf2_debug_names_index::expand_matching_symbols
5347 (struct objfile *objfile,
5348 const lookup_name_info &name, domain_enum domain,
5349 int global,
5350 symbol_compare_ftype *ordered_compare)
5351 {
5352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5353
5354 /* debug_names_table is NULL if OBJF_READNOW. */
5355 if (!per_objfile->per_bfd->debug_names_table)
5356 return;
5357
5358 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5359 const block_search_flags block_flags
5360 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5361
5362 const char *match_name = name.ada ().lookup_name ().c_str ();
5363 auto matcher = [&] (const char *symname)
5364 {
5365 if (ordered_compare == nullptr)
5366 return true;
5367 return ordered_compare (symname, match_name) == 0;
5368 };
5369
5370 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5371 [&] (offset_type namei)
5372 {
5373 /* The name was matched, now expand corresponding CUs that were
5374 marked. */
5375 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5376 per_objfile);
5377
5378 struct dwarf2_per_cu_data *per_cu;
5379 while ((per_cu = iter.next ()) != NULL)
5380 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5381 nullptr);
5382 return true;
5383 }, per_objfile);
5384 }
5385
5386 bool
5387 dwarf2_debug_names_index::expand_symtabs_matching
5388 (struct objfile *objfile,
5389 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5390 const lookup_name_info *lookup_name,
5391 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5392 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5393 block_search_flags search_flags,
5394 domain_enum domain,
5395 enum search_domain kind)
5396 {
5397 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5398
5399 /* debug_names_table is NULL if OBJF_READNOW. */
5400 if (!per_objfile->per_bfd->debug_names_table)
5401 return true;
5402
5403 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5404
5405 /* This invariant is documented in quick-functions.h. */
5406 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5407 if (lookup_name == nullptr)
5408 {
5409 for (dwarf2_per_cu_data *per_cu
5410 : all_comp_units_range (per_objfile->per_bfd))
5411 {
5412 QUIT;
5413
5414 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5415 file_matcher,
5416 expansion_notify))
5417 return false;
5418 }
5419 return true;
5420 }
5421
5422 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5423
5424 bool result
5425 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5426 symbol_matcher,
5427 [&] (offset_type namei)
5428 {
5429 /* The name was matched, now expand corresponding CUs that were
5430 marked. */
5431 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5432
5433 struct dwarf2_per_cu_data *per_cu;
5434 while ((per_cu = iter.next ()) != NULL)
5435 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5436 file_matcher,
5437 expansion_notify))
5438 return false;
5439 return true;
5440 }, per_objfile);
5441
5442 return result;
5443 }
5444
5445 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5446 to either a dwarf2_per_bfd or dwz_file object. */
5447
5448 template <typename T>
5449 static gdb::array_view<const gdb_byte>
5450 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5451 {
5452 dwarf2_section_info *section = &section_owner->gdb_index;
5453
5454 if (section->empty ())
5455 return {};
5456
5457 /* Older elfutils strip versions could keep the section in the main
5458 executable while splitting it for the separate debug info file. */
5459 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5460 return {};
5461
5462 section->read (obj);
5463
5464 /* dwarf2_section_info::size is a bfd_size_type, while
5465 gdb::array_view works with size_t. On 32-bit hosts, with
5466 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5467 is 32-bit. So we need an explicit narrowing conversion here.
5468 This is fine, because it's impossible to allocate or mmap an
5469 array/buffer larger than what size_t can represent. */
5470 return gdb::make_array_view (section->buffer, section->size);
5471 }
5472
5473 /* Lookup the index cache for the contents of the index associated to
5474 DWARF2_OBJ. */
5475
5476 static gdb::array_view<const gdb_byte>
5477 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5478 {
5479 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5480 if (build_id == nullptr)
5481 return {};
5482
5483 return global_index_cache.lookup_gdb_index (build_id,
5484 &dwarf2_per_bfd->index_cache_res);
5485 }
5486
5487 /* Same as the above, but for DWZ. */
5488
5489 static gdb::array_view<const gdb_byte>
5490 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5491 {
5492 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5493 if (build_id == nullptr)
5494 return {};
5495
5496 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5497 }
5498
5499 /* See dwarf2/public.h. */
5500
5501 void
5502 dwarf2_initialize_objfile (struct objfile *objfile)
5503 {
5504 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5505 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5506
5507 dwarf_read_debug_printf ("called");
5508
5509 /* If we're about to read full symbols, don't bother with the
5510 indices. In this case we also don't care if some other debug
5511 format is making psymtabs, because they are all about to be
5512 expanded anyway. */
5513 if ((objfile->flags & OBJF_READNOW))
5514 {
5515 dwarf_read_debug_printf ("readnow requested");
5516
5517 /* When using READNOW, the using_index flag (set below) indicates that
5518 PER_BFD was already initialized, when we loaded some other objfile. */
5519 if (per_bfd->using_index)
5520 {
5521 dwarf_read_debug_printf ("using_index already set");
5522 objfile->qf.push_front (make_dwarf_gdb_index ());
5523 return;
5524 }
5525
5526 per_bfd->using_index = 1;
5527 create_all_comp_units (per_objfile);
5528 per_bfd->quick_file_names_table
5529 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5530
5531 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5532 {
5533 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5534
5535 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5536 struct dwarf2_per_cu_quick_data);
5537 }
5538
5539 /* Arrange for gdb to see the "quick" functions. However, these
5540 functions will be no-ops because we will have expanded all
5541 symtabs. */
5542 objfile->qf.push_front (make_dwarf_gdb_index ());
5543 return;
5544 }
5545
5546 /* Was a debug names index already read when we processed an objfile sharing
5547 PER_BFD? */
5548 if (per_bfd->debug_names_table != nullptr)
5549 {
5550 dwarf_read_debug_printf ("re-using shared debug names table");
5551 objfile->qf.push_front (make_dwarf_debug_names ());
5552 return;
5553 }
5554
5555 /* Was a GDB index already read when we processed an objfile sharing
5556 PER_BFD? */
5557 if (per_bfd->index_table != nullptr)
5558 {
5559 dwarf_read_debug_printf ("re-using shared index table");
5560 objfile->qf.push_front (make_dwarf_gdb_index ());
5561 return;
5562 }
5563
5564 /* There might already be partial symtabs built for this BFD. This happens
5565 when loading the same binary twice with the index-cache enabled. If so,
5566 don't try to read an index. The objfile / per_objfile initialization will
5567 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5568 code path. */
5569 if (per_bfd->partial_symtabs != nullptr)
5570 {
5571 dwarf_read_debug_printf ("re-using shared partial symtabs");
5572 objfile->qf.push_front (make_lazy_dwarf_reader ());
5573 return;
5574 }
5575
5576 if (dwarf2_read_debug_names (per_objfile))
5577 {
5578 dwarf_read_debug_printf ("found debug names");
5579 objfile->qf.push_front (make_dwarf_debug_names ());
5580 return;
5581 }
5582
5583 if (dwarf2_read_gdb_index (per_objfile,
5584 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5585 get_gdb_index_contents_from_section<dwz_file>))
5586 {
5587 dwarf_read_debug_printf ("found gdb index from file");
5588 objfile->qf.push_front (make_dwarf_gdb_index ());
5589 return;
5590 }
5591
5592 /* ... otherwise, try to find the index in the index cache. */
5593 if (dwarf2_read_gdb_index (per_objfile,
5594 get_gdb_index_contents_from_cache,
5595 get_gdb_index_contents_from_cache_dwz))
5596 {
5597 dwarf_read_debug_printf ("found gdb index from cache");
5598 global_index_cache.hit ();
5599 objfile->qf.push_front (make_dwarf_gdb_index ());
5600 return;
5601 }
5602
5603 global_index_cache.miss ();
5604 objfile->qf.push_front (make_lazy_dwarf_reader ());
5605 }
5606
5607 \f
5608
5609 /* Build a partial symbol table. */
5610
5611 void
5612 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5613 {
5614 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5615 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5616
5617 if (per_bfd->partial_symtabs != nullptr)
5618 {
5619 /* Partial symbols were already read, so now we can simply
5620 attach them. */
5621 if (psf == nullptr)
5622 {
5623 psf = new psymbol_functions (per_bfd->partial_symtabs);
5624 objfile->qf.emplace_front (psf);
5625 }
5626 else
5627 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5628 return;
5629 }
5630
5631 if (psf == nullptr)
5632 {
5633 psf = new psymbol_functions;
5634 objfile->qf.emplace_front (psf);
5635 }
5636 const std::shared_ptr<psymtab_storage> &partial_symtabs
5637 = psf->get_partial_symtabs ();
5638
5639 /* Set the local reference to partial symtabs, so that we don't try
5640 to read them again if reading another objfile with the same BFD.
5641 If we can't in fact share, this won't make a difference anyway as
5642 the dwarf2_per_bfd object won't be shared. */
5643 per_bfd->partial_symtabs = partial_symtabs;
5644
5645 try
5646 {
5647 /* This isn't really ideal: all the data we allocate on the
5648 objfile's obstack is still uselessly kept around. However,
5649 freeing it seems unsafe. */
5650 psymtab_discarder psymtabs (partial_symtabs.get ());
5651 dwarf2_build_psymtabs_hard (per_objfile);
5652 psymtabs.keep ();
5653
5654 /* (maybe) store an index in the cache. */
5655 global_index_cache.store (per_objfile);
5656 }
5657 catch (const gdb_exception_error &except)
5658 {
5659 exception_print (gdb_stderr, except);
5660 }
5661 }
5662
5663 /* Find the base address of the compilation unit for range lists and
5664 location lists. It will normally be specified by DW_AT_low_pc.
5665 In DWARF-3 draft 4, the base address could be overridden by
5666 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5667 compilation units with discontinuous ranges. */
5668
5669 static void
5670 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5671 {
5672 struct attribute *attr;
5673
5674 cu->base_address.reset ();
5675
5676 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5677 if (attr != nullptr)
5678 cu->base_address = attr->as_address ();
5679 else
5680 {
5681 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5682 if (attr != nullptr)
5683 cu->base_address = attr->as_address ();
5684 }
5685 }
5686
5687 /* Helper function that returns the proper abbrev section for
5688 THIS_CU. */
5689
5690 static struct dwarf2_section_info *
5691 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5692 {
5693 struct dwarf2_section_info *abbrev;
5694 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5695
5696 if (this_cu->is_dwz)
5697 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5698 else
5699 abbrev = &per_bfd->abbrev;
5700
5701 return abbrev;
5702 }
5703
5704 /* Fetch the abbreviation table offset from a comp or type unit header. */
5705
5706 static sect_offset
5707 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5708 struct dwarf2_section_info *section,
5709 sect_offset sect_off)
5710 {
5711 bfd *abfd = section->get_bfd_owner ();
5712 const gdb_byte *info_ptr;
5713 unsigned int initial_length_size, offset_size;
5714 uint16_t version;
5715
5716 section->read (per_objfile->objfile);
5717 info_ptr = section->buffer + to_underlying (sect_off);
5718 read_initial_length (abfd, info_ptr, &initial_length_size);
5719 offset_size = initial_length_size == 4 ? 4 : 8;
5720 info_ptr += initial_length_size;
5721
5722 version = read_2_bytes (abfd, info_ptr);
5723 info_ptr += 2;
5724 if (version >= 5)
5725 {
5726 /* Skip unit type and address size. */
5727 info_ptr += 2;
5728 }
5729
5730 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5731 }
5732
5733 /* A partial symtab that is used only for include files. */
5734 struct dwarf2_include_psymtab : public partial_symtab
5735 {
5736 dwarf2_include_psymtab (const char *filename,
5737 psymtab_storage *partial_symtabs,
5738 objfile_per_bfd_storage *objfile_per_bfd)
5739 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5740 {
5741 }
5742
5743 void read_symtab (struct objfile *objfile) override
5744 {
5745 /* It's an include file, no symbols to read for it.
5746 Everything is in the includer symtab. */
5747
5748 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5749 expansion of the includer psymtab. We use the dependencies[0] field to
5750 model the includer. But if we go the regular route of calling
5751 expand_psymtab here, and having expand_psymtab call expand_dependencies
5752 to expand the includer, we'll only use expand_psymtab on the includer
5753 (making it a non-toplevel psymtab), while if we expand the includer via
5754 another path, we'll use read_symtab (making it a toplevel psymtab).
5755 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5756 psymtab, and trigger read_symtab on the includer here directly. */
5757 includer ()->read_symtab (objfile);
5758 }
5759
5760 void expand_psymtab (struct objfile *objfile) override
5761 {
5762 /* This is not called by read_symtab, and should not be called by any
5763 expand_dependencies. */
5764 gdb_assert (false);
5765 }
5766
5767 bool readin_p (struct objfile *objfile) const override
5768 {
5769 return includer ()->readin_p (objfile);
5770 }
5771
5772 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5773 {
5774 compunit_symtab *cust = includer ()->get_compunit_symtab (objfile);
5775 while (cust != nullptr && cust->user != nullptr)
5776 cust = cust->user;
5777 return cust;
5778 }
5779
5780 private:
5781 partial_symtab *includer () const
5782 {
5783 /* An include psymtab has exactly one dependency: the psymtab that
5784 includes it. */
5785 gdb_assert (this->number_of_dependencies == 1);
5786 return this->dependencies[0];
5787 }
5788 };
5789
5790 /* Allocate a new partial symtab for file named NAME and mark this new
5791 partial symtab as being an include of PST. */
5792
5793 static void
5794 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5795 const char *name,
5796 dwarf2_psymtab *pst,
5797 psymtab_storage *partial_symtabs,
5798 objfile_per_bfd_storage *objfile_per_bfd)
5799 {
5800 dwarf2_include_psymtab *subpst
5801 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5802
5803 if (!IS_ABSOLUTE_PATH (subpst->filename))
5804 subpst->dirname = pst->dirname;
5805
5806 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5807 subpst->dependencies[0] = pst;
5808 subpst->number_of_dependencies = 1;
5809 }
5810
5811 /* Read the Line Number Program data and extract the list of files
5812 included by the source file represented by PST. Build an include
5813 partial symtab for each of these included files. */
5814
5815 static void
5816 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5817 struct die_info *die,
5818 const file_and_directory &fnd,
5819 dwarf2_psymtab *pst)
5820 {
5821 line_header_up lh;
5822 struct attribute *attr;
5823
5824 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5825 if (attr != nullptr && attr->form_is_unsigned ())
5826 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5827 if (lh == NULL)
5828 return; /* No linetable, so no includes. */
5829
5830 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5831 that we pass in the raw text_low here; that is ok because we're
5832 only decoding the line table to make include partial symtabs, and
5833 so the addresses aren't really used. */
5834 dwarf_decode_lines (lh.get (), fnd, cu, pst,
5835 pst->raw_text_low (), 1);
5836 }
5837
5838 static hashval_t
5839 hash_signatured_type (const void *item)
5840 {
5841 const struct signatured_type *sig_type
5842 = (const struct signatured_type *) item;
5843
5844 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5845 return sig_type->signature;
5846 }
5847
5848 static int
5849 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5850 {
5851 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5852 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5853
5854 return lhs->signature == rhs->signature;
5855 }
5856
5857 /* Allocate a hash table for signatured types. */
5858
5859 static htab_up
5860 allocate_signatured_type_table ()
5861 {
5862 return htab_up (htab_create_alloc (41,
5863 hash_signatured_type,
5864 eq_signatured_type,
5865 NULL, xcalloc, xfree));
5866 }
5867
5868 /* A helper for create_debug_types_hash_table. Read types from SECTION
5869 and fill them into TYPES_HTAB. It will process only type units,
5870 therefore DW_UT_type. */
5871
5872 static void
5873 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5874 struct dwo_file *dwo_file,
5875 dwarf2_section_info *section, htab_up &types_htab,
5876 rcuh_kind section_kind)
5877 {
5878 struct objfile *objfile = per_objfile->objfile;
5879 struct dwarf2_section_info *abbrev_section;
5880 bfd *abfd;
5881 const gdb_byte *info_ptr, *end_ptr;
5882
5883 abbrev_section = &dwo_file->sections.abbrev;
5884
5885 dwarf_read_debug_printf ("Reading %s for %s",
5886 section->get_name (),
5887 abbrev_section->get_file_name ());
5888
5889 section->read (objfile);
5890 info_ptr = section->buffer;
5891
5892 if (info_ptr == NULL)
5893 return;
5894
5895 /* We can't set abfd until now because the section may be empty or
5896 not present, in which case the bfd is unknown. */
5897 abfd = section->get_bfd_owner ();
5898
5899 /* We don't use cutu_reader here because we don't need to read
5900 any dies: the signature is in the header. */
5901
5902 end_ptr = info_ptr + section->size;
5903 while (info_ptr < end_ptr)
5904 {
5905 signatured_type_up sig_type;
5906 struct dwo_unit *dwo_tu;
5907 void **slot;
5908 const gdb_byte *ptr = info_ptr;
5909 struct comp_unit_head header;
5910 unsigned int length;
5911
5912 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5913
5914 /* Initialize it due to a false compiler warning. */
5915 header.signature = -1;
5916 header.type_cu_offset_in_tu = (cu_offset) -1;
5917
5918 /* We need to read the type's signature in order to build the hash
5919 table, but we don't need anything else just yet. */
5920
5921 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5922 abbrev_section, ptr, section_kind);
5923
5924 length = header.get_length ();
5925
5926 /* Skip dummy type units. */
5927 if (ptr >= info_ptr + length
5928 || peek_abbrev_code (abfd, ptr) == 0
5929 || (header.unit_type != DW_UT_type
5930 && header.unit_type != DW_UT_split_type))
5931 {
5932 info_ptr += length;
5933 continue;
5934 }
5935
5936 if (types_htab == NULL)
5937 types_htab = allocate_dwo_unit_table ();
5938
5939 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5940 dwo_tu->dwo_file = dwo_file;
5941 dwo_tu->signature = header.signature;
5942 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5943 dwo_tu->section = section;
5944 dwo_tu->sect_off = sect_off;
5945 dwo_tu->length = length;
5946
5947 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5948 gdb_assert (slot != NULL);
5949 if (*slot != NULL)
5950 complaint (_("debug type entry at offset %s is duplicate to"
5951 " the entry at offset %s, signature %s"),
5952 sect_offset_str (sect_off),
5953 sect_offset_str (dwo_tu->sect_off),
5954 hex_string (header.signature));
5955 *slot = dwo_tu;
5956
5957 dwarf_read_debug_printf_v (" offset %s, signature %s",
5958 sect_offset_str (sect_off),
5959 hex_string (header.signature));
5960
5961 info_ptr += length;
5962 }
5963 }
5964
5965 /* Create the hash table of all entries in the .debug_types
5966 (or .debug_types.dwo) section(s).
5967 DWO_FILE is a pointer to the DWO file object.
5968
5969 The result is a pointer to the hash table or NULL if there are no types.
5970
5971 Note: This function processes DWO files only, not DWP files. */
5972
5973 static void
5974 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5975 struct dwo_file *dwo_file,
5976 gdb::array_view<dwarf2_section_info> type_sections,
5977 htab_up &types_htab)
5978 {
5979 for (dwarf2_section_info &section : type_sections)
5980 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5981 rcuh_kind::TYPE);
5982 }
5983
5984 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5985 If SLOT is non-NULL, it is the entry to use in the hash table.
5986 Otherwise we find one. */
5987
5988 static struct signatured_type *
5989 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5990 {
5991 if (per_objfile->per_bfd->all_comp_units.size ()
5992 == per_objfile->per_bfd->all_comp_units.capacity ())
5993 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5994
5995 signatured_type_up sig_type_holder
5996 = per_objfile->per_bfd->allocate_signatured_type (sig);
5997 signatured_type *sig_type = sig_type_holder.get ();
5998
5999 per_objfile->per_bfd->all_comp_units.emplace_back
6000 (sig_type_holder.release ());
6001 if (per_objfile->per_bfd->using_index)
6002 {
6003 sig_type->v.quick =
6004 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6005 struct dwarf2_per_cu_quick_data);
6006 }
6007
6008 if (slot == NULL)
6009 {
6010 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6011 sig_type, INSERT);
6012 }
6013 gdb_assert (*slot == NULL);
6014 *slot = sig_type;
6015 /* The rest of sig_type must be filled in by the caller. */
6016 return sig_type;
6017 }
6018
6019 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6020 Fill in SIG_ENTRY with DWO_ENTRY. */
6021
6022 static void
6023 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6024 struct signatured_type *sig_entry,
6025 struct dwo_unit *dwo_entry)
6026 {
6027 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6028
6029 /* Make sure we're not clobbering something we don't expect to. */
6030 gdb_assert (! sig_entry->queued);
6031 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6032 if (per_bfd->using_index)
6033 {
6034 gdb_assert (sig_entry->v.quick != NULL);
6035 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6036 }
6037 else
6038 gdb_assert (sig_entry->v.psymtab == NULL);
6039 gdb_assert (sig_entry->signature == dwo_entry->signature);
6040 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6041 gdb_assert (sig_entry->type_unit_group == NULL);
6042 gdb_assert (sig_entry->dwo_unit == NULL);
6043
6044 sig_entry->section = dwo_entry->section;
6045 sig_entry->sect_off = dwo_entry->sect_off;
6046 sig_entry->length = dwo_entry->length;
6047 sig_entry->reading_dwo_directly = 1;
6048 sig_entry->per_bfd = per_bfd;
6049 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6050 sig_entry->dwo_unit = dwo_entry;
6051 }
6052
6053 /* Subroutine of lookup_signatured_type.
6054 If we haven't read the TU yet, create the signatured_type data structure
6055 for a TU to be read in directly from a DWO file, bypassing the stub.
6056 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6057 using .gdb_index, then when reading a CU we want to stay in the DWO file
6058 containing that CU. Otherwise we could end up reading several other DWO
6059 files (due to comdat folding) to process the transitive closure of all the
6060 mentioned TUs, and that can be slow. The current DWO file will have every
6061 type signature that it needs.
6062 We only do this for .gdb_index because in the psymtab case we already have
6063 to read all the DWOs to build the type unit groups. */
6064
6065 static struct signatured_type *
6066 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6067 {
6068 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6069 struct dwo_file *dwo_file;
6070 struct dwo_unit find_dwo_entry, *dwo_entry;
6071 void **slot;
6072
6073 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6074
6075 /* If TU skeletons have been removed then we may not have read in any
6076 TUs yet. */
6077 if (per_objfile->per_bfd->signatured_types == NULL)
6078 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6079
6080 /* We only ever need to read in one copy of a signatured type.
6081 Use the global signatured_types array to do our own comdat-folding
6082 of types. If this is the first time we're reading this TU, and
6083 the TU has an entry in .gdb_index, replace the recorded data from
6084 .gdb_index with this TU. */
6085
6086 signatured_type find_sig_entry (sig);
6087 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6088 &find_sig_entry, INSERT);
6089 signatured_type *sig_entry = (struct signatured_type *) *slot;
6090
6091 /* We can get here with the TU already read, *or* in the process of being
6092 read. Don't reassign the global entry to point to this DWO if that's
6093 the case. Also note that if the TU is already being read, it may not
6094 have come from a DWO, the program may be a mix of Fission-compiled
6095 code and non-Fission-compiled code. */
6096
6097 /* Have we already tried to read this TU?
6098 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6099 needn't exist in the global table yet). */
6100 if (sig_entry != NULL && sig_entry->tu_read)
6101 return sig_entry;
6102
6103 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6104 dwo_unit of the TU itself. */
6105 dwo_file = cu->dwo_unit->dwo_file;
6106
6107 /* Ok, this is the first time we're reading this TU. */
6108 if (dwo_file->tus == NULL)
6109 return NULL;
6110 find_dwo_entry.signature = sig;
6111 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6112 &find_dwo_entry);
6113 if (dwo_entry == NULL)
6114 return NULL;
6115
6116 /* If the global table doesn't have an entry for this TU, add one. */
6117 if (sig_entry == NULL)
6118 sig_entry = add_type_unit (per_objfile, sig, slot);
6119
6120 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6121 sig_entry->tu_read = 1;
6122 return sig_entry;
6123 }
6124
6125 /* Subroutine of lookup_signatured_type.
6126 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6127 then try the DWP file. If the TU stub (skeleton) has been removed then
6128 it won't be in .gdb_index. */
6129
6130 static struct signatured_type *
6131 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6132 {
6133 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6134 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6135 struct dwo_unit *dwo_entry;
6136 void **slot;
6137
6138 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6139 gdb_assert (dwp_file != NULL);
6140
6141 /* If TU skeletons have been removed then we may not have read in any
6142 TUs yet. */
6143 if (per_objfile->per_bfd->signatured_types == NULL)
6144 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6145
6146 signatured_type find_sig_entry (sig);
6147 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6148 &find_sig_entry, INSERT);
6149 signatured_type *sig_entry = (struct signatured_type *) *slot;
6150
6151 /* Have we already tried to read this TU?
6152 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6153 needn't exist in the global table yet). */
6154 if (sig_entry != NULL)
6155 return sig_entry;
6156
6157 if (dwp_file->tus == NULL)
6158 return NULL;
6159 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6160 1 /* is_debug_types */);
6161 if (dwo_entry == NULL)
6162 return NULL;
6163
6164 sig_entry = add_type_unit (per_objfile, sig, slot);
6165 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6166
6167 return sig_entry;
6168 }
6169
6170 /* Lookup a signature based type for DW_FORM_ref_sig8.
6171 Returns NULL if signature SIG is not present in the table.
6172 It is up to the caller to complain about this. */
6173
6174 static struct signatured_type *
6175 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6176 {
6177 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6178
6179 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6180 {
6181 /* We're in a DWO/DWP file, and we're using .gdb_index.
6182 These cases require special processing. */
6183 if (get_dwp_file (per_objfile) == NULL)
6184 return lookup_dwo_signatured_type (cu, sig);
6185 else
6186 return lookup_dwp_signatured_type (cu, sig);
6187 }
6188 else
6189 {
6190 if (per_objfile->per_bfd->signatured_types == NULL)
6191 return NULL;
6192 signatured_type find_entry (sig);
6193 return ((struct signatured_type *)
6194 htab_find (per_objfile->per_bfd->signatured_types.get (),
6195 &find_entry));
6196 }
6197 }
6198
6199 /* Low level DIE reading support. */
6200
6201 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6202
6203 static void
6204 init_cu_die_reader (struct die_reader_specs *reader,
6205 struct dwarf2_cu *cu,
6206 struct dwarf2_section_info *section,
6207 struct dwo_file *dwo_file,
6208 struct abbrev_table *abbrev_table)
6209 {
6210 gdb_assert (section->readin && section->buffer != NULL);
6211 reader->abfd = section->get_bfd_owner ();
6212 reader->cu = cu;
6213 reader->dwo_file = dwo_file;
6214 reader->die_section = section;
6215 reader->buffer = section->buffer;
6216 reader->buffer_end = section->buffer + section->size;
6217 reader->abbrev_table = abbrev_table;
6218 }
6219
6220 /* Subroutine of cutu_reader to simplify it.
6221 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6222 There's just a lot of work to do, and cutu_reader is big enough
6223 already.
6224
6225 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6226 from it to the DIE in the DWO. If NULL we are skipping the stub.
6227 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6228 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6229 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6230 STUB_COMP_DIR may be non-NULL.
6231 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6232 are filled in with the info of the DIE from the DWO file.
6233 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6234 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6235 kept around for at least as long as *RESULT_READER.
6236
6237 The result is non-zero if a valid (non-dummy) DIE was found. */
6238
6239 static int
6240 read_cutu_die_from_dwo (dwarf2_cu *cu,
6241 struct dwo_unit *dwo_unit,
6242 struct die_info *stub_comp_unit_die,
6243 const char *stub_comp_dir,
6244 struct die_reader_specs *result_reader,
6245 const gdb_byte **result_info_ptr,
6246 struct die_info **result_comp_unit_die,
6247 abbrev_table_up *result_dwo_abbrev_table)
6248 {
6249 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6250 dwarf2_per_cu_data *per_cu = cu->per_cu;
6251 struct objfile *objfile = per_objfile->objfile;
6252 bfd *abfd;
6253 const gdb_byte *begin_info_ptr, *info_ptr;
6254 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6255 int i,num_extra_attrs;
6256 struct dwarf2_section_info *dwo_abbrev_section;
6257 struct die_info *comp_unit_die;
6258
6259 /* At most one of these may be provided. */
6260 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6261
6262 /* These attributes aren't processed until later:
6263 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6264 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6265 referenced later. However, these attributes are found in the stub
6266 which we won't have later. In order to not impose this complication
6267 on the rest of the code, we read them here and copy them to the
6268 DWO CU/TU die. */
6269
6270 stmt_list = NULL;
6271 low_pc = NULL;
6272 high_pc = NULL;
6273 ranges = NULL;
6274 comp_dir = NULL;
6275
6276 if (stub_comp_unit_die != NULL)
6277 {
6278 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6279 DWO file. */
6280 if (!per_cu->is_debug_types)
6281 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6282 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6283 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6284 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6285 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6286
6287 cu->addr_base = stub_comp_unit_die->addr_base ();
6288
6289 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6290 We need the value before we can process DW_AT_ranges values from the
6291 DWO. */
6292 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6293
6294 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6295 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6296 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6297 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6298 section. */
6299 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6300 }
6301 else if (stub_comp_dir != NULL)
6302 {
6303 /* Reconstruct the comp_dir attribute to simplify the code below. */
6304 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6305 comp_dir->name = DW_AT_comp_dir;
6306 comp_dir->form = DW_FORM_string;
6307 comp_dir->set_string_noncanonical (stub_comp_dir);
6308 }
6309
6310 /* Set up for reading the DWO CU/TU. */
6311 cu->dwo_unit = dwo_unit;
6312 dwarf2_section_info *section = dwo_unit->section;
6313 section->read (objfile);
6314 abfd = section->get_bfd_owner ();
6315 begin_info_ptr = info_ptr = (section->buffer
6316 + to_underlying (dwo_unit->sect_off));
6317 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6318
6319 if (per_cu->is_debug_types)
6320 {
6321 signatured_type *sig_type = (struct signatured_type *) per_cu;
6322
6323 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6324 section, dwo_abbrev_section,
6325 info_ptr, rcuh_kind::TYPE);
6326 /* This is not an assert because it can be caused by bad debug info. */
6327 if (sig_type->signature != cu->header.signature)
6328 {
6329 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6330 " TU at offset %s [in module %s]"),
6331 hex_string (sig_type->signature),
6332 hex_string (cu->header.signature),
6333 sect_offset_str (dwo_unit->sect_off),
6334 bfd_get_filename (abfd));
6335 }
6336 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6337 /* For DWOs coming from DWP files, we don't know the CU length
6338 nor the type's offset in the TU until now. */
6339 dwo_unit->length = cu->header.get_length ();
6340 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6341
6342 /* Establish the type offset that can be used to lookup the type.
6343 For DWO files, we don't know it until now. */
6344 sig_type->type_offset_in_section
6345 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6346 }
6347 else
6348 {
6349 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6350 section, dwo_abbrev_section,
6351 info_ptr, rcuh_kind::COMPILE);
6352 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6353 /* For DWOs coming from DWP files, we don't know the CU length
6354 until now. */
6355 dwo_unit->length = cu->header.get_length ();
6356 }
6357
6358 dwo_abbrev_section->read (objfile);
6359 *result_dwo_abbrev_table
6360 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6361 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6362 result_dwo_abbrev_table->get ());
6363
6364 /* Read in the die, but leave space to copy over the attributes
6365 from the stub. This has the benefit of simplifying the rest of
6366 the code - all the work to maintain the illusion of a single
6367 DW_TAG_{compile,type}_unit DIE is done here. */
6368 num_extra_attrs = ((stmt_list != NULL)
6369 + (low_pc != NULL)
6370 + (high_pc != NULL)
6371 + (ranges != NULL)
6372 + (comp_dir != NULL));
6373 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6374 num_extra_attrs);
6375
6376 /* Copy over the attributes from the stub to the DIE we just read in. */
6377 comp_unit_die = *result_comp_unit_die;
6378 i = comp_unit_die->num_attrs;
6379 if (stmt_list != NULL)
6380 comp_unit_die->attrs[i++] = *stmt_list;
6381 if (low_pc != NULL)
6382 comp_unit_die->attrs[i++] = *low_pc;
6383 if (high_pc != NULL)
6384 comp_unit_die->attrs[i++] = *high_pc;
6385 if (ranges != NULL)
6386 comp_unit_die->attrs[i++] = *ranges;
6387 if (comp_dir != NULL)
6388 comp_unit_die->attrs[i++] = *comp_dir;
6389 comp_unit_die->num_attrs += num_extra_attrs;
6390
6391 if (dwarf_die_debug)
6392 {
6393 gdb_printf (gdb_stdlog,
6394 "Read die from %s@0x%x of %s:\n",
6395 section->get_name (),
6396 (unsigned) (begin_info_ptr - section->buffer),
6397 bfd_get_filename (abfd));
6398 dump_die (comp_unit_die, dwarf_die_debug);
6399 }
6400
6401 /* Skip dummy compilation units. */
6402 if (info_ptr >= begin_info_ptr + dwo_unit->length
6403 || peek_abbrev_code (abfd, info_ptr) == 0)
6404 return 0;
6405
6406 *result_info_ptr = info_ptr;
6407 return 1;
6408 }
6409
6410 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6411 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6412 signature is part of the header. */
6413 static gdb::optional<ULONGEST>
6414 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6415 {
6416 if (cu->header.version >= 5)
6417 return cu->header.signature;
6418 struct attribute *attr;
6419 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6420 if (attr == nullptr || !attr->form_is_unsigned ())
6421 return gdb::optional<ULONGEST> ();
6422 return attr->as_unsigned ();
6423 }
6424
6425 /* Subroutine of cutu_reader to simplify it.
6426 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6427 Returns NULL if the specified DWO unit cannot be found. */
6428
6429 static struct dwo_unit *
6430 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6431 {
6432 dwarf2_per_cu_data *per_cu = cu->per_cu;
6433 struct dwo_unit *dwo_unit;
6434 const char *comp_dir;
6435
6436 gdb_assert (cu != NULL);
6437
6438 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6439 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6440 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6441
6442 if (per_cu->is_debug_types)
6443 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6444 else
6445 {
6446 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6447
6448 if (!signature.has_value ())
6449 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6450 " [in module %s]"),
6451 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6452
6453 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6454 }
6455
6456 return dwo_unit;
6457 }
6458
6459 /* Subroutine of cutu_reader to simplify it.
6460 See it for a description of the parameters.
6461 Read a TU directly from a DWO file, bypassing the stub. */
6462
6463 void
6464 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6465 dwarf2_per_objfile *per_objfile,
6466 dwarf2_cu *existing_cu)
6467 {
6468 struct signatured_type *sig_type;
6469
6470 /* Verify we can do the following downcast, and that we have the
6471 data we need. */
6472 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6473 sig_type = (struct signatured_type *) this_cu;
6474 gdb_assert (sig_type->dwo_unit != NULL);
6475
6476 dwarf2_cu *cu;
6477
6478 if (existing_cu != nullptr)
6479 {
6480 cu = existing_cu;
6481 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6482 /* There's no need to do the rereading_dwo_cu handling that
6483 cutu_reader does since we don't read the stub. */
6484 }
6485 else
6486 {
6487 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6488 in per_objfile yet. */
6489 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6490 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6491 cu = m_new_cu.get ();
6492 }
6493
6494 /* A future optimization, if needed, would be to use an existing
6495 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6496 could share abbrev tables. */
6497
6498 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6499 NULL /* stub_comp_unit_die */,
6500 sig_type->dwo_unit->dwo_file->comp_dir,
6501 this, &info_ptr,
6502 &comp_unit_die,
6503 &m_dwo_abbrev_table) == 0)
6504 {
6505 /* Dummy die. */
6506 dummy_p = true;
6507 }
6508 }
6509
6510 /* Initialize a CU (or TU) and read its DIEs.
6511 If the CU defers to a DWO file, read the DWO file as well.
6512
6513 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6514 Otherwise the table specified in the comp unit header is read in and used.
6515 This is an optimization for when we already have the abbrev table.
6516
6517 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6518 allocated. */
6519
6520 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6521 dwarf2_per_objfile *per_objfile,
6522 struct abbrev_table *abbrev_table,
6523 dwarf2_cu *existing_cu,
6524 bool skip_partial)
6525 : die_reader_specs {},
6526 m_this_cu (this_cu)
6527 {
6528 struct objfile *objfile = per_objfile->objfile;
6529 struct dwarf2_section_info *section = this_cu->section;
6530 bfd *abfd = section->get_bfd_owner ();
6531 const gdb_byte *begin_info_ptr;
6532 struct signatured_type *sig_type = NULL;
6533 struct dwarf2_section_info *abbrev_section;
6534 /* Non-zero if CU currently points to a DWO file and we need to
6535 reread it. When this happens we need to reread the skeleton die
6536 before we can reread the DWO file (this only applies to CUs, not TUs). */
6537 int rereading_dwo_cu = 0;
6538
6539 if (dwarf_die_debug)
6540 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6541 this_cu->is_debug_types ? "type" : "comp",
6542 sect_offset_str (this_cu->sect_off));
6543
6544 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6545 file (instead of going through the stub), short-circuit all of this. */
6546 if (this_cu->reading_dwo_directly)
6547 {
6548 /* Narrow down the scope of possibilities to have to understand. */
6549 gdb_assert (this_cu->is_debug_types);
6550 gdb_assert (abbrev_table == NULL);
6551 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6552 return;
6553 }
6554
6555 /* This is cheap if the section is already read in. */
6556 section->read (objfile);
6557
6558 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6559
6560 abbrev_section = get_abbrev_section_for_cu (this_cu);
6561
6562 dwarf2_cu *cu;
6563
6564 if (existing_cu != nullptr)
6565 {
6566 cu = existing_cu;
6567 /* If this CU is from a DWO file we need to start over, we need to
6568 refetch the attributes from the skeleton CU.
6569 This could be optimized by retrieving those attributes from when we
6570 were here the first time: the previous comp_unit_die was stored in
6571 comp_unit_obstack. But there's no data yet that we need this
6572 optimization. */
6573 if (cu->dwo_unit != NULL)
6574 rereading_dwo_cu = 1;
6575 }
6576 else
6577 {
6578 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6579 in per_objfile yet. */
6580 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6581 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6582 cu = m_new_cu.get ();
6583 }
6584
6585 /* Get the header. */
6586 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6587 {
6588 /* We already have the header, there's no need to read it in again. */
6589 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6590 }
6591 else
6592 {
6593 if (this_cu->is_debug_types)
6594 {
6595 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6596 section, abbrev_section,
6597 info_ptr, rcuh_kind::TYPE);
6598
6599 /* Since per_cu is the first member of struct signatured_type,
6600 we can go from a pointer to one to a pointer to the other. */
6601 sig_type = (struct signatured_type *) this_cu;
6602 gdb_assert (sig_type->signature == cu->header.signature);
6603 gdb_assert (sig_type->type_offset_in_tu
6604 == cu->header.type_cu_offset_in_tu);
6605 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6606
6607 /* LENGTH has not been set yet for type units if we're
6608 using .gdb_index. */
6609 this_cu->length = cu->header.get_length ();
6610
6611 /* Establish the type offset that can be used to lookup the type. */
6612 sig_type->type_offset_in_section =
6613 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6614
6615 this_cu->dwarf_version = cu->header.version;
6616 }
6617 else
6618 {
6619 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6620 section, abbrev_section,
6621 info_ptr,
6622 rcuh_kind::COMPILE);
6623
6624 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6625 if (this_cu->length == 0)
6626 this_cu->length = cu->header.get_length ();
6627 else
6628 gdb_assert (this_cu->length == cu->header.get_length ());
6629 this_cu->dwarf_version = cu->header.version;
6630 }
6631 }
6632
6633 /* Skip dummy compilation units. */
6634 if (info_ptr >= begin_info_ptr + this_cu->length
6635 || peek_abbrev_code (abfd, info_ptr) == 0)
6636 {
6637 dummy_p = true;
6638 return;
6639 }
6640
6641 /* If we don't have them yet, read the abbrevs for this compilation unit.
6642 And if we need to read them now, make sure they're freed when we're
6643 done. */
6644 if (abbrev_table != NULL)
6645 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6646 else
6647 {
6648 abbrev_section->read (objfile);
6649 m_abbrev_table_holder
6650 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6651 abbrev_table = m_abbrev_table_holder.get ();
6652 }
6653
6654 /* Read the top level CU/TU die. */
6655 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6656 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6657
6658 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6659 {
6660 dummy_p = true;
6661 return;
6662 }
6663
6664 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6665 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6666 table from the DWO file and pass the ownership over to us. It will be
6667 referenced from READER, so we must make sure to free it after we're done
6668 with READER.
6669
6670 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6671 DWO CU, that this test will fail (the attribute will not be present). */
6672 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6673 if (dwo_name != nullptr)
6674 {
6675 struct dwo_unit *dwo_unit;
6676 struct die_info *dwo_comp_unit_die;
6677
6678 if (comp_unit_die->has_children)
6679 {
6680 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6681 " has children (offset %s) [in module %s]"),
6682 sect_offset_str (this_cu->sect_off),
6683 bfd_get_filename (abfd));
6684 }
6685 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6686 if (dwo_unit != NULL)
6687 {
6688 if (read_cutu_die_from_dwo (cu, dwo_unit,
6689 comp_unit_die, NULL,
6690 this, &info_ptr,
6691 &dwo_comp_unit_die,
6692 &m_dwo_abbrev_table) == 0)
6693 {
6694 /* Dummy die. */
6695 dummy_p = true;
6696 return;
6697 }
6698 comp_unit_die = dwo_comp_unit_die;
6699 }
6700 else
6701 {
6702 /* Yikes, we couldn't find the rest of the DIE, we only have
6703 the stub. A complaint has already been logged. There's
6704 not much more we can do except pass on the stub DIE to
6705 die_reader_func. We don't want to throw an error on bad
6706 debug info. */
6707 }
6708 }
6709 }
6710
6711 void
6712 cutu_reader::keep ()
6713 {
6714 /* Done, clean up. */
6715 gdb_assert (!dummy_p);
6716 if (m_new_cu != NULL)
6717 {
6718 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6719 now. */
6720 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6721 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6722 }
6723 }
6724
6725 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6726 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6727 assumed to have already done the lookup to find the DWO file).
6728
6729 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6730 THIS_CU->is_debug_types, but nothing else.
6731
6732 We fill in THIS_CU->length.
6733
6734 THIS_CU->cu is always freed when done.
6735 This is done in order to not leave THIS_CU->cu in a state where we have
6736 to care whether it refers to the "main" CU or the DWO CU.
6737
6738 When parent_cu is passed, it is used to provide a default value for
6739 str_offsets_base and addr_base from the parent. */
6740
6741 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6742 dwarf2_per_objfile *per_objfile,
6743 struct dwarf2_cu *parent_cu,
6744 struct dwo_file *dwo_file)
6745 : die_reader_specs {},
6746 m_this_cu (this_cu)
6747 {
6748 struct objfile *objfile = per_objfile->objfile;
6749 struct dwarf2_section_info *section = this_cu->section;
6750 bfd *abfd = section->get_bfd_owner ();
6751 struct dwarf2_section_info *abbrev_section;
6752 const gdb_byte *begin_info_ptr, *info_ptr;
6753
6754 if (dwarf_die_debug)
6755 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6756 this_cu->is_debug_types ? "type" : "comp",
6757 sect_offset_str (this_cu->sect_off));
6758
6759 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6760
6761 abbrev_section = (dwo_file != NULL
6762 ? &dwo_file->sections.abbrev
6763 : get_abbrev_section_for_cu (this_cu));
6764
6765 /* This is cheap if the section is already read in. */
6766 section->read (objfile);
6767
6768 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6769
6770 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6771 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6772 section, abbrev_section, info_ptr,
6773 (this_cu->is_debug_types
6774 ? rcuh_kind::TYPE
6775 : rcuh_kind::COMPILE));
6776
6777 if (parent_cu != nullptr)
6778 {
6779 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6780 m_new_cu->addr_base = parent_cu->addr_base;
6781 }
6782 this_cu->length = m_new_cu->header.get_length ();
6783
6784 /* Skip dummy compilation units. */
6785 if (info_ptr >= begin_info_ptr + this_cu->length
6786 || peek_abbrev_code (abfd, info_ptr) == 0)
6787 {
6788 dummy_p = true;
6789 return;
6790 }
6791
6792 abbrev_section->read (objfile);
6793 m_abbrev_table_holder
6794 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6795
6796 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6797 m_abbrev_table_holder.get ());
6798 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6799 }
6800
6801 \f
6802 /* Type Unit Groups.
6803
6804 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6805 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6806 so that all types coming from the same compilation (.o file) are grouped
6807 together. A future step could be to put the types in the same symtab as
6808 the CU the types ultimately came from. */
6809
6810 static hashval_t
6811 hash_type_unit_group (const void *item)
6812 {
6813 const struct type_unit_group *tu_group
6814 = (const struct type_unit_group *) item;
6815
6816 return hash_stmt_list_entry (&tu_group->hash);
6817 }
6818
6819 static int
6820 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6821 {
6822 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6823 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6824
6825 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6826 }
6827
6828 /* Allocate a hash table for type unit groups. */
6829
6830 static htab_up
6831 allocate_type_unit_groups_table ()
6832 {
6833 return htab_up (htab_create_alloc (3,
6834 hash_type_unit_group,
6835 eq_type_unit_group,
6836 htab_delete_entry<type_unit_group>,
6837 xcalloc, xfree));
6838 }
6839
6840 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6841 partial symtabs. We combine several TUs per psymtab to not let the size
6842 of any one psymtab grow too big. */
6843 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6844 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6845
6846 /* Helper routine for get_type_unit_group.
6847 Create the type_unit_group object used to hold one or more TUs. */
6848
6849 static std::unique_ptr<type_unit_group>
6850 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6851 {
6852 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6853 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6854
6855 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6856 tu_group->per_bfd = per_bfd;
6857
6858 if (per_bfd->using_index)
6859 {
6860 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6861 struct dwarf2_per_cu_quick_data);
6862 }
6863 else
6864 {
6865 unsigned int line_offset = to_underlying (line_offset_struct);
6866 dwarf2_psymtab *pst;
6867 std::string name;
6868
6869 /* Give the symtab a useful name for debug purposes. */
6870 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6871 name = string_printf ("<type_units_%d>",
6872 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6873 else
6874 name = string_printf ("<type_units_at_0x%x>", line_offset);
6875
6876 pst = create_partial_symtab (tu_group.get (), per_objfile,
6877 name.c_str ());
6878 pst->anonymous = true;
6879 }
6880
6881 tu_group->hash.dwo_unit = cu->dwo_unit;
6882 tu_group->hash.line_sect_off = line_offset_struct;
6883
6884 return tu_group;
6885 }
6886
6887 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6888 STMT_LIST is a DW_AT_stmt_list attribute. */
6889
6890 static struct type_unit_group *
6891 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6892 {
6893 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6894 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6895 struct type_unit_group *tu_group;
6896 void **slot;
6897 unsigned int line_offset;
6898 struct type_unit_group type_unit_group_for_lookup;
6899
6900 if (per_objfile->per_bfd->type_unit_groups == NULL)
6901 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6902
6903 /* Do we need to create a new group, or can we use an existing one? */
6904
6905 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6906 {
6907 line_offset = stmt_list->as_unsigned ();
6908 ++tu_stats->nr_symtab_sharers;
6909 }
6910 else
6911 {
6912 /* Ugh, no stmt_list. Rare, but we have to handle it.
6913 We can do various things here like create one group per TU or
6914 spread them over multiple groups to split up the expansion work.
6915 To avoid worst case scenarios (too many groups or too large groups)
6916 we, umm, group them in bunches. */
6917 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6918 | (tu_stats->nr_stmt_less_type_units
6919 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6920 ++tu_stats->nr_stmt_less_type_units;
6921 }
6922
6923 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6924 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6925 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6926 &type_unit_group_for_lookup, INSERT);
6927 if (*slot == nullptr)
6928 {
6929 sect_offset line_offset_struct = (sect_offset) line_offset;
6930 std::unique_ptr<type_unit_group> grp
6931 = create_type_unit_group (cu, line_offset_struct);
6932 *slot = grp.release ();
6933 ++tu_stats->nr_symtabs;
6934 }
6935
6936 tu_group = (struct type_unit_group *) *slot;
6937 gdb_assert (tu_group != nullptr);
6938 return tu_group;
6939 }
6940 \f
6941 /* Partial symbol tables. */
6942
6943 /* Create a psymtab named NAME and assign it to PER_CU.
6944
6945 The caller must fill in the following details:
6946 dirname, textlow, texthigh. */
6947
6948 static dwarf2_psymtab *
6949 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6950 dwarf2_per_objfile *per_objfile,
6951 const char *name)
6952 {
6953 dwarf2_psymtab *pst
6954 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6955 per_objfile->objfile->per_bfd, per_cu);
6956
6957 pst->psymtabs_addrmap_supported = true;
6958
6959 /* This is the glue that links PST into GDB's symbol API. */
6960 per_cu->v.psymtab = pst;
6961
6962 return pst;
6963 }
6964
6965 /* DIE reader function for process_psymtab_comp_unit. */
6966
6967 static void
6968 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6969 const gdb_byte *info_ptr,
6970 struct die_info *comp_unit_die,
6971 enum language pretend_language)
6972 {
6973 struct dwarf2_cu *cu = reader->cu;
6974 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6975 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6976 struct objfile *objfile = per_objfile->objfile;
6977 struct gdbarch *gdbarch = objfile->arch ();
6978 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6979 CORE_ADDR baseaddr;
6980 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6981 dwarf2_psymtab *pst;
6982 enum pc_bounds_kind cu_bounds_kind;
6983
6984 gdb_assert (! per_cu->is_debug_types);
6985
6986 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
6987
6988 /* Allocate a new partial symbol table structure. */
6989 static const char artificial[] = "<artificial>";
6990 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
6991 if (strcmp (fnd.get_name (), artificial) == 0)
6992 {
6993 gdb::unique_xmalloc_ptr<char> debug_filename
6994 (concat (artificial, "@",
6995 sect_offset_str (per_cu->sect_off),
6996 (char *) NULL));
6997 fnd.set_name (std::move (debug_filename));
6998 }
6999
7000 pst = create_partial_symtab (per_cu, per_objfile, fnd.get_name ());
7001
7002 /* This must be done before calling dwarf2_build_include_psymtabs. */
7003 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7004
7005 baseaddr = objfile->text_section_offset ();
7006
7007 dwarf2_find_base_address (comp_unit_die, cu);
7008
7009 /* Possibly set the default values of LOWPC and HIGHPC from
7010 `DW_AT_ranges'. */
7011 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7012 &best_highpc, cu, pst);
7013 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7014 {
7015 CORE_ADDR low
7016 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7017 - baseaddr);
7018 CORE_ADDR high
7019 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7020 - baseaddr - 1);
7021 /* Store the contiguous range if it is not empty; it can be
7022 empty for CUs with no code. */
7023 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7024 low, high, pst);
7025 }
7026
7027 /* Check if comp unit has_children.
7028 If so, read the rest of the partial symbols from this comp unit.
7029 If not, there's no more debug_info for this comp unit. */
7030 if (comp_unit_die->has_children)
7031 {
7032 struct partial_die_info *first_die;
7033 CORE_ADDR lowpc, highpc;
7034
7035 lowpc = ((CORE_ADDR) -1);
7036 highpc = ((CORE_ADDR) 0);
7037
7038 first_die = load_partial_dies (reader, info_ptr, 1);
7039
7040 scan_partial_symbols (first_die, &lowpc, &highpc,
7041 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7042
7043 /* If we didn't find a lowpc, set it to highpc to avoid
7044 complaints from `maint check'. */
7045 if (lowpc == ((CORE_ADDR) -1))
7046 lowpc = highpc;
7047
7048 /* If the compilation unit didn't have an explicit address range,
7049 then use the information extracted from its child dies. */
7050 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7051 {
7052 best_lowpc = lowpc;
7053 best_highpc = highpc;
7054 }
7055 }
7056 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7057 best_lowpc + baseaddr)
7058 - baseaddr);
7059 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7060 best_highpc + baseaddr)
7061 - baseaddr);
7062
7063 pst->end ();
7064
7065 if (!cu->per_cu->imported_symtabs_empty ())
7066 {
7067 int i;
7068 int len = cu->per_cu->imported_symtabs_size ();
7069
7070 /* Fill in 'dependencies' here; we fill in 'users' in a
7071 post-pass. */
7072 pst->number_of_dependencies = len;
7073 pst->dependencies
7074 = per_bfd->partial_symtabs->allocate_dependencies (len);
7075 for (i = 0; i < len; ++i)
7076 {
7077 pst->dependencies[i]
7078 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7079 }
7080
7081 cu->per_cu->imported_symtabs_free ();
7082 }
7083
7084 /* Get the list of files included in the current compilation unit,
7085 and build a psymtab for each of them. */
7086 dwarf2_build_include_psymtabs (cu, comp_unit_die, fnd, pst);
7087
7088 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7089 ", %d global, %d static syms",
7090 per_cu->is_debug_types ? "type" : "comp",
7091 sect_offset_str (per_cu->sect_off),
7092 paddress (gdbarch, pst->text_low (objfile)),
7093 paddress (gdbarch, pst->text_high (objfile)),
7094 (int) pst->global_psymbols.size (),
7095 (int) pst->static_psymbols.size ());
7096 }
7097
7098 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7099 Process compilation unit THIS_CU for a psymtab. */
7100
7101 static void
7102 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7103 dwarf2_per_objfile *per_objfile,
7104 bool want_partial_unit,
7105 enum language pretend_language)
7106 {
7107 /* If this compilation unit was already read in, free the
7108 cached copy in order to read it in again. This is
7109 necessary because we skipped some symbols when we first
7110 read in the compilation unit (see load_partial_dies).
7111 This problem could be avoided, but the benefit is unclear. */
7112 per_objfile->remove_cu (this_cu);
7113
7114 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7115
7116 if (reader.comp_unit_die == nullptr)
7117 return;
7118
7119 switch (reader.comp_unit_die->tag)
7120 {
7121 case DW_TAG_compile_unit:
7122 this_cu->unit_type = DW_UT_compile;
7123 break;
7124 case DW_TAG_partial_unit:
7125 this_cu->unit_type = DW_UT_partial;
7126 break;
7127 case DW_TAG_type_unit:
7128 this_cu->unit_type = DW_UT_type;
7129 break;
7130 default:
7131 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7132 dwarf_tag_name (reader.comp_unit_die->tag),
7133 sect_offset_str (reader.cu->per_cu->sect_off),
7134 objfile_name (per_objfile->objfile));
7135 }
7136
7137 if (reader.dummy_p)
7138 {
7139 /* Nothing. */
7140 }
7141 else if (this_cu->is_debug_types)
7142 build_type_psymtabs_reader (&reader, reader.info_ptr,
7143 reader.comp_unit_die);
7144 else if (want_partial_unit
7145 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7146 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7147 reader.comp_unit_die,
7148 pretend_language);
7149
7150 /* Age out any secondary CUs. */
7151 per_objfile->age_comp_units ();
7152 }
7153
7154 /* Reader function for build_type_psymtabs. */
7155
7156 static void
7157 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7158 const gdb_byte *info_ptr,
7159 struct die_info *type_unit_die)
7160 {
7161 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7162 struct dwarf2_cu *cu = reader->cu;
7163 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7164 struct signatured_type *sig_type;
7165 struct type_unit_group *tu_group;
7166 struct attribute *attr;
7167 struct partial_die_info *first_die;
7168 CORE_ADDR lowpc, highpc;
7169 dwarf2_psymtab *pst;
7170
7171 gdb_assert (per_cu->is_debug_types);
7172 sig_type = (struct signatured_type *) per_cu;
7173
7174 if (! type_unit_die->has_children)
7175 return;
7176
7177 attr = type_unit_die->attr (DW_AT_stmt_list);
7178 tu_group = get_type_unit_group (cu, attr);
7179
7180 if (tu_group->tus == nullptr)
7181 tu_group->tus = new std::vector<signatured_type *>;
7182 tu_group->tus->push_back (sig_type);
7183
7184 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7185 pst = create_partial_symtab (per_cu, per_objfile, "");
7186 pst->anonymous = true;
7187
7188 first_die = load_partial_dies (reader, info_ptr, 1);
7189
7190 lowpc = (CORE_ADDR) -1;
7191 highpc = (CORE_ADDR) 0;
7192 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7193
7194 pst->end ();
7195 }
7196
7197 /* Struct used to sort TUs by their abbreviation table offset. */
7198
7199 struct tu_abbrev_offset
7200 {
7201 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7202 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7203 {}
7204
7205 /* This is used when sorting. */
7206 bool operator< (const tu_abbrev_offset &other) const
7207 {
7208 return abbrev_offset < other.abbrev_offset;
7209 }
7210
7211 signatured_type *sig_type;
7212 sect_offset abbrev_offset;
7213 };
7214
7215 /* Efficiently read all the type units.
7216
7217 The efficiency is because we sort TUs by the abbrev table they use and
7218 only read each abbrev table once. In one program there are 200K TUs
7219 sharing 8K abbrev tables.
7220
7221 The main purpose of this function is to support building the
7222 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7223 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7224 can collapse the search space by grouping them by stmt_list.
7225 The savings can be significant, in the same program from above the 200K TUs
7226 share 8K stmt_list tables.
7227
7228 FUNC is expected to call get_type_unit_group, which will create the
7229 struct type_unit_group if necessary and add it to
7230 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7231
7232 static void
7233 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7234 {
7235 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7236 abbrev_table_up abbrev_table;
7237 sect_offset abbrev_offset;
7238
7239 /* It's up to the caller to not call us multiple times. */
7240 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7241
7242 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7243 return;
7244
7245 /* TUs typically share abbrev tables, and there can be way more TUs than
7246 abbrev tables. Sort by abbrev table to reduce the number of times we
7247 read each abbrev table in.
7248 Alternatives are to punt or to maintain a cache of abbrev tables.
7249 This is simpler and efficient enough for now.
7250
7251 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7252 symtab to use). Typically TUs with the same abbrev offset have the same
7253 stmt_list value too so in practice this should work well.
7254
7255 The basic algorithm here is:
7256
7257 sort TUs by abbrev table
7258 for each TU with same abbrev table:
7259 read abbrev table if first user
7260 read TU top level DIE
7261 [IWBN if DWO skeletons had DW_AT_stmt_list]
7262 call FUNC */
7263
7264 dwarf_read_debug_printf ("Building type unit groups ...");
7265
7266 /* Sort in a separate table to maintain the order of all_comp_units
7267 for .gdb_index: TU indices directly index all_type_units. */
7268 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7269 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7270
7271 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7272 {
7273 if (cu->is_debug_types)
7274 {
7275 auto sig_type = static_cast<signatured_type *> (cu.get ());
7276 sorted_by_abbrev.emplace_back
7277 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7278 sig_type->sect_off));
7279 }
7280 }
7281
7282 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7283
7284 abbrev_offset = (sect_offset) ~(unsigned) 0;
7285
7286 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7287 {
7288 /* Switch to the next abbrev table if necessary. */
7289 if (abbrev_table == NULL
7290 || tu.abbrev_offset != abbrev_offset)
7291 {
7292 abbrev_offset = tu.abbrev_offset;
7293 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7294 abbrev_table =
7295 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7296 ++tu_stats->nr_uniq_abbrev_tables;
7297 }
7298
7299 cutu_reader reader (tu.sig_type, per_objfile,
7300 abbrev_table.get (), nullptr, false);
7301 if (!reader.dummy_p)
7302 build_type_psymtabs_reader (&reader, reader.info_ptr,
7303 reader.comp_unit_die);
7304 }
7305 }
7306
7307 /* Print collected type unit statistics. */
7308
7309 static void
7310 print_tu_stats (dwarf2_per_objfile *per_objfile)
7311 {
7312 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7313
7314 dwarf_read_debug_printf ("Type unit statistics:");
7315 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7316 dwarf_read_debug_printf (" %d uniq abbrev tables",
7317 tu_stats->nr_uniq_abbrev_tables);
7318 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7319 tu_stats->nr_symtabs);
7320 dwarf_read_debug_printf (" %d symtab sharers",
7321 tu_stats->nr_symtab_sharers);
7322 dwarf_read_debug_printf (" %d type units without a stmt_list",
7323 tu_stats->nr_stmt_less_type_units);
7324 dwarf_read_debug_printf (" %d all_type_units reallocs",
7325 tu_stats->nr_all_type_units_reallocs);
7326 }
7327
7328 /* Traversal function for build_type_psymtabs. */
7329
7330 static int
7331 build_type_psymtab_dependencies (void **slot, void *info)
7332 {
7333 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7334 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7335 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7336 dwarf2_psymtab *pst = tu_group->v.psymtab;
7337 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7338 int i;
7339
7340 gdb_assert (len > 0);
7341 gdb_assert (tu_group->type_unit_group_p ());
7342
7343 pst->number_of_dependencies = len;
7344 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7345 for (i = 0; i < len; ++i)
7346 {
7347 struct signatured_type *iter = tu_group->tus->at (i);
7348 gdb_assert (iter->is_debug_types);
7349 pst->dependencies[i] = iter->v.psymtab;
7350 iter->type_unit_group = tu_group;
7351 }
7352
7353 delete tu_group->tus;
7354 tu_group->tus = nullptr;
7355
7356 return 1;
7357 }
7358
7359 /* Traversal function for process_skeletonless_type_unit.
7360 Read a TU in a DWO file and build partial symbols for it. */
7361
7362 static int
7363 process_skeletonless_type_unit (void **slot, void *info)
7364 {
7365 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7366 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7367
7368 /* If this TU doesn't exist in the global table, add it and read it in. */
7369
7370 if (per_objfile->per_bfd->signatured_types == NULL)
7371 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7372
7373 signatured_type find_entry (dwo_unit->signature);
7374 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7375 &find_entry, INSERT);
7376 /* If we've already seen this type there's nothing to do. What's happening
7377 is we're doing our own version of comdat-folding here. */
7378 if (*slot != NULL)
7379 return 1;
7380
7381 /* This does the job that create_all_comp_units would have done for
7382 this TU. */
7383 signatured_type *entry
7384 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7385 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7386 *slot = entry;
7387
7388 /* This does the job that build_type_psymtabs would have done. */
7389 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7390 if (!reader.dummy_p)
7391 build_type_psymtabs_reader (&reader, reader.info_ptr,
7392 reader.comp_unit_die);
7393
7394 return 1;
7395 }
7396
7397 /* Traversal function for process_skeletonless_type_units. */
7398
7399 static int
7400 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7401 {
7402 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7403
7404 if (dwo_file->tus != NULL)
7405 htab_traverse_noresize (dwo_file->tus.get (),
7406 process_skeletonless_type_unit, info);
7407
7408 return 1;
7409 }
7410
7411 /* Scan all TUs of DWO files, verifying we've processed them.
7412 This is needed in case a TU was emitted without its skeleton.
7413 Note: This can't be done until we know what all the DWO files are. */
7414
7415 static void
7416 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7417 {
7418 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7419 if (get_dwp_file (per_objfile) == NULL
7420 && per_objfile->per_bfd->dwo_files != NULL)
7421 {
7422 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7423 process_dwo_file_for_skeletonless_type_units,
7424 per_objfile);
7425 }
7426 }
7427
7428 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7429
7430 static void
7431 set_partial_user (dwarf2_per_objfile *per_objfile)
7432 {
7433 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7434 {
7435 dwarf2_psymtab *pst = per_cu->v.psymtab;
7436
7437 if (pst == NULL)
7438 continue;
7439
7440 for (int j = 0; j < pst->number_of_dependencies; ++j)
7441 {
7442 /* Set the 'user' field only if it is not already set. */
7443 if (pst->dependencies[j]->user == NULL)
7444 pst->dependencies[j]->user = pst;
7445 }
7446 }
7447 }
7448
7449 /* Build the partial symbol table by doing a quick pass through the
7450 .debug_info and .debug_abbrev sections. */
7451
7452 static void
7453 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7454 {
7455 struct objfile *objfile = per_objfile->objfile;
7456 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7457
7458 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7459 objfile_name (objfile));
7460
7461 scoped_restore restore_reading_psyms
7462 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7463
7464 per_bfd->info.read (objfile);
7465
7466 /* Any cached compilation units will be linked by the per-objfile
7467 read_in_chain. Make sure to free them when we're done. */
7468 free_cached_comp_units freer (per_objfile);
7469
7470 create_all_comp_units (per_objfile);
7471 build_type_psymtabs (per_objfile);
7472
7473 /* Create a temporary address map on a temporary obstack. We later
7474 copy this to the final obstack. */
7475 auto_obstack temp_obstack;
7476
7477 scoped_restore save_psymtabs_addrmap
7478 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7479 addrmap_create_mutable (&temp_obstack));
7480
7481 for (const auto &per_cu : per_bfd->all_comp_units)
7482 {
7483 if (per_cu->v.psymtab != NULL)
7484 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7485 continue;
7486 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7487 language_minimal);
7488 }
7489
7490 /* This has to wait until we read the CUs, we need the list of DWOs. */
7491 process_skeletonless_type_units (per_objfile);
7492
7493 /* Now that all TUs have been processed we can fill in the dependencies. */
7494 if (per_bfd->type_unit_groups != NULL)
7495 {
7496 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7497 build_type_psymtab_dependencies, per_objfile);
7498 }
7499
7500 if (dwarf_read_debug > 0)
7501 print_tu_stats (per_objfile);
7502
7503 set_partial_user (per_objfile);
7504
7505 per_bfd->partial_symtabs->psymtabs_addrmap
7506 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7507 per_bfd->partial_symtabs->obstack ());
7508 /* At this point we want to keep the address map. */
7509 save_psymtabs_addrmap.release ();
7510
7511 dwarf_read_debug_printf ("Done building psymtabs of %s",
7512 objfile_name (objfile));
7513 }
7514
7515 /* Load the partial DIEs for a secondary CU into memory.
7516 This is also used when rereading a primary CU with load_all_dies. */
7517
7518 static void
7519 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7520 dwarf2_per_objfile *per_objfile,
7521 dwarf2_cu *existing_cu)
7522 {
7523 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7524
7525 if (!reader.dummy_p)
7526 {
7527 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7528 language_minimal);
7529
7530 /* Check if comp unit has_children.
7531 If so, read the rest of the partial symbols from this comp unit.
7532 If not, there's no more debug_info for this comp unit. */
7533 if (reader.comp_unit_die->has_children)
7534 load_partial_dies (&reader, reader.info_ptr, 0);
7535
7536 reader.keep ();
7537 }
7538 }
7539
7540 static void
7541 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7542 struct dwarf2_section_info *section,
7543 struct dwarf2_section_info *abbrev_section,
7544 unsigned int is_dwz,
7545 htab_up &types_htab,
7546 rcuh_kind section_kind)
7547 {
7548 const gdb_byte *info_ptr;
7549 struct objfile *objfile = per_objfile->objfile;
7550
7551 dwarf_read_debug_printf ("Reading %s for %s",
7552 section->get_name (),
7553 section->get_file_name ());
7554
7555 section->read (objfile);
7556
7557 info_ptr = section->buffer;
7558
7559 while (info_ptr < section->buffer + section->size)
7560 {
7561 dwarf2_per_cu_data_up this_cu;
7562
7563 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7564
7565 comp_unit_head cu_header;
7566 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7567 abbrev_section, info_ptr,
7568 section_kind);
7569
7570 /* Save the compilation unit for later lookup. */
7571 if (cu_header.unit_type != DW_UT_type)
7572 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7573 else
7574 {
7575 if (types_htab == nullptr)
7576 types_htab = allocate_signatured_type_table ();
7577
7578 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7579 (cu_header.signature);
7580 signatured_type *sig_ptr = sig_type.get ();
7581 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7582 this_cu.reset (sig_type.release ());
7583
7584 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7585 gdb_assert (slot != nullptr);
7586 if (*slot != nullptr)
7587 complaint (_("debug type entry at offset %s is duplicate to"
7588 " the entry at offset %s, signature %s"),
7589 sect_offset_str (sect_off),
7590 sect_offset_str (sig_ptr->sect_off),
7591 hex_string (sig_ptr->signature));
7592 *slot = sig_ptr;
7593 }
7594 this_cu->sect_off = sect_off;
7595 this_cu->length = cu_header.length + cu_header.initial_length_size;
7596 this_cu->is_dwz = is_dwz;
7597 this_cu->section = section;
7598
7599 info_ptr = info_ptr + this_cu->length;
7600 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7601 }
7602 }
7603
7604 /* Create a list of all compilation units in OBJFILE.
7605 This is only done for -readnow and building partial symtabs. */
7606
7607 static void
7608 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7609 {
7610 htab_up types_htab;
7611
7612 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7613 &per_objfile->per_bfd->abbrev, 0,
7614 types_htab, rcuh_kind::COMPILE);
7615 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7616 read_comp_units_from_section (per_objfile, &section,
7617 &per_objfile->per_bfd->abbrev, 0,
7618 types_htab, rcuh_kind::TYPE);
7619
7620 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7621 if (dwz != NULL)
7622 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7623 types_htab, rcuh_kind::COMPILE);
7624
7625 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7626 }
7627
7628 /* Process all loaded DIEs for compilation unit CU, starting at
7629 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7630 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7631 DW_AT_ranges). See the comments of add_partial_subprogram on how
7632 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7633
7634 static void
7635 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7636 CORE_ADDR *highpc, int set_addrmap,
7637 struct dwarf2_cu *cu)
7638 {
7639 struct partial_die_info *pdi;
7640
7641 /* Now, march along the PDI's, descending into ones which have
7642 interesting children but skipping the children of the other ones,
7643 until we reach the end of the compilation unit. */
7644
7645 pdi = first_die;
7646
7647 while (pdi != NULL)
7648 {
7649 pdi->fixup (cu);
7650
7651 /* Anonymous namespaces or modules have no name but have interesting
7652 children, so we need to look at them. Ditto for anonymous
7653 enums. */
7654
7655 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7656 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7657 || pdi->tag == DW_TAG_imported_unit
7658 || pdi->tag == DW_TAG_inlined_subroutine)
7659 {
7660 switch (pdi->tag)
7661 {
7662 case DW_TAG_subprogram:
7663 case DW_TAG_inlined_subroutine:
7664 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7665 if (cu->per_cu->lang == language_cplus)
7666 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7667 set_addrmap, cu);
7668 break;
7669 case DW_TAG_constant:
7670 case DW_TAG_variable:
7671 case DW_TAG_typedef:
7672 case DW_TAG_union_type:
7673 if (!pdi->is_declaration
7674 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7675 {
7676 add_partial_symbol (pdi, cu);
7677 }
7678 break;
7679 case DW_TAG_class_type:
7680 case DW_TAG_interface_type:
7681 case DW_TAG_structure_type:
7682 if (!pdi->is_declaration)
7683 {
7684 add_partial_symbol (pdi, cu);
7685 }
7686 if ((cu->per_cu->lang == language_rust
7687 || cu->per_cu->lang == language_cplus)
7688 && pdi->has_children)
7689 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7690 set_addrmap, cu);
7691 break;
7692 case DW_TAG_enumeration_type:
7693 if (!pdi->is_declaration)
7694 add_partial_enumeration (pdi, cu);
7695 break;
7696 case DW_TAG_base_type:
7697 case DW_TAG_generic_subrange:
7698 case DW_TAG_subrange_type:
7699 /* File scope base type definitions are added to the partial
7700 symbol table. */
7701 add_partial_symbol (pdi, cu);
7702 break;
7703 case DW_TAG_namespace:
7704 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7705 break;
7706 case DW_TAG_module:
7707 if (!pdi->is_declaration)
7708 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7709 break;
7710 case DW_TAG_imported_unit:
7711 {
7712 struct dwarf2_per_cu_data *per_cu;
7713
7714 /* For now we don't handle imported units in type units. */
7715 if (cu->per_cu->is_debug_types)
7716 {
7717 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7718 " supported in type units [in module %s]"),
7719 objfile_name (cu->per_objfile->objfile));
7720 }
7721
7722 per_cu = dwarf2_find_containing_comp_unit
7723 (pdi->d.sect_off, pdi->is_dwz,
7724 cu->per_objfile->per_bfd);
7725
7726 /* Go read the partial unit, if needed. */
7727 if (per_cu->v.psymtab == NULL)
7728 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7729 cu->per_cu->lang);
7730
7731 if (pdi->die_parent == nullptr
7732 && per_cu->unit_type == DW_UT_compile
7733 && per_cu->lang == language_cplus)
7734 /* Regard import as hint. See corresponding code in
7735 process_imported_unit_die. */
7736 break;
7737
7738 cu->per_cu->imported_symtabs_push (per_cu);
7739 }
7740 break;
7741 case DW_TAG_imported_declaration:
7742 add_partial_symbol (pdi, cu);
7743 break;
7744 default:
7745 break;
7746 }
7747 }
7748
7749 /* If the die has a sibling, skip to the sibling. */
7750
7751 pdi = pdi->die_sibling;
7752 }
7753 }
7754
7755 /* Functions used to compute the fully scoped name of a partial DIE.
7756
7757 Normally, this is simple. For C++, the parent DIE's fully scoped
7758 name is concatenated with "::" and the partial DIE's name.
7759 Enumerators are an exception; they use the scope of their parent
7760 enumeration type, i.e. the name of the enumeration type is not
7761 prepended to the enumerator.
7762
7763 There are two complexities. One is DW_AT_specification; in this
7764 case "parent" means the parent of the target of the specification,
7765 instead of the direct parent of the DIE. The other is compilers
7766 which do not emit DW_TAG_namespace; in this case we try to guess
7767 the fully qualified name of structure types from their members'
7768 linkage names. This must be done using the DIE's children rather
7769 than the children of any DW_AT_specification target. We only need
7770 to do this for structures at the top level, i.e. if the target of
7771 any DW_AT_specification (if any; otherwise the DIE itself) does not
7772 have a parent. */
7773
7774 /* Compute the scope prefix associated with PDI's parent, in
7775 compilation unit CU. The result will be allocated on CU's
7776 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7777 field. NULL is returned if no prefix is necessary. */
7778 static const char *
7779 partial_die_parent_scope (struct partial_die_info *pdi,
7780 struct dwarf2_cu *cu)
7781 {
7782 const char *grandparent_scope;
7783 struct partial_die_info *parent, *real_pdi;
7784
7785 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7786 then this means the parent of the specification DIE. */
7787
7788 real_pdi = pdi;
7789 while (real_pdi->has_specification)
7790 {
7791 auto res = find_partial_die (real_pdi->spec_offset,
7792 real_pdi->spec_is_dwz, cu);
7793 real_pdi = res.pdi;
7794 cu = res.cu;
7795 }
7796
7797 parent = real_pdi->die_parent;
7798 if (parent == NULL)
7799 return NULL;
7800
7801 if (parent->scope_set)
7802 return parent->scope;
7803
7804 parent->fixup (cu);
7805
7806 grandparent_scope = partial_die_parent_scope (parent, cu);
7807
7808 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7809 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7810 Work around this problem here. */
7811 if (cu->per_cu->lang == language_cplus
7812 && parent->tag == DW_TAG_namespace
7813 && strcmp (parent->name (cu), "::") == 0
7814 && grandparent_scope == NULL)
7815 {
7816 parent->scope = NULL;
7817 parent->scope_set = 1;
7818 return NULL;
7819 }
7820
7821 /* Nested subroutines in Fortran get a prefix. */
7822 if (pdi->tag == DW_TAG_enumerator)
7823 /* Enumerators should not get the name of the enumeration as a prefix. */
7824 parent->scope = grandparent_scope;
7825 else if (parent->tag == DW_TAG_namespace
7826 || parent->tag == DW_TAG_module
7827 || parent->tag == DW_TAG_structure_type
7828 || parent->tag == DW_TAG_class_type
7829 || parent->tag == DW_TAG_interface_type
7830 || parent->tag == DW_TAG_union_type
7831 || parent->tag == DW_TAG_enumeration_type
7832 || (cu->per_cu->lang == language_fortran
7833 && parent->tag == DW_TAG_subprogram
7834 && pdi->tag == DW_TAG_subprogram))
7835 {
7836 if (grandparent_scope == NULL)
7837 parent->scope = parent->name (cu);
7838 else
7839 parent->scope = typename_concat (&cu->comp_unit_obstack,
7840 grandparent_scope,
7841 parent->name (cu), 0, cu);
7842 }
7843 else
7844 {
7845 /* FIXME drow/2004-04-01: What should we be doing with
7846 function-local names? For partial symbols, we should probably be
7847 ignoring them. */
7848 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7849 dwarf_tag_name (parent->tag),
7850 sect_offset_str (pdi->sect_off));
7851 parent->scope = grandparent_scope;
7852 }
7853
7854 parent->scope_set = 1;
7855 return parent->scope;
7856 }
7857
7858 /* Return the fully scoped name associated with PDI, from compilation unit
7859 CU. The result will be allocated with malloc. */
7860
7861 static gdb::unique_xmalloc_ptr<char>
7862 partial_die_full_name (struct partial_die_info *pdi,
7863 struct dwarf2_cu *cu)
7864 {
7865 const char *parent_scope;
7866
7867 /* If this is a template instantiation, we can not work out the
7868 template arguments from partial DIEs. So, unfortunately, we have
7869 to go through the full DIEs. At least any work we do building
7870 types here will be reused if full symbols are loaded later. */
7871 if (pdi->has_template_arguments)
7872 {
7873 pdi->fixup (cu);
7874
7875 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7876 {
7877 struct die_info *die;
7878 struct attribute attr;
7879 struct dwarf2_cu *ref_cu = cu;
7880
7881 /* DW_FORM_ref_addr is using section offset. */
7882 attr.name = (enum dwarf_attribute) 0;
7883 attr.form = DW_FORM_ref_addr;
7884 attr.u.unsnd = to_underlying (pdi->sect_off);
7885 die = follow_die_ref (NULL, &attr, &ref_cu);
7886
7887 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7888 }
7889 }
7890
7891 parent_scope = partial_die_parent_scope (pdi, cu);
7892 if (parent_scope == NULL)
7893 return NULL;
7894 else
7895 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7896 pdi->name (cu),
7897 0, cu));
7898 }
7899
7900 static void
7901 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7902 {
7903 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7904 struct objfile *objfile = per_objfile->objfile;
7905 struct gdbarch *gdbarch = objfile->arch ();
7906 CORE_ADDR addr = 0;
7907 const char *actual_name = NULL;
7908 CORE_ADDR baseaddr;
7909
7910 baseaddr = objfile->text_section_offset ();
7911
7912 gdb::unique_xmalloc_ptr<char> built_actual_name
7913 = partial_die_full_name (pdi, cu);
7914 if (built_actual_name != NULL)
7915 actual_name = built_actual_name.get ();
7916
7917 if (actual_name == NULL)
7918 actual_name = pdi->name (cu);
7919
7920 partial_symbol psymbol;
7921 memset (&psymbol, 0, sizeof (psymbol));
7922 psymbol.ginfo.set_language (cu->per_cu->lang,
7923 &objfile->objfile_obstack);
7924 psymbol.ginfo.set_section_index (-1);
7925
7926 /* The code below indicates that the psymbol should be installed by
7927 setting this. */
7928 gdb::optional<psymbol_placement> where;
7929
7930 switch (pdi->tag)
7931 {
7932 case DW_TAG_inlined_subroutine:
7933 case DW_TAG_subprogram:
7934 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7935 - baseaddr);
7936 if (pdi->is_external
7937 || cu->per_cu->lang == language_ada
7938 || (cu->per_cu->lang == language_fortran
7939 && pdi->die_parent != NULL
7940 && pdi->die_parent->tag == DW_TAG_subprogram))
7941 {
7942 /* Normally, only "external" DIEs are part of the global scope.
7943 But in Ada and Fortran, we want to be able to access nested
7944 procedures globally. So all Ada and Fortran subprograms are
7945 stored in the global scope. */
7946 where = psymbol_placement::GLOBAL;
7947 }
7948 else
7949 where = psymbol_placement::STATIC;
7950
7951 psymbol.domain = VAR_DOMAIN;
7952 psymbol.aclass = LOC_BLOCK;
7953 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7954 psymbol.ginfo.set_value_address (addr);
7955
7956 if (pdi->main_subprogram && actual_name != NULL)
7957 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
7958 break;
7959 case DW_TAG_constant:
7960 psymbol.domain = VAR_DOMAIN;
7961 psymbol.aclass = LOC_STATIC;
7962 where = (pdi->is_external
7963 ? psymbol_placement::GLOBAL
7964 : psymbol_placement::STATIC);
7965 break;
7966 case DW_TAG_variable:
7967 if (pdi->d.locdesc)
7968 addr = decode_locdesc (pdi->d.locdesc, cu);
7969
7970 if (pdi->d.locdesc
7971 && addr == 0
7972 && !per_objfile->per_bfd->has_section_at_zero)
7973 {
7974 /* A global or static variable may also have been stripped
7975 out by the linker if unused, in which case its address
7976 will be nullified; do not add such variables into partial
7977 symbol table then. */
7978 }
7979 else if (pdi->is_external)
7980 {
7981 /* Global Variable.
7982 Don't enter into the minimal symbol tables as there is
7983 a minimal symbol table entry from the ELF symbols already.
7984 Enter into partial symbol table if it has a location
7985 descriptor or a type.
7986 If the location descriptor is missing, new_symbol will create
7987 a LOC_UNRESOLVED symbol, the address of the variable will then
7988 be determined from the minimal symbol table whenever the variable
7989 is referenced.
7990 The address for the partial symbol table entry is not
7991 used by GDB, but it comes in handy for debugging partial symbol
7992 table building. */
7993
7994 if (pdi->d.locdesc || pdi->has_type)
7995 {
7996 psymbol.domain = VAR_DOMAIN;
7997 psymbol.aclass = LOC_STATIC;
7998 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7999 psymbol.ginfo.set_value_address (addr);
8000 where = psymbol_placement::GLOBAL;
8001 }
8002 }
8003 else
8004 {
8005 int has_loc = pdi->d.locdesc != NULL;
8006
8007 /* Static Variable. Skip symbols whose value we cannot know (those
8008 without location descriptors or constant values). */
8009 if (!has_loc && !pdi->has_const_value)
8010 return;
8011
8012 psymbol.domain = VAR_DOMAIN;
8013 psymbol.aclass = LOC_STATIC;
8014 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8015 if (has_loc)
8016 psymbol.ginfo.set_value_address (addr);
8017 where = psymbol_placement::STATIC;
8018 }
8019 break;
8020 case DW_TAG_array_type:
8021 case DW_TAG_typedef:
8022 case DW_TAG_base_type:
8023 case DW_TAG_subrange_type:
8024 case DW_TAG_generic_subrange:
8025 psymbol.domain = VAR_DOMAIN;
8026 psymbol.aclass = LOC_TYPEDEF;
8027 where = psymbol_placement::STATIC;
8028 break;
8029 case DW_TAG_imported_declaration:
8030 case DW_TAG_namespace:
8031 psymbol.domain = VAR_DOMAIN;
8032 psymbol.aclass = LOC_TYPEDEF;
8033 where = psymbol_placement::GLOBAL;
8034 break;
8035 case DW_TAG_module:
8036 /* With Fortran 77 there might be a "BLOCK DATA" module
8037 available without any name. If so, we skip the module as it
8038 doesn't bring any value. */
8039 if (actual_name != nullptr)
8040 {
8041 psymbol.domain = MODULE_DOMAIN;
8042 psymbol.aclass = LOC_TYPEDEF;
8043 where = psymbol_placement::GLOBAL;
8044 }
8045 break;
8046 case DW_TAG_class_type:
8047 case DW_TAG_interface_type:
8048 case DW_TAG_structure_type:
8049 case DW_TAG_union_type:
8050 case DW_TAG_enumeration_type:
8051 /* Skip external references. The DWARF standard says in the section
8052 about "Structure, Union, and Class Type Entries": "An incomplete
8053 structure, union or class type is represented by a structure,
8054 union or class entry that does not have a byte size attribute
8055 and that has a DW_AT_declaration attribute." */
8056 if (!pdi->has_byte_size && pdi->is_declaration)
8057 return;
8058
8059 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8060 static vs. global. */
8061 psymbol.domain = STRUCT_DOMAIN;
8062 psymbol.aclass = LOC_TYPEDEF;
8063 where = (cu->per_cu->lang == language_cplus
8064 ? psymbol_placement::GLOBAL
8065 : psymbol_placement::STATIC);
8066 break;
8067 case DW_TAG_enumerator:
8068 psymbol.domain = VAR_DOMAIN;
8069 psymbol.aclass = LOC_CONST;
8070 where = (cu->per_cu->lang == language_cplus
8071 ? psymbol_placement::GLOBAL
8072 : psymbol_placement::STATIC);
8073 break;
8074 default:
8075 break;
8076 }
8077
8078 if (where.has_value ())
8079 {
8080 if (built_actual_name != nullptr)
8081 actual_name = objfile->intern (actual_name);
8082 if (pdi->linkage_name == nullptr
8083 || cu->per_cu->lang == language_ada)
8084 psymbol.ginfo.set_linkage_name (actual_name);
8085 else
8086 {
8087 psymbol.ginfo.set_demangled_name (actual_name,
8088 &objfile->objfile_obstack);
8089 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8090 }
8091 cu->per_cu->v.psymtab->add_psymbol
8092 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8093 objfile);
8094 }
8095 }
8096
8097 /* Read a partial die corresponding to a namespace; also, add a symbol
8098 corresponding to that namespace to the symbol table. NAMESPACE is
8099 the name of the enclosing namespace. */
8100
8101 static void
8102 add_partial_namespace (struct partial_die_info *pdi,
8103 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8104 int set_addrmap, struct dwarf2_cu *cu)
8105 {
8106 /* Add a symbol for the namespace. */
8107
8108 add_partial_symbol (pdi, cu);
8109
8110 /* Now scan partial symbols in that namespace. */
8111
8112 if (pdi->has_children)
8113 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8114 }
8115
8116 /* Read a partial die corresponding to a Fortran module. */
8117
8118 static void
8119 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8120 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8121 {
8122 /* Add a symbol for the namespace. */
8123
8124 add_partial_symbol (pdi, cu);
8125
8126 /* Now scan partial symbols in that module. */
8127
8128 if (pdi->has_children)
8129 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8130 }
8131
8132 static int
8133 dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
8134 dwarf2_psymtab *, dwarf_tag);
8135
8136 /* Read a partial die corresponding to a subprogram or an inlined
8137 subprogram and create a partial symbol for that subprogram.
8138 When the CU language allows it, this routine also defines a partial
8139 symbol for each nested subprogram that this subprogram contains.
8140 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8141 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8142
8143 PDI may also be a lexical block, in which case we simply search
8144 recursively for subprograms defined inside that lexical block.
8145 Again, this is only performed when the CU language allows this
8146 type of definitions. */
8147
8148 static void
8149 add_partial_subprogram (struct partial_die_info *pdi,
8150 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8151 int set_addrmap, struct dwarf2_cu *cu)
8152 {
8153 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8154 {
8155 if (pdi->has_pc_info)
8156 {
8157 if (pdi->lowpc < *lowpc)
8158 *lowpc = pdi->lowpc;
8159 if (pdi->highpc > *highpc)
8160 *highpc = pdi->highpc;
8161 if (set_addrmap)
8162 {
8163 struct objfile *objfile = cu->per_objfile->objfile;
8164 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8165 struct gdbarch *gdbarch = objfile->arch ();
8166 CORE_ADDR baseaddr;
8167 CORE_ADDR this_highpc;
8168 CORE_ADDR this_lowpc;
8169
8170 baseaddr = objfile->text_section_offset ();
8171 this_lowpc
8172 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8173 pdi->lowpc + baseaddr)
8174 - baseaddr);
8175 this_highpc
8176 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8177 pdi->highpc + baseaddr)
8178 - baseaddr);
8179 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8180 this_lowpc, this_highpc - 1,
8181 cu->per_cu->v.psymtab);
8182 }
8183 }
8184
8185 if (pdi->has_range_info
8186 && dwarf2_ranges_read (pdi->ranges_offset, &pdi->lowpc, &pdi->highpc,
8187 cu,
8188 set_addrmap ? cu->per_cu->v.psymtab : nullptr,
8189 pdi->tag))
8190 {
8191 if (pdi->lowpc < *lowpc)
8192 *lowpc = pdi->lowpc;
8193 if (pdi->highpc > *highpc)
8194 *highpc = pdi->highpc;
8195 }
8196
8197 if (pdi->has_pc_info || pdi->has_range_info
8198 || (!pdi->is_external && pdi->may_be_inlined))
8199 {
8200 if (!pdi->is_declaration)
8201 /* Ignore subprogram DIEs that do not have a name, they are
8202 illegal. Do not emit a complaint at this point, we will
8203 do so when we convert this psymtab into a symtab. */
8204 if (pdi->name (cu))
8205 add_partial_symbol (pdi, cu);
8206 }
8207 }
8208
8209 if (! pdi->has_children)
8210 return;
8211
8212 if (cu->per_cu->lang == language_ada
8213 || cu->per_cu->lang == language_fortran)
8214 {
8215 pdi = pdi->die_child;
8216 while (pdi != NULL)
8217 {
8218 pdi->fixup (cu);
8219 if (pdi->tag == DW_TAG_subprogram
8220 || pdi->tag == DW_TAG_inlined_subroutine
8221 || pdi->tag == DW_TAG_lexical_block)
8222 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8223 pdi = pdi->die_sibling;
8224 }
8225 }
8226 }
8227
8228 /* Read a partial die corresponding to an enumeration type. */
8229
8230 static void
8231 add_partial_enumeration (struct partial_die_info *enum_pdi,
8232 struct dwarf2_cu *cu)
8233 {
8234 struct partial_die_info *pdi;
8235
8236 if (enum_pdi->name (cu) != NULL)
8237 add_partial_symbol (enum_pdi, cu);
8238
8239 pdi = enum_pdi->die_child;
8240 while (pdi)
8241 {
8242 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8243 complaint (_("malformed enumerator DIE ignored"));
8244 else
8245 add_partial_symbol (pdi, cu);
8246 pdi = pdi->die_sibling;
8247 }
8248 }
8249
8250 /* Return the initial uleb128 in the die at INFO_PTR. */
8251
8252 static unsigned int
8253 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8254 {
8255 unsigned int bytes_read;
8256
8257 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8258 }
8259
8260 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8261 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8262
8263 Return the corresponding abbrev, or NULL if the number is zero (indicating
8264 an empty DIE). In either case *BYTES_READ will be set to the length of
8265 the initial number. */
8266
8267 static const struct abbrev_info *
8268 peek_die_abbrev (const die_reader_specs &reader,
8269 const gdb_byte *info_ptr, unsigned int *bytes_read)
8270 {
8271 dwarf2_cu *cu = reader.cu;
8272 bfd *abfd = reader.abfd;
8273 unsigned int abbrev_number
8274 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8275
8276 if (abbrev_number == 0)
8277 return NULL;
8278
8279 const abbrev_info *abbrev
8280 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8281 if (!abbrev)
8282 {
8283 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8284 " at offset %s [in module %s]"),
8285 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8286 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8287 }
8288
8289 return abbrev;
8290 }
8291
8292 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8293 Returns a pointer to the end of a series of DIEs, terminated by an empty
8294 DIE. Any children of the skipped DIEs will also be skipped. */
8295
8296 static const gdb_byte *
8297 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8298 {
8299 while (1)
8300 {
8301 unsigned int bytes_read;
8302 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8303 &bytes_read);
8304
8305 if (abbrev == NULL)
8306 return info_ptr + bytes_read;
8307 else
8308 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8309 }
8310 }
8311
8312 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8313 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8314 abbrev corresponding to that skipped uleb128 should be passed in
8315 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8316 children. */
8317
8318 static const gdb_byte *
8319 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8320 const struct abbrev_info *abbrev)
8321 {
8322 unsigned int bytes_read;
8323 struct attribute attr;
8324 bfd *abfd = reader->abfd;
8325 struct dwarf2_cu *cu = reader->cu;
8326 const gdb_byte *buffer = reader->buffer;
8327 const gdb_byte *buffer_end = reader->buffer_end;
8328 unsigned int form, i;
8329
8330 for (i = 0; i < abbrev->num_attrs; i++)
8331 {
8332 /* The only abbrev we care about is DW_AT_sibling. */
8333 if (abbrev->attrs[i].name == DW_AT_sibling)
8334 {
8335 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8336 if (attr.form == DW_FORM_ref_addr)
8337 complaint (_("ignoring absolute DW_AT_sibling"));
8338 else
8339 {
8340 sect_offset off = attr.get_ref_die_offset ();
8341 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8342
8343 if (sibling_ptr < info_ptr)
8344 complaint (_("DW_AT_sibling points backwards"));
8345 else if (sibling_ptr > reader->buffer_end)
8346 reader->die_section->overflow_complaint ();
8347 else
8348 return sibling_ptr;
8349 }
8350 }
8351
8352 /* If it isn't DW_AT_sibling, skip this attribute. */
8353 form = abbrev->attrs[i].form;
8354 skip_attribute:
8355 switch (form)
8356 {
8357 case DW_FORM_ref_addr:
8358 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8359 and later it is offset sized. */
8360 if (cu->header.version == 2)
8361 info_ptr += cu->header.addr_size;
8362 else
8363 info_ptr += cu->header.offset_size;
8364 break;
8365 case DW_FORM_GNU_ref_alt:
8366 info_ptr += cu->header.offset_size;
8367 break;
8368 case DW_FORM_addr:
8369 info_ptr += cu->header.addr_size;
8370 break;
8371 case DW_FORM_data1:
8372 case DW_FORM_ref1:
8373 case DW_FORM_flag:
8374 case DW_FORM_strx1:
8375 info_ptr += 1;
8376 break;
8377 case DW_FORM_flag_present:
8378 case DW_FORM_implicit_const:
8379 break;
8380 case DW_FORM_data2:
8381 case DW_FORM_ref2:
8382 case DW_FORM_strx2:
8383 info_ptr += 2;
8384 break;
8385 case DW_FORM_strx3:
8386 info_ptr += 3;
8387 break;
8388 case DW_FORM_data4:
8389 case DW_FORM_ref4:
8390 case DW_FORM_strx4:
8391 info_ptr += 4;
8392 break;
8393 case DW_FORM_data8:
8394 case DW_FORM_ref8:
8395 case DW_FORM_ref_sig8:
8396 info_ptr += 8;
8397 break;
8398 case DW_FORM_data16:
8399 info_ptr += 16;
8400 break;
8401 case DW_FORM_string:
8402 read_direct_string (abfd, info_ptr, &bytes_read);
8403 info_ptr += bytes_read;
8404 break;
8405 case DW_FORM_sec_offset:
8406 case DW_FORM_strp:
8407 case DW_FORM_GNU_strp_alt:
8408 info_ptr += cu->header.offset_size;
8409 break;
8410 case DW_FORM_exprloc:
8411 case DW_FORM_block:
8412 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8413 info_ptr += bytes_read;
8414 break;
8415 case DW_FORM_block1:
8416 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8417 break;
8418 case DW_FORM_block2:
8419 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8420 break;
8421 case DW_FORM_block4:
8422 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8423 break;
8424 case DW_FORM_addrx:
8425 case DW_FORM_strx:
8426 case DW_FORM_sdata:
8427 case DW_FORM_udata:
8428 case DW_FORM_ref_udata:
8429 case DW_FORM_GNU_addr_index:
8430 case DW_FORM_GNU_str_index:
8431 case DW_FORM_rnglistx:
8432 case DW_FORM_loclistx:
8433 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8434 break;
8435 case DW_FORM_indirect:
8436 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8437 info_ptr += bytes_read;
8438 /* We need to continue parsing from here, so just go back to
8439 the top. */
8440 goto skip_attribute;
8441
8442 default:
8443 error (_("Dwarf Error: Cannot handle %s "
8444 "in DWARF reader [in module %s]"),
8445 dwarf_form_name (form),
8446 bfd_get_filename (abfd));
8447 }
8448 }
8449
8450 if (abbrev->has_children)
8451 return skip_children (reader, info_ptr);
8452 else
8453 return info_ptr;
8454 }
8455
8456 /* Locate ORIG_PDI's sibling.
8457 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8458
8459 static const gdb_byte *
8460 locate_pdi_sibling (const struct die_reader_specs *reader,
8461 struct partial_die_info *orig_pdi,
8462 const gdb_byte *info_ptr)
8463 {
8464 /* Do we know the sibling already? */
8465
8466 if (orig_pdi->sibling)
8467 return orig_pdi->sibling;
8468
8469 /* Are there any children to deal with? */
8470
8471 if (!orig_pdi->has_children)
8472 return info_ptr;
8473
8474 /* Skip the children the long way. */
8475
8476 return skip_children (reader, info_ptr);
8477 }
8478
8479 /* Expand this partial symbol table into a full symbol table. SELF is
8480 not NULL. */
8481
8482 void
8483 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8484 {
8485 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8486
8487 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8488
8489 /* If this psymtab is constructed from a debug-only objfile, the
8490 has_section_at_zero flag will not necessarily be correct. We
8491 can get the correct value for this flag by looking at the data
8492 associated with the (presumably stripped) associated objfile. */
8493 if (objfile->separate_debug_objfile_backlink)
8494 {
8495 dwarf2_per_objfile *per_objfile_backlink
8496 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8497
8498 per_objfile->per_bfd->has_section_at_zero
8499 = per_objfile_backlink->per_bfd->has_section_at_zero;
8500 }
8501
8502 expand_psymtab (objfile);
8503
8504 process_cu_includes (per_objfile);
8505 }
8506 \f
8507 /* Reading in full CUs. */
8508
8509 /* Add PER_CU to the queue. */
8510
8511 static void
8512 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8513 dwarf2_per_objfile *per_objfile,
8514 enum language pretend_language)
8515 {
8516 per_cu->queued = 1;
8517
8518 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8519 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8520 }
8521
8522 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8523
8524 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8525 dependency.
8526
8527 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8528 DIEs, false otherwise.
8529
8530 Explanation: there is an invariant that if a CU is queued for expansion
8531 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8532 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8533 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8534 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8535 invariant is respected.
8536
8537 The caller is therefore not required to load the CU's DIEs (we return false)
8538 if:
8539
8540 - the CU is already expanded, and therefore does not get enqueued
8541 - the CU gets enqueued for expansion, but its DIEs are already loaded
8542
8543 Note that the caller should not use this function's return value as an
8544 indicator of whether the CU's DIEs are loaded right now, it should check
8545 that by calling `dwarf2_per_objfile::get_cu` instead. */
8546
8547 static int
8548 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8549 dwarf2_per_cu_data *per_cu,
8550 dwarf2_per_objfile *per_objfile,
8551 enum language pretend_language)
8552 {
8553 /* We may arrive here during partial symbol reading, if we need full
8554 DIEs to process an unusual case (e.g. template arguments). Do
8555 not queue PER_CU, just tell our caller to load its DIEs. */
8556 if (per_cu->per_bfd->reading_partial_symbols)
8557 {
8558 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8559
8560 if (cu == NULL || cu->dies == NULL)
8561 return 1;
8562 return 0;
8563 }
8564
8565 /* Mark the dependence relation so that we don't flush PER_CU
8566 too early. */
8567 if (dependent_cu != NULL)
8568 dependent_cu->add_dependence (per_cu);
8569
8570 /* If it's already on the queue, we have nothing to do. */
8571 if (per_cu->queued)
8572 {
8573 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8574 loaded. */
8575 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8576
8577 /* If the CU is queued for expansion, it should not already be
8578 expanded. */
8579 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8580
8581 /* The DIEs are already loaded, the caller doesn't need to do it. */
8582 return 0;
8583 }
8584
8585 bool queued = false;
8586 if (!per_objfile->symtab_set_p (per_cu))
8587 {
8588 /* Add it to the queue. */
8589 queue_comp_unit (per_cu, per_objfile, pretend_language);
8590 queued = true;
8591 }
8592
8593 /* If the compilation unit is already loaded, just mark it as
8594 used. */
8595 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8596 if (cu != nullptr)
8597 cu->last_used = 0;
8598
8599 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8600 and the DIEs are not already loaded. */
8601 return queued && cu == nullptr;
8602 }
8603
8604 /* Process the queue. */
8605
8606 static void
8607 process_queue (dwarf2_per_objfile *per_objfile)
8608 {
8609 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8610 objfile_name (per_objfile->objfile));
8611
8612 /* The queue starts out with one item, but following a DIE reference
8613 may load a new CU, adding it to the end of the queue. */
8614 while (!per_objfile->per_bfd->queue->empty ())
8615 {
8616 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8617 dwarf2_per_cu_data *per_cu = item.per_cu;
8618
8619 if (!per_objfile->symtab_set_p (per_cu))
8620 {
8621 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8622
8623 /* Skip dummy CUs. */
8624 if (cu != nullptr)
8625 {
8626 unsigned int debug_print_threshold;
8627 char buf[100];
8628
8629 if (per_cu->is_debug_types)
8630 {
8631 struct signatured_type *sig_type =
8632 (struct signatured_type *) per_cu;
8633
8634 sprintf (buf, "TU %s at offset %s",
8635 hex_string (sig_type->signature),
8636 sect_offset_str (per_cu->sect_off));
8637 /* There can be 100s of TUs.
8638 Only print them in verbose mode. */
8639 debug_print_threshold = 2;
8640 }
8641 else
8642 {
8643 sprintf (buf, "CU at offset %s",
8644 sect_offset_str (per_cu->sect_off));
8645 debug_print_threshold = 1;
8646 }
8647
8648 if (dwarf_read_debug >= debug_print_threshold)
8649 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8650
8651 if (per_cu->is_debug_types)
8652 process_full_type_unit (cu, item.pretend_language);
8653 else
8654 process_full_comp_unit (cu, item.pretend_language);
8655
8656 if (dwarf_read_debug >= debug_print_threshold)
8657 dwarf_read_debug_printf ("Done expanding %s", buf);
8658 }
8659 }
8660
8661 per_cu->queued = 0;
8662 per_objfile->per_bfd->queue->pop ();
8663 }
8664
8665 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8666 objfile_name (per_objfile->objfile));
8667 }
8668
8669 /* Read in full symbols for PST, and anything it depends on. */
8670
8671 void
8672 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8673 {
8674 gdb_assert (!readin_p (objfile));
8675
8676 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8677 free_cached_comp_units freer (per_objfile);
8678 expand_dependencies (objfile);
8679
8680 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8681 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8682 }
8683
8684 /* See psympriv.h. */
8685
8686 bool
8687 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8688 {
8689 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8690 return per_objfile->symtab_set_p (per_cu_data);
8691 }
8692
8693 /* See psympriv.h. */
8694
8695 compunit_symtab *
8696 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8697 {
8698 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8699 return per_objfile->get_symtab (per_cu_data);
8700 }
8701
8702 /* Trivial hash function for die_info: the hash value of a DIE
8703 is its offset in .debug_info for this objfile. */
8704
8705 static hashval_t
8706 die_hash (const void *item)
8707 {
8708 const struct die_info *die = (const struct die_info *) item;
8709
8710 return to_underlying (die->sect_off);
8711 }
8712
8713 /* Trivial comparison function for die_info structures: two DIEs
8714 are equal if they have the same offset. */
8715
8716 static int
8717 die_eq (const void *item_lhs, const void *item_rhs)
8718 {
8719 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8720 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8721
8722 return die_lhs->sect_off == die_rhs->sect_off;
8723 }
8724
8725 /* Load the DIEs associated with PER_CU into memory.
8726
8727 In some cases, the caller, while reading partial symbols, will need to load
8728 the full symbols for the CU for some reason. It will already have a
8729 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8730 rather than creating a new one. */
8731
8732 static void
8733 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8734 dwarf2_per_objfile *per_objfile,
8735 dwarf2_cu *existing_cu,
8736 bool skip_partial,
8737 enum language pretend_language)
8738 {
8739 gdb_assert (! this_cu->is_debug_types);
8740
8741 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8742 if (reader.dummy_p)
8743 return;
8744
8745 struct dwarf2_cu *cu = reader.cu;
8746 const gdb_byte *info_ptr = reader.info_ptr;
8747
8748 gdb_assert (cu->die_hash == NULL);
8749 cu->die_hash =
8750 htab_create_alloc_ex (cu->header.length / 12,
8751 die_hash,
8752 die_eq,
8753 NULL,
8754 &cu->comp_unit_obstack,
8755 hashtab_obstack_allocate,
8756 dummy_obstack_deallocate);
8757
8758 if (reader.comp_unit_die->has_children)
8759 reader.comp_unit_die->child
8760 = read_die_and_siblings (&reader, reader.info_ptr,
8761 &info_ptr, reader.comp_unit_die);
8762 cu->dies = reader.comp_unit_die;
8763 /* comp_unit_die is not stored in die_hash, no need. */
8764
8765 /* We try not to read any attributes in this function, because not
8766 all CUs needed for references have been loaded yet, and symbol
8767 table processing isn't initialized. But we have to set the CU language,
8768 or we won't be able to build types correctly.
8769 Similarly, if we do not read the producer, we can not apply
8770 producer-specific interpretation. */
8771 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8772
8773 reader.keep ();
8774 }
8775
8776 /* Add a DIE to the delayed physname list. */
8777
8778 static void
8779 add_to_method_list (struct type *type, int fnfield_index, int index,
8780 const char *name, struct die_info *die,
8781 struct dwarf2_cu *cu)
8782 {
8783 struct delayed_method_info mi;
8784 mi.type = type;
8785 mi.fnfield_index = fnfield_index;
8786 mi.index = index;
8787 mi.name = name;
8788 mi.die = die;
8789 cu->method_list.push_back (mi);
8790 }
8791
8792 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8793 "const" / "volatile". If so, decrements LEN by the length of the
8794 modifier and return true. Otherwise return false. */
8795
8796 template<size_t N>
8797 static bool
8798 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8799 {
8800 size_t mod_len = sizeof (mod) - 1;
8801 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8802 {
8803 len -= mod_len;
8804 return true;
8805 }
8806 return false;
8807 }
8808
8809 /* Compute the physnames of any methods on the CU's method list.
8810
8811 The computation of method physnames is delayed in order to avoid the
8812 (bad) condition that one of the method's formal parameters is of an as yet
8813 incomplete type. */
8814
8815 static void
8816 compute_delayed_physnames (struct dwarf2_cu *cu)
8817 {
8818 /* Only C++ delays computing physnames. */
8819 if (cu->method_list.empty ())
8820 return;
8821 gdb_assert (cu->per_cu->lang == language_cplus);
8822
8823 for (const delayed_method_info &mi : cu->method_list)
8824 {
8825 const char *physname;
8826 struct fn_fieldlist *fn_flp
8827 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8828 physname = dwarf2_physname (mi.name, mi.die, cu);
8829 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8830 = physname ? physname : "";
8831
8832 /* Since there's no tag to indicate whether a method is a
8833 const/volatile overload, extract that information out of the
8834 demangled name. */
8835 if (physname != NULL)
8836 {
8837 size_t len = strlen (physname);
8838
8839 while (1)
8840 {
8841 if (physname[len] == ')') /* shortcut */
8842 break;
8843 else if (check_modifier (physname, len, " const"))
8844 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8845 else if (check_modifier (physname, len, " volatile"))
8846 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8847 else
8848 break;
8849 }
8850 }
8851 }
8852
8853 /* The list is no longer needed. */
8854 cu->method_list.clear ();
8855 }
8856
8857 /* Go objects should be embedded in a DW_TAG_module DIE,
8858 and it's not clear if/how imported objects will appear.
8859 To keep Go support simple until that's worked out,
8860 go back through what we've read and create something usable.
8861 We could do this while processing each DIE, and feels kinda cleaner,
8862 but that way is more invasive.
8863 This is to, for example, allow the user to type "p var" or "b main"
8864 without having to specify the package name, and allow lookups
8865 of module.object to work in contexts that use the expression
8866 parser. */
8867
8868 static void
8869 fixup_go_packaging (struct dwarf2_cu *cu)
8870 {
8871 gdb::unique_xmalloc_ptr<char> package_name;
8872 struct pending *list;
8873 int i;
8874
8875 for (list = *cu->get_builder ()->get_global_symbols ();
8876 list != NULL;
8877 list = list->next)
8878 {
8879 for (i = 0; i < list->nsyms; ++i)
8880 {
8881 struct symbol *sym = list->symbol[i];
8882
8883 if (sym->language () == language_go
8884 && sym->aclass () == LOC_BLOCK)
8885 {
8886 gdb::unique_xmalloc_ptr<char> this_package_name
8887 (go_symbol_package_name (sym));
8888
8889 if (this_package_name == NULL)
8890 continue;
8891 if (package_name == NULL)
8892 package_name = std::move (this_package_name);
8893 else
8894 {
8895 struct objfile *objfile = cu->per_objfile->objfile;
8896 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8897 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8898 (symbol_symtab (sym) != NULL
8899 ? symtab_to_filename_for_display
8900 (symbol_symtab (sym))
8901 : objfile_name (objfile)),
8902 this_package_name.get (), package_name.get ());
8903 }
8904 }
8905 }
8906 }
8907
8908 if (package_name != NULL)
8909 {
8910 struct objfile *objfile = cu->per_objfile->objfile;
8911 const char *saved_package_name = objfile->intern (package_name.get ());
8912 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8913 saved_package_name);
8914 struct symbol *sym;
8915
8916 sym = new (&objfile->objfile_obstack) symbol;
8917 sym->set_language (language_go, &objfile->objfile_obstack);
8918 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8919 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8920 e.g., "main" finds the "main" module and not C's main(). */
8921 sym->set_domain (STRUCT_DOMAIN);
8922 sym->set_aclass_index (LOC_TYPEDEF);
8923 sym->set_type (type);
8924
8925 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8926 }
8927 }
8928
8929 /* Allocate a fully-qualified name consisting of the two parts on the
8930 obstack. */
8931
8932 static const char *
8933 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8934 {
8935 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8936 }
8937
8938 /* A helper that allocates a variant part to attach to a Rust enum
8939 type. OBSTACK is where the results should be allocated. TYPE is
8940 the type we're processing. DISCRIMINANT_INDEX is the index of the
8941 discriminant. It must be the index of one of the fields of TYPE,
8942 or -1 to mean there is no discriminant (univariant enum).
8943 DEFAULT_INDEX is the index of the default field; or -1 if there is
8944 no default. RANGES is indexed by "effective" field number (the
8945 field index, but omitting the discriminant and default fields) and
8946 must hold the discriminant values used by the variants. Note that
8947 RANGES must have a lifetime at least as long as OBSTACK -- either
8948 already allocated on it, or static. */
8949
8950 static void
8951 alloc_rust_variant (struct obstack *obstack, struct type *type,
8952 int discriminant_index, int default_index,
8953 gdb::array_view<discriminant_range> ranges)
8954 {
8955 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8956 gdb_assert (discriminant_index == -1
8957 || (discriminant_index >= 0
8958 && discriminant_index < type->num_fields ()));
8959 gdb_assert (default_index == -1
8960 || (default_index >= 0 && default_index < type->num_fields ()));
8961
8962 /* We have one variant for each non-discriminant field. */
8963 int n_variants = type->num_fields ();
8964 if (discriminant_index != -1)
8965 --n_variants;
8966
8967 variant *variants = new (obstack) variant[n_variants];
8968 int var_idx = 0;
8969 int range_idx = 0;
8970 for (int i = 0; i < type->num_fields (); ++i)
8971 {
8972 if (i == discriminant_index)
8973 continue;
8974
8975 variants[var_idx].first_field = i;
8976 variants[var_idx].last_field = i + 1;
8977
8978 /* The default field does not need a range, but other fields do.
8979 We skipped the discriminant above. */
8980 if (i != default_index)
8981 {
8982 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8983 ++range_idx;
8984 }
8985
8986 ++var_idx;
8987 }
8988
8989 gdb_assert (range_idx == ranges.size ());
8990 gdb_assert (var_idx == n_variants);
8991
8992 variant_part *part = new (obstack) variant_part;
8993 part->discriminant_index = discriminant_index;
8994 /* If there is no discriminant, then whether it is signed is of no
8995 consequence. */
8996 part->is_unsigned
8997 = (discriminant_index == -1
8998 ? false
8999 : type->field (discriminant_index).type ()->is_unsigned ());
9000 part->variants = gdb::array_view<variant> (variants, n_variants);
9001
9002 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9003 gdb::array_view<variant_part> *prop_value
9004 = new (storage) gdb::array_view<variant_part> (part, 1);
9005
9006 struct dynamic_prop prop;
9007 prop.set_variant_parts (prop_value);
9008
9009 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9010 }
9011
9012 /* Some versions of rustc emitted enums in an unusual way.
9013
9014 Ordinary enums were emitted as unions. The first element of each
9015 structure in the union was named "RUST$ENUM$DISR". This element
9016 held the discriminant.
9017
9018 These versions of Rust also implemented the "non-zero"
9019 optimization. When the enum had two values, and one is empty and
9020 the other holds a pointer that cannot be zero, the pointer is used
9021 as the discriminant, with a zero value meaning the empty variant.
9022 Here, the union's first member is of the form
9023 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9024 where the fieldnos are the indices of the fields that should be
9025 traversed in order to find the field (which may be several fields deep)
9026 and the variantname is the name of the variant of the case when the
9027 field is zero.
9028
9029 This function recognizes whether TYPE is of one of these forms,
9030 and, if so, smashes it to be a variant type. */
9031
9032 static void
9033 quirk_rust_enum (struct type *type, struct objfile *objfile)
9034 {
9035 gdb_assert (type->code () == TYPE_CODE_UNION);
9036
9037 /* We don't need to deal with empty enums. */
9038 if (type->num_fields () == 0)
9039 return;
9040
9041 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9042 if (type->num_fields () == 1
9043 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
9044 {
9045 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
9046
9047 /* Decode the field name to find the offset of the
9048 discriminant. */
9049 ULONGEST bit_offset = 0;
9050 struct type *field_type = type->field (0).type ();
9051 while (name[0] >= '0' && name[0] <= '9')
9052 {
9053 char *tail;
9054 unsigned long index = strtoul (name, &tail, 10);
9055 name = tail;
9056 if (*name != '$'
9057 || index >= field_type->num_fields ()
9058 || (field_type->field (index).loc_kind ()
9059 != FIELD_LOC_KIND_BITPOS))
9060 {
9061 complaint (_("Could not parse Rust enum encoding string \"%s\""
9062 "[in module %s]"),
9063 type->field (0).name (),
9064 objfile_name (objfile));
9065 return;
9066 }
9067 ++name;
9068
9069 bit_offset += field_type->field (index).loc_bitpos ();
9070 field_type = field_type->field (index).type ();
9071 }
9072
9073 /* Smash this type to be a structure type. We have to do this
9074 because the type has already been recorded. */
9075 type->set_code (TYPE_CODE_STRUCT);
9076 type->set_num_fields (3);
9077 /* Save the field we care about. */
9078 struct field saved_field = type->field (0);
9079 type->set_fields
9080 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9081
9082 /* Put the discriminant at index 0. */
9083 type->field (0).set_type (field_type);
9084 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9085 type->field (0).set_name ("<<discriminant>>");
9086 type->field (0).set_loc_bitpos (bit_offset);
9087
9088 /* The order of fields doesn't really matter, so put the real
9089 field at index 1 and the data-less field at index 2. */
9090 type->field (1) = saved_field;
9091 type->field (1).set_name
9092 (rust_last_path_segment (type->field (1).type ()->name ()));
9093 type->field (1).type ()->set_name
9094 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9095 type->field (1).name ()));
9096
9097 const char *dataless_name
9098 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9099 name);
9100 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9101 dataless_name);
9102 type->field (2).set_type (dataless_type);
9103 /* NAME points into the original discriminant name, which
9104 already has the correct lifetime. */
9105 type->field (2).set_name (name);
9106 type->field (2).set_loc_bitpos (0);
9107
9108 /* Indicate that this is a variant type. */
9109 static discriminant_range ranges[1] = { { 0, 0 } };
9110 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9111 }
9112 /* A union with a single anonymous field is probably an old-style
9113 univariant enum. */
9114 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
9115 {
9116 /* Smash this type to be a structure type. We have to do this
9117 because the type has already been recorded. */
9118 type->set_code (TYPE_CODE_STRUCT);
9119
9120 struct type *field_type = type->field (0).type ();
9121 const char *variant_name
9122 = rust_last_path_segment (field_type->name ());
9123 type->field (0).set_name (variant_name);
9124 field_type->set_name
9125 (rust_fully_qualify (&objfile->objfile_obstack,
9126 type->name (), variant_name));
9127
9128 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9129 }
9130 else
9131 {
9132 struct type *disr_type = nullptr;
9133 for (int i = 0; i < type->num_fields (); ++i)
9134 {
9135 disr_type = type->field (i).type ();
9136
9137 if (disr_type->code () != TYPE_CODE_STRUCT)
9138 {
9139 /* All fields of a true enum will be structs. */
9140 return;
9141 }
9142 else if (disr_type->num_fields () == 0)
9143 {
9144 /* Could be data-less variant, so keep going. */
9145 disr_type = nullptr;
9146 }
9147 else if (strcmp (disr_type->field (0).name (),
9148 "RUST$ENUM$DISR") != 0)
9149 {
9150 /* Not a Rust enum. */
9151 return;
9152 }
9153 else
9154 {
9155 /* Found one. */
9156 break;
9157 }
9158 }
9159
9160 /* If we got here without a discriminant, then it's probably
9161 just a union. */
9162 if (disr_type == nullptr)
9163 return;
9164
9165 /* Smash this type to be a structure type. We have to do this
9166 because the type has already been recorded. */
9167 type->set_code (TYPE_CODE_STRUCT);
9168
9169 /* Make space for the discriminant field. */
9170 struct field *disr_field = &disr_type->field (0);
9171 field *new_fields
9172 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9173 * sizeof (struct field)));
9174 memcpy (new_fields + 1, type->fields (),
9175 type->num_fields () * sizeof (struct field));
9176 type->set_fields (new_fields);
9177 type->set_num_fields (type->num_fields () + 1);
9178
9179 /* Install the discriminant at index 0 in the union. */
9180 type->field (0) = *disr_field;
9181 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9182 type->field (0).set_name ("<<discriminant>>");
9183
9184 /* We need a way to find the correct discriminant given a
9185 variant name. For convenience we build a map here. */
9186 struct type *enum_type = disr_field->type ();
9187 std::unordered_map<std::string, ULONGEST> discriminant_map;
9188 for (int i = 0; i < enum_type->num_fields (); ++i)
9189 {
9190 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
9191 {
9192 const char *name
9193 = rust_last_path_segment (enum_type->field (i).name ());
9194 discriminant_map[name] = enum_type->field (i).loc_enumval ();
9195 }
9196 }
9197
9198 int n_fields = type->num_fields ();
9199 /* We don't need a range entry for the discriminant, but we do
9200 need one for every other field, as there is no default
9201 variant. */
9202 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9203 discriminant_range,
9204 n_fields - 1);
9205 /* Skip the discriminant here. */
9206 for (int i = 1; i < n_fields; ++i)
9207 {
9208 /* Find the final word in the name of this variant's type.
9209 That name can be used to look up the correct
9210 discriminant. */
9211 const char *variant_name
9212 = rust_last_path_segment (type->field (i).type ()->name ());
9213
9214 auto iter = discriminant_map.find (variant_name);
9215 if (iter != discriminant_map.end ())
9216 {
9217 ranges[i - 1].low = iter->second;
9218 ranges[i - 1].high = iter->second;
9219 }
9220
9221 /* In Rust, each element should have the size of the
9222 enclosing enum. */
9223 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9224
9225 /* Remove the discriminant field, if it exists. */
9226 struct type *sub_type = type->field (i).type ();
9227 if (sub_type->num_fields () > 0)
9228 {
9229 sub_type->set_num_fields (sub_type->num_fields () - 1);
9230 sub_type->set_fields (sub_type->fields () + 1);
9231 }
9232 type->field (i).set_name (variant_name);
9233 sub_type->set_name
9234 (rust_fully_qualify (&objfile->objfile_obstack,
9235 type->name (), variant_name));
9236 }
9237
9238 /* Indicate that this is a variant type. */
9239 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9240 gdb::array_view<discriminant_range> (ranges,
9241 n_fields - 1));
9242 }
9243 }
9244
9245 /* Rewrite some Rust unions to be structures with variants parts. */
9246
9247 static void
9248 rust_union_quirks (struct dwarf2_cu *cu)
9249 {
9250 gdb_assert (cu->per_cu->lang == language_rust);
9251 for (type *type_ : cu->rust_unions)
9252 quirk_rust_enum (type_, cu->per_objfile->objfile);
9253 /* We don't need this any more. */
9254 cu->rust_unions.clear ();
9255 }
9256
9257 /* See read.h. */
9258
9259 type_unit_group_unshareable *
9260 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9261 {
9262 auto iter = this->m_type_units.find (tu_group);
9263 if (iter != this->m_type_units.end ())
9264 return iter->second.get ();
9265
9266 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9267 type_unit_group_unshareable *result = uniq.get ();
9268 this->m_type_units[tu_group] = std::move (uniq);
9269 return result;
9270 }
9271
9272 struct type *
9273 dwarf2_per_objfile::get_type_for_signatured_type
9274 (signatured_type *sig_type) const
9275 {
9276 auto iter = this->m_type_map.find (sig_type);
9277 if (iter == this->m_type_map.end ())
9278 return nullptr;
9279
9280 return iter->second;
9281 }
9282
9283 void dwarf2_per_objfile::set_type_for_signatured_type
9284 (signatured_type *sig_type, struct type *type)
9285 {
9286 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9287
9288 this->m_type_map[sig_type] = type;
9289 }
9290
9291 /* A helper function for computing the list of all symbol tables
9292 included by PER_CU. */
9293
9294 static void
9295 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9296 htab_t all_children, htab_t all_type_symtabs,
9297 dwarf2_per_cu_data *per_cu,
9298 dwarf2_per_objfile *per_objfile,
9299 struct compunit_symtab *immediate_parent)
9300 {
9301 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9302 if (*slot != NULL)
9303 {
9304 /* This inclusion and its children have been processed. */
9305 return;
9306 }
9307
9308 *slot = per_cu;
9309
9310 /* Only add a CU if it has a symbol table. */
9311 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9312 if (cust != NULL)
9313 {
9314 /* If this is a type unit only add its symbol table if we haven't
9315 seen it yet (type unit per_cu's can share symtabs). */
9316 if (per_cu->is_debug_types)
9317 {
9318 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9319 if (*slot == NULL)
9320 {
9321 *slot = cust;
9322 result->push_back (cust);
9323 if (cust->user == NULL)
9324 cust->user = immediate_parent;
9325 }
9326 }
9327 else
9328 {
9329 result->push_back (cust);
9330 if (cust->user == NULL)
9331 cust->user = immediate_parent;
9332 }
9333 }
9334
9335 if (!per_cu->imported_symtabs_empty ())
9336 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9337 {
9338 recursively_compute_inclusions (result, all_children,
9339 all_type_symtabs, ptr, per_objfile,
9340 cust);
9341 }
9342 }
9343
9344 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9345 PER_CU. */
9346
9347 static void
9348 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9349 dwarf2_per_objfile *per_objfile)
9350 {
9351 gdb_assert (! per_cu->is_debug_types);
9352
9353 if (!per_cu->imported_symtabs_empty ())
9354 {
9355 int len;
9356 std::vector<compunit_symtab *> result_symtabs;
9357 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9358
9359 /* If we don't have a symtab, we can just skip this case. */
9360 if (cust == NULL)
9361 return;
9362
9363 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9364 htab_eq_pointer,
9365 NULL, xcalloc, xfree));
9366 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9367 htab_eq_pointer,
9368 NULL, xcalloc, xfree));
9369
9370 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9371 {
9372 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9373 all_type_symtabs.get (), ptr,
9374 per_objfile, cust);
9375 }
9376
9377 /* Now we have a transitive closure of all the included symtabs. */
9378 len = result_symtabs.size ();
9379 cust->includes
9380 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9381 struct compunit_symtab *, len + 1);
9382 memcpy (cust->includes, result_symtabs.data (),
9383 len * sizeof (compunit_symtab *));
9384 cust->includes[len] = NULL;
9385 }
9386 }
9387
9388 /* Compute the 'includes' field for the symtabs of all the CUs we just
9389 read. */
9390
9391 static void
9392 process_cu_includes (dwarf2_per_objfile *per_objfile)
9393 {
9394 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9395 {
9396 if (! iter->is_debug_types)
9397 compute_compunit_symtab_includes (iter, per_objfile);
9398 }
9399
9400 per_objfile->per_bfd->just_read_cus.clear ();
9401 }
9402
9403 /* Generate full symbol information for CU, whose DIEs have
9404 already been loaded into memory. */
9405
9406 static void
9407 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9408 {
9409 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9410 struct objfile *objfile = per_objfile->objfile;
9411 struct gdbarch *gdbarch = objfile->arch ();
9412 CORE_ADDR lowpc, highpc;
9413 struct compunit_symtab *cust;
9414 CORE_ADDR baseaddr;
9415 struct block *static_block;
9416 CORE_ADDR addr;
9417
9418 baseaddr = objfile->text_section_offset ();
9419
9420 /* Clear the list here in case something was left over. */
9421 cu->method_list.clear ();
9422
9423 dwarf2_find_base_address (cu->dies, cu);
9424
9425 /* Before we start reading the top-level DIE, ensure it has a valid tag
9426 type. */
9427 switch (cu->dies->tag)
9428 {
9429 case DW_TAG_compile_unit:
9430 case DW_TAG_partial_unit:
9431 case DW_TAG_type_unit:
9432 break;
9433 default:
9434 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9435 dwarf_tag_name (cu->dies->tag),
9436 sect_offset_str (cu->per_cu->sect_off),
9437 objfile_name (per_objfile->objfile));
9438 }
9439
9440 /* Do line number decoding in read_file_scope () */
9441 process_die (cu->dies, cu);
9442
9443 /* For now fudge the Go package. */
9444 if (cu->per_cu->lang == language_go)
9445 fixup_go_packaging (cu);
9446
9447 /* Now that we have processed all the DIEs in the CU, all the types
9448 should be complete, and it should now be safe to compute all of the
9449 physnames. */
9450 compute_delayed_physnames (cu);
9451
9452 if (cu->per_cu->lang == language_rust)
9453 rust_union_quirks (cu);
9454
9455 /* Some compilers don't define a DW_AT_high_pc attribute for the
9456 compilation unit. If the DW_AT_high_pc is missing, synthesize
9457 it, by scanning the DIE's below the compilation unit. */
9458 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9459
9460 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9461 static_block
9462 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
9463
9464 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9465 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9466 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9467 addrmap to help ensure it has an accurate map of pc values belonging to
9468 this comp unit. */
9469 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9470
9471 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
9472 (static_block, SECT_OFF_TEXT (objfile), 0);
9473
9474 if (cust != NULL)
9475 {
9476 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9477
9478 /* Set symtab language to language from DW_AT_language. If the
9479 compilation is from a C file generated by language preprocessors, do
9480 not set the language if it was already deduced by start_subfile. */
9481 if (!(cu->per_cu->lang == language_c
9482 && cust->primary_filetab ()->language () != language_unknown))
9483 cust->primary_filetab ()->set_language (cu->per_cu->lang);
9484
9485 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9486 produce DW_AT_location with location lists but it can be possibly
9487 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9488 there were bugs in prologue debug info, fixed later in GCC-4.5
9489 by "unwind info for epilogues" patch (which is not directly related).
9490
9491 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9492 needed, it would be wrong due to missing DW_AT_producer there.
9493
9494 Still one can confuse GDB by using non-standard GCC compilation
9495 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9496 */
9497 if (cu->has_loclist && gcc_4_minor >= 5)
9498 cust->set_locations_valid (true);
9499
9500 if (gcc_4_minor >= 5)
9501 cust->set_epilogue_unwind_valid (true);
9502
9503 cust->set_call_site_htab (cu->call_site_htab);
9504 }
9505
9506 per_objfile->set_symtab (cu->per_cu, cust);
9507
9508 /* Push it for inclusion processing later. */
9509 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9510
9511 /* Not needed any more. */
9512 cu->reset_builder ();
9513 }
9514
9515 /* Generate full symbol information for type unit CU, whose DIEs have
9516 already been loaded into memory. */
9517
9518 static void
9519 process_full_type_unit (dwarf2_cu *cu,
9520 enum language pretend_language)
9521 {
9522 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9523 struct objfile *objfile = per_objfile->objfile;
9524 struct compunit_symtab *cust;
9525 struct signatured_type *sig_type;
9526
9527 gdb_assert (cu->per_cu->is_debug_types);
9528 sig_type = (struct signatured_type *) cu->per_cu;
9529
9530 /* Clear the list here in case something was left over. */
9531 cu->method_list.clear ();
9532
9533 /* The symbol tables are set up in read_type_unit_scope. */
9534 process_die (cu->dies, cu);
9535
9536 /* For now fudge the Go package. */
9537 if (cu->per_cu->lang == language_go)
9538 fixup_go_packaging (cu);
9539
9540 /* Now that we have processed all the DIEs in the CU, all the types
9541 should be complete, and it should now be safe to compute all of the
9542 physnames. */
9543 compute_delayed_physnames (cu);
9544
9545 if (cu->per_cu->lang == language_rust)
9546 rust_union_quirks (cu);
9547
9548 /* TUs share symbol tables.
9549 If this is the first TU to use this symtab, complete the construction
9550 of it with end_expandable_symtab. Otherwise, complete the addition of
9551 this TU's symbols to the existing symtab. */
9552 type_unit_group_unshareable *tug_unshare =
9553 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9554 if (tug_unshare->compunit_symtab == NULL)
9555 {
9556 buildsym_compunit *builder = cu->get_builder ();
9557 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9558 tug_unshare->compunit_symtab = cust;
9559
9560 if (cust != NULL)
9561 {
9562 /* Set symtab language to language from DW_AT_language. If the
9563 compilation is from a C file generated by language preprocessors,
9564 do not set the language if it was already deduced by
9565 start_subfile. */
9566 if (!(cu->per_cu->lang == language_c
9567 && cust->primary_filetab ()->language () != language_c))
9568 cust->primary_filetab ()->set_language (cu->per_cu->lang);
9569 }
9570 }
9571 else
9572 {
9573 cu->get_builder ()->augment_type_symtab ();
9574 cust = tug_unshare->compunit_symtab;
9575 }
9576
9577 per_objfile->set_symtab (cu->per_cu, cust);
9578
9579 /* Not needed any more. */
9580 cu->reset_builder ();
9581 }
9582
9583 /* Process an imported unit DIE. */
9584
9585 static void
9586 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9587 {
9588 struct attribute *attr;
9589
9590 /* For now we don't handle imported units in type units. */
9591 if (cu->per_cu->is_debug_types)
9592 {
9593 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9594 " supported in type units [in module %s]"),
9595 objfile_name (cu->per_objfile->objfile));
9596 }
9597
9598 attr = dwarf2_attr (die, DW_AT_import, cu);
9599 if (attr != NULL)
9600 {
9601 sect_offset sect_off = attr->get_ref_die_offset ();
9602 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9603 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9604 dwarf2_per_cu_data *per_cu
9605 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9606 per_objfile->per_bfd);
9607
9608 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9609 into another compilation unit, at root level. Regard this as a hint,
9610 and ignore it. */
9611 if (die->parent && die->parent->parent == NULL
9612 && per_cu->unit_type == DW_UT_compile
9613 && per_cu->lang == language_cplus)
9614 return;
9615
9616 /* If necessary, add it to the queue and load its DIEs. */
9617 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9618 cu->per_cu->lang))
9619 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9620 false, cu->per_cu->lang);
9621
9622 cu->per_cu->imported_symtabs_push (per_cu);
9623 }
9624 }
9625
9626 /* RAII object that represents a process_die scope: i.e.,
9627 starts/finishes processing a DIE. */
9628 class process_die_scope
9629 {
9630 public:
9631 process_die_scope (die_info *die, dwarf2_cu *cu)
9632 : m_die (die), m_cu (cu)
9633 {
9634 /* We should only be processing DIEs not already in process. */
9635 gdb_assert (!m_die->in_process);
9636 m_die->in_process = true;
9637 }
9638
9639 ~process_die_scope ()
9640 {
9641 m_die->in_process = false;
9642
9643 /* If we're done processing the DIE for the CU that owns the line
9644 header, we don't need the line header anymore. */
9645 if (m_cu->line_header_die_owner == m_die)
9646 {
9647 delete m_cu->line_header;
9648 m_cu->line_header = NULL;
9649 m_cu->line_header_die_owner = NULL;
9650 }
9651 }
9652
9653 private:
9654 die_info *m_die;
9655 dwarf2_cu *m_cu;
9656 };
9657
9658 /* Process a die and its children. */
9659
9660 static void
9661 process_die (struct die_info *die, struct dwarf2_cu *cu)
9662 {
9663 process_die_scope scope (die, cu);
9664
9665 switch (die->tag)
9666 {
9667 case DW_TAG_padding:
9668 break;
9669 case DW_TAG_compile_unit:
9670 case DW_TAG_partial_unit:
9671 read_file_scope (die, cu);
9672 break;
9673 case DW_TAG_type_unit:
9674 read_type_unit_scope (die, cu);
9675 break;
9676 case DW_TAG_subprogram:
9677 /* Nested subprograms in Fortran get a prefix. */
9678 if (cu->per_cu->lang == language_fortran
9679 && die->parent != NULL
9680 && die->parent->tag == DW_TAG_subprogram)
9681 cu->processing_has_namespace_info = true;
9682 /* Fall through. */
9683 case DW_TAG_inlined_subroutine:
9684 read_func_scope (die, cu);
9685 break;
9686 case DW_TAG_lexical_block:
9687 case DW_TAG_try_block:
9688 case DW_TAG_catch_block:
9689 read_lexical_block_scope (die, cu);
9690 break;
9691 case DW_TAG_call_site:
9692 case DW_TAG_GNU_call_site:
9693 read_call_site_scope (die, cu);
9694 break;
9695 case DW_TAG_class_type:
9696 case DW_TAG_interface_type:
9697 case DW_TAG_structure_type:
9698 case DW_TAG_union_type:
9699 case DW_TAG_namelist:
9700 process_structure_scope (die, cu);
9701 break;
9702 case DW_TAG_enumeration_type:
9703 process_enumeration_scope (die, cu);
9704 break;
9705
9706 /* These dies have a type, but processing them does not create
9707 a symbol or recurse to process the children. Therefore we can
9708 read them on-demand through read_type_die. */
9709 case DW_TAG_subroutine_type:
9710 case DW_TAG_set_type:
9711 case DW_TAG_pointer_type:
9712 case DW_TAG_ptr_to_member_type:
9713 case DW_TAG_reference_type:
9714 case DW_TAG_rvalue_reference_type:
9715 case DW_TAG_string_type:
9716 break;
9717
9718 case DW_TAG_array_type:
9719 /* We only need to handle this case for Ada -- in other
9720 languages, it's normal for the compiler to emit a typedef
9721 instead. */
9722 if (cu->per_cu->lang != language_ada)
9723 break;
9724 /* FALLTHROUGH */
9725 case DW_TAG_base_type:
9726 case DW_TAG_subrange_type:
9727 case DW_TAG_generic_subrange:
9728 case DW_TAG_typedef:
9729 /* Add a typedef symbol for the type definition, if it has a
9730 DW_AT_name. */
9731 new_symbol (die, read_type_die (die, cu), cu);
9732 break;
9733 case DW_TAG_common_block:
9734 read_common_block (die, cu);
9735 break;
9736 case DW_TAG_common_inclusion:
9737 break;
9738 case DW_TAG_namespace:
9739 cu->processing_has_namespace_info = true;
9740 read_namespace (die, cu);
9741 break;
9742 case DW_TAG_module:
9743 cu->processing_has_namespace_info = true;
9744 read_module (die, cu);
9745 break;
9746 case DW_TAG_imported_declaration:
9747 cu->processing_has_namespace_info = true;
9748 if (read_namespace_alias (die, cu))
9749 break;
9750 /* The declaration is not a global namespace alias. */
9751 /* Fall through. */
9752 case DW_TAG_imported_module:
9753 cu->processing_has_namespace_info = true;
9754 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9755 || cu->per_cu->lang != language_fortran))
9756 complaint (_("Tag '%s' has unexpected children"),
9757 dwarf_tag_name (die->tag));
9758 read_import_statement (die, cu);
9759 break;
9760
9761 case DW_TAG_imported_unit:
9762 process_imported_unit_die (die, cu);
9763 break;
9764
9765 case DW_TAG_variable:
9766 read_variable (die, cu);
9767 break;
9768
9769 default:
9770 new_symbol (die, NULL, cu);
9771 break;
9772 }
9773 }
9774 \f
9775 /* DWARF name computation. */
9776
9777 /* A helper function for dwarf2_compute_name which determines whether DIE
9778 needs to have the name of the scope prepended to the name listed in the
9779 die. */
9780
9781 static int
9782 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9783 {
9784 struct attribute *attr;
9785
9786 switch (die->tag)
9787 {
9788 case DW_TAG_namespace:
9789 case DW_TAG_typedef:
9790 case DW_TAG_class_type:
9791 case DW_TAG_interface_type:
9792 case DW_TAG_structure_type:
9793 case DW_TAG_union_type:
9794 case DW_TAG_enumeration_type:
9795 case DW_TAG_enumerator:
9796 case DW_TAG_subprogram:
9797 case DW_TAG_inlined_subroutine:
9798 case DW_TAG_member:
9799 case DW_TAG_imported_declaration:
9800 return 1;
9801
9802 case DW_TAG_variable:
9803 case DW_TAG_constant:
9804 /* We only need to prefix "globally" visible variables. These include
9805 any variable marked with DW_AT_external or any variable that
9806 lives in a namespace. [Variables in anonymous namespaces
9807 require prefixing, but they are not DW_AT_external.] */
9808
9809 if (dwarf2_attr (die, DW_AT_specification, cu))
9810 {
9811 struct dwarf2_cu *spec_cu = cu;
9812
9813 return die_needs_namespace (die_specification (die, &spec_cu),
9814 spec_cu);
9815 }
9816
9817 attr = dwarf2_attr (die, DW_AT_external, cu);
9818 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9819 && die->parent->tag != DW_TAG_module)
9820 return 0;
9821 /* A variable in a lexical block of some kind does not need a
9822 namespace, even though in C++ such variables may be external
9823 and have a mangled name. */
9824 if (die->parent->tag == DW_TAG_lexical_block
9825 || die->parent->tag == DW_TAG_try_block
9826 || die->parent->tag == DW_TAG_catch_block
9827 || die->parent->tag == DW_TAG_subprogram)
9828 return 0;
9829 return 1;
9830
9831 default:
9832 return 0;
9833 }
9834 }
9835
9836 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9837 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9838 defined for the given DIE. */
9839
9840 static struct attribute *
9841 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9842 {
9843 struct attribute *attr;
9844
9845 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9846 if (attr == NULL)
9847 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9848
9849 return attr;
9850 }
9851
9852 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9853 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9854 defined for the given DIE. */
9855
9856 static const char *
9857 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9858 {
9859 const char *linkage_name;
9860
9861 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9862 if (linkage_name == NULL)
9863 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9864
9865 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9866 See https://github.com/rust-lang/rust/issues/32925. */
9867 if (cu->per_cu->lang == language_rust && linkage_name != NULL
9868 && strchr (linkage_name, '{') != NULL)
9869 linkage_name = NULL;
9870
9871 return linkage_name;
9872 }
9873
9874 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9875 compute the physname for the object, which include a method's:
9876 - formal parameters (C++),
9877 - receiver type (Go),
9878
9879 The term "physname" is a bit confusing.
9880 For C++, for example, it is the demangled name.
9881 For Go, for example, it's the mangled name.
9882
9883 For Ada, return the DIE's linkage name rather than the fully qualified
9884 name. PHYSNAME is ignored..
9885
9886 The result is allocated on the objfile->per_bfd's obstack and
9887 canonicalized. */
9888
9889 static const char *
9890 dwarf2_compute_name (const char *name,
9891 struct die_info *die, struct dwarf2_cu *cu,
9892 int physname)
9893 {
9894 struct objfile *objfile = cu->per_objfile->objfile;
9895
9896 if (name == NULL)
9897 name = dwarf2_name (die, cu);
9898
9899 enum language lang = cu->per_cu->lang;
9900
9901 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9902 but otherwise compute it by typename_concat inside GDB.
9903 FIXME: Actually this is not really true, or at least not always true.
9904 It's all very confusing. compute_and_set_names doesn't try to demangle
9905 Fortran names because there is no mangling standard. So new_symbol
9906 will set the demangled name to the result of dwarf2_full_name, and it is
9907 the demangled name that GDB uses if it exists. */
9908 if (lang == language_ada
9909 || (lang == language_fortran && physname))
9910 {
9911 /* For Ada unit, we prefer the linkage name over the name, as
9912 the former contains the exported name, which the user expects
9913 to be able to reference. Ideally, we want the user to be able
9914 to reference this entity using either natural or linkage name,
9915 but we haven't started looking at this enhancement yet. */
9916 const char *linkage_name = dw2_linkage_name (die, cu);
9917
9918 if (linkage_name != NULL)
9919 return linkage_name;
9920 }
9921
9922 /* These are the only languages we know how to qualify names in. */
9923 if (name != NULL
9924 && (lang == language_cplus
9925 || lang == language_fortran || lang == language_d
9926 || lang == language_rust))
9927 {
9928 if (die_needs_namespace (die, cu))
9929 {
9930 const char *prefix;
9931 const char *canonical_name = NULL;
9932
9933 string_file buf;
9934
9935 prefix = determine_prefix (die, cu);
9936 if (*prefix != '\0')
9937 {
9938 gdb::unique_xmalloc_ptr<char> prefixed_name
9939 (typename_concat (NULL, prefix, name, physname, cu));
9940
9941 buf.puts (prefixed_name.get ());
9942 }
9943 else
9944 buf.puts (name);
9945
9946 /* Template parameters may be specified in the DIE's DW_AT_name, or
9947 as children with DW_TAG_template_type_param or
9948 DW_TAG_value_type_param. If the latter, add them to the name
9949 here. If the name already has template parameters, then
9950 skip this step; some versions of GCC emit both, and
9951 it is more efficient to use the pre-computed name.
9952
9953 Something to keep in mind about this process: it is very
9954 unlikely, or in some cases downright impossible, to produce
9955 something that will match the mangled name of a function.
9956 If the definition of the function has the same debug info,
9957 we should be able to match up with it anyway. But fallbacks
9958 using the minimal symbol, for instance to find a method
9959 implemented in a stripped copy of libstdc++, will not work.
9960 If we do not have debug info for the definition, we will have to
9961 match them up some other way.
9962
9963 When we do name matching there is a related problem with function
9964 templates; two instantiated function templates are allowed to
9965 differ only by their return types, which we do not add here. */
9966
9967 if (lang == language_cplus && strchr (name, '<') == NULL)
9968 {
9969 struct attribute *attr;
9970 struct die_info *child;
9971 int first = 1;
9972
9973 die->building_fullname = 1;
9974
9975 for (child = die->child; child != NULL; child = child->sibling)
9976 {
9977 struct type *type;
9978 LONGEST value;
9979 const gdb_byte *bytes;
9980 struct dwarf2_locexpr_baton *baton;
9981 struct value *v;
9982
9983 if (child->tag != DW_TAG_template_type_param
9984 && child->tag != DW_TAG_template_value_param)
9985 continue;
9986
9987 if (first)
9988 {
9989 buf.puts ("<");
9990 first = 0;
9991 }
9992 else
9993 buf.puts (", ");
9994
9995 attr = dwarf2_attr (child, DW_AT_type, cu);
9996 if (attr == NULL)
9997 {
9998 complaint (_("template parameter missing DW_AT_type"));
9999 buf.puts ("UNKNOWN_TYPE");
10000 continue;
10001 }
10002 type = die_type (child, cu);
10003
10004 if (child->tag == DW_TAG_template_type_param)
10005 {
10006 cu->language_defn->print_type (type, "", &buf, -1, 0,
10007 &type_print_raw_options);
10008 continue;
10009 }
10010
10011 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10012 if (attr == NULL)
10013 {
10014 complaint (_("template parameter missing "
10015 "DW_AT_const_value"));
10016 buf.puts ("UNKNOWN_VALUE");
10017 continue;
10018 }
10019
10020 dwarf2_const_value_attr (attr, type, name,
10021 &cu->comp_unit_obstack, cu,
10022 &value, &bytes, &baton);
10023
10024 if (type->has_no_signedness ())
10025 /* GDB prints characters as NUMBER 'CHAR'. If that's
10026 changed, this can use value_print instead. */
10027 cu->language_defn->printchar (value, type, &buf);
10028 else
10029 {
10030 struct value_print_options opts;
10031
10032 if (baton != NULL)
10033 v = dwarf2_evaluate_loc_desc (type, NULL,
10034 baton->data,
10035 baton->size,
10036 baton->per_cu,
10037 baton->per_objfile);
10038 else if (bytes != NULL)
10039 {
10040 v = allocate_value (type);
10041 memcpy (value_contents_writeable (v).data (), bytes,
10042 TYPE_LENGTH (type));
10043 }
10044 else
10045 v = value_from_longest (type, value);
10046
10047 /* Specify decimal so that we do not depend on
10048 the radix. */
10049 get_formatted_print_options (&opts, 'd');
10050 opts.raw = 1;
10051 value_print (v, &buf, &opts);
10052 release_value (v);
10053 }
10054 }
10055
10056 die->building_fullname = 0;
10057
10058 if (!first)
10059 {
10060 /* Close the argument list, with a space if necessary
10061 (nested templates). */
10062 if (!buf.empty () && buf.string ().back () == '>')
10063 buf.puts (" >");
10064 else
10065 buf.puts (">");
10066 }
10067 }
10068
10069 /* For C++ methods, append formal parameter type
10070 information, if PHYSNAME. */
10071
10072 if (physname && die->tag == DW_TAG_subprogram
10073 && lang == language_cplus)
10074 {
10075 struct type *type = read_type_die (die, cu);
10076
10077 c_type_print_args (type, &buf, 1, lang,
10078 &type_print_raw_options);
10079
10080 if (lang == language_cplus)
10081 {
10082 /* Assume that an artificial first parameter is
10083 "this", but do not crash if it is not. RealView
10084 marks unnamed (and thus unused) parameters as
10085 artificial; there is no way to differentiate
10086 the two cases. */
10087 if (type->num_fields () > 0
10088 && TYPE_FIELD_ARTIFICIAL (type, 0)
10089 && type->field (0).type ()->code () == TYPE_CODE_PTR
10090 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10091 buf.puts (" const");
10092 }
10093 }
10094
10095 const std::string &intermediate_name = buf.string ();
10096
10097 if (lang == language_cplus)
10098 canonical_name
10099 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10100 objfile);
10101
10102 /* If we only computed INTERMEDIATE_NAME, or if
10103 INTERMEDIATE_NAME is already canonical, then we need to
10104 intern it. */
10105 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10106 name = objfile->intern (intermediate_name);
10107 else
10108 name = canonical_name;
10109 }
10110 }
10111
10112 return name;
10113 }
10114
10115 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10116 If scope qualifiers are appropriate they will be added. The result
10117 will be allocated on the storage_obstack, or NULL if the DIE does
10118 not have a name. NAME may either be from a previous call to
10119 dwarf2_name or NULL.
10120
10121 The output string will be canonicalized (if C++). */
10122
10123 static const char *
10124 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10125 {
10126 return dwarf2_compute_name (name, die, cu, 0);
10127 }
10128
10129 /* Construct a physname for the given DIE in CU. NAME may either be
10130 from a previous call to dwarf2_name or NULL. The result will be
10131 allocated on the objfile_objstack or NULL if the DIE does not have a
10132 name.
10133
10134 The output string will be canonicalized (if C++). */
10135
10136 static const char *
10137 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10138 {
10139 struct objfile *objfile = cu->per_objfile->objfile;
10140 const char *retval, *mangled = NULL, *canon = NULL;
10141 int need_copy = 1;
10142
10143 /* In this case dwarf2_compute_name is just a shortcut not building anything
10144 on its own. */
10145 if (!die_needs_namespace (die, cu))
10146 return dwarf2_compute_name (name, die, cu, 1);
10147
10148 if (cu->per_cu->lang != language_rust)
10149 mangled = dw2_linkage_name (die, cu);
10150
10151 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10152 has computed. */
10153 gdb::unique_xmalloc_ptr<char> demangled;
10154 if (mangled != NULL)
10155 {
10156 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10157 {
10158 /* Do nothing (do not demangle the symbol name). */
10159 }
10160 else
10161 {
10162 /* Use DMGL_RET_DROP for C++ template functions to suppress
10163 their return type. It is easier for GDB users to search
10164 for such functions as `name(params)' than `long name(params)'.
10165 In such case the minimal symbol names do not match the full
10166 symbol names but for template functions there is never a need
10167 to look up their definition from their declaration so
10168 the only disadvantage remains the minimal symbol variant
10169 `long name(params)' does not have the proper inferior type. */
10170 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
10171 | DMGL_RET_DROP));
10172 }
10173 if (demangled)
10174 canon = demangled.get ();
10175 else
10176 {
10177 canon = mangled;
10178 need_copy = 0;
10179 }
10180 }
10181
10182 if (canon == NULL || check_physname)
10183 {
10184 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10185
10186 if (canon != NULL && strcmp (physname, canon) != 0)
10187 {
10188 /* It may not mean a bug in GDB. The compiler could also
10189 compute DW_AT_linkage_name incorrectly. But in such case
10190 GDB would need to be bug-to-bug compatible. */
10191
10192 complaint (_("Computed physname <%s> does not match demangled <%s> "
10193 "(from linkage <%s>) - DIE at %s [in module %s]"),
10194 physname, canon, mangled, sect_offset_str (die->sect_off),
10195 objfile_name (objfile));
10196
10197 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10198 is available here - over computed PHYSNAME. It is safer
10199 against both buggy GDB and buggy compilers. */
10200
10201 retval = canon;
10202 }
10203 else
10204 {
10205 retval = physname;
10206 need_copy = 0;
10207 }
10208 }
10209 else
10210 retval = canon;
10211
10212 if (need_copy)
10213 retval = objfile->intern (retval);
10214
10215 return retval;
10216 }
10217
10218 /* Inspect DIE in CU for a namespace alias. If one exists, record
10219 a new symbol for it.
10220
10221 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10222
10223 static int
10224 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10225 {
10226 struct attribute *attr;
10227
10228 /* If the die does not have a name, this is not a namespace
10229 alias. */
10230 attr = dwarf2_attr (die, DW_AT_name, cu);
10231 if (attr != NULL)
10232 {
10233 int num;
10234 struct die_info *d = die;
10235 struct dwarf2_cu *imported_cu = cu;
10236
10237 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10238 keep inspecting DIEs until we hit the underlying import. */
10239 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10240 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10241 {
10242 attr = dwarf2_attr (d, DW_AT_import, cu);
10243 if (attr == NULL)
10244 break;
10245
10246 d = follow_die_ref (d, attr, &imported_cu);
10247 if (d->tag != DW_TAG_imported_declaration)
10248 break;
10249 }
10250
10251 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10252 {
10253 complaint (_("DIE at %s has too many recursively imported "
10254 "declarations"), sect_offset_str (d->sect_off));
10255 return 0;
10256 }
10257
10258 if (attr != NULL)
10259 {
10260 struct type *type;
10261 sect_offset sect_off = attr->get_ref_die_offset ();
10262
10263 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10264 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10265 {
10266 /* This declaration is a global namespace alias. Add
10267 a symbol for it whose type is the aliased namespace. */
10268 new_symbol (die, type, cu);
10269 return 1;
10270 }
10271 }
10272 }
10273
10274 return 0;
10275 }
10276
10277 /* Return the using directives repository (global or local?) to use in the
10278 current context for CU.
10279
10280 For Ada, imported declarations can materialize renamings, which *may* be
10281 global. However it is impossible (for now?) in DWARF to distinguish
10282 "external" imported declarations and "static" ones. As all imported
10283 declarations seem to be static in all other languages, make them all CU-wide
10284 global only in Ada. */
10285
10286 static struct using_direct **
10287 using_directives (struct dwarf2_cu *cu)
10288 {
10289 if (cu->per_cu->lang == language_ada
10290 && cu->get_builder ()->outermost_context_p ())
10291 return cu->get_builder ()->get_global_using_directives ();
10292 else
10293 return cu->get_builder ()->get_local_using_directives ();
10294 }
10295
10296 /* Read the import statement specified by the given die and record it. */
10297
10298 static void
10299 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10300 {
10301 struct objfile *objfile = cu->per_objfile->objfile;
10302 struct attribute *import_attr;
10303 struct die_info *imported_die, *child_die;
10304 struct dwarf2_cu *imported_cu;
10305 const char *imported_name;
10306 const char *imported_name_prefix;
10307 const char *canonical_name;
10308 const char *import_alias;
10309 const char *imported_declaration = NULL;
10310 const char *import_prefix;
10311 std::vector<const char *> excludes;
10312
10313 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10314 if (import_attr == NULL)
10315 {
10316 complaint (_("Tag '%s' has no DW_AT_import"),
10317 dwarf_tag_name (die->tag));
10318 return;
10319 }
10320
10321 imported_cu = cu;
10322 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10323 imported_name = dwarf2_name (imported_die, imported_cu);
10324 if (imported_name == NULL)
10325 {
10326 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10327
10328 The import in the following code:
10329 namespace A
10330 {
10331 typedef int B;
10332 }
10333
10334 int main ()
10335 {
10336 using A::B;
10337 B b;
10338 return b;
10339 }
10340
10341 ...
10342 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10343 <52> DW_AT_decl_file : 1
10344 <53> DW_AT_decl_line : 6
10345 <54> DW_AT_import : <0x75>
10346 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10347 <59> DW_AT_name : B
10348 <5b> DW_AT_decl_file : 1
10349 <5c> DW_AT_decl_line : 2
10350 <5d> DW_AT_type : <0x6e>
10351 ...
10352 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10353 <76> DW_AT_byte_size : 4
10354 <77> DW_AT_encoding : 5 (signed)
10355
10356 imports the wrong die ( 0x75 instead of 0x58 ).
10357 This case will be ignored until the gcc bug is fixed. */
10358 return;
10359 }
10360
10361 /* Figure out the local name after import. */
10362 import_alias = dwarf2_name (die, cu);
10363
10364 /* Figure out where the statement is being imported to. */
10365 import_prefix = determine_prefix (die, cu);
10366
10367 /* Figure out what the scope of the imported die is and prepend it
10368 to the name of the imported die. */
10369 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10370
10371 if (imported_die->tag != DW_TAG_namespace
10372 && imported_die->tag != DW_TAG_module)
10373 {
10374 imported_declaration = imported_name;
10375 canonical_name = imported_name_prefix;
10376 }
10377 else if (strlen (imported_name_prefix) > 0)
10378 canonical_name = obconcat (&objfile->objfile_obstack,
10379 imported_name_prefix,
10380 (cu->per_cu->lang == language_d
10381 ? "."
10382 : "::"),
10383 imported_name, (char *) NULL);
10384 else
10385 canonical_name = imported_name;
10386
10387 if (die->tag == DW_TAG_imported_module
10388 && cu->per_cu->lang == language_fortran)
10389 for (child_die = die->child; child_die && child_die->tag;
10390 child_die = child_die->sibling)
10391 {
10392 /* DWARF-4: A Fortran use statement with a “rename list” may be
10393 represented by an imported module entry with an import attribute
10394 referring to the module and owned entries corresponding to those
10395 entities that are renamed as part of being imported. */
10396
10397 if (child_die->tag != DW_TAG_imported_declaration)
10398 {
10399 complaint (_("child DW_TAG_imported_declaration expected "
10400 "- DIE at %s [in module %s]"),
10401 sect_offset_str (child_die->sect_off),
10402 objfile_name (objfile));
10403 continue;
10404 }
10405
10406 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10407 if (import_attr == NULL)
10408 {
10409 complaint (_("Tag '%s' has no DW_AT_import"),
10410 dwarf_tag_name (child_die->tag));
10411 continue;
10412 }
10413
10414 imported_cu = cu;
10415 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10416 &imported_cu);
10417 imported_name = dwarf2_name (imported_die, imported_cu);
10418 if (imported_name == NULL)
10419 {
10420 complaint (_("child DW_TAG_imported_declaration has unknown "
10421 "imported name - DIE at %s [in module %s]"),
10422 sect_offset_str (child_die->sect_off),
10423 objfile_name (objfile));
10424 continue;
10425 }
10426
10427 excludes.push_back (imported_name);
10428
10429 process_die (child_die, cu);
10430 }
10431
10432 add_using_directive (using_directives (cu),
10433 import_prefix,
10434 canonical_name,
10435 import_alias,
10436 imported_declaration,
10437 excludes,
10438 0,
10439 &objfile->objfile_obstack);
10440 }
10441
10442 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10443 types, but gives them a size of zero. Starting with version 14,
10444 ICC is compatible with GCC. */
10445
10446 static bool
10447 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10448 {
10449 if (!cu->checked_producer)
10450 check_producer (cu);
10451
10452 return cu->producer_is_icc_lt_14;
10453 }
10454
10455 /* ICC generates a DW_AT_type for C void functions. This was observed on
10456 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10457 which says that void functions should not have a DW_AT_type. */
10458
10459 static bool
10460 producer_is_icc (struct dwarf2_cu *cu)
10461 {
10462 if (!cu->checked_producer)
10463 check_producer (cu);
10464
10465 return cu->producer_is_icc;
10466 }
10467
10468 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10469 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10470 this, it was first present in GCC release 4.3.0. */
10471
10472 static bool
10473 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10474 {
10475 if (!cu->checked_producer)
10476 check_producer (cu);
10477
10478 return cu->producer_is_gcc_lt_4_3;
10479 }
10480
10481 static file_and_directory &
10482 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10483 {
10484 if (cu->per_cu->fnd != nullptr)
10485 return *cu->per_cu->fnd;
10486
10487 /* Find the filename. Do not use dwarf2_name here, since the filename
10488 is not a source language identifier. */
10489 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
10490 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
10491
10492 if (res.get_comp_dir () == nullptr
10493 && producer_is_gcc_lt_4_3 (cu)
10494 && res.get_name () != nullptr
10495 && IS_ABSOLUTE_PATH (res.get_name ()))
10496 res.set_comp_dir (ldirname (res.get_name ()));
10497
10498 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
10499 return *cu->per_cu->fnd;
10500 }
10501
10502 /* Handle DW_AT_stmt_list for a compilation unit.
10503 DIE is the DW_TAG_compile_unit die for CU.
10504 COMP_DIR is the compilation directory. LOWPC is passed to
10505 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10506
10507 static void
10508 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10509 const file_and_directory &fnd, CORE_ADDR lowpc) /* ARI: editCase function */
10510 {
10511 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10512 struct attribute *attr;
10513 struct line_header line_header_local;
10514 hashval_t line_header_local_hash;
10515 void **slot;
10516 int decode_mapping;
10517
10518 gdb_assert (! cu->per_cu->is_debug_types);
10519
10520 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10521 if (attr == NULL || !attr->form_is_unsigned ())
10522 return;
10523
10524 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10525
10526 /* The line header hash table is only created if needed (it exists to
10527 prevent redundant reading of the line table for partial_units).
10528 If we're given a partial_unit, we'll need it. If we're given a
10529 compile_unit, then use the line header hash table if it's already
10530 created, but don't create one just yet. */
10531
10532 if (per_objfile->line_header_hash == NULL
10533 && die->tag == DW_TAG_partial_unit)
10534 {
10535 per_objfile->line_header_hash
10536 .reset (htab_create_alloc (127, line_header_hash_voidp,
10537 line_header_eq_voidp,
10538 htab_delete_entry<line_header>,
10539 xcalloc, xfree));
10540 }
10541
10542 line_header_local.sect_off = line_offset;
10543 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10544 line_header_local_hash = line_header_hash (&line_header_local);
10545 if (per_objfile->line_header_hash != NULL)
10546 {
10547 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10548 &line_header_local,
10549 line_header_local_hash, NO_INSERT);
10550
10551 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10552 is not present in *SLOT (since if there is something in *SLOT then
10553 it will be for a partial_unit). */
10554 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10555 {
10556 gdb_assert (*slot != NULL);
10557 cu->line_header = (struct line_header *) *slot;
10558 return;
10559 }
10560 }
10561
10562 /* dwarf_decode_line_header does not yet provide sufficient information.
10563 We always have to call also dwarf_decode_lines for it. */
10564 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10565 if (lh == NULL)
10566 return;
10567
10568 cu->line_header = lh.release ();
10569 cu->line_header_die_owner = die;
10570
10571 if (per_objfile->line_header_hash == NULL)
10572 slot = NULL;
10573 else
10574 {
10575 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10576 &line_header_local,
10577 line_header_local_hash, INSERT);
10578 gdb_assert (slot != NULL);
10579 }
10580 if (slot != NULL && *slot == NULL)
10581 {
10582 /* This newly decoded line number information unit will be owned
10583 by line_header_hash hash table. */
10584 *slot = cu->line_header;
10585 cu->line_header_die_owner = NULL;
10586 }
10587 else
10588 {
10589 /* We cannot free any current entry in (*slot) as that struct line_header
10590 may be already used by multiple CUs. Create only temporary decoded
10591 line_header for this CU - it may happen at most once for each line
10592 number information unit. And if we're not using line_header_hash
10593 then this is what we want as well. */
10594 gdb_assert (die->tag != DW_TAG_partial_unit);
10595 }
10596 decode_mapping = (die->tag != DW_TAG_partial_unit);
10597 dwarf_decode_lines (cu->line_header, fnd, cu, nullptr, lowpc,
10598 decode_mapping);
10599
10600 }
10601
10602 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10603
10604 static void
10605 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10606 {
10607 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10608 struct objfile *objfile = per_objfile->objfile;
10609 struct gdbarch *gdbarch = objfile->arch ();
10610 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10611 CORE_ADDR highpc = ((CORE_ADDR) 0);
10612 struct attribute *attr;
10613 struct die_info *child_die;
10614 CORE_ADDR baseaddr;
10615
10616 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
10617 baseaddr = objfile->text_section_offset ();
10618
10619 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10620
10621 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10622 from finish_block. */
10623 if (lowpc == ((CORE_ADDR) -1))
10624 lowpc = highpc;
10625 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10626
10627 file_and_directory &fnd = find_file_and_directory (die, cu);
10628
10629 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
10630 lowpc);
10631
10632 gdb_assert (per_objfile->sym_cu == nullptr);
10633 scoped_restore restore_sym_cu
10634 = make_scoped_restore (&per_objfile->sym_cu, cu);
10635
10636 /* Decode line number information if present. We do this before
10637 processing child DIEs, so that the line header table is available
10638 for DW_AT_decl_file. The PC check is here because, if LOWPC and
10639 HIGHPC are both 0x0, then there won't be any interesting code in
10640 the CU, but a check later on (in
10641 lnp_state_machine::check_line_address) will fail to properly
10642 exclude an entry that was removed via --gc-sections. */
10643 if (lowpc != highpc)
10644 handle_DW_AT_stmt_list (die, cu, fnd, lowpc);
10645
10646 /* Process all dies in compilation unit. */
10647 if (die->child != NULL)
10648 {
10649 child_die = die->child;
10650 while (child_die && child_die->tag)
10651 {
10652 process_die (child_die, cu);
10653 child_die = child_die->sibling;
10654 }
10655 }
10656 per_objfile->sym_cu = nullptr;
10657
10658 /* Decode macro information, if present. Dwarf 2 macro information
10659 refers to information in the line number info statement program
10660 header, so we can only read it if we've read the header
10661 successfully. */
10662 attr = dwarf2_attr (die, DW_AT_macros, cu);
10663 if (attr == NULL)
10664 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10665 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10666 {
10667 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10668 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10669
10670 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10671 }
10672 else
10673 {
10674 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10675 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10676 {
10677 unsigned int macro_offset = attr->as_unsigned ();
10678
10679 dwarf_decode_macros (cu, macro_offset, 0);
10680 }
10681 }
10682 }
10683
10684 void
10685 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10686 {
10687 struct type_unit_group *tu_group;
10688 int first_time;
10689 struct attribute *attr;
10690 unsigned int i;
10691 struct signatured_type *sig_type;
10692
10693 gdb_assert (per_cu->is_debug_types);
10694 sig_type = (struct signatured_type *) per_cu;
10695
10696 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10697
10698 /* If we're using .gdb_index (includes -readnow) then
10699 per_cu->type_unit_group may not have been set up yet. */
10700 if (sig_type->type_unit_group == NULL)
10701 sig_type->type_unit_group = get_type_unit_group (this, attr);
10702 tu_group = sig_type->type_unit_group;
10703
10704 /* If we've already processed this stmt_list there's no real need to
10705 do it again, we could fake it and just recreate the part we need
10706 (file name,index -> symtab mapping). If data shows this optimization
10707 is useful we can do it then. */
10708 type_unit_group_unshareable *tug_unshare
10709 = per_objfile->get_type_unit_group_unshareable (tu_group);
10710 first_time = tug_unshare->compunit_symtab == NULL;
10711
10712 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10713 debug info. */
10714 line_header_up lh;
10715 if (attr != NULL && attr->form_is_unsigned ())
10716 {
10717 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10718 lh = dwarf_decode_line_header (line_offset, this);
10719 }
10720 if (lh == NULL)
10721 {
10722 if (first_time)
10723 start_compunit_symtab ("", NULL, 0);
10724 else
10725 {
10726 gdb_assert (tug_unshare->symtabs == NULL);
10727 gdb_assert (m_builder == nullptr);
10728 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10729 m_builder.reset (new struct buildsym_compunit
10730 (cust->objfile (), "",
10731 cust->dirname (),
10732 compunit_language (cust),
10733 0, cust));
10734 list_in_scope = get_builder ()->get_file_symbols ();
10735 }
10736 return;
10737 }
10738
10739 line_header = lh.release ();
10740 line_header_die_owner = die;
10741
10742 if (first_time)
10743 {
10744 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
10745
10746 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10747 still initializing it, and our caller (a few levels up)
10748 process_full_type_unit still needs to know if this is the first
10749 time. */
10750
10751 tug_unshare->symtabs
10752 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
10753 struct symtab *, line_header->file_names_size ());
10754
10755 auto &file_names = line_header->file_names ();
10756 for (i = 0; i < file_names.size (); ++i)
10757 {
10758 file_entry &fe = file_names[i];
10759 dwarf2_start_subfile (this, fe.name,
10760 fe.include_dir (line_header));
10761 buildsym_compunit *b = get_builder ();
10762 if (b->get_current_subfile ()->symtab == NULL)
10763 {
10764 /* NOTE: start_subfile will recognize when it's been
10765 passed a file it has already seen. So we can't
10766 assume there's a simple mapping from
10767 cu->line_header->file_names to subfiles, plus
10768 cu->line_header->file_names may contain dups. */
10769 b->get_current_subfile ()->symtab
10770 = allocate_symtab (cust, b->get_current_subfile ()->name);
10771 }
10772
10773 fe.symtab = b->get_current_subfile ()->symtab;
10774 tug_unshare->symtabs[i] = fe.symtab;
10775 }
10776 }
10777 else
10778 {
10779 gdb_assert (m_builder == nullptr);
10780 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10781 m_builder.reset (new struct buildsym_compunit
10782 (cust->objfile (), "",
10783 cust->dirname (),
10784 compunit_language (cust),
10785 0, cust));
10786 list_in_scope = get_builder ()->get_file_symbols ();
10787
10788 auto &file_names = line_header->file_names ();
10789 for (i = 0; i < file_names.size (); ++i)
10790 {
10791 file_entry &fe = file_names[i];
10792 fe.symtab = tug_unshare->symtabs[i];
10793 }
10794 }
10795
10796 /* The main symtab is allocated last. Type units don't have DW_AT_name
10797 so they don't have a "real" (so to speak) symtab anyway.
10798 There is later code that will assign the main symtab to all symbols
10799 that don't have one. We need to handle the case of a symbol with a
10800 missing symtab (DW_AT_decl_file) anyway. */
10801 }
10802
10803 /* Process DW_TAG_type_unit.
10804 For TUs we want to skip the first top level sibling if it's not the
10805 actual type being defined by this TU. In this case the first top
10806 level sibling is there to provide context only. */
10807
10808 static void
10809 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10810 {
10811 struct die_info *child_die;
10812
10813 prepare_one_comp_unit (cu, die, language_minimal);
10814
10815 /* Initialize (or reinitialize) the machinery for building symtabs.
10816 We do this before processing child DIEs, so that the line header table
10817 is available for DW_AT_decl_file. */
10818 cu->setup_type_unit_groups (die);
10819
10820 if (die->child != NULL)
10821 {
10822 child_die = die->child;
10823 while (child_die && child_die->tag)
10824 {
10825 process_die (child_die, cu);
10826 child_die = child_die->sibling;
10827 }
10828 }
10829 }
10830 \f
10831 /* DWO/DWP files.
10832
10833 http://gcc.gnu.org/wiki/DebugFission
10834 http://gcc.gnu.org/wiki/DebugFissionDWP
10835
10836 To simplify handling of both DWO files ("object" files with the DWARF info)
10837 and DWP files (a file with the DWOs packaged up into one file), we treat
10838 DWP files as having a collection of virtual DWO files. */
10839
10840 static hashval_t
10841 hash_dwo_file (const void *item)
10842 {
10843 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10844 hashval_t hash;
10845
10846 hash = htab_hash_string (dwo_file->dwo_name);
10847 if (dwo_file->comp_dir != NULL)
10848 hash += htab_hash_string (dwo_file->comp_dir);
10849 return hash;
10850 }
10851
10852 static int
10853 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10854 {
10855 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10856 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10857
10858 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10859 return 0;
10860 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10861 return lhs->comp_dir == rhs->comp_dir;
10862 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10863 }
10864
10865 /* Allocate a hash table for DWO files. */
10866
10867 static htab_up
10868 allocate_dwo_file_hash_table ()
10869 {
10870 return htab_up (htab_create_alloc (41,
10871 hash_dwo_file,
10872 eq_dwo_file,
10873 htab_delete_entry<dwo_file>,
10874 xcalloc, xfree));
10875 }
10876
10877 /* Lookup DWO file DWO_NAME. */
10878
10879 static void **
10880 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10881 const char *dwo_name,
10882 const char *comp_dir)
10883 {
10884 struct dwo_file find_entry;
10885 void **slot;
10886
10887 if (per_objfile->per_bfd->dwo_files == NULL)
10888 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10889
10890 find_entry.dwo_name = dwo_name;
10891 find_entry.comp_dir = comp_dir;
10892 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10893 INSERT);
10894
10895 return slot;
10896 }
10897
10898 static hashval_t
10899 hash_dwo_unit (const void *item)
10900 {
10901 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10902
10903 /* This drops the top 32 bits of the id, but is ok for a hash. */
10904 return dwo_unit->signature;
10905 }
10906
10907 static int
10908 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10909 {
10910 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10911 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10912
10913 /* The signature is assumed to be unique within the DWO file.
10914 So while object file CU dwo_id's always have the value zero,
10915 that's OK, assuming each object file DWO file has only one CU,
10916 and that's the rule for now. */
10917 return lhs->signature == rhs->signature;
10918 }
10919
10920 /* Allocate a hash table for DWO CUs,TUs.
10921 There is one of these tables for each of CUs,TUs for each DWO file. */
10922
10923 static htab_up
10924 allocate_dwo_unit_table ()
10925 {
10926 /* Start out with a pretty small number.
10927 Generally DWO files contain only one CU and maybe some TUs. */
10928 return htab_up (htab_create_alloc (3,
10929 hash_dwo_unit,
10930 eq_dwo_unit,
10931 NULL, xcalloc, xfree));
10932 }
10933
10934 /* die_reader_func for create_dwo_cu. */
10935
10936 static void
10937 create_dwo_cu_reader (const struct die_reader_specs *reader,
10938 const gdb_byte *info_ptr,
10939 struct die_info *comp_unit_die,
10940 struct dwo_file *dwo_file,
10941 struct dwo_unit *dwo_unit)
10942 {
10943 struct dwarf2_cu *cu = reader->cu;
10944 sect_offset sect_off = cu->per_cu->sect_off;
10945 struct dwarf2_section_info *section = cu->per_cu->section;
10946
10947 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10948 if (!signature.has_value ())
10949 {
10950 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10951 " its dwo_id [in module %s]"),
10952 sect_offset_str (sect_off), dwo_file->dwo_name);
10953 return;
10954 }
10955
10956 dwo_unit->dwo_file = dwo_file;
10957 dwo_unit->signature = *signature;
10958 dwo_unit->section = section;
10959 dwo_unit->sect_off = sect_off;
10960 dwo_unit->length = cu->per_cu->length;
10961
10962 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10963 sect_offset_str (sect_off),
10964 hex_string (dwo_unit->signature));
10965 }
10966
10967 /* Create the dwo_units for the CUs in a DWO_FILE.
10968 Note: This function processes DWO files only, not DWP files. */
10969
10970 static void
10971 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10972 dwarf2_cu *cu, struct dwo_file &dwo_file,
10973 dwarf2_section_info &section, htab_up &cus_htab)
10974 {
10975 struct objfile *objfile = per_objfile->objfile;
10976 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10977 const gdb_byte *info_ptr, *end_ptr;
10978
10979 section.read (objfile);
10980 info_ptr = section.buffer;
10981
10982 if (info_ptr == NULL)
10983 return;
10984
10985 dwarf_read_debug_printf ("Reading %s for %s:",
10986 section.get_name (),
10987 section.get_file_name ());
10988
10989 end_ptr = info_ptr + section.size;
10990 while (info_ptr < end_ptr)
10991 {
10992 struct dwarf2_per_cu_data per_cu;
10993 struct dwo_unit read_unit {};
10994 struct dwo_unit *dwo_unit;
10995 void **slot;
10996 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10997
10998 per_cu.per_bfd = per_bfd;
10999 per_cu.is_debug_types = 0;
11000 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11001 per_cu.section = &section;
11002
11003 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11004 if (!reader.dummy_p)
11005 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11006 &dwo_file, &read_unit);
11007 info_ptr += per_cu.length;
11008
11009 // If the unit could not be parsed, skip it.
11010 if (read_unit.dwo_file == NULL)
11011 continue;
11012
11013 if (cus_htab == NULL)
11014 cus_htab = allocate_dwo_unit_table ();
11015
11016 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11017 struct dwo_unit);
11018 *dwo_unit = read_unit;
11019 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11020 gdb_assert (slot != NULL);
11021 if (*slot != NULL)
11022 {
11023 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11024 sect_offset dup_sect_off = dup_cu->sect_off;
11025
11026 complaint (_("debug cu entry at offset %s is duplicate to"
11027 " the entry at offset %s, signature %s"),
11028 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11029 hex_string (dwo_unit->signature));
11030 }
11031 *slot = (void *)dwo_unit;
11032 }
11033 }
11034
11035 /* DWP file .debug_{cu,tu}_index section format:
11036 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11037 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11038
11039 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11040 officially standard DWP format was published with DWARF v5 and is called
11041 Version 5. There are no versions 3 or 4.
11042
11043 DWP Version 1:
11044
11045 Both index sections have the same format, and serve to map a 64-bit
11046 signature to a set of section numbers. Each section begins with a header,
11047 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11048 indexes, and a pool of 32-bit section numbers. The index sections will be
11049 aligned at 8-byte boundaries in the file.
11050
11051 The index section header consists of:
11052
11053 V, 32 bit version number
11054 -, 32 bits unused
11055 N, 32 bit number of compilation units or type units in the index
11056 M, 32 bit number of slots in the hash table
11057
11058 Numbers are recorded using the byte order of the application binary.
11059
11060 The hash table begins at offset 16 in the section, and consists of an array
11061 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11062 order of the application binary). Unused slots in the hash table are 0.
11063 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11064
11065 The parallel table begins immediately after the hash table
11066 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11067 array of 32-bit indexes (using the byte order of the application binary),
11068 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11069 table contains a 32-bit index into the pool of section numbers. For unused
11070 hash table slots, the corresponding entry in the parallel table will be 0.
11071
11072 The pool of section numbers begins immediately following the hash table
11073 (at offset 16 + 12 * M from the beginning of the section). The pool of
11074 section numbers consists of an array of 32-bit words (using the byte order
11075 of the application binary). Each item in the array is indexed starting
11076 from 0. The hash table entry provides the index of the first section
11077 number in the set. Additional section numbers in the set follow, and the
11078 set is terminated by a 0 entry (section number 0 is not used in ELF).
11079
11080 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11081 section must be the first entry in the set, and the .debug_abbrev.dwo must
11082 be the second entry. Other members of the set may follow in any order.
11083
11084 ---
11085
11086 DWP Versions 2 and 5:
11087
11088 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11089 and the entries in the index tables are now offsets into these sections.
11090 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11091 section.
11092
11093 Index Section Contents:
11094 Header
11095 Hash Table of Signatures dwp_hash_table.hash_table
11096 Parallel Table of Indices dwp_hash_table.unit_table
11097 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11098 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11099
11100 The index section header consists of:
11101
11102 V, 32 bit version number
11103 L, 32 bit number of columns in the table of section offsets
11104 N, 32 bit number of compilation units or type units in the index
11105 M, 32 bit number of slots in the hash table
11106
11107 Numbers are recorded using the byte order of the application binary.
11108
11109 The hash table has the same format as version 1.
11110 The parallel table of indices has the same format as version 1,
11111 except that the entries are origin-1 indices into the table of sections
11112 offsets and the table of section sizes.
11113
11114 The table of offsets begins immediately following the parallel table
11115 (at offset 16 + 12 * M from the beginning of the section). The table is
11116 a two-dimensional array of 32-bit words (using the byte order of the
11117 application binary), with L columns and N+1 rows, in row-major order.
11118 Each row in the array is indexed starting from 0. The first row provides
11119 a key to the remaining rows: each column in this row provides an identifier
11120 for a debug section, and the offsets in the same column of subsequent rows
11121 refer to that section. The section identifiers for Version 2 are:
11122
11123 DW_SECT_INFO 1 .debug_info.dwo
11124 DW_SECT_TYPES 2 .debug_types.dwo
11125 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11126 DW_SECT_LINE 4 .debug_line.dwo
11127 DW_SECT_LOC 5 .debug_loc.dwo
11128 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11129 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11130 DW_SECT_MACRO 8 .debug_macro.dwo
11131
11132 The section identifiers for Version 5 are:
11133
11134 DW_SECT_INFO_V5 1 .debug_info.dwo
11135 DW_SECT_RESERVED_V5 2 --
11136 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11137 DW_SECT_LINE_V5 4 .debug_line.dwo
11138 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11139 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11140 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11141 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11142
11143 The offsets provided by the CU and TU index sections are the base offsets
11144 for the contributions made by each CU or TU to the corresponding section
11145 in the package file. Each CU and TU header contains an abbrev_offset
11146 field, used to find the abbreviations table for that CU or TU within the
11147 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11148 be interpreted as relative to the base offset given in the index section.
11149 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11150 should be interpreted as relative to the base offset for .debug_line.dwo,
11151 and offsets into other debug sections obtained from DWARF attributes should
11152 also be interpreted as relative to the corresponding base offset.
11153
11154 The table of sizes begins immediately following the table of offsets.
11155 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11156 with L columns and N rows, in row-major order. Each row in the array is
11157 indexed starting from 1 (row 0 is shared by the two tables).
11158
11159 ---
11160
11161 Hash table lookup is handled the same in version 1 and 2:
11162
11163 We assume that N and M will not exceed 2^32 - 1.
11164 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11165
11166 Given a 64-bit compilation unit signature or a type signature S, an entry
11167 in the hash table is located as follows:
11168
11169 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11170 the low-order k bits all set to 1.
11171
11172 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11173
11174 3) If the hash table entry at index H matches the signature, use that
11175 entry. If the hash table entry at index H is unused (all zeroes),
11176 terminate the search: the signature is not present in the table.
11177
11178 4) Let H = (H + H') modulo M. Repeat at Step 3.
11179
11180 Because M > N and H' and M are relatively prime, the search is guaranteed
11181 to stop at an unused slot or find the match. */
11182
11183 /* Create a hash table to map DWO IDs to their CU/TU entry in
11184 .debug_{info,types}.dwo in DWP_FILE.
11185 Returns NULL if there isn't one.
11186 Note: This function processes DWP files only, not DWO files. */
11187
11188 static struct dwp_hash_table *
11189 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11190 struct dwp_file *dwp_file, int is_debug_types)
11191 {
11192 struct objfile *objfile = per_objfile->objfile;
11193 bfd *dbfd = dwp_file->dbfd.get ();
11194 const gdb_byte *index_ptr, *index_end;
11195 struct dwarf2_section_info *index;
11196 uint32_t version, nr_columns, nr_units, nr_slots;
11197 struct dwp_hash_table *htab;
11198
11199 if (is_debug_types)
11200 index = &dwp_file->sections.tu_index;
11201 else
11202 index = &dwp_file->sections.cu_index;
11203
11204 if (index->empty ())
11205 return NULL;
11206 index->read (objfile);
11207
11208 index_ptr = index->buffer;
11209 index_end = index_ptr + index->size;
11210
11211 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11212 For now it's safe to just read 4 bytes (particularly as it's difficult to
11213 tell if you're dealing with Version 5 before you've read the version). */
11214 version = read_4_bytes (dbfd, index_ptr);
11215 index_ptr += 4;
11216 if (version == 2 || version == 5)
11217 nr_columns = read_4_bytes (dbfd, index_ptr);
11218 else
11219 nr_columns = 0;
11220 index_ptr += 4;
11221 nr_units = read_4_bytes (dbfd, index_ptr);
11222 index_ptr += 4;
11223 nr_slots = read_4_bytes (dbfd, index_ptr);
11224 index_ptr += 4;
11225
11226 if (version != 1 && version != 2 && version != 5)
11227 {
11228 error (_("Dwarf Error: unsupported DWP file version (%s)"
11229 " [in module %s]"),
11230 pulongest (version), dwp_file->name);
11231 }
11232 if (nr_slots != (nr_slots & -nr_slots))
11233 {
11234 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11235 " is not power of 2 [in module %s]"),
11236 pulongest (nr_slots), dwp_file->name);
11237 }
11238
11239 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11240 htab->version = version;
11241 htab->nr_columns = nr_columns;
11242 htab->nr_units = nr_units;
11243 htab->nr_slots = nr_slots;
11244 htab->hash_table = index_ptr;
11245 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11246
11247 /* Exit early if the table is empty. */
11248 if (nr_slots == 0 || nr_units == 0
11249 || (version == 2 && nr_columns == 0)
11250 || (version == 5 && nr_columns == 0))
11251 {
11252 /* All must be zero. */
11253 if (nr_slots != 0 || nr_units != 0
11254 || (version == 2 && nr_columns != 0)
11255 || (version == 5 && nr_columns != 0))
11256 {
11257 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11258 " all zero [in modules %s]"),
11259 dwp_file->name);
11260 }
11261 return htab;
11262 }
11263
11264 if (version == 1)
11265 {
11266 htab->section_pool.v1.indices =
11267 htab->unit_table + sizeof (uint32_t) * nr_slots;
11268 /* It's harder to decide whether the section is too small in v1.
11269 V1 is deprecated anyway so we punt. */
11270 }
11271 else if (version == 2)
11272 {
11273 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11274 int *ids = htab->section_pool.v2.section_ids;
11275 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11276 /* Reverse map for error checking. */
11277 int ids_seen[DW_SECT_MAX + 1];
11278 int i;
11279
11280 if (nr_columns < 2)
11281 {
11282 error (_("Dwarf Error: bad DWP hash table, too few columns"
11283 " in section table [in module %s]"),
11284 dwp_file->name);
11285 }
11286 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11287 {
11288 error (_("Dwarf Error: bad DWP hash table, too many columns"
11289 " in section table [in module %s]"),
11290 dwp_file->name);
11291 }
11292 memset (ids, 255, sizeof_ids);
11293 memset (ids_seen, 255, sizeof (ids_seen));
11294 for (i = 0; i < nr_columns; ++i)
11295 {
11296 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11297
11298 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11299 {
11300 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11301 " in section table [in module %s]"),
11302 id, dwp_file->name);
11303 }
11304 if (ids_seen[id] != -1)
11305 {
11306 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11307 " id %d in section table [in module %s]"),
11308 id, dwp_file->name);
11309 }
11310 ids_seen[id] = i;
11311 ids[i] = id;
11312 }
11313 /* Must have exactly one info or types section. */
11314 if (((ids_seen[DW_SECT_INFO] != -1)
11315 + (ids_seen[DW_SECT_TYPES] != -1))
11316 != 1)
11317 {
11318 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11319 " DWO info/types section [in module %s]"),
11320 dwp_file->name);
11321 }
11322 /* Must have an abbrev section. */
11323 if (ids_seen[DW_SECT_ABBREV] == -1)
11324 {
11325 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11326 " section [in module %s]"),
11327 dwp_file->name);
11328 }
11329 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11330 htab->section_pool.v2.sizes =
11331 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11332 * nr_units * nr_columns);
11333 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11334 * nr_units * nr_columns))
11335 > index_end)
11336 {
11337 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11338 " [in module %s]"),
11339 dwp_file->name);
11340 }
11341 }
11342 else /* version == 5 */
11343 {
11344 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11345 int *ids = htab->section_pool.v5.section_ids;
11346 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11347 /* Reverse map for error checking. */
11348 int ids_seen[DW_SECT_MAX_V5 + 1];
11349
11350 if (nr_columns < 2)
11351 {
11352 error (_("Dwarf Error: bad DWP hash table, too few columns"
11353 " in section table [in module %s]"),
11354 dwp_file->name);
11355 }
11356 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11357 {
11358 error (_("Dwarf Error: bad DWP hash table, too many columns"
11359 " in section table [in module %s]"),
11360 dwp_file->name);
11361 }
11362 memset (ids, 255, sizeof_ids);
11363 memset (ids_seen, 255, sizeof (ids_seen));
11364 for (int i = 0; i < nr_columns; ++i)
11365 {
11366 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11367
11368 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11369 {
11370 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11371 " in section table [in module %s]"),
11372 id, dwp_file->name);
11373 }
11374 if (ids_seen[id] != -1)
11375 {
11376 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11377 " id %d in section table [in module %s]"),
11378 id, dwp_file->name);
11379 }
11380 ids_seen[id] = i;
11381 ids[i] = id;
11382 }
11383 /* Must have seen an info section. */
11384 if (ids_seen[DW_SECT_INFO_V5] == -1)
11385 {
11386 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11387 " DWO info/types section [in module %s]"),
11388 dwp_file->name);
11389 }
11390 /* Must have an abbrev section. */
11391 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11392 {
11393 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11394 " section [in module %s]"),
11395 dwp_file->name);
11396 }
11397 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11398 htab->section_pool.v5.sizes
11399 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11400 * nr_units * nr_columns);
11401 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11402 * nr_units * nr_columns))
11403 > index_end)
11404 {
11405 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11406 " [in module %s]"),
11407 dwp_file->name);
11408 }
11409 }
11410
11411 return htab;
11412 }
11413
11414 /* Update SECTIONS with the data from SECTP.
11415
11416 This function is like the other "locate" section routines, but in
11417 this context the sections to read comes from the DWP V1 hash table,
11418 not the full ELF section table.
11419
11420 The result is non-zero for success, or zero if an error was found. */
11421
11422 static int
11423 locate_v1_virtual_dwo_sections (asection *sectp,
11424 struct virtual_v1_dwo_sections *sections)
11425 {
11426 const struct dwop_section_names *names = &dwop_section_names;
11427
11428 if (names->abbrev_dwo.matches (sectp->name))
11429 {
11430 /* There can be only one. */
11431 if (sections->abbrev.s.section != NULL)
11432 return 0;
11433 sections->abbrev.s.section = sectp;
11434 sections->abbrev.size = bfd_section_size (sectp);
11435 }
11436 else if (names->info_dwo.matches (sectp->name)
11437 || names->types_dwo.matches (sectp->name))
11438 {
11439 /* There can be only one. */
11440 if (sections->info_or_types.s.section != NULL)
11441 return 0;
11442 sections->info_or_types.s.section = sectp;
11443 sections->info_or_types.size = bfd_section_size (sectp);
11444 }
11445 else if (names->line_dwo.matches (sectp->name))
11446 {
11447 /* There can be only one. */
11448 if (sections->line.s.section != NULL)
11449 return 0;
11450 sections->line.s.section = sectp;
11451 sections->line.size = bfd_section_size (sectp);
11452 }
11453 else if (names->loc_dwo.matches (sectp->name))
11454 {
11455 /* There can be only one. */
11456 if (sections->loc.s.section != NULL)
11457 return 0;
11458 sections->loc.s.section = sectp;
11459 sections->loc.size = bfd_section_size (sectp);
11460 }
11461 else if (names->macinfo_dwo.matches (sectp->name))
11462 {
11463 /* There can be only one. */
11464 if (sections->macinfo.s.section != NULL)
11465 return 0;
11466 sections->macinfo.s.section = sectp;
11467 sections->macinfo.size = bfd_section_size (sectp);
11468 }
11469 else if (names->macro_dwo.matches (sectp->name))
11470 {
11471 /* There can be only one. */
11472 if (sections->macro.s.section != NULL)
11473 return 0;
11474 sections->macro.s.section = sectp;
11475 sections->macro.size = bfd_section_size (sectp);
11476 }
11477 else if (names->str_offsets_dwo.matches (sectp->name))
11478 {
11479 /* There can be only one. */
11480 if (sections->str_offsets.s.section != NULL)
11481 return 0;
11482 sections->str_offsets.s.section = sectp;
11483 sections->str_offsets.size = bfd_section_size (sectp);
11484 }
11485 else
11486 {
11487 /* No other kind of section is valid. */
11488 return 0;
11489 }
11490
11491 return 1;
11492 }
11493
11494 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11495 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11496 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11497 This is for DWP version 1 files. */
11498
11499 static struct dwo_unit *
11500 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11501 struct dwp_file *dwp_file,
11502 uint32_t unit_index,
11503 const char *comp_dir,
11504 ULONGEST signature, int is_debug_types)
11505 {
11506 const struct dwp_hash_table *dwp_htab =
11507 is_debug_types ? dwp_file->tus : dwp_file->cus;
11508 bfd *dbfd = dwp_file->dbfd.get ();
11509 const char *kind = is_debug_types ? "TU" : "CU";
11510 struct dwo_file *dwo_file;
11511 struct dwo_unit *dwo_unit;
11512 struct virtual_v1_dwo_sections sections;
11513 void **dwo_file_slot;
11514 int i;
11515
11516 gdb_assert (dwp_file->version == 1);
11517
11518 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11519 kind, pulongest (unit_index), hex_string (signature),
11520 dwp_file->name);
11521
11522 /* Fetch the sections of this DWO unit.
11523 Put a limit on the number of sections we look for so that bad data
11524 doesn't cause us to loop forever. */
11525
11526 #define MAX_NR_V1_DWO_SECTIONS \
11527 (1 /* .debug_info or .debug_types */ \
11528 + 1 /* .debug_abbrev */ \
11529 + 1 /* .debug_line */ \
11530 + 1 /* .debug_loc */ \
11531 + 1 /* .debug_str_offsets */ \
11532 + 1 /* .debug_macro or .debug_macinfo */ \
11533 + 1 /* trailing zero */)
11534
11535 memset (&sections, 0, sizeof (sections));
11536
11537 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11538 {
11539 asection *sectp;
11540 uint32_t section_nr =
11541 read_4_bytes (dbfd,
11542 dwp_htab->section_pool.v1.indices
11543 + (unit_index + i) * sizeof (uint32_t));
11544
11545 if (section_nr == 0)
11546 break;
11547 if (section_nr >= dwp_file->num_sections)
11548 {
11549 error (_("Dwarf Error: bad DWP hash table, section number too large"
11550 " [in module %s]"),
11551 dwp_file->name);
11552 }
11553
11554 sectp = dwp_file->elf_sections[section_nr];
11555 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11556 {
11557 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11558 " [in module %s]"),
11559 dwp_file->name);
11560 }
11561 }
11562
11563 if (i < 2
11564 || sections.info_or_types.empty ()
11565 || sections.abbrev.empty ())
11566 {
11567 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11568 " [in module %s]"),
11569 dwp_file->name);
11570 }
11571 if (i == MAX_NR_V1_DWO_SECTIONS)
11572 {
11573 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11574 " [in module %s]"),
11575 dwp_file->name);
11576 }
11577
11578 /* It's easier for the rest of the code if we fake a struct dwo_file and
11579 have dwo_unit "live" in that. At least for now.
11580
11581 The DWP file can be made up of a random collection of CUs and TUs.
11582 However, for each CU + set of TUs that came from the same original DWO
11583 file, we can combine them back into a virtual DWO file to save space
11584 (fewer struct dwo_file objects to allocate). Remember that for really
11585 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11586
11587 std::string virtual_dwo_name =
11588 string_printf ("virtual-dwo/%d-%d-%d-%d",
11589 sections.abbrev.get_id (),
11590 sections.line.get_id (),
11591 sections.loc.get_id (),
11592 sections.str_offsets.get_id ());
11593 /* Can we use an existing virtual DWO file? */
11594 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11595 comp_dir);
11596 /* Create one if necessary. */
11597 if (*dwo_file_slot == NULL)
11598 {
11599 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11600 virtual_dwo_name.c_str ());
11601
11602 dwo_file = new struct dwo_file;
11603 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11604 dwo_file->comp_dir = comp_dir;
11605 dwo_file->sections.abbrev = sections.abbrev;
11606 dwo_file->sections.line = sections.line;
11607 dwo_file->sections.loc = sections.loc;
11608 dwo_file->sections.macinfo = sections.macinfo;
11609 dwo_file->sections.macro = sections.macro;
11610 dwo_file->sections.str_offsets = sections.str_offsets;
11611 /* The "str" section is global to the entire DWP file. */
11612 dwo_file->sections.str = dwp_file->sections.str;
11613 /* The info or types section is assigned below to dwo_unit,
11614 there's no need to record it in dwo_file.
11615 Also, we can't simply record type sections in dwo_file because
11616 we record a pointer into the vector in dwo_unit. As we collect more
11617 types we'll grow the vector and eventually have to reallocate space
11618 for it, invalidating all copies of pointers into the previous
11619 contents. */
11620 *dwo_file_slot = dwo_file;
11621 }
11622 else
11623 {
11624 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11625 virtual_dwo_name.c_str ());
11626
11627 dwo_file = (struct dwo_file *) *dwo_file_slot;
11628 }
11629
11630 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11631 dwo_unit->dwo_file = dwo_file;
11632 dwo_unit->signature = signature;
11633 dwo_unit->section =
11634 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11635 *dwo_unit->section = sections.info_or_types;
11636 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11637
11638 return dwo_unit;
11639 }
11640
11641 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11642 simplify them. Given a pointer to the containing section SECTION, and
11643 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11644 virtual section of just that piece. */
11645
11646 static struct dwarf2_section_info
11647 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11648 struct dwarf2_section_info *section,
11649 bfd_size_type offset, bfd_size_type size)
11650 {
11651 struct dwarf2_section_info result;
11652 asection *sectp;
11653
11654 gdb_assert (section != NULL);
11655 gdb_assert (!section->is_virtual);
11656
11657 memset (&result, 0, sizeof (result));
11658 result.s.containing_section = section;
11659 result.is_virtual = true;
11660
11661 if (size == 0)
11662 return result;
11663
11664 sectp = section->get_bfd_section ();
11665
11666 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11667 bounds of the real section. This is a pretty-rare event, so just
11668 flag an error (easier) instead of a warning and trying to cope. */
11669 if (sectp == NULL
11670 || offset + size > bfd_section_size (sectp))
11671 {
11672 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11673 " in section %s [in module %s]"),
11674 sectp ? bfd_section_name (sectp) : "<unknown>",
11675 objfile_name (per_objfile->objfile));
11676 }
11677
11678 result.virtual_offset = offset;
11679 result.size = size;
11680 return result;
11681 }
11682
11683 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11684 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11685 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11686 This is for DWP version 2 files. */
11687
11688 static struct dwo_unit *
11689 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11690 struct dwp_file *dwp_file,
11691 uint32_t unit_index,
11692 const char *comp_dir,
11693 ULONGEST signature, int is_debug_types)
11694 {
11695 const struct dwp_hash_table *dwp_htab =
11696 is_debug_types ? dwp_file->tus : dwp_file->cus;
11697 bfd *dbfd = dwp_file->dbfd.get ();
11698 const char *kind = is_debug_types ? "TU" : "CU";
11699 struct dwo_file *dwo_file;
11700 struct dwo_unit *dwo_unit;
11701 struct virtual_v2_or_v5_dwo_sections sections;
11702 void **dwo_file_slot;
11703 int i;
11704
11705 gdb_assert (dwp_file->version == 2);
11706
11707 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11708 kind, pulongest (unit_index), hex_string (signature),
11709 dwp_file->name);
11710
11711 /* Fetch the section offsets of this DWO unit. */
11712
11713 memset (&sections, 0, sizeof (sections));
11714
11715 for (i = 0; i < dwp_htab->nr_columns; ++i)
11716 {
11717 uint32_t offset = read_4_bytes (dbfd,
11718 dwp_htab->section_pool.v2.offsets
11719 + (((unit_index - 1) * dwp_htab->nr_columns
11720 + i)
11721 * sizeof (uint32_t)));
11722 uint32_t size = read_4_bytes (dbfd,
11723 dwp_htab->section_pool.v2.sizes
11724 + (((unit_index - 1) * dwp_htab->nr_columns
11725 + i)
11726 * sizeof (uint32_t)));
11727
11728 switch (dwp_htab->section_pool.v2.section_ids[i])
11729 {
11730 case DW_SECT_INFO:
11731 case DW_SECT_TYPES:
11732 sections.info_or_types_offset = offset;
11733 sections.info_or_types_size = size;
11734 break;
11735 case DW_SECT_ABBREV:
11736 sections.abbrev_offset = offset;
11737 sections.abbrev_size = size;
11738 break;
11739 case DW_SECT_LINE:
11740 sections.line_offset = offset;
11741 sections.line_size = size;
11742 break;
11743 case DW_SECT_LOC:
11744 sections.loc_offset = offset;
11745 sections.loc_size = size;
11746 break;
11747 case DW_SECT_STR_OFFSETS:
11748 sections.str_offsets_offset = offset;
11749 sections.str_offsets_size = size;
11750 break;
11751 case DW_SECT_MACINFO:
11752 sections.macinfo_offset = offset;
11753 sections.macinfo_size = size;
11754 break;
11755 case DW_SECT_MACRO:
11756 sections.macro_offset = offset;
11757 sections.macro_size = size;
11758 break;
11759 }
11760 }
11761
11762 /* It's easier for the rest of the code if we fake a struct dwo_file and
11763 have dwo_unit "live" in that. At least for now.
11764
11765 The DWP file can be made up of a random collection of CUs and TUs.
11766 However, for each CU + set of TUs that came from the same original DWO
11767 file, we can combine them back into a virtual DWO file to save space
11768 (fewer struct dwo_file objects to allocate). Remember that for really
11769 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11770
11771 std::string virtual_dwo_name =
11772 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11773 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11774 (long) (sections.line_size ? sections.line_offset : 0),
11775 (long) (sections.loc_size ? sections.loc_offset : 0),
11776 (long) (sections.str_offsets_size
11777 ? sections.str_offsets_offset : 0));
11778 /* Can we use an existing virtual DWO file? */
11779 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11780 comp_dir);
11781 /* Create one if necessary. */
11782 if (*dwo_file_slot == NULL)
11783 {
11784 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11785 virtual_dwo_name.c_str ());
11786
11787 dwo_file = new struct dwo_file;
11788 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11789 dwo_file->comp_dir = comp_dir;
11790 dwo_file->sections.abbrev =
11791 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11792 sections.abbrev_offset,
11793 sections.abbrev_size);
11794 dwo_file->sections.line =
11795 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11796 sections.line_offset,
11797 sections.line_size);
11798 dwo_file->sections.loc =
11799 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11800 sections.loc_offset, sections.loc_size);
11801 dwo_file->sections.macinfo =
11802 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11803 sections.macinfo_offset,
11804 sections.macinfo_size);
11805 dwo_file->sections.macro =
11806 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11807 sections.macro_offset,
11808 sections.macro_size);
11809 dwo_file->sections.str_offsets =
11810 create_dwp_v2_or_v5_section (per_objfile,
11811 &dwp_file->sections.str_offsets,
11812 sections.str_offsets_offset,
11813 sections.str_offsets_size);
11814 /* The "str" section is global to the entire DWP file. */
11815 dwo_file->sections.str = dwp_file->sections.str;
11816 /* The info or types section is assigned below to dwo_unit,
11817 there's no need to record it in dwo_file.
11818 Also, we can't simply record type sections in dwo_file because
11819 we record a pointer into the vector in dwo_unit. As we collect more
11820 types we'll grow the vector and eventually have to reallocate space
11821 for it, invalidating all copies of pointers into the previous
11822 contents. */
11823 *dwo_file_slot = dwo_file;
11824 }
11825 else
11826 {
11827 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11828 virtual_dwo_name.c_str ());
11829
11830 dwo_file = (struct dwo_file *) *dwo_file_slot;
11831 }
11832
11833 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11834 dwo_unit->dwo_file = dwo_file;
11835 dwo_unit->signature = signature;
11836 dwo_unit->section =
11837 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11838 *dwo_unit->section = create_dwp_v2_or_v5_section
11839 (per_objfile,
11840 is_debug_types
11841 ? &dwp_file->sections.types
11842 : &dwp_file->sections.info,
11843 sections.info_or_types_offset,
11844 sections.info_or_types_size);
11845 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11846
11847 return dwo_unit;
11848 }
11849
11850 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11851 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11852 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11853 This is for DWP version 5 files. */
11854
11855 static struct dwo_unit *
11856 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11857 struct dwp_file *dwp_file,
11858 uint32_t unit_index,
11859 const char *comp_dir,
11860 ULONGEST signature, int is_debug_types)
11861 {
11862 const struct dwp_hash_table *dwp_htab
11863 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11864 bfd *dbfd = dwp_file->dbfd.get ();
11865 const char *kind = is_debug_types ? "TU" : "CU";
11866 struct dwo_file *dwo_file;
11867 struct dwo_unit *dwo_unit;
11868 struct virtual_v2_or_v5_dwo_sections sections {};
11869 void **dwo_file_slot;
11870
11871 gdb_assert (dwp_file->version == 5);
11872
11873 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11874 kind, pulongest (unit_index), hex_string (signature),
11875 dwp_file->name);
11876
11877 /* Fetch the section offsets of this DWO unit. */
11878
11879 /* memset (&sections, 0, sizeof (sections)); */
11880
11881 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11882 {
11883 uint32_t offset = read_4_bytes (dbfd,
11884 dwp_htab->section_pool.v5.offsets
11885 + (((unit_index - 1)
11886 * dwp_htab->nr_columns
11887 + i)
11888 * sizeof (uint32_t)));
11889 uint32_t size = read_4_bytes (dbfd,
11890 dwp_htab->section_pool.v5.sizes
11891 + (((unit_index - 1) * dwp_htab->nr_columns
11892 + i)
11893 * sizeof (uint32_t)));
11894
11895 switch (dwp_htab->section_pool.v5.section_ids[i])
11896 {
11897 case DW_SECT_ABBREV_V5:
11898 sections.abbrev_offset = offset;
11899 sections.abbrev_size = size;
11900 break;
11901 case DW_SECT_INFO_V5:
11902 sections.info_or_types_offset = offset;
11903 sections.info_or_types_size = size;
11904 break;
11905 case DW_SECT_LINE_V5:
11906 sections.line_offset = offset;
11907 sections.line_size = size;
11908 break;
11909 case DW_SECT_LOCLISTS_V5:
11910 sections.loclists_offset = offset;
11911 sections.loclists_size = size;
11912 break;
11913 case DW_SECT_MACRO_V5:
11914 sections.macro_offset = offset;
11915 sections.macro_size = size;
11916 break;
11917 case DW_SECT_RNGLISTS_V5:
11918 sections.rnglists_offset = offset;
11919 sections.rnglists_size = size;
11920 break;
11921 case DW_SECT_STR_OFFSETS_V5:
11922 sections.str_offsets_offset = offset;
11923 sections.str_offsets_size = size;
11924 break;
11925 case DW_SECT_RESERVED_V5:
11926 default:
11927 break;
11928 }
11929 }
11930
11931 /* It's easier for the rest of the code if we fake a struct dwo_file and
11932 have dwo_unit "live" in that. At least for now.
11933
11934 The DWP file can be made up of a random collection of CUs and TUs.
11935 However, for each CU + set of TUs that came from the same original DWO
11936 file, we can combine them back into a virtual DWO file to save space
11937 (fewer struct dwo_file objects to allocate). Remember that for really
11938 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11939
11940 std::string virtual_dwo_name =
11941 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11942 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11943 (long) (sections.line_size ? sections.line_offset : 0),
11944 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11945 (long) (sections.str_offsets_size
11946 ? sections.str_offsets_offset : 0),
11947 (long) (sections.macro_size ? sections.macro_offset : 0),
11948 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11949 /* Can we use an existing virtual DWO file? */
11950 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11951 virtual_dwo_name.c_str (),
11952 comp_dir);
11953 /* Create one if necessary. */
11954 if (*dwo_file_slot == NULL)
11955 {
11956 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11957 virtual_dwo_name.c_str ());
11958
11959 dwo_file = new struct dwo_file;
11960 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11961 dwo_file->comp_dir = comp_dir;
11962 dwo_file->sections.abbrev =
11963 create_dwp_v2_or_v5_section (per_objfile,
11964 &dwp_file->sections.abbrev,
11965 sections.abbrev_offset,
11966 sections.abbrev_size);
11967 dwo_file->sections.line =
11968 create_dwp_v2_or_v5_section (per_objfile,
11969 &dwp_file->sections.line,
11970 sections.line_offset, sections.line_size);
11971 dwo_file->sections.macro =
11972 create_dwp_v2_or_v5_section (per_objfile,
11973 &dwp_file->sections.macro,
11974 sections.macro_offset,
11975 sections.macro_size);
11976 dwo_file->sections.loclists =
11977 create_dwp_v2_or_v5_section (per_objfile,
11978 &dwp_file->sections.loclists,
11979 sections.loclists_offset,
11980 sections.loclists_size);
11981 dwo_file->sections.rnglists =
11982 create_dwp_v2_or_v5_section (per_objfile,
11983 &dwp_file->sections.rnglists,
11984 sections.rnglists_offset,
11985 sections.rnglists_size);
11986 dwo_file->sections.str_offsets =
11987 create_dwp_v2_or_v5_section (per_objfile,
11988 &dwp_file->sections.str_offsets,
11989 sections.str_offsets_offset,
11990 sections.str_offsets_size);
11991 /* The "str" section is global to the entire DWP file. */
11992 dwo_file->sections.str = dwp_file->sections.str;
11993 /* The info or types section is assigned below to dwo_unit,
11994 there's no need to record it in dwo_file.
11995 Also, we can't simply record type sections in dwo_file because
11996 we record a pointer into the vector in dwo_unit. As we collect more
11997 types we'll grow the vector and eventually have to reallocate space
11998 for it, invalidating all copies of pointers into the previous
11999 contents. */
12000 *dwo_file_slot = dwo_file;
12001 }
12002 else
12003 {
12004 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12005 virtual_dwo_name.c_str ());
12006
12007 dwo_file = (struct dwo_file *) *dwo_file_slot;
12008 }
12009
12010 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12011 dwo_unit->dwo_file = dwo_file;
12012 dwo_unit->signature = signature;
12013 dwo_unit->section
12014 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12015 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12016 &dwp_file->sections.info,
12017 sections.info_or_types_offset,
12018 sections.info_or_types_size);
12019 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12020
12021 return dwo_unit;
12022 }
12023
12024 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12025 Returns NULL if the signature isn't found. */
12026
12027 static struct dwo_unit *
12028 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12029 struct dwp_file *dwp_file, const char *comp_dir,
12030 ULONGEST signature, int is_debug_types)
12031 {
12032 const struct dwp_hash_table *dwp_htab =
12033 is_debug_types ? dwp_file->tus : dwp_file->cus;
12034 bfd *dbfd = dwp_file->dbfd.get ();
12035 uint32_t mask = dwp_htab->nr_slots - 1;
12036 uint32_t hash = signature & mask;
12037 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12038 unsigned int i;
12039 void **slot;
12040 struct dwo_unit find_dwo_cu;
12041
12042 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12043 find_dwo_cu.signature = signature;
12044 slot = htab_find_slot (is_debug_types
12045 ? dwp_file->loaded_tus.get ()
12046 : dwp_file->loaded_cus.get (),
12047 &find_dwo_cu, INSERT);
12048
12049 if (*slot != NULL)
12050 return (struct dwo_unit *) *slot;
12051
12052 /* Use a for loop so that we don't loop forever on bad debug info. */
12053 for (i = 0; i < dwp_htab->nr_slots; ++i)
12054 {
12055 ULONGEST signature_in_table;
12056
12057 signature_in_table =
12058 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12059 if (signature_in_table == signature)
12060 {
12061 uint32_t unit_index =
12062 read_4_bytes (dbfd,
12063 dwp_htab->unit_table + hash * sizeof (uint32_t));
12064
12065 if (dwp_file->version == 1)
12066 {
12067 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12068 unit_index, comp_dir,
12069 signature, is_debug_types);
12070 }
12071 else if (dwp_file->version == 2)
12072 {
12073 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12074 unit_index, comp_dir,
12075 signature, is_debug_types);
12076 }
12077 else /* version == 5 */
12078 {
12079 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12080 unit_index, comp_dir,
12081 signature, is_debug_types);
12082 }
12083 return (struct dwo_unit *) *slot;
12084 }
12085 if (signature_in_table == 0)
12086 return NULL;
12087 hash = (hash + hash2) & mask;
12088 }
12089
12090 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12091 " [in module %s]"),
12092 dwp_file->name);
12093 }
12094
12095 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12096 Open the file specified by FILE_NAME and hand it off to BFD for
12097 preliminary analysis. Return a newly initialized bfd *, which
12098 includes a canonicalized copy of FILE_NAME.
12099 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12100 SEARCH_CWD is true if the current directory is to be searched.
12101 It will be searched before debug-file-directory.
12102 If successful, the file is added to the bfd include table of the
12103 objfile's bfd (see gdb_bfd_record_inclusion).
12104 If unable to find/open the file, return NULL.
12105 NOTE: This function is derived from symfile_bfd_open. */
12106
12107 static gdb_bfd_ref_ptr
12108 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12109 const char *file_name, int is_dwp, int search_cwd)
12110 {
12111 int desc;
12112 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12113 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12114 to debug_file_directory. */
12115 const char *search_path;
12116 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12117
12118 gdb::unique_xmalloc_ptr<char> search_path_holder;
12119 if (search_cwd)
12120 {
12121 if (!debug_file_directory.empty ())
12122 {
12123 search_path_holder.reset (concat (".", dirname_separator_string,
12124 debug_file_directory.c_str (),
12125 (char *) NULL));
12126 search_path = search_path_holder.get ();
12127 }
12128 else
12129 search_path = ".";
12130 }
12131 else
12132 search_path = debug_file_directory.c_str ();
12133
12134 /* Add the path for the executable binary to the list of search paths. */
12135 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12136 search_path_holder.reset (concat (objfile_dir.c_str (),
12137 dirname_separator_string,
12138 search_path, nullptr));
12139 search_path = search_path_holder.get ();
12140
12141 openp_flags flags = OPF_RETURN_REALPATH;
12142 if (is_dwp)
12143 flags |= OPF_SEARCH_IN_PATH;
12144
12145 gdb::unique_xmalloc_ptr<char> absolute_name;
12146 desc = openp (search_path, flags, file_name,
12147 O_RDONLY | O_BINARY, &absolute_name);
12148 if (desc < 0)
12149 return NULL;
12150
12151 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12152 gnutarget, desc));
12153 if (sym_bfd == NULL)
12154 return NULL;
12155 bfd_set_cacheable (sym_bfd.get (), 1);
12156
12157 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12158 return NULL;
12159
12160 /* Success. Record the bfd as having been included by the objfile's bfd.
12161 This is important because things like demangled_names_hash lives in the
12162 objfile's per_bfd space and may have references to things like symbol
12163 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12164 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12165
12166 return sym_bfd;
12167 }
12168
12169 /* Try to open DWO file FILE_NAME.
12170 COMP_DIR is the DW_AT_comp_dir attribute.
12171 The result is the bfd handle of the file.
12172 If there is a problem finding or opening the file, return NULL.
12173 Upon success, the canonicalized path of the file is stored in the bfd,
12174 same as symfile_bfd_open. */
12175
12176 static gdb_bfd_ref_ptr
12177 open_dwo_file (dwarf2_per_objfile *per_objfile,
12178 const char *file_name, const char *comp_dir)
12179 {
12180 if (IS_ABSOLUTE_PATH (file_name))
12181 return try_open_dwop_file (per_objfile, file_name,
12182 0 /*is_dwp*/, 0 /*search_cwd*/);
12183
12184 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12185
12186 if (comp_dir != NULL)
12187 {
12188 gdb::unique_xmalloc_ptr<char> path_to_try
12189 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12190
12191 /* NOTE: If comp_dir is a relative path, this will also try the
12192 search path, which seems useful. */
12193 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12194 0 /*is_dwp*/,
12195 1 /*search_cwd*/));
12196 if (abfd != NULL)
12197 return abfd;
12198 }
12199
12200 /* That didn't work, try debug-file-directory, which, despite its name,
12201 is a list of paths. */
12202
12203 if (debug_file_directory.empty ())
12204 return NULL;
12205
12206 return try_open_dwop_file (per_objfile, file_name,
12207 0 /*is_dwp*/, 1 /*search_cwd*/);
12208 }
12209
12210 /* This function is mapped across the sections and remembers the offset and
12211 size of each of the DWO debugging sections we are interested in. */
12212
12213 static void
12214 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12215 dwo_sections *dwo_sections)
12216 {
12217 const struct dwop_section_names *names = &dwop_section_names;
12218
12219 if (names->abbrev_dwo.matches (sectp->name))
12220 {
12221 dwo_sections->abbrev.s.section = sectp;
12222 dwo_sections->abbrev.size = bfd_section_size (sectp);
12223 }
12224 else if (names->info_dwo.matches (sectp->name))
12225 {
12226 dwo_sections->info.s.section = sectp;
12227 dwo_sections->info.size = bfd_section_size (sectp);
12228 }
12229 else if (names->line_dwo.matches (sectp->name))
12230 {
12231 dwo_sections->line.s.section = sectp;
12232 dwo_sections->line.size = bfd_section_size (sectp);
12233 }
12234 else if (names->loc_dwo.matches (sectp->name))
12235 {
12236 dwo_sections->loc.s.section = sectp;
12237 dwo_sections->loc.size = bfd_section_size (sectp);
12238 }
12239 else if (names->loclists_dwo.matches (sectp->name))
12240 {
12241 dwo_sections->loclists.s.section = sectp;
12242 dwo_sections->loclists.size = bfd_section_size (sectp);
12243 }
12244 else if (names->macinfo_dwo.matches (sectp->name))
12245 {
12246 dwo_sections->macinfo.s.section = sectp;
12247 dwo_sections->macinfo.size = bfd_section_size (sectp);
12248 }
12249 else if (names->macro_dwo.matches (sectp->name))
12250 {
12251 dwo_sections->macro.s.section = sectp;
12252 dwo_sections->macro.size = bfd_section_size (sectp);
12253 }
12254 else if (names->rnglists_dwo.matches (sectp->name))
12255 {
12256 dwo_sections->rnglists.s.section = sectp;
12257 dwo_sections->rnglists.size = bfd_section_size (sectp);
12258 }
12259 else if (names->str_dwo.matches (sectp->name))
12260 {
12261 dwo_sections->str.s.section = sectp;
12262 dwo_sections->str.size = bfd_section_size (sectp);
12263 }
12264 else if (names->str_offsets_dwo.matches (sectp->name))
12265 {
12266 dwo_sections->str_offsets.s.section = sectp;
12267 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12268 }
12269 else if (names->types_dwo.matches (sectp->name))
12270 {
12271 struct dwarf2_section_info type_section;
12272
12273 memset (&type_section, 0, sizeof (type_section));
12274 type_section.s.section = sectp;
12275 type_section.size = bfd_section_size (sectp);
12276 dwo_sections->types.push_back (type_section);
12277 }
12278 }
12279
12280 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12281 by PER_CU. This is for the non-DWP case.
12282 The result is NULL if DWO_NAME can't be found. */
12283
12284 static struct dwo_file *
12285 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12286 const char *comp_dir)
12287 {
12288 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12289
12290 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12291 if (dbfd == NULL)
12292 {
12293 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12294
12295 return NULL;
12296 }
12297
12298 dwo_file_up dwo_file (new struct dwo_file);
12299 dwo_file->dwo_name = dwo_name;
12300 dwo_file->comp_dir = comp_dir;
12301 dwo_file->dbfd = std::move (dbfd);
12302
12303 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12304 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12305 &dwo_file->sections);
12306
12307 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12308 dwo_file->cus);
12309
12310 if (cu->per_cu->dwarf_version < 5)
12311 {
12312 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12313 dwo_file->sections.types, dwo_file->tus);
12314 }
12315 else
12316 {
12317 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12318 &dwo_file->sections.info, dwo_file->tus,
12319 rcuh_kind::COMPILE);
12320 }
12321
12322 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12323
12324 return dwo_file.release ();
12325 }
12326
12327 /* This function is mapped across the sections and remembers the offset and
12328 size of each of the DWP debugging sections common to version 1 and 2 that
12329 we are interested in. */
12330
12331 static void
12332 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12333 dwp_file *dwp_file)
12334 {
12335 const struct dwop_section_names *names = &dwop_section_names;
12336 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12337
12338 /* Record the ELF section number for later lookup: this is what the
12339 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12340 gdb_assert (elf_section_nr < dwp_file->num_sections);
12341 dwp_file->elf_sections[elf_section_nr] = sectp;
12342
12343 /* Look for specific sections that we need. */
12344 if (names->str_dwo.matches (sectp->name))
12345 {
12346 dwp_file->sections.str.s.section = sectp;
12347 dwp_file->sections.str.size = bfd_section_size (sectp);
12348 }
12349 else if (names->cu_index.matches (sectp->name))
12350 {
12351 dwp_file->sections.cu_index.s.section = sectp;
12352 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12353 }
12354 else if (names->tu_index.matches (sectp->name))
12355 {
12356 dwp_file->sections.tu_index.s.section = sectp;
12357 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12358 }
12359 }
12360
12361 /* This function is mapped across the sections and remembers the offset and
12362 size of each of the DWP version 2 debugging sections that we are interested
12363 in. This is split into a separate function because we don't know if we
12364 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12365
12366 static void
12367 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12368 {
12369 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12370 const struct dwop_section_names *names = &dwop_section_names;
12371 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12372
12373 /* Record the ELF section number for later lookup: this is what the
12374 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12375 gdb_assert (elf_section_nr < dwp_file->num_sections);
12376 dwp_file->elf_sections[elf_section_nr] = sectp;
12377
12378 /* Look for specific sections that we need. */
12379 if (names->abbrev_dwo.matches (sectp->name))
12380 {
12381 dwp_file->sections.abbrev.s.section = sectp;
12382 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12383 }
12384 else if (names->info_dwo.matches (sectp->name))
12385 {
12386 dwp_file->sections.info.s.section = sectp;
12387 dwp_file->sections.info.size = bfd_section_size (sectp);
12388 }
12389 else if (names->line_dwo.matches (sectp->name))
12390 {
12391 dwp_file->sections.line.s.section = sectp;
12392 dwp_file->sections.line.size = bfd_section_size (sectp);
12393 }
12394 else if (names->loc_dwo.matches (sectp->name))
12395 {
12396 dwp_file->sections.loc.s.section = sectp;
12397 dwp_file->sections.loc.size = bfd_section_size (sectp);
12398 }
12399 else if (names->macinfo_dwo.matches (sectp->name))
12400 {
12401 dwp_file->sections.macinfo.s.section = sectp;
12402 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12403 }
12404 else if (names->macro_dwo.matches (sectp->name))
12405 {
12406 dwp_file->sections.macro.s.section = sectp;
12407 dwp_file->sections.macro.size = bfd_section_size (sectp);
12408 }
12409 else if (names->str_offsets_dwo.matches (sectp->name))
12410 {
12411 dwp_file->sections.str_offsets.s.section = sectp;
12412 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12413 }
12414 else if (names->types_dwo.matches (sectp->name))
12415 {
12416 dwp_file->sections.types.s.section = sectp;
12417 dwp_file->sections.types.size = bfd_section_size (sectp);
12418 }
12419 }
12420
12421 /* This function is mapped across the sections and remembers the offset and
12422 size of each of the DWP version 5 debugging sections that we are interested
12423 in. This is split into a separate function because we don't know if we
12424 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12425
12426 static void
12427 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12428 {
12429 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12430 const struct dwop_section_names *names = &dwop_section_names;
12431 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12432
12433 /* Record the ELF section number for later lookup: this is what the
12434 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12435 gdb_assert (elf_section_nr < dwp_file->num_sections);
12436 dwp_file->elf_sections[elf_section_nr] = sectp;
12437
12438 /* Look for specific sections that we need. */
12439 if (names->abbrev_dwo.matches (sectp->name))
12440 {
12441 dwp_file->sections.abbrev.s.section = sectp;
12442 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12443 }
12444 else if (names->info_dwo.matches (sectp->name))
12445 {
12446 dwp_file->sections.info.s.section = sectp;
12447 dwp_file->sections.info.size = bfd_section_size (sectp);
12448 }
12449 else if (names->line_dwo.matches (sectp->name))
12450 {
12451 dwp_file->sections.line.s.section = sectp;
12452 dwp_file->sections.line.size = bfd_section_size (sectp);
12453 }
12454 else if (names->loclists_dwo.matches (sectp->name))
12455 {
12456 dwp_file->sections.loclists.s.section = sectp;
12457 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12458 }
12459 else if (names->macro_dwo.matches (sectp->name))
12460 {
12461 dwp_file->sections.macro.s.section = sectp;
12462 dwp_file->sections.macro.size = bfd_section_size (sectp);
12463 }
12464 else if (names->rnglists_dwo.matches (sectp->name))
12465 {
12466 dwp_file->sections.rnglists.s.section = sectp;
12467 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12468 }
12469 else if (names->str_offsets_dwo.matches (sectp->name))
12470 {
12471 dwp_file->sections.str_offsets.s.section = sectp;
12472 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12473 }
12474 }
12475
12476 /* Hash function for dwp_file loaded CUs/TUs. */
12477
12478 static hashval_t
12479 hash_dwp_loaded_cutus (const void *item)
12480 {
12481 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12482
12483 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12484 return dwo_unit->signature;
12485 }
12486
12487 /* Equality function for dwp_file loaded CUs/TUs. */
12488
12489 static int
12490 eq_dwp_loaded_cutus (const void *a, const void *b)
12491 {
12492 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12493 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12494
12495 return dua->signature == dub->signature;
12496 }
12497
12498 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12499
12500 static htab_up
12501 allocate_dwp_loaded_cutus_table ()
12502 {
12503 return htab_up (htab_create_alloc (3,
12504 hash_dwp_loaded_cutus,
12505 eq_dwp_loaded_cutus,
12506 NULL, xcalloc, xfree));
12507 }
12508
12509 /* Try to open DWP file FILE_NAME.
12510 The result is the bfd handle of the file.
12511 If there is a problem finding or opening the file, return NULL.
12512 Upon success, the canonicalized path of the file is stored in the bfd,
12513 same as symfile_bfd_open. */
12514
12515 static gdb_bfd_ref_ptr
12516 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12517 {
12518 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12519 1 /*is_dwp*/,
12520 1 /*search_cwd*/));
12521 if (abfd != NULL)
12522 return abfd;
12523
12524 /* Work around upstream bug 15652.
12525 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12526 [Whether that's a "bug" is debatable, but it is getting in our way.]
12527 We have no real idea where the dwp file is, because gdb's realpath-ing
12528 of the executable's path may have discarded the needed info.
12529 [IWBN if the dwp file name was recorded in the executable, akin to
12530 .gnu_debuglink, but that doesn't exist yet.]
12531 Strip the directory from FILE_NAME and search again. */
12532 if (!debug_file_directory.empty ())
12533 {
12534 /* Don't implicitly search the current directory here.
12535 If the user wants to search "." to handle this case,
12536 it must be added to debug-file-directory. */
12537 return try_open_dwop_file (per_objfile, lbasename (file_name),
12538 1 /*is_dwp*/,
12539 0 /*search_cwd*/);
12540 }
12541
12542 return NULL;
12543 }
12544
12545 /* Initialize the use of the DWP file for the current objfile.
12546 By convention the name of the DWP file is ${objfile}.dwp.
12547 The result is NULL if it can't be found. */
12548
12549 static std::unique_ptr<struct dwp_file>
12550 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12551 {
12552 struct objfile *objfile = per_objfile->objfile;
12553
12554 /* Try to find first .dwp for the binary file before any symbolic links
12555 resolving. */
12556
12557 /* If the objfile is a debug file, find the name of the real binary
12558 file and get the name of dwp file from there. */
12559 std::string dwp_name;
12560 if (objfile->separate_debug_objfile_backlink != NULL)
12561 {
12562 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12563 const char *backlink_basename = lbasename (backlink->original_name);
12564
12565 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12566 }
12567 else
12568 dwp_name = objfile->original_name;
12569
12570 dwp_name += ".dwp";
12571
12572 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12573 if (dbfd == NULL
12574 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12575 {
12576 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12577 dwp_name = objfile_name (objfile);
12578 dwp_name += ".dwp";
12579 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12580 }
12581
12582 if (dbfd == NULL)
12583 {
12584 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12585
12586 return std::unique_ptr<dwp_file> ();
12587 }
12588
12589 const char *name = bfd_get_filename (dbfd.get ());
12590 std::unique_ptr<struct dwp_file> dwp_file
12591 (new struct dwp_file (name, std::move (dbfd)));
12592
12593 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12594 dwp_file->elf_sections =
12595 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12596 dwp_file->num_sections, asection *);
12597
12598 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12599 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12600 dwp_file.get ());
12601
12602 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12603
12604 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12605
12606 /* The DWP file version is stored in the hash table. Oh well. */
12607 if (dwp_file->cus && dwp_file->tus
12608 && dwp_file->cus->version != dwp_file->tus->version)
12609 {
12610 /* Technically speaking, we should try to limp along, but this is
12611 pretty bizarre. We use pulongest here because that's the established
12612 portability solution (e.g, we cannot use %u for uint32_t). */
12613 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12614 " TU version %s [in DWP file %s]"),
12615 pulongest (dwp_file->cus->version),
12616 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12617 }
12618
12619 if (dwp_file->cus)
12620 dwp_file->version = dwp_file->cus->version;
12621 else if (dwp_file->tus)
12622 dwp_file->version = dwp_file->tus->version;
12623 else
12624 dwp_file->version = 2;
12625
12626 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12627 {
12628 if (dwp_file->version == 2)
12629 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12630 dwp_file.get ());
12631 else
12632 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12633 dwp_file.get ());
12634 }
12635
12636 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12637 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12638
12639 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12640 dwarf_read_debug_printf (" %s CUs, %s TUs",
12641 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12642 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12643
12644 return dwp_file;
12645 }
12646
12647 /* Wrapper around open_and_init_dwp_file, only open it once. */
12648
12649 static struct dwp_file *
12650 get_dwp_file (dwarf2_per_objfile *per_objfile)
12651 {
12652 if (!per_objfile->per_bfd->dwp_checked)
12653 {
12654 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12655 per_objfile->per_bfd->dwp_checked = 1;
12656 }
12657 return per_objfile->per_bfd->dwp_file.get ();
12658 }
12659
12660 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12661 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12662 or in the DWP file for the objfile, referenced by THIS_UNIT.
12663 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12664 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12665
12666 This is called, for example, when wanting to read a variable with a
12667 complex location. Therefore we don't want to do file i/o for every call.
12668 Therefore we don't want to look for a DWO file on every call.
12669 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12670 then we check if we've already seen DWO_NAME, and only THEN do we check
12671 for a DWO file.
12672
12673 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12674 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12675
12676 static struct dwo_unit *
12677 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12678 ULONGEST signature, int is_debug_types)
12679 {
12680 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12681 struct objfile *objfile = per_objfile->objfile;
12682 const char *kind = is_debug_types ? "TU" : "CU";
12683 void **dwo_file_slot;
12684 struct dwo_file *dwo_file;
12685 struct dwp_file *dwp_file;
12686
12687 /* First see if there's a DWP file.
12688 If we have a DWP file but didn't find the DWO inside it, don't
12689 look for the original DWO file. It makes gdb behave differently
12690 depending on whether one is debugging in the build tree. */
12691
12692 dwp_file = get_dwp_file (per_objfile);
12693 if (dwp_file != NULL)
12694 {
12695 const struct dwp_hash_table *dwp_htab =
12696 is_debug_types ? dwp_file->tus : dwp_file->cus;
12697
12698 if (dwp_htab != NULL)
12699 {
12700 struct dwo_unit *dwo_cutu =
12701 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12702 is_debug_types);
12703
12704 if (dwo_cutu != NULL)
12705 {
12706 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12707 kind, hex_string (signature),
12708 host_address_to_string (dwo_cutu));
12709
12710 return dwo_cutu;
12711 }
12712 }
12713 }
12714 else
12715 {
12716 /* No DWP file, look for the DWO file. */
12717
12718 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12719 if (*dwo_file_slot == NULL)
12720 {
12721 /* Read in the file and build a table of the CUs/TUs it contains. */
12722 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12723 }
12724 /* NOTE: This will be NULL if unable to open the file. */
12725 dwo_file = (struct dwo_file *) *dwo_file_slot;
12726
12727 if (dwo_file != NULL)
12728 {
12729 struct dwo_unit *dwo_cutu = NULL;
12730
12731 if (is_debug_types && dwo_file->tus)
12732 {
12733 struct dwo_unit find_dwo_cutu;
12734
12735 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12736 find_dwo_cutu.signature = signature;
12737 dwo_cutu
12738 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12739 &find_dwo_cutu);
12740 }
12741 else if (!is_debug_types && dwo_file->cus)
12742 {
12743 struct dwo_unit find_dwo_cutu;
12744
12745 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12746 find_dwo_cutu.signature = signature;
12747 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12748 &find_dwo_cutu);
12749 }
12750
12751 if (dwo_cutu != NULL)
12752 {
12753 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12754 kind, dwo_name, hex_string (signature),
12755 host_address_to_string (dwo_cutu));
12756
12757 return dwo_cutu;
12758 }
12759 }
12760 }
12761
12762 /* We didn't find it. This could mean a dwo_id mismatch, or
12763 someone deleted the DWO/DWP file, or the search path isn't set up
12764 correctly to find the file. */
12765
12766 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12767 kind, dwo_name, hex_string (signature));
12768
12769 /* This is a warning and not a complaint because it can be caused by
12770 pilot error (e.g., user accidentally deleting the DWO). */
12771 {
12772 /* Print the name of the DWP file if we looked there, helps the user
12773 better diagnose the problem. */
12774 std::string dwp_text;
12775
12776 if (dwp_file != NULL)
12777 dwp_text = string_printf (" [in DWP file %s]",
12778 lbasename (dwp_file->name));
12779
12780 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12781 " [in module %s]"),
12782 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12783 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12784 }
12785 return NULL;
12786 }
12787
12788 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12789 See lookup_dwo_cutu_unit for details. */
12790
12791 static struct dwo_unit *
12792 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12793 ULONGEST signature)
12794 {
12795 gdb_assert (!cu->per_cu->is_debug_types);
12796
12797 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12798 }
12799
12800 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12801 See lookup_dwo_cutu_unit for details. */
12802
12803 static struct dwo_unit *
12804 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12805 {
12806 gdb_assert (cu->per_cu->is_debug_types);
12807
12808 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12809
12810 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12811 }
12812
12813 /* Traversal function for queue_and_load_all_dwo_tus. */
12814
12815 static int
12816 queue_and_load_dwo_tu (void **slot, void *info)
12817 {
12818 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12819 dwarf2_cu *cu = (dwarf2_cu *) info;
12820 ULONGEST signature = dwo_unit->signature;
12821 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12822
12823 if (sig_type != NULL)
12824 {
12825 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12826 a real dependency of PER_CU on SIG_TYPE. That is detected later
12827 while processing PER_CU. */
12828 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12829 cu->per_cu->lang))
12830 load_full_type_unit (sig_type, cu->per_objfile);
12831 cu->per_cu->imported_symtabs_push (sig_type);
12832 }
12833
12834 return 1;
12835 }
12836
12837 /* Queue all TUs contained in the DWO of CU to be read in.
12838 The DWO may have the only definition of the type, though it may not be
12839 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12840 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12841
12842 static void
12843 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12844 {
12845 struct dwo_unit *dwo_unit;
12846 struct dwo_file *dwo_file;
12847
12848 gdb_assert (cu != nullptr);
12849 gdb_assert (!cu->per_cu->is_debug_types);
12850 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12851
12852 dwo_unit = cu->dwo_unit;
12853 gdb_assert (dwo_unit != NULL);
12854
12855 dwo_file = dwo_unit->dwo_file;
12856 if (dwo_file->tus != NULL)
12857 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12858 }
12859
12860 /* Read in various DIEs. */
12861
12862 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12863 Inherit only the children of the DW_AT_abstract_origin DIE not being
12864 already referenced by DW_AT_abstract_origin from the children of the
12865 current DIE. */
12866
12867 static void
12868 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12869 {
12870 struct die_info *child_die;
12871 sect_offset *offsetp;
12872 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12873 struct die_info *origin_die;
12874 /* Iterator of the ORIGIN_DIE children. */
12875 struct die_info *origin_child_die;
12876 struct attribute *attr;
12877 struct dwarf2_cu *origin_cu;
12878 struct pending **origin_previous_list_in_scope;
12879
12880 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12881 if (!attr)
12882 return;
12883
12884 /* Note that following die references may follow to a die in a
12885 different cu. */
12886
12887 origin_cu = cu;
12888 origin_die = follow_die_ref (die, attr, &origin_cu);
12889
12890 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12891 symbols in. */
12892 origin_previous_list_in_scope = origin_cu->list_in_scope;
12893 origin_cu->list_in_scope = cu->list_in_scope;
12894
12895 if (die->tag != origin_die->tag
12896 && !(die->tag == DW_TAG_inlined_subroutine
12897 && origin_die->tag == DW_TAG_subprogram))
12898 complaint (_("DIE %s and its abstract origin %s have different tags"),
12899 sect_offset_str (die->sect_off),
12900 sect_offset_str (origin_die->sect_off));
12901
12902 /* Find if the concrete and abstract trees are structurally the
12903 same. This is a shallow traversal and it is not bullet-proof;
12904 the compiler can trick the debugger into believing that the trees
12905 are isomorphic, whereas they actually are not. However, the
12906 likelyhood of this happening is pretty low, and a full-fledged
12907 check would be an overkill. */
12908 bool are_isomorphic = true;
12909 die_info *concrete_child = die->child;
12910 die_info *abstract_child = origin_die->child;
12911 while (concrete_child != nullptr || abstract_child != nullptr)
12912 {
12913 if (concrete_child == nullptr
12914 || abstract_child == nullptr
12915 || concrete_child->tag != abstract_child->tag)
12916 {
12917 are_isomorphic = false;
12918 break;
12919 }
12920
12921 concrete_child = concrete_child->sibling;
12922 abstract_child = abstract_child->sibling;
12923 }
12924
12925 /* Walk the origin's children in parallel to the concrete children.
12926 This helps match an origin child in case the debug info misses
12927 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12928 origin tree may not have the same tree structure as the concrete
12929 DIE, though. */
12930 die_info *corresponding_abstract_child
12931 = are_isomorphic ? origin_die->child : nullptr;
12932
12933 std::vector<sect_offset> offsets;
12934
12935 for (child_die = die->child;
12936 child_die && child_die->tag;
12937 child_die = child_die->sibling)
12938 {
12939 struct die_info *child_origin_die;
12940 struct dwarf2_cu *child_origin_cu;
12941
12942 /* We are trying to process concrete instance entries:
12943 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12944 it's not relevant to our analysis here. i.e. detecting DIEs that are
12945 present in the abstract instance but not referenced in the concrete
12946 one. */
12947 if (child_die->tag == DW_TAG_call_site
12948 || child_die->tag == DW_TAG_GNU_call_site)
12949 {
12950 if (are_isomorphic)
12951 corresponding_abstract_child
12952 = corresponding_abstract_child->sibling;
12953 continue;
12954 }
12955
12956 /* For each CHILD_DIE, find the corresponding child of
12957 ORIGIN_DIE. If there is more than one layer of
12958 DW_AT_abstract_origin, follow them all; there shouldn't be,
12959 but GCC versions at least through 4.4 generate this (GCC PR
12960 40573). */
12961 child_origin_die = child_die;
12962 child_origin_cu = cu;
12963 while (1)
12964 {
12965 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12966 child_origin_cu);
12967 if (attr == NULL)
12968 break;
12969 child_origin_die = follow_die_ref (child_origin_die, attr,
12970 &child_origin_cu);
12971 }
12972
12973 /* If missing DW_AT_abstract_origin, try the corresponding child
12974 of the origin. Clang emits such lexical scopes. */
12975 if (child_origin_die == child_die
12976 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12977 && are_isomorphic
12978 && child_die->tag == DW_TAG_lexical_block)
12979 child_origin_die = corresponding_abstract_child;
12980
12981 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12982 counterpart may exist. */
12983 if (child_origin_die != child_die)
12984 {
12985 if (child_die->tag != child_origin_die->tag
12986 && !(child_die->tag == DW_TAG_inlined_subroutine
12987 && child_origin_die->tag == DW_TAG_subprogram))
12988 complaint (_("Child DIE %s and its abstract origin %s have "
12989 "different tags"),
12990 sect_offset_str (child_die->sect_off),
12991 sect_offset_str (child_origin_die->sect_off));
12992 if (child_origin_die->parent != origin_die)
12993 complaint (_("Child DIE %s and its abstract origin %s have "
12994 "different parents"),
12995 sect_offset_str (child_die->sect_off),
12996 sect_offset_str (child_origin_die->sect_off));
12997 else
12998 offsets.push_back (child_origin_die->sect_off);
12999 }
13000
13001 if (are_isomorphic)
13002 corresponding_abstract_child = corresponding_abstract_child->sibling;
13003 }
13004 std::sort (offsets.begin (), offsets.end ());
13005 sect_offset *offsets_end = offsets.data () + offsets.size ();
13006 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13007 if (offsetp[-1] == *offsetp)
13008 complaint (_("Multiple children of DIE %s refer "
13009 "to DIE %s as their abstract origin"),
13010 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13011
13012 offsetp = offsets.data ();
13013 origin_child_die = origin_die->child;
13014 while (origin_child_die && origin_child_die->tag)
13015 {
13016 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13017 while (offsetp < offsets_end
13018 && *offsetp < origin_child_die->sect_off)
13019 offsetp++;
13020 if (offsetp >= offsets_end
13021 || *offsetp > origin_child_die->sect_off)
13022 {
13023 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13024 Check whether we're already processing ORIGIN_CHILD_DIE.
13025 This can happen with mutually referenced abstract_origins.
13026 PR 16581. */
13027 if (!origin_child_die->in_process)
13028 process_die (origin_child_die, origin_cu);
13029 }
13030 origin_child_die = origin_child_die->sibling;
13031 }
13032 origin_cu->list_in_scope = origin_previous_list_in_scope;
13033
13034 if (cu != origin_cu)
13035 compute_delayed_physnames (origin_cu);
13036 }
13037
13038 static void
13039 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13040 {
13041 struct objfile *objfile = cu->per_objfile->objfile;
13042 struct gdbarch *gdbarch = objfile->arch ();
13043 struct context_stack *newobj;
13044 CORE_ADDR lowpc;
13045 CORE_ADDR highpc;
13046 struct die_info *child_die;
13047 struct attribute *attr, *call_line, *call_file;
13048 const char *name;
13049 CORE_ADDR baseaddr;
13050 struct block *block;
13051 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13052 std::vector<struct symbol *> template_args;
13053 struct template_symbol *templ_func = NULL;
13054
13055 if (inlined_func)
13056 {
13057 /* If we do not have call site information, we can't show the
13058 caller of this inlined function. That's too confusing, so
13059 only use the scope for local variables. */
13060 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13061 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13062 if (call_line == NULL || call_file == NULL)
13063 {
13064 read_lexical_block_scope (die, cu);
13065 return;
13066 }
13067 }
13068
13069 baseaddr = objfile->text_section_offset ();
13070
13071 name = dwarf2_name (die, cu);
13072
13073 /* Ignore functions with missing or empty names. These are actually
13074 illegal according to the DWARF standard. */
13075 if (name == NULL)
13076 {
13077 complaint (_("missing name for subprogram DIE at %s"),
13078 sect_offset_str (die->sect_off));
13079 return;
13080 }
13081
13082 /* Ignore functions with missing or invalid low and high pc attributes. */
13083 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13084 <= PC_BOUNDS_INVALID)
13085 {
13086 attr = dwarf2_attr (die, DW_AT_external, cu);
13087 if (attr == nullptr || !attr->as_boolean ())
13088 complaint (_("cannot get low and high bounds "
13089 "for subprogram DIE at %s"),
13090 sect_offset_str (die->sect_off));
13091 return;
13092 }
13093
13094 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13095 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13096
13097 /* If we have any template arguments, then we must allocate a
13098 different sort of symbol. */
13099 for (child_die = die->child; child_die; child_die = child_die->sibling)
13100 {
13101 if (child_die->tag == DW_TAG_template_type_param
13102 || child_die->tag == DW_TAG_template_value_param)
13103 {
13104 templ_func = new (&objfile->objfile_obstack) template_symbol;
13105 templ_func->subclass = SYMBOL_TEMPLATE;
13106 break;
13107 }
13108 }
13109
13110 gdb_assert (cu->get_builder () != nullptr);
13111 newobj = cu->get_builder ()->push_context (0, lowpc);
13112 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13113 (struct symbol *) templ_func);
13114
13115 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13116 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13117 cu->per_cu->lang);
13118
13119 /* If there is a location expression for DW_AT_frame_base, record
13120 it. */
13121 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13122 if (attr != nullptr)
13123 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13124
13125 /* If there is a location for the static link, record it. */
13126 newobj->static_link = NULL;
13127 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13128 if (attr != nullptr)
13129 {
13130 newobj->static_link
13131 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13132 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13133 cu->addr_type ());
13134 }
13135
13136 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13137
13138 if (die->child != NULL)
13139 {
13140 child_die = die->child;
13141 while (child_die && child_die->tag)
13142 {
13143 if (child_die->tag == DW_TAG_template_type_param
13144 || child_die->tag == DW_TAG_template_value_param)
13145 {
13146 struct symbol *arg = new_symbol (child_die, NULL, cu);
13147
13148 if (arg != NULL)
13149 template_args.push_back (arg);
13150 }
13151 else
13152 process_die (child_die, cu);
13153 child_die = child_die->sibling;
13154 }
13155 }
13156
13157 inherit_abstract_dies (die, cu);
13158
13159 /* If we have a DW_AT_specification, we might need to import using
13160 directives from the context of the specification DIE. See the
13161 comment in determine_prefix. */
13162 if (cu->per_cu->lang == language_cplus
13163 && dwarf2_attr (die, DW_AT_specification, cu))
13164 {
13165 struct dwarf2_cu *spec_cu = cu;
13166 struct die_info *spec_die = die_specification (die, &spec_cu);
13167
13168 while (spec_die)
13169 {
13170 child_die = spec_die->child;
13171 while (child_die && child_die->tag)
13172 {
13173 if (child_die->tag == DW_TAG_imported_module)
13174 process_die (child_die, spec_cu);
13175 child_die = child_die->sibling;
13176 }
13177
13178 /* In some cases, GCC generates specification DIEs that
13179 themselves contain DW_AT_specification attributes. */
13180 spec_die = die_specification (spec_die, &spec_cu);
13181 }
13182 }
13183
13184 struct context_stack cstk = cu->get_builder ()->pop_context ();
13185 /* Make a block for the local symbols within. */
13186 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13187 cstk.static_link, lowpc, highpc);
13188
13189 /* For C++, set the block's scope. */
13190 if ((cu->per_cu->lang == language_cplus
13191 || cu->per_cu->lang == language_fortran
13192 || cu->per_cu->lang == language_d
13193 || cu->per_cu->lang == language_rust)
13194 && cu->processing_has_namespace_info)
13195 block_set_scope (block, determine_prefix (die, cu),
13196 &objfile->objfile_obstack);
13197
13198 /* If we have address ranges, record them. */
13199 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13200
13201 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13202
13203 /* Attach template arguments to function. */
13204 if (!template_args.empty ())
13205 {
13206 gdb_assert (templ_func != NULL);
13207
13208 templ_func->n_template_arguments = template_args.size ();
13209 templ_func->template_arguments
13210 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13211 templ_func->n_template_arguments);
13212 memcpy (templ_func->template_arguments,
13213 template_args.data (),
13214 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13215
13216 /* Make sure that the symtab is set on the new symbols. Even
13217 though they don't appear in this symtab directly, other parts
13218 of gdb assume that symbols do, and this is reasonably
13219 true. */
13220 for (symbol *sym : template_args)
13221 symbol_set_symtab (sym, symbol_symtab (templ_func));
13222 }
13223
13224 /* In C++, we can have functions nested inside functions (e.g., when
13225 a function declares a class that has methods). This means that
13226 when we finish processing a function scope, we may need to go
13227 back to building a containing block's symbol lists. */
13228 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13229 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13230
13231 /* If we've finished processing a top-level function, subsequent
13232 symbols go in the file symbol list. */
13233 if (cu->get_builder ()->outermost_context_p ())
13234 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13235 }
13236
13237 /* Process all the DIES contained within a lexical block scope. Start
13238 a new scope, process the dies, and then close the scope. */
13239
13240 static void
13241 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13242 {
13243 struct objfile *objfile = cu->per_objfile->objfile;
13244 struct gdbarch *gdbarch = objfile->arch ();
13245 CORE_ADDR lowpc, highpc;
13246 struct die_info *child_die;
13247 CORE_ADDR baseaddr;
13248
13249 baseaddr = objfile->text_section_offset ();
13250
13251 /* Ignore blocks with missing or invalid low and high pc attributes. */
13252 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13253 as multiple lexical blocks? Handling children in a sane way would
13254 be nasty. Might be easier to properly extend generic blocks to
13255 describe ranges. */
13256 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13257 {
13258 case PC_BOUNDS_NOT_PRESENT:
13259 /* DW_TAG_lexical_block has no attributes, process its children as if
13260 there was no wrapping by that DW_TAG_lexical_block.
13261 GCC does no longer produces such DWARF since GCC r224161. */
13262 for (child_die = die->child;
13263 child_die != NULL && child_die->tag;
13264 child_die = child_die->sibling)
13265 {
13266 /* We might already be processing this DIE. This can happen
13267 in an unusual circumstance -- where a subroutine A
13268 appears lexically in another subroutine B, but A actually
13269 inlines B. The recursion is broken here, rather than in
13270 inherit_abstract_dies, because it seems better to simply
13271 drop concrete children here. */
13272 if (!child_die->in_process)
13273 process_die (child_die, cu);
13274 }
13275 return;
13276 case PC_BOUNDS_INVALID:
13277 return;
13278 }
13279 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13280 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13281
13282 cu->get_builder ()->push_context (0, lowpc);
13283 if (die->child != NULL)
13284 {
13285 child_die = die->child;
13286 while (child_die && child_die->tag)
13287 {
13288 process_die (child_die, cu);
13289 child_die = child_die->sibling;
13290 }
13291 }
13292 inherit_abstract_dies (die, cu);
13293 struct context_stack cstk = cu->get_builder ()->pop_context ();
13294
13295 if (*cu->get_builder ()->get_local_symbols () != NULL
13296 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13297 {
13298 struct block *block
13299 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13300 cstk.start_addr, highpc);
13301
13302 /* Note that recording ranges after traversing children, as we
13303 do here, means that recording a parent's ranges entails
13304 walking across all its children's ranges as they appear in
13305 the address map, which is quadratic behavior.
13306
13307 It would be nicer to record the parent's ranges before
13308 traversing its children, simply overriding whatever you find
13309 there. But since we don't even decide whether to create a
13310 block until after we've traversed its children, that's hard
13311 to do. */
13312 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13313 }
13314 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13315 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13316 }
13317
13318 static void dwarf2_ranges_read_low_addrs (unsigned offset,
13319 struct dwarf2_cu *cu,
13320 dwarf_tag tag,
13321 std::vector<CORE_ADDR> &result);
13322
13323 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13324
13325 static void
13326 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13327 {
13328 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13329 struct objfile *objfile = per_objfile->objfile;
13330 struct gdbarch *gdbarch = objfile->arch ();
13331 CORE_ADDR pc, baseaddr;
13332 struct attribute *attr;
13333 void **slot;
13334 int nparams;
13335 struct die_info *child_die;
13336
13337 baseaddr = objfile->text_section_offset ();
13338
13339 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13340 if (attr == NULL)
13341 {
13342 /* This was a pre-DWARF-5 GNU extension alias
13343 for DW_AT_call_return_pc. */
13344 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13345 }
13346 if (!attr)
13347 {
13348 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13349 "DIE %s [in module %s]"),
13350 sect_offset_str (die->sect_off), objfile_name (objfile));
13351 return;
13352 }
13353 pc = attr->as_address () + baseaddr;
13354 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13355 pc -= baseaddr;
13356
13357 if (cu->call_site_htab == NULL)
13358 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
13359 call_site::eq, NULL,
13360 &objfile->objfile_obstack,
13361 hashtab_obstack_allocate, NULL);
13362 struct call_site call_site_local (pc, nullptr, nullptr);
13363 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13364 if (*slot != NULL)
13365 {
13366 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13367 "DIE %s [in module %s]"),
13368 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13369 objfile_name (objfile));
13370 return;
13371 }
13372
13373 /* Count parameters at the caller. */
13374
13375 nparams = 0;
13376 for (child_die = die->child; child_die && child_die->tag;
13377 child_die = child_die->sibling)
13378 {
13379 if (child_die->tag != DW_TAG_call_site_parameter
13380 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13381 {
13382 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13383 "DW_TAG_call_site child DIE %s [in module %s]"),
13384 child_die->tag, sect_offset_str (child_die->sect_off),
13385 objfile_name (objfile));
13386 continue;
13387 }
13388
13389 nparams++;
13390 }
13391
13392 struct call_site *call_site
13393 = new (XOBNEWVAR (&objfile->objfile_obstack,
13394 struct call_site,
13395 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
13396 struct call_site (pc, cu->per_cu, per_objfile);
13397 *slot = call_site;
13398
13399 /* We never call the destructor of call_site, so we must ensure it is
13400 trivially destructible. */
13401 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
13402
13403 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13404 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13405 {
13406 struct die_info *func_die;
13407
13408 /* Skip also over DW_TAG_inlined_subroutine. */
13409 for (func_die = die->parent;
13410 func_die && func_die->tag != DW_TAG_subprogram
13411 && func_die->tag != DW_TAG_subroutine_type;
13412 func_die = func_die->parent);
13413
13414 /* DW_AT_call_all_calls is a superset
13415 of DW_AT_call_all_tail_calls. */
13416 if (func_die
13417 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13418 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13419 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13420 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13421 {
13422 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13423 not complete. But keep CALL_SITE for look ups via call_site_htab,
13424 both the initial caller containing the real return address PC and
13425 the final callee containing the current PC of a chain of tail
13426 calls do not need to have the tail call list complete. But any
13427 function candidate for a virtual tail call frame searched via
13428 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13429 determined unambiguously. */
13430 }
13431 else
13432 {
13433 struct type *func_type = NULL;
13434
13435 if (func_die)
13436 func_type = get_die_type (func_die, cu);
13437 if (func_type != NULL)
13438 {
13439 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13440
13441 /* Enlist this call site to the function. */
13442 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13443 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13444 }
13445 else
13446 complaint (_("Cannot find function owning DW_TAG_call_site "
13447 "DIE %s [in module %s]"),
13448 sect_offset_str (die->sect_off), objfile_name (objfile));
13449 }
13450 }
13451
13452 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13453 if (attr == NULL)
13454 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13455 if (attr == NULL)
13456 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13457 if (attr == NULL)
13458 {
13459 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13460 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13461 }
13462
13463 call_site->target.set_loc_dwarf_block (nullptr);
13464 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13465 /* Keep NULL DWARF_BLOCK. */;
13466 else if (attr->form_is_block ())
13467 {
13468 struct dwarf2_locexpr_baton *dlbaton;
13469 struct dwarf_block *block = attr->as_block ();
13470
13471 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13472 dlbaton->data = block->data;
13473 dlbaton->size = block->size;
13474 dlbaton->per_objfile = per_objfile;
13475 dlbaton->per_cu = cu->per_cu;
13476
13477 call_site->target.set_loc_dwarf_block (dlbaton);
13478 }
13479 else if (attr->form_is_ref ())
13480 {
13481 struct dwarf2_cu *target_cu = cu;
13482 struct die_info *target_die;
13483
13484 target_die = follow_die_ref (die, attr, &target_cu);
13485 gdb_assert (target_cu->per_objfile->objfile == objfile);
13486
13487 struct attribute *ranges_attr
13488 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
13489
13490 if (die_is_declaration (target_die, target_cu))
13491 {
13492 const char *target_physname;
13493
13494 /* Prefer the mangled name; otherwise compute the demangled one. */
13495 target_physname = dw2_linkage_name (target_die, target_cu);
13496 if (target_physname == NULL)
13497 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13498 if (target_physname == NULL)
13499 complaint (_("DW_AT_call_target target DIE has invalid "
13500 "physname, for referencing DIE %s [in module %s]"),
13501 sect_offset_str (die->sect_off), objfile_name (objfile));
13502 else
13503 call_site->target.set_loc_physname (target_physname);
13504 }
13505 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
13506 {
13507 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
13508 + target_cu->gnu_ranges_base);
13509 std::vector<CORE_ADDR> addresses;
13510 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
13511 target_die->tag, addresses);
13512 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
13513 addresses.size ());
13514 std::copy (addresses.begin (), addresses.end (), saved);
13515 call_site->target.set_loc_array (addresses.size (), saved);
13516 }
13517 else
13518 {
13519 CORE_ADDR lowpc;
13520
13521 /* DW_AT_entry_pc should be preferred. */
13522 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13523 <= PC_BOUNDS_INVALID)
13524 complaint (_("DW_AT_call_target target DIE has invalid "
13525 "low pc, for referencing DIE %s [in module %s]"),
13526 sect_offset_str (die->sect_off), objfile_name (objfile));
13527 else
13528 {
13529 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
13530 - baseaddr);
13531 call_site->target.set_loc_physaddr (lowpc);
13532 }
13533 }
13534 }
13535 else
13536 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13537 "block nor reference, for DIE %s [in module %s]"),
13538 sect_offset_str (die->sect_off), objfile_name (objfile));
13539
13540 for (child_die = die->child;
13541 child_die && child_die->tag;
13542 child_die = child_die->sibling)
13543 {
13544 struct call_site_parameter *parameter;
13545 struct attribute *loc, *origin;
13546
13547 if (child_die->tag != DW_TAG_call_site_parameter
13548 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13549 {
13550 /* Already printed the complaint above. */
13551 continue;
13552 }
13553
13554 gdb_assert (call_site->parameter_count < nparams);
13555 parameter = &call_site->parameter[call_site->parameter_count];
13556
13557 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13558 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13559 register is contained in DW_AT_call_value. */
13560
13561 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13562 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13563 if (origin == NULL)
13564 {
13565 /* This was a pre-DWARF-5 GNU extension alias
13566 for DW_AT_call_parameter. */
13567 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13568 }
13569 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13570 {
13571 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13572
13573 sect_offset sect_off = origin->get_ref_die_offset ();
13574 if (!cu->header.offset_in_cu_p (sect_off))
13575 {
13576 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13577 binding can be done only inside one CU. Such referenced DIE
13578 therefore cannot be even moved to DW_TAG_partial_unit. */
13579 complaint (_("DW_AT_call_parameter offset is not in CU for "
13580 "DW_TAG_call_site child DIE %s [in module %s]"),
13581 sect_offset_str (child_die->sect_off),
13582 objfile_name (objfile));
13583 continue;
13584 }
13585 parameter->u.param_cu_off
13586 = (cu_offset) (sect_off - cu->header.sect_off);
13587 }
13588 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13589 {
13590 complaint (_("No DW_FORM_block* DW_AT_location for "
13591 "DW_TAG_call_site child DIE %s [in module %s]"),
13592 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13593 continue;
13594 }
13595 else
13596 {
13597 struct dwarf_block *block = loc->as_block ();
13598
13599 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13600 (block->data, &block->data[block->size]);
13601 if (parameter->u.dwarf_reg != -1)
13602 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13603 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13604 &block->data[block->size],
13605 &parameter->u.fb_offset))
13606 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13607 else
13608 {
13609 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13610 "for DW_FORM_block* DW_AT_location is supported for "
13611 "DW_TAG_call_site child DIE %s "
13612 "[in module %s]"),
13613 sect_offset_str (child_die->sect_off),
13614 objfile_name (objfile));
13615 continue;
13616 }
13617 }
13618
13619 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13620 if (attr == NULL)
13621 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13622 if (attr == NULL || !attr->form_is_block ())
13623 {
13624 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13625 "DW_TAG_call_site child DIE %s [in module %s]"),
13626 sect_offset_str (child_die->sect_off),
13627 objfile_name (objfile));
13628 continue;
13629 }
13630
13631 struct dwarf_block *block = attr->as_block ();
13632 parameter->value = block->data;
13633 parameter->value_size = block->size;
13634
13635 /* Parameters are not pre-cleared by memset above. */
13636 parameter->data_value = NULL;
13637 parameter->data_value_size = 0;
13638 call_site->parameter_count++;
13639
13640 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13641 if (attr == NULL)
13642 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13643 if (attr != nullptr)
13644 {
13645 if (!attr->form_is_block ())
13646 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13647 "DW_TAG_call_site child DIE %s [in module %s]"),
13648 sect_offset_str (child_die->sect_off),
13649 objfile_name (objfile));
13650 else
13651 {
13652 block = attr->as_block ();
13653 parameter->data_value = block->data;
13654 parameter->data_value_size = block->size;
13655 }
13656 }
13657 }
13658 }
13659
13660 /* Helper function for read_variable. If DIE represents a virtual
13661 table, then return the type of the concrete object that is
13662 associated with the virtual table. Otherwise, return NULL. */
13663
13664 static struct type *
13665 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13666 {
13667 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13668 if (attr == NULL)
13669 return NULL;
13670
13671 /* Find the type DIE. */
13672 struct die_info *type_die = NULL;
13673 struct dwarf2_cu *type_cu = cu;
13674
13675 if (attr->form_is_ref ())
13676 type_die = follow_die_ref (die, attr, &type_cu);
13677 if (type_die == NULL)
13678 return NULL;
13679
13680 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13681 return NULL;
13682 return die_containing_type (type_die, type_cu);
13683 }
13684
13685 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13686
13687 static void
13688 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13689 {
13690 struct rust_vtable_symbol *storage = NULL;
13691
13692 if (cu->per_cu->lang == language_rust)
13693 {
13694 struct type *containing_type = rust_containing_type (die, cu);
13695
13696 if (containing_type != NULL)
13697 {
13698 struct objfile *objfile = cu->per_objfile->objfile;
13699
13700 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13701 storage->concrete_type = containing_type;
13702 storage->subclass = SYMBOL_RUST_VTABLE;
13703 }
13704 }
13705
13706 struct symbol *res = new_symbol (die, NULL, cu, storage);
13707 struct attribute *abstract_origin
13708 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13709 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13710 if (res == NULL && loc && abstract_origin)
13711 {
13712 /* We have a variable without a name, but with a location and an abstract
13713 origin. This may be a concrete instance of an abstract variable
13714 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13715 later. */
13716 struct dwarf2_cu *origin_cu = cu;
13717 struct die_info *origin_die
13718 = follow_die_ref (die, abstract_origin, &origin_cu);
13719 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13720 per_objfile->per_bfd->abstract_to_concrete
13721 [origin_die->sect_off].push_back (die->sect_off);
13722 }
13723 }
13724
13725 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13726 reading .debug_rnglists.
13727 Callback's type should be:
13728 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13729 Return true if the attributes are present and valid, otherwise,
13730 return false. */
13731
13732 template <typename Callback>
13733 static bool
13734 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13735 dwarf_tag tag, Callback &&callback)
13736 {
13737 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13738 struct objfile *objfile = per_objfile->objfile;
13739 bfd *obfd = objfile->obfd;
13740 /* Base address selection entry. */
13741 gdb::optional<CORE_ADDR> base;
13742 const gdb_byte *buffer;
13743 bool overflow = false;
13744 ULONGEST addr_index;
13745 struct dwarf2_section_info *rnglists_section;
13746
13747 base = cu->base_address;
13748 rnglists_section = cu_debug_rnglists_section (cu, tag);
13749 rnglists_section->read (objfile);
13750
13751 if (offset >= rnglists_section->size)
13752 {
13753 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13754 offset);
13755 return false;
13756 }
13757 buffer = rnglists_section->buffer + offset;
13758
13759 while (1)
13760 {
13761 /* Initialize it due to a false compiler warning. */
13762 CORE_ADDR range_beginning = 0, range_end = 0;
13763 const gdb_byte *buf_end = (rnglists_section->buffer
13764 + rnglists_section->size);
13765 unsigned int bytes_read;
13766
13767 if (buffer == buf_end)
13768 {
13769 overflow = true;
13770 break;
13771 }
13772 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13773 switch (rlet)
13774 {
13775 case DW_RLE_end_of_list:
13776 break;
13777 case DW_RLE_base_address:
13778 if (buffer + cu->header.addr_size > buf_end)
13779 {
13780 overflow = true;
13781 break;
13782 }
13783 base = cu->header.read_address (obfd, buffer, &bytes_read);
13784 buffer += bytes_read;
13785 break;
13786 case DW_RLE_base_addressx:
13787 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13788 buffer += bytes_read;
13789 base = read_addr_index (cu, addr_index);
13790 break;
13791 case DW_RLE_start_length:
13792 if (buffer + cu->header.addr_size > buf_end)
13793 {
13794 overflow = true;
13795 break;
13796 }
13797 range_beginning = cu->header.read_address (obfd, buffer,
13798 &bytes_read);
13799 buffer += bytes_read;
13800 range_end = (range_beginning
13801 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13802 buffer += bytes_read;
13803 if (buffer > buf_end)
13804 {
13805 overflow = true;
13806 break;
13807 }
13808 break;
13809 case DW_RLE_startx_length:
13810 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13811 buffer += bytes_read;
13812 range_beginning = read_addr_index (cu, addr_index);
13813 if (buffer > buf_end)
13814 {
13815 overflow = true;
13816 break;
13817 }
13818 range_end = (range_beginning
13819 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13820 buffer += bytes_read;
13821 break;
13822 case DW_RLE_offset_pair:
13823 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13824 buffer += bytes_read;
13825 if (buffer > buf_end)
13826 {
13827 overflow = true;
13828 break;
13829 }
13830 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13831 buffer += bytes_read;
13832 if (buffer > buf_end)
13833 {
13834 overflow = true;
13835 break;
13836 }
13837 break;
13838 case DW_RLE_start_end:
13839 if (buffer + 2 * cu->header.addr_size > buf_end)
13840 {
13841 overflow = true;
13842 break;
13843 }
13844 range_beginning = cu->header.read_address (obfd, buffer,
13845 &bytes_read);
13846 buffer += bytes_read;
13847 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13848 buffer += bytes_read;
13849 break;
13850 case DW_RLE_startx_endx:
13851 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13852 buffer += bytes_read;
13853 range_beginning = read_addr_index (cu, addr_index);
13854 if (buffer > buf_end)
13855 {
13856 overflow = true;
13857 break;
13858 }
13859 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13860 buffer += bytes_read;
13861 range_end = read_addr_index (cu, addr_index);
13862 break;
13863 default:
13864 complaint (_("Invalid .debug_rnglists data (no base address)"));
13865 return false;
13866 }
13867 if (rlet == DW_RLE_end_of_list || overflow)
13868 break;
13869 if (rlet == DW_RLE_base_address)
13870 continue;
13871
13872 if (range_beginning > range_end)
13873 {
13874 /* Inverted range entries are invalid. */
13875 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13876 return false;
13877 }
13878
13879 /* Empty range entries have no effect. */
13880 if (range_beginning == range_end)
13881 continue;
13882
13883 /* Only DW_RLE_offset_pair needs the base address added. */
13884 if (rlet == DW_RLE_offset_pair)
13885 {
13886 if (!base.has_value ())
13887 {
13888 /* We have no valid base address for the DW_RLE_offset_pair. */
13889 complaint (_("Invalid .debug_rnglists data (no base address for "
13890 "DW_RLE_offset_pair)"));
13891 return false;
13892 }
13893
13894 range_beginning += *base;
13895 range_end += *base;
13896 }
13897
13898 /* A not-uncommon case of bad debug info.
13899 Don't pollute the addrmap with bad data. */
13900 if (range_beginning == 0
13901 && !per_objfile->per_bfd->has_section_at_zero)
13902 {
13903 complaint (_(".debug_rnglists entry has start address of zero"
13904 " [in module %s]"), objfile_name (objfile));
13905 continue;
13906 }
13907
13908 callback (range_beginning, range_end);
13909 }
13910
13911 if (overflow)
13912 {
13913 complaint (_("Offset %d is not terminated "
13914 "for DW_AT_ranges attribute"),
13915 offset);
13916 return false;
13917 }
13918
13919 return true;
13920 }
13921
13922 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13923 Callback's type should be:
13924 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13925 Return 1 if the attributes are present and valid, otherwise, return 0. */
13926
13927 template <typename Callback>
13928 static int
13929 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13930 Callback &&callback)
13931 {
13932 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13933 struct objfile *objfile = per_objfile->objfile;
13934 struct comp_unit_head *cu_header = &cu->header;
13935 bfd *obfd = objfile->obfd;
13936 unsigned int addr_size = cu_header->addr_size;
13937 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13938 /* Base address selection entry. */
13939 gdb::optional<CORE_ADDR> base;
13940 unsigned int dummy;
13941 const gdb_byte *buffer;
13942
13943 if (cu_header->version >= 5)
13944 return dwarf2_rnglists_process (offset, cu, tag, callback);
13945
13946 base = cu->base_address;
13947
13948 per_objfile->per_bfd->ranges.read (objfile);
13949 if (offset >= per_objfile->per_bfd->ranges.size)
13950 {
13951 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13952 offset);
13953 return 0;
13954 }
13955 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13956
13957 while (1)
13958 {
13959 CORE_ADDR range_beginning, range_end;
13960
13961 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13962 buffer += addr_size;
13963 range_end = cu->header.read_address (obfd, buffer, &dummy);
13964 buffer += addr_size;
13965 offset += 2 * addr_size;
13966
13967 /* An end of list marker is a pair of zero addresses. */
13968 if (range_beginning == 0 && range_end == 0)
13969 /* Found the end of list entry. */
13970 break;
13971
13972 /* Each base address selection entry is a pair of 2 values.
13973 The first is the largest possible address, the second is
13974 the base address. Check for a base address here. */
13975 if ((range_beginning & mask) == mask)
13976 {
13977 /* If we found the largest possible address, then we already
13978 have the base address in range_end. */
13979 base = range_end;
13980 continue;
13981 }
13982
13983 if (!base.has_value ())
13984 {
13985 /* We have no valid base address for the ranges
13986 data. */
13987 complaint (_("Invalid .debug_ranges data (no base address)"));
13988 return 0;
13989 }
13990
13991 if (range_beginning > range_end)
13992 {
13993 /* Inverted range entries are invalid. */
13994 complaint (_("Invalid .debug_ranges data (inverted range)"));
13995 return 0;
13996 }
13997
13998 /* Empty range entries have no effect. */
13999 if (range_beginning == range_end)
14000 continue;
14001
14002 range_beginning += *base;
14003 range_end += *base;
14004
14005 /* A not-uncommon case of bad debug info.
14006 Don't pollute the addrmap with bad data. */
14007 if (range_beginning == 0
14008 && !per_objfile->per_bfd->has_section_at_zero)
14009 {
14010 complaint (_(".debug_ranges entry has start address of zero"
14011 " [in module %s]"), objfile_name (objfile));
14012 continue;
14013 }
14014
14015 callback (range_beginning, range_end);
14016 }
14017
14018 return 1;
14019 }
14020
14021 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14022 Return 1 if the attributes are present and valid, otherwise, return 0.
14023 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14024
14025 static int
14026 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14027 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14028 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14029 {
14030 struct objfile *objfile = cu->per_objfile->objfile;
14031 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14032 struct gdbarch *gdbarch = objfile->arch ();
14033 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14034 int low_set = 0;
14035 CORE_ADDR low = 0;
14036 CORE_ADDR high = 0;
14037 int retval;
14038
14039 retval = dwarf2_ranges_process (offset, cu, tag,
14040 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14041 {
14042 if (ranges_pst != NULL)
14043 {
14044 CORE_ADDR lowpc;
14045 CORE_ADDR highpc;
14046
14047 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14048 range_beginning + baseaddr)
14049 - baseaddr);
14050 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14051 range_end + baseaddr)
14052 - baseaddr);
14053 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14054 lowpc, highpc - 1, ranges_pst);
14055 }
14056
14057 /* FIXME: This is recording everything as a low-high
14058 segment of consecutive addresses. We should have a
14059 data structure for discontiguous block ranges
14060 instead. */
14061 if (! low_set)
14062 {
14063 low = range_beginning;
14064 high = range_end;
14065 low_set = 1;
14066 }
14067 else
14068 {
14069 if (range_beginning < low)
14070 low = range_beginning;
14071 if (range_end > high)
14072 high = range_end;
14073 }
14074 });
14075 if (!retval)
14076 return 0;
14077
14078 if (! low_set)
14079 /* If the first entry is an end-of-list marker, the range
14080 describes an empty scope, i.e. no instructions. */
14081 return 0;
14082
14083 if (low_return)
14084 *low_return = low;
14085 if (high_return)
14086 *high_return = high;
14087 return 1;
14088 }
14089
14090 /* Process ranges and fill in a vector of the low PC values only. */
14091
14092 static void
14093 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
14094 dwarf_tag tag,
14095 std::vector<CORE_ADDR> &result)
14096 {
14097 dwarf2_ranges_process (offset, cu, tag,
14098 [&] (CORE_ADDR start, CORE_ADDR end)
14099 {
14100 result.push_back (start);
14101 });
14102 }
14103
14104 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14105 definition for the return value. *LOWPC and *HIGHPC are set iff
14106 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14107
14108 static enum pc_bounds_kind
14109 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14110 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14111 dwarf2_psymtab *pst)
14112 {
14113 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14114 struct attribute *attr;
14115 struct attribute *attr_high;
14116 CORE_ADDR low = 0;
14117 CORE_ADDR high = 0;
14118 enum pc_bounds_kind ret;
14119
14120 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14121 if (attr_high)
14122 {
14123 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14124 if (attr != nullptr)
14125 {
14126 low = attr->as_address ();
14127 high = attr_high->as_address ();
14128 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14129 high += low;
14130 }
14131 else
14132 /* Found high w/o low attribute. */
14133 return PC_BOUNDS_INVALID;
14134
14135 /* Found consecutive range of addresses. */
14136 ret = PC_BOUNDS_HIGH_LOW;
14137 }
14138 else
14139 {
14140 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14141 if (attr != nullptr && attr->form_is_unsigned ())
14142 {
14143 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14144 on DWARF version). */
14145 ULONGEST ranges_offset = attr->as_unsigned ();
14146
14147 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14148 this value. */
14149 if (die->tag != DW_TAG_compile_unit)
14150 ranges_offset += cu->gnu_ranges_base;
14151
14152 /* Value of the DW_AT_ranges attribute is the offset in the
14153 .debug_ranges section. */
14154 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14155 die->tag))
14156 return PC_BOUNDS_INVALID;
14157 /* Found discontinuous range of addresses. */
14158 ret = PC_BOUNDS_RANGES;
14159 }
14160 else
14161 return PC_BOUNDS_NOT_PRESENT;
14162 }
14163
14164 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14165 if (high <= low)
14166 return PC_BOUNDS_INVALID;
14167
14168 /* When using the GNU linker, .gnu.linkonce. sections are used to
14169 eliminate duplicate copies of functions and vtables and such.
14170 The linker will arbitrarily choose one and discard the others.
14171 The AT_*_pc values for such functions refer to local labels in
14172 these sections. If the section from that file was discarded, the
14173 labels are not in the output, so the relocs get a value of 0.
14174 If this is a discarded function, mark the pc bounds as invalid,
14175 so that GDB will ignore it. */
14176 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14177 return PC_BOUNDS_INVALID;
14178
14179 *lowpc = low;
14180 if (highpc)
14181 *highpc = high;
14182 return ret;
14183 }
14184
14185 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14186 its low and high PC addresses. Do nothing if these addresses could not
14187 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14188 and HIGHPC to the high address if greater than HIGHPC. */
14189
14190 static void
14191 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14192 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14193 struct dwarf2_cu *cu)
14194 {
14195 CORE_ADDR low, high;
14196 struct die_info *child = die->child;
14197
14198 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14199 {
14200 *lowpc = std::min (*lowpc, low);
14201 *highpc = std::max (*highpc, high);
14202 }
14203
14204 /* If the language does not allow nested subprograms (either inside
14205 subprograms or lexical blocks), we're done. */
14206 if (cu->per_cu->lang != language_ada)
14207 return;
14208
14209 /* Check all the children of the given DIE. If it contains nested
14210 subprograms, then check their pc bounds. Likewise, we need to
14211 check lexical blocks as well, as they may also contain subprogram
14212 definitions. */
14213 while (child && child->tag)
14214 {
14215 if (child->tag == DW_TAG_subprogram
14216 || child->tag == DW_TAG_lexical_block)
14217 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14218 child = child->sibling;
14219 }
14220 }
14221
14222 /* Get the low and high pc's represented by the scope DIE, and store
14223 them in *LOWPC and *HIGHPC. If the correct values can't be
14224 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14225
14226 static void
14227 get_scope_pc_bounds (struct die_info *die,
14228 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14229 struct dwarf2_cu *cu)
14230 {
14231 CORE_ADDR best_low = (CORE_ADDR) -1;
14232 CORE_ADDR best_high = (CORE_ADDR) 0;
14233 CORE_ADDR current_low, current_high;
14234
14235 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14236 >= PC_BOUNDS_RANGES)
14237 {
14238 best_low = current_low;
14239 best_high = current_high;
14240 }
14241 else
14242 {
14243 struct die_info *child = die->child;
14244
14245 while (child && child->tag)
14246 {
14247 switch (child->tag) {
14248 case DW_TAG_subprogram:
14249 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14250 break;
14251 case DW_TAG_namespace:
14252 case DW_TAG_module:
14253 /* FIXME: carlton/2004-01-16: Should we do this for
14254 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14255 that current GCC's always emit the DIEs corresponding
14256 to definitions of methods of classes as children of a
14257 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14258 the DIEs giving the declarations, which could be
14259 anywhere). But I don't see any reason why the
14260 standards says that they have to be there. */
14261 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14262
14263 if (current_low != ((CORE_ADDR) -1))
14264 {
14265 best_low = std::min (best_low, current_low);
14266 best_high = std::max (best_high, current_high);
14267 }
14268 break;
14269 default:
14270 /* Ignore. */
14271 break;
14272 }
14273
14274 child = child->sibling;
14275 }
14276 }
14277
14278 *lowpc = best_low;
14279 *highpc = best_high;
14280 }
14281
14282 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14283 in DIE. */
14284
14285 static void
14286 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14287 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14288 {
14289 struct objfile *objfile = cu->per_objfile->objfile;
14290 struct gdbarch *gdbarch = objfile->arch ();
14291 struct attribute *attr;
14292 struct attribute *attr_high;
14293
14294 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14295 if (attr_high)
14296 {
14297 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14298 if (attr != nullptr)
14299 {
14300 CORE_ADDR low = attr->as_address ();
14301 CORE_ADDR high = attr_high->as_address ();
14302
14303 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14304 high += low;
14305
14306 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14307 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14308 cu->get_builder ()->record_block_range (block, low, high - 1);
14309 }
14310 }
14311
14312 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14313 if (attr != nullptr && attr->form_is_unsigned ())
14314 {
14315 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14316 on DWARF version). */
14317 ULONGEST ranges_offset = attr->as_unsigned ();
14318
14319 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14320 this value. */
14321 if (die->tag != DW_TAG_compile_unit)
14322 ranges_offset += cu->gnu_ranges_base;
14323
14324 std::vector<blockrange> blockvec;
14325 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14326 [&] (CORE_ADDR start, CORE_ADDR end)
14327 {
14328 start += baseaddr;
14329 end += baseaddr;
14330 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14331 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14332 cu->get_builder ()->record_block_range (block, start, end - 1);
14333 blockvec.emplace_back (start, end);
14334 });
14335
14336 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14337 }
14338 }
14339
14340 /* Check whether the producer field indicates either of GCC < 4.6, or the
14341 Intel C/C++ compiler, and cache the result in CU. */
14342
14343 static void
14344 check_producer (struct dwarf2_cu *cu)
14345 {
14346 int major, minor;
14347
14348 if (cu->producer == NULL)
14349 {
14350 /* For unknown compilers expect their behavior is DWARF version
14351 compliant.
14352
14353 GCC started to support .debug_types sections by -gdwarf-4 since
14354 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14355 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14356 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14357 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14358 }
14359 else if (producer_is_gcc (cu->producer, &major, &minor))
14360 {
14361 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14362 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14363 cu->producer_is_gcc_11 = major == 11;
14364 }
14365 else if (producer_is_icc (cu->producer, &major, &minor))
14366 {
14367 cu->producer_is_icc = true;
14368 cu->producer_is_icc_lt_14 = major < 14;
14369 }
14370 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14371 cu->producer_is_codewarrior = true;
14372 else
14373 {
14374 /* For other non-GCC compilers, expect their behavior is DWARF version
14375 compliant. */
14376 }
14377
14378 cu->checked_producer = true;
14379 }
14380
14381 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14382 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14383 during 4.6.0 experimental. */
14384
14385 static bool
14386 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14387 {
14388 if (!cu->checked_producer)
14389 check_producer (cu);
14390
14391 return cu->producer_is_gxx_lt_4_6;
14392 }
14393
14394
14395 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14396 with incorrect is_stmt attributes. */
14397
14398 static bool
14399 producer_is_codewarrior (struct dwarf2_cu *cu)
14400 {
14401 if (!cu->checked_producer)
14402 check_producer (cu);
14403
14404 return cu->producer_is_codewarrior;
14405 }
14406
14407 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14408 If that attribute is not available, return the appropriate
14409 default. */
14410
14411 static enum dwarf_access_attribute
14412 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14413 {
14414 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14415 if (attr != nullptr)
14416 {
14417 LONGEST value = attr->constant_value (-1);
14418 if (value == DW_ACCESS_public
14419 || value == DW_ACCESS_protected
14420 || value == DW_ACCESS_private)
14421 return (dwarf_access_attribute) value;
14422 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14423 plongest (value));
14424 }
14425
14426 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14427 {
14428 /* The default DWARF 2 accessibility for members is public, the default
14429 accessibility for inheritance is private. */
14430
14431 if (die->tag != DW_TAG_inheritance)
14432 return DW_ACCESS_public;
14433 else
14434 return DW_ACCESS_private;
14435 }
14436 else
14437 {
14438 /* DWARF 3+ defines the default accessibility a different way. The same
14439 rules apply now for DW_TAG_inheritance as for the members and it only
14440 depends on the container kind. */
14441
14442 if (die->parent->tag == DW_TAG_class_type)
14443 return DW_ACCESS_private;
14444 else
14445 return DW_ACCESS_public;
14446 }
14447 }
14448
14449 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
14450 *OFFSET to the byte offset. If the attribute was not found return
14451 0, otherwise return 1. If it was found but could not properly be
14452 handled, set *OFFSET to 0. */
14453
14454 static int
14455 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14456 LONGEST *offset)
14457 {
14458 struct attribute *attr;
14459
14460 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14461 if (attr != NULL)
14462 {
14463 *offset = 0;
14464
14465 /* Note that we do not check for a section offset first here.
14466 This is because DW_AT_data_member_location is new in DWARF 4,
14467 so if we see it, we can assume that a constant form is really
14468 a constant and not a section offset. */
14469 if (attr->form_is_constant ())
14470 *offset = attr->constant_value (0);
14471 else if (attr->form_is_section_offset ())
14472 dwarf2_complex_location_expr_complaint ();
14473 else if (attr->form_is_block ())
14474 *offset = decode_locdesc (attr->as_block (), cu);
14475 else
14476 dwarf2_complex_location_expr_complaint ();
14477
14478 return 1;
14479 }
14480 else
14481 {
14482 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14483 if (attr != nullptr)
14484 {
14485 *offset = attr->constant_value (0);
14486 return 1;
14487 }
14488 }
14489
14490 return 0;
14491 }
14492
14493 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
14494 store the results in FIELD. */
14495
14496 static void
14497 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14498 struct field *field)
14499 {
14500 struct attribute *attr;
14501
14502 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14503 if (attr != NULL)
14504 {
14505 if (attr->form_is_constant ())
14506 {
14507 LONGEST offset = attr->constant_value (0);
14508
14509 /* Work around this GCC 11 bug, where it would erroneously use -1
14510 data member locations, instead of 0:
14511
14512 Negative DW_AT_data_member_location
14513 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
14514 */
14515 if (offset == -1 && cu->producer_is_gcc_11)
14516 {
14517 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
14518 offset = 0;
14519 }
14520
14521 field->set_loc_bitpos (offset * bits_per_byte);
14522 }
14523 else if (attr->form_is_section_offset ())
14524 dwarf2_complex_location_expr_complaint ();
14525 else if (attr->form_is_block ())
14526 {
14527 bool handled;
14528 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14529 if (handled)
14530 field->set_loc_bitpos (offset * bits_per_byte);
14531 else
14532 {
14533 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14534 struct objfile *objfile = per_objfile->objfile;
14535 struct dwarf2_locexpr_baton *dlbaton
14536 = XOBNEW (&objfile->objfile_obstack,
14537 struct dwarf2_locexpr_baton);
14538 dlbaton->data = attr->as_block ()->data;
14539 dlbaton->size = attr->as_block ()->size;
14540 /* When using this baton, we want to compute the address
14541 of the field, not the value. This is why
14542 is_reference is set to false here. */
14543 dlbaton->is_reference = false;
14544 dlbaton->per_objfile = per_objfile;
14545 dlbaton->per_cu = cu->per_cu;
14546
14547 field->set_loc_dwarf_block (dlbaton);
14548 }
14549 }
14550 else
14551 dwarf2_complex_location_expr_complaint ();
14552 }
14553 else
14554 {
14555 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14556 if (attr != nullptr)
14557 field->set_loc_bitpos (attr->constant_value (0));
14558 }
14559 }
14560
14561 /* Add an aggregate field to the field list. */
14562
14563 static void
14564 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14565 struct dwarf2_cu *cu)
14566 {
14567 struct objfile *objfile = cu->per_objfile->objfile;
14568 struct gdbarch *gdbarch = objfile->arch ();
14569 struct nextfield *new_field;
14570 struct attribute *attr;
14571 struct field *fp;
14572 const char *fieldname = "";
14573
14574 if (die->tag == DW_TAG_inheritance)
14575 {
14576 fip->baseclasses.emplace_back ();
14577 new_field = &fip->baseclasses.back ();
14578 }
14579 else
14580 {
14581 fip->fields.emplace_back ();
14582 new_field = &fip->fields.back ();
14583 }
14584
14585 new_field->offset = die->sect_off;
14586
14587 new_field->accessibility = dwarf2_access_attribute (die, cu);
14588 if (new_field->accessibility != DW_ACCESS_public)
14589 fip->non_public_fields = true;
14590
14591 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14592 if (attr != nullptr)
14593 new_field->virtuality = attr->as_virtuality ();
14594 else
14595 new_field->virtuality = DW_VIRTUALITY_none;
14596
14597 fp = &new_field->field;
14598
14599 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
14600 && !die_is_declaration (die, cu))
14601 {
14602 if (die->tag == DW_TAG_namelist_item)
14603 {
14604 /* Typically, DW_TAG_namelist_item are references to namelist items.
14605 If so, follow that reference. */
14606 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
14607 struct die_info *item_die = nullptr;
14608 struct dwarf2_cu *item_cu = cu;
14609 if (attr1->form_is_ref ())
14610 item_die = follow_die_ref (die, attr1, &item_cu);
14611 if (item_die != nullptr)
14612 die = item_die;
14613 }
14614 /* Data member other than a C++ static data member. */
14615
14616 /* Get type of field. */
14617 fp->set_type (die_type (die, cu));
14618
14619 fp->set_loc_bitpos (0);
14620
14621 /* Get bit size of field (zero if none). */
14622 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14623 if (attr != nullptr)
14624 {
14625 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14626 }
14627 else
14628 {
14629 FIELD_BITSIZE (*fp) = 0;
14630 }
14631
14632 /* Get bit offset of field. */
14633 handle_member_location (die, cu, fp);
14634 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14635 if (attr != nullptr && attr->form_is_constant ())
14636 {
14637 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14638 {
14639 /* For big endian bits, the DW_AT_bit_offset gives the
14640 additional bit offset from the MSB of the containing
14641 anonymous object to the MSB of the field. We don't
14642 have to do anything special since we don't need to
14643 know the size of the anonymous object. */
14644 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
14645 }
14646 else
14647 {
14648 /* For little endian bits, compute the bit offset to the
14649 MSB of the anonymous object, subtract off the number of
14650 bits from the MSB of the field to the MSB of the
14651 object, and then subtract off the number of bits of
14652 the field itself. The result is the bit offset of
14653 the LSB of the field. */
14654 int anonymous_size;
14655 int bit_offset = attr->constant_value (0);
14656
14657 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14658 if (attr != nullptr && attr->form_is_constant ())
14659 {
14660 /* The size of the anonymous object containing
14661 the bit field is explicit, so use the
14662 indicated size (in bytes). */
14663 anonymous_size = attr->constant_value (0);
14664 }
14665 else
14666 {
14667 /* The size of the anonymous object containing
14668 the bit field must be inferred from the type
14669 attribute of the data member containing the
14670 bit field. */
14671 anonymous_size = TYPE_LENGTH (fp->type ());
14672 }
14673 fp->set_loc_bitpos (fp->loc_bitpos ()
14674 + anonymous_size * bits_per_byte
14675 - bit_offset - FIELD_BITSIZE (*fp));
14676 }
14677 }
14678
14679 /* Get name of field. */
14680 fieldname = dwarf2_name (die, cu);
14681 if (fieldname == NULL)
14682 fieldname = "";
14683
14684 /* The name is already allocated along with this objfile, so we don't
14685 need to duplicate it for the type. */
14686 fp->set_name (fieldname);
14687
14688 /* Change accessibility for artificial fields (e.g. virtual table
14689 pointer or virtual base class pointer) to private. */
14690 if (dwarf2_attr (die, DW_AT_artificial, cu))
14691 {
14692 FIELD_ARTIFICIAL (*fp) = 1;
14693 new_field->accessibility = DW_ACCESS_private;
14694 fip->non_public_fields = true;
14695 }
14696 }
14697 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14698 {
14699 /* C++ static member. */
14700
14701 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14702 is a declaration, but all versions of G++ as of this writing
14703 (so through at least 3.2.1) incorrectly generate
14704 DW_TAG_variable tags. */
14705
14706 const char *physname;
14707
14708 /* Get name of field. */
14709 fieldname = dwarf2_name (die, cu);
14710 if (fieldname == NULL)
14711 return;
14712
14713 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14714 if (attr
14715 /* Only create a symbol if this is an external value.
14716 new_symbol checks this and puts the value in the global symbol
14717 table, which we want. If it is not external, new_symbol
14718 will try to put the value in cu->list_in_scope which is wrong. */
14719 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14720 {
14721 /* A static const member, not much different than an enum as far as
14722 we're concerned, except that we can support more types. */
14723 new_symbol (die, NULL, cu);
14724 }
14725
14726 /* Get physical name. */
14727 physname = dwarf2_physname (fieldname, die, cu);
14728
14729 /* The name is already allocated along with this objfile, so we don't
14730 need to duplicate it for the type. */
14731 fp->set_loc_physname (physname ? physname : "");
14732 fp->set_type (die_type (die, cu));
14733 fp->set_name (fieldname);
14734 }
14735 else if (die->tag == DW_TAG_inheritance)
14736 {
14737 /* C++ base class field. */
14738 handle_member_location (die, cu, fp);
14739 FIELD_BITSIZE (*fp) = 0;
14740 fp->set_type (die_type (die, cu));
14741 fp->set_name (fp->type ()->name ());
14742 }
14743 else
14744 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14745 }
14746
14747 /* Can the type given by DIE define another type? */
14748
14749 static bool
14750 type_can_define_types (const struct die_info *die)
14751 {
14752 switch (die->tag)
14753 {
14754 case DW_TAG_typedef:
14755 case DW_TAG_class_type:
14756 case DW_TAG_structure_type:
14757 case DW_TAG_union_type:
14758 case DW_TAG_enumeration_type:
14759 return true;
14760
14761 default:
14762 return false;
14763 }
14764 }
14765
14766 /* Add a type definition defined in the scope of the FIP's class. */
14767
14768 static void
14769 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14770 struct dwarf2_cu *cu)
14771 {
14772 struct decl_field fp;
14773 memset (&fp, 0, sizeof (fp));
14774
14775 gdb_assert (type_can_define_types (die));
14776
14777 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14778 fp.name = dwarf2_name (die, cu);
14779 fp.type = read_type_die (die, cu);
14780
14781 /* Save accessibility. */
14782 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14783 switch (accessibility)
14784 {
14785 case DW_ACCESS_public:
14786 /* The assumed value if neither private nor protected. */
14787 break;
14788 case DW_ACCESS_private:
14789 fp.is_private = 1;
14790 break;
14791 case DW_ACCESS_protected:
14792 fp.is_protected = 1;
14793 break;
14794 }
14795
14796 if (die->tag == DW_TAG_typedef)
14797 fip->typedef_field_list.push_back (fp);
14798 else
14799 fip->nested_types_list.push_back (fp);
14800 }
14801
14802 /* A convenience typedef that's used when finding the discriminant
14803 field for a variant part. */
14804 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14805 offset_map_type;
14806
14807 /* Compute the discriminant range for a given variant. OBSTACK is
14808 where the results will be stored. VARIANT is the variant to
14809 process. IS_UNSIGNED indicates whether the discriminant is signed
14810 or unsigned. */
14811
14812 static const gdb::array_view<discriminant_range>
14813 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14814 bool is_unsigned)
14815 {
14816 std::vector<discriminant_range> ranges;
14817
14818 if (variant.default_branch)
14819 return {};
14820
14821 if (variant.discr_list_data == nullptr)
14822 {
14823 discriminant_range r
14824 = {variant.discriminant_value, variant.discriminant_value};
14825 ranges.push_back (r);
14826 }
14827 else
14828 {
14829 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14830 variant.discr_list_data->size);
14831 while (!data.empty ())
14832 {
14833 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14834 {
14835 complaint (_("invalid discriminant marker: %d"), data[0]);
14836 break;
14837 }
14838 bool is_range = data[0] == DW_DSC_range;
14839 data = data.slice (1);
14840
14841 ULONGEST low, high;
14842 unsigned int bytes_read;
14843
14844 if (data.empty ())
14845 {
14846 complaint (_("DW_AT_discr_list missing low value"));
14847 break;
14848 }
14849 if (is_unsigned)
14850 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14851 else
14852 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14853 &bytes_read);
14854 data = data.slice (bytes_read);
14855
14856 if (is_range)
14857 {
14858 if (data.empty ())
14859 {
14860 complaint (_("DW_AT_discr_list missing high value"));
14861 break;
14862 }
14863 if (is_unsigned)
14864 high = read_unsigned_leb128 (nullptr, data.data (),
14865 &bytes_read);
14866 else
14867 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14868 &bytes_read);
14869 data = data.slice (bytes_read);
14870 }
14871 else
14872 high = low;
14873
14874 ranges.push_back ({ low, high });
14875 }
14876 }
14877
14878 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14879 ranges.size ());
14880 std::copy (ranges.begin (), ranges.end (), result);
14881 return gdb::array_view<discriminant_range> (result, ranges.size ());
14882 }
14883
14884 static const gdb::array_view<variant_part> create_variant_parts
14885 (struct obstack *obstack,
14886 const offset_map_type &offset_map,
14887 struct field_info *fi,
14888 const std::vector<variant_part_builder> &variant_parts);
14889
14890 /* Fill in a "struct variant" for a given variant field. RESULT is
14891 the variant to fill in. OBSTACK is where any needed allocations
14892 will be done. OFFSET_MAP holds the mapping from section offsets to
14893 fields for the type. FI describes the fields of the type we're
14894 processing. FIELD is the variant field we're converting. */
14895
14896 static void
14897 create_one_variant (variant &result, struct obstack *obstack,
14898 const offset_map_type &offset_map,
14899 struct field_info *fi, const variant_field &field)
14900 {
14901 result.discriminants = convert_variant_range (obstack, field, false);
14902 result.first_field = field.first_field + fi->baseclasses.size ();
14903 result.last_field = field.last_field + fi->baseclasses.size ();
14904 result.parts = create_variant_parts (obstack, offset_map, fi,
14905 field.variant_parts);
14906 }
14907
14908 /* Fill in a "struct variant_part" for a given variant part. RESULT
14909 is the variant part to fill in. OBSTACK is where any needed
14910 allocations will be done. OFFSET_MAP holds the mapping from
14911 section offsets to fields for the type. FI describes the fields of
14912 the type we're processing. BUILDER is the variant part to be
14913 converted. */
14914
14915 static void
14916 create_one_variant_part (variant_part &result,
14917 struct obstack *obstack,
14918 const offset_map_type &offset_map,
14919 struct field_info *fi,
14920 const variant_part_builder &builder)
14921 {
14922 auto iter = offset_map.find (builder.discriminant_offset);
14923 if (iter == offset_map.end ())
14924 {
14925 result.discriminant_index = -1;
14926 /* Doesn't matter. */
14927 result.is_unsigned = false;
14928 }
14929 else
14930 {
14931 result.discriminant_index = iter->second;
14932 result.is_unsigned
14933 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14934 }
14935
14936 size_t n = builder.variants.size ();
14937 variant *output = new (obstack) variant[n];
14938 for (size_t i = 0; i < n; ++i)
14939 create_one_variant (output[i], obstack, offset_map, fi,
14940 builder.variants[i]);
14941
14942 result.variants = gdb::array_view<variant> (output, n);
14943 }
14944
14945 /* Create a vector of variant parts that can be attached to a type.
14946 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14947 holds the mapping from section offsets to fields for the type. FI
14948 describes the fields of the type we're processing. VARIANT_PARTS
14949 is the vector to convert. */
14950
14951 static const gdb::array_view<variant_part>
14952 create_variant_parts (struct obstack *obstack,
14953 const offset_map_type &offset_map,
14954 struct field_info *fi,
14955 const std::vector<variant_part_builder> &variant_parts)
14956 {
14957 if (variant_parts.empty ())
14958 return {};
14959
14960 size_t n = variant_parts.size ();
14961 variant_part *result = new (obstack) variant_part[n];
14962 for (size_t i = 0; i < n; ++i)
14963 create_one_variant_part (result[i], obstack, offset_map, fi,
14964 variant_parts[i]);
14965
14966 return gdb::array_view<variant_part> (result, n);
14967 }
14968
14969 /* Compute the variant part vector for FIP, attaching it to TYPE when
14970 done. */
14971
14972 static void
14973 add_variant_property (struct field_info *fip, struct type *type,
14974 struct dwarf2_cu *cu)
14975 {
14976 /* Map section offsets of fields to their field index. Note the
14977 field index here does not take the number of baseclasses into
14978 account. */
14979 offset_map_type offset_map;
14980 for (int i = 0; i < fip->fields.size (); ++i)
14981 offset_map[fip->fields[i].offset] = i;
14982
14983 struct objfile *objfile = cu->per_objfile->objfile;
14984 gdb::array_view<const variant_part> parts
14985 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14986 fip->variant_parts);
14987
14988 struct dynamic_prop prop;
14989 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14990 obstack_copy (&objfile->objfile_obstack, &parts,
14991 sizeof (parts)));
14992
14993 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14994 }
14995
14996 /* Create the vector of fields, and attach it to the type. */
14997
14998 static void
14999 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15000 struct dwarf2_cu *cu)
15001 {
15002 int nfields = fip->nfields ();
15003
15004 /* Record the field count, allocate space for the array of fields,
15005 and create blank accessibility bitfields if necessary. */
15006 type->set_num_fields (nfields);
15007 type->set_fields
15008 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15009
15010 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
15011 {
15012 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15013
15014 TYPE_FIELD_PRIVATE_BITS (type) =
15015 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15016 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15017
15018 TYPE_FIELD_PROTECTED_BITS (type) =
15019 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15020 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15021
15022 TYPE_FIELD_IGNORE_BITS (type) =
15023 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15024 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15025 }
15026
15027 /* If the type has baseclasses, allocate and clear a bit vector for
15028 TYPE_FIELD_VIRTUAL_BITS. */
15029 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
15030 {
15031 int num_bytes = B_BYTES (fip->baseclasses.size ());
15032 unsigned char *pointer;
15033
15034 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15035 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15036 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15037 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15038 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15039 }
15040
15041 if (!fip->variant_parts.empty ())
15042 add_variant_property (fip, type, cu);
15043
15044 /* Copy the saved-up fields into the field vector. */
15045 for (int i = 0; i < nfields; ++i)
15046 {
15047 struct nextfield &field
15048 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15049 : fip->fields[i - fip->baseclasses.size ()]);
15050
15051 type->field (i) = field.field;
15052 switch (field.accessibility)
15053 {
15054 case DW_ACCESS_private:
15055 if (cu->per_cu->lang != language_ada)
15056 SET_TYPE_FIELD_PRIVATE (type, i);
15057 break;
15058
15059 case DW_ACCESS_protected:
15060 if (cu->per_cu->lang != language_ada)
15061 SET_TYPE_FIELD_PROTECTED (type, i);
15062 break;
15063
15064 case DW_ACCESS_public:
15065 break;
15066
15067 default:
15068 /* Unknown accessibility. Complain and treat it as public. */
15069 {
15070 complaint (_("unsupported accessibility %d"),
15071 field.accessibility);
15072 }
15073 break;
15074 }
15075 if (i < fip->baseclasses.size ())
15076 {
15077 switch (field.virtuality)
15078 {
15079 case DW_VIRTUALITY_virtual:
15080 case DW_VIRTUALITY_pure_virtual:
15081 if (cu->per_cu->lang == language_ada)
15082 error (_("unexpected virtuality in component of Ada type"));
15083 SET_TYPE_FIELD_VIRTUAL (type, i);
15084 break;
15085 }
15086 }
15087 }
15088 }
15089
15090 /* Return true if this member function is a constructor, false
15091 otherwise. */
15092
15093 static int
15094 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15095 {
15096 const char *fieldname;
15097 const char *type_name;
15098 int len;
15099
15100 if (die->parent == NULL)
15101 return 0;
15102
15103 if (die->parent->tag != DW_TAG_structure_type
15104 && die->parent->tag != DW_TAG_union_type
15105 && die->parent->tag != DW_TAG_class_type)
15106 return 0;
15107
15108 fieldname = dwarf2_name (die, cu);
15109 type_name = dwarf2_name (die->parent, cu);
15110 if (fieldname == NULL || type_name == NULL)
15111 return 0;
15112
15113 len = strlen (fieldname);
15114 return (strncmp (fieldname, type_name, len) == 0
15115 && (type_name[len] == '\0' || type_name[len] == '<'));
15116 }
15117
15118 /* Add a member function to the proper fieldlist. */
15119
15120 static void
15121 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15122 struct type *type, struct dwarf2_cu *cu)
15123 {
15124 struct objfile *objfile = cu->per_objfile->objfile;
15125 struct attribute *attr;
15126 int i;
15127 struct fnfieldlist *flp = nullptr;
15128 struct fn_field *fnp;
15129 const char *fieldname;
15130 struct type *this_type;
15131
15132 if (cu->per_cu->lang == language_ada)
15133 error (_("unexpected member function in Ada type"));
15134
15135 /* Get name of member function. */
15136 fieldname = dwarf2_name (die, cu);
15137 if (fieldname == NULL)
15138 return;
15139
15140 /* Look up member function name in fieldlist. */
15141 for (i = 0; i < fip->fnfieldlists.size (); i++)
15142 {
15143 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15144 {
15145 flp = &fip->fnfieldlists[i];
15146 break;
15147 }
15148 }
15149
15150 /* Create a new fnfieldlist if necessary. */
15151 if (flp == nullptr)
15152 {
15153 fip->fnfieldlists.emplace_back ();
15154 flp = &fip->fnfieldlists.back ();
15155 flp->name = fieldname;
15156 i = fip->fnfieldlists.size () - 1;
15157 }
15158
15159 /* Create a new member function field and add it to the vector of
15160 fnfieldlists. */
15161 flp->fnfields.emplace_back ();
15162 fnp = &flp->fnfields.back ();
15163
15164 /* Delay processing of the physname until later. */
15165 if (cu->per_cu->lang == language_cplus)
15166 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15167 die, cu);
15168 else
15169 {
15170 const char *physname = dwarf2_physname (fieldname, die, cu);
15171 fnp->physname = physname ? physname : "";
15172 }
15173
15174 fnp->type = alloc_type (objfile);
15175 this_type = read_type_die (die, cu);
15176 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15177 {
15178 int nparams = this_type->num_fields ();
15179
15180 /* TYPE is the domain of this method, and THIS_TYPE is the type
15181 of the method itself (TYPE_CODE_METHOD). */
15182 smash_to_method_type (fnp->type, type,
15183 TYPE_TARGET_TYPE (this_type),
15184 this_type->fields (),
15185 this_type->num_fields (),
15186 this_type->has_varargs ());
15187
15188 /* Handle static member functions.
15189 Dwarf2 has no clean way to discern C++ static and non-static
15190 member functions. G++ helps GDB by marking the first
15191 parameter for non-static member functions (which is the this
15192 pointer) as artificial. We obtain this information from
15193 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15194 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15195 fnp->voffset = VOFFSET_STATIC;
15196 }
15197 else
15198 complaint (_("member function type missing for '%s'"),
15199 dwarf2_full_name (fieldname, die, cu));
15200
15201 /* Get fcontext from DW_AT_containing_type if present. */
15202 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15203 fnp->fcontext = die_containing_type (die, cu);
15204
15205 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15206 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15207
15208 /* Get accessibility. */
15209 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15210 switch (accessibility)
15211 {
15212 case DW_ACCESS_private:
15213 fnp->is_private = 1;
15214 break;
15215 case DW_ACCESS_protected:
15216 fnp->is_protected = 1;
15217 break;
15218 }
15219
15220 /* Check for artificial methods. */
15221 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15222 if (attr && attr->as_boolean ())
15223 fnp->is_artificial = 1;
15224
15225 /* Check for defaulted methods. */
15226 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15227 if (attr != nullptr)
15228 fnp->defaulted = attr->defaulted ();
15229
15230 /* Check for deleted methods. */
15231 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15232 if (attr != nullptr && attr->as_boolean ())
15233 fnp->is_deleted = 1;
15234
15235 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15236
15237 /* Get index in virtual function table if it is a virtual member
15238 function. For older versions of GCC, this is an offset in the
15239 appropriate virtual table, as specified by DW_AT_containing_type.
15240 For everyone else, it is an expression to be evaluated relative
15241 to the object address. */
15242
15243 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15244 if (attr != nullptr)
15245 {
15246 if (attr->form_is_block () && attr->as_block ()->size > 0)
15247 {
15248 struct dwarf_block *block = attr->as_block ();
15249
15250 if (block->data[0] == DW_OP_constu)
15251 {
15252 /* Old-style GCC. */
15253 fnp->voffset = decode_locdesc (block, cu) + 2;
15254 }
15255 else if (block->data[0] == DW_OP_deref
15256 || (block->size > 1
15257 && block->data[0] == DW_OP_deref_size
15258 && block->data[1] == cu->header.addr_size))
15259 {
15260 fnp->voffset = decode_locdesc (block, cu);
15261 if ((fnp->voffset % cu->header.addr_size) != 0)
15262 dwarf2_complex_location_expr_complaint ();
15263 else
15264 fnp->voffset /= cu->header.addr_size;
15265 fnp->voffset += 2;
15266 }
15267 else
15268 dwarf2_complex_location_expr_complaint ();
15269
15270 if (!fnp->fcontext)
15271 {
15272 /* If there is no `this' field and no DW_AT_containing_type,
15273 we cannot actually find a base class context for the
15274 vtable! */
15275 if (this_type->num_fields () == 0
15276 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15277 {
15278 complaint (_("cannot determine context for virtual member "
15279 "function \"%s\" (offset %s)"),
15280 fieldname, sect_offset_str (die->sect_off));
15281 }
15282 else
15283 {
15284 fnp->fcontext
15285 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15286 }
15287 }
15288 }
15289 else if (attr->form_is_section_offset ())
15290 {
15291 dwarf2_complex_location_expr_complaint ();
15292 }
15293 else
15294 {
15295 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15296 fieldname);
15297 }
15298 }
15299 else
15300 {
15301 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15302 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15303 {
15304 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15305 complaint (_("Member function \"%s\" (offset %s) is virtual "
15306 "but the vtable offset is not specified"),
15307 fieldname, sect_offset_str (die->sect_off));
15308 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15309 TYPE_CPLUS_DYNAMIC (type) = 1;
15310 }
15311 }
15312 }
15313
15314 /* Create the vector of member function fields, and attach it to the type. */
15315
15316 static void
15317 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15318 struct dwarf2_cu *cu)
15319 {
15320 if (cu->per_cu->lang == language_ada)
15321 error (_("unexpected member functions in Ada type"));
15322
15323 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15324 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15325 TYPE_ALLOC (type,
15326 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15327
15328 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15329 {
15330 struct fnfieldlist &nf = fip->fnfieldlists[i];
15331 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15332
15333 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15334 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15335 fn_flp->fn_fields = (struct fn_field *)
15336 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15337
15338 for (int k = 0; k < nf.fnfields.size (); ++k)
15339 fn_flp->fn_fields[k] = nf.fnfields[k];
15340 }
15341
15342 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15343 }
15344
15345 /* Returns non-zero if NAME is the name of a vtable member in CU's
15346 language, zero otherwise. */
15347 static int
15348 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15349 {
15350 static const char vptr[] = "_vptr";
15351
15352 /* Look for the C++ form of the vtable. */
15353 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15354 return 1;
15355
15356 return 0;
15357 }
15358
15359 /* GCC outputs unnamed structures that are really pointers to member
15360 functions, with the ABI-specified layout. If TYPE describes
15361 such a structure, smash it into a member function type.
15362
15363 GCC shouldn't do this; it should just output pointer to member DIEs.
15364 This is GCC PR debug/28767. */
15365
15366 static void
15367 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15368 {
15369 struct type *pfn_type, *self_type, *new_type;
15370
15371 /* Check for a structure with no name and two children. */
15372 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15373 return;
15374
15375 /* Check for __pfn and __delta members. */
15376 if (type->field (0).name () == NULL
15377 || strcmp (type->field (0).name (), "__pfn") != 0
15378 || type->field (1).name () == NULL
15379 || strcmp (type->field (1).name (), "__delta") != 0)
15380 return;
15381
15382 /* Find the type of the method. */
15383 pfn_type = type->field (0).type ();
15384 if (pfn_type == NULL
15385 || pfn_type->code () != TYPE_CODE_PTR
15386 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15387 return;
15388
15389 /* Look for the "this" argument. */
15390 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15391 if (pfn_type->num_fields () == 0
15392 /* || pfn_type->field (0).type () == NULL */
15393 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15394 return;
15395
15396 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15397 new_type = alloc_type (objfile);
15398 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15399 pfn_type->fields (), pfn_type->num_fields (),
15400 pfn_type->has_varargs ());
15401 smash_to_methodptr_type (type, new_type);
15402 }
15403
15404 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15405 requires rewriting, then copy it and return the updated copy.
15406 Otherwise return nullptr. */
15407
15408 static struct type *
15409 rewrite_array_type (struct type *type)
15410 {
15411 if (type->code () != TYPE_CODE_ARRAY)
15412 return nullptr;
15413
15414 struct type *index_type = type->index_type ();
15415 range_bounds *current_bounds = index_type->bounds ();
15416
15417 /* Handle multi-dimensional arrays. */
15418 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15419 if (new_target == nullptr)
15420 {
15421 /* Maybe we don't need to rewrite this array. */
15422 if (current_bounds->low.kind () == PROP_CONST
15423 && current_bounds->high.kind () == PROP_CONST)
15424 return nullptr;
15425 }
15426
15427 /* Either the target type was rewritten, or the bounds have to be
15428 updated. Either way we want to copy the type and update
15429 everything. */
15430 struct type *copy = copy_type (type);
15431 int nfields = copy->num_fields ();
15432 field *new_fields
15433 = ((struct field *) TYPE_ZALLOC (copy,
15434 nfields * sizeof (struct field)));
15435 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15436 copy->set_fields (new_fields);
15437 if (new_target != nullptr)
15438 TYPE_TARGET_TYPE (copy) = new_target;
15439
15440 struct type *index_copy = copy_type (index_type);
15441 range_bounds *bounds
15442 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15443 sizeof (range_bounds));
15444 *bounds = *current_bounds;
15445 bounds->low.set_const_val (1);
15446 bounds->high.set_const_val (0);
15447 index_copy->set_bounds (bounds);
15448 copy->set_index_type (index_copy);
15449
15450 return copy;
15451 }
15452
15453 /* While some versions of GCC will generate complicated DWARF for an
15454 array (see quirk_ada_thick_pointer), more recent versions were
15455 modified to emit an explicit thick pointer structure. However, in
15456 this case, the array still has DWARF expressions for its ranges,
15457 and these must be ignored. */
15458
15459 static void
15460 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15461 struct type *type)
15462 {
15463 gdb_assert (cu->per_cu->lang == language_ada);
15464
15465 /* Check for a structure with two children. */
15466 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15467 return;
15468
15469 /* Check for P_ARRAY and P_BOUNDS members. */
15470 if (type->field (0).name () == NULL
15471 || strcmp (type->field (0).name (), "P_ARRAY") != 0
15472 || type->field (1).name () == NULL
15473 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
15474 return;
15475
15476 /* Make sure we're looking at a pointer to an array. */
15477 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15478 return;
15479
15480 /* The Ada code already knows how to handle these types, so all that
15481 we need to do is turn the bounds into static bounds. However, we
15482 don't want to rewrite existing array or index types in-place,
15483 because those may be referenced in other contexts where this
15484 rewriting is undesirable. */
15485 struct type *new_ary_type
15486 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15487 if (new_ary_type != nullptr)
15488 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15489 }
15490
15491 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15492 appropriate error checking and issuing complaints if there is a
15493 problem. */
15494
15495 static ULONGEST
15496 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15497 {
15498 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15499
15500 if (attr == nullptr)
15501 return 0;
15502
15503 if (!attr->form_is_constant ())
15504 {
15505 complaint (_("DW_AT_alignment must have constant form"
15506 " - DIE at %s [in module %s]"),
15507 sect_offset_str (die->sect_off),
15508 objfile_name (cu->per_objfile->objfile));
15509 return 0;
15510 }
15511
15512 LONGEST val = attr->constant_value (0);
15513 if (val < 0)
15514 {
15515 complaint (_("DW_AT_alignment value must not be negative"
15516 " - DIE at %s [in module %s]"),
15517 sect_offset_str (die->sect_off),
15518 objfile_name (cu->per_objfile->objfile));
15519 return 0;
15520 }
15521 ULONGEST align = val;
15522
15523 if (align == 0)
15524 {
15525 complaint (_("DW_AT_alignment value must not be zero"
15526 " - DIE at %s [in module %s]"),
15527 sect_offset_str (die->sect_off),
15528 objfile_name (cu->per_objfile->objfile));
15529 return 0;
15530 }
15531 if ((align & (align - 1)) != 0)
15532 {
15533 complaint (_("DW_AT_alignment value must be a power of 2"
15534 " - DIE at %s [in module %s]"),
15535 sect_offset_str (die->sect_off),
15536 objfile_name (cu->per_objfile->objfile));
15537 return 0;
15538 }
15539
15540 return align;
15541 }
15542
15543 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15544 the alignment for TYPE. */
15545
15546 static void
15547 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15548 struct type *type)
15549 {
15550 if (!set_type_align (type, get_alignment (cu, die)))
15551 complaint (_("DW_AT_alignment value too large"
15552 " - DIE at %s [in module %s]"),
15553 sect_offset_str (die->sect_off),
15554 objfile_name (cu->per_objfile->objfile));
15555 }
15556
15557 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15558 constant for a type, according to DWARF5 spec, Table 5.5. */
15559
15560 static bool
15561 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15562 {
15563 switch (value)
15564 {
15565 case DW_CC_normal:
15566 case DW_CC_pass_by_reference:
15567 case DW_CC_pass_by_value:
15568 return true;
15569
15570 default:
15571 complaint (_("unrecognized DW_AT_calling_convention value "
15572 "(%s) for a type"), pulongest (value));
15573 return false;
15574 }
15575 }
15576
15577 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15578 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15579 also according to GNU-specific values (see include/dwarf2.h). */
15580
15581 static bool
15582 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15583 {
15584 switch (value)
15585 {
15586 case DW_CC_normal:
15587 case DW_CC_program:
15588 case DW_CC_nocall:
15589 return true;
15590
15591 case DW_CC_GNU_renesas_sh:
15592 case DW_CC_GNU_borland_fastcall_i386:
15593 case DW_CC_GDB_IBM_OpenCL:
15594 return true;
15595
15596 default:
15597 complaint (_("unrecognized DW_AT_calling_convention value "
15598 "(%s) for a subroutine"), pulongest (value));
15599 return false;
15600 }
15601 }
15602
15603 /* Called when we find the DIE that starts a structure or union scope
15604 (definition) to create a type for the structure or union. Fill in
15605 the type's name and general properties; the members will not be
15606 processed until process_structure_scope. A symbol table entry for
15607 the type will also not be done until process_structure_scope (assuming
15608 the type has a name).
15609
15610 NOTE: we need to call these functions regardless of whether or not the
15611 DIE has a DW_AT_name attribute, since it might be an anonymous
15612 structure or union. This gets the type entered into our set of
15613 user defined types. */
15614
15615 static struct type *
15616 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15617 {
15618 struct objfile *objfile = cu->per_objfile->objfile;
15619 struct type *type;
15620 struct attribute *attr;
15621 const char *name;
15622
15623 /* If the definition of this type lives in .debug_types, read that type.
15624 Don't follow DW_AT_specification though, that will take us back up
15625 the chain and we want to go down. */
15626 attr = die->attr (DW_AT_signature);
15627 if (attr != nullptr)
15628 {
15629 type = get_DW_AT_signature_type (die, attr, cu);
15630
15631 /* The type's CU may not be the same as CU.
15632 Ensure TYPE is recorded with CU in die_type_hash. */
15633 return set_die_type (die, type, cu);
15634 }
15635
15636 type = alloc_type (objfile);
15637 INIT_CPLUS_SPECIFIC (type);
15638
15639 name = dwarf2_name (die, cu);
15640 if (name != NULL)
15641 {
15642 if (cu->per_cu->lang == language_cplus
15643 || cu->per_cu->lang == language_d
15644 || cu->per_cu->lang == language_rust)
15645 {
15646 const char *full_name = dwarf2_full_name (name, die, cu);
15647
15648 /* dwarf2_full_name might have already finished building the DIE's
15649 type. If so, there is no need to continue. */
15650 if (get_die_type (die, cu) != NULL)
15651 return get_die_type (die, cu);
15652
15653 type->set_name (full_name);
15654 }
15655 else
15656 {
15657 /* The name is already allocated along with this objfile, so
15658 we don't need to duplicate it for the type. */
15659 type->set_name (name);
15660 }
15661 }
15662
15663 if (die->tag == DW_TAG_structure_type)
15664 {
15665 type->set_code (TYPE_CODE_STRUCT);
15666 }
15667 else if (die->tag == DW_TAG_union_type)
15668 {
15669 type->set_code (TYPE_CODE_UNION);
15670 }
15671 else if (die->tag == DW_TAG_namelist)
15672 {
15673 type->set_code (TYPE_CODE_NAMELIST);
15674 }
15675 else
15676 {
15677 type->set_code (TYPE_CODE_STRUCT);
15678 }
15679
15680 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
15681 type->set_is_declared_class (true);
15682
15683 /* Store the calling convention in the type if it's available in
15684 the die. Otherwise the calling convention remains set to
15685 the default value DW_CC_normal. */
15686 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15687 if (attr != nullptr
15688 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15689 {
15690 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15691 TYPE_CPLUS_CALLING_CONVENTION (type)
15692 = (enum dwarf_calling_convention) (attr->constant_value (0));
15693 }
15694
15695 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15696 if (attr != nullptr)
15697 {
15698 if (attr->form_is_constant ())
15699 TYPE_LENGTH (type) = attr->constant_value (0);
15700 else
15701 {
15702 struct dynamic_prop prop;
15703 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15704 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15705 TYPE_LENGTH (type) = 0;
15706 }
15707 }
15708 else
15709 {
15710 TYPE_LENGTH (type) = 0;
15711 }
15712
15713 maybe_set_alignment (cu, die, type);
15714
15715 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15716 {
15717 /* ICC<14 does not output the required DW_AT_declaration on
15718 incomplete types, but gives them a size of zero. */
15719 type->set_is_stub (true);
15720 }
15721 else
15722 type->set_stub_is_supported (true);
15723
15724 if (die_is_declaration (die, cu))
15725 type->set_is_stub (true);
15726 else if (attr == NULL && die->child == NULL
15727 && producer_is_realview (cu->producer))
15728 /* RealView does not output the required DW_AT_declaration
15729 on incomplete types. */
15730 type->set_is_stub (true);
15731
15732 /* We need to add the type field to the die immediately so we don't
15733 infinitely recurse when dealing with pointers to the structure
15734 type within the structure itself. */
15735 set_die_type (die, type, cu);
15736
15737 /* set_die_type should be already done. */
15738 set_descriptive_type (type, die, cu);
15739
15740 return type;
15741 }
15742
15743 static void handle_struct_member_die
15744 (struct die_info *child_die,
15745 struct type *type,
15746 struct field_info *fi,
15747 std::vector<struct symbol *> *template_args,
15748 struct dwarf2_cu *cu);
15749
15750 /* A helper for handle_struct_member_die that handles
15751 DW_TAG_variant_part. */
15752
15753 static void
15754 handle_variant_part (struct die_info *die, struct type *type,
15755 struct field_info *fi,
15756 std::vector<struct symbol *> *template_args,
15757 struct dwarf2_cu *cu)
15758 {
15759 variant_part_builder *new_part;
15760 if (fi->current_variant_part == nullptr)
15761 {
15762 fi->variant_parts.emplace_back ();
15763 new_part = &fi->variant_parts.back ();
15764 }
15765 else if (!fi->current_variant_part->processing_variant)
15766 {
15767 complaint (_("nested DW_TAG_variant_part seen "
15768 "- DIE at %s [in module %s]"),
15769 sect_offset_str (die->sect_off),
15770 objfile_name (cu->per_objfile->objfile));
15771 return;
15772 }
15773 else
15774 {
15775 variant_field &current = fi->current_variant_part->variants.back ();
15776 current.variant_parts.emplace_back ();
15777 new_part = &current.variant_parts.back ();
15778 }
15779
15780 /* When we recurse, we want callees to add to this new variant
15781 part. */
15782 scoped_restore save_current_variant_part
15783 = make_scoped_restore (&fi->current_variant_part, new_part);
15784
15785 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15786 if (discr == NULL)
15787 {
15788 /* It's a univariant form, an extension we support. */
15789 }
15790 else if (discr->form_is_ref ())
15791 {
15792 struct dwarf2_cu *target_cu = cu;
15793 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15794
15795 new_part->discriminant_offset = target_die->sect_off;
15796 }
15797 else
15798 {
15799 complaint (_("DW_AT_discr does not have DIE reference form"
15800 " - DIE at %s [in module %s]"),
15801 sect_offset_str (die->sect_off),
15802 objfile_name (cu->per_objfile->objfile));
15803 }
15804
15805 for (die_info *child_die = die->child;
15806 child_die != NULL;
15807 child_die = child_die->sibling)
15808 handle_struct_member_die (child_die, type, fi, template_args, cu);
15809 }
15810
15811 /* A helper for handle_struct_member_die that handles
15812 DW_TAG_variant. */
15813
15814 static void
15815 handle_variant (struct die_info *die, struct type *type,
15816 struct field_info *fi,
15817 std::vector<struct symbol *> *template_args,
15818 struct dwarf2_cu *cu)
15819 {
15820 if (fi->current_variant_part == nullptr)
15821 {
15822 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15823 "- DIE at %s [in module %s]"),
15824 sect_offset_str (die->sect_off),
15825 objfile_name (cu->per_objfile->objfile));
15826 return;
15827 }
15828 if (fi->current_variant_part->processing_variant)
15829 {
15830 complaint (_("nested DW_TAG_variant seen "
15831 "- DIE at %s [in module %s]"),
15832 sect_offset_str (die->sect_off),
15833 objfile_name (cu->per_objfile->objfile));
15834 return;
15835 }
15836
15837 scoped_restore save_processing_variant
15838 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15839 true);
15840
15841 fi->current_variant_part->variants.emplace_back ();
15842 variant_field &variant = fi->current_variant_part->variants.back ();
15843 variant.first_field = fi->fields.size ();
15844
15845 /* In a variant we want to get the discriminant and also add a
15846 field for our sole member child. */
15847 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15848 if (discr == nullptr || !discr->form_is_constant ())
15849 {
15850 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15851 if (discr == nullptr || discr->as_block ()->size == 0)
15852 variant.default_branch = true;
15853 else
15854 variant.discr_list_data = discr->as_block ();
15855 }
15856 else
15857 variant.discriminant_value = discr->constant_value (0);
15858
15859 for (die_info *variant_child = die->child;
15860 variant_child != NULL;
15861 variant_child = variant_child->sibling)
15862 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15863
15864 variant.last_field = fi->fields.size ();
15865 }
15866
15867 /* A helper for process_structure_scope that handles a single member
15868 DIE. */
15869
15870 static void
15871 handle_struct_member_die (struct die_info *child_die, struct type *type,
15872 struct field_info *fi,
15873 std::vector<struct symbol *> *template_args,
15874 struct dwarf2_cu *cu)
15875 {
15876 if (child_die->tag == DW_TAG_member
15877 || child_die->tag == DW_TAG_variable
15878 || child_die->tag == DW_TAG_namelist_item)
15879 {
15880 /* NOTE: carlton/2002-11-05: A C++ static data member
15881 should be a DW_TAG_member that is a declaration, but
15882 all versions of G++ as of this writing (so through at
15883 least 3.2.1) incorrectly generate DW_TAG_variable
15884 tags for them instead. */
15885 dwarf2_add_field (fi, child_die, cu);
15886 }
15887 else if (child_die->tag == DW_TAG_subprogram)
15888 {
15889 /* Rust doesn't have member functions in the C++ sense.
15890 However, it does emit ordinary functions as children
15891 of a struct DIE. */
15892 if (cu->per_cu->lang == language_rust)
15893 read_func_scope (child_die, cu);
15894 else
15895 {
15896 /* C++ member function. */
15897 dwarf2_add_member_fn (fi, child_die, type, cu);
15898 }
15899 }
15900 else if (child_die->tag == DW_TAG_inheritance)
15901 {
15902 /* C++ base class field. */
15903 dwarf2_add_field (fi, child_die, cu);
15904 }
15905 else if (type_can_define_types (child_die))
15906 dwarf2_add_type_defn (fi, child_die, cu);
15907 else if (child_die->tag == DW_TAG_template_type_param
15908 || child_die->tag == DW_TAG_template_value_param)
15909 {
15910 struct symbol *arg = new_symbol (child_die, NULL, cu);
15911
15912 if (arg != NULL)
15913 template_args->push_back (arg);
15914 }
15915 else if (child_die->tag == DW_TAG_variant_part)
15916 handle_variant_part (child_die, type, fi, template_args, cu);
15917 else if (child_die->tag == DW_TAG_variant)
15918 handle_variant (child_die, type, fi, template_args, cu);
15919 }
15920
15921 /* Finish creating a structure or union type, including filling in its
15922 members and creating a symbol for it. This function also handles Fortran
15923 namelist variables, their items or members and creating a symbol for
15924 them. */
15925
15926 static void
15927 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15928 {
15929 struct objfile *objfile = cu->per_objfile->objfile;
15930 struct die_info *child_die;
15931 struct type *type;
15932
15933 type = get_die_type (die, cu);
15934 if (type == NULL)
15935 type = read_structure_type (die, cu);
15936
15937 bool has_template_parameters = false;
15938 if (die->child != NULL && ! die_is_declaration (die, cu))
15939 {
15940 struct field_info fi;
15941 std::vector<struct symbol *> template_args;
15942
15943 child_die = die->child;
15944
15945 while (child_die && child_die->tag)
15946 {
15947 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15948 child_die = child_die->sibling;
15949 }
15950
15951 /* Attach template arguments to type. */
15952 if (!template_args.empty ())
15953 {
15954 has_template_parameters = true;
15955 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15956 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15957 TYPE_TEMPLATE_ARGUMENTS (type)
15958 = XOBNEWVEC (&objfile->objfile_obstack,
15959 struct symbol *,
15960 TYPE_N_TEMPLATE_ARGUMENTS (type));
15961 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15962 template_args.data (),
15963 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15964 * sizeof (struct symbol *)));
15965 }
15966
15967 /* Attach fields and member functions to the type. */
15968 if (fi.nfields () > 0)
15969 dwarf2_attach_fields_to_type (&fi, type, cu);
15970 if (!fi.fnfieldlists.empty ())
15971 {
15972 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15973
15974 /* Get the type which refers to the base class (possibly this
15975 class itself) which contains the vtable pointer for the current
15976 class from the DW_AT_containing_type attribute. This use of
15977 DW_AT_containing_type is a GNU extension. */
15978
15979 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15980 {
15981 struct type *t = die_containing_type (die, cu);
15982
15983 set_type_vptr_basetype (type, t);
15984 if (type == t)
15985 {
15986 int i;
15987
15988 /* Our own class provides vtbl ptr. */
15989 for (i = t->num_fields () - 1;
15990 i >= TYPE_N_BASECLASSES (t);
15991 --i)
15992 {
15993 const char *fieldname = t->field (i).name ();
15994
15995 if (is_vtable_name (fieldname, cu))
15996 {
15997 set_type_vptr_fieldno (type, i);
15998 break;
15999 }
16000 }
16001
16002 /* Complain if virtual function table field not found. */
16003 if (i < TYPE_N_BASECLASSES (t))
16004 complaint (_("virtual function table pointer "
16005 "not found when defining class '%s'"),
16006 type->name () ? type->name () : "");
16007 }
16008 else
16009 {
16010 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16011 }
16012 }
16013 else if (cu->producer
16014 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16015 {
16016 /* The IBM XLC compiler does not provide direct indication
16017 of the containing type, but the vtable pointer is
16018 always named __vfp. */
16019
16020 int i;
16021
16022 for (i = type->num_fields () - 1;
16023 i >= TYPE_N_BASECLASSES (type);
16024 --i)
16025 {
16026 if (strcmp (type->field (i).name (), "__vfp") == 0)
16027 {
16028 set_type_vptr_fieldno (type, i);
16029 set_type_vptr_basetype (type, type);
16030 break;
16031 }
16032 }
16033 }
16034 }
16035
16036 /* Copy fi.typedef_field_list linked list elements content into the
16037 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16038 if (!fi.typedef_field_list.empty ())
16039 {
16040 int count = fi.typedef_field_list.size ();
16041
16042 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16043 TYPE_TYPEDEF_FIELD_ARRAY (type)
16044 = ((struct decl_field *)
16045 TYPE_ALLOC (type,
16046 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16047 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16048
16049 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16050 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16051 }
16052
16053 /* Copy fi.nested_types_list linked list elements content into the
16054 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16055 if (!fi.nested_types_list.empty ()
16056 && cu->per_cu->lang != language_ada)
16057 {
16058 int count = fi.nested_types_list.size ();
16059
16060 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16061 TYPE_NESTED_TYPES_ARRAY (type)
16062 = ((struct decl_field *)
16063 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16064 TYPE_NESTED_TYPES_COUNT (type) = count;
16065
16066 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16067 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16068 }
16069 }
16070
16071 quirk_gcc_member_function_pointer (type, objfile);
16072 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
16073 cu->rust_unions.push_back (type);
16074 else if (cu->per_cu->lang == language_ada)
16075 quirk_ada_thick_pointer_struct (die, cu, type);
16076
16077 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16078 snapshots) has been known to create a die giving a declaration
16079 for a class that has, as a child, a die giving a definition for a
16080 nested class. So we have to process our children even if the
16081 current die is a declaration. Normally, of course, a declaration
16082 won't have any children at all. */
16083
16084 child_die = die->child;
16085
16086 while (child_die != NULL && child_die->tag)
16087 {
16088 if (child_die->tag == DW_TAG_member
16089 || child_die->tag == DW_TAG_variable
16090 || child_die->tag == DW_TAG_inheritance
16091 || child_die->tag == DW_TAG_template_value_param
16092 || child_die->tag == DW_TAG_template_type_param)
16093 {
16094 /* Do nothing. */
16095 }
16096 else
16097 process_die (child_die, cu);
16098
16099 child_die = child_die->sibling;
16100 }
16101
16102 /* Do not consider external references. According to the DWARF standard,
16103 these DIEs are identified by the fact that they have no byte_size
16104 attribute, and a declaration attribute. */
16105 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16106 || !die_is_declaration (die, cu)
16107 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16108 {
16109 struct symbol *sym = new_symbol (die, type, cu);
16110
16111 if (has_template_parameters)
16112 {
16113 struct symtab *symtab;
16114 if (sym != nullptr)
16115 symtab = symbol_symtab (sym);
16116 else if (cu->line_header != nullptr)
16117 {
16118 /* Any related symtab will do. */
16119 symtab
16120 = cu->line_header->file_names ()[0].symtab;
16121 }
16122 else
16123 {
16124 symtab = nullptr;
16125 complaint (_("could not find suitable "
16126 "symtab for template parameter"
16127 " - DIE at %s [in module %s]"),
16128 sect_offset_str (die->sect_off),
16129 objfile_name (objfile));
16130 }
16131
16132 if (symtab != nullptr)
16133 {
16134 /* Make sure that the symtab is set on the new symbols.
16135 Even though they don't appear in this symtab directly,
16136 other parts of gdb assume that symbols do, and this is
16137 reasonably true. */
16138 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16139 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16140 }
16141 }
16142 }
16143 }
16144
16145 /* Assuming DIE is an enumeration type, and TYPE is its associated
16146 type, update TYPE using some information only available in DIE's
16147 children. In particular, the fields are computed. */
16148
16149 static void
16150 update_enumeration_type_from_children (struct die_info *die,
16151 struct type *type,
16152 struct dwarf2_cu *cu)
16153 {
16154 struct die_info *child_die;
16155 int unsigned_enum = 1;
16156 int flag_enum = 1;
16157
16158 auto_obstack obstack;
16159 std::vector<struct field> fields;
16160
16161 for (child_die = die->child;
16162 child_die != NULL && child_die->tag;
16163 child_die = child_die->sibling)
16164 {
16165 struct attribute *attr;
16166 LONGEST value;
16167 const gdb_byte *bytes;
16168 struct dwarf2_locexpr_baton *baton;
16169 const char *name;
16170
16171 if (child_die->tag != DW_TAG_enumerator)
16172 continue;
16173
16174 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16175 if (attr == NULL)
16176 continue;
16177
16178 name = dwarf2_name (child_die, cu);
16179 if (name == NULL)
16180 name = "<anonymous enumerator>";
16181
16182 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16183 &value, &bytes, &baton);
16184 if (value < 0)
16185 {
16186 unsigned_enum = 0;
16187 flag_enum = 0;
16188 }
16189 else
16190 {
16191 if (count_one_bits_ll (value) >= 2)
16192 flag_enum = 0;
16193 }
16194
16195 fields.emplace_back ();
16196 struct field &field = fields.back ();
16197 field.set_name (dwarf2_physname (name, child_die, cu));
16198 field.set_loc_enumval (value);
16199 }
16200
16201 if (!fields.empty ())
16202 {
16203 type->set_num_fields (fields.size ());
16204 type->set_fields
16205 ((struct field *)
16206 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16207 memcpy (type->fields (), fields.data (),
16208 sizeof (struct field) * fields.size ());
16209 }
16210
16211 if (unsigned_enum)
16212 type->set_is_unsigned (true);
16213
16214 if (flag_enum)
16215 type->set_is_flag_enum (true);
16216 }
16217
16218 /* Given a DW_AT_enumeration_type die, set its type. We do not
16219 complete the type's fields yet, or create any symbols. */
16220
16221 static struct type *
16222 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16223 {
16224 struct objfile *objfile = cu->per_objfile->objfile;
16225 struct type *type;
16226 struct attribute *attr;
16227 const char *name;
16228
16229 /* If the definition of this type lives in .debug_types, read that type.
16230 Don't follow DW_AT_specification though, that will take us back up
16231 the chain and we want to go down. */
16232 attr = die->attr (DW_AT_signature);
16233 if (attr != nullptr)
16234 {
16235 type = get_DW_AT_signature_type (die, attr, cu);
16236
16237 /* The type's CU may not be the same as CU.
16238 Ensure TYPE is recorded with CU in die_type_hash. */
16239 return set_die_type (die, type, cu);
16240 }
16241
16242 type = alloc_type (objfile);
16243
16244 type->set_code (TYPE_CODE_ENUM);
16245 name = dwarf2_full_name (NULL, die, cu);
16246 if (name != NULL)
16247 type->set_name (name);
16248
16249 attr = dwarf2_attr (die, DW_AT_type, cu);
16250 if (attr != NULL)
16251 {
16252 struct type *underlying_type = die_type (die, cu);
16253
16254 TYPE_TARGET_TYPE (type) = underlying_type;
16255 }
16256
16257 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16258 if (attr != nullptr)
16259 {
16260 TYPE_LENGTH (type) = attr->constant_value (0);
16261 }
16262 else
16263 {
16264 TYPE_LENGTH (type) = 0;
16265 }
16266
16267 maybe_set_alignment (cu, die, type);
16268
16269 /* The enumeration DIE can be incomplete. In Ada, any type can be
16270 declared as private in the package spec, and then defined only
16271 inside the package body. Such types are known as Taft Amendment
16272 Types. When another package uses such a type, an incomplete DIE
16273 may be generated by the compiler. */
16274 if (die_is_declaration (die, cu))
16275 type->set_is_stub (true);
16276
16277 /* If this type has an underlying type that is not a stub, then we
16278 may use its attributes. We always use the "unsigned" attribute
16279 in this situation, because ordinarily we guess whether the type
16280 is unsigned -- but the guess can be wrong and the underlying type
16281 can tell us the reality. However, we defer to a local size
16282 attribute if one exists, because this lets the compiler override
16283 the underlying type if needed. */
16284 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16285 {
16286 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16287 underlying_type = check_typedef (underlying_type);
16288
16289 type->set_is_unsigned (underlying_type->is_unsigned ());
16290
16291 if (TYPE_LENGTH (type) == 0)
16292 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16293
16294 if (TYPE_RAW_ALIGN (type) == 0
16295 && TYPE_RAW_ALIGN (underlying_type) != 0)
16296 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16297 }
16298
16299 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16300
16301 set_die_type (die, type, cu);
16302
16303 /* Finish the creation of this type by using the enum's children.
16304 Note that, as usual, this must come after set_die_type to avoid
16305 infinite recursion when trying to compute the names of the
16306 enumerators. */
16307 update_enumeration_type_from_children (die, type, cu);
16308
16309 return type;
16310 }
16311
16312 /* Given a pointer to a die which begins an enumeration, process all
16313 the dies that define the members of the enumeration, and create the
16314 symbol for the enumeration type.
16315
16316 NOTE: We reverse the order of the element list. */
16317
16318 static void
16319 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16320 {
16321 struct type *this_type;
16322
16323 this_type = get_die_type (die, cu);
16324 if (this_type == NULL)
16325 this_type = read_enumeration_type (die, cu);
16326
16327 if (die->child != NULL)
16328 {
16329 struct die_info *child_die;
16330 const char *name;
16331
16332 child_die = die->child;
16333 while (child_die && child_die->tag)
16334 {
16335 if (child_die->tag != DW_TAG_enumerator)
16336 {
16337 process_die (child_die, cu);
16338 }
16339 else
16340 {
16341 name = dwarf2_name (child_die, cu);
16342 if (name)
16343 new_symbol (child_die, this_type, cu);
16344 }
16345
16346 child_die = child_die->sibling;
16347 }
16348 }
16349
16350 /* If we are reading an enum from a .debug_types unit, and the enum
16351 is a declaration, and the enum is not the signatured type in the
16352 unit, then we do not want to add a symbol for it. Adding a
16353 symbol would in some cases obscure the true definition of the
16354 enum, giving users an incomplete type when the definition is
16355 actually available. Note that we do not want to do this for all
16356 enums which are just declarations, because C++0x allows forward
16357 enum declarations. */
16358 if (cu->per_cu->is_debug_types
16359 && die_is_declaration (die, cu))
16360 {
16361 struct signatured_type *sig_type;
16362
16363 sig_type = (struct signatured_type *) cu->per_cu;
16364 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16365 if (sig_type->type_offset_in_section != die->sect_off)
16366 return;
16367 }
16368
16369 new_symbol (die, this_type, cu);
16370 }
16371
16372 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16373 expression for an index type and finds the corresponding field
16374 offset in the hidden "P_BOUNDS" structure. Returns true on success
16375 and updates *FIELD, false if it fails to recognize an
16376 expression. */
16377
16378 static bool
16379 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16380 int *bounds_offset, struct field *field,
16381 struct dwarf2_cu *cu)
16382 {
16383 struct attribute *attr = dwarf2_attr (die, name, cu);
16384 if (attr == nullptr || !attr->form_is_block ())
16385 return false;
16386
16387 const struct dwarf_block *block = attr->as_block ();
16388 const gdb_byte *start = block->data;
16389 const gdb_byte *end = block->data + block->size;
16390
16391 /* The expression to recognize generally looks like:
16392
16393 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16394 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16395
16396 However, the second "plus_uconst" may be missing:
16397
16398 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16399 DW_OP_deref_size: 4)
16400
16401 This happens when the field is at the start of the structure.
16402
16403 Also, the final deref may not be sized:
16404
16405 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16406 DW_OP_deref)
16407
16408 This happens when the size of the index type happens to be the
16409 same as the architecture's word size. This can occur with or
16410 without the second plus_uconst. */
16411
16412 if (end - start < 2)
16413 return false;
16414 if (*start++ != DW_OP_push_object_address)
16415 return false;
16416 if (*start++ != DW_OP_plus_uconst)
16417 return false;
16418
16419 uint64_t this_bound_off;
16420 start = gdb_read_uleb128 (start, end, &this_bound_off);
16421 if (start == nullptr || (int) this_bound_off != this_bound_off)
16422 return false;
16423 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16424 is consistent among all bounds. */
16425 if (*bounds_offset == -1)
16426 *bounds_offset = this_bound_off;
16427 else if (*bounds_offset != this_bound_off)
16428 return false;
16429
16430 if (start == end || *start++ != DW_OP_deref)
16431 return false;
16432
16433 int offset = 0;
16434 if (start ==end)
16435 return false;
16436 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16437 {
16438 /* This means an offset of 0. */
16439 }
16440 else if (*start++ != DW_OP_plus_uconst)
16441 return false;
16442 else
16443 {
16444 /* The size is the parameter to DW_OP_plus_uconst. */
16445 uint64_t val;
16446 start = gdb_read_uleb128 (start, end, &val);
16447 if (start == nullptr)
16448 return false;
16449 if ((int) val != val)
16450 return false;
16451 offset = val;
16452 }
16453
16454 if (start == end)
16455 return false;
16456
16457 uint64_t size;
16458 if (*start == DW_OP_deref_size)
16459 {
16460 start = gdb_read_uleb128 (start + 1, end, &size);
16461 if (start == nullptr)
16462 return false;
16463 }
16464 else if (*start == DW_OP_deref)
16465 {
16466 size = cu->header.addr_size;
16467 ++start;
16468 }
16469 else
16470 return false;
16471
16472 field->set_loc_bitpos (8 * offset);
16473 if (size != TYPE_LENGTH (field->type ()))
16474 FIELD_BITSIZE (*field) = 8 * size;
16475
16476 return true;
16477 }
16478
16479 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16480 some kinds of Ada arrays:
16481
16482 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16483 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16484 <11e0> DW_AT_data_location: 2 byte block: 97 6
16485 (DW_OP_push_object_address; DW_OP_deref)
16486 <11e3> DW_AT_type : <0x1173>
16487 <11e7> DW_AT_sibling : <0x1201>
16488 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16489 <11ec> DW_AT_type : <0x1206>
16490 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16491 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16492 DW_OP_deref_size: 4)
16493 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16494 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16495 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16496
16497 This actually represents a "thick pointer", which is a structure
16498 with two elements: one that is a pointer to the array data, and one
16499 that is a pointer to another structure; this second structure holds
16500 the array bounds.
16501
16502 This returns a new type on success, or nullptr if this didn't
16503 recognize the type. */
16504
16505 static struct type *
16506 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16507 struct type *type)
16508 {
16509 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16510 /* So far we've only seen this with block form. */
16511 if (attr == nullptr || !attr->form_is_block ())
16512 return nullptr;
16513
16514 /* Note that this will fail if the structure layout is changed by
16515 the compiler. However, we have no good way to recognize some
16516 other layout, because we don't know what expression the compiler
16517 might choose to emit should this happen. */
16518 struct dwarf_block *blk = attr->as_block ();
16519 if (blk->size != 2
16520 || blk->data[0] != DW_OP_push_object_address
16521 || blk->data[1] != DW_OP_deref)
16522 return nullptr;
16523
16524 int bounds_offset = -1;
16525 int max_align = -1;
16526 std::vector<struct field> range_fields;
16527 for (struct die_info *child_die = die->child;
16528 child_die;
16529 child_die = child_die->sibling)
16530 {
16531 if (child_die->tag == DW_TAG_subrange_type)
16532 {
16533 struct type *underlying = read_subrange_index_type (child_die, cu);
16534
16535 int this_align = type_align (underlying);
16536 if (this_align > max_align)
16537 max_align = this_align;
16538
16539 range_fields.emplace_back ();
16540 range_fields.emplace_back ();
16541
16542 struct field &lower = range_fields[range_fields.size () - 2];
16543 struct field &upper = range_fields[range_fields.size () - 1];
16544
16545 lower.set_type (underlying);
16546 FIELD_ARTIFICIAL (lower) = 1;
16547
16548 upper.set_type (underlying);
16549 FIELD_ARTIFICIAL (upper) = 1;
16550
16551 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16552 &bounds_offset, &lower, cu)
16553 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16554 &bounds_offset, &upper, cu))
16555 return nullptr;
16556 }
16557 }
16558
16559 /* This shouldn't really happen, but double-check that we found
16560 where the bounds are stored. */
16561 if (bounds_offset == -1)
16562 return nullptr;
16563
16564 struct objfile *objfile = cu->per_objfile->objfile;
16565 for (int i = 0; i < range_fields.size (); i += 2)
16566 {
16567 char name[20];
16568
16569 /* Set the name of each field in the bounds. */
16570 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16571 range_fields[i].set_name (objfile->intern (name));
16572 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16573 range_fields[i + 1].set_name (objfile->intern (name));
16574 }
16575
16576 struct type *bounds = alloc_type (objfile);
16577 bounds->set_code (TYPE_CODE_STRUCT);
16578
16579 bounds->set_num_fields (range_fields.size ());
16580 bounds->set_fields
16581 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16582 * sizeof (struct field))));
16583 memcpy (bounds->fields (), range_fields.data (),
16584 bounds->num_fields () * sizeof (struct field));
16585
16586 int last_fieldno = range_fields.size () - 1;
16587 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
16588 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16589 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16590
16591 /* Rewrite the existing array type in place. Specifically, we
16592 remove any dynamic properties we might have read, and we replace
16593 the index types. */
16594 struct type *iter = type;
16595 for (int i = 0; i < range_fields.size (); i += 2)
16596 {
16597 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16598 iter->main_type->dyn_prop_list = nullptr;
16599 iter->set_index_type
16600 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16601 iter = TYPE_TARGET_TYPE (iter);
16602 }
16603
16604 struct type *result = alloc_type (objfile);
16605 result->set_code (TYPE_CODE_STRUCT);
16606
16607 result->set_num_fields (2);
16608 result->set_fields
16609 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16610 * sizeof (struct field))));
16611
16612 /* The names are chosen to coincide with what the compiler does with
16613 -fgnat-encodings=all, which the Ada code in gdb already
16614 understands. */
16615 result->field (0).set_name ("P_ARRAY");
16616 result->field (0).set_type (lookup_pointer_type (type));
16617
16618 result->field (1).set_name ("P_BOUNDS");
16619 result->field (1).set_type (lookup_pointer_type (bounds));
16620 result->field (1).set_loc_bitpos (8 * bounds_offset);
16621
16622 result->set_name (type->name ());
16623 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16624 + TYPE_LENGTH (result->field (1).type ()));
16625
16626 return result;
16627 }
16628
16629 /* Extract all information from a DW_TAG_array_type DIE and put it in
16630 the DIE's type field. For now, this only handles one dimensional
16631 arrays. */
16632
16633 static struct type *
16634 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16635 {
16636 struct objfile *objfile = cu->per_objfile->objfile;
16637 struct die_info *child_die;
16638 struct type *type;
16639 struct type *element_type, *range_type, *index_type;
16640 struct attribute *attr;
16641 const char *name;
16642 struct dynamic_prop *byte_stride_prop = NULL;
16643 unsigned int bit_stride = 0;
16644
16645 element_type = die_type (die, cu);
16646
16647 /* The die_type call above may have already set the type for this DIE. */
16648 type = get_die_type (die, cu);
16649 if (type)
16650 return type;
16651
16652 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16653 if (attr != NULL)
16654 {
16655 int stride_ok;
16656 struct type *prop_type = cu->addr_sized_int_type (false);
16657
16658 byte_stride_prop
16659 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16660 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16661 prop_type);
16662 if (!stride_ok)
16663 {
16664 complaint (_("unable to read array DW_AT_byte_stride "
16665 " - DIE at %s [in module %s]"),
16666 sect_offset_str (die->sect_off),
16667 objfile_name (cu->per_objfile->objfile));
16668 /* Ignore this attribute. We will likely not be able to print
16669 arrays of this type correctly, but there is little we can do
16670 to help if we cannot read the attribute's value. */
16671 byte_stride_prop = NULL;
16672 }
16673 }
16674
16675 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16676 if (attr != NULL)
16677 bit_stride = attr->constant_value (0);
16678
16679 /* Irix 6.2 native cc creates array types without children for
16680 arrays with unspecified length. */
16681 if (die->child == NULL)
16682 {
16683 index_type = objfile_type (objfile)->builtin_int;
16684 range_type = create_static_range_type (NULL, index_type, 0, -1);
16685 type = create_array_type_with_stride (NULL, element_type, range_type,
16686 byte_stride_prop, bit_stride);
16687 return set_die_type (die, type, cu);
16688 }
16689
16690 std::vector<struct type *> range_types;
16691 child_die = die->child;
16692 while (child_die && child_die->tag)
16693 {
16694 if (child_die->tag == DW_TAG_subrange_type
16695 || child_die->tag == DW_TAG_generic_subrange)
16696 {
16697 struct type *child_type = read_type_die (child_die, cu);
16698
16699 if (child_type != NULL)
16700 {
16701 /* The range type was succesfully read. Save it for the
16702 array type creation. */
16703 range_types.push_back (child_type);
16704 }
16705 }
16706 child_die = child_die->sibling;
16707 }
16708
16709 if (range_types.empty ())
16710 {
16711 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16712 sect_offset_str (die->sect_off),
16713 objfile_name (cu->per_objfile->objfile));
16714 return NULL;
16715 }
16716
16717 /* Dwarf2 dimensions are output from left to right, create the
16718 necessary array types in backwards order. */
16719
16720 type = element_type;
16721
16722 if (read_array_order (die, cu) == DW_ORD_col_major)
16723 {
16724 int i = 0;
16725
16726 while (i < range_types.size ())
16727 {
16728 type = create_array_type_with_stride (NULL, type, range_types[i++],
16729 byte_stride_prop, bit_stride);
16730 bit_stride = 0;
16731 byte_stride_prop = nullptr;
16732 }
16733 }
16734 else
16735 {
16736 size_t ndim = range_types.size ();
16737 while (ndim-- > 0)
16738 {
16739 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16740 byte_stride_prop, bit_stride);
16741 bit_stride = 0;
16742 byte_stride_prop = nullptr;
16743 }
16744 }
16745
16746 gdb_assert (type != element_type);
16747
16748 /* Understand Dwarf2 support for vector types (like they occur on
16749 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16750 array type. This is not part of the Dwarf2/3 standard yet, but a
16751 custom vendor extension. The main difference between a regular
16752 array and the vector variant is that vectors are passed by value
16753 to functions. */
16754 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16755 if (attr != nullptr)
16756 make_vector_type (type);
16757
16758 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16759 implementation may choose to implement triple vectors using this
16760 attribute. */
16761 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16762 if (attr != nullptr && attr->form_is_unsigned ())
16763 {
16764 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16765 TYPE_LENGTH (type) = attr->as_unsigned ();
16766 else
16767 complaint (_("DW_AT_byte_size for array type smaller "
16768 "than the total size of elements"));
16769 }
16770
16771 name = dwarf2_name (die, cu);
16772 if (name)
16773 type->set_name (name);
16774
16775 maybe_set_alignment (cu, die, type);
16776
16777 struct type *replacement_type = nullptr;
16778 if (cu->per_cu->lang == language_ada)
16779 {
16780 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16781 if (replacement_type != nullptr)
16782 type = replacement_type;
16783 }
16784
16785 /* Install the type in the die. */
16786 set_die_type (die, type, cu, replacement_type != nullptr);
16787
16788 /* set_die_type should be already done. */
16789 set_descriptive_type (type, die, cu);
16790
16791 return type;
16792 }
16793
16794 static enum dwarf_array_dim_ordering
16795 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16796 {
16797 struct attribute *attr;
16798
16799 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16800
16801 if (attr != nullptr)
16802 {
16803 LONGEST val = attr->constant_value (-1);
16804 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16805 return (enum dwarf_array_dim_ordering) val;
16806 }
16807
16808 /* GNU F77 is a special case, as at 08/2004 array type info is the
16809 opposite order to the dwarf2 specification, but data is still
16810 laid out as per normal fortran.
16811
16812 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16813 version checking. */
16814
16815 if (cu->per_cu->lang == language_fortran
16816 && cu->producer && strstr (cu->producer, "GNU F77"))
16817 {
16818 return DW_ORD_row_major;
16819 }
16820
16821 switch (cu->language_defn->array_ordering ())
16822 {
16823 case array_column_major:
16824 return DW_ORD_col_major;
16825 case array_row_major:
16826 default:
16827 return DW_ORD_row_major;
16828 };
16829 }
16830
16831 /* Extract all information from a DW_TAG_set_type DIE and put it in
16832 the DIE's type field. */
16833
16834 static struct type *
16835 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16836 {
16837 struct type *domain_type, *set_type;
16838 struct attribute *attr;
16839
16840 domain_type = die_type (die, cu);
16841
16842 /* The die_type call above may have already set the type for this DIE. */
16843 set_type = get_die_type (die, cu);
16844 if (set_type)
16845 return set_type;
16846
16847 set_type = create_set_type (NULL, domain_type);
16848
16849 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16850 if (attr != nullptr && attr->form_is_unsigned ())
16851 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16852
16853 maybe_set_alignment (cu, die, set_type);
16854
16855 return set_die_type (die, set_type, cu);
16856 }
16857
16858 /* A helper for read_common_block that creates a locexpr baton.
16859 SYM is the symbol which we are marking as computed.
16860 COMMON_DIE is the DIE for the common block.
16861 COMMON_LOC is the location expression attribute for the common
16862 block itself.
16863 MEMBER_LOC is the location expression attribute for the particular
16864 member of the common block that we are processing.
16865 CU is the CU from which the above come. */
16866
16867 static void
16868 mark_common_block_symbol_computed (struct symbol *sym,
16869 struct die_info *common_die,
16870 struct attribute *common_loc,
16871 struct attribute *member_loc,
16872 struct dwarf2_cu *cu)
16873 {
16874 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16875 struct objfile *objfile = per_objfile->objfile;
16876 struct dwarf2_locexpr_baton *baton;
16877 gdb_byte *ptr;
16878 unsigned int cu_off;
16879 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16880 LONGEST offset = 0;
16881
16882 gdb_assert (common_loc && member_loc);
16883 gdb_assert (common_loc->form_is_block ());
16884 gdb_assert (member_loc->form_is_block ()
16885 || member_loc->form_is_constant ());
16886
16887 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16888 baton->per_objfile = per_objfile;
16889 baton->per_cu = cu->per_cu;
16890 gdb_assert (baton->per_cu);
16891
16892 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16893
16894 if (member_loc->form_is_constant ())
16895 {
16896 offset = member_loc->constant_value (0);
16897 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16898 }
16899 else
16900 baton->size += member_loc->as_block ()->size;
16901
16902 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16903 baton->data = ptr;
16904
16905 *ptr++ = DW_OP_call4;
16906 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16907 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16908 ptr += 4;
16909
16910 if (member_loc->form_is_constant ())
16911 {
16912 *ptr++ = DW_OP_addr;
16913 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16914 ptr += cu->header.addr_size;
16915 }
16916 else
16917 {
16918 /* We have to copy the data here, because DW_OP_call4 will only
16919 use a DW_AT_location attribute. */
16920 struct dwarf_block *block = member_loc->as_block ();
16921 memcpy (ptr, block->data, block->size);
16922 ptr += block->size;
16923 }
16924
16925 *ptr++ = DW_OP_plus;
16926 gdb_assert (ptr - baton->data == baton->size);
16927
16928 SYMBOL_LOCATION_BATON (sym) = baton;
16929 sym->set_aclass_index (dwarf2_locexpr_index);
16930 }
16931
16932 /* Create appropriate locally-scoped variables for all the
16933 DW_TAG_common_block entries. Also create a struct common_block
16934 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16935 is used to separate the common blocks name namespace from regular
16936 variable names. */
16937
16938 static void
16939 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16940 {
16941 struct attribute *attr;
16942
16943 attr = dwarf2_attr (die, DW_AT_location, cu);
16944 if (attr != nullptr)
16945 {
16946 /* Support the .debug_loc offsets. */
16947 if (attr->form_is_block ())
16948 {
16949 /* Ok. */
16950 }
16951 else if (attr->form_is_section_offset ())
16952 {
16953 dwarf2_complex_location_expr_complaint ();
16954 attr = NULL;
16955 }
16956 else
16957 {
16958 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16959 "common block member");
16960 attr = NULL;
16961 }
16962 }
16963
16964 if (die->child != NULL)
16965 {
16966 struct objfile *objfile = cu->per_objfile->objfile;
16967 struct die_info *child_die;
16968 size_t n_entries = 0, size;
16969 struct common_block *common_block;
16970 struct symbol *sym;
16971
16972 for (child_die = die->child;
16973 child_die && child_die->tag;
16974 child_die = child_die->sibling)
16975 ++n_entries;
16976
16977 size = (sizeof (struct common_block)
16978 + (n_entries - 1) * sizeof (struct symbol *));
16979 common_block
16980 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16981 size);
16982 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16983 common_block->n_entries = 0;
16984
16985 for (child_die = die->child;
16986 child_die && child_die->tag;
16987 child_die = child_die->sibling)
16988 {
16989 /* Create the symbol in the DW_TAG_common_block block in the current
16990 symbol scope. */
16991 sym = new_symbol (child_die, NULL, cu);
16992 if (sym != NULL)
16993 {
16994 struct attribute *member_loc;
16995
16996 common_block->contents[common_block->n_entries++] = sym;
16997
16998 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16999 cu);
17000 if (member_loc)
17001 {
17002 /* GDB has handled this for a long time, but it is
17003 not specified by DWARF. It seems to have been
17004 emitted by gfortran at least as recently as:
17005 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17006 complaint (_("Variable in common block has "
17007 "DW_AT_data_member_location "
17008 "- DIE at %s [in module %s]"),
17009 sect_offset_str (child_die->sect_off),
17010 objfile_name (objfile));
17011
17012 if (member_loc->form_is_section_offset ())
17013 dwarf2_complex_location_expr_complaint ();
17014 else if (member_loc->form_is_constant ()
17015 || member_loc->form_is_block ())
17016 {
17017 if (attr != nullptr)
17018 mark_common_block_symbol_computed (sym, die, attr,
17019 member_loc, cu);
17020 }
17021 else
17022 dwarf2_complex_location_expr_complaint ();
17023 }
17024 }
17025 }
17026
17027 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17028 sym->set_value_common_block (common_block);
17029 }
17030 }
17031
17032 /* Create a type for a C++ namespace. */
17033
17034 static struct type *
17035 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17036 {
17037 struct objfile *objfile = cu->per_objfile->objfile;
17038 const char *previous_prefix, *name;
17039 int is_anonymous;
17040 struct type *type;
17041
17042 /* For extensions, reuse the type of the original namespace. */
17043 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17044 {
17045 struct die_info *ext_die;
17046 struct dwarf2_cu *ext_cu = cu;
17047
17048 ext_die = dwarf2_extension (die, &ext_cu);
17049 type = read_type_die (ext_die, ext_cu);
17050
17051 /* EXT_CU may not be the same as CU.
17052 Ensure TYPE is recorded with CU in die_type_hash. */
17053 return set_die_type (die, type, cu);
17054 }
17055
17056 name = namespace_name (die, &is_anonymous, cu);
17057
17058 /* Now build the name of the current namespace. */
17059
17060 previous_prefix = determine_prefix (die, cu);
17061 if (previous_prefix[0] != '\0')
17062 name = typename_concat (&objfile->objfile_obstack,
17063 previous_prefix, name, 0, cu);
17064
17065 /* Create the type. */
17066 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17067
17068 return set_die_type (die, type, cu);
17069 }
17070
17071 /* Read a namespace scope. */
17072
17073 static void
17074 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17075 {
17076 struct objfile *objfile = cu->per_objfile->objfile;
17077 int is_anonymous;
17078
17079 /* Add a symbol associated to this if we haven't seen the namespace
17080 before. Also, add a using directive if it's an anonymous
17081 namespace. */
17082
17083 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17084 {
17085 struct type *type;
17086
17087 type = read_type_die (die, cu);
17088 new_symbol (die, type, cu);
17089
17090 namespace_name (die, &is_anonymous, cu);
17091 if (is_anonymous)
17092 {
17093 const char *previous_prefix = determine_prefix (die, cu);
17094
17095 std::vector<const char *> excludes;
17096 add_using_directive (using_directives (cu),
17097 previous_prefix, type->name (), NULL,
17098 NULL, excludes, 0, &objfile->objfile_obstack);
17099 }
17100 }
17101
17102 if (die->child != NULL)
17103 {
17104 struct die_info *child_die = die->child;
17105
17106 while (child_die && child_die->tag)
17107 {
17108 process_die (child_die, cu);
17109 child_die = child_die->sibling;
17110 }
17111 }
17112 }
17113
17114 /* Read a Fortran module as type. This DIE can be only a declaration used for
17115 imported module. Still we need that type as local Fortran "use ... only"
17116 declaration imports depend on the created type in determine_prefix. */
17117
17118 static struct type *
17119 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17120 {
17121 struct objfile *objfile = cu->per_objfile->objfile;
17122 const char *module_name;
17123 struct type *type;
17124
17125 module_name = dwarf2_name (die, cu);
17126 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17127
17128 return set_die_type (die, type, cu);
17129 }
17130
17131 /* Read a Fortran module. */
17132
17133 static void
17134 read_module (struct die_info *die, struct dwarf2_cu *cu)
17135 {
17136 struct die_info *child_die = die->child;
17137 struct type *type;
17138
17139 type = read_type_die (die, cu);
17140 new_symbol (die, type, cu);
17141
17142 while (child_die && child_die->tag)
17143 {
17144 process_die (child_die, cu);
17145 child_die = child_die->sibling;
17146 }
17147 }
17148
17149 /* Return the name of the namespace represented by DIE. Set
17150 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17151 namespace. */
17152
17153 static const char *
17154 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17155 {
17156 struct die_info *current_die;
17157 const char *name = NULL;
17158
17159 /* Loop through the extensions until we find a name. */
17160
17161 for (current_die = die;
17162 current_die != NULL;
17163 current_die = dwarf2_extension (die, &cu))
17164 {
17165 /* We don't use dwarf2_name here so that we can detect the absence
17166 of a name -> anonymous namespace. */
17167 name = dwarf2_string_attr (die, DW_AT_name, cu);
17168
17169 if (name != NULL)
17170 break;
17171 }
17172
17173 /* Is it an anonymous namespace? */
17174
17175 *is_anonymous = (name == NULL);
17176 if (*is_anonymous)
17177 name = CP_ANONYMOUS_NAMESPACE_STR;
17178
17179 return name;
17180 }
17181
17182 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17183 the user defined type vector. */
17184
17185 static struct type *
17186 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17187 {
17188 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17189 struct comp_unit_head *cu_header = &cu->header;
17190 struct type *type;
17191 struct attribute *attr_byte_size;
17192 struct attribute *attr_address_class;
17193 int byte_size, addr_class;
17194 struct type *target_type;
17195
17196 target_type = die_type (die, cu);
17197
17198 /* The die_type call above may have already set the type for this DIE. */
17199 type = get_die_type (die, cu);
17200 if (type)
17201 return type;
17202
17203 type = lookup_pointer_type (target_type);
17204
17205 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17206 if (attr_byte_size)
17207 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17208 else
17209 byte_size = cu_header->addr_size;
17210
17211 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17212 if (attr_address_class)
17213 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17214 else
17215 addr_class = DW_ADDR_none;
17216
17217 ULONGEST alignment = get_alignment (cu, die);
17218
17219 /* If the pointer size, alignment, or address class is different
17220 than the default, create a type variant marked as such and set
17221 the length accordingly. */
17222 if (TYPE_LENGTH (type) != byte_size
17223 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17224 && alignment != TYPE_RAW_ALIGN (type))
17225 || addr_class != DW_ADDR_none)
17226 {
17227 if (gdbarch_address_class_type_flags_p (gdbarch))
17228 {
17229 type_instance_flags type_flags
17230 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17231 addr_class);
17232 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17233 == 0);
17234 type = make_type_with_address_space (type, type_flags);
17235 }
17236 else if (TYPE_LENGTH (type) != byte_size)
17237 {
17238 complaint (_("invalid pointer size %d"), byte_size);
17239 }
17240 else if (TYPE_RAW_ALIGN (type) != alignment)
17241 {
17242 complaint (_("Invalid DW_AT_alignment"
17243 " - DIE at %s [in module %s]"),
17244 sect_offset_str (die->sect_off),
17245 objfile_name (cu->per_objfile->objfile));
17246 }
17247 else
17248 {
17249 /* Should we also complain about unhandled address classes? */
17250 }
17251 }
17252
17253 TYPE_LENGTH (type) = byte_size;
17254 set_type_align (type, alignment);
17255 return set_die_type (die, type, cu);
17256 }
17257
17258 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17259 the user defined type vector. */
17260
17261 static struct type *
17262 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17263 {
17264 struct type *type;
17265 struct type *to_type;
17266 struct type *domain;
17267
17268 to_type = die_type (die, cu);
17269 domain = die_containing_type (die, cu);
17270
17271 /* The calls above may have already set the type for this DIE. */
17272 type = get_die_type (die, cu);
17273 if (type)
17274 return type;
17275
17276 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17277 type = lookup_methodptr_type (to_type);
17278 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17279 {
17280 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17281
17282 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17283 to_type->fields (), to_type->num_fields (),
17284 to_type->has_varargs ());
17285 type = lookup_methodptr_type (new_type);
17286 }
17287 else
17288 type = lookup_memberptr_type (to_type, domain);
17289
17290 return set_die_type (die, type, cu);
17291 }
17292
17293 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17294 the user defined type vector. */
17295
17296 static struct type *
17297 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17298 enum type_code refcode)
17299 {
17300 struct comp_unit_head *cu_header = &cu->header;
17301 struct type *type, *target_type;
17302 struct attribute *attr;
17303
17304 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17305
17306 target_type = die_type (die, cu);
17307
17308 /* The die_type call above may have already set the type for this DIE. */
17309 type = get_die_type (die, cu);
17310 if (type)
17311 return type;
17312
17313 type = lookup_reference_type (target_type, refcode);
17314 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17315 if (attr != nullptr)
17316 {
17317 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17318 }
17319 else
17320 {
17321 TYPE_LENGTH (type) = cu_header->addr_size;
17322 }
17323 maybe_set_alignment (cu, die, type);
17324 return set_die_type (die, type, cu);
17325 }
17326
17327 /* Add the given cv-qualifiers to the element type of the array. GCC
17328 outputs DWARF type qualifiers that apply to an array, not the
17329 element type. But GDB relies on the array element type to carry
17330 the cv-qualifiers. This mimics section 6.7.3 of the C99
17331 specification. */
17332
17333 static struct type *
17334 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17335 struct type *base_type, int cnst, int voltl)
17336 {
17337 struct type *el_type, *inner_array;
17338
17339 base_type = copy_type (base_type);
17340 inner_array = base_type;
17341
17342 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17343 {
17344 TYPE_TARGET_TYPE (inner_array) =
17345 copy_type (TYPE_TARGET_TYPE (inner_array));
17346 inner_array = TYPE_TARGET_TYPE (inner_array);
17347 }
17348
17349 el_type = TYPE_TARGET_TYPE (inner_array);
17350 cnst |= TYPE_CONST (el_type);
17351 voltl |= TYPE_VOLATILE (el_type);
17352 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17353
17354 return set_die_type (die, base_type, cu);
17355 }
17356
17357 static struct type *
17358 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17359 {
17360 struct type *base_type, *cv_type;
17361
17362 base_type = die_type (die, cu);
17363
17364 /* The die_type call above may have already set the type for this DIE. */
17365 cv_type = get_die_type (die, cu);
17366 if (cv_type)
17367 return cv_type;
17368
17369 /* In case the const qualifier is applied to an array type, the element type
17370 is so qualified, not the array type (section 6.7.3 of C99). */
17371 if (base_type->code () == TYPE_CODE_ARRAY)
17372 return add_array_cv_type (die, cu, base_type, 1, 0);
17373
17374 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17375 return set_die_type (die, cv_type, cu);
17376 }
17377
17378 static struct type *
17379 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17380 {
17381 struct type *base_type, *cv_type;
17382
17383 base_type = die_type (die, cu);
17384
17385 /* The die_type call above may have already set the type for this DIE. */
17386 cv_type = get_die_type (die, cu);
17387 if (cv_type)
17388 return cv_type;
17389
17390 /* In case the volatile qualifier is applied to an array type, the
17391 element type is so qualified, not the array type (section 6.7.3
17392 of C99). */
17393 if (base_type->code () == TYPE_CODE_ARRAY)
17394 return add_array_cv_type (die, cu, base_type, 0, 1);
17395
17396 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17397 return set_die_type (die, cv_type, cu);
17398 }
17399
17400 /* Handle DW_TAG_restrict_type. */
17401
17402 static struct type *
17403 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17404 {
17405 struct type *base_type, *cv_type;
17406
17407 base_type = die_type (die, cu);
17408
17409 /* The die_type call above may have already set the type for this DIE. */
17410 cv_type = get_die_type (die, cu);
17411 if (cv_type)
17412 return cv_type;
17413
17414 cv_type = make_restrict_type (base_type);
17415 return set_die_type (die, cv_type, cu);
17416 }
17417
17418 /* Handle DW_TAG_atomic_type. */
17419
17420 static struct type *
17421 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17422 {
17423 struct type *base_type, *cv_type;
17424
17425 base_type = die_type (die, cu);
17426
17427 /* The die_type call above may have already set the type for this DIE. */
17428 cv_type = get_die_type (die, cu);
17429 if (cv_type)
17430 return cv_type;
17431
17432 cv_type = make_atomic_type (base_type);
17433 return set_die_type (die, cv_type, cu);
17434 }
17435
17436 /* Extract all information from a DW_TAG_string_type DIE and add to
17437 the user defined type vector. It isn't really a user defined type,
17438 but it behaves like one, with other DIE's using an AT_user_def_type
17439 attribute to reference it. */
17440
17441 static struct type *
17442 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17443 {
17444 struct objfile *objfile = cu->per_objfile->objfile;
17445 struct gdbarch *gdbarch = objfile->arch ();
17446 struct type *type, *range_type, *index_type, *char_type;
17447 struct attribute *attr;
17448 struct dynamic_prop prop;
17449 bool length_is_constant = true;
17450 LONGEST length;
17451
17452 /* There are a couple of places where bit sizes might be made use of
17453 when parsing a DW_TAG_string_type, however, no producer that we know
17454 of make use of these. Handling bit sizes that are a multiple of the
17455 byte size is easy enough, but what about other bit sizes? Lets deal
17456 with that problem when we have to. Warn about these attributes being
17457 unsupported, then parse the type and ignore them like we always
17458 have. */
17459 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17460 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17461 {
17462 static bool warning_printed = false;
17463 if (!warning_printed)
17464 {
17465 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17466 "currently supported on DW_TAG_string_type."));
17467 warning_printed = true;
17468 }
17469 }
17470
17471 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17472 if (attr != nullptr && !attr->form_is_constant ())
17473 {
17474 /* The string length describes the location at which the length of
17475 the string can be found. The size of the length field can be
17476 specified with one of the attributes below. */
17477 struct type *prop_type;
17478 struct attribute *len
17479 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17480 if (len == nullptr)
17481 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17482 if (len != nullptr && len->form_is_constant ())
17483 {
17484 /* Pass 0 as the default as we know this attribute is constant
17485 and the default value will not be returned. */
17486 LONGEST sz = len->constant_value (0);
17487 prop_type = objfile_int_type (objfile, sz, true);
17488 }
17489 else
17490 {
17491 /* If the size is not specified then we assume it is the size of
17492 an address on this target. */
17493 prop_type = cu->addr_sized_int_type (true);
17494 }
17495
17496 /* Convert the attribute into a dynamic property. */
17497 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17498 length = 1;
17499 else
17500 length_is_constant = false;
17501 }
17502 else if (attr != nullptr)
17503 {
17504 /* This DW_AT_string_length just contains the length with no
17505 indirection. There's no need to create a dynamic property in this
17506 case. Pass 0 for the default value as we know it will not be
17507 returned in this case. */
17508 length = attr->constant_value (0);
17509 }
17510 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17511 {
17512 /* We don't currently support non-constant byte sizes for strings. */
17513 length = attr->constant_value (1);
17514 }
17515 else
17516 {
17517 /* Use 1 as a fallback length if we have nothing else. */
17518 length = 1;
17519 }
17520
17521 index_type = objfile_type (objfile)->builtin_int;
17522 if (length_is_constant)
17523 range_type = create_static_range_type (NULL, index_type, 1, length);
17524 else
17525 {
17526 struct dynamic_prop low_bound;
17527
17528 low_bound.set_const_val (1);
17529 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17530 }
17531 char_type = language_string_char_type (cu->language_defn, gdbarch);
17532 type = create_string_type (NULL, char_type, range_type);
17533
17534 return set_die_type (die, type, cu);
17535 }
17536
17537 /* Assuming that DIE corresponds to a function, returns nonzero
17538 if the function is prototyped. */
17539
17540 static int
17541 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17542 {
17543 struct attribute *attr;
17544
17545 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17546 if (attr && attr->as_boolean ())
17547 return 1;
17548
17549 /* The DWARF standard implies that the DW_AT_prototyped attribute
17550 is only meaningful for C, but the concept also extends to other
17551 languages that allow unprototyped functions (Eg: Objective C).
17552 For all other languages, assume that functions are always
17553 prototyped. */
17554 if (cu->per_cu->lang != language_c
17555 && cu->per_cu->lang != language_objc
17556 && cu->per_cu->lang != language_opencl)
17557 return 1;
17558
17559 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17560 prototyped and unprototyped functions; default to prototyped,
17561 since that is more common in modern code (and RealView warns
17562 about unprototyped functions). */
17563 if (producer_is_realview (cu->producer))
17564 return 1;
17565
17566 return 0;
17567 }
17568
17569 /* Handle DIES due to C code like:
17570
17571 struct foo
17572 {
17573 int (*funcp)(int a, long l);
17574 int b;
17575 };
17576
17577 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17578
17579 static struct type *
17580 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17581 {
17582 struct objfile *objfile = cu->per_objfile->objfile;
17583 struct type *type; /* Type that this function returns. */
17584 struct type *ftype; /* Function that returns above type. */
17585 struct attribute *attr;
17586
17587 type = die_type (die, cu);
17588
17589 /* The die_type call above may have already set the type for this DIE. */
17590 ftype = get_die_type (die, cu);
17591 if (ftype)
17592 return ftype;
17593
17594 ftype = lookup_function_type (type);
17595
17596 if (prototyped_function_p (die, cu))
17597 ftype->set_is_prototyped (true);
17598
17599 /* Store the calling convention in the type if it's available in
17600 the subroutine die. Otherwise set the calling convention to
17601 the default value DW_CC_normal. */
17602 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17603 if (attr != nullptr
17604 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17605 TYPE_CALLING_CONVENTION (ftype)
17606 = (enum dwarf_calling_convention) attr->constant_value (0);
17607 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17608 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17609 else
17610 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17611
17612 /* Record whether the function returns normally to its caller or not
17613 if the DWARF producer set that information. */
17614 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17615 if (attr && attr->as_boolean ())
17616 TYPE_NO_RETURN (ftype) = 1;
17617
17618 /* We need to add the subroutine type to the die immediately so
17619 we don't infinitely recurse when dealing with parameters
17620 declared as the same subroutine type. */
17621 set_die_type (die, ftype, cu);
17622
17623 if (die->child != NULL)
17624 {
17625 struct type *void_type = objfile_type (objfile)->builtin_void;
17626 struct die_info *child_die;
17627 int nparams, iparams;
17628
17629 /* Count the number of parameters.
17630 FIXME: GDB currently ignores vararg functions, but knows about
17631 vararg member functions. */
17632 nparams = 0;
17633 child_die = die->child;
17634 while (child_die && child_die->tag)
17635 {
17636 if (child_die->tag == DW_TAG_formal_parameter)
17637 nparams++;
17638 else if (child_die->tag == DW_TAG_unspecified_parameters)
17639 ftype->set_has_varargs (true);
17640
17641 child_die = child_die->sibling;
17642 }
17643
17644 /* Allocate storage for parameters and fill them in. */
17645 ftype->set_num_fields (nparams);
17646 ftype->set_fields
17647 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17648
17649 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17650 even if we error out during the parameters reading below. */
17651 for (iparams = 0; iparams < nparams; iparams++)
17652 ftype->field (iparams).set_type (void_type);
17653
17654 iparams = 0;
17655 child_die = die->child;
17656 while (child_die && child_die->tag)
17657 {
17658 if (child_die->tag == DW_TAG_formal_parameter)
17659 {
17660 struct type *arg_type;
17661
17662 /* DWARF version 2 has no clean way to discern C++
17663 static and non-static member functions. G++ helps
17664 GDB by marking the first parameter for non-static
17665 member functions (which is the this pointer) as
17666 artificial. We pass this information to
17667 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17668
17669 DWARF version 3 added DW_AT_object_pointer, which GCC
17670 4.5 does not yet generate. */
17671 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17672 if (attr != nullptr)
17673 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17674 else
17675 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17676 arg_type = die_type (child_die, cu);
17677
17678 /* RealView does not mark THIS as const, which the testsuite
17679 expects. GCC marks THIS as const in method definitions,
17680 but not in the class specifications (GCC PR 43053). */
17681 if (cu->per_cu->lang == language_cplus
17682 && !TYPE_CONST (arg_type)
17683 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17684 {
17685 int is_this = 0;
17686 struct dwarf2_cu *arg_cu = cu;
17687 const char *name = dwarf2_name (child_die, cu);
17688
17689 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17690 if (attr != nullptr)
17691 {
17692 /* If the compiler emits this, use it. */
17693 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17694 is_this = 1;
17695 }
17696 else if (name && strcmp (name, "this") == 0)
17697 /* Function definitions will have the argument names. */
17698 is_this = 1;
17699 else if (name == NULL && iparams == 0)
17700 /* Declarations may not have the names, so like
17701 elsewhere in GDB, assume an artificial first
17702 argument is "this". */
17703 is_this = 1;
17704
17705 if (is_this)
17706 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17707 arg_type, 0);
17708 }
17709
17710 ftype->field (iparams).set_type (arg_type);
17711 iparams++;
17712 }
17713 child_die = child_die->sibling;
17714 }
17715 }
17716
17717 return ftype;
17718 }
17719
17720 static struct type *
17721 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17722 {
17723 struct objfile *objfile = cu->per_objfile->objfile;
17724 const char *name = NULL;
17725 struct type *this_type, *target_type;
17726
17727 name = dwarf2_full_name (NULL, die, cu);
17728 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17729 this_type->set_target_is_stub (true);
17730 set_die_type (die, this_type, cu);
17731 target_type = die_type (die, cu);
17732 if (target_type != this_type)
17733 TYPE_TARGET_TYPE (this_type) = target_type;
17734 else
17735 {
17736 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17737 spec and cause infinite loops in GDB. */
17738 complaint (_("Self-referential DW_TAG_typedef "
17739 "- DIE at %s [in module %s]"),
17740 sect_offset_str (die->sect_off), objfile_name (objfile));
17741 TYPE_TARGET_TYPE (this_type) = NULL;
17742 }
17743 if (name == NULL)
17744 {
17745 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17746 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17747 Handle these by just returning the target type, rather than
17748 constructing an anonymous typedef type and trying to handle this
17749 elsewhere. */
17750 set_die_type (die, target_type, cu);
17751 return target_type;
17752 }
17753 return this_type;
17754 }
17755
17756 /* Helper for get_dwarf2_rational_constant that computes the value of
17757 a given gmp_mpz given an attribute. */
17758
17759 static void
17760 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17761 {
17762 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17763 location expression that pushes an implicit value. */
17764 if (attr->form == DW_FORM_exprloc)
17765 {
17766 dwarf_block *blk = attr->as_block ();
17767 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17768 {
17769 uint64_t len;
17770 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17771 blk->data + blk->size,
17772 &len);
17773 if (ptr - blk->data + len <= blk->size)
17774 {
17775 mpz_import (value->val, len,
17776 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17777 1, 0, 0, ptr);
17778 return;
17779 }
17780 }
17781
17782 /* On failure set it to 1. */
17783 *value = gdb_mpz (1);
17784 }
17785 else if (attr->form_is_block ())
17786 {
17787 dwarf_block *blk = attr->as_block ();
17788 mpz_import (value->val, blk->size,
17789 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17790 1, 0, 0, blk->data);
17791 }
17792 else
17793 *value = gdb_mpz (attr->constant_value (1));
17794 }
17795
17796 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17797 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17798
17799 If the numerator and/or numerator attribute is missing,
17800 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17801 untouched. */
17802
17803 static void
17804 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17805 gdb_mpz *numerator, gdb_mpz *denominator)
17806 {
17807 struct attribute *num_attr, *denom_attr;
17808
17809 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17810 if (num_attr == nullptr)
17811 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17812 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17813
17814 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17815 if (denom_attr == nullptr)
17816 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17817 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17818
17819 if (num_attr == nullptr || denom_attr == nullptr)
17820 return;
17821
17822 get_mpz (cu, numerator, num_attr);
17823 get_mpz (cu, denominator, denom_attr);
17824 }
17825
17826 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17827 rational constant, rather than a signed one.
17828
17829 If the rational constant has a negative value, a complaint
17830 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17831
17832 static void
17833 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17834 struct dwarf2_cu *cu,
17835 gdb_mpz *numerator,
17836 gdb_mpz *denominator)
17837 {
17838 gdb_mpz num (1);
17839 gdb_mpz denom (1);
17840
17841 get_dwarf2_rational_constant (die, cu, &num, &denom);
17842 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17843 {
17844 mpz_neg (num.val, num.val);
17845 mpz_neg (denom.val, denom.val);
17846 }
17847 else if (mpz_sgn (num.val) == -1)
17848 {
17849 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17850 " in DIE at %s"),
17851 sect_offset_str (die->sect_off));
17852 return;
17853 }
17854 else if (mpz_sgn (denom.val) == -1)
17855 {
17856 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17857 " in DIE at %s"),
17858 sect_offset_str (die->sect_off));
17859 return;
17860 }
17861
17862 *numerator = std::move (num);
17863 *denominator = std::move (denom);
17864 }
17865
17866 /* Assuming that ENCODING is a string whose contents starting at the
17867 K'th character is "_nn" where "nn" is a decimal number, scan that
17868 number and set RESULT to the value. K is updated to point to the
17869 character immediately following the number.
17870
17871 If the string does not conform to the format described above, false
17872 is returned, and K may or may not be changed. */
17873
17874 static bool
17875 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17876 {
17877 /* The next character should be an underscore ('_') followed
17878 by a digit. */
17879 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17880 return false;
17881
17882 /* Skip the underscore. */
17883 k++;
17884 int start = k;
17885
17886 /* Determine the number of digits for our number. */
17887 while (isdigit (encoding[k]))
17888 k++;
17889 if (k == start)
17890 return false;
17891
17892 std::string copy (&encoding[start], k - start);
17893 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17894 return false;
17895
17896 return true;
17897 }
17898
17899 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17900 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17901 DENOM, update OFFSET, and return true on success. Return false on
17902 failure. */
17903
17904 static bool
17905 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17906 gdb_mpz *num, gdb_mpz *denom)
17907 {
17908 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17909 return false;
17910 return ada_get_gnat_encoded_number (encoding, offset, denom);
17911 }
17912
17913 /* Assuming DIE corresponds to a fixed point type, finish the creation
17914 of the corresponding TYPE by setting its type-specific data. CU is
17915 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17916 encodings. It is nullptr if the GNAT encoding should be
17917 ignored. */
17918
17919 static void
17920 finish_fixed_point_type (struct type *type, const char *suffix,
17921 struct die_info *die, struct dwarf2_cu *cu)
17922 {
17923 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17924 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17925
17926 /* If GNAT encodings are preferred, don't examine the
17927 attributes. */
17928 struct attribute *attr = nullptr;
17929 if (suffix == nullptr)
17930 {
17931 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17932 if (attr == nullptr)
17933 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17934 if (attr == nullptr)
17935 attr = dwarf2_attr (die, DW_AT_small, cu);
17936 }
17937
17938 /* Numerator and denominator of our fixed-point type's scaling factor.
17939 The default is a scaling factor of 1, which we use as a fallback
17940 when we are not able to decode it (problem with the debugging info,
17941 unsupported forms, bug in GDB, etc...). Using that as the default
17942 allows us to at least print the unscaled value, which might still
17943 be useful to a user. */
17944 gdb_mpz scale_num (1);
17945 gdb_mpz scale_denom (1);
17946
17947 if (attr == nullptr)
17948 {
17949 int offset = 0;
17950 if (suffix != nullptr
17951 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17952 &scale_denom)
17953 /* The number might be encoded as _nn_dd_nn_dd, where the
17954 second ratio is the 'small value. In this situation, we
17955 want the second value. */
17956 && (suffix[offset] != '_'
17957 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17958 &scale_denom)))
17959 {
17960 /* Found it. */
17961 }
17962 else
17963 {
17964 /* Scaling factor not found. Assume a scaling factor of 1,
17965 and hope for the best. At least the user will be able to
17966 see the encoded value. */
17967 scale_num = 1;
17968 scale_denom = 1;
17969 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17970 sect_offset_str (die->sect_off));
17971 }
17972 }
17973 else if (attr->name == DW_AT_binary_scale)
17974 {
17975 LONGEST scale_exp = attr->constant_value (0);
17976 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17977
17978 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17979 }
17980 else if (attr->name == DW_AT_decimal_scale)
17981 {
17982 LONGEST scale_exp = attr->constant_value (0);
17983 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17984
17985 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17986 }
17987 else if (attr->name == DW_AT_small)
17988 {
17989 struct die_info *scale_die;
17990 struct dwarf2_cu *scale_cu = cu;
17991
17992 scale_die = follow_die_ref (die, attr, &scale_cu);
17993 if (scale_die->tag == DW_TAG_constant)
17994 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17995 &scale_num, &scale_denom);
17996 else
17997 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17998 " (DIE at %s)"),
17999 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18000 }
18001 else
18002 {
18003 complaint (_("unsupported scale attribute %s for fixed-point type"
18004 " (DIE at %s)"),
18005 dwarf_attr_name (attr->name),
18006 sect_offset_str (die->sect_off));
18007 }
18008
18009 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18010 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18011 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18012 mpq_canonicalize (scaling_factor.val);
18013 }
18014
18015 /* The gnat-encoding suffix for fixed point. */
18016
18017 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18018
18019 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18020 "XF" suffix of the name. The text after this is what encodes the
18021 'small and 'delta information. Otherwise, return nullptr. */
18022
18023 static const char *
18024 gnat_encoded_fixed_point_type_info (const char *name)
18025 {
18026 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18027 }
18028
18029 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18030 (which may be different from NAME) to the architecture back-end to allow
18031 it to guess the correct format if necessary. */
18032
18033 static struct type *
18034 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18035 const char *name_hint, enum bfd_endian byte_order)
18036 {
18037 struct gdbarch *gdbarch = objfile->arch ();
18038 const struct floatformat **format;
18039 struct type *type;
18040
18041 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18042 if (format)
18043 type = init_float_type (objfile, bits, name, format, byte_order);
18044 else
18045 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18046
18047 return type;
18048 }
18049
18050 /* Allocate an integer type of size BITS and name NAME. */
18051
18052 static struct type *
18053 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18054 int bits, int unsigned_p, const char *name)
18055 {
18056 struct type *type;
18057
18058 /* Versions of Intel's C Compiler generate an integer type called "void"
18059 instead of using DW_TAG_unspecified_type. This has been seen on
18060 at least versions 14, 17, and 18. */
18061 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18062 && strcmp (name, "void") == 0)
18063 type = objfile_type (objfile)->builtin_void;
18064 else
18065 type = init_integer_type (objfile, bits, unsigned_p, name);
18066
18067 return type;
18068 }
18069
18070 /* Return true if DIE has a DW_AT_small attribute whose value is
18071 a constant rational, where both the numerator and denominator
18072 are equal to zero.
18073
18074 CU is the DIE's Compilation Unit. */
18075
18076 static bool
18077 has_zero_over_zero_small_attribute (struct die_info *die,
18078 struct dwarf2_cu *cu)
18079 {
18080 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18081 if (attr == nullptr)
18082 return false;
18083
18084 struct dwarf2_cu *scale_cu = cu;
18085 struct die_info *scale_die
18086 = follow_die_ref (die, attr, &scale_cu);
18087
18088 if (scale_die->tag != DW_TAG_constant)
18089 return false;
18090
18091 gdb_mpz num (1), denom (1);
18092 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18093 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18094 }
18095
18096 /* Initialise and return a floating point type of size BITS suitable for
18097 use as a component of a complex number. The NAME_HINT is passed through
18098 when initialising the floating point type and is the name of the complex
18099 type.
18100
18101 As DWARF doesn't currently provide an explicit name for the components
18102 of a complex number, but it can be helpful to have these components
18103 named, we try to select a suitable name based on the size of the
18104 component. */
18105 static struct type *
18106 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18107 struct objfile *objfile,
18108 int bits, const char *name_hint,
18109 enum bfd_endian byte_order)
18110 {
18111 gdbarch *gdbarch = objfile->arch ();
18112 struct type *tt = nullptr;
18113
18114 /* Try to find a suitable floating point builtin type of size BITS.
18115 We're going to use the name of this type as the name for the complex
18116 target type that we are about to create. */
18117 switch (cu->per_cu->lang)
18118 {
18119 case language_fortran:
18120 switch (bits)
18121 {
18122 case 32:
18123 tt = builtin_f_type (gdbarch)->builtin_real;
18124 break;
18125 case 64:
18126 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18127 break;
18128 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18129 case 128:
18130 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18131 break;
18132 }
18133 break;
18134 default:
18135 switch (bits)
18136 {
18137 case 32:
18138 tt = builtin_type (gdbarch)->builtin_float;
18139 break;
18140 case 64:
18141 tt = builtin_type (gdbarch)->builtin_double;
18142 break;
18143 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18144 case 128:
18145 tt = builtin_type (gdbarch)->builtin_long_double;
18146 break;
18147 }
18148 break;
18149 }
18150
18151 /* If the type we found doesn't match the size we were looking for, then
18152 pretend we didn't find a type at all, the complex target type we
18153 create will then be nameless. */
18154 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18155 tt = nullptr;
18156
18157 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18158 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18159 }
18160
18161 /* Find a representation of a given base type and install
18162 it in the TYPE field of the die. */
18163
18164 static struct type *
18165 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18166 {
18167 struct objfile *objfile = cu->per_objfile->objfile;
18168 struct type *type;
18169 struct attribute *attr;
18170 int encoding = 0, bits = 0;
18171 const char *name;
18172 gdbarch *arch;
18173
18174 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18175 if (attr != nullptr && attr->form_is_constant ())
18176 encoding = attr->constant_value (0);
18177 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18178 if (attr != nullptr)
18179 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18180 name = dwarf2_name (die, cu);
18181 if (!name)
18182 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18183
18184 arch = objfile->arch ();
18185 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18186
18187 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18188 if (attr != nullptr && attr->form_is_constant ())
18189 {
18190 int endianity = attr->constant_value (0);
18191
18192 switch (endianity)
18193 {
18194 case DW_END_big:
18195 byte_order = BFD_ENDIAN_BIG;
18196 break;
18197 case DW_END_little:
18198 byte_order = BFD_ENDIAN_LITTLE;
18199 break;
18200 default:
18201 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18202 break;
18203 }
18204 }
18205
18206 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18207 && cu->per_cu->lang == language_ada
18208 && has_zero_over_zero_small_attribute (die, cu))
18209 {
18210 /* brobecker/2018-02-24: This is a fixed point type for which
18211 the scaling factor is represented as fraction whose value
18212 does not make sense (zero divided by zero), so we should
18213 normally never see these. However, there is a small category
18214 of fixed point types for which GNAT is unable to provide
18215 the scaling factor via the standard DWARF mechanisms, and
18216 for which the info is provided via the GNAT encodings instead.
18217 This is likely what this DIE is about. */
18218 encoding = (encoding == DW_ATE_signed_fixed
18219 ? DW_ATE_signed
18220 : DW_ATE_unsigned);
18221 }
18222
18223 /* With GNAT encodings, fixed-point information will be encoded in
18224 the type name. Note that this can also occur with the above
18225 zero-over-zero case, which is why this is a separate "if" rather
18226 than an "else if". */
18227 const char *gnat_encoding_suffix = nullptr;
18228 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18229 && cu->per_cu->lang == language_ada
18230 && name != nullptr)
18231 {
18232 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18233 if (gnat_encoding_suffix != nullptr)
18234 {
18235 gdb_assert (startswith (gnat_encoding_suffix,
18236 GNAT_FIXED_POINT_SUFFIX));
18237 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18238 name, gnat_encoding_suffix - name);
18239 /* Use -1 here so that SUFFIX points at the "_" after the
18240 "XF". */
18241 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18242
18243 encoding = (encoding == DW_ATE_signed
18244 ? DW_ATE_signed_fixed
18245 : DW_ATE_unsigned_fixed);
18246 }
18247 }
18248
18249 switch (encoding)
18250 {
18251 case DW_ATE_address:
18252 /* Turn DW_ATE_address into a void * pointer. */
18253 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18254 type = init_pointer_type (objfile, bits, name, type);
18255 break;
18256 case DW_ATE_boolean:
18257 type = init_boolean_type (objfile, bits, 1, name);
18258 break;
18259 case DW_ATE_complex_float:
18260 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18261 byte_order);
18262 if (type->code () == TYPE_CODE_ERROR)
18263 {
18264 if (name == nullptr)
18265 {
18266 struct obstack *obstack
18267 = &cu->per_objfile->objfile->objfile_obstack;
18268 name = obconcat (obstack, "_Complex ", type->name (),
18269 nullptr);
18270 }
18271 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18272 }
18273 else
18274 type = init_complex_type (name, type);
18275 break;
18276 case DW_ATE_decimal_float:
18277 type = init_decfloat_type (objfile, bits, name);
18278 break;
18279 case DW_ATE_float:
18280 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18281 break;
18282 case DW_ATE_signed:
18283 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18284 break;
18285 case DW_ATE_unsigned:
18286 if (cu->per_cu->lang == language_fortran
18287 && name
18288 && startswith (name, "character("))
18289 type = init_character_type (objfile, bits, 1, name);
18290 else
18291 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18292 break;
18293 case DW_ATE_signed_char:
18294 if (cu->per_cu->lang == language_ada
18295 || cu->per_cu->lang == language_m2
18296 || cu->per_cu->lang == language_pascal
18297 || cu->per_cu->lang == language_fortran)
18298 type = init_character_type (objfile, bits, 0, name);
18299 else
18300 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18301 break;
18302 case DW_ATE_unsigned_char:
18303 if (cu->per_cu->lang == language_ada
18304 || cu->per_cu->lang == language_m2
18305 || cu->per_cu->lang == language_pascal
18306 || cu->per_cu->lang == language_fortran
18307 || cu->per_cu->lang == language_rust)
18308 type = init_character_type (objfile, bits, 1, name);
18309 else
18310 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18311 break;
18312 case DW_ATE_UTF:
18313 {
18314 type = init_character_type (objfile, bits, 1, name);
18315 return set_die_type (die, type, cu);
18316 }
18317 break;
18318 case DW_ATE_signed_fixed:
18319 type = init_fixed_point_type (objfile, bits, 0, name);
18320 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18321 break;
18322 case DW_ATE_unsigned_fixed:
18323 type = init_fixed_point_type (objfile, bits, 1, name);
18324 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18325 break;
18326
18327 default:
18328 complaint (_("unsupported DW_AT_encoding: '%s'"),
18329 dwarf_type_encoding_name (encoding));
18330 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18331 break;
18332 }
18333
18334 if (type->code () == TYPE_CODE_INT
18335 && name != nullptr
18336 && strcmp (name, "char") == 0)
18337 type->set_has_no_signedness (true);
18338
18339 maybe_set_alignment (cu, die, type);
18340
18341 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18342
18343 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18344 {
18345 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18346 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18347 {
18348 unsigned real_bit_size = attr->as_unsigned ();
18349 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18350 /* Only use the attributes if they make sense together. */
18351 if (attr == nullptr
18352 || (attr->as_unsigned () + real_bit_size
18353 <= 8 * TYPE_LENGTH (type)))
18354 {
18355 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18356 = real_bit_size;
18357 if (attr != nullptr)
18358 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18359 = attr->as_unsigned ();
18360 }
18361 }
18362 }
18363
18364 return set_die_type (die, type, cu);
18365 }
18366
18367 /* A helper function that returns the name of DIE, if it refers to a
18368 variable declaration. */
18369
18370 static const char *
18371 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18372 {
18373 if (die->tag != DW_TAG_variable)
18374 return nullptr;
18375
18376 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18377 if (attr == nullptr || !attr->as_boolean ())
18378 return nullptr;
18379
18380 attr = dwarf2_attr (die, DW_AT_name, cu);
18381 if (attr == nullptr)
18382 return nullptr;
18383 return attr->as_string ();
18384 }
18385
18386 /* Parse dwarf attribute if it's a block, reference or constant and put the
18387 resulting value of the attribute into struct bound_prop.
18388 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18389
18390 static int
18391 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18392 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18393 struct type *default_type)
18394 {
18395 struct dwarf2_property_baton *baton;
18396 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18397 struct objfile *objfile = per_objfile->objfile;
18398 struct obstack *obstack = &objfile->objfile_obstack;
18399
18400 gdb_assert (default_type != NULL);
18401
18402 if (attr == NULL || prop == NULL)
18403 return 0;
18404
18405 if (attr->form_is_block ())
18406 {
18407 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18408 baton->property_type = default_type;
18409 baton->locexpr.per_cu = cu->per_cu;
18410 baton->locexpr.per_objfile = per_objfile;
18411
18412 struct dwarf_block *block;
18413 if (attr->form == DW_FORM_data16)
18414 {
18415 size_t data_size = 16;
18416 block = XOBNEW (obstack, struct dwarf_block);
18417 block->size = (data_size
18418 + 2 /* Extra bytes for DW_OP and arg. */);
18419 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18420 data[0] = DW_OP_implicit_value;
18421 data[1] = data_size;
18422 memcpy (&data[2], attr->as_block ()->data, data_size);
18423 block->data = data;
18424 }
18425 else
18426 block = attr->as_block ();
18427
18428 baton->locexpr.size = block->size;
18429 baton->locexpr.data = block->data;
18430 switch (attr->name)
18431 {
18432 case DW_AT_string_length:
18433 baton->locexpr.is_reference = true;
18434 break;
18435 default:
18436 baton->locexpr.is_reference = false;
18437 break;
18438 }
18439
18440 prop->set_locexpr (baton);
18441 gdb_assert (prop->baton () != NULL);
18442 }
18443 else if (attr->form_is_ref ())
18444 {
18445 struct dwarf2_cu *target_cu = cu;
18446 struct die_info *target_die;
18447 struct attribute *target_attr;
18448
18449 target_die = follow_die_ref (die, attr, &target_cu);
18450 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18451 if (target_attr == NULL)
18452 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18453 target_cu);
18454 if (target_attr == nullptr)
18455 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
18456 target_cu);
18457 if (target_attr == NULL)
18458 {
18459 const char *name = var_decl_name (target_die, target_cu);
18460 if (name != nullptr)
18461 {
18462 prop->set_variable_name (name);
18463 return 1;
18464 }
18465 return 0;
18466 }
18467
18468 switch (target_attr->name)
18469 {
18470 case DW_AT_location:
18471 if (target_attr->form_is_section_offset ())
18472 {
18473 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18474 baton->property_type = die_type (target_die, target_cu);
18475 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18476 prop->set_loclist (baton);
18477 gdb_assert (prop->baton () != NULL);
18478 }
18479 else if (target_attr->form_is_block ())
18480 {
18481 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18482 baton->property_type = die_type (target_die, target_cu);
18483 baton->locexpr.per_cu = cu->per_cu;
18484 baton->locexpr.per_objfile = per_objfile;
18485 struct dwarf_block *block = target_attr->as_block ();
18486 baton->locexpr.size = block->size;
18487 baton->locexpr.data = block->data;
18488 baton->locexpr.is_reference = true;
18489 prop->set_locexpr (baton);
18490 gdb_assert (prop->baton () != NULL);
18491 }
18492 else
18493 {
18494 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18495 "dynamic property");
18496 return 0;
18497 }
18498 break;
18499 case DW_AT_data_member_location:
18500 case DW_AT_data_bit_offset:
18501 {
18502 LONGEST offset;
18503
18504 if (!handle_member_location (target_die, target_cu, &offset))
18505 return 0;
18506
18507 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18508 baton->property_type = read_type_die (target_die->parent,
18509 target_cu);
18510 baton->offset_info.offset = offset;
18511 baton->offset_info.type = die_type (target_die, target_cu);
18512 prop->set_addr_offset (baton);
18513 break;
18514 }
18515 }
18516 }
18517 else if (attr->form_is_constant ())
18518 prop->set_const_val (attr->constant_value (0));
18519 else if (attr->form_is_section_offset ())
18520 {
18521 switch (attr->name)
18522 {
18523 case DW_AT_string_length:
18524 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18525 baton->property_type = default_type;
18526 fill_in_loclist_baton (cu, &baton->loclist, attr);
18527 prop->set_loclist (baton);
18528 gdb_assert (prop->baton () != NULL);
18529 break;
18530 default:
18531 goto invalid;
18532 }
18533 }
18534 else
18535 goto invalid;
18536
18537 return 1;
18538
18539 invalid:
18540 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18541 dwarf2_name (die, cu));
18542 return 0;
18543 }
18544
18545 /* See read.h. */
18546
18547 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18548 present (which is valid) then compute the default type based on the
18549 compilation units address size. */
18550
18551 static struct type *
18552 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18553 {
18554 struct type *index_type = die_type (die, cu);
18555
18556 /* Dwarf-2 specifications explicitly allows to create subrange types
18557 without specifying a base type.
18558 In that case, the base type must be set to the type of
18559 the lower bound, upper bound or count, in that order, if any of these
18560 three attributes references an object that has a type.
18561 If no base type is found, the Dwarf-2 specifications say that
18562 a signed integer type of size equal to the size of an address should
18563 be used.
18564 For the following C code: `extern char gdb_int [];'
18565 GCC produces an empty range DIE.
18566 FIXME: muller/2010-05-28: Possible references to object for low bound,
18567 high bound or count are not yet handled by this code. */
18568 if (index_type->code () == TYPE_CODE_VOID)
18569 index_type = cu->addr_sized_int_type (false);
18570
18571 return index_type;
18572 }
18573
18574 /* Read the given DW_AT_subrange DIE. */
18575
18576 static struct type *
18577 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18578 {
18579 struct type *base_type, *orig_base_type;
18580 struct type *range_type;
18581 struct attribute *attr;
18582 struct dynamic_prop low, high;
18583 int low_default_is_valid;
18584 int high_bound_is_count = 0;
18585 const char *name;
18586 ULONGEST negative_mask;
18587
18588 orig_base_type = read_subrange_index_type (die, cu);
18589
18590 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18591 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18592 creating the range type, but we use the result of check_typedef
18593 when examining properties of the type. */
18594 base_type = check_typedef (orig_base_type);
18595
18596 /* The die_type call above may have already set the type for this DIE. */
18597 range_type = get_die_type (die, cu);
18598 if (range_type)
18599 return range_type;
18600
18601 high.set_const_val (0);
18602
18603 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18604 omitting DW_AT_lower_bound. */
18605 switch (cu->per_cu->lang)
18606 {
18607 case language_c:
18608 case language_cplus:
18609 low.set_const_val (0);
18610 low_default_is_valid = 1;
18611 break;
18612 case language_fortran:
18613 low.set_const_val (1);
18614 low_default_is_valid = 1;
18615 break;
18616 case language_d:
18617 case language_objc:
18618 case language_rust:
18619 low.set_const_val (0);
18620 low_default_is_valid = (cu->header.version >= 4);
18621 break;
18622 case language_ada:
18623 case language_m2:
18624 case language_pascal:
18625 low.set_const_val (1);
18626 low_default_is_valid = (cu->header.version >= 4);
18627 break;
18628 default:
18629 low.set_const_val (0);
18630 low_default_is_valid = 0;
18631 break;
18632 }
18633
18634 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18635 if (attr != nullptr)
18636 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18637 else if (!low_default_is_valid)
18638 complaint (_("Missing DW_AT_lower_bound "
18639 "- DIE at %s [in module %s]"),
18640 sect_offset_str (die->sect_off),
18641 objfile_name (cu->per_objfile->objfile));
18642
18643 struct attribute *attr_ub, *attr_count;
18644 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18645 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18646 {
18647 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18648 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18649 {
18650 /* If bounds are constant do the final calculation here. */
18651 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18652 high.set_const_val (low.const_val () + high.const_val () - 1);
18653 else
18654 high_bound_is_count = 1;
18655 }
18656 else
18657 {
18658 if (attr_ub != NULL)
18659 complaint (_("Unresolved DW_AT_upper_bound "
18660 "- DIE at %s [in module %s]"),
18661 sect_offset_str (die->sect_off),
18662 objfile_name (cu->per_objfile->objfile));
18663 if (attr_count != NULL)
18664 complaint (_("Unresolved DW_AT_count "
18665 "- DIE at %s [in module %s]"),
18666 sect_offset_str (die->sect_off),
18667 objfile_name (cu->per_objfile->objfile));
18668 }
18669 }
18670
18671 LONGEST bias = 0;
18672 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18673 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18674 bias = bias_attr->constant_value (0);
18675
18676 /* Normally, the DWARF producers are expected to use a signed
18677 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18678 But this is unfortunately not always the case, as witnessed
18679 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18680 is used instead. To work around that ambiguity, we treat
18681 the bounds as signed, and thus sign-extend their values, when
18682 the base type is signed. */
18683 negative_mask =
18684 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18685 if (low.kind () == PROP_CONST
18686 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18687 low.set_const_val (low.const_val () | negative_mask);
18688 if (high.kind () == PROP_CONST
18689 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18690 high.set_const_val (high.const_val () | negative_mask);
18691
18692 /* Check for bit and byte strides. */
18693 struct dynamic_prop byte_stride_prop;
18694 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18695 if (attr_byte_stride != nullptr)
18696 {
18697 struct type *prop_type = cu->addr_sized_int_type (false);
18698 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18699 prop_type);
18700 }
18701
18702 struct dynamic_prop bit_stride_prop;
18703 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18704 if (attr_bit_stride != nullptr)
18705 {
18706 /* It only makes sense to have either a bit or byte stride. */
18707 if (attr_byte_stride != nullptr)
18708 {
18709 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18710 "- DIE at %s [in module %s]"),
18711 sect_offset_str (die->sect_off),
18712 objfile_name (cu->per_objfile->objfile));
18713 attr_bit_stride = nullptr;
18714 }
18715 else
18716 {
18717 struct type *prop_type = cu->addr_sized_int_type (false);
18718 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18719 prop_type);
18720 }
18721 }
18722
18723 if (attr_byte_stride != nullptr
18724 || attr_bit_stride != nullptr)
18725 {
18726 bool byte_stride_p = (attr_byte_stride != nullptr);
18727 struct dynamic_prop *stride
18728 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18729
18730 range_type
18731 = create_range_type_with_stride (NULL, orig_base_type, &low,
18732 &high, bias, stride, byte_stride_p);
18733 }
18734 else
18735 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18736
18737 if (high_bound_is_count)
18738 range_type->bounds ()->flag_upper_bound_is_count = 1;
18739
18740 /* Ada expects an empty array on no boundary attributes. */
18741 if (attr == NULL && cu->per_cu->lang != language_ada)
18742 range_type->bounds ()->high.set_undefined ();
18743
18744 name = dwarf2_name (die, cu);
18745 if (name)
18746 range_type->set_name (name);
18747
18748 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18749 if (attr != nullptr)
18750 TYPE_LENGTH (range_type) = attr->constant_value (0);
18751
18752 maybe_set_alignment (cu, die, range_type);
18753
18754 set_die_type (die, range_type, cu);
18755
18756 /* set_die_type should be already done. */
18757 set_descriptive_type (range_type, die, cu);
18758
18759 return range_type;
18760 }
18761
18762 static struct type *
18763 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18764 {
18765 struct type *type;
18766
18767 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18768 type->set_name (dwarf2_name (die, cu));
18769
18770 /* In Ada, an unspecified type is typically used when the description
18771 of the type is deferred to a different unit. When encountering
18772 such a type, we treat it as a stub, and try to resolve it later on,
18773 when needed. */
18774 if (cu->per_cu->lang == language_ada)
18775 type->set_is_stub (true);
18776
18777 return set_die_type (die, type, cu);
18778 }
18779
18780 /* Read a single die and all its descendents. Set the die's sibling
18781 field to NULL; set other fields in the die correctly, and set all
18782 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18783 location of the info_ptr after reading all of those dies. PARENT
18784 is the parent of the die in question. */
18785
18786 static struct die_info *
18787 read_die_and_children (const struct die_reader_specs *reader,
18788 const gdb_byte *info_ptr,
18789 const gdb_byte **new_info_ptr,
18790 struct die_info *parent)
18791 {
18792 struct die_info *die;
18793 const gdb_byte *cur_ptr;
18794
18795 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18796 if (die == NULL)
18797 {
18798 *new_info_ptr = cur_ptr;
18799 return NULL;
18800 }
18801 store_in_ref_table (die, reader->cu);
18802
18803 if (die->has_children)
18804 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18805 else
18806 {
18807 die->child = NULL;
18808 *new_info_ptr = cur_ptr;
18809 }
18810
18811 die->sibling = NULL;
18812 die->parent = parent;
18813 return die;
18814 }
18815
18816 /* Read a die, all of its descendents, and all of its siblings; set
18817 all of the fields of all of the dies correctly. Arguments are as
18818 in read_die_and_children. */
18819
18820 static struct die_info *
18821 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18822 const gdb_byte *info_ptr,
18823 const gdb_byte **new_info_ptr,
18824 struct die_info *parent)
18825 {
18826 struct die_info *first_die, *last_sibling;
18827 const gdb_byte *cur_ptr;
18828
18829 cur_ptr = info_ptr;
18830 first_die = last_sibling = NULL;
18831
18832 while (1)
18833 {
18834 struct die_info *die
18835 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18836
18837 if (die == NULL)
18838 {
18839 *new_info_ptr = cur_ptr;
18840 return first_die;
18841 }
18842
18843 if (!first_die)
18844 first_die = die;
18845 else
18846 last_sibling->sibling = die;
18847
18848 last_sibling = die;
18849 }
18850 }
18851
18852 /* Read a die, all of its descendents, and all of its siblings; set
18853 all of the fields of all of the dies correctly. Arguments are as
18854 in read_die_and_children.
18855 This the main entry point for reading a DIE and all its children. */
18856
18857 static struct die_info *
18858 read_die_and_siblings (const struct die_reader_specs *reader,
18859 const gdb_byte *info_ptr,
18860 const gdb_byte **new_info_ptr,
18861 struct die_info *parent)
18862 {
18863 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18864 new_info_ptr, parent);
18865
18866 if (dwarf_die_debug)
18867 {
18868 gdb_printf (gdb_stdlog,
18869 "Read die from %s@0x%x of %s:\n",
18870 reader->die_section->get_name (),
18871 (unsigned) (info_ptr - reader->die_section->buffer),
18872 bfd_get_filename (reader->abfd));
18873 dump_die (die, dwarf_die_debug);
18874 }
18875
18876 return die;
18877 }
18878
18879 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18880 attributes.
18881 The caller is responsible for filling in the extra attributes
18882 and updating (*DIEP)->num_attrs.
18883 Set DIEP to point to a newly allocated die with its information,
18884 except for its child, sibling, and parent fields. */
18885
18886 static const gdb_byte *
18887 read_full_die_1 (const struct die_reader_specs *reader,
18888 struct die_info **diep, const gdb_byte *info_ptr,
18889 int num_extra_attrs)
18890 {
18891 unsigned int abbrev_number, bytes_read, i;
18892 const struct abbrev_info *abbrev;
18893 struct die_info *die;
18894 struct dwarf2_cu *cu = reader->cu;
18895 bfd *abfd = reader->abfd;
18896
18897 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18898 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18899 info_ptr += bytes_read;
18900 if (!abbrev_number)
18901 {
18902 *diep = NULL;
18903 return info_ptr;
18904 }
18905
18906 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18907 if (!abbrev)
18908 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18909 abbrev_number,
18910 bfd_get_filename (abfd));
18911
18912 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18913 die->sect_off = sect_off;
18914 die->tag = abbrev->tag;
18915 die->abbrev = abbrev_number;
18916 die->has_children = abbrev->has_children;
18917
18918 /* Make the result usable.
18919 The caller needs to update num_attrs after adding the extra
18920 attributes. */
18921 die->num_attrs = abbrev->num_attrs;
18922
18923 bool any_need_reprocess = false;
18924 for (i = 0; i < abbrev->num_attrs; ++i)
18925 {
18926 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18927 info_ptr);
18928 if (die->attrs[i].requires_reprocessing_p ())
18929 any_need_reprocess = true;
18930 }
18931
18932 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18933 if (attr != nullptr && attr->form_is_unsigned ())
18934 cu->str_offsets_base = attr->as_unsigned ();
18935
18936 attr = die->attr (DW_AT_loclists_base);
18937 if (attr != nullptr)
18938 cu->loclist_base = attr->as_unsigned ();
18939
18940 auto maybe_addr_base = die->addr_base ();
18941 if (maybe_addr_base.has_value ())
18942 cu->addr_base = *maybe_addr_base;
18943
18944 attr = die->attr (DW_AT_rnglists_base);
18945 if (attr != nullptr)
18946 cu->rnglists_base = attr->as_unsigned ();
18947
18948 if (any_need_reprocess)
18949 {
18950 for (i = 0; i < abbrev->num_attrs; ++i)
18951 {
18952 if (die->attrs[i].requires_reprocessing_p ())
18953 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18954 }
18955 }
18956 *diep = die;
18957 return info_ptr;
18958 }
18959
18960 /* Read a die and all its attributes.
18961 Set DIEP to point to a newly allocated die with its information,
18962 except for its child, sibling, and parent fields. */
18963
18964 static const gdb_byte *
18965 read_full_die (const struct die_reader_specs *reader,
18966 struct die_info **diep, const gdb_byte *info_ptr)
18967 {
18968 const gdb_byte *result;
18969
18970 result = read_full_die_1 (reader, diep, info_ptr, 0);
18971
18972 if (dwarf_die_debug)
18973 {
18974 gdb_printf (gdb_stdlog,
18975 "Read die from %s@0x%x of %s:\n",
18976 reader->die_section->get_name (),
18977 (unsigned) (info_ptr - reader->die_section->buffer),
18978 bfd_get_filename (reader->abfd));
18979 dump_die (*diep, dwarf_die_debug);
18980 }
18981
18982 return result;
18983 }
18984 \f
18985
18986 /* Returns nonzero if TAG represents a type that we might generate a partial
18987 symbol for. */
18988
18989 static int
18990 is_type_tag_for_partial (int tag, enum language lang)
18991 {
18992 switch (tag)
18993 {
18994 #if 0
18995 /* Some types that would be reasonable to generate partial symbols for,
18996 that we don't at present. Note that normally this does not
18997 matter, mainly because C compilers don't give names to these
18998 types, but instead emit DW_TAG_typedef. */
18999 case DW_TAG_file_type:
19000 case DW_TAG_ptr_to_member_type:
19001 case DW_TAG_set_type:
19002 case DW_TAG_string_type:
19003 case DW_TAG_subroutine_type:
19004 #endif
19005
19006 /* GNAT may emit an array with a name, but no typedef, so we
19007 need to make a symbol in this case. */
19008 case DW_TAG_array_type:
19009 return lang == language_ada;
19010
19011 case DW_TAG_base_type:
19012 case DW_TAG_class_type:
19013 case DW_TAG_interface_type:
19014 case DW_TAG_enumeration_type:
19015 case DW_TAG_structure_type:
19016 case DW_TAG_subrange_type:
19017 case DW_TAG_generic_subrange:
19018 case DW_TAG_typedef:
19019 case DW_TAG_union_type:
19020 return 1;
19021 default:
19022 return 0;
19023 }
19024 }
19025
19026 /* Load all DIEs that are interesting for partial symbols into memory. */
19027
19028 static struct partial_die_info *
19029 load_partial_dies (const struct die_reader_specs *reader,
19030 const gdb_byte *info_ptr, int building_psymtab)
19031 {
19032 struct dwarf2_cu *cu = reader->cu;
19033 struct objfile *objfile = cu->per_objfile->objfile;
19034 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19035 unsigned int bytes_read;
19036 unsigned int load_all = 0;
19037 int nesting_level = 1;
19038
19039 parent_die = NULL;
19040 last_die = NULL;
19041
19042 gdb_assert (cu->per_cu != NULL);
19043 if (cu->load_all_dies)
19044 load_all = 1;
19045
19046 cu->partial_dies
19047 = htab_create_alloc_ex (cu->header.length / 12,
19048 partial_die_hash,
19049 partial_die_eq,
19050 NULL,
19051 &cu->comp_unit_obstack,
19052 hashtab_obstack_allocate,
19053 dummy_obstack_deallocate);
19054
19055 while (1)
19056 {
19057 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19058 &bytes_read);
19059
19060 /* A NULL abbrev means the end of a series of children. */
19061 if (abbrev == NULL)
19062 {
19063 if (--nesting_level == 0)
19064 return first_die;
19065
19066 info_ptr += bytes_read;
19067 last_die = parent_die;
19068 parent_die = parent_die->die_parent;
19069 continue;
19070 }
19071
19072 /* Check for template arguments. We never save these; if
19073 they're seen, we just mark the parent, and go on our way. */
19074 if (parent_die != NULL
19075 && cu->per_cu->lang == language_cplus
19076 && (abbrev->tag == DW_TAG_template_type_param
19077 || abbrev->tag == DW_TAG_template_value_param))
19078 {
19079 parent_die->has_template_arguments = 1;
19080
19081 if (!load_all)
19082 {
19083 /* We don't need a partial DIE for the template argument. */
19084 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19085 continue;
19086 }
19087 }
19088
19089 /* We only recurse into c++ subprograms looking for template arguments.
19090 Skip their other children. */
19091 if (!load_all
19092 && cu->per_cu->lang == language_cplus
19093 && parent_die != NULL
19094 && parent_die->tag == DW_TAG_subprogram
19095 && abbrev->tag != DW_TAG_inlined_subroutine)
19096 {
19097 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19098 continue;
19099 }
19100
19101 /* Check whether this DIE is interesting enough to save. Normally
19102 we would not be interested in members here, but there may be
19103 later variables referencing them via DW_AT_specification (for
19104 static members). */
19105 if (!load_all
19106 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
19107 && abbrev->tag != DW_TAG_constant
19108 && abbrev->tag != DW_TAG_enumerator
19109 && abbrev->tag != DW_TAG_subprogram
19110 && abbrev->tag != DW_TAG_inlined_subroutine
19111 && abbrev->tag != DW_TAG_lexical_block
19112 && abbrev->tag != DW_TAG_variable
19113 && abbrev->tag != DW_TAG_namespace
19114 && abbrev->tag != DW_TAG_module
19115 && abbrev->tag != DW_TAG_member
19116 && abbrev->tag != DW_TAG_imported_unit
19117 && abbrev->tag != DW_TAG_imported_declaration)
19118 {
19119 /* Otherwise we skip to the next sibling, if any. */
19120 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19121 continue;
19122 }
19123
19124 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19125 abbrev);
19126
19127 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19128
19129 /* This two-pass algorithm for processing partial symbols has a
19130 high cost in cache pressure. Thus, handle some simple cases
19131 here which cover the majority of C partial symbols. DIEs
19132 which neither have specification tags in them, nor could have
19133 specification tags elsewhere pointing at them, can simply be
19134 processed and discarded.
19135
19136 This segment is also optional; scan_partial_symbols and
19137 add_partial_symbol will handle these DIEs if we chain
19138 them in normally. When compilers which do not emit large
19139 quantities of duplicate debug information are more common,
19140 this code can probably be removed. */
19141
19142 /* Any complete simple types at the top level (pretty much all
19143 of them, for a language without namespaces), can be processed
19144 directly. */
19145 if (parent_die == NULL
19146 && pdi.has_specification == 0
19147 && pdi.is_declaration == 0
19148 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19149 || pdi.tag == DW_TAG_base_type
19150 || pdi.tag == DW_TAG_array_type
19151 || pdi.tag == DW_TAG_generic_subrange
19152 || pdi.tag == DW_TAG_subrange_type))
19153 {
19154 if (building_psymtab && pdi.raw_name != NULL)
19155 add_partial_symbol (&pdi, cu);
19156
19157 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19158 continue;
19159 }
19160
19161 /* The exception for DW_TAG_typedef with has_children above is
19162 a workaround of GCC PR debug/47510. In the case of this complaint
19163 type_name_or_error will error on such types later.
19164
19165 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19166 it could not find the child DIEs referenced later, this is checked
19167 above. In correct DWARF DW_TAG_typedef should have no children. */
19168
19169 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19170 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19171 "- DIE at %s [in module %s]"),
19172 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19173
19174 /* If we're at the second level, and we're an enumerator, and
19175 our parent has no specification (meaning possibly lives in a
19176 namespace elsewhere), then we can add the partial symbol now
19177 instead of queueing it. */
19178 if (pdi.tag == DW_TAG_enumerator
19179 && parent_die != NULL
19180 && parent_die->die_parent == NULL
19181 && parent_die->tag == DW_TAG_enumeration_type
19182 && parent_die->has_specification == 0)
19183 {
19184 if (pdi.raw_name == NULL)
19185 complaint (_("malformed enumerator DIE ignored"));
19186 else if (building_psymtab)
19187 add_partial_symbol (&pdi, cu);
19188
19189 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19190 continue;
19191 }
19192
19193 struct partial_die_info *part_die
19194 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19195
19196 /* We'll save this DIE so link it in. */
19197 part_die->die_parent = parent_die;
19198 part_die->die_sibling = NULL;
19199 part_die->die_child = NULL;
19200
19201 if (last_die && last_die == parent_die)
19202 last_die->die_child = part_die;
19203 else if (last_die)
19204 last_die->die_sibling = part_die;
19205
19206 last_die = part_die;
19207
19208 if (first_die == NULL)
19209 first_die = part_die;
19210
19211 /* Maybe add the DIE to the hash table. Not all DIEs that we
19212 find interesting need to be in the hash table, because we
19213 also have the parent/sibling/child chains; only those that we
19214 might refer to by offset later during partial symbol reading.
19215
19216 For now this means things that might have be the target of a
19217 DW_AT_specification, DW_AT_abstract_origin, or
19218 DW_AT_extension. DW_AT_extension will refer only to
19219 namespaces; DW_AT_abstract_origin refers to functions (and
19220 many things under the function DIE, but we do not recurse
19221 into function DIEs during partial symbol reading) and
19222 possibly variables as well; DW_AT_specification refers to
19223 declarations. Declarations ought to have the DW_AT_declaration
19224 flag. It happens that GCC forgets to put it in sometimes, but
19225 only for functions, not for types.
19226
19227 Adding more things than necessary to the hash table is harmless
19228 except for the performance cost. Adding too few will result in
19229 wasted time in find_partial_die, when we reread the compilation
19230 unit with load_all_dies set. */
19231
19232 if (load_all
19233 || abbrev->tag == DW_TAG_constant
19234 || abbrev->tag == DW_TAG_subprogram
19235 || abbrev->tag == DW_TAG_variable
19236 || abbrev->tag == DW_TAG_namespace
19237 || part_die->is_declaration)
19238 {
19239 void **slot;
19240
19241 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19242 to_underlying (part_die->sect_off),
19243 INSERT);
19244 *slot = part_die;
19245 }
19246
19247 /* For some DIEs we want to follow their children (if any). For C
19248 we have no reason to follow the children of structures; for other
19249 languages we have to, so that we can get at method physnames
19250 to infer fully qualified class names, for DW_AT_specification,
19251 and for C++ template arguments. For C++, we also look one level
19252 inside functions to find template arguments (if the name of the
19253 function does not already contain the template arguments).
19254
19255 For Ada and Fortran, we need to scan the children of subprograms
19256 and lexical blocks as well because these languages allow the
19257 definition of nested entities that could be interesting for the
19258 debugger, such as nested subprograms for instance. */
19259 if (last_die->has_children
19260 && (load_all
19261 || last_die->tag == DW_TAG_namespace
19262 || last_die->tag == DW_TAG_module
19263 || last_die->tag == DW_TAG_enumeration_type
19264 || (cu->per_cu->lang == language_cplus
19265 && last_die->tag == DW_TAG_subprogram
19266 && (last_die->raw_name == NULL
19267 || strchr (last_die->raw_name, '<') == NULL))
19268 || (cu->per_cu->lang != language_c
19269 && (last_die->tag == DW_TAG_class_type
19270 || last_die->tag == DW_TAG_interface_type
19271 || last_die->tag == DW_TAG_structure_type
19272 || last_die->tag == DW_TAG_union_type))
19273 || ((cu->per_cu->lang == language_ada
19274 || cu->per_cu->lang == language_fortran)
19275 && (last_die->tag == DW_TAG_subprogram
19276 || last_die->tag == DW_TAG_lexical_block))))
19277 {
19278 nesting_level++;
19279 parent_die = last_die;
19280 continue;
19281 }
19282
19283 /* Otherwise we skip to the next sibling, if any. */
19284 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19285
19286 /* Back to the top, do it again. */
19287 }
19288 }
19289
19290 partial_die_info::partial_die_info (sect_offset sect_off_,
19291 const struct abbrev_info *abbrev)
19292 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19293 {
19294 }
19295
19296 /* See class definition. */
19297
19298 const char *
19299 partial_die_info::name (dwarf2_cu *cu)
19300 {
19301 if (!canonical_name && raw_name != nullptr)
19302 {
19303 struct objfile *objfile = cu->per_objfile->objfile;
19304 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19305 canonical_name = 1;
19306 }
19307
19308 return raw_name;
19309 }
19310
19311 /* Read a minimal amount of information into the minimal die structure.
19312 INFO_PTR should point just after the initial uleb128 of a DIE. */
19313
19314 const gdb_byte *
19315 partial_die_info::read (const struct die_reader_specs *reader,
19316 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19317 {
19318 struct dwarf2_cu *cu = reader->cu;
19319 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19320 unsigned int i;
19321 int has_low_pc_attr = 0;
19322 int has_high_pc_attr = 0;
19323 int high_pc_relative = 0;
19324
19325 for (i = 0; i < abbrev.num_attrs; ++i)
19326 {
19327 attribute attr;
19328 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19329 /* String and address offsets that need to do the reprocessing have
19330 already been read at this point, so there is no need to wait until
19331 the loop terminates to do the reprocessing. */
19332 if (attr.requires_reprocessing_p ())
19333 read_attribute_reprocess (reader, &attr, tag);
19334 /* Store the data if it is of an attribute we want to keep in a
19335 partial symbol table. */
19336 switch (attr.name)
19337 {
19338 case DW_AT_name:
19339 switch (tag)
19340 {
19341 case DW_TAG_compile_unit:
19342 case DW_TAG_partial_unit:
19343 case DW_TAG_type_unit:
19344 /* Compilation units have a DW_AT_name that is a filename, not
19345 a source language identifier. */
19346 case DW_TAG_enumeration_type:
19347 case DW_TAG_enumerator:
19348 /* These tags always have simple identifiers already; no need
19349 to canonicalize them. */
19350 canonical_name = 1;
19351 raw_name = attr.as_string ();
19352 break;
19353 default:
19354 canonical_name = 0;
19355 raw_name = attr.as_string ();
19356 break;
19357 }
19358 break;
19359 case DW_AT_linkage_name:
19360 case DW_AT_MIPS_linkage_name:
19361 /* Note that both forms of linkage name might appear. We
19362 assume they will be the same, and we only store the last
19363 one we see. */
19364 linkage_name = attr.as_string ();
19365 break;
19366 case DW_AT_low_pc:
19367 has_low_pc_attr = 1;
19368 lowpc = attr.as_address ();
19369 break;
19370 case DW_AT_high_pc:
19371 has_high_pc_attr = 1;
19372 highpc = attr.as_address ();
19373 if (cu->header.version >= 4 && attr.form_is_constant ())
19374 high_pc_relative = 1;
19375 break;
19376 case DW_AT_location:
19377 /* Support the .debug_loc offsets. */
19378 if (attr.form_is_block ())
19379 {
19380 d.locdesc = attr.as_block ();
19381 }
19382 else if (attr.form_is_section_offset ())
19383 {
19384 dwarf2_complex_location_expr_complaint ();
19385 }
19386 else
19387 {
19388 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19389 "partial symbol information");
19390 }
19391 break;
19392 case DW_AT_external:
19393 is_external = attr.as_boolean ();
19394 break;
19395 case DW_AT_declaration:
19396 is_declaration = attr.as_boolean ();
19397 break;
19398 case DW_AT_type:
19399 has_type = 1;
19400 break;
19401 case DW_AT_abstract_origin:
19402 case DW_AT_specification:
19403 case DW_AT_extension:
19404 has_specification = 1;
19405 spec_offset = attr.get_ref_die_offset ();
19406 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19407 || cu->per_cu->is_dwz);
19408 break;
19409 case DW_AT_sibling:
19410 /* Ignore absolute siblings, they might point outside of
19411 the current compile unit. */
19412 if (attr.form == DW_FORM_ref_addr)
19413 complaint (_("ignoring absolute DW_AT_sibling"));
19414 else
19415 {
19416 const gdb_byte *buffer = reader->buffer;
19417 sect_offset off = attr.get_ref_die_offset ();
19418 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19419
19420 if (sibling_ptr < info_ptr)
19421 complaint (_("DW_AT_sibling points backwards"));
19422 else if (sibling_ptr > reader->buffer_end)
19423 reader->die_section->overflow_complaint ();
19424 else
19425 sibling = sibling_ptr;
19426 }
19427 break;
19428 case DW_AT_byte_size:
19429 has_byte_size = 1;
19430 break;
19431 case DW_AT_const_value:
19432 has_const_value = 1;
19433 break;
19434 case DW_AT_calling_convention:
19435 /* DWARF doesn't provide a way to identify a program's source-level
19436 entry point. DW_AT_calling_convention attributes are only meant
19437 to describe functions' calling conventions.
19438
19439 However, because it's a necessary piece of information in
19440 Fortran, and before DWARF 4 DW_CC_program was the only
19441 piece of debugging information whose definition refers to
19442 a 'main program' at all, several compilers marked Fortran
19443 main programs with DW_CC_program --- even when those
19444 functions use the standard calling conventions.
19445
19446 Although DWARF now specifies a way to provide this
19447 information, we support this practice for backward
19448 compatibility. */
19449 if (attr.constant_value (0) == DW_CC_program
19450 && cu->per_cu->lang == language_fortran)
19451 main_subprogram = 1;
19452 break;
19453 case DW_AT_inline:
19454 {
19455 LONGEST value = attr.constant_value (-1);
19456 if (value == DW_INL_inlined
19457 || value == DW_INL_declared_inlined)
19458 may_be_inlined = 1;
19459 }
19460 break;
19461
19462 case DW_AT_import:
19463 if (tag == DW_TAG_imported_unit)
19464 {
19465 d.sect_off = attr.get_ref_die_offset ();
19466 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19467 || cu->per_cu->is_dwz);
19468 }
19469 break;
19470
19471 case DW_AT_main_subprogram:
19472 main_subprogram = attr.as_boolean ();
19473 break;
19474
19475 case DW_AT_ranges:
19476 {
19477 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19478 on DWARF version). */
19479 ranges_offset = attr.as_unsigned ();
19480
19481 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19482 this value. */
19483 if (tag != DW_TAG_compile_unit)
19484 ranges_offset += cu->gnu_ranges_base;
19485
19486 has_range_info = 1;
19487 }
19488 break;
19489
19490 default:
19491 break;
19492 }
19493 }
19494
19495 /* For Ada, if both the name and the linkage name appear, we prefer
19496 the latter. This lets "catch exception" work better, regardless
19497 of the order in which the name and linkage name were emitted.
19498 Really, though, this is just a workaround for the fact that gdb
19499 doesn't store both the name and the linkage name. */
19500 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
19501 raw_name = linkage_name;
19502
19503 if (high_pc_relative)
19504 highpc += lowpc;
19505
19506 if (has_low_pc_attr && has_high_pc_attr)
19507 {
19508 /* When using the GNU linker, .gnu.linkonce. sections are used to
19509 eliminate duplicate copies of functions and vtables and such.
19510 The linker will arbitrarily choose one and discard the others.
19511 The AT_*_pc values for such functions refer to local labels in
19512 these sections. If the section from that file was discarded, the
19513 labels are not in the output, so the relocs get a value of 0.
19514 If this is a discarded function, mark the pc bounds as invalid,
19515 so that GDB will ignore it. */
19516 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19517 {
19518 struct objfile *objfile = per_objfile->objfile;
19519 struct gdbarch *gdbarch = objfile->arch ();
19520
19521 complaint (_("DW_AT_low_pc %s is zero "
19522 "for DIE at %s [in module %s]"),
19523 paddress (gdbarch, lowpc),
19524 sect_offset_str (sect_off),
19525 objfile_name (objfile));
19526 }
19527 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19528 else if (lowpc >= highpc)
19529 {
19530 struct objfile *objfile = per_objfile->objfile;
19531 struct gdbarch *gdbarch = objfile->arch ();
19532
19533 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19534 "for DIE at %s [in module %s]"),
19535 paddress (gdbarch, lowpc),
19536 paddress (gdbarch, highpc),
19537 sect_offset_str (sect_off),
19538 objfile_name (objfile));
19539 }
19540 else
19541 has_pc_info = 1;
19542 }
19543
19544 return info_ptr;
19545 }
19546
19547 /* Find a cached partial DIE at OFFSET in CU. */
19548
19549 struct partial_die_info *
19550 dwarf2_cu::find_partial_die (sect_offset sect_off)
19551 {
19552 struct partial_die_info *lookup_die = NULL;
19553 struct partial_die_info part_die (sect_off);
19554
19555 lookup_die = ((struct partial_die_info *)
19556 htab_find_with_hash (partial_dies, &part_die,
19557 to_underlying (sect_off)));
19558
19559 return lookup_die;
19560 }
19561
19562 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19563 except in the case of .debug_types DIEs which do not reference
19564 outside their CU (they do however referencing other types via
19565 DW_FORM_ref_sig8). */
19566
19567 static const struct cu_partial_die_info
19568 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19569 {
19570 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19571 struct objfile *objfile = per_objfile->objfile;
19572 struct partial_die_info *pd = NULL;
19573
19574 if (offset_in_dwz == cu->per_cu->is_dwz
19575 && cu->header.offset_in_cu_p (sect_off))
19576 {
19577 pd = cu->find_partial_die (sect_off);
19578 if (pd != NULL)
19579 return { cu, pd };
19580 /* We missed recording what we needed.
19581 Load all dies and try again. */
19582 }
19583 else
19584 {
19585 /* TUs don't reference other CUs/TUs (except via type signatures). */
19586 if (cu->per_cu->is_debug_types)
19587 {
19588 error (_("Dwarf Error: Type Unit at offset %s contains"
19589 " external reference to offset %s [in module %s].\n"),
19590 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19591 bfd_get_filename (objfile->obfd));
19592 }
19593 dwarf2_per_cu_data *per_cu
19594 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19595 per_objfile->per_bfd);
19596
19597 cu = per_objfile->get_cu (per_cu);
19598 if (cu == NULL || cu->partial_dies == NULL)
19599 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19600
19601 cu = per_objfile->get_cu (per_cu);
19602
19603 cu->last_used = 0;
19604 pd = cu->find_partial_die (sect_off);
19605 }
19606
19607 /* If we didn't find it, and not all dies have been loaded,
19608 load them all and try again. */
19609
19610 if (pd == NULL && cu->load_all_dies == 0)
19611 {
19612 cu->load_all_dies = 1;
19613
19614 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19615 THIS_CU->cu may already be in use. So we can't just free it and
19616 replace its DIEs with the ones we read in. Instead, we leave those
19617 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19618 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19619 set. */
19620 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19621
19622 pd = cu->find_partial_die (sect_off);
19623 }
19624
19625 if (pd == NULL)
19626 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19627 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19628 return { cu, pd };
19629 }
19630
19631 /* See if we can figure out if the class lives in a namespace. We do
19632 this by looking for a member function; its demangled name will
19633 contain namespace info, if there is any. */
19634
19635 static void
19636 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19637 struct dwarf2_cu *cu)
19638 {
19639 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19640 what template types look like, because the demangler
19641 frequently doesn't give the same name as the debug info. We
19642 could fix this by only using the demangled name to get the
19643 prefix (but see comment in read_structure_type). */
19644
19645 struct partial_die_info *real_pdi;
19646 struct partial_die_info *child_pdi;
19647
19648 /* If this DIE (this DIE's specification, if any) has a parent, then
19649 we should not do this. We'll prepend the parent's fully qualified
19650 name when we create the partial symbol. */
19651
19652 real_pdi = struct_pdi;
19653 while (real_pdi->has_specification)
19654 {
19655 auto res = find_partial_die (real_pdi->spec_offset,
19656 real_pdi->spec_is_dwz, cu);
19657 real_pdi = res.pdi;
19658 cu = res.cu;
19659 }
19660
19661 if (real_pdi->die_parent != NULL)
19662 return;
19663
19664 for (child_pdi = struct_pdi->die_child;
19665 child_pdi != NULL;
19666 child_pdi = child_pdi->die_sibling)
19667 {
19668 if (child_pdi->tag == DW_TAG_subprogram
19669 && child_pdi->linkage_name != NULL)
19670 {
19671 gdb::unique_xmalloc_ptr<char> actual_class_name
19672 (cu->language_defn->class_name_from_physname
19673 (child_pdi->linkage_name));
19674 if (actual_class_name != NULL)
19675 {
19676 struct objfile *objfile = cu->per_objfile->objfile;
19677 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19678 struct_pdi->canonical_name = 1;
19679 }
19680 break;
19681 }
19682 }
19683 }
19684
19685 /* Return true if a DIE with TAG may have the DW_AT_const_value
19686 attribute. */
19687
19688 static bool
19689 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19690 {
19691 switch (tag)
19692 {
19693 case DW_TAG_constant:
19694 case DW_TAG_enumerator:
19695 case DW_TAG_formal_parameter:
19696 case DW_TAG_template_value_param:
19697 case DW_TAG_variable:
19698 return true;
19699 }
19700
19701 return false;
19702 }
19703
19704 void
19705 partial_die_info::fixup (struct dwarf2_cu *cu)
19706 {
19707 /* Once we've fixed up a die, there's no point in doing so again.
19708 This also avoids a memory leak if we were to call
19709 guess_partial_die_structure_name multiple times. */
19710 if (fixup_called)
19711 return;
19712
19713 /* If we found a reference attribute and the DIE has no name, try
19714 to find a name in the referred to DIE. */
19715
19716 if (raw_name == NULL && has_specification)
19717 {
19718 struct partial_die_info *spec_die;
19719
19720 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19721 spec_die = res.pdi;
19722 cu = res.cu;
19723
19724 spec_die->fixup (cu);
19725
19726 if (spec_die->raw_name)
19727 {
19728 raw_name = spec_die->raw_name;
19729 canonical_name = spec_die->canonical_name;
19730
19731 /* Copy DW_AT_external attribute if it is set. */
19732 if (spec_die->is_external)
19733 is_external = spec_die->is_external;
19734 }
19735 }
19736
19737 if (!has_const_value && has_specification
19738 && can_have_DW_AT_const_value_p (tag))
19739 {
19740 struct partial_die_info *spec_die;
19741
19742 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19743 spec_die = res.pdi;
19744 cu = res.cu;
19745
19746 spec_die->fixup (cu);
19747
19748 if (spec_die->has_const_value)
19749 {
19750 /* Copy DW_AT_const_value attribute if it is set. */
19751 has_const_value = spec_die->has_const_value;
19752 }
19753 }
19754
19755 /* Set default names for some unnamed DIEs. */
19756
19757 if (raw_name == NULL && tag == DW_TAG_namespace)
19758 {
19759 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19760 canonical_name = 1;
19761 }
19762
19763 /* If there is no parent die to provide a namespace, and there are
19764 children, see if we can determine the namespace from their linkage
19765 name. */
19766 if (cu->per_cu->lang == language_cplus
19767 && !cu->per_objfile->per_bfd->types.empty ()
19768 && die_parent == NULL
19769 && has_children
19770 && (tag == DW_TAG_class_type
19771 || tag == DW_TAG_structure_type
19772 || tag == DW_TAG_union_type))
19773 guess_partial_die_structure_name (this, cu);
19774
19775 /* GCC might emit a nameless struct or union that has a linkage
19776 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19777 if (raw_name == NULL
19778 && (tag == DW_TAG_class_type
19779 || tag == DW_TAG_interface_type
19780 || tag == DW_TAG_structure_type
19781 || tag == DW_TAG_union_type)
19782 && linkage_name != NULL)
19783 {
19784 gdb::unique_xmalloc_ptr<char> demangled
19785 (gdb_demangle (linkage_name, DMGL_TYPES));
19786 if (demangled != nullptr)
19787 {
19788 const char *base;
19789
19790 /* Strip any leading namespaces/classes, keep only the base name.
19791 DW_AT_name for named DIEs does not contain the prefixes. */
19792 base = strrchr (demangled.get (), ':');
19793 if (base && base > demangled.get () && base[-1] == ':')
19794 base++;
19795 else
19796 base = demangled.get ();
19797
19798 struct objfile *objfile = cu->per_objfile->objfile;
19799 raw_name = objfile->intern (base);
19800 canonical_name = 1;
19801 }
19802 }
19803
19804 fixup_called = 1;
19805 }
19806
19807 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19808 contents from the given SECTION in the HEADER.
19809
19810 HEADER_OFFSET is the offset of the header in the section. */
19811 static void
19812 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19813 struct dwarf2_section_info *section,
19814 sect_offset header_offset)
19815 {
19816 unsigned int bytes_read;
19817 bfd *abfd = section->get_bfd_owner ();
19818 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19819
19820 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19821 info_ptr += bytes_read;
19822
19823 header->version = read_2_bytes (abfd, info_ptr);
19824 info_ptr += 2;
19825
19826 header->addr_size = read_1_byte (abfd, info_ptr);
19827 info_ptr += 1;
19828
19829 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19830 info_ptr += 1;
19831
19832 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19833 }
19834
19835 /* Return the DW_AT_loclists_base value for the CU. */
19836 static ULONGEST
19837 lookup_loclist_base (struct dwarf2_cu *cu)
19838 {
19839 /* For the .dwo unit, the loclist_base points to the first offset following
19840 the header. The header consists of the following entities-
19841 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19842 bit format)
19843 2. version (2 bytes)
19844 3. address size (1 byte)
19845 4. segment selector size (1 byte)
19846 5. offset entry count (4 bytes)
19847 These sizes are derived as per the DWARFv5 standard. */
19848 if (cu->dwo_unit != nullptr)
19849 {
19850 if (cu->header.initial_length_size == 4)
19851 return LOCLIST_HEADER_SIZE32;
19852 return LOCLIST_HEADER_SIZE64;
19853 }
19854 return cu->loclist_base;
19855 }
19856
19857 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19858 array of offsets in the .debug_loclists section. */
19859
19860 static sect_offset
19861 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19862 {
19863 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19864 struct objfile *objfile = per_objfile->objfile;
19865 bfd *abfd = objfile->obfd;
19866 ULONGEST loclist_header_size =
19867 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19868 : LOCLIST_HEADER_SIZE64);
19869 ULONGEST loclist_base = lookup_loclist_base (cu);
19870
19871 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19872 ULONGEST start_offset =
19873 loclist_base + loclist_index * cu->header.offset_size;
19874
19875 /* Get loclists section. */
19876 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19877
19878 /* Read the loclists section content. */
19879 section->read (objfile);
19880 if (section->buffer == NULL)
19881 error (_("DW_FORM_loclistx used without .debug_loclists "
19882 "section [in module %s]"), objfile_name (objfile));
19883
19884 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19885 so if loclist_base is smaller than the header size, we have a problem. */
19886 if (loclist_base < loclist_header_size)
19887 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19888 objfile_name (objfile));
19889
19890 /* Read the header of the loclists contribution. */
19891 struct loclists_rnglists_header header;
19892 read_loclists_rnglists_header (&header, section,
19893 (sect_offset) (loclist_base - loclist_header_size));
19894
19895 /* Verify the loclist index is valid. */
19896 if (loclist_index >= header.offset_entry_count)
19897 error (_("DW_FORM_loclistx pointing outside of "
19898 ".debug_loclists offset array [in module %s]"),
19899 objfile_name (objfile));
19900
19901 /* Validate that reading won't go beyond the end of the section. */
19902 if (start_offset + cu->header.offset_size > section->size)
19903 error (_("Reading DW_FORM_loclistx index beyond end of"
19904 ".debug_loclists section [in module %s]"),
19905 objfile_name (objfile));
19906
19907 const gdb_byte *info_ptr = section->buffer + start_offset;
19908
19909 if (cu->header.offset_size == 4)
19910 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19911 else
19912 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19913 }
19914
19915 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19916 array of offsets in the .debug_rnglists section. */
19917
19918 static sect_offset
19919 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19920 dwarf_tag tag)
19921 {
19922 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19923 struct objfile *objfile = dwarf2_per_objfile->objfile;
19924 bfd *abfd = objfile->obfd;
19925 ULONGEST rnglist_header_size =
19926 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19927 : RNGLIST_HEADER_SIZE64);
19928
19929 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19930 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19931 doesn't apply. */
19932 ULONGEST rnglist_base =
19933 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19934
19935 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19936 ULONGEST start_offset =
19937 rnglist_base + rnglist_index * cu->header.offset_size;
19938
19939 /* Get rnglists section. */
19940 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19941
19942 /* Read the rnglists section content. */
19943 section->read (objfile);
19944 if (section->buffer == nullptr)
19945 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19946 "[in module %s]"),
19947 objfile_name (objfile));
19948
19949 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19950 so if rnglist_base is smaller than the header size, we have a problem. */
19951 if (rnglist_base < rnglist_header_size)
19952 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19953 objfile_name (objfile));
19954
19955 /* Read the header of the rnglists contribution. */
19956 struct loclists_rnglists_header header;
19957 read_loclists_rnglists_header (&header, section,
19958 (sect_offset) (rnglist_base - rnglist_header_size));
19959
19960 /* Verify the rnglist index is valid. */
19961 if (rnglist_index >= header.offset_entry_count)
19962 error (_("DW_FORM_rnglistx index pointing outside of "
19963 ".debug_rnglists offset array [in module %s]"),
19964 objfile_name (objfile));
19965
19966 /* Validate that reading won't go beyond the end of the section. */
19967 if (start_offset + cu->header.offset_size > section->size)
19968 error (_("Reading DW_FORM_rnglistx index beyond end of"
19969 ".debug_rnglists section [in module %s]"),
19970 objfile_name (objfile));
19971
19972 const gdb_byte *info_ptr = section->buffer + start_offset;
19973
19974 if (cu->header.offset_size == 4)
19975 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19976 else
19977 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19978 }
19979
19980 /* Process the attributes that had to be skipped in the first round. These
19981 attributes are the ones that need str_offsets_base or addr_base attributes.
19982 They could not have been processed in the first round, because at the time
19983 the values of str_offsets_base or addr_base may not have been known. */
19984 static void
19985 read_attribute_reprocess (const struct die_reader_specs *reader,
19986 struct attribute *attr, dwarf_tag tag)
19987 {
19988 struct dwarf2_cu *cu = reader->cu;
19989 switch (attr->form)
19990 {
19991 case DW_FORM_addrx:
19992 case DW_FORM_GNU_addr_index:
19993 attr->set_address (read_addr_index (cu,
19994 attr->as_unsigned_reprocess ()));
19995 break;
19996 case DW_FORM_loclistx:
19997 {
19998 sect_offset loclists_sect_off
19999 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20000
20001 attr->set_unsigned (to_underlying (loclists_sect_off));
20002 }
20003 break;
20004 case DW_FORM_rnglistx:
20005 {
20006 sect_offset rnglists_sect_off
20007 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20008
20009 attr->set_unsigned (to_underlying (rnglists_sect_off));
20010 }
20011 break;
20012 case DW_FORM_strx:
20013 case DW_FORM_strx1:
20014 case DW_FORM_strx2:
20015 case DW_FORM_strx3:
20016 case DW_FORM_strx4:
20017 case DW_FORM_GNU_str_index:
20018 {
20019 unsigned int str_index = attr->as_unsigned_reprocess ();
20020 gdb_assert (!attr->canonical_string_p ());
20021 if (reader->dwo_file != NULL)
20022 attr->set_string_noncanonical (read_dwo_str_index (reader,
20023 str_index));
20024 else
20025 attr->set_string_noncanonical (read_stub_str_index (cu,
20026 str_index));
20027 break;
20028 }
20029 default:
20030 gdb_assert_not_reached ("Unexpected DWARF form.");
20031 }
20032 }
20033
20034 /* Read an attribute value described by an attribute form. */
20035
20036 static const gdb_byte *
20037 read_attribute_value (const struct die_reader_specs *reader,
20038 struct attribute *attr, unsigned form,
20039 LONGEST implicit_const, const gdb_byte *info_ptr)
20040 {
20041 struct dwarf2_cu *cu = reader->cu;
20042 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20043 struct objfile *objfile = per_objfile->objfile;
20044 bfd *abfd = reader->abfd;
20045 struct comp_unit_head *cu_header = &cu->header;
20046 unsigned int bytes_read;
20047 struct dwarf_block *blk;
20048
20049 attr->form = (enum dwarf_form) form;
20050 switch (form)
20051 {
20052 case DW_FORM_ref_addr:
20053 if (cu_header->version == 2)
20054 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20055 &bytes_read));
20056 else
20057 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20058 &bytes_read));
20059 info_ptr += bytes_read;
20060 break;
20061 case DW_FORM_GNU_ref_alt:
20062 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20063 &bytes_read));
20064 info_ptr += bytes_read;
20065 break;
20066 case DW_FORM_addr:
20067 {
20068 struct gdbarch *gdbarch = objfile->arch ();
20069 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20070 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20071 attr->set_address (addr);
20072 info_ptr += bytes_read;
20073 }
20074 break;
20075 case DW_FORM_block2:
20076 blk = dwarf_alloc_block (cu);
20077 blk->size = read_2_bytes (abfd, info_ptr);
20078 info_ptr += 2;
20079 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20080 info_ptr += blk->size;
20081 attr->set_block (blk);
20082 break;
20083 case DW_FORM_block4:
20084 blk = dwarf_alloc_block (cu);
20085 blk->size = read_4_bytes (abfd, info_ptr);
20086 info_ptr += 4;
20087 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20088 info_ptr += blk->size;
20089 attr->set_block (blk);
20090 break;
20091 case DW_FORM_data2:
20092 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20093 info_ptr += 2;
20094 break;
20095 case DW_FORM_data4:
20096 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20097 info_ptr += 4;
20098 break;
20099 case DW_FORM_data8:
20100 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20101 info_ptr += 8;
20102 break;
20103 case DW_FORM_data16:
20104 blk = dwarf_alloc_block (cu);
20105 blk->size = 16;
20106 blk->data = read_n_bytes (abfd, info_ptr, 16);
20107 info_ptr += 16;
20108 attr->set_block (blk);
20109 break;
20110 case DW_FORM_sec_offset:
20111 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20112 &bytes_read));
20113 info_ptr += bytes_read;
20114 break;
20115 case DW_FORM_loclistx:
20116 {
20117 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20118 &bytes_read));
20119 info_ptr += bytes_read;
20120 }
20121 break;
20122 case DW_FORM_string:
20123 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20124 &bytes_read));
20125 info_ptr += bytes_read;
20126 break;
20127 case DW_FORM_strp:
20128 if (!cu->per_cu->is_dwz)
20129 {
20130 attr->set_string_noncanonical
20131 (read_indirect_string (per_objfile,
20132 abfd, info_ptr, cu_header,
20133 &bytes_read));
20134 info_ptr += bytes_read;
20135 break;
20136 }
20137 /* FALLTHROUGH */
20138 case DW_FORM_line_strp:
20139 if (!cu->per_cu->is_dwz)
20140 {
20141 attr->set_string_noncanonical
20142 (per_objfile->read_line_string (info_ptr, cu_header,
20143 &bytes_read));
20144 info_ptr += bytes_read;
20145 break;
20146 }
20147 /* FALLTHROUGH */
20148 case DW_FORM_GNU_strp_alt:
20149 {
20150 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20151 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20152 &bytes_read);
20153
20154 attr->set_string_noncanonical
20155 (dwz->read_string (objfile, str_offset));
20156 info_ptr += bytes_read;
20157 }
20158 break;
20159 case DW_FORM_exprloc:
20160 case DW_FORM_block:
20161 blk = dwarf_alloc_block (cu);
20162 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20163 info_ptr += bytes_read;
20164 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20165 info_ptr += blk->size;
20166 attr->set_block (blk);
20167 break;
20168 case DW_FORM_block1:
20169 blk = dwarf_alloc_block (cu);
20170 blk->size = read_1_byte (abfd, info_ptr);
20171 info_ptr += 1;
20172 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20173 info_ptr += blk->size;
20174 attr->set_block (blk);
20175 break;
20176 case DW_FORM_data1:
20177 case DW_FORM_flag:
20178 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20179 info_ptr += 1;
20180 break;
20181 case DW_FORM_flag_present:
20182 attr->set_unsigned (1);
20183 break;
20184 case DW_FORM_sdata:
20185 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20186 info_ptr += bytes_read;
20187 break;
20188 case DW_FORM_rnglistx:
20189 {
20190 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20191 &bytes_read));
20192 info_ptr += bytes_read;
20193 }
20194 break;
20195 case DW_FORM_udata:
20196 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20197 info_ptr += bytes_read;
20198 break;
20199 case DW_FORM_ref1:
20200 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20201 + read_1_byte (abfd, info_ptr)));
20202 info_ptr += 1;
20203 break;
20204 case DW_FORM_ref2:
20205 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20206 + read_2_bytes (abfd, info_ptr)));
20207 info_ptr += 2;
20208 break;
20209 case DW_FORM_ref4:
20210 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20211 + read_4_bytes (abfd, info_ptr)));
20212 info_ptr += 4;
20213 break;
20214 case DW_FORM_ref8:
20215 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20216 + read_8_bytes (abfd, info_ptr)));
20217 info_ptr += 8;
20218 break;
20219 case DW_FORM_ref_sig8:
20220 attr->set_signature (read_8_bytes (abfd, info_ptr));
20221 info_ptr += 8;
20222 break;
20223 case DW_FORM_ref_udata:
20224 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20225 + read_unsigned_leb128 (abfd, info_ptr,
20226 &bytes_read)));
20227 info_ptr += bytes_read;
20228 break;
20229 case DW_FORM_indirect:
20230 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20231 info_ptr += bytes_read;
20232 if (form == DW_FORM_implicit_const)
20233 {
20234 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20235 info_ptr += bytes_read;
20236 }
20237 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20238 info_ptr);
20239 break;
20240 case DW_FORM_implicit_const:
20241 attr->set_signed (implicit_const);
20242 break;
20243 case DW_FORM_addrx:
20244 case DW_FORM_GNU_addr_index:
20245 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20246 &bytes_read));
20247 info_ptr += bytes_read;
20248 break;
20249 case DW_FORM_strx:
20250 case DW_FORM_strx1:
20251 case DW_FORM_strx2:
20252 case DW_FORM_strx3:
20253 case DW_FORM_strx4:
20254 case DW_FORM_GNU_str_index:
20255 {
20256 ULONGEST str_index;
20257 if (form == DW_FORM_strx1)
20258 {
20259 str_index = read_1_byte (abfd, info_ptr);
20260 info_ptr += 1;
20261 }
20262 else if (form == DW_FORM_strx2)
20263 {
20264 str_index = read_2_bytes (abfd, info_ptr);
20265 info_ptr += 2;
20266 }
20267 else if (form == DW_FORM_strx3)
20268 {
20269 str_index = read_3_bytes (abfd, info_ptr);
20270 info_ptr += 3;
20271 }
20272 else if (form == DW_FORM_strx4)
20273 {
20274 str_index = read_4_bytes (abfd, info_ptr);
20275 info_ptr += 4;
20276 }
20277 else
20278 {
20279 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20280 info_ptr += bytes_read;
20281 }
20282 attr->set_unsigned_reprocess (str_index);
20283 }
20284 break;
20285 default:
20286 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20287 dwarf_form_name (form),
20288 bfd_get_filename (abfd));
20289 }
20290
20291 /* Super hack. */
20292 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20293 attr->form = DW_FORM_GNU_ref_alt;
20294
20295 /* We have seen instances where the compiler tried to emit a byte
20296 size attribute of -1 which ended up being encoded as an unsigned
20297 0xffffffff. Although 0xffffffff is technically a valid size value,
20298 an object of this size seems pretty unlikely so we can relatively
20299 safely treat these cases as if the size attribute was invalid and
20300 treat them as zero by default. */
20301 if (attr->name == DW_AT_byte_size
20302 && form == DW_FORM_data4
20303 && attr->as_unsigned () >= 0xffffffff)
20304 {
20305 complaint
20306 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20307 hex_string (attr->as_unsigned ()));
20308 attr->set_unsigned (0);
20309 }
20310
20311 return info_ptr;
20312 }
20313
20314 /* Read an attribute described by an abbreviated attribute. */
20315
20316 static const gdb_byte *
20317 read_attribute (const struct die_reader_specs *reader,
20318 struct attribute *attr, const struct attr_abbrev *abbrev,
20319 const gdb_byte *info_ptr)
20320 {
20321 attr->name = abbrev->name;
20322 attr->string_is_canonical = 0;
20323 attr->requires_reprocessing = 0;
20324 return read_attribute_value (reader, attr, abbrev->form,
20325 abbrev->implicit_const, info_ptr);
20326 }
20327
20328 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20329
20330 static const char *
20331 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20332 LONGEST str_offset)
20333 {
20334 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20335 str_offset, "DW_FORM_strp");
20336 }
20337
20338 /* Return pointer to string at .debug_str offset as read from BUF.
20339 BUF is assumed to be in a compilation unit described by CU_HEADER.
20340 Return *BYTES_READ_PTR count of bytes read from BUF. */
20341
20342 static const char *
20343 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20344 const gdb_byte *buf,
20345 const struct comp_unit_head *cu_header,
20346 unsigned int *bytes_read_ptr)
20347 {
20348 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20349
20350 return read_indirect_string_at_offset (per_objfile, str_offset);
20351 }
20352
20353 /* See read.h. */
20354
20355 const char *
20356 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20357 unsigned int offset_size)
20358 {
20359 bfd *abfd = objfile->obfd;
20360 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
20361
20362 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20363 }
20364
20365 /* See read.h. */
20366
20367 const char *
20368 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20369 const struct comp_unit_head *cu_header,
20370 unsigned int *bytes_read_ptr)
20371 {
20372 bfd *abfd = objfile->obfd;
20373 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20374
20375 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20376 }
20377
20378 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20379 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20380 ADDR_SIZE is the size of addresses from the CU header. */
20381
20382 static CORE_ADDR
20383 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20384 gdb::optional<ULONGEST> addr_base, int addr_size)
20385 {
20386 struct objfile *objfile = per_objfile->objfile;
20387 bfd *abfd = objfile->obfd;
20388 const gdb_byte *info_ptr;
20389 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20390
20391 per_objfile->per_bfd->addr.read (objfile);
20392 if (per_objfile->per_bfd->addr.buffer == NULL)
20393 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20394 objfile_name (objfile));
20395 if (addr_base_or_zero + addr_index * addr_size
20396 >= per_objfile->per_bfd->addr.size)
20397 error (_("DW_FORM_addr_index pointing outside of "
20398 ".debug_addr section [in module %s]"),
20399 objfile_name (objfile));
20400 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20401 + addr_index * addr_size);
20402 if (addr_size == 4)
20403 return bfd_get_32 (abfd, info_ptr);
20404 else
20405 return bfd_get_64 (abfd, info_ptr);
20406 }
20407
20408 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20409
20410 static CORE_ADDR
20411 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20412 {
20413 return read_addr_index_1 (cu->per_objfile, addr_index,
20414 cu->addr_base, cu->header.addr_size);
20415 }
20416
20417 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20418
20419 static CORE_ADDR
20420 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20421 unsigned int *bytes_read)
20422 {
20423 bfd *abfd = cu->per_objfile->objfile->obfd;
20424 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20425
20426 return read_addr_index (cu, addr_index);
20427 }
20428
20429 /* See read.h. */
20430
20431 CORE_ADDR
20432 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20433 dwarf2_per_objfile *per_objfile,
20434 unsigned int addr_index)
20435 {
20436 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20437 gdb::optional<ULONGEST> addr_base;
20438 int addr_size;
20439
20440 /* We need addr_base and addr_size.
20441 If we don't have PER_CU->cu, we have to get it.
20442 Nasty, but the alternative is storing the needed info in PER_CU,
20443 which at this point doesn't seem justified: it's not clear how frequently
20444 it would get used and it would increase the size of every PER_CU.
20445 Entry points like dwarf2_per_cu_addr_size do a similar thing
20446 so we're not in uncharted territory here.
20447 Alas we need to be a bit more complicated as addr_base is contained
20448 in the DIE.
20449
20450 We don't need to read the entire CU(/TU).
20451 We just need the header and top level die.
20452
20453 IWBN to use the aging mechanism to let us lazily later discard the CU.
20454 For now we skip this optimization. */
20455
20456 if (cu != NULL)
20457 {
20458 addr_base = cu->addr_base;
20459 addr_size = cu->header.addr_size;
20460 }
20461 else
20462 {
20463 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20464 addr_base = reader.cu->addr_base;
20465 addr_size = reader.cu->header.addr_size;
20466 }
20467
20468 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20469 }
20470
20471 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20472 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20473 DWO file. */
20474
20475 static const char *
20476 read_str_index (struct dwarf2_cu *cu,
20477 struct dwarf2_section_info *str_section,
20478 struct dwarf2_section_info *str_offsets_section,
20479 ULONGEST str_offsets_base, ULONGEST str_index)
20480 {
20481 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20482 struct objfile *objfile = per_objfile->objfile;
20483 const char *objf_name = objfile_name (objfile);
20484 bfd *abfd = objfile->obfd;
20485 const gdb_byte *info_ptr;
20486 ULONGEST str_offset;
20487 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20488
20489 str_section->read (objfile);
20490 str_offsets_section->read (objfile);
20491 if (str_section->buffer == NULL)
20492 error (_("%s used without %s section"
20493 " in CU at offset %s [in module %s]"),
20494 form_name, str_section->get_name (),
20495 sect_offset_str (cu->header.sect_off), objf_name);
20496 if (str_offsets_section->buffer == NULL)
20497 error (_("%s used without %s section"
20498 " in CU at offset %s [in module %s]"),
20499 form_name, str_section->get_name (),
20500 sect_offset_str (cu->header.sect_off), objf_name);
20501 info_ptr = (str_offsets_section->buffer
20502 + str_offsets_base
20503 + str_index * cu->header.offset_size);
20504 if (cu->header.offset_size == 4)
20505 str_offset = bfd_get_32 (abfd, info_ptr);
20506 else
20507 str_offset = bfd_get_64 (abfd, info_ptr);
20508 if (str_offset >= str_section->size)
20509 error (_("Offset from %s pointing outside of"
20510 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20511 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20512 return (const char *) (str_section->buffer + str_offset);
20513 }
20514
20515 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20516
20517 static const char *
20518 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20519 {
20520 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20521 ? reader->cu->header.addr_size : 0;
20522 return read_str_index (reader->cu,
20523 &reader->dwo_file->sections.str,
20524 &reader->dwo_file->sections.str_offsets,
20525 str_offsets_base, str_index);
20526 }
20527
20528 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20529
20530 static const char *
20531 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20532 {
20533 struct objfile *objfile = cu->per_objfile->objfile;
20534 const char *objf_name = objfile_name (objfile);
20535 static const char form_name[] = "DW_FORM_GNU_str_index";
20536 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20537
20538 if (!cu->str_offsets_base.has_value ())
20539 error (_("%s used in Fission stub without %s"
20540 " in CU at offset 0x%lx [in module %s]"),
20541 form_name, str_offsets_attr_name,
20542 (long) cu->header.offset_size, objf_name);
20543
20544 return read_str_index (cu,
20545 &cu->per_objfile->per_bfd->str,
20546 &cu->per_objfile->per_bfd->str_offsets,
20547 *cu->str_offsets_base, str_index);
20548 }
20549
20550 /* Return the length of an LEB128 number in BUF. */
20551
20552 static int
20553 leb128_size (const gdb_byte *buf)
20554 {
20555 const gdb_byte *begin = buf;
20556 gdb_byte byte;
20557
20558 while (1)
20559 {
20560 byte = *buf++;
20561 if ((byte & 128) == 0)
20562 return buf - begin;
20563 }
20564 }
20565
20566 static enum language
20567 dwarf_lang_to_enum_language (unsigned int lang)
20568 {
20569 enum language language;
20570
20571 switch (lang)
20572 {
20573 case DW_LANG_C89:
20574 case DW_LANG_C99:
20575 case DW_LANG_C11:
20576 case DW_LANG_C:
20577 case DW_LANG_UPC:
20578 language = language_c;
20579 break;
20580 case DW_LANG_Java:
20581 case DW_LANG_C_plus_plus:
20582 case DW_LANG_C_plus_plus_11:
20583 case DW_LANG_C_plus_plus_14:
20584 language = language_cplus;
20585 break;
20586 case DW_LANG_D:
20587 language = language_d;
20588 break;
20589 case DW_LANG_Fortran77:
20590 case DW_LANG_Fortran90:
20591 case DW_LANG_Fortran95:
20592 case DW_LANG_Fortran03:
20593 case DW_LANG_Fortran08:
20594 language = language_fortran;
20595 break;
20596 case DW_LANG_Go:
20597 language = language_go;
20598 break;
20599 case DW_LANG_Mips_Assembler:
20600 language = language_asm;
20601 break;
20602 case DW_LANG_Ada83:
20603 case DW_LANG_Ada95:
20604 language = language_ada;
20605 break;
20606 case DW_LANG_Modula2:
20607 language = language_m2;
20608 break;
20609 case DW_LANG_Pascal83:
20610 language = language_pascal;
20611 break;
20612 case DW_LANG_ObjC:
20613 language = language_objc;
20614 break;
20615 case DW_LANG_Rust:
20616 case DW_LANG_Rust_old:
20617 language = language_rust;
20618 break;
20619 case DW_LANG_OpenCL:
20620 language = language_opencl;
20621 break;
20622 case DW_LANG_Cobol74:
20623 case DW_LANG_Cobol85:
20624 default:
20625 language = language_minimal;
20626 break;
20627 }
20628
20629 return language;
20630 }
20631
20632 /* Return the named attribute or NULL if not there. */
20633
20634 static struct attribute *
20635 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20636 {
20637 for (;;)
20638 {
20639 unsigned int i;
20640 struct attribute *spec = NULL;
20641
20642 for (i = 0; i < die->num_attrs; ++i)
20643 {
20644 if (die->attrs[i].name == name)
20645 return &die->attrs[i];
20646 if (die->attrs[i].name == DW_AT_specification
20647 || die->attrs[i].name == DW_AT_abstract_origin)
20648 spec = &die->attrs[i];
20649 }
20650
20651 if (!spec)
20652 break;
20653
20654 die = follow_die_ref (die, spec, &cu);
20655 }
20656
20657 return NULL;
20658 }
20659
20660 /* Return the string associated with a string-typed attribute, or NULL if it
20661 is either not found or is of an incorrect type. */
20662
20663 static const char *
20664 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20665 {
20666 struct attribute *attr;
20667 const char *str = NULL;
20668
20669 attr = dwarf2_attr (die, name, cu);
20670
20671 if (attr != NULL)
20672 {
20673 str = attr->as_string ();
20674 if (str == nullptr)
20675 complaint (_("string type expected for attribute %s for "
20676 "DIE at %s in module %s"),
20677 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20678 objfile_name (cu->per_objfile->objfile));
20679 }
20680
20681 return str;
20682 }
20683
20684 /* Return the dwo name or NULL if not present. If present, it is in either
20685 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20686 static const char *
20687 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20688 {
20689 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20690 if (dwo_name == nullptr)
20691 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20692 return dwo_name;
20693 }
20694
20695 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20696 and holds a non-zero value. This function should only be used for
20697 DW_FORM_flag or DW_FORM_flag_present attributes. */
20698
20699 static int
20700 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20701 {
20702 struct attribute *attr = dwarf2_attr (die, name, cu);
20703
20704 return attr != nullptr && attr->as_boolean ();
20705 }
20706
20707 static int
20708 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20709 {
20710 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20711 which value is non-zero. However, we have to be careful with
20712 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20713 (via dwarf2_flag_true_p) follows this attribute. So we may
20714 end up accidently finding a declaration attribute that belongs
20715 to a different DIE referenced by the specification attribute,
20716 even though the given DIE does not have a declaration attribute. */
20717 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20718 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20719 }
20720
20721 /* Return the die giving the specification for DIE, if there is
20722 one. *SPEC_CU is the CU containing DIE on input, and the CU
20723 containing the return value on output. If there is no
20724 specification, but there is an abstract origin, that is
20725 returned. */
20726
20727 static struct die_info *
20728 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20729 {
20730 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20731 *spec_cu);
20732
20733 if (spec_attr == NULL)
20734 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20735
20736 if (spec_attr == NULL)
20737 return NULL;
20738 else
20739 return follow_die_ref (die, spec_attr, spec_cu);
20740 }
20741
20742 /* A convenience function to find the proper .debug_line section for a CU. */
20743
20744 static struct dwarf2_section_info *
20745 get_debug_line_section (struct dwarf2_cu *cu)
20746 {
20747 struct dwarf2_section_info *section;
20748 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20749
20750 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20751 DWO file. */
20752 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20753 section = &cu->dwo_unit->dwo_file->sections.line;
20754 else if (cu->per_cu->is_dwz)
20755 {
20756 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20757
20758 section = &dwz->line;
20759 }
20760 else
20761 section = &per_objfile->per_bfd->line;
20762
20763 return section;
20764 }
20765
20766 /* Read the statement program header starting at OFFSET in
20767 .debug_line, or .debug_line.dwo. Return a pointer
20768 to a struct line_header, allocated using xmalloc.
20769 Returns NULL if there is a problem reading the header, e.g., if it
20770 has a version we don't understand.
20771
20772 NOTE: the strings in the include directory and file name tables of
20773 the returned object point into the dwarf line section buffer,
20774 and must not be freed. */
20775
20776 static line_header_up
20777 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20778 {
20779 struct dwarf2_section_info *section;
20780 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20781
20782 section = get_debug_line_section (cu);
20783 section->read (per_objfile->objfile);
20784 if (section->buffer == NULL)
20785 {
20786 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20787 complaint (_("missing .debug_line.dwo section"));
20788 else
20789 complaint (_("missing .debug_line section"));
20790 return 0;
20791 }
20792
20793 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20794 per_objfile, section, &cu->header);
20795 }
20796
20797 /* Subroutine of dwarf_decode_lines to simplify it.
20798 Return the file name for the given file_entry.
20799 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
20800 If space for the result is malloc'd, *NAME_HOLDER will be set.
20801 Returns NULL if FILE_INDEX should be ignored, i.e., it is
20802 equivalent to CU_INFO. */
20803
20804 static const char *
20805 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
20806 const file_and_directory &cu_info,
20807 gdb::unique_xmalloc_ptr<char> *name_holder)
20808 {
20809 const char *include_name = fe.name;
20810 const char *include_name_to_compare = include_name;
20811
20812 const char *dir_name = fe.include_dir (lh);
20813
20814 gdb::unique_xmalloc_ptr<char> hold_compare;
20815 if (!IS_ABSOLUTE_PATH (include_name)
20816 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
20817 {
20818 /* Avoid creating a duplicate name for CU_INFO.
20819 We do this by comparing INCLUDE_NAME and CU_INFO.
20820 Before we do the comparison, however, we need to account
20821 for DIR_NAME and COMP_DIR.
20822 First prepend dir_name (if non-NULL). If we still don't
20823 have an absolute path prepend comp_dir (if non-NULL).
20824 However, the directory we record in the include-file's
20825 psymtab does not contain COMP_DIR (to match the
20826 corresponding symtab(s)).
20827
20828 Example:
20829
20830 bash$ cd /tmp
20831 bash$ gcc -g ./hello.c
20832 include_name = "hello.c"
20833 dir_name = "."
20834 DW_AT_comp_dir = comp_dir = "/tmp"
20835 DW_AT_name = "./hello.c"
20836
20837 */
20838
20839 if (dir_name != NULL)
20840 {
20841 name_holder->reset (concat (dir_name, SLASH_STRING,
20842 include_name, (char *) NULL));
20843 include_name = name_holder->get ();
20844 include_name_to_compare = include_name;
20845 }
20846 if (!IS_ABSOLUTE_PATH (include_name)
20847 && cu_info.get_comp_dir () != nullptr)
20848 {
20849 hold_compare.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20850 include_name, (char *) NULL));
20851 include_name_to_compare = hold_compare.get ();
20852 }
20853 }
20854
20855 gdb::unique_xmalloc_ptr<char> copied_name;
20856 const char *cu_filename = cu_info.get_name ();
20857 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
20858 {
20859 copied_name.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20860 cu_filename, (char *) NULL));
20861 cu_filename = copied_name.get ();
20862 }
20863
20864 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
20865 return nullptr;
20866 return include_name;
20867 }
20868
20869 /* State machine to track the state of the line number program. */
20870
20871 class lnp_state_machine
20872 {
20873 public:
20874 /* Initialize a machine state for the start of a line number
20875 program. */
20876 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20877 bool record_lines_p);
20878
20879 file_entry *current_file ()
20880 {
20881 /* lh->file_names is 0-based, but the file name numbers in the
20882 statement program are 1-based. */
20883 return m_line_header->file_name_at (m_file);
20884 }
20885
20886 /* Record the line in the state machine. END_SEQUENCE is true if
20887 we're processing the end of a sequence. */
20888 void record_line (bool end_sequence);
20889
20890 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20891 nop-out rest of the lines in this sequence. */
20892 void check_line_address (struct dwarf2_cu *cu,
20893 const gdb_byte *line_ptr,
20894 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20895
20896 void handle_set_discriminator (unsigned int discriminator)
20897 {
20898 m_discriminator = discriminator;
20899 m_line_has_non_zero_discriminator |= discriminator != 0;
20900 }
20901
20902 /* Handle DW_LNE_set_address. */
20903 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20904 {
20905 m_op_index = 0;
20906 address += baseaddr;
20907 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20908 }
20909
20910 /* Handle DW_LNS_advance_pc. */
20911 void handle_advance_pc (CORE_ADDR adjust);
20912
20913 /* Handle a special opcode. */
20914 void handle_special_opcode (unsigned char op_code);
20915
20916 /* Handle DW_LNS_advance_line. */
20917 void handle_advance_line (int line_delta)
20918 {
20919 advance_line (line_delta);
20920 }
20921
20922 /* Handle DW_LNS_set_file. */
20923 void handle_set_file (file_name_index file);
20924
20925 /* Handle DW_LNS_negate_stmt. */
20926 void handle_negate_stmt ()
20927 {
20928 m_flags ^= LEF_IS_STMT;
20929 }
20930
20931 /* Handle DW_LNS_const_add_pc. */
20932 void handle_const_add_pc ();
20933
20934 /* Handle DW_LNS_fixed_advance_pc. */
20935 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20936 {
20937 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20938 m_op_index = 0;
20939 }
20940
20941 /* Handle DW_LNS_copy. */
20942 void handle_copy ()
20943 {
20944 record_line (false);
20945 m_discriminator = 0;
20946 m_flags &= ~LEF_PROLOGUE_END;
20947 }
20948
20949 /* Handle DW_LNE_end_sequence. */
20950 void handle_end_sequence ()
20951 {
20952 m_currently_recording_lines = true;
20953 }
20954
20955 /* Handle DW_LNS_set_prologue_end. */
20956 void handle_set_prologue_end ()
20957 {
20958 m_flags |= LEF_PROLOGUE_END;
20959 }
20960
20961 private:
20962 /* Advance the line by LINE_DELTA. */
20963 void advance_line (int line_delta)
20964 {
20965 m_line += line_delta;
20966
20967 if (line_delta != 0)
20968 m_line_has_non_zero_discriminator = m_discriminator != 0;
20969 }
20970
20971 struct dwarf2_cu *m_cu;
20972
20973 gdbarch *m_gdbarch;
20974
20975 /* True if we're recording lines.
20976 Otherwise we're building partial symtabs and are just interested in
20977 finding include files mentioned by the line number program. */
20978 bool m_record_lines_p;
20979
20980 /* The line number header. */
20981 line_header *m_line_header;
20982
20983 /* These are part of the standard DWARF line number state machine,
20984 and initialized according to the DWARF spec. */
20985
20986 unsigned char m_op_index = 0;
20987 /* The line table index of the current file. */
20988 file_name_index m_file = 1;
20989 unsigned int m_line = 1;
20990
20991 /* These are initialized in the constructor. */
20992
20993 CORE_ADDR m_address;
20994 linetable_entry_flags m_flags;
20995 unsigned int m_discriminator;
20996
20997 /* Additional bits of state we need to track. */
20998
20999 /* The last file that we called dwarf2_start_subfile for.
21000 This is only used for TLLs. */
21001 unsigned int m_last_file = 0;
21002 /* The last file a line number was recorded for. */
21003 struct subfile *m_last_subfile = NULL;
21004
21005 /* The address of the last line entry. */
21006 CORE_ADDR m_last_address;
21007
21008 /* Set to true when a previous line at the same address (using
21009 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
21010 when a line entry at a new address (m_address different to
21011 m_last_address) is processed. */
21012 bool m_stmt_at_address = false;
21013
21014 /* When true, record the lines we decode. */
21015 bool m_currently_recording_lines = false;
21016
21017 /* The last line number that was recorded, used to coalesce
21018 consecutive entries for the same line. This can happen, for
21019 example, when discriminators are present. PR 17276. */
21020 unsigned int m_last_line = 0;
21021 bool m_line_has_non_zero_discriminator = false;
21022 };
21023
21024 void
21025 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21026 {
21027 CORE_ADDR addr_adj = (((m_op_index + adjust)
21028 / m_line_header->maximum_ops_per_instruction)
21029 * m_line_header->minimum_instruction_length);
21030 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21031 m_op_index = ((m_op_index + adjust)
21032 % m_line_header->maximum_ops_per_instruction);
21033 }
21034
21035 void
21036 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21037 {
21038 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21039 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21040 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21041 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21042 / m_line_header->maximum_ops_per_instruction)
21043 * m_line_header->minimum_instruction_length);
21044 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21045 m_op_index = ((m_op_index + adj_opcode_d)
21046 % m_line_header->maximum_ops_per_instruction);
21047
21048 int line_delta = m_line_header->line_base + adj_opcode_r;
21049 advance_line (line_delta);
21050 record_line (false);
21051 m_discriminator = 0;
21052 m_flags &= ~LEF_PROLOGUE_END;
21053 }
21054
21055 void
21056 lnp_state_machine::handle_set_file (file_name_index file)
21057 {
21058 m_file = file;
21059
21060 const file_entry *fe = current_file ();
21061 if (fe == NULL)
21062 dwarf2_debug_line_missing_file_complaint ();
21063 else if (m_record_lines_p)
21064 {
21065 const char *dir = fe->include_dir (m_line_header);
21066
21067 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21068 m_line_has_non_zero_discriminator = m_discriminator != 0;
21069 dwarf2_start_subfile (m_cu, fe->name, dir);
21070 }
21071 }
21072
21073 void
21074 lnp_state_machine::handle_const_add_pc ()
21075 {
21076 CORE_ADDR adjust
21077 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21078
21079 CORE_ADDR addr_adj
21080 = (((m_op_index + adjust)
21081 / m_line_header->maximum_ops_per_instruction)
21082 * m_line_header->minimum_instruction_length);
21083
21084 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21085 m_op_index = ((m_op_index + adjust)
21086 % m_line_header->maximum_ops_per_instruction);
21087 }
21088
21089 /* Return non-zero if we should add LINE to the line number table.
21090 LINE is the line to add, LAST_LINE is the last line that was added,
21091 LAST_SUBFILE is the subfile for LAST_LINE.
21092 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21093 had a non-zero discriminator.
21094
21095 We have to be careful in the presence of discriminators.
21096 E.g., for this line:
21097
21098 for (i = 0; i < 100000; i++);
21099
21100 clang can emit four line number entries for that one line,
21101 each with a different discriminator.
21102 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21103
21104 However, we want gdb to coalesce all four entries into one.
21105 Otherwise the user could stepi into the middle of the line and
21106 gdb would get confused about whether the pc really was in the
21107 middle of the line.
21108
21109 Things are further complicated by the fact that two consecutive
21110 line number entries for the same line is a heuristic used by gcc
21111 to denote the end of the prologue. So we can't just discard duplicate
21112 entries, we have to be selective about it. The heuristic we use is
21113 that we only collapse consecutive entries for the same line if at least
21114 one of those entries has a non-zero discriminator. PR 17276.
21115
21116 Note: Addresses in the line number state machine can never go backwards
21117 within one sequence, thus this coalescing is ok. */
21118
21119 static int
21120 dwarf_record_line_p (struct dwarf2_cu *cu,
21121 unsigned int line, unsigned int last_line,
21122 int line_has_non_zero_discriminator,
21123 struct subfile *last_subfile)
21124 {
21125 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21126 return 1;
21127 if (line != last_line)
21128 return 1;
21129 /* Same line for the same file that we've seen already.
21130 As a last check, for pr 17276, only record the line if the line
21131 has never had a non-zero discriminator. */
21132 if (!line_has_non_zero_discriminator)
21133 return 1;
21134 return 0;
21135 }
21136
21137 /* Use the CU's builder to record line number LINE beginning at
21138 address ADDRESS in the line table of subfile SUBFILE. */
21139
21140 static void
21141 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21142 unsigned int line, CORE_ADDR address,
21143 linetable_entry_flags flags,
21144 struct dwarf2_cu *cu)
21145 {
21146 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21147
21148 if (dwarf_line_debug)
21149 {
21150 gdb_printf (gdb_stdlog,
21151 "Recording line %u, file %s, address %s\n",
21152 line, lbasename (subfile->name),
21153 paddress (gdbarch, address));
21154 }
21155
21156 if (cu != nullptr)
21157 cu->get_builder ()->record_line (subfile, line, addr, flags);
21158 }
21159
21160 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21161 Mark the end of a set of line number records.
21162 The arguments are the same as for dwarf_record_line_1.
21163 If SUBFILE is NULL the request is ignored. */
21164
21165 static void
21166 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21167 CORE_ADDR address, struct dwarf2_cu *cu)
21168 {
21169 if (subfile == NULL)
21170 return;
21171
21172 if (dwarf_line_debug)
21173 {
21174 gdb_printf (gdb_stdlog,
21175 "Finishing current line, file %s, address %s\n",
21176 lbasename (subfile->name),
21177 paddress (gdbarch, address));
21178 }
21179
21180 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
21181 }
21182
21183 void
21184 lnp_state_machine::record_line (bool end_sequence)
21185 {
21186 if (dwarf_line_debug)
21187 {
21188 gdb_printf (gdb_stdlog,
21189 "Processing actual line %u: file %u,"
21190 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
21191 m_line, m_file,
21192 paddress (m_gdbarch, m_address),
21193 (m_flags & LEF_IS_STMT) != 0,
21194 (m_flags & LEF_PROLOGUE_END) != 0,
21195 m_discriminator,
21196 (end_sequence ? "\t(end sequence)" : ""));
21197 }
21198
21199 file_entry *fe = current_file ();
21200
21201 if (fe == NULL)
21202 dwarf2_debug_line_missing_file_complaint ();
21203 /* For now we ignore lines not starting on an instruction boundary.
21204 But not when processing end_sequence for compatibility with the
21205 previous version of the code. */
21206 else if (m_op_index == 0 || end_sequence)
21207 {
21208 fe->included_p = true;
21209 if (m_record_lines_p)
21210 {
21211 /* When we switch files we insert an end maker in the first file,
21212 switch to the second file and add a new line entry. The
21213 problem is that the end marker inserted in the first file will
21214 discard any previous line entries at the same address. If the
21215 line entries in the first file are marked as is-stmt, while
21216 the new line in the second file is non-stmt, then this means
21217 the end marker will discard is-stmt lines so we can have a
21218 non-stmt line. This means that there are less addresses at
21219 which the user can insert a breakpoint.
21220
21221 To improve this we track the last address in m_last_address,
21222 and whether we have seen an is-stmt at this address. Then
21223 when switching files, if we have seen a stmt at the current
21224 address, and we are switching to create a non-stmt line, then
21225 discard the new line. */
21226 bool file_changed
21227 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21228 bool ignore_this_line
21229 = ((file_changed && !end_sequence && m_last_address == m_address
21230 && ((m_flags & LEF_IS_STMT) == 0)
21231 && m_stmt_at_address)
21232 || (!end_sequence && m_line == 0));
21233
21234 if ((file_changed && !ignore_this_line) || end_sequence)
21235 {
21236 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21237 m_currently_recording_lines ? m_cu : nullptr);
21238 }
21239
21240 if (!end_sequence && !ignore_this_line)
21241 {
21242 linetable_entry_flags lte_flags = m_flags;
21243 if (producer_is_codewarrior (m_cu))
21244 lte_flags |= LEF_IS_STMT;
21245
21246 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21247 m_line_has_non_zero_discriminator,
21248 m_last_subfile))
21249 {
21250 buildsym_compunit *builder = m_cu->get_builder ();
21251 dwarf_record_line_1 (m_gdbarch,
21252 builder->get_current_subfile (),
21253 m_line, m_address, lte_flags,
21254 m_currently_recording_lines ? m_cu : nullptr);
21255 }
21256 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21257 m_last_line = m_line;
21258 }
21259 }
21260 }
21261
21262 /* Track whether we have seen any IS_STMT true at m_address in case we
21263 have multiple line table entries all at m_address. */
21264 if (m_last_address != m_address)
21265 {
21266 m_stmt_at_address = false;
21267 m_last_address = m_address;
21268 }
21269 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
21270 }
21271
21272 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21273 line_header *lh, bool record_lines_p)
21274 {
21275 m_cu = cu;
21276 m_gdbarch = arch;
21277 m_record_lines_p = record_lines_p;
21278 m_line_header = lh;
21279
21280 m_currently_recording_lines = true;
21281
21282 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21283 was a line entry for it so that the backend has a chance to adjust it
21284 and also record it in case it needs it. This is currently used by MIPS
21285 code, cf. `mips_adjust_dwarf2_line'. */
21286 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21287 m_flags = 0;
21288 if (lh->default_is_stmt)
21289 m_flags |= LEF_IS_STMT;
21290 m_discriminator = 0;
21291
21292 m_last_address = m_address;
21293 m_stmt_at_address = false;
21294 }
21295
21296 void
21297 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21298 const gdb_byte *line_ptr,
21299 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21300 {
21301 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21302 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21303 located at 0x0. In this case, additionally check that if
21304 ADDRESS < UNRELOCATED_LOWPC. */
21305
21306 if ((address == 0 && address < unrelocated_lowpc)
21307 || address == (CORE_ADDR) -1)
21308 {
21309 /* This line table is for a function which has been
21310 GCd by the linker. Ignore it. PR gdb/12528 */
21311
21312 struct objfile *objfile = cu->per_objfile->objfile;
21313 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21314
21315 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21316 line_offset, objfile_name (objfile));
21317 m_currently_recording_lines = false;
21318 /* Note: m_currently_recording_lines is left as false until we see
21319 DW_LNE_end_sequence. */
21320 }
21321 }
21322
21323 /* Subroutine of dwarf_decode_lines to simplify it.
21324 Process the line number information in LH.
21325 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21326 program in order to set included_p for every referenced header. */
21327
21328 static void
21329 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21330 const int decode_for_pst_p, CORE_ADDR lowpc)
21331 {
21332 const gdb_byte *line_ptr, *extended_end;
21333 const gdb_byte *line_end;
21334 unsigned int bytes_read, extended_len;
21335 unsigned char op_code, extended_op;
21336 CORE_ADDR baseaddr;
21337 struct objfile *objfile = cu->per_objfile->objfile;
21338 bfd *abfd = objfile->obfd;
21339 struct gdbarch *gdbarch = objfile->arch ();
21340 /* True if we're recording line info (as opposed to building partial
21341 symtabs and just interested in finding include files mentioned by
21342 the line number program). */
21343 bool record_lines_p = !decode_for_pst_p;
21344
21345 baseaddr = objfile->text_section_offset ();
21346
21347 line_ptr = lh->statement_program_start;
21348 line_end = lh->statement_program_end;
21349
21350 /* Read the statement sequences until there's nothing left. */
21351 while (line_ptr < line_end)
21352 {
21353 /* The DWARF line number program state machine. Reset the state
21354 machine at the start of each sequence. */
21355 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21356 bool end_sequence = false;
21357
21358 if (record_lines_p)
21359 {
21360 /* Start a subfile for the current file of the state
21361 machine. */
21362 const file_entry *fe = state_machine.current_file ();
21363
21364 if (fe != NULL)
21365 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21366 }
21367
21368 /* Decode the table. */
21369 while (line_ptr < line_end && !end_sequence)
21370 {
21371 op_code = read_1_byte (abfd, line_ptr);
21372 line_ptr += 1;
21373
21374 if (op_code >= lh->opcode_base)
21375 {
21376 /* Special opcode. */
21377 state_machine.handle_special_opcode (op_code);
21378 }
21379 else switch (op_code)
21380 {
21381 case DW_LNS_extended_op:
21382 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21383 &bytes_read);
21384 line_ptr += bytes_read;
21385 extended_end = line_ptr + extended_len;
21386 extended_op = read_1_byte (abfd, line_ptr);
21387 line_ptr += 1;
21388 if (DW_LNE_lo_user <= extended_op
21389 && extended_op <= DW_LNE_hi_user)
21390 {
21391 /* Vendor extension, ignore. */
21392 line_ptr = extended_end;
21393 break;
21394 }
21395 switch (extended_op)
21396 {
21397 case DW_LNE_end_sequence:
21398 state_machine.handle_end_sequence ();
21399 end_sequence = true;
21400 break;
21401 case DW_LNE_set_address:
21402 {
21403 CORE_ADDR address
21404 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21405 line_ptr += bytes_read;
21406
21407 state_machine.check_line_address (cu, line_ptr,
21408 lowpc - baseaddr, address);
21409 state_machine.handle_set_address (baseaddr, address);
21410 }
21411 break;
21412 case DW_LNE_define_file:
21413 {
21414 const char *cur_file;
21415 unsigned int mod_time, length;
21416 dir_index dindex;
21417
21418 cur_file = read_direct_string (abfd, line_ptr,
21419 &bytes_read);
21420 line_ptr += bytes_read;
21421 dindex = (dir_index)
21422 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21423 line_ptr += bytes_read;
21424 mod_time =
21425 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21426 line_ptr += bytes_read;
21427 length =
21428 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21429 line_ptr += bytes_read;
21430 lh->add_file_name (cur_file, dindex, mod_time, length);
21431 }
21432 break;
21433 case DW_LNE_set_discriminator:
21434 {
21435 /* The discriminator is not interesting to the
21436 debugger; just ignore it. We still need to
21437 check its value though:
21438 if there are consecutive entries for the same
21439 (non-prologue) line we want to coalesce them.
21440 PR 17276. */
21441 unsigned int discr
21442 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21443 line_ptr += bytes_read;
21444
21445 state_machine.handle_set_discriminator (discr);
21446 }
21447 break;
21448 default:
21449 complaint (_("mangled .debug_line section"));
21450 return;
21451 }
21452 /* Make sure that we parsed the extended op correctly. If e.g.
21453 we expected a different address size than the producer used,
21454 we may have read the wrong number of bytes. */
21455 if (line_ptr != extended_end)
21456 {
21457 complaint (_("mangled .debug_line section"));
21458 return;
21459 }
21460 break;
21461 case DW_LNS_copy:
21462 state_machine.handle_copy ();
21463 break;
21464 case DW_LNS_advance_pc:
21465 {
21466 CORE_ADDR adjust
21467 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21468 line_ptr += bytes_read;
21469
21470 state_machine.handle_advance_pc (adjust);
21471 }
21472 break;
21473 case DW_LNS_advance_line:
21474 {
21475 int line_delta
21476 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21477 line_ptr += bytes_read;
21478
21479 state_machine.handle_advance_line (line_delta);
21480 }
21481 break;
21482 case DW_LNS_set_file:
21483 {
21484 file_name_index file
21485 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21486 &bytes_read);
21487 line_ptr += bytes_read;
21488
21489 state_machine.handle_set_file (file);
21490 }
21491 break;
21492 case DW_LNS_set_column:
21493 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21494 line_ptr += bytes_read;
21495 break;
21496 case DW_LNS_negate_stmt:
21497 state_machine.handle_negate_stmt ();
21498 break;
21499 case DW_LNS_set_basic_block:
21500 break;
21501 /* Add to the address register of the state machine the
21502 address increment value corresponding to special opcode
21503 255. I.e., this value is scaled by the minimum
21504 instruction length since special opcode 255 would have
21505 scaled the increment. */
21506 case DW_LNS_const_add_pc:
21507 state_machine.handle_const_add_pc ();
21508 break;
21509 case DW_LNS_fixed_advance_pc:
21510 {
21511 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21512 line_ptr += 2;
21513
21514 state_machine.handle_fixed_advance_pc (addr_adj);
21515 }
21516 break;
21517 case DW_LNS_set_prologue_end:
21518 state_machine.handle_set_prologue_end ();
21519 break;
21520 default:
21521 {
21522 /* Unknown standard opcode, ignore it. */
21523 int i;
21524
21525 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21526 {
21527 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21528 line_ptr += bytes_read;
21529 }
21530 }
21531 }
21532 }
21533
21534 if (!end_sequence)
21535 dwarf2_debug_line_missing_end_sequence_complaint ();
21536
21537 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21538 in which case we still finish recording the last line). */
21539 state_machine.record_line (true);
21540 }
21541 }
21542
21543 /* Decode the Line Number Program (LNP) for the given line_header
21544 structure and CU. The actual information extracted and the type
21545 of structures created from the LNP depends on the value of PST.
21546
21547 1. If PST is NULL, then this procedure uses the data from the program
21548 to create all necessary symbol tables, and their linetables.
21549
21550 2. If PST is not NULL, this procedure reads the program to determine
21551 the list of files included by the unit represented by PST, and
21552 builds all the associated partial symbol tables.
21553
21554 FND holds the CU file name and directory, if known.
21555 It is used for relative paths in the line table.
21556
21557 NOTE: It is important that psymtabs have the same file name (via
21558 strcmp) as the corresponding symtab. Since the directory is not
21559 used in the name of the symtab we don't use it in the name of the
21560 psymtabs we create. E.g. expand_line_sal requires this when
21561 finding psymtabs to expand. A good testcase for this is
21562 mb-inline.exp.
21563
21564 LOWPC is the lowest address in CU (or 0 if not known).
21565
21566 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21567 for its PC<->lines mapping information. Otherwise only the filename
21568 table is read in. */
21569
21570 static void
21571 dwarf_decode_lines (struct line_header *lh, const file_and_directory &fnd,
21572 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21573 CORE_ADDR lowpc, int decode_mapping)
21574 {
21575 struct objfile *objfile = cu->per_objfile->objfile;
21576 const int decode_for_pst_p = (pst != NULL);
21577
21578 if (decode_mapping)
21579 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21580
21581 if (decode_for_pst_p)
21582 {
21583 /* Now that we're done scanning the Line Header Program, we can
21584 create the psymtab of each included file. */
21585 for (auto &file_entry : lh->file_names ())
21586 if (file_entry.included_p)
21587 {
21588 gdb::unique_xmalloc_ptr<char> name_holder;
21589 const char *include_name =
21590 compute_include_file_name (lh, file_entry, fnd, &name_holder);
21591 if (include_name != NULL)
21592 dwarf2_create_include_psymtab
21593 (cu->per_objfile->per_bfd, include_name, pst,
21594 cu->per_objfile->per_bfd->partial_symtabs.get (),
21595 objfile->per_bfd);
21596 }
21597 }
21598 else
21599 {
21600 /* Make sure a symtab is created for every file, even files
21601 which contain only variables (i.e. no code with associated
21602 line numbers). */
21603 buildsym_compunit *builder = cu->get_builder ();
21604 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21605
21606 for (auto &fe : lh->file_names ())
21607 {
21608 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21609 if (builder->get_current_subfile ()->symtab == NULL)
21610 {
21611 builder->get_current_subfile ()->symtab
21612 = allocate_symtab (cust,
21613 builder->get_current_subfile ()->name);
21614 }
21615 fe.symtab = builder->get_current_subfile ()->symtab;
21616 }
21617 }
21618 }
21619
21620 /* Start a subfile for DWARF. FILENAME is the name of the file and
21621 DIRNAME the name of the source directory which contains FILENAME
21622 or NULL if not known.
21623 This routine tries to keep line numbers from identical absolute and
21624 relative file names in a common subfile.
21625
21626 Using the `list' example from the GDB testsuite, which resides in
21627 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21628 of /srcdir/list0.c yields the following debugging information for list0.c:
21629
21630 DW_AT_name: /srcdir/list0.c
21631 DW_AT_comp_dir: /compdir
21632 files.files[0].name: list0.h
21633 files.files[0].dir: /srcdir
21634 files.files[1].name: list0.c
21635 files.files[1].dir: /srcdir
21636
21637 The line number information for list0.c has to end up in a single
21638 subfile, so that `break /srcdir/list0.c:1' works as expected.
21639 start_subfile will ensure that this happens provided that we pass the
21640 concatenation of files.files[1].dir and files.files[1].name as the
21641 subfile's name. */
21642
21643 static void
21644 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21645 const char *dirname)
21646 {
21647 gdb::unique_xmalloc_ptr<char> copy;
21648
21649 /* In order not to lose the line information directory,
21650 we concatenate it to the filename when it makes sense.
21651 Note that the Dwarf3 standard says (speaking of filenames in line
21652 information): ``The directory index is ignored for file names
21653 that represent full path names''. Thus ignoring dirname in the
21654 `else' branch below isn't an issue. */
21655
21656 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21657 {
21658 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21659 filename = copy.get ();
21660 }
21661
21662 cu->get_builder ()->start_subfile (filename);
21663 }
21664
21665 static void
21666 var_decode_location (struct attribute *attr, struct symbol *sym,
21667 struct dwarf2_cu *cu)
21668 {
21669 struct objfile *objfile = cu->per_objfile->objfile;
21670 struct comp_unit_head *cu_header = &cu->header;
21671
21672 /* NOTE drow/2003-01-30: There used to be a comment and some special
21673 code here to turn a symbol with DW_AT_external and a
21674 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21675 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21676 with some versions of binutils) where shared libraries could have
21677 relocations against symbols in their debug information - the
21678 minimal symbol would have the right address, but the debug info
21679 would not. It's no longer necessary, because we will explicitly
21680 apply relocations when we read in the debug information now. */
21681
21682 /* A DW_AT_location attribute with no contents indicates that a
21683 variable has been optimized away. */
21684 if (attr->form_is_block () && attr->as_block ()->size == 0)
21685 {
21686 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21687 return;
21688 }
21689
21690 /* Handle one degenerate form of location expression specially, to
21691 preserve GDB's previous behavior when section offsets are
21692 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21693 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21694
21695 if (attr->form_is_block ())
21696 {
21697 struct dwarf_block *block = attr->as_block ();
21698
21699 if ((block->data[0] == DW_OP_addr
21700 && block->size == 1 + cu_header->addr_size)
21701 || ((block->data[0] == DW_OP_GNU_addr_index
21702 || block->data[0] == DW_OP_addrx)
21703 && (block->size
21704 == 1 + leb128_size (&block->data[1]))))
21705 {
21706 unsigned int dummy;
21707
21708 if (block->data[0] == DW_OP_addr)
21709 sym->set_value_address
21710 (cu->header.read_address (objfile->obfd, block->data + 1,
21711 &dummy));
21712 else
21713 sym->set_value_address
21714 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
21715 sym->set_aclass_index (LOC_STATIC);
21716 fixup_symbol_section (sym, objfile);
21717 sym->set_value_address
21718 (sym->value_address ()
21719 + objfile->section_offsets[sym->section_index ()]);
21720 return;
21721 }
21722 }
21723
21724 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21725 expression evaluator, and use LOC_COMPUTED only when necessary
21726 (i.e. when the value of a register or memory location is
21727 referenced, or a thread-local block, etc.). Then again, it might
21728 not be worthwhile. I'm assuming that it isn't unless performance
21729 or memory numbers show me otherwise. */
21730
21731 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21732
21733 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21734 cu->has_loclist = true;
21735 }
21736
21737 /* Given a pointer to a DWARF information entry, figure out if we need
21738 to make a symbol table entry for it, and if so, create a new entry
21739 and return a pointer to it.
21740 If TYPE is NULL, determine symbol type from the die, otherwise
21741 used the passed type.
21742 If SPACE is not NULL, use it to hold the new symbol. If it is
21743 NULL, allocate a new symbol on the objfile's obstack. */
21744
21745 static struct symbol *
21746 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21747 struct symbol *space)
21748 {
21749 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21750 struct objfile *objfile = per_objfile->objfile;
21751 struct gdbarch *gdbarch = objfile->arch ();
21752 struct symbol *sym = NULL;
21753 const char *name;
21754 struct attribute *attr = NULL;
21755 struct attribute *attr2 = NULL;
21756 CORE_ADDR baseaddr;
21757 struct pending **list_to_add = NULL;
21758
21759 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21760
21761 baseaddr = objfile->text_section_offset ();
21762
21763 name = dwarf2_name (die, cu);
21764 if (name)
21765 {
21766 int suppress_add = 0;
21767
21768 if (space)
21769 sym = space;
21770 else
21771 sym = new (&objfile->objfile_obstack) symbol;
21772 OBJSTAT (objfile, n_syms++);
21773
21774 /* Cache this symbol's name and the name's demangled form (if any). */
21775 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
21776 /* Fortran does not have mangling standard and the mangling does differ
21777 between gfortran, iFort etc. */
21778 const char *physname
21779 = (cu->per_cu->lang == language_fortran
21780 ? dwarf2_full_name (name, die, cu)
21781 : dwarf2_physname (name, die, cu));
21782 const char *linkagename = dw2_linkage_name (die, cu);
21783
21784 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
21785 sym->set_linkage_name (physname);
21786 else
21787 {
21788 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21789 sym->set_linkage_name (linkagename);
21790 }
21791
21792 /* Handle DW_AT_artificial. */
21793 attr = dwarf2_attr (die, DW_AT_artificial, cu);
21794 if (attr != nullptr)
21795 sym->artificial = attr->as_boolean ();
21796
21797 /* Default assumptions.
21798 Use the passed type or decode it from the die. */
21799 sym->set_domain (VAR_DOMAIN);
21800 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21801 if (type != NULL)
21802 sym->set_type (type);
21803 else
21804 sym->set_type (die_type (die, cu));
21805 attr = dwarf2_attr (die,
21806 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21807 cu);
21808 if (attr != nullptr)
21809 sym->set_line (attr->constant_value (0));
21810
21811 attr = dwarf2_attr (die,
21812 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21813 cu);
21814 if (attr != nullptr && attr->is_nonnegative ())
21815 {
21816 file_name_index file_index
21817 = (file_name_index) attr->as_nonnegative ();
21818 struct file_entry *fe;
21819
21820 if (cu->line_header != NULL)
21821 fe = cu->line_header->file_name_at (file_index);
21822 else
21823 fe = NULL;
21824
21825 if (fe == NULL)
21826 complaint (_("file index out of range"));
21827 else
21828 symbol_set_symtab (sym, fe->symtab);
21829 }
21830
21831 switch (die->tag)
21832 {
21833 case DW_TAG_label:
21834 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21835 if (attr != nullptr)
21836 {
21837 CORE_ADDR addr;
21838
21839 addr = attr->as_address ();
21840 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21841 sym->set_value_address (addr);
21842 sym->set_aclass_index (LOC_LABEL);
21843 }
21844 else
21845 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21846 sym->set_type (objfile_type (objfile)->builtin_core_addr);
21847 sym->set_domain (LABEL_DOMAIN);
21848 add_symbol_to_list (sym, cu->list_in_scope);
21849 break;
21850 case DW_TAG_subprogram:
21851 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21852 finish_block. */
21853 sym->set_aclass_index (LOC_BLOCK);
21854 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21855 if ((attr2 != nullptr && attr2->as_boolean ())
21856 || cu->per_cu->lang == language_ada
21857 || cu->per_cu->lang == language_fortran)
21858 {
21859 /* Subprograms marked external are stored as a global symbol.
21860 Ada and Fortran subprograms, whether marked external or
21861 not, are always stored as a global symbol, because we want
21862 to be able to access them globally. For instance, we want
21863 to be able to break on a nested subprogram without having
21864 to specify the context. */
21865 list_to_add = cu->get_builder ()->get_global_symbols ();
21866 }
21867 else
21868 {
21869 list_to_add = cu->list_in_scope;
21870 }
21871 break;
21872 case DW_TAG_inlined_subroutine:
21873 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21874 finish_block. */
21875 sym->set_aclass_index (LOC_BLOCK);
21876 sym->set_is_inlined (1);
21877 list_to_add = cu->list_in_scope;
21878 break;
21879 case DW_TAG_template_value_param:
21880 suppress_add = 1;
21881 /* Fall through. */
21882 case DW_TAG_constant:
21883 case DW_TAG_variable:
21884 case DW_TAG_member:
21885 /* Compilation with minimal debug info may result in
21886 variables with missing type entries. Change the
21887 misleading `void' type to something sensible. */
21888 if (sym->type ()->code () == TYPE_CODE_VOID)
21889 sym->set_type (objfile_type (objfile)->builtin_int);
21890
21891 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21892 /* In the case of DW_TAG_member, we should only be called for
21893 static const members. */
21894 if (die->tag == DW_TAG_member)
21895 {
21896 /* dwarf2_add_field uses die_is_declaration,
21897 so we do the same. */
21898 gdb_assert (die_is_declaration (die, cu));
21899 gdb_assert (attr);
21900 }
21901 if (attr != nullptr)
21902 {
21903 dwarf2_const_value (attr, sym, cu);
21904 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21905 if (!suppress_add)
21906 {
21907 if (attr2 != nullptr && attr2->as_boolean ())
21908 list_to_add = cu->get_builder ()->get_global_symbols ();
21909 else
21910 list_to_add = cu->list_in_scope;
21911 }
21912 break;
21913 }
21914 attr = dwarf2_attr (die, DW_AT_location, cu);
21915 if (attr != nullptr)
21916 {
21917 var_decode_location (attr, sym, cu);
21918 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21919
21920 /* Fortran explicitly imports any global symbols to the local
21921 scope by DW_TAG_common_block. */
21922 if (cu->per_cu->lang == language_fortran && die->parent
21923 && die->parent->tag == DW_TAG_common_block)
21924 attr2 = NULL;
21925
21926 if (sym->aclass () == LOC_STATIC
21927 && sym->value_address () == 0
21928 && !per_objfile->per_bfd->has_section_at_zero)
21929 {
21930 /* When a static variable is eliminated by the linker,
21931 the corresponding debug information is not stripped
21932 out, but the variable address is set to null;
21933 do not add such variables into symbol table. */
21934 }
21935 else if (attr2 != nullptr && attr2->as_boolean ())
21936 {
21937 if (sym->aclass () == LOC_STATIC
21938 && (objfile->flags & OBJF_MAINLINE) == 0
21939 && per_objfile->per_bfd->can_copy)
21940 {
21941 /* A global static variable might be subject to
21942 copy relocation. We first check for a local
21943 minsym, though, because maybe the symbol was
21944 marked hidden, in which case this would not
21945 apply. */
21946 bound_minimal_symbol found
21947 = (lookup_minimal_symbol_linkage
21948 (sym->linkage_name (), objfile));
21949 if (found.minsym != nullptr)
21950 sym->maybe_copied = 1;
21951 }
21952
21953 /* A variable with DW_AT_external is never static,
21954 but it may be block-scoped. */
21955 list_to_add
21956 = ((cu->list_in_scope
21957 == cu->get_builder ()->get_file_symbols ())
21958 ? cu->get_builder ()->get_global_symbols ()
21959 : cu->list_in_scope);
21960 }
21961 else
21962 list_to_add = cu->list_in_scope;
21963 }
21964 else
21965 {
21966 /* We do not know the address of this symbol.
21967 If it is an external symbol and we have type information
21968 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21969 The address of the variable will then be determined from
21970 the minimal symbol table whenever the variable is
21971 referenced. */
21972 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21973
21974 /* Fortran explicitly imports any global symbols to the local
21975 scope by DW_TAG_common_block. */
21976 if (cu->per_cu->lang == language_fortran && die->parent
21977 && die->parent->tag == DW_TAG_common_block)
21978 {
21979 /* SYMBOL_CLASS doesn't matter here because
21980 read_common_block is going to reset it. */
21981 if (!suppress_add)
21982 list_to_add = cu->list_in_scope;
21983 }
21984 else if (attr2 != nullptr && attr2->as_boolean ()
21985 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21986 {
21987 /* A variable with DW_AT_external is never static, but it
21988 may be block-scoped. */
21989 list_to_add
21990 = ((cu->list_in_scope
21991 == cu->get_builder ()->get_file_symbols ())
21992 ? cu->get_builder ()->get_global_symbols ()
21993 : cu->list_in_scope);
21994
21995 sym->set_aclass_index (LOC_UNRESOLVED);
21996 }
21997 else if (!die_is_declaration (die, cu))
21998 {
21999 /* Use the default LOC_OPTIMIZED_OUT class. */
22000 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
22001 if (!suppress_add)
22002 list_to_add = cu->list_in_scope;
22003 }
22004 }
22005 break;
22006 case DW_TAG_formal_parameter:
22007 {
22008 /* If we are inside a function, mark this as an argument. If
22009 not, we might be looking at an argument to an inlined function
22010 when we do not have enough information to show inlined frames;
22011 pretend it's a local variable in that case so that the user can
22012 still see it. */
22013 struct context_stack *curr
22014 = cu->get_builder ()->get_current_context_stack ();
22015 if (curr != nullptr && curr->name != nullptr)
22016 sym->set_is_argument (1);
22017 attr = dwarf2_attr (die, DW_AT_location, cu);
22018 if (attr != nullptr)
22019 {
22020 var_decode_location (attr, sym, cu);
22021 }
22022 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22023 if (attr != nullptr)
22024 {
22025 dwarf2_const_value (attr, sym, cu);
22026 }
22027
22028 list_to_add = cu->list_in_scope;
22029 }
22030 break;
22031 case DW_TAG_unspecified_parameters:
22032 /* From varargs functions; gdb doesn't seem to have any
22033 interest in this information, so just ignore it for now.
22034 (FIXME?) */
22035 break;
22036 case DW_TAG_template_type_param:
22037 suppress_add = 1;
22038 /* Fall through. */
22039 case DW_TAG_class_type:
22040 case DW_TAG_interface_type:
22041 case DW_TAG_structure_type:
22042 case DW_TAG_union_type:
22043 case DW_TAG_set_type:
22044 case DW_TAG_enumeration_type:
22045 case DW_TAG_namelist:
22046 if (die->tag == DW_TAG_namelist)
22047 {
22048 sym->set_aclass_index (LOC_STATIC);
22049 sym->set_domain (VAR_DOMAIN);
22050 }
22051 else
22052 {
22053 sym->set_aclass_index (LOC_TYPEDEF);
22054 sym->set_domain (STRUCT_DOMAIN);
22055 }
22056 {
22057 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22058 really ever be static objects: otherwise, if you try
22059 to, say, break of a class's method and you're in a file
22060 which doesn't mention that class, it won't work unless
22061 the check for all static symbols in lookup_symbol_aux
22062 saves you. See the OtherFileClass tests in
22063 gdb.c++/namespace.exp. */
22064
22065 if (!suppress_add)
22066 {
22067 buildsym_compunit *builder = cu->get_builder ();
22068 list_to_add
22069 = (cu->list_in_scope == builder->get_file_symbols ()
22070 && cu->per_cu->lang == language_cplus
22071 ? builder->get_global_symbols ()
22072 : cu->list_in_scope);
22073
22074 /* The semantics of C++ state that "struct foo {
22075 ... }" also defines a typedef for "foo". */
22076 if (cu->per_cu->lang == language_cplus
22077 || cu->per_cu->lang == language_ada
22078 || cu->per_cu->lang == language_d
22079 || cu->per_cu->lang == language_rust)
22080 {
22081 /* The symbol's name is already allocated along
22082 with this objfile, so we don't need to
22083 duplicate it for the type. */
22084 if (sym->type ()->name () == 0)
22085 sym->type ()->set_name (sym->search_name ());
22086 }
22087 }
22088 }
22089 break;
22090 case DW_TAG_typedef:
22091 sym->set_aclass_index (LOC_TYPEDEF);
22092 sym->set_domain (VAR_DOMAIN);
22093 list_to_add = cu->list_in_scope;
22094 break;
22095 case DW_TAG_array_type:
22096 case DW_TAG_base_type:
22097 case DW_TAG_subrange_type:
22098 case DW_TAG_generic_subrange:
22099 sym->set_aclass_index (LOC_TYPEDEF);
22100 sym->set_domain (VAR_DOMAIN);
22101 list_to_add = cu->list_in_scope;
22102 break;
22103 case DW_TAG_enumerator:
22104 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22105 if (attr != nullptr)
22106 {
22107 dwarf2_const_value (attr, sym, cu);
22108 }
22109 {
22110 /* NOTE: carlton/2003-11-10: See comment above in the
22111 DW_TAG_class_type, etc. block. */
22112
22113 list_to_add
22114 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22115 && cu->per_cu->lang == language_cplus
22116 ? cu->get_builder ()->get_global_symbols ()
22117 : cu->list_in_scope);
22118 }
22119 break;
22120 case DW_TAG_imported_declaration:
22121 case DW_TAG_namespace:
22122 sym->set_aclass_index (LOC_TYPEDEF);
22123 list_to_add = cu->get_builder ()->get_global_symbols ();
22124 break;
22125 case DW_TAG_module:
22126 sym->set_aclass_index (LOC_TYPEDEF);
22127 sym->set_domain (MODULE_DOMAIN);
22128 list_to_add = cu->get_builder ()->get_global_symbols ();
22129 break;
22130 case DW_TAG_common_block:
22131 sym->set_aclass_index (LOC_COMMON_BLOCK);
22132 sym->set_domain (COMMON_BLOCK_DOMAIN);
22133 add_symbol_to_list (sym, cu->list_in_scope);
22134 break;
22135 default:
22136 /* Not a tag we recognize. Hopefully we aren't processing
22137 trash data, but since we must specifically ignore things
22138 we don't recognize, there is nothing else we should do at
22139 this point. */
22140 complaint (_("unsupported tag: '%s'"),
22141 dwarf_tag_name (die->tag));
22142 break;
22143 }
22144
22145 if (suppress_add)
22146 {
22147 sym->hash_next = objfile->template_symbols;
22148 objfile->template_symbols = sym;
22149 list_to_add = NULL;
22150 }
22151
22152 if (list_to_add != NULL)
22153 add_symbol_to_list (sym, list_to_add);
22154
22155 /* For the benefit of old versions of GCC, check for anonymous
22156 namespaces based on the demangled name. */
22157 if (!cu->processing_has_namespace_info
22158 && cu->per_cu->lang == language_cplus)
22159 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22160 }
22161 return (sym);
22162 }
22163
22164 /* Given an attr with a DW_FORM_dataN value in host byte order,
22165 zero-extend it as appropriate for the symbol's type. The DWARF
22166 standard (v4) is not entirely clear about the meaning of using
22167 DW_FORM_dataN for a constant with a signed type, where the type is
22168 wider than the data. The conclusion of a discussion on the DWARF
22169 list was that this is unspecified. We choose to always zero-extend
22170 because that is the interpretation long in use by GCC. */
22171
22172 static gdb_byte *
22173 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22174 struct dwarf2_cu *cu, LONGEST *value, int bits)
22175 {
22176 struct objfile *objfile = cu->per_objfile->objfile;
22177 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22178 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22179 LONGEST l = attr->constant_value (0);
22180
22181 if (bits < sizeof (*value) * 8)
22182 {
22183 l &= ((LONGEST) 1 << bits) - 1;
22184 *value = l;
22185 }
22186 else if (bits == sizeof (*value) * 8)
22187 *value = l;
22188 else
22189 {
22190 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22191 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22192 return bytes;
22193 }
22194
22195 return NULL;
22196 }
22197
22198 /* Read a constant value from an attribute. Either set *VALUE, or if
22199 the value does not fit in *VALUE, set *BYTES - either already
22200 allocated on the objfile obstack, or newly allocated on OBSTACK,
22201 or, set *BATON, if we translated the constant to a location
22202 expression. */
22203
22204 static void
22205 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22206 const char *name, struct obstack *obstack,
22207 struct dwarf2_cu *cu,
22208 LONGEST *value, const gdb_byte **bytes,
22209 struct dwarf2_locexpr_baton **baton)
22210 {
22211 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22212 struct objfile *objfile = per_objfile->objfile;
22213 struct comp_unit_head *cu_header = &cu->header;
22214 struct dwarf_block *blk;
22215 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22216 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22217
22218 *value = 0;
22219 *bytes = NULL;
22220 *baton = NULL;
22221
22222 switch (attr->form)
22223 {
22224 case DW_FORM_addr:
22225 case DW_FORM_addrx:
22226 case DW_FORM_GNU_addr_index:
22227 {
22228 gdb_byte *data;
22229
22230 if (TYPE_LENGTH (type) != cu_header->addr_size)
22231 dwarf2_const_value_length_mismatch_complaint (name,
22232 cu_header->addr_size,
22233 TYPE_LENGTH (type));
22234 /* Symbols of this form are reasonably rare, so we just
22235 piggyback on the existing location code rather than writing
22236 a new implementation of symbol_computed_ops. */
22237 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22238 (*baton)->per_objfile = per_objfile;
22239 (*baton)->per_cu = cu->per_cu;
22240 gdb_assert ((*baton)->per_cu);
22241
22242 (*baton)->size = 2 + cu_header->addr_size;
22243 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22244 (*baton)->data = data;
22245
22246 data[0] = DW_OP_addr;
22247 store_unsigned_integer (&data[1], cu_header->addr_size,
22248 byte_order, attr->as_address ());
22249 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22250 }
22251 break;
22252 case DW_FORM_string:
22253 case DW_FORM_strp:
22254 case DW_FORM_strx:
22255 case DW_FORM_GNU_str_index:
22256 case DW_FORM_GNU_strp_alt:
22257 /* The string is already allocated on the objfile obstack, point
22258 directly to it. */
22259 *bytes = (const gdb_byte *) attr->as_string ();
22260 break;
22261 case DW_FORM_block1:
22262 case DW_FORM_block2:
22263 case DW_FORM_block4:
22264 case DW_FORM_block:
22265 case DW_FORM_exprloc:
22266 case DW_FORM_data16:
22267 blk = attr->as_block ();
22268 if (TYPE_LENGTH (type) != blk->size)
22269 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22270 TYPE_LENGTH (type));
22271 *bytes = blk->data;
22272 break;
22273
22274 /* The DW_AT_const_value attributes are supposed to carry the
22275 symbol's value "represented as it would be on the target
22276 architecture." By the time we get here, it's already been
22277 converted to host endianness, so we just need to sign- or
22278 zero-extend it as appropriate. */
22279 case DW_FORM_data1:
22280 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22281 break;
22282 case DW_FORM_data2:
22283 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22284 break;
22285 case DW_FORM_data4:
22286 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22287 break;
22288 case DW_FORM_data8:
22289 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22290 break;
22291
22292 case DW_FORM_sdata:
22293 case DW_FORM_implicit_const:
22294 *value = attr->as_signed ();
22295 break;
22296
22297 case DW_FORM_udata:
22298 *value = attr->as_unsigned ();
22299 break;
22300
22301 default:
22302 complaint (_("unsupported const value attribute form: '%s'"),
22303 dwarf_form_name (attr->form));
22304 *value = 0;
22305 break;
22306 }
22307 }
22308
22309
22310 /* Copy constant value from an attribute to a symbol. */
22311
22312 static void
22313 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22314 struct dwarf2_cu *cu)
22315 {
22316 struct objfile *objfile = cu->per_objfile->objfile;
22317 LONGEST value;
22318 const gdb_byte *bytes;
22319 struct dwarf2_locexpr_baton *baton;
22320
22321 dwarf2_const_value_attr (attr, sym->type (),
22322 sym->print_name (),
22323 &objfile->objfile_obstack, cu,
22324 &value, &bytes, &baton);
22325
22326 if (baton != NULL)
22327 {
22328 SYMBOL_LOCATION_BATON (sym) = baton;
22329 sym->set_aclass_index (dwarf2_locexpr_index);
22330 }
22331 else if (bytes != NULL)
22332 {
22333 sym->set_value_bytes (bytes);
22334 sym->set_aclass_index (LOC_CONST_BYTES);
22335 }
22336 else
22337 {
22338 sym->set_value_longest (value);
22339 sym->set_aclass_index (LOC_CONST);
22340 }
22341 }
22342
22343 /* Return the type of the die in question using its DW_AT_type attribute. */
22344
22345 static struct type *
22346 die_type (struct die_info *die, struct dwarf2_cu *cu)
22347 {
22348 struct attribute *type_attr;
22349
22350 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22351 if (!type_attr)
22352 {
22353 struct objfile *objfile = cu->per_objfile->objfile;
22354 /* A missing DW_AT_type represents a void type. */
22355 return objfile_type (objfile)->builtin_void;
22356 }
22357
22358 return lookup_die_type (die, type_attr, cu);
22359 }
22360
22361 /* True iff CU's producer generates GNAT Ada auxiliary information
22362 that allows to find parallel types through that information instead
22363 of having to do expensive parallel lookups by type name. */
22364
22365 static int
22366 need_gnat_info (struct dwarf2_cu *cu)
22367 {
22368 /* Assume that the Ada compiler was GNAT, which always produces
22369 the auxiliary information. */
22370 return (cu->per_cu->lang == language_ada);
22371 }
22372
22373 /* Return the auxiliary type of the die in question using its
22374 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22375 attribute is not present. */
22376
22377 static struct type *
22378 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22379 {
22380 struct attribute *type_attr;
22381
22382 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22383 if (!type_attr)
22384 return NULL;
22385
22386 return lookup_die_type (die, type_attr, cu);
22387 }
22388
22389 /* If DIE has a descriptive_type attribute, then set the TYPE's
22390 descriptive type accordingly. */
22391
22392 static void
22393 set_descriptive_type (struct type *type, struct die_info *die,
22394 struct dwarf2_cu *cu)
22395 {
22396 struct type *descriptive_type = die_descriptive_type (die, cu);
22397
22398 if (descriptive_type)
22399 {
22400 ALLOCATE_GNAT_AUX_TYPE (type);
22401 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22402 }
22403 }
22404
22405 /* Return the containing type of the die in question using its
22406 DW_AT_containing_type attribute. */
22407
22408 static struct type *
22409 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22410 {
22411 struct attribute *type_attr;
22412 struct objfile *objfile = cu->per_objfile->objfile;
22413
22414 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22415 if (!type_attr)
22416 error (_("Dwarf Error: Problem turning containing type into gdb type "
22417 "[in module %s]"), objfile_name (objfile));
22418
22419 return lookup_die_type (die, type_attr, cu);
22420 }
22421
22422 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22423
22424 static struct type *
22425 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22426 {
22427 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22428 struct objfile *objfile = per_objfile->objfile;
22429 char *saved;
22430
22431 std::string message
22432 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22433 objfile_name (objfile),
22434 sect_offset_str (cu->header.sect_off),
22435 sect_offset_str (die->sect_off));
22436 saved = obstack_strdup (&objfile->objfile_obstack, message);
22437
22438 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22439 }
22440
22441 /* Look up the type of DIE in CU using its type attribute ATTR.
22442 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22443 DW_AT_containing_type.
22444 If there is no type substitute an error marker. */
22445
22446 static struct type *
22447 lookup_die_type (struct die_info *die, const struct attribute *attr,
22448 struct dwarf2_cu *cu)
22449 {
22450 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22451 struct objfile *objfile = per_objfile->objfile;
22452 struct type *this_type;
22453
22454 gdb_assert (attr->name == DW_AT_type
22455 || attr->name == DW_AT_GNAT_descriptive_type
22456 || attr->name == DW_AT_containing_type);
22457
22458 /* First see if we have it cached. */
22459
22460 if (attr->form == DW_FORM_GNU_ref_alt)
22461 {
22462 struct dwarf2_per_cu_data *per_cu;
22463 sect_offset sect_off = attr->get_ref_die_offset ();
22464
22465 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22466 per_objfile->per_bfd);
22467 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22468 }
22469 else if (attr->form_is_ref ())
22470 {
22471 sect_offset sect_off = attr->get_ref_die_offset ();
22472
22473 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22474 }
22475 else if (attr->form == DW_FORM_ref_sig8)
22476 {
22477 ULONGEST signature = attr->as_signature ();
22478
22479 return get_signatured_type (die, signature, cu);
22480 }
22481 else
22482 {
22483 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22484 " at %s [in module %s]"),
22485 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22486 objfile_name (objfile));
22487 return build_error_marker_type (cu, die);
22488 }
22489
22490 /* If not cached we need to read it in. */
22491
22492 if (this_type == NULL)
22493 {
22494 struct die_info *type_die = NULL;
22495 struct dwarf2_cu *type_cu = cu;
22496
22497 if (attr->form_is_ref ())
22498 type_die = follow_die_ref (die, attr, &type_cu);
22499 if (type_die == NULL)
22500 return build_error_marker_type (cu, die);
22501 /* If we find the type now, it's probably because the type came
22502 from an inter-CU reference and the type's CU got expanded before
22503 ours. */
22504 this_type = read_type_die (type_die, type_cu);
22505 }
22506
22507 /* If we still don't have a type use an error marker. */
22508
22509 if (this_type == NULL)
22510 return build_error_marker_type (cu, die);
22511
22512 return this_type;
22513 }
22514
22515 /* Return the type in DIE, CU.
22516 Returns NULL for invalid types.
22517
22518 This first does a lookup in die_type_hash,
22519 and only reads the die in if necessary.
22520
22521 NOTE: This can be called when reading in partial or full symbols. */
22522
22523 static struct type *
22524 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22525 {
22526 struct type *this_type;
22527
22528 this_type = get_die_type (die, cu);
22529 if (this_type)
22530 return this_type;
22531
22532 return read_type_die_1 (die, cu);
22533 }
22534
22535 /* Read the type in DIE, CU.
22536 Returns NULL for invalid types. */
22537
22538 static struct type *
22539 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22540 {
22541 struct type *this_type = NULL;
22542
22543 switch (die->tag)
22544 {
22545 case DW_TAG_class_type:
22546 case DW_TAG_interface_type:
22547 case DW_TAG_structure_type:
22548 case DW_TAG_union_type:
22549 this_type = read_structure_type (die, cu);
22550 break;
22551 case DW_TAG_enumeration_type:
22552 this_type = read_enumeration_type (die, cu);
22553 break;
22554 case DW_TAG_subprogram:
22555 case DW_TAG_subroutine_type:
22556 case DW_TAG_inlined_subroutine:
22557 this_type = read_subroutine_type (die, cu);
22558 break;
22559 case DW_TAG_array_type:
22560 this_type = read_array_type (die, cu);
22561 break;
22562 case DW_TAG_set_type:
22563 this_type = read_set_type (die, cu);
22564 break;
22565 case DW_TAG_pointer_type:
22566 this_type = read_tag_pointer_type (die, cu);
22567 break;
22568 case DW_TAG_ptr_to_member_type:
22569 this_type = read_tag_ptr_to_member_type (die, cu);
22570 break;
22571 case DW_TAG_reference_type:
22572 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22573 break;
22574 case DW_TAG_rvalue_reference_type:
22575 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22576 break;
22577 case DW_TAG_const_type:
22578 this_type = read_tag_const_type (die, cu);
22579 break;
22580 case DW_TAG_volatile_type:
22581 this_type = read_tag_volatile_type (die, cu);
22582 break;
22583 case DW_TAG_restrict_type:
22584 this_type = read_tag_restrict_type (die, cu);
22585 break;
22586 case DW_TAG_string_type:
22587 this_type = read_tag_string_type (die, cu);
22588 break;
22589 case DW_TAG_typedef:
22590 this_type = read_typedef (die, cu);
22591 break;
22592 case DW_TAG_generic_subrange:
22593 case DW_TAG_subrange_type:
22594 this_type = read_subrange_type (die, cu);
22595 break;
22596 case DW_TAG_base_type:
22597 this_type = read_base_type (die, cu);
22598 break;
22599 case DW_TAG_unspecified_type:
22600 this_type = read_unspecified_type (die, cu);
22601 break;
22602 case DW_TAG_namespace:
22603 this_type = read_namespace_type (die, cu);
22604 break;
22605 case DW_TAG_module:
22606 this_type = read_module_type (die, cu);
22607 break;
22608 case DW_TAG_atomic_type:
22609 this_type = read_tag_atomic_type (die, cu);
22610 break;
22611 default:
22612 complaint (_("unexpected tag in read_type_die: '%s'"),
22613 dwarf_tag_name (die->tag));
22614 break;
22615 }
22616
22617 return this_type;
22618 }
22619
22620 /* See if we can figure out if the class lives in a namespace. We do
22621 this by looking for a member function; its demangled name will
22622 contain namespace info, if there is any.
22623 Return the computed name or NULL.
22624 Space for the result is allocated on the objfile's obstack.
22625 This is the full-die version of guess_partial_die_structure_name.
22626 In this case we know DIE has no useful parent. */
22627
22628 static const char *
22629 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22630 {
22631 struct die_info *spec_die;
22632 struct dwarf2_cu *spec_cu;
22633 struct die_info *child;
22634 struct objfile *objfile = cu->per_objfile->objfile;
22635
22636 spec_cu = cu;
22637 spec_die = die_specification (die, &spec_cu);
22638 if (spec_die != NULL)
22639 {
22640 die = spec_die;
22641 cu = spec_cu;
22642 }
22643
22644 for (child = die->child;
22645 child != NULL;
22646 child = child->sibling)
22647 {
22648 if (child->tag == DW_TAG_subprogram)
22649 {
22650 const char *linkage_name = dw2_linkage_name (child, cu);
22651
22652 if (linkage_name != NULL)
22653 {
22654 gdb::unique_xmalloc_ptr<char> actual_name
22655 (cu->language_defn->class_name_from_physname (linkage_name));
22656 const char *name = NULL;
22657
22658 if (actual_name != NULL)
22659 {
22660 const char *die_name = dwarf2_name (die, cu);
22661
22662 if (die_name != NULL
22663 && strcmp (die_name, actual_name.get ()) != 0)
22664 {
22665 /* Strip off the class name from the full name.
22666 We want the prefix. */
22667 int die_name_len = strlen (die_name);
22668 int actual_name_len = strlen (actual_name.get ());
22669 const char *ptr = actual_name.get ();
22670
22671 /* Test for '::' as a sanity check. */
22672 if (actual_name_len > die_name_len + 2
22673 && ptr[actual_name_len - die_name_len - 1] == ':')
22674 name = obstack_strndup (
22675 &objfile->per_bfd->storage_obstack,
22676 ptr, actual_name_len - die_name_len - 2);
22677 }
22678 }
22679 return name;
22680 }
22681 }
22682 }
22683
22684 return NULL;
22685 }
22686
22687 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22688 prefix part in such case. See
22689 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22690
22691 static const char *
22692 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22693 {
22694 struct attribute *attr;
22695 const char *base;
22696
22697 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22698 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22699 return NULL;
22700
22701 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22702 return NULL;
22703
22704 attr = dw2_linkage_name_attr (die, cu);
22705 const char *attr_name = attr->as_string ();
22706 if (attr == NULL || attr_name == NULL)
22707 return NULL;
22708
22709 /* dwarf2_name had to be already called. */
22710 gdb_assert (attr->canonical_string_p ());
22711
22712 /* Strip the base name, keep any leading namespaces/classes. */
22713 base = strrchr (attr_name, ':');
22714 if (base == NULL || base == attr_name || base[-1] != ':')
22715 return "";
22716
22717 struct objfile *objfile = cu->per_objfile->objfile;
22718 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22719 attr_name,
22720 &base[-1] - attr_name);
22721 }
22722
22723 /* Return the name of the namespace/class that DIE is defined within,
22724 or "" if we can't tell. The caller should not xfree the result.
22725
22726 For example, if we're within the method foo() in the following
22727 code:
22728
22729 namespace N {
22730 class C {
22731 void foo () {
22732 }
22733 };
22734 }
22735
22736 then determine_prefix on foo's die will return "N::C". */
22737
22738 static const char *
22739 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22740 {
22741 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22742 struct die_info *parent, *spec_die;
22743 struct dwarf2_cu *spec_cu;
22744 struct type *parent_type;
22745 const char *retval;
22746
22747 if (cu->per_cu->lang != language_cplus
22748 && cu->per_cu->lang != language_fortran
22749 && cu->per_cu->lang != language_d
22750 && cu->per_cu->lang != language_rust)
22751 return "";
22752
22753 retval = anonymous_struct_prefix (die, cu);
22754 if (retval)
22755 return retval;
22756
22757 /* We have to be careful in the presence of DW_AT_specification.
22758 For example, with GCC 3.4, given the code
22759
22760 namespace N {
22761 void foo() {
22762 // Definition of N::foo.
22763 }
22764 }
22765
22766 then we'll have a tree of DIEs like this:
22767
22768 1: DW_TAG_compile_unit
22769 2: DW_TAG_namespace // N
22770 3: DW_TAG_subprogram // declaration of N::foo
22771 4: DW_TAG_subprogram // definition of N::foo
22772 DW_AT_specification // refers to die #3
22773
22774 Thus, when processing die #4, we have to pretend that we're in
22775 the context of its DW_AT_specification, namely the contex of die
22776 #3. */
22777 spec_cu = cu;
22778 spec_die = die_specification (die, &spec_cu);
22779 if (spec_die == NULL)
22780 parent = die->parent;
22781 else
22782 {
22783 parent = spec_die->parent;
22784 cu = spec_cu;
22785 }
22786
22787 if (parent == NULL)
22788 return "";
22789 else if (parent->building_fullname)
22790 {
22791 const char *name;
22792 const char *parent_name;
22793
22794 /* It has been seen on RealView 2.2 built binaries,
22795 DW_TAG_template_type_param types actually _defined_ as
22796 children of the parent class:
22797
22798 enum E {};
22799 template class <class Enum> Class{};
22800 Class<enum E> class_e;
22801
22802 1: DW_TAG_class_type (Class)
22803 2: DW_TAG_enumeration_type (E)
22804 3: DW_TAG_enumerator (enum1:0)
22805 3: DW_TAG_enumerator (enum2:1)
22806 ...
22807 2: DW_TAG_template_type_param
22808 DW_AT_type DW_FORM_ref_udata (E)
22809
22810 Besides being broken debug info, it can put GDB into an
22811 infinite loop. Consider:
22812
22813 When we're building the full name for Class<E>, we'll start
22814 at Class, and go look over its template type parameters,
22815 finding E. We'll then try to build the full name of E, and
22816 reach here. We're now trying to build the full name of E,
22817 and look over the parent DIE for containing scope. In the
22818 broken case, if we followed the parent DIE of E, we'd again
22819 find Class, and once again go look at its template type
22820 arguments, etc., etc. Simply don't consider such parent die
22821 as source-level parent of this die (it can't be, the language
22822 doesn't allow it), and break the loop here. */
22823 name = dwarf2_name (die, cu);
22824 parent_name = dwarf2_name (parent, cu);
22825 complaint (_("template param type '%s' defined within parent '%s'"),
22826 name ? name : "<unknown>",
22827 parent_name ? parent_name : "<unknown>");
22828 return "";
22829 }
22830 else
22831 switch (parent->tag)
22832 {
22833 case DW_TAG_namespace:
22834 parent_type = read_type_die (parent, cu);
22835 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22836 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22837 Work around this problem here. */
22838 if (cu->per_cu->lang == language_cplus
22839 && strcmp (parent_type->name (), "::") == 0)
22840 return "";
22841 /* We give a name to even anonymous namespaces. */
22842 return parent_type->name ();
22843 case DW_TAG_class_type:
22844 case DW_TAG_interface_type:
22845 case DW_TAG_structure_type:
22846 case DW_TAG_union_type:
22847 case DW_TAG_module:
22848 parent_type = read_type_die (parent, cu);
22849 if (parent_type->name () != NULL)
22850 return parent_type->name ();
22851 else
22852 /* An anonymous structure is only allowed non-static data
22853 members; no typedefs, no member functions, et cetera.
22854 So it does not need a prefix. */
22855 return "";
22856 case DW_TAG_compile_unit:
22857 case DW_TAG_partial_unit:
22858 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22859 if (cu->per_cu->lang == language_cplus
22860 && !per_objfile->per_bfd->types.empty ()
22861 && die->child != NULL
22862 && (die->tag == DW_TAG_class_type
22863 || die->tag == DW_TAG_structure_type
22864 || die->tag == DW_TAG_union_type))
22865 {
22866 const char *name = guess_full_die_structure_name (die, cu);
22867 if (name != NULL)
22868 return name;
22869 }
22870 return "";
22871 case DW_TAG_subprogram:
22872 /* Nested subroutines in Fortran get a prefix with the name
22873 of the parent's subroutine. */
22874 if (cu->per_cu->lang == language_fortran)
22875 {
22876 if ((die->tag == DW_TAG_subprogram)
22877 && (dwarf2_name (parent, cu) != NULL))
22878 return dwarf2_name (parent, cu);
22879 }
22880 return "";
22881 case DW_TAG_enumeration_type:
22882 parent_type = read_type_die (parent, cu);
22883 if (parent_type->is_declared_class ())
22884 {
22885 if (parent_type->name () != NULL)
22886 return parent_type->name ();
22887 return "";
22888 }
22889 /* Fall through. */
22890 default:
22891 return determine_prefix (parent, cu);
22892 }
22893 }
22894
22895 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22896 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22897 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22898 an obconcat, otherwise allocate storage for the result. The CU argument is
22899 used to determine the language and hence, the appropriate separator. */
22900
22901 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22902
22903 static char *
22904 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22905 int physname, struct dwarf2_cu *cu)
22906 {
22907 const char *lead = "";
22908 const char *sep;
22909
22910 if (suffix == NULL || suffix[0] == '\0'
22911 || prefix == NULL || prefix[0] == '\0')
22912 sep = "";
22913 else if (cu->per_cu->lang == language_d)
22914 {
22915 /* For D, the 'main' function could be defined in any module, but it
22916 should never be prefixed. */
22917 if (strcmp (suffix, "D main") == 0)
22918 {
22919 prefix = "";
22920 sep = "";
22921 }
22922 else
22923 sep = ".";
22924 }
22925 else if (cu->per_cu->lang == language_fortran && physname)
22926 {
22927 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22928 DW_AT_MIPS_linkage_name is preferred and used instead. */
22929
22930 lead = "__";
22931 sep = "_MOD_";
22932 }
22933 else
22934 sep = "::";
22935
22936 if (prefix == NULL)
22937 prefix = "";
22938 if (suffix == NULL)
22939 suffix = "";
22940
22941 if (obs == NULL)
22942 {
22943 char *retval
22944 = ((char *)
22945 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22946
22947 strcpy (retval, lead);
22948 strcat (retval, prefix);
22949 strcat (retval, sep);
22950 strcat (retval, suffix);
22951 return retval;
22952 }
22953 else
22954 {
22955 /* We have an obstack. */
22956 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22957 }
22958 }
22959
22960 /* Get name of a die, return NULL if not found. */
22961
22962 static const char *
22963 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22964 struct objfile *objfile)
22965 {
22966 if (name && cu->per_cu->lang == language_cplus)
22967 {
22968 gdb::unique_xmalloc_ptr<char> canon_name
22969 = cp_canonicalize_string (name);
22970
22971 if (canon_name != nullptr)
22972 name = objfile->intern (canon_name.get ());
22973 }
22974
22975 return name;
22976 }
22977
22978 /* Get name of a die, return NULL if not found.
22979 Anonymous namespaces are converted to their magic string. */
22980
22981 static const char *
22982 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22983 {
22984 struct attribute *attr;
22985 struct objfile *objfile = cu->per_objfile->objfile;
22986
22987 attr = dwarf2_attr (die, DW_AT_name, cu);
22988 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22989 if (attr_name == nullptr
22990 && die->tag != DW_TAG_namespace
22991 && die->tag != DW_TAG_class_type
22992 && die->tag != DW_TAG_interface_type
22993 && die->tag != DW_TAG_structure_type
22994 && die->tag != DW_TAG_namelist
22995 && die->tag != DW_TAG_union_type)
22996 return NULL;
22997
22998 switch (die->tag)
22999 {
23000 case DW_TAG_compile_unit:
23001 case DW_TAG_partial_unit:
23002 /* Compilation units have a DW_AT_name that is a filename, not
23003 a source language identifier. */
23004 case DW_TAG_enumeration_type:
23005 case DW_TAG_enumerator:
23006 /* These tags always have simple identifiers already; no need
23007 to canonicalize them. */
23008 return attr_name;
23009
23010 case DW_TAG_namespace:
23011 if (attr_name != nullptr)
23012 return attr_name;
23013 return CP_ANONYMOUS_NAMESPACE_STR;
23014
23015 case DW_TAG_class_type:
23016 case DW_TAG_interface_type:
23017 case DW_TAG_structure_type:
23018 case DW_TAG_union_type:
23019 case DW_TAG_namelist:
23020 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23021 structures or unions. These were of the form "._%d" in GCC 4.1,
23022 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23023 and GCC 4.4. We work around this problem by ignoring these. */
23024 if (attr_name != nullptr
23025 && (startswith (attr_name, "._")
23026 || startswith (attr_name, "<anonymous")))
23027 return NULL;
23028
23029 /* GCC might emit a nameless typedef that has a linkage name. See
23030 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23031 if (!attr || attr_name == NULL)
23032 {
23033 attr = dw2_linkage_name_attr (die, cu);
23034 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23035 if (attr == NULL || attr_name == NULL)
23036 return NULL;
23037
23038 /* Avoid demangling attr_name the second time on a second
23039 call for the same DIE. */
23040 if (!attr->canonical_string_p ())
23041 {
23042 gdb::unique_xmalloc_ptr<char> demangled
23043 (gdb_demangle (attr_name, DMGL_TYPES));
23044 if (demangled == nullptr)
23045 return nullptr;
23046
23047 attr->set_string_canonical (objfile->intern (demangled.get ()));
23048 attr_name = attr->as_string ();
23049 }
23050
23051 /* Strip any leading namespaces/classes, keep only the
23052 base name. DW_AT_name for named DIEs does not
23053 contain the prefixes. */
23054 const char *base = strrchr (attr_name, ':');
23055 if (base && base > attr_name && base[-1] == ':')
23056 return &base[1];
23057 else
23058 return attr_name;
23059 }
23060 break;
23061
23062 default:
23063 break;
23064 }
23065
23066 if (!attr->canonical_string_p ())
23067 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23068 objfile));
23069 return attr->as_string ();
23070 }
23071
23072 /* Return the die that this die in an extension of, or NULL if there
23073 is none. *EXT_CU is the CU containing DIE on input, and the CU
23074 containing the return value on output. */
23075
23076 static struct die_info *
23077 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23078 {
23079 struct attribute *attr;
23080
23081 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23082 if (attr == NULL)
23083 return NULL;
23084
23085 return follow_die_ref (die, attr, ext_cu);
23086 }
23087
23088 static void
23089 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23090 {
23091 unsigned int i;
23092
23093 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
23094 indent, "",
23095 dwarf_tag_name (die->tag), die->abbrev,
23096 sect_offset_str (die->sect_off));
23097
23098 if (die->parent != NULL)
23099 gdb_printf (f, "%*s parent at offset: %s\n",
23100 indent, "",
23101 sect_offset_str (die->parent->sect_off));
23102
23103 gdb_printf (f, "%*s has children: %s\n",
23104 indent, "",
23105 dwarf_bool_name (die->child != NULL));
23106
23107 gdb_printf (f, "%*s attributes:\n", indent, "");
23108
23109 for (i = 0; i < die->num_attrs; ++i)
23110 {
23111 gdb_printf (f, "%*s %s (%s) ",
23112 indent, "",
23113 dwarf_attr_name (die->attrs[i].name),
23114 dwarf_form_name (die->attrs[i].form));
23115
23116 switch (die->attrs[i].form)
23117 {
23118 case DW_FORM_addr:
23119 case DW_FORM_addrx:
23120 case DW_FORM_GNU_addr_index:
23121 gdb_printf (f, "address: ");
23122 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
23123 break;
23124 case DW_FORM_block2:
23125 case DW_FORM_block4:
23126 case DW_FORM_block:
23127 case DW_FORM_block1:
23128 gdb_printf (f, "block: size %s",
23129 pulongest (die->attrs[i].as_block ()->size));
23130 break;
23131 case DW_FORM_exprloc:
23132 gdb_printf (f, "expression: size %s",
23133 pulongest (die->attrs[i].as_block ()->size));
23134 break;
23135 case DW_FORM_data16:
23136 gdb_printf (f, "constant of 16 bytes");
23137 break;
23138 case DW_FORM_ref_addr:
23139 gdb_printf (f, "ref address: ");
23140 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
23141 break;
23142 case DW_FORM_GNU_ref_alt:
23143 gdb_printf (f, "alt ref address: ");
23144 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
23145 break;
23146 case DW_FORM_ref1:
23147 case DW_FORM_ref2:
23148 case DW_FORM_ref4:
23149 case DW_FORM_ref8:
23150 case DW_FORM_ref_udata:
23151 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
23152 (long) (die->attrs[i].as_unsigned ()));
23153 break;
23154 case DW_FORM_data1:
23155 case DW_FORM_data2:
23156 case DW_FORM_data4:
23157 case DW_FORM_data8:
23158 case DW_FORM_udata:
23159 gdb_printf (f, "constant: %s",
23160 pulongest (die->attrs[i].as_unsigned ()));
23161 break;
23162 case DW_FORM_sec_offset:
23163 gdb_printf (f, "section offset: %s",
23164 pulongest (die->attrs[i].as_unsigned ()));
23165 break;
23166 case DW_FORM_ref_sig8:
23167 gdb_printf (f, "signature: %s",
23168 hex_string (die->attrs[i].as_signature ()));
23169 break;
23170 case DW_FORM_string:
23171 case DW_FORM_strp:
23172 case DW_FORM_line_strp:
23173 case DW_FORM_strx:
23174 case DW_FORM_GNU_str_index:
23175 case DW_FORM_GNU_strp_alt:
23176 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
23177 die->attrs[i].as_string ()
23178 ? die->attrs[i].as_string () : "",
23179 die->attrs[i].canonical_string_p () ? "is" : "not");
23180 break;
23181 case DW_FORM_flag:
23182 if (die->attrs[i].as_boolean ())
23183 gdb_printf (f, "flag: TRUE");
23184 else
23185 gdb_printf (f, "flag: FALSE");
23186 break;
23187 case DW_FORM_flag_present:
23188 gdb_printf (f, "flag: TRUE");
23189 break;
23190 case DW_FORM_indirect:
23191 /* The reader will have reduced the indirect form to
23192 the "base form" so this form should not occur. */
23193 gdb_printf (f,
23194 "unexpected attribute form: DW_FORM_indirect");
23195 break;
23196 case DW_FORM_sdata:
23197 case DW_FORM_implicit_const:
23198 gdb_printf (f, "constant: %s",
23199 plongest (die->attrs[i].as_signed ()));
23200 break;
23201 default:
23202 gdb_printf (f, "unsupported attribute form: %d.",
23203 die->attrs[i].form);
23204 break;
23205 }
23206 gdb_printf (f, "\n");
23207 }
23208 }
23209
23210 static void
23211 dump_die_for_error (struct die_info *die)
23212 {
23213 dump_die_shallow (gdb_stderr, 0, die);
23214 }
23215
23216 static void
23217 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23218 {
23219 int indent = level * 4;
23220
23221 gdb_assert (die != NULL);
23222
23223 if (level >= max_level)
23224 return;
23225
23226 dump_die_shallow (f, indent, die);
23227
23228 if (die->child != NULL)
23229 {
23230 gdb_printf (f, "%*s Children:", indent, "");
23231 if (level + 1 < max_level)
23232 {
23233 gdb_printf (f, "\n");
23234 dump_die_1 (f, level + 1, max_level, die->child);
23235 }
23236 else
23237 {
23238 gdb_printf (f,
23239 " [not printed, max nesting level reached]\n");
23240 }
23241 }
23242
23243 if (die->sibling != NULL && level > 0)
23244 {
23245 dump_die_1 (f, level, max_level, die->sibling);
23246 }
23247 }
23248
23249 /* This is called from the pdie macro in gdbinit.in.
23250 It's not static so gcc will keep a copy callable from gdb. */
23251
23252 void
23253 dump_die (struct die_info *die, int max_level)
23254 {
23255 dump_die_1 (gdb_stdlog, 0, max_level, die);
23256 }
23257
23258 static void
23259 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23260 {
23261 void **slot;
23262
23263 slot = htab_find_slot_with_hash (cu->die_hash, die,
23264 to_underlying (die->sect_off),
23265 INSERT);
23266
23267 *slot = die;
23268 }
23269
23270 /* Follow reference or signature attribute ATTR of SRC_DIE.
23271 On entry *REF_CU is the CU of SRC_DIE.
23272 On exit *REF_CU is the CU of the result. */
23273
23274 static struct die_info *
23275 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23276 struct dwarf2_cu **ref_cu)
23277 {
23278 struct die_info *die;
23279
23280 if (attr->form_is_ref ())
23281 die = follow_die_ref (src_die, attr, ref_cu);
23282 else if (attr->form == DW_FORM_ref_sig8)
23283 die = follow_die_sig (src_die, attr, ref_cu);
23284 else
23285 {
23286 dump_die_for_error (src_die);
23287 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23288 objfile_name ((*ref_cu)->per_objfile->objfile));
23289 }
23290
23291 return die;
23292 }
23293
23294 /* Follow reference OFFSET.
23295 On entry *REF_CU is the CU of the source die referencing OFFSET.
23296 On exit *REF_CU is the CU of the result.
23297 Returns NULL if OFFSET is invalid. */
23298
23299 static struct die_info *
23300 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23301 struct dwarf2_cu **ref_cu)
23302 {
23303 struct die_info temp_die;
23304 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23305 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23306
23307 gdb_assert (cu->per_cu != NULL);
23308
23309 target_cu = cu;
23310
23311 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23312 "source CU contains target offset: %d",
23313 sect_offset_str (cu->per_cu->sect_off),
23314 sect_offset_str (sect_off),
23315 cu->header.offset_in_cu_p (sect_off));
23316
23317 if (cu->per_cu->is_debug_types)
23318 {
23319 /* .debug_types CUs cannot reference anything outside their CU.
23320 If they need to, they have to reference a signatured type via
23321 DW_FORM_ref_sig8. */
23322 if (!cu->header.offset_in_cu_p (sect_off))
23323 return NULL;
23324 }
23325 else if (offset_in_dwz != cu->per_cu->is_dwz
23326 || !cu->header.offset_in_cu_p (sect_off))
23327 {
23328 struct dwarf2_per_cu_data *per_cu;
23329
23330 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23331 per_objfile->per_bfd);
23332
23333 dwarf_read_debug_printf_v ("target CU offset: %s, "
23334 "target CU DIEs loaded: %d",
23335 sect_offset_str (per_cu->sect_off),
23336 per_objfile->get_cu (per_cu) != nullptr);
23337
23338 /* If necessary, add it to the queue and load its DIEs.
23339
23340 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23341 it doesn't mean they are currently loaded. Since we require them
23342 to be loaded, we must check for ourselves. */
23343 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
23344 || per_objfile->get_cu (per_cu) == nullptr)
23345 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23346 false, cu->per_cu->lang);
23347
23348 target_cu = per_objfile->get_cu (per_cu);
23349 gdb_assert (target_cu != nullptr);
23350 }
23351 else if (cu->dies == NULL)
23352 {
23353 /* We're loading full DIEs during partial symbol reading. */
23354 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23355 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23356 language_minimal);
23357 }
23358
23359 *ref_cu = target_cu;
23360 temp_die.sect_off = sect_off;
23361
23362 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23363 &temp_die,
23364 to_underlying (sect_off));
23365 }
23366
23367 /* Follow reference attribute ATTR of SRC_DIE.
23368 On entry *REF_CU is the CU of SRC_DIE.
23369 On exit *REF_CU is the CU of the result. */
23370
23371 static struct die_info *
23372 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23373 struct dwarf2_cu **ref_cu)
23374 {
23375 sect_offset sect_off = attr->get_ref_die_offset ();
23376 struct dwarf2_cu *cu = *ref_cu;
23377 struct die_info *die;
23378
23379 die = follow_die_offset (sect_off,
23380 (attr->form == DW_FORM_GNU_ref_alt
23381 || cu->per_cu->is_dwz),
23382 ref_cu);
23383 if (!die)
23384 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23385 "at %s [in module %s]"),
23386 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23387 objfile_name (cu->per_objfile->objfile));
23388
23389 return die;
23390 }
23391
23392 /* See read.h. */
23393
23394 struct dwarf2_locexpr_baton
23395 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23396 dwarf2_per_cu_data *per_cu,
23397 dwarf2_per_objfile *per_objfile,
23398 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23399 bool resolve_abstract_p)
23400 {
23401 struct die_info *die;
23402 struct attribute *attr;
23403 struct dwarf2_locexpr_baton retval;
23404 struct objfile *objfile = per_objfile->objfile;
23405
23406 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23407 if (cu == nullptr)
23408 cu = load_cu (per_cu, per_objfile, false);
23409
23410 if (cu == nullptr)
23411 {
23412 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23413 Instead just throw an error, not much else we can do. */
23414 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23415 sect_offset_str (sect_off), objfile_name (objfile));
23416 }
23417
23418 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23419 if (!die)
23420 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23421 sect_offset_str (sect_off), objfile_name (objfile));
23422
23423 attr = dwarf2_attr (die, DW_AT_location, cu);
23424 if (!attr && resolve_abstract_p
23425 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23426 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23427 {
23428 CORE_ADDR pc = get_frame_pc ();
23429 CORE_ADDR baseaddr = objfile->text_section_offset ();
23430 struct gdbarch *gdbarch = objfile->arch ();
23431
23432 for (const auto &cand_off
23433 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23434 {
23435 struct dwarf2_cu *cand_cu = cu;
23436 struct die_info *cand
23437 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23438 if (!cand
23439 || !cand->parent
23440 || cand->parent->tag != DW_TAG_subprogram)
23441 continue;
23442
23443 CORE_ADDR pc_low, pc_high;
23444 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23445 if (pc_low == ((CORE_ADDR) -1))
23446 continue;
23447 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23448 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23449 if (!(pc_low <= pc && pc < pc_high))
23450 continue;
23451
23452 die = cand;
23453 attr = dwarf2_attr (die, DW_AT_location, cu);
23454 break;
23455 }
23456 }
23457
23458 if (!attr)
23459 {
23460 /* DWARF: "If there is no such attribute, then there is no effect.".
23461 DATA is ignored if SIZE is 0. */
23462
23463 retval.data = NULL;
23464 retval.size = 0;
23465 }
23466 else if (attr->form_is_section_offset ())
23467 {
23468 struct dwarf2_loclist_baton loclist_baton;
23469 CORE_ADDR pc = get_frame_pc ();
23470 size_t size;
23471
23472 fill_in_loclist_baton (cu, &loclist_baton, attr);
23473
23474 retval.data = dwarf2_find_location_expression (&loclist_baton,
23475 &size, pc);
23476 retval.size = size;
23477 }
23478 else
23479 {
23480 if (!attr->form_is_block ())
23481 error (_("Dwarf Error: DIE at %s referenced in module %s "
23482 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23483 sect_offset_str (sect_off), objfile_name (objfile));
23484
23485 struct dwarf_block *block = attr->as_block ();
23486 retval.data = block->data;
23487 retval.size = block->size;
23488 }
23489 retval.per_objfile = per_objfile;
23490 retval.per_cu = cu->per_cu;
23491
23492 per_objfile->age_comp_units ();
23493
23494 return retval;
23495 }
23496
23497 /* See read.h. */
23498
23499 struct dwarf2_locexpr_baton
23500 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23501 dwarf2_per_cu_data *per_cu,
23502 dwarf2_per_objfile *per_objfile,
23503 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23504 {
23505 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23506
23507 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23508 get_frame_pc);
23509 }
23510
23511 /* Write a constant of a given type as target-ordered bytes into
23512 OBSTACK. */
23513
23514 static const gdb_byte *
23515 write_constant_as_bytes (struct obstack *obstack,
23516 enum bfd_endian byte_order,
23517 struct type *type,
23518 ULONGEST value,
23519 LONGEST *len)
23520 {
23521 gdb_byte *result;
23522
23523 *len = TYPE_LENGTH (type);
23524 result = (gdb_byte *) obstack_alloc (obstack, *len);
23525 store_unsigned_integer (result, *len, byte_order, value);
23526
23527 return result;
23528 }
23529
23530 /* See read.h. */
23531
23532 const gdb_byte *
23533 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23534 dwarf2_per_cu_data *per_cu,
23535 dwarf2_per_objfile *per_objfile,
23536 obstack *obstack,
23537 LONGEST *len)
23538 {
23539 struct die_info *die;
23540 struct attribute *attr;
23541 const gdb_byte *result = NULL;
23542 struct type *type;
23543 LONGEST value;
23544 enum bfd_endian byte_order;
23545 struct objfile *objfile = per_objfile->objfile;
23546
23547 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23548 if (cu == nullptr)
23549 cu = load_cu (per_cu, per_objfile, false);
23550
23551 if (cu == nullptr)
23552 {
23553 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23554 Instead just throw an error, not much else we can do. */
23555 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23556 sect_offset_str (sect_off), objfile_name (objfile));
23557 }
23558
23559 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23560 if (!die)
23561 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23562 sect_offset_str (sect_off), objfile_name (objfile));
23563
23564 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23565 if (attr == NULL)
23566 return NULL;
23567
23568 byte_order = (bfd_big_endian (objfile->obfd)
23569 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23570
23571 switch (attr->form)
23572 {
23573 case DW_FORM_addr:
23574 case DW_FORM_addrx:
23575 case DW_FORM_GNU_addr_index:
23576 {
23577 gdb_byte *tem;
23578
23579 *len = cu->header.addr_size;
23580 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23581 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23582 result = tem;
23583 }
23584 break;
23585 case DW_FORM_string:
23586 case DW_FORM_strp:
23587 case DW_FORM_strx:
23588 case DW_FORM_GNU_str_index:
23589 case DW_FORM_GNU_strp_alt:
23590 /* The string is already allocated on the objfile obstack, point
23591 directly to it. */
23592 {
23593 const char *attr_name = attr->as_string ();
23594 result = (const gdb_byte *) attr_name;
23595 *len = strlen (attr_name);
23596 }
23597 break;
23598 case DW_FORM_block1:
23599 case DW_FORM_block2:
23600 case DW_FORM_block4:
23601 case DW_FORM_block:
23602 case DW_FORM_exprloc:
23603 case DW_FORM_data16:
23604 {
23605 struct dwarf_block *block = attr->as_block ();
23606 result = block->data;
23607 *len = block->size;
23608 }
23609 break;
23610
23611 /* The DW_AT_const_value attributes are supposed to carry the
23612 symbol's value "represented as it would be on the target
23613 architecture." By the time we get here, it's already been
23614 converted to host endianness, so we just need to sign- or
23615 zero-extend it as appropriate. */
23616 case DW_FORM_data1:
23617 type = die_type (die, cu);
23618 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23619 if (result == NULL)
23620 result = write_constant_as_bytes (obstack, byte_order,
23621 type, value, len);
23622 break;
23623 case DW_FORM_data2:
23624 type = die_type (die, cu);
23625 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23626 if (result == NULL)
23627 result = write_constant_as_bytes (obstack, byte_order,
23628 type, value, len);
23629 break;
23630 case DW_FORM_data4:
23631 type = die_type (die, cu);
23632 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23633 if (result == NULL)
23634 result = write_constant_as_bytes (obstack, byte_order,
23635 type, value, len);
23636 break;
23637 case DW_FORM_data8:
23638 type = die_type (die, cu);
23639 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23640 if (result == NULL)
23641 result = write_constant_as_bytes (obstack, byte_order,
23642 type, value, len);
23643 break;
23644
23645 case DW_FORM_sdata:
23646 case DW_FORM_implicit_const:
23647 type = die_type (die, cu);
23648 result = write_constant_as_bytes (obstack, byte_order,
23649 type, attr->as_signed (), len);
23650 break;
23651
23652 case DW_FORM_udata:
23653 type = die_type (die, cu);
23654 result = write_constant_as_bytes (obstack, byte_order,
23655 type, attr->as_unsigned (), len);
23656 break;
23657
23658 default:
23659 complaint (_("unsupported const value attribute form: '%s'"),
23660 dwarf_form_name (attr->form));
23661 break;
23662 }
23663
23664 return result;
23665 }
23666
23667 /* See read.h. */
23668
23669 struct type *
23670 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23671 dwarf2_per_cu_data *per_cu,
23672 dwarf2_per_objfile *per_objfile,
23673 const char **var_name)
23674 {
23675 struct die_info *die;
23676
23677 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23678 if (cu == nullptr)
23679 cu = load_cu (per_cu, per_objfile, false);
23680
23681 if (cu == nullptr)
23682 return nullptr;
23683
23684 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23685 if (!die)
23686 return NULL;
23687
23688 if (var_name != nullptr)
23689 *var_name = var_decl_name (die, cu);
23690 return die_type (die, cu);
23691 }
23692
23693 /* See read.h. */
23694
23695 struct type *
23696 dwarf2_get_die_type (cu_offset die_offset,
23697 dwarf2_per_cu_data *per_cu,
23698 dwarf2_per_objfile *per_objfile)
23699 {
23700 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23701 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23702 }
23703
23704 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23705 On entry *REF_CU is the CU of SRC_DIE.
23706 On exit *REF_CU is the CU of the result.
23707 Returns NULL if the referenced DIE isn't found. */
23708
23709 static struct die_info *
23710 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23711 struct dwarf2_cu **ref_cu)
23712 {
23713 struct die_info temp_die;
23714 struct dwarf2_cu *sig_cu;
23715 struct die_info *die;
23716 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23717
23718
23719 /* While it might be nice to assert sig_type->type == NULL here,
23720 we can get here for DW_AT_imported_declaration where we need
23721 the DIE not the type. */
23722
23723 /* If necessary, add it to the queue and load its DIEs.
23724
23725 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23726 it doesn't mean they are currently loaded. Since we require them
23727 to be loaded, we must check for ourselves. */
23728 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23729 language_minimal)
23730 || per_objfile->get_cu (sig_type) == nullptr)
23731 read_signatured_type (sig_type, per_objfile);
23732
23733 sig_cu = per_objfile->get_cu (sig_type);
23734 gdb_assert (sig_cu != NULL);
23735 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23736 temp_die.sect_off = sig_type->type_offset_in_section;
23737 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23738 to_underlying (temp_die.sect_off));
23739 if (die)
23740 {
23741 /* For .gdb_index version 7 keep track of included TUs.
23742 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23743 if (per_objfile->per_bfd->index_table != NULL
23744 && per_objfile->per_bfd->index_table->version <= 7)
23745 {
23746 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23747 }
23748
23749 *ref_cu = sig_cu;
23750 return die;
23751 }
23752
23753 return NULL;
23754 }
23755
23756 /* Follow signatured type referenced by ATTR in SRC_DIE.
23757 On entry *REF_CU is the CU of SRC_DIE.
23758 On exit *REF_CU is the CU of the result.
23759 The result is the DIE of the type.
23760 If the referenced type cannot be found an error is thrown. */
23761
23762 static struct die_info *
23763 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23764 struct dwarf2_cu **ref_cu)
23765 {
23766 ULONGEST signature = attr->as_signature ();
23767 struct signatured_type *sig_type;
23768 struct die_info *die;
23769
23770 gdb_assert (attr->form == DW_FORM_ref_sig8);
23771
23772 sig_type = lookup_signatured_type (*ref_cu, signature);
23773 /* sig_type will be NULL if the signatured type is missing from
23774 the debug info. */
23775 if (sig_type == NULL)
23776 {
23777 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23778 " from DIE at %s [in module %s]"),
23779 hex_string (signature), sect_offset_str (src_die->sect_off),
23780 objfile_name ((*ref_cu)->per_objfile->objfile));
23781 }
23782
23783 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23784 if (die == NULL)
23785 {
23786 dump_die_for_error (src_die);
23787 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23788 " from DIE at %s [in module %s]"),
23789 hex_string (signature), sect_offset_str (src_die->sect_off),
23790 objfile_name ((*ref_cu)->per_objfile->objfile));
23791 }
23792
23793 return die;
23794 }
23795
23796 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23797 reading in and processing the type unit if necessary. */
23798
23799 static struct type *
23800 get_signatured_type (struct die_info *die, ULONGEST signature,
23801 struct dwarf2_cu *cu)
23802 {
23803 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23804 struct signatured_type *sig_type;
23805 struct dwarf2_cu *type_cu;
23806 struct die_info *type_die;
23807 struct type *type;
23808
23809 sig_type = lookup_signatured_type (cu, signature);
23810 /* sig_type will be NULL if the signatured type is missing from
23811 the debug info. */
23812 if (sig_type == NULL)
23813 {
23814 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23815 " from DIE at %s [in module %s]"),
23816 hex_string (signature), sect_offset_str (die->sect_off),
23817 objfile_name (per_objfile->objfile));
23818 return build_error_marker_type (cu, die);
23819 }
23820
23821 /* If we already know the type we're done. */
23822 type = per_objfile->get_type_for_signatured_type (sig_type);
23823 if (type != nullptr)
23824 return type;
23825
23826 type_cu = cu;
23827 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23828 if (type_die != NULL)
23829 {
23830 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23831 is created. This is important, for example, because for c++ classes
23832 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23833 type = read_type_die (type_die, type_cu);
23834 if (type == NULL)
23835 {
23836 complaint (_("Dwarf Error: Cannot build signatured type %s"
23837 " referenced from DIE at %s [in module %s]"),
23838 hex_string (signature), sect_offset_str (die->sect_off),
23839 objfile_name (per_objfile->objfile));
23840 type = build_error_marker_type (cu, die);
23841 }
23842 }
23843 else
23844 {
23845 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23846 " from DIE at %s [in module %s]"),
23847 hex_string (signature), sect_offset_str (die->sect_off),
23848 objfile_name (per_objfile->objfile));
23849 type = build_error_marker_type (cu, die);
23850 }
23851
23852 per_objfile->set_type_for_signatured_type (sig_type, type);
23853
23854 return type;
23855 }
23856
23857 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23858 reading in and processing the type unit if necessary. */
23859
23860 static struct type *
23861 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23862 struct dwarf2_cu *cu) /* ARI: editCase function */
23863 {
23864 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23865 if (attr->form_is_ref ())
23866 {
23867 struct dwarf2_cu *type_cu = cu;
23868 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23869
23870 return read_type_die (type_die, type_cu);
23871 }
23872 else if (attr->form == DW_FORM_ref_sig8)
23873 {
23874 return get_signatured_type (die, attr->as_signature (), cu);
23875 }
23876 else
23877 {
23878 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23879
23880 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23881 " at %s [in module %s]"),
23882 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23883 objfile_name (per_objfile->objfile));
23884 return build_error_marker_type (cu, die);
23885 }
23886 }
23887
23888 /* Load the DIEs associated with type unit PER_CU into memory. */
23889
23890 static void
23891 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23892 dwarf2_per_objfile *per_objfile)
23893 {
23894 struct signatured_type *sig_type;
23895
23896 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23897 gdb_assert (! per_cu->type_unit_group_p ());
23898
23899 /* We have the per_cu, but we need the signatured_type.
23900 Fortunately this is an easy translation. */
23901 gdb_assert (per_cu->is_debug_types);
23902 sig_type = (struct signatured_type *) per_cu;
23903
23904 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23905
23906 read_signatured_type (sig_type, per_objfile);
23907
23908 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23909 }
23910
23911 /* Read in a signatured type and build its CU and DIEs.
23912 If the type is a stub for the real type in a DWO file,
23913 read in the real type from the DWO file as well. */
23914
23915 static void
23916 read_signatured_type (signatured_type *sig_type,
23917 dwarf2_per_objfile *per_objfile)
23918 {
23919 gdb_assert (sig_type->is_debug_types);
23920 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23921
23922 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23923
23924 if (!reader.dummy_p)
23925 {
23926 struct dwarf2_cu *cu = reader.cu;
23927 const gdb_byte *info_ptr = reader.info_ptr;
23928
23929 gdb_assert (cu->die_hash == NULL);
23930 cu->die_hash =
23931 htab_create_alloc_ex (cu->header.length / 12,
23932 die_hash,
23933 die_eq,
23934 NULL,
23935 &cu->comp_unit_obstack,
23936 hashtab_obstack_allocate,
23937 dummy_obstack_deallocate);
23938
23939 if (reader.comp_unit_die->has_children)
23940 reader.comp_unit_die->child
23941 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23942 reader.comp_unit_die);
23943 cu->dies = reader.comp_unit_die;
23944 /* comp_unit_die is not stored in die_hash, no need. */
23945
23946 /* We try not to read any attributes in this function, because
23947 not all CUs needed for references have been loaded yet, and
23948 symbol table processing isn't initialized. But we have to
23949 set the CU language, or we won't be able to build types
23950 correctly. Similarly, if we do not read the producer, we can
23951 not apply producer-specific interpretation. */
23952 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23953
23954 reader.keep ();
23955 }
23956
23957 sig_type->tu_read = 1;
23958 }
23959
23960 /* Decode simple location descriptions.
23961 Given a pointer to a dwarf block that defines a location, compute
23962 the location and return the value. If COMPUTED is non-null, it is
23963 set to true to indicate that decoding was successful, and false
23964 otherwise. If COMPUTED is null, then this function may emit a
23965 complaint. */
23966
23967 static CORE_ADDR
23968 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23969 {
23970 struct objfile *objfile = cu->per_objfile->objfile;
23971 size_t i;
23972 size_t size = blk->size;
23973 const gdb_byte *data = blk->data;
23974 CORE_ADDR stack[64];
23975 int stacki;
23976 unsigned int bytes_read, unsnd;
23977 gdb_byte op;
23978
23979 if (computed != nullptr)
23980 *computed = false;
23981
23982 i = 0;
23983 stacki = 0;
23984 stack[stacki] = 0;
23985 stack[++stacki] = 0;
23986
23987 while (i < size)
23988 {
23989 op = data[i++];
23990 switch (op)
23991 {
23992 case DW_OP_lit0:
23993 case DW_OP_lit1:
23994 case DW_OP_lit2:
23995 case DW_OP_lit3:
23996 case DW_OP_lit4:
23997 case DW_OP_lit5:
23998 case DW_OP_lit6:
23999 case DW_OP_lit7:
24000 case DW_OP_lit8:
24001 case DW_OP_lit9:
24002 case DW_OP_lit10:
24003 case DW_OP_lit11:
24004 case DW_OP_lit12:
24005 case DW_OP_lit13:
24006 case DW_OP_lit14:
24007 case DW_OP_lit15:
24008 case DW_OP_lit16:
24009 case DW_OP_lit17:
24010 case DW_OP_lit18:
24011 case DW_OP_lit19:
24012 case DW_OP_lit20:
24013 case DW_OP_lit21:
24014 case DW_OP_lit22:
24015 case DW_OP_lit23:
24016 case DW_OP_lit24:
24017 case DW_OP_lit25:
24018 case DW_OP_lit26:
24019 case DW_OP_lit27:
24020 case DW_OP_lit28:
24021 case DW_OP_lit29:
24022 case DW_OP_lit30:
24023 case DW_OP_lit31:
24024 stack[++stacki] = op - DW_OP_lit0;
24025 break;
24026
24027 case DW_OP_reg0:
24028 case DW_OP_reg1:
24029 case DW_OP_reg2:
24030 case DW_OP_reg3:
24031 case DW_OP_reg4:
24032 case DW_OP_reg5:
24033 case DW_OP_reg6:
24034 case DW_OP_reg7:
24035 case DW_OP_reg8:
24036 case DW_OP_reg9:
24037 case DW_OP_reg10:
24038 case DW_OP_reg11:
24039 case DW_OP_reg12:
24040 case DW_OP_reg13:
24041 case DW_OP_reg14:
24042 case DW_OP_reg15:
24043 case DW_OP_reg16:
24044 case DW_OP_reg17:
24045 case DW_OP_reg18:
24046 case DW_OP_reg19:
24047 case DW_OP_reg20:
24048 case DW_OP_reg21:
24049 case DW_OP_reg22:
24050 case DW_OP_reg23:
24051 case DW_OP_reg24:
24052 case DW_OP_reg25:
24053 case DW_OP_reg26:
24054 case DW_OP_reg27:
24055 case DW_OP_reg28:
24056 case DW_OP_reg29:
24057 case DW_OP_reg30:
24058 case DW_OP_reg31:
24059 stack[++stacki] = op - DW_OP_reg0;
24060 if (i < size)
24061 {
24062 if (computed == nullptr)
24063 dwarf2_complex_location_expr_complaint ();
24064 else
24065 return 0;
24066 }
24067 break;
24068
24069 case DW_OP_regx:
24070 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24071 i += bytes_read;
24072 stack[++stacki] = unsnd;
24073 if (i < size)
24074 {
24075 if (computed == nullptr)
24076 dwarf2_complex_location_expr_complaint ();
24077 else
24078 return 0;
24079 }
24080 break;
24081
24082 case DW_OP_addr:
24083 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24084 &bytes_read);
24085 i += bytes_read;
24086 break;
24087
24088 case DW_OP_const1u:
24089 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24090 i += 1;
24091 break;
24092
24093 case DW_OP_const1s:
24094 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24095 i += 1;
24096 break;
24097
24098 case DW_OP_const2u:
24099 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24100 i += 2;
24101 break;
24102
24103 case DW_OP_const2s:
24104 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24105 i += 2;
24106 break;
24107
24108 case DW_OP_const4u:
24109 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24110 i += 4;
24111 break;
24112
24113 case DW_OP_const4s:
24114 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24115 i += 4;
24116 break;
24117
24118 case DW_OP_const8u:
24119 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24120 i += 8;
24121 break;
24122
24123 case DW_OP_constu:
24124 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24125 &bytes_read);
24126 i += bytes_read;
24127 break;
24128
24129 case DW_OP_consts:
24130 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24131 i += bytes_read;
24132 break;
24133
24134 case DW_OP_dup:
24135 stack[stacki + 1] = stack[stacki];
24136 stacki++;
24137 break;
24138
24139 case DW_OP_plus:
24140 stack[stacki - 1] += stack[stacki];
24141 stacki--;
24142 break;
24143
24144 case DW_OP_plus_uconst:
24145 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24146 &bytes_read);
24147 i += bytes_read;
24148 break;
24149
24150 case DW_OP_minus:
24151 stack[stacki - 1] -= stack[stacki];
24152 stacki--;
24153 break;
24154
24155 case DW_OP_deref:
24156 /* If we're not the last op, then we definitely can't encode
24157 this using GDB's address_class enum. This is valid for partial
24158 global symbols, although the variable's address will be bogus
24159 in the psymtab. */
24160 if (i < size)
24161 {
24162 if (computed == nullptr)
24163 dwarf2_complex_location_expr_complaint ();
24164 else
24165 return 0;
24166 }
24167 break;
24168
24169 case DW_OP_GNU_push_tls_address:
24170 case DW_OP_form_tls_address:
24171 /* The top of the stack has the offset from the beginning
24172 of the thread control block at which the variable is located. */
24173 /* Nothing should follow this operator, so the top of stack would
24174 be returned. */
24175 /* This is valid for partial global symbols, but the variable's
24176 address will be bogus in the psymtab. Make it always at least
24177 non-zero to not look as a variable garbage collected by linker
24178 which have DW_OP_addr 0. */
24179 if (i < size)
24180 {
24181 if (computed == nullptr)
24182 dwarf2_complex_location_expr_complaint ();
24183 else
24184 return 0;
24185 }
24186 stack[stacki]++;
24187 break;
24188
24189 case DW_OP_GNU_uninit:
24190 if (computed != nullptr)
24191 return 0;
24192 break;
24193
24194 case DW_OP_addrx:
24195 case DW_OP_GNU_addr_index:
24196 case DW_OP_GNU_const_index:
24197 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24198 &bytes_read);
24199 i += bytes_read;
24200 break;
24201
24202 default:
24203 if (computed == nullptr)
24204 {
24205 const char *name = get_DW_OP_name (op);
24206
24207 if (name)
24208 complaint (_("unsupported stack op: '%s'"),
24209 name);
24210 else
24211 complaint (_("unsupported stack op: '%02x'"),
24212 op);
24213 }
24214
24215 return (stack[stacki]);
24216 }
24217
24218 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24219 outside of the allocated space. Also enforce minimum>0. */
24220 if (stacki >= ARRAY_SIZE (stack) - 1)
24221 {
24222 if (computed == nullptr)
24223 complaint (_("location description stack overflow"));
24224 return 0;
24225 }
24226
24227 if (stacki <= 0)
24228 {
24229 if (computed == nullptr)
24230 complaint (_("location description stack underflow"));
24231 return 0;
24232 }
24233 }
24234
24235 if (computed != nullptr)
24236 *computed = true;
24237 return (stack[stacki]);
24238 }
24239
24240 /* memory allocation interface */
24241
24242 static struct dwarf_block *
24243 dwarf_alloc_block (struct dwarf2_cu *cu)
24244 {
24245 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24246 }
24247
24248 static struct die_info *
24249 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24250 {
24251 struct die_info *die;
24252 size_t size = sizeof (struct die_info);
24253
24254 if (num_attrs > 1)
24255 size += (num_attrs - 1) * sizeof (struct attribute);
24256
24257 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24258 memset (die, 0, sizeof (struct die_info));
24259 return (die);
24260 }
24261
24262 \f
24263
24264 /* Macro support. */
24265
24266 /* An overload of dwarf_decode_macros that finds the correct section
24267 and ensures it is read in before calling the other overload. */
24268
24269 static void
24270 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24271 int section_is_gnu)
24272 {
24273 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24274 struct objfile *objfile = per_objfile->objfile;
24275 const struct line_header *lh = cu->line_header;
24276 unsigned int offset_size = cu->header.offset_size;
24277 struct dwarf2_section_info *section;
24278 const char *section_name;
24279
24280 if (cu->dwo_unit != nullptr)
24281 {
24282 if (section_is_gnu)
24283 {
24284 section = &cu->dwo_unit->dwo_file->sections.macro;
24285 section_name = ".debug_macro.dwo";
24286 }
24287 else
24288 {
24289 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24290 section_name = ".debug_macinfo.dwo";
24291 }
24292 }
24293 else
24294 {
24295 if (section_is_gnu)
24296 {
24297 section = &per_objfile->per_bfd->macro;
24298 section_name = ".debug_macro";
24299 }
24300 else
24301 {
24302 section = &per_objfile->per_bfd->macinfo;
24303 section_name = ".debug_macinfo";
24304 }
24305 }
24306
24307 section->read (objfile);
24308 if (section->buffer == nullptr)
24309 {
24310 complaint (_("missing %s section"), section_name);
24311 return;
24312 }
24313
24314 buildsym_compunit *builder = cu->get_builder ();
24315
24316 struct dwarf2_section_info *str_offsets_section;
24317 struct dwarf2_section_info *str_section;
24318 gdb::optional<ULONGEST> str_offsets_base;
24319
24320 if (cu->dwo_unit != nullptr)
24321 {
24322 str_offsets_section = &cu->dwo_unit->dwo_file
24323 ->sections.str_offsets;
24324 str_section = &cu->dwo_unit->dwo_file->sections.str;
24325 str_offsets_base = cu->header.addr_size;
24326 }
24327 else
24328 {
24329 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24330 str_section = &per_objfile->per_bfd->str;
24331 str_offsets_base = cu->str_offsets_base;
24332 }
24333
24334 dwarf_decode_macros (per_objfile, builder, section, lh,
24335 offset_size, offset, str_section, str_offsets_section,
24336 str_offsets_base, section_is_gnu);
24337 }
24338
24339 /* Return the .debug_loc section to use for CU.
24340 For DWO files use .debug_loc.dwo. */
24341
24342 static struct dwarf2_section_info *
24343 cu_debug_loc_section (struct dwarf2_cu *cu)
24344 {
24345 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24346
24347 if (cu->dwo_unit)
24348 {
24349 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24350
24351 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24352 }
24353 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24354 : &per_objfile->per_bfd->loc);
24355 }
24356
24357 /* Return the .debug_rnglists section to use for CU. */
24358 static struct dwarf2_section_info *
24359 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24360 {
24361 if (cu->header.version < 5)
24362 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24363 cu->header.version);
24364 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24365
24366 /* Make sure we read the .debug_rnglists section from the file that
24367 contains the DW_AT_ranges attribute we are reading. Normally that
24368 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24369 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24370 program. */
24371 if (cu->dwo_unit != nullptr
24372 && tag != DW_TAG_compile_unit
24373 && tag != DW_TAG_skeleton_unit)
24374 {
24375 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24376
24377 if (sections->rnglists.size > 0)
24378 return &sections->rnglists;
24379 else
24380 error (_(".debug_rnglists section is missing from .dwo file."));
24381 }
24382 return &dwarf2_per_objfile->per_bfd->rnglists;
24383 }
24384
24385 /* A helper function that fills in a dwarf2_loclist_baton. */
24386
24387 static void
24388 fill_in_loclist_baton (struct dwarf2_cu *cu,
24389 struct dwarf2_loclist_baton *baton,
24390 const struct attribute *attr)
24391 {
24392 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24393 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24394
24395 section->read (per_objfile->objfile);
24396
24397 baton->per_objfile = per_objfile;
24398 baton->per_cu = cu->per_cu;
24399 gdb_assert (baton->per_cu);
24400 /* We don't know how long the location list is, but make sure we
24401 don't run off the edge of the section. */
24402 baton->size = section->size - attr->as_unsigned ();
24403 baton->data = section->buffer + attr->as_unsigned ();
24404 if (cu->base_address.has_value ())
24405 baton->base_address = *cu->base_address;
24406 else
24407 baton->base_address = 0;
24408 baton->from_dwo = cu->dwo_unit != NULL;
24409 }
24410
24411 static void
24412 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24413 struct dwarf2_cu *cu, int is_block)
24414 {
24415 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24416 struct objfile *objfile = per_objfile->objfile;
24417 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24418
24419 if (attr->form_is_section_offset ()
24420 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24421 the section. If so, fall through to the complaint in the
24422 other branch. */
24423 && attr->as_unsigned () < section->get_size (objfile))
24424 {
24425 struct dwarf2_loclist_baton *baton;
24426
24427 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24428
24429 fill_in_loclist_baton (cu, baton, attr);
24430
24431 if (!cu->base_address.has_value ())
24432 complaint (_("Location list used without "
24433 "specifying the CU base address."));
24434
24435 sym->set_aclass_index ((is_block
24436 ? dwarf2_loclist_block_index
24437 : dwarf2_loclist_index));
24438 SYMBOL_LOCATION_BATON (sym) = baton;
24439 }
24440 else
24441 {
24442 struct dwarf2_locexpr_baton *baton;
24443
24444 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24445 baton->per_objfile = per_objfile;
24446 baton->per_cu = cu->per_cu;
24447 gdb_assert (baton->per_cu);
24448
24449 if (attr->form_is_block ())
24450 {
24451 /* Note that we're just copying the block's data pointer
24452 here, not the actual data. We're still pointing into the
24453 info_buffer for SYM's objfile; right now we never release
24454 that buffer, but when we do clean up properly this may
24455 need to change. */
24456 struct dwarf_block *block = attr->as_block ();
24457 baton->size = block->size;
24458 baton->data = block->data;
24459 }
24460 else
24461 {
24462 dwarf2_invalid_attrib_class_complaint ("location description",
24463 sym->natural_name ());
24464 baton->size = 0;
24465 }
24466
24467 sym->set_aclass_index ((is_block
24468 ? dwarf2_locexpr_block_index
24469 : dwarf2_locexpr_index));
24470 SYMBOL_LOCATION_BATON (sym) = baton;
24471 }
24472 }
24473
24474 /* See read.h. */
24475
24476 const comp_unit_head *
24477 dwarf2_per_cu_data::get_header () const
24478 {
24479 if (!m_header_read_in)
24480 {
24481 const gdb_byte *info_ptr
24482 = this->section->buffer + to_underlying (this->sect_off);
24483
24484 memset (&m_header, 0, sizeof (m_header));
24485
24486 read_comp_unit_head (&m_header, info_ptr, this->section,
24487 rcuh_kind::COMPILE);
24488
24489 m_header_read_in = true;
24490 }
24491
24492 return &m_header;
24493 }
24494
24495 /* See read.h. */
24496
24497 int
24498 dwarf2_per_cu_data::addr_size () const
24499 {
24500 return this->get_header ()->addr_size;
24501 }
24502
24503 /* See read.h. */
24504
24505 int
24506 dwarf2_per_cu_data::offset_size () const
24507 {
24508 return this->get_header ()->offset_size;
24509 }
24510
24511 /* See read.h. */
24512
24513 int
24514 dwarf2_per_cu_data::ref_addr_size () const
24515 {
24516 const comp_unit_head *header = this->get_header ();
24517
24518 if (header->version == 2)
24519 return header->addr_size;
24520 else
24521 return header->offset_size;
24522 }
24523
24524 /* A helper function for dwarf2_find_containing_comp_unit that returns
24525 the index of the result, and that searches a vector. It will
24526 return a result even if the offset in question does not actually
24527 occur in any CU. This is separate so that it can be unit
24528 tested. */
24529
24530 static int
24531 dwarf2_find_containing_comp_unit
24532 (sect_offset sect_off,
24533 unsigned int offset_in_dwz,
24534 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24535 {
24536 int low, high;
24537
24538 low = 0;
24539 high = all_comp_units.size () - 1;
24540 while (high > low)
24541 {
24542 struct dwarf2_per_cu_data *mid_cu;
24543 int mid = low + (high - low) / 2;
24544
24545 mid_cu = all_comp_units[mid].get ();
24546 if (mid_cu->is_dwz > offset_in_dwz
24547 || (mid_cu->is_dwz == offset_in_dwz
24548 && mid_cu->sect_off + mid_cu->length > sect_off))
24549 high = mid;
24550 else
24551 low = mid + 1;
24552 }
24553 gdb_assert (low == high);
24554 return low;
24555 }
24556
24557 /* Locate the .debug_info compilation unit from CU's objfile which contains
24558 the DIE at OFFSET. Raises an error on failure. */
24559
24560 static struct dwarf2_per_cu_data *
24561 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24562 unsigned int offset_in_dwz,
24563 dwarf2_per_bfd *per_bfd)
24564 {
24565 int low = dwarf2_find_containing_comp_unit
24566 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
24567 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
24568
24569 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24570 {
24571 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24572 error (_("Dwarf Error: could not find partial DIE containing "
24573 "offset %s [in module %s]"),
24574 sect_offset_str (sect_off),
24575 bfd_get_filename (per_bfd->obfd));
24576
24577 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
24578 <= sect_off);
24579 return per_bfd->all_comp_units[low - 1].get ();
24580 }
24581 else
24582 {
24583 if (low == per_bfd->all_comp_units.size () - 1
24584 && sect_off >= this_cu->sect_off + this_cu->length)
24585 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24586 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24587 return this_cu;
24588 }
24589 }
24590
24591 #if GDB_SELF_TEST
24592
24593 namespace selftests {
24594 namespace find_containing_comp_unit {
24595
24596 static void
24597 run_test ()
24598 {
24599 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24600 dwarf2_per_cu_data *one_ptr = one.get ();
24601 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24602 dwarf2_per_cu_data *two_ptr = two.get ();
24603 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24604 dwarf2_per_cu_data *three_ptr = three.get ();
24605 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24606 dwarf2_per_cu_data *four_ptr = four.get ();
24607
24608 one->length = 5;
24609 two->sect_off = sect_offset (one->length);
24610 two->length = 7;
24611
24612 three->length = 5;
24613 three->is_dwz = 1;
24614 four->sect_off = sect_offset (three->length);
24615 four->length = 7;
24616 four->is_dwz = 1;
24617
24618 std::vector<dwarf2_per_cu_data_up> units;
24619 units.push_back (std::move (one));
24620 units.push_back (std::move (two));
24621 units.push_back (std::move (three));
24622 units.push_back (std::move (four));
24623
24624 int result;
24625
24626 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24627 SELF_CHECK (units[result].get () == one_ptr);
24628 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24629 SELF_CHECK (units[result].get () == one_ptr);
24630 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24631 SELF_CHECK (units[result].get () == two_ptr);
24632
24633 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24634 SELF_CHECK (units[result].get () == three_ptr);
24635 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24636 SELF_CHECK (units[result].get () == three_ptr);
24637 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24638 SELF_CHECK (units[result].get () == four_ptr);
24639 }
24640
24641 }
24642 }
24643
24644 #endif /* GDB_SELF_TEST */
24645
24646 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24647
24648 static void
24649 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24650 enum language pretend_language)
24651 {
24652 struct attribute *attr;
24653
24654 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24655
24656 /* Set the language we're debugging. */
24657 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24658 if (cu->producer != nullptr
24659 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
24660 {
24661 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24662 attribute is not standardised yet. As a workaround for the
24663 language detection we fall back to the DW_AT_producer
24664 string. */
24665 cu->per_cu->lang = language_opencl;
24666 }
24667 else if (cu->producer != nullptr
24668 && strstr (cu->producer, "GNU Go ") != NULL)
24669 {
24670 /* Similar hack for Go. */
24671 cu->per_cu->lang = language_go;
24672 }
24673 else if (attr != nullptr)
24674 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
24675 else
24676 cu->per_cu->lang = pretend_language;
24677 cu->language_defn = language_def (cu->per_cu->lang);
24678 }
24679
24680 /* See read.h. */
24681
24682 dwarf2_cu *
24683 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24684 {
24685 auto it = m_dwarf2_cus.find (per_cu);
24686 if (it == m_dwarf2_cus.end ())
24687 return nullptr;
24688
24689 return it->second;
24690 }
24691
24692 /* See read.h. */
24693
24694 void
24695 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24696 {
24697 gdb_assert (this->get_cu (per_cu) == nullptr);
24698
24699 m_dwarf2_cus[per_cu] = cu;
24700 }
24701
24702 /* See read.h. */
24703
24704 void
24705 dwarf2_per_objfile::age_comp_units ()
24706 {
24707 dwarf_read_debug_printf_v ("running");
24708
24709 /* This is not expected to be called in the middle of CU expansion. There is
24710 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24711 loaded in memory. Calling age_comp_units while the queue is in use could
24712 make us free the DIEs for a CU that is in the queue and therefore break
24713 that invariant. */
24714 gdb_assert (!this->per_bfd->queue.has_value ());
24715
24716 /* Start by clearing all marks. */
24717 for (auto pair : m_dwarf2_cus)
24718 pair.second->clear_mark ();
24719
24720 /* Traverse all CUs, mark them and their dependencies if used recently
24721 enough. */
24722 for (auto pair : m_dwarf2_cus)
24723 {
24724 dwarf2_cu *cu = pair.second;
24725
24726 cu->last_used++;
24727 if (cu->last_used <= dwarf_max_cache_age)
24728 cu->mark ();
24729 }
24730
24731 /* Delete all CUs still not marked. */
24732 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24733 {
24734 dwarf2_cu *cu = it->second;
24735
24736 if (!cu->is_marked ())
24737 {
24738 dwarf_read_debug_printf_v ("deleting old CU %s",
24739 sect_offset_str (cu->per_cu->sect_off));
24740 delete cu;
24741 it = m_dwarf2_cus.erase (it);
24742 }
24743 else
24744 it++;
24745 }
24746 }
24747
24748 /* See read.h. */
24749
24750 void
24751 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24752 {
24753 auto it = m_dwarf2_cus.find (per_cu);
24754 if (it == m_dwarf2_cus.end ())
24755 return;
24756
24757 delete it->second;
24758
24759 m_dwarf2_cus.erase (it);
24760 }
24761
24762 dwarf2_per_objfile::~dwarf2_per_objfile ()
24763 {
24764 remove_all_cus ();
24765 }
24766
24767 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24768 We store these in a hash table separate from the DIEs, and preserve them
24769 when the DIEs are flushed out of cache.
24770
24771 The CU "per_cu" pointer is needed because offset alone is not enough to
24772 uniquely identify the type. A file may have multiple .debug_types sections,
24773 or the type may come from a DWO file. Furthermore, while it's more logical
24774 to use per_cu->section+offset, with Fission the section with the data is in
24775 the DWO file but we don't know that section at the point we need it.
24776 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24777 because we can enter the lookup routine, get_die_type_at_offset, from
24778 outside this file, and thus won't necessarily have PER_CU->cu.
24779 Fortunately, PER_CU is stable for the life of the objfile. */
24780
24781 struct dwarf2_per_cu_offset_and_type
24782 {
24783 const struct dwarf2_per_cu_data *per_cu;
24784 sect_offset sect_off;
24785 struct type *type;
24786 };
24787
24788 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24789
24790 static hashval_t
24791 per_cu_offset_and_type_hash (const void *item)
24792 {
24793 const struct dwarf2_per_cu_offset_and_type *ofs
24794 = (const struct dwarf2_per_cu_offset_and_type *) item;
24795
24796 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24797 }
24798
24799 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24800
24801 static int
24802 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24803 {
24804 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24805 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24806 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24807 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24808
24809 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24810 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24811 }
24812
24813 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24814 table if necessary. For convenience, return TYPE.
24815
24816 The DIEs reading must have careful ordering to:
24817 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24818 reading current DIE.
24819 * Not trying to dereference contents of still incompletely read in types
24820 while reading in other DIEs.
24821 * Enable referencing still incompletely read in types just by a pointer to
24822 the type without accessing its fields.
24823
24824 Therefore caller should follow these rules:
24825 * Try to fetch any prerequisite types we may need to build this DIE type
24826 before building the type and calling set_die_type.
24827 * After building type call set_die_type for current DIE as soon as
24828 possible before fetching more types to complete the current type.
24829 * Make the type as complete as possible before fetching more types. */
24830
24831 static struct type *
24832 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24833 bool skip_data_location)
24834 {
24835 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24836 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24837 struct objfile *objfile = per_objfile->objfile;
24838 struct attribute *attr;
24839 struct dynamic_prop prop;
24840
24841 /* For Ada types, make sure that the gnat-specific data is always
24842 initialized (if not already set). There are a few types where
24843 we should not be doing so, because the type-specific area is
24844 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24845 where the type-specific area is used to store the floatformat).
24846 But this is not a problem, because the gnat-specific information
24847 is actually not needed for these types. */
24848 if (need_gnat_info (cu)
24849 && type->code () != TYPE_CODE_FUNC
24850 && type->code () != TYPE_CODE_FLT
24851 && type->code () != TYPE_CODE_METHODPTR
24852 && type->code () != TYPE_CODE_MEMBERPTR
24853 && type->code () != TYPE_CODE_METHOD
24854 && type->code () != TYPE_CODE_FIXED_POINT
24855 && !HAVE_GNAT_AUX_INFO (type))
24856 INIT_GNAT_SPECIFIC (type);
24857
24858 /* Read DW_AT_allocated and set in type. */
24859 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24860 if (attr != NULL)
24861 {
24862 struct type *prop_type = cu->addr_sized_int_type (false);
24863 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24864 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24865 }
24866
24867 /* Read DW_AT_associated and set in type. */
24868 attr = dwarf2_attr (die, DW_AT_associated, cu);
24869 if (attr != NULL)
24870 {
24871 struct type *prop_type = cu->addr_sized_int_type (false);
24872 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24873 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24874 }
24875
24876 /* Read DW_AT_rank and set in type. */
24877 attr = dwarf2_attr (die, DW_AT_rank, cu);
24878 if (attr != NULL)
24879 {
24880 struct type *prop_type = cu->addr_sized_int_type (false);
24881 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24882 type->add_dyn_prop (DYN_PROP_RANK, prop);
24883 }
24884
24885 /* Read DW_AT_data_location and set in type. */
24886 if (!skip_data_location)
24887 {
24888 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24889 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24890 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24891 }
24892
24893 if (per_objfile->die_type_hash == NULL)
24894 per_objfile->die_type_hash
24895 = htab_up (htab_create_alloc (127,
24896 per_cu_offset_and_type_hash,
24897 per_cu_offset_and_type_eq,
24898 NULL, xcalloc, xfree));
24899
24900 ofs.per_cu = cu->per_cu;
24901 ofs.sect_off = die->sect_off;
24902 ofs.type = type;
24903 slot = (struct dwarf2_per_cu_offset_and_type **)
24904 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24905 if (*slot)
24906 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24907 sect_offset_str (die->sect_off));
24908 *slot = XOBNEW (&objfile->objfile_obstack,
24909 struct dwarf2_per_cu_offset_and_type);
24910 **slot = ofs;
24911 return type;
24912 }
24913
24914 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24915 or return NULL if the die does not have a saved type. */
24916
24917 static struct type *
24918 get_die_type_at_offset (sect_offset sect_off,
24919 dwarf2_per_cu_data *per_cu,
24920 dwarf2_per_objfile *per_objfile)
24921 {
24922 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24923
24924 if (per_objfile->die_type_hash == NULL)
24925 return NULL;
24926
24927 ofs.per_cu = per_cu;
24928 ofs.sect_off = sect_off;
24929 slot = ((struct dwarf2_per_cu_offset_and_type *)
24930 htab_find (per_objfile->die_type_hash.get (), &ofs));
24931 if (slot)
24932 return slot->type;
24933 else
24934 return NULL;
24935 }
24936
24937 /* Look up the type for DIE in CU in die_type_hash,
24938 or return NULL if DIE does not have a saved type. */
24939
24940 static struct type *
24941 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24942 {
24943 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24944 }
24945
24946 /* Trivial hash function for partial_die_info: the hash value of a DIE
24947 is its offset in .debug_info for this objfile. */
24948
24949 static hashval_t
24950 partial_die_hash (const void *item)
24951 {
24952 const struct partial_die_info *part_die
24953 = (const struct partial_die_info *) item;
24954
24955 return to_underlying (part_die->sect_off);
24956 }
24957
24958 /* Trivial comparison function for partial_die_info structures: two DIEs
24959 are equal if they have the same offset. */
24960
24961 static int
24962 partial_die_eq (const void *item_lhs, const void *item_rhs)
24963 {
24964 const struct partial_die_info *part_die_lhs
24965 = (const struct partial_die_info *) item_lhs;
24966 const struct partial_die_info *part_die_rhs
24967 = (const struct partial_die_info *) item_rhs;
24968
24969 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24970 }
24971
24972 struct cmd_list_element *set_dwarf_cmdlist;
24973 struct cmd_list_element *show_dwarf_cmdlist;
24974
24975 static void
24976 show_check_physname (struct ui_file *file, int from_tty,
24977 struct cmd_list_element *c, const char *value)
24978 {
24979 gdb_printf (file,
24980 _("Whether to check \"physname\" is %s.\n"),
24981 value);
24982 }
24983
24984 void _initialize_dwarf2_read ();
24985 void
24986 _initialize_dwarf2_read ()
24987 {
24988 add_setshow_prefix_cmd ("dwarf", class_maintenance,
24989 _("\
24990 Set DWARF specific variables.\n\
24991 Configure DWARF variables such as the cache size."),
24992 _("\
24993 Show DWARF specific variables.\n\
24994 Show DWARF variables such as the cache size."),
24995 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
24996 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
24997
24998 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24999 &dwarf_max_cache_age, _("\
25000 Set the upper bound on the age of cached DWARF compilation units."), _("\
25001 Show the upper bound on the age of cached DWARF compilation units."), _("\
25002 A higher limit means that cached compilation units will be stored\n\
25003 in memory longer, and more total memory will be used. Zero disables\n\
25004 caching, which can slow down startup."),
25005 NULL,
25006 show_dwarf_max_cache_age,
25007 &set_dwarf_cmdlist,
25008 &show_dwarf_cmdlist);
25009
25010 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25011 Set debugging of the DWARF reader."), _("\
25012 Show debugging of the DWARF reader."), _("\
25013 When enabled (non-zero), debugging messages are printed during DWARF\n\
25014 reading and symtab expansion. A value of 1 (one) provides basic\n\
25015 information. A value greater than 1 provides more verbose information."),
25016 NULL,
25017 NULL,
25018 &setdebuglist, &showdebuglist);
25019
25020 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25021 Set debugging of the DWARF DIE reader."), _("\
25022 Show debugging of the DWARF DIE reader."), _("\
25023 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25024 The value is the maximum depth to print."),
25025 NULL,
25026 NULL,
25027 &setdebuglist, &showdebuglist);
25028
25029 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25030 Set debugging of the dwarf line reader."), _("\
25031 Show debugging of the dwarf line reader."), _("\
25032 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25033 A value of 1 (one) provides basic information.\n\
25034 A value greater than 1 provides more verbose information."),
25035 NULL,
25036 NULL,
25037 &setdebuglist, &showdebuglist);
25038
25039 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25040 Set cross-checking of \"physname\" code against demangler."), _("\
25041 Show cross-checking of \"physname\" code against demangler."), _("\
25042 When enabled, GDB's internal \"physname\" code is checked against\n\
25043 the demangler."),
25044 NULL, show_check_physname,
25045 &setdebuglist, &showdebuglist);
25046
25047 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25048 no_class, &use_deprecated_index_sections, _("\
25049 Set whether to use deprecated gdb_index sections."), _("\
25050 Show whether to use deprecated gdb_index sections."), _("\
25051 When enabled, deprecated .gdb_index sections are used anyway.\n\
25052 Normally they are ignored either because of a missing feature or\n\
25053 performance issue.\n\
25054 Warning: This option must be enabled before gdb reads the file."),
25055 NULL,
25056 NULL,
25057 &setlist, &showlist);
25058
25059 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25060 &dwarf2_locexpr_funcs);
25061 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25062 &dwarf2_loclist_funcs);
25063
25064 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25065 &dwarf2_block_frame_base_locexpr_funcs);
25066 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25067 &dwarf2_block_frame_base_loclist_funcs);
25068
25069 #if GDB_SELF_TEST
25070 selftests::register_test ("dw2_expand_symtabs_matching",
25071 selftests::dw2_expand_symtabs_matching::run_test);
25072 selftests::register_test ("dwarf2_find_containing_comp_unit",
25073 selftests::find_containing_comp_unit::run_test);
25074 #endif
25075 }