]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Split create_addrmap_from_aranges
[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
2583 to populate given addrmap. Returns true on success, false on
2584 failure. */
2585
2586 static bool
2587 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2588 struct dwarf2_section_info *section,
2589 addrmap *mutable_map)
2590 {
2591 struct objfile *objfile = per_objfile->objfile;
2592 bfd *abfd = objfile->obfd;
2593 struct gdbarch *gdbarch = objfile->arch ();
2594 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2595 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2596
2597 std::unordered_map<sect_offset,
2598 dwarf2_per_cu_data *,
2599 gdb::hash_enum<sect_offset>>
2600 debug_info_offset_to_per_cu;
2601 for (const auto &per_cu : per_bfd->all_comp_units)
2602 {
2603 /* A TU will not need aranges, and skipping them here is an easy
2604 way of ignoring .debug_types -- and possibly seeing a
2605 duplicate section offset -- entirely. */
2606 if (per_cu->is_debug_types)
2607 continue;
2608
2609 const auto insertpair
2610 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2611 per_cu.get ());
2612 if (!insertpair.second)
2613 {
2614 warning (_("Section .debug_aranges in %s has duplicate "
2615 "debug_info_offset %s, ignoring .debug_aranges."),
2616 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2617 return false;
2618 }
2619 }
2620
2621 section->read (objfile);
2622
2623 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2624
2625 const gdb_byte *addr = section->buffer;
2626
2627 while (addr < section->buffer + section->size)
2628 {
2629 const gdb_byte *const entry_addr = addr;
2630 unsigned int bytes_read;
2631
2632 const LONGEST entry_length = read_initial_length (abfd, addr,
2633 &bytes_read);
2634 addr += bytes_read;
2635
2636 const gdb_byte *const entry_end = addr + entry_length;
2637 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2638 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2639 if (addr + entry_length > section->buffer + section->size)
2640 {
2641 warning (_("Section .debug_aranges in %s entry at offset %s "
2642 "length %s exceeds section length %s, "
2643 "ignoring .debug_aranges."),
2644 objfile_name (objfile),
2645 plongest (entry_addr - section->buffer),
2646 plongest (bytes_read + entry_length),
2647 pulongest (section->size));
2648 return false;
2649 }
2650
2651 /* The version number. */
2652 const uint16_t version = read_2_bytes (abfd, addr);
2653 addr += 2;
2654 if (version != 2)
2655 {
2656 warning (_("Section .debug_aranges in %s entry at offset %s "
2657 "has unsupported version %d, ignoring .debug_aranges."),
2658 objfile_name (objfile),
2659 plongest (entry_addr - section->buffer), version);
2660 return false;
2661 }
2662
2663 const uint64_t debug_info_offset
2664 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2665 addr += offset_size;
2666 const auto per_cu_it
2667 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2668 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2669 {
2670 warning (_("Section .debug_aranges in %s entry at offset %s "
2671 "debug_info_offset %s does not exists, "
2672 "ignoring .debug_aranges."),
2673 objfile_name (objfile),
2674 plongest (entry_addr - section->buffer),
2675 pulongest (debug_info_offset));
2676 return false;
2677 }
2678 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2679
2680 const uint8_t address_size = *addr++;
2681 if (address_size < 1 || address_size > 8)
2682 {
2683 warning (_("Section .debug_aranges in %s entry at offset %s "
2684 "address_size %u is invalid, ignoring .debug_aranges."),
2685 objfile_name (objfile),
2686 plongest (entry_addr - section->buffer), address_size);
2687 return false;
2688 }
2689
2690 const uint8_t segment_selector_size = *addr++;
2691 if (segment_selector_size != 0)
2692 {
2693 warning (_("Section .debug_aranges in %s entry at offset %s "
2694 "segment_selector_size %u is not supported, "
2695 "ignoring .debug_aranges."),
2696 objfile_name (objfile),
2697 plongest (entry_addr - section->buffer),
2698 segment_selector_size);
2699 return false;
2700 }
2701
2702 /* Must pad to an alignment boundary that is twice the address
2703 size. It is undocumented by the DWARF standard but GCC does
2704 use it. However, not every compiler does this. We can see
2705 whether it has happened by looking at the total length of the
2706 contents of the aranges for this CU -- it if isn't a multiple
2707 of twice the address size, then we skip any leftover
2708 bytes. */
2709 addr += (entry_end - addr) % (2 * address_size);
2710
2711 for (;;)
2712 {
2713 if (addr + 2 * address_size > entry_end)
2714 {
2715 warning (_("Section .debug_aranges in %s entry at offset %s "
2716 "address list is not properly terminated, "
2717 "ignoring .debug_aranges."),
2718 objfile_name (objfile),
2719 plongest (entry_addr - section->buffer));
2720 return false;
2721 }
2722 ULONGEST start = extract_unsigned_integer (addr, address_size,
2723 dwarf5_byte_order);
2724 addr += address_size;
2725 ULONGEST length = extract_unsigned_integer (addr, address_size,
2726 dwarf5_byte_order);
2727 addr += address_size;
2728 if (start == 0 && length == 0)
2729 break;
2730 if (start == 0 && !per_bfd->has_section_at_zero)
2731 {
2732 /* Symbol was eliminated due to a COMDAT group. */
2733 continue;
2734 }
2735 ULONGEST end = start + length;
2736 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2737 - baseaddr);
2738 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2739 - baseaddr);
2740 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2741 }
2742 }
2743
2744 return true;
2745 }
2746
2747 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2748 populate the psymtabs_addrmap. */
2749
2750 static void
2751 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2752 struct dwarf2_section_info *section)
2753 {
2754 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2755
2756 auto_obstack temp_obstack;
2757 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2758
2759 if (read_addrmap_from_aranges (per_objfile, section, mutable_map))
2760 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2761 &per_bfd->obstack);
2762 }
2763
2764 /* A helper function that reads the .gdb_index from BUFFER and fills
2765 in MAP. FILENAME is the name of the file containing the data;
2766 it is used for error reporting. DEPRECATED_OK is true if it is
2767 ok to use deprecated sections.
2768
2769 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2770 out parameters that are filled in with information about the CU and
2771 TU lists in the section.
2772
2773 Returns true if all went well, false otherwise. */
2774
2775 static bool
2776 read_gdb_index_from_buffer (const char *filename,
2777 bool deprecated_ok,
2778 gdb::array_view<const gdb_byte> buffer,
2779 struct mapped_index *map,
2780 const gdb_byte **cu_list,
2781 offset_type *cu_list_elements,
2782 const gdb_byte **types_list,
2783 offset_type *types_list_elements)
2784 {
2785 const gdb_byte *addr = &buffer[0];
2786 offset_view metadata (buffer);
2787
2788 /* Version check. */
2789 offset_type version = metadata[0];
2790 /* Versions earlier than 3 emitted every copy of a psymbol. This
2791 causes the index to behave very poorly for certain requests. Version 3
2792 contained incomplete addrmap. So, it seems better to just ignore such
2793 indices. */
2794 if (version < 4)
2795 {
2796 static int warning_printed = 0;
2797 if (!warning_printed)
2798 {
2799 warning (_("Skipping obsolete .gdb_index section in %s."),
2800 filename);
2801 warning_printed = 1;
2802 }
2803 return 0;
2804 }
2805 /* Index version 4 uses a different hash function than index version
2806 5 and later.
2807
2808 Versions earlier than 6 did not emit psymbols for inlined
2809 functions. Using these files will cause GDB not to be able to
2810 set breakpoints on inlined functions by name, so we ignore these
2811 indices unless the user has done
2812 "set use-deprecated-index-sections on". */
2813 if (version < 6 && !deprecated_ok)
2814 {
2815 static int warning_printed = 0;
2816 if (!warning_printed)
2817 {
2818 warning (_("\
2819 Skipping deprecated .gdb_index section in %s.\n\
2820 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2821 to use the section anyway."),
2822 filename);
2823 warning_printed = 1;
2824 }
2825 return 0;
2826 }
2827 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2828 of the TU (for symbols coming from TUs),
2829 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2830 Plus gold-generated indices can have duplicate entries for global symbols,
2831 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2832 These are just performance bugs, and we can't distinguish gdb-generated
2833 indices from gold-generated ones, so issue no warning here. */
2834
2835 /* Indexes with higher version than the one supported by GDB may be no
2836 longer backward compatible. */
2837 if (version > 8)
2838 return 0;
2839
2840 map->version = version;
2841
2842 int i = 1;
2843 *cu_list = addr + metadata[i];
2844 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2845 ++i;
2846
2847 *types_list = addr + metadata[i];
2848 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2849 ++i;
2850
2851 const gdb_byte *address_table = addr + metadata[i];
2852 const gdb_byte *address_table_end = addr + metadata[i + 1];
2853 map->address_table
2854 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2855 ++i;
2856
2857 const gdb_byte *symbol_table = addr + metadata[i];
2858 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2859 map->symbol_table
2860 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2861 symbol_table_end));
2862
2863 ++i;
2864 map->constant_pool = buffer.slice (metadata[i]);
2865
2866 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2867 {
2868 /* An empty constant pool implies that all symbol table entries are
2869 empty. Make map->symbol_table.empty () == true. */
2870 map->symbol_table
2871 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2872 symbol_table));
2873 }
2874
2875 return 1;
2876 }
2877
2878 /* Callback types for dwarf2_read_gdb_index. */
2879
2880 typedef gdb::function_view
2881 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2882 get_gdb_index_contents_ftype;
2883 typedef gdb::function_view
2884 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2885 get_gdb_index_contents_dwz_ftype;
2886
2887 /* Read .gdb_index. If everything went ok, initialize the "quick"
2888 elements of all the CUs and return 1. Otherwise, return 0. */
2889
2890 static int
2891 dwarf2_read_gdb_index
2892 (dwarf2_per_objfile *per_objfile,
2893 get_gdb_index_contents_ftype get_gdb_index_contents,
2894 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2895 {
2896 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2897 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2898 struct dwz_file *dwz;
2899 struct objfile *objfile = per_objfile->objfile;
2900 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2901
2902 gdb::array_view<const gdb_byte> main_index_contents
2903 = get_gdb_index_contents (objfile, per_bfd);
2904
2905 if (main_index_contents.empty ())
2906 return 0;
2907
2908 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2909 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2910 use_deprecated_index_sections,
2911 main_index_contents, map.get (), &cu_list,
2912 &cu_list_elements, &types_list,
2913 &types_list_elements))
2914 return 0;
2915
2916 /* Don't use the index if it's empty. */
2917 if (map->symbol_table.empty ())
2918 return 0;
2919
2920 /* If there is a .dwz file, read it so we can get its CU list as
2921 well. */
2922 dwz = dwarf2_get_dwz_file (per_bfd);
2923 if (dwz != NULL)
2924 {
2925 struct mapped_index dwz_map;
2926 const gdb_byte *dwz_types_ignore;
2927 offset_type dwz_types_elements_ignore;
2928
2929 gdb::array_view<const gdb_byte> dwz_index_content
2930 = get_gdb_index_contents_dwz (objfile, dwz);
2931
2932 if (dwz_index_content.empty ())
2933 return 0;
2934
2935 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2936 1, dwz_index_content, &dwz_map,
2937 &dwz_list, &dwz_list_elements,
2938 &dwz_types_ignore,
2939 &dwz_types_elements_ignore))
2940 {
2941 warning (_("could not read '.gdb_index' section from %s; skipping"),
2942 bfd_get_filename (dwz->dwz_bfd.get ()));
2943 return 0;
2944 }
2945 }
2946
2947 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2948 dwz_list_elements);
2949
2950 if (types_list_elements)
2951 {
2952 /* We can only handle a single .debug_types when we have an
2953 index. */
2954 if (per_bfd->types.size () != 1)
2955 return 0;
2956
2957 dwarf2_section_info *section = &per_bfd->types[0];
2958
2959 create_signatured_type_table_from_index (per_bfd, section, types_list,
2960 types_list_elements);
2961 }
2962
2963 create_addrmap_from_index (per_objfile, map.get ());
2964
2965 per_bfd->index_table = std::move (map);
2966 per_bfd->using_index = 1;
2967 per_bfd->quick_file_names_table =
2968 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2969
2970 return 1;
2971 }
2972
2973 /* die_reader_func for dw2_get_file_names. */
2974
2975 static void
2976 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2977 struct die_info *comp_unit_die)
2978 {
2979 struct dwarf2_cu *cu = reader->cu;
2980 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2981 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2982 struct dwarf2_per_cu_data *lh_cu;
2983 struct attribute *attr;
2984 void **slot;
2985 struct quick_file_names *qfn;
2986
2987 gdb_assert (! this_cu->is_debug_types);
2988
2989 this_cu->v.quick->files_read = true;
2990 /* Our callers never want to match partial units -- instead they
2991 will match the enclosing full CU. */
2992 if (comp_unit_die->tag == DW_TAG_partial_unit)
2993 return;
2994
2995 lh_cu = this_cu;
2996 slot = NULL;
2997
2998 line_header_up lh;
2999 sect_offset line_offset {};
3000
3001 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3002 if (attr != nullptr && attr->form_is_unsigned ())
3003 {
3004 struct quick_file_names find_entry;
3005
3006 line_offset = (sect_offset) attr->as_unsigned ();
3007
3008 /* We may have already read in this line header (TU line header sharing).
3009 If we have we're done. */
3010 find_entry.hash.dwo_unit = cu->dwo_unit;
3011 find_entry.hash.line_sect_off = line_offset;
3012 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3013 &find_entry, INSERT);
3014 if (*slot != NULL)
3015 {
3016 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3017 return;
3018 }
3019
3020 lh = dwarf_decode_line_header (line_offset, cu);
3021 }
3022
3023 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
3024
3025 int offset = 0;
3026 if (!fnd.is_unknown ())
3027 ++offset;
3028 else if (lh == nullptr)
3029 return;
3030
3031 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3032 qfn->hash.dwo_unit = cu->dwo_unit;
3033 qfn->hash.line_sect_off = line_offset;
3034 /* There may not be a DW_AT_stmt_list. */
3035 if (slot != nullptr)
3036 *slot = qfn;
3037
3038 std::vector<const char *> include_names;
3039 if (lh != nullptr)
3040 {
3041 for (const auto &entry : lh->file_names ())
3042 {
3043 gdb::unique_xmalloc_ptr<char> name_holder;
3044 const char *include_name =
3045 compute_include_file_name (lh.get (), entry, fnd, &name_holder);
3046 if (include_name != nullptr)
3047 {
3048 include_name = per_objfile->objfile->intern (include_name);
3049 include_names.push_back (include_name);
3050 }
3051 }
3052 }
3053
3054 qfn->num_file_names = offset + include_names.size ();
3055 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
3056 qfn->file_names =
3057 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3058 qfn->num_file_names);
3059 if (offset != 0)
3060 qfn->file_names[0] = xstrdup (fnd.get_name ());
3061
3062 if (!include_names.empty ())
3063 memcpy (&qfn->file_names[offset], include_names.data (),
3064 include_names.size () * sizeof (const char *));
3065
3066 qfn->real_names = NULL;
3067
3068 lh_cu->v.quick->file_names = qfn;
3069 }
3070
3071 /* A helper for the "quick" functions which attempts to read the line
3072 table for THIS_CU. */
3073
3074 static struct quick_file_names *
3075 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3076 dwarf2_per_objfile *per_objfile)
3077 {
3078 /* This should never be called for TUs. */
3079 gdb_assert (! this_cu->is_debug_types);
3080 /* Nor type unit groups. */
3081 gdb_assert (! this_cu->type_unit_group_p ());
3082
3083 if (this_cu->v.quick->files_read)
3084 return this_cu->v.quick->file_names;
3085
3086 cutu_reader reader (this_cu, per_objfile);
3087 if (!reader.dummy_p)
3088 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3089
3090 return this_cu->v.quick->file_names;
3091 }
3092
3093 /* A helper for the "quick" functions which computes and caches the
3094 real path for a given file name from the line table. */
3095
3096 static const char *
3097 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3098 struct quick_file_names *qfn, int index)
3099 {
3100 if (qfn->real_names == NULL)
3101 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3102 qfn->num_file_names, const char *);
3103
3104 if (qfn->real_names[index] == NULL)
3105 {
3106 const char *dirname = nullptr;
3107
3108 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
3109 dirname = qfn->comp_dir;
3110
3111 gdb::unique_xmalloc_ptr<char> fullname;
3112 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
3113
3114 qfn->real_names[index] = fullname.release ();
3115 }
3116
3117 return qfn->real_names[index];
3118 }
3119
3120 struct symtab *
3121 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3122 {
3123 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3124 dwarf2_per_cu_data *dwarf_cu
3125 = per_objfile->per_bfd->all_comp_units.back ().get ();
3126 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3127
3128 if (cust == NULL)
3129 return NULL;
3130
3131 return cust->primary_filetab ();
3132 }
3133
3134 /* See read.h. */
3135
3136 void
3137 dwarf2_per_cu_data::free_cached_file_names ()
3138 {
3139 if (per_bfd == nullptr || !per_bfd->using_index || v.quick == nullptr)
3140 return;
3141
3142 struct quick_file_names *file_data = v.quick->file_names;
3143 if (file_data != nullptr && file_data->real_names != nullptr)
3144 {
3145 for (int i = 0; i < file_data->num_file_names; ++i)
3146 {
3147 xfree ((void *) file_data->real_names[i]);
3148 file_data->real_names[i] = nullptr;
3149 }
3150 }
3151 }
3152
3153 void
3154 dwarf2_base_index_functions::forget_cached_source_info
3155 (struct objfile *objfile)
3156 {
3157 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3158
3159 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
3160 per_cu->free_cached_file_names ();
3161 }
3162
3163 /* Struct used to manage iterating over all CUs looking for a symbol. */
3164
3165 struct dw2_symtab_iterator
3166 {
3167 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3168 dwarf2_per_objfile *per_objfile;
3169 /* If set, only look for symbols that match that block. Valid values are
3170 GLOBAL_BLOCK and STATIC_BLOCK. */
3171 gdb::optional<block_enum> block_index;
3172 /* The kind of symbol we're looking for. */
3173 domain_enum domain;
3174 /* The list of CUs from the index entry of the symbol,
3175 or NULL if not found. */
3176 offset_view vec;
3177 /* The next element in VEC to look at. */
3178 int next;
3179 /* The number of elements in VEC, or zero if there is no match. */
3180 int length;
3181 /* Have we seen a global version of the symbol?
3182 If so we can ignore all further global instances.
3183 This is to work around gold/15646, inefficient gold-generated
3184 indices. */
3185 int global_seen;
3186 };
3187
3188 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3189
3190 static void
3191 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3192 dwarf2_per_objfile *per_objfile,
3193 gdb::optional<block_enum> block_index,
3194 domain_enum domain, offset_type namei)
3195 {
3196 iter->per_objfile = per_objfile;
3197 iter->block_index = block_index;
3198 iter->domain = domain;
3199 iter->next = 0;
3200 iter->global_seen = 0;
3201 iter->vec = {};
3202 iter->length = 0;
3203
3204 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3205 /* index is NULL if OBJF_READNOW. */
3206 if (index == NULL)
3207 return;
3208
3209 gdb_assert (!index->symbol_name_slot_invalid (namei));
3210 offset_type vec_idx = index->symbol_vec_index (namei);
3211
3212 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3213 iter->length = iter->vec[0];
3214 }
3215
3216 /* Return the next matching CU or NULL if there are no more. */
3217
3218 static struct dwarf2_per_cu_data *
3219 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3220 {
3221 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3222
3223 for ( ; iter->next < iter->length; ++iter->next)
3224 {
3225 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3226 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3227 gdb_index_symbol_kind symbol_kind =
3228 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3229 /* Only check the symbol attributes if they're present.
3230 Indices prior to version 7 don't record them,
3231 and indices >= 7 may elide them for certain symbols
3232 (gold does this). */
3233 int attrs_valid =
3234 (per_objfile->per_bfd->index_table->version >= 7
3235 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3236
3237 /* Don't crash on bad data. */
3238 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3239 {
3240 complaint (_(".gdb_index entry has bad CU index"
3241 " [in module %s]"), objfile_name (per_objfile->objfile));
3242 continue;
3243 }
3244
3245 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3246
3247 /* Skip if already read in. */
3248 if (per_objfile->symtab_set_p (per_cu))
3249 continue;
3250
3251 /* Check static vs global. */
3252 if (attrs_valid)
3253 {
3254 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3255
3256 if (iter->block_index.has_value ())
3257 {
3258 bool want_static = *iter->block_index == STATIC_BLOCK;
3259
3260 if (is_static != want_static)
3261 continue;
3262 }
3263
3264 /* Work around gold/15646. */
3265 if (!is_static
3266 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3267 {
3268 if (iter->global_seen)
3269 continue;
3270
3271 iter->global_seen = 1;
3272 }
3273 }
3274
3275 /* Only check the symbol's kind if it has one. */
3276 if (attrs_valid)
3277 {
3278 switch (iter->domain)
3279 {
3280 case VAR_DOMAIN:
3281 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3282 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3283 /* Some types are also in VAR_DOMAIN. */
3284 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3285 continue;
3286 break;
3287 case STRUCT_DOMAIN:
3288 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3289 continue;
3290 break;
3291 case LABEL_DOMAIN:
3292 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3293 continue;
3294 break;
3295 case MODULE_DOMAIN:
3296 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3297 continue;
3298 break;
3299 default:
3300 break;
3301 }
3302 }
3303
3304 ++iter->next;
3305 return per_cu;
3306 }
3307
3308 return NULL;
3309 }
3310
3311 void
3312 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3313 bool print_bcache)
3314 {
3315 if (print_bcache)
3316 return;
3317
3318 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3319 int total = per_objfile->per_bfd->all_comp_units.size ();
3320 int count = 0;
3321
3322 for (int i = 0; i < total; ++i)
3323 {
3324 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3325
3326 if (!per_objfile->symtab_set_p (per_cu))
3327 ++count;
3328 }
3329 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3330 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3331 }
3332
3333 /* This dumps minimal information about the index.
3334 It is called via "mt print objfiles".
3335 One use is to verify .gdb_index has been loaded by the
3336 gdb.dwarf2/gdb-index.exp testcase. */
3337
3338 void
3339 dwarf2_gdb_index::dump (struct objfile *objfile)
3340 {
3341 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3342
3343 gdb_assert (per_objfile->per_bfd->using_index);
3344 gdb_printf (".gdb_index:");
3345 if (per_objfile->per_bfd->index_table != NULL)
3346 {
3347 gdb_printf (" version %d\n",
3348 per_objfile->per_bfd->index_table->version);
3349 }
3350 else
3351 gdb_printf (" faked for \"readnow\"\n");
3352 gdb_printf ("\n");
3353 }
3354
3355 void
3356 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3357 {
3358 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3359 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3360
3361 for (int i = 0; i < total_units; ++i)
3362 {
3363 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3364
3365 /* We don't want to directly expand a partial CU, because if we
3366 read it with the wrong language, then assertion failures can
3367 be triggered later on. See PR symtab/23010. So, tell
3368 dw2_instantiate_symtab to skip partial CUs -- any important
3369 partial CU will be read via DW_TAG_imported_unit anyway. */
3370 dw2_instantiate_symtab (per_cu, per_objfile, true);
3371 }
3372 }
3373
3374 static bool
3375 dw2_expand_symtabs_matching_symbol
3376 (mapped_index_base &index,
3377 const lookup_name_info &lookup_name_in,
3378 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3379 gdb::function_view<bool (offset_type)> match_callback,
3380 dwarf2_per_objfile *per_objfile);
3381
3382 static bool
3383 dw2_expand_symtabs_matching_one
3384 (dwarf2_per_cu_data *per_cu,
3385 dwarf2_per_objfile *per_objfile,
3386 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3387 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3388
3389 void
3390 dwarf2_gdb_index::expand_matching_symbols
3391 (struct objfile *objfile,
3392 const lookup_name_info &name, domain_enum domain,
3393 int global,
3394 symbol_compare_ftype *ordered_compare)
3395 {
3396 /* Used for Ada. */
3397 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3398
3399 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3400
3401 if (per_objfile->per_bfd->index_table != nullptr)
3402 {
3403 mapped_index &index = *per_objfile->per_bfd->index_table;
3404
3405 const char *match_name = name.ada ().lookup_name ().c_str ();
3406 auto matcher = [&] (const char *symname)
3407 {
3408 if (ordered_compare == nullptr)
3409 return true;
3410 return ordered_compare (symname, match_name) == 0;
3411 };
3412
3413 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3414 [&] (offset_type namei)
3415 {
3416 struct dw2_symtab_iterator iter;
3417 struct dwarf2_per_cu_data *per_cu;
3418
3419 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3420 namei);
3421 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3422 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3423 nullptr);
3424 return true;
3425 }, per_objfile);
3426 }
3427 else
3428 {
3429 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3430 proceed assuming all symtabs have been read in. */
3431 }
3432 }
3433
3434 /* Starting from a search name, return the string that finds the upper
3435 bound of all strings that start with SEARCH_NAME in a sorted name
3436 list. Returns the empty string to indicate that the upper bound is
3437 the end of the list. */
3438
3439 static std::string
3440 make_sort_after_prefix_name (const char *search_name)
3441 {
3442 /* When looking to complete "func", we find the upper bound of all
3443 symbols that start with "func" by looking for where we'd insert
3444 the closest string that would follow "func" in lexicographical
3445 order. Usually, that's "func"-with-last-character-incremented,
3446 i.e. "fund". Mind non-ASCII characters, though. Usually those
3447 will be UTF-8 multi-byte sequences, but we can't be certain.
3448 Especially mind the 0xff character, which is a valid character in
3449 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3450 rule out compilers allowing it in identifiers. Note that
3451 conveniently, strcmp/strcasecmp are specified to compare
3452 characters interpreted as unsigned char. So what we do is treat
3453 the whole string as a base 256 number composed of a sequence of
3454 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3455 to 0, and carries 1 to the following more-significant position.
3456 If the very first character in SEARCH_NAME ends up incremented
3457 and carries/overflows, then the upper bound is the end of the
3458 list. The string after the empty string is also the empty
3459 string.
3460
3461 Some examples of this operation:
3462
3463 SEARCH_NAME => "+1" RESULT
3464
3465 "abc" => "abd"
3466 "ab\xff" => "ac"
3467 "\xff" "a" "\xff" => "\xff" "b"
3468 "\xff" => ""
3469 "\xff\xff" => ""
3470 "" => ""
3471
3472 Then, with these symbols for example:
3473
3474 func
3475 func1
3476 fund
3477
3478 completing "func" looks for symbols between "func" and
3479 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3480 which finds "func" and "func1", but not "fund".
3481
3482 And with:
3483
3484 funcÿ (Latin1 'ÿ' [0xff])
3485 funcÿ1
3486 fund
3487
3488 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3489 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3490
3491 And with:
3492
3493 ÿÿ (Latin1 'ÿ' [0xff])
3494 ÿÿ1
3495
3496 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3497 the end of the list.
3498 */
3499 std::string after = search_name;
3500 while (!after.empty () && (unsigned char) after.back () == 0xff)
3501 after.pop_back ();
3502 if (!after.empty ())
3503 after.back () = (unsigned char) after.back () + 1;
3504 return after;
3505 }
3506
3507 /* See declaration. */
3508
3509 std::pair<std::vector<name_component>::const_iterator,
3510 std::vector<name_component>::const_iterator>
3511 mapped_index_base::find_name_components_bounds
3512 (const lookup_name_info &lookup_name_without_params, language lang,
3513 dwarf2_per_objfile *per_objfile) const
3514 {
3515 auto *name_cmp
3516 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3517
3518 const char *lang_name
3519 = lookup_name_without_params.language_lookup_name (lang);
3520
3521 /* Comparison function object for lower_bound that matches against a
3522 given symbol name. */
3523 auto lookup_compare_lower = [&] (const name_component &elem,
3524 const char *name)
3525 {
3526 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3527 const char *elem_name = elem_qualified + elem.name_offset;
3528 return name_cmp (elem_name, name) < 0;
3529 };
3530
3531 /* Comparison function object for upper_bound that matches against a
3532 given symbol name. */
3533 auto lookup_compare_upper = [&] (const char *name,
3534 const name_component &elem)
3535 {
3536 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3537 const char *elem_name = elem_qualified + elem.name_offset;
3538 return name_cmp (name, elem_name) < 0;
3539 };
3540
3541 auto begin = this->name_components.begin ();
3542 auto end = this->name_components.end ();
3543
3544 /* Find the lower bound. */
3545 auto lower = [&] ()
3546 {
3547 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3548 return begin;
3549 else
3550 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3551 } ();
3552
3553 /* Find the upper bound. */
3554 auto upper = [&] ()
3555 {
3556 if (lookup_name_without_params.completion_mode ())
3557 {
3558 /* In completion mode, we want UPPER to point past all
3559 symbols names that have the same prefix. I.e., with
3560 these symbols, and completing "func":
3561
3562 function << lower bound
3563 function1
3564 other_function << upper bound
3565
3566 We find the upper bound by looking for the insertion
3567 point of "func"-with-last-character-incremented,
3568 i.e. "fund". */
3569 std::string after = make_sort_after_prefix_name (lang_name);
3570 if (after.empty ())
3571 return end;
3572 return std::lower_bound (lower, end, after.c_str (),
3573 lookup_compare_lower);
3574 }
3575 else
3576 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3577 } ();
3578
3579 return {lower, upper};
3580 }
3581
3582 /* See declaration. */
3583
3584 void
3585 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3586 {
3587 if (!this->name_components.empty ())
3588 return;
3589
3590 this->name_components_casing = case_sensitivity;
3591 auto *name_cmp
3592 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3593
3594 /* The code below only knows how to break apart components of C++
3595 symbol names (and other languages that use '::' as
3596 namespace/module separator) and Ada symbol names. */
3597 auto count = this->symbol_name_count ();
3598 for (offset_type idx = 0; idx < count; idx++)
3599 {
3600 if (this->symbol_name_slot_invalid (idx))
3601 continue;
3602
3603 const char *name = this->symbol_name_at (idx, per_objfile);
3604
3605 /* Add each name component to the name component table. */
3606 unsigned int previous_len = 0;
3607
3608 if (strstr (name, "::") != nullptr)
3609 {
3610 for (unsigned int current_len = cp_find_first_component (name);
3611 name[current_len] != '\0';
3612 current_len += cp_find_first_component (name + current_len))
3613 {
3614 gdb_assert (name[current_len] == ':');
3615 this->name_components.push_back ({previous_len, idx});
3616 /* Skip the '::'. */
3617 current_len += 2;
3618 previous_len = current_len;
3619 }
3620 }
3621 else
3622 {
3623 /* Handle the Ada encoded (aka mangled) form here. */
3624 for (const char *iter = strstr (name, "__");
3625 iter != nullptr;
3626 iter = strstr (iter, "__"))
3627 {
3628 this->name_components.push_back ({previous_len, idx});
3629 iter += 2;
3630 previous_len = iter - name;
3631 }
3632 }
3633
3634 this->name_components.push_back ({previous_len, idx});
3635 }
3636
3637 /* Sort name_components elements by name. */
3638 auto name_comp_compare = [&] (const name_component &left,
3639 const name_component &right)
3640 {
3641 const char *left_qualified
3642 = this->symbol_name_at (left.idx, per_objfile);
3643 const char *right_qualified
3644 = this->symbol_name_at (right.idx, per_objfile);
3645
3646 const char *left_name = left_qualified + left.name_offset;
3647 const char *right_name = right_qualified + right.name_offset;
3648
3649 return name_cmp (left_name, right_name) < 0;
3650 };
3651
3652 std::sort (this->name_components.begin (),
3653 this->name_components.end (),
3654 name_comp_compare);
3655 }
3656
3657 /* Helper for dw2_expand_symtabs_matching that works with a
3658 mapped_index_base instead of the containing objfile. This is split
3659 to a separate function in order to be able to unit test the
3660 name_components matching using a mock mapped_index_base. For each
3661 symbol name that matches, calls MATCH_CALLBACK, passing it the
3662 symbol's index in the mapped_index_base symbol table. */
3663
3664 static bool
3665 dw2_expand_symtabs_matching_symbol
3666 (mapped_index_base &index,
3667 const lookup_name_info &lookup_name_in,
3668 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3669 gdb::function_view<bool (offset_type)> match_callback,
3670 dwarf2_per_objfile *per_objfile)
3671 {
3672 lookup_name_info lookup_name_without_params
3673 = lookup_name_in.make_ignore_params ();
3674
3675 /* Build the symbol name component sorted vector, if we haven't
3676 yet. */
3677 index.build_name_components (per_objfile);
3678
3679 /* The same symbol may appear more than once in the range though.
3680 E.g., if we're looking for symbols that complete "w", and we have
3681 a symbol named "w1::w2", we'll find the two name components for
3682 that same symbol in the range. To be sure we only call the
3683 callback once per symbol, we first collect the symbol name
3684 indexes that matched in a temporary vector and ignore
3685 duplicates. */
3686 std::vector<offset_type> matches;
3687
3688 struct name_and_matcher
3689 {
3690 symbol_name_matcher_ftype *matcher;
3691 const char *name;
3692
3693 bool operator== (const name_and_matcher &other) const
3694 {
3695 return matcher == other.matcher && strcmp (name, other.name) == 0;
3696 }
3697 };
3698
3699 /* A vector holding all the different symbol name matchers, for all
3700 languages. */
3701 std::vector<name_and_matcher> matchers;
3702
3703 for (int i = 0; i < nr_languages; i++)
3704 {
3705 enum language lang_e = (enum language) i;
3706
3707 const language_defn *lang = language_def (lang_e);
3708 symbol_name_matcher_ftype *name_matcher
3709 = lang->get_symbol_name_matcher (lookup_name_without_params);
3710
3711 name_and_matcher key {
3712 name_matcher,
3713 lookup_name_without_params.language_lookup_name (lang_e)
3714 };
3715
3716 /* Don't insert the same comparison routine more than once.
3717 Note that we do this linear walk. This is not a problem in
3718 practice because the number of supported languages is
3719 low. */
3720 if (std::find (matchers.begin (), matchers.end (), key)
3721 != matchers.end ())
3722 continue;
3723 matchers.push_back (std::move (key));
3724
3725 auto bounds
3726 = index.find_name_components_bounds (lookup_name_without_params,
3727 lang_e, per_objfile);
3728
3729 /* Now for each symbol name in range, check to see if we have a name
3730 match, and if so, call the MATCH_CALLBACK callback. */
3731
3732 for (; bounds.first != bounds.second; ++bounds.first)
3733 {
3734 const char *qualified
3735 = index.symbol_name_at (bounds.first->idx, per_objfile);
3736
3737 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3738 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3739 continue;
3740
3741 matches.push_back (bounds.first->idx);
3742 }
3743 }
3744
3745 std::sort (matches.begin (), matches.end ());
3746
3747 /* Finally call the callback, once per match. */
3748 ULONGEST prev = -1;
3749 bool result = true;
3750 for (offset_type idx : matches)
3751 {
3752 if (prev != idx)
3753 {
3754 if (!match_callback (idx))
3755 {
3756 result = false;
3757 break;
3758 }
3759 prev = idx;
3760 }
3761 }
3762
3763 /* Above we use a type wider than idx's for 'prev', since 0 and
3764 (offset_type)-1 are both possible values. */
3765 static_assert (sizeof (prev) > sizeof (offset_type), "");
3766
3767 return result;
3768 }
3769
3770 #if GDB_SELF_TEST
3771
3772 namespace selftests { namespace dw2_expand_symtabs_matching {
3773
3774 /* A mock .gdb_index/.debug_names-like name index table, enough to
3775 exercise dw2_expand_symtabs_matching_symbol, which works with the
3776 mapped_index_base interface. Builds an index from the symbol list
3777 passed as parameter to the constructor. */
3778 class mock_mapped_index : public mapped_index_base
3779 {
3780 public:
3781 mock_mapped_index (gdb::array_view<const char *> symbols)
3782 : m_symbol_table (symbols)
3783 {}
3784
3785 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3786
3787 /* Return the number of names in the symbol table. */
3788 size_t symbol_name_count () const override
3789 {
3790 return m_symbol_table.size ();
3791 }
3792
3793 /* Get the name of the symbol at IDX in the symbol table. */
3794 const char *symbol_name_at
3795 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3796 {
3797 return m_symbol_table[idx];
3798 }
3799
3800 private:
3801 gdb::array_view<const char *> m_symbol_table;
3802 };
3803
3804 /* Convenience function that converts a NULL pointer to a "<null>"
3805 string, to pass to print routines. */
3806
3807 static const char *
3808 string_or_null (const char *str)
3809 {
3810 return str != NULL ? str : "<null>";
3811 }
3812
3813 /* Check if a lookup_name_info built from
3814 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3815 index. EXPECTED_LIST is the list of expected matches, in expected
3816 matching order. If no match expected, then an empty list is
3817 specified. Returns true on success. On failure prints a warning
3818 indicating the file:line that failed, and returns false. */
3819
3820 static bool
3821 check_match (const char *file, int line,
3822 mock_mapped_index &mock_index,
3823 const char *name, symbol_name_match_type match_type,
3824 bool completion_mode,
3825 std::initializer_list<const char *> expected_list,
3826 dwarf2_per_objfile *per_objfile)
3827 {
3828 lookup_name_info lookup_name (name, match_type, completion_mode);
3829
3830 bool matched = true;
3831
3832 auto mismatch = [&] (const char *expected_str,
3833 const char *got)
3834 {
3835 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3836 "expected=\"%s\", got=\"%s\"\n"),
3837 file, line,
3838 (match_type == symbol_name_match_type::FULL
3839 ? "FULL" : "WILD"),
3840 name, string_or_null (expected_str), string_or_null (got));
3841 matched = false;
3842 };
3843
3844 auto expected_it = expected_list.begin ();
3845 auto expected_end = expected_list.end ();
3846
3847 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3848 nullptr,
3849 [&] (offset_type idx)
3850 {
3851 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3852 const char *expected_str
3853 = expected_it == expected_end ? NULL : *expected_it++;
3854
3855 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3856 mismatch (expected_str, matched_name);
3857 return true;
3858 }, per_objfile);
3859
3860 const char *expected_str
3861 = expected_it == expected_end ? NULL : *expected_it++;
3862 if (expected_str != NULL)
3863 mismatch (expected_str, NULL);
3864
3865 return matched;
3866 }
3867
3868 /* The symbols added to the mock mapped_index for testing (in
3869 canonical form). */
3870 static const char *test_symbols[] = {
3871 "function",
3872 "std::bar",
3873 "std::zfunction",
3874 "std::zfunction2",
3875 "w1::w2",
3876 "ns::foo<char*>",
3877 "ns::foo<int>",
3878 "ns::foo<long>",
3879 "ns2::tmpl<int>::foo2",
3880 "(anonymous namespace)::A::B::C",
3881
3882 /* These are used to check that the increment-last-char in the
3883 matching algorithm for completion doesn't match "t1_fund" when
3884 completing "t1_func". */
3885 "t1_func",
3886 "t1_func1",
3887 "t1_fund",
3888 "t1_fund1",
3889
3890 /* A UTF-8 name with multi-byte sequences to make sure that
3891 cp-name-parser understands this as a single identifier ("função"
3892 is "function" in PT). */
3893 u8"u8função",
3894
3895 /* \377 (0xff) is Latin1 'ÿ'. */
3896 "yfunc\377",
3897
3898 /* \377 (0xff) is Latin1 'ÿ'. */
3899 "\377",
3900 "\377\377123",
3901
3902 /* A name with all sorts of complications. Starts with "z" to make
3903 it easier for the completion tests below. */
3904 #define Z_SYM_NAME \
3905 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3906 "::tuple<(anonymous namespace)::ui*, " \
3907 "std::default_delete<(anonymous namespace)::ui>, void>"
3908
3909 Z_SYM_NAME
3910 };
3911
3912 /* Returns true if the mapped_index_base::find_name_component_bounds
3913 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3914 in completion mode. */
3915
3916 static bool
3917 check_find_bounds_finds (mapped_index_base &index,
3918 const char *search_name,
3919 gdb::array_view<const char *> expected_syms,
3920 dwarf2_per_objfile *per_objfile)
3921 {
3922 lookup_name_info lookup_name (search_name,
3923 symbol_name_match_type::FULL, true);
3924
3925 auto bounds = index.find_name_components_bounds (lookup_name,
3926 language_cplus,
3927 per_objfile);
3928
3929 size_t distance = std::distance (bounds.first, bounds.second);
3930 if (distance != expected_syms.size ())
3931 return false;
3932
3933 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3934 {
3935 auto nc_elem = bounds.first + exp_elem;
3936 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3937 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3938 return false;
3939 }
3940
3941 return true;
3942 }
3943
3944 /* Test the lower-level mapped_index::find_name_component_bounds
3945 method. */
3946
3947 static void
3948 test_mapped_index_find_name_component_bounds ()
3949 {
3950 mock_mapped_index mock_index (test_symbols);
3951
3952 mock_index.build_name_components (NULL /* per_objfile */);
3953
3954 /* Test the lower-level mapped_index::find_name_component_bounds
3955 method in completion mode. */
3956 {
3957 static const char *expected_syms[] = {
3958 "t1_func",
3959 "t1_func1",
3960 };
3961
3962 SELF_CHECK (check_find_bounds_finds
3963 (mock_index, "t1_func", expected_syms,
3964 NULL /* per_objfile */));
3965 }
3966
3967 /* Check that the increment-last-char in the name matching algorithm
3968 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3969 {
3970 static const char *expected_syms1[] = {
3971 "\377",
3972 "\377\377123",
3973 };
3974 SELF_CHECK (check_find_bounds_finds
3975 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3976
3977 static const char *expected_syms2[] = {
3978 "\377\377123",
3979 };
3980 SELF_CHECK (check_find_bounds_finds
3981 (mock_index, "\377\377", expected_syms2,
3982 NULL /* per_objfile */));
3983 }
3984 }
3985
3986 /* Test dw2_expand_symtabs_matching_symbol. */
3987
3988 static void
3989 test_dw2_expand_symtabs_matching_symbol ()
3990 {
3991 mock_mapped_index mock_index (test_symbols);
3992
3993 /* We let all tests run until the end even if some fails, for debug
3994 convenience. */
3995 bool any_mismatch = false;
3996
3997 /* Create the expected symbols list (an initializer_list). Needed
3998 because lists have commas, and we need to pass them to CHECK,
3999 which is a macro. */
4000 #define EXPECT(...) { __VA_ARGS__ }
4001
4002 /* Wrapper for check_match that passes down the current
4003 __FILE__/__LINE__. */
4004 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4005 any_mismatch |= !check_match (__FILE__, __LINE__, \
4006 mock_index, \
4007 NAME, MATCH_TYPE, COMPLETION_MODE, \
4008 EXPECTED_LIST, NULL)
4009
4010 /* Identity checks. */
4011 for (const char *sym : test_symbols)
4012 {
4013 /* Should be able to match all existing symbols. */
4014 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4015 EXPECT (sym));
4016
4017 /* Should be able to match all existing symbols with
4018 parameters. */
4019 std::string with_params = std::string (sym) + "(int)";
4020 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4021 EXPECT (sym));
4022
4023 /* Should be able to match all existing symbols with
4024 parameters and qualifiers. */
4025 with_params = std::string (sym) + " ( int ) const";
4026 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4027 EXPECT (sym));
4028
4029 /* This should really find sym, but cp-name-parser.y doesn't
4030 know about lvalue/rvalue qualifiers yet. */
4031 with_params = std::string (sym) + " ( int ) &&";
4032 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4033 {});
4034 }
4035
4036 /* Check that the name matching algorithm for completion doesn't get
4037 confused with Latin1 'ÿ' / 0xff. */
4038 {
4039 static const char str[] = "\377";
4040 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4041 EXPECT ("\377", "\377\377123"));
4042 }
4043
4044 /* Check that the increment-last-char in the matching algorithm for
4045 completion doesn't match "t1_fund" when completing "t1_func". */
4046 {
4047 static const char str[] = "t1_func";
4048 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4049 EXPECT ("t1_func", "t1_func1"));
4050 }
4051
4052 /* Check that completion mode works at each prefix of the expected
4053 symbol name. */
4054 {
4055 static const char str[] = "function(int)";
4056 size_t len = strlen (str);
4057 std::string lookup;
4058
4059 for (size_t i = 1; i < len; i++)
4060 {
4061 lookup.assign (str, i);
4062 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4063 EXPECT ("function"));
4064 }
4065 }
4066
4067 /* While "w" is a prefix of both components, the match function
4068 should still only be called once. */
4069 {
4070 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4071 EXPECT ("w1::w2"));
4072 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4073 EXPECT ("w1::w2"));
4074 }
4075
4076 /* Same, with a "complicated" symbol. */
4077 {
4078 static const char str[] = Z_SYM_NAME;
4079 size_t len = strlen (str);
4080 std::string lookup;
4081
4082 for (size_t i = 1; i < len; i++)
4083 {
4084 lookup.assign (str, i);
4085 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4086 EXPECT (Z_SYM_NAME));
4087 }
4088 }
4089
4090 /* In FULL mode, an incomplete symbol doesn't match. */
4091 {
4092 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4093 {});
4094 }
4095
4096 /* A complete symbol with parameters matches any overload, since the
4097 index has no overload info. */
4098 {
4099 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4100 EXPECT ("std::zfunction", "std::zfunction2"));
4101 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4102 EXPECT ("std::zfunction", "std::zfunction2"));
4103 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4104 EXPECT ("std::zfunction", "std::zfunction2"));
4105 }
4106
4107 /* Check that whitespace is ignored appropriately. A symbol with a
4108 template argument list. */
4109 {
4110 static const char expected[] = "ns::foo<int>";
4111 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4112 EXPECT (expected));
4113 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4114 EXPECT (expected));
4115 }
4116
4117 /* Check that whitespace is ignored appropriately. A symbol with a
4118 template argument list that includes a pointer. */
4119 {
4120 static const char expected[] = "ns::foo<char*>";
4121 /* Try both completion and non-completion modes. */
4122 static const bool completion_mode[2] = {false, true};
4123 for (size_t i = 0; i < 2; i++)
4124 {
4125 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4126 completion_mode[i], EXPECT (expected));
4127 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4128 completion_mode[i], EXPECT (expected));
4129
4130 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4131 completion_mode[i], EXPECT (expected));
4132 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4133 completion_mode[i], EXPECT (expected));
4134 }
4135 }
4136
4137 {
4138 /* Check method qualifiers are ignored. */
4139 static const char expected[] = "ns::foo<char*>";
4140 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4141 symbol_name_match_type::FULL, true, EXPECT (expected));
4142 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4143 symbol_name_match_type::FULL, true, EXPECT (expected));
4144 CHECK_MATCH ("foo < char * > ( int ) const",
4145 symbol_name_match_type::WILD, true, EXPECT (expected));
4146 CHECK_MATCH ("foo < char * > ( int ) &&",
4147 symbol_name_match_type::WILD, true, EXPECT (expected));
4148 }
4149
4150 /* Test lookup names that don't match anything. */
4151 {
4152 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4153 {});
4154
4155 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4156 {});
4157 }
4158
4159 /* Some wild matching tests, exercising "(anonymous namespace)",
4160 which should not be confused with a parameter list. */
4161 {
4162 static const char *syms[] = {
4163 "A::B::C",
4164 "B::C",
4165 "C",
4166 "A :: B :: C ( int )",
4167 "B :: C ( int )",
4168 "C ( int )",
4169 };
4170
4171 for (const char *s : syms)
4172 {
4173 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4174 EXPECT ("(anonymous namespace)::A::B::C"));
4175 }
4176 }
4177
4178 {
4179 static const char expected[] = "ns2::tmpl<int>::foo2";
4180 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4181 EXPECT (expected));
4182 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4183 EXPECT (expected));
4184 }
4185
4186 SELF_CHECK (!any_mismatch);
4187
4188 #undef EXPECT
4189 #undef CHECK_MATCH
4190 }
4191
4192 static void
4193 run_test ()
4194 {
4195 test_mapped_index_find_name_component_bounds ();
4196 test_dw2_expand_symtabs_matching_symbol ();
4197 }
4198
4199 }} // namespace selftests::dw2_expand_symtabs_matching
4200
4201 #endif /* GDB_SELF_TEST */
4202
4203 /* If FILE_MATCHER is NULL or if PER_CU has
4204 dwarf2_per_cu_quick_data::MARK set (see
4205 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4206 EXPANSION_NOTIFY on it. */
4207
4208 static bool
4209 dw2_expand_symtabs_matching_one
4210 (dwarf2_per_cu_data *per_cu,
4211 dwarf2_per_objfile *per_objfile,
4212 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4213 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4214 {
4215 if (file_matcher == NULL || per_cu->v.quick->mark)
4216 {
4217 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4218
4219 compunit_symtab *symtab
4220 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4221 gdb_assert (symtab != nullptr);
4222
4223 if (expansion_notify != NULL && symtab_was_null)
4224 return expansion_notify (symtab);
4225 }
4226 return true;
4227 }
4228
4229 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4230 matched, to expand corresponding CUs that were marked. IDX is the
4231 index of the symbol name that matched. */
4232
4233 static bool
4234 dw2_expand_marked_cus
4235 (dwarf2_per_objfile *per_objfile, offset_type idx,
4236 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4237 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4238 block_search_flags search_flags,
4239 search_domain kind)
4240 {
4241 offset_type vec_len, vec_idx;
4242 bool global_seen = false;
4243 mapped_index &index = *per_objfile->per_bfd->index_table;
4244
4245 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4246 vec_len = vec[0];
4247 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4248 {
4249 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4250 /* This value is only valid for index versions >= 7. */
4251 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4252 gdb_index_symbol_kind symbol_kind =
4253 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4254 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4255 /* Only check the symbol attributes if they're present.
4256 Indices prior to version 7 don't record them,
4257 and indices >= 7 may elide them for certain symbols
4258 (gold does this). */
4259 int attrs_valid =
4260 (index.version >= 7
4261 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4262
4263 /* Work around gold/15646. */
4264 if (attrs_valid
4265 && !is_static
4266 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4267 {
4268 if (global_seen)
4269 continue;
4270
4271 global_seen = true;
4272 }
4273
4274 /* Only check the symbol's kind if it has one. */
4275 if (attrs_valid)
4276 {
4277 if (is_static)
4278 {
4279 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4280 continue;
4281 }
4282 else
4283 {
4284 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4285 continue;
4286 }
4287
4288 switch (kind)
4289 {
4290 case VARIABLES_DOMAIN:
4291 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4292 continue;
4293 break;
4294 case FUNCTIONS_DOMAIN:
4295 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4296 continue;
4297 break;
4298 case TYPES_DOMAIN:
4299 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4300 continue;
4301 break;
4302 case MODULES_DOMAIN:
4303 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4304 continue;
4305 break;
4306 default:
4307 break;
4308 }
4309 }
4310
4311 /* Don't crash on bad data. */
4312 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4313 {
4314 complaint (_(".gdb_index entry has bad CU index"
4315 " [in module %s]"), objfile_name (per_objfile->objfile));
4316 continue;
4317 }
4318
4319 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4320 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4321 expansion_notify))
4322 return false;
4323 }
4324
4325 return true;
4326 }
4327
4328 /* If FILE_MATCHER is non-NULL, set all the
4329 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4330 that match FILE_MATCHER. */
4331
4332 static void
4333 dw_expand_symtabs_matching_file_matcher
4334 (dwarf2_per_objfile *per_objfile,
4335 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4336 {
4337 if (file_matcher == NULL)
4338 return;
4339
4340 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4341 htab_eq_pointer,
4342 NULL, xcalloc, xfree));
4343 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4344 htab_eq_pointer,
4345 NULL, xcalloc, xfree));
4346
4347 /* The rule is CUs specify all the files, including those used by
4348 any TU, so there's no need to scan TUs here. */
4349
4350 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4351 {
4352 QUIT;
4353
4354 if (per_cu->is_debug_types)
4355 continue;
4356 per_cu->v.quick->mark = 0;
4357
4358 /* We only need to look at symtabs not already expanded. */
4359 if (per_objfile->symtab_set_p (per_cu.get ()))
4360 continue;
4361
4362 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4363 per_objfile);
4364 if (file_data == NULL)
4365 continue;
4366
4367 if (htab_find (visited_not_found.get (), file_data) != NULL)
4368 continue;
4369 else if (htab_find (visited_found.get (), file_data) != NULL)
4370 {
4371 per_cu->v.quick->mark = 1;
4372 continue;
4373 }
4374
4375 for (int j = 0; j < file_data->num_file_names; ++j)
4376 {
4377 const char *this_real_name;
4378
4379 if (file_matcher (file_data->file_names[j], false))
4380 {
4381 per_cu->v.quick->mark = 1;
4382 break;
4383 }
4384
4385 /* Before we invoke realpath, which can get expensive when many
4386 files are involved, do a quick comparison of the basenames. */
4387 if (!basenames_may_differ
4388 && !file_matcher (lbasename (file_data->file_names[j]),
4389 true))
4390 continue;
4391
4392 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4393 if (file_matcher (this_real_name, false))
4394 {
4395 per_cu->v.quick->mark = 1;
4396 break;
4397 }
4398 }
4399
4400 void **slot = htab_find_slot (per_cu->v.quick->mark
4401 ? visited_found.get ()
4402 : visited_not_found.get (),
4403 file_data, INSERT);
4404 *slot = file_data;
4405 }
4406 }
4407
4408 bool
4409 dwarf2_gdb_index::expand_symtabs_matching
4410 (struct objfile *objfile,
4411 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4412 const lookup_name_info *lookup_name,
4413 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4414 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4415 block_search_flags search_flags,
4416 domain_enum domain,
4417 enum search_domain kind)
4418 {
4419 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4420
4421 /* index_table is NULL if OBJF_READNOW. */
4422 if (!per_objfile->per_bfd->index_table)
4423 return true;
4424
4425 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4426
4427 /* This invariant is documented in quick-functions.h. */
4428 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4429 if (lookup_name == nullptr)
4430 {
4431 for (dwarf2_per_cu_data *per_cu
4432 : all_comp_units_range (per_objfile->per_bfd))
4433 {
4434 QUIT;
4435
4436 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4437 file_matcher,
4438 expansion_notify))
4439 return false;
4440 }
4441 return true;
4442 }
4443
4444 mapped_index &index = *per_objfile->per_bfd->index_table;
4445
4446 bool result
4447 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4448 symbol_matcher,
4449 [&] (offset_type idx)
4450 {
4451 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4452 expansion_notify, search_flags, kind))
4453 return false;
4454 return true;
4455 }, per_objfile);
4456
4457 return result;
4458 }
4459
4460 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4461 symtab. */
4462
4463 static struct compunit_symtab *
4464 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4465 CORE_ADDR pc)
4466 {
4467 int i;
4468
4469 if (cust->blockvector () != nullptr
4470 && blockvector_contains_pc (cust->blockvector (), pc))
4471 return cust;
4472
4473 if (cust->includes == NULL)
4474 return NULL;
4475
4476 for (i = 0; cust->includes[i]; ++i)
4477 {
4478 struct compunit_symtab *s = cust->includes[i];
4479
4480 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4481 if (s != NULL)
4482 return s;
4483 }
4484
4485 return NULL;
4486 }
4487
4488 struct compunit_symtab *
4489 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4490 (struct objfile *objfile,
4491 struct bound_minimal_symbol msymbol,
4492 CORE_ADDR pc,
4493 struct obj_section *section,
4494 int warn_if_readin)
4495 {
4496 struct dwarf2_per_cu_data *data;
4497 struct compunit_symtab *result;
4498
4499 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4500 if (per_objfile->per_bfd->index_addrmap == nullptr)
4501 return NULL;
4502
4503 CORE_ADDR baseaddr = objfile->text_section_offset ();
4504 data = ((struct dwarf2_per_cu_data *)
4505 addrmap_find (per_objfile->per_bfd->index_addrmap,
4506 pc - baseaddr));
4507 if (!data)
4508 return NULL;
4509
4510 if (warn_if_readin && per_objfile->symtab_set_p (data))
4511 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4512 paddress (objfile->arch (), pc));
4513
4514 result = recursively_find_pc_sect_compunit_symtab
4515 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4516
4517 gdb_assert (result != NULL);
4518 return result;
4519 }
4520
4521 void
4522 dwarf2_base_index_functions::map_symbol_filenames
4523 (struct objfile *objfile,
4524 gdb::function_view<symbol_filename_ftype> fun,
4525 bool need_fullname)
4526 {
4527 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4528
4529 /* Use caches to ensure we only call FUN once for each filename. */
4530 filename_seen_cache filenames_cache;
4531 std::unordered_set<quick_file_names *> qfn_cache;
4532
4533 /* The rule is CUs specify all the files, including those used by any TU,
4534 so there's no need to scan TUs here. We can ignore file names coming
4535 from already-expanded CUs. It is possible that an expanded CU might
4536 reuse the file names data from a currently unexpanded CU, in this
4537 case we don't want to report the files from the unexpanded CU. */
4538
4539 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4540 {
4541 if (!per_cu->is_debug_types
4542 && per_objfile->symtab_set_p (per_cu.get ()))
4543 {
4544 if (per_cu->v.quick->file_names != nullptr)
4545 qfn_cache.insert (per_cu->v.quick->file_names);
4546 }
4547 }
4548
4549 for (dwarf2_per_cu_data *per_cu
4550 : all_comp_units_range (per_objfile->per_bfd))
4551 {
4552 /* We only need to look at symtabs not already expanded. */
4553 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4554 continue;
4555
4556 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4557 if (file_data == nullptr
4558 || qfn_cache.find (file_data) != qfn_cache.end ())
4559 continue;
4560
4561 for (int j = 0; j < file_data->num_file_names; ++j)
4562 {
4563 const char *filename = file_data->file_names[j];
4564 const char *key = filename;
4565 const char *fullname = nullptr;
4566
4567 if (need_fullname)
4568 {
4569 fullname = dw2_get_real_path (per_objfile, file_data, j);
4570 key = fullname;
4571 }
4572
4573 if (!filenames_cache.seen (key))
4574 fun (filename, fullname);
4575 }
4576 }
4577 }
4578
4579 bool
4580 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4581 {
4582 return true;
4583 }
4584
4585 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4586
4587 bool
4588 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4589 {
4590 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4591
4592 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4593 {
4594 /* Is this already expanded? */
4595 if (per_objfile->symtab_set_p (per_cu.get ()))
4596 continue;
4597
4598 /* It has not yet been expanded. */
4599 return true;
4600 }
4601
4602 return false;
4603 }
4604
4605 /* DWARF-5 debug_names reader. */
4606
4607 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4608 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4609
4610 /* A helper function that reads the .debug_names section in SECTION
4611 and fills in MAP. FILENAME is the name of the file containing the
4612 section; it is used for error reporting.
4613
4614 Returns true if all went well, false otherwise. */
4615
4616 static bool
4617 read_debug_names_from_section (struct objfile *objfile,
4618 const char *filename,
4619 struct dwarf2_section_info *section,
4620 mapped_debug_names &map)
4621 {
4622 if (section->empty ())
4623 return false;
4624
4625 /* Older elfutils strip versions could keep the section in the main
4626 executable while splitting it for the separate debug info file. */
4627 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4628 return false;
4629
4630 section->read (objfile);
4631
4632 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4633
4634 const gdb_byte *addr = section->buffer;
4635
4636 bfd *const abfd = section->get_bfd_owner ();
4637
4638 unsigned int bytes_read;
4639 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4640 addr += bytes_read;
4641
4642 map.dwarf5_is_dwarf64 = bytes_read != 4;
4643 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4644 if (bytes_read + length != section->size)
4645 {
4646 /* There may be multiple per-CU indices. */
4647 warning (_("Section .debug_names in %s length %s does not match "
4648 "section length %s, ignoring .debug_names."),
4649 filename, plongest (bytes_read + length),
4650 pulongest (section->size));
4651 return false;
4652 }
4653
4654 /* The version number. */
4655 uint16_t version = read_2_bytes (abfd, addr);
4656 addr += 2;
4657 if (version != 5)
4658 {
4659 warning (_("Section .debug_names in %s has unsupported version %d, "
4660 "ignoring .debug_names."),
4661 filename, version);
4662 return false;
4663 }
4664
4665 /* Padding. */
4666 uint16_t padding = read_2_bytes (abfd, addr);
4667 addr += 2;
4668 if (padding != 0)
4669 {
4670 warning (_("Section .debug_names in %s has unsupported padding %d, "
4671 "ignoring .debug_names."),
4672 filename, padding);
4673 return false;
4674 }
4675
4676 /* comp_unit_count - The number of CUs in the CU list. */
4677 map.cu_count = read_4_bytes (abfd, addr);
4678 addr += 4;
4679
4680 /* local_type_unit_count - The number of TUs in the local TU
4681 list. */
4682 map.tu_count = read_4_bytes (abfd, addr);
4683 addr += 4;
4684
4685 /* foreign_type_unit_count - The number of TUs in the foreign TU
4686 list. */
4687 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4688 addr += 4;
4689 if (foreign_tu_count != 0)
4690 {
4691 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4692 "ignoring .debug_names."),
4693 filename, static_cast<unsigned long> (foreign_tu_count));
4694 return false;
4695 }
4696
4697 /* bucket_count - The number of hash buckets in the hash lookup
4698 table. */
4699 map.bucket_count = read_4_bytes (abfd, addr);
4700 addr += 4;
4701
4702 /* name_count - The number of unique names in the index. */
4703 map.name_count = read_4_bytes (abfd, addr);
4704 addr += 4;
4705
4706 /* abbrev_table_size - The size in bytes of the abbreviations
4707 table. */
4708 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4709 addr += 4;
4710
4711 /* augmentation_string_size - The size in bytes of the augmentation
4712 string. This value is rounded up to a multiple of 4. */
4713 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4714 addr += 4;
4715 map.augmentation_is_gdb = ((augmentation_string_size
4716 == sizeof (dwarf5_augmentation))
4717 && memcmp (addr, dwarf5_augmentation,
4718 sizeof (dwarf5_augmentation)) == 0);
4719 augmentation_string_size += (-augmentation_string_size) & 3;
4720 addr += augmentation_string_size;
4721
4722 /* List of CUs */
4723 map.cu_table_reordered = addr;
4724 addr += map.cu_count * map.offset_size;
4725
4726 /* List of Local TUs */
4727 map.tu_table_reordered = addr;
4728 addr += map.tu_count * map.offset_size;
4729
4730 /* Hash Lookup Table */
4731 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4732 addr += map.bucket_count * 4;
4733 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4734 addr += map.name_count * 4;
4735
4736 /* Name Table */
4737 map.name_table_string_offs_reordered = addr;
4738 addr += map.name_count * map.offset_size;
4739 map.name_table_entry_offs_reordered = addr;
4740 addr += map.name_count * map.offset_size;
4741
4742 const gdb_byte *abbrev_table_start = addr;
4743 for (;;)
4744 {
4745 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4746 addr += bytes_read;
4747 if (index_num == 0)
4748 break;
4749
4750 const auto insertpair
4751 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4752 if (!insertpair.second)
4753 {
4754 warning (_("Section .debug_names in %s has duplicate index %s, "
4755 "ignoring .debug_names."),
4756 filename, pulongest (index_num));
4757 return false;
4758 }
4759 mapped_debug_names::index_val &indexval = insertpair.first->second;
4760 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4761 addr += bytes_read;
4762
4763 for (;;)
4764 {
4765 mapped_debug_names::index_val::attr attr;
4766 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4767 addr += bytes_read;
4768 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4769 addr += bytes_read;
4770 if (attr.form == DW_FORM_implicit_const)
4771 {
4772 attr.implicit_const = read_signed_leb128 (abfd, addr,
4773 &bytes_read);
4774 addr += bytes_read;
4775 }
4776 if (attr.dw_idx == 0 && attr.form == 0)
4777 break;
4778 indexval.attr_vec.push_back (std::move (attr));
4779 }
4780 }
4781 if (addr != abbrev_table_start + abbrev_table_size)
4782 {
4783 warning (_("Section .debug_names in %s has abbreviation_table "
4784 "of size %s vs. written as %u, ignoring .debug_names."),
4785 filename, plongest (addr - abbrev_table_start),
4786 abbrev_table_size);
4787 return false;
4788 }
4789 map.entry_pool = addr;
4790
4791 return true;
4792 }
4793
4794 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4795 list. */
4796
4797 static void
4798 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4799 const mapped_debug_names &map,
4800 dwarf2_section_info &section,
4801 bool is_dwz)
4802 {
4803 if (!map.augmentation_is_gdb)
4804 {
4805 for (uint32_t i = 0; i < map.cu_count; ++i)
4806 {
4807 sect_offset sect_off
4808 = (sect_offset) (extract_unsigned_integer
4809 (map.cu_table_reordered + i * map.offset_size,
4810 map.offset_size,
4811 map.dwarf5_byte_order));
4812 /* We don't know the length of the CU, because the CU list in a
4813 .debug_names index can be incomplete, so we can't use the start
4814 of the next CU as end of this CU. We create the CUs here with
4815 length 0, and in cutu_reader::cutu_reader we'll fill in the
4816 actual length. */
4817 dwarf2_per_cu_data_up per_cu
4818 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4819 sect_off, 0);
4820 per_bfd->all_comp_units.push_back (std::move (per_cu));
4821 }
4822 return;
4823 }
4824
4825 sect_offset sect_off_prev;
4826 for (uint32_t i = 0; i <= map.cu_count; ++i)
4827 {
4828 sect_offset sect_off_next;
4829 if (i < map.cu_count)
4830 {
4831 sect_off_next
4832 = (sect_offset) (extract_unsigned_integer
4833 (map.cu_table_reordered + i * map.offset_size,
4834 map.offset_size,
4835 map.dwarf5_byte_order));
4836 }
4837 else
4838 sect_off_next = (sect_offset) section.size;
4839 if (i >= 1)
4840 {
4841 const ULONGEST length = sect_off_next - sect_off_prev;
4842 dwarf2_per_cu_data_up per_cu
4843 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4844 sect_off_prev, length);
4845 per_bfd->all_comp_units.push_back (std::move (per_cu));
4846 }
4847 sect_off_prev = sect_off_next;
4848 }
4849 }
4850
4851 /* Read the CU list from the mapped index, and use it to create all
4852 the CU objects for this dwarf2_per_objfile. */
4853
4854 static void
4855 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4856 const mapped_debug_names &map,
4857 const mapped_debug_names &dwz_map)
4858 {
4859 gdb_assert (per_bfd->all_comp_units.empty ());
4860 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4861
4862 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4863 false /* is_dwz */);
4864
4865 if (dwz_map.cu_count == 0)
4866 return;
4867
4868 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4869 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4870 true /* is_dwz */);
4871 }
4872
4873 /* Read .debug_names. If everything went ok, initialize the "quick"
4874 elements of all the CUs and return true. Otherwise, return false. */
4875
4876 static bool
4877 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4878 {
4879 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4880 mapped_debug_names dwz_map;
4881 struct objfile *objfile = per_objfile->objfile;
4882 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4883
4884 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4885 &per_bfd->debug_names, *map))
4886 return false;
4887
4888 /* Don't use the index if it's empty. */
4889 if (map->name_count == 0)
4890 return false;
4891
4892 /* If there is a .dwz file, read it so we can get its CU list as
4893 well. */
4894 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4895 if (dwz != NULL)
4896 {
4897 if (!read_debug_names_from_section (objfile,
4898 bfd_get_filename (dwz->dwz_bfd.get ()),
4899 &dwz->debug_names, dwz_map))
4900 {
4901 warning (_("could not read '.debug_names' section from %s; skipping"),
4902 bfd_get_filename (dwz->dwz_bfd.get ()));
4903 return false;
4904 }
4905 }
4906
4907 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4908
4909 if (map->tu_count != 0)
4910 {
4911 /* We can only handle a single .debug_types when we have an
4912 index. */
4913 if (per_bfd->types.size () != 1)
4914 return false;
4915
4916 dwarf2_section_info *section = &per_bfd->types[0];
4917
4918 create_signatured_type_table_from_debug_names
4919 (per_objfile, *map, section, &per_bfd->abbrev);
4920 }
4921
4922 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4923
4924 per_bfd->debug_names_table = std::move (map);
4925 per_bfd->using_index = 1;
4926 per_bfd->quick_file_names_table =
4927 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4928
4929 return true;
4930 }
4931
4932 /* Type used to manage iterating over all CUs looking for a symbol for
4933 .debug_names. */
4934
4935 class dw2_debug_names_iterator
4936 {
4937 public:
4938 dw2_debug_names_iterator (const mapped_debug_names &map,
4939 block_search_flags block_index,
4940 domain_enum domain,
4941 const char *name, dwarf2_per_objfile *per_objfile)
4942 : m_map (map), m_block_index (block_index), m_domain (domain),
4943 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4944 m_per_objfile (per_objfile)
4945 {}
4946
4947 dw2_debug_names_iterator (const mapped_debug_names &map,
4948 search_domain search, uint32_t namei,
4949 dwarf2_per_objfile *per_objfile,
4950 domain_enum domain = UNDEF_DOMAIN)
4951 : m_map (map),
4952 m_domain (domain),
4953 m_search (search),
4954 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4955 m_per_objfile (per_objfile)
4956 {}
4957
4958 dw2_debug_names_iterator (const mapped_debug_names &map,
4959 block_search_flags block_index, domain_enum domain,
4960 uint32_t namei, dwarf2_per_objfile *per_objfile)
4961 : m_map (map), m_block_index (block_index), m_domain (domain),
4962 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4963 m_per_objfile (per_objfile)
4964 {}
4965
4966 /* Return the next matching CU or NULL if there are no more. */
4967 dwarf2_per_cu_data *next ();
4968
4969 private:
4970 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4971 const char *name,
4972 dwarf2_per_objfile *per_objfile);
4973 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4974 uint32_t namei,
4975 dwarf2_per_objfile *per_objfile);
4976
4977 /* The internalized form of .debug_names. */
4978 const mapped_debug_names &m_map;
4979
4980 /* Restrict the search to these blocks. */
4981 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4982 | SEARCH_STATIC_BLOCK);
4983
4984 /* The kind of symbol we're looking for. */
4985 const domain_enum m_domain = UNDEF_DOMAIN;
4986 const search_domain m_search = ALL_DOMAIN;
4987
4988 /* The list of CUs from the index entry of the symbol, or NULL if
4989 not found. */
4990 const gdb_byte *m_addr;
4991
4992 dwarf2_per_objfile *m_per_objfile;
4993 };
4994
4995 const char *
4996 mapped_debug_names::namei_to_name
4997 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4998 {
4999 const ULONGEST namei_string_offs
5000 = extract_unsigned_integer ((name_table_string_offs_reordered
5001 + namei * offset_size),
5002 offset_size,
5003 dwarf5_byte_order);
5004 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5005 }
5006
5007 /* Find a slot in .debug_names for the object named NAME. If NAME is
5008 found, return pointer to its pool data. If NAME cannot be found,
5009 return NULL. */
5010
5011 const gdb_byte *
5012 dw2_debug_names_iterator::find_vec_in_debug_names
5013 (const mapped_debug_names &map, const char *name,
5014 dwarf2_per_objfile *per_objfile)
5015 {
5016 int (*cmp) (const char *, const char *);
5017
5018 gdb::unique_xmalloc_ptr<char> without_params;
5019 if (current_language->la_language == language_cplus
5020 || current_language->la_language == language_fortran
5021 || current_language->la_language == language_d)
5022 {
5023 /* NAME is already canonical. Drop any qualifiers as
5024 .debug_names does not contain any. */
5025
5026 if (strchr (name, '(') != NULL)
5027 {
5028 without_params = cp_remove_params (name);
5029 if (without_params != NULL)
5030 name = without_params.get ();
5031 }
5032 }
5033
5034 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5035
5036 const uint32_t full_hash = dwarf5_djb_hash (name);
5037 uint32_t namei
5038 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5039 (map.bucket_table_reordered
5040 + (full_hash % map.bucket_count)), 4,
5041 map.dwarf5_byte_order);
5042 if (namei == 0)
5043 return NULL;
5044 --namei;
5045 if (namei >= map.name_count)
5046 {
5047 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5048 "[in module %s]"),
5049 namei, map.name_count,
5050 objfile_name (per_objfile->objfile));
5051 return NULL;
5052 }
5053
5054 for (;;)
5055 {
5056 const uint32_t namei_full_hash
5057 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5058 (map.hash_table_reordered + namei), 4,
5059 map.dwarf5_byte_order);
5060 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5061 return NULL;
5062
5063 if (full_hash == namei_full_hash)
5064 {
5065 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5066
5067 #if 0 /* An expensive sanity check. */
5068 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5069 {
5070 complaint (_("Wrong .debug_names hash for string at index %u "
5071 "[in module %s]"),
5072 namei, objfile_name (dwarf2_per_objfile->objfile));
5073 return NULL;
5074 }
5075 #endif
5076
5077 if (cmp (namei_string, name) == 0)
5078 {
5079 const ULONGEST namei_entry_offs
5080 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5081 + namei * map.offset_size),
5082 map.offset_size, map.dwarf5_byte_order);
5083 return map.entry_pool + namei_entry_offs;
5084 }
5085 }
5086
5087 ++namei;
5088 if (namei >= map.name_count)
5089 return NULL;
5090 }
5091 }
5092
5093 const gdb_byte *
5094 dw2_debug_names_iterator::find_vec_in_debug_names
5095 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5096 {
5097 if (namei >= map.name_count)
5098 {
5099 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5100 "[in module %s]"),
5101 namei, map.name_count,
5102 objfile_name (per_objfile->objfile));
5103 return NULL;
5104 }
5105
5106 const ULONGEST namei_entry_offs
5107 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5108 + namei * map.offset_size),
5109 map.offset_size, map.dwarf5_byte_order);
5110 return map.entry_pool + namei_entry_offs;
5111 }
5112
5113 /* See dw2_debug_names_iterator. */
5114
5115 dwarf2_per_cu_data *
5116 dw2_debug_names_iterator::next ()
5117 {
5118 if (m_addr == NULL)
5119 return NULL;
5120
5121 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5122 struct objfile *objfile = m_per_objfile->objfile;
5123 bfd *const abfd = objfile->obfd;
5124
5125 again:
5126
5127 unsigned int bytes_read;
5128 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5129 m_addr += bytes_read;
5130 if (abbrev == 0)
5131 return NULL;
5132
5133 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5134 if (indexval_it == m_map.abbrev_map.cend ())
5135 {
5136 complaint (_("Wrong .debug_names undefined abbrev code %s "
5137 "[in module %s]"),
5138 pulongest (abbrev), objfile_name (objfile));
5139 return NULL;
5140 }
5141 const mapped_debug_names::index_val &indexval = indexval_it->second;
5142 enum class symbol_linkage {
5143 unknown,
5144 static_,
5145 extern_,
5146 } symbol_linkage_ = symbol_linkage::unknown;
5147 dwarf2_per_cu_data *per_cu = NULL;
5148 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5149 {
5150 ULONGEST ull;
5151 switch (attr.form)
5152 {
5153 case DW_FORM_implicit_const:
5154 ull = attr.implicit_const;
5155 break;
5156 case DW_FORM_flag_present:
5157 ull = 1;
5158 break;
5159 case DW_FORM_udata:
5160 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5161 m_addr += bytes_read;
5162 break;
5163 case DW_FORM_ref4:
5164 ull = read_4_bytes (abfd, m_addr);
5165 m_addr += 4;
5166 break;
5167 case DW_FORM_ref8:
5168 ull = read_8_bytes (abfd, m_addr);
5169 m_addr += 8;
5170 break;
5171 case DW_FORM_ref_sig8:
5172 ull = read_8_bytes (abfd, m_addr);
5173 m_addr += 8;
5174 break;
5175 default:
5176 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5177 dwarf_form_name (attr.form),
5178 objfile_name (objfile));
5179 return NULL;
5180 }
5181 switch (attr.dw_idx)
5182 {
5183 case DW_IDX_compile_unit:
5184 /* Don't crash on bad data. */
5185 if (ull >= per_bfd->all_comp_units.size ())
5186 {
5187 complaint (_(".debug_names entry has bad CU index %s"
5188 " [in module %s]"),
5189 pulongest (ull),
5190 objfile_name (objfile));
5191 continue;
5192 }
5193 per_cu = per_bfd->get_cu (ull);
5194 break;
5195 case DW_IDX_type_unit:
5196 /* Don't crash on bad data. */
5197 if (ull >= per_bfd->tu_stats.nr_tus)
5198 {
5199 complaint (_(".debug_names entry has bad TU index %s"
5200 " [in module %s]"),
5201 pulongest (ull),
5202 objfile_name (objfile));
5203 continue;
5204 }
5205 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5206 break;
5207 case DW_IDX_die_offset:
5208 /* In a per-CU index (as opposed to a per-module index), index
5209 entries without CU attribute implicitly refer to the single CU. */
5210 if (per_cu == NULL)
5211 per_cu = per_bfd->get_cu (0);
5212 break;
5213 case DW_IDX_GNU_internal:
5214 if (!m_map.augmentation_is_gdb)
5215 break;
5216 symbol_linkage_ = symbol_linkage::static_;
5217 break;
5218 case DW_IDX_GNU_external:
5219 if (!m_map.augmentation_is_gdb)
5220 break;
5221 symbol_linkage_ = symbol_linkage::extern_;
5222 break;
5223 }
5224 }
5225
5226 /* Skip if already read in. */
5227 if (m_per_objfile->symtab_set_p (per_cu))
5228 goto again;
5229
5230 /* Check static vs global. */
5231 if (symbol_linkage_ != symbol_linkage::unknown)
5232 {
5233 if (symbol_linkage_ == symbol_linkage::static_)
5234 {
5235 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5236 goto again;
5237 }
5238 else
5239 {
5240 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5241 goto again;
5242 }
5243 }
5244
5245 /* Match dw2_symtab_iter_next, symbol_kind
5246 and debug_names::psymbol_tag. */
5247 switch (m_domain)
5248 {
5249 case VAR_DOMAIN:
5250 switch (indexval.dwarf_tag)
5251 {
5252 case DW_TAG_variable:
5253 case DW_TAG_subprogram:
5254 /* Some types are also in VAR_DOMAIN. */
5255 case DW_TAG_typedef:
5256 case DW_TAG_structure_type:
5257 break;
5258 default:
5259 goto again;
5260 }
5261 break;
5262 case STRUCT_DOMAIN:
5263 switch (indexval.dwarf_tag)
5264 {
5265 case DW_TAG_typedef:
5266 case DW_TAG_structure_type:
5267 break;
5268 default:
5269 goto again;
5270 }
5271 break;
5272 case LABEL_DOMAIN:
5273 switch (indexval.dwarf_tag)
5274 {
5275 case 0:
5276 case DW_TAG_variable:
5277 break;
5278 default:
5279 goto again;
5280 }
5281 break;
5282 case MODULE_DOMAIN:
5283 switch (indexval.dwarf_tag)
5284 {
5285 case DW_TAG_module:
5286 break;
5287 default:
5288 goto again;
5289 }
5290 break;
5291 default:
5292 break;
5293 }
5294
5295 /* Match dw2_expand_symtabs_matching, symbol_kind and
5296 debug_names::psymbol_tag. */
5297 switch (m_search)
5298 {
5299 case VARIABLES_DOMAIN:
5300 switch (indexval.dwarf_tag)
5301 {
5302 case DW_TAG_variable:
5303 break;
5304 default:
5305 goto again;
5306 }
5307 break;
5308 case FUNCTIONS_DOMAIN:
5309 switch (indexval.dwarf_tag)
5310 {
5311 case DW_TAG_subprogram:
5312 break;
5313 default:
5314 goto again;
5315 }
5316 break;
5317 case TYPES_DOMAIN:
5318 switch (indexval.dwarf_tag)
5319 {
5320 case DW_TAG_typedef:
5321 case DW_TAG_structure_type:
5322 break;
5323 default:
5324 goto again;
5325 }
5326 break;
5327 case MODULES_DOMAIN:
5328 switch (indexval.dwarf_tag)
5329 {
5330 case DW_TAG_module:
5331 break;
5332 default:
5333 goto again;
5334 }
5335 default:
5336 break;
5337 }
5338
5339 return per_cu;
5340 }
5341
5342 /* This dumps minimal information about .debug_names. It is called
5343 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5344 uses this to verify that .debug_names has been loaded. */
5345
5346 void
5347 dwarf2_debug_names_index::dump (struct objfile *objfile)
5348 {
5349 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5350
5351 gdb_assert (per_objfile->per_bfd->using_index);
5352 gdb_printf (".debug_names:");
5353 if (per_objfile->per_bfd->debug_names_table)
5354 gdb_printf (" exists\n");
5355 else
5356 gdb_printf (" faked for \"readnow\"\n");
5357 gdb_printf ("\n");
5358 }
5359
5360 void
5361 dwarf2_debug_names_index::expand_matching_symbols
5362 (struct objfile *objfile,
5363 const lookup_name_info &name, domain_enum domain,
5364 int global,
5365 symbol_compare_ftype *ordered_compare)
5366 {
5367 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5368
5369 /* debug_names_table is NULL if OBJF_READNOW. */
5370 if (!per_objfile->per_bfd->debug_names_table)
5371 return;
5372
5373 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5374 const block_search_flags block_flags
5375 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5376
5377 const char *match_name = name.ada ().lookup_name ().c_str ();
5378 auto matcher = [&] (const char *symname)
5379 {
5380 if (ordered_compare == nullptr)
5381 return true;
5382 return ordered_compare (symname, match_name) == 0;
5383 };
5384
5385 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5386 [&] (offset_type namei)
5387 {
5388 /* The name was matched, now expand corresponding CUs that were
5389 marked. */
5390 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5391 per_objfile);
5392
5393 struct dwarf2_per_cu_data *per_cu;
5394 while ((per_cu = iter.next ()) != NULL)
5395 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5396 nullptr);
5397 return true;
5398 }, per_objfile);
5399 }
5400
5401 bool
5402 dwarf2_debug_names_index::expand_symtabs_matching
5403 (struct objfile *objfile,
5404 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5405 const lookup_name_info *lookup_name,
5406 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5407 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5408 block_search_flags search_flags,
5409 domain_enum domain,
5410 enum search_domain kind)
5411 {
5412 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5413
5414 /* debug_names_table is NULL if OBJF_READNOW. */
5415 if (!per_objfile->per_bfd->debug_names_table)
5416 return true;
5417
5418 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5419
5420 /* This invariant is documented in quick-functions.h. */
5421 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5422 if (lookup_name == nullptr)
5423 {
5424 for (dwarf2_per_cu_data *per_cu
5425 : all_comp_units_range (per_objfile->per_bfd))
5426 {
5427 QUIT;
5428
5429 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5430 file_matcher,
5431 expansion_notify))
5432 return false;
5433 }
5434 return true;
5435 }
5436
5437 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5438
5439 bool result
5440 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5441 symbol_matcher,
5442 [&] (offset_type namei)
5443 {
5444 /* The name was matched, now expand corresponding CUs that were
5445 marked. */
5446 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5447
5448 struct dwarf2_per_cu_data *per_cu;
5449 while ((per_cu = iter.next ()) != NULL)
5450 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5451 file_matcher,
5452 expansion_notify))
5453 return false;
5454 return true;
5455 }, per_objfile);
5456
5457 return result;
5458 }
5459
5460 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5461 to either a dwarf2_per_bfd or dwz_file object. */
5462
5463 template <typename T>
5464 static gdb::array_view<const gdb_byte>
5465 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5466 {
5467 dwarf2_section_info *section = &section_owner->gdb_index;
5468
5469 if (section->empty ())
5470 return {};
5471
5472 /* Older elfutils strip versions could keep the section in the main
5473 executable while splitting it for the separate debug info file. */
5474 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5475 return {};
5476
5477 section->read (obj);
5478
5479 /* dwarf2_section_info::size is a bfd_size_type, while
5480 gdb::array_view works with size_t. On 32-bit hosts, with
5481 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5482 is 32-bit. So we need an explicit narrowing conversion here.
5483 This is fine, because it's impossible to allocate or mmap an
5484 array/buffer larger than what size_t can represent. */
5485 return gdb::make_array_view (section->buffer, section->size);
5486 }
5487
5488 /* Lookup the index cache for the contents of the index associated to
5489 DWARF2_OBJ. */
5490
5491 static gdb::array_view<const gdb_byte>
5492 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5493 {
5494 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5495 if (build_id == nullptr)
5496 return {};
5497
5498 return global_index_cache.lookup_gdb_index (build_id,
5499 &dwarf2_per_bfd->index_cache_res);
5500 }
5501
5502 /* Same as the above, but for DWZ. */
5503
5504 static gdb::array_view<const gdb_byte>
5505 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5506 {
5507 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5508 if (build_id == nullptr)
5509 return {};
5510
5511 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5512 }
5513
5514 /* See dwarf2/public.h. */
5515
5516 void
5517 dwarf2_initialize_objfile (struct objfile *objfile)
5518 {
5519 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5520 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5521
5522 dwarf_read_debug_printf ("called");
5523
5524 /* If we're about to read full symbols, don't bother with the
5525 indices. In this case we also don't care if some other debug
5526 format is making psymtabs, because they are all about to be
5527 expanded anyway. */
5528 if ((objfile->flags & OBJF_READNOW))
5529 {
5530 dwarf_read_debug_printf ("readnow requested");
5531
5532 /* When using READNOW, the using_index flag (set below) indicates that
5533 PER_BFD was already initialized, when we loaded some other objfile. */
5534 if (per_bfd->using_index)
5535 {
5536 dwarf_read_debug_printf ("using_index already set");
5537 objfile->qf.push_front (make_dwarf_gdb_index ());
5538 return;
5539 }
5540
5541 per_bfd->using_index = 1;
5542 create_all_comp_units (per_objfile);
5543 per_bfd->quick_file_names_table
5544 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5545
5546 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5547 {
5548 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5549
5550 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5551 struct dwarf2_per_cu_quick_data);
5552 }
5553
5554 /* Arrange for gdb to see the "quick" functions. However, these
5555 functions will be no-ops because we will have expanded all
5556 symtabs. */
5557 objfile->qf.push_front (make_dwarf_gdb_index ());
5558 return;
5559 }
5560
5561 /* Was a debug names index already read when we processed an objfile sharing
5562 PER_BFD? */
5563 if (per_bfd->debug_names_table != nullptr)
5564 {
5565 dwarf_read_debug_printf ("re-using shared debug names table");
5566 objfile->qf.push_front (make_dwarf_debug_names ());
5567 return;
5568 }
5569
5570 /* Was a GDB index already read when we processed an objfile sharing
5571 PER_BFD? */
5572 if (per_bfd->index_table != nullptr)
5573 {
5574 dwarf_read_debug_printf ("re-using shared index table");
5575 objfile->qf.push_front (make_dwarf_gdb_index ());
5576 return;
5577 }
5578
5579 /* There might already be partial symtabs built for this BFD. This happens
5580 when loading the same binary twice with the index-cache enabled. If so,
5581 don't try to read an index. The objfile / per_objfile initialization will
5582 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5583 code path. */
5584 if (per_bfd->partial_symtabs != nullptr)
5585 {
5586 dwarf_read_debug_printf ("re-using shared partial symtabs");
5587 objfile->qf.push_front (make_lazy_dwarf_reader ());
5588 return;
5589 }
5590
5591 if (dwarf2_read_debug_names (per_objfile))
5592 {
5593 dwarf_read_debug_printf ("found debug names");
5594 objfile->qf.push_front (make_dwarf_debug_names ());
5595 return;
5596 }
5597
5598 if (dwarf2_read_gdb_index (per_objfile,
5599 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5600 get_gdb_index_contents_from_section<dwz_file>))
5601 {
5602 dwarf_read_debug_printf ("found gdb index from file");
5603 objfile->qf.push_front (make_dwarf_gdb_index ());
5604 return;
5605 }
5606
5607 /* ... otherwise, try to find the index in the index cache. */
5608 if (dwarf2_read_gdb_index (per_objfile,
5609 get_gdb_index_contents_from_cache,
5610 get_gdb_index_contents_from_cache_dwz))
5611 {
5612 dwarf_read_debug_printf ("found gdb index from cache");
5613 global_index_cache.hit ();
5614 objfile->qf.push_front (make_dwarf_gdb_index ());
5615 return;
5616 }
5617
5618 global_index_cache.miss ();
5619 objfile->qf.push_front (make_lazy_dwarf_reader ());
5620 }
5621
5622 \f
5623
5624 /* Build a partial symbol table. */
5625
5626 void
5627 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5628 {
5629 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5630 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5631
5632 if (per_bfd->partial_symtabs != nullptr)
5633 {
5634 /* Partial symbols were already read, so now we can simply
5635 attach them. */
5636 if (psf == nullptr)
5637 {
5638 psf = new psymbol_functions (per_bfd->partial_symtabs);
5639 objfile->qf.emplace_front (psf);
5640 }
5641 else
5642 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5643 return;
5644 }
5645
5646 if (psf == nullptr)
5647 {
5648 psf = new psymbol_functions;
5649 objfile->qf.emplace_front (psf);
5650 }
5651 const std::shared_ptr<psymtab_storage> &partial_symtabs
5652 = psf->get_partial_symtabs ();
5653
5654 /* Set the local reference to partial symtabs, so that we don't try
5655 to read them again if reading another objfile with the same BFD.
5656 If we can't in fact share, this won't make a difference anyway as
5657 the dwarf2_per_bfd object won't be shared. */
5658 per_bfd->partial_symtabs = partial_symtabs;
5659
5660 try
5661 {
5662 /* This isn't really ideal: all the data we allocate on the
5663 objfile's obstack is still uselessly kept around. However,
5664 freeing it seems unsafe. */
5665 psymtab_discarder psymtabs (partial_symtabs.get ());
5666 dwarf2_build_psymtabs_hard (per_objfile);
5667 psymtabs.keep ();
5668
5669 /* (maybe) store an index in the cache. */
5670 global_index_cache.store (per_objfile);
5671 }
5672 catch (const gdb_exception_error &except)
5673 {
5674 exception_print (gdb_stderr, except);
5675 }
5676 }
5677
5678 /* Find the base address of the compilation unit for range lists and
5679 location lists. It will normally be specified by DW_AT_low_pc.
5680 In DWARF-3 draft 4, the base address could be overridden by
5681 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5682 compilation units with discontinuous ranges. */
5683
5684 static void
5685 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5686 {
5687 struct attribute *attr;
5688
5689 cu->base_address.reset ();
5690
5691 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5692 if (attr != nullptr)
5693 cu->base_address = attr->as_address ();
5694 else
5695 {
5696 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5697 if (attr != nullptr)
5698 cu->base_address = attr->as_address ();
5699 }
5700 }
5701
5702 /* Helper function that returns the proper abbrev section for
5703 THIS_CU. */
5704
5705 static struct dwarf2_section_info *
5706 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5707 {
5708 struct dwarf2_section_info *abbrev;
5709 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5710
5711 if (this_cu->is_dwz)
5712 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5713 else
5714 abbrev = &per_bfd->abbrev;
5715
5716 return abbrev;
5717 }
5718
5719 /* Fetch the abbreviation table offset from a comp or type unit header. */
5720
5721 static sect_offset
5722 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5723 struct dwarf2_section_info *section,
5724 sect_offset sect_off)
5725 {
5726 bfd *abfd = section->get_bfd_owner ();
5727 const gdb_byte *info_ptr;
5728 unsigned int initial_length_size, offset_size;
5729 uint16_t version;
5730
5731 section->read (per_objfile->objfile);
5732 info_ptr = section->buffer + to_underlying (sect_off);
5733 read_initial_length (abfd, info_ptr, &initial_length_size);
5734 offset_size = initial_length_size == 4 ? 4 : 8;
5735 info_ptr += initial_length_size;
5736
5737 version = read_2_bytes (abfd, info_ptr);
5738 info_ptr += 2;
5739 if (version >= 5)
5740 {
5741 /* Skip unit type and address size. */
5742 info_ptr += 2;
5743 }
5744
5745 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5746 }
5747
5748 /* A partial symtab that is used only for include files. */
5749 struct dwarf2_include_psymtab : public partial_symtab
5750 {
5751 dwarf2_include_psymtab (const char *filename,
5752 psymtab_storage *partial_symtabs,
5753 objfile_per_bfd_storage *objfile_per_bfd)
5754 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5755 {
5756 }
5757
5758 void read_symtab (struct objfile *objfile) override
5759 {
5760 /* It's an include file, no symbols to read for it.
5761 Everything is in the includer symtab. */
5762
5763 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5764 expansion of the includer psymtab. We use the dependencies[0] field to
5765 model the includer. But if we go the regular route of calling
5766 expand_psymtab here, and having expand_psymtab call expand_dependencies
5767 to expand the includer, we'll only use expand_psymtab on the includer
5768 (making it a non-toplevel psymtab), while if we expand the includer via
5769 another path, we'll use read_symtab (making it a toplevel psymtab).
5770 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5771 psymtab, and trigger read_symtab on the includer here directly. */
5772 includer ()->read_symtab (objfile);
5773 }
5774
5775 void expand_psymtab (struct objfile *objfile) override
5776 {
5777 /* This is not called by read_symtab, and should not be called by any
5778 expand_dependencies. */
5779 gdb_assert (false);
5780 }
5781
5782 bool readin_p (struct objfile *objfile) const override
5783 {
5784 return includer ()->readin_p (objfile);
5785 }
5786
5787 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5788 {
5789 compunit_symtab *cust = includer ()->get_compunit_symtab (objfile);
5790 while (cust != nullptr && cust->user != nullptr)
5791 cust = cust->user;
5792 return cust;
5793 }
5794
5795 private:
5796 partial_symtab *includer () const
5797 {
5798 /* An include psymtab has exactly one dependency: the psymtab that
5799 includes it. */
5800 gdb_assert (this->number_of_dependencies == 1);
5801 return this->dependencies[0];
5802 }
5803 };
5804
5805 /* Allocate a new partial symtab for file named NAME and mark this new
5806 partial symtab as being an include of PST. */
5807
5808 static void
5809 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5810 const char *name,
5811 dwarf2_psymtab *pst,
5812 psymtab_storage *partial_symtabs,
5813 objfile_per_bfd_storage *objfile_per_bfd)
5814 {
5815 dwarf2_include_psymtab *subpst
5816 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5817
5818 if (!IS_ABSOLUTE_PATH (subpst->filename))
5819 subpst->dirname = pst->dirname;
5820
5821 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5822 subpst->dependencies[0] = pst;
5823 subpst->number_of_dependencies = 1;
5824 }
5825
5826 /* Read the Line Number Program data and extract the list of files
5827 included by the source file represented by PST. Build an include
5828 partial symtab for each of these included files. */
5829
5830 static void
5831 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5832 struct die_info *die,
5833 const file_and_directory &fnd,
5834 dwarf2_psymtab *pst)
5835 {
5836 line_header_up lh;
5837 struct attribute *attr;
5838
5839 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5840 if (attr != nullptr && attr->form_is_unsigned ())
5841 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5842 if (lh == NULL)
5843 return; /* No linetable, so no includes. */
5844
5845 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5846 that we pass in the raw text_low here; that is ok because we're
5847 only decoding the line table to make include partial symtabs, and
5848 so the addresses aren't really used. */
5849 dwarf_decode_lines (lh.get (), fnd, cu, pst,
5850 pst->raw_text_low (), 1);
5851 }
5852
5853 static hashval_t
5854 hash_signatured_type (const void *item)
5855 {
5856 const struct signatured_type *sig_type
5857 = (const struct signatured_type *) item;
5858
5859 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5860 return sig_type->signature;
5861 }
5862
5863 static int
5864 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5865 {
5866 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5867 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5868
5869 return lhs->signature == rhs->signature;
5870 }
5871
5872 /* Allocate a hash table for signatured types. */
5873
5874 static htab_up
5875 allocate_signatured_type_table ()
5876 {
5877 return htab_up (htab_create_alloc (41,
5878 hash_signatured_type,
5879 eq_signatured_type,
5880 NULL, xcalloc, xfree));
5881 }
5882
5883 /* A helper for create_debug_types_hash_table. Read types from SECTION
5884 and fill them into TYPES_HTAB. It will process only type units,
5885 therefore DW_UT_type. */
5886
5887 static void
5888 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5889 struct dwo_file *dwo_file,
5890 dwarf2_section_info *section, htab_up &types_htab,
5891 rcuh_kind section_kind)
5892 {
5893 struct objfile *objfile = per_objfile->objfile;
5894 struct dwarf2_section_info *abbrev_section;
5895 bfd *abfd;
5896 const gdb_byte *info_ptr, *end_ptr;
5897
5898 abbrev_section = &dwo_file->sections.abbrev;
5899
5900 dwarf_read_debug_printf ("Reading %s for %s",
5901 section->get_name (),
5902 abbrev_section->get_file_name ());
5903
5904 section->read (objfile);
5905 info_ptr = section->buffer;
5906
5907 if (info_ptr == NULL)
5908 return;
5909
5910 /* We can't set abfd until now because the section may be empty or
5911 not present, in which case the bfd is unknown. */
5912 abfd = section->get_bfd_owner ();
5913
5914 /* We don't use cutu_reader here because we don't need to read
5915 any dies: the signature is in the header. */
5916
5917 end_ptr = info_ptr + section->size;
5918 while (info_ptr < end_ptr)
5919 {
5920 signatured_type_up sig_type;
5921 struct dwo_unit *dwo_tu;
5922 void **slot;
5923 const gdb_byte *ptr = info_ptr;
5924 struct comp_unit_head header;
5925 unsigned int length;
5926
5927 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5928
5929 /* Initialize it due to a false compiler warning. */
5930 header.signature = -1;
5931 header.type_cu_offset_in_tu = (cu_offset) -1;
5932
5933 /* We need to read the type's signature in order to build the hash
5934 table, but we don't need anything else just yet. */
5935
5936 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5937 abbrev_section, ptr, section_kind);
5938
5939 length = header.get_length ();
5940
5941 /* Skip dummy type units. */
5942 if (ptr >= info_ptr + length
5943 || peek_abbrev_code (abfd, ptr) == 0
5944 || (header.unit_type != DW_UT_type
5945 && header.unit_type != DW_UT_split_type))
5946 {
5947 info_ptr += length;
5948 continue;
5949 }
5950
5951 if (types_htab == NULL)
5952 types_htab = allocate_dwo_unit_table ();
5953
5954 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5955 dwo_tu->dwo_file = dwo_file;
5956 dwo_tu->signature = header.signature;
5957 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5958 dwo_tu->section = section;
5959 dwo_tu->sect_off = sect_off;
5960 dwo_tu->length = length;
5961
5962 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5963 gdb_assert (slot != NULL);
5964 if (*slot != NULL)
5965 complaint (_("debug type entry at offset %s is duplicate to"
5966 " the entry at offset %s, signature %s"),
5967 sect_offset_str (sect_off),
5968 sect_offset_str (dwo_tu->sect_off),
5969 hex_string (header.signature));
5970 *slot = dwo_tu;
5971
5972 dwarf_read_debug_printf_v (" offset %s, signature %s",
5973 sect_offset_str (sect_off),
5974 hex_string (header.signature));
5975
5976 info_ptr += length;
5977 }
5978 }
5979
5980 /* Create the hash table of all entries in the .debug_types
5981 (or .debug_types.dwo) section(s).
5982 DWO_FILE is a pointer to the DWO file object.
5983
5984 The result is a pointer to the hash table or NULL if there are no types.
5985
5986 Note: This function processes DWO files only, not DWP files. */
5987
5988 static void
5989 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5990 struct dwo_file *dwo_file,
5991 gdb::array_view<dwarf2_section_info> type_sections,
5992 htab_up &types_htab)
5993 {
5994 for (dwarf2_section_info &section : type_sections)
5995 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5996 rcuh_kind::TYPE);
5997 }
5998
5999 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6000 If SLOT is non-NULL, it is the entry to use in the hash table.
6001 Otherwise we find one. */
6002
6003 static struct signatured_type *
6004 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6005 {
6006 if (per_objfile->per_bfd->all_comp_units.size ()
6007 == per_objfile->per_bfd->all_comp_units.capacity ())
6008 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6009
6010 signatured_type_up sig_type_holder
6011 = per_objfile->per_bfd->allocate_signatured_type (sig);
6012 signatured_type *sig_type = sig_type_holder.get ();
6013
6014 per_objfile->per_bfd->all_comp_units.emplace_back
6015 (sig_type_holder.release ());
6016 if (per_objfile->per_bfd->using_index)
6017 {
6018 sig_type->v.quick =
6019 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6020 struct dwarf2_per_cu_quick_data);
6021 }
6022
6023 if (slot == NULL)
6024 {
6025 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6026 sig_type, INSERT);
6027 }
6028 gdb_assert (*slot == NULL);
6029 *slot = sig_type;
6030 /* The rest of sig_type must be filled in by the caller. */
6031 return sig_type;
6032 }
6033
6034 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6035 Fill in SIG_ENTRY with DWO_ENTRY. */
6036
6037 static void
6038 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6039 struct signatured_type *sig_entry,
6040 struct dwo_unit *dwo_entry)
6041 {
6042 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6043
6044 /* Make sure we're not clobbering something we don't expect to. */
6045 gdb_assert (! sig_entry->queued);
6046 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6047 if (per_bfd->using_index)
6048 {
6049 gdb_assert (sig_entry->v.quick != NULL);
6050 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6051 }
6052 else
6053 gdb_assert (sig_entry->v.psymtab == NULL);
6054 gdb_assert (sig_entry->signature == dwo_entry->signature);
6055 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6056 gdb_assert (sig_entry->type_unit_group == NULL);
6057 gdb_assert (sig_entry->dwo_unit == NULL);
6058
6059 sig_entry->section = dwo_entry->section;
6060 sig_entry->sect_off = dwo_entry->sect_off;
6061 sig_entry->length = dwo_entry->length;
6062 sig_entry->reading_dwo_directly = 1;
6063 sig_entry->per_bfd = per_bfd;
6064 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6065 sig_entry->dwo_unit = dwo_entry;
6066 }
6067
6068 /* Subroutine of lookup_signatured_type.
6069 If we haven't read the TU yet, create the signatured_type data structure
6070 for a TU to be read in directly from a DWO file, bypassing the stub.
6071 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6072 using .gdb_index, then when reading a CU we want to stay in the DWO file
6073 containing that CU. Otherwise we could end up reading several other DWO
6074 files (due to comdat folding) to process the transitive closure of all the
6075 mentioned TUs, and that can be slow. The current DWO file will have every
6076 type signature that it needs.
6077 We only do this for .gdb_index because in the psymtab case we already have
6078 to read all the DWOs to build the type unit groups. */
6079
6080 static struct signatured_type *
6081 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6082 {
6083 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6084 struct dwo_file *dwo_file;
6085 struct dwo_unit find_dwo_entry, *dwo_entry;
6086 void **slot;
6087
6088 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6089
6090 /* If TU skeletons have been removed then we may not have read in any
6091 TUs yet. */
6092 if (per_objfile->per_bfd->signatured_types == NULL)
6093 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6094
6095 /* We only ever need to read in one copy of a signatured type.
6096 Use the global signatured_types array to do our own comdat-folding
6097 of types. If this is the first time we're reading this TU, and
6098 the TU has an entry in .gdb_index, replace the recorded data from
6099 .gdb_index with this TU. */
6100
6101 signatured_type find_sig_entry (sig);
6102 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6103 &find_sig_entry, INSERT);
6104 signatured_type *sig_entry = (struct signatured_type *) *slot;
6105
6106 /* We can get here with the TU already read, *or* in the process of being
6107 read. Don't reassign the global entry to point to this DWO if that's
6108 the case. Also note that if the TU is already being read, it may not
6109 have come from a DWO, the program may be a mix of Fission-compiled
6110 code and non-Fission-compiled code. */
6111
6112 /* Have we already tried to read this TU?
6113 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6114 needn't exist in the global table yet). */
6115 if (sig_entry != NULL && sig_entry->tu_read)
6116 return sig_entry;
6117
6118 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6119 dwo_unit of the TU itself. */
6120 dwo_file = cu->dwo_unit->dwo_file;
6121
6122 /* Ok, this is the first time we're reading this TU. */
6123 if (dwo_file->tus == NULL)
6124 return NULL;
6125 find_dwo_entry.signature = sig;
6126 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6127 &find_dwo_entry);
6128 if (dwo_entry == NULL)
6129 return NULL;
6130
6131 /* If the global table doesn't have an entry for this TU, add one. */
6132 if (sig_entry == NULL)
6133 sig_entry = add_type_unit (per_objfile, sig, slot);
6134
6135 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6136 sig_entry->tu_read = 1;
6137 return sig_entry;
6138 }
6139
6140 /* Subroutine of lookup_signatured_type.
6141 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6142 then try the DWP file. If the TU stub (skeleton) has been removed then
6143 it won't be in .gdb_index. */
6144
6145 static struct signatured_type *
6146 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6147 {
6148 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6149 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6150 struct dwo_unit *dwo_entry;
6151 void **slot;
6152
6153 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6154 gdb_assert (dwp_file != NULL);
6155
6156 /* If TU skeletons have been removed then we may not have read in any
6157 TUs yet. */
6158 if (per_objfile->per_bfd->signatured_types == NULL)
6159 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6160
6161 signatured_type find_sig_entry (sig);
6162 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6163 &find_sig_entry, INSERT);
6164 signatured_type *sig_entry = (struct signatured_type *) *slot;
6165
6166 /* Have we already tried to read this TU?
6167 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6168 needn't exist in the global table yet). */
6169 if (sig_entry != NULL)
6170 return sig_entry;
6171
6172 if (dwp_file->tus == NULL)
6173 return NULL;
6174 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6175 1 /* is_debug_types */);
6176 if (dwo_entry == NULL)
6177 return NULL;
6178
6179 sig_entry = add_type_unit (per_objfile, sig, slot);
6180 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6181
6182 return sig_entry;
6183 }
6184
6185 /* Lookup a signature based type for DW_FORM_ref_sig8.
6186 Returns NULL if signature SIG is not present in the table.
6187 It is up to the caller to complain about this. */
6188
6189 static struct signatured_type *
6190 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6191 {
6192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6193
6194 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6195 {
6196 /* We're in a DWO/DWP file, and we're using .gdb_index.
6197 These cases require special processing. */
6198 if (get_dwp_file (per_objfile) == NULL)
6199 return lookup_dwo_signatured_type (cu, sig);
6200 else
6201 return lookup_dwp_signatured_type (cu, sig);
6202 }
6203 else
6204 {
6205 if (per_objfile->per_bfd->signatured_types == NULL)
6206 return NULL;
6207 signatured_type find_entry (sig);
6208 return ((struct signatured_type *)
6209 htab_find (per_objfile->per_bfd->signatured_types.get (),
6210 &find_entry));
6211 }
6212 }
6213
6214 /* Low level DIE reading support. */
6215
6216 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6217
6218 static void
6219 init_cu_die_reader (struct die_reader_specs *reader,
6220 struct dwarf2_cu *cu,
6221 struct dwarf2_section_info *section,
6222 struct dwo_file *dwo_file,
6223 struct abbrev_table *abbrev_table)
6224 {
6225 gdb_assert (section->readin && section->buffer != NULL);
6226 reader->abfd = section->get_bfd_owner ();
6227 reader->cu = cu;
6228 reader->dwo_file = dwo_file;
6229 reader->die_section = section;
6230 reader->buffer = section->buffer;
6231 reader->buffer_end = section->buffer + section->size;
6232 reader->abbrev_table = abbrev_table;
6233 }
6234
6235 /* Subroutine of cutu_reader to simplify it.
6236 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6237 There's just a lot of work to do, and cutu_reader is big enough
6238 already.
6239
6240 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6241 from it to the DIE in the DWO. If NULL we are skipping the stub.
6242 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6243 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6244 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6245 STUB_COMP_DIR may be non-NULL.
6246 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6247 are filled in with the info of the DIE from the DWO file.
6248 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6249 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6250 kept around for at least as long as *RESULT_READER.
6251
6252 The result is non-zero if a valid (non-dummy) DIE was found. */
6253
6254 static int
6255 read_cutu_die_from_dwo (dwarf2_cu *cu,
6256 struct dwo_unit *dwo_unit,
6257 struct die_info *stub_comp_unit_die,
6258 const char *stub_comp_dir,
6259 struct die_reader_specs *result_reader,
6260 const gdb_byte **result_info_ptr,
6261 struct die_info **result_comp_unit_die,
6262 abbrev_table_up *result_dwo_abbrev_table)
6263 {
6264 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6265 dwarf2_per_cu_data *per_cu = cu->per_cu;
6266 struct objfile *objfile = per_objfile->objfile;
6267 bfd *abfd;
6268 const gdb_byte *begin_info_ptr, *info_ptr;
6269 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6270 int i,num_extra_attrs;
6271 struct dwarf2_section_info *dwo_abbrev_section;
6272 struct die_info *comp_unit_die;
6273
6274 /* At most one of these may be provided. */
6275 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6276
6277 /* These attributes aren't processed until later:
6278 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6279 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6280 referenced later. However, these attributes are found in the stub
6281 which we won't have later. In order to not impose this complication
6282 on the rest of the code, we read them here and copy them to the
6283 DWO CU/TU die. */
6284
6285 stmt_list = NULL;
6286 low_pc = NULL;
6287 high_pc = NULL;
6288 ranges = NULL;
6289 comp_dir = NULL;
6290
6291 if (stub_comp_unit_die != NULL)
6292 {
6293 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6294 DWO file. */
6295 if (!per_cu->is_debug_types)
6296 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6297 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6298 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6299 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6300 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6301
6302 cu->addr_base = stub_comp_unit_die->addr_base ();
6303
6304 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6305 We need the value before we can process DW_AT_ranges values from the
6306 DWO. */
6307 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6308
6309 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6310 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6311 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6312 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6313 section. */
6314 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6315 }
6316 else if (stub_comp_dir != NULL)
6317 {
6318 /* Reconstruct the comp_dir attribute to simplify the code below. */
6319 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6320 comp_dir->name = DW_AT_comp_dir;
6321 comp_dir->form = DW_FORM_string;
6322 comp_dir->set_string_noncanonical (stub_comp_dir);
6323 }
6324
6325 /* Set up for reading the DWO CU/TU. */
6326 cu->dwo_unit = dwo_unit;
6327 dwarf2_section_info *section = dwo_unit->section;
6328 section->read (objfile);
6329 abfd = section->get_bfd_owner ();
6330 begin_info_ptr = info_ptr = (section->buffer
6331 + to_underlying (dwo_unit->sect_off));
6332 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6333
6334 if (per_cu->is_debug_types)
6335 {
6336 signatured_type *sig_type = (struct signatured_type *) per_cu;
6337
6338 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6339 section, dwo_abbrev_section,
6340 info_ptr, rcuh_kind::TYPE);
6341 /* This is not an assert because it can be caused by bad debug info. */
6342 if (sig_type->signature != cu->header.signature)
6343 {
6344 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6345 " TU at offset %s [in module %s]"),
6346 hex_string (sig_type->signature),
6347 hex_string (cu->header.signature),
6348 sect_offset_str (dwo_unit->sect_off),
6349 bfd_get_filename (abfd));
6350 }
6351 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6352 /* For DWOs coming from DWP files, we don't know the CU length
6353 nor the type's offset in the TU until now. */
6354 dwo_unit->length = cu->header.get_length ();
6355 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6356
6357 /* Establish the type offset that can be used to lookup the type.
6358 For DWO files, we don't know it until now. */
6359 sig_type->type_offset_in_section
6360 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6361 }
6362 else
6363 {
6364 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6365 section, dwo_abbrev_section,
6366 info_ptr, rcuh_kind::COMPILE);
6367 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6368 /* For DWOs coming from DWP files, we don't know the CU length
6369 until now. */
6370 dwo_unit->length = cu->header.get_length ();
6371 }
6372
6373 dwo_abbrev_section->read (objfile);
6374 *result_dwo_abbrev_table
6375 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6376 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6377 result_dwo_abbrev_table->get ());
6378
6379 /* Read in the die, but leave space to copy over the attributes
6380 from the stub. This has the benefit of simplifying the rest of
6381 the code - all the work to maintain the illusion of a single
6382 DW_TAG_{compile,type}_unit DIE is done here. */
6383 num_extra_attrs = ((stmt_list != NULL)
6384 + (low_pc != NULL)
6385 + (high_pc != NULL)
6386 + (ranges != NULL)
6387 + (comp_dir != NULL));
6388 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6389 num_extra_attrs);
6390
6391 /* Copy over the attributes from the stub to the DIE we just read in. */
6392 comp_unit_die = *result_comp_unit_die;
6393 i = comp_unit_die->num_attrs;
6394 if (stmt_list != NULL)
6395 comp_unit_die->attrs[i++] = *stmt_list;
6396 if (low_pc != NULL)
6397 comp_unit_die->attrs[i++] = *low_pc;
6398 if (high_pc != NULL)
6399 comp_unit_die->attrs[i++] = *high_pc;
6400 if (ranges != NULL)
6401 comp_unit_die->attrs[i++] = *ranges;
6402 if (comp_dir != NULL)
6403 comp_unit_die->attrs[i++] = *comp_dir;
6404 comp_unit_die->num_attrs += num_extra_attrs;
6405
6406 if (dwarf_die_debug)
6407 {
6408 gdb_printf (gdb_stdlog,
6409 "Read die from %s@0x%x of %s:\n",
6410 section->get_name (),
6411 (unsigned) (begin_info_ptr - section->buffer),
6412 bfd_get_filename (abfd));
6413 dump_die (comp_unit_die, dwarf_die_debug);
6414 }
6415
6416 /* Skip dummy compilation units. */
6417 if (info_ptr >= begin_info_ptr + dwo_unit->length
6418 || peek_abbrev_code (abfd, info_ptr) == 0)
6419 return 0;
6420
6421 *result_info_ptr = info_ptr;
6422 return 1;
6423 }
6424
6425 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6426 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6427 signature is part of the header. */
6428 static gdb::optional<ULONGEST>
6429 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6430 {
6431 if (cu->header.version >= 5)
6432 return cu->header.signature;
6433 struct attribute *attr;
6434 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6435 if (attr == nullptr || !attr->form_is_unsigned ())
6436 return gdb::optional<ULONGEST> ();
6437 return attr->as_unsigned ();
6438 }
6439
6440 /* Subroutine of cutu_reader to simplify it.
6441 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6442 Returns NULL if the specified DWO unit cannot be found. */
6443
6444 static struct dwo_unit *
6445 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6446 {
6447 dwarf2_per_cu_data *per_cu = cu->per_cu;
6448 struct dwo_unit *dwo_unit;
6449 const char *comp_dir;
6450
6451 gdb_assert (cu != NULL);
6452
6453 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6454 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6455 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6456
6457 if (per_cu->is_debug_types)
6458 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6459 else
6460 {
6461 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6462
6463 if (!signature.has_value ())
6464 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6465 " [in module %s]"),
6466 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6467
6468 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6469 }
6470
6471 return dwo_unit;
6472 }
6473
6474 /* Subroutine of cutu_reader to simplify it.
6475 See it for a description of the parameters.
6476 Read a TU directly from a DWO file, bypassing the stub. */
6477
6478 void
6479 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6480 dwarf2_per_objfile *per_objfile,
6481 dwarf2_cu *existing_cu)
6482 {
6483 struct signatured_type *sig_type;
6484
6485 /* Verify we can do the following downcast, and that we have the
6486 data we need. */
6487 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6488 sig_type = (struct signatured_type *) this_cu;
6489 gdb_assert (sig_type->dwo_unit != NULL);
6490
6491 dwarf2_cu *cu;
6492
6493 if (existing_cu != nullptr)
6494 {
6495 cu = existing_cu;
6496 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6497 /* There's no need to do the rereading_dwo_cu handling that
6498 cutu_reader does since we don't read the stub. */
6499 }
6500 else
6501 {
6502 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6503 in per_objfile yet. */
6504 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6505 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6506 cu = m_new_cu.get ();
6507 }
6508
6509 /* A future optimization, if needed, would be to use an existing
6510 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6511 could share abbrev tables. */
6512
6513 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6514 NULL /* stub_comp_unit_die */,
6515 sig_type->dwo_unit->dwo_file->comp_dir,
6516 this, &info_ptr,
6517 &comp_unit_die,
6518 &m_dwo_abbrev_table) == 0)
6519 {
6520 /* Dummy die. */
6521 dummy_p = true;
6522 }
6523 }
6524
6525 /* Initialize a CU (or TU) and read its DIEs.
6526 If the CU defers to a DWO file, read the DWO file as well.
6527
6528 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6529 Otherwise the table specified in the comp unit header is read in and used.
6530 This is an optimization for when we already have the abbrev table.
6531
6532 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6533 allocated. */
6534
6535 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6536 dwarf2_per_objfile *per_objfile,
6537 struct abbrev_table *abbrev_table,
6538 dwarf2_cu *existing_cu,
6539 bool skip_partial)
6540 : die_reader_specs {},
6541 m_this_cu (this_cu)
6542 {
6543 struct objfile *objfile = per_objfile->objfile;
6544 struct dwarf2_section_info *section = this_cu->section;
6545 bfd *abfd = section->get_bfd_owner ();
6546 const gdb_byte *begin_info_ptr;
6547 struct signatured_type *sig_type = NULL;
6548 struct dwarf2_section_info *abbrev_section;
6549 /* Non-zero if CU currently points to a DWO file and we need to
6550 reread it. When this happens we need to reread the skeleton die
6551 before we can reread the DWO file (this only applies to CUs, not TUs). */
6552 int rereading_dwo_cu = 0;
6553
6554 if (dwarf_die_debug)
6555 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6556 this_cu->is_debug_types ? "type" : "comp",
6557 sect_offset_str (this_cu->sect_off));
6558
6559 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6560 file (instead of going through the stub), short-circuit all of this. */
6561 if (this_cu->reading_dwo_directly)
6562 {
6563 /* Narrow down the scope of possibilities to have to understand. */
6564 gdb_assert (this_cu->is_debug_types);
6565 gdb_assert (abbrev_table == NULL);
6566 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6567 return;
6568 }
6569
6570 /* This is cheap if the section is already read in. */
6571 section->read (objfile);
6572
6573 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6574
6575 abbrev_section = get_abbrev_section_for_cu (this_cu);
6576
6577 dwarf2_cu *cu;
6578
6579 if (existing_cu != nullptr)
6580 {
6581 cu = existing_cu;
6582 /* If this CU is from a DWO file we need to start over, we need to
6583 refetch the attributes from the skeleton CU.
6584 This could be optimized by retrieving those attributes from when we
6585 were here the first time: the previous comp_unit_die was stored in
6586 comp_unit_obstack. But there's no data yet that we need this
6587 optimization. */
6588 if (cu->dwo_unit != NULL)
6589 rereading_dwo_cu = 1;
6590 }
6591 else
6592 {
6593 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6594 in per_objfile yet. */
6595 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6596 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6597 cu = m_new_cu.get ();
6598 }
6599
6600 /* Get the header. */
6601 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6602 {
6603 /* We already have the header, there's no need to read it in again. */
6604 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6605 }
6606 else
6607 {
6608 if (this_cu->is_debug_types)
6609 {
6610 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6611 section, abbrev_section,
6612 info_ptr, rcuh_kind::TYPE);
6613
6614 /* Since per_cu is the first member of struct signatured_type,
6615 we can go from a pointer to one to a pointer to the other. */
6616 sig_type = (struct signatured_type *) this_cu;
6617 gdb_assert (sig_type->signature == cu->header.signature);
6618 gdb_assert (sig_type->type_offset_in_tu
6619 == cu->header.type_cu_offset_in_tu);
6620 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6621
6622 /* LENGTH has not been set yet for type units if we're
6623 using .gdb_index. */
6624 this_cu->length = cu->header.get_length ();
6625
6626 /* Establish the type offset that can be used to lookup the type. */
6627 sig_type->type_offset_in_section =
6628 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6629
6630 this_cu->dwarf_version = cu->header.version;
6631 }
6632 else
6633 {
6634 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6635 section, abbrev_section,
6636 info_ptr,
6637 rcuh_kind::COMPILE);
6638
6639 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6640 if (this_cu->length == 0)
6641 this_cu->length = cu->header.get_length ();
6642 else
6643 gdb_assert (this_cu->length == cu->header.get_length ());
6644 this_cu->dwarf_version = cu->header.version;
6645 }
6646 }
6647
6648 /* Skip dummy compilation units. */
6649 if (info_ptr >= begin_info_ptr + this_cu->length
6650 || peek_abbrev_code (abfd, info_ptr) == 0)
6651 {
6652 dummy_p = true;
6653 return;
6654 }
6655
6656 /* If we don't have them yet, read the abbrevs for this compilation unit.
6657 And if we need to read them now, make sure they're freed when we're
6658 done. */
6659 if (abbrev_table != NULL)
6660 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6661 else
6662 {
6663 abbrev_section->read (objfile);
6664 m_abbrev_table_holder
6665 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6666 abbrev_table = m_abbrev_table_holder.get ();
6667 }
6668
6669 /* Read the top level CU/TU die. */
6670 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6671 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6672
6673 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6674 {
6675 dummy_p = true;
6676 return;
6677 }
6678
6679 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6680 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6681 table from the DWO file and pass the ownership over to us. It will be
6682 referenced from READER, so we must make sure to free it after we're done
6683 with READER.
6684
6685 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6686 DWO CU, that this test will fail (the attribute will not be present). */
6687 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6688 if (dwo_name != nullptr)
6689 {
6690 struct dwo_unit *dwo_unit;
6691 struct die_info *dwo_comp_unit_die;
6692
6693 if (comp_unit_die->has_children)
6694 {
6695 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6696 " has children (offset %s) [in module %s]"),
6697 sect_offset_str (this_cu->sect_off),
6698 bfd_get_filename (abfd));
6699 }
6700 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6701 if (dwo_unit != NULL)
6702 {
6703 if (read_cutu_die_from_dwo (cu, dwo_unit,
6704 comp_unit_die, NULL,
6705 this, &info_ptr,
6706 &dwo_comp_unit_die,
6707 &m_dwo_abbrev_table) == 0)
6708 {
6709 /* Dummy die. */
6710 dummy_p = true;
6711 return;
6712 }
6713 comp_unit_die = dwo_comp_unit_die;
6714 }
6715 else
6716 {
6717 /* Yikes, we couldn't find the rest of the DIE, we only have
6718 the stub. A complaint has already been logged. There's
6719 not much more we can do except pass on the stub DIE to
6720 die_reader_func. We don't want to throw an error on bad
6721 debug info. */
6722 }
6723 }
6724 }
6725
6726 void
6727 cutu_reader::keep ()
6728 {
6729 /* Done, clean up. */
6730 gdb_assert (!dummy_p);
6731 if (m_new_cu != NULL)
6732 {
6733 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6734 now. */
6735 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6736 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6737 }
6738 }
6739
6740 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6741 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6742 assumed to have already done the lookup to find the DWO file).
6743
6744 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6745 THIS_CU->is_debug_types, but nothing else.
6746
6747 We fill in THIS_CU->length.
6748
6749 THIS_CU->cu is always freed when done.
6750 This is done in order to not leave THIS_CU->cu in a state where we have
6751 to care whether it refers to the "main" CU or the DWO CU.
6752
6753 When parent_cu is passed, it is used to provide a default value for
6754 str_offsets_base and addr_base from the parent. */
6755
6756 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6757 dwarf2_per_objfile *per_objfile,
6758 struct dwarf2_cu *parent_cu,
6759 struct dwo_file *dwo_file)
6760 : die_reader_specs {},
6761 m_this_cu (this_cu)
6762 {
6763 struct objfile *objfile = per_objfile->objfile;
6764 struct dwarf2_section_info *section = this_cu->section;
6765 bfd *abfd = section->get_bfd_owner ();
6766 struct dwarf2_section_info *abbrev_section;
6767 const gdb_byte *begin_info_ptr, *info_ptr;
6768
6769 if (dwarf_die_debug)
6770 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6771 this_cu->is_debug_types ? "type" : "comp",
6772 sect_offset_str (this_cu->sect_off));
6773
6774 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6775
6776 abbrev_section = (dwo_file != NULL
6777 ? &dwo_file->sections.abbrev
6778 : get_abbrev_section_for_cu (this_cu));
6779
6780 /* This is cheap if the section is already read in. */
6781 section->read (objfile);
6782
6783 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6784
6785 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6786 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6787 section, abbrev_section, info_ptr,
6788 (this_cu->is_debug_types
6789 ? rcuh_kind::TYPE
6790 : rcuh_kind::COMPILE));
6791
6792 if (parent_cu != nullptr)
6793 {
6794 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6795 m_new_cu->addr_base = parent_cu->addr_base;
6796 }
6797 this_cu->length = m_new_cu->header.get_length ();
6798
6799 /* Skip dummy compilation units. */
6800 if (info_ptr >= begin_info_ptr + this_cu->length
6801 || peek_abbrev_code (abfd, info_ptr) == 0)
6802 {
6803 dummy_p = true;
6804 return;
6805 }
6806
6807 abbrev_section->read (objfile);
6808 m_abbrev_table_holder
6809 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6810
6811 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6812 m_abbrev_table_holder.get ());
6813 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6814 }
6815
6816 \f
6817 /* Type Unit Groups.
6818
6819 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6820 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6821 so that all types coming from the same compilation (.o file) are grouped
6822 together. A future step could be to put the types in the same symtab as
6823 the CU the types ultimately came from. */
6824
6825 static hashval_t
6826 hash_type_unit_group (const void *item)
6827 {
6828 const struct type_unit_group *tu_group
6829 = (const struct type_unit_group *) item;
6830
6831 return hash_stmt_list_entry (&tu_group->hash);
6832 }
6833
6834 static int
6835 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6836 {
6837 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6838 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6839
6840 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6841 }
6842
6843 /* Allocate a hash table for type unit groups. */
6844
6845 static htab_up
6846 allocate_type_unit_groups_table ()
6847 {
6848 return htab_up (htab_create_alloc (3,
6849 hash_type_unit_group,
6850 eq_type_unit_group,
6851 htab_delete_entry<type_unit_group>,
6852 xcalloc, xfree));
6853 }
6854
6855 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6856 partial symtabs. We combine several TUs per psymtab to not let the size
6857 of any one psymtab grow too big. */
6858 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6859 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6860
6861 /* Helper routine for get_type_unit_group.
6862 Create the type_unit_group object used to hold one or more TUs. */
6863
6864 static std::unique_ptr<type_unit_group>
6865 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6866 {
6867 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6868 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6869
6870 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6871 tu_group->per_bfd = per_bfd;
6872
6873 if (per_bfd->using_index)
6874 {
6875 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6876 struct dwarf2_per_cu_quick_data);
6877 }
6878 else
6879 {
6880 unsigned int line_offset = to_underlying (line_offset_struct);
6881 dwarf2_psymtab *pst;
6882 std::string name;
6883
6884 /* Give the symtab a useful name for debug purposes. */
6885 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6886 name = string_printf ("<type_units_%d>",
6887 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6888 else
6889 name = string_printf ("<type_units_at_0x%x>", line_offset);
6890
6891 pst = create_partial_symtab (tu_group.get (), per_objfile,
6892 name.c_str ());
6893 pst->anonymous = true;
6894 }
6895
6896 tu_group->hash.dwo_unit = cu->dwo_unit;
6897 tu_group->hash.line_sect_off = line_offset_struct;
6898
6899 return tu_group;
6900 }
6901
6902 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6903 STMT_LIST is a DW_AT_stmt_list attribute. */
6904
6905 static struct type_unit_group *
6906 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6907 {
6908 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6909 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6910 struct type_unit_group *tu_group;
6911 void **slot;
6912 unsigned int line_offset;
6913 struct type_unit_group type_unit_group_for_lookup;
6914
6915 if (per_objfile->per_bfd->type_unit_groups == NULL)
6916 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6917
6918 /* Do we need to create a new group, or can we use an existing one? */
6919
6920 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6921 {
6922 line_offset = stmt_list->as_unsigned ();
6923 ++tu_stats->nr_symtab_sharers;
6924 }
6925 else
6926 {
6927 /* Ugh, no stmt_list. Rare, but we have to handle it.
6928 We can do various things here like create one group per TU or
6929 spread them over multiple groups to split up the expansion work.
6930 To avoid worst case scenarios (too many groups or too large groups)
6931 we, umm, group them in bunches. */
6932 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6933 | (tu_stats->nr_stmt_less_type_units
6934 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6935 ++tu_stats->nr_stmt_less_type_units;
6936 }
6937
6938 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6939 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6940 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6941 &type_unit_group_for_lookup, INSERT);
6942 if (*slot == nullptr)
6943 {
6944 sect_offset line_offset_struct = (sect_offset) line_offset;
6945 std::unique_ptr<type_unit_group> grp
6946 = create_type_unit_group (cu, line_offset_struct);
6947 *slot = grp.release ();
6948 ++tu_stats->nr_symtabs;
6949 }
6950
6951 tu_group = (struct type_unit_group *) *slot;
6952 gdb_assert (tu_group != nullptr);
6953 return tu_group;
6954 }
6955 \f
6956 /* Partial symbol tables. */
6957
6958 /* Create a psymtab named NAME and assign it to PER_CU.
6959
6960 The caller must fill in the following details:
6961 dirname, textlow, texthigh. */
6962
6963 static dwarf2_psymtab *
6964 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6965 dwarf2_per_objfile *per_objfile,
6966 const char *name)
6967 {
6968 dwarf2_psymtab *pst
6969 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6970 per_objfile->objfile->per_bfd, per_cu);
6971
6972 pst->psymtabs_addrmap_supported = true;
6973
6974 /* This is the glue that links PST into GDB's symbol API. */
6975 per_cu->v.psymtab = pst;
6976
6977 return pst;
6978 }
6979
6980 /* DIE reader function for process_psymtab_comp_unit. */
6981
6982 static void
6983 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6984 const gdb_byte *info_ptr,
6985 struct die_info *comp_unit_die,
6986 enum language pretend_language)
6987 {
6988 struct dwarf2_cu *cu = reader->cu;
6989 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6990 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6991 struct objfile *objfile = per_objfile->objfile;
6992 struct gdbarch *gdbarch = objfile->arch ();
6993 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6994 CORE_ADDR baseaddr;
6995 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6996 dwarf2_psymtab *pst;
6997 enum pc_bounds_kind cu_bounds_kind;
6998
6999 gdb_assert (! per_cu->is_debug_types);
7000
7001 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7002
7003 /* Allocate a new partial symbol table structure. */
7004 static const char artificial[] = "<artificial>";
7005 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
7006 if (strcmp (fnd.get_name (), artificial) == 0)
7007 {
7008 gdb::unique_xmalloc_ptr<char> debug_filename
7009 (concat (artificial, "@",
7010 sect_offset_str (per_cu->sect_off),
7011 (char *) NULL));
7012 fnd.set_name (std::move (debug_filename));
7013 }
7014
7015 pst = create_partial_symtab (per_cu, per_objfile, fnd.get_name ());
7016
7017 /* This must be done before calling dwarf2_build_include_psymtabs. */
7018 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7019
7020 baseaddr = objfile->text_section_offset ();
7021
7022 dwarf2_find_base_address (comp_unit_die, cu);
7023
7024 /* Possibly set the default values of LOWPC and HIGHPC from
7025 `DW_AT_ranges'. */
7026 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7027 &best_highpc, cu, pst);
7028 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7029 {
7030 CORE_ADDR low
7031 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7032 - baseaddr);
7033 CORE_ADDR high
7034 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7035 - baseaddr - 1);
7036 /* Store the contiguous range if it is not empty; it can be
7037 empty for CUs with no code. */
7038 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7039 low, high, pst);
7040 }
7041
7042 /* Check if comp unit has_children.
7043 If so, read the rest of the partial symbols from this comp unit.
7044 If not, there's no more debug_info for this comp unit. */
7045 if (comp_unit_die->has_children)
7046 {
7047 struct partial_die_info *first_die;
7048 CORE_ADDR lowpc, highpc;
7049
7050 lowpc = ((CORE_ADDR) -1);
7051 highpc = ((CORE_ADDR) 0);
7052
7053 first_die = load_partial_dies (reader, info_ptr, 1);
7054
7055 scan_partial_symbols (first_die, &lowpc, &highpc,
7056 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7057
7058 /* If we didn't find a lowpc, set it to highpc to avoid
7059 complaints from `maint check'. */
7060 if (lowpc == ((CORE_ADDR) -1))
7061 lowpc = highpc;
7062
7063 /* If the compilation unit didn't have an explicit address range,
7064 then use the information extracted from its child dies. */
7065 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7066 {
7067 best_lowpc = lowpc;
7068 best_highpc = highpc;
7069 }
7070 }
7071 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7072 best_lowpc + baseaddr)
7073 - baseaddr);
7074 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7075 best_highpc + baseaddr)
7076 - baseaddr);
7077
7078 pst->end ();
7079
7080 if (!cu->per_cu->imported_symtabs_empty ())
7081 {
7082 int i;
7083 int len = cu->per_cu->imported_symtabs_size ();
7084
7085 /* Fill in 'dependencies' here; we fill in 'users' in a
7086 post-pass. */
7087 pst->number_of_dependencies = len;
7088 pst->dependencies
7089 = per_bfd->partial_symtabs->allocate_dependencies (len);
7090 for (i = 0; i < len; ++i)
7091 {
7092 pst->dependencies[i]
7093 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7094 }
7095
7096 cu->per_cu->imported_symtabs_free ();
7097 }
7098
7099 /* Get the list of files included in the current compilation unit,
7100 and build a psymtab for each of them. */
7101 dwarf2_build_include_psymtabs (cu, comp_unit_die, fnd, pst);
7102
7103 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7104 ", %d global, %d static syms",
7105 per_cu->is_debug_types ? "type" : "comp",
7106 sect_offset_str (per_cu->sect_off),
7107 paddress (gdbarch, pst->text_low (objfile)),
7108 paddress (gdbarch, pst->text_high (objfile)),
7109 (int) pst->global_psymbols.size (),
7110 (int) pst->static_psymbols.size ());
7111 }
7112
7113 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7114 Process compilation unit THIS_CU for a psymtab. */
7115
7116 static void
7117 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7118 dwarf2_per_objfile *per_objfile,
7119 bool want_partial_unit,
7120 enum language pretend_language)
7121 {
7122 /* If this compilation unit was already read in, free the
7123 cached copy in order to read it in again. This is
7124 necessary because we skipped some symbols when we first
7125 read in the compilation unit (see load_partial_dies).
7126 This problem could be avoided, but the benefit is unclear. */
7127 per_objfile->remove_cu (this_cu);
7128
7129 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7130
7131 if (reader.comp_unit_die == nullptr)
7132 return;
7133
7134 switch (reader.comp_unit_die->tag)
7135 {
7136 case DW_TAG_compile_unit:
7137 this_cu->unit_type = DW_UT_compile;
7138 break;
7139 case DW_TAG_partial_unit:
7140 this_cu->unit_type = DW_UT_partial;
7141 break;
7142 case DW_TAG_type_unit:
7143 this_cu->unit_type = DW_UT_type;
7144 break;
7145 default:
7146 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7147 dwarf_tag_name (reader.comp_unit_die->tag),
7148 sect_offset_str (reader.cu->per_cu->sect_off),
7149 objfile_name (per_objfile->objfile));
7150 }
7151
7152 if (reader.dummy_p)
7153 {
7154 /* Nothing. */
7155 }
7156 else if (this_cu->is_debug_types)
7157 build_type_psymtabs_reader (&reader, reader.info_ptr,
7158 reader.comp_unit_die);
7159 else if (want_partial_unit
7160 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7161 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7162 reader.comp_unit_die,
7163 pretend_language);
7164
7165 /* Age out any secondary CUs. */
7166 per_objfile->age_comp_units ();
7167 }
7168
7169 /* Reader function for build_type_psymtabs. */
7170
7171 static void
7172 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7173 const gdb_byte *info_ptr,
7174 struct die_info *type_unit_die)
7175 {
7176 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7177 struct dwarf2_cu *cu = reader->cu;
7178 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7179 struct signatured_type *sig_type;
7180 struct type_unit_group *tu_group;
7181 struct attribute *attr;
7182 struct partial_die_info *first_die;
7183 CORE_ADDR lowpc, highpc;
7184 dwarf2_psymtab *pst;
7185
7186 gdb_assert (per_cu->is_debug_types);
7187 sig_type = (struct signatured_type *) per_cu;
7188
7189 if (! type_unit_die->has_children)
7190 return;
7191
7192 attr = type_unit_die->attr (DW_AT_stmt_list);
7193 tu_group = get_type_unit_group (cu, attr);
7194
7195 if (tu_group->tus == nullptr)
7196 tu_group->tus = new std::vector<signatured_type *>;
7197 tu_group->tus->push_back (sig_type);
7198
7199 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7200 pst = create_partial_symtab (per_cu, per_objfile, "");
7201 pst->anonymous = true;
7202
7203 first_die = load_partial_dies (reader, info_ptr, 1);
7204
7205 lowpc = (CORE_ADDR) -1;
7206 highpc = (CORE_ADDR) 0;
7207 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7208
7209 pst->end ();
7210 }
7211
7212 /* Struct used to sort TUs by their abbreviation table offset. */
7213
7214 struct tu_abbrev_offset
7215 {
7216 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7217 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7218 {}
7219
7220 /* This is used when sorting. */
7221 bool operator< (const tu_abbrev_offset &other) const
7222 {
7223 return abbrev_offset < other.abbrev_offset;
7224 }
7225
7226 signatured_type *sig_type;
7227 sect_offset abbrev_offset;
7228 };
7229
7230 /* Efficiently read all the type units.
7231
7232 The efficiency is because we sort TUs by the abbrev table they use and
7233 only read each abbrev table once. In one program there are 200K TUs
7234 sharing 8K abbrev tables.
7235
7236 The main purpose of this function is to support building the
7237 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7238 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7239 can collapse the search space by grouping them by stmt_list.
7240 The savings can be significant, in the same program from above the 200K TUs
7241 share 8K stmt_list tables.
7242
7243 FUNC is expected to call get_type_unit_group, which will create the
7244 struct type_unit_group if necessary and add it to
7245 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7246
7247 static void
7248 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7249 {
7250 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7251 abbrev_table_up abbrev_table;
7252 sect_offset abbrev_offset;
7253
7254 /* It's up to the caller to not call us multiple times. */
7255 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7256
7257 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7258 return;
7259
7260 /* TUs typically share abbrev tables, and there can be way more TUs than
7261 abbrev tables. Sort by abbrev table to reduce the number of times we
7262 read each abbrev table in.
7263 Alternatives are to punt or to maintain a cache of abbrev tables.
7264 This is simpler and efficient enough for now.
7265
7266 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7267 symtab to use). Typically TUs with the same abbrev offset have the same
7268 stmt_list value too so in practice this should work well.
7269
7270 The basic algorithm here is:
7271
7272 sort TUs by abbrev table
7273 for each TU with same abbrev table:
7274 read abbrev table if first user
7275 read TU top level DIE
7276 [IWBN if DWO skeletons had DW_AT_stmt_list]
7277 call FUNC */
7278
7279 dwarf_read_debug_printf ("Building type unit groups ...");
7280
7281 /* Sort in a separate table to maintain the order of all_comp_units
7282 for .gdb_index: TU indices directly index all_type_units. */
7283 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7284 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7285
7286 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7287 {
7288 if (cu->is_debug_types)
7289 {
7290 auto sig_type = static_cast<signatured_type *> (cu.get ());
7291 sorted_by_abbrev.emplace_back
7292 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7293 sig_type->sect_off));
7294 }
7295 }
7296
7297 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7298
7299 abbrev_offset = (sect_offset) ~(unsigned) 0;
7300
7301 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7302 {
7303 /* Switch to the next abbrev table if necessary. */
7304 if (abbrev_table == NULL
7305 || tu.abbrev_offset != abbrev_offset)
7306 {
7307 abbrev_offset = tu.abbrev_offset;
7308 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7309 abbrev_table =
7310 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7311 ++tu_stats->nr_uniq_abbrev_tables;
7312 }
7313
7314 cutu_reader reader (tu.sig_type, per_objfile,
7315 abbrev_table.get (), nullptr, false);
7316 if (!reader.dummy_p)
7317 build_type_psymtabs_reader (&reader, reader.info_ptr,
7318 reader.comp_unit_die);
7319 }
7320 }
7321
7322 /* Print collected type unit statistics. */
7323
7324 static void
7325 print_tu_stats (dwarf2_per_objfile *per_objfile)
7326 {
7327 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7328
7329 dwarf_read_debug_printf ("Type unit statistics:");
7330 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7331 dwarf_read_debug_printf (" %d uniq abbrev tables",
7332 tu_stats->nr_uniq_abbrev_tables);
7333 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7334 tu_stats->nr_symtabs);
7335 dwarf_read_debug_printf (" %d symtab sharers",
7336 tu_stats->nr_symtab_sharers);
7337 dwarf_read_debug_printf (" %d type units without a stmt_list",
7338 tu_stats->nr_stmt_less_type_units);
7339 dwarf_read_debug_printf (" %d all_type_units reallocs",
7340 tu_stats->nr_all_type_units_reallocs);
7341 }
7342
7343 /* Traversal function for build_type_psymtabs. */
7344
7345 static int
7346 build_type_psymtab_dependencies (void **slot, void *info)
7347 {
7348 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7349 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7350 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7351 dwarf2_psymtab *pst = tu_group->v.psymtab;
7352 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7353 int i;
7354
7355 gdb_assert (len > 0);
7356 gdb_assert (tu_group->type_unit_group_p ());
7357
7358 pst->number_of_dependencies = len;
7359 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7360 for (i = 0; i < len; ++i)
7361 {
7362 struct signatured_type *iter = tu_group->tus->at (i);
7363 gdb_assert (iter->is_debug_types);
7364 pst->dependencies[i] = iter->v.psymtab;
7365 iter->type_unit_group = tu_group;
7366 }
7367
7368 delete tu_group->tus;
7369 tu_group->tus = nullptr;
7370
7371 return 1;
7372 }
7373
7374 /* Traversal function for process_skeletonless_type_unit.
7375 Read a TU in a DWO file and build partial symbols for it. */
7376
7377 static int
7378 process_skeletonless_type_unit (void **slot, void *info)
7379 {
7380 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7381 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7382
7383 /* If this TU doesn't exist in the global table, add it and read it in. */
7384
7385 if (per_objfile->per_bfd->signatured_types == NULL)
7386 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7387
7388 signatured_type find_entry (dwo_unit->signature);
7389 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7390 &find_entry, INSERT);
7391 /* If we've already seen this type there's nothing to do. What's happening
7392 is we're doing our own version of comdat-folding here. */
7393 if (*slot != NULL)
7394 return 1;
7395
7396 /* This does the job that create_all_comp_units would have done for
7397 this TU. */
7398 signatured_type *entry
7399 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7400 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7401 *slot = entry;
7402
7403 /* This does the job that build_type_psymtabs would have done. */
7404 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7405 if (!reader.dummy_p)
7406 build_type_psymtabs_reader (&reader, reader.info_ptr,
7407 reader.comp_unit_die);
7408
7409 return 1;
7410 }
7411
7412 /* Traversal function for process_skeletonless_type_units. */
7413
7414 static int
7415 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7416 {
7417 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7418
7419 if (dwo_file->tus != NULL)
7420 htab_traverse_noresize (dwo_file->tus.get (),
7421 process_skeletonless_type_unit, info);
7422
7423 return 1;
7424 }
7425
7426 /* Scan all TUs of DWO files, verifying we've processed them.
7427 This is needed in case a TU was emitted without its skeleton.
7428 Note: This can't be done until we know what all the DWO files are. */
7429
7430 static void
7431 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7432 {
7433 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7434 if (get_dwp_file (per_objfile) == NULL
7435 && per_objfile->per_bfd->dwo_files != NULL)
7436 {
7437 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7438 process_dwo_file_for_skeletonless_type_units,
7439 per_objfile);
7440 }
7441 }
7442
7443 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7444
7445 static void
7446 set_partial_user (dwarf2_per_objfile *per_objfile)
7447 {
7448 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7449 {
7450 dwarf2_psymtab *pst = per_cu->v.psymtab;
7451
7452 if (pst == NULL)
7453 continue;
7454
7455 for (int j = 0; j < pst->number_of_dependencies; ++j)
7456 {
7457 /* Set the 'user' field only if it is not already set. */
7458 if (pst->dependencies[j]->user == NULL)
7459 pst->dependencies[j]->user = pst;
7460 }
7461 }
7462 }
7463
7464 /* Build the partial symbol table by doing a quick pass through the
7465 .debug_info and .debug_abbrev sections. */
7466
7467 static void
7468 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7469 {
7470 struct objfile *objfile = per_objfile->objfile;
7471 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7472
7473 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7474 objfile_name (objfile));
7475
7476 scoped_restore restore_reading_psyms
7477 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7478
7479 per_bfd->info.read (objfile);
7480
7481 /* Any cached compilation units will be linked by the per-objfile
7482 read_in_chain. Make sure to free them when we're done. */
7483 free_cached_comp_units freer (per_objfile);
7484
7485 create_all_comp_units (per_objfile);
7486 build_type_psymtabs (per_objfile);
7487
7488 /* Create a temporary address map on a temporary obstack. We later
7489 copy this to the final obstack. */
7490 auto_obstack temp_obstack;
7491
7492 scoped_restore save_psymtabs_addrmap
7493 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7494 addrmap_create_mutable (&temp_obstack));
7495
7496 for (const auto &per_cu : per_bfd->all_comp_units)
7497 {
7498 if (per_cu->v.psymtab != NULL)
7499 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7500 continue;
7501 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7502 language_minimal);
7503 }
7504
7505 /* This has to wait until we read the CUs, we need the list of DWOs. */
7506 process_skeletonless_type_units (per_objfile);
7507
7508 /* Now that all TUs have been processed we can fill in the dependencies. */
7509 if (per_bfd->type_unit_groups != NULL)
7510 {
7511 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7512 build_type_psymtab_dependencies, per_objfile);
7513 }
7514
7515 if (dwarf_read_debug > 0)
7516 print_tu_stats (per_objfile);
7517
7518 set_partial_user (per_objfile);
7519
7520 per_bfd->partial_symtabs->psymtabs_addrmap
7521 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7522 per_bfd->partial_symtabs->obstack ());
7523 /* At this point we want to keep the address map. */
7524 save_psymtabs_addrmap.release ();
7525
7526 dwarf_read_debug_printf ("Done building psymtabs of %s",
7527 objfile_name (objfile));
7528 }
7529
7530 /* Load the partial DIEs for a secondary CU into memory.
7531 This is also used when rereading a primary CU with load_all_dies. */
7532
7533 static void
7534 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7535 dwarf2_per_objfile *per_objfile,
7536 dwarf2_cu *existing_cu)
7537 {
7538 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7539
7540 if (!reader.dummy_p)
7541 {
7542 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7543 language_minimal);
7544
7545 /* Check if comp unit has_children.
7546 If so, read the rest of the partial symbols from this comp unit.
7547 If not, there's no more debug_info for this comp unit. */
7548 if (reader.comp_unit_die->has_children)
7549 load_partial_dies (&reader, reader.info_ptr, 0);
7550
7551 reader.keep ();
7552 }
7553 }
7554
7555 static void
7556 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7557 struct dwarf2_section_info *section,
7558 struct dwarf2_section_info *abbrev_section,
7559 unsigned int is_dwz,
7560 htab_up &types_htab,
7561 rcuh_kind section_kind)
7562 {
7563 const gdb_byte *info_ptr;
7564 struct objfile *objfile = per_objfile->objfile;
7565
7566 dwarf_read_debug_printf ("Reading %s for %s",
7567 section->get_name (),
7568 section->get_file_name ());
7569
7570 section->read (objfile);
7571
7572 info_ptr = section->buffer;
7573
7574 while (info_ptr < section->buffer + section->size)
7575 {
7576 dwarf2_per_cu_data_up this_cu;
7577
7578 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7579
7580 comp_unit_head cu_header;
7581 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7582 abbrev_section, info_ptr,
7583 section_kind);
7584
7585 /* Save the compilation unit for later lookup. */
7586 if (cu_header.unit_type != DW_UT_type)
7587 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7588 else
7589 {
7590 if (types_htab == nullptr)
7591 types_htab = allocate_signatured_type_table ();
7592
7593 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7594 (cu_header.signature);
7595 signatured_type *sig_ptr = sig_type.get ();
7596 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7597 this_cu.reset (sig_type.release ());
7598
7599 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7600 gdb_assert (slot != nullptr);
7601 if (*slot != nullptr)
7602 complaint (_("debug type entry at offset %s is duplicate to"
7603 " the entry at offset %s, signature %s"),
7604 sect_offset_str (sect_off),
7605 sect_offset_str (sig_ptr->sect_off),
7606 hex_string (sig_ptr->signature));
7607 *slot = sig_ptr;
7608 }
7609 this_cu->sect_off = sect_off;
7610 this_cu->length = cu_header.length + cu_header.initial_length_size;
7611 this_cu->is_dwz = is_dwz;
7612 this_cu->section = section;
7613
7614 info_ptr = info_ptr + this_cu->length;
7615 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7616 }
7617 }
7618
7619 /* Create a list of all compilation units in OBJFILE.
7620 This is only done for -readnow and building partial symtabs. */
7621
7622 static void
7623 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7624 {
7625 htab_up types_htab;
7626
7627 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7628 &per_objfile->per_bfd->abbrev, 0,
7629 types_htab, rcuh_kind::COMPILE);
7630 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7631 read_comp_units_from_section (per_objfile, &section,
7632 &per_objfile->per_bfd->abbrev, 0,
7633 types_htab, rcuh_kind::TYPE);
7634
7635 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7636 if (dwz != NULL)
7637 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7638 types_htab, rcuh_kind::COMPILE);
7639
7640 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7641 }
7642
7643 /* Process all loaded DIEs for compilation unit CU, starting at
7644 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7645 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7646 DW_AT_ranges). See the comments of add_partial_subprogram on how
7647 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7648
7649 static void
7650 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7651 CORE_ADDR *highpc, int set_addrmap,
7652 struct dwarf2_cu *cu)
7653 {
7654 struct partial_die_info *pdi;
7655
7656 /* Now, march along the PDI's, descending into ones which have
7657 interesting children but skipping the children of the other ones,
7658 until we reach the end of the compilation unit. */
7659
7660 pdi = first_die;
7661
7662 while (pdi != NULL)
7663 {
7664 pdi->fixup (cu);
7665
7666 /* Anonymous namespaces or modules have no name but have interesting
7667 children, so we need to look at them. Ditto for anonymous
7668 enums. */
7669
7670 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7671 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7672 || pdi->tag == DW_TAG_imported_unit
7673 || pdi->tag == DW_TAG_inlined_subroutine)
7674 {
7675 switch (pdi->tag)
7676 {
7677 case DW_TAG_subprogram:
7678 case DW_TAG_inlined_subroutine:
7679 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7680 if (cu->per_cu->lang == language_cplus)
7681 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7682 set_addrmap, cu);
7683 break;
7684 case DW_TAG_constant:
7685 case DW_TAG_variable:
7686 case DW_TAG_typedef:
7687 case DW_TAG_union_type:
7688 if (!pdi->is_declaration
7689 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7690 {
7691 add_partial_symbol (pdi, cu);
7692 }
7693 break;
7694 case DW_TAG_class_type:
7695 case DW_TAG_interface_type:
7696 case DW_TAG_structure_type:
7697 if (!pdi->is_declaration)
7698 {
7699 add_partial_symbol (pdi, cu);
7700 }
7701 if ((cu->per_cu->lang == language_rust
7702 || cu->per_cu->lang == language_cplus)
7703 && pdi->has_children)
7704 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7705 set_addrmap, cu);
7706 break;
7707 case DW_TAG_enumeration_type:
7708 if (!pdi->is_declaration)
7709 add_partial_enumeration (pdi, cu);
7710 break;
7711 case DW_TAG_base_type:
7712 case DW_TAG_generic_subrange:
7713 case DW_TAG_subrange_type:
7714 /* File scope base type definitions are added to the partial
7715 symbol table. */
7716 add_partial_symbol (pdi, cu);
7717 break;
7718 case DW_TAG_namespace:
7719 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7720 break;
7721 case DW_TAG_module:
7722 if (!pdi->is_declaration)
7723 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7724 break;
7725 case DW_TAG_imported_unit:
7726 {
7727 struct dwarf2_per_cu_data *per_cu;
7728
7729 /* For now we don't handle imported units in type units. */
7730 if (cu->per_cu->is_debug_types)
7731 {
7732 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7733 " supported in type units [in module %s]"),
7734 objfile_name (cu->per_objfile->objfile));
7735 }
7736
7737 per_cu = dwarf2_find_containing_comp_unit
7738 (pdi->d.sect_off, pdi->is_dwz,
7739 cu->per_objfile->per_bfd);
7740
7741 /* Go read the partial unit, if needed. */
7742 if (per_cu->v.psymtab == NULL)
7743 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7744 cu->per_cu->lang);
7745
7746 if (pdi->die_parent == nullptr
7747 && per_cu->unit_type == DW_UT_compile
7748 && per_cu->lang == language_cplus)
7749 /* Regard import as hint. See corresponding code in
7750 process_imported_unit_die. */
7751 break;
7752
7753 cu->per_cu->imported_symtabs_push (per_cu);
7754 }
7755 break;
7756 case DW_TAG_imported_declaration:
7757 add_partial_symbol (pdi, cu);
7758 break;
7759 default:
7760 break;
7761 }
7762 }
7763
7764 /* If the die has a sibling, skip to the sibling. */
7765
7766 pdi = pdi->die_sibling;
7767 }
7768 }
7769
7770 /* Functions used to compute the fully scoped name of a partial DIE.
7771
7772 Normally, this is simple. For C++, the parent DIE's fully scoped
7773 name is concatenated with "::" and the partial DIE's name.
7774 Enumerators are an exception; they use the scope of their parent
7775 enumeration type, i.e. the name of the enumeration type is not
7776 prepended to the enumerator.
7777
7778 There are two complexities. One is DW_AT_specification; in this
7779 case "parent" means the parent of the target of the specification,
7780 instead of the direct parent of the DIE. The other is compilers
7781 which do not emit DW_TAG_namespace; in this case we try to guess
7782 the fully qualified name of structure types from their members'
7783 linkage names. This must be done using the DIE's children rather
7784 than the children of any DW_AT_specification target. We only need
7785 to do this for structures at the top level, i.e. if the target of
7786 any DW_AT_specification (if any; otherwise the DIE itself) does not
7787 have a parent. */
7788
7789 /* Compute the scope prefix associated with PDI's parent, in
7790 compilation unit CU. The result will be allocated on CU's
7791 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7792 field. NULL is returned if no prefix is necessary. */
7793 static const char *
7794 partial_die_parent_scope (struct partial_die_info *pdi,
7795 struct dwarf2_cu *cu)
7796 {
7797 const char *grandparent_scope;
7798 struct partial_die_info *parent, *real_pdi;
7799
7800 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7801 then this means the parent of the specification DIE. */
7802
7803 real_pdi = pdi;
7804 while (real_pdi->has_specification)
7805 {
7806 auto res = find_partial_die (real_pdi->spec_offset,
7807 real_pdi->spec_is_dwz, cu);
7808 real_pdi = res.pdi;
7809 cu = res.cu;
7810 }
7811
7812 parent = real_pdi->die_parent;
7813 if (parent == NULL)
7814 return NULL;
7815
7816 if (parent->scope_set)
7817 return parent->scope;
7818
7819 parent->fixup (cu);
7820
7821 grandparent_scope = partial_die_parent_scope (parent, cu);
7822
7823 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7824 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7825 Work around this problem here. */
7826 if (cu->per_cu->lang == language_cplus
7827 && parent->tag == DW_TAG_namespace
7828 && strcmp (parent->name (cu), "::") == 0
7829 && grandparent_scope == NULL)
7830 {
7831 parent->scope = NULL;
7832 parent->scope_set = 1;
7833 return NULL;
7834 }
7835
7836 /* Nested subroutines in Fortran get a prefix. */
7837 if (pdi->tag == DW_TAG_enumerator)
7838 /* Enumerators should not get the name of the enumeration as a prefix. */
7839 parent->scope = grandparent_scope;
7840 else if (parent->tag == DW_TAG_namespace
7841 || parent->tag == DW_TAG_module
7842 || parent->tag == DW_TAG_structure_type
7843 || parent->tag == DW_TAG_class_type
7844 || parent->tag == DW_TAG_interface_type
7845 || parent->tag == DW_TAG_union_type
7846 || parent->tag == DW_TAG_enumeration_type
7847 || (cu->per_cu->lang == language_fortran
7848 && parent->tag == DW_TAG_subprogram
7849 && pdi->tag == DW_TAG_subprogram))
7850 {
7851 if (grandparent_scope == NULL)
7852 parent->scope = parent->name (cu);
7853 else
7854 parent->scope = typename_concat (&cu->comp_unit_obstack,
7855 grandparent_scope,
7856 parent->name (cu), 0, cu);
7857 }
7858 else
7859 {
7860 /* FIXME drow/2004-04-01: What should we be doing with
7861 function-local names? For partial symbols, we should probably be
7862 ignoring them. */
7863 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7864 dwarf_tag_name (parent->tag),
7865 sect_offset_str (pdi->sect_off));
7866 parent->scope = grandparent_scope;
7867 }
7868
7869 parent->scope_set = 1;
7870 return parent->scope;
7871 }
7872
7873 /* Return the fully scoped name associated with PDI, from compilation unit
7874 CU. The result will be allocated with malloc. */
7875
7876 static gdb::unique_xmalloc_ptr<char>
7877 partial_die_full_name (struct partial_die_info *pdi,
7878 struct dwarf2_cu *cu)
7879 {
7880 const char *parent_scope;
7881
7882 /* If this is a template instantiation, we can not work out the
7883 template arguments from partial DIEs. So, unfortunately, we have
7884 to go through the full DIEs. At least any work we do building
7885 types here will be reused if full symbols are loaded later. */
7886 if (pdi->has_template_arguments)
7887 {
7888 pdi->fixup (cu);
7889
7890 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7891 {
7892 struct die_info *die;
7893 struct attribute attr;
7894 struct dwarf2_cu *ref_cu = cu;
7895
7896 /* DW_FORM_ref_addr is using section offset. */
7897 attr.name = (enum dwarf_attribute) 0;
7898 attr.form = DW_FORM_ref_addr;
7899 attr.u.unsnd = to_underlying (pdi->sect_off);
7900 die = follow_die_ref (NULL, &attr, &ref_cu);
7901
7902 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7903 }
7904 }
7905
7906 parent_scope = partial_die_parent_scope (pdi, cu);
7907 if (parent_scope == NULL)
7908 return NULL;
7909 else
7910 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7911 pdi->name (cu),
7912 0, cu));
7913 }
7914
7915 static void
7916 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7917 {
7918 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7919 struct objfile *objfile = per_objfile->objfile;
7920 struct gdbarch *gdbarch = objfile->arch ();
7921 CORE_ADDR addr = 0;
7922 const char *actual_name = NULL;
7923 CORE_ADDR baseaddr;
7924
7925 baseaddr = objfile->text_section_offset ();
7926
7927 gdb::unique_xmalloc_ptr<char> built_actual_name
7928 = partial_die_full_name (pdi, cu);
7929 if (built_actual_name != NULL)
7930 actual_name = built_actual_name.get ();
7931
7932 if (actual_name == NULL)
7933 actual_name = pdi->name (cu);
7934
7935 partial_symbol psymbol;
7936 memset (&psymbol, 0, sizeof (psymbol));
7937 psymbol.ginfo.set_language (cu->per_cu->lang,
7938 &objfile->objfile_obstack);
7939 psymbol.ginfo.set_section_index (-1);
7940
7941 /* The code below indicates that the psymbol should be installed by
7942 setting this. */
7943 gdb::optional<psymbol_placement> where;
7944
7945 switch (pdi->tag)
7946 {
7947 case DW_TAG_inlined_subroutine:
7948 case DW_TAG_subprogram:
7949 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7950 - baseaddr);
7951 if (pdi->is_external
7952 || cu->per_cu->lang == language_ada
7953 || (cu->per_cu->lang == language_fortran
7954 && pdi->die_parent != NULL
7955 && pdi->die_parent->tag == DW_TAG_subprogram))
7956 {
7957 /* Normally, only "external" DIEs are part of the global scope.
7958 But in Ada and Fortran, we want to be able to access nested
7959 procedures globally. So all Ada and Fortran subprograms are
7960 stored in the global scope. */
7961 where = psymbol_placement::GLOBAL;
7962 }
7963 else
7964 where = psymbol_placement::STATIC;
7965
7966 psymbol.domain = VAR_DOMAIN;
7967 psymbol.aclass = LOC_BLOCK;
7968 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7969 psymbol.ginfo.set_value_address (addr);
7970
7971 if (pdi->main_subprogram && actual_name != NULL)
7972 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
7973 break;
7974 case DW_TAG_constant:
7975 psymbol.domain = VAR_DOMAIN;
7976 psymbol.aclass = LOC_STATIC;
7977 where = (pdi->is_external
7978 ? psymbol_placement::GLOBAL
7979 : psymbol_placement::STATIC);
7980 break;
7981 case DW_TAG_variable:
7982 if (pdi->d.locdesc)
7983 addr = decode_locdesc (pdi->d.locdesc, cu);
7984
7985 if (pdi->d.locdesc
7986 && addr == 0
7987 && !per_objfile->per_bfd->has_section_at_zero)
7988 {
7989 /* A global or static variable may also have been stripped
7990 out by the linker if unused, in which case its address
7991 will be nullified; do not add such variables into partial
7992 symbol table then. */
7993 }
7994 else if (pdi->is_external)
7995 {
7996 /* Global Variable.
7997 Don't enter into the minimal symbol tables as there is
7998 a minimal symbol table entry from the ELF symbols already.
7999 Enter into partial symbol table if it has a location
8000 descriptor or a type.
8001 If the location descriptor is missing, new_symbol will create
8002 a LOC_UNRESOLVED symbol, the address of the variable will then
8003 be determined from the minimal symbol table whenever the variable
8004 is referenced.
8005 The address for the partial symbol table entry is not
8006 used by GDB, but it comes in handy for debugging partial symbol
8007 table building. */
8008
8009 if (pdi->d.locdesc || pdi->has_type)
8010 {
8011 psymbol.domain = VAR_DOMAIN;
8012 psymbol.aclass = LOC_STATIC;
8013 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8014 psymbol.ginfo.set_value_address (addr);
8015 where = psymbol_placement::GLOBAL;
8016 }
8017 }
8018 else
8019 {
8020 int has_loc = pdi->d.locdesc != NULL;
8021
8022 /* Static Variable. Skip symbols whose value we cannot know (those
8023 without location descriptors or constant values). */
8024 if (!has_loc && !pdi->has_const_value)
8025 return;
8026
8027 psymbol.domain = VAR_DOMAIN;
8028 psymbol.aclass = LOC_STATIC;
8029 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8030 if (has_loc)
8031 psymbol.ginfo.set_value_address (addr);
8032 where = psymbol_placement::STATIC;
8033 }
8034 break;
8035 case DW_TAG_array_type:
8036 case DW_TAG_typedef:
8037 case DW_TAG_base_type:
8038 case DW_TAG_subrange_type:
8039 case DW_TAG_generic_subrange:
8040 psymbol.domain = VAR_DOMAIN;
8041 psymbol.aclass = LOC_TYPEDEF;
8042 where = psymbol_placement::STATIC;
8043 break;
8044 case DW_TAG_imported_declaration:
8045 case DW_TAG_namespace:
8046 psymbol.domain = VAR_DOMAIN;
8047 psymbol.aclass = LOC_TYPEDEF;
8048 where = psymbol_placement::GLOBAL;
8049 break;
8050 case DW_TAG_module:
8051 /* With Fortran 77 there might be a "BLOCK DATA" module
8052 available without any name. If so, we skip the module as it
8053 doesn't bring any value. */
8054 if (actual_name != nullptr)
8055 {
8056 psymbol.domain = MODULE_DOMAIN;
8057 psymbol.aclass = LOC_TYPEDEF;
8058 where = psymbol_placement::GLOBAL;
8059 }
8060 break;
8061 case DW_TAG_class_type:
8062 case DW_TAG_interface_type:
8063 case DW_TAG_structure_type:
8064 case DW_TAG_union_type:
8065 case DW_TAG_enumeration_type:
8066 /* Skip external references. The DWARF standard says in the section
8067 about "Structure, Union, and Class Type Entries": "An incomplete
8068 structure, union or class type is represented by a structure,
8069 union or class entry that does not have a byte size attribute
8070 and that has a DW_AT_declaration attribute." */
8071 if (!pdi->has_byte_size && pdi->is_declaration)
8072 return;
8073
8074 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8075 static vs. global. */
8076 psymbol.domain = STRUCT_DOMAIN;
8077 psymbol.aclass = LOC_TYPEDEF;
8078 where = (cu->per_cu->lang == language_cplus
8079 ? psymbol_placement::GLOBAL
8080 : psymbol_placement::STATIC);
8081 break;
8082 case DW_TAG_enumerator:
8083 psymbol.domain = VAR_DOMAIN;
8084 psymbol.aclass = LOC_CONST;
8085 where = (cu->per_cu->lang == language_cplus
8086 ? psymbol_placement::GLOBAL
8087 : psymbol_placement::STATIC);
8088 break;
8089 default:
8090 break;
8091 }
8092
8093 if (where.has_value ())
8094 {
8095 if (built_actual_name != nullptr)
8096 actual_name = objfile->intern (actual_name);
8097 if (pdi->linkage_name == nullptr
8098 || cu->per_cu->lang == language_ada)
8099 psymbol.ginfo.set_linkage_name (actual_name);
8100 else
8101 {
8102 psymbol.ginfo.set_demangled_name (actual_name,
8103 &objfile->objfile_obstack);
8104 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8105 }
8106 cu->per_cu->v.psymtab->add_psymbol
8107 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8108 objfile);
8109 }
8110 }
8111
8112 /* Read a partial die corresponding to a namespace; also, add a symbol
8113 corresponding to that namespace to the symbol table. NAMESPACE is
8114 the name of the enclosing namespace. */
8115
8116 static void
8117 add_partial_namespace (struct partial_die_info *pdi,
8118 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8119 int set_addrmap, struct dwarf2_cu *cu)
8120 {
8121 /* Add a symbol for the namespace. */
8122
8123 add_partial_symbol (pdi, cu);
8124
8125 /* Now scan partial symbols in that namespace. */
8126
8127 if (pdi->has_children)
8128 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8129 }
8130
8131 /* Read a partial die corresponding to a Fortran module. */
8132
8133 static void
8134 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8135 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8136 {
8137 /* Add a symbol for the namespace. */
8138
8139 add_partial_symbol (pdi, cu);
8140
8141 /* Now scan partial symbols in that module. */
8142
8143 if (pdi->has_children)
8144 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8145 }
8146
8147 static int
8148 dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
8149 dwarf2_psymtab *, dwarf_tag);
8150
8151 /* Read a partial die corresponding to a subprogram or an inlined
8152 subprogram and create a partial symbol for that subprogram.
8153 When the CU language allows it, this routine also defines a partial
8154 symbol for each nested subprogram that this subprogram contains.
8155 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8156 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8157
8158 PDI may also be a lexical block, in which case we simply search
8159 recursively for subprograms defined inside that lexical block.
8160 Again, this is only performed when the CU language allows this
8161 type of definitions. */
8162
8163 static void
8164 add_partial_subprogram (struct partial_die_info *pdi,
8165 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8166 int set_addrmap, struct dwarf2_cu *cu)
8167 {
8168 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8169 {
8170 if (pdi->has_pc_info)
8171 {
8172 if (pdi->lowpc < *lowpc)
8173 *lowpc = pdi->lowpc;
8174 if (pdi->highpc > *highpc)
8175 *highpc = pdi->highpc;
8176 if (set_addrmap)
8177 {
8178 struct objfile *objfile = cu->per_objfile->objfile;
8179 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8180 struct gdbarch *gdbarch = objfile->arch ();
8181 CORE_ADDR baseaddr;
8182 CORE_ADDR this_highpc;
8183 CORE_ADDR this_lowpc;
8184
8185 baseaddr = objfile->text_section_offset ();
8186 this_lowpc
8187 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8188 pdi->lowpc + baseaddr)
8189 - baseaddr);
8190 this_highpc
8191 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8192 pdi->highpc + baseaddr)
8193 - baseaddr);
8194 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8195 this_lowpc, this_highpc - 1,
8196 cu->per_cu->v.psymtab);
8197 }
8198 }
8199
8200 if (pdi->has_range_info
8201 && dwarf2_ranges_read (pdi->ranges_offset, &pdi->lowpc, &pdi->highpc,
8202 cu,
8203 set_addrmap ? cu->per_cu->v.psymtab : nullptr,
8204 pdi->tag))
8205 {
8206 if (pdi->lowpc < *lowpc)
8207 *lowpc = pdi->lowpc;
8208 if (pdi->highpc > *highpc)
8209 *highpc = pdi->highpc;
8210 }
8211
8212 if (pdi->has_pc_info || pdi->has_range_info
8213 || (!pdi->is_external && pdi->may_be_inlined))
8214 {
8215 if (!pdi->is_declaration)
8216 /* Ignore subprogram DIEs that do not have a name, they are
8217 illegal. Do not emit a complaint at this point, we will
8218 do so when we convert this psymtab into a symtab. */
8219 if (pdi->name (cu))
8220 add_partial_symbol (pdi, cu);
8221 }
8222 }
8223
8224 if (! pdi->has_children)
8225 return;
8226
8227 if (cu->per_cu->lang == language_ada
8228 || cu->per_cu->lang == language_fortran)
8229 {
8230 pdi = pdi->die_child;
8231 while (pdi != NULL)
8232 {
8233 pdi->fixup (cu);
8234 if (pdi->tag == DW_TAG_subprogram
8235 || pdi->tag == DW_TAG_inlined_subroutine
8236 || pdi->tag == DW_TAG_lexical_block)
8237 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8238 pdi = pdi->die_sibling;
8239 }
8240 }
8241 }
8242
8243 /* Read a partial die corresponding to an enumeration type. */
8244
8245 static void
8246 add_partial_enumeration (struct partial_die_info *enum_pdi,
8247 struct dwarf2_cu *cu)
8248 {
8249 struct partial_die_info *pdi;
8250
8251 if (enum_pdi->name (cu) != NULL)
8252 add_partial_symbol (enum_pdi, cu);
8253
8254 pdi = enum_pdi->die_child;
8255 while (pdi)
8256 {
8257 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8258 complaint (_("malformed enumerator DIE ignored"));
8259 else
8260 add_partial_symbol (pdi, cu);
8261 pdi = pdi->die_sibling;
8262 }
8263 }
8264
8265 /* Return the initial uleb128 in the die at INFO_PTR. */
8266
8267 static unsigned int
8268 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8269 {
8270 unsigned int bytes_read;
8271
8272 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8273 }
8274
8275 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8276 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8277
8278 Return the corresponding abbrev, or NULL if the number is zero (indicating
8279 an empty DIE). In either case *BYTES_READ will be set to the length of
8280 the initial number. */
8281
8282 static const struct abbrev_info *
8283 peek_die_abbrev (const die_reader_specs &reader,
8284 const gdb_byte *info_ptr, unsigned int *bytes_read)
8285 {
8286 dwarf2_cu *cu = reader.cu;
8287 bfd *abfd = reader.abfd;
8288 unsigned int abbrev_number
8289 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8290
8291 if (abbrev_number == 0)
8292 return NULL;
8293
8294 const abbrev_info *abbrev
8295 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8296 if (!abbrev)
8297 {
8298 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8299 " at offset %s [in module %s]"),
8300 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8301 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8302 }
8303
8304 return abbrev;
8305 }
8306
8307 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8308 Returns a pointer to the end of a series of DIEs, terminated by an empty
8309 DIE. Any children of the skipped DIEs will also be skipped. */
8310
8311 static const gdb_byte *
8312 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8313 {
8314 while (1)
8315 {
8316 unsigned int bytes_read;
8317 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8318 &bytes_read);
8319
8320 if (abbrev == NULL)
8321 return info_ptr + bytes_read;
8322 else
8323 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8324 }
8325 }
8326
8327 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8328 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8329 abbrev corresponding to that skipped uleb128 should be passed in
8330 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8331 children. */
8332
8333 static const gdb_byte *
8334 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8335 const struct abbrev_info *abbrev)
8336 {
8337 unsigned int bytes_read;
8338 struct attribute attr;
8339 bfd *abfd = reader->abfd;
8340 struct dwarf2_cu *cu = reader->cu;
8341 const gdb_byte *buffer = reader->buffer;
8342 const gdb_byte *buffer_end = reader->buffer_end;
8343 unsigned int form, i;
8344
8345 for (i = 0; i < abbrev->num_attrs; i++)
8346 {
8347 /* The only abbrev we care about is DW_AT_sibling. */
8348 if (abbrev->attrs[i].name == DW_AT_sibling)
8349 {
8350 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8351 if (attr.form == DW_FORM_ref_addr)
8352 complaint (_("ignoring absolute DW_AT_sibling"));
8353 else
8354 {
8355 sect_offset off = attr.get_ref_die_offset ();
8356 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8357
8358 if (sibling_ptr < info_ptr)
8359 complaint (_("DW_AT_sibling points backwards"));
8360 else if (sibling_ptr > reader->buffer_end)
8361 reader->die_section->overflow_complaint ();
8362 else
8363 return sibling_ptr;
8364 }
8365 }
8366
8367 /* If it isn't DW_AT_sibling, skip this attribute. */
8368 form = abbrev->attrs[i].form;
8369 skip_attribute:
8370 switch (form)
8371 {
8372 case DW_FORM_ref_addr:
8373 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8374 and later it is offset sized. */
8375 if (cu->header.version == 2)
8376 info_ptr += cu->header.addr_size;
8377 else
8378 info_ptr += cu->header.offset_size;
8379 break;
8380 case DW_FORM_GNU_ref_alt:
8381 info_ptr += cu->header.offset_size;
8382 break;
8383 case DW_FORM_addr:
8384 info_ptr += cu->header.addr_size;
8385 break;
8386 case DW_FORM_data1:
8387 case DW_FORM_ref1:
8388 case DW_FORM_flag:
8389 case DW_FORM_strx1:
8390 info_ptr += 1;
8391 break;
8392 case DW_FORM_flag_present:
8393 case DW_FORM_implicit_const:
8394 break;
8395 case DW_FORM_data2:
8396 case DW_FORM_ref2:
8397 case DW_FORM_strx2:
8398 info_ptr += 2;
8399 break;
8400 case DW_FORM_strx3:
8401 info_ptr += 3;
8402 break;
8403 case DW_FORM_data4:
8404 case DW_FORM_ref4:
8405 case DW_FORM_strx4:
8406 info_ptr += 4;
8407 break;
8408 case DW_FORM_data8:
8409 case DW_FORM_ref8:
8410 case DW_FORM_ref_sig8:
8411 info_ptr += 8;
8412 break;
8413 case DW_FORM_data16:
8414 info_ptr += 16;
8415 break;
8416 case DW_FORM_string:
8417 read_direct_string (abfd, info_ptr, &bytes_read);
8418 info_ptr += bytes_read;
8419 break;
8420 case DW_FORM_sec_offset:
8421 case DW_FORM_strp:
8422 case DW_FORM_GNU_strp_alt:
8423 info_ptr += cu->header.offset_size;
8424 break;
8425 case DW_FORM_exprloc:
8426 case DW_FORM_block:
8427 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8428 info_ptr += bytes_read;
8429 break;
8430 case DW_FORM_block1:
8431 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8432 break;
8433 case DW_FORM_block2:
8434 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8435 break;
8436 case DW_FORM_block4:
8437 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8438 break;
8439 case DW_FORM_addrx:
8440 case DW_FORM_strx:
8441 case DW_FORM_sdata:
8442 case DW_FORM_udata:
8443 case DW_FORM_ref_udata:
8444 case DW_FORM_GNU_addr_index:
8445 case DW_FORM_GNU_str_index:
8446 case DW_FORM_rnglistx:
8447 case DW_FORM_loclistx:
8448 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8449 break;
8450 case DW_FORM_indirect:
8451 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8452 info_ptr += bytes_read;
8453 /* We need to continue parsing from here, so just go back to
8454 the top. */
8455 goto skip_attribute;
8456
8457 default:
8458 error (_("Dwarf Error: Cannot handle %s "
8459 "in DWARF reader [in module %s]"),
8460 dwarf_form_name (form),
8461 bfd_get_filename (abfd));
8462 }
8463 }
8464
8465 if (abbrev->has_children)
8466 return skip_children (reader, info_ptr);
8467 else
8468 return info_ptr;
8469 }
8470
8471 /* Locate ORIG_PDI's sibling.
8472 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8473
8474 static const gdb_byte *
8475 locate_pdi_sibling (const struct die_reader_specs *reader,
8476 struct partial_die_info *orig_pdi,
8477 const gdb_byte *info_ptr)
8478 {
8479 /* Do we know the sibling already? */
8480
8481 if (orig_pdi->sibling)
8482 return orig_pdi->sibling;
8483
8484 /* Are there any children to deal with? */
8485
8486 if (!orig_pdi->has_children)
8487 return info_ptr;
8488
8489 /* Skip the children the long way. */
8490
8491 return skip_children (reader, info_ptr);
8492 }
8493
8494 /* Expand this partial symbol table into a full symbol table. SELF is
8495 not NULL. */
8496
8497 void
8498 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8499 {
8500 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8501
8502 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8503
8504 /* If this psymtab is constructed from a debug-only objfile, the
8505 has_section_at_zero flag will not necessarily be correct. We
8506 can get the correct value for this flag by looking at the data
8507 associated with the (presumably stripped) associated objfile. */
8508 if (objfile->separate_debug_objfile_backlink)
8509 {
8510 dwarf2_per_objfile *per_objfile_backlink
8511 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8512
8513 per_objfile->per_bfd->has_section_at_zero
8514 = per_objfile_backlink->per_bfd->has_section_at_zero;
8515 }
8516
8517 expand_psymtab (objfile);
8518
8519 process_cu_includes (per_objfile);
8520 }
8521 \f
8522 /* Reading in full CUs. */
8523
8524 /* Add PER_CU to the queue. */
8525
8526 static void
8527 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8528 dwarf2_per_objfile *per_objfile,
8529 enum language pretend_language)
8530 {
8531 per_cu->queued = 1;
8532
8533 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8534 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8535 }
8536
8537 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8538
8539 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8540 dependency.
8541
8542 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8543 DIEs, false otherwise.
8544
8545 Explanation: there is an invariant that if a CU is queued for expansion
8546 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8547 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8548 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8549 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8550 invariant is respected.
8551
8552 The caller is therefore not required to load the CU's DIEs (we return false)
8553 if:
8554
8555 - the CU is already expanded, and therefore does not get enqueued
8556 - the CU gets enqueued for expansion, but its DIEs are already loaded
8557
8558 Note that the caller should not use this function's return value as an
8559 indicator of whether the CU's DIEs are loaded right now, it should check
8560 that by calling `dwarf2_per_objfile::get_cu` instead. */
8561
8562 static int
8563 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8564 dwarf2_per_cu_data *per_cu,
8565 dwarf2_per_objfile *per_objfile,
8566 enum language pretend_language)
8567 {
8568 /* We may arrive here during partial symbol reading, if we need full
8569 DIEs to process an unusual case (e.g. template arguments). Do
8570 not queue PER_CU, just tell our caller to load its DIEs. */
8571 if (per_cu->per_bfd->reading_partial_symbols)
8572 {
8573 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8574
8575 if (cu == NULL || cu->dies == NULL)
8576 return 1;
8577 return 0;
8578 }
8579
8580 /* Mark the dependence relation so that we don't flush PER_CU
8581 too early. */
8582 if (dependent_cu != NULL)
8583 dependent_cu->add_dependence (per_cu);
8584
8585 /* If it's already on the queue, we have nothing to do. */
8586 if (per_cu->queued)
8587 {
8588 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8589 loaded. */
8590 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8591
8592 /* If the CU is queued for expansion, it should not already be
8593 expanded. */
8594 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8595
8596 /* The DIEs are already loaded, the caller doesn't need to do it. */
8597 return 0;
8598 }
8599
8600 bool queued = false;
8601 if (!per_objfile->symtab_set_p (per_cu))
8602 {
8603 /* Add it to the queue. */
8604 queue_comp_unit (per_cu, per_objfile, pretend_language);
8605 queued = true;
8606 }
8607
8608 /* If the compilation unit is already loaded, just mark it as
8609 used. */
8610 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8611 if (cu != nullptr)
8612 cu->last_used = 0;
8613
8614 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8615 and the DIEs are not already loaded. */
8616 return queued && cu == nullptr;
8617 }
8618
8619 /* Process the queue. */
8620
8621 static void
8622 process_queue (dwarf2_per_objfile *per_objfile)
8623 {
8624 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8625 objfile_name (per_objfile->objfile));
8626
8627 /* The queue starts out with one item, but following a DIE reference
8628 may load a new CU, adding it to the end of the queue. */
8629 while (!per_objfile->per_bfd->queue->empty ())
8630 {
8631 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8632 dwarf2_per_cu_data *per_cu = item.per_cu;
8633
8634 if (!per_objfile->symtab_set_p (per_cu))
8635 {
8636 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8637
8638 /* Skip dummy CUs. */
8639 if (cu != nullptr)
8640 {
8641 unsigned int debug_print_threshold;
8642 char buf[100];
8643
8644 if (per_cu->is_debug_types)
8645 {
8646 struct signatured_type *sig_type =
8647 (struct signatured_type *) per_cu;
8648
8649 sprintf (buf, "TU %s at offset %s",
8650 hex_string (sig_type->signature),
8651 sect_offset_str (per_cu->sect_off));
8652 /* There can be 100s of TUs.
8653 Only print them in verbose mode. */
8654 debug_print_threshold = 2;
8655 }
8656 else
8657 {
8658 sprintf (buf, "CU at offset %s",
8659 sect_offset_str (per_cu->sect_off));
8660 debug_print_threshold = 1;
8661 }
8662
8663 if (dwarf_read_debug >= debug_print_threshold)
8664 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8665
8666 if (per_cu->is_debug_types)
8667 process_full_type_unit (cu, item.pretend_language);
8668 else
8669 process_full_comp_unit (cu, item.pretend_language);
8670
8671 if (dwarf_read_debug >= debug_print_threshold)
8672 dwarf_read_debug_printf ("Done expanding %s", buf);
8673 }
8674 }
8675
8676 per_cu->queued = 0;
8677 per_objfile->per_bfd->queue->pop ();
8678 }
8679
8680 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8681 objfile_name (per_objfile->objfile));
8682 }
8683
8684 /* Read in full symbols for PST, and anything it depends on. */
8685
8686 void
8687 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8688 {
8689 gdb_assert (!readin_p (objfile));
8690
8691 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8692 free_cached_comp_units freer (per_objfile);
8693 expand_dependencies (objfile);
8694
8695 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8696 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8697 }
8698
8699 /* See psympriv.h. */
8700
8701 bool
8702 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8703 {
8704 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8705 return per_objfile->symtab_set_p (per_cu_data);
8706 }
8707
8708 /* See psympriv.h. */
8709
8710 compunit_symtab *
8711 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8712 {
8713 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8714 return per_objfile->get_symtab (per_cu_data);
8715 }
8716
8717 /* Trivial hash function for die_info: the hash value of a DIE
8718 is its offset in .debug_info for this objfile. */
8719
8720 static hashval_t
8721 die_hash (const void *item)
8722 {
8723 const struct die_info *die = (const struct die_info *) item;
8724
8725 return to_underlying (die->sect_off);
8726 }
8727
8728 /* Trivial comparison function for die_info structures: two DIEs
8729 are equal if they have the same offset. */
8730
8731 static int
8732 die_eq (const void *item_lhs, const void *item_rhs)
8733 {
8734 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8735 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8736
8737 return die_lhs->sect_off == die_rhs->sect_off;
8738 }
8739
8740 /* Load the DIEs associated with PER_CU into memory.
8741
8742 In some cases, the caller, while reading partial symbols, will need to load
8743 the full symbols for the CU for some reason. It will already have a
8744 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8745 rather than creating a new one. */
8746
8747 static void
8748 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8749 dwarf2_per_objfile *per_objfile,
8750 dwarf2_cu *existing_cu,
8751 bool skip_partial,
8752 enum language pretend_language)
8753 {
8754 gdb_assert (! this_cu->is_debug_types);
8755
8756 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8757 if (reader.dummy_p)
8758 return;
8759
8760 struct dwarf2_cu *cu = reader.cu;
8761 const gdb_byte *info_ptr = reader.info_ptr;
8762
8763 gdb_assert (cu->die_hash == NULL);
8764 cu->die_hash =
8765 htab_create_alloc_ex (cu->header.length / 12,
8766 die_hash,
8767 die_eq,
8768 NULL,
8769 &cu->comp_unit_obstack,
8770 hashtab_obstack_allocate,
8771 dummy_obstack_deallocate);
8772
8773 if (reader.comp_unit_die->has_children)
8774 reader.comp_unit_die->child
8775 = read_die_and_siblings (&reader, reader.info_ptr,
8776 &info_ptr, reader.comp_unit_die);
8777 cu->dies = reader.comp_unit_die;
8778 /* comp_unit_die is not stored in die_hash, no need. */
8779
8780 /* We try not to read any attributes in this function, because not
8781 all CUs needed for references have been loaded yet, and symbol
8782 table processing isn't initialized. But we have to set the CU language,
8783 or we won't be able to build types correctly.
8784 Similarly, if we do not read the producer, we can not apply
8785 producer-specific interpretation. */
8786 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8787
8788 reader.keep ();
8789 }
8790
8791 /* Add a DIE to the delayed physname list. */
8792
8793 static void
8794 add_to_method_list (struct type *type, int fnfield_index, int index,
8795 const char *name, struct die_info *die,
8796 struct dwarf2_cu *cu)
8797 {
8798 struct delayed_method_info mi;
8799 mi.type = type;
8800 mi.fnfield_index = fnfield_index;
8801 mi.index = index;
8802 mi.name = name;
8803 mi.die = die;
8804 cu->method_list.push_back (mi);
8805 }
8806
8807 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8808 "const" / "volatile". If so, decrements LEN by the length of the
8809 modifier and return true. Otherwise return false. */
8810
8811 template<size_t N>
8812 static bool
8813 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8814 {
8815 size_t mod_len = sizeof (mod) - 1;
8816 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8817 {
8818 len -= mod_len;
8819 return true;
8820 }
8821 return false;
8822 }
8823
8824 /* Compute the physnames of any methods on the CU's method list.
8825
8826 The computation of method physnames is delayed in order to avoid the
8827 (bad) condition that one of the method's formal parameters is of an as yet
8828 incomplete type. */
8829
8830 static void
8831 compute_delayed_physnames (struct dwarf2_cu *cu)
8832 {
8833 /* Only C++ delays computing physnames. */
8834 if (cu->method_list.empty ())
8835 return;
8836 gdb_assert (cu->per_cu->lang == language_cplus);
8837
8838 for (const delayed_method_info &mi : cu->method_list)
8839 {
8840 const char *physname;
8841 struct fn_fieldlist *fn_flp
8842 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8843 physname = dwarf2_physname (mi.name, mi.die, cu);
8844 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8845 = physname ? physname : "";
8846
8847 /* Since there's no tag to indicate whether a method is a
8848 const/volatile overload, extract that information out of the
8849 demangled name. */
8850 if (physname != NULL)
8851 {
8852 size_t len = strlen (physname);
8853
8854 while (1)
8855 {
8856 if (physname[len] == ')') /* shortcut */
8857 break;
8858 else if (check_modifier (physname, len, " const"))
8859 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8860 else if (check_modifier (physname, len, " volatile"))
8861 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8862 else
8863 break;
8864 }
8865 }
8866 }
8867
8868 /* The list is no longer needed. */
8869 cu->method_list.clear ();
8870 }
8871
8872 /* Go objects should be embedded in a DW_TAG_module DIE,
8873 and it's not clear if/how imported objects will appear.
8874 To keep Go support simple until that's worked out,
8875 go back through what we've read and create something usable.
8876 We could do this while processing each DIE, and feels kinda cleaner,
8877 but that way is more invasive.
8878 This is to, for example, allow the user to type "p var" or "b main"
8879 without having to specify the package name, and allow lookups
8880 of module.object to work in contexts that use the expression
8881 parser. */
8882
8883 static void
8884 fixup_go_packaging (struct dwarf2_cu *cu)
8885 {
8886 gdb::unique_xmalloc_ptr<char> package_name;
8887 struct pending *list;
8888 int i;
8889
8890 for (list = *cu->get_builder ()->get_global_symbols ();
8891 list != NULL;
8892 list = list->next)
8893 {
8894 for (i = 0; i < list->nsyms; ++i)
8895 {
8896 struct symbol *sym = list->symbol[i];
8897
8898 if (sym->language () == language_go
8899 && sym->aclass () == LOC_BLOCK)
8900 {
8901 gdb::unique_xmalloc_ptr<char> this_package_name
8902 (go_symbol_package_name (sym));
8903
8904 if (this_package_name == NULL)
8905 continue;
8906 if (package_name == NULL)
8907 package_name = std::move (this_package_name);
8908 else
8909 {
8910 struct objfile *objfile = cu->per_objfile->objfile;
8911 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8912 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8913 (symbol_symtab (sym) != NULL
8914 ? symtab_to_filename_for_display
8915 (symbol_symtab (sym))
8916 : objfile_name (objfile)),
8917 this_package_name.get (), package_name.get ());
8918 }
8919 }
8920 }
8921 }
8922
8923 if (package_name != NULL)
8924 {
8925 struct objfile *objfile = cu->per_objfile->objfile;
8926 const char *saved_package_name = objfile->intern (package_name.get ());
8927 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8928 saved_package_name);
8929 struct symbol *sym;
8930
8931 sym = new (&objfile->objfile_obstack) symbol;
8932 sym->set_language (language_go, &objfile->objfile_obstack);
8933 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8934 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8935 e.g., "main" finds the "main" module and not C's main(). */
8936 sym->set_domain (STRUCT_DOMAIN);
8937 sym->set_aclass_index (LOC_TYPEDEF);
8938 sym->set_type (type);
8939
8940 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8941 }
8942 }
8943
8944 /* Allocate a fully-qualified name consisting of the two parts on the
8945 obstack. */
8946
8947 static const char *
8948 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8949 {
8950 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8951 }
8952
8953 /* A helper that allocates a variant part to attach to a Rust enum
8954 type. OBSTACK is where the results should be allocated. TYPE is
8955 the type we're processing. DISCRIMINANT_INDEX is the index of the
8956 discriminant. It must be the index of one of the fields of TYPE,
8957 or -1 to mean there is no discriminant (univariant enum).
8958 DEFAULT_INDEX is the index of the default field; or -1 if there is
8959 no default. RANGES is indexed by "effective" field number (the
8960 field index, but omitting the discriminant and default fields) and
8961 must hold the discriminant values used by the variants. Note that
8962 RANGES must have a lifetime at least as long as OBSTACK -- either
8963 already allocated on it, or static. */
8964
8965 static void
8966 alloc_rust_variant (struct obstack *obstack, struct type *type,
8967 int discriminant_index, int default_index,
8968 gdb::array_view<discriminant_range> ranges)
8969 {
8970 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8971 gdb_assert (discriminant_index == -1
8972 || (discriminant_index >= 0
8973 && discriminant_index < type->num_fields ()));
8974 gdb_assert (default_index == -1
8975 || (default_index >= 0 && default_index < type->num_fields ()));
8976
8977 /* We have one variant for each non-discriminant field. */
8978 int n_variants = type->num_fields ();
8979 if (discriminant_index != -1)
8980 --n_variants;
8981
8982 variant *variants = new (obstack) variant[n_variants];
8983 int var_idx = 0;
8984 int range_idx = 0;
8985 for (int i = 0; i < type->num_fields (); ++i)
8986 {
8987 if (i == discriminant_index)
8988 continue;
8989
8990 variants[var_idx].first_field = i;
8991 variants[var_idx].last_field = i + 1;
8992
8993 /* The default field does not need a range, but other fields do.
8994 We skipped the discriminant above. */
8995 if (i != default_index)
8996 {
8997 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8998 ++range_idx;
8999 }
9000
9001 ++var_idx;
9002 }
9003
9004 gdb_assert (range_idx == ranges.size ());
9005 gdb_assert (var_idx == n_variants);
9006
9007 variant_part *part = new (obstack) variant_part;
9008 part->discriminant_index = discriminant_index;
9009 /* If there is no discriminant, then whether it is signed is of no
9010 consequence. */
9011 part->is_unsigned
9012 = (discriminant_index == -1
9013 ? false
9014 : type->field (discriminant_index).type ()->is_unsigned ());
9015 part->variants = gdb::array_view<variant> (variants, n_variants);
9016
9017 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9018 gdb::array_view<variant_part> *prop_value
9019 = new (storage) gdb::array_view<variant_part> (part, 1);
9020
9021 struct dynamic_prop prop;
9022 prop.set_variant_parts (prop_value);
9023
9024 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9025 }
9026
9027 /* Some versions of rustc emitted enums in an unusual way.
9028
9029 Ordinary enums were emitted as unions. The first element of each
9030 structure in the union was named "RUST$ENUM$DISR". This element
9031 held the discriminant.
9032
9033 These versions of Rust also implemented the "non-zero"
9034 optimization. When the enum had two values, and one is empty and
9035 the other holds a pointer that cannot be zero, the pointer is used
9036 as the discriminant, with a zero value meaning the empty variant.
9037 Here, the union's first member is of the form
9038 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9039 where the fieldnos are the indices of the fields that should be
9040 traversed in order to find the field (which may be several fields deep)
9041 and the variantname is the name of the variant of the case when the
9042 field is zero.
9043
9044 This function recognizes whether TYPE is of one of these forms,
9045 and, if so, smashes it to be a variant type. */
9046
9047 static void
9048 quirk_rust_enum (struct type *type, struct objfile *objfile)
9049 {
9050 gdb_assert (type->code () == TYPE_CODE_UNION);
9051
9052 /* We don't need to deal with empty enums. */
9053 if (type->num_fields () == 0)
9054 return;
9055
9056 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9057 if (type->num_fields () == 1
9058 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
9059 {
9060 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
9061
9062 /* Decode the field name to find the offset of the
9063 discriminant. */
9064 ULONGEST bit_offset = 0;
9065 struct type *field_type = type->field (0).type ();
9066 while (name[0] >= '0' && name[0] <= '9')
9067 {
9068 char *tail;
9069 unsigned long index = strtoul (name, &tail, 10);
9070 name = tail;
9071 if (*name != '$'
9072 || index >= field_type->num_fields ()
9073 || (field_type->field (index).loc_kind ()
9074 != FIELD_LOC_KIND_BITPOS))
9075 {
9076 complaint (_("Could not parse Rust enum encoding string \"%s\""
9077 "[in module %s]"),
9078 type->field (0).name (),
9079 objfile_name (objfile));
9080 return;
9081 }
9082 ++name;
9083
9084 bit_offset += field_type->field (index).loc_bitpos ();
9085 field_type = field_type->field (index).type ();
9086 }
9087
9088 /* Smash this type to be a structure type. We have to do this
9089 because the type has already been recorded. */
9090 type->set_code (TYPE_CODE_STRUCT);
9091 type->set_num_fields (3);
9092 /* Save the field we care about. */
9093 struct field saved_field = type->field (0);
9094 type->set_fields
9095 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9096
9097 /* Put the discriminant at index 0. */
9098 type->field (0).set_type (field_type);
9099 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9100 type->field (0).set_name ("<<discriminant>>");
9101 type->field (0).set_loc_bitpos (bit_offset);
9102
9103 /* The order of fields doesn't really matter, so put the real
9104 field at index 1 and the data-less field at index 2. */
9105 type->field (1) = saved_field;
9106 type->field (1).set_name
9107 (rust_last_path_segment (type->field (1).type ()->name ()));
9108 type->field (1).type ()->set_name
9109 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9110 type->field (1).name ()));
9111
9112 const char *dataless_name
9113 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9114 name);
9115 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9116 dataless_name);
9117 type->field (2).set_type (dataless_type);
9118 /* NAME points into the original discriminant name, which
9119 already has the correct lifetime. */
9120 type->field (2).set_name (name);
9121 type->field (2).set_loc_bitpos (0);
9122
9123 /* Indicate that this is a variant type. */
9124 static discriminant_range ranges[1] = { { 0, 0 } };
9125 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9126 }
9127 /* A union with a single anonymous field is probably an old-style
9128 univariant enum. */
9129 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
9130 {
9131 /* Smash this type to be a structure type. We have to do this
9132 because the type has already been recorded. */
9133 type->set_code (TYPE_CODE_STRUCT);
9134
9135 struct type *field_type = type->field (0).type ();
9136 const char *variant_name
9137 = rust_last_path_segment (field_type->name ());
9138 type->field (0).set_name (variant_name);
9139 field_type->set_name
9140 (rust_fully_qualify (&objfile->objfile_obstack,
9141 type->name (), variant_name));
9142
9143 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9144 }
9145 else
9146 {
9147 struct type *disr_type = nullptr;
9148 for (int i = 0; i < type->num_fields (); ++i)
9149 {
9150 disr_type = type->field (i).type ();
9151
9152 if (disr_type->code () != TYPE_CODE_STRUCT)
9153 {
9154 /* All fields of a true enum will be structs. */
9155 return;
9156 }
9157 else if (disr_type->num_fields () == 0)
9158 {
9159 /* Could be data-less variant, so keep going. */
9160 disr_type = nullptr;
9161 }
9162 else if (strcmp (disr_type->field (0).name (),
9163 "RUST$ENUM$DISR") != 0)
9164 {
9165 /* Not a Rust enum. */
9166 return;
9167 }
9168 else
9169 {
9170 /* Found one. */
9171 break;
9172 }
9173 }
9174
9175 /* If we got here without a discriminant, then it's probably
9176 just a union. */
9177 if (disr_type == nullptr)
9178 return;
9179
9180 /* Smash this type to be a structure type. We have to do this
9181 because the type has already been recorded. */
9182 type->set_code (TYPE_CODE_STRUCT);
9183
9184 /* Make space for the discriminant field. */
9185 struct field *disr_field = &disr_type->field (0);
9186 field *new_fields
9187 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9188 * sizeof (struct field)));
9189 memcpy (new_fields + 1, type->fields (),
9190 type->num_fields () * sizeof (struct field));
9191 type->set_fields (new_fields);
9192 type->set_num_fields (type->num_fields () + 1);
9193
9194 /* Install the discriminant at index 0 in the union. */
9195 type->field (0) = *disr_field;
9196 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9197 type->field (0).set_name ("<<discriminant>>");
9198
9199 /* We need a way to find the correct discriminant given a
9200 variant name. For convenience we build a map here. */
9201 struct type *enum_type = disr_field->type ();
9202 std::unordered_map<std::string, ULONGEST> discriminant_map;
9203 for (int i = 0; i < enum_type->num_fields (); ++i)
9204 {
9205 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
9206 {
9207 const char *name
9208 = rust_last_path_segment (enum_type->field (i).name ());
9209 discriminant_map[name] = enum_type->field (i).loc_enumval ();
9210 }
9211 }
9212
9213 int n_fields = type->num_fields ();
9214 /* We don't need a range entry for the discriminant, but we do
9215 need one for every other field, as there is no default
9216 variant. */
9217 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9218 discriminant_range,
9219 n_fields - 1);
9220 /* Skip the discriminant here. */
9221 for (int i = 1; i < n_fields; ++i)
9222 {
9223 /* Find the final word in the name of this variant's type.
9224 That name can be used to look up the correct
9225 discriminant. */
9226 const char *variant_name
9227 = rust_last_path_segment (type->field (i).type ()->name ());
9228
9229 auto iter = discriminant_map.find (variant_name);
9230 if (iter != discriminant_map.end ())
9231 {
9232 ranges[i - 1].low = iter->second;
9233 ranges[i - 1].high = iter->second;
9234 }
9235
9236 /* In Rust, each element should have the size of the
9237 enclosing enum. */
9238 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9239
9240 /* Remove the discriminant field, if it exists. */
9241 struct type *sub_type = type->field (i).type ();
9242 if (sub_type->num_fields () > 0)
9243 {
9244 sub_type->set_num_fields (sub_type->num_fields () - 1);
9245 sub_type->set_fields (sub_type->fields () + 1);
9246 }
9247 type->field (i).set_name (variant_name);
9248 sub_type->set_name
9249 (rust_fully_qualify (&objfile->objfile_obstack,
9250 type->name (), variant_name));
9251 }
9252
9253 /* Indicate that this is a variant type. */
9254 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9255 gdb::array_view<discriminant_range> (ranges,
9256 n_fields - 1));
9257 }
9258 }
9259
9260 /* Rewrite some Rust unions to be structures with variants parts. */
9261
9262 static void
9263 rust_union_quirks (struct dwarf2_cu *cu)
9264 {
9265 gdb_assert (cu->per_cu->lang == language_rust);
9266 for (type *type_ : cu->rust_unions)
9267 quirk_rust_enum (type_, cu->per_objfile->objfile);
9268 /* We don't need this any more. */
9269 cu->rust_unions.clear ();
9270 }
9271
9272 /* See read.h. */
9273
9274 type_unit_group_unshareable *
9275 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9276 {
9277 auto iter = this->m_type_units.find (tu_group);
9278 if (iter != this->m_type_units.end ())
9279 return iter->second.get ();
9280
9281 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9282 type_unit_group_unshareable *result = uniq.get ();
9283 this->m_type_units[tu_group] = std::move (uniq);
9284 return result;
9285 }
9286
9287 struct type *
9288 dwarf2_per_objfile::get_type_for_signatured_type
9289 (signatured_type *sig_type) const
9290 {
9291 auto iter = this->m_type_map.find (sig_type);
9292 if (iter == this->m_type_map.end ())
9293 return nullptr;
9294
9295 return iter->second;
9296 }
9297
9298 void dwarf2_per_objfile::set_type_for_signatured_type
9299 (signatured_type *sig_type, struct type *type)
9300 {
9301 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9302
9303 this->m_type_map[sig_type] = type;
9304 }
9305
9306 /* A helper function for computing the list of all symbol tables
9307 included by PER_CU. */
9308
9309 static void
9310 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9311 htab_t all_children, htab_t all_type_symtabs,
9312 dwarf2_per_cu_data *per_cu,
9313 dwarf2_per_objfile *per_objfile,
9314 struct compunit_symtab *immediate_parent)
9315 {
9316 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9317 if (*slot != NULL)
9318 {
9319 /* This inclusion and its children have been processed. */
9320 return;
9321 }
9322
9323 *slot = per_cu;
9324
9325 /* Only add a CU if it has a symbol table. */
9326 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9327 if (cust != NULL)
9328 {
9329 /* If this is a type unit only add its symbol table if we haven't
9330 seen it yet (type unit per_cu's can share symtabs). */
9331 if (per_cu->is_debug_types)
9332 {
9333 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9334 if (*slot == NULL)
9335 {
9336 *slot = cust;
9337 result->push_back (cust);
9338 if (cust->user == NULL)
9339 cust->user = immediate_parent;
9340 }
9341 }
9342 else
9343 {
9344 result->push_back (cust);
9345 if (cust->user == NULL)
9346 cust->user = immediate_parent;
9347 }
9348 }
9349
9350 if (!per_cu->imported_symtabs_empty ())
9351 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9352 {
9353 recursively_compute_inclusions (result, all_children,
9354 all_type_symtabs, ptr, per_objfile,
9355 cust);
9356 }
9357 }
9358
9359 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9360 PER_CU. */
9361
9362 static void
9363 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9364 dwarf2_per_objfile *per_objfile)
9365 {
9366 gdb_assert (! per_cu->is_debug_types);
9367
9368 if (!per_cu->imported_symtabs_empty ())
9369 {
9370 int len;
9371 std::vector<compunit_symtab *> result_symtabs;
9372 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9373
9374 /* If we don't have a symtab, we can just skip this case. */
9375 if (cust == NULL)
9376 return;
9377
9378 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9379 htab_eq_pointer,
9380 NULL, xcalloc, xfree));
9381 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9382 htab_eq_pointer,
9383 NULL, xcalloc, xfree));
9384
9385 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9386 {
9387 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9388 all_type_symtabs.get (), ptr,
9389 per_objfile, cust);
9390 }
9391
9392 /* Now we have a transitive closure of all the included symtabs. */
9393 len = result_symtabs.size ();
9394 cust->includes
9395 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9396 struct compunit_symtab *, len + 1);
9397 memcpy (cust->includes, result_symtabs.data (),
9398 len * sizeof (compunit_symtab *));
9399 cust->includes[len] = NULL;
9400 }
9401 }
9402
9403 /* Compute the 'includes' field for the symtabs of all the CUs we just
9404 read. */
9405
9406 static void
9407 process_cu_includes (dwarf2_per_objfile *per_objfile)
9408 {
9409 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9410 {
9411 if (! iter->is_debug_types)
9412 compute_compunit_symtab_includes (iter, per_objfile);
9413 }
9414
9415 per_objfile->per_bfd->just_read_cus.clear ();
9416 }
9417
9418 /* Generate full symbol information for CU, whose DIEs have
9419 already been loaded into memory. */
9420
9421 static void
9422 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9423 {
9424 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9425 struct objfile *objfile = per_objfile->objfile;
9426 struct gdbarch *gdbarch = objfile->arch ();
9427 CORE_ADDR lowpc, highpc;
9428 struct compunit_symtab *cust;
9429 CORE_ADDR baseaddr;
9430 struct block *static_block;
9431 CORE_ADDR addr;
9432
9433 baseaddr = objfile->text_section_offset ();
9434
9435 /* Clear the list here in case something was left over. */
9436 cu->method_list.clear ();
9437
9438 dwarf2_find_base_address (cu->dies, cu);
9439
9440 /* Before we start reading the top-level DIE, ensure it has a valid tag
9441 type. */
9442 switch (cu->dies->tag)
9443 {
9444 case DW_TAG_compile_unit:
9445 case DW_TAG_partial_unit:
9446 case DW_TAG_type_unit:
9447 break;
9448 default:
9449 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9450 dwarf_tag_name (cu->dies->tag),
9451 sect_offset_str (cu->per_cu->sect_off),
9452 objfile_name (per_objfile->objfile));
9453 }
9454
9455 /* Do line number decoding in read_file_scope () */
9456 process_die (cu->dies, cu);
9457
9458 /* For now fudge the Go package. */
9459 if (cu->per_cu->lang == language_go)
9460 fixup_go_packaging (cu);
9461
9462 /* Now that we have processed all the DIEs in the CU, all the types
9463 should be complete, and it should now be safe to compute all of the
9464 physnames. */
9465 compute_delayed_physnames (cu);
9466
9467 if (cu->per_cu->lang == language_rust)
9468 rust_union_quirks (cu);
9469
9470 /* Some compilers don't define a DW_AT_high_pc attribute for the
9471 compilation unit. If the DW_AT_high_pc is missing, synthesize
9472 it, by scanning the DIE's below the compilation unit. */
9473 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9474
9475 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9476 static_block
9477 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
9478
9479 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9480 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9481 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9482 addrmap to help ensure it has an accurate map of pc values belonging to
9483 this comp unit. */
9484 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9485
9486 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
9487 (static_block, SECT_OFF_TEXT (objfile), 0);
9488
9489 if (cust != NULL)
9490 {
9491 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9492
9493 /* Set symtab language to language from DW_AT_language. If the
9494 compilation is from a C file generated by language preprocessors, do
9495 not set the language if it was already deduced by start_subfile. */
9496 if (!(cu->per_cu->lang == language_c
9497 && cust->primary_filetab ()->language () != language_unknown))
9498 cust->primary_filetab ()->set_language (cu->per_cu->lang);
9499
9500 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9501 produce DW_AT_location with location lists but it can be possibly
9502 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9503 there were bugs in prologue debug info, fixed later in GCC-4.5
9504 by "unwind info for epilogues" patch (which is not directly related).
9505
9506 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9507 needed, it would be wrong due to missing DW_AT_producer there.
9508
9509 Still one can confuse GDB by using non-standard GCC compilation
9510 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9511 */
9512 if (cu->has_loclist && gcc_4_minor >= 5)
9513 cust->set_locations_valid (true);
9514
9515 if (gcc_4_minor >= 5)
9516 cust->set_epilogue_unwind_valid (true);
9517
9518 cust->set_call_site_htab (cu->call_site_htab);
9519 }
9520
9521 per_objfile->set_symtab (cu->per_cu, cust);
9522
9523 /* Push it for inclusion processing later. */
9524 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9525
9526 /* Not needed any more. */
9527 cu->reset_builder ();
9528 }
9529
9530 /* Generate full symbol information for type unit CU, whose DIEs have
9531 already been loaded into memory. */
9532
9533 static void
9534 process_full_type_unit (dwarf2_cu *cu,
9535 enum language pretend_language)
9536 {
9537 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9538 struct objfile *objfile = per_objfile->objfile;
9539 struct compunit_symtab *cust;
9540 struct signatured_type *sig_type;
9541
9542 gdb_assert (cu->per_cu->is_debug_types);
9543 sig_type = (struct signatured_type *) cu->per_cu;
9544
9545 /* Clear the list here in case something was left over. */
9546 cu->method_list.clear ();
9547
9548 /* The symbol tables are set up in read_type_unit_scope. */
9549 process_die (cu->dies, cu);
9550
9551 /* For now fudge the Go package. */
9552 if (cu->per_cu->lang == language_go)
9553 fixup_go_packaging (cu);
9554
9555 /* Now that we have processed all the DIEs in the CU, all the types
9556 should be complete, and it should now be safe to compute all of the
9557 physnames. */
9558 compute_delayed_physnames (cu);
9559
9560 if (cu->per_cu->lang == language_rust)
9561 rust_union_quirks (cu);
9562
9563 /* TUs share symbol tables.
9564 If this is the first TU to use this symtab, complete the construction
9565 of it with end_expandable_symtab. Otherwise, complete the addition of
9566 this TU's symbols to the existing symtab. */
9567 type_unit_group_unshareable *tug_unshare =
9568 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9569 if (tug_unshare->compunit_symtab == NULL)
9570 {
9571 buildsym_compunit *builder = cu->get_builder ();
9572 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9573 tug_unshare->compunit_symtab = cust;
9574
9575 if (cust != NULL)
9576 {
9577 /* Set symtab language to language from DW_AT_language. If the
9578 compilation is from a C file generated by language preprocessors,
9579 do not set the language if it was already deduced by
9580 start_subfile. */
9581 if (!(cu->per_cu->lang == language_c
9582 && cust->primary_filetab ()->language () != language_c))
9583 cust->primary_filetab ()->set_language (cu->per_cu->lang);
9584 }
9585 }
9586 else
9587 {
9588 cu->get_builder ()->augment_type_symtab ();
9589 cust = tug_unshare->compunit_symtab;
9590 }
9591
9592 per_objfile->set_symtab (cu->per_cu, cust);
9593
9594 /* Not needed any more. */
9595 cu->reset_builder ();
9596 }
9597
9598 /* Process an imported unit DIE. */
9599
9600 static void
9601 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9602 {
9603 struct attribute *attr;
9604
9605 /* For now we don't handle imported units in type units. */
9606 if (cu->per_cu->is_debug_types)
9607 {
9608 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9609 " supported in type units [in module %s]"),
9610 objfile_name (cu->per_objfile->objfile));
9611 }
9612
9613 attr = dwarf2_attr (die, DW_AT_import, cu);
9614 if (attr != NULL)
9615 {
9616 sect_offset sect_off = attr->get_ref_die_offset ();
9617 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9618 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9619 dwarf2_per_cu_data *per_cu
9620 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9621 per_objfile->per_bfd);
9622
9623 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9624 into another compilation unit, at root level. Regard this as a hint,
9625 and ignore it. */
9626 if (die->parent && die->parent->parent == NULL
9627 && per_cu->unit_type == DW_UT_compile
9628 && per_cu->lang == language_cplus)
9629 return;
9630
9631 /* If necessary, add it to the queue and load its DIEs. */
9632 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9633 cu->per_cu->lang))
9634 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9635 false, cu->per_cu->lang);
9636
9637 cu->per_cu->imported_symtabs_push (per_cu);
9638 }
9639 }
9640
9641 /* RAII object that represents a process_die scope: i.e.,
9642 starts/finishes processing a DIE. */
9643 class process_die_scope
9644 {
9645 public:
9646 process_die_scope (die_info *die, dwarf2_cu *cu)
9647 : m_die (die), m_cu (cu)
9648 {
9649 /* We should only be processing DIEs not already in process. */
9650 gdb_assert (!m_die->in_process);
9651 m_die->in_process = true;
9652 }
9653
9654 ~process_die_scope ()
9655 {
9656 m_die->in_process = false;
9657
9658 /* If we're done processing the DIE for the CU that owns the line
9659 header, we don't need the line header anymore. */
9660 if (m_cu->line_header_die_owner == m_die)
9661 {
9662 delete m_cu->line_header;
9663 m_cu->line_header = NULL;
9664 m_cu->line_header_die_owner = NULL;
9665 }
9666 }
9667
9668 private:
9669 die_info *m_die;
9670 dwarf2_cu *m_cu;
9671 };
9672
9673 /* Process a die and its children. */
9674
9675 static void
9676 process_die (struct die_info *die, struct dwarf2_cu *cu)
9677 {
9678 process_die_scope scope (die, cu);
9679
9680 switch (die->tag)
9681 {
9682 case DW_TAG_padding:
9683 break;
9684 case DW_TAG_compile_unit:
9685 case DW_TAG_partial_unit:
9686 read_file_scope (die, cu);
9687 break;
9688 case DW_TAG_type_unit:
9689 read_type_unit_scope (die, cu);
9690 break;
9691 case DW_TAG_subprogram:
9692 /* Nested subprograms in Fortran get a prefix. */
9693 if (cu->per_cu->lang == language_fortran
9694 && die->parent != NULL
9695 && die->parent->tag == DW_TAG_subprogram)
9696 cu->processing_has_namespace_info = true;
9697 /* Fall through. */
9698 case DW_TAG_inlined_subroutine:
9699 read_func_scope (die, cu);
9700 break;
9701 case DW_TAG_lexical_block:
9702 case DW_TAG_try_block:
9703 case DW_TAG_catch_block:
9704 read_lexical_block_scope (die, cu);
9705 break;
9706 case DW_TAG_call_site:
9707 case DW_TAG_GNU_call_site:
9708 read_call_site_scope (die, cu);
9709 break;
9710 case DW_TAG_class_type:
9711 case DW_TAG_interface_type:
9712 case DW_TAG_structure_type:
9713 case DW_TAG_union_type:
9714 case DW_TAG_namelist:
9715 process_structure_scope (die, cu);
9716 break;
9717 case DW_TAG_enumeration_type:
9718 process_enumeration_scope (die, cu);
9719 break;
9720
9721 /* These dies have a type, but processing them does not create
9722 a symbol or recurse to process the children. Therefore we can
9723 read them on-demand through read_type_die. */
9724 case DW_TAG_subroutine_type:
9725 case DW_TAG_set_type:
9726 case DW_TAG_pointer_type:
9727 case DW_TAG_ptr_to_member_type:
9728 case DW_TAG_reference_type:
9729 case DW_TAG_rvalue_reference_type:
9730 case DW_TAG_string_type:
9731 break;
9732
9733 case DW_TAG_array_type:
9734 /* We only need to handle this case for Ada -- in other
9735 languages, it's normal for the compiler to emit a typedef
9736 instead. */
9737 if (cu->per_cu->lang != language_ada)
9738 break;
9739 /* FALLTHROUGH */
9740 case DW_TAG_base_type:
9741 case DW_TAG_subrange_type:
9742 case DW_TAG_generic_subrange:
9743 case DW_TAG_typedef:
9744 /* Add a typedef symbol for the type definition, if it has a
9745 DW_AT_name. */
9746 new_symbol (die, read_type_die (die, cu), cu);
9747 break;
9748 case DW_TAG_common_block:
9749 read_common_block (die, cu);
9750 break;
9751 case DW_TAG_common_inclusion:
9752 break;
9753 case DW_TAG_namespace:
9754 cu->processing_has_namespace_info = true;
9755 read_namespace (die, cu);
9756 break;
9757 case DW_TAG_module:
9758 cu->processing_has_namespace_info = true;
9759 read_module (die, cu);
9760 break;
9761 case DW_TAG_imported_declaration:
9762 cu->processing_has_namespace_info = true;
9763 if (read_namespace_alias (die, cu))
9764 break;
9765 /* The declaration is not a global namespace alias. */
9766 /* Fall through. */
9767 case DW_TAG_imported_module:
9768 cu->processing_has_namespace_info = true;
9769 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9770 || cu->per_cu->lang != language_fortran))
9771 complaint (_("Tag '%s' has unexpected children"),
9772 dwarf_tag_name (die->tag));
9773 read_import_statement (die, cu);
9774 break;
9775
9776 case DW_TAG_imported_unit:
9777 process_imported_unit_die (die, cu);
9778 break;
9779
9780 case DW_TAG_variable:
9781 read_variable (die, cu);
9782 break;
9783
9784 default:
9785 new_symbol (die, NULL, cu);
9786 break;
9787 }
9788 }
9789 \f
9790 /* DWARF name computation. */
9791
9792 /* A helper function for dwarf2_compute_name which determines whether DIE
9793 needs to have the name of the scope prepended to the name listed in the
9794 die. */
9795
9796 static int
9797 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9798 {
9799 struct attribute *attr;
9800
9801 switch (die->tag)
9802 {
9803 case DW_TAG_namespace:
9804 case DW_TAG_typedef:
9805 case DW_TAG_class_type:
9806 case DW_TAG_interface_type:
9807 case DW_TAG_structure_type:
9808 case DW_TAG_union_type:
9809 case DW_TAG_enumeration_type:
9810 case DW_TAG_enumerator:
9811 case DW_TAG_subprogram:
9812 case DW_TAG_inlined_subroutine:
9813 case DW_TAG_member:
9814 case DW_TAG_imported_declaration:
9815 return 1;
9816
9817 case DW_TAG_variable:
9818 case DW_TAG_constant:
9819 /* We only need to prefix "globally" visible variables. These include
9820 any variable marked with DW_AT_external or any variable that
9821 lives in a namespace. [Variables in anonymous namespaces
9822 require prefixing, but they are not DW_AT_external.] */
9823
9824 if (dwarf2_attr (die, DW_AT_specification, cu))
9825 {
9826 struct dwarf2_cu *spec_cu = cu;
9827
9828 return die_needs_namespace (die_specification (die, &spec_cu),
9829 spec_cu);
9830 }
9831
9832 attr = dwarf2_attr (die, DW_AT_external, cu);
9833 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9834 && die->parent->tag != DW_TAG_module)
9835 return 0;
9836 /* A variable in a lexical block of some kind does not need a
9837 namespace, even though in C++ such variables may be external
9838 and have a mangled name. */
9839 if (die->parent->tag == DW_TAG_lexical_block
9840 || die->parent->tag == DW_TAG_try_block
9841 || die->parent->tag == DW_TAG_catch_block
9842 || die->parent->tag == DW_TAG_subprogram)
9843 return 0;
9844 return 1;
9845
9846 default:
9847 return 0;
9848 }
9849 }
9850
9851 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9852 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9853 defined for the given DIE. */
9854
9855 static struct attribute *
9856 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9857 {
9858 struct attribute *attr;
9859
9860 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9861 if (attr == NULL)
9862 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9863
9864 return attr;
9865 }
9866
9867 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9868 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9869 defined for the given DIE. */
9870
9871 static const char *
9872 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9873 {
9874 const char *linkage_name;
9875
9876 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9877 if (linkage_name == NULL)
9878 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9879
9880 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9881 See https://github.com/rust-lang/rust/issues/32925. */
9882 if (cu->per_cu->lang == language_rust && linkage_name != NULL
9883 && strchr (linkage_name, '{') != NULL)
9884 linkage_name = NULL;
9885
9886 return linkage_name;
9887 }
9888
9889 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9890 compute the physname for the object, which include a method's:
9891 - formal parameters (C++),
9892 - receiver type (Go),
9893
9894 The term "physname" is a bit confusing.
9895 For C++, for example, it is the demangled name.
9896 For Go, for example, it's the mangled name.
9897
9898 For Ada, return the DIE's linkage name rather than the fully qualified
9899 name. PHYSNAME is ignored..
9900
9901 The result is allocated on the objfile->per_bfd's obstack and
9902 canonicalized. */
9903
9904 static const char *
9905 dwarf2_compute_name (const char *name,
9906 struct die_info *die, struct dwarf2_cu *cu,
9907 int physname)
9908 {
9909 struct objfile *objfile = cu->per_objfile->objfile;
9910
9911 if (name == NULL)
9912 name = dwarf2_name (die, cu);
9913
9914 enum language lang = cu->per_cu->lang;
9915
9916 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9917 but otherwise compute it by typename_concat inside GDB.
9918 FIXME: Actually this is not really true, or at least not always true.
9919 It's all very confusing. compute_and_set_names doesn't try to demangle
9920 Fortran names because there is no mangling standard. So new_symbol
9921 will set the demangled name to the result of dwarf2_full_name, and it is
9922 the demangled name that GDB uses if it exists. */
9923 if (lang == language_ada
9924 || (lang == language_fortran && physname))
9925 {
9926 /* For Ada unit, we prefer the linkage name over the name, as
9927 the former contains the exported name, which the user expects
9928 to be able to reference. Ideally, we want the user to be able
9929 to reference this entity using either natural or linkage name,
9930 but we haven't started looking at this enhancement yet. */
9931 const char *linkage_name = dw2_linkage_name (die, cu);
9932
9933 if (linkage_name != NULL)
9934 return linkage_name;
9935 }
9936
9937 /* These are the only languages we know how to qualify names in. */
9938 if (name != NULL
9939 && (lang == language_cplus
9940 || lang == language_fortran || lang == language_d
9941 || lang == language_rust))
9942 {
9943 if (die_needs_namespace (die, cu))
9944 {
9945 const char *prefix;
9946 const char *canonical_name = NULL;
9947
9948 string_file buf;
9949
9950 prefix = determine_prefix (die, cu);
9951 if (*prefix != '\0')
9952 {
9953 gdb::unique_xmalloc_ptr<char> prefixed_name
9954 (typename_concat (NULL, prefix, name, physname, cu));
9955
9956 buf.puts (prefixed_name.get ());
9957 }
9958 else
9959 buf.puts (name);
9960
9961 /* Template parameters may be specified in the DIE's DW_AT_name, or
9962 as children with DW_TAG_template_type_param or
9963 DW_TAG_value_type_param. If the latter, add them to the name
9964 here. If the name already has template parameters, then
9965 skip this step; some versions of GCC emit both, and
9966 it is more efficient to use the pre-computed name.
9967
9968 Something to keep in mind about this process: it is very
9969 unlikely, or in some cases downright impossible, to produce
9970 something that will match the mangled name of a function.
9971 If the definition of the function has the same debug info,
9972 we should be able to match up with it anyway. But fallbacks
9973 using the minimal symbol, for instance to find a method
9974 implemented in a stripped copy of libstdc++, will not work.
9975 If we do not have debug info for the definition, we will have to
9976 match them up some other way.
9977
9978 When we do name matching there is a related problem with function
9979 templates; two instantiated function templates are allowed to
9980 differ only by their return types, which we do not add here. */
9981
9982 if (lang == language_cplus && strchr (name, '<') == NULL)
9983 {
9984 struct attribute *attr;
9985 struct die_info *child;
9986 int first = 1;
9987
9988 die->building_fullname = 1;
9989
9990 for (child = die->child; child != NULL; child = child->sibling)
9991 {
9992 struct type *type;
9993 LONGEST value;
9994 const gdb_byte *bytes;
9995 struct dwarf2_locexpr_baton *baton;
9996 struct value *v;
9997
9998 if (child->tag != DW_TAG_template_type_param
9999 && child->tag != DW_TAG_template_value_param)
10000 continue;
10001
10002 if (first)
10003 {
10004 buf.puts ("<");
10005 first = 0;
10006 }
10007 else
10008 buf.puts (", ");
10009
10010 attr = dwarf2_attr (child, DW_AT_type, cu);
10011 if (attr == NULL)
10012 {
10013 complaint (_("template parameter missing DW_AT_type"));
10014 buf.puts ("UNKNOWN_TYPE");
10015 continue;
10016 }
10017 type = die_type (child, cu);
10018
10019 if (child->tag == DW_TAG_template_type_param)
10020 {
10021 cu->language_defn->print_type (type, "", &buf, -1, 0,
10022 &type_print_raw_options);
10023 continue;
10024 }
10025
10026 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10027 if (attr == NULL)
10028 {
10029 complaint (_("template parameter missing "
10030 "DW_AT_const_value"));
10031 buf.puts ("UNKNOWN_VALUE");
10032 continue;
10033 }
10034
10035 dwarf2_const_value_attr (attr, type, name,
10036 &cu->comp_unit_obstack, cu,
10037 &value, &bytes, &baton);
10038
10039 if (type->has_no_signedness ())
10040 /* GDB prints characters as NUMBER 'CHAR'. If that's
10041 changed, this can use value_print instead. */
10042 cu->language_defn->printchar (value, type, &buf);
10043 else
10044 {
10045 struct value_print_options opts;
10046
10047 if (baton != NULL)
10048 v = dwarf2_evaluate_loc_desc (type, NULL,
10049 baton->data,
10050 baton->size,
10051 baton->per_cu,
10052 baton->per_objfile);
10053 else if (bytes != NULL)
10054 {
10055 v = allocate_value (type);
10056 memcpy (value_contents_writeable (v).data (), bytes,
10057 TYPE_LENGTH (type));
10058 }
10059 else
10060 v = value_from_longest (type, value);
10061
10062 /* Specify decimal so that we do not depend on
10063 the radix. */
10064 get_formatted_print_options (&opts, 'd');
10065 opts.raw = 1;
10066 value_print (v, &buf, &opts);
10067 release_value (v);
10068 }
10069 }
10070
10071 die->building_fullname = 0;
10072
10073 if (!first)
10074 {
10075 /* Close the argument list, with a space if necessary
10076 (nested templates). */
10077 if (!buf.empty () && buf.string ().back () == '>')
10078 buf.puts (" >");
10079 else
10080 buf.puts (">");
10081 }
10082 }
10083
10084 /* For C++ methods, append formal parameter type
10085 information, if PHYSNAME. */
10086
10087 if (physname && die->tag == DW_TAG_subprogram
10088 && lang == language_cplus)
10089 {
10090 struct type *type = read_type_die (die, cu);
10091
10092 c_type_print_args (type, &buf, 1, lang,
10093 &type_print_raw_options);
10094
10095 if (lang == language_cplus)
10096 {
10097 /* Assume that an artificial first parameter is
10098 "this", but do not crash if it is not. RealView
10099 marks unnamed (and thus unused) parameters as
10100 artificial; there is no way to differentiate
10101 the two cases. */
10102 if (type->num_fields () > 0
10103 && TYPE_FIELD_ARTIFICIAL (type, 0)
10104 && type->field (0).type ()->code () == TYPE_CODE_PTR
10105 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10106 buf.puts (" const");
10107 }
10108 }
10109
10110 const std::string &intermediate_name = buf.string ();
10111
10112 if (lang == language_cplus)
10113 canonical_name
10114 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10115 objfile);
10116
10117 /* If we only computed INTERMEDIATE_NAME, or if
10118 INTERMEDIATE_NAME is already canonical, then we need to
10119 intern it. */
10120 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10121 name = objfile->intern (intermediate_name);
10122 else
10123 name = canonical_name;
10124 }
10125 }
10126
10127 return name;
10128 }
10129
10130 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10131 If scope qualifiers are appropriate they will be added. The result
10132 will be allocated on the storage_obstack, or NULL if the DIE does
10133 not have a name. NAME may either be from a previous call to
10134 dwarf2_name or NULL.
10135
10136 The output string will be canonicalized (if C++). */
10137
10138 static const char *
10139 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10140 {
10141 return dwarf2_compute_name (name, die, cu, 0);
10142 }
10143
10144 /* Construct a physname for the given DIE in CU. NAME may either be
10145 from a previous call to dwarf2_name or NULL. The result will be
10146 allocated on the objfile_objstack or NULL if the DIE does not have a
10147 name.
10148
10149 The output string will be canonicalized (if C++). */
10150
10151 static const char *
10152 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10153 {
10154 struct objfile *objfile = cu->per_objfile->objfile;
10155 const char *retval, *mangled = NULL, *canon = NULL;
10156 int need_copy = 1;
10157
10158 /* In this case dwarf2_compute_name is just a shortcut not building anything
10159 on its own. */
10160 if (!die_needs_namespace (die, cu))
10161 return dwarf2_compute_name (name, die, cu, 1);
10162
10163 if (cu->per_cu->lang != language_rust)
10164 mangled = dw2_linkage_name (die, cu);
10165
10166 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10167 has computed. */
10168 gdb::unique_xmalloc_ptr<char> demangled;
10169 if (mangled != NULL)
10170 {
10171 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10172 {
10173 /* Do nothing (do not demangle the symbol name). */
10174 }
10175 else
10176 {
10177 /* Use DMGL_RET_DROP for C++ template functions to suppress
10178 their return type. It is easier for GDB users to search
10179 for such functions as `name(params)' than `long name(params)'.
10180 In such case the minimal symbol names do not match the full
10181 symbol names but for template functions there is never a need
10182 to look up their definition from their declaration so
10183 the only disadvantage remains the minimal symbol variant
10184 `long name(params)' does not have the proper inferior type. */
10185 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
10186 | DMGL_RET_DROP));
10187 }
10188 if (demangled)
10189 canon = demangled.get ();
10190 else
10191 {
10192 canon = mangled;
10193 need_copy = 0;
10194 }
10195 }
10196
10197 if (canon == NULL || check_physname)
10198 {
10199 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10200
10201 if (canon != NULL && strcmp (physname, canon) != 0)
10202 {
10203 /* It may not mean a bug in GDB. The compiler could also
10204 compute DW_AT_linkage_name incorrectly. But in such case
10205 GDB would need to be bug-to-bug compatible. */
10206
10207 complaint (_("Computed physname <%s> does not match demangled <%s> "
10208 "(from linkage <%s>) - DIE at %s [in module %s]"),
10209 physname, canon, mangled, sect_offset_str (die->sect_off),
10210 objfile_name (objfile));
10211
10212 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10213 is available here - over computed PHYSNAME. It is safer
10214 against both buggy GDB and buggy compilers. */
10215
10216 retval = canon;
10217 }
10218 else
10219 {
10220 retval = physname;
10221 need_copy = 0;
10222 }
10223 }
10224 else
10225 retval = canon;
10226
10227 if (need_copy)
10228 retval = objfile->intern (retval);
10229
10230 return retval;
10231 }
10232
10233 /* Inspect DIE in CU for a namespace alias. If one exists, record
10234 a new symbol for it.
10235
10236 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10237
10238 static int
10239 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10240 {
10241 struct attribute *attr;
10242
10243 /* If the die does not have a name, this is not a namespace
10244 alias. */
10245 attr = dwarf2_attr (die, DW_AT_name, cu);
10246 if (attr != NULL)
10247 {
10248 int num;
10249 struct die_info *d = die;
10250 struct dwarf2_cu *imported_cu = cu;
10251
10252 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10253 keep inspecting DIEs until we hit the underlying import. */
10254 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10255 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10256 {
10257 attr = dwarf2_attr (d, DW_AT_import, cu);
10258 if (attr == NULL)
10259 break;
10260
10261 d = follow_die_ref (d, attr, &imported_cu);
10262 if (d->tag != DW_TAG_imported_declaration)
10263 break;
10264 }
10265
10266 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10267 {
10268 complaint (_("DIE at %s has too many recursively imported "
10269 "declarations"), sect_offset_str (d->sect_off));
10270 return 0;
10271 }
10272
10273 if (attr != NULL)
10274 {
10275 struct type *type;
10276 sect_offset sect_off = attr->get_ref_die_offset ();
10277
10278 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10279 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10280 {
10281 /* This declaration is a global namespace alias. Add
10282 a symbol for it whose type is the aliased namespace. */
10283 new_symbol (die, type, cu);
10284 return 1;
10285 }
10286 }
10287 }
10288
10289 return 0;
10290 }
10291
10292 /* Return the using directives repository (global or local?) to use in the
10293 current context for CU.
10294
10295 For Ada, imported declarations can materialize renamings, which *may* be
10296 global. However it is impossible (for now?) in DWARF to distinguish
10297 "external" imported declarations and "static" ones. As all imported
10298 declarations seem to be static in all other languages, make them all CU-wide
10299 global only in Ada. */
10300
10301 static struct using_direct **
10302 using_directives (struct dwarf2_cu *cu)
10303 {
10304 if (cu->per_cu->lang == language_ada
10305 && cu->get_builder ()->outermost_context_p ())
10306 return cu->get_builder ()->get_global_using_directives ();
10307 else
10308 return cu->get_builder ()->get_local_using_directives ();
10309 }
10310
10311 /* Read the import statement specified by the given die and record it. */
10312
10313 static void
10314 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10315 {
10316 struct objfile *objfile = cu->per_objfile->objfile;
10317 struct attribute *import_attr;
10318 struct die_info *imported_die, *child_die;
10319 struct dwarf2_cu *imported_cu;
10320 const char *imported_name;
10321 const char *imported_name_prefix;
10322 const char *canonical_name;
10323 const char *import_alias;
10324 const char *imported_declaration = NULL;
10325 const char *import_prefix;
10326 std::vector<const char *> excludes;
10327
10328 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10329 if (import_attr == NULL)
10330 {
10331 complaint (_("Tag '%s' has no DW_AT_import"),
10332 dwarf_tag_name (die->tag));
10333 return;
10334 }
10335
10336 imported_cu = cu;
10337 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10338 imported_name = dwarf2_name (imported_die, imported_cu);
10339 if (imported_name == NULL)
10340 {
10341 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10342
10343 The import in the following code:
10344 namespace A
10345 {
10346 typedef int B;
10347 }
10348
10349 int main ()
10350 {
10351 using A::B;
10352 B b;
10353 return b;
10354 }
10355
10356 ...
10357 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10358 <52> DW_AT_decl_file : 1
10359 <53> DW_AT_decl_line : 6
10360 <54> DW_AT_import : <0x75>
10361 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10362 <59> DW_AT_name : B
10363 <5b> DW_AT_decl_file : 1
10364 <5c> DW_AT_decl_line : 2
10365 <5d> DW_AT_type : <0x6e>
10366 ...
10367 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10368 <76> DW_AT_byte_size : 4
10369 <77> DW_AT_encoding : 5 (signed)
10370
10371 imports the wrong die ( 0x75 instead of 0x58 ).
10372 This case will be ignored until the gcc bug is fixed. */
10373 return;
10374 }
10375
10376 /* Figure out the local name after import. */
10377 import_alias = dwarf2_name (die, cu);
10378
10379 /* Figure out where the statement is being imported to. */
10380 import_prefix = determine_prefix (die, cu);
10381
10382 /* Figure out what the scope of the imported die is and prepend it
10383 to the name of the imported die. */
10384 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10385
10386 if (imported_die->tag != DW_TAG_namespace
10387 && imported_die->tag != DW_TAG_module)
10388 {
10389 imported_declaration = imported_name;
10390 canonical_name = imported_name_prefix;
10391 }
10392 else if (strlen (imported_name_prefix) > 0)
10393 canonical_name = obconcat (&objfile->objfile_obstack,
10394 imported_name_prefix,
10395 (cu->per_cu->lang == language_d
10396 ? "."
10397 : "::"),
10398 imported_name, (char *) NULL);
10399 else
10400 canonical_name = imported_name;
10401
10402 if (die->tag == DW_TAG_imported_module
10403 && cu->per_cu->lang == language_fortran)
10404 for (child_die = die->child; child_die && child_die->tag;
10405 child_die = child_die->sibling)
10406 {
10407 /* DWARF-4: A Fortran use statement with a “rename list” may be
10408 represented by an imported module entry with an import attribute
10409 referring to the module and owned entries corresponding to those
10410 entities that are renamed as part of being imported. */
10411
10412 if (child_die->tag != DW_TAG_imported_declaration)
10413 {
10414 complaint (_("child DW_TAG_imported_declaration expected "
10415 "- DIE at %s [in module %s]"),
10416 sect_offset_str (child_die->sect_off),
10417 objfile_name (objfile));
10418 continue;
10419 }
10420
10421 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10422 if (import_attr == NULL)
10423 {
10424 complaint (_("Tag '%s' has no DW_AT_import"),
10425 dwarf_tag_name (child_die->tag));
10426 continue;
10427 }
10428
10429 imported_cu = cu;
10430 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10431 &imported_cu);
10432 imported_name = dwarf2_name (imported_die, imported_cu);
10433 if (imported_name == NULL)
10434 {
10435 complaint (_("child DW_TAG_imported_declaration has unknown "
10436 "imported name - DIE at %s [in module %s]"),
10437 sect_offset_str (child_die->sect_off),
10438 objfile_name (objfile));
10439 continue;
10440 }
10441
10442 excludes.push_back (imported_name);
10443
10444 process_die (child_die, cu);
10445 }
10446
10447 add_using_directive (using_directives (cu),
10448 import_prefix,
10449 canonical_name,
10450 import_alias,
10451 imported_declaration,
10452 excludes,
10453 0,
10454 &objfile->objfile_obstack);
10455 }
10456
10457 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10458 types, but gives them a size of zero. Starting with version 14,
10459 ICC is compatible with GCC. */
10460
10461 static bool
10462 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10463 {
10464 if (!cu->checked_producer)
10465 check_producer (cu);
10466
10467 return cu->producer_is_icc_lt_14;
10468 }
10469
10470 /* ICC generates a DW_AT_type for C void functions. This was observed on
10471 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10472 which says that void functions should not have a DW_AT_type. */
10473
10474 static bool
10475 producer_is_icc (struct dwarf2_cu *cu)
10476 {
10477 if (!cu->checked_producer)
10478 check_producer (cu);
10479
10480 return cu->producer_is_icc;
10481 }
10482
10483 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10484 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10485 this, it was first present in GCC release 4.3.0. */
10486
10487 static bool
10488 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10489 {
10490 if (!cu->checked_producer)
10491 check_producer (cu);
10492
10493 return cu->producer_is_gcc_lt_4_3;
10494 }
10495
10496 static file_and_directory &
10497 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10498 {
10499 if (cu->per_cu->fnd != nullptr)
10500 return *cu->per_cu->fnd;
10501
10502 /* Find the filename. Do not use dwarf2_name here, since the filename
10503 is not a source language identifier. */
10504 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
10505 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
10506
10507 if (res.get_comp_dir () == nullptr
10508 && producer_is_gcc_lt_4_3 (cu)
10509 && res.get_name () != nullptr
10510 && IS_ABSOLUTE_PATH (res.get_name ()))
10511 res.set_comp_dir (ldirname (res.get_name ()));
10512
10513 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
10514 return *cu->per_cu->fnd;
10515 }
10516
10517 /* Handle DW_AT_stmt_list for a compilation unit.
10518 DIE is the DW_TAG_compile_unit die for CU.
10519 COMP_DIR is the compilation directory. LOWPC is passed to
10520 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10521
10522 static void
10523 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10524 const file_and_directory &fnd, CORE_ADDR lowpc) /* ARI: editCase function */
10525 {
10526 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10527 struct attribute *attr;
10528 struct line_header line_header_local;
10529 hashval_t line_header_local_hash;
10530 void **slot;
10531 int decode_mapping;
10532
10533 gdb_assert (! cu->per_cu->is_debug_types);
10534
10535 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10536 if (attr == NULL || !attr->form_is_unsigned ())
10537 return;
10538
10539 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10540
10541 /* The line header hash table is only created if needed (it exists to
10542 prevent redundant reading of the line table for partial_units).
10543 If we're given a partial_unit, we'll need it. If we're given a
10544 compile_unit, then use the line header hash table if it's already
10545 created, but don't create one just yet. */
10546
10547 if (per_objfile->line_header_hash == NULL
10548 && die->tag == DW_TAG_partial_unit)
10549 {
10550 per_objfile->line_header_hash
10551 .reset (htab_create_alloc (127, line_header_hash_voidp,
10552 line_header_eq_voidp,
10553 htab_delete_entry<line_header>,
10554 xcalloc, xfree));
10555 }
10556
10557 line_header_local.sect_off = line_offset;
10558 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10559 line_header_local_hash = line_header_hash (&line_header_local);
10560 if (per_objfile->line_header_hash != NULL)
10561 {
10562 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10563 &line_header_local,
10564 line_header_local_hash, NO_INSERT);
10565
10566 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10567 is not present in *SLOT (since if there is something in *SLOT then
10568 it will be for a partial_unit). */
10569 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10570 {
10571 gdb_assert (*slot != NULL);
10572 cu->line_header = (struct line_header *) *slot;
10573 return;
10574 }
10575 }
10576
10577 /* dwarf_decode_line_header does not yet provide sufficient information.
10578 We always have to call also dwarf_decode_lines for it. */
10579 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10580 if (lh == NULL)
10581 return;
10582
10583 cu->line_header = lh.release ();
10584 cu->line_header_die_owner = die;
10585
10586 if (per_objfile->line_header_hash == NULL)
10587 slot = NULL;
10588 else
10589 {
10590 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10591 &line_header_local,
10592 line_header_local_hash, INSERT);
10593 gdb_assert (slot != NULL);
10594 }
10595 if (slot != NULL && *slot == NULL)
10596 {
10597 /* This newly decoded line number information unit will be owned
10598 by line_header_hash hash table. */
10599 *slot = cu->line_header;
10600 cu->line_header_die_owner = NULL;
10601 }
10602 else
10603 {
10604 /* We cannot free any current entry in (*slot) as that struct line_header
10605 may be already used by multiple CUs. Create only temporary decoded
10606 line_header for this CU - it may happen at most once for each line
10607 number information unit. And if we're not using line_header_hash
10608 then this is what we want as well. */
10609 gdb_assert (die->tag != DW_TAG_partial_unit);
10610 }
10611 decode_mapping = (die->tag != DW_TAG_partial_unit);
10612 dwarf_decode_lines (cu->line_header, fnd, cu, nullptr, lowpc,
10613 decode_mapping);
10614
10615 }
10616
10617 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10618
10619 static void
10620 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10621 {
10622 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10623 struct objfile *objfile = per_objfile->objfile;
10624 struct gdbarch *gdbarch = objfile->arch ();
10625 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10626 CORE_ADDR highpc = ((CORE_ADDR) 0);
10627 struct attribute *attr;
10628 struct die_info *child_die;
10629 CORE_ADDR baseaddr;
10630
10631 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
10632 baseaddr = objfile->text_section_offset ();
10633
10634 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10635
10636 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10637 from finish_block. */
10638 if (lowpc == ((CORE_ADDR) -1))
10639 lowpc = highpc;
10640 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10641
10642 file_and_directory &fnd = find_file_and_directory (die, cu);
10643
10644 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
10645 lowpc);
10646
10647 gdb_assert (per_objfile->sym_cu == nullptr);
10648 scoped_restore restore_sym_cu
10649 = make_scoped_restore (&per_objfile->sym_cu, cu);
10650
10651 /* Decode line number information if present. We do this before
10652 processing child DIEs, so that the line header table is available
10653 for DW_AT_decl_file. The PC check is here because, if LOWPC and
10654 HIGHPC are both 0x0, then there won't be any interesting code in
10655 the CU, but a check later on (in
10656 lnp_state_machine::check_line_address) will fail to properly
10657 exclude an entry that was removed via --gc-sections. */
10658 if (lowpc != highpc)
10659 handle_DW_AT_stmt_list (die, cu, fnd, lowpc);
10660
10661 /* Process all dies in compilation unit. */
10662 if (die->child != NULL)
10663 {
10664 child_die = die->child;
10665 while (child_die && child_die->tag)
10666 {
10667 process_die (child_die, cu);
10668 child_die = child_die->sibling;
10669 }
10670 }
10671 per_objfile->sym_cu = nullptr;
10672
10673 /* Decode macro information, if present. Dwarf 2 macro information
10674 refers to information in the line number info statement program
10675 header, so we can only read it if we've read the header
10676 successfully. */
10677 attr = dwarf2_attr (die, DW_AT_macros, cu);
10678 if (attr == NULL)
10679 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10680 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10681 {
10682 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10683 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10684
10685 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10686 }
10687 else
10688 {
10689 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10690 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10691 {
10692 unsigned int macro_offset = attr->as_unsigned ();
10693
10694 dwarf_decode_macros (cu, macro_offset, 0);
10695 }
10696 }
10697 }
10698
10699 void
10700 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10701 {
10702 struct type_unit_group *tu_group;
10703 int first_time;
10704 struct attribute *attr;
10705 unsigned int i;
10706 struct signatured_type *sig_type;
10707
10708 gdb_assert (per_cu->is_debug_types);
10709 sig_type = (struct signatured_type *) per_cu;
10710
10711 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10712
10713 /* If we're using .gdb_index (includes -readnow) then
10714 per_cu->type_unit_group may not have been set up yet. */
10715 if (sig_type->type_unit_group == NULL)
10716 sig_type->type_unit_group = get_type_unit_group (this, attr);
10717 tu_group = sig_type->type_unit_group;
10718
10719 /* If we've already processed this stmt_list there's no real need to
10720 do it again, we could fake it and just recreate the part we need
10721 (file name,index -> symtab mapping). If data shows this optimization
10722 is useful we can do it then. */
10723 type_unit_group_unshareable *tug_unshare
10724 = per_objfile->get_type_unit_group_unshareable (tu_group);
10725 first_time = tug_unshare->compunit_symtab == NULL;
10726
10727 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10728 debug info. */
10729 line_header_up lh;
10730 if (attr != NULL && attr->form_is_unsigned ())
10731 {
10732 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10733 lh = dwarf_decode_line_header (line_offset, this);
10734 }
10735 if (lh == NULL)
10736 {
10737 if (first_time)
10738 start_compunit_symtab ("", NULL, 0);
10739 else
10740 {
10741 gdb_assert (tug_unshare->symtabs == NULL);
10742 gdb_assert (m_builder == nullptr);
10743 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10744 m_builder.reset (new struct buildsym_compunit
10745 (cust->objfile (), "",
10746 cust->dirname (),
10747 compunit_language (cust),
10748 0, cust));
10749 list_in_scope = get_builder ()->get_file_symbols ();
10750 }
10751 return;
10752 }
10753
10754 line_header = lh.release ();
10755 line_header_die_owner = die;
10756
10757 if (first_time)
10758 {
10759 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
10760
10761 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10762 still initializing it, and our caller (a few levels up)
10763 process_full_type_unit still needs to know if this is the first
10764 time. */
10765
10766 tug_unshare->symtabs
10767 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
10768 struct symtab *, line_header->file_names_size ());
10769
10770 auto &file_names = line_header->file_names ();
10771 for (i = 0; i < file_names.size (); ++i)
10772 {
10773 file_entry &fe = file_names[i];
10774 dwarf2_start_subfile (this, fe.name,
10775 fe.include_dir (line_header));
10776 buildsym_compunit *b = get_builder ();
10777 if (b->get_current_subfile ()->symtab == NULL)
10778 {
10779 /* NOTE: start_subfile will recognize when it's been
10780 passed a file it has already seen. So we can't
10781 assume there's a simple mapping from
10782 cu->line_header->file_names to subfiles, plus
10783 cu->line_header->file_names may contain dups. */
10784 b->get_current_subfile ()->symtab
10785 = allocate_symtab (cust, b->get_current_subfile ()->name);
10786 }
10787
10788 fe.symtab = b->get_current_subfile ()->symtab;
10789 tug_unshare->symtabs[i] = fe.symtab;
10790 }
10791 }
10792 else
10793 {
10794 gdb_assert (m_builder == nullptr);
10795 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10796 m_builder.reset (new struct buildsym_compunit
10797 (cust->objfile (), "",
10798 cust->dirname (),
10799 compunit_language (cust),
10800 0, cust));
10801 list_in_scope = get_builder ()->get_file_symbols ();
10802
10803 auto &file_names = line_header->file_names ();
10804 for (i = 0; i < file_names.size (); ++i)
10805 {
10806 file_entry &fe = file_names[i];
10807 fe.symtab = tug_unshare->symtabs[i];
10808 }
10809 }
10810
10811 /* The main symtab is allocated last. Type units don't have DW_AT_name
10812 so they don't have a "real" (so to speak) symtab anyway.
10813 There is later code that will assign the main symtab to all symbols
10814 that don't have one. We need to handle the case of a symbol with a
10815 missing symtab (DW_AT_decl_file) anyway. */
10816 }
10817
10818 /* Process DW_TAG_type_unit.
10819 For TUs we want to skip the first top level sibling if it's not the
10820 actual type being defined by this TU. In this case the first top
10821 level sibling is there to provide context only. */
10822
10823 static void
10824 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10825 {
10826 struct die_info *child_die;
10827
10828 prepare_one_comp_unit (cu, die, language_minimal);
10829
10830 /* Initialize (or reinitialize) the machinery for building symtabs.
10831 We do this before processing child DIEs, so that the line header table
10832 is available for DW_AT_decl_file. */
10833 cu->setup_type_unit_groups (die);
10834
10835 if (die->child != NULL)
10836 {
10837 child_die = die->child;
10838 while (child_die && child_die->tag)
10839 {
10840 process_die (child_die, cu);
10841 child_die = child_die->sibling;
10842 }
10843 }
10844 }
10845 \f
10846 /* DWO/DWP files.
10847
10848 http://gcc.gnu.org/wiki/DebugFission
10849 http://gcc.gnu.org/wiki/DebugFissionDWP
10850
10851 To simplify handling of both DWO files ("object" files with the DWARF info)
10852 and DWP files (a file with the DWOs packaged up into one file), we treat
10853 DWP files as having a collection of virtual DWO files. */
10854
10855 static hashval_t
10856 hash_dwo_file (const void *item)
10857 {
10858 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10859 hashval_t hash;
10860
10861 hash = htab_hash_string (dwo_file->dwo_name);
10862 if (dwo_file->comp_dir != NULL)
10863 hash += htab_hash_string (dwo_file->comp_dir);
10864 return hash;
10865 }
10866
10867 static int
10868 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10869 {
10870 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10871 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10872
10873 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10874 return 0;
10875 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10876 return lhs->comp_dir == rhs->comp_dir;
10877 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10878 }
10879
10880 /* Allocate a hash table for DWO files. */
10881
10882 static htab_up
10883 allocate_dwo_file_hash_table ()
10884 {
10885 return htab_up (htab_create_alloc (41,
10886 hash_dwo_file,
10887 eq_dwo_file,
10888 htab_delete_entry<dwo_file>,
10889 xcalloc, xfree));
10890 }
10891
10892 /* Lookup DWO file DWO_NAME. */
10893
10894 static void **
10895 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10896 const char *dwo_name,
10897 const char *comp_dir)
10898 {
10899 struct dwo_file find_entry;
10900 void **slot;
10901
10902 if (per_objfile->per_bfd->dwo_files == NULL)
10903 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10904
10905 find_entry.dwo_name = dwo_name;
10906 find_entry.comp_dir = comp_dir;
10907 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10908 INSERT);
10909
10910 return slot;
10911 }
10912
10913 static hashval_t
10914 hash_dwo_unit (const void *item)
10915 {
10916 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10917
10918 /* This drops the top 32 bits of the id, but is ok for a hash. */
10919 return dwo_unit->signature;
10920 }
10921
10922 static int
10923 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10924 {
10925 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10926 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10927
10928 /* The signature is assumed to be unique within the DWO file.
10929 So while object file CU dwo_id's always have the value zero,
10930 that's OK, assuming each object file DWO file has only one CU,
10931 and that's the rule for now. */
10932 return lhs->signature == rhs->signature;
10933 }
10934
10935 /* Allocate a hash table for DWO CUs,TUs.
10936 There is one of these tables for each of CUs,TUs for each DWO file. */
10937
10938 static htab_up
10939 allocate_dwo_unit_table ()
10940 {
10941 /* Start out with a pretty small number.
10942 Generally DWO files contain only one CU and maybe some TUs. */
10943 return htab_up (htab_create_alloc (3,
10944 hash_dwo_unit,
10945 eq_dwo_unit,
10946 NULL, xcalloc, xfree));
10947 }
10948
10949 /* die_reader_func for create_dwo_cu. */
10950
10951 static void
10952 create_dwo_cu_reader (const struct die_reader_specs *reader,
10953 const gdb_byte *info_ptr,
10954 struct die_info *comp_unit_die,
10955 struct dwo_file *dwo_file,
10956 struct dwo_unit *dwo_unit)
10957 {
10958 struct dwarf2_cu *cu = reader->cu;
10959 sect_offset sect_off = cu->per_cu->sect_off;
10960 struct dwarf2_section_info *section = cu->per_cu->section;
10961
10962 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10963 if (!signature.has_value ())
10964 {
10965 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10966 " its dwo_id [in module %s]"),
10967 sect_offset_str (sect_off), dwo_file->dwo_name);
10968 return;
10969 }
10970
10971 dwo_unit->dwo_file = dwo_file;
10972 dwo_unit->signature = *signature;
10973 dwo_unit->section = section;
10974 dwo_unit->sect_off = sect_off;
10975 dwo_unit->length = cu->per_cu->length;
10976
10977 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10978 sect_offset_str (sect_off),
10979 hex_string (dwo_unit->signature));
10980 }
10981
10982 /* Create the dwo_units for the CUs in a DWO_FILE.
10983 Note: This function processes DWO files only, not DWP files. */
10984
10985 static void
10986 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10987 dwarf2_cu *cu, struct dwo_file &dwo_file,
10988 dwarf2_section_info &section, htab_up &cus_htab)
10989 {
10990 struct objfile *objfile = per_objfile->objfile;
10991 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10992 const gdb_byte *info_ptr, *end_ptr;
10993
10994 section.read (objfile);
10995 info_ptr = section.buffer;
10996
10997 if (info_ptr == NULL)
10998 return;
10999
11000 dwarf_read_debug_printf ("Reading %s for %s:",
11001 section.get_name (),
11002 section.get_file_name ());
11003
11004 end_ptr = info_ptr + section.size;
11005 while (info_ptr < end_ptr)
11006 {
11007 struct dwarf2_per_cu_data per_cu;
11008 struct dwo_unit read_unit {};
11009 struct dwo_unit *dwo_unit;
11010 void **slot;
11011 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11012
11013 per_cu.per_bfd = per_bfd;
11014 per_cu.is_debug_types = 0;
11015 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11016 per_cu.section = &section;
11017
11018 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11019 if (!reader.dummy_p)
11020 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11021 &dwo_file, &read_unit);
11022 info_ptr += per_cu.length;
11023
11024 // If the unit could not be parsed, skip it.
11025 if (read_unit.dwo_file == NULL)
11026 continue;
11027
11028 if (cus_htab == NULL)
11029 cus_htab = allocate_dwo_unit_table ();
11030
11031 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11032 struct dwo_unit);
11033 *dwo_unit = read_unit;
11034 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11035 gdb_assert (slot != NULL);
11036 if (*slot != NULL)
11037 {
11038 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11039 sect_offset dup_sect_off = dup_cu->sect_off;
11040
11041 complaint (_("debug cu entry at offset %s is duplicate to"
11042 " the entry at offset %s, signature %s"),
11043 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11044 hex_string (dwo_unit->signature));
11045 }
11046 *slot = (void *)dwo_unit;
11047 }
11048 }
11049
11050 /* DWP file .debug_{cu,tu}_index section format:
11051 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11052 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11053
11054 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11055 officially standard DWP format was published with DWARF v5 and is called
11056 Version 5. There are no versions 3 or 4.
11057
11058 DWP Version 1:
11059
11060 Both index sections have the same format, and serve to map a 64-bit
11061 signature to a set of section numbers. Each section begins with a header,
11062 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11063 indexes, and a pool of 32-bit section numbers. The index sections will be
11064 aligned at 8-byte boundaries in the file.
11065
11066 The index section header consists of:
11067
11068 V, 32 bit version number
11069 -, 32 bits unused
11070 N, 32 bit number of compilation units or type units in the index
11071 M, 32 bit number of slots in the hash table
11072
11073 Numbers are recorded using the byte order of the application binary.
11074
11075 The hash table begins at offset 16 in the section, and consists of an array
11076 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11077 order of the application binary). Unused slots in the hash table are 0.
11078 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11079
11080 The parallel table begins immediately after the hash table
11081 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11082 array of 32-bit indexes (using the byte order of the application binary),
11083 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11084 table contains a 32-bit index into the pool of section numbers. For unused
11085 hash table slots, the corresponding entry in the parallel table will be 0.
11086
11087 The pool of section numbers begins immediately following the hash table
11088 (at offset 16 + 12 * M from the beginning of the section). The pool of
11089 section numbers consists of an array of 32-bit words (using the byte order
11090 of the application binary). Each item in the array is indexed starting
11091 from 0. The hash table entry provides the index of the first section
11092 number in the set. Additional section numbers in the set follow, and the
11093 set is terminated by a 0 entry (section number 0 is not used in ELF).
11094
11095 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11096 section must be the first entry in the set, and the .debug_abbrev.dwo must
11097 be the second entry. Other members of the set may follow in any order.
11098
11099 ---
11100
11101 DWP Versions 2 and 5:
11102
11103 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11104 and the entries in the index tables are now offsets into these sections.
11105 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11106 section.
11107
11108 Index Section Contents:
11109 Header
11110 Hash Table of Signatures dwp_hash_table.hash_table
11111 Parallel Table of Indices dwp_hash_table.unit_table
11112 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11113 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11114
11115 The index section header consists of:
11116
11117 V, 32 bit version number
11118 L, 32 bit number of columns in the table of section offsets
11119 N, 32 bit number of compilation units or type units in the index
11120 M, 32 bit number of slots in the hash table
11121
11122 Numbers are recorded using the byte order of the application binary.
11123
11124 The hash table has the same format as version 1.
11125 The parallel table of indices has the same format as version 1,
11126 except that the entries are origin-1 indices into the table of sections
11127 offsets and the table of section sizes.
11128
11129 The table of offsets begins immediately following the parallel table
11130 (at offset 16 + 12 * M from the beginning of the section). The table is
11131 a two-dimensional array of 32-bit words (using the byte order of the
11132 application binary), with L columns and N+1 rows, in row-major order.
11133 Each row in the array is indexed starting from 0. The first row provides
11134 a key to the remaining rows: each column in this row provides an identifier
11135 for a debug section, and the offsets in the same column of subsequent rows
11136 refer to that section. The section identifiers for Version 2 are:
11137
11138 DW_SECT_INFO 1 .debug_info.dwo
11139 DW_SECT_TYPES 2 .debug_types.dwo
11140 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11141 DW_SECT_LINE 4 .debug_line.dwo
11142 DW_SECT_LOC 5 .debug_loc.dwo
11143 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11144 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11145 DW_SECT_MACRO 8 .debug_macro.dwo
11146
11147 The section identifiers for Version 5 are:
11148
11149 DW_SECT_INFO_V5 1 .debug_info.dwo
11150 DW_SECT_RESERVED_V5 2 --
11151 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11152 DW_SECT_LINE_V5 4 .debug_line.dwo
11153 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11154 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11155 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11156 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11157
11158 The offsets provided by the CU and TU index sections are the base offsets
11159 for the contributions made by each CU or TU to the corresponding section
11160 in the package file. Each CU and TU header contains an abbrev_offset
11161 field, used to find the abbreviations table for that CU or TU within the
11162 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11163 be interpreted as relative to the base offset given in the index section.
11164 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11165 should be interpreted as relative to the base offset for .debug_line.dwo,
11166 and offsets into other debug sections obtained from DWARF attributes should
11167 also be interpreted as relative to the corresponding base offset.
11168
11169 The table of sizes begins immediately following the table of offsets.
11170 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11171 with L columns and N rows, in row-major order. Each row in the array is
11172 indexed starting from 1 (row 0 is shared by the two tables).
11173
11174 ---
11175
11176 Hash table lookup is handled the same in version 1 and 2:
11177
11178 We assume that N and M will not exceed 2^32 - 1.
11179 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11180
11181 Given a 64-bit compilation unit signature or a type signature S, an entry
11182 in the hash table is located as follows:
11183
11184 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11185 the low-order k bits all set to 1.
11186
11187 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11188
11189 3) If the hash table entry at index H matches the signature, use that
11190 entry. If the hash table entry at index H is unused (all zeroes),
11191 terminate the search: the signature is not present in the table.
11192
11193 4) Let H = (H + H') modulo M. Repeat at Step 3.
11194
11195 Because M > N and H' and M are relatively prime, the search is guaranteed
11196 to stop at an unused slot or find the match. */
11197
11198 /* Create a hash table to map DWO IDs to their CU/TU entry in
11199 .debug_{info,types}.dwo in DWP_FILE.
11200 Returns NULL if there isn't one.
11201 Note: This function processes DWP files only, not DWO files. */
11202
11203 static struct dwp_hash_table *
11204 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11205 struct dwp_file *dwp_file, int is_debug_types)
11206 {
11207 struct objfile *objfile = per_objfile->objfile;
11208 bfd *dbfd = dwp_file->dbfd.get ();
11209 const gdb_byte *index_ptr, *index_end;
11210 struct dwarf2_section_info *index;
11211 uint32_t version, nr_columns, nr_units, nr_slots;
11212 struct dwp_hash_table *htab;
11213
11214 if (is_debug_types)
11215 index = &dwp_file->sections.tu_index;
11216 else
11217 index = &dwp_file->sections.cu_index;
11218
11219 if (index->empty ())
11220 return NULL;
11221 index->read (objfile);
11222
11223 index_ptr = index->buffer;
11224 index_end = index_ptr + index->size;
11225
11226 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11227 For now it's safe to just read 4 bytes (particularly as it's difficult to
11228 tell if you're dealing with Version 5 before you've read the version). */
11229 version = read_4_bytes (dbfd, index_ptr);
11230 index_ptr += 4;
11231 if (version == 2 || version == 5)
11232 nr_columns = read_4_bytes (dbfd, index_ptr);
11233 else
11234 nr_columns = 0;
11235 index_ptr += 4;
11236 nr_units = read_4_bytes (dbfd, index_ptr);
11237 index_ptr += 4;
11238 nr_slots = read_4_bytes (dbfd, index_ptr);
11239 index_ptr += 4;
11240
11241 if (version != 1 && version != 2 && version != 5)
11242 {
11243 error (_("Dwarf Error: unsupported DWP file version (%s)"
11244 " [in module %s]"),
11245 pulongest (version), dwp_file->name);
11246 }
11247 if (nr_slots != (nr_slots & -nr_slots))
11248 {
11249 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11250 " is not power of 2 [in module %s]"),
11251 pulongest (nr_slots), dwp_file->name);
11252 }
11253
11254 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11255 htab->version = version;
11256 htab->nr_columns = nr_columns;
11257 htab->nr_units = nr_units;
11258 htab->nr_slots = nr_slots;
11259 htab->hash_table = index_ptr;
11260 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11261
11262 /* Exit early if the table is empty. */
11263 if (nr_slots == 0 || nr_units == 0
11264 || (version == 2 && nr_columns == 0)
11265 || (version == 5 && nr_columns == 0))
11266 {
11267 /* All must be zero. */
11268 if (nr_slots != 0 || nr_units != 0
11269 || (version == 2 && nr_columns != 0)
11270 || (version == 5 && nr_columns != 0))
11271 {
11272 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11273 " all zero [in modules %s]"),
11274 dwp_file->name);
11275 }
11276 return htab;
11277 }
11278
11279 if (version == 1)
11280 {
11281 htab->section_pool.v1.indices =
11282 htab->unit_table + sizeof (uint32_t) * nr_slots;
11283 /* It's harder to decide whether the section is too small in v1.
11284 V1 is deprecated anyway so we punt. */
11285 }
11286 else if (version == 2)
11287 {
11288 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11289 int *ids = htab->section_pool.v2.section_ids;
11290 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11291 /* Reverse map for error checking. */
11292 int ids_seen[DW_SECT_MAX + 1];
11293 int i;
11294
11295 if (nr_columns < 2)
11296 {
11297 error (_("Dwarf Error: bad DWP hash table, too few columns"
11298 " in section table [in module %s]"),
11299 dwp_file->name);
11300 }
11301 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11302 {
11303 error (_("Dwarf Error: bad DWP hash table, too many columns"
11304 " in section table [in module %s]"),
11305 dwp_file->name);
11306 }
11307 memset (ids, 255, sizeof_ids);
11308 memset (ids_seen, 255, sizeof (ids_seen));
11309 for (i = 0; i < nr_columns; ++i)
11310 {
11311 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11312
11313 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11314 {
11315 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11316 " in section table [in module %s]"),
11317 id, dwp_file->name);
11318 }
11319 if (ids_seen[id] != -1)
11320 {
11321 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11322 " id %d in section table [in module %s]"),
11323 id, dwp_file->name);
11324 }
11325 ids_seen[id] = i;
11326 ids[i] = id;
11327 }
11328 /* Must have exactly one info or types section. */
11329 if (((ids_seen[DW_SECT_INFO] != -1)
11330 + (ids_seen[DW_SECT_TYPES] != -1))
11331 != 1)
11332 {
11333 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11334 " DWO info/types section [in module %s]"),
11335 dwp_file->name);
11336 }
11337 /* Must have an abbrev section. */
11338 if (ids_seen[DW_SECT_ABBREV] == -1)
11339 {
11340 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11341 " section [in module %s]"),
11342 dwp_file->name);
11343 }
11344 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11345 htab->section_pool.v2.sizes =
11346 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11347 * nr_units * nr_columns);
11348 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11349 * nr_units * nr_columns))
11350 > index_end)
11351 {
11352 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11353 " [in module %s]"),
11354 dwp_file->name);
11355 }
11356 }
11357 else /* version == 5 */
11358 {
11359 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11360 int *ids = htab->section_pool.v5.section_ids;
11361 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11362 /* Reverse map for error checking. */
11363 int ids_seen[DW_SECT_MAX_V5 + 1];
11364
11365 if (nr_columns < 2)
11366 {
11367 error (_("Dwarf Error: bad DWP hash table, too few columns"
11368 " in section table [in module %s]"),
11369 dwp_file->name);
11370 }
11371 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11372 {
11373 error (_("Dwarf Error: bad DWP hash table, too many columns"
11374 " in section table [in module %s]"),
11375 dwp_file->name);
11376 }
11377 memset (ids, 255, sizeof_ids);
11378 memset (ids_seen, 255, sizeof (ids_seen));
11379 for (int i = 0; i < nr_columns; ++i)
11380 {
11381 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11382
11383 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11384 {
11385 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11386 " in section table [in module %s]"),
11387 id, dwp_file->name);
11388 }
11389 if (ids_seen[id] != -1)
11390 {
11391 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11392 " id %d in section table [in module %s]"),
11393 id, dwp_file->name);
11394 }
11395 ids_seen[id] = i;
11396 ids[i] = id;
11397 }
11398 /* Must have seen an info section. */
11399 if (ids_seen[DW_SECT_INFO_V5] == -1)
11400 {
11401 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11402 " DWO info/types section [in module %s]"),
11403 dwp_file->name);
11404 }
11405 /* Must have an abbrev section. */
11406 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11407 {
11408 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11409 " section [in module %s]"),
11410 dwp_file->name);
11411 }
11412 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11413 htab->section_pool.v5.sizes
11414 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11415 * nr_units * nr_columns);
11416 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11417 * nr_units * nr_columns))
11418 > index_end)
11419 {
11420 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11421 " [in module %s]"),
11422 dwp_file->name);
11423 }
11424 }
11425
11426 return htab;
11427 }
11428
11429 /* Update SECTIONS with the data from SECTP.
11430
11431 This function is like the other "locate" section routines, but in
11432 this context the sections to read comes from the DWP V1 hash table,
11433 not the full ELF section table.
11434
11435 The result is non-zero for success, or zero if an error was found. */
11436
11437 static int
11438 locate_v1_virtual_dwo_sections (asection *sectp,
11439 struct virtual_v1_dwo_sections *sections)
11440 {
11441 const struct dwop_section_names *names = &dwop_section_names;
11442
11443 if (names->abbrev_dwo.matches (sectp->name))
11444 {
11445 /* There can be only one. */
11446 if (sections->abbrev.s.section != NULL)
11447 return 0;
11448 sections->abbrev.s.section = sectp;
11449 sections->abbrev.size = bfd_section_size (sectp);
11450 }
11451 else if (names->info_dwo.matches (sectp->name)
11452 || names->types_dwo.matches (sectp->name))
11453 {
11454 /* There can be only one. */
11455 if (sections->info_or_types.s.section != NULL)
11456 return 0;
11457 sections->info_or_types.s.section = sectp;
11458 sections->info_or_types.size = bfd_section_size (sectp);
11459 }
11460 else if (names->line_dwo.matches (sectp->name))
11461 {
11462 /* There can be only one. */
11463 if (sections->line.s.section != NULL)
11464 return 0;
11465 sections->line.s.section = sectp;
11466 sections->line.size = bfd_section_size (sectp);
11467 }
11468 else if (names->loc_dwo.matches (sectp->name))
11469 {
11470 /* There can be only one. */
11471 if (sections->loc.s.section != NULL)
11472 return 0;
11473 sections->loc.s.section = sectp;
11474 sections->loc.size = bfd_section_size (sectp);
11475 }
11476 else if (names->macinfo_dwo.matches (sectp->name))
11477 {
11478 /* There can be only one. */
11479 if (sections->macinfo.s.section != NULL)
11480 return 0;
11481 sections->macinfo.s.section = sectp;
11482 sections->macinfo.size = bfd_section_size (sectp);
11483 }
11484 else if (names->macro_dwo.matches (sectp->name))
11485 {
11486 /* There can be only one. */
11487 if (sections->macro.s.section != NULL)
11488 return 0;
11489 sections->macro.s.section = sectp;
11490 sections->macro.size = bfd_section_size (sectp);
11491 }
11492 else if (names->str_offsets_dwo.matches (sectp->name))
11493 {
11494 /* There can be only one. */
11495 if (sections->str_offsets.s.section != NULL)
11496 return 0;
11497 sections->str_offsets.s.section = sectp;
11498 sections->str_offsets.size = bfd_section_size (sectp);
11499 }
11500 else
11501 {
11502 /* No other kind of section is valid. */
11503 return 0;
11504 }
11505
11506 return 1;
11507 }
11508
11509 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11510 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11511 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11512 This is for DWP version 1 files. */
11513
11514 static struct dwo_unit *
11515 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11516 struct dwp_file *dwp_file,
11517 uint32_t unit_index,
11518 const char *comp_dir,
11519 ULONGEST signature, int is_debug_types)
11520 {
11521 const struct dwp_hash_table *dwp_htab =
11522 is_debug_types ? dwp_file->tus : dwp_file->cus;
11523 bfd *dbfd = dwp_file->dbfd.get ();
11524 const char *kind = is_debug_types ? "TU" : "CU";
11525 struct dwo_file *dwo_file;
11526 struct dwo_unit *dwo_unit;
11527 struct virtual_v1_dwo_sections sections;
11528 void **dwo_file_slot;
11529 int i;
11530
11531 gdb_assert (dwp_file->version == 1);
11532
11533 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11534 kind, pulongest (unit_index), hex_string (signature),
11535 dwp_file->name);
11536
11537 /* Fetch the sections of this DWO unit.
11538 Put a limit on the number of sections we look for so that bad data
11539 doesn't cause us to loop forever. */
11540
11541 #define MAX_NR_V1_DWO_SECTIONS \
11542 (1 /* .debug_info or .debug_types */ \
11543 + 1 /* .debug_abbrev */ \
11544 + 1 /* .debug_line */ \
11545 + 1 /* .debug_loc */ \
11546 + 1 /* .debug_str_offsets */ \
11547 + 1 /* .debug_macro or .debug_macinfo */ \
11548 + 1 /* trailing zero */)
11549
11550 memset (&sections, 0, sizeof (sections));
11551
11552 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11553 {
11554 asection *sectp;
11555 uint32_t section_nr =
11556 read_4_bytes (dbfd,
11557 dwp_htab->section_pool.v1.indices
11558 + (unit_index + i) * sizeof (uint32_t));
11559
11560 if (section_nr == 0)
11561 break;
11562 if (section_nr >= dwp_file->num_sections)
11563 {
11564 error (_("Dwarf Error: bad DWP hash table, section number too large"
11565 " [in module %s]"),
11566 dwp_file->name);
11567 }
11568
11569 sectp = dwp_file->elf_sections[section_nr];
11570 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11571 {
11572 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11573 " [in module %s]"),
11574 dwp_file->name);
11575 }
11576 }
11577
11578 if (i < 2
11579 || sections.info_or_types.empty ()
11580 || sections.abbrev.empty ())
11581 {
11582 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11583 " [in module %s]"),
11584 dwp_file->name);
11585 }
11586 if (i == MAX_NR_V1_DWO_SECTIONS)
11587 {
11588 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11589 " [in module %s]"),
11590 dwp_file->name);
11591 }
11592
11593 /* It's easier for the rest of the code if we fake a struct dwo_file and
11594 have dwo_unit "live" in that. At least for now.
11595
11596 The DWP file can be made up of a random collection of CUs and TUs.
11597 However, for each CU + set of TUs that came from the same original DWO
11598 file, we can combine them back into a virtual DWO file to save space
11599 (fewer struct dwo_file objects to allocate). Remember that for really
11600 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11601
11602 std::string virtual_dwo_name =
11603 string_printf ("virtual-dwo/%d-%d-%d-%d",
11604 sections.abbrev.get_id (),
11605 sections.line.get_id (),
11606 sections.loc.get_id (),
11607 sections.str_offsets.get_id ());
11608 /* Can we use an existing virtual DWO file? */
11609 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11610 comp_dir);
11611 /* Create one if necessary. */
11612 if (*dwo_file_slot == NULL)
11613 {
11614 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11615 virtual_dwo_name.c_str ());
11616
11617 dwo_file = new struct dwo_file;
11618 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11619 dwo_file->comp_dir = comp_dir;
11620 dwo_file->sections.abbrev = sections.abbrev;
11621 dwo_file->sections.line = sections.line;
11622 dwo_file->sections.loc = sections.loc;
11623 dwo_file->sections.macinfo = sections.macinfo;
11624 dwo_file->sections.macro = sections.macro;
11625 dwo_file->sections.str_offsets = sections.str_offsets;
11626 /* The "str" section is global to the entire DWP file. */
11627 dwo_file->sections.str = dwp_file->sections.str;
11628 /* The info or types section is assigned below to dwo_unit,
11629 there's no need to record it in dwo_file.
11630 Also, we can't simply record type sections in dwo_file because
11631 we record a pointer into the vector in dwo_unit. As we collect more
11632 types we'll grow the vector and eventually have to reallocate space
11633 for it, invalidating all copies of pointers into the previous
11634 contents. */
11635 *dwo_file_slot = dwo_file;
11636 }
11637 else
11638 {
11639 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11640 virtual_dwo_name.c_str ());
11641
11642 dwo_file = (struct dwo_file *) *dwo_file_slot;
11643 }
11644
11645 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11646 dwo_unit->dwo_file = dwo_file;
11647 dwo_unit->signature = signature;
11648 dwo_unit->section =
11649 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11650 *dwo_unit->section = sections.info_or_types;
11651 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11652
11653 return dwo_unit;
11654 }
11655
11656 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11657 simplify them. Given a pointer to the containing section SECTION, and
11658 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11659 virtual section of just that piece. */
11660
11661 static struct dwarf2_section_info
11662 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11663 struct dwarf2_section_info *section,
11664 bfd_size_type offset, bfd_size_type size)
11665 {
11666 struct dwarf2_section_info result;
11667 asection *sectp;
11668
11669 gdb_assert (section != NULL);
11670 gdb_assert (!section->is_virtual);
11671
11672 memset (&result, 0, sizeof (result));
11673 result.s.containing_section = section;
11674 result.is_virtual = true;
11675
11676 if (size == 0)
11677 return result;
11678
11679 sectp = section->get_bfd_section ();
11680
11681 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11682 bounds of the real section. This is a pretty-rare event, so just
11683 flag an error (easier) instead of a warning and trying to cope. */
11684 if (sectp == NULL
11685 || offset + size > bfd_section_size (sectp))
11686 {
11687 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11688 " in section %s [in module %s]"),
11689 sectp ? bfd_section_name (sectp) : "<unknown>",
11690 objfile_name (per_objfile->objfile));
11691 }
11692
11693 result.virtual_offset = offset;
11694 result.size = size;
11695 return result;
11696 }
11697
11698 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11699 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11700 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11701 This is for DWP version 2 files. */
11702
11703 static struct dwo_unit *
11704 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11705 struct dwp_file *dwp_file,
11706 uint32_t unit_index,
11707 const char *comp_dir,
11708 ULONGEST signature, int is_debug_types)
11709 {
11710 const struct dwp_hash_table *dwp_htab =
11711 is_debug_types ? dwp_file->tus : dwp_file->cus;
11712 bfd *dbfd = dwp_file->dbfd.get ();
11713 const char *kind = is_debug_types ? "TU" : "CU";
11714 struct dwo_file *dwo_file;
11715 struct dwo_unit *dwo_unit;
11716 struct virtual_v2_or_v5_dwo_sections sections;
11717 void **dwo_file_slot;
11718 int i;
11719
11720 gdb_assert (dwp_file->version == 2);
11721
11722 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11723 kind, pulongest (unit_index), hex_string (signature),
11724 dwp_file->name);
11725
11726 /* Fetch the section offsets of this DWO unit. */
11727
11728 memset (&sections, 0, sizeof (sections));
11729
11730 for (i = 0; i < dwp_htab->nr_columns; ++i)
11731 {
11732 uint32_t offset = read_4_bytes (dbfd,
11733 dwp_htab->section_pool.v2.offsets
11734 + (((unit_index - 1) * dwp_htab->nr_columns
11735 + i)
11736 * sizeof (uint32_t)));
11737 uint32_t size = read_4_bytes (dbfd,
11738 dwp_htab->section_pool.v2.sizes
11739 + (((unit_index - 1) * dwp_htab->nr_columns
11740 + i)
11741 * sizeof (uint32_t)));
11742
11743 switch (dwp_htab->section_pool.v2.section_ids[i])
11744 {
11745 case DW_SECT_INFO:
11746 case DW_SECT_TYPES:
11747 sections.info_or_types_offset = offset;
11748 sections.info_or_types_size = size;
11749 break;
11750 case DW_SECT_ABBREV:
11751 sections.abbrev_offset = offset;
11752 sections.abbrev_size = size;
11753 break;
11754 case DW_SECT_LINE:
11755 sections.line_offset = offset;
11756 sections.line_size = size;
11757 break;
11758 case DW_SECT_LOC:
11759 sections.loc_offset = offset;
11760 sections.loc_size = size;
11761 break;
11762 case DW_SECT_STR_OFFSETS:
11763 sections.str_offsets_offset = offset;
11764 sections.str_offsets_size = size;
11765 break;
11766 case DW_SECT_MACINFO:
11767 sections.macinfo_offset = offset;
11768 sections.macinfo_size = size;
11769 break;
11770 case DW_SECT_MACRO:
11771 sections.macro_offset = offset;
11772 sections.macro_size = size;
11773 break;
11774 }
11775 }
11776
11777 /* It's easier for the rest of the code if we fake a struct dwo_file and
11778 have dwo_unit "live" in that. At least for now.
11779
11780 The DWP file can be made up of a random collection of CUs and TUs.
11781 However, for each CU + set of TUs that came from the same original DWO
11782 file, we can combine them back into a virtual DWO file to save space
11783 (fewer struct dwo_file objects to allocate). Remember that for really
11784 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11785
11786 std::string virtual_dwo_name =
11787 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11788 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11789 (long) (sections.line_size ? sections.line_offset : 0),
11790 (long) (sections.loc_size ? sections.loc_offset : 0),
11791 (long) (sections.str_offsets_size
11792 ? sections.str_offsets_offset : 0));
11793 /* Can we use an existing virtual DWO file? */
11794 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11795 comp_dir);
11796 /* Create one if necessary. */
11797 if (*dwo_file_slot == NULL)
11798 {
11799 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11800 virtual_dwo_name.c_str ());
11801
11802 dwo_file = new struct dwo_file;
11803 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11804 dwo_file->comp_dir = comp_dir;
11805 dwo_file->sections.abbrev =
11806 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11807 sections.abbrev_offset,
11808 sections.abbrev_size);
11809 dwo_file->sections.line =
11810 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11811 sections.line_offset,
11812 sections.line_size);
11813 dwo_file->sections.loc =
11814 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11815 sections.loc_offset, sections.loc_size);
11816 dwo_file->sections.macinfo =
11817 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11818 sections.macinfo_offset,
11819 sections.macinfo_size);
11820 dwo_file->sections.macro =
11821 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11822 sections.macro_offset,
11823 sections.macro_size);
11824 dwo_file->sections.str_offsets =
11825 create_dwp_v2_or_v5_section (per_objfile,
11826 &dwp_file->sections.str_offsets,
11827 sections.str_offsets_offset,
11828 sections.str_offsets_size);
11829 /* The "str" section is global to the entire DWP file. */
11830 dwo_file->sections.str = dwp_file->sections.str;
11831 /* The info or types section is assigned below to dwo_unit,
11832 there's no need to record it in dwo_file.
11833 Also, we can't simply record type sections in dwo_file because
11834 we record a pointer into the vector in dwo_unit. As we collect more
11835 types we'll grow the vector and eventually have to reallocate space
11836 for it, invalidating all copies of pointers into the previous
11837 contents. */
11838 *dwo_file_slot = dwo_file;
11839 }
11840 else
11841 {
11842 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11843 virtual_dwo_name.c_str ());
11844
11845 dwo_file = (struct dwo_file *) *dwo_file_slot;
11846 }
11847
11848 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11849 dwo_unit->dwo_file = dwo_file;
11850 dwo_unit->signature = signature;
11851 dwo_unit->section =
11852 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11853 *dwo_unit->section = create_dwp_v2_or_v5_section
11854 (per_objfile,
11855 is_debug_types
11856 ? &dwp_file->sections.types
11857 : &dwp_file->sections.info,
11858 sections.info_or_types_offset,
11859 sections.info_or_types_size);
11860 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11861
11862 return dwo_unit;
11863 }
11864
11865 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11866 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11867 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11868 This is for DWP version 5 files. */
11869
11870 static struct dwo_unit *
11871 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11872 struct dwp_file *dwp_file,
11873 uint32_t unit_index,
11874 const char *comp_dir,
11875 ULONGEST signature, int is_debug_types)
11876 {
11877 const struct dwp_hash_table *dwp_htab
11878 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11879 bfd *dbfd = dwp_file->dbfd.get ();
11880 const char *kind = is_debug_types ? "TU" : "CU";
11881 struct dwo_file *dwo_file;
11882 struct dwo_unit *dwo_unit;
11883 struct virtual_v2_or_v5_dwo_sections sections {};
11884 void **dwo_file_slot;
11885
11886 gdb_assert (dwp_file->version == 5);
11887
11888 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11889 kind, pulongest (unit_index), hex_string (signature),
11890 dwp_file->name);
11891
11892 /* Fetch the section offsets of this DWO unit. */
11893
11894 /* memset (&sections, 0, sizeof (sections)); */
11895
11896 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11897 {
11898 uint32_t offset = read_4_bytes (dbfd,
11899 dwp_htab->section_pool.v5.offsets
11900 + (((unit_index - 1)
11901 * dwp_htab->nr_columns
11902 + i)
11903 * sizeof (uint32_t)));
11904 uint32_t size = read_4_bytes (dbfd,
11905 dwp_htab->section_pool.v5.sizes
11906 + (((unit_index - 1) * dwp_htab->nr_columns
11907 + i)
11908 * sizeof (uint32_t)));
11909
11910 switch (dwp_htab->section_pool.v5.section_ids[i])
11911 {
11912 case DW_SECT_ABBREV_V5:
11913 sections.abbrev_offset = offset;
11914 sections.abbrev_size = size;
11915 break;
11916 case DW_SECT_INFO_V5:
11917 sections.info_or_types_offset = offset;
11918 sections.info_or_types_size = size;
11919 break;
11920 case DW_SECT_LINE_V5:
11921 sections.line_offset = offset;
11922 sections.line_size = size;
11923 break;
11924 case DW_SECT_LOCLISTS_V5:
11925 sections.loclists_offset = offset;
11926 sections.loclists_size = size;
11927 break;
11928 case DW_SECT_MACRO_V5:
11929 sections.macro_offset = offset;
11930 sections.macro_size = size;
11931 break;
11932 case DW_SECT_RNGLISTS_V5:
11933 sections.rnglists_offset = offset;
11934 sections.rnglists_size = size;
11935 break;
11936 case DW_SECT_STR_OFFSETS_V5:
11937 sections.str_offsets_offset = offset;
11938 sections.str_offsets_size = size;
11939 break;
11940 case DW_SECT_RESERVED_V5:
11941 default:
11942 break;
11943 }
11944 }
11945
11946 /* It's easier for the rest of the code if we fake a struct dwo_file and
11947 have dwo_unit "live" in that. At least for now.
11948
11949 The DWP file can be made up of a random collection of CUs and TUs.
11950 However, for each CU + set of TUs that came from the same original DWO
11951 file, we can combine them back into a virtual DWO file to save space
11952 (fewer struct dwo_file objects to allocate). Remember that for really
11953 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11954
11955 std::string virtual_dwo_name =
11956 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11957 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11958 (long) (sections.line_size ? sections.line_offset : 0),
11959 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11960 (long) (sections.str_offsets_size
11961 ? sections.str_offsets_offset : 0),
11962 (long) (sections.macro_size ? sections.macro_offset : 0),
11963 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11964 /* Can we use an existing virtual DWO file? */
11965 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11966 virtual_dwo_name.c_str (),
11967 comp_dir);
11968 /* Create one if necessary. */
11969 if (*dwo_file_slot == NULL)
11970 {
11971 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11972 virtual_dwo_name.c_str ());
11973
11974 dwo_file = new struct dwo_file;
11975 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11976 dwo_file->comp_dir = comp_dir;
11977 dwo_file->sections.abbrev =
11978 create_dwp_v2_or_v5_section (per_objfile,
11979 &dwp_file->sections.abbrev,
11980 sections.abbrev_offset,
11981 sections.abbrev_size);
11982 dwo_file->sections.line =
11983 create_dwp_v2_or_v5_section (per_objfile,
11984 &dwp_file->sections.line,
11985 sections.line_offset, sections.line_size);
11986 dwo_file->sections.macro =
11987 create_dwp_v2_or_v5_section (per_objfile,
11988 &dwp_file->sections.macro,
11989 sections.macro_offset,
11990 sections.macro_size);
11991 dwo_file->sections.loclists =
11992 create_dwp_v2_or_v5_section (per_objfile,
11993 &dwp_file->sections.loclists,
11994 sections.loclists_offset,
11995 sections.loclists_size);
11996 dwo_file->sections.rnglists =
11997 create_dwp_v2_or_v5_section (per_objfile,
11998 &dwp_file->sections.rnglists,
11999 sections.rnglists_offset,
12000 sections.rnglists_size);
12001 dwo_file->sections.str_offsets =
12002 create_dwp_v2_or_v5_section (per_objfile,
12003 &dwp_file->sections.str_offsets,
12004 sections.str_offsets_offset,
12005 sections.str_offsets_size);
12006 /* The "str" section is global to the entire DWP file. */
12007 dwo_file->sections.str = dwp_file->sections.str;
12008 /* The info or types section is assigned below to dwo_unit,
12009 there's no need to record it in dwo_file.
12010 Also, we can't simply record type sections in dwo_file because
12011 we record a pointer into the vector in dwo_unit. As we collect more
12012 types we'll grow the vector and eventually have to reallocate space
12013 for it, invalidating all copies of pointers into the previous
12014 contents. */
12015 *dwo_file_slot = dwo_file;
12016 }
12017 else
12018 {
12019 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12020 virtual_dwo_name.c_str ());
12021
12022 dwo_file = (struct dwo_file *) *dwo_file_slot;
12023 }
12024
12025 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12026 dwo_unit->dwo_file = dwo_file;
12027 dwo_unit->signature = signature;
12028 dwo_unit->section
12029 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12030 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12031 &dwp_file->sections.info,
12032 sections.info_or_types_offset,
12033 sections.info_or_types_size);
12034 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12035
12036 return dwo_unit;
12037 }
12038
12039 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12040 Returns NULL if the signature isn't found. */
12041
12042 static struct dwo_unit *
12043 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12044 struct dwp_file *dwp_file, const char *comp_dir,
12045 ULONGEST signature, int is_debug_types)
12046 {
12047 const struct dwp_hash_table *dwp_htab =
12048 is_debug_types ? dwp_file->tus : dwp_file->cus;
12049 bfd *dbfd = dwp_file->dbfd.get ();
12050 uint32_t mask = dwp_htab->nr_slots - 1;
12051 uint32_t hash = signature & mask;
12052 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12053 unsigned int i;
12054 void **slot;
12055 struct dwo_unit find_dwo_cu;
12056
12057 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12058 find_dwo_cu.signature = signature;
12059 slot = htab_find_slot (is_debug_types
12060 ? dwp_file->loaded_tus.get ()
12061 : dwp_file->loaded_cus.get (),
12062 &find_dwo_cu, INSERT);
12063
12064 if (*slot != NULL)
12065 return (struct dwo_unit *) *slot;
12066
12067 /* Use a for loop so that we don't loop forever on bad debug info. */
12068 for (i = 0; i < dwp_htab->nr_slots; ++i)
12069 {
12070 ULONGEST signature_in_table;
12071
12072 signature_in_table =
12073 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12074 if (signature_in_table == signature)
12075 {
12076 uint32_t unit_index =
12077 read_4_bytes (dbfd,
12078 dwp_htab->unit_table + hash * sizeof (uint32_t));
12079
12080 if (dwp_file->version == 1)
12081 {
12082 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12083 unit_index, comp_dir,
12084 signature, is_debug_types);
12085 }
12086 else if (dwp_file->version == 2)
12087 {
12088 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12089 unit_index, comp_dir,
12090 signature, is_debug_types);
12091 }
12092 else /* version == 5 */
12093 {
12094 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12095 unit_index, comp_dir,
12096 signature, is_debug_types);
12097 }
12098 return (struct dwo_unit *) *slot;
12099 }
12100 if (signature_in_table == 0)
12101 return NULL;
12102 hash = (hash + hash2) & mask;
12103 }
12104
12105 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12106 " [in module %s]"),
12107 dwp_file->name);
12108 }
12109
12110 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12111 Open the file specified by FILE_NAME and hand it off to BFD for
12112 preliminary analysis. Return a newly initialized bfd *, which
12113 includes a canonicalized copy of FILE_NAME.
12114 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12115 SEARCH_CWD is true if the current directory is to be searched.
12116 It will be searched before debug-file-directory.
12117 If successful, the file is added to the bfd include table of the
12118 objfile's bfd (see gdb_bfd_record_inclusion).
12119 If unable to find/open the file, return NULL.
12120 NOTE: This function is derived from symfile_bfd_open. */
12121
12122 static gdb_bfd_ref_ptr
12123 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12124 const char *file_name, int is_dwp, int search_cwd)
12125 {
12126 int desc;
12127 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12128 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12129 to debug_file_directory. */
12130 const char *search_path;
12131 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12132
12133 gdb::unique_xmalloc_ptr<char> search_path_holder;
12134 if (search_cwd)
12135 {
12136 if (!debug_file_directory.empty ())
12137 {
12138 search_path_holder.reset (concat (".", dirname_separator_string,
12139 debug_file_directory.c_str (),
12140 (char *) NULL));
12141 search_path = search_path_holder.get ();
12142 }
12143 else
12144 search_path = ".";
12145 }
12146 else
12147 search_path = debug_file_directory.c_str ();
12148
12149 /* Add the path for the executable binary to the list of search paths. */
12150 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12151 search_path_holder.reset (concat (objfile_dir.c_str (),
12152 dirname_separator_string,
12153 search_path, nullptr));
12154 search_path = search_path_holder.get ();
12155
12156 openp_flags flags = OPF_RETURN_REALPATH;
12157 if (is_dwp)
12158 flags |= OPF_SEARCH_IN_PATH;
12159
12160 gdb::unique_xmalloc_ptr<char> absolute_name;
12161 desc = openp (search_path, flags, file_name,
12162 O_RDONLY | O_BINARY, &absolute_name);
12163 if (desc < 0)
12164 return NULL;
12165
12166 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12167 gnutarget, desc));
12168 if (sym_bfd == NULL)
12169 return NULL;
12170 bfd_set_cacheable (sym_bfd.get (), 1);
12171
12172 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12173 return NULL;
12174
12175 /* Success. Record the bfd as having been included by the objfile's bfd.
12176 This is important because things like demangled_names_hash lives in the
12177 objfile's per_bfd space and may have references to things like symbol
12178 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12179 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12180
12181 return sym_bfd;
12182 }
12183
12184 /* Try to open DWO file FILE_NAME.
12185 COMP_DIR is the DW_AT_comp_dir attribute.
12186 The result is the bfd handle of the file.
12187 If there is a problem finding or opening the file, return NULL.
12188 Upon success, the canonicalized path of the file is stored in the bfd,
12189 same as symfile_bfd_open. */
12190
12191 static gdb_bfd_ref_ptr
12192 open_dwo_file (dwarf2_per_objfile *per_objfile,
12193 const char *file_name, const char *comp_dir)
12194 {
12195 if (IS_ABSOLUTE_PATH (file_name))
12196 return try_open_dwop_file (per_objfile, file_name,
12197 0 /*is_dwp*/, 0 /*search_cwd*/);
12198
12199 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12200
12201 if (comp_dir != NULL)
12202 {
12203 gdb::unique_xmalloc_ptr<char> path_to_try
12204 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12205
12206 /* NOTE: If comp_dir is a relative path, this will also try the
12207 search path, which seems useful. */
12208 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12209 0 /*is_dwp*/,
12210 1 /*search_cwd*/));
12211 if (abfd != NULL)
12212 return abfd;
12213 }
12214
12215 /* That didn't work, try debug-file-directory, which, despite its name,
12216 is a list of paths. */
12217
12218 if (debug_file_directory.empty ())
12219 return NULL;
12220
12221 return try_open_dwop_file (per_objfile, file_name,
12222 0 /*is_dwp*/, 1 /*search_cwd*/);
12223 }
12224
12225 /* This function is mapped across the sections and remembers the offset and
12226 size of each of the DWO debugging sections we are interested in. */
12227
12228 static void
12229 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12230 dwo_sections *dwo_sections)
12231 {
12232 const struct dwop_section_names *names = &dwop_section_names;
12233
12234 if (names->abbrev_dwo.matches (sectp->name))
12235 {
12236 dwo_sections->abbrev.s.section = sectp;
12237 dwo_sections->abbrev.size = bfd_section_size (sectp);
12238 }
12239 else if (names->info_dwo.matches (sectp->name))
12240 {
12241 dwo_sections->info.s.section = sectp;
12242 dwo_sections->info.size = bfd_section_size (sectp);
12243 }
12244 else if (names->line_dwo.matches (sectp->name))
12245 {
12246 dwo_sections->line.s.section = sectp;
12247 dwo_sections->line.size = bfd_section_size (sectp);
12248 }
12249 else if (names->loc_dwo.matches (sectp->name))
12250 {
12251 dwo_sections->loc.s.section = sectp;
12252 dwo_sections->loc.size = bfd_section_size (sectp);
12253 }
12254 else if (names->loclists_dwo.matches (sectp->name))
12255 {
12256 dwo_sections->loclists.s.section = sectp;
12257 dwo_sections->loclists.size = bfd_section_size (sectp);
12258 }
12259 else if (names->macinfo_dwo.matches (sectp->name))
12260 {
12261 dwo_sections->macinfo.s.section = sectp;
12262 dwo_sections->macinfo.size = bfd_section_size (sectp);
12263 }
12264 else if (names->macro_dwo.matches (sectp->name))
12265 {
12266 dwo_sections->macro.s.section = sectp;
12267 dwo_sections->macro.size = bfd_section_size (sectp);
12268 }
12269 else if (names->rnglists_dwo.matches (sectp->name))
12270 {
12271 dwo_sections->rnglists.s.section = sectp;
12272 dwo_sections->rnglists.size = bfd_section_size (sectp);
12273 }
12274 else if (names->str_dwo.matches (sectp->name))
12275 {
12276 dwo_sections->str.s.section = sectp;
12277 dwo_sections->str.size = bfd_section_size (sectp);
12278 }
12279 else if (names->str_offsets_dwo.matches (sectp->name))
12280 {
12281 dwo_sections->str_offsets.s.section = sectp;
12282 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12283 }
12284 else if (names->types_dwo.matches (sectp->name))
12285 {
12286 struct dwarf2_section_info type_section;
12287
12288 memset (&type_section, 0, sizeof (type_section));
12289 type_section.s.section = sectp;
12290 type_section.size = bfd_section_size (sectp);
12291 dwo_sections->types.push_back (type_section);
12292 }
12293 }
12294
12295 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12296 by PER_CU. This is for the non-DWP case.
12297 The result is NULL if DWO_NAME can't be found. */
12298
12299 static struct dwo_file *
12300 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12301 const char *comp_dir)
12302 {
12303 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12304
12305 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12306 if (dbfd == NULL)
12307 {
12308 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12309
12310 return NULL;
12311 }
12312
12313 dwo_file_up dwo_file (new struct dwo_file);
12314 dwo_file->dwo_name = dwo_name;
12315 dwo_file->comp_dir = comp_dir;
12316 dwo_file->dbfd = std::move (dbfd);
12317
12318 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12319 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12320 &dwo_file->sections);
12321
12322 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12323 dwo_file->cus);
12324
12325 if (cu->per_cu->dwarf_version < 5)
12326 {
12327 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12328 dwo_file->sections.types, dwo_file->tus);
12329 }
12330 else
12331 {
12332 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12333 &dwo_file->sections.info, dwo_file->tus,
12334 rcuh_kind::COMPILE);
12335 }
12336
12337 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12338
12339 return dwo_file.release ();
12340 }
12341
12342 /* This function is mapped across the sections and remembers the offset and
12343 size of each of the DWP debugging sections common to version 1 and 2 that
12344 we are interested in. */
12345
12346 static void
12347 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12348 dwp_file *dwp_file)
12349 {
12350 const struct dwop_section_names *names = &dwop_section_names;
12351 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12352
12353 /* Record the ELF section number for later lookup: this is what the
12354 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12355 gdb_assert (elf_section_nr < dwp_file->num_sections);
12356 dwp_file->elf_sections[elf_section_nr] = sectp;
12357
12358 /* Look for specific sections that we need. */
12359 if (names->str_dwo.matches (sectp->name))
12360 {
12361 dwp_file->sections.str.s.section = sectp;
12362 dwp_file->sections.str.size = bfd_section_size (sectp);
12363 }
12364 else if (names->cu_index.matches (sectp->name))
12365 {
12366 dwp_file->sections.cu_index.s.section = sectp;
12367 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12368 }
12369 else if (names->tu_index.matches (sectp->name))
12370 {
12371 dwp_file->sections.tu_index.s.section = sectp;
12372 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12373 }
12374 }
12375
12376 /* This function is mapped across the sections and remembers the offset and
12377 size of each of the DWP version 2 debugging sections that we are interested
12378 in. This is split into a separate function because we don't know if we
12379 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12380
12381 static void
12382 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12383 {
12384 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12385 const struct dwop_section_names *names = &dwop_section_names;
12386 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12387
12388 /* Record the ELF section number for later lookup: this is what the
12389 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12390 gdb_assert (elf_section_nr < dwp_file->num_sections);
12391 dwp_file->elf_sections[elf_section_nr] = sectp;
12392
12393 /* Look for specific sections that we need. */
12394 if (names->abbrev_dwo.matches (sectp->name))
12395 {
12396 dwp_file->sections.abbrev.s.section = sectp;
12397 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12398 }
12399 else if (names->info_dwo.matches (sectp->name))
12400 {
12401 dwp_file->sections.info.s.section = sectp;
12402 dwp_file->sections.info.size = bfd_section_size (sectp);
12403 }
12404 else if (names->line_dwo.matches (sectp->name))
12405 {
12406 dwp_file->sections.line.s.section = sectp;
12407 dwp_file->sections.line.size = bfd_section_size (sectp);
12408 }
12409 else if (names->loc_dwo.matches (sectp->name))
12410 {
12411 dwp_file->sections.loc.s.section = sectp;
12412 dwp_file->sections.loc.size = bfd_section_size (sectp);
12413 }
12414 else if (names->macinfo_dwo.matches (sectp->name))
12415 {
12416 dwp_file->sections.macinfo.s.section = sectp;
12417 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12418 }
12419 else if (names->macro_dwo.matches (sectp->name))
12420 {
12421 dwp_file->sections.macro.s.section = sectp;
12422 dwp_file->sections.macro.size = bfd_section_size (sectp);
12423 }
12424 else if (names->str_offsets_dwo.matches (sectp->name))
12425 {
12426 dwp_file->sections.str_offsets.s.section = sectp;
12427 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12428 }
12429 else if (names->types_dwo.matches (sectp->name))
12430 {
12431 dwp_file->sections.types.s.section = sectp;
12432 dwp_file->sections.types.size = bfd_section_size (sectp);
12433 }
12434 }
12435
12436 /* This function is mapped across the sections and remembers the offset and
12437 size of each of the DWP version 5 debugging sections that we are interested
12438 in. This is split into a separate function because we don't know if we
12439 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12440
12441 static void
12442 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12443 {
12444 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12445 const struct dwop_section_names *names = &dwop_section_names;
12446 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12447
12448 /* Record the ELF section number for later lookup: this is what the
12449 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12450 gdb_assert (elf_section_nr < dwp_file->num_sections);
12451 dwp_file->elf_sections[elf_section_nr] = sectp;
12452
12453 /* Look for specific sections that we need. */
12454 if (names->abbrev_dwo.matches (sectp->name))
12455 {
12456 dwp_file->sections.abbrev.s.section = sectp;
12457 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12458 }
12459 else if (names->info_dwo.matches (sectp->name))
12460 {
12461 dwp_file->sections.info.s.section = sectp;
12462 dwp_file->sections.info.size = bfd_section_size (sectp);
12463 }
12464 else if (names->line_dwo.matches (sectp->name))
12465 {
12466 dwp_file->sections.line.s.section = sectp;
12467 dwp_file->sections.line.size = bfd_section_size (sectp);
12468 }
12469 else if (names->loclists_dwo.matches (sectp->name))
12470 {
12471 dwp_file->sections.loclists.s.section = sectp;
12472 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12473 }
12474 else if (names->macro_dwo.matches (sectp->name))
12475 {
12476 dwp_file->sections.macro.s.section = sectp;
12477 dwp_file->sections.macro.size = bfd_section_size (sectp);
12478 }
12479 else if (names->rnglists_dwo.matches (sectp->name))
12480 {
12481 dwp_file->sections.rnglists.s.section = sectp;
12482 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12483 }
12484 else if (names->str_offsets_dwo.matches (sectp->name))
12485 {
12486 dwp_file->sections.str_offsets.s.section = sectp;
12487 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12488 }
12489 }
12490
12491 /* Hash function for dwp_file loaded CUs/TUs. */
12492
12493 static hashval_t
12494 hash_dwp_loaded_cutus (const void *item)
12495 {
12496 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12497
12498 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12499 return dwo_unit->signature;
12500 }
12501
12502 /* Equality function for dwp_file loaded CUs/TUs. */
12503
12504 static int
12505 eq_dwp_loaded_cutus (const void *a, const void *b)
12506 {
12507 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12508 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12509
12510 return dua->signature == dub->signature;
12511 }
12512
12513 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12514
12515 static htab_up
12516 allocate_dwp_loaded_cutus_table ()
12517 {
12518 return htab_up (htab_create_alloc (3,
12519 hash_dwp_loaded_cutus,
12520 eq_dwp_loaded_cutus,
12521 NULL, xcalloc, xfree));
12522 }
12523
12524 /* Try to open DWP file FILE_NAME.
12525 The result is the bfd handle of the file.
12526 If there is a problem finding or opening the file, return NULL.
12527 Upon success, the canonicalized path of the file is stored in the bfd,
12528 same as symfile_bfd_open. */
12529
12530 static gdb_bfd_ref_ptr
12531 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12532 {
12533 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12534 1 /*is_dwp*/,
12535 1 /*search_cwd*/));
12536 if (abfd != NULL)
12537 return abfd;
12538
12539 /* Work around upstream bug 15652.
12540 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12541 [Whether that's a "bug" is debatable, but it is getting in our way.]
12542 We have no real idea where the dwp file is, because gdb's realpath-ing
12543 of the executable's path may have discarded the needed info.
12544 [IWBN if the dwp file name was recorded in the executable, akin to
12545 .gnu_debuglink, but that doesn't exist yet.]
12546 Strip the directory from FILE_NAME and search again. */
12547 if (!debug_file_directory.empty ())
12548 {
12549 /* Don't implicitly search the current directory here.
12550 If the user wants to search "." to handle this case,
12551 it must be added to debug-file-directory. */
12552 return try_open_dwop_file (per_objfile, lbasename (file_name),
12553 1 /*is_dwp*/,
12554 0 /*search_cwd*/);
12555 }
12556
12557 return NULL;
12558 }
12559
12560 /* Initialize the use of the DWP file for the current objfile.
12561 By convention the name of the DWP file is ${objfile}.dwp.
12562 The result is NULL if it can't be found. */
12563
12564 static std::unique_ptr<struct dwp_file>
12565 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12566 {
12567 struct objfile *objfile = per_objfile->objfile;
12568
12569 /* Try to find first .dwp for the binary file before any symbolic links
12570 resolving. */
12571
12572 /* If the objfile is a debug file, find the name of the real binary
12573 file and get the name of dwp file from there. */
12574 std::string dwp_name;
12575 if (objfile->separate_debug_objfile_backlink != NULL)
12576 {
12577 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12578 const char *backlink_basename = lbasename (backlink->original_name);
12579
12580 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12581 }
12582 else
12583 dwp_name = objfile->original_name;
12584
12585 dwp_name += ".dwp";
12586
12587 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12588 if (dbfd == NULL
12589 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12590 {
12591 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12592 dwp_name = objfile_name (objfile);
12593 dwp_name += ".dwp";
12594 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12595 }
12596
12597 if (dbfd == NULL)
12598 {
12599 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12600
12601 return std::unique_ptr<dwp_file> ();
12602 }
12603
12604 const char *name = bfd_get_filename (dbfd.get ());
12605 std::unique_ptr<struct dwp_file> dwp_file
12606 (new struct dwp_file (name, std::move (dbfd)));
12607
12608 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12609 dwp_file->elf_sections =
12610 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12611 dwp_file->num_sections, asection *);
12612
12613 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12614 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12615 dwp_file.get ());
12616
12617 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12618
12619 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12620
12621 /* The DWP file version is stored in the hash table. Oh well. */
12622 if (dwp_file->cus && dwp_file->tus
12623 && dwp_file->cus->version != dwp_file->tus->version)
12624 {
12625 /* Technically speaking, we should try to limp along, but this is
12626 pretty bizarre. We use pulongest here because that's the established
12627 portability solution (e.g, we cannot use %u for uint32_t). */
12628 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12629 " TU version %s [in DWP file %s]"),
12630 pulongest (dwp_file->cus->version),
12631 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12632 }
12633
12634 if (dwp_file->cus)
12635 dwp_file->version = dwp_file->cus->version;
12636 else if (dwp_file->tus)
12637 dwp_file->version = dwp_file->tus->version;
12638 else
12639 dwp_file->version = 2;
12640
12641 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12642 {
12643 if (dwp_file->version == 2)
12644 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12645 dwp_file.get ());
12646 else
12647 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12648 dwp_file.get ());
12649 }
12650
12651 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12652 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12653
12654 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12655 dwarf_read_debug_printf (" %s CUs, %s TUs",
12656 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12657 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12658
12659 return dwp_file;
12660 }
12661
12662 /* Wrapper around open_and_init_dwp_file, only open it once. */
12663
12664 static struct dwp_file *
12665 get_dwp_file (dwarf2_per_objfile *per_objfile)
12666 {
12667 if (!per_objfile->per_bfd->dwp_checked)
12668 {
12669 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12670 per_objfile->per_bfd->dwp_checked = 1;
12671 }
12672 return per_objfile->per_bfd->dwp_file.get ();
12673 }
12674
12675 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12676 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12677 or in the DWP file for the objfile, referenced by THIS_UNIT.
12678 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12679 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12680
12681 This is called, for example, when wanting to read a variable with a
12682 complex location. Therefore we don't want to do file i/o for every call.
12683 Therefore we don't want to look for a DWO file on every call.
12684 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12685 then we check if we've already seen DWO_NAME, and only THEN do we check
12686 for a DWO file.
12687
12688 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12689 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12690
12691 static struct dwo_unit *
12692 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12693 ULONGEST signature, int is_debug_types)
12694 {
12695 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12696 struct objfile *objfile = per_objfile->objfile;
12697 const char *kind = is_debug_types ? "TU" : "CU";
12698 void **dwo_file_slot;
12699 struct dwo_file *dwo_file;
12700 struct dwp_file *dwp_file;
12701
12702 /* First see if there's a DWP file.
12703 If we have a DWP file but didn't find the DWO inside it, don't
12704 look for the original DWO file. It makes gdb behave differently
12705 depending on whether one is debugging in the build tree. */
12706
12707 dwp_file = get_dwp_file (per_objfile);
12708 if (dwp_file != NULL)
12709 {
12710 const struct dwp_hash_table *dwp_htab =
12711 is_debug_types ? dwp_file->tus : dwp_file->cus;
12712
12713 if (dwp_htab != NULL)
12714 {
12715 struct dwo_unit *dwo_cutu =
12716 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12717 is_debug_types);
12718
12719 if (dwo_cutu != NULL)
12720 {
12721 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12722 kind, hex_string (signature),
12723 host_address_to_string (dwo_cutu));
12724
12725 return dwo_cutu;
12726 }
12727 }
12728 }
12729 else
12730 {
12731 /* No DWP file, look for the DWO file. */
12732
12733 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12734 if (*dwo_file_slot == NULL)
12735 {
12736 /* Read in the file and build a table of the CUs/TUs it contains. */
12737 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12738 }
12739 /* NOTE: This will be NULL if unable to open the file. */
12740 dwo_file = (struct dwo_file *) *dwo_file_slot;
12741
12742 if (dwo_file != NULL)
12743 {
12744 struct dwo_unit *dwo_cutu = NULL;
12745
12746 if (is_debug_types && dwo_file->tus)
12747 {
12748 struct dwo_unit find_dwo_cutu;
12749
12750 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12751 find_dwo_cutu.signature = signature;
12752 dwo_cutu
12753 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12754 &find_dwo_cutu);
12755 }
12756 else if (!is_debug_types && dwo_file->cus)
12757 {
12758 struct dwo_unit find_dwo_cutu;
12759
12760 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12761 find_dwo_cutu.signature = signature;
12762 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12763 &find_dwo_cutu);
12764 }
12765
12766 if (dwo_cutu != NULL)
12767 {
12768 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12769 kind, dwo_name, hex_string (signature),
12770 host_address_to_string (dwo_cutu));
12771
12772 return dwo_cutu;
12773 }
12774 }
12775 }
12776
12777 /* We didn't find it. This could mean a dwo_id mismatch, or
12778 someone deleted the DWO/DWP file, or the search path isn't set up
12779 correctly to find the file. */
12780
12781 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12782 kind, dwo_name, hex_string (signature));
12783
12784 /* This is a warning and not a complaint because it can be caused by
12785 pilot error (e.g., user accidentally deleting the DWO). */
12786 {
12787 /* Print the name of the DWP file if we looked there, helps the user
12788 better diagnose the problem. */
12789 std::string dwp_text;
12790
12791 if (dwp_file != NULL)
12792 dwp_text = string_printf (" [in DWP file %s]",
12793 lbasename (dwp_file->name));
12794
12795 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12796 " [in module %s]"),
12797 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12798 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12799 }
12800 return NULL;
12801 }
12802
12803 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12804 See lookup_dwo_cutu_unit for details. */
12805
12806 static struct dwo_unit *
12807 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12808 ULONGEST signature)
12809 {
12810 gdb_assert (!cu->per_cu->is_debug_types);
12811
12812 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12813 }
12814
12815 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12816 See lookup_dwo_cutu_unit for details. */
12817
12818 static struct dwo_unit *
12819 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12820 {
12821 gdb_assert (cu->per_cu->is_debug_types);
12822
12823 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12824
12825 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12826 }
12827
12828 /* Traversal function for queue_and_load_all_dwo_tus. */
12829
12830 static int
12831 queue_and_load_dwo_tu (void **slot, void *info)
12832 {
12833 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12834 dwarf2_cu *cu = (dwarf2_cu *) info;
12835 ULONGEST signature = dwo_unit->signature;
12836 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12837
12838 if (sig_type != NULL)
12839 {
12840 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12841 a real dependency of PER_CU on SIG_TYPE. That is detected later
12842 while processing PER_CU. */
12843 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12844 cu->per_cu->lang))
12845 load_full_type_unit (sig_type, cu->per_objfile);
12846 cu->per_cu->imported_symtabs_push (sig_type);
12847 }
12848
12849 return 1;
12850 }
12851
12852 /* Queue all TUs contained in the DWO of CU to be read in.
12853 The DWO may have the only definition of the type, though it may not be
12854 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12855 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12856
12857 static void
12858 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12859 {
12860 struct dwo_unit *dwo_unit;
12861 struct dwo_file *dwo_file;
12862
12863 gdb_assert (cu != nullptr);
12864 gdb_assert (!cu->per_cu->is_debug_types);
12865 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12866
12867 dwo_unit = cu->dwo_unit;
12868 gdb_assert (dwo_unit != NULL);
12869
12870 dwo_file = dwo_unit->dwo_file;
12871 if (dwo_file->tus != NULL)
12872 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12873 }
12874
12875 /* Read in various DIEs. */
12876
12877 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12878 Inherit only the children of the DW_AT_abstract_origin DIE not being
12879 already referenced by DW_AT_abstract_origin from the children of the
12880 current DIE. */
12881
12882 static void
12883 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12884 {
12885 struct die_info *child_die;
12886 sect_offset *offsetp;
12887 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12888 struct die_info *origin_die;
12889 /* Iterator of the ORIGIN_DIE children. */
12890 struct die_info *origin_child_die;
12891 struct attribute *attr;
12892 struct dwarf2_cu *origin_cu;
12893 struct pending **origin_previous_list_in_scope;
12894
12895 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12896 if (!attr)
12897 return;
12898
12899 /* Note that following die references may follow to a die in a
12900 different cu. */
12901
12902 origin_cu = cu;
12903 origin_die = follow_die_ref (die, attr, &origin_cu);
12904
12905 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12906 symbols in. */
12907 origin_previous_list_in_scope = origin_cu->list_in_scope;
12908 origin_cu->list_in_scope = cu->list_in_scope;
12909
12910 if (die->tag != origin_die->tag
12911 && !(die->tag == DW_TAG_inlined_subroutine
12912 && origin_die->tag == DW_TAG_subprogram))
12913 complaint (_("DIE %s and its abstract origin %s have different tags"),
12914 sect_offset_str (die->sect_off),
12915 sect_offset_str (origin_die->sect_off));
12916
12917 /* Find if the concrete and abstract trees are structurally the
12918 same. This is a shallow traversal and it is not bullet-proof;
12919 the compiler can trick the debugger into believing that the trees
12920 are isomorphic, whereas they actually are not. However, the
12921 likelyhood of this happening is pretty low, and a full-fledged
12922 check would be an overkill. */
12923 bool are_isomorphic = true;
12924 die_info *concrete_child = die->child;
12925 die_info *abstract_child = origin_die->child;
12926 while (concrete_child != nullptr || abstract_child != nullptr)
12927 {
12928 if (concrete_child == nullptr
12929 || abstract_child == nullptr
12930 || concrete_child->tag != abstract_child->tag)
12931 {
12932 are_isomorphic = false;
12933 break;
12934 }
12935
12936 concrete_child = concrete_child->sibling;
12937 abstract_child = abstract_child->sibling;
12938 }
12939
12940 /* Walk the origin's children in parallel to the concrete children.
12941 This helps match an origin child in case the debug info misses
12942 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12943 origin tree may not have the same tree structure as the concrete
12944 DIE, though. */
12945 die_info *corresponding_abstract_child
12946 = are_isomorphic ? origin_die->child : nullptr;
12947
12948 std::vector<sect_offset> offsets;
12949
12950 for (child_die = die->child;
12951 child_die && child_die->tag;
12952 child_die = child_die->sibling)
12953 {
12954 struct die_info *child_origin_die;
12955 struct dwarf2_cu *child_origin_cu;
12956
12957 /* We are trying to process concrete instance entries:
12958 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12959 it's not relevant to our analysis here. i.e. detecting DIEs that are
12960 present in the abstract instance but not referenced in the concrete
12961 one. */
12962 if (child_die->tag == DW_TAG_call_site
12963 || child_die->tag == DW_TAG_GNU_call_site)
12964 {
12965 if (are_isomorphic)
12966 corresponding_abstract_child
12967 = corresponding_abstract_child->sibling;
12968 continue;
12969 }
12970
12971 /* For each CHILD_DIE, find the corresponding child of
12972 ORIGIN_DIE. If there is more than one layer of
12973 DW_AT_abstract_origin, follow them all; there shouldn't be,
12974 but GCC versions at least through 4.4 generate this (GCC PR
12975 40573). */
12976 child_origin_die = child_die;
12977 child_origin_cu = cu;
12978 while (1)
12979 {
12980 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12981 child_origin_cu);
12982 if (attr == NULL)
12983 break;
12984 child_origin_die = follow_die_ref (child_origin_die, attr,
12985 &child_origin_cu);
12986 }
12987
12988 /* If missing DW_AT_abstract_origin, try the corresponding child
12989 of the origin. Clang emits such lexical scopes. */
12990 if (child_origin_die == child_die
12991 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12992 && are_isomorphic
12993 && child_die->tag == DW_TAG_lexical_block)
12994 child_origin_die = corresponding_abstract_child;
12995
12996 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12997 counterpart may exist. */
12998 if (child_origin_die != child_die)
12999 {
13000 if (child_die->tag != child_origin_die->tag
13001 && !(child_die->tag == DW_TAG_inlined_subroutine
13002 && child_origin_die->tag == DW_TAG_subprogram))
13003 complaint (_("Child DIE %s and its abstract origin %s have "
13004 "different tags"),
13005 sect_offset_str (child_die->sect_off),
13006 sect_offset_str (child_origin_die->sect_off));
13007 if (child_origin_die->parent != origin_die)
13008 complaint (_("Child DIE %s and its abstract origin %s have "
13009 "different parents"),
13010 sect_offset_str (child_die->sect_off),
13011 sect_offset_str (child_origin_die->sect_off));
13012 else
13013 offsets.push_back (child_origin_die->sect_off);
13014 }
13015
13016 if (are_isomorphic)
13017 corresponding_abstract_child = corresponding_abstract_child->sibling;
13018 }
13019 std::sort (offsets.begin (), offsets.end ());
13020 sect_offset *offsets_end = offsets.data () + offsets.size ();
13021 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13022 if (offsetp[-1] == *offsetp)
13023 complaint (_("Multiple children of DIE %s refer "
13024 "to DIE %s as their abstract origin"),
13025 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13026
13027 offsetp = offsets.data ();
13028 origin_child_die = origin_die->child;
13029 while (origin_child_die && origin_child_die->tag)
13030 {
13031 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13032 while (offsetp < offsets_end
13033 && *offsetp < origin_child_die->sect_off)
13034 offsetp++;
13035 if (offsetp >= offsets_end
13036 || *offsetp > origin_child_die->sect_off)
13037 {
13038 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13039 Check whether we're already processing ORIGIN_CHILD_DIE.
13040 This can happen with mutually referenced abstract_origins.
13041 PR 16581. */
13042 if (!origin_child_die->in_process)
13043 process_die (origin_child_die, origin_cu);
13044 }
13045 origin_child_die = origin_child_die->sibling;
13046 }
13047 origin_cu->list_in_scope = origin_previous_list_in_scope;
13048
13049 if (cu != origin_cu)
13050 compute_delayed_physnames (origin_cu);
13051 }
13052
13053 static void
13054 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13055 {
13056 struct objfile *objfile = cu->per_objfile->objfile;
13057 struct gdbarch *gdbarch = objfile->arch ();
13058 struct context_stack *newobj;
13059 CORE_ADDR lowpc;
13060 CORE_ADDR highpc;
13061 struct die_info *child_die;
13062 struct attribute *attr, *call_line, *call_file;
13063 const char *name;
13064 CORE_ADDR baseaddr;
13065 struct block *block;
13066 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13067 std::vector<struct symbol *> template_args;
13068 struct template_symbol *templ_func = NULL;
13069
13070 if (inlined_func)
13071 {
13072 /* If we do not have call site information, we can't show the
13073 caller of this inlined function. That's too confusing, so
13074 only use the scope for local variables. */
13075 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13076 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13077 if (call_line == NULL || call_file == NULL)
13078 {
13079 read_lexical_block_scope (die, cu);
13080 return;
13081 }
13082 }
13083
13084 baseaddr = objfile->text_section_offset ();
13085
13086 name = dwarf2_name (die, cu);
13087
13088 /* Ignore functions with missing or empty names. These are actually
13089 illegal according to the DWARF standard. */
13090 if (name == NULL)
13091 {
13092 complaint (_("missing name for subprogram DIE at %s"),
13093 sect_offset_str (die->sect_off));
13094 return;
13095 }
13096
13097 /* Ignore functions with missing or invalid low and high pc attributes. */
13098 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13099 <= PC_BOUNDS_INVALID)
13100 {
13101 attr = dwarf2_attr (die, DW_AT_external, cu);
13102 if (attr == nullptr || !attr->as_boolean ())
13103 complaint (_("cannot get low and high bounds "
13104 "for subprogram DIE at %s"),
13105 sect_offset_str (die->sect_off));
13106 return;
13107 }
13108
13109 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13110 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13111
13112 /* If we have any template arguments, then we must allocate a
13113 different sort of symbol. */
13114 for (child_die = die->child; child_die; child_die = child_die->sibling)
13115 {
13116 if (child_die->tag == DW_TAG_template_type_param
13117 || child_die->tag == DW_TAG_template_value_param)
13118 {
13119 templ_func = new (&objfile->objfile_obstack) template_symbol;
13120 templ_func->subclass = SYMBOL_TEMPLATE;
13121 break;
13122 }
13123 }
13124
13125 gdb_assert (cu->get_builder () != nullptr);
13126 newobj = cu->get_builder ()->push_context (0, lowpc);
13127 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13128 (struct symbol *) templ_func);
13129
13130 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13131 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13132 cu->per_cu->lang);
13133
13134 /* If there is a location expression for DW_AT_frame_base, record
13135 it. */
13136 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13137 if (attr != nullptr)
13138 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13139
13140 /* If there is a location for the static link, record it. */
13141 newobj->static_link = NULL;
13142 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13143 if (attr != nullptr)
13144 {
13145 newobj->static_link
13146 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13147 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13148 cu->addr_type ());
13149 }
13150
13151 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13152
13153 if (die->child != NULL)
13154 {
13155 child_die = die->child;
13156 while (child_die && child_die->tag)
13157 {
13158 if (child_die->tag == DW_TAG_template_type_param
13159 || child_die->tag == DW_TAG_template_value_param)
13160 {
13161 struct symbol *arg = new_symbol (child_die, NULL, cu);
13162
13163 if (arg != NULL)
13164 template_args.push_back (arg);
13165 }
13166 else
13167 process_die (child_die, cu);
13168 child_die = child_die->sibling;
13169 }
13170 }
13171
13172 inherit_abstract_dies (die, cu);
13173
13174 /* If we have a DW_AT_specification, we might need to import using
13175 directives from the context of the specification DIE. See the
13176 comment in determine_prefix. */
13177 if (cu->per_cu->lang == language_cplus
13178 && dwarf2_attr (die, DW_AT_specification, cu))
13179 {
13180 struct dwarf2_cu *spec_cu = cu;
13181 struct die_info *spec_die = die_specification (die, &spec_cu);
13182
13183 while (spec_die)
13184 {
13185 child_die = spec_die->child;
13186 while (child_die && child_die->tag)
13187 {
13188 if (child_die->tag == DW_TAG_imported_module)
13189 process_die (child_die, spec_cu);
13190 child_die = child_die->sibling;
13191 }
13192
13193 /* In some cases, GCC generates specification DIEs that
13194 themselves contain DW_AT_specification attributes. */
13195 spec_die = die_specification (spec_die, &spec_cu);
13196 }
13197 }
13198
13199 struct context_stack cstk = cu->get_builder ()->pop_context ();
13200 /* Make a block for the local symbols within. */
13201 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13202 cstk.static_link, lowpc, highpc);
13203
13204 /* For C++, set the block's scope. */
13205 if ((cu->per_cu->lang == language_cplus
13206 || cu->per_cu->lang == language_fortran
13207 || cu->per_cu->lang == language_d
13208 || cu->per_cu->lang == language_rust)
13209 && cu->processing_has_namespace_info)
13210 block_set_scope (block, determine_prefix (die, cu),
13211 &objfile->objfile_obstack);
13212
13213 /* If we have address ranges, record them. */
13214 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13215
13216 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13217
13218 /* Attach template arguments to function. */
13219 if (!template_args.empty ())
13220 {
13221 gdb_assert (templ_func != NULL);
13222
13223 templ_func->n_template_arguments = template_args.size ();
13224 templ_func->template_arguments
13225 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13226 templ_func->n_template_arguments);
13227 memcpy (templ_func->template_arguments,
13228 template_args.data (),
13229 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13230
13231 /* Make sure that the symtab is set on the new symbols. Even
13232 though they don't appear in this symtab directly, other parts
13233 of gdb assume that symbols do, and this is reasonably
13234 true. */
13235 for (symbol *sym : template_args)
13236 symbol_set_symtab (sym, symbol_symtab (templ_func));
13237 }
13238
13239 /* In C++, we can have functions nested inside functions (e.g., when
13240 a function declares a class that has methods). This means that
13241 when we finish processing a function scope, we may need to go
13242 back to building a containing block's symbol lists. */
13243 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13244 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13245
13246 /* If we've finished processing a top-level function, subsequent
13247 symbols go in the file symbol list. */
13248 if (cu->get_builder ()->outermost_context_p ())
13249 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13250 }
13251
13252 /* Process all the DIES contained within a lexical block scope. Start
13253 a new scope, process the dies, and then close the scope. */
13254
13255 static void
13256 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13257 {
13258 struct objfile *objfile = cu->per_objfile->objfile;
13259 struct gdbarch *gdbarch = objfile->arch ();
13260 CORE_ADDR lowpc, highpc;
13261 struct die_info *child_die;
13262 CORE_ADDR baseaddr;
13263
13264 baseaddr = objfile->text_section_offset ();
13265
13266 /* Ignore blocks with missing or invalid low and high pc attributes. */
13267 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13268 as multiple lexical blocks? Handling children in a sane way would
13269 be nasty. Might be easier to properly extend generic blocks to
13270 describe ranges. */
13271 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13272 {
13273 case PC_BOUNDS_NOT_PRESENT:
13274 /* DW_TAG_lexical_block has no attributes, process its children as if
13275 there was no wrapping by that DW_TAG_lexical_block.
13276 GCC does no longer produces such DWARF since GCC r224161. */
13277 for (child_die = die->child;
13278 child_die != NULL && child_die->tag;
13279 child_die = child_die->sibling)
13280 {
13281 /* We might already be processing this DIE. This can happen
13282 in an unusual circumstance -- where a subroutine A
13283 appears lexically in another subroutine B, but A actually
13284 inlines B. The recursion is broken here, rather than in
13285 inherit_abstract_dies, because it seems better to simply
13286 drop concrete children here. */
13287 if (!child_die->in_process)
13288 process_die (child_die, cu);
13289 }
13290 return;
13291 case PC_BOUNDS_INVALID:
13292 return;
13293 }
13294 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13295 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13296
13297 cu->get_builder ()->push_context (0, lowpc);
13298 if (die->child != NULL)
13299 {
13300 child_die = die->child;
13301 while (child_die && child_die->tag)
13302 {
13303 process_die (child_die, cu);
13304 child_die = child_die->sibling;
13305 }
13306 }
13307 inherit_abstract_dies (die, cu);
13308 struct context_stack cstk = cu->get_builder ()->pop_context ();
13309
13310 if (*cu->get_builder ()->get_local_symbols () != NULL
13311 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13312 {
13313 struct block *block
13314 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13315 cstk.start_addr, highpc);
13316
13317 /* Note that recording ranges after traversing children, as we
13318 do here, means that recording a parent's ranges entails
13319 walking across all its children's ranges as they appear in
13320 the address map, which is quadratic behavior.
13321
13322 It would be nicer to record the parent's ranges before
13323 traversing its children, simply overriding whatever you find
13324 there. But since we don't even decide whether to create a
13325 block until after we've traversed its children, that's hard
13326 to do. */
13327 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13328 }
13329 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13330 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13331 }
13332
13333 static void dwarf2_ranges_read_low_addrs (unsigned offset,
13334 struct dwarf2_cu *cu,
13335 dwarf_tag tag,
13336 std::vector<CORE_ADDR> &result);
13337
13338 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13339
13340 static void
13341 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13342 {
13343 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13344 struct objfile *objfile = per_objfile->objfile;
13345 struct gdbarch *gdbarch = objfile->arch ();
13346 CORE_ADDR pc, baseaddr;
13347 struct attribute *attr;
13348 void **slot;
13349 int nparams;
13350 struct die_info *child_die;
13351
13352 baseaddr = objfile->text_section_offset ();
13353
13354 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13355 if (attr == NULL)
13356 {
13357 /* This was a pre-DWARF-5 GNU extension alias
13358 for DW_AT_call_return_pc. */
13359 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13360 }
13361 if (!attr)
13362 {
13363 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13364 "DIE %s [in module %s]"),
13365 sect_offset_str (die->sect_off), objfile_name (objfile));
13366 return;
13367 }
13368 pc = attr->as_address () + baseaddr;
13369 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13370 pc -= baseaddr;
13371
13372 if (cu->call_site_htab == NULL)
13373 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
13374 call_site::eq, NULL,
13375 &objfile->objfile_obstack,
13376 hashtab_obstack_allocate, NULL);
13377 struct call_site call_site_local (pc, nullptr, nullptr);
13378 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13379 if (*slot != NULL)
13380 {
13381 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13382 "DIE %s [in module %s]"),
13383 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13384 objfile_name (objfile));
13385 return;
13386 }
13387
13388 /* Count parameters at the caller. */
13389
13390 nparams = 0;
13391 for (child_die = die->child; child_die && child_die->tag;
13392 child_die = child_die->sibling)
13393 {
13394 if (child_die->tag != DW_TAG_call_site_parameter
13395 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13396 {
13397 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13398 "DW_TAG_call_site child DIE %s [in module %s]"),
13399 child_die->tag, sect_offset_str (child_die->sect_off),
13400 objfile_name (objfile));
13401 continue;
13402 }
13403
13404 nparams++;
13405 }
13406
13407 struct call_site *call_site
13408 = new (XOBNEWVAR (&objfile->objfile_obstack,
13409 struct call_site,
13410 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
13411 struct call_site (pc, cu->per_cu, per_objfile);
13412 *slot = call_site;
13413
13414 /* We never call the destructor of call_site, so we must ensure it is
13415 trivially destructible. */
13416 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
13417
13418 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13419 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13420 {
13421 struct die_info *func_die;
13422
13423 /* Skip also over DW_TAG_inlined_subroutine. */
13424 for (func_die = die->parent;
13425 func_die && func_die->tag != DW_TAG_subprogram
13426 && func_die->tag != DW_TAG_subroutine_type;
13427 func_die = func_die->parent);
13428
13429 /* DW_AT_call_all_calls is a superset
13430 of DW_AT_call_all_tail_calls. */
13431 if (func_die
13432 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13433 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13434 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13435 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13436 {
13437 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13438 not complete. But keep CALL_SITE for look ups via call_site_htab,
13439 both the initial caller containing the real return address PC and
13440 the final callee containing the current PC of a chain of tail
13441 calls do not need to have the tail call list complete. But any
13442 function candidate for a virtual tail call frame searched via
13443 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13444 determined unambiguously. */
13445 }
13446 else
13447 {
13448 struct type *func_type = NULL;
13449
13450 if (func_die)
13451 func_type = get_die_type (func_die, cu);
13452 if (func_type != NULL)
13453 {
13454 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13455
13456 /* Enlist this call site to the function. */
13457 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13458 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13459 }
13460 else
13461 complaint (_("Cannot find function owning DW_TAG_call_site "
13462 "DIE %s [in module %s]"),
13463 sect_offset_str (die->sect_off), objfile_name (objfile));
13464 }
13465 }
13466
13467 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13468 if (attr == NULL)
13469 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13470 if (attr == NULL)
13471 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13472 if (attr == NULL)
13473 {
13474 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13475 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13476 }
13477
13478 call_site->target.set_loc_dwarf_block (nullptr);
13479 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13480 /* Keep NULL DWARF_BLOCK. */;
13481 else if (attr->form_is_block ())
13482 {
13483 struct dwarf2_locexpr_baton *dlbaton;
13484 struct dwarf_block *block = attr->as_block ();
13485
13486 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13487 dlbaton->data = block->data;
13488 dlbaton->size = block->size;
13489 dlbaton->per_objfile = per_objfile;
13490 dlbaton->per_cu = cu->per_cu;
13491
13492 call_site->target.set_loc_dwarf_block (dlbaton);
13493 }
13494 else if (attr->form_is_ref ())
13495 {
13496 struct dwarf2_cu *target_cu = cu;
13497 struct die_info *target_die;
13498
13499 target_die = follow_die_ref (die, attr, &target_cu);
13500 gdb_assert (target_cu->per_objfile->objfile == objfile);
13501
13502 struct attribute *ranges_attr
13503 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
13504
13505 if (die_is_declaration (target_die, target_cu))
13506 {
13507 const char *target_physname;
13508
13509 /* Prefer the mangled name; otherwise compute the demangled one. */
13510 target_physname = dw2_linkage_name (target_die, target_cu);
13511 if (target_physname == NULL)
13512 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13513 if (target_physname == NULL)
13514 complaint (_("DW_AT_call_target target DIE has invalid "
13515 "physname, for referencing DIE %s [in module %s]"),
13516 sect_offset_str (die->sect_off), objfile_name (objfile));
13517 else
13518 call_site->target.set_loc_physname (target_physname);
13519 }
13520 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
13521 {
13522 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
13523 + target_cu->gnu_ranges_base);
13524 std::vector<CORE_ADDR> addresses;
13525 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
13526 target_die->tag, addresses);
13527 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
13528 addresses.size ());
13529 std::copy (addresses.begin (), addresses.end (), saved);
13530 call_site->target.set_loc_array (addresses.size (), saved);
13531 }
13532 else
13533 {
13534 CORE_ADDR lowpc;
13535
13536 /* DW_AT_entry_pc should be preferred. */
13537 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13538 <= PC_BOUNDS_INVALID)
13539 complaint (_("DW_AT_call_target target DIE has invalid "
13540 "low pc, for referencing DIE %s [in module %s]"),
13541 sect_offset_str (die->sect_off), objfile_name (objfile));
13542 else
13543 {
13544 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
13545 - baseaddr);
13546 call_site->target.set_loc_physaddr (lowpc);
13547 }
13548 }
13549 }
13550 else
13551 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13552 "block nor reference, for DIE %s [in module %s]"),
13553 sect_offset_str (die->sect_off), objfile_name (objfile));
13554
13555 for (child_die = die->child;
13556 child_die && child_die->tag;
13557 child_die = child_die->sibling)
13558 {
13559 struct call_site_parameter *parameter;
13560 struct attribute *loc, *origin;
13561
13562 if (child_die->tag != DW_TAG_call_site_parameter
13563 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13564 {
13565 /* Already printed the complaint above. */
13566 continue;
13567 }
13568
13569 gdb_assert (call_site->parameter_count < nparams);
13570 parameter = &call_site->parameter[call_site->parameter_count];
13571
13572 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13573 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13574 register is contained in DW_AT_call_value. */
13575
13576 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13577 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13578 if (origin == NULL)
13579 {
13580 /* This was a pre-DWARF-5 GNU extension alias
13581 for DW_AT_call_parameter. */
13582 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13583 }
13584 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13585 {
13586 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13587
13588 sect_offset sect_off = origin->get_ref_die_offset ();
13589 if (!cu->header.offset_in_cu_p (sect_off))
13590 {
13591 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13592 binding can be done only inside one CU. Such referenced DIE
13593 therefore cannot be even moved to DW_TAG_partial_unit. */
13594 complaint (_("DW_AT_call_parameter offset is not in CU for "
13595 "DW_TAG_call_site child DIE %s [in module %s]"),
13596 sect_offset_str (child_die->sect_off),
13597 objfile_name (objfile));
13598 continue;
13599 }
13600 parameter->u.param_cu_off
13601 = (cu_offset) (sect_off - cu->header.sect_off);
13602 }
13603 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13604 {
13605 complaint (_("No DW_FORM_block* DW_AT_location for "
13606 "DW_TAG_call_site child DIE %s [in module %s]"),
13607 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13608 continue;
13609 }
13610 else
13611 {
13612 struct dwarf_block *block = loc->as_block ();
13613
13614 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13615 (block->data, &block->data[block->size]);
13616 if (parameter->u.dwarf_reg != -1)
13617 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13618 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13619 &block->data[block->size],
13620 &parameter->u.fb_offset))
13621 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13622 else
13623 {
13624 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13625 "for DW_FORM_block* DW_AT_location is supported for "
13626 "DW_TAG_call_site child DIE %s "
13627 "[in module %s]"),
13628 sect_offset_str (child_die->sect_off),
13629 objfile_name (objfile));
13630 continue;
13631 }
13632 }
13633
13634 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13635 if (attr == NULL)
13636 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13637 if (attr == NULL || !attr->form_is_block ())
13638 {
13639 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13640 "DW_TAG_call_site child DIE %s [in module %s]"),
13641 sect_offset_str (child_die->sect_off),
13642 objfile_name (objfile));
13643 continue;
13644 }
13645
13646 struct dwarf_block *block = attr->as_block ();
13647 parameter->value = block->data;
13648 parameter->value_size = block->size;
13649
13650 /* Parameters are not pre-cleared by memset above. */
13651 parameter->data_value = NULL;
13652 parameter->data_value_size = 0;
13653 call_site->parameter_count++;
13654
13655 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13656 if (attr == NULL)
13657 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13658 if (attr != nullptr)
13659 {
13660 if (!attr->form_is_block ())
13661 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13662 "DW_TAG_call_site child DIE %s [in module %s]"),
13663 sect_offset_str (child_die->sect_off),
13664 objfile_name (objfile));
13665 else
13666 {
13667 block = attr->as_block ();
13668 parameter->data_value = block->data;
13669 parameter->data_value_size = block->size;
13670 }
13671 }
13672 }
13673 }
13674
13675 /* Helper function for read_variable. If DIE represents a virtual
13676 table, then return the type of the concrete object that is
13677 associated with the virtual table. Otherwise, return NULL. */
13678
13679 static struct type *
13680 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13681 {
13682 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13683 if (attr == NULL)
13684 return NULL;
13685
13686 /* Find the type DIE. */
13687 struct die_info *type_die = NULL;
13688 struct dwarf2_cu *type_cu = cu;
13689
13690 if (attr->form_is_ref ())
13691 type_die = follow_die_ref (die, attr, &type_cu);
13692 if (type_die == NULL)
13693 return NULL;
13694
13695 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13696 return NULL;
13697 return die_containing_type (type_die, type_cu);
13698 }
13699
13700 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13701
13702 static void
13703 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13704 {
13705 struct rust_vtable_symbol *storage = NULL;
13706
13707 if (cu->per_cu->lang == language_rust)
13708 {
13709 struct type *containing_type = rust_containing_type (die, cu);
13710
13711 if (containing_type != NULL)
13712 {
13713 struct objfile *objfile = cu->per_objfile->objfile;
13714
13715 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13716 storage->concrete_type = containing_type;
13717 storage->subclass = SYMBOL_RUST_VTABLE;
13718 }
13719 }
13720
13721 struct symbol *res = new_symbol (die, NULL, cu, storage);
13722 struct attribute *abstract_origin
13723 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13724 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13725 if (res == NULL && loc && abstract_origin)
13726 {
13727 /* We have a variable without a name, but with a location and an abstract
13728 origin. This may be a concrete instance of an abstract variable
13729 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13730 later. */
13731 struct dwarf2_cu *origin_cu = cu;
13732 struct die_info *origin_die
13733 = follow_die_ref (die, abstract_origin, &origin_cu);
13734 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13735 per_objfile->per_bfd->abstract_to_concrete
13736 [origin_die->sect_off].push_back (die->sect_off);
13737 }
13738 }
13739
13740 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13741 reading .debug_rnglists.
13742 Callback's type should be:
13743 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13744 Return true if the attributes are present and valid, otherwise,
13745 return false. */
13746
13747 template <typename Callback>
13748 static bool
13749 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13750 dwarf_tag tag, Callback &&callback)
13751 {
13752 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13753 struct objfile *objfile = per_objfile->objfile;
13754 bfd *obfd = objfile->obfd;
13755 /* Base address selection entry. */
13756 gdb::optional<CORE_ADDR> base;
13757 const gdb_byte *buffer;
13758 bool overflow = false;
13759 ULONGEST addr_index;
13760 struct dwarf2_section_info *rnglists_section;
13761
13762 base = cu->base_address;
13763 rnglists_section = cu_debug_rnglists_section (cu, tag);
13764 rnglists_section->read (objfile);
13765
13766 if (offset >= rnglists_section->size)
13767 {
13768 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13769 offset);
13770 return false;
13771 }
13772 buffer = rnglists_section->buffer + offset;
13773
13774 while (1)
13775 {
13776 /* Initialize it due to a false compiler warning. */
13777 CORE_ADDR range_beginning = 0, range_end = 0;
13778 const gdb_byte *buf_end = (rnglists_section->buffer
13779 + rnglists_section->size);
13780 unsigned int bytes_read;
13781
13782 if (buffer == buf_end)
13783 {
13784 overflow = true;
13785 break;
13786 }
13787 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13788 switch (rlet)
13789 {
13790 case DW_RLE_end_of_list:
13791 break;
13792 case DW_RLE_base_address:
13793 if (buffer + cu->header.addr_size > buf_end)
13794 {
13795 overflow = true;
13796 break;
13797 }
13798 base = cu->header.read_address (obfd, buffer, &bytes_read);
13799 buffer += bytes_read;
13800 break;
13801 case DW_RLE_base_addressx:
13802 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13803 buffer += bytes_read;
13804 base = read_addr_index (cu, addr_index);
13805 break;
13806 case DW_RLE_start_length:
13807 if (buffer + cu->header.addr_size > buf_end)
13808 {
13809 overflow = true;
13810 break;
13811 }
13812 range_beginning = cu->header.read_address (obfd, buffer,
13813 &bytes_read);
13814 buffer += bytes_read;
13815 range_end = (range_beginning
13816 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13817 buffer += bytes_read;
13818 if (buffer > buf_end)
13819 {
13820 overflow = true;
13821 break;
13822 }
13823 break;
13824 case DW_RLE_startx_length:
13825 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13826 buffer += bytes_read;
13827 range_beginning = read_addr_index (cu, addr_index);
13828 if (buffer > buf_end)
13829 {
13830 overflow = true;
13831 break;
13832 }
13833 range_end = (range_beginning
13834 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13835 buffer += bytes_read;
13836 break;
13837 case DW_RLE_offset_pair:
13838 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13839 buffer += bytes_read;
13840 if (buffer > buf_end)
13841 {
13842 overflow = true;
13843 break;
13844 }
13845 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13846 buffer += bytes_read;
13847 if (buffer > buf_end)
13848 {
13849 overflow = true;
13850 break;
13851 }
13852 break;
13853 case DW_RLE_start_end:
13854 if (buffer + 2 * cu->header.addr_size > buf_end)
13855 {
13856 overflow = true;
13857 break;
13858 }
13859 range_beginning = cu->header.read_address (obfd, buffer,
13860 &bytes_read);
13861 buffer += bytes_read;
13862 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13863 buffer += bytes_read;
13864 break;
13865 case DW_RLE_startx_endx:
13866 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13867 buffer += bytes_read;
13868 range_beginning = read_addr_index (cu, addr_index);
13869 if (buffer > buf_end)
13870 {
13871 overflow = true;
13872 break;
13873 }
13874 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13875 buffer += bytes_read;
13876 range_end = read_addr_index (cu, addr_index);
13877 break;
13878 default:
13879 complaint (_("Invalid .debug_rnglists data (no base address)"));
13880 return false;
13881 }
13882 if (rlet == DW_RLE_end_of_list || overflow)
13883 break;
13884 if (rlet == DW_RLE_base_address)
13885 continue;
13886
13887 if (range_beginning > range_end)
13888 {
13889 /* Inverted range entries are invalid. */
13890 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13891 return false;
13892 }
13893
13894 /* Empty range entries have no effect. */
13895 if (range_beginning == range_end)
13896 continue;
13897
13898 /* Only DW_RLE_offset_pair needs the base address added. */
13899 if (rlet == DW_RLE_offset_pair)
13900 {
13901 if (!base.has_value ())
13902 {
13903 /* We have no valid base address for the DW_RLE_offset_pair. */
13904 complaint (_("Invalid .debug_rnglists data (no base address for "
13905 "DW_RLE_offset_pair)"));
13906 return false;
13907 }
13908
13909 range_beginning += *base;
13910 range_end += *base;
13911 }
13912
13913 /* A not-uncommon case of bad debug info.
13914 Don't pollute the addrmap with bad data. */
13915 if (range_beginning == 0
13916 && !per_objfile->per_bfd->has_section_at_zero)
13917 {
13918 complaint (_(".debug_rnglists entry has start address of zero"
13919 " [in module %s]"), objfile_name (objfile));
13920 continue;
13921 }
13922
13923 callback (range_beginning, range_end);
13924 }
13925
13926 if (overflow)
13927 {
13928 complaint (_("Offset %d is not terminated "
13929 "for DW_AT_ranges attribute"),
13930 offset);
13931 return false;
13932 }
13933
13934 return true;
13935 }
13936
13937 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13938 Callback's type should be:
13939 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13940 Return 1 if the attributes are present and valid, otherwise, return 0. */
13941
13942 template <typename Callback>
13943 static int
13944 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13945 Callback &&callback)
13946 {
13947 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13948 struct objfile *objfile = per_objfile->objfile;
13949 struct comp_unit_head *cu_header = &cu->header;
13950 bfd *obfd = objfile->obfd;
13951 unsigned int addr_size = cu_header->addr_size;
13952 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13953 /* Base address selection entry. */
13954 gdb::optional<CORE_ADDR> base;
13955 unsigned int dummy;
13956 const gdb_byte *buffer;
13957
13958 if (cu_header->version >= 5)
13959 return dwarf2_rnglists_process (offset, cu, tag, callback);
13960
13961 base = cu->base_address;
13962
13963 per_objfile->per_bfd->ranges.read (objfile);
13964 if (offset >= per_objfile->per_bfd->ranges.size)
13965 {
13966 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13967 offset);
13968 return 0;
13969 }
13970 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13971
13972 while (1)
13973 {
13974 CORE_ADDR range_beginning, range_end;
13975
13976 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13977 buffer += addr_size;
13978 range_end = cu->header.read_address (obfd, buffer, &dummy);
13979 buffer += addr_size;
13980 offset += 2 * addr_size;
13981
13982 /* An end of list marker is a pair of zero addresses. */
13983 if (range_beginning == 0 && range_end == 0)
13984 /* Found the end of list entry. */
13985 break;
13986
13987 /* Each base address selection entry is a pair of 2 values.
13988 The first is the largest possible address, the second is
13989 the base address. Check for a base address here. */
13990 if ((range_beginning & mask) == mask)
13991 {
13992 /* If we found the largest possible address, then we already
13993 have the base address in range_end. */
13994 base = range_end;
13995 continue;
13996 }
13997
13998 if (!base.has_value ())
13999 {
14000 /* We have no valid base address for the ranges
14001 data. */
14002 complaint (_("Invalid .debug_ranges data (no base address)"));
14003 return 0;
14004 }
14005
14006 if (range_beginning > range_end)
14007 {
14008 /* Inverted range entries are invalid. */
14009 complaint (_("Invalid .debug_ranges data (inverted range)"));
14010 return 0;
14011 }
14012
14013 /* Empty range entries have no effect. */
14014 if (range_beginning == range_end)
14015 continue;
14016
14017 range_beginning += *base;
14018 range_end += *base;
14019
14020 /* A not-uncommon case of bad debug info.
14021 Don't pollute the addrmap with bad data. */
14022 if (range_beginning == 0
14023 && !per_objfile->per_bfd->has_section_at_zero)
14024 {
14025 complaint (_(".debug_ranges entry has start address of zero"
14026 " [in module %s]"), objfile_name (objfile));
14027 continue;
14028 }
14029
14030 callback (range_beginning, range_end);
14031 }
14032
14033 return 1;
14034 }
14035
14036 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14037 Return 1 if the attributes are present and valid, otherwise, return 0.
14038 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14039
14040 static int
14041 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14042 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14043 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14044 {
14045 struct objfile *objfile = cu->per_objfile->objfile;
14046 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14047 struct gdbarch *gdbarch = objfile->arch ();
14048 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14049 int low_set = 0;
14050 CORE_ADDR low = 0;
14051 CORE_ADDR high = 0;
14052 int retval;
14053
14054 retval = dwarf2_ranges_process (offset, cu, tag,
14055 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14056 {
14057 if (ranges_pst != NULL)
14058 {
14059 CORE_ADDR lowpc;
14060 CORE_ADDR highpc;
14061
14062 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14063 range_beginning + baseaddr)
14064 - baseaddr);
14065 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14066 range_end + baseaddr)
14067 - baseaddr);
14068 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14069 lowpc, highpc - 1, ranges_pst);
14070 }
14071
14072 /* FIXME: This is recording everything as a low-high
14073 segment of consecutive addresses. We should have a
14074 data structure for discontiguous block ranges
14075 instead. */
14076 if (! low_set)
14077 {
14078 low = range_beginning;
14079 high = range_end;
14080 low_set = 1;
14081 }
14082 else
14083 {
14084 if (range_beginning < low)
14085 low = range_beginning;
14086 if (range_end > high)
14087 high = range_end;
14088 }
14089 });
14090 if (!retval)
14091 return 0;
14092
14093 if (! low_set)
14094 /* If the first entry is an end-of-list marker, the range
14095 describes an empty scope, i.e. no instructions. */
14096 return 0;
14097
14098 if (low_return)
14099 *low_return = low;
14100 if (high_return)
14101 *high_return = high;
14102 return 1;
14103 }
14104
14105 /* Process ranges and fill in a vector of the low PC values only. */
14106
14107 static void
14108 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
14109 dwarf_tag tag,
14110 std::vector<CORE_ADDR> &result)
14111 {
14112 dwarf2_ranges_process (offset, cu, tag,
14113 [&] (CORE_ADDR start, CORE_ADDR end)
14114 {
14115 result.push_back (start);
14116 });
14117 }
14118
14119 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14120 definition for the return value. *LOWPC and *HIGHPC are set iff
14121 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14122
14123 static enum pc_bounds_kind
14124 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14125 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14126 dwarf2_psymtab *pst)
14127 {
14128 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14129 struct attribute *attr;
14130 struct attribute *attr_high;
14131 CORE_ADDR low = 0;
14132 CORE_ADDR high = 0;
14133 enum pc_bounds_kind ret;
14134
14135 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14136 if (attr_high)
14137 {
14138 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14139 if (attr != nullptr)
14140 {
14141 low = attr->as_address ();
14142 high = attr_high->as_address ();
14143 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14144 high += low;
14145 }
14146 else
14147 /* Found high w/o low attribute. */
14148 return PC_BOUNDS_INVALID;
14149
14150 /* Found consecutive range of addresses. */
14151 ret = PC_BOUNDS_HIGH_LOW;
14152 }
14153 else
14154 {
14155 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14156 if (attr != nullptr && attr->form_is_unsigned ())
14157 {
14158 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14159 on DWARF version). */
14160 ULONGEST ranges_offset = attr->as_unsigned ();
14161
14162 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14163 this value. */
14164 if (die->tag != DW_TAG_compile_unit)
14165 ranges_offset += cu->gnu_ranges_base;
14166
14167 /* Value of the DW_AT_ranges attribute is the offset in the
14168 .debug_ranges section. */
14169 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14170 die->tag))
14171 return PC_BOUNDS_INVALID;
14172 /* Found discontinuous range of addresses. */
14173 ret = PC_BOUNDS_RANGES;
14174 }
14175 else
14176 return PC_BOUNDS_NOT_PRESENT;
14177 }
14178
14179 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14180 if (high <= low)
14181 return PC_BOUNDS_INVALID;
14182
14183 /* When using the GNU linker, .gnu.linkonce. sections are used to
14184 eliminate duplicate copies of functions and vtables and such.
14185 The linker will arbitrarily choose one and discard the others.
14186 The AT_*_pc values for such functions refer to local labels in
14187 these sections. If the section from that file was discarded, the
14188 labels are not in the output, so the relocs get a value of 0.
14189 If this is a discarded function, mark the pc bounds as invalid,
14190 so that GDB will ignore it. */
14191 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14192 return PC_BOUNDS_INVALID;
14193
14194 *lowpc = low;
14195 if (highpc)
14196 *highpc = high;
14197 return ret;
14198 }
14199
14200 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14201 its low and high PC addresses. Do nothing if these addresses could not
14202 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14203 and HIGHPC to the high address if greater than HIGHPC. */
14204
14205 static void
14206 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14207 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14208 struct dwarf2_cu *cu)
14209 {
14210 CORE_ADDR low, high;
14211 struct die_info *child = die->child;
14212
14213 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14214 {
14215 *lowpc = std::min (*lowpc, low);
14216 *highpc = std::max (*highpc, high);
14217 }
14218
14219 /* If the language does not allow nested subprograms (either inside
14220 subprograms or lexical blocks), we're done. */
14221 if (cu->per_cu->lang != language_ada)
14222 return;
14223
14224 /* Check all the children of the given DIE. If it contains nested
14225 subprograms, then check their pc bounds. Likewise, we need to
14226 check lexical blocks as well, as they may also contain subprogram
14227 definitions. */
14228 while (child && child->tag)
14229 {
14230 if (child->tag == DW_TAG_subprogram
14231 || child->tag == DW_TAG_lexical_block)
14232 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14233 child = child->sibling;
14234 }
14235 }
14236
14237 /* Get the low and high pc's represented by the scope DIE, and store
14238 them in *LOWPC and *HIGHPC. If the correct values can't be
14239 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14240
14241 static void
14242 get_scope_pc_bounds (struct die_info *die,
14243 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14244 struct dwarf2_cu *cu)
14245 {
14246 CORE_ADDR best_low = (CORE_ADDR) -1;
14247 CORE_ADDR best_high = (CORE_ADDR) 0;
14248 CORE_ADDR current_low, current_high;
14249
14250 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14251 >= PC_BOUNDS_RANGES)
14252 {
14253 best_low = current_low;
14254 best_high = current_high;
14255 }
14256 else
14257 {
14258 struct die_info *child = die->child;
14259
14260 while (child && child->tag)
14261 {
14262 switch (child->tag) {
14263 case DW_TAG_subprogram:
14264 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14265 break;
14266 case DW_TAG_namespace:
14267 case DW_TAG_module:
14268 /* FIXME: carlton/2004-01-16: Should we do this for
14269 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14270 that current GCC's always emit the DIEs corresponding
14271 to definitions of methods of classes as children of a
14272 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14273 the DIEs giving the declarations, which could be
14274 anywhere). But I don't see any reason why the
14275 standards says that they have to be there. */
14276 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14277
14278 if (current_low != ((CORE_ADDR) -1))
14279 {
14280 best_low = std::min (best_low, current_low);
14281 best_high = std::max (best_high, current_high);
14282 }
14283 break;
14284 default:
14285 /* Ignore. */
14286 break;
14287 }
14288
14289 child = child->sibling;
14290 }
14291 }
14292
14293 *lowpc = best_low;
14294 *highpc = best_high;
14295 }
14296
14297 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14298 in DIE. */
14299
14300 static void
14301 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14302 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14303 {
14304 struct objfile *objfile = cu->per_objfile->objfile;
14305 struct gdbarch *gdbarch = objfile->arch ();
14306 struct attribute *attr;
14307 struct attribute *attr_high;
14308
14309 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14310 if (attr_high)
14311 {
14312 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14313 if (attr != nullptr)
14314 {
14315 CORE_ADDR low = attr->as_address ();
14316 CORE_ADDR high = attr_high->as_address ();
14317
14318 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14319 high += low;
14320
14321 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14322 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14323 cu->get_builder ()->record_block_range (block, low, high - 1);
14324 }
14325 }
14326
14327 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14328 if (attr != nullptr && attr->form_is_unsigned ())
14329 {
14330 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14331 on DWARF version). */
14332 ULONGEST ranges_offset = attr->as_unsigned ();
14333
14334 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14335 this value. */
14336 if (die->tag != DW_TAG_compile_unit)
14337 ranges_offset += cu->gnu_ranges_base;
14338
14339 std::vector<blockrange> blockvec;
14340 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14341 [&] (CORE_ADDR start, CORE_ADDR end)
14342 {
14343 start += baseaddr;
14344 end += baseaddr;
14345 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14346 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14347 cu->get_builder ()->record_block_range (block, start, end - 1);
14348 blockvec.emplace_back (start, end);
14349 });
14350
14351 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14352 }
14353 }
14354
14355 /* Check whether the producer field indicates either of GCC < 4.6, or the
14356 Intel C/C++ compiler, and cache the result in CU. */
14357
14358 static void
14359 check_producer (struct dwarf2_cu *cu)
14360 {
14361 int major, minor;
14362
14363 if (cu->producer == NULL)
14364 {
14365 /* For unknown compilers expect their behavior is DWARF version
14366 compliant.
14367
14368 GCC started to support .debug_types sections by -gdwarf-4 since
14369 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14370 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14371 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14372 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14373 }
14374 else if (producer_is_gcc (cu->producer, &major, &minor))
14375 {
14376 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14377 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14378 cu->producer_is_gcc_11 = major == 11;
14379 }
14380 else if (producer_is_icc (cu->producer, &major, &minor))
14381 {
14382 cu->producer_is_icc = true;
14383 cu->producer_is_icc_lt_14 = major < 14;
14384 }
14385 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14386 cu->producer_is_codewarrior = true;
14387 else
14388 {
14389 /* For other non-GCC compilers, expect their behavior is DWARF version
14390 compliant. */
14391 }
14392
14393 cu->checked_producer = true;
14394 }
14395
14396 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14397 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14398 during 4.6.0 experimental. */
14399
14400 static bool
14401 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14402 {
14403 if (!cu->checked_producer)
14404 check_producer (cu);
14405
14406 return cu->producer_is_gxx_lt_4_6;
14407 }
14408
14409
14410 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14411 with incorrect is_stmt attributes. */
14412
14413 static bool
14414 producer_is_codewarrior (struct dwarf2_cu *cu)
14415 {
14416 if (!cu->checked_producer)
14417 check_producer (cu);
14418
14419 return cu->producer_is_codewarrior;
14420 }
14421
14422 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14423 If that attribute is not available, return the appropriate
14424 default. */
14425
14426 static enum dwarf_access_attribute
14427 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14428 {
14429 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14430 if (attr != nullptr)
14431 {
14432 LONGEST value = attr->constant_value (-1);
14433 if (value == DW_ACCESS_public
14434 || value == DW_ACCESS_protected
14435 || value == DW_ACCESS_private)
14436 return (dwarf_access_attribute) value;
14437 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14438 plongest (value));
14439 }
14440
14441 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14442 {
14443 /* The default DWARF 2 accessibility for members is public, the default
14444 accessibility for inheritance is private. */
14445
14446 if (die->tag != DW_TAG_inheritance)
14447 return DW_ACCESS_public;
14448 else
14449 return DW_ACCESS_private;
14450 }
14451 else
14452 {
14453 /* DWARF 3+ defines the default accessibility a different way. The same
14454 rules apply now for DW_TAG_inheritance as for the members and it only
14455 depends on the container kind. */
14456
14457 if (die->parent->tag == DW_TAG_class_type)
14458 return DW_ACCESS_private;
14459 else
14460 return DW_ACCESS_public;
14461 }
14462 }
14463
14464 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
14465 *OFFSET to the byte offset. If the attribute was not found return
14466 0, otherwise return 1. If it was found but could not properly be
14467 handled, set *OFFSET to 0. */
14468
14469 static int
14470 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14471 LONGEST *offset)
14472 {
14473 struct attribute *attr;
14474
14475 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14476 if (attr != NULL)
14477 {
14478 *offset = 0;
14479
14480 /* Note that we do not check for a section offset first here.
14481 This is because DW_AT_data_member_location is new in DWARF 4,
14482 so if we see it, we can assume that a constant form is really
14483 a constant and not a section offset. */
14484 if (attr->form_is_constant ())
14485 *offset = attr->constant_value (0);
14486 else if (attr->form_is_section_offset ())
14487 dwarf2_complex_location_expr_complaint ();
14488 else if (attr->form_is_block ())
14489 *offset = decode_locdesc (attr->as_block (), cu);
14490 else
14491 dwarf2_complex_location_expr_complaint ();
14492
14493 return 1;
14494 }
14495 else
14496 {
14497 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14498 if (attr != nullptr)
14499 {
14500 *offset = attr->constant_value (0);
14501 return 1;
14502 }
14503 }
14504
14505 return 0;
14506 }
14507
14508 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
14509 store the results in FIELD. */
14510
14511 static void
14512 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14513 struct field *field)
14514 {
14515 struct attribute *attr;
14516
14517 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14518 if (attr != NULL)
14519 {
14520 if (attr->form_is_constant ())
14521 {
14522 LONGEST offset = attr->constant_value (0);
14523
14524 /* Work around this GCC 11 bug, where it would erroneously use -1
14525 data member locations, instead of 0:
14526
14527 Negative DW_AT_data_member_location
14528 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
14529 */
14530 if (offset == -1 && cu->producer_is_gcc_11)
14531 {
14532 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
14533 offset = 0;
14534 }
14535
14536 field->set_loc_bitpos (offset * bits_per_byte);
14537 }
14538 else if (attr->form_is_section_offset ())
14539 dwarf2_complex_location_expr_complaint ();
14540 else if (attr->form_is_block ())
14541 {
14542 bool handled;
14543 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14544 if (handled)
14545 field->set_loc_bitpos (offset * bits_per_byte);
14546 else
14547 {
14548 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14549 struct objfile *objfile = per_objfile->objfile;
14550 struct dwarf2_locexpr_baton *dlbaton
14551 = XOBNEW (&objfile->objfile_obstack,
14552 struct dwarf2_locexpr_baton);
14553 dlbaton->data = attr->as_block ()->data;
14554 dlbaton->size = attr->as_block ()->size;
14555 /* When using this baton, we want to compute the address
14556 of the field, not the value. This is why
14557 is_reference is set to false here. */
14558 dlbaton->is_reference = false;
14559 dlbaton->per_objfile = per_objfile;
14560 dlbaton->per_cu = cu->per_cu;
14561
14562 field->set_loc_dwarf_block (dlbaton);
14563 }
14564 }
14565 else
14566 dwarf2_complex_location_expr_complaint ();
14567 }
14568 else
14569 {
14570 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14571 if (attr != nullptr)
14572 field->set_loc_bitpos (attr->constant_value (0));
14573 }
14574 }
14575
14576 /* Add an aggregate field to the field list. */
14577
14578 static void
14579 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14580 struct dwarf2_cu *cu)
14581 {
14582 struct objfile *objfile = cu->per_objfile->objfile;
14583 struct gdbarch *gdbarch = objfile->arch ();
14584 struct nextfield *new_field;
14585 struct attribute *attr;
14586 struct field *fp;
14587 const char *fieldname = "";
14588
14589 if (die->tag == DW_TAG_inheritance)
14590 {
14591 fip->baseclasses.emplace_back ();
14592 new_field = &fip->baseclasses.back ();
14593 }
14594 else
14595 {
14596 fip->fields.emplace_back ();
14597 new_field = &fip->fields.back ();
14598 }
14599
14600 new_field->offset = die->sect_off;
14601
14602 new_field->accessibility = dwarf2_access_attribute (die, cu);
14603 if (new_field->accessibility != DW_ACCESS_public)
14604 fip->non_public_fields = true;
14605
14606 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14607 if (attr != nullptr)
14608 new_field->virtuality = attr->as_virtuality ();
14609 else
14610 new_field->virtuality = DW_VIRTUALITY_none;
14611
14612 fp = &new_field->field;
14613
14614 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
14615 && !die_is_declaration (die, cu))
14616 {
14617 if (die->tag == DW_TAG_namelist_item)
14618 {
14619 /* Typically, DW_TAG_namelist_item are references to namelist items.
14620 If so, follow that reference. */
14621 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
14622 struct die_info *item_die = nullptr;
14623 struct dwarf2_cu *item_cu = cu;
14624 if (attr1->form_is_ref ())
14625 item_die = follow_die_ref (die, attr1, &item_cu);
14626 if (item_die != nullptr)
14627 die = item_die;
14628 }
14629 /* Data member other than a C++ static data member. */
14630
14631 /* Get type of field. */
14632 fp->set_type (die_type (die, cu));
14633
14634 fp->set_loc_bitpos (0);
14635
14636 /* Get bit size of field (zero if none). */
14637 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14638 if (attr != nullptr)
14639 {
14640 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14641 }
14642 else
14643 {
14644 FIELD_BITSIZE (*fp) = 0;
14645 }
14646
14647 /* Get bit offset of field. */
14648 handle_member_location (die, cu, fp);
14649 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14650 if (attr != nullptr && attr->form_is_constant ())
14651 {
14652 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14653 {
14654 /* For big endian bits, the DW_AT_bit_offset gives the
14655 additional bit offset from the MSB of the containing
14656 anonymous object to the MSB of the field. We don't
14657 have to do anything special since we don't need to
14658 know the size of the anonymous object. */
14659 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
14660 }
14661 else
14662 {
14663 /* For little endian bits, compute the bit offset to the
14664 MSB of the anonymous object, subtract off the number of
14665 bits from the MSB of the field to the MSB of the
14666 object, and then subtract off the number of bits of
14667 the field itself. The result is the bit offset of
14668 the LSB of the field. */
14669 int anonymous_size;
14670 int bit_offset = attr->constant_value (0);
14671
14672 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14673 if (attr != nullptr && attr->form_is_constant ())
14674 {
14675 /* The size of the anonymous object containing
14676 the bit field is explicit, so use the
14677 indicated size (in bytes). */
14678 anonymous_size = attr->constant_value (0);
14679 }
14680 else
14681 {
14682 /* The size of the anonymous object containing
14683 the bit field must be inferred from the type
14684 attribute of the data member containing the
14685 bit field. */
14686 anonymous_size = TYPE_LENGTH (fp->type ());
14687 }
14688 fp->set_loc_bitpos (fp->loc_bitpos ()
14689 + anonymous_size * bits_per_byte
14690 - bit_offset - FIELD_BITSIZE (*fp));
14691 }
14692 }
14693
14694 /* Get name of field. */
14695 fieldname = dwarf2_name (die, cu);
14696 if (fieldname == NULL)
14697 fieldname = "";
14698
14699 /* The name is already allocated along with this objfile, so we don't
14700 need to duplicate it for the type. */
14701 fp->set_name (fieldname);
14702
14703 /* Change accessibility for artificial fields (e.g. virtual table
14704 pointer or virtual base class pointer) to private. */
14705 if (dwarf2_attr (die, DW_AT_artificial, cu))
14706 {
14707 FIELD_ARTIFICIAL (*fp) = 1;
14708 new_field->accessibility = DW_ACCESS_private;
14709 fip->non_public_fields = true;
14710 }
14711 }
14712 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14713 {
14714 /* C++ static member. */
14715
14716 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14717 is a declaration, but all versions of G++ as of this writing
14718 (so through at least 3.2.1) incorrectly generate
14719 DW_TAG_variable tags. */
14720
14721 const char *physname;
14722
14723 /* Get name of field. */
14724 fieldname = dwarf2_name (die, cu);
14725 if (fieldname == NULL)
14726 return;
14727
14728 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14729 if (attr
14730 /* Only create a symbol if this is an external value.
14731 new_symbol checks this and puts the value in the global symbol
14732 table, which we want. If it is not external, new_symbol
14733 will try to put the value in cu->list_in_scope which is wrong. */
14734 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14735 {
14736 /* A static const member, not much different than an enum as far as
14737 we're concerned, except that we can support more types. */
14738 new_symbol (die, NULL, cu);
14739 }
14740
14741 /* Get physical name. */
14742 physname = dwarf2_physname (fieldname, die, cu);
14743
14744 /* The name is already allocated along with this objfile, so we don't
14745 need to duplicate it for the type. */
14746 fp->set_loc_physname (physname ? physname : "");
14747 fp->set_type (die_type (die, cu));
14748 fp->set_name (fieldname);
14749 }
14750 else if (die->tag == DW_TAG_inheritance)
14751 {
14752 /* C++ base class field. */
14753 handle_member_location (die, cu, fp);
14754 FIELD_BITSIZE (*fp) = 0;
14755 fp->set_type (die_type (die, cu));
14756 fp->set_name (fp->type ()->name ());
14757 }
14758 else
14759 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14760 }
14761
14762 /* Can the type given by DIE define another type? */
14763
14764 static bool
14765 type_can_define_types (const struct die_info *die)
14766 {
14767 switch (die->tag)
14768 {
14769 case DW_TAG_typedef:
14770 case DW_TAG_class_type:
14771 case DW_TAG_structure_type:
14772 case DW_TAG_union_type:
14773 case DW_TAG_enumeration_type:
14774 return true;
14775
14776 default:
14777 return false;
14778 }
14779 }
14780
14781 /* Add a type definition defined in the scope of the FIP's class. */
14782
14783 static void
14784 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14785 struct dwarf2_cu *cu)
14786 {
14787 struct decl_field fp;
14788 memset (&fp, 0, sizeof (fp));
14789
14790 gdb_assert (type_can_define_types (die));
14791
14792 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14793 fp.name = dwarf2_name (die, cu);
14794 fp.type = read_type_die (die, cu);
14795
14796 /* Save accessibility. */
14797 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14798 switch (accessibility)
14799 {
14800 case DW_ACCESS_public:
14801 /* The assumed value if neither private nor protected. */
14802 break;
14803 case DW_ACCESS_private:
14804 fp.is_private = 1;
14805 break;
14806 case DW_ACCESS_protected:
14807 fp.is_protected = 1;
14808 break;
14809 }
14810
14811 if (die->tag == DW_TAG_typedef)
14812 fip->typedef_field_list.push_back (fp);
14813 else
14814 fip->nested_types_list.push_back (fp);
14815 }
14816
14817 /* A convenience typedef that's used when finding the discriminant
14818 field for a variant part. */
14819 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14820 offset_map_type;
14821
14822 /* Compute the discriminant range for a given variant. OBSTACK is
14823 where the results will be stored. VARIANT is the variant to
14824 process. IS_UNSIGNED indicates whether the discriminant is signed
14825 or unsigned. */
14826
14827 static const gdb::array_view<discriminant_range>
14828 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14829 bool is_unsigned)
14830 {
14831 std::vector<discriminant_range> ranges;
14832
14833 if (variant.default_branch)
14834 return {};
14835
14836 if (variant.discr_list_data == nullptr)
14837 {
14838 discriminant_range r
14839 = {variant.discriminant_value, variant.discriminant_value};
14840 ranges.push_back (r);
14841 }
14842 else
14843 {
14844 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14845 variant.discr_list_data->size);
14846 while (!data.empty ())
14847 {
14848 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14849 {
14850 complaint (_("invalid discriminant marker: %d"), data[0]);
14851 break;
14852 }
14853 bool is_range = data[0] == DW_DSC_range;
14854 data = data.slice (1);
14855
14856 ULONGEST low, high;
14857 unsigned int bytes_read;
14858
14859 if (data.empty ())
14860 {
14861 complaint (_("DW_AT_discr_list missing low value"));
14862 break;
14863 }
14864 if (is_unsigned)
14865 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14866 else
14867 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14868 &bytes_read);
14869 data = data.slice (bytes_read);
14870
14871 if (is_range)
14872 {
14873 if (data.empty ())
14874 {
14875 complaint (_("DW_AT_discr_list missing high value"));
14876 break;
14877 }
14878 if (is_unsigned)
14879 high = read_unsigned_leb128 (nullptr, data.data (),
14880 &bytes_read);
14881 else
14882 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14883 &bytes_read);
14884 data = data.slice (bytes_read);
14885 }
14886 else
14887 high = low;
14888
14889 ranges.push_back ({ low, high });
14890 }
14891 }
14892
14893 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14894 ranges.size ());
14895 std::copy (ranges.begin (), ranges.end (), result);
14896 return gdb::array_view<discriminant_range> (result, ranges.size ());
14897 }
14898
14899 static const gdb::array_view<variant_part> create_variant_parts
14900 (struct obstack *obstack,
14901 const offset_map_type &offset_map,
14902 struct field_info *fi,
14903 const std::vector<variant_part_builder> &variant_parts);
14904
14905 /* Fill in a "struct variant" for a given variant field. RESULT is
14906 the variant to fill in. OBSTACK is where any needed allocations
14907 will be done. OFFSET_MAP holds the mapping from section offsets to
14908 fields for the type. FI describes the fields of the type we're
14909 processing. FIELD is the variant field we're converting. */
14910
14911 static void
14912 create_one_variant (variant &result, struct obstack *obstack,
14913 const offset_map_type &offset_map,
14914 struct field_info *fi, const variant_field &field)
14915 {
14916 result.discriminants = convert_variant_range (obstack, field, false);
14917 result.first_field = field.first_field + fi->baseclasses.size ();
14918 result.last_field = field.last_field + fi->baseclasses.size ();
14919 result.parts = create_variant_parts (obstack, offset_map, fi,
14920 field.variant_parts);
14921 }
14922
14923 /* Fill in a "struct variant_part" for a given variant part. RESULT
14924 is the variant part to fill in. OBSTACK is where any needed
14925 allocations will be done. OFFSET_MAP holds the mapping from
14926 section offsets to fields for the type. FI describes the fields of
14927 the type we're processing. BUILDER is the variant part to be
14928 converted. */
14929
14930 static void
14931 create_one_variant_part (variant_part &result,
14932 struct obstack *obstack,
14933 const offset_map_type &offset_map,
14934 struct field_info *fi,
14935 const variant_part_builder &builder)
14936 {
14937 auto iter = offset_map.find (builder.discriminant_offset);
14938 if (iter == offset_map.end ())
14939 {
14940 result.discriminant_index = -1;
14941 /* Doesn't matter. */
14942 result.is_unsigned = false;
14943 }
14944 else
14945 {
14946 result.discriminant_index = iter->second;
14947 result.is_unsigned
14948 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14949 }
14950
14951 size_t n = builder.variants.size ();
14952 variant *output = new (obstack) variant[n];
14953 for (size_t i = 0; i < n; ++i)
14954 create_one_variant (output[i], obstack, offset_map, fi,
14955 builder.variants[i]);
14956
14957 result.variants = gdb::array_view<variant> (output, n);
14958 }
14959
14960 /* Create a vector of variant parts that can be attached to a type.
14961 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14962 holds the mapping from section offsets to fields for the type. FI
14963 describes the fields of the type we're processing. VARIANT_PARTS
14964 is the vector to convert. */
14965
14966 static const gdb::array_view<variant_part>
14967 create_variant_parts (struct obstack *obstack,
14968 const offset_map_type &offset_map,
14969 struct field_info *fi,
14970 const std::vector<variant_part_builder> &variant_parts)
14971 {
14972 if (variant_parts.empty ())
14973 return {};
14974
14975 size_t n = variant_parts.size ();
14976 variant_part *result = new (obstack) variant_part[n];
14977 for (size_t i = 0; i < n; ++i)
14978 create_one_variant_part (result[i], obstack, offset_map, fi,
14979 variant_parts[i]);
14980
14981 return gdb::array_view<variant_part> (result, n);
14982 }
14983
14984 /* Compute the variant part vector for FIP, attaching it to TYPE when
14985 done. */
14986
14987 static void
14988 add_variant_property (struct field_info *fip, struct type *type,
14989 struct dwarf2_cu *cu)
14990 {
14991 /* Map section offsets of fields to their field index. Note the
14992 field index here does not take the number of baseclasses into
14993 account. */
14994 offset_map_type offset_map;
14995 for (int i = 0; i < fip->fields.size (); ++i)
14996 offset_map[fip->fields[i].offset] = i;
14997
14998 struct objfile *objfile = cu->per_objfile->objfile;
14999 gdb::array_view<const variant_part> parts
15000 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15001 fip->variant_parts);
15002
15003 struct dynamic_prop prop;
15004 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15005 obstack_copy (&objfile->objfile_obstack, &parts,
15006 sizeof (parts)));
15007
15008 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15009 }
15010
15011 /* Create the vector of fields, and attach it to the type. */
15012
15013 static void
15014 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15015 struct dwarf2_cu *cu)
15016 {
15017 int nfields = fip->nfields ();
15018
15019 /* Record the field count, allocate space for the array of fields,
15020 and create blank accessibility bitfields if necessary. */
15021 type->set_num_fields (nfields);
15022 type->set_fields
15023 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15024
15025 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
15026 {
15027 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15028
15029 TYPE_FIELD_PRIVATE_BITS (type) =
15030 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15031 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15032
15033 TYPE_FIELD_PROTECTED_BITS (type) =
15034 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15035 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15036
15037 TYPE_FIELD_IGNORE_BITS (type) =
15038 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15039 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15040 }
15041
15042 /* If the type has baseclasses, allocate and clear a bit vector for
15043 TYPE_FIELD_VIRTUAL_BITS. */
15044 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
15045 {
15046 int num_bytes = B_BYTES (fip->baseclasses.size ());
15047 unsigned char *pointer;
15048
15049 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15050 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15051 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15052 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15053 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15054 }
15055
15056 if (!fip->variant_parts.empty ())
15057 add_variant_property (fip, type, cu);
15058
15059 /* Copy the saved-up fields into the field vector. */
15060 for (int i = 0; i < nfields; ++i)
15061 {
15062 struct nextfield &field
15063 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15064 : fip->fields[i - fip->baseclasses.size ()]);
15065
15066 type->field (i) = field.field;
15067 switch (field.accessibility)
15068 {
15069 case DW_ACCESS_private:
15070 if (cu->per_cu->lang != language_ada)
15071 SET_TYPE_FIELD_PRIVATE (type, i);
15072 break;
15073
15074 case DW_ACCESS_protected:
15075 if (cu->per_cu->lang != language_ada)
15076 SET_TYPE_FIELD_PROTECTED (type, i);
15077 break;
15078
15079 case DW_ACCESS_public:
15080 break;
15081
15082 default:
15083 /* Unknown accessibility. Complain and treat it as public. */
15084 {
15085 complaint (_("unsupported accessibility %d"),
15086 field.accessibility);
15087 }
15088 break;
15089 }
15090 if (i < fip->baseclasses.size ())
15091 {
15092 switch (field.virtuality)
15093 {
15094 case DW_VIRTUALITY_virtual:
15095 case DW_VIRTUALITY_pure_virtual:
15096 if (cu->per_cu->lang == language_ada)
15097 error (_("unexpected virtuality in component of Ada type"));
15098 SET_TYPE_FIELD_VIRTUAL (type, i);
15099 break;
15100 }
15101 }
15102 }
15103 }
15104
15105 /* Return true if this member function is a constructor, false
15106 otherwise. */
15107
15108 static int
15109 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15110 {
15111 const char *fieldname;
15112 const char *type_name;
15113 int len;
15114
15115 if (die->parent == NULL)
15116 return 0;
15117
15118 if (die->parent->tag != DW_TAG_structure_type
15119 && die->parent->tag != DW_TAG_union_type
15120 && die->parent->tag != DW_TAG_class_type)
15121 return 0;
15122
15123 fieldname = dwarf2_name (die, cu);
15124 type_name = dwarf2_name (die->parent, cu);
15125 if (fieldname == NULL || type_name == NULL)
15126 return 0;
15127
15128 len = strlen (fieldname);
15129 return (strncmp (fieldname, type_name, len) == 0
15130 && (type_name[len] == '\0' || type_name[len] == '<'));
15131 }
15132
15133 /* Add a member function to the proper fieldlist. */
15134
15135 static void
15136 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15137 struct type *type, struct dwarf2_cu *cu)
15138 {
15139 struct objfile *objfile = cu->per_objfile->objfile;
15140 struct attribute *attr;
15141 int i;
15142 struct fnfieldlist *flp = nullptr;
15143 struct fn_field *fnp;
15144 const char *fieldname;
15145 struct type *this_type;
15146
15147 if (cu->per_cu->lang == language_ada)
15148 error (_("unexpected member function in Ada type"));
15149
15150 /* Get name of member function. */
15151 fieldname = dwarf2_name (die, cu);
15152 if (fieldname == NULL)
15153 return;
15154
15155 /* Look up member function name in fieldlist. */
15156 for (i = 0; i < fip->fnfieldlists.size (); i++)
15157 {
15158 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15159 {
15160 flp = &fip->fnfieldlists[i];
15161 break;
15162 }
15163 }
15164
15165 /* Create a new fnfieldlist if necessary. */
15166 if (flp == nullptr)
15167 {
15168 fip->fnfieldlists.emplace_back ();
15169 flp = &fip->fnfieldlists.back ();
15170 flp->name = fieldname;
15171 i = fip->fnfieldlists.size () - 1;
15172 }
15173
15174 /* Create a new member function field and add it to the vector of
15175 fnfieldlists. */
15176 flp->fnfields.emplace_back ();
15177 fnp = &flp->fnfields.back ();
15178
15179 /* Delay processing of the physname until later. */
15180 if (cu->per_cu->lang == language_cplus)
15181 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15182 die, cu);
15183 else
15184 {
15185 const char *physname = dwarf2_physname (fieldname, die, cu);
15186 fnp->physname = physname ? physname : "";
15187 }
15188
15189 fnp->type = alloc_type (objfile);
15190 this_type = read_type_die (die, cu);
15191 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15192 {
15193 int nparams = this_type->num_fields ();
15194
15195 /* TYPE is the domain of this method, and THIS_TYPE is the type
15196 of the method itself (TYPE_CODE_METHOD). */
15197 smash_to_method_type (fnp->type, type,
15198 TYPE_TARGET_TYPE (this_type),
15199 this_type->fields (),
15200 this_type->num_fields (),
15201 this_type->has_varargs ());
15202
15203 /* Handle static member functions.
15204 Dwarf2 has no clean way to discern C++ static and non-static
15205 member functions. G++ helps GDB by marking the first
15206 parameter for non-static member functions (which is the this
15207 pointer) as artificial. We obtain this information from
15208 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15209 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15210 fnp->voffset = VOFFSET_STATIC;
15211 }
15212 else
15213 complaint (_("member function type missing for '%s'"),
15214 dwarf2_full_name (fieldname, die, cu));
15215
15216 /* Get fcontext from DW_AT_containing_type if present. */
15217 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15218 fnp->fcontext = die_containing_type (die, cu);
15219
15220 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15221 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15222
15223 /* Get accessibility. */
15224 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15225 switch (accessibility)
15226 {
15227 case DW_ACCESS_private:
15228 fnp->is_private = 1;
15229 break;
15230 case DW_ACCESS_protected:
15231 fnp->is_protected = 1;
15232 break;
15233 }
15234
15235 /* Check for artificial methods. */
15236 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15237 if (attr && attr->as_boolean ())
15238 fnp->is_artificial = 1;
15239
15240 /* Check for defaulted methods. */
15241 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15242 if (attr != nullptr)
15243 fnp->defaulted = attr->defaulted ();
15244
15245 /* Check for deleted methods. */
15246 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15247 if (attr != nullptr && attr->as_boolean ())
15248 fnp->is_deleted = 1;
15249
15250 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15251
15252 /* Get index in virtual function table if it is a virtual member
15253 function. For older versions of GCC, this is an offset in the
15254 appropriate virtual table, as specified by DW_AT_containing_type.
15255 For everyone else, it is an expression to be evaluated relative
15256 to the object address. */
15257
15258 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15259 if (attr != nullptr)
15260 {
15261 if (attr->form_is_block () && attr->as_block ()->size > 0)
15262 {
15263 struct dwarf_block *block = attr->as_block ();
15264
15265 if (block->data[0] == DW_OP_constu)
15266 {
15267 /* Old-style GCC. */
15268 fnp->voffset = decode_locdesc (block, cu) + 2;
15269 }
15270 else if (block->data[0] == DW_OP_deref
15271 || (block->size > 1
15272 && block->data[0] == DW_OP_deref_size
15273 && block->data[1] == cu->header.addr_size))
15274 {
15275 fnp->voffset = decode_locdesc (block, cu);
15276 if ((fnp->voffset % cu->header.addr_size) != 0)
15277 dwarf2_complex_location_expr_complaint ();
15278 else
15279 fnp->voffset /= cu->header.addr_size;
15280 fnp->voffset += 2;
15281 }
15282 else
15283 dwarf2_complex_location_expr_complaint ();
15284
15285 if (!fnp->fcontext)
15286 {
15287 /* If there is no `this' field and no DW_AT_containing_type,
15288 we cannot actually find a base class context for the
15289 vtable! */
15290 if (this_type->num_fields () == 0
15291 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15292 {
15293 complaint (_("cannot determine context for virtual member "
15294 "function \"%s\" (offset %s)"),
15295 fieldname, sect_offset_str (die->sect_off));
15296 }
15297 else
15298 {
15299 fnp->fcontext
15300 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15301 }
15302 }
15303 }
15304 else if (attr->form_is_section_offset ())
15305 {
15306 dwarf2_complex_location_expr_complaint ();
15307 }
15308 else
15309 {
15310 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15311 fieldname);
15312 }
15313 }
15314 else
15315 {
15316 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15317 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15318 {
15319 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15320 complaint (_("Member function \"%s\" (offset %s) is virtual "
15321 "but the vtable offset is not specified"),
15322 fieldname, sect_offset_str (die->sect_off));
15323 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15324 TYPE_CPLUS_DYNAMIC (type) = 1;
15325 }
15326 }
15327 }
15328
15329 /* Create the vector of member function fields, and attach it to the type. */
15330
15331 static void
15332 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15333 struct dwarf2_cu *cu)
15334 {
15335 if (cu->per_cu->lang == language_ada)
15336 error (_("unexpected member functions in Ada type"));
15337
15338 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15339 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15340 TYPE_ALLOC (type,
15341 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15342
15343 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15344 {
15345 struct fnfieldlist &nf = fip->fnfieldlists[i];
15346 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15347
15348 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15349 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15350 fn_flp->fn_fields = (struct fn_field *)
15351 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15352
15353 for (int k = 0; k < nf.fnfields.size (); ++k)
15354 fn_flp->fn_fields[k] = nf.fnfields[k];
15355 }
15356
15357 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15358 }
15359
15360 /* Returns non-zero if NAME is the name of a vtable member in CU's
15361 language, zero otherwise. */
15362 static int
15363 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15364 {
15365 static const char vptr[] = "_vptr";
15366
15367 /* Look for the C++ form of the vtable. */
15368 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15369 return 1;
15370
15371 return 0;
15372 }
15373
15374 /* GCC outputs unnamed structures that are really pointers to member
15375 functions, with the ABI-specified layout. If TYPE describes
15376 such a structure, smash it into a member function type.
15377
15378 GCC shouldn't do this; it should just output pointer to member DIEs.
15379 This is GCC PR debug/28767. */
15380
15381 static void
15382 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15383 {
15384 struct type *pfn_type, *self_type, *new_type;
15385
15386 /* Check for a structure with no name and two children. */
15387 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15388 return;
15389
15390 /* Check for __pfn and __delta members. */
15391 if (type->field (0).name () == NULL
15392 || strcmp (type->field (0).name (), "__pfn") != 0
15393 || type->field (1).name () == NULL
15394 || strcmp (type->field (1).name (), "__delta") != 0)
15395 return;
15396
15397 /* Find the type of the method. */
15398 pfn_type = type->field (0).type ();
15399 if (pfn_type == NULL
15400 || pfn_type->code () != TYPE_CODE_PTR
15401 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15402 return;
15403
15404 /* Look for the "this" argument. */
15405 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15406 if (pfn_type->num_fields () == 0
15407 /* || pfn_type->field (0).type () == NULL */
15408 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15409 return;
15410
15411 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15412 new_type = alloc_type (objfile);
15413 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15414 pfn_type->fields (), pfn_type->num_fields (),
15415 pfn_type->has_varargs ());
15416 smash_to_methodptr_type (type, new_type);
15417 }
15418
15419 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15420 requires rewriting, then copy it and return the updated copy.
15421 Otherwise return nullptr. */
15422
15423 static struct type *
15424 rewrite_array_type (struct type *type)
15425 {
15426 if (type->code () != TYPE_CODE_ARRAY)
15427 return nullptr;
15428
15429 struct type *index_type = type->index_type ();
15430 range_bounds *current_bounds = index_type->bounds ();
15431
15432 /* Handle multi-dimensional arrays. */
15433 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15434 if (new_target == nullptr)
15435 {
15436 /* Maybe we don't need to rewrite this array. */
15437 if (current_bounds->low.kind () == PROP_CONST
15438 && current_bounds->high.kind () == PROP_CONST)
15439 return nullptr;
15440 }
15441
15442 /* Either the target type was rewritten, or the bounds have to be
15443 updated. Either way we want to copy the type and update
15444 everything. */
15445 struct type *copy = copy_type (type);
15446 int nfields = copy->num_fields ();
15447 field *new_fields
15448 = ((struct field *) TYPE_ZALLOC (copy,
15449 nfields * sizeof (struct field)));
15450 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15451 copy->set_fields (new_fields);
15452 if (new_target != nullptr)
15453 TYPE_TARGET_TYPE (copy) = new_target;
15454
15455 struct type *index_copy = copy_type (index_type);
15456 range_bounds *bounds
15457 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15458 sizeof (range_bounds));
15459 *bounds = *current_bounds;
15460 bounds->low.set_const_val (1);
15461 bounds->high.set_const_val (0);
15462 index_copy->set_bounds (bounds);
15463 copy->set_index_type (index_copy);
15464
15465 return copy;
15466 }
15467
15468 /* While some versions of GCC will generate complicated DWARF for an
15469 array (see quirk_ada_thick_pointer), more recent versions were
15470 modified to emit an explicit thick pointer structure. However, in
15471 this case, the array still has DWARF expressions for its ranges,
15472 and these must be ignored. */
15473
15474 static void
15475 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15476 struct type *type)
15477 {
15478 gdb_assert (cu->per_cu->lang == language_ada);
15479
15480 /* Check for a structure with two children. */
15481 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15482 return;
15483
15484 /* Check for P_ARRAY and P_BOUNDS members. */
15485 if (type->field (0).name () == NULL
15486 || strcmp (type->field (0).name (), "P_ARRAY") != 0
15487 || type->field (1).name () == NULL
15488 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
15489 return;
15490
15491 /* Make sure we're looking at a pointer to an array. */
15492 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15493 return;
15494
15495 /* The Ada code already knows how to handle these types, so all that
15496 we need to do is turn the bounds into static bounds. However, we
15497 don't want to rewrite existing array or index types in-place,
15498 because those may be referenced in other contexts where this
15499 rewriting is undesirable. */
15500 struct type *new_ary_type
15501 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15502 if (new_ary_type != nullptr)
15503 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15504 }
15505
15506 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15507 appropriate error checking and issuing complaints if there is a
15508 problem. */
15509
15510 static ULONGEST
15511 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15512 {
15513 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15514
15515 if (attr == nullptr)
15516 return 0;
15517
15518 if (!attr->form_is_constant ())
15519 {
15520 complaint (_("DW_AT_alignment must have constant form"
15521 " - DIE at %s [in module %s]"),
15522 sect_offset_str (die->sect_off),
15523 objfile_name (cu->per_objfile->objfile));
15524 return 0;
15525 }
15526
15527 LONGEST val = attr->constant_value (0);
15528 if (val < 0)
15529 {
15530 complaint (_("DW_AT_alignment value must not be negative"
15531 " - DIE at %s [in module %s]"),
15532 sect_offset_str (die->sect_off),
15533 objfile_name (cu->per_objfile->objfile));
15534 return 0;
15535 }
15536 ULONGEST align = val;
15537
15538 if (align == 0)
15539 {
15540 complaint (_("DW_AT_alignment value must not be zero"
15541 " - DIE at %s [in module %s]"),
15542 sect_offset_str (die->sect_off),
15543 objfile_name (cu->per_objfile->objfile));
15544 return 0;
15545 }
15546 if ((align & (align - 1)) != 0)
15547 {
15548 complaint (_("DW_AT_alignment value must be a power of 2"
15549 " - DIE at %s [in module %s]"),
15550 sect_offset_str (die->sect_off),
15551 objfile_name (cu->per_objfile->objfile));
15552 return 0;
15553 }
15554
15555 return align;
15556 }
15557
15558 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15559 the alignment for TYPE. */
15560
15561 static void
15562 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15563 struct type *type)
15564 {
15565 if (!set_type_align (type, get_alignment (cu, die)))
15566 complaint (_("DW_AT_alignment value too large"
15567 " - DIE at %s [in module %s]"),
15568 sect_offset_str (die->sect_off),
15569 objfile_name (cu->per_objfile->objfile));
15570 }
15571
15572 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15573 constant for a type, according to DWARF5 spec, Table 5.5. */
15574
15575 static bool
15576 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15577 {
15578 switch (value)
15579 {
15580 case DW_CC_normal:
15581 case DW_CC_pass_by_reference:
15582 case DW_CC_pass_by_value:
15583 return true;
15584
15585 default:
15586 complaint (_("unrecognized DW_AT_calling_convention value "
15587 "(%s) for a type"), pulongest (value));
15588 return false;
15589 }
15590 }
15591
15592 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15593 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15594 also according to GNU-specific values (see include/dwarf2.h). */
15595
15596 static bool
15597 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15598 {
15599 switch (value)
15600 {
15601 case DW_CC_normal:
15602 case DW_CC_program:
15603 case DW_CC_nocall:
15604 return true;
15605
15606 case DW_CC_GNU_renesas_sh:
15607 case DW_CC_GNU_borland_fastcall_i386:
15608 case DW_CC_GDB_IBM_OpenCL:
15609 return true;
15610
15611 default:
15612 complaint (_("unrecognized DW_AT_calling_convention value "
15613 "(%s) for a subroutine"), pulongest (value));
15614 return false;
15615 }
15616 }
15617
15618 /* Called when we find the DIE that starts a structure or union scope
15619 (definition) to create a type for the structure or union. Fill in
15620 the type's name and general properties; the members will not be
15621 processed until process_structure_scope. A symbol table entry for
15622 the type will also not be done until process_structure_scope (assuming
15623 the type has a name).
15624
15625 NOTE: we need to call these functions regardless of whether or not the
15626 DIE has a DW_AT_name attribute, since it might be an anonymous
15627 structure or union. This gets the type entered into our set of
15628 user defined types. */
15629
15630 static struct type *
15631 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15632 {
15633 struct objfile *objfile = cu->per_objfile->objfile;
15634 struct type *type;
15635 struct attribute *attr;
15636 const char *name;
15637
15638 /* If the definition of this type lives in .debug_types, read that type.
15639 Don't follow DW_AT_specification though, that will take us back up
15640 the chain and we want to go down. */
15641 attr = die->attr (DW_AT_signature);
15642 if (attr != nullptr)
15643 {
15644 type = get_DW_AT_signature_type (die, attr, cu);
15645
15646 /* The type's CU may not be the same as CU.
15647 Ensure TYPE is recorded with CU in die_type_hash. */
15648 return set_die_type (die, type, cu);
15649 }
15650
15651 type = alloc_type (objfile);
15652 INIT_CPLUS_SPECIFIC (type);
15653
15654 name = dwarf2_name (die, cu);
15655 if (name != NULL)
15656 {
15657 if (cu->per_cu->lang == language_cplus
15658 || cu->per_cu->lang == language_d
15659 || cu->per_cu->lang == language_rust)
15660 {
15661 const char *full_name = dwarf2_full_name (name, die, cu);
15662
15663 /* dwarf2_full_name might have already finished building the DIE's
15664 type. If so, there is no need to continue. */
15665 if (get_die_type (die, cu) != NULL)
15666 return get_die_type (die, cu);
15667
15668 type->set_name (full_name);
15669 }
15670 else
15671 {
15672 /* The name is already allocated along with this objfile, so
15673 we don't need to duplicate it for the type. */
15674 type->set_name (name);
15675 }
15676 }
15677
15678 if (die->tag == DW_TAG_structure_type)
15679 {
15680 type->set_code (TYPE_CODE_STRUCT);
15681 }
15682 else if (die->tag == DW_TAG_union_type)
15683 {
15684 type->set_code (TYPE_CODE_UNION);
15685 }
15686 else if (die->tag == DW_TAG_namelist)
15687 {
15688 type->set_code (TYPE_CODE_NAMELIST);
15689 }
15690 else
15691 {
15692 type->set_code (TYPE_CODE_STRUCT);
15693 }
15694
15695 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
15696 type->set_is_declared_class (true);
15697
15698 /* Store the calling convention in the type if it's available in
15699 the die. Otherwise the calling convention remains set to
15700 the default value DW_CC_normal. */
15701 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15702 if (attr != nullptr
15703 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15704 {
15705 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15706 TYPE_CPLUS_CALLING_CONVENTION (type)
15707 = (enum dwarf_calling_convention) (attr->constant_value (0));
15708 }
15709
15710 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15711 if (attr != nullptr)
15712 {
15713 if (attr->form_is_constant ())
15714 TYPE_LENGTH (type) = attr->constant_value (0);
15715 else
15716 {
15717 struct dynamic_prop prop;
15718 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15719 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15720 TYPE_LENGTH (type) = 0;
15721 }
15722 }
15723 else
15724 {
15725 TYPE_LENGTH (type) = 0;
15726 }
15727
15728 maybe_set_alignment (cu, die, type);
15729
15730 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15731 {
15732 /* ICC<14 does not output the required DW_AT_declaration on
15733 incomplete types, but gives them a size of zero. */
15734 type->set_is_stub (true);
15735 }
15736 else
15737 type->set_stub_is_supported (true);
15738
15739 if (die_is_declaration (die, cu))
15740 type->set_is_stub (true);
15741 else if (attr == NULL && die->child == NULL
15742 && producer_is_realview (cu->producer))
15743 /* RealView does not output the required DW_AT_declaration
15744 on incomplete types. */
15745 type->set_is_stub (true);
15746
15747 /* We need to add the type field to the die immediately so we don't
15748 infinitely recurse when dealing with pointers to the structure
15749 type within the structure itself. */
15750 set_die_type (die, type, cu);
15751
15752 /* set_die_type should be already done. */
15753 set_descriptive_type (type, die, cu);
15754
15755 return type;
15756 }
15757
15758 static void handle_struct_member_die
15759 (struct die_info *child_die,
15760 struct type *type,
15761 struct field_info *fi,
15762 std::vector<struct symbol *> *template_args,
15763 struct dwarf2_cu *cu);
15764
15765 /* A helper for handle_struct_member_die that handles
15766 DW_TAG_variant_part. */
15767
15768 static void
15769 handle_variant_part (struct die_info *die, struct type *type,
15770 struct field_info *fi,
15771 std::vector<struct symbol *> *template_args,
15772 struct dwarf2_cu *cu)
15773 {
15774 variant_part_builder *new_part;
15775 if (fi->current_variant_part == nullptr)
15776 {
15777 fi->variant_parts.emplace_back ();
15778 new_part = &fi->variant_parts.back ();
15779 }
15780 else if (!fi->current_variant_part->processing_variant)
15781 {
15782 complaint (_("nested DW_TAG_variant_part seen "
15783 "- DIE at %s [in module %s]"),
15784 sect_offset_str (die->sect_off),
15785 objfile_name (cu->per_objfile->objfile));
15786 return;
15787 }
15788 else
15789 {
15790 variant_field &current = fi->current_variant_part->variants.back ();
15791 current.variant_parts.emplace_back ();
15792 new_part = &current.variant_parts.back ();
15793 }
15794
15795 /* When we recurse, we want callees to add to this new variant
15796 part. */
15797 scoped_restore save_current_variant_part
15798 = make_scoped_restore (&fi->current_variant_part, new_part);
15799
15800 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15801 if (discr == NULL)
15802 {
15803 /* It's a univariant form, an extension we support. */
15804 }
15805 else if (discr->form_is_ref ())
15806 {
15807 struct dwarf2_cu *target_cu = cu;
15808 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15809
15810 new_part->discriminant_offset = target_die->sect_off;
15811 }
15812 else
15813 {
15814 complaint (_("DW_AT_discr does not have DIE reference form"
15815 " - DIE at %s [in module %s]"),
15816 sect_offset_str (die->sect_off),
15817 objfile_name (cu->per_objfile->objfile));
15818 }
15819
15820 for (die_info *child_die = die->child;
15821 child_die != NULL;
15822 child_die = child_die->sibling)
15823 handle_struct_member_die (child_die, type, fi, template_args, cu);
15824 }
15825
15826 /* A helper for handle_struct_member_die that handles
15827 DW_TAG_variant. */
15828
15829 static void
15830 handle_variant (struct die_info *die, struct type *type,
15831 struct field_info *fi,
15832 std::vector<struct symbol *> *template_args,
15833 struct dwarf2_cu *cu)
15834 {
15835 if (fi->current_variant_part == nullptr)
15836 {
15837 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15838 "- DIE at %s [in module %s]"),
15839 sect_offset_str (die->sect_off),
15840 objfile_name (cu->per_objfile->objfile));
15841 return;
15842 }
15843 if (fi->current_variant_part->processing_variant)
15844 {
15845 complaint (_("nested DW_TAG_variant seen "
15846 "- DIE at %s [in module %s]"),
15847 sect_offset_str (die->sect_off),
15848 objfile_name (cu->per_objfile->objfile));
15849 return;
15850 }
15851
15852 scoped_restore save_processing_variant
15853 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15854 true);
15855
15856 fi->current_variant_part->variants.emplace_back ();
15857 variant_field &variant = fi->current_variant_part->variants.back ();
15858 variant.first_field = fi->fields.size ();
15859
15860 /* In a variant we want to get the discriminant and also add a
15861 field for our sole member child. */
15862 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15863 if (discr == nullptr || !discr->form_is_constant ())
15864 {
15865 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15866 if (discr == nullptr || discr->as_block ()->size == 0)
15867 variant.default_branch = true;
15868 else
15869 variant.discr_list_data = discr->as_block ();
15870 }
15871 else
15872 variant.discriminant_value = discr->constant_value (0);
15873
15874 for (die_info *variant_child = die->child;
15875 variant_child != NULL;
15876 variant_child = variant_child->sibling)
15877 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15878
15879 variant.last_field = fi->fields.size ();
15880 }
15881
15882 /* A helper for process_structure_scope that handles a single member
15883 DIE. */
15884
15885 static void
15886 handle_struct_member_die (struct die_info *child_die, struct type *type,
15887 struct field_info *fi,
15888 std::vector<struct symbol *> *template_args,
15889 struct dwarf2_cu *cu)
15890 {
15891 if (child_die->tag == DW_TAG_member
15892 || child_die->tag == DW_TAG_variable
15893 || child_die->tag == DW_TAG_namelist_item)
15894 {
15895 /* NOTE: carlton/2002-11-05: A C++ static data member
15896 should be a DW_TAG_member that is a declaration, but
15897 all versions of G++ as of this writing (so through at
15898 least 3.2.1) incorrectly generate DW_TAG_variable
15899 tags for them instead. */
15900 dwarf2_add_field (fi, child_die, cu);
15901 }
15902 else if (child_die->tag == DW_TAG_subprogram)
15903 {
15904 /* Rust doesn't have member functions in the C++ sense.
15905 However, it does emit ordinary functions as children
15906 of a struct DIE. */
15907 if (cu->per_cu->lang == language_rust)
15908 read_func_scope (child_die, cu);
15909 else
15910 {
15911 /* C++ member function. */
15912 dwarf2_add_member_fn (fi, child_die, type, cu);
15913 }
15914 }
15915 else if (child_die->tag == DW_TAG_inheritance)
15916 {
15917 /* C++ base class field. */
15918 dwarf2_add_field (fi, child_die, cu);
15919 }
15920 else if (type_can_define_types (child_die))
15921 dwarf2_add_type_defn (fi, child_die, cu);
15922 else if (child_die->tag == DW_TAG_template_type_param
15923 || child_die->tag == DW_TAG_template_value_param)
15924 {
15925 struct symbol *arg = new_symbol (child_die, NULL, cu);
15926
15927 if (arg != NULL)
15928 template_args->push_back (arg);
15929 }
15930 else if (child_die->tag == DW_TAG_variant_part)
15931 handle_variant_part (child_die, type, fi, template_args, cu);
15932 else if (child_die->tag == DW_TAG_variant)
15933 handle_variant (child_die, type, fi, template_args, cu);
15934 }
15935
15936 /* Finish creating a structure or union type, including filling in its
15937 members and creating a symbol for it. This function also handles Fortran
15938 namelist variables, their items or members and creating a symbol for
15939 them. */
15940
15941 static void
15942 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15943 {
15944 struct objfile *objfile = cu->per_objfile->objfile;
15945 struct die_info *child_die;
15946 struct type *type;
15947
15948 type = get_die_type (die, cu);
15949 if (type == NULL)
15950 type = read_structure_type (die, cu);
15951
15952 bool has_template_parameters = false;
15953 if (die->child != NULL && ! die_is_declaration (die, cu))
15954 {
15955 struct field_info fi;
15956 std::vector<struct symbol *> template_args;
15957
15958 child_die = die->child;
15959
15960 while (child_die && child_die->tag)
15961 {
15962 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15963 child_die = child_die->sibling;
15964 }
15965
15966 /* Attach template arguments to type. */
15967 if (!template_args.empty ())
15968 {
15969 has_template_parameters = true;
15970 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15971 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15972 TYPE_TEMPLATE_ARGUMENTS (type)
15973 = XOBNEWVEC (&objfile->objfile_obstack,
15974 struct symbol *,
15975 TYPE_N_TEMPLATE_ARGUMENTS (type));
15976 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15977 template_args.data (),
15978 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15979 * sizeof (struct symbol *)));
15980 }
15981
15982 /* Attach fields and member functions to the type. */
15983 if (fi.nfields () > 0)
15984 dwarf2_attach_fields_to_type (&fi, type, cu);
15985 if (!fi.fnfieldlists.empty ())
15986 {
15987 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15988
15989 /* Get the type which refers to the base class (possibly this
15990 class itself) which contains the vtable pointer for the current
15991 class from the DW_AT_containing_type attribute. This use of
15992 DW_AT_containing_type is a GNU extension. */
15993
15994 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15995 {
15996 struct type *t = die_containing_type (die, cu);
15997
15998 set_type_vptr_basetype (type, t);
15999 if (type == t)
16000 {
16001 int i;
16002
16003 /* Our own class provides vtbl ptr. */
16004 for (i = t->num_fields () - 1;
16005 i >= TYPE_N_BASECLASSES (t);
16006 --i)
16007 {
16008 const char *fieldname = t->field (i).name ();
16009
16010 if (is_vtable_name (fieldname, cu))
16011 {
16012 set_type_vptr_fieldno (type, i);
16013 break;
16014 }
16015 }
16016
16017 /* Complain if virtual function table field not found. */
16018 if (i < TYPE_N_BASECLASSES (t))
16019 complaint (_("virtual function table pointer "
16020 "not found when defining class '%s'"),
16021 type->name () ? type->name () : "");
16022 }
16023 else
16024 {
16025 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16026 }
16027 }
16028 else if (cu->producer
16029 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16030 {
16031 /* The IBM XLC compiler does not provide direct indication
16032 of the containing type, but the vtable pointer is
16033 always named __vfp. */
16034
16035 int i;
16036
16037 for (i = type->num_fields () - 1;
16038 i >= TYPE_N_BASECLASSES (type);
16039 --i)
16040 {
16041 if (strcmp (type->field (i).name (), "__vfp") == 0)
16042 {
16043 set_type_vptr_fieldno (type, i);
16044 set_type_vptr_basetype (type, type);
16045 break;
16046 }
16047 }
16048 }
16049 }
16050
16051 /* Copy fi.typedef_field_list linked list elements content into the
16052 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16053 if (!fi.typedef_field_list.empty ())
16054 {
16055 int count = fi.typedef_field_list.size ();
16056
16057 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16058 TYPE_TYPEDEF_FIELD_ARRAY (type)
16059 = ((struct decl_field *)
16060 TYPE_ALLOC (type,
16061 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16062 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16063
16064 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16065 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16066 }
16067
16068 /* Copy fi.nested_types_list linked list elements content into the
16069 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16070 if (!fi.nested_types_list.empty ()
16071 && cu->per_cu->lang != language_ada)
16072 {
16073 int count = fi.nested_types_list.size ();
16074
16075 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16076 TYPE_NESTED_TYPES_ARRAY (type)
16077 = ((struct decl_field *)
16078 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16079 TYPE_NESTED_TYPES_COUNT (type) = count;
16080
16081 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16082 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16083 }
16084 }
16085
16086 quirk_gcc_member_function_pointer (type, objfile);
16087 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
16088 cu->rust_unions.push_back (type);
16089 else if (cu->per_cu->lang == language_ada)
16090 quirk_ada_thick_pointer_struct (die, cu, type);
16091
16092 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16093 snapshots) has been known to create a die giving a declaration
16094 for a class that has, as a child, a die giving a definition for a
16095 nested class. So we have to process our children even if the
16096 current die is a declaration. Normally, of course, a declaration
16097 won't have any children at all. */
16098
16099 child_die = die->child;
16100
16101 while (child_die != NULL && child_die->tag)
16102 {
16103 if (child_die->tag == DW_TAG_member
16104 || child_die->tag == DW_TAG_variable
16105 || child_die->tag == DW_TAG_inheritance
16106 || child_die->tag == DW_TAG_template_value_param
16107 || child_die->tag == DW_TAG_template_type_param)
16108 {
16109 /* Do nothing. */
16110 }
16111 else
16112 process_die (child_die, cu);
16113
16114 child_die = child_die->sibling;
16115 }
16116
16117 /* Do not consider external references. According to the DWARF standard,
16118 these DIEs are identified by the fact that they have no byte_size
16119 attribute, and a declaration attribute. */
16120 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16121 || !die_is_declaration (die, cu)
16122 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16123 {
16124 struct symbol *sym = new_symbol (die, type, cu);
16125
16126 if (has_template_parameters)
16127 {
16128 struct symtab *symtab;
16129 if (sym != nullptr)
16130 symtab = symbol_symtab (sym);
16131 else if (cu->line_header != nullptr)
16132 {
16133 /* Any related symtab will do. */
16134 symtab
16135 = cu->line_header->file_names ()[0].symtab;
16136 }
16137 else
16138 {
16139 symtab = nullptr;
16140 complaint (_("could not find suitable "
16141 "symtab for template parameter"
16142 " - DIE at %s [in module %s]"),
16143 sect_offset_str (die->sect_off),
16144 objfile_name (objfile));
16145 }
16146
16147 if (symtab != nullptr)
16148 {
16149 /* Make sure that the symtab is set on the new symbols.
16150 Even though they don't appear in this symtab directly,
16151 other parts of gdb assume that symbols do, and this is
16152 reasonably true. */
16153 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16154 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16155 }
16156 }
16157 }
16158 }
16159
16160 /* Assuming DIE is an enumeration type, and TYPE is its associated
16161 type, update TYPE using some information only available in DIE's
16162 children. In particular, the fields are computed. */
16163
16164 static void
16165 update_enumeration_type_from_children (struct die_info *die,
16166 struct type *type,
16167 struct dwarf2_cu *cu)
16168 {
16169 struct die_info *child_die;
16170 int unsigned_enum = 1;
16171 int flag_enum = 1;
16172
16173 auto_obstack obstack;
16174 std::vector<struct field> fields;
16175
16176 for (child_die = die->child;
16177 child_die != NULL && child_die->tag;
16178 child_die = child_die->sibling)
16179 {
16180 struct attribute *attr;
16181 LONGEST value;
16182 const gdb_byte *bytes;
16183 struct dwarf2_locexpr_baton *baton;
16184 const char *name;
16185
16186 if (child_die->tag != DW_TAG_enumerator)
16187 continue;
16188
16189 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16190 if (attr == NULL)
16191 continue;
16192
16193 name = dwarf2_name (child_die, cu);
16194 if (name == NULL)
16195 name = "<anonymous enumerator>";
16196
16197 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16198 &value, &bytes, &baton);
16199 if (value < 0)
16200 {
16201 unsigned_enum = 0;
16202 flag_enum = 0;
16203 }
16204 else
16205 {
16206 if (count_one_bits_ll (value) >= 2)
16207 flag_enum = 0;
16208 }
16209
16210 fields.emplace_back ();
16211 struct field &field = fields.back ();
16212 field.set_name (dwarf2_physname (name, child_die, cu));
16213 field.set_loc_enumval (value);
16214 }
16215
16216 if (!fields.empty ())
16217 {
16218 type->set_num_fields (fields.size ());
16219 type->set_fields
16220 ((struct field *)
16221 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16222 memcpy (type->fields (), fields.data (),
16223 sizeof (struct field) * fields.size ());
16224 }
16225
16226 if (unsigned_enum)
16227 type->set_is_unsigned (true);
16228
16229 if (flag_enum)
16230 type->set_is_flag_enum (true);
16231 }
16232
16233 /* Given a DW_AT_enumeration_type die, set its type. We do not
16234 complete the type's fields yet, or create any symbols. */
16235
16236 static struct type *
16237 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16238 {
16239 struct objfile *objfile = cu->per_objfile->objfile;
16240 struct type *type;
16241 struct attribute *attr;
16242 const char *name;
16243
16244 /* If the definition of this type lives in .debug_types, read that type.
16245 Don't follow DW_AT_specification though, that will take us back up
16246 the chain and we want to go down. */
16247 attr = die->attr (DW_AT_signature);
16248 if (attr != nullptr)
16249 {
16250 type = get_DW_AT_signature_type (die, attr, cu);
16251
16252 /* The type's CU may not be the same as CU.
16253 Ensure TYPE is recorded with CU in die_type_hash. */
16254 return set_die_type (die, type, cu);
16255 }
16256
16257 type = alloc_type (objfile);
16258
16259 type->set_code (TYPE_CODE_ENUM);
16260 name = dwarf2_full_name (NULL, die, cu);
16261 if (name != NULL)
16262 type->set_name (name);
16263
16264 attr = dwarf2_attr (die, DW_AT_type, cu);
16265 if (attr != NULL)
16266 {
16267 struct type *underlying_type = die_type (die, cu);
16268
16269 TYPE_TARGET_TYPE (type) = underlying_type;
16270 }
16271
16272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16273 if (attr != nullptr)
16274 {
16275 TYPE_LENGTH (type) = attr->constant_value (0);
16276 }
16277 else
16278 {
16279 TYPE_LENGTH (type) = 0;
16280 }
16281
16282 maybe_set_alignment (cu, die, type);
16283
16284 /* The enumeration DIE can be incomplete. In Ada, any type can be
16285 declared as private in the package spec, and then defined only
16286 inside the package body. Such types are known as Taft Amendment
16287 Types. When another package uses such a type, an incomplete DIE
16288 may be generated by the compiler. */
16289 if (die_is_declaration (die, cu))
16290 type->set_is_stub (true);
16291
16292 /* If this type has an underlying type that is not a stub, then we
16293 may use its attributes. We always use the "unsigned" attribute
16294 in this situation, because ordinarily we guess whether the type
16295 is unsigned -- but the guess can be wrong and the underlying type
16296 can tell us the reality. However, we defer to a local size
16297 attribute if one exists, because this lets the compiler override
16298 the underlying type if needed. */
16299 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16300 {
16301 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16302 underlying_type = check_typedef (underlying_type);
16303
16304 type->set_is_unsigned (underlying_type->is_unsigned ());
16305
16306 if (TYPE_LENGTH (type) == 0)
16307 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16308
16309 if (TYPE_RAW_ALIGN (type) == 0
16310 && TYPE_RAW_ALIGN (underlying_type) != 0)
16311 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16312 }
16313
16314 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16315
16316 set_die_type (die, type, cu);
16317
16318 /* Finish the creation of this type by using the enum's children.
16319 Note that, as usual, this must come after set_die_type to avoid
16320 infinite recursion when trying to compute the names of the
16321 enumerators. */
16322 update_enumeration_type_from_children (die, type, cu);
16323
16324 return type;
16325 }
16326
16327 /* Given a pointer to a die which begins an enumeration, process all
16328 the dies that define the members of the enumeration, and create the
16329 symbol for the enumeration type.
16330
16331 NOTE: We reverse the order of the element list. */
16332
16333 static void
16334 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16335 {
16336 struct type *this_type;
16337
16338 this_type = get_die_type (die, cu);
16339 if (this_type == NULL)
16340 this_type = read_enumeration_type (die, cu);
16341
16342 if (die->child != NULL)
16343 {
16344 struct die_info *child_die;
16345 const char *name;
16346
16347 child_die = die->child;
16348 while (child_die && child_die->tag)
16349 {
16350 if (child_die->tag != DW_TAG_enumerator)
16351 {
16352 process_die (child_die, cu);
16353 }
16354 else
16355 {
16356 name = dwarf2_name (child_die, cu);
16357 if (name)
16358 new_symbol (child_die, this_type, cu);
16359 }
16360
16361 child_die = child_die->sibling;
16362 }
16363 }
16364
16365 /* If we are reading an enum from a .debug_types unit, and the enum
16366 is a declaration, and the enum is not the signatured type in the
16367 unit, then we do not want to add a symbol for it. Adding a
16368 symbol would in some cases obscure the true definition of the
16369 enum, giving users an incomplete type when the definition is
16370 actually available. Note that we do not want to do this for all
16371 enums which are just declarations, because C++0x allows forward
16372 enum declarations. */
16373 if (cu->per_cu->is_debug_types
16374 && die_is_declaration (die, cu))
16375 {
16376 struct signatured_type *sig_type;
16377
16378 sig_type = (struct signatured_type *) cu->per_cu;
16379 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16380 if (sig_type->type_offset_in_section != die->sect_off)
16381 return;
16382 }
16383
16384 new_symbol (die, this_type, cu);
16385 }
16386
16387 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16388 expression for an index type and finds the corresponding field
16389 offset in the hidden "P_BOUNDS" structure. Returns true on success
16390 and updates *FIELD, false if it fails to recognize an
16391 expression. */
16392
16393 static bool
16394 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16395 int *bounds_offset, struct field *field,
16396 struct dwarf2_cu *cu)
16397 {
16398 struct attribute *attr = dwarf2_attr (die, name, cu);
16399 if (attr == nullptr || !attr->form_is_block ())
16400 return false;
16401
16402 const struct dwarf_block *block = attr->as_block ();
16403 const gdb_byte *start = block->data;
16404 const gdb_byte *end = block->data + block->size;
16405
16406 /* The expression to recognize generally looks like:
16407
16408 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16409 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16410
16411 However, the second "plus_uconst" may be missing:
16412
16413 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16414 DW_OP_deref_size: 4)
16415
16416 This happens when the field is at the start of the structure.
16417
16418 Also, the final deref may not be sized:
16419
16420 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16421 DW_OP_deref)
16422
16423 This happens when the size of the index type happens to be the
16424 same as the architecture's word size. This can occur with or
16425 without the second plus_uconst. */
16426
16427 if (end - start < 2)
16428 return false;
16429 if (*start++ != DW_OP_push_object_address)
16430 return false;
16431 if (*start++ != DW_OP_plus_uconst)
16432 return false;
16433
16434 uint64_t this_bound_off;
16435 start = gdb_read_uleb128 (start, end, &this_bound_off);
16436 if (start == nullptr || (int) this_bound_off != this_bound_off)
16437 return false;
16438 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16439 is consistent among all bounds. */
16440 if (*bounds_offset == -1)
16441 *bounds_offset = this_bound_off;
16442 else if (*bounds_offset != this_bound_off)
16443 return false;
16444
16445 if (start == end || *start++ != DW_OP_deref)
16446 return false;
16447
16448 int offset = 0;
16449 if (start ==end)
16450 return false;
16451 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16452 {
16453 /* This means an offset of 0. */
16454 }
16455 else if (*start++ != DW_OP_plus_uconst)
16456 return false;
16457 else
16458 {
16459 /* The size is the parameter to DW_OP_plus_uconst. */
16460 uint64_t val;
16461 start = gdb_read_uleb128 (start, end, &val);
16462 if (start == nullptr)
16463 return false;
16464 if ((int) val != val)
16465 return false;
16466 offset = val;
16467 }
16468
16469 if (start == end)
16470 return false;
16471
16472 uint64_t size;
16473 if (*start == DW_OP_deref_size)
16474 {
16475 start = gdb_read_uleb128 (start + 1, end, &size);
16476 if (start == nullptr)
16477 return false;
16478 }
16479 else if (*start == DW_OP_deref)
16480 {
16481 size = cu->header.addr_size;
16482 ++start;
16483 }
16484 else
16485 return false;
16486
16487 field->set_loc_bitpos (8 * offset);
16488 if (size != TYPE_LENGTH (field->type ()))
16489 FIELD_BITSIZE (*field) = 8 * size;
16490
16491 return true;
16492 }
16493
16494 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16495 some kinds of Ada arrays:
16496
16497 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16498 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16499 <11e0> DW_AT_data_location: 2 byte block: 97 6
16500 (DW_OP_push_object_address; DW_OP_deref)
16501 <11e3> DW_AT_type : <0x1173>
16502 <11e7> DW_AT_sibling : <0x1201>
16503 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16504 <11ec> DW_AT_type : <0x1206>
16505 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16506 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16507 DW_OP_deref_size: 4)
16508 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16509 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16510 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16511
16512 This actually represents a "thick pointer", which is a structure
16513 with two elements: one that is a pointer to the array data, and one
16514 that is a pointer to another structure; this second structure holds
16515 the array bounds.
16516
16517 This returns a new type on success, or nullptr if this didn't
16518 recognize the type. */
16519
16520 static struct type *
16521 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16522 struct type *type)
16523 {
16524 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16525 /* So far we've only seen this with block form. */
16526 if (attr == nullptr || !attr->form_is_block ())
16527 return nullptr;
16528
16529 /* Note that this will fail if the structure layout is changed by
16530 the compiler. However, we have no good way to recognize some
16531 other layout, because we don't know what expression the compiler
16532 might choose to emit should this happen. */
16533 struct dwarf_block *blk = attr->as_block ();
16534 if (blk->size != 2
16535 || blk->data[0] != DW_OP_push_object_address
16536 || blk->data[1] != DW_OP_deref)
16537 return nullptr;
16538
16539 int bounds_offset = -1;
16540 int max_align = -1;
16541 std::vector<struct field> range_fields;
16542 for (struct die_info *child_die = die->child;
16543 child_die;
16544 child_die = child_die->sibling)
16545 {
16546 if (child_die->tag == DW_TAG_subrange_type)
16547 {
16548 struct type *underlying = read_subrange_index_type (child_die, cu);
16549
16550 int this_align = type_align (underlying);
16551 if (this_align > max_align)
16552 max_align = this_align;
16553
16554 range_fields.emplace_back ();
16555 range_fields.emplace_back ();
16556
16557 struct field &lower = range_fields[range_fields.size () - 2];
16558 struct field &upper = range_fields[range_fields.size () - 1];
16559
16560 lower.set_type (underlying);
16561 FIELD_ARTIFICIAL (lower) = 1;
16562
16563 upper.set_type (underlying);
16564 FIELD_ARTIFICIAL (upper) = 1;
16565
16566 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16567 &bounds_offset, &lower, cu)
16568 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16569 &bounds_offset, &upper, cu))
16570 return nullptr;
16571 }
16572 }
16573
16574 /* This shouldn't really happen, but double-check that we found
16575 where the bounds are stored. */
16576 if (bounds_offset == -1)
16577 return nullptr;
16578
16579 struct objfile *objfile = cu->per_objfile->objfile;
16580 for (int i = 0; i < range_fields.size (); i += 2)
16581 {
16582 char name[20];
16583
16584 /* Set the name of each field in the bounds. */
16585 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16586 range_fields[i].set_name (objfile->intern (name));
16587 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16588 range_fields[i + 1].set_name (objfile->intern (name));
16589 }
16590
16591 struct type *bounds = alloc_type (objfile);
16592 bounds->set_code (TYPE_CODE_STRUCT);
16593
16594 bounds->set_num_fields (range_fields.size ());
16595 bounds->set_fields
16596 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16597 * sizeof (struct field))));
16598 memcpy (bounds->fields (), range_fields.data (),
16599 bounds->num_fields () * sizeof (struct field));
16600
16601 int last_fieldno = range_fields.size () - 1;
16602 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
16603 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16604 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16605
16606 /* Rewrite the existing array type in place. Specifically, we
16607 remove any dynamic properties we might have read, and we replace
16608 the index types. */
16609 struct type *iter = type;
16610 for (int i = 0; i < range_fields.size (); i += 2)
16611 {
16612 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16613 iter->main_type->dyn_prop_list = nullptr;
16614 iter->set_index_type
16615 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16616 iter = TYPE_TARGET_TYPE (iter);
16617 }
16618
16619 struct type *result = alloc_type (objfile);
16620 result->set_code (TYPE_CODE_STRUCT);
16621
16622 result->set_num_fields (2);
16623 result->set_fields
16624 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16625 * sizeof (struct field))));
16626
16627 /* The names are chosen to coincide with what the compiler does with
16628 -fgnat-encodings=all, which the Ada code in gdb already
16629 understands. */
16630 result->field (0).set_name ("P_ARRAY");
16631 result->field (0).set_type (lookup_pointer_type (type));
16632
16633 result->field (1).set_name ("P_BOUNDS");
16634 result->field (1).set_type (lookup_pointer_type (bounds));
16635 result->field (1).set_loc_bitpos (8 * bounds_offset);
16636
16637 result->set_name (type->name ());
16638 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16639 + TYPE_LENGTH (result->field (1).type ()));
16640
16641 return result;
16642 }
16643
16644 /* Extract all information from a DW_TAG_array_type DIE and put it in
16645 the DIE's type field. For now, this only handles one dimensional
16646 arrays. */
16647
16648 static struct type *
16649 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16650 {
16651 struct objfile *objfile = cu->per_objfile->objfile;
16652 struct die_info *child_die;
16653 struct type *type;
16654 struct type *element_type, *range_type, *index_type;
16655 struct attribute *attr;
16656 const char *name;
16657 struct dynamic_prop *byte_stride_prop = NULL;
16658 unsigned int bit_stride = 0;
16659
16660 element_type = die_type (die, cu);
16661
16662 /* The die_type call above may have already set the type for this DIE. */
16663 type = get_die_type (die, cu);
16664 if (type)
16665 return type;
16666
16667 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16668 if (attr != NULL)
16669 {
16670 int stride_ok;
16671 struct type *prop_type = cu->addr_sized_int_type (false);
16672
16673 byte_stride_prop
16674 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16675 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16676 prop_type);
16677 if (!stride_ok)
16678 {
16679 complaint (_("unable to read array DW_AT_byte_stride "
16680 " - DIE at %s [in module %s]"),
16681 sect_offset_str (die->sect_off),
16682 objfile_name (cu->per_objfile->objfile));
16683 /* Ignore this attribute. We will likely not be able to print
16684 arrays of this type correctly, but there is little we can do
16685 to help if we cannot read the attribute's value. */
16686 byte_stride_prop = NULL;
16687 }
16688 }
16689
16690 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16691 if (attr != NULL)
16692 bit_stride = attr->constant_value (0);
16693
16694 /* Irix 6.2 native cc creates array types without children for
16695 arrays with unspecified length. */
16696 if (die->child == NULL)
16697 {
16698 index_type = objfile_type (objfile)->builtin_int;
16699 range_type = create_static_range_type (NULL, index_type, 0, -1);
16700 type = create_array_type_with_stride (NULL, element_type, range_type,
16701 byte_stride_prop, bit_stride);
16702 return set_die_type (die, type, cu);
16703 }
16704
16705 std::vector<struct type *> range_types;
16706 child_die = die->child;
16707 while (child_die && child_die->tag)
16708 {
16709 if (child_die->tag == DW_TAG_subrange_type
16710 || child_die->tag == DW_TAG_generic_subrange)
16711 {
16712 struct type *child_type = read_type_die (child_die, cu);
16713
16714 if (child_type != NULL)
16715 {
16716 /* The range type was succesfully read. Save it for the
16717 array type creation. */
16718 range_types.push_back (child_type);
16719 }
16720 }
16721 child_die = child_die->sibling;
16722 }
16723
16724 if (range_types.empty ())
16725 {
16726 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16727 sect_offset_str (die->sect_off),
16728 objfile_name (cu->per_objfile->objfile));
16729 return NULL;
16730 }
16731
16732 /* Dwarf2 dimensions are output from left to right, create the
16733 necessary array types in backwards order. */
16734
16735 type = element_type;
16736
16737 if (read_array_order (die, cu) == DW_ORD_col_major)
16738 {
16739 int i = 0;
16740
16741 while (i < range_types.size ())
16742 {
16743 type = create_array_type_with_stride (NULL, type, range_types[i++],
16744 byte_stride_prop, bit_stride);
16745 bit_stride = 0;
16746 byte_stride_prop = nullptr;
16747 }
16748 }
16749 else
16750 {
16751 size_t ndim = range_types.size ();
16752 while (ndim-- > 0)
16753 {
16754 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16755 byte_stride_prop, bit_stride);
16756 bit_stride = 0;
16757 byte_stride_prop = nullptr;
16758 }
16759 }
16760
16761 gdb_assert (type != element_type);
16762
16763 /* Understand Dwarf2 support for vector types (like they occur on
16764 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16765 array type. This is not part of the Dwarf2/3 standard yet, but a
16766 custom vendor extension. The main difference between a regular
16767 array and the vector variant is that vectors are passed by value
16768 to functions. */
16769 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16770 if (attr != nullptr)
16771 make_vector_type (type);
16772
16773 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16774 implementation may choose to implement triple vectors using this
16775 attribute. */
16776 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16777 if (attr != nullptr && attr->form_is_unsigned ())
16778 {
16779 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16780 TYPE_LENGTH (type) = attr->as_unsigned ();
16781 else
16782 complaint (_("DW_AT_byte_size for array type smaller "
16783 "than the total size of elements"));
16784 }
16785
16786 name = dwarf2_name (die, cu);
16787 if (name)
16788 type->set_name (name);
16789
16790 maybe_set_alignment (cu, die, type);
16791
16792 struct type *replacement_type = nullptr;
16793 if (cu->per_cu->lang == language_ada)
16794 {
16795 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16796 if (replacement_type != nullptr)
16797 type = replacement_type;
16798 }
16799
16800 /* Install the type in the die. */
16801 set_die_type (die, type, cu, replacement_type != nullptr);
16802
16803 /* set_die_type should be already done. */
16804 set_descriptive_type (type, die, cu);
16805
16806 return type;
16807 }
16808
16809 static enum dwarf_array_dim_ordering
16810 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16811 {
16812 struct attribute *attr;
16813
16814 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16815
16816 if (attr != nullptr)
16817 {
16818 LONGEST val = attr->constant_value (-1);
16819 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16820 return (enum dwarf_array_dim_ordering) val;
16821 }
16822
16823 /* GNU F77 is a special case, as at 08/2004 array type info is the
16824 opposite order to the dwarf2 specification, but data is still
16825 laid out as per normal fortran.
16826
16827 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16828 version checking. */
16829
16830 if (cu->per_cu->lang == language_fortran
16831 && cu->producer && strstr (cu->producer, "GNU F77"))
16832 {
16833 return DW_ORD_row_major;
16834 }
16835
16836 switch (cu->language_defn->array_ordering ())
16837 {
16838 case array_column_major:
16839 return DW_ORD_col_major;
16840 case array_row_major:
16841 default:
16842 return DW_ORD_row_major;
16843 };
16844 }
16845
16846 /* Extract all information from a DW_TAG_set_type DIE and put it in
16847 the DIE's type field. */
16848
16849 static struct type *
16850 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16851 {
16852 struct type *domain_type, *set_type;
16853 struct attribute *attr;
16854
16855 domain_type = die_type (die, cu);
16856
16857 /* The die_type call above may have already set the type for this DIE. */
16858 set_type = get_die_type (die, cu);
16859 if (set_type)
16860 return set_type;
16861
16862 set_type = create_set_type (NULL, domain_type);
16863
16864 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16865 if (attr != nullptr && attr->form_is_unsigned ())
16866 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16867
16868 maybe_set_alignment (cu, die, set_type);
16869
16870 return set_die_type (die, set_type, cu);
16871 }
16872
16873 /* A helper for read_common_block that creates a locexpr baton.
16874 SYM is the symbol which we are marking as computed.
16875 COMMON_DIE is the DIE for the common block.
16876 COMMON_LOC is the location expression attribute for the common
16877 block itself.
16878 MEMBER_LOC is the location expression attribute for the particular
16879 member of the common block that we are processing.
16880 CU is the CU from which the above come. */
16881
16882 static void
16883 mark_common_block_symbol_computed (struct symbol *sym,
16884 struct die_info *common_die,
16885 struct attribute *common_loc,
16886 struct attribute *member_loc,
16887 struct dwarf2_cu *cu)
16888 {
16889 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16890 struct objfile *objfile = per_objfile->objfile;
16891 struct dwarf2_locexpr_baton *baton;
16892 gdb_byte *ptr;
16893 unsigned int cu_off;
16894 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16895 LONGEST offset = 0;
16896
16897 gdb_assert (common_loc && member_loc);
16898 gdb_assert (common_loc->form_is_block ());
16899 gdb_assert (member_loc->form_is_block ()
16900 || member_loc->form_is_constant ());
16901
16902 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16903 baton->per_objfile = per_objfile;
16904 baton->per_cu = cu->per_cu;
16905 gdb_assert (baton->per_cu);
16906
16907 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16908
16909 if (member_loc->form_is_constant ())
16910 {
16911 offset = member_loc->constant_value (0);
16912 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16913 }
16914 else
16915 baton->size += member_loc->as_block ()->size;
16916
16917 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16918 baton->data = ptr;
16919
16920 *ptr++ = DW_OP_call4;
16921 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16922 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16923 ptr += 4;
16924
16925 if (member_loc->form_is_constant ())
16926 {
16927 *ptr++ = DW_OP_addr;
16928 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16929 ptr += cu->header.addr_size;
16930 }
16931 else
16932 {
16933 /* We have to copy the data here, because DW_OP_call4 will only
16934 use a DW_AT_location attribute. */
16935 struct dwarf_block *block = member_loc->as_block ();
16936 memcpy (ptr, block->data, block->size);
16937 ptr += block->size;
16938 }
16939
16940 *ptr++ = DW_OP_plus;
16941 gdb_assert (ptr - baton->data == baton->size);
16942
16943 SYMBOL_LOCATION_BATON (sym) = baton;
16944 sym->set_aclass_index (dwarf2_locexpr_index);
16945 }
16946
16947 /* Create appropriate locally-scoped variables for all the
16948 DW_TAG_common_block entries. Also create a struct common_block
16949 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16950 is used to separate the common blocks name namespace from regular
16951 variable names. */
16952
16953 static void
16954 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16955 {
16956 struct attribute *attr;
16957
16958 attr = dwarf2_attr (die, DW_AT_location, cu);
16959 if (attr != nullptr)
16960 {
16961 /* Support the .debug_loc offsets. */
16962 if (attr->form_is_block ())
16963 {
16964 /* Ok. */
16965 }
16966 else if (attr->form_is_section_offset ())
16967 {
16968 dwarf2_complex_location_expr_complaint ();
16969 attr = NULL;
16970 }
16971 else
16972 {
16973 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16974 "common block member");
16975 attr = NULL;
16976 }
16977 }
16978
16979 if (die->child != NULL)
16980 {
16981 struct objfile *objfile = cu->per_objfile->objfile;
16982 struct die_info *child_die;
16983 size_t n_entries = 0, size;
16984 struct common_block *common_block;
16985 struct symbol *sym;
16986
16987 for (child_die = die->child;
16988 child_die && child_die->tag;
16989 child_die = child_die->sibling)
16990 ++n_entries;
16991
16992 size = (sizeof (struct common_block)
16993 + (n_entries - 1) * sizeof (struct symbol *));
16994 common_block
16995 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16996 size);
16997 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16998 common_block->n_entries = 0;
16999
17000 for (child_die = die->child;
17001 child_die && child_die->tag;
17002 child_die = child_die->sibling)
17003 {
17004 /* Create the symbol in the DW_TAG_common_block block in the current
17005 symbol scope. */
17006 sym = new_symbol (child_die, NULL, cu);
17007 if (sym != NULL)
17008 {
17009 struct attribute *member_loc;
17010
17011 common_block->contents[common_block->n_entries++] = sym;
17012
17013 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17014 cu);
17015 if (member_loc)
17016 {
17017 /* GDB has handled this for a long time, but it is
17018 not specified by DWARF. It seems to have been
17019 emitted by gfortran at least as recently as:
17020 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17021 complaint (_("Variable in common block has "
17022 "DW_AT_data_member_location "
17023 "- DIE at %s [in module %s]"),
17024 sect_offset_str (child_die->sect_off),
17025 objfile_name (objfile));
17026
17027 if (member_loc->form_is_section_offset ())
17028 dwarf2_complex_location_expr_complaint ();
17029 else if (member_loc->form_is_constant ()
17030 || member_loc->form_is_block ())
17031 {
17032 if (attr != nullptr)
17033 mark_common_block_symbol_computed (sym, die, attr,
17034 member_loc, cu);
17035 }
17036 else
17037 dwarf2_complex_location_expr_complaint ();
17038 }
17039 }
17040 }
17041
17042 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17043 sym->set_value_common_block (common_block);
17044 }
17045 }
17046
17047 /* Create a type for a C++ namespace. */
17048
17049 static struct type *
17050 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17051 {
17052 struct objfile *objfile = cu->per_objfile->objfile;
17053 const char *previous_prefix, *name;
17054 int is_anonymous;
17055 struct type *type;
17056
17057 /* For extensions, reuse the type of the original namespace. */
17058 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17059 {
17060 struct die_info *ext_die;
17061 struct dwarf2_cu *ext_cu = cu;
17062
17063 ext_die = dwarf2_extension (die, &ext_cu);
17064 type = read_type_die (ext_die, ext_cu);
17065
17066 /* EXT_CU may not be the same as CU.
17067 Ensure TYPE is recorded with CU in die_type_hash. */
17068 return set_die_type (die, type, cu);
17069 }
17070
17071 name = namespace_name (die, &is_anonymous, cu);
17072
17073 /* Now build the name of the current namespace. */
17074
17075 previous_prefix = determine_prefix (die, cu);
17076 if (previous_prefix[0] != '\0')
17077 name = typename_concat (&objfile->objfile_obstack,
17078 previous_prefix, name, 0, cu);
17079
17080 /* Create the type. */
17081 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17082
17083 return set_die_type (die, type, cu);
17084 }
17085
17086 /* Read a namespace scope. */
17087
17088 static void
17089 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17090 {
17091 struct objfile *objfile = cu->per_objfile->objfile;
17092 int is_anonymous;
17093
17094 /* Add a symbol associated to this if we haven't seen the namespace
17095 before. Also, add a using directive if it's an anonymous
17096 namespace. */
17097
17098 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17099 {
17100 struct type *type;
17101
17102 type = read_type_die (die, cu);
17103 new_symbol (die, type, cu);
17104
17105 namespace_name (die, &is_anonymous, cu);
17106 if (is_anonymous)
17107 {
17108 const char *previous_prefix = determine_prefix (die, cu);
17109
17110 std::vector<const char *> excludes;
17111 add_using_directive (using_directives (cu),
17112 previous_prefix, type->name (), NULL,
17113 NULL, excludes, 0, &objfile->objfile_obstack);
17114 }
17115 }
17116
17117 if (die->child != NULL)
17118 {
17119 struct die_info *child_die = die->child;
17120
17121 while (child_die && child_die->tag)
17122 {
17123 process_die (child_die, cu);
17124 child_die = child_die->sibling;
17125 }
17126 }
17127 }
17128
17129 /* Read a Fortran module as type. This DIE can be only a declaration used for
17130 imported module. Still we need that type as local Fortran "use ... only"
17131 declaration imports depend on the created type in determine_prefix. */
17132
17133 static struct type *
17134 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17135 {
17136 struct objfile *objfile = cu->per_objfile->objfile;
17137 const char *module_name;
17138 struct type *type;
17139
17140 module_name = dwarf2_name (die, cu);
17141 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17142
17143 return set_die_type (die, type, cu);
17144 }
17145
17146 /* Read a Fortran module. */
17147
17148 static void
17149 read_module (struct die_info *die, struct dwarf2_cu *cu)
17150 {
17151 struct die_info *child_die = die->child;
17152 struct type *type;
17153
17154 type = read_type_die (die, cu);
17155 new_symbol (die, type, cu);
17156
17157 while (child_die && child_die->tag)
17158 {
17159 process_die (child_die, cu);
17160 child_die = child_die->sibling;
17161 }
17162 }
17163
17164 /* Return the name of the namespace represented by DIE. Set
17165 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17166 namespace. */
17167
17168 static const char *
17169 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17170 {
17171 struct die_info *current_die;
17172 const char *name = NULL;
17173
17174 /* Loop through the extensions until we find a name. */
17175
17176 for (current_die = die;
17177 current_die != NULL;
17178 current_die = dwarf2_extension (die, &cu))
17179 {
17180 /* We don't use dwarf2_name here so that we can detect the absence
17181 of a name -> anonymous namespace. */
17182 name = dwarf2_string_attr (die, DW_AT_name, cu);
17183
17184 if (name != NULL)
17185 break;
17186 }
17187
17188 /* Is it an anonymous namespace? */
17189
17190 *is_anonymous = (name == NULL);
17191 if (*is_anonymous)
17192 name = CP_ANONYMOUS_NAMESPACE_STR;
17193
17194 return name;
17195 }
17196
17197 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17198 the user defined type vector. */
17199
17200 static struct type *
17201 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17202 {
17203 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17204 struct comp_unit_head *cu_header = &cu->header;
17205 struct type *type;
17206 struct attribute *attr_byte_size;
17207 struct attribute *attr_address_class;
17208 int byte_size, addr_class;
17209 struct type *target_type;
17210
17211 target_type = die_type (die, cu);
17212
17213 /* The die_type call above may have already set the type for this DIE. */
17214 type = get_die_type (die, cu);
17215 if (type)
17216 return type;
17217
17218 type = lookup_pointer_type (target_type);
17219
17220 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17221 if (attr_byte_size)
17222 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17223 else
17224 byte_size = cu_header->addr_size;
17225
17226 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17227 if (attr_address_class)
17228 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17229 else
17230 addr_class = DW_ADDR_none;
17231
17232 ULONGEST alignment = get_alignment (cu, die);
17233
17234 /* If the pointer size, alignment, or address class is different
17235 than the default, create a type variant marked as such and set
17236 the length accordingly. */
17237 if (TYPE_LENGTH (type) != byte_size
17238 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17239 && alignment != TYPE_RAW_ALIGN (type))
17240 || addr_class != DW_ADDR_none)
17241 {
17242 if (gdbarch_address_class_type_flags_p (gdbarch))
17243 {
17244 type_instance_flags type_flags
17245 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17246 addr_class);
17247 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17248 == 0);
17249 type = make_type_with_address_space (type, type_flags);
17250 }
17251 else if (TYPE_LENGTH (type) != byte_size)
17252 {
17253 complaint (_("invalid pointer size %d"), byte_size);
17254 }
17255 else if (TYPE_RAW_ALIGN (type) != alignment)
17256 {
17257 complaint (_("Invalid DW_AT_alignment"
17258 " - DIE at %s [in module %s]"),
17259 sect_offset_str (die->sect_off),
17260 objfile_name (cu->per_objfile->objfile));
17261 }
17262 else
17263 {
17264 /* Should we also complain about unhandled address classes? */
17265 }
17266 }
17267
17268 TYPE_LENGTH (type) = byte_size;
17269 set_type_align (type, alignment);
17270 return set_die_type (die, type, cu);
17271 }
17272
17273 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17274 the user defined type vector. */
17275
17276 static struct type *
17277 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17278 {
17279 struct type *type;
17280 struct type *to_type;
17281 struct type *domain;
17282
17283 to_type = die_type (die, cu);
17284 domain = die_containing_type (die, cu);
17285
17286 /* The calls above may have already set the type for this DIE. */
17287 type = get_die_type (die, cu);
17288 if (type)
17289 return type;
17290
17291 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17292 type = lookup_methodptr_type (to_type);
17293 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17294 {
17295 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17296
17297 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17298 to_type->fields (), to_type->num_fields (),
17299 to_type->has_varargs ());
17300 type = lookup_methodptr_type (new_type);
17301 }
17302 else
17303 type = lookup_memberptr_type (to_type, domain);
17304
17305 return set_die_type (die, type, cu);
17306 }
17307
17308 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17309 the user defined type vector. */
17310
17311 static struct type *
17312 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17313 enum type_code refcode)
17314 {
17315 struct comp_unit_head *cu_header = &cu->header;
17316 struct type *type, *target_type;
17317 struct attribute *attr;
17318
17319 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17320
17321 target_type = die_type (die, cu);
17322
17323 /* The die_type call above may have already set the type for this DIE. */
17324 type = get_die_type (die, cu);
17325 if (type)
17326 return type;
17327
17328 type = lookup_reference_type (target_type, refcode);
17329 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17330 if (attr != nullptr)
17331 {
17332 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17333 }
17334 else
17335 {
17336 TYPE_LENGTH (type) = cu_header->addr_size;
17337 }
17338 maybe_set_alignment (cu, die, type);
17339 return set_die_type (die, type, cu);
17340 }
17341
17342 /* Add the given cv-qualifiers to the element type of the array. GCC
17343 outputs DWARF type qualifiers that apply to an array, not the
17344 element type. But GDB relies on the array element type to carry
17345 the cv-qualifiers. This mimics section 6.7.3 of the C99
17346 specification. */
17347
17348 static struct type *
17349 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17350 struct type *base_type, int cnst, int voltl)
17351 {
17352 struct type *el_type, *inner_array;
17353
17354 base_type = copy_type (base_type);
17355 inner_array = base_type;
17356
17357 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17358 {
17359 TYPE_TARGET_TYPE (inner_array) =
17360 copy_type (TYPE_TARGET_TYPE (inner_array));
17361 inner_array = TYPE_TARGET_TYPE (inner_array);
17362 }
17363
17364 el_type = TYPE_TARGET_TYPE (inner_array);
17365 cnst |= TYPE_CONST (el_type);
17366 voltl |= TYPE_VOLATILE (el_type);
17367 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17368
17369 return set_die_type (die, base_type, cu);
17370 }
17371
17372 static struct type *
17373 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17374 {
17375 struct type *base_type, *cv_type;
17376
17377 base_type = die_type (die, cu);
17378
17379 /* The die_type call above may have already set the type for this DIE. */
17380 cv_type = get_die_type (die, cu);
17381 if (cv_type)
17382 return cv_type;
17383
17384 /* In case the const qualifier is applied to an array type, the element type
17385 is so qualified, not the array type (section 6.7.3 of C99). */
17386 if (base_type->code () == TYPE_CODE_ARRAY)
17387 return add_array_cv_type (die, cu, base_type, 1, 0);
17388
17389 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17390 return set_die_type (die, cv_type, cu);
17391 }
17392
17393 static struct type *
17394 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17395 {
17396 struct type *base_type, *cv_type;
17397
17398 base_type = die_type (die, cu);
17399
17400 /* The die_type call above may have already set the type for this DIE. */
17401 cv_type = get_die_type (die, cu);
17402 if (cv_type)
17403 return cv_type;
17404
17405 /* In case the volatile qualifier is applied to an array type, the
17406 element type is so qualified, not the array type (section 6.7.3
17407 of C99). */
17408 if (base_type->code () == TYPE_CODE_ARRAY)
17409 return add_array_cv_type (die, cu, base_type, 0, 1);
17410
17411 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17412 return set_die_type (die, cv_type, cu);
17413 }
17414
17415 /* Handle DW_TAG_restrict_type. */
17416
17417 static struct type *
17418 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17419 {
17420 struct type *base_type, *cv_type;
17421
17422 base_type = die_type (die, cu);
17423
17424 /* The die_type call above may have already set the type for this DIE. */
17425 cv_type = get_die_type (die, cu);
17426 if (cv_type)
17427 return cv_type;
17428
17429 cv_type = make_restrict_type (base_type);
17430 return set_die_type (die, cv_type, cu);
17431 }
17432
17433 /* Handle DW_TAG_atomic_type. */
17434
17435 static struct type *
17436 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17437 {
17438 struct type *base_type, *cv_type;
17439
17440 base_type = die_type (die, cu);
17441
17442 /* The die_type call above may have already set the type for this DIE. */
17443 cv_type = get_die_type (die, cu);
17444 if (cv_type)
17445 return cv_type;
17446
17447 cv_type = make_atomic_type (base_type);
17448 return set_die_type (die, cv_type, cu);
17449 }
17450
17451 /* Extract all information from a DW_TAG_string_type DIE and add to
17452 the user defined type vector. It isn't really a user defined type,
17453 but it behaves like one, with other DIE's using an AT_user_def_type
17454 attribute to reference it. */
17455
17456 static struct type *
17457 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17458 {
17459 struct objfile *objfile = cu->per_objfile->objfile;
17460 struct gdbarch *gdbarch = objfile->arch ();
17461 struct type *type, *range_type, *index_type, *char_type;
17462 struct attribute *attr;
17463 struct dynamic_prop prop;
17464 bool length_is_constant = true;
17465 LONGEST length;
17466
17467 /* There are a couple of places where bit sizes might be made use of
17468 when parsing a DW_TAG_string_type, however, no producer that we know
17469 of make use of these. Handling bit sizes that are a multiple of the
17470 byte size is easy enough, but what about other bit sizes? Lets deal
17471 with that problem when we have to. Warn about these attributes being
17472 unsupported, then parse the type and ignore them like we always
17473 have. */
17474 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17475 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17476 {
17477 static bool warning_printed = false;
17478 if (!warning_printed)
17479 {
17480 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17481 "currently supported on DW_TAG_string_type."));
17482 warning_printed = true;
17483 }
17484 }
17485
17486 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17487 if (attr != nullptr && !attr->form_is_constant ())
17488 {
17489 /* The string length describes the location at which the length of
17490 the string can be found. The size of the length field can be
17491 specified with one of the attributes below. */
17492 struct type *prop_type;
17493 struct attribute *len
17494 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17495 if (len == nullptr)
17496 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17497 if (len != nullptr && len->form_is_constant ())
17498 {
17499 /* Pass 0 as the default as we know this attribute is constant
17500 and the default value will not be returned. */
17501 LONGEST sz = len->constant_value (0);
17502 prop_type = objfile_int_type (objfile, sz, true);
17503 }
17504 else
17505 {
17506 /* If the size is not specified then we assume it is the size of
17507 an address on this target. */
17508 prop_type = cu->addr_sized_int_type (true);
17509 }
17510
17511 /* Convert the attribute into a dynamic property. */
17512 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17513 length = 1;
17514 else
17515 length_is_constant = false;
17516 }
17517 else if (attr != nullptr)
17518 {
17519 /* This DW_AT_string_length just contains the length with no
17520 indirection. There's no need to create a dynamic property in this
17521 case. Pass 0 for the default value as we know it will not be
17522 returned in this case. */
17523 length = attr->constant_value (0);
17524 }
17525 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17526 {
17527 /* We don't currently support non-constant byte sizes for strings. */
17528 length = attr->constant_value (1);
17529 }
17530 else
17531 {
17532 /* Use 1 as a fallback length if we have nothing else. */
17533 length = 1;
17534 }
17535
17536 index_type = objfile_type (objfile)->builtin_int;
17537 if (length_is_constant)
17538 range_type = create_static_range_type (NULL, index_type, 1, length);
17539 else
17540 {
17541 struct dynamic_prop low_bound;
17542
17543 low_bound.set_const_val (1);
17544 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17545 }
17546 char_type = language_string_char_type (cu->language_defn, gdbarch);
17547 type = create_string_type (NULL, char_type, range_type);
17548
17549 return set_die_type (die, type, cu);
17550 }
17551
17552 /* Assuming that DIE corresponds to a function, returns nonzero
17553 if the function is prototyped. */
17554
17555 static int
17556 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17557 {
17558 struct attribute *attr;
17559
17560 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17561 if (attr && attr->as_boolean ())
17562 return 1;
17563
17564 /* The DWARF standard implies that the DW_AT_prototyped attribute
17565 is only meaningful for C, but the concept also extends to other
17566 languages that allow unprototyped functions (Eg: Objective C).
17567 For all other languages, assume that functions are always
17568 prototyped. */
17569 if (cu->per_cu->lang != language_c
17570 && cu->per_cu->lang != language_objc
17571 && cu->per_cu->lang != language_opencl)
17572 return 1;
17573
17574 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17575 prototyped and unprototyped functions; default to prototyped,
17576 since that is more common in modern code (and RealView warns
17577 about unprototyped functions). */
17578 if (producer_is_realview (cu->producer))
17579 return 1;
17580
17581 return 0;
17582 }
17583
17584 /* Handle DIES due to C code like:
17585
17586 struct foo
17587 {
17588 int (*funcp)(int a, long l);
17589 int b;
17590 };
17591
17592 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17593
17594 static struct type *
17595 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17596 {
17597 struct objfile *objfile = cu->per_objfile->objfile;
17598 struct type *type; /* Type that this function returns. */
17599 struct type *ftype; /* Function that returns above type. */
17600 struct attribute *attr;
17601
17602 type = die_type (die, cu);
17603
17604 /* The die_type call above may have already set the type for this DIE. */
17605 ftype = get_die_type (die, cu);
17606 if (ftype)
17607 return ftype;
17608
17609 ftype = lookup_function_type (type);
17610
17611 if (prototyped_function_p (die, cu))
17612 ftype->set_is_prototyped (true);
17613
17614 /* Store the calling convention in the type if it's available in
17615 the subroutine die. Otherwise set the calling convention to
17616 the default value DW_CC_normal. */
17617 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17618 if (attr != nullptr
17619 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17620 TYPE_CALLING_CONVENTION (ftype)
17621 = (enum dwarf_calling_convention) attr->constant_value (0);
17622 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17623 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17624 else
17625 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17626
17627 /* Record whether the function returns normally to its caller or not
17628 if the DWARF producer set that information. */
17629 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17630 if (attr && attr->as_boolean ())
17631 TYPE_NO_RETURN (ftype) = 1;
17632
17633 /* We need to add the subroutine type to the die immediately so
17634 we don't infinitely recurse when dealing with parameters
17635 declared as the same subroutine type. */
17636 set_die_type (die, ftype, cu);
17637
17638 if (die->child != NULL)
17639 {
17640 struct type *void_type = objfile_type (objfile)->builtin_void;
17641 struct die_info *child_die;
17642 int nparams, iparams;
17643
17644 /* Count the number of parameters.
17645 FIXME: GDB currently ignores vararg functions, but knows about
17646 vararg member functions. */
17647 nparams = 0;
17648 child_die = die->child;
17649 while (child_die && child_die->tag)
17650 {
17651 if (child_die->tag == DW_TAG_formal_parameter)
17652 nparams++;
17653 else if (child_die->tag == DW_TAG_unspecified_parameters)
17654 ftype->set_has_varargs (true);
17655
17656 child_die = child_die->sibling;
17657 }
17658
17659 /* Allocate storage for parameters and fill them in. */
17660 ftype->set_num_fields (nparams);
17661 ftype->set_fields
17662 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17663
17664 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17665 even if we error out during the parameters reading below. */
17666 for (iparams = 0; iparams < nparams; iparams++)
17667 ftype->field (iparams).set_type (void_type);
17668
17669 iparams = 0;
17670 child_die = die->child;
17671 while (child_die && child_die->tag)
17672 {
17673 if (child_die->tag == DW_TAG_formal_parameter)
17674 {
17675 struct type *arg_type;
17676
17677 /* DWARF version 2 has no clean way to discern C++
17678 static and non-static member functions. G++ helps
17679 GDB by marking the first parameter for non-static
17680 member functions (which is the this pointer) as
17681 artificial. We pass this information to
17682 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17683
17684 DWARF version 3 added DW_AT_object_pointer, which GCC
17685 4.5 does not yet generate. */
17686 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17687 if (attr != nullptr)
17688 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17689 else
17690 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17691 arg_type = die_type (child_die, cu);
17692
17693 /* RealView does not mark THIS as const, which the testsuite
17694 expects. GCC marks THIS as const in method definitions,
17695 but not in the class specifications (GCC PR 43053). */
17696 if (cu->per_cu->lang == language_cplus
17697 && !TYPE_CONST (arg_type)
17698 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17699 {
17700 int is_this = 0;
17701 struct dwarf2_cu *arg_cu = cu;
17702 const char *name = dwarf2_name (child_die, cu);
17703
17704 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17705 if (attr != nullptr)
17706 {
17707 /* If the compiler emits this, use it. */
17708 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17709 is_this = 1;
17710 }
17711 else if (name && strcmp (name, "this") == 0)
17712 /* Function definitions will have the argument names. */
17713 is_this = 1;
17714 else if (name == NULL && iparams == 0)
17715 /* Declarations may not have the names, so like
17716 elsewhere in GDB, assume an artificial first
17717 argument is "this". */
17718 is_this = 1;
17719
17720 if (is_this)
17721 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17722 arg_type, 0);
17723 }
17724
17725 ftype->field (iparams).set_type (arg_type);
17726 iparams++;
17727 }
17728 child_die = child_die->sibling;
17729 }
17730 }
17731
17732 return ftype;
17733 }
17734
17735 static struct type *
17736 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17737 {
17738 struct objfile *objfile = cu->per_objfile->objfile;
17739 const char *name = NULL;
17740 struct type *this_type, *target_type;
17741
17742 name = dwarf2_full_name (NULL, die, cu);
17743 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17744 this_type->set_target_is_stub (true);
17745 set_die_type (die, this_type, cu);
17746 target_type = die_type (die, cu);
17747 if (target_type != this_type)
17748 TYPE_TARGET_TYPE (this_type) = target_type;
17749 else
17750 {
17751 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17752 spec and cause infinite loops in GDB. */
17753 complaint (_("Self-referential DW_TAG_typedef "
17754 "- DIE at %s [in module %s]"),
17755 sect_offset_str (die->sect_off), objfile_name (objfile));
17756 TYPE_TARGET_TYPE (this_type) = NULL;
17757 }
17758 if (name == NULL)
17759 {
17760 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17761 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17762 Handle these by just returning the target type, rather than
17763 constructing an anonymous typedef type and trying to handle this
17764 elsewhere. */
17765 set_die_type (die, target_type, cu);
17766 return target_type;
17767 }
17768 return this_type;
17769 }
17770
17771 /* Helper for get_dwarf2_rational_constant that computes the value of
17772 a given gmp_mpz given an attribute. */
17773
17774 static void
17775 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17776 {
17777 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17778 location expression that pushes an implicit value. */
17779 if (attr->form == DW_FORM_exprloc)
17780 {
17781 dwarf_block *blk = attr->as_block ();
17782 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17783 {
17784 uint64_t len;
17785 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17786 blk->data + blk->size,
17787 &len);
17788 if (ptr - blk->data + len <= blk->size)
17789 {
17790 mpz_import (value->val, len,
17791 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17792 1, 0, 0, ptr);
17793 return;
17794 }
17795 }
17796
17797 /* On failure set it to 1. */
17798 *value = gdb_mpz (1);
17799 }
17800 else if (attr->form_is_block ())
17801 {
17802 dwarf_block *blk = attr->as_block ();
17803 mpz_import (value->val, blk->size,
17804 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17805 1, 0, 0, blk->data);
17806 }
17807 else
17808 *value = gdb_mpz (attr->constant_value (1));
17809 }
17810
17811 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17812 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17813
17814 If the numerator and/or numerator attribute is missing,
17815 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17816 untouched. */
17817
17818 static void
17819 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17820 gdb_mpz *numerator, gdb_mpz *denominator)
17821 {
17822 struct attribute *num_attr, *denom_attr;
17823
17824 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17825 if (num_attr == nullptr)
17826 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17827 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17828
17829 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17830 if (denom_attr == nullptr)
17831 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17832 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17833
17834 if (num_attr == nullptr || denom_attr == nullptr)
17835 return;
17836
17837 get_mpz (cu, numerator, num_attr);
17838 get_mpz (cu, denominator, denom_attr);
17839 }
17840
17841 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17842 rational constant, rather than a signed one.
17843
17844 If the rational constant has a negative value, a complaint
17845 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17846
17847 static void
17848 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17849 struct dwarf2_cu *cu,
17850 gdb_mpz *numerator,
17851 gdb_mpz *denominator)
17852 {
17853 gdb_mpz num (1);
17854 gdb_mpz denom (1);
17855
17856 get_dwarf2_rational_constant (die, cu, &num, &denom);
17857 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17858 {
17859 mpz_neg (num.val, num.val);
17860 mpz_neg (denom.val, denom.val);
17861 }
17862 else if (mpz_sgn (num.val) == -1)
17863 {
17864 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17865 " in DIE at %s"),
17866 sect_offset_str (die->sect_off));
17867 return;
17868 }
17869 else if (mpz_sgn (denom.val) == -1)
17870 {
17871 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17872 " in DIE at %s"),
17873 sect_offset_str (die->sect_off));
17874 return;
17875 }
17876
17877 *numerator = std::move (num);
17878 *denominator = std::move (denom);
17879 }
17880
17881 /* Assuming that ENCODING is a string whose contents starting at the
17882 K'th character is "_nn" where "nn" is a decimal number, scan that
17883 number and set RESULT to the value. K is updated to point to the
17884 character immediately following the number.
17885
17886 If the string does not conform to the format described above, false
17887 is returned, and K may or may not be changed. */
17888
17889 static bool
17890 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17891 {
17892 /* The next character should be an underscore ('_') followed
17893 by a digit. */
17894 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17895 return false;
17896
17897 /* Skip the underscore. */
17898 k++;
17899 int start = k;
17900
17901 /* Determine the number of digits for our number. */
17902 while (isdigit (encoding[k]))
17903 k++;
17904 if (k == start)
17905 return false;
17906
17907 std::string copy (&encoding[start], k - start);
17908 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17909 return false;
17910
17911 return true;
17912 }
17913
17914 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17915 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17916 DENOM, update OFFSET, and return true on success. Return false on
17917 failure. */
17918
17919 static bool
17920 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17921 gdb_mpz *num, gdb_mpz *denom)
17922 {
17923 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17924 return false;
17925 return ada_get_gnat_encoded_number (encoding, offset, denom);
17926 }
17927
17928 /* Assuming DIE corresponds to a fixed point type, finish the creation
17929 of the corresponding TYPE by setting its type-specific data. CU is
17930 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17931 encodings. It is nullptr if the GNAT encoding should be
17932 ignored. */
17933
17934 static void
17935 finish_fixed_point_type (struct type *type, const char *suffix,
17936 struct die_info *die, struct dwarf2_cu *cu)
17937 {
17938 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17939 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17940
17941 /* If GNAT encodings are preferred, don't examine the
17942 attributes. */
17943 struct attribute *attr = nullptr;
17944 if (suffix == nullptr)
17945 {
17946 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17947 if (attr == nullptr)
17948 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17949 if (attr == nullptr)
17950 attr = dwarf2_attr (die, DW_AT_small, cu);
17951 }
17952
17953 /* Numerator and denominator of our fixed-point type's scaling factor.
17954 The default is a scaling factor of 1, which we use as a fallback
17955 when we are not able to decode it (problem with the debugging info,
17956 unsupported forms, bug in GDB, etc...). Using that as the default
17957 allows us to at least print the unscaled value, which might still
17958 be useful to a user. */
17959 gdb_mpz scale_num (1);
17960 gdb_mpz scale_denom (1);
17961
17962 if (attr == nullptr)
17963 {
17964 int offset = 0;
17965 if (suffix != nullptr
17966 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17967 &scale_denom)
17968 /* The number might be encoded as _nn_dd_nn_dd, where the
17969 second ratio is the 'small value. In this situation, we
17970 want the second value. */
17971 && (suffix[offset] != '_'
17972 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17973 &scale_denom)))
17974 {
17975 /* Found it. */
17976 }
17977 else
17978 {
17979 /* Scaling factor not found. Assume a scaling factor of 1,
17980 and hope for the best. At least the user will be able to
17981 see the encoded value. */
17982 scale_num = 1;
17983 scale_denom = 1;
17984 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17985 sect_offset_str (die->sect_off));
17986 }
17987 }
17988 else if (attr->name == DW_AT_binary_scale)
17989 {
17990 LONGEST scale_exp = attr->constant_value (0);
17991 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17992
17993 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17994 }
17995 else if (attr->name == DW_AT_decimal_scale)
17996 {
17997 LONGEST scale_exp = attr->constant_value (0);
17998 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17999
18000 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18001 }
18002 else if (attr->name == DW_AT_small)
18003 {
18004 struct die_info *scale_die;
18005 struct dwarf2_cu *scale_cu = cu;
18006
18007 scale_die = follow_die_ref (die, attr, &scale_cu);
18008 if (scale_die->tag == DW_TAG_constant)
18009 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18010 &scale_num, &scale_denom);
18011 else
18012 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18013 " (DIE at %s)"),
18014 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18015 }
18016 else
18017 {
18018 complaint (_("unsupported scale attribute %s for fixed-point type"
18019 " (DIE at %s)"),
18020 dwarf_attr_name (attr->name),
18021 sect_offset_str (die->sect_off));
18022 }
18023
18024 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18025 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18026 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18027 mpq_canonicalize (scaling_factor.val);
18028 }
18029
18030 /* The gnat-encoding suffix for fixed point. */
18031
18032 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18033
18034 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18035 "XF" suffix of the name. The text after this is what encodes the
18036 'small and 'delta information. Otherwise, return nullptr. */
18037
18038 static const char *
18039 gnat_encoded_fixed_point_type_info (const char *name)
18040 {
18041 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18042 }
18043
18044 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18045 (which may be different from NAME) to the architecture back-end to allow
18046 it to guess the correct format if necessary. */
18047
18048 static struct type *
18049 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18050 const char *name_hint, enum bfd_endian byte_order)
18051 {
18052 struct gdbarch *gdbarch = objfile->arch ();
18053 const struct floatformat **format;
18054 struct type *type;
18055
18056 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18057 if (format)
18058 type = init_float_type (objfile, bits, name, format, byte_order);
18059 else
18060 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18061
18062 return type;
18063 }
18064
18065 /* Allocate an integer type of size BITS and name NAME. */
18066
18067 static struct type *
18068 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18069 int bits, int unsigned_p, const char *name)
18070 {
18071 struct type *type;
18072
18073 /* Versions of Intel's C Compiler generate an integer type called "void"
18074 instead of using DW_TAG_unspecified_type. This has been seen on
18075 at least versions 14, 17, and 18. */
18076 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18077 && strcmp (name, "void") == 0)
18078 type = objfile_type (objfile)->builtin_void;
18079 else
18080 type = init_integer_type (objfile, bits, unsigned_p, name);
18081
18082 return type;
18083 }
18084
18085 /* Return true if DIE has a DW_AT_small attribute whose value is
18086 a constant rational, where both the numerator and denominator
18087 are equal to zero.
18088
18089 CU is the DIE's Compilation Unit. */
18090
18091 static bool
18092 has_zero_over_zero_small_attribute (struct die_info *die,
18093 struct dwarf2_cu *cu)
18094 {
18095 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18096 if (attr == nullptr)
18097 return false;
18098
18099 struct dwarf2_cu *scale_cu = cu;
18100 struct die_info *scale_die
18101 = follow_die_ref (die, attr, &scale_cu);
18102
18103 if (scale_die->tag != DW_TAG_constant)
18104 return false;
18105
18106 gdb_mpz num (1), denom (1);
18107 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18108 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18109 }
18110
18111 /* Initialise and return a floating point type of size BITS suitable for
18112 use as a component of a complex number. The NAME_HINT is passed through
18113 when initialising the floating point type and is the name of the complex
18114 type.
18115
18116 As DWARF doesn't currently provide an explicit name for the components
18117 of a complex number, but it can be helpful to have these components
18118 named, we try to select a suitable name based on the size of the
18119 component. */
18120 static struct type *
18121 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18122 struct objfile *objfile,
18123 int bits, const char *name_hint,
18124 enum bfd_endian byte_order)
18125 {
18126 gdbarch *gdbarch = objfile->arch ();
18127 struct type *tt = nullptr;
18128
18129 /* Try to find a suitable floating point builtin type of size BITS.
18130 We're going to use the name of this type as the name for the complex
18131 target type that we are about to create. */
18132 switch (cu->per_cu->lang)
18133 {
18134 case language_fortran:
18135 switch (bits)
18136 {
18137 case 32:
18138 tt = builtin_f_type (gdbarch)->builtin_real;
18139 break;
18140 case 64:
18141 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18142 break;
18143 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18144 case 128:
18145 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18146 break;
18147 }
18148 break;
18149 default:
18150 switch (bits)
18151 {
18152 case 32:
18153 tt = builtin_type (gdbarch)->builtin_float;
18154 break;
18155 case 64:
18156 tt = builtin_type (gdbarch)->builtin_double;
18157 break;
18158 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18159 case 128:
18160 tt = builtin_type (gdbarch)->builtin_long_double;
18161 break;
18162 }
18163 break;
18164 }
18165
18166 /* If the type we found doesn't match the size we were looking for, then
18167 pretend we didn't find a type at all, the complex target type we
18168 create will then be nameless. */
18169 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18170 tt = nullptr;
18171
18172 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18173 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18174 }
18175
18176 /* Find a representation of a given base type and install
18177 it in the TYPE field of the die. */
18178
18179 static struct type *
18180 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18181 {
18182 struct objfile *objfile = cu->per_objfile->objfile;
18183 struct type *type;
18184 struct attribute *attr;
18185 int encoding = 0, bits = 0;
18186 const char *name;
18187 gdbarch *arch;
18188
18189 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18190 if (attr != nullptr && attr->form_is_constant ())
18191 encoding = attr->constant_value (0);
18192 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18193 if (attr != nullptr)
18194 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18195 name = dwarf2_name (die, cu);
18196 if (!name)
18197 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18198
18199 arch = objfile->arch ();
18200 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18201
18202 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18203 if (attr != nullptr && attr->form_is_constant ())
18204 {
18205 int endianity = attr->constant_value (0);
18206
18207 switch (endianity)
18208 {
18209 case DW_END_big:
18210 byte_order = BFD_ENDIAN_BIG;
18211 break;
18212 case DW_END_little:
18213 byte_order = BFD_ENDIAN_LITTLE;
18214 break;
18215 default:
18216 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18217 break;
18218 }
18219 }
18220
18221 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18222 && cu->per_cu->lang == language_ada
18223 && has_zero_over_zero_small_attribute (die, cu))
18224 {
18225 /* brobecker/2018-02-24: This is a fixed point type for which
18226 the scaling factor is represented as fraction whose value
18227 does not make sense (zero divided by zero), so we should
18228 normally never see these. However, there is a small category
18229 of fixed point types for which GNAT is unable to provide
18230 the scaling factor via the standard DWARF mechanisms, and
18231 for which the info is provided via the GNAT encodings instead.
18232 This is likely what this DIE is about. */
18233 encoding = (encoding == DW_ATE_signed_fixed
18234 ? DW_ATE_signed
18235 : DW_ATE_unsigned);
18236 }
18237
18238 /* With GNAT encodings, fixed-point information will be encoded in
18239 the type name. Note that this can also occur with the above
18240 zero-over-zero case, which is why this is a separate "if" rather
18241 than an "else if". */
18242 const char *gnat_encoding_suffix = nullptr;
18243 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18244 && cu->per_cu->lang == language_ada
18245 && name != nullptr)
18246 {
18247 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18248 if (gnat_encoding_suffix != nullptr)
18249 {
18250 gdb_assert (startswith (gnat_encoding_suffix,
18251 GNAT_FIXED_POINT_SUFFIX));
18252 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18253 name, gnat_encoding_suffix - name);
18254 /* Use -1 here so that SUFFIX points at the "_" after the
18255 "XF". */
18256 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18257
18258 encoding = (encoding == DW_ATE_signed
18259 ? DW_ATE_signed_fixed
18260 : DW_ATE_unsigned_fixed);
18261 }
18262 }
18263
18264 switch (encoding)
18265 {
18266 case DW_ATE_address:
18267 /* Turn DW_ATE_address into a void * pointer. */
18268 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18269 type = init_pointer_type (objfile, bits, name, type);
18270 break;
18271 case DW_ATE_boolean:
18272 type = init_boolean_type (objfile, bits, 1, name);
18273 break;
18274 case DW_ATE_complex_float:
18275 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18276 byte_order);
18277 if (type->code () == TYPE_CODE_ERROR)
18278 {
18279 if (name == nullptr)
18280 {
18281 struct obstack *obstack
18282 = &cu->per_objfile->objfile->objfile_obstack;
18283 name = obconcat (obstack, "_Complex ", type->name (),
18284 nullptr);
18285 }
18286 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18287 }
18288 else
18289 type = init_complex_type (name, type);
18290 break;
18291 case DW_ATE_decimal_float:
18292 type = init_decfloat_type (objfile, bits, name);
18293 break;
18294 case DW_ATE_float:
18295 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18296 break;
18297 case DW_ATE_signed:
18298 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18299 break;
18300 case DW_ATE_unsigned:
18301 if (cu->per_cu->lang == language_fortran
18302 && name
18303 && startswith (name, "character("))
18304 type = init_character_type (objfile, bits, 1, name);
18305 else
18306 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18307 break;
18308 case DW_ATE_signed_char:
18309 if (cu->per_cu->lang == language_ada
18310 || cu->per_cu->lang == language_m2
18311 || cu->per_cu->lang == language_pascal
18312 || cu->per_cu->lang == language_fortran)
18313 type = init_character_type (objfile, bits, 0, name);
18314 else
18315 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18316 break;
18317 case DW_ATE_unsigned_char:
18318 if (cu->per_cu->lang == language_ada
18319 || cu->per_cu->lang == language_m2
18320 || cu->per_cu->lang == language_pascal
18321 || cu->per_cu->lang == language_fortran
18322 || cu->per_cu->lang == language_rust)
18323 type = init_character_type (objfile, bits, 1, name);
18324 else
18325 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18326 break;
18327 case DW_ATE_UTF:
18328 {
18329 type = init_character_type (objfile, bits, 1, name);
18330 return set_die_type (die, type, cu);
18331 }
18332 break;
18333 case DW_ATE_signed_fixed:
18334 type = init_fixed_point_type (objfile, bits, 0, name);
18335 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18336 break;
18337 case DW_ATE_unsigned_fixed:
18338 type = init_fixed_point_type (objfile, bits, 1, name);
18339 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18340 break;
18341
18342 default:
18343 complaint (_("unsupported DW_AT_encoding: '%s'"),
18344 dwarf_type_encoding_name (encoding));
18345 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18346 break;
18347 }
18348
18349 if (type->code () == TYPE_CODE_INT
18350 && name != nullptr
18351 && strcmp (name, "char") == 0)
18352 type->set_has_no_signedness (true);
18353
18354 maybe_set_alignment (cu, die, type);
18355
18356 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18357
18358 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18359 {
18360 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18361 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18362 {
18363 unsigned real_bit_size = attr->as_unsigned ();
18364 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18365 /* Only use the attributes if they make sense together. */
18366 if (attr == nullptr
18367 || (attr->as_unsigned () + real_bit_size
18368 <= 8 * TYPE_LENGTH (type)))
18369 {
18370 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18371 = real_bit_size;
18372 if (attr != nullptr)
18373 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18374 = attr->as_unsigned ();
18375 }
18376 }
18377 }
18378
18379 return set_die_type (die, type, cu);
18380 }
18381
18382 /* A helper function that returns the name of DIE, if it refers to a
18383 variable declaration. */
18384
18385 static const char *
18386 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18387 {
18388 if (die->tag != DW_TAG_variable)
18389 return nullptr;
18390
18391 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18392 if (attr == nullptr || !attr->as_boolean ())
18393 return nullptr;
18394
18395 attr = dwarf2_attr (die, DW_AT_name, cu);
18396 if (attr == nullptr)
18397 return nullptr;
18398 return attr->as_string ();
18399 }
18400
18401 /* Parse dwarf attribute if it's a block, reference or constant and put the
18402 resulting value of the attribute into struct bound_prop.
18403 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18404
18405 static int
18406 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18407 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18408 struct type *default_type)
18409 {
18410 struct dwarf2_property_baton *baton;
18411 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18412 struct objfile *objfile = per_objfile->objfile;
18413 struct obstack *obstack = &objfile->objfile_obstack;
18414
18415 gdb_assert (default_type != NULL);
18416
18417 if (attr == NULL || prop == NULL)
18418 return 0;
18419
18420 if (attr->form_is_block ())
18421 {
18422 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18423 baton->property_type = default_type;
18424 baton->locexpr.per_cu = cu->per_cu;
18425 baton->locexpr.per_objfile = per_objfile;
18426
18427 struct dwarf_block *block;
18428 if (attr->form == DW_FORM_data16)
18429 {
18430 size_t data_size = 16;
18431 block = XOBNEW (obstack, struct dwarf_block);
18432 block->size = (data_size
18433 + 2 /* Extra bytes for DW_OP and arg. */);
18434 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18435 data[0] = DW_OP_implicit_value;
18436 data[1] = data_size;
18437 memcpy (&data[2], attr->as_block ()->data, data_size);
18438 block->data = data;
18439 }
18440 else
18441 block = attr->as_block ();
18442
18443 baton->locexpr.size = block->size;
18444 baton->locexpr.data = block->data;
18445 switch (attr->name)
18446 {
18447 case DW_AT_string_length:
18448 baton->locexpr.is_reference = true;
18449 break;
18450 default:
18451 baton->locexpr.is_reference = false;
18452 break;
18453 }
18454
18455 prop->set_locexpr (baton);
18456 gdb_assert (prop->baton () != NULL);
18457 }
18458 else if (attr->form_is_ref ())
18459 {
18460 struct dwarf2_cu *target_cu = cu;
18461 struct die_info *target_die;
18462 struct attribute *target_attr;
18463
18464 target_die = follow_die_ref (die, attr, &target_cu);
18465 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18466 if (target_attr == NULL)
18467 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18468 target_cu);
18469 if (target_attr == nullptr)
18470 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
18471 target_cu);
18472 if (target_attr == NULL)
18473 {
18474 const char *name = var_decl_name (target_die, target_cu);
18475 if (name != nullptr)
18476 {
18477 prop->set_variable_name (name);
18478 return 1;
18479 }
18480 return 0;
18481 }
18482
18483 switch (target_attr->name)
18484 {
18485 case DW_AT_location:
18486 if (target_attr->form_is_section_offset ())
18487 {
18488 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18489 baton->property_type = die_type (target_die, target_cu);
18490 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18491 prop->set_loclist (baton);
18492 gdb_assert (prop->baton () != NULL);
18493 }
18494 else if (target_attr->form_is_block ())
18495 {
18496 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18497 baton->property_type = die_type (target_die, target_cu);
18498 baton->locexpr.per_cu = cu->per_cu;
18499 baton->locexpr.per_objfile = per_objfile;
18500 struct dwarf_block *block = target_attr->as_block ();
18501 baton->locexpr.size = block->size;
18502 baton->locexpr.data = block->data;
18503 baton->locexpr.is_reference = true;
18504 prop->set_locexpr (baton);
18505 gdb_assert (prop->baton () != NULL);
18506 }
18507 else
18508 {
18509 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18510 "dynamic property");
18511 return 0;
18512 }
18513 break;
18514 case DW_AT_data_member_location:
18515 case DW_AT_data_bit_offset:
18516 {
18517 LONGEST offset;
18518
18519 if (!handle_member_location (target_die, target_cu, &offset))
18520 return 0;
18521
18522 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18523 baton->property_type = read_type_die (target_die->parent,
18524 target_cu);
18525 baton->offset_info.offset = offset;
18526 baton->offset_info.type = die_type (target_die, target_cu);
18527 prop->set_addr_offset (baton);
18528 break;
18529 }
18530 }
18531 }
18532 else if (attr->form_is_constant ())
18533 prop->set_const_val (attr->constant_value (0));
18534 else if (attr->form_is_section_offset ())
18535 {
18536 switch (attr->name)
18537 {
18538 case DW_AT_string_length:
18539 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18540 baton->property_type = default_type;
18541 fill_in_loclist_baton (cu, &baton->loclist, attr);
18542 prop->set_loclist (baton);
18543 gdb_assert (prop->baton () != NULL);
18544 break;
18545 default:
18546 goto invalid;
18547 }
18548 }
18549 else
18550 goto invalid;
18551
18552 return 1;
18553
18554 invalid:
18555 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18556 dwarf2_name (die, cu));
18557 return 0;
18558 }
18559
18560 /* See read.h. */
18561
18562 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18563 present (which is valid) then compute the default type based on the
18564 compilation units address size. */
18565
18566 static struct type *
18567 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18568 {
18569 struct type *index_type = die_type (die, cu);
18570
18571 /* Dwarf-2 specifications explicitly allows to create subrange types
18572 without specifying a base type.
18573 In that case, the base type must be set to the type of
18574 the lower bound, upper bound or count, in that order, if any of these
18575 three attributes references an object that has a type.
18576 If no base type is found, the Dwarf-2 specifications say that
18577 a signed integer type of size equal to the size of an address should
18578 be used.
18579 For the following C code: `extern char gdb_int [];'
18580 GCC produces an empty range DIE.
18581 FIXME: muller/2010-05-28: Possible references to object for low bound,
18582 high bound or count are not yet handled by this code. */
18583 if (index_type->code () == TYPE_CODE_VOID)
18584 index_type = cu->addr_sized_int_type (false);
18585
18586 return index_type;
18587 }
18588
18589 /* Read the given DW_AT_subrange DIE. */
18590
18591 static struct type *
18592 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18593 {
18594 struct type *base_type, *orig_base_type;
18595 struct type *range_type;
18596 struct attribute *attr;
18597 struct dynamic_prop low, high;
18598 int low_default_is_valid;
18599 int high_bound_is_count = 0;
18600 const char *name;
18601 ULONGEST negative_mask;
18602
18603 orig_base_type = read_subrange_index_type (die, cu);
18604
18605 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18606 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18607 creating the range type, but we use the result of check_typedef
18608 when examining properties of the type. */
18609 base_type = check_typedef (orig_base_type);
18610
18611 /* The die_type call above may have already set the type for this DIE. */
18612 range_type = get_die_type (die, cu);
18613 if (range_type)
18614 return range_type;
18615
18616 high.set_const_val (0);
18617
18618 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18619 omitting DW_AT_lower_bound. */
18620 switch (cu->per_cu->lang)
18621 {
18622 case language_c:
18623 case language_cplus:
18624 low.set_const_val (0);
18625 low_default_is_valid = 1;
18626 break;
18627 case language_fortran:
18628 low.set_const_val (1);
18629 low_default_is_valid = 1;
18630 break;
18631 case language_d:
18632 case language_objc:
18633 case language_rust:
18634 low.set_const_val (0);
18635 low_default_is_valid = (cu->header.version >= 4);
18636 break;
18637 case language_ada:
18638 case language_m2:
18639 case language_pascal:
18640 low.set_const_val (1);
18641 low_default_is_valid = (cu->header.version >= 4);
18642 break;
18643 default:
18644 low.set_const_val (0);
18645 low_default_is_valid = 0;
18646 break;
18647 }
18648
18649 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18650 if (attr != nullptr)
18651 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18652 else if (!low_default_is_valid)
18653 complaint (_("Missing DW_AT_lower_bound "
18654 "- DIE at %s [in module %s]"),
18655 sect_offset_str (die->sect_off),
18656 objfile_name (cu->per_objfile->objfile));
18657
18658 struct attribute *attr_ub, *attr_count;
18659 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18660 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18661 {
18662 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18663 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18664 {
18665 /* If bounds are constant do the final calculation here. */
18666 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18667 high.set_const_val (low.const_val () + high.const_val () - 1);
18668 else
18669 high_bound_is_count = 1;
18670 }
18671 else
18672 {
18673 if (attr_ub != NULL)
18674 complaint (_("Unresolved DW_AT_upper_bound "
18675 "- DIE at %s [in module %s]"),
18676 sect_offset_str (die->sect_off),
18677 objfile_name (cu->per_objfile->objfile));
18678 if (attr_count != NULL)
18679 complaint (_("Unresolved DW_AT_count "
18680 "- DIE at %s [in module %s]"),
18681 sect_offset_str (die->sect_off),
18682 objfile_name (cu->per_objfile->objfile));
18683 }
18684 }
18685
18686 LONGEST bias = 0;
18687 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18688 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18689 bias = bias_attr->constant_value (0);
18690
18691 /* Normally, the DWARF producers are expected to use a signed
18692 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18693 But this is unfortunately not always the case, as witnessed
18694 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18695 is used instead. To work around that ambiguity, we treat
18696 the bounds as signed, and thus sign-extend their values, when
18697 the base type is signed. */
18698 negative_mask =
18699 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18700 if (low.kind () == PROP_CONST
18701 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18702 low.set_const_val (low.const_val () | negative_mask);
18703 if (high.kind () == PROP_CONST
18704 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18705 high.set_const_val (high.const_val () | negative_mask);
18706
18707 /* Check for bit and byte strides. */
18708 struct dynamic_prop byte_stride_prop;
18709 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18710 if (attr_byte_stride != nullptr)
18711 {
18712 struct type *prop_type = cu->addr_sized_int_type (false);
18713 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18714 prop_type);
18715 }
18716
18717 struct dynamic_prop bit_stride_prop;
18718 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18719 if (attr_bit_stride != nullptr)
18720 {
18721 /* It only makes sense to have either a bit or byte stride. */
18722 if (attr_byte_stride != nullptr)
18723 {
18724 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18725 "- DIE at %s [in module %s]"),
18726 sect_offset_str (die->sect_off),
18727 objfile_name (cu->per_objfile->objfile));
18728 attr_bit_stride = nullptr;
18729 }
18730 else
18731 {
18732 struct type *prop_type = cu->addr_sized_int_type (false);
18733 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18734 prop_type);
18735 }
18736 }
18737
18738 if (attr_byte_stride != nullptr
18739 || attr_bit_stride != nullptr)
18740 {
18741 bool byte_stride_p = (attr_byte_stride != nullptr);
18742 struct dynamic_prop *stride
18743 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18744
18745 range_type
18746 = create_range_type_with_stride (NULL, orig_base_type, &low,
18747 &high, bias, stride, byte_stride_p);
18748 }
18749 else
18750 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18751
18752 if (high_bound_is_count)
18753 range_type->bounds ()->flag_upper_bound_is_count = 1;
18754
18755 /* Ada expects an empty array on no boundary attributes. */
18756 if (attr == NULL && cu->per_cu->lang != language_ada)
18757 range_type->bounds ()->high.set_undefined ();
18758
18759 name = dwarf2_name (die, cu);
18760 if (name)
18761 range_type->set_name (name);
18762
18763 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18764 if (attr != nullptr)
18765 TYPE_LENGTH (range_type) = attr->constant_value (0);
18766
18767 maybe_set_alignment (cu, die, range_type);
18768
18769 set_die_type (die, range_type, cu);
18770
18771 /* set_die_type should be already done. */
18772 set_descriptive_type (range_type, die, cu);
18773
18774 return range_type;
18775 }
18776
18777 static struct type *
18778 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18779 {
18780 struct type *type;
18781
18782 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18783 type->set_name (dwarf2_name (die, cu));
18784
18785 /* In Ada, an unspecified type is typically used when the description
18786 of the type is deferred to a different unit. When encountering
18787 such a type, we treat it as a stub, and try to resolve it later on,
18788 when needed. */
18789 if (cu->per_cu->lang == language_ada)
18790 type->set_is_stub (true);
18791
18792 return set_die_type (die, type, cu);
18793 }
18794
18795 /* Read a single die and all its descendents. Set the die's sibling
18796 field to NULL; set other fields in the die correctly, and set all
18797 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18798 location of the info_ptr after reading all of those dies. PARENT
18799 is the parent of the die in question. */
18800
18801 static struct die_info *
18802 read_die_and_children (const struct die_reader_specs *reader,
18803 const gdb_byte *info_ptr,
18804 const gdb_byte **new_info_ptr,
18805 struct die_info *parent)
18806 {
18807 struct die_info *die;
18808 const gdb_byte *cur_ptr;
18809
18810 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18811 if (die == NULL)
18812 {
18813 *new_info_ptr = cur_ptr;
18814 return NULL;
18815 }
18816 store_in_ref_table (die, reader->cu);
18817
18818 if (die->has_children)
18819 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18820 else
18821 {
18822 die->child = NULL;
18823 *new_info_ptr = cur_ptr;
18824 }
18825
18826 die->sibling = NULL;
18827 die->parent = parent;
18828 return die;
18829 }
18830
18831 /* Read a die, all of its descendents, and all of its siblings; set
18832 all of the fields of all of the dies correctly. Arguments are as
18833 in read_die_and_children. */
18834
18835 static struct die_info *
18836 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18837 const gdb_byte *info_ptr,
18838 const gdb_byte **new_info_ptr,
18839 struct die_info *parent)
18840 {
18841 struct die_info *first_die, *last_sibling;
18842 const gdb_byte *cur_ptr;
18843
18844 cur_ptr = info_ptr;
18845 first_die = last_sibling = NULL;
18846
18847 while (1)
18848 {
18849 struct die_info *die
18850 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18851
18852 if (die == NULL)
18853 {
18854 *new_info_ptr = cur_ptr;
18855 return first_die;
18856 }
18857
18858 if (!first_die)
18859 first_die = die;
18860 else
18861 last_sibling->sibling = die;
18862
18863 last_sibling = die;
18864 }
18865 }
18866
18867 /* Read a die, all of its descendents, and all of its siblings; set
18868 all of the fields of all of the dies correctly. Arguments are as
18869 in read_die_and_children.
18870 This the main entry point for reading a DIE and all its children. */
18871
18872 static struct die_info *
18873 read_die_and_siblings (const struct die_reader_specs *reader,
18874 const gdb_byte *info_ptr,
18875 const gdb_byte **new_info_ptr,
18876 struct die_info *parent)
18877 {
18878 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18879 new_info_ptr, parent);
18880
18881 if (dwarf_die_debug)
18882 {
18883 gdb_printf (gdb_stdlog,
18884 "Read die from %s@0x%x of %s:\n",
18885 reader->die_section->get_name (),
18886 (unsigned) (info_ptr - reader->die_section->buffer),
18887 bfd_get_filename (reader->abfd));
18888 dump_die (die, dwarf_die_debug);
18889 }
18890
18891 return die;
18892 }
18893
18894 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18895 attributes.
18896 The caller is responsible for filling in the extra attributes
18897 and updating (*DIEP)->num_attrs.
18898 Set DIEP to point to a newly allocated die with its information,
18899 except for its child, sibling, and parent fields. */
18900
18901 static const gdb_byte *
18902 read_full_die_1 (const struct die_reader_specs *reader,
18903 struct die_info **diep, const gdb_byte *info_ptr,
18904 int num_extra_attrs)
18905 {
18906 unsigned int abbrev_number, bytes_read, i;
18907 const struct abbrev_info *abbrev;
18908 struct die_info *die;
18909 struct dwarf2_cu *cu = reader->cu;
18910 bfd *abfd = reader->abfd;
18911
18912 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18913 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18914 info_ptr += bytes_read;
18915 if (!abbrev_number)
18916 {
18917 *diep = NULL;
18918 return info_ptr;
18919 }
18920
18921 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18922 if (!abbrev)
18923 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18924 abbrev_number,
18925 bfd_get_filename (abfd));
18926
18927 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18928 die->sect_off = sect_off;
18929 die->tag = abbrev->tag;
18930 die->abbrev = abbrev_number;
18931 die->has_children = abbrev->has_children;
18932
18933 /* Make the result usable.
18934 The caller needs to update num_attrs after adding the extra
18935 attributes. */
18936 die->num_attrs = abbrev->num_attrs;
18937
18938 bool any_need_reprocess = false;
18939 for (i = 0; i < abbrev->num_attrs; ++i)
18940 {
18941 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18942 info_ptr);
18943 if (die->attrs[i].requires_reprocessing_p ())
18944 any_need_reprocess = true;
18945 }
18946
18947 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18948 if (attr != nullptr && attr->form_is_unsigned ())
18949 cu->str_offsets_base = attr->as_unsigned ();
18950
18951 attr = die->attr (DW_AT_loclists_base);
18952 if (attr != nullptr)
18953 cu->loclist_base = attr->as_unsigned ();
18954
18955 auto maybe_addr_base = die->addr_base ();
18956 if (maybe_addr_base.has_value ())
18957 cu->addr_base = *maybe_addr_base;
18958
18959 attr = die->attr (DW_AT_rnglists_base);
18960 if (attr != nullptr)
18961 cu->rnglists_base = attr->as_unsigned ();
18962
18963 if (any_need_reprocess)
18964 {
18965 for (i = 0; i < abbrev->num_attrs; ++i)
18966 {
18967 if (die->attrs[i].requires_reprocessing_p ())
18968 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18969 }
18970 }
18971 *diep = die;
18972 return info_ptr;
18973 }
18974
18975 /* Read a die and all its attributes.
18976 Set DIEP to point to a newly allocated die with its information,
18977 except for its child, sibling, and parent fields. */
18978
18979 static const gdb_byte *
18980 read_full_die (const struct die_reader_specs *reader,
18981 struct die_info **diep, const gdb_byte *info_ptr)
18982 {
18983 const gdb_byte *result;
18984
18985 result = read_full_die_1 (reader, diep, info_ptr, 0);
18986
18987 if (dwarf_die_debug)
18988 {
18989 gdb_printf (gdb_stdlog,
18990 "Read die from %s@0x%x of %s:\n",
18991 reader->die_section->get_name (),
18992 (unsigned) (info_ptr - reader->die_section->buffer),
18993 bfd_get_filename (reader->abfd));
18994 dump_die (*diep, dwarf_die_debug);
18995 }
18996
18997 return result;
18998 }
18999 \f
19000
19001 /* Returns nonzero if TAG represents a type that we might generate a partial
19002 symbol for. */
19003
19004 static int
19005 is_type_tag_for_partial (int tag, enum language lang)
19006 {
19007 switch (tag)
19008 {
19009 #if 0
19010 /* Some types that would be reasonable to generate partial symbols for,
19011 that we don't at present. Note that normally this does not
19012 matter, mainly because C compilers don't give names to these
19013 types, but instead emit DW_TAG_typedef. */
19014 case DW_TAG_file_type:
19015 case DW_TAG_ptr_to_member_type:
19016 case DW_TAG_set_type:
19017 case DW_TAG_string_type:
19018 case DW_TAG_subroutine_type:
19019 #endif
19020
19021 /* GNAT may emit an array with a name, but no typedef, so we
19022 need to make a symbol in this case. */
19023 case DW_TAG_array_type:
19024 return lang == language_ada;
19025
19026 case DW_TAG_base_type:
19027 case DW_TAG_class_type:
19028 case DW_TAG_interface_type:
19029 case DW_TAG_enumeration_type:
19030 case DW_TAG_structure_type:
19031 case DW_TAG_subrange_type:
19032 case DW_TAG_generic_subrange:
19033 case DW_TAG_typedef:
19034 case DW_TAG_union_type:
19035 return 1;
19036 default:
19037 return 0;
19038 }
19039 }
19040
19041 /* Load all DIEs that are interesting for partial symbols into memory. */
19042
19043 static struct partial_die_info *
19044 load_partial_dies (const struct die_reader_specs *reader,
19045 const gdb_byte *info_ptr, int building_psymtab)
19046 {
19047 struct dwarf2_cu *cu = reader->cu;
19048 struct objfile *objfile = cu->per_objfile->objfile;
19049 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19050 unsigned int bytes_read;
19051 unsigned int load_all = 0;
19052 int nesting_level = 1;
19053
19054 parent_die = NULL;
19055 last_die = NULL;
19056
19057 gdb_assert (cu->per_cu != NULL);
19058 if (cu->load_all_dies)
19059 load_all = 1;
19060
19061 cu->partial_dies
19062 = htab_create_alloc_ex (cu->header.length / 12,
19063 partial_die_hash,
19064 partial_die_eq,
19065 NULL,
19066 &cu->comp_unit_obstack,
19067 hashtab_obstack_allocate,
19068 dummy_obstack_deallocate);
19069
19070 while (1)
19071 {
19072 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19073 &bytes_read);
19074
19075 /* A NULL abbrev means the end of a series of children. */
19076 if (abbrev == NULL)
19077 {
19078 if (--nesting_level == 0)
19079 return first_die;
19080
19081 info_ptr += bytes_read;
19082 last_die = parent_die;
19083 parent_die = parent_die->die_parent;
19084 continue;
19085 }
19086
19087 /* Check for template arguments. We never save these; if
19088 they're seen, we just mark the parent, and go on our way. */
19089 if (parent_die != NULL
19090 && cu->per_cu->lang == language_cplus
19091 && (abbrev->tag == DW_TAG_template_type_param
19092 || abbrev->tag == DW_TAG_template_value_param))
19093 {
19094 parent_die->has_template_arguments = 1;
19095
19096 if (!load_all)
19097 {
19098 /* We don't need a partial DIE for the template argument. */
19099 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19100 continue;
19101 }
19102 }
19103
19104 /* We only recurse into c++ subprograms looking for template arguments.
19105 Skip their other children. */
19106 if (!load_all
19107 && cu->per_cu->lang == language_cplus
19108 && parent_die != NULL
19109 && parent_die->tag == DW_TAG_subprogram
19110 && abbrev->tag != DW_TAG_inlined_subroutine)
19111 {
19112 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19113 continue;
19114 }
19115
19116 /* Check whether this DIE is interesting enough to save. Normally
19117 we would not be interested in members here, but there may be
19118 later variables referencing them via DW_AT_specification (for
19119 static members). */
19120 if (!load_all
19121 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
19122 && abbrev->tag != DW_TAG_constant
19123 && abbrev->tag != DW_TAG_enumerator
19124 && abbrev->tag != DW_TAG_subprogram
19125 && abbrev->tag != DW_TAG_inlined_subroutine
19126 && abbrev->tag != DW_TAG_lexical_block
19127 && abbrev->tag != DW_TAG_variable
19128 && abbrev->tag != DW_TAG_namespace
19129 && abbrev->tag != DW_TAG_module
19130 && abbrev->tag != DW_TAG_member
19131 && abbrev->tag != DW_TAG_imported_unit
19132 && abbrev->tag != DW_TAG_imported_declaration)
19133 {
19134 /* Otherwise we skip to the next sibling, if any. */
19135 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19136 continue;
19137 }
19138
19139 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19140 abbrev);
19141
19142 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19143
19144 /* This two-pass algorithm for processing partial symbols has a
19145 high cost in cache pressure. Thus, handle some simple cases
19146 here which cover the majority of C partial symbols. DIEs
19147 which neither have specification tags in them, nor could have
19148 specification tags elsewhere pointing at them, can simply be
19149 processed and discarded.
19150
19151 This segment is also optional; scan_partial_symbols and
19152 add_partial_symbol will handle these DIEs if we chain
19153 them in normally. When compilers which do not emit large
19154 quantities of duplicate debug information are more common,
19155 this code can probably be removed. */
19156
19157 /* Any complete simple types at the top level (pretty much all
19158 of them, for a language without namespaces), can be processed
19159 directly. */
19160 if (parent_die == NULL
19161 && pdi.has_specification == 0
19162 && pdi.is_declaration == 0
19163 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19164 || pdi.tag == DW_TAG_base_type
19165 || pdi.tag == DW_TAG_array_type
19166 || pdi.tag == DW_TAG_generic_subrange
19167 || pdi.tag == DW_TAG_subrange_type))
19168 {
19169 if (building_psymtab && pdi.raw_name != NULL)
19170 add_partial_symbol (&pdi, cu);
19171
19172 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19173 continue;
19174 }
19175
19176 /* The exception for DW_TAG_typedef with has_children above is
19177 a workaround of GCC PR debug/47510. In the case of this complaint
19178 type_name_or_error will error on such types later.
19179
19180 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19181 it could not find the child DIEs referenced later, this is checked
19182 above. In correct DWARF DW_TAG_typedef should have no children. */
19183
19184 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19185 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19186 "- DIE at %s [in module %s]"),
19187 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19188
19189 /* If we're at the second level, and we're an enumerator, and
19190 our parent has no specification (meaning possibly lives in a
19191 namespace elsewhere), then we can add the partial symbol now
19192 instead of queueing it. */
19193 if (pdi.tag == DW_TAG_enumerator
19194 && parent_die != NULL
19195 && parent_die->die_parent == NULL
19196 && parent_die->tag == DW_TAG_enumeration_type
19197 && parent_die->has_specification == 0)
19198 {
19199 if (pdi.raw_name == NULL)
19200 complaint (_("malformed enumerator DIE ignored"));
19201 else if (building_psymtab)
19202 add_partial_symbol (&pdi, cu);
19203
19204 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19205 continue;
19206 }
19207
19208 struct partial_die_info *part_die
19209 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19210
19211 /* We'll save this DIE so link it in. */
19212 part_die->die_parent = parent_die;
19213 part_die->die_sibling = NULL;
19214 part_die->die_child = NULL;
19215
19216 if (last_die && last_die == parent_die)
19217 last_die->die_child = part_die;
19218 else if (last_die)
19219 last_die->die_sibling = part_die;
19220
19221 last_die = part_die;
19222
19223 if (first_die == NULL)
19224 first_die = part_die;
19225
19226 /* Maybe add the DIE to the hash table. Not all DIEs that we
19227 find interesting need to be in the hash table, because we
19228 also have the parent/sibling/child chains; only those that we
19229 might refer to by offset later during partial symbol reading.
19230
19231 For now this means things that might have be the target of a
19232 DW_AT_specification, DW_AT_abstract_origin, or
19233 DW_AT_extension. DW_AT_extension will refer only to
19234 namespaces; DW_AT_abstract_origin refers to functions (and
19235 many things under the function DIE, but we do not recurse
19236 into function DIEs during partial symbol reading) and
19237 possibly variables as well; DW_AT_specification refers to
19238 declarations. Declarations ought to have the DW_AT_declaration
19239 flag. It happens that GCC forgets to put it in sometimes, but
19240 only for functions, not for types.
19241
19242 Adding more things than necessary to the hash table is harmless
19243 except for the performance cost. Adding too few will result in
19244 wasted time in find_partial_die, when we reread the compilation
19245 unit with load_all_dies set. */
19246
19247 if (load_all
19248 || abbrev->tag == DW_TAG_constant
19249 || abbrev->tag == DW_TAG_subprogram
19250 || abbrev->tag == DW_TAG_variable
19251 || abbrev->tag == DW_TAG_namespace
19252 || part_die->is_declaration)
19253 {
19254 void **slot;
19255
19256 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19257 to_underlying (part_die->sect_off),
19258 INSERT);
19259 *slot = part_die;
19260 }
19261
19262 /* For some DIEs we want to follow their children (if any). For C
19263 we have no reason to follow the children of structures; for other
19264 languages we have to, so that we can get at method physnames
19265 to infer fully qualified class names, for DW_AT_specification,
19266 and for C++ template arguments. For C++, we also look one level
19267 inside functions to find template arguments (if the name of the
19268 function does not already contain the template arguments).
19269
19270 For Ada and Fortran, we need to scan the children of subprograms
19271 and lexical blocks as well because these languages allow the
19272 definition of nested entities that could be interesting for the
19273 debugger, such as nested subprograms for instance. */
19274 if (last_die->has_children
19275 && (load_all
19276 || last_die->tag == DW_TAG_namespace
19277 || last_die->tag == DW_TAG_module
19278 || last_die->tag == DW_TAG_enumeration_type
19279 || (cu->per_cu->lang == language_cplus
19280 && last_die->tag == DW_TAG_subprogram
19281 && (last_die->raw_name == NULL
19282 || strchr (last_die->raw_name, '<') == NULL))
19283 || (cu->per_cu->lang != language_c
19284 && (last_die->tag == DW_TAG_class_type
19285 || last_die->tag == DW_TAG_interface_type
19286 || last_die->tag == DW_TAG_structure_type
19287 || last_die->tag == DW_TAG_union_type))
19288 || ((cu->per_cu->lang == language_ada
19289 || cu->per_cu->lang == language_fortran)
19290 && (last_die->tag == DW_TAG_subprogram
19291 || last_die->tag == DW_TAG_lexical_block))))
19292 {
19293 nesting_level++;
19294 parent_die = last_die;
19295 continue;
19296 }
19297
19298 /* Otherwise we skip to the next sibling, if any. */
19299 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19300
19301 /* Back to the top, do it again. */
19302 }
19303 }
19304
19305 partial_die_info::partial_die_info (sect_offset sect_off_,
19306 const struct abbrev_info *abbrev)
19307 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19308 {
19309 }
19310
19311 /* See class definition. */
19312
19313 const char *
19314 partial_die_info::name (dwarf2_cu *cu)
19315 {
19316 if (!canonical_name && raw_name != nullptr)
19317 {
19318 struct objfile *objfile = cu->per_objfile->objfile;
19319 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19320 canonical_name = 1;
19321 }
19322
19323 return raw_name;
19324 }
19325
19326 /* Read a minimal amount of information into the minimal die structure.
19327 INFO_PTR should point just after the initial uleb128 of a DIE. */
19328
19329 const gdb_byte *
19330 partial_die_info::read (const struct die_reader_specs *reader,
19331 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19332 {
19333 struct dwarf2_cu *cu = reader->cu;
19334 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19335 unsigned int i;
19336 int has_low_pc_attr = 0;
19337 int has_high_pc_attr = 0;
19338 int high_pc_relative = 0;
19339
19340 for (i = 0; i < abbrev.num_attrs; ++i)
19341 {
19342 attribute attr;
19343 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19344 /* String and address offsets that need to do the reprocessing have
19345 already been read at this point, so there is no need to wait until
19346 the loop terminates to do the reprocessing. */
19347 if (attr.requires_reprocessing_p ())
19348 read_attribute_reprocess (reader, &attr, tag);
19349 /* Store the data if it is of an attribute we want to keep in a
19350 partial symbol table. */
19351 switch (attr.name)
19352 {
19353 case DW_AT_name:
19354 switch (tag)
19355 {
19356 case DW_TAG_compile_unit:
19357 case DW_TAG_partial_unit:
19358 case DW_TAG_type_unit:
19359 /* Compilation units have a DW_AT_name that is a filename, not
19360 a source language identifier. */
19361 case DW_TAG_enumeration_type:
19362 case DW_TAG_enumerator:
19363 /* These tags always have simple identifiers already; no need
19364 to canonicalize them. */
19365 canonical_name = 1;
19366 raw_name = attr.as_string ();
19367 break;
19368 default:
19369 canonical_name = 0;
19370 raw_name = attr.as_string ();
19371 break;
19372 }
19373 break;
19374 case DW_AT_linkage_name:
19375 case DW_AT_MIPS_linkage_name:
19376 /* Note that both forms of linkage name might appear. We
19377 assume they will be the same, and we only store the last
19378 one we see. */
19379 linkage_name = attr.as_string ();
19380 break;
19381 case DW_AT_low_pc:
19382 has_low_pc_attr = 1;
19383 lowpc = attr.as_address ();
19384 break;
19385 case DW_AT_high_pc:
19386 has_high_pc_attr = 1;
19387 highpc = attr.as_address ();
19388 if (cu->header.version >= 4 && attr.form_is_constant ())
19389 high_pc_relative = 1;
19390 break;
19391 case DW_AT_location:
19392 /* Support the .debug_loc offsets. */
19393 if (attr.form_is_block ())
19394 {
19395 d.locdesc = attr.as_block ();
19396 }
19397 else if (attr.form_is_section_offset ())
19398 {
19399 dwarf2_complex_location_expr_complaint ();
19400 }
19401 else
19402 {
19403 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19404 "partial symbol information");
19405 }
19406 break;
19407 case DW_AT_external:
19408 is_external = attr.as_boolean ();
19409 break;
19410 case DW_AT_declaration:
19411 is_declaration = attr.as_boolean ();
19412 break;
19413 case DW_AT_type:
19414 has_type = 1;
19415 break;
19416 case DW_AT_abstract_origin:
19417 case DW_AT_specification:
19418 case DW_AT_extension:
19419 has_specification = 1;
19420 spec_offset = attr.get_ref_die_offset ();
19421 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19422 || cu->per_cu->is_dwz);
19423 break;
19424 case DW_AT_sibling:
19425 /* Ignore absolute siblings, they might point outside of
19426 the current compile unit. */
19427 if (attr.form == DW_FORM_ref_addr)
19428 complaint (_("ignoring absolute DW_AT_sibling"));
19429 else
19430 {
19431 const gdb_byte *buffer = reader->buffer;
19432 sect_offset off = attr.get_ref_die_offset ();
19433 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19434
19435 if (sibling_ptr < info_ptr)
19436 complaint (_("DW_AT_sibling points backwards"));
19437 else if (sibling_ptr > reader->buffer_end)
19438 reader->die_section->overflow_complaint ();
19439 else
19440 sibling = sibling_ptr;
19441 }
19442 break;
19443 case DW_AT_byte_size:
19444 has_byte_size = 1;
19445 break;
19446 case DW_AT_const_value:
19447 has_const_value = 1;
19448 break;
19449 case DW_AT_calling_convention:
19450 /* DWARF doesn't provide a way to identify a program's source-level
19451 entry point. DW_AT_calling_convention attributes are only meant
19452 to describe functions' calling conventions.
19453
19454 However, because it's a necessary piece of information in
19455 Fortran, and before DWARF 4 DW_CC_program was the only
19456 piece of debugging information whose definition refers to
19457 a 'main program' at all, several compilers marked Fortran
19458 main programs with DW_CC_program --- even when those
19459 functions use the standard calling conventions.
19460
19461 Although DWARF now specifies a way to provide this
19462 information, we support this practice for backward
19463 compatibility. */
19464 if (attr.constant_value (0) == DW_CC_program
19465 && cu->per_cu->lang == language_fortran)
19466 main_subprogram = 1;
19467 break;
19468 case DW_AT_inline:
19469 {
19470 LONGEST value = attr.constant_value (-1);
19471 if (value == DW_INL_inlined
19472 || value == DW_INL_declared_inlined)
19473 may_be_inlined = 1;
19474 }
19475 break;
19476
19477 case DW_AT_import:
19478 if (tag == DW_TAG_imported_unit)
19479 {
19480 d.sect_off = attr.get_ref_die_offset ();
19481 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19482 || cu->per_cu->is_dwz);
19483 }
19484 break;
19485
19486 case DW_AT_main_subprogram:
19487 main_subprogram = attr.as_boolean ();
19488 break;
19489
19490 case DW_AT_ranges:
19491 {
19492 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19493 on DWARF version). */
19494 ranges_offset = attr.as_unsigned ();
19495
19496 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19497 this value. */
19498 if (tag != DW_TAG_compile_unit)
19499 ranges_offset += cu->gnu_ranges_base;
19500
19501 has_range_info = 1;
19502 }
19503 break;
19504
19505 default:
19506 break;
19507 }
19508 }
19509
19510 /* For Ada, if both the name and the linkage name appear, we prefer
19511 the latter. This lets "catch exception" work better, regardless
19512 of the order in which the name and linkage name were emitted.
19513 Really, though, this is just a workaround for the fact that gdb
19514 doesn't store both the name and the linkage name. */
19515 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
19516 raw_name = linkage_name;
19517
19518 if (high_pc_relative)
19519 highpc += lowpc;
19520
19521 if (has_low_pc_attr && has_high_pc_attr)
19522 {
19523 /* When using the GNU linker, .gnu.linkonce. sections are used to
19524 eliminate duplicate copies of functions and vtables and such.
19525 The linker will arbitrarily choose one and discard the others.
19526 The AT_*_pc values for such functions refer to local labels in
19527 these sections. If the section from that file was discarded, the
19528 labels are not in the output, so the relocs get a value of 0.
19529 If this is a discarded function, mark the pc bounds as invalid,
19530 so that GDB will ignore it. */
19531 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19532 {
19533 struct objfile *objfile = per_objfile->objfile;
19534 struct gdbarch *gdbarch = objfile->arch ();
19535
19536 complaint (_("DW_AT_low_pc %s is zero "
19537 "for DIE at %s [in module %s]"),
19538 paddress (gdbarch, lowpc),
19539 sect_offset_str (sect_off),
19540 objfile_name (objfile));
19541 }
19542 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19543 else if (lowpc >= highpc)
19544 {
19545 struct objfile *objfile = per_objfile->objfile;
19546 struct gdbarch *gdbarch = objfile->arch ();
19547
19548 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19549 "for DIE at %s [in module %s]"),
19550 paddress (gdbarch, lowpc),
19551 paddress (gdbarch, highpc),
19552 sect_offset_str (sect_off),
19553 objfile_name (objfile));
19554 }
19555 else
19556 has_pc_info = 1;
19557 }
19558
19559 return info_ptr;
19560 }
19561
19562 /* Find a cached partial DIE at OFFSET in CU. */
19563
19564 struct partial_die_info *
19565 dwarf2_cu::find_partial_die (sect_offset sect_off)
19566 {
19567 struct partial_die_info *lookup_die = NULL;
19568 struct partial_die_info part_die (sect_off);
19569
19570 lookup_die = ((struct partial_die_info *)
19571 htab_find_with_hash (partial_dies, &part_die,
19572 to_underlying (sect_off)));
19573
19574 return lookup_die;
19575 }
19576
19577 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19578 except in the case of .debug_types DIEs which do not reference
19579 outside their CU (they do however referencing other types via
19580 DW_FORM_ref_sig8). */
19581
19582 static const struct cu_partial_die_info
19583 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19584 {
19585 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19586 struct objfile *objfile = per_objfile->objfile;
19587 struct partial_die_info *pd = NULL;
19588
19589 if (offset_in_dwz == cu->per_cu->is_dwz
19590 && cu->header.offset_in_cu_p (sect_off))
19591 {
19592 pd = cu->find_partial_die (sect_off);
19593 if (pd != NULL)
19594 return { cu, pd };
19595 /* We missed recording what we needed.
19596 Load all dies and try again. */
19597 }
19598 else
19599 {
19600 /* TUs don't reference other CUs/TUs (except via type signatures). */
19601 if (cu->per_cu->is_debug_types)
19602 {
19603 error (_("Dwarf Error: Type Unit at offset %s contains"
19604 " external reference to offset %s [in module %s].\n"),
19605 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19606 bfd_get_filename (objfile->obfd));
19607 }
19608 dwarf2_per_cu_data *per_cu
19609 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19610 per_objfile->per_bfd);
19611
19612 cu = per_objfile->get_cu (per_cu);
19613 if (cu == NULL || cu->partial_dies == NULL)
19614 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19615
19616 cu = per_objfile->get_cu (per_cu);
19617
19618 cu->last_used = 0;
19619 pd = cu->find_partial_die (sect_off);
19620 }
19621
19622 /* If we didn't find it, and not all dies have been loaded,
19623 load them all and try again. */
19624
19625 if (pd == NULL && cu->load_all_dies == 0)
19626 {
19627 cu->load_all_dies = 1;
19628
19629 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19630 THIS_CU->cu may already be in use. So we can't just free it and
19631 replace its DIEs with the ones we read in. Instead, we leave those
19632 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19633 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19634 set. */
19635 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19636
19637 pd = cu->find_partial_die (sect_off);
19638 }
19639
19640 if (pd == NULL)
19641 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19642 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19643 return { cu, pd };
19644 }
19645
19646 /* See if we can figure out if the class lives in a namespace. We do
19647 this by looking for a member function; its demangled name will
19648 contain namespace info, if there is any. */
19649
19650 static void
19651 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19652 struct dwarf2_cu *cu)
19653 {
19654 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19655 what template types look like, because the demangler
19656 frequently doesn't give the same name as the debug info. We
19657 could fix this by only using the demangled name to get the
19658 prefix (but see comment in read_structure_type). */
19659
19660 struct partial_die_info *real_pdi;
19661 struct partial_die_info *child_pdi;
19662
19663 /* If this DIE (this DIE's specification, if any) has a parent, then
19664 we should not do this. We'll prepend the parent's fully qualified
19665 name when we create the partial symbol. */
19666
19667 real_pdi = struct_pdi;
19668 while (real_pdi->has_specification)
19669 {
19670 auto res = find_partial_die (real_pdi->spec_offset,
19671 real_pdi->spec_is_dwz, cu);
19672 real_pdi = res.pdi;
19673 cu = res.cu;
19674 }
19675
19676 if (real_pdi->die_parent != NULL)
19677 return;
19678
19679 for (child_pdi = struct_pdi->die_child;
19680 child_pdi != NULL;
19681 child_pdi = child_pdi->die_sibling)
19682 {
19683 if (child_pdi->tag == DW_TAG_subprogram
19684 && child_pdi->linkage_name != NULL)
19685 {
19686 gdb::unique_xmalloc_ptr<char> actual_class_name
19687 (cu->language_defn->class_name_from_physname
19688 (child_pdi->linkage_name));
19689 if (actual_class_name != NULL)
19690 {
19691 struct objfile *objfile = cu->per_objfile->objfile;
19692 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19693 struct_pdi->canonical_name = 1;
19694 }
19695 break;
19696 }
19697 }
19698 }
19699
19700 /* Return true if a DIE with TAG may have the DW_AT_const_value
19701 attribute. */
19702
19703 static bool
19704 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19705 {
19706 switch (tag)
19707 {
19708 case DW_TAG_constant:
19709 case DW_TAG_enumerator:
19710 case DW_TAG_formal_parameter:
19711 case DW_TAG_template_value_param:
19712 case DW_TAG_variable:
19713 return true;
19714 }
19715
19716 return false;
19717 }
19718
19719 void
19720 partial_die_info::fixup (struct dwarf2_cu *cu)
19721 {
19722 /* Once we've fixed up a die, there's no point in doing so again.
19723 This also avoids a memory leak if we were to call
19724 guess_partial_die_structure_name multiple times. */
19725 if (fixup_called)
19726 return;
19727
19728 /* If we found a reference attribute and the DIE has no name, try
19729 to find a name in the referred to DIE. */
19730
19731 if (raw_name == NULL && has_specification)
19732 {
19733 struct partial_die_info *spec_die;
19734
19735 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19736 spec_die = res.pdi;
19737 cu = res.cu;
19738
19739 spec_die->fixup (cu);
19740
19741 if (spec_die->raw_name)
19742 {
19743 raw_name = spec_die->raw_name;
19744 canonical_name = spec_die->canonical_name;
19745
19746 /* Copy DW_AT_external attribute if it is set. */
19747 if (spec_die->is_external)
19748 is_external = spec_die->is_external;
19749 }
19750 }
19751
19752 if (!has_const_value && has_specification
19753 && can_have_DW_AT_const_value_p (tag))
19754 {
19755 struct partial_die_info *spec_die;
19756
19757 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19758 spec_die = res.pdi;
19759 cu = res.cu;
19760
19761 spec_die->fixup (cu);
19762
19763 if (spec_die->has_const_value)
19764 {
19765 /* Copy DW_AT_const_value attribute if it is set. */
19766 has_const_value = spec_die->has_const_value;
19767 }
19768 }
19769
19770 /* Set default names for some unnamed DIEs. */
19771
19772 if (raw_name == NULL && tag == DW_TAG_namespace)
19773 {
19774 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19775 canonical_name = 1;
19776 }
19777
19778 /* If there is no parent die to provide a namespace, and there are
19779 children, see if we can determine the namespace from their linkage
19780 name. */
19781 if (cu->per_cu->lang == language_cplus
19782 && !cu->per_objfile->per_bfd->types.empty ()
19783 && die_parent == NULL
19784 && has_children
19785 && (tag == DW_TAG_class_type
19786 || tag == DW_TAG_structure_type
19787 || tag == DW_TAG_union_type))
19788 guess_partial_die_structure_name (this, cu);
19789
19790 /* GCC might emit a nameless struct or union that has a linkage
19791 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19792 if (raw_name == NULL
19793 && (tag == DW_TAG_class_type
19794 || tag == DW_TAG_interface_type
19795 || tag == DW_TAG_structure_type
19796 || tag == DW_TAG_union_type)
19797 && linkage_name != NULL)
19798 {
19799 gdb::unique_xmalloc_ptr<char> demangled
19800 (gdb_demangle (linkage_name, DMGL_TYPES));
19801 if (demangled != nullptr)
19802 {
19803 const char *base;
19804
19805 /* Strip any leading namespaces/classes, keep only the base name.
19806 DW_AT_name for named DIEs does not contain the prefixes. */
19807 base = strrchr (demangled.get (), ':');
19808 if (base && base > demangled.get () && base[-1] == ':')
19809 base++;
19810 else
19811 base = demangled.get ();
19812
19813 struct objfile *objfile = cu->per_objfile->objfile;
19814 raw_name = objfile->intern (base);
19815 canonical_name = 1;
19816 }
19817 }
19818
19819 fixup_called = 1;
19820 }
19821
19822 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19823 contents from the given SECTION in the HEADER.
19824
19825 HEADER_OFFSET is the offset of the header in the section. */
19826 static void
19827 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19828 struct dwarf2_section_info *section,
19829 sect_offset header_offset)
19830 {
19831 unsigned int bytes_read;
19832 bfd *abfd = section->get_bfd_owner ();
19833 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19834
19835 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19836 info_ptr += bytes_read;
19837
19838 header->version = read_2_bytes (abfd, info_ptr);
19839 info_ptr += 2;
19840
19841 header->addr_size = read_1_byte (abfd, info_ptr);
19842 info_ptr += 1;
19843
19844 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19845 info_ptr += 1;
19846
19847 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19848 }
19849
19850 /* Return the DW_AT_loclists_base value for the CU. */
19851 static ULONGEST
19852 lookup_loclist_base (struct dwarf2_cu *cu)
19853 {
19854 /* For the .dwo unit, the loclist_base points to the first offset following
19855 the header. The header consists of the following entities-
19856 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19857 bit format)
19858 2. version (2 bytes)
19859 3. address size (1 byte)
19860 4. segment selector size (1 byte)
19861 5. offset entry count (4 bytes)
19862 These sizes are derived as per the DWARFv5 standard. */
19863 if (cu->dwo_unit != nullptr)
19864 {
19865 if (cu->header.initial_length_size == 4)
19866 return LOCLIST_HEADER_SIZE32;
19867 return LOCLIST_HEADER_SIZE64;
19868 }
19869 return cu->loclist_base;
19870 }
19871
19872 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19873 array of offsets in the .debug_loclists section. */
19874
19875 static sect_offset
19876 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19877 {
19878 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19879 struct objfile *objfile = per_objfile->objfile;
19880 bfd *abfd = objfile->obfd;
19881 ULONGEST loclist_header_size =
19882 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19883 : LOCLIST_HEADER_SIZE64);
19884 ULONGEST loclist_base = lookup_loclist_base (cu);
19885
19886 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19887 ULONGEST start_offset =
19888 loclist_base + loclist_index * cu->header.offset_size;
19889
19890 /* Get loclists section. */
19891 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19892
19893 /* Read the loclists section content. */
19894 section->read (objfile);
19895 if (section->buffer == NULL)
19896 error (_("DW_FORM_loclistx used without .debug_loclists "
19897 "section [in module %s]"), objfile_name (objfile));
19898
19899 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19900 so if loclist_base is smaller than the header size, we have a problem. */
19901 if (loclist_base < loclist_header_size)
19902 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19903 objfile_name (objfile));
19904
19905 /* Read the header of the loclists contribution. */
19906 struct loclists_rnglists_header header;
19907 read_loclists_rnglists_header (&header, section,
19908 (sect_offset) (loclist_base - loclist_header_size));
19909
19910 /* Verify the loclist index is valid. */
19911 if (loclist_index >= header.offset_entry_count)
19912 error (_("DW_FORM_loclistx pointing outside of "
19913 ".debug_loclists offset array [in module %s]"),
19914 objfile_name (objfile));
19915
19916 /* Validate that reading won't go beyond the end of the section. */
19917 if (start_offset + cu->header.offset_size > section->size)
19918 error (_("Reading DW_FORM_loclistx index beyond end of"
19919 ".debug_loclists section [in module %s]"),
19920 objfile_name (objfile));
19921
19922 const gdb_byte *info_ptr = section->buffer + start_offset;
19923
19924 if (cu->header.offset_size == 4)
19925 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19926 else
19927 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19928 }
19929
19930 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19931 array of offsets in the .debug_rnglists section. */
19932
19933 static sect_offset
19934 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19935 dwarf_tag tag)
19936 {
19937 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19938 struct objfile *objfile = dwarf2_per_objfile->objfile;
19939 bfd *abfd = objfile->obfd;
19940 ULONGEST rnglist_header_size =
19941 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19942 : RNGLIST_HEADER_SIZE64);
19943
19944 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19945 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19946 doesn't apply. */
19947 ULONGEST rnglist_base =
19948 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19949
19950 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19951 ULONGEST start_offset =
19952 rnglist_base + rnglist_index * cu->header.offset_size;
19953
19954 /* Get rnglists section. */
19955 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19956
19957 /* Read the rnglists section content. */
19958 section->read (objfile);
19959 if (section->buffer == nullptr)
19960 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19961 "[in module %s]"),
19962 objfile_name (objfile));
19963
19964 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19965 so if rnglist_base is smaller than the header size, we have a problem. */
19966 if (rnglist_base < rnglist_header_size)
19967 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19968 objfile_name (objfile));
19969
19970 /* Read the header of the rnglists contribution. */
19971 struct loclists_rnglists_header header;
19972 read_loclists_rnglists_header (&header, section,
19973 (sect_offset) (rnglist_base - rnglist_header_size));
19974
19975 /* Verify the rnglist index is valid. */
19976 if (rnglist_index >= header.offset_entry_count)
19977 error (_("DW_FORM_rnglistx index pointing outside of "
19978 ".debug_rnglists offset array [in module %s]"),
19979 objfile_name (objfile));
19980
19981 /* Validate that reading won't go beyond the end of the section. */
19982 if (start_offset + cu->header.offset_size > section->size)
19983 error (_("Reading DW_FORM_rnglistx index beyond end of"
19984 ".debug_rnglists section [in module %s]"),
19985 objfile_name (objfile));
19986
19987 const gdb_byte *info_ptr = section->buffer + start_offset;
19988
19989 if (cu->header.offset_size == 4)
19990 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19991 else
19992 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19993 }
19994
19995 /* Process the attributes that had to be skipped in the first round. These
19996 attributes are the ones that need str_offsets_base or addr_base attributes.
19997 They could not have been processed in the first round, because at the time
19998 the values of str_offsets_base or addr_base may not have been known. */
19999 static void
20000 read_attribute_reprocess (const struct die_reader_specs *reader,
20001 struct attribute *attr, dwarf_tag tag)
20002 {
20003 struct dwarf2_cu *cu = reader->cu;
20004 switch (attr->form)
20005 {
20006 case DW_FORM_addrx:
20007 case DW_FORM_GNU_addr_index:
20008 attr->set_address (read_addr_index (cu,
20009 attr->as_unsigned_reprocess ()));
20010 break;
20011 case DW_FORM_loclistx:
20012 {
20013 sect_offset loclists_sect_off
20014 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20015
20016 attr->set_unsigned (to_underlying (loclists_sect_off));
20017 }
20018 break;
20019 case DW_FORM_rnglistx:
20020 {
20021 sect_offset rnglists_sect_off
20022 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20023
20024 attr->set_unsigned (to_underlying (rnglists_sect_off));
20025 }
20026 break;
20027 case DW_FORM_strx:
20028 case DW_FORM_strx1:
20029 case DW_FORM_strx2:
20030 case DW_FORM_strx3:
20031 case DW_FORM_strx4:
20032 case DW_FORM_GNU_str_index:
20033 {
20034 unsigned int str_index = attr->as_unsigned_reprocess ();
20035 gdb_assert (!attr->canonical_string_p ());
20036 if (reader->dwo_file != NULL)
20037 attr->set_string_noncanonical (read_dwo_str_index (reader,
20038 str_index));
20039 else
20040 attr->set_string_noncanonical (read_stub_str_index (cu,
20041 str_index));
20042 break;
20043 }
20044 default:
20045 gdb_assert_not_reached ("Unexpected DWARF form.");
20046 }
20047 }
20048
20049 /* Read an attribute value described by an attribute form. */
20050
20051 static const gdb_byte *
20052 read_attribute_value (const struct die_reader_specs *reader,
20053 struct attribute *attr, unsigned form,
20054 LONGEST implicit_const, const gdb_byte *info_ptr)
20055 {
20056 struct dwarf2_cu *cu = reader->cu;
20057 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20058 struct objfile *objfile = per_objfile->objfile;
20059 bfd *abfd = reader->abfd;
20060 struct comp_unit_head *cu_header = &cu->header;
20061 unsigned int bytes_read;
20062 struct dwarf_block *blk;
20063
20064 attr->form = (enum dwarf_form) form;
20065 switch (form)
20066 {
20067 case DW_FORM_ref_addr:
20068 if (cu_header->version == 2)
20069 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20070 &bytes_read));
20071 else
20072 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20073 &bytes_read));
20074 info_ptr += bytes_read;
20075 break;
20076 case DW_FORM_GNU_ref_alt:
20077 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20078 &bytes_read));
20079 info_ptr += bytes_read;
20080 break;
20081 case DW_FORM_addr:
20082 {
20083 struct gdbarch *gdbarch = objfile->arch ();
20084 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20085 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20086 attr->set_address (addr);
20087 info_ptr += bytes_read;
20088 }
20089 break;
20090 case DW_FORM_block2:
20091 blk = dwarf_alloc_block (cu);
20092 blk->size = read_2_bytes (abfd, info_ptr);
20093 info_ptr += 2;
20094 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20095 info_ptr += blk->size;
20096 attr->set_block (blk);
20097 break;
20098 case DW_FORM_block4:
20099 blk = dwarf_alloc_block (cu);
20100 blk->size = read_4_bytes (abfd, info_ptr);
20101 info_ptr += 4;
20102 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20103 info_ptr += blk->size;
20104 attr->set_block (blk);
20105 break;
20106 case DW_FORM_data2:
20107 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20108 info_ptr += 2;
20109 break;
20110 case DW_FORM_data4:
20111 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20112 info_ptr += 4;
20113 break;
20114 case DW_FORM_data8:
20115 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20116 info_ptr += 8;
20117 break;
20118 case DW_FORM_data16:
20119 blk = dwarf_alloc_block (cu);
20120 blk->size = 16;
20121 blk->data = read_n_bytes (abfd, info_ptr, 16);
20122 info_ptr += 16;
20123 attr->set_block (blk);
20124 break;
20125 case DW_FORM_sec_offset:
20126 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20127 &bytes_read));
20128 info_ptr += bytes_read;
20129 break;
20130 case DW_FORM_loclistx:
20131 {
20132 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20133 &bytes_read));
20134 info_ptr += bytes_read;
20135 }
20136 break;
20137 case DW_FORM_string:
20138 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20139 &bytes_read));
20140 info_ptr += bytes_read;
20141 break;
20142 case DW_FORM_strp:
20143 if (!cu->per_cu->is_dwz)
20144 {
20145 attr->set_string_noncanonical
20146 (read_indirect_string (per_objfile,
20147 abfd, info_ptr, cu_header,
20148 &bytes_read));
20149 info_ptr += bytes_read;
20150 break;
20151 }
20152 /* FALLTHROUGH */
20153 case DW_FORM_line_strp:
20154 if (!cu->per_cu->is_dwz)
20155 {
20156 attr->set_string_noncanonical
20157 (per_objfile->read_line_string (info_ptr, cu_header,
20158 &bytes_read));
20159 info_ptr += bytes_read;
20160 break;
20161 }
20162 /* FALLTHROUGH */
20163 case DW_FORM_GNU_strp_alt:
20164 {
20165 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20166 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20167 &bytes_read);
20168
20169 attr->set_string_noncanonical
20170 (dwz->read_string (objfile, str_offset));
20171 info_ptr += bytes_read;
20172 }
20173 break;
20174 case DW_FORM_exprloc:
20175 case DW_FORM_block:
20176 blk = dwarf_alloc_block (cu);
20177 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20178 info_ptr += bytes_read;
20179 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20180 info_ptr += blk->size;
20181 attr->set_block (blk);
20182 break;
20183 case DW_FORM_block1:
20184 blk = dwarf_alloc_block (cu);
20185 blk->size = read_1_byte (abfd, info_ptr);
20186 info_ptr += 1;
20187 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20188 info_ptr += blk->size;
20189 attr->set_block (blk);
20190 break;
20191 case DW_FORM_data1:
20192 case DW_FORM_flag:
20193 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20194 info_ptr += 1;
20195 break;
20196 case DW_FORM_flag_present:
20197 attr->set_unsigned (1);
20198 break;
20199 case DW_FORM_sdata:
20200 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20201 info_ptr += bytes_read;
20202 break;
20203 case DW_FORM_rnglistx:
20204 {
20205 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20206 &bytes_read));
20207 info_ptr += bytes_read;
20208 }
20209 break;
20210 case DW_FORM_udata:
20211 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20212 info_ptr += bytes_read;
20213 break;
20214 case DW_FORM_ref1:
20215 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20216 + read_1_byte (abfd, info_ptr)));
20217 info_ptr += 1;
20218 break;
20219 case DW_FORM_ref2:
20220 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20221 + read_2_bytes (abfd, info_ptr)));
20222 info_ptr += 2;
20223 break;
20224 case DW_FORM_ref4:
20225 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20226 + read_4_bytes (abfd, info_ptr)));
20227 info_ptr += 4;
20228 break;
20229 case DW_FORM_ref8:
20230 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20231 + read_8_bytes (abfd, info_ptr)));
20232 info_ptr += 8;
20233 break;
20234 case DW_FORM_ref_sig8:
20235 attr->set_signature (read_8_bytes (abfd, info_ptr));
20236 info_ptr += 8;
20237 break;
20238 case DW_FORM_ref_udata:
20239 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20240 + read_unsigned_leb128 (abfd, info_ptr,
20241 &bytes_read)));
20242 info_ptr += bytes_read;
20243 break;
20244 case DW_FORM_indirect:
20245 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20246 info_ptr += bytes_read;
20247 if (form == DW_FORM_implicit_const)
20248 {
20249 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20250 info_ptr += bytes_read;
20251 }
20252 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20253 info_ptr);
20254 break;
20255 case DW_FORM_implicit_const:
20256 attr->set_signed (implicit_const);
20257 break;
20258 case DW_FORM_addrx:
20259 case DW_FORM_GNU_addr_index:
20260 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20261 &bytes_read));
20262 info_ptr += bytes_read;
20263 break;
20264 case DW_FORM_strx:
20265 case DW_FORM_strx1:
20266 case DW_FORM_strx2:
20267 case DW_FORM_strx3:
20268 case DW_FORM_strx4:
20269 case DW_FORM_GNU_str_index:
20270 {
20271 ULONGEST str_index;
20272 if (form == DW_FORM_strx1)
20273 {
20274 str_index = read_1_byte (abfd, info_ptr);
20275 info_ptr += 1;
20276 }
20277 else if (form == DW_FORM_strx2)
20278 {
20279 str_index = read_2_bytes (abfd, info_ptr);
20280 info_ptr += 2;
20281 }
20282 else if (form == DW_FORM_strx3)
20283 {
20284 str_index = read_3_bytes (abfd, info_ptr);
20285 info_ptr += 3;
20286 }
20287 else if (form == DW_FORM_strx4)
20288 {
20289 str_index = read_4_bytes (abfd, info_ptr);
20290 info_ptr += 4;
20291 }
20292 else
20293 {
20294 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20295 info_ptr += bytes_read;
20296 }
20297 attr->set_unsigned_reprocess (str_index);
20298 }
20299 break;
20300 default:
20301 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20302 dwarf_form_name (form),
20303 bfd_get_filename (abfd));
20304 }
20305
20306 /* Super hack. */
20307 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20308 attr->form = DW_FORM_GNU_ref_alt;
20309
20310 /* We have seen instances where the compiler tried to emit a byte
20311 size attribute of -1 which ended up being encoded as an unsigned
20312 0xffffffff. Although 0xffffffff is technically a valid size value,
20313 an object of this size seems pretty unlikely so we can relatively
20314 safely treat these cases as if the size attribute was invalid and
20315 treat them as zero by default. */
20316 if (attr->name == DW_AT_byte_size
20317 && form == DW_FORM_data4
20318 && attr->as_unsigned () >= 0xffffffff)
20319 {
20320 complaint
20321 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20322 hex_string (attr->as_unsigned ()));
20323 attr->set_unsigned (0);
20324 }
20325
20326 return info_ptr;
20327 }
20328
20329 /* Read an attribute described by an abbreviated attribute. */
20330
20331 static const gdb_byte *
20332 read_attribute (const struct die_reader_specs *reader,
20333 struct attribute *attr, const struct attr_abbrev *abbrev,
20334 const gdb_byte *info_ptr)
20335 {
20336 attr->name = abbrev->name;
20337 attr->string_is_canonical = 0;
20338 attr->requires_reprocessing = 0;
20339 return read_attribute_value (reader, attr, abbrev->form,
20340 abbrev->implicit_const, info_ptr);
20341 }
20342
20343 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20344
20345 static const char *
20346 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20347 LONGEST str_offset)
20348 {
20349 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20350 str_offset, "DW_FORM_strp");
20351 }
20352
20353 /* Return pointer to string at .debug_str offset as read from BUF.
20354 BUF is assumed to be in a compilation unit described by CU_HEADER.
20355 Return *BYTES_READ_PTR count of bytes read from BUF. */
20356
20357 static const char *
20358 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20359 const gdb_byte *buf,
20360 const struct comp_unit_head *cu_header,
20361 unsigned int *bytes_read_ptr)
20362 {
20363 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20364
20365 return read_indirect_string_at_offset (per_objfile, str_offset);
20366 }
20367
20368 /* See read.h. */
20369
20370 const char *
20371 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20372 unsigned int offset_size)
20373 {
20374 bfd *abfd = objfile->obfd;
20375 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
20376
20377 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20378 }
20379
20380 /* See read.h. */
20381
20382 const char *
20383 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20384 const struct comp_unit_head *cu_header,
20385 unsigned int *bytes_read_ptr)
20386 {
20387 bfd *abfd = objfile->obfd;
20388 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20389
20390 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20391 }
20392
20393 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20394 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20395 ADDR_SIZE is the size of addresses from the CU header. */
20396
20397 static CORE_ADDR
20398 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20399 gdb::optional<ULONGEST> addr_base, int addr_size)
20400 {
20401 struct objfile *objfile = per_objfile->objfile;
20402 bfd *abfd = objfile->obfd;
20403 const gdb_byte *info_ptr;
20404 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20405
20406 per_objfile->per_bfd->addr.read (objfile);
20407 if (per_objfile->per_bfd->addr.buffer == NULL)
20408 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20409 objfile_name (objfile));
20410 if (addr_base_or_zero + addr_index * addr_size
20411 >= per_objfile->per_bfd->addr.size)
20412 error (_("DW_FORM_addr_index pointing outside of "
20413 ".debug_addr section [in module %s]"),
20414 objfile_name (objfile));
20415 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20416 + addr_index * addr_size);
20417 if (addr_size == 4)
20418 return bfd_get_32 (abfd, info_ptr);
20419 else
20420 return bfd_get_64 (abfd, info_ptr);
20421 }
20422
20423 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20424
20425 static CORE_ADDR
20426 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20427 {
20428 return read_addr_index_1 (cu->per_objfile, addr_index,
20429 cu->addr_base, cu->header.addr_size);
20430 }
20431
20432 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20433
20434 static CORE_ADDR
20435 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20436 unsigned int *bytes_read)
20437 {
20438 bfd *abfd = cu->per_objfile->objfile->obfd;
20439 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20440
20441 return read_addr_index (cu, addr_index);
20442 }
20443
20444 /* See read.h. */
20445
20446 CORE_ADDR
20447 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20448 dwarf2_per_objfile *per_objfile,
20449 unsigned int addr_index)
20450 {
20451 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20452 gdb::optional<ULONGEST> addr_base;
20453 int addr_size;
20454
20455 /* We need addr_base and addr_size.
20456 If we don't have PER_CU->cu, we have to get it.
20457 Nasty, but the alternative is storing the needed info in PER_CU,
20458 which at this point doesn't seem justified: it's not clear how frequently
20459 it would get used and it would increase the size of every PER_CU.
20460 Entry points like dwarf2_per_cu_addr_size do a similar thing
20461 so we're not in uncharted territory here.
20462 Alas we need to be a bit more complicated as addr_base is contained
20463 in the DIE.
20464
20465 We don't need to read the entire CU(/TU).
20466 We just need the header and top level die.
20467
20468 IWBN to use the aging mechanism to let us lazily later discard the CU.
20469 For now we skip this optimization. */
20470
20471 if (cu != NULL)
20472 {
20473 addr_base = cu->addr_base;
20474 addr_size = cu->header.addr_size;
20475 }
20476 else
20477 {
20478 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20479 addr_base = reader.cu->addr_base;
20480 addr_size = reader.cu->header.addr_size;
20481 }
20482
20483 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20484 }
20485
20486 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20487 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20488 DWO file. */
20489
20490 static const char *
20491 read_str_index (struct dwarf2_cu *cu,
20492 struct dwarf2_section_info *str_section,
20493 struct dwarf2_section_info *str_offsets_section,
20494 ULONGEST str_offsets_base, ULONGEST str_index)
20495 {
20496 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20497 struct objfile *objfile = per_objfile->objfile;
20498 const char *objf_name = objfile_name (objfile);
20499 bfd *abfd = objfile->obfd;
20500 const gdb_byte *info_ptr;
20501 ULONGEST str_offset;
20502 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20503
20504 str_section->read (objfile);
20505 str_offsets_section->read (objfile);
20506 if (str_section->buffer == NULL)
20507 error (_("%s used without %s section"
20508 " in CU at offset %s [in module %s]"),
20509 form_name, str_section->get_name (),
20510 sect_offset_str (cu->header.sect_off), objf_name);
20511 if (str_offsets_section->buffer == NULL)
20512 error (_("%s used without %s section"
20513 " in CU at offset %s [in module %s]"),
20514 form_name, str_section->get_name (),
20515 sect_offset_str (cu->header.sect_off), objf_name);
20516 info_ptr = (str_offsets_section->buffer
20517 + str_offsets_base
20518 + str_index * cu->header.offset_size);
20519 if (cu->header.offset_size == 4)
20520 str_offset = bfd_get_32 (abfd, info_ptr);
20521 else
20522 str_offset = bfd_get_64 (abfd, info_ptr);
20523 if (str_offset >= str_section->size)
20524 error (_("Offset from %s pointing outside of"
20525 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20526 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20527 return (const char *) (str_section->buffer + str_offset);
20528 }
20529
20530 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20531
20532 static const char *
20533 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20534 {
20535 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20536 ? reader->cu->header.addr_size : 0;
20537 return read_str_index (reader->cu,
20538 &reader->dwo_file->sections.str,
20539 &reader->dwo_file->sections.str_offsets,
20540 str_offsets_base, str_index);
20541 }
20542
20543 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20544
20545 static const char *
20546 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20547 {
20548 struct objfile *objfile = cu->per_objfile->objfile;
20549 const char *objf_name = objfile_name (objfile);
20550 static const char form_name[] = "DW_FORM_GNU_str_index";
20551 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20552
20553 if (!cu->str_offsets_base.has_value ())
20554 error (_("%s used in Fission stub without %s"
20555 " in CU at offset 0x%lx [in module %s]"),
20556 form_name, str_offsets_attr_name,
20557 (long) cu->header.offset_size, objf_name);
20558
20559 return read_str_index (cu,
20560 &cu->per_objfile->per_bfd->str,
20561 &cu->per_objfile->per_bfd->str_offsets,
20562 *cu->str_offsets_base, str_index);
20563 }
20564
20565 /* Return the length of an LEB128 number in BUF. */
20566
20567 static int
20568 leb128_size (const gdb_byte *buf)
20569 {
20570 const gdb_byte *begin = buf;
20571 gdb_byte byte;
20572
20573 while (1)
20574 {
20575 byte = *buf++;
20576 if ((byte & 128) == 0)
20577 return buf - begin;
20578 }
20579 }
20580
20581 static enum language
20582 dwarf_lang_to_enum_language (unsigned int lang)
20583 {
20584 enum language language;
20585
20586 switch (lang)
20587 {
20588 case DW_LANG_C89:
20589 case DW_LANG_C99:
20590 case DW_LANG_C11:
20591 case DW_LANG_C:
20592 case DW_LANG_UPC:
20593 language = language_c;
20594 break;
20595 case DW_LANG_Java:
20596 case DW_LANG_C_plus_plus:
20597 case DW_LANG_C_plus_plus_11:
20598 case DW_LANG_C_plus_plus_14:
20599 language = language_cplus;
20600 break;
20601 case DW_LANG_D:
20602 language = language_d;
20603 break;
20604 case DW_LANG_Fortran77:
20605 case DW_LANG_Fortran90:
20606 case DW_LANG_Fortran95:
20607 case DW_LANG_Fortran03:
20608 case DW_LANG_Fortran08:
20609 language = language_fortran;
20610 break;
20611 case DW_LANG_Go:
20612 language = language_go;
20613 break;
20614 case DW_LANG_Mips_Assembler:
20615 language = language_asm;
20616 break;
20617 case DW_LANG_Ada83:
20618 case DW_LANG_Ada95:
20619 language = language_ada;
20620 break;
20621 case DW_LANG_Modula2:
20622 language = language_m2;
20623 break;
20624 case DW_LANG_Pascal83:
20625 language = language_pascal;
20626 break;
20627 case DW_LANG_ObjC:
20628 language = language_objc;
20629 break;
20630 case DW_LANG_Rust:
20631 case DW_LANG_Rust_old:
20632 language = language_rust;
20633 break;
20634 case DW_LANG_OpenCL:
20635 language = language_opencl;
20636 break;
20637 case DW_LANG_Cobol74:
20638 case DW_LANG_Cobol85:
20639 default:
20640 language = language_minimal;
20641 break;
20642 }
20643
20644 return language;
20645 }
20646
20647 /* Return the named attribute or NULL if not there. */
20648
20649 static struct attribute *
20650 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20651 {
20652 for (;;)
20653 {
20654 unsigned int i;
20655 struct attribute *spec = NULL;
20656
20657 for (i = 0; i < die->num_attrs; ++i)
20658 {
20659 if (die->attrs[i].name == name)
20660 return &die->attrs[i];
20661 if (die->attrs[i].name == DW_AT_specification
20662 || die->attrs[i].name == DW_AT_abstract_origin)
20663 spec = &die->attrs[i];
20664 }
20665
20666 if (!spec)
20667 break;
20668
20669 die = follow_die_ref (die, spec, &cu);
20670 }
20671
20672 return NULL;
20673 }
20674
20675 /* Return the string associated with a string-typed attribute, or NULL if it
20676 is either not found or is of an incorrect type. */
20677
20678 static const char *
20679 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20680 {
20681 struct attribute *attr;
20682 const char *str = NULL;
20683
20684 attr = dwarf2_attr (die, name, cu);
20685
20686 if (attr != NULL)
20687 {
20688 str = attr->as_string ();
20689 if (str == nullptr)
20690 complaint (_("string type expected for attribute %s for "
20691 "DIE at %s in module %s"),
20692 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20693 objfile_name (cu->per_objfile->objfile));
20694 }
20695
20696 return str;
20697 }
20698
20699 /* Return the dwo name or NULL if not present. If present, it is in either
20700 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20701 static const char *
20702 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20703 {
20704 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20705 if (dwo_name == nullptr)
20706 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20707 return dwo_name;
20708 }
20709
20710 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20711 and holds a non-zero value. This function should only be used for
20712 DW_FORM_flag or DW_FORM_flag_present attributes. */
20713
20714 static int
20715 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20716 {
20717 struct attribute *attr = dwarf2_attr (die, name, cu);
20718
20719 return attr != nullptr && attr->as_boolean ();
20720 }
20721
20722 static int
20723 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20724 {
20725 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20726 which value is non-zero. However, we have to be careful with
20727 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20728 (via dwarf2_flag_true_p) follows this attribute. So we may
20729 end up accidently finding a declaration attribute that belongs
20730 to a different DIE referenced by the specification attribute,
20731 even though the given DIE does not have a declaration attribute. */
20732 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20733 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20734 }
20735
20736 /* Return the die giving the specification for DIE, if there is
20737 one. *SPEC_CU is the CU containing DIE on input, and the CU
20738 containing the return value on output. If there is no
20739 specification, but there is an abstract origin, that is
20740 returned. */
20741
20742 static struct die_info *
20743 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20744 {
20745 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20746 *spec_cu);
20747
20748 if (spec_attr == NULL)
20749 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20750
20751 if (spec_attr == NULL)
20752 return NULL;
20753 else
20754 return follow_die_ref (die, spec_attr, spec_cu);
20755 }
20756
20757 /* A convenience function to find the proper .debug_line section for a CU. */
20758
20759 static struct dwarf2_section_info *
20760 get_debug_line_section (struct dwarf2_cu *cu)
20761 {
20762 struct dwarf2_section_info *section;
20763 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20764
20765 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20766 DWO file. */
20767 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20768 section = &cu->dwo_unit->dwo_file->sections.line;
20769 else if (cu->per_cu->is_dwz)
20770 {
20771 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20772
20773 section = &dwz->line;
20774 }
20775 else
20776 section = &per_objfile->per_bfd->line;
20777
20778 return section;
20779 }
20780
20781 /* Read the statement program header starting at OFFSET in
20782 .debug_line, or .debug_line.dwo. Return a pointer
20783 to a struct line_header, allocated using xmalloc.
20784 Returns NULL if there is a problem reading the header, e.g., if it
20785 has a version we don't understand.
20786
20787 NOTE: the strings in the include directory and file name tables of
20788 the returned object point into the dwarf line section buffer,
20789 and must not be freed. */
20790
20791 static line_header_up
20792 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20793 {
20794 struct dwarf2_section_info *section;
20795 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20796
20797 section = get_debug_line_section (cu);
20798 section->read (per_objfile->objfile);
20799 if (section->buffer == NULL)
20800 {
20801 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20802 complaint (_("missing .debug_line.dwo section"));
20803 else
20804 complaint (_("missing .debug_line section"));
20805 return 0;
20806 }
20807
20808 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20809 per_objfile, section, &cu->header);
20810 }
20811
20812 /* Subroutine of dwarf_decode_lines to simplify it.
20813 Return the file name for the given file_entry.
20814 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
20815 If space for the result is malloc'd, *NAME_HOLDER will be set.
20816 Returns NULL if FILE_INDEX should be ignored, i.e., it is
20817 equivalent to CU_INFO. */
20818
20819 static const char *
20820 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
20821 const file_and_directory &cu_info,
20822 gdb::unique_xmalloc_ptr<char> *name_holder)
20823 {
20824 const char *include_name = fe.name;
20825 const char *include_name_to_compare = include_name;
20826
20827 const char *dir_name = fe.include_dir (lh);
20828
20829 gdb::unique_xmalloc_ptr<char> hold_compare;
20830 if (!IS_ABSOLUTE_PATH (include_name)
20831 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
20832 {
20833 /* Avoid creating a duplicate name for CU_INFO.
20834 We do this by comparing INCLUDE_NAME and CU_INFO.
20835 Before we do the comparison, however, we need to account
20836 for DIR_NAME and COMP_DIR.
20837 First prepend dir_name (if non-NULL). If we still don't
20838 have an absolute path prepend comp_dir (if non-NULL).
20839 However, the directory we record in the include-file's
20840 psymtab does not contain COMP_DIR (to match the
20841 corresponding symtab(s)).
20842
20843 Example:
20844
20845 bash$ cd /tmp
20846 bash$ gcc -g ./hello.c
20847 include_name = "hello.c"
20848 dir_name = "."
20849 DW_AT_comp_dir = comp_dir = "/tmp"
20850 DW_AT_name = "./hello.c"
20851
20852 */
20853
20854 if (dir_name != NULL)
20855 {
20856 name_holder->reset (concat (dir_name, SLASH_STRING,
20857 include_name, (char *) NULL));
20858 include_name = name_holder->get ();
20859 include_name_to_compare = include_name;
20860 }
20861 if (!IS_ABSOLUTE_PATH (include_name)
20862 && cu_info.get_comp_dir () != nullptr)
20863 {
20864 hold_compare.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20865 include_name, (char *) NULL));
20866 include_name_to_compare = hold_compare.get ();
20867 }
20868 }
20869
20870 gdb::unique_xmalloc_ptr<char> copied_name;
20871 const char *cu_filename = cu_info.get_name ();
20872 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
20873 {
20874 copied_name.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20875 cu_filename, (char *) NULL));
20876 cu_filename = copied_name.get ();
20877 }
20878
20879 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
20880 return nullptr;
20881 return include_name;
20882 }
20883
20884 /* State machine to track the state of the line number program. */
20885
20886 class lnp_state_machine
20887 {
20888 public:
20889 /* Initialize a machine state for the start of a line number
20890 program. */
20891 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20892 bool record_lines_p);
20893
20894 file_entry *current_file ()
20895 {
20896 /* lh->file_names is 0-based, but the file name numbers in the
20897 statement program are 1-based. */
20898 return m_line_header->file_name_at (m_file);
20899 }
20900
20901 /* Record the line in the state machine. END_SEQUENCE is true if
20902 we're processing the end of a sequence. */
20903 void record_line (bool end_sequence);
20904
20905 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20906 nop-out rest of the lines in this sequence. */
20907 void check_line_address (struct dwarf2_cu *cu,
20908 const gdb_byte *line_ptr,
20909 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20910
20911 void handle_set_discriminator (unsigned int discriminator)
20912 {
20913 m_discriminator = discriminator;
20914 m_line_has_non_zero_discriminator |= discriminator != 0;
20915 }
20916
20917 /* Handle DW_LNE_set_address. */
20918 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20919 {
20920 m_op_index = 0;
20921 address += baseaddr;
20922 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20923 }
20924
20925 /* Handle DW_LNS_advance_pc. */
20926 void handle_advance_pc (CORE_ADDR adjust);
20927
20928 /* Handle a special opcode. */
20929 void handle_special_opcode (unsigned char op_code);
20930
20931 /* Handle DW_LNS_advance_line. */
20932 void handle_advance_line (int line_delta)
20933 {
20934 advance_line (line_delta);
20935 }
20936
20937 /* Handle DW_LNS_set_file. */
20938 void handle_set_file (file_name_index file);
20939
20940 /* Handle DW_LNS_negate_stmt. */
20941 void handle_negate_stmt ()
20942 {
20943 m_flags ^= LEF_IS_STMT;
20944 }
20945
20946 /* Handle DW_LNS_const_add_pc. */
20947 void handle_const_add_pc ();
20948
20949 /* Handle DW_LNS_fixed_advance_pc. */
20950 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20951 {
20952 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20953 m_op_index = 0;
20954 }
20955
20956 /* Handle DW_LNS_copy. */
20957 void handle_copy ()
20958 {
20959 record_line (false);
20960 m_discriminator = 0;
20961 m_flags &= ~LEF_PROLOGUE_END;
20962 }
20963
20964 /* Handle DW_LNE_end_sequence. */
20965 void handle_end_sequence ()
20966 {
20967 m_currently_recording_lines = true;
20968 }
20969
20970 /* Handle DW_LNS_set_prologue_end. */
20971 void handle_set_prologue_end ()
20972 {
20973 m_flags |= LEF_PROLOGUE_END;
20974 }
20975
20976 private:
20977 /* Advance the line by LINE_DELTA. */
20978 void advance_line (int line_delta)
20979 {
20980 m_line += line_delta;
20981
20982 if (line_delta != 0)
20983 m_line_has_non_zero_discriminator = m_discriminator != 0;
20984 }
20985
20986 struct dwarf2_cu *m_cu;
20987
20988 gdbarch *m_gdbarch;
20989
20990 /* True if we're recording lines.
20991 Otherwise we're building partial symtabs and are just interested in
20992 finding include files mentioned by the line number program. */
20993 bool m_record_lines_p;
20994
20995 /* The line number header. */
20996 line_header *m_line_header;
20997
20998 /* These are part of the standard DWARF line number state machine,
20999 and initialized according to the DWARF spec. */
21000
21001 unsigned char m_op_index = 0;
21002 /* The line table index of the current file. */
21003 file_name_index m_file = 1;
21004 unsigned int m_line = 1;
21005
21006 /* These are initialized in the constructor. */
21007
21008 CORE_ADDR m_address;
21009 linetable_entry_flags m_flags;
21010 unsigned int m_discriminator;
21011
21012 /* Additional bits of state we need to track. */
21013
21014 /* The last file that we called dwarf2_start_subfile for.
21015 This is only used for TLLs. */
21016 unsigned int m_last_file = 0;
21017 /* The last file a line number was recorded for. */
21018 struct subfile *m_last_subfile = NULL;
21019
21020 /* The address of the last line entry. */
21021 CORE_ADDR m_last_address;
21022
21023 /* Set to true when a previous line at the same address (using
21024 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
21025 when a line entry at a new address (m_address different to
21026 m_last_address) is processed. */
21027 bool m_stmt_at_address = false;
21028
21029 /* When true, record the lines we decode. */
21030 bool m_currently_recording_lines = false;
21031
21032 /* The last line number that was recorded, used to coalesce
21033 consecutive entries for the same line. This can happen, for
21034 example, when discriminators are present. PR 17276. */
21035 unsigned int m_last_line = 0;
21036 bool m_line_has_non_zero_discriminator = false;
21037 };
21038
21039 void
21040 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21041 {
21042 CORE_ADDR addr_adj = (((m_op_index + adjust)
21043 / m_line_header->maximum_ops_per_instruction)
21044 * m_line_header->minimum_instruction_length);
21045 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21046 m_op_index = ((m_op_index + adjust)
21047 % m_line_header->maximum_ops_per_instruction);
21048 }
21049
21050 void
21051 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21052 {
21053 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21054 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21055 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21056 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21057 / m_line_header->maximum_ops_per_instruction)
21058 * m_line_header->minimum_instruction_length);
21059 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21060 m_op_index = ((m_op_index + adj_opcode_d)
21061 % m_line_header->maximum_ops_per_instruction);
21062
21063 int line_delta = m_line_header->line_base + adj_opcode_r;
21064 advance_line (line_delta);
21065 record_line (false);
21066 m_discriminator = 0;
21067 m_flags &= ~LEF_PROLOGUE_END;
21068 }
21069
21070 void
21071 lnp_state_machine::handle_set_file (file_name_index file)
21072 {
21073 m_file = file;
21074
21075 const file_entry *fe = current_file ();
21076 if (fe == NULL)
21077 dwarf2_debug_line_missing_file_complaint ();
21078 else if (m_record_lines_p)
21079 {
21080 const char *dir = fe->include_dir (m_line_header);
21081
21082 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21083 m_line_has_non_zero_discriminator = m_discriminator != 0;
21084 dwarf2_start_subfile (m_cu, fe->name, dir);
21085 }
21086 }
21087
21088 void
21089 lnp_state_machine::handle_const_add_pc ()
21090 {
21091 CORE_ADDR adjust
21092 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21093
21094 CORE_ADDR addr_adj
21095 = (((m_op_index + adjust)
21096 / m_line_header->maximum_ops_per_instruction)
21097 * m_line_header->minimum_instruction_length);
21098
21099 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21100 m_op_index = ((m_op_index + adjust)
21101 % m_line_header->maximum_ops_per_instruction);
21102 }
21103
21104 /* Return non-zero if we should add LINE to the line number table.
21105 LINE is the line to add, LAST_LINE is the last line that was added,
21106 LAST_SUBFILE is the subfile for LAST_LINE.
21107 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21108 had a non-zero discriminator.
21109
21110 We have to be careful in the presence of discriminators.
21111 E.g., for this line:
21112
21113 for (i = 0; i < 100000; i++);
21114
21115 clang can emit four line number entries for that one line,
21116 each with a different discriminator.
21117 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21118
21119 However, we want gdb to coalesce all four entries into one.
21120 Otherwise the user could stepi into the middle of the line and
21121 gdb would get confused about whether the pc really was in the
21122 middle of the line.
21123
21124 Things are further complicated by the fact that two consecutive
21125 line number entries for the same line is a heuristic used by gcc
21126 to denote the end of the prologue. So we can't just discard duplicate
21127 entries, we have to be selective about it. The heuristic we use is
21128 that we only collapse consecutive entries for the same line if at least
21129 one of those entries has a non-zero discriminator. PR 17276.
21130
21131 Note: Addresses in the line number state machine can never go backwards
21132 within one sequence, thus this coalescing is ok. */
21133
21134 static int
21135 dwarf_record_line_p (struct dwarf2_cu *cu,
21136 unsigned int line, unsigned int last_line,
21137 int line_has_non_zero_discriminator,
21138 struct subfile *last_subfile)
21139 {
21140 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21141 return 1;
21142 if (line != last_line)
21143 return 1;
21144 /* Same line for the same file that we've seen already.
21145 As a last check, for pr 17276, only record the line if the line
21146 has never had a non-zero discriminator. */
21147 if (!line_has_non_zero_discriminator)
21148 return 1;
21149 return 0;
21150 }
21151
21152 /* Use the CU's builder to record line number LINE beginning at
21153 address ADDRESS in the line table of subfile SUBFILE. */
21154
21155 static void
21156 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21157 unsigned int line, CORE_ADDR address,
21158 linetable_entry_flags flags,
21159 struct dwarf2_cu *cu)
21160 {
21161 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21162
21163 if (dwarf_line_debug)
21164 {
21165 gdb_printf (gdb_stdlog,
21166 "Recording line %u, file %s, address %s\n",
21167 line, lbasename (subfile->name),
21168 paddress (gdbarch, address));
21169 }
21170
21171 if (cu != nullptr)
21172 cu->get_builder ()->record_line (subfile, line, addr, flags);
21173 }
21174
21175 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21176 Mark the end of a set of line number records.
21177 The arguments are the same as for dwarf_record_line_1.
21178 If SUBFILE is NULL the request is ignored. */
21179
21180 static void
21181 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21182 CORE_ADDR address, struct dwarf2_cu *cu)
21183 {
21184 if (subfile == NULL)
21185 return;
21186
21187 if (dwarf_line_debug)
21188 {
21189 gdb_printf (gdb_stdlog,
21190 "Finishing current line, file %s, address %s\n",
21191 lbasename (subfile->name),
21192 paddress (gdbarch, address));
21193 }
21194
21195 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
21196 }
21197
21198 void
21199 lnp_state_machine::record_line (bool end_sequence)
21200 {
21201 if (dwarf_line_debug)
21202 {
21203 gdb_printf (gdb_stdlog,
21204 "Processing actual line %u: file %u,"
21205 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
21206 m_line, m_file,
21207 paddress (m_gdbarch, m_address),
21208 (m_flags & LEF_IS_STMT) != 0,
21209 (m_flags & LEF_PROLOGUE_END) != 0,
21210 m_discriminator,
21211 (end_sequence ? "\t(end sequence)" : ""));
21212 }
21213
21214 file_entry *fe = current_file ();
21215
21216 if (fe == NULL)
21217 dwarf2_debug_line_missing_file_complaint ();
21218 /* For now we ignore lines not starting on an instruction boundary.
21219 But not when processing end_sequence for compatibility with the
21220 previous version of the code. */
21221 else if (m_op_index == 0 || end_sequence)
21222 {
21223 fe->included_p = true;
21224 if (m_record_lines_p)
21225 {
21226 /* When we switch files we insert an end maker in the first file,
21227 switch to the second file and add a new line entry. The
21228 problem is that the end marker inserted in the first file will
21229 discard any previous line entries at the same address. If the
21230 line entries in the first file are marked as is-stmt, while
21231 the new line in the second file is non-stmt, then this means
21232 the end marker will discard is-stmt lines so we can have a
21233 non-stmt line. This means that there are less addresses at
21234 which the user can insert a breakpoint.
21235
21236 To improve this we track the last address in m_last_address,
21237 and whether we have seen an is-stmt at this address. Then
21238 when switching files, if we have seen a stmt at the current
21239 address, and we are switching to create a non-stmt line, then
21240 discard the new line. */
21241 bool file_changed
21242 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21243 bool ignore_this_line
21244 = ((file_changed && !end_sequence && m_last_address == m_address
21245 && ((m_flags & LEF_IS_STMT) == 0)
21246 && m_stmt_at_address)
21247 || (!end_sequence && m_line == 0));
21248
21249 if ((file_changed && !ignore_this_line) || end_sequence)
21250 {
21251 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21252 m_currently_recording_lines ? m_cu : nullptr);
21253 }
21254
21255 if (!end_sequence && !ignore_this_line)
21256 {
21257 linetable_entry_flags lte_flags = m_flags;
21258 if (producer_is_codewarrior (m_cu))
21259 lte_flags |= LEF_IS_STMT;
21260
21261 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21262 m_line_has_non_zero_discriminator,
21263 m_last_subfile))
21264 {
21265 buildsym_compunit *builder = m_cu->get_builder ();
21266 dwarf_record_line_1 (m_gdbarch,
21267 builder->get_current_subfile (),
21268 m_line, m_address, lte_flags,
21269 m_currently_recording_lines ? m_cu : nullptr);
21270 }
21271 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21272 m_last_line = m_line;
21273 }
21274 }
21275 }
21276
21277 /* Track whether we have seen any IS_STMT true at m_address in case we
21278 have multiple line table entries all at m_address. */
21279 if (m_last_address != m_address)
21280 {
21281 m_stmt_at_address = false;
21282 m_last_address = m_address;
21283 }
21284 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
21285 }
21286
21287 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21288 line_header *lh, bool record_lines_p)
21289 {
21290 m_cu = cu;
21291 m_gdbarch = arch;
21292 m_record_lines_p = record_lines_p;
21293 m_line_header = lh;
21294
21295 m_currently_recording_lines = true;
21296
21297 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21298 was a line entry for it so that the backend has a chance to adjust it
21299 and also record it in case it needs it. This is currently used by MIPS
21300 code, cf. `mips_adjust_dwarf2_line'. */
21301 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21302 m_flags = 0;
21303 if (lh->default_is_stmt)
21304 m_flags |= LEF_IS_STMT;
21305 m_discriminator = 0;
21306
21307 m_last_address = m_address;
21308 m_stmt_at_address = false;
21309 }
21310
21311 void
21312 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21313 const gdb_byte *line_ptr,
21314 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21315 {
21316 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21317 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21318 located at 0x0. In this case, additionally check that if
21319 ADDRESS < UNRELOCATED_LOWPC. */
21320
21321 if ((address == 0 && address < unrelocated_lowpc)
21322 || address == (CORE_ADDR) -1)
21323 {
21324 /* This line table is for a function which has been
21325 GCd by the linker. Ignore it. PR gdb/12528 */
21326
21327 struct objfile *objfile = cu->per_objfile->objfile;
21328 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21329
21330 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21331 line_offset, objfile_name (objfile));
21332 m_currently_recording_lines = false;
21333 /* Note: m_currently_recording_lines is left as false until we see
21334 DW_LNE_end_sequence. */
21335 }
21336 }
21337
21338 /* Subroutine of dwarf_decode_lines to simplify it.
21339 Process the line number information in LH.
21340 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21341 program in order to set included_p for every referenced header. */
21342
21343 static void
21344 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21345 const int decode_for_pst_p, CORE_ADDR lowpc)
21346 {
21347 const gdb_byte *line_ptr, *extended_end;
21348 const gdb_byte *line_end;
21349 unsigned int bytes_read, extended_len;
21350 unsigned char op_code, extended_op;
21351 CORE_ADDR baseaddr;
21352 struct objfile *objfile = cu->per_objfile->objfile;
21353 bfd *abfd = objfile->obfd;
21354 struct gdbarch *gdbarch = objfile->arch ();
21355 /* True if we're recording line info (as opposed to building partial
21356 symtabs and just interested in finding include files mentioned by
21357 the line number program). */
21358 bool record_lines_p = !decode_for_pst_p;
21359
21360 baseaddr = objfile->text_section_offset ();
21361
21362 line_ptr = lh->statement_program_start;
21363 line_end = lh->statement_program_end;
21364
21365 /* Read the statement sequences until there's nothing left. */
21366 while (line_ptr < line_end)
21367 {
21368 /* The DWARF line number program state machine. Reset the state
21369 machine at the start of each sequence. */
21370 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21371 bool end_sequence = false;
21372
21373 if (record_lines_p)
21374 {
21375 /* Start a subfile for the current file of the state
21376 machine. */
21377 const file_entry *fe = state_machine.current_file ();
21378
21379 if (fe != NULL)
21380 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21381 }
21382
21383 /* Decode the table. */
21384 while (line_ptr < line_end && !end_sequence)
21385 {
21386 op_code = read_1_byte (abfd, line_ptr);
21387 line_ptr += 1;
21388
21389 if (op_code >= lh->opcode_base)
21390 {
21391 /* Special opcode. */
21392 state_machine.handle_special_opcode (op_code);
21393 }
21394 else switch (op_code)
21395 {
21396 case DW_LNS_extended_op:
21397 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21398 &bytes_read);
21399 line_ptr += bytes_read;
21400 extended_end = line_ptr + extended_len;
21401 extended_op = read_1_byte (abfd, line_ptr);
21402 line_ptr += 1;
21403 if (DW_LNE_lo_user <= extended_op
21404 && extended_op <= DW_LNE_hi_user)
21405 {
21406 /* Vendor extension, ignore. */
21407 line_ptr = extended_end;
21408 break;
21409 }
21410 switch (extended_op)
21411 {
21412 case DW_LNE_end_sequence:
21413 state_machine.handle_end_sequence ();
21414 end_sequence = true;
21415 break;
21416 case DW_LNE_set_address:
21417 {
21418 CORE_ADDR address
21419 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21420 line_ptr += bytes_read;
21421
21422 state_machine.check_line_address (cu, line_ptr,
21423 lowpc - baseaddr, address);
21424 state_machine.handle_set_address (baseaddr, address);
21425 }
21426 break;
21427 case DW_LNE_define_file:
21428 {
21429 const char *cur_file;
21430 unsigned int mod_time, length;
21431 dir_index dindex;
21432
21433 cur_file = read_direct_string (abfd, line_ptr,
21434 &bytes_read);
21435 line_ptr += bytes_read;
21436 dindex = (dir_index)
21437 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21438 line_ptr += bytes_read;
21439 mod_time =
21440 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21441 line_ptr += bytes_read;
21442 length =
21443 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21444 line_ptr += bytes_read;
21445 lh->add_file_name (cur_file, dindex, mod_time, length);
21446 }
21447 break;
21448 case DW_LNE_set_discriminator:
21449 {
21450 /* The discriminator is not interesting to the
21451 debugger; just ignore it. We still need to
21452 check its value though:
21453 if there are consecutive entries for the same
21454 (non-prologue) line we want to coalesce them.
21455 PR 17276. */
21456 unsigned int discr
21457 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21458 line_ptr += bytes_read;
21459
21460 state_machine.handle_set_discriminator (discr);
21461 }
21462 break;
21463 default:
21464 complaint (_("mangled .debug_line section"));
21465 return;
21466 }
21467 /* Make sure that we parsed the extended op correctly. If e.g.
21468 we expected a different address size than the producer used,
21469 we may have read the wrong number of bytes. */
21470 if (line_ptr != extended_end)
21471 {
21472 complaint (_("mangled .debug_line section"));
21473 return;
21474 }
21475 break;
21476 case DW_LNS_copy:
21477 state_machine.handle_copy ();
21478 break;
21479 case DW_LNS_advance_pc:
21480 {
21481 CORE_ADDR adjust
21482 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21483 line_ptr += bytes_read;
21484
21485 state_machine.handle_advance_pc (adjust);
21486 }
21487 break;
21488 case DW_LNS_advance_line:
21489 {
21490 int line_delta
21491 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21492 line_ptr += bytes_read;
21493
21494 state_machine.handle_advance_line (line_delta);
21495 }
21496 break;
21497 case DW_LNS_set_file:
21498 {
21499 file_name_index file
21500 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21501 &bytes_read);
21502 line_ptr += bytes_read;
21503
21504 state_machine.handle_set_file (file);
21505 }
21506 break;
21507 case DW_LNS_set_column:
21508 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21509 line_ptr += bytes_read;
21510 break;
21511 case DW_LNS_negate_stmt:
21512 state_machine.handle_negate_stmt ();
21513 break;
21514 case DW_LNS_set_basic_block:
21515 break;
21516 /* Add to the address register of the state machine the
21517 address increment value corresponding to special opcode
21518 255. I.e., this value is scaled by the minimum
21519 instruction length since special opcode 255 would have
21520 scaled the increment. */
21521 case DW_LNS_const_add_pc:
21522 state_machine.handle_const_add_pc ();
21523 break;
21524 case DW_LNS_fixed_advance_pc:
21525 {
21526 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21527 line_ptr += 2;
21528
21529 state_machine.handle_fixed_advance_pc (addr_adj);
21530 }
21531 break;
21532 case DW_LNS_set_prologue_end:
21533 state_machine.handle_set_prologue_end ();
21534 break;
21535 default:
21536 {
21537 /* Unknown standard opcode, ignore it. */
21538 int i;
21539
21540 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21541 {
21542 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21543 line_ptr += bytes_read;
21544 }
21545 }
21546 }
21547 }
21548
21549 if (!end_sequence)
21550 dwarf2_debug_line_missing_end_sequence_complaint ();
21551
21552 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21553 in which case we still finish recording the last line). */
21554 state_machine.record_line (true);
21555 }
21556 }
21557
21558 /* Decode the Line Number Program (LNP) for the given line_header
21559 structure and CU. The actual information extracted and the type
21560 of structures created from the LNP depends on the value of PST.
21561
21562 1. If PST is NULL, then this procedure uses the data from the program
21563 to create all necessary symbol tables, and their linetables.
21564
21565 2. If PST is not NULL, this procedure reads the program to determine
21566 the list of files included by the unit represented by PST, and
21567 builds all the associated partial symbol tables.
21568
21569 FND holds the CU file name and directory, if known.
21570 It is used for relative paths in the line table.
21571
21572 NOTE: It is important that psymtabs have the same file name (via
21573 strcmp) as the corresponding symtab. Since the directory is not
21574 used in the name of the symtab we don't use it in the name of the
21575 psymtabs we create. E.g. expand_line_sal requires this when
21576 finding psymtabs to expand. A good testcase for this is
21577 mb-inline.exp.
21578
21579 LOWPC is the lowest address in CU (or 0 if not known).
21580
21581 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21582 for its PC<->lines mapping information. Otherwise only the filename
21583 table is read in. */
21584
21585 static void
21586 dwarf_decode_lines (struct line_header *lh, const file_and_directory &fnd,
21587 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21588 CORE_ADDR lowpc, int decode_mapping)
21589 {
21590 struct objfile *objfile = cu->per_objfile->objfile;
21591 const int decode_for_pst_p = (pst != NULL);
21592
21593 if (decode_mapping)
21594 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21595
21596 if (decode_for_pst_p)
21597 {
21598 /* Now that we're done scanning the Line Header Program, we can
21599 create the psymtab of each included file. */
21600 for (auto &file_entry : lh->file_names ())
21601 if (file_entry.included_p)
21602 {
21603 gdb::unique_xmalloc_ptr<char> name_holder;
21604 const char *include_name =
21605 compute_include_file_name (lh, file_entry, fnd, &name_holder);
21606 if (include_name != NULL)
21607 dwarf2_create_include_psymtab
21608 (cu->per_objfile->per_bfd, include_name, pst,
21609 cu->per_objfile->per_bfd->partial_symtabs.get (),
21610 objfile->per_bfd);
21611 }
21612 }
21613 else
21614 {
21615 /* Make sure a symtab is created for every file, even files
21616 which contain only variables (i.e. no code with associated
21617 line numbers). */
21618 buildsym_compunit *builder = cu->get_builder ();
21619 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21620
21621 for (auto &fe : lh->file_names ())
21622 {
21623 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21624 if (builder->get_current_subfile ()->symtab == NULL)
21625 {
21626 builder->get_current_subfile ()->symtab
21627 = allocate_symtab (cust,
21628 builder->get_current_subfile ()->name);
21629 }
21630 fe.symtab = builder->get_current_subfile ()->symtab;
21631 }
21632 }
21633 }
21634
21635 /* Start a subfile for DWARF. FILENAME is the name of the file and
21636 DIRNAME the name of the source directory which contains FILENAME
21637 or NULL if not known.
21638 This routine tries to keep line numbers from identical absolute and
21639 relative file names in a common subfile.
21640
21641 Using the `list' example from the GDB testsuite, which resides in
21642 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21643 of /srcdir/list0.c yields the following debugging information for list0.c:
21644
21645 DW_AT_name: /srcdir/list0.c
21646 DW_AT_comp_dir: /compdir
21647 files.files[0].name: list0.h
21648 files.files[0].dir: /srcdir
21649 files.files[1].name: list0.c
21650 files.files[1].dir: /srcdir
21651
21652 The line number information for list0.c has to end up in a single
21653 subfile, so that `break /srcdir/list0.c:1' works as expected.
21654 start_subfile will ensure that this happens provided that we pass the
21655 concatenation of files.files[1].dir and files.files[1].name as the
21656 subfile's name. */
21657
21658 static void
21659 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21660 const char *dirname)
21661 {
21662 gdb::unique_xmalloc_ptr<char> copy;
21663
21664 /* In order not to lose the line information directory,
21665 we concatenate it to the filename when it makes sense.
21666 Note that the Dwarf3 standard says (speaking of filenames in line
21667 information): ``The directory index is ignored for file names
21668 that represent full path names''. Thus ignoring dirname in the
21669 `else' branch below isn't an issue. */
21670
21671 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21672 {
21673 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21674 filename = copy.get ();
21675 }
21676
21677 cu->get_builder ()->start_subfile (filename);
21678 }
21679
21680 static void
21681 var_decode_location (struct attribute *attr, struct symbol *sym,
21682 struct dwarf2_cu *cu)
21683 {
21684 struct objfile *objfile = cu->per_objfile->objfile;
21685 struct comp_unit_head *cu_header = &cu->header;
21686
21687 /* NOTE drow/2003-01-30: There used to be a comment and some special
21688 code here to turn a symbol with DW_AT_external and a
21689 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21690 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21691 with some versions of binutils) where shared libraries could have
21692 relocations against symbols in their debug information - the
21693 minimal symbol would have the right address, but the debug info
21694 would not. It's no longer necessary, because we will explicitly
21695 apply relocations when we read in the debug information now. */
21696
21697 /* A DW_AT_location attribute with no contents indicates that a
21698 variable has been optimized away. */
21699 if (attr->form_is_block () && attr->as_block ()->size == 0)
21700 {
21701 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21702 return;
21703 }
21704
21705 /* Handle one degenerate form of location expression specially, to
21706 preserve GDB's previous behavior when section offsets are
21707 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21708 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21709
21710 if (attr->form_is_block ())
21711 {
21712 struct dwarf_block *block = attr->as_block ();
21713
21714 if ((block->data[0] == DW_OP_addr
21715 && block->size == 1 + cu_header->addr_size)
21716 || ((block->data[0] == DW_OP_GNU_addr_index
21717 || block->data[0] == DW_OP_addrx)
21718 && (block->size
21719 == 1 + leb128_size (&block->data[1]))))
21720 {
21721 unsigned int dummy;
21722
21723 if (block->data[0] == DW_OP_addr)
21724 sym->set_value_address
21725 (cu->header.read_address (objfile->obfd, block->data + 1,
21726 &dummy));
21727 else
21728 sym->set_value_address
21729 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
21730 sym->set_aclass_index (LOC_STATIC);
21731 fixup_symbol_section (sym, objfile);
21732 sym->set_value_address
21733 (sym->value_address ()
21734 + objfile->section_offsets[sym->section_index ()]);
21735 return;
21736 }
21737 }
21738
21739 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21740 expression evaluator, and use LOC_COMPUTED only when necessary
21741 (i.e. when the value of a register or memory location is
21742 referenced, or a thread-local block, etc.). Then again, it might
21743 not be worthwhile. I'm assuming that it isn't unless performance
21744 or memory numbers show me otherwise. */
21745
21746 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21747
21748 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21749 cu->has_loclist = true;
21750 }
21751
21752 /* Given a pointer to a DWARF information entry, figure out if we need
21753 to make a symbol table entry for it, and if so, create a new entry
21754 and return a pointer to it.
21755 If TYPE is NULL, determine symbol type from the die, otherwise
21756 used the passed type.
21757 If SPACE is not NULL, use it to hold the new symbol. If it is
21758 NULL, allocate a new symbol on the objfile's obstack. */
21759
21760 static struct symbol *
21761 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21762 struct symbol *space)
21763 {
21764 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21765 struct objfile *objfile = per_objfile->objfile;
21766 struct gdbarch *gdbarch = objfile->arch ();
21767 struct symbol *sym = NULL;
21768 const char *name;
21769 struct attribute *attr = NULL;
21770 struct attribute *attr2 = NULL;
21771 CORE_ADDR baseaddr;
21772 struct pending **list_to_add = NULL;
21773
21774 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21775
21776 baseaddr = objfile->text_section_offset ();
21777
21778 name = dwarf2_name (die, cu);
21779 if (name)
21780 {
21781 int suppress_add = 0;
21782
21783 if (space)
21784 sym = space;
21785 else
21786 sym = new (&objfile->objfile_obstack) symbol;
21787 OBJSTAT (objfile, n_syms++);
21788
21789 /* Cache this symbol's name and the name's demangled form (if any). */
21790 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
21791 /* Fortran does not have mangling standard and the mangling does differ
21792 between gfortran, iFort etc. */
21793 const char *physname
21794 = (cu->per_cu->lang == language_fortran
21795 ? dwarf2_full_name (name, die, cu)
21796 : dwarf2_physname (name, die, cu));
21797 const char *linkagename = dw2_linkage_name (die, cu);
21798
21799 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
21800 sym->set_linkage_name (physname);
21801 else
21802 {
21803 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21804 sym->set_linkage_name (linkagename);
21805 }
21806
21807 /* Handle DW_AT_artificial. */
21808 attr = dwarf2_attr (die, DW_AT_artificial, cu);
21809 if (attr != nullptr)
21810 sym->artificial = attr->as_boolean ();
21811
21812 /* Default assumptions.
21813 Use the passed type or decode it from the die. */
21814 sym->set_domain (VAR_DOMAIN);
21815 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21816 if (type != NULL)
21817 sym->set_type (type);
21818 else
21819 sym->set_type (die_type (die, cu));
21820 attr = dwarf2_attr (die,
21821 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21822 cu);
21823 if (attr != nullptr)
21824 sym->set_line (attr->constant_value (0));
21825
21826 attr = dwarf2_attr (die,
21827 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21828 cu);
21829 if (attr != nullptr && attr->is_nonnegative ())
21830 {
21831 file_name_index file_index
21832 = (file_name_index) attr->as_nonnegative ();
21833 struct file_entry *fe;
21834
21835 if (cu->line_header != NULL)
21836 fe = cu->line_header->file_name_at (file_index);
21837 else
21838 fe = NULL;
21839
21840 if (fe == NULL)
21841 complaint (_("file index out of range"));
21842 else
21843 symbol_set_symtab (sym, fe->symtab);
21844 }
21845
21846 switch (die->tag)
21847 {
21848 case DW_TAG_label:
21849 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21850 if (attr != nullptr)
21851 {
21852 CORE_ADDR addr;
21853
21854 addr = attr->as_address ();
21855 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21856 sym->set_value_address (addr);
21857 sym->set_aclass_index (LOC_LABEL);
21858 }
21859 else
21860 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
21861 sym->set_type (objfile_type (objfile)->builtin_core_addr);
21862 sym->set_domain (LABEL_DOMAIN);
21863 add_symbol_to_list (sym, cu->list_in_scope);
21864 break;
21865 case DW_TAG_subprogram:
21866 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21867 finish_block. */
21868 sym->set_aclass_index (LOC_BLOCK);
21869 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21870 if ((attr2 != nullptr && attr2->as_boolean ())
21871 || cu->per_cu->lang == language_ada
21872 || cu->per_cu->lang == language_fortran)
21873 {
21874 /* Subprograms marked external are stored as a global symbol.
21875 Ada and Fortran subprograms, whether marked external or
21876 not, are always stored as a global symbol, because we want
21877 to be able to access them globally. For instance, we want
21878 to be able to break on a nested subprogram without having
21879 to specify the context. */
21880 list_to_add = cu->get_builder ()->get_global_symbols ();
21881 }
21882 else
21883 {
21884 list_to_add = cu->list_in_scope;
21885 }
21886 break;
21887 case DW_TAG_inlined_subroutine:
21888 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21889 finish_block. */
21890 sym->set_aclass_index (LOC_BLOCK);
21891 sym->set_is_inlined (1);
21892 list_to_add = cu->list_in_scope;
21893 break;
21894 case DW_TAG_template_value_param:
21895 suppress_add = 1;
21896 /* Fall through. */
21897 case DW_TAG_constant:
21898 case DW_TAG_variable:
21899 case DW_TAG_member:
21900 /* Compilation with minimal debug info may result in
21901 variables with missing type entries. Change the
21902 misleading `void' type to something sensible. */
21903 if (sym->type ()->code () == TYPE_CODE_VOID)
21904 sym->set_type (objfile_type (objfile)->builtin_int);
21905
21906 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21907 /* In the case of DW_TAG_member, we should only be called for
21908 static const members. */
21909 if (die->tag == DW_TAG_member)
21910 {
21911 /* dwarf2_add_field uses die_is_declaration,
21912 so we do the same. */
21913 gdb_assert (die_is_declaration (die, cu));
21914 gdb_assert (attr);
21915 }
21916 if (attr != nullptr)
21917 {
21918 dwarf2_const_value (attr, sym, cu);
21919 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21920 if (!suppress_add)
21921 {
21922 if (attr2 != nullptr && attr2->as_boolean ())
21923 list_to_add = cu->get_builder ()->get_global_symbols ();
21924 else
21925 list_to_add = cu->list_in_scope;
21926 }
21927 break;
21928 }
21929 attr = dwarf2_attr (die, DW_AT_location, cu);
21930 if (attr != nullptr)
21931 {
21932 var_decode_location (attr, sym, cu);
21933 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21934
21935 /* Fortran explicitly imports any global symbols to the local
21936 scope by DW_TAG_common_block. */
21937 if (cu->per_cu->lang == language_fortran && die->parent
21938 && die->parent->tag == DW_TAG_common_block)
21939 attr2 = NULL;
21940
21941 if (sym->aclass () == LOC_STATIC
21942 && sym->value_address () == 0
21943 && !per_objfile->per_bfd->has_section_at_zero)
21944 {
21945 /* When a static variable is eliminated by the linker,
21946 the corresponding debug information is not stripped
21947 out, but the variable address is set to null;
21948 do not add such variables into symbol table. */
21949 }
21950 else if (attr2 != nullptr && attr2->as_boolean ())
21951 {
21952 if (sym->aclass () == LOC_STATIC
21953 && (objfile->flags & OBJF_MAINLINE) == 0
21954 && per_objfile->per_bfd->can_copy)
21955 {
21956 /* A global static variable might be subject to
21957 copy relocation. We first check for a local
21958 minsym, though, because maybe the symbol was
21959 marked hidden, in which case this would not
21960 apply. */
21961 bound_minimal_symbol found
21962 = (lookup_minimal_symbol_linkage
21963 (sym->linkage_name (), objfile));
21964 if (found.minsym != nullptr)
21965 sym->maybe_copied = 1;
21966 }
21967
21968 /* A variable with DW_AT_external is never static,
21969 but it may be block-scoped. */
21970 list_to_add
21971 = ((cu->list_in_scope
21972 == cu->get_builder ()->get_file_symbols ())
21973 ? cu->get_builder ()->get_global_symbols ()
21974 : cu->list_in_scope);
21975 }
21976 else
21977 list_to_add = cu->list_in_scope;
21978 }
21979 else
21980 {
21981 /* We do not know the address of this symbol.
21982 If it is an external symbol and we have type information
21983 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21984 The address of the variable will then be determined from
21985 the minimal symbol table whenever the variable is
21986 referenced. */
21987 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21988
21989 /* Fortran explicitly imports any global symbols to the local
21990 scope by DW_TAG_common_block. */
21991 if (cu->per_cu->lang == language_fortran && die->parent
21992 && die->parent->tag == DW_TAG_common_block)
21993 {
21994 /* SYMBOL_CLASS doesn't matter here because
21995 read_common_block is going to reset it. */
21996 if (!suppress_add)
21997 list_to_add = cu->list_in_scope;
21998 }
21999 else if (attr2 != nullptr && attr2->as_boolean ()
22000 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22001 {
22002 /* A variable with DW_AT_external is never static, but it
22003 may be block-scoped. */
22004 list_to_add
22005 = ((cu->list_in_scope
22006 == cu->get_builder ()->get_file_symbols ())
22007 ? cu->get_builder ()->get_global_symbols ()
22008 : cu->list_in_scope);
22009
22010 sym->set_aclass_index (LOC_UNRESOLVED);
22011 }
22012 else if (!die_is_declaration (die, cu))
22013 {
22014 /* Use the default LOC_OPTIMIZED_OUT class. */
22015 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
22016 if (!suppress_add)
22017 list_to_add = cu->list_in_scope;
22018 }
22019 }
22020 break;
22021 case DW_TAG_formal_parameter:
22022 {
22023 /* If we are inside a function, mark this as an argument. If
22024 not, we might be looking at an argument to an inlined function
22025 when we do not have enough information to show inlined frames;
22026 pretend it's a local variable in that case so that the user can
22027 still see it. */
22028 struct context_stack *curr
22029 = cu->get_builder ()->get_current_context_stack ();
22030 if (curr != nullptr && curr->name != nullptr)
22031 sym->set_is_argument (1);
22032 attr = dwarf2_attr (die, DW_AT_location, cu);
22033 if (attr != nullptr)
22034 {
22035 var_decode_location (attr, sym, cu);
22036 }
22037 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22038 if (attr != nullptr)
22039 {
22040 dwarf2_const_value (attr, sym, cu);
22041 }
22042
22043 list_to_add = cu->list_in_scope;
22044 }
22045 break;
22046 case DW_TAG_unspecified_parameters:
22047 /* From varargs functions; gdb doesn't seem to have any
22048 interest in this information, so just ignore it for now.
22049 (FIXME?) */
22050 break;
22051 case DW_TAG_template_type_param:
22052 suppress_add = 1;
22053 /* Fall through. */
22054 case DW_TAG_class_type:
22055 case DW_TAG_interface_type:
22056 case DW_TAG_structure_type:
22057 case DW_TAG_union_type:
22058 case DW_TAG_set_type:
22059 case DW_TAG_enumeration_type:
22060 case DW_TAG_namelist:
22061 if (die->tag == DW_TAG_namelist)
22062 {
22063 sym->set_aclass_index (LOC_STATIC);
22064 sym->set_domain (VAR_DOMAIN);
22065 }
22066 else
22067 {
22068 sym->set_aclass_index (LOC_TYPEDEF);
22069 sym->set_domain (STRUCT_DOMAIN);
22070 }
22071 {
22072 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22073 really ever be static objects: otherwise, if you try
22074 to, say, break of a class's method and you're in a file
22075 which doesn't mention that class, it won't work unless
22076 the check for all static symbols in lookup_symbol_aux
22077 saves you. See the OtherFileClass tests in
22078 gdb.c++/namespace.exp. */
22079
22080 if (!suppress_add)
22081 {
22082 buildsym_compunit *builder = cu->get_builder ();
22083 list_to_add
22084 = (cu->list_in_scope == builder->get_file_symbols ()
22085 && cu->per_cu->lang == language_cplus
22086 ? builder->get_global_symbols ()
22087 : cu->list_in_scope);
22088
22089 /* The semantics of C++ state that "struct foo {
22090 ... }" also defines a typedef for "foo". */
22091 if (cu->per_cu->lang == language_cplus
22092 || cu->per_cu->lang == language_ada
22093 || cu->per_cu->lang == language_d
22094 || cu->per_cu->lang == language_rust)
22095 {
22096 /* The symbol's name is already allocated along
22097 with this objfile, so we don't need to
22098 duplicate it for the type. */
22099 if (sym->type ()->name () == 0)
22100 sym->type ()->set_name (sym->search_name ());
22101 }
22102 }
22103 }
22104 break;
22105 case DW_TAG_typedef:
22106 sym->set_aclass_index (LOC_TYPEDEF);
22107 sym->set_domain (VAR_DOMAIN);
22108 list_to_add = cu->list_in_scope;
22109 break;
22110 case DW_TAG_array_type:
22111 case DW_TAG_base_type:
22112 case DW_TAG_subrange_type:
22113 case DW_TAG_generic_subrange:
22114 sym->set_aclass_index (LOC_TYPEDEF);
22115 sym->set_domain (VAR_DOMAIN);
22116 list_to_add = cu->list_in_scope;
22117 break;
22118 case DW_TAG_enumerator:
22119 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22120 if (attr != nullptr)
22121 {
22122 dwarf2_const_value (attr, sym, cu);
22123 }
22124 {
22125 /* NOTE: carlton/2003-11-10: See comment above in the
22126 DW_TAG_class_type, etc. block. */
22127
22128 list_to_add
22129 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22130 && cu->per_cu->lang == language_cplus
22131 ? cu->get_builder ()->get_global_symbols ()
22132 : cu->list_in_scope);
22133 }
22134 break;
22135 case DW_TAG_imported_declaration:
22136 case DW_TAG_namespace:
22137 sym->set_aclass_index (LOC_TYPEDEF);
22138 list_to_add = cu->get_builder ()->get_global_symbols ();
22139 break;
22140 case DW_TAG_module:
22141 sym->set_aclass_index (LOC_TYPEDEF);
22142 sym->set_domain (MODULE_DOMAIN);
22143 list_to_add = cu->get_builder ()->get_global_symbols ();
22144 break;
22145 case DW_TAG_common_block:
22146 sym->set_aclass_index (LOC_COMMON_BLOCK);
22147 sym->set_domain (COMMON_BLOCK_DOMAIN);
22148 add_symbol_to_list (sym, cu->list_in_scope);
22149 break;
22150 default:
22151 /* Not a tag we recognize. Hopefully we aren't processing
22152 trash data, but since we must specifically ignore things
22153 we don't recognize, there is nothing else we should do at
22154 this point. */
22155 complaint (_("unsupported tag: '%s'"),
22156 dwarf_tag_name (die->tag));
22157 break;
22158 }
22159
22160 if (suppress_add)
22161 {
22162 sym->hash_next = objfile->template_symbols;
22163 objfile->template_symbols = sym;
22164 list_to_add = NULL;
22165 }
22166
22167 if (list_to_add != NULL)
22168 add_symbol_to_list (sym, list_to_add);
22169
22170 /* For the benefit of old versions of GCC, check for anonymous
22171 namespaces based on the demangled name. */
22172 if (!cu->processing_has_namespace_info
22173 && cu->per_cu->lang == language_cplus)
22174 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22175 }
22176 return (sym);
22177 }
22178
22179 /* Given an attr with a DW_FORM_dataN value in host byte order,
22180 zero-extend it as appropriate for the symbol's type. The DWARF
22181 standard (v4) is not entirely clear about the meaning of using
22182 DW_FORM_dataN for a constant with a signed type, where the type is
22183 wider than the data. The conclusion of a discussion on the DWARF
22184 list was that this is unspecified. We choose to always zero-extend
22185 because that is the interpretation long in use by GCC. */
22186
22187 static gdb_byte *
22188 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22189 struct dwarf2_cu *cu, LONGEST *value, int bits)
22190 {
22191 struct objfile *objfile = cu->per_objfile->objfile;
22192 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22193 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22194 LONGEST l = attr->constant_value (0);
22195
22196 if (bits < sizeof (*value) * 8)
22197 {
22198 l &= ((LONGEST) 1 << bits) - 1;
22199 *value = l;
22200 }
22201 else if (bits == sizeof (*value) * 8)
22202 *value = l;
22203 else
22204 {
22205 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22206 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22207 return bytes;
22208 }
22209
22210 return NULL;
22211 }
22212
22213 /* Read a constant value from an attribute. Either set *VALUE, or if
22214 the value does not fit in *VALUE, set *BYTES - either already
22215 allocated on the objfile obstack, or newly allocated on OBSTACK,
22216 or, set *BATON, if we translated the constant to a location
22217 expression. */
22218
22219 static void
22220 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22221 const char *name, struct obstack *obstack,
22222 struct dwarf2_cu *cu,
22223 LONGEST *value, const gdb_byte **bytes,
22224 struct dwarf2_locexpr_baton **baton)
22225 {
22226 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22227 struct objfile *objfile = per_objfile->objfile;
22228 struct comp_unit_head *cu_header = &cu->header;
22229 struct dwarf_block *blk;
22230 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22231 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22232
22233 *value = 0;
22234 *bytes = NULL;
22235 *baton = NULL;
22236
22237 switch (attr->form)
22238 {
22239 case DW_FORM_addr:
22240 case DW_FORM_addrx:
22241 case DW_FORM_GNU_addr_index:
22242 {
22243 gdb_byte *data;
22244
22245 if (TYPE_LENGTH (type) != cu_header->addr_size)
22246 dwarf2_const_value_length_mismatch_complaint (name,
22247 cu_header->addr_size,
22248 TYPE_LENGTH (type));
22249 /* Symbols of this form are reasonably rare, so we just
22250 piggyback on the existing location code rather than writing
22251 a new implementation of symbol_computed_ops. */
22252 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22253 (*baton)->per_objfile = per_objfile;
22254 (*baton)->per_cu = cu->per_cu;
22255 gdb_assert ((*baton)->per_cu);
22256
22257 (*baton)->size = 2 + cu_header->addr_size;
22258 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22259 (*baton)->data = data;
22260
22261 data[0] = DW_OP_addr;
22262 store_unsigned_integer (&data[1], cu_header->addr_size,
22263 byte_order, attr->as_address ());
22264 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22265 }
22266 break;
22267 case DW_FORM_string:
22268 case DW_FORM_strp:
22269 case DW_FORM_strx:
22270 case DW_FORM_GNU_str_index:
22271 case DW_FORM_GNU_strp_alt:
22272 /* The string is already allocated on the objfile obstack, point
22273 directly to it. */
22274 *bytes = (const gdb_byte *) attr->as_string ();
22275 break;
22276 case DW_FORM_block1:
22277 case DW_FORM_block2:
22278 case DW_FORM_block4:
22279 case DW_FORM_block:
22280 case DW_FORM_exprloc:
22281 case DW_FORM_data16:
22282 blk = attr->as_block ();
22283 if (TYPE_LENGTH (type) != blk->size)
22284 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22285 TYPE_LENGTH (type));
22286 *bytes = blk->data;
22287 break;
22288
22289 /* The DW_AT_const_value attributes are supposed to carry the
22290 symbol's value "represented as it would be on the target
22291 architecture." By the time we get here, it's already been
22292 converted to host endianness, so we just need to sign- or
22293 zero-extend it as appropriate. */
22294 case DW_FORM_data1:
22295 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22296 break;
22297 case DW_FORM_data2:
22298 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22299 break;
22300 case DW_FORM_data4:
22301 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22302 break;
22303 case DW_FORM_data8:
22304 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22305 break;
22306
22307 case DW_FORM_sdata:
22308 case DW_FORM_implicit_const:
22309 *value = attr->as_signed ();
22310 break;
22311
22312 case DW_FORM_udata:
22313 *value = attr->as_unsigned ();
22314 break;
22315
22316 default:
22317 complaint (_("unsupported const value attribute form: '%s'"),
22318 dwarf_form_name (attr->form));
22319 *value = 0;
22320 break;
22321 }
22322 }
22323
22324
22325 /* Copy constant value from an attribute to a symbol. */
22326
22327 static void
22328 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22329 struct dwarf2_cu *cu)
22330 {
22331 struct objfile *objfile = cu->per_objfile->objfile;
22332 LONGEST value;
22333 const gdb_byte *bytes;
22334 struct dwarf2_locexpr_baton *baton;
22335
22336 dwarf2_const_value_attr (attr, sym->type (),
22337 sym->print_name (),
22338 &objfile->objfile_obstack, cu,
22339 &value, &bytes, &baton);
22340
22341 if (baton != NULL)
22342 {
22343 SYMBOL_LOCATION_BATON (sym) = baton;
22344 sym->set_aclass_index (dwarf2_locexpr_index);
22345 }
22346 else if (bytes != NULL)
22347 {
22348 sym->set_value_bytes (bytes);
22349 sym->set_aclass_index (LOC_CONST_BYTES);
22350 }
22351 else
22352 {
22353 sym->set_value_longest (value);
22354 sym->set_aclass_index (LOC_CONST);
22355 }
22356 }
22357
22358 /* Return the type of the die in question using its DW_AT_type attribute. */
22359
22360 static struct type *
22361 die_type (struct die_info *die, struct dwarf2_cu *cu)
22362 {
22363 struct attribute *type_attr;
22364
22365 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22366 if (!type_attr)
22367 {
22368 struct objfile *objfile = cu->per_objfile->objfile;
22369 /* A missing DW_AT_type represents a void type. */
22370 return objfile_type (objfile)->builtin_void;
22371 }
22372
22373 return lookup_die_type (die, type_attr, cu);
22374 }
22375
22376 /* True iff CU's producer generates GNAT Ada auxiliary information
22377 that allows to find parallel types through that information instead
22378 of having to do expensive parallel lookups by type name. */
22379
22380 static int
22381 need_gnat_info (struct dwarf2_cu *cu)
22382 {
22383 /* Assume that the Ada compiler was GNAT, which always produces
22384 the auxiliary information. */
22385 return (cu->per_cu->lang == language_ada);
22386 }
22387
22388 /* Return the auxiliary type of the die in question using its
22389 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22390 attribute is not present. */
22391
22392 static struct type *
22393 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22394 {
22395 struct attribute *type_attr;
22396
22397 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22398 if (!type_attr)
22399 return NULL;
22400
22401 return lookup_die_type (die, type_attr, cu);
22402 }
22403
22404 /* If DIE has a descriptive_type attribute, then set the TYPE's
22405 descriptive type accordingly. */
22406
22407 static void
22408 set_descriptive_type (struct type *type, struct die_info *die,
22409 struct dwarf2_cu *cu)
22410 {
22411 struct type *descriptive_type = die_descriptive_type (die, cu);
22412
22413 if (descriptive_type)
22414 {
22415 ALLOCATE_GNAT_AUX_TYPE (type);
22416 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22417 }
22418 }
22419
22420 /* Return the containing type of the die in question using its
22421 DW_AT_containing_type attribute. */
22422
22423 static struct type *
22424 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22425 {
22426 struct attribute *type_attr;
22427 struct objfile *objfile = cu->per_objfile->objfile;
22428
22429 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22430 if (!type_attr)
22431 error (_("Dwarf Error: Problem turning containing type into gdb type "
22432 "[in module %s]"), objfile_name (objfile));
22433
22434 return lookup_die_type (die, type_attr, cu);
22435 }
22436
22437 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22438
22439 static struct type *
22440 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22441 {
22442 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22443 struct objfile *objfile = per_objfile->objfile;
22444 char *saved;
22445
22446 std::string message
22447 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22448 objfile_name (objfile),
22449 sect_offset_str (cu->header.sect_off),
22450 sect_offset_str (die->sect_off));
22451 saved = obstack_strdup (&objfile->objfile_obstack, message);
22452
22453 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22454 }
22455
22456 /* Look up the type of DIE in CU using its type attribute ATTR.
22457 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22458 DW_AT_containing_type.
22459 If there is no type substitute an error marker. */
22460
22461 static struct type *
22462 lookup_die_type (struct die_info *die, const struct attribute *attr,
22463 struct dwarf2_cu *cu)
22464 {
22465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22466 struct objfile *objfile = per_objfile->objfile;
22467 struct type *this_type;
22468
22469 gdb_assert (attr->name == DW_AT_type
22470 || attr->name == DW_AT_GNAT_descriptive_type
22471 || attr->name == DW_AT_containing_type);
22472
22473 /* First see if we have it cached. */
22474
22475 if (attr->form == DW_FORM_GNU_ref_alt)
22476 {
22477 struct dwarf2_per_cu_data *per_cu;
22478 sect_offset sect_off = attr->get_ref_die_offset ();
22479
22480 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22481 per_objfile->per_bfd);
22482 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22483 }
22484 else if (attr->form_is_ref ())
22485 {
22486 sect_offset sect_off = attr->get_ref_die_offset ();
22487
22488 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22489 }
22490 else if (attr->form == DW_FORM_ref_sig8)
22491 {
22492 ULONGEST signature = attr->as_signature ();
22493
22494 return get_signatured_type (die, signature, cu);
22495 }
22496 else
22497 {
22498 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22499 " at %s [in module %s]"),
22500 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22501 objfile_name (objfile));
22502 return build_error_marker_type (cu, die);
22503 }
22504
22505 /* If not cached we need to read it in. */
22506
22507 if (this_type == NULL)
22508 {
22509 struct die_info *type_die = NULL;
22510 struct dwarf2_cu *type_cu = cu;
22511
22512 if (attr->form_is_ref ())
22513 type_die = follow_die_ref (die, attr, &type_cu);
22514 if (type_die == NULL)
22515 return build_error_marker_type (cu, die);
22516 /* If we find the type now, it's probably because the type came
22517 from an inter-CU reference and the type's CU got expanded before
22518 ours. */
22519 this_type = read_type_die (type_die, type_cu);
22520 }
22521
22522 /* If we still don't have a type use an error marker. */
22523
22524 if (this_type == NULL)
22525 return build_error_marker_type (cu, die);
22526
22527 return this_type;
22528 }
22529
22530 /* Return the type in DIE, CU.
22531 Returns NULL for invalid types.
22532
22533 This first does a lookup in die_type_hash,
22534 and only reads the die in if necessary.
22535
22536 NOTE: This can be called when reading in partial or full symbols. */
22537
22538 static struct type *
22539 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22540 {
22541 struct type *this_type;
22542
22543 this_type = get_die_type (die, cu);
22544 if (this_type)
22545 return this_type;
22546
22547 return read_type_die_1 (die, cu);
22548 }
22549
22550 /* Read the type in DIE, CU.
22551 Returns NULL for invalid types. */
22552
22553 static struct type *
22554 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22555 {
22556 struct type *this_type = NULL;
22557
22558 switch (die->tag)
22559 {
22560 case DW_TAG_class_type:
22561 case DW_TAG_interface_type:
22562 case DW_TAG_structure_type:
22563 case DW_TAG_union_type:
22564 this_type = read_structure_type (die, cu);
22565 break;
22566 case DW_TAG_enumeration_type:
22567 this_type = read_enumeration_type (die, cu);
22568 break;
22569 case DW_TAG_subprogram:
22570 case DW_TAG_subroutine_type:
22571 case DW_TAG_inlined_subroutine:
22572 this_type = read_subroutine_type (die, cu);
22573 break;
22574 case DW_TAG_array_type:
22575 this_type = read_array_type (die, cu);
22576 break;
22577 case DW_TAG_set_type:
22578 this_type = read_set_type (die, cu);
22579 break;
22580 case DW_TAG_pointer_type:
22581 this_type = read_tag_pointer_type (die, cu);
22582 break;
22583 case DW_TAG_ptr_to_member_type:
22584 this_type = read_tag_ptr_to_member_type (die, cu);
22585 break;
22586 case DW_TAG_reference_type:
22587 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22588 break;
22589 case DW_TAG_rvalue_reference_type:
22590 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22591 break;
22592 case DW_TAG_const_type:
22593 this_type = read_tag_const_type (die, cu);
22594 break;
22595 case DW_TAG_volatile_type:
22596 this_type = read_tag_volatile_type (die, cu);
22597 break;
22598 case DW_TAG_restrict_type:
22599 this_type = read_tag_restrict_type (die, cu);
22600 break;
22601 case DW_TAG_string_type:
22602 this_type = read_tag_string_type (die, cu);
22603 break;
22604 case DW_TAG_typedef:
22605 this_type = read_typedef (die, cu);
22606 break;
22607 case DW_TAG_generic_subrange:
22608 case DW_TAG_subrange_type:
22609 this_type = read_subrange_type (die, cu);
22610 break;
22611 case DW_TAG_base_type:
22612 this_type = read_base_type (die, cu);
22613 break;
22614 case DW_TAG_unspecified_type:
22615 this_type = read_unspecified_type (die, cu);
22616 break;
22617 case DW_TAG_namespace:
22618 this_type = read_namespace_type (die, cu);
22619 break;
22620 case DW_TAG_module:
22621 this_type = read_module_type (die, cu);
22622 break;
22623 case DW_TAG_atomic_type:
22624 this_type = read_tag_atomic_type (die, cu);
22625 break;
22626 default:
22627 complaint (_("unexpected tag in read_type_die: '%s'"),
22628 dwarf_tag_name (die->tag));
22629 break;
22630 }
22631
22632 return this_type;
22633 }
22634
22635 /* See if we can figure out if the class lives in a namespace. We do
22636 this by looking for a member function; its demangled name will
22637 contain namespace info, if there is any.
22638 Return the computed name or NULL.
22639 Space for the result is allocated on the objfile's obstack.
22640 This is the full-die version of guess_partial_die_structure_name.
22641 In this case we know DIE has no useful parent. */
22642
22643 static const char *
22644 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22645 {
22646 struct die_info *spec_die;
22647 struct dwarf2_cu *spec_cu;
22648 struct die_info *child;
22649 struct objfile *objfile = cu->per_objfile->objfile;
22650
22651 spec_cu = cu;
22652 spec_die = die_specification (die, &spec_cu);
22653 if (spec_die != NULL)
22654 {
22655 die = spec_die;
22656 cu = spec_cu;
22657 }
22658
22659 for (child = die->child;
22660 child != NULL;
22661 child = child->sibling)
22662 {
22663 if (child->tag == DW_TAG_subprogram)
22664 {
22665 const char *linkage_name = dw2_linkage_name (child, cu);
22666
22667 if (linkage_name != NULL)
22668 {
22669 gdb::unique_xmalloc_ptr<char> actual_name
22670 (cu->language_defn->class_name_from_physname (linkage_name));
22671 const char *name = NULL;
22672
22673 if (actual_name != NULL)
22674 {
22675 const char *die_name = dwarf2_name (die, cu);
22676
22677 if (die_name != NULL
22678 && strcmp (die_name, actual_name.get ()) != 0)
22679 {
22680 /* Strip off the class name from the full name.
22681 We want the prefix. */
22682 int die_name_len = strlen (die_name);
22683 int actual_name_len = strlen (actual_name.get ());
22684 const char *ptr = actual_name.get ();
22685
22686 /* Test for '::' as a sanity check. */
22687 if (actual_name_len > die_name_len + 2
22688 && ptr[actual_name_len - die_name_len - 1] == ':')
22689 name = obstack_strndup (
22690 &objfile->per_bfd->storage_obstack,
22691 ptr, actual_name_len - die_name_len - 2);
22692 }
22693 }
22694 return name;
22695 }
22696 }
22697 }
22698
22699 return NULL;
22700 }
22701
22702 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22703 prefix part in such case. See
22704 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22705
22706 static const char *
22707 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22708 {
22709 struct attribute *attr;
22710 const char *base;
22711
22712 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22713 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22714 return NULL;
22715
22716 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22717 return NULL;
22718
22719 attr = dw2_linkage_name_attr (die, cu);
22720 const char *attr_name = attr->as_string ();
22721 if (attr == NULL || attr_name == NULL)
22722 return NULL;
22723
22724 /* dwarf2_name had to be already called. */
22725 gdb_assert (attr->canonical_string_p ());
22726
22727 /* Strip the base name, keep any leading namespaces/classes. */
22728 base = strrchr (attr_name, ':');
22729 if (base == NULL || base == attr_name || base[-1] != ':')
22730 return "";
22731
22732 struct objfile *objfile = cu->per_objfile->objfile;
22733 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22734 attr_name,
22735 &base[-1] - attr_name);
22736 }
22737
22738 /* Return the name of the namespace/class that DIE is defined within,
22739 or "" if we can't tell. The caller should not xfree the result.
22740
22741 For example, if we're within the method foo() in the following
22742 code:
22743
22744 namespace N {
22745 class C {
22746 void foo () {
22747 }
22748 };
22749 }
22750
22751 then determine_prefix on foo's die will return "N::C". */
22752
22753 static const char *
22754 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22755 {
22756 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22757 struct die_info *parent, *spec_die;
22758 struct dwarf2_cu *spec_cu;
22759 struct type *parent_type;
22760 const char *retval;
22761
22762 if (cu->per_cu->lang != language_cplus
22763 && cu->per_cu->lang != language_fortran
22764 && cu->per_cu->lang != language_d
22765 && cu->per_cu->lang != language_rust)
22766 return "";
22767
22768 retval = anonymous_struct_prefix (die, cu);
22769 if (retval)
22770 return retval;
22771
22772 /* We have to be careful in the presence of DW_AT_specification.
22773 For example, with GCC 3.4, given the code
22774
22775 namespace N {
22776 void foo() {
22777 // Definition of N::foo.
22778 }
22779 }
22780
22781 then we'll have a tree of DIEs like this:
22782
22783 1: DW_TAG_compile_unit
22784 2: DW_TAG_namespace // N
22785 3: DW_TAG_subprogram // declaration of N::foo
22786 4: DW_TAG_subprogram // definition of N::foo
22787 DW_AT_specification // refers to die #3
22788
22789 Thus, when processing die #4, we have to pretend that we're in
22790 the context of its DW_AT_specification, namely the contex of die
22791 #3. */
22792 spec_cu = cu;
22793 spec_die = die_specification (die, &spec_cu);
22794 if (spec_die == NULL)
22795 parent = die->parent;
22796 else
22797 {
22798 parent = spec_die->parent;
22799 cu = spec_cu;
22800 }
22801
22802 if (parent == NULL)
22803 return "";
22804 else if (parent->building_fullname)
22805 {
22806 const char *name;
22807 const char *parent_name;
22808
22809 /* It has been seen on RealView 2.2 built binaries,
22810 DW_TAG_template_type_param types actually _defined_ as
22811 children of the parent class:
22812
22813 enum E {};
22814 template class <class Enum> Class{};
22815 Class<enum E> class_e;
22816
22817 1: DW_TAG_class_type (Class)
22818 2: DW_TAG_enumeration_type (E)
22819 3: DW_TAG_enumerator (enum1:0)
22820 3: DW_TAG_enumerator (enum2:1)
22821 ...
22822 2: DW_TAG_template_type_param
22823 DW_AT_type DW_FORM_ref_udata (E)
22824
22825 Besides being broken debug info, it can put GDB into an
22826 infinite loop. Consider:
22827
22828 When we're building the full name for Class<E>, we'll start
22829 at Class, and go look over its template type parameters,
22830 finding E. We'll then try to build the full name of E, and
22831 reach here. We're now trying to build the full name of E,
22832 and look over the parent DIE for containing scope. In the
22833 broken case, if we followed the parent DIE of E, we'd again
22834 find Class, and once again go look at its template type
22835 arguments, etc., etc. Simply don't consider such parent die
22836 as source-level parent of this die (it can't be, the language
22837 doesn't allow it), and break the loop here. */
22838 name = dwarf2_name (die, cu);
22839 parent_name = dwarf2_name (parent, cu);
22840 complaint (_("template param type '%s' defined within parent '%s'"),
22841 name ? name : "<unknown>",
22842 parent_name ? parent_name : "<unknown>");
22843 return "";
22844 }
22845 else
22846 switch (parent->tag)
22847 {
22848 case DW_TAG_namespace:
22849 parent_type = read_type_die (parent, cu);
22850 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22851 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22852 Work around this problem here. */
22853 if (cu->per_cu->lang == language_cplus
22854 && strcmp (parent_type->name (), "::") == 0)
22855 return "";
22856 /* We give a name to even anonymous namespaces. */
22857 return parent_type->name ();
22858 case DW_TAG_class_type:
22859 case DW_TAG_interface_type:
22860 case DW_TAG_structure_type:
22861 case DW_TAG_union_type:
22862 case DW_TAG_module:
22863 parent_type = read_type_die (parent, cu);
22864 if (parent_type->name () != NULL)
22865 return parent_type->name ();
22866 else
22867 /* An anonymous structure is only allowed non-static data
22868 members; no typedefs, no member functions, et cetera.
22869 So it does not need a prefix. */
22870 return "";
22871 case DW_TAG_compile_unit:
22872 case DW_TAG_partial_unit:
22873 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22874 if (cu->per_cu->lang == language_cplus
22875 && !per_objfile->per_bfd->types.empty ()
22876 && die->child != NULL
22877 && (die->tag == DW_TAG_class_type
22878 || die->tag == DW_TAG_structure_type
22879 || die->tag == DW_TAG_union_type))
22880 {
22881 const char *name = guess_full_die_structure_name (die, cu);
22882 if (name != NULL)
22883 return name;
22884 }
22885 return "";
22886 case DW_TAG_subprogram:
22887 /* Nested subroutines in Fortran get a prefix with the name
22888 of the parent's subroutine. */
22889 if (cu->per_cu->lang == language_fortran)
22890 {
22891 if ((die->tag == DW_TAG_subprogram)
22892 && (dwarf2_name (parent, cu) != NULL))
22893 return dwarf2_name (parent, cu);
22894 }
22895 return "";
22896 case DW_TAG_enumeration_type:
22897 parent_type = read_type_die (parent, cu);
22898 if (parent_type->is_declared_class ())
22899 {
22900 if (parent_type->name () != NULL)
22901 return parent_type->name ();
22902 return "";
22903 }
22904 /* Fall through. */
22905 default:
22906 return determine_prefix (parent, cu);
22907 }
22908 }
22909
22910 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22911 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22912 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22913 an obconcat, otherwise allocate storage for the result. The CU argument is
22914 used to determine the language and hence, the appropriate separator. */
22915
22916 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22917
22918 static char *
22919 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22920 int physname, struct dwarf2_cu *cu)
22921 {
22922 const char *lead = "";
22923 const char *sep;
22924
22925 if (suffix == NULL || suffix[0] == '\0'
22926 || prefix == NULL || prefix[0] == '\0')
22927 sep = "";
22928 else if (cu->per_cu->lang == language_d)
22929 {
22930 /* For D, the 'main' function could be defined in any module, but it
22931 should never be prefixed. */
22932 if (strcmp (suffix, "D main") == 0)
22933 {
22934 prefix = "";
22935 sep = "";
22936 }
22937 else
22938 sep = ".";
22939 }
22940 else if (cu->per_cu->lang == language_fortran && physname)
22941 {
22942 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22943 DW_AT_MIPS_linkage_name is preferred and used instead. */
22944
22945 lead = "__";
22946 sep = "_MOD_";
22947 }
22948 else
22949 sep = "::";
22950
22951 if (prefix == NULL)
22952 prefix = "";
22953 if (suffix == NULL)
22954 suffix = "";
22955
22956 if (obs == NULL)
22957 {
22958 char *retval
22959 = ((char *)
22960 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22961
22962 strcpy (retval, lead);
22963 strcat (retval, prefix);
22964 strcat (retval, sep);
22965 strcat (retval, suffix);
22966 return retval;
22967 }
22968 else
22969 {
22970 /* We have an obstack. */
22971 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22972 }
22973 }
22974
22975 /* Get name of a die, return NULL if not found. */
22976
22977 static const char *
22978 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22979 struct objfile *objfile)
22980 {
22981 if (name && cu->per_cu->lang == language_cplus)
22982 {
22983 gdb::unique_xmalloc_ptr<char> canon_name
22984 = cp_canonicalize_string (name);
22985
22986 if (canon_name != nullptr)
22987 name = objfile->intern (canon_name.get ());
22988 }
22989
22990 return name;
22991 }
22992
22993 /* Get name of a die, return NULL if not found.
22994 Anonymous namespaces are converted to their magic string. */
22995
22996 static const char *
22997 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22998 {
22999 struct attribute *attr;
23000 struct objfile *objfile = cu->per_objfile->objfile;
23001
23002 attr = dwarf2_attr (die, DW_AT_name, cu);
23003 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23004 if (attr_name == nullptr
23005 && die->tag != DW_TAG_namespace
23006 && die->tag != DW_TAG_class_type
23007 && die->tag != DW_TAG_interface_type
23008 && die->tag != DW_TAG_structure_type
23009 && die->tag != DW_TAG_namelist
23010 && die->tag != DW_TAG_union_type)
23011 return NULL;
23012
23013 switch (die->tag)
23014 {
23015 case DW_TAG_compile_unit:
23016 case DW_TAG_partial_unit:
23017 /* Compilation units have a DW_AT_name that is a filename, not
23018 a source language identifier. */
23019 case DW_TAG_enumeration_type:
23020 case DW_TAG_enumerator:
23021 /* These tags always have simple identifiers already; no need
23022 to canonicalize them. */
23023 return attr_name;
23024
23025 case DW_TAG_namespace:
23026 if (attr_name != nullptr)
23027 return attr_name;
23028 return CP_ANONYMOUS_NAMESPACE_STR;
23029
23030 case DW_TAG_class_type:
23031 case DW_TAG_interface_type:
23032 case DW_TAG_structure_type:
23033 case DW_TAG_union_type:
23034 case DW_TAG_namelist:
23035 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23036 structures or unions. These were of the form "._%d" in GCC 4.1,
23037 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23038 and GCC 4.4. We work around this problem by ignoring these. */
23039 if (attr_name != nullptr
23040 && (startswith (attr_name, "._")
23041 || startswith (attr_name, "<anonymous")))
23042 return NULL;
23043
23044 /* GCC might emit a nameless typedef that has a linkage name. See
23045 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23046 if (!attr || attr_name == NULL)
23047 {
23048 attr = dw2_linkage_name_attr (die, cu);
23049 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23050 if (attr == NULL || attr_name == NULL)
23051 return NULL;
23052
23053 /* Avoid demangling attr_name the second time on a second
23054 call for the same DIE. */
23055 if (!attr->canonical_string_p ())
23056 {
23057 gdb::unique_xmalloc_ptr<char> demangled
23058 (gdb_demangle (attr_name, DMGL_TYPES));
23059 if (demangled == nullptr)
23060 return nullptr;
23061
23062 attr->set_string_canonical (objfile->intern (demangled.get ()));
23063 attr_name = attr->as_string ();
23064 }
23065
23066 /* Strip any leading namespaces/classes, keep only the
23067 base name. DW_AT_name for named DIEs does not
23068 contain the prefixes. */
23069 const char *base = strrchr (attr_name, ':');
23070 if (base && base > attr_name && base[-1] == ':')
23071 return &base[1];
23072 else
23073 return attr_name;
23074 }
23075 break;
23076
23077 default:
23078 break;
23079 }
23080
23081 if (!attr->canonical_string_p ())
23082 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23083 objfile));
23084 return attr->as_string ();
23085 }
23086
23087 /* Return the die that this die in an extension of, or NULL if there
23088 is none. *EXT_CU is the CU containing DIE on input, and the CU
23089 containing the return value on output. */
23090
23091 static struct die_info *
23092 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23093 {
23094 struct attribute *attr;
23095
23096 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23097 if (attr == NULL)
23098 return NULL;
23099
23100 return follow_die_ref (die, attr, ext_cu);
23101 }
23102
23103 static void
23104 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23105 {
23106 unsigned int i;
23107
23108 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
23109 indent, "",
23110 dwarf_tag_name (die->tag), die->abbrev,
23111 sect_offset_str (die->sect_off));
23112
23113 if (die->parent != NULL)
23114 gdb_printf (f, "%*s parent at offset: %s\n",
23115 indent, "",
23116 sect_offset_str (die->parent->sect_off));
23117
23118 gdb_printf (f, "%*s has children: %s\n",
23119 indent, "",
23120 dwarf_bool_name (die->child != NULL));
23121
23122 gdb_printf (f, "%*s attributes:\n", indent, "");
23123
23124 for (i = 0; i < die->num_attrs; ++i)
23125 {
23126 gdb_printf (f, "%*s %s (%s) ",
23127 indent, "",
23128 dwarf_attr_name (die->attrs[i].name),
23129 dwarf_form_name (die->attrs[i].form));
23130
23131 switch (die->attrs[i].form)
23132 {
23133 case DW_FORM_addr:
23134 case DW_FORM_addrx:
23135 case DW_FORM_GNU_addr_index:
23136 gdb_printf (f, "address: ");
23137 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
23138 break;
23139 case DW_FORM_block2:
23140 case DW_FORM_block4:
23141 case DW_FORM_block:
23142 case DW_FORM_block1:
23143 gdb_printf (f, "block: size %s",
23144 pulongest (die->attrs[i].as_block ()->size));
23145 break;
23146 case DW_FORM_exprloc:
23147 gdb_printf (f, "expression: size %s",
23148 pulongest (die->attrs[i].as_block ()->size));
23149 break;
23150 case DW_FORM_data16:
23151 gdb_printf (f, "constant of 16 bytes");
23152 break;
23153 case DW_FORM_ref_addr:
23154 gdb_printf (f, "ref address: ");
23155 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
23156 break;
23157 case DW_FORM_GNU_ref_alt:
23158 gdb_printf (f, "alt ref address: ");
23159 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
23160 break;
23161 case DW_FORM_ref1:
23162 case DW_FORM_ref2:
23163 case DW_FORM_ref4:
23164 case DW_FORM_ref8:
23165 case DW_FORM_ref_udata:
23166 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
23167 (long) (die->attrs[i].as_unsigned ()));
23168 break;
23169 case DW_FORM_data1:
23170 case DW_FORM_data2:
23171 case DW_FORM_data4:
23172 case DW_FORM_data8:
23173 case DW_FORM_udata:
23174 gdb_printf (f, "constant: %s",
23175 pulongest (die->attrs[i].as_unsigned ()));
23176 break;
23177 case DW_FORM_sec_offset:
23178 gdb_printf (f, "section offset: %s",
23179 pulongest (die->attrs[i].as_unsigned ()));
23180 break;
23181 case DW_FORM_ref_sig8:
23182 gdb_printf (f, "signature: %s",
23183 hex_string (die->attrs[i].as_signature ()));
23184 break;
23185 case DW_FORM_string:
23186 case DW_FORM_strp:
23187 case DW_FORM_line_strp:
23188 case DW_FORM_strx:
23189 case DW_FORM_GNU_str_index:
23190 case DW_FORM_GNU_strp_alt:
23191 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
23192 die->attrs[i].as_string ()
23193 ? die->attrs[i].as_string () : "",
23194 die->attrs[i].canonical_string_p () ? "is" : "not");
23195 break;
23196 case DW_FORM_flag:
23197 if (die->attrs[i].as_boolean ())
23198 gdb_printf (f, "flag: TRUE");
23199 else
23200 gdb_printf (f, "flag: FALSE");
23201 break;
23202 case DW_FORM_flag_present:
23203 gdb_printf (f, "flag: TRUE");
23204 break;
23205 case DW_FORM_indirect:
23206 /* The reader will have reduced the indirect form to
23207 the "base form" so this form should not occur. */
23208 gdb_printf (f,
23209 "unexpected attribute form: DW_FORM_indirect");
23210 break;
23211 case DW_FORM_sdata:
23212 case DW_FORM_implicit_const:
23213 gdb_printf (f, "constant: %s",
23214 plongest (die->attrs[i].as_signed ()));
23215 break;
23216 default:
23217 gdb_printf (f, "unsupported attribute form: %d.",
23218 die->attrs[i].form);
23219 break;
23220 }
23221 gdb_printf (f, "\n");
23222 }
23223 }
23224
23225 static void
23226 dump_die_for_error (struct die_info *die)
23227 {
23228 dump_die_shallow (gdb_stderr, 0, die);
23229 }
23230
23231 static void
23232 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23233 {
23234 int indent = level * 4;
23235
23236 gdb_assert (die != NULL);
23237
23238 if (level >= max_level)
23239 return;
23240
23241 dump_die_shallow (f, indent, die);
23242
23243 if (die->child != NULL)
23244 {
23245 gdb_printf (f, "%*s Children:", indent, "");
23246 if (level + 1 < max_level)
23247 {
23248 gdb_printf (f, "\n");
23249 dump_die_1 (f, level + 1, max_level, die->child);
23250 }
23251 else
23252 {
23253 gdb_printf (f,
23254 " [not printed, max nesting level reached]\n");
23255 }
23256 }
23257
23258 if (die->sibling != NULL && level > 0)
23259 {
23260 dump_die_1 (f, level, max_level, die->sibling);
23261 }
23262 }
23263
23264 /* This is called from the pdie macro in gdbinit.in.
23265 It's not static so gcc will keep a copy callable from gdb. */
23266
23267 void
23268 dump_die (struct die_info *die, int max_level)
23269 {
23270 dump_die_1 (gdb_stdlog, 0, max_level, die);
23271 }
23272
23273 static void
23274 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23275 {
23276 void **slot;
23277
23278 slot = htab_find_slot_with_hash (cu->die_hash, die,
23279 to_underlying (die->sect_off),
23280 INSERT);
23281
23282 *slot = die;
23283 }
23284
23285 /* Follow reference or signature attribute ATTR of SRC_DIE.
23286 On entry *REF_CU is the CU of SRC_DIE.
23287 On exit *REF_CU is the CU of the result. */
23288
23289 static struct die_info *
23290 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23291 struct dwarf2_cu **ref_cu)
23292 {
23293 struct die_info *die;
23294
23295 if (attr->form_is_ref ())
23296 die = follow_die_ref (src_die, attr, ref_cu);
23297 else if (attr->form == DW_FORM_ref_sig8)
23298 die = follow_die_sig (src_die, attr, ref_cu);
23299 else
23300 {
23301 dump_die_for_error (src_die);
23302 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23303 objfile_name ((*ref_cu)->per_objfile->objfile));
23304 }
23305
23306 return die;
23307 }
23308
23309 /* Follow reference OFFSET.
23310 On entry *REF_CU is the CU of the source die referencing OFFSET.
23311 On exit *REF_CU is the CU of the result.
23312 Returns NULL if OFFSET is invalid. */
23313
23314 static struct die_info *
23315 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23316 struct dwarf2_cu **ref_cu)
23317 {
23318 struct die_info temp_die;
23319 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23320 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23321
23322 gdb_assert (cu->per_cu != NULL);
23323
23324 target_cu = cu;
23325
23326 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23327 "source CU contains target offset: %d",
23328 sect_offset_str (cu->per_cu->sect_off),
23329 sect_offset_str (sect_off),
23330 cu->header.offset_in_cu_p (sect_off));
23331
23332 if (cu->per_cu->is_debug_types)
23333 {
23334 /* .debug_types CUs cannot reference anything outside their CU.
23335 If they need to, they have to reference a signatured type via
23336 DW_FORM_ref_sig8. */
23337 if (!cu->header.offset_in_cu_p (sect_off))
23338 return NULL;
23339 }
23340 else if (offset_in_dwz != cu->per_cu->is_dwz
23341 || !cu->header.offset_in_cu_p (sect_off))
23342 {
23343 struct dwarf2_per_cu_data *per_cu;
23344
23345 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23346 per_objfile->per_bfd);
23347
23348 dwarf_read_debug_printf_v ("target CU offset: %s, "
23349 "target CU DIEs loaded: %d",
23350 sect_offset_str (per_cu->sect_off),
23351 per_objfile->get_cu (per_cu) != nullptr);
23352
23353 /* If necessary, add it to the queue and load its DIEs.
23354
23355 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23356 it doesn't mean they are currently loaded. Since we require them
23357 to be loaded, we must check for ourselves. */
23358 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
23359 || per_objfile->get_cu (per_cu) == nullptr)
23360 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23361 false, cu->per_cu->lang);
23362
23363 target_cu = per_objfile->get_cu (per_cu);
23364 gdb_assert (target_cu != nullptr);
23365 }
23366 else if (cu->dies == NULL)
23367 {
23368 /* We're loading full DIEs during partial symbol reading. */
23369 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23370 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23371 language_minimal);
23372 }
23373
23374 *ref_cu = target_cu;
23375 temp_die.sect_off = sect_off;
23376
23377 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23378 &temp_die,
23379 to_underlying (sect_off));
23380 }
23381
23382 /* Follow reference attribute ATTR of SRC_DIE.
23383 On entry *REF_CU is the CU of SRC_DIE.
23384 On exit *REF_CU is the CU of the result. */
23385
23386 static struct die_info *
23387 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23388 struct dwarf2_cu **ref_cu)
23389 {
23390 sect_offset sect_off = attr->get_ref_die_offset ();
23391 struct dwarf2_cu *cu = *ref_cu;
23392 struct die_info *die;
23393
23394 die = follow_die_offset (sect_off,
23395 (attr->form == DW_FORM_GNU_ref_alt
23396 || cu->per_cu->is_dwz),
23397 ref_cu);
23398 if (!die)
23399 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23400 "at %s [in module %s]"),
23401 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23402 objfile_name (cu->per_objfile->objfile));
23403
23404 return die;
23405 }
23406
23407 /* See read.h. */
23408
23409 struct dwarf2_locexpr_baton
23410 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23411 dwarf2_per_cu_data *per_cu,
23412 dwarf2_per_objfile *per_objfile,
23413 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23414 bool resolve_abstract_p)
23415 {
23416 struct die_info *die;
23417 struct attribute *attr;
23418 struct dwarf2_locexpr_baton retval;
23419 struct objfile *objfile = per_objfile->objfile;
23420
23421 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23422 if (cu == nullptr)
23423 cu = load_cu (per_cu, per_objfile, false);
23424
23425 if (cu == nullptr)
23426 {
23427 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23428 Instead just throw an error, not much else we can do. */
23429 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23430 sect_offset_str (sect_off), objfile_name (objfile));
23431 }
23432
23433 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23434 if (!die)
23435 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23436 sect_offset_str (sect_off), objfile_name (objfile));
23437
23438 attr = dwarf2_attr (die, DW_AT_location, cu);
23439 if (!attr && resolve_abstract_p
23440 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23441 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23442 {
23443 CORE_ADDR pc = get_frame_pc ();
23444 CORE_ADDR baseaddr = objfile->text_section_offset ();
23445 struct gdbarch *gdbarch = objfile->arch ();
23446
23447 for (const auto &cand_off
23448 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23449 {
23450 struct dwarf2_cu *cand_cu = cu;
23451 struct die_info *cand
23452 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23453 if (!cand
23454 || !cand->parent
23455 || cand->parent->tag != DW_TAG_subprogram)
23456 continue;
23457
23458 CORE_ADDR pc_low, pc_high;
23459 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23460 if (pc_low == ((CORE_ADDR) -1))
23461 continue;
23462 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23463 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23464 if (!(pc_low <= pc && pc < pc_high))
23465 continue;
23466
23467 die = cand;
23468 attr = dwarf2_attr (die, DW_AT_location, cu);
23469 break;
23470 }
23471 }
23472
23473 if (!attr)
23474 {
23475 /* DWARF: "If there is no such attribute, then there is no effect.".
23476 DATA is ignored if SIZE is 0. */
23477
23478 retval.data = NULL;
23479 retval.size = 0;
23480 }
23481 else if (attr->form_is_section_offset ())
23482 {
23483 struct dwarf2_loclist_baton loclist_baton;
23484 CORE_ADDR pc = get_frame_pc ();
23485 size_t size;
23486
23487 fill_in_loclist_baton (cu, &loclist_baton, attr);
23488
23489 retval.data = dwarf2_find_location_expression (&loclist_baton,
23490 &size, pc);
23491 retval.size = size;
23492 }
23493 else
23494 {
23495 if (!attr->form_is_block ())
23496 error (_("Dwarf Error: DIE at %s referenced in module %s "
23497 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23498 sect_offset_str (sect_off), objfile_name (objfile));
23499
23500 struct dwarf_block *block = attr->as_block ();
23501 retval.data = block->data;
23502 retval.size = block->size;
23503 }
23504 retval.per_objfile = per_objfile;
23505 retval.per_cu = cu->per_cu;
23506
23507 per_objfile->age_comp_units ();
23508
23509 return retval;
23510 }
23511
23512 /* See read.h. */
23513
23514 struct dwarf2_locexpr_baton
23515 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23516 dwarf2_per_cu_data *per_cu,
23517 dwarf2_per_objfile *per_objfile,
23518 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23519 {
23520 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23521
23522 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23523 get_frame_pc);
23524 }
23525
23526 /* Write a constant of a given type as target-ordered bytes into
23527 OBSTACK. */
23528
23529 static const gdb_byte *
23530 write_constant_as_bytes (struct obstack *obstack,
23531 enum bfd_endian byte_order,
23532 struct type *type,
23533 ULONGEST value,
23534 LONGEST *len)
23535 {
23536 gdb_byte *result;
23537
23538 *len = TYPE_LENGTH (type);
23539 result = (gdb_byte *) obstack_alloc (obstack, *len);
23540 store_unsigned_integer (result, *len, byte_order, value);
23541
23542 return result;
23543 }
23544
23545 /* See read.h. */
23546
23547 const gdb_byte *
23548 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23549 dwarf2_per_cu_data *per_cu,
23550 dwarf2_per_objfile *per_objfile,
23551 obstack *obstack,
23552 LONGEST *len)
23553 {
23554 struct die_info *die;
23555 struct attribute *attr;
23556 const gdb_byte *result = NULL;
23557 struct type *type;
23558 LONGEST value;
23559 enum bfd_endian byte_order;
23560 struct objfile *objfile = per_objfile->objfile;
23561
23562 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23563 if (cu == nullptr)
23564 cu = load_cu (per_cu, per_objfile, false);
23565
23566 if (cu == nullptr)
23567 {
23568 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23569 Instead just throw an error, not much else we can do. */
23570 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23571 sect_offset_str (sect_off), objfile_name (objfile));
23572 }
23573
23574 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23575 if (!die)
23576 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23577 sect_offset_str (sect_off), objfile_name (objfile));
23578
23579 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23580 if (attr == NULL)
23581 return NULL;
23582
23583 byte_order = (bfd_big_endian (objfile->obfd)
23584 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23585
23586 switch (attr->form)
23587 {
23588 case DW_FORM_addr:
23589 case DW_FORM_addrx:
23590 case DW_FORM_GNU_addr_index:
23591 {
23592 gdb_byte *tem;
23593
23594 *len = cu->header.addr_size;
23595 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23596 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23597 result = tem;
23598 }
23599 break;
23600 case DW_FORM_string:
23601 case DW_FORM_strp:
23602 case DW_FORM_strx:
23603 case DW_FORM_GNU_str_index:
23604 case DW_FORM_GNU_strp_alt:
23605 /* The string is already allocated on the objfile obstack, point
23606 directly to it. */
23607 {
23608 const char *attr_name = attr->as_string ();
23609 result = (const gdb_byte *) attr_name;
23610 *len = strlen (attr_name);
23611 }
23612 break;
23613 case DW_FORM_block1:
23614 case DW_FORM_block2:
23615 case DW_FORM_block4:
23616 case DW_FORM_block:
23617 case DW_FORM_exprloc:
23618 case DW_FORM_data16:
23619 {
23620 struct dwarf_block *block = attr->as_block ();
23621 result = block->data;
23622 *len = block->size;
23623 }
23624 break;
23625
23626 /* The DW_AT_const_value attributes are supposed to carry the
23627 symbol's value "represented as it would be on the target
23628 architecture." By the time we get here, it's already been
23629 converted to host endianness, so we just need to sign- or
23630 zero-extend it as appropriate. */
23631 case DW_FORM_data1:
23632 type = die_type (die, cu);
23633 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23634 if (result == NULL)
23635 result = write_constant_as_bytes (obstack, byte_order,
23636 type, value, len);
23637 break;
23638 case DW_FORM_data2:
23639 type = die_type (die, cu);
23640 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23641 if (result == NULL)
23642 result = write_constant_as_bytes (obstack, byte_order,
23643 type, value, len);
23644 break;
23645 case DW_FORM_data4:
23646 type = die_type (die, cu);
23647 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23648 if (result == NULL)
23649 result = write_constant_as_bytes (obstack, byte_order,
23650 type, value, len);
23651 break;
23652 case DW_FORM_data8:
23653 type = die_type (die, cu);
23654 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23655 if (result == NULL)
23656 result = write_constant_as_bytes (obstack, byte_order,
23657 type, value, len);
23658 break;
23659
23660 case DW_FORM_sdata:
23661 case DW_FORM_implicit_const:
23662 type = die_type (die, cu);
23663 result = write_constant_as_bytes (obstack, byte_order,
23664 type, attr->as_signed (), len);
23665 break;
23666
23667 case DW_FORM_udata:
23668 type = die_type (die, cu);
23669 result = write_constant_as_bytes (obstack, byte_order,
23670 type, attr->as_unsigned (), len);
23671 break;
23672
23673 default:
23674 complaint (_("unsupported const value attribute form: '%s'"),
23675 dwarf_form_name (attr->form));
23676 break;
23677 }
23678
23679 return result;
23680 }
23681
23682 /* See read.h. */
23683
23684 struct type *
23685 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23686 dwarf2_per_cu_data *per_cu,
23687 dwarf2_per_objfile *per_objfile,
23688 const char **var_name)
23689 {
23690 struct die_info *die;
23691
23692 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23693 if (cu == nullptr)
23694 cu = load_cu (per_cu, per_objfile, false);
23695
23696 if (cu == nullptr)
23697 return nullptr;
23698
23699 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23700 if (!die)
23701 return NULL;
23702
23703 if (var_name != nullptr)
23704 *var_name = var_decl_name (die, cu);
23705 return die_type (die, cu);
23706 }
23707
23708 /* See read.h. */
23709
23710 struct type *
23711 dwarf2_get_die_type (cu_offset die_offset,
23712 dwarf2_per_cu_data *per_cu,
23713 dwarf2_per_objfile *per_objfile)
23714 {
23715 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23716 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23717 }
23718
23719 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23720 On entry *REF_CU is the CU of SRC_DIE.
23721 On exit *REF_CU is the CU of the result.
23722 Returns NULL if the referenced DIE isn't found. */
23723
23724 static struct die_info *
23725 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23726 struct dwarf2_cu **ref_cu)
23727 {
23728 struct die_info temp_die;
23729 struct dwarf2_cu *sig_cu;
23730 struct die_info *die;
23731 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23732
23733
23734 /* While it might be nice to assert sig_type->type == NULL here,
23735 we can get here for DW_AT_imported_declaration where we need
23736 the DIE not the type. */
23737
23738 /* If necessary, add it to the queue and load its DIEs.
23739
23740 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23741 it doesn't mean they are currently loaded. Since we require them
23742 to be loaded, we must check for ourselves. */
23743 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23744 language_minimal)
23745 || per_objfile->get_cu (sig_type) == nullptr)
23746 read_signatured_type (sig_type, per_objfile);
23747
23748 sig_cu = per_objfile->get_cu (sig_type);
23749 gdb_assert (sig_cu != NULL);
23750 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23751 temp_die.sect_off = sig_type->type_offset_in_section;
23752 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23753 to_underlying (temp_die.sect_off));
23754 if (die)
23755 {
23756 /* For .gdb_index version 7 keep track of included TUs.
23757 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23758 if (per_objfile->per_bfd->index_table != NULL
23759 && per_objfile->per_bfd->index_table->version <= 7)
23760 {
23761 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23762 }
23763
23764 *ref_cu = sig_cu;
23765 return die;
23766 }
23767
23768 return NULL;
23769 }
23770
23771 /* Follow signatured type referenced by ATTR in SRC_DIE.
23772 On entry *REF_CU is the CU of SRC_DIE.
23773 On exit *REF_CU is the CU of the result.
23774 The result is the DIE of the type.
23775 If the referenced type cannot be found an error is thrown. */
23776
23777 static struct die_info *
23778 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23779 struct dwarf2_cu **ref_cu)
23780 {
23781 ULONGEST signature = attr->as_signature ();
23782 struct signatured_type *sig_type;
23783 struct die_info *die;
23784
23785 gdb_assert (attr->form == DW_FORM_ref_sig8);
23786
23787 sig_type = lookup_signatured_type (*ref_cu, signature);
23788 /* sig_type will be NULL if the signatured type is missing from
23789 the debug info. */
23790 if (sig_type == NULL)
23791 {
23792 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23793 " from DIE at %s [in module %s]"),
23794 hex_string (signature), sect_offset_str (src_die->sect_off),
23795 objfile_name ((*ref_cu)->per_objfile->objfile));
23796 }
23797
23798 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23799 if (die == NULL)
23800 {
23801 dump_die_for_error (src_die);
23802 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23803 " from DIE at %s [in module %s]"),
23804 hex_string (signature), sect_offset_str (src_die->sect_off),
23805 objfile_name ((*ref_cu)->per_objfile->objfile));
23806 }
23807
23808 return die;
23809 }
23810
23811 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23812 reading in and processing the type unit if necessary. */
23813
23814 static struct type *
23815 get_signatured_type (struct die_info *die, ULONGEST signature,
23816 struct dwarf2_cu *cu)
23817 {
23818 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23819 struct signatured_type *sig_type;
23820 struct dwarf2_cu *type_cu;
23821 struct die_info *type_die;
23822 struct type *type;
23823
23824 sig_type = lookup_signatured_type (cu, signature);
23825 /* sig_type will be NULL if the signatured type is missing from
23826 the debug info. */
23827 if (sig_type == NULL)
23828 {
23829 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23830 " from DIE at %s [in module %s]"),
23831 hex_string (signature), sect_offset_str (die->sect_off),
23832 objfile_name (per_objfile->objfile));
23833 return build_error_marker_type (cu, die);
23834 }
23835
23836 /* If we already know the type we're done. */
23837 type = per_objfile->get_type_for_signatured_type (sig_type);
23838 if (type != nullptr)
23839 return type;
23840
23841 type_cu = cu;
23842 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23843 if (type_die != NULL)
23844 {
23845 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23846 is created. This is important, for example, because for c++ classes
23847 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23848 type = read_type_die (type_die, type_cu);
23849 if (type == NULL)
23850 {
23851 complaint (_("Dwarf Error: Cannot build signatured type %s"
23852 " referenced from DIE at %s [in module %s]"),
23853 hex_string (signature), sect_offset_str (die->sect_off),
23854 objfile_name (per_objfile->objfile));
23855 type = build_error_marker_type (cu, die);
23856 }
23857 }
23858 else
23859 {
23860 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23861 " from DIE at %s [in module %s]"),
23862 hex_string (signature), sect_offset_str (die->sect_off),
23863 objfile_name (per_objfile->objfile));
23864 type = build_error_marker_type (cu, die);
23865 }
23866
23867 per_objfile->set_type_for_signatured_type (sig_type, type);
23868
23869 return type;
23870 }
23871
23872 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23873 reading in and processing the type unit if necessary. */
23874
23875 static struct type *
23876 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23877 struct dwarf2_cu *cu) /* ARI: editCase function */
23878 {
23879 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23880 if (attr->form_is_ref ())
23881 {
23882 struct dwarf2_cu *type_cu = cu;
23883 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23884
23885 return read_type_die (type_die, type_cu);
23886 }
23887 else if (attr->form == DW_FORM_ref_sig8)
23888 {
23889 return get_signatured_type (die, attr->as_signature (), cu);
23890 }
23891 else
23892 {
23893 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23894
23895 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23896 " at %s [in module %s]"),
23897 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23898 objfile_name (per_objfile->objfile));
23899 return build_error_marker_type (cu, die);
23900 }
23901 }
23902
23903 /* Load the DIEs associated with type unit PER_CU into memory. */
23904
23905 static void
23906 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23907 dwarf2_per_objfile *per_objfile)
23908 {
23909 struct signatured_type *sig_type;
23910
23911 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23912 gdb_assert (! per_cu->type_unit_group_p ());
23913
23914 /* We have the per_cu, but we need the signatured_type.
23915 Fortunately this is an easy translation. */
23916 gdb_assert (per_cu->is_debug_types);
23917 sig_type = (struct signatured_type *) per_cu;
23918
23919 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23920
23921 read_signatured_type (sig_type, per_objfile);
23922
23923 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23924 }
23925
23926 /* Read in a signatured type and build its CU and DIEs.
23927 If the type is a stub for the real type in a DWO file,
23928 read in the real type from the DWO file as well. */
23929
23930 static void
23931 read_signatured_type (signatured_type *sig_type,
23932 dwarf2_per_objfile *per_objfile)
23933 {
23934 gdb_assert (sig_type->is_debug_types);
23935 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23936
23937 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23938
23939 if (!reader.dummy_p)
23940 {
23941 struct dwarf2_cu *cu = reader.cu;
23942 const gdb_byte *info_ptr = reader.info_ptr;
23943
23944 gdb_assert (cu->die_hash == NULL);
23945 cu->die_hash =
23946 htab_create_alloc_ex (cu->header.length / 12,
23947 die_hash,
23948 die_eq,
23949 NULL,
23950 &cu->comp_unit_obstack,
23951 hashtab_obstack_allocate,
23952 dummy_obstack_deallocate);
23953
23954 if (reader.comp_unit_die->has_children)
23955 reader.comp_unit_die->child
23956 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23957 reader.comp_unit_die);
23958 cu->dies = reader.comp_unit_die;
23959 /* comp_unit_die is not stored in die_hash, no need. */
23960
23961 /* We try not to read any attributes in this function, because
23962 not all CUs needed for references have been loaded yet, and
23963 symbol table processing isn't initialized. But we have to
23964 set the CU language, or we won't be able to build types
23965 correctly. Similarly, if we do not read the producer, we can
23966 not apply producer-specific interpretation. */
23967 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23968
23969 reader.keep ();
23970 }
23971
23972 sig_type->tu_read = 1;
23973 }
23974
23975 /* Decode simple location descriptions.
23976 Given a pointer to a dwarf block that defines a location, compute
23977 the location and return the value. If COMPUTED is non-null, it is
23978 set to true to indicate that decoding was successful, and false
23979 otherwise. If COMPUTED is null, then this function may emit a
23980 complaint. */
23981
23982 static CORE_ADDR
23983 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23984 {
23985 struct objfile *objfile = cu->per_objfile->objfile;
23986 size_t i;
23987 size_t size = blk->size;
23988 const gdb_byte *data = blk->data;
23989 CORE_ADDR stack[64];
23990 int stacki;
23991 unsigned int bytes_read, unsnd;
23992 gdb_byte op;
23993
23994 if (computed != nullptr)
23995 *computed = false;
23996
23997 i = 0;
23998 stacki = 0;
23999 stack[stacki] = 0;
24000 stack[++stacki] = 0;
24001
24002 while (i < size)
24003 {
24004 op = data[i++];
24005 switch (op)
24006 {
24007 case DW_OP_lit0:
24008 case DW_OP_lit1:
24009 case DW_OP_lit2:
24010 case DW_OP_lit3:
24011 case DW_OP_lit4:
24012 case DW_OP_lit5:
24013 case DW_OP_lit6:
24014 case DW_OP_lit7:
24015 case DW_OP_lit8:
24016 case DW_OP_lit9:
24017 case DW_OP_lit10:
24018 case DW_OP_lit11:
24019 case DW_OP_lit12:
24020 case DW_OP_lit13:
24021 case DW_OP_lit14:
24022 case DW_OP_lit15:
24023 case DW_OP_lit16:
24024 case DW_OP_lit17:
24025 case DW_OP_lit18:
24026 case DW_OP_lit19:
24027 case DW_OP_lit20:
24028 case DW_OP_lit21:
24029 case DW_OP_lit22:
24030 case DW_OP_lit23:
24031 case DW_OP_lit24:
24032 case DW_OP_lit25:
24033 case DW_OP_lit26:
24034 case DW_OP_lit27:
24035 case DW_OP_lit28:
24036 case DW_OP_lit29:
24037 case DW_OP_lit30:
24038 case DW_OP_lit31:
24039 stack[++stacki] = op - DW_OP_lit0;
24040 break;
24041
24042 case DW_OP_reg0:
24043 case DW_OP_reg1:
24044 case DW_OP_reg2:
24045 case DW_OP_reg3:
24046 case DW_OP_reg4:
24047 case DW_OP_reg5:
24048 case DW_OP_reg6:
24049 case DW_OP_reg7:
24050 case DW_OP_reg8:
24051 case DW_OP_reg9:
24052 case DW_OP_reg10:
24053 case DW_OP_reg11:
24054 case DW_OP_reg12:
24055 case DW_OP_reg13:
24056 case DW_OP_reg14:
24057 case DW_OP_reg15:
24058 case DW_OP_reg16:
24059 case DW_OP_reg17:
24060 case DW_OP_reg18:
24061 case DW_OP_reg19:
24062 case DW_OP_reg20:
24063 case DW_OP_reg21:
24064 case DW_OP_reg22:
24065 case DW_OP_reg23:
24066 case DW_OP_reg24:
24067 case DW_OP_reg25:
24068 case DW_OP_reg26:
24069 case DW_OP_reg27:
24070 case DW_OP_reg28:
24071 case DW_OP_reg29:
24072 case DW_OP_reg30:
24073 case DW_OP_reg31:
24074 stack[++stacki] = op - DW_OP_reg0;
24075 if (i < size)
24076 {
24077 if (computed == nullptr)
24078 dwarf2_complex_location_expr_complaint ();
24079 else
24080 return 0;
24081 }
24082 break;
24083
24084 case DW_OP_regx:
24085 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24086 i += bytes_read;
24087 stack[++stacki] = unsnd;
24088 if (i < size)
24089 {
24090 if (computed == nullptr)
24091 dwarf2_complex_location_expr_complaint ();
24092 else
24093 return 0;
24094 }
24095 break;
24096
24097 case DW_OP_addr:
24098 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24099 &bytes_read);
24100 i += bytes_read;
24101 break;
24102
24103 case DW_OP_const1u:
24104 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24105 i += 1;
24106 break;
24107
24108 case DW_OP_const1s:
24109 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24110 i += 1;
24111 break;
24112
24113 case DW_OP_const2u:
24114 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24115 i += 2;
24116 break;
24117
24118 case DW_OP_const2s:
24119 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24120 i += 2;
24121 break;
24122
24123 case DW_OP_const4u:
24124 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24125 i += 4;
24126 break;
24127
24128 case DW_OP_const4s:
24129 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24130 i += 4;
24131 break;
24132
24133 case DW_OP_const8u:
24134 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24135 i += 8;
24136 break;
24137
24138 case DW_OP_constu:
24139 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24140 &bytes_read);
24141 i += bytes_read;
24142 break;
24143
24144 case DW_OP_consts:
24145 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24146 i += bytes_read;
24147 break;
24148
24149 case DW_OP_dup:
24150 stack[stacki + 1] = stack[stacki];
24151 stacki++;
24152 break;
24153
24154 case DW_OP_plus:
24155 stack[stacki - 1] += stack[stacki];
24156 stacki--;
24157 break;
24158
24159 case DW_OP_plus_uconst:
24160 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24161 &bytes_read);
24162 i += bytes_read;
24163 break;
24164
24165 case DW_OP_minus:
24166 stack[stacki - 1] -= stack[stacki];
24167 stacki--;
24168 break;
24169
24170 case DW_OP_deref:
24171 /* If we're not the last op, then we definitely can't encode
24172 this using GDB's address_class enum. This is valid for partial
24173 global symbols, although the variable's address will be bogus
24174 in the psymtab. */
24175 if (i < size)
24176 {
24177 if (computed == nullptr)
24178 dwarf2_complex_location_expr_complaint ();
24179 else
24180 return 0;
24181 }
24182 break;
24183
24184 case DW_OP_GNU_push_tls_address:
24185 case DW_OP_form_tls_address:
24186 /* The top of the stack has the offset from the beginning
24187 of the thread control block at which the variable is located. */
24188 /* Nothing should follow this operator, so the top of stack would
24189 be returned. */
24190 /* This is valid for partial global symbols, but the variable's
24191 address will be bogus in the psymtab. Make it always at least
24192 non-zero to not look as a variable garbage collected by linker
24193 which have DW_OP_addr 0. */
24194 if (i < size)
24195 {
24196 if (computed == nullptr)
24197 dwarf2_complex_location_expr_complaint ();
24198 else
24199 return 0;
24200 }
24201 stack[stacki]++;
24202 break;
24203
24204 case DW_OP_GNU_uninit:
24205 if (computed != nullptr)
24206 return 0;
24207 break;
24208
24209 case DW_OP_addrx:
24210 case DW_OP_GNU_addr_index:
24211 case DW_OP_GNU_const_index:
24212 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24213 &bytes_read);
24214 i += bytes_read;
24215 break;
24216
24217 default:
24218 if (computed == nullptr)
24219 {
24220 const char *name = get_DW_OP_name (op);
24221
24222 if (name)
24223 complaint (_("unsupported stack op: '%s'"),
24224 name);
24225 else
24226 complaint (_("unsupported stack op: '%02x'"),
24227 op);
24228 }
24229
24230 return (stack[stacki]);
24231 }
24232
24233 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24234 outside of the allocated space. Also enforce minimum>0. */
24235 if (stacki >= ARRAY_SIZE (stack) - 1)
24236 {
24237 if (computed == nullptr)
24238 complaint (_("location description stack overflow"));
24239 return 0;
24240 }
24241
24242 if (stacki <= 0)
24243 {
24244 if (computed == nullptr)
24245 complaint (_("location description stack underflow"));
24246 return 0;
24247 }
24248 }
24249
24250 if (computed != nullptr)
24251 *computed = true;
24252 return (stack[stacki]);
24253 }
24254
24255 /* memory allocation interface */
24256
24257 static struct dwarf_block *
24258 dwarf_alloc_block (struct dwarf2_cu *cu)
24259 {
24260 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24261 }
24262
24263 static struct die_info *
24264 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24265 {
24266 struct die_info *die;
24267 size_t size = sizeof (struct die_info);
24268
24269 if (num_attrs > 1)
24270 size += (num_attrs - 1) * sizeof (struct attribute);
24271
24272 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24273 memset (die, 0, sizeof (struct die_info));
24274 return (die);
24275 }
24276
24277 \f
24278
24279 /* Macro support. */
24280
24281 /* An overload of dwarf_decode_macros that finds the correct section
24282 and ensures it is read in before calling the other overload. */
24283
24284 static void
24285 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24286 int section_is_gnu)
24287 {
24288 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24289 struct objfile *objfile = per_objfile->objfile;
24290 const struct line_header *lh = cu->line_header;
24291 unsigned int offset_size = cu->header.offset_size;
24292 struct dwarf2_section_info *section;
24293 const char *section_name;
24294
24295 if (cu->dwo_unit != nullptr)
24296 {
24297 if (section_is_gnu)
24298 {
24299 section = &cu->dwo_unit->dwo_file->sections.macro;
24300 section_name = ".debug_macro.dwo";
24301 }
24302 else
24303 {
24304 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24305 section_name = ".debug_macinfo.dwo";
24306 }
24307 }
24308 else
24309 {
24310 if (section_is_gnu)
24311 {
24312 section = &per_objfile->per_bfd->macro;
24313 section_name = ".debug_macro";
24314 }
24315 else
24316 {
24317 section = &per_objfile->per_bfd->macinfo;
24318 section_name = ".debug_macinfo";
24319 }
24320 }
24321
24322 section->read (objfile);
24323 if (section->buffer == nullptr)
24324 {
24325 complaint (_("missing %s section"), section_name);
24326 return;
24327 }
24328
24329 buildsym_compunit *builder = cu->get_builder ();
24330
24331 struct dwarf2_section_info *str_offsets_section;
24332 struct dwarf2_section_info *str_section;
24333 gdb::optional<ULONGEST> str_offsets_base;
24334
24335 if (cu->dwo_unit != nullptr)
24336 {
24337 str_offsets_section = &cu->dwo_unit->dwo_file
24338 ->sections.str_offsets;
24339 str_section = &cu->dwo_unit->dwo_file->sections.str;
24340 str_offsets_base = cu->header.addr_size;
24341 }
24342 else
24343 {
24344 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24345 str_section = &per_objfile->per_bfd->str;
24346 str_offsets_base = cu->str_offsets_base;
24347 }
24348
24349 dwarf_decode_macros (per_objfile, builder, section, lh,
24350 offset_size, offset, str_section, str_offsets_section,
24351 str_offsets_base, section_is_gnu);
24352 }
24353
24354 /* Return the .debug_loc section to use for CU.
24355 For DWO files use .debug_loc.dwo. */
24356
24357 static struct dwarf2_section_info *
24358 cu_debug_loc_section (struct dwarf2_cu *cu)
24359 {
24360 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24361
24362 if (cu->dwo_unit)
24363 {
24364 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24365
24366 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24367 }
24368 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24369 : &per_objfile->per_bfd->loc);
24370 }
24371
24372 /* Return the .debug_rnglists section to use for CU. */
24373 static struct dwarf2_section_info *
24374 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24375 {
24376 if (cu->header.version < 5)
24377 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24378 cu->header.version);
24379 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24380
24381 /* Make sure we read the .debug_rnglists section from the file that
24382 contains the DW_AT_ranges attribute we are reading. Normally that
24383 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24384 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24385 program. */
24386 if (cu->dwo_unit != nullptr
24387 && tag != DW_TAG_compile_unit
24388 && tag != DW_TAG_skeleton_unit)
24389 {
24390 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24391
24392 if (sections->rnglists.size > 0)
24393 return &sections->rnglists;
24394 else
24395 error (_(".debug_rnglists section is missing from .dwo file."));
24396 }
24397 return &dwarf2_per_objfile->per_bfd->rnglists;
24398 }
24399
24400 /* A helper function that fills in a dwarf2_loclist_baton. */
24401
24402 static void
24403 fill_in_loclist_baton (struct dwarf2_cu *cu,
24404 struct dwarf2_loclist_baton *baton,
24405 const struct attribute *attr)
24406 {
24407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24408 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24409
24410 section->read (per_objfile->objfile);
24411
24412 baton->per_objfile = per_objfile;
24413 baton->per_cu = cu->per_cu;
24414 gdb_assert (baton->per_cu);
24415 /* We don't know how long the location list is, but make sure we
24416 don't run off the edge of the section. */
24417 baton->size = section->size - attr->as_unsigned ();
24418 baton->data = section->buffer + attr->as_unsigned ();
24419 if (cu->base_address.has_value ())
24420 baton->base_address = *cu->base_address;
24421 else
24422 baton->base_address = 0;
24423 baton->from_dwo = cu->dwo_unit != NULL;
24424 }
24425
24426 static void
24427 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24428 struct dwarf2_cu *cu, int is_block)
24429 {
24430 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24431 struct objfile *objfile = per_objfile->objfile;
24432 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24433
24434 if (attr->form_is_section_offset ()
24435 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24436 the section. If so, fall through to the complaint in the
24437 other branch. */
24438 && attr->as_unsigned () < section->get_size (objfile))
24439 {
24440 struct dwarf2_loclist_baton *baton;
24441
24442 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24443
24444 fill_in_loclist_baton (cu, baton, attr);
24445
24446 if (!cu->base_address.has_value ())
24447 complaint (_("Location list used without "
24448 "specifying the CU base address."));
24449
24450 sym->set_aclass_index ((is_block
24451 ? dwarf2_loclist_block_index
24452 : dwarf2_loclist_index));
24453 SYMBOL_LOCATION_BATON (sym) = baton;
24454 }
24455 else
24456 {
24457 struct dwarf2_locexpr_baton *baton;
24458
24459 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24460 baton->per_objfile = per_objfile;
24461 baton->per_cu = cu->per_cu;
24462 gdb_assert (baton->per_cu);
24463
24464 if (attr->form_is_block ())
24465 {
24466 /* Note that we're just copying the block's data pointer
24467 here, not the actual data. We're still pointing into the
24468 info_buffer for SYM's objfile; right now we never release
24469 that buffer, but when we do clean up properly this may
24470 need to change. */
24471 struct dwarf_block *block = attr->as_block ();
24472 baton->size = block->size;
24473 baton->data = block->data;
24474 }
24475 else
24476 {
24477 dwarf2_invalid_attrib_class_complaint ("location description",
24478 sym->natural_name ());
24479 baton->size = 0;
24480 }
24481
24482 sym->set_aclass_index ((is_block
24483 ? dwarf2_locexpr_block_index
24484 : dwarf2_locexpr_index));
24485 SYMBOL_LOCATION_BATON (sym) = baton;
24486 }
24487 }
24488
24489 /* See read.h. */
24490
24491 const comp_unit_head *
24492 dwarf2_per_cu_data::get_header () const
24493 {
24494 if (!m_header_read_in)
24495 {
24496 const gdb_byte *info_ptr
24497 = this->section->buffer + to_underlying (this->sect_off);
24498
24499 memset (&m_header, 0, sizeof (m_header));
24500
24501 read_comp_unit_head (&m_header, info_ptr, this->section,
24502 rcuh_kind::COMPILE);
24503
24504 m_header_read_in = true;
24505 }
24506
24507 return &m_header;
24508 }
24509
24510 /* See read.h. */
24511
24512 int
24513 dwarf2_per_cu_data::addr_size () const
24514 {
24515 return this->get_header ()->addr_size;
24516 }
24517
24518 /* See read.h. */
24519
24520 int
24521 dwarf2_per_cu_data::offset_size () const
24522 {
24523 return this->get_header ()->offset_size;
24524 }
24525
24526 /* See read.h. */
24527
24528 int
24529 dwarf2_per_cu_data::ref_addr_size () const
24530 {
24531 const comp_unit_head *header = this->get_header ();
24532
24533 if (header->version == 2)
24534 return header->addr_size;
24535 else
24536 return header->offset_size;
24537 }
24538
24539 /* A helper function for dwarf2_find_containing_comp_unit that returns
24540 the index of the result, and that searches a vector. It will
24541 return a result even if the offset in question does not actually
24542 occur in any CU. This is separate so that it can be unit
24543 tested. */
24544
24545 static int
24546 dwarf2_find_containing_comp_unit
24547 (sect_offset sect_off,
24548 unsigned int offset_in_dwz,
24549 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24550 {
24551 int low, high;
24552
24553 low = 0;
24554 high = all_comp_units.size () - 1;
24555 while (high > low)
24556 {
24557 struct dwarf2_per_cu_data *mid_cu;
24558 int mid = low + (high - low) / 2;
24559
24560 mid_cu = all_comp_units[mid].get ();
24561 if (mid_cu->is_dwz > offset_in_dwz
24562 || (mid_cu->is_dwz == offset_in_dwz
24563 && mid_cu->sect_off + mid_cu->length > sect_off))
24564 high = mid;
24565 else
24566 low = mid + 1;
24567 }
24568 gdb_assert (low == high);
24569 return low;
24570 }
24571
24572 /* Locate the .debug_info compilation unit from CU's objfile which contains
24573 the DIE at OFFSET. Raises an error on failure. */
24574
24575 static struct dwarf2_per_cu_data *
24576 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24577 unsigned int offset_in_dwz,
24578 dwarf2_per_bfd *per_bfd)
24579 {
24580 int low = dwarf2_find_containing_comp_unit
24581 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
24582 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
24583
24584 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24585 {
24586 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24587 error (_("Dwarf Error: could not find partial DIE containing "
24588 "offset %s [in module %s]"),
24589 sect_offset_str (sect_off),
24590 bfd_get_filename (per_bfd->obfd));
24591
24592 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
24593 <= sect_off);
24594 return per_bfd->all_comp_units[low - 1].get ();
24595 }
24596 else
24597 {
24598 if (low == per_bfd->all_comp_units.size () - 1
24599 && sect_off >= this_cu->sect_off + this_cu->length)
24600 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24601 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24602 return this_cu;
24603 }
24604 }
24605
24606 #if GDB_SELF_TEST
24607
24608 namespace selftests {
24609 namespace find_containing_comp_unit {
24610
24611 static void
24612 run_test ()
24613 {
24614 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24615 dwarf2_per_cu_data *one_ptr = one.get ();
24616 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24617 dwarf2_per_cu_data *two_ptr = two.get ();
24618 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24619 dwarf2_per_cu_data *three_ptr = three.get ();
24620 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24621 dwarf2_per_cu_data *four_ptr = four.get ();
24622
24623 one->length = 5;
24624 two->sect_off = sect_offset (one->length);
24625 two->length = 7;
24626
24627 three->length = 5;
24628 three->is_dwz = 1;
24629 four->sect_off = sect_offset (three->length);
24630 four->length = 7;
24631 four->is_dwz = 1;
24632
24633 std::vector<dwarf2_per_cu_data_up> units;
24634 units.push_back (std::move (one));
24635 units.push_back (std::move (two));
24636 units.push_back (std::move (three));
24637 units.push_back (std::move (four));
24638
24639 int result;
24640
24641 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24642 SELF_CHECK (units[result].get () == one_ptr);
24643 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24644 SELF_CHECK (units[result].get () == one_ptr);
24645 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24646 SELF_CHECK (units[result].get () == two_ptr);
24647
24648 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24649 SELF_CHECK (units[result].get () == three_ptr);
24650 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24651 SELF_CHECK (units[result].get () == three_ptr);
24652 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24653 SELF_CHECK (units[result].get () == four_ptr);
24654 }
24655
24656 }
24657 }
24658
24659 #endif /* GDB_SELF_TEST */
24660
24661 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24662
24663 static void
24664 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24665 enum language pretend_language)
24666 {
24667 struct attribute *attr;
24668
24669 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24670
24671 /* Set the language we're debugging. */
24672 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24673 if (cu->producer != nullptr
24674 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
24675 {
24676 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24677 attribute is not standardised yet. As a workaround for the
24678 language detection we fall back to the DW_AT_producer
24679 string. */
24680 cu->per_cu->lang = language_opencl;
24681 }
24682 else if (cu->producer != nullptr
24683 && strstr (cu->producer, "GNU Go ") != NULL)
24684 {
24685 /* Similar hack for Go. */
24686 cu->per_cu->lang = language_go;
24687 }
24688 else if (attr != nullptr)
24689 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
24690 else
24691 cu->per_cu->lang = pretend_language;
24692 cu->language_defn = language_def (cu->per_cu->lang);
24693 }
24694
24695 /* See read.h. */
24696
24697 dwarf2_cu *
24698 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24699 {
24700 auto it = m_dwarf2_cus.find (per_cu);
24701 if (it == m_dwarf2_cus.end ())
24702 return nullptr;
24703
24704 return it->second;
24705 }
24706
24707 /* See read.h. */
24708
24709 void
24710 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24711 {
24712 gdb_assert (this->get_cu (per_cu) == nullptr);
24713
24714 m_dwarf2_cus[per_cu] = cu;
24715 }
24716
24717 /* See read.h. */
24718
24719 void
24720 dwarf2_per_objfile::age_comp_units ()
24721 {
24722 dwarf_read_debug_printf_v ("running");
24723
24724 /* This is not expected to be called in the middle of CU expansion. There is
24725 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24726 loaded in memory. Calling age_comp_units while the queue is in use could
24727 make us free the DIEs for a CU that is in the queue and therefore break
24728 that invariant. */
24729 gdb_assert (!this->per_bfd->queue.has_value ());
24730
24731 /* Start by clearing all marks. */
24732 for (auto pair : m_dwarf2_cus)
24733 pair.second->clear_mark ();
24734
24735 /* Traverse all CUs, mark them and their dependencies if used recently
24736 enough. */
24737 for (auto pair : m_dwarf2_cus)
24738 {
24739 dwarf2_cu *cu = pair.second;
24740
24741 cu->last_used++;
24742 if (cu->last_used <= dwarf_max_cache_age)
24743 cu->mark ();
24744 }
24745
24746 /* Delete all CUs still not marked. */
24747 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24748 {
24749 dwarf2_cu *cu = it->second;
24750
24751 if (!cu->is_marked ())
24752 {
24753 dwarf_read_debug_printf_v ("deleting old CU %s",
24754 sect_offset_str (cu->per_cu->sect_off));
24755 delete cu;
24756 it = m_dwarf2_cus.erase (it);
24757 }
24758 else
24759 it++;
24760 }
24761 }
24762
24763 /* See read.h. */
24764
24765 void
24766 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24767 {
24768 auto it = m_dwarf2_cus.find (per_cu);
24769 if (it == m_dwarf2_cus.end ())
24770 return;
24771
24772 delete it->second;
24773
24774 m_dwarf2_cus.erase (it);
24775 }
24776
24777 dwarf2_per_objfile::~dwarf2_per_objfile ()
24778 {
24779 remove_all_cus ();
24780 }
24781
24782 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24783 We store these in a hash table separate from the DIEs, and preserve them
24784 when the DIEs are flushed out of cache.
24785
24786 The CU "per_cu" pointer is needed because offset alone is not enough to
24787 uniquely identify the type. A file may have multiple .debug_types sections,
24788 or the type may come from a DWO file. Furthermore, while it's more logical
24789 to use per_cu->section+offset, with Fission the section with the data is in
24790 the DWO file but we don't know that section at the point we need it.
24791 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24792 because we can enter the lookup routine, get_die_type_at_offset, from
24793 outside this file, and thus won't necessarily have PER_CU->cu.
24794 Fortunately, PER_CU is stable for the life of the objfile. */
24795
24796 struct dwarf2_per_cu_offset_and_type
24797 {
24798 const struct dwarf2_per_cu_data *per_cu;
24799 sect_offset sect_off;
24800 struct type *type;
24801 };
24802
24803 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24804
24805 static hashval_t
24806 per_cu_offset_and_type_hash (const void *item)
24807 {
24808 const struct dwarf2_per_cu_offset_and_type *ofs
24809 = (const struct dwarf2_per_cu_offset_and_type *) item;
24810
24811 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24812 }
24813
24814 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24815
24816 static int
24817 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24818 {
24819 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24820 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24821 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24822 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24823
24824 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24825 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24826 }
24827
24828 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24829 table if necessary. For convenience, return TYPE.
24830
24831 The DIEs reading must have careful ordering to:
24832 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24833 reading current DIE.
24834 * Not trying to dereference contents of still incompletely read in types
24835 while reading in other DIEs.
24836 * Enable referencing still incompletely read in types just by a pointer to
24837 the type without accessing its fields.
24838
24839 Therefore caller should follow these rules:
24840 * Try to fetch any prerequisite types we may need to build this DIE type
24841 before building the type and calling set_die_type.
24842 * After building type call set_die_type for current DIE as soon as
24843 possible before fetching more types to complete the current type.
24844 * Make the type as complete as possible before fetching more types. */
24845
24846 static struct type *
24847 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24848 bool skip_data_location)
24849 {
24850 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24851 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24852 struct objfile *objfile = per_objfile->objfile;
24853 struct attribute *attr;
24854 struct dynamic_prop prop;
24855
24856 /* For Ada types, make sure that the gnat-specific data is always
24857 initialized (if not already set). There are a few types where
24858 we should not be doing so, because the type-specific area is
24859 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24860 where the type-specific area is used to store the floatformat).
24861 But this is not a problem, because the gnat-specific information
24862 is actually not needed for these types. */
24863 if (need_gnat_info (cu)
24864 && type->code () != TYPE_CODE_FUNC
24865 && type->code () != TYPE_CODE_FLT
24866 && type->code () != TYPE_CODE_METHODPTR
24867 && type->code () != TYPE_CODE_MEMBERPTR
24868 && type->code () != TYPE_CODE_METHOD
24869 && type->code () != TYPE_CODE_FIXED_POINT
24870 && !HAVE_GNAT_AUX_INFO (type))
24871 INIT_GNAT_SPECIFIC (type);
24872
24873 /* Read DW_AT_allocated and set in type. */
24874 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24875 if (attr != NULL)
24876 {
24877 struct type *prop_type = cu->addr_sized_int_type (false);
24878 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24879 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24880 }
24881
24882 /* Read DW_AT_associated and set in type. */
24883 attr = dwarf2_attr (die, DW_AT_associated, cu);
24884 if (attr != NULL)
24885 {
24886 struct type *prop_type = cu->addr_sized_int_type (false);
24887 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24888 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24889 }
24890
24891 /* Read DW_AT_rank and set in type. */
24892 attr = dwarf2_attr (die, DW_AT_rank, cu);
24893 if (attr != NULL)
24894 {
24895 struct type *prop_type = cu->addr_sized_int_type (false);
24896 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24897 type->add_dyn_prop (DYN_PROP_RANK, prop);
24898 }
24899
24900 /* Read DW_AT_data_location and set in type. */
24901 if (!skip_data_location)
24902 {
24903 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24904 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24905 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24906 }
24907
24908 if (per_objfile->die_type_hash == NULL)
24909 per_objfile->die_type_hash
24910 = htab_up (htab_create_alloc (127,
24911 per_cu_offset_and_type_hash,
24912 per_cu_offset_and_type_eq,
24913 NULL, xcalloc, xfree));
24914
24915 ofs.per_cu = cu->per_cu;
24916 ofs.sect_off = die->sect_off;
24917 ofs.type = type;
24918 slot = (struct dwarf2_per_cu_offset_and_type **)
24919 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24920 if (*slot)
24921 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24922 sect_offset_str (die->sect_off));
24923 *slot = XOBNEW (&objfile->objfile_obstack,
24924 struct dwarf2_per_cu_offset_and_type);
24925 **slot = ofs;
24926 return type;
24927 }
24928
24929 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24930 or return NULL if the die does not have a saved type. */
24931
24932 static struct type *
24933 get_die_type_at_offset (sect_offset sect_off,
24934 dwarf2_per_cu_data *per_cu,
24935 dwarf2_per_objfile *per_objfile)
24936 {
24937 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24938
24939 if (per_objfile->die_type_hash == NULL)
24940 return NULL;
24941
24942 ofs.per_cu = per_cu;
24943 ofs.sect_off = sect_off;
24944 slot = ((struct dwarf2_per_cu_offset_and_type *)
24945 htab_find (per_objfile->die_type_hash.get (), &ofs));
24946 if (slot)
24947 return slot->type;
24948 else
24949 return NULL;
24950 }
24951
24952 /* Look up the type for DIE in CU in die_type_hash,
24953 or return NULL if DIE does not have a saved type. */
24954
24955 static struct type *
24956 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24957 {
24958 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24959 }
24960
24961 /* Trivial hash function for partial_die_info: the hash value of a DIE
24962 is its offset in .debug_info for this objfile. */
24963
24964 static hashval_t
24965 partial_die_hash (const void *item)
24966 {
24967 const struct partial_die_info *part_die
24968 = (const struct partial_die_info *) item;
24969
24970 return to_underlying (part_die->sect_off);
24971 }
24972
24973 /* Trivial comparison function for partial_die_info structures: two DIEs
24974 are equal if they have the same offset. */
24975
24976 static int
24977 partial_die_eq (const void *item_lhs, const void *item_rhs)
24978 {
24979 const struct partial_die_info *part_die_lhs
24980 = (const struct partial_die_info *) item_lhs;
24981 const struct partial_die_info *part_die_rhs
24982 = (const struct partial_die_info *) item_rhs;
24983
24984 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24985 }
24986
24987 struct cmd_list_element *set_dwarf_cmdlist;
24988 struct cmd_list_element *show_dwarf_cmdlist;
24989
24990 static void
24991 show_check_physname (struct ui_file *file, int from_tty,
24992 struct cmd_list_element *c, const char *value)
24993 {
24994 gdb_printf (file,
24995 _("Whether to check \"physname\" is %s.\n"),
24996 value);
24997 }
24998
24999 void _initialize_dwarf2_read ();
25000 void
25001 _initialize_dwarf2_read ()
25002 {
25003 add_setshow_prefix_cmd ("dwarf", class_maintenance,
25004 _("\
25005 Set DWARF specific variables.\n\
25006 Configure DWARF variables such as the cache size."),
25007 _("\
25008 Show DWARF specific variables.\n\
25009 Show DWARF variables such as the cache size."),
25010 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
25011 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
25012
25013 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25014 &dwarf_max_cache_age, _("\
25015 Set the upper bound on the age of cached DWARF compilation units."), _("\
25016 Show the upper bound on the age of cached DWARF compilation units."), _("\
25017 A higher limit means that cached compilation units will be stored\n\
25018 in memory longer, and more total memory will be used. Zero disables\n\
25019 caching, which can slow down startup."),
25020 NULL,
25021 show_dwarf_max_cache_age,
25022 &set_dwarf_cmdlist,
25023 &show_dwarf_cmdlist);
25024
25025 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25026 Set debugging of the DWARF reader."), _("\
25027 Show debugging of the DWARF reader."), _("\
25028 When enabled (non-zero), debugging messages are printed during DWARF\n\
25029 reading and symtab expansion. A value of 1 (one) provides basic\n\
25030 information. A value greater than 1 provides more verbose information."),
25031 NULL,
25032 NULL,
25033 &setdebuglist, &showdebuglist);
25034
25035 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25036 Set debugging of the DWARF DIE reader."), _("\
25037 Show debugging of the DWARF DIE reader."), _("\
25038 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25039 The value is the maximum depth to print."),
25040 NULL,
25041 NULL,
25042 &setdebuglist, &showdebuglist);
25043
25044 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25045 Set debugging of the dwarf line reader."), _("\
25046 Show debugging of the dwarf line reader."), _("\
25047 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25048 A value of 1 (one) provides basic information.\n\
25049 A value greater than 1 provides more verbose information."),
25050 NULL,
25051 NULL,
25052 &setdebuglist, &showdebuglist);
25053
25054 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25055 Set cross-checking of \"physname\" code against demangler."), _("\
25056 Show cross-checking of \"physname\" code against demangler."), _("\
25057 When enabled, GDB's internal \"physname\" code is checked against\n\
25058 the demangler."),
25059 NULL, show_check_physname,
25060 &setdebuglist, &showdebuglist);
25061
25062 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25063 no_class, &use_deprecated_index_sections, _("\
25064 Set whether to use deprecated gdb_index sections."), _("\
25065 Show whether to use deprecated gdb_index sections."), _("\
25066 When enabled, deprecated .gdb_index sections are used anyway.\n\
25067 Normally they are ignored either because of a missing feature or\n\
25068 performance issue.\n\
25069 Warning: This option must be enabled before gdb reads the file."),
25070 NULL,
25071 NULL,
25072 &setlist, &showlist);
25073
25074 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25075 &dwarf2_locexpr_funcs);
25076 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25077 &dwarf2_loclist_funcs);
25078
25079 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25080 &dwarf2_block_frame_base_locexpr_funcs);
25081 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25082 &dwarf2_block_frame_base_loclist_funcs);
25083
25084 #if GDB_SELF_TEST
25085 selftests::register_test ("dw2_expand_symtabs_matching",
25086 selftests::dw2_expand_symtabs_matching::run_test);
25087 selftests::register_test ("dwarf2_find_containing_comp_unit",
25088 selftests::find_containing_comp_unit::run_test);
25089 #endif
25090 }