]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Change how .debug_aranges padding is skipped
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91 #include <unordered_set>
92
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
97
98 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
99
100 #define dwarf_read_debug_printf(fmt, ...) \
101 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
102 ##__VA_ARGS__)
103
104 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
105
106 #define dwarf_read_debug_printf_v(fmt, ...) \
107 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
109
110 /* When non-zero, dump DIEs after they are read in. */
111 static unsigned int dwarf_die_debug = 0;
112
113 /* When non-zero, dump line number entries as they are read in. */
114 unsigned int dwarf_line_debug = 0;
115
116 /* When true, cross-check physname against demangler. */
117 static bool check_physname = false;
118
119 /* When true, do not reject deprecated .gdb_index sections. */
120 static bool use_deprecated_index_sections = false;
121
122 /* This is used to store the data that is always per objfile. */
123 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125 /* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
135
136 /* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138 static int dwarf2_locexpr_index;
139 static int dwarf2_loclist_index;
140 static int dwarf2_locexpr_block_index;
141 static int dwarf2_loclist_block_index;
142
143 /* Size of .debug_loclists section header for 32-bit DWARF format. */
144 #define LOCLIST_HEADER_SIZE32 12
145
146 /* Size of .debug_loclists section header for 64-bit DWARF format. */
147 #define LOCLIST_HEADER_SIZE64 20
148
149 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
150 #define RNGLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
153 #define RNGLIST_HEADER_SIZE64 20
154
155 /* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168 struct name_component
169 {
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178 };
179
180 /* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183 struct mapped_index_base
184 {
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
211 void build_name_components (dwarf2_per_objfile *per_objfile);
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
218 find_name_components_bounds (const lookup_name_info &ln_no_params,
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
221
222 /* Prevent deleting/destroying via a base class pointer. */
223 protected:
224 ~mapped_index_base() = default;
225 };
226
227 /* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231 class offset_view
232 {
233 public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262 private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265 };
266
267 /* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
269 struct mapped_index final : public mapped_index_base
270 {
271 /* Index data format version. */
272 int version = 0;
273
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
276
277 /* The symbol table, implemented as a hash table. */
278 offset_view symbol_table;
279
280 /* A pointer to the constant pool. */
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
296
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
301 }
302
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size () / 2; }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
363 };
364
365 /* See dwarf2read.h. */
366
367 dwarf2_per_objfile *
368 get_dwarf2_per_objfile (struct objfile *objfile)
369 {
370 return dwarf2_objfile_data_key.get (objfile);
371 }
372
373 /* Default names of the debugging sections. */
374
375 /* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
378 const struct dwarf2_debug_sections dwarf2_elf_names =
379 {
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
384 { ".debug_loclists", ".zdebug_loclists" },
385 { ".debug_macinfo", ".zdebug_macinfo" },
386 { ".debug_macro", ".zdebug_macro" },
387 { ".debug_str", ".zdebug_str" },
388 { ".debug_str_offsets", ".zdebug_str_offsets" },
389 { ".debug_line_str", ".zdebug_line_str" },
390 { ".debug_ranges", ".zdebug_ranges" },
391 { ".debug_rnglists", ".zdebug_rnglists" },
392 { ".debug_types", ".zdebug_types" },
393 { ".debug_addr", ".zdebug_addr" },
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
396 { ".gdb_index", ".zgdb_index" },
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
399 23
400 };
401
402 /* List of DWO/DWP sections. */
403
404 static const struct dwop_section_names
405 {
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
410 struct dwarf2_section_names loclists_dwo;
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
413 struct dwarf2_section_names rnglists_dwo;
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
419 }
420 dwop_section_names =
421 {
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
435 };
436
437 /* local data types */
438
439 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441 struct loclists_rnglists_header
442 {
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461 };
462
463 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466 struct stmt_list_hash
467 {
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
472 sect_offset line_sect_off;
473 };
474
475 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
479
480 struct type_unit_group : public dwarf2_per_cu_data
481 {
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
485 std::vector<signatured_type *> *tus = nullptr;
486
487 /* The data used to construct the hash key. */
488 struct stmt_list_hash hash {};
489 };
490
491 /* These sections are what may appear in a (real or virtual) DWO file. */
492
493 struct dwo_sections
494 {
495 struct dwarf2_section_info abbrev;
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
498 struct dwarf2_section_info loclists;
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
501 struct dwarf2_section_info rnglists;
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
506 std::vector<dwarf2_section_info> types;
507 };
508
509 /* CUs/TUs in DWP/DWO files. */
510
511 struct dwo_unit
512 {
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
522 struct dwarf2_section_info *section;
523
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530 };
531
532 /* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536 enum dwp_v2_section_ids
537 {
538 DW_SECT_MIN = 1
539 };
540
541 /* Data for one DWO file.
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
551
552 struct dwo_file
553 {
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
561 const char *dwo_name = nullptr;
562
563 /* The DW_AT_comp_dir attribute. */
564 const char *comp_dir = nullptr;
565
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
568 gdb_bfd_ref_ptr dbfd;
569
570 /* The sections that make up this DWO file.
571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
572 sections (for lack of a better name). */
573 struct dwo_sections sections {};
574
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
579 htab_up cus;
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
583 htab_up tus;
584 };
585
586 /* These sections are what may appear in a DWP file. */
587
588 struct dwp_sections
589 {
590 /* These are used by all DWP versions (1, 2 and 5). */
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
594
595 /* These are only used by DWP version 2 and version 5 files.
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
607 struct dwarf2_section_info loclists;
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
610 struct dwarf2_section_info rnglists;
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
613 };
614
615 /* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
617
618 struct virtual_v1_dwo_sections
619 {
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
627 That is recorded here, and copied to dwo_unit.section. */
628 struct dwarf2_section_info info_or_types;
629 };
630
631 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
636 struct virtual_v2_or_v5_dwo_sections
637 {
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666 };
667
668 /* Contents of DWP hash tables. */
669
670 struct dwp_hash_table
671 {
672 uint32_t version, nr_columns;
673 uint32_t nr_units, nr_slots;
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685 #define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700 #define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
712 } section_pool;
713 };
714
715 /* Data for one DWP file. */
716
717 struct dwp_file
718 {
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
725 /* Name of the file. */
726 const char *name;
727
728 /* File format version. */
729 int version = 0;
730
731 /* The bfd. */
732 gdb_bfd_ref_ptr dbfd;
733
734 /* Section info for this file. */
735 struct dwp_sections sections {};
736
737 /* Table of CUs in the file. */
738 const struct dwp_hash_table *cus = nullptr;
739
740 /* Table of TUs in the file. */
741 const struct dwp_hash_table *tus = nullptr;
742
743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
744 htab_up loaded_cus;
745 htab_up loaded_tus;
746
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
751 };
752
753 /* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
756 struct exists to abstract away the constant parameters of die reading. */
757
758 struct die_reader_specs
759 {
760 /* The bfd of die_section. */
761 bfd *abfd;
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
767 struct dwo_file *dwo_file;
768
769 /* The section the die comes from.
770 This is either .debug_info or .debug_types, or the .dwo variants. */
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
774 const gdb_byte *buffer;
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
778
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
781 };
782
783 /* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786 class cutu_reader : public die_reader_specs
787 {
788 public:
789
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
792 struct abbrev_table *abbrev_table,
793 dwarf2_cu *existing_cu,
794 bool skip_partial);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
797 dwarf2_per_objfile *per_objfile,
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
805 bool dummy_p = false;
806
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
811 private:
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
814 dwarf2_cu *existing_cu);
815
816 struct dwarf2_per_cu_data *m_this_cu;
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824 };
825
826 /* When we construct a partial symbol table entry we only
827 need this much information. */
828 struct partial_die_info : public allocate_on_obstack
829 {
830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
835 partial_die_info (const partial_die_info &) = default;
836
837 /* Adjust the partial die before generating a symbol for it. This
838 function may set the is_external flag or change the DIE's
839 name. */
840 void fixup (struct dwarf2_cu *cu);
841
842 /* Read a minimal amount of information into the minimal die
843 structure. */
844 const gdb_byte *read (const struct die_reader_specs *reader,
845 const struct abbrev_info &abbrev,
846 const gdb_byte *info_ptr);
847
848 /* Compute the name of this partial DIE. This memoizes the
849 result, so it is safe to call multiple times. */
850 const char *name (dwarf2_cu *cu);
851
852 /* Offset of this DIE. */
853 const sect_offset sect_off;
854
855 /* DWARF-2 tag for this DIE. */
856 const ENUM_BITFIELD(dwarf_tag) tag : 16;
857
858 /* Assorted flags describing the data found in this DIE. */
859 const unsigned int has_children : 1;
860
861 unsigned int is_external : 1;
862 unsigned int is_declaration : 1;
863 unsigned int has_type : 1;
864 unsigned int has_specification : 1;
865 unsigned int has_pc_info : 1;
866 unsigned int may_be_inlined : 1;
867
868 /* This DIE has been marked DW_AT_main_subprogram. */
869 unsigned int main_subprogram : 1;
870
871 /* Flag set if the SCOPE field of this structure has been
872 computed. */
873 unsigned int scope_set : 1;
874
875 /* Flag set if the DIE has a byte_size attribute. */
876 unsigned int has_byte_size : 1;
877
878 /* Flag set if the DIE has a DW_AT_const_value attribute. */
879 unsigned int has_const_value : 1;
880
881 /* Flag set if any of the DIE's children are template arguments. */
882 unsigned int has_template_arguments : 1;
883
884 /* Flag set if fixup has been called on this die. */
885 unsigned int fixup_called : 1;
886
887 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
888 unsigned int is_dwz : 1;
889
890 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
891 unsigned int spec_is_dwz : 1;
892
893 unsigned int canonical_name : 1;
894
895 /* The name of this DIE. Normally the value of DW_AT_name, but
896 sometimes a default name for unnamed DIEs. */
897 const char *raw_name = nullptr;
898
899 /* The linkage name, if present. */
900 const char *linkage_name = nullptr;
901
902 /* The scope to prepend to our children. This is generally
903 allocated on the comp_unit_obstack, so will disappear
904 when this compilation unit leaves the cache. */
905 const char *scope = nullptr;
906
907 /* Some data associated with the partial DIE. The tag determines
908 which field is live. */
909 union
910 {
911 /* The location description associated with this DIE, if any. */
912 struct dwarf_block *locdesc;
913 /* The offset of an import, for DW_TAG_imported_unit. */
914 sect_offset sect_off;
915 } d {};
916
917 /* If HAS_PC_INFO, the PC range associated with this DIE. */
918 CORE_ADDR lowpc = 0;
919 CORE_ADDR highpc = 0;
920
921 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
922 DW_AT_sibling, if any. */
923 /* NOTE: This member isn't strictly necessary, partial_die_info::read
924 could return DW_AT_sibling values to its caller load_partial_dies. */
925 const gdb_byte *sibling = nullptr;
926
927 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
928 DW_AT_specification (or DW_AT_abstract_origin or
929 DW_AT_extension). */
930 sect_offset spec_offset {};
931
932 /* Pointers to this DIE's parent, first child, and next sibling,
933 if any. */
934 struct partial_die_info *die_parent = nullptr;
935 struct partial_die_info *die_child = nullptr;
936 struct partial_die_info *die_sibling = nullptr;
937
938 friend struct partial_die_info *
939 dwarf2_cu::find_partial_die (sect_offset sect_off);
940
941 private:
942 /* Only need to do look up in dwarf2_cu::find_partial_die. */
943 partial_die_info (sect_offset sect_off)
944 : partial_die_info (sect_off, DW_TAG_padding, 0)
945 {
946 }
947
948 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
949 int has_children_)
950 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
951 {
952 is_external = 0;
953 is_declaration = 0;
954 has_type = 0;
955 has_specification = 0;
956 has_pc_info = 0;
957 may_be_inlined = 0;
958 main_subprogram = 0;
959 scope_set = 0;
960 has_byte_size = 0;
961 has_const_value = 0;
962 has_template_arguments = 0;
963 fixup_called = 0;
964 is_dwz = 0;
965 spec_is_dwz = 0;
966 canonical_name = 0;
967 }
968 };
969
970 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
971 but this would require a corresponding change in unpack_field_as_long
972 and friends. */
973 static int bits_per_byte = 8;
974
975 struct variant_part_builder;
976
977 /* When reading a variant, we track a bit more information about the
978 field, and store it in an object of this type. */
979
980 struct variant_field
981 {
982 int first_field = -1;
983 int last_field = -1;
984
985 /* A variant can contain other variant parts. */
986 std::vector<variant_part_builder> variant_parts;
987
988 /* If we see a DW_TAG_variant, then this will be set if this is the
989 default branch. */
990 bool default_branch = false;
991 /* If we see a DW_AT_discr_value, then this will be the discriminant
992 value. */
993 ULONGEST discriminant_value = 0;
994 /* If we see a DW_AT_discr_list, then this is a pointer to the list
995 data. */
996 struct dwarf_block *discr_list_data = nullptr;
997 };
998
999 /* This represents a DW_TAG_variant_part. */
1000
1001 struct variant_part_builder
1002 {
1003 /* The offset of the discriminant field. */
1004 sect_offset discriminant_offset {};
1005
1006 /* Variants that are direct children of this variant part. */
1007 std::vector<variant_field> variants;
1008
1009 /* True if we're currently reading a variant. */
1010 bool processing_variant = false;
1011 };
1012
1013 struct nextfield
1014 {
1015 int accessibility = 0;
1016 int virtuality = 0;
1017 /* Variant parts need to find the discriminant, which is a DIE
1018 reference. We track the section offset of each field to make
1019 this link. */
1020 sect_offset offset;
1021 struct field field {};
1022 };
1023
1024 struct fnfieldlist
1025 {
1026 const char *name = nullptr;
1027 std::vector<struct fn_field> fnfields;
1028 };
1029
1030 /* The routines that read and process dies for a C struct or C++ class
1031 pass lists of data member fields and lists of member function fields
1032 in an instance of a field_info structure, as defined below. */
1033 struct field_info
1034 {
1035 /* List of data member and baseclasses fields. */
1036 std::vector<struct nextfield> fields;
1037 std::vector<struct nextfield> baseclasses;
1038
1039 /* Set if the accessibility of one of the fields is not public. */
1040 bool non_public_fields = false;
1041
1042 /* Member function fieldlist array, contains name of possibly overloaded
1043 member function, number of overloaded member functions and a pointer
1044 to the head of the member function field chain. */
1045 std::vector<struct fnfieldlist> fnfieldlists;
1046
1047 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1048 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1049 std::vector<struct decl_field> typedef_field_list;
1050
1051 /* Nested types defined by this class and the number of elements in this
1052 list. */
1053 std::vector<struct decl_field> nested_types_list;
1054
1055 /* If non-null, this is the variant part we are currently
1056 reading. */
1057 variant_part_builder *current_variant_part = nullptr;
1058 /* This holds all the top-level variant parts attached to the type
1059 we're reading. */
1060 std::vector<variant_part_builder> variant_parts;
1061
1062 /* Return the total number of fields (including baseclasses). */
1063 int nfields () const
1064 {
1065 return fields.size () + baseclasses.size ();
1066 }
1067 };
1068
1069 /* Loaded secondary compilation units are kept in memory until they
1070 have not been referenced for the processing of this many
1071 compilation units. Set this to zero to disable caching. Cache
1072 sizes of up to at least twenty will improve startup time for
1073 typical inter-CU-reference binaries, at an obvious memory cost. */
1074 static int dwarf_max_cache_age = 5;
1075 static void
1076 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1077 struct cmd_list_element *c, const char *value)
1078 {
1079 fprintf_filtered (file, _("The upper bound on the age of cached "
1080 "DWARF compilation units is %s.\n"),
1081 value);
1082 }
1083 \f
1084 /* local function prototypes */
1085
1086 static void dwarf2_find_base_address (struct die_info *die,
1087 struct dwarf2_cu *cu);
1088
1089 static dwarf2_psymtab *create_partial_symtab
1090 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1091 const char *name);
1092
1093 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1094 const gdb_byte *info_ptr,
1095 struct die_info *type_unit_die);
1096
1097 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1098
1099 static void scan_partial_symbols (struct partial_die_info *,
1100 CORE_ADDR *, CORE_ADDR *,
1101 int, struct dwarf2_cu *);
1102
1103 static void add_partial_symbol (struct partial_die_info *,
1104 struct dwarf2_cu *);
1105
1106 static void add_partial_namespace (struct partial_die_info *pdi,
1107 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1108 int set_addrmap, struct dwarf2_cu *cu);
1109
1110 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1111 CORE_ADDR *highpc, int set_addrmap,
1112 struct dwarf2_cu *cu);
1113
1114 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1115 struct dwarf2_cu *cu);
1116
1117 static void add_partial_subprogram (struct partial_die_info *pdi,
1118 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1119 int need_pc, struct dwarf2_cu *cu);
1120
1121 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1122
1123 static struct partial_die_info *load_partial_dies
1124 (const struct die_reader_specs *, const gdb_byte *, int);
1125
1126 /* A pair of partial_die_info and compilation unit. */
1127 struct cu_partial_die_info
1128 {
1129 /* The compilation unit of the partial_die_info. */
1130 struct dwarf2_cu *cu;
1131 /* A partial_die_info. */
1132 struct partial_die_info *pdi;
1133
1134 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1135 : cu (cu),
1136 pdi (pdi)
1137 { /* Nothing. */ }
1138
1139 private:
1140 cu_partial_die_info () = delete;
1141 };
1142
1143 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1144 struct dwarf2_cu *);
1145
1146 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1147 struct attribute *,
1148 const struct attr_abbrev *,
1149 const gdb_byte *);
1150
1151 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1152 struct attribute *attr, dwarf_tag tag);
1153
1154 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1155
1156 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1157 dwarf2_section_info *, sect_offset);
1158
1159 static const char *read_indirect_string
1160 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1161 const struct comp_unit_head *, unsigned int *);
1162
1163 static const char *read_indirect_string_at_offset
1164 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1165
1166 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1167 const gdb_byte *,
1168 unsigned int *);
1169
1170 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1171 ULONGEST str_index);
1172
1173 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1174 ULONGEST str_index);
1175
1176 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1177 struct dwarf2_cu *);
1178
1179 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1180 struct dwarf2_cu *cu);
1181
1182 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1183
1184 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1185 struct dwarf2_cu *cu);
1186
1187 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1188
1189 static struct die_info *die_specification (struct die_info *die,
1190 struct dwarf2_cu **);
1191
1192 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1193 struct dwarf2_cu *cu);
1194
1195 static void dwarf_decode_lines (struct line_header *, const char *,
1196 struct dwarf2_cu *, dwarf2_psymtab *,
1197 CORE_ADDR, int decode_mapping);
1198
1199 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1200 const char *);
1201
1202 static struct symbol *new_symbol (struct die_info *, struct type *,
1203 struct dwarf2_cu *, struct symbol * = NULL);
1204
1205 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1206 struct dwarf2_cu *);
1207
1208 static void dwarf2_const_value_attr (const struct attribute *attr,
1209 struct type *type,
1210 const char *name,
1211 struct obstack *obstack,
1212 struct dwarf2_cu *cu, LONGEST *value,
1213 const gdb_byte **bytes,
1214 struct dwarf2_locexpr_baton **baton);
1215
1216 static struct type *read_subrange_index_type (struct die_info *die,
1217 struct dwarf2_cu *cu);
1218
1219 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1220
1221 static int need_gnat_info (struct dwarf2_cu *);
1222
1223 static struct type *die_descriptive_type (struct die_info *,
1224 struct dwarf2_cu *);
1225
1226 static void set_descriptive_type (struct type *, struct die_info *,
1227 struct dwarf2_cu *);
1228
1229 static struct type *die_containing_type (struct die_info *,
1230 struct dwarf2_cu *);
1231
1232 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1233 struct dwarf2_cu *);
1234
1235 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1236
1237 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1238
1239 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1240
1241 static char *typename_concat (struct obstack *obs, const char *prefix,
1242 const char *suffix, int physname,
1243 struct dwarf2_cu *cu);
1244
1245 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1246
1247 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1248
1249 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1250
1251 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1252
1253 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1254
1255 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1256
1257 /* Return the .debug_loclists section to use for cu. */
1258 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1259
1260 /* Return the .debug_rnglists section to use for cu. */
1261 static struct dwarf2_section_info *cu_debug_rnglists_section
1262 (struct dwarf2_cu *cu, dwarf_tag tag);
1263
1264 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1265 values. Keep the items ordered with increasing constraints compliance. */
1266 enum pc_bounds_kind
1267 {
1268 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1269 PC_BOUNDS_NOT_PRESENT,
1270
1271 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1272 were present but they do not form a valid range of PC addresses. */
1273 PC_BOUNDS_INVALID,
1274
1275 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1276 PC_BOUNDS_RANGES,
1277
1278 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1279 PC_BOUNDS_HIGH_LOW,
1280 };
1281
1282 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1283 CORE_ADDR *, CORE_ADDR *,
1284 struct dwarf2_cu *,
1285 dwarf2_psymtab *);
1286
1287 static void get_scope_pc_bounds (struct die_info *,
1288 CORE_ADDR *, CORE_ADDR *,
1289 struct dwarf2_cu *);
1290
1291 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1292 CORE_ADDR, struct dwarf2_cu *);
1293
1294 static void dwarf2_add_field (struct field_info *, struct die_info *,
1295 struct dwarf2_cu *);
1296
1297 static void dwarf2_attach_fields_to_type (struct field_info *,
1298 struct type *, struct dwarf2_cu *);
1299
1300 static void dwarf2_add_member_fn (struct field_info *,
1301 struct die_info *, struct type *,
1302 struct dwarf2_cu *);
1303
1304 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1305 struct type *,
1306 struct dwarf2_cu *);
1307
1308 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1309
1310 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1311
1312 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1313
1314 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1315
1316 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1317
1318 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1319
1320 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1321
1322 static struct type *read_module_type (struct die_info *die,
1323 struct dwarf2_cu *cu);
1324
1325 static const char *namespace_name (struct die_info *die,
1326 int *is_anonymous, struct dwarf2_cu *);
1327
1328 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1329
1330 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1331 bool * = nullptr);
1332
1333 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1334 struct dwarf2_cu *);
1335
1336 static struct die_info *read_die_and_siblings_1
1337 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1338 struct die_info *);
1339
1340 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1341 const gdb_byte *info_ptr,
1342 const gdb_byte **new_info_ptr,
1343 struct die_info *parent);
1344
1345 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1346 struct die_info **, const gdb_byte *,
1347 int);
1348
1349 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1350 struct die_info **, const gdb_byte *);
1351
1352 static void process_die (struct die_info *, struct dwarf2_cu *);
1353
1354 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1355 struct objfile *);
1356
1357 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1358
1359 static const char *dwarf2_full_name (const char *name,
1360 struct die_info *die,
1361 struct dwarf2_cu *cu);
1362
1363 static const char *dwarf2_physname (const char *name, struct die_info *die,
1364 struct dwarf2_cu *cu);
1365
1366 static struct die_info *dwarf2_extension (struct die_info *die,
1367 struct dwarf2_cu **);
1368
1369 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1370
1371 static void dump_die_for_error (struct die_info *);
1372
1373 static void dump_die_1 (struct ui_file *, int level, int max_level,
1374 struct die_info *);
1375
1376 /*static*/ void dump_die (struct die_info *, int max_level);
1377
1378 static void store_in_ref_table (struct die_info *,
1379 struct dwarf2_cu *);
1380
1381 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1382 const struct attribute *,
1383 struct dwarf2_cu **);
1384
1385 static struct die_info *follow_die_ref (struct die_info *,
1386 const struct attribute *,
1387 struct dwarf2_cu **);
1388
1389 static struct die_info *follow_die_sig (struct die_info *,
1390 const struct attribute *,
1391 struct dwarf2_cu **);
1392
1393 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1394 struct dwarf2_cu *);
1395
1396 static struct type *get_DW_AT_signature_type (struct die_info *,
1397 const struct attribute *,
1398 struct dwarf2_cu *);
1399
1400 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1401 dwarf2_per_objfile *per_objfile);
1402
1403 static void read_signatured_type (signatured_type *sig_type,
1404 dwarf2_per_objfile *per_objfile);
1405
1406 static int attr_to_dynamic_prop (const struct attribute *attr,
1407 struct die_info *die, struct dwarf2_cu *cu,
1408 struct dynamic_prop *prop, struct type *type);
1409
1410 /* memory allocation interface */
1411
1412 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1413
1414 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1415
1416 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1417
1418 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1419 struct dwarf2_loclist_baton *baton,
1420 const struct attribute *attr);
1421
1422 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1423 struct symbol *sym,
1424 struct dwarf2_cu *cu,
1425 int is_block);
1426
1427 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1428 const gdb_byte *info_ptr,
1429 const struct abbrev_info *abbrev);
1430
1431 static hashval_t partial_die_hash (const void *item);
1432
1433 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1434
1435 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1436 (sect_offset sect_off, unsigned int offset_in_dwz,
1437 dwarf2_per_objfile *per_objfile);
1438
1439 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1440 struct die_info *comp_unit_die,
1441 enum language pretend_language);
1442
1443 static struct type *set_die_type (struct die_info *, struct type *,
1444 struct dwarf2_cu *, bool = false);
1445
1446 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1447
1448 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1449 dwarf2_per_objfile *per_objfile,
1450 dwarf2_cu *existing_cu,
1451 bool skip_partial,
1452 enum language pretend_language);
1453
1454 static void process_full_comp_unit (dwarf2_cu *cu,
1455 enum language pretend_language);
1456
1457 static void process_full_type_unit (dwarf2_cu *cu,
1458 enum language pretend_language);
1459
1460 static struct type *get_die_type_at_offset (sect_offset,
1461 dwarf2_per_cu_data *per_cu,
1462 dwarf2_per_objfile *per_objfile);
1463
1464 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1465
1466 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1467 dwarf2_per_objfile *per_objfile,
1468 enum language pretend_language);
1469
1470 static void process_queue (dwarf2_per_objfile *per_objfile);
1471
1472 /* Class, the destructor of which frees all allocated queue entries. This
1473 will only have work to do if an error was thrown while processing the
1474 dwarf. If no error was thrown then the queue entries should have all
1475 been processed, and freed, as we went along. */
1476
1477 class dwarf2_queue_guard
1478 {
1479 public:
1480 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1481 : m_per_objfile (per_objfile)
1482 {
1483 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1484
1485 m_per_objfile->per_bfd->queue.emplace ();
1486 }
1487
1488 /* Free any entries remaining on the queue. There should only be
1489 entries left if we hit an error while processing the dwarf. */
1490 ~dwarf2_queue_guard ()
1491 {
1492 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1493
1494 m_per_objfile->per_bfd->queue.reset ();
1495 }
1496
1497 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1498
1499 private:
1500 dwarf2_per_objfile *m_per_objfile;
1501 };
1502
1503 dwarf2_queue_item::~dwarf2_queue_item ()
1504 {
1505 /* Anything still marked queued is likely to be in an
1506 inconsistent state, so discard it. */
1507 if (per_cu->queued)
1508 {
1509 per_objfile->remove_cu (per_cu);
1510 per_cu->queued = 0;
1511 }
1512 }
1513
1514 /* See dwarf2/read.h. */
1515
1516 void
1517 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1518 {
1519 if (data->is_debug_types)
1520 delete static_cast<signatured_type *> (data);
1521 else
1522 delete data;
1523 }
1524
1525 /* The return type of find_file_and_directory. Note, the enclosed
1526 string pointers are only valid while this object is valid. */
1527
1528 struct file_and_directory
1529 {
1530 /* The filename. This is never NULL. */
1531 const char *name;
1532
1533 /* The compilation directory. NULL if not known. If we needed to
1534 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1535 points directly to the DW_AT_comp_dir string attribute owned by
1536 the obstack that owns the DIE. */
1537 const char *comp_dir;
1538
1539 /* If we needed to build a new string for comp_dir, this is what
1540 owns the storage. */
1541 std::string comp_dir_storage;
1542 };
1543
1544 static file_and_directory find_file_and_directory (struct die_info *die,
1545 struct dwarf2_cu *cu);
1546
1547 static htab_up allocate_signatured_type_table ();
1548
1549 static htab_up allocate_dwo_unit_table ();
1550
1551 static struct dwo_unit *lookup_dwo_unit_in_dwp
1552 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1553 const char *comp_dir, ULONGEST signature, int is_debug_types);
1554
1555 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1556
1557 static struct dwo_unit *lookup_dwo_comp_unit
1558 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1559 ULONGEST signature);
1560
1561 static struct dwo_unit *lookup_dwo_type_unit
1562 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1563
1564 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1565
1566 /* A unique pointer to a dwo_file. */
1567
1568 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1569
1570 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1571
1572 static void check_producer (struct dwarf2_cu *cu);
1573 \f
1574 /* Various complaints about symbol reading that don't abort the process. */
1575
1576 static void
1577 dwarf2_debug_line_missing_file_complaint (void)
1578 {
1579 complaint (_(".debug_line section has line data without a file"));
1580 }
1581
1582 static void
1583 dwarf2_debug_line_missing_end_sequence_complaint (void)
1584 {
1585 complaint (_(".debug_line section has line "
1586 "program sequence without an end"));
1587 }
1588
1589 static void
1590 dwarf2_complex_location_expr_complaint (void)
1591 {
1592 complaint (_("location expression too complex"));
1593 }
1594
1595 static void
1596 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1597 int arg3)
1598 {
1599 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1600 arg1, arg2, arg3);
1601 }
1602
1603 static void
1604 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1605 {
1606 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1607 arg1, arg2);
1608 }
1609
1610 /* Hash function for line_header_hash. */
1611
1612 static hashval_t
1613 line_header_hash (const struct line_header *ofs)
1614 {
1615 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1616 }
1617
1618 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1619
1620 static hashval_t
1621 line_header_hash_voidp (const void *item)
1622 {
1623 const struct line_header *ofs = (const struct line_header *) item;
1624
1625 return line_header_hash (ofs);
1626 }
1627
1628 /* Equality function for line_header_hash. */
1629
1630 static int
1631 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1632 {
1633 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1634 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1635
1636 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1637 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1638 }
1639
1640 \f
1641
1642 /* See declaration. */
1643
1644 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1645 bool can_copy_)
1646 : obfd (obfd),
1647 can_copy (can_copy_)
1648 {
1649 if (names == NULL)
1650 names = &dwarf2_elf_names;
1651
1652 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1653 locate_sections (obfd, sec, *names);
1654 }
1655
1656 dwarf2_per_bfd::~dwarf2_per_bfd ()
1657 {
1658 for (auto &per_cu : all_comp_units)
1659 per_cu->imported_symtabs_free ();
1660
1661 /* Everything else should be on this->obstack. */
1662 }
1663
1664 /* See read.h. */
1665
1666 void
1667 dwarf2_per_objfile::remove_all_cus ()
1668 {
1669 gdb_assert (!this->per_bfd->queue.has_value ());
1670
1671 for (auto pair : m_dwarf2_cus)
1672 delete pair.second;
1673
1674 m_dwarf2_cus.clear ();
1675 }
1676
1677 /* A helper class that calls free_cached_comp_units on
1678 destruction. */
1679
1680 class free_cached_comp_units
1681 {
1682 public:
1683
1684 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1685 : m_per_objfile (per_objfile)
1686 {
1687 }
1688
1689 ~free_cached_comp_units ()
1690 {
1691 m_per_objfile->remove_all_cus ();
1692 }
1693
1694 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1695
1696 private:
1697
1698 dwarf2_per_objfile *m_per_objfile;
1699 };
1700
1701 /* See read.h. */
1702
1703 bool
1704 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1705 {
1706 gdb_assert (per_cu->index < this->m_symtabs.size ());
1707
1708 return this->m_symtabs[per_cu->index] != nullptr;
1709 }
1710
1711 /* See read.h. */
1712
1713 compunit_symtab *
1714 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1715 {
1716 gdb_assert (per_cu->index < this->m_symtabs.size ());
1717
1718 return this->m_symtabs[per_cu->index];
1719 }
1720
1721 /* See read.h. */
1722
1723 void
1724 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1725 compunit_symtab *symtab)
1726 {
1727 gdb_assert (per_cu->index < this->m_symtabs.size ());
1728 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1729
1730 this->m_symtabs[per_cu->index] = symtab;
1731 }
1732
1733 /* Try to locate the sections we need for DWARF 2 debugging
1734 information and return true if we have enough to do something.
1735 NAMES points to the dwarf2 section names, or is NULL if the standard
1736 ELF names are used. CAN_COPY is true for formats where symbol
1737 interposition is possible and so symbol values must follow copy
1738 relocation rules. */
1739
1740 int
1741 dwarf2_has_info (struct objfile *objfile,
1742 const struct dwarf2_debug_sections *names,
1743 bool can_copy)
1744 {
1745 if (objfile->flags & OBJF_READNEVER)
1746 return 0;
1747
1748 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1749
1750 if (per_objfile == NULL)
1751 {
1752 dwarf2_per_bfd *per_bfd;
1753
1754 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1755 BFD doesn't require relocations.
1756
1757 We don't share with objfiles for which -readnow was requested,
1758 because it would complicate things when loading the same BFD with
1759 -readnow and then without -readnow. */
1760 if (!gdb_bfd_requires_relocations (objfile->obfd)
1761 && (objfile->flags & OBJF_READNOW) == 0)
1762 {
1763 /* See if one has been created for this BFD yet. */
1764 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1765
1766 if (per_bfd == nullptr)
1767 {
1768 /* No, create it now. */
1769 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1770 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1771 }
1772 }
1773 else
1774 {
1775 /* No sharing possible, create one specifically for this objfile. */
1776 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1777 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1778 }
1779
1780 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1781 }
1782
1783 return (!per_objfile->per_bfd->info.is_virtual
1784 && per_objfile->per_bfd->info.s.section != NULL
1785 && !per_objfile->per_bfd->abbrev.is_virtual
1786 && per_objfile->per_bfd->abbrev.s.section != NULL);
1787 }
1788
1789 /* See declaration. */
1790
1791 void
1792 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1793 const dwarf2_debug_sections &names)
1794 {
1795 flagword aflag = bfd_section_flags (sectp);
1796
1797 if ((aflag & SEC_HAS_CONTENTS) == 0)
1798 {
1799 }
1800 else if (elf_section_data (sectp)->this_hdr.sh_size
1801 > bfd_get_file_size (abfd))
1802 {
1803 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1804 warning (_("Discarding section %s which has a section size (%s"
1805 ") larger than the file size [in module %s]"),
1806 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1807 bfd_get_filename (abfd));
1808 }
1809 else if (names.info.matches (sectp->name))
1810 {
1811 this->info.s.section = sectp;
1812 this->info.size = bfd_section_size (sectp);
1813 }
1814 else if (names.abbrev.matches (sectp->name))
1815 {
1816 this->abbrev.s.section = sectp;
1817 this->abbrev.size = bfd_section_size (sectp);
1818 }
1819 else if (names.line.matches (sectp->name))
1820 {
1821 this->line.s.section = sectp;
1822 this->line.size = bfd_section_size (sectp);
1823 }
1824 else if (names.loc.matches (sectp->name))
1825 {
1826 this->loc.s.section = sectp;
1827 this->loc.size = bfd_section_size (sectp);
1828 }
1829 else if (names.loclists.matches (sectp->name))
1830 {
1831 this->loclists.s.section = sectp;
1832 this->loclists.size = bfd_section_size (sectp);
1833 }
1834 else if (names.macinfo.matches (sectp->name))
1835 {
1836 this->macinfo.s.section = sectp;
1837 this->macinfo.size = bfd_section_size (sectp);
1838 }
1839 else if (names.macro.matches (sectp->name))
1840 {
1841 this->macro.s.section = sectp;
1842 this->macro.size = bfd_section_size (sectp);
1843 }
1844 else if (names.str.matches (sectp->name))
1845 {
1846 this->str.s.section = sectp;
1847 this->str.size = bfd_section_size (sectp);
1848 }
1849 else if (names.str_offsets.matches (sectp->name))
1850 {
1851 this->str_offsets.s.section = sectp;
1852 this->str_offsets.size = bfd_section_size (sectp);
1853 }
1854 else if (names.line_str.matches (sectp->name))
1855 {
1856 this->line_str.s.section = sectp;
1857 this->line_str.size = bfd_section_size (sectp);
1858 }
1859 else if (names.addr.matches (sectp->name))
1860 {
1861 this->addr.s.section = sectp;
1862 this->addr.size = bfd_section_size (sectp);
1863 }
1864 else if (names.frame.matches (sectp->name))
1865 {
1866 this->frame.s.section = sectp;
1867 this->frame.size = bfd_section_size (sectp);
1868 }
1869 else if (names.eh_frame.matches (sectp->name))
1870 {
1871 this->eh_frame.s.section = sectp;
1872 this->eh_frame.size = bfd_section_size (sectp);
1873 }
1874 else if (names.ranges.matches (sectp->name))
1875 {
1876 this->ranges.s.section = sectp;
1877 this->ranges.size = bfd_section_size (sectp);
1878 }
1879 else if (names.rnglists.matches (sectp->name))
1880 {
1881 this->rnglists.s.section = sectp;
1882 this->rnglists.size = bfd_section_size (sectp);
1883 }
1884 else if (names.types.matches (sectp->name))
1885 {
1886 struct dwarf2_section_info type_section;
1887
1888 memset (&type_section, 0, sizeof (type_section));
1889 type_section.s.section = sectp;
1890 type_section.size = bfd_section_size (sectp);
1891
1892 this->types.push_back (type_section);
1893 }
1894 else if (names.gdb_index.matches (sectp->name))
1895 {
1896 this->gdb_index.s.section = sectp;
1897 this->gdb_index.size = bfd_section_size (sectp);
1898 }
1899 else if (names.debug_names.matches (sectp->name))
1900 {
1901 this->debug_names.s.section = sectp;
1902 this->debug_names.size = bfd_section_size (sectp);
1903 }
1904 else if (names.debug_aranges.matches (sectp->name))
1905 {
1906 this->debug_aranges.s.section = sectp;
1907 this->debug_aranges.size = bfd_section_size (sectp);
1908 }
1909
1910 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1911 && bfd_section_vma (sectp) == 0)
1912 this->has_section_at_zero = true;
1913 }
1914
1915 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1916 SECTION_NAME. */
1917
1918 void
1919 dwarf2_get_section_info (struct objfile *objfile,
1920 enum dwarf2_section_enum sect,
1921 asection **sectp, const gdb_byte **bufp,
1922 bfd_size_type *sizep)
1923 {
1924 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1925 struct dwarf2_section_info *info;
1926
1927 /* We may see an objfile without any DWARF, in which case we just
1928 return nothing. */
1929 if (per_objfile == NULL)
1930 {
1931 *sectp = NULL;
1932 *bufp = NULL;
1933 *sizep = 0;
1934 return;
1935 }
1936 switch (sect)
1937 {
1938 case DWARF2_DEBUG_FRAME:
1939 info = &per_objfile->per_bfd->frame;
1940 break;
1941 case DWARF2_EH_FRAME:
1942 info = &per_objfile->per_bfd->eh_frame;
1943 break;
1944 default:
1945 gdb_assert_not_reached ("unexpected section");
1946 }
1947
1948 info->read (objfile);
1949
1950 *sectp = info->get_bfd_section ();
1951 *bufp = info->buffer;
1952 *sizep = info->size;
1953 }
1954
1955 \f
1956 /* DWARF quick_symbol_functions support. */
1957
1958 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1959 unique line tables, so we maintain a separate table of all .debug_line
1960 derived entries to support the sharing.
1961 All the quick functions need is the list of file names. We discard the
1962 line_header when we're done and don't need to record it here. */
1963 struct quick_file_names
1964 {
1965 /* The data used to construct the hash key. */
1966 struct stmt_list_hash hash;
1967
1968 /* The number of entries in file_names, real_names. */
1969 unsigned int num_file_names;
1970
1971 /* The file names from the line table, after being run through
1972 file_full_name. */
1973 const char **file_names;
1974
1975 /* The file names from the line table after being run through
1976 gdb_realpath. These are computed lazily. */
1977 const char **real_names;
1978 };
1979
1980 /* When using the index (and thus not using psymtabs), each CU has an
1981 object of this type. This is used to hold information needed by
1982 the various "quick" methods. */
1983 struct dwarf2_per_cu_quick_data
1984 {
1985 /* The file table. This can be NULL if there was no file table
1986 or it's currently not read in.
1987 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
1988 struct quick_file_names *file_names;
1989
1990 /* A temporary mark bit used when iterating over all CUs in
1991 expand_symtabs_matching. */
1992 unsigned int mark : 1;
1993
1994 /* True if we've tried to read the file table and found there isn't one.
1995 There will be no point in trying to read it again next time. */
1996 unsigned int no_file_data : 1;
1997 };
1998
1999 /* A subclass of psymbol_functions that arranges to read the DWARF
2000 partial symbols when needed. */
2001 struct lazy_dwarf_reader : public psymbol_functions
2002 {
2003 using psymbol_functions::psymbol_functions;
2004
2005 bool can_lazily_read_symbols () override
2006 {
2007 return true;
2008 }
2009
2010 void read_partial_symbols (struct objfile *objfile) override
2011 {
2012 if (dwarf2_has_info (objfile, nullptr))
2013 dwarf2_build_psymtabs (objfile, this);
2014 }
2015 };
2016
2017 static quick_symbol_functions_up
2018 make_lazy_dwarf_reader ()
2019 {
2020 return quick_symbol_functions_up (new lazy_dwarf_reader);
2021 }
2022
2023 struct dwarf2_base_index_functions : public quick_symbol_functions
2024 {
2025 bool has_symbols (struct objfile *objfile) override;
2026
2027 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2028
2029 void forget_cached_source_info (struct objfile *objfile) override;
2030
2031 enum language lookup_global_symbol_language (struct objfile *objfile,
2032 const char *name,
2033 domain_enum domain,
2034 bool *symbol_found_p) override
2035 {
2036 *symbol_found_p = false;
2037 return language_unknown;
2038 }
2039
2040 void print_stats (struct objfile *objfile, bool print_bcache) override;
2041
2042 void expand_all_symtabs (struct objfile *objfile) override;
2043
2044 struct compunit_symtab *find_pc_sect_compunit_symtab
2045 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2046 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2047
2048 struct compunit_symtab *find_compunit_symtab_by_address
2049 (struct objfile *objfile, CORE_ADDR address) override
2050 {
2051 return nullptr;
2052 }
2053
2054 void map_symbol_filenames (struct objfile *objfile,
2055 gdb::function_view<symbol_filename_ftype> fun,
2056 bool need_fullname) override;
2057 };
2058
2059 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2060 {
2061 void dump (struct objfile *objfile) override;
2062
2063 void expand_matching_symbols
2064 (struct objfile *,
2065 const lookup_name_info &lookup_name,
2066 domain_enum domain,
2067 int global,
2068 symbol_compare_ftype *ordered_compare) override;
2069
2070 bool expand_symtabs_matching
2071 (struct objfile *objfile,
2072 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2073 const lookup_name_info *lookup_name,
2074 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2075 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2076 block_search_flags search_flags,
2077 domain_enum domain,
2078 enum search_domain kind) override;
2079 };
2080
2081 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2082 {
2083 void dump (struct objfile *objfile) override;
2084
2085 void expand_matching_symbols
2086 (struct objfile *,
2087 const lookup_name_info &lookup_name,
2088 domain_enum domain,
2089 int global,
2090 symbol_compare_ftype *ordered_compare) override;
2091
2092 bool expand_symtabs_matching
2093 (struct objfile *objfile,
2094 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2095 const lookup_name_info *lookup_name,
2096 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2097 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2098 block_search_flags search_flags,
2099 domain_enum domain,
2100 enum search_domain kind) override;
2101 };
2102
2103 static quick_symbol_functions_up
2104 make_dwarf_gdb_index ()
2105 {
2106 return quick_symbol_functions_up (new dwarf2_gdb_index);
2107 }
2108
2109 static quick_symbol_functions_up
2110 make_dwarf_debug_names ()
2111 {
2112 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2113 }
2114
2115 /* Utility hash function for a stmt_list_hash. */
2116
2117 static hashval_t
2118 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2119 {
2120 hashval_t v = 0;
2121
2122 if (stmt_list_hash->dwo_unit != NULL)
2123 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2124 v += to_underlying (stmt_list_hash->line_sect_off);
2125 return v;
2126 }
2127
2128 /* Utility equality function for a stmt_list_hash. */
2129
2130 static int
2131 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2132 const struct stmt_list_hash *rhs)
2133 {
2134 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2135 return 0;
2136 if (lhs->dwo_unit != NULL
2137 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2138 return 0;
2139
2140 return lhs->line_sect_off == rhs->line_sect_off;
2141 }
2142
2143 /* Hash function for a quick_file_names. */
2144
2145 static hashval_t
2146 hash_file_name_entry (const void *e)
2147 {
2148 const struct quick_file_names *file_data
2149 = (const struct quick_file_names *) e;
2150
2151 return hash_stmt_list_entry (&file_data->hash);
2152 }
2153
2154 /* Equality function for a quick_file_names. */
2155
2156 static int
2157 eq_file_name_entry (const void *a, const void *b)
2158 {
2159 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2160 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2161
2162 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2163 }
2164
2165 /* Delete function for a quick_file_names. */
2166
2167 static void
2168 delete_file_name_entry (void *e)
2169 {
2170 struct quick_file_names *file_data = (struct quick_file_names *) e;
2171 int i;
2172
2173 for (i = 0; i < file_data->num_file_names; ++i)
2174 {
2175 xfree ((void*) file_data->file_names[i]);
2176 if (file_data->real_names)
2177 xfree ((void*) file_data->real_names[i]);
2178 }
2179
2180 /* The space for the struct itself lives on the obstack, so we don't
2181 free it here. */
2182 }
2183
2184 /* Create a quick_file_names hash table. */
2185
2186 static htab_up
2187 create_quick_file_names_table (unsigned int nr_initial_entries)
2188 {
2189 return htab_up (htab_create_alloc (nr_initial_entries,
2190 hash_file_name_entry, eq_file_name_entry,
2191 delete_file_name_entry, xcalloc, xfree));
2192 }
2193
2194 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2195 function is unrelated to symtabs, symtab would have to be created afterwards.
2196 You should call age_cached_comp_units after processing the CU. */
2197
2198 static dwarf2_cu *
2199 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2200 bool skip_partial)
2201 {
2202 if (per_cu->is_debug_types)
2203 load_full_type_unit (per_cu, per_objfile);
2204 else
2205 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2206 skip_partial, language_minimal);
2207
2208 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2209 if (cu == nullptr)
2210 return nullptr; /* Dummy CU. */
2211
2212 dwarf2_find_base_address (cu->dies, cu);
2213
2214 return cu;
2215 }
2216
2217 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2218
2219 static void
2220 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2221 dwarf2_per_objfile *per_objfile, bool skip_partial)
2222 {
2223 /* Skip type_unit_groups, reading the type units they contain
2224 is handled elsewhere. */
2225 if (per_cu->type_unit_group_p ())
2226 return;
2227
2228 {
2229 /* The destructor of dwarf2_queue_guard frees any entries left on
2230 the queue. After this point we're guaranteed to leave this function
2231 with the dwarf queue empty. */
2232 dwarf2_queue_guard q_guard (per_objfile);
2233
2234 if (!per_objfile->symtab_set_p (per_cu))
2235 {
2236 queue_comp_unit (per_cu, per_objfile, language_minimal);
2237 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2238
2239 /* If we just loaded a CU from a DWO, and we're working with an index
2240 that may badly handle TUs, load all the TUs in that DWO as well.
2241 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2242 if (!per_cu->is_debug_types
2243 && cu != NULL
2244 && cu->dwo_unit != NULL
2245 && per_objfile->per_bfd->index_table != NULL
2246 && per_objfile->per_bfd->index_table->version <= 7
2247 /* DWP files aren't supported yet. */
2248 && get_dwp_file (per_objfile) == NULL)
2249 queue_and_load_all_dwo_tus (cu);
2250 }
2251
2252 process_queue (per_objfile);
2253 }
2254
2255 /* Age the cache, releasing compilation units that have not
2256 been used recently. */
2257 per_objfile->age_comp_units ();
2258 }
2259
2260 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2261 the per-objfile for which this symtab is instantiated.
2262
2263 Returns the resulting symbol table. */
2264
2265 static struct compunit_symtab *
2266 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2267 dwarf2_per_objfile *per_objfile,
2268 bool skip_partial)
2269 {
2270 gdb_assert (per_objfile->per_bfd->using_index);
2271
2272 if (!per_objfile->symtab_set_p (per_cu))
2273 {
2274 free_cached_comp_units freer (per_objfile);
2275 scoped_restore decrementer = increment_reading_symtab ();
2276 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2277 process_cu_includes (per_objfile);
2278 }
2279
2280 return per_objfile->get_symtab (per_cu);
2281 }
2282
2283 /* See read.h. */
2284
2285 dwarf2_per_cu_data_up
2286 dwarf2_per_bfd::allocate_per_cu ()
2287 {
2288 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2289 result->per_bfd = this;
2290 result->index = all_comp_units.size ();
2291 return result;
2292 }
2293
2294 /* See read.h. */
2295
2296 signatured_type_up
2297 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2298 {
2299 signatured_type_up result (new signatured_type (signature));
2300 result->per_bfd = this;
2301 result->index = all_comp_units.size ();
2302 result->is_debug_types = true;
2303 tu_stats.nr_tus++;
2304 return result;
2305 }
2306
2307 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2308 obstack, and constructed with the specified field values. */
2309
2310 static dwarf2_per_cu_data_up
2311 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2312 struct dwarf2_section_info *section,
2313 int is_dwz,
2314 sect_offset sect_off, ULONGEST length)
2315 {
2316 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2317 the_cu->sect_off = sect_off;
2318 the_cu->length = length;
2319 the_cu->section = section;
2320 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2321 struct dwarf2_per_cu_quick_data);
2322 the_cu->is_dwz = is_dwz;
2323 return the_cu;
2324 }
2325
2326 /* A helper for create_cus_from_index that handles a given list of
2327 CUs. */
2328
2329 static void
2330 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2331 const gdb_byte *cu_list, offset_type n_elements,
2332 struct dwarf2_section_info *section,
2333 int is_dwz)
2334 {
2335 for (offset_type i = 0; i < n_elements; i += 2)
2336 {
2337 gdb_static_assert (sizeof (ULONGEST) >= 8);
2338
2339 sect_offset sect_off
2340 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2341 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2342 cu_list += 2 * 8;
2343
2344 dwarf2_per_cu_data_up per_cu
2345 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2346 length);
2347 per_bfd->all_comp_units.push_back (std::move (per_cu));
2348 }
2349 }
2350
2351 /* Read the CU list from the mapped index, and use it to create all
2352 the CU objects for PER_BFD. */
2353
2354 static void
2355 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2356 const gdb_byte *cu_list, offset_type cu_list_elements,
2357 const gdb_byte *dwz_list, offset_type dwz_elements)
2358 {
2359 gdb_assert (per_bfd->all_comp_units.empty ());
2360 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2361
2362 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2363 &per_bfd->info, 0);
2364
2365 if (dwz_elements == 0)
2366 return;
2367
2368 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2369 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2370 &dwz->info, 1);
2371 }
2372
2373 /* Create the signatured type hash table from the index. */
2374
2375 static void
2376 create_signatured_type_table_from_index
2377 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2378 const gdb_byte *bytes, offset_type elements)
2379 {
2380 htab_up sig_types_hash = allocate_signatured_type_table ();
2381
2382 for (offset_type i = 0; i < elements; i += 3)
2383 {
2384 signatured_type_up sig_type;
2385 ULONGEST signature;
2386 void **slot;
2387 cu_offset type_offset_in_tu;
2388
2389 gdb_static_assert (sizeof (ULONGEST) >= 8);
2390 sect_offset sect_off
2391 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2392 type_offset_in_tu
2393 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2394 BFD_ENDIAN_LITTLE);
2395 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2396 bytes += 3 * 8;
2397
2398 sig_type = per_bfd->allocate_signatured_type (signature);
2399 sig_type->type_offset_in_tu = type_offset_in_tu;
2400 sig_type->section = section;
2401 sig_type->sect_off = sect_off;
2402 sig_type->v.quick
2403 = OBSTACK_ZALLOC (&per_bfd->obstack,
2404 struct dwarf2_per_cu_quick_data);
2405
2406 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2407 *slot = sig_type.get ();
2408
2409 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2410 }
2411
2412 per_bfd->signatured_types = std::move (sig_types_hash);
2413 }
2414
2415 /* Create the signatured type hash table from .debug_names. */
2416
2417 static void
2418 create_signatured_type_table_from_debug_names
2419 (dwarf2_per_objfile *per_objfile,
2420 const mapped_debug_names &map,
2421 struct dwarf2_section_info *section,
2422 struct dwarf2_section_info *abbrev_section)
2423 {
2424 struct objfile *objfile = per_objfile->objfile;
2425
2426 section->read (objfile);
2427 abbrev_section->read (objfile);
2428
2429 htab_up sig_types_hash = allocate_signatured_type_table ();
2430
2431 for (uint32_t i = 0; i < map.tu_count; ++i)
2432 {
2433 signatured_type_up sig_type;
2434 void **slot;
2435
2436 sect_offset sect_off
2437 = (sect_offset) (extract_unsigned_integer
2438 (map.tu_table_reordered + i * map.offset_size,
2439 map.offset_size,
2440 map.dwarf5_byte_order));
2441
2442 comp_unit_head cu_header;
2443 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2444 abbrev_section,
2445 section->buffer + to_underlying (sect_off),
2446 rcuh_kind::TYPE);
2447
2448 sig_type = per_objfile->per_bfd->allocate_signatured_type
2449 (cu_header.signature);
2450 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2451 sig_type->section = section;
2452 sig_type->sect_off = sect_off;
2453 sig_type->v.quick
2454 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2455 struct dwarf2_per_cu_quick_data);
2456
2457 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2458 *slot = sig_type.get ();
2459
2460 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2461 }
2462
2463 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2464 }
2465
2466 /* Read the address map data from the mapped index, and use it to
2467 populate the psymtabs_addrmap. */
2468
2469 static void
2470 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2471 struct mapped_index *index)
2472 {
2473 struct objfile *objfile = per_objfile->objfile;
2474 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2475 struct gdbarch *gdbarch = objfile->arch ();
2476 const gdb_byte *iter, *end;
2477 struct addrmap *mutable_map;
2478 CORE_ADDR baseaddr;
2479
2480 auto_obstack temp_obstack;
2481
2482 mutable_map = addrmap_create_mutable (&temp_obstack);
2483
2484 iter = index->address_table.data ();
2485 end = iter + index->address_table.size ();
2486
2487 baseaddr = objfile->text_section_offset ();
2488
2489 while (iter < end)
2490 {
2491 ULONGEST hi, lo, cu_index;
2492 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2493 iter += 8;
2494 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2495 iter += 8;
2496 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2497 iter += 4;
2498
2499 if (lo > hi)
2500 {
2501 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2502 hex_string (lo), hex_string (hi));
2503 continue;
2504 }
2505
2506 if (cu_index >= per_bfd->all_comp_units.size ())
2507 {
2508 complaint (_(".gdb_index address table has invalid CU number %u"),
2509 (unsigned) cu_index);
2510 continue;
2511 }
2512
2513 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2514 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2515 addrmap_set_empty (mutable_map, lo, hi - 1,
2516 per_bfd->get_cu (cu_index));
2517 }
2518
2519 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2520 &per_bfd->obstack);
2521 }
2522
2523 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2524 populate the psymtabs_addrmap. */
2525
2526 static void
2527 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2528 struct dwarf2_section_info *section)
2529 {
2530 struct objfile *objfile = per_objfile->objfile;
2531 bfd *abfd = objfile->obfd;
2532 struct gdbarch *gdbarch = objfile->arch ();
2533 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2534 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2535
2536 auto_obstack temp_obstack;
2537 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2538
2539 std::unordered_map<sect_offset,
2540 dwarf2_per_cu_data *,
2541 gdb::hash_enum<sect_offset>>
2542 debug_info_offset_to_per_cu;
2543 for (const auto &per_cu : per_bfd->all_comp_units)
2544 {
2545 const auto insertpair
2546 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2547 per_cu.get ());
2548 if (!insertpair.second)
2549 {
2550 warning (_("Section .debug_aranges in %s has duplicate "
2551 "debug_info_offset %s, ignoring .debug_aranges."),
2552 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2553 return;
2554 }
2555 }
2556
2557 section->read (objfile);
2558
2559 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2560
2561 const gdb_byte *addr = section->buffer;
2562
2563 while (addr < section->buffer + section->size)
2564 {
2565 const gdb_byte *const entry_addr = addr;
2566 unsigned int bytes_read;
2567
2568 const LONGEST entry_length = read_initial_length (abfd, addr,
2569 &bytes_read);
2570 addr += bytes_read;
2571
2572 const gdb_byte *const entry_end = addr + entry_length;
2573 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2574 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2575 if (addr + entry_length > section->buffer + section->size)
2576 {
2577 warning (_("Section .debug_aranges in %s entry at offset %s "
2578 "length %s exceeds section length %s, "
2579 "ignoring .debug_aranges."),
2580 objfile_name (objfile),
2581 plongest (entry_addr - section->buffer),
2582 plongest (bytes_read + entry_length),
2583 pulongest (section->size));
2584 return;
2585 }
2586
2587 /* The version number. */
2588 const uint16_t version = read_2_bytes (abfd, addr);
2589 addr += 2;
2590 if (version != 2)
2591 {
2592 warning (_("Section .debug_aranges in %s entry at offset %s "
2593 "has unsupported version %d, ignoring .debug_aranges."),
2594 objfile_name (objfile),
2595 plongest (entry_addr - section->buffer), version);
2596 return;
2597 }
2598
2599 const uint64_t debug_info_offset
2600 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2601 addr += offset_size;
2602 const auto per_cu_it
2603 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2604 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2605 {
2606 warning (_("Section .debug_aranges in %s entry at offset %s "
2607 "debug_info_offset %s does not exists, "
2608 "ignoring .debug_aranges."),
2609 objfile_name (objfile),
2610 plongest (entry_addr - section->buffer),
2611 pulongest (debug_info_offset));
2612 return;
2613 }
2614 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2615
2616 const uint8_t address_size = *addr++;
2617 if (address_size < 1 || address_size > 8)
2618 {
2619 warning (_("Section .debug_aranges in %s entry at offset %s "
2620 "address_size %u is invalid, ignoring .debug_aranges."),
2621 objfile_name (objfile),
2622 plongest (entry_addr - section->buffer), address_size);
2623 return;
2624 }
2625
2626 const uint8_t segment_selector_size = *addr++;
2627 if (segment_selector_size != 0)
2628 {
2629 warning (_("Section .debug_aranges in %s entry at offset %s "
2630 "segment_selector_size %u is not supported, "
2631 "ignoring .debug_aranges."),
2632 objfile_name (objfile),
2633 plongest (entry_addr - section->buffer),
2634 segment_selector_size);
2635 return;
2636 }
2637
2638 /* Must pad to an alignment boundary that is twice the address
2639 size. It is undocumented by the DWARF standard but GCC does
2640 use it. However, not every compiler does this. We can see
2641 whether it has happened by looking at the total length of the
2642 contents of the aranges for this CU -- it if isn't a multiple
2643 of twice the address size, then we skip any leftover
2644 bytes. */
2645 addr += (entry_end - addr) % (2 * address_size);
2646
2647 for (;;)
2648 {
2649 if (addr + 2 * address_size > entry_end)
2650 {
2651 warning (_("Section .debug_aranges in %s entry at offset %s "
2652 "address list is not properly terminated, "
2653 "ignoring .debug_aranges."),
2654 objfile_name (objfile),
2655 plongest (entry_addr - section->buffer));
2656 return;
2657 }
2658 ULONGEST start = extract_unsigned_integer (addr, address_size,
2659 dwarf5_byte_order);
2660 addr += address_size;
2661 ULONGEST length = extract_unsigned_integer (addr, address_size,
2662 dwarf5_byte_order);
2663 addr += address_size;
2664 if (start == 0 && length == 0)
2665 break;
2666 if (start == 0 && !per_bfd->has_section_at_zero)
2667 {
2668 /* Symbol was eliminated due to a COMDAT group. */
2669 continue;
2670 }
2671 ULONGEST end = start + length;
2672 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2673 - baseaddr);
2674 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2675 - baseaddr);
2676 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2677 }
2678 }
2679
2680 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2681 &per_bfd->obstack);
2682 }
2683
2684 /* A helper function that reads the .gdb_index from BUFFER and fills
2685 in MAP. FILENAME is the name of the file containing the data;
2686 it is used for error reporting. DEPRECATED_OK is true if it is
2687 ok to use deprecated sections.
2688
2689 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2690 out parameters that are filled in with information about the CU and
2691 TU lists in the section.
2692
2693 Returns true if all went well, false otherwise. */
2694
2695 static bool
2696 read_gdb_index_from_buffer (const char *filename,
2697 bool deprecated_ok,
2698 gdb::array_view<const gdb_byte> buffer,
2699 struct mapped_index *map,
2700 const gdb_byte **cu_list,
2701 offset_type *cu_list_elements,
2702 const gdb_byte **types_list,
2703 offset_type *types_list_elements)
2704 {
2705 const gdb_byte *addr = &buffer[0];
2706 offset_view metadata (buffer);
2707
2708 /* Version check. */
2709 offset_type version = metadata[0];
2710 /* Versions earlier than 3 emitted every copy of a psymbol. This
2711 causes the index to behave very poorly for certain requests. Version 3
2712 contained incomplete addrmap. So, it seems better to just ignore such
2713 indices. */
2714 if (version < 4)
2715 {
2716 static int warning_printed = 0;
2717 if (!warning_printed)
2718 {
2719 warning (_("Skipping obsolete .gdb_index section in %s."),
2720 filename);
2721 warning_printed = 1;
2722 }
2723 return 0;
2724 }
2725 /* Index version 4 uses a different hash function than index version
2726 5 and later.
2727
2728 Versions earlier than 6 did not emit psymbols for inlined
2729 functions. Using these files will cause GDB not to be able to
2730 set breakpoints on inlined functions by name, so we ignore these
2731 indices unless the user has done
2732 "set use-deprecated-index-sections on". */
2733 if (version < 6 && !deprecated_ok)
2734 {
2735 static int warning_printed = 0;
2736 if (!warning_printed)
2737 {
2738 warning (_("\
2739 Skipping deprecated .gdb_index section in %s.\n\
2740 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2741 to use the section anyway."),
2742 filename);
2743 warning_printed = 1;
2744 }
2745 return 0;
2746 }
2747 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2748 of the TU (for symbols coming from TUs),
2749 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2750 Plus gold-generated indices can have duplicate entries for global symbols,
2751 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2752 These are just performance bugs, and we can't distinguish gdb-generated
2753 indices from gold-generated ones, so issue no warning here. */
2754
2755 /* Indexes with higher version than the one supported by GDB may be no
2756 longer backward compatible. */
2757 if (version > 8)
2758 return 0;
2759
2760 map->version = version;
2761
2762 int i = 1;
2763 *cu_list = addr + metadata[i];
2764 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2765 ++i;
2766
2767 *types_list = addr + metadata[i];
2768 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2769 ++i;
2770
2771 const gdb_byte *address_table = addr + metadata[i];
2772 const gdb_byte *address_table_end = addr + metadata[i + 1];
2773 map->address_table
2774 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2775 ++i;
2776
2777 const gdb_byte *symbol_table = addr + metadata[i];
2778 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2779 map->symbol_table
2780 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2781 symbol_table_end));
2782
2783 ++i;
2784 map->constant_pool = buffer.slice (metadata[i]);
2785
2786 return 1;
2787 }
2788
2789 /* Callback types for dwarf2_read_gdb_index. */
2790
2791 typedef gdb::function_view
2792 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2793 get_gdb_index_contents_ftype;
2794 typedef gdb::function_view
2795 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2796 get_gdb_index_contents_dwz_ftype;
2797
2798 /* Read .gdb_index. If everything went ok, initialize the "quick"
2799 elements of all the CUs and return 1. Otherwise, return 0. */
2800
2801 static int
2802 dwarf2_read_gdb_index
2803 (dwarf2_per_objfile *per_objfile,
2804 get_gdb_index_contents_ftype get_gdb_index_contents,
2805 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2806 {
2807 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2808 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2809 struct dwz_file *dwz;
2810 struct objfile *objfile = per_objfile->objfile;
2811 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2812
2813 gdb::array_view<const gdb_byte> main_index_contents
2814 = get_gdb_index_contents (objfile, per_bfd);
2815
2816 if (main_index_contents.empty ())
2817 return 0;
2818
2819 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2820 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2821 use_deprecated_index_sections,
2822 main_index_contents, map.get (), &cu_list,
2823 &cu_list_elements, &types_list,
2824 &types_list_elements))
2825 return 0;
2826
2827 /* Don't use the index if it's empty. */
2828 if (map->symbol_table.empty ())
2829 return 0;
2830
2831 /* If there is a .dwz file, read it so we can get its CU list as
2832 well. */
2833 dwz = dwarf2_get_dwz_file (per_bfd);
2834 if (dwz != NULL)
2835 {
2836 struct mapped_index dwz_map;
2837 const gdb_byte *dwz_types_ignore;
2838 offset_type dwz_types_elements_ignore;
2839
2840 gdb::array_view<const gdb_byte> dwz_index_content
2841 = get_gdb_index_contents_dwz (objfile, dwz);
2842
2843 if (dwz_index_content.empty ())
2844 return 0;
2845
2846 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2847 1, dwz_index_content, &dwz_map,
2848 &dwz_list, &dwz_list_elements,
2849 &dwz_types_ignore,
2850 &dwz_types_elements_ignore))
2851 {
2852 warning (_("could not read '.gdb_index' section from %s; skipping"),
2853 bfd_get_filename (dwz->dwz_bfd.get ()));
2854 return 0;
2855 }
2856 }
2857
2858 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2859 dwz_list_elements);
2860
2861 if (types_list_elements)
2862 {
2863 /* We can only handle a single .debug_types when we have an
2864 index. */
2865 if (per_bfd->types.size () != 1)
2866 return 0;
2867
2868 dwarf2_section_info *section = &per_bfd->types[0];
2869
2870 create_signatured_type_table_from_index (per_bfd, section, types_list,
2871 types_list_elements);
2872 }
2873
2874 create_addrmap_from_index (per_objfile, map.get ());
2875
2876 per_bfd->index_table = std::move (map);
2877 per_bfd->using_index = 1;
2878 per_bfd->quick_file_names_table =
2879 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2880
2881 return 1;
2882 }
2883
2884 /* die_reader_func for dw2_get_file_names. */
2885
2886 static void
2887 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2888 struct die_info *comp_unit_die)
2889 {
2890 struct dwarf2_cu *cu = reader->cu;
2891 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2892 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2893 struct dwarf2_per_cu_data *lh_cu;
2894 struct attribute *attr;
2895 void **slot;
2896 struct quick_file_names *qfn;
2897
2898 gdb_assert (! this_cu->is_debug_types);
2899
2900 /* Our callers never want to match partial units -- instead they
2901 will match the enclosing full CU. */
2902 if (comp_unit_die->tag == DW_TAG_partial_unit)
2903 {
2904 this_cu->v.quick->no_file_data = 1;
2905 return;
2906 }
2907
2908 lh_cu = this_cu;
2909 slot = NULL;
2910
2911 line_header_up lh;
2912 sect_offset line_offset {};
2913
2914 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2915 if (attr != nullptr && attr->form_is_unsigned ())
2916 {
2917 struct quick_file_names find_entry;
2918
2919 line_offset = (sect_offset) attr->as_unsigned ();
2920
2921 /* We may have already read in this line header (TU line header sharing).
2922 If we have we're done. */
2923 find_entry.hash.dwo_unit = cu->dwo_unit;
2924 find_entry.hash.line_sect_off = line_offset;
2925 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2926 &find_entry, INSERT);
2927 if (*slot != NULL)
2928 {
2929 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
2930 return;
2931 }
2932
2933 lh = dwarf_decode_line_header (line_offset, cu);
2934 }
2935 if (lh == NULL)
2936 {
2937 lh_cu->v.quick->no_file_data = 1;
2938 return;
2939 }
2940
2941 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2942 qfn->hash.dwo_unit = cu->dwo_unit;
2943 qfn->hash.line_sect_off = line_offset;
2944 gdb_assert (slot != NULL);
2945 *slot = qfn;
2946
2947 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
2948
2949 int offset = 0;
2950 if (strcmp (fnd.name, "<unknown>") != 0)
2951 ++offset;
2952
2953 qfn->num_file_names = offset + lh->file_names_size ();
2954 qfn->file_names =
2955 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2956 qfn->num_file_names);
2957 if (offset != 0)
2958 qfn->file_names[0] = xstrdup (fnd.name);
2959 for (int i = 0; i < lh->file_names_size (); ++i)
2960 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
2961 fnd.comp_dir).release ();
2962 qfn->real_names = NULL;
2963
2964 lh_cu->v.quick->file_names = qfn;
2965 }
2966
2967 /* A helper for the "quick" functions which attempts to read the line
2968 table for THIS_CU. */
2969
2970 static struct quick_file_names *
2971 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2972 dwarf2_per_objfile *per_objfile)
2973 {
2974 /* This should never be called for TUs. */
2975 gdb_assert (! this_cu->is_debug_types);
2976 /* Nor type unit groups. */
2977 gdb_assert (! this_cu->type_unit_group_p ());
2978
2979 if (this_cu->v.quick->file_names != NULL)
2980 return this_cu->v.quick->file_names;
2981 /* If we know there is no line data, no point in looking again. */
2982 if (this_cu->v.quick->no_file_data)
2983 return NULL;
2984
2985 cutu_reader reader (this_cu, per_objfile);
2986 if (!reader.dummy_p)
2987 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2988
2989 if (this_cu->v.quick->no_file_data)
2990 return NULL;
2991 return this_cu->v.quick->file_names;
2992 }
2993
2994 /* A helper for the "quick" functions which computes and caches the
2995 real path for a given file name from the line table. */
2996
2997 static const char *
2998 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
2999 struct quick_file_names *qfn, int index)
3000 {
3001 if (qfn->real_names == NULL)
3002 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3003 qfn->num_file_names, const char *);
3004
3005 if (qfn->real_names[index] == NULL)
3006 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3007
3008 return qfn->real_names[index];
3009 }
3010
3011 struct symtab *
3012 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3013 {
3014 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3015 dwarf2_per_cu_data *dwarf_cu
3016 = per_objfile->per_bfd->all_comp_units.back ().get ();
3017 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3018
3019 if (cust == NULL)
3020 return NULL;
3021
3022 return compunit_primary_filetab (cust);
3023 }
3024
3025 /* Traversal function for dw2_forget_cached_source_info. */
3026
3027 static int
3028 dw2_free_cached_file_names (void **slot, void *info)
3029 {
3030 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3031
3032 if (file_data->real_names)
3033 {
3034 int i;
3035
3036 for (i = 0; i < file_data->num_file_names; ++i)
3037 {
3038 xfree ((void*) file_data->real_names[i]);
3039 file_data->real_names[i] = NULL;
3040 }
3041 }
3042
3043 return 1;
3044 }
3045
3046 void
3047 dwarf2_base_index_functions::forget_cached_source_info
3048 (struct objfile *objfile)
3049 {
3050 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3051
3052 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3053 dw2_free_cached_file_names, NULL);
3054 }
3055
3056 /* Struct used to manage iterating over all CUs looking for a symbol. */
3057
3058 struct dw2_symtab_iterator
3059 {
3060 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3061 dwarf2_per_objfile *per_objfile;
3062 /* If set, only look for symbols that match that block. Valid values are
3063 GLOBAL_BLOCK and STATIC_BLOCK. */
3064 gdb::optional<block_enum> block_index;
3065 /* The kind of symbol we're looking for. */
3066 domain_enum domain;
3067 /* The list of CUs from the index entry of the symbol,
3068 or NULL if not found. */
3069 offset_view vec;
3070 /* The next element in VEC to look at. */
3071 int next;
3072 /* The number of elements in VEC, or zero if there is no match. */
3073 int length;
3074 /* Have we seen a global version of the symbol?
3075 If so we can ignore all further global instances.
3076 This is to work around gold/15646, inefficient gold-generated
3077 indices. */
3078 int global_seen;
3079 };
3080
3081 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3082
3083 static void
3084 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3085 dwarf2_per_objfile *per_objfile,
3086 gdb::optional<block_enum> block_index,
3087 domain_enum domain, offset_type namei)
3088 {
3089 iter->per_objfile = per_objfile;
3090 iter->block_index = block_index;
3091 iter->domain = domain;
3092 iter->next = 0;
3093 iter->global_seen = 0;
3094 iter->vec = {};
3095 iter->length = 0;
3096
3097 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3098 /* index is NULL if OBJF_READNOW. */
3099 if (index == NULL)
3100 return;
3101
3102 gdb_assert (!index->symbol_name_slot_invalid (namei));
3103 offset_type vec_idx = index->symbol_vec_index (namei);
3104
3105 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3106 iter->length = iter->vec[0];
3107 }
3108
3109 /* Return the next matching CU or NULL if there are no more. */
3110
3111 static struct dwarf2_per_cu_data *
3112 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3113 {
3114 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3115
3116 for ( ; iter->next < iter->length; ++iter->next)
3117 {
3118 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3119 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3120 gdb_index_symbol_kind symbol_kind =
3121 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3122 /* Only check the symbol attributes if they're present.
3123 Indices prior to version 7 don't record them,
3124 and indices >= 7 may elide them for certain symbols
3125 (gold does this). */
3126 int attrs_valid =
3127 (per_objfile->per_bfd->index_table->version >= 7
3128 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3129
3130 /* Don't crash on bad data. */
3131 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3132 {
3133 complaint (_(".gdb_index entry has bad CU index"
3134 " [in module %s]"), objfile_name (per_objfile->objfile));
3135 continue;
3136 }
3137
3138 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3139
3140 /* Skip if already read in. */
3141 if (per_objfile->symtab_set_p (per_cu))
3142 continue;
3143
3144 /* Check static vs global. */
3145 if (attrs_valid)
3146 {
3147 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3148
3149 if (iter->block_index.has_value ())
3150 {
3151 bool want_static = *iter->block_index == STATIC_BLOCK;
3152
3153 if (is_static != want_static)
3154 continue;
3155 }
3156
3157 /* Work around gold/15646. */
3158 if (!is_static
3159 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3160 {
3161 if (iter->global_seen)
3162 continue;
3163
3164 iter->global_seen = 1;
3165 }
3166 }
3167
3168 /* Only check the symbol's kind if it has one. */
3169 if (attrs_valid)
3170 {
3171 switch (iter->domain)
3172 {
3173 case VAR_DOMAIN:
3174 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3175 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3176 /* Some types are also in VAR_DOMAIN. */
3177 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3178 continue;
3179 break;
3180 case STRUCT_DOMAIN:
3181 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3182 continue;
3183 break;
3184 case LABEL_DOMAIN:
3185 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3186 continue;
3187 break;
3188 case MODULE_DOMAIN:
3189 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3190 continue;
3191 break;
3192 default:
3193 break;
3194 }
3195 }
3196
3197 ++iter->next;
3198 return per_cu;
3199 }
3200
3201 return NULL;
3202 }
3203
3204 void
3205 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3206 bool print_bcache)
3207 {
3208 if (print_bcache)
3209 return;
3210
3211 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3212 int total = per_objfile->per_bfd->all_comp_units.size ();
3213 int count = 0;
3214
3215 for (int i = 0; i < total; ++i)
3216 {
3217 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3218
3219 if (!per_objfile->symtab_set_p (per_cu))
3220 ++count;
3221 }
3222 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3223 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3224 }
3225
3226 /* This dumps minimal information about the index.
3227 It is called via "mt print objfiles".
3228 One use is to verify .gdb_index has been loaded by the
3229 gdb.dwarf2/gdb-index.exp testcase. */
3230
3231 void
3232 dwarf2_gdb_index::dump (struct objfile *objfile)
3233 {
3234 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3235
3236 gdb_assert (per_objfile->per_bfd->using_index);
3237 printf_filtered (".gdb_index:");
3238 if (per_objfile->per_bfd->index_table != NULL)
3239 {
3240 printf_filtered (" version %d\n",
3241 per_objfile->per_bfd->index_table->version);
3242 }
3243 else
3244 printf_filtered (" faked for \"readnow\"\n");
3245 printf_filtered ("\n");
3246 }
3247
3248 void
3249 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3250 {
3251 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3252 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3253
3254 for (int i = 0; i < total_units; ++i)
3255 {
3256 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3257
3258 /* We don't want to directly expand a partial CU, because if we
3259 read it with the wrong language, then assertion failures can
3260 be triggered later on. See PR symtab/23010. So, tell
3261 dw2_instantiate_symtab to skip partial CUs -- any important
3262 partial CU will be read via DW_TAG_imported_unit anyway. */
3263 dw2_instantiate_symtab (per_cu, per_objfile, true);
3264 }
3265 }
3266
3267 static bool
3268 dw2_expand_symtabs_matching_symbol
3269 (mapped_index_base &index,
3270 const lookup_name_info &lookup_name_in,
3271 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3272 gdb::function_view<bool (offset_type)> match_callback,
3273 dwarf2_per_objfile *per_objfile);
3274
3275 static bool
3276 dw2_expand_symtabs_matching_one
3277 (dwarf2_per_cu_data *per_cu,
3278 dwarf2_per_objfile *per_objfile,
3279 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3280 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3281
3282 void
3283 dwarf2_gdb_index::expand_matching_symbols
3284 (struct objfile *objfile,
3285 const lookup_name_info &name, domain_enum domain,
3286 int global,
3287 symbol_compare_ftype *ordered_compare)
3288 {
3289 /* Used for Ada. */
3290 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3291
3292 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3293
3294 if (per_objfile->per_bfd->index_table != nullptr)
3295 {
3296 mapped_index &index = *per_objfile->per_bfd->index_table;
3297
3298 const char *match_name = name.ada ().lookup_name ().c_str ();
3299 auto matcher = [&] (const char *symname)
3300 {
3301 if (ordered_compare == nullptr)
3302 return true;
3303 return ordered_compare (symname, match_name) == 0;
3304 };
3305
3306 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3307 [&] (offset_type namei)
3308 {
3309 struct dw2_symtab_iterator iter;
3310 struct dwarf2_per_cu_data *per_cu;
3311
3312 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3313 namei);
3314 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3315 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3316 nullptr);
3317 return true;
3318 }, per_objfile);
3319 }
3320 else
3321 {
3322 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3323 proceed assuming all symtabs have been read in. */
3324 }
3325 }
3326
3327 /* Starting from a search name, return the string that finds the upper
3328 bound of all strings that start with SEARCH_NAME in a sorted name
3329 list. Returns the empty string to indicate that the upper bound is
3330 the end of the list. */
3331
3332 static std::string
3333 make_sort_after_prefix_name (const char *search_name)
3334 {
3335 /* When looking to complete "func", we find the upper bound of all
3336 symbols that start with "func" by looking for where we'd insert
3337 the closest string that would follow "func" in lexicographical
3338 order. Usually, that's "func"-with-last-character-incremented,
3339 i.e. "fund". Mind non-ASCII characters, though. Usually those
3340 will be UTF-8 multi-byte sequences, but we can't be certain.
3341 Especially mind the 0xff character, which is a valid character in
3342 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3343 rule out compilers allowing it in identifiers. Note that
3344 conveniently, strcmp/strcasecmp are specified to compare
3345 characters interpreted as unsigned char. So what we do is treat
3346 the whole string as a base 256 number composed of a sequence of
3347 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3348 to 0, and carries 1 to the following more-significant position.
3349 If the very first character in SEARCH_NAME ends up incremented
3350 and carries/overflows, then the upper bound is the end of the
3351 list. The string after the empty string is also the empty
3352 string.
3353
3354 Some examples of this operation:
3355
3356 SEARCH_NAME => "+1" RESULT
3357
3358 "abc" => "abd"
3359 "ab\xff" => "ac"
3360 "\xff" "a" "\xff" => "\xff" "b"
3361 "\xff" => ""
3362 "\xff\xff" => ""
3363 "" => ""
3364
3365 Then, with these symbols for example:
3366
3367 func
3368 func1
3369 fund
3370
3371 completing "func" looks for symbols between "func" and
3372 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3373 which finds "func" and "func1", but not "fund".
3374
3375 And with:
3376
3377 funcÿ (Latin1 'ÿ' [0xff])
3378 funcÿ1
3379 fund
3380
3381 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3382 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3383
3384 And with:
3385
3386 ÿÿ (Latin1 'ÿ' [0xff])
3387 ÿÿ1
3388
3389 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3390 the end of the list.
3391 */
3392 std::string after = search_name;
3393 while (!after.empty () && (unsigned char) after.back () == 0xff)
3394 after.pop_back ();
3395 if (!after.empty ())
3396 after.back () = (unsigned char) after.back () + 1;
3397 return after;
3398 }
3399
3400 /* See declaration. */
3401
3402 std::pair<std::vector<name_component>::const_iterator,
3403 std::vector<name_component>::const_iterator>
3404 mapped_index_base::find_name_components_bounds
3405 (const lookup_name_info &lookup_name_without_params, language lang,
3406 dwarf2_per_objfile *per_objfile) const
3407 {
3408 auto *name_cmp
3409 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3410
3411 const char *lang_name
3412 = lookup_name_without_params.language_lookup_name (lang);
3413
3414 /* Comparison function object for lower_bound that matches against a
3415 given symbol name. */
3416 auto lookup_compare_lower = [&] (const name_component &elem,
3417 const char *name)
3418 {
3419 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3420 const char *elem_name = elem_qualified + elem.name_offset;
3421 return name_cmp (elem_name, name) < 0;
3422 };
3423
3424 /* Comparison function object for upper_bound that matches against a
3425 given symbol name. */
3426 auto lookup_compare_upper = [&] (const char *name,
3427 const name_component &elem)
3428 {
3429 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3430 const char *elem_name = elem_qualified + elem.name_offset;
3431 return name_cmp (name, elem_name) < 0;
3432 };
3433
3434 auto begin = this->name_components.begin ();
3435 auto end = this->name_components.end ();
3436
3437 /* Find the lower bound. */
3438 auto lower = [&] ()
3439 {
3440 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3441 return begin;
3442 else
3443 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3444 } ();
3445
3446 /* Find the upper bound. */
3447 auto upper = [&] ()
3448 {
3449 if (lookup_name_without_params.completion_mode ())
3450 {
3451 /* In completion mode, we want UPPER to point past all
3452 symbols names that have the same prefix. I.e., with
3453 these symbols, and completing "func":
3454
3455 function << lower bound
3456 function1
3457 other_function << upper bound
3458
3459 We find the upper bound by looking for the insertion
3460 point of "func"-with-last-character-incremented,
3461 i.e. "fund". */
3462 std::string after = make_sort_after_prefix_name (lang_name);
3463 if (after.empty ())
3464 return end;
3465 return std::lower_bound (lower, end, after.c_str (),
3466 lookup_compare_lower);
3467 }
3468 else
3469 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3470 } ();
3471
3472 return {lower, upper};
3473 }
3474
3475 /* See declaration. */
3476
3477 void
3478 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3479 {
3480 if (!this->name_components.empty ())
3481 return;
3482
3483 this->name_components_casing = case_sensitivity;
3484 auto *name_cmp
3485 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3486
3487 /* The code below only knows how to break apart components of C++
3488 symbol names (and other languages that use '::' as
3489 namespace/module separator) and Ada symbol names. */
3490 auto count = this->symbol_name_count ();
3491 for (offset_type idx = 0; idx < count; idx++)
3492 {
3493 if (this->symbol_name_slot_invalid (idx))
3494 continue;
3495
3496 const char *name = this->symbol_name_at (idx, per_objfile);
3497
3498 /* Add each name component to the name component table. */
3499 unsigned int previous_len = 0;
3500
3501 if (strstr (name, "::") != nullptr)
3502 {
3503 for (unsigned int current_len = cp_find_first_component (name);
3504 name[current_len] != '\0';
3505 current_len += cp_find_first_component (name + current_len))
3506 {
3507 gdb_assert (name[current_len] == ':');
3508 this->name_components.push_back ({previous_len, idx});
3509 /* Skip the '::'. */
3510 current_len += 2;
3511 previous_len = current_len;
3512 }
3513 }
3514 else
3515 {
3516 /* Handle the Ada encoded (aka mangled) form here. */
3517 for (const char *iter = strstr (name, "__");
3518 iter != nullptr;
3519 iter = strstr (iter, "__"))
3520 {
3521 this->name_components.push_back ({previous_len, idx});
3522 iter += 2;
3523 previous_len = iter - name;
3524 }
3525 }
3526
3527 this->name_components.push_back ({previous_len, idx});
3528 }
3529
3530 /* Sort name_components elements by name. */
3531 auto name_comp_compare = [&] (const name_component &left,
3532 const name_component &right)
3533 {
3534 const char *left_qualified
3535 = this->symbol_name_at (left.idx, per_objfile);
3536 const char *right_qualified
3537 = this->symbol_name_at (right.idx, per_objfile);
3538
3539 const char *left_name = left_qualified + left.name_offset;
3540 const char *right_name = right_qualified + right.name_offset;
3541
3542 return name_cmp (left_name, right_name) < 0;
3543 };
3544
3545 std::sort (this->name_components.begin (),
3546 this->name_components.end (),
3547 name_comp_compare);
3548 }
3549
3550 /* Helper for dw2_expand_symtabs_matching that works with a
3551 mapped_index_base instead of the containing objfile. This is split
3552 to a separate function in order to be able to unit test the
3553 name_components matching using a mock mapped_index_base. For each
3554 symbol name that matches, calls MATCH_CALLBACK, passing it the
3555 symbol's index in the mapped_index_base symbol table. */
3556
3557 static bool
3558 dw2_expand_symtabs_matching_symbol
3559 (mapped_index_base &index,
3560 const lookup_name_info &lookup_name_in,
3561 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3562 gdb::function_view<bool (offset_type)> match_callback,
3563 dwarf2_per_objfile *per_objfile)
3564 {
3565 lookup_name_info lookup_name_without_params
3566 = lookup_name_in.make_ignore_params ();
3567
3568 /* Build the symbol name component sorted vector, if we haven't
3569 yet. */
3570 index.build_name_components (per_objfile);
3571
3572 /* The same symbol may appear more than once in the range though.
3573 E.g., if we're looking for symbols that complete "w", and we have
3574 a symbol named "w1::w2", we'll find the two name components for
3575 that same symbol in the range. To be sure we only call the
3576 callback once per symbol, we first collect the symbol name
3577 indexes that matched in a temporary vector and ignore
3578 duplicates. */
3579 std::vector<offset_type> matches;
3580
3581 struct name_and_matcher
3582 {
3583 symbol_name_matcher_ftype *matcher;
3584 const char *name;
3585
3586 bool operator== (const name_and_matcher &other) const
3587 {
3588 return matcher == other.matcher && strcmp (name, other.name) == 0;
3589 }
3590 };
3591
3592 /* A vector holding all the different symbol name matchers, for all
3593 languages. */
3594 std::vector<name_and_matcher> matchers;
3595
3596 for (int i = 0; i < nr_languages; i++)
3597 {
3598 enum language lang_e = (enum language) i;
3599
3600 const language_defn *lang = language_def (lang_e);
3601 symbol_name_matcher_ftype *name_matcher
3602 = lang->get_symbol_name_matcher (lookup_name_without_params);
3603
3604 name_and_matcher key {
3605 name_matcher,
3606 lookup_name_without_params.language_lookup_name (lang_e)
3607 };
3608
3609 /* Don't insert the same comparison routine more than once.
3610 Note that we do this linear walk. This is not a problem in
3611 practice because the number of supported languages is
3612 low. */
3613 if (std::find (matchers.begin (), matchers.end (), key)
3614 != matchers.end ())
3615 continue;
3616 matchers.push_back (std::move (key));
3617
3618 auto bounds
3619 = index.find_name_components_bounds (lookup_name_without_params,
3620 lang_e, per_objfile);
3621
3622 /* Now for each symbol name in range, check to see if we have a name
3623 match, and if so, call the MATCH_CALLBACK callback. */
3624
3625 for (; bounds.first != bounds.second; ++bounds.first)
3626 {
3627 const char *qualified
3628 = index.symbol_name_at (bounds.first->idx, per_objfile);
3629
3630 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3631 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3632 continue;
3633
3634 matches.push_back (bounds.first->idx);
3635 }
3636 }
3637
3638 std::sort (matches.begin (), matches.end ());
3639
3640 /* Finally call the callback, once per match. */
3641 ULONGEST prev = -1;
3642 bool result = true;
3643 for (offset_type idx : matches)
3644 {
3645 if (prev != idx)
3646 {
3647 if (!match_callback (idx))
3648 {
3649 result = false;
3650 break;
3651 }
3652 prev = idx;
3653 }
3654 }
3655
3656 /* Above we use a type wider than idx's for 'prev', since 0 and
3657 (offset_type)-1 are both possible values. */
3658 static_assert (sizeof (prev) > sizeof (offset_type), "");
3659
3660 return result;
3661 }
3662
3663 #if GDB_SELF_TEST
3664
3665 namespace selftests { namespace dw2_expand_symtabs_matching {
3666
3667 /* A mock .gdb_index/.debug_names-like name index table, enough to
3668 exercise dw2_expand_symtabs_matching_symbol, which works with the
3669 mapped_index_base interface. Builds an index from the symbol list
3670 passed as parameter to the constructor. */
3671 class mock_mapped_index : public mapped_index_base
3672 {
3673 public:
3674 mock_mapped_index (gdb::array_view<const char *> symbols)
3675 : m_symbol_table (symbols)
3676 {}
3677
3678 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3679
3680 /* Return the number of names in the symbol table. */
3681 size_t symbol_name_count () const override
3682 {
3683 return m_symbol_table.size ();
3684 }
3685
3686 /* Get the name of the symbol at IDX in the symbol table. */
3687 const char *symbol_name_at
3688 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3689 {
3690 return m_symbol_table[idx];
3691 }
3692
3693 private:
3694 gdb::array_view<const char *> m_symbol_table;
3695 };
3696
3697 /* Convenience function that converts a NULL pointer to a "<null>"
3698 string, to pass to print routines. */
3699
3700 static const char *
3701 string_or_null (const char *str)
3702 {
3703 return str != NULL ? str : "<null>";
3704 }
3705
3706 /* Check if a lookup_name_info built from
3707 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3708 index. EXPECTED_LIST is the list of expected matches, in expected
3709 matching order. If no match expected, then an empty list is
3710 specified. Returns true on success. On failure prints a warning
3711 indicating the file:line that failed, and returns false. */
3712
3713 static bool
3714 check_match (const char *file, int line,
3715 mock_mapped_index &mock_index,
3716 const char *name, symbol_name_match_type match_type,
3717 bool completion_mode,
3718 std::initializer_list<const char *> expected_list,
3719 dwarf2_per_objfile *per_objfile)
3720 {
3721 lookup_name_info lookup_name (name, match_type, completion_mode);
3722
3723 bool matched = true;
3724
3725 auto mismatch = [&] (const char *expected_str,
3726 const char *got)
3727 {
3728 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3729 "expected=\"%s\", got=\"%s\"\n"),
3730 file, line,
3731 (match_type == symbol_name_match_type::FULL
3732 ? "FULL" : "WILD"),
3733 name, string_or_null (expected_str), string_or_null (got));
3734 matched = false;
3735 };
3736
3737 auto expected_it = expected_list.begin ();
3738 auto expected_end = expected_list.end ();
3739
3740 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3741 nullptr,
3742 [&] (offset_type idx)
3743 {
3744 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3745 const char *expected_str
3746 = expected_it == expected_end ? NULL : *expected_it++;
3747
3748 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3749 mismatch (expected_str, matched_name);
3750 return true;
3751 }, per_objfile);
3752
3753 const char *expected_str
3754 = expected_it == expected_end ? NULL : *expected_it++;
3755 if (expected_str != NULL)
3756 mismatch (expected_str, NULL);
3757
3758 return matched;
3759 }
3760
3761 /* The symbols added to the mock mapped_index for testing (in
3762 canonical form). */
3763 static const char *test_symbols[] = {
3764 "function",
3765 "std::bar",
3766 "std::zfunction",
3767 "std::zfunction2",
3768 "w1::w2",
3769 "ns::foo<char*>",
3770 "ns::foo<int>",
3771 "ns::foo<long>",
3772 "ns2::tmpl<int>::foo2",
3773 "(anonymous namespace)::A::B::C",
3774
3775 /* These are used to check that the increment-last-char in the
3776 matching algorithm for completion doesn't match "t1_fund" when
3777 completing "t1_func". */
3778 "t1_func",
3779 "t1_func1",
3780 "t1_fund",
3781 "t1_fund1",
3782
3783 /* A UTF-8 name with multi-byte sequences to make sure that
3784 cp-name-parser understands this as a single identifier ("função"
3785 is "function" in PT). */
3786 u8"u8função",
3787
3788 /* \377 (0xff) is Latin1 'ÿ'. */
3789 "yfunc\377",
3790
3791 /* \377 (0xff) is Latin1 'ÿ'. */
3792 "\377",
3793 "\377\377123",
3794
3795 /* A name with all sorts of complications. Starts with "z" to make
3796 it easier for the completion tests below. */
3797 #define Z_SYM_NAME \
3798 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3799 "::tuple<(anonymous namespace)::ui*, " \
3800 "std::default_delete<(anonymous namespace)::ui>, void>"
3801
3802 Z_SYM_NAME
3803 };
3804
3805 /* Returns true if the mapped_index_base::find_name_component_bounds
3806 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3807 in completion mode. */
3808
3809 static bool
3810 check_find_bounds_finds (mapped_index_base &index,
3811 const char *search_name,
3812 gdb::array_view<const char *> expected_syms,
3813 dwarf2_per_objfile *per_objfile)
3814 {
3815 lookup_name_info lookup_name (search_name,
3816 symbol_name_match_type::FULL, true);
3817
3818 auto bounds = index.find_name_components_bounds (lookup_name,
3819 language_cplus,
3820 per_objfile);
3821
3822 size_t distance = std::distance (bounds.first, bounds.second);
3823 if (distance != expected_syms.size ())
3824 return false;
3825
3826 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3827 {
3828 auto nc_elem = bounds.first + exp_elem;
3829 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3830 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3831 return false;
3832 }
3833
3834 return true;
3835 }
3836
3837 /* Test the lower-level mapped_index::find_name_component_bounds
3838 method. */
3839
3840 static void
3841 test_mapped_index_find_name_component_bounds ()
3842 {
3843 mock_mapped_index mock_index (test_symbols);
3844
3845 mock_index.build_name_components (NULL /* per_objfile */);
3846
3847 /* Test the lower-level mapped_index::find_name_component_bounds
3848 method in completion mode. */
3849 {
3850 static const char *expected_syms[] = {
3851 "t1_func",
3852 "t1_func1",
3853 };
3854
3855 SELF_CHECK (check_find_bounds_finds
3856 (mock_index, "t1_func", expected_syms,
3857 NULL /* per_objfile */));
3858 }
3859
3860 /* Check that the increment-last-char in the name matching algorithm
3861 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3862 {
3863 static const char *expected_syms1[] = {
3864 "\377",
3865 "\377\377123",
3866 };
3867 SELF_CHECK (check_find_bounds_finds
3868 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3869
3870 static const char *expected_syms2[] = {
3871 "\377\377123",
3872 };
3873 SELF_CHECK (check_find_bounds_finds
3874 (mock_index, "\377\377", expected_syms2,
3875 NULL /* per_objfile */));
3876 }
3877 }
3878
3879 /* Test dw2_expand_symtabs_matching_symbol. */
3880
3881 static void
3882 test_dw2_expand_symtabs_matching_symbol ()
3883 {
3884 mock_mapped_index mock_index (test_symbols);
3885
3886 /* We let all tests run until the end even if some fails, for debug
3887 convenience. */
3888 bool any_mismatch = false;
3889
3890 /* Create the expected symbols list (an initializer_list). Needed
3891 because lists have commas, and we need to pass them to CHECK,
3892 which is a macro. */
3893 #define EXPECT(...) { __VA_ARGS__ }
3894
3895 /* Wrapper for check_match that passes down the current
3896 __FILE__/__LINE__. */
3897 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3898 any_mismatch |= !check_match (__FILE__, __LINE__, \
3899 mock_index, \
3900 NAME, MATCH_TYPE, COMPLETION_MODE, \
3901 EXPECTED_LIST, NULL)
3902
3903 /* Identity checks. */
3904 for (const char *sym : test_symbols)
3905 {
3906 /* Should be able to match all existing symbols. */
3907 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3908 EXPECT (sym));
3909
3910 /* Should be able to match all existing symbols with
3911 parameters. */
3912 std::string with_params = std::string (sym) + "(int)";
3913 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3914 EXPECT (sym));
3915
3916 /* Should be able to match all existing symbols with
3917 parameters and qualifiers. */
3918 with_params = std::string (sym) + " ( int ) const";
3919 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3920 EXPECT (sym));
3921
3922 /* This should really find sym, but cp-name-parser.y doesn't
3923 know about lvalue/rvalue qualifiers yet. */
3924 with_params = std::string (sym) + " ( int ) &&";
3925 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3926 {});
3927 }
3928
3929 /* Check that the name matching algorithm for completion doesn't get
3930 confused with Latin1 'ÿ' / 0xff. */
3931 {
3932 static const char str[] = "\377";
3933 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3934 EXPECT ("\377", "\377\377123"));
3935 }
3936
3937 /* Check that the increment-last-char in the matching algorithm for
3938 completion doesn't match "t1_fund" when completing "t1_func". */
3939 {
3940 static const char str[] = "t1_func";
3941 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3942 EXPECT ("t1_func", "t1_func1"));
3943 }
3944
3945 /* Check that completion mode works at each prefix of the expected
3946 symbol name. */
3947 {
3948 static const char str[] = "function(int)";
3949 size_t len = strlen (str);
3950 std::string lookup;
3951
3952 for (size_t i = 1; i < len; i++)
3953 {
3954 lookup.assign (str, i);
3955 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3956 EXPECT ("function"));
3957 }
3958 }
3959
3960 /* While "w" is a prefix of both components, the match function
3961 should still only be called once. */
3962 {
3963 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3964 EXPECT ("w1::w2"));
3965 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3966 EXPECT ("w1::w2"));
3967 }
3968
3969 /* Same, with a "complicated" symbol. */
3970 {
3971 static const char str[] = Z_SYM_NAME;
3972 size_t len = strlen (str);
3973 std::string lookup;
3974
3975 for (size_t i = 1; i < len; i++)
3976 {
3977 lookup.assign (str, i);
3978 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3979 EXPECT (Z_SYM_NAME));
3980 }
3981 }
3982
3983 /* In FULL mode, an incomplete symbol doesn't match. */
3984 {
3985 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3986 {});
3987 }
3988
3989 /* A complete symbol with parameters matches any overload, since the
3990 index has no overload info. */
3991 {
3992 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
3993 EXPECT ("std::zfunction", "std::zfunction2"));
3994 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
3995 EXPECT ("std::zfunction", "std::zfunction2"));
3996 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
3997 EXPECT ("std::zfunction", "std::zfunction2"));
3998 }
3999
4000 /* Check that whitespace is ignored appropriately. A symbol with a
4001 template argument list. */
4002 {
4003 static const char expected[] = "ns::foo<int>";
4004 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4005 EXPECT (expected));
4006 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4007 EXPECT (expected));
4008 }
4009
4010 /* Check that whitespace is ignored appropriately. A symbol with a
4011 template argument list that includes a pointer. */
4012 {
4013 static const char expected[] = "ns::foo<char*>";
4014 /* Try both completion and non-completion modes. */
4015 static const bool completion_mode[2] = {false, true};
4016 for (size_t i = 0; i < 2; i++)
4017 {
4018 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4019 completion_mode[i], EXPECT (expected));
4020 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4021 completion_mode[i], EXPECT (expected));
4022
4023 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4024 completion_mode[i], EXPECT (expected));
4025 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4026 completion_mode[i], EXPECT (expected));
4027 }
4028 }
4029
4030 {
4031 /* Check method qualifiers are ignored. */
4032 static const char expected[] = "ns::foo<char*>";
4033 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4034 symbol_name_match_type::FULL, true, EXPECT (expected));
4035 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4036 symbol_name_match_type::FULL, true, EXPECT (expected));
4037 CHECK_MATCH ("foo < char * > ( int ) const",
4038 symbol_name_match_type::WILD, true, EXPECT (expected));
4039 CHECK_MATCH ("foo < char * > ( int ) &&",
4040 symbol_name_match_type::WILD, true, EXPECT (expected));
4041 }
4042
4043 /* Test lookup names that don't match anything. */
4044 {
4045 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4046 {});
4047
4048 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4049 {});
4050 }
4051
4052 /* Some wild matching tests, exercising "(anonymous namespace)",
4053 which should not be confused with a parameter list. */
4054 {
4055 static const char *syms[] = {
4056 "A::B::C",
4057 "B::C",
4058 "C",
4059 "A :: B :: C ( int )",
4060 "B :: C ( int )",
4061 "C ( int )",
4062 };
4063
4064 for (const char *s : syms)
4065 {
4066 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4067 EXPECT ("(anonymous namespace)::A::B::C"));
4068 }
4069 }
4070
4071 {
4072 static const char expected[] = "ns2::tmpl<int>::foo2";
4073 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4074 EXPECT (expected));
4075 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4076 EXPECT (expected));
4077 }
4078
4079 SELF_CHECK (!any_mismatch);
4080
4081 #undef EXPECT
4082 #undef CHECK_MATCH
4083 }
4084
4085 static void
4086 run_test ()
4087 {
4088 test_mapped_index_find_name_component_bounds ();
4089 test_dw2_expand_symtabs_matching_symbol ();
4090 }
4091
4092 }} // namespace selftests::dw2_expand_symtabs_matching
4093
4094 #endif /* GDB_SELF_TEST */
4095
4096 /* If FILE_MATCHER is NULL or if PER_CU has
4097 dwarf2_per_cu_quick_data::MARK set (see
4098 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4099 EXPANSION_NOTIFY on it. */
4100
4101 static bool
4102 dw2_expand_symtabs_matching_one
4103 (dwarf2_per_cu_data *per_cu,
4104 dwarf2_per_objfile *per_objfile,
4105 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4106 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4107 {
4108 if (file_matcher == NULL || per_cu->v.quick->mark)
4109 {
4110 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4111
4112 compunit_symtab *symtab
4113 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4114 gdb_assert (symtab != nullptr);
4115
4116 if (expansion_notify != NULL && symtab_was_null)
4117 return expansion_notify (symtab);
4118 }
4119 return true;
4120 }
4121
4122 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4123 matched, to expand corresponding CUs that were marked. IDX is the
4124 index of the symbol name that matched. */
4125
4126 static bool
4127 dw2_expand_marked_cus
4128 (dwarf2_per_objfile *per_objfile, offset_type idx,
4129 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4130 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4131 block_search_flags search_flags,
4132 search_domain kind)
4133 {
4134 offset_type vec_len, vec_idx;
4135 bool global_seen = false;
4136 mapped_index &index = *per_objfile->per_bfd->index_table;
4137
4138 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4139 vec_len = vec[0];
4140 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4141 {
4142 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4143 /* This value is only valid for index versions >= 7. */
4144 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4145 gdb_index_symbol_kind symbol_kind =
4146 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4147 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4148 /* Only check the symbol attributes if they're present.
4149 Indices prior to version 7 don't record them,
4150 and indices >= 7 may elide them for certain symbols
4151 (gold does this). */
4152 int attrs_valid =
4153 (index.version >= 7
4154 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4155
4156 /* Work around gold/15646. */
4157 if (attrs_valid
4158 && !is_static
4159 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4160 {
4161 if (global_seen)
4162 continue;
4163
4164 global_seen = true;
4165 }
4166
4167 /* Only check the symbol's kind if it has one. */
4168 if (attrs_valid)
4169 {
4170 if (is_static)
4171 {
4172 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4173 continue;
4174 }
4175 else
4176 {
4177 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4178 continue;
4179 }
4180
4181 switch (kind)
4182 {
4183 case VARIABLES_DOMAIN:
4184 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4185 continue;
4186 break;
4187 case FUNCTIONS_DOMAIN:
4188 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4189 continue;
4190 break;
4191 case TYPES_DOMAIN:
4192 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4193 continue;
4194 break;
4195 case MODULES_DOMAIN:
4196 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4197 continue;
4198 break;
4199 default:
4200 break;
4201 }
4202 }
4203
4204 /* Don't crash on bad data. */
4205 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4206 {
4207 complaint (_(".gdb_index entry has bad CU index"
4208 " [in module %s]"), objfile_name (per_objfile->objfile));
4209 continue;
4210 }
4211
4212 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4213 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4214 expansion_notify))
4215 return false;
4216 }
4217
4218 return true;
4219 }
4220
4221 /* If FILE_MATCHER is non-NULL, set all the
4222 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4223 that match FILE_MATCHER. */
4224
4225 static void
4226 dw_expand_symtabs_matching_file_matcher
4227 (dwarf2_per_objfile *per_objfile,
4228 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4229 {
4230 if (file_matcher == NULL)
4231 return;
4232
4233 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4234 htab_eq_pointer,
4235 NULL, xcalloc, xfree));
4236 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4237 htab_eq_pointer,
4238 NULL, xcalloc, xfree));
4239
4240 /* The rule is CUs specify all the files, including those used by
4241 any TU, so there's no need to scan TUs here. */
4242
4243 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4244 {
4245 QUIT;
4246
4247 if (per_cu->is_debug_types)
4248 continue;
4249 per_cu->v.quick->mark = 0;
4250
4251 /* We only need to look at symtabs not already expanded. */
4252 if (per_objfile->symtab_set_p (per_cu.get ()))
4253 continue;
4254
4255 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4256 per_objfile);
4257 if (file_data == NULL)
4258 continue;
4259
4260 if (htab_find (visited_not_found.get (), file_data) != NULL)
4261 continue;
4262 else if (htab_find (visited_found.get (), file_data) != NULL)
4263 {
4264 per_cu->v.quick->mark = 1;
4265 continue;
4266 }
4267
4268 for (int j = 0; j < file_data->num_file_names; ++j)
4269 {
4270 const char *this_real_name;
4271
4272 if (file_matcher (file_data->file_names[j], false))
4273 {
4274 per_cu->v.quick->mark = 1;
4275 break;
4276 }
4277
4278 /* Before we invoke realpath, which can get expensive when many
4279 files are involved, do a quick comparison of the basenames. */
4280 if (!basenames_may_differ
4281 && !file_matcher (lbasename (file_data->file_names[j]),
4282 true))
4283 continue;
4284
4285 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4286 if (file_matcher (this_real_name, false))
4287 {
4288 per_cu->v.quick->mark = 1;
4289 break;
4290 }
4291 }
4292
4293 void **slot = htab_find_slot (per_cu->v.quick->mark
4294 ? visited_found.get ()
4295 : visited_not_found.get (),
4296 file_data, INSERT);
4297 *slot = file_data;
4298 }
4299 }
4300
4301 bool
4302 dwarf2_gdb_index::expand_symtabs_matching
4303 (struct objfile *objfile,
4304 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4305 const lookup_name_info *lookup_name,
4306 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4307 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4308 block_search_flags search_flags,
4309 domain_enum domain,
4310 enum search_domain kind)
4311 {
4312 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4313
4314 /* index_table is NULL if OBJF_READNOW. */
4315 if (!per_objfile->per_bfd->index_table)
4316 return true;
4317
4318 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4319
4320 if (symbol_matcher == NULL && lookup_name == NULL)
4321 {
4322 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4323 {
4324 QUIT;
4325
4326 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
4327 file_matcher,
4328 expansion_notify))
4329 return false;
4330 }
4331 return true;
4332 }
4333
4334 mapped_index &index = *per_objfile->per_bfd->index_table;
4335
4336 bool result
4337 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4338 symbol_matcher,
4339 [&] (offset_type idx)
4340 {
4341 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4342 expansion_notify, search_flags, kind))
4343 return false;
4344 return true;
4345 }, per_objfile);
4346
4347 return result;
4348 }
4349
4350 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4351 symtab. */
4352
4353 static struct compunit_symtab *
4354 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4355 CORE_ADDR pc)
4356 {
4357 int i;
4358
4359 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4360 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4361 return cust;
4362
4363 if (cust->includes == NULL)
4364 return NULL;
4365
4366 for (i = 0; cust->includes[i]; ++i)
4367 {
4368 struct compunit_symtab *s = cust->includes[i];
4369
4370 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4371 if (s != NULL)
4372 return s;
4373 }
4374
4375 return NULL;
4376 }
4377
4378 struct compunit_symtab *
4379 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4380 (struct objfile *objfile,
4381 struct bound_minimal_symbol msymbol,
4382 CORE_ADDR pc,
4383 struct obj_section *section,
4384 int warn_if_readin)
4385 {
4386 struct dwarf2_per_cu_data *data;
4387 struct compunit_symtab *result;
4388
4389 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4390 if (per_objfile->per_bfd->index_addrmap == nullptr)
4391 return NULL;
4392
4393 CORE_ADDR baseaddr = objfile->text_section_offset ();
4394 data = ((struct dwarf2_per_cu_data *)
4395 addrmap_find (per_objfile->per_bfd->index_addrmap,
4396 pc - baseaddr));
4397 if (!data)
4398 return NULL;
4399
4400 if (warn_if_readin && per_objfile->symtab_set_p (data))
4401 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4402 paddress (objfile->arch (), pc));
4403
4404 result = recursively_find_pc_sect_compunit_symtab
4405 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4406
4407 gdb_assert (result != NULL);
4408 return result;
4409 }
4410
4411 void
4412 dwarf2_base_index_functions::map_symbol_filenames
4413 (struct objfile *objfile,
4414 gdb::function_view<symbol_filename_ftype> fun,
4415 bool need_fullname)
4416 {
4417 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4418
4419 /* Use caches to ensure we only call FUN once for each filename. */
4420 filename_seen_cache filenames_cache;
4421 std::unordered_set<quick_file_names *> qfn_cache;
4422
4423 /* The rule is CUs specify all the files, including those used by any TU,
4424 so there's no need to scan TUs here. We can ignore file names coming
4425 from already-expanded CUs. It is possible that an expanded CU might
4426 reuse the file names data from a currently unexpanded CU, in this
4427 case we don't want to report the files from the unexpanded CU. */
4428
4429 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4430 {
4431 if (per_objfile->symtab_set_p (per_cu.get ()))
4432 {
4433 if (per_cu->v.quick->file_names != nullptr)
4434 qfn_cache.insert (per_cu->v.quick->file_names);
4435 }
4436 }
4437
4438 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4439 {
4440 /* We only need to look at symtabs not already expanded. */
4441 if (per_objfile->symtab_set_p (per_cu.get ()))
4442 continue;
4443
4444 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4445 per_objfile);
4446 if (file_data == nullptr
4447 || qfn_cache.find (file_data) != qfn_cache.end ())
4448 continue;
4449
4450 for (int j = 0; j < file_data->num_file_names; ++j)
4451 {
4452 const char *filename = file_data->file_names[j];
4453 filenames_cache.seen (filename);
4454 }
4455 }
4456
4457 filenames_cache.traverse ([&] (const char *filename)
4458 {
4459 gdb::unique_xmalloc_ptr<char> this_real_name;
4460
4461 if (need_fullname)
4462 this_real_name = gdb_realpath (filename);
4463 fun (filename, this_real_name.get ());
4464 });
4465 }
4466
4467 bool
4468 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4469 {
4470 return true;
4471 }
4472
4473 /* DWARF-5 debug_names reader. */
4474
4475 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4476 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4477
4478 /* A helper function that reads the .debug_names section in SECTION
4479 and fills in MAP. FILENAME is the name of the file containing the
4480 section; it is used for error reporting.
4481
4482 Returns true if all went well, false otherwise. */
4483
4484 static bool
4485 read_debug_names_from_section (struct objfile *objfile,
4486 const char *filename,
4487 struct dwarf2_section_info *section,
4488 mapped_debug_names &map)
4489 {
4490 if (section->empty ())
4491 return false;
4492
4493 /* Older elfutils strip versions could keep the section in the main
4494 executable while splitting it for the separate debug info file. */
4495 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4496 return false;
4497
4498 section->read (objfile);
4499
4500 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4501
4502 const gdb_byte *addr = section->buffer;
4503
4504 bfd *const abfd = section->get_bfd_owner ();
4505
4506 unsigned int bytes_read;
4507 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4508 addr += bytes_read;
4509
4510 map.dwarf5_is_dwarf64 = bytes_read != 4;
4511 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4512 if (bytes_read + length != section->size)
4513 {
4514 /* There may be multiple per-CU indices. */
4515 warning (_("Section .debug_names in %s length %s does not match "
4516 "section length %s, ignoring .debug_names."),
4517 filename, plongest (bytes_read + length),
4518 pulongest (section->size));
4519 return false;
4520 }
4521
4522 /* The version number. */
4523 uint16_t version = read_2_bytes (abfd, addr);
4524 addr += 2;
4525 if (version != 5)
4526 {
4527 warning (_("Section .debug_names in %s has unsupported version %d, "
4528 "ignoring .debug_names."),
4529 filename, version);
4530 return false;
4531 }
4532
4533 /* Padding. */
4534 uint16_t padding = read_2_bytes (abfd, addr);
4535 addr += 2;
4536 if (padding != 0)
4537 {
4538 warning (_("Section .debug_names in %s has unsupported padding %d, "
4539 "ignoring .debug_names."),
4540 filename, padding);
4541 return false;
4542 }
4543
4544 /* comp_unit_count - The number of CUs in the CU list. */
4545 map.cu_count = read_4_bytes (abfd, addr);
4546 addr += 4;
4547
4548 /* local_type_unit_count - The number of TUs in the local TU
4549 list. */
4550 map.tu_count = read_4_bytes (abfd, addr);
4551 addr += 4;
4552
4553 /* foreign_type_unit_count - The number of TUs in the foreign TU
4554 list. */
4555 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4556 addr += 4;
4557 if (foreign_tu_count != 0)
4558 {
4559 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4560 "ignoring .debug_names."),
4561 filename, static_cast<unsigned long> (foreign_tu_count));
4562 return false;
4563 }
4564
4565 /* bucket_count - The number of hash buckets in the hash lookup
4566 table. */
4567 map.bucket_count = read_4_bytes (abfd, addr);
4568 addr += 4;
4569
4570 /* name_count - The number of unique names in the index. */
4571 map.name_count = read_4_bytes (abfd, addr);
4572 addr += 4;
4573
4574 /* abbrev_table_size - The size in bytes of the abbreviations
4575 table. */
4576 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4577 addr += 4;
4578
4579 /* augmentation_string_size - The size in bytes of the augmentation
4580 string. This value is rounded up to a multiple of 4. */
4581 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4582 addr += 4;
4583 map.augmentation_is_gdb = ((augmentation_string_size
4584 == sizeof (dwarf5_augmentation))
4585 && memcmp (addr, dwarf5_augmentation,
4586 sizeof (dwarf5_augmentation)) == 0);
4587 augmentation_string_size += (-augmentation_string_size) & 3;
4588 addr += augmentation_string_size;
4589
4590 /* List of CUs */
4591 map.cu_table_reordered = addr;
4592 addr += map.cu_count * map.offset_size;
4593
4594 /* List of Local TUs */
4595 map.tu_table_reordered = addr;
4596 addr += map.tu_count * map.offset_size;
4597
4598 /* Hash Lookup Table */
4599 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4600 addr += map.bucket_count * 4;
4601 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4602 addr += map.name_count * 4;
4603
4604 /* Name Table */
4605 map.name_table_string_offs_reordered = addr;
4606 addr += map.name_count * map.offset_size;
4607 map.name_table_entry_offs_reordered = addr;
4608 addr += map.name_count * map.offset_size;
4609
4610 const gdb_byte *abbrev_table_start = addr;
4611 for (;;)
4612 {
4613 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4614 addr += bytes_read;
4615 if (index_num == 0)
4616 break;
4617
4618 const auto insertpair
4619 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4620 if (!insertpair.second)
4621 {
4622 warning (_("Section .debug_names in %s has duplicate index %s, "
4623 "ignoring .debug_names."),
4624 filename, pulongest (index_num));
4625 return false;
4626 }
4627 mapped_debug_names::index_val &indexval = insertpair.first->second;
4628 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4629 addr += bytes_read;
4630
4631 for (;;)
4632 {
4633 mapped_debug_names::index_val::attr attr;
4634 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4635 addr += bytes_read;
4636 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4637 addr += bytes_read;
4638 if (attr.form == DW_FORM_implicit_const)
4639 {
4640 attr.implicit_const = read_signed_leb128 (abfd, addr,
4641 &bytes_read);
4642 addr += bytes_read;
4643 }
4644 if (attr.dw_idx == 0 && attr.form == 0)
4645 break;
4646 indexval.attr_vec.push_back (std::move (attr));
4647 }
4648 }
4649 if (addr != abbrev_table_start + abbrev_table_size)
4650 {
4651 warning (_("Section .debug_names in %s has abbreviation_table "
4652 "of size %s vs. written as %u, ignoring .debug_names."),
4653 filename, plongest (addr - abbrev_table_start),
4654 abbrev_table_size);
4655 return false;
4656 }
4657 map.entry_pool = addr;
4658
4659 return true;
4660 }
4661
4662 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4663 list. */
4664
4665 static void
4666 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4667 const mapped_debug_names &map,
4668 dwarf2_section_info &section,
4669 bool is_dwz)
4670 {
4671 if (!map.augmentation_is_gdb)
4672 {
4673 for (uint32_t i = 0; i < map.cu_count; ++i)
4674 {
4675 sect_offset sect_off
4676 = (sect_offset) (extract_unsigned_integer
4677 (map.cu_table_reordered + i * map.offset_size,
4678 map.offset_size,
4679 map.dwarf5_byte_order));
4680 /* We don't know the length of the CU, because the CU list in a
4681 .debug_names index can be incomplete, so we can't use the start
4682 of the next CU as end of this CU. We create the CUs here with
4683 length 0, and in cutu_reader::cutu_reader we'll fill in the
4684 actual length. */
4685 dwarf2_per_cu_data_up per_cu
4686 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4687 sect_off, 0);
4688 per_bfd->all_comp_units.push_back (std::move (per_cu));
4689 }
4690 return;
4691 }
4692
4693 sect_offset sect_off_prev;
4694 for (uint32_t i = 0; i <= map.cu_count; ++i)
4695 {
4696 sect_offset sect_off_next;
4697 if (i < map.cu_count)
4698 {
4699 sect_off_next
4700 = (sect_offset) (extract_unsigned_integer
4701 (map.cu_table_reordered + i * map.offset_size,
4702 map.offset_size,
4703 map.dwarf5_byte_order));
4704 }
4705 else
4706 sect_off_next = (sect_offset) section.size;
4707 if (i >= 1)
4708 {
4709 const ULONGEST length = sect_off_next - sect_off_prev;
4710 dwarf2_per_cu_data_up per_cu
4711 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4712 sect_off_prev, length);
4713 per_bfd->all_comp_units.push_back (std::move (per_cu));
4714 }
4715 sect_off_prev = sect_off_next;
4716 }
4717 }
4718
4719 /* Read the CU list from the mapped index, and use it to create all
4720 the CU objects for this dwarf2_per_objfile. */
4721
4722 static void
4723 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4724 const mapped_debug_names &map,
4725 const mapped_debug_names &dwz_map)
4726 {
4727 gdb_assert (per_bfd->all_comp_units.empty ());
4728 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4729
4730 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4731 false /* is_dwz */);
4732
4733 if (dwz_map.cu_count == 0)
4734 return;
4735
4736 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4737 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4738 true /* is_dwz */);
4739 }
4740
4741 /* Read .debug_names. If everything went ok, initialize the "quick"
4742 elements of all the CUs and return true. Otherwise, return false. */
4743
4744 static bool
4745 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4746 {
4747 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4748 mapped_debug_names dwz_map;
4749 struct objfile *objfile = per_objfile->objfile;
4750 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4751
4752 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4753 &per_bfd->debug_names, *map))
4754 return false;
4755
4756 /* Don't use the index if it's empty. */
4757 if (map->name_count == 0)
4758 return false;
4759
4760 /* If there is a .dwz file, read it so we can get its CU list as
4761 well. */
4762 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4763 if (dwz != NULL)
4764 {
4765 if (!read_debug_names_from_section (objfile,
4766 bfd_get_filename (dwz->dwz_bfd.get ()),
4767 &dwz->debug_names, dwz_map))
4768 {
4769 warning (_("could not read '.debug_names' section from %s; skipping"),
4770 bfd_get_filename (dwz->dwz_bfd.get ()));
4771 return false;
4772 }
4773 }
4774
4775 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4776
4777 if (map->tu_count != 0)
4778 {
4779 /* We can only handle a single .debug_types when we have an
4780 index. */
4781 if (per_bfd->types.size () != 1)
4782 return false;
4783
4784 dwarf2_section_info *section = &per_bfd->types[0];
4785
4786 create_signatured_type_table_from_debug_names
4787 (per_objfile, *map, section, &per_bfd->abbrev);
4788 }
4789
4790 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4791
4792 per_bfd->debug_names_table = std::move (map);
4793 per_bfd->using_index = 1;
4794 per_bfd->quick_file_names_table =
4795 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4796
4797 return true;
4798 }
4799
4800 /* Type used to manage iterating over all CUs looking for a symbol for
4801 .debug_names. */
4802
4803 class dw2_debug_names_iterator
4804 {
4805 public:
4806 dw2_debug_names_iterator (const mapped_debug_names &map,
4807 block_search_flags block_index,
4808 domain_enum domain,
4809 const char *name, dwarf2_per_objfile *per_objfile)
4810 : m_map (map), m_block_index (block_index), m_domain (domain),
4811 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4812 m_per_objfile (per_objfile)
4813 {}
4814
4815 dw2_debug_names_iterator (const mapped_debug_names &map,
4816 search_domain search, uint32_t namei,
4817 dwarf2_per_objfile *per_objfile,
4818 domain_enum domain = UNDEF_DOMAIN)
4819 : m_map (map),
4820 m_domain (domain),
4821 m_search (search),
4822 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4823 m_per_objfile (per_objfile)
4824 {}
4825
4826 dw2_debug_names_iterator (const mapped_debug_names &map,
4827 block_search_flags block_index, domain_enum domain,
4828 uint32_t namei, dwarf2_per_objfile *per_objfile)
4829 : m_map (map), m_block_index (block_index), m_domain (domain),
4830 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4831 m_per_objfile (per_objfile)
4832 {}
4833
4834 /* Return the next matching CU or NULL if there are no more. */
4835 dwarf2_per_cu_data *next ();
4836
4837 private:
4838 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4839 const char *name,
4840 dwarf2_per_objfile *per_objfile);
4841 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4842 uint32_t namei,
4843 dwarf2_per_objfile *per_objfile);
4844
4845 /* The internalized form of .debug_names. */
4846 const mapped_debug_names &m_map;
4847
4848 /* Restrict the search to these blocks. */
4849 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4850 | SEARCH_STATIC_BLOCK);
4851
4852 /* The kind of symbol we're looking for. */
4853 const domain_enum m_domain = UNDEF_DOMAIN;
4854 const search_domain m_search = ALL_DOMAIN;
4855
4856 /* The list of CUs from the index entry of the symbol, or NULL if
4857 not found. */
4858 const gdb_byte *m_addr;
4859
4860 dwarf2_per_objfile *m_per_objfile;
4861 };
4862
4863 const char *
4864 mapped_debug_names::namei_to_name
4865 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4866 {
4867 const ULONGEST namei_string_offs
4868 = extract_unsigned_integer ((name_table_string_offs_reordered
4869 + namei * offset_size),
4870 offset_size,
4871 dwarf5_byte_order);
4872 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4873 }
4874
4875 /* Find a slot in .debug_names for the object named NAME. If NAME is
4876 found, return pointer to its pool data. If NAME cannot be found,
4877 return NULL. */
4878
4879 const gdb_byte *
4880 dw2_debug_names_iterator::find_vec_in_debug_names
4881 (const mapped_debug_names &map, const char *name,
4882 dwarf2_per_objfile *per_objfile)
4883 {
4884 int (*cmp) (const char *, const char *);
4885
4886 gdb::unique_xmalloc_ptr<char> without_params;
4887 if (current_language->la_language == language_cplus
4888 || current_language->la_language == language_fortran
4889 || current_language->la_language == language_d)
4890 {
4891 /* NAME is already canonical. Drop any qualifiers as
4892 .debug_names does not contain any. */
4893
4894 if (strchr (name, '(') != NULL)
4895 {
4896 without_params = cp_remove_params (name);
4897 if (without_params != NULL)
4898 name = without_params.get ();
4899 }
4900 }
4901
4902 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4903
4904 const uint32_t full_hash = dwarf5_djb_hash (name);
4905 uint32_t namei
4906 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4907 (map.bucket_table_reordered
4908 + (full_hash % map.bucket_count)), 4,
4909 map.dwarf5_byte_order);
4910 if (namei == 0)
4911 return NULL;
4912 --namei;
4913 if (namei >= map.name_count)
4914 {
4915 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4916 "[in module %s]"),
4917 namei, map.name_count,
4918 objfile_name (per_objfile->objfile));
4919 return NULL;
4920 }
4921
4922 for (;;)
4923 {
4924 const uint32_t namei_full_hash
4925 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4926 (map.hash_table_reordered + namei), 4,
4927 map.dwarf5_byte_order);
4928 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4929 return NULL;
4930
4931 if (full_hash == namei_full_hash)
4932 {
4933 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4934
4935 #if 0 /* An expensive sanity check. */
4936 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4937 {
4938 complaint (_("Wrong .debug_names hash for string at index %u "
4939 "[in module %s]"),
4940 namei, objfile_name (dwarf2_per_objfile->objfile));
4941 return NULL;
4942 }
4943 #endif
4944
4945 if (cmp (namei_string, name) == 0)
4946 {
4947 const ULONGEST namei_entry_offs
4948 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4949 + namei * map.offset_size),
4950 map.offset_size, map.dwarf5_byte_order);
4951 return map.entry_pool + namei_entry_offs;
4952 }
4953 }
4954
4955 ++namei;
4956 if (namei >= map.name_count)
4957 return NULL;
4958 }
4959 }
4960
4961 const gdb_byte *
4962 dw2_debug_names_iterator::find_vec_in_debug_names
4963 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4964 {
4965 if (namei >= map.name_count)
4966 {
4967 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4968 "[in module %s]"),
4969 namei, map.name_count,
4970 objfile_name (per_objfile->objfile));
4971 return NULL;
4972 }
4973
4974 const ULONGEST namei_entry_offs
4975 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4976 + namei * map.offset_size),
4977 map.offset_size, map.dwarf5_byte_order);
4978 return map.entry_pool + namei_entry_offs;
4979 }
4980
4981 /* See dw2_debug_names_iterator. */
4982
4983 dwarf2_per_cu_data *
4984 dw2_debug_names_iterator::next ()
4985 {
4986 if (m_addr == NULL)
4987 return NULL;
4988
4989 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4990 struct objfile *objfile = m_per_objfile->objfile;
4991 bfd *const abfd = objfile->obfd;
4992
4993 again:
4994
4995 unsigned int bytes_read;
4996 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4997 m_addr += bytes_read;
4998 if (abbrev == 0)
4999 return NULL;
5000
5001 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5002 if (indexval_it == m_map.abbrev_map.cend ())
5003 {
5004 complaint (_("Wrong .debug_names undefined abbrev code %s "
5005 "[in module %s]"),
5006 pulongest (abbrev), objfile_name (objfile));
5007 return NULL;
5008 }
5009 const mapped_debug_names::index_val &indexval = indexval_it->second;
5010 enum class symbol_linkage {
5011 unknown,
5012 static_,
5013 extern_,
5014 } symbol_linkage_ = symbol_linkage::unknown;
5015 dwarf2_per_cu_data *per_cu = NULL;
5016 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5017 {
5018 ULONGEST ull;
5019 switch (attr.form)
5020 {
5021 case DW_FORM_implicit_const:
5022 ull = attr.implicit_const;
5023 break;
5024 case DW_FORM_flag_present:
5025 ull = 1;
5026 break;
5027 case DW_FORM_udata:
5028 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5029 m_addr += bytes_read;
5030 break;
5031 case DW_FORM_ref4:
5032 ull = read_4_bytes (abfd, m_addr);
5033 m_addr += 4;
5034 break;
5035 case DW_FORM_ref8:
5036 ull = read_8_bytes (abfd, m_addr);
5037 m_addr += 8;
5038 break;
5039 case DW_FORM_ref_sig8:
5040 ull = read_8_bytes (abfd, m_addr);
5041 m_addr += 8;
5042 break;
5043 default:
5044 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5045 dwarf_form_name (attr.form),
5046 objfile_name (objfile));
5047 return NULL;
5048 }
5049 switch (attr.dw_idx)
5050 {
5051 case DW_IDX_compile_unit:
5052 /* Don't crash on bad data. */
5053 if (ull >= per_bfd->all_comp_units.size ())
5054 {
5055 complaint (_(".debug_names entry has bad CU index %s"
5056 " [in module %s]"),
5057 pulongest (ull),
5058 objfile_name (objfile));
5059 continue;
5060 }
5061 per_cu = per_bfd->get_cu (ull);
5062 break;
5063 case DW_IDX_type_unit:
5064 /* Don't crash on bad data. */
5065 if (ull >= per_bfd->tu_stats.nr_tus)
5066 {
5067 complaint (_(".debug_names entry has bad TU index %s"
5068 " [in module %s]"),
5069 pulongest (ull),
5070 objfile_name (objfile));
5071 continue;
5072 }
5073 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5074 break;
5075 case DW_IDX_die_offset:
5076 /* In a per-CU index (as opposed to a per-module index), index
5077 entries without CU attribute implicitly refer to the single CU. */
5078 if (per_cu == NULL)
5079 per_cu = per_bfd->get_cu (0);
5080 break;
5081 case DW_IDX_GNU_internal:
5082 if (!m_map.augmentation_is_gdb)
5083 break;
5084 symbol_linkage_ = symbol_linkage::static_;
5085 break;
5086 case DW_IDX_GNU_external:
5087 if (!m_map.augmentation_is_gdb)
5088 break;
5089 symbol_linkage_ = symbol_linkage::extern_;
5090 break;
5091 }
5092 }
5093
5094 /* Skip if already read in. */
5095 if (m_per_objfile->symtab_set_p (per_cu))
5096 goto again;
5097
5098 /* Check static vs global. */
5099 if (symbol_linkage_ != symbol_linkage::unknown)
5100 {
5101 if (symbol_linkage_ == symbol_linkage::static_)
5102 {
5103 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5104 goto again;
5105 }
5106 else
5107 {
5108 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5109 goto again;
5110 }
5111 }
5112
5113 /* Match dw2_symtab_iter_next, symbol_kind
5114 and debug_names::psymbol_tag. */
5115 switch (m_domain)
5116 {
5117 case VAR_DOMAIN:
5118 switch (indexval.dwarf_tag)
5119 {
5120 case DW_TAG_variable:
5121 case DW_TAG_subprogram:
5122 /* Some types are also in VAR_DOMAIN. */
5123 case DW_TAG_typedef:
5124 case DW_TAG_structure_type:
5125 break;
5126 default:
5127 goto again;
5128 }
5129 break;
5130 case STRUCT_DOMAIN:
5131 switch (indexval.dwarf_tag)
5132 {
5133 case DW_TAG_typedef:
5134 case DW_TAG_structure_type:
5135 break;
5136 default:
5137 goto again;
5138 }
5139 break;
5140 case LABEL_DOMAIN:
5141 switch (indexval.dwarf_tag)
5142 {
5143 case 0:
5144 case DW_TAG_variable:
5145 break;
5146 default:
5147 goto again;
5148 }
5149 break;
5150 case MODULE_DOMAIN:
5151 switch (indexval.dwarf_tag)
5152 {
5153 case DW_TAG_module:
5154 break;
5155 default:
5156 goto again;
5157 }
5158 break;
5159 default:
5160 break;
5161 }
5162
5163 /* Match dw2_expand_symtabs_matching, symbol_kind and
5164 debug_names::psymbol_tag. */
5165 switch (m_search)
5166 {
5167 case VARIABLES_DOMAIN:
5168 switch (indexval.dwarf_tag)
5169 {
5170 case DW_TAG_variable:
5171 break;
5172 default:
5173 goto again;
5174 }
5175 break;
5176 case FUNCTIONS_DOMAIN:
5177 switch (indexval.dwarf_tag)
5178 {
5179 case DW_TAG_subprogram:
5180 break;
5181 default:
5182 goto again;
5183 }
5184 break;
5185 case TYPES_DOMAIN:
5186 switch (indexval.dwarf_tag)
5187 {
5188 case DW_TAG_typedef:
5189 case DW_TAG_structure_type:
5190 break;
5191 default:
5192 goto again;
5193 }
5194 break;
5195 case MODULES_DOMAIN:
5196 switch (indexval.dwarf_tag)
5197 {
5198 case DW_TAG_module:
5199 break;
5200 default:
5201 goto again;
5202 }
5203 default:
5204 break;
5205 }
5206
5207 return per_cu;
5208 }
5209
5210 /* This dumps minimal information about .debug_names. It is called
5211 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5212 uses this to verify that .debug_names has been loaded. */
5213
5214 void
5215 dwarf2_debug_names_index::dump (struct objfile *objfile)
5216 {
5217 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5218
5219 gdb_assert (per_objfile->per_bfd->using_index);
5220 printf_filtered (".debug_names:");
5221 if (per_objfile->per_bfd->debug_names_table)
5222 printf_filtered (" exists\n");
5223 else
5224 printf_filtered (" faked for \"readnow\"\n");
5225 printf_filtered ("\n");
5226 }
5227
5228 void
5229 dwarf2_debug_names_index::expand_matching_symbols
5230 (struct objfile *objfile,
5231 const lookup_name_info &name, domain_enum domain,
5232 int global,
5233 symbol_compare_ftype *ordered_compare)
5234 {
5235 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5236
5237 /* debug_names_table is NULL if OBJF_READNOW. */
5238 if (!per_objfile->per_bfd->debug_names_table)
5239 return;
5240
5241 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5242 const block_search_flags block_flags
5243 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5244
5245 const char *match_name = name.ada ().lookup_name ().c_str ();
5246 auto matcher = [&] (const char *symname)
5247 {
5248 if (ordered_compare == nullptr)
5249 return true;
5250 return ordered_compare (symname, match_name) == 0;
5251 };
5252
5253 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5254 [&] (offset_type namei)
5255 {
5256 /* The name was matched, now expand corresponding CUs that were
5257 marked. */
5258 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5259 per_objfile);
5260
5261 struct dwarf2_per_cu_data *per_cu;
5262 while ((per_cu = iter.next ()) != NULL)
5263 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5264 nullptr);
5265 return true;
5266 }, per_objfile);
5267 }
5268
5269 bool
5270 dwarf2_debug_names_index::expand_symtabs_matching
5271 (struct objfile *objfile,
5272 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5273 const lookup_name_info *lookup_name,
5274 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5275 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5276 block_search_flags search_flags,
5277 domain_enum domain,
5278 enum search_domain kind)
5279 {
5280 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5281
5282 /* debug_names_table is NULL if OBJF_READNOW. */
5283 if (!per_objfile->per_bfd->debug_names_table)
5284 return true;
5285
5286 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5287
5288 if (symbol_matcher == NULL && lookup_name == NULL)
5289 {
5290 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
5291 {
5292 QUIT;
5293
5294 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
5295 file_matcher,
5296 expansion_notify))
5297 return false;
5298 }
5299 return true;
5300 }
5301
5302 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5303
5304 bool result
5305 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5306 symbol_matcher,
5307 [&] (offset_type namei)
5308 {
5309 /* The name was matched, now expand corresponding CUs that were
5310 marked. */
5311 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5312
5313 struct dwarf2_per_cu_data *per_cu;
5314 while ((per_cu = iter.next ()) != NULL)
5315 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5316 file_matcher,
5317 expansion_notify))
5318 return false;
5319 return true;
5320 }, per_objfile);
5321
5322 return result;
5323 }
5324
5325 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5326 to either a dwarf2_per_bfd or dwz_file object. */
5327
5328 template <typename T>
5329 static gdb::array_view<const gdb_byte>
5330 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5331 {
5332 dwarf2_section_info *section = &section_owner->gdb_index;
5333
5334 if (section->empty ())
5335 return {};
5336
5337 /* Older elfutils strip versions could keep the section in the main
5338 executable while splitting it for the separate debug info file. */
5339 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5340 return {};
5341
5342 section->read (obj);
5343
5344 /* dwarf2_section_info::size is a bfd_size_type, while
5345 gdb::array_view works with size_t. On 32-bit hosts, with
5346 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5347 is 32-bit. So we need an explicit narrowing conversion here.
5348 This is fine, because it's impossible to allocate or mmap an
5349 array/buffer larger than what size_t can represent. */
5350 return gdb::make_array_view (section->buffer, section->size);
5351 }
5352
5353 /* Lookup the index cache for the contents of the index associated to
5354 DWARF2_OBJ. */
5355
5356 static gdb::array_view<const gdb_byte>
5357 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5358 {
5359 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5360 if (build_id == nullptr)
5361 return {};
5362
5363 return global_index_cache.lookup_gdb_index (build_id,
5364 &dwarf2_per_bfd->index_cache_res);
5365 }
5366
5367 /* Same as the above, but for DWZ. */
5368
5369 static gdb::array_view<const gdb_byte>
5370 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5371 {
5372 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5373 if (build_id == nullptr)
5374 return {};
5375
5376 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5377 }
5378
5379 /* See dwarf2/public.h. */
5380
5381 void
5382 dwarf2_initialize_objfile (struct objfile *objfile)
5383 {
5384 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5385 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5386
5387 dwarf_read_debug_printf ("called");
5388
5389 /* If we're about to read full symbols, don't bother with the
5390 indices. In this case we also don't care if some other debug
5391 format is making psymtabs, because they are all about to be
5392 expanded anyway. */
5393 if ((objfile->flags & OBJF_READNOW))
5394 {
5395 dwarf_read_debug_printf ("readnow requested");
5396
5397 /* When using READNOW, the using_index flag (set below) indicates that
5398 PER_BFD was already initialized, when we loaded some other objfile. */
5399 if (per_bfd->using_index)
5400 {
5401 dwarf_read_debug_printf ("using_index already set");
5402 per_objfile->resize_symtabs ();
5403 objfile->qf.push_front (make_dwarf_gdb_index ());
5404 return;
5405 }
5406
5407 per_bfd->using_index = 1;
5408 create_all_comp_units (per_objfile);
5409 per_bfd->quick_file_names_table
5410 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5411 per_objfile->resize_symtabs ();
5412
5413 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5414 {
5415 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5416
5417 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5418 struct dwarf2_per_cu_quick_data);
5419 }
5420
5421 /* Arrange for gdb to see the "quick" functions. However, these
5422 functions will be no-ops because we will have expanded all
5423 symtabs. */
5424 objfile->qf.push_front (make_dwarf_gdb_index ());
5425 return;
5426 }
5427
5428 /* Was a debug names index already read when we processed an objfile sharing
5429 PER_BFD? */
5430 if (per_bfd->debug_names_table != nullptr)
5431 {
5432 dwarf_read_debug_printf ("re-using shared debug names table");
5433 per_objfile->resize_symtabs ();
5434 objfile->qf.push_front (make_dwarf_debug_names ());
5435 return;
5436 }
5437
5438 /* Was a GDB index already read when we processed an objfile sharing
5439 PER_BFD? */
5440 if (per_bfd->index_table != nullptr)
5441 {
5442 dwarf_read_debug_printf ("re-using shared index table");
5443 per_objfile->resize_symtabs ();
5444 objfile->qf.push_front (make_dwarf_gdb_index ());
5445 return;
5446 }
5447
5448 /* There might already be partial symtabs built for this BFD. This happens
5449 when loading the same binary twice with the index-cache enabled. If so,
5450 don't try to read an index. The objfile / per_objfile initialization will
5451 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5452 code path. */
5453 if (per_bfd->partial_symtabs != nullptr)
5454 {
5455 dwarf_read_debug_printf ("re-using shared partial symtabs");
5456 objfile->qf.push_front (make_lazy_dwarf_reader ());
5457 return;
5458 }
5459
5460 if (dwarf2_read_debug_names (per_objfile))
5461 {
5462 dwarf_read_debug_printf ("found debug names");
5463 per_objfile->resize_symtabs ();
5464 objfile->qf.push_front (make_dwarf_debug_names ());
5465 return;
5466 }
5467
5468 if (dwarf2_read_gdb_index (per_objfile,
5469 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5470 get_gdb_index_contents_from_section<dwz_file>))
5471 {
5472 dwarf_read_debug_printf ("found gdb index from file");
5473 per_objfile->resize_symtabs ();
5474 objfile->qf.push_front (make_dwarf_gdb_index ());
5475 return;
5476 }
5477
5478 /* ... otherwise, try to find the index in the index cache. */
5479 if (dwarf2_read_gdb_index (per_objfile,
5480 get_gdb_index_contents_from_cache,
5481 get_gdb_index_contents_from_cache_dwz))
5482 {
5483 dwarf_read_debug_printf ("found gdb index from cache");
5484 global_index_cache.hit ();
5485 per_objfile->resize_symtabs ();
5486 objfile->qf.push_front (make_dwarf_gdb_index ());
5487 return;
5488 }
5489
5490 global_index_cache.miss ();
5491 objfile->qf.push_front (make_lazy_dwarf_reader ());
5492 }
5493
5494 \f
5495
5496 /* Build a partial symbol table. */
5497
5498 void
5499 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5500 {
5501 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5502 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5503
5504 if (per_bfd->partial_symtabs != nullptr)
5505 {
5506 /* Partial symbols were already read, so now we can simply
5507 attach them. */
5508 if (psf == nullptr)
5509 {
5510 psf = new psymbol_functions (per_bfd->partial_symtabs);
5511 objfile->qf.emplace_front (psf);
5512 }
5513 else
5514 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5515 per_objfile->resize_symtabs ();
5516 return;
5517 }
5518
5519 if (psf == nullptr)
5520 {
5521 psf = new psymbol_functions;
5522 objfile->qf.emplace_front (psf);
5523 }
5524 const std::shared_ptr<psymtab_storage> &partial_symtabs
5525 = psf->get_partial_symtabs ();
5526
5527 /* Set the local reference to partial symtabs, so that we don't try
5528 to read them again if reading another objfile with the same BFD.
5529 If we can't in fact share, this won't make a difference anyway as
5530 the dwarf2_per_bfd object won't be shared. */
5531 per_bfd->partial_symtabs = partial_symtabs;
5532
5533 try
5534 {
5535 /* This isn't really ideal: all the data we allocate on the
5536 objfile's obstack is still uselessly kept around. However,
5537 freeing it seems unsafe. */
5538 psymtab_discarder psymtabs (partial_symtabs.get ());
5539 dwarf2_build_psymtabs_hard (per_objfile);
5540 psymtabs.keep ();
5541
5542 per_objfile->resize_symtabs ();
5543
5544 /* (maybe) store an index in the cache. */
5545 global_index_cache.store (per_objfile);
5546 }
5547 catch (const gdb_exception_error &except)
5548 {
5549 exception_print (gdb_stderr, except);
5550 }
5551 }
5552
5553 /* Find the base address of the compilation unit for range lists and
5554 location lists. It will normally be specified by DW_AT_low_pc.
5555 In DWARF-3 draft 4, the base address could be overridden by
5556 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5557 compilation units with discontinuous ranges. */
5558
5559 static void
5560 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5561 {
5562 struct attribute *attr;
5563
5564 cu->base_address.reset ();
5565
5566 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5567 if (attr != nullptr)
5568 cu->base_address = attr->as_address ();
5569 else
5570 {
5571 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5572 if (attr != nullptr)
5573 cu->base_address = attr->as_address ();
5574 }
5575 }
5576
5577 /* Helper function that returns the proper abbrev section for
5578 THIS_CU. */
5579
5580 static struct dwarf2_section_info *
5581 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5582 {
5583 struct dwarf2_section_info *abbrev;
5584 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5585
5586 if (this_cu->is_dwz)
5587 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5588 else
5589 abbrev = &per_bfd->abbrev;
5590
5591 return abbrev;
5592 }
5593
5594 /* Fetch the abbreviation table offset from a comp or type unit header. */
5595
5596 static sect_offset
5597 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5598 struct dwarf2_section_info *section,
5599 sect_offset sect_off)
5600 {
5601 bfd *abfd = section->get_bfd_owner ();
5602 const gdb_byte *info_ptr;
5603 unsigned int initial_length_size, offset_size;
5604 uint16_t version;
5605
5606 section->read (per_objfile->objfile);
5607 info_ptr = section->buffer + to_underlying (sect_off);
5608 read_initial_length (abfd, info_ptr, &initial_length_size);
5609 offset_size = initial_length_size == 4 ? 4 : 8;
5610 info_ptr += initial_length_size;
5611
5612 version = read_2_bytes (abfd, info_ptr);
5613 info_ptr += 2;
5614 if (version >= 5)
5615 {
5616 /* Skip unit type and address size. */
5617 info_ptr += 2;
5618 }
5619
5620 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5621 }
5622
5623 /* A partial symtab that is used only for include files. */
5624 struct dwarf2_include_psymtab : public partial_symtab
5625 {
5626 dwarf2_include_psymtab (const char *filename,
5627 psymtab_storage *partial_symtabs,
5628 objfile_per_bfd_storage *objfile_per_bfd)
5629 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5630 {
5631 }
5632
5633 void read_symtab (struct objfile *objfile) override
5634 {
5635 /* It's an include file, no symbols to read for it.
5636 Everything is in the includer symtab. */
5637
5638 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5639 expansion of the includer psymtab. We use the dependencies[0] field to
5640 model the includer. But if we go the regular route of calling
5641 expand_psymtab here, and having expand_psymtab call expand_dependencies
5642 to expand the includer, we'll only use expand_psymtab on the includer
5643 (making it a non-toplevel psymtab), while if we expand the includer via
5644 another path, we'll use read_symtab (making it a toplevel psymtab).
5645 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5646 psymtab, and trigger read_symtab on the includer here directly. */
5647 includer ()->read_symtab (objfile);
5648 }
5649
5650 void expand_psymtab (struct objfile *objfile) override
5651 {
5652 /* This is not called by read_symtab, and should not be called by any
5653 expand_dependencies. */
5654 gdb_assert (false);
5655 }
5656
5657 bool readin_p (struct objfile *objfile) const override
5658 {
5659 return includer ()->readin_p (objfile);
5660 }
5661
5662 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5663 {
5664 return nullptr;
5665 }
5666
5667 private:
5668 partial_symtab *includer () const
5669 {
5670 /* An include psymtab has exactly one dependency: the psymtab that
5671 includes it. */
5672 gdb_assert (this->number_of_dependencies == 1);
5673 return this->dependencies[0];
5674 }
5675 };
5676
5677 /* Allocate a new partial symtab for file named NAME and mark this new
5678 partial symtab as being an include of PST. */
5679
5680 static void
5681 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5682 const char *name,
5683 dwarf2_psymtab *pst,
5684 psymtab_storage *partial_symtabs,
5685 objfile_per_bfd_storage *objfile_per_bfd)
5686 {
5687 dwarf2_include_psymtab *subpst
5688 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5689
5690 if (!IS_ABSOLUTE_PATH (subpst->filename))
5691 subpst->dirname = pst->dirname;
5692
5693 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5694 subpst->dependencies[0] = pst;
5695 subpst->number_of_dependencies = 1;
5696 }
5697
5698 /* Read the Line Number Program data and extract the list of files
5699 included by the source file represented by PST. Build an include
5700 partial symtab for each of these included files. */
5701
5702 static void
5703 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5704 struct die_info *die,
5705 dwarf2_psymtab *pst)
5706 {
5707 line_header_up lh;
5708 struct attribute *attr;
5709
5710 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5711 if (attr != nullptr && attr->form_is_unsigned ())
5712 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5713 if (lh == NULL)
5714 return; /* No linetable, so no includes. */
5715
5716 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5717 that we pass in the raw text_low here; that is ok because we're
5718 only decoding the line table to make include partial symtabs, and
5719 so the addresses aren't really used. */
5720 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5721 pst->raw_text_low (), 1);
5722 }
5723
5724 static hashval_t
5725 hash_signatured_type (const void *item)
5726 {
5727 const struct signatured_type *sig_type
5728 = (const struct signatured_type *) item;
5729
5730 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5731 return sig_type->signature;
5732 }
5733
5734 static int
5735 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5736 {
5737 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5738 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5739
5740 return lhs->signature == rhs->signature;
5741 }
5742
5743 /* Allocate a hash table for signatured types. */
5744
5745 static htab_up
5746 allocate_signatured_type_table ()
5747 {
5748 return htab_up (htab_create_alloc (41,
5749 hash_signatured_type,
5750 eq_signatured_type,
5751 NULL, xcalloc, xfree));
5752 }
5753
5754 /* A helper for create_debug_types_hash_table. Read types from SECTION
5755 and fill them into TYPES_HTAB. It will process only type units,
5756 therefore DW_UT_type. */
5757
5758 static void
5759 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5760 struct dwo_file *dwo_file,
5761 dwarf2_section_info *section, htab_up &types_htab,
5762 rcuh_kind section_kind)
5763 {
5764 struct objfile *objfile = per_objfile->objfile;
5765 struct dwarf2_section_info *abbrev_section;
5766 bfd *abfd;
5767 const gdb_byte *info_ptr, *end_ptr;
5768
5769 abbrev_section = &dwo_file->sections.abbrev;
5770
5771 dwarf_read_debug_printf ("Reading %s for %s",
5772 section->get_name (),
5773 abbrev_section->get_file_name ());
5774
5775 section->read (objfile);
5776 info_ptr = section->buffer;
5777
5778 if (info_ptr == NULL)
5779 return;
5780
5781 /* We can't set abfd until now because the section may be empty or
5782 not present, in which case the bfd is unknown. */
5783 abfd = section->get_bfd_owner ();
5784
5785 /* We don't use cutu_reader here because we don't need to read
5786 any dies: the signature is in the header. */
5787
5788 end_ptr = info_ptr + section->size;
5789 while (info_ptr < end_ptr)
5790 {
5791 signatured_type_up sig_type;
5792 struct dwo_unit *dwo_tu;
5793 void **slot;
5794 const gdb_byte *ptr = info_ptr;
5795 struct comp_unit_head header;
5796 unsigned int length;
5797
5798 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5799
5800 /* Initialize it due to a false compiler warning. */
5801 header.signature = -1;
5802 header.type_cu_offset_in_tu = (cu_offset) -1;
5803
5804 /* We need to read the type's signature in order to build the hash
5805 table, but we don't need anything else just yet. */
5806
5807 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5808 abbrev_section, ptr, section_kind);
5809
5810 length = header.get_length ();
5811
5812 /* Skip dummy type units. */
5813 if (ptr >= info_ptr + length
5814 || peek_abbrev_code (abfd, ptr) == 0
5815 || (header.unit_type != DW_UT_type
5816 && header.unit_type != DW_UT_split_type))
5817 {
5818 info_ptr += length;
5819 continue;
5820 }
5821
5822 if (types_htab == NULL)
5823 types_htab = allocate_dwo_unit_table ();
5824
5825 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5826 dwo_tu->dwo_file = dwo_file;
5827 dwo_tu->signature = header.signature;
5828 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5829 dwo_tu->section = section;
5830 dwo_tu->sect_off = sect_off;
5831 dwo_tu->length = length;
5832
5833 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5834 gdb_assert (slot != NULL);
5835 if (*slot != NULL)
5836 complaint (_("debug type entry at offset %s is duplicate to"
5837 " the entry at offset %s, signature %s"),
5838 sect_offset_str (sect_off),
5839 sect_offset_str (dwo_tu->sect_off),
5840 hex_string (header.signature));
5841 *slot = dwo_tu;
5842
5843 dwarf_read_debug_printf_v (" offset %s, signature %s",
5844 sect_offset_str (sect_off),
5845 hex_string (header.signature));
5846
5847 info_ptr += length;
5848 }
5849 }
5850
5851 /* Create the hash table of all entries in the .debug_types
5852 (or .debug_types.dwo) section(s).
5853 DWO_FILE is a pointer to the DWO file object.
5854
5855 The result is a pointer to the hash table or NULL if there are no types.
5856
5857 Note: This function processes DWO files only, not DWP files. */
5858
5859 static void
5860 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5861 struct dwo_file *dwo_file,
5862 gdb::array_view<dwarf2_section_info> type_sections,
5863 htab_up &types_htab)
5864 {
5865 for (dwarf2_section_info &section : type_sections)
5866 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5867 rcuh_kind::TYPE);
5868 }
5869
5870 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5871 If SLOT is non-NULL, it is the entry to use in the hash table.
5872 Otherwise we find one. */
5873
5874 static struct signatured_type *
5875 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5876 {
5877 if (per_objfile->per_bfd->all_comp_units.size ()
5878 == per_objfile->per_bfd->all_comp_units.capacity ())
5879 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5880
5881 signatured_type_up sig_type_holder
5882 = per_objfile->per_bfd->allocate_signatured_type (sig);
5883 signatured_type *sig_type = sig_type_holder.get ();
5884
5885 per_objfile->resize_symtabs ();
5886
5887 per_objfile->per_bfd->all_comp_units.emplace_back
5888 (sig_type_holder.release ());
5889 if (per_objfile->per_bfd->using_index)
5890 {
5891 sig_type->v.quick =
5892 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
5893 struct dwarf2_per_cu_quick_data);
5894 }
5895
5896 if (slot == NULL)
5897 {
5898 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5899 sig_type, INSERT);
5900 }
5901 gdb_assert (*slot == NULL);
5902 *slot = sig_type;
5903 /* The rest of sig_type must be filled in by the caller. */
5904 return sig_type;
5905 }
5906
5907 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5908 Fill in SIG_ENTRY with DWO_ENTRY. */
5909
5910 static void
5911 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5912 struct signatured_type *sig_entry,
5913 struct dwo_unit *dwo_entry)
5914 {
5915 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5916
5917 /* Make sure we're not clobbering something we don't expect to. */
5918 gdb_assert (! sig_entry->queued);
5919 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5920 if (per_bfd->using_index)
5921 {
5922 gdb_assert (sig_entry->v.quick != NULL);
5923 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5924 }
5925 else
5926 gdb_assert (sig_entry->v.psymtab == NULL);
5927 gdb_assert (sig_entry->signature == dwo_entry->signature);
5928 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5929 gdb_assert (sig_entry->type_unit_group == NULL);
5930 gdb_assert (sig_entry->dwo_unit == NULL);
5931
5932 sig_entry->section = dwo_entry->section;
5933 sig_entry->sect_off = dwo_entry->sect_off;
5934 sig_entry->length = dwo_entry->length;
5935 sig_entry->reading_dwo_directly = 1;
5936 sig_entry->per_bfd = per_bfd;
5937 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5938 sig_entry->dwo_unit = dwo_entry;
5939 }
5940
5941 /* Subroutine of lookup_signatured_type.
5942 If we haven't read the TU yet, create the signatured_type data structure
5943 for a TU to be read in directly from a DWO file, bypassing the stub.
5944 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5945 using .gdb_index, then when reading a CU we want to stay in the DWO file
5946 containing that CU. Otherwise we could end up reading several other DWO
5947 files (due to comdat folding) to process the transitive closure of all the
5948 mentioned TUs, and that can be slow. The current DWO file will have every
5949 type signature that it needs.
5950 We only do this for .gdb_index because in the psymtab case we already have
5951 to read all the DWOs to build the type unit groups. */
5952
5953 static struct signatured_type *
5954 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5955 {
5956 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5957 struct dwo_file *dwo_file;
5958 struct dwo_unit find_dwo_entry, *dwo_entry;
5959 void **slot;
5960
5961 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
5962
5963 /* If TU skeletons have been removed then we may not have read in any
5964 TUs yet. */
5965 if (per_objfile->per_bfd->signatured_types == NULL)
5966 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5967
5968 /* We only ever need to read in one copy of a signatured type.
5969 Use the global signatured_types array to do our own comdat-folding
5970 of types. If this is the first time we're reading this TU, and
5971 the TU has an entry in .gdb_index, replace the recorded data from
5972 .gdb_index with this TU. */
5973
5974 signatured_type find_sig_entry (sig);
5975 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5976 &find_sig_entry, INSERT);
5977 signatured_type *sig_entry = (struct signatured_type *) *slot;
5978
5979 /* We can get here with the TU already read, *or* in the process of being
5980 read. Don't reassign the global entry to point to this DWO if that's
5981 the case. Also note that if the TU is already being read, it may not
5982 have come from a DWO, the program may be a mix of Fission-compiled
5983 code and non-Fission-compiled code. */
5984
5985 /* Have we already tried to read this TU?
5986 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5987 needn't exist in the global table yet). */
5988 if (sig_entry != NULL && sig_entry->tu_read)
5989 return sig_entry;
5990
5991 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5992 dwo_unit of the TU itself. */
5993 dwo_file = cu->dwo_unit->dwo_file;
5994
5995 /* Ok, this is the first time we're reading this TU. */
5996 if (dwo_file->tus == NULL)
5997 return NULL;
5998 find_dwo_entry.signature = sig;
5999 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6000 &find_dwo_entry);
6001 if (dwo_entry == NULL)
6002 return NULL;
6003
6004 /* If the global table doesn't have an entry for this TU, add one. */
6005 if (sig_entry == NULL)
6006 sig_entry = add_type_unit (per_objfile, sig, slot);
6007
6008 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6009 sig_entry->tu_read = 1;
6010 return sig_entry;
6011 }
6012
6013 /* Subroutine of lookup_signatured_type.
6014 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6015 then try the DWP file. If the TU stub (skeleton) has been removed then
6016 it won't be in .gdb_index. */
6017
6018 static struct signatured_type *
6019 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6020 {
6021 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6022 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6023 struct dwo_unit *dwo_entry;
6024 void **slot;
6025
6026 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6027 gdb_assert (dwp_file != NULL);
6028
6029 /* If TU skeletons have been removed then we may not have read in any
6030 TUs yet. */
6031 if (per_objfile->per_bfd->signatured_types == NULL)
6032 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6033
6034 signatured_type find_sig_entry (sig);
6035 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6036 &find_sig_entry, INSERT);
6037 signatured_type *sig_entry = (struct signatured_type *) *slot;
6038
6039 /* Have we already tried to read this TU?
6040 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6041 needn't exist in the global table yet). */
6042 if (sig_entry != NULL)
6043 return sig_entry;
6044
6045 if (dwp_file->tus == NULL)
6046 return NULL;
6047 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6048 1 /* is_debug_types */);
6049 if (dwo_entry == NULL)
6050 return NULL;
6051
6052 sig_entry = add_type_unit (per_objfile, sig, slot);
6053 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6054
6055 return sig_entry;
6056 }
6057
6058 /* Lookup a signature based type for DW_FORM_ref_sig8.
6059 Returns NULL if signature SIG is not present in the table.
6060 It is up to the caller to complain about this. */
6061
6062 static struct signatured_type *
6063 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6064 {
6065 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6066
6067 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6068 {
6069 /* We're in a DWO/DWP file, and we're using .gdb_index.
6070 These cases require special processing. */
6071 if (get_dwp_file (per_objfile) == NULL)
6072 return lookup_dwo_signatured_type (cu, sig);
6073 else
6074 return lookup_dwp_signatured_type (cu, sig);
6075 }
6076 else
6077 {
6078 if (per_objfile->per_bfd->signatured_types == NULL)
6079 return NULL;
6080 signatured_type find_entry (sig);
6081 return ((struct signatured_type *)
6082 htab_find (per_objfile->per_bfd->signatured_types.get (),
6083 &find_entry));
6084 }
6085 }
6086
6087 /* Low level DIE reading support. */
6088
6089 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6090
6091 static void
6092 init_cu_die_reader (struct die_reader_specs *reader,
6093 struct dwarf2_cu *cu,
6094 struct dwarf2_section_info *section,
6095 struct dwo_file *dwo_file,
6096 struct abbrev_table *abbrev_table)
6097 {
6098 gdb_assert (section->readin && section->buffer != NULL);
6099 reader->abfd = section->get_bfd_owner ();
6100 reader->cu = cu;
6101 reader->dwo_file = dwo_file;
6102 reader->die_section = section;
6103 reader->buffer = section->buffer;
6104 reader->buffer_end = section->buffer + section->size;
6105 reader->abbrev_table = abbrev_table;
6106 }
6107
6108 /* Subroutine of cutu_reader to simplify it.
6109 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6110 There's just a lot of work to do, and cutu_reader is big enough
6111 already.
6112
6113 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6114 from it to the DIE in the DWO. If NULL we are skipping the stub.
6115 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6116 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6117 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6118 STUB_COMP_DIR may be non-NULL.
6119 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6120 are filled in with the info of the DIE from the DWO file.
6121 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6122 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6123 kept around for at least as long as *RESULT_READER.
6124
6125 The result is non-zero if a valid (non-dummy) DIE was found. */
6126
6127 static int
6128 read_cutu_die_from_dwo (dwarf2_cu *cu,
6129 struct dwo_unit *dwo_unit,
6130 struct die_info *stub_comp_unit_die,
6131 const char *stub_comp_dir,
6132 struct die_reader_specs *result_reader,
6133 const gdb_byte **result_info_ptr,
6134 struct die_info **result_comp_unit_die,
6135 abbrev_table_up *result_dwo_abbrev_table)
6136 {
6137 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6138 dwarf2_per_cu_data *per_cu = cu->per_cu;
6139 struct objfile *objfile = per_objfile->objfile;
6140 bfd *abfd;
6141 const gdb_byte *begin_info_ptr, *info_ptr;
6142 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6143 int i,num_extra_attrs;
6144 struct dwarf2_section_info *dwo_abbrev_section;
6145 struct die_info *comp_unit_die;
6146
6147 /* At most one of these may be provided. */
6148 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6149
6150 /* These attributes aren't processed until later:
6151 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6152 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6153 referenced later. However, these attributes are found in the stub
6154 which we won't have later. In order to not impose this complication
6155 on the rest of the code, we read them here and copy them to the
6156 DWO CU/TU die. */
6157
6158 stmt_list = NULL;
6159 low_pc = NULL;
6160 high_pc = NULL;
6161 ranges = NULL;
6162 comp_dir = NULL;
6163
6164 if (stub_comp_unit_die != NULL)
6165 {
6166 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6167 DWO file. */
6168 if (!per_cu->is_debug_types)
6169 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6170 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6171 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6172 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6173 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6174
6175 cu->addr_base = stub_comp_unit_die->addr_base ();
6176
6177 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6178 We need the value before we can process DW_AT_ranges values from the
6179 DWO. */
6180 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6181
6182 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6183 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6184 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6185 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6186 section. */
6187 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6188 }
6189 else if (stub_comp_dir != NULL)
6190 {
6191 /* Reconstruct the comp_dir attribute to simplify the code below. */
6192 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6193 comp_dir->name = DW_AT_comp_dir;
6194 comp_dir->form = DW_FORM_string;
6195 comp_dir->set_string_noncanonical (stub_comp_dir);
6196 }
6197
6198 /* Set up for reading the DWO CU/TU. */
6199 cu->dwo_unit = dwo_unit;
6200 dwarf2_section_info *section = dwo_unit->section;
6201 section->read (objfile);
6202 abfd = section->get_bfd_owner ();
6203 begin_info_ptr = info_ptr = (section->buffer
6204 + to_underlying (dwo_unit->sect_off));
6205 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6206
6207 if (per_cu->is_debug_types)
6208 {
6209 signatured_type *sig_type = (struct signatured_type *) per_cu;
6210
6211 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6212 section, dwo_abbrev_section,
6213 info_ptr, rcuh_kind::TYPE);
6214 /* This is not an assert because it can be caused by bad debug info. */
6215 if (sig_type->signature != cu->header.signature)
6216 {
6217 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6218 " TU at offset %s [in module %s]"),
6219 hex_string (sig_type->signature),
6220 hex_string (cu->header.signature),
6221 sect_offset_str (dwo_unit->sect_off),
6222 bfd_get_filename (abfd));
6223 }
6224 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6225 /* For DWOs coming from DWP files, we don't know the CU length
6226 nor the type's offset in the TU until now. */
6227 dwo_unit->length = cu->header.get_length ();
6228 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6229
6230 /* Establish the type offset that can be used to lookup the type.
6231 For DWO files, we don't know it until now. */
6232 sig_type->type_offset_in_section
6233 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6234 }
6235 else
6236 {
6237 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6238 section, dwo_abbrev_section,
6239 info_ptr, rcuh_kind::COMPILE);
6240 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6241 /* For DWOs coming from DWP files, we don't know the CU length
6242 until now. */
6243 dwo_unit->length = cu->header.get_length ();
6244 }
6245
6246 dwo_abbrev_section->read (objfile);
6247 *result_dwo_abbrev_table
6248 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6249 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6250 result_dwo_abbrev_table->get ());
6251
6252 /* Read in the die, but leave space to copy over the attributes
6253 from the stub. This has the benefit of simplifying the rest of
6254 the code - all the work to maintain the illusion of a single
6255 DW_TAG_{compile,type}_unit DIE is done here. */
6256 num_extra_attrs = ((stmt_list != NULL)
6257 + (low_pc != NULL)
6258 + (high_pc != NULL)
6259 + (ranges != NULL)
6260 + (comp_dir != NULL));
6261 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6262 num_extra_attrs);
6263
6264 /* Copy over the attributes from the stub to the DIE we just read in. */
6265 comp_unit_die = *result_comp_unit_die;
6266 i = comp_unit_die->num_attrs;
6267 if (stmt_list != NULL)
6268 comp_unit_die->attrs[i++] = *stmt_list;
6269 if (low_pc != NULL)
6270 comp_unit_die->attrs[i++] = *low_pc;
6271 if (high_pc != NULL)
6272 comp_unit_die->attrs[i++] = *high_pc;
6273 if (ranges != NULL)
6274 comp_unit_die->attrs[i++] = *ranges;
6275 if (comp_dir != NULL)
6276 comp_unit_die->attrs[i++] = *comp_dir;
6277 comp_unit_die->num_attrs += num_extra_attrs;
6278
6279 if (dwarf_die_debug)
6280 {
6281 fprintf_unfiltered (gdb_stdlog,
6282 "Read die from %s@0x%x of %s:\n",
6283 section->get_name (),
6284 (unsigned) (begin_info_ptr - section->buffer),
6285 bfd_get_filename (abfd));
6286 dump_die (comp_unit_die, dwarf_die_debug);
6287 }
6288
6289 /* Skip dummy compilation units. */
6290 if (info_ptr >= begin_info_ptr + dwo_unit->length
6291 || peek_abbrev_code (abfd, info_ptr) == 0)
6292 return 0;
6293
6294 *result_info_ptr = info_ptr;
6295 return 1;
6296 }
6297
6298 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6299 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6300 signature is part of the header. */
6301 static gdb::optional<ULONGEST>
6302 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6303 {
6304 if (cu->header.version >= 5)
6305 return cu->header.signature;
6306 struct attribute *attr;
6307 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6308 if (attr == nullptr || !attr->form_is_unsigned ())
6309 return gdb::optional<ULONGEST> ();
6310 return attr->as_unsigned ();
6311 }
6312
6313 /* Subroutine of cutu_reader to simplify it.
6314 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6315 Returns NULL if the specified DWO unit cannot be found. */
6316
6317 static struct dwo_unit *
6318 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6319 {
6320 dwarf2_per_cu_data *per_cu = cu->per_cu;
6321 struct dwo_unit *dwo_unit;
6322 const char *comp_dir;
6323
6324 gdb_assert (cu != NULL);
6325
6326 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6327 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6328 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6329
6330 if (per_cu->is_debug_types)
6331 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6332 else
6333 {
6334 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6335
6336 if (!signature.has_value ())
6337 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6338 " [in module %s]"),
6339 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6340
6341 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6342 }
6343
6344 return dwo_unit;
6345 }
6346
6347 /* Subroutine of cutu_reader to simplify it.
6348 See it for a description of the parameters.
6349 Read a TU directly from a DWO file, bypassing the stub. */
6350
6351 void
6352 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6353 dwarf2_per_objfile *per_objfile,
6354 dwarf2_cu *existing_cu)
6355 {
6356 struct signatured_type *sig_type;
6357
6358 /* Verify we can do the following downcast, and that we have the
6359 data we need. */
6360 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6361 sig_type = (struct signatured_type *) this_cu;
6362 gdb_assert (sig_type->dwo_unit != NULL);
6363
6364 dwarf2_cu *cu;
6365
6366 if (existing_cu != nullptr)
6367 {
6368 cu = existing_cu;
6369 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6370 /* There's no need to do the rereading_dwo_cu handling that
6371 cutu_reader does since we don't read the stub. */
6372 }
6373 else
6374 {
6375 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6376 in per_objfile yet. */
6377 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6378 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6379 cu = m_new_cu.get ();
6380 }
6381
6382 /* A future optimization, if needed, would be to use an existing
6383 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6384 could share abbrev tables. */
6385
6386 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6387 NULL /* stub_comp_unit_die */,
6388 sig_type->dwo_unit->dwo_file->comp_dir,
6389 this, &info_ptr,
6390 &comp_unit_die,
6391 &m_dwo_abbrev_table) == 0)
6392 {
6393 /* Dummy die. */
6394 dummy_p = true;
6395 }
6396 }
6397
6398 /* Initialize a CU (or TU) and read its DIEs.
6399 If the CU defers to a DWO file, read the DWO file as well.
6400
6401 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6402 Otherwise the table specified in the comp unit header is read in and used.
6403 This is an optimization for when we already have the abbrev table.
6404
6405 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6406 allocated. */
6407
6408 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6409 dwarf2_per_objfile *per_objfile,
6410 struct abbrev_table *abbrev_table,
6411 dwarf2_cu *existing_cu,
6412 bool skip_partial)
6413 : die_reader_specs {},
6414 m_this_cu (this_cu)
6415 {
6416 struct objfile *objfile = per_objfile->objfile;
6417 struct dwarf2_section_info *section = this_cu->section;
6418 bfd *abfd = section->get_bfd_owner ();
6419 const gdb_byte *begin_info_ptr;
6420 struct signatured_type *sig_type = NULL;
6421 struct dwarf2_section_info *abbrev_section;
6422 /* Non-zero if CU currently points to a DWO file and we need to
6423 reread it. When this happens we need to reread the skeleton die
6424 before we can reread the DWO file (this only applies to CUs, not TUs). */
6425 int rereading_dwo_cu = 0;
6426
6427 if (dwarf_die_debug)
6428 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6429 this_cu->is_debug_types ? "type" : "comp",
6430 sect_offset_str (this_cu->sect_off));
6431
6432 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6433 file (instead of going through the stub), short-circuit all of this. */
6434 if (this_cu->reading_dwo_directly)
6435 {
6436 /* Narrow down the scope of possibilities to have to understand. */
6437 gdb_assert (this_cu->is_debug_types);
6438 gdb_assert (abbrev_table == NULL);
6439 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6440 return;
6441 }
6442
6443 /* This is cheap if the section is already read in. */
6444 section->read (objfile);
6445
6446 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6447
6448 abbrev_section = get_abbrev_section_for_cu (this_cu);
6449
6450 dwarf2_cu *cu;
6451
6452 if (existing_cu != nullptr)
6453 {
6454 cu = existing_cu;
6455 /* If this CU is from a DWO file we need to start over, we need to
6456 refetch the attributes from the skeleton CU.
6457 This could be optimized by retrieving those attributes from when we
6458 were here the first time: the previous comp_unit_die was stored in
6459 comp_unit_obstack. But there's no data yet that we need this
6460 optimization. */
6461 if (cu->dwo_unit != NULL)
6462 rereading_dwo_cu = 1;
6463 }
6464 else
6465 {
6466 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6467 in per_objfile yet. */
6468 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6469 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6470 cu = m_new_cu.get ();
6471 }
6472
6473 /* Get the header. */
6474 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6475 {
6476 /* We already have the header, there's no need to read it in again. */
6477 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6478 }
6479 else
6480 {
6481 if (this_cu->is_debug_types)
6482 {
6483 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6484 section, abbrev_section,
6485 info_ptr, rcuh_kind::TYPE);
6486
6487 /* Since per_cu is the first member of struct signatured_type,
6488 we can go from a pointer to one to a pointer to the other. */
6489 sig_type = (struct signatured_type *) this_cu;
6490 gdb_assert (sig_type->signature == cu->header.signature);
6491 gdb_assert (sig_type->type_offset_in_tu
6492 == cu->header.type_cu_offset_in_tu);
6493 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6494
6495 /* LENGTH has not been set yet for type units if we're
6496 using .gdb_index. */
6497 this_cu->length = cu->header.get_length ();
6498
6499 /* Establish the type offset that can be used to lookup the type. */
6500 sig_type->type_offset_in_section =
6501 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6502
6503 this_cu->dwarf_version = cu->header.version;
6504 }
6505 else
6506 {
6507 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6508 section, abbrev_section,
6509 info_ptr,
6510 rcuh_kind::COMPILE);
6511
6512 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6513 if (this_cu->length == 0)
6514 this_cu->length = cu->header.get_length ();
6515 else
6516 gdb_assert (this_cu->length == cu->header.get_length ());
6517 this_cu->dwarf_version = cu->header.version;
6518 }
6519 }
6520
6521 /* Skip dummy compilation units. */
6522 if (info_ptr >= begin_info_ptr + this_cu->length
6523 || peek_abbrev_code (abfd, info_ptr) == 0)
6524 {
6525 dummy_p = true;
6526 return;
6527 }
6528
6529 /* If we don't have them yet, read the abbrevs for this compilation unit.
6530 And if we need to read them now, make sure they're freed when we're
6531 done. */
6532 if (abbrev_table != NULL)
6533 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6534 else
6535 {
6536 abbrev_section->read (objfile);
6537 m_abbrev_table_holder
6538 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6539 abbrev_table = m_abbrev_table_holder.get ();
6540 }
6541
6542 /* Read the top level CU/TU die. */
6543 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6544 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6545
6546 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6547 {
6548 dummy_p = true;
6549 return;
6550 }
6551
6552 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6553 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6554 table from the DWO file and pass the ownership over to us. It will be
6555 referenced from READER, so we must make sure to free it after we're done
6556 with READER.
6557
6558 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6559 DWO CU, that this test will fail (the attribute will not be present). */
6560 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6561 if (dwo_name != nullptr)
6562 {
6563 struct dwo_unit *dwo_unit;
6564 struct die_info *dwo_comp_unit_die;
6565
6566 if (comp_unit_die->has_children)
6567 {
6568 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6569 " has children (offset %s) [in module %s]"),
6570 sect_offset_str (this_cu->sect_off),
6571 bfd_get_filename (abfd));
6572 }
6573 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6574 if (dwo_unit != NULL)
6575 {
6576 if (read_cutu_die_from_dwo (cu, dwo_unit,
6577 comp_unit_die, NULL,
6578 this, &info_ptr,
6579 &dwo_comp_unit_die,
6580 &m_dwo_abbrev_table) == 0)
6581 {
6582 /* Dummy die. */
6583 dummy_p = true;
6584 return;
6585 }
6586 comp_unit_die = dwo_comp_unit_die;
6587 }
6588 else
6589 {
6590 /* Yikes, we couldn't find the rest of the DIE, we only have
6591 the stub. A complaint has already been logged. There's
6592 not much more we can do except pass on the stub DIE to
6593 die_reader_func. We don't want to throw an error on bad
6594 debug info. */
6595 }
6596 }
6597 }
6598
6599 void
6600 cutu_reader::keep ()
6601 {
6602 /* Done, clean up. */
6603 gdb_assert (!dummy_p);
6604 if (m_new_cu != NULL)
6605 {
6606 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6607 now. */
6608 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6609 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6610 }
6611 }
6612
6613 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6614 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6615 assumed to have already done the lookup to find the DWO file).
6616
6617 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6618 THIS_CU->is_debug_types, but nothing else.
6619
6620 We fill in THIS_CU->length.
6621
6622 THIS_CU->cu is always freed when done.
6623 This is done in order to not leave THIS_CU->cu in a state where we have
6624 to care whether it refers to the "main" CU or the DWO CU.
6625
6626 When parent_cu is passed, it is used to provide a default value for
6627 str_offsets_base and addr_base from the parent. */
6628
6629 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6630 dwarf2_per_objfile *per_objfile,
6631 struct dwarf2_cu *parent_cu,
6632 struct dwo_file *dwo_file)
6633 : die_reader_specs {},
6634 m_this_cu (this_cu)
6635 {
6636 struct objfile *objfile = per_objfile->objfile;
6637 struct dwarf2_section_info *section = this_cu->section;
6638 bfd *abfd = section->get_bfd_owner ();
6639 struct dwarf2_section_info *abbrev_section;
6640 const gdb_byte *begin_info_ptr, *info_ptr;
6641
6642 if (dwarf_die_debug)
6643 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6644 this_cu->is_debug_types ? "type" : "comp",
6645 sect_offset_str (this_cu->sect_off));
6646
6647 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6648
6649 abbrev_section = (dwo_file != NULL
6650 ? &dwo_file->sections.abbrev
6651 : get_abbrev_section_for_cu (this_cu));
6652
6653 /* This is cheap if the section is already read in. */
6654 section->read (objfile);
6655
6656 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6657
6658 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6659 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6660 section, abbrev_section, info_ptr,
6661 (this_cu->is_debug_types
6662 ? rcuh_kind::TYPE
6663 : rcuh_kind::COMPILE));
6664
6665 if (parent_cu != nullptr)
6666 {
6667 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6668 m_new_cu->addr_base = parent_cu->addr_base;
6669 }
6670 this_cu->length = m_new_cu->header.get_length ();
6671
6672 /* Skip dummy compilation units. */
6673 if (info_ptr >= begin_info_ptr + this_cu->length
6674 || peek_abbrev_code (abfd, info_ptr) == 0)
6675 {
6676 dummy_p = true;
6677 return;
6678 }
6679
6680 abbrev_section->read (objfile);
6681 m_abbrev_table_holder
6682 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6683
6684 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6685 m_abbrev_table_holder.get ());
6686 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6687 }
6688
6689 \f
6690 /* Type Unit Groups.
6691
6692 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6693 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6694 so that all types coming from the same compilation (.o file) are grouped
6695 together. A future step could be to put the types in the same symtab as
6696 the CU the types ultimately came from. */
6697
6698 static hashval_t
6699 hash_type_unit_group (const void *item)
6700 {
6701 const struct type_unit_group *tu_group
6702 = (const struct type_unit_group *) item;
6703
6704 return hash_stmt_list_entry (&tu_group->hash);
6705 }
6706
6707 static int
6708 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6709 {
6710 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6711 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6712
6713 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6714 }
6715
6716 /* Allocate a hash table for type unit groups. */
6717
6718 static htab_up
6719 allocate_type_unit_groups_table ()
6720 {
6721 return htab_up (htab_create_alloc (3,
6722 hash_type_unit_group,
6723 eq_type_unit_group,
6724 htab_delete_entry<type_unit_group>,
6725 xcalloc, xfree));
6726 }
6727
6728 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6729 partial symtabs. We combine several TUs per psymtab to not let the size
6730 of any one psymtab grow too big. */
6731 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6732 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6733
6734 /* Helper routine for get_type_unit_group.
6735 Create the type_unit_group object used to hold one or more TUs. */
6736
6737 static std::unique_ptr<type_unit_group>
6738 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6739 {
6740 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6741 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6742
6743 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6744 tu_group->per_bfd = per_bfd;
6745
6746 if (per_bfd->using_index)
6747 {
6748 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6749 struct dwarf2_per_cu_quick_data);
6750 }
6751 else
6752 {
6753 unsigned int line_offset = to_underlying (line_offset_struct);
6754 dwarf2_psymtab *pst;
6755 std::string name;
6756
6757 /* Give the symtab a useful name for debug purposes. */
6758 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6759 name = string_printf ("<type_units_%d>",
6760 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6761 else
6762 name = string_printf ("<type_units_at_0x%x>", line_offset);
6763
6764 pst = create_partial_symtab (tu_group.get (), per_objfile,
6765 name.c_str ());
6766 pst->anonymous = true;
6767 }
6768
6769 tu_group->hash.dwo_unit = cu->dwo_unit;
6770 tu_group->hash.line_sect_off = line_offset_struct;
6771
6772 return tu_group;
6773 }
6774
6775 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6776 STMT_LIST is a DW_AT_stmt_list attribute. */
6777
6778 static struct type_unit_group *
6779 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6780 {
6781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6782 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6783 struct type_unit_group *tu_group;
6784 void **slot;
6785 unsigned int line_offset;
6786 struct type_unit_group type_unit_group_for_lookup;
6787
6788 if (per_objfile->per_bfd->type_unit_groups == NULL)
6789 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6790
6791 /* Do we need to create a new group, or can we use an existing one? */
6792
6793 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6794 {
6795 line_offset = stmt_list->as_unsigned ();
6796 ++tu_stats->nr_symtab_sharers;
6797 }
6798 else
6799 {
6800 /* Ugh, no stmt_list. Rare, but we have to handle it.
6801 We can do various things here like create one group per TU or
6802 spread them over multiple groups to split up the expansion work.
6803 To avoid worst case scenarios (too many groups or too large groups)
6804 we, umm, group them in bunches. */
6805 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6806 | (tu_stats->nr_stmt_less_type_units
6807 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6808 ++tu_stats->nr_stmt_less_type_units;
6809 }
6810
6811 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6812 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6813 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6814 &type_unit_group_for_lookup, INSERT);
6815 if (*slot == nullptr)
6816 {
6817 sect_offset line_offset_struct = (sect_offset) line_offset;
6818 std::unique_ptr<type_unit_group> grp
6819 = create_type_unit_group (cu, line_offset_struct);
6820 *slot = grp.release ();
6821 ++tu_stats->nr_symtabs;
6822 }
6823
6824 tu_group = (struct type_unit_group *) *slot;
6825 gdb_assert (tu_group != nullptr);
6826 return tu_group;
6827 }
6828 \f
6829 /* Partial symbol tables. */
6830
6831 /* Create a psymtab named NAME and assign it to PER_CU.
6832
6833 The caller must fill in the following details:
6834 dirname, textlow, texthigh. */
6835
6836 static dwarf2_psymtab *
6837 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6838 dwarf2_per_objfile *per_objfile,
6839 const char *name)
6840 {
6841 dwarf2_psymtab *pst
6842 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6843 per_objfile->objfile->per_bfd, per_cu);
6844
6845 pst->psymtabs_addrmap_supported = true;
6846
6847 /* This is the glue that links PST into GDB's symbol API. */
6848 per_cu->v.psymtab = pst;
6849
6850 return pst;
6851 }
6852
6853 /* DIE reader function for process_psymtab_comp_unit. */
6854
6855 static void
6856 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6857 const gdb_byte *info_ptr,
6858 struct die_info *comp_unit_die,
6859 enum language pretend_language)
6860 {
6861 struct dwarf2_cu *cu = reader->cu;
6862 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6863 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6864 struct objfile *objfile = per_objfile->objfile;
6865 struct gdbarch *gdbarch = objfile->arch ();
6866 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6867 CORE_ADDR baseaddr;
6868 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6869 dwarf2_psymtab *pst;
6870 enum pc_bounds_kind cu_bounds_kind;
6871 const char *filename;
6872
6873 gdb_assert (! per_cu->is_debug_types);
6874
6875 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
6876
6877 /* Allocate a new partial symbol table structure. */
6878 gdb::unique_xmalloc_ptr<char> debug_filename;
6879 static const char artificial[] = "<artificial>";
6880 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6881 if (filename == NULL)
6882 filename = "";
6883 else if (strcmp (filename, artificial) == 0)
6884 {
6885 debug_filename.reset (concat (artificial, "@",
6886 sect_offset_str (per_cu->sect_off),
6887 (char *) NULL));
6888 filename = debug_filename.get ();
6889 }
6890
6891 pst = create_partial_symtab (per_cu, per_objfile, filename);
6892
6893 /* This must be done before calling dwarf2_build_include_psymtabs. */
6894 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6895
6896 baseaddr = objfile->text_section_offset ();
6897
6898 dwarf2_find_base_address (comp_unit_die, cu);
6899
6900 /* Possibly set the default values of LOWPC and HIGHPC from
6901 `DW_AT_ranges'. */
6902 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6903 &best_highpc, cu, pst);
6904 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6905 {
6906 CORE_ADDR low
6907 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
6908 - baseaddr);
6909 CORE_ADDR high
6910 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
6911 - baseaddr - 1);
6912 /* Store the contiguous range if it is not empty; it can be
6913 empty for CUs with no code. */
6914 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
6915 low, high, pst);
6916 }
6917
6918 /* Check if comp unit has_children.
6919 If so, read the rest of the partial symbols from this comp unit.
6920 If not, there's no more debug_info for this comp unit. */
6921 if (comp_unit_die->has_children)
6922 {
6923 struct partial_die_info *first_die;
6924 CORE_ADDR lowpc, highpc;
6925
6926 lowpc = ((CORE_ADDR) -1);
6927 highpc = ((CORE_ADDR) 0);
6928
6929 first_die = load_partial_dies (reader, info_ptr, 1);
6930
6931 scan_partial_symbols (first_die, &lowpc, &highpc,
6932 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6933
6934 /* If we didn't find a lowpc, set it to highpc to avoid
6935 complaints from `maint check'. */
6936 if (lowpc == ((CORE_ADDR) -1))
6937 lowpc = highpc;
6938
6939 /* If the compilation unit didn't have an explicit address range,
6940 then use the information extracted from its child dies. */
6941 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6942 {
6943 best_lowpc = lowpc;
6944 best_highpc = highpc;
6945 }
6946 }
6947 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
6948 best_lowpc + baseaddr)
6949 - baseaddr);
6950 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
6951 best_highpc + baseaddr)
6952 - baseaddr);
6953
6954 pst->end ();
6955
6956 if (!cu->per_cu->imported_symtabs_empty ())
6957 {
6958 int i;
6959 int len = cu->per_cu->imported_symtabs_size ();
6960
6961 /* Fill in 'dependencies' here; we fill in 'users' in a
6962 post-pass. */
6963 pst->number_of_dependencies = len;
6964 pst->dependencies
6965 = per_bfd->partial_symtabs->allocate_dependencies (len);
6966 for (i = 0; i < len; ++i)
6967 {
6968 pst->dependencies[i]
6969 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
6970 }
6971
6972 cu->per_cu->imported_symtabs_free ();
6973 }
6974
6975 /* Get the list of files included in the current compilation unit,
6976 and build a psymtab for each of them. */
6977 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6978
6979 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
6980 ", %d global, %d static syms",
6981 per_cu->is_debug_types ? "type" : "comp",
6982 sect_offset_str (per_cu->sect_off),
6983 paddress (gdbarch, pst->text_low (objfile)),
6984 paddress (gdbarch, pst->text_high (objfile)),
6985 (int) pst->global_psymbols.size (),
6986 (int) pst->static_psymbols.size ());
6987 }
6988
6989 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6990 Process compilation unit THIS_CU for a psymtab. */
6991
6992 static void
6993 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
6994 dwarf2_per_objfile *per_objfile,
6995 bool want_partial_unit,
6996 enum language pretend_language)
6997 {
6998 /* If this compilation unit was already read in, free the
6999 cached copy in order to read it in again. This is
7000 necessary because we skipped some symbols when we first
7001 read in the compilation unit (see load_partial_dies).
7002 This problem could be avoided, but the benefit is unclear. */
7003 per_objfile->remove_cu (this_cu);
7004
7005 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7006
7007 if (reader.comp_unit_die == nullptr)
7008 return;
7009
7010 switch (reader.comp_unit_die->tag)
7011 {
7012 case DW_TAG_compile_unit:
7013 this_cu->unit_type = DW_UT_compile;
7014 break;
7015 case DW_TAG_partial_unit:
7016 this_cu->unit_type = DW_UT_partial;
7017 break;
7018 case DW_TAG_type_unit:
7019 this_cu->unit_type = DW_UT_type;
7020 break;
7021 default:
7022 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7023 dwarf_tag_name (reader.comp_unit_die->tag),
7024 sect_offset_str (reader.cu->per_cu->sect_off),
7025 objfile_name (per_objfile->objfile));
7026 }
7027
7028 if (reader.dummy_p)
7029 {
7030 /* Nothing. */
7031 }
7032 else if (this_cu->is_debug_types)
7033 build_type_psymtabs_reader (&reader, reader.info_ptr,
7034 reader.comp_unit_die);
7035 else if (want_partial_unit
7036 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7037 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7038 reader.comp_unit_die,
7039 pretend_language);
7040
7041 /* Age out any secondary CUs. */
7042 per_objfile->age_comp_units ();
7043 }
7044
7045 /* Reader function for build_type_psymtabs. */
7046
7047 static void
7048 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7049 const gdb_byte *info_ptr,
7050 struct die_info *type_unit_die)
7051 {
7052 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7053 struct dwarf2_cu *cu = reader->cu;
7054 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7055 struct signatured_type *sig_type;
7056 struct type_unit_group *tu_group;
7057 struct attribute *attr;
7058 struct partial_die_info *first_die;
7059 CORE_ADDR lowpc, highpc;
7060 dwarf2_psymtab *pst;
7061
7062 gdb_assert (per_cu->is_debug_types);
7063 sig_type = (struct signatured_type *) per_cu;
7064
7065 if (! type_unit_die->has_children)
7066 return;
7067
7068 attr = type_unit_die->attr (DW_AT_stmt_list);
7069 tu_group = get_type_unit_group (cu, attr);
7070
7071 if (tu_group->tus == nullptr)
7072 tu_group->tus = new std::vector<signatured_type *>;
7073 tu_group->tus->push_back (sig_type);
7074
7075 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7076 pst = create_partial_symtab (per_cu, per_objfile, "");
7077 pst->anonymous = true;
7078
7079 first_die = load_partial_dies (reader, info_ptr, 1);
7080
7081 lowpc = (CORE_ADDR) -1;
7082 highpc = (CORE_ADDR) 0;
7083 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7084
7085 pst->end ();
7086 }
7087
7088 /* Struct used to sort TUs by their abbreviation table offset. */
7089
7090 struct tu_abbrev_offset
7091 {
7092 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7093 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7094 {}
7095
7096 /* This is used when sorting. */
7097 bool operator< (const tu_abbrev_offset &other) const
7098 {
7099 return abbrev_offset < other.abbrev_offset;
7100 }
7101
7102 signatured_type *sig_type;
7103 sect_offset abbrev_offset;
7104 };
7105
7106 /* Efficiently read all the type units.
7107
7108 The efficiency is because we sort TUs by the abbrev table they use and
7109 only read each abbrev table once. In one program there are 200K TUs
7110 sharing 8K abbrev tables.
7111
7112 The main purpose of this function is to support building the
7113 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7114 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7115 can collapse the search space by grouping them by stmt_list.
7116 The savings can be significant, in the same program from above the 200K TUs
7117 share 8K stmt_list tables.
7118
7119 FUNC is expected to call get_type_unit_group, which will create the
7120 struct type_unit_group if necessary and add it to
7121 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7122
7123 static void
7124 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7125 {
7126 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7127 abbrev_table_up abbrev_table;
7128 sect_offset abbrev_offset;
7129
7130 /* It's up to the caller to not call us multiple times. */
7131 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7132
7133 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7134 return;
7135
7136 /* TUs typically share abbrev tables, and there can be way more TUs than
7137 abbrev tables. Sort by abbrev table to reduce the number of times we
7138 read each abbrev table in.
7139 Alternatives are to punt or to maintain a cache of abbrev tables.
7140 This is simpler and efficient enough for now.
7141
7142 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7143 symtab to use). Typically TUs with the same abbrev offset have the same
7144 stmt_list value too so in practice this should work well.
7145
7146 The basic algorithm here is:
7147
7148 sort TUs by abbrev table
7149 for each TU with same abbrev table:
7150 read abbrev table if first user
7151 read TU top level DIE
7152 [IWBN if DWO skeletons had DW_AT_stmt_list]
7153 call FUNC */
7154
7155 dwarf_read_debug_printf ("Building type unit groups ...");
7156
7157 /* Sort in a separate table to maintain the order of all_comp_units
7158 for .gdb_index: TU indices directly index all_type_units. */
7159 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7160 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7161
7162 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7163 {
7164 if (cu->is_debug_types)
7165 {
7166 auto sig_type = static_cast<signatured_type *> (cu.get ());
7167 sorted_by_abbrev.emplace_back
7168 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7169 sig_type->sect_off));
7170 }
7171 }
7172
7173 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7174
7175 abbrev_offset = (sect_offset) ~(unsigned) 0;
7176
7177 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7178 {
7179 /* Switch to the next abbrev table if necessary. */
7180 if (abbrev_table == NULL
7181 || tu.abbrev_offset != abbrev_offset)
7182 {
7183 abbrev_offset = tu.abbrev_offset;
7184 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7185 abbrev_table =
7186 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7187 ++tu_stats->nr_uniq_abbrev_tables;
7188 }
7189
7190 cutu_reader reader (tu.sig_type, per_objfile,
7191 abbrev_table.get (), nullptr, false);
7192 if (!reader.dummy_p)
7193 build_type_psymtabs_reader (&reader, reader.info_ptr,
7194 reader.comp_unit_die);
7195 }
7196 }
7197
7198 /* Print collected type unit statistics. */
7199
7200 static void
7201 print_tu_stats (dwarf2_per_objfile *per_objfile)
7202 {
7203 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7204
7205 dwarf_read_debug_printf ("Type unit statistics:");
7206 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7207 dwarf_read_debug_printf (" %d uniq abbrev tables",
7208 tu_stats->nr_uniq_abbrev_tables);
7209 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7210 tu_stats->nr_symtabs);
7211 dwarf_read_debug_printf (" %d symtab sharers",
7212 tu_stats->nr_symtab_sharers);
7213 dwarf_read_debug_printf (" %d type units without a stmt_list",
7214 tu_stats->nr_stmt_less_type_units);
7215 dwarf_read_debug_printf (" %d all_type_units reallocs",
7216 tu_stats->nr_all_type_units_reallocs);
7217 }
7218
7219 /* Traversal function for build_type_psymtabs. */
7220
7221 static int
7222 build_type_psymtab_dependencies (void **slot, void *info)
7223 {
7224 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7225 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7226 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7227 dwarf2_psymtab *pst = tu_group->v.psymtab;
7228 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7229 int i;
7230
7231 gdb_assert (len > 0);
7232 gdb_assert (tu_group->type_unit_group_p ());
7233
7234 pst->number_of_dependencies = len;
7235 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7236 for (i = 0; i < len; ++i)
7237 {
7238 struct signatured_type *iter = tu_group->tus->at (i);
7239 gdb_assert (iter->is_debug_types);
7240 pst->dependencies[i] = iter->v.psymtab;
7241 iter->type_unit_group = tu_group;
7242 }
7243
7244 delete tu_group->tus;
7245 tu_group->tus = nullptr;
7246
7247 return 1;
7248 }
7249
7250 /* Traversal function for process_skeletonless_type_unit.
7251 Read a TU in a DWO file and build partial symbols for it. */
7252
7253 static int
7254 process_skeletonless_type_unit (void **slot, void *info)
7255 {
7256 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7257 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7258
7259 /* If this TU doesn't exist in the global table, add it and read it in. */
7260
7261 if (per_objfile->per_bfd->signatured_types == NULL)
7262 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7263
7264 signatured_type find_entry (dwo_unit->signature);
7265 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7266 &find_entry, INSERT);
7267 /* If we've already seen this type there's nothing to do. What's happening
7268 is we're doing our own version of comdat-folding here. */
7269 if (*slot != NULL)
7270 return 1;
7271
7272 /* This does the job that create_all_comp_units would have done for
7273 this TU. */
7274 signatured_type *entry
7275 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7276 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7277 *slot = entry;
7278
7279 /* This does the job that build_type_psymtabs would have done. */
7280 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7281 if (!reader.dummy_p)
7282 build_type_psymtabs_reader (&reader, reader.info_ptr,
7283 reader.comp_unit_die);
7284
7285 return 1;
7286 }
7287
7288 /* Traversal function for process_skeletonless_type_units. */
7289
7290 static int
7291 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7292 {
7293 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7294
7295 if (dwo_file->tus != NULL)
7296 htab_traverse_noresize (dwo_file->tus.get (),
7297 process_skeletonless_type_unit, info);
7298
7299 return 1;
7300 }
7301
7302 /* Scan all TUs of DWO files, verifying we've processed them.
7303 This is needed in case a TU was emitted without its skeleton.
7304 Note: This can't be done until we know what all the DWO files are. */
7305
7306 static void
7307 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7308 {
7309 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7310 if (get_dwp_file (per_objfile) == NULL
7311 && per_objfile->per_bfd->dwo_files != NULL)
7312 {
7313 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7314 process_dwo_file_for_skeletonless_type_units,
7315 per_objfile);
7316 }
7317 }
7318
7319 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7320
7321 static void
7322 set_partial_user (dwarf2_per_objfile *per_objfile)
7323 {
7324 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7325 {
7326 dwarf2_psymtab *pst = per_cu->v.psymtab;
7327
7328 if (pst == NULL)
7329 continue;
7330
7331 for (int j = 0; j < pst->number_of_dependencies; ++j)
7332 {
7333 /* Set the 'user' field only if it is not already set. */
7334 if (pst->dependencies[j]->user == NULL)
7335 pst->dependencies[j]->user = pst;
7336 }
7337 }
7338 }
7339
7340 /* Build the partial symbol table by doing a quick pass through the
7341 .debug_info and .debug_abbrev sections. */
7342
7343 static void
7344 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7345 {
7346 struct objfile *objfile = per_objfile->objfile;
7347 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7348
7349 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7350 objfile_name (objfile));
7351
7352 scoped_restore restore_reading_psyms
7353 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7354
7355 per_bfd->info.read (objfile);
7356
7357 /* Any cached compilation units will be linked by the per-objfile
7358 read_in_chain. Make sure to free them when we're done. */
7359 free_cached_comp_units freer (per_objfile);
7360
7361 create_all_comp_units (per_objfile);
7362 build_type_psymtabs (per_objfile);
7363
7364 /* Create a temporary address map on a temporary obstack. We later
7365 copy this to the final obstack. */
7366 auto_obstack temp_obstack;
7367
7368 scoped_restore save_psymtabs_addrmap
7369 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7370 addrmap_create_mutable (&temp_obstack));
7371
7372 for (const auto &per_cu : per_bfd->all_comp_units)
7373 {
7374 if (per_cu->v.psymtab != NULL)
7375 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7376 continue;
7377 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7378 language_minimal);
7379 }
7380
7381 /* This has to wait until we read the CUs, we need the list of DWOs. */
7382 process_skeletonless_type_units (per_objfile);
7383
7384 /* Now that all TUs have been processed we can fill in the dependencies. */
7385 if (per_bfd->type_unit_groups != NULL)
7386 {
7387 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7388 build_type_psymtab_dependencies, per_objfile);
7389 }
7390
7391 if (dwarf_read_debug > 0)
7392 print_tu_stats (per_objfile);
7393
7394 set_partial_user (per_objfile);
7395
7396 per_bfd->partial_symtabs->psymtabs_addrmap
7397 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7398 per_bfd->partial_symtabs->obstack ());
7399 /* At this point we want to keep the address map. */
7400 save_psymtabs_addrmap.release ();
7401
7402 dwarf_read_debug_printf ("Done building psymtabs of %s",
7403 objfile_name (objfile));
7404 }
7405
7406 /* Load the partial DIEs for a secondary CU into memory.
7407 This is also used when rereading a primary CU with load_all_dies. */
7408
7409 static void
7410 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7411 dwarf2_per_objfile *per_objfile,
7412 dwarf2_cu *existing_cu)
7413 {
7414 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7415
7416 if (!reader.dummy_p)
7417 {
7418 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7419 language_minimal);
7420
7421 /* Check if comp unit has_children.
7422 If so, read the rest of the partial symbols from this comp unit.
7423 If not, there's no more debug_info for this comp unit. */
7424 if (reader.comp_unit_die->has_children)
7425 load_partial_dies (&reader, reader.info_ptr, 0);
7426
7427 reader.keep ();
7428 }
7429 }
7430
7431 static void
7432 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7433 struct dwarf2_section_info *section,
7434 struct dwarf2_section_info *abbrev_section,
7435 unsigned int is_dwz,
7436 htab_up &types_htab,
7437 rcuh_kind section_kind)
7438 {
7439 const gdb_byte *info_ptr;
7440 struct objfile *objfile = per_objfile->objfile;
7441
7442 dwarf_read_debug_printf ("Reading %s for %s",
7443 section->get_name (),
7444 section->get_file_name ());
7445
7446 section->read (objfile);
7447
7448 info_ptr = section->buffer;
7449
7450 while (info_ptr < section->buffer + section->size)
7451 {
7452 dwarf2_per_cu_data_up this_cu;
7453
7454 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7455
7456 comp_unit_head cu_header;
7457 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7458 abbrev_section, info_ptr,
7459 section_kind);
7460
7461 /* Save the compilation unit for later lookup. */
7462 if (cu_header.unit_type != DW_UT_type)
7463 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7464 else
7465 {
7466 if (types_htab == nullptr)
7467 types_htab = allocate_signatured_type_table ();
7468
7469 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7470 (cu_header.signature);
7471 signatured_type *sig_ptr = sig_type.get ();
7472 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7473 this_cu.reset (sig_type.release ());
7474
7475 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7476 gdb_assert (slot != nullptr);
7477 if (*slot != nullptr)
7478 complaint (_("debug type entry at offset %s is duplicate to"
7479 " the entry at offset %s, signature %s"),
7480 sect_offset_str (sect_off),
7481 sect_offset_str (sig_ptr->sect_off),
7482 hex_string (sig_ptr->signature));
7483 *slot = sig_ptr;
7484 }
7485 this_cu->sect_off = sect_off;
7486 this_cu->length = cu_header.length + cu_header.initial_length_size;
7487 this_cu->is_dwz = is_dwz;
7488 this_cu->section = section;
7489
7490 info_ptr = info_ptr + this_cu->length;
7491 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7492 }
7493 }
7494
7495 /* Create a list of all compilation units in OBJFILE.
7496 This is only done for -readnow and building partial symtabs. */
7497
7498 static void
7499 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7500 {
7501 htab_up types_htab;
7502
7503 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7504 &per_objfile->per_bfd->abbrev, 0,
7505 types_htab, rcuh_kind::COMPILE);
7506 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7507 read_comp_units_from_section (per_objfile, &section,
7508 &per_objfile->per_bfd->abbrev, 0,
7509 types_htab, rcuh_kind::TYPE);
7510
7511 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7512 if (dwz != NULL)
7513 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7514 types_htab, rcuh_kind::COMPILE);
7515
7516 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7517 }
7518
7519 /* Process all loaded DIEs for compilation unit CU, starting at
7520 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7521 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7522 DW_AT_ranges). See the comments of add_partial_subprogram on how
7523 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7524
7525 static void
7526 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7527 CORE_ADDR *highpc, int set_addrmap,
7528 struct dwarf2_cu *cu)
7529 {
7530 struct partial_die_info *pdi;
7531
7532 /* Now, march along the PDI's, descending into ones which have
7533 interesting children but skipping the children of the other ones,
7534 until we reach the end of the compilation unit. */
7535
7536 pdi = first_die;
7537
7538 while (pdi != NULL)
7539 {
7540 pdi->fixup (cu);
7541
7542 /* Anonymous namespaces or modules have no name but have interesting
7543 children, so we need to look at them. Ditto for anonymous
7544 enums. */
7545
7546 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7547 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7548 || pdi->tag == DW_TAG_imported_unit
7549 || pdi->tag == DW_TAG_inlined_subroutine)
7550 {
7551 switch (pdi->tag)
7552 {
7553 case DW_TAG_subprogram:
7554 case DW_TAG_inlined_subroutine:
7555 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7556 if (cu->per_cu->lang == language_cplus)
7557 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7558 set_addrmap, cu);
7559 break;
7560 case DW_TAG_constant:
7561 case DW_TAG_variable:
7562 case DW_TAG_typedef:
7563 case DW_TAG_union_type:
7564 if (!pdi->is_declaration
7565 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7566 {
7567 add_partial_symbol (pdi, cu);
7568 }
7569 break;
7570 case DW_TAG_class_type:
7571 case DW_TAG_interface_type:
7572 case DW_TAG_structure_type:
7573 if (!pdi->is_declaration)
7574 {
7575 add_partial_symbol (pdi, cu);
7576 }
7577 if ((cu->per_cu->lang == language_rust
7578 || cu->per_cu->lang == language_cplus)
7579 && pdi->has_children)
7580 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7581 set_addrmap, cu);
7582 break;
7583 case DW_TAG_enumeration_type:
7584 if (!pdi->is_declaration)
7585 add_partial_enumeration (pdi, cu);
7586 break;
7587 case DW_TAG_base_type:
7588 case DW_TAG_subrange_type:
7589 /* File scope base type definitions are added to the partial
7590 symbol table. */
7591 add_partial_symbol (pdi, cu);
7592 break;
7593 case DW_TAG_namespace:
7594 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7595 break;
7596 case DW_TAG_module:
7597 if (!pdi->is_declaration)
7598 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7599 break;
7600 case DW_TAG_imported_unit:
7601 {
7602 struct dwarf2_per_cu_data *per_cu;
7603
7604 /* For now we don't handle imported units in type units. */
7605 if (cu->per_cu->is_debug_types)
7606 {
7607 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7608 " supported in type units [in module %s]"),
7609 objfile_name (cu->per_objfile->objfile));
7610 }
7611
7612 per_cu = dwarf2_find_containing_comp_unit
7613 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
7614
7615 /* Go read the partial unit, if needed. */
7616 if (per_cu->v.psymtab == NULL)
7617 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7618 cu->per_cu->lang);
7619
7620 cu->per_cu->imported_symtabs_push (per_cu);
7621 }
7622 break;
7623 case DW_TAG_imported_declaration:
7624 add_partial_symbol (pdi, cu);
7625 break;
7626 default:
7627 break;
7628 }
7629 }
7630
7631 /* If the die has a sibling, skip to the sibling. */
7632
7633 pdi = pdi->die_sibling;
7634 }
7635 }
7636
7637 /* Functions used to compute the fully scoped name of a partial DIE.
7638
7639 Normally, this is simple. For C++, the parent DIE's fully scoped
7640 name is concatenated with "::" and the partial DIE's name.
7641 Enumerators are an exception; they use the scope of their parent
7642 enumeration type, i.e. the name of the enumeration type is not
7643 prepended to the enumerator.
7644
7645 There are two complexities. One is DW_AT_specification; in this
7646 case "parent" means the parent of the target of the specification,
7647 instead of the direct parent of the DIE. The other is compilers
7648 which do not emit DW_TAG_namespace; in this case we try to guess
7649 the fully qualified name of structure types from their members'
7650 linkage names. This must be done using the DIE's children rather
7651 than the children of any DW_AT_specification target. We only need
7652 to do this for structures at the top level, i.e. if the target of
7653 any DW_AT_specification (if any; otherwise the DIE itself) does not
7654 have a parent. */
7655
7656 /* Compute the scope prefix associated with PDI's parent, in
7657 compilation unit CU. The result will be allocated on CU's
7658 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7659 field. NULL is returned if no prefix is necessary. */
7660 static const char *
7661 partial_die_parent_scope (struct partial_die_info *pdi,
7662 struct dwarf2_cu *cu)
7663 {
7664 const char *grandparent_scope;
7665 struct partial_die_info *parent, *real_pdi;
7666
7667 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7668 then this means the parent of the specification DIE. */
7669
7670 real_pdi = pdi;
7671 while (real_pdi->has_specification)
7672 {
7673 auto res = find_partial_die (real_pdi->spec_offset,
7674 real_pdi->spec_is_dwz, cu);
7675 real_pdi = res.pdi;
7676 cu = res.cu;
7677 }
7678
7679 parent = real_pdi->die_parent;
7680 if (parent == NULL)
7681 return NULL;
7682
7683 if (parent->scope_set)
7684 return parent->scope;
7685
7686 parent->fixup (cu);
7687
7688 grandparent_scope = partial_die_parent_scope (parent, cu);
7689
7690 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7691 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7692 Work around this problem here. */
7693 if (cu->per_cu->lang == language_cplus
7694 && parent->tag == DW_TAG_namespace
7695 && strcmp (parent->name (cu), "::") == 0
7696 && grandparent_scope == NULL)
7697 {
7698 parent->scope = NULL;
7699 parent->scope_set = 1;
7700 return NULL;
7701 }
7702
7703 /* Nested subroutines in Fortran get a prefix. */
7704 if (pdi->tag == DW_TAG_enumerator)
7705 /* Enumerators should not get the name of the enumeration as a prefix. */
7706 parent->scope = grandparent_scope;
7707 else if (parent->tag == DW_TAG_namespace
7708 || parent->tag == DW_TAG_module
7709 || parent->tag == DW_TAG_structure_type
7710 || parent->tag == DW_TAG_class_type
7711 || parent->tag == DW_TAG_interface_type
7712 || parent->tag == DW_TAG_union_type
7713 || parent->tag == DW_TAG_enumeration_type
7714 || (cu->per_cu->lang == language_fortran
7715 && parent->tag == DW_TAG_subprogram
7716 && pdi->tag == DW_TAG_subprogram))
7717 {
7718 if (grandparent_scope == NULL)
7719 parent->scope = parent->name (cu);
7720 else
7721 parent->scope = typename_concat (&cu->comp_unit_obstack,
7722 grandparent_scope,
7723 parent->name (cu), 0, cu);
7724 }
7725 else
7726 {
7727 /* FIXME drow/2004-04-01: What should we be doing with
7728 function-local names? For partial symbols, we should probably be
7729 ignoring them. */
7730 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7731 dwarf_tag_name (parent->tag),
7732 sect_offset_str (pdi->sect_off));
7733 parent->scope = grandparent_scope;
7734 }
7735
7736 parent->scope_set = 1;
7737 return parent->scope;
7738 }
7739
7740 /* Return the fully scoped name associated with PDI, from compilation unit
7741 CU. The result will be allocated with malloc. */
7742
7743 static gdb::unique_xmalloc_ptr<char>
7744 partial_die_full_name (struct partial_die_info *pdi,
7745 struct dwarf2_cu *cu)
7746 {
7747 const char *parent_scope;
7748
7749 /* If this is a template instantiation, we can not work out the
7750 template arguments from partial DIEs. So, unfortunately, we have
7751 to go through the full DIEs. At least any work we do building
7752 types here will be reused if full symbols are loaded later. */
7753 if (pdi->has_template_arguments)
7754 {
7755 pdi->fixup (cu);
7756
7757 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7758 {
7759 struct die_info *die;
7760 struct attribute attr;
7761 struct dwarf2_cu *ref_cu = cu;
7762
7763 /* DW_FORM_ref_addr is using section offset. */
7764 attr.name = (enum dwarf_attribute) 0;
7765 attr.form = DW_FORM_ref_addr;
7766 attr.u.unsnd = to_underlying (pdi->sect_off);
7767 die = follow_die_ref (NULL, &attr, &ref_cu);
7768
7769 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7770 }
7771 }
7772
7773 parent_scope = partial_die_parent_scope (pdi, cu);
7774 if (parent_scope == NULL)
7775 return NULL;
7776 else
7777 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7778 pdi->name (cu),
7779 0, cu));
7780 }
7781
7782 static void
7783 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7784 {
7785 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7786 struct objfile *objfile = per_objfile->objfile;
7787 struct gdbarch *gdbarch = objfile->arch ();
7788 CORE_ADDR addr = 0;
7789 const char *actual_name = NULL;
7790 CORE_ADDR baseaddr;
7791
7792 baseaddr = objfile->text_section_offset ();
7793
7794 gdb::unique_xmalloc_ptr<char> built_actual_name
7795 = partial_die_full_name (pdi, cu);
7796 if (built_actual_name != NULL)
7797 actual_name = built_actual_name.get ();
7798
7799 if (actual_name == NULL)
7800 actual_name = pdi->name (cu);
7801
7802 partial_symbol psymbol;
7803 memset (&psymbol, 0, sizeof (psymbol));
7804 psymbol.ginfo.set_language (cu->per_cu->lang,
7805 &objfile->objfile_obstack);
7806 psymbol.ginfo.set_section_index (-1);
7807
7808 /* The code below indicates that the psymbol should be installed by
7809 setting this. */
7810 gdb::optional<psymbol_placement> where;
7811
7812 switch (pdi->tag)
7813 {
7814 case DW_TAG_inlined_subroutine:
7815 case DW_TAG_subprogram:
7816 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7817 - baseaddr);
7818 if (pdi->is_external
7819 || cu->per_cu->lang == language_ada
7820 || (cu->per_cu->lang == language_fortran
7821 && pdi->die_parent != NULL
7822 && pdi->die_parent->tag == DW_TAG_subprogram))
7823 {
7824 /* Normally, only "external" DIEs are part of the global scope.
7825 But in Ada and Fortran, we want to be able to access nested
7826 procedures globally. So all Ada and Fortran subprograms are
7827 stored in the global scope. */
7828 where = psymbol_placement::GLOBAL;
7829 }
7830 else
7831 where = psymbol_placement::STATIC;
7832
7833 psymbol.domain = VAR_DOMAIN;
7834 psymbol.aclass = LOC_BLOCK;
7835 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7836 psymbol.ginfo.value.address = addr;
7837
7838 if (pdi->main_subprogram && actual_name != NULL)
7839 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
7840 break;
7841 case DW_TAG_constant:
7842 psymbol.domain = VAR_DOMAIN;
7843 psymbol.aclass = LOC_STATIC;
7844 where = (pdi->is_external
7845 ? psymbol_placement::GLOBAL
7846 : psymbol_placement::STATIC);
7847 break;
7848 case DW_TAG_variable:
7849 if (pdi->d.locdesc)
7850 addr = decode_locdesc (pdi->d.locdesc, cu);
7851
7852 if (pdi->d.locdesc
7853 && addr == 0
7854 && !per_objfile->per_bfd->has_section_at_zero)
7855 {
7856 /* A global or static variable may also have been stripped
7857 out by the linker if unused, in which case its address
7858 will be nullified; do not add such variables into partial
7859 symbol table then. */
7860 }
7861 else if (pdi->is_external)
7862 {
7863 /* Global Variable.
7864 Don't enter into the minimal symbol tables as there is
7865 a minimal symbol table entry from the ELF symbols already.
7866 Enter into partial symbol table if it has a location
7867 descriptor or a type.
7868 If the location descriptor is missing, new_symbol will create
7869 a LOC_UNRESOLVED symbol, the address of the variable will then
7870 be determined from the minimal symbol table whenever the variable
7871 is referenced.
7872 The address for the partial symbol table entry is not
7873 used by GDB, but it comes in handy for debugging partial symbol
7874 table building. */
7875
7876 if (pdi->d.locdesc || pdi->has_type)
7877 {
7878 psymbol.domain = VAR_DOMAIN;
7879 psymbol.aclass = LOC_STATIC;
7880 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7881 psymbol.ginfo.value.address = addr;
7882 where = psymbol_placement::GLOBAL;
7883 }
7884 }
7885 else
7886 {
7887 int has_loc = pdi->d.locdesc != NULL;
7888
7889 /* Static Variable. Skip symbols whose value we cannot know (those
7890 without location descriptors or constant values). */
7891 if (!has_loc && !pdi->has_const_value)
7892 return;
7893
7894 psymbol.domain = VAR_DOMAIN;
7895 psymbol.aclass = LOC_STATIC;
7896 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7897 if (has_loc)
7898 psymbol.ginfo.value.address = addr;
7899 where = psymbol_placement::STATIC;
7900 }
7901 break;
7902 case DW_TAG_array_type:
7903 case DW_TAG_typedef:
7904 case DW_TAG_base_type:
7905 case DW_TAG_subrange_type:
7906 psymbol.domain = VAR_DOMAIN;
7907 psymbol.aclass = LOC_TYPEDEF;
7908 where = psymbol_placement::STATIC;
7909 break;
7910 case DW_TAG_imported_declaration:
7911 case DW_TAG_namespace:
7912 psymbol.domain = VAR_DOMAIN;
7913 psymbol.aclass = LOC_TYPEDEF;
7914 where = psymbol_placement::GLOBAL;
7915 break;
7916 case DW_TAG_module:
7917 /* With Fortran 77 there might be a "BLOCK DATA" module
7918 available without any name. If so, we skip the module as it
7919 doesn't bring any value. */
7920 if (actual_name != nullptr)
7921 {
7922 psymbol.domain = MODULE_DOMAIN;
7923 psymbol.aclass = LOC_TYPEDEF;
7924 where = psymbol_placement::GLOBAL;
7925 }
7926 break;
7927 case DW_TAG_class_type:
7928 case DW_TAG_interface_type:
7929 case DW_TAG_structure_type:
7930 case DW_TAG_union_type:
7931 case DW_TAG_enumeration_type:
7932 /* Skip external references. The DWARF standard says in the section
7933 about "Structure, Union, and Class Type Entries": "An incomplete
7934 structure, union or class type is represented by a structure,
7935 union or class entry that does not have a byte size attribute
7936 and that has a DW_AT_declaration attribute." */
7937 if (!pdi->has_byte_size && pdi->is_declaration)
7938 return;
7939
7940 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7941 static vs. global. */
7942 psymbol.domain = STRUCT_DOMAIN;
7943 psymbol.aclass = LOC_TYPEDEF;
7944 where = (cu->per_cu->lang == language_cplus
7945 ? psymbol_placement::GLOBAL
7946 : psymbol_placement::STATIC);
7947 break;
7948 case DW_TAG_enumerator:
7949 psymbol.domain = VAR_DOMAIN;
7950 psymbol.aclass = LOC_CONST;
7951 where = (cu->per_cu->lang == language_cplus
7952 ? psymbol_placement::GLOBAL
7953 : psymbol_placement::STATIC);
7954 break;
7955 default:
7956 break;
7957 }
7958
7959 if (where.has_value ())
7960 {
7961 if (built_actual_name != nullptr)
7962 actual_name = objfile->intern (actual_name);
7963 if (pdi->linkage_name == nullptr
7964 || cu->per_cu->lang == language_ada)
7965 psymbol.ginfo.set_linkage_name (actual_name);
7966 else
7967 {
7968 psymbol.ginfo.set_demangled_name (actual_name,
7969 &objfile->objfile_obstack);
7970 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
7971 }
7972 cu->per_cu->v.psymtab->add_psymbol
7973 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
7974 objfile);
7975 }
7976 }
7977
7978 /* Read a partial die corresponding to a namespace; also, add a symbol
7979 corresponding to that namespace to the symbol table. NAMESPACE is
7980 the name of the enclosing namespace. */
7981
7982 static void
7983 add_partial_namespace (struct partial_die_info *pdi,
7984 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7985 int set_addrmap, struct dwarf2_cu *cu)
7986 {
7987 /* Add a symbol for the namespace. */
7988
7989 add_partial_symbol (pdi, cu);
7990
7991 /* Now scan partial symbols in that namespace. */
7992
7993 if (pdi->has_children)
7994 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7995 }
7996
7997 /* Read a partial die corresponding to a Fortran module. */
7998
7999 static void
8000 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8001 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8002 {
8003 /* Add a symbol for the namespace. */
8004
8005 add_partial_symbol (pdi, cu);
8006
8007 /* Now scan partial symbols in that module. */
8008
8009 if (pdi->has_children)
8010 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8011 }
8012
8013 /* Read a partial die corresponding to a subprogram or an inlined
8014 subprogram and create a partial symbol for that subprogram.
8015 When the CU language allows it, this routine also defines a partial
8016 symbol for each nested subprogram that this subprogram contains.
8017 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8018 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8019
8020 PDI may also be a lexical block, in which case we simply search
8021 recursively for subprograms defined inside that lexical block.
8022 Again, this is only performed when the CU language allows this
8023 type of definitions. */
8024
8025 static void
8026 add_partial_subprogram (struct partial_die_info *pdi,
8027 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8028 int set_addrmap, struct dwarf2_cu *cu)
8029 {
8030 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8031 {
8032 if (pdi->has_pc_info)
8033 {
8034 if (pdi->lowpc < *lowpc)
8035 *lowpc = pdi->lowpc;
8036 if (pdi->highpc > *highpc)
8037 *highpc = pdi->highpc;
8038 if (set_addrmap)
8039 {
8040 struct objfile *objfile = cu->per_objfile->objfile;
8041 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8042 struct gdbarch *gdbarch = objfile->arch ();
8043 CORE_ADDR baseaddr;
8044 CORE_ADDR this_highpc;
8045 CORE_ADDR this_lowpc;
8046
8047 baseaddr = objfile->text_section_offset ();
8048 this_lowpc
8049 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8050 pdi->lowpc + baseaddr)
8051 - baseaddr);
8052 this_highpc
8053 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8054 pdi->highpc + baseaddr)
8055 - baseaddr);
8056 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8057 this_lowpc, this_highpc - 1,
8058 cu->per_cu->v.psymtab);
8059 }
8060 }
8061
8062 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8063 {
8064 if (!pdi->is_declaration)
8065 /* Ignore subprogram DIEs that do not have a name, they are
8066 illegal. Do not emit a complaint at this point, we will
8067 do so when we convert this psymtab into a symtab. */
8068 if (pdi->name (cu))
8069 add_partial_symbol (pdi, cu);
8070 }
8071 }
8072
8073 if (! pdi->has_children)
8074 return;
8075
8076 if (cu->per_cu->lang == language_ada
8077 || cu->per_cu->lang == language_fortran)
8078 {
8079 pdi = pdi->die_child;
8080 while (pdi != NULL)
8081 {
8082 pdi->fixup (cu);
8083 if (pdi->tag == DW_TAG_subprogram
8084 || pdi->tag == DW_TAG_inlined_subroutine
8085 || pdi->tag == DW_TAG_lexical_block)
8086 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8087 pdi = pdi->die_sibling;
8088 }
8089 }
8090 }
8091
8092 /* Read a partial die corresponding to an enumeration type. */
8093
8094 static void
8095 add_partial_enumeration (struct partial_die_info *enum_pdi,
8096 struct dwarf2_cu *cu)
8097 {
8098 struct partial_die_info *pdi;
8099
8100 if (enum_pdi->name (cu) != NULL)
8101 add_partial_symbol (enum_pdi, cu);
8102
8103 pdi = enum_pdi->die_child;
8104 while (pdi)
8105 {
8106 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8107 complaint (_("malformed enumerator DIE ignored"));
8108 else
8109 add_partial_symbol (pdi, cu);
8110 pdi = pdi->die_sibling;
8111 }
8112 }
8113
8114 /* Return the initial uleb128 in the die at INFO_PTR. */
8115
8116 static unsigned int
8117 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8118 {
8119 unsigned int bytes_read;
8120
8121 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8122 }
8123
8124 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8125 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8126
8127 Return the corresponding abbrev, or NULL if the number is zero (indicating
8128 an empty DIE). In either case *BYTES_READ will be set to the length of
8129 the initial number. */
8130
8131 static const struct abbrev_info *
8132 peek_die_abbrev (const die_reader_specs &reader,
8133 const gdb_byte *info_ptr, unsigned int *bytes_read)
8134 {
8135 dwarf2_cu *cu = reader.cu;
8136 bfd *abfd = reader.abfd;
8137 unsigned int abbrev_number
8138 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8139
8140 if (abbrev_number == 0)
8141 return NULL;
8142
8143 const abbrev_info *abbrev
8144 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8145 if (!abbrev)
8146 {
8147 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8148 " at offset %s [in module %s]"),
8149 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8150 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8151 }
8152
8153 return abbrev;
8154 }
8155
8156 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8157 Returns a pointer to the end of a series of DIEs, terminated by an empty
8158 DIE. Any children of the skipped DIEs will also be skipped. */
8159
8160 static const gdb_byte *
8161 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8162 {
8163 while (1)
8164 {
8165 unsigned int bytes_read;
8166 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8167 &bytes_read);
8168
8169 if (abbrev == NULL)
8170 return info_ptr + bytes_read;
8171 else
8172 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8173 }
8174 }
8175
8176 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8177 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8178 abbrev corresponding to that skipped uleb128 should be passed in
8179 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8180 children. */
8181
8182 static const gdb_byte *
8183 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8184 const struct abbrev_info *abbrev)
8185 {
8186 unsigned int bytes_read;
8187 struct attribute attr;
8188 bfd *abfd = reader->abfd;
8189 struct dwarf2_cu *cu = reader->cu;
8190 const gdb_byte *buffer = reader->buffer;
8191 const gdb_byte *buffer_end = reader->buffer_end;
8192 unsigned int form, i;
8193
8194 for (i = 0; i < abbrev->num_attrs; i++)
8195 {
8196 /* The only abbrev we care about is DW_AT_sibling. */
8197 if (abbrev->attrs[i].name == DW_AT_sibling)
8198 {
8199 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8200 if (attr.form == DW_FORM_ref_addr)
8201 complaint (_("ignoring absolute DW_AT_sibling"));
8202 else
8203 {
8204 sect_offset off = attr.get_ref_die_offset ();
8205 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8206
8207 if (sibling_ptr < info_ptr)
8208 complaint (_("DW_AT_sibling points backwards"));
8209 else if (sibling_ptr > reader->buffer_end)
8210 reader->die_section->overflow_complaint ();
8211 else
8212 return sibling_ptr;
8213 }
8214 }
8215
8216 /* If it isn't DW_AT_sibling, skip this attribute. */
8217 form = abbrev->attrs[i].form;
8218 skip_attribute:
8219 switch (form)
8220 {
8221 case DW_FORM_ref_addr:
8222 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8223 and later it is offset sized. */
8224 if (cu->header.version == 2)
8225 info_ptr += cu->header.addr_size;
8226 else
8227 info_ptr += cu->header.offset_size;
8228 break;
8229 case DW_FORM_GNU_ref_alt:
8230 info_ptr += cu->header.offset_size;
8231 break;
8232 case DW_FORM_addr:
8233 info_ptr += cu->header.addr_size;
8234 break;
8235 case DW_FORM_data1:
8236 case DW_FORM_ref1:
8237 case DW_FORM_flag:
8238 case DW_FORM_strx1:
8239 info_ptr += 1;
8240 break;
8241 case DW_FORM_flag_present:
8242 case DW_FORM_implicit_const:
8243 break;
8244 case DW_FORM_data2:
8245 case DW_FORM_ref2:
8246 case DW_FORM_strx2:
8247 info_ptr += 2;
8248 break;
8249 case DW_FORM_strx3:
8250 info_ptr += 3;
8251 break;
8252 case DW_FORM_data4:
8253 case DW_FORM_ref4:
8254 case DW_FORM_strx4:
8255 info_ptr += 4;
8256 break;
8257 case DW_FORM_data8:
8258 case DW_FORM_ref8:
8259 case DW_FORM_ref_sig8:
8260 info_ptr += 8;
8261 break;
8262 case DW_FORM_data16:
8263 info_ptr += 16;
8264 break;
8265 case DW_FORM_string:
8266 read_direct_string (abfd, info_ptr, &bytes_read);
8267 info_ptr += bytes_read;
8268 break;
8269 case DW_FORM_sec_offset:
8270 case DW_FORM_strp:
8271 case DW_FORM_GNU_strp_alt:
8272 info_ptr += cu->header.offset_size;
8273 break;
8274 case DW_FORM_exprloc:
8275 case DW_FORM_block:
8276 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8277 info_ptr += bytes_read;
8278 break;
8279 case DW_FORM_block1:
8280 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8281 break;
8282 case DW_FORM_block2:
8283 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8284 break;
8285 case DW_FORM_block4:
8286 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8287 break;
8288 case DW_FORM_addrx:
8289 case DW_FORM_strx:
8290 case DW_FORM_sdata:
8291 case DW_FORM_udata:
8292 case DW_FORM_ref_udata:
8293 case DW_FORM_GNU_addr_index:
8294 case DW_FORM_GNU_str_index:
8295 case DW_FORM_rnglistx:
8296 case DW_FORM_loclistx:
8297 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8298 break;
8299 case DW_FORM_indirect:
8300 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8301 info_ptr += bytes_read;
8302 /* We need to continue parsing from here, so just go back to
8303 the top. */
8304 goto skip_attribute;
8305
8306 default:
8307 error (_("Dwarf Error: Cannot handle %s "
8308 "in DWARF reader [in module %s]"),
8309 dwarf_form_name (form),
8310 bfd_get_filename (abfd));
8311 }
8312 }
8313
8314 if (abbrev->has_children)
8315 return skip_children (reader, info_ptr);
8316 else
8317 return info_ptr;
8318 }
8319
8320 /* Locate ORIG_PDI's sibling.
8321 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8322
8323 static const gdb_byte *
8324 locate_pdi_sibling (const struct die_reader_specs *reader,
8325 struct partial_die_info *orig_pdi,
8326 const gdb_byte *info_ptr)
8327 {
8328 /* Do we know the sibling already? */
8329
8330 if (orig_pdi->sibling)
8331 return orig_pdi->sibling;
8332
8333 /* Are there any children to deal with? */
8334
8335 if (!orig_pdi->has_children)
8336 return info_ptr;
8337
8338 /* Skip the children the long way. */
8339
8340 return skip_children (reader, info_ptr);
8341 }
8342
8343 /* Expand this partial symbol table into a full symbol table. SELF is
8344 not NULL. */
8345
8346 void
8347 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8348 {
8349 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8350
8351 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8352
8353 /* If this psymtab is constructed from a debug-only objfile, the
8354 has_section_at_zero flag will not necessarily be correct. We
8355 can get the correct value for this flag by looking at the data
8356 associated with the (presumably stripped) associated objfile. */
8357 if (objfile->separate_debug_objfile_backlink)
8358 {
8359 dwarf2_per_objfile *per_objfile_backlink
8360 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8361
8362 per_objfile->per_bfd->has_section_at_zero
8363 = per_objfile_backlink->per_bfd->has_section_at_zero;
8364 }
8365
8366 expand_psymtab (objfile);
8367
8368 process_cu_includes (per_objfile);
8369 }
8370 \f
8371 /* Reading in full CUs. */
8372
8373 /* Add PER_CU to the queue. */
8374
8375 static void
8376 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8377 dwarf2_per_objfile *per_objfile,
8378 enum language pretend_language)
8379 {
8380 per_cu->queued = 1;
8381
8382 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8383 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8384 }
8385
8386 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8387
8388 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8389 dependency.
8390
8391 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8392 DIEs, false otherwise.
8393
8394 Explanation: there is an invariant that if a CU is queued for expansion
8395 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8396 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8397 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8398 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8399 invariant is respected.
8400
8401 The caller is therefore not required to load the CU's DIEs (we return false)
8402 if:
8403
8404 - the CU is already expanded, and therefore does not get enqueued
8405 - the CU gets enqueued for expansion, but its DIEs are already loaded
8406
8407 Note that the caller should not use this function's return value as an
8408 indicator of whether the CU's DIEs are loaded right now, it should check
8409 that by calling `dwarf2_per_objfile::get_cu` instead. */
8410
8411 static int
8412 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8413 dwarf2_per_cu_data *per_cu,
8414 dwarf2_per_objfile *per_objfile,
8415 enum language pretend_language)
8416 {
8417 /* We may arrive here during partial symbol reading, if we need full
8418 DIEs to process an unusual case (e.g. template arguments). Do
8419 not queue PER_CU, just tell our caller to load its DIEs. */
8420 if (per_cu->per_bfd->reading_partial_symbols)
8421 {
8422 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8423
8424 if (cu == NULL || cu->dies == NULL)
8425 return 1;
8426 return 0;
8427 }
8428
8429 /* Mark the dependence relation so that we don't flush PER_CU
8430 too early. */
8431 if (dependent_cu != NULL)
8432 dependent_cu->add_dependence (per_cu);
8433
8434 /* If it's already on the queue, we have nothing to do. */
8435 if (per_cu->queued)
8436 {
8437 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8438 loaded. */
8439 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8440
8441 /* If the CU is queued for expansion, it should not already be
8442 expanded. */
8443 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8444
8445 /* The DIEs are already loaded, the caller doesn't need to do it. */
8446 return 0;
8447 }
8448
8449 bool queued = false;
8450 if (!per_objfile->symtab_set_p (per_cu))
8451 {
8452 /* Add it to the queue. */
8453 queue_comp_unit (per_cu, per_objfile, pretend_language);
8454 queued = true;
8455 }
8456
8457 /* If the compilation unit is already loaded, just mark it as
8458 used. */
8459 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8460 if (cu != nullptr)
8461 cu->last_used = 0;
8462
8463 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8464 and the DIEs are not already loaded. */
8465 return queued && cu == nullptr;
8466 }
8467
8468 /* Process the queue. */
8469
8470 static void
8471 process_queue (dwarf2_per_objfile *per_objfile)
8472 {
8473 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8474 objfile_name (per_objfile->objfile));
8475
8476 /* The queue starts out with one item, but following a DIE reference
8477 may load a new CU, adding it to the end of the queue. */
8478 while (!per_objfile->per_bfd->queue->empty ())
8479 {
8480 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8481 dwarf2_per_cu_data *per_cu = item.per_cu;
8482
8483 if (!per_objfile->symtab_set_p (per_cu))
8484 {
8485 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8486
8487 /* Skip dummy CUs. */
8488 if (cu != nullptr)
8489 {
8490 unsigned int debug_print_threshold;
8491 char buf[100];
8492
8493 if (per_cu->is_debug_types)
8494 {
8495 struct signatured_type *sig_type =
8496 (struct signatured_type *) per_cu;
8497
8498 sprintf (buf, "TU %s at offset %s",
8499 hex_string (sig_type->signature),
8500 sect_offset_str (per_cu->sect_off));
8501 /* There can be 100s of TUs.
8502 Only print them in verbose mode. */
8503 debug_print_threshold = 2;
8504 }
8505 else
8506 {
8507 sprintf (buf, "CU at offset %s",
8508 sect_offset_str (per_cu->sect_off));
8509 debug_print_threshold = 1;
8510 }
8511
8512 if (dwarf_read_debug >= debug_print_threshold)
8513 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8514
8515 if (per_cu->is_debug_types)
8516 process_full_type_unit (cu, item.pretend_language);
8517 else
8518 process_full_comp_unit (cu, item.pretend_language);
8519
8520 if (dwarf_read_debug >= debug_print_threshold)
8521 dwarf_read_debug_printf ("Done expanding %s", buf);
8522 }
8523 }
8524
8525 per_cu->queued = 0;
8526 per_objfile->per_bfd->queue->pop ();
8527 }
8528
8529 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8530 objfile_name (per_objfile->objfile));
8531 }
8532
8533 /* Read in full symbols for PST, and anything it depends on. */
8534
8535 void
8536 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8537 {
8538 gdb_assert (!readin_p (objfile));
8539
8540 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8541 free_cached_comp_units freer (per_objfile);
8542 expand_dependencies (objfile);
8543
8544 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8545 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8546 }
8547
8548 /* See psympriv.h. */
8549
8550 bool
8551 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8552 {
8553 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8554 return per_objfile->symtab_set_p (per_cu_data);
8555 }
8556
8557 /* See psympriv.h. */
8558
8559 compunit_symtab *
8560 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8561 {
8562 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8563 return per_objfile->get_symtab (per_cu_data);
8564 }
8565
8566 /* Trivial hash function for die_info: the hash value of a DIE
8567 is its offset in .debug_info for this objfile. */
8568
8569 static hashval_t
8570 die_hash (const void *item)
8571 {
8572 const struct die_info *die = (const struct die_info *) item;
8573
8574 return to_underlying (die->sect_off);
8575 }
8576
8577 /* Trivial comparison function for die_info structures: two DIEs
8578 are equal if they have the same offset. */
8579
8580 static int
8581 die_eq (const void *item_lhs, const void *item_rhs)
8582 {
8583 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8584 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8585
8586 return die_lhs->sect_off == die_rhs->sect_off;
8587 }
8588
8589 /* Load the DIEs associated with PER_CU into memory.
8590
8591 In some cases, the caller, while reading partial symbols, will need to load
8592 the full symbols for the CU for some reason. It will already have a
8593 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8594 rather than creating a new one. */
8595
8596 static void
8597 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8598 dwarf2_per_objfile *per_objfile,
8599 dwarf2_cu *existing_cu,
8600 bool skip_partial,
8601 enum language pretend_language)
8602 {
8603 gdb_assert (! this_cu->is_debug_types);
8604
8605 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8606 if (reader.dummy_p)
8607 return;
8608
8609 struct dwarf2_cu *cu = reader.cu;
8610 const gdb_byte *info_ptr = reader.info_ptr;
8611
8612 gdb_assert (cu->die_hash == NULL);
8613 cu->die_hash =
8614 htab_create_alloc_ex (cu->header.length / 12,
8615 die_hash,
8616 die_eq,
8617 NULL,
8618 &cu->comp_unit_obstack,
8619 hashtab_obstack_allocate,
8620 dummy_obstack_deallocate);
8621
8622 if (reader.comp_unit_die->has_children)
8623 reader.comp_unit_die->child
8624 = read_die_and_siblings (&reader, reader.info_ptr,
8625 &info_ptr, reader.comp_unit_die);
8626 cu->dies = reader.comp_unit_die;
8627 /* comp_unit_die is not stored in die_hash, no need. */
8628
8629 /* We try not to read any attributes in this function, because not
8630 all CUs needed for references have been loaded yet, and symbol
8631 table processing isn't initialized. But we have to set the CU language,
8632 or we won't be able to build types correctly.
8633 Similarly, if we do not read the producer, we can not apply
8634 producer-specific interpretation. */
8635 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8636
8637 reader.keep ();
8638 }
8639
8640 /* Add a DIE to the delayed physname list. */
8641
8642 static void
8643 add_to_method_list (struct type *type, int fnfield_index, int index,
8644 const char *name, struct die_info *die,
8645 struct dwarf2_cu *cu)
8646 {
8647 struct delayed_method_info mi;
8648 mi.type = type;
8649 mi.fnfield_index = fnfield_index;
8650 mi.index = index;
8651 mi.name = name;
8652 mi.die = die;
8653 cu->method_list.push_back (mi);
8654 }
8655
8656 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8657 "const" / "volatile". If so, decrements LEN by the length of the
8658 modifier and return true. Otherwise return false. */
8659
8660 template<size_t N>
8661 static bool
8662 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8663 {
8664 size_t mod_len = sizeof (mod) - 1;
8665 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8666 {
8667 len -= mod_len;
8668 return true;
8669 }
8670 return false;
8671 }
8672
8673 /* Compute the physnames of any methods on the CU's method list.
8674
8675 The computation of method physnames is delayed in order to avoid the
8676 (bad) condition that one of the method's formal parameters is of an as yet
8677 incomplete type. */
8678
8679 static void
8680 compute_delayed_physnames (struct dwarf2_cu *cu)
8681 {
8682 /* Only C++ delays computing physnames. */
8683 if (cu->method_list.empty ())
8684 return;
8685 gdb_assert (cu->per_cu->lang == language_cplus);
8686
8687 for (const delayed_method_info &mi : cu->method_list)
8688 {
8689 const char *physname;
8690 struct fn_fieldlist *fn_flp
8691 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8692 physname = dwarf2_physname (mi.name, mi.die, cu);
8693 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8694 = physname ? physname : "";
8695
8696 /* Since there's no tag to indicate whether a method is a
8697 const/volatile overload, extract that information out of the
8698 demangled name. */
8699 if (physname != NULL)
8700 {
8701 size_t len = strlen (physname);
8702
8703 while (1)
8704 {
8705 if (physname[len] == ')') /* shortcut */
8706 break;
8707 else if (check_modifier (physname, len, " const"))
8708 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8709 else if (check_modifier (physname, len, " volatile"))
8710 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8711 else
8712 break;
8713 }
8714 }
8715 }
8716
8717 /* The list is no longer needed. */
8718 cu->method_list.clear ();
8719 }
8720
8721 /* Go objects should be embedded in a DW_TAG_module DIE,
8722 and it's not clear if/how imported objects will appear.
8723 To keep Go support simple until that's worked out,
8724 go back through what we've read and create something usable.
8725 We could do this while processing each DIE, and feels kinda cleaner,
8726 but that way is more invasive.
8727 This is to, for example, allow the user to type "p var" or "b main"
8728 without having to specify the package name, and allow lookups
8729 of module.object to work in contexts that use the expression
8730 parser. */
8731
8732 static void
8733 fixup_go_packaging (struct dwarf2_cu *cu)
8734 {
8735 gdb::unique_xmalloc_ptr<char> package_name;
8736 struct pending *list;
8737 int i;
8738
8739 for (list = *cu->get_builder ()->get_global_symbols ();
8740 list != NULL;
8741 list = list->next)
8742 {
8743 for (i = 0; i < list->nsyms; ++i)
8744 {
8745 struct symbol *sym = list->symbol[i];
8746
8747 if (sym->language () == language_go
8748 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8749 {
8750 gdb::unique_xmalloc_ptr<char> this_package_name
8751 (go_symbol_package_name (sym));
8752
8753 if (this_package_name == NULL)
8754 continue;
8755 if (package_name == NULL)
8756 package_name = std::move (this_package_name);
8757 else
8758 {
8759 struct objfile *objfile = cu->per_objfile->objfile;
8760 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8761 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8762 (symbol_symtab (sym) != NULL
8763 ? symtab_to_filename_for_display
8764 (symbol_symtab (sym))
8765 : objfile_name (objfile)),
8766 this_package_name.get (), package_name.get ());
8767 }
8768 }
8769 }
8770 }
8771
8772 if (package_name != NULL)
8773 {
8774 struct objfile *objfile = cu->per_objfile->objfile;
8775 const char *saved_package_name = objfile->intern (package_name.get ());
8776 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8777 saved_package_name);
8778 struct symbol *sym;
8779
8780 sym = new (&objfile->objfile_obstack) symbol;
8781 sym->set_language (language_go, &objfile->objfile_obstack);
8782 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8783 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8784 e.g., "main" finds the "main" module and not C's main(). */
8785 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8786 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8787 SYMBOL_TYPE (sym) = type;
8788
8789 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8790 }
8791 }
8792
8793 /* Allocate a fully-qualified name consisting of the two parts on the
8794 obstack. */
8795
8796 static const char *
8797 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8798 {
8799 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8800 }
8801
8802 /* A helper that allocates a variant part to attach to a Rust enum
8803 type. OBSTACK is where the results should be allocated. TYPE is
8804 the type we're processing. DISCRIMINANT_INDEX is the index of the
8805 discriminant. It must be the index of one of the fields of TYPE,
8806 or -1 to mean there is no discriminant (univariant enum).
8807 DEFAULT_INDEX is the index of the default field; or -1 if there is
8808 no default. RANGES is indexed by "effective" field number (the
8809 field index, but omitting the discriminant and default fields) and
8810 must hold the discriminant values used by the variants. Note that
8811 RANGES must have a lifetime at least as long as OBSTACK -- either
8812 already allocated on it, or static. */
8813
8814 static void
8815 alloc_rust_variant (struct obstack *obstack, struct type *type,
8816 int discriminant_index, int default_index,
8817 gdb::array_view<discriminant_range> ranges)
8818 {
8819 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8820 gdb_assert (discriminant_index == -1
8821 || (discriminant_index >= 0
8822 && discriminant_index < type->num_fields ()));
8823 gdb_assert (default_index == -1
8824 || (default_index >= 0 && default_index < type->num_fields ()));
8825
8826 /* We have one variant for each non-discriminant field. */
8827 int n_variants = type->num_fields ();
8828 if (discriminant_index != -1)
8829 --n_variants;
8830
8831 variant *variants = new (obstack) variant[n_variants];
8832 int var_idx = 0;
8833 int range_idx = 0;
8834 for (int i = 0; i < type->num_fields (); ++i)
8835 {
8836 if (i == discriminant_index)
8837 continue;
8838
8839 variants[var_idx].first_field = i;
8840 variants[var_idx].last_field = i + 1;
8841
8842 /* The default field does not need a range, but other fields do.
8843 We skipped the discriminant above. */
8844 if (i != default_index)
8845 {
8846 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8847 ++range_idx;
8848 }
8849
8850 ++var_idx;
8851 }
8852
8853 gdb_assert (range_idx == ranges.size ());
8854 gdb_assert (var_idx == n_variants);
8855
8856 variant_part *part = new (obstack) variant_part;
8857 part->discriminant_index = discriminant_index;
8858 /* If there is no discriminant, then whether it is signed is of no
8859 consequence. */
8860 part->is_unsigned
8861 = (discriminant_index == -1
8862 ? false
8863 : type->field (discriminant_index).type ()->is_unsigned ());
8864 part->variants = gdb::array_view<variant> (variants, n_variants);
8865
8866 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
8867 gdb::array_view<variant_part> *prop_value
8868 = new (storage) gdb::array_view<variant_part> (part, 1);
8869
8870 struct dynamic_prop prop;
8871 prop.set_variant_parts (prop_value);
8872
8873 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
8874 }
8875
8876 /* Some versions of rustc emitted enums in an unusual way.
8877
8878 Ordinary enums were emitted as unions. The first element of each
8879 structure in the union was named "RUST$ENUM$DISR". This element
8880 held the discriminant.
8881
8882 These versions of Rust also implemented the "non-zero"
8883 optimization. When the enum had two values, and one is empty and
8884 the other holds a pointer that cannot be zero, the pointer is used
8885 as the discriminant, with a zero value meaning the empty variant.
8886 Here, the union's first member is of the form
8887 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8888 where the fieldnos are the indices of the fields that should be
8889 traversed in order to find the field (which may be several fields deep)
8890 and the variantname is the name of the variant of the case when the
8891 field is zero.
8892
8893 This function recognizes whether TYPE is of one of these forms,
8894 and, if so, smashes it to be a variant type. */
8895
8896 static void
8897 quirk_rust_enum (struct type *type, struct objfile *objfile)
8898 {
8899 gdb_assert (type->code () == TYPE_CODE_UNION);
8900
8901 /* We don't need to deal with empty enums. */
8902 if (type->num_fields () == 0)
8903 return;
8904
8905 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
8906 if (type->num_fields () == 1
8907 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
8908 {
8909 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
8910
8911 /* Decode the field name to find the offset of the
8912 discriminant. */
8913 ULONGEST bit_offset = 0;
8914 struct type *field_type = type->field (0).type ();
8915 while (name[0] >= '0' && name[0] <= '9')
8916 {
8917 char *tail;
8918 unsigned long index = strtoul (name, &tail, 10);
8919 name = tail;
8920 if (*name != '$'
8921 || index >= field_type->num_fields ()
8922 || (TYPE_FIELD_LOC_KIND (field_type, index)
8923 != FIELD_LOC_KIND_BITPOS))
8924 {
8925 complaint (_("Could not parse Rust enum encoding string \"%s\""
8926 "[in module %s]"),
8927 TYPE_FIELD_NAME (type, 0),
8928 objfile_name (objfile));
8929 return;
8930 }
8931 ++name;
8932
8933 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
8934 field_type = field_type->field (index).type ();
8935 }
8936
8937 /* Smash this type to be a structure type. We have to do this
8938 because the type has already been recorded. */
8939 type->set_code (TYPE_CODE_STRUCT);
8940 type->set_num_fields (3);
8941 /* Save the field we care about. */
8942 struct field saved_field = type->field (0);
8943 type->set_fields
8944 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
8945
8946 /* Put the discriminant at index 0. */
8947 type->field (0).set_type (field_type);
8948 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8949 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
8950 SET_FIELD_BITPOS (type->field (0), bit_offset);
8951
8952 /* The order of fields doesn't really matter, so put the real
8953 field at index 1 and the data-less field at index 2. */
8954 type->field (1) = saved_field;
8955 TYPE_FIELD_NAME (type, 1)
8956 = rust_last_path_segment (type->field (1).type ()->name ());
8957 type->field (1).type ()->set_name
8958 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8959 TYPE_FIELD_NAME (type, 1)));
8960
8961 const char *dataless_name
8962 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8963 name);
8964 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8965 dataless_name);
8966 type->field (2).set_type (dataless_type);
8967 /* NAME points into the original discriminant name, which
8968 already has the correct lifetime. */
8969 TYPE_FIELD_NAME (type, 2) = name;
8970 SET_FIELD_BITPOS (type->field (2), 0);
8971
8972 /* Indicate that this is a variant type. */
8973 static discriminant_range ranges[1] = { { 0, 0 } };
8974 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8975 }
8976 /* A union with a single anonymous field is probably an old-style
8977 univariant enum. */
8978 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
8979 {
8980 /* Smash this type to be a structure type. We have to do this
8981 because the type has already been recorded. */
8982 type->set_code (TYPE_CODE_STRUCT);
8983
8984 struct type *field_type = type->field (0).type ();
8985 const char *variant_name
8986 = rust_last_path_segment (field_type->name ());
8987 TYPE_FIELD_NAME (type, 0) = variant_name;
8988 field_type->set_name
8989 (rust_fully_qualify (&objfile->objfile_obstack,
8990 type->name (), variant_name));
8991
8992 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
8993 }
8994 else
8995 {
8996 struct type *disr_type = nullptr;
8997 for (int i = 0; i < type->num_fields (); ++i)
8998 {
8999 disr_type = type->field (i).type ();
9000
9001 if (disr_type->code () != TYPE_CODE_STRUCT)
9002 {
9003 /* All fields of a true enum will be structs. */
9004 return;
9005 }
9006 else if (disr_type->num_fields () == 0)
9007 {
9008 /* Could be data-less variant, so keep going. */
9009 disr_type = nullptr;
9010 }
9011 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9012 "RUST$ENUM$DISR") != 0)
9013 {
9014 /* Not a Rust enum. */
9015 return;
9016 }
9017 else
9018 {
9019 /* Found one. */
9020 break;
9021 }
9022 }
9023
9024 /* If we got here without a discriminant, then it's probably
9025 just a union. */
9026 if (disr_type == nullptr)
9027 return;
9028
9029 /* Smash this type to be a structure type. We have to do this
9030 because the type has already been recorded. */
9031 type->set_code (TYPE_CODE_STRUCT);
9032
9033 /* Make space for the discriminant field. */
9034 struct field *disr_field = &disr_type->field (0);
9035 field *new_fields
9036 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9037 * sizeof (struct field)));
9038 memcpy (new_fields + 1, type->fields (),
9039 type->num_fields () * sizeof (struct field));
9040 type->set_fields (new_fields);
9041 type->set_num_fields (type->num_fields () + 1);
9042
9043 /* Install the discriminant at index 0 in the union. */
9044 type->field (0) = *disr_field;
9045 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9046 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9047
9048 /* We need a way to find the correct discriminant given a
9049 variant name. For convenience we build a map here. */
9050 struct type *enum_type = disr_field->type ();
9051 std::unordered_map<std::string, ULONGEST> discriminant_map;
9052 for (int i = 0; i < enum_type->num_fields (); ++i)
9053 {
9054 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9055 {
9056 const char *name
9057 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9058 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9059 }
9060 }
9061
9062 int n_fields = type->num_fields ();
9063 /* We don't need a range entry for the discriminant, but we do
9064 need one for every other field, as there is no default
9065 variant. */
9066 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9067 discriminant_range,
9068 n_fields - 1);
9069 /* Skip the discriminant here. */
9070 for (int i = 1; i < n_fields; ++i)
9071 {
9072 /* Find the final word in the name of this variant's type.
9073 That name can be used to look up the correct
9074 discriminant. */
9075 const char *variant_name
9076 = rust_last_path_segment (type->field (i).type ()->name ());
9077
9078 auto iter = discriminant_map.find (variant_name);
9079 if (iter != discriminant_map.end ())
9080 {
9081 ranges[i - 1].low = iter->second;
9082 ranges[i - 1].high = iter->second;
9083 }
9084
9085 /* In Rust, each element should have the size of the
9086 enclosing enum. */
9087 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9088
9089 /* Remove the discriminant field, if it exists. */
9090 struct type *sub_type = type->field (i).type ();
9091 if (sub_type->num_fields () > 0)
9092 {
9093 sub_type->set_num_fields (sub_type->num_fields () - 1);
9094 sub_type->set_fields (sub_type->fields () + 1);
9095 }
9096 TYPE_FIELD_NAME (type, i) = variant_name;
9097 sub_type->set_name
9098 (rust_fully_qualify (&objfile->objfile_obstack,
9099 type->name (), variant_name));
9100 }
9101
9102 /* Indicate that this is a variant type. */
9103 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9104 gdb::array_view<discriminant_range> (ranges,
9105 n_fields - 1));
9106 }
9107 }
9108
9109 /* Rewrite some Rust unions to be structures with variants parts. */
9110
9111 static void
9112 rust_union_quirks (struct dwarf2_cu *cu)
9113 {
9114 gdb_assert (cu->per_cu->lang == language_rust);
9115 for (type *type_ : cu->rust_unions)
9116 quirk_rust_enum (type_, cu->per_objfile->objfile);
9117 /* We don't need this any more. */
9118 cu->rust_unions.clear ();
9119 }
9120
9121 /* See read.h. */
9122
9123 type_unit_group_unshareable *
9124 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9125 {
9126 auto iter = this->m_type_units.find (tu_group);
9127 if (iter != this->m_type_units.end ())
9128 return iter->second.get ();
9129
9130 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9131 type_unit_group_unshareable *result = uniq.get ();
9132 this->m_type_units[tu_group] = std::move (uniq);
9133 return result;
9134 }
9135
9136 struct type *
9137 dwarf2_per_objfile::get_type_for_signatured_type
9138 (signatured_type *sig_type) const
9139 {
9140 auto iter = this->m_type_map.find (sig_type);
9141 if (iter == this->m_type_map.end ())
9142 return nullptr;
9143
9144 return iter->second;
9145 }
9146
9147 void dwarf2_per_objfile::set_type_for_signatured_type
9148 (signatured_type *sig_type, struct type *type)
9149 {
9150 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9151
9152 this->m_type_map[sig_type] = type;
9153 }
9154
9155 /* A helper function for computing the list of all symbol tables
9156 included by PER_CU. */
9157
9158 static void
9159 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9160 htab_t all_children, htab_t all_type_symtabs,
9161 dwarf2_per_cu_data *per_cu,
9162 dwarf2_per_objfile *per_objfile,
9163 struct compunit_symtab *immediate_parent)
9164 {
9165 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9166 if (*slot != NULL)
9167 {
9168 /* This inclusion and its children have been processed. */
9169 return;
9170 }
9171
9172 *slot = per_cu;
9173
9174 /* Only add a CU if it has a symbol table. */
9175 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9176 if (cust != NULL)
9177 {
9178 /* If this is a type unit only add its symbol table if we haven't
9179 seen it yet (type unit per_cu's can share symtabs). */
9180 if (per_cu->is_debug_types)
9181 {
9182 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9183 if (*slot == NULL)
9184 {
9185 *slot = cust;
9186 result->push_back (cust);
9187 if (cust->user == NULL)
9188 cust->user = immediate_parent;
9189 }
9190 }
9191 else
9192 {
9193 result->push_back (cust);
9194 if (cust->user == NULL)
9195 cust->user = immediate_parent;
9196 }
9197 }
9198
9199 if (!per_cu->imported_symtabs_empty ())
9200 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9201 {
9202 recursively_compute_inclusions (result, all_children,
9203 all_type_symtabs, ptr, per_objfile,
9204 cust);
9205 }
9206 }
9207
9208 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9209 PER_CU. */
9210
9211 static void
9212 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9213 dwarf2_per_objfile *per_objfile)
9214 {
9215 gdb_assert (! per_cu->is_debug_types);
9216
9217 if (!per_cu->imported_symtabs_empty ())
9218 {
9219 int len;
9220 std::vector<compunit_symtab *> result_symtabs;
9221 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9222
9223 /* If we don't have a symtab, we can just skip this case. */
9224 if (cust == NULL)
9225 return;
9226
9227 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9228 htab_eq_pointer,
9229 NULL, xcalloc, xfree));
9230 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9231 htab_eq_pointer,
9232 NULL, xcalloc, xfree));
9233
9234 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9235 {
9236 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9237 all_type_symtabs.get (), ptr,
9238 per_objfile, cust);
9239 }
9240
9241 /* Now we have a transitive closure of all the included symtabs. */
9242 len = result_symtabs.size ();
9243 cust->includes
9244 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9245 struct compunit_symtab *, len + 1);
9246 memcpy (cust->includes, result_symtabs.data (),
9247 len * sizeof (compunit_symtab *));
9248 cust->includes[len] = NULL;
9249 }
9250 }
9251
9252 /* Compute the 'includes' field for the symtabs of all the CUs we just
9253 read. */
9254
9255 static void
9256 process_cu_includes (dwarf2_per_objfile *per_objfile)
9257 {
9258 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9259 {
9260 if (! iter->is_debug_types)
9261 compute_compunit_symtab_includes (iter, per_objfile);
9262 }
9263
9264 per_objfile->per_bfd->just_read_cus.clear ();
9265 }
9266
9267 /* Generate full symbol information for CU, whose DIEs have
9268 already been loaded into memory. */
9269
9270 static void
9271 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9272 {
9273 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9274 struct objfile *objfile = per_objfile->objfile;
9275 struct gdbarch *gdbarch = objfile->arch ();
9276 CORE_ADDR lowpc, highpc;
9277 struct compunit_symtab *cust;
9278 CORE_ADDR baseaddr;
9279 struct block *static_block;
9280 CORE_ADDR addr;
9281
9282 baseaddr = objfile->text_section_offset ();
9283
9284 /* Clear the list here in case something was left over. */
9285 cu->method_list.clear ();
9286
9287 dwarf2_find_base_address (cu->dies, cu);
9288
9289 /* Before we start reading the top-level DIE, ensure it has a valid tag
9290 type. */
9291 switch (cu->dies->tag)
9292 {
9293 case DW_TAG_compile_unit:
9294 case DW_TAG_partial_unit:
9295 case DW_TAG_type_unit:
9296 break;
9297 default:
9298 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9299 dwarf_tag_name (cu->dies->tag),
9300 sect_offset_str (cu->per_cu->sect_off),
9301 objfile_name (per_objfile->objfile));
9302 }
9303
9304 /* Do line number decoding in read_file_scope () */
9305 process_die (cu->dies, cu);
9306
9307 /* For now fudge the Go package. */
9308 if (cu->per_cu->lang == language_go)
9309 fixup_go_packaging (cu);
9310
9311 /* Now that we have processed all the DIEs in the CU, all the types
9312 should be complete, and it should now be safe to compute all of the
9313 physnames. */
9314 compute_delayed_physnames (cu);
9315
9316 if (cu->per_cu->lang == language_rust)
9317 rust_union_quirks (cu);
9318
9319 /* Some compilers don't define a DW_AT_high_pc attribute for the
9320 compilation unit. If the DW_AT_high_pc is missing, synthesize
9321 it, by scanning the DIE's below the compilation unit. */
9322 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9323
9324 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9325 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9326
9327 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9328 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9329 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9330 addrmap to help ensure it has an accurate map of pc values belonging to
9331 this comp unit. */
9332 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9333
9334 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9335 SECT_OFF_TEXT (objfile),
9336 0);
9337
9338 if (cust != NULL)
9339 {
9340 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9341
9342 /* Set symtab language to language from DW_AT_language. If the
9343 compilation is from a C file generated by language preprocessors, do
9344 not set the language if it was already deduced by start_subfile. */
9345 if (!(cu->per_cu->lang == language_c
9346 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9347 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
9348
9349 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9350 produce DW_AT_location with location lists but it can be possibly
9351 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9352 there were bugs in prologue debug info, fixed later in GCC-4.5
9353 by "unwind info for epilogues" patch (which is not directly related).
9354
9355 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9356 needed, it would be wrong due to missing DW_AT_producer there.
9357
9358 Still one can confuse GDB by using non-standard GCC compilation
9359 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9360 */
9361 if (cu->has_loclist && gcc_4_minor >= 5)
9362 cust->locations_valid = 1;
9363
9364 if (gcc_4_minor >= 5)
9365 cust->epilogue_unwind_valid = 1;
9366
9367 cust->call_site_htab = cu->call_site_htab;
9368 }
9369
9370 per_objfile->set_symtab (cu->per_cu, cust);
9371
9372 /* Push it for inclusion processing later. */
9373 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9374
9375 /* Not needed any more. */
9376 cu->reset_builder ();
9377 }
9378
9379 /* Generate full symbol information for type unit CU, whose DIEs have
9380 already been loaded into memory. */
9381
9382 static void
9383 process_full_type_unit (dwarf2_cu *cu,
9384 enum language pretend_language)
9385 {
9386 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9387 struct objfile *objfile = per_objfile->objfile;
9388 struct compunit_symtab *cust;
9389 struct signatured_type *sig_type;
9390
9391 gdb_assert (cu->per_cu->is_debug_types);
9392 sig_type = (struct signatured_type *) cu->per_cu;
9393
9394 /* Clear the list here in case something was left over. */
9395 cu->method_list.clear ();
9396
9397 /* The symbol tables are set up in read_type_unit_scope. */
9398 process_die (cu->dies, cu);
9399
9400 /* For now fudge the Go package. */
9401 if (cu->per_cu->lang == language_go)
9402 fixup_go_packaging (cu);
9403
9404 /* Now that we have processed all the DIEs in the CU, all the types
9405 should be complete, and it should now be safe to compute all of the
9406 physnames. */
9407 compute_delayed_physnames (cu);
9408
9409 if (cu->per_cu->lang == language_rust)
9410 rust_union_quirks (cu);
9411
9412 /* TUs share symbol tables.
9413 If this is the first TU to use this symtab, complete the construction
9414 of it with end_expandable_symtab. Otherwise, complete the addition of
9415 this TU's symbols to the existing symtab. */
9416 type_unit_group_unshareable *tug_unshare =
9417 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9418 if (tug_unshare->compunit_symtab == NULL)
9419 {
9420 buildsym_compunit *builder = cu->get_builder ();
9421 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9422 tug_unshare->compunit_symtab = cust;
9423
9424 if (cust != NULL)
9425 {
9426 /* Set symtab language to language from DW_AT_language. If the
9427 compilation is from a C file generated by language preprocessors,
9428 do not set the language if it was already deduced by
9429 start_subfile. */
9430 if (!(cu->per_cu->lang == language_c
9431 && COMPUNIT_FILETABS (cust)->language != language_c))
9432 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
9433 }
9434 }
9435 else
9436 {
9437 cu->get_builder ()->augment_type_symtab ();
9438 cust = tug_unshare->compunit_symtab;
9439 }
9440
9441 per_objfile->set_symtab (cu->per_cu, cust);
9442
9443 /* Not needed any more. */
9444 cu->reset_builder ();
9445 }
9446
9447 /* Process an imported unit DIE. */
9448
9449 static void
9450 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9451 {
9452 struct attribute *attr;
9453
9454 /* For now we don't handle imported units in type units. */
9455 if (cu->per_cu->is_debug_types)
9456 {
9457 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9458 " supported in type units [in module %s]"),
9459 objfile_name (cu->per_objfile->objfile));
9460 }
9461
9462 attr = dwarf2_attr (die, DW_AT_import, cu);
9463 if (attr != NULL)
9464 {
9465 sect_offset sect_off = attr->get_ref_die_offset ();
9466 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9467 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9468 dwarf2_per_cu_data *per_cu
9469 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9470
9471 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9472 into another compilation unit, at root level. Regard this as a hint,
9473 and ignore it. */
9474 if (die->parent && die->parent->parent == NULL
9475 && per_cu->unit_type == DW_UT_compile
9476 && per_cu->lang == language_cplus)
9477 return;
9478
9479 /* If necessary, add it to the queue and load its DIEs. */
9480 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9481 cu->per_cu->lang))
9482 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9483 false, cu->per_cu->lang);
9484
9485 cu->per_cu->imported_symtabs_push (per_cu);
9486 }
9487 }
9488
9489 /* RAII object that represents a process_die scope: i.e.,
9490 starts/finishes processing a DIE. */
9491 class process_die_scope
9492 {
9493 public:
9494 process_die_scope (die_info *die, dwarf2_cu *cu)
9495 : m_die (die), m_cu (cu)
9496 {
9497 /* We should only be processing DIEs not already in process. */
9498 gdb_assert (!m_die->in_process);
9499 m_die->in_process = true;
9500 }
9501
9502 ~process_die_scope ()
9503 {
9504 m_die->in_process = false;
9505
9506 /* If we're done processing the DIE for the CU that owns the line
9507 header, we don't need the line header anymore. */
9508 if (m_cu->line_header_die_owner == m_die)
9509 {
9510 delete m_cu->line_header;
9511 m_cu->line_header = NULL;
9512 m_cu->line_header_die_owner = NULL;
9513 }
9514 }
9515
9516 private:
9517 die_info *m_die;
9518 dwarf2_cu *m_cu;
9519 };
9520
9521 /* Process a die and its children. */
9522
9523 static void
9524 process_die (struct die_info *die, struct dwarf2_cu *cu)
9525 {
9526 process_die_scope scope (die, cu);
9527
9528 switch (die->tag)
9529 {
9530 case DW_TAG_padding:
9531 break;
9532 case DW_TAG_compile_unit:
9533 case DW_TAG_partial_unit:
9534 read_file_scope (die, cu);
9535 break;
9536 case DW_TAG_type_unit:
9537 read_type_unit_scope (die, cu);
9538 break;
9539 case DW_TAG_subprogram:
9540 /* Nested subprograms in Fortran get a prefix. */
9541 if (cu->per_cu->lang == language_fortran
9542 && die->parent != NULL
9543 && die->parent->tag == DW_TAG_subprogram)
9544 cu->processing_has_namespace_info = true;
9545 /* Fall through. */
9546 case DW_TAG_inlined_subroutine:
9547 read_func_scope (die, cu);
9548 break;
9549 case DW_TAG_lexical_block:
9550 case DW_TAG_try_block:
9551 case DW_TAG_catch_block:
9552 read_lexical_block_scope (die, cu);
9553 break;
9554 case DW_TAG_call_site:
9555 case DW_TAG_GNU_call_site:
9556 read_call_site_scope (die, cu);
9557 break;
9558 case DW_TAG_class_type:
9559 case DW_TAG_interface_type:
9560 case DW_TAG_structure_type:
9561 case DW_TAG_union_type:
9562 process_structure_scope (die, cu);
9563 break;
9564 case DW_TAG_enumeration_type:
9565 process_enumeration_scope (die, cu);
9566 break;
9567
9568 /* These dies have a type, but processing them does not create
9569 a symbol or recurse to process the children. Therefore we can
9570 read them on-demand through read_type_die. */
9571 case DW_TAG_subroutine_type:
9572 case DW_TAG_set_type:
9573 case DW_TAG_pointer_type:
9574 case DW_TAG_ptr_to_member_type:
9575 case DW_TAG_reference_type:
9576 case DW_TAG_rvalue_reference_type:
9577 case DW_TAG_string_type:
9578 break;
9579
9580 case DW_TAG_array_type:
9581 /* We only need to handle this case for Ada -- in other
9582 languages, it's normal for the compiler to emit a typedef
9583 instead. */
9584 if (cu->per_cu->lang != language_ada)
9585 break;
9586 /* FALLTHROUGH */
9587 case DW_TAG_base_type:
9588 case DW_TAG_subrange_type:
9589 case DW_TAG_typedef:
9590 /* Add a typedef symbol for the type definition, if it has a
9591 DW_AT_name. */
9592 new_symbol (die, read_type_die (die, cu), cu);
9593 break;
9594 case DW_TAG_common_block:
9595 read_common_block (die, cu);
9596 break;
9597 case DW_TAG_common_inclusion:
9598 break;
9599 case DW_TAG_namespace:
9600 cu->processing_has_namespace_info = true;
9601 read_namespace (die, cu);
9602 break;
9603 case DW_TAG_module:
9604 cu->processing_has_namespace_info = true;
9605 read_module (die, cu);
9606 break;
9607 case DW_TAG_imported_declaration:
9608 cu->processing_has_namespace_info = true;
9609 if (read_namespace_alias (die, cu))
9610 break;
9611 /* The declaration is not a global namespace alias. */
9612 /* Fall through. */
9613 case DW_TAG_imported_module:
9614 cu->processing_has_namespace_info = true;
9615 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9616 || cu->per_cu->lang != language_fortran))
9617 complaint (_("Tag '%s' has unexpected children"),
9618 dwarf_tag_name (die->tag));
9619 read_import_statement (die, cu);
9620 break;
9621
9622 case DW_TAG_imported_unit:
9623 process_imported_unit_die (die, cu);
9624 break;
9625
9626 case DW_TAG_variable:
9627 read_variable (die, cu);
9628 break;
9629
9630 default:
9631 new_symbol (die, NULL, cu);
9632 break;
9633 }
9634 }
9635 \f
9636 /* DWARF name computation. */
9637
9638 /* A helper function for dwarf2_compute_name which determines whether DIE
9639 needs to have the name of the scope prepended to the name listed in the
9640 die. */
9641
9642 static int
9643 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9644 {
9645 struct attribute *attr;
9646
9647 switch (die->tag)
9648 {
9649 case DW_TAG_namespace:
9650 case DW_TAG_typedef:
9651 case DW_TAG_class_type:
9652 case DW_TAG_interface_type:
9653 case DW_TAG_structure_type:
9654 case DW_TAG_union_type:
9655 case DW_TAG_enumeration_type:
9656 case DW_TAG_enumerator:
9657 case DW_TAG_subprogram:
9658 case DW_TAG_inlined_subroutine:
9659 case DW_TAG_member:
9660 case DW_TAG_imported_declaration:
9661 return 1;
9662
9663 case DW_TAG_variable:
9664 case DW_TAG_constant:
9665 /* We only need to prefix "globally" visible variables. These include
9666 any variable marked with DW_AT_external or any variable that
9667 lives in a namespace. [Variables in anonymous namespaces
9668 require prefixing, but they are not DW_AT_external.] */
9669
9670 if (dwarf2_attr (die, DW_AT_specification, cu))
9671 {
9672 struct dwarf2_cu *spec_cu = cu;
9673
9674 return die_needs_namespace (die_specification (die, &spec_cu),
9675 spec_cu);
9676 }
9677
9678 attr = dwarf2_attr (die, DW_AT_external, cu);
9679 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9680 && die->parent->tag != DW_TAG_module)
9681 return 0;
9682 /* A variable in a lexical block of some kind does not need a
9683 namespace, even though in C++ such variables may be external
9684 and have a mangled name. */
9685 if (die->parent->tag == DW_TAG_lexical_block
9686 || die->parent->tag == DW_TAG_try_block
9687 || die->parent->tag == DW_TAG_catch_block
9688 || die->parent->tag == DW_TAG_subprogram)
9689 return 0;
9690 return 1;
9691
9692 default:
9693 return 0;
9694 }
9695 }
9696
9697 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9698 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9699 defined for the given DIE. */
9700
9701 static struct attribute *
9702 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9703 {
9704 struct attribute *attr;
9705
9706 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9707 if (attr == NULL)
9708 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9709
9710 return attr;
9711 }
9712
9713 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9714 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9715 defined for the given DIE. */
9716
9717 static const char *
9718 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9719 {
9720 const char *linkage_name;
9721
9722 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9723 if (linkage_name == NULL)
9724 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9725
9726 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9727 See https://github.com/rust-lang/rust/issues/32925. */
9728 if (cu->per_cu->lang == language_rust && linkage_name != NULL
9729 && strchr (linkage_name, '{') != NULL)
9730 linkage_name = NULL;
9731
9732 return linkage_name;
9733 }
9734
9735 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9736 compute the physname for the object, which include a method's:
9737 - formal parameters (C++),
9738 - receiver type (Go),
9739
9740 The term "physname" is a bit confusing.
9741 For C++, for example, it is the demangled name.
9742 For Go, for example, it's the mangled name.
9743
9744 For Ada, return the DIE's linkage name rather than the fully qualified
9745 name. PHYSNAME is ignored..
9746
9747 The result is allocated on the objfile->per_bfd's obstack and
9748 canonicalized. */
9749
9750 static const char *
9751 dwarf2_compute_name (const char *name,
9752 struct die_info *die, struct dwarf2_cu *cu,
9753 int physname)
9754 {
9755 struct objfile *objfile = cu->per_objfile->objfile;
9756
9757 if (name == NULL)
9758 name = dwarf2_name (die, cu);
9759
9760 enum language lang = cu->per_cu->lang;
9761
9762 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9763 but otherwise compute it by typename_concat inside GDB.
9764 FIXME: Actually this is not really true, or at least not always true.
9765 It's all very confusing. compute_and_set_names doesn't try to demangle
9766 Fortran names because there is no mangling standard. So new_symbol
9767 will set the demangled name to the result of dwarf2_full_name, and it is
9768 the demangled name that GDB uses if it exists. */
9769 if (lang == language_ada
9770 || (lang == language_fortran && physname))
9771 {
9772 /* For Ada unit, we prefer the linkage name over the name, as
9773 the former contains the exported name, which the user expects
9774 to be able to reference. Ideally, we want the user to be able
9775 to reference this entity using either natural or linkage name,
9776 but we haven't started looking at this enhancement yet. */
9777 const char *linkage_name = dw2_linkage_name (die, cu);
9778
9779 if (linkage_name != NULL)
9780 return linkage_name;
9781 }
9782
9783 /* These are the only languages we know how to qualify names in. */
9784 if (name != NULL
9785 && (lang == language_cplus
9786 || lang == language_fortran || lang == language_d
9787 || lang == language_rust))
9788 {
9789 if (die_needs_namespace (die, cu))
9790 {
9791 const char *prefix;
9792 const char *canonical_name = NULL;
9793
9794 string_file buf;
9795
9796 prefix = determine_prefix (die, cu);
9797 if (*prefix != '\0')
9798 {
9799 gdb::unique_xmalloc_ptr<char> prefixed_name
9800 (typename_concat (NULL, prefix, name, physname, cu));
9801
9802 buf.puts (prefixed_name.get ());
9803 }
9804 else
9805 buf.puts (name);
9806
9807 /* Template parameters may be specified in the DIE's DW_AT_name, or
9808 as children with DW_TAG_template_type_param or
9809 DW_TAG_value_type_param. If the latter, add them to the name
9810 here. If the name already has template parameters, then
9811 skip this step; some versions of GCC emit both, and
9812 it is more efficient to use the pre-computed name.
9813
9814 Something to keep in mind about this process: it is very
9815 unlikely, or in some cases downright impossible, to produce
9816 something that will match the mangled name of a function.
9817 If the definition of the function has the same debug info,
9818 we should be able to match up with it anyway. But fallbacks
9819 using the minimal symbol, for instance to find a method
9820 implemented in a stripped copy of libstdc++, will not work.
9821 If we do not have debug info for the definition, we will have to
9822 match them up some other way.
9823
9824 When we do name matching there is a related problem with function
9825 templates; two instantiated function templates are allowed to
9826 differ only by their return types, which we do not add here. */
9827
9828 if (lang == language_cplus && strchr (name, '<') == NULL)
9829 {
9830 struct attribute *attr;
9831 struct die_info *child;
9832 int first = 1;
9833
9834 die->building_fullname = 1;
9835
9836 for (child = die->child; child != NULL; child = child->sibling)
9837 {
9838 struct type *type;
9839 LONGEST value;
9840 const gdb_byte *bytes;
9841 struct dwarf2_locexpr_baton *baton;
9842 struct value *v;
9843
9844 if (child->tag != DW_TAG_template_type_param
9845 && child->tag != DW_TAG_template_value_param)
9846 continue;
9847
9848 if (first)
9849 {
9850 buf.puts ("<");
9851 first = 0;
9852 }
9853 else
9854 buf.puts (", ");
9855
9856 attr = dwarf2_attr (child, DW_AT_type, cu);
9857 if (attr == NULL)
9858 {
9859 complaint (_("template parameter missing DW_AT_type"));
9860 buf.puts ("UNKNOWN_TYPE");
9861 continue;
9862 }
9863 type = die_type (child, cu);
9864
9865 if (child->tag == DW_TAG_template_type_param)
9866 {
9867 cu->language_defn->print_type (type, "", &buf, -1, 0,
9868 &type_print_raw_options);
9869 continue;
9870 }
9871
9872 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9873 if (attr == NULL)
9874 {
9875 complaint (_("template parameter missing "
9876 "DW_AT_const_value"));
9877 buf.puts ("UNKNOWN_VALUE");
9878 continue;
9879 }
9880
9881 dwarf2_const_value_attr (attr, type, name,
9882 &cu->comp_unit_obstack, cu,
9883 &value, &bytes, &baton);
9884
9885 if (type->has_no_signedness ())
9886 /* GDB prints characters as NUMBER 'CHAR'. If that's
9887 changed, this can use value_print instead. */
9888 cu->language_defn->printchar (value, type, &buf);
9889 else
9890 {
9891 struct value_print_options opts;
9892
9893 if (baton != NULL)
9894 v = dwarf2_evaluate_loc_desc (type, NULL,
9895 baton->data,
9896 baton->size,
9897 baton->per_cu,
9898 baton->per_objfile);
9899 else if (bytes != NULL)
9900 {
9901 v = allocate_value (type);
9902 memcpy (value_contents_writeable (v), bytes,
9903 TYPE_LENGTH (type));
9904 }
9905 else
9906 v = value_from_longest (type, value);
9907
9908 /* Specify decimal so that we do not depend on
9909 the radix. */
9910 get_formatted_print_options (&opts, 'd');
9911 opts.raw = 1;
9912 value_print (v, &buf, &opts);
9913 release_value (v);
9914 }
9915 }
9916
9917 die->building_fullname = 0;
9918
9919 if (!first)
9920 {
9921 /* Close the argument list, with a space if necessary
9922 (nested templates). */
9923 if (!buf.empty () && buf.string ().back () == '>')
9924 buf.puts (" >");
9925 else
9926 buf.puts (">");
9927 }
9928 }
9929
9930 /* For C++ methods, append formal parameter type
9931 information, if PHYSNAME. */
9932
9933 if (physname && die->tag == DW_TAG_subprogram
9934 && lang == language_cplus)
9935 {
9936 struct type *type = read_type_die (die, cu);
9937
9938 c_type_print_args (type, &buf, 1, lang,
9939 &type_print_raw_options);
9940
9941 if (lang == language_cplus)
9942 {
9943 /* Assume that an artificial first parameter is
9944 "this", but do not crash if it is not. RealView
9945 marks unnamed (and thus unused) parameters as
9946 artificial; there is no way to differentiate
9947 the two cases. */
9948 if (type->num_fields () > 0
9949 && TYPE_FIELD_ARTIFICIAL (type, 0)
9950 && type->field (0).type ()->code () == TYPE_CODE_PTR
9951 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
9952 buf.puts (" const");
9953 }
9954 }
9955
9956 const std::string &intermediate_name = buf.string ();
9957
9958 if (lang == language_cplus)
9959 canonical_name
9960 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9961 objfile);
9962
9963 /* If we only computed INTERMEDIATE_NAME, or if
9964 INTERMEDIATE_NAME is already canonical, then we need to
9965 intern it. */
9966 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9967 name = objfile->intern (intermediate_name);
9968 else
9969 name = canonical_name;
9970 }
9971 }
9972
9973 return name;
9974 }
9975
9976 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9977 If scope qualifiers are appropriate they will be added. The result
9978 will be allocated on the storage_obstack, or NULL if the DIE does
9979 not have a name. NAME may either be from a previous call to
9980 dwarf2_name or NULL.
9981
9982 The output string will be canonicalized (if C++). */
9983
9984 static const char *
9985 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9986 {
9987 return dwarf2_compute_name (name, die, cu, 0);
9988 }
9989
9990 /* Construct a physname for the given DIE in CU. NAME may either be
9991 from a previous call to dwarf2_name or NULL. The result will be
9992 allocated on the objfile_objstack or NULL if the DIE does not have a
9993 name.
9994
9995 The output string will be canonicalized (if C++). */
9996
9997 static const char *
9998 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9999 {
10000 struct objfile *objfile = cu->per_objfile->objfile;
10001 const char *retval, *mangled = NULL, *canon = NULL;
10002 int need_copy = 1;
10003
10004 /* In this case dwarf2_compute_name is just a shortcut not building anything
10005 on its own. */
10006 if (!die_needs_namespace (die, cu))
10007 return dwarf2_compute_name (name, die, cu, 1);
10008
10009 if (cu->per_cu->lang != language_rust)
10010 mangled = dw2_linkage_name (die, cu);
10011
10012 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10013 has computed. */
10014 gdb::unique_xmalloc_ptr<char> demangled;
10015 if (mangled != NULL)
10016 {
10017 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10018 {
10019 /* Do nothing (do not demangle the symbol name). */
10020 }
10021 else
10022 {
10023 /* Use DMGL_RET_DROP for C++ template functions to suppress
10024 their return type. It is easier for GDB users to search
10025 for such functions as `name(params)' than `long name(params)'.
10026 In such case the minimal symbol names do not match the full
10027 symbol names but for template functions there is never a need
10028 to look up their definition from their declaration so
10029 the only disadvantage remains the minimal symbol variant
10030 `long name(params)' does not have the proper inferior type. */
10031 demangled.reset (gdb_demangle (mangled,
10032 (DMGL_PARAMS | DMGL_ANSI
10033 | DMGL_RET_DROP)));
10034 }
10035 if (demangled)
10036 canon = demangled.get ();
10037 else
10038 {
10039 canon = mangled;
10040 need_copy = 0;
10041 }
10042 }
10043
10044 if (canon == NULL || check_physname)
10045 {
10046 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10047
10048 if (canon != NULL && strcmp (physname, canon) != 0)
10049 {
10050 /* It may not mean a bug in GDB. The compiler could also
10051 compute DW_AT_linkage_name incorrectly. But in such case
10052 GDB would need to be bug-to-bug compatible. */
10053
10054 complaint (_("Computed physname <%s> does not match demangled <%s> "
10055 "(from linkage <%s>) - DIE at %s [in module %s]"),
10056 physname, canon, mangled, sect_offset_str (die->sect_off),
10057 objfile_name (objfile));
10058
10059 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10060 is available here - over computed PHYSNAME. It is safer
10061 against both buggy GDB and buggy compilers. */
10062
10063 retval = canon;
10064 }
10065 else
10066 {
10067 retval = physname;
10068 need_copy = 0;
10069 }
10070 }
10071 else
10072 retval = canon;
10073
10074 if (need_copy)
10075 retval = objfile->intern (retval);
10076
10077 return retval;
10078 }
10079
10080 /* Inspect DIE in CU for a namespace alias. If one exists, record
10081 a new symbol for it.
10082
10083 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10084
10085 static int
10086 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10087 {
10088 struct attribute *attr;
10089
10090 /* If the die does not have a name, this is not a namespace
10091 alias. */
10092 attr = dwarf2_attr (die, DW_AT_name, cu);
10093 if (attr != NULL)
10094 {
10095 int num;
10096 struct die_info *d = die;
10097 struct dwarf2_cu *imported_cu = cu;
10098
10099 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10100 keep inspecting DIEs until we hit the underlying import. */
10101 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10102 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10103 {
10104 attr = dwarf2_attr (d, DW_AT_import, cu);
10105 if (attr == NULL)
10106 break;
10107
10108 d = follow_die_ref (d, attr, &imported_cu);
10109 if (d->tag != DW_TAG_imported_declaration)
10110 break;
10111 }
10112
10113 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10114 {
10115 complaint (_("DIE at %s has too many recursively imported "
10116 "declarations"), sect_offset_str (d->sect_off));
10117 return 0;
10118 }
10119
10120 if (attr != NULL)
10121 {
10122 struct type *type;
10123 sect_offset sect_off = attr->get_ref_die_offset ();
10124
10125 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10126 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10127 {
10128 /* This declaration is a global namespace alias. Add
10129 a symbol for it whose type is the aliased namespace. */
10130 new_symbol (die, type, cu);
10131 return 1;
10132 }
10133 }
10134 }
10135
10136 return 0;
10137 }
10138
10139 /* Return the using directives repository (global or local?) to use in the
10140 current context for CU.
10141
10142 For Ada, imported declarations can materialize renamings, which *may* be
10143 global. However it is impossible (for now?) in DWARF to distinguish
10144 "external" imported declarations and "static" ones. As all imported
10145 declarations seem to be static in all other languages, make them all CU-wide
10146 global only in Ada. */
10147
10148 static struct using_direct **
10149 using_directives (struct dwarf2_cu *cu)
10150 {
10151 if (cu->per_cu->lang == language_ada
10152 && cu->get_builder ()->outermost_context_p ())
10153 return cu->get_builder ()->get_global_using_directives ();
10154 else
10155 return cu->get_builder ()->get_local_using_directives ();
10156 }
10157
10158 /* Read the import statement specified by the given die and record it. */
10159
10160 static void
10161 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10162 {
10163 struct objfile *objfile = cu->per_objfile->objfile;
10164 struct attribute *import_attr;
10165 struct die_info *imported_die, *child_die;
10166 struct dwarf2_cu *imported_cu;
10167 const char *imported_name;
10168 const char *imported_name_prefix;
10169 const char *canonical_name;
10170 const char *import_alias;
10171 const char *imported_declaration = NULL;
10172 const char *import_prefix;
10173 std::vector<const char *> excludes;
10174
10175 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10176 if (import_attr == NULL)
10177 {
10178 complaint (_("Tag '%s' has no DW_AT_import"),
10179 dwarf_tag_name (die->tag));
10180 return;
10181 }
10182
10183 imported_cu = cu;
10184 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10185 imported_name = dwarf2_name (imported_die, imported_cu);
10186 if (imported_name == NULL)
10187 {
10188 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10189
10190 The import in the following code:
10191 namespace A
10192 {
10193 typedef int B;
10194 }
10195
10196 int main ()
10197 {
10198 using A::B;
10199 B b;
10200 return b;
10201 }
10202
10203 ...
10204 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10205 <52> DW_AT_decl_file : 1
10206 <53> DW_AT_decl_line : 6
10207 <54> DW_AT_import : <0x75>
10208 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10209 <59> DW_AT_name : B
10210 <5b> DW_AT_decl_file : 1
10211 <5c> DW_AT_decl_line : 2
10212 <5d> DW_AT_type : <0x6e>
10213 ...
10214 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10215 <76> DW_AT_byte_size : 4
10216 <77> DW_AT_encoding : 5 (signed)
10217
10218 imports the wrong die ( 0x75 instead of 0x58 ).
10219 This case will be ignored until the gcc bug is fixed. */
10220 return;
10221 }
10222
10223 /* Figure out the local name after import. */
10224 import_alias = dwarf2_name (die, cu);
10225
10226 /* Figure out where the statement is being imported to. */
10227 import_prefix = determine_prefix (die, cu);
10228
10229 /* Figure out what the scope of the imported die is and prepend it
10230 to the name of the imported die. */
10231 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10232
10233 if (imported_die->tag != DW_TAG_namespace
10234 && imported_die->tag != DW_TAG_module)
10235 {
10236 imported_declaration = imported_name;
10237 canonical_name = imported_name_prefix;
10238 }
10239 else if (strlen (imported_name_prefix) > 0)
10240 canonical_name = obconcat (&objfile->objfile_obstack,
10241 imported_name_prefix,
10242 (cu->per_cu->lang == language_d
10243 ? "."
10244 : "::"),
10245 imported_name, (char *) NULL);
10246 else
10247 canonical_name = imported_name;
10248
10249 if (die->tag == DW_TAG_imported_module
10250 && cu->per_cu->lang == language_fortran)
10251 for (child_die = die->child; child_die && child_die->tag;
10252 child_die = child_die->sibling)
10253 {
10254 /* DWARF-4: A Fortran use statement with a “rename list” may be
10255 represented by an imported module entry with an import attribute
10256 referring to the module and owned entries corresponding to those
10257 entities that are renamed as part of being imported. */
10258
10259 if (child_die->tag != DW_TAG_imported_declaration)
10260 {
10261 complaint (_("child DW_TAG_imported_declaration expected "
10262 "- DIE at %s [in module %s]"),
10263 sect_offset_str (child_die->sect_off),
10264 objfile_name (objfile));
10265 continue;
10266 }
10267
10268 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10269 if (import_attr == NULL)
10270 {
10271 complaint (_("Tag '%s' has no DW_AT_import"),
10272 dwarf_tag_name (child_die->tag));
10273 continue;
10274 }
10275
10276 imported_cu = cu;
10277 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10278 &imported_cu);
10279 imported_name = dwarf2_name (imported_die, imported_cu);
10280 if (imported_name == NULL)
10281 {
10282 complaint (_("child DW_TAG_imported_declaration has unknown "
10283 "imported name - DIE at %s [in module %s]"),
10284 sect_offset_str (child_die->sect_off),
10285 objfile_name (objfile));
10286 continue;
10287 }
10288
10289 excludes.push_back (imported_name);
10290
10291 process_die (child_die, cu);
10292 }
10293
10294 add_using_directive (using_directives (cu),
10295 import_prefix,
10296 canonical_name,
10297 import_alias,
10298 imported_declaration,
10299 excludes,
10300 0,
10301 &objfile->objfile_obstack);
10302 }
10303
10304 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10305 types, but gives them a size of zero. Starting with version 14,
10306 ICC is compatible with GCC. */
10307
10308 static bool
10309 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10310 {
10311 if (!cu->checked_producer)
10312 check_producer (cu);
10313
10314 return cu->producer_is_icc_lt_14;
10315 }
10316
10317 /* ICC generates a DW_AT_type for C void functions. This was observed on
10318 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10319 which says that void functions should not have a DW_AT_type. */
10320
10321 static bool
10322 producer_is_icc (struct dwarf2_cu *cu)
10323 {
10324 if (!cu->checked_producer)
10325 check_producer (cu);
10326
10327 return cu->producer_is_icc;
10328 }
10329
10330 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10331 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10332 this, it was first present in GCC release 4.3.0. */
10333
10334 static bool
10335 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10336 {
10337 if (!cu->checked_producer)
10338 check_producer (cu);
10339
10340 return cu->producer_is_gcc_lt_4_3;
10341 }
10342
10343 static file_and_directory
10344 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10345 {
10346 file_and_directory res;
10347
10348 /* Find the filename. Do not use dwarf2_name here, since the filename
10349 is not a source language identifier. */
10350 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10351 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10352
10353 if (res.comp_dir == NULL
10354 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10355 && IS_ABSOLUTE_PATH (res.name))
10356 {
10357 res.comp_dir_storage = ldirname (res.name);
10358 if (!res.comp_dir_storage.empty ())
10359 res.comp_dir = res.comp_dir_storage.c_str ();
10360 }
10361 if (res.comp_dir != NULL)
10362 {
10363 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10364 directory, get rid of it. */
10365 const char *cp = strchr (res.comp_dir, ':');
10366
10367 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10368 res.comp_dir = cp + 1;
10369 }
10370
10371 if (res.name == NULL)
10372 res.name = "<unknown>";
10373
10374 return res;
10375 }
10376
10377 /* Handle DW_AT_stmt_list for a compilation unit.
10378 DIE is the DW_TAG_compile_unit die for CU.
10379 COMP_DIR is the compilation directory. LOWPC is passed to
10380 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10381
10382 static void
10383 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10384 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10385 {
10386 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10387 struct attribute *attr;
10388 struct line_header line_header_local;
10389 hashval_t line_header_local_hash;
10390 void **slot;
10391 int decode_mapping;
10392
10393 gdb_assert (! cu->per_cu->is_debug_types);
10394
10395 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10396 if (attr == NULL || !attr->form_is_unsigned ())
10397 return;
10398
10399 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10400
10401 /* The line header hash table is only created if needed (it exists to
10402 prevent redundant reading of the line table for partial_units).
10403 If we're given a partial_unit, we'll need it. If we're given a
10404 compile_unit, then use the line header hash table if it's already
10405 created, but don't create one just yet. */
10406
10407 if (per_objfile->line_header_hash == NULL
10408 && die->tag == DW_TAG_partial_unit)
10409 {
10410 per_objfile->line_header_hash
10411 .reset (htab_create_alloc (127, line_header_hash_voidp,
10412 line_header_eq_voidp,
10413 htab_delete_entry<line_header>,
10414 xcalloc, xfree));
10415 }
10416
10417 line_header_local.sect_off = line_offset;
10418 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10419 line_header_local_hash = line_header_hash (&line_header_local);
10420 if (per_objfile->line_header_hash != NULL)
10421 {
10422 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10423 &line_header_local,
10424 line_header_local_hash, NO_INSERT);
10425
10426 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10427 is not present in *SLOT (since if there is something in *SLOT then
10428 it will be for a partial_unit). */
10429 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10430 {
10431 gdb_assert (*slot != NULL);
10432 cu->line_header = (struct line_header *) *slot;
10433 return;
10434 }
10435 }
10436
10437 /* dwarf_decode_line_header does not yet provide sufficient information.
10438 We always have to call also dwarf_decode_lines for it. */
10439 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10440 if (lh == NULL)
10441 return;
10442
10443 cu->line_header = lh.release ();
10444 cu->line_header_die_owner = die;
10445
10446 if (per_objfile->line_header_hash == NULL)
10447 slot = NULL;
10448 else
10449 {
10450 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10451 &line_header_local,
10452 line_header_local_hash, INSERT);
10453 gdb_assert (slot != NULL);
10454 }
10455 if (slot != NULL && *slot == NULL)
10456 {
10457 /* This newly decoded line number information unit will be owned
10458 by line_header_hash hash table. */
10459 *slot = cu->line_header;
10460 cu->line_header_die_owner = NULL;
10461 }
10462 else
10463 {
10464 /* We cannot free any current entry in (*slot) as that struct line_header
10465 may be already used by multiple CUs. Create only temporary decoded
10466 line_header for this CU - it may happen at most once for each line
10467 number information unit. And if we're not using line_header_hash
10468 then this is what we want as well. */
10469 gdb_assert (die->tag != DW_TAG_partial_unit);
10470 }
10471 decode_mapping = (die->tag != DW_TAG_partial_unit);
10472 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10473 decode_mapping);
10474
10475 }
10476
10477 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10478
10479 static void
10480 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10481 {
10482 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10483 struct objfile *objfile = per_objfile->objfile;
10484 struct gdbarch *gdbarch = objfile->arch ();
10485 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10486 CORE_ADDR highpc = ((CORE_ADDR) 0);
10487 struct attribute *attr;
10488 struct die_info *child_die;
10489 CORE_ADDR baseaddr;
10490
10491 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
10492 baseaddr = objfile->text_section_offset ();
10493
10494 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10495
10496 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10497 from finish_block. */
10498 if (lowpc == ((CORE_ADDR) -1))
10499 lowpc = highpc;
10500 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10501
10502 file_and_directory fnd = find_file_and_directory (die, cu);
10503
10504 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10505
10506 gdb_assert (per_objfile->sym_cu == nullptr);
10507 scoped_restore restore_sym_cu
10508 = make_scoped_restore (&per_objfile->sym_cu, cu);
10509
10510 /* Decode line number information if present. We do this before
10511 processing child DIEs, so that the line header table is available
10512 for DW_AT_decl_file. */
10513 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10514
10515 /* Process all dies in compilation unit. */
10516 if (die->child != NULL)
10517 {
10518 child_die = die->child;
10519 while (child_die && child_die->tag)
10520 {
10521 process_die (child_die, cu);
10522 child_die = child_die->sibling;
10523 }
10524 }
10525 per_objfile->sym_cu = nullptr;
10526
10527 /* Decode macro information, if present. Dwarf 2 macro information
10528 refers to information in the line number info statement program
10529 header, so we can only read it if we've read the header
10530 successfully. */
10531 attr = dwarf2_attr (die, DW_AT_macros, cu);
10532 if (attr == NULL)
10533 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10534 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10535 {
10536 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10537 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10538
10539 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10540 }
10541 else
10542 {
10543 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10544 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10545 {
10546 unsigned int macro_offset = attr->as_unsigned ();
10547
10548 dwarf_decode_macros (cu, macro_offset, 0);
10549 }
10550 }
10551 }
10552
10553 void
10554 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10555 {
10556 struct type_unit_group *tu_group;
10557 int first_time;
10558 struct attribute *attr;
10559 unsigned int i;
10560 struct signatured_type *sig_type;
10561
10562 gdb_assert (per_cu->is_debug_types);
10563 sig_type = (struct signatured_type *) per_cu;
10564
10565 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10566
10567 /* If we're using .gdb_index (includes -readnow) then
10568 per_cu->type_unit_group may not have been set up yet. */
10569 if (sig_type->type_unit_group == NULL)
10570 sig_type->type_unit_group = get_type_unit_group (this, attr);
10571 tu_group = sig_type->type_unit_group;
10572
10573 /* If we've already processed this stmt_list there's no real need to
10574 do it again, we could fake it and just recreate the part we need
10575 (file name,index -> symtab mapping). If data shows this optimization
10576 is useful we can do it then. */
10577 type_unit_group_unshareable *tug_unshare
10578 = per_objfile->get_type_unit_group_unshareable (tu_group);
10579 first_time = tug_unshare->compunit_symtab == NULL;
10580
10581 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10582 debug info. */
10583 line_header_up lh;
10584 if (attr != NULL && attr->form_is_unsigned ())
10585 {
10586 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10587 lh = dwarf_decode_line_header (line_offset, this);
10588 }
10589 if (lh == NULL)
10590 {
10591 if (first_time)
10592 start_symtab ("", NULL, 0);
10593 else
10594 {
10595 gdb_assert (tug_unshare->symtabs == NULL);
10596 gdb_assert (m_builder == nullptr);
10597 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10598 m_builder.reset (new struct buildsym_compunit
10599 (COMPUNIT_OBJFILE (cust), "",
10600 COMPUNIT_DIRNAME (cust),
10601 compunit_language (cust),
10602 0, cust));
10603 list_in_scope = get_builder ()->get_file_symbols ();
10604 }
10605 return;
10606 }
10607
10608 line_header = lh.release ();
10609 line_header_die_owner = die;
10610
10611 if (first_time)
10612 {
10613 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10614
10615 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10616 still initializing it, and our caller (a few levels up)
10617 process_full_type_unit still needs to know if this is the first
10618 time. */
10619
10620 tug_unshare->symtabs
10621 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10622 struct symtab *, line_header->file_names_size ());
10623
10624 auto &file_names = line_header->file_names ();
10625 for (i = 0; i < file_names.size (); ++i)
10626 {
10627 file_entry &fe = file_names[i];
10628 dwarf2_start_subfile (this, fe.name,
10629 fe.include_dir (line_header));
10630 buildsym_compunit *b = get_builder ();
10631 if (b->get_current_subfile ()->symtab == NULL)
10632 {
10633 /* NOTE: start_subfile will recognize when it's been
10634 passed a file it has already seen. So we can't
10635 assume there's a simple mapping from
10636 cu->line_header->file_names to subfiles, plus
10637 cu->line_header->file_names may contain dups. */
10638 b->get_current_subfile ()->symtab
10639 = allocate_symtab (cust, b->get_current_subfile ()->name);
10640 }
10641
10642 fe.symtab = b->get_current_subfile ()->symtab;
10643 tug_unshare->symtabs[i] = fe.symtab;
10644 }
10645 }
10646 else
10647 {
10648 gdb_assert (m_builder == nullptr);
10649 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10650 m_builder.reset (new struct buildsym_compunit
10651 (COMPUNIT_OBJFILE (cust), "",
10652 COMPUNIT_DIRNAME (cust),
10653 compunit_language (cust),
10654 0, cust));
10655 list_in_scope = get_builder ()->get_file_symbols ();
10656
10657 auto &file_names = line_header->file_names ();
10658 for (i = 0; i < file_names.size (); ++i)
10659 {
10660 file_entry &fe = file_names[i];
10661 fe.symtab = tug_unshare->symtabs[i];
10662 }
10663 }
10664
10665 /* The main symtab is allocated last. Type units don't have DW_AT_name
10666 so they don't have a "real" (so to speak) symtab anyway.
10667 There is later code that will assign the main symtab to all symbols
10668 that don't have one. We need to handle the case of a symbol with a
10669 missing symtab (DW_AT_decl_file) anyway. */
10670 }
10671
10672 /* Process DW_TAG_type_unit.
10673 For TUs we want to skip the first top level sibling if it's not the
10674 actual type being defined by this TU. In this case the first top
10675 level sibling is there to provide context only. */
10676
10677 static void
10678 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10679 {
10680 struct die_info *child_die;
10681
10682 prepare_one_comp_unit (cu, die, language_minimal);
10683
10684 /* Initialize (or reinitialize) the machinery for building symtabs.
10685 We do this before processing child DIEs, so that the line header table
10686 is available for DW_AT_decl_file. */
10687 cu->setup_type_unit_groups (die);
10688
10689 if (die->child != NULL)
10690 {
10691 child_die = die->child;
10692 while (child_die && child_die->tag)
10693 {
10694 process_die (child_die, cu);
10695 child_die = child_die->sibling;
10696 }
10697 }
10698 }
10699 \f
10700 /* DWO/DWP files.
10701
10702 http://gcc.gnu.org/wiki/DebugFission
10703 http://gcc.gnu.org/wiki/DebugFissionDWP
10704
10705 To simplify handling of both DWO files ("object" files with the DWARF info)
10706 and DWP files (a file with the DWOs packaged up into one file), we treat
10707 DWP files as having a collection of virtual DWO files. */
10708
10709 static hashval_t
10710 hash_dwo_file (const void *item)
10711 {
10712 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10713 hashval_t hash;
10714
10715 hash = htab_hash_string (dwo_file->dwo_name);
10716 if (dwo_file->comp_dir != NULL)
10717 hash += htab_hash_string (dwo_file->comp_dir);
10718 return hash;
10719 }
10720
10721 static int
10722 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10723 {
10724 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10725 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10726
10727 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10728 return 0;
10729 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10730 return lhs->comp_dir == rhs->comp_dir;
10731 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10732 }
10733
10734 /* Allocate a hash table for DWO files. */
10735
10736 static htab_up
10737 allocate_dwo_file_hash_table ()
10738 {
10739 return htab_up (htab_create_alloc (41,
10740 hash_dwo_file,
10741 eq_dwo_file,
10742 htab_delete_entry<dwo_file>,
10743 xcalloc, xfree));
10744 }
10745
10746 /* Lookup DWO file DWO_NAME. */
10747
10748 static void **
10749 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10750 const char *dwo_name,
10751 const char *comp_dir)
10752 {
10753 struct dwo_file find_entry;
10754 void **slot;
10755
10756 if (per_objfile->per_bfd->dwo_files == NULL)
10757 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10758
10759 find_entry.dwo_name = dwo_name;
10760 find_entry.comp_dir = comp_dir;
10761 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10762 INSERT);
10763
10764 return slot;
10765 }
10766
10767 static hashval_t
10768 hash_dwo_unit (const void *item)
10769 {
10770 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10771
10772 /* This drops the top 32 bits of the id, but is ok for a hash. */
10773 return dwo_unit->signature;
10774 }
10775
10776 static int
10777 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10778 {
10779 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10780 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10781
10782 /* The signature is assumed to be unique within the DWO file.
10783 So while object file CU dwo_id's always have the value zero,
10784 that's OK, assuming each object file DWO file has only one CU,
10785 and that's the rule for now. */
10786 return lhs->signature == rhs->signature;
10787 }
10788
10789 /* Allocate a hash table for DWO CUs,TUs.
10790 There is one of these tables for each of CUs,TUs for each DWO file. */
10791
10792 static htab_up
10793 allocate_dwo_unit_table ()
10794 {
10795 /* Start out with a pretty small number.
10796 Generally DWO files contain only one CU and maybe some TUs. */
10797 return htab_up (htab_create_alloc (3,
10798 hash_dwo_unit,
10799 eq_dwo_unit,
10800 NULL, xcalloc, xfree));
10801 }
10802
10803 /* die_reader_func for create_dwo_cu. */
10804
10805 static void
10806 create_dwo_cu_reader (const struct die_reader_specs *reader,
10807 const gdb_byte *info_ptr,
10808 struct die_info *comp_unit_die,
10809 struct dwo_file *dwo_file,
10810 struct dwo_unit *dwo_unit)
10811 {
10812 struct dwarf2_cu *cu = reader->cu;
10813 sect_offset sect_off = cu->per_cu->sect_off;
10814 struct dwarf2_section_info *section = cu->per_cu->section;
10815
10816 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10817 if (!signature.has_value ())
10818 {
10819 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10820 " its dwo_id [in module %s]"),
10821 sect_offset_str (sect_off), dwo_file->dwo_name);
10822 return;
10823 }
10824
10825 dwo_unit->dwo_file = dwo_file;
10826 dwo_unit->signature = *signature;
10827 dwo_unit->section = section;
10828 dwo_unit->sect_off = sect_off;
10829 dwo_unit->length = cu->per_cu->length;
10830
10831 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10832 sect_offset_str (sect_off),
10833 hex_string (dwo_unit->signature));
10834 }
10835
10836 /* Create the dwo_units for the CUs in a DWO_FILE.
10837 Note: This function processes DWO files only, not DWP files. */
10838
10839 static void
10840 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10841 dwarf2_cu *cu, struct dwo_file &dwo_file,
10842 dwarf2_section_info &section, htab_up &cus_htab)
10843 {
10844 struct objfile *objfile = per_objfile->objfile;
10845 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10846 const gdb_byte *info_ptr, *end_ptr;
10847
10848 section.read (objfile);
10849 info_ptr = section.buffer;
10850
10851 if (info_ptr == NULL)
10852 return;
10853
10854 dwarf_read_debug_printf ("Reading %s for %s:",
10855 section.get_name (),
10856 section.get_file_name ());
10857
10858 end_ptr = info_ptr + section.size;
10859 while (info_ptr < end_ptr)
10860 {
10861 struct dwarf2_per_cu_data per_cu;
10862 struct dwo_unit read_unit {};
10863 struct dwo_unit *dwo_unit;
10864 void **slot;
10865 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10866
10867 per_cu.per_bfd = per_bfd;
10868 per_cu.is_debug_types = 0;
10869 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10870 per_cu.section = &section;
10871
10872 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
10873 if (!reader.dummy_p)
10874 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
10875 &dwo_file, &read_unit);
10876 info_ptr += per_cu.length;
10877
10878 // If the unit could not be parsed, skip it.
10879 if (read_unit.dwo_file == NULL)
10880 continue;
10881
10882 if (cus_htab == NULL)
10883 cus_htab = allocate_dwo_unit_table ();
10884
10885 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
10886 struct dwo_unit);
10887 *dwo_unit = read_unit;
10888 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
10889 gdb_assert (slot != NULL);
10890 if (*slot != NULL)
10891 {
10892 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10893 sect_offset dup_sect_off = dup_cu->sect_off;
10894
10895 complaint (_("debug cu entry at offset %s is duplicate to"
10896 " the entry at offset %s, signature %s"),
10897 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
10898 hex_string (dwo_unit->signature));
10899 }
10900 *slot = (void *)dwo_unit;
10901 }
10902 }
10903
10904 /* DWP file .debug_{cu,tu}_index section format:
10905 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10906 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10907
10908 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10909 officially standard DWP format was published with DWARF v5 and is called
10910 Version 5. There are no versions 3 or 4.
10911
10912 DWP Version 1:
10913
10914 Both index sections have the same format, and serve to map a 64-bit
10915 signature to a set of section numbers. Each section begins with a header,
10916 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10917 indexes, and a pool of 32-bit section numbers. The index sections will be
10918 aligned at 8-byte boundaries in the file.
10919
10920 The index section header consists of:
10921
10922 V, 32 bit version number
10923 -, 32 bits unused
10924 N, 32 bit number of compilation units or type units in the index
10925 M, 32 bit number of slots in the hash table
10926
10927 Numbers are recorded using the byte order of the application binary.
10928
10929 The hash table begins at offset 16 in the section, and consists of an array
10930 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10931 order of the application binary). Unused slots in the hash table are 0.
10932 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10933
10934 The parallel table begins immediately after the hash table
10935 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10936 array of 32-bit indexes (using the byte order of the application binary),
10937 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10938 table contains a 32-bit index into the pool of section numbers. For unused
10939 hash table slots, the corresponding entry in the parallel table will be 0.
10940
10941 The pool of section numbers begins immediately following the hash table
10942 (at offset 16 + 12 * M from the beginning of the section). The pool of
10943 section numbers consists of an array of 32-bit words (using the byte order
10944 of the application binary). Each item in the array is indexed starting
10945 from 0. The hash table entry provides the index of the first section
10946 number in the set. Additional section numbers in the set follow, and the
10947 set is terminated by a 0 entry (section number 0 is not used in ELF).
10948
10949 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10950 section must be the first entry in the set, and the .debug_abbrev.dwo must
10951 be the second entry. Other members of the set may follow in any order.
10952
10953 ---
10954
10955 DWP Versions 2 and 5:
10956
10957 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10958 and the entries in the index tables are now offsets into these sections.
10959 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10960 section.
10961
10962 Index Section Contents:
10963 Header
10964 Hash Table of Signatures dwp_hash_table.hash_table
10965 Parallel Table of Indices dwp_hash_table.unit_table
10966 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10967 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10968
10969 The index section header consists of:
10970
10971 V, 32 bit version number
10972 L, 32 bit number of columns in the table of section offsets
10973 N, 32 bit number of compilation units or type units in the index
10974 M, 32 bit number of slots in the hash table
10975
10976 Numbers are recorded using the byte order of the application binary.
10977
10978 The hash table has the same format as version 1.
10979 The parallel table of indices has the same format as version 1,
10980 except that the entries are origin-1 indices into the table of sections
10981 offsets and the table of section sizes.
10982
10983 The table of offsets begins immediately following the parallel table
10984 (at offset 16 + 12 * M from the beginning of the section). The table is
10985 a two-dimensional array of 32-bit words (using the byte order of the
10986 application binary), with L columns and N+1 rows, in row-major order.
10987 Each row in the array is indexed starting from 0. The first row provides
10988 a key to the remaining rows: each column in this row provides an identifier
10989 for a debug section, and the offsets in the same column of subsequent rows
10990 refer to that section. The section identifiers for Version 2 are:
10991
10992 DW_SECT_INFO 1 .debug_info.dwo
10993 DW_SECT_TYPES 2 .debug_types.dwo
10994 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10995 DW_SECT_LINE 4 .debug_line.dwo
10996 DW_SECT_LOC 5 .debug_loc.dwo
10997 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10998 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10999 DW_SECT_MACRO 8 .debug_macro.dwo
11000
11001 The section identifiers for Version 5 are:
11002
11003 DW_SECT_INFO_V5 1 .debug_info.dwo
11004 DW_SECT_RESERVED_V5 2 --
11005 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11006 DW_SECT_LINE_V5 4 .debug_line.dwo
11007 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11008 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11009 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11010 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11011
11012 The offsets provided by the CU and TU index sections are the base offsets
11013 for the contributions made by each CU or TU to the corresponding section
11014 in the package file. Each CU and TU header contains an abbrev_offset
11015 field, used to find the abbreviations table for that CU or TU within the
11016 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11017 be interpreted as relative to the base offset given in the index section.
11018 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11019 should be interpreted as relative to the base offset for .debug_line.dwo,
11020 and offsets into other debug sections obtained from DWARF attributes should
11021 also be interpreted as relative to the corresponding base offset.
11022
11023 The table of sizes begins immediately following the table of offsets.
11024 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11025 with L columns and N rows, in row-major order. Each row in the array is
11026 indexed starting from 1 (row 0 is shared by the two tables).
11027
11028 ---
11029
11030 Hash table lookup is handled the same in version 1 and 2:
11031
11032 We assume that N and M will not exceed 2^32 - 1.
11033 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11034
11035 Given a 64-bit compilation unit signature or a type signature S, an entry
11036 in the hash table is located as follows:
11037
11038 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11039 the low-order k bits all set to 1.
11040
11041 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11042
11043 3) If the hash table entry at index H matches the signature, use that
11044 entry. If the hash table entry at index H is unused (all zeroes),
11045 terminate the search: the signature is not present in the table.
11046
11047 4) Let H = (H + H') modulo M. Repeat at Step 3.
11048
11049 Because M > N and H' and M are relatively prime, the search is guaranteed
11050 to stop at an unused slot or find the match. */
11051
11052 /* Create a hash table to map DWO IDs to their CU/TU entry in
11053 .debug_{info,types}.dwo in DWP_FILE.
11054 Returns NULL if there isn't one.
11055 Note: This function processes DWP files only, not DWO files. */
11056
11057 static struct dwp_hash_table *
11058 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11059 struct dwp_file *dwp_file, int is_debug_types)
11060 {
11061 struct objfile *objfile = per_objfile->objfile;
11062 bfd *dbfd = dwp_file->dbfd.get ();
11063 const gdb_byte *index_ptr, *index_end;
11064 struct dwarf2_section_info *index;
11065 uint32_t version, nr_columns, nr_units, nr_slots;
11066 struct dwp_hash_table *htab;
11067
11068 if (is_debug_types)
11069 index = &dwp_file->sections.tu_index;
11070 else
11071 index = &dwp_file->sections.cu_index;
11072
11073 if (index->empty ())
11074 return NULL;
11075 index->read (objfile);
11076
11077 index_ptr = index->buffer;
11078 index_end = index_ptr + index->size;
11079
11080 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11081 For now it's safe to just read 4 bytes (particularly as it's difficult to
11082 tell if you're dealing with Version 5 before you've read the version). */
11083 version = read_4_bytes (dbfd, index_ptr);
11084 index_ptr += 4;
11085 if (version == 2 || version == 5)
11086 nr_columns = read_4_bytes (dbfd, index_ptr);
11087 else
11088 nr_columns = 0;
11089 index_ptr += 4;
11090 nr_units = read_4_bytes (dbfd, index_ptr);
11091 index_ptr += 4;
11092 nr_slots = read_4_bytes (dbfd, index_ptr);
11093 index_ptr += 4;
11094
11095 if (version != 1 && version != 2 && version != 5)
11096 {
11097 error (_("Dwarf Error: unsupported DWP file version (%s)"
11098 " [in module %s]"),
11099 pulongest (version), dwp_file->name);
11100 }
11101 if (nr_slots != (nr_slots & -nr_slots))
11102 {
11103 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11104 " is not power of 2 [in module %s]"),
11105 pulongest (nr_slots), dwp_file->name);
11106 }
11107
11108 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11109 htab->version = version;
11110 htab->nr_columns = nr_columns;
11111 htab->nr_units = nr_units;
11112 htab->nr_slots = nr_slots;
11113 htab->hash_table = index_ptr;
11114 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11115
11116 /* Exit early if the table is empty. */
11117 if (nr_slots == 0 || nr_units == 0
11118 || (version == 2 && nr_columns == 0)
11119 || (version == 5 && nr_columns == 0))
11120 {
11121 /* All must be zero. */
11122 if (nr_slots != 0 || nr_units != 0
11123 || (version == 2 && nr_columns != 0)
11124 || (version == 5 && nr_columns != 0))
11125 {
11126 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11127 " all zero [in modules %s]"),
11128 dwp_file->name);
11129 }
11130 return htab;
11131 }
11132
11133 if (version == 1)
11134 {
11135 htab->section_pool.v1.indices =
11136 htab->unit_table + sizeof (uint32_t) * nr_slots;
11137 /* It's harder to decide whether the section is too small in v1.
11138 V1 is deprecated anyway so we punt. */
11139 }
11140 else if (version == 2)
11141 {
11142 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11143 int *ids = htab->section_pool.v2.section_ids;
11144 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11145 /* Reverse map for error checking. */
11146 int ids_seen[DW_SECT_MAX + 1];
11147 int i;
11148
11149 if (nr_columns < 2)
11150 {
11151 error (_("Dwarf Error: bad DWP hash table, too few columns"
11152 " in section table [in module %s]"),
11153 dwp_file->name);
11154 }
11155 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11156 {
11157 error (_("Dwarf Error: bad DWP hash table, too many columns"
11158 " in section table [in module %s]"),
11159 dwp_file->name);
11160 }
11161 memset (ids, 255, sizeof_ids);
11162 memset (ids_seen, 255, sizeof (ids_seen));
11163 for (i = 0; i < nr_columns; ++i)
11164 {
11165 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11166
11167 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11168 {
11169 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11170 " in section table [in module %s]"),
11171 id, dwp_file->name);
11172 }
11173 if (ids_seen[id] != -1)
11174 {
11175 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11176 " id %d in section table [in module %s]"),
11177 id, dwp_file->name);
11178 }
11179 ids_seen[id] = i;
11180 ids[i] = id;
11181 }
11182 /* Must have exactly one info or types section. */
11183 if (((ids_seen[DW_SECT_INFO] != -1)
11184 + (ids_seen[DW_SECT_TYPES] != -1))
11185 != 1)
11186 {
11187 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11188 " DWO info/types section [in module %s]"),
11189 dwp_file->name);
11190 }
11191 /* Must have an abbrev section. */
11192 if (ids_seen[DW_SECT_ABBREV] == -1)
11193 {
11194 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11195 " section [in module %s]"),
11196 dwp_file->name);
11197 }
11198 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11199 htab->section_pool.v2.sizes =
11200 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11201 * nr_units * nr_columns);
11202 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11203 * nr_units * nr_columns))
11204 > index_end)
11205 {
11206 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11207 " [in module %s]"),
11208 dwp_file->name);
11209 }
11210 }
11211 else /* version == 5 */
11212 {
11213 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11214 int *ids = htab->section_pool.v5.section_ids;
11215 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11216 /* Reverse map for error checking. */
11217 int ids_seen[DW_SECT_MAX_V5 + 1];
11218
11219 if (nr_columns < 2)
11220 {
11221 error (_("Dwarf Error: bad DWP hash table, too few columns"
11222 " in section table [in module %s]"),
11223 dwp_file->name);
11224 }
11225 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11226 {
11227 error (_("Dwarf Error: bad DWP hash table, too many columns"
11228 " in section table [in module %s]"),
11229 dwp_file->name);
11230 }
11231 memset (ids, 255, sizeof_ids);
11232 memset (ids_seen, 255, sizeof (ids_seen));
11233 for (int i = 0; i < nr_columns; ++i)
11234 {
11235 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11236
11237 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11238 {
11239 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11240 " in section table [in module %s]"),
11241 id, dwp_file->name);
11242 }
11243 if (ids_seen[id] != -1)
11244 {
11245 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11246 " id %d in section table [in module %s]"),
11247 id, dwp_file->name);
11248 }
11249 ids_seen[id] = i;
11250 ids[i] = id;
11251 }
11252 /* Must have seen an info section. */
11253 if (ids_seen[DW_SECT_INFO_V5] == -1)
11254 {
11255 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11256 " DWO info/types section [in module %s]"),
11257 dwp_file->name);
11258 }
11259 /* Must have an abbrev section. */
11260 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11261 {
11262 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11263 " section [in module %s]"),
11264 dwp_file->name);
11265 }
11266 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11267 htab->section_pool.v5.sizes
11268 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11269 * nr_units * nr_columns);
11270 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11271 * nr_units * nr_columns))
11272 > index_end)
11273 {
11274 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11275 " [in module %s]"),
11276 dwp_file->name);
11277 }
11278 }
11279
11280 return htab;
11281 }
11282
11283 /* Update SECTIONS with the data from SECTP.
11284
11285 This function is like the other "locate" section routines, but in
11286 this context the sections to read comes from the DWP V1 hash table,
11287 not the full ELF section table.
11288
11289 The result is non-zero for success, or zero if an error was found. */
11290
11291 static int
11292 locate_v1_virtual_dwo_sections (asection *sectp,
11293 struct virtual_v1_dwo_sections *sections)
11294 {
11295 const struct dwop_section_names *names = &dwop_section_names;
11296
11297 if (names->abbrev_dwo.matches (sectp->name))
11298 {
11299 /* There can be only one. */
11300 if (sections->abbrev.s.section != NULL)
11301 return 0;
11302 sections->abbrev.s.section = sectp;
11303 sections->abbrev.size = bfd_section_size (sectp);
11304 }
11305 else if (names->info_dwo.matches (sectp->name)
11306 || names->types_dwo.matches (sectp->name))
11307 {
11308 /* There can be only one. */
11309 if (sections->info_or_types.s.section != NULL)
11310 return 0;
11311 sections->info_or_types.s.section = sectp;
11312 sections->info_or_types.size = bfd_section_size (sectp);
11313 }
11314 else if (names->line_dwo.matches (sectp->name))
11315 {
11316 /* There can be only one. */
11317 if (sections->line.s.section != NULL)
11318 return 0;
11319 sections->line.s.section = sectp;
11320 sections->line.size = bfd_section_size (sectp);
11321 }
11322 else if (names->loc_dwo.matches (sectp->name))
11323 {
11324 /* There can be only one. */
11325 if (sections->loc.s.section != NULL)
11326 return 0;
11327 sections->loc.s.section = sectp;
11328 sections->loc.size = bfd_section_size (sectp);
11329 }
11330 else if (names->macinfo_dwo.matches (sectp->name))
11331 {
11332 /* There can be only one. */
11333 if (sections->macinfo.s.section != NULL)
11334 return 0;
11335 sections->macinfo.s.section = sectp;
11336 sections->macinfo.size = bfd_section_size (sectp);
11337 }
11338 else if (names->macro_dwo.matches (sectp->name))
11339 {
11340 /* There can be only one. */
11341 if (sections->macro.s.section != NULL)
11342 return 0;
11343 sections->macro.s.section = sectp;
11344 sections->macro.size = bfd_section_size (sectp);
11345 }
11346 else if (names->str_offsets_dwo.matches (sectp->name))
11347 {
11348 /* There can be only one. */
11349 if (sections->str_offsets.s.section != NULL)
11350 return 0;
11351 sections->str_offsets.s.section = sectp;
11352 sections->str_offsets.size = bfd_section_size (sectp);
11353 }
11354 else
11355 {
11356 /* No other kind of section is valid. */
11357 return 0;
11358 }
11359
11360 return 1;
11361 }
11362
11363 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11364 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11365 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11366 This is for DWP version 1 files. */
11367
11368 static struct dwo_unit *
11369 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11370 struct dwp_file *dwp_file,
11371 uint32_t unit_index,
11372 const char *comp_dir,
11373 ULONGEST signature, int is_debug_types)
11374 {
11375 const struct dwp_hash_table *dwp_htab =
11376 is_debug_types ? dwp_file->tus : dwp_file->cus;
11377 bfd *dbfd = dwp_file->dbfd.get ();
11378 const char *kind = is_debug_types ? "TU" : "CU";
11379 struct dwo_file *dwo_file;
11380 struct dwo_unit *dwo_unit;
11381 struct virtual_v1_dwo_sections sections;
11382 void **dwo_file_slot;
11383 int i;
11384
11385 gdb_assert (dwp_file->version == 1);
11386
11387 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11388 kind, pulongest (unit_index), hex_string (signature),
11389 dwp_file->name);
11390
11391 /* Fetch the sections of this DWO unit.
11392 Put a limit on the number of sections we look for so that bad data
11393 doesn't cause us to loop forever. */
11394
11395 #define MAX_NR_V1_DWO_SECTIONS \
11396 (1 /* .debug_info or .debug_types */ \
11397 + 1 /* .debug_abbrev */ \
11398 + 1 /* .debug_line */ \
11399 + 1 /* .debug_loc */ \
11400 + 1 /* .debug_str_offsets */ \
11401 + 1 /* .debug_macro or .debug_macinfo */ \
11402 + 1 /* trailing zero */)
11403
11404 memset (&sections, 0, sizeof (sections));
11405
11406 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11407 {
11408 asection *sectp;
11409 uint32_t section_nr =
11410 read_4_bytes (dbfd,
11411 dwp_htab->section_pool.v1.indices
11412 + (unit_index + i) * sizeof (uint32_t));
11413
11414 if (section_nr == 0)
11415 break;
11416 if (section_nr >= dwp_file->num_sections)
11417 {
11418 error (_("Dwarf Error: bad DWP hash table, section number too large"
11419 " [in module %s]"),
11420 dwp_file->name);
11421 }
11422
11423 sectp = dwp_file->elf_sections[section_nr];
11424 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11425 {
11426 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11427 " [in module %s]"),
11428 dwp_file->name);
11429 }
11430 }
11431
11432 if (i < 2
11433 || sections.info_or_types.empty ()
11434 || sections.abbrev.empty ())
11435 {
11436 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11437 " [in module %s]"),
11438 dwp_file->name);
11439 }
11440 if (i == MAX_NR_V1_DWO_SECTIONS)
11441 {
11442 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11443 " [in module %s]"),
11444 dwp_file->name);
11445 }
11446
11447 /* It's easier for the rest of the code if we fake a struct dwo_file and
11448 have dwo_unit "live" in that. At least for now.
11449
11450 The DWP file can be made up of a random collection of CUs and TUs.
11451 However, for each CU + set of TUs that came from the same original DWO
11452 file, we can combine them back into a virtual DWO file to save space
11453 (fewer struct dwo_file objects to allocate). Remember that for really
11454 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11455
11456 std::string virtual_dwo_name =
11457 string_printf ("virtual-dwo/%d-%d-%d-%d",
11458 sections.abbrev.get_id (),
11459 sections.line.get_id (),
11460 sections.loc.get_id (),
11461 sections.str_offsets.get_id ());
11462 /* Can we use an existing virtual DWO file? */
11463 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11464 comp_dir);
11465 /* Create one if necessary. */
11466 if (*dwo_file_slot == NULL)
11467 {
11468 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11469 virtual_dwo_name.c_str ());
11470
11471 dwo_file = new struct dwo_file;
11472 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11473 dwo_file->comp_dir = comp_dir;
11474 dwo_file->sections.abbrev = sections.abbrev;
11475 dwo_file->sections.line = sections.line;
11476 dwo_file->sections.loc = sections.loc;
11477 dwo_file->sections.macinfo = sections.macinfo;
11478 dwo_file->sections.macro = sections.macro;
11479 dwo_file->sections.str_offsets = sections.str_offsets;
11480 /* The "str" section is global to the entire DWP file. */
11481 dwo_file->sections.str = dwp_file->sections.str;
11482 /* The info or types section is assigned below to dwo_unit,
11483 there's no need to record it in dwo_file.
11484 Also, we can't simply record type sections in dwo_file because
11485 we record a pointer into the vector in dwo_unit. As we collect more
11486 types we'll grow the vector and eventually have to reallocate space
11487 for it, invalidating all copies of pointers into the previous
11488 contents. */
11489 *dwo_file_slot = dwo_file;
11490 }
11491 else
11492 {
11493 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11494 virtual_dwo_name.c_str ());
11495
11496 dwo_file = (struct dwo_file *) *dwo_file_slot;
11497 }
11498
11499 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11500 dwo_unit->dwo_file = dwo_file;
11501 dwo_unit->signature = signature;
11502 dwo_unit->section =
11503 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11504 *dwo_unit->section = sections.info_or_types;
11505 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11506
11507 return dwo_unit;
11508 }
11509
11510 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11511 simplify them. Given a pointer to the containing section SECTION, and
11512 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11513 virtual section of just that piece. */
11514
11515 static struct dwarf2_section_info
11516 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11517 struct dwarf2_section_info *section,
11518 bfd_size_type offset, bfd_size_type size)
11519 {
11520 struct dwarf2_section_info result;
11521 asection *sectp;
11522
11523 gdb_assert (section != NULL);
11524 gdb_assert (!section->is_virtual);
11525
11526 memset (&result, 0, sizeof (result));
11527 result.s.containing_section = section;
11528 result.is_virtual = true;
11529
11530 if (size == 0)
11531 return result;
11532
11533 sectp = section->get_bfd_section ();
11534
11535 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11536 bounds of the real section. This is a pretty-rare event, so just
11537 flag an error (easier) instead of a warning and trying to cope. */
11538 if (sectp == NULL
11539 || offset + size > bfd_section_size (sectp))
11540 {
11541 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11542 " in section %s [in module %s]"),
11543 sectp ? bfd_section_name (sectp) : "<unknown>",
11544 objfile_name (per_objfile->objfile));
11545 }
11546
11547 result.virtual_offset = offset;
11548 result.size = size;
11549 return result;
11550 }
11551
11552 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11553 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11554 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11555 This is for DWP version 2 files. */
11556
11557 static struct dwo_unit *
11558 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11559 struct dwp_file *dwp_file,
11560 uint32_t unit_index,
11561 const char *comp_dir,
11562 ULONGEST signature, int is_debug_types)
11563 {
11564 const struct dwp_hash_table *dwp_htab =
11565 is_debug_types ? dwp_file->tus : dwp_file->cus;
11566 bfd *dbfd = dwp_file->dbfd.get ();
11567 const char *kind = is_debug_types ? "TU" : "CU";
11568 struct dwo_file *dwo_file;
11569 struct dwo_unit *dwo_unit;
11570 struct virtual_v2_or_v5_dwo_sections sections;
11571 void **dwo_file_slot;
11572 int i;
11573
11574 gdb_assert (dwp_file->version == 2);
11575
11576 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11577 kind, pulongest (unit_index), hex_string (signature),
11578 dwp_file->name);
11579
11580 /* Fetch the section offsets of this DWO unit. */
11581
11582 memset (&sections, 0, sizeof (sections));
11583
11584 for (i = 0; i < dwp_htab->nr_columns; ++i)
11585 {
11586 uint32_t offset = read_4_bytes (dbfd,
11587 dwp_htab->section_pool.v2.offsets
11588 + (((unit_index - 1) * dwp_htab->nr_columns
11589 + i)
11590 * sizeof (uint32_t)));
11591 uint32_t size = read_4_bytes (dbfd,
11592 dwp_htab->section_pool.v2.sizes
11593 + (((unit_index - 1) * dwp_htab->nr_columns
11594 + i)
11595 * sizeof (uint32_t)));
11596
11597 switch (dwp_htab->section_pool.v2.section_ids[i])
11598 {
11599 case DW_SECT_INFO:
11600 case DW_SECT_TYPES:
11601 sections.info_or_types_offset = offset;
11602 sections.info_or_types_size = size;
11603 break;
11604 case DW_SECT_ABBREV:
11605 sections.abbrev_offset = offset;
11606 sections.abbrev_size = size;
11607 break;
11608 case DW_SECT_LINE:
11609 sections.line_offset = offset;
11610 sections.line_size = size;
11611 break;
11612 case DW_SECT_LOC:
11613 sections.loc_offset = offset;
11614 sections.loc_size = size;
11615 break;
11616 case DW_SECT_STR_OFFSETS:
11617 sections.str_offsets_offset = offset;
11618 sections.str_offsets_size = size;
11619 break;
11620 case DW_SECT_MACINFO:
11621 sections.macinfo_offset = offset;
11622 sections.macinfo_size = size;
11623 break;
11624 case DW_SECT_MACRO:
11625 sections.macro_offset = offset;
11626 sections.macro_size = size;
11627 break;
11628 }
11629 }
11630
11631 /* It's easier for the rest of the code if we fake a struct dwo_file and
11632 have dwo_unit "live" in that. At least for now.
11633
11634 The DWP file can be made up of a random collection of CUs and TUs.
11635 However, for each CU + set of TUs that came from the same original DWO
11636 file, we can combine them back into a virtual DWO file to save space
11637 (fewer struct dwo_file objects to allocate). Remember that for really
11638 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11639
11640 std::string virtual_dwo_name =
11641 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11642 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11643 (long) (sections.line_size ? sections.line_offset : 0),
11644 (long) (sections.loc_size ? sections.loc_offset : 0),
11645 (long) (sections.str_offsets_size
11646 ? sections.str_offsets_offset : 0));
11647 /* Can we use an existing virtual DWO file? */
11648 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11649 comp_dir);
11650 /* Create one if necessary. */
11651 if (*dwo_file_slot == NULL)
11652 {
11653 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11654 virtual_dwo_name.c_str ());
11655
11656 dwo_file = new struct dwo_file;
11657 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11658 dwo_file->comp_dir = comp_dir;
11659 dwo_file->sections.abbrev =
11660 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11661 sections.abbrev_offset,
11662 sections.abbrev_size);
11663 dwo_file->sections.line =
11664 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11665 sections.line_offset,
11666 sections.line_size);
11667 dwo_file->sections.loc =
11668 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11669 sections.loc_offset, sections.loc_size);
11670 dwo_file->sections.macinfo =
11671 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11672 sections.macinfo_offset,
11673 sections.macinfo_size);
11674 dwo_file->sections.macro =
11675 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11676 sections.macro_offset,
11677 sections.macro_size);
11678 dwo_file->sections.str_offsets =
11679 create_dwp_v2_or_v5_section (per_objfile,
11680 &dwp_file->sections.str_offsets,
11681 sections.str_offsets_offset,
11682 sections.str_offsets_size);
11683 /* The "str" section is global to the entire DWP file. */
11684 dwo_file->sections.str = dwp_file->sections.str;
11685 /* The info or types section is assigned below to dwo_unit,
11686 there's no need to record it in dwo_file.
11687 Also, we can't simply record type sections in dwo_file because
11688 we record a pointer into the vector in dwo_unit. As we collect more
11689 types we'll grow the vector and eventually have to reallocate space
11690 for it, invalidating all copies of pointers into the previous
11691 contents. */
11692 *dwo_file_slot = dwo_file;
11693 }
11694 else
11695 {
11696 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11697 virtual_dwo_name.c_str ());
11698
11699 dwo_file = (struct dwo_file *) *dwo_file_slot;
11700 }
11701
11702 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11703 dwo_unit->dwo_file = dwo_file;
11704 dwo_unit->signature = signature;
11705 dwo_unit->section =
11706 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11707 *dwo_unit->section = create_dwp_v2_or_v5_section
11708 (per_objfile,
11709 is_debug_types
11710 ? &dwp_file->sections.types
11711 : &dwp_file->sections.info,
11712 sections.info_or_types_offset,
11713 sections.info_or_types_size);
11714 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11715
11716 return dwo_unit;
11717 }
11718
11719 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11720 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11721 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11722 This is for DWP version 5 files. */
11723
11724 static struct dwo_unit *
11725 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11726 struct dwp_file *dwp_file,
11727 uint32_t unit_index,
11728 const char *comp_dir,
11729 ULONGEST signature, int is_debug_types)
11730 {
11731 const struct dwp_hash_table *dwp_htab
11732 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11733 bfd *dbfd = dwp_file->dbfd.get ();
11734 const char *kind = is_debug_types ? "TU" : "CU";
11735 struct dwo_file *dwo_file;
11736 struct dwo_unit *dwo_unit;
11737 struct virtual_v2_or_v5_dwo_sections sections {};
11738 void **dwo_file_slot;
11739
11740 gdb_assert (dwp_file->version == 5);
11741
11742 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11743 kind, pulongest (unit_index), hex_string (signature),
11744 dwp_file->name);
11745
11746 /* Fetch the section offsets of this DWO unit. */
11747
11748 /* memset (&sections, 0, sizeof (sections)); */
11749
11750 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11751 {
11752 uint32_t offset = read_4_bytes (dbfd,
11753 dwp_htab->section_pool.v5.offsets
11754 + (((unit_index - 1)
11755 * dwp_htab->nr_columns
11756 + i)
11757 * sizeof (uint32_t)));
11758 uint32_t size = read_4_bytes (dbfd,
11759 dwp_htab->section_pool.v5.sizes
11760 + (((unit_index - 1) * dwp_htab->nr_columns
11761 + i)
11762 * sizeof (uint32_t)));
11763
11764 switch (dwp_htab->section_pool.v5.section_ids[i])
11765 {
11766 case DW_SECT_ABBREV_V5:
11767 sections.abbrev_offset = offset;
11768 sections.abbrev_size = size;
11769 break;
11770 case DW_SECT_INFO_V5:
11771 sections.info_or_types_offset = offset;
11772 sections.info_or_types_size = size;
11773 break;
11774 case DW_SECT_LINE_V5:
11775 sections.line_offset = offset;
11776 sections.line_size = size;
11777 break;
11778 case DW_SECT_LOCLISTS_V5:
11779 sections.loclists_offset = offset;
11780 sections.loclists_size = size;
11781 break;
11782 case DW_SECT_MACRO_V5:
11783 sections.macro_offset = offset;
11784 sections.macro_size = size;
11785 break;
11786 case DW_SECT_RNGLISTS_V5:
11787 sections.rnglists_offset = offset;
11788 sections.rnglists_size = size;
11789 break;
11790 case DW_SECT_STR_OFFSETS_V5:
11791 sections.str_offsets_offset = offset;
11792 sections.str_offsets_size = size;
11793 break;
11794 case DW_SECT_RESERVED_V5:
11795 default:
11796 break;
11797 }
11798 }
11799
11800 /* It's easier for the rest of the code if we fake a struct dwo_file and
11801 have dwo_unit "live" in that. At least for now.
11802
11803 The DWP file can be made up of a random collection of CUs and TUs.
11804 However, for each CU + set of TUs that came from the same original DWO
11805 file, we can combine them back into a virtual DWO file to save space
11806 (fewer struct dwo_file objects to allocate). Remember that for really
11807 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11808
11809 std::string virtual_dwo_name =
11810 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11811 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11812 (long) (sections.line_size ? sections.line_offset : 0),
11813 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11814 (long) (sections.str_offsets_size
11815 ? sections.str_offsets_offset : 0),
11816 (long) (sections.macro_size ? sections.macro_offset : 0),
11817 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11818 /* Can we use an existing virtual DWO file? */
11819 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11820 virtual_dwo_name.c_str (),
11821 comp_dir);
11822 /* Create one if necessary. */
11823 if (*dwo_file_slot == NULL)
11824 {
11825 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11826 virtual_dwo_name.c_str ());
11827
11828 dwo_file = new struct dwo_file;
11829 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11830 dwo_file->comp_dir = comp_dir;
11831 dwo_file->sections.abbrev =
11832 create_dwp_v2_or_v5_section (per_objfile,
11833 &dwp_file->sections.abbrev,
11834 sections.abbrev_offset,
11835 sections.abbrev_size);
11836 dwo_file->sections.line =
11837 create_dwp_v2_or_v5_section (per_objfile,
11838 &dwp_file->sections.line,
11839 sections.line_offset, sections.line_size);
11840 dwo_file->sections.macro =
11841 create_dwp_v2_or_v5_section (per_objfile,
11842 &dwp_file->sections.macro,
11843 sections.macro_offset,
11844 sections.macro_size);
11845 dwo_file->sections.loclists =
11846 create_dwp_v2_or_v5_section (per_objfile,
11847 &dwp_file->sections.loclists,
11848 sections.loclists_offset,
11849 sections.loclists_size);
11850 dwo_file->sections.rnglists =
11851 create_dwp_v2_or_v5_section (per_objfile,
11852 &dwp_file->sections.rnglists,
11853 sections.rnglists_offset,
11854 sections.rnglists_size);
11855 dwo_file->sections.str_offsets =
11856 create_dwp_v2_or_v5_section (per_objfile,
11857 &dwp_file->sections.str_offsets,
11858 sections.str_offsets_offset,
11859 sections.str_offsets_size);
11860 /* The "str" section is global to the entire DWP file. */
11861 dwo_file->sections.str = dwp_file->sections.str;
11862 /* The info or types section is assigned below to dwo_unit,
11863 there's no need to record it in dwo_file.
11864 Also, we can't simply record type sections in dwo_file because
11865 we record a pointer into the vector in dwo_unit. As we collect more
11866 types we'll grow the vector and eventually have to reallocate space
11867 for it, invalidating all copies of pointers into the previous
11868 contents. */
11869 *dwo_file_slot = dwo_file;
11870 }
11871 else
11872 {
11873 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11874 virtual_dwo_name.c_str ());
11875
11876 dwo_file = (struct dwo_file *) *dwo_file_slot;
11877 }
11878
11879 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11880 dwo_unit->dwo_file = dwo_file;
11881 dwo_unit->signature = signature;
11882 dwo_unit->section
11883 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11884 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
11885 &dwp_file->sections.info,
11886 sections.info_or_types_offset,
11887 sections.info_or_types_size);
11888 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11889
11890 return dwo_unit;
11891 }
11892
11893 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11894 Returns NULL if the signature isn't found. */
11895
11896 static struct dwo_unit *
11897 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
11898 struct dwp_file *dwp_file, const char *comp_dir,
11899 ULONGEST signature, int is_debug_types)
11900 {
11901 const struct dwp_hash_table *dwp_htab =
11902 is_debug_types ? dwp_file->tus : dwp_file->cus;
11903 bfd *dbfd = dwp_file->dbfd.get ();
11904 uint32_t mask = dwp_htab->nr_slots - 1;
11905 uint32_t hash = signature & mask;
11906 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11907 unsigned int i;
11908 void **slot;
11909 struct dwo_unit find_dwo_cu;
11910
11911 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11912 find_dwo_cu.signature = signature;
11913 slot = htab_find_slot (is_debug_types
11914 ? dwp_file->loaded_tus.get ()
11915 : dwp_file->loaded_cus.get (),
11916 &find_dwo_cu, INSERT);
11917
11918 if (*slot != NULL)
11919 return (struct dwo_unit *) *slot;
11920
11921 /* Use a for loop so that we don't loop forever on bad debug info. */
11922 for (i = 0; i < dwp_htab->nr_slots; ++i)
11923 {
11924 ULONGEST signature_in_table;
11925
11926 signature_in_table =
11927 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11928 if (signature_in_table == signature)
11929 {
11930 uint32_t unit_index =
11931 read_4_bytes (dbfd,
11932 dwp_htab->unit_table + hash * sizeof (uint32_t));
11933
11934 if (dwp_file->version == 1)
11935 {
11936 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11937 unit_index, comp_dir,
11938 signature, is_debug_types);
11939 }
11940 else if (dwp_file->version == 2)
11941 {
11942 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11943 unit_index, comp_dir,
11944 signature, is_debug_types);
11945 }
11946 else /* version == 5 */
11947 {
11948 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11949 unit_index, comp_dir,
11950 signature, is_debug_types);
11951 }
11952 return (struct dwo_unit *) *slot;
11953 }
11954 if (signature_in_table == 0)
11955 return NULL;
11956 hash = (hash + hash2) & mask;
11957 }
11958
11959 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11960 " [in module %s]"),
11961 dwp_file->name);
11962 }
11963
11964 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11965 Open the file specified by FILE_NAME and hand it off to BFD for
11966 preliminary analysis. Return a newly initialized bfd *, which
11967 includes a canonicalized copy of FILE_NAME.
11968 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11969 SEARCH_CWD is true if the current directory is to be searched.
11970 It will be searched before debug-file-directory.
11971 If successful, the file is added to the bfd include table of the
11972 objfile's bfd (see gdb_bfd_record_inclusion).
11973 If unable to find/open the file, return NULL.
11974 NOTE: This function is derived from symfile_bfd_open. */
11975
11976 static gdb_bfd_ref_ptr
11977 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11978 const char *file_name, int is_dwp, int search_cwd)
11979 {
11980 int desc;
11981 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11982 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11983 to debug_file_directory. */
11984 const char *search_path;
11985 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11986
11987 gdb::unique_xmalloc_ptr<char> search_path_holder;
11988 if (search_cwd)
11989 {
11990 if (*debug_file_directory != '\0')
11991 {
11992 search_path_holder.reset (concat (".", dirname_separator_string,
11993 debug_file_directory,
11994 (char *) NULL));
11995 search_path = search_path_holder.get ();
11996 }
11997 else
11998 search_path = ".";
11999 }
12000 else
12001 search_path = debug_file_directory;
12002
12003 /* Add the path for the executable binary to the list of search paths. */
12004 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12005 search_path_holder.reset (concat (objfile_dir.c_str (),
12006 dirname_separator_string,
12007 search_path, nullptr));
12008 search_path = search_path_holder.get ();
12009
12010 openp_flags flags = OPF_RETURN_REALPATH;
12011 if (is_dwp)
12012 flags |= OPF_SEARCH_IN_PATH;
12013
12014 gdb::unique_xmalloc_ptr<char> absolute_name;
12015 desc = openp (search_path, flags, file_name,
12016 O_RDONLY | O_BINARY, &absolute_name);
12017 if (desc < 0)
12018 return NULL;
12019
12020 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12021 gnutarget, desc));
12022 if (sym_bfd == NULL)
12023 return NULL;
12024 bfd_set_cacheable (sym_bfd.get (), 1);
12025
12026 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12027 return NULL;
12028
12029 /* Success. Record the bfd as having been included by the objfile's bfd.
12030 This is important because things like demangled_names_hash lives in the
12031 objfile's per_bfd space and may have references to things like symbol
12032 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12033 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12034
12035 return sym_bfd;
12036 }
12037
12038 /* Try to open DWO file FILE_NAME.
12039 COMP_DIR is the DW_AT_comp_dir attribute.
12040 The result is the bfd handle of the file.
12041 If there is a problem finding or opening the file, return NULL.
12042 Upon success, the canonicalized path of the file is stored in the bfd,
12043 same as symfile_bfd_open. */
12044
12045 static gdb_bfd_ref_ptr
12046 open_dwo_file (dwarf2_per_objfile *per_objfile,
12047 const char *file_name, const char *comp_dir)
12048 {
12049 if (IS_ABSOLUTE_PATH (file_name))
12050 return try_open_dwop_file (per_objfile, file_name,
12051 0 /*is_dwp*/, 0 /*search_cwd*/);
12052
12053 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12054
12055 if (comp_dir != NULL)
12056 {
12057 gdb::unique_xmalloc_ptr<char> path_to_try
12058 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12059
12060 /* NOTE: If comp_dir is a relative path, this will also try the
12061 search path, which seems useful. */
12062 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12063 0 /*is_dwp*/,
12064 1 /*search_cwd*/));
12065 if (abfd != NULL)
12066 return abfd;
12067 }
12068
12069 /* That didn't work, try debug-file-directory, which, despite its name,
12070 is a list of paths. */
12071
12072 if (*debug_file_directory == '\0')
12073 return NULL;
12074
12075 return try_open_dwop_file (per_objfile, file_name,
12076 0 /*is_dwp*/, 1 /*search_cwd*/);
12077 }
12078
12079 /* This function is mapped across the sections and remembers the offset and
12080 size of each of the DWO debugging sections we are interested in. */
12081
12082 static void
12083 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12084 dwo_sections *dwo_sections)
12085 {
12086 const struct dwop_section_names *names = &dwop_section_names;
12087
12088 if (names->abbrev_dwo.matches (sectp->name))
12089 {
12090 dwo_sections->abbrev.s.section = sectp;
12091 dwo_sections->abbrev.size = bfd_section_size (sectp);
12092 }
12093 else if (names->info_dwo.matches (sectp->name))
12094 {
12095 dwo_sections->info.s.section = sectp;
12096 dwo_sections->info.size = bfd_section_size (sectp);
12097 }
12098 else if (names->line_dwo.matches (sectp->name))
12099 {
12100 dwo_sections->line.s.section = sectp;
12101 dwo_sections->line.size = bfd_section_size (sectp);
12102 }
12103 else if (names->loc_dwo.matches (sectp->name))
12104 {
12105 dwo_sections->loc.s.section = sectp;
12106 dwo_sections->loc.size = bfd_section_size (sectp);
12107 }
12108 else if (names->loclists_dwo.matches (sectp->name))
12109 {
12110 dwo_sections->loclists.s.section = sectp;
12111 dwo_sections->loclists.size = bfd_section_size (sectp);
12112 }
12113 else if (names->macinfo_dwo.matches (sectp->name))
12114 {
12115 dwo_sections->macinfo.s.section = sectp;
12116 dwo_sections->macinfo.size = bfd_section_size (sectp);
12117 }
12118 else if (names->macro_dwo.matches (sectp->name))
12119 {
12120 dwo_sections->macro.s.section = sectp;
12121 dwo_sections->macro.size = bfd_section_size (sectp);
12122 }
12123 else if (names->rnglists_dwo.matches (sectp->name))
12124 {
12125 dwo_sections->rnglists.s.section = sectp;
12126 dwo_sections->rnglists.size = bfd_section_size (sectp);
12127 }
12128 else if (names->str_dwo.matches (sectp->name))
12129 {
12130 dwo_sections->str.s.section = sectp;
12131 dwo_sections->str.size = bfd_section_size (sectp);
12132 }
12133 else if (names->str_offsets_dwo.matches (sectp->name))
12134 {
12135 dwo_sections->str_offsets.s.section = sectp;
12136 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12137 }
12138 else if (names->types_dwo.matches (sectp->name))
12139 {
12140 struct dwarf2_section_info type_section;
12141
12142 memset (&type_section, 0, sizeof (type_section));
12143 type_section.s.section = sectp;
12144 type_section.size = bfd_section_size (sectp);
12145 dwo_sections->types.push_back (type_section);
12146 }
12147 }
12148
12149 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12150 by PER_CU. This is for the non-DWP case.
12151 The result is NULL if DWO_NAME can't be found. */
12152
12153 static struct dwo_file *
12154 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12155 const char *comp_dir)
12156 {
12157 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12158
12159 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12160 if (dbfd == NULL)
12161 {
12162 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12163
12164 return NULL;
12165 }
12166
12167 dwo_file_up dwo_file (new struct dwo_file);
12168 dwo_file->dwo_name = dwo_name;
12169 dwo_file->comp_dir = comp_dir;
12170 dwo_file->dbfd = std::move (dbfd);
12171
12172 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12173 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12174 &dwo_file->sections);
12175
12176 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12177 dwo_file->cus);
12178
12179 if (cu->per_cu->dwarf_version < 5)
12180 {
12181 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12182 dwo_file->sections.types, dwo_file->tus);
12183 }
12184 else
12185 {
12186 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12187 &dwo_file->sections.info, dwo_file->tus,
12188 rcuh_kind::COMPILE);
12189 }
12190
12191 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12192
12193 return dwo_file.release ();
12194 }
12195
12196 /* This function is mapped across the sections and remembers the offset and
12197 size of each of the DWP debugging sections common to version 1 and 2 that
12198 we are interested in. */
12199
12200 static void
12201 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12202 dwp_file *dwp_file)
12203 {
12204 const struct dwop_section_names *names = &dwop_section_names;
12205 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12206
12207 /* Record the ELF section number for later lookup: this is what the
12208 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12209 gdb_assert (elf_section_nr < dwp_file->num_sections);
12210 dwp_file->elf_sections[elf_section_nr] = sectp;
12211
12212 /* Look for specific sections that we need. */
12213 if (names->str_dwo.matches (sectp->name))
12214 {
12215 dwp_file->sections.str.s.section = sectp;
12216 dwp_file->sections.str.size = bfd_section_size (sectp);
12217 }
12218 else if (names->cu_index.matches (sectp->name))
12219 {
12220 dwp_file->sections.cu_index.s.section = sectp;
12221 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12222 }
12223 else if (names->tu_index.matches (sectp->name))
12224 {
12225 dwp_file->sections.tu_index.s.section = sectp;
12226 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12227 }
12228 }
12229
12230 /* This function is mapped across the sections and remembers the offset and
12231 size of each of the DWP version 2 debugging sections that we are interested
12232 in. This is split into a separate function because we don't know if we
12233 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12234
12235 static void
12236 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12237 {
12238 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12239 const struct dwop_section_names *names = &dwop_section_names;
12240 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12241
12242 /* Record the ELF section number for later lookup: this is what the
12243 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12244 gdb_assert (elf_section_nr < dwp_file->num_sections);
12245 dwp_file->elf_sections[elf_section_nr] = sectp;
12246
12247 /* Look for specific sections that we need. */
12248 if (names->abbrev_dwo.matches (sectp->name))
12249 {
12250 dwp_file->sections.abbrev.s.section = sectp;
12251 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12252 }
12253 else if (names->info_dwo.matches (sectp->name))
12254 {
12255 dwp_file->sections.info.s.section = sectp;
12256 dwp_file->sections.info.size = bfd_section_size (sectp);
12257 }
12258 else if (names->line_dwo.matches (sectp->name))
12259 {
12260 dwp_file->sections.line.s.section = sectp;
12261 dwp_file->sections.line.size = bfd_section_size (sectp);
12262 }
12263 else if (names->loc_dwo.matches (sectp->name))
12264 {
12265 dwp_file->sections.loc.s.section = sectp;
12266 dwp_file->sections.loc.size = bfd_section_size (sectp);
12267 }
12268 else if (names->macinfo_dwo.matches (sectp->name))
12269 {
12270 dwp_file->sections.macinfo.s.section = sectp;
12271 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12272 }
12273 else if (names->macro_dwo.matches (sectp->name))
12274 {
12275 dwp_file->sections.macro.s.section = sectp;
12276 dwp_file->sections.macro.size = bfd_section_size (sectp);
12277 }
12278 else if (names->str_offsets_dwo.matches (sectp->name))
12279 {
12280 dwp_file->sections.str_offsets.s.section = sectp;
12281 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12282 }
12283 else if (names->types_dwo.matches (sectp->name))
12284 {
12285 dwp_file->sections.types.s.section = sectp;
12286 dwp_file->sections.types.size = bfd_section_size (sectp);
12287 }
12288 }
12289
12290 /* This function is mapped across the sections and remembers the offset and
12291 size of each of the DWP version 5 debugging sections that we are interested
12292 in. This is split into a separate function because we don't know if we
12293 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12294
12295 static void
12296 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12297 {
12298 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12299 const struct dwop_section_names *names = &dwop_section_names;
12300 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12301
12302 /* Record the ELF section number for later lookup: this is what the
12303 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12304 gdb_assert (elf_section_nr < dwp_file->num_sections);
12305 dwp_file->elf_sections[elf_section_nr] = sectp;
12306
12307 /* Look for specific sections that we need. */
12308 if (names->abbrev_dwo.matches (sectp->name))
12309 {
12310 dwp_file->sections.abbrev.s.section = sectp;
12311 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12312 }
12313 else if (names->info_dwo.matches (sectp->name))
12314 {
12315 dwp_file->sections.info.s.section = sectp;
12316 dwp_file->sections.info.size = bfd_section_size (sectp);
12317 }
12318 else if (names->line_dwo.matches (sectp->name))
12319 {
12320 dwp_file->sections.line.s.section = sectp;
12321 dwp_file->sections.line.size = bfd_section_size (sectp);
12322 }
12323 else if (names->loclists_dwo.matches (sectp->name))
12324 {
12325 dwp_file->sections.loclists.s.section = sectp;
12326 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12327 }
12328 else if (names->macro_dwo.matches (sectp->name))
12329 {
12330 dwp_file->sections.macro.s.section = sectp;
12331 dwp_file->sections.macro.size = bfd_section_size (sectp);
12332 }
12333 else if (names->rnglists_dwo.matches (sectp->name))
12334 {
12335 dwp_file->sections.rnglists.s.section = sectp;
12336 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12337 }
12338 else if (names->str_offsets_dwo.matches (sectp->name))
12339 {
12340 dwp_file->sections.str_offsets.s.section = sectp;
12341 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12342 }
12343 }
12344
12345 /* Hash function for dwp_file loaded CUs/TUs. */
12346
12347 static hashval_t
12348 hash_dwp_loaded_cutus (const void *item)
12349 {
12350 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12351
12352 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12353 return dwo_unit->signature;
12354 }
12355
12356 /* Equality function for dwp_file loaded CUs/TUs. */
12357
12358 static int
12359 eq_dwp_loaded_cutus (const void *a, const void *b)
12360 {
12361 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12362 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12363
12364 return dua->signature == dub->signature;
12365 }
12366
12367 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12368
12369 static htab_up
12370 allocate_dwp_loaded_cutus_table ()
12371 {
12372 return htab_up (htab_create_alloc (3,
12373 hash_dwp_loaded_cutus,
12374 eq_dwp_loaded_cutus,
12375 NULL, xcalloc, xfree));
12376 }
12377
12378 /* Try to open DWP file FILE_NAME.
12379 The result is the bfd handle of the file.
12380 If there is a problem finding or opening the file, return NULL.
12381 Upon success, the canonicalized path of the file is stored in the bfd,
12382 same as symfile_bfd_open. */
12383
12384 static gdb_bfd_ref_ptr
12385 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12386 {
12387 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12388 1 /*is_dwp*/,
12389 1 /*search_cwd*/));
12390 if (abfd != NULL)
12391 return abfd;
12392
12393 /* Work around upstream bug 15652.
12394 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12395 [Whether that's a "bug" is debatable, but it is getting in our way.]
12396 We have no real idea where the dwp file is, because gdb's realpath-ing
12397 of the executable's path may have discarded the needed info.
12398 [IWBN if the dwp file name was recorded in the executable, akin to
12399 .gnu_debuglink, but that doesn't exist yet.]
12400 Strip the directory from FILE_NAME and search again. */
12401 if (*debug_file_directory != '\0')
12402 {
12403 /* Don't implicitly search the current directory here.
12404 If the user wants to search "." to handle this case,
12405 it must be added to debug-file-directory. */
12406 return try_open_dwop_file (per_objfile, lbasename (file_name),
12407 1 /*is_dwp*/,
12408 0 /*search_cwd*/);
12409 }
12410
12411 return NULL;
12412 }
12413
12414 /* Initialize the use of the DWP file for the current objfile.
12415 By convention the name of the DWP file is ${objfile}.dwp.
12416 The result is NULL if it can't be found. */
12417
12418 static std::unique_ptr<struct dwp_file>
12419 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12420 {
12421 struct objfile *objfile = per_objfile->objfile;
12422
12423 /* Try to find first .dwp for the binary file before any symbolic links
12424 resolving. */
12425
12426 /* If the objfile is a debug file, find the name of the real binary
12427 file and get the name of dwp file from there. */
12428 std::string dwp_name;
12429 if (objfile->separate_debug_objfile_backlink != NULL)
12430 {
12431 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12432 const char *backlink_basename = lbasename (backlink->original_name);
12433
12434 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12435 }
12436 else
12437 dwp_name = objfile->original_name;
12438
12439 dwp_name += ".dwp";
12440
12441 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12442 if (dbfd == NULL
12443 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12444 {
12445 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12446 dwp_name = objfile_name (objfile);
12447 dwp_name += ".dwp";
12448 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12449 }
12450
12451 if (dbfd == NULL)
12452 {
12453 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12454
12455 return std::unique_ptr<dwp_file> ();
12456 }
12457
12458 const char *name = bfd_get_filename (dbfd.get ());
12459 std::unique_ptr<struct dwp_file> dwp_file
12460 (new struct dwp_file (name, std::move (dbfd)));
12461
12462 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12463 dwp_file->elf_sections =
12464 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12465 dwp_file->num_sections, asection *);
12466
12467 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12468 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12469 dwp_file.get ());
12470
12471 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12472
12473 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12474
12475 /* The DWP file version is stored in the hash table. Oh well. */
12476 if (dwp_file->cus && dwp_file->tus
12477 && dwp_file->cus->version != dwp_file->tus->version)
12478 {
12479 /* Technically speaking, we should try to limp along, but this is
12480 pretty bizarre. We use pulongest here because that's the established
12481 portability solution (e.g, we cannot use %u for uint32_t). */
12482 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12483 " TU version %s [in DWP file %s]"),
12484 pulongest (dwp_file->cus->version),
12485 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12486 }
12487
12488 if (dwp_file->cus)
12489 dwp_file->version = dwp_file->cus->version;
12490 else if (dwp_file->tus)
12491 dwp_file->version = dwp_file->tus->version;
12492 else
12493 dwp_file->version = 2;
12494
12495 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12496 {
12497 if (dwp_file->version == 2)
12498 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12499 dwp_file.get ());
12500 else
12501 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12502 dwp_file.get ());
12503 }
12504
12505 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12506 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12507
12508 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12509 dwarf_read_debug_printf (" %s CUs, %s TUs",
12510 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12511 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12512
12513 return dwp_file;
12514 }
12515
12516 /* Wrapper around open_and_init_dwp_file, only open it once. */
12517
12518 static struct dwp_file *
12519 get_dwp_file (dwarf2_per_objfile *per_objfile)
12520 {
12521 if (!per_objfile->per_bfd->dwp_checked)
12522 {
12523 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12524 per_objfile->per_bfd->dwp_checked = 1;
12525 }
12526 return per_objfile->per_bfd->dwp_file.get ();
12527 }
12528
12529 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12530 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12531 or in the DWP file for the objfile, referenced by THIS_UNIT.
12532 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12533 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12534
12535 This is called, for example, when wanting to read a variable with a
12536 complex location. Therefore we don't want to do file i/o for every call.
12537 Therefore we don't want to look for a DWO file on every call.
12538 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12539 then we check if we've already seen DWO_NAME, and only THEN do we check
12540 for a DWO file.
12541
12542 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12543 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12544
12545 static struct dwo_unit *
12546 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12547 ULONGEST signature, int is_debug_types)
12548 {
12549 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12550 struct objfile *objfile = per_objfile->objfile;
12551 const char *kind = is_debug_types ? "TU" : "CU";
12552 void **dwo_file_slot;
12553 struct dwo_file *dwo_file;
12554 struct dwp_file *dwp_file;
12555
12556 /* First see if there's a DWP file.
12557 If we have a DWP file but didn't find the DWO inside it, don't
12558 look for the original DWO file. It makes gdb behave differently
12559 depending on whether one is debugging in the build tree. */
12560
12561 dwp_file = get_dwp_file (per_objfile);
12562 if (dwp_file != NULL)
12563 {
12564 const struct dwp_hash_table *dwp_htab =
12565 is_debug_types ? dwp_file->tus : dwp_file->cus;
12566
12567 if (dwp_htab != NULL)
12568 {
12569 struct dwo_unit *dwo_cutu =
12570 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12571 is_debug_types);
12572
12573 if (dwo_cutu != NULL)
12574 {
12575 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12576 kind, hex_string (signature),
12577 host_address_to_string (dwo_cutu));
12578
12579 return dwo_cutu;
12580 }
12581 }
12582 }
12583 else
12584 {
12585 /* No DWP file, look for the DWO file. */
12586
12587 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12588 if (*dwo_file_slot == NULL)
12589 {
12590 /* Read in the file and build a table of the CUs/TUs it contains. */
12591 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12592 }
12593 /* NOTE: This will be NULL if unable to open the file. */
12594 dwo_file = (struct dwo_file *) *dwo_file_slot;
12595
12596 if (dwo_file != NULL)
12597 {
12598 struct dwo_unit *dwo_cutu = NULL;
12599
12600 if (is_debug_types && dwo_file->tus)
12601 {
12602 struct dwo_unit find_dwo_cutu;
12603
12604 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12605 find_dwo_cutu.signature = signature;
12606 dwo_cutu
12607 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12608 &find_dwo_cutu);
12609 }
12610 else if (!is_debug_types && dwo_file->cus)
12611 {
12612 struct dwo_unit find_dwo_cutu;
12613
12614 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12615 find_dwo_cutu.signature = signature;
12616 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12617 &find_dwo_cutu);
12618 }
12619
12620 if (dwo_cutu != NULL)
12621 {
12622 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12623 kind, dwo_name, hex_string (signature),
12624 host_address_to_string (dwo_cutu));
12625
12626 return dwo_cutu;
12627 }
12628 }
12629 }
12630
12631 /* We didn't find it. This could mean a dwo_id mismatch, or
12632 someone deleted the DWO/DWP file, or the search path isn't set up
12633 correctly to find the file. */
12634
12635 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12636 kind, dwo_name, hex_string (signature));
12637
12638 /* This is a warning and not a complaint because it can be caused by
12639 pilot error (e.g., user accidentally deleting the DWO). */
12640 {
12641 /* Print the name of the DWP file if we looked there, helps the user
12642 better diagnose the problem. */
12643 std::string dwp_text;
12644
12645 if (dwp_file != NULL)
12646 dwp_text = string_printf (" [in DWP file %s]",
12647 lbasename (dwp_file->name));
12648
12649 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12650 " [in module %s]"),
12651 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12652 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12653 }
12654 return NULL;
12655 }
12656
12657 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12658 See lookup_dwo_cutu_unit for details. */
12659
12660 static struct dwo_unit *
12661 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12662 ULONGEST signature)
12663 {
12664 gdb_assert (!cu->per_cu->is_debug_types);
12665
12666 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12667 }
12668
12669 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12670 See lookup_dwo_cutu_unit for details. */
12671
12672 static struct dwo_unit *
12673 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12674 {
12675 gdb_assert (cu->per_cu->is_debug_types);
12676
12677 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12678
12679 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12680 }
12681
12682 /* Traversal function for queue_and_load_all_dwo_tus. */
12683
12684 static int
12685 queue_and_load_dwo_tu (void **slot, void *info)
12686 {
12687 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12688 dwarf2_cu *cu = (dwarf2_cu *) info;
12689 ULONGEST signature = dwo_unit->signature;
12690 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12691
12692 if (sig_type != NULL)
12693 {
12694 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12695 a real dependency of PER_CU on SIG_TYPE. That is detected later
12696 while processing PER_CU. */
12697 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12698 cu->per_cu->lang))
12699 load_full_type_unit (sig_type, cu->per_objfile);
12700 cu->per_cu->imported_symtabs_push (sig_type);
12701 }
12702
12703 return 1;
12704 }
12705
12706 /* Queue all TUs contained in the DWO of CU to be read in.
12707 The DWO may have the only definition of the type, though it may not be
12708 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12709 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12710
12711 static void
12712 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12713 {
12714 struct dwo_unit *dwo_unit;
12715 struct dwo_file *dwo_file;
12716
12717 gdb_assert (cu != nullptr);
12718 gdb_assert (!cu->per_cu->is_debug_types);
12719 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12720
12721 dwo_unit = cu->dwo_unit;
12722 gdb_assert (dwo_unit != NULL);
12723
12724 dwo_file = dwo_unit->dwo_file;
12725 if (dwo_file->tus != NULL)
12726 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12727 }
12728
12729 /* Read in various DIEs. */
12730
12731 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12732 Inherit only the children of the DW_AT_abstract_origin DIE not being
12733 already referenced by DW_AT_abstract_origin from the children of the
12734 current DIE. */
12735
12736 static void
12737 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12738 {
12739 struct die_info *child_die;
12740 sect_offset *offsetp;
12741 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12742 struct die_info *origin_die;
12743 /* Iterator of the ORIGIN_DIE children. */
12744 struct die_info *origin_child_die;
12745 struct attribute *attr;
12746 struct dwarf2_cu *origin_cu;
12747 struct pending **origin_previous_list_in_scope;
12748
12749 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12750 if (!attr)
12751 return;
12752
12753 /* Note that following die references may follow to a die in a
12754 different cu. */
12755
12756 origin_cu = cu;
12757 origin_die = follow_die_ref (die, attr, &origin_cu);
12758
12759 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12760 symbols in. */
12761 origin_previous_list_in_scope = origin_cu->list_in_scope;
12762 origin_cu->list_in_scope = cu->list_in_scope;
12763
12764 if (die->tag != origin_die->tag
12765 && !(die->tag == DW_TAG_inlined_subroutine
12766 && origin_die->tag == DW_TAG_subprogram))
12767 complaint (_("DIE %s and its abstract origin %s have different tags"),
12768 sect_offset_str (die->sect_off),
12769 sect_offset_str (origin_die->sect_off));
12770
12771 /* Find if the concrete and abstract trees are structurally the
12772 same. This is a shallow traversal and it is not bullet-proof;
12773 the compiler can trick the debugger into believing that the trees
12774 are isomorphic, whereas they actually are not. However, the
12775 likelyhood of this happening is pretty low, and a full-fledged
12776 check would be an overkill. */
12777 bool are_isomorphic = true;
12778 die_info *concrete_child = die->child;
12779 die_info *abstract_child = origin_die->child;
12780 while (concrete_child != nullptr || abstract_child != nullptr)
12781 {
12782 if (concrete_child == nullptr
12783 || abstract_child == nullptr
12784 || concrete_child->tag != abstract_child->tag)
12785 {
12786 are_isomorphic = false;
12787 break;
12788 }
12789
12790 concrete_child = concrete_child->sibling;
12791 abstract_child = abstract_child->sibling;
12792 }
12793
12794 /* Walk the origin's children in parallel to the concrete children.
12795 This helps match an origin child in case the debug info misses
12796 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12797 origin tree may not have the same tree structure as the concrete
12798 DIE, though. */
12799 die_info *corresponding_abstract_child
12800 = are_isomorphic ? origin_die->child : nullptr;
12801
12802 std::vector<sect_offset> offsets;
12803
12804 for (child_die = die->child;
12805 child_die && child_die->tag;
12806 child_die = child_die->sibling)
12807 {
12808 struct die_info *child_origin_die;
12809 struct dwarf2_cu *child_origin_cu;
12810
12811 /* We are trying to process concrete instance entries:
12812 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12813 it's not relevant to our analysis here. i.e. detecting DIEs that are
12814 present in the abstract instance but not referenced in the concrete
12815 one. */
12816 if (child_die->tag == DW_TAG_call_site
12817 || child_die->tag == DW_TAG_GNU_call_site)
12818 {
12819 if (are_isomorphic)
12820 corresponding_abstract_child
12821 = corresponding_abstract_child->sibling;
12822 continue;
12823 }
12824
12825 /* For each CHILD_DIE, find the corresponding child of
12826 ORIGIN_DIE. If there is more than one layer of
12827 DW_AT_abstract_origin, follow them all; there shouldn't be,
12828 but GCC versions at least through 4.4 generate this (GCC PR
12829 40573). */
12830 child_origin_die = child_die;
12831 child_origin_cu = cu;
12832 while (1)
12833 {
12834 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12835 child_origin_cu);
12836 if (attr == NULL)
12837 break;
12838 child_origin_die = follow_die_ref (child_origin_die, attr,
12839 &child_origin_cu);
12840 }
12841
12842 /* If missing DW_AT_abstract_origin, try the corresponding child
12843 of the origin. Clang emits such lexical scopes. */
12844 if (child_origin_die == child_die
12845 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12846 && are_isomorphic
12847 && child_die->tag == DW_TAG_lexical_block)
12848 child_origin_die = corresponding_abstract_child;
12849
12850 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12851 counterpart may exist. */
12852 if (child_origin_die != child_die)
12853 {
12854 if (child_die->tag != child_origin_die->tag
12855 && !(child_die->tag == DW_TAG_inlined_subroutine
12856 && child_origin_die->tag == DW_TAG_subprogram))
12857 complaint (_("Child DIE %s and its abstract origin %s have "
12858 "different tags"),
12859 sect_offset_str (child_die->sect_off),
12860 sect_offset_str (child_origin_die->sect_off));
12861 if (child_origin_die->parent != origin_die)
12862 complaint (_("Child DIE %s and its abstract origin %s have "
12863 "different parents"),
12864 sect_offset_str (child_die->sect_off),
12865 sect_offset_str (child_origin_die->sect_off));
12866 else
12867 offsets.push_back (child_origin_die->sect_off);
12868 }
12869
12870 if (are_isomorphic)
12871 corresponding_abstract_child = corresponding_abstract_child->sibling;
12872 }
12873 std::sort (offsets.begin (), offsets.end ());
12874 sect_offset *offsets_end = offsets.data () + offsets.size ();
12875 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12876 if (offsetp[-1] == *offsetp)
12877 complaint (_("Multiple children of DIE %s refer "
12878 "to DIE %s as their abstract origin"),
12879 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12880
12881 offsetp = offsets.data ();
12882 origin_child_die = origin_die->child;
12883 while (origin_child_die && origin_child_die->tag)
12884 {
12885 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12886 while (offsetp < offsets_end
12887 && *offsetp < origin_child_die->sect_off)
12888 offsetp++;
12889 if (offsetp >= offsets_end
12890 || *offsetp > origin_child_die->sect_off)
12891 {
12892 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12893 Check whether we're already processing ORIGIN_CHILD_DIE.
12894 This can happen with mutually referenced abstract_origins.
12895 PR 16581. */
12896 if (!origin_child_die->in_process)
12897 process_die (origin_child_die, origin_cu);
12898 }
12899 origin_child_die = origin_child_die->sibling;
12900 }
12901 origin_cu->list_in_scope = origin_previous_list_in_scope;
12902
12903 if (cu != origin_cu)
12904 compute_delayed_physnames (origin_cu);
12905 }
12906
12907 static void
12908 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12909 {
12910 struct objfile *objfile = cu->per_objfile->objfile;
12911 struct gdbarch *gdbarch = objfile->arch ();
12912 struct context_stack *newobj;
12913 CORE_ADDR lowpc;
12914 CORE_ADDR highpc;
12915 struct die_info *child_die;
12916 struct attribute *attr, *call_line, *call_file;
12917 const char *name;
12918 CORE_ADDR baseaddr;
12919 struct block *block;
12920 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12921 std::vector<struct symbol *> template_args;
12922 struct template_symbol *templ_func = NULL;
12923
12924 if (inlined_func)
12925 {
12926 /* If we do not have call site information, we can't show the
12927 caller of this inlined function. That's too confusing, so
12928 only use the scope for local variables. */
12929 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12930 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12931 if (call_line == NULL || call_file == NULL)
12932 {
12933 read_lexical_block_scope (die, cu);
12934 return;
12935 }
12936 }
12937
12938 baseaddr = objfile->text_section_offset ();
12939
12940 name = dwarf2_name (die, cu);
12941
12942 /* Ignore functions with missing or empty names. These are actually
12943 illegal according to the DWARF standard. */
12944 if (name == NULL)
12945 {
12946 complaint (_("missing name for subprogram DIE at %s"),
12947 sect_offset_str (die->sect_off));
12948 return;
12949 }
12950
12951 /* Ignore functions with missing or invalid low and high pc attributes. */
12952 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12953 <= PC_BOUNDS_INVALID)
12954 {
12955 attr = dwarf2_attr (die, DW_AT_external, cu);
12956 if (attr == nullptr || !attr->as_boolean ())
12957 complaint (_("cannot get low and high bounds "
12958 "for subprogram DIE at %s"),
12959 sect_offset_str (die->sect_off));
12960 return;
12961 }
12962
12963 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12964 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12965
12966 /* If we have any template arguments, then we must allocate a
12967 different sort of symbol. */
12968 for (child_die = die->child; child_die; child_die = child_die->sibling)
12969 {
12970 if (child_die->tag == DW_TAG_template_type_param
12971 || child_die->tag == DW_TAG_template_value_param)
12972 {
12973 templ_func = new (&objfile->objfile_obstack) template_symbol;
12974 templ_func->subclass = SYMBOL_TEMPLATE;
12975 break;
12976 }
12977 }
12978
12979 gdb_assert (cu->get_builder () != nullptr);
12980 newobj = cu->get_builder ()->push_context (0, lowpc);
12981 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12982 (struct symbol *) templ_func);
12983
12984 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12985 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12986 cu->per_cu->lang);
12987
12988 /* If there is a location expression for DW_AT_frame_base, record
12989 it. */
12990 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12991 if (attr != nullptr)
12992 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12993
12994 /* If there is a location for the static link, record it. */
12995 newobj->static_link = NULL;
12996 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12997 if (attr != nullptr)
12998 {
12999 newobj->static_link
13000 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13001 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13002 cu->addr_type ());
13003 }
13004
13005 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13006
13007 if (die->child != NULL)
13008 {
13009 child_die = die->child;
13010 while (child_die && child_die->tag)
13011 {
13012 if (child_die->tag == DW_TAG_template_type_param
13013 || child_die->tag == DW_TAG_template_value_param)
13014 {
13015 struct symbol *arg = new_symbol (child_die, NULL, cu);
13016
13017 if (arg != NULL)
13018 template_args.push_back (arg);
13019 }
13020 else
13021 process_die (child_die, cu);
13022 child_die = child_die->sibling;
13023 }
13024 }
13025
13026 inherit_abstract_dies (die, cu);
13027
13028 /* If we have a DW_AT_specification, we might need to import using
13029 directives from the context of the specification DIE. See the
13030 comment in determine_prefix. */
13031 if (cu->per_cu->lang == language_cplus
13032 && dwarf2_attr (die, DW_AT_specification, cu))
13033 {
13034 struct dwarf2_cu *spec_cu = cu;
13035 struct die_info *spec_die = die_specification (die, &spec_cu);
13036
13037 while (spec_die)
13038 {
13039 child_die = spec_die->child;
13040 while (child_die && child_die->tag)
13041 {
13042 if (child_die->tag == DW_TAG_imported_module)
13043 process_die (child_die, spec_cu);
13044 child_die = child_die->sibling;
13045 }
13046
13047 /* In some cases, GCC generates specification DIEs that
13048 themselves contain DW_AT_specification attributes. */
13049 spec_die = die_specification (spec_die, &spec_cu);
13050 }
13051 }
13052
13053 struct context_stack cstk = cu->get_builder ()->pop_context ();
13054 /* Make a block for the local symbols within. */
13055 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13056 cstk.static_link, lowpc, highpc);
13057
13058 /* For C++, set the block's scope. */
13059 if ((cu->per_cu->lang == language_cplus
13060 || cu->per_cu->lang == language_fortran
13061 || cu->per_cu->lang == language_d
13062 || cu->per_cu->lang == language_rust)
13063 && cu->processing_has_namespace_info)
13064 block_set_scope (block, determine_prefix (die, cu),
13065 &objfile->objfile_obstack);
13066
13067 /* If we have address ranges, record them. */
13068 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13069
13070 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13071
13072 /* Attach template arguments to function. */
13073 if (!template_args.empty ())
13074 {
13075 gdb_assert (templ_func != NULL);
13076
13077 templ_func->n_template_arguments = template_args.size ();
13078 templ_func->template_arguments
13079 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13080 templ_func->n_template_arguments);
13081 memcpy (templ_func->template_arguments,
13082 template_args.data (),
13083 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13084
13085 /* Make sure that the symtab is set on the new symbols. Even
13086 though they don't appear in this symtab directly, other parts
13087 of gdb assume that symbols do, and this is reasonably
13088 true. */
13089 for (symbol *sym : template_args)
13090 symbol_set_symtab (sym, symbol_symtab (templ_func));
13091 }
13092
13093 /* In C++, we can have functions nested inside functions (e.g., when
13094 a function declares a class that has methods). This means that
13095 when we finish processing a function scope, we may need to go
13096 back to building a containing block's symbol lists. */
13097 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13098 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13099
13100 /* If we've finished processing a top-level function, subsequent
13101 symbols go in the file symbol list. */
13102 if (cu->get_builder ()->outermost_context_p ())
13103 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13104 }
13105
13106 /* Process all the DIES contained within a lexical block scope. Start
13107 a new scope, process the dies, and then close the scope. */
13108
13109 static void
13110 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13111 {
13112 struct objfile *objfile = cu->per_objfile->objfile;
13113 struct gdbarch *gdbarch = objfile->arch ();
13114 CORE_ADDR lowpc, highpc;
13115 struct die_info *child_die;
13116 CORE_ADDR baseaddr;
13117
13118 baseaddr = objfile->text_section_offset ();
13119
13120 /* Ignore blocks with missing or invalid low and high pc attributes. */
13121 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13122 as multiple lexical blocks? Handling children in a sane way would
13123 be nasty. Might be easier to properly extend generic blocks to
13124 describe ranges. */
13125 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13126 {
13127 case PC_BOUNDS_NOT_PRESENT:
13128 /* DW_TAG_lexical_block has no attributes, process its children as if
13129 there was no wrapping by that DW_TAG_lexical_block.
13130 GCC does no longer produces such DWARF since GCC r224161. */
13131 for (child_die = die->child;
13132 child_die != NULL && child_die->tag;
13133 child_die = child_die->sibling)
13134 {
13135 /* We might already be processing this DIE. This can happen
13136 in an unusual circumstance -- where a subroutine A
13137 appears lexically in another subroutine B, but A actually
13138 inlines B. The recursion is broken here, rather than in
13139 inherit_abstract_dies, because it seems better to simply
13140 drop concrete children here. */
13141 if (!child_die->in_process)
13142 process_die (child_die, cu);
13143 }
13144 return;
13145 case PC_BOUNDS_INVALID:
13146 return;
13147 }
13148 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13149 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13150
13151 cu->get_builder ()->push_context (0, lowpc);
13152 if (die->child != NULL)
13153 {
13154 child_die = die->child;
13155 while (child_die && child_die->tag)
13156 {
13157 process_die (child_die, cu);
13158 child_die = child_die->sibling;
13159 }
13160 }
13161 inherit_abstract_dies (die, cu);
13162 struct context_stack cstk = cu->get_builder ()->pop_context ();
13163
13164 if (*cu->get_builder ()->get_local_symbols () != NULL
13165 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13166 {
13167 struct block *block
13168 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13169 cstk.start_addr, highpc);
13170
13171 /* Note that recording ranges after traversing children, as we
13172 do here, means that recording a parent's ranges entails
13173 walking across all its children's ranges as they appear in
13174 the address map, which is quadratic behavior.
13175
13176 It would be nicer to record the parent's ranges before
13177 traversing its children, simply overriding whatever you find
13178 there. But since we don't even decide whether to create a
13179 block until after we've traversed its children, that's hard
13180 to do. */
13181 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13182 }
13183 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13184 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13185 }
13186
13187 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13188
13189 static void
13190 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13191 {
13192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13193 struct objfile *objfile = per_objfile->objfile;
13194 struct gdbarch *gdbarch = objfile->arch ();
13195 CORE_ADDR pc, baseaddr;
13196 struct attribute *attr;
13197 struct call_site *call_site, call_site_local;
13198 void **slot;
13199 int nparams;
13200 struct die_info *child_die;
13201
13202 baseaddr = objfile->text_section_offset ();
13203
13204 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13205 if (attr == NULL)
13206 {
13207 /* This was a pre-DWARF-5 GNU extension alias
13208 for DW_AT_call_return_pc. */
13209 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13210 }
13211 if (!attr)
13212 {
13213 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13214 "DIE %s [in module %s]"),
13215 sect_offset_str (die->sect_off), objfile_name (objfile));
13216 return;
13217 }
13218 pc = attr->as_address () + baseaddr;
13219 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13220
13221 if (cu->call_site_htab == NULL)
13222 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13223 NULL, &objfile->objfile_obstack,
13224 hashtab_obstack_allocate, NULL);
13225 call_site_local.pc = pc;
13226 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13227 if (*slot != NULL)
13228 {
13229 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13230 "DIE %s [in module %s]"),
13231 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13232 objfile_name (objfile));
13233 return;
13234 }
13235
13236 /* Count parameters at the caller. */
13237
13238 nparams = 0;
13239 for (child_die = die->child; child_die && child_die->tag;
13240 child_die = child_die->sibling)
13241 {
13242 if (child_die->tag != DW_TAG_call_site_parameter
13243 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13244 {
13245 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13246 "DW_TAG_call_site child DIE %s [in module %s]"),
13247 child_die->tag, sect_offset_str (child_die->sect_off),
13248 objfile_name (objfile));
13249 continue;
13250 }
13251
13252 nparams++;
13253 }
13254
13255 call_site
13256 = ((struct call_site *)
13257 obstack_alloc (&objfile->objfile_obstack,
13258 sizeof (*call_site)
13259 + (sizeof (*call_site->parameter) * (nparams - 1))));
13260 *slot = call_site;
13261 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13262 call_site->pc = pc;
13263
13264 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13265 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13266 {
13267 struct die_info *func_die;
13268
13269 /* Skip also over DW_TAG_inlined_subroutine. */
13270 for (func_die = die->parent;
13271 func_die && func_die->tag != DW_TAG_subprogram
13272 && func_die->tag != DW_TAG_subroutine_type;
13273 func_die = func_die->parent);
13274
13275 /* DW_AT_call_all_calls is a superset
13276 of DW_AT_call_all_tail_calls. */
13277 if (func_die
13278 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13279 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13280 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13281 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13282 {
13283 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13284 not complete. But keep CALL_SITE for look ups via call_site_htab,
13285 both the initial caller containing the real return address PC and
13286 the final callee containing the current PC of a chain of tail
13287 calls do not need to have the tail call list complete. But any
13288 function candidate for a virtual tail call frame searched via
13289 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13290 determined unambiguously. */
13291 }
13292 else
13293 {
13294 struct type *func_type = NULL;
13295
13296 if (func_die)
13297 func_type = get_die_type (func_die, cu);
13298 if (func_type != NULL)
13299 {
13300 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13301
13302 /* Enlist this call site to the function. */
13303 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13304 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13305 }
13306 else
13307 complaint (_("Cannot find function owning DW_TAG_call_site "
13308 "DIE %s [in module %s]"),
13309 sect_offset_str (die->sect_off), objfile_name (objfile));
13310 }
13311 }
13312
13313 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13314 if (attr == NULL)
13315 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13316 if (attr == NULL)
13317 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13318 if (attr == NULL)
13319 {
13320 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13321 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13322 }
13323 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13324 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13325 /* Keep NULL DWARF_BLOCK. */;
13326 else if (attr->form_is_block ())
13327 {
13328 struct dwarf2_locexpr_baton *dlbaton;
13329 struct dwarf_block *block = attr->as_block ();
13330
13331 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13332 dlbaton->data = block->data;
13333 dlbaton->size = block->size;
13334 dlbaton->per_objfile = per_objfile;
13335 dlbaton->per_cu = cu->per_cu;
13336
13337 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13338 }
13339 else if (attr->form_is_ref ())
13340 {
13341 struct dwarf2_cu *target_cu = cu;
13342 struct die_info *target_die;
13343
13344 target_die = follow_die_ref (die, attr, &target_cu);
13345 gdb_assert (target_cu->per_objfile->objfile == objfile);
13346 if (die_is_declaration (target_die, target_cu))
13347 {
13348 const char *target_physname;
13349
13350 /* Prefer the mangled name; otherwise compute the demangled one. */
13351 target_physname = dw2_linkage_name (target_die, target_cu);
13352 if (target_physname == NULL)
13353 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13354 if (target_physname == NULL)
13355 complaint (_("DW_AT_call_target target DIE has invalid "
13356 "physname, for referencing DIE %s [in module %s]"),
13357 sect_offset_str (die->sect_off), objfile_name (objfile));
13358 else
13359 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13360 }
13361 else
13362 {
13363 CORE_ADDR lowpc;
13364
13365 /* DW_AT_entry_pc should be preferred. */
13366 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13367 <= PC_BOUNDS_INVALID)
13368 complaint (_("DW_AT_call_target target DIE has invalid "
13369 "low pc, for referencing DIE %s [in module %s]"),
13370 sect_offset_str (die->sect_off), objfile_name (objfile));
13371 else
13372 {
13373 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13374 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13375 }
13376 }
13377 }
13378 else
13379 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13380 "block nor reference, for DIE %s [in module %s]"),
13381 sect_offset_str (die->sect_off), objfile_name (objfile));
13382
13383 call_site->per_cu = cu->per_cu;
13384 call_site->per_objfile = per_objfile;
13385
13386 for (child_die = die->child;
13387 child_die && child_die->tag;
13388 child_die = child_die->sibling)
13389 {
13390 struct call_site_parameter *parameter;
13391 struct attribute *loc, *origin;
13392
13393 if (child_die->tag != DW_TAG_call_site_parameter
13394 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13395 {
13396 /* Already printed the complaint above. */
13397 continue;
13398 }
13399
13400 gdb_assert (call_site->parameter_count < nparams);
13401 parameter = &call_site->parameter[call_site->parameter_count];
13402
13403 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13404 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13405 register is contained in DW_AT_call_value. */
13406
13407 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13408 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13409 if (origin == NULL)
13410 {
13411 /* This was a pre-DWARF-5 GNU extension alias
13412 for DW_AT_call_parameter. */
13413 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13414 }
13415 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13416 {
13417 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13418
13419 sect_offset sect_off = origin->get_ref_die_offset ();
13420 if (!cu->header.offset_in_cu_p (sect_off))
13421 {
13422 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13423 binding can be done only inside one CU. Such referenced DIE
13424 therefore cannot be even moved to DW_TAG_partial_unit. */
13425 complaint (_("DW_AT_call_parameter offset is not in CU for "
13426 "DW_TAG_call_site child DIE %s [in module %s]"),
13427 sect_offset_str (child_die->sect_off),
13428 objfile_name (objfile));
13429 continue;
13430 }
13431 parameter->u.param_cu_off
13432 = (cu_offset) (sect_off - cu->header.sect_off);
13433 }
13434 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13435 {
13436 complaint (_("No DW_FORM_block* DW_AT_location for "
13437 "DW_TAG_call_site child DIE %s [in module %s]"),
13438 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13439 continue;
13440 }
13441 else
13442 {
13443 struct dwarf_block *block = loc->as_block ();
13444
13445 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13446 (block->data, &block->data[block->size]);
13447 if (parameter->u.dwarf_reg != -1)
13448 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13449 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13450 &block->data[block->size],
13451 &parameter->u.fb_offset))
13452 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13453 else
13454 {
13455 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13456 "for DW_FORM_block* DW_AT_location is supported for "
13457 "DW_TAG_call_site child DIE %s "
13458 "[in module %s]"),
13459 sect_offset_str (child_die->sect_off),
13460 objfile_name (objfile));
13461 continue;
13462 }
13463 }
13464
13465 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13466 if (attr == NULL)
13467 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13468 if (attr == NULL || !attr->form_is_block ())
13469 {
13470 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13471 "DW_TAG_call_site child DIE %s [in module %s]"),
13472 sect_offset_str (child_die->sect_off),
13473 objfile_name (objfile));
13474 continue;
13475 }
13476
13477 struct dwarf_block *block = attr->as_block ();
13478 parameter->value = block->data;
13479 parameter->value_size = block->size;
13480
13481 /* Parameters are not pre-cleared by memset above. */
13482 parameter->data_value = NULL;
13483 parameter->data_value_size = 0;
13484 call_site->parameter_count++;
13485
13486 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13487 if (attr == NULL)
13488 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13489 if (attr != nullptr)
13490 {
13491 if (!attr->form_is_block ())
13492 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13493 "DW_TAG_call_site child DIE %s [in module %s]"),
13494 sect_offset_str (child_die->sect_off),
13495 objfile_name (objfile));
13496 else
13497 {
13498 block = attr->as_block ();
13499 parameter->data_value = block->data;
13500 parameter->data_value_size = block->size;
13501 }
13502 }
13503 }
13504 }
13505
13506 /* Helper function for read_variable. If DIE represents a virtual
13507 table, then return the type of the concrete object that is
13508 associated with the virtual table. Otherwise, return NULL. */
13509
13510 static struct type *
13511 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13512 {
13513 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13514 if (attr == NULL)
13515 return NULL;
13516
13517 /* Find the type DIE. */
13518 struct die_info *type_die = NULL;
13519 struct dwarf2_cu *type_cu = cu;
13520
13521 if (attr->form_is_ref ())
13522 type_die = follow_die_ref (die, attr, &type_cu);
13523 if (type_die == NULL)
13524 return NULL;
13525
13526 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13527 return NULL;
13528 return die_containing_type (type_die, type_cu);
13529 }
13530
13531 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13532
13533 static void
13534 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13535 {
13536 struct rust_vtable_symbol *storage = NULL;
13537
13538 if (cu->per_cu->lang == language_rust)
13539 {
13540 struct type *containing_type = rust_containing_type (die, cu);
13541
13542 if (containing_type != NULL)
13543 {
13544 struct objfile *objfile = cu->per_objfile->objfile;
13545
13546 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13547 storage->concrete_type = containing_type;
13548 storage->subclass = SYMBOL_RUST_VTABLE;
13549 }
13550 }
13551
13552 struct symbol *res = new_symbol (die, NULL, cu, storage);
13553 struct attribute *abstract_origin
13554 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13555 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13556 if (res == NULL && loc && abstract_origin)
13557 {
13558 /* We have a variable without a name, but with a location and an abstract
13559 origin. This may be a concrete instance of an abstract variable
13560 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13561 later. */
13562 struct dwarf2_cu *origin_cu = cu;
13563 struct die_info *origin_die
13564 = follow_die_ref (die, abstract_origin, &origin_cu);
13565 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13566 per_objfile->per_bfd->abstract_to_concrete
13567 [origin_die->sect_off].push_back (die->sect_off);
13568 }
13569 }
13570
13571 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13572 reading .debug_rnglists.
13573 Callback's type should be:
13574 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13575 Return true if the attributes are present and valid, otherwise,
13576 return false. */
13577
13578 template <typename Callback>
13579 static bool
13580 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13581 dwarf_tag tag, Callback &&callback)
13582 {
13583 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13584 struct objfile *objfile = per_objfile->objfile;
13585 bfd *obfd = objfile->obfd;
13586 /* Base address selection entry. */
13587 gdb::optional<CORE_ADDR> base;
13588 const gdb_byte *buffer;
13589 CORE_ADDR baseaddr;
13590 bool overflow = false;
13591 ULONGEST addr_index;
13592 struct dwarf2_section_info *rnglists_section;
13593
13594 base = cu->base_address;
13595 rnglists_section = cu_debug_rnglists_section (cu, tag);
13596 rnglists_section->read (objfile);
13597
13598 if (offset >= rnglists_section->size)
13599 {
13600 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13601 offset);
13602 return false;
13603 }
13604 buffer = rnglists_section->buffer + offset;
13605
13606 baseaddr = objfile->text_section_offset ();
13607
13608 while (1)
13609 {
13610 /* Initialize it due to a false compiler warning. */
13611 CORE_ADDR range_beginning = 0, range_end = 0;
13612 const gdb_byte *buf_end = (rnglists_section->buffer
13613 + rnglists_section->size);
13614 unsigned int bytes_read;
13615
13616 if (buffer == buf_end)
13617 {
13618 overflow = true;
13619 break;
13620 }
13621 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13622 switch (rlet)
13623 {
13624 case DW_RLE_end_of_list:
13625 break;
13626 case DW_RLE_base_address:
13627 if (buffer + cu->header.addr_size > buf_end)
13628 {
13629 overflow = true;
13630 break;
13631 }
13632 base = cu->header.read_address (obfd, buffer, &bytes_read);
13633 buffer += bytes_read;
13634 break;
13635 case DW_RLE_base_addressx:
13636 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13637 buffer += bytes_read;
13638 base = read_addr_index (cu, addr_index);
13639 break;
13640 case DW_RLE_start_length:
13641 if (buffer + cu->header.addr_size > buf_end)
13642 {
13643 overflow = true;
13644 break;
13645 }
13646 range_beginning = cu->header.read_address (obfd, buffer,
13647 &bytes_read);
13648 buffer += bytes_read;
13649 range_end = (range_beginning
13650 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13651 buffer += bytes_read;
13652 if (buffer > buf_end)
13653 {
13654 overflow = true;
13655 break;
13656 }
13657 break;
13658 case DW_RLE_startx_length:
13659 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13660 buffer += bytes_read;
13661 range_beginning = read_addr_index (cu, addr_index);
13662 if (buffer > buf_end)
13663 {
13664 overflow = true;
13665 break;
13666 }
13667 range_end = (range_beginning
13668 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13669 buffer += bytes_read;
13670 break;
13671 case DW_RLE_offset_pair:
13672 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13673 buffer += bytes_read;
13674 if (buffer > buf_end)
13675 {
13676 overflow = true;
13677 break;
13678 }
13679 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13680 buffer += bytes_read;
13681 if (buffer > buf_end)
13682 {
13683 overflow = true;
13684 break;
13685 }
13686 break;
13687 case DW_RLE_start_end:
13688 if (buffer + 2 * cu->header.addr_size > buf_end)
13689 {
13690 overflow = true;
13691 break;
13692 }
13693 range_beginning = cu->header.read_address (obfd, buffer,
13694 &bytes_read);
13695 buffer += bytes_read;
13696 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13697 buffer += bytes_read;
13698 break;
13699 case DW_RLE_startx_endx:
13700 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13701 buffer += bytes_read;
13702 range_beginning = read_addr_index (cu, addr_index);
13703 if (buffer > buf_end)
13704 {
13705 overflow = true;
13706 break;
13707 }
13708 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13709 buffer += bytes_read;
13710 range_end = read_addr_index (cu, addr_index);
13711 break;
13712 default:
13713 complaint (_("Invalid .debug_rnglists data (no base address)"));
13714 return false;
13715 }
13716 if (rlet == DW_RLE_end_of_list || overflow)
13717 break;
13718 if (rlet == DW_RLE_base_address)
13719 continue;
13720
13721 if (range_beginning > range_end)
13722 {
13723 /* Inverted range entries are invalid. */
13724 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13725 return false;
13726 }
13727
13728 /* Empty range entries have no effect. */
13729 if (range_beginning == range_end)
13730 continue;
13731
13732 /* Only DW_RLE_offset_pair needs the base address added. */
13733 if (rlet == DW_RLE_offset_pair)
13734 {
13735 if (!base.has_value ())
13736 {
13737 /* We have no valid base address for the DW_RLE_offset_pair. */
13738 complaint (_("Invalid .debug_rnglists data (no base address for "
13739 "DW_RLE_offset_pair)"));
13740 return false;
13741 }
13742
13743 range_beginning += *base;
13744 range_end += *base;
13745 }
13746
13747 /* A not-uncommon case of bad debug info.
13748 Don't pollute the addrmap with bad data. */
13749 if (range_beginning + baseaddr == 0
13750 && !per_objfile->per_bfd->has_section_at_zero)
13751 {
13752 complaint (_(".debug_rnglists entry has start address of zero"
13753 " [in module %s]"), objfile_name (objfile));
13754 continue;
13755 }
13756
13757 callback (range_beginning, range_end);
13758 }
13759
13760 if (overflow)
13761 {
13762 complaint (_("Offset %d is not terminated "
13763 "for DW_AT_ranges attribute"),
13764 offset);
13765 return false;
13766 }
13767
13768 return true;
13769 }
13770
13771 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13772 Callback's type should be:
13773 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13774 Return 1 if the attributes are present and valid, otherwise, return 0. */
13775
13776 template <typename Callback>
13777 static int
13778 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13779 Callback &&callback)
13780 {
13781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13782 struct objfile *objfile = per_objfile->objfile;
13783 struct comp_unit_head *cu_header = &cu->header;
13784 bfd *obfd = objfile->obfd;
13785 unsigned int addr_size = cu_header->addr_size;
13786 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13787 /* Base address selection entry. */
13788 gdb::optional<CORE_ADDR> base;
13789 unsigned int dummy;
13790 const gdb_byte *buffer;
13791 CORE_ADDR baseaddr;
13792
13793 if (cu_header->version >= 5)
13794 return dwarf2_rnglists_process (offset, cu, tag, callback);
13795
13796 base = cu->base_address;
13797
13798 per_objfile->per_bfd->ranges.read (objfile);
13799 if (offset >= per_objfile->per_bfd->ranges.size)
13800 {
13801 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13802 offset);
13803 return 0;
13804 }
13805 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13806
13807 baseaddr = objfile->text_section_offset ();
13808
13809 while (1)
13810 {
13811 CORE_ADDR range_beginning, range_end;
13812
13813 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13814 buffer += addr_size;
13815 range_end = cu->header.read_address (obfd, buffer, &dummy);
13816 buffer += addr_size;
13817 offset += 2 * addr_size;
13818
13819 /* An end of list marker is a pair of zero addresses. */
13820 if (range_beginning == 0 && range_end == 0)
13821 /* Found the end of list entry. */
13822 break;
13823
13824 /* Each base address selection entry is a pair of 2 values.
13825 The first is the largest possible address, the second is
13826 the base address. Check for a base address here. */
13827 if ((range_beginning & mask) == mask)
13828 {
13829 /* If we found the largest possible address, then we already
13830 have the base address in range_end. */
13831 base = range_end;
13832 continue;
13833 }
13834
13835 if (!base.has_value ())
13836 {
13837 /* We have no valid base address for the ranges
13838 data. */
13839 complaint (_("Invalid .debug_ranges data (no base address)"));
13840 return 0;
13841 }
13842
13843 if (range_beginning > range_end)
13844 {
13845 /* Inverted range entries are invalid. */
13846 complaint (_("Invalid .debug_ranges data (inverted range)"));
13847 return 0;
13848 }
13849
13850 /* Empty range entries have no effect. */
13851 if (range_beginning == range_end)
13852 continue;
13853
13854 range_beginning += *base;
13855 range_end += *base;
13856
13857 /* A not-uncommon case of bad debug info.
13858 Don't pollute the addrmap with bad data. */
13859 if (range_beginning + baseaddr == 0
13860 && !per_objfile->per_bfd->has_section_at_zero)
13861 {
13862 complaint (_(".debug_ranges entry has start address of zero"
13863 " [in module %s]"), objfile_name (objfile));
13864 continue;
13865 }
13866
13867 callback (range_beginning, range_end);
13868 }
13869
13870 return 1;
13871 }
13872
13873 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13874 Return 1 if the attributes are present and valid, otherwise, return 0.
13875 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
13876
13877 static int
13878 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13879 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13880 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
13881 {
13882 struct objfile *objfile = cu->per_objfile->objfile;
13883 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
13884 struct gdbarch *gdbarch = objfile->arch ();
13885 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13886 int low_set = 0;
13887 CORE_ADDR low = 0;
13888 CORE_ADDR high = 0;
13889 int retval;
13890
13891 retval = dwarf2_ranges_process (offset, cu, tag,
13892 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13893 {
13894 if (ranges_pst != NULL)
13895 {
13896 CORE_ADDR lowpc;
13897 CORE_ADDR highpc;
13898
13899 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13900 range_beginning + baseaddr)
13901 - baseaddr);
13902 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13903 range_end + baseaddr)
13904 - baseaddr);
13905 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
13906 lowpc, highpc - 1, ranges_pst);
13907 }
13908
13909 /* FIXME: This is recording everything as a low-high
13910 segment of consecutive addresses. We should have a
13911 data structure for discontiguous block ranges
13912 instead. */
13913 if (! low_set)
13914 {
13915 low = range_beginning;
13916 high = range_end;
13917 low_set = 1;
13918 }
13919 else
13920 {
13921 if (range_beginning < low)
13922 low = range_beginning;
13923 if (range_end > high)
13924 high = range_end;
13925 }
13926 });
13927 if (!retval)
13928 return 0;
13929
13930 if (! low_set)
13931 /* If the first entry is an end-of-list marker, the range
13932 describes an empty scope, i.e. no instructions. */
13933 return 0;
13934
13935 if (low_return)
13936 *low_return = low;
13937 if (high_return)
13938 *high_return = high;
13939 return 1;
13940 }
13941
13942 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13943 definition for the return value. *LOWPC and *HIGHPC are set iff
13944 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13945
13946 static enum pc_bounds_kind
13947 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13948 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13949 dwarf2_psymtab *pst)
13950 {
13951 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13952 struct attribute *attr;
13953 struct attribute *attr_high;
13954 CORE_ADDR low = 0;
13955 CORE_ADDR high = 0;
13956 enum pc_bounds_kind ret;
13957
13958 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13959 if (attr_high)
13960 {
13961 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13962 if (attr != nullptr)
13963 {
13964 low = attr->as_address ();
13965 high = attr_high->as_address ();
13966 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13967 high += low;
13968 }
13969 else
13970 /* Found high w/o low attribute. */
13971 return PC_BOUNDS_INVALID;
13972
13973 /* Found consecutive range of addresses. */
13974 ret = PC_BOUNDS_HIGH_LOW;
13975 }
13976 else
13977 {
13978 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13979 if (attr != nullptr && attr->form_is_unsigned ())
13980 {
13981 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13982 on DWARF version). */
13983 ULONGEST ranges_offset = attr->as_unsigned ();
13984
13985 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13986 this value. */
13987 if (die->tag != DW_TAG_compile_unit)
13988 ranges_offset += cu->gnu_ranges_base;
13989
13990 /* Value of the DW_AT_ranges attribute is the offset in the
13991 .debug_ranges section. */
13992 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
13993 die->tag))
13994 return PC_BOUNDS_INVALID;
13995 /* Found discontinuous range of addresses. */
13996 ret = PC_BOUNDS_RANGES;
13997 }
13998 else
13999 return PC_BOUNDS_NOT_PRESENT;
14000 }
14001
14002 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14003 if (high <= low)
14004 return PC_BOUNDS_INVALID;
14005
14006 /* When using the GNU linker, .gnu.linkonce. sections are used to
14007 eliminate duplicate copies of functions and vtables and such.
14008 The linker will arbitrarily choose one and discard the others.
14009 The AT_*_pc values for such functions refer to local labels in
14010 these sections. If the section from that file was discarded, the
14011 labels are not in the output, so the relocs get a value of 0.
14012 If this is a discarded function, mark the pc bounds as invalid,
14013 so that GDB will ignore it. */
14014 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14015 return PC_BOUNDS_INVALID;
14016
14017 *lowpc = low;
14018 if (highpc)
14019 *highpc = high;
14020 return ret;
14021 }
14022
14023 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14024 its low and high PC addresses. Do nothing if these addresses could not
14025 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14026 and HIGHPC to the high address if greater than HIGHPC. */
14027
14028 static void
14029 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14030 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14031 struct dwarf2_cu *cu)
14032 {
14033 CORE_ADDR low, high;
14034 struct die_info *child = die->child;
14035
14036 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14037 {
14038 *lowpc = std::min (*lowpc, low);
14039 *highpc = std::max (*highpc, high);
14040 }
14041
14042 /* If the language does not allow nested subprograms (either inside
14043 subprograms or lexical blocks), we're done. */
14044 if (cu->per_cu->lang != language_ada)
14045 return;
14046
14047 /* Check all the children of the given DIE. If it contains nested
14048 subprograms, then check their pc bounds. Likewise, we need to
14049 check lexical blocks as well, as they may also contain subprogram
14050 definitions. */
14051 while (child && child->tag)
14052 {
14053 if (child->tag == DW_TAG_subprogram
14054 || child->tag == DW_TAG_lexical_block)
14055 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14056 child = child->sibling;
14057 }
14058 }
14059
14060 /* Get the low and high pc's represented by the scope DIE, and store
14061 them in *LOWPC and *HIGHPC. If the correct values can't be
14062 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14063
14064 static void
14065 get_scope_pc_bounds (struct die_info *die,
14066 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14067 struct dwarf2_cu *cu)
14068 {
14069 CORE_ADDR best_low = (CORE_ADDR) -1;
14070 CORE_ADDR best_high = (CORE_ADDR) 0;
14071 CORE_ADDR current_low, current_high;
14072
14073 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14074 >= PC_BOUNDS_RANGES)
14075 {
14076 best_low = current_low;
14077 best_high = current_high;
14078 }
14079 else
14080 {
14081 struct die_info *child = die->child;
14082
14083 while (child && child->tag)
14084 {
14085 switch (child->tag) {
14086 case DW_TAG_subprogram:
14087 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14088 break;
14089 case DW_TAG_namespace:
14090 case DW_TAG_module:
14091 /* FIXME: carlton/2004-01-16: Should we do this for
14092 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14093 that current GCC's always emit the DIEs corresponding
14094 to definitions of methods of classes as children of a
14095 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14096 the DIEs giving the declarations, which could be
14097 anywhere). But I don't see any reason why the
14098 standards says that they have to be there. */
14099 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14100
14101 if (current_low != ((CORE_ADDR) -1))
14102 {
14103 best_low = std::min (best_low, current_low);
14104 best_high = std::max (best_high, current_high);
14105 }
14106 break;
14107 default:
14108 /* Ignore. */
14109 break;
14110 }
14111
14112 child = child->sibling;
14113 }
14114 }
14115
14116 *lowpc = best_low;
14117 *highpc = best_high;
14118 }
14119
14120 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14121 in DIE. */
14122
14123 static void
14124 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14125 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14126 {
14127 struct objfile *objfile = cu->per_objfile->objfile;
14128 struct gdbarch *gdbarch = objfile->arch ();
14129 struct attribute *attr;
14130 struct attribute *attr_high;
14131
14132 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14133 if (attr_high)
14134 {
14135 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14136 if (attr != nullptr)
14137 {
14138 CORE_ADDR low = attr->as_address ();
14139 CORE_ADDR high = attr_high->as_address ();
14140
14141 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14142 high += low;
14143
14144 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14145 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14146 cu->get_builder ()->record_block_range (block, low, high - 1);
14147 }
14148 }
14149
14150 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14151 if (attr != nullptr && attr->form_is_unsigned ())
14152 {
14153 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14154 on DWARF version). */
14155 ULONGEST ranges_offset = attr->as_unsigned ();
14156
14157 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14158 this value. */
14159 if (die->tag != DW_TAG_compile_unit)
14160 ranges_offset += cu->gnu_ranges_base;
14161
14162 std::vector<blockrange> blockvec;
14163 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14164 [&] (CORE_ADDR start, CORE_ADDR end)
14165 {
14166 start += baseaddr;
14167 end += baseaddr;
14168 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14169 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14170 cu->get_builder ()->record_block_range (block, start, end - 1);
14171 blockvec.emplace_back (start, end);
14172 });
14173
14174 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14175 }
14176 }
14177
14178 /* Check whether the producer field indicates either of GCC < 4.6, or the
14179 Intel C/C++ compiler, and cache the result in CU. */
14180
14181 static void
14182 check_producer (struct dwarf2_cu *cu)
14183 {
14184 int major, minor;
14185
14186 if (cu->producer == NULL)
14187 {
14188 /* For unknown compilers expect their behavior is DWARF version
14189 compliant.
14190
14191 GCC started to support .debug_types sections by -gdwarf-4 since
14192 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14193 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14194 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14195 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14196 }
14197 else if (producer_is_gcc (cu->producer, &major, &minor))
14198 {
14199 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14200 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14201 }
14202 else if (producer_is_icc (cu->producer, &major, &minor))
14203 {
14204 cu->producer_is_icc = true;
14205 cu->producer_is_icc_lt_14 = major < 14;
14206 }
14207 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14208 cu->producer_is_codewarrior = true;
14209 else
14210 {
14211 /* For other non-GCC compilers, expect their behavior is DWARF version
14212 compliant. */
14213 }
14214
14215 cu->checked_producer = true;
14216 }
14217
14218 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14219 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14220 during 4.6.0 experimental. */
14221
14222 static bool
14223 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14224 {
14225 if (!cu->checked_producer)
14226 check_producer (cu);
14227
14228 return cu->producer_is_gxx_lt_4_6;
14229 }
14230
14231
14232 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14233 with incorrect is_stmt attributes. */
14234
14235 static bool
14236 producer_is_codewarrior (struct dwarf2_cu *cu)
14237 {
14238 if (!cu->checked_producer)
14239 check_producer (cu);
14240
14241 return cu->producer_is_codewarrior;
14242 }
14243
14244 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14245 If that attribute is not available, return the appropriate
14246 default. */
14247
14248 static enum dwarf_access_attribute
14249 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14250 {
14251 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14252 if (attr != nullptr)
14253 {
14254 LONGEST value = attr->constant_value (-1);
14255 if (value == DW_ACCESS_public
14256 || value == DW_ACCESS_protected
14257 || value == DW_ACCESS_private)
14258 return (dwarf_access_attribute) value;
14259 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14260 plongest (value));
14261 }
14262
14263 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14264 {
14265 /* The default DWARF 2 accessibility for members is public, the default
14266 accessibility for inheritance is private. */
14267
14268 if (die->tag != DW_TAG_inheritance)
14269 return DW_ACCESS_public;
14270 else
14271 return DW_ACCESS_private;
14272 }
14273 else
14274 {
14275 /* DWARF 3+ defines the default accessibility a different way. The same
14276 rules apply now for DW_TAG_inheritance as for the members and it only
14277 depends on the container kind. */
14278
14279 if (die->parent->tag == DW_TAG_class_type)
14280 return DW_ACCESS_private;
14281 else
14282 return DW_ACCESS_public;
14283 }
14284 }
14285
14286 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14287 offset. If the attribute was not found return 0, otherwise return
14288 1. If it was found but could not properly be handled, set *OFFSET
14289 to 0. */
14290
14291 static int
14292 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14293 LONGEST *offset)
14294 {
14295 struct attribute *attr;
14296
14297 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14298 if (attr != NULL)
14299 {
14300 *offset = 0;
14301
14302 /* Note that we do not check for a section offset first here.
14303 This is because DW_AT_data_member_location is new in DWARF 4,
14304 so if we see it, we can assume that a constant form is really
14305 a constant and not a section offset. */
14306 if (attr->form_is_constant ())
14307 *offset = attr->constant_value (0);
14308 else if (attr->form_is_section_offset ())
14309 dwarf2_complex_location_expr_complaint ();
14310 else if (attr->form_is_block ())
14311 *offset = decode_locdesc (attr->as_block (), cu);
14312 else
14313 dwarf2_complex_location_expr_complaint ();
14314
14315 return 1;
14316 }
14317
14318 return 0;
14319 }
14320
14321 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14322
14323 static void
14324 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14325 struct field *field)
14326 {
14327 struct attribute *attr;
14328
14329 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14330 if (attr != NULL)
14331 {
14332 if (attr->form_is_constant ())
14333 {
14334 LONGEST offset = attr->constant_value (0);
14335 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14336 }
14337 else if (attr->form_is_section_offset ())
14338 dwarf2_complex_location_expr_complaint ();
14339 else if (attr->form_is_block ())
14340 {
14341 bool handled;
14342 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14343 if (handled)
14344 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14345 else
14346 {
14347 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14348 struct objfile *objfile = per_objfile->objfile;
14349 struct dwarf2_locexpr_baton *dlbaton
14350 = XOBNEW (&objfile->objfile_obstack,
14351 struct dwarf2_locexpr_baton);
14352 dlbaton->data = attr->as_block ()->data;
14353 dlbaton->size = attr->as_block ()->size;
14354 /* When using this baton, we want to compute the address
14355 of the field, not the value. This is why
14356 is_reference is set to false here. */
14357 dlbaton->is_reference = false;
14358 dlbaton->per_objfile = per_objfile;
14359 dlbaton->per_cu = cu->per_cu;
14360
14361 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14362 }
14363 }
14364 else
14365 dwarf2_complex_location_expr_complaint ();
14366 }
14367 }
14368
14369 /* Add an aggregate field to the field list. */
14370
14371 static void
14372 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14373 struct dwarf2_cu *cu)
14374 {
14375 struct objfile *objfile = cu->per_objfile->objfile;
14376 struct gdbarch *gdbarch = objfile->arch ();
14377 struct nextfield *new_field;
14378 struct attribute *attr;
14379 struct field *fp;
14380 const char *fieldname = "";
14381
14382 if (die->tag == DW_TAG_inheritance)
14383 {
14384 fip->baseclasses.emplace_back ();
14385 new_field = &fip->baseclasses.back ();
14386 }
14387 else
14388 {
14389 fip->fields.emplace_back ();
14390 new_field = &fip->fields.back ();
14391 }
14392
14393 new_field->offset = die->sect_off;
14394
14395 new_field->accessibility = dwarf2_access_attribute (die, cu);
14396 if (new_field->accessibility != DW_ACCESS_public)
14397 fip->non_public_fields = true;
14398
14399 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14400 if (attr != nullptr)
14401 new_field->virtuality = attr->as_virtuality ();
14402 else
14403 new_field->virtuality = DW_VIRTUALITY_none;
14404
14405 fp = &new_field->field;
14406
14407 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14408 {
14409 /* Data member other than a C++ static data member. */
14410
14411 /* Get type of field. */
14412 fp->set_type (die_type (die, cu));
14413
14414 SET_FIELD_BITPOS (*fp, 0);
14415
14416 /* Get bit size of field (zero if none). */
14417 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14418 if (attr != nullptr)
14419 {
14420 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14421 }
14422 else
14423 {
14424 FIELD_BITSIZE (*fp) = 0;
14425 }
14426
14427 /* Get bit offset of field. */
14428 handle_data_member_location (die, cu, fp);
14429 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14430 if (attr != nullptr && attr->form_is_constant ())
14431 {
14432 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14433 {
14434 /* For big endian bits, the DW_AT_bit_offset gives the
14435 additional bit offset from the MSB of the containing
14436 anonymous object to the MSB of the field. We don't
14437 have to do anything special since we don't need to
14438 know the size of the anonymous object. */
14439 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14440 + attr->constant_value (0)));
14441 }
14442 else
14443 {
14444 /* For little endian bits, compute the bit offset to the
14445 MSB of the anonymous object, subtract off the number of
14446 bits from the MSB of the field to the MSB of the
14447 object, and then subtract off the number of bits of
14448 the field itself. The result is the bit offset of
14449 the LSB of the field. */
14450 int anonymous_size;
14451 int bit_offset = attr->constant_value (0);
14452
14453 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14454 if (attr != nullptr && attr->form_is_constant ())
14455 {
14456 /* The size of the anonymous object containing
14457 the bit field is explicit, so use the
14458 indicated size (in bytes). */
14459 anonymous_size = attr->constant_value (0);
14460 }
14461 else
14462 {
14463 /* The size of the anonymous object containing
14464 the bit field must be inferred from the type
14465 attribute of the data member containing the
14466 bit field. */
14467 anonymous_size = TYPE_LENGTH (fp->type ());
14468 }
14469 SET_FIELD_BITPOS (*fp,
14470 (FIELD_BITPOS (*fp)
14471 + anonymous_size * bits_per_byte
14472 - bit_offset - FIELD_BITSIZE (*fp)));
14473 }
14474 }
14475 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14476 if (attr != NULL)
14477 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14478 + attr->constant_value (0)));
14479
14480 /* Get name of field. */
14481 fieldname = dwarf2_name (die, cu);
14482 if (fieldname == NULL)
14483 fieldname = "";
14484
14485 /* The name is already allocated along with this objfile, so we don't
14486 need to duplicate it for the type. */
14487 fp->name = fieldname;
14488
14489 /* Change accessibility for artificial fields (e.g. virtual table
14490 pointer or virtual base class pointer) to private. */
14491 if (dwarf2_attr (die, DW_AT_artificial, cu))
14492 {
14493 FIELD_ARTIFICIAL (*fp) = 1;
14494 new_field->accessibility = DW_ACCESS_private;
14495 fip->non_public_fields = true;
14496 }
14497 }
14498 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14499 {
14500 /* C++ static member. */
14501
14502 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14503 is a declaration, but all versions of G++ as of this writing
14504 (so through at least 3.2.1) incorrectly generate
14505 DW_TAG_variable tags. */
14506
14507 const char *physname;
14508
14509 /* Get name of field. */
14510 fieldname = dwarf2_name (die, cu);
14511 if (fieldname == NULL)
14512 return;
14513
14514 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14515 if (attr
14516 /* Only create a symbol if this is an external value.
14517 new_symbol checks this and puts the value in the global symbol
14518 table, which we want. If it is not external, new_symbol
14519 will try to put the value in cu->list_in_scope which is wrong. */
14520 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14521 {
14522 /* A static const member, not much different than an enum as far as
14523 we're concerned, except that we can support more types. */
14524 new_symbol (die, NULL, cu);
14525 }
14526
14527 /* Get physical name. */
14528 physname = dwarf2_physname (fieldname, die, cu);
14529
14530 /* The name is already allocated along with this objfile, so we don't
14531 need to duplicate it for the type. */
14532 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14533 fp->set_type (die_type (die, cu));
14534 FIELD_NAME (*fp) = fieldname;
14535 }
14536 else if (die->tag == DW_TAG_inheritance)
14537 {
14538 /* C++ base class field. */
14539 handle_data_member_location (die, cu, fp);
14540 FIELD_BITSIZE (*fp) = 0;
14541 fp->set_type (die_type (die, cu));
14542 FIELD_NAME (*fp) = fp->type ()->name ();
14543 }
14544 else
14545 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14546 }
14547
14548 /* Can the type given by DIE define another type? */
14549
14550 static bool
14551 type_can_define_types (const struct die_info *die)
14552 {
14553 switch (die->tag)
14554 {
14555 case DW_TAG_typedef:
14556 case DW_TAG_class_type:
14557 case DW_TAG_structure_type:
14558 case DW_TAG_union_type:
14559 case DW_TAG_enumeration_type:
14560 return true;
14561
14562 default:
14563 return false;
14564 }
14565 }
14566
14567 /* Add a type definition defined in the scope of the FIP's class. */
14568
14569 static void
14570 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14571 struct dwarf2_cu *cu)
14572 {
14573 struct decl_field fp;
14574 memset (&fp, 0, sizeof (fp));
14575
14576 gdb_assert (type_can_define_types (die));
14577
14578 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14579 fp.name = dwarf2_name (die, cu);
14580 fp.type = read_type_die (die, cu);
14581
14582 /* Save accessibility. */
14583 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14584 switch (accessibility)
14585 {
14586 case DW_ACCESS_public:
14587 /* The assumed value if neither private nor protected. */
14588 break;
14589 case DW_ACCESS_private:
14590 fp.is_private = 1;
14591 break;
14592 case DW_ACCESS_protected:
14593 fp.is_protected = 1;
14594 break;
14595 }
14596
14597 if (die->tag == DW_TAG_typedef)
14598 fip->typedef_field_list.push_back (fp);
14599 else
14600 fip->nested_types_list.push_back (fp);
14601 }
14602
14603 /* A convenience typedef that's used when finding the discriminant
14604 field for a variant part. */
14605 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14606 offset_map_type;
14607
14608 /* Compute the discriminant range for a given variant. OBSTACK is
14609 where the results will be stored. VARIANT is the variant to
14610 process. IS_UNSIGNED indicates whether the discriminant is signed
14611 or unsigned. */
14612
14613 static const gdb::array_view<discriminant_range>
14614 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14615 bool is_unsigned)
14616 {
14617 std::vector<discriminant_range> ranges;
14618
14619 if (variant.default_branch)
14620 return {};
14621
14622 if (variant.discr_list_data == nullptr)
14623 {
14624 discriminant_range r
14625 = {variant.discriminant_value, variant.discriminant_value};
14626 ranges.push_back (r);
14627 }
14628 else
14629 {
14630 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14631 variant.discr_list_data->size);
14632 while (!data.empty ())
14633 {
14634 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14635 {
14636 complaint (_("invalid discriminant marker: %d"), data[0]);
14637 break;
14638 }
14639 bool is_range = data[0] == DW_DSC_range;
14640 data = data.slice (1);
14641
14642 ULONGEST low, high;
14643 unsigned int bytes_read;
14644
14645 if (data.empty ())
14646 {
14647 complaint (_("DW_AT_discr_list missing low value"));
14648 break;
14649 }
14650 if (is_unsigned)
14651 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14652 else
14653 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14654 &bytes_read);
14655 data = data.slice (bytes_read);
14656
14657 if (is_range)
14658 {
14659 if (data.empty ())
14660 {
14661 complaint (_("DW_AT_discr_list missing high value"));
14662 break;
14663 }
14664 if (is_unsigned)
14665 high = read_unsigned_leb128 (nullptr, data.data (),
14666 &bytes_read);
14667 else
14668 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14669 &bytes_read);
14670 data = data.slice (bytes_read);
14671 }
14672 else
14673 high = low;
14674
14675 ranges.push_back ({ low, high });
14676 }
14677 }
14678
14679 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14680 ranges.size ());
14681 std::copy (ranges.begin (), ranges.end (), result);
14682 return gdb::array_view<discriminant_range> (result, ranges.size ());
14683 }
14684
14685 static const gdb::array_view<variant_part> create_variant_parts
14686 (struct obstack *obstack,
14687 const offset_map_type &offset_map,
14688 struct field_info *fi,
14689 const std::vector<variant_part_builder> &variant_parts);
14690
14691 /* Fill in a "struct variant" for a given variant field. RESULT is
14692 the variant to fill in. OBSTACK is where any needed allocations
14693 will be done. OFFSET_MAP holds the mapping from section offsets to
14694 fields for the type. FI describes the fields of the type we're
14695 processing. FIELD is the variant field we're converting. */
14696
14697 static void
14698 create_one_variant (variant &result, struct obstack *obstack,
14699 const offset_map_type &offset_map,
14700 struct field_info *fi, const variant_field &field)
14701 {
14702 result.discriminants = convert_variant_range (obstack, field, false);
14703 result.first_field = field.first_field + fi->baseclasses.size ();
14704 result.last_field = field.last_field + fi->baseclasses.size ();
14705 result.parts = create_variant_parts (obstack, offset_map, fi,
14706 field.variant_parts);
14707 }
14708
14709 /* Fill in a "struct variant_part" for a given variant part. RESULT
14710 is the variant part to fill in. OBSTACK is where any needed
14711 allocations will be done. OFFSET_MAP holds the mapping from
14712 section offsets to fields for the type. FI describes the fields of
14713 the type we're processing. BUILDER is the variant part to be
14714 converted. */
14715
14716 static void
14717 create_one_variant_part (variant_part &result,
14718 struct obstack *obstack,
14719 const offset_map_type &offset_map,
14720 struct field_info *fi,
14721 const variant_part_builder &builder)
14722 {
14723 auto iter = offset_map.find (builder.discriminant_offset);
14724 if (iter == offset_map.end ())
14725 {
14726 result.discriminant_index = -1;
14727 /* Doesn't matter. */
14728 result.is_unsigned = false;
14729 }
14730 else
14731 {
14732 result.discriminant_index = iter->second;
14733 result.is_unsigned
14734 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14735 }
14736
14737 size_t n = builder.variants.size ();
14738 variant *output = new (obstack) variant[n];
14739 for (size_t i = 0; i < n; ++i)
14740 create_one_variant (output[i], obstack, offset_map, fi,
14741 builder.variants[i]);
14742
14743 result.variants = gdb::array_view<variant> (output, n);
14744 }
14745
14746 /* Create a vector of variant parts that can be attached to a type.
14747 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14748 holds the mapping from section offsets to fields for the type. FI
14749 describes the fields of the type we're processing. VARIANT_PARTS
14750 is the vector to convert. */
14751
14752 static const gdb::array_view<variant_part>
14753 create_variant_parts (struct obstack *obstack,
14754 const offset_map_type &offset_map,
14755 struct field_info *fi,
14756 const std::vector<variant_part_builder> &variant_parts)
14757 {
14758 if (variant_parts.empty ())
14759 return {};
14760
14761 size_t n = variant_parts.size ();
14762 variant_part *result = new (obstack) variant_part[n];
14763 for (size_t i = 0; i < n; ++i)
14764 create_one_variant_part (result[i], obstack, offset_map, fi,
14765 variant_parts[i]);
14766
14767 return gdb::array_view<variant_part> (result, n);
14768 }
14769
14770 /* Compute the variant part vector for FIP, attaching it to TYPE when
14771 done. */
14772
14773 static void
14774 add_variant_property (struct field_info *fip, struct type *type,
14775 struct dwarf2_cu *cu)
14776 {
14777 /* Map section offsets of fields to their field index. Note the
14778 field index here does not take the number of baseclasses into
14779 account. */
14780 offset_map_type offset_map;
14781 for (int i = 0; i < fip->fields.size (); ++i)
14782 offset_map[fip->fields[i].offset] = i;
14783
14784 struct objfile *objfile = cu->per_objfile->objfile;
14785 gdb::array_view<variant_part> parts
14786 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14787 fip->variant_parts);
14788
14789 struct dynamic_prop prop;
14790 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14791 obstack_copy (&objfile->objfile_obstack, &parts,
14792 sizeof (parts)));
14793
14794 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14795 }
14796
14797 /* Create the vector of fields, and attach it to the type. */
14798
14799 static void
14800 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14801 struct dwarf2_cu *cu)
14802 {
14803 int nfields = fip->nfields ();
14804
14805 /* Record the field count, allocate space for the array of fields,
14806 and create blank accessibility bitfields if necessary. */
14807 type->set_num_fields (nfields);
14808 type->set_fields
14809 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14810
14811 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
14812 {
14813 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14814
14815 TYPE_FIELD_PRIVATE_BITS (type) =
14816 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14817 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14818
14819 TYPE_FIELD_PROTECTED_BITS (type) =
14820 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14821 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14822
14823 TYPE_FIELD_IGNORE_BITS (type) =
14824 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14825 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14826 }
14827
14828 /* If the type has baseclasses, allocate and clear a bit vector for
14829 TYPE_FIELD_VIRTUAL_BITS. */
14830 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
14831 {
14832 int num_bytes = B_BYTES (fip->baseclasses.size ());
14833 unsigned char *pointer;
14834
14835 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14836 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14837 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14838 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14839 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14840 }
14841
14842 if (!fip->variant_parts.empty ())
14843 add_variant_property (fip, type, cu);
14844
14845 /* Copy the saved-up fields into the field vector. */
14846 for (int i = 0; i < nfields; ++i)
14847 {
14848 struct nextfield &field
14849 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14850 : fip->fields[i - fip->baseclasses.size ()]);
14851
14852 type->field (i) = field.field;
14853 switch (field.accessibility)
14854 {
14855 case DW_ACCESS_private:
14856 if (cu->per_cu->lang != language_ada)
14857 SET_TYPE_FIELD_PRIVATE (type, i);
14858 break;
14859
14860 case DW_ACCESS_protected:
14861 if (cu->per_cu->lang != language_ada)
14862 SET_TYPE_FIELD_PROTECTED (type, i);
14863 break;
14864
14865 case DW_ACCESS_public:
14866 break;
14867
14868 default:
14869 /* Unknown accessibility. Complain and treat it as public. */
14870 {
14871 complaint (_("unsupported accessibility %d"),
14872 field.accessibility);
14873 }
14874 break;
14875 }
14876 if (i < fip->baseclasses.size ())
14877 {
14878 switch (field.virtuality)
14879 {
14880 case DW_VIRTUALITY_virtual:
14881 case DW_VIRTUALITY_pure_virtual:
14882 if (cu->per_cu->lang == language_ada)
14883 error (_("unexpected virtuality in component of Ada type"));
14884 SET_TYPE_FIELD_VIRTUAL (type, i);
14885 break;
14886 }
14887 }
14888 }
14889 }
14890
14891 /* Return true if this member function is a constructor, false
14892 otherwise. */
14893
14894 static int
14895 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14896 {
14897 const char *fieldname;
14898 const char *type_name;
14899 int len;
14900
14901 if (die->parent == NULL)
14902 return 0;
14903
14904 if (die->parent->tag != DW_TAG_structure_type
14905 && die->parent->tag != DW_TAG_union_type
14906 && die->parent->tag != DW_TAG_class_type)
14907 return 0;
14908
14909 fieldname = dwarf2_name (die, cu);
14910 type_name = dwarf2_name (die->parent, cu);
14911 if (fieldname == NULL || type_name == NULL)
14912 return 0;
14913
14914 len = strlen (fieldname);
14915 return (strncmp (fieldname, type_name, len) == 0
14916 && (type_name[len] == '\0' || type_name[len] == '<'));
14917 }
14918
14919 /* Add a member function to the proper fieldlist. */
14920
14921 static void
14922 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14923 struct type *type, struct dwarf2_cu *cu)
14924 {
14925 struct objfile *objfile = cu->per_objfile->objfile;
14926 struct attribute *attr;
14927 int i;
14928 struct fnfieldlist *flp = nullptr;
14929 struct fn_field *fnp;
14930 const char *fieldname;
14931 struct type *this_type;
14932
14933 if (cu->per_cu->lang == language_ada)
14934 error (_("unexpected member function in Ada type"));
14935
14936 /* Get name of member function. */
14937 fieldname = dwarf2_name (die, cu);
14938 if (fieldname == NULL)
14939 return;
14940
14941 /* Look up member function name in fieldlist. */
14942 for (i = 0; i < fip->fnfieldlists.size (); i++)
14943 {
14944 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14945 {
14946 flp = &fip->fnfieldlists[i];
14947 break;
14948 }
14949 }
14950
14951 /* Create a new fnfieldlist if necessary. */
14952 if (flp == nullptr)
14953 {
14954 fip->fnfieldlists.emplace_back ();
14955 flp = &fip->fnfieldlists.back ();
14956 flp->name = fieldname;
14957 i = fip->fnfieldlists.size () - 1;
14958 }
14959
14960 /* Create a new member function field and add it to the vector of
14961 fnfieldlists. */
14962 flp->fnfields.emplace_back ();
14963 fnp = &flp->fnfields.back ();
14964
14965 /* Delay processing of the physname until later. */
14966 if (cu->per_cu->lang == language_cplus)
14967 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14968 die, cu);
14969 else
14970 {
14971 const char *physname = dwarf2_physname (fieldname, die, cu);
14972 fnp->physname = physname ? physname : "";
14973 }
14974
14975 fnp->type = alloc_type (objfile);
14976 this_type = read_type_die (die, cu);
14977 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14978 {
14979 int nparams = this_type->num_fields ();
14980
14981 /* TYPE is the domain of this method, and THIS_TYPE is the type
14982 of the method itself (TYPE_CODE_METHOD). */
14983 smash_to_method_type (fnp->type, type,
14984 TYPE_TARGET_TYPE (this_type),
14985 this_type->fields (),
14986 this_type->num_fields (),
14987 this_type->has_varargs ());
14988
14989 /* Handle static member functions.
14990 Dwarf2 has no clean way to discern C++ static and non-static
14991 member functions. G++ helps GDB by marking the first
14992 parameter for non-static member functions (which is the this
14993 pointer) as artificial. We obtain this information from
14994 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14995 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14996 fnp->voffset = VOFFSET_STATIC;
14997 }
14998 else
14999 complaint (_("member function type missing for '%s'"),
15000 dwarf2_full_name (fieldname, die, cu));
15001
15002 /* Get fcontext from DW_AT_containing_type if present. */
15003 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15004 fnp->fcontext = die_containing_type (die, cu);
15005
15006 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15007 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15008
15009 /* Get accessibility. */
15010 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15011 switch (accessibility)
15012 {
15013 case DW_ACCESS_private:
15014 fnp->is_private = 1;
15015 break;
15016 case DW_ACCESS_protected:
15017 fnp->is_protected = 1;
15018 break;
15019 }
15020
15021 /* Check for artificial methods. */
15022 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15023 if (attr && attr->as_boolean ())
15024 fnp->is_artificial = 1;
15025
15026 /* Check for defaulted methods. */
15027 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15028 if (attr != nullptr)
15029 fnp->defaulted = attr->defaulted ();
15030
15031 /* Check for deleted methods. */
15032 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15033 if (attr != nullptr && attr->as_boolean ())
15034 fnp->is_deleted = 1;
15035
15036 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15037
15038 /* Get index in virtual function table if it is a virtual member
15039 function. For older versions of GCC, this is an offset in the
15040 appropriate virtual table, as specified by DW_AT_containing_type.
15041 For everyone else, it is an expression to be evaluated relative
15042 to the object address. */
15043
15044 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15045 if (attr != nullptr)
15046 {
15047 if (attr->form_is_block () && attr->as_block ()->size > 0)
15048 {
15049 struct dwarf_block *block = attr->as_block ();
15050
15051 if (block->data[0] == DW_OP_constu)
15052 {
15053 /* Old-style GCC. */
15054 fnp->voffset = decode_locdesc (block, cu) + 2;
15055 }
15056 else if (block->data[0] == DW_OP_deref
15057 || (block->size > 1
15058 && block->data[0] == DW_OP_deref_size
15059 && block->data[1] == cu->header.addr_size))
15060 {
15061 fnp->voffset = decode_locdesc (block, cu);
15062 if ((fnp->voffset % cu->header.addr_size) != 0)
15063 dwarf2_complex_location_expr_complaint ();
15064 else
15065 fnp->voffset /= cu->header.addr_size;
15066 fnp->voffset += 2;
15067 }
15068 else
15069 dwarf2_complex_location_expr_complaint ();
15070
15071 if (!fnp->fcontext)
15072 {
15073 /* If there is no `this' field and no DW_AT_containing_type,
15074 we cannot actually find a base class context for the
15075 vtable! */
15076 if (this_type->num_fields () == 0
15077 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15078 {
15079 complaint (_("cannot determine context for virtual member "
15080 "function \"%s\" (offset %s)"),
15081 fieldname, sect_offset_str (die->sect_off));
15082 }
15083 else
15084 {
15085 fnp->fcontext
15086 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15087 }
15088 }
15089 }
15090 else if (attr->form_is_section_offset ())
15091 {
15092 dwarf2_complex_location_expr_complaint ();
15093 }
15094 else
15095 {
15096 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15097 fieldname);
15098 }
15099 }
15100 else
15101 {
15102 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15103 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15104 {
15105 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15106 complaint (_("Member function \"%s\" (offset %s) is virtual "
15107 "but the vtable offset is not specified"),
15108 fieldname, sect_offset_str (die->sect_off));
15109 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15110 TYPE_CPLUS_DYNAMIC (type) = 1;
15111 }
15112 }
15113 }
15114
15115 /* Create the vector of member function fields, and attach it to the type. */
15116
15117 static void
15118 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15119 struct dwarf2_cu *cu)
15120 {
15121 if (cu->per_cu->lang == language_ada)
15122 error (_("unexpected member functions in Ada type"));
15123
15124 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15125 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15126 TYPE_ALLOC (type,
15127 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15128
15129 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15130 {
15131 struct fnfieldlist &nf = fip->fnfieldlists[i];
15132 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15133
15134 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15135 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15136 fn_flp->fn_fields = (struct fn_field *)
15137 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15138
15139 for (int k = 0; k < nf.fnfields.size (); ++k)
15140 fn_flp->fn_fields[k] = nf.fnfields[k];
15141 }
15142
15143 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15144 }
15145
15146 /* Returns non-zero if NAME is the name of a vtable member in CU's
15147 language, zero otherwise. */
15148 static int
15149 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15150 {
15151 static const char vptr[] = "_vptr";
15152
15153 /* Look for the C++ form of the vtable. */
15154 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15155 return 1;
15156
15157 return 0;
15158 }
15159
15160 /* GCC outputs unnamed structures that are really pointers to member
15161 functions, with the ABI-specified layout. If TYPE describes
15162 such a structure, smash it into a member function type.
15163
15164 GCC shouldn't do this; it should just output pointer to member DIEs.
15165 This is GCC PR debug/28767. */
15166
15167 static void
15168 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15169 {
15170 struct type *pfn_type, *self_type, *new_type;
15171
15172 /* Check for a structure with no name and two children. */
15173 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15174 return;
15175
15176 /* Check for __pfn and __delta members. */
15177 if (TYPE_FIELD_NAME (type, 0) == NULL
15178 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15179 || TYPE_FIELD_NAME (type, 1) == NULL
15180 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15181 return;
15182
15183 /* Find the type of the method. */
15184 pfn_type = type->field (0).type ();
15185 if (pfn_type == NULL
15186 || pfn_type->code () != TYPE_CODE_PTR
15187 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15188 return;
15189
15190 /* Look for the "this" argument. */
15191 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15192 if (pfn_type->num_fields () == 0
15193 /* || pfn_type->field (0).type () == NULL */
15194 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15195 return;
15196
15197 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15198 new_type = alloc_type (objfile);
15199 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15200 pfn_type->fields (), pfn_type->num_fields (),
15201 pfn_type->has_varargs ());
15202 smash_to_methodptr_type (type, new_type);
15203 }
15204
15205 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15206 requires rewriting, then copy it and return the updated copy.
15207 Otherwise return nullptr. */
15208
15209 static struct type *
15210 rewrite_array_type (struct type *type)
15211 {
15212 if (type->code () != TYPE_CODE_ARRAY)
15213 return nullptr;
15214
15215 struct type *index_type = type->index_type ();
15216 range_bounds *current_bounds = index_type->bounds ();
15217
15218 /* Handle multi-dimensional arrays. */
15219 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15220 if (new_target == nullptr)
15221 {
15222 /* Maybe we don't need to rewrite this array. */
15223 if (current_bounds->low.kind () == PROP_CONST
15224 && current_bounds->high.kind () == PROP_CONST)
15225 return nullptr;
15226 }
15227
15228 /* Either the target type was rewritten, or the bounds have to be
15229 updated. Either way we want to copy the type and update
15230 everything. */
15231 struct type *copy = copy_type (type);
15232 int nfields = copy->num_fields ();
15233 field *new_fields
15234 = ((struct field *) TYPE_ZALLOC (copy,
15235 nfields * sizeof (struct field)));
15236 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15237 copy->set_fields (new_fields);
15238 if (new_target != nullptr)
15239 TYPE_TARGET_TYPE (copy) = new_target;
15240
15241 struct type *index_copy = copy_type (index_type);
15242 range_bounds *bounds
15243 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15244 sizeof (range_bounds));
15245 *bounds = *current_bounds;
15246 bounds->low.set_const_val (1);
15247 bounds->high.set_const_val (0);
15248 index_copy->set_bounds (bounds);
15249 copy->set_index_type (index_copy);
15250
15251 return copy;
15252 }
15253
15254 /* While some versions of GCC will generate complicated DWARF for an
15255 array (see quirk_ada_thick_pointer), more recent versions were
15256 modified to emit an explicit thick pointer structure. However, in
15257 this case, the array still has DWARF expressions for its ranges,
15258 and these must be ignored. */
15259
15260 static void
15261 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15262 struct type *type)
15263 {
15264 gdb_assert (cu->per_cu->lang == language_ada);
15265
15266 /* Check for a structure with two children. */
15267 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15268 return;
15269
15270 /* Check for P_ARRAY and P_BOUNDS members. */
15271 if (TYPE_FIELD_NAME (type, 0) == NULL
15272 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15273 || TYPE_FIELD_NAME (type, 1) == NULL
15274 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15275 return;
15276
15277 /* Make sure we're looking at a pointer to an array. */
15278 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15279 return;
15280
15281 /* The Ada code already knows how to handle these types, so all that
15282 we need to do is turn the bounds into static bounds. However, we
15283 don't want to rewrite existing array or index types in-place,
15284 because those may be referenced in other contexts where this
15285 rewriting is undesirable. */
15286 struct type *new_ary_type
15287 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15288 if (new_ary_type != nullptr)
15289 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15290 }
15291
15292 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15293 appropriate error checking and issuing complaints if there is a
15294 problem. */
15295
15296 static ULONGEST
15297 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15298 {
15299 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15300
15301 if (attr == nullptr)
15302 return 0;
15303
15304 if (!attr->form_is_constant ())
15305 {
15306 complaint (_("DW_AT_alignment must have constant form"
15307 " - DIE at %s [in module %s]"),
15308 sect_offset_str (die->sect_off),
15309 objfile_name (cu->per_objfile->objfile));
15310 return 0;
15311 }
15312
15313 LONGEST val = attr->constant_value (0);
15314 if (val < 0)
15315 {
15316 complaint (_("DW_AT_alignment value must not be negative"
15317 " - DIE at %s [in module %s]"),
15318 sect_offset_str (die->sect_off),
15319 objfile_name (cu->per_objfile->objfile));
15320 return 0;
15321 }
15322 ULONGEST align = val;
15323
15324 if (align == 0)
15325 {
15326 complaint (_("DW_AT_alignment value must not be zero"
15327 " - DIE at %s [in module %s]"),
15328 sect_offset_str (die->sect_off),
15329 objfile_name (cu->per_objfile->objfile));
15330 return 0;
15331 }
15332 if ((align & (align - 1)) != 0)
15333 {
15334 complaint (_("DW_AT_alignment value must be a power of 2"
15335 " - DIE at %s [in module %s]"),
15336 sect_offset_str (die->sect_off),
15337 objfile_name (cu->per_objfile->objfile));
15338 return 0;
15339 }
15340
15341 return align;
15342 }
15343
15344 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15345 the alignment for TYPE. */
15346
15347 static void
15348 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15349 struct type *type)
15350 {
15351 if (!set_type_align (type, get_alignment (cu, die)))
15352 complaint (_("DW_AT_alignment value too large"
15353 " - DIE at %s [in module %s]"),
15354 sect_offset_str (die->sect_off),
15355 objfile_name (cu->per_objfile->objfile));
15356 }
15357
15358 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15359 constant for a type, according to DWARF5 spec, Table 5.5. */
15360
15361 static bool
15362 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15363 {
15364 switch (value)
15365 {
15366 case DW_CC_normal:
15367 case DW_CC_pass_by_reference:
15368 case DW_CC_pass_by_value:
15369 return true;
15370
15371 default:
15372 complaint (_("unrecognized DW_AT_calling_convention value "
15373 "(%s) for a type"), pulongest (value));
15374 return false;
15375 }
15376 }
15377
15378 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15379 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15380 also according to GNU-specific values (see include/dwarf2.h). */
15381
15382 static bool
15383 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15384 {
15385 switch (value)
15386 {
15387 case DW_CC_normal:
15388 case DW_CC_program:
15389 case DW_CC_nocall:
15390 return true;
15391
15392 case DW_CC_GNU_renesas_sh:
15393 case DW_CC_GNU_borland_fastcall_i386:
15394 case DW_CC_GDB_IBM_OpenCL:
15395 return true;
15396
15397 default:
15398 complaint (_("unrecognized DW_AT_calling_convention value "
15399 "(%s) for a subroutine"), pulongest (value));
15400 return false;
15401 }
15402 }
15403
15404 /* Called when we find the DIE that starts a structure or union scope
15405 (definition) to create a type for the structure or union. Fill in
15406 the type's name and general properties; the members will not be
15407 processed until process_structure_scope. A symbol table entry for
15408 the type will also not be done until process_structure_scope (assuming
15409 the type has a name).
15410
15411 NOTE: we need to call these functions regardless of whether or not the
15412 DIE has a DW_AT_name attribute, since it might be an anonymous
15413 structure or union. This gets the type entered into our set of
15414 user defined types. */
15415
15416 static struct type *
15417 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15418 {
15419 struct objfile *objfile = cu->per_objfile->objfile;
15420 struct type *type;
15421 struct attribute *attr;
15422 const char *name;
15423
15424 /* If the definition of this type lives in .debug_types, read that type.
15425 Don't follow DW_AT_specification though, that will take us back up
15426 the chain and we want to go down. */
15427 attr = die->attr (DW_AT_signature);
15428 if (attr != nullptr)
15429 {
15430 type = get_DW_AT_signature_type (die, attr, cu);
15431
15432 /* The type's CU may not be the same as CU.
15433 Ensure TYPE is recorded with CU in die_type_hash. */
15434 return set_die_type (die, type, cu);
15435 }
15436
15437 type = alloc_type (objfile);
15438 INIT_CPLUS_SPECIFIC (type);
15439
15440 name = dwarf2_name (die, cu);
15441 if (name != NULL)
15442 {
15443 if (cu->per_cu->lang == language_cplus
15444 || cu->per_cu->lang == language_d
15445 || cu->per_cu->lang == language_rust)
15446 {
15447 const char *full_name = dwarf2_full_name (name, die, cu);
15448
15449 /* dwarf2_full_name might have already finished building the DIE's
15450 type. If so, there is no need to continue. */
15451 if (get_die_type (die, cu) != NULL)
15452 return get_die_type (die, cu);
15453
15454 type->set_name (full_name);
15455 }
15456 else
15457 {
15458 /* The name is already allocated along with this objfile, so
15459 we don't need to duplicate it for the type. */
15460 type->set_name (name);
15461 }
15462 }
15463
15464 if (die->tag == DW_TAG_structure_type)
15465 {
15466 type->set_code (TYPE_CODE_STRUCT);
15467 }
15468 else if (die->tag == DW_TAG_union_type)
15469 {
15470 type->set_code (TYPE_CODE_UNION);
15471 }
15472 else
15473 {
15474 type->set_code (TYPE_CODE_STRUCT);
15475 }
15476
15477 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
15478 type->set_is_declared_class (true);
15479
15480 /* Store the calling convention in the type if it's available in
15481 the die. Otherwise the calling convention remains set to
15482 the default value DW_CC_normal. */
15483 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15484 if (attr != nullptr
15485 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15486 {
15487 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15488 TYPE_CPLUS_CALLING_CONVENTION (type)
15489 = (enum dwarf_calling_convention) (attr->constant_value (0));
15490 }
15491
15492 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15493 if (attr != nullptr)
15494 {
15495 if (attr->form_is_constant ())
15496 TYPE_LENGTH (type) = attr->constant_value (0);
15497 else
15498 {
15499 struct dynamic_prop prop;
15500 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15501 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15502 TYPE_LENGTH (type) = 0;
15503 }
15504 }
15505 else
15506 {
15507 TYPE_LENGTH (type) = 0;
15508 }
15509
15510 maybe_set_alignment (cu, die, type);
15511
15512 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15513 {
15514 /* ICC<14 does not output the required DW_AT_declaration on
15515 incomplete types, but gives them a size of zero. */
15516 type->set_is_stub (true);
15517 }
15518 else
15519 type->set_stub_is_supported (true);
15520
15521 if (die_is_declaration (die, cu))
15522 type->set_is_stub (true);
15523 else if (attr == NULL && die->child == NULL
15524 && producer_is_realview (cu->producer))
15525 /* RealView does not output the required DW_AT_declaration
15526 on incomplete types. */
15527 type->set_is_stub (true);
15528
15529 /* We need to add the type field to the die immediately so we don't
15530 infinitely recurse when dealing with pointers to the structure
15531 type within the structure itself. */
15532 set_die_type (die, type, cu);
15533
15534 /* set_die_type should be already done. */
15535 set_descriptive_type (type, die, cu);
15536
15537 return type;
15538 }
15539
15540 static void handle_struct_member_die
15541 (struct die_info *child_die,
15542 struct type *type,
15543 struct field_info *fi,
15544 std::vector<struct symbol *> *template_args,
15545 struct dwarf2_cu *cu);
15546
15547 /* A helper for handle_struct_member_die that handles
15548 DW_TAG_variant_part. */
15549
15550 static void
15551 handle_variant_part (struct die_info *die, struct type *type,
15552 struct field_info *fi,
15553 std::vector<struct symbol *> *template_args,
15554 struct dwarf2_cu *cu)
15555 {
15556 variant_part_builder *new_part;
15557 if (fi->current_variant_part == nullptr)
15558 {
15559 fi->variant_parts.emplace_back ();
15560 new_part = &fi->variant_parts.back ();
15561 }
15562 else if (!fi->current_variant_part->processing_variant)
15563 {
15564 complaint (_("nested DW_TAG_variant_part seen "
15565 "- DIE at %s [in module %s]"),
15566 sect_offset_str (die->sect_off),
15567 objfile_name (cu->per_objfile->objfile));
15568 return;
15569 }
15570 else
15571 {
15572 variant_field &current = fi->current_variant_part->variants.back ();
15573 current.variant_parts.emplace_back ();
15574 new_part = &current.variant_parts.back ();
15575 }
15576
15577 /* When we recurse, we want callees to add to this new variant
15578 part. */
15579 scoped_restore save_current_variant_part
15580 = make_scoped_restore (&fi->current_variant_part, new_part);
15581
15582 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15583 if (discr == NULL)
15584 {
15585 /* It's a univariant form, an extension we support. */
15586 }
15587 else if (discr->form_is_ref ())
15588 {
15589 struct dwarf2_cu *target_cu = cu;
15590 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15591
15592 new_part->discriminant_offset = target_die->sect_off;
15593 }
15594 else
15595 {
15596 complaint (_("DW_AT_discr does not have DIE reference form"
15597 " - DIE at %s [in module %s]"),
15598 sect_offset_str (die->sect_off),
15599 objfile_name (cu->per_objfile->objfile));
15600 }
15601
15602 for (die_info *child_die = die->child;
15603 child_die != NULL;
15604 child_die = child_die->sibling)
15605 handle_struct_member_die (child_die, type, fi, template_args, cu);
15606 }
15607
15608 /* A helper for handle_struct_member_die that handles
15609 DW_TAG_variant. */
15610
15611 static void
15612 handle_variant (struct die_info *die, struct type *type,
15613 struct field_info *fi,
15614 std::vector<struct symbol *> *template_args,
15615 struct dwarf2_cu *cu)
15616 {
15617 if (fi->current_variant_part == nullptr)
15618 {
15619 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15620 "- DIE at %s [in module %s]"),
15621 sect_offset_str (die->sect_off),
15622 objfile_name (cu->per_objfile->objfile));
15623 return;
15624 }
15625 if (fi->current_variant_part->processing_variant)
15626 {
15627 complaint (_("nested DW_TAG_variant seen "
15628 "- DIE at %s [in module %s]"),
15629 sect_offset_str (die->sect_off),
15630 objfile_name (cu->per_objfile->objfile));
15631 return;
15632 }
15633
15634 scoped_restore save_processing_variant
15635 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15636 true);
15637
15638 fi->current_variant_part->variants.emplace_back ();
15639 variant_field &variant = fi->current_variant_part->variants.back ();
15640 variant.first_field = fi->fields.size ();
15641
15642 /* In a variant we want to get the discriminant and also add a
15643 field for our sole member child. */
15644 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15645 if (discr == nullptr || !discr->form_is_constant ())
15646 {
15647 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15648 if (discr == nullptr || discr->as_block ()->size == 0)
15649 variant.default_branch = true;
15650 else
15651 variant.discr_list_data = discr->as_block ();
15652 }
15653 else
15654 variant.discriminant_value = discr->constant_value (0);
15655
15656 for (die_info *variant_child = die->child;
15657 variant_child != NULL;
15658 variant_child = variant_child->sibling)
15659 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15660
15661 variant.last_field = fi->fields.size ();
15662 }
15663
15664 /* A helper for process_structure_scope that handles a single member
15665 DIE. */
15666
15667 static void
15668 handle_struct_member_die (struct die_info *child_die, struct type *type,
15669 struct field_info *fi,
15670 std::vector<struct symbol *> *template_args,
15671 struct dwarf2_cu *cu)
15672 {
15673 if (child_die->tag == DW_TAG_member
15674 || child_die->tag == DW_TAG_variable)
15675 {
15676 /* NOTE: carlton/2002-11-05: A C++ static data member
15677 should be a DW_TAG_member that is a declaration, but
15678 all versions of G++ as of this writing (so through at
15679 least 3.2.1) incorrectly generate DW_TAG_variable
15680 tags for them instead. */
15681 dwarf2_add_field (fi, child_die, cu);
15682 }
15683 else if (child_die->tag == DW_TAG_subprogram)
15684 {
15685 /* Rust doesn't have member functions in the C++ sense.
15686 However, it does emit ordinary functions as children
15687 of a struct DIE. */
15688 if (cu->per_cu->lang == language_rust)
15689 read_func_scope (child_die, cu);
15690 else
15691 {
15692 /* C++ member function. */
15693 dwarf2_add_member_fn (fi, child_die, type, cu);
15694 }
15695 }
15696 else if (child_die->tag == DW_TAG_inheritance)
15697 {
15698 /* C++ base class field. */
15699 dwarf2_add_field (fi, child_die, cu);
15700 }
15701 else if (type_can_define_types (child_die))
15702 dwarf2_add_type_defn (fi, child_die, cu);
15703 else if (child_die->tag == DW_TAG_template_type_param
15704 || child_die->tag == DW_TAG_template_value_param)
15705 {
15706 struct symbol *arg = new_symbol (child_die, NULL, cu);
15707
15708 if (arg != NULL)
15709 template_args->push_back (arg);
15710 }
15711 else if (child_die->tag == DW_TAG_variant_part)
15712 handle_variant_part (child_die, type, fi, template_args, cu);
15713 else if (child_die->tag == DW_TAG_variant)
15714 handle_variant (child_die, type, fi, template_args, cu);
15715 }
15716
15717 /* Finish creating a structure or union type, including filling in
15718 its members and creating a symbol for it. */
15719
15720 static void
15721 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15722 {
15723 struct objfile *objfile = cu->per_objfile->objfile;
15724 struct die_info *child_die;
15725 struct type *type;
15726
15727 type = get_die_type (die, cu);
15728 if (type == NULL)
15729 type = read_structure_type (die, cu);
15730
15731 bool has_template_parameters = false;
15732 if (die->child != NULL && ! die_is_declaration (die, cu))
15733 {
15734 struct field_info fi;
15735 std::vector<struct symbol *> template_args;
15736
15737 child_die = die->child;
15738
15739 while (child_die && child_die->tag)
15740 {
15741 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15742 child_die = child_die->sibling;
15743 }
15744
15745 /* Attach template arguments to type. */
15746 if (!template_args.empty ())
15747 {
15748 has_template_parameters = true;
15749 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15750 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15751 TYPE_TEMPLATE_ARGUMENTS (type)
15752 = XOBNEWVEC (&objfile->objfile_obstack,
15753 struct symbol *,
15754 TYPE_N_TEMPLATE_ARGUMENTS (type));
15755 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15756 template_args.data (),
15757 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15758 * sizeof (struct symbol *)));
15759 }
15760
15761 /* Attach fields and member functions to the type. */
15762 if (fi.nfields () > 0)
15763 dwarf2_attach_fields_to_type (&fi, type, cu);
15764 if (!fi.fnfieldlists.empty ())
15765 {
15766 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15767
15768 /* Get the type which refers to the base class (possibly this
15769 class itself) which contains the vtable pointer for the current
15770 class from the DW_AT_containing_type attribute. This use of
15771 DW_AT_containing_type is a GNU extension. */
15772
15773 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15774 {
15775 struct type *t = die_containing_type (die, cu);
15776
15777 set_type_vptr_basetype (type, t);
15778 if (type == t)
15779 {
15780 int i;
15781
15782 /* Our own class provides vtbl ptr. */
15783 for (i = t->num_fields () - 1;
15784 i >= TYPE_N_BASECLASSES (t);
15785 --i)
15786 {
15787 const char *fieldname = TYPE_FIELD_NAME (t, i);
15788
15789 if (is_vtable_name (fieldname, cu))
15790 {
15791 set_type_vptr_fieldno (type, i);
15792 break;
15793 }
15794 }
15795
15796 /* Complain if virtual function table field not found. */
15797 if (i < TYPE_N_BASECLASSES (t))
15798 complaint (_("virtual function table pointer "
15799 "not found when defining class '%s'"),
15800 type->name () ? type->name () : "");
15801 }
15802 else
15803 {
15804 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15805 }
15806 }
15807 else if (cu->producer
15808 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15809 {
15810 /* The IBM XLC compiler does not provide direct indication
15811 of the containing type, but the vtable pointer is
15812 always named __vfp. */
15813
15814 int i;
15815
15816 for (i = type->num_fields () - 1;
15817 i >= TYPE_N_BASECLASSES (type);
15818 --i)
15819 {
15820 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15821 {
15822 set_type_vptr_fieldno (type, i);
15823 set_type_vptr_basetype (type, type);
15824 break;
15825 }
15826 }
15827 }
15828 }
15829
15830 /* Copy fi.typedef_field_list linked list elements content into the
15831 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15832 if (!fi.typedef_field_list.empty ())
15833 {
15834 int count = fi.typedef_field_list.size ();
15835
15836 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15837 TYPE_TYPEDEF_FIELD_ARRAY (type)
15838 = ((struct decl_field *)
15839 TYPE_ALLOC (type,
15840 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15841 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15842
15843 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15844 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15845 }
15846
15847 /* Copy fi.nested_types_list linked list elements content into the
15848 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15849 if (!fi.nested_types_list.empty ()
15850 && cu->per_cu->lang != language_ada)
15851 {
15852 int count = fi.nested_types_list.size ();
15853
15854 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15855 TYPE_NESTED_TYPES_ARRAY (type)
15856 = ((struct decl_field *)
15857 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15858 TYPE_NESTED_TYPES_COUNT (type) = count;
15859
15860 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15861 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15862 }
15863 }
15864
15865 quirk_gcc_member_function_pointer (type, objfile);
15866 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
15867 cu->rust_unions.push_back (type);
15868 else if (cu->per_cu->lang == language_ada)
15869 quirk_ada_thick_pointer_struct (die, cu, type);
15870
15871 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15872 snapshots) has been known to create a die giving a declaration
15873 for a class that has, as a child, a die giving a definition for a
15874 nested class. So we have to process our children even if the
15875 current die is a declaration. Normally, of course, a declaration
15876 won't have any children at all. */
15877
15878 child_die = die->child;
15879
15880 while (child_die != NULL && child_die->tag)
15881 {
15882 if (child_die->tag == DW_TAG_member
15883 || child_die->tag == DW_TAG_variable
15884 || child_die->tag == DW_TAG_inheritance
15885 || child_die->tag == DW_TAG_template_value_param
15886 || child_die->tag == DW_TAG_template_type_param)
15887 {
15888 /* Do nothing. */
15889 }
15890 else
15891 process_die (child_die, cu);
15892
15893 child_die = child_die->sibling;
15894 }
15895
15896 /* Do not consider external references. According to the DWARF standard,
15897 these DIEs are identified by the fact that they have no byte_size
15898 attribute, and a declaration attribute. */
15899 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15900 || !die_is_declaration (die, cu)
15901 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15902 {
15903 struct symbol *sym = new_symbol (die, type, cu);
15904
15905 if (has_template_parameters)
15906 {
15907 struct symtab *symtab;
15908 if (sym != nullptr)
15909 symtab = symbol_symtab (sym);
15910 else if (cu->line_header != nullptr)
15911 {
15912 /* Any related symtab will do. */
15913 symtab
15914 = cu->line_header->file_names ()[0].symtab;
15915 }
15916 else
15917 {
15918 symtab = nullptr;
15919 complaint (_("could not find suitable "
15920 "symtab for template parameter"
15921 " - DIE at %s [in module %s]"),
15922 sect_offset_str (die->sect_off),
15923 objfile_name (objfile));
15924 }
15925
15926 if (symtab != nullptr)
15927 {
15928 /* Make sure that the symtab is set on the new symbols.
15929 Even though they don't appear in this symtab directly,
15930 other parts of gdb assume that symbols do, and this is
15931 reasonably true. */
15932 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15933 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15934 }
15935 }
15936 }
15937 }
15938
15939 /* Assuming DIE is an enumeration type, and TYPE is its associated
15940 type, update TYPE using some information only available in DIE's
15941 children. In particular, the fields are computed. */
15942
15943 static void
15944 update_enumeration_type_from_children (struct die_info *die,
15945 struct type *type,
15946 struct dwarf2_cu *cu)
15947 {
15948 struct die_info *child_die;
15949 int unsigned_enum = 1;
15950 int flag_enum = 1;
15951
15952 auto_obstack obstack;
15953 std::vector<struct field> fields;
15954
15955 for (child_die = die->child;
15956 child_die != NULL && child_die->tag;
15957 child_die = child_die->sibling)
15958 {
15959 struct attribute *attr;
15960 LONGEST value;
15961 const gdb_byte *bytes;
15962 struct dwarf2_locexpr_baton *baton;
15963 const char *name;
15964
15965 if (child_die->tag != DW_TAG_enumerator)
15966 continue;
15967
15968 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15969 if (attr == NULL)
15970 continue;
15971
15972 name = dwarf2_name (child_die, cu);
15973 if (name == NULL)
15974 name = "<anonymous enumerator>";
15975
15976 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15977 &value, &bytes, &baton);
15978 if (value < 0)
15979 {
15980 unsigned_enum = 0;
15981 flag_enum = 0;
15982 }
15983 else
15984 {
15985 if (count_one_bits_ll (value) >= 2)
15986 flag_enum = 0;
15987 }
15988
15989 fields.emplace_back ();
15990 struct field &field = fields.back ();
15991 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15992 SET_FIELD_ENUMVAL (field, value);
15993 }
15994
15995 if (!fields.empty ())
15996 {
15997 type->set_num_fields (fields.size ());
15998 type->set_fields
15999 ((struct field *)
16000 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16001 memcpy (type->fields (), fields.data (),
16002 sizeof (struct field) * fields.size ());
16003 }
16004
16005 if (unsigned_enum)
16006 type->set_is_unsigned (true);
16007
16008 if (flag_enum)
16009 type->set_is_flag_enum (true);
16010 }
16011
16012 /* Given a DW_AT_enumeration_type die, set its type. We do not
16013 complete the type's fields yet, or create any symbols. */
16014
16015 static struct type *
16016 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16017 {
16018 struct objfile *objfile = cu->per_objfile->objfile;
16019 struct type *type;
16020 struct attribute *attr;
16021 const char *name;
16022
16023 /* If the definition of this type lives in .debug_types, read that type.
16024 Don't follow DW_AT_specification though, that will take us back up
16025 the chain and we want to go down. */
16026 attr = die->attr (DW_AT_signature);
16027 if (attr != nullptr)
16028 {
16029 type = get_DW_AT_signature_type (die, attr, cu);
16030
16031 /* The type's CU may not be the same as CU.
16032 Ensure TYPE is recorded with CU in die_type_hash. */
16033 return set_die_type (die, type, cu);
16034 }
16035
16036 type = alloc_type (objfile);
16037
16038 type->set_code (TYPE_CODE_ENUM);
16039 name = dwarf2_full_name (NULL, die, cu);
16040 if (name != NULL)
16041 type->set_name (name);
16042
16043 attr = dwarf2_attr (die, DW_AT_type, cu);
16044 if (attr != NULL)
16045 {
16046 struct type *underlying_type = die_type (die, cu);
16047
16048 TYPE_TARGET_TYPE (type) = underlying_type;
16049 }
16050
16051 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16052 if (attr != nullptr)
16053 {
16054 TYPE_LENGTH (type) = attr->constant_value (0);
16055 }
16056 else
16057 {
16058 TYPE_LENGTH (type) = 0;
16059 }
16060
16061 maybe_set_alignment (cu, die, type);
16062
16063 /* The enumeration DIE can be incomplete. In Ada, any type can be
16064 declared as private in the package spec, and then defined only
16065 inside the package body. Such types are known as Taft Amendment
16066 Types. When another package uses such a type, an incomplete DIE
16067 may be generated by the compiler. */
16068 if (die_is_declaration (die, cu))
16069 type->set_is_stub (true);
16070
16071 /* If this type has an underlying type that is not a stub, then we
16072 may use its attributes. We always use the "unsigned" attribute
16073 in this situation, because ordinarily we guess whether the type
16074 is unsigned -- but the guess can be wrong and the underlying type
16075 can tell us the reality. However, we defer to a local size
16076 attribute if one exists, because this lets the compiler override
16077 the underlying type if needed. */
16078 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16079 {
16080 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16081 underlying_type = check_typedef (underlying_type);
16082
16083 type->set_is_unsigned (underlying_type->is_unsigned ());
16084
16085 if (TYPE_LENGTH (type) == 0)
16086 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16087
16088 if (TYPE_RAW_ALIGN (type) == 0
16089 && TYPE_RAW_ALIGN (underlying_type) != 0)
16090 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16091 }
16092
16093 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16094
16095 set_die_type (die, type, cu);
16096
16097 /* Finish the creation of this type by using the enum's children.
16098 Note that, as usual, this must come after set_die_type to avoid
16099 infinite recursion when trying to compute the names of the
16100 enumerators. */
16101 update_enumeration_type_from_children (die, type, cu);
16102
16103 return type;
16104 }
16105
16106 /* Given a pointer to a die which begins an enumeration, process all
16107 the dies that define the members of the enumeration, and create the
16108 symbol for the enumeration type.
16109
16110 NOTE: We reverse the order of the element list. */
16111
16112 static void
16113 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16114 {
16115 struct type *this_type;
16116
16117 this_type = get_die_type (die, cu);
16118 if (this_type == NULL)
16119 this_type = read_enumeration_type (die, cu);
16120
16121 if (die->child != NULL)
16122 {
16123 struct die_info *child_die;
16124 const char *name;
16125
16126 child_die = die->child;
16127 while (child_die && child_die->tag)
16128 {
16129 if (child_die->tag != DW_TAG_enumerator)
16130 {
16131 process_die (child_die, cu);
16132 }
16133 else
16134 {
16135 name = dwarf2_name (child_die, cu);
16136 if (name)
16137 new_symbol (child_die, this_type, cu);
16138 }
16139
16140 child_die = child_die->sibling;
16141 }
16142 }
16143
16144 /* If we are reading an enum from a .debug_types unit, and the enum
16145 is a declaration, and the enum is not the signatured type in the
16146 unit, then we do not want to add a symbol for it. Adding a
16147 symbol would in some cases obscure the true definition of the
16148 enum, giving users an incomplete type when the definition is
16149 actually available. Note that we do not want to do this for all
16150 enums which are just declarations, because C++0x allows forward
16151 enum declarations. */
16152 if (cu->per_cu->is_debug_types
16153 && die_is_declaration (die, cu))
16154 {
16155 struct signatured_type *sig_type;
16156
16157 sig_type = (struct signatured_type *) cu->per_cu;
16158 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16159 if (sig_type->type_offset_in_section != die->sect_off)
16160 return;
16161 }
16162
16163 new_symbol (die, this_type, cu);
16164 }
16165
16166 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16167 expression for an index type and finds the corresponding field
16168 offset in the hidden "P_BOUNDS" structure. Returns true on success
16169 and updates *FIELD, false if it fails to recognize an
16170 expression. */
16171
16172 static bool
16173 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16174 int *bounds_offset, struct field *field,
16175 struct dwarf2_cu *cu)
16176 {
16177 struct attribute *attr = dwarf2_attr (die, name, cu);
16178 if (attr == nullptr || !attr->form_is_block ())
16179 return false;
16180
16181 const struct dwarf_block *block = attr->as_block ();
16182 const gdb_byte *start = block->data;
16183 const gdb_byte *end = block->data + block->size;
16184
16185 /* The expression to recognize generally looks like:
16186
16187 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16188 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16189
16190 However, the second "plus_uconst" may be missing:
16191
16192 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16193 DW_OP_deref_size: 4)
16194
16195 This happens when the field is at the start of the structure.
16196
16197 Also, the final deref may not be sized:
16198
16199 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16200 DW_OP_deref)
16201
16202 This happens when the size of the index type happens to be the
16203 same as the architecture's word size. This can occur with or
16204 without the second plus_uconst. */
16205
16206 if (end - start < 2)
16207 return false;
16208 if (*start++ != DW_OP_push_object_address)
16209 return false;
16210 if (*start++ != DW_OP_plus_uconst)
16211 return false;
16212
16213 uint64_t this_bound_off;
16214 start = gdb_read_uleb128 (start, end, &this_bound_off);
16215 if (start == nullptr || (int) this_bound_off != this_bound_off)
16216 return false;
16217 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16218 is consistent among all bounds. */
16219 if (*bounds_offset == -1)
16220 *bounds_offset = this_bound_off;
16221 else if (*bounds_offset != this_bound_off)
16222 return false;
16223
16224 if (start == end || *start++ != DW_OP_deref)
16225 return false;
16226
16227 int offset = 0;
16228 if (start ==end)
16229 return false;
16230 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16231 {
16232 /* This means an offset of 0. */
16233 }
16234 else if (*start++ != DW_OP_plus_uconst)
16235 return false;
16236 else
16237 {
16238 /* The size is the parameter to DW_OP_plus_uconst. */
16239 uint64_t val;
16240 start = gdb_read_uleb128 (start, end, &val);
16241 if (start == nullptr)
16242 return false;
16243 if ((int) val != val)
16244 return false;
16245 offset = val;
16246 }
16247
16248 if (start == end)
16249 return false;
16250
16251 uint64_t size;
16252 if (*start == DW_OP_deref_size)
16253 {
16254 start = gdb_read_uleb128 (start + 1, end, &size);
16255 if (start == nullptr)
16256 return false;
16257 }
16258 else if (*start == DW_OP_deref)
16259 {
16260 size = cu->header.addr_size;
16261 ++start;
16262 }
16263 else
16264 return false;
16265
16266 SET_FIELD_BITPOS (*field, 8 * offset);
16267 if (size != TYPE_LENGTH (field->type ()))
16268 FIELD_BITSIZE (*field) = 8 * size;
16269
16270 return true;
16271 }
16272
16273 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16274 some kinds of Ada arrays:
16275
16276 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16277 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16278 <11e0> DW_AT_data_location: 2 byte block: 97 6
16279 (DW_OP_push_object_address; DW_OP_deref)
16280 <11e3> DW_AT_type : <0x1173>
16281 <11e7> DW_AT_sibling : <0x1201>
16282 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16283 <11ec> DW_AT_type : <0x1206>
16284 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16285 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16286 DW_OP_deref_size: 4)
16287 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16288 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16289 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16290
16291 This actually represents a "thick pointer", which is a structure
16292 with two elements: one that is a pointer to the array data, and one
16293 that is a pointer to another structure; this second structure holds
16294 the array bounds.
16295
16296 This returns a new type on success, or nullptr if this didn't
16297 recognize the type. */
16298
16299 static struct type *
16300 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16301 struct type *type)
16302 {
16303 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16304 /* So far we've only seen this with block form. */
16305 if (attr == nullptr || !attr->form_is_block ())
16306 return nullptr;
16307
16308 /* Note that this will fail if the structure layout is changed by
16309 the compiler. However, we have no good way to recognize some
16310 other layout, because we don't know what expression the compiler
16311 might choose to emit should this happen. */
16312 struct dwarf_block *blk = attr->as_block ();
16313 if (blk->size != 2
16314 || blk->data[0] != DW_OP_push_object_address
16315 || blk->data[1] != DW_OP_deref)
16316 return nullptr;
16317
16318 int bounds_offset = -1;
16319 int max_align = -1;
16320 std::vector<struct field> range_fields;
16321 for (struct die_info *child_die = die->child;
16322 child_die;
16323 child_die = child_die->sibling)
16324 {
16325 if (child_die->tag == DW_TAG_subrange_type)
16326 {
16327 struct type *underlying = read_subrange_index_type (child_die, cu);
16328
16329 int this_align = type_align (underlying);
16330 if (this_align > max_align)
16331 max_align = this_align;
16332
16333 range_fields.emplace_back ();
16334 range_fields.emplace_back ();
16335
16336 struct field &lower = range_fields[range_fields.size () - 2];
16337 struct field &upper = range_fields[range_fields.size () - 1];
16338
16339 lower.set_type (underlying);
16340 FIELD_ARTIFICIAL (lower) = 1;
16341
16342 upper.set_type (underlying);
16343 FIELD_ARTIFICIAL (upper) = 1;
16344
16345 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16346 &bounds_offset, &lower, cu)
16347 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16348 &bounds_offset, &upper, cu))
16349 return nullptr;
16350 }
16351 }
16352
16353 /* This shouldn't really happen, but double-check that we found
16354 where the bounds are stored. */
16355 if (bounds_offset == -1)
16356 return nullptr;
16357
16358 struct objfile *objfile = cu->per_objfile->objfile;
16359 for (int i = 0; i < range_fields.size (); i += 2)
16360 {
16361 char name[20];
16362
16363 /* Set the name of each field in the bounds. */
16364 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16365 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16366 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16367 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16368 }
16369
16370 struct type *bounds = alloc_type (objfile);
16371 bounds->set_code (TYPE_CODE_STRUCT);
16372
16373 bounds->set_num_fields (range_fields.size ());
16374 bounds->set_fields
16375 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16376 * sizeof (struct field))));
16377 memcpy (bounds->fields (), range_fields.data (),
16378 bounds->num_fields () * sizeof (struct field));
16379
16380 int last_fieldno = range_fields.size () - 1;
16381 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16382 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16383 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16384
16385 /* Rewrite the existing array type in place. Specifically, we
16386 remove any dynamic properties we might have read, and we replace
16387 the index types. */
16388 struct type *iter = type;
16389 for (int i = 0; i < range_fields.size (); i += 2)
16390 {
16391 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16392 iter->main_type->dyn_prop_list = nullptr;
16393 iter->set_index_type
16394 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16395 iter = TYPE_TARGET_TYPE (iter);
16396 }
16397
16398 struct type *result = alloc_type (objfile);
16399 result->set_code (TYPE_CODE_STRUCT);
16400
16401 result->set_num_fields (2);
16402 result->set_fields
16403 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16404 * sizeof (struct field))));
16405
16406 /* The names are chosen to coincide with what the compiler does with
16407 -fgnat-encodings=all, which the Ada code in gdb already
16408 understands. */
16409 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16410 result->field (0).set_type (lookup_pointer_type (type));
16411
16412 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16413 result->field (1).set_type (lookup_pointer_type (bounds));
16414 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16415
16416 result->set_name (type->name ());
16417 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16418 + TYPE_LENGTH (result->field (1).type ()));
16419
16420 return result;
16421 }
16422
16423 /* Extract all information from a DW_TAG_array_type DIE and put it in
16424 the DIE's type field. For now, this only handles one dimensional
16425 arrays. */
16426
16427 static struct type *
16428 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16429 {
16430 struct objfile *objfile = cu->per_objfile->objfile;
16431 struct die_info *child_die;
16432 struct type *type;
16433 struct type *element_type, *range_type, *index_type;
16434 struct attribute *attr;
16435 const char *name;
16436 struct dynamic_prop *byte_stride_prop = NULL;
16437 unsigned int bit_stride = 0;
16438
16439 element_type = die_type (die, cu);
16440
16441 /* The die_type call above may have already set the type for this DIE. */
16442 type = get_die_type (die, cu);
16443 if (type)
16444 return type;
16445
16446 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16447 if (attr != NULL)
16448 {
16449 int stride_ok;
16450 struct type *prop_type = cu->addr_sized_int_type (false);
16451
16452 byte_stride_prop
16453 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16454 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16455 prop_type);
16456 if (!stride_ok)
16457 {
16458 complaint (_("unable to read array DW_AT_byte_stride "
16459 " - DIE at %s [in module %s]"),
16460 sect_offset_str (die->sect_off),
16461 objfile_name (cu->per_objfile->objfile));
16462 /* Ignore this attribute. We will likely not be able to print
16463 arrays of this type correctly, but there is little we can do
16464 to help if we cannot read the attribute's value. */
16465 byte_stride_prop = NULL;
16466 }
16467 }
16468
16469 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16470 if (attr != NULL)
16471 bit_stride = attr->constant_value (0);
16472
16473 /* Irix 6.2 native cc creates array types without children for
16474 arrays with unspecified length. */
16475 if (die->child == NULL)
16476 {
16477 index_type = objfile_type (objfile)->builtin_int;
16478 range_type = create_static_range_type (NULL, index_type, 0, -1);
16479 type = create_array_type_with_stride (NULL, element_type, range_type,
16480 byte_stride_prop, bit_stride);
16481 return set_die_type (die, type, cu);
16482 }
16483
16484 std::vector<struct type *> range_types;
16485 child_die = die->child;
16486 while (child_die && child_die->tag)
16487 {
16488 if (child_die->tag == DW_TAG_subrange_type)
16489 {
16490 struct type *child_type = read_type_die (child_die, cu);
16491
16492 if (child_type != NULL)
16493 {
16494 /* The range type was succesfully read. Save it for the
16495 array type creation. */
16496 range_types.push_back (child_type);
16497 }
16498 }
16499 child_die = child_die->sibling;
16500 }
16501
16502 if (range_types.empty ())
16503 {
16504 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16505 sect_offset_str (die->sect_off),
16506 objfile_name (cu->per_objfile->objfile));
16507 return NULL;
16508 }
16509
16510 /* Dwarf2 dimensions are output from left to right, create the
16511 necessary array types in backwards order. */
16512
16513 type = element_type;
16514
16515 if (read_array_order (die, cu) == DW_ORD_col_major)
16516 {
16517 int i = 0;
16518
16519 while (i < range_types.size ())
16520 {
16521 type = create_array_type_with_stride (NULL, type, range_types[i++],
16522 byte_stride_prop, bit_stride);
16523 bit_stride = 0;
16524 byte_stride_prop = nullptr;
16525 }
16526 }
16527 else
16528 {
16529 size_t ndim = range_types.size ();
16530 while (ndim-- > 0)
16531 {
16532 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16533 byte_stride_prop, bit_stride);
16534 bit_stride = 0;
16535 byte_stride_prop = nullptr;
16536 }
16537 }
16538
16539 gdb_assert (type != element_type);
16540
16541 /* Understand Dwarf2 support for vector types (like they occur on
16542 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16543 array type. This is not part of the Dwarf2/3 standard yet, but a
16544 custom vendor extension. The main difference between a regular
16545 array and the vector variant is that vectors are passed by value
16546 to functions. */
16547 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16548 if (attr != nullptr)
16549 make_vector_type (type);
16550
16551 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16552 implementation may choose to implement triple vectors using this
16553 attribute. */
16554 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16555 if (attr != nullptr && attr->form_is_unsigned ())
16556 {
16557 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16558 TYPE_LENGTH (type) = attr->as_unsigned ();
16559 else
16560 complaint (_("DW_AT_byte_size for array type smaller "
16561 "than the total size of elements"));
16562 }
16563
16564 name = dwarf2_name (die, cu);
16565 if (name)
16566 type->set_name (name);
16567
16568 maybe_set_alignment (cu, die, type);
16569
16570 struct type *replacement_type = nullptr;
16571 if (cu->per_cu->lang == language_ada)
16572 {
16573 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16574 if (replacement_type != nullptr)
16575 type = replacement_type;
16576 }
16577
16578 /* Install the type in the die. */
16579 set_die_type (die, type, cu, replacement_type != nullptr);
16580
16581 /* set_die_type should be already done. */
16582 set_descriptive_type (type, die, cu);
16583
16584 return type;
16585 }
16586
16587 static enum dwarf_array_dim_ordering
16588 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16589 {
16590 struct attribute *attr;
16591
16592 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16593
16594 if (attr != nullptr)
16595 {
16596 LONGEST val = attr->constant_value (-1);
16597 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16598 return (enum dwarf_array_dim_ordering) val;
16599 }
16600
16601 /* GNU F77 is a special case, as at 08/2004 array type info is the
16602 opposite order to the dwarf2 specification, but data is still
16603 laid out as per normal fortran.
16604
16605 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16606 version checking. */
16607
16608 if (cu->per_cu->lang == language_fortran
16609 && cu->producer && strstr (cu->producer, "GNU F77"))
16610 {
16611 return DW_ORD_row_major;
16612 }
16613
16614 switch (cu->language_defn->array_ordering ())
16615 {
16616 case array_column_major:
16617 return DW_ORD_col_major;
16618 case array_row_major:
16619 default:
16620 return DW_ORD_row_major;
16621 };
16622 }
16623
16624 /* Extract all information from a DW_TAG_set_type DIE and put it in
16625 the DIE's type field. */
16626
16627 static struct type *
16628 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16629 {
16630 struct type *domain_type, *set_type;
16631 struct attribute *attr;
16632
16633 domain_type = die_type (die, cu);
16634
16635 /* The die_type call above may have already set the type for this DIE. */
16636 set_type = get_die_type (die, cu);
16637 if (set_type)
16638 return set_type;
16639
16640 set_type = create_set_type (NULL, domain_type);
16641
16642 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16643 if (attr != nullptr && attr->form_is_unsigned ())
16644 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16645
16646 maybe_set_alignment (cu, die, set_type);
16647
16648 return set_die_type (die, set_type, cu);
16649 }
16650
16651 /* A helper for read_common_block that creates a locexpr baton.
16652 SYM is the symbol which we are marking as computed.
16653 COMMON_DIE is the DIE for the common block.
16654 COMMON_LOC is the location expression attribute for the common
16655 block itself.
16656 MEMBER_LOC is the location expression attribute for the particular
16657 member of the common block that we are processing.
16658 CU is the CU from which the above come. */
16659
16660 static void
16661 mark_common_block_symbol_computed (struct symbol *sym,
16662 struct die_info *common_die,
16663 struct attribute *common_loc,
16664 struct attribute *member_loc,
16665 struct dwarf2_cu *cu)
16666 {
16667 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16668 struct objfile *objfile = per_objfile->objfile;
16669 struct dwarf2_locexpr_baton *baton;
16670 gdb_byte *ptr;
16671 unsigned int cu_off;
16672 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16673 LONGEST offset = 0;
16674
16675 gdb_assert (common_loc && member_loc);
16676 gdb_assert (common_loc->form_is_block ());
16677 gdb_assert (member_loc->form_is_block ()
16678 || member_loc->form_is_constant ());
16679
16680 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16681 baton->per_objfile = per_objfile;
16682 baton->per_cu = cu->per_cu;
16683 gdb_assert (baton->per_cu);
16684
16685 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16686
16687 if (member_loc->form_is_constant ())
16688 {
16689 offset = member_loc->constant_value (0);
16690 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16691 }
16692 else
16693 baton->size += member_loc->as_block ()->size;
16694
16695 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16696 baton->data = ptr;
16697
16698 *ptr++ = DW_OP_call4;
16699 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16700 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16701 ptr += 4;
16702
16703 if (member_loc->form_is_constant ())
16704 {
16705 *ptr++ = DW_OP_addr;
16706 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16707 ptr += cu->header.addr_size;
16708 }
16709 else
16710 {
16711 /* We have to copy the data here, because DW_OP_call4 will only
16712 use a DW_AT_location attribute. */
16713 struct dwarf_block *block = member_loc->as_block ();
16714 memcpy (ptr, block->data, block->size);
16715 ptr += block->size;
16716 }
16717
16718 *ptr++ = DW_OP_plus;
16719 gdb_assert (ptr - baton->data == baton->size);
16720
16721 SYMBOL_LOCATION_BATON (sym) = baton;
16722 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16723 }
16724
16725 /* Create appropriate locally-scoped variables for all the
16726 DW_TAG_common_block entries. Also create a struct common_block
16727 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16728 is used to separate the common blocks name namespace from regular
16729 variable names. */
16730
16731 static void
16732 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16733 {
16734 struct attribute *attr;
16735
16736 attr = dwarf2_attr (die, DW_AT_location, cu);
16737 if (attr != nullptr)
16738 {
16739 /* Support the .debug_loc offsets. */
16740 if (attr->form_is_block ())
16741 {
16742 /* Ok. */
16743 }
16744 else if (attr->form_is_section_offset ())
16745 {
16746 dwarf2_complex_location_expr_complaint ();
16747 attr = NULL;
16748 }
16749 else
16750 {
16751 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16752 "common block member");
16753 attr = NULL;
16754 }
16755 }
16756
16757 if (die->child != NULL)
16758 {
16759 struct objfile *objfile = cu->per_objfile->objfile;
16760 struct die_info *child_die;
16761 size_t n_entries = 0, size;
16762 struct common_block *common_block;
16763 struct symbol *sym;
16764
16765 for (child_die = die->child;
16766 child_die && child_die->tag;
16767 child_die = child_die->sibling)
16768 ++n_entries;
16769
16770 size = (sizeof (struct common_block)
16771 + (n_entries - 1) * sizeof (struct symbol *));
16772 common_block
16773 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16774 size);
16775 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16776 common_block->n_entries = 0;
16777
16778 for (child_die = die->child;
16779 child_die && child_die->tag;
16780 child_die = child_die->sibling)
16781 {
16782 /* Create the symbol in the DW_TAG_common_block block in the current
16783 symbol scope. */
16784 sym = new_symbol (child_die, NULL, cu);
16785 if (sym != NULL)
16786 {
16787 struct attribute *member_loc;
16788
16789 common_block->contents[common_block->n_entries++] = sym;
16790
16791 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16792 cu);
16793 if (member_loc)
16794 {
16795 /* GDB has handled this for a long time, but it is
16796 not specified by DWARF. It seems to have been
16797 emitted by gfortran at least as recently as:
16798 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16799 complaint (_("Variable in common block has "
16800 "DW_AT_data_member_location "
16801 "- DIE at %s [in module %s]"),
16802 sect_offset_str (child_die->sect_off),
16803 objfile_name (objfile));
16804
16805 if (member_loc->form_is_section_offset ())
16806 dwarf2_complex_location_expr_complaint ();
16807 else if (member_loc->form_is_constant ()
16808 || member_loc->form_is_block ())
16809 {
16810 if (attr != nullptr)
16811 mark_common_block_symbol_computed (sym, die, attr,
16812 member_loc, cu);
16813 }
16814 else
16815 dwarf2_complex_location_expr_complaint ();
16816 }
16817 }
16818 }
16819
16820 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16821 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16822 }
16823 }
16824
16825 /* Create a type for a C++ namespace. */
16826
16827 static struct type *
16828 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16829 {
16830 struct objfile *objfile = cu->per_objfile->objfile;
16831 const char *previous_prefix, *name;
16832 int is_anonymous;
16833 struct type *type;
16834
16835 /* For extensions, reuse the type of the original namespace. */
16836 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16837 {
16838 struct die_info *ext_die;
16839 struct dwarf2_cu *ext_cu = cu;
16840
16841 ext_die = dwarf2_extension (die, &ext_cu);
16842 type = read_type_die (ext_die, ext_cu);
16843
16844 /* EXT_CU may not be the same as CU.
16845 Ensure TYPE is recorded with CU in die_type_hash. */
16846 return set_die_type (die, type, cu);
16847 }
16848
16849 name = namespace_name (die, &is_anonymous, cu);
16850
16851 /* Now build the name of the current namespace. */
16852
16853 previous_prefix = determine_prefix (die, cu);
16854 if (previous_prefix[0] != '\0')
16855 name = typename_concat (&objfile->objfile_obstack,
16856 previous_prefix, name, 0, cu);
16857
16858 /* Create the type. */
16859 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16860
16861 return set_die_type (die, type, cu);
16862 }
16863
16864 /* Read a namespace scope. */
16865
16866 static void
16867 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16868 {
16869 struct objfile *objfile = cu->per_objfile->objfile;
16870 int is_anonymous;
16871
16872 /* Add a symbol associated to this if we haven't seen the namespace
16873 before. Also, add a using directive if it's an anonymous
16874 namespace. */
16875
16876 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16877 {
16878 struct type *type;
16879
16880 type = read_type_die (die, cu);
16881 new_symbol (die, type, cu);
16882
16883 namespace_name (die, &is_anonymous, cu);
16884 if (is_anonymous)
16885 {
16886 const char *previous_prefix = determine_prefix (die, cu);
16887
16888 std::vector<const char *> excludes;
16889 add_using_directive (using_directives (cu),
16890 previous_prefix, type->name (), NULL,
16891 NULL, excludes, 0, &objfile->objfile_obstack);
16892 }
16893 }
16894
16895 if (die->child != NULL)
16896 {
16897 struct die_info *child_die = die->child;
16898
16899 while (child_die && child_die->tag)
16900 {
16901 process_die (child_die, cu);
16902 child_die = child_die->sibling;
16903 }
16904 }
16905 }
16906
16907 /* Read a Fortran module as type. This DIE can be only a declaration used for
16908 imported module. Still we need that type as local Fortran "use ... only"
16909 declaration imports depend on the created type in determine_prefix. */
16910
16911 static struct type *
16912 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16913 {
16914 struct objfile *objfile = cu->per_objfile->objfile;
16915 const char *module_name;
16916 struct type *type;
16917
16918 module_name = dwarf2_name (die, cu);
16919 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16920
16921 return set_die_type (die, type, cu);
16922 }
16923
16924 /* Read a Fortran module. */
16925
16926 static void
16927 read_module (struct die_info *die, struct dwarf2_cu *cu)
16928 {
16929 struct die_info *child_die = die->child;
16930 struct type *type;
16931
16932 type = read_type_die (die, cu);
16933 new_symbol (die, type, cu);
16934
16935 while (child_die && child_die->tag)
16936 {
16937 process_die (child_die, cu);
16938 child_die = child_die->sibling;
16939 }
16940 }
16941
16942 /* Return the name of the namespace represented by DIE. Set
16943 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16944 namespace. */
16945
16946 static const char *
16947 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16948 {
16949 struct die_info *current_die;
16950 const char *name = NULL;
16951
16952 /* Loop through the extensions until we find a name. */
16953
16954 for (current_die = die;
16955 current_die != NULL;
16956 current_die = dwarf2_extension (die, &cu))
16957 {
16958 /* We don't use dwarf2_name here so that we can detect the absence
16959 of a name -> anonymous namespace. */
16960 name = dwarf2_string_attr (die, DW_AT_name, cu);
16961
16962 if (name != NULL)
16963 break;
16964 }
16965
16966 /* Is it an anonymous namespace? */
16967
16968 *is_anonymous = (name == NULL);
16969 if (*is_anonymous)
16970 name = CP_ANONYMOUS_NAMESPACE_STR;
16971
16972 return name;
16973 }
16974
16975 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16976 the user defined type vector. */
16977
16978 static struct type *
16979 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16980 {
16981 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16982 struct comp_unit_head *cu_header = &cu->header;
16983 struct type *type;
16984 struct attribute *attr_byte_size;
16985 struct attribute *attr_address_class;
16986 int byte_size, addr_class;
16987 struct type *target_type;
16988
16989 target_type = die_type (die, cu);
16990
16991 /* The die_type call above may have already set the type for this DIE. */
16992 type = get_die_type (die, cu);
16993 if (type)
16994 return type;
16995
16996 type = lookup_pointer_type (target_type);
16997
16998 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16999 if (attr_byte_size)
17000 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17001 else
17002 byte_size = cu_header->addr_size;
17003
17004 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17005 if (attr_address_class)
17006 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17007 else
17008 addr_class = DW_ADDR_none;
17009
17010 ULONGEST alignment = get_alignment (cu, die);
17011
17012 /* If the pointer size, alignment, or address class is different
17013 than the default, create a type variant marked as such and set
17014 the length accordingly. */
17015 if (TYPE_LENGTH (type) != byte_size
17016 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17017 && alignment != TYPE_RAW_ALIGN (type))
17018 || addr_class != DW_ADDR_none)
17019 {
17020 if (gdbarch_address_class_type_flags_p (gdbarch))
17021 {
17022 type_instance_flags type_flags
17023 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17024 addr_class);
17025 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17026 == 0);
17027 type = make_type_with_address_space (type, type_flags);
17028 }
17029 else if (TYPE_LENGTH (type) != byte_size)
17030 {
17031 complaint (_("invalid pointer size %d"), byte_size);
17032 }
17033 else if (TYPE_RAW_ALIGN (type) != alignment)
17034 {
17035 complaint (_("Invalid DW_AT_alignment"
17036 " - DIE at %s [in module %s]"),
17037 sect_offset_str (die->sect_off),
17038 objfile_name (cu->per_objfile->objfile));
17039 }
17040 else
17041 {
17042 /* Should we also complain about unhandled address classes? */
17043 }
17044 }
17045
17046 TYPE_LENGTH (type) = byte_size;
17047 set_type_align (type, alignment);
17048 return set_die_type (die, type, cu);
17049 }
17050
17051 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17052 the user defined type vector. */
17053
17054 static struct type *
17055 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17056 {
17057 struct type *type;
17058 struct type *to_type;
17059 struct type *domain;
17060
17061 to_type = die_type (die, cu);
17062 domain = die_containing_type (die, cu);
17063
17064 /* The calls above may have already set the type for this DIE. */
17065 type = get_die_type (die, cu);
17066 if (type)
17067 return type;
17068
17069 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17070 type = lookup_methodptr_type (to_type);
17071 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17072 {
17073 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17074
17075 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17076 to_type->fields (), to_type->num_fields (),
17077 to_type->has_varargs ());
17078 type = lookup_methodptr_type (new_type);
17079 }
17080 else
17081 type = lookup_memberptr_type (to_type, domain);
17082
17083 return set_die_type (die, type, cu);
17084 }
17085
17086 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17087 the user defined type vector. */
17088
17089 static struct type *
17090 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17091 enum type_code refcode)
17092 {
17093 struct comp_unit_head *cu_header = &cu->header;
17094 struct type *type, *target_type;
17095 struct attribute *attr;
17096
17097 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17098
17099 target_type = die_type (die, cu);
17100
17101 /* The die_type call above may have already set the type for this DIE. */
17102 type = get_die_type (die, cu);
17103 if (type)
17104 return type;
17105
17106 type = lookup_reference_type (target_type, refcode);
17107 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17108 if (attr != nullptr)
17109 {
17110 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17111 }
17112 else
17113 {
17114 TYPE_LENGTH (type) = cu_header->addr_size;
17115 }
17116 maybe_set_alignment (cu, die, type);
17117 return set_die_type (die, type, cu);
17118 }
17119
17120 /* Add the given cv-qualifiers to the element type of the array. GCC
17121 outputs DWARF type qualifiers that apply to an array, not the
17122 element type. But GDB relies on the array element type to carry
17123 the cv-qualifiers. This mimics section 6.7.3 of the C99
17124 specification. */
17125
17126 static struct type *
17127 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17128 struct type *base_type, int cnst, int voltl)
17129 {
17130 struct type *el_type, *inner_array;
17131
17132 base_type = copy_type (base_type);
17133 inner_array = base_type;
17134
17135 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17136 {
17137 TYPE_TARGET_TYPE (inner_array) =
17138 copy_type (TYPE_TARGET_TYPE (inner_array));
17139 inner_array = TYPE_TARGET_TYPE (inner_array);
17140 }
17141
17142 el_type = TYPE_TARGET_TYPE (inner_array);
17143 cnst |= TYPE_CONST (el_type);
17144 voltl |= TYPE_VOLATILE (el_type);
17145 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17146
17147 return set_die_type (die, base_type, cu);
17148 }
17149
17150 static struct type *
17151 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17152 {
17153 struct type *base_type, *cv_type;
17154
17155 base_type = die_type (die, cu);
17156
17157 /* The die_type call above may have already set the type for this DIE. */
17158 cv_type = get_die_type (die, cu);
17159 if (cv_type)
17160 return cv_type;
17161
17162 /* In case the const qualifier is applied to an array type, the element type
17163 is so qualified, not the array type (section 6.7.3 of C99). */
17164 if (base_type->code () == TYPE_CODE_ARRAY)
17165 return add_array_cv_type (die, cu, base_type, 1, 0);
17166
17167 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17168 return set_die_type (die, cv_type, cu);
17169 }
17170
17171 static struct type *
17172 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17173 {
17174 struct type *base_type, *cv_type;
17175
17176 base_type = die_type (die, cu);
17177
17178 /* The die_type call above may have already set the type for this DIE. */
17179 cv_type = get_die_type (die, cu);
17180 if (cv_type)
17181 return cv_type;
17182
17183 /* In case the volatile qualifier is applied to an array type, the
17184 element type is so qualified, not the array type (section 6.7.3
17185 of C99). */
17186 if (base_type->code () == TYPE_CODE_ARRAY)
17187 return add_array_cv_type (die, cu, base_type, 0, 1);
17188
17189 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17190 return set_die_type (die, cv_type, cu);
17191 }
17192
17193 /* Handle DW_TAG_restrict_type. */
17194
17195 static struct type *
17196 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17197 {
17198 struct type *base_type, *cv_type;
17199
17200 base_type = die_type (die, cu);
17201
17202 /* The die_type call above may have already set the type for this DIE. */
17203 cv_type = get_die_type (die, cu);
17204 if (cv_type)
17205 return cv_type;
17206
17207 cv_type = make_restrict_type (base_type);
17208 return set_die_type (die, cv_type, cu);
17209 }
17210
17211 /* Handle DW_TAG_atomic_type. */
17212
17213 static struct type *
17214 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17215 {
17216 struct type *base_type, *cv_type;
17217
17218 base_type = die_type (die, cu);
17219
17220 /* The die_type call above may have already set the type for this DIE. */
17221 cv_type = get_die_type (die, cu);
17222 if (cv_type)
17223 return cv_type;
17224
17225 cv_type = make_atomic_type (base_type);
17226 return set_die_type (die, cv_type, cu);
17227 }
17228
17229 /* Extract all information from a DW_TAG_string_type DIE and add to
17230 the user defined type vector. It isn't really a user defined type,
17231 but it behaves like one, with other DIE's using an AT_user_def_type
17232 attribute to reference it. */
17233
17234 static struct type *
17235 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17236 {
17237 struct objfile *objfile = cu->per_objfile->objfile;
17238 struct gdbarch *gdbarch = objfile->arch ();
17239 struct type *type, *range_type, *index_type, *char_type;
17240 struct attribute *attr;
17241 struct dynamic_prop prop;
17242 bool length_is_constant = true;
17243 LONGEST length;
17244
17245 /* There are a couple of places where bit sizes might be made use of
17246 when parsing a DW_TAG_string_type, however, no producer that we know
17247 of make use of these. Handling bit sizes that are a multiple of the
17248 byte size is easy enough, but what about other bit sizes? Lets deal
17249 with that problem when we have to. Warn about these attributes being
17250 unsupported, then parse the type and ignore them like we always
17251 have. */
17252 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17253 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17254 {
17255 static bool warning_printed = false;
17256 if (!warning_printed)
17257 {
17258 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17259 "currently supported on DW_TAG_string_type."));
17260 warning_printed = true;
17261 }
17262 }
17263
17264 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17265 if (attr != nullptr && !attr->form_is_constant ())
17266 {
17267 /* The string length describes the location at which the length of
17268 the string can be found. The size of the length field can be
17269 specified with one of the attributes below. */
17270 struct type *prop_type;
17271 struct attribute *len
17272 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17273 if (len == nullptr)
17274 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17275 if (len != nullptr && len->form_is_constant ())
17276 {
17277 /* Pass 0 as the default as we know this attribute is constant
17278 and the default value will not be returned. */
17279 LONGEST sz = len->constant_value (0);
17280 prop_type = cu->per_objfile->int_type (sz, true);
17281 }
17282 else
17283 {
17284 /* If the size is not specified then we assume it is the size of
17285 an address on this target. */
17286 prop_type = cu->addr_sized_int_type (true);
17287 }
17288
17289 /* Convert the attribute into a dynamic property. */
17290 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17291 length = 1;
17292 else
17293 length_is_constant = false;
17294 }
17295 else if (attr != nullptr)
17296 {
17297 /* This DW_AT_string_length just contains the length with no
17298 indirection. There's no need to create a dynamic property in this
17299 case. Pass 0 for the default value as we know it will not be
17300 returned in this case. */
17301 length = attr->constant_value (0);
17302 }
17303 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17304 {
17305 /* We don't currently support non-constant byte sizes for strings. */
17306 length = attr->constant_value (1);
17307 }
17308 else
17309 {
17310 /* Use 1 as a fallback length if we have nothing else. */
17311 length = 1;
17312 }
17313
17314 index_type = objfile_type (objfile)->builtin_int;
17315 if (length_is_constant)
17316 range_type = create_static_range_type (NULL, index_type, 1, length);
17317 else
17318 {
17319 struct dynamic_prop low_bound;
17320
17321 low_bound.set_const_val (1);
17322 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17323 }
17324 char_type = language_string_char_type (cu->language_defn, gdbarch);
17325 type = create_string_type (NULL, char_type, range_type);
17326
17327 return set_die_type (die, type, cu);
17328 }
17329
17330 /* Assuming that DIE corresponds to a function, returns nonzero
17331 if the function is prototyped. */
17332
17333 static int
17334 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17335 {
17336 struct attribute *attr;
17337
17338 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17339 if (attr && attr->as_boolean ())
17340 return 1;
17341
17342 /* The DWARF standard implies that the DW_AT_prototyped attribute
17343 is only meaningful for C, but the concept also extends to other
17344 languages that allow unprototyped functions (Eg: Objective C).
17345 For all other languages, assume that functions are always
17346 prototyped. */
17347 if (cu->per_cu->lang != language_c
17348 && cu->per_cu->lang != language_objc
17349 && cu->per_cu->lang != language_opencl)
17350 return 1;
17351
17352 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17353 prototyped and unprototyped functions; default to prototyped,
17354 since that is more common in modern code (and RealView warns
17355 about unprototyped functions). */
17356 if (producer_is_realview (cu->producer))
17357 return 1;
17358
17359 return 0;
17360 }
17361
17362 /* Handle DIES due to C code like:
17363
17364 struct foo
17365 {
17366 int (*funcp)(int a, long l);
17367 int b;
17368 };
17369
17370 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17371
17372 static struct type *
17373 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17374 {
17375 struct objfile *objfile = cu->per_objfile->objfile;
17376 struct type *type; /* Type that this function returns. */
17377 struct type *ftype; /* Function that returns above type. */
17378 struct attribute *attr;
17379
17380 type = die_type (die, cu);
17381
17382 /* The die_type call above may have already set the type for this DIE. */
17383 ftype = get_die_type (die, cu);
17384 if (ftype)
17385 return ftype;
17386
17387 ftype = lookup_function_type (type);
17388
17389 if (prototyped_function_p (die, cu))
17390 ftype->set_is_prototyped (true);
17391
17392 /* Store the calling convention in the type if it's available in
17393 the subroutine die. Otherwise set the calling convention to
17394 the default value DW_CC_normal. */
17395 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17396 if (attr != nullptr
17397 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17398 TYPE_CALLING_CONVENTION (ftype)
17399 = (enum dwarf_calling_convention) attr->constant_value (0);
17400 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17401 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17402 else
17403 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17404
17405 /* Record whether the function returns normally to its caller or not
17406 if the DWARF producer set that information. */
17407 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17408 if (attr && attr->as_boolean ())
17409 TYPE_NO_RETURN (ftype) = 1;
17410
17411 /* We need to add the subroutine type to the die immediately so
17412 we don't infinitely recurse when dealing with parameters
17413 declared as the same subroutine type. */
17414 set_die_type (die, ftype, cu);
17415
17416 if (die->child != NULL)
17417 {
17418 struct type *void_type = objfile_type (objfile)->builtin_void;
17419 struct die_info *child_die;
17420 int nparams, iparams;
17421
17422 /* Count the number of parameters.
17423 FIXME: GDB currently ignores vararg functions, but knows about
17424 vararg member functions. */
17425 nparams = 0;
17426 child_die = die->child;
17427 while (child_die && child_die->tag)
17428 {
17429 if (child_die->tag == DW_TAG_formal_parameter)
17430 nparams++;
17431 else if (child_die->tag == DW_TAG_unspecified_parameters)
17432 ftype->set_has_varargs (true);
17433
17434 child_die = child_die->sibling;
17435 }
17436
17437 /* Allocate storage for parameters and fill them in. */
17438 ftype->set_num_fields (nparams);
17439 ftype->set_fields
17440 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17441
17442 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17443 even if we error out during the parameters reading below. */
17444 for (iparams = 0; iparams < nparams; iparams++)
17445 ftype->field (iparams).set_type (void_type);
17446
17447 iparams = 0;
17448 child_die = die->child;
17449 while (child_die && child_die->tag)
17450 {
17451 if (child_die->tag == DW_TAG_formal_parameter)
17452 {
17453 struct type *arg_type;
17454
17455 /* DWARF version 2 has no clean way to discern C++
17456 static and non-static member functions. G++ helps
17457 GDB by marking the first parameter for non-static
17458 member functions (which is the this pointer) as
17459 artificial. We pass this information to
17460 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17461
17462 DWARF version 3 added DW_AT_object_pointer, which GCC
17463 4.5 does not yet generate. */
17464 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17465 if (attr != nullptr)
17466 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17467 else
17468 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17469 arg_type = die_type (child_die, cu);
17470
17471 /* RealView does not mark THIS as const, which the testsuite
17472 expects. GCC marks THIS as const in method definitions,
17473 but not in the class specifications (GCC PR 43053). */
17474 if (cu->per_cu->lang == language_cplus
17475 && !TYPE_CONST (arg_type)
17476 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17477 {
17478 int is_this = 0;
17479 struct dwarf2_cu *arg_cu = cu;
17480 const char *name = dwarf2_name (child_die, cu);
17481
17482 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17483 if (attr != nullptr)
17484 {
17485 /* If the compiler emits this, use it. */
17486 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17487 is_this = 1;
17488 }
17489 else if (name && strcmp (name, "this") == 0)
17490 /* Function definitions will have the argument names. */
17491 is_this = 1;
17492 else if (name == NULL && iparams == 0)
17493 /* Declarations may not have the names, so like
17494 elsewhere in GDB, assume an artificial first
17495 argument is "this". */
17496 is_this = 1;
17497
17498 if (is_this)
17499 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17500 arg_type, 0);
17501 }
17502
17503 ftype->field (iparams).set_type (arg_type);
17504 iparams++;
17505 }
17506 child_die = child_die->sibling;
17507 }
17508 }
17509
17510 return ftype;
17511 }
17512
17513 static struct type *
17514 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17515 {
17516 struct objfile *objfile = cu->per_objfile->objfile;
17517 const char *name = NULL;
17518 struct type *this_type, *target_type;
17519
17520 name = dwarf2_full_name (NULL, die, cu);
17521 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17522 this_type->set_target_is_stub (true);
17523 set_die_type (die, this_type, cu);
17524 target_type = die_type (die, cu);
17525 if (target_type != this_type)
17526 TYPE_TARGET_TYPE (this_type) = target_type;
17527 else
17528 {
17529 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17530 spec and cause infinite loops in GDB. */
17531 complaint (_("Self-referential DW_TAG_typedef "
17532 "- DIE at %s [in module %s]"),
17533 sect_offset_str (die->sect_off), objfile_name (objfile));
17534 TYPE_TARGET_TYPE (this_type) = NULL;
17535 }
17536 if (name == NULL)
17537 {
17538 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17539 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17540 Handle these by just returning the target type, rather than
17541 constructing an anonymous typedef type and trying to handle this
17542 elsewhere. */
17543 set_die_type (die, target_type, cu);
17544 return target_type;
17545 }
17546 return this_type;
17547 }
17548
17549 /* Helper for get_dwarf2_rational_constant that computes the value of
17550 a given gmp_mpz given an attribute. */
17551
17552 static void
17553 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17554 {
17555 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17556 location expression that pushes an implicit value. */
17557 if (attr->form == DW_FORM_exprloc)
17558 {
17559 dwarf_block *blk = attr->as_block ();
17560 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17561 {
17562 uint64_t len;
17563 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17564 blk->data + blk->size,
17565 &len);
17566 if (ptr - blk->data + len <= blk->size)
17567 {
17568 mpz_import (value->val, len,
17569 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17570 1, 0, 0, ptr);
17571 return;
17572 }
17573 }
17574
17575 /* On failure set it to 1. */
17576 *value = gdb_mpz (1);
17577 }
17578 else if (attr->form_is_block ())
17579 {
17580 dwarf_block *blk = attr->as_block ();
17581 mpz_import (value->val, blk->size,
17582 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17583 1, 0, 0, blk->data);
17584 }
17585 else
17586 *value = gdb_mpz (attr->constant_value (1));
17587 }
17588
17589 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17590 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17591
17592 If the numerator and/or numerator attribute is missing,
17593 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17594 untouched. */
17595
17596 static void
17597 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17598 gdb_mpz *numerator, gdb_mpz *denominator)
17599 {
17600 struct attribute *num_attr, *denom_attr;
17601
17602 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17603 if (num_attr == nullptr)
17604 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17605 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17606
17607 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17608 if (denom_attr == nullptr)
17609 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17610 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17611
17612 if (num_attr == nullptr || denom_attr == nullptr)
17613 return;
17614
17615 get_mpz (cu, numerator, num_attr);
17616 get_mpz (cu, denominator, denom_attr);
17617 }
17618
17619 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17620 rational constant, rather than a signed one.
17621
17622 If the rational constant has a negative value, a complaint
17623 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17624
17625 static void
17626 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17627 struct dwarf2_cu *cu,
17628 gdb_mpz *numerator,
17629 gdb_mpz *denominator)
17630 {
17631 gdb_mpz num (1);
17632 gdb_mpz denom (1);
17633
17634 get_dwarf2_rational_constant (die, cu, &num, &denom);
17635 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17636 {
17637 mpz_neg (num.val, num.val);
17638 mpz_neg (denom.val, denom.val);
17639 }
17640 else if (mpz_sgn (num.val) == -1)
17641 {
17642 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17643 " in DIE at %s"),
17644 sect_offset_str (die->sect_off));
17645 return;
17646 }
17647 else if (mpz_sgn (denom.val) == -1)
17648 {
17649 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17650 " in DIE at %s"),
17651 sect_offset_str (die->sect_off));
17652 return;
17653 }
17654
17655 *numerator = std::move (num);
17656 *denominator = std::move (denom);
17657 }
17658
17659 /* Assuming that ENCODING is a string whose contents starting at the
17660 K'th character is "_nn" where "nn" is a decimal number, scan that
17661 number and set RESULT to the value. K is updated to point to the
17662 character immediately following the number.
17663
17664 If the string does not conform to the format described above, false
17665 is returned, and K may or may not be changed. */
17666
17667 static bool
17668 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17669 {
17670 /* The next character should be an underscore ('_') followed
17671 by a digit. */
17672 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17673 return false;
17674
17675 /* Skip the underscore. */
17676 k++;
17677 int start = k;
17678
17679 /* Determine the number of digits for our number. */
17680 while (isdigit (encoding[k]))
17681 k++;
17682 if (k == start)
17683 return false;
17684
17685 std::string copy (&encoding[start], k - start);
17686 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17687 return false;
17688
17689 return true;
17690 }
17691
17692 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17693 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17694 DENOM, update OFFSET, and return true on success. Return false on
17695 failure. */
17696
17697 static bool
17698 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17699 gdb_mpz *num, gdb_mpz *denom)
17700 {
17701 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17702 return false;
17703 return ada_get_gnat_encoded_number (encoding, offset, denom);
17704 }
17705
17706 /* Assuming DIE corresponds to a fixed point type, finish the creation
17707 of the corresponding TYPE by setting its type-specific data. CU is
17708 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17709 encodings. It is nullptr if the GNAT encoding should be
17710 ignored. */
17711
17712 static void
17713 finish_fixed_point_type (struct type *type, const char *suffix,
17714 struct die_info *die, struct dwarf2_cu *cu)
17715 {
17716 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17717 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17718
17719 /* If GNAT encodings are preferred, don't examine the
17720 attributes. */
17721 struct attribute *attr = nullptr;
17722 if (suffix == nullptr)
17723 {
17724 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17725 if (attr == nullptr)
17726 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17727 if (attr == nullptr)
17728 attr = dwarf2_attr (die, DW_AT_small, cu);
17729 }
17730
17731 /* Numerator and denominator of our fixed-point type's scaling factor.
17732 The default is a scaling factor of 1, which we use as a fallback
17733 when we are not able to decode it (problem with the debugging info,
17734 unsupported forms, bug in GDB, etc...). Using that as the default
17735 allows us to at least print the unscaled value, which might still
17736 be useful to a user. */
17737 gdb_mpz scale_num (1);
17738 gdb_mpz scale_denom (1);
17739
17740 if (attr == nullptr)
17741 {
17742 int offset = 0;
17743 if (suffix != nullptr
17744 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17745 &scale_denom)
17746 /* The number might be encoded as _nn_dd_nn_dd, where the
17747 second ratio is the 'small value. In this situation, we
17748 want the second value. */
17749 && (suffix[offset] != '_'
17750 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17751 &scale_denom)))
17752 {
17753 /* Found it. */
17754 }
17755 else
17756 {
17757 /* Scaling factor not found. Assume a scaling factor of 1,
17758 and hope for the best. At least the user will be able to
17759 see the encoded value. */
17760 scale_num = 1;
17761 scale_denom = 1;
17762 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17763 sect_offset_str (die->sect_off));
17764 }
17765 }
17766 else if (attr->name == DW_AT_binary_scale)
17767 {
17768 LONGEST scale_exp = attr->constant_value (0);
17769 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17770
17771 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17772 }
17773 else if (attr->name == DW_AT_decimal_scale)
17774 {
17775 LONGEST scale_exp = attr->constant_value (0);
17776 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17777
17778 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17779 }
17780 else if (attr->name == DW_AT_small)
17781 {
17782 struct die_info *scale_die;
17783 struct dwarf2_cu *scale_cu = cu;
17784
17785 scale_die = follow_die_ref (die, attr, &scale_cu);
17786 if (scale_die->tag == DW_TAG_constant)
17787 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17788 &scale_num, &scale_denom);
17789 else
17790 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17791 " (DIE at %s)"),
17792 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17793 }
17794 else
17795 {
17796 complaint (_("unsupported scale attribute %s for fixed-point type"
17797 " (DIE at %s)"),
17798 dwarf_attr_name (attr->name),
17799 sect_offset_str (die->sect_off));
17800 }
17801
17802 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17803 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17804 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17805 mpq_canonicalize (scaling_factor.val);
17806 }
17807
17808 /* The gnat-encoding suffix for fixed point. */
17809
17810 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17811
17812 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17813 "XF" suffix of the name. The text after this is what encodes the
17814 'small and 'delta information. Otherwise, return nullptr. */
17815
17816 static const char *
17817 gnat_encoded_fixed_point_type_info (const char *name)
17818 {
17819 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17820 }
17821
17822 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17823 (which may be different from NAME) to the architecture back-end to allow
17824 it to guess the correct format if necessary. */
17825
17826 static struct type *
17827 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17828 const char *name_hint, enum bfd_endian byte_order)
17829 {
17830 struct gdbarch *gdbarch = objfile->arch ();
17831 const struct floatformat **format;
17832 struct type *type;
17833
17834 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17835 if (format)
17836 type = init_float_type (objfile, bits, name, format, byte_order);
17837 else
17838 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17839
17840 return type;
17841 }
17842
17843 /* Allocate an integer type of size BITS and name NAME. */
17844
17845 static struct type *
17846 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17847 int bits, int unsigned_p, const char *name)
17848 {
17849 struct type *type;
17850
17851 /* Versions of Intel's C Compiler generate an integer type called "void"
17852 instead of using DW_TAG_unspecified_type. This has been seen on
17853 at least versions 14, 17, and 18. */
17854 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17855 && strcmp (name, "void") == 0)
17856 type = objfile_type (objfile)->builtin_void;
17857 else
17858 type = init_integer_type (objfile, bits, unsigned_p, name);
17859
17860 return type;
17861 }
17862
17863 /* Return true if DIE has a DW_AT_small attribute whose value is
17864 a constant rational, where both the numerator and denominator
17865 are equal to zero.
17866
17867 CU is the DIE's Compilation Unit. */
17868
17869 static bool
17870 has_zero_over_zero_small_attribute (struct die_info *die,
17871 struct dwarf2_cu *cu)
17872 {
17873 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17874 if (attr == nullptr)
17875 return false;
17876
17877 struct dwarf2_cu *scale_cu = cu;
17878 struct die_info *scale_die
17879 = follow_die_ref (die, attr, &scale_cu);
17880
17881 if (scale_die->tag != DW_TAG_constant)
17882 return false;
17883
17884 gdb_mpz num (1), denom (1);
17885 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
17886 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
17887 }
17888
17889 /* Initialise and return a floating point type of size BITS suitable for
17890 use as a component of a complex number. The NAME_HINT is passed through
17891 when initialising the floating point type and is the name of the complex
17892 type.
17893
17894 As DWARF doesn't currently provide an explicit name for the components
17895 of a complex number, but it can be helpful to have these components
17896 named, we try to select a suitable name based on the size of the
17897 component. */
17898 static struct type *
17899 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17900 struct objfile *objfile,
17901 int bits, const char *name_hint,
17902 enum bfd_endian byte_order)
17903 {
17904 gdbarch *gdbarch = objfile->arch ();
17905 struct type *tt = nullptr;
17906
17907 /* Try to find a suitable floating point builtin type of size BITS.
17908 We're going to use the name of this type as the name for the complex
17909 target type that we are about to create. */
17910 switch (cu->per_cu->lang)
17911 {
17912 case language_fortran:
17913 switch (bits)
17914 {
17915 case 32:
17916 tt = builtin_f_type (gdbarch)->builtin_real;
17917 break;
17918 case 64:
17919 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17920 break;
17921 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17922 case 128:
17923 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17924 break;
17925 }
17926 break;
17927 default:
17928 switch (bits)
17929 {
17930 case 32:
17931 tt = builtin_type (gdbarch)->builtin_float;
17932 break;
17933 case 64:
17934 tt = builtin_type (gdbarch)->builtin_double;
17935 break;
17936 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17937 case 128:
17938 tt = builtin_type (gdbarch)->builtin_long_double;
17939 break;
17940 }
17941 break;
17942 }
17943
17944 /* If the type we found doesn't match the size we were looking for, then
17945 pretend we didn't find a type at all, the complex target type we
17946 create will then be nameless. */
17947 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17948 tt = nullptr;
17949
17950 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17951 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17952 }
17953
17954 /* Find a representation of a given base type and install
17955 it in the TYPE field of the die. */
17956
17957 static struct type *
17958 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17959 {
17960 struct objfile *objfile = cu->per_objfile->objfile;
17961 struct type *type;
17962 struct attribute *attr;
17963 int encoding = 0, bits = 0;
17964 const char *name;
17965 gdbarch *arch;
17966
17967 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17968 if (attr != nullptr && attr->form_is_constant ())
17969 encoding = attr->constant_value (0);
17970 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17971 if (attr != nullptr)
17972 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17973 name = dwarf2_name (die, cu);
17974 if (!name)
17975 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17976
17977 arch = objfile->arch ();
17978 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17979
17980 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17981 if (attr != nullptr && attr->form_is_constant ())
17982 {
17983 int endianity = attr->constant_value (0);
17984
17985 switch (endianity)
17986 {
17987 case DW_END_big:
17988 byte_order = BFD_ENDIAN_BIG;
17989 break;
17990 case DW_END_little:
17991 byte_order = BFD_ENDIAN_LITTLE;
17992 break;
17993 default:
17994 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17995 break;
17996 }
17997 }
17998
17999 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18000 && cu->per_cu->lang == language_ada
18001 && has_zero_over_zero_small_attribute (die, cu))
18002 {
18003 /* brobecker/2018-02-24: This is a fixed point type for which
18004 the scaling factor is represented as fraction whose value
18005 does not make sense (zero divided by zero), so we should
18006 normally never see these. However, there is a small category
18007 of fixed point types for which GNAT is unable to provide
18008 the scaling factor via the standard DWARF mechanisms, and
18009 for which the info is provided via the GNAT encodings instead.
18010 This is likely what this DIE is about. */
18011 encoding = (encoding == DW_ATE_signed_fixed
18012 ? DW_ATE_signed
18013 : DW_ATE_unsigned);
18014 }
18015
18016 /* With GNAT encodings, fixed-point information will be encoded in
18017 the type name. Note that this can also occur with the above
18018 zero-over-zero case, which is why this is a separate "if" rather
18019 than an "else if". */
18020 const char *gnat_encoding_suffix = nullptr;
18021 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18022 && cu->per_cu->lang == language_ada
18023 && name != nullptr)
18024 {
18025 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18026 if (gnat_encoding_suffix != nullptr)
18027 {
18028 gdb_assert (startswith (gnat_encoding_suffix,
18029 GNAT_FIXED_POINT_SUFFIX));
18030 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18031 name, gnat_encoding_suffix - name);
18032 /* Use -1 here so that SUFFIX points at the "_" after the
18033 "XF". */
18034 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18035
18036 encoding = (encoding == DW_ATE_signed
18037 ? DW_ATE_signed_fixed
18038 : DW_ATE_unsigned_fixed);
18039 }
18040 }
18041
18042 switch (encoding)
18043 {
18044 case DW_ATE_address:
18045 /* Turn DW_ATE_address into a void * pointer. */
18046 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18047 type = init_pointer_type (objfile, bits, name, type);
18048 break;
18049 case DW_ATE_boolean:
18050 type = init_boolean_type (objfile, bits, 1, name);
18051 break;
18052 case DW_ATE_complex_float:
18053 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18054 byte_order);
18055 if (type->code () == TYPE_CODE_ERROR)
18056 {
18057 if (name == nullptr)
18058 {
18059 struct obstack *obstack
18060 = &cu->per_objfile->objfile->objfile_obstack;
18061 name = obconcat (obstack, "_Complex ", type->name (),
18062 nullptr);
18063 }
18064 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18065 }
18066 else
18067 type = init_complex_type (name, type);
18068 break;
18069 case DW_ATE_decimal_float:
18070 type = init_decfloat_type (objfile, bits, name);
18071 break;
18072 case DW_ATE_float:
18073 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18074 break;
18075 case DW_ATE_signed:
18076 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18077 break;
18078 case DW_ATE_unsigned:
18079 if (cu->per_cu->lang == language_fortran
18080 && name
18081 && startswith (name, "character("))
18082 type = init_character_type (objfile, bits, 1, name);
18083 else
18084 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18085 break;
18086 case DW_ATE_signed_char:
18087 if (cu->per_cu->lang == language_ada
18088 || cu->per_cu->lang == language_m2
18089 || cu->per_cu->lang == language_pascal
18090 || cu->per_cu->lang == language_fortran)
18091 type = init_character_type (objfile, bits, 0, name);
18092 else
18093 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18094 break;
18095 case DW_ATE_unsigned_char:
18096 if (cu->per_cu->lang == language_ada
18097 || cu->per_cu->lang == language_m2
18098 || cu->per_cu->lang == language_pascal
18099 || cu->per_cu->lang == language_fortran
18100 || cu->per_cu->lang == language_rust)
18101 type = init_character_type (objfile, bits, 1, name);
18102 else
18103 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18104 break;
18105 case DW_ATE_UTF:
18106 {
18107 if (bits == 16)
18108 type = builtin_type (arch)->builtin_char16;
18109 else if (bits == 32)
18110 type = builtin_type (arch)->builtin_char32;
18111 else
18112 {
18113 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18114 bits);
18115 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18116 }
18117 return set_die_type (die, type, cu);
18118 }
18119 break;
18120 case DW_ATE_signed_fixed:
18121 type = init_fixed_point_type (objfile, bits, 0, name);
18122 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18123 break;
18124 case DW_ATE_unsigned_fixed:
18125 type = init_fixed_point_type (objfile, bits, 1, name);
18126 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18127 break;
18128
18129 default:
18130 complaint (_("unsupported DW_AT_encoding: '%s'"),
18131 dwarf_type_encoding_name (encoding));
18132 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18133 break;
18134 }
18135
18136 if (name && strcmp (name, "char") == 0)
18137 type->set_has_no_signedness (true);
18138
18139 maybe_set_alignment (cu, die, type);
18140
18141 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18142
18143 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18144 {
18145 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18146 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18147 {
18148 unsigned real_bit_size = attr->as_unsigned ();
18149 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18150 /* Only use the attributes if they make sense together. */
18151 if (attr == nullptr
18152 || (attr->as_unsigned () + real_bit_size
18153 <= 8 * TYPE_LENGTH (type)))
18154 {
18155 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18156 = real_bit_size;
18157 if (attr != nullptr)
18158 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18159 = attr->as_unsigned ();
18160 }
18161 }
18162 }
18163
18164 return set_die_type (die, type, cu);
18165 }
18166
18167 /* A helper function that returns the name of DIE, if it refers to a
18168 variable declaration. */
18169
18170 static const char *
18171 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18172 {
18173 if (die->tag != DW_TAG_variable)
18174 return nullptr;
18175
18176 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18177 if (attr == nullptr || !attr->as_boolean ())
18178 return nullptr;
18179
18180 attr = dwarf2_attr (die, DW_AT_name, cu);
18181 if (attr == nullptr)
18182 return nullptr;
18183 return attr->as_string ();
18184 }
18185
18186 /* Parse dwarf attribute if it's a block, reference or constant and put the
18187 resulting value of the attribute into struct bound_prop.
18188 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18189
18190 static int
18191 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18192 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18193 struct type *default_type)
18194 {
18195 struct dwarf2_property_baton *baton;
18196 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18197 struct objfile *objfile = per_objfile->objfile;
18198 struct obstack *obstack = &objfile->objfile_obstack;
18199
18200 gdb_assert (default_type != NULL);
18201
18202 if (attr == NULL || prop == NULL)
18203 return 0;
18204
18205 if (attr->form_is_block ())
18206 {
18207 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18208 baton->property_type = default_type;
18209 baton->locexpr.per_cu = cu->per_cu;
18210 baton->locexpr.per_objfile = per_objfile;
18211
18212 struct dwarf_block *block = attr->as_block ();
18213 baton->locexpr.size = block->size;
18214 baton->locexpr.data = block->data;
18215 switch (attr->name)
18216 {
18217 case DW_AT_string_length:
18218 baton->locexpr.is_reference = true;
18219 break;
18220 default:
18221 baton->locexpr.is_reference = false;
18222 break;
18223 }
18224
18225 prop->set_locexpr (baton);
18226 gdb_assert (prop->baton () != NULL);
18227 }
18228 else if (attr->form_is_ref ())
18229 {
18230 struct dwarf2_cu *target_cu = cu;
18231 struct die_info *target_die;
18232 struct attribute *target_attr;
18233
18234 target_die = follow_die_ref (die, attr, &target_cu);
18235 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18236 if (target_attr == NULL)
18237 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18238 target_cu);
18239 if (target_attr == NULL)
18240 {
18241 const char *name = var_decl_name (target_die, target_cu);
18242 if (name != nullptr)
18243 {
18244 prop->set_variable_name (name);
18245 return 1;
18246 }
18247 return 0;
18248 }
18249
18250 switch (target_attr->name)
18251 {
18252 case DW_AT_location:
18253 if (target_attr->form_is_section_offset ())
18254 {
18255 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18256 baton->property_type = die_type (target_die, target_cu);
18257 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18258 prop->set_loclist (baton);
18259 gdb_assert (prop->baton () != NULL);
18260 }
18261 else if (target_attr->form_is_block ())
18262 {
18263 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18264 baton->property_type = die_type (target_die, target_cu);
18265 baton->locexpr.per_cu = cu->per_cu;
18266 baton->locexpr.per_objfile = per_objfile;
18267 struct dwarf_block *block = target_attr->as_block ();
18268 baton->locexpr.size = block->size;
18269 baton->locexpr.data = block->data;
18270 baton->locexpr.is_reference = true;
18271 prop->set_locexpr (baton);
18272 gdb_assert (prop->baton () != NULL);
18273 }
18274 else
18275 {
18276 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18277 "dynamic property");
18278 return 0;
18279 }
18280 break;
18281 case DW_AT_data_member_location:
18282 {
18283 LONGEST offset;
18284
18285 if (!handle_data_member_location (target_die, target_cu,
18286 &offset))
18287 return 0;
18288
18289 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18290 baton->property_type = read_type_die (target_die->parent,
18291 target_cu);
18292 baton->offset_info.offset = offset;
18293 baton->offset_info.type = die_type (target_die, target_cu);
18294 prop->set_addr_offset (baton);
18295 break;
18296 }
18297 }
18298 }
18299 else if (attr->form_is_constant ())
18300 prop->set_const_val (attr->constant_value (0));
18301 else
18302 {
18303 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18304 dwarf2_name (die, cu));
18305 return 0;
18306 }
18307
18308 return 1;
18309 }
18310
18311 /* See read.h. */
18312
18313 struct type *
18314 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18315 {
18316 struct type *int_type;
18317
18318 /* Helper macro to examine the various builtin types. */
18319 #define TRY_TYPE(F) \
18320 int_type = (unsigned_p \
18321 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18322 : objfile_type (objfile)->builtin_ ## F); \
18323 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18324 return int_type
18325
18326 TRY_TYPE (char);
18327 TRY_TYPE (short);
18328 TRY_TYPE (int);
18329 TRY_TYPE (long);
18330 TRY_TYPE (long_long);
18331
18332 #undef TRY_TYPE
18333
18334 gdb_assert_not_reached ("unable to find suitable integer type");
18335 }
18336
18337 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18338 present (which is valid) then compute the default type based on the
18339 compilation units address size. */
18340
18341 static struct type *
18342 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18343 {
18344 struct type *index_type = die_type (die, cu);
18345
18346 /* Dwarf-2 specifications explicitly allows to create subrange types
18347 without specifying a base type.
18348 In that case, the base type must be set to the type of
18349 the lower bound, upper bound or count, in that order, if any of these
18350 three attributes references an object that has a type.
18351 If no base type is found, the Dwarf-2 specifications say that
18352 a signed integer type of size equal to the size of an address should
18353 be used.
18354 For the following C code: `extern char gdb_int [];'
18355 GCC produces an empty range DIE.
18356 FIXME: muller/2010-05-28: Possible references to object for low bound,
18357 high bound or count are not yet handled by this code. */
18358 if (index_type->code () == TYPE_CODE_VOID)
18359 index_type = cu->addr_sized_int_type (false);
18360
18361 return index_type;
18362 }
18363
18364 /* Read the given DW_AT_subrange DIE. */
18365
18366 static struct type *
18367 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18368 {
18369 struct type *base_type, *orig_base_type;
18370 struct type *range_type;
18371 struct attribute *attr;
18372 struct dynamic_prop low, high;
18373 int low_default_is_valid;
18374 int high_bound_is_count = 0;
18375 const char *name;
18376 ULONGEST negative_mask;
18377
18378 orig_base_type = read_subrange_index_type (die, cu);
18379
18380 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18381 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18382 creating the range type, but we use the result of check_typedef
18383 when examining properties of the type. */
18384 base_type = check_typedef (orig_base_type);
18385
18386 /* The die_type call above may have already set the type for this DIE. */
18387 range_type = get_die_type (die, cu);
18388 if (range_type)
18389 return range_type;
18390
18391 high.set_const_val (0);
18392
18393 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18394 omitting DW_AT_lower_bound. */
18395 switch (cu->per_cu->lang)
18396 {
18397 case language_c:
18398 case language_cplus:
18399 low.set_const_val (0);
18400 low_default_is_valid = 1;
18401 break;
18402 case language_fortran:
18403 low.set_const_val (1);
18404 low_default_is_valid = 1;
18405 break;
18406 case language_d:
18407 case language_objc:
18408 case language_rust:
18409 low.set_const_val (0);
18410 low_default_is_valid = (cu->header.version >= 4);
18411 break;
18412 case language_ada:
18413 case language_m2:
18414 case language_pascal:
18415 low.set_const_val (1);
18416 low_default_is_valid = (cu->header.version >= 4);
18417 break;
18418 default:
18419 low.set_const_val (0);
18420 low_default_is_valid = 0;
18421 break;
18422 }
18423
18424 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18425 if (attr != nullptr)
18426 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18427 else if (!low_default_is_valid)
18428 complaint (_("Missing DW_AT_lower_bound "
18429 "- DIE at %s [in module %s]"),
18430 sect_offset_str (die->sect_off),
18431 objfile_name (cu->per_objfile->objfile));
18432
18433 struct attribute *attr_ub, *attr_count;
18434 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18435 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18436 {
18437 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18438 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18439 {
18440 /* If bounds are constant do the final calculation here. */
18441 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18442 high.set_const_val (low.const_val () + high.const_val () - 1);
18443 else
18444 high_bound_is_count = 1;
18445 }
18446 else
18447 {
18448 if (attr_ub != NULL)
18449 complaint (_("Unresolved DW_AT_upper_bound "
18450 "- DIE at %s [in module %s]"),
18451 sect_offset_str (die->sect_off),
18452 objfile_name (cu->per_objfile->objfile));
18453 if (attr_count != NULL)
18454 complaint (_("Unresolved DW_AT_count "
18455 "- DIE at %s [in module %s]"),
18456 sect_offset_str (die->sect_off),
18457 objfile_name (cu->per_objfile->objfile));
18458 }
18459 }
18460
18461 LONGEST bias = 0;
18462 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18463 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18464 bias = bias_attr->constant_value (0);
18465
18466 /* Normally, the DWARF producers are expected to use a signed
18467 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18468 But this is unfortunately not always the case, as witnessed
18469 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18470 is used instead. To work around that ambiguity, we treat
18471 the bounds as signed, and thus sign-extend their values, when
18472 the base type is signed. */
18473 negative_mask =
18474 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18475 if (low.kind () == PROP_CONST
18476 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18477 low.set_const_val (low.const_val () | negative_mask);
18478 if (high.kind () == PROP_CONST
18479 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18480 high.set_const_val (high.const_val () | negative_mask);
18481
18482 /* Check for bit and byte strides. */
18483 struct dynamic_prop byte_stride_prop;
18484 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18485 if (attr_byte_stride != nullptr)
18486 {
18487 struct type *prop_type = cu->addr_sized_int_type (false);
18488 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18489 prop_type);
18490 }
18491
18492 struct dynamic_prop bit_stride_prop;
18493 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18494 if (attr_bit_stride != nullptr)
18495 {
18496 /* It only makes sense to have either a bit or byte stride. */
18497 if (attr_byte_stride != nullptr)
18498 {
18499 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18500 "- DIE at %s [in module %s]"),
18501 sect_offset_str (die->sect_off),
18502 objfile_name (cu->per_objfile->objfile));
18503 attr_bit_stride = nullptr;
18504 }
18505 else
18506 {
18507 struct type *prop_type = cu->addr_sized_int_type (false);
18508 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18509 prop_type);
18510 }
18511 }
18512
18513 if (attr_byte_stride != nullptr
18514 || attr_bit_stride != nullptr)
18515 {
18516 bool byte_stride_p = (attr_byte_stride != nullptr);
18517 struct dynamic_prop *stride
18518 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18519
18520 range_type
18521 = create_range_type_with_stride (NULL, orig_base_type, &low,
18522 &high, bias, stride, byte_stride_p);
18523 }
18524 else
18525 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18526
18527 if (high_bound_is_count)
18528 range_type->bounds ()->flag_upper_bound_is_count = 1;
18529
18530 /* Ada expects an empty array on no boundary attributes. */
18531 if (attr == NULL && cu->per_cu->lang != language_ada)
18532 range_type->bounds ()->high.set_undefined ();
18533
18534 name = dwarf2_name (die, cu);
18535 if (name)
18536 range_type->set_name (name);
18537
18538 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18539 if (attr != nullptr)
18540 TYPE_LENGTH (range_type) = attr->constant_value (0);
18541
18542 maybe_set_alignment (cu, die, range_type);
18543
18544 set_die_type (die, range_type, cu);
18545
18546 /* set_die_type should be already done. */
18547 set_descriptive_type (range_type, die, cu);
18548
18549 return range_type;
18550 }
18551
18552 static struct type *
18553 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18554 {
18555 struct type *type;
18556
18557 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18558 type->set_name (dwarf2_name (die, cu));
18559
18560 /* In Ada, an unspecified type is typically used when the description
18561 of the type is deferred to a different unit. When encountering
18562 such a type, we treat it as a stub, and try to resolve it later on,
18563 when needed. */
18564 if (cu->per_cu->lang == language_ada)
18565 type->set_is_stub (true);
18566
18567 return set_die_type (die, type, cu);
18568 }
18569
18570 /* Read a single die and all its descendents. Set the die's sibling
18571 field to NULL; set other fields in the die correctly, and set all
18572 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18573 location of the info_ptr after reading all of those dies. PARENT
18574 is the parent of the die in question. */
18575
18576 static struct die_info *
18577 read_die_and_children (const struct die_reader_specs *reader,
18578 const gdb_byte *info_ptr,
18579 const gdb_byte **new_info_ptr,
18580 struct die_info *parent)
18581 {
18582 struct die_info *die;
18583 const gdb_byte *cur_ptr;
18584
18585 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18586 if (die == NULL)
18587 {
18588 *new_info_ptr = cur_ptr;
18589 return NULL;
18590 }
18591 store_in_ref_table (die, reader->cu);
18592
18593 if (die->has_children)
18594 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18595 else
18596 {
18597 die->child = NULL;
18598 *new_info_ptr = cur_ptr;
18599 }
18600
18601 die->sibling = NULL;
18602 die->parent = parent;
18603 return die;
18604 }
18605
18606 /* Read a die, all of its descendents, and all of its siblings; set
18607 all of the fields of all of the dies correctly. Arguments are as
18608 in read_die_and_children. */
18609
18610 static struct die_info *
18611 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18612 const gdb_byte *info_ptr,
18613 const gdb_byte **new_info_ptr,
18614 struct die_info *parent)
18615 {
18616 struct die_info *first_die, *last_sibling;
18617 const gdb_byte *cur_ptr;
18618
18619 cur_ptr = info_ptr;
18620 first_die = last_sibling = NULL;
18621
18622 while (1)
18623 {
18624 struct die_info *die
18625 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18626
18627 if (die == NULL)
18628 {
18629 *new_info_ptr = cur_ptr;
18630 return first_die;
18631 }
18632
18633 if (!first_die)
18634 first_die = die;
18635 else
18636 last_sibling->sibling = die;
18637
18638 last_sibling = die;
18639 }
18640 }
18641
18642 /* Read a die, all of its descendents, and all of its siblings; set
18643 all of the fields of all of the dies correctly. Arguments are as
18644 in read_die_and_children.
18645 This the main entry point for reading a DIE and all its children. */
18646
18647 static struct die_info *
18648 read_die_and_siblings (const struct die_reader_specs *reader,
18649 const gdb_byte *info_ptr,
18650 const gdb_byte **new_info_ptr,
18651 struct die_info *parent)
18652 {
18653 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18654 new_info_ptr, parent);
18655
18656 if (dwarf_die_debug)
18657 {
18658 fprintf_unfiltered (gdb_stdlog,
18659 "Read die from %s@0x%x of %s:\n",
18660 reader->die_section->get_name (),
18661 (unsigned) (info_ptr - reader->die_section->buffer),
18662 bfd_get_filename (reader->abfd));
18663 dump_die (die, dwarf_die_debug);
18664 }
18665
18666 return die;
18667 }
18668
18669 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18670 attributes.
18671 The caller is responsible for filling in the extra attributes
18672 and updating (*DIEP)->num_attrs.
18673 Set DIEP to point to a newly allocated die with its information,
18674 except for its child, sibling, and parent fields. */
18675
18676 static const gdb_byte *
18677 read_full_die_1 (const struct die_reader_specs *reader,
18678 struct die_info **diep, const gdb_byte *info_ptr,
18679 int num_extra_attrs)
18680 {
18681 unsigned int abbrev_number, bytes_read, i;
18682 const struct abbrev_info *abbrev;
18683 struct die_info *die;
18684 struct dwarf2_cu *cu = reader->cu;
18685 bfd *abfd = reader->abfd;
18686
18687 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18688 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18689 info_ptr += bytes_read;
18690 if (!abbrev_number)
18691 {
18692 *diep = NULL;
18693 return info_ptr;
18694 }
18695
18696 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18697 if (!abbrev)
18698 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18699 abbrev_number,
18700 bfd_get_filename (abfd));
18701
18702 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18703 die->sect_off = sect_off;
18704 die->tag = abbrev->tag;
18705 die->abbrev = abbrev_number;
18706 die->has_children = abbrev->has_children;
18707
18708 /* Make the result usable.
18709 The caller needs to update num_attrs after adding the extra
18710 attributes. */
18711 die->num_attrs = abbrev->num_attrs;
18712
18713 bool any_need_reprocess = false;
18714 for (i = 0; i < abbrev->num_attrs; ++i)
18715 {
18716 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18717 info_ptr);
18718 if (die->attrs[i].requires_reprocessing_p ())
18719 any_need_reprocess = true;
18720 }
18721
18722 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18723 if (attr != nullptr && attr->form_is_unsigned ())
18724 cu->str_offsets_base = attr->as_unsigned ();
18725
18726 attr = die->attr (DW_AT_loclists_base);
18727 if (attr != nullptr)
18728 cu->loclist_base = attr->as_unsigned ();
18729
18730 auto maybe_addr_base = die->addr_base ();
18731 if (maybe_addr_base.has_value ())
18732 cu->addr_base = *maybe_addr_base;
18733
18734 attr = die->attr (DW_AT_rnglists_base);
18735 if (attr != nullptr)
18736 cu->rnglists_base = attr->as_unsigned ();
18737
18738 if (any_need_reprocess)
18739 {
18740 for (i = 0; i < abbrev->num_attrs; ++i)
18741 {
18742 if (die->attrs[i].requires_reprocessing_p ())
18743 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18744 }
18745 }
18746 *diep = die;
18747 return info_ptr;
18748 }
18749
18750 /* Read a die and all its attributes.
18751 Set DIEP to point to a newly allocated die with its information,
18752 except for its child, sibling, and parent fields. */
18753
18754 static const gdb_byte *
18755 read_full_die (const struct die_reader_specs *reader,
18756 struct die_info **diep, const gdb_byte *info_ptr)
18757 {
18758 const gdb_byte *result;
18759
18760 result = read_full_die_1 (reader, diep, info_ptr, 0);
18761
18762 if (dwarf_die_debug)
18763 {
18764 fprintf_unfiltered (gdb_stdlog,
18765 "Read die from %s@0x%x of %s:\n",
18766 reader->die_section->get_name (),
18767 (unsigned) (info_ptr - reader->die_section->buffer),
18768 bfd_get_filename (reader->abfd));
18769 dump_die (*diep, dwarf_die_debug);
18770 }
18771
18772 return result;
18773 }
18774 \f
18775
18776 /* Returns nonzero if TAG represents a type that we might generate a partial
18777 symbol for. */
18778
18779 static int
18780 is_type_tag_for_partial (int tag, enum language lang)
18781 {
18782 switch (tag)
18783 {
18784 #if 0
18785 /* Some types that would be reasonable to generate partial symbols for,
18786 that we don't at present. Note that normally this does not
18787 matter, mainly because C compilers don't give names to these
18788 types, but instead emit DW_TAG_typedef. */
18789 case DW_TAG_file_type:
18790 case DW_TAG_ptr_to_member_type:
18791 case DW_TAG_set_type:
18792 case DW_TAG_string_type:
18793 case DW_TAG_subroutine_type:
18794 #endif
18795
18796 /* GNAT may emit an array with a name, but no typedef, so we
18797 need to make a symbol in this case. */
18798 case DW_TAG_array_type:
18799 return lang == language_ada;
18800
18801 case DW_TAG_base_type:
18802 case DW_TAG_class_type:
18803 case DW_TAG_interface_type:
18804 case DW_TAG_enumeration_type:
18805 case DW_TAG_structure_type:
18806 case DW_TAG_subrange_type:
18807 case DW_TAG_typedef:
18808 case DW_TAG_union_type:
18809 return 1;
18810 default:
18811 return 0;
18812 }
18813 }
18814
18815 /* Load all DIEs that are interesting for partial symbols into memory. */
18816
18817 static struct partial_die_info *
18818 load_partial_dies (const struct die_reader_specs *reader,
18819 const gdb_byte *info_ptr, int building_psymtab)
18820 {
18821 struct dwarf2_cu *cu = reader->cu;
18822 struct objfile *objfile = cu->per_objfile->objfile;
18823 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18824 unsigned int bytes_read;
18825 unsigned int load_all = 0;
18826 int nesting_level = 1;
18827
18828 parent_die = NULL;
18829 last_die = NULL;
18830
18831 gdb_assert (cu->per_cu != NULL);
18832 if (cu->load_all_dies)
18833 load_all = 1;
18834
18835 cu->partial_dies
18836 = htab_create_alloc_ex (cu->header.length / 12,
18837 partial_die_hash,
18838 partial_die_eq,
18839 NULL,
18840 &cu->comp_unit_obstack,
18841 hashtab_obstack_allocate,
18842 dummy_obstack_deallocate);
18843
18844 while (1)
18845 {
18846 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18847 &bytes_read);
18848
18849 /* A NULL abbrev means the end of a series of children. */
18850 if (abbrev == NULL)
18851 {
18852 if (--nesting_level == 0)
18853 return first_die;
18854
18855 info_ptr += bytes_read;
18856 last_die = parent_die;
18857 parent_die = parent_die->die_parent;
18858 continue;
18859 }
18860
18861 /* Check for template arguments. We never save these; if
18862 they're seen, we just mark the parent, and go on our way. */
18863 if (parent_die != NULL
18864 && cu->per_cu->lang == language_cplus
18865 && (abbrev->tag == DW_TAG_template_type_param
18866 || abbrev->tag == DW_TAG_template_value_param))
18867 {
18868 parent_die->has_template_arguments = 1;
18869
18870 if (!load_all)
18871 {
18872 /* We don't need a partial DIE for the template argument. */
18873 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18874 continue;
18875 }
18876 }
18877
18878 /* We only recurse into c++ subprograms looking for template arguments.
18879 Skip their other children. */
18880 if (!load_all
18881 && cu->per_cu->lang == language_cplus
18882 && parent_die != NULL
18883 && parent_die->tag == DW_TAG_subprogram
18884 && abbrev->tag != DW_TAG_inlined_subroutine)
18885 {
18886 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18887 continue;
18888 }
18889
18890 /* Check whether this DIE is interesting enough to save. Normally
18891 we would not be interested in members here, but there may be
18892 later variables referencing them via DW_AT_specification (for
18893 static members). */
18894 if (!load_all
18895 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
18896 && abbrev->tag != DW_TAG_constant
18897 && abbrev->tag != DW_TAG_enumerator
18898 && abbrev->tag != DW_TAG_subprogram
18899 && abbrev->tag != DW_TAG_inlined_subroutine
18900 && abbrev->tag != DW_TAG_lexical_block
18901 && abbrev->tag != DW_TAG_variable
18902 && abbrev->tag != DW_TAG_namespace
18903 && abbrev->tag != DW_TAG_module
18904 && abbrev->tag != DW_TAG_member
18905 && abbrev->tag != DW_TAG_imported_unit
18906 && abbrev->tag != DW_TAG_imported_declaration)
18907 {
18908 /* Otherwise we skip to the next sibling, if any. */
18909 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18910 continue;
18911 }
18912
18913 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18914 abbrev);
18915
18916 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18917
18918 /* This two-pass algorithm for processing partial symbols has a
18919 high cost in cache pressure. Thus, handle some simple cases
18920 here which cover the majority of C partial symbols. DIEs
18921 which neither have specification tags in them, nor could have
18922 specification tags elsewhere pointing at them, can simply be
18923 processed and discarded.
18924
18925 This segment is also optional; scan_partial_symbols and
18926 add_partial_symbol will handle these DIEs if we chain
18927 them in normally. When compilers which do not emit large
18928 quantities of duplicate debug information are more common,
18929 this code can probably be removed. */
18930
18931 /* Any complete simple types at the top level (pretty much all
18932 of them, for a language without namespaces), can be processed
18933 directly. */
18934 if (parent_die == NULL
18935 && pdi.has_specification == 0
18936 && pdi.is_declaration == 0
18937 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18938 || pdi.tag == DW_TAG_base_type
18939 || pdi.tag == DW_TAG_array_type
18940 || pdi.tag == DW_TAG_subrange_type))
18941 {
18942 if (building_psymtab && pdi.raw_name != NULL)
18943 add_partial_symbol (&pdi, cu);
18944
18945 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18946 continue;
18947 }
18948
18949 /* The exception for DW_TAG_typedef with has_children above is
18950 a workaround of GCC PR debug/47510. In the case of this complaint
18951 type_name_or_error will error on such types later.
18952
18953 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18954 it could not find the child DIEs referenced later, this is checked
18955 above. In correct DWARF DW_TAG_typedef should have no children. */
18956
18957 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18958 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18959 "- DIE at %s [in module %s]"),
18960 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18961
18962 /* If we're at the second level, and we're an enumerator, and
18963 our parent has no specification (meaning possibly lives in a
18964 namespace elsewhere), then we can add the partial symbol now
18965 instead of queueing it. */
18966 if (pdi.tag == DW_TAG_enumerator
18967 && parent_die != NULL
18968 && parent_die->die_parent == NULL
18969 && parent_die->tag == DW_TAG_enumeration_type
18970 && parent_die->has_specification == 0)
18971 {
18972 if (pdi.raw_name == NULL)
18973 complaint (_("malformed enumerator DIE ignored"));
18974 else if (building_psymtab)
18975 add_partial_symbol (&pdi, cu);
18976
18977 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18978 continue;
18979 }
18980
18981 struct partial_die_info *part_die
18982 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18983
18984 /* We'll save this DIE so link it in. */
18985 part_die->die_parent = parent_die;
18986 part_die->die_sibling = NULL;
18987 part_die->die_child = NULL;
18988
18989 if (last_die && last_die == parent_die)
18990 last_die->die_child = part_die;
18991 else if (last_die)
18992 last_die->die_sibling = part_die;
18993
18994 last_die = part_die;
18995
18996 if (first_die == NULL)
18997 first_die = part_die;
18998
18999 /* Maybe add the DIE to the hash table. Not all DIEs that we
19000 find interesting need to be in the hash table, because we
19001 also have the parent/sibling/child chains; only those that we
19002 might refer to by offset later during partial symbol reading.
19003
19004 For now this means things that might have be the target of a
19005 DW_AT_specification, DW_AT_abstract_origin, or
19006 DW_AT_extension. DW_AT_extension will refer only to
19007 namespaces; DW_AT_abstract_origin refers to functions (and
19008 many things under the function DIE, but we do not recurse
19009 into function DIEs during partial symbol reading) and
19010 possibly variables as well; DW_AT_specification refers to
19011 declarations. Declarations ought to have the DW_AT_declaration
19012 flag. It happens that GCC forgets to put it in sometimes, but
19013 only for functions, not for types.
19014
19015 Adding more things than necessary to the hash table is harmless
19016 except for the performance cost. Adding too few will result in
19017 wasted time in find_partial_die, when we reread the compilation
19018 unit with load_all_dies set. */
19019
19020 if (load_all
19021 || abbrev->tag == DW_TAG_constant
19022 || abbrev->tag == DW_TAG_subprogram
19023 || abbrev->tag == DW_TAG_variable
19024 || abbrev->tag == DW_TAG_namespace
19025 || part_die->is_declaration)
19026 {
19027 void **slot;
19028
19029 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19030 to_underlying (part_die->sect_off),
19031 INSERT);
19032 *slot = part_die;
19033 }
19034
19035 /* For some DIEs we want to follow their children (if any). For C
19036 we have no reason to follow the children of structures; for other
19037 languages we have to, so that we can get at method physnames
19038 to infer fully qualified class names, for DW_AT_specification,
19039 and for C++ template arguments. For C++, we also look one level
19040 inside functions to find template arguments (if the name of the
19041 function does not already contain the template arguments).
19042
19043 For Ada and Fortran, we need to scan the children of subprograms
19044 and lexical blocks as well because these languages allow the
19045 definition of nested entities that could be interesting for the
19046 debugger, such as nested subprograms for instance. */
19047 if (last_die->has_children
19048 && (load_all
19049 || last_die->tag == DW_TAG_namespace
19050 || last_die->tag == DW_TAG_module
19051 || last_die->tag == DW_TAG_enumeration_type
19052 || (cu->per_cu->lang == language_cplus
19053 && last_die->tag == DW_TAG_subprogram
19054 && (last_die->raw_name == NULL
19055 || strchr (last_die->raw_name, '<') == NULL))
19056 || (cu->per_cu->lang != language_c
19057 && (last_die->tag == DW_TAG_class_type
19058 || last_die->tag == DW_TAG_interface_type
19059 || last_die->tag == DW_TAG_structure_type
19060 || last_die->tag == DW_TAG_union_type))
19061 || ((cu->per_cu->lang == language_ada
19062 || cu->per_cu->lang == language_fortran)
19063 && (last_die->tag == DW_TAG_subprogram
19064 || last_die->tag == DW_TAG_lexical_block))))
19065 {
19066 nesting_level++;
19067 parent_die = last_die;
19068 continue;
19069 }
19070
19071 /* Otherwise we skip to the next sibling, if any. */
19072 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19073
19074 /* Back to the top, do it again. */
19075 }
19076 }
19077
19078 partial_die_info::partial_die_info (sect_offset sect_off_,
19079 const struct abbrev_info *abbrev)
19080 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19081 {
19082 }
19083
19084 /* See class definition. */
19085
19086 const char *
19087 partial_die_info::name (dwarf2_cu *cu)
19088 {
19089 if (!canonical_name && raw_name != nullptr)
19090 {
19091 struct objfile *objfile = cu->per_objfile->objfile;
19092 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19093 canonical_name = 1;
19094 }
19095
19096 return raw_name;
19097 }
19098
19099 /* Read a minimal amount of information into the minimal die structure.
19100 INFO_PTR should point just after the initial uleb128 of a DIE. */
19101
19102 const gdb_byte *
19103 partial_die_info::read (const struct die_reader_specs *reader,
19104 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19105 {
19106 struct dwarf2_cu *cu = reader->cu;
19107 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19108 unsigned int i;
19109 int has_low_pc_attr = 0;
19110 int has_high_pc_attr = 0;
19111 int high_pc_relative = 0;
19112
19113 for (i = 0; i < abbrev.num_attrs; ++i)
19114 {
19115 attribute attr;
19116 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19117 /* String and address offsets that need to do the reprocessing have
19118 already been read at this point, so there is no need to wait until
19119 the loop terminates to do the reprocessing. */
19120 if (attr.requires_reprocessing_p ())
19121 read_attribute_reprocess (reader, &attr, tag);
19122 /* Store the data if it is of an attribute we want to keep in a
19123 partial symbol table. */
19124 switch (attr.name)
19125 {
19126 case DW_AT_name:
19127 switch (tag)
19128 {
19129 case DW_TAG_compile_unit:
19130 case DW_TAG_partial_unit:
19131 case DW_TAG_type_unit:
19132 /* Compilation units have a DW_AT_name that is a filename, not
19133 a source language identifier. */
19134 case DW_TAG_enumeration_type:
19135 case DW_TAG_enumerator:
19136 /* These tags always have simple identifiers already; no need
19137 to canonicalize them. */
19138 canonical_name = 1;
19139 raw_name = attr.as_string ();
19140 break;
19141 default:
19142 canonical_name = 0;
19143 raw_name = attr.as_string ();
19144 break;
19145 }
19146 break;
19147 case DW_AT_linkage_name:
19148 case DW_AT_MIPS_linkage_name:
19149 /* Note that both forms of linkage name might appear. We
19150 assume they will be the same, and we only store the last
19151 one we see. */
19152 linkage_name = attr.as_string ();
19153 break;
19154 case DW_AT_low_pc:
19155 has_low_pc_attr = 1;
19156 lowpc = attr.as_address ();
19157 break;
19158 case DW_AT_high_pc:
19159 has_high_pc_attr = 1;
19160 highpc = attr.as_address ();
19161 if (cu->header.version >= 4 && attr.form_is_constant ())
19162 high_pc_relative = 1;
19163 break;
19164 case DW_AT_location:
19165 /* Support the .debug_loc offsets. */
19166 if (attr.form_is_block ())
19167 {
19168 d.locdesc = attr.as_block ();
19169 }
19170 else if (attr.form_is_section_offset ())
19171 {
19172 dwarf2_complex_location_expr_complaint ();
19173 }
19174 else
19175 {
19176 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19177 "partial symbol information");
19178 }
19179 break;
19180 case DW_AT_external:
19181 is_external = attr.as_boolean ();
19182 break;
19183 case DW_AT_declaration:
19184 is_declaration = attr.as_boolean ();
19185 break;
19186 case DW_AT_type:
19187 has_type = 1;
19188 break;
19189 case DW_AT_abstract_origin:
19190 case DW_AT_specification:
19191 case DW_AT_extension:
19192 has_specification = 1;
19193 spec_offset = attr.get_ref_die_offset ();
19194 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19195 || cu->per_cu->is_dwz);
19196 break;
19197 case DW_AT_sibling:
19198 /* Ignore absolute siblings, they might point outside of
19199 the current compile unit. */
19200 if (attr.form == DW_FORM_ref_addr)
19201 complaint (_("ignoring absolute DW_AT_sibling"));
19202 else
19203 {
19204 const gdb_byte *buffer = reader->buffer;
19205 sect_offset off = attr.get_ref_die_offset ();
19206 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19207
19208 if (sibling_ptr < info_ptr)
19209 complaint (_("DW_AT_sibling points backwards"));
19210 else if (sibling_ptr > reader->buffer_end)
19211 reader->die_section->overflow_complaint ();
19212 else
19213 sibling = sibling_ptr;
19214 }
19215 break;
19216 case DW_AT_byte_size:
19217 has_byte_size = 1;
19218 break;
19219 case DW_AT_const_value:
19220 has_const_value = 1;
19221 break;
19222 case DW_AT_calling_convention:
19223 /* DWARF doesn't provide a way to identify a program's source-level
19224 entry point. DW_AT_calling_convention attributes are only meant
19225 to describe functions' calling conventions.
19226
19227 However, because it's a necessary piece of information in
19228 Fortran, and before DWARF 4 DW_CC_program was the only
19229 piece of debugging information whose definition refers to
19230 a 'main program' at all, several compilers marked Fortran
19231 main programs with DW_CC_program --- even when those
19232 functions use the standard calling conventions.
19233
19234 Although DWARF now specifies a way to provide this
19235 information, we support this practice for backward
19236 compatibility. */
19237 if (attr.constant_value (0) == DW_CC_program
19238 && cu->per_cu->lang == language_fortran)
19239 main_subprogram = 1;
19240 break;
19241 case DW_AT_inline:
19242 {
19243 LONGEST value = attr.constant_value (-1);
19244 if (value == DW_INL_inlined
19245 || value == DW_INL_declared_inlined)
19246 may_be_inlined = 1;
19247 }
19248 break;
19249
19250 case DW_AT_import:
19251 if (tag == DW_TAG_imported_unit)
19252 {
19253 d.sect_off = attr.get_ref_die_offset ();
19254 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19255 || cu->per_cu->is_dwz);
19256 }
19257 break;
19258
19259 case DW_AT_main_subprogram:
19260 main_subprogram = attr.as_boolean ();
19261 break;
19262
19263 case DW_AT_ranges:
19264 {
19265 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19266 on DWARF version). */
19267 ULONGEST ranges_offset = attr.as_unsigned ();
19268
19269 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19270 this value. */
19271 if (tag != DW_TAG_compile_unit)
19272 ranges_offset += cu->gnu_ranges_base;
19273
19274 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19275 nullptr, tag))
19276 has_pc_info = 1;
19277 }
19278 break;
19279
19280 default:
19281 break;
19282 }
19283 }
19284
19285 /* For Ada, if both the name and the linkage name appear, we prefer
19286 the latter. This lets "catch exception" work better, regardless
19287 of the order in which the name and linkage name were emitted.
19288 Really, though, this is just a workaround for the fact that gdb
19289 doesn't store both the name and the linkage name. */
19290 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
19291 raw_name = linkage_name;
19292
19293 if (high_pc_relative)
19294 highpc += lowpc;
19295
19296 if (has_low_pc_attr && has_high_pc_attr)
19297 {
19298 /* When using the GNU linker, .gnu.linkonce. sections are used to
19299 eliminate duplicate copies of functions and vtables and such.
19300 The linker will arbitrarily choose one and discard the others.
19301 The AT_*_pc values for such functions refer to local labels in
19302 these sections. If the section from that file was discarded, the
19303 labels are not in the output, so the relocs get a value of 0.
19304 If this is a discarded function, mark the pc bounds as invalid,
19305 so that GDB will ignore it. */
19306 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19307 {
19308 struct objfile *objfile = per_objfile->objfile;
19309 struct gdbarch *gdbarch = objfile->arch ();
19310
19311 complaint (_("DW_AT_low_pc %s is zero "
19312 "for DIE at %s [in module %s]"),
19313 paddress (gdbarch, lowpc),
19314 sect_offset_str (sect_off),
19315 objfile_name (objfile));
19316 }
19317 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19318 else if (lowpc >= highpc)
19319 {
19320 struct objfile *objfile = per_objfile->objfile;
19321 struct gdbarch *gdbarch = objfile->arch ();
19322
19323 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19324 "for DIE at %s [in module %s]"),
19325 paddress (gdbarch, lowpc),
19326 paddress (gdbarch, highpc),
19327 sect_offset_str (sect_off),
19328 objfile_name (objfile));
19329 }
19330 else
19331 has_pc_info = 1;
19332 }
19333
19334 return info_ptr;
19335 }
19336
19337 /* Find a cached partial DIE at OFFSET in CU. */
19338
19339 struct partial_die_info *
19340 dwarf2_cu::find_partial_die (sect_offset sect_off)
19341 {
19342 struct partial_die_info *lookup_die = NULL;
19343 struct partial_die_info part_die (sect_off);
19344
19345 lookup_die = ((struct partial_die_info *)
19346 htab_find_with_hash (partial_dies, &part_die,
19347 to_underlying (sect_off)));
19348
19349 return lookup_die;
19350 }
19351
19352 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19353 except in the case of .debug_types DIEs which do not reference
19354 outside their CU (they do however referencing other types via
19355 DW_FORM_ref_sig8). */
19356
19357 static const struct cu_partial_die_info
19358 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19359 {
19360 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19361 struct objfile *objfile = per_objfile->objfile;
19362 struct partial_die_info *pd = NULL;
19363
19364 if (offset_in_dwz == cu->per_cu->is_dwz
19365 && cu->header.offset_in_cu_p (sect_off))
19366 {
19367 pd = cu->find_partial_die (sect_off);
19368 if (pd != NULL)
19369 return { cu, pd };
19370 /* We missed recording what we needed.
19371 Load all dies and try again. */
19372 }
19373 else
19374 {
19375 /* TUs don't reference other CUs/TUs (except via type signatures). */
19376 if (cu->per_cu->is_debug_types)
19377 {
19378 error (_("Dwarf Error: Type Unit at offset %s contains"
19379 " external reference to offset %s [in module %s].\n"),
19380 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19381 bfd_get_filename (objfile->obfd));
19382 }
19383 dwarf2_per_cu_data *per_cu
19384 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19385 per_objfile);
19386
19387 cu = per_objfile->get_cu (per_cu);
19388 if (cu == NULL || cu->partial_dies == NULL)
19389 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19390
19391 cu = per_objfile->get_cu (per_cu);
19392
19393 cu->last_used = 0;
19394 pd = cu->find_partial_die (sect_off);
19395 }
19396
19397 /* If we didn't find it, and not all dies have been loaded,
19398 load them all and try again. */
19399
19400 if (pd == NULL && cu->load_all_dies == 0)
19401 {
19402 cu->load_all_dies = 1;
19403
19404 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19405 THIS_CU->cu may already be in use. So we can't just free it and
19406 replace its DIEs with the ones we read in. Instead, we leave those
19407 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19408 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19409 set. */
19410 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19411
19412 pd = cu->find_partial_die (sect_off);
19413 }
19414
19415 if (pd == NULL)
19416 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19417 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19418 return { cu, pd };
19419 }
19420
19421 /* See if we can figure out if the class lives in a namespace. We do
19422 this by looking for a member function; its demangled name will
19423 contain namespace info, if there is any. */
19424
19425 static void
19426 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19427 struct dwarf2_cu *cu)
19428 {
19429 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19430 what template types look like, because the demangler
19431 frequently doesn't give the same name as the debug info. We
19432 could fix this by only using the demangled name to get the
19433 prefix (but see comment in read_structure_type). */
19434
19435 struct partial_die_info *real_pdi;
19436 struct partial_die_info *child_pdi;
19437
19438 /* If this DIE (this DIE's specification, if any) has a parent, then
19439 we should not do this. We'll prepend the parent's fully qualified
19440 name when we create the partial symbol. */
19441
19442 real_pdi = struct_pdi;
19443 while (real_pdi->has_specification)
19444 {
19445 auto res = find_partial_die (real_pdi->spec_offset,
19446 real_pdi->spec_is_dwz, cu);
19447 real_pdi = res.pdi;
19448 cu = res.cu;
19449 }
19450
19451 if (real_pdi->die_parent != NULL)
19452 return;
19453
19454 for (child_pdi = struct_pdi->die_child;
19455 child_pdi != NULL;
19456 child_pdi = child_pdi->die_sibling)
19457 {
19458 if (child_pdi->tag == DW_TAG_subprogram
19459 && child_pdi->linkage_name != NULL)
19460 {
19461 gdb::unique_xmalloc_ptr<char> actual_class_name
19462 (cu->language_defn->class_name_from_physname
19463 (child_pdi->linkage_name));
19464 if (actual_class_name != NULL)
19465 {
19466 struct objfile *objfile = cu->per_objfile->objfile;
19467 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19468 struct_pdi->canonical_name = 1;
19469 }
19470 break;
19471 }
19472 }
19473 }
19474
19475 /* Return true if a DIE with TAG may have the DW_AT_const_value
19476 attribute. */
19477
19478 static bool
19479 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19480 {
19481 switch (tag)
19482 {
19483 case DW_TAG_constant:
19484 case DW_TAG_enumerator:
19485 case DW_TAG_formal_parameter:
19486 case DW_TAG_template_value_param:
19487 case DW_TAG_variable:
19488 return true;
19489 }
19490
19491 return false;
19492 }
19493
19494 void
19495 partial_die_info::fixup (struct dwarf2_cu *cu)
19496 {
19497 /* Once we've fixed up a die, there's no point in doing so again.
19498 This also avoids a memory leak if we were to call
19499 guess_partial_die_structure_name multiple times. */
19500 if (fixup_called)
19501 return;
19502
19503 /* If we found a reference attribute and the DIE has no name, try
19504 to find a name in the referred to DIE. */
19505
19506 if (raw_name == NULL && has_specification)
19507 {
19508 struct partial_die_info *spec_die;
19509
19510 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19511 spec_die = res.pdi;
19512 cu = res.cu;
19513
19514 spec_die->fixup (cu);
19515
19516 if (spec_die->raw_name)
19517 {
19518 raw_name = spec_die->raw_name;
19519 canonical_name = spec_die->canonical_name;
19520
19521 /* Copy DW_AT_external attribute if it is set. */
19522 if (spec_die->is_external)
19523 is_external = spec_die->is_external;
19524 }
19525 }
19526
19527 if (!has_const_value && has_specification
19528 && can_have_DW_AT_const_value_p (tag))
19529 {
19530 struct partial_die_info *spec_die;
19531
19532 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19533 spec_die = res.pdi;
19534 cu = res.cu;
19535
19536 spec_die->fixup (cu);
19537
19538 if (spec_die->has_const_value)
19539 {
19540 /* Copy DW_AT_const_value attribute if it is set. */
19541 has_const_value = spec_die->has_const_value;
19542 }
19543 }
19544
19545 /* Set default names for some unnamed DIEs. */
19546
19547 if (raw_name == NULL && tag == DW_TAG_namespace)
19548 {
19549 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19550 canonical_name = 1;
19551 }
19552
19553 /* If there is no parent die to provide a namespace, and there are
19554 children, see if we can determine the namespace from their linkage
19555 name. */
19556 if (cu->per_cu->lang == language_cplus
19557 && !cu->per_objfile->per_bfd->types.empty ()
19558 && die_parent == NULL
19559 && has_children
19560 && (tag == DW_TAG_class_type
19561 || tag == DW_TAG_structure_type
19562 || tag == DW_TAG_union_type))
19563 guess_partial_die_structure_name (this, cu);
19564
19565 /* GCC might emit a nameless struct or union that has a linkage
19566 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19567 if (raw_name == NULL
19568 && (tag == DW_TAG_class_type
19569 || tag == DW_TAG_interface_type
19570 || tag == DW_TAG_structure_type
19571 || tag == DW_TAG_union_type)
19572 && linkage_name != NULL)
19573 {
19574 gdb::unique_xmalloc_ptr<char> demangled
19575 (gdb_demangle (linkage_name, DMGL_TYPES));
19576 if (demangled != nullptr)
19577 {
19578 const char *base;
19579
19580 /* Strip any leading namespaces/classes, keep only the base name.
19581 DW_AT_name for named DIEs does not contain the prefixes. */
19582 base = strrchr (demangled.get (), ':');
19583 if (base && base > demangled.get () && base[-1] == ':')
19584 base++;
19585 else
19586 base = demangled.get ();
19587
19588 struct objfile *objfile = cu->per_objfile->objfile;
19589 raw_name = objfile->intern (base);
19590 canonical_name = 1;
19591 }
19592 }
19593
19594 fixup_called = 1;
19595 }
19596
19597 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19598 contents from the given SECTION in the HEADER.
19599
19600 HEADER_OFFSET is the offset of the header in the section. */
19601 static void
19602 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19603 struct dwarf2_section_info *section,
19604 sect_offset header_offset)
19605 {
19606 unsigned int bytes_read;
19607 bfd *abfd = section->get_bfd_owner ();
19608 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19609
19610 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19611 info_ptr += bytes_read;
19612
19613 header->version = read_2_bytes (abfd, info_ptr);
19614 info_ptr += 2;
19615
19616 header->addr_size = read_1_byte (abfd, info_ptr);
19617 info_ptr += 1;
19618
19619 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19620 info_ptr += 1;
19621
19622 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19623 }
19624
19625 /* Return the DW_AT_loclists_base value for the CU. */
19626 static ULONGEST
19627 lookup_loclist_base (struct dwarf2_cu *cu)
19628 {
19629 /* For the .dwo unit, the loclist_base points to the first offset following
19630 the header. The header consists of the following entities-
19631 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19632 bit format)
19633 2. version (2 bytes)
19634 3. address size (1 byte)
19635 4. segment selector size (1 byte)
19636 5. offset entry count (4 bytes)
19637 These sizes are derived as per the DWARFv5 standard. */
19638 if (cu->dwo_unit != nullptr)
19639 {
19640 if (cu->header.initial_length_size == 4)
19641 return LOCLIST_HEADER_SIZE32;
19642 return LOCLIST_HEADER_SIZE64;
19643 }
19644 return cu->loclist_base;
19645 }
19646
19647 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19648 array of offsets in the .debug_loclists section. */
19649
19650 static sect_offset
19651 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19652 {
19653 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19654 struct objfile *objfile = per_objfile->objfile;
19655 bfd *abfd = objfile->obfd;
19656 ULONGEST loclist_header_size =
19657 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19658 : LOCLIST_HEADER_SIZE64);
19659 ULONGEST loclist_base = lookup_loclist_base (cu);
19660
19661 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19662 ULONGEST start_offset =
19663 loclist_base + loclist_index * cu->header.offset_size;
19664
19665 /* Get loclists section. */
19666 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19667
19668 /* Read the loclists section content. */
19669 section->read (objfile);
19670 if (section->buffer == NULL)
19671 error (_("DW_FORM_loclistx used without .debug_loclists "
19672 "section [in module %s]"), objfile_name (objfile));
19673
19674 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19675 so if loclist_base is smaller than the header size, we have a problem. */
19676 if (loclist_base < loclist_header_size)
19677 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19678 objfile_name (objfile));
19679
19680 /* Read the header of the loclists contribution. */
19681 struct loclists_rnglists_header header;
19682 read_loclists_rnglists_header (&header, section,
19683 (sect_offset) (loclist_base - loclist_header_size));
19684
19685 /* Verify the loclist index is valid. */
19686 if (loclist_index >= header.offset_entry_count)
19687 error (_("DW_FORM_loclistx pointing outside of "
19688 ".debug_loclists offset array [in module %s]"),
19689 objfile_name (objfile));
19690
19691 /* Validate that reading won't go beyond the end of the section. */
19692 if (start_offset + cu->header.offset_size > section->size)
19693 error (_("Reading DW_FORM_loclistx index beyond end of"
19694 ".debug_loclists section [in module %s]"),
19695 objfile_name (objfile));
19696
19697 const gdb_byte *info_ptr = section->buffer + start_offset;
19698
19699 if (cu->header.offset_size == 4)
19700 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19701 else
19702 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19703 }
19704
19705 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19706 array of offsets in the .debug_rnglists section. */
19707
19708 static sect_offset
19709 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19710 dwarf_tag tag)
19711 {
19712 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19713 struct objfile *objfile = dwarf2_per_objfile->objfile;
19714 bfd *abfd = objfile->obfd;
19715 ULONGEST rnglist_header_size =
19716 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19717 : RNGLIST_HEADER_SIZE64);
19718
19719 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19720 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19721 doesn't apply. */
19722 ULONGEST rnglist_base =
19723 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19724
19725 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19726 ULONGEST start_offset =
19727 rnglist_base + rnglist_index * cu->header.offset_size;
19728
19729 /* Get rnglists section. */
19730 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19731
19732 /* Read the rnglists section content. */
19733 section->read (objfile);
19734 if (section->buffer == nullptr)
19735 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19736 "[in module %s]"),
19737 objfile_name (objfile));
19738
19739 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19740 so if rnglist_base is smaller than the header size, we have a problem. */
19741 if (rnglist_base < rnglist_header_size)
19742 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19743 objfile_name (objfile));
19744
19745 /* Read the header of the rnglists contribution. */
19746 struct loclists_rnglists_header header;
19747 read_loclists_rnglists_header (&header, section,
19748 (sect_offset) (rnglist_base - rnglist_header_size));
19749
19750 /* Verify the rnglist index is valid. */
19751 if (rnglist_index >= header.offset_entry_count)
19752 error (_("DW_FORM_rnglistx index pointing outside of "
19753 ".debug_rnglists offset array [in module %s]"),
19754 objfile_name (objfile));
19755
19756 /* Validate that reading won't go beyond the end of the section. */
19757 if (start_offset + cu->header.offset_size > section->size)
19758 error (_("Reading DW_FORM_rnglistx index beyond end of"
19759 ".debug_rnglists section [in module %s]"),
19760 objfile_name (objfile));
19761
19762 const gdb_byte *info_ptr = section->buffer + start_offset;
19763
19764 if (cu->header.offset_size == 4)
19765 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19766 else
19767 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19768 }
19769
19770 /* Process the attributes that had to be skipped in the first round. These
19771 attributes are the ones that need str_offsets_base or addr_base attributes.
19772 They could not have been processed in the first round, because at the time
19773 the values of str_offsets_base or addr_base may not have been known. */
19774 static void
19775 read_attribute_reprocess (const struct die_reader_specs *reader,
19776 struct attribute *attr, dwarf_tag tag)
19777 {
19778 struct dwarf2_cu *cu = reader->cu;
19779 switch (attr->form)
19780 {
19781 case DW_FORM_addrx:
19782 case DW_FORM_GNU_addr_index:
19783 attr->set_address (read_addr_index (cu,
19784 attr->as_unsigned_reprocess ()));
19785 break;
19786 case DW_FORM_loclistx:
19787 {
19788 sect_offset loclists_sect_off
19789 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19790
19791 attr->set_unsigned (to_underlying (loclists_sect_off));
19792 }
19793 break;
19794 case DW_FORM_rnglistx:
19795 {
19796 sect_offset rnglists_sect_off
19797 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19798
19799 attr->set_unsigned (to_underlying (rnglists_sect_off));
19800 }
19801 break;
19802 case DW_FORM_strx:
19803 case DW_FORM_strx1:
19804 case DW_FORM_strx2:
19805 case DW_FORM_strx3:
19806 case DW_FORM_strx4:
19807 case DW_FORM_GNU_str_index:
19808 {
19809 unsigned int str_index = attr->as_unsigned_reprocess ();
19810 gdb_assert (!attr->canonical_string_p ());
19811 if (reader->dwo_file != NULL)
19812 attr->set_string_noncanonical (read_dwo_str_index (reader,
19813 str_index));
19814 else
19815 attr->set_string_noncanonical (read_stub_str_index (cu,
19816 str_index));
19817 break;
19818 }
19819 default:
19820 gdb_assert_not_reached (_("Unexpected DWARF form."));
19821 }
19822 }
19823
19824 /* Read an attribute value described by an attribute form. */
19825
19826 static const gdb_byte *
19827 read_attribute_value (const struct die_reader_specs *reader,
19828 struct attribute *attr, unsigned form,
19829 LONGEST implicit_const, const gdb_byte *info_ptr)
19830 {
19831 struct dwarf2_cu *cu = reader->cu;
19832 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19833 struct objfile *objfile = per_objfile->objfile;
19834 bfd *abfd = reader->abfd;
19835 struct comp_unit_head *cu_header = &cu->header;
19836 unsigned int bytes_read;
19837 struct dwarf_block *blk;
19838
19839 attr->form = (enum dwarf_form) form;
19840 switch (form)
19841 {
19842 case DW_FORM_ref_addr:
19843 if (cu_header->version == 2)
19844 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
19845 &bytes_read));
19846 else
19847 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19848 &bytes_read));
19849 info_ptr += bytes_read;
19850 break;
19851 case DW_FORM_GNU_ref_alt:
19852 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19853 &bytes_read));
19854 info_ptr += bytes_read;
19855 break;
19856 case DW_FORM_addr:
19857 {
19858 struct gdbarch *gdbarch = objfile->arch ();
19859 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
19860 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19861 attr->set_address (addr);
19862 info_ptr += bytes_read;
19863 }
19864 break;
19865 case DW_FORM_block2:
19866 blk = dwarf_alloc_block (cu);
19867 blk->size = read_2_bytes (abfd, info_ptr);
19868 info_ptr += 2;
19869 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19870 info_ptr += blk->size;
19871 attr->set_block (blk);
19872 break;
19873 case DW_FORM_block4:
19874 blk = dwarf_alloc_block (cu);
19875 blk->size = read_4_bytes (abfd, info_ptr);
19876 info_ptr += 4;
19877 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19878 info_ptr += blk->size;
19879 attr->set_block (blk);
19880 break;
19881 case DW_FORM_data2:
19882 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19883 info_ptr += 2;
19884 break;
19885 case DW_FORM_data4:
19886 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19887 info_ptr += 4;
19888 break;
19889 case DW_FORM_data8:
19890 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19891 info_ptr += 8;
19892 break;
19893 case DW_FORM_data16:
19894 blk = dwarf_alloc_block (cu);
19895 blk->size = 16;
19896 blk->data = read_n_bytes (abfd, info_ptr, 16);
19897 info_ptr += 16;
19898 attr->set_block (blk);
19899 break;
19900 case DW_FORM_sec_offset:
19901 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19902 &bytes_read));
19903 info_ptr += bytes_read;
19904 break;
19905 case DW_FORM_loclistx:
19906 {
19907 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19908 &bytes_read));
19909 info_ptr += bytes_read;
19910 }
19911 break;
19912 case DW_FORM_string:
19913 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19914 &bytes_read));
19915 info_ptr += bytes_read;
19916 break;
19917 case DW_FORM_strp:
19918 if (!cu->per_cu->is_dwz)
19919 {
19920 attr->set_string_noncanonical
19921 (read_indirect_string (per_objfile,
19922 abfd, info_ptr, cu_header,
19923 &bytes_read));
19924 info_ptr += bytes_read;
19925 break;
19926 }
19927 /* FALLTHROUGH */
19928 case DW_FORM_line_strp:
19929 if (!cu->per_cu->is_dwz)
19930 {
19931 attr->set_string_noncanonical
19932 (per_objfile->read_line_string (info_ptr, cu_header,
19933 &bytes_read));
19934 info_ptr += bytes_read;
19935 break;
19936 }
19937 /* FALLTHROUGH */
19938 case DW_FORM_GNU_strp_alt:
19939 {
19940 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19941 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19942 &bytes_read);
19943
19944 attr->set_string_noncanonical
19945 (dwz->read_string (objfile, str_offset));
19946 info_ptr += bytes_read;
19947 }
19948 break;
19949 case DW_FORM_exprloc:
19950 case DW_FORM_block:
19951 blk = dwarf_alloc_block (cu);
19952 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19953 info_ptr += bytes_read;
19954 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19955 info_ptr += blk->size;
19956 attr->set_block (blk);
19957 break;
19958 case DW_FORM_block1:
19959 blk = dwarf_alloc_block (cu);
19960 blk->size = read_1_byte (abfd, info_ptr);
19961 info_ptr += 1;
19962 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19963 info_ptr += blk->size;
19964 attr->set_block (blk);
19965 break;
19966 case DW_FORM_data1:
19967 case DW_FORM_flag:
19968 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19969 info_ptr += 1;
19970 break;
19971 case DW_FORM_flag_present:
19972 attr->set_unsigned (1);
19973 break;
19974 case DW_FORM_sdata:
19975 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19976 info_ptr += bytes_read;
19977 break;
19978 case DW_FORM_rnglistx:
19979 {
19980 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19981 &bytes_read));
19982 info_ptr += bytes_read;
19983 }
19984 break;
19985 case DW_FORM_udata:
19986 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19987 info_ptr += bytes_read;
19988 break;
19989 case DW_FORM_ref1:
19990 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19991 + read_1_byte (abfd, info_ptr)));
19992 info_ptr += 1;
19993 break;
19994 case DW_FORM_ref2:
19995 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19996 + read_2_bytes (abfd, info_ptr)));
19997 info_ptr += 2;
19998 break;
19999 case DW_FORM_ref4:
20000 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20001 + read_4_bytes (abfd, info_ptr)));
20002 info_ptr += 4;
20003 break;
20004 case DW_FORM_ref8:
20005 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20006 + read_8_bytes (abfd, info_ptr)));
20007 info_ptr += 8;
20008 break;
20009 case DW_FORM_ref_sig8:
20010 attr->set_signature (read_8_bytes (abfd, info_ptr));
20011 info_ptr += 8;
20012 break;
20013 case DW_FORM_ref_udata:
20014 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20015 + read_unsigned_leb128 (abfd, info_ptr,
20016 &bytes_read)));
20017 info_ptr += bytes_read;
20018 break;
20019 case DW_FORM_indirect:
20020 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20021 info_ptr += bytes_read;
20022 if (form == DW_FORM_implicit_const)
20023 {
20024 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20025 info_ptr += bytes_read;
20026 }
20027 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20028 info_ptr);
20029 break;
20030 case DW_FORM_implicit_const:
20031 attr->set_signed (implicit_const);
20032 break;
20033 case DW_FORM_addrx:
20034 case DW_FORM_GNU_addr_index:
20035 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20036 &bytes_read));
20037 info_ptr += bytes_read;
20038 break;
20039 case DW_FORM_strx:
20040 case DW_FORM_strx1:
20041 case DW_FORM_strx2:
20042 case DW_FORM_strx3:
20043 case DW_FORM_strx4:
20044 case DW_FORM_GNU_str_index:
20045 {
20046 ULONGEST str_index;
20047 if (form == DW_FORM_strx1)
20048 {
20049 str_index = read_1_byte (abfd, info_ptr);
20050 info_ptr += 1;
20051 }
20052 else if (form == DW_FORM_strx2)
20053 {
20054 str_index = read_2_bytes (abfd, info_ptr);
20055 info_ptr += 2;
20056 }
20057 else if (form == DW_FORM_strx3)
20058 {
20059 str_index = read_3_bytes (abfd, info_ptr);
20060 info_ptr += 3;
20061 }
20062 else if (form == DW_FORM_strx4)
20063 {
20064 str_index = read_4_bytes (abfd, info_ptr);
20065 info_ptr += 4;
20066 }
20067 else
20068 {
20069 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20070 info_ptr += bytes_read;
20071 }
20072 attr->set_unsigned_reprocess (str_index);
20073 }
20074 break;
20075 default:
20076 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20077 dwarf_form_name (form),
20078 bfd_get_filename (abfd));
20079 }
20080
20081 /* Super hack. */
20082 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20083 attr->form = DW_FORM_GNU_ref_alt;
20084
20085 /* We have seen instances where the compiler tried to emit a byte
20086 size attribute of -1 which ended up being encoded as an unsigned
20087 0xffffffff. Although 0xffffffff is technically a valid size value,
20088 an object of this size seems pretty unlikely so we can relatively
20089 safely treat these cases as if the size attribute was invalid and
20090 treat them as zero by default. */
20091 if (attr->name == DW_AT_byte_size
20092 && form == DW_FORM_data4
20093 && attr->as_unsigned () >= 0xffffffff)
20094 {
20095 complaint
20096 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20097 hex_string (attr->as_unsigned ()));
20098 attr->set_unsigned (0);
20099 }
20100
20101 return info_ptr;
20102 }
20103
20104 /* Read an attribute described by an abbreviated attribute. */
20105
20106 static const gdb_byte *
20107 read_attribute (const struct die_reader_specs *reader,
20108 struct attribute *attr, const struct attr_abbrev *abbrev,
20109 const gdb_byte *info_ptr)
20110 {
20111 attr->name = abbrev->name;
20112 attr->string_is_canonical = 0;
20113 attr->requires_reprocessing = 0;
20114 return read_attribute_value (reader, attr, abbrev->form,
20115 abbrev->implicit_const, info_ptr);
20116 }
20117
20118 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20119
20120 static const char *
20121 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20122 LONGEST str_offset)
20123 {
20124 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20125 str_offset, "DW_FORM_strp");
20126 }
20127
20128 /* Return pointer to string at .debug_str offset as read from BUF.
20129 BUF is assumed to be in a compilation unit described by CU_HEADER.
20130 Return *BYTES_READ_PTR count of bytes read from BUF. */
20131
20132 static const char *
20133 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20134 const gdb_byte *buf,
20135 const struct comp_unit_head *cu_header,
20136 unsigned int *bytes_read_ptr)
20137 {
20138 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20139
20140 return read_indirect_string_at_offset (per_objfile, str_offset);
20141 }
20142
20143 /* See read.h. */
20144
20145 const char *
20146 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20147 const struct comp_unit_head *cu_header,
20148 unsigned int *bytes_read_ptr)
20149 {
20150 bfd *abfd = objfile->obfd;
20151 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20152
20153 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20154 }
20155
20156 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20157 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20158 ADDR_SIZE is the size of addresses from the CU header. */
20159
20160 static CORE_ADDR
20161 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20162 gdb::optional<ULONGEST> addr_base, int addr_size)
20163 {
20164 struct objfile *objfile = per_objfile->objfile;
20165 bfd *abfd = objfile->obfd;
20166 const gdb_byte *info_ptr;
20167 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20168
20169 per_objfile->per_bfd->addr.read (objfile);
20170 if (per_objfile->per_bfd->addr.buffer == NULL)
20171 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20172 objfile_name (objfile));
20173 if (addr_base_or_zero + addr_index * addr_size
20174 >= per_objfile->per_bfd->addr.size)
20175 error (_("DW_FORM_addr_index pointing outside of "
20176 ".debug_addr section [in module %s]"),
20177 objfile_name (objfile));
20178 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20179 + addr_index * addr_size);
20180 if (addr_size == 4)
20181 return bfd_get_32 (abfd, info_ptr);
20182 else
20183 return bfd_get_64 (abfd, info_ptr);
20184 }
20185
20186 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20187
20188 static CORE_ADDR
20189 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20190 {
20191 return read_addr_index_1 (cu->per_objfile, addr_index,
20192 cu->addr_base, cu->header.addr_size);
20193 }
20194
20195 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20196
20197 static CORE_ADDR
20198 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20199 unsigned int *bytes_read)
20200 {
20201 bfd *abfd = cu->per_objfile->objfile->obfd;
20202 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20203
20204 return read_addr_index (cu, addr_index);
20205 }
20206
20207 /* See read.h. */
20208
20209 CORE_ADDR
20210 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20211 dwarf2_per_objfile *per_objfile,
20212 unsigned int addr_index)
20213 {
20214 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20215 gdb::optional<ULONGEST> addr_base;
20216 int addr_size;
20217
20218 /* We need addr_base and addr_size.
20219 If we don't have PER_CU->cu, we have to get it.
20220 Nasty, but the alternative is storing the needed info in PER_CU,
20221 which at this point doesn't seem justified: it's not clear how frequently
20222 it would get used and it would increase the size of every PER_CU.
20223 Entry points like dwarf2_per_cu_addr_size do a similar thing
20224 so we're not in uncharted territory here.
20225 Alas we need to be a bit more complicated as addr_base is contained
20226 in the DIE.
20227
20228 We don't need to read the entire CU(/TU).
20229 We just need the header and top level die.
20230
20231 IWBN to use the aging mechanism to let us lazily later discard the CU.
20232 For now we skip this optimization. */
20233
20234 if (cu != NULL)
20235 {
20236 addr_base = cu->addr_base;
20237 addr_size = cu->header.addr_size;
20238 }
20239 else
20240 {
20241 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20242 addr_base = reader.cu->addr_base;
20243 addr_size = reader.cu->header.addr_size;
20244 }
20245
20246 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20247 }
20248
20249 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20250 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20251 DWO file. */
20252
20253 static const char *
20254 read_str_index (struct dwarf2_cu *cu,
20255 struct dwarf2_section_info *str_section,
20256 struct dwarf2_section_info *str_offsets_section,
20257 ULONGEST str_offsets_base, ULONGEST str_index)
20258 {
20259 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20260 struct objfile *objfile = per_objfile->objfile;
20261 const char *objf_name = objfile_name (objfile);
20262 bfd *abfd = objfile->obfd;
20263 const gdb_byte *info_ptr;
20264 ULONGEST str_offset;
20265 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20266
20267 str_section->read (objfile);
20268 str_offsets_section->read (objfile);
20269 if (str_section->buffer == NULL)
20270 error (_("%s used without %s section"
20271 " in CU at offset %s [in module %s]"),
20272 form_name, str_section->get_name (),
20273 sect_offset_str (cu->header.sect_off), objf_name);
20274 if (str_offsets_section->buffer == NULL)
20275 error (_("%s used without %s section"
20276 " in CU at offset %s [in module %s]"),
20277 form_name, str_section->get_name (),
20278 sect_offset_str (cu->header.sect_off), objf_name);
20279 info_ptr = (str_offsets_section->buffer
20280 + str_offsets_base
20281 + str_index * cu->header.offset_size);
20282 if (cu->header.offset_size == 4)
20283 str_offset = bfd_get_32 (abfd, info_ptr);
20284 else
20285 str_offset = bfd_get_64 (abfd, info_ptr);
20286 if (str_offset >= str_section->size)
20287 error (_("Offset from %s pointing outside of"
20288 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20289 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20290 return (const char *) (str_section->buffer + str_offset);
20291 }
20292
20293 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20294
20295 static const char *
20296 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20297 {
20298 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20299 ? reader->cu->header.addr_size : 0;
20300 return read_str_index (reader->cu,
20301 &reader->dwo_file->sections.str,
20302 &reader->dwo_file->sections.str_offsets,
20303 str_offsets_base, str_index);
20304 }
20305
20306 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20307
20308 static const char *
20309 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20310 {
20311 struct objfile *objfile = cu->per_objfile->objfile;
20312 const char *objf_name = objfile_name (objfile);
20313 static const char form_name[] = "DW_FORM_GNU_str_index";
20314 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20315
20316 if (!cu->str_offsets_base.has_value ())
20317 error (_("%s used in Fission stub without %s"
20318 " in CU at offset 0x%lx [in module %s]"),
20319 form_name, str_offsets_attr_name,
20320 (long) cu->header.offset_size, objf_name);
20321
20322 return read_str_index (cu,
20323 &cu->per_objfile->per_bfd->str,
20324 &cu->per_objfile->per_bfd->str_offsets,
20325 *cu->str_offsets_base, str_index);
20326 }
20327
20328 /* Return the length of an LEB128 number in BUF. */
20329
20330 static int
20331 leb128_size (const gdb_byte *buf)
20332 {
20333 const gdb_byte *begin = buf;
20334 gdb_byte byte;
20335
20336 while (1)
20337 {
20338 byte = *buf++;
20339 if ((byte & 128) == 0)
20340 return buf - begin;
20341 }
20342 }
20343
20344 static enum language
20345 dwarf_lang_to_enum_language (unsigned int lang)
20346 {
20347 enum language language;
20348
20349 switch (lang)
20350 {
20351 case DW_LANG_C89:
20352 case DW_LANG_C99:
20353 case DW_LANG_C11:
20354 case DW_LANG_C:
20355 case DW_LANG_UPC:
20356 language = language_c;
20357 break;
20358 case DW_LANG_Java:
20359 case DW_LANG_C_plus_plus:
20360 case DW_LANG_C_plus_plus_11:
20361 case DW_LANG_C_plus_plus_14:
20362 language = language_cplus;
20363 break;
20364 case DW_LANG_D:
20365 language = language_d;
20366 break;
20367 case DW_LANG_Fortran77:
20368 case DW_LANG_Fortran90:
20369 case DW_LANG_Fortran95:
20370 case DW_LANG_Fortran03:
20371 case DW_LANG_Fortran08:
20372 language = language_fortran;
20373 break;
20374 case DW_LANG_Go:
20375 language = language_go;
20376 break;
20377 case DW_LANG_Mips_Assembler:
20378 language = language_asm;
20379 break;
20380 case DW_LANG_Ada83:
20381 case DW_LANG_Ada95:
20382 language = language_ada;
20383 break;
20384 case DW_LANG_Modula2:
20385 language = language_m2;
20386 break;
20387 case DW_LANG_Pascal83:
20388 language = language_pascal;
20389 break;
20390 case DW_LANG_ObjC:
20391 language = language_objc;
20392 break;
20393 case DW_LANG_Rust:
20394 case DW_LANG_Rust_old:
20395 language = language_rust;
20396 break;
20397 case DW_LANG_OpenCL:
20398 language = language_opencl;
20399 break;
20400 case DW_LANG_Cobol74:
20401 case DW_LANG_Cobol85:
20402 default:
20403 language = language_minimal;
20404 break;
20405 }
20406
20407 return language;
20408 }
20409
20410 /* Return the named attribute or NULL if not there. */
20411
20412 static struct attribute *
20413 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20414 {
20415 for (;;)
20416 {
20417 unsigned int i;
20418 struct attribute *spec = NULL;
20419
20420 for (i = 0; i < die->num_attrs; ++i)
20421 {
20422 if (die->attrs[i].name == name)
20423 return &die->attrs[i];
20424 if (die->attrs[i].name == DW_AT_specification
20425 || die->attrs[i].name == DW_AT_abstract_origin)
20426 spec = &die->attrs[i];
20427 }
20428
20429 if (!spec)
20430 break;
20431
20432 die = follow_die_ref (die, spec, &cu);
20433 }
20434
20435 return NULL;
20436 }
20437
20438 /* Return the string associated with a string-typed attribute, or NULL if it
20439 is either not found or is of an incorrect type. */
20440
20441 static const char *
20442 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20443 {
20444 struct attribute *attr;
20445 const char *str = NULL;
20446
20447 attr = dwarf2_attr (die, name, cu);
20448
20449 if (attr != NULL)
20450 {
20451 str = attr->as_string ();
20452 if (str == nullptr)
20453 complaint (_("string type expected for attribute %s for "
20454 "DIE at %s in module %s"),
20455 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20456 objfile_name (cu->per_objfile->objfile));
20457 }
20458
20459 return str;
20460 }
20461
20462 /* Return the dwo name or NULL if not present. If present, it is in either
20463 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20464 static const char *
20465 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20466 {
20467 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20468 if (dwo_name == nullptr)
20469 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20470 return dwo_name;
20471 }
20472
20473 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20474 and holds a non-zero value. This function should only be used for
20475 DW_FORM_flag or DW_FORM_flag_present attributes. */
20476
20477 static int
20478 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20479 {
20480 struct attribute *attr = dwarf2_attr (die, name, cu);
20481
20482 return attr != nullptr && attr->as_boolean ();
20483 }
20484
20485 static int
20486 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20487 {
20488 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20489 which value is non-zero. However, we have to be careful with
20490 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20491 (via dwarf2_flag_true_p) follows this attribute. So we may
20492 end up accidently finding a declaration attribute that belongs
20493 to a different DIE referenced by the specification attribute,
20494 even though the given DIE does not have a declaration attribute. */
20495 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20496 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20497 }
20498
20499 /* Return the die giving the specification for DIE, if there is
20500 one. *SPEC_CU is the CU containing DIE on input, and the CU
20501 containing the return value on output. If there is no
20502 specification, but there is an abstract origin, that is
20503 returned. */
20504
20505 static struct die_info *
20506 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20507 {
20508 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20509 *spec_cu);
20510
20511 if (spec_attr == NULL)
20512 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20513
20514 if (spec_attr == NULL)
20515 return NULL;
20516 else
20517 return follow_die_ref (die, spec_attr, spec_cu);
20518 }
20519
20520 /* A convenience function to find the proper .debug_line section for a CU. */
20521
20522 static struct dwarf2_section_info *
20523 get_debug_line_section (struct dwarf2_cu *cu)
20524 {
20525 struct dwarf2_section_info *section;
20526 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20527
20528 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20529 DWO file. */
20530 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20531 section = &cu->dwo_unit->dwo_file->sections.line;
20532 else if (cu->per_cu->is_dwz)
20533 {
20534 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20535
20536 section = &dwz->line;
20537 }
20538 else
20539 section = &per_objfile->per_bfd->line;
20540
20541 return section;
20542 }
20543
20544 /* Read the statement program header starting at OFFSET in
20545 .debug_line, or .debug_line.dwo. Return a pointer
20546 to a struct line_header, allocated using xmalloc.
20547 Returns NULL if there is a problem reading the header, e.g., if it
20548 has a version we don't understand.
20549
20550 NOTE: the strings in the include directory and file name tables of
20551 the returned object point into the dwarf line section buffer,
20552 and must not be freed. */
20553
20554 static line_header_up
20555 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20556 {
20557 struct dwarf2_section_info *section;
20558 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20559
20560 section = get_debug_line_section (cu);
20561 section->read (per_objfile->objfile);
20562 if (section->buffer == NULL)
20563 {
20564 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20565 complaint (_("missing .debug_line.dwo section"));
20566 else
20567 complaint (_("missing .debug_line section"));
20568 return 0;
20569 }
20570
20571 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20572 per_objfile, section, &cu->header);
20573 }
20574
20575 /* Subroutine of dwarf_decode_lines to simplify it.
20576 Return the file name of the psymtab for the given file_entry.
20577 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20578 If space for the result is malloc'd, *NAME_HOLDER will be set.
20579 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20580
20581 static const char *
20582 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20583 const dwarf2_psymtab *pst,
20584 const char *comp_dir,
20585 gdb::unique_xmalloc_ptr<char> *name_holder)
20586 {
20587 const char *include_name = fe.name;
20588 const char *include_name_to_compare = include_name;
20589 const char *pst_filename;
20590 int file_is_pst;
20591
20592 const char *dir_name = fe.include_dir (lh);
20593
20594 gdb::unique_xmalloc_ptr<char> hold_compare;
20595 if (!IS_ABSOLUTE_PATH (include_name)
20596 && (dir_name != NULL || comp_dir != NULL))
20597 {
20598 /* Avoid creating a duplicate psymtab for PST.
20599 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20600 Before we do the comparison, however, we need to account
20601 for DIR_NAME and COMP_DIR.
20602 First prepend dir_name (if non-NULL). If we still don't
20603 have an absolute path prepend comp_dir (if non-NULL).
20604 However, the directory we record in the include-file's
20605 psymtab does not contain COMP_DIR (to match the
20606 corresponding symtab(s)).
20607
20608 Example:
20609
20610 bash$ cd /tmp
20611 bash$ gcc -g ./hello.c
20612 include_name = "hello.c"
20613 dir_name = "."
20614 DW_AT_comp_dir = comp_dir = "/tmp"
20615 DW_AT_name = "./hello.c"
20616
20617 */
20618
20619 if (dir_name != NULL)
20620 {
20621 name_holder->reset (concat (dir_name, SLASH_STRING,
20622 include_name, (char *) NULL));
20623 include_name = name_holder->get ();
20624 include_name_to_compare = include_name;
20625 }
20626 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20627 {
20628 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20629 include_name, (char *) NULL));
20630 include_name_to_compare = hold_compare.get ();
20631 }
20632 }
20633
20634 pst_filename = pst->filename;
20635 gdb::unique_xmalloc_ptr<char> copied_name;
20636 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20637 {
20638 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20639 pst_filename, (char *) NULL));
20640 pst_filename = copied_name.get ();
20641 }
20642
20643 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20644
20645 if (file_is_pst)
20646 return NULL;
20647 return include_name;
20648 }
20649
20650 /* State machine to track the state of the line number program. */
20651
20652 class lnp_state_machine
20653 {
20654 public:
20655 /* Initialize a machine state for the start of a line number
20656 program. */
20657 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20658 bool record_lines_p);
20659
20660 file_entry *current_file ()
20661 {
20662 /* lh->file_names is 0-based, but the file name numbers in the
20663 statement program are 1-based. */
20664 return m_line_header->file_name_at (m_file);
20665 }
20666
20667 /* Record the line in the state machine. END_SEQUENCE is true if
20668 we're processing the end of a sequence. */
20669 void record_line (bool end_sequence);
20670
20671 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20672 nop-out rest of the lines in this sequence. */
20673 void check_line_address (struct dwarf2_cu *cu,
20674 const gdb_byte *line_ptr,
20675 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20676
20677 void handle_set_discriminator (unsigned int discriminator)
20678 {
20679 m_discriminator = discriminator;
20680 m_line_has_non_zero_discriminator |= discriminator != 0;
20681 }
20682
20683 /* Handle DW_LNE_set_address. */
20684 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20685 {
20686 m_op_index = 0;
20687 address += baseaddr;
20688 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20689 }
20690
20691 /* Handle DW_LNS_advance_pc. */
20692 void handle_advance_pc (CORE_ADDR adjust);
20693
20694 /* Handle a special opcode. */
20695 void handle_special_opcode (unsigned char op_code);
20696
20697 /* Handle DW_LNS_advance_line. */
20698 void handle_advance_line (int line_delta)
20699 {
20700 advance_line (line_delta);
20701 }
20702
20703 /* Handle DW_LNS_set_file. */
20704 void handle_set_file (file_name_index file);
20705
20706 /* Handle DW_LNS_negate_stmt. */
20707 void handle_negate_stmt ()
20708 {
20709 m_is_stmt = !m_is_stmt;
20710 }
20711
20712 /* Handle DW_LNS_const_add_pc. */
20713 void handle_const_add_pc ();
20714
20715 /* Handle DW_LNS_fixed_advance_pc. */
20716 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20717 {
20718 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20719 m_op_index = 0;
20720 }
20721
20722 /* Handle DW_LNS_copy. */
20723 void handle_copy ()
20724 {
20725 record_line (false);
20726 m_discriminator = 0;
20727 }
20728
20729 /* Handle DW_LNE_end_sequence. */
20730 void handle_end_sequence ()
20731 {
20732 m_currently_recording_lines = true;
20733 }
20734
20735 private:
20736 /* Advance the line by LINE_DELTA. */
20737 void advance_line (int line_delta)
20738 {
20739 m_line += line_delta;
20740
20741 if (line_delta != 0)
20742 m_line_has_non_zero_discriminator = m_discriminator != 0;
20743 }
20744
20745 struct dwarf2_cu *m_cu;
20746
20747 gdbarch *m_gdbarch;
20748
20749 /* True if we're recording lines.
20750 Otherwise we're building partial symtabs and are just interested in
20751 finding include files mentioned by the line number program. */
20752 bool m_record_lines_p;
20753
20754 /* The line number header. */
20755 line_header *m_line_header;
20756
20757 /* These are part of the standard DWARF line number state machine,
20758 and initialized according to the DWARF spec. */
20759
20760 unsigned char m_op_index = 0;
20761 /* The line table index of the current file. */
20762 file_name_index m_file = 1;
20763 unsigned int m_line = 1;
20764
20765 /* These are initialized in the constructor. */
20766
20767 CORE_ADDR m_address;
20768 bool m_is_stmt;
20769 unsigned int m_discriminator;
20770
20771 /* Additional bits of state we need to track. */
20772
20773 /* The last file that we called dwarf2_start_subfile for.
20774 This is only used for TLLs. */
20775 unsigned int m_last_file = 0;
20776 /* The last file a line number was recorded for. */
20777 struct subfile *m_last_subfile = NULL;
20778
20779 /* The address of the last line entry. */
20780 CORE_ADDR m_last_address;
20781
20782 /* Set to true when a previous line at the same address (using
20783 m_last_address) had m_is_stmt true. This is reset to false when a
20784 line entry at a new address (m_address different to m_last_address) is
20785 processed. */
20786 bool m_stmt_at_address = false;
20787
20788 /* When true, record the lines we decode. */
20789 bool m_currently_recording_lines = false;
20790
20791 /* The last line number that was recorded, used to coalesce
20792 consecutive entries for the same line. This can happen, for
20793 example, when discriminators are present. PR 17276. */
20794 unsigned int m_last_line = 0;
20795 bool m_line_has_non_zero_discriminator = false;
20796 };
20797
20798 void
20799 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20800 {
20801 CORE_ADDR addr_adj = (((m_op_index + adjust)
20802 / m_line_header->maximum_ops_per_instruction)
20803 * m_line_header->minimum_instruction_length);
20804 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20805 m_op_index = ((m_op_index + adjust)
20806 % m_line_header->maximum_ops_per_instruction);
20807 }
20808
20809 void
20810 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20811 {
20812 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20813 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20814 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20815 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20816 / m_line_header->maximum_ops_per_instruction)
20817 * m_line_header->minimum_instruction_length);
20818 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20819 m_op_index = ((m_op_index + adj_opcode_d)
20820 % m_line_header->maximum_ops_per_instruction);
20821
20822 int line_delta = m_line_header->line_base + adj_opcode_r;
20823 advance_line (line_delta);
20824 record_line (false);
20825 m_discriminator = 0;
20826 }
20827
20828 void
20829 lnp_state_machine::handle_set_file (file_name_index file)
20830 {
20831 m_file = file;
20832
20833 const file_entry *fe = current_file ();
20834 if (fe == NULL)
20835 dwarf2_debug_line_missing_file_complaint ();
20836 else if (m_record_lines_p)
20837 {
20838 const char *dir = fe->include_dir (m_line_header);
20839
20840 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20841 m_line_has_non_zero_discriminator = m_discriminator != 0;
20842 dwarf2_start_subfile (m_cu, fe->name, dir);
20843 }
20844 }
20845
20846 void
20847 lnp_state_machine::handle_const_add_pc ()
20848 {
20849 CORE_ADDR adjust
20850 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20851
20852 CORE_ADDR addr_adj
20853 = (((m_op_index + adjust)
20854 / m_line_header->maximum_ops_per_instruction)
20855 * m_line_header->minimum_instruction_length);
20856
20857 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20858 m_op_index = ((m_op_index + adjust)
20859 % m_line_header->maximum_ops_per_instruction);
20860 }
20861
20862 /* Return non-zero if we should add LINE to the line number table.
20863 LINE is the line to add, LAST_LINE is the last line that was added,
20864 LAST_SUBFILE is the subfile for LAST_LINE.
20865 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20866 had a non-zero discriminator.
20867
20868 We have to be careful in the presence of discriminators.
20869 E.g., for this line:
20870
20871 for (i = 0; i < 100000; i++);
20872
20873 clang can emit four line number entries for that one line,
20874 each with a different discriminator.
20875 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20876
20877 However, we want gdb to coalesce all four entries into one.
20878 Otherwise the user could stepi into the middle of the line and
20879 gdb would get confused about whether the pc really was in the
20880 middle of the line.
20881
20882 Things are further complicated by the fact that two consecutive
20883 line number entries for the same line is a heuristic used by gcc
20884 to denote the end of the prologue. So we can't just discard duplicate
20885 entries, we have to be selective about it. The heuristic we use is
20886 that we only collapse consecutive entries for the same line if at least
20887 one of those entries has a non-zero discriminator. PR 17276.
20888
20889 Note: Addresses in the line number state machine can never go backwards
20890 within one sequence, thus this coalescing is ok. */
20891
20892 static int
20893 dwarf_record_line_p (struct dwarf2_cu *cu,
20894 unsigned int line, unsigned int last_line,
20895 int line_has_non_zero_discriminator,
20896 struct subfile *last_subfile)
20897 {
20898 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20899 return 1;
20900 if (line != last_line)
20901 return 1;
20902 /* Same line for the same file that we've seen already.
20903 As a last check, for pr 17276, only record the line if the line
20904 has never had a non-zero discriminator. */
20905 if (!line_has_non_zero_discriminator)
20906 return 1;
20907 return 0;
20908 }
20909
20910 /* Use the CU's builder to record line number LINE beginning at
20911 address ADDRESS in the line table of subfile SUBFILE. */
20912
20913 static void
20914 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20915 unsigned int line, CORE_ADDR address, bool is_stmt,
20916 struct dwarf2_cu *cu)
20917 {
20918 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20919
20920 if (dwarf_line_debug)
20921 {
20922 fprintf_unfiltered (gdb_stdlog,
20923 "Recording line %u, file %s, address %s\n",
20924 line, lbasename (subfile->name),
20925 paddress (gdbarch, address));
20926 }
20927
20928 if (cu != nullptr)
20929 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20930 }
20931
20932 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20933 Mark the end of a set of line number records.
20934 The arguments are the same as for dwarf_record_line_1.
20935 If SUBFILE is NULL the request is ignored. */
20936
20937 static void
20938 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20939 CORE_ADDR address, struct dwarf2_cu *cu)
20940 {
20941 if (subfile == NULL)
20942 return;
20943
20944 if (dwarf_line_debug)
20945 {
20946 fprintf_unfiltered (gdb_stdlog,
20947 "Finishing current line, file %s, address %s\n",
20948 lbasename (subfile->name),
20949 paddress (gdbarch, address));
20950 }
20951
20952 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20953 }
20954
20955 void
20956 lnp_state_machine::record_line (bool end_sequence)
20957 {
20958 if (dwarf_line_debug)
20959 {
20960 fprintf_unfiltered (gdb_stdlog,
20961 "Processing actual line %u: file %u,"
20962 " address %s, is_stmt %u, discrim %u%s\n",
20963 m_line, m_file,
20964 paddress (m_gdbarch, m_address),
20965 m_is_stmt, m_discriminator,
20966 (end_sequence ? "\t(end sequence)" : ""));
20967 }
20968
20969 file_entry *fe = current_file ();
20970
20971 if (fe == NULL)
20972 dwarf2_debug_line_missing_file_complaint ();
20973 /* For now we ignore lines not starting on an instruction boundary.
20974 But not when processing end_sequence for compatibility with the
20975 previous version of the code. */
20976 else if (m_op_index == 0 || end_sequence)
20977 {
20978 fe->included_p = 1;
20979 if (m_record_lines_p)
20980 {
20981 /* When we switch files we insert an end maker in the first file,
20982 switch to the second file and add a new line entry. The
20983 problem is that the end marker inserted in the first file will
20984 discard any previous line entries at the same address. If the
20985 line entries in the first file are marked as is-stmt, while
20986 the new line in the second file is non-stmt, then this means
20987 the end marker will discard is-stmt lines so we can have a
20988 non-stmt line. This means that there are less addresses at
20989 which the user can insert a breakpoint.
20990
20991 To improve this we track the last address in m_last_address,
20992 and whether we have seen an is-stmt at this address. Then
20993 when switching files, if we have seen a stmt at the current
20994 address, and we are switching to create a non-stmt line, then
20995 discard the new line. */
20996 bool file_changed
20997 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20998 bool ignore_this_line
20999 = ((file_changed && !end_sequence && m_last_address == m_address
21000 && !m_is_stmt && m_stmt_at_address)
21001 || (!end_sequence && m_line == 0));
21002
21003 if ((file_changed && !ignore_this_line) || end_sequence)
21004 {
21005 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21006 m_currently_recording_lines ? m_cu : nullptr);
21007 }
21008
21009 if (!end_sequence && !ignore_this_line)
21010 {
21011 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21012
21013 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21014 m_line_has_non_zero_discriminator,
21015 m_last_subfile))
21016 {
21017 buildsym_compunit *builder = m_cu->get_builder ();
21018 dwarf_record_line_1 (m_gdbarch,
21019 builder->get_current_subfile (),
21020 m_line, m_address, is_stmt,
21021 m_currently_recording_lines ? m_cu : nullptr);
21022 }
21023 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21024 m_last_line = m_line;
21025 }
21026 }
21027 }
21028
21029 /* Track whether we have seen any m_is_stmt true at m_address in case we
21030 have multiple line table entries all at m_address. */
21031 if (m_last_address != m_address)
21032 {
21033 m_stmt_at_address = false;
21034 m_last_address = m_address;
21035 }
21036 m_stmt_at_address |= m_is_stmt;
21037 }
21038
21039 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21040 line_header *lh, bool record_lines_p)
21041 {
21042 m_cu = cu;
21043 m_gdbarch = arch;
21044 m_record_lines_p = record_lines_p;
21045 m_line_header = lh;
21046
21047 m_currently_recording_lines = true;
21048
21049 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21050 was a line entry for it so that the backend has a chance to adjust it
21051 and also record it in case it needs it. This is currently used by MIPS
21052 code, cf. `mips_adjust_dwarf2_line'. */
21053 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21054 m_is_stmt = lh->default_is_stmt;
21055 m_discriminator = 0;
21056
21057 m_last_address = m_address;
21058 m_stmt_at_address = false;
21059 }
21060
21061 void
21062 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21063 const gdb_byte *line_ptr,
21064 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21065 {
21066 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21067 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21068 located at 0x0. In this case, additionally check that if
21069 ADDRESS < UNRELOCATED_LOWPC. */
21070
21071 if ((address == 0 && address < unrelocated_lowpc)
21072 || address == (CORE_ADDR) -1)
21073 {
21074 /* This line table is for a function which has been
21075 GCd by the linker. Ignore it. PR gdb/12528 */
21076
21077 struct objfile *objfile = cu->per_objfile->objfile;
21078 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21079
21080 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21081 line_offset, objfile_name (objfile));
21082 m_currently_recording_lines = false;
21083 /* Note: m_currently_recording_lines is left as false until we see
21084 DW_LNE_end_sequence. */
21085 }
21086 }
21087
21088 /* Subroutine of dwarf_decode_lines to simplify it.
21089 Process the line number information in LH.
21090 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21091 program in order to set included_p for every referenced header. */
21092
21093 static void
21094 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21095 const int decode_for_pst_p, CORE_ADDR lowpc)
21096 {
21097 const gdb_byte *line_ptr, *extended_end;
21098 const gdb_byte *line_end;
21099 unsigned int bytes_read, extended_len;
21100 unsigned char op_code, extended_op;
21101 CORE_ADDR baseaddr;
21102 struct objfile *objfile = cu->per_objfile->objfile;
21103 bfd *abfd = objfile->obfd;
21104 struct gdbarch *gdbarch = objfile->arch ();
21105 /* True if we're recording line info (as opposed to building partial
21106 symtabs and just interested in finding include files mentioned by
21107 the line number program). */
21108 bool record_lines_p = !decode_for_pst_p;
21109
21110 baseaddr = objfile->text_section_offset ();
21111
21112 line_ptr = lh->statement_program_start;
21113 line_end = lh->statement_program_end;
21114
21115 /* Read the statement sequences until there's nothing left. */
21116 while (line_ptr < line_end)
21117 {
21118 /* The DWARF line number program state machine. Reset the state
21119 machine at the start of each sequence. */
21120 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21121 bool end_sequence = false;
21122
21123 if (record_lines_p)
21124 {
21125 /* Start a subfile for the current file of the state
21126 machine. */
21127 const file_entry *fe = state_machine.current_file ();
21128
21129 if (fe != NULL)
21130 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21131 }
21132
21133 /* Decode the table. */
21134 while (line_ptr < line_end && !end_sequence)
21135 {
21136 op_code = read_1_byte (abfd, line_ptr);
21137 line_ptr += 1;
21138
21139 if (op_code >= lh->opcode_base)
21140 {
21141 /* Special opcode. */
21142 state_machine.handle_special_opcode (op_code);
21143 }
21144 else switch (op_code)
21145 {
21146 case DW_LNS_extended_op:
21147 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21148 &bytes_read);
21149 line_ptr += bytes_read;
21150 extended_end = line_ptr + extended_len;
21151 extended_op = read_1_byte (abfd, line_ptr);
21152 line_ptr += 1;
21153 if (DW_LNE_lo_user <= extended_op
21154 && extended_op <= DW_LNE_hi_user)
21155 {
21156 /* Vendor extension, ignore. */
21157 line_ptr = extended_end;
21158 break;
21159 }
21160 switch (extended_op)
21161 {
21162 case DW_LNE_end_sequence:
21163 state_machine.handle_end_sequence ();
21164 end_sequence = true;
21165 break;
21166 case DW_LNE_set_address:
21167 {
21168 CORE_ADDR address
21169 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21170 line_ptr += bytes_read;
21171
21172 state_machine.check_line_address (cu, line_ptr,
21173 lowpc - baseaddr, address);
21174 state_machine.handle_set_address (baseaddr, address);
21175 }
21176 break;
21177 case DW_LNE_define_file:
21178 {
21179 const char *cur_file;
21180 unsigned int mod_time, length;
21181 dir_index dindex;
21182
21183 cur_file = read_direct_string (abfd, line_ptr,
21184 &bytes_read);
21185 line_ptr += bytes_read;
21186 dindex = (dir_index)
21187 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21188 line_ptr += bytes_read;
21189 mod_time =
21190 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21191 line_ptr += bytes_read;
21192 length =
21193 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21194 line_ptr += bytes_read;
21195 lh->add_file_name (cur_file, dindex, mod_time, length);
21196 }
21197 break;
21198 case DW_LNE_set_discriminator:
21199 {
21200 /* The discriminator is not interesting to the
21201 debugger; just ignore it. We still need to
21202 check its value though:
21203 if there are consecutive entries for the same
21204 (non-prologue) line we want to coalesce them.
21205 PR 17276. */
21206 unsigned int discr
21207 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21208 line_ptr += bytes_read;
21209
21210 state_machine.handle_set_discriminator (discr);
21211 }
21212 break;
21213 default:
21214 complaint (_("mangled .debug_line section"));
21215 return;
21216 }
21217 /* Make sure that we parsed the extended op correctly. If e.g.
21218 we expected a different address size than the producer used,
21219 we may have read the wrong number of bytes. */
21220 if (line_ptr != extended_end)
21221 {
21222 complaint (_("mangled .debug_line section"));
21223 return;
21224 }
21225 break;
21226 case DW_LNS_copy:
21227 state_machine.handle_copy ();
21228 break;
21229 case DW_LNS_advance_pc:
21230 {
21231 CORE_ADDR adjust
21232 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21233 line_ptr += bytes_read;
21234
21235 state_machine.handle_advance_pc (adjust);
21236 }
21237 break;
21238 case DW_LNS_advance_line:
21239 {
21240 int line_delta
21241 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21242 line_ptr += bytes_read;
21243
21244 state_machine.handle_advance_line (line_delta);
21245 }
21246 break;
21247 case DW_LNS_set_file:
21248 {
21249 file_name_index file
21250 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21251 &bytes_read);
21252 line_ptr += bytes_read;
21253
21254 state_machine.handle_set_file (file);
21255 }
21256 break;
21257 case DW_LNS_set_column:
21258 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21259 line_ptr += bytes_read;
21260 break;
21261 case DW_LNS_negate_stmt:
21262 state_machine.handle_negate_stmt ();
21263 break;
21264 case DW_LNS_set_basic_block:
21265 break;
21266 /* Add to the address register of the state machine the
21267 address increment value corresponding to special opcode
21268 255. I.e., this value is scaled by the minimum
21269 instruction length since special opcode 255 would have
21270 scaled the increment. */
21271 case DW_LNS_const_add_pc:
21272 state_machine.handle_const_add_pc ();
21273 break;
21274 case DW_LNS_fixed_advance_pc:
21275 {
21276 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21277 line_ptr += 2;
21278
21279 state_machine.handle_fixed_advance_pc (addr_adj);
21280 }
21281 break;
21282 default:
21283 {
21284 /* Unknown standard opcode, ignore it. */
21285 int i;
21286
21287 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21288 {
21289 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21290 line_ptr += bytes_read;
21291 }
21292 }
21293 }
21294 }
21295
21296 if (!end_sequence)
21297 dwarf2_debug_line_missing_end_sequence_complaint ();
21298
21299 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21300 in which case we still finish recording the last line). */
21301 state_machine.record_line (true);
21302 }
21303 }
21304
21305 /* Decode the Line Number Program (LNP) for the given line_header
21306 structure and CU. The actual information extracted and the type
21307 of structures created from the LNP depends on the value of PST.
21308
21309 1. If PST is NULL, then this procedure uses the data from the program
21310 to create all necessary symbol tables, and their linetables.
21311
21312 2. If PST is not NULL, this procedure reads the program to determine
21313 the list of files included by the unit represented by PST, and
21314 builds all the associated partial symbol tables.
21315
21316 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21317 It is used for relative paths in the line table.
21318 NOTE: When processing partial symtabs (pst != NULL),
21319 comp_dir == pst->dirname.
21320
21321 NOTE: It is important that psymtabs have the same file name (via strcmp)
21322 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21323 symtab we don't use it in the name of the psymtabs we create.
21324 E.g. expand_line_sal requires this when finding psymtabs to expand.
21325 A good testcase for this is mb-inline.exp.
21326
21327 LOWPC is the lowest address in CU (or 0 if not known).
21328
21329 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21330 for its PC<->lines mapping information. Otherwise only the filename
21331 table is read in. */
21332
21333 static void
21334 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21335 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21336 CORE_ADDR lowpc, int decode_mapping)
21337 {
21338 struct objfile *objfile = cu->per_objfile->objfile;
21339 const int decode_for_pst_p = (pst != NULL);
21340
21341 if (decode_mapping)
21342 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21343
21344 if (decode_for_pst_p)
21345 {
21346 /* Now that we're done scanning the Line Header Program, we can
21347 create the psymtab of each included file. */
21348 for (auto &file_entry : lh->file_names ())
21349 if (file_entry.included_p == 1)
21350 {
21351 gdb::unique_xmalloc_ptr<char> name_holder;
21352 const char *include_name =
21353 psymtab_include_file_name (lh, file_entry, pst,
21354 comp_dir, &name_holder);
21355 if (include_name != NULL)
21356 dwarf2_create_include_psymtab
21357 (cu->per_objfile->per_bfd, include_name, pst,
21358 cu->per_objfile->per_bfd->partial_symtabs.get (),
21359 objfile->per_bfd);
21360 }
21361 }
21362 else
21363 {
21364 /* Make sure a symtab is created for every file, even files
21365 which contain only variables (i.e. no code with associated
21366 line numbers). */
21367 buildsym_compunit *builder = cu->get_builder ();
21368 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21369
21370 for (auto &fe : lh->file_names ())
21371 {
21372 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21373 if (builder->get_current_subfile ()->symtab == NULL)
21374 {
21375 builder->get_current_subfile ()->symtab
21376 = allocate_symtab (cust,
21377 builder->get_current_subfile ()->name);
21378 }
21379 fe.symtab = builder->get_current_subfile ()->symtab;
21380 }
21381 }
21382 }
21383
21384 /* Start a subfile for DWARF. FILENAME is the name of the file and
21385 DIRNAME the name of the source directory which contains FILENAME
21386 or NULL if not known.
21387 This routine tries to keep line numbers from identical absolute and
21388 relative file names in a common subfile.
21389
21390 Using the `list' example from the GDB testsuite, which resides in
21391 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21392 of /srcdir/list0.c yields the following debugging information for list0.c:
21393
21394 DW_AT_name: /srcdir/list0.c
21395 DW_AT_comp_dir: /compdir
21396 files.files[0].name: list0.h
21397 files.files[0].dir: /srcdir
21398 files.files[1].name: list0.c
21399 files.files[1].dir: /srcdir
21400
21401 The line number information for list0.c has to end up in a single
21402 subfile, so that `break /srcdir/list0.c:1' works as expected.
21403 start_subfile will ensure that this happens provided that we pass the
21404 concatenation of files.files[1].dir and files.files[1].name as the
21405 subfile's name. */
21406
21407 static void
21408 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21409 const char *dirname)
21410 {
21411 gdb::unique_xmalloc_ptr<char> copy;
21412
21413 /* In order not to lose the line information directory,
21414 we concatenate it to the filename when it makes sense.
21415 Note that the Dwarf3 standard says (speaking of filenames in line
21416 information): ``The directory index is ignored for file names
21417 that represent full path names''. Thus ignoring dirname in the
21418 `else' branch below isn't an issue. */
21419
21420 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21421 {
21422 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21423 filename = copy.get ();
21424 }
21425
21426 cu->get_builder ()->start_subfile (filename);
21427 }
21428
21429 static void
21430 var_decode_location (struct attribute *attr, struct symbol *sym,
21431 struct dwarf2_cu *cu)
21432 {
21433 struct objfile *objfile = cu->per_objfile->objfile;
21434 struct comp_unit_head *cu_header = &cu->header;
21435
21436 /* NOTE drow/2003-01-30: There used to be a comment and some special
21437 code here to turn a symbol with DW_AT_external and a
21438 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21439 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21440 with some versions of binutils) where shared libraries could have
21441 relocations against symbols in their debug information - the
21442 minimal symbol would have the right address, but the debug info
21443 would not. It's no longer necessary, because we will explicitly
21444 apply relocations when we read in the debug information now. */
21445
21446 /* A DW_AT_location attribute with no contents indicates that a
21447 variable has been optimized away. */
21448 if (attr->form_is_block () && attr->as_block ()->size == 0)
21449 {
21450 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21451 return;
21452 }
21453
21454 /* Handle one degenerate form of location expression specially, to
21455 preserve GDB's previous behavior when section offsets are
21456 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21457 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21458
21459 if (attr->form_is_block ())
21460 {
21461 struct dwarf_block *block = attr->as_block ();
21462
21463 if ((block->data[0] == DW_OP_addr
21464 && block->size == 1 + cu_header->addr_size)
21465 || ((block->data[0] == DW_OP_GNU_addr_index
21466 || block->data[0] == DW_OP_addrx)
21467 && (block->size
21468 == 1 + leb128_size (&block->data[1]))))
21469 {
21470 unsigned int dummy;
21471
21472 if (block->data[0] == DW_OP_addr)
21473 SET_SYMBOL_VALUE_ADDRESS
21474 (sym, cu->header.read_address (objfile->obfd,
21475 block->data + 1,
21476 &dummy));
21477 else
21478 SET_SYMBOL_VALUE_ADDRESS
21479 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21480 &dummy));
21481 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21482 fixup_symbol_section (sym, objfile);
21483 SET_SYMBOL_VALUE_ADDRESS
21484 (sym,
21485 SYMBOL_VALUE_ADDRESS (sym)
21486 + objfile->section_offsets[sym->section_index ()]);
21487 return;
21488 }
21489 }
21490
21491 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21492 expression evaluator, and use LOC_COMPUTED only when necessary
21493 (i.e. when the value of a register or memory location is
21494 referenced, or a thread-local block, etc.). Then again, it might
21495 not be worthwhile. I'm assuming that it isn't unless performance
21496 or memory numbers show me otherwise. */
21497
21498 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21499
21500 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21501 cu->has_loclist = true;
21502 }
21503
21504 /* Given a pointer to a DWARF information entry, figure out if we need
21505 to make a symbol table entry for it, and if so, create a new entry
21506 and return a pointer to it.
21507 If TYPE is NULL, determine symbol type from the die, otherwise
21508 used the passed type.
21509 If SPACE is not NULL, use it to hold the new symbol. If it is
21510 NULL, allocate a new symbol on the objfile's obstack. */
21511
21512 static struct symbol *
21513 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21514 struct symbol *space)
21515 {
21516 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21517 struct objfile *objfile = per_objfile->objfile;
21518 struct gdbarch *gdbarch = objfile->arch ();
21519 struct symbol *sym = NULL;
21520 const char *name;
21521 struct attribute *attr = NULL;
21522 struct attribute *attr2 = NULL;
21523 CORE_ADDR baseaddr;
21524 struct pending **list_to_add = NULL;
21525
21526 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21527
21528 baseaddr = objfile->text_section_offset ();
21529
21530 name = dwarf2_name (die, cu);
21531 if (name)
21532 {
21533 int suppress_add = 0;
21534
21535 if (space)
21536 sym = space;
21537 else
21538 sym = new (&objfile->objfile_obstack) symbol;
21539 OBJSTAT (objfile, n_syms++);
21540
21541 /* Cache this symbol's name and the name's demangled form (if any). */
21542 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
21543 /* Fortran does not have mangling standard and the mangling does differ
21544 between gfortran, iFort etc. */
21545 const char *physname
21546 = (cu->per_cu->lang == language_fortran
21547 ? dwarf2_full_name (name, die, cu)
21548 : dwarf2_physname (name, die, cu));
21549 const char *linkagename = dw2_linkage_name (die, cu);
21550
21551 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
21552 sym->set_linkage_name (physname);
21553 else
21554 {
21555 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21556 sym->set_linkage_name (linkagename);
21557 }
21558
21559 /* Default assumptions.
21560 Use the passed type or decode it from the die. */
21561 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21562 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21563 if (type != NULL)
21564 SYMBOL_TYPE (sym) = type;
21565 else
21566 SYMBOL_TYPE (sym) = die_type (die, cu);
21567 attr = dwarf2_attr (die,
21568 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21569 cu);
21570 if (attr != nullptr)
21571 SYMBOL_LINE (sym) = attr->constant_value (0);
21572
21573 attr = dwarf2_attr (die,
21574 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21575 cu);
21576 if (attr != nullptr && attr->is_nonnegative ())
21577 {
21578 file_name_index file_index
21579 = (file_name_index) attr->as_nonnegative ();
21580 struct file_entry *fe;
21581
21582 if (cu->line_header != NULL)
21583 fe = cu->line_header->file_name_at (file_index);
21584 else
21585 fe = NULL;
21586
21587 if (fe == NULL)
21588 complaint (_("file index out of range"));
21589 else
21590 symbol_set_symtab (sym, fe->symtab);
21591 }
21592
21593 switch (die->tag)
21594 {
21595 case DW_TAG_label:
21596 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21597 if (attr != nullptr)
21598 {
21599 CORE_ADDR addr;
21600
21601 addr = attr->as_address ();
21602 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21603 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21604 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21605 }
21606 else
21607 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21608 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21609 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21610 add_symbol_to_list (sym, cu->list_in_scope);
21611 break;
21612 case DW_TAG_subprogram:
21613 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21614 finish_block. */
21615 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21616 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21617 if ((attr2 != nullptr && attr2->as_boolean ())
21618 || cu->per_cu->lang == language_ada
21619 || cu->per_cu->lang == language_fortran)
21620 {
21621 /* Subprograms marked external are stored as a global symbol.
21622 Ada and Fortran subprograms, whether marked external or
21623 not, are always stored as a global symbol, because we want
21624 to be able to access them globally. For instance, we want
21625 to be able to break on a nested subprogram without having
21626 to specify the context. */
21627 list_to_add = cu->get_builder ()->get_global_symbols ();
21628 }
21629 else
21630 {
21631 list_to_add = cu->list_in_scope;
21632 }
21633 break;
21634 case DW_TAG_inlined_subroutine:
21635 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21636 finish_block. */
21637 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21638 SYMBOL_INLINED (sym) = 1;
21639 list_to_add = cu->list_in_scope;
21640 break;
21641 case DW_TAG_template_value_param:
21642 suppress_add = 1;
21643 /* Fall through. */
21644 case DW_TAG_constant:
21645 case DW_TAG_variable:
21646 case DW_TAG_member:
21647 /* Compilation with minimal debug info may result in
21648 variables with missing type entries. Change the
21649 misleading `void' type to something sensible. */
21650 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21651 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21652
21653 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21654 /* In the case of DW_TAG_member, we should only be called for
21655 static const members. */
21656 if (die->tag == DW_TAG_member)
21657 {
21658 /* dwarf2_add_field uses die_is_declaration,
21659 so we do the same. */
21660 gdb_assert (die_is_declaration (die, cu));
21661 gdb_assert (attr);
21662 }
21663 if (attr != nullptr)
21664 {
21665 dwarf2_const_value (attr, sym, cu);
21666 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21667 if (!suppress_add)
21668 {
21669 if (attr2 != nullptr && attr2->as_boolean ())
21670 list_to_add = cu->get_builder ()->get_global_symbols ();
21671 else
21672 list_to_add = cu->list_in_scope;
21673 }
21674 break;
21675 }
21676 attr = dwarf2_attr (die, DW_AT_location, cu);
21677 if (attr != nullptr)
21678 {
21679 var_decode_location (attr, sym, cu);
21680 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21681
21682 /* Fortran explicitly imports any global symbols to the local
21683 scope by DW_TAG_common_block. */
21684 if (cu->per_cu->lang == language_fortran && die->parent
21685 && die->parent->tag == DW_TAG_common_block)
21686 attr2 = NULL;
21687
21688 if (SYMBOL_CLASS (sym) == LOC_STATIC
21689 && SYMBOL_VALUE_ADDRESS (sym) == 0
21690 && !per_objfile->per_bfd->has_section_at_zero)
21691 {
21692 /* When a static variable is eliminated by the linker,
21693 the corresponding debug information is not stripped
21694 out, but the variable address is set to null;
21695 do not add such variables into symbol table. */
21696 }
21697 else if (attr2 != nullptr && attr2->as_boolean ())
21698 {
21699 if (SYMBOL_CLASS (sym) == LOC_STATIC
21700 && (objfile->flags & OBJF_MAINLINE) == 0
21701 && per_objfile->per_bfd->can_copy)
21702 {
21703 /* A global static variable might be subject to
21704 copy relocation. We first check for a local
21705 minsym, though, because maybe the symbol was
21706 marked hidden, in which case this would not
21707 apply. */
21708 bound_minimal_symbol found
21709 = (lookup_minimal_symbol_linkage
21710 (sym->linkage_name (), objfile));
21711 if (found.minsym != nullptr)
21712 sym->maybe_copied = 1;
21713 }
21714
21715 /* A variable with DW_AT_external is never static,
21716 but it may be block-scoped. */
21717 list_to_add
21718 = ((cu->list_in_scope
21719 == cu->get_builder ()->get_file_symbols ())
21720 ? cu->get_builder ()->get_global_symbols ()
21721 : cu->list_in_scope);
21722 }
21723 else
21724 list_to_add = cu->list_in_scope;
21725 }
21726 else
21727 {
21728 /* We do not know the address of this symbol.
21729 If it is an external symbol and we have type information
21730 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21731 The address of the variable will then be determined from
21732 the minimal symbol table whenever the variable is
21733 referenced. */
21734 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21735
21736 /* Fortran explicitly imports any global symbols to the local
21737 scope by DW_TAG_common_block. */
21738 if (cu->per_cu->lang == language_fortran && die->parent
21739 && die->parent->tag == DW_TAG_common_block)
21740 {
21741 /* SYMBOL_CLASS doesn't matter here because
21742 read_common_block is going to reset it. */
21743 if (!suppress_add)
21744 list_to_add = cu->list_in_scope;
21745 }
21746 else if (attr2 != nullptr && attr2->as_boolean ()
21747 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21748 {
21749 /* A variable with DW_AT_external is never static, but it
21750 may be block-scoped. */
21751 list_to_add
21752 = ((cu->list_in_scope
21753 == cu->get_builder ()->get_file_symbols ())
21754 ? cu->get_builder ()->get_global_symbols ()
21755 : cu->list_in_scope);
21756
21757 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21758 }
21759 else if (!die_is_declaration (die, cu))
21760 {
21761 /* Use the default LOC_OPTIMIZED_OUT class. */
21762 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21763 if (!suppress_add)
21764 list_to_add = cu->list_in_scope;
21765 }
21766 }
21767 break;
21768 case DW_TAG_formal_parameter:
21769 {
21770 /* If we are inside a function, mark this as an argument. If
21771 not, we might be looking at an argument to an inlined function
21772 when we do not have enough information to show inlined frames;
21773 pretend it's a local variable in that case so that the user can
21774 still see it. */
21775 struct context_stack *curr
21776 = cu->get_builder ()->get_current_context_stack ();
21777 if (curr != nullptr && curr->name != nullptr)
21778 SYMBOL_IS_ARGUMENT (sym) = 1;
21779 attr = dwarf2_attr (die, DW_AT_location, cu);
21780 if (attr != nullptr)
21781 {
21782 var_decode_location (attr, sym, cu);
21783 }
21784 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21785 if (attr != nullptr)
21786 {
21787 dwarf2_const_value (attr, sym, cu);
21788 }
21789
21790 list_to_add = cu->list_in_scope;
21791 }
21792 break;
21793 case DW_TAG_unspecified_parameters:
21794 /* From varargs functions; gdb doesn't seem to have any
21795 interest in this information, so just ignore it for now.
21796 (FIXME?) */
21797 break;
21798 case DW_TAG_template_type_param:
21799 suppress_add = 1;
21800 /* Fall through. */
21801 case DW_TAG_class_type:
21802 case DW_TAG_interface_type:
21803 case DW_TAG_structure_type:
21804 case DW_TAG_union_type:
21805 case DW_TAG_set_type:
21806 case DW_TAG_enumeration_type:
21807 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21808 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21809
21810 {
21811 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21812 really ever be static objects: otherwise, if you try
21813 to, say, break of a class's method and you're in a file
21814 which doesn't mention that class, it won't work unless
21815 the check for all static symbols in lookup_symbol_aux
21816 saves you. See the OtherFileClass tests in
21817 gdb.c++/namespace.exp. */
21818
21819 if (!suppress_add)
21820 {
21821 buildsym_compunit *builder = cu->get_builder ();
21822 list_to_add
21823 = (cu->list_in_scope == builder->get_file_symbols ()
21824 && cu->per_cu->lang == language_cplus
21825 ? builder->get_global_symbols ()
21826 : cu->list_in_scope);
21827
21828 /* The semantics of C++ state that "struct foo {
21829 ... }" also defines a typedef for "foo". */
21830 if (cu->per_cu->lang == language_cplus
21831 || cu->per_cu->lang == language_ada
21832 || cu->per_cu->lang == language_d
21833 || cu->per_cu->lang == language_rust)
21834 {
21835 /* The symbol's name is already allocated along
21836 with this objfile, so we don't need to
21837 duplicate it for the type. */
21838 if (SYMBOL_TYPE (sym)->name () == 0)
21839 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21840 }
21841 }
21842 }
21843 break;
21844 case DW_TAG_typedef:
21845 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21846 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21847 list_to_add = cu->list_in_scope;
21848 break;
21849 case DW_TAG_array_type:
21850 case DW_TAG_base_type:
21851 case DW_TAG_subrange_type:
21852 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21853 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21854 list_to_add = cu->list_in_scope;
21855 break;
21856 case DW_TAG_enumerator:
21857 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21858 if (attr != nullptr)
21859 {
21860 dwarf2_const_value (attr, sym, cu);
21861 }
21862 {
21863 /* NOTE: carlton/2003-11-10: See comment above in the
21864 DW_TAG_class_type, etc. block. */
21865
21866 list_to_add
21867 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21868 && cu->per_cu->lang == language_cplus
21869 ? cu->get_builder ()->get_global_symbols ()
21870 : cu->list_in_scope);
21871 }
21872 break;
21873 case DW_TAG_imported_declaration:
21874 case DW_TAG_namespace:
21875 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21876 list_to_add = cu->get_builder ()->get_global_symbols ();
21877 break;
21878 case DW_TAG_module:
21879 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21880 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21881 list_to_add = cu->get_builder ()->get_global_symbols ();
21882 break;
21883 case DW_TAG_common_block:
21884 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21885 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21886 add_symbol_to_list (sym, cu->list_in_scope);
21887 break;
21888 default:
21889 /* Not a tag we recognize. Hopefully we aren't processing
21890 trash data, but since we must specifically ignore things
21891 we don't recognize, there is nothing else we should do at
21892 this point. */
21893 complaint (_("unsupported tag: '%s'"),
21894 dwarf_tag_name (die->tag));
21895 break;
21896 }
21897
21898 if (suppress_add)
21899 {
21900 sym->hash_next = objfile->template_symbols;
21901 objfile->template_symbols = sym;
21902 list_to_add = NULL;
21903 }
21904
21905 if (list_to_add != NULL)
21906 add_symbol_to_list (sym, list_to_add);
21907
21908 /* For the benefit of old versions of GCC, check for anonymous
21909 namespaces based on the demangled name. */
21910 if (!cu->processing_has_namespace_info
21911 && cu->per_cu->lang == language_cplus)
21912 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21913 }
21914 return (sym);
21915 }
21916
21917 /* Given an attr with a DW_FORM_dataN value in host byte order,
21918 zero-extend it as appropriate for the symbol's type. The DWARF
21919 standard (v4) is not entirely clear about the meaning of using
21920 DW_FORM_dataN for a constant with a signed type, where the type is
21921 wider than the data. The conclusion of a discussion on the DWARF
21922 list was that this is unspecified. We choose to always zero-extend
21923 because that is the interpretation long in use by GCC. */
21924
21925 static gdb_byte *
21926 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21927 struct dwarf2_cu *cu, LONGEST *value, int bits)
21928 {
21929 struct objfile *objfile = cu->per_objfile->objfile;
21930 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21931 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21932 LONGEST l = attr->constant_value (0);
21933
21934 if (bits < sizeof (*value) * 8)
21935 {
21936 l &= ((LONGEST) 1 << bits) - 1;
21937 *value = l;
21938 }
21939 else if (bits == sizeof (*value) * 8)
21940 *value = l;
21941 else
21942 {
21943 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21944 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21945 return bytes;
21946 }
21947
21948 return NULL;
21949 }
21950
21951 /* Read a constant value from an attribute. Either set *VALUE, or if
21952 the value does not fit in *VALUE, set *BYTES - either already
21953 allocated on the objfile obstack, or newly allocated on OBSTACK,
21954 or, set *BATON, if we translated the constant to a location
21955 expression. */
21956
21957 static void
21958 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21959 const char *name, struct obstack *obstack,
21960 struct dwarf2_cu *cu,
21961 LONGEST *value, const gdb_byte **bytes,
21962 struct dwarf2_locexpr_baton **baton)
21963 {
21964 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21965 struct objfile *objfile = per_objfile->objfile;
21966 struct comp_unit_head *cu_header = &cu->header;
21967 struct dwarf_block *blk;
21968 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21969 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21970
21971 *value = 0;
21972 *bytes = NULL;
21973 *baton = NULL;
21974
21975 switch (attr->form)
21976 {
21977 case DW_FORM_addr:
21978 case DW_FORM_addrx:
21979 case DW_FORM_GNU_addr_index:
21980 {
21981 gdb_byte *data;
21982
21983 if (TYPE_LENGTH (type) != cu_header->addr_size)
21984 dwarf2_const_value_length_mismatch_complaint (name,
21985 cu_header->addr_size,
21986 TYPE_LENGTH (type));
21987 /* Symbols of this form are reasonably rare, so we just
21988 piggyback on the existing location code rather than writing
21989 a new implementation of symbol_computed_ops. */
21990 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21991 (*baton)->per_objfile = per_objfile;
21992 (*baton)->per_cu = cu->per_cu;
21993 gdb_assert ((*baton)->per_cu);
21994
21995 (*baton)->size = 2 + cu_header->addr_size;
21996 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21997 (*baton)->data = data;
21998
21999 data[0] = DW_OP_addr;
22000 store_unsigned_integer (&data[1], cu_header->addr_size,
22001 byte_order, attr->as_address ());
22002 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22003 }
22004 break;
22005 case DW_FORM_string:
22006 case DW_FORM_strp:
22007 case DW_FORM_strx:
22008 case DW_FORM_GNU_str_index:
22009 case DW_FORM_GNU_strp_alt:
22010 /* The string is already allocated on the objfile obstack, point
22011 directly to it. */
22012 *bytes = (const gdb_byte *) attr->as_string ();
22013 break;
22014 case DW_FORM_block1:
22015 case DW_FORM_block2:
22016 case DW_FORM_block4:
22017 case DW_FORM_block:
22018 case DW_FORM_exprloc:
22019 case DW_FORM_data16:
22020 blk = attr->as_block ();
22021 if (TYPE_LENGTH (type) != blk->size)
22022 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22023 TYPE_LENGTH (type));
22024 *bytes = blk->data;
22025 break;
22026
22027 /* The DW_AT_const_value attributes are supposed to carry the
22028 symbol's value "represented as it would be on the target
22029 architecture." By the time we get here, it's already been
22030 converted to host endianness, so we just need to sign- or
22031 zero-extend it as appropriate. */
22032 case DW_FORM_data1:
22033 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22034 break;
22035 case DW_FORM_data2:
22036 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22037 break;
22038 case DW_FORM_data4:
22039 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22040 break;
22041 case DW_FORM_data8:
22042 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22043 break;
22044
22045 case DW_FORM_sdata:
22046 case DW_FORM_implicit_const:
22047 *value = attr->as_signed ();
22048 break;
22049
22050 case DW_FORM_udata:
22051 *value = attr->as_unsigned ();
22052 break;
22053
22054 default:
22055 complaint (_("unsupported const value attribute form: '%s'"),
22056 dwarf_form_name (attr->form));
22057 *value = 0;
22058 break;
22059 }
22060 }
22061
22062
22063 /* Copy constant value from an attribute to a symbol. */
22064
22065 static void
22066 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22067 struct dwarf2_cu *cu)
22068 {
22069 struct objfile *objfile = cu->per_objfile->objfile;
22070 LONGEST value;
22071 const gdb_byte *bytes;
22072 struct dwarf2_locexpr_baton *baton;
22073
22074 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22075 sym->print_name (),
22076 &objfile->objfile_obstack, cu,
22077 &value, &bytes, &baton);
22078
22079 if (baton != NULL)
22080 {
22081 SYMBOL_LOCATION_BATON (sym) = baton;
22082 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22083 }
22084 else if (bytes != NULL)
22085 {
22086 SYMBOL_VALUE_BYTES (sym) = bytes;
22087 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22088 }
22089 else
22090 {
22091 SYMBOL_VALUE (sym) = value;
22092 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22093 }
22094 }
22095
22096 /* Return the type of the die in question using its DW_AT_type attribute. */
22097
22098 static struct type *
22099 die_type (struct die_info *die, struct dwarf2_cu *cu)
22100 {
22101 struct attribute *type_attr;
22102
22103 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22104 if (!type_attr)
22105 {
22106 struct objfile *objfile = cu->per_objfile->objfile;
22107 /* A missing DW_AT_type represents a void type. */
22108 return objfile_type (objfile)->builtin_void;
22109 }
22110
22111 return lookup_die_type (die, type_attr, cu);
22112 }
22113
22114 /* True iff CU's producer generates GNAT Ada auxiliary information
22115 that allows to find parallel types through that information instead
22116 of having to do expensive parallel lookups by type name. */
22117
22118 static int
22119 need_gnat_info (struct dwarf2_cu *cu)
22120 {
22121 /* Assume that the Ada compiler was GNAT, which always produces
22122 the auxiliary information. */
22123 return (cu->per_cu->lang == language_ada);
22124 }
22125
22126 /* Return the auxiliary type of the die in question using its
22127 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22128 attribute is not present. */
22129
22130 static struct type *
22131 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22132 {
22133 struct attribute *type_attr;
22134
22135 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22136 if (!type_attr)
22137 return NULL;
22138
22139 return lookup_die_type (die, type_attr, cu);
22140 }
22141
22142 /* If DIE has a descriptive_type attribute, then set the TYPE's
22143 descriptive type accordingly. */
22144
22145 static void
22146 set_descriptive_type (struct type *type, struct die_info *die,
22147 struct dwarf2_cu *cu)
22148 {
22149 struct type *descriptive_type = die_descriptive_type (die, cu);
22150
22151 if (descriptive_type)
22152 {
22153 ALLOCATE_GNAT_AUX_TYPE (type);
22154 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22155 }
22156 }
22157
22158 /* Return the containing type of the die in question using its
22159 DW_AT_containing_type attribute. */
22160
22161 static struct type *
22162 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22163 {
22164 struct attribute *type_attr;
22165 struct objfile *objfile = cu->per_objfile->objfile;
22166
22167 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22168 if (!type_attr)
22169 error (_("Dwarf Error: Problem turning containing type into gdb type "
22170 "[in module %s]"), objfile_name (objfile));
22171
22172 return lookup_die_type (die, type_attr, cu);
22173 }
22174
22175 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22176
22177 static struct type *
22178 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22179 {
22180 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22181 struct objfile *objfile = per_objfile->objfile;
22182 char *saved;
22183
22184 std::string message
22185 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22186 objfile_name (objfile),
22187 sect_offset_str (cu->header.sect_off),
22188 sect_offset_str (die->sect_off));
22189 saved = obstack_strdup (&objfile->objfile_obstack, message);
22190
22191 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22192 }
22193
22194 /* Look up the type of DIE in CU using its type attribute ATTR.
22195 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22196 DW_AT_containing_type.
22197 If there is no type substitute an error marker. */
22198
22199 static struct type *
22200 lookup_die_type (struct die_info *die, const struct attribute *attr,
22201 struct dwarf2_cu *cu)
22202 {
22203 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22204 struct objfile *objfile = per_objfile->objfile;
22205 struct type *this_type;
22206
22207 gdb_assert (attr->name == DW_AT_type
22208 || attr->name == DW_AT_GNAT_descriptive_type
22209 || attr->name == DW_AT_containing_type);
22210
22211 /* First see if we have it cached. */
22212
22213 if (attr->form == DW_FORM_GNU_ref_alt)
22214 {
22215 struct dwarf2_per_cu_data *per_cu;
22216 sect_offset sect_off = attr->get_ref_die_offset ();
22217
22218 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22219 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22220 }
22221 else if (attr->form_is_ref ())
22222 {
22223 sect_offset sect_off = attr->get_ref_die_offset ();
22224
22225 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22226 }
22227 else if (attr->form == DW_FORM_ref_sig8)
22228 {
22229 ULONGEST signature = attr->as_signature ();
22230
22231 return get_signatured_type (die, signature, cu);
22232 }
22233 else
22234 {
22235 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22236 " at %s [in module %s]"),
22237 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22238 objfile_name (objfile));
22239 return build_error_marker_type (cu, die);
22240 }
22241
22242 /* If not cached we need to read it in. */
22243
22244 if (this_type == NULL)
22245 {
22246 struct die_info *type_die = NULL;
22247 struct dwarf2_cu *type_cu = cu;
22248
22249 if (attr->form_is_ref ())
22250 type_die = follow_die_ref (die, attr, &type_cu);
22251 if (type_die == NULL)
22252 return build_error_marker_type (cu, die);
22253 /* If we find the type now, it's probably because the type came
22254 from an inter-CU reference and the type's CU got expanded before
22255 ours. */
22256 this_type = read_type_die (type_die, type_cu);
22257 }
22258
22259 /* If we still don't have a type use an error marker. */
22260
22261 if (this_type == NULL)
22262 return build_error_marker_type (cu, die);
22263
22264 return this_type;
22265 }
22266
22267 /* Return the type in DIE, CU.
22268 Returns NULL for invalid types.
22269
22270 This first does a lookup in die_type_hash,
22271 and only reads the die in if necessary.
22272
22273 NOTE: This can be called when reading in partial or full symbols. */
22274
22275 static struct type *
22276 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22277 {
22278 struct type *this_type;
22279
22280 this_type = get_die_type (die, cu);
22281 if (this_type)
22282 return this_type;
22283
22284 return read_type_die_1 (die, cu);
22285 }
22286
22287 /* Read the type in DIE, CU.
22288 Returns NULL for invalid types. */
22289
22290 static struct type *
22291 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22292 {
22293 struct type *this_type = NULL;
22294
22295 switch (die->tag)
22296 {
22297 case DW_TAG_class_type:
22298 case DW_TAG_interface_type:
22299 case DW_TAG_structure_type:
22300 case DW_TAG_union_type:
22301 this_type = read_structure_type (die, cu);
22302 break;
22303 case DW_TAG_enumeration_type:
22304 this_type = read_enumeration_type (die, cu);
22305 break;
22306 case DW_TAG_subprogram:
22307 case DW_TAG_subroutine_type:
22308 case DW_TAG_inlined_subroutine:
22309 this_type = read_subroutine_type (die, cu);
22310 break;
22311 case DW_TAG_array_type:
22312 this_type = read_array_type (die, cu);
22313 break;
22314 case DW_TAG_set_type:
22315 this_type = read_set_type (die, cu);
22316 break;
22317 case DW_TAG_pointer_type:
22318 this_type = read_tag_pointer_type (die, cu);
22319 break;
22320 case DW_TAG_ptr_to_member_type:
22321 this_type = read_tag_ptr_to_member_type (die, cu);
22322 break;
22323 case DW_TAG_reference_type:
22324 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22325 break;
22326 case DW_TAG_rvalue_reference_type:
22327 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22328 break;
22329 case DW_TAG_const_type:
22330 this_type = read_tag_const_type (die, cu);
22331 break;
22332 case DW_TAG_volatile_type:
22333 this_type = read_tag_volatile_type (die, cu);
22334 break;
22335 case DW_TAG_restrict_type:
22336 this_type = read_tag_restrict_type (die, cu);
22337 break;
22338 case DW_TAG_string_type:
22339 this_type = read_tag_string_type (die, cu);
22340 break;
22341 case DW_TAG_typedef:
22342 this_type = read_typedef (die, cu);
22343 break;
22344 case DW_TAG_subrange_type:
22345 this_type = read_subrange_type (die, cu);
22346 break;
22347 case DW_TAG_base_type:
22348 this_type = read_base_type (die, cu);
22349 break;
22350 case DW_TAG_unspecified_type:
22351 this_type = read_unspecified_type (die, cu);
22352 break;
22353 case DW_TAG_namespace:
22354 this_type = read_namespace_type (die, cu);
22355 break;
22356 case DW_TAG_module:
22357 this_type = read_module_type (die, cu);
22358 break;
22359 case DW_TAG_atomic_type:
22360 this_type = read_tag_atomic_type (die, cu);
22361 break;
22362 default:
22363 complaint (_("unexpected tag in read_type_die: '%s'"),
22364 dwarf_tag_name (die->tag));
22365 break;
22366 }
22367
22368 return this_type;
22369 }
22370
22371 /* See if we can figure out if the class lives in a namespace. We do
22372 this by looking for a member function; its demangled name will
22373 contain namespace info, if there is any.
22374 Return the computed name or NULL.
22375 Space for the result is allocated on the objfile's obstack.
22376 This is the full-die version of guess_partial_die_structure_name.
22377 In this case we know DIE has no useful parent. */
22378
22379 static const char *
22380 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22381 {
22382 struct die_info *spec_die;
22383 struct dwarf2_cu *spec_cu;
22384 struct die_info *child;
22385 struct objfile *objfile = cu->per_objfile->objfile;
22386
22387 spec_cu = cu;
22388 spec_die = die_specification (die, &spec_cu);
22389 if (spec_die != NULL)
22390 {
22391 die = spec_die;
22392 cu = spec_cu;
22393 }
22394
22395 for (child = die->child;
22396 child != NULL;
22397 child = child->sibling)
22398 {
22399 if (child->tag == DW_TAG_subprogram)
22400 {
22401 const char *linkage_name = dw2_linkage_name (child, cu);
22402
22403 if (linkage_name != NULL)
22404 {
22405 gdb::unique_xmalloc_ptr<char> actual_name
22406 (cu->language_defn->class_name_from_physname (linkage_name));
22407 const char *name = NULL;
22408
22409 if (actual_name != NULL)
22410 {
22411 const char *die_name = dwarf2_name (die, cu);
22412
22413 if (die_name != NULL
22414 && strcmp (die_name, actual_name.get ()) != 0)
22415 {
22416 /* Strip off the class name from the full name.
22417 We want the prefix. */
22418 int die_name_len = strlen (die_name);
22419 int actual_name_len = strlen (actual_name.get ());
22420 const char *ptr = actual_name.get ();
22421
22422 /* Test for '::' as a sanity check. */
22423 if (actual_name_len > die_name_len + 2
22424 && ptr[actual_name_len - die_name_len - 1] == ':')
22425 name = obstack_strndup (
22426 &objfile->per_bfd->storage_obstack,
22427 ptr, actual_name_len - die_name_len - 2);
22428 }
22429 }
22430 return name;
22431 }
22432 }
22433 }
22434
22435 return NULL;
22436 }
22437
22438 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22439 prefix part in such case. See
22440 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22441
22442 static const char *
22443 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22444 {
22445 struct attribute *attr;
22446 const char *base;
22447
22448 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22449 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22450 return NULL;
22451
22452 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22453 return NULL;
22454
22455 attr = dw2_linkage_name_attr (die, cu);
22456 const char *attr_name = attr->as_string ();
22457 if (attr == NULL || attr_name == NULL)
22458 return NULL;
22459
22460 /* dwarf2_name had to be already called. */
22461 gdb_assert (attr->canonical_string_p ());
22462
22463 /* Strip the base name, keep any leading namespaces/classes. */
22464 base = strrchr (attr_name, ':');
22465 if (base == NULL || base == attr_name || base[-1] != ':')
22466 return "";
22467
22468 struct objfile *objfile = cu->per_objfile->objfile;
22469 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22470 attr_name,
22471 &base[-1] - attr_name);
22472 }
22473
22474 /* Return the name of the namespace/class that DIE is defined within,
22475 or "" if we can't tell. The caller should not xfree the result.
22476
22477 For example, if we're within the method foo() in the following
22478 code:
22479
22480 namespace N {
22481 class C {
22482 void foo () {
22483 }
22484 };
22485 }
22486
22487 then determine_prefix on foo's die will return "N::C". */
22488
22489 static const char *
22490 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22491 {
22492 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22493 struct die_info *parent, *spec_die;
22494 struct dwarf2_cu *spec_cu;
22495 struct type *parent_type;
22496 const char *retval;
22497
22498 if (cu->per_cu->lang != language_cplus
22499 && cu->per_cu->lang != language_fortran
22500 && cu->per_cu->lang != language_d
22501 && cu->per_cu->lang != language_rust)
22502 return "";
22503
22504 retval = anonymous_struct_prefix (die, cu);
22505 if (retval)
22506 return retval;
22507
22508 /* We have to be careful in the presence of DW_AT_specification.
22509 For example, with GCC 3.4, given the code
22510
22511 namespace N {
22512 void foo() {
22513 // Definition of N::foo.
22514 }
22515 }
22516
22517 then we'll have a tree of DIEs like this:
22518
22519 1: DW_TAG_compile_unit
22520 2: DW_TAG_namespace // N
22521 3: DW_TAG_subprogram // declaration of N::foo
22522 4: DW_TAG_subprogram // definition of N::foo
22523 DW_AT_specification // refers to die #3
22524
22525 Thus, when processing die #4, we have to pretend that we're in
22526 the context of its DW_AT_specification, namely the contex of die
22527 #3. */
22528 spec_cu = cu;
22529 spec_die = die_specification (die, &spec_cu);
22530 if (spec_die == NULL)
22531 parent = die->parent;
22532 else
22533 {
22534 parent = spec_die->parent;
22535 cu = spec_cu;
22536 }
22537
22538 if (parent == NULL)
22539 return "";
22540 else if (parent->building_fullname)
22541 {
22542 const char *name;
22543 const char *parent_name;
22544
22545 /* It has been seen on RealView 2.2 built binaries,
22546 DW_TAG_template_type_param types actually _defined_ as
22547 children of the parent class:
22548
22549 enum E {};
22550 template class <class Enum> Class{};
22551 Class<enum E> class_e;
22552
22553 1: DW_TAG_class_type (Class)
22554 2: DW_TAG_enumeration_type (E)
22555 3: DW_TAG_enumerator (enum1:0)
22556 3: DW_TAG_enumerator (enum2:1)
22557 ...
22558 2: DW_TAG_template_type_param
22559 DW_AT_type DW_FORM_ref_udata (E)
22560
22561 Besides being broken debug info, it can put GDB into an
22562 infinite loop. Consider:
22563
22564 When we're building the full name for Class<E>, we'll start
22565 at Class, and go look over its template type parameters,
22566 finding E. We'll then try to build the full name of E, and
22567 reach here. We're now trying to build the full name of E,
22568 and look over the parent DIE for containing scope. In the
22569 broken case, if we followed the parent DIE of E, we'd again
22570 find Class, and once again go look at its template type
22571 arguments, etc., etc. Simply don't consider such parent die
22572 as source-level parent of this die (it can't be, the language
22573 doesn't allow it), and break the loop here. */
22574 name = dwarf2_name (die, cu);
22575 parent_name = dwarf2_name (parent, cu);
22576 complaint (_("template param type '%s' defined within parent '%s'"),
22577 name ? name : "<unknown>",
22578 parent_name ? parent_name : "<unknown>");
22579 return "";
22580 }
22581 else
22582 switch (parent->tag)
22583 {
22584 case DW_TAG_namespace:
22585 parent_type = read_type_die (parent, cu);
22586 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22587 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22588 Work around this problem here. */
22589 if (cu->per_cu->lang == language_cplus
22590 && strcmp (parent_type->name (), "::") == 0)
22591 return "";
22592 /* We give a name to even anonymous namespaces. */
22593 return parent_type->name ();
22594 case DW_TAG_class_type:
22595 case DW_TAG_interface_type:
22596 case DW_TAG_structure_type:
22597 case DW_TAG_union_type:
22598 case DW_TAG_module:
22599 parent_type = read_type_die (parent, cu);
22600 if (parent_type->name () != NULL)
22601 return parent_type->name ();
22602 else
22603 /* An anonymous structure is only allowed non-static data
22604 members; no typedefs, no member functions, et cetera.
22605 So it does not need a prefix. */
22606 return "";
22607 case DW_TAG_compile_unit:
22608 case DW_TAG_partial_unit:
22609 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22610 if (cu->per_cu->lang == language_cplus
22611 && !per_objfile->per_bfd->types.empty ()
22612 && die->child != NULL
22613 && (die->tag == DW_TAG_class_type
22614 || die->tag == DW_TAG_structure_type
22615 || die->tag == DW_TAG_union_type))
22616 {
22617 const char *name = guess_full_die_structure_name (die, cu);
22618 if (name != NULL)
22619 return name;
22620 }
22621 return "";
22622 case DW_TAG_subprogram:
22623 /* Nested subroutines in Fortran get a prefix with the name
22624 of the parent's subroutine. */
22625 if (cu->per_cu->lang == language_fortran)
22626 {
22627 if ((die->tag == DW_TAG_subprogram)
22628 && (dwarf2_name (parent, cu) != NULL))
22629 return dwarf2_name (parent, cu);
22630 }
22631 return determine_prefix (parent, cu);
22632 case DW_TAG_enumeration_type:
22633 parent_type = read_type_die (parent, cu);
22634 if (parent_type->is_declared_class ())
22635 {
22636 if (parent_type->name () != NULL)
22637 return parent_type->name ();
22638 return "";
22639 }
22640 /* Fall through. */
22641 default:
22642 return determine_prefix (parent, cu);
22643 }
22644 }
22645
22646 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22647 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22648 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22649 an obconcat, otherwise allocate storage for the result. The CU argument is
22650 used to determine the language and hence, the appropriate separator. */
22651
22652 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22653
22654 static char *
22655 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22656 int physname, struct dwarf2_cu *cu)
22657 {
22658 const char *lead = "";
22659 const char *sep;
22660
22661 if (suffix == NULL || suffix[0] == '\0'
22662 || prefix == NULL || prefix[0] == '\0')
22663 sep = "";
22664 else if (cu->per_cu->lang == language_d)
22665 {
22666 /* For D, the 'main' function could be defined in any module, but it
22667 should never be prefixed. */
22668 if (strcmp (suffix, "D main") == 0)
22669 {
22670 prefix = "";
22671 sep = "";
22672 }
22673 else
22674 sep = ".";
22675 }
22676 else if (cu->per_cu->lang == language_fortran && physname)
22677 {
22678 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22679 DW_AT_MIPS_linkage_name is preferred and used instead. */
22680
22681 lead = "__";
22682 sep = "_MOD_";
22683 }
22684 else
22685 sep = "::";
22686
22687 if (prefix == NULL)
22688 prefix = "";
22689 if (suffix == NULL)
22690 suffix = "";
22691
22692 if (obs == NULL)
22693 {
22694 char *retval
22695 = ((char *)
22696 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22697
22698 strcpy (retval, lead);
22699 strcat (retval, prefix);
22700 strcat (retval, sep);
22701 strcat (retval, suffix);
22702 return retval;
22703 }
22704 else
22705 {
22706 /* We have an obstack. */
22707 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22708 }
22709 }
22710
22711 /* Get name of a die, return NULL if not found. */
22712
22713 static const char *
22714 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22715 struct objfile *objfile)
22716 {
22717 if (name && cu->per_cu->lang == language_cplus)
22718 {
22719 gdb::unique_xmalloc_ptr<char> canon_name
22720 = cp_canonicalize_string (name);
22721
22722 if (canon_name != nullptr)
22723 name = objfile->intern (canon_name.get ());
22724 }
22725
22726 return name;
22727 }
22728
22729 /* Get name of a die, return NULL if not found.
22730 Anonymous namespaces are converted to their magic string. */
22731
22732 static const char *
22733 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22734 {
22735 struct attribute *attr;
22736 struct objfile *objfile = cu->per_objfile->objfile;
22737
22738 attr = dwarf2_attr (die, DW_AT_name, cu);
22739 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22740 if (attr_name == nullptr
22741 && die->tag != DW_TAG_namespace
22742 && die->tag != DW_TAG_class_type
22743 && die->tag != DW_TAG_interface_type
22744 && die->tag != DW_TAG_structure_type
22745 && die->tag != DW_TAG_union_type)
22746 return NULL;
22747
22748 switch (die->tag)
22749 {
22750 case DW_TAG_compile_unit:
22751 case DW_TAG_partial_unit:
22752 /* Compilation units have a DW_AT_name that is a filename, not
22753 a source language identifier. */
22754 case DW_TAG_enumeration_type:
22755 case DW_TAG_enumerator:
22756 /* These tags always have simple identifiers already; no need
22757 to canonicalize them. */
22758 return attr_name;
22759
22760 case DW_TAG_namespace:
22761 if (attr_name != nullptr)
22762 return attr_name;
22763 return CP_ANONYMOUS_NAMESPACE_STR;
22764
22765 case DW_TAG_class_type:
22766 case DW_TAG_interface_type:
22767 case DW_TAG_structure_type:
22768 case DW_TAG_union_type:
22769 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22770 structures or unions. These were of the form "._%d" in GCC 4.1,
22771 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22772 and GCC 4.4. We work around this problem by ignoring these. */
22773 if (attr_name != nullptr
22774 && (startswith (attr_name, "._")
22775 || startswith (attr_name, "<anonymous")))
22776 return NULL;
22777
22778 /* GCC might emit a nameless typedef that has a linkage name. See
22779 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22780 if (!attr || attr_name == NULL)
22781 {
22782 attr = dw2_linkage_name_attr (die, cu);
22783 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22784 if (attr == NULL || attr_name == NULL)
22785 return NULL;
22786
22787 /* Avoid demangling attr_name the second time on a second
22788 call for the same DIE. */
22789 if (!attr->canonical_string_p ())
22790 {
22791 gdb::unique_xmalloc_ptr<char> demangled
22792 (gdb_demangle (attr_name, DMGL_TYPES));
22793 if (demangled == nullptr)
22794 return nullptr;
22795
22796 attr->set_string_canonical (objfile->intern (demangled.get ()));
22797 attr_name = attr->as_string ();
22798 }
22799
22800 /* Strip any leading namespaces/classes, keep only the
22801 base name. DW_AT_name for named DIEs does not
22802 contain the prefixes. */
22803 const char *base = strrchr (attr_name, ':');
22804 if (base && base > attr_name && base[-1] == ':')
22805 return &base[1];
22806 else
22807 return attr_name;
22808 }
22809 break;
22810
22811 default:
22812 break;
22813 }
22814
22815 if (!attr->canonical_string_p ())
22816 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22817 objfile));
22818 return attr->as_string ();
22819 }
22820
22821 /* Return the die that this die in an extension of, or NULL if there
22822 is none. *EXT_CU is the CU containing DIE on input, and the CU
22823 containing the return value on output. */
22824
22825 static struct die_info *
22826 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22827 {
22828 struct attribute *attr;
22829
22830 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22831 if (attr == NULL)
22832 return NULL;
22833
22834 return follow_die_ref (die, attr, ext_cu);
22835 }
22836
22837 static void
22838 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22839 {
22840 unsigned int i;
22841
22842 print_spaces (indent, f);
22843 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22844 dwarf_tag_name (die->tag), die->abbrev,
22845 sect_offset_str (die->sect_off));
22846
22847 if (die->parent != NULL)
22848 {
22849 print_spaces (indent, f);
22850 fprintf_unfiltered (f, " parent at offset: %s\n",
22851 sect_offset_str (die->parent->sect_off));
22852 }
22853
22854 print_spaces (indent, f);
22855 fprintf_unfiltered (f, " has children: %s\n",
22856 dwarf_bool_name (die->child != NULL));
22857
22858 print_spaces (indent, f);
22859 fprintf_unfiltered (f, " attributes:\n");
22860
22861 for (i = 0; i < die->num_attrs; ++i)
22862 {
22863 print_spaces (indent, f);
22864 fprintf_unfiltered (f, " %s (%s) ",
22865 dwarf_attr_name (die->attrs[i].name),
22866 dwarf_form_name (die->attrs[i].form));
22867
22868 switch (die->attrs[i].form)
22869 {
22870 case DW_FORM_addr:
22871 case DW_FORM_addrx:
22872 case DW_FORM_GNU_addr_index:
22873 fprintf_unfiltered (f, "address: ");
22874 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
22875 break;
22876 case DW_FORM_block2:
22877 case DW_FORM_block4:
22878 case DW_FORM_block:
22879 case DW_FORM_block1:
22880 fprintf_unfiltered (f, "block: size %s",
22881 pulongest (die->attrs[i].as_block ()->size));
22882 break;
22883 case DW_FORM_exprloc:
22884 fprintf_unfiltered (f, "expression: size %s",
22885 pulongest (die->attrs[i].as_block ()->size));
22886 break;
22887 case DW_FORM_data16:
22888 fprintf_unfiltered (f, "constant of 16 bytes");
22889 break;
22890 case DW_FORM_ref_addr:
22891 fprintf_unfiltered (f, "ref address: ");
22892 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22893 break;
22894 case DW_FORM_GNU_ref_alt:
22895 fprintf_unfiltered (f, "alt ref address: ");
22896 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22897 break;
22898 case DW_FORM_ref1:
22899 case DW_FORM_ref2:
22900 case DW_FORM_ref4:
22901 case DW_FORM_ref8:
22902 case DW_FORM_ref_udata:
22903 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22904 (long) (die->attrs[i].as_unsigned ()));
22905 break;
22906 case DW_FORM_data1:
22907 case DW_FORM_data2:
22908 case DW_FORM_data4:
22909 case DW_FORM_data8:
22910 case DW_FORM_udata:
22911 fprintf_unfiltered (f, "constant: %s",
22912 pulongest (die->attrs[i].as_unsigned ()));
22913 break;
22914 case DW_FORM_sec_offset:
22915 fprintf_unfiltered (f, "section offset: %s",
22916 pulongest (die->attrs[i].as_unsigned ()));
22917 break;
22918 case DW_FORM_ref_sig8:
22919 fprintf_unfiltered (f, "signature: %s",
22920 hex_string (die->attrs[i].as_signature ()));
22921 break;
22922 case DW_FORM_string:
22923 case DW_FORM_strp:
22924 case DW_FORM_line_strp:
22925 case DW_FORM_strx:
22926 case DW_FORM_GNU_str_index:
22927 case DW_FORM_GNU_strp_alt:
22928 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22929 die->attrs[i].as_string ()
22930 ? die->attrs[i].as_string () : "",
22931 die->attrs[i].canonical_string_p () ? "is" : "not");
22932 break;
22933 case DW_FORM_flag:
22934 if (die->attrs[i].as_boolean ())
22935 fprintf_unfiltered (f, "flag: TRUE");
22936 else
22937 fprintf_unfiltered (f, "flag: FALSE");
22938 break;
22939 case DW_FORM_flag_present:
22940 fprintf_unfiltered (f, "flag: TRUE");
22941 break;
22942 case DW_FORM_indirect:
22943 /* The reader will have reduced the indirect form to
22944 the "base form" so this form should not occur. */
22945 fprintf_unfiltered (f,
22946 "unexpected attribute form: DW_FORM_indirect");
22947 break;
22948 case DW_FORM_sdata:
22949 case DW_FORM_implicit_const:
22950 fprintf_unfiltered (f, "constant: %s",
22951 plongest (die->attrs[i].as_signed ()));
22952 break;
22953 default:
22954 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22955 die->attrs[i].form);
22956 break;
22957 }
22958 fprintf_unfiltered (f, "\n");
22959 }
22960 }
22961
22962 static void
22963 dump_die_for_error (struct die_info *die)
22964 {
22965 dump_die_shallow (gdb_stderr, 0, die);
22966 }
22967
22968 static void
22969 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22970 {
22971 int indent = level * 4;
22972
22973 gdb_assert (die != NULL);
22974
22975 if (level >= max_level)
22976 return;
22977
22978 dump_die_shallow (f, indent, die);
22979
22980 if (die->child != NULL)
22981 {
22982 print_spaces (indent, f);
22983 fprintf_unfiltered (f, " Children:");
22984 if (level + 1 < max_level)
22985 {
22986 fprintf_unfiltered (f, "\n");
22987 dump_die_1 (f, level + 1, max_level, die->child);
22988 }
22989 else
22990 {
22991 fprintf_unfiltered (f,
22992 " [not printed, max nesting level reached]\n");
22993 }
22994 }
22995
22996 if (die->sibling != NULL && level > 0)
22997 {
22998 dump_die_1 (f, level, max_level, die->sibling);
22999 }
23000 }
23001
23002 /* This is called from the pdie macro in gdbinit.in.
23003 It's not static so gcc will keep a copy callable from gdb. */
23004
23005 void
23006 dump_die (struct die_info *die, int max_level)
23007 {
23008 dump_die_1 (gdb_stdlog, 0, max_level, die);
23009 }
23010
23011 static void
23012 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23013 {
23014 void **slot;
23015
23016 slot = htab_find_slot_with_hash (cu->die_hash, die,
23017 to_underlying (die->sect_off),
23018 INSERT);
23019
23020 *slot = die;
23021 }
23022
23023 /* Follow reference or signature attribute ATTR of SRC_DIE.
23024 On entry *REF_CU is the CU of SRC_DIE.
23025 On exit *REF_CU is the CU of the result. */
23026
23027 static struct die_info *
23028 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23029 struct dwarf2_cu **ref_cu)
23030 {
23031 struct die_info *die;
23032
23033 if (attr->form_is_ref ())
23034 die = follow_die_ref (src_die, attr, ref_cu);
23035 else if (attr->form == DW_FORM_ref_sig8)
23036 die = follow_die_sig (src_die, attr, ref_cu);
23037 else
23038 {
23039 dump_die_for_error (src_die);
23040 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23041 objfile_name ((*ref_cu)->per_objfile->objfile));
23042 }
23043
23044 return die;
23045 }
23046
23047 /* Follow reference OFFSET.
23048 On entry *REF_CU is the CU of the source die referencing OFFSET.
23049 On exit *REF_CU is the CU of the result.
23050 Returns NULL if OFFSET is invalid. */
23051
23052 static struct die_info *
23053 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23054 struct dwarf2_cu **ref_cu)
23055 {
23056 struct die_info temp_die;
23057 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23058 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23059
23060 gdb_assert (cu->per_cu != NULL);
23061
23062 target_cu = cu;
23063
23064 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23065 "source CU contains target offset: %d",
23066 sect_offset_str (cu->per_cu->sect_off),
23067 sect_offset_str (sect_off),
23068 cu->header.offset_in_cu_p (sect_off));
23069
23070 if (cu->per_cu->is_debug_types)
23071 {
23072 /* .debug_types CUs cannot reference anything outside their CU.
23073 If they need to, they have to reference a signatured type via
23074 DW_FORM_ref_sig8. */
23075 if (!cu->header.offset_in_cu_p (sect_off))
23076 return NULL;
23077 }
23078 else if (offset_in_dwz != cu->per_cu->is_dwz
23079 || !cu->header.offset_in_cu_p (sect_off))
23080 {
23081 struct dwarf2_per_cu_data *per_cu;
23082
23083 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23084 per_objfile);
23085
23086 dwarf_read_debug_printf_v ("target CU offset: %s, "
23087 "target CU DIEs loaded: %d",
23088 sect_offset_str (per_cu->sect_off),
23089 per_objfile->get_cu (per_cu) != nullptr);
23090
23091 /* If necessary, add it to the queue and load its DIEs.
23092
23093 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23094 it doesn't mean they are currently loaded. Since we require them
23095 to be loaded, we must check for ourselves. */
23096 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
23097 || per_objfile->get_cu (per_cu) == nullptr)
23098 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23099 false, cu->per_cu->lang);
23100
23101 target_cu = per_objfile->get_cu (per_cu);
23102 gdb_assert (target_cu != nullptr);
23103 }
23104 else if (cu->dies == NULL)
23105 {
23106 /* We're loading full DIEs during partial symbol reading. */
23107 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23108 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23109 language_minimal);
23110 }
23111
23112 *ref_cu = target_cu;
23113 temp_die.sect_off = sect_off;
23114
23115 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23116 &temp_die,
23117 to_underlying (sect_off));
23118 }
23119
23120 /* Follow reference attribute ATTR of SRC_DIE.
23121 On entry *REF_CU is the CU of SRC_DIE.
23122 On exit *REF_CU is the CU of the result. */
23123
23124 static struct die_info *
23125 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23126 struct dwarf2_cu **ref_cu)
23127 {
23128 sect_offset sect_off = attr->get_ref_die_offset ();
23129 struct dwarf2_cu *cu = *ref_cu;
23130 struct die_info *die;
23131
23132 die = follow_die_offset (sect_off,
23133 (attr->form == DW_FORM_GNU_ref_alt
23134 || cu->per_cu->is_dwz),
23135 ref_cu);
23136 if (!die)
23137 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23138 "at %s [in module %s]"),
23139 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23140 objfile_name (cu->per_objfile->objfile));
23141
23142 return die;
23143 }
23144
23145 /* See read.h. */
23146
23147 struct dwarf2_locexpr_baton
23148 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23149 dwarf2_per_cu_data *per_cu,
23150 dwarf2_per_objfile *per_objfile,
23151 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23152 bool resolve_abstract_p)
23153 {
23154 struct die_info *die;
23155 struct attribute *attr;
23156 struct dwarf2_locexpr_baton retval;
23157 struct objfile *objfile = per_objfile->objfile;
23158
23159 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23160 if (cu == nullptr)
23161 cu = load_cu (per_cu, per_objfile, false);
23162
23163 if (cu == nullptr)
23164 {
23165 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23166 Instead just throw an error, not much else we can do. */
23167 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23168 sect_offset_str (sect_off), objfile_name (objfile));
23169 }
23170
23171 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23172 if (!die)
23173 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23174 sect_offset_str (sect_off), objfile_name (objfile));
23175
23176 attr = dwarf2_attr (die, DW_AT_location, cu);
23177 if (!attr && resolve_abstract_p
23178 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23179 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23180 {
23181 CORE_ADDR pc = get_frame_pc ();
23182 CORE_ADDR baseaddr = objfile->text_section_offset ();
23183 struct gdbarch *gdbarch = objfile->arch ();
23184
23185 for (const auto &cand_off
23186 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23187 {
23188 struct dwarf2_cu *cand_cu = cu;
23189 struct die_info *cand
23190 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23191 if (!cand
23192 || !cand->parent
23193 || cand->parent->tag != DW_TAG_subprogram)
23194 continue;
23195
23196 CORE_ADDR pc_low, pc_high;
23197 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23198 if (pc_low == ((CORE_ADDR) -1))
23199 continue;
23200 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23201 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23202 if (!(pc_low <= pc && pc < pc_high))
23203 continue;
23204
23205 die = cand;
23206 attr = dwarf2_attr (die, DW_AT_location, cu);
23207 break;
23208 }
23209 }
23210
23211 if (!attr)
23212 {
23213 /* DWARF: "If there is no such attribute, then there is no effect.".
23214 DATA is ignored if SIZE is 0. */
23215
23216 retval.data = NULL;
23217 retval.size = 0;
23218 }
23219 else if (attr->form_is_section_offset ())
23220 {
23221 struct dwarf2_loclist_baton loclist_baton;
23222 CORE_ADDR pc = get_frame_pc ();
23223 size_t size;
23224
23225 fill_in_loclist_baton (cu, &loclist_baton, attr);
23226
23227 retval.data = dwarf2_find_location_expression (&loclist_baton,
23228 &size, pc);
23229 retval.size = size;
23230 }
23231 else
23232 {
23233 if (!attr->form_is_block ())
23234 error (_("Dwarf Error: DIE at %s referenced in module %s "
23235 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23236 sect_offset_str (sect_off), objfile_name (objfile));
23237
23238 struct dwarf_block *block = attr->as_block ();
23239 retval.data = block->data;
23240 retval.size = block->size;
23241 }
23242 retval.per_objfile = per_objfile;
23243 retval.per_cu = cu->per_cu;
23244
23245 per_objfile->age_comp_units ();
23246
23247 return retval;
23248 }
23249
23250 /* See read.h. */
23251
23252 struct dwarf2_locexpr_baton
23253 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23254 dwarf2_per_cu_data *per_cu,
23255 dwarf2_per_objfile *per_objfile,
23256 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23257 {
23258 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23259
23260 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23261 get_frame_pc);
23262 }
23263
23264 /* Write a constant of a given type as target-ordered bytes into
23265 OBSTACK. */
23266
23267 static const gdb_byte *
23268 write_constant_as_bytes (struct obstack *obstack,
23269 enum bfd_endian byte_order,
23270 struct type *type,
23271 ULONGEST value,
23272 LONGEST *len)
23273 {
23274 gdb_byte *result;
23275
23276 *len = TYPE_LENGTH (type);
23277 result = (gdb_byte *) obstack_alloc (obstack, *len);
23278 store_unsigned_integer (result, *len, byte_order, value);
23279
23280 return result;
23281 }
23282
23283 /* See read.h. */
23284
23285 const gdb_byte *
23286 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23287 dwarf2_per_cu_data *per_cu,
23288 dwarf2_per_objfile *per_objfile,
23289 obstack *obstack,
23290 LONGEST *len)
23291 {
23292 struct die_info *die;
23293 struct attribute *attr;
23294 const gdb_byte *result = NULL;
23295 struct type *type;
23296 LONGEST value;
23297 enum bfd_endian byte_order;
23298 struct objfile *objfile = per_objfile->objfile;
23299
23300 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23301 if (cu == nullptr)
23302 cu = load_cu (per_cu, per_objfile, false);
23303
23304 if (cu == nullptr)
23305 {
23306 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23307 Instead just throw an error, not much else we can do. */
23308 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23309 sect_offset_str (sect_off), objfile_name (objfile));
23310 }
23311
23312 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23313 if (!die)
23314 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23315 sect_offset_str (sect_off), objfile_name (objfile));
23316
23317 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23318 if (attr == NULL)
23319 return NULL;
23320
23321 byte_order = (bfd_big_endian (objfile->obfd)
23322 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23323
23324 switch (attr->form)
23325 {
23326 case DW_FORM_addr:
23327 case DW_FORM_addrx:
23328 case DW_FORM_GNU_addr_index:
23329 {
23330 gdb_byte *tem;
23331
23332 *len = cu->header.addr_size;
23333 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23334 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23335 result = tem;
23336 }
23337 break;
23338 case DW_FORM_string:
23339 case DW_FORM_strp:
23340 case DW_FORM_strx:
23341 case DW_FORM_GNU_str_index:
23342 case DW_FORM_GNU_strp_alt:
23343 /* The string is already allocated on the objfile obstack, point
23344 directly to it. */
23345 {
23346 const char *attr_name = attr->as_string ();
23347 result = (const gdb_byte *) attr_name;
23348 *len = strlen (attr_name);
23349 }
23350 break;
23351 case DW_FORM_block1:
23352 case DW_FORM_block2:
23353 case DW_FORM_block4:
23354 case DW_FORM_block:
23355 case DW_FORM_exprloc:
23356 case DW_FORM_data16:
23357 {
23358 struct dwarf_block *block = attr->as_block ();
23359 result = block->data;
23360 *len = block->size;
23361 }
23362 break;
23363
23364 /* The DW_AT_const_value attributes are supposed to carry the
23365 symbol's value "represented as it would be on the target
23366 architecture." By the time we get here, it's already been
23367 converted to host endianness, so we just need to sign- or
23368 zero-extend it as appropriate. */
23369 case DW_FORM_data1:
23370 type = die_type (die, cu);
23371 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23372 if (result == NULL)
23373 result = write_constant_as_bytes (obstack, byte_order,
23374 type, value, len);
23375 break;
23376 case DW_FORM_data2:
23377 type = die_type (die, cu);
23378 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23379 if (result == NULL)
23380 result = write_constant_as_bytes (obstack, byte_order,
23381 type, value, len);
23382 break;
23383 case DW_FORM_data4:
23384 type = die_type (die, cu);
23385 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23386 if (result == NULL)
23387 result = write_constant_as_bytes (obstack, byte_order,
23388 type, value, len);
23389 break;
23390 case DW_FORM_data8:
23391 type = die_type (die, cu);
23392 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23393 if (result == NULL)
23394 result = write_constant_as_bytes (obstack, byte_order,
23395 type, value, len);
23396 break;
23397
23398 case DW_FORM_sdata:
23399 case DW_FORM_implicit_const:
23400 type = die_type (die, cu);
23401 result = write_constant_as_bytes (obstack, byte_order,
23402 type, attr->as_signed (), len);
23403 break;
23404
23405 case DW_FORM_udata:
23406 type = die_type (die, cu);
23407 result = write_constant_as_bytes (obstack, byte_order,
23408 type, attr->as_unsigned (), len);
23409 break;
23410
23411 default:
23412 complaint (_("unsupported const value attribute form: '%s'"),
23413 dwarf_form_name (attr->form));
23414 break;
23415 }
23416
23417 return result;
23418 }
23419
23420 /* See read.h. */
23421
23422 struct type *
23423 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23424 dwarf2_per_cu_data *per_cu,
23425 dwarf2_per_objfile *per_objfile,
23426 const char **var_name)
23427 {
23428 struct die_info *die;
23429
23430 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23431 if (cu == nullptr)
23432 cu = load_cu (per_cu, per_objfile, false);
23433
23434 if (cu == nullptr)
23435 return nullptr;
23436
23437 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23438 if (!die)
23439 return NULL;
23440
23441 if (var_name != nullptr)
23442 *var_name = var_decl_name (die, cu);
23443 return die_type (die, cu);
23444 }
23445
23446 /* See read.h. */
23447
23448 struct type *
23449 dwarf2_get_die_type (cu_offset die_offset,
23450 dwarf2_per_cu_data *per_cu,
23451 dwarf2_per_objfile *per_objfile)
23452 {
23453 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23454 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23455 }
23456
23457 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23458 On entry *REF_CU is the CU of SRC_DIE.
23459 On exit *REF_CU is the CU of the result.
23460 Returns NULL if the referenced DIE isn't found. */
23461
23462 static struct die_info *
23463 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23464 struct dwarf2_cu **ref_cu)
23465 {
23466 struct die_info temp_die;
23467 struct dwarf2_cu *sig_cu;
23468 struct die_info *die;
23469 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23470
23471
23472 /* While it might be nice to assert sig_type->type == NULL here,
23473 we can get here for DW_AT_imported_declaration where we need
23474 the DIE not the type. */
23475
23476 /* If necessary, add it to the queue and load its DIEs.
23477
23478 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23479 it doesn't mean they are currently loaded. Since we require them
23480 to be loaded, we must check for ourselves. */
23481 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23482 language_minimal)
23483 || per_objfile->get_cu (sig_type) == nullptr)
23484 read_signatured_type (sig_type, per_objfile);
23485
23486 sig_cu = per_objfile->get_cu (sig_type);
23487 gdb_assert (sig_cu != NULL);
23488 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23489 temp_die.sect_off = sig_type->type_offset_in_section;
23490 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23491 to_underlying (temp_die.sect_off));
23492 if (die)
23493 {
23494 /* For .gdb_index version 7 keep track of included TUs.
23495 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23496 if (per_objfile->per_bfd->index_table != NULL
23497 && per_objfile->per_bfd->index_table->version <= 7)
23498 {
23499 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23500 }
23501
23502 *ref_cu = sig_cu;
23503 return die;
23504 }
23505
23506 return NULL;
23507 }
23508
23509 /* Follow signatured type referenced by ATTR in SRC_DIE.
23510 On entry *REF_CU is the CU of SRC_DIE.
23511 On exit *REF_CU is the CU of the result.
23512 The result is the DIE of the type.
23513 If the referenced type cannot be found an error is thrown. */
23514
23515 static struct die_info *
23516 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23517 struct dwarf2_cu **ref_cu)
23518 {
23519 ULONGEST signature = attr->as_signature ();
23520 struct signatured_type *sig_type;
23521 struct die_info *die;
23522
23523 gdb_assert (attr->form == DW_FORM_ref_sig8);
23524
23525 sig_type = lookup_signatured_type (*ref_cu, signature);
23526 /* sig_type will be NULL if the signatured type is missing from
23527 the debug info. */
23528 if (sig_type == NULL)
23529 {
23530 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23531 " from DIE at %s [in module %s]"),
23532 hex_string (signature), sect_offset_str (src_die->sect_off),
23533 objfile_name ((*ref_cu)->per_objfile->objfile));
23534 }
23535
23536 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23537 if (die == NULL)
23538 {
23539 dump_die_for_error (src_die);
23540 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23541 " from DIE at %s [in module %s]"),
23542 hex_string (signature), sect_offset_str (src_die->sect_off),
23543 objfile_name ((*ref_cu)->per_objfile->objfile));
23544 }
23545
23546 return die;
23547 }
23548
23549 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23550 reading in and processing the type unit if necessary. */
23551
23552 static struct type *
23553 get_signatured_type (struct die_info *die, ULONGEST signature,
23554 struct dwarf2_cu *cu)
23555 {
23556 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23557 struct signatured_type *sig_type;
23558 struct dwarf2_cu *type_cu;
23559 struct die_info *type_die;
23560 struct type *type;
23561
23562 sig_type = lookup_signatured_type (cu, signature);
23563 /* sig_type will be NULL if the signatured type is missing from
23564 the debug info. */
23565 if (sig_type == NULL)
23566 {
23567 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23568 " from DIE at %s [in module %s]"),
23569 hex_string (signature), sect_offset_str (die->sect_off),
23570 objfile_name (per_objfile->objfile));
23571 return build_error_marker_type (cu, die);
23572 }
23573
23574 /* If we already know the type we're done. */
23575 type = per_objfile->get_type_for_signatured_type (sig_type);
23576 if (type != nullptr)
23577 return type;
23578
23579 type_cu = cu;
23580 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23581 if (type_die != NULL)
23582 {
23583 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23584 is created. This is important, for example, because for c++ classes
23585 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23586 type = read_type_die (type_die, type_cu);
23587 if (type == NULL)
23588 {
23589 complaint (_("Dwarf Error: Cannot build signatured type %s"
23590 " referenced from DIE at %s [in module %s]"),
23591 hex_string (signature), sect_offset_str (die->sect_off),
23592 objfile_name (per_objfile->objfile));
23593 type = build_error_marker_type (cu, die);
23594 }
23595 }
23596 else
23597 {
23598 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23599 " from DIE at %s [in module %s]"),
23600 hex_string (signature), sect_offset_str (die->sect_off),
23601 objfile_name (per_objfile->objfile));
23602 type = build_error_marker_type (cu, die);
23603 }
23604
23605 per_objfile->set_type_for_signatured_type (sig_type, type);
23606
23607 return type;
23608 }
23609
23610 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23611 reading in and processing the type unit if necessary. */
23612
23613 static struct type *
23614 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23615 struct dwarf2_cu *cu) /* ARI: editCase function */
23616 {
23617 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23618 if (attr->form_is_ref ())
23619 {
23620 struct dwarf2_cu *type_cu = cu;
23621 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23622
23623 return read_type_die (type_die, type_cu);
23624 }
23625 else if (attr->form == DW_FORM_ref_sig8)
23626 {
23627 return get_signatured_type (die, attr->as_signature (), cu);
23628 }
23629 else
23630 {
23631 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23632
23633 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23634 " at %s [in module %s]"),
23635 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23636 objfile_name (per_objfile->objfile));
23637 return build_error_marker_type (cu, die);
23638 }
23639 }
23640
23641 /* Load the DIEs associated with type unit PER_CU into memory. */
23642
23643 static void
23644 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23645 dwarf2_per_objfile *per_objfile)
23646 {
23647 struct signatured_type *sig_type;
23648
23649 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23650 gdb_assert (! per_cu->type_unit_group_p ());
23651
23652 /* We have the per_cu, but we need the signatured_type.
23653 Fortunately this is an easy translation. */
23654 gdb_assert (per_cu->is_debug_types);
23655 sig_type = (struct signatured_type *) per_cu;
23656
23657 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23658
23659 read_signatured_type (sig_type, per_objfile);
23660
23661 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23662 }
23663
23664 /* Read in a signatured type and build its CU and DIEs.
23665 If the type is a stub for the real type in a DWO file,
23666 read in the real type from the DWO file as well. */
23667
23668 static void
23669 read_signatured_type (signatured_type *sig_type,
23670 dwarf2_per_objfile *per_objfile)
23671 {
23672 gdb_assert (sig_type->is_debug_types);
23673 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23674
23675 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23676
23677 if (!reader.dummy_p)
23678 {
23679 struct dwarf2_cu *cu = reader.cu;
23680 const gdb_byte *info_ptr = reader.info_ptr;
23681
23682 gdb_assert (cu->die_hash == NULL);
23683 cu->die_hash =
23684 htab_create_alloc_ex (cu->header.length / 12,
23685 die_hash,
23686 die_eq,
23687 NULL,
23688 &cu->comp_unit_obstack,
23689 hashtab_obstack_allocate,
23690 dummy_obstack_deallocate);
23691
23692 if (reader.comp_unit_die->has_children)
23693 reader.comp_unit_die->child
23694 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23695 reader.comp_unit_die);
23696 cu->dies = reader.comp_unit_die;
23697 /* comp_unit_die is not stored in die_hash, no need. */
23698
23699 /* We try not to read any attributes in this function, because
23700 not all CUs needed for references have been loaded yet, and
23701 symbol table processing isn't initialized. But we have to
23702 set the CU language, or we won't be able to build types
23703 correctly. Similarly, if we do not read the producer, we can
23704 not apply producer-specific interpretation. */
23705 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23706
23707 reader.keep ();
23708 }
23709
23710 sig_type->tu_read = 1;
23711 }
23712
23713 /* Decode simple location descriptions.
23714 Given a pointer to a dwarf block that defines a location, compute
23715 the location and return the value. If COMPUTED is non-null, it is
23716 set to true to indicate that decoding was successful, and false
23717 otherwise. If COMPUTED is null, then this function may emit a
23718 complaint. */
23719
23720 static CORE_ADDR
23721 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23722 {
23723 struct objfile *objfile = cu->per_objfile->objfile;
23724 size_t i;
23725 size_t size = blk->size;
23726 const gdb_byte *data = blk->data;
23727 CORE_ADDR stack[64];
23728 int stacki;
23729 unsigned int bytes_read, unsnd;
23730 gdb_byte op;
23731
23732 if (computed != nullptr)
23733 *computed = false;
23734
23735 i = 0;
23736 stacki = 0;
23737 stack[stacki] = 0;
23738 stack[++stacki] = 0;
23739
23740 while (i < size)
23741 {
23742 op = data[i++];
23743 switch (op)
23744 {
23745 case DW_OP_lit0:
23746 case DW_OP_lit1:
23747 case DW_OP_lit2:
23748 case DW_OP_lit3:
23749 case DW_OP_lit4:
23750 case DW_OP_lit5:
23751 case DW_OP_lit6:
23752 case DW_OP_lit7:
23753 case DW_OP_lit8:
23754 case DW_OP_lit9:
23755 case DW_OP_lit10:
23756 case DW_OP_lit11:
23757 case DW_OP_lit12:
23758 case DW_OP_lit13:
23759 case DW_OP_lit14:
23760 case DW_OP_lit15:
23761 case DW_OP_lit16:
23762 case DW_OP_lit17:
23763 case DW_OP_lit18:
23764 case DW_OP_lit19:
23765 case DW_OP_lit20:
23766 case DW_OP_lit21:
23767 case DW_OP_lit22:
23768 case DW_OP_lit23:
23769 case DW_OP_lit24:
23770 case DW_OP_lit25:
23771 case DW_OP_lit26:
23772 case DW_OP_lit27:
23773 case DW_OP_lit28:
23774 case DW_OP_lit29:
23775 case DW_OP_lit30:
23776 case DW_OP_lit31:
23777 stack[++stacki] = op - DW_OP_lit0;
23778 break;
23779
23780 case DW_OP_reg0:
23781 case DW_OP_reg1:
23782 case DW_OP_reg2:
23783 case DW_OP_reg3:
23784 case DW_OP_reg4:
23785 case DW_OP_reg5:
23786 case DW_OP_reg6:
23787 case DW_OP_reg7:
23788 case DW_OP_reg8:
23789 case DW_OP_reg9:
23790 case DW_OP_reg10:
23791 case DW_OP_reg11:
23792 case DW_OP_reg12:
23793 case DW_OP_reg13:
23794 case DW_OP_reg14:
23795 case DW_OP_reg15:
23796 case DW_OP_reg16:
23797 case DW_OP_reg17:
23798 case DW_OP_reg18:
23799 case DW_OP_reg19:
23800 case DW_OP_reg20:
23801 case DW_OP_reg21:
23802 case DW_OP_reg22:
23803 case DW_OP_reg23:
23804 case DW_OP_reg24:
23805 case DW_OP_reg25:
23806 case DW_OP_reg26:
23807 case DW_OP_reg27:
23808 case DW_OP_reg28:
23809 case DW_OP_reg29:
23810 case DW_OP_reg30:
23811 case DW_OP_reg31:
23812 stack[++stacki] = op - DW_OP_reg0;
23813 if (i < size)
23814 {
23815 if (computed == nullptr)
23816 dwarf2_complex_location_expr_complaint ();
23817 else
23818 return 0;
23819 }
23820 break;
23821
23822 case DW_OP_regx:
23823 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23824 i += bytes_read;
23825 stack[++stacki] = unsnd;
23826 if (i < size)
23827 {
23828 if (computed == nullptr)
23829 dwarf2_complex_location_expr_complaint ();
23830 else
23831 return 0;
23832 }
23833 break;
23834
23835 case DW_OP_addr:
23836 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23837 &bytes_read);
23838 i += bytes_read;
23839 break;
23840
23841 case DW_OP_const1u:
23842 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23843 i += 1;
23844 break;
23845
23846 case DW_OP_const1s:
23847 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23848 i += 1;
23849 break;
23850
23851 case DW_OP_const2u:
23852 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23853 i += 2;
23854 break;
23855
23856 case DW_OP_const2s:
23857 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23858 i += 2;
23859 break;
23860
23861 case DW_OP_const4u:
23862 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23863 i += 4;
23864 break;
23865
23866 case DW_OP_const4s:
23867 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23868 i += 4;
23869 break;
23870
23871 case DW_OP_const8u:
23872 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23873 i += 8;
23874 break;
23875
23876 case DW_OP_constu:
23877 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23878 &bytes_read);
23879 i += bytes_read;
23880 break;
23881
23882 case DW_OP_consts:
23883 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23884 i += bytes_read;
23885 break;
23886
23887 case DW_OP_dup:
23888 stack[stacki + 1] = stack[stacki];
23889 stacki++;
23890 break;
23891
23892 case DW_OP_plus:
23893 stack[stacki - 1] += stack[stacki];
23894 stacki--;
23895 break;
23896
23897 case DW_OP_plus_uconst:
23898 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23899 &bytes_read);
23900 i += bytes_read;
23901 break;
23902
23903 case DW_OP_minus:
23904 stack[stacki - 1] -= stack[stacki];
23905 stacki--;
23906 break;
23907
23908 case DW_OP_deref:
23909 /* If we're not the last op, then we definitely can't encode
23910 this using GDB's address_class enum. This is valid for partial
23911 global symbols, although the variable's address will be bogus
23912 in the psymtab. */
23913 if (i < size)
23914 {
23915 if (computed == nullptr)
23916 dwarf2_complex_location_expr_complaint ();
23917 else
23918 return 0;
23919 }
23920 break;
23921
23922 case DW_OP_GNU_push_tls_address:
23923 case DW_OP_form_tls_address:
23924 /* The top of the stack has the offset from the beginning
23925 of the thread control block at which the variable is located. */
23926 /* Nothing should follow this operator, so the top of stack would
23927 be returned. */
23928 /* This is valid for partial global symbols, but the variable's
23929 address will be bogus in the psymtab. Make it always at least
23930 non-zero to not look as a variable garbage collected by linker
23931 which have DW_OP_addr 0. */
23932 if (i < size)
23933 {
23934 if (computed == nullptr)
23935 dwarf2_complex_location_expr_complaint ();
23936 else
23937 return 0;
23938 }
23939 stack[stacki]++;
23940 break;
23941
23942 case DW_OP_GNU_uninit:
23943 if (computed != nullptr)
23944 return 0;
23945 break;
23946
23947 case DW_OP_addrx:
23948 case DW_OP_GNU_addr_index:
23949 case DW_OP_GNU_const_index:
23950 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23951 &bytes_read);
23952 i += bytes_read;
23953 break;
23954
23955 default:
23956 if (computed == nullptr)
23957 {
23958 const char *name = get_DW_OP_name (op);
23959
23960 if (name)
23961 complaint (_("unsupported stack op: '%s'"),
23962 name);
23963 else
23964 complaint (_("unsupported stack op: '%02x'"),
23965 op);
23966 }
23967
23968 return (stack[stacki]);
23969 }
23970
23971 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23972 outside of the allocated space. Also enforce minimum>0. */
23973 if (stacki >= ARRAY_SIZE (stack) - 1)
23974 {
23975 if (computed == nullptr)
23976 complaint (_("location description stack overflow"));
23977 return 0;
23978 }
23979
23980 if (stacki <= 0)
23981 {
23982 if (computed == nullptr)
23983 complaint (_("location description stack underflow"));
23984 return 0;
23985 }
23986 }
23987
23988 if (computed != nullptr)
23989 *computed = true;
23990 return (stack[stacki]);
23991 }
23992
23993 /* memory allocation interface */
23994
23995 static struct dwarf_block *
23996 dwarf_alloc_block (struct dwarf2_cu *cu)
23997 {
23998 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23999 }
24000
24001 static struct die_info *
24002 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24003 {
24004 struct die_info *die;
24005 size_t size = sizeof (struct die_info);
24006
24007 if (num_attrs > 1)
24008 size += (num_attrs - 1) * sizeof (struct attribute);
24009
24010 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24011 memset (die, 0, sizeof (struct die_info));
24012 return (die);
24013 }
24014
24015 \f
24016
24017 /* Macro support. */
24018
24019 /* An overload of dwarf_decode_macros that finds the correct section
24020 and ensures it is read in before calling the other overload. */
24021
24022 static void
24023 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24024 int section_is_gnu)
24025 {
24026 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24027 struct objfile *objfile = per_objfile->objfile;
24028 const struct line_header *lh = cu->line_header;
24029 unsigned int offset_size = cu->header.offset_size;
24030 struct dwarf2_section_info *section;
24031 const char *section_name;
24032
24033 if (cu->dwo_unit != nullptr)
24034 {
24035 if (section_is_gnu)
24036 {
24037 section = &cu->dwo_unit->dwo_file->sections.macro;
24038 section_name = ".debug_macro.dwo";
24039 }
24040 else
24041 {
24042 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24043 section_name = ".debug_macinfo.dwo";
24044 }
24045 }
24046 else
24047 {
24048 if (section_is_gnu)
24049 {
24050 section = &per_objfile->per_bfd->macro;
24051 section_name = ".debug_macro";
24052 }
24053 else
24054 {
24055 section = &per_objfile->per_bfd->macinfo;
24056 section_name = ".debug_macinfo";
24057 }
24058 }
24059
24060 section->read (objfile);
24061 if (section->buffer == nullptr)
24062 {
24063 complaint (_("missing %s section"), section_name);
24064 return;
24065 }
24066
24067 buildsym_compunit *builder = cu->get_builder ();
24068
24069 struct dwarf2_section_info *str_offsets_section;
24070 struct dwarf2_section_info *str_section;
24071 ULONGEST str_offsets_base;
24072
24073 if (cu->dwo_unit != nullptr)
24074 {
24075 str_offsets_section = &cu->dwo_unit->dwo_file
24076 ->sections.str_offsets;
24077 str_section = &cu->dwo_unit->dwo_file->sections.str;
24078 str_offsets_base = cu->header.addr_size;
24079 }
24080 else
24081 {
24082 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24083 str_section = &per_objfile->per_bfd->str;
24084 str_offsets_base = *cu->str_offsets_base;
24085 }
24086
24087 dwarf_decode_macros (per_objfile, builder, section, lh,
24088 offset_size, offset, str_section, str_offsets_section,
24089 str_offsets_base, section_is_gnu);
24090 }
24091
24092 /* Return the .debug_loc section to use for CU.
24093 For DWO files use .debug_loc.dwo. */
24094
24095 static struct dwarf2_section_info *
24096 cu_debug_loc_section (struct dwarf2_cu *cu)
24097 {
24098 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24099
24100 if (cu->dwo_unit)
24101 {
24102 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24103
24104 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24105 }
24106 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24107 : &per_objfile->per_bfd->loc);
24108 }
24109
24110 /* Return the .debug_rnglists section to use for CU. */
24111 static struct dwarf2_section_info *
24112 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24113 {
24114 if (cu->header.version < 5)
24115 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24116 cu->header.version);
24117 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24118
24119 /* Make sure we read the .debug_rnglists section from the file that
24120 contains the DW_AT_ranges attribute we are reading. Normally that
24121 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24122 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24123 program. */
24124 if (cu->dwo_unit != nullptr
24125 && tag != DW_TAG_compile_unit
24126 && tag != DW_TAG_skeleton_unit)
24127 {
24128 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24129
24130 if (sections->rnglists.size > 0)
24131 return &sections->rnglists;
24132 else
24133 error (_(".debug_rnglists section is missing from .dwo file."));
24134 }
24135 return &dwarf2_per_objfile->per_bfd->rnglists;
24136 }
24137
24138 /* A helper function that fills in a dwarf2_loclist_baton. */
24139
24140 static void
24141 fill_in_loclist_baton (struct dwarf2_cu *cu,
24142 struct dwarf2_loclist_baton *baton,
24143 const struct attribute *attr)
24144 {
24145 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24146 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24147
24148 section->read (per_objfile->objfile);
24149
24150 baton->per_objfile = per_objfile;
24151 baton->per_cu = cu->per_cu;
24152 gdb_assert (baton->per_cu);
24153 /* We don't know how long the location list is, but make sure we
24154 don't run off the edge of the section. */
24155 baton->size = section->size - attr->as_unsigned ();
24156 baton->data = section->buffer + attr->as_unsigned ();
24157 if (cu->base_address.has_value ())
24158 baton->base_address = *cu->base_address;
24159 else
24160 baton->base_address = 0;
24161 baton->from_dwo = cu->dwo_unit != NULL;
24162 }
24163
24164 static void
24165 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24166 struct dwarf2_cu *cu, int is_block)
24167 {
24168 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24169 struct objfile *objfile = per_objfile->objfile;
24170 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24171
24172 if (attr->form_is_section_offset ()
24173 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24174 the section. If so, fall through to the complaint in the
24175 other branch. */
24176 && attr->as_unsigned () < section->get_size (objfile))
24177 {
24178 struct dwarf2_loclist_baton *baton;
24179
24180 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24181
24182 fill_in_loclist_baton (cu, baton, attr);
24183
24184 if (!cu->base_address.has_value ())
24185 complaint (_("Location list used without "
24186 "specifying the CU base address."));
24187
24188 SYMBOL_ACLASS_INDEX (sym) = (is_block
24189 ? dwarf2_loclist_block_index
24190 : dwarf2_loclist_index);
24191 SYMBOL_LOCATION_BATON (sym) = baton;
24192 }
24193 else
24194 {
24195 struct dwarf2_locexpr_baton *baton;
24196
24197 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24198 baton->per_objfile = per_objfile;
24199 baton->per_cu = cu->per_cu;
24200 gdb_assert (baton->per_cu);
24201
24202 if (attr->form_is_block ())
24203 {
24204 /* Note that we're just copying the block's data pointer
24205 here, not the actual data. We're still pointing into the
24206 info_buffer for SYM's objfile; right now we never release
24207 that buffer, but when we do clean up properly this may
24208 need to change. */
24209 struct dwarf_block *block = attr->as_block ();
24210 baton->size = block->size;
24211 baton->data = block->data;
24212 }
24213 else
24214 {
24215 dwarf2_invalid_attrib_class_complaint ("location description",
24216 sym->natural_name ());
24217 baton->size = 0;
24218 }
24219
24220 SYMBOL_ACLASS_INDEX (sym) = (is_block
24221 ? dwarf2_locexpr_block_index
24222 : dwarf2_locexpr_index);
24223 SYMBOL_LOCATION_BATON (sym) = baton;
24224 }
24225 }
24226
24227 /* See read.h. */
24228
24229 const comp_unit_head *
24230 dwarf2_per_cu_data::get_header () const
24231 {
24232 if (!m_header_read_in)
24233 {
24234 const gdb_byte *info_ptr
24235 = this->section->buffer + to_underlying (this->sect_off);
24236
24237 memset (&m_header, 0, sizeof (m_header));
24238
24239 read_comp_unit_head (&m_header, info_ptr, this->section,
24240 rcuh_kind::COMPILE);
24241
24242 m_header_read_in = true;
24243 }
24244
24245 return &m_header;
24246 }
24247
24248 /* See read.h. */
24249
24250 int
24251 dwarf2_per_cu_data::addr_size () const
24252 {
24253 return this->get_header ()->addr_size;
24254 }
24255
24256 /* See read.h. */
24257
24258 int
24259 dwarf2_per_cu_data::offset_size () const
24260 {
24261 return this->get_header ()->offset_size;
24262 }
24263
24264 /* See read.h. */
24265
24266 int
24267 dwarf2_per_cu_data::ref_addr_size () const
24268 {
24269 const comp_unit_head *header = this->get_header ();
24270
24271 if (header->version == 2)
24272 return header->addr_size;
24273 else
24274 return header->offset_size;
24275 }
24276
24277 /* A helper function for dwarf2_find_containing_comp_unit that returns
24278 the index of the result, and that searches a vector. It will
24279 return a result even if the offset in question does not actually
24280 occur in any CU. This is separate so that it can be unit
24281 tested. */
24282
24283 static int
24284 dwarf2_find_containing_comp_unit
24285 (sect_offset sect_off,
24286 unsigned int offset_in_dwz,
24287 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24288 {
24289 int low, high;
24290
24291 low = 0;
24292 high = all_comp_units.size () - 1;
24293 while (high > low)
24294 {
24295 struct dwarf2_per_cu_data *mid_cu;
24296 int mid = low + (high - low) / 2;
24297
24298 mid_cu = all_comp_units[mid].get ();
24299 if (mid_cu->is_dwz > offset_in_dwz
24300 || (mid_cu->is_dwz == offset_in_dwz
24301 && mid_cu->sect_off + mid_cu->length > sect_off))
24302 high = mid;
24303 else
24304 low = mid + 1;
24305 }
24306 gdb_assert (low == high);
24307 return low;
24308 }
24309
24310 /* Locate the .debug_info compilation unit from CU's objfile which contains
24311 the DIE at OFFSET. Raises an error on failure. */
24312
24313 static struct dwarf2_per_cu_data *
24314 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24315 unsigned int offset_in_dwz,
24316 dwarf2_per_objfile *per_objfile)
24317 {
24318 int low = dwarf2_find_containing_comp_unit
24319 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24320 dwarf2_per_cu_data *this_cu
24321 = per_objfile->per_bfd->all_comp_units[low].get ();
24322
24323 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24324 {
24325 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24326 error (_("Dwarf Error: could not find partial DIE containing "
24327 "offset %s [in module %s]"),
24328 sect_offset_str (sect_off),
24329 bfd_get_filename (per_objfile->objfile->obfd));
24330
24331 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24332 <= sect_off);
24333 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
24334 }
24335 else
24336 {
24337 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24338 && sect_off >= this_cu->sect_off + this_cu->length)
24339 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24340 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24341 return this_cu;
24342 }
24343 }
24344
24345 #if GDB_SELF_TEST
24346
24347 namespace selftests {
24348 namespace find_containing_comp_unit {
24349
24350 static void
24351 run_test ()
24352 {
24353 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24354 dwarf2_per_cu_data *one_ptr = one.get ();
24355 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24356 dwarf2_per_cu_data *two_ptr = two.get ();
24357 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24358 dwarf2_per_cu_data *three_ptr = three.get ();
24359 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24360 dwarf2_per_cu_data *four_ptr = four.get ();
24361
24362 one->length = 5;
24363 two->sect_off = sect_offset (one->length);
24364 two->length = 7;
24365
24366 three->length = 5;
24367 three->is_dwz = 1;
24368 four->sect_off = sect_offset (three->length);
24369 four->length = 7;
24370 four->is_dwz = 1;
24371
24372 std::vector<dwarf2_per_cu_data_up> units;
24373 units.push_back (std::move (one));
24374 units.push_back (std::move (two));
24375 units.push_back (std::move (three));
24376 units.push_back (std::move (four));
24377
24378 int result;
24379
24380 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24381 SELF_CHECK (units[result].get () == one_ptr);
24382 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24383 SELF_CHECK (units[result].get () == one_ptr);
24384 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24385 SELF_CHECK (units[result].get () == two_ptr);
24386
24387 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24388 SELF_CHECK (units[result].get () == three_ptr);
24389 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24390 SELF_CHECK (units[result].get () == three_ptr);
24391 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24392 SELF_CHECK (units[result].get () == four_ptr);
24393 }
24394
24395 }
24396 }
24397
24398 #endif /* GDB_SELF_TEST */
24399
24400 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24401
24402 static void
24403 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24404 enum language pretend_language)
24405 {
24406 struct attribute *attr;
24407
24408 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24409
24410 /* Set the language we're debugging. */
24411 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24412 if (cu->producer != nullptr
24413 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
24414 {
24415 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24416 attribute is not standardised yet. As a workaround for the
24417 language detection we fall back to the DW_AT_producer
24418 string. */
24419 cu->per_cu->lang = language_opencl;
24420 }
24421 else if (cu->producer != nullptr
24422 && strstr (cu->producer, "GNU Go ") != NULL)
24423 {
24424 /* Similar hack for Go. */
24425 cu->per_cu->lang = language_go;
24426 }
24427 else if (attr != nullptr)
24428 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
24429 else
24430 cu->per_cu->lang = pretend_language;
24431 cu->language_defn = language_def (cu->per_cu->lang);
24432 }
24433
24434 /* See read.h. */
24435
24436 dwarf2_cu *
24437 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24438 {
24439 auto it = m_dwarf2_cus.find (per_cu);
24440 if (it == m_dwarf2_cus.end ())
24441 return nullptr;
24442
24443 return it->second;
24444 }
24445
24446 /* See read.h. */
24447
24448 void
24449 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24450 {
24451 gdb_assert (this->get_cu (per_cu) == nullptr);
24452
24453 m_dwarf2_cus[per_cu] = cu;
24454 }
24455
24456 /* See read.h. */
24457
24458 void
24459 dwarf2_per_objfile::age_comp_units ()
24460 {
24461 dwarf_read_debug_printf_v ("running");
24462
24463 /* This is not expected to be called in the middle of CU expansion. There is
24464 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24465 loaded in memory. Calling age_comp_units while the queue is in use could
24466 make us free the DIEs for a CU that is in the queue and therefore break
24467 that invariant. */
24468 gdb_assert (!this->per_bfd->queue.has_value ());
24469
24470 /* Start by clearing all marks. */
24471 for (auto pair : m_dwarf2_cus)
24472 pair.second->clear_mark ();
24473
24474 /* Traverse all CUs, mark them and their dependencies if used recently
24475 enough. */
24476 for (auto pair : m_dwarf2_cus)
24477 {
24478 dwarf2_cu *cu = pair.second;
24479
24480 cu->last_used++;
24481 if (cu->last_used <= dwarf_max_cache_age)
24482 cu->mark ();
24483 }
24484
24485 /* Delete all CUs still not marked. */
24486 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24487 {
24488 dwarf2_cu *cu = it->second;
24489
24490 if (!cu->is_marked ())
24491 {
24492 dwarf_read_debug_printf_v ("deleting old CU %s",
24493 sect_offset_str (cu->per_cu->sect_off));
24494 delete cu;
24495 it = m_dwarf2_cus.erase (it);
24496 }
24497 else
24498 it++;
24499 }
24500 }
24501
24502 /* See read.h. */
24503
24504 void
24505 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24506 {
24507 auto it = m_dwarf2_cus.find (per_cu);
24508 if (it == m_dwarf2_cus.end ())
24509 return;
24510
24511 delete it->second;
24512
24513 m_dwarf2_cus.erase (it);
24514 }
24515
24516 dwarf2_per_objfile::~dwarf2_per_objfile ()
24517 {
24518 remove_all_cus ();
24519 }
24520
24521 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24522 We store these in a hash table separate from the DIEs, and preserve them
24523 when the DIEs are flushed out of cache.
24524
24525 The CU "per_cu" pointer is needed because offset alone is not enough to
24526 uniquely identify the type. A file may have multiple .debug_types sections,
24527 or the type may come from a DWO file. Furthermore, while it's more logical
24528 to use per_cu->section+offset, with Fission the section with the data is in
24529 the DWO file but we don't know that section at the point we need it.
24530 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24531 because we can enter the lookup routine, get_die_type_at_offset, from
24532 outside this file, and thus won't necessarily have PER_CU->cu.
24533 Fortunately, PER_CU is stable for the life of the objfile. */
24534
24535 struct dwarf2_per_cu_offset_and_type
24536 {
24537 const struct dwarf2_per_cu_data *per_cu;
24538 sect_offset sect_off;
24539 struct type *type;
24540 };
24541
24542 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24543
24544 static hashval_t
24545 per_cu_offset_and_type_hash (const void *item)
24546 {
24547 const struct dwarf2_per_cu_offset_and_type *ofs
24548 = (const struct dwarf2_per_cu_offset_and_type *) item;
24549
24550 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24551 }
24552
24553 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24554
24555 static int
24556 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24557 {
24558 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24559 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24560 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24561 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24562
24563 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24564 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24565 }
24566
24567 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24568 table if necessary. For convenience, return TYPE.
24569
24570 The DIEs reading must have careful ordering to:
24571 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24572 reading current DIE.
24573 * Not trying to dereference contents of still incompletely read in types
24574 while reading in other DIEs.
24575 * Enable referencing still incompletely read in types just by a pointer to
24576 the type without accessing its fields.
24577
24578 Therefore caller should follow these rules:
24579 * Try to fetch any prerequisite types we may need to build this DIE type
24580 before building the type and calling set_die_type.
24581 * After building type call set_die_type for current DIE as soon as
24582 possible before fetching more types to complete the current type.
24583 * Make the type as complete as possible before fetching more types. */
24584
24585 static struct type *
24586 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24587 bool skip_data_location)
24588 {
24589 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24590 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24591 struct objfile *objfile = per_objfile->objfile;
24592 struct attribute *attr;
24593 struct dynamic_prop prop;
24594
24595 /* For Ada types, make sure that the gnat-specific data is always
24596 initialized (if not already set). There are a few types where
24597 we should not be doing so, because the type-specific area is
24598 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24599 where the type-specific area is used to store the floatformat).
24600 But this is not a problem, because the gnat-specific information
24601 is actually not needed for these types. */
24602 if (need_gnat_info (cu)
24603 && type->code () != TYPE_CODE_FUNC
24604 && type->code () != TYPE_CODE_FLT
24605 && type->code () != TYPE_CODE_METHODPTR
24606 && type->code () != TYPE_CODE_MEMBERPTR
24607 && type->code () != TYPE_CODE_METHOD
24608 && type->code () != TYPE_CODE_FIXED_POINT
24609 && !HAVE_GNAT_AUX_INFO (type))
24610 INIT_GNAT_SPECIFIC (type);
24611
24612 /* Read DW_AT_allocated and set in type. */
24613 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24614 if (attr != NULL)
24615 {
24616 struct type *prop_type = cu->addr_sized_int_type (false);
24617 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24618 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24619 }
24620
24621 /* Read DW_AT_associated and set in type. */
24622 attr = dwarf2_attr (die, DW_AT_associated, cu);
24623 if (attr != NULL)
24624 {
24625 struct type *prop_type = cu->addr_sized_int_type (false);
24626 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24627 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24628 }
24629
24630 /* Read DW_AT_data_location and set in type. */
24631 if (!skip_data_location)
24632 {
24633 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24634 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24635 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24636 }
24637
24638 if (per_objfile->die_type_hash == NULL)
24639 per_objfile->die_type_hash
24640 = htab_up (htab_create_alloc (127,
24641 per_cu_offset_and_type_hash,
24642 per_cu_offset_and_type_eq,
24643 NULL, xcalloc, xfree));
24644
24645 ofs.per_cu = cu->per_cu;
24646 ofs.sect_off = die->sect_off;
24647 ofs.type = type;
24648 slot = (struct dwarf2_per_cu_offset_and_type **)
24649 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24650 if (*slot)
24651 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24652 sect_offset_str (die->sect_off));
24653 *slot = XOBNEW (&objfile->objfile_obstack,
24654 struct dwarf2_per_cu_offset_and_type);
24655 **slot = ofs;
24656 return type;
24657 }
24658
24659 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24660 or return NULL if the die does not have a saved type. */
24661
24662 static struct type *
24663 get_die_type_at_offset (sect_offset sect_off,
24664 dwarf2_per_cu_data *per_cu,
24665 dwarf2_per_objfile *per_objfile)
24666 {
24667 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24668
24669 if (per_objfile->die_type_hash == NULL)
24670 return NULL;
24671
24672 ofs.per_cu = per_cu;
24673 ofs.sect_off = sect_off;
24674 slot = ((struct dwarf2_per_cu_offset_and_type *)
24675 htab_find (per_objfile->die_type_hash.get (), &ofs));
24676 if (slot)
24677 return slot->type;
24678 else
24679 return NULL;
24680 }
24681
24682 /* Look up the type for DIE in CU in die_type_hash,
24683 or return NULL if DIE does not have a saved type. */
24684
24685 static struct type *
24686 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24687 {
24688 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24689 }
24690
24691 /* Trivial hash function for partial_die_info: the hash value of a DIE
24692 is its offset in .debug_info for this objfile. */
24693
24694 static hashval_t
24695 partial_die_hash (const void *item)
24696 {
24697 const struct partial_die_info *part_die
24698 = (const struct partial_die_info *) item;
24699
24700 return to_underlying (part_die->sect_off);
24701 }
24702
24703 /* Trivial comparison function for partial_die_info structures: two DIEs
24704 are equal if they have the same offset. */
24705
24706 static int
24707 partial_die_eq (const void *item_lhs, const void *item_rhs)
24708 {
24709 const struct partial_die_info *part_die_lhs
24710 = (const struct partial_die_info *) item_lhs;
24711 const struct partial_die_info *part_die_rhs
24712 = (const struct partial_die_info *) item_rhs;
24713
24714 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24715 }
24716
24717 struct cmd_list_element *set_dwarf_cmdlist;
24718 struct cmd_list_element *show_dwarf_cmdlist;
24719
24720 static void
24721 show_check_physname (struct ui_file *file, int from_tty,
24722 struct cmd_list_element *c, const char *value)
24723 {
24724 fprintf_filtered (file,
24725 _("Whether to check \"physname\" is %s.\n"),
24726 value);
24727 }
24728
24729 void _initialize_dwarf2_read ();
24730 void
24731 _initialize_dwarf2_read ()
24732 {
24733 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24734 Set DWARF specific variables.\n\
24735 Configure DWARF variables such as the cache size."),
24736 &set_dwarf_cmdlist,
24737 0/*allow-unknown*/, &maintenance_set_cmdlist);
24738
24739 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24740 Show DWARF specific variables.\n\
24741 Show DWARF variables such as the cache size."),
24742 &show_dwarf_cmdlist,
24743 0/*allow-unknown*/, &maintenance_show_cmdlist);
24744
24745 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24746 &dwarf_max_cache_age, _("\
24747 Set the upper bound on the age of cached DWARF compilation units."), _("\
24748 Show the upper bound on the age of cached DWARF compilation units."), _("\
24749 A higher limit means that cached compilation units will be stored\n\
24750 in memory longer, and more total memory will be used. Zero disables\n\
24751 caching, which can slow down startup."),
24752 NULL,
24753 show_dwarf_max_cache_age,
24754 &set_dwarf_cmdlist,
24755 &show_dwarf_cmdlist);
24756
24757 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24758 Set debugging of the DWARF reader."), _("\
24759 Show debugging of the DWARF reader."), _("\
24760 When enabled (non-zero), debugging messages are printed during DWARF\n\
24761 reading and symtab expansion. A value of 1 (one) provides basic\n\
24762 information. A value greater than 1 provides more verbose information."),
24763 NULL,
24764 NULL,
24765 &setdebuglist, &showdebuglist);
24766
24767 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24768 Set debugging of the DWARF DIE reader."), _("\
24769 Show debugging of the DWARF DIE reader."), _("\
24770 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24771 The value is the maximum depth to print."),
24772 NULL,
24773 NULL,
24774 &setdebuglist, &showdebuglist);
24775
24776 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24777 Set debugging of the dwarf line reader."), _("\
24778 Show debugging of the dwarf line reader."), _("\
24779 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24780 A value of 1 (one) provides basic information.\n\
24781 A value greater than 1 provides more verbose information."),
24782 NULL,
24783 NULL,
24784 &setdebuglist, &showdebuglist);
24785
24786 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24787 Set cross-checking of \"physname\" code against demangler."), _("\
24788 Show cross-checking of \"physname\" code against demangler."), _("\
24789 When enabled, GDB's internal \"physname\" code is checked against\n\
24790 the demangler."),
24791 NULL, show_check_physname,
24792 &setdebuglist, &showdebuglist);
24793
24794 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24795 no_class, &use_deprecated_index_sections, _("\
24796 Set whether to use deprecated gdb_index sections."), _("\
24797 Show whether to use deprecated gdb_index sections."), _("\
24798 When enabled, deprecated .gdb_index sections are used anyway.\n\
24799 Normally they are ignored either because of a missing feature or\n\
24800 performance issue.\n\
24801 Warning: This option must be enabled before gdb reads the file."),
24802 NULL,
24803 NULL,
24804 &setlist, &showlist);
24805
24806 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24807 &dwarf2_locexpr_funcs);
24808 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24809 &dwarf2_loclist_funcs);
24810
24811 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24812 &dwarf2_block_frame_base_locexpr_funcs);
24813 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24814 &dwarf2_block_frame_base_loclist_funcs);
24815
24816 #if GDB_SELF_TEST
24817 selftests::register_test ("dw2_expand_symtabs_matching",
24818 selftests::dw2_expand_symtabs_matching::run_test);
24819 selftests::register_test ("dwarf2_find_containing_comp_unit",
24820 selftests::find_containing_comp_unit::run_test);
24821 #endif
24822 }