]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Set is_debug_types in allocate_signatured_type
[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
836 /* Adjust the partial die before generating a symbol for it. This
837 function may set the is_external flag or change the DIE's
838 name. */
839 void fixup (struct dwarf2_cu *cu);
840
841 /* Read a minimal amount of information into the minimal die
842 structure. */
843 const gdb_byte *read (const struct die_reader_specs *reader,
844 const struct abbrev_info &abbrev,
845 const gdb_byte *info_ptr);
846
847 /* Compute the name of this partial DIE. This memoizes the
848 result, so it is safe to call multiple times. */
849 const char *name (dwarf2_cu *cu);
850
851 /* Offset of this DIE. */
852 const sect_offset sect_off;
853
854 /* DWARF-2 tag for this DIE. */
855 const ENUM_BITFIELD(dwarf_tag) tag : 16;
856
857 /* Assorted flags describing the data found in this DIE. */
858 const unsigned int has_children : 1;
859
860 unsigned int is_external : 1;
861 unsigned int is_declaration : 1;
862 unsigned int has_type : 1;
863 unsigned int has_specification : 1;
864 unsigned int has_pc_info : 1;
865 unsigned int may_be_inlined : 1;
866
867 /* This DIE has been marked DW_AT_main_subprogram. */
868 unsigned int main_subprogram : 1;
869
870 /* Flag set if the SCOPE field of this structure has been
871 computed. */
872 unsigned int scope_set : 1;
873
874 /* Flag set if the DIE has a byte_size attribute. */
875 unsigned int has_byte_size : 1;
876
877 /* Flag set if the DIE has a DW_AT_const_value attribute. */
878 unsigned int has_const_value : 1;
879
880 /* Flag set if any of the DIE's children are template arguments. */
881 unsigned int has_template_arguments : 1;
882
883 /* Flag set if fixup has been called on this die. */
884 unsigned int fixup_called : 1;
885
886 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
887 unsigned int is_dwz : 1;
888
889 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
890 unsigned int spec_is_dwz : 1;
891
892 unsigned int canonical_name : 1;
893
894 /* The name of this DIE. Normally the value of DW_AT_name, but
895 sometimes a default name for unnamed DIEs. */
896 const char *raw_name = nullptr;
897
898 /* The linkage name, if present. */
899 const char *linkage_name = nullptr;
900
901 /* The scope to prepend to our children. This is generally
902 allocated on the comp_unit_obstack, so will disappear
903 when this compilation unit leaves the cache. */
904 const char *scope = nullptr;
905
906 /* Some data associated with the partial DIE. The tag determines
907 which field is live. */
908 union
909 {
910 /* The location description associated with this DIE, if any. */
911 struct dwarf_block *locdesc;
912 /* The offset of an import, for DW_TAG_imported_unit. */
913 sect_offset sect_off;
914 } d {};
915
916 /* If HAS_PC_INFO, the PC range associated with this DIE. */
917 CORE_ADDR lowpc = 0;
918 CORE_ADDR highpc = 0;
919
920 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
921 DW_AT_sibling, if any. */
922 /* NOTE: This member isn't strictly necessary, partial_die_info::read
923 could return DW_AT_sibling values to its caller load_partial_dies. */
924 const gdb_byte *sibling = nullptr;
925
926 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
927 DW_AT_specification (or DW_AT_abstract_origin or
928 DW_AT_extension). */
929 sect_offset spec_offset {};
930
931 /* Pointers to this DIE's parent, first child, and next sibling,
932 if any. */
933 struct partial_die_info *die_parent = nullptr;
934 struct partial_die_info *die_child = nullptr;
935 struct partial_die_info *die_sibling = nullptr;
936
937 friend struct partial_die_info *
938 dwarf2_cu::find_partial_die (sect_offset sect_off);
939
940 private:
941 /* Only need to do look up in dwarf2_cu::find_partial_die. */
942 partial_die_info (sect_offset sect_off)
943 : partial_die_info (sect_off, DW_TAG_padding, 0)
944 {
945 }
946
947 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
948 int has_children_)
949 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
950 {
951 is_external = 0;
952 is_declaration = 0;
953 has_type = 0;
954 has_specification = 0;
955 has_pc_info = 0;
956 may_be_inlined = 0;
957 main_subprogram = 0;
958 scope_set = 0;
959 has_byte_size = 0;
960 has_const_value = 0;
961 has_template_arguments = 0;
962 fixup_called = 0;
963 is_dwz = 0;
964 spec_is_dwz = 0;
965 canonical_name = 0;
966 }
967 };
968
969 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
970 but this would require a corresponding change in unpack_field_as_long
971 and friends. */
972 static int bits_per_byte = 8;
973
974 struct variant_part_builder;
975
976 /* When reading a variant, we track a bit more information about the
977 field, and store it in an object of this type. */
978
979 struct variant_field
980 {
981 int first_field = -1;
982 int last_field = -1;
983
984 /* A variant can contain other variant parts. */
985 std::vector<variant_part_builder> variant_parts;
986
987 /* If we see a DW_TAG_variant, then this will be set if this is the
988 default branch. */
989 bool default_branch = false;
990 /* If we see a DW_AT_discr_value, then this will be the discriminant
991 value. */
992 ULONGEST discriminant_value = 0;
993 /* If we see a DW_AT_discr_list, then this is a pointer to the list
994 data. */
995 struct dwarf_block *discr_list_data = nullptr;
996 };
997
998 /* This represents a DW_TAG_variant_part. */
999
1000 struct variant_part_builder
1001 {
1002 /* The offset of the discriminant field. */
1003 sect_offset discriminant_offset {};
1004
1005 /* Variants that are direct children of this variant part. */
1006 std::vector<variant_field> variants;
1007
1008 /* True if we're currently reading a variant. */
1009 bool processing_variant = false;
1010 };
1011
1012 struct nextfield
1013 {
1014 int accessibility = 0;
1015 int virtuality = 0;
1016 /* Variant parts need to find the discriminant, which is a DIE
1017 reference. We track the section offset of each field to make
1018 this link. */
1019 sect_offset offset;
1020 struct field field {};
1021 };
1022
1023 struct fnfieldlist
1024 {
1025 const char *name = nullptr;
1026 std::vector<struct fn_field> fnfields;
1027 };
1028
1029 /* The routines that read and process dies for a C struct or C++ class
1030 pass lists of data member fields and lists of member function fields
1031 in an instance of a field_info structure, as defined below. */
1032 struct field_info
1033 {
1034 /* List of data member and baseclasses fields. */
1035 std::vector<struct nextfield> fields;
1036 std::vector<struct nextfield> baseclasses;
1037
1038 /* Set if the accessibility of one of the fields is not public. */
1039 bool non_public_fields = false;
1040
1041 /* Member function fieldlist array, contains name of possibly overloaded
1042 member function, number of overloaded member functions and a pointer
1043 to the head of the member function field chain. */
1044 std::vector<struct fnfieldlist> fnfieldlists;
1045
1046 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1047 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1048 std::vector<struct decl_field> typedef_field_list;
1049
1050 /* Nested types defined by this class and the number of elements in this
1051 list. */
1052 std::vector<struct decl_field> nested_types_list;
1053
1054 /* If non-null, this is the variant part we are currently
1055 reading. */
1056 variant_part_builder *current_variant_part = nullptr;
1057 /* This holds all the top-level variant parts attached to the type
1058 we're reading. */
1059 std::vector<variant_part_builder> variant_parts;
1060
1061 /* Return the total number of fields (including baseclasses). */
1062 int nfields () const
1063 {
1064 return fields.size () + baseclasses.size ();
1065 }
1066 };
1067
1068 /* Loaded secondary compilation units are kept in memory until they
1069 have not been referenced for the processing of this many
1070 compilation units. Set this to zero to disable caching. Cache
1071 sizes of up to at least twenty will improve startup time for
1072 typical inter-CU-reference binaries, at an obvious memory cost. */
1073 static int dwarf_max_cache_age = 5;
1074 static void
1075 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1076 struct cmd_list_element *c, const char *value)
1077 {
1078 fprintf_filtered (file, _("The upper bound on the age of cached "
1079 "DWARF compilation units is %s.\n"),
1080 value);
1081 }
1082 \f
1083 /* local function prototypes */
1084
1085 static void dwarf2_find_base_address (struct die_info *die,
1086 struct dwarf2_cu *cu);
1087
1088 static dwarf2_psymtab *create_partial_symtab
1089 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1090 const char *name);
1091
1092 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1093 const gdb_byte *info_ptr,
1094 struct die_info *type_unit_die);
1095
1096 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1097
1098 static void scan_partial_symbols (struct partial_die_info *,
1099 CORE_ADDR *, CORE_ADDR *,
1100 int, struct dwarf2_cu *);
1101
1102 static void add_partial_symbol (struct partial_die_info *,
1103 struct dwarf2_cu *);
1104
1105 static void add_partial_namespace (struct partial_die_info *pdi,
1106 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1107 int set_addrmap, struct dwarf2_cu *cu);
1108
1109 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1110 CORE_ADDR *highpc, int set_addrmap,
1111 struct dwarf2_cu *cu);
1112
1113 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1114 struct dwarf2_cu *cu);
1115
1116 static void add_partial_subprogram (struct partial_die_info *pdi,
1117 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1118 int need_pc, struct dwarf2_cu *cu);
1119
1120 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1121
1122 static struct partial_die_info *load_partial_dies
1123 (const struct die_reader_specs *, const gdb_byte *, int);
1124
1125 /* A pair of partial_die_info and compilation unit. */
1126 struct cu_partial_die_info
1127 {
1128 /* The compilation unit of the partial_die_info. */
1129 struct dwarf2_cu *cu;
1130 /* A partial_die_info. */
1131 struct partial_die_info *pdi;
1132
1133 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1134 : cu (cu),
1135 pdi (pdi)
1136 { /* Nothing. */ }
1137
1138 private:
1139 cu_partial_die_info () = delete;
1140 };
1141
1142 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1143 struct dwarf2_cu *);
1144
1145 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1146 struct attribute *,
1147 const struct attr_abbrev *,
1148 const gdb_byte *);
1149
1150 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1151 struct attribute *attr, dwarf_tag tag);
1152
1153 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1154
1155 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1156 dwarf2_section_info *, sect_offset);
1157
1158 static const char *read_indirect_string
1159 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1160 const struct comp_unit_head *, unsigned int *);
1161
1162 static const char *read_indirect_string_at_offset
1163 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1164
1165 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1166 const gdb_byte *,
1167 unsigned int *);
1168
1169 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1170 ULONGEST str_index);
1171
1172 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1173 ULONGEST str_index);
1174
1175 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1176
1177 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1178 struct dwarf2_cu *);
1179
1180 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1181 struct dwarf2_cu *cu);
1182
1183 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1184
1185 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1186 struct dwarf2_cu *cu);
1187
1188 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1189
1190 static struct die_info *die_specification (struct die_info *die,
1191 struct dwarf2_cu **);
1192
1193 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1194 struct dwarf2_cu *cu);
1195
1196 static void dwarf_decode_lines (struct line_header *, const char *,
1197 struct dwarf2_cu *, dwarf2_psymtab *,
1198 CORE_ADDR, int decode_mapping);
1199
1200 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1201 const char *);
1202
1203 static struct symbol *new_symbol (struct die_info *, struct type *,
1204 struct dwarf2_cu *, struct symbol * = NULL);
1205
1206 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1207 struct dwarf2_cu *);
1208
1209 static void dwarf2_const_value_attr (const struct attribute *attr,
1210 struct type *type,
1211 const char *name,
1212 struct obstack *obstack,
1213 struct dwarf2_cu *cu, LONGEST *value,
1214 const gdb_byte **bytes,
1215 struct dwarf2_locexpr_baton **baton);
1216
1217 static struct type *read_subrange_index_type (struct die_info *die,
1218 struct dwarf2_cu *cu);
1219
1220 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1221
1222 static int need_gnat_info (struct dwarf2_cu *);
1223
1224 static struct type *die_descriptive_type (struct die_info *,
1225 struct dwarf2_cu *);
1226
1227 static void set_descriptive_type (struct type *, struct die_info *,
1228 struct dwarf2_cu *);
1229
1230 static struct type *die_containing_type (struct die_info *,
1231 struct dwarf2_cu *);
1232
1233 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1234 struct dwarf2_cu *);
1235
1236 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1237
1238 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1239
1240 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1241
1242 static char *typename_concat (struct obstack *obs, const char *prefix,
1243 const char *suffix, int physname,
1244 struct dwarf2_cu *cu);
1245
1246 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1247
1248 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1249
1250 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1251
1252 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1253
1254 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1255
1256 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1257
1258 /* Return the .debug_loclists section to use for cu. */
1259 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1260
1261 /* Return the .debug_rnglists section to use for cu. */
1262 static struct dwarf2_section_info *cu_debug_rnglists_section
1263 (struct dwarf2_cu *cu, dwarf_tag tag);
1264
1265 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1266 values. Keep the items ordered with increasing constraints compliance. */
1267 enum pc_bounds_kind
1268 {
1269 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1270 PC_BOUNDS_NOT_PRESENT,
1271
1272 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1273 were present but they do not form a valid range of PC addresses. */
1274 PC_BOUNDS_INVALID,
1275
1276 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1277 PC_BOUNDS_RANGES,
1278
1279 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1280 PC_BOUNDS_HIGH_LOW,
1281 };
1282
1283 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1284 CORE_ADDR *, CORE_ADDR *,
1285 struct dwarf2_cu *,
1286 dwarf2_psymtab *);
1287
1288 static void get_scope_pc_bounds (struct die_info *,
1289 CORE_ADDR *, CORE_ADDR *,
1290 struct dwarf2_cu *);
1291
1292 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1293 CORE_ADDR, struct dwarf2_cu *);
1294
1295 static void dwarf2_add_field (struct field_info *, struct die_info *,
1296 struct dwarf2_cu *);
1297
1298 static void dwarf2_attach_fields_to_type (struct field_info *,
1299 struct type *, struct dwarf2_cu *);
1300
1301 static void dwarf2_add_member_fn (struct field_info *,
1302 struct die_info *, struct type *,
1303 struct dwarf2_cu *);
1304
1305 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1306 struct type *,
1307 struct dwarf2_cu *);
1308
1309 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1310
1311 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1312
1313 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1314
1315 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1316
1317 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1318
1319 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1320
1321 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1322
1323 static struct type *read_module_type (struct die_info *die,
1324 struct dwarf2_cu *cu);
1325
1326 static const char *namespace_name (struct die_info *die,
1327 int *is_anonymous, struct dwarf2_cu *);
1328
1329 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1330
1331 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1332 bool * = nullptr);
1333
1334 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1335 struct dwarf2_cu *);
1336
1337 static struct die_info *read_die_and_siblings_1
1338 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1339 struct die_info *);
1340
1341 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1342 const gdb_byte *info_ptr,
1343 const gdb_byte **new_info_ptr,
1344 struct die_info *parent);
1345
1346 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1347 struct die_info **, const gdb_byte *,
1348 int);
1349
1350 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1351 struct die_info **, const gdb_byte *);
1352
1353 static void process_die (struct die_info *, struct dwarf2_cu *);
1354
1355 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1356 struct objfile *);
1357
1358 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1359
1360 static const char *dwarf2_full_name (const char *name,
1361 struct die_info *die,
1362 struct dwarf2_cu *cu);
1363
1364 static const char *dwarf2_physname (const char *name, struct die_info *die,
1365 struct dwarf2_cu *cu);
1366
1367 static struct die_info *dwarf2_extension (struct die_info *die,
1368 struct dwarf2_cu **);
1369
1370 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1371
1372 static void dump_die_for_error (struct die_info *);
1373
1374 static void dump_die_1 (struct ui_file *, int level, int max_level,
1375 struct die_info *);
1376
1377 /*static*/ void dump_die (struct die_info *, int max_level);
1378
1379 static void store_in_ref_table (struct die_info *,
1380 struct dwarf2_cu *);
1381
1382 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1383 const struct attribute *,
1384 struct dwarf2_cu **);
1385
1386 static struct die_info *follow_die_ref (struct die_info *,
1387 const struct attribute *,
1388 struct dwarf2_cu **);
1389
1390 static struct die_info *follow_die_sig (struct die_info *,
1391 const struct attribute *,
1392 struct dwarf2_cu **);
1393
1394 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1395 struct dwarf2_cu *);
1396
1397 static struct type *get_DW_AT_signature_type (struct die_info *,
1398 const struct attribute *,
1399 struct dwarf2_cu *);
1400
1401 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1402 dwarf2_per_objfile *per_objfile);
1403
1404 static void read_signatured_type (signatured_type *sig_type,
1405 dwarf2_per_objfile *per_objfile);
1406
1407 static int attr_to_dynamic_prop (const struct attribute *attr,
1408 struct die_info *die, struct dwarf2_cu *cu,
1409 struct dynamic_prop *prop, struct type *type);
1410
1411 /* memory allocation interface */
1412
1413 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1414
1415 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1416
1417 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1418
1419 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1420 struct dwarf2_loclist_baton *baton,
1421 const struct attribute *attr);
1422
1423 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1424 struct symbol *sym,
1425 struct dwarf2_cu *cu,
1426 int is_block);
1427
1428 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1429 const gdb_byte *info_ptr,
1430 const struct abbrev_info *abbrev);
1431
1432 static hashval_t partial_die_hash (const void *item);
1433
1434 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1435
1436 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1437 (sect_offset sect_off, unsigned int offset_in_dwz,
1438 dwarf2_per_objfile *per_objfile);
1439
1440 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1441 struct die_info *comp_unit_die,
1442 enum language pretend_language);
1443
1444 static struct type *set_die_type (struct die_info *, struct type *,
1445 struct dwarf2_cu *, bool = false);
1446
1447 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1448
1449 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1450 dwarf2_per_objfile *per_objfile,
1451 dwarf2_cu *existing_cu,
1452 bool skip_partial,
1453 enum language pretend_language);
1454
1455 static void process_full_comp_unit (dwarf2_cu *cu,
1456 enum language pretend_language);
1457
1458 static void process_full_type_unit (dwarf2_cu *cu,
1459 enum language pretend_language);
1460
1461 static struct type *get_die_type_at_offset (sect_offset,
1462 dwarf2_per_cu_data *per_cu,
1463 dwarf2_per_objfile *per_objfile);
1464
1465 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1466
1467 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1468 dwarf2_per_objfile *per_objfile,
1469 enum language pretend_language);
1470
1471 static void process_queue (dwarf2_per_objfile *per_objfile);
1472
1473 /* Class, the destructor of which frees all allocated queue entries. This
1474 will only have work to do if an error was thrown while processing the
1475 dwarf. If no error was thrown then the queue entries should have all
1476 been processed, and freed, as we went along. */
1477
1478 class dwarf2_queue_guard
1479 {
1480 public:
1481 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1482 : m_per_objfile (per_objfile)
1483 {
1484 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1485
1486 m_per_objfile->per_bfd->queue.emplace ();
1487 }
1488
1489 /* Free any entries remaining on the queue. There should only be
1490 entries left if we hit an error while processing the dwarf. */
1491 ~dwarf2_queue_guard ()
1492 {
1493 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1494
1495 m_per_objfile->per_bfd->queue.reset ();
1496 }
1497
1498 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1499
1500 private:
1501 dwarf2_per_objfile *m_per_objfile;
1502 };
1503
1504 dwarf2_queue_item::~dwarf2_queue_item ()
1505 {
1506 /* Anything still marked queued is likely to be in an
1507 inconsistent state, so discard it. */
1508 if (per_cu->queued)
1509 {
1510 per_objfile->remove_cu (per_cu);
1511 per_cu->queued = 0;
1512 }
1513 }
1514
1515 /* See dwarf2/read.h. */
1516
1517 void
1518 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1519 {
1520 if (data->is_debug_types)
1521 delete static_cast<signatured_type *> (data);
1522 else
1523 delete data;
1524 }
1525
1526 /* The return type of find_file_and_directory. Note, the enclosed
1527 string pointers are only valid while this object is valid. */
1528
1529 struct file_and_directory
1530 {
1531 /* The filename. This is never NULL. */
1532 const char *name;
1533
1534 /* The compilation directory. NULL if not known. If we needed to
1535 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1536 points directly to the DW_AT_comp_dir string attribute owned by
1537 the obstack that owns the DIE. */
1538 const char *comp_dir;
1539
1540 /* If we needed to build a new string for comp_dir, this is what
1541 owns the storage. */
1542 std::string comp_dir_storage;
1543 };
1544
1545 static file_and_directory find_file_and_directory (struct die_info *die,
1546 struct dwarf2_cu *cu);
1547
1548 static htab_up allocate_signatured_type_table ();
1549
1550 static htab_up allocate_dwo_unit_table ();
1551
1552 static struct dwo_unit *lookup_dwo_unit_in_dwp
1553 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1554 const char *comp_dir, ULONGEST signature, int is_debug_types);
1555
1556 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1557
1558 static struct dwo_unit *lookup_dwo_comp_unit
1559 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1560 ULONGEST signature);
1561
1562 static struct dwo_unit *lookup_dwo_type_unit
1563 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1564
1565 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1566
1567 /* A unique pointer to a dwo_file. */
1568
1569 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1570
1571 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1572
1573 static void check_producer (struct dwarf2_cu *cu);
1574 \f
1575 /* Various complaints about symbol reading that don't abort the process. */
1576
1577 static void
1578 dwarf2_debug_line_missing_file_complaint (void)
1579 {
1580 complaint (_(".debug_line section has line data without a file"));
1581 }
1582
1583 static void
1584 dwarf2_debug_line_missing_end_sequence_complaint (void)
1585 {
1586 complaint (_(".debug_line section has line "
1587 "program sequence without an end"));
1588 }
1589
1590 static void
1591 dwarf2_complex_location_expr_complaint (void)
1592 {
1593 complaint (_("location expression too complex"));
1594 }
1595
1596 static void
1597 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1598 int arg3)
1599 {
1600 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1601 arg1, arg2, arg3);
1602 }
1603
1604 static void
1605 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1606 {
1607 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1608 arg1, arg2);
1609 }
1610
1611 /* Hash function for line_header_hash. */
1612
1613 static hashval_t
1614 line_header_hash (const struct line_header *ofs)
1615 {
1616 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1617 }
1618
1619 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1620
1621 static hashval_t
1622 line_header_hash_voidp (const void *item)
1623 {
1624 const struct line_header *ofs = (const struct line_header *) item;
1625
1626 return line_header_hash (ofs);
1627 }
1628
1629 /* Equality function for line_header_hash. */
1630
1631 static int
1632 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1633 {
1634 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1635 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1636
1637 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1638 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1639 }
1640
1641 \f
1642
1643 /* See declaration. */
1644
1645 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1646 bool can_copy_)
1647 : obfd (obfd),
1648 can_copy (can_copy_)
1649 {
1650 if (names == NULL)
1651 names = &dwarf2_elf_names;
1652
1653 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1654 locate_sections (obfd, sec, *names);
1655 }
1656
1657 dwarf2_per_bfd::~dwarf2_per_bfd ()
1658 {
1659 for (auto &per_cu : all_comp_units)
1660 per_cu->imported_symtabs_free ();
1661
1662 /* Everything else should be on this->obstack. */
1663 }
1664
1665 /* See read.h. */
1666
1667 void
1668 dwarf2_per_objfile::remove_all_cus ()
1669 {
1670 gdb_assert (!this->per_bfd->queue.has_value ());
1671
1672 for (auto pair : m_dwarf2_cus)
1673 delete pair.second;
1674
1675 m_dwarf2_cus.clear ();
1676 }
1677
1678 /* A helper class that calls free_cached_comp_units on
1679 destruction. */
1680
1681 class free_cached_comp_units
1682 {
1683 public:
1684
1685 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1686 : m_per_objfile (per_objfile)
1687 {
1688 }
1689
1690 ~free_cached_comp_units ()
1691 {
1692 m_per_objfile->remove_all_cus ();
1693 }
1694
1695 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1696
1697 private:
1698
1699 dwarf2_per_objfile *m_per_objfile;
1700 };
1701
1702 /* See read.h. */
1703
1704 bool
1705 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1706 {
1707 gdb_assert (per_cu->index < this->m_symtabs.size ());
1708
1709 return this->m_symtabs[per_cu->index] != nullptr;
1710 }
1711
1712 /* See read.h. */
1713
1714 compunit_symtab *
1715 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1716 {
1717 gdb_assert (per_cu->index < this->m_symtabs.size ());
1718
1719 return this->m_symtabs[per_cu->index];
1720 }
1721
1722 /* See read.h. */
1723
1724 void
1725 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1726 compunit_symtab *symtab)
1727 {
1728 gdb_assert (per_cu->index < this->m_symtabs.size ());
1729 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1730
1731 this->m_symtabs[per_cu->index] = symtab;
1732 }
1733
1734 /* Try to locate the sections we need for DWARF 2 debugging
1735 information and return true if we have enough to do something.
1736 NAMES points to the dwarf2 section names, or is NULL if the standard
1737 ELF names are used. CAN_COPY is true for formats where symbol
1738 interposition is possible and so symbol values must follow copy
1739 relocation rules. */
1740
1741 int
1742 dwarf2_has_info (struct objfile *objfile,
1743 const struct dwarf2_debug_sections *names,
1744 bool can_copy)
1745 {
1746 if (objfile->flags & OBJF_READNEVER)
1747 return 0;
1748
1749 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1750
1751 if (per_objfile == NULL)
1752 {
1753 dwarf2_per_bfd *per_bfd;
1754
1755 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1756 BFD doesn't require relocations.
1757
1758 We don't share with objfiles for which -readnow was requested,
1759 because it would complicate things when loading the same BFD with
1760 -readnow and then without -readnow. */
1761 if (!gdb_bfd_requires_relocations (objfile->obfd)
1762 && (objfile->flags & OBJF_READNOW) == 0)
1763 {
1764 /* See if one has been created for this BFD yet. */
1765 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1766
1767 if (per_bfd == nullptr)
1768 {
1769 /* No, create it now. */
1770 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1771 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1772 }
1773 }
1774 else
1775 {
1776 /* No sharing possible, create one specifically for this objfile. */
1777 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1778 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1779 }
1780
1781 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1782 }
1783
1784 return (!per_objfile->per_bfd->info.is_virtual
1785 && per_objfile->per_bfd->info.s.section != NULL
1786 && !per_objfile->per_bfd->abbrev.is_virtual
1787 && per_objfile->per_bfd->abbrev.s.section != NULL);
1788 }
1789
1790 /* See declaration. */
1791
1792 void
1793 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1794 const dwarf2_debug_sections &names)
1795 {
1796 flagword aflag = bfd_section_flags (sectp);
1797
1798 if ((aflag & SEC_HAS_CONTENTS) == 0)
1799 {
1800 }
1801 else if (elf_section_data (sectp)->this_hdr.sh_size
1802 > bfd_get_file_size (abfd))
1803 {
1804 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1805 warning (_("Discarding section %s which has a section size (%s"
1806 ") larger than the file size [in module %s]"),
1807 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1808 bfd_get_filename (abfd));
1809 }
1810 else if (names.info.matches (sectp->name))
1811 {
1812 this->info.s.section = sectp;
1813 this->info.size = bfd_section_size (sectp);
1814 }
1815 else if (names.abbrev.matches (sectp->name))
1816 {
1817 this->abbrev.s.section = sectp;
1818 this->abbrev.size = bfd_section_size (sectp);
1819 }
1820 else if (names.line.matches (sectp->name))
1821 {
1822 this->line.s.section = sectp;
1823 this->line.size = bfd_section_size (sectp);
1824 }
1825 else if (names.loc.matches (sectp->name))
1826 {
1827 this->loc.s.section = sectp;
1828 this->loc.size = bfd_section_size (sectp);
1829 }
1830 else if (names.loclists.matches (sectp->name))
1831 {
1832 this->loclists.s.section = sectp;
1833 this->loclists.size = bfd_section_size (sectp);
1834 }
1835 else if (names.macinfo.matches (sectp->name))
1836 {
1837 this->macinfo.s.section = sectp;
1838 this->macinfo.size = bfd_section_size (sectp);
1839 }
1840 else if (names.macro.matches (sectp->name))
1841 {
1842 this->macro.s.section = sectp;
1843 this->macro.size = bfd_section_size (sectp);
1844 }
1845 else if (names.str.matches (sectp->name))
1846 {
1847 this->str.s.section = sectp;
1848 this->str.size = bfd_section_size (sectp);
1849 }
1850 else if (names.str_offsets.matches (sectp->name))
1851 {
1852 this->str_offsets.s.section = sectp;
1853 this->str_offsets.size = bfd_section_size (sectp);
1854 }
1855 else if (names.line_str.matches (sectp->name))
1856 {
1857 this->line_str.s.section = sectp;
1858 this->line_str.size = bfd_section_size (sectp);
1859 }
1860 else if (names.addr.matches (sectp->name))
1861 {
1862 this->addr.s.section = sectp;
1863 this->addr.size = bfd_section_size (sectp);
1864 }
1865 else if (names.frame.matches (sectp->name))
1866 {
1867 this->frame.s.section = sectp;
1868 this->frame.size = bfd_section_size (sectp);
1869 }
1870 else if (names.eh_frame.matches (sectp->name))
1871 {
1872 this->eh_frame.s.section = sectp;
1873 this->eh_frame.size = bfd_section_size (sectp);
1874 }
1875 else if (names.ranges.matches (sectp->name))
1876 {
1877 this->ranges.s.section = sectp;
1878 this->ranges.size = bfd_section_size (sectp);
1879 }
1880 else if (names.rnglists.matches (sectp->name))
1881 {
1882 this->rnglists.s.section = sectp;
1883 this->rnglists.size = bfd_section_size (sectp);
1884 }
1885 else if (names.types.matches (sectp->name))
1886 {
1887 struct dwarf2_section_info type_section;
1888
1889 memset (&type_section, 0, sizeof (type_section));
1890 type_section.s.section = sectp;
1891 type_section.size = bfd_section_size (sectp);
1892
1893 this->types.push_back (type_section);
1894 }
1895 else if (names.gdb_index.matches (sectp->name))
1896 {
1897 this->gdb_index.s.section = sectp;
1898 this->gdb_index.size = bfd_section_size (sectp);
1899 }
1900 else if (names.debug_names.matches (sectp->name))
1901 {
1902 this->debug_names.s.section = sectp;
1903 this->debug_names.size = bfd_section_size (sectp);
1904 }
1905 else if (names.debug_aranges.matches (sectp->name))
1906 {
1907 this->debug_aranges.s.section = sectp;
1908 this->debug_aranges.size = bfd_section_size (sectp);
1909 }
1910
1911 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1912 && bfd_section_vma (sectp) == 0)
1913 this->has_section_at_zero = true;
1914 }
1915
1916 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1917 SECTION_NAME. */
1918
1919 void
1920 dwarf2_get_section_info (struct objfile *objfile,
1921 enum dwarf2_section_enum sect,
1922 asection **sectp, const gdb_byte **bufp,
1923 bfd_size_type *sizep)
1924 {
1925 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1926 struct dwarf2_section_info *info;
1927
1928 /* We may see an objfile without any DWARF, in which case we just
1929 return nothing. */
1930 if (per_objfile == NULL)
1931 {
1932 *sectp = NULL;
1933 *bufp = NULL;
1934 *sizep = 0;
1935 return;
1936 }
1937 switch (sect)
1938 {
1939 case DWARF2_DEBUG_FRAME:
1940 info = &per_objfile->per_bfd->frame;
1941 break;
1942 case DWARF2_EH_FRAME:
1943 info = &per_objfile->per_bfd->eh_frame;
1944 break;
1945 default:
1946 gdb_assert_not_reached ("unexpected section");
1947 }
1948
1949 info->read (objfile);
1950
1951 *sectp = info->get_bfd_section ();
1952 *bufp = info->buffer;
1953 *sizep = info->size;
1954 }
1955
1956 \f
1957 /* DWARF quick_symbol_functions support. */
1958
1959 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1960 unique line tables, so we maintain a separate table of all .debug_line
1961 derived entries to support the sharing.
1962 All the quick functions need is the list of file names. We discard the
1963 line_header when we're done and don't need to record it here. */
1964 struct quick_file_names
1965 {
1966 /* The data used to construct the hash key. */
1967 struct stmt_list_hash hash;
1968
1969 /* The number of entries in file_names, real_names. */
1970 unsigned int num_file_names;
1971
1972 /* The file names from the line table, after being run through
1973 file_full_name. */
1974 const char **file_names;
1975
1976 /* The file names from the line table after being run through
1977 gdb_realpath. These are computed lazily. */
1978 const char **real_names;
1979 };
1980
1981 /* When using the index (and thus not using psymtabs), each CU has an
1982 object of this type. This is used to hold information needed by
1983 the various "quick" methods. */
1984 struct dwarf2_per_cu_quick_data
1985 {
1986 /* The file table. This can be NULL if there was no file table
1987 or it's currently not read in.
1988 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
1989 struct quick_file_names *file_names;
1990
1991 /* A temporary mark bit used when iterating over all CUs in
1992 expand_symtabs_matching. */
1993 unsigned int mark : 1;
1994
1995 /* True if we've tried to read the file table and found there isn't one.
1996 There will be no point in trying to read it again next time. */
1997 unsigned int no_file_data : 1;
1998 };
1999
2000 /* A subclass of psymbol_functions that arranges to read the DWARF
2001 partial symbols when needed. */
2002 struct lazy_dwarf_reader : public psymbol_functions
2003 {
2004 using psymbol_functions::psymbol_functions;
2005
2006 bool can_lazily_read_symbols () override
2007 {
2008 return true;
2009 }
2010
2011 void read_partial_symbols (struct objfile *objfile) override
2012 {
2013 if (dwarf2_has_info (objfile, nullptr))
2014 dwarf2_build_psymtabs (objfile, this);
2015 }
2016 };
2017
2018 static quick_symbol_functions_up
2019 make_lazy_dwarf_reader ()
2020 {
2021 return quick_symbol_functions_up (new lazy_dwarf_reader);
2022 }
2023
2024 struct dwarf2_base_index_functions : public quick_symbol_functions
2025 {
2026 bool has_symbols (struct objfile *objfile) override;
2027
2028 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2029
2030 void forget_cached_source_info (struct objfile *objfile) override;
2031
2032 enum language lookup_global_symbol_language (struct objfile *objfile,
2033 const char *name,
2034 domain_enum domain,
2035 bool *symbol_found_p) override
2036 {
2037 *symbol_found_p = false;
2038 return language_unknown;
2039 }
2040
2041 void print_stats (struct objfile *objfile, bool print_bcache) override;
2042
2043 void expand_all_symtabs (struct objfile *objfile) override;
2044
2045 struct compunit_symtab *find_pc_sect_compunit_symtab
2046 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2047 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2048
2049 struct compunit_symtab *find_compunit_symtab_by_address
2050 (struct objfile *objfile, CORE_ADDR address) override
2051 {
2052 return nullptr;
2053 }
2054
2055 void map_symbol_filenames (struct objfile *objfile,
2056 gdb::function_view<symbol_filename_ftype> fun,
2057 bool need_fullname) override;
2058 };
2059
2060 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2061 {
2062 void dump (struct objfile *objfile) override;
2063
2064 void expand_matching_symbols
2065 (struct objfile *,
2066 const lookup_name_info &lookup_name,
2067 domain_enum domain,
2068 int global,
2069 symbol_compare_ftype *ordered_compare) override;
2070
2071 bool expand_symtabs_matching
2072 (struct objfile *objfile,
2073 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2074 const lookup_name_info *lookup_name,
2075 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2076 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2077 block_search_flags search_flags,
2078 domain_enum domain,
2079 enum search_domain kind) override;
2080 };
2081
2082 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2083 {
2084 void dump (struct objfile *objfile) override;
2085
2086 void expand_matching_symbols
2087 (struct objfile *,
2088 const lookup_name_info &lookup_name,
2089 domain_enum domain,
2090 int global,
2091 symbol_compare_ftype *ordered_compare) override;
2092
2093 bool expand_symtabs_matching
2094 (struct objfile *objfile,
2095 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2096 const lookup_name_info *lookup_name,
2097 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2098 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2099 block_search_flags search_flags,
2100 domain_enum domain,
2101 enum search_domain kind) override;
2102 };
2103
2104 static quick_symbol_functions_up
2105 make_dwarf_gdb_index ()
2106 {
2107 return quick_symbol_functions_up (new dwarf2_gdb_index);
2108 }
2109
2110 static quick_symbol_functions_up
2111 make_dwarf_debug_names ()
2112 {
2113 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2114 }
2115
2116 /* Utility hash function for a stmt_list_hash. */
2117
2118 static hashval_t
2119 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2120 {
2121 hashval_t v = 0;
2122
2123 if (stmt_list_hash->dwo_unit != NULL)
2124 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2125 v += to_underlying (stmt_list_hash->line_sect_off);
2126 return v;
2127 }
2128
2129 /* Utility equality function for a stmt_list_hash. */
2130
2131 static int
2132 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2133 const struct stmt_list_hash *rhs)
2134 {
2135 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2136 return 0;
2137 if (lhs->dwo_unit != NULL
2138 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2139 return 0;
2140
2141 return lhs->line_sect_off == rhs->line_sect_off;
2142 }
2143
2144 /* Hash function for a quick_file_names. */
2145
2146 static hashval_t
2147 hash_file_name_entry (const void *e)
2148 {
2149 const struct quick_file_names *file_data
2150 = (const struct quick_file_names *) e;
2151
2152 return hash_stmt_list_entry (&file_data->hash);
2153 }
2154
2155 /* Equality function for a quick_file_names. */
2156
2157 static int
2158 eq_file_name_entry (const void *a, const void *b)
2159 {
2160 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2161 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2162
2163 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2164 }
2165
2166 /* Delete function for a quick_file_names. */
2167
2168 static void
2169 delete_file_name_entry (void *e)
2170 {
2171 struct quick_file_names *file_data = (struct quick_file_names *) e;
2172 int i;
2173
2174 for (i = 0; i < file_data->num_file_names; ++i)
2175 {
2176 xfree ((void*) file_data->file_names[i]);
2177 if (file_data->real_names)
2178 xfree ((void*) file_data->real_names[i]);
2179 }
2180
2181 /* The space for the struct itself lives on the obstack, so we don't
2182 free it here. */
2183 }
2184
2185 /* Create a quick_file_names hash table. */
2186
2187 static htab_up
2188 create_quick_file_names_table (unsigned int nr_initial_entries)
2189 {
2190 return htab_up (htab_create_alloc (nr_initial_entries,
2191 hash_file_name_entry, eq_file_name_entry,
2192 delete_file_name_entry, xcalloc, xfree));
2193 }
2194
2195 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2196 function is unrelated to symtabs, symtab would have to be created afterwards.
2197 You should call age_cached_comp_units after processing the CU. */
2198
2199 static dwarf2_cu *
2200 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2201 bool skip_partial)
2202 {
2203 if (per_cu->is_debug_types)
2204 load_full_type_unit (per_cu, per_objfile);
2205 else
2206 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2207 skip_partial, language_minimal);
2208
2209 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2210 if (cu == nullptr)
2211 return nullptr; /* Dummy CU. */
2212
2213 dwarf2_find_base_address (cu->dies, cu);
2214
2215 return cu;
2216 }
2217
2218 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2219
2220 static void
2221 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2222 dwarf2_per_objfile *per_objfile, bool skip_partial)
2223 {
2224 /* Skip type_unit_groups, reading the type units they contain
2225 is handled elsewhere. */
2226 if (per_cu->type_unit_group_p ())
2227 return;
2228
2229 {
2230 /* The destructor of dwarf2_queue_guard frees any entries left on
2231 the queue. After this point we're guaranteed to leave this function
2232 with the dwarf queue empty. */
2233 dwarf2_queue_guard q_guard (per_objfile);
2234
2235 if (!per_objfile->symtab_set_p (per_cu))
2236 {
2237 queue_comp_unit (per_cu, per_objfile, language_minimal);
2238 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2239
2240 /* If we just loaded a CU from a DWO, and we're working with an index
2241 that may badly handle TUs, load all the TUs in that DWO as well.
2242 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2243 if (!per_cu->is_debug_types
2244 && cu != NULL
2245 && cu->dwo_unit != NULL
2246 && per_objfile->per_bfd->index_table != NULL
2247 && per_objfile->per_bfd->index_table->version <= 7
2248 /* DWP files aren't supported yet. */
2249 && get_dwp_file (per_objfile) == NULL)
2250 queue_and_load_all_dwo_tus (cu);
2251 }
2252
2253 process_queue (per_objfile);
2254 }
2255
2256 /* Age the cache, releasing compilation units that have not
2257 been used recently. */
2258 per_objfile->age_comp_units ();
2259 }
2260
2261 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2262 the per-objfile for which this symtab is instantiated.
2263
2264 Returns the resulting symbol table. */
2265
2266 static struct compunit_symtab *
2267 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2268 dwarf2_per_objfile *per_objfile,
2269 bool skip_partial)
2270 {
2271 gdb_assert (per_objfile->per_bfd->using_index);
2272
2273 if (!per_objfile->symtab_set_p (per_cu))
2274 {
2275 free_cached_comp_units freer (per_objfile);
2276 scoped_restore decrementer = increment_reading_symtab ();
2277 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2278 process_cu_includes (per_objfile);
2279 }
2280
2281 return per_objfile->get_symtab (per_cu);
2282 }
2283
2284 /* See read.h. */
2285
2286 dwarf2_per_cu_data_up
2287 dwarf2_per_bfd::allocate_per_cu ()
2288 {
2289 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2290 result->per_bfd = this;
2291 result->index = all_comp_units.size ();
2292 return result;
2293 }
2294
2295 /* See read.h. */
2296
2297 std::unique_ptr<signatured_type>
2298 dwarf2_per_bfd::allocate_signatured_type ()
2299 {
2300 std::unique_ptr<signatured_type> result (new signatured_type);
2301 result->per_bfd = this;
2302 result->index = all_comp_units.size ();
2303 result->is_debug_types = true;
2304 tu_stats.nr_tus++;
2305 return result;
2306 }
2307
2308 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2309 obstack, and constructed with the specified field values. */
2310
2311 static dwarf2_per_cu_data_up
2312 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2313 struct dwarf2_section_info *section,
2314 int is_dwz,
2315 sect_offset sect_off, ULONGEST length)
2316 {
2317 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2318 the_cu->sect_off = sect_off;
2319 the_cu->length = length;
2320 the_cu->section = section;
2321 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2322 struct dwarf2_per_cu_quick_data);
2323 the_cu->is_dwz = is_dwz;
2324 return the_cu;
2325 }
2326
2327 /* A helper for create_cus_from_index that handles a given list of
2328 CUs. */
2329
2330 static void
2331 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2332 const gdb_byte *cu_list, offset_type n_elements,
2333 struct dwarf2_section_info *section,
2334 int is_dwz)
2335 {
2336 for (offset_type i = 0; i < n_elements; i += 2)
2337 {
2338 gdb_static_assert (sizeof (ULONGEST) >= 8);
2339
2340 sect_offset sect_off
2341 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2342 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2343 cu_list += 2 * 8;
2344
2345 dwarf2_per_cu_data_up per_cu
2346 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2347 length);
2348 per_bfd->all_comp_units.push_back (std::move (per_cu));
2349 }
2350 }
2351
2352 /* Read the CU list from the mapped index, and use it to create all
2353 the CU objects for PER_BFD. */
2354
2355 static void
2356 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2357 const gdb_byte *cu_list, offset_type cu_list_elements,
2358 const gdb_byte *dwz_list, offset_type dwz_elements)
2359 {
2360 gdb_assert (per_bfd->all_comp_units.empty ());
2361 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2362
2363 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2364 &per_bfd->info, 0);
2365
2366 if (dwz_elements == 0)
2367 return;
2368
2369 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2370 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2371 &dwz->info, 1);
2372 }
2373
2374 /* Create the signatured type hash table from the index. */
2375
2376 static void
2377 create_signatured_type_table_from_index
2378 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2379 const gdb_byte *bytes, offset_type elements)
2380 {
2381 htab_up sig_types_hash = allocate_signatured_type_table ();
2382
2383 for (offset_type i = 0; i < elements; i += 3)
2384 {
2385 std::unique_ptr<signatured_type> sig_type;
2386 ULONGEST signature;
2387 void **slot;
2388 cu_offset type_offset_in_tu;
2389
2390 gdb_static_assert (sizeof (ULONGEST) >= 8);
2391 sect_offset sect_off
2392 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2393 type_offset_in_tu
2394 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2395 BFD_ENDIAN_LITTLE);
2396 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2397 bytes += 3 * 8;
2398
2399 sig_type = per_bfd->allocate_signatured_type ();
2400 sig_type->signature = signature;
2401 sig_type->type_offset_in_tu = type_offset_in_tu;
2402 sig_type->section = section;
2403 sig_type->sect_off = sect_off;
2404 sig_type->v.quick
2405 = OBSTACK_ZALLOC (&per_bfd->obstack,
2406 struct dwarf2_per_cu_quick_data);
2407
2408 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2409 *slot = sig_type.get ();
2410
2411 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2412 }
2413
2414 per_bfd->signatured_types = std::move (sig_types_hash);
2415 }
2416
2417 /* Create the signatured type hash table from .debug_names. */
2418
2419 static void
2420 create_signatured_type_table_from_debug_names
2421 (dwarf2_per_objfile *per_objfile,
2422 const mapped_debug_names &map,
2423 struct dwarf2_section_info *section,
2424 struct dwarf2_section_info *abbrev_section)
2425 {
2426 struct objfile *objfile = per_objfile->objfile;
2427
2428 section->read (objfile);
2429 abbrev_section->read (objfile);
2430
2431 htab_up sig_types_hash = allocate_signatured_type_table ();
2432
2433 for (uint32_t i = 0; i < map.tu_count; ++i)
2434 {
2435 std::unique_ptr<signatured_type> sig_type;
2436 void **slot;
2437
2438 sect_offset sect_off
2439 = (sect_offset) (extract_unsigned_integer
2440 (map.tu_table_reordered + i * map.offset_size,
2441 map.offset_size,
2442 map.dwarf5_byte_order));
2443
2444 comp_unit_head cu_header;
2445 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2446 abbrev_section,
2447 section->buffer + to_underlying (sect_off),
2448 rcuh_kind::TYPE);
2449
2450 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2451 sig_type->signature = cu_header.signature;
2452 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2453 sig_type->section = section;
2454 sig_type->sect_off = sect_off;
2455 sig_type->v.quick
2456 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2457 struct dwarf2_per_cu_quick_data);
2458
2459 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2460 *slot = sig_type.get ();
2461
2462 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2463 }
2464
2465 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2466 }
2467
2468 /* Read the address map data from the mapped index, and use it to
2469 populate the psymtabs_addrmap. */
2470
2471 static void
2472 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2473 struct mapped_index *index)
2474 {
2475 struct objfile *objfile = per_objfile->objfile;
2476 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2477 struct gdbarch *gdbarch = objfile->arch ();
2478 const gdb_byte *iter, *end;
2479 struct addrmap *mutable_map;
2480 CORE_ADDR baseaddr;
2481
2482 auto_obstack temp_obstack;
2483
2484 mutable_map = addrmap_create_mutable (&temp_obstack);
2485
2486 iter = index->address_table.data ();
2487 end = iter + index->address_table.size ();
2488
2489 baseaddr = objfile->text_section_offset ();
2490
2491 while (iter < end)
2492 {
2493 ULONGEST hi, lo, cu_index;
2494 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2495 iter += 8;
2496 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2497 iter += 8;
2498 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2499 iter += 4;
2500
2501 if (lo > hi)
2502 {
2503 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2504 hex_string (lo), hex_string (hi));
2505 continue;
2506 }
2507
2508 if (cu_index >= per_bfd->all_comp_units.size ())
2509 {
2510 complaint (_(".gdb_index address table has invalid CU number %u"),
2511 (unsigned) cu_index);
2512 continue;
2513 }
2514
2515 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2516 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2517 addrmap_set_empty (mutable_map, lo, hi - 1,
2518 per_bfd->get_cu (cu_index));
2519 }
2520
2521 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2522 &per_bfd->obstack);
2523 }
2524
2525 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2526 populate the psymtabs_addrmap. */
2527
2528 static void
2529 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2530 struct dwarf2_section_info *section)
2531 {
2532 struct objfile *objfile = per_objfile->objfile;
2533 bfd *abfd = objfile->obfd;
2534 struct gdbarch *gdbarch = objfile->arch ();
2535 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2536 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2537
2538 auto_obstack temp_obstack;
2539 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2540
2541 std::unordered_map<sect_offset,
2542 dwarf2_per_cu_data *,
2543 gdb::hash_enum<sect_offset>>
2544 debug_info_offset_to_per_cu;
2545 for (const auto &per_cu : per_bfd->all_comp_units)
2546 {
2547 const auto insertpair
2548 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2549 per_cu.get ());
2550 if (!insertpair.second)
2551 {
2552 warning (_("Section .debug_aranges in %s has duplicate "
2553 "debug_info_offset %s, ignoring .debug_aranges."),
2554 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2555 return;
2556 }
2557 }
2558
2559 section->read (objfile);
2560
2561 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2562
2563 const gdb_byte *addr = section->buffer;
2564
2565 while (addr < section->buffer + section->size)
2566 {
2567 const gdb_byte *const entry_addr = addr;
2568 unsigned int bytes_read;
2569
2570 const LONGEST entry_length = read_initial_length (abfd, addr,
2571 &bytes_read);
2572 addr += bytes_read;
2573
2574 const gdb_byte *const entry_end = addr + entry_length;
2575 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2576 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2577 if (addr + entry_length > section->buffer + section->size)
2578 {
2579 warning (_("Section .debug_aranges in %s entry at offset %s "
2580 "length %s exceeds section length %s, "
2581 "ignoring .debug_aranges."),
2582 objfile_name (objfile),
2583 plongest (entry_addr - section->buffer),
2584 plongest (bytes_read + entry_length),
2585 pulongest (section->size));
2586 return;
2587 }
2588
2589 /* The version number. */
2590 const uint16_t version = read_2_bytes (abfd, addr);
2591 addr += 2;
2592 if (version != 2)
2593 {
2594 warning (_("Section .debug_aranges in %s entry at offset %s "
2595 "has unsupported version %d, ignoring .debug_aranges."),
2596 objfile_name (objfile),
2597 plongest (entry_addr - section->buffer), version);
2598 return;
2599 }
2600
2601 const uint64_t debug_info_offset
2602 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2603 addr += offset_size;
2604 const auto per_cu_it
2605 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2606 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2607 {
2608 warning (_("Section .debug_aranges in %s entry at offset %s "
2609 "debug_info_offset %s does not exists, "
2610 "ignoring .debug_aranges."),
2611 objfile_name (objfile),
2612 plongest (entry_addr - section->buffer),
2613 pulongest (debug_info_offset));
2614 return;
2615 }
2616 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2617
2618 const uint8_t address_size = *addr++;
2619 if (address_size < 1 || address_size > 8)
2620 {
2621 warning (_("Section .debug_aranges in %s entry at offset %s "
2622 "address_size %u is invalid, ignoring .debug_aranges."),
2623 objfile_name (objfile),
2624 plongest (entry_addr - section->buffer), address_size);
2625 return;
2626 }
2627
2628 const uint8_t segment_selector_size = *addr++;
2629 if (segment_selector_size != 0)
2630 {
2631 warning (_("Section .debug_aranges in %s entry at offset %s "
2632 "segment_selector_size %u is not supported, "
2633 "ignoring .debug_aranges."),
2634 objfile_name (objfile),
2635 plongest (entry_addr - section->buffer),
2636 segment_selector_size);
2637 return;
2638 }
2639
2640 /* Must pad to an alignment boundary that is twice the address
2641 size. It is undocumented by the DWARF standard but GCC does
2642 use it. */
2643 for (size_t padding = ((-(addr - section->buffer))
2644 & (2 * address_size - 1));
2645 padding > 0; padding--)
2646 if (*addr++ != 0)
2647 {
2648 warning (_("Section .debug_aranges in %s entry at offset %s "
2649 "padding is not zero, ignoring .debug_aranges."),
2650 objfile_name (objfile),
2651 plongest (entry_addr - section->buffer));
2652 return;
2653 }
2654
2655 for (;;)
2656 {
2657 if (addr + 2 * address_size > entry_end)
2658 {
2659 warning (_("Section .debug_aranges in %s entry at offset %s "
2660 "address list is not properly terminated, "
2661 "ignoring .debug_aranges."),
2662 objfile_name (objfile),
2663 plongest (entry_addr - section->buffer));
2664 return;
2665 }
2666 ULONGEST start = extract_unsigned_integer (addr, address_size,
2667 dwarf5_byte_order);
2668 addr += address_size;
2669 ULONGEST length = extract_unsigned_integer (addr, address_size,
2670 dwarf5_byte_order);
2671 addr += address_size;
2672 if (start == 0 && length == 0)
2673 break;
2674 if (start == 0 && !per_bfd->has_section_at_zero)
2675 {
2676 /* Symbol was eliminated due to a COMDAT group. */
2677 continue;
2678 }
2679 ULONGEST end = start + length;
2680 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2681 - baseaddr);
2682 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2683 - baseaddr);
2684 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2685 }
2686 }
2687
2688 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2689 &per_bfd->obstack);
2690 }
2691
2692 /* A helper function that reads the .gdb_index from BUFFER and fills
2693 in MAP. FILENAME is the name of the file containing the data;
2694 it is used for error reporting. DEPRECATED_OK is true if it is
2695 ok to use deprecated sections.
2696
2697 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2698 out parameters that are filled in with information about the CU and
2699 TU lists in the section.
2700
2701 Returns true if all went well, false otherwise. */
2702
2703 static bool
2704 read_gdb_index_from_buffer (const char *filename,
2705 bool deprecated_ok,
2706 gdb::array_view<const gdb_byte> buffer,
2707 struct mapped_index *map,
2708 const gdb_byte **cu_list,
2709 offset_type *cu_list_elements,
2710 const gdb_byte **types_list,
2711 offset_type *types_list_elements)
2712 {
2713 const gdb_byte *addr = &buffer[0];
2714 offset_view metadata (buffer);
2715
2716 /* Version check. */
2717 offset_type version = metadata[0];
2718 /* Versions earlier than 3 emitted every copy of a psymbol. This
2719 causes the index to behave very poorly for certain requests. Version 3
2720 contained incomplete addrmap. So, it seems better to just ignore such
2721 indices. */
2722 if (version < 4)
2723 {
2724 static int warning_printed = 0;
2725 if (!warning_printed)
2726 {
2727 warning (_("Skipping obsolete .gdb_index section in %s."),
2728 filename);
2729 warning_printed = 1;
2730 }
2731 return 0;
2732 }
2733 /* Index version 4 uses a different hash function than index version
2734 5 and later.
2735
2736 Versions earlier than 6 did not emit psymbols for inlined
2737 functions. Using these files will cause GDB not to be able to
2738 set breakpoints on inlined functions by name, so we ignore these
2739 indices unless the user has done
2740 "set use-deprecated-index-sections on". */
2741 if (version < 6 && !deprecated_ok)
2742 {
2743 static int warning_printed = 0;
2744 if (!warning_printed)
2745 {
2746 warning (_("\
2747 Skipping deprecated .gdb_index section in %s.\n\
2748 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2749 to use the section anyway."),
2750 filename);
2751 warning_printed = 1;
2752 }
2753 return 0;
2754 }
2755 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2756 of the TU (for symbols coming from TUs),
2757 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2758 Plus gold-generated indices can have duplicate entries for global symbols,
2759 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2760 These are just performance bugs, and we can't distinguish gdb-generated
2761 indices from gold-generated ones, so issue no warning here. */
2762
2763 /* Indexes with higher version than the one supported by GDB may be no
2764 longer backward compatible. */
2765 if (version > 8)
2766 return 0;
2767
2768 map->version = version;
2769
2770 int i = 1;
2771 *cu_list = addr + metadata[i];
2772 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2773 ++i;
2774
2775 *types_list = addr + metadata[i];
2776 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2777 ++i;
2778
2779 const gdb_byte *address_table = addr + metadata[i];
2780 const gdb_byte *address_table_end = addr + metadata[i + 1];
2781 map->address_table
2782 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2783 ++i;
2784
2785 const gdb_byte *symbol_table = addr + metadata[i];
2786 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2787 map->symbol_table
2788 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2789 symbol_table_end));
2790
2791 ++i;
2792 map->constant_pool = buffer.slice (metadata[i]);
2793
2794 return 1;
2795 }
2796
2797 /* Callback types for dwarf2_read_gdb_index. */
2798
2799 typedef gdb::function_view
2800 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2801 get_gdb_index_contents_ftype;
2802 typedef gdb::function_view
2803 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2804 get_gdb_index_contents_dwz_ftype;
2805
2806 /* Read .gdb_index. If everything went ok, initialize the "quick"
2807 elements of all the CUs and return 1. Otherwise, return 0. */
2808
2809 static int
2810 dwarf2_read_gdb_index
2811 (dwarf2_per_objfile *per_objfile,
2812 get_gdb_index_contents_ftype get_gdb_index_contents,
2813 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2814 {
2815 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2816 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2817 struct dwz_file *dwz;
2818 struct objfile *objfile = per_objfile->objfile;
2819 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2820
2821 gdb::array_view<const gdb_byte> main_index_contents
2822 = get_gdb_index_contents (objfile, per_bfd);
2823
2824 if (main_index_contents.empty ())
2825 return 0;
2826
2827 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2828 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2829 use_deprecated_index_sections,
2830 main_index_contents, map.get (), &cu_list,
2831 &cu_list_elements, &types_list,
2832 &types_list_elements))
2833 return 0;
2834
2835 /* Don't use the index if it's empty. */
2836 if (map->symbol_table.empty ())
2837 return 0;
2838
2839 /* If there is a .dwz file, read it so we can get its CU list as
2840 well. */
2841 dwz = dwarf2_get_dwz_file (per_bfd);
2842 if (dwz != NULL)
2843 {
2844 struct mapped_index dwz_map;
2845 const gdb_byte *dwz_types_ignore;
2846 offset_type dwz_types_elements_ignore;
2847
2848 gdb::array_view<const gdb_byte> dwz_index_content
2849 = get_gdb_index_contents_dwz (objfile, dwz);
2850
2851 if (dwz_index_content.empty ())
2852 return 0;
2853
2854 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2855 1, dwz_index_content, &dwz_map,
2856 &dwz_list, &dwz_list_elements,
2857 &dwz_types_ignore,
2858 &dwz_types_elements_ignore))
2859 {
2860 warning (_("could not read '.gdb_index' section from %s; skipping"),
2861 bfd_get_filename (dwz->dwz_bfd.get ()));
2862 return 0;
2863 }
2864 }
2865
2866 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2867 dwz_list_elements);
2868
2869 if (types_list_elements)
2870 {
2871 /* We can only handle a single .debug_types when we have an
2872 index. */
2873 if (per_bfd->types.size () != 1)
2874 return 0;
2875
2876 dwarf2_section_info *section = &per_bfd->types[0];
2877
2878 create_signatured_type_table_from_index (per_bfd, section, types_list,
2879 types_list_elements);
2880 }
2881
2882 create_addrmap_from_index (per_objfile, map.get ());
2883
2884 per_bfd->index_table = std::move (map);
2885 per_bfd->using_index = 1;
2886 per_bfd->quick_file_names_table =
2887 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2888
2889 return 1;
2890 }
2891
2892 /* die_reader_func for dw2_get_file_names. */
2893
2894 static void
2895 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2896 struct die_info *comp_unit_die)
2897 {
2898 struct dwarf2_cu *cu = reader->cu;
2899 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2900 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2901 struct dwarf2_per_cu_data *lh_cu;
2902 struct attribute *attr;
2903 void **slot;
2904 struct quick_file_names *qfn;
2905
2906 gdb_assert (! this_cu->is_debug_types);
2907
2908 /* Our callers never want to match partial units -- instead they
2909 will match the enclosing full CU. */
2910 if (comp_unit_die->tag == DW_TAG_partial_unit)
2911 {
2912 this_cu->v.quick->no_file_data = 1;
2913 return;
2914 }
2915
2916 lh_cu = this_cu;
2917 slot = NULL;
2918
2919 line_header_up lh;
2920 sect_offset line_offset {};
2921
2922 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2923 if (attr != nullptr && attr->form_is_unsigned ())
2924 {
2925 struct quick_file_names find_entry;
2926
2927 line_offset = (sect_offset) attr->as_unsigned ();
2928
2929 /* We may have already read in this line header (TU line header sharing).
2930 If we have we're done. */
2931 find_entry.hash.dwo_unit = cu->dwo_unit;
2932 find_entry.hash.line_sect_off = line_offset;
2933 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2934 &find_entry, INSERT);
2935 if (*slot != NULL)
2936 {
2937 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
2938 return;
2939 }
2940
2941 lh = dwarf_decode_line_header (line_offset, cu);
2942 }
2943 if (lh == NULL)
2944 {
2945 lh_cu->v.quick->no_file_data = 1;
2946 return;
2947 }
2948
2949 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2950 qfn->hash.dwo_unit = cu->dwo_unit;
2951 qfn->hash.line_sect_off = line_offset;
2952 gdb_assert (slot != NULL);
2953 *slot = qfn;
2954
2955 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
2956
2957 int offset = 0;
2958 if (strcmp (fnd.name, "<unknown>") != 0)
2959 ++offset;
2960
2961 qfn->num_file_names = offset + lh->file_names_size ();
2962 qfn->file_names =
2963 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2964 qfn->num_file_names);
2965 if (offset != 0)
2966 qfn->file_names[0] = xstrdup (fnd.name);
2967 for (int i = 0; i < lh->file_names_size (); ++i)
2968 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
2969 fnd.comp_dir).release ();
2970 qfn->real_names = NULL;
2971
2972 lh_cu->v.quick->file_names = qfn;
2973 }
2974
2975 /* A helper for the "quick" functions which attempts to read the line
2976 table for THIS_CU. */
2977
2978 static struct quick_file_names *
2979 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2980 dwarf2_per_objfile *per_objfile)
2981 {
2982 /* This should never be called for TUs. */
2983 gdb_assert (! this_cu->is_debug_types);
2984 /* Nor type unit groups. */
2985 gdb_assert (! this_cu->type_unit_group_p ());
2986
2987 if (this_cu->v.quick->file_names != NULL)
2988 return this_cu->v.quick->file_names;
2989 /* If we know there is no line data, no point in looking again. */
2990 if (this_cu->v.quick->no_file_data)
2991 return NULL;
2992
2993 cutu_reader reader (this_cu, per_objfile);
2994 if (!reader.dummy_p)
2995 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2996
2997 if (this_cu->v.quick->no_file_data)
2998 return NULL;
2999 return this_cu->v.quick->file_names;
3000 }
3001
3002 /* A helper for the "quick" functions which computes and caches the
3003 real path for a given file name from the line table. */
3004
3005 static const char *
3006 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3007 struct quick_file_names *qfn, int index)
3008 {
3009 if (qfn->real_names == NULL)
3010 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3011 qfn->num_file_names, const char *);
3012
3013 if (qfn->real_names[index] == NULL)
3014 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3015
3016 return qfn->real_names[index];
3017 }
3018
3019 struct symtab *
3020 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3021 {
3022 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3023 dwarf2_per_cu_data *dwarf_cu
3024 = per_objfile->per_bfd->all_comp_units.back ().get ();
3025 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3026
3027 if (cust == NULL)
3028 return NULL;
3029
3030 return compunit_primary_filetab (cust);
3031 }
3032
3033 /* Traversal function for dw2_forget_cached_source_info. */
3034
3035 static int
3036 dw2_free_cached_file_names (void **slot, void *info)
3037 {
3038 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3039
3040 if (file_data->real_names)
3041 {
3042 int i;
3043
3044 for (i = 0; i < file_data->num_file_names; ++i)
3045 {
3046 xfree ((void*) file_data->real_names[i]);
3047 file_data->real_names[i] = NULL;
3048 }
3049 }
3050
3051 return 1;
3052 }
3053
3054 void
3055 dwarf2_base_index_functions::forget_cached_source_info
3056 (struct objfile *objfile)
3057 {
3058 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3059
3060 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3061 dw2_free_cached_file_names, NULL);
3062 }
3063
3064 /* Struct used to manage iterating over all CUs looking for a symbol. */
3065
3066 struct dw2_symtab_iterator
3067 {
3068 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3069 dwarf2_per_objfile *per_objfile;
3070 /* If set, only look for symbols that match that block. Valid values are
3071 GLOBAL_BLOCK and STATIC_BLOCK. */
3072 gdb::optional<block_enum> block_index;
3073 /* The kind of symbol we're looking for. */
3074 domain_enum domain;
3075 /* The list of CUs from the index entry of the symbol,
3076 or NULL if not found. */
3077 offset_view vec;
3078 /* The next element in VEC to look at. */
3079 int next;
3080 /* The number of elements in VEC, or zero if there is no match. */
3081 int length;
3082 /* Have we seen a global version of the symbol?
3083 If so we can ignore all further global instances.
3084 This is to work around gold/15646, inefficient gold-generated
3085 indices. */
3086 int global_seen;
3087 };
3088
3089 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3090
3091 static void
3092 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3093 dwarf2_per_objfile *per_objfile,
3094 gdb::optional<block_enum> block_index,
3095 domain_enum domain, offset_type namei)
3096 {
3097 iter->per_objfile = per_objfile;
3098 iter->block_index = block_index;
3099 iter->domain = domain;
3100 iter->next = 0;
3101 iter->global_seen = 0;
3102 iter->vec = {};
3103 iter->length = 0;
3104
3105 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3106 /* index is NULL if OBJF_READNOW. */
3107 if (index == NULL)
3108 return;
3109
3110 gdb_assert (!index->symbol_name_slot_invalid (namei));
3111 offset_type vec_idx = index->symbol_vec_index (namei);
3112
3113 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3114 iter->length = iter->vec[0];
3115 }
3116
3117 /* Return the next matching CU or NULL if there are no more. */
3118
3119 static struct dwarf2_per_cu_data *
3120 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3121 {
3122 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3123
3124 for ( ; iter->next < iter->length; ++iter->next)
3125 {
3126 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3127 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3128 gdb_index_symbol_kind symbol_kind =
3129 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3130 /* Only check the symbol attributes if they're present.
3131 Indices prior to version 7 don't record them,
3132 and indices >= 7 may elide them for certain symbols
3133 (gold does this). */
3134 int attrs_valid =
3135 (per_objfile->per_bfd->index_table->version >= 7
3136 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3137
3138 /* Don't crash on bad data. */
3139 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3140 {
3141 complaint (_(".gdb_index entry has bad CU index"
3142 " [in module %s]"), objfile_name (per_objfile->objfile));
3143 continue;
3144 }
3145
3146 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3147
3148 /* Skip if already read in. */
3149 if (per_objfile->symtab_set_p (per_cu))
3150 continue;
3151
3152 /* Check static vs global. */
3153 if (attrs_valid)
3154 {
3155 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3156
3157 if (iter->block_index.has_value ())
3158 {
3159 bool want_static = *iter->block_index == STATIC_BLOCK;
3160
3161 if (is_static != want_static)
3162 continue;
3163 }
3164
3165 /* Work around gold/15646. */
3166 if (!is_static
3167 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3168 {
3169 if (iter->global_seen)
3170 continue;
3171
3172 iter->global_seen = 1;
3173 }
3174 }
3175
3176 /* Only check the symbol's kind if it has one. */
3177 if (attrs_valid)
3178 {
3179 switch (iter->domain)
3180 {
3181 case VAR_DOMAIN:
3182 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3183 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3184 /* Some types are also in VAR_DOMAIN. */
3185 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3186 continue;
3187 break;
3188 case STRUCT_DOMAIN:
3189 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3190 continue;
3191 break;
3192 case LABEL_DOMAIN:
3193 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3194 continue;
3195 break;
3196 case MODULE_DOMAIN:
3197 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3198 continue;
3199 break;
3200 default:
3201 break;
3202 }
3203 }
3204
3205 ++iter->next;
3206 return per_cu;
3207 }
3208
3209 return NULL;
3210 }
3211
3212 void
3213 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3214 bool print_bcache)
3215 {
3216 if (print_bcache)
3217 return;
3218
3219 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3220 int total = per_objfile->per_bfd->all_comp_units.size ();
3221 int count = 0;
3222
3223 for (int i = 0; i < total; ++i)
3224 {
3225 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3226
3227 if (!per_objfile->symtab_set_p (per_cu))
3228 ++count;
3229 }
3230 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3231 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3232 }
3233
3234 /* This dumps minimal information about the index.
3235 It is called via "mt print objfiles".
3236 One use is to verify .gdb_index has been loaded by the
3237 gdb.dwarf2/gdb-index.exp testcase. */
3238
3239 void
3240 dwarf2_gdb_index::dump (struct objfile *objfile)
3241 {
3242 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3243
3244 gdb_assert (per_objfile->per_bfd->using_index);
3245 printf_filtered (".gdb_index:");
3246 if (per_objfile->per_bfd->index_table != NULL)
3247 {
3248 printf_filtered (" version %d\n",
3249 per_objfile->per_bfd->index_table->version);
3250 }
3251 else
3252 printf_filtered (" faked for \"readnow\"\n");
3253 printf_filtered ("\n");
3254 }
3255
3256 void
3257 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3258 {
3259 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3260 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3261
3262 for (int i = 0; i < total_units; ++i)
3263 {
3264 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3265
3266 /* We don't want to directly expand a partial CU, because if we
3267 read it with the wrong language, then assertion failures can
3268 be triggered later on. See PR symtab/23010. So, tell
3269 dw2_instantiate_symtab to skip partial CUs -- any important
3270 partial CU will be read via DW_TAG_imported_unit anyway. */
3271 dw2_instantiate_symtab (per_cu, per_objfile, true);
3272 }
3273 }
3274
3275 static bool
3276 dw2_expand_symtabs_matching_symbol
3277 (mapped_index_base &index,
3278 const lookup_name_info &lookup_name_in,
3279 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3280 gdb::function_view<bool (offset_type)> match_callback,
3281 dwarf2_per_objfile *per_objfile);
3282
3283 static bool
3284 dw2_expand_symtabs_matching_one
3285 (dwarf2_per_cu_data *per_cu,
3286 dwarf2_per_objfile *per_objfile,
3287 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3288 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3289
3290 void
3291 dwarf2_gdb_index::expand_matching_symbols
3292 (struct objfile *objfile,
3293 const lookup_name_info &name, domain_enum domain,
3294 int global,
3295 symbol_compare_ftype *ordered_compare)
3296 {
3297 /* Used for Ada. */
3298 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3299
3300 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3301
3302 if (per_objfile->per_bfd->index_table != nullptr)
3303 {
3304 mapped_index &index = *per_objfile->per_bfd->index_table;
3305
3306 const char *match_name = name.ada ().lookup_name ().c_str ();
3307 auto matcher = [&] (const char *symname)
3308 {
3309 if (ordered_compare == nullptr)
3310 return true;
3311 return ordered_compare (symname, match_name) == 0;
3312 };
3313
3314 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3315 [&] (offset_type namei)
3316 {
3317 struct dw2_symtab_iterator iter;
3318 struct dwarf2_per_cu_data *per_cu;
3319
3320 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3321 namei);
3322 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3323 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3324 nullptr);
3325 return true;
3326 }, per_objfile);
3327 }
3328 else
3329 {
3330 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3331 proceed assuming all symtabs have been read in. */
3332 }
3333 }
3334
3335 /* Starting from a search name, return the string that finds the upper
3336 bound of all strings that start with SEARCH_NAME in a sorted name
3337 list. Returns the empty string to indicate that the upper bound is
3338 the end of the list. */
3339
3340 static std::string
3341 make_sort_after_prefix_name (const char *search_name)
3342 {
3343 /* When looking to complete "func", we find the upper bound of all
3344 symbols that start with "func" by looking for where we'd insert
3345 the closest string that would follow "func" in lexicographical
3346 order. Usually, that's "func"-with-last-character-incremented,
3347 i.e. "fund". Mind non-ASCII characters, though. Usually those
3348 will be UTF-8 multi-byte sequences, but we can't be certain.
3349 Especially mind the 0xff character, which is a valid character in
3350 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3351 rule out compilers allowing it in identifiers. Note that
3352 conveniently, strcmp/strcasecmp are specified to compare
3353 characters interpreted as unsigned char. So what we do is treat
3354 the whole string as a base 256 number composed of a sequence of
3355 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3356 to 0, and carries 1 to the following more-significant position.
3357 If the very first character in SEARCH_NAME ends up incremented
3358 and carries/overflows, then the upper bound is the end of the
3359 list. The string after the empty string is also the empty
3360 string.
3361
3362 Some examples of this operation:
3363
3364 SEARCH_NAME => "+1" RESULT
3365
3366 "abc" => "abd"
3367 "ab\xff" => "ac"
3368 "\xff" "a" "\xff" => "\xff" "b"
3369 "\xff" => ""
3370 "\xff\xff" => ""
3371 "" => ""
3372
3373 Then, with these symbols for example:
3374
3375 func
3376 func1
3377 fund
3378
3379 completing "func" looks for symbols between "func" and
3380 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3381 which finds "func" and "func1", but not "fund".
3382
3383 And with:
3384
3385 funcÿ (Latin1 'ÿ' [0xff])
3386 funcÿ1
3387 fund
3388
3389 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3390 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3391
3392 And with:
3393
3394 ÿÿ (Latin1 'ÿ' [0xff])
3395 ÿÿ1
3396
3397 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3398 the end of the list.
3399 */
3400 std::string after = search_name;
3401 while (!after.empty () && (unsigned char) after.back () == 0xff)
3402 after.pop_back ();
3403 if (!after.empty ())
3404 after.back () = (unsigned char) after.back () + 1;
3405 return after;
3406 }
3407
3408 /* See declaration. */
3409
3410 std::pair<std::vector<name_component>::const_iterator,
3411 std::vector<name_component>::const_iterator>
3412 mapped_index_base::find_name_components_bounds
3413 (const lookup_name_info &lookup_name_without_params, language lang,
3414 dwarf2_per_objfile *per_objfile) const
3415 {
3416 auto *name_cmp
3417 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3418
3419 const char *lang_name
3420 = lookup_name_without_params.language_lookup_name (lang);
3421
3422 /* Comparison function object for lower_bound that matches against a
3423 given symbol name. */
3424 auto lookup_compare_lower = [&] (const name_component &elem,
3425 const char *name)
3426 {
3427 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3428 const char *elem_name = elem_qualified + elem.name_offset;
3429 return name_cmp (elem_name, name) < 0;
3430 };
3431
3432 /* Comparison function object for upper_bound that matches against a
3433 given symbol name. */
3434 auto lookup_compare_upper = [&] (const char *name,
3435 const name_component &elem)
3436 {
3437 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3438 const char *elem_name = elem_qualified + elem.name_offset;
3439 return name_cmp (name, elem_name) < 0;
3440 };
3441
3442 auto begin = this->name_components.begin ();
3443 auto end = this->name_components.end ();
3444
3445 /* Find the lower bound. */
3446 auto lower = [&] ()
3447 {
3448 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3449 return begin;
3450 else
3451 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3452 } ();
3453
3454 /* Find the upper bound. */
3455 auto upper = [&] ()
3456 {
3457 if (lookup_name_without_params.completion_mode ())
3458 {
3459 /* In completion mode, we want UPPER to point past all
3460 symbols names that have the same prefix. I.e., with
3461 these symbols, and completing "func":
3462
3463 function << lower bound
3464 function1
3465 other_function << upper bound
3466
3467 We find the upper bound by looking for the insertion
3468 point of "func"-with-last-character-incremented,
3469 i.e. "fund". */
3470 std::string after = make_sort_after_prefix_name (lang_name);
3471 if (after.empty ())
3472 return end;
3473 return std::lower_bound (lower, end, after.c_str (),
3474 lookup_compare_lower);
3475 }
3476 else
3477 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3478 } ();
3479
3480 return {lower, upper};
3481 }
3482
3483 /* See declaration. */
3484
3485 void
3486 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3487 {
3488 if (!this->name_components.empty ())
3489 return;
3490
3491 this->name_components_casing = case_sensitivity;
3492 auto *name_cmp
3493 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3494
3495 /* The code below only knows how to break apart components of C++
3496 symbol names (and other languages that use '::' as
3497 namespace/module separator) and Ada symbol names. */
3498 auto count = this->symbol_name_count ();
3499 for (offset_type idx = 0; idx < count; idx++)
3500 {
3501 if (this->symbol_name_slot_invalid (idx))
3502 continue;
3503
3504 const char *name = this->symbol_name_at (idx, per_objfile);
3505
3506 /* Add each name component to the name component table. */
3507 unsigned int previous_len = 0;
3508
3509 if (strstr (name, "::") != nullptr)
3510 {
3511 for (unsigned int current_len = cp_find_first_component (name);
3512 name[current_len] != '\0';
3513 current_len += cp_find_first_component (name + current_len))
3514 {
3515 gdb_assert (name[current_len] == ':');
3516 this->name_components.push_back ({previous_len, idx});
3517 /* Skip the '::'. */
3518 current_len += 2;
3519 previous_len = current_len;
3520 }
3521 }
3522 else
3523 {
3524 /* Handle the Ada encoded (aka mangled) form here. */
3525 for (const char *iter = strstr (name, "__");
3526 iter != nullptr;
3527 iter = strstr (iter, "__"))
3528 {
3529 this->name_components.push_back ({previous_len, idx});
3530 iter += 2;
3531 previous_len = iter - name;
3532 }
3533 }
3534
3535 this->name_components.push_back ({previous_len, idx});
3536 }
3537
3538 /* Sort name_components elements by name. */
3539 auto name_comp_compare = [&] (const name_component &left,
3540 const name_component &right)
3541 {
3542 const char *left_qualified
3543 = this->symbol_name_at (left.idx, per_objfile);
3544 const char *right_qualified
3545 = this->symbol_name_at (right.idx, per_objfile);
3546
3547 const char *left_name = left_qualified + left.name_offset;
3548 const char *right_name = right_qualified + right.name_offset;
3549
3550 return name_cmp (left_name, right_name) < 0;
3551 };
3552
3553 std::sort (this->name_components.begin (),
3554 this->name_components.end (),
3555 name_comp_compare);
3556 }
3557
3558 /* Helper for dw2_expand_symtabs_matching that works with a
3559 mapped_index_base instead of the containing objfile. This is split
3560 to a separate function in order to be able to unit test the
3561 name_components matching using a mock mapped_index_base. For each
3562 symbol name that matches, calls MATCH_CALLBACK, passing it the
3563 symbol's index in the mapped_index_base symbol table. */
3564
3565 static bool
3566 dw2_expand_symtabs_matching_symbol
3567 (mapped_index_base &index,
3568 const lookup_name_info &lookup_name_in,
3569 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3570 gdb::function_view<bool (offset_type)> match_callback,
3571 dwarf2_per_objfile *per_objfile)
3572 {
3573 lookup_name_info lookup_name_without_params
3574 = lookup_name_in.make_ignore_params ();
3575
3576 /* Build the symbol name component sorted vector, if we haven't
3577 yet. */
3578 index.build_name_components (per_objfile);
3579
3580 /* The same symbol may appear more than once in the range though.
3581 E.g., if we're looking for symbols that complete "w", and we have
3582 a symbol named "w1::w2", we'll find the two name components for
3583 that same symbol in the range. To be sure we only call the
3584 callback once per symbol, we first collect the symbol name
3585 indexes that matched in a temporary vector and ignore
3586 duplicates. */
3587 std::vector<offset_type> matches;
3588
3589 struct name_and_matcher
3590 {
3591 symbol_name_matcher_ftype *matcher;
3592 const char *name;
3593
3594 bool operator== (const name_and_matcher &other) const
3595 {
3596 return matcher == other.matcher && strcmp (name, other.name) == 0;
3597 }
3598 };
3599
3600 /* A vector holding all the different symbol name matchers, for all
3601 languages. */
3602 std::vector<name_and_matcher> matchers;
3603
3604 for (int i = 0; i < nr_languages; i++)
3605 {
3606 enum language lang_e = (enum language) i;
3607
3608 const language_defn *lang = language_def (lang_e);
3609 symbol_name_matcher_ftype *name_matcher
3610 = lang->get_symbol_name_matcher (lookup_name_without_params);
3611
3612 name_and_matcher key {
3613 name_matcher,
3614 lookup_name_without_params.language_lookup_name (lang_e)
3615 };
3616
3617 /* Don't insert the same comparison routine more than once.
3618 Note that we do this linear walk. This is not a problem in
3619 practice because the number of supported languages is
3620 low. */
3621 if (std::find (matchers.begin (), matchers.end (), key)
3622 != matchers.end ())
3623 continue;
3624 matchers.push_back (std::move (key));
3625
3626 auto bounds
3627 = index.find_name_components_bounds (lookup_name_without_params,
3628 lang_e, per_objfile);
3629
3630 /* Now for each symbol name in range, check to see if we have a name
3631 match, and if so, call the MATCH_CALLBACK callback. */
3632
3633 for (; bounds.first != bounds.second; ++bounds.first)
3634 {
3635 const char *qualified
3636 = index.symbol_name_at (bounds.first->idx, per_objfile);
3637
3638 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3639 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3640 continue;
3641
3642 matches.push_back (bounds.first->idx);
3643 }
3644 }
3645
3646 std::sort (matches.begin (), matches.end ());
3647
3648 /* Finally call the callback, once per match. */
3649 ULONGEST prev = -1;
3650 bool result = true;
3651 for (offset_type idx : matches)
3652 {
3653 if (prev != idx)
3654 {
3655 if (!match_callback (idx))
3656 {
3657 result = false;
3658 break;
3659 }
3660 prev = idx;
3661 }
3662 }
3663
3664 /* Above we use a type wider than idx's for 'prev', since 0 and
3665 (offset_type)-1 are both possible values. */
3666 static_assert (sizeof (prev) > sizeof (offset_type), "");
3667
3668 return result;
3669 }
3670
3671 #if GDB_SELF_TEST
3672
3673 namespace selftests { namespace dw2_expand_symtabs_matching {
3674
3675 /* A mock .gdb_index/.debug_names-like name index table, enough to
3676 exercise dw2_expand_symtabs_matching_symbol, which works with the
3677 mapped_index_base interface. Builds an index from the symbol list
3678 passed as parameter to the constructor. */
3679 class mock_mapped_index : public mapped_index_base
3680 {
3681 public:
3682 mock_mapped_index (gdb::array_view<const char *> symbols)
3683 : m_symbol_table (symbols)
3684 {}
3685
3686 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3687
3688 /* Return the number of names in the symbol table. */
3689 size_t symbol_name_count () const override
3690 {
3691 return m_symbol_table.size ();
3692 }
3693
3694 /* Get the name of the symbol at IDX in the symbol table. */
3695 const char *symbol_name_at
3696 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3697 {
3698 return m_symbol_table[idx];
3699 }
3700
3701 private:
3702 gdb::array_view<const char *> m_symbol_table;
3703 };
3704
3705 /* Convenience function that converts a NULL pointer to a "<null>"
3706 string, to pass to print routines. */
3707
3708 static const char *
3709 string_or_null (const char *str)
3710 {
3711 return str != NULL ? str : "<null>";
3712 }
3713
3714 /* Check if a lookup_name_info built from
3715 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3716 index. EXPECTED_LIST is the list of expected matches, in expected
3717 matching order. If no match expected, then an empty list is
3718 specified. Returns true on success. On failure prints a warning
3719 indicating the file:line that failed, and returns false. */
3720
3721 static bool
3722 check_match (const char *file, int line,
3723 mock_mapped_index &mock_index,
3724 const char *name, symbol_name_match_type match_type,
3725 bool completion_mode,
3726 std::initializer_list<const char *> expected_list,
3727 dwarf2_per_objfile *per_objfile)
3728 {
3729 lookup_name_info lookup_name (name, match_type, completion_mode);
3730
3731 bool matched = true;
3732
3733 auto mismatch = [&] (const char *expected_str,
3734 const char *got)
3735 {
3736 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3737 "expected=\"%s\", got=\"%s\"\n"),
3738 file, line,
3739 (match_type == symbol_name_match_type::FULL
3740 ? "FULL" : "WILD"),
3741 name, string_or_null (expected_str), string_or_null (got));
3742 matched = false;
3743 };
3744
3745 auto expected_it = expected_list.begin ();
3746 auto expected_end = expected_list.end ();
3747
3748 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3749 nullptr,
3750 [&] (offset_type idx)
3751 {
3752 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3753 const char *expected_str
3754 = expected_it == expected_end ? NULL : *expected_it++;
3755
3756 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3757 mismatch (expected_str, matched_name);
3758 return true;
3759 }, per_objfile);
3760
3761 const char *expected_str
3762 = expected_it == expected_end ? NULL : *expected_it++;
3763 if (expected_str != NULL)
3764 mismatch (expected_str, NULL);
3765
3766 return matched;
3767 }
3768
3769 /* The symbols added to the mock mapped_index for testing (in
3770 canonical form). */
3771 static const char *test_symbols[] = {
3772 "function",
3773 "std::bar",
3774 "std::zfunction",
3775 "std::zfunction2",
3776 "w1::w2",
3777 "ns::foo<char*>",
3778 "ns::foo<int>",
3779 "ns::foo<long>",
3780 "ns2::tmpl<int>::foo2",
3781 "(anonymous namespace)::A::B::C",
3782
3783 /* These are used to check that the increment-last-char in the
3784 matching algorithm for completion doesn't match "t1_fund" when
3785 completing "t1_func". */
3786 "t1_func",
3787 "t1_func1",
3788 "t1_fund",
3789 "t1_fund1",
3790
3791 /* A UTF-8 name with multi-byte sequences to make sure that
3792 cp-name-parser understands this as a single identifier ("função"
3793 is "function" in PT). */
3794 u8"u8função",
3795
3796 /* \377 (0xff) is Latin1 'ÿ'. */
3797 "yfunc\377",
3798
3799 /* \377 (0xff) is Latin1 'ÿ'. */
3800 "\377",
3801 "\377\377123",
3802
3803 /* A name with all sorts of complications. Starts with "z" to make
3804 it easier for the completion tests below. */
3805 #define Z_SYM_NAME \
3806 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3807 "::tuple<(anonymous namespace)::ui*, " \
3808 "std::default_delete<(anonymous namespace)::ui>, void>"
3809
3810 Z_SYM_NAME
3811 };
3812
3813 /* Returns true if the mapped_index_base::find_name_component_bounds
3814 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3815 in completion mode. */
3816
3817 static bool
3818 check_find_bounds_finds (mapped_index_base &index,
3819 const char *search_name,
3820 gdb::array_view<const char *> expected_syms,
3821 dwarf2_per_objfile *per_objfile)
3822 {
3823 lookup_name_info lookup_name (search_name,
3824 symbol_name_match_type::FULL, true);
3825
3826 auto bounds = index.find_name_components_bounds (lookup_name,
3827 language_cplus,
3828 per_objfile);
3829
3830 size_t distance = std::distance (bounds.first, bounds.second);
3831 if (distance != expected_syms.size ())
3832 return false;
3833
3834 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3835 {
3836 auto nc_elem = bounds.first + exp_elem;
3837 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3838 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3839 return false;
3840 }
3841
3842 return true;
3843 }
3844
3845 /* Test the lower-level mapped_index::find_name_component_bounds
3846 method. */
3847
3848 static void
3849 test_mapped_index_find_name_component_bounds ()
3850 {
3851 mock_mapped_index mock_index (test_symbols);
3852
3853 mock_index.build_name_components (NULL /* per_objfile */);
3854
3855 /* Test the lower-level mapped_index::find_name_component_bounds
3856 method in completion mode. */
3857 {
3858 static const char *expected_syms[] = {
3859 "t1_func",
3860 "t1_func1",
3861 };
3862
3863 SELF_CHECK (check_find_bounds_finds
3864 (mock_index, "t1_func", expected_syms,
3865 NULL /* per_objfile */));
3866 }
3867
3868 /* Check that the increment-last-char in the name matching algorithm
3869 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3870 {
3871 static const char *expected_syms1[] = {
3872 "\377",
3873 "\377\377123",
3874 };
3875 SELF_CHECK (check_find_bounds_finds
3876 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3877
3878 static const char *expected_syms2[] = {
3879 "\377\377123",
3880 };
3881 SELF_CHECK (check_find_bounds_finds
3882 (mock_index, "\377\377", expected_syms2,
3883 NULL /* per_objfile */));
3884 }
3885 }
3886
3887 /* Test dw2_expand_symtabs_matching_symbol. */
3888
3889 static void
3890 test_dw2_expand_symtabs_matching_symbol ()
3891 {
3892 mock_mapped_index mock_index (test_symbols);
3893
3894 /* We let all tests run until the end even if some fails, for debug
3895 convenience. */
3896 bool any_mismatch = false;
3897
3898 /* Create the expected symbols list (an initializer_list). Needed
3899 because lists have commas, and we need to pass them to CHECK,
3900 which is a macro. */
3901 #define EXPECT(...) { __VA_ARGS__ }
3902
3903 /* Wrapper for check_match that passes down the current
3904 __FILE__/__LINE__. */
3905 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3906 any_mismatch |= !check_match (__FILE__, __LINE__, \
3907 mock_index, \
3908 NAME, MATCH_TYPE, COMPLETION_MODE, \
3909 EXPECTED_LIST, NULL)
3910
3911 /* Identity checks. */
3912 for (const char *sym : test_symbols)
3913 {
3914 /* Should be able to match all existing symbols. */
3915 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3916 EXPECT (sym));
3917
3918 /* Should be able to match all existing symbols with
3919 parameters. */
3920 std::string with_params = std::string (sym) + "(int)";
3921 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3922 EXPECT (sym));
3923
3924 /* Should be able to match all existing symbols with
3925 parameters and qualifiers. */
3926 with_params = std::string (sym) + " ( int ) const";
3927 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3928 EXPECT (sym));
3929
3930 /* This should really find sym, but cp-name-parser.y doesn't
3931 know about lvalue/rvalue qualifiers yet. */
3932 with_params = std::string (sym) + " ( int ) &&";
3933 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3934 {});
3935 }
3936
3937 /* Check that the name matching algorithm for completion doesn't get
3938 confused with Latin1 'ÿ' / 0xff. */
3939 {
3940 static const char str[] = "\377";
3941 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3942 EXPECT ("\377", "\377\377123"));
3943 }
3944
3945 /* Check that the increment-last-char in the matching algorithm for
3946 completion doesn't match "t1_fund" when completing "t1_func". */
3947 {
3948 static const char str[] = "t1_func";
3949 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3950 EXPECT ("t1_func", "t1_func1"));
3951 }
3952
3953 /* Check that completion mode works at each prefix of the expected
3954 symbol name. */
3955 {
3956 static const char str[] = "function(int)";
3957 size_t len = strlen (str);
3958 std::string lookup;
3959
3960 for (size_t i = 1; i < len; i++)
3961 {
3962 lookup.assign (str, i);
3963 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3964 EXPECT ("function"));
3965 }
3966 }
3967
3968 /* While "w" is a prefix of both components, the match function
3969 should still only be called once. */
3970 {
3971 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3972 EXPECT ("w1::w2"));
3973 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3974 EXPECT ("w1::w2"));
3975 }
3976
3977 /* Same, with a "complicated" symbol. */
3978 {
3979 static const char str[] = Z_SYM_NAME;
3980 size_t len = strlen (str);
3981 std::string lookup;
3982
3983 for (size_t i = 1; i < len; i++)
3984 {
3985 lookup.assign (str, i);
3986 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3987 EXPECT (Z_SYM_NAME));
3988 }
3989 }
3990
3991 /* In FULL mode, an incomplete symbol doesn't match. */
3992 {
3993 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3994 {});
3995 }
3996
3997 /* A complete symbol with parameters matches any overload, since the
3998 index has no overload info. */
3999 {
4000 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4001 EXPECT ("std::zfunction", "std::zfunction2"));
4002 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4003 EXPECT ("std::zfunction", "std::zfunction2"));
4004 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4005 EXPECT ("std::zfunction", "std::zfunction2"));
4006 }
4007
4008 /* Check that whitespace is ignored appropriately. A symbol with a
4009 template argument list. */
4010 {
4011 static const char expected[] = "ns::foo<int>";
4012 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4013 EXPECT (expected));
4014 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4015 EXPECT (expected));
4016 }
4017
4018 /* Check that whitespace is ignored appropriately. A symbol with a
4019 template argument list that includes a pointer. */
4020 {
4021 static const char expected[] = "ns::foo<char*>";
4022 /* Try both completion and non-completion modes. */
4023 static const bool completion_mode[2] = {false, true};
4024 for (size_t i = 0; i < 2; i++)
4025 {
4026 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4027 completion_mode[i], EXPECT (expected));
4028 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4029 completion_mode[i], EXPECT (expected));
4030
4031 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4032 completion_mode[i], EXPECT (expected));
4033 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4034 completion_mode[i], EXPECT (expected));
4035 }
4036 }
4037
4038 {
4039 /* Check method qualifiers are ignored. */
4040 static const char expected[] = "ns::foo<char*>";
4041 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4042 symbol_name_match_type::FULL, true, EXPECT (expected));
4043 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4044 symbol_name_match_type::FULL, true, EXPECT (expected));
4045 CHECK_MATCH ("foo < char * > ( int ) const",
4046 symbol_name_match_type::WILD, true, EXPECT (expected));
4047 CHECK_MATCH ("foo < char * > ( int ) &&",
4048 symbol_name_match_type::WILD, true, EXPECT (expected));
4049 }
4050
4051 /* Test lookup names that don't match anything. */
4052 {
4053 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4054 {});
4055
4056 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4057 {});
4058 }
4059
4060 /* Some wild matching tests, exercising "(anonymous namespace)",
4061 which should not be confused with a parameter list. */
4062 {
4063 static const char *syms[] = {
4064 "A::B::C",
4065 "B::C",
4066 "C",
4067 "A :: B :: C ( int )",
4068 "B :: C ( int )",
4069 "C ( int )",
4070 };
4071
4072 for (const char *s : syms)
4073 {
4074 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4075 EXPECT ("(anonymous namespace)::A::B::C"));
4076 }
4077 }
4078
4079 {
4080 static const char expected[] = "ns2::tmpl<int>::foo2";
4081 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4082 EXPECT (expected));
4083 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4084 EXPECT (expected));
4085 }
4086
4087 SELF_CHECK (!any_mismatch);
4088
4089 #undef EXPECT
4090 #undef CHECK_MATCH
4091 }
4092
4093 static void
4094 run_test ()
4095 {
4096 test_mapped_index_find_name_component_bounds ();
4097 test_dw2_expand_symtabs_matching_symbol ();
4098 }
4099
4100 }} // namespace selftests::dw2_expand_symtabs_matching
4101
4102 #endif /* GDB_SELF_TEST */
4103
4104 /* If FILE_MATCHER is NULL or if PER_CU has
4105 dwarf2_per_cu_quick_data::MARK set (see
4106 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4107 EXPANSION_NOTIFY on it. */
4108
4109 static bool
4110 dw2_expand_symtabs_matching_one
4111 (dwarf2_per_cu_data *per_cu,
4112 dwarf2_per_objfile *per_objfile,
4113 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4114 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4115 {
4116 if (file_matcher == NULL || per_cu->v.quick->mark)
4117 {
4118 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4119
4120 compunit_symtab *symtab
4121 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4122 gdb_assert (symtab != nullptr);
4123
4124 if (expansion_notify != NULL && symtab_was_null)
4125 return expansion_notify (symtab);
4126 }
4127 return true;
4128 }
4129
4130 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4131 matched, to expand corresponding CUs that were marked. IDX is the
4132 index of the symbol name that matched. */
4133
4134 static bool
4135 dw2_expand_marked_cus
4136 (dwarf2_per_objfile *per_objfile, offset_type idx,
4137 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4138 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4139 block_search_flags search_flags,
4140 search_domain kind)
4141 {
4142 offset_type vec_len, vec_idx;
4143 bool global_seen = false;
4144 mapped_index &index = *per_objfile->per_bfd->index_table;
4145
4146 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4147 vec_len = vec[0];
4148 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4149 {
4150 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4151 /* This value is only valid for index versions >= 7. */
4152 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4153 gdb_index_symbol_kind symbol_kind =
4154 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4155 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4156 /* Only check the symbol attributes if they're present.
4157 Indices prior to version 7 don't record them,
4158 and indices >= 7 may elide them for certain symbols
4159 (gold does this). */
4160 int attrs_valid =
4161 (index.version >= 7
4162 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4163
4164 /* Work around gold/15646. */
4165 if (attrs_valid
4166 && !is_static
4167 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4168 {
4169 if (global_seen)
4170 continue;
4171
4172 global_seen = true;
4173 }
4174
4175 /* Only check the symbol's kind if it has one. */
4176 if (attrs_valid)
4177 {
4178 if (is_static)
4179 {
4180 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4181 continue;
4182 }
4183 else
4184 {
4185 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4186 continue;
4187 }
4188
4189 switch (kind)
4190 {
4191 case VARIABLES_DOMAIN:
4192 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4193 continue;
4194 break;
4195 case FUNCTIONS_DOMAIN:
4196 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4197 continue;
4198 break;
4199 case TYPES_DOMAIN:
4200 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4201 continue;
4202 break;
4203 case MODULES_DOMAIN:
4204 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4205 continue;
4206 break;
4207 default:
4208 break;
4209 }
4210 }
4211
4212 /* Don't crash on bad data. */
4213 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4214 {
4215 complaint (_(".gdb_index entry has bad CU index"
4216 " [in module %s]"), objfile_name (per_objfile->objfile));
4217 continue;
4218 }
4219
4220 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4221 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4222 expansion_notify))
4223 return false;
4224 }
4225
4226 return true;
4227 }
4228
4229 /* If FILE_MATCHER is non-NULL, set all the
4230 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4231 that match FILE_MATCHER. */
4232
4233 static void
4234 dw_expand_symtabs_matching_file_matcher
4235 (dwarf2_per_objfile *per_objfile,
4236 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4237 {
4238 if (file_matcher == NULL)
4239 return;
4240
4241 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4242 htab_eq_pointer,
4243 NULL, xcalloc, xfree));
4244 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4245 htab_eq_pointer,
4246 NULL, xcalloc, xfree));
4247
4248 /* The rule is CUs specify all the files, including those used by
4249 any TU, so there's no need to scan TUs here. */
4250
4251 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4252 {
4253 QUIT;
4254
4255 if (per_cu->is_debug_types)
4256 continue;
4257 per_cu->v.quick->mark = 0;
4258
4259 /* We only need to look at symtabs not already expanded. */
4260 if (per_objfile->symtab_set_p (per_cu.get ()))
4261 continue;
4262
4263 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4264 per_objfile);
4265 if (file_data == NULL)
4266 continue;
4267
4268 if (htab_find (visited_not_found.get (), file_data) != NULL)
4269 continue;
4270 else if (htab_find (visited_found.get (), file_data) != NULL)
4271 {
4272 per_cu->v.quick->mark = 1;
4273 continue;
4274 }
4275
4276 for (int j = 0; j < file_data->num_file_names; ++j)
4277 {
4278 const char *this_real_name;
4279
4280 if (file_matcher (file_data->file_names[j], false))
4281 {
4282 per_cu->v.quick->mark = 1;
4283 break;
4284 }
4285
4286 /* Before we invoke realpath, which can get expensive when many
4287 files are involved, do a quick comparison of the basenames. */
4288 if (!basenames_may_differ
4289 && !file_matcher (lbasename (file_data->file_names[j]),
4290 true))
4291 continue;
4292
4293 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4294 if (file_matcher (this_real_name, false))
4295 {
4296 per_cu->v.quick->mark = 1;
4297 break;
4298 }
4299 }
4300
4301 void **slot = htab_find_slot (per_cu->v.quick->mark
4302 ? visited_found.get ()
4303 : visited_not_found.get (),
4304 file_data, INSERT);
4305 *slot = file_data;
4306 }
4307 }
4308
4309 bool
4310 dwarf2_gdb_index::expand_symtabs_matching
4311 (struct objfile *objfile,
4312 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4313 const lookup_name_info *lookup_name,
4314 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4315 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4316 block_search_flags search_flags,
4317 domain_enum domain,
4318 enum search_domain kind)
4319 {
4320 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4321
4322 /* index_table is NULL if OBJF_READNOW. */
4323 if (!per_objfile->per_bfd->index_table)
4324 return true;
4325
4326 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4327
4328 if (symbol_matcher == NULL && lookup_name == NULL)
4329 {
4330 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4331 {
4332 QUIT;
4333
4334 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
4335 file_matcher,
4336 expansion_notify))
4337 return false;
4338 }
4339 return true;
4340 }
4341
4342 mapped_index &index = *per_objfile->per_bfd->index_table;
4343
4344 bool result
4345 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4346 symbol_matcher,
4347 [&] (offset_type idx)
4348 {
4349 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4350 expansion_notify, search_flags, kind))
4351 return false;
4352 return true;
4353 }, per_objfile);
4354
4355 return result;
4356 }
4357
4358 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4359 symtab. */
4360
4361 static struct compunit_symtab *
4362 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4363 CORE_ADDR pc)
4364 {
4365 int i;
4366
4367 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4368 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4369 return cust;
4370
4371 if (cust->includes == NULL)
4372 return NULL;
4373
4374 for (i = 0; cust->includes[i]; ++i)
4375 {
4376 struct compunit_symtab *s = cust->includes[i];
4377
4378 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4379 if (s != NULL)
4380 return s;
4381 }
4382
4383 return NULL;
4384 }
4385
4386 struct compunit_symtab *
4387 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4388 (struct objfile *objfile,
4389 struct bound_minimal_symbol msymbol,
4390 CORE_ADDR pc,
4391 struct obj_section *section,
4392 int warn_if_readin)
4393 {
4394 struct dwarf2_per_cu_data *data;
4395 struct compunit_symtab *result;
4396
4397 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4398 if (per_objfile->per_bfd->index_addrmap == nullptr)
4399 return NULL;
4400
4401 CORE_ADDR baseaddr = objfile->text_section_offset ();
4402 data = ((struct dwarf2_per_cu_data *)
4403 addrmap_find (per_objfile->per_bfd->index_addrmap,
4404 pc - baseaddr));
4405 if (!data)
4406 return NULL;
4407
4408 if (warn_if_readin && per_objfile->symtab_set_p (data))
4409 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4410 paddress (objfile->arch (), pc));
4411
4412 result = recursively_find_pc_sect_compunit_symtab
4413 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4414
4415 gdb_assert (result != NULL);
4416 return result;
4417 }
4418
4419 void
4420 dwarf2_base_index_functions::map_symbol_filenames
4421 (struct objfile *objfile,
4422 gdb::function_view<symbol_filename_ftype> fun,
4423 bool need_fullname)
4424 {
4425 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4426
4427 /* Use caches to ensure we only call FUN once for each filename. */
4428 filename_seen_cache filenames_cache;
4429 std::unordered_set<quick_file_names *> qfn_cache;
4430
4431 /* The rule is CUs specify all the files, including those used by any TU,
4432 so there's no need to scan TUs here. We can ignore file names coming
4433 from already-expanded CUs. It is possible that an expanded CU might
4434 reuse the file names data from a currently unexpanded CU, in this
4435 case we don't want to report the files from the unexpanded CU. */
4436
4437 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4438 {
4439 if (per_objfile->symtab_set_p (per_cu.get ()))
4440 {
4441 if (per_cu->v.quick->file_names != nullptr)
4442 qfn_cache.insert (per_cu->v.quick->file_names);
4443 }
4444 }
4445
4446 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4447 {
4448 /* We only need to look at symtabs not already expanded. */
4449 if (per_objfile->symtab_set_p (per_cu.get ()))
4450 continue;
4451
4452 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4453 per_objfile);
4454 if (file_data == nullptr
4455 || qfn_cache.find (file_data) != qfn_cache.end ())
4456 continue;
4457
4458 for (int j = 0; j < file_data->num_file_names; ++j)
4459 {
4460 const char *filename = file_data->file_names[j];
4461 filenames_cache.seen (filename);
4462 }
4463 }
4464
4465 filenames_cache.traverse ([&] (const char *filename)
4466 {
4467 gdb::unique_xmalloc_ptr<char> this_real_name;
4468
4469 if (need_fullname)
4470 this_real_name = gdb_realpath (filename);
4471 fun (filename, this_real_name.get ());
4472 });
4473 }
4474
4475 bool
4476 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4477 {
4478 return true;
4479 }
4480
4481 /* DWARF-5 debug_names reader. */
4482
4483 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4484 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4485
4486 /* A helper function that reads the .debug_names section in SECTION
4487 and fills in MAP. FILENAME is the name of the file containing the
4488 section; it is used for error reporting.
4489
4490 Returns true if all went well, false otherwise. */
4491
4492 static bool
4493 read_debug_names_from_section (struct objfile *objfile,
4494 const char *filename,
4495 struct dwarf2_section_info *section,
4496 mapped_debug_names &map)
4497 {
4498 if (section->empty ())
4499 return false;
4500
4501 /* Older elfutils strip versions could keep the section in the main
4502 executable while splitting it for the separate debug info file. */
4503 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4504 return false;
4505
4506 section->read (objfile);
4507
4508 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4509
4510 const gdb_byte *addr = section->buffer;
4511
4512 bfd *const abfd = section->get_bfd_owner ();
4513
4514 unsigned int bytes_read;
4515 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4516 addr += bytes_read;
4517
4518 map.dwarf5_is_dwarf64 = bytes_read != 4;
4519 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4520 if (bytes_read + length != section->size)
4521 {
4522 /* There may be multiple per-CU indices. */
4523 warning (_("Section .debug_names in %s length %s does not match "
4524 "section length %s, ignoring .debug_names."),
4525 filename, plongest (bytes_read + length),
4526 pulongest (section->size));
4527 return false;
4528 }
4529
4530 /* The version number. */
4531 uint16_t version = read_2_bytes (abfd, addr);
4532 addr += 2;
4533 if (version != 5)
4534 {
4535 warning (_("Section .debug_names in %s has unsupported version %d, "
4536 "ignoring .debug_names."),
4537 filename, version);
4538 return false;
4539 }
4540
4541 /* Padding. */
4542 uint16_t padding = read_2_bytes (abfd, addr);
4543 addr += 2;
4544 if (padding != 0)
4545 {
4546 warning (_("Section .debug_names in %s has unsupported padding %d, "
4547 "ignoring .debug_names."),
4548 filename, padding);
4549 return false;
4550 }
4551
4552 /* comp_unit_count - The number of CUs in the CU list. */
4553 map.cu_count = read_4_bytes (abfd, addr);
4554 addr += 4;
4555
4556 /* local_type_unit_count - The number of TUs in the local TU
4557 list. */
4558 map.tu_count = read_4_bytes (abfd, addr);
4559 addr += 4;
4560
4561 /* foreign_type_unit_count - The number of TUs in the foreign TU
4562 list. */
4563 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4564 addr += 4;
4565 if (foreign_tu_count != 0)
4566 {
4567 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4568 "ignoring .debug_names."),
4569 filename, static_cast<unsigned long> (foreign_tu_count));
4570 return false;
4571 }
4572
4573 /* bucket_count - The number of hash buckets in the hash lookup
4574 table. */
4575 map.bucket_count = read_4_bytes (abfd, addr);
4576 addr += 4;
4577
4578 /* name_count - The number of unique names in the index. */
4579 map.name_count = read_4_bytes (abfd, addr);
4580 addr += 4;
4581
4582 /* abbrev_table_size - The size in bytes of the abbreviations
4583 table. */
4584 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4585 addr += 4;
4586
4587 /* augmentation_string_size - The size in bytes of the augmentation
4588 string. This value is rounded up to a multiple of 4. */
4589 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4590 addr += 4;
4591 map.augmentation_is_gdb = ((augmentation_string_size
4592 == sizeof (dwarf5_augmentation))
4593 && memcmp (addr, dwarf5_augmentation,
4594 sizeof (dwarf5_augmentation)) == 0);
4595 augmentation_string_size += (-augmentation_string_size) & 3;
4596 addr += augmentation_string_size;
4597
4598 /* List of CUs */
4599 map.cu_table_reordered = addr;
4600 addr += map.cu_count * map.offset_size;
4601
4602 /* List of Local TUs */
4603 map.tu_table_reordered = addr;
4604 addr += map.tu_count * map.offset_size;
4605
4606 /* Hash Lookup Table */
4607 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4608 addr += map.bucket_count * 4;
4609 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4610 addr += map.name_count * 4;
4611
4612 /* Name Table */
4613 map.name_table_string_offs_reordered = addr;
4614 addr += map.name_count * map.offset_size;
4615 map.name_table_entry_offs_reordered = addr;
4616 addr += map.name_count * map.offset_size;
4617
4618 const gdb_byte *abbrev_table_start = addr;
4619 for (;;)
4620 {
4621 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4622 addr += bytes_read;
4623 if (index_num == 0)
4624 break;
4625
4626 const auto insertpair
4627 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4628 if (!insertpair.second)
4629 {
4630 warning (_("Section .debug_names in %s has duplicate index %s, "
4631 "ignoring .debug_names."),
4632 filename, pulongest (index_num));
4633 return false;
4634 }
4635 mapped_debug_names::index_val &indexval = insertpair.first->second;
4636 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4637 addr += bytes_read;
4638
4639 for (;;)
4640 {
4641 mapped_debug_names::index_val::attr attr;
4642 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4643 addr += bytes_read;
4644 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4645 addr += bytes_read;
4646 if (attr.form == DW_FORM_implicit_const)
4647 {
4648 attr.implicit_const = read_signed_leb128 (abfd, addr,
4649 &bytes_read);
4650 addr += bytes_read;
4651 }
4652 if (attr.dw_idx == 0 && attr.form == 0)
4653 break;
4654 indexval.attr_vec.push_back (std::move (attr));
4655 }
4656 }
4657 if (addr != abbrev_table_start + abbrev_table_size)
4658 {
4659 warning (_("Section .debug_names in %s has abbreviation_table "
4660 "of size %s vs. written as %u, ignoring .debug_names."),
4661 filename, plongest (addr - abbrev_table_start),
4662 abbrev_table_size);
4663 return false;
4664 }
4665 map.entry_pool = addr;
4666
4667 return true;
4668 }
4669
4670 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4671 list. */
4672
4673 static void
4674 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4675 const mapped_debug_names &map,
4676 dwarf2_section_info &section,
4677 bool is_dwz)
4678 {
4679 if (!map.augmentation_is_gdb)
4680 {
4681 for (uint32_t i = 0; i < map.cu_count; ++i)
4682 {
4683 sect_offset sect_off
4684 = (sect_offset) (extract_unsigned_integer
4685 (map.cu_table_reordered + i * map.offset_size,
4686 map.offset_size,
4687 map.dwarf5_byte_order));
4688 /* We don't know the length of the CU, because the CU list in a
4689 .debug_names index can be incomplete, so we can't use the start
4690 of the next CU as end of this CU. We create the CUs here with
4691 length 0, and in cutu_reader::cutu_reader we'll fill in the
4692 actual length. */
4693 dwarf2_per_cu_data_up per_cu
4694 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4695 sect_off, 0);
4696 per_bfd->all_comp_units.push_back (std::move (per_cu));
4697 }
4698 return;
4699 }
4700
4701 sect_offset sect_off_prev;
4702 for (uint32_t i = 0; i <= map.cu_count; ++i)
4703 {
4704 sect_offset sect_off_next;
4705 if (i < map.cu_count)
4706 {
4707 sect_off_next
4708 = (sect_offset) (extract_unsigned_integer
4709 (map.cu_table_reordered + i * map.offset_size,
4710 map.offset_size,
4711 map.dwarf5_byte_order));
4712 }
4713 else
4714 sect_off_next = (sect_offset) section.size;
4715 if (i >= 1)
4716 {
4717 const ULONGEST length = sect_off_next - sect_off_prev;
4718 dwarf2_per_cu_data_up per_cu
4719 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4720 sect_off_prev, length);
4721 per_bfd->all_comp_units.push_back (std::move (per_cu));
4722 }
4723 sect_off_prev = sect_off_next;
4724 }
4725 }
4726
4727 /* Read the CU list from the mapped index, and use it to create all
4728 the CU objects for this dwarf2_per_objfile. */
4729
4730 static void
4731 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4732 const mapped_debug_names &map,
4733 const mapped_debug_names &dwz_map)
4734 {
4735 gdb_assert (per_bfd->all_comp_units.empty ());
4736 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4737
4738 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4739 false /* is_dwz */);
4740
4741 if (dwz_map.cu_count == 0)
4742 return;
4743
4744 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4745 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4746 true /* is_dwz */);
4747 }
4748
4749 /* Read .debug_names. If everything went ok, initialize the "quick"
4750 elements of all the CUs and return true. Otherwise, return false. */
4751
4752 static bool
4753 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4754 {
4755 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4756 mapped_debug_names dwz_map;
4757 struct objfile *objfile = per_objfile->objfile;
4758 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4759
4760 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4761 &per_bfd->debug_names, *map))
4762 return false;
4763
4764 /* Don't use the index if it's empty. */
4765 if (map->name_count == 0)
4766 return false;
4767
4768 /* If there is a .dwz file, read it so we can get its CU list as
4769 well. */
4770 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4771 if (dwz != NULL)
4772 {
4773 if (!read_debug_names_from_section (objfile,
4774 bfd_get_filename (dwz->dwz_bfd.get ()),
4775 &dwz->debug_names, dwz_map))
4776 {
4777 warning (_("could not read '.debug_names' section from %s; skipping"),
4778 bfd_get_filename (dwz->dwz_bfd.get ()));
4779 return false;
4780 }
4781 }
4782
4783 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4784
4785 if (map->tu_count != 0)
4786 {
4787 /* We can only handle a single .debug_types when we have an
4788 index. */
4789 if (per_bfd->types.size () != 1)
4790 return false;
4791
4792 dwarf2_section_info *section = &per_bfd->types[0];
4793
4794 create_signatured_type_table_from_debug_names
4795 (per_objfile, *map, section, &per_bfd->abbrev);
4796 }
4797
4798 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4799
4800 per_bfd->debug_names_table = std::move (map);
4801 per_bfd->using_index = 1;
4802 per_bfd->quick_file_names_table =
4803 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4804
4805 return true;
4806 }
4807
4808 /* Type used to manage iterating over all CUs looking for a symbol for
4809 .debug_names. */
4810
4811 class dw2_debug_names_iterator
4812 {
4813 public:
4814 dw2_debug_names_iterator (const mapped_debug_names &map,
4815 block_search_flags block_index,
4816 domain_enum domain,
4817 const char *name, dwarf2_per_objfile *per_objfile)
4818 : m_map (map), m_block_index (block_index), m_domain (domain),
4819 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4820 m_per_objfile (per_objfile)
4821 {}
4822
4823 dw2_debug_names_iterator (const mapped_debug_names &map,
4824 search_domain search, uint32_t namei,
4825 dwarf2_per_objfile *per_objfile,
4826 domain_enum domain = UNDEF_DOMAIN)
4827 : m_map (map),
4828 m_domain (domain),
4829 m_search (search),
4830 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4831 m_per_objfile (per_objfile)
4832 {}
4833
4834 dw2_debug_names_iterator (const mapped_debug_names &map,
4835 block_search_flags block_index, domain_enum domain,
4836 uint32_t namei, dwarf2_per_objfile *per_objfile)
4837 : m_map (map), m_block_index (block_index), m_domain (domain),
4838 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4839 m_per_objfile (per_objfile)
4840 {}
4841
4842 /* Return the next matching CU or NULL if there are no more. */
4843 dwarf2_per_cu_data *next ();
4844
4845 private:
4846 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4847 const char *name,
4848 dwarf2_per_objfile *per_objfile);
4849 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4850 uint32_t namei,
4851 dwarf2_per_objfile *per_objfile);
4852
4853 /* The internalized form of .debug_names. */
4854 const mapped_debug_names &m_map;
4855
4856 /* Restrict the search to these blocks. */
4857 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4858 | SEARCH_STATIC_BLOCK);
4859
4860 /* The kind of symbol we're looking for. */
4861 const domain_enum m_domain = UNDEF_DOMAIN;
4862 const search_domain m_search = ALL_DOMAIN;
4863
4864 /* The list of CUs from the index entry of the symbol, or NULL if
4865 not found. */
4866 const gdb_byte *m_addr;
4867
4868 dwarf2_per_objfile *m_per_objfile;
4869 };
4870
4871 const char *
4872 mapped_debug_names::namei_to_name
4873 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4874 {
4875 const ULONGEST namei_string_offs
4876 = extract_unsigned_integer ((name_table_string_offs_reordered
4877 + namei * offset_size),
4878 offset_size,
4879 dwarf5_byte_order);
4880 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4881 }
4882
4883 /* Find a slot in .debug_names for the object named NAME. If NAME is
4884 found, return pointer to its pool data. If NAME cannot be found,
4885 return NULL. */
4886
4887 const gdb_byte *
4888 dw2_debug_names_iterator::find_vec_in_debug_names
4889 (const mapped_debug_names &map, const char *name,
4890 dwarf2_per_objfile *per_objfile)
4891 {
4892 int (*cmp) (const char *, const char *);
4893
4894 gdb::unique_xmalloc_ptr<char> without_params;
4895 if (current_language->la_language == language_cplus
4896 || current_language->la_language == language_fortran
4897 || current_language->la_language == language_d)
4898 {
4899 /* NAME is already canonical. Drop any qualifiers as
4900 .debug_names does not contain any. */
4901
4902 if (strchr (name, '(') != NULL)
4903 {
4904 without_params = cp_remove_params (name);
4905 if (without_params != NULL)
4906 name = without_params.get ();
4907 }
4908 }
4909
4910 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4911
4912 const uint32_t full_hash = dwarf5_djb_hash (name);
4913 uint32_t namei
4914 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4915 (map.bucket_table_reordered
4916 + (full_hash % map.bucket_count)), 4,
4917 map.dwarf5_byte_order);
4918 if (namei == 0)
4919 return NULL;
4920 --namei;
4921 if (namei >= map.name_count)
4922 {
4923 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4924 "[in module %s]"),
4925 namei, map.name_count,
4926 objfile_name (per_objfile->objfile));
4927 return NULL;
4928 }
4929
4930 for (;;)
4931 {
4932 const uint32_t namei_full_hash
4933 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4934 (map.hash_table_reordered + namei), 4,
4935 map.dwarf5_byte_order);
4936 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4937 return NULL;
4938
4939 if (full_hash == namei_full_hash)
4940 {
4941 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4942
4943 #if 0 /* An expensive sanity check. */
4944 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4945 {
4946 complaint (_("Wrong .debug_names hash for string at index %u "
4947 "[in module %s]"),
4948 namei, objfile_name (dwarf2_per_objfile->objfile));
4949 return NULL;
4950 }
4951 #endif
4952
4953 if (cmp (namei_string, name) == 0)
4954 {
4955 const ULONGEST namei_entry_offs
4956 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4957 + namei * map.offset_size),
4958 map.offset_size, map.dwarf5_byte_order);
4959 return map.entry_pool + namei_entry_offs;
4960 }
4961 }
4962
4963 ++namei;
4964 if (namei >= map.name_count)
4965 return NULL;
4966 }
4967 }
4968
4969 const gdb_byte *
4970 dw2_debug_names_iterator::find_vec_in_debug_names
4971 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4972 {
4973 if (namei >= map.name_count)
4974 {
4975 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4976 "[in module %s]"),
4977 namei, map.name_count,
4978 objfile_name (per_objfile->objfile));
4979 return NULL;
4980 }
4981
4982 const ULONGEST namei_entry_offs
4983 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4984 + namei * map.offset_size),
4985 map.offset_size, map.dwarf5_byte_order);
4986 return map.entry_pool + namei_entry_offs;
4987 }
4988
4989 /* See dw2_debug_names_iterator. */
4990
4991 dwarf2_per_cu_data *
4992 dw2_debug_names_iterator::next ()
4993 {
4994 if (m_addr == NULL)
4995 return NULL;
4996
4997 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4998 struct objfile *objfile = m_per_objfile->objfile;
4999 bfd *const abfd = objfile->obfd;
5000
5001 again:
5002
5003 unsigned int bytes_read;
5004 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5005 m_addr += bytes_read;
5006 if (abbrev == 0)
5007 return NULL;
5008
5009 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5010 if (indexval_it == m_map.abbrev_map.cend ())
5011 {
5012 complaint (_("Wrong .debug_names undefined abbrev code %s "
5013 "[in module %s]"),
5014 pulongest (abbrev), objfile_name (objfile));
5015 return NULL;
5016 }
5017 const mapped_debug_names::index_val &indexval = indexval_it->second;
5018 enum class symbol_linkage {
5019 unknown,
5020 static_,
5021 extern_,
5022 } symbol_linkage_ = symbol_linkage::unknown;
5023 dwarf2_per_cu_data *per_cu = NULL;
5024 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5025 {
5026 ULONGEST ull;
5027 switch (attr.form)
5028 {
5029 case DW_FORM_implicit_const:
5030 ull = attr.implicit_const;
5031 break;
5032 case DW_FORM_flag_present:
5033 ull = 1;
5034 break;
5035 case DW_FORM_udata:
5036 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5037 m_addr += bytes_read;
5038 break;
5039 case DW_FORM_ref4:
5040 ull = read_4_bytes (abfd, m_addr);
5041 m_addr += 4;
5042 break;
5043 case DW_FORM_ref8:
5044 ull = read_8_bytes (abfd, m_addr);
5045 m_addr += 8;
5046 break;
5047 case DW_FORM_ref_sig8:
5048 ull = read_8_bytes (abfd, m_addr);
5049 m_addr += 8;
5050 break;
5051 default:
5052 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5053 dwarf_form_name (attr.form),
5054 objfile_name (objfile));
5055 return NULL;
5056 }
5057 switch (attr.dw_idx)
5058 {
5059 case DW_IDX_compile_unit:
5060 /* Don't crash on bad data. */
5061 if (ull >= per_bfd->all_comp_units.size ())
5062 {
5063 complaint (_(".debug_names entry has bad CU index %s"
5064 " [in module %s]"),
5065 pulongest (ull),
5066 objfile_name (objfile));
5067 continue;
5068 }
5069 per_cu = per_bfd->get_cu (ull);
5070 break;
5071 case DW_IDX_type_unit:
5072 /* Don't crash on bad data. */
5073 if (ull >= per_bfd->tu_stats.nr_tus)
5074 {
5075 complaint (_(".debug_names entry has bad TU index %s"
5076 " [in module %s]"),
5077 pulongest (ull),
5078 objfile_name (objfile));
5079 continue;
5080 }
5081 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5082 break;
5083 case DW_IDX_die_offset:
5084 /* In a per-CU index (as opposed to a per-module index), index
5085 entries without CU attribute implicitly refer to the single CU. */
5086 if (per_cu == NULL)
5087 per_cu = per_bfd->get_cu (0);
5088 break;
5089 case DW_IDX_GNU_internal:
5090 if (!m_map.augmentation_is_gdb)
5091 break;
5092 symbol_linkage_ = symbol_linkage::static_;
5093 break;
5094 case DW_IDX_GNU_external:
5095 if (!m_map.augmentation_is_gdb)
5096 break;
5097 symbol_linkage_ = symbol_linkage::extern_;
5098 break;
5099 }
5100 }
5101
5102 /* Skip if already read in. */
5103 if (m_per_objfile->symtab_set_p (per_cu))
5104 goto again;
5105
5106 /* Check static vs global. */
5107 if (symbol_linkage_ != symbol_linkage::unknown)
5108 {
5109 if (symbol_linkage_ == symbol_linkage::static_)
5110 {
5111 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5112 goto again;
5113 }
5114 else
5115 {
5116 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5117 goto again;
5118 }
5119 }
5120
5121 /* Match dw2_symtab_iter_next, symbol_kind
5122 and debug_names::psymbol_tag. */
5123 switch (m_domain)
5124 {
5125 case VAR_DOMAIN:
5126 switch (indexval.dwarf_tag)
5127 {
5128 case DW_TAG_variable:
5129 case DW_TAG_subprogram:
5130 /* Some types are also in VAR_DOMAIN. */
5131 case DW_TAG_typedef:
5132 case DW_TAG_structure_type:
5133 break;
5134 default:
5135 goto again;
5136 }
5137 break;
5138 case STRUCT_DOMAIN:
5139 switch (indexval.dwarf_tag)
5140 {
5141 case DW_TAG_typedef:
5142 case DW_TAG_structure_type:
5143 break;
5144 default:
5145 goto again;
5146 }
5147 break;
5148 case LABEL_DOMAIN:
5149 switch (indexval.dwarf_tag)
5150 {
5151 case 0:
5152 case DW_TAG_variable:
5153 break;
5154 default:
5155 goto again;
5156 }
5157 break;
5158 case MODULE_DOMAIN:
5159 switch (indexval.dwarf_tag)
5160 {
5161 case DW_TAG_module:
5162 break;
5163 default:
5164 goto again;
5165 }
5166 break;
5167 default:
5168 break;
5169 }
5170
5171 /* Match dw2_expand_symtabs_matching, symbol_kind and
5172 debug_names::psymbol_tag. */
5173 switch (m_search)
5174 {
5175 case VARIABLES_DOMAIN:
5176 switch (indexval.dwarf_tag)
5177 {
5178 case DW_TAG_variable:
5179 break;
5180 default:
5181 goto again;
5182 }
5183 break;
5184 case FUNCTIONS_DOMAIN:
5185 switch (indexval.dwarf_tag)
5186 {
5187 case DW_TAG_subprogram:
5188 break;
5189 default:
5190 goto again;
5191 }
5192 break;
5193 case TYPES_DOMAIN:
5194 switch (indexval.dwarf_tag)
5195 {
5196 case DW_TAG_typedef:
5197 case DW_TAG_structure_type:
5198 break;
5199 default:
5200 goto again;
5201 }
5202 break;
5203 case MODULES_DOMAIN:
5204 switch (indexval.dwarf_tag)
5205 {
5206 case DW_TAG_module:
5207 break;
5208 default:
5209 goto again;
5210 }
5211 default:
5212 break;
5213 }
5214
5215 return per_cu;
5216 }
5217
5218 /* This dumps minimal information about .debug_names. It is called
5219 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5220 uses this to verify that .debug_names has been loaded. */
5221
5222 void
5223 dwarf2_debug_names_index::dump (struct objfile *objfile)
5224 {
5225 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5226
5227 gdb_assert (per_objfile->per_bfd->using_index);
5228 printf_filtered (".debug_names:");
5229 if (per_objfile->per_bfd->debug_names_table)
5230 printf_filtered (" exists\n");
5231 else
5232 printf_filtered (" faked for \"readnow\"\n");
5233 printf_filtered ("\n");
5234 }
5235
5236 void
5237 dwarf2_debug_names_index::expand_matching_symbols
5238 (struct objfile *objfile,
5239 const lookup_name_info &name, domain_enum domain,
5240 int global,
5241 symbol_compare_ftype *ordered_compare)
5242 {
5243 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5244
5245 /* debug_names_table is NULL if OBJF_READNOW. */
5246 if (!per_objfile->per_bfd->debug_names_table)
5247 return;
5248
5249 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5250 const block_search_flags block_flags
5251 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5252
5253 const char *match_name = name.ada ().lookup_name ().c_str ();
5254 auto matcher = [&] (const char *symname)
5255 {
5256 if (ordered_compare == nullptr)
5257 return true;
5258 return ordered_compare (symname, match_name) == 0;
5259 };
5260
5261 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5262 [&] (offset_type namei)
5263 {
5264 /* The name was matched, now expand corresponding CUs that were
5265 marked. */
5266 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5267 per_objfile);
5268
5269 struct dwarf2_per_cu_data *per_cu;
5270 while ((per_cu = iter.next ()) != NULL)
5271 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5272 nullptr);
5273 return true;
5274 }, per_objfile);
5275 }
5276
5277 bool
5278 dwarf2_debug_names_index::expand_symtabs_matching
5279 (struct objfile *objfile,
5280 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5281 const lookup_name_info *lookup_name,
5282 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5283 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5284 block_search_flags search_flags,
5285 domain_enum domain,
5286 enum search_domain kind)
5287 {
5288 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5289
5290 /* debug_names_table is NULL if OBJF_READNOW. */
5291 if (!per_objfile->per_bfd->debug_names_table)
5292 return true;
5293
5294 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5295
5296 if (symbol_matcher == NULL && lookup_name == NULL)
5297 {
5298 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
5299 {
5300 QUIT;
5301
5302 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
5303 file_matcher,
5304 expansion_notify))
5305 return false;
5306 }
5307 return true;
5308 }
5309
5310 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5311
5312 bool result
5313 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5314 symbol_matcher,
5315 [&] (offset_type namei)
5316 {
5317 /* The name was matched, now expand corresponding CUs that were
5318 marked. */
5319 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5320
5321 struct dwarf2_per_cu_data *per_cu;
5322 while ((per_cu = iter.next ()) != NULL)
5323 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5324 file_matcher,
5325 expansion_notify))
5326 return false;
5327 return true;
5328 }, per_objfile);
5329
5330 return result;
5331 }
5332
5333 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5334 to either a dwarf2_per_bfd or dwz_file object. */
5335
5336 template <typename T>
5337 static gdb::array_view<const gdb_byte>
5338 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5339 {
5340 dwarf2_section_info *section = &section_owner->gdb_index;
5341
5342 if (section->empty ())
5343 return {};
5344
5345 /* Older elfutils strip versions could keep the section in the main
5346 executable while splitting it for the separate debug info file. */
5347 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5348 return {};
5349
5350 section->read (obj);
5351
5352 /* dwarf2_section_info::size is a bfd_size_type, while
5353 gdb::array_view works with size_t. On 32-bit hosts, with
5354 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5355 is 32-bit. So we need an explicit narrowing conversion here.
5356 This is fine, because it's impossible to allocate or mmap an
5357 array/buffer larger than what size_t can represent. */
5358 return gdb::make_array_view (section->buffer, section->size);
5359 }
5360
5361 /* Lookup the index cache for the contents of the index associated to
5362 DWARF2_OBJ. */
5363
5364 static gdb::array_view<const gdb_byte>
5365 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5366 {
5367 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5368 if (build_id == nullptr)
5369 return {};
5370
5371 return global_index_cache.lookup_gdb_index (build_id,
5372 &dwarf2_per_bfd->index_cache_res);
5373 }
5374
5375 /* Same as the above, but for DWZ. */
5376
5377 static gdb::array_view<const gdb_byte>
5378 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5379 {
5380 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5381 if (build_id == nullptr)
5382 return {};
5383
5384 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5385 }
5386
5387 /* See dwarf2/public.h. */
5388
5389 void
5390 dwarf2_initialize_objfile (struct objfile *objfile)
5391 {
5392 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5393 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5394
5395 dwarf_read_debug_printf ("called");
5396
5397 /* If we're about to read full symbols, don't bother with the
5398 indices. In this case we also don't care if some other debug
5399 format is making psymtabs, because they are all about to be
5400 expanded anyway. */
5401 if ((objfile->flags & OBJF_READNOW))
5402 {
5403 dwarf_read_debug_printf ("readnow requested");
5404
5405 /* When using READNOW, the using_index flag (set below) indicates that
5406 PER_BFD was already initialized, when we loaded some other objfile. */
5407 if (per_bfd->using_index)
5408 {
5409 dwarf_read_debug_printf ("using_index already set");
5410 per_objfile->resize_symtabs ();
5411 objfile->qf.push_front (make_dwarf_gdb_index ());
5412 return;
5413 }
5414
5415 per_bfd->using_index = 1;
5416 create_all_comp_units (per_objfile);
5417 per_bfd->quick_file_names_table
5418 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5419 per_objfile->resize_symtabs ();
5420
5421 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5422 {
5423 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5424
5425 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5426 struct dwarf2_per_cu_quick_data);
5427 }
5428
5429 /* Arrange for gdb to see the "quick" functions. However, these
5430 functions will be no-ops because we will have expanded all
5431 symtabs. */
5432 objfile->qf.push_front (make_dwarf_gdb_index ());
5433 return;
5434 }
5435
5436 /* Was a debug names index already read when we processed an objfile sharing
5437 PER_BFD? */
5438 if (per_bfd->debug_names_table != nullptr)
5439 {
5440 dwarf_read_debug_printf ("re-using shared debug names table");
5441 per_objfile->resize_symtabs ();
5442 objfile->qf.push_front (make_dwarf_debug_names ());
5443 return;
5444 }
5445
5446 /* Was a GDB index already read when we processed an objfile sharing
5447 PER_BFD? */
5448 if (per_bfd->index_table != nullptr)
5449 {
5450 dwarf_read_debug_printf ("re-using shared index table");
5451 per_objfile->resize_symtabs ();
5452 objfile->qf.push_front (make_dwarf_gdb_index ());
5453 return;
5454 }
5455
5456 /* There might already be partial symtabs built for this BFD. This happens
5457 when loading the same binary twice with the index-cache enabled. If so,
5458 don't try to read an index. The objfile / per_objfile initialization will
5459 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5460 code path. */
5461 if (per_bfd->partial_symtabs != nullptr)
5462 {
5463 dwarf_read_debug_printf ("re-using shared partial symtabs");
5464 objfile->qf.push_front (make_lazy_dwarf_reader ());
5465 return;
5466 }
5467
5468 if (dwarf2_read_debug_names (per_objfile))
5469 {
5470 dwarf_read_debug_printf ("found debug names");
5471 per_objfile->resize_symtabs ();
5472 objfile->qf.push_front (make_dwarf_debug_names ());
5473 return;
5474 }
5475
5476 if (dwarf2_read_gdb_index (per_objfile,
5477 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5478 get_gdb_index_contents_from_section<dwz_file>))
5479 {
5480 dwarf_read_debug_printf ("found gdb index from file");
5481 per_objfile->resize_symtabs ();
5482 objfile->qf.push_front (make_dwarf_gdb_index ());
5483 return;
5484 }
5485
5486 /* ... otherwise, try to find the index in the index cache. */
5487 if (dwarf2_read_gdb_index (per_objfile,
5488 get_gdb_index_contents_from_cache,
5489 get_gdb_index_contents_from_cache_dwz))
5490 {
5491 dwarf_read_debug_printf ("found gdb index from cache");
5492 global_index_cache.hit ();
5493 per_objfile->resize_symtabs ();
5494 objfile->qf.push_front (make_dwarf_gdb_index ());
5495 return;
5496 }
5497
5498 global_index_cache.miss ();
5499 objfile->qf.push_front (make_lazy_dwarf_reader ());
5500 }
5501
5502 \f
5503
5504 /* Build a partial symbol table. */
5505
5506 void
5507 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5508 {
5509 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5510 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5511
5512 if (per_bfd->partial_symtabs != nullptr)
5513 {
5514 /* Partial symbols were already read, so now we can simply
5515 attach them. */
5516 if (psf == nullptr)
5517 {
5518 psf = new psymbol_functions (per_bfd->partial_symtabs);
5519 objfile->qf.emplace_front (psf);
5520 }
5521 else
5522 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5523 per_objfile->resize_symtabs ();
5524 return;
5525 }
5526
5527 if (psf == nullptr)
5528 {
5529 psf = new psymbol_functions;
5530 objfile->qf.emplace_front (psf);
5531 }
5532 const std::shared_ptr<psymtab_storage> &partial_symtabs
5533 = psf->get_partial_symtabs ();
5534
5535 /* Set the local reference to partial symtabs, so that we don't try
5536 to read them again if reading another objfile with the same BFD.
5537 If we can't in fact share, this won't make a difference anyway as
5538 the dwarf2_per_bfd object won't be shared. */
5539 per_bfd->partial_symtabs = partial_symtabs;
5540
5541 try
5542 {
5543 /* This isn't really ideal: all the data we allocate on the
5544 objfile's obstack is still uselessly kept around. However,
5545 freeing it seems unsafe. */
5546 psymtab_discarder psymtabs (partial_symtabs.get ());
5547 dwarf2_build_psymtabs_hard (per_objfile);
5548 psymtabs.keep ();
5549
5550 per_objfile->resize_symtabs ();
5551
5552 /* (maybe) store an index in the cache. */
5553 global_index_cache.store (per_objfile);
5554 }
5555 catch (const gdb_exception_error &except)
5556 {
5557 exception_print (gdb_stderr, except);
5558 }
5559 }
5560
5561 /* Find the base address of the compilation unit for range lists and
5562 location lists. It will normally be specified by DW_AT_low_pc.
5563 In DWARF-3 draft 4, the base address could be overridden by
5564 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5565 compilation units with discontinuous ranges. */
5566
5567 static void
5568 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5569 {
5570 struct attribute *attr;
5571
5572 cu->base_address.reset ();
5573
5574 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5575 if (attr != nullptr)
5576 cu->base_address = attr->as_address ();
5577 else
5578 {
5579 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5580 if (attr != nullptr)
5581 cu->base_address = attr->as_address ();
5582 }
5583 }
5584
5585 /* Helper function that returns the proper abbrev section for
5586 THIS_CU. */
5587
5588 static struct dwarf2_section_info *
5589 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5590 {
5591 struct dwarf2_section_info *abbrev;
5592 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5593
5594 if (this_cu->is_dwz)
5595 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5596 else
5597 abbrev = &per_bfd->abbrev;
5598
5599 return abbrev;
5600 }
5601
5602 /* Fetch the abbreviation table offset from a comp or type unit header. */
5603
5604 static sect_offset
5605 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5606 struct dwarf2_section_info *section,
5607 sect_offset sect_off)
5608 {
5609 bfd *abfd = section->get_bfd_owner ();
5610 const gdb_byte *info_ptr;
5611 unsigned int initial_length_size, offset_size;
5612 uint16_t version;
5613
5614 section->read (per_objfile->objfile);
5615 info_ptr = section->buffer + to_underlying (sect_off);
5616 read_initial_length (abfd, info_ptr, &initial_length_size);
5617 offset_size = initial_length_size == 4 ? 4 : 8;
5618 info_ptr += initial_length_size;
5619
5620 version = read_2_bytes (abfd, info_ptr);
5621 info_ptr += 2;
5622 if (version >= 5)
5623 {
5624 /* Skip unit type and address size. */
5625 info_ptr += 2;
5626 }
5627
5628 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5629 }
5630
5631 /* A partial symtab that is used only for include files. */
5632 struct dwarf2_include_psymtab : public partial_symtab
5633 {
5634 dwarf2_include_psymtab (const char *filename,
5635 psymtab_storage *partial_symtabs,
5636 objfile_per_bfd_storage *objfile_per_bfd)
5637 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5638 {
5639 }
5640
5641 void read_symtab (struct objfile *objfile) override
5642 {
5643 /* It's an include file, no symbols to read for it.
5644 Everything is in the includer symtab. */
5645
5646 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5647 expansion of the includer psymtab. We use the dependencies[0] field to
5648 model the includer. But if we go the regular route of calling
5649 expand_psymtab here, and having expand_psymtab call expand_dependencies
5650 to expand the includer, we'll only use expand_psymtab on the includer
5651 (making it a non-toplevel psymtab), while if we expand the includer via
5652 another path, we'll use read_symtab (making it a toplevel psymtab).
5653 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5654 psymtab, and trigger read_symtab on the includer here directly. */
5655 includer ()->read_symtab (objfile);
5656 }
5657
5658 void expand_psymtab (struct objfile *objfile) override
5659 {
5660 /* This is not called by read_symtab, and should not be called by any
5661 expand_dependencies. */
5662 gdb_assert (false);
5663 }
5664
5665 bool readin_p (struct objfile *objfile) const override
5666 {
5667 return includer ()->readin_p (objfile);
5668 }
5669
5670 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5671 {
5672 return nullptr;
5673 }
5674
5675 private:
5676 partial_symtab *includer () const
5677 {
5678 /* An include psymtab has exactly one dependency: the psymtab that
5679 includes it. */
5680 gdb_assert (this->number_of_dependencies == 1);
5681 return this->dependencies[0];
5682 }
5683 };
5684
5685 /* Allocate a new partial symtab for file named NAME and mark this new
5686 partial symtab as being an include of PST. */
5687
5688 static void
5689 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5690 const char *name,
5691 dwarf2_psymtab *pst,
5692 psymtab_storage *partial_symtabs,
5693 objfile_per_bfd_storage *objfile_per_bfd)
5694 {
5695 dwarf2_include_psymtab *subpst
5696 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5697
5698 if (!IS_ABSOLUTE_PATH (subpst->filename))
5699 subpst->dirname = pst->dirname;
5700
5701 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5702 subpst->dependencies[0] = pst;
5703 subpst->number_of_dependencies = 1;
5704 }
5705
5706 /* Read the Line Number Program data and extract the list of files
5707 included by the source file represented by PST. Build an include
5708 partial symtab for each of these included files. */
5709
5710 static void
5711 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5712 struct die_info *die,
5713 dwarf2_psymtab *pst)
5714 {
5715 line_header_up lh;
5716 struct attribute *attr;
5717
5718 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5719 if (attr != nullptr && attr->form_is_unsigned ())
5720 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5721 if (lh == NULL)
5722 return; /* No linetable, so no includes. */
5723
5724 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5725 that we pass in the raw text_low here; that is ok because we're
5726 only decoding the line table to make include partial symtabs, and
5727 so the addresses aren't really used. */
5728 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5729 pst->raw_text_low (), 1);
5730 }
5731
5732 static hashval_t
5733 hash_signatured_type (const void *item)
5734 {
5735 const struct signatured_type *sig_type
5736 = (const struct signatured_type *) item;
5737
5738 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5739 return sig_type->signature;
5740 }
5741
5742 static int
5743 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5744 {
5745 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5746 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5747
5748 return lhs->signature == rhs->signature;
5749 }
5750
5751 /* Allocate a hash table for signatured types. */
5752
5753 static htab_up
5754 allocate_signatured_type_table ()
5755 {
5756 return htab_up (htab_create_alloc (41,
5757 hash_signatured_type,
5758 eq_signatured_type,
5759 NULL, xcalloc, xfree));
5760 }
5761
5762 /* A helper for create_debug_types_hash_table. Read types from SECTION
5763 and fill them into TYPES_HTAB. It will process only type units,
5764 therefore DW_UT_type. */
5765
5766 static void
5767 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5768 struct dwo_file *dwo_file,
5769 dwarf2_section_info *section, htab_up &types_htab,
5770 rcuh_kind section_kind)
5771 {
5772 struct objfile *objfile = per_objfile->objfile;
5773 struct dwarf2_section_info *abbrev_section;
5774 bfd *abfd;
5775 const gdb_byte *info_ptr, *end_ptr;
5776
5777 abbrev_section = &dwo_file->sections.abbrev;
5778
5779 dwarf_read_debug_printf ("Reading %s for %s",
5780 section->get_name (),
5781 abbrev_section->get_file_name ());
5782
5783 section->read (objfile);
5784 info_ptr = section->buffer;
5785
5786 if (info_ptr == NULL)
5787 return;
5788
5789 /* We can't set abfd until now because the section may be empty or
5790 not present, in which case the bfd is unknown. */
5791 abfd = section->get_bfd_owner ();
5792
5793 /* We don't use cutu_reader here because we don't need to read
5794 any dies: the signature is in the header. */
5795
5796 end_ptr = info_ptr + section->size;
5797 while (info_ptr < end_ptr)
5798 {
5799 std::unique_ptr<signatured_type> sig_type;
5800 struct dwo_unit *dwo_tu;
5801 void **slot;
5802 const gdb_byte *ptr = info_ptr;
5803 struct comp_unit_head header;
5804 unsigned int length;
5805
5806 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5807
5808 /* Initialize it due to a false compiler warning. */
5809 header.signature = -1;
5810 header.type_cu_offset_in_tu = (cu_offset) -1;
5811
5812 /* We need to read the type's signature in order to build the hash
5813 table, but we don't need anything else just yet. */
5814
5815 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5816 abbrev_section, ptr, section_kind);
5817
5818 length = header.get_length ();
5819
5820 /* Skip dummy type units. */
5821 if (ptr >= info_ptr + length
5822 || peek_abbrev_code (abfd, ptr) == 0
5823 || (header.unit_type != DW_UT_type
5824 && header.unit_type != DW_UT_split_type))
5825 {
5826 info_ptr += length;
5827 continue;
5828 }
5829
5830 if (types_htab == NULL)
5831 types_htab = allocate_dwo_unit_table ();
5832
5833 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5834 dwo_tu->dwo_file = dwo_file;
5835 dwo_tu->signature = header.signature;
5836 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5837 dwo_tu->section = section;
5838 dwo_tu->sect_off = sect_off;
5839 dwo_tu->length = length;
5840
5841 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5842 gdb_assert (slot != NULL);
5843 if (*slot != NULL)
5844 complaint (_("debug type entry at offset %s is duplicate to"
5845 " the entry at offset %s, signature %s"),
5846 sect_offset_str (sect_off),
5847 sect_offset_str (dwo_tu->sect_off),
5848 hex_string (header.signature));
5849 *slot = dwo_tu;
5850
5851 dwarf_read_debug_printf_v (" offset %s, signature %s",
5852 sect_offset_str (sect_off),
5853 hex_string (header.signature));
5854
5855 info_ptr += length;
5856 }
5857 }
5858
5859 /* Create the hash table of all entries in the .debug_types
5860 (or .debug_types.dwo) section(s).
5861 DWO_FILE is a pointer to the DWO file object.
5862
5863 The result is a pointer to the hash table or NULL if there are no types.
5864
5865 Note: This function processes DWO files only, not DWP files. */
5866
5867 static void
5868 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5869 struct dwo_file *dwo_file,
5870 gdb::array_view<dwarf2_section_info> type_sections,
5871 htab_up &types_htab)
5872 {
5873 for (dwarf2_section_info &section : type_sections)
5874 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5875 rcuh_kind::TYPE);
5876 }
5877
5878 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5879 If SLOT is non-NULL, it is the entry to use in the hash table.
5880 Otherwise we find one. */
5881
5882 static struct signatured_type *
5883 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5884 {
5885 if (per_objfile->per_bfd->all_comp_units.size ()
5886 == per_objfile->per_bfd->all_comp_units.capacity ())
5887 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5888
5889 std::unique_ptr<signatured_type> sig_type_holder
5890 = per_objfile->per_bfd->allocate_signatured_type ();
5891 signatured_type *sig_type = sig_type_holder.get ();
5892
5893 per_objfile->resize_symtabs ();
5894
5895 per_objfile->per_bfd->all_comp_units.emplace_back
5896 (sig_type_holder.release ());
5897 sig_type->signature = sig;
5898 if (per_objfile->per_bfd->using_index)
5899 {
5900 sig_type->v.quick =
5901 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
5902 struct dwarf2_per_cu_quick_data);
5903 }
5904
5905 if (slot == NULL)
5906 {
5907 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5908 sig_type, INSERT);
5909 }
5910 gdb_assert (*slot == NULL);
5911 *slot = sig_type;
5912 /* The rest of sig_type must be filled in by the caller. */
5913 return sig_type;
5914 }
5915
5916 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5917 Fill in SIG_ENTRY with DWO_ENTRY. */
5918
5919 static void
5920 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5921 struct signatured_type *sig_entry,
5922 struct dwo_unit *dwo_entry)
5923 {
5924 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5925
5926 /* Make sure we're not clobbering something we don't expect to. */
5927 gdb_assert (! sig_entry->queued);
5928 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5929 if (per_bfd->using_index)
5930 {
5931 gdb_assert (sig_entry->v.quick != NULL);
5932 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5933 }
5934 else
5935 gdb_assert (sig_entry->v.psymtab == NULL);
5936 gdb_assert (sig_entry->signature == dwo_entry->signature);
5937 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
5938 gdb_assert (sig_entry->type_unit_group == NULL);
5939 gdb_assert (sig_entry->dwo_unit == NULL);
5940
5941 sig_entry->section = dwo_entry->section;
5942 sig_entry->sect_off = dwo_entry->sect_off;
5943 sig_entry->length = dwo_entry->length;
5944 sig_entry->reading_dwo_directly = 1;
5945 sig_entry->per_bfd = per_bfd;
5946 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5947 sig_entry->dwo_unit = dwo_entry;
5948 }
5949
5950 /* Subroutine of lookup_signatured_type.
5951 If we haven't read the TU yet, create the signatured_type data structure
5952 for a TU to be read in directly from a DWO file, bypassing the stub.
5953 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5954 using .gdb_index, then when reading a CU we want to stay in the DWO file
5955 containing that CU. Otherwise we could end up reading several other DWO
5956 files (due to comdat folding) to process the transitive closure of all the
5957 mentioned TUs, and that can be slow. The current DWO file will have every
5958 type signature that it needs.
5959 We only do this for .gdb_index because in the psymtab case we already have
5960 to read all the DWOs to build the type unit groups. */
5961
5962 static struct signatured_type *
5963 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5964 {
5965 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5966 struct dwo_file *dwo_file;
5967 struct dwo_unit find_dwo_entry, *dwo_entry;
5968 struct signatured_type find_sig_entry, *sig_entry;
5969 void **slot;
5970
5971 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
5972
5973 /* If TU skeletons have been removed then we may not have read in any
5974 TUs yet. */
5975 if (per_objfile->per_bfd->signatured_types == NULL)
5976 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5977
5978 /* We only ever need to read in one copy of a signatured type.
5979 Use the global signatured_types array to do our own comdat-folding
5980 of types. If this is the first time we're reading this TU, and
5981 the TU has an entry in .gdb_index, replace the recorded data from
5982 .gdb_index with this TU. */
5983
5984 find_sig_entry.signature = sig;
5985 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5986 &find_sig_entry, INSERT);
5987 sig_entry = (struct signatured_type *) *slot;
5988
5989 /* We can get here with the TU already read, *or* in the process of being
5990 read. Don't reassign the global entry to point to this DWO if that's
5991 the case. Also note that if the TU is already being read, it may not
5992 have come from a DWO, the program may be a mix of Fission-compiled
5993 code and non-Fission-compiled code. */
5994
5995 /* Have we already tried to read this TU?
5996 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5997 needn't exist in the global table yet). */
5998 if (sig_entry != NULL && sig_entry->tu_read)
5999 return sig_entry;
6000
6001 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6002 dwo_unit of the TU itself. */
6003 dwo_file = cu->dwo_unit->dwo_file;
6004
6005 /* Ok, this is the first time we're reading this TU. */
6006 if (dwo_file->tus == NULL)
6007 return NULL;
6008 find_dwo_entry.signature = sig;
6009 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6010 &find_dwo_entry);
6011 if (dwo_entry == NULL)
6012 return NULL;
6013
6014 /* If the global table doesn't have an entry for this TU, add one. */
6015 if (sig_entry == NULL)
6016 sig_entry = add_type_unit (per_objfile, sig, slot);
6017
6018 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6019 sig_entry->tu_read = 1;
6020 return sig_entry;
6021 }
6022
6023 /* Subroutine of lookup_signatured_type.
6024 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6025 then try the DWP file. If the TU stub (skeleton) has been removed then
6026 it won't be in .gdb_index. */
6027
6028 static struct signatured_type *
6029 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6030 {
6031 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6032 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6033 struct dwo_unit *dwo_entry;
6034 struct signatured_type find_sig_entry, *sig_entry;
6035 void **slot;
6036
6037 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6038 gdb_assert (dwp_file != NULL);
6039
6040 /* If TU skeletons have been removed then we may not have read in any
6041 TUs yet. */
6042 if (per_objfile->per_bfd->signatured_types == NULL)
6043 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6044
6045 find_sig_entry.signature = sig;
6046 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6047 &find_sig_entry, INSERT);
6048 sig_entry = (struct signatured_type *) *slot;
6049
6050 /* Have we already tried to read this TU?
6051 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6052 needn't exist in the global table yet). */
6053 if (sig_entry != NULL)
6054 return sig_entry;
6055
6056 if (dwp_file->tus == NULL)
6057 return NULL;
6058 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6059 1 /* is_debug_types */);
6060 if (dwo_entry == NULL)
6061 return NULL;
6062
6063 sig_entry = add_type_unit (per_objfile, sig, slot);
6064 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6065
6066 return sig_entry;
6067 }
6068
6069 /* Lookup a signature based type for DW_FORM_ref_sig8.
6070 Returns NULL if signature SIG is not present in the table.
6071 It is up to the caller to complain about this. */
6072
6073 static struct signatured_type *
6074 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6075 {
6076 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6077
6078 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6079 {
6080 /* We're in a DWO/DWP file, and we're using .gdb_index.
6081 These cases require special processing. */
6082 if (get_dwp_file (per_objfile) == NULL)
6083 return lookup_dwo_signatured_type (cu, sig);
6084 else
6085 return lookup_dwp_signatured_type (cu, sig);
6086 }
6087 else
6088 {
6089 struct signatured_type find_entry, *entry;
6090
6091 if (per_objfile->per_bfd->signatured_types == NULL)
6092 return NULL;
6093 find_entry.signature = sig;
6094 entry = ((struct signatured_type *)
6095 htab_find (per_objfile->per_bfd->signatured_types.get (),
6096 &find_entry));
6097 return entry;
6098 }
6099 }
6100
6101 /* Low level DIE reading support. */
6102
6103 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6104
6105 static void
6106 init_cu_die_reader (struct die_reader_specs *reader,
6107 struct dwarf2_cu *cu,
6108 struct dwarf2_section_info *section,
6109 struct dwo_file *dwo_file,
6110 struct abbrev_table *abbrev_table)
6111 {
6112 gdb_assert (section->readin && section->buffer != NULL);
6113 reader->abfd = section->get_bfd_owner ();
6114 reader->cu = cu;
6115 reader->dwo_file = dwo_file;
6116 reader->die_section = section;
6117 reader->buffer = section->buffer;
6118 reader->buffer_end = section->buffer + section->size;
6119 reader->abbrev_table = abbrev_table;
6120 }
6121
6122 /* Subroutine of cutu_reader to simplify it.
6123 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6124 There's just a lot of work to do, and cutu_reader is big enough
6125 already.
6126
6127 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6128 from it to the DIE in the DWO. If NULL we are skipping the stub.
6129 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6130 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6131 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6132 STUB_COMP_DIR may be non-NULL.
6133 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6134 are filled in with the info of the DIE from the DWO file.
6135 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6136 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6137 kept around for at least as long as *RESULT_READER.
6138
6139 The result is non-zero if a valid (non-dummy) DIE was found. */
6140
6141 static int
6142 read_cutu_die_from_dwo (dwarf2_cu *cu,
6143 struct dwo_unit *dwo_unit,
6144 struct die_info *stub_comp_unit_die,
6145 const char *stub_comp_dir,
6146 struct die_reader_specs *result_reader,
6147 const gdb_byte **result_info_ptr,
6148 struct die_info **result_comp_unit_die,
6149 abbrev_table_up *result_dwo_abbrev_table)
6150 {
6151 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6152 dwarf2_per_cu_data *per_cu = cu->per_cu;
6153 struct objfile *objfile = per_objfile->objfile;
6154 bfd *abfd;
6155 const gdb_byte *begin_info_ptr, *info_ptr;
6156 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6157 int i,num_extra_attrs;
6158 struct dwarf2_section_info *dwo_abbrev_section;
6159 struct die_info *comp_unit_die;
6160
6161 /* At most one of these may be provided. */
6162 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6163
6164 /* These attributes aren't processed until later:
6165 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6166 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6167 referenced later. However, these attributes are found in the stub
6168 which we won't have later. In order to not impose this complication
6169 on the rest of the code, we read them here and copy them to the
6170 DWO CU/TU die. */
6171
6172 stmt_list = NULL;
6173 low_pc = NULL;
6174 high_pc = NULL;
6175 ranges = NULL;
6176 comp_dir = NULL;
6177
6178 if (stub_comp_unit_die != NULL)
6179 {
6180 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6181 DWO file. */
6182 if (!per_cu->is_debug_types)
6183 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6184 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6185 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6186 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6187 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6188
6189 cu->addr_base = stub_comp_unit_die->addr_base ();
6190
6191 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6192 We need the value before we can process DW_AT_ranges values from the
6193 DWO. */
6194 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6195
6196 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6197 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6198 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6199 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6200 section. */
6201 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6202 }
6203 else if (stub_comp_dir != NULL)
6204 {
6205 /* Reconstruct the comp_dir attribute to simplify the code below. */
6206 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6207 comp_dir->name = DW_AT_comp_dir;
6208 comp_dir->form = DW_FORM_string;
6209 comp_dir->set_string_noncanonical (stub_comp_dir);
6210 }
6211
6212 /* Set up for reading the DWO CU/TU. */
6213 cu->dwo_unit = dwo_unit;
6214 dwarf2_section_info *section = dwo_unit->section;
6215 section->read (objfile);
6216 abfd = section->get_bfd_owner ();
6217 begin_info_ptr = info_ptr = (section->buffer
6218 + to_underlying (dwo_unit->sect_off));
6219 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6220
6221 if (per_cu->is_debug_types)
6222 {
6223 signatured_type *sig_type = (struct signatured_type *) per_cu;
6224
6225 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6226 section, dwo_abbrev_section,
6227 info_ptr, rcuh_kind::TYPE);
6228 /* This is not an assert because it can be caused by bad debug info. */
6229 if (sig_type->signature != cu->header.signature)
6230 {
6231 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6232 " TU at offset %s [in module %s]"),
6233 hex_string (sig_type->signature),
6234 hex_string (cu->header.signature),
6235 sect_offset_str (dwo_unit->sect_off),
6236 bfd_get_filename (abfd));
6237 }
6238 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6239 /* For DWOs coming from DWP files, we don't know the CU length
6240 nor the type's offset in the TU until now. */
6241 dwo_unit->length = cu->header.get_length ();
6242 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6243
6244 /* Establish the type offset that can be used to lookup the type.
6245 For DWO files, we don't know it until now. */
6246 sig_type->type_offset_in_section
6247 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6248 }
6249 else
6250 {
6251 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6252 section, dwo_abbrev_section,
6253 info_ptr, rcuh_kind::COMPILE);
6254 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6255 /* For DWOs coming from DWP files, we don't know the CU length
6256 until now. */
6257 dwo_unit->length = cu->header.get_length ();
6258 }
6259
6260 dwo_abbrev_section->read (objfile);
6261 *result_dwo_abbrev_table
6262 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6263 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6264 result_dwo_abbrev_table->get ());
6265
6266 /* Read in the die, but leave space to copy over the attributes
6267 from the stub. This has the benefit of simplifying the rest of
6268 the code - all the work to maintain the illusion of a single
6269 DW_TAG_{compile,type}_unit DIE is done here. */
6270 num_extra_attrs = ((stmt_list != NULL)
6271 + (low_pc != NULL)
6272 + (high_pc != NULL)
6273 + (ranges != NULL)
6274 + (comp_dir != NULL));
6275 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6276 num_extra_attrs);
6277
6278 /* Copy over the attributes from the stub to the DIE we just read in. */
6279 comp_unit_die = *result_comp_unit_die;
6280 i = comp_unit_die->num_attrs;
6281 if (stmt_list != NULL)
6282 comp_unit_die->attrs[i++] = *stmt_list;
6283 if (low_pc != NULL)
6284 comp_unit_die->attrs[i++] = *low_pc;
6285 if (high_pc != NULL)
6286 comp_unit_die->attrs[i++] = *high_pc;
6287 if (ranges != NULL)
6288 comp_unit_die->attrs[i++] = *ranges;
6289 if (comp_dir != NULL)
6290 comp_unit_die->attrs[i++] = *comp_dir;
6291 comp_unit_die->num_attrs += num_extra_attrs;
6292
6293 if (dwarf_die_debug)
6294 {
6295 fprintf_unfiltered (gdb_stdlog,
6296 "Read die from %s@0x%x of %s:\n",
6297 section->get_name (),
6298 (unsigned) (begin_info_ptr - section->buffer),
6299 bfd_get_filename (abfd));
6300 dump_die (comp_unit_die, dwarf_die_debug);
6301 }
6302
6303 /* Skip dummy compilation units. */
6304 if (info_ptr >= begin_info_ptr + dwo_unit->length
6305 || peek_abbrev_code (abfd, info_ptr) == 0)
6306 return 0;
6307
6308 *result_info_ptr = info_ptr;
6309 return 1;
6310 }
6311
6312 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6313 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6314 signature is part of the header. */
6315 static gdb::optional<ULONGEST>
6316 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6317 {
6318 if (cu->header.version >= 5)
6319 return cu->header.signature;
6320 struct attribute *attr;
6321 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6322 if (attr == nullptr || !attr->form_is_unsigned ())
6323 return gdb::optional<ULONGEST> ();
6324 return attr->as_unsigned ();
6325 }
6326
6327 /* Subroutine of cutu_reader to simplify it.
6328 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6329 Returns NULL if the specified DWO unit cannot be found. */
6330
6331 static struct dwo_unit *
6332 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6333 {
6334 dwarf2_per_cu_data *per_cu = cu->per_cu;
6335 struct dwo_unit *dwo_unit;
6336 const char *comp_dir;
6337
6338 gdb_assert (cu != NULL);
6339
6340 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6341 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6342 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6343
6344 if (per_cu->is_debug_types)
6345 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6346 else
6347 {
6348 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6349
6350 if (!signature.has_value ())
6351 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6352 " [in module %s]"),
6353 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6354
6355 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6356 }
6357
6358 return dwo_unit;
6359 }
6360
6361 /* Subroutine of cutu_reader to simplify it.
6362 See it for a description of the parameters.
6363 Read a TU directly from a DWO file, bypassing the stub. */
6364
6365 void
6366 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6367 dwarf2_per_objfile *per_objfile,
6368 dwarf2_cu *existing_cu)
6369 {
6370 struct signatured_type *sig_type;
6371
6372 /* Verify we can do the following downcast, and that we have the
6373 data we need. */
6374 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6375 sig_type = (struct signatured_type *) this_cu;
6376 gdb_assert (sig_type->dwo_unit != NULL);
6377
6378 dwarf2_cu *cu;
6379
6380 if (existing_cu != nullptr)
6381 {
6382 cu = existing_cu;
6383 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6384 /* There's no need to do the rereading_dwo_cu handling that
6385 cutu_reader does since we don't read the stub. */
6386 }
6387 else
6388 {
6389 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6390 in per_objfile yet. */
6391 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6392 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6393 cu = m_new_cu.get ();
6394 }
6395
6396 /* A future optimization, if needed, would be to use an existing
6397 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6398 could share abbrev tables. */
6399
6400 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6401 NULL /* stub_comp_unit_die */,
6402 sig_type->dwo_unit->dwo_file->comp_dir,
6403 this, &info_ptr,
6404 &comp_unit_die,
6405 &m_dwo_abbrev_table) == 0)
6406 {
6407 /* Dummy die. */
6408 dummy_p = true;
6409 }
6410 }
6411
6412 /* Initialize a CU (or TU) and read its DIEs.
6413 If the CU defers to a DWO file, read the DWO file as well.
6414
6415 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6416 Otherwise the table specified in the comp unit header is read in and used.
6417 This is an optimization for when we already have the abbrev table.
6418
6419 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6420 allocated. */
6421
6422 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6423 dwarf2_per_objfile *per_objfile,
6424 struct abbrev_table *abbrev_table,
6425 dwarf2_cu *existing_cu,
6426 bool skip_partial)
6427 : die_reader_specs {},
6428 m_this_cu (this_cu)
6429 {
6430 struct objfile *objfile = per_objfile->objfile;
6431 struct dwarf2_section_info *section = this_cu->section;
6432 bfd *abfd = section->get_bfd_owner ();
6433 const gdb_byte *begin_info_ptr;
6434 struct signatured_type *sig_type = NULL;
6435 struct dwarf2_section_info *abbrev_section;
6436 /* Non-zero if CU currently points to a DWO file and we need to
6437 reread it. When this happens we need to reread the skeleton die
6438 before we can reread the DWO file (this only applies to CUs, not TUs). */
6439 int rereading_dwo_cu = 0;
6440
6441 if (dwarf_die_debug)
6442 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6443 this_cu->is_debug_types ? "type" : "comp",
6444 sect_offset_str (this_cu->sect_off));
6445
6446 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6447 file (instead of going through the stub), short-circuit all of this. */
6448 if (this_cu->reading_dwo_directly)
6449 {
6450 /* Narrow down the scope of possibilities to have to understand. */
6451 gdb_assert (this_cu->is_debug_types);
6452 gdb_assert (abbrev_table == NULL);
6453 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6454 return;
6455 }
6456
6457 /* This is cheap if the section is already read in. */
6458 section->read (objfile);
6459
6460 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6461
6462 abbrev_section = get_abbrev_section_for_cu (this_cu);
6463
6464 dwarf2_cu *cu;
6465
6466 if (existing_cu != nullptr)
6467 {
6468 cu = existing_cu;
6469 /* If this CU is from a DWO file we need to start over, we need to
6470 refetch the attributes from the skeleton CU.
6471 This could be optimized by retrieving those attributes from when we
6472 were here the first time: the previous comp_unit_die was stored in
6473 comp_unit_obstack. But there's no data yet that we need this
6474 optimization. */
6475 if (cu->dwo_unit != NULL)
6476 rereading_dwo_cu = 1;
6477 }
6478 else
6479 {
6480 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6481 in per_objfile yet. */
6482 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6483 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6484 cu = m_new_cu.get ();
6485 }
6486
6487 /* Get the header. */
6488 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6489 {
6490 /* We already have the header, there's no need to read it in again. */
6491 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6492 }
6493 else
6494 {
6495 if (this_cu->is_debug_types)
6496 {
6497 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6498 section, abbrev_section,
6499 info_ptr, rcuh_kind::TYPE);
6500
6501 /* Since per_cu is the first member of struct signatured_type,
6502 we can go from a pointer to one to a pointer to the other. */
6503 sig_type = (struct signatured_type *) this_cu;
6504 gdb_assert (sig_type->signature == cu->header.signature);
6505 gdb_assert (sig_type->type_offset_in_tu
6506 == cu->header.type_cu_offset_in_tu);
6507 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6508
6509 /* LENGTH has not been set yet for type units if we're
6510 using .gdb_index. */
6511 this_cu->length = cu->header.get_length ();
6512
6513 /* Establish the type offset that can be used to lookup the type. */
6514 sig_type->type_offset_in_section =
6515 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6516
6517 this_cu->dwarf_version = cu->header.version;
6518 }
6519 else
6520 {
6521 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6522 section, abbrev_section,
6523 info_ptr,
6524 rcuh_kind::COMPILE);
6525
6526 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6527 if (this_cu->length == 0)
6528 this_cu->length = cu->header.get_length ();
6529 else
6530 gdb_assert (this_cu->length == cu->header.get_length ());
6531 this_cu->dwarf_version = cu->header.version;
6532 }
6533 }
6534
6535 /* Skip dummy compilation units. */
6536 if (info_ptr >= begin_info_ptr + this_cu->length
6537 || peek_abbrev_code (abfd, info_ptr) == 0)
6538 {
6539 dummy_p = true;
6540 return;
6541 }
6542
6543 /* If we don't have them yet, read the abbrevs for this compilation unit.
6544 And if we need to read them now, make sure they're freed when we're
6545 done. */
6546 if (abbrev_table != NULL)
6547 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6548 else
6549 {
6550 abbrev_section->read (objfile);
6551 m_abbrev_table_holder
6552 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6553 abbrev_table = m_abbrev_table_holder.get ();
6554 }
6555
6556 /* Read the top level CU/TU die. */
6557 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6558 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6559
6560 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6561 {
6562 dummy_p = true;
6563 return;
6564 }
6565
6566 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6567 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6568 table from the DWO file and pass the ownership over to us. It will be
6569 referenced from READER, so we must make sure to free it after we're done
6570 with READER.
6571
6572 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6573 DWO CU, that this test will fail (the attribute will not be present). */
6574 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6575 if (dwo_name != nullptr)
6576 {
6577 struct dwo_unit *dwo_unit;
6578 struct die_info *dwo_comp_unit_die;
6579
6580 if (comp_unit_die->has_children)
6581 {
6582 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6583 " has children (offset %s) [in module %s]"),
6584 sect_offset_str (this_cu->sect_off),
6585 bfd_get_filename (abfd));
6586 }
6587 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6588 if (dwo_unit != NULL)
6589 {
6590 if (read_cutu_die_from_dwo (cu, dwo_unit,
6591 comp_unit_die, NULL,
6592 this, &info_ptr,
6593 &dwo_comp_unit_die,
6594 &m_dwo_abbrev_table) == 0)
6595 {
6596 /* Dummy die. */
6597 dummy_p = true;
6598 return;
6599 }
6600 comp_unit_die = dwo_comp_unit_die;
6601 }
6602 else
6603 {
6604 /* Yikes, we couldn't find the rest of the DIE, we only have
6605 the stub. A complaint has already been logged. There's
6606 not much more we can do except pass on the stub DIE to
6607 die_reader_func. We don't want to throw an error on bad
6608 debug info. */
6609 }
6610 }
6611 }
6612
6613 void
6614 cutu_reader::keep ()
6615 {
6616 /* Done, clean up. */
6617 gdb_assert (!dummy_p);
6618 if (m_new_cu != NULL)
6619 {
6620 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6621 now. */
6622 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6623 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6624 }
6625 }
6626
6627 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6628 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6629 assumed to have already done the lookup to find the DWO file).
6630
6631 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6632 THIS_CU->is_debug_types, but nothing else.
6633
6634 We fill in THIS_CU->length.
6635
6636 THIS_CU->cu is always freed when done.
6637 This is done in order to not leave THIS_CU->cu in a state where we have
6638 to care whether it refers to the "main" CU or the DWO CU.
6639
6640 When parent_cu is passed, it is used to provide a default value for
6641 str_offsets_base and addr_base from the parent. */
6642
6643 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6644 dwarf2_per_objfile *per_objfile,
6645 struct dwarf2_cu *parent_cu,
6646 struct dwo_file *dwo_file)
6647 : die_reader_specs {},
6648 m_this_cu (this_cu)
6649 {
6650 struct objfile *objfile = per_objfile->objfile;
6651 struct dwarf2_section_info *section = this_cu->section;
6652 bfd *abfd = section->get_bfd_owner ();
6653 struct dwarf2_section_info *abbrev_section;
6654 const gdb_byte *begin_info_ptr, *info_ptr;
6655
6656 if (dwarf_die_debug)
6657 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6658 this_cu->is_debug_types ? "type" : "comp",
6659 sect_offset_str (this_cu->sect_off));
6660
6661 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6662
6663 abbrev_section = (dwo_file != NULL
6664 ? &dwo_file->sections.abbrev
6665 : get_abbrev_section_for_cu (this_cu));
6666
6667 /* This is cheap if the section is already read in. */
6668 section->read (objfile);
6669
6670 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6671
6672 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6673 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6674 section, abbrev_section, info_ptr,
6675 (this_cu->is_debug_types
6676 ? rcuh_kind::TYPE
6677 : rcuh_kind::COMPILE));
6678
6679 if (parent_cu != nullptr)
6680 {
6681 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6682 m_new_cu->addr_base = parent_cu->addr_base;
6683 }
6684 this_cu->length = m_new_cu->header.get_length ();
6685
6686 /* Skip dummy compilation units. */
6687 if (info_ptr >= begin_info_ptr + this_cu->length
6688 || peek_abbrev_code (abfd, info_ptr) == 0)
6689 {
6690 dummy_p = true;
6691 return;
6692 }
6693
6694 abbrev_section->read (objfile);
6695 m_abbrev_table_holder
6696 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6697
6698 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6699 m_abbrev_table_holder.get ());
6700 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6701 }
6702
6703 \f
6704 /* Type Unit Groups.
6705
6706 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6707 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6708 so that all types coming from the same compilation (.o file) are grouped
6709 together. A future step could be to put the types in the same symtab as
6710 the CU the types ultimately came from. */
6711
6712 static hashval_t
6713 hash_type_unit_group (const void *item)
6714 {
6715 const struct type_unit_group *tu_group
6716 = (const struct type_unit_group *) item;
6717
6718 return hash_stmt_list_entry (&tu_group->hash);
6719 }
6720
6721 static int
6722 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6723 {
6724 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6725 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6726
6727 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6728 }
6729
6730 /* Allocate a hash table for type unit groups. */
6731
6732 static htab_up
6733 allocate_type_unit_groups_table ()
6734 {
6735 return htab_up (htab_create_alloc (3,
6736 hash_type_unit_group,
6737 eq_type_unit_group,
6738 htab_delete_entry<type_unit_group>,
6739 xcalloc, xfree));
6740 }
6741
6742 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6743 partial symtabs. We combine several TUs per psymtab to not let the size
6744 of any one psymtab grow too big. */
6745 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6746 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6747
6748 /* Helper routine for get_type_unit_group.
6749 Create the type_unit_group object used to hold one or more TUs. */
6750
6751 static std::unique_ptr<type_unit_group>
6752 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6753 {
6754 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6755 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6756
6757 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6758 tu_group->per_bfd = per_bfd;
6759
6760 if (per_bfd->using_index)
6761 {
6762 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6763 struct dwarf2_per_cu_quick_data);
6764 }
6765 else
6766 {
6767 unsigned int line_offset = to_underlying (line_offset_struct);
6768 dwarf2_psymtab *pst;
6769 std::string name;
6770
6771 /* Give the symtab a useful name for debug purposes. */
6772 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6773 name = string_printf ("<type_units_%d>",
6774 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6775 else
6776 name = string_printf ("<type_units_at_0x%x>", line_offset);
6777
6778 pst = create_partial_symtab (tu_group.get (), per_objfile,
6779 name.c_str ());
6780 pst->anonymous = true;
6781 }
6782
6783 tu_group->hash.dwo_unit = cu->dwo_unit;
6784 tu_group->hash.line_sect_off = line_offset_struct;
6785
6786 return tu_group;
6787 }
6788
6789 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6790 STMT_LIST is a DW_AT_stmt_list attribute. */
6791
6792 static struct type_unit_group *
6793 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6794 {
6795 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6796 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6797 struct type_unit_group *tu_group;
6798 void **slot;
6799 unsigned int line_offset;
6800 struct type_unit_group type_unit_group_for_lookup;
6801
6802 if (per_objfile->per_bfd->type_unit_groups == NULL)
6803 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6804
6805 /* Do we need to create a new group, or can we use an existing one? */
6806
6807 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6808 {
6809 line_offset = stmt_list->as_unsigned ();
6810 ++tu_stats->nr_symtab_sharers;
6811 }
6812 else
6813 {
6814 /* Ugh, no stmt_list. Rare, but we have to handle it.
6815 We can do various things here like create one group per TU or
6816 spread them over multiple groups to split up the expansion work.
6817 To avoid worst case scenarios (too many groups or too large groups)
6818 we, umm, group them in bunches. */
6819 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6820 | (tu_stats->nr_stmt_less_type_units
6821 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6822 ++tu_stats->nr_stmt_less_type_units;
6823 }
6824
6825 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6826 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6827 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6828 &type_unit_group_for_lookup, INSERT);
6829 if (*slot == nullptr)
6830 {
6831 sect_offset line_offset_struct = (sect_offset) line_offset;
6832 std::unique_ptr<type_unit_group> grp
6833 = create_type_unit_group (cu, line_offset_struct);
6834 *slot = grp.release ();
6835 ++tu_stats->nr_symtabs;
6836 }
6837
6838 tu_group = (struct type_unit_group *) *slot;
6839 gdb_assert (tu_group != nullptr);
6840 return tu_group;
6841 }
6842 \f
6843 /* Partial symbol tables. */
6844
6845 /* Create a psymtab named NAME and assign it to PER_CU.
6846
6847 The caller must fill in the following details:
6848 dirname, textlow, texthigh. */
6849
6850 static dwarf2_psymtab *
6851 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6852 dwarf2_per_objfile *per_objfile,
6853 const char *name)
6854 {
6855 dwarf2_psymtab *pst
6856 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6857 per_objfile->objfile->per_bfd, per_cu);
6858
6859 pst->psymtabs_addrmap_supported = true;
6860
6861 /* This is the glue that links PST into GDB's symbol API. */
6862 per_cu->v.psymtab = pst;
6863
6864 return pst;
6865 }
6866
6867 /* DIE reader function for process_psymtab_comp_unit. */
6868
6869 static void
6870 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6871 const gdb_byte *info_ptr,
6872 struct die_info *comp_unit_die,
6873 enum language pretend_language)
6874 {
6875 struct dwarf2_cu *cu = reader->cu;
6876 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6877 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6878 struct objfile *objfile = per_objfile->objfile;
6879 struct gdbarch *gdbarch = objfile->arch ();
6880 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6881 CORE_ADDR baseaddr;
6882 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6883 dwarf2_psymtab *pst;
6884 enum pc_bounds_kind cu_bounds_kind;
6885 const char *filename;
6886
6887 gdb_assert (! per_cu->is_debug_types);
6888
6889 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
6890
6891 /* Allocate a new partial symbol table structure. */
6892 gdb::unique_xmalloc_ptr<char> debug_filename;
6893 static const char artificial[] = "<artificial>";
6894 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6895 if (filename == NULL)
6896 filename = "";
6897 else if (strcmp (filename, artificial) == 0)
6898 {
6899 debug_filename.reset (concat (artificial, "@",
6900 sect_offset_str (per_cu->sect_off),
6901 (char *) NULL));
6902 filename = debug_filename.get ();
6903 }
6904
6905 pst = create_partial_symtab (per_cu, per_objfile, filename);
6906
6907 /* This must be done before calling dwarf2_build_include_psymtabs. */
6908 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6909
6910 baseaddr = objfile->text_section_offset ();
6911
6912 dwarf2_find_base_address (comp_unit_die, cu);
6913
6914 /* Possibly set the default values of LOWPC and HIGHPC from
6915 `DW_AT_ranges'. */
6916 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6917 &best_highpc, cu, pst);
6918 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6919 {
6920 CORE_ADDR low
6921 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
6922 - baseaddr);
6923 CORE_ADDR high
6924 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
6925 - baseaddr - 1);
6926 /* Store the contiguous range if it is not empty; it can be
6927 empty for CUs with no code. */
6928 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
6929 low, high, pst);
6930 }
6931
6932 /* Check if comp unit has_children.
6933 If so, read the rest of the partial symbols from this comp unit.
6934 If not, there's no more debug_info for this comp unit. */
6935 if (comp_unit_die->has_children)
6936 {
6937 struct partial_die_info *first_die;
6938 CORE_ADDR lowpc, highpc;
6939
6940 lowpc = ((CORE_ADDR) -1);
6941 highpc = ((CORE_ADDR) 0);
6942
6943 first_die = load_partial_dies (reader, info_ptr, 1);
6944
6945 scan_partial_symbols (first_die, &lowpc, &highpc,
6946 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6947
6948 /* If we didn't find a lowpc, set it to highpc to avoid
6949 complaints from `maint check'. */
6950 if (lowpc == ((CORE_ADDR) -1))
6951 lowpc = highpc;
6952
6953 /* If the compilation unit didn't have an explicit address range,
6954 then use the information extracted from its child dies. */
6955 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6956 {
6957 best_lowpc = lowpc;
6958 best_highpc = highpc;
6959 }
6960 }
6961 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
6962 best_lowpc + baseaddr)
6963 - baseaddr);
6964 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
6965 best_highpc + baseaddr)
6966 - baseaddr);
6967
6968 pst->end ();
6969
6970 if (!cu->per_cu->imported_symtabs_empty ())
6971 {
6972 int i;
6973 int len = cu->per_cu->imported_symtabs_size ();
6974
6975 /* Fill in 'dependencies' here; we fill in 'users' in a
6976 post-pass. */
6977 pst->number_of_dependencies = len;
6978 pst->dependencies
6979 = per_bfd->partial_symtabs->allocate_dependencies (len);
6980 for (i = 0; i < len; ++i)
6981 {
6982 pst->dependencies[i]
6983 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
6984 }
6985
6986 cu->per_cu->imported_symtabs_free ();
6987 }
6988
6989 /* Get the list of files included in the current compilation unit,
6990 and build a psymtab for each of them. */
6991 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6992
6993 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
6994 ", %d global, %d static syms",
6995 per_cu->is_debug_types ? "type" : "comp",
6996 sect_offset_str (per_cu->sect_off),
6997 paddress (gdbarch, pst->text_low (objfile)),
6998 paddress (gdbarch, pst->text_high (objfile)),
6999 (int) pst->global_psymbols.size (),
7000 (int) pst->static_psymbols.size ());
7001 }
7002
7003 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7004 Process compilation unit THIS_CU for a psymtab. */
7005
7006 static void
7007 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7008 dwarf2_per_objfile *per_objfile,
7009 bool want_partial_unit,
7010 enum language pretend_language)
7011 {
7012 /* If this compilation unit was already read in, free the
7013 cached copy in order to read it in again. This is
7014 necessary because we skipped some symbols when we first
7015 read in the compilation unit (see load_partial_dies).
7016 This problem could be avoided, but the benefit is unclear. */
7017 per_objfile->remove_cu (this_cu);
7018
7019 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7020
7021 if (reader.comp_unit_die == nullptr)
7022 return;
7023
7024 switch (reader.comp_unit_die->tag)
7025 {
7026 case DW_TAG_compile_unit:
7027 this_cu->unit_type = DW_UT_compile;
7028 break;
7029 case DW_TAG_partial_unit:
7030 this_cu->unit_type = DW_UT_partial;
7031 break;
7032 case DW_TAG_type_unit:
7033 this_cu->unit_type = DW_UT_type;
7034 break;
7035 default:
7036 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7037 dwarf_tag_name (reader.comp_unit_die->tag),
7038 sect_offset_str (reader.cu->per_cu->sect_off),
7039 objfile_name (per_objfile->objfile));
7040 }
7041
7042 if (reader.dummy_p)
7043 {
7044 /* Nothing. */
7045 }
7046 else if (this_cu->is_debug_types)
7047 build_type_psymtabs_reader (&reader, reader.info_ptr,
7048 reader.comp_unit_die);
7049 else if (want_partial_unit
7050 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7051 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7052 reader.comp_unit_die,
7053 pretend_language);
7054
7055 this_cu->lang = reader.cu->language;
7056
7057 /* Age out any secondary CUs. */
7058 per_objfile->age_comp_units ();
7059 }
7060
7061 /* Reader function for build_type_psymtabs. */
7062
7063 static void
7064 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7065 const gdb_byte *info_ptr,
7066 struct die_info *type_unit_die)
7067 {
7068 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7069 struct dwarf2_cu *cu = reader->cu;
7070 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7071 struct signatured_type *sig_type;
7072 struct type_unit_group *tu_group;
7073 struct attribute *attr;
7074 struct partial_die_info *first_die;
7075 CORE_ADDR lowpc, highpc;
7076 dwarf2_psymtab *pst;
7077
7078 gdb_assert (per_cu->is_debug_types);
7079 sig_type = (struct signatured_type *) per_cu;
7080
7081 if (! type_unit_die->has_children)
7082 return;
7083
7084 attr = type_unit_die->attr (DW_AT_stmt_list);
7085 tu_group = get_type_unit_group (cu, attr);
7086
7087 if (tu_group->tus == nullptr)
7088 tu_group->tus = new std::vector<signatured_type *>;
7089 tu_group->tus->push_back (sig_type);
7090
7091 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7092 pst = create_partial_symtab (per_cu, per_objfile, "");
7093 pst->anonymous = true;
7094
7095 first_die = load_partial_dies (reader, info_ptr, 1);
7096
7097 lowpc = (CORE_ADDR) -1;
7098 highpc = (CORE_ADDR) 0;
7099 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7100
7101 pst->end ();
7102 }
7103
7104 /* Struct used to sort TUs by their abbreviation table offset. */
7105
7106 struct tu_abbrev_offset
7107 {
7108 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7109 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7110 {}
7111
7112 /* This is used when sorting. */
7113 bool operator< (const tu_abbrev_offset &other) const
7114 {
7115 return abbrev_offset < other.abbrev_offset;
7116 }
7117
7118 signatured_type *sig_type;
7119 sect_offset abbrev_offset;
7120 };
7121
7122 /* Efficiently read all the type units.
7123
7124 The efficiency is because we sort TUs by the abbrev table they use and
7125 only read each abbrev table once. In one program there are 200K TUs
7126 sharing 8K abbrev tables.
7127
7128 The main purpose of this function is to support building the
7129 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7130 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7131 can collapse the search space by grouping them by stmt_list.
7132 The savings can be significant, in the same program from above the 200K TUs
7133 share 8K stmt_list tables.
7134
7135 FUNC is expected to call get_type_unit_group, which will create the
7136 struct type_unit_group if necessary and add it to
7137 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7138
7139 static void
7140 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7141 {
7142 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7143 abbrev_table_up abbrev_table;
7144 sect_offset abbrev_offset;
7145
7146 /* It's up to the caller to not call us multiple times. */
7147 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7148
7149 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7150 return;
7151
7152 /* TUs typically share abbrev tables, and there can be way more TUs than
7153 abbrev tables. Sort by abbrev table to reduce the number of times we
7154 read each abbrev table in.
7155 Alternatives are to punt or to maintain a cache of abbrev tables.
7156 This is simpler and efficient enough for now.
7157
7158 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7159 symtab to use). Typically TUs with the same abbrev offset have the same
7160 stmt_list value too so in practice this should work well.
7161
7162 The basic algorithm here is:
7163
7164 sort TUs by abbrev table
7165 for each TU with same abbrev table:
7166 read abbrev table if first user
7167 read TU top level DIE
7168 [IWBN if DWO skeletons had DW_AT_stmt_list]
7169 call FUNC */
7170
7171 dwarf_read_debug_printf ("Building type unit groups ...");
7172
7173 /* Sort in a separate table to maintain the order of all_comp_units
7174 for .gdb_index: TU indices directly index all_type_units. */
7175 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7176 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7177
7178 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7179 {
7180 if (cu->is_debug_types)
7181 {
7182 auto sig_type = static_cast<signatured_type *> (cu.get ());
7183 sorted_by_abbrev.emplace_back
7184 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7185 sig_type->sect_off));
7186 }
7187 }
7188
7189 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7190
7191 abbrev_offset = (sect_offset) ~(unsigned) 0;
7192
7193 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7194 {
7195 /* Switch to the next abbrev table if necessary. */
7196 if (abbrev_table == NULL
7197 || tu.abbrev_offset != abbrev_offset)
7198 {
7199 abbrev_offset = tu.abbrev_offset;
7200 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7201 abbrev_table =
7202 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7203 ++tu_stats->nr_uniq_abbrev_tables;
7204 }
7205
7206 cutu_reader reader (tu.sig_type, per_objfile,
7207 abbrev_table.get (), nullptr, false);
7208 if (!reader.dummy_p)
7209 build_type_psymtabs_reader (&reader, reader.info_ptr,
7210 reader.comp_unit_die);
7211 }
7212 }
7213
7214 /* Print collected type unit statistics. */
7215
7216 static void
7217 print_tu_stats (dwarf2_per_objfile *per_objfile)
7218 {
7219 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7220
7221 dwarf_read_debug_printf ("Type unit statistics:");
7222 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7223 dwarf_read_debug_printf (" %d uniq abbrev tables",
7224 tu_stats->nr_uniq_abbrev_tables);
7225 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7226 tu_stats->nr_symtabs);
7227 dwarf_read_debug_printf (" %d symtab sharers",
7228 tu_stats->nr_symtab_sharers);
7229 dwarf_read_debug_printf (" %d type units without a stmt_list",
7230 tu_stats->nr_stmt_less_type_units);
7231 dwarf_read_debug_printf (" %d all_type_units reallocs",
7232 tu_stats->nr_all_type_units_reallocs);
7233 }
7234
7235 /* Traversal function for build_type_psymtabs. */
7236
7237 static int
7238 build_type_psymtab_dependencies (void **slot, void *info)
7239 {
7240 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7241 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7242 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7243 dwarf2_psymtab *pst = tu_group->v.psymtab;
7244 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7245 int i;
7246
7247 gdb_assert (len > 0);
7248 gdb_assert (tu_group->type_unit_group_p ());
7249
7250 pst->number_of_dependencies = len;
7251 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7252 for (i = 0; i < len; ++i)
7253 {
7254 struct signatured_type *iter = tu_group->tus->at (i);
7255 gdb_assert (iter->is_debug_types);
7256 pst->dependencies[i] = iter->v.psymtab;
7257 iter->type_unit_group = tu_group;
7258 }
7259
7260 delete tu_group->tus;
7261 tu_group->tus = nullptr;
7262
7263 return 1;
7264 }
7265
7266 /* Traversal function for process_skeletonless_type_unit.
7267 Read a TU in a DWO file and build partial symbols for it. */
7268
7269 static int
7270 process_skeletonless_type_unit (void **slot, void *info)
7271 {
7272 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7273 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7274 struct signatured_type find_entry, *entry;
7275
7276 /* If this TU doesn't exist in the global table, add it and read it in. */
7277
7278 if (per_objfile->per_bfd->signatured_types == NULL)
7279 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7280
7281 find_entry.signature = dwo_unit->signature;
7282 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7283 &find_entry, INSERT);
7284 /* If we've already seen this type there's nothing to do. What's happening
7285 is we're doing our own version of comdat-folding here. */
7286 if (*slot != NULL)
7287 return 1;
7288
7289 /* This does the job that create_all_comp_units would have done for
7290 this TU. */
7291 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7292 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7293 *slot = entry;
7294
7295 /* This does the job that build_type_psymtabs would have done. */
7296 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7297 if (!reader.dummy_p)
7298 build_type_psymtabs_reader (&reader, reader.info_ptr,
7299 reader.comp_unit_die);
7300
7301 return 1;
7302 }
7303
7304 /* Traversal function for process_skeletonless_type_units. */
7305
7306 static int
7307 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7308 {
7309 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7310
7311 if (dwo_file->tus != NULL)
7312 htab_traverse_noresize (dwo_file->tus.get (),
7313 process_skeletonless_type_unit, info);
7314
7315 return 1;
7316 }
7317
7318 /* Scan all TUs of DWO files, verifying we've processed them.
7319 This is needed in case a TU was emitted without its skeleton.
7320 Note: This can't be done until we know what all the DWO files are. */
7321
7322 static void
7323 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7324 {
7325 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7326 if (get_dwp_file (per_objfile) == NULL
7327 && per_objfile->per_bfd->dwo_files != NULL)
7328 {
7329 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7330 process_dwo_file_for_skeletonless_type_units,
7331 per_objfile);
7332 }
7333 }
7334
7335 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7336
7337 static void
7338 set_partial_user (dwarf2_per_objfile *per_objfile)
7339 {
7340 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7341 {
7342 dwarf2_psymtab *pst = per_cu->v.psymtab;
7343
7344 if (pst == NULL)
7345 continue;
7346
7347 for (int j = 0; j < pst->number_of_dependencies; ++j)
7348 {
7349 /* Set the 'user' field only if it is not already set. */
7350 if (pst->dependencies[j]->user == NULL)
7351 pst->dependencies[j]->user = pst;
7352 }
7353 }
7354 }
7355
7356 /* Build the partial symbol table by doing a quick pass through the
7357 .debug_info and .debug_abbrev sections. */
7358
7359 static void
7360 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7361 {
7362 struct objfile *objfile = per_objfile->objfile;
7363 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7364
7365 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7366 objfile_name (objfile));
7367
7368 scoped_restore restore_reading_psyms
7369 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7370
7371 per_bfd->info.read (objfile);
7372
7373 /* Any cached compilation units will be linked by the per-objfile
7374 read_in_chain. Make sure to free them when we're done. */
7375 free_cached_comp_units freer (per_objfile);
7376
7377 create_all_comp_units (per_objfile);
7378 build_type_psymtabs (per_objfile);
7379
7380 /* Create a temporary address map on a temporary obstack. We later
7381 copy this to the final obstack. */
7382 auto_obstack temp_obstack;
7383
7384 scoped_restore save_psymtabs_addrmap
7385 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7386 addrmap_create_mutable (&temp_obstack));
7387
7388 for (const auto &per_cu : per_bfd->all_comp_units)
7389 {
7390 if (per_cu->v.psymtab != NULL)
7391 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7392 continue;
7393 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7394 language_minimal);
7395 }
7396
7397 /* This has to wait until we read the CUs, we need the list of DWOs. */
7398 process_skeletonless_type_units (per_objfile);
7399
7400 /* Now that all TUs have been processed we can fill in the dependencies. */
7401 if (per_bfd->type_unit_groups != NULL)
7402 {
7403 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7404 build_type_psymtab_dependencies, per_objfile);
7405 }
7406
7407 if (dwarf_read_debug > 0)
7408 print_tu_stats (per_objfile);
7409
7410 set_partial_user (per_objfile);
7411
7412 per_bfd->partial_symtabs->psymtabs_addrmap
7413 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7414 per_bfd->partial_symtabs->obstack ());
7415 /* At this point we want to keep the address map. */
7416 save_psymtabs_addrmap.release ();
7417
7418 dwarf_read_debug_printf ("Done building psymtabs of %s",
7419 objfile_name (objfile));
7420 }
7421
7422 /* Load the partial DIEs for a secondary CU into memory.
7423 This is also used when rereading a primary CU with load_all_dies. */
7424
7425 static void
7426 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7427 dwarf2_per_objfile *per_objfile,
7428 dwarf2_cu *existing_cu)
7429 {
7430 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7431
7432 if (!reader.dummy_p)
7433 {
7434 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7435 language_minimal);
7436
7437 /* Check if comp unit has_children.
7438 If so, read the rest of the partial symbols from this comp unit.
7439 If not, there's no more debug_info for this comp unit. */
7440 if (reader.comp_unit_die->has_children)
7441 load_partial_dies (&reader, reader.info_ptr, 0);
7442
7443 reader.keep ();
7444 }
7445 }
7446
7447 static void
7448 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7449 struct dwarf2_section_info *section,
7450 struct dwarf2_section_info *abbrev_section,
7451 unsigned int is_dwz,
7452 htab_up &types_htab,
7453 rcuh_kind section_kind)
7454 {
7455 const gdb_byte *info_ptr;
7456 struct objfile *objfile = per_objfile->objfile;
7457
7458 dwarf_read_debug_printf ("Reading %s for %s",
7459 section->get_name (),
7460 section->get_file_name ());
7461
7462 section->read (objfile);
7463
7464 info_ptr = section->buffer;
7465
7466 while (info_ptr < section->buffer + section->size)
7467 {
7468 dwarf2_per_cu_data_up this_cu;
7469
7470 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7471
7472 comp_unit_head cu_header;
7473 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7474 abbrev_section, info_ptr,
7475 section_kind);
7476
7477 /* Save the compilation unit for later lookup. */
7478 if (cu_header.unit_type != DW_UT_type)
7479 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7480 else
7481 {
7482 if (types_htab == nullptr)
7483 types_htab = allocate_signatured_type_table ();
7484
7485 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
7486 signatured_type *sig_ptr = sig_type.get ();
7487 sig_type->signature = cu_header.signature;
7488 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7489 this_cu.reset (sig_type.release ());
7490
7491 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7492 gdb_assert (slot != nullptr);
7493 if (*slot != nullptr)
7494 complaint (_("debug type entry at offset %s is duplicate to"
7495 " the entry at offset %s, signature %s"),
7496 sect_offset_str (sect_off),
7497 sect_offset_str (sig_ptr->sect_off),
7498 hex_string (sig_ptr->signature));
7499 *slot = sig_ptr;
7500 }
7501 this_cu->sect_off = sect_off;
7502 this_cu->length = cu_header.length + cu_header.initial_length_size;
7503 this_cu->is_dwz = is_dwz;
7504 this_cu->section = section;
7505
7506 info_ptr = info_ptr + this_cu->length;
7507 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7508 }
7509 }
7510
7511 /* Create a list of all compilation units in OBJFILE.
7512 This is only done for -readnow and building partial symtabs. */
7513
7514 static void
7515 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7516 {
7517 htab_up types_htab;
7518
7519 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7520 &per_objfile->per_bfd->abbrev, 0,
7521 types_htab, rcuh_kind::COMPILE);
7522 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7523 read_comp_units_from_section (per_objfile, &section,
7524 &per_objfile->per_bfd->abbrev, 0,
7525 types_htab, rcuh_kind::TYPE);
7526
7527 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7528 if (dwz != NULL)
7529 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7530 types_htab, rcuh_kind::COMPILE);
7531
7532 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7533 }
7534
7535 /* Process all loaded DIEs for compilation unit CU, starting at
7536 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7537 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7538 DW_AT_ranges). See the comments of add_partial_subprogram on how
7539 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7540
7541 static void
7542 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7543 CORE_ADDR *highpc, int set_addrmap,
7544 struct dwarf2_cu *cu)
7545 {
7546 struct partial_die_info *pdi;
7547
7548 /* Now, march along the PDI's, descending into ones which have
7549 interesting children but skipping the children of the other ones,
7550 until we reach the end of the compilation unit. */
7551
7552 pdi = first_die;
7553
7554 while (pdi != NULL)
7555 {
7556 pdi->fixup (cu);
7557
7558 /* Anonymous namespaces or modules have no name but have interesting
7559 children, so we need to look at them. Ditto for anonymous
7560 enums. */
7561
7562 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7563 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7564 || pdi->tag == DW_TAG_imported_unit
7565 || pdi->tag == DW_TAG_inlined_subroutine)
7566 {
7567 switch (pdi->tag)
7568 {
7569 case DW_TAG_subprogram:
7570 case DW_TAG_inlined_subroutine:
7571 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7572 if (cu->language == language_cplus)
7573 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7574 set_addrmap, cu);
7575 break;
7576 case DW_TAG_constant:
7577 case DW_TAG_variable:
7578 case DW_TAG_typedef:
7579 case DW_TAG_union_type:
7580 if (!pdi->is_declaration
7581 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7582 {
7583 add_partial_symbol (pdi, cu);
7584 }
7585 break;
7586 case DW_TAG_class_type:
7587 case DW_TAG_interface_type:
7588 case DW_TAG_structure_type:
7589 if (!pdi->is_declaration)
7590 {
7591 add_partial_symbol (pdi, cu);
7592 }
7593 if ((cu->language == language_rust
7594 || cu->language == language_cplus) && pdi->has_children)
7595 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7596 set_addrmap, cu);
7597 break;
7598 case DW_TAG_enumeration_type:
7599 if (!pdi->is_declaration)
7600 add_partial_enumeration (pdi, cu);
7601 break;
7602 case DW_TAG_base_type:
7603 case DW_TAG_subrange_type:
7604 /* File scope base type definitions are added to the partial
7605 symbol table. */
7606 add_partial_symbol (pdi, cu);
7607 break;
7608 case DW_TAG_namespace:
7609 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7610 break;
7611 case DW_TAG_module:
7612 if (!pdi->is_declaration)
7613 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7614 break;
7615 case DW_TAG_imported_unit:
7616 {
7617 struct dwarf2_per_cu_data *per_cu;
7618
7619 /* For now we don't handle imported units in type units. */
7620 if (cu->per_cu->is_debug_types)
7621 {
7622 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7623 " supported in type units [in module %s]"),
7624 objfile_name (cu->per_objfile->objfile));
7625 }
7626
7627 per_cu = dwarf2_find_containing_comp_unit
7628 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
7629
7630 /* Go read the partial unit, if needed. */
7631 if (per_cu->v.psymtab == NULL)
7632 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7633 cu->language);
7634
7635 cu->per_cu->imported_symtabs_push (per_cu);
7636 }
7637 break;
7638 case DW_TAG_imported_declaration:
7639 add_partial_symbol (pdi, cu);
7640 break;
7641 default:
7642 break;
7643 }
7644 }
7645
7646 /* If the die has a sibling, skip to the sibling. */
7647
7648 pdi = pdi->die_sibling;
7649 }
7650 }
7651
7652 /* Functions used to compute the fully scoped name of a partial DIE.
7653
7654 Normally, this is simple. For C++, the parent DIE's fully scoped
7655 name is concatenated with "::" and the partial DIE's name.
7656 Enumerators are an exception; they use the scope of their parent
7657 enumeration type, i.e. the name of the enumeration type is not
7658 prepended to the enumerator.
7659
7660 There are two complexities. One is DW_AT_specification; in this
7661 case "parent" means the parent of the target of the specification,
7662 instead of the direct parent of the DIE. The other is compilers
7663 which do not emit DW_TAG_namespace; in this case we try to guess
7664 the fully qualified name of structure types from their members'
7665 linkage names. This must be done using the DIE's children rather
7666 than the children of any DW_AT_specification target. We only need
7667 to do this for structures at the top level, i.e. if the target of
7668 any DW_AT_specification (if any; otherwise the DIE itself) does not
7669 have a parent. */
7670
7671 /* Compute the scope prefix associated with PDI's parent, in
7672 compilation unit CU. The result will be allocated on CU's
7673 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7674 field. NULL is returned if no prefix is necessary. */
7675 static const char *
7676 partial_die_parent_scope (struct partial_die_info *pdi,
7677 struct dwarf2_cu *cu)
7678 {
7679 const char *grandparent_scope;
7680 struct partial_die_info *parent, *real_pdi;
7681
7682 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7683 then this means the parent of the specification DIE. */
7684
7685 real_pdi = pdi;
7686 while (real_pdi->has_specification)
7687 {
7688 auto res = find_partial_die (real_pdi->spec_offset,
7689 real_pdi->spec_is_dwz, cu);
7690 real_pdi = res.pdi;
7691 cu = res.cu;
7692 }
7693
7694 parent = real_pdi->die_parent;
7695 if (parent == NULL)
7696 return NULL;
7697
7698 if (parent->scope_set)
7699 return parent->scope;
7700
7701 parent->fixup (cu);
7702
7703 grandparent_scope = partial_die_parent_scope (parent, cu);
7704
7705 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7706 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7707 Work around this problem here. */
7708 if (cu->language == language_cplus
7709 && parent->tag == DW_TAG_namespace
7710 && strcmp (parent->name (cu), "::") == 0
7711 && grandparent_scope == NULL)
7712 {
7713 parent->scope = NULL;
7714 parent->scope_set = 1;
7715 return NULL;
7716 }
7717
7718 /* Nested subroutines in Fortran get a prefix. */
7719 if (pdi->tag == DW_TAG_enumerator)
7720 /* Enumerators should not get the name of the enumeration as a prefix. */
7721 parent->scope = grandparent_scope;
7722 else if (parent->tag == DW_TAG_namespace
7723 || parent->tag == DW_TAG_module
7724 || parent->tag == DW_TAG_structure_type
7725 || parent->tag == DW_TAG_class_type
7726 || parent->tag == DW_TAG_interface_type
7727 || parent->tag == DW_TAG_union_type
7728 || parent->tag == DW_TAG_enumeration_type
7729 || (cu->language == language_fortran
7730 && parent->tag == DW_TAG_subprogram
7731 && pdi->tag == DW_TAG_subprogram))
7732 {
7733 if (grandparent_scope == NULL)
7734 parent->scope = parent->name (cu);
7735 else
7736 parent->scope = typename_concat (&cu->comp_unit_obstack,
7737 grandparent_scope,
7738 parent->name (cu), 0, cu);
7739 }
7740 else
7741 {
7742 /* FIXME drow/2004-04-01: What should we be doing with
7743 function-local names? For partial symbols, we should probably be
7744 ignoring them. */
7745 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7746 dwarf_tag_name (parent->tag),
7747 sect_offset_str (pdi->sect_off));
7748 parent->scope = grandparent_scope;
7749 }
7750
7751 parent->scope_set = 1;
7752 return parent->scope;
7753 }
7754
7755 /* Return the fully scoped name associated with PDI, from compilation unit
7756 CU. The result will be allocated with malloc. */
7757
7758 static gdb::unique_xmalloc_ptr<char>
7759 partial_die_full_name (struct partial_die_info *pdi,
7760 struct dwarf2_cu *cu)
7761 {
7762 const char *parent_scope;
7763
7764 /* If this is a template instantiation, we can not work out the
7765 template arguments from partial DIEs. So, unfortunately, we have
7766 to go through the full DIEs. At least any work we do building
7767 types here will be reused if full symbols are loaded later. */
7768 if (pdi->has_template_arguments)
7769 {
7770 pdi->fixup (cu);
7771
7772 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7773 {
7774 struct die_info *die;
7775 struct attribute attr;
7776 struct dwarf2_cu *ref_cu = cu;
7777
7778 /* DW_FORM_ref_addr is using section offset. */
7779 attr.name = (enum dwarf_attribute) 0;
7780 attr.form = DW_FORM_ref_addr;
7781 attr.u.unsnd = to_underlying (pdi->sect_off);
7782 die = follow_die_ref (NULL, &attr, &ref_cu);
7783
7784 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7785 }
7786 }
7787
7788 parent_scope = partial_die_parent_scope (pdi, cu);
7789 if (parent_scope == NULL)
7790 return NULL;
7791 else
7792 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7793 pdi->name (cu),
7794 0, cu));
7795 }
7796
7797 static void
7798 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7799 {
7800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7801 struct objfile *objfile = per_objfile->objfile;
7802 struct gdbarch *gdbarch = objfile->arch ();
7803 CORE_ADDR addr = 0;
7804 const char *actual_name = NULL;
7805 CORE_ADDR baseaddr;
7806
7807 baseaddr = objfile->text_section_offset ();
7808
7809 gdb::unique_xmalloc_ptr<char> built_actual_name
7810 = partial_die_full_name (pdi, cu);
7811 if (built_actual_name != NULL)
7812 actual_name = built_actual_name.get ();
7813
7814 if (actual_name == NULL)
7815 actual_name = pdi->name (cu);
7816
7817 partial_symbol psymbol;
7818 memset (&psymbol, 0, sizeof (psymbol));
7819 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
7820 psymbol.ginfo.set_section_index (-1);
7821
7822 /* The code below indicates that the psymbol should be installed by
7823 setting this. */
7824 gdb::optional<psymbol_placement> where;
7825
7826 switch (pdi->tag)
7827 {
7828 case DW_TAG_inlined_subroutine:
7829 case DW_TAG_subprogram:
7830 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7831 - baseaddr);
7832 if (pdi->is_external
7833 || cu->language == language_ada
7834 || (cu->language == language_fortran
7835 && pdi->die_parent != NULL
7836 && pdi->die_parent->tag == DW_TAG_subprogram))
7837 {
7838 /* Normally, only "external" DIEs are part of the global scope.
7839 But in Ada and Fortran, we want to be able to access nested
7840 procedures globally. So all Ada and Fortran subprograms are
7841 stored in the global scope. */
7842 where = psymbol_placement::GLOBAL;
7843 }
7844 else
7845 where = psymbol_placement::STATIC;
7846
7847 psymbol.domain = VAR_DOMAIN;
7848 psymbol.aclass = LOC_BLOCK;
7849 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7850 psymbol.ginfo.value.address = addr;
7851
7852 if (pdi->main_subprogram && actual_name != NULL)
7853 set_objfile_main_name (objfile, actual_name, cu->language);
7854 break;
7855 case DW_TAG_constant:
7856 psymbol.domain = VAR_DOMAIN;
7857 psymbol.aclass = LOC_STATIC;
7858 where = (pdi->is_external
7859 ? psymbol_placement::GLOBAL
7860 : psymbol_placement::STATIC);
7861 break;
7862 case DW_TAG_variable:
7863 if (pdi->d.locdesc)
7864 addr = decode_locdesc (pdi->d.locdesc, cu);
7865
7866 if (pdi->d.locdesc
7867 && addr == 0
7868 && !per_objfile->per_bfd->has_section_at_zero)
7869 {
7870 /* A global or static variable may also have been stripped
7871 out by the linker if unused, in which case its address
7872 will be nullified; do not add such variables into partial
7873 symbol table then. */
7874 }
7875 else if (pdi->is_external)
7876 {
7877 /* Global Variable.
7878 Don't enter into the minimal symbol tables as there is
7879 a minimal symbol table entry from the ELF symbols already.
7880 Enter into partial symbol table if it has a location
7881 descriptor or a type.
7882 If the location descriptor is missing, new_symbol will create
7883 a LOC_UNRESOLVED symbol, the address of the variable will then
7884 be determined from the minimal symbol table whenever the variable
7885 is referenced.
7886 The address for the partial symbol table entry is not
7887 used by GDB, but it comes in handy for debugging partial symbol
7888 table building. */
7889
7890 if (pdi->d.locdesc || pdi->has_type)
7891 {
7892 psymbol.domain = VAR_DOMAIN;
7893 psymbol.aclass = LOC_STATIC;
7894 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7895 psymbol.ginfo.value.address = addr;
7896 where = psymbol_placement::GLOBAL;
7897 }
7898 }
7899 else
7900 {
7901 int has_loc = pdi->d.locdesc != NULL;
7902
7903 /* Static Variable. Skip symbols whose value we cannot know (those
7904 without location descriptors or constant values). */
7905 if (!has_loc && !pdi->has_const_value)
7906 return;
7907
7908 psymbol.domain = VAR_DOMAIN;
7909 psymbol.aclass = LOC_STATIC;
7910 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7911 if (has_loc)
7912 psymbol.ginfo.value.address = addr;
7913 where = psymbol_placement::STATIC;
7914 }
7915 break;
7916 case DW_TAG_array_type:
7917 case DW_TAG_typedef:
7918 case DW_TAG_base_type:
7919 case DW_TAG_subrange_type:
7920 psymbol.domain = VAR_DOMAIN;
7921 psymbol.aclass = LOC_TYPEDEF;
7922 where = psymbol_placement::STATIC;
7923 break;
7924 case DW_TAG_imported_declaration:
7925 case DW_TAG_namespace:
7926 psymbol.domain = VAR_DOMAIN;
7927 psymbol.aclass = LOC_TYPEDEF;
7928 where = psymbol_placement::GLOBAL;
7929 break;
7930 case DW_TAG_module:
7931 /* With Fortran 77 there might be a "BLOCK DATA" module
7932 available without any name. If so, we skip the module as it
7933 doesn't bring any value. */
7934 if (actual_name != nullptr)
7935 {
7936 psymbol.domain = MODULE_DOMAIN;
7937 psymbol.aclass = LOC_TYPEDEF;
7938 where = psymbol_placement::GLOBAL;
7939 }
7940 break;
7941 case DW_TAG_class_type:
7942 case DW_TAG_interface_type:
7943 case DW_TAG_structure_type:
7944 case DW_TAG_union_type:
7945 case DW_TAG_enumeration_type:
7946 /* Skip external references. The DWARF standard says in the section
7947 about "Structure, Union, and Class Type Entries": "An incomplete
7948 structure, union or class type is represented by a structure,
7949 union or class entry that does not have a byte size attribute
7950 and that has a DW_AT_declaration attribute." */
7951 if (!pdi->has_byte_size && pdi->is_declaration)
7952 return;
7953
7954 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7955 static vs. global. */
7956 psymbol.domain = STRUCT_DOMAIN;
7957 psymbol.aclass = LOC_TYPEDEF;
7958 where = (cu->language == language_cplus
7959 ? psymbol_placement::GLOBAL
7960 : psymbol_placement::STATIC);
7961 break;
7962 case DW_TAG_enumerator:
7963 psymbol.domain = VAR_DOMAIN;
7964 psymbol.aclass = LOC_CONST;
7965 where = (cu->language == language_cplus
7966 ? psymbol_placement::GLOBAL
7967 : psymbol_placement::STATIC);
7968 break;
7969 default:
7970 break;
7971 }
7972
7973 if (where.has_value ())
7974 {
7975 if (built_actual_name != nullptr)
7976 actual_name = objfile->intern (actual_name);
7977 if (pdi->linkage_name == nullptr || cu->language == language_ada)
7978 psymbol.ginfo.set_linkage_name (actual_name);
7979 else
7980 {
7981 psymbol.ginfo.set_demangled_name (actual_name,
7982 &objfile->objfile_obstack);
7983 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
7984 }
7985 cu->per_cu->v.psymtab->add_psymbol
7986 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
7987 objfile);
7988 }
7989 }
7990
7991 /* Read a partial die corresponding to a namespace; also, add a symbol
7992 corresponding to that namespace to the symbol table. NAMESPACE is
7993 the name of the enclosing namespace. */
7994
7995 static void
7996 add_partial_namespace (struct partial_die_info *pdi,
7997 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7998 int set_addrmap, struct dwarf2_cu *cu)
7999 {
8000 /* Add a symbol for the namespace. */
8001
8002 add_partial_symbol (pdi, cu);
8003
8004 /* Now scan partial symbols in that namespace. */
8005
8006 if (pdi->has_children)
8007 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8008 }
8009
8010 /* Read a partial die corresponding to a Fortran module. */
8011
8012 static void
8013 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8014 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8015 {
8016 /* Add a symbol for the namespace. */
8017
8018 add_partial_symbol (pdi, cu);
8019
8020 /* Now scan partial symbols in that module. */
8021
8022 if (pdi->has_children)
8023 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8024 }
8025
8026 /* Read a partial die corresponding to a subprogram or an inlined
8027 subprogram and create a partial symbol for that subprogram.
8028 When the CU language allows it, this routine also defines a partial
8029 symbol for each nested subprogram that this subprogram contains.
8030 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8031 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8032
8033 PDI may also be a lexical block, in which case we simply search
8034 recursively for subprograms defined inside that lexical block.
8035 Again, this is only performed when the CU language allows this
8036 type of definitions. */
8037
8038 static void
8039 add_partial_subprogram (struct partial_die_info *pdi,
8040 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8041 int set_addrmap, struct dwarf2_cu *cu)
8042 {
8043 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8044 {
8045 if (pdi->has_pc_info)
8046 {
8047 if (pdi->lowpc < *lowpc)
8048 *lowpc = pdi->lowpc;
8049 if (pdi->highpc > *highpc)
8050 *highpc = pdi->highpc;
8051 if (set_addrmap)
8052 {
8053 struct objfile *objfile = cu->per_objfile->objfile;
8054 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8055 struct gdbarch *gdbarch = objfile->arch ();
8056 CORE_ADDR baseaddr;
8057 CORE_ADDR this_highpc;
8058 CORE_ADDR this_lowpc;
8059
8060 baseaddr = objfile->text_section_offset ();
8061 this_lowpc
8062 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8063 pdi->lowpc + baseaddr)
8064 - baseaddr);
8065 this_highpc
8066 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8067 pdi->highpc + baseaddr)
8068 - baseaddr);
8069 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8070 this_lowpc, this_highpc - 1,
8071 cu->per_cu->v.psymtab);
8072 }
8073 }
8074
8075 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8076 {
8077 if (!pdi->is_declaration)
8078 /* Ignore subprogram DIEs that do not have a name, they are
8079 illegal. Do not emit a complaint at this point, we will
8080 do so when we convert this psymtab into a symtab. */
8081 if (pdi->name (cu))
8082 add_partial_symbol (pdi, cu);
8083 }
8084 }
8085
8086 if (! pdi->has_children)
8087 return;
8088
8089 if (cu->language == language_ada || cu->language == language_fortran)
8090 {
8091 pdi = pdi->die_child;
8092 while (pdi != NULL)
8093 {
8094 pdi->fixup (cu);
8095 if (pdi->tag == DW_TAG_subprogram
8096 || pdi->tag == DW_TAG_inlined_subroutine
8097 || pdi->tag == DW_TAG_lexical_block)
8098 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8099 pdi = pdi->die_sibling;
8100 }
8101 }
8102 }
8103
8104 /* Read a partial die corresponding to an enumeration type. */
8105
8106 static void
8107 add_partial_enumeration (struct partial_die_info *enum_pdi,
8108 struct dwarf2_cu *cu)
8109 {
8110 struct partial_die_info *pdi;
8111
8112 if (enum_pdi->name (cu) != NULL)
8113 add_partial_symbol (enum_pdi, cu);
8114
8115 pdi = enum_pdi->die_child;
8116 while (pdi)
8117 {
8118 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8119 complaint (_("malformed enumerator DIE ignored"));
8120 else
8121 add_partial_symbol (pdi, cu);
8122 pdi = pdi->die_sibling;
8123 }
8124 }
8125
8126 /* Return the initial uleb128 in the die at INFO_PTR. */
8127
8128 static unsigned int
8129 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8130 {
8131 unsigned int bytes_read;
8132
8133 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8134 }
8135
8136 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8137 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8138
8139 Return the corresponding abbrev, or NULL if the number is zero (indicating
8140 an empty DIE). In either case *BYTES_READ will be set to the length of
8141 the initial number. */
8142
8143 static const struct abbrev_info *
8144 peek_die_abbrev (const die_reader_specs &reader,
8145 const gdb_byte *info_ptr, unsigned int *bytes_read)
8146 {
8147 dwarf2_cu *cu = reader.cu;
8148 bfd *abfd = reader.abfd;
8149 unsigned int abbrev_number
8150 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8151
8152 if (abbrev_number == 0)
8153 return NULL;
8154
8155 const abbrev_info *abbrev
8156 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8157 if (!abbrev)
8158 {
8159 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8160 " at offset %s [in module %s]"),
8161 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8162 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8163 }
8164
8165 return abbrev;
8166 }
8167
8168 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8169 Returns a pointer to the end of a series of DIEs, terminated by an empty
8170 DIE. Any children of the skipped DIEs will also be skipped. */
8171
8172 static const gdb_byte *
8173 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8174 {
8175 while (1)
8176 {
8177 unsigned int bytes_read;
8178 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8179 &bytes_read);
8180
8181 if (abbrev == NULL)
8182 return info_ptr + bytes_read;
8183 else
8184 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8185 }
8186 }
8187
8188 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8189 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8190 abbrev corresponding to that skipped uleb128 should be passed in
8191 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8192 children. */
8193
8194 static const gdb_byte *
8195 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8196 const struct abbrev_info *abbrev)
8197 {
8198 unsigned int bytes_read;
8199 struct attribute attr;
8200 bfd *abfd = reader->abfd;
8201 struct dwarf2_cu *cu = reader->cu;
8202 const gdb_byte *buffer = reader->buffer;
8203 const gdb_byte *buffer_end = reader->buffer_end;
8204 unsigned int form, i;
8205
8206 for (i = 0; i < abbrev->num_attrs; i++)
8207 {
8208 /* The only abbrev we care about is DW_AT_sibling. */
8209 if (abbrev->attrs[i].name == DW_AT_sibling)
8210 {
8211 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8212 if (attr.form == DW_FORM_ref_addr)
8213 complaint (_("ignoring absolute DW_AT_sibling"));
8214 else
8215 {
8216 sect_offset off = attr.get_ref_die_offset ();
8217 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8218
8219 if (sibling_ptr < info_ptr)
8220 complaint (_("DW_AT_sibling points backwards"));
8221 else if (sibling_ptr > reader->buffer_end)
8222 reader->die_section->overflow_complaint ();
8223 else
8224 return sibling_ptr;
8225 }
8226 }
8227
8228 /* If it isn't DW_AT_sibling, skip this attribute. */
8229 form = abbrev->attrs[i].form;
8230 skip_attribute:
8231 switch (form)
8232 {
8233 case DW_FORM_ref_addr:
8234 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8235 and later it is offset sized. */
8236 if (cu->header.version == 2)
8237 info_ptr += cu->header.addr_size;
8238 else
8239 info_ptr += cu->header.offset_size;
8240 break;
8241 case DW_FORM_GNU_ref_alt:
8242 info_ptr += cu->header.offset_size;
8243 break;
8244 case DW_FORM_addr:
8245 info_ptr += cu->header.addr_size;
8246 break;
8247 case DW_FORM_data1:
8248 case DW_FORM_ref1:
8249 case DW_FORM_flag:
8250 case DW_FORM_strx1:
8251 info_ptr += 1;
8252 break;
8253 case DW_FORM_flag_present:
8254 case DW_FORM_implicit_const:
8255 break;
8256 case DW_FORM_data2:
8257 case DW_FORM_ref2:
8258 case DW_FORM_strx2:
8259 info_ptr += 2;
8260 break;
8261 case DW_FORM_strx3:
8262 info_ptr += 3;
8263 break;
8264 case DW_FORM_data4:
8265 case DW_FORM_ref4:
8266 case DW_FORM_strx4:
8267 info_ptr += 4;
8268 break;
8269 case DW_FORM_data8:
8270 case DW_FORM_ref8:
8271 case DW_FORM_ref_sig8:
8272 info_ptr += 8;
8273 break;
8274 case DW_FORM_data16:
8275 info_ptr += 16;
8276 break;
8277 case DW_FORM_string:
8278 read_direct_string (abfd, info_ptr, &bytes_read);
8279 info_ptr += bytes_read;
8280 break;
8281 case DW_FORM_sec_offset:
8282 case DW_FORM_strp:
8283 case DW_FORM_GNU_strp_alt:
8284 info_ptr += cu->header.offset_size;
8285 break;
8286 case DW_FORM_exprloc:
8287 case DW_FORM_block:
8288 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8289 info_ptr += bytes_read;
8290 break;
8291 case DW_FORM_block1:
8292 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8293 break;
8294 case DW_FORM_block2:
8295 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8296 break;
8297 case DW_FORM_block4:
8298 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8299 break;
8300 case DW_FORM_addrx:
8301 case DW_FORM_strx:
8302 case DW_FORM_sdata:
8303 case DW_FORM_udata:
8304 case DW_FORM_ref_udata:
8305 case DW_FORM_GNU_addr_index:
8306 case DW_FORM_GNU_str_index:
8307 case DW_FORM_rnglistx:
8308 case DW_FORM_loclistx:
8309 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8310 break;
8311 case DW_FORM_indirect:
8312 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8313 info_ptr += bytes_read;
8314 /* We need to continue parsing from here, so just go back to
8315 the top. */
8316 goto skip_attribute;
8317
8318 default:
8319 error (_("Dwarf Error: Cannot handle %s "
8320 "in DWARF reader [in module %s]"),
8321 dwarf_form_name (form),
8322 bfd_get_filename (abfd));
8323 }
8324 }
8325
8326 if (abbrev->has_children)
8327 return skip_children (reader, info_ptr);
8328 else
8329 return info_ptr;
8330 }
8331
8332 /* Locate ORIG_PDI's sibling.
8333 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8334
8335 static const gdb_byte *
8336 locate_pdi_sibling (const struct die_reader_specs *reader,
8337 struct partial_die_info *orig_pdi,
8338 const gdb_byte *info_ptr)
8339 {
8340 /* Do we know the sibling already? */
8341
8342 if (orig_pdi->sibling)
8343 return orig_pdi->sibling;
8344
8345 /* Are there any children to deal with? */
8346
8347 if (!orig_pdi->has_children)
8348 return info_ptr;
8349
8350 /* Skip the children the long way. */
8351
8352 return skip_children (reader, info_ptr);
8353 }
8354
8355 /* Expand this partial symbol table into a full symbol table. SELF is
8356 not NULL. */
8357
8358 void
8359 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8360 {
8361 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8362
8363 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8364
8365 /* If this psymtab is constructed from a debug-only objfile, the
8366 has_section_at_zero flag will not necessarily be correct. We
8367 can get the correct value for this flag by looking at the data
8368 associated with the (presumably stripped) associated objfile. */
8369 if (objfile->separate_debug_objfile_backlink)
8370 {
8371 dwarf2_per_objfile *per_objfile_backlink
8372 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8373
8374 per_objfile->per_bfd->has_section_at_zero
8375 = per_objfile_backlink->per_bfd->has_section_at_zero;
8376 }
8377
8378 expand_psymtab (objfile);
8379
8380 process_cu_includes (per_objfile);
8381 }
8382 \f
8383 /* Reading in full CUs. */
8384
8385 /* Add PER_CU to the queue. */
8386
8387 static void
8388 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8389 dwarf2_per_objfile *per_objfile,
8390 enum language pretend_language)
8391 {
8392 per_cu->queued = 1;
8393
8394 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8395 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8396 }
8397
8398 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8399
8400 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8401 dependency.
8402
8403 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8404 DIEs, false otherwise.
8405
8406 Explanation: there is an invariant that if a CU is queued for expansion
8407 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8408 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8409 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8410 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8411 invariant is respected.
8412
8413 The caller is therefore not required to load the CU's DIEs (we return false)
8414 if:
8415
8416 - the CU is already expanded, and therefore does not get enqueued
8417 - the CU gets enqueued for expansion, but its DIEs are already loaded
8418
8419 Note that the caller should not use this function's return value as an
8420 indicator of whether the CU's DIEs are loaded right now, it should check
8421 that by calling `dwarf2_per_objfile::get_cu` instead. */
8422
8423 static int
8424 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8425 dwarf2_per_cu_data *per_cu,
8426 dwarf2_per_objfile *per_objfile,
8427 enum language pretend_language)
8428 {
8429 /* We may arrive here during partial symbol reading, if we need full
8430 DIEs to process an unusual case (e.g. template arguments). Do
8431 not queue PER_CU, just tell our caller to load its DIEs. */
8432 if (per_cu->per_bfd->reading_partial_symbols)
8433 {
8434 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8435
8436 if (cu == NULL || cu->dies == NULL)
8437 return 1;
8438 return 0;
8439 }
8440
8441 /* Mark the dependence relation so that we don't flush PER_CU
8442 too early. */
8443 if (dependent_cu != NULL)
8444 dependent_cu->add_dependence (per_cu);
8445
8446 /* If it's already on the queue, we have nothing to do. */
8447 if (per_cu->queued)
8448 {
8449 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8450 loaded. */
8451 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8452
8453 /* If the CU is queued for expansion, it should not already be
8454 expanded. */
8455 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8456
8457 /* The DIEs are already loaded, the caller doesn't need to do it. */
8458 return 0;
8459 }
8460
8461 bool queued = false;
8462 if (!per_objfile->symtab_set_p (per_cu))
8463 {
8464 /* Add it to the queue. */
8465 queue_comp_unit (per_cu, per_objfile, pretend_language);
8466 queued = true;
8467 }
8468
8469 /* If the compilation unit is already loaded, just mark it as
8470 used. */
8471 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8472 if (cu != nullptr)
8473 cu->last_used = 0;
8474
8475 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8476 and the DIEs are not already loaded. */
8477 return queued && cu == nullptr;
8478 }
8479
8480 /* Process the queue. */
8481
8482 static void
8483 process_queue (dwarf2_per_objfile *per_objfile)
8484 {
8485 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8486 objfile_name (per_objfile->objfile));
8487
8488 /* The queue starts out with one item, but following a DIE reference
8489 may load a new CU, adding it to the end of the queue. */
8490 while (!per_objfile->per_bfd->queue->empty ())
8491 {
8492 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8493 dwarf2_per_cu_data *per_cu = item.per_cu;
8494
8495 if (!per_objfile->symtab_set_p (per_cu))
8496 {
8497 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8498
8499 /* Skip dummy CUs. */
8500 if (cu != nullptr)
8501 {
8502 unsigned int debug_print_threshold;
8503 char buf[100];
8504
8505 if (per_cu->is_debug_types)
8506 {
8507 struct signatured_type *sig_type =
8508 (struct signatured_type *) per_cu;
8509
8510 sprintf (buf, "TU %s at offset %s",
8511 hex_string (sig_type->signature),
8512 sect_offset_str (per_cu->sect_off));
8513 /* There can be 100s of TUs.
8514 Only print them in verbose mode. */
8515 debug_print_threshold = 2;
8516 }
8517 else
8518 {
8519 sprintf (buf, "CU at offset %s",
8520 sect_offset_str (per_cu->sect_off));
8521 debug_print_threshold = 1;
8522 }
8523
8524 if (dwarf_read_debug >= debug_print_threshold)
8525 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8526
8527 if (per_cu->is_debug_types)
8528 process_full_type_unit (cu, item.pretend_language);
8529 else
8530 process_full_comp_unit (cu, item.pretend_language);
8531
8532 if (dwarf_read_debug >= debug_print_threshold)
8533 dwarf_read_debug_printf ("Done expanding %s", buf);
8534 }
8535 }
8536
8537 per_cu->queued = 0;
8538 per_objfile->per_bfd->queue->pop ();
8539 }
8540
8541 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8542 objfile_name (per_objfile->objfile));
8543 }
8544
8545 /* Read in full symbols for PST, and anything it depends on. */
8546
8547 void
8548 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8549 {
8550 gdb_assert (!readin_p (objfile));
8551
8552 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8553 free_cached_comp_units freer (per_objfile);
8554 expand_dependencies (objfile);
8555
8556 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8557 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8558 }
8559
8560 /* See psympriv.h. */
8561
8562 bool
8563 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8564 {
8565 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8566 return per_objfile->symtab_set_p (per_cu_data);
8567 }
8568
8569 /* See psympriv.h. */
8570
8571 compunit_symtab *
8572 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8573 {
8574 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8575 return per_objfile->get_symtab (per_cu_data);
8576 }
8577
8578 /* Trivial hash function for die_info: the hash value of a DIE
8579 is its offset in .debug_info for this objfile. */
8580
8581 static hashval_t
8582 die_hash (const void *item)
8583 {
8584 const struct die_info *die = (const struct die_info *) item;
8585
8586 return to_underlying (die->sect_off);
8587 }
8588
8589 /* Trivial comparison function for die_info structures: two DIEs
8590 are equal if they have the same offset. */
8591
8592 static int
8593 die_eq (const void *item_lhs, const void *item_rhs)
8594 {
8595 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8596 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8597
8598 return die_lhs->sect_off == die_rhs->sect_off;
8599 }
8600
8601 /* Load the DIEs associated with PER_CU into memory.
8602
8603 In some cases, the caller, while reading partial symbols, will need to load
8604 the full symbols for the CU for some reason. It will already have a
8605 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8606 rather than creating a new one. */
8607
8608 static void
8609 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8610 dwarf2_per_objfile *per_objfile,
8611 dwarf2_cu *existing_cu,
8612 bool skip_partial,
8613 enum language pretend_language)
8614 {
8615 gdb_assert (! this_cu->is_debug_types);
8616
8617 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8618 if (reader.dummy_p)
8619 return;
8620
8621 struct dwarf2_cu *cu = reader.cu;
8622 const gdb_byte *info_ptr = reader.info_ptr;
8623
8624 gdb_assert (cu->die_hash == NULL);
8625 cu->die_hash =
8626 htab_create_alloc_ex (cu->header.length / 12,
8627 die_hash,
8628 die_eq,
8629 NULL,
8630 &cu->comp_unit_obstack,
8631 hashtab_obstack_allocate,
8632 dummy_obstack_deallocate);
8633
8634 if (reader.comp_unit_die->has_children)
8635 reader.comp_unit_die->child
8636 = read_die_and_siblings (&reader, reader.info_ptr,
8637 &info_ptr, reader.comp_unit_die);
8638 cu->dies = reader.comp_unit_die;
8639 /* comp_unit_die is not stored in die_hash, no need. */
8640
8641 /* We try not to read any attributes in this function, because not
8642 all CUs needed for references have been loaded yet, and symbol
8643 table processing isn't initialized. But we have to set the CU language,
8644 or we won't be able to build types correctly.
8645 Similarly, if we do not read the producer, we can not apply
8646 producer-specific interpretation. */
8647 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8648
8649 reader.keep ();
8650 }
8651
8652 /* Add a DIE to the delayed physname list. */
8653
8654 static void
8655 add_to_method_list (struct type *type, int fnfield_index, int index,
8656 const char *name, struct die_info *die,
8657 struct dwarf2_cu *cu)
8658 {
8659 struct delayed_method_info mi;
8660 mi.type = type;
8661 mi.fnfield_index = fnfield_index;
8662 mi.index = index;
8663 mi.name = name;
8664 mi.die = die;
8665 cu->method_list.push_back (mi);
8666 }
8667
8668 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8669 "const" / "volatile". If so, decrements LEN by the length of the
8670 modifier and return true. Otherwise return false. */
8671
8672 template<size_t N>
8673 static bool
8674 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8675 {
8676 size_t mod_len = sizeof (mod) - 1;
8677 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8678 {
8679 len -= mod_len;
8680 return true;
8681 }
8682 return false;
8683 }
8684
8685 /* Compute the physnames of any methods on the CU's method list.
8686
8687 The computation of method physnames is delayed in order to avoid the
8688 (bad) condition that one of the method's formal parameters is of an as yet
8689 incomplete type. */
8690
8691 static void
8692 compute_delayed_physnames (struct dwarf2_cu *cu)
8693 {
8694 /* Only C++ delays computing physnames. */
8695 if (cu->method_list.empty ())
8696 return;
8697 gdb_assert (cu->language == language_cplus);
8698
8699 for (const delayed_method_info &mi : cu->method_list)
8700 {
8701 const char *physname;
8702 struct fn_fieldlist *fn_flp
8703 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8704 physname = dwarf2_physname (mi.name, mi.die, cu);
8705 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8706 = physname ? physname : "";
8707
8708 /* Since there's no tag to indicate whether a method is a
8709 const/volatile overload, extract that information out of the
8710 demangled name. */
8711 if (physname != NULL)
8712 {
8713 size_t len = strlen (physname);
8714
8715 while (1)
8716 {
8717 if (physname[len] == ')') /* shortcut */
8718 break;
8719 else if (check_modifier (physname, len, " const"))
8720 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8721 else if (check_modifier (physname, len, " volatile"))
8722 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8723 else
8724 break;
8725 }
8726 }
8727 }
8728
8729 /* The list is no longer needed. */
8730 cu->method_list.clear ();
8731 }
8732
8733 /* Go objects should be embedded in a DW_TAG_module DIE,
8734 and it's not clear if/how imported objects will appear.
8735 To keep Go support simple until that's worked out,
8736 go back through what we've read and create something usable.
8737 We could do this while processing each DIE, and feels kinda cleaner,
8738 but that way is more invasive.
8739 This is to, for example, allow the user to type "p var" or "b main"
8740 without having to specify the package name, and allow lookups
8741 of module.object to work in contexts that use the expression
8742 parser. */
8743
8744 static void
8745 fixup_go_packaging (struct dwarf2_cu *cu)
8746 {
8747 gdb::unique_xmalloc_ptr<char> package_name;
8748 struct pending *list;
8749 int i;
8750
8751 for (list = *cu->get_builder ()->get_global_symbols ();
8752 list != NULL;
8753 list = list->next)
8754 {
8755 for (i = 0; i < list->nsyms; ++i)
8756 {
8757 struct symbol *sym = list->symbol[i];
8758
8759 if (sym->language () == language_go
8760 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8761 {
8762 gdb::unique_xmalloc_ptr<char> this_package_name
8763 (go_symbol_package_name (sym));
8764
8765 if (this_package_name == NULL)
8766 continue;
8767 if (package_name == NULL)
8768 package_name = std::move (this_package_name);
8769 else
8770 {
8771 struct objfile *objfile = cu->per_objfile->objfile;
8772 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8773 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8774 (symbol_symtab (sym) != NULL
8775 ? symtab_to_filename_for_display
8776 (symbol_symtab (sym))
8777 : objfile_name (objfile)),
8778 this_package_name.get (), package_name.get ());
8779 }
8780 }
8781 }
8782 }
8783
8784 if (package_name != NULL)
8785 {
8786 struct objfile *objfile = cu->per_objfile->objfile;
8787 const char *saved_package_name = objfile->intern (package_name.get ());
8788 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8789 saved_package_name);
8790 struct symbol *sym;
8791
8792 sym = new (&objfile->objfile_obstack) symbol;
8793 sym->set_language (language_go, &objfile->objfile_obstack);
8794 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8795 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8796 e.g., "main" finds the "main" module and not C's main(). */
8797 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8798 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8799 SYMBOL_TYPE (sym) = type;
8800
8801 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8802 }
8803 }
8804
8805 /* Allocate a fully-qualified name consisting of the two parts on the
8806 obstack. */
8807
8808 static const char *
8809 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8810 {
8811 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8812 }
8813
8814 /* A helper that allocates a variant part to attach to a Rust enum
8815 type. OBSTACK is where the results should be allocated. TYPE is
8816 the type we're processing. DISCRIMINANT_INDEX is the index of the
8817 discriminant. It must be the index of one of the fields of TYPE,
8818 or -1 to mean there is no discriminant (univariant enum).
8819 DEFAULT_INDEX is the index of the default field; or -1 if there is
8820 no default. RANGES is indexed by "effective" field number (the
8821 field index, but omitting the discriminant and default fields) and
8822 must hold the discriminant values used by the variants. Note that
8823 RANGES must have a lifetime at least as long as OBSTACK -- either
8824 already allocated on it, or static. */
8825
8826 static void
8827 alloc_rust_variant (struct obstack *obstack, struct type *type,
8828 int discriminant_index, int default_index,
8829 gdb::array_view<discriminant_range> ranges)
8830 {
8831 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8832 gdb_assert (discriminant_index == -1
8833 || (discriminant_index >= 0
8834 && discriminant_index < type->num_fields ()));
8835 gdb_assert (default_index == -1
8836 || (default_index >= 0 && default_index < type->num_fields ()));
8837
8838 /* We have one variant for each non-discriminant field. */
8839 int n_variants = type->num_fields ();
8840 if (discriminant_index != -1)
8841 --n_variants;
8842
8843 variant *variants = new (obstack) variant[n_variants];
8844 int var_idx = 0;
8845 int range_idx = 0;
8846 for (int i = 0; i < type->num_fields (); ++i)
8847 {
8848 if (i == discriminant_index)
8849 continue;
8850
8851 variants[var_idx].first_field = i;
8852 variants[var_idx].last_field = i + 1;
8853
8854 /* The default field does not need a range, but other fields do.
8855 We skipped the discriminant above. */
8856 if (i != default_index)
8857 {
8858 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8859 ++range_idx;
8860 }
8861
8862 ++var_idx;
8863 }
8864
8865 gdb_assert (range_idx == ranges.size ());
8866 gdb_assert (var_idx == n_variants);
8867
8868 variant_part *part = new (obstack) variant_part;
8869 part->discriminant_index = discriminant_index;
8870 /* If there is no discriminant, then whether it is signed is of no
8871 consequence. */
8872 part->is_unsigned
8873 = (discriminant_index == -1
8874 ? false
8875 : type->field (discriminant_index).type ()->is_unsigned ());
8876 part->variants = gdb::array_view<variant> (variants, n_variants);
8877
8878 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
8879 gdb::array_view<variant_part> *prop_value
8880 = new (storage) gdb::array_view<variant_part> (part, 1);
8881
8882 struct dynamic_prop prop;
8883 prop.set_variant_parts (prop_value);
8884
8885 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
8886 }
8887
8888 /* Some versions of rustc emitted enums in an unusual way.
8889
8890 Ordinary enums were emitted as unions. The first element of each
8891 structure in the union was named "RUST$ENUM$DISR". This element
8892 held the discriminant.
8893
8894 These versions of Rust also implemented the "non-zero"
8895 optimization. When the enum had two values, and one is empty and
8896 the other holds a pointer that cannot be zero, the pointer is used
8897 as the discriminant, with a zero value meaning the empty variant.
8898 Here, the union's first member is of the form
8899 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8900 where the fieldnos are the indices of the fields that should be
8901 traversed in order to find the field (which may be several fields deep)
8902 and the variantname is the name of the variant of the case when the
8903 field is zero.
8904
8905 This function recognizes whether TYPE is of one of these forms,
8906 and, if so, smashes it to be a variant type. */
8907
8908 static void
8909 quirk_rust_enum (struct type *type, struct objfile *objfile)
8910 {
8911 gdb_assert (type->code () == TYPE_CODE_UNION);
8912
8913 /* We don't need to deal with empty enums. */
8914 if (type->num_fields () == 0)
8915 return;
8916
8917 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
8918 if (type->num_fields () == 1
8919 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
8920 {
8921 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
8922
8923 /* Decode the field name to find the offset of the
8924 discriminant. */
8925 ULONGEST bit_offset = 0;
8926 struct type *field_type = type->field (0).type ();
8927 while (name[0] >= '0' && name[0] <= '9')
8928 {
8929 char *tail;
8930 unsigned long index = strtoul (name, &tail, 10);
8931 name = tail;
8932 if (*name != '$'
8933 || index >= field_type->num_fields ()
8934 || (TYPE_FIELD_LOC_KIND (field_type, index)
8935 != FIELD_LOC_KIND_BITPOS))
8936 {
8937 complaint (_("Could not parse Rust enum encoding string \"%s\""
8938 "[in module %s]"),
8939 TYPE_FIELD_NAME (type, 0),
8940 objfile_name (objfile));
8941 return;
8942 }
8943 ++name;
8944
8945 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
8946 field_type = field_type->field (index).type ();
8947 }
8948
8949 /* Smash this type to be a structure type. We have to do this
8950 because the type has already been recorded. */
8951 type->set_code (TYPE_CODE_STRUCT);
8952 type->set_num_fields (3);
8953 /* Save the field we care about. */
8954 struct field saved_field = type->field (0);
8955 type->set_fields
8956 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
8957
8958 /* Put the discriminant at index 0. */
8959 type->field (0).set_type (field_type);
8960 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8961 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
8962 SET_FIELD_BITPOS (type->field (0), bit_offset);
8963
8964 /* The order of fields doesn't really matter, so put the real
8965 field at index 1 and the data-less field at index 2. */
8966 type->field (1) = saved_field;
8967 TYPE_FIELD_NAME (type, 1)
8968 = rust_last_path_segment (type->field (1).type ()->name ());
8969 type->field (1).type ()->set_name
8970 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8971 TYPE_FIELD_NAME (type, 1)));
8972
8973 const char *dataless_name
8974 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8975 name);
8976 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8977 dataless_name);
8978 type->field (2).set_type (dataless_type);
8979 /* NAME points into the original discriminant name, which
8980 already has the correct lifetime. */
8981 TYPE_FIELD_NAME (type, 2) = name;
8982 SET_FIELD_BITPOS (type->field (2), 0);
8983
8984 /* Indicate that this is a variant type. */
8985 static discriminant_range ranges[1] = { { 0, 0 } };
8986 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8987 }
8988 /* A union with a single anonymous field is probably an old-style
8989 univariant enum. */
8990 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
8991 {
8992 /* Smash this type to be a structure type. We have to do this
8993 because the type has already been recorded. */
8994 type->set_code (TYPE_CODE_STRUCT);
8995
8996 struct type *field_type = type->field (0).type ();
8997 const char *variant_name
8998 = rust_last_path_segment (field_type->name ());
8999 TYPE_FIELD_NAME (type, 0) = variant_name;
9000 field_type->set_name
9001 (rust_fully_qualify (&objfile->objfile_obstack,
9002 type->name (), variant_name));
9003
9004 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9005 }
9006 else
9007 {
9008 struct type *disr_type = nullptr;
9009 for (int i = 0; i < type->num_fields (); ++i)
9010 {
9011 disr_type = type->field (i).type ();
9012
9013 if (disr_type->code () != TYPE_CODE_STRUCT)
9014 {
9015 /* All fields of a true enum will be structs. */
9016 return;
9017 }
9018 else if (disr_type->num_fields () == 0)
9019 {
9020 /* Could be data-less variant, so keep going. */
9021 disr_type = nullptr;
9022 }
9023 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9024 "RUST$ENUM$DISR") != 0)
9025 {
9026 /* Not a Rust enum. */
9027 return;
9028 }
9029 else
9030 {
9031 /* Found one. */
9032 break;
9033 }
9034 }
9035
9036 /* If we got here without a discriminant, then it's probably
9037 just a union. */
9038 if (disr_type == nullptr)
9039 return;
9040
9041 /* Smash this type to be a structure type. We have to do this
9042 because the type has already been recorded. */
9043 type->set_code (TYPE_CODE_STRUCT);
9044
9045 /* Make space for the discriminant field. */
9046 struct field *disr_field = &disr_type->field (0);
9047 field *new_fields
9048 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9049 * sizeof (struct field)));
9050 memcpy (new_fields + 1, type->fields (),
9051 type->num_fields () * sizeof (struct field));
9052 type->set_fields (new_fields);
9053 type->set_num_fields (type->num_fields () + 1);
9054
9055 /* Install the discriminant at index 0 in the union. */
9056 type->field (0) = *disr_field;
9057 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9058 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9059
9060 /* We need a way to find the correct discriminant given a
9061 variant name. For convenience we build a map here. */
9062 struct type *enum_type = disr_field->type ();
9063 std::unordered_map<std::string, ULONGEST> discriminant_map;
9064 for (int i = 0; i < enum_type->num_fields (); ++i)
9065 {
9066 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9067 {
9068 const char *name
9069 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9070 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9071 }
9072 }
9073
9074 int n_fields = type->num_fields ();
9075 /* We don't need a range entry for the discriminant, but we do
9076 need one for every other field, as there is no default
9077 variant. */
9078 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9079 discriminant_range,
9080 n_fields - 1);
9081 /* Skip the discriminant here. */
9082 for (int i = 1; i < n_fields; ++i)
9083 {
9084 /* Find the final word in the name of this variant's type.
9085 That name can be used to look up the correct
9086 discriminant. */
9087 const char *variant_name
9088 = rust_last_path_segment (type->field (i).type ()->name ());
9089
9090 auto iter = discriminant_map.find (variant_name);
9091 if (iter != discriminant_map.end ())
9092 {
9093 ranges[i - 1].low = iter->second;
9094 ranges[i - 1].high = iter->second;
9095 }
9096
9097 /* In Rust, each element should have the size of the
9098 enclosing enum. */
9099 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9100
9101 /* Remove the discriminant field, if it exists. */
9102 struct type *sub_type = type->field (i).type ();
9103 if (sub_type->num_fields () > 0)
9104 {
9105 sub_type->set_num_fields (sub_type->num_fields () - 1);
9106 sub_type->set_fields (sub_type->fields () + 1);
9107 }
9108 TYPE_FIELD_NAME (type, i) = variant_name;
9109 sub_type->set_name
9110 (rust_fully_qualify (&objfile->objfile_obstack,
9111 type->name (), variant_name));
9112 }
9113
9114 /* Indicate that this is a variant type. */
9115 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9116 gdb::array_view<discriminant_range> (ranges,
9117 n_fields - 1));
9118 }
9119 }
9120
9121 /* Rewrite some Rust unions to be structures with variants parts. */
9122
9123 static void
9124 rust_union_quirks (struct dwarf2_cu *cu)
9125 {
9126 gdb_assert (cu->language == language_rust);
9127 for (type *type_ : cu->rust_unions)
9128 quirk_rust_enum (type_, cu->per_objfile->objfile);
9129 /* We don't need this any more. */
9130 cu->rust_unions.clear ();
9131 }
9132
9133 /* See read.h. */
9134
9135 type_unit_group_unshareable *
9136 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9137 {
9138 auto iter = this->m_type_units.find (tu_group);
9139 if (iter != this->m_type_units.end ())
9140 return iter->second.get ();
9141
9142 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9143 type_unit_group_unshareable *result = uniq.get ();
9144 this->m_type_units[tu_group] = std::move (uniq);
9145 return result;
9146 }
9147
9148 struct type *
9149 dwarf2_per_objfile::get_type_for_signatured_type
9150 (signatured_type *sig_type) const
9151 {
9152 auto iter = this->m_type_map.find (sig_type);
9153 if (iter == this->m_type_map.end ())
9154 return nullptr;
9155
9156 return iter->second;
9157 }
9158
9159 void dwarf2_per_objfile::set_type_for_signatured_type
9160 (signatured_type *sig_type, struct type *type)
9161 {
9162 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9163
9164 this->m_type_map[sig_type] = type;
9165 }
9166
9167 /* A helper function for computing the list of all symbol tables
9168 included by PER_CU. */
9169
9170 static void
9171 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9172 htab_t all_children, htab_t all_type_symtabs,
9173 dwarf2_per_cu_data *per_cu,
9174 dwarf2_per_objfile *per_objfile,
9175 struct compunit_symtab *immediate_parent)
9176 {
9177 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9178 if (*slot != NULL)
9179 {
9180 /* This inclusion and its children have been processed. */
9181 return;
9182 }
9183
9184 *slot = per_cu;
9185
9186 /* Only add a CU if it has a symbol table. */
9187 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9188 if (cust != NULL)
9189 {
9190 /* If this is a type unit only add its symbol table if we haven't
9191 seen it yet (type unit per_cu's can share symtabs). */
9192 if (per_cu->is_debug_types)
9193 {
9194 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9195 if (*slot == NULL)
9196 {
9197 *slot = cust;
9198 result->push_back (cust);
9199 if (cust->user == NULL)
9200 cust->user = immediate_parent;
9201 }
9202 }
9203 else
9204 {
9205 result->push_back (cust);
9206 if (cust->user == NULL)
9207 cust->user = immediate_parent;
9208 }
9209 }
9210
9211 if (!per_cu->imported_symtabs_empty ())
9212 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9213 {
9214 recursively_compute_inclusions (result, all_children,
9215 all_type_symtabs, ptr, per_objfile,
9216 cust);
9217 }
9218 }
9219
9220 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9221 PER_CU. */
9222
9223 static void
9224 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9225 dwarf2_per_objfile *per_objfile)
9226 {
9227 gdb_assert (! per_cu->is_debug_types);
9228
9229 if (!per_cu->imported_symtabs_empty ())
9230 {
9231 int len;
9232 std::vector<compunit_symtab *> result_symtabs;
9233 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9234
9235 /* If we don't have a symtab, we can just skip this case. */
9236 if (cust == NULL)
9237 return;
9238
9239 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9240 htab_eq_pointer,
9241 NULL, xcalloc, xfree));
9242 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9243 htab_eq_pointer,
9244 NULL, xcalloc, xfree));
9245
9246 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9247 {
9248 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9249 all_type_symtabs.get (), ptr,
9250 per_objfile, cust);
9251 }
9252
9253 /* Now we have a transitive closure of all the included symtabs. */
9254 len = result_symtabs.size ();
9255 cust->includes
9256 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9257 struct compunit_symtab *, len + 1);
9258 memcpy (cust->includes, result_symtabs.data (),
9259 len * sizeof (compunit_symtab *));
9260 cust->includes[len] = NULL;
9261 }
9262 }
9263
9264 /* Compute the 'includes' field for the symtabs of all the CUs we just
9265 read. */
9266
9267 static void
9268 process_cu_includes (dwarf2_per_objfile *per_objfile)
9269 {
9270 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9271 {
9272 if (! iter->is_debug_types)
9273 compute_compunit_symtab_includes (iter, per_objfile);
9274 }
9275
9276 per_objfile->per_bfd->just_read_cus.clear ();
9277 }
9278
9279 /* Generate full symbol information for CU, whose DIEs have
9280 already been loaded into memory. */
9281
9282 static void
9283 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9284 {
9285 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9286 struct objfile *objfile = per_objfile->objfile;
9287 struct gdbarch *gdbarch = objfile->arch ();
9288 CORE_ADDR lowpc, highpc;
9289 struct compunit_symtab *cust;
9290 CORE_ADDR baseaddr;
9291 struct block *static_block;
9292 CORE_ADDR addr;
9293
9294 baseaddr = objfile->text_section_offset ();
9295
9296 /* Clear the list here in case something was left over. */
9297 cu->method_list.clear ();
9298
9299 cu->language = pretend_language;
9300 cu->language_defn = language_def (cu->language);
9301
9302 dwarf2_find_base_address (cu->dies, cu);
9303
9304 /* Before we start reading the top-level DIE, ensure it has a valid tag
9305 type. */
9306 switch (cu->dies->tag)
9307 {
9308 case DW_TAG_compile_unit:
9309 case DW_TAG_partial_unit:
9310 case DW_TAG_type_unit:
9311 break;
9312 default:
9313 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9314 dwarf_tag_name (cu->dies->tag),
9315 sect_offset_str (cu->per_cu->sect_off),
9316 objfile_name (per_objfile->objfile));
9317 }
9318
9319 /* Do line number decoding in read_file_scope () */
9320 process_die (cu->dies, cu);
9321
9322 /* For now fudge the Go package. */
9323 if (cu->language == language_go)
9324 fixup_go_packaging (cu);
9325
9326 /* Now that we have processed all the DIEs in the CU, all the types
9327 should be complete, and it should now be safe to compute all of the
9328 physnames. */
9329 compute_delayed_physnames (cu);
9330
9331 if (cu->language == language_rust)
9332 rust_union_quirks (cu);
9333
9334 /* Some compilers don't define a DW_AT_high_pc attribute for the
9335 compilation unit. If the DW_AT_high_pc is missing, synthesize
9336 it, by scanning the DIE's below the compilation unit. */
9337 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9338
9339 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9340 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9341
9342 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9343 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9344 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9345 addrmap to help ensure it has an accurate map of pc values belonging to
9346 this comp unit. */
9347 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9348
9349 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9350 SECT_OFF_TEXT (objfile),
9351 0);
9352
9353 if (cust != NULL)
9354 {
9355 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9356
9357 /* Set symtab language to language from DW_AT_language. If the
9358 compilation is from a C file generated by language preprocessors, do
9359 not set the language if it was already deduced by start_subfile. */
9360 if (!(cu->language == language_c
9361 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9362 COMPUNIT_FILETABS (cust)->language = cu->language;
9363
9364 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9365 produce DW_AT_location with location lists but it can be possibly
9366 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9367 there were bugs in prologue debug info, fixed later in GCC-4.5
9368 by "unwind info for epilogues" patch (which is not directly related).
9369
9370 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9371 needed, it would be wrong due to missing DW_AT_producer there.
9372
9373 Still one can confuse GDB by using non-standard GCC compilation
9374 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9375 */
9376 if (cu->has_loclist && gcc_4_minor >= 5)
9377 cust->locations_valid = 1;
9378
9379 if (gcc_4_minor >= 5)
9380 cust->epilogue_unwind_valid = 1;
9381
9382 cust->call_site_htab = cu->call_site_htab;
9383 }
9384
9385 per_objfile->set_symtab (cu->per_cu, cust);
9386
9387 /* Push it for inclusion processing later. */
9388 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9389
9390 /* Not needed any more. */
9391 cu->reset_builder ();
9392 }
9393
9394 /* Generate full symbol information for type unit CU, whose DIEs have
9395 already been loaded into memory. */
9396
9397 static void
9398 process_full_type_unit (dwarf2_cu *cu,
9399 enum language pretend_language)
9400 {
9401 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9402 struct objfile *objfile = per_objfile->objfile;
9403 struct compunit_symtab *cust;
9404 struct signatured_type *sig_type;
9405
9406 gdb_assert (cu->per_cu->is_debug_types);
9407 sig_type = (struct signatured_type *) cu->per_cu;
9408
9409 /* Clear the list here in case something was left over. */
9410 cu->method_list.clear ();
9411
9412 cu->language = pretend_language;
9413 cu->language_defn = language_def (cu->language);
9414
9415 /* The symbol tables are set up in read_type_unit_scope. */
9416 process_die (cu->dies, cu);
9417
9418 /* For now fudge the Go package. */
9419 if (cu->language == language_go)
9420 fixup_go_packaging (cu);
9421
9422 /* Now that we have processed all the DIEs in the CU, all the types
9423 should be complete, and it should now be safe to compute all of the
9424 physnames. */
9425 compute_delayed_physnames (cu);
9426
9427 if (cu->language == language_rust)
9428 rust_union_quirks (cu);
9429
9430 /* TUs share symbol tables.
9431 If this is the first TU to use this symtab, complete the construction
9432 of it with end_expandable_symtab. Otherwise, complete the addition of
9433 this TU's symbols to the existing symtab. */
9434 type_unit_group_unshareable *tug_unshare =
9435 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9436 if (tug_unshare->compunit_symtab == NULL)
9437 {
9438 buildsym_compunit *builder = cu->get_builder ();
9439 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9440 tug_unshare->compunit_symtab = cust;
9441
9442 if (cust != NULL)
9443 {
9444 /* Set symtab language to language from DW_AT_language. If the
9445 compilation is from a C file generated by language preprocessors,
9446 do not set the language if it was already deduced by
9447 start_subfile. */
9448 if (!(cu->language == language_c
9449 && COMPUNIT_FILETABS (cust)->language != language_c))
9450 COMPUNIT_FILETABS (cust)->language = cu->language;
9451 }
9452 }
9453 else
9454 {
9455 cu->get_builder ()->augment_type_symtab ();
9456 cust = tug_unshare->compunit_symtab;
9457 }
9458
9459 per_objfile->set_symtab (cu->per_cu, cust);
9460
9461 /* Not needed any more. */
9462 cu->reset_builder ();
9463 }
9464
9465 /* Process an imported unit DIE. */
9466
9467 static void
9468 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9469 {
9470 struct attribute *attr;
9471
9472 /* For now we don't handle imported units in type units. */
9473 if (cu->per_cu->is_debug_types)
9474 {
9475 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9476 " supported in type units [in module %s]"),
9477 objfile_name (cu->per_objfile->objfile));
9478 }
9479
9480 attr = dwarf2_attr (die, DW_AT_import, cu);
9481 if (attr != NULL)
9482 {
9483 sect_offset sect_off = attr->get_ref_die_offset ();
9484 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9485 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9486 dwarf2_per_cu_data *per_cu
9487 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9488
9489 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9490 into another compilation unit, at root level. Regard this as a hint,
9491 and ignore it. */
9492 if (die->parent && die->parent->parent == NULL
9493 && per_cu->unit_type == DW_UT_compile
9494 && per_cu->lang == language_cplus)
9495 return;
9496
9497 /* If necessary, add it to the queue and load its DIEs. */
9498 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9499 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9500 false, cu->language);
9501
9502 cu->per_cu->imported_symtabs_push (per_cu);
9503 }
9504 }
9505
9506 /* RAII object that represents a process_die scope: i.e.,
9507 starts/finishes processing a DIE. */
9508 class process_die_scope
9509 {
9510 public:
9511 process_die_scope (die_info *die, dwarf2_cu *cu)
9512 : m_die (die), m_cu (cu)
9513 {
9514 /* We should only be processing DIEs not already in process. */
9515 gdb_assert (!m_die->in_process);
9516 m_die->in_process = true;
9517 }
9518
9519 ~process_die_scope ()
9520 {
9521 m_die->in_process = false;
9522
9523 /* If we're done processing the DIE for the CU that owns the line
9524 header, we don't need the line header anymore. */
9525 if (m_cu->line_header_die_owner == m_die)
9526 {
9527 delete m_cu->line_header;
9528 m_cu->line_header = NULL;
9529 m_cu->line_header_die_owner = NULL;
9530 }
9531 }
9532
9533 private:
9534 die_info *m_die;
9535 dwarf2_cu *m_cu;
9536 };
9537
9538 /* Process a die and its children. */
9539
9540 static void
9541 process_die (struct die_info *die, struct dwarf2_cu *cu)
9542 {
9543 process_die_scope scope (die, cu);
9544
9545 switch (die->tag)
9546 {
9547 case DW_TAG_padding:
9548 break;
9549 case DW_TAG_compile_unit:
9550 case DW_TAG_partial_unit:
9551 read_file_scope (die, cu);
9552 break;
9553 case DW_TAG_type_unit:
9554 read_type_unit_scope (die, cu);
9555 break;
9556 case DW_TAG_subprogram:
9557 /* Nested subprograms in Fortran get a prefix. */
9558 if (cu->language == language_fortran
9559 && die->parent != NULL
9560 && die->parent->tag == DW_TAG_subprogram)
9561 cu->processing_has_namespace_info = true;
9562 /* Fall through. */
9563 case DW_TAG_inlined_subroutine:
9564 read_func_scope (die, cu);
9565 break;
9566 case DW_TAG_lexical_block:
9567 case DW_TAG_try_block:
9568 case DW_TAG_catch_block:
9569 read_lexical_block_scope (die, cu);
9570 break;
9571 case DW_TAG_call_site:
9572 case DW_TAG_GNU_call_site:
9573 read_call_site_scope (die, cu);
9574 break;
9575 case DW_TAG_class_type:
9576 case DW_TAG_interface_type:
9577 case DW_TAG_structure_type:
9578 case DW_TAG_union_type:
9579 process_structure_scope (die, cu);
9580 break;
9581 case DW_TAG_enumeration_type:
9582 process_enumeration_scope (die, cu);
9583 break;
9584
9585 /* These dies have a type, but processing them does not create
9586 a symbol or recurse to process the children. Therefore we can
9587 read them on-demand through read_type_die. */
9588 case DW_TAG_subroutine_type:
9589 case DW_TAG_set_type:
9590 case DW_TAG_pointer_type:
9591 case DW_TAG_ptr_to_member_type:
9592 case DW_TAG_reference_type:
9593 case DW_TAG_rvalue_reference_type:
9594 case DW_TAG_string_type:
9595 break;
9596
9597 case DW_TAG_array_type:
9598 /* We only need to handle this case for Ada -- in other
9599 languages, it's normal for the compiler to emit a typedef
9600 instead. */
9601 if (cu->language != language_ada)
9602 break;
9603 /* FALLTHROUGH */
9604 case DW_TAG_base_type:
9605 case DW_TAG_subrange_type:
9606 case DW_TAG_typedef:
9607 /* Add a typedef symbol for the type definition, if it has a
9608 DW_AT_name. */
9609 new_symbol (die, read_type_die (die, cu), cu);
9610 break;
9611 case DW_TAG_common_block:
9612 read_common_block (die, cu);
9613 break;
9614 case DW_TAG_common_inclusion:
9615 break;
9616 case DW_TAG_namespace:
9617 cu->processing_has_namespace_info = true;
9618 read_namespace (die, cu);
9619 break;
9620 case DW_TAG_module:
9621 cu->processing_has_namespace_info = true;
9622 read_module (die, cu);
9623 break;
9624 case DW_TAG_imported_declaration:
9625 cu->processing_has_namespace_info = true;
9626 if (read_namespace_alias (die, cu))
9627 break;
9628 /* The declaration is not a global namespace alias. */
9629 /* Fall through. */
9630 case DW_TAG_imported_module:
9631 cu->processing_has_namespace_info = true;
9632 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9633 || cu->language != language_fortran))
9634 complaint (_("Tag '%s' has unexpected children"),
9635 dwarf_tag_name (die->tag));
9636 read_import_statement (die, cu);
9637 break;
9638
9639 case DW_TAG_imported_unit:
9640 process_imported_unit_die (die, cu);
9641 break;
9642
9643 case DW_TAG_variable:
9644 read_variable (die, cu);
9645 break;
9646
9647 default:
9648 new_symbol (die, NULL, cu);
9649 break;
9650 }
9651 }
9652 \f
9653 /* DWARF name computation. */
9654
9655 /* A helper function for dwarf2_compute_name which determines whether DIE
9656 needs to have the name of the scope prepended to the name listed in the
9657 die. */
9658
9659 static int
9660 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9661 {
9662 struct attribute *attr;
9663
9664 switch (die->tag)
9665 {
9666 case DW_TAG_namespace:
9667 case DW_TAG_typedef:
9668 case DW_TAG_class_type:
9669 case DW_TAG_interface_type:
9670 case DW_TAG_structure_type:
9671 case DW_TAG_union_type:
9672 case DW_TAG_enumeration_type:
9673 case DW_TAG_enumerator:
9674 case DW_TAG_subprogram:
9675 case DW_TAG_inlined_subroutine:
9676 case DW_TAG_member:
9677 case DW_TAG_imported_declaration:
9678 return 1;
9679
9680 case DW_TAG_variable:
9681 case DW_TAG_constant:
9682 /* We only need to prefix "globally" visible variables. These include
9683 any variable marked with DW_AT_external or any variable that
9684 lives in a namespace. [Variables in anonymous namespaces
9685 require prefixing, but they are not DW_AT_external.] */
9686
9687 if (dwarf2_attr (die, DW_AT_specification, cu))
9688 {
9689 struct dwarf2_cu *spec_cu = cu;
9690
9691 return die_needs_namespace (die_specification (die, &spec_cu),
9692 spec_cu);
9693 }
9694
9695 attr = dwarf2_attr (die, DW_AT_external, cu);
9696 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9697 && die->parent->tag != DW_TAG_module)
9698 return 0;
9699 /* A variable in a lexical block of some kind does not need a
9700 namespace, even though in C++ such variables may be external
9701 and have a mangled name. */
9702 if (die->parent->tag == DW_TAG_lexical_block
9703 || die->parent->tag == DW_TAG_try_block
9704 || die->parent->tag == DW_TAG_catch_block
9705 || die->parent->tag == DW_TAG_subprogram)
9706 return 0;
9707 return 1;
9708
9709 default:
9710 return 0;
9711 }
9712 }
9713
9714 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9715 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9716 defined for the given DIE. */
9717
9718 static struct attribute *
9719 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9720 {
9721 struct attribute *attr;
9722
9723 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9724 if (attr == NULL)
9725 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9726
9727 return attr;
9728 }
9729
9730 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9731 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9732 defined for the given DIE. */
9733
9734 static const char *
9735 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9736 {
9737 const char *linkage_name;
9738
9739 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9740 if (linkage_name == NULL)
9741 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9742
9743 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9744 See https://github.com/rust-lang/rust/issues/32925. */
9745 if (cu->language == language_rust && linkage_name != NULL
9746 && strchr (linkage_name, '{') != NULL)
9747 linkage_name = NULL;
9748
9749 return linkage_name;
9750 }
9751
9752 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9753 compute the physname for the object, which include a method's:
9754 - formal parameters (C++),
9755 - receiver type (Go),
9756
9757 The term "physname" is a bit confusing.
9758 For C++, for example, it is the demangled name.
9759 For Go, for example, it's the mangled name.
9760
9761 For Ada, return the DIE's linkage name rather than the fully qualified
9762 name. PHYSNAME is ignored..
9763
9764 The result is allocated on the objfile->per_bfd's obstack and
9765 canonicalized. */
9766
9767 static const char *
9768 dwarf2_compute_name (const char *name,
9769 struct die_info *die, struct dwarf2_cu *cu,
9770 int physname)
9771 {
9772 struct objfile *objfile = cu->per_objfile->objfile;
9773
9774 if (name == NULL)
9775 name = dwarf2_name (die, cu);
9776
9777 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9778 but otherwise compute it by typename_concat inside GDB.
9779 FIXME: Actually this is not really true, or at least not always true.
9780 It's all very confusing. compute_and_set_names doesn't try to demangle
9781 Fortran names because there is no mangling standard. So new_symbol
9782 will set the demangled name to the result of dwarf2_full_name, and it is
9783 the demangled name that GDB uses if it exists. */
9784 if (cu->language == language_ada
9785 || (cu->language == language_fortran && physname))
9786 {
9787 /* For Ada unit, we prefer the linkage name over the name, as
9788 the former contains the exported name, which the user expects
9789 to be able to reference. Ideally, we want the user to be able
9790 to reference this entity using either natural or linkage name,
9791 but we haven't started looking at this enhancement yet. */
9792 const char *linkage_name = dw2_linkage_name (die, cu);
9793
9794 if (linkage_name != NULL)
9795 return linkage_name;
9796 }
9797
9798 /* These are the only languages we know how to qualify names in. */
9799 if (name != NULL
9800 && (cu->language == language_cplus
9801 || cu->language == language_fortran || cu->language == language_d
9802 || cu->language == language_rust))
9803 {
9804 if (die_needs_namespace (die, cu))
9805 {
9806 const char *prefix;
9807 const char *canonical_name = NULL;
9808
9809 string_file buf;
9810
9811 prefix = determine_prefix (die, cu);
9812 if (*prefix != '\0')
9813 {
9814 gdb::unique_xmalloc_ptr<char> prefixed_name
9815 (typename_concat (NULL, prefix, name, physname, cu));
9816
9817 buf.puts (prefixed_name.get ());
9818 }
9819 else
9820 buf.puts (name);
9821
9822 /* Template parameters may be specified in the DIE's DW_AT_name, or
9823 as children with DW_TAG_template_type_param or
9824 DW_TAG_value_type_param. If the latter, add them to the name
9825 here. If the name already has template parameters, then
9826 skip this step; some versions of GCC emit both, and
9827 it is more efficient to use the pre-computed name.
9828
9829 Something to keep in mind about this process: it is very
9830 unlikely, or in some cases downright impossible, to produce
9831 something that will match the mangled name of a function.
9832 If the definition of the function has the same debug info,
9833 we should be able to match up with it anyway. But fallbacks
9834 using the minimal symbol, for instance to find a method
9835 implemented in a stripped copy of libstdc++, will not work.
9836 If we do not have debug info for the definition, we will have to
9837 match them up some other way.
9838
9839 When we do name matching there is a related problem with function
9840 templates; two instantiated function templates are allowed to
9841 differ only by their return types, which we do not add here. */
9842
9843 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9844 {
9845 struct attribute *attr;
9846 struct die_info *child;
9847 int first = 1;
9848 const language_defn *cplus_lang = language_def (cu->language);
9849
9850 die->building_fullname = 1;
9851
9852 for (child = die->child; child != NULL; child = child->sibling)
9853 {
9854 struct type *type;
9855 LONGEST value;
9856 const gdb_byte *bytes;
9857 struct dwarf2_locexpr_baton *baton;
9858 struct value *v;
9859
9860 if (child->tag != DW_TAG_template_type_param
9861 && child->tag != DW_TAG_template_value_param)
9862 continue;
9863
9864 if (first)
9865 {
9866 buf.puts ("<");
9867 first = 0;
9868 }
9869 else
9870 buf.puts (", ");
9871
9872 attr = dwarf2_attr (child, DW_AT_type, cu);
9873 if (attr == NULL)
9874 {
9875 complaint (_("template parameter missing DW_AT_type"));
9876 buf.puts ("UNKNOWN_TYPE");
9877 continue;
9878 }
9879 type = die_type (child, cu);
9880
9881 if (child->tag == DW_TAG_template_type_param)
9882 {
9883 cplus_lang->print_type (type, "", &buf, -1, 0,
9884 &type_print_raw_options);
9885 continue;
9886 }
9887
9888 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9889 if (attr == NULL)
9890 {
9891 complaint (_("template parameter missing "
9892 "DW_AT_const_value"));
9893 buf.puts ("UNKNOWN_VALUE");
9894 continue;
9895 }
9896
9897 dwarf2_const_value_attr (attr, type, name,
9898 &cu->comp_unit_obstack, cu,
9899 &value, &bytes, &baton);
9900
9901 if (type->has_no_signedness ())
9902 /* GDB prints characters as NUMBER 'CHAR'. If that's
9903 changed, this can use value_print instead. */
9904 cplus_lang->printchar (value, type, &buf);
9905 else
9906 {
9907 struct value_print_options opts;
9908
9909 if (baton != NULL)
9910 v = dwarf2_evaluate_loc_desc (type, NULL,
9911 baton->data,
9912 baton->size,
9913 baton->per_cu,
9914 baton->per_objfile);
9915 else if (bytes != NULL)
9916 {
9917 v = allocate_value (type);
9918 memcpy (value_contents_writeable (v), bytes,
9919 TYPE_LENGTH (type));
9920 }
9921 else
9922 v = value_from_longest (type, value);
9923
9924 /* Specify decimal so that we do not depend on
9925 the radix. */
9926 get_formatted_print_options (&opts, 'd');
9927 opts.raw = 1;
9928 value_print (v, &buf, &opts);
9929 release_value (v);
9930 }
9931 }
9932
9933 die->building_fullname = 0;
9934
9935 if (!first)
9936 {
9937 /* Close the argument list, with a space if necessary
9938 (nested templates). */
9939 if (!buf.empty () && buf.string ().back () == '>')
9940 buf.puts (" >");
9941 else
9942 buf.puts (">");
9943 }
9944 }
9945
9946 /* For C++ methods, append formal parameter type
9947 information, if PHYSNAME. */
9948
9949 if (physname && die->tag == DW_TAG_subprogram
9950 && cu->language == language_cplus)
9951 {
9952 struct type *type = read_type_die (die, cu);
9953
9954 c_type_print_args (type, &buf, 1, cu->language,
9955 &type_print_raw_options);
9956
9957 if (cu->language == language_cplus)
9958 {
9959 /* Assume that an artificial first parameter is
9960 "this", but do not crash if it is not. RealView
9961 marks unnamed (and thus unused) parameters as
9962 artificial; there is no way to differentiate
9963 the two cases. */
9964 if (type->num_fields () > 0
9965 && TYPE_FIELD_ARTIFICIAL (type, 0)
9966 && type->field (0).type ()->code () == TYPE_CODE_PTR
9967 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
9968 buf.puts (" const");
9969 }
9970 }
9971
9972 const std::string &intermediate_name = buf.string ();
9973
9974 if (cu->language == language_cplus)
9975 canonical_name
9976 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9977 objfile);
9978
9979 /* If we only computed INTERMEDIATE_NAME, or if
9980 INTERMEDIATE_NAME is already canonical, then we need to
9981 intern it. */
9982 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9983 name = objfile->intern (intermediate_name);
9984 else
9985 name = canonical_name;
9986 }
9987 }
9988
9989 return name;
9990 }
9991
9992 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9993 If scope qualifiers are appropriate they will be added. The result
9994 will be allocated on the storage_obstack, or NULL if the DIE does
9995 not have a name. NAME may either be from a previous call to
9996 dwarf2_name or NULL.
9997
9998 The output string will be canonicalized (if C++). */
9999
10000 static const char *
10001 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10002 {
10003 return dwarf2_compute_name (name, die, cu, 0);
10004 }
10005
10006 /* Construct a physname for the given DIE in CU. NAME may either be
10007 from a previous call to dwarf2_name or NULL. The result will be
10008 allocated on the objfile_objstack or NULL if the DIE does not have a
10009 name.
10010
10011 The output string will be canonicalized (if C++). */
10012
10013 static const char *
10014 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10015 {
10016 struct objfile *objfile = cu->per_objfile->objfile;
10017 const char *retval, *mangled = NULL, *canon = NULL;
10018 int need_copy = 1;
10019
10020 /* In this case dwarf2_compute_name is just a shortcut not building anything
10021 on its own. */
10022 if (!die_needs_namespace (die, cu))
10023 return dwarf2_compute_name (name, die, cu, 1);
10024
10025 if (cu->language != language_rust)
10026 mangled = dw2_linkage_name (die, cu);
10027
10028 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10029 has computed. */
10030 gdb::unique_xmalloc_ptr<char> demangled;
10031 if (mangled != NULL)
10032 {
10033
10034 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10035 {
10036 /* Do nothing (do not demangle the symbol name). */
10037 }
10038 else
10039 {
10040 /* Use DMGL_RET_DROP for C++ template functions to suppress
10041 their return type. It is easier for GDB users to search
10042 for such functions as `name(params)' than `long name(params)'.
10043 In such case the minimal symbol names do not match the full
10044 symbol names but for template functions there is never a need
10045 to look up their definition from their declaration so
10046 the only disadvantage remains the minimal symbol variant
10047 `long name(params)' does not have the proper inferior type. */
10048 demangled.reset (gdb_demangle (mangled,
10049 (DMGL_PARAMS | DMGL_ANSI
10050 | DMGL_RET_DROP)));
10051 }
10052 if (demangled)
10053 canon = demangled.get ();
10054 else
10055 {
10056 canon = mangled;
10057 need_copy = 0;
10058 }
10059 }
10060
10061 if (canon == NULL || check_physname)
10062 {
10063 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10064
10065 if (canon != NULL && strcmp (physname, canon) != 0)
10066 {
10067 /* It may not mean a bug in GDB. The compiler could also
10068 compute DW_AT_linkage_name incorrectly. But in such case
10069 GDB would need to be bug-to-bug compatible. */
10070
10071 complaint (_("Computed physname <%s> does not match demangled <%s> "
10072 "(from linkage <%s>) - DIE at %s [in module %s]"),
10073 physname, canon, mangled, sect_offset_str (die->sect_off),
10074 objfile_name (objfile));
10075
10076 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10077 is available here - over computed PHYSNAME. It is safer
10078 against both buggy GDB and buggy compilers. */
10079
10080 retval = canon;
10081 }
10082 else
10083 {
10084 retval = physname;
10085 need_copy = 0;
10086 }
10087 }
10088 else
10089 retval = canon;
10090
10091 if (need_copy)
10092 retval = objfile->intern (retval);
10093
10094 return retval;
10095 }
10096
10097 /* Inspect DIE in CU for a namespace alias. If one exists, record
10098 a new symbol for it.
10099
10100 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10101
10102 static int
10103 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10104 {
10105 struct attribute *attr;
10106
10107 /* If the die does not have a name, this is not a namespace
10108 alias. */
10109 attr = dwarf2_attr (die, DW_AT_name, cu);
10110 if (attr != NULL)
10111 {
10112 int num;
10113 struct die_info *d = die;
10114 struct dwarf2_cu *imported_cu = cu;
10115
10116 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10117 keep inspecting DIEs until we hit the underlying import. */
10118 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10119 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10120 {
10121 attr = dwarf2_attr (d, DW_AT_import, cu);
10122 if (attr == NULL)
10123 break;
10124
10125 d = follow_die_ref (d, attr, &imported_cu);
10126 if (d->tag != DW_TAG_imported_declaration)
10127 break;
10128 }
10129
10130 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10131 {
10132 complaint (_("DIE at %s has too many recursively imported "
10133 "declarations"), sect_offset_str (d->sect_off));
10134 return 0;
10135 }
10136
10137 if (attr != NULL)
10138 {
10139 struct type *type;
10140 sect_offset sect_off = attr->get_ref_die_offset ();
10141
10142 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10143 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10144 {
10145 /* This declaration is a global namespace alias. Add
10146 a symbol for it whose type is the aliased namespace. */
10147 new_symbol (die, type, cu);
10148 return 1;
10149 }
10150 }
10151 }
10152
10153 return 0;
10154 }
10155
10156 /* Return the using directives repository (global or local?) to use in the
10157 current context for CU.
10158
10159 For Ada, imported declarations can materialize renamings, which *may* be
10160 global. However it is impossible (for now?) in DWARF to distinguish
10161 "external" imported declarations and "static" ones. As all imported
10162 declarations seem to be static in all other languages, make them all CU-wide
10163 global only in Ada. */
10164
10165 static struct using_direct **
10166 using_directives (struct dwarf2_cu *cu)
10167 {
10168 if (cu->language == language_ada
10169 && cu->get_builder ()->outermost_context_p ())
10170 return cu->get_builder ()->get_global_using_directives ();
10171 else
10172 return cu->get_builder ()->get_local_using_directives ();
10173 }
10174
10175 /* Read the import statement specified by the given die and record it. */
10176
10177 static void
10178 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10179 {
10180 struct objfile *objfile = cu->per_objfile->objfile;
10181 struct attribute *import_attr;
10182 struct die_info *imported_die, *child_die;
10183 struct dwarf2_cu *imported_cu;
10184 const char *imported_name;
10185 const char *imported_name_prefix;
10186 const char *canonical_name;
10187 const char *import_alias;
10188 const char *imported_declaration = NULL;
10189 const char *import_prefix;
10190 std::vector<const char *> excludes;
10191
10192 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10193 if (import_attr == NULL)
10194 {
10195 complaint (_("Tag '%s' has no DW_AT_import"),
10196 dwarf_tag_name (die->tag));
10197 return;
10198 }
10199
10200 imported_cu = cu;
10201 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10202 imported_name = dwarf2_name (imported_die, imported_cu);
10203 if (imported_name == NULL)
10204 {
10205 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10206
10207 The import in the following code:
10208 namespace A
10209 {
10210 typedef int B;
10211 }
10212
10213 int main ()
10214 {
10215 using A::B;
10216 B b;
10217 return b;
10218 }
10219
10220 ...
10221 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10222 <52> DW_AT_decl_file : 1
10223 <53> DW_AT_decl_line : 6
10224 <54> DW_AT_import : <0x75>
10225 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10226 <59> DW_AT_name : B
10227 <5b> DW_AT_decl_file : 1
10228 <5c> DW_AT_decl_line : 2
10229 <5d> DW_AT_type : <0x6e>
10230 ...
10231 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10232 <76> DW_AT_byte_size : 4
10233 <77> DW_AT_encoding : 5 (signed)
10234
10235 imports the wrong die ( 0x75 instead of 0x58 ).
10236 This case will be ignored until the gcc bug is fixed. */
10237 return;
10238 }
10239
10240 /* Figure out the local name after import. */
10241 import_alias = dwarf2_name (die, cu);
10242
10243 /* Figure out where the statement is being imported to. */
10244 import_prefix = determine_prefix (die, cu);
10245
10246 /* Figure out what the scope of the imported die is and prepend it
10247 to the name of the imported die. */
10248 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10249
10250 if (imported_die->tag != DW_TAG_namespace
10251 && imported_die->tag != DW_TAG_module)
10252 {
10253 imported_declaration = imported_name;
10254 canonical_name = imported_name_prefix;
10255 }
10256 else if (strlen (imported_name_prefix) > 0)
10257 canonical_name = obconcat (&objfile->objfile_obstack,
10258 imported_name_prefix,
10259 (cu->language == language_d ? "." : "::"),
10260 imported_name, (char *) NULL);
10261 else
10262 canonical_name = imported_name;
10263
10264 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10265 for (child_die = die->child; child_die && child_die->tag;
10266 child_die = child_die->sibling)
10267 {
10268 /* DWARF-4: A Fortran use statement with a “rename list” may be
10269 represented by an imported module entry with an import attribute
10270 referring to the module and owned entries corresponding to those
10271 entities that are renamed as part of being imported. */
10272
10273 if (child_die->tag != DW_TAG_imported_declaration)
10274 {
10275 complaint (_("child DW_TAG_imported_declaration expected "
10276 "- DIE at %s [in module %s]"),
10277 sect_offset_str (child_die->sect_off),
10278 objfile_name (objfile));
10279 continue;
10280 }
10281
10282 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10283 if (import_attr == NULL)
10284 {
10285 complaint (_("Tag '%s' has no DW_AT_import"),
10286 dwarf_tag_name (child_die->tag));
10287 continue;
10288 }
10289
10290 imported_cu = cu;
10291 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10292 &imported_cu);
10293 imported_name = dwarf2_name (imported_die, imported_cu);
10294 if (imported_name == NULL)
10295 {
10296 complaint (_("child DW_TAG_imported_declaration has unknown "
10297 "imported name - DIE at %s [in module %s]"),
10298 sect_offset_str (child_die->sect_off),
10299 objfile_name (objfile));
10300 continue;
10301 }
10302
10303 excludes.push_back (imported_name);
10304
10305 process_die (child_die, cu);
10306 }
10307
10308 add_using_directive (using_directives (cu),
10309 import_prefix,
10310 canonical_name,
10311 import_alias,
10312 imported_declaration,
10313 excludes,
10314 0,
10315 &objfile->objfile_obstack);
10316 }
10317
10318 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10319 types, but gives them a size of zero. Starting with version 14,
10320 ICC is compatible with GCC. */
10321
10322 static bool
10323 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10324 {
10325 if (!cu->checked_producer)
10326 check_producer (cu);
10327
10328 return cu->producer_is_icc_lt_14;
10329 }
10330
10331 /* ICC generates a DW_AT_type for C void functions. This was observed on
10332 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10333 which says that void functions should not have a DW_AT_type. */
10334
10335 static bool
10336 producer_is_icc (struct dwarf2_cu *cu)
10337 {
10338 if (!cu->checked_producer)
10339 check_producer (cu);
10340
10341 return cu->producer_is_icc;
10342 }
10343
10344 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10345 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10346 this, it was first present in GCC release 4.3.0. */
10347
10348 static bool
10349 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10350 {
10351 if (!cu->checked_producer)
10352 check_producer (cu);
10353
10354 return cu->producer_is_gcc_lt_4_3;
10355 }
10356
10357 static file_and_directory
10358 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10359 {
10360 file_and_directory res;
10361
10362 /* Find the filename. Do not use dwarf2_name here, since the filename
10363 is not a source language identifier. */
10364 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10365 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10366
10367 if (res.comp_dir == NULL
10368 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10369 && IS_ABSOLUTE_PATH (res.name))
10370 {
10371 res.comp_dir_storage = ldirname (res.name);
10372 if (!res.comp_dir_storage.empty ())
10373 res.comp_dir = res.comp_dir_storage.c_str ();
10374 }
10375 if (res.comp_dir != NULL)
10376 {
10377 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10378 directory, get rid of it. */
10379 const char *cp = strchr (res.comp_dir, ':');
10380
10381 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10382 res.comp_dir = cp + 1;
10383 }
10384
10385 if (res.name == NULL)
10386 res.name = "<unknown>";
10387
10388 return res;
10389 }
10390
10391 /* Handle DW_AT_stmt_list for a compilation unit.
10392 DIE is the DW_TAG_compile_unit die for CU.
10393 COMP_DIR is the compilation directory. LOWPC is passed to
10394 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10395
10396 static void
10397 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10398 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10399 {
10400 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10401 struct attribute *attr;
10402 struct line_header line_header_local;
10403 hashval_t line_header_local_hash;
10404 void **slot;
10405 int decode_mapping;
10406
10407 gdb_assert (! cu->per_cu->is_debug_types);
10408
10409 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10410 if (attr == NULL || !attr->form_is_unsigned ())
10411 return;
10412
10413 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10414
10415 /* The line header hash table is only created if needed (it exists to
10416 prevent redundant reading of the line table for partial_units).
10417 If we're given a partial_unit, we'll need it. If we're given a
10418 compile_unit, then use the line header hash table if it's already
10419 created, but don't create one just yet. */
10420
10421 if (per_objfile->line_header_hash == NULL
10422 && die->tag == DW_TAG_partial_unit)
10423 {
10424 per_objfile->line_header_hash
10425 .reset (htab_create_alloc (127, line_header_hash_voidp,
10426 line_header_eq_voidp,
10427 htab_delete_entry<line_header>,
10428 xcalloc, xfree));
10429 }
10430
10431 line_header_local.sect_off = line_offset;
10432 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10433 line_header_local_hash = line_header_hash (&line_header_local);
10434 if (per_objfile->line_header_hash != NULL)
10435 {
10436 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10437 &line_header_local,
10438 line_header_local_hash, NO_INSERT);
10439
10440 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10441 is not present in *SLOT (since if there is something in *SLOT then
10442 it will be for a partial_unit). */
10443 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10444 {
10445 gdb_assert (*slot != NULL);
10446 cu->line_header = (struct line_header *) *slot;
10447 return;
10448 }
10449 }
10450
10451 /* dwarf_decode_line_header does not yet provide sufficient information.
10452 We always have to call also dwarf_decode_lines for it. */
10453 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10454 if (lh == NULL)
10455 return;
10456
10457 cu->line_header = lh.release ();
10458 cu->line_header_die_owner = die;
10459
10460 if (per_objfile->line_header_hash == NULL)
10461 slot = NULL;
10462 else
10463 {
10464 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10465 &line_header_local,
10466 line_header_local_hash, INSERT);
10467 gdb_assert (slot != NULL);
10468 }
10469 if (slot != NULL && *slot == NULL)
10470 {
10471 /* This newly decoded line number information unit will be owned
10472 by line_header_hash hash table. */
10473 *slot = cu->line_header;
10474 cu->line_header_die_owner = NULL;
10475 }
10476 else
10477 {
10478 /* We cannot free any current entry in (*slot) as that struct line_header
10479 may be already used by multiple CUs. Create only temporary decoded
10480 line_header for this CU - it may happen at most once for each line
10481 number information unit. And if we're not using line_header_hash
10482 then this is what we want as well. */
10483 gdb_assert (die->tag != DW_TAG_partial_unit);
10484 }
10485 decode_mapping = (die->tag != DW_TAG_partial_unit);
10486 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10487 decode_mapping);
10488
10489 }
10490
10491 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10492
10493 static void
10494 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10495 {
10496 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10497 struct objfile *objfile = per_objfile->objfile;
10498 struct gdbarch *gdbarch = objfile->arch ();
10499 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10500 CORE_ADDR highpc = ((CORE_ADDR) 0);
10501 struct attribute *attr;
10502 struct die_info *child_die;
10503 CORE_ADDR baseaddr;
10504
10505 prepare_one_comp_unit (cu, die, cu->language);
10506 baseaddr = objfile->text_section_offset ();
10507
10508 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10509
10510 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10511 from finish_block. */
10512 if (lowpc == ((CORE_ADDR) -1))
10513 lowpc = highpc;
10514 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10515
10516 file_and_directory fnd = find_file_and_directory (die, cu);
10517
10518 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10519 standardised yet. As a workaround for the language detection we fall
10520 back to the DW_AT_producer string. */
10521 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10522 cu->language = language_opencl;
10523
10524 /* Similar hack for Go. */
10525 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10526 set_cu_language (DW_LANG_Go, cu);
10527
10528 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10529
10530 /* Decode line number information if present. We do this before
10531 processing child DIEs, so that the line header table is available
10532 for DW_AT_decl_file. */
10533 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10534
10535 /* Process all dies in compilation unit. */
10536 if (die->child != NULL)
10537 {
10538 child_die = die->child;
10539 while (child_die && child_die->tag)
10540 {
10541 process_die (child_die, cu);
10542 child_die = child_die->sibling;
10543 }
10544 }
10545
10546 /* Decode macro information, if present. Dwarf 2 macro information
10547 refers to information in the line number info statement program
10548 header, so we can only read it if we've read the header
10549 successfully. */
10550 attr = dwarf2_attr (die, DW_AT_macros, cu);
10551 if (attr == NULL)
10552 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10553 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10554 {
10555 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10556 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10557
10558 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10559 }
10560 else
10561 {
10562 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10563 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10564 {
10565 unsigned int macro_offset = attr->as_unsigned ();
10566
10567 dwarf_decode_macros (cu, macro_offset, 0);
10568 }
10569 }
10570 }
10571
10572 void
10573 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10574 {
10575 struct type_unit_group *tu_group;
10576 int first_time;
10577 struct attribute *attr;
10578 unsigned int i;
10579 struct signatured_type *sig_type;
10580
10581 gdb_assert (per_cu->is_debug_types);
10582 sig_type = (struct signatured_type *) per_cu;
10583
10584 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10585
10586 /* If we're using .gdb_index (includes -readnow) then
10587 per_cu->type_unit_group may not have been set up yet. */
10588 if (sig_type->type_unit_group == NULL)
10589 sig_type->type_unit_group = get_type_unit_group (this, attr);
10590 tu_group = sig_type->type_unit_group;
10591
10592 /* If we've already processed this stmt_list there's no real need to
10593 do it again, we could fake it and just recreate the part we need
10594 (file name,index -> symtab mapping). If data shows this optimization
10595 is useful we can do it then. */
10596 type_unit_group_unshareable *tug_unshare
10597 = per_objfile->get_type_unit_group_unshareable (tu_group);
10598 first_time = tug_unshare->compunit_symtab == NULL;
10599
10600 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10601 debug info. */
10602 line_header_up lh;
10603 if (attr != NULL && attr->form_is_unsigned ())
10604 {
10605 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10606 lh = dwarf_decode_line_header (line_offset, this);
10607 }
10608 if (lh == NULL)
10609 {
10610 if (first_time)
10611 start_symtab ("", NULL, 0);
10612 else
10613 {
10614 gdb_assert (tug_unshare->symtabs == NULL);
10615 gdb_assert (m_builder == nullptr);
10616 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10617 m_builder.reset (new struct buildsym_compunit
10618 (COMPUNIT_OBJFILE (cust), "",
10619 COMPUNIT_DIRNAME (cust),
10620 compunit_language (cust),
10621 0, cust));
10622 list_in_scope = get_builder ()->get_file_symbols ();
10623 }
10624 return;
10625 }
10626
10627 line_header = lh.release ();
10628 line_header_die_owner = die;
10629
10630 if (first_time)
10631 {
10632 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10633
10634 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10635 still initializing it, and our caller (a few levels up)
10636 process_full_type_unit still needs to know if this is the first
10637 time. */
10638
10639 tug_unshare->symtabs
10640 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10641 struct symtab *, line_header->file_names_size ());
10642
10643 auto &file_names = line_header->file_names ();
10644 for (i = 0; i < file_names.size (); ++i)
10645 {
10646 file_entry &fe = file_names[i];
10647 dwarf2_start_subfile (this, fe.name,
10648 fe.include_dir (line_header));
10649 buildsym_compunit *b = get_builder ();
10650 if (b->get_current_subfile ()->symtab == NULL)
10651 {
10652 /* NOTE: start_subfile will recognize when it's been
10653 passed a file it has already seen. So we can't
10654 assume there's a simple mapping from
10655 cu->line_header->file_names to subfiles, plus
10656 cu->line_header->file_names may contain dups. */
10657 b->get_current_subfile ()->symtab
10658 = allocate_symtab (cust, b->get_current_subfile ()->name);
10659 }
10660
10661 fe.symtab = b->get_current_subfile ()->symtab;
10662 tug_unshare->symtabs[i] = fe.symtab;
10663 }
10664 }
10665 else
10666 {
10667 gdb_assert (m_builder == nullptr);
10668 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10669 m_builder.reset (new struct buildsym_compunit
10670 (COMPUNIT_OBJFILE (cust), "",
10671 COMPUNIT_DIRNAME (cust),
10672 compunit_language (cust),
10673 0, cust));
10674 list_in_scope = get_builder ()->get_file_symbols ();
10675
10676 auto &file_names = line_header->file_names ();
10677 for (i = 0; i < file_names.size (); ++i)
10678 {
10679 file_entry &fe = file_names[i];
10680 fe.symtab = tug_unshare->symtabs[i];
10681 }
10682 }
10683
10684 /* The main symtab is allocated last. Type units don't have DW_AT_name
10685 so they don't have a "real" (so to speak) symtab anyway.
10686 There is later code that will assign the main symtab to all symbols
10687 that don't have one. We need to handle the case of a symbol with a
10688 missing symtab (DW_AT_decl_file) anyway. */
10689 }
10690
10691 /* Process DW_TAG_type_unit.
10692 For TUs we want to skip the first top level sibling if it's not the
10693 actual type being defined by this TU. In this case the first top
10694 level sibling is there to provide context only. */
10695
10696 static void
10697 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10698 {
10699 struct die_info *child_die;
10700
10701 prepare_one_comp_unit (cu, die, language_minimal);
10702
10703 /* Initialize (or reinitialize) the machinery for building symtabs.
10704 We do this before processing child DIEs, so that the line header table
10705 is available for DW_AT_decl_file. */
10706 cu->setup_type_unit_groups (die);
10707
10708 if (die->child != NULL)
10709 {
10710 child_die = die->child;
10711 while (child_die && child_die->tag)
10712 {
10713 process_die (child_die, cu);
10714 child_die = child_die->sibling;
10715 }
10716 }
10717 }
10718 \f
10719 /* DWO/DWP files.
10720
10721 http://gcc.gnu.org/wiki/DebugFission
10722 http://gcc.gnu.org/wiki/DebugFissionDWP
10723
10724 To simplify handling of both DWO files ("object" files with the DWARF info)
10725 and DWP files (a file with the DWOs packaged up into one file), we treat
10726 DWP files as having a collection of virtual DWO files. */
10727
10728 static hashval_t
10729 hash_dwo_file (const void *item)
10730 {
10731 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10732 hashval_t hash;
10733
10734 hash = htab_hash_string (dwo_file->dwo_name);
10735 if (dwo_file->comp_dir != NULL)
10736 hash += htab_hash_string (dwo_file->comp_dir);
10737 return hash;
10738 }
10739
10740 static int
10741 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10742 {
10743 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10744 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10745
10746 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10747 return 0;
10748 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10749 return lhs->comp_dir == rhs->comp_dir;
10750 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10751 }
10752
10753 /* Allocate a hash table for DWO files. */
10754
10755 static htab_up
10756 allocate_dwo_file_hash_table ()
10757 {
10758 return htab_up (htab_create_alloc (41,
10759 hash_dwo_file,
10760 eq_dwo_file,
10761 htab_delete_entry<dwo_file>,
10762 xcalloc, xfree));
10763 }
10764
10765 /* Lookup DWO file DWO_NAME. */
10766
10767 static void **
10768 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10769 const char *dwo_name,
10770 const char *comp_dir)
10771 {
10772 struct dwo_file find_entry;
10773 void **slot;
10774
10775 if (per_objfile->per_bfd->dwo_files == NULL)
10776 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10777
10778 find_entry.dwo_name = dwo_name;
10779 find_entry.comp_dir = comp_dir;
10780 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10781 INSERT);
10782
10783 return slot;
10784 }
10785
10786 static hashval_t
10787 hash_dwo_unit (const void *item)
10788 {
10789 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10790
10791 /* This drops the top 32 bits of the id, but is ok for a hash. */
10792 return dwo_unit->signature;
10793 }
10794
10795 static int
10796 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10797 {
10798 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10799 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10800
10801 /* The signature is assumed to be unique within the DWO file.
10802 So while object file CU dwo_id's always have the value zero,
10803 that's OK, assuming each object file DWO file has only one CU,
10804 and that's the rule for now. */
10805 return lhs->signature == rhs->signature;
10806 }
10807
10808 /* Allocate a hash table for DWO CUs,TUs.
10809 There is one of these tables for each of CUs,TUs for each DWO file. */
10810
10811 static htab_up
10812 allocate_dwo_unit_table ()
10813 {
10814 /* Start out with a pretty small number.
10815 Generally DWO files contain only one CU and maybe some TUs. */
10816 return htab_up (htab_create_alloc (3,
10817 hash_dwo_unit,
10818 eq_dwo_unit,
10819 NULL, xcalloc, xfree));
10820 }
10821
10822 /* die_reader_func for create_dwo_cu. */
10823
10824 static void
10825 create_dwo_cu_reader (const struct die_reader_specs *reader,
10826 const gdb_byte *info_ptr,
10827 struct die_info *comp_unit_die,
10828 struct dwo_file *dwo_file,
10829 struct dwo_unit *dwo_unit)
10830 {
10831 struct dwarf2_cu *cu = reader->cu;
10832 sect_offset sect_off = cu->per_cu->sect_off;
10833 struct dwarf2_section_info *section = cu->per_cu->section;
10834
10835 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10836 if (!signature.has_value ())
10837 {
10838 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10839 " its dwo_id [in module %s]"),
10840 sect_offset_str (sect_off), dwo_file->dwo_name);
10841 return;
10842 }
10843
10844 dwo_unit->dwo_file = dwo_file;
10845 dwo_unit->signature = *signature;
10846 dwo_unit->section = section;
10847 dwo_unit->sect_off = sect_off;
10848 dwo_unit->length = cu->per_cu->length;
10849
10850 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10851 sect_offset_str (sect_off),
10852 hex_string (dwo_unit->signature));
10853 }
10854
10855 /* Create the dwo_units for the CUs in a DWO_FILE.
10856 Note: This function processes DWO files only, not DWP files. */
10857
10858 static void
10859 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10860 dwarf2_cu *cu, struct dwo_file &dwo_file,
10861 dwarf2_section_info &section, htab_up &cus_htab)
10862 {
10863 struct objfile *objfile = per_objfile->objfile;
10864 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10865 const gdb_byte *info_ptr, *end_ptr;
10866
10867 section.read (objfile);
10868 info_ptr = section.buffer;
10869
10870 if (info_ptr == NULL)
10871 return;
10872
10873 dwarf_read_debug_printf ("Reading %s for %s:",
10874 section.get_name (),
10875 section.get_file_name ());
10876
10877 end_ptr = info_ptr + section.size;
10878 while (info_ptr < end_ptr)
10879 {
10880 struct dwarf2_per_cu_data per_cu;
10881 struct dwo_unit read_unit {};
10882 struct dwo_unit *dwo_unit;
10883 void **slot;
10884 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10885
10886 per_cu.per_bfd = per_bfd;
10887 per_cu.is_debug_types = 0;
10888 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10889 per_cu.section = &section;
10890
10891 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
10892 if (!reader.dummy_p)
10893 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
10894 &dwo_file, &read_unit);
10895 info_ptr += per_cu.length;
10896
10897 // If the unit could not be parsed, skip it.
10898 if (read_unit.dwo_file == NULL)
10899 continue;
10900
10901 if (cus_htab == NULL)
10902 cus_htab = allocate_dwo_unit_table ();
10903
10904 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
10905 struct dwo_unit);
10906 *dwo_unit = read_unit;
10907 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
10908 gdb_assert (slot != NULL);
10909 if (*slot != NULL)
10910 {
10911 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10912 sect_offset dup_sect_off = dup_cu->sect_off;
10913
10914 complaint (_("debug cu entry at offset %s is duplicate to"
10915 " the entry at offset %s, signature %s"),
10916 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
10917 hex_string (dwo_unit->signature));
10918 }
10919 *slot = (void *)dwo_unit;
10920 }
10921 }
10922
10923 /* DWP file .debug_{cu,tu}_index section format:
10924 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10925 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10926
10927 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10928 officially standard DWP format was published with DWARF v5 and is called
10929 Version 5. There are no versions 3 or 4.
10930
10931 DWP Version 1:
10932
10933 Both index sections have the same format, and serve to map a 64-bit
10934 signature to a set of section numbers. Each section begins with a header,
10935 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10936 indexes, and a pool of 32-bit section numbers. The index sections will be
10937 aligned at 8-byte boundaries in the file.
10938
10939 The index section header consists of:
10940
10941 V, 32 bit version number
10942 -, 32 bits unused
10943 N, 32 bit number of compilation units or type units in the index
10944 M, 32 bit number of slots in the hash table
10945
10946 Numbers are recorded using the byte order of the application binary.
10947
10948 The hash table begins at offset 16 in the section, and consists of an array
10949 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10950 order of the application binary). Unused slots in the hash table are 0.
10951 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10952
10953 The parallel table begins immediately after the hash table
10954 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10955 array of 32-bit indexes (using the byte order of the application binary),
10956 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10957 table contains a 32-bit index into the pool of section numbers. For unused
10958 hash table slots, the corresponding entry in the parallel table will be 0.
10959
10960 The pool of section numbers begins immediately following the hash table
10961 (at offset 16 + 12 * M from the beginning of the section). The pool of
10962 section numbers consists of an array of 32-bit words (using the byte order
10963 of the application binary). Each item in the array is indexed starting
10964 from 0. The hash table entry provides the index of the first section
10965 number in the set. Additional section numbers in the set follow, and the
10966 set is terminated by a 0 entry (section number 0 is not used in ELF).
10967
10968 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10969 section must be the first entry in the set, and the .debug_abbrev.dwo must
10970 be the second entry. Other members of the set may follow in any order.
10971
10972 ---
10973
10974 DWP Versions 2 and 5:
10975
10976 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10977 and the entries in the index tables are now offsets into these sections.
10978 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10979 section.
10980
10981 Index Section Contents:
10982 Header
10983 Hash Table of Signatures dwp_hash_table.hash_table
10984 Parallel Table of Indices dwp_hash_table.unit_table
10985 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10986 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10987
10988 The index section header consists of:
10989
10990 V, 32 bit version number
10991 L, 32 bit number of columns in the table of section offsets
10992 N, 32 bit number of compilation units or type units in the index
10993 M, 32 bit number of slots in the hash table
10994
10995 Numbers are recorded using the byte order of the application binary.
10996
10997 The hash table has the same format as version 1.
10998 The parallel table of indices has the same format as version 1,
10999 except that the entries are origin-1 indices into the table of sections
11000 offsets and the table of section sizes.
11001
11002 The table of offsets begins immediately following the parallel table
11003 (at offset 16 + 12 * M from the beginning of the section). The table is
11004 a two-dimensional array of 32-bit words (using the byte order of the
11005 application binary), with L columns and N+1 rows, in row-major order.
11006 Each row in the array is indexed starting from 0. The first row provides
11007 a key to the remaining rows: each column in this row provides an identifier
11008 for a debug section, and the offsets in the same column of subsequent rows
11009 refer to that section. The section identifiers for Version 2 are:
11010
11011 DW_SECT_INFO 1 .debug_info.dwo
11012 DW_SECT_TYPES 2 .debug_types.dwo
11013 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11014 DW_SECT_LINE 4 .debug_line.dwo
11015 DW_SECT_LOC 5 .debug_loc.dwo
11016 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11017 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11018 DW_SECT_MACRO 8 .debug_macro.dwo
11019
11020 The section identifiers for Version 5 are:
11021
11022 DW_SECT_INFO_V5 1 .debug_info.dwo
11023 DW_SECT_RESERVED_V5 2 --
11024 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11025 DW_SECT_LINE_V5 4 .debug_line.dwo
11026 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11027 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11028 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11029 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11030
11031 The offsets provided by the CU and TU index sections are the base offsets
11032 for the contributions made by each CU or TU to the corresponding section
11033 in the package file. Each CU and TU header contains an abbrev_offset
11034 field, used to find the abbreviations table for that CU or TU within the
11035 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11036 be interpreted as relative to the base offset given in the index section.
11037 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11038 should be interpreted as relative to the base offset for .debug_line.dwo,
11039 and offsets into other debug sections obtained from DWARF attributes should
11040 also be interpreted as relative to the corresponding base offset.
11041
11042 The table of sizes begins immediately following the table of offsets.
11043 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11044 with L columns and N rows, in row-major order. Each row in the array is
11045 indexed starting from 1 (row 0 is shared by the two tables).
11046
11047 ---
11048
11049 Hash table lookup is handled the same in version 1 and 2:
11050
11051 We assume that N and M will not exceed 2^32 - 1.
11052 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11053
11054 Given a 64-bit compilation unit signature or a type signature S, an entry
11055 in the hash table is located as follows:
11056
11057 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11058 the low-order k bits all set to 1.
11059
11060 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11061
11062 3) If the hash table entry at index H matches the signature, use that
11063 entry. If the hash table entry at index H is unused (all zeroes),
11064 terminate the search: the signature is not present in the table.
11065
11066 4) Let H = (H + H') modulo M. Repeat at Step 3.
11067
11068 Because M > N and H' and M are relatively prime, the search is guaranteed
11069 to stop at an unused slot or find the match. */
11070
11071 /* Create a hash table to map DWO IDs to their CU/TU entry in
11072 .debug_{info,types}.dwo in DWP_FILE.
11073 Returns NULL if there isn't one.
11074 Note: This function processes DWP files only, not DWO files. */
11075
11076 static struct dwp_hash_table *
11077 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11078 struct dwp_file *dwp_file, int is_debug_types)
11079 {
11080 struct objfile *objfile = per_objfile->objfile;
11081 bfd *dbfd = dwp_file->dbfd.get ();
11082 const gdb_byte *index_ptr, *index_end;
11083 struct dwarf2_section_info *index;
11084 uint32_t version, nr_columns, nr_units, nr_slots;
11085 struct dwp_hash_table *htab;
11086
11087 if (is_debug_types)
11088 index = &dwp_file->sections.tu_index;
11089 else
11090 index = &dwp_file->sections.cu_index;
11091
11092 if (index->empty ())
11093 return NULL;
11094 index->read (objfile);
11095
11096 index_ptr = index->buffer;
11097 index_end = index_ptr + index->size;
11098
11099 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11100 For now it's safe to just read 4 bytes (particularly as it's difficult to
11101 tell if you're dealing with Version 5 before you've read the version). */
11102 version = read_4_bytes (dbfd, index_ptr);
11103 index_ptr += 4;
11104 if (version == 2 || version == 5)
11105 nr_columns = read_4_bytes (dbfd, index_ptr);
11106 else
11107 nr_columns = 0;
11108 index_ptr += 4;
11109 nr_units = read_4_bytes (dbfd, index_ptr);
11110 index_ptr += 4;
11111 nr_slots = read_4_bytes (dbfd, index_ptr);
11112 index_ptr += 4;
11113
11114 if (version != 1 && version != 2 && version != 5)
11115 {
11116 error (_("Dwarf Error: unsupported DWP file version (%s)"
11117 " [in module %s]"),
11118 pulongest (version), dwp_file->name);
11119 }
11120 if (nr_slots != (nr_slots & -nr_slots))
11121 {
11122 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11123 " is not power of 2 [in module %s]"),
11124 pulongest (nr_slots), dwp_file->name);
11125 }
11126
11127 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11128 htab->version = version;
11129 htab->nr_columns = nr_columns;
11130 htab->nr_units = nr_units;
11131 htab->nr_slots = nr_slots;
11132 htab->hash_table = index_ptr;
11133 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11134
11135 /* Exit early if the table is empty. */
11136 if (nr_slots == 0 || nr_units == 0
11137 || (version == 2 && nr_columns == 0)
11138 || (version == 5 && nr_columns == 0))
11139 {
11140 /* All must be zero. */
11141 if (nr_slots != 0 || nr_units != 0
11142 || (version == 2 && nr_columns != 0)
11143 || (version == 5 && nr_columns != 0))
11144 {
11145 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11146 " all zero [in modules %s]"),
11147 dwp_file->name);
11148 }
11149 return htab;
11150 }
11151
11152 if (version == 1)
11153 {
11154 htab->section_pool.v1.indices =
11155 htab->unit_table + sizeof (uint32_t) * nr_slots;
11156 /* It's harder to decide whether the section is too small in v1.
11157 V1 is deprecated anyway so we punt. */
11158 }
11159 else if (version == 2)
11160 {
11161 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11162 int *ids = htab->section_pool.v2.section_ids;
11163 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11164 /* Reverse map for error checking. */
11165 int ids_seen[DW_SECT_MAX + 1];
11166 int i;
11167
11168 if (nr_columns < 2)
11169 {
11170 error (_("Dwarf Error: bad DWP hash table, too few columns"
11171 " in section table [in module %s]"),
11172 dwp_file->name);
11173 }
11174 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11175 {
11176 error (_("Dwarf Error: bad DWP hash table, too many columns"
11177 " in section table [in module %s]"),
11178 dwp_file->name);
11179 }
11180 memset (ids, 255, sizeof_ids);
11181 memset (ids_seen, 255, sizeof (ids_seen));
11182 for (i = 0; i < nr_columns; ++i)
11183 {
11184 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11185
11186 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11187 {
11188 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11189 " in section table [in module %s]"),
11190 id, dwp_file->name);
11191 }
11192 if (ids_seen[id] != -1)
11193 {
11194 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11195 " id %d in section table [in module %s]"),
11196 id, dwp_file->name);
11197 }
11198 ids_seen[id] = i;
11199 ids[i] = id;
11200 }
11201 /* Must have exactly one info or types section. */
11202 if (((ids_seen[DW_SECT_INFO] != -1)
11203 + (ids_seen[DW_SECT_TYPES] != -1))
11204 != 1)
11205 {
11206 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11207 " DWO info/types section [in module %s]"),
11208 dwp_file->name);
11209 }
11210 /* Must have an abbrev section. */
11211 if (ids_seen[DW_SECT_ABBREV] == -1)
11212 {
11213 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11214 " section [in module %s]"),
11215 dwp_file->name);
11216 }
11217 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11218 htab->section_pool.v2.sizes =
11219 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11220 * nr_units * nr_columns);
11221 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11222 * nr_units * nr_columns))
11223 > index_end)
11224 {
11225 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11226 " [in module %s]"),
11227 dwp_file->name);
11228 }
11229 }
11230 else /* version == 5 */
11231 {
11232 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11233 int *ids = htab->section_pool.v5.section_ids;
11234 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11235 /* Reverse map for error checking. */
11236 int ids_seen[DW_SECT_MAX_V5 + 1];
11237
11238 if (nr_columns < 2)
11239 {
11240 error (_("Dwarf Error: bad DWP hash table, too few columns"
11241 " in section table [in module %s]"),
11242 dwp_file->name);
11243 }
11244 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11245 {
11246 error (_("Dwarf Error: bad DWP hash table, too many columns"
11247 " in section table [in module %s]"),
11248 dwp_file->name);
11249 }
11250 memset (ids, 255, sizeof_ids);
11251 memset (ids_seen, 255, sizeof (ids_seen));
11252 for (int i = 0; i < nr_columns; ++i)
11253 {
11254 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11255
11256 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11257 {
11258 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11259 " in section table [in module %s]"),
11260 id, dwp_file->name);
11261 }
11262 if (ids_seen[id] != -1)
11263 {
11264 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11265 " id %d in section table [in module %s]"),
11266 id, dwp_file->name);
11267 }
11268 ids_seen[id] = i;
11269 ids[i] = id;
11270 }
11271 /* Must have seen an info section. */
11272 if (ids_seen[DW_SECT_INFO_V5] == -1)
11273 {
11274 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11275 " DWO info/types section [in module %s]"),
11276 dwp_file->name);
11277 }
11278 /* Must have an abbrev section. */
11279 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11280 {
11281 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11282 " section [in module %s]"),
11283 dwp_file->name);
11284 }
11285 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11286 htab->section_pool.v5.sizes
11287 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11288 * nr_units * nr_columns);
11289 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11290 * nr_units * nr_columns))
11291 > index_end)
11292 {
11293 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11294 " [in module %s]"),
11295 dwp_file->name);
11296 }
11297 }
11298
11299 return htab;
11300 }
11301
11302 /* Update SECTIONS with the data from SECTP.
11303
11304 This function is like the other "locate" section routines, but in
11305 this context the sections to read comes from the DWP V1 hash table,
11306 not the full ELF section table.
11307
11308 The result is non-zero for success, or zero if an error was found. */
11309
11310 static int
11311 locate_v1_virtual_dwo_sections (asection *sectp,
11312 struct virtual_v1_dwo_sections *sections)
11313 {
11314 const struct dwop_section_names *names = &dwop_section_names;
11315
11316 if (names->abbrev_dwo.matches (sectp->name))
11317 {
11318 /* There can be only one. */
11319 if (sections->abbrev.s.section != NULL)
11320 return 0;
11321 sections->abbrev.s.section = sectp;
11322 sections->abbrev.size = bfd_section_size (sectp);
11323 }
11324 else if (names->info_dwo.matches (sectp->name)
11325 || names->types_dwo.matches (sectp->name))
11326 {
11327 /* There can be only one. */
11328 if (sections->info_or_types.s.section != NULL)
11329 return 0;
11330 sections->info_or_types.s.section = sectp;
11331 sections->info_or_types.size = bfd_section_size (sectp);
11332 }
11333 else if (names->line_dwo.matches (sectp->name))
11334 {
11335 /* There can be only one. */
11336 if (sections->line.s.section != NULL)
11337 return 0;
11338 sections->line.s.section = sectp;
11339 sections->line.size = bfd_section_size (sectp);
11340 }
11341 else if (names->loc_dwo.matches (sectp->name))
11342 {
11343 /* There can be only one. */
11344 if (sections->loc.s.section != NULL)
11345 return 0;
11346 sections->loc.s.section = sectp;
11347 sections->loc.size = bfd_section_size (sectp);
11348 }
11349 else if (names->macinfo_dwo.matches (sectp->name))
11350 {
11351 /* There can be only one. */
11352 if (sections->macinfo.s.section != NULL)
11353 return 0;
11354 sections->macinfo.s.section = sectp;
11355 sections->macinfo.size = bfd_section_size (sectp);
11356 }
11357 else if (names->macro_dwo.matches (sectp->name))
11358 {
11359 /* There can be only one. */
11360 if (sections->macro.s.section != NULL)
11361 return 0;
11362 sections->macro.s.section = sectp;
11363 sections->macro.size = bfd_section_size (sectp);
11364 }
11365 else if (names->str_offsets_dwo.matches (sectp->name))
11366 {
11367 /* There can be only one. */
11368 if (sections->str_offsets.s.section != NULL)
11369 return 0;
11370 sections->str_offsets.s.section = sectp;
11371 sections->str_offsets.size = bfd_section_size (sectp);
11372 }
11373 else
11374 {
11375 /* No other kind of section is valid. */
11376 return 0;
11377 }
11378
11379 return 1;
11380 }
11381
11382 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11383 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11384 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11385 This is for DWP version 1 files. */
11386
11387 static struct dwo_unit *
11388 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11389 struct dwp_file *dwp_file,
11390 uint32_t unit_index,
11391 const char *comp_dir,
11392 ULONGEST signature, int is_debug_types)
11393 {
11394 const struct dwp_hash_table *dwp_htab =
11395 is_debug_types ? dwp_file->tus : dwp_file->cus;
11396 bfd *dbfd = dwp_file->dbfd.get ();
11397 const char *kind = is_debug_types ? "TU" : "CU";
11398 struct dwo_file *dwo_file;
11399 struct dwo_unit *dwo_unit;
11400 struct virtual_v1_dwo_sections sections;
11401 void **dwo_file_slot;
11402 int i;
11403
11404 gdb_assert (dwp_file->version == 1);
11405
11406 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11407 kind, pulongest (unit_index), hex_string (signature),
11408 dwp_file->name);
11409
11410 /* Fetch the sections of this DWO unit.
11411 Put a limit on the number of sections we look for so that bad data
11412 doesn't cause us to loop forever. */
11413
11414 #define MAX_NR_V1_DWO_SECTIONS \
11415 (1 /* .debug_info or .debug_types */ \
11416 + 1 /* .debug_abbrev */ \
11417 + 1 /* .debug_line */ \
11418 + 1 /* .debug_loc */ \
11419 + 1 /* .debug_str_offsets */ \
11420 + 1 /* .debug_macro or .debug_macinfo */ \
11421 + 1 /* trailing zero */)
11422
11423 memset (&sections, 0, sizeof (sections));
11424
11425 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11426 {
11427 asection *sectp;
11428 uint32_t section_nr =
11429 read_4_bytes (dbfd,
11430 dwp_htab->section_pool.v1.indices
11431 + (unit_index + i) * sizeof (uint32_t));
11432
11433 if (section_nr == 0)
11434 break;
11435 if (section_nr >= dwp_file->num_sections)
11436 {
11437 error (_("Dwarf Error: bad DWP hash table, section number too large"
11438 " [in module %s]"),
11439 dwp_file->name);
11440 }
11441
11442 sectp = dwp_file->elf_sections[section_nr];
11443 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11444 {
11445 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11446 " [in module %s]"),
11447 dwp_file->name);
11448 }
11449 }
11450
11451 if (i < 2
11452 || sections.info_or_types.empty ()
11453 || sections.abbrev.empty ())
11454 {
11455 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11456 " [in module %s]"),
11457 dwp_file->name);
11458 }
11459 if (i == MAX_NR_V1_DWO_SECTIONS)
11460 {
11461 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11462 " [in module %s]"),
11463 dwp_file->name);
11464 }
11465
11466 /* It's easier for the rest of the code if we fake a struct dwo_file and
11467 have dwo_unit "live" in that. At least for now.
11468
11469 The DWP file can be made up of a random collection of CUs and TUs.
11470 However, for each CU + set of TUs that came from the same original DWO
11471 file, we can combine them back into a virtual DWO file to save space
11472 (fewer struct dwo_file objects to allocate). Remember that for really
11473 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11474
11475 std::string virtual_dwo_name =
11476 string_printf ("virtual-dwo/%d-%d-%d-%d",
11477 sections.abbrev.get_id (),
11478 sections.line.get_id (),
11479 sections.loc.get_id (),
11480 sections.str_offsets.get_id ());
11481 /* Can we use an existing virtual DWO file? */
11482 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11483 comp_dir);
11484 /* Create one if necessary. */
11485 if (*dwo_file_slot == NULL)
11486 {
11487 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11488 virtual_dwo_name.c_str ());
11489
11490 dwo_file = new struct dwo_file;
11491 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11492 dwo_file->comp_dir = comp_dir;
11493 dwo_file->sections.abbrev = sections.abbrev;
11494 dwo_file->sections.line = sections.line;
11495 dwo_file->sections.loc = sections.loc;
11496 dwo_file->sections.macinfo = sections.macinfo;
11497 dwo_file->sections.macro = sections.macro;
11498 dwo_file->sections.str_offsets = sections.str_offsets;
11499 /* The "str" section is global to the entire DWP file. */
11500 dwo_file->sections.str = dwp_file->sections.str;
11501 /* The info or types section is assigned below to dwo_unit,
11502 there's no need to record it in dwo_file.
11503 Also, we can't simply record type sections in dwo_file because
11504 we record a pointer into the vector in dwo_unit. As we collect more
11505 types we'll grow the vector and eventually have to reallocate space
11506 for it, invalidating all copies of pointers into the previous
11507 contents. */
11508 *dwo_file_slot = dwo_file;
11509 }
11510 else
11511 {
11512 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11513 virtual_dwo_name.c_str ());
11514
11515 dwo_file = (struct dwo_file *) *dwo_file_slot;
11516 }
11517
11518 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11519 dwo_unit->dwo_file = dwo_file;
11520 dwo_unit->signature = signature;
11521 dwo_unit->section =
11522 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11523 *dwo_unit->section = sections.info_or_types;
11524 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11525
11526 return dwo_unit;
11527 }
11528
11529 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11530 simplify them. Given a pointer to the containing section SECTION, and
11531 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11532 virtual section of just that piece. */
11533
11534 static struct dwarf2_section_info
11535 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11536 struct dwarf2_section_info *section,
11537 bfd_size_type offset, bfd_size_type size)
11538 {
11539 struct dwarf2_section_info result;
11540 asection *sectp;
11541
11542 gdb_assert (section != NULL);
11543 gdb_assert (!section->is_virtual);
11544
11545 memset (&result, 0, sizeof (result));
11546 result.s.containing_section = section;
11547 result.is_virtual = true;
11548
11549 if (size == 0)
11550 return result;
11551
11552 sectp = section->get_bfd_section ();
11553
11554 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11555 bounds of the real section. This is a pretty-rare event, so just
11556 flag an error (easier) instead of a warning and trying to cope. */
11557 if (sectp == NULL
11558 || offset + size > bfd_section_size (sectp))
11559 {
11560 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11561 " in section %s [in module %s]"),
11562 sectp ? bfd_section_name (sectp) : "<unknown>",
11563 objfile_name (per_objfile->objfile));
11564 }
11565
11566 result.virtual_offset = offset;
11567 result.size = size;
11568 return result;
11569 }
11570
11571 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11572 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11573 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11574 This is for DWP version 2 files. */
11575
11576 static struct dwo_unit *
11577 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11578 struct dwp_file *dwp_file,
11579 uint32_t unit_index,
11580 const char *comp_dir,
11581 ULONGEST signature, int is_debug_types)
11582 {
11583 const struct dwp_hash_table *dwp_htab =
11584 is_debug_types ? dwp_file->tus : dwp_file->cus;
11585 bfd *dbfd = dwp_file->dbfd.get ();
11586 const char *kind = is_debug_types ? "TU" : "CU";
11587 struct dwo_file *dwo_file;
11588 struct dwo_unit *dwo_unit;
11589 struct virtual_v2_or_v5_dwo_sections sections;
11590 void **dwo_file_slot;
11591 int i;
11592
11593 gdb_assert (dwp_file->version == 2);
11594
11595 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11596 kind, pulongest (unit_index), hex_string (signature),
11597 dwp_file->name);
11598
11599 /* Fetch the section offsets of this DWO unit. */
11600
11601 memset (&sections, 0, sizeof (sections));
11602
11603 for (i = 0; i < dwp_htab->nr_columns; ++i)
11604 {
11605 uint32_t offset = read_4_bytes (dbfd,
11606 dwp_htab->section_pool.v2.offsets
11607 + (((unit_index - 1) * dwp_htab->nr_columns
11608 + i)
11609 * sizeof (uint32_t)));
11610 uint32_t size = read_4_bytes (dbfd,
11611 dwp_htab->section_pool.v2.sizes
11612 + (((unit_index - 1) * dwp_htab->nr_columns
11613 + i)
11614 * sizeof (uint32_t)));
11615
11616 switch (dwp_htab->section_pool.v2.section_ids[i])
11617 {
11618 case DW_SECT_INFO:
11619 case DW_SECT_TYPES:
11620 sections.info_or_types_offset = offset;
11621 sections.info_or_types_size = size;
11622 break;
11623 case DW_SECT_ABBREV:
11624 sections.abbrev_offset = offset;
11625 sections.abbrev_size = size;
11626 break;
11627 case DW_SECT_LINE:
11628 sections.line_offset = offset;
11629 sections.line_size = size;
11630 break;
11631 case DW_SECT_LOC:
11632 sections.loc_offset = offset;
11633 sections.loc_size = size;
11634 break;
11635 case DW_SECT_STR_OFFSETS:
11636 sections.str_offsets_offset = offset;
11637 sections.str_offsets_size = size;
11638 break;
11639 case DW_SECT_MACINFO:
11640 sections.macinfo_offset = offset;
11641 sections.macinfo_size = size;
11642 break;
11643 case DW_SECT_MACRO:
11644 sections.macro_offset = offset;
11645 sections.macro_size = size;
11646 break;
11647 }
11648 }
11649
11650 /* It's easier for the rest of the code if we fake a struct dwo_file and
11651 have dwo_unit "live" in that. At least for now.
11652
11653 The DWP file can be made up of a random collection of CUs and TUs.
11654 However, for each CU + set of TUs that came from the same original DWO
11655 file, we can combine them back into a virtual DWO file to save space
11656 (fewer struct dwo_file objects to allocate). Remember that for really
11657 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11658
11659 std::string virtual_dwo_name =
11660 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11661 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11662 (long) (sections.line_size ? sections.line_offset : 0),
11663 (long) (sections.loc_size ? sections.loc_offset : 0),
11664 (long) (sections.str_offsets_size
11665 ? sections.str_offsets_offset : 0));
11666 /* Can we use an existing virtual DWO file? */
11667 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11668 comp_dir);
11669 /* Create one if necessary. */
11670 if (*dwo_file_slot == NULL)
11671 {
11672 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11673 virtual_dwo_name.c_str ());
11674
11675 dwo_file = new struct dwo_file;
11676 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11677 dwo_file->comp_dir = comp_dir;
11678 dwo_file->sections.abbrev =
11679 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11680 sections.abbrev_offset,
11681 sections.abbrev_size);
11682 dwo_file->sections.line =
11683 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11684 sections.line_offset,
11685 sections.line_size);
11686 dwo_file->sections.loc =
11687 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11688 sections.loc_offset, sections.loc_size);
11689 dwo_file->sections.macinfo =
11690 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11691 sections.macinfo_offset,
11692 sections.macinfo_size);
11693 dwo_file->sections.macro =
11694 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11695 sections.macro_offset,
11696 sections.macro_size);
11697 dwo_file->sections.str_offsets =
11698 create_dwp_v2_or_v5_section (per_objfile,
11699 &dwp_file->sections.str_offsets,
11700 sections.str_offsets_offset,
11701 sections.str_offsets_size);
11702 /* The "str" section is global to the entire DWP file. */
11703 dwo_file->sections.str = dwp_file->sections.str;
11704 /* The info or types section is assigned below to dwo_unit,
11705 there's no need to record it in dwo_file.
11706 Also, we can't simply record type sections in dwo_file because
11707 we record a pointer into the vector in dwo_unit. As we collect more
11708 types we'll grow the vector and eventually have to reallocate space
11709 for it, invalidating all copies of pointers into the previous
11710 contents. */
11711 *dwo_file_slot = dwo_file;
11712 }
11713 else
11714 {
11715 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11716 virtual_dwo_name.c_str ());
11717
11718 dwo_file = (struct dwo_file *) *dwo_file_slot;
11719 }
11720
11721 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11722 dwo_unit->dwo_file = dwo_file;
11723 dwo_unit->signature = signature;
11724 dwo_unit->section =
11725 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11726 *dwo_unit->section = create_dwp_v2_or_v5_section
11727 (per_objfile,
11728 is_debug_types
11729 ? &dwp_file->sections.types
11730 : &dwp_file->sections.info,
11731 sections.info_or_types_offset,
11732 sections.info_or_types_size);
11733 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11734
11735 return dwo_unit;
11736 }
11737
11738 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11739 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11740 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11741 This is for DWP version 5 files. */
11742
11743 static struct dwo_unit *
11744 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11745 struct dwp_file *dwp_file,
11746 uint32_t unit_index,
11747 const char *comp_dir,
11748 ULONGEST signature, int is_debug_types)
11749 {
11750 const struct dwp_hash_table *dwp_htab
11751 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11752 bfd *dbfd = dwp_file->dbfd.get ();
11753 const char *kind = is_debug_types ? "TU" : "CU";
11754 struct dwo_file *dwo_file;
11755 struct dwo_unit *dwo_unit;
11756 struct virtual_v2_or_v5_dwo_sections sections {};
11757 void **dwo_file_slot;
11758
11759 gdb_assert (dwp_file->version == 5);
11760
11761 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11762 kind, pulongest (unit_index), hex_string (signature),
11763 dwp_file->name);
11764
11765 /* Fetch the section offsets of this DWO unit. */
11766
11767 /* memset (&sections, 0, sizeof (sections)); */
11768
11769 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11770 {
11771 uint32_t offset = read_4_bytes (dbfd,
11772 dwp_htab->section_pool.v5.offsets
11773 + (((unit_index - 1)
11774 * dwp_htab->nr_columns
11775 + i)
11776 * sizeof (uint32_t)));
11777 uint32_t size = read_4_bytes (dbfd,
11778 dwp_htab->section_pool.v5.sizes
11779 + (((unit_index - 1) * dwp_htab->nr_columns
11780 + i)
11781 * sizeof (uint32_t)));
11782
11783 switch (dwp_htab->section_pool.v5.section_ids[i])
11784 {
11785 case DW_SECT_ABBREV_V5:
11786 sections.abbrev_offset = offset;
11787 sections.abbrev_size = size;
11788 break;
11789 case DW_SECT_INFO_V5:
11790 sections.info_or_types_offset = offset;
11791 sections.info_or_types_size = size;
11792 break;
11793 case DW_SECT_LINE_V5:
11794 sections.line_offset = offset;
11795 sections.line_size = size;
11796 break;
11797 case DW_SECT_LOCLISTS_V5:
11798 sections.loclists_offset = offset;
11799 sections.loclists_size = size;
11800 break;
11801 case DW_SECT_MACRO_V5:
11802 sections.macro_offset = offset;
11803 sections.macro_size = size;
11804 break;
11805 case DW_SECT_RNGLISTS_V5:
11806 sections.rnglists_offset = offset;
11807 sections.rnglists_size = size;
11808 break;
11809 case DW_SECT_STR_OFFSETS_V5:
11810 sections.str_offsets_offset = offset;
11811 sections.str_offsets_size = size;
11812 break;
11813 case DW_SECT_RESERVED_V5:
11814 default:
11815 break;
11816 }
11817 }
11818
11819 /* It's easier for the rest of the code if we fake a struct dwo_file and
11820 have dwo_unit "live" in that. At least for now.
11821
11822 The DWP file can be made up of a random collection of CUs and TUs.
11823 However, for each CU + set of TUs that came from the same original DWO
11824 file, we can combine them back into a virtual DWO file to save space
11825 (fewer struct dwo_file objects to allocate). Remember that for really
11826 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11827
11828 std::string virtual_dwo_name =
11829 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11830 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11831 (long) (sections.line_size ? sections.line_offset : 0),
11832 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11833 (long) (sections.str_offsets_size
11834 ? sections.str_offsets_offset : 0),
11835 (long) (sections.macro_size ? sections.macro_offset : 0),
11836 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11837 /* Can we use an existing virtual DWO file? */
11838 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11839 virtual_dwo_name.c_str (),
11840 comp_dir);
11841 /* Create one if necessary. */
11842 if (*dwo_file_slot == NULL)
11843 {
11844 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11845 virtual_dwo_name.c_str ());
11846
11847 dwo_file = new struct dwo_file;
11848 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11849 dwo_file->comp_dir = comp_dir;
11850 dwo_file->sections.abbrev =
11851 create_dwp_v2_or_v5_section (per_objfile,
11852 &dwp_file->sections.abbrev,
11853 sections.abbrev_offset,
11854 sections.abbrev_size);
11855 dwo_file->sections.line =
11856 create_dwp_v2_or_v5_section (per_objfile,
11857 &dwp_file->sections.line,
11858 sections.line_offset, sections.line_size);
11859 dwo_file->sections.macro =
11860 create_dwp_v2_or_v5_section (per_objfile,
11861 &dwp_file->sections.macro,
11862 sections.macro_offset,
11863 sections.macro_size);
11864 dwo_file->sections.loclists =
11865 create_dwp_v2_or_v5_section (per_objfile,
11866 &dwp_file->sections.loclists,
11867 sections.loclists_offset,
11868 sections.loclists_size);
11869 dwo_file->sections.rnglists =
11870 create_dwp_v2_or_v5_section (per_objfile,
11871 &dwp_file->sections.rnglists,
11872 sections.rnglists_offset,
11873 sections.rnglists_size);
11874 dwo_file->sections.str_offsets =
11875 create_dwp_v2_or_v5_section (per_objfile,
11876 &dwp_file->sections.str_offsets,
11877 sections.str_offsets_offset,
11878 sections.str_offsets_size);
11879 /* The "str" section is global to the entire DWP file. */
11880 dwo_file->sections.str = dwp_file->sections.str;
11881 /* The info or types section is assigned below to dwo_unit,
11882 there's no need to record it in dwo_file.
11883 Also, we can't simply record type sections in dwo_file because
11884 we record a pointer into the vector in dwo_unit. As we collect more
11885 types we'll grow the vector and eventually have to reallocate space
11886 for it, invalidating all copies of pointers into the previous
11887 contents. */
11888 *dwo_file_slot = dwo_file;
11889 }
11890 else
11891 {
11892 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11893 virtual_dwo_name.c_str ());
11894
11895 dwo_file = (struct dwo_file *) *dwo_file_slot;
11896 }
11897
11898 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11899 dwo_unit->dwo_file = dwo_file;
11900 dwo_unit->signature = signature;
11901 dwo_unit->section
11902 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11903 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
11904 &dwp_file->sections.info,
11905 sections.info_or_types_offset,
11906 sections.info_or_types_size);
11907 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11908
11909 return dwo_unit;
11910 }
11911
11912 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11913 Returns NULL if the signature isn't found. */
11914
11915 static struct dwo_unit *
11916 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
11917 struct dwp_file *dwp_file, const char *comp_dir,
11918 ULONGEST signature, int is_debug_types)
11919 {
11920 const struct dwp_hash_table *dwp_htab =
11921 is_debug_types ? dwp_file->tus : dwp_file->cus;
11922 bfd *dbfd = dwp_file->dbfd.get ();
11923 uint32_t mask = dwp_htab->nr_slots - 1;
11924 uint32_t hash = signature & mask;
11925 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11926 unsigned int i;
11927 void **slot;
11928 struct dwo_unit find_dwo_cu;
11929
11930 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11931 find_dwo_cu.signature = signature;
11932 slot = htab_find_slot (is_debug_types
11933 ? dwp_file->loaded_tus.get ()
11934 : dwp_file->loaded_cus.get (),
11935 &find_dwo_cu, INSERT);
11936
11937 if (*slot != NULL)
11938 return (struct dwo_unit *) *slot;
11939
11940 /* Use a for loop so that we don't loop forever on bad debug info. */
11941 for (i = 0; i < dwp_htab->nr_slots; ++i)
11942 {
11943 ULONGEST signature_in_table;
11944
11945 signature_in_table =
11946 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11947 if (signature_in_table == signature)
11948 {
11949 uint32_t unit_index =
11950 read_4_bytes (dbfd,
11951 dwp_htab->unit_table + hash * sizeof (uint32_t));
11952
11953 if (dwp_file->version == 1)
11954 {
11955 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11956 unit_index, comp_dir,
11957 signature, is_debug_types);
11958 }
11959 else if (dwp_file->version == 2)
11960 {
11961 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11962 unit_index, comp_dir,
11963 signature, is_debug_types);
11964 }
11965 else /* version == 5 */
11966 {
11967 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11968 unit_index, comp_dir,
11969 signature, is_debug_types);
11970 }
11971 return (struct dwo_unit *) *slot;
11972 }
11973 if (signature_in_table == 0)
11974 return NULL;
11975 hash = (hash + hash2) & mask;
11976 }
11977
11978 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11979 " [in module %s]"),
11980 dwp_file->name);
11981 }
11982
11983 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11984 Open the file specified by FILE_NAME and hand it off to BFD for
11985 preliminary analysis. Return a newly initialized bfd *, which
11986 includes a canonicalized copy of FILE_NAME.
11987 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11988 SEARCH_CWD is true if the current directory is to be searched.
11989 It will be searched before debug-file-directory.
11990 If successful, the file is added to the bfd include table of the
11991 objfile's bfd (see gdb_bfd_record_inclusion).
11992 If unable to find/open the file, return NULL.
11993 NOTE: This function is derived from symfile_bfd_open. */
11994
11995 static gdb_bfd_ref_ptr
11996 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11997 const char *file_name, int is_dwp, int search_cwd)
11998 {
11999 int desc;
12000 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12001 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12002 to debug_file_directory. */
12003 const char *search_path;
12004 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12005
12006 gdb::unique_xmalloc_ptr<char> search_path_holder;
12007 if (search_cwd)
12008 {
12009 if (*debug_file_directory != '\0')
12010 {
12011 search_path_holder.reset (concat (".", dirname_separator_string,
12012 debug_file_directory,
12013 (char *) NULL));
12014 search_path = search_path_holder.get ();
12015 }
12016 else
12017 search_path = ".";
12018 }
12019 else
12020 search_path = debug_file_directory;
12021
12022 /* Add the path for the executable binary to the list of search paths. */
12023 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12024 search_path_holder.reset (concat (objfile_dir.c_str (),
12025 dirname_separator_string,
12026 search_path, nullptr));
12027 search_path = search_path_holder.get ();
12028
12029 openp_flags flags = OPF_RETURN_REALPATH;
12030 if (is_dwp)
12031 flags |= OPF_SEARCH_IN_PATH;
12032
12033 gdb::unique_xmalloc_ptr<char> absolute_name;
12034 desc = openp (search_path, flags, file_name,
12035 O_RDONLY | O_BINARY, &absolute_name);
12036 if (desc < 0)
12037 return NULL;
12038
12039 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12040 gnutarget, desc));
12041 if (sym_bfd == NULL)
12042 return NULL;
12043 bfd_set_cacheable (sym_bfd.get (), 1);
12044
12045 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12046 return NULL;
12047
12048 /* Success. Record the bfd as having been included by the objfile's bfd.
12049 This is important because things like demangled_names_hash lives in the
12050 objfile's per_bfd space and may have references to things like symbol
12051 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12052 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12053
12054 return sym_bfd;
12055 }
12056
12057 /* Try to open DWO file FILE_NAME.
12058 COMP_DIR is the DW_AT_comp_dir attribute.
12059 The result is the bfd handle of the file.
12060 If there is a problem finding or opening the file, return NULL.
12061 Upon success, the canonicalized path of the file is stored in the bfd,
12062 same as symfile_bfd_open. */
12063
12064 static gdb_bfd_ref_ptr
12065 open_dwo_file (dwarf2_per_objfile *per_objfile,
12066 const char *file_name, const char *comp_dir)
12067 {
12068 if (IS_ABSOLUTE_PATH (file_name))
12069 return try_open_dwop_file (per_objfile, file_name,
12070 0 /*is_dwp*/, 0 /*search_cwd*/);
12071
12072 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12073
12074 if (comp_dir != NULL)
12075 {
12076 gdb::unique_xmalloc_ptr<char> path_to_try
12077 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12078
12079 /* NOTE: If comp_dir is a relative path, this will also try the
12080 search path, which seems useful. */
12081 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12082 0 /*is_dwp*/,
12083 1 /*search_cwd*/));
12084 if (abfd != NULL)
12085 return abfd;
12086 }
12087
12088 /* That didn't work, try debug-file-directory, which, despite its name,
12089 is a list of paths. */
12090
12091 if (*debug_file_directory == '\0')
12092 return NULL;
12093
12094 return try_open_dwop_file (per_objfile, file_name,
12095 0 /*is_dwp*/, 1 /*search_cwd*/);
12096 }
12097
12098 /* This function is mapped across the sections and remembers the offset and
12099 size of each of the DWO debugging sections we are interested in. */
12100
12101 static void
12102 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12103 dwo_sections *dwo_sections)
12104 {
12105 const struct dwop_section_names *names = &dwop_section_names;
12106
12107 if (names->abbrev_dwo.matches (sectp->name))
12108 {
12109 dwo_sections->abbrev.s.section = sectp;
12110 dwo_sections->abbrev.size = bfd_section_size (sectp);
12111 }
12112 else if (names->info_dwo.matches (sectp->name))
12113 {
12114 dwo_sections->info.s.section = sectp;
12115 dwo_sections->info.size = bfd_section_size (sectp);
12116 }
12117 else if (names->line_dwo.matches (sectp->name))
12118 {
12119 dwo_sections->line.s.section = sectp;
12120 dwo_sections->line.size = bfd_section_size (sectp);
12121 }
12122 else if (names->loc_dwo.matches (sectp->name))
12123 {
12124 dwo_sections->loc.s.section = sectp;
12125 dwo_sections->loc.size = bfd_section_size (sectp);
12126 }
12127 else if (names->loclists_dwo.matches (sectp->name))
12128 {
12129 dwo_sections->loclists.s.section = sectp;
12130 dwo_sections->loclists.size = bfd_section_size (sectp);
12131 }
12132 else if (names->macinfo_dwo.matches (sectp->name))
12133 {
12134 dwo_sections->macinfo.s.section = sectp;
12135 dwo_sections->macinfo.size = bfd_section_size (sectp);
12136 }
12137 else if (names->macro_dwo.matches (sectp->name))
12138 {
12139 dwo_sections->macro.s.section = sectp;
12140 dwo_sections->macro.size = bfd_section_size (sectp);
12141 }
12142 else if (names->rnglists_dwo.matches (sectp->name))
12143 {
12144 dwo_sections->rnglists.s.section = sectp;
12145 dwo_sections->rnglists.size = bfd_section_size (sectp);
12146 }
12147 else if (names->str_dwo.matches (sectp->name))
12148 {
12149 dwo_sections->str.s.section = sectp;
12150 dwo_sections->str.size = bfd_section_size (sectp);
12151 }
12152 else if (names->str_offsets_dwo.matches (sectp->name))
12153 {
12154 dwo_sections->str_offsets.s.section = sectp;
12155 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12156 }
12157 else if (names->types_dwo.matches (sectp->name))
12158 {
12159 struct dwarf2_section_info type_section;
12160
12161 memset (&type_section, 0, sizeof (type_section));
12162 type_section.s.section = sectp;
12163 type_section.size = bfd_section_size (sectp);
12164 dwo_sections->types.push_back (type_section);
12165 }
12166 }
12167
12168 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12169 by PER_CU. This is for the non-DWP case.
12170 The result is NULL if DWO_NAME can't be found. */
12171
12172 static struct dwo_file *
12173 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12174 const char *comp_dir)
12175 {
12176 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12177
12178 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12179 if (dbfd == NULL)
12180 {
12181 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12182
12183 return NULL;
12184 }
12185
12186 dwo_file_up dwo_file (new struct dwo_file);
12187 dwo_file->dwo_name = dwo_name;
12188 dwo_file->comp_dir = comp_dir;
12189 dwo_file->dbfd = std::move (dbfd);
12190
12191 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12192 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12193 &dwo_file->sections);
12194
12195 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12196 dwo_file->cus);
12197
12198 if (cu->per_cu->dwarf_version < 5)
12199 {
12200 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12201 dwo_file->sections.types, dwo_file->tus);
12202 }
12203 else
12204 {
12205 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12206 &dwo_file->sections.info, dwo_file->tus,
12207 rcuh_kind::COMPILE);
12208 }
12209
12210 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12211
12212 return dwo_file.release ();
12213 }
12214
12215 /* This function is mapped across the sections and remembers the offset and
12216 size of each of the DWP debugging sections common to version 1 and 2 that
12217 we are interested in. */
12218
12219 static void
12220 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12221 dwp_file *dwp_file)
12222 {
12223 const struct dwop_section_names *names = &dwop_section_names;
12224 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12225
12226 /* Record the ELF section number for later lookup: this is what the
12227 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12228 gdb_assert (elf_section_nr < dwp_file->num_sections);
12229 dwp_file->elf_sections[elf_section_nr] = sectp;
12230
12231 /* Look for specific sections that we need. */
12232 if (names->str_dwo.matches (sectp->name))
12233 {
12234 dwp_file->sections.str.s.section = sectp;
12235 dwp_file->sections.str.size = bfd_section_size (sectp);
12236 }
12237 else if (names->cu_index.matches (sectp->name))
12238 {
12239 dwp_file->sections.cu_index.s.section = sectp;
12240 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12241 }
12242 else if (names->tu_index.matches (sectp->name))
12243 {
12244 dwp_file->sections.tu_index.s.section = sectp;
12245 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12246 }
12247 }
12248
12249 /* This function is mapped across the sections and remembers the offset and
12250 size of each of the DWP version 2 debugging sections that we are interested
12251 in. This is split into a separate function because we don't know if we
12252 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12253
12254 static void
12255 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12256 {
12257 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12258 const struct dwop_section_names *names = &dwop_section_names;
12259 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12260
12261 /* Record the ELF section number for later lookup: this is what the
12262 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12263 gdb_assert (elf_section_nr < dwp_file->num_sections);
12264 dwp_file->elf_sections[elf_section_nr] = sectp;
12265
12266 /* Look for specific sections that we need. */
12267 if (names->abbrev_dwo.matches (sectp->name))
12268 {
12269 dwp_file->sections.abbrev.s.section = sectp;
12270 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12271 }
12272 else if (names->info_dwo.matches (sectp->name))
12273 {
12274 dwp_file->sections.info.s.section = sectp;
12275 dwp_file->sections.info.size = bfd_section_size (sectp);
12276 }
12277 else if (names->line_dwo.matches (sectp->name))
12278 {
12279 dwp_file->sections.line.s.section = sectp;
12280 dwp_file->sections.line.size = bfd_section_size (sectp);
12281 }
12282 else if (names->loc_dwo.matches (sectp->name))
12283 {
12284 dwp_file->sections.loc.s.section = sectp;
12285 dwp_file->sections.loc.size = bfd_section_size (sectp);
12286 }
12287 else if (names->macinfo_dwo.matches (sectp->name))
12288 {
12289 dwp_file->sections.macinfo.s.section = sectp;
12290 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12291 }
12292 else if (names->macro_dwo.matches (sectp->name))
12293 {
12294 dwp_file->sections.macro.s.section = sectp;
12295 dwp_file->sections.macro.size = bfd_section_size (sectp);
12296 }
12297 else if (names->str_offsets_dwo.matches (sectp->name))
12298 {
12299 dwp_file->sections.str_offsets.s.section = sectp;
12300 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12301 }
12302 else if (names->types_dwo.matches (sectp->name))
12303 {
12304 dwp_file->sections.types.s.section = sectp;
12305 dwp_file->sections.types.size = bfd_section_size (sectp);
12306 }
12307 }
12308
12309 /* This function is mapped across the sections and remembers the offset and
12310 size of each of the DWP version 5 debugging sections that we are interested
12311 in. This is split into a separate function because we don't know if we
12312 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12313
12314 static void
12315 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12316 {
12317 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12318 const struct dwop_section_names *names = &dwop_section_names;
12319 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12320
12321 /* Record the ELF section number for later lookup: this is what the
12322 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12323 gdb_assert (elf_section_nr < dwp_file->num_sections);
12324 dwp_file->elf_sections[elf_section_nr] = sectp;
12325
12326 /* Look for specific sections that we need. */
12327 if (names->abbrev_dwo.matches (sectp->name))
12328 {
12329 dwp_file->sections.abbrev.s.section = sectp;
12330 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12331 }
12332 else if (names->info_dwo.matches (sectp->name))
12333 {
12334 dwp_file->sections.info.s.section = sectp;
12335 dwp_file->sections.info.size = bfd_section_size (sectp);
12336 }
12337 else if (names->line_dwo.matches (sectp->name))
12338 {
12339 dwp_file->sections.line.s.section = sectp;
12340 dwp_file->sections.line.size = bfd_section_size (sectp);
12341 }
12342 else if (names->loclists_dwo.matches (sectp->name))
12343 {
12344 dwp_file->sections.loclists.s.section = sectp;
12345 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12346 }
12347 else if (names->macro_dwo.matches (sectp->name))
12348 {
12349 dwp_file->sections.macro.s.section = sectp;
12350 dwp_file->sections.macro.size = bfd_section_size (sectp);
12351 }
12352 else if (names->rnglists_dwo.matches (sectp->name))
12353 {
12354 dwp_file->sections.rnglists.s.section = sectp;
12355 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12356 }
12357 else if (names->str_offsets_dwo.matches (sectp->name))
12358 {
12359 dwp_file->sections.str_offsets.s.section = sectp;
12360 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12361 }
12362 }
12363
12364 /* Hash function for dwp_file loaded CUs/TUs. */
12365
12366 static hashval_t
12367 hash_dwp_loaded_cutus (const void *item)
12368 {
12369 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12370
12371 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12372 return dwo_unit->signature;
12373 }
12374
12375 /* Equality function for dwp_file loaded CUs/TUs. */
12376
12377 static int
12378 eq_dwp_loaded_cutus (const void *a, const void *b)
12379 {
12380 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12381 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12382
12383 return dua->signature == dub->signature;
12384 }
12385
12386 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12387
12388 static htab_up
12389 allocate_dwp_loaded_cutus_table ()
12390 {
12391 return htab_up (htab_create_alloc (3,
12392 hash_dwp_loaded_cutus,
12393 eq_dwp_loaded_cutus,
12394 NULL, xcalloc, xfree));
12395 }
12396
12397 /* Try to open DWP file FILE_NAME.
12398 The result is the bfd handle of the file.
12399 If there is a problem finding or opening the file, return NULL.
12400 Upon success, the canonicalized path of the file is stored in the bfd,
12401 same as symfile_bfd_open. */
12402
12403 static gdb_bfd_ref_ptr
12404 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12405 {
12406 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12407 1 /*is_dwp*/,
12408 1 /*search_cwd*/));
12409 if (abfd != NULL)
12410 return abfd;
12411
12412 /* Work around upstream bug 15652.
12413 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12414 [Whether that's a "bug" is debatable, but it is getting in our way.]
12415 We have no real idea where the dwp file is, because gdb's realpath-ing
12416 of the executable's path may have discarded the needed info.
12417 [IWBN if the dwp file name was recorded in the executable, akin to
12418 .gnu_debuglink, but that doesn't exist yet.]
12419 Strip the directory from FILE_NAME and search again. */
12420 if (*debug_file_directory != '\0')
12421 {
12422 /* Don't implicitly search the current directory here.
12423 If the user wants to search "." to handle this case,
12424 it must be added to debug-file-directory. */
12425 return try_open_dwop_file (per_objfile, lbasename (file_name),
12426 1 /*is_dwp*/,
12427 0 /*search_cwd*/);
12428 }
12429
12430 return NULL;
12431 }
12432
12433 /* Initialize the use of the DWP file for the current objfile.
12434 By convention the name of the DWP file is ${objfile}.dwp.
12435 The result is NULL if it can't be found. */
12436
12437 static std::unique_ptr<struct dwp_file>
12438 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12439 {
12440 struct objfile *objfile = per_objfile->objfile;
12441
12442 /* Try to find first .dwp for the binary file before any symbolic links
12443 resolving. */
12444
12445 /* If the objfile is a debug file, find the name of the real binary
12446 file and get the name of dwp file from there. */
12447 std::string dwp_name;
12448 if (objfile->separate_debug_objfile_backlink != NULL)
12449 {
12450 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12451 const char *backlink_basename = lbasename (backlink->original_name);
12452
12453 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12454 }
12455 else
12456 dwp_name = objfile->original_name;
12457
12458 dwp_name += ".dwp";
12459
12460 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12461 if (dbfd == NULL
12462 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12463 {
12464 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12465 dwp_name = objfile_name (objfile);
12466 dwp_name += ".dwp";
12467 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12468 }
12469
12470 if (dbfd == NULL)
12471 {
12472 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12473
12474 return std::unique_ptr<dwp_file> ();
12475 }
12476
12477 const char *name = bfd_get_filename (dbfd.get ());
12478 std::unique_ptr<struct dwp_file> dwp_file
12479 (new struct dwp_file (name, std::move (dbfd)));
12480
12481 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12482 dwp_file->elf_sections =
12483 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12484 dwp_file->num_sections, asection *);
12485
12486 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12487 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12488 dwp_file.get ());
12489
12490 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12491
12492 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12493
12494 /* The DWP file version is stored in the hash table. Oh well. */
12495 if (dwp_file->cus && dwp_file->tus
12496 && dwp_file->cus->version != dwp_file->tus->version)
12497 {
12498 /* Technically speaking, we should try to limp along, but this is
12499 pretty bizarre. We use pulongest here because that's the established
12500 portability solution (e.g, we cannot use %u for uint32_t). */
12501 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12502 " TU version %s [in DWP file %s]"),
12503 pulongest (dwp_file->cus->version),
12504 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12505 }
12506
12507 if (dwp_file->cus)
12508 dwp_file->version = dwp_file->cus->version;
12509 else if (dwp_file->tus)
12510 dwp_file->version = dwp_file->tus->version;
12511 else
12512 dwp_file->version = 2;
12513
12514 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12515 {
12516 if (dwp_file->version == 2)
12517 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12518 dwp_file.get ());
12519 else
12520 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12521 dwp_file.get ());
12522 }
12523
12524 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12525 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12526
12527 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12528 dwarf_read_debug_printf (" %s CUs, %s TUs",
12529 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12530 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12531
12532 return dwp_file;
12533 }
12534
12535 /* Wrapper around open_and_init_dwp_file, only open it once. */
12536
12537 static struct dwp_file *
12538 get_dwp_file (dwarf2_per_objfile *per_objfile)
12539 {
12540 if (!per_objfile->per_bfd->dwp_checked)
12541 {
12542 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12543 per_objfile->per_bfd->dwp_checked = 1;
12544 }
12545 return per_objfile->per_bfd->dwp_file.get ();
12546 }
12547
12548 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12549 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12550 or in the DWP file for the objfile, referenced by THIS_UNIT.
12551 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12552 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12553
12554 This is called, for example, when wanting to read a variable with a
12555 complex location. Therefore we don't want to do file i/o for every call.
12556 Therefore we don't want to look for a DWO file on every call.
12557 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12558 then we check if we've already seen DWO_NAME, and only THEN do we check
12559 for a DWO file.
12560
12561 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12562 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12563
12564 static struct dwo_unit *
12565 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12566 ULONGEST signature, int is_debug_types)
12567 {
12568 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12569 struct objfile *objfile = per_objfile->objfile;
12570 const char *kind = is_debug_types ? "TU" : "CU";
12571 void **dwo_file_slot;
12572 struct dwo_file *dwo_file;
12573 struct dwp_file *dwp_file;
12574
12575 /* First see if there's a DWP file.
12576 If we have a DWP file but didn't find the DWO inside it, don't
12577 look for the original DWO file. It makes gdb behave differently
12578 depending on whether one is debugging in the build tree. */
12579
12580 dwp_file = get_dwp_file (per_objfile);
12581 if (dwp_file != NULL)
12582 {
12583 const struct dwp_hash_table *dwp_htab =
12584 is_debug_types ? dwp_file->tus : dwp_file->cus;
12585
12586 if (dwp_htab != NULL)
12587 {
12588 struct dwo_unit *dwo_cutu =
12589 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12590 is_debug_types);
12591
12592 if (dwo_cutu != NULL)
12593 {
12594 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12595 kind, hex_string (signature),
12596 host_address_to_string (dwo_cutu));
12597
12598 return dwo_cutu;
12599 }
12600 }
12601 }
12602 else
12603 {
12604 /* No DWP file, look for the DWO file. */
12605
12606 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12607 if (*dwo_file_slot == NULL)
12608 {
12609 /* Read in the file and build a table of the CUs/TUs it contains. */
12610 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12611 }
12612 /* NOTE: This will be NULL if unable to open the file. */
12613 dwo_file = (struct dwo_file *) *dwo_file_slot;
12614
12615 if (dwo_file != NULL)
12616 {
12617 struct dwo_unit *dwo_cutu = NULL;
12618
12619 if (is_debug_types && dwo_file->tus)
12620 {
12621 struct dwo_unit find_dwo_cutu;
12622
12623 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12624 find_dwo_cutu.signature = signature;
12625 dwo_cutu
12626 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12627 &find_dwo_cutu);
12628 }
12629 else if (!is_debug_types && dwo_file->cus)
12630 {
12631 struct dwo_unit find_dwo_cutu;
12632
12633 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12634 find_dwo_cutu.signature = signature;
12635 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12636 &find_dwo_cutu);
12637 }
12638
12639 if (dwo_cutu != NULL)
12640 {
12641 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12642 kind, dwo_name, hex_string (signature),
12643 host_address_to_string (dwo_cutu));
12644
12645 return dwo_cutu;
12646 }
12647 }
12648 }
12649
12650 /* We didn't find it. This could mean a dwo_id mismatch, or
12651 someone deleted the DWO/DWP file, or the search path isn't set up
12652 correctly to find the file. */
12653
12654 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12655 kind, dwo_name, hex_string (signature));
12656
12657 /* This is a warning and not a complaint because it can be caused by
12658 pilot error (e.g., user accidentally deleting the DWO). */
12659 {
12660 /* Print the name of the DWP file if we looked there, helps the user
12661 better diagnose the problem. */
12662 std::string dwp_text;
12663
12664 if (dwp_file != NULL)
12665 dwp_text = string_printf (" [in DWP file %s]",
12666 lbasename (dwp_file->name));
12667
12668 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12669 " [in module %s]"),
12670 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12671 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12672 }
12673 return NULL;
12674 }
12675
12676 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12677 See lookup_dwo_cutu_unit for details. */
12678
12679 static struct dwo_unit *
12680 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12681 ULONGEST signature)
12682 {
12683 gdb_assert (!cu->per_cu->is_debug_types);
12684
12685 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12686 }
12687
12688 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12689 See lookup_dwo_cutu_unit for details. */
12690
12691 static struct dwo_unit *
12692 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12693 {
12694 gdb_assert (cu->per_cu->is_debug_types);
12695
12696 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12697
12698 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12699 }
12700
12701 /* Traversal function for queue_and_load_all_dwo_tus. */
12702
12703 static int
12704 queue_and_load_dwo_tu (void **slot, void *info)
12705 {
12706 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12707 dwarf2_cu *cu = (dwarf2_cu *) info;
12708 ULONGEST signature = dwo_unit->signature;
12709 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12710
12711 if (sig_type != NULL)
12712 {
12713 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12714 a real dependency of PER_CU on SIG_TYPE. That is detected later
12715 while processing PER_CU. */
12716 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12717 cu->language))
12718 load_full_type_unit (sig_type, cu->per_objfile);
12719 cu->per_cu->imported_symtabs_push (sig_type);
12720 }
12721
12722 return 1;
12723 }
12724
12725 /* Queue all TUs contained in the DWO of CU to be read in.
12726 The DWO may have the only definition of the type, though it may not be
12727 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12728 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12729
12730 static void
12731 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12732 {
12733 struct dwo_unit *dwo_unit;
12734 struct dwo_file *dwo_file;
12735
12736 gdb_assert (cu != nullptr);
12737 gdb_assert (!cu->per_cu->is_debug_types);
12738 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12739
12740 dwo_unit = cu->dwo_unit;
12741 gdb_assert (dwo_unit != NULL);
12742
12743 dwo_file = dwo_unit->dwo_file;
12744 if (dwo_file->tus != NULL)
12745 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12746 }
12747
12748 /* Read in various DIEs. */
12749
12750 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12751 Inherit only the children of the DW_AT_abstract_origin DIE not being
12752 already referenced by DW_AT_abstract_origin from the children of the
12753 current DIE. */
12754
12755 static void
12756 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12757 {
12758 struct die_info *child_die;
12759 sect_offset *offsetp;
12760 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12761 struct die_info *origin_die;
12762 /* Iterator of the ORIGIN_DIE children. */
12763 struct die_info *origin_child_die;
12764 struct attribute *attr;
12765 struct dwarf2_cu *origin_cu;
12766 struct pending **origin_previous_list_in_scope;
12767
12768 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12769 if (!attr)
12770 return;
12771
12772 /* Note that following die references may follow to a die in a
12773 different cu. */
12774
12775 origin_cu = cu;
12776 origin_die = follow_die_ref (die, attr, &origin_cu);
12777
12778 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12779 symbols in. */
12780 origin_previous_list_in_scope = origin_cu->list_in_scope;
12781 origin_cu->list_in_scope = cu->list_in_scope;
12782
12783 if (die->tag != origin_die->tag
12784 && !(die->tag == DW_TAG_inlined_subroutine
12785 && origin_die->tag == DW_TAG_subprogram))
12786 complaint (_("DIE %s and its abstract origin %s have different tags"),
12787 sect_offset_str (die->sect_off),
12788 sect_offset_str (origin_die->sect_off));
12789
12790 /* Find if the concrete and abstract trees are structurally the
12791 same. This is a shallow traversal and it is not bullet-proof;
12792 the compiler can trick the debugger into believing that the trees
12793 are isomorphic, whereas they actually are not. However, the
12794 likelyhood of this happening is pretty low, and a full-fledged
12795 check would be an overkill. */
12796 bool are_isomorphic = true;
12797 die_info *concrete_child = die->child;
12798 die_info *abstract_child = origin_die->child;
12799 while (concrete_child != nullptr || abstract_child != nullptr)
12800 {
12801 if (concrete_child == nullptr
12802 || abstract_child == nullptr
12803 || concrete_child->tag != abstract_child->tag)
12804 {
12805 are_isomorphic = false;
12806 break;
12807 }
12808
12809 concrete_child = concrete_child->sibling;
12810 abstract_child = abstract_child->sibling;
12811 }
12812
12813 /* Walk the origin's children in parallel to the concrete children.
12814 This helps match an origin child in case the debug info misses
12815 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12816 origin tree may not have the same tree structure as the concrete
12817 DIE, though. */
12818 die_info *corresponding_abstract_child
12819 = are_isomorphic ? origin_die->child : nullptr;
12820
12821 std::vector<sect_offset> offsets;
12822
12823 for (child_die = die->child;
12824 child_die && child_die->tag;
12825 child_die = child_die->sibling)
12826 {
12827 struct die_info *child_origin_die;
12828 struct dwarf2_cu *child_origin_cu;
12829
12830 /* We are trying to process concrete instance entries:
12831 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12832 it's not relevant to our analysis here. i.e. detecting DIEs that are
12833 present in the abstract instance but not referenced in the concrete
12834 one. */
12835 if (child_die->tag == DW_TAG_call_site
12836 || child_die->tag == DW_TAG_GNU_call_site)
12837 {
12838 if (are_isomorphic)
12839 corresponding_abstract_child
12840 = corresponding_abstract_child->sibling;
12841 continue;
12842 }
12843
12844 /* For each CHILD_DIE, find the corresponding child of
12845 ORIGIN_DIE. If there is more than one layer of
12846 DW_AT_abstract_origin, follow them all; there shouldn't be,
12847 but GCC versions at least through 4.4 generate this (GCC PR
12848 40573). */
12849 child_origin_die = child_die;
12850 child_origin_cu = cu;
12851 while (1)
12852 {
12853 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12854 child_origin_cu);
12855 if (attr == NULL)
12856 break;
12857 child_origin_die = follow_die_ref (child_origin_die, attr,
12858 &child_origin_cu);
12859 }
12860
12861 /* If missing DW_AT_abstract_origin, try the corresponding child
12862 of the origin. Clang emits such lexical scopes. */
12863 if (child_origin_die == child_die
12864 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12865 && are_isomorphic
12866 && child_die->tag == DW_TAG_lexical_block)
12867 child_origin_die = corresponding_abstract_child;
12868
12869 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12870 counterpart may exist. */
12871 if (child_origin_die != child_die)
12872 {
12873 if (child_die->tag != child_origin_die->tag
12874 && !(child_die->tag == DW_TAG_inlined_subroutine
12875 && child_origin_die->tag == DW_TAG_subprogram))
12876 complaint (_("Child DIE %s and its abstract origin %s have "
12877 "different tags"),
12878 sect_offset_str (child_die->sect_off),
12879 sect_offset_str (child_origin_die->sect_off));
12880 if (child_origin_die->parent != origin_die)
12881 complaint (_("Child DIE %s and its abstract origin %s have "
12882 "different parents"),
12883 sect_offset_str (child_die->sect_off),
12884 sect_offset_str (child_origin_die->sect_off));
12885 else
12886 offsets.push_back (child_origin_die->sect_off);
12887 }
12888
12889 if (are_isomorphic)
12890 corresponding_abstract_child = corresponding_abstract_child->sibling;
12891 }
12892 std::sort (offsets.begin (), offsets.end ());
12893 sect_offset *offsets_end = offsets.data () + offsets.size ();
12894 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12895 if (offsetp[-1] == *offsetp)
12896 complaint (_("Multiple children of DIE %s refer "
12897 "to DIE %s as their abstract origin"),
12898 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12899
12900 offsetp = offsets.data ();
12901 origin_child_die = origin_die->child;
12902 while (origin_child_die && origin_child_die->tag)
12903 {
12904 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12905 while (offsetp < offsets_end
12906 && *offsetp < origin_child_die->sect_off)
12907 offsetp++;
12908 if (offsetp >= offsets_end
12909 || *offsetp > origin_child_die->sect_off)
12910 {
12911 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12912 Check whether we're already processing ORIGIN_CHILD_DIE.
12913 This can happen with mutually referenced abstract_origins.
12914 PR 16581. */
12915 if (!origin_child_die->in_process)
12916 process_die (origin_child_die, origin_cu);
12917 }
12918 origin_child_die = origin_child_die->sibling;
12919 }
12920 origin_cu->list_in_scope = origin_previous_list_in_scope;
12921
12922 if (cu != origin_cu)
12923 compute_delayed_physnames (origin_cu);
12924 }
12925
12926 static void
12927 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12928 {
12929 struct objfile *objfile = cu->per_objfile->objfile;
12930 struct gdbarch *gdbarch = objfile->arch ();
12931 struct context_stack *newobj;
12932 CORE_ADDR lowpc;
12933 CORE_ADDR highpc;
12934 struct die_info *child_die;
12935 struct attribute *attr, *call_line, *call_file;
12936 const char *name;
12937 CORE_ADDR baseaddr;
12938 struct block *block;
12939 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12940 std::vector<struct symbol *> template_args;
12941 struct template_symbol *templ_func = NULL;
12942
12943 if (inlined_func)
12944 {
12945 /* If we do not have call site information, we can't show the
12946 caller of this inlined function. That's too confusing, so
12947 only use the scope for local variables. */
12948 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12949 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12950 if (call_line == NULL || call_file == NULL)
12951 {
12952 read_lexical_block_scope (die, cu);
12953 return;
12954 }
12955 }
12956
12957 baseaddr = objfile->text_section_offset ();
12958
12959 name = dwarf2_name (die, cu);
12960
12961 /* Ignore functions with missing or empty names. These are actually
12962 illegal according to the DWARF standard. */
12963 if (name == NULL)
12964 {
12965 complaint (_("missing name for subprogram DIE at %s"),
12966 sect_offset_str (die->sect_off));
12967 return;
12968 }
12969
12970 /* Ignore functions with missing or invalid low and high pc attributes. */
12971 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12972 <= PC_BOUNDS_INVALID)
12973 {
12974 attr = dwarf2_attr (die, DW_AT_external, cu);
12975 if (attr == nullptr || !attr->as_boolean ())
12976 complaint (_("cannot get low and high bounds "
12977 "for subprogram DIE at %s"),
12978 sect_offset_str (die->sect_off));
12979 return;
12980 }
12981
12982 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12983 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12984
12985 /* If we have any template arguments, then we must allocate a
12986 different sort of symbol. */
12987 for (child_die = die->child; child_die; child_die = child_die->sibling)
12988 {
12989 if (child_die->tag == DW_TAG_template_type_param
12990 || child_die->tag == DW_TAG_template_value_param)
12991 {
12992 templ_func = new (&objfile->objfile_obstack) template_symbol;
12993 templ_func->subclass = SYMBOL_TEMPLATE;
12994 break;
12995 }
12996 }
12997
12998 gdb_assert (cu->get_builder () != nullptr);
12999 newobj = cu->get_builder ()->push_context (0, lowpc);
13000 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13001 (struct symbol *) templ_func);
13002
13003 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13004 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13005 cu->language);
13006
13007 /* If there is a location expression for DW_AT_frame_base, record
13008 it. */
13009 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13010 if (attr != nullptr)
13011 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13012
13013 /* If there is a location for the static link, record it. */
13014 newobj->static_link = NULL;
13015 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13016 if (attr != nullptr)
13017 {
13018 newobj->static_link
13019 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13020 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13021 cu->addr_type ());
13022 }
13023
13024 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13025
13026 if (die->child != NULL)
13027 {
13028 child_die = die->child;
13029 while (child_die && child_die->tag)
13030 {
13031 if (child_die->tag == DW_TAG_template_type_param
13032 || child_die->tag == DW_TAG_template_value_param)
13033 {
13034 struct symbol *arg = new_symbol (child_die, NULL, cu);
13035
13036 if (arg != NULL)
13037 template_args.push_back (arg);
13038 }
13039 else
13040 process_die (child_die, cu);
13041 child_die = child_die->sibling;
13042 }
13043 }
13044
13045 inherit_abstract_dies (die, cu);
13046
13047 /* If we have a DW_AT_specification, we might need to import using
13048 directives from the context of the specification DIE. See the
13049 comment in determine_prefix. */
13050 if (cu->language == language_cplus
13051 && dwarf2_attr (die, DW_AT_specification, cu))
13052 {
13053 struct dwarf2_cu *spec_cu = cu;
13054 struct die_info *spec_die = die_specification (die, &spec_cu);
13055
13056 while (spec_die)
13057 {
13058 child_die = spec_die->child;
13059 while (child_die && child_die->tag)
13060 {
13061 if (child_die->tag == DW_TAG_imported_module)
13062 process_die (child_die, spec_cu);
13063 child_die = child_die->sibling;
13064 }
13065
13066 /* In some cases, GCC generates specification DIEs that
13067 themselves contain DW_AT_specification attributes. */
13068 spec_die = die_specification (spec_die, &spec_cu);
13069 }
13070 }
13071
13072 struct context_stack cstk = cu->get_builder ()->pop_context ();
13073 /* Make a block for the local symbols within. */
13074 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13075 cstk.static_link, lowpc, highpc);
13076
13077 /* For C++, set the block's scope. */
13078 if ((cu->language == language_cplus
13079 || cu->language == language_fortran
13080 || cu->language == language_d
13081 || cu->language == language_rust)
13082 && cu->processing_has_namespace_info)
13083 block_set_scope (block, determine_prefix (die, cu),
13084 &objfile->objfile_obstack);
13085
13086 /* If we have address ranges, record them. */
13087 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13088
13089 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13090
13091 /* Attach template arguments to function. */
13092 if (!template_args.empty ())
13093 {
13094 gdb_assert (templ_func != NULL);
13095
13096 templ_func->n_template_arguments = template_args.size ();
13097 templ_func->template_arguments
13098 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13099 templ_func->n_template_arguments);
13100 memcpy (templ_func->template_arguments,
13101 template_args.data (),
13102 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13103
13104 /* Make sure that the symtab is set on the new symbols. Even
13105 though they don't appear in this symtab directly, other parts
13106 of gdb assume that symbols do, and this is reasonably
13107 true. */
13108 for (symbol *sym : template_args)
13109 symbol_set_symtab (sym, symbol_symtab (templ_func));
13110 }
13111
13112 /* In C++, we can have functions nested inside functions (e.g., when
13113 a function declares a class that has methods). This means that
13114 when we finish processing a function scope, we may need to go
13115 back to building a containing block's symbol lists. */
13116 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13117 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13118
13119 /* If we've finished processing a top-level function, subsequent
13120 symbols go in the file symbol list. */
13121 if (cu->get_builder ()->outermost_context_p ())
13122 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13123 }
13124
13125 /* Process all the DIES contained within a lexical block scope. Start
13126 a new scope, process the dies, and then close the scope. */
13127
13128 static void
13129 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13130 {
13131 struct objfile *objfile = cu->per_objfile->objfile;
13132 struct gdbarch *gdbarch = objfile->arch ();
13133 CORE_ADDR lowpc, highpc;
13134 struct die_info *child_die;
13135 CORE_ADDR baseaddr;
13136
13137 baseaddr = objfile->text_section_offset ();
13138
13139 /* Ignore blocks with missing or invalid low and high pc attributes. */
13140 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13141 as multiple lexical blocks? Handling children in a sane way would
13142 be nasty. Might be easier to properly extend generic blocks to
13143 describe ranges. */
13144 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13145 {
13146 case PC_BOUNDS_NOT_PRESENT:
13147 /* DW_TAG_lexical_block has no attributes, process its children as if
13148 there was no wrapping by that DW_TAG_lexical_block.
13149 GCC does no longer produces such DWARF since GCC r224161. */
13150 for (child_die = die->child;
13151 child_die != NULL && child_die->tag;
13152 child_die = child_die->sibling)
13153 {
13154 /* We might already be processing this DIE. This can happen
13155 in an unusual circumstance -- where a subroutine A
13156 appears lexically in another subroutine B, but A actually
13157 inlines B. The recursion is broken here, rather than in
13158 inherit_abstract_dies, because it seems better to simply
13159 drop concrete children here. */
13160 if (!child_die->in_process)
13161 process_die (child_die, cu);
13162 }
13163 return;
13164 case PC_BOUNDS_INVALID:
13165 return;
13166 }
13167 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13168 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13169
13170 cu->get_builder ()->push_context (0, lowpc);
13171 if (die->child != NULL)
13172 {
13173 child_die = die->child;
13174 while (child_die && child_die->tag)
13175 {
13176 process_die (child_die, cu);
13177 child_die = child_die->sibling;
13178 }
13179 }
13180 inherit_abstract_dies (die, cu);
13181 struct context_stack cstk = cu->get_builder ()->pop_context ();
13182
13183 if (*cu->get_builder ()->get_local_symbols () != NULL
13184 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13185 {
13186 struct block *block
13187 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13188 cstk.start_addr, highpc);
13189
13190 /* Note that recording ranges after traversing children, as we
13191 do here, means that recording a parent's ranges entails
13192 walking across all its children's ranges as they appear in
13193 the address map, which is quadratic behavior.
13194
13195 It would be nicer to record the parent's ranges before
13196 traversing its children, simply overriding whatever you find
13197 there. But since we don't even decide whether to create a
13198 block until after we've traversed its children, that's hard
13199 to do. */
13200 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13201 }
13202 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13203 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13204 }
13205
13206 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13207
13208 static void
13209 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13210 {
13211 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13212 struct objfile *objfile = per_objfile->objfile;
13213 struct gdbarch *gdbarch = objfile->arch ();
13214 CORE_ADDR pc, baseaddr;
13215 struct attribute *attr;
13216 struct call_site *call_site, call_site_local;
13217 void **slot;
13218 int nparams;
13219 struct die_info *child_die;
13220
13221 baseaddr = objfile->text_section_offset ();
13222
13223 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13224 if (attr == NULL)
13225 {
13226 /* This was a pre-DWARF-5 GNU extension alias
13227 for DW_AT_call_return_pc. */
13228 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13229 }
13230 if (!attr)
13231 {
13232 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13233 "DIE %s [in module %s]"),
13234 sect_offset_str (die->sect_off), objfile_name (objfile));
13235 return;
13236 }
13237 pc = attr->as_address () + baseaddr;
13238 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13239
13240 if (cu->call_site_htab == NULL)
13241 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13242 NULL, &objfile->objfile_obstack,
13243 hashtab_obstack_allocate, NULL);
13244 call_site_local.pc = pc;
13245 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13246 if (*slot != NULL)
13247 {
13248 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13249 "DIE %s [in module %s]"),
13250 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13251 objfile_name (objfile));
13252 return;
13253 }
13254
13255 /* Count parameters at the caller. */
13256
13257 nparams = 0;
13258 for (child_die = die->child; child_die && child_die->tag;
13259 child_die = child_die->sibling)
13260 {
13261 if (child_die->tag != DW_TAG_call_site_parameter
13262 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13263 {
13264 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13265 "DW_TAG_call_site child DIE %s [in module %s]"),
13266 child_die->tag, sect_offset_str (child_die->sect_off),
13267 objfile_name (objfile));
13268 continue;
13269 }
13270
13271 nparams++;
13272 }
13273
13274 call_site
13275 = ((struct call_site *)
13276 obstack_alloc (&objfile->objfile_obstack,
13277 sizeof (*call_site)
13278 + (sizeof (*call_site->parameter) * (nparams - 1))));
13279 *slot = call_site;
13280 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13281 call_site->pc = pc;
13282
13283 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13284 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13285 {
13286 struct die_info *func_die;
13287
13288 /* Skip also over DW_TAG_inlined_subroutine. */
13289 for (func_die = die->parent;
13290 func_die && func_die->tag != DW_TAG_subprogram
13291 && func_die->tag != DW_TAG_subroutine_type;
13292 func_die = func_die->parent);
13293
13294 /* DW_AT_call_all_calls is a superset
13295 of DW_AT_call_all_tail_calls. */
13296 if (func_die
13297 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13298 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13299 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13300 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13301 {
13302 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13303 not complete. But keep CALL_SITE for look ups via call_site_htab,
13304 both the initial caller containing the real return address PC and
13305 the final callee containing the current PC of a chain of tail
13306 calls do not need to have the tail call list complete. But any
13307 function candidate for a virtual tail call frame searched via
13308 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13309 determined unambiguously. */
13310 }
13311 else
13312 {
13313 struct type *func_type = NULL;
13314
13315 if (func_die)
13316 func_type = get_die_type (func_die, cu);
13317 if (func_type != NULL)
13318 {
13319 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13320
13321 /* Enlist this call site to the function. */
13322 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13323 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13324 }
13325 else
13326 complaint (_("Cannot find function owning DW_TAG_call_site "
13327 "DIE %s [in module %s]"),
13328 sect_offset_str (die->sect_off), objfile_name (objfile));
13329 }
13330 }
13331
13332 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13333 if (attr == NULL)
13334 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13335 if (attr == NULL)
13336 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13337 if (attr == NULL)
13338 {
13339 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13340 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13341 }
13342 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13343 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13344 /* Keep NULL DWARF_BLOCK. */;
13345 else if (attr->form_is_block ())
13346 {
13347 struct dwarf2_locexpr_baton *dlbaton;
13348 struct dwarf_block *block = attr->as_block ();
13349
13350 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13351 dlbaton->data = block->data;
13352 dlbaton->size = block->size;
13353 dlbaton->per_objfile = per_objfile;
13354 dlbaton->per_cu = cu->per_cu;
13355
13356 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13357 }
13358 else if (attr->form_is_ref ())
13359 {
13360 struct dwarf2_cu *target_cu = cu;
13361 struct die_info *target_die;
13362
13363 target_die = follow_die_ref (die, attr, &target_cu);
13364 gdb_assert (target_cu->per_objfile->objfile == objfile);
13365 if (die_is_declaration (target_die, target_cu))
13366 {
13367 const char *target_physname;
13368
13369 /* Prefer the mangled name; otherwise compute the demangled one. */
13370 target_physname = dw2_linkage_name (target_die, target_cu);
13371 if (target_physname == NULL)
13372 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13373 if (target_physname == NULL)
13374 complaint (_("DW_AT_call_target target DIE has invalid "
13375 "physname, for referencing DIE %s [in module %s]"),
13376 sect_offset_str (die->sect_off), objfile_name (objfile));
13377 else
13378 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13379 }
13380 else
13381 {
13382 CORE_ADDR lowpc;
13383
13384 /* DW_AT_entry_pc should be preferred. */
13385 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13386 <= PC_BOUNDS_INVALID)
13387 complaint (_("DW_AT_call_target target DIE has invalid "
13388 "low pc, for referencing DIE %s [in module %s]"),
13389 sect_offset_str (die->sect_off), objfile_name (objfile));
13390 else
13391 {
13392 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13393 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13394 }
13395 }
13396 }
13397 else
13398 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13399 "block nor reference, for DIE %s [in module %s]"),
13400 sect_offset_str (die->sect_off), objfile_name (objfile));
13401
13402 call_site->per_cu = cu->per_cu;
13403 call_site->per_objfile = per_objfile;
13404
13405 for (child_die = die->child;
13406 child_die && child_die->tag;
13407 child_die = child_die->sibling)
13408 {
13409 struct call_site_parameter *parameter;
13410 struct attribute *loc, *origin;
13411
13412 if (child_die->tag != DW_TAG_call_site_parameter
13413 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13414 {
13415 /* Already printed the complaint above. */
13416 continue;
13417 }
13418
13419 gdb_assert (call_site->parameter_count < nparams);
13420 parameter = &call_site->parameter[call_site->parameter_count];
13421
13422 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13423 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13424 register is contained in DW_AT_call_value. */
13425
13426 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13427 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13428 if (origin == NULL)
13429 {
13430 /* This was a pre-DWARF-5 GNU extension alias
13431 for DW_AT_call_parameter. */
13432 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13433 }
13434 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13435 {
13436 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13437
13438 sect_offset sect_off = origin->get_ref_die_offset ();
13439 if (!cu->header.offset_in_cu_p (sect_off))
13440 {
13441 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13442 binding can be done only inside one CU. Such referenced DIE
13443 therefore cannot be even moved to DW_TAG_partial_unit. */
13444 complaint (_("DW_AT_call_parameter offset is not in CU for "
13445 "DW_TAG_call_site child DIE %s [in module %s]"),
13446 sect_offset_str (child_die->sect_off),
13447 objfile_name (objfile));
13448 continue;
13449 }
13450 parameter->u.param_cu_off
13451 = (cu_offset) (sect_off - cu->header.sect_off);
13452 }
13453 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13454 {
13455 complaint (_("No DW_FORM_block* DW_AT_location for "
13456 "DW_TAG_call_site child DIE %s [in module %s]"),
13457 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13458 continue;
13459 }
13460 else
13461 {
13462 struct dwarf_block *block = loc->as_block ();
13463
13464 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13465 (block->data, &block->data[block->size]);
13466 if (parameter->u.dwarf_reg != -1)
13467 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13468 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13469 &block->data[block->size],
13470 &parameter->u.fb_offset))
13471 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13472 else
13473 {
13474 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13475 "for DW_FORM_block* DW_AT_location is supported for "
13476 "DW_TAG_call_site child DIE %s "
13477 "[in module %s]"),
13478 sect_offset_str (child_die->sect_off),
13479 objfile_name (objfile));
13480 continue;
13481 }
13482 }
13483
13484 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13485 if (attr == NULL)
13486 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13487 if (attr == NULL || !attr->form_is_block ())
13488 {
13489 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13490 "DW_TAG_call_site child DIE %s [in module %s]"),
13491 sect_offset_str (child_die->sect_off),
13492 objfile_name (objfile));
13493 continue;
13494 }
13495
13496 struct dwarf_block *block = attr->as_block ();
13497 parameter->value = block->data;
13498 parameter->value_size = block->size;
13499
13500 /* Parameters are not pre-cleared by memset above. */
13501 parameter->data_value = NULL;
13502 parameter->data_value_size = 0;
13503 call_site->parameter_count++;
13504
13505 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13506 if (attr == NULL)
13507 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13508 if (attr != nullptr)
13509 {
13510 if (!attr->form_is_block ())
13511 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13512 "DW_TAG_call_site child DIE %s [in module %s]"),
13513 sect_offset_str (child_die->sect_off),
13514 objfile_name (objfile));
13515 else
13516 {
13517 block = attr->as_block ();
13518 parameter->data_value = block->data;
13519 parameter->data_value_size = block->size;
13520 }
13521 }
13522 }
13523 }
13524
13525 /* Helper function for read_variable. If DIE represents a virtual
13526 table, then return the type of the concrete object that is
13527 associated with the virtual table. Otherwise, return NULL. */
13528
13529 static struct type *
13530 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13531 {
13532 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13533 if (attr == NULL)
13534 return NULL;
13535
13536 /* Find the type DIE. */
13537 struct die_info *type_die = NULL;
13538 struct dwarf2_cu *type_cu = cu;
13539
13540 if (attr->form_is_ref ())
13541 type_die = follow_die_ref (die, attr, &type_cu);
13542 if (type_die == NULL)
13543 return NULL;
13544
13545 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13546 return NULL;
13547 return die_containing_type (type_die, type_cu);
13548 }
13549
13550 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13551
13552 static void
13553 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13554 {
13555 struct rust_vtable_symbol *storage = NULL;
13556
13557 if (cu->language == language_rust)
13558 {
13559 struct type *containing_type = rust_containing_type (die, cu);
13560
13561 if (containing_type != NULL)
13562 {
13563 struct objfile *objfile = cu->per_objfile->objfile;
13564
13565 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13566 storage->concrete_type = containing_type;
13567 storage->subclass = SYMBOL_RUST_VTABLE;
13568 }
13569 }
13570
13571 struct symbol *res = new_symbol (die, NULL, cu, storage);
13572 struct attribute *abstract_origin
13573 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13574 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13575 if (res == NULL && loc && abstract_origin)
13576 {
13577 /* We have a variable without a name, but with a location and an abstract
13578 origin. This may be a concrete instance of an abstract variable
13579 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13580 later. */
13581 struct dwarf2_cu *origin_cu = cu;
13582 struct die_info *origin_die
13583 = follow_die_ref (die, abstract_origin, &origin_cu);
13584 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13585 per_objfile->per_bfd->abstract_to_concrete
13586 [origin_die->sect_off].push_back (die->sect_off);
13587 }
13588 }
13589
13590 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13591 reading .debug_rnglists.
13592 Callback's type should be:
13593 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13594 Return true if the attributes are present and valid, otherwise,
13595 return false. */
13596
13597 template <typename Callback>
13598 static bool
13599 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13600 dwarf_tag tag, Callback &&callback)
13601 {
13602 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13603 struct objfile *objfile = per_objfile->objfile;
13604 bfd *obfd = objfile->obfd;
13605 /* Base address selection entry. */
13606 gdb::optional<CORE_ADDR> base;
13607 const gdb_byte *buffer;
13608 CORE_ADDR baseaddr;
13609 bool overflow = false;
13610 ULONGEST addr_index;
13611 struct dwarf2_section_info *rnglists_section;
13612
13613 base = cu->base_address;
13614 rnglists_section = cu_debug_rnglists_section (cu, tag);
13615 rnglists_section->read (objfile);
13616
13617 if (offset >= rnglists_section->size)
13618 {
13619 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13620 offset);
13621 return false;
13622 }
13623 buffer = rnglists_section->buffer + offset;
13624
13625 baseaddr = objfile->text_section_offset ();
13626
13627 while (1)
13628 {
13629 /* Initialize it due to a false compiler warning. */
13630 CORE_ADDR range_beginning = 0, range_end = 0;
13631 const gdb_byte *buf_end = (rnglists_section->buffer
13632 + rnglists_section->size);
13633 unsigned int bytes_read;
13634
13635 if (buffer == buf_end)
13636 {
13637 overflow = true;
13638 break;
13639 }
13640 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13641 switch (rlet)
13642 {
13643 case DW_RLE_end_of_list:
13644 break;
13645 case DW_RLE_base_address:
13646 if (buffer + cu->header.addr_size > buf_end)
13647 {
13648 overflow = true;
13649 break;
13650 }
13651 base = cu->header.read_address (obfd, buffer, &bytes_read);
13652 buffer += bytes_read;
13653 break;
13654 case DW_RLE_base_addressx:
13655 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13656 buffer += bytes_read;
13657 base = read_addr_index (cu, addr_index);
13658 break;
13659 case DW_RLE_start_length:
13660 if (buffer + cu->header.addr_size > buf_end)
13661 {
13662 overflow = true;
13663 break;
13664 }
13665 range_beginning = cu->header.read_address (obfd, buffer,
13666 &bytes_read);
13667 buffer += bytes_read;
13668 range_end = (range_beginning
13669 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13670 buffer += bytes_read;
13671 if (buffer > buf_end)
13672 {
13673 overflow = true;
13674 break;
13675 }
13676 break;
13677 case DW_RLE_startx_length:
13678 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13679 buffer += bytes_read;
13680 range_beginning = read_addr_index (cu, addr_index);
13681 if (buffer > buf_end)
13682 {
13683 overflow = true;
13684 break;
13685 }
13686 range_end = (range_beginning
13687 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13688 buffer += bytes_read;
13689 break;
13690 case DW_RLE_offset_pair:
13691 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13692 buffer += bytes_read;
13693 if (buffer > buf_end)
13694 {
13695 overflow = true;
13696 break;
13697 }
13698 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13699 buffer += bytes_read;
13700 if (buffer > buf_end)
13701 {
13702 overflow = true;
13703 break;
13704 }
13705 break;
13706 case DW_RLE_start_end:
13707 if (buffer + 2 * cu->header.addr_size > buf_end)
13708 {
13709 overflow = true;
13710 break;
13711 }
13712 range_beginning = cu->header.read_address (obfd, buffer,
13713 &bytes_read);
13714 buffer += bytes_read;
13715 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13716 buffer += bytes_read;
13717 break;
13718 case DW_RLE_startx_endx:
13719 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13720 buffer += bytes_read;
13721 range_beginning = read_addr_index (cu, addr_index);
13722 if (buffer > buf_end)
13723 {
13724 overflow = true;
13725 break;
13726 }
13727 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13728 buffer += bytes_read;
13729 range_end = read_addr_index (cu, addr_index);
13730 break;
13731 default:
13732 complaint (_("Invalid .debug_rnglists data (no base address)"));
13733 return false;
13734 }
13735 if (rlet == DW_RLE_end_of_list || overflow)
13736 break;
13737 if (rlet == DW_RLE_base_address)
13738 continue;
13739
13740 if (range_beginning > range_end)
13741 {
13742 /* Inverted range entries are invalid. */
13743 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13744 return false;
13745 }
13746
13747 /* Empty range entries have no effect. */
13748 if (range_beginning == range_end)
13749 continue;
13750
13751 /* Only DW_RLE_offset_pair needs the base address added. */
13752 if (rlet == DW_RLE_offset_pair)
13753 {
13754 if (!base.has_value ())
13755 {
13756 /* We have no valid base address for the DW_RLE_offset_pair. */
13757 complaint (_("Invalid .debug_rnglists data (no base address for "
13758 "DW_RLE_offset_pair)"));
13759 return false;
13760 }
13761
13762 range_beginning += *base;
13763 range_end += *base;
13764 }
13765
13766 /* A not-uncommon case of bad debug info.
13767 Don't pollute the addrmap with bad data. */
13768 if (range_beginning + baseaddr == 0
13769 && !per_objfile->per_bfd->has_section_at_zero)
13770 {
13771 complaint (_(".debug_rnglists entry has start address of zero"
13772 " [in module %s]"), objfile_name (objfile));
13773 continue;
13774 }
13775
13776 callback (range_beginning, range_end);
13777 }
13778
13779 if (overflow)
13780 {
13781 complaint (_("Offset %d is not terminated "
13782 "for DW_AT_ranges attribute"),
13783 offset);
13784 return false;
13785 }
13786
13787 return true;
13788 }
13789
13790 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13791 Callback's type should be:
13792 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13793 Return 1 if the attributes are present and valid, otherwise, return 0. */
13794
13795 template <typename Callback>
13796 static int
13797 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13798 Callback &&callback)
13799 {
13800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13801 struct objfile *objfile = per_objfile->objfile;
13802 struct comp_unit_head *cu_header = &cu->header;
13803 bfd *obfd = objfile->obfd;
13804 unsigned int addr_size = cu_header->addr_size;
13805 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13806 /* Base address selection entry. */
13807 gdb::optional<CORE_ADDR> base;
13808 unsigned int dummy;
13809 const gdb_byte *buffer;
13810 CORE_ADDR baseaddr;
13811
13812 if (cu_header->version >= 5)
13813 return dwarf2_rnglists_process (offset, cu, tag, callback);
13814
13815 base = cu->base_address;
13816
13817 per_objfile->per_bfd->ranges.read (objfile);
13818 if (offset >= per_objfile->per_bfd->ranges.size)
13819 {
13820 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13821 offset);
13822 return 0;
13823 }
13824 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13825
13826 baseaddr = objfile->text_section_offset ();
13827
13828 while (1)
13829 {
13830 CORE_ADDR range_beginning, range_end;
13831
13832 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13833 buffer += addr_size;
13834 range_end = cu->header.read_address (obfd, buffer, &dummy);
13835 buffer += addr_size;
13836 offset += 2 * addr_size;
13837
13838 /* An end of list marker is a pair of zero addresses. */
13839 if (range_beginning == 0 && range_end == 0)
13840 /* Found the end of list entry. */
13841 break;
13842
13843 /* Each base address selection entry is a pair of 2 values.
13844 The first is the largest possible address, the second is
13845 the base address. Check for a base address here. */
13846 if ((range_beginning & mask) == mask)
13847 {
13848 /* If we found the largest possible address, then we already
13849 have the base address in range_end. */
13850 base = range_end;
13851 continue;
13852 }
13853
13854 if (!base.has_value ())
13855 {
13856 /* We have no valid base address for the ranges
13857 data. */
13858 complaint (_("Invalid .debug_ranges data (no base address)"));
13859 return 0;
13860 }
13861
13862 if (range_beginning > range_end)
13863 {
13864 /* Inverted range entries are invalid. */
13865 complaint (_("Invalid .debug_ranges data (inverted range)"));
13866 return 0;
13867 }
13868
13869 /* Empty range entries have no effect. */
13870 if (range_beginning == range_end)
13871 continue;
13872
13873 range_beginning += *base;
13874 range_end += *base;
13875
13876 /* A not-uncommon case of bad debug info.
13877 Don't pollute the addrmap with bad data. */
13878 if (range_beginning + baseaddr == 0
13879 && !per_objfile->per_bfd->has_section_at_zero)
13880 {
13881 complaint (_(".debug_ranges entry has start address of zero"
13882 " [in module %s]"), objfile_name (objfile));
13883 continue;
13884 }
13885
13886 callback (range_beginning, range_end);
13887 }
13888
13889 return 1;
13890 }
13891
13892 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13893 Return 1 if the attributes are present and valid, otherwise, return 0.
13894 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
13895
13896 static int
13897 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13898 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13899 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
13900 {
13901 struct objfile *objfile = cu->per_objfile->objfile;
13902 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
13903 struct gdbarch *gdbarch = objfile->arch ();
13904 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13905 int low_set = 0;
13906 CORE_ADDR low = 0;
13907 CORE_ADDR high = 0;
13908 int retval;
13909
13910 retval = dwarf2_ranges_process (offset, cu, tag,
13911 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13912 {
13913 if (ranges_pst != NULL)
13914 {
13915 CORE_ADDR lowpc;
13916 CORE_ADDR highpc;
13917
13918 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13919 range_beginning + baseaddr)
13920 - baseaddr);
13921 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13922 range_end + baseaddr)
13923 - baseaddr);
13924 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
13925 lowpc, highpc - 1, ranges_pst);
13926 }
13927
13928 /* FIXME: This is recording everything as a low-high
13929 segment of consecutive addresses. We should have a
13930 data structure for discontiguous block ranges
13931 instead. */
13932 if (! low_set)
13933 {
13934 low = range_beginning;
13935 high = range_end;
13936 low_set = 1;
13937 }
13938 else
13939 {
13940 if (range_beginning < low)
13941 low = range_beginning;
13942 if (range_end > high)
13943 high = range_end;
13944 }
13945 });
13946 if (!retval)
13947 return 0;
13948
13949 if (! low_set)
13950 /* If the first entry is an end-of-list marker, the range
13951 describes an empty scope, i.e. no instructions. */
13952 return 0;
13953
13954 if (low_return)
13955 *low_return = low;
13956 if (high_return)
13957 *high_return = high;
13958 return 1;
13959 }
13960
13961 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13962 definition for the return value. *LOWPC and *HIGHPC are set iff
13963 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13964
13965 static enum pc_bounds_kind
13966 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13967 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13968 dwarf2_psymtab *pst)
13969 {
13970 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13971 struct attribute *attr;
13972 struct attribute *attr_high;
13973 CORE_ADDR low = 0;
13974 CORE_ADDR high = 0;
13975 enum pc_bounds_kind ret;
13976
13977 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13978 if (attr_high)
13979 {
13980 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13981 if (attr != nullptr)
13982 {
13983 low = attr->as_address ();
13984 high = attr_high->as_address ();
13985 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13986 high += low;
13987 }
13988 else
13989 /* Found high w/o low attribute. */
13990 return PC_BOUNDS_INVALID;
13991
13992 /* Found consecutive range of addresses. */
13993 ret = PC_BOUNDS_HIGH_LOW;
13994 }
13995 else
13996 {
13997 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13998 if (attr != nullptr && attr->form_is_unsigned ())
13999 {
14000 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14001 on DWARF version). */
14002 ULONGEST ranges_offset = attr->as_unsigned ();
14003
14004 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14005 this value. */
14006 if (die->tag != DW_TAG_compile_unit)
14007 ranges_offset += cu->gnu_ranges_base;
14008
14009 /* Value of the DW_AT_ranges attribute is the offset in the
14010 .debug_ranges section. */
14011 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14012 die->tag))
14013 return PC_BOUNDS_INVALID;
14014 /* Found discontinuous range of addresses. */
14015 ret = PC_BOUNDS_RANGES;
14016 }
14017 else
14018 return PC_BOUNDS_NOT_PRESENT;
14019 }
14020
14021 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14022 if (high <= low)
14023 return PC_BOUNDS_INVALID;
14024
14025 /* When using the GNU linker, .gnu.linkonce. sections are used to
14026 eliminate duplicate copies of functions and vtables and such.
14027 The linker will arbitrarily choose one and discard the others.
14028 The AT_*_pc values for such functions refer to local labels in
14029 these sections. If the section from that file was discarded, the
14030 labels are not in the output, so the relocs get a value of 0.
14031 If this is a discarded function, mark the pc bounds as invalid,
14032 so that GDB will ignore it. */
14033 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14034 return PC_BOUNDS_INVALID;
14035
14036 *lowpc = low;
14037 if (highpc)
14038 *highpc = high;
14039 return ret;
14040 }
14041
14042 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14043 its low and high PC addresses. Do nothing if these addresses could not
14044 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14045 and HIGHPC to the high address if greater than HIGHPC. */
14046
14047 static void
14048 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14049 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14050 struct dwarf2_cu *cu)
14051 {
14052 CORE_ADDR low, high;
14053 struct die_info *child = die->child;
14054
14055 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14056 {
14057 *lowpc = std::min (*lowpc, low);
14058 *highpc = std::max (*highpc, high);
14059 }
14060
14061 /* If the language does not allow nested subprograms (either inside
14062 subprograms or lexical blocks), we're done. */
14063 if (cu->language != language_ada)
14064 return;
14065
14066 /* Check all the children of the given DIE. If it contains nested
14067 subprograms, then check their pc bounds. Likewise, we need to
14068 check lexical blocks as well, as they may also contain subprogram
14069 definitions. */
14070 while (child && child->tag)
14071 {
14072 if (child->tag == DW_TAG_subprogram
14073 || child->tag == DW_TAG_lexical_block)
14074 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14075 child = child->sibling;
14076 }
14077 }
14078
14079 /* Get the low and high pc's represented by the scope DIE, and store
14080 them in *LOWPC and *HIGHPC. If the correct values can't be
14081 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14082
14083 static void
14084 get_scope_pc_bounds (struct die_info *die,
14085 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14086 struct dwarf2_cu *cu)
14087 {
14088 CORE_ADDR best_low = (CORE_ADDR) -1;
14089 CORE_ADDR best_high = (CORE_ADDR) 0;
14090 CORE_ADDR current_low, current_high;
14091
14092 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14093 >= PC_BOUNDS_RANGES)
14094 {
14095 best_low = current_low;
14096 best_high = current_high;
14097 }
14098 else
14099 {
14100 struct die_info *child = die->child;
14101
14102 while (child && child->tag)
14103 {
14104 switch (child->tag) {
14105 case DW_TAG_subprogram:
14106 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14107 break;
14108 case DW_TAG_namespace:
14109 case DW_TAG_module:
14110 /* FIXME: carlton/2004-01-16: Should we do this for
14111 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14112 that current GCC's always emit the DIEs corresponding
14113 to definitions of methods of classes as children of a
14114 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14115 the DIEs giving the declarations, which could be
14116 anywhere). But I don't see any reason why the
14117 standards says that they have to be there. */
14118 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14119
14120 if (current_low != ((CORE_ADDR) -1))
14121 {
14122 best_low = std::min (best_low, current_low);
14123 best_high = std::max (best_high, current_high);
14124 }
14125 break;
14126 default:
14127 /* Ignore. */
14128 break;
14129 }
14130
14131 child = child->sibling;
14132 }
14133 }
14134
14135 *lowpc = best_low;
14136 *highpc = best_high;
14137 }
14138
14139 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14140 in DIE. */
14141
14142 static void
14143 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14144 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14145 {
14146 struct objfile *objfile = cu->per_objfile->objfile;
14147 struct gdbarch *gdbarch = objfile->arch ();
14148 struct attribute *attr;
14149 struct attribute *attr_high;
14150
14151 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14152 if (attr_high)
14153 {
14154 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14155 if (attr != nullptr)
14156 {
14157 CORE_ADDR low = attr->as_address ();
14158 CORE_ADDR high = attr_high->as_address ();
14159
14160 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14161 high += low;
14162
14163 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14164 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14165 cu->get_builder ()->record_block_range (block, low, high - 1);
14166 }
14167 }
14168
14169 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14170 if (attr != nullptr && attr->form_is_unsigned ())
14171 {
14172 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14173 on DWARF version). */
14174 ULONGEST ranges_offset = attr->as_unsigned ();
14175
14176 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14177 this value. */
14178 if (die->tag != DW_TAG_compile_unit)
14179 ranges_offset += cu->gnu_ranges_base;
14180
14181 std::vector<blockrange> blockvec;
14182 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14183 [&] (CORE_ADDR start, CORE_ADDR end)
14184 {
14185 start += baseaddr;
14186 end += baseaddr;
14187 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14188 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14189 cu->get_builder ()->record_block_range (block, start, end - 1);
14190 blockvec.emplace_back (start, end);
14191 });
14192
14193 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14194 }
14195 }
14196
14197 /* Check whether the producer field indicates either of GCC < 4.6, or the
14198 Intel C/C++ compiler, and cache the result in CU. */
14199
14200 static void
14201 check_producer (struct dwarf2_cu *cu)
14202 {
14203 int major, minor;
14204
14205 if (cu->producer == NULL)
14206 {
14207 /* For unknown compilers expect their behavior is DWARF version
14208 compliant.
14209
14210 GCC started to support .debug_types sections by -gdwarf-4 since
14211 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14212 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14213 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14214 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14215 }
14216 else if (producer_is_gcc (cu->producer, &major, &minor))
14217 {
14218 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14219 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14220 }
14221 else if (producer_is_icc (cu->producer, &major, &minor))
14222 {
14223 cu->producer_is_icc = true;
14224 cu->producer_is_icc_lt_14 = major < 14;
14225 }
14226 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14227 cu->producer_is_codewarrior = true;
14228 else
14229 {
14230 /* For other non-GCC compilers, expect their behavior is DWARF version
14231 compliant. */
14232 }
14233
14234 cu->checked_producer = true;
14235 }
14236
14237 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14238 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14239 during 4.6.0 experimental. */
14240
14241 static bool
14242 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14243 {
14244 if (!cu->checked_producer)
14245 check_producer (cu);
14246
14247 return cu->producer_is_gxx_lt_4_6;
14248 }
14249
14250
14251 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14252 with incorrect is_stmt attributes. */
14253
14254 static bool
14255 producer_is_codewarrior (struct dwarf2_cu *cu)
14256 {
14257 if (!cu->checked_producer)
14258 check_producer (cu);
14259
14260 return cu->producer_is_codewarrior;
14261 }
14262
14263 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14264 If that attribute is not available, return the appropriate
14265 default. */
14266
14267 static enum dwarf_access_attribute
14268 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14269 {
14270 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14271 if (attr != nullptr)
14272 {
14273 LONGEST value = attr->constant_value (-1);
14274 if (value == DW_ACCESS_public
14275 || value == DW_ACCESS_protected
14276 || value == DW_ACCESS_private)
14277 return (dwarf_access_attribute) value;
14278 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14279 plongest (value));
14280 }
14281
14282 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14283 {
14284 /* The default DWARF 2 accessibility for members is public, the default
14285 accessibility for inheritance is private. */
14286
14287 if (die->tag != DW_TAG_inheritance)
14288 return DW_ACCESS_public;
14289 else
14290 return DW_ACCESS_private;
14291 }
14292 else
14293 {
14294 /* DWARF 3+ defines the default accessibility a different way. The same
14295 rules apply now for DW_TAG_inheritance as for the members and it only
14296 depends on the container kind. */
14297
14298 if (die->parent->tag == DW_TAG_class_type)
14299 return DW_ACCESS_private;
14300 else
14301 return DW_ACCESS_public;
14302 }
14303 }
14304
14305 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14306 offset. If the attribute was not found return 0, otherwise return
14307 1. If it was found but could not properly be handled, set *OFFSET
14308 to 0. */
14309
14310 static int
14311 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14312 LONGEST *offset)
14313 {
14314 struct attribute *attr;
14315
14316 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14317 if (attr != NULL)
14318 {
14319 *offset = 0;
14320
14321 /* Note that we do not check for a section offset first here.
14322 This is because DW_AT_data_member_location is new in DWARF 4,
14323 so if we see it, we can assume that a constant form is really
14324 a constant and not a section offset. */
14325 if (attr->form_is_constant ())
14326 *offset = attr->constant_value (0);
14327 else if (attr->form_is_section_offset ())
14328 dwarf2_complex_location_expr_complaint ();
14329 else if (attr->form_is_block ())
14330 *offset = decode_locdesc (attr->as_block (), cu);
14331 else
14332 dwarf2_complex_location_expr_complaint ();
14333
14334 return 1;
14335 }
14336
14337 return 0;
14338 }
14339
14340 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14341
14342 static void
14343 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14344 struct field *field)
14345 {
14346 struct attribute *attr;
14347
14348 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14349 if (attr != NULL)
14350 {
14351 if (attr->form_is_constant ())
14352 {
14353 LONGEST offset = attr->constant_value (0);
14354 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14355 }
14356 else if (attr->form_is_section_offset ())
14357 dwarf2_complex_location_expr_complaint ();
14358 else if (attr->form_is_block ())
14359 {
14360 bool handled;
14361 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14362 if (handled)
14363 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14364 else
14365 {
14366 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14367 struct objfile *objfile = per_objfile->objfile;
14368 struct dwarf2_locexpr_baton *dlbaton
14369 = XOBNEW (&objfile->objfile_obstack,
14370 struct dwarf2_locexpr_baton);
14371 dlbaton->data = attr->as_block ()->data;
14372 dlbaton->size = attr->as_block ()->size;
14373 /* When using this baton, we want to compute the address
14374 of the field, not the value. This is why
14375 is_reference is set to false here. */
14376 dlbaton->is_reference = false;
14377 dlbaton->per_objfile = per_objfile;
14378 dlbaton->per_cu = cu->per_cu;
14379
14380 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14381 }
14382 }
14383 else
14384 dwarf2_complex_location_expr_complaint ();
14385 }
14386 }
14387
14388 /* Add an aggregate field to the field list. */
14389
14390 static void
14391 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14392 struct dwarf2_cu *cu)
14393 {
14394 struct objfile *objfile = cu->per_objfile->objfile;
14395 struct gdbarch *gdbarch = objfile->arch ();
14396 struct nextfield *new_field;
14397 struct attribute *attr;
14398 struct field *fp;
14399 const char *fieldname = "";
14400
14401 if (die->tag == DW_TAG_inheritance)
14402 {
14403 fip->baseclasses.emplace_back ();
14404 new_field = &fip->baseclasses.back ();
14405 }
14406 else
14407 {
14408 fip->fields.emplace_back ();
14409 new_field = &fip->fields.back ();
14410 }
14411
14412 new_field->offset = die->sect_off;
14413
14414 new_field->accessibility = dwarf2_access_attribute (die, cu);
14415 if (new_field->accessibility != DW_ACCESS_public)
14416 fip->non_public_fields = true;
14417
14418 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14419 if (attr != nullptr)
14420 new_field->virtuality = attr->as_virtuality ();
14421 else
14422 new_field->virtuality = DW_VIRTUALITY_none;
14423
14424 fp = &new_field->field;
14425
14426 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14427 {
14428 /* Data member other than a C++ static data member. */
14429
14430 /* Get type of field. */
14431 fp->set_type (die_type (die, cu));
14432
14433 SET_FIELD_BITPOS (*fp, 0);
14434
14435 /* Get bit size of field (zero if none). */
14436 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14437 if (attr != nullptr)
14438 {
14439 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14440 }
14441 else
14442 {
14443 FIELD_BITSIZE (*fp) = 0;
14444 }
14445
14446 /* Get bit offset of field. */
14447 handle_data_member_location (die, cu, fp);
14448 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14449 if (attr != nullptr && attr->form_is_constant ())
14450 {
14451 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14452 {
14453 /* For big endian bits, the DW_AT_bit_offset gives the
14454 additional bit offset from the MSB of the containing
14455 anonymous object to the MSB of the field. We don't
14456 have to do anything special since we don't need to
14457 know the size of the anonymous object. */
14458 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14459 + attr->constant_value (0)));
14460 }
14461 else
14462 {
14463 /* For little endian bits, compute the bit offset to the
14464 MSB of the anonymous object, subtract off the number of
14465 bits from the MSB of the field to the MSB of the
14466 object, and then subtract off the number of bits of
14467 the field itself. The result is the bit offset of
14468 the LSB of the field. */
14469 int anonymous_size;
14470 int bit_offset = attr->constant_value (0);
14471
14472 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14473 if (attr != nullptr && attr->form_is_constant ())
14474 {
14475 /* The size of the anonymous object containing
14476 the bit field is explicit, so use the
14477 indicated size (in bytes). */
14478 anonymous_size = attr->constant_value (0);
14479 }
14480 else
14481 {
14482 /* The size of the anonymous object containing
14483 the bit field must be inferred from the type
14484 attribute of the data member containing the
14485 bit field. */
14486 anonymous_size = TYPE_LENGTH (fp->type ());
14487 }
14488 SET_FIELD_BITPOS (*fp,
14489 (FIELD_BITPOS (*fp)
14490 + anonymous_size * bits_per_byte
14491 - bit_offset - FIELD_BITSIZE (*fp)));
14492 }
14493 }
14494 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14495 if (attr != NULL)
14496 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14497 + attr->constant_value (0)));
14498
14499 /* Get name of field. */
14500 fieldname = dwarf2_name (die, cu);
14501 if (fieldname == NULL)
14502 fieldname = "";
14503
14504 /* The name is already allocated along with this objfile, so we don't
14505 need to duplicate it for the type. */
14506 fp->name = fieldname;
14507
14508 /* Change accessibility for artificial fields (e.g. virtual table
14509 pointer or virtual base class pointer) to private. */
14510 if (dwarf2_attr (die, DW_AT_artificial, cu))
14511 {
14512 FIELD_ARTIFICIAL (*fp) = 1;
14513 new_field->accessibility = DW_ACCESS_private;
14514 fip->non_public_fields = true;
14515 }
14516 }
14517 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14518 {
14519 /* C++ static member. */
14520
14521 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14522 is a declaration, but all versions of G++ as of this writing
14523 (so through at least 3.2.1) incorrectly generate
14524 DW_TAG_variable tags. */
14525
14526 const char *physname;
14527
14528 /* Get name of field. */
14529 fieldname = dwarf2_name (die, cu);
14530 if (fieldname == NULL)
14531 return;
14532
14533 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14534 if (attr
14535 /* Only create a symbol if this is an external value.
14536 new_symbol checks this and puts the value in the global symbol
14537 table, which we want. If it is not external, new_symbol
14538 will try to put the value in cu->list_in_scope which is wrong. */
14539 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14540 {
14541 /* A static const member, not much different than an enum as far as
14542 we're concerned, except that we can support more types. */
14543 new_symbol (die, NULL, cu);
14544 }
14545
14546 /* Get physical name. */
14547 physname = dwarf2_physname (fieldname, die, cu);
14548
14549 /* The name is already allocated along with this objfile, so we don't
14550 need to duplicate it for the type. */
14551 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14552 fp->set_type (die_type (die, cu));
14553 FIELD_NAME (*fp) = fieldname;
14554 }
14555 else if (die->tag == DW_TAG_inheritance)
14556 {
14557 /* C++ base class field. */
14558 handle_data_member_location (die, cu, fp);
14559 FIELD_BITSIZE (*fp) = 0;
14560 fp->set_type (die_type (die, cu));
14561 FIELD_NAME (*fp) = fp->type ()->name ();
14562 }
14563 else
14564 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14565 }
14566
14567 /* Can the type given by DIE define another type? */
14568
14569 static bool
14570 type_can_define_types (const struct die_info *die)
14571 {
14572 switch (die->tag)
14573 {
14574 case DW_TAG_typedef:
14575 case DW_TAG_class_type:
14576 case DW_TAG_structure_type:
14577 case DW_TAG_union_type:
14578 case DW_TAG_enumeration_type:
14579 return true;
14580
14581 default:
14582 return false;
14583 }
14584 }
14585
14586 /* Add a type definition defined in the scope of the FIP's class. */
14587
14588 static void
14589 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14590 struct dwarf2_cu *cu)
14591 {
14592 struct decl_field fp;
14593 memset (&fp, 0, sizeof (fp));
14594
14595 gdb_assert (type_can_define_types (die));
14596
14597 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14598 fp.name = dwarf2_name (die, cu);
14599 fp.type = read_type_die (die, cu);
14600
14601 /* Save accessibility. */
14602 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14603 switch (accessibility)
14604 {
14605 case DW_ACCESS_public:
14606 /* The assumed value if neither private nor protected. */
14607 break;
14608 case DW_ACCESS_private:
14609 fp.is_private = 1;
14610 break;
14611 case DW_ACCESS_protected:
14612 fp.is_protected = 1;
14613 break;
14614 }
14615
14616 if (die->tag == DW_TAG_typedef)
14617 fip->typedef_field_list.push_back (fp);
14618 else
14619 fip->nested_types_list.push_back (fp);
14620 }
14621
14622 /* A convenience typedef that's used when finding the discriminant
14623 field for a variant part. */
14624 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14625 offset_map_type;
14626
14627 /* Compute the discriminant range for a given variant. OBSTACK is
14628 where the results will be stored. VARIANT is the variant to
14629 process. IS_UNSIGNED indicates whether the discriminant is signed
14630 or unsigned. */
14631
14632 static const gdb::array_view<discriminant_range>
14633 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14634 bool is_unsigned)
14635 {
14636 std::vector<discriminant_range> ranges;
14637
14638 if (variant.default_branch)
14639 return {};
14640
14641 if (variant.discr_list_data == nullptr)
14642 {
14643 discriminant_range r
14644 = {variant.discriminant_value, variant.discriminant_value};
14645 ranges.push_back (r);
14646 }
14647 else
14648 {
14649 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14650 variant.discr_list_data->size);
14651 while (!data.empty ())
14652 {
14653 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14654 {
14655 complaint (_("invalid discriminant marker: %d"), data[0]);
14656 break;
14657 }
14658 bool is_range = data[0] == DW_DSC_range;
14659 data = data.slice (1);
14660
14661 ULONGEST low, high;
14662 unsigned int bytes_read;
14663
14664 if (data.empty ())
14665 {
14666 complaint (_("DW_AT_discr_list missing low value"));
14667 break;
14668 }
14669 if (is_unsigned)
14670 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14671 else
14672 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14673 &bytes_read);
14674 data = data.slice (bytes_read);
14675
14676 if (is_range)
14677 {
14678 if (data.empty ())
14679 {
14680 complaint (_("DW_AT_discr_list missing high value"));
14681 break;
14682 }
14683 if (is_unsigned)
14684 high = read_unsigned_leb128 (nullptr, data.data (),
14685 &bytes_read);
14686 else
14687 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14688 &bytes_read);
14689 data = data.slice (bytes_read);
14690 }
14691 else
14692 high = low;
14693
14694 ranges.push_back ({ low, high });
14695 }
14696 }
14697
14698 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14699 ranges.size ());
14700 std::copy (ranges.begin (), ranges.end (), result);
14701 return gdb::array_view<discriminant_range> (result, ranges.size ());
14702 }
14703
14704 static const gdb::array_view<variant_part> create_variant_parts
14705 (struct obstack *obstack,
14706 const offset_map_type &offset_map,
14707 struct field_info *fi,
14708 const std::vector<variant_part_builder> &variant_parts);
14709
14710 /* Fill in a "struct variant" for a given variant field. RESULT is
14711 the variant to fill in. OBSTACK is where any needed allocations
14712 will be done. OFFSET_MAP holds the mapping from section offsets to
14713 fields for the type. FI describes the fields of the type we're
14714 processing. FIELD is the variant field we're converting. */
14715
14716 static void
14717 create_one_variant (variant &result, struct obstack *obstack,
14718 const offset_map_type &offset_map,
14719 struct field_info *fi, const variant_field &field)
14720 {
14721 result.discriminants = convert_variant_range (obstack, field, false);
14722 result.first_field = field.first_field + fi->baseclasses.size ();
14723 result.last_field = field.last_field + fi->baseclasses.size ();
14724 result.parts = create_variant_parts (obstack, offset_map, fi,
14725 field.variant_parts);
14726 }
14727
14728 /* Fill in a "struct variant_part" for a given variant part. RESULT
14729 is the variant part to fill in. OBSTACK is where any needed
14730 allocations will be done. OFFSET_MAP holds the mapping from
14731 section offsets to fields for the type. FI describes the fields of
14732 the type we're processing. BUILDER is the variant part to be
14733 converted. */
14734
14735 static void
14736 create_one_variant_part (variant_part &result,
14737 struct obstack *obstack,
14738 const offset_map_type &offset_map,
14739 struct field_info *fi,
14740 const variant_part_builder &builder)
14741 {
14742 auto iter = offset_map.find (builder.discriminant_offset);
14743 if (iter == offset_map.end ())
14744 {
14745 result.discriminant_index = -1;
14746 /* Doesn't matter. */
14747 result.is_unsigned = false;
14748 }
14749 else
14750 {
14751 result.discriminant_index = iter->second;
14752 result.is_unsigned
14753 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14754 }
14755
14756 size_t n = builder.variants.size ();
14757 variant *output = new (obstack) variant[n];
14758 for (size_t i = 0; i < n; ++i)
14759 create_one_variant (output[i], obstack, offset_map, fi,
14760 builder.variants[i]);
14761
14762 result.variants = gdb::array_view<variant> (output, n);
14763 }
14764
14765 /* Create a vector of variant parts that can be attached to a type.
14766 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14767 holds the mapping from section offsets to fields for the type. FI
14768 describes the fields of the type we're processing. VARIANT_PARTS
14769 is the vector to convert. */
14770
14771 static const gdb::array_view<variant_part>
14772 create_variant_parts (struct obstack *obstack,
14773 const offset_map_type &offset_map,
14774 struct field_info *fi,
14775 const std::vector<variant_part_builder> &variant_parts)
14776 {
14777 if (variant_parts.empty ())
14778 return {};
14779
14780 size_t n = variant_parts.size ();
14781 variant_part *result = new (obstack) variant_part[n];
14782 for (size_t i = 0; i < n; ++i)
14783 create_one_variant_part (result[i], obstack, offset_map, fi,
14784 variant_parts[i]);
14785
14786 return gdb::array_view<variant_part> (result, n);
14787 }
14788
14789 /* Compute the variant part vector for FIP, attaching it to TYPE when
14790 done. */
14791
14792 static void
14793 add_variant_property (struct field_info *fip, struct type *type,
14794 struct dwarf2_cu *cu)
14795 {
14796 /* Map section offsets of fields to their field index. Note the
14797 field index here does not take the number of baseclasses into
14798 account. */
14799 offset_map_type offset_map;
14800 for (int i = 0; i < fip->fields.size (); ++i)
14801 offset_map[fip->fields[i].offset] = i;
14802
14803 struct objfile *objfile = cu->per_objfile->objfile;
14804 gdb::array_view<variant_part> parts
14805 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14806 fip->variant_parts);
14807
14808 struct dynamic_prop prop;
14809 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14810 obstack_copy (&objfile->objfile_obstack, &parts,
14811 sizeof (parts)));
14812
14813 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14814 }
14815
14816 /* Create the vector of fields, and attach it to the type. */
14817
14818 static void
14819 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14820 struct dwarf2_cu *cu)
14821 {
14822 int nfields = fip->nfields ();
14823
14824 /* Record the field count, allocate space for the array of fields,
14825 and create blank accessibility bitfields if necessary. */
14826 type->set_num_fields (nfields);
14827 type->set_fields
14828 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14829
14830 if (fip->non_public_fields && cu->language != language_ada)
14831 {
14832 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14833
14834 TYPE_FIELD_PRIVATE_BITS (type) =
14835 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14836 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14837
14838 TYPE_FIELD_PROTECTED_BITS (type) =
14839 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14840 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14841
14842 TYPE_FIELD_IGNORE_BITS (type) =
14843 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14844 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14845 }
14846
14847 /* If the type has baseclasses, allocate and clear a bit vector for
14848 TYPE_FIELD_VIRTUAL_BITS. */
14849 if (!fip->baseclasses.empty () && cu->language != language_ada)
14850 {
14851 int num_bytes = B_BYTES (fip->baseclasses.size ());
14852 unsigned char *pointer;
14853
14854 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14855 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14856 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14857 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14858 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14859 }
14860
14861 if (!fip->variant_parts.empty ())
14862 add_variant_property (fip, type, cu);
14863
14864 /* Copy the saved-up fields into the field vector. */
14865 for (int i = 0; i < nfields; ++i)
14866 {
14867 struct nextfield &field
14868 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14869 : fip->fields[i - fip->baseclasses.size ()]);
14870
14871 type->field (i) = field.field;
14872 switch (field.accessibility)
14873 {
14874 case DW_ACCESS_private:
14875 if (cu->language != language_ada)
14876 SET_TYPE_FIELD_PRIVATE (type, i);
14877 break;
14878
14879 case DW_ACCESS_protected:
14880 if (cu->language != language_ada)
14881 SET_TYPE_FIELD_PROTECTED (type, i);
14882 break;
14883
14884 case DW_ACCESS_public:
14885 break;
14886
14887 default:
14888 /* Unknown accessibility. Complain and treat it as public. */
14889 {
14890 complaint (_("unsupported accessibility %d"),
14891 field.accessibility);
14892 }
14893 break;
14894 }
14895 if (i < fip->baseclasses.size ())
14896 {
14897 switch (field.virtuality)
14898 {
14899 case DW_VIRTUALITY_virtual:
14900 case DW_VIRTUALITY_pure_virtual:
14901 if (cu->language == language_ada)
14902 error (_("unexpected virtuality in component of Ada type"));
14903 SET_TYPE_FIELD_VIRTUAL (type, i);
14904 break;
14905 }
14906 }
14907 }
14908 }
14909
14910 /* Return true if this member function is a constructor, false
14911 otherwise. */
14912
14913 static int
14914 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14915 {
14916 const char *fieldname;
14917 const char *type_name;
14918 int len;
14919
14920 if (die->parent == NULL)
14921 return 0;
14922
14923 if (die->parent->tag != DW_TAG_structure_type
14924 && die->parent->tag != DW_TAG_union_type
14925 && die->parent->tag != DW_TAG_class_type)
14926 return 0;
14927
14928 fieldname = dwarf2_name (die, cu);
14929 type_name = dwarf2_name (die->parent, cu);
14930 if (fieldname == NULL || type_name == NULL)
14931 return 0;
14932
14933 len = strlen (fieldname);
14934 return (strncmp (fieldname, type_name, len) == 0
14935 && (type_name[len] == '\0' || type_name[len] == '<'));
14936 }
14937
14938 /* Add a member function to the proper fieldlist. */
14939
14940 static void
14941 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14942 struct type *type, struct dwarf2_cu *cu)
14943 {
14944 struct objfile *objfile = cu->per_objfile->objfile;
14945 struct attribute *attr;
14946 int i;
14947 struct fnfieldlist *flp = nullptr;
14948 struct fn_field *fnp;
14949 const char *fieldname;
14950 struct type *this_type;
14951
14952 if (cu->language == language_ada)
14953 error (_("unexpected member function in Ada type"));
14954
14955 /* Get name of member function. */
14956 fieldname = dwarf2_name (die, cu);
14957 if (fieldname == NULL)
14958 return;
14959
14960 /* Look up member function name in fieldlist. */
14961 for (i = 0; i < fip->fnfieldlists.size (); i++)
14962 {
14963 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14964 {
14965 flp = &fip->fnfieldlists[i];
14966 break;
14967 }
14968 }
14969
14970 /* Create a new fnfieldlist if necessary. */
14971 if (flp == nullptr)
14972 {
14973 fip->fnfieldlists.emplace_back ();
14974 flp = &fip->fnfieldlists.back ();
14975 flp->name = fieldname;
14976 i = fip->fnfieldlists.size () - 1;
14977 }
14978
14979 /* Create a new member function field and add it to the vector of
14980 fnfieldlists. */
14981 flp->fnfields.emplace_back ();
14982 fnp = &flp->fnfields.back ();
14983
14984 /* Delay processing of the physname until later. */
14985 if (cu->language == language_cplus)
14986 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14987 die, cu);
14988 else
14989 {
14990 const char *physname = dwarf2_physname (fieldname, die, cu);
14991 fnp->physname = physname ? physname : "";
14992 }
14993
14994 fnp->type = alloc_type (objfile);
14995 this_type = read_type_die (die, cu);
14996 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14997 {
14998 int nparams = this_type->num_fields ();
14999
15000 /* TYPE is the domain of this method, and THIS_TYPE is the type
15001 of the method itself (TYPE_CODE_METHOD). */
15002 smash_to_method_type (fnp->type, type,
15003 TYPE_TARGET_TYPE (this_type),
15004 this_type->fields (),
15005 this_type->num_fields (),
15006 this_type->has_varargs ());
15007
15008 /* Handle static member functions.
15009 Dwarf2 has no clean way to discern C++ static and non-static
15010 member functions. G++ helps GDB by marking the first
15011 parameter for non-static member functions (which is the this
15012 pointer) as artificial. We obtain this information from
15013 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15014 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15015 fnp->voffset = VOFFSET_STATIC;
15016 }
15017 else
15018 complaint (_("member function type missing for '%s'"),
15019 dwarf2_full_name (fieldname, die, cu));
15020
15021 /* Get fcontext from DW_AT_containing_type if present. */
15022 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15023 fnp->fcontext = die_containing_type (die, cu);
15024
15025 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15026 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15027
15028 /* Get accessibility. */
15029 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15030 switch (accessibility)
15031 {
15032 case DW_ACCESS_private:
15033 fnp->is_private = 1;
15034 break;
15035 case DW_ACCESS_protected:
15036 fnp->is_protected = 1;
15037 break;
15038 }
15039
15040 /* Check for artificial methods. */
15041 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15042 if (attr && attr->as_boolean ())
15043 fnp->is_artificial = 1;
15044
15045 /* Check for defaulted methods. */
15046 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15047 if (attr != nullptr)
15048 fnp->defaulted = attr->defaulted ();
15049
15050 /* Check for deleted methods. */
15051 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15052 if (attr != nullptr && attr->as_boolean ())
15053 fnp->is_deleted = 1;
15054
15055 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15056
15057 /* Get index in virtual function table if it is a virtual member
15058 function. For older versions of GCC, this is an offset in the
15059 appropriate virtual table, as specified by DW_AT_containing_type.
15060 For everyone else, it is an expression to be evaluated relative
15061 to the object address. */
15062
15063 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15064 if (attr != nullptr)
15065 {
15066 if (attr->form_is_block () && attr->as_block ()->size > 0)
15067 {
15068 struct dwarf_block *block = attr->as_block ();
15069
15070 if (block->data[0] == DW_OP_constu)
15071 {
15072 /* Old-style GCC. */
15073 fnp->voffset = decode_locdesc (block, cu) + 2;
15074 }
15075 else if (block->data[0] == DW_OP_deref
15076 || (block->size > 1
15077 && block->data[0] == DW_OP_deref_size
15078 && block->data[1] == cu->header.addr_size))
15079 {
15080 fnp->voffset = decode_locdesc (block, cu);
15081 if ((fnp->voffset % cu->header.addr_size) != 0)
15082 dwarf2_complex_location_expr_complaint ();
15083 else
15084 fnp->voffset /= cu->header.addr_size;
15085 fnp->voffset += 2;
15086 }
15087 else
15088 dwarf2_complex_location_expr_complaint ();
15089
15090 if (!fnp->fcontext)
15091 {
15092 /* If there is no `this' field and no DW_AT_containing_type,
15093 we cannot actually find a base class context for the
15094 vtable! */
15095 if (this_type->num_fields () == 0
15096 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15097 {
15098 complaint (_("cannot determine context for virtual member "
15099 "function \"%s\" (offset %s)"),
15100 fieldname, sect_offset_str (die->sect_off));
15101 }
15102 else
15103 {
15104 fnp->fcontext
15105 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15106 }
15107 }
15108 }
15109 else if (attr->form_is_section_offset ())
15110 {
15111 dwarf2_complex_location_expr_complaint ();
15112 }
15113 else
15114 {
15115 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15116 fieldname);
15117 }
15118 }
15119 else
15120 {
15121 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15122 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15123 {
15124 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15125 complaint (_("Member function \"%s\" (offset %s) is virtual "
15126 "but the vtable offset is not specified"),
15127 fieldname, sect_offset_str (die->sect_off));
15128 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15129 TYPE_CPLUS_DYNAMIC (type) = 1;
15130 }
15131 }
15132 }
15133
15134 /* Create the vector of member function fields, and attach it to the type. */
15135
15136 static void
15137 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15138 struct dwarf2_cu *cu)
15139 {
15140 if (cu->language == language_ada)
15141 error (_("unexpected member functions in Ada type"));
15142
15143 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15144 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15145 TYPE_ALLOC (type,
15146 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15147
15148 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15149 {
15150 struct fnfieldlist &nf = fip->fnfieldlists[i];
15151 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15152
15153 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15154 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15155 fn_flp->fn_fields = (struct fn_field *)
15156 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15157
15158 for (int k = 0; k < nf.fnfields.size (); ++k)
15159 fn_flp->fn_fields[k] = nf.fnfields[k];
15160 }
15161
15162 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15163 }
15164
15165 /* Returns non-zero if NAME is the name of a vtable member in CU's
15166 language, zero otherwise. */
15167 static int
15168 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15169 {
15170 static const char vptr[] = "_vptr";
15171
15172 /* Look for the C++ form of the vtable. */
15173 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15174 return 1;
15175
15176 return 0;
15177 }
15178
15179 /* GCC outputs unnamed structures that are really pointers to member
15180 functions, with the ABI-specified layout. If TYPE describes
15181 such a structure, smash it into a member function type.
15182
15183 GCC shouldn't do this; it should just output pointer to member DIEs.
15184 This is GCC PR debug/28767. */
15185
15186 static void
15187 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15188 {
15189 struct type *pfn_type, *self_type, *new_type;
15190
15191 /* Check for a structure with no name and two children. */
15192 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15193 return;
15194
15195 /* Check for __pfn and __delta members. */
15196 if (TYPE_FIELD_NAME (type, 0) == NULL
15197 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15198 || TYPE_FIELD_NAME (type, 1) == NULL
15199 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15200 return;
15201
15202 /* Find the type of the method. */
15203 pfn_type = type->field (0).type ();
15204 if (pfn_type == NULL
15205 || pfn_type->code () != TYPE_CODE_PTR
15206 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15207 return;
15208
15209 /* Look for the "this" argument. */
15210 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15211 if (pfn_type->num_fields () == 0
15212 /* || pfn_type->field (0).type () == NULL */
15213 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15214 return;
15215
15216 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15217 new_type = alloc_type (objfile);
15218 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15219 pfn_type->fields (), pfn_type->num_fields (),
15220 pfn_type->has_varargs ());
15221 smash_to_methodptr_type (type, new_type);
15222 }
15223
15224 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15225 requires rewriting, then copy it and return the updated copy.
15226 Otherwise return nullptr. */
15227
15228 static struct type *
15229 rewrite_array_type (struct type *type)
15230 {
15231 if (type->code () != TYPE_CODE_ARRAY)
15232 return nullptr;
15233
15234 struct type *index_type = type->index_type ();
15235 range_bounds *current_bounds = index_type->bounds ();
15236
15237 /* Handle multi-dimensional arrays. */
15238 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15239 if (new_target == nullptr)
15240 {
15241 /* Maybe we don't need to rewrite this array. */
15242 if (current_bounds->low.kind () == PROP_CONST
15243 && current_bounds->high.kind () == PROP_CONST)
15244 return nullptr;
15245 }
15246
15247 /* Either the target type was rewritten, or the bounds have to be
15248 updated. Either way we want to copy the type and update
15249 everything. */
15250 struct type *copy = copy_type (type);
15251 int nfields = copy->num_fields ();
15252 field *new_fields
15253 = ((struct field *) TYPE_ZALLOC (copy,
15254 nfields * sizeof (struct field)));
15255 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15256 copy->set_fields (new_fields);
15257 if (new_target != nullptr)
15258 TYPE_TARGET_TYPE (copy) = new_target;
15259
15260 struct type *index_copy = copy_type (index_type);
15261 range_bounds *bounds
15262 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15263 sizeof (range_bounds));
15264 *bounds = *current_bounds;
15265 bounds->low.set_const_val (1);
15266 bounds->high.set_const_val (0);
15267 index_copy->set_bounds (bounds);
15268 copy->set_index_type (index_copy);
15269
15270 return copy;
15271 }
15272
15273 /* While some versions of GCC will generate complicated DWARF for an
15274 array (see quirk_ada_thick_pointer), more recent versions were
15275 modified to emit an explicit thick pointer structure. However, in
15276 this case, the array still has DWARF expressions for its ranges,
15277 and these must be ignored. */
15278
15279 static void
15280 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15281 struct type *type)
15282 {
15283 gdb_assert (cu->language == language_ada);
15284
15285 /* Check for a structure with two children. */
15286 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15287 return;
15288
15289 /* Check for P_ARRAY and P_BOUNDS members. */
15290 if (TYPE_FIELD_NAME (type, 0) == NULL
15291 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15292 || TYPE_FIELD_NAME (type, 1) == NULL
15293 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15294 return;
15295
15296 /* Make sure we're looking at a pointer to an array. */
15297 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15298 return;
15299
15300 /* The Ada code already knows how to handle these types, so all that
15301 we need to do is turn the bounds into static bounds. However, we
15302 don't want to rewrite existing array or index types in-place,
15303 because those may be referenced in other contexts where this
15304 rewriting is undesirable. */
15305 struct type *new_ary_type
15306 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15307 if (new_ary_type != nullptr)
15308 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15309 }
15310
15311 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15312 appropriate error checking and issuing complaints if there is a
15313 problem. */
15314
15315 static ULONGEST
15316 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15317 {
15318 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15319
15320 if (attr == nullptr)
15321 return 0;
15322
15323 if (!attr->form_is_constant ())
15324 {
15325 complaint (_("DW_AT_alignment must have constant form"
15326 " - DIE at %s [in module %s]"),
15327 sect_offset_str (die->sect_off),
15328 objfile_name (cu->per_objfile->objfile));
15329 return 0;
15330 }
15331
15332 LONGEST val = attr->constant_value (0);
15333 if (val < 0)
15334 {
15335 complaint (_("DW_AT_alignment value must not be negative"
15336 " - DIE at %s [in module %s]"),
15337 sect_offset_str (die->sect_off),
15338 objfile_name (cu->per_objfile->objfile));
15339 return 0;
15340 }
15341 ULONGEST align = val;
15342
15343 if (align == 0)
15344 {
15345 complaint (_("DW_AT_alignment value must not be zero"
15346 " - DIE at %s [in module %s]"),
15347 sect_offset_str (die->sect_off),
15348 objfile_name (cu->per_objfile->objfile));
15349 return 0;
15350 }
15351 if ((align & (align - 1)) != 0)
15352 {
15353 complaint (_("DW_AT_alignment value must be a power of 2"
15354 " - DIE at %s [in module %s]"),
15355 sect_offset_str (die->sect_off),
15356 objfile_name (cu->per_objfile->objfile));
15357 return 0;
15358 }
15359
15360 return align;
15361 }
15362
15363 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15364 the alignment for TYPE. */
15365
15366 static void
15367 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15368 struct type *type)
15369 {
15370 if (!set_type_align (type, get_alignment (cu, die)))
15371 complaint (_("DW_AT_alignment value too large"
15372 " - DIE at %s [in module %s]"),
15373 sect_offset_str (die->sect_off),
15374 objfile_name (cu->per_objfile->objfile));
15375 }
15376
15377 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15378 constant for a type, according to DWARF5 spec, Table 5.5. */
15379
15380 static bool
15381 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15382 {
15383 switch (value)
15384 {
15385 case DW_CC_normal:
15386 case DW_CC_pass_by_reference:
15387 case DW_CC_pass_by_value:
15388 return true;
15389
15390 default:
15391 complaint (_("unrecognized DW_AT_calling_convention value "
15392 "(%s) for a type"), pulongest (value));
15393 return false;
15394 }
15395 }
15396
15397 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15398 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15399 also according to GNU-specific values (see include/dwarf2.h). */
15400
15401 static bool
15402 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15403 {
15404 switch (value)
15405 {
15406 case DW_CC_normal:
15407 case DW_CC_program:
15408 case DW_CC_nocall:
15409 return true;
15410
15411 case DW_CC_GNU_renesas_sh:
15412 case DW_CC_GNU_borland_fastcall_i386:
15413 case DW_CC_GDB_IBM_OpenCL:
15414 return true;
15415
15416 default:
15417 complaint (_("unrecognized DW_AT_calling_convention value "
15418 "(%s) for a subroutine"), pulongest (value));
15419 return false;
15420 }
15421 }
15422
15423 /* Called when we find the DIE that starts a structure or union scope
15424 (definition) to create a type for the structure or union. Fill in
15425 the type's name and general properties; the members will not be
15426 processed until process_structure_scope. A symbol table entry for
15427 the type will also not be done until process_structure_scope (assuming
15428 the type has a name).
15429
15430 NOTE: we need to call these functions regardless of whether or not the
15431 DIE has a DW_AT_name attribute, since it might be an anonymous
15432 structure or union. This gets the type entered into our set of
15433 user defined types. */
15434
15435 static struct type *
15436 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15437 {
15438 struct objfile *objfile = cu->per_objfile->objfile;
15439 struct type *type;
15440 struct attribute *attr;
15441 const char *name;
15442
15443 /* If the definition of this type lives in .debug_types, read that type.
15444 Don't follow DW_AT_specification though, that will take us back up
15445 the chain and we want to go down. */
15446 attr = die->attr (DW_AT_signature);
15447 if (attr != nullptr)
15448 {
15449 type = get_DW_AT_signature_type (die, attr, cu);
15450
15451 /* The type's CU may not be the same as CU.
15452 Ensure TYPE is recorded with CU in die_type_hash. */
15453 return set_die_type (die, type, cu);
15454 }
15455
15456 type = alloc_type (objfile);
15457 INIT_CPLUS_SPECIFIC (type);
15458
15459 name = dwarf2_name (die, cu);
15460 if (name != NULL)
15461 {
15462 if (cu->language == language_cplus
15463 || cu->language == language_d
15464 || cu->language == language_rust)
15465 {
15466 const char *full_name = dwarf2_full_name (name, die, cu);
15467
15468 /* dwarf2_full_name might have already finished building the DIE's
15469 type. If so, there is no need to continue. */
15470 if (get_die_type (die, cu) != NULL)
15471 return get_die_type (die, cu);
15472
15473 type->set_name (full_name);
15474 }
15475 else
15476 {
15477 /* The name is already allocated along with this objfile, so
15478 we don't need to duplicate it for the type. */
15479 type->set_name (name);
15480 }
15481 }
15482
15483 if (die->tag == DW_TAG_structure_type)
15484 {
15485 type->set_code (TYPE_CODE_STRUCT);
15486 }
15487 else if (die->tag == DW_TAG_union_type)
15488 {
15489 type->set_code (TYPE_CODE_UNION);
15490 }
15491 else
15492 {
15493 type->set_code (TYPE_CODE_STRUCT);
15494 }
15495
15496 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15497 type->set_is_declared_class (true);
15498
15499 /* Store the calling convention in the type if it's available in
15500 the die. Otherwise the calling convention remains set to
15501 the default value DW_CC_normal. */
15502 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15503 if (attr != nullptr
15504 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15505 {
15506 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15507 TYPE_CPLUS_CALLING_CONVENTION (type)
15508 = (enum dwarf_calling_convention) (attr->constant_value (0));
15509 }
15510
15511 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15512 if (attr != nullptr)
15513 {
15514 if (attr->form_is_constant ())
15515 TYPE_LENGTH (type) = attr->constant_value (0);
15516 else
15517 {
15518 struct dynamic_prop prop;
15519 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15520 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15521 TYPE_LENGTH (type) = 0;
15522 }
15523 }
15524 else
15525 {
15526 TYPE_LENGTH (type) = 0;
15527 }
15528
15529 maybe_set_alignment (cu, die, type);
15530
15531 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15532 {
15533 /* ICC<14 does not output the required DW_AT_declaration on
15534 incomplete types, but gives them a size of zero. */
15535 type->set_is_stub (true);
15536 }
15537 else
15538 type->set_stub_is_supported (true);
15539
15540 if (die_is_declaration (die, cu))
15541 type->set_is_stub (true);
15542 else if (attr == NULL && die->child == NULL
15543 && producer_is_realview (cu->producer))
15544 /* RealView does not output the required DW_AT_declaration
15545 on incomplete types. */
15546 type->set_is_stub (true);
15547
15548 /* We need to add the type field to the die immediately so we don't
15549 infinitely recurse when dealing with pointers to the structure
15550 type within the structure itself. */
15551 set_die_type (die, type, cu);
15552
15553 /* set_die_type should be already done. */
15554 set_descriptive_type (type, die, cu);
15555
15556 return type;
15557 }
15558
15559 static void handle_struct_member_die
15560 (struct die_info *child_die,
15561 struct type *type,
15562 struct field_info *fi,
15563 std::vector<struct symbol *> *template_args,
15564 struct dwarf2_cu *cu);
15565
15566 /* A helper for handle_struct_member_die that handles
15567 DW_TAG_variant_part. */
15568
15569 static void
15570 handle_variant_part (struct die_info *die, struct type *type,
15571 struct field_info *fi,
15572 std::vector<struct symbol *> *template_args,
15573 struct dwarf2_cu *cu)
15574 {
15575 variant_part_builder *new_part;
15576 if (fi->current_variant_part == nullptr)
15577 {
15578 fi->variant_parts.emplace_back ();
15579 new_part = &fi->variant_parts.back ();
15580 }
15581 else if (!fi->current_variant_part->processing_variant)
15582 {
15583 complaint (_("nested DW_TAG_variant_part seen "
15584 "- DIE at %s [in module %s]"),
15585 sect_offset_str (die->sect_off),
15586 objfile_name (cu->per_objfile->objfile));
15587 return;
15588 }
15589 else
15590 {
15591 variant_field &current = fi->current_variant_part->variants.back ();
15592 current.variant_parts.emplace_back ();
15593 new_part = &current.variant_parts.back ();
15594 }
15595
15596 /* When we recurse, we want callees to add to this new variant
15597 part. */
15598 scoped_restore save_current_variant_part
15599 = make_scoped_restore (&fi->current_variant_part, new_part);
15600
15601 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15602 if (discr == NULL)
15603 {
15604 /* It's a univariant form, an extension we support. */
15605 }
15606 else if (discr->form_is_ref ())
15607 {
15608 struct dwarf2_cu *target_cu = cu;
15609 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15610
15611 new_part->discriminant_offset = target_die->sect_off;
15612 }
15613 else
15614 {
15615 complaint (_("DW_AT_discr does not have DIE reference form"
15616 " - DIE at %s [in module %s]"),
15617 sect_offset_str (die->sect_off),
15618 objfile_name (cu->per_objfile->objfile));
15619 }
15620
15621 for (die_info *child_die = die->child;
15622 child_die != NULL;
15623 child_die = child_die->sibling)
15624 handle_struct_member_die (child_die, type, fi, template_args, cu);
15625 }
15626
15627 /* A helper for handle_struct_member_die that handles
15628 DW_TAG_variant. */
15629
15630 static void
15631 handle_variant (struct die_info *die, struct type *type,
15632 struct field_info *fi,
15633 std::vector<struct symbol *> *template_args,
15634 struct dwarf2_cu *cu)
15635 {
15636 if (fi->current_variant_part == nullptr)
15637 {
15638 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15639 "- DIE at %s [in module %s]"),
15640 sect_offset_str (die->sect_off),
15641 objfile_name (cu->per_objfile->objfile));
15642 return;
15643 }
15644 if (fi->current_variant_part->processing_variant)
15645 {
15646 complaint (_("nested DW_TAG_variant seen "
15647 "- DIE at %s [in module %s]"),
15648 sect_offset_str (die->sect_off),
15649 objfile_name (cu->per_objfile->objfile));
15650 return;
15651 }
15652
15653 scoped_restore save_processing_variant
15654 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15655 true);
15656
15657 fi->current_variant_part->variants.emplace_back ();
15658 variant_field &variant = fi->current_variant_part->variants.back ();
15659 variant.first_field = fi->fields.size ();
15660
15661 /* In a variant we want to get the discriminant and also add a
15662 field for our sole member child. */
15663 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15664 if (discr == nullptr || !discr->form_is_constant ())
15665 {
15666 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15667 if (discr == nullptr || discr->as_block ()->size == 0)
15668 variant.default_branch = true;
15669 else
15670 variant.discr_list_data = discr->as_block ();
15671 }
15672 else
15673 variant.discriminant_value = discr->constant_value (0);
15674
15675 for (die_info *variant_child = die->child;
15676 variant_child != NULL;
15677 variant_child = variant_child->sibling)
15678 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15679
15680 variant.last_field = fi->fields.size ();
15681 }
15682
15683 /* A helper for process_structure_scope that handles a single member
15684 DIE. */
15685
15686 static void
15687 handle_struct_member_die (struct die_info *child_die, struct type *type,
15688 struct field_info *fi,
15689 std::vector<struct symbol *> *template_args,
15690 struct dwarf2_cu *cu)
15691 {
15692 if (child_die->tag == DW_TAG_member
15693 || child_die->tag == DW_TAG_variable)
15694 {
15695 /* NOTE: carlton/2002-11-05: A C++ static data member
15696 should be a DW_TAG_member that is a declaration, but
15697 all versions of G++ as of this writing (so through at
15698 least 3.2.1) incorrectly generate DW_TAG_variable
15699 tags for them instead. */
15700 dwarf2_add_field (fi, child_die, cu);
15701 }
15702 else if (child_die->tag == DW_TAG_subprogram)
15703 {
15704 /* Rust doesn't have member functions in the C++ sense.
15705 However, it does emit ordinary functions as children
15706 of a struct DIE. */
15707 if (cu->language == language_rust)
15708 read_func_scope (child_die, cu);
15709 else
15710 {
15711 /* C++ member function. */
15712 dwarf2_add_member_fn (fi, child_die, type, cu);
15713 }
15714 }
15715 else if (child_die->tag == DW_TAG_inheritance)
15716 {
15717 /* C++ base class field. */
15718 dwarf2_add_field (fi, child_die, cu);
15719 }
15720 else if (type_can_define_types (child_die))
15721 dwarf2_add_type_defn (fi, child_die, cu);
15722 else if (child_die->tag == DW_TAG_template_type_param
15723 || child_die->tag == DW_TAG_template_value_param)
15724 {
15725 struct symbol *arg = new_symbol (child_die, NULL, cu);
15726
15727 if (arg != NULL)
15728 template_args->push_back (arg);
15729 }
15730 else if (child_die->tag == DW_TAG_variant_part)
15731 handle_variant_part (child_die, type, fi, template_args, cu);
15732 else if (child_die->tag == DW_TAG_variant)
15733 handle_variant (child_die, type, fi, template_args, cu);
15734 }
15735
15736 /* Finish creating a structure or union type, including filling in
15737 its members and creating a symbol for it. */
15738
15739 static void
15740 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15741 {
15742 struct objfile *objfile = cu->per_objfile->objfile;
15743 struct die_info *child_die;
15744 struct type *type;
15745
15746 type = get_die_type (die, cu);
15747 if (type == NULL)
15748 type = read_structure_type (die, cu);
15749
15750 bool has_template_parameters = false;
15751 if (die->child != NULL && ! die_is_declaration (die, cu))
15752 {
15753 struct field_info fi;
15754 std::vector<struct symbol *> template_args;
15755
15756 child_die = die->child;
15757
15758 while (child_die && child_die->tag)
15759 {
15760 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15761 child_die = child_die->sibling;
15762 }
15763
15764 /* Attach template arguments to type. */
15765 if (!template_args.empty ())
15766 {
15767 has_template_parameters = true;
15768 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15769 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15770 TYPE_TEMPLATE_ARGUMENTS (type)
15771 = XOBNEWVEC (&objfile->objfile_obstack,
15772 struct symbol *,
15773 TYPE_N_TEMPLATE_ARGUMENTS (type));
15774 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15775 template_args.data (),
15776 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15777 * sizeof (struct symbol *)));
15778 }
15779
15780 /* Attach fields and member functions to the type. */
15781 if (fi.nfields () > 0)
15782 dwarf2_attach_fields_to_type (&fi, type, cu);
15783 if (!fi.fnfieldlists.empty ())
15784 {
15785 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15786
15787 /* Get the type which refers to the base class (possibly this
15788 class itself) which contains the vtable pointer for the current
15789 class from the DW_AT_containing_type attribute. This use of
15790 DW_AT_containing_type is a GNU extension. */
15791
15792 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15793 {
15794 struct type *t = die_containing_type (die, cu);
15795
15796 set_type_vptr_basetype (type, t);
15797 if (type == t)
15798 {
15799 int i;
15800
15801 /* Our own class provides vtbl ptr. */
15802 for (i = t->num_fields () - 1;
15803 i >= TYPE_N_BASECLASSES (t);
15804 --i)
15805 {
15806 const char *fieldname = TYPE_FIELD_NAME (t, i);
15807
15808 if (is_vtable_name (fieldname, cu))
15809 {
15810 set_type_vptr_fieldno (type, i);
15811 break;
15812 }
15813 }
15814
15815 /* Complain if virtual function table field not found. */
15816 if (i < TYPE_N_BASECLASSES (t))
15817 complaint (_("virtual function table pointer "
15818 "not found when defining class '%s'"),
15819 type->name () ? type->name () : "");
15820 }
15821 else
15822 {
15823 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15824 }
15825 }
15826 else if (cu->producer
15827 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15828 {
15829 /* The IBM XLC compiler does not provide direct indication
15830 of the containing type, but the vtable pointer is
15831 always named __vfp. */
15832
15833 int i;
15834
15835 for (i = type->num_fields () - 1;
15836 i >= TYPE_N_BASECLASSES (type);
15837 --i)
15838 {
15839 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15840 {
15841 set_type_vptr_fieldno (type, i);
15842 set_type_vptr_basetype (type, type);
15843 break;
15844 }
15845 }
15846 }
15847 }
15848
15849 /* Copy fi.typedef_field_list linked list elements content into the
15850 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15851 if (!fi.typedef_field_list.empty ())
15852 {
15853 int count = fi.typedef_field_list.size ();
15854
15855 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15856 TYPE_TYPEDEF_FIELD_ARRAY (type)
15857 = ((struct decl_field *)
15858 TYPE_ALLOC (type,
15859 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15860 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15861
15862 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15863 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15864 }
15865
15866 /* Copy fi.nested_types_list linked list elements content into the
15867 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15868 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15869 {
15870 int count = fi.nested_types_list.size ();
15871
15872 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15873 TYPE_NESTED_TYPES_ARRAY (type)
15874 = ((struct decl_field *)
15875 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15876 TYPE_NESTED_TYPES_COUNT (type) = count;
15877
15878 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15879 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15880 }
15881 }
15882
15883 quirk_gcc_member_function_pointer (type, objfile);
15884 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15885 cu->rust_unions.push_back (type);
15886 else if (cu->language == language_ada)
15887 quirk_ada_thick_pointer_struct (die, cu, type);
15888
15889 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15890 snapshots) has been known to create a die giving a declaration
15891 for a class that has, as a child, a die giving a definition for a
15892 nested class. So we have to process our children even if the
15893 current die is a declaration. Normally, of course, a declaration
15894 won't have any children at all. */
15895
15896 child_die = die->child;
15897
15898 while (child_die != NULL && child_die->tag)
15899 {
15900 if (child_die->tag == DW_TAG_member
15901 || child_die->tag == DW_TAG_variable
15902 || child_die->tag == DW_TAG_inheritance
15903 || child_die->tag == DW_TAG_template_value_param
15904 || child_die->tag == DW_TAG_template_type_param)
15905 {
15906 /* Do nothing. */
15907 }
15908 else
15909 process_die (child_die, cu);
15910
15911 child_die = child_die->sibling;
15912 }
15913
15914 /* Do not consider external references. According to the DWARF standard,
15915 these DIEs are identified by the fact that they have no byte_size
15916 attribute, and a declaration attribute. */
15917 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15918 || !die_is_declaration (die, cu)
15919 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15920 {
15921 struct symbol *sym = new_symbol (die, type, cu);
15922
15923 if (has_template_parameters)
15924 {
15925 struct symtab *symtab;
15926 if (sym != nullptr)
15927 symtab = symbol_symtab (sym);
15928 else if (cu->line_header != nullptr)
15929 {
15930 /* Any related symtab will do. */
15931 symtab
15932 = cu->line_header->file_names ()[0].symtab;
15933 }
15934 else
15935 {
15936 symtab = nullptr;
15937 complaint (_("could not find suitable "
15938 "symtab for template parameter"
15939 " - DIE at %s [in module %s]"),
15940 sect_offset_str (die->sect_off),
15941 objfile_name (objfile));
15942 }
15943
15944 if (symtab != nullptr)
15945 {
15946 /* Make sure that the symtab is set on the new symbols.
15947 Even though they don't appear in this symtab directly,
15948 other parts of gdb assume that symbols do, and this is
15949 reasonably true. */
15950 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15951 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15952 }
15953 }
15954 }
15955 }
15956
15957 /* Assuming DIE is an enumeration type, and TYPE is its associated
15958 type, update TYPE using some information only available in DIE's
15959 children. In particular, the fields are computed. */
15960
15961 static void
15962 update_enumeration_type_from_children (struct die_info *die,
15963 struct type *type,
15964 struct dwarf2_cu *cu)
15965 {
15966 struct die_info *child_die;
15967 int unsigned_enum = 1;
15968 int flag_enum = 1;
15969
15970 auto_obstack obstack;
15971 std::vector<struct field> fields;
15972
15973 for (child_die = die->child;
15974 child_die != NULL && child_die->tag;
15975 child_die = child_die->sibling)
15976 {
15977 struct attribute *attr;
15978 LONGEST value;
15979 const gdb_byte *bytes;
15980 struct dwarf2_locexpr_baton *baton;
15981 const char *name;
15982
15983 if (child_die->tag != DW_TAG_enumerator)
15984 continue;
15985
15986 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15987 if (attr == NULL)
15988 continue;
15989
15990 name = dwarf2_name (child_die, cu);
15991 if (name == NULL)
15992 name = "<anonymous enumerator>";
15993
15994 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15995 &value, &bytes, &baton);
15996 if (value < 0)
15997 {
15998 unsigned_enum = 0;
15999 flag_enum = 0;
16000 }
16001 else
16002 {
16003 if (count_one_bits_ll (value) >= 2)
16004 flag_enum = 0;
16005 }
16006
16007 fields.emplace_back ();
16008 struct field &field = fields.back ();
16009 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16010 SET_FIELD_ENUMVAL (field, value);
16011 }
16012
16013 if (!fields.empty ())
16014 {
16015 type->set_num_fields (fields.size ());
16016 type->set_fields
16017 ((struct field *)
16018 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16019 memcpy (type->fields (), fields.data (),
16020 sizeof (struct field) * fields.size ());
16021 }
16022
16023 if (unsigned_enum)
16024 type->set_is_unsigned (true);
16025
16026 if (flag_enum)
16027 type->set_is_flag_enum (true);
16028 }
16029
16030 /* Given a DW_AT_enumeration_type die, set its type. We do not
16031 complete the type's fields yet, or create any symbols. */
16032
16033 static struct type *
16034 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16035 {
16036 struct objfile *objfile = cu->per_objfile->objfile;
16037 struct type *type;
16038 struct attribute *attr;
16039 const char *name;
16040
16041 /* If the definition of this type lives in .debug_types, read that type.
16042 Don't follow DW_AT_specification though, that will take us back up
16043 the chain and we want to go down. */
16044 attr = die->attr (DW_AT_signature);
16045 if (attr != nullptr)
16046 {
16047 type = get_DW_AT_signature_type (die, attr, cu);
16048
16049 /* The type's CU may not be the same as CU.
16050 Ensure TYPE is recorded with CU in die_type_hash. */
16051 return set_die_type (die, type, cu);
16052 }
16053
16054 type = alloc_type (objfile);
16055
16056 type->set_code (TYPE_CODE_ENUM);
16057 name = dwarf2_full_name (NULL, die, cu);
16058 if (name != NULL)
16059 type->set_name (name);
16060
16061 attr = dwarf2_attr (die, DW_AT_type, cu);
16062 if (attr != NULL)
16063 {
16064 struct type *underlying_type = die_type (die, cu);
16065
16066 TYPE_TARGET_TYPE (type) = underlying_type;
16067 }
16068
16069 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16070 if (attr != nullptr)
16071 {
16072 TYPE_LENGTH (type) = attr->constant_value (0);
16073 }
16074 else
16075 {
16076 TYPE_LENGTH (type) = 0;
16077 }
16078
16079 maybe_set_alignment (cu, die, type);
16080
16081 /* The enumeration DIE can be incomplete. In Ada, any type can be
16082 declared as private in the package spec, and then defined only
16083 inside the package body. Such types are known as Taft Amendment
16084 Types. When another package uses such a type, an incomplete DIE
16085 may be generated by the compiler. */
16086 if (die_is_declaration (die, cu))
16087 type->set_is_stub (true);
16088
16089 /* If this type has an underlying type that is not a stub, then we
16090 may use its attributes. We always use the "unsigned" attribute
16091 in this situation, because ordinarily we guess whether the type
16092 is unsigned -- but the guess can be wrong and the underlying type
16093 can tell us the reality. However, we defer to a local size
16094 attribute if one exists, because this lets the compiler override
16095 the underlying type if needed. */
16096 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16097 {
16098 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16099 underlying_type = check_typedef (underlying_type);
16100
16101 type->set_is_unsigned (underlying_type->is_unsigned ());
16102
16103 if (TYPE_LENGTH (type) == 0)
16104 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16105
16106 if (TYPE_RAW_ALIGN (type) == 0
16107 && TYPE_RAW_ALIGN (underlying_type) != 0)
16108 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16109 }
16110
16111 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16112
16113 set_die_type (die, type, cu);
16114
16115 /* Finish the creation of this type by using the enum's children.
16116 Note that, as usual, this must come after set_die_type to avoid
16117 infinite recursion when trying to compute the names of the
16118 enumerators. */
16119 update_enumeration_type_from_children (die, type, cu);
16120
16121 return type;
16122 }
16123
16124 /* Given a pointer to a die which begins an enumeration, process all
16125 the dies that define the members of the enumeration, and create the
16126 symbol for the enumeration type.
16127
16128 NOTE: We reverse the order of the element list. */
16129
16130 static void
16131 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16132 {
16133 struct type *this_type;
16134
16135 this_type = get_die_type (die, cu);
16136 if (this_type == NULL)
16137 this_type = read_enumeration_type (die, cu);
16138
16139 if (die->child != NULL)
16140 {
16141 struct die_info *child_die;
16142 const char *name;
16143
16144 child_die = die->child;
16145 while (child_die && child_die->tag)
16146 {
16147 if (child_die->tag != DW_TAG_enumerator)
16148 {
16149 process_die (child_die, cu);
16150 }
16151 else
16152 {
16153 name = dwarf2_name (child_die, cu);
16154 if (name)
16155 new_symbol (child_die, this_type, cu);
16156 }
16157
16158 child_die = child_die->sibling;
16159 }
16160 }
16161
16162 /* If we are reading an enum from a .debug_types unit, and the enum
16163 is a declaration, and the enum is not the signatured type in the
16164 unit, then we do not want to add a symbol for it. Adding a
16165 symbol would in some cases obscure the true definition of the
16166 enum, giving users an incomplete type when the definition is
16167 actually available. Note that we do not want to do this for all
16168 enums which are just declarations, because C++0x allows forward
16169 enum declarations. */
16170 if (cu->per_cu->is_debug_types
16171 && die_is_declaration (die, cu))
16172 {
16173 struct signatured_type *sig_type;
16174
16175 sig_type = (struct signatured_type *) cu->per_cu;
16176 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16177 if (sig_type->type_offset_in_section != die->sect_off)
16178 return;
16179 }
16180
16181 new_symbol (die, this_type, cu);
16182 }
16183
16184 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16185 expression for an index type and finds the corresponding field
16186 offset in the hidden "P_BOUNDS" structure. Returns true on success
16187 and updates *FIELD, false if it fails to recognize an
16188 expression. */
16189
16190 static bool
16191 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16192 int *bounds_offset, struct field *field,
16193 struct dwarf2_cu *cu)
16194 {
16195 struct attribute *attr = dwarf2_attr (die, name, cu);
16196 if (attr == nullptr || !attr->form_is_block ())
16197 return false;
16198
16199 const struct dwarf_block *block = attr->as_block ();
16200 const gdb_byte *start = block->data;
16201 const gdb_byte *end = block->data + block->size;
16202
16203 /* The expression to recognize generally looks like:
16204
16205 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16206 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16207
16208 However, the second "plus_uconst" may be missing:
16209
16210 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16211 DW_OP_deref_size: 4)
16212
16213 This happens when the field is at the start of the structure.
16214
16215 Also, the final deref may not be sized:
16216
16217 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16218 DW_OP_deref)
16219
16220 This happens when the size of the index type happens to be the
16221 same as the architecture's word size. This can occur with or
16222 without the second plus_uconst. */
16223
16224 if (end - start < 2)
16225 return false;
16226 if (*start++ != DW_OP_push_object_address)
16227 return false;
16228 if (*start++ != DW_OP_plus_uconst)
16229 return false;
16230
16231 uint64_t this_bound_off;
16232 start = gdb_read_uleb128 (start, end, &this_bound_off);
16233 if (start == nullptr || (int) this_bound_off != this_bound_off)
16234 return false;
16235 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16236 is consistent among all bounds. */
16237 if (*bounds_offset == -1)
16238 *bounds_offset = this_bound_off;
16239 else if (*bounds_offset != this_bound_off)
16240 return false;
16241
16242 if (start == end || *start++ != DW_OP_deref)
16243 return false;
16244
16245 int offset = 0;
16246 if (start ==end)
16247 return false;
16248 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16249 {
16250 /* This means an offset of 0. */
16251 }
16252 else if (*start++ != DW_OP_plus_uconst)
16253 return false;
16254 else
16255 {
16256 /* The size is the parameter to DW_OP_plus_uconst. */
16257 uint64_t val;
16258 start = gdb_read_uleb128 (start, end, &val);
16259 if (start == nullptr)
16260 return false;
16261 if ((int) val != val)
16262 return false;
16263 offset = val;
16264 }
16265
16266 if (start == end)
16267 return false;
16268
16269 uint64_t size;
16270 if (*start == DW_OP_deref_size)
16271 {
16272 start = gdb_read_uleb128 (start + 1, end, &size);
16273 if (start == nullptr)
16274 return false;
16275 }
16276 else if (*start == DW_OP_deref)
16277 {
16278 size = cu->header.addr_size;
16279 ++start;
16280 }
16281 else
16282 return false;
16283
16284 SET_FIELD_BITPOS (*field, 8 * offset);
16285 if (size != TYPE_LENGTH (field->type ()))
16286 FIELD_BITSIZE (*field) = 8 * size;
16287
16288 return true;
16289 }
16290
16291 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16292 some kinds of Ada arrays:
16293
16294 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16295 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16296 <11e0> DW_AT_data_location: 2 byte block: 97 6
16297 (DW_OP_push_object_address; DW_OP_deref)
16298 <11e3> DW_AT_type : <0x1173>
16299 <11e7> DW_AT_sibling : <0x1201>
16300 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16301 <11ec> DW_AT_type : <0x1206>
16302 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16303 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16304 DW_OP_deref_size: 4)
16305 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16306 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16307 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16308
16309 This actually represents a "thick pointer", which is a structure
16310 with two elements: one that is a pointer to the array data, and one
16311 that is a pointer to another structure; this second structure holds
16312 the array bounds.
16313
16314 This returns a new type on success, or nullptr if this didn't
16315 recognize the type. */
16316
16317 static struct type *
16318 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16319 struct type *type)
16320 {
16321 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16322 /* So far we've only seen this with block form. */
16323 if (attr == nullptr || !attr->form_is_block ())
16324 return nullptr;
16325
16326 /* Note that this will fail if the structure layout is changed by
16327 the compiler. However, we have no good way to recognize some
16328 other layout, because we don't know what expression the compiler
16329 might choose to emit should this happen. */
16330 struct dwarf_block *blk = attr->as_block ();
16331 if (blk->size != 2
16332 || blk->data[0] != DW_OP_push_object_address
16333 || blk->data[1] != DW_OP_deref)
16334 return nullptr;
16335
16336 int bounds_offset = -1;
16337 int max_align = -1;
16338 std::vector<struct field> range_fields;
16339 for (struct die_info *child_die = die->child;
16340 child_die;
16341 child_die = child_die->sibling)
16342 {
16343 if (child_die->tag == DW_TAG_subrange_type)
16344 {
16345 struct type *underlying = read_subrange_index_type (child_die, cu);
16346
16347 int this_align = type_align (underlying);
16348 if (this_align > max_align)
16349 max_align = this_align;
16350
16351 range_fields.emplace_back ();
16352 range_fields.emplace_back ();
16353
16354 struct field &lower = range_fields[range_fields.size () - 2];
16355 struct field &upper = range_fields[range_fields.size () - 1];
16356
16357 lower.set_type (underlying);
16358 FIELD_ARTIFICIAL (lower) = 1;
16359
16360 upper.set_type (underlying);
16361 FIELD_ARTIFICIAL (upper) = 1;
16362
16363 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16364 &bounds_offset, &lower, cu)
16365 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16366 &bounds_offset, &upper, cu))
16367 return nullptr;
16368 }
16369 }
16370
16371 /* This shouldn't really happen, but double-check that we found
16372 where the bounds are stored. */
16373 if (bounds_offset == -1)
16374 return nullptr;
16375
16376 struct objfile *objfile = cu->per_objfile->objfile;
16377 for (int i = 0; i < range_fields.size (); i += 2)
16378 {
16379 char name[20];
16380
16381 /* Set the name of each field in the bounds. */
16382 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16383 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16384 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16385 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16386 }
16387
16388 struct type *bounds = alloc_type (objfile);
16389 bounds->set_code (TYPE_CODE_STRUCT);
16390
16391 bounds->set_num_fields (range_fields.size ());
16392 bounds->set_fields
16393 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16394 * sizeof (struct field))));
16395 memcpy (bounds->fields (), range_fields.data (),
16396 bounds->num_fields () * sizeof (struct field));
16397
16398 int last_fieldno = range_fields.size () - 1;
16399 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16400 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16401 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16402
16403 /* Rewrite the existing array type in place. Specifically, we
16404 remove any dynamic properties we might have read, and we replace
16405 the index types. */
16406 struct type *iter = type;
16407 for (int i = 0; i < range_fields.size (); i += 2)
16408 {
16409 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16410 iter->main_type->dyn_prop_list = nullptr;
16411 iter->set_index_type
16412 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16413 iter = TYPE_TARGET_TYPE (iter);
16414 }
16415
16416 struct type *result = alloc_type (objfile);
16417 result->set_code (TYPE_CODE_STRUCT);
16418
16419 result->set_num_fields (2);
16420 result->set_fields
16421 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16422 * sizeof (struct field))));
16423
16424 /* The names are chosen to coincide with what the compiler does with
16425 -fgnat-encodings=all, which the Ada code in gdb already
16426 understands. */
16427 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16428 result->field (0).set_type (lookup_pointer_type (type));
16429
16430 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16431 result->field (1).set_type (lookup_pointer_type (bounds));
16432 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16433
16434 result->set_name (type->name ());
16435 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16436 + TYPE_LENGTH (result->field (1).type ()));
16437
16438 return result;
16439 }
16440
16441 /* Extract all information from a DW_TAG_array_type DIE and put it in
16442 the DIE's type field. For now, this only handles one dimensional
16443 arrays. */
16444
16445 static struct type *
16446 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16447 {
16448 struct objfile *objfile = cu->per_objfile->objfile;
16449 struct die_info *child_die;
16450 struct type *type;
16451 struct type *element_type, *range_type, *index_type;
16452 struct attribute *attr;
16453 const char *name;
16454 struct dynamic_prop *byte_stride_prop = NULL;
16455 unsigned int bit_stride = 0;
16456
16457 element_type = die_type (die, cu);
16458
16459 /* The die_type call above may have already set the type for this DIE. */
16460 type = get_die_type (die, cu);
16461 if (type)
16462 return type;
16463
16464 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16465 if (attr != NULL)
16466 {
16467 int stride_ok;
16468 struct type *prop_type = cu->addr_sized_int_type (false);
16469
16470 byte_stride_prop
16471 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16472 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16473 prop_type);
16474 if (!stride_ok)
16475 {
16476 complaint (_("unable to read array DW_AT_byte_stride "
16477 " - DIE at %s [in module %s]"),
16478 sect_offset_str (die->sect_off),
16479 objfile_name (cu->per_objfile->objfile));
16480 /* Ignore this attribute. We will likely not be able to print
16481 arrays of this type correctly, but there is little we can do
16482 to help if we cannot read the attribute's value. */
16483 byte_stride_prop = NULL;
16484 }
16485 }
16486
16487 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16488 if (attr != NULL)
16489 bit_stride = attr->constant_value (0);
16490
16491 /* Irix 6.2 native cc creates array types without children for
16492 arrays with unspecified length. */
16493 if (die->child == NULL)
16494 {
16495 index_type = objfile_type (objfile)->builtin_int;
16496 range_type = create_static_range_type (NULL, index_type, 0, -1);
16497 type = create_array_type_with_stride (NULL, element_type, range_type,
16498 byte_stride_prop, bit_stride);
16499 return set_die_type (die, type, cu);
16500 }
16501
16502 std::vector<struct type *> range_types;
16503 child_die = die->child;
16504 while (child_die && child_die->tag)
16505 {
16506 if (child_die->tag == DW_TAG_subrange_type)
16507 {
16508 struct type *child_type = read_type_die (child_die, cu);
16509
16510 if (child_type != NULL)
16511 {
16512 /* The range type was succesfully read. Save it for the
16513 array type creation. */
16514 range_types.push_back (child_type);
16515 }
16516 }
16517 child_die = child_die->sibling;
16518 }
16519
16520 if (range_types.empty ())
16521 {
16522 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16523 sect_offset_str (die->sect_off),
16524 objfile_name (cu->per_objfile->objfile));
16525 return NULL;
16526 }
16527
16528 /* Dwarf2 dimensions are output from left to right, create the
16529 necessary array types in backwards order. */
16530
16531 type = element_type;
16532
16533 if (read_array_order (die, cu) == DW_ORD_col_major)
16534 {
16535 int i = 0;
16536
16537 while (i < range_types.size ())
16538 {
16539 type = create_array_type_with_stride (NULL, type, range_types[i++],
16540 byte_stride_prop, bit_stride);
16541 bit_stride = 0;
16542 byte_stride_prop = nullptr;
16543 }
16544 }
16545 else
16546 {
16547 size_t ndim = range_types.size ();
16548 while (ndim-- > 0)
16549 {
16550 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16551 byte_stride_prop, bit_stride);
16552 bit_stride = 0;
16553 byte_stride_prop = nullptr;
16554 }
16555 }
16556
16557 gdb_assert (type != element_type);
16558
16559 /* Understand Dwarf2 support for vector types (like they occur on
16560 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16561 array type. This is not part of the Dwarf2/3 standard yet, but a
16562 custom vendor extension. The main difference between a regular
16563 array and the vector variant is that vectors are passed by value
16564 to functions. */
16565 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16566 if (attr != nullptr)
16567 make_vector_type (type);
16568
16569 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16570 implementation may choose to implement triple vectors using this
16571 attribute. */
16572 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16573 if (attr != nullptr && attr->form_is_unsigned ())
16574 {
16575 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16576 TYPE_LENGTH (type) = attr->as_unsigned ();
16577 else
16578 complaint (_("DW_AT_byte_size for array type smaller "
16579 "than the total size of elements"));
16580 }
16581
16582 name = dwarf2_name (die, cu);
16583 if (name)
16584 type->set_name (name);
16585
16586 maybe_set_alignment (cu, die, type);
16587
16588 struct type *replacement_type = nullptr;
16589 if (cu->language == language_ada)
16590 {
16591 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16592 if (replacement_type != nullptr)
16593 type = replacement_type;
16594 }
16595
16596 /* Install the type in the die. */
16597 set_die_type (die, type, cu, replacement_type != nullptr);
16598
16599 /* set_die_type should be already done. */
16600 set_descriptive_type (type, die, cu);
16601
16602 return type;
16603 }
16604
16605 static enum dwarf_array_dim_ordering
16606 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16607 {
16608 struct attribute *attr;
16609
16610 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16611
16612 if (attr != nullptr)
16613 {
16614 LONGEST val = attr->constant_value (-1);
16615 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16616 return (enum dwarf_array_dim_ordering) val;
16617 }
16618
16619 /* GNU F77 is a special case, as at 08/2004 array type info is the
16620 opposite order to the dwarf2 specification, but data is still
16621 laid out as per normal fortran.
16622
16623 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16624 version checking. */
16625
16626 if (cu->language == language_fortran
16627 && cu->producer && strstr (cu->producer, "GNU F77"))
16628 {
16629 return DW_ORD_row_major;
16630 }
16631
16632 switch (cu->language_defn->array_ordering ())
16633 {
16634 case array_column_major:
16635 return DW_ORD_col_major;
16636 case array_row_major:
16637 default:
16638 return DW_ORD_row_major;
16639 };
16640 }
16641
16642 /* Extract all information from a DW_TAG_set_type DIE and put it in
16643 the DIE's type field. */
16644
16645 static struct type *
16646 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16647 {
16648 struct type *domain_type, *set_type;
16649 struct attribute *attr;
16650
16651 domain_type = die_type (die, cu);
16652
16653 /* The die_type call above may have already set the type for this DIE. */
16654 set_type = get_die_type (die, cu);
16655 if (set_type)
16656 return set_type;
16657
16658 set_type = create_set_type (NULL, domain_type);
16659
16660 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16661 if (attr != nullptr && attr->form_is_unsigned ())
16662 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16663
16664 maybe_set_alignment (cu, die, set_type);
16665
16666 return set_die_type (die, set_type, cu);
16667 }
16668
16669 /* A helper for read_common_block that creates a locexpr baton.
16670 SYM is the symbol which we are marking as computed.
16671 COMMON_DIE is the DIE for the common block.
16672 COMMON_LOC is the location expression attribute for the common
16673 block itself.
16674 MEMBER_LOC is the location expression attribute for the particular
16675 member of the common block that we are processing.
16676 CU is the CU from which the above come. */
16677
16678 static void
16679 mark_common_block_symbol_computed (struct symbol *sym,
16680 struct die_info *common_die,
16681 struct attribute *common_loc,
16682 struct attribute *member_loc,
16683 struct dwarf2_cu *cu)
16684 {
16685 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16686 struct objfile *objfile = per_objfile->objfile;
16687 struct dwarf2_locexpr_baton *baton;
16688 gdb_byte *ptr;
16689 unsigned int cu_off;
16690 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16691 LONGEST offset = 0;
16692
16693 gdb_assert (common_loc && member_loc);
16694 gdb_assert (common_loc->form_is_block ());
16695 gdb_assert (member_loc->form_is_block ()
16696 || member_loc->form_is_constant ());
16697
16698 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16699 baton->per_objfile = per_objfile;
16700 baton->per_cu = cu->per_cu;
16701 gdb_assert (baton->per_cu);
16702
16703 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16704
16705 if (member_loc->form_is_constant ())
16706 {
16707 offset = member_loc->constant_value (0);
16708 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16709 }
16710 else
16711 baton->size += member_loc->as_block ()->size;
16712
16713 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16714 baton->data = ptr;
16715
16716 *ptr++ = DW_OP_call4;
16717 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16718 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16719 ptr += 4;
16720
16721 if (member_loc->form_is_constant ())
16722 {
16723 *ptr++ = DW_OP_addr;
16724 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16725 ptr += cu->header.addr_size;
16726 }
16727 else
16728 {
16729 /* We have to copy the data here, because DW_OP_call4 will only
16730 use a DW_AT_location attribute. */
16731 struct dwarf_block *block = member_loc->as_block ();
16732 memcpy (ptr, block->data, block->size);
16733 ptr += block->size;
16734 }
16735
16736 *ptr++ = DW_OP_plus;
16737 gdb_assert (ptr - baton->data == baton->size);
16738
16739 SYMBOL_LOCATION_BATON (sym) = baton;
16740 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16741 }
16742
16743 /* Create appropriate locally-scoped variables for all the
16744 DW_TAG_common_block entries. Also create a struct common_block
16745 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16746 is used to separate the common blocks name namespace from regular
16747 variable names. */
16748
16749 static void
16750 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16751 {
16752 struct attribute *attr;
16753
16754 attr = dwarf2_attr (die, DW_AT_location, cu);
16755 if (attr != nullptr)
16756 {
16757 /* Support the .debug_loc offsets. */
16758 if (attr->form_is_block ())
16759 {
16760 /* Ok. */
16761 }
16762 else if (attr->form_is_section_offset ())
16763 {
16764 dwarf2_complex_location_expr_complaint ();
16765 attr = NULL;
16766 }
16767 else
16768 {
16769 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16770 "common block member");
16771 attr = NULL;
16772 }
16773 }
16774
16775 if (die->child != NULL)
16776 {
16777 struct objfile *objfile = cu->per_objfile->objfile;
16778 struct die_info *child_die;
16779 size_t n_entries = 0, size;
16780 struct common_block *common_block;
16781 struct symbol *sym;
16782
16783 for (child_die = die->child;
16784 child_die && child_die->tag;
16785 child_die = child_die->sibling)
16786 ++n_entries;
16787
16788 size = (sizeof (struct common_block)
16789 + (n_entries - 1) * sizeof (struct symbol *));
16790 common_block
16791 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16792 size);
16793 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16794 common_block->n_entries = 0;
16795
16796 for (child_die = die->child;
16797 child_die && child_die->tag;
16798 child_die = child_die->sibling)
16799 {
16800 /* Create the symbol in the DW_TAG_common_block block in the current
16801 symbol scope. */
16802 sym = new_symbol (child_die, NULL, cu);
16803 if (sym != NULL)
16804 {
16805 struct attribute *member_loc;
16806
16807 common_block->contents[common_block->n_entries++] = sym;
16808
16809 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16810 cu);
16811 if (member_loc)
16812 {
16813 /* GDB has handled this for a long time, but it is
16814 not specified by DWARF. It seems to have been
16815 emitted by gfortran at least as recently as:
16816 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16817 complaint (_("Variable in common block has "
16818 "DW_AT_data_member_location "
16819 "- DIE at %s [in module %s]"),
16820 sect_offset_str (child_die->sect_off),
16821 objfile_name (objfile));
16822
16823 if (member_loc->form_is_section_offset ())
16824 dwarf2_complex_location_expr_complaint ();
16825 else if (member_loc->form_is_constant ()
16826 || member_loc->form_is_block ())
16827 {
16828 if (attr != nullptr)
16829 mark_common_block_symbol_computed (sym, die, attr,
16830 member_loc, cu);
16831 }
16832 else
16833 dwarf2_complex_location_expr_complaint ();
16834 }
16835 }
16836 }
16837
16838 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16839 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16840 }
16841 }
16842
16843 /* Create a type for a C++ namespace. */
16844
16845 static struct type *
16846 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16847 {
16848 struct objfile *objfile = cu->per_objfile->objfile;
16849 const char *previous_prefix, *name;
16850 int is_anonymous;
16851 struct type *type;
16852
16853 /* For extensions, reuse the type of the original namespace. */
16854 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16855 {
16856 struct die_info *ext_die;
16857 struct dwarf2_cu *ext_cu = cu;
16858
16859 ext_die = dwarf2_extension (die, &ext_cu);
16860 type = read_type_die (ext_die, ext_cu);
16861
16862 /* EXT_CU may not be the same as CU.
16863 Ensure TYPE is recorded with CU in die_type_hash. */
16864 return set_die_type (die, type, cu);
16865 }
16866
16867 name = namespace_name (die, &is_anonymous, cu);
16868
16869 /* Now build the name of the current namespace. */
16870
16871 previous_prefix = determine_prefix (die, cu);
16872 if (previous_prefix[0] != '\0')
16873 name = typename_concat (&objfile->objfile_obstack,
16874 previous_prefix, name, 0, cu);
16875
16876 /* Create the type. */
16877 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16878
16879 return set_die_type (die, type, cu);
16880 }
16881
16882 /* Read a namespace scope. */
16883
16884 static void
16885 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16886 {
16887 struct objfile *objfile = cu->per_objfile->objfile;
16888 int is_anonymous;
16889
16890 /* Add a symbol associated to this if we haven't seen the namespace
16891 before. Also, add a using directive if it's an anonymous
16892 namespace. */
16893
16894 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16895 {
16896 struct type *type;
16897
16898 type = read_type_die (die, cu);
16899 new_symbol (die, type, cu);
16900
16901 namespace_name (die, &is_anonymous, cu);
16902 if (is_anonymous)
16903 {
16904 const char *previous_prefix = determine_prefix (die, cu);
16905
16906 std::vector<const char *> excludes;
16907 add_using_directive (using_directives (cu),
16908 previous_prefix, type->name (), NULL,
16909 NULL, excludes, 0, &objfile->objfile_obstack);
16910 }
16911 }
16912
16913 if (die->child != NULL)
16914 {
16915 struct die_info *child_die = die->child;
16916
16917 while (child_die && child_die->tag)
16918 {
16919 process_die (child_die, cu);
16920 child_die = child_die->sibling;
16921 }
16922 }
16923 }
16924
16925 /* Read a Fortran module as type. This DIE can be only a declaration used for
16926 imported module. Still we need that type as local Fortran "use ... only"
16927 declaration imports depend on the created type in determine_prefix. */
16928
16929 static struct type *
16930 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16931 {
16932 struct objfile *objfile = cu->per_objfile->objfile;
16933 const char *module_name;
16934 struct type *type;
16935
16936 module_name = dwarf2_name (die, cu);
16937 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16938
16939 return set_die_type (die, type, cu);
16940 }
16941
16942 /* Read a Fortran module. */
16943
16944 static void
16945 read_module (struct die_info *die, struct dwarf2_cu *cu)
16946 {
16947 struct die_info *child_die = die->child;
16948 struct type *type;
16949
16950 type = read_type_die (die, cu);
16951 new_symbol (die, type, cu);
16952
16953 while (child_die && child_die->tag)
16954 {
16955 process_die (child_die, cu);
16956 child_die = child_die->sibling;
16957 }
16958 }
16959
16960 /* Return the name of the namespace represented by DIE. Set
16961 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16962 namespace. */
16963
16964 static const char *
16965 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16966 {
16967 struct die_info *current_die;
16968 const char *name = NULL;
16969
16970 /* Loop through the extensions until we find a name. */
16971
16972 for (current_die = die;
16973 current_die != NULL;
16974 current_die = dwarf2_extension (die, &cu))
16975 {
16976 /* We don't use dwarf2_name here so that we can detect the absence
16977 of a name -> anonymous namespace. */
16978 name = dwarf2_string_attr (die, DW_AT_name, cu);
16979
16980 if (name != NULL)
16981 break;
16982 }
16983
16984 /* Is it an anonymous namespace? */
16985
16986 *is_anonymous = (name == NULL);
16987 if (*is_anonymous)
16988 name = CP_ANONYMOUS_NAMESPACE_STR;
16989
16990 return name;
16991 }
16992
16993 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16994 the user defined type vector. */
16995
16996 static struct type *
16997 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16998 {
16999 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17000 struct comp_unit_head *cu_header = &cu->header;
17001 struct type *type;
17002 struct attribute *attr_byte_size;
17003 struct attribute *attr_address_class;
17004 int byte_size, addr_class;
17005 struct type *target_type;
17006
17007 target_type = die_type (die, cu);
17008
17009 /* The die_type call above may have already set the type for this DIE. */
17010 type = get_die_type (die, cu);
17011 if (type)
17012 return type;
17013
17014 type = lookup_pointer_type (target_type);
17015
17016 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17017 if (attr_byte_size)
17018 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17019 else
17020 byte_size = cu_header->addr_size;
17021
17022 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17023 if (attr_address_class)
17024 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17025 else
17026 addr_class = DW_ADDR_none;
17027
17028 ULONGEST alignment = get_alignment (cu, die);
17029
17030 /* If the pointer size, alignment, or address class is different
17031 than the default, create a type variant marked as such and set
17032 the length accordingly. */
17033 if (TYPE_LENGTH (type) != byte_size
17034 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17035 && alignment != TYPE_RAW_ALIGN (type))
17036 || addr_class != DW_ADDR_none)
17037 {
17038 if (gdbarch_address_class_type_flags_p (gdbarch))
17039 {
17040 type_instance_flags type_flags
17041 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17042 addr_class);
17043 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17044 == 0);
17045 type = make_type_with_address_space (type, type_flags);
17046 }
17047 else if (TYPE_LENGTH (type) != byte_size)
17048 {
17049 complaint (_("invalid pointer size %d"), byte_size);
17050 }
17051 else if (TYPE_RAW_ALIGN (type) != alignment)
17052 {
17053 complaint (_("Invalid DW_AT_alignment"
17054 " - DIE at %s [in module %s]"),
17055 sect_offset_str (die->sect_off),
17056 objfile_name (cu->per_objfile->objfile));
17057 }
17058 else
17059 {
17060 /* Should we also complain about unhandled address classes? */
17061 }
17062 }
17063
17064 TYPE_LENGTH (type) = byte_size;
17065 set_type_align (type, alignment);
17066 return set_die_type (die, type, cu);
17067 }
17068
17069 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17070 the user defined type vector. */
17071
17072 static struct type *
17073 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17074 {
17075 struct type *type;
17076 struct type *to_type;
17077 struct type *domain;
17078
17079 to_type = die_type (die, cu);
17080 domain = die_containing_type (die, cu);
17081
17082 /* The calls above may have already set the type for this DIE. */
17083 type = get_die_type (die, cu);
17084 if (type)
17085 return type;
17086
17087 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17088 type = lookup_methodptr_type (to_type);
17089 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17090 {
17091 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17092
17093 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17094 to_type->fields (), to_type->num_fields (),
17095 to_type->has_varargs ());
17096 type = lookup_methodptr_type (new_type);
17097 }
17098 else
17099 type = lookup_memberptr_type (to_type, domain);
17100
17101 return set_die_type (die, type, cu);
17102 }
17103
17104 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17105 the user defined type vector. */
17106
17107 static struct type *
17108 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17109 enum type_code refcode)
17110 {
17111 struct comp_unit_head *cu_header = &cu->header;
17112 struct type *type, *target_type;
17113 struct attribute *attr;
17114
17115 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17116
17117 target_type = die_type (die, cu);
17118
17119 /* The die_type call above may have already set the type for this DIE. */
17120 type = get_die_type (die, cu);
17121 if (type)
17122 return type;
17123
17124 type = lookup_reference_type (target_type, refcode);
17125 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17126 if (attr != nullptr)
17127 {
17128 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17129 }
17130 else
17131 {
17132 TYPE_LENGTH (type) = cu_header->addr_size;
17133 }
17134 maybe_set_alignment (cu, die, type);
17135 return set_die_type (die, type, cu);
17136 }
17137
17138 /* Add the given cv-qualifiers to the element type of the array. GCC
17139 outputs DWARF type qualifiers that apply to an array, not the
17140 element type. But GDB relies on the array element type to carry
17141 the cv-qualifiers. This mimics section 6.7.3 of the C99
17142 specification. */
17143
17144 static struct type *
17145 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17146 struct type *base_type, int cnst, int voltl)
17147 {
17148 struct type *el_type, *inner_array;
17149
17150 base_type = copy_type (base_type);
17151 inner_array = base_type;
17152
17153 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17154 {
17155 TYPE_TARGET_TYPE (inner_array) =
17156 copy_type (TYPE_TARGET_TYPE (inner_array));
17157 inner_array = TYPE_TARGET_TYPE (inner_array);
17158 }
17159
17160 el_type = TYPE_TARGET_TYPE (inner_array);
17161 cnst |= TYPE_CONST (el_type);
17162 voltl |= TYPE_VOLATILE (el_type);
17163 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17164
17165 return set_die_type (die, base_type, cu);
17166 }
17167
17168 static struct type *
17169 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17170 {
17171 struct type *base_type, *cv_type;
17172
17173 base_type = die_type (die, cu);
17174
17175 /* The die_type call above may have already set the type for this DIE. */
17176 cv_type = get_die_type (die, cu);
17177 if (cv_type)
17178 return cv_type;
17179
17180 /* In case the const qualifier is applied to an array type, the element type
17181 is so qualified, not the array type (section 6.7.3 of C99). */
17182 if (base_type->code () == TYPE_CODE_ARRAY)
17183 return add_array_cv_type (die, cu, base_type, 1, 0);
17184
17185 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17186 return set_die_type (die, cv_type, cu);
17187 }
17188
17189 static struct type *
17190 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17191 {
17192 struct type *base_type, *cv_type;
17193
17194 base_type = die_type (die, cu);
17195
17196 /* The die_type call above may have already set the type for this DIE. */
17197 cv_type = get_die_type (die, cu);
17198 if (cv_type)
17199 return cv_type;
17200
17201 /* In case the volatile qualifier is applied to an array type, the
17202 element type is so qualified, not the array type (section 6.7.3
17203 of C99). */
17204 if (base_type->code () == TYPE_CODE_ARRAY)
17205 return add_array_cv_type (die, cu, base_type, 0, 1);
17206
17207 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17208 return set_die_type (die, cv_type, cu);
17209 }
17210
17211 /* Handle DW_TAG_restrict_type. */
17212
17213 static struct type *
17214 read_tag_restrict_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_restrict_type (base_type);
17226 return set_die_type (die, cv_type, cu);
17227 }
17228
17229 /* Handle DW_TAG_atomic_type. */
17230
17231 static struct type *
17232 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17233 {
17234 struct type *base_type, *cv_type;
17235
17236 base_type = die_type (die, cu);
17237
17238 /* The die_type call above may have already set the type for this DIE. */
17239 cv_type = get_die_type (die, cu);
17240 if (cv_type)
17241 return cv_type;
17242
17243 cv_type = make_atomic_type (base_type);
17244 return set_die_type (die, cv_type, cu);
17245 }
17246
17247 /* Extract all information from a DW_TAG_string_type DIE and add to
17248 the user defined type vector. It isn't really a user defined type,
17249 but it behaves like one, with other DIE's using an AT_user_def_type
17250 attribute to reference it. */
17251
17252 static struct type *
17253 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17254 {
17255 struct objfile *objfile = cu->per_objfile->objfile;
17256 struct gdbarch *gdbarch = objfile->arch ();
17257 struct type *type, *range_type, *index_type, *char_type;
17258 struct attribute *attr;
17259 struct dynamic_prop prop;
17260 bool length_is_constant = true;
17261 LONGEST length;
17262
17263 /* There are a couple of places where bit sizes might be made use of
17264 when parsing a DW_TAG_string_type, however, no producer that we know
17265 of make use of these. Handling bit sizes that are a multiple of the
17266 byte size is easy enough, but what about other bit sizes? Lets deal
17267 with that problem when we have to. Warn about these attributes being
17268 unsupported, then parse the type and ignore them like we always
17269 have. */
17270 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17271 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17272 {
17273 static bool warning_printed = false;
17274 if (!warning_printed)
17275 {
17276 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17277 "currently supported on DW_TAG_string_type."));
17278 warning_printed = true;
17279 }
17280 }
17281
17282 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17283 if (attr != nullptr && !attr->form_is_constant ())
17284 {
17285 /* The string length describes the location at which the length of
17286 the string can be found. The size of the length field can be
17287 specified with one of the attributes below. */
17288 struct type *prop_type;
17289 struct attribute *len
17290 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17291 if (len == nullptr)
17292 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17293 if (len != nullptr && len->form_is_constant ())
17294 {
17295 /* Pass 0 as the default as we know this attribute is constant
17296 and the default value will not be returned. */
17297 LONGEST sz = len->constant_value (0);
17298 prop_type = cu->per_objfile->int_type (sz, true);
17299 }
17300 else
17301 {
17302 /* If the size is not specified then we assume it is the size of
17303 an address on this target. */
17304 prop_type = cu->addr_sized_int_type (true);
17305 }
17306
17307 /* Convert the attribute into a dynamic property. */
17308 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17309 length = 1;
17310 else
17311 length_is_constant = false;
17312 }
17313 else if (attr != nullptr)
17314 {
17315 /* This DW_AT_string_length just contains the length with no
17316 indirection. There's no need to create a dynamic property in this
17317 case. Pass 0 for the default value as we know it will not be
17318 returned in this case. */
17319 length = attr->constant_value (0);
17320 }
17321 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17322 {
17323 /* We don't currently support non-constant byte sizes for strings. */
17324 length = attr->constant_value (1);
17325 }
17326 else
17327 {
17328 /* Use 1 as a fallback length if we have nothing else. */
17329 length = 1;
17330 }
17331
17332 index_type = objfile_type (objfile)->builtin_int;
17333 if (length_is_constant)
17334 range_type = create_static_range_type (NULL, index_type, 1, length);
17335 else
17336 {
17337 struct dynamic_prop low_bound;
17338
17339 low_bound.set_const_val (1);
17340 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17341 }
17342 char_type = language_string_char_type (cu->language_defn, gdbarch);
17343 type = create_string_type (NULL, char_type, range_type);
17344
17345 return set_die_type (die, type, cu);
17346 }
17347
17348 /* Assuming that DIE corresponds to a function, returns nonzero
17349 if the function is prototyped. */
17350
17351 static int
17352 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17353 {
17354 struct attribute *attr;
17355
17356 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17357 if (attr && attr->as_boolean ())
17358 return 1;
17359
17360 /* The DWARF standard implies that the DW_AT_prototyped attribute
17361 is only meaningful for C, but the concept also extends to other
17362 languages that allow unprototyped functions (Eg: Objective C).
17363 For all other languages, assume that functions are always
17364 prototyped. */
17365 if (cu->language != language_c
17366 && cu->language != language_objc
17367 && cu->language != language_opencl)
17368 return 1;
17369
17370 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17371 prototyped and unprototyped functions; default to prototyped,
17372 since that is more common in modern code (and RealView warns
17373 about unprototyped functions). */
17374 if (producer_is_realview (cu->producer))
17375 return 1;
17376
17377 return 0;
17378 }
17379
17380 /* Handle DIES due to C code like:
17381
17382 struct foo
17383 {
17384 int (*funcp)(int a, long l);
17385 int b;
17386 };
17387
17388 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17389
17390 static struct type *
17391 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17392 {
17393 struct objfile *objfile = cu->per_objfile->objfile;
17394 struct type *type; /* Type that this function returns. */
17395 struct type *ftype; /* Function that returns above type. */
17396 struct attribute *attr;
17397
17398 type = die_type (die, cu);
17399
17400 /* The die_type call above may have already set the type for this DIE. */
17401 ftype = get_die_type (die, cu);
17402 if (ftype)
17403 return ftype;
17404
17405 ftype = lookup_function_type (type);
17406
17407 if (prototyped_function_p (die, cu))
17408 ftype->set_is_prototyped (true);
17409
17410 /* Store the calling convention in the type if it's available in
17411 the subroutine die. Otherwise set the calling convention to
17412 the default value DW_CC_normal. */
17413 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17414 if (attr != nullptr
17415 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17416 TYPE_CALLING_CONVENTION (ftype)
17417 = (enum dwarf_calling_convention) attr->constant_value (0);
17418 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17419 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17420 else
17421 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17422
17423 /* Record whether the function returns normally to its caller or not
17424 if the DWARF producer set that information. */
17425 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17426 if (attr && attr->as_boolean ())
17427 TYPE_NO_RETURN (ftype) = 1;
17428
17429 /* We need to add the subroutine type to the die immediately so
17430 we don't infinitely recurse when dealing with parameters
17431 declared as the same subroutine type. */
17432 set_die_type (die, ftype, cu);
17433
17434 if (die->child != NULL)
17435 {
17436 struct type *void_type = objfile_type (objfile)->builtin_void;
17437 struct die_info *child_die;
17438 int nparams, iparams;
17439
17440 /* Count the number of parameters.
17441 FIXME: GDB currently ignores vararg functions, but knows about
17442 vararg member functions. */
17443 nparams = 0;
17444 child_die = die->child;
17445 while (child_die && child_die->tag)
17446 {
17447 if (child_die->tag == DW_TAG_formal_parameter)
17448 nparams++;
17449 else if (child_die->tag == DW_TAG_unspecified_parameters)
17450 ftype->set_has_varargs (true);
17451
17452 child_die = child_die->sibling;
17453 }
17454
17455 /* Allocate storage for parameters and fill them in. */
17456 ftype->set_num_fields (nparams);
17457 ftype->set_fields
17458 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17459
17460 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17461 even if we error out during the parameters reading below. */
17462 for (iparams = 0; iparams < nparams; iparams++)
17463 ftype->field (iparams).set_type (void_type);
17464
17465 iparams = 0;
17466 child_die = die->child;
17467 while (child_die && child_die->tag)
17468 {
17469 if (child_die->tag == DW_TAG_formal_parameter)
17470 {
17471 struct type *arg_type;
17472
17473 /* DWARF version 2 has no clean way to discern C++
17474 static and non-static member functions. G++ helps
17475 GDB by marking the first parameter for non-static
17476 member functions (which is the this pointer) as
17477 artificial. We pass this information to
17478 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17479
17480 DWARF version 3 added DW_AT_object_pointer, which GCC
17481 4.5 does not yet generate. */
17482 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17483 if (attr != nullptr)
17484 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17485 else
17486 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17487 arg_type = die_type (child_die, cu);
17488
17489 /* RealView does not mark THIS as const, which the testsuite
17490 expects. GCC marks THIS as const in method definitions,
17491 but not in the class specifications (GCC PR 43053). */
17492 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17493 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17494 {
17495 int is_this = 0;
17496 struct dwarf2_cu *arg_cu = cu;
17497 const char *name = dwarf2_name (child_die, cu);
17498
17499 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17500 if (attr != nullptr)
17501 {
17502 /* If the compiler emits this, use it. */
17503 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17504 is_this = 1;
17505 }
17506 else if (name && strcmp (name, "this") == 0)
17507 /* Function definitions will have the argument names. */
17508 is_this = 1;
17509 else if (name == NULL && iparams == 0)
17510 /* Declarations may not have the names, so like
17511 elsewhere in GDB, assume an artificial first
17512 argument is "this". */
17513 is_this = 1;
17514
17515 if (is_this)
17516 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17517 arg_type, 0);
17518 }
17519
17520 ftype->field (iparams).set_type (arg_type);
17521 iparams++;
17522 }
17523 child_die = child_die->sibling;
17524 }
17525 }
17526
17527 return ftype;
17528 }
17529
17530 static struct type *
17531 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17532 {
17533 struct objfile *objfile = cu->per_objfile->objfile;
17534 const char *name = NULL;
17535 struct type *this_type, *target_type;
17536
17537 name = dwarf2_full_name (NULL, die, cu);
17538 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17539 this_type->set_target_is_stub (true);
17540 set_die_type (die, this_type, cu);
17541 target_type = die_type (die, cu);
17542 if (target_type != this_type)
17543 TYPE_TARGET_TYPE (this_type) = target_type;
17544 else
17545 {
17546 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17547 spec and cause infinite loops in GDB. */
17548 complaint (_("Self-referential DW_TAG_typedef "
17549 "- DIE at %s [in module %s]"),
17550 sect_offset_str (die->sect_off), objfile_name (objfile));
17551 TYPE_TARGET_TYPE (this_type) = NULL;
17552 }
17553 if (name == NULL)
17554 {
17555 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17556 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17557 Handle these by just returning the target type, rather than
17558 constructing an anonymous typedef type and trying to handle this
17559 elsewhere. */
17560 set_die_type (die, target_type, cu);
17561 return target_type;
17562 }
17563 return this_type;
17564 }
17565
17566 /* Helper for get_dwarf2_rational_constant that computes the value of
17567 a given gmp_mpz given an attribute. */
17568
17569 static void
17570 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17571 {
17572 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17573 location expression that pushes an implicit value. */
17574 if (attr->form == DW_FORM_exprloc)
17575 {
17576 dwarf_block *blk = attr->as_block ();
17577 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17578 {
17579 uint64_t len;
17580 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17581 blk->data + blk->size,
17582 &len);
17583 if (ptr - blk->data + len <= blk->size)
17584 {
17585 mpz_import (value->val, len,
17586 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17587 1, 0, 0, ptr);
17588 return;
17589 }
17590 }
17591
17592 /* On failure set it to 1. */
17593 *value = gdb_mpz (1);
17594 }
17595 else if (attr->form_is_block ())
17596 {
17597 dwarf_block *blk = attr->as_block ();
17598 mpz_import (value->val, blk->size,
17599 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17600 1, 0, 0, blk->data);
17601 }
17602 else
17603 *value = gdb_mpz (attr->constant_value (1));
17604 }
17605
17606 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17607 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17608
17609 If the numerator and/or numerator attribute is missing,
17610 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17611 untouched. */
17612
17613 static void
17614 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17615 gdb_mpz *numerator, gdb_mpz *denominator)
17616 {
17617 struct attribute *num_attr, *denom_attr;
17618
17619 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17620 if (num_attr == nullptr)
17621 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17622 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17623
17624 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17625 if (denom_attr == nullptr)
17626 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17627 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17628
17629 if (num_attr == nullptr || denom_attr == nullptr)
17630 return;
17631
17632 get_mpz (cu, numerator, num_attr);
17633 get_mpz (cu, denominator, denom_attr);
17634 }
17635
17636 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17637 rational constant, rather than a signed one.
17638
17639 If the rational constant has a negative value, a complaint
17640 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17641
17642 static void
17643 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17644 struct dwarf2_cu *cu,
17645 gdb_mpz *numerator,
17646 gdb_mpz *denominator)
17647 {
17648 gdb_mpz num (1);
17649 gdb_mpz denom (1);
17650
17651 get_dwarf2_rational_constant (die, cu, &num, &denom);
17652 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17653 {
17654 mpz_neg (num.val, num.val);
17655 mpz_neg (denom.val, denom.val);
17656 }
17657 else if (mpz_sgn (num.val) == -1)
17658 {
17659 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17660 " in DIE at %s"),
17661 sect_offset_str (die->sect_off));
17662 return;
17663 }
17664 else if (mpz_sgn (denom.val) == -1)
17665 {
17666 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17667 " in DIE at %s"),
17668 sect_offset_str (die->sect_off));
17669 return;
17670 }
17671
17672 *numerator = std::move (num);
17673 *denominator = std::move (denom);
17674 }
17675
17676 /* Assuming that ENCODING is a string whose contents starting at the
17677 K'th character is "_nn" where "nn" is a decimal number, scan that
17678 number and set RESULT to the value. K is updated to point to the
17679 character immediately following the number.
17680
17681 If the string does not conform to the format described above, false
17682 is returned, and K may or may not be changed. */
17683
17684 static bool
17685 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17686 {
17687 /* The next character should be an underscore ('_') followed
17688 by a digit. */
17689 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17690 return false;
17691
17692 /* Skip the underscore. */
17693 k++;
17694 int start = k;
17695
17696 /* Determine the number of digits for our number. */
17697 while (isdigit (encoding[k]))
17698 k++;
17699 if (k == start)
17700 return false;
17701
17702 std::string copy (&encoding[start], k - start);
17703 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17704 return false;
17705
17706 return true;
17707 }
17708
17709 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17710 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17711 DENOM, update OFFSET, and return true on success. Return false on
17712 failure. */
17713
17714 static bool
17715 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17716 gdb_mpz *num, gdb_mpz *denom)
17717 {
17718 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17719 return false;
17720 return ada_get_gnat_encoded_number (encoding, offset, denom);
17721 }
17722
17723 /* Assuming DIE corresponds to a fixed point type, finish the creation
17724 of the corresponding TYPE by setting its type-specific data. CU is
17725 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17726 encodings. It is nullptr if the GNAT encoding should be
17727 ignored. */
17728
17729 static void
17730 finish_fixed_point_type (struct type *type, const char *suffix,
17731 struct die_info *die, struct dwarf2_cu *cu)
17732 {
17733 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17734 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17735
17736 /* If GNAT encodings are preferred, don't examine the
17737 attributes. */
17738 struct attribute *attr = nullptr;
17739 if (suffix == nullptr)
17740 {
17741 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17742 if (attr == nullptr)
17743 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17744 if (attr == nullptr)
17745 attr = dwarf2_attr (die, DW_AT_small, cu);
17746 }
17747
17748 /* Numerator and denominator of our fixed-point type's scaling factor.
17749 The default is a scaling factor of 1, which we use as a fallback
17750 when we are not able to decode it (problem with the debugging info,
17751 unsupported forms, bug in GDB, etc...). Using that as the default
17752 allows us to at least print the unscaled value, which might still
17753 be useful to a user. */
17754 gdb_mpz scale_num (1);
17755 gdb_mpz scale_denom (1);
17756
17757 if (attr == nullptr)
17758 {
17759 int offset = 0;
17760 if (suffix != nullptr
17761 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17762 &scale_denom)
17763 /* The number might be encoded as _nn_dd_nn_dd, where the
17764 second ratio is the 'small value. In this situation, we
17765 want the second value. */
17766 && (suffix[offset] != '_'
17767 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17768 &scale_denom)))
17769 {
17770 /* Found it. */
17771 }
17772 else
17773 {
17774 /* Scaling factor not found. Assume a scaling factor of 1,
17775 and hope for the best. At least the user will be able to
17776 see the encoded value. */
17777 scale_num = 1;
17778 scale_denom = 1;
17779 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17780 sect_offset_str (die->sect_off));
17781 }
17782 }
17783 else if (attr->name == DW_AT_binary_scale)
17784 {
17785 LONGEST scale_exp = attr->constant_value (0);
17786 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17787
17788 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17789 }
17790 else if (attr->name == DW_AT_decimal_scale)
17791 {
17792 LONGEST scale_exp = attr->constant_value (0);
17793 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17794
17795 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17796 }
17797 else if (attr->name == DW_AT_small)
17798 {
17799 struct die_info *scale_die;
17800 struct dwarf2_cu *scale_cu = cu;
17801
17802 scale_die = follow_die_ref (die, attr, &scale_cu);
17803 if (scale_die->tag == DW_TAG_constant)
17804 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17805 &scale_num, &scale_denom);
17806 else
17807 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17808 " (DIE at %s)"),
17809 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17810 }
17811 else
17812 {
17813 complaint (_("unsupported scale attribute %s for fixed-point type"
17814 " (DIE at %s)"),
17815 dwarf_attr_name (attr->name),
17816 sect_offset_str (die->sect_off));
17817 }
17818
17819 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17820 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17821 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17822 mpq_canonicalize (scaling_factor.val);
17823 }
17824
17825 /* The gnat-encoding suffix for fixed point. */
17826
17827 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17828
17829 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17830 "XF" suffix of the name. The text after this is what encodes the
17831 'small and 'delta information. Otherwise, return nullptr. */
17832
17833 static const char *
17834 gnat_encoded_fixed_point_type_info (const char *name)
17835 {
17836 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17837 }
17838
17839 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17840 (which may be different from NAME) to the architecture back-end to allow
17841 it to guess the correct format if necessary. */
17842
17843 static struct type *
17844 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17845 const char *name_hint, enum bfd_endian byte_order)
17846 {
17847 struct gdbarch *gdbarch = objfile->arch ();
17848 const struct floatformat **format;
17849 struct type *type;
17850
17851 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17852 if (format)
17853 type = init_float_type (objfile, bits, name, format, byte_order);
17854 else
17855 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17856
17857 return type;
17858 }
17859
17860 /* Allocate an integer type of size BITS and name NAME. */
17861
17862 static struct type *
17863 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17864 int bits, int unsigned_p, const char *name)
17865 {
17866 struct type *type;
17867
17868 /* Versions of Intel's C Compiler generate an integer type called "void"
17869 instead of using DW_TAG_unspecified_type. This has been seen on
17870 at least versions 14, 17, and 18. */
17871 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17872 && strcmp (name, "void") == 0)
17873 type = objfile_type (objfile)->builtin_void;
17874 else
17875 type = init_integer_type (objfile, bits, unsigned_p, name);
17876
17877 return type;
17878 }
17879
17880 /* Return true if DIE has a DW_AT_small attribute whose value is
17881 a constant rational, where both the numerator and denominator
17882 are equal to zero.
17883
17884 CU is the DIE's Compilation Unit. */
17885
17886 static bool
17887 has_zero_over_zero_small_attribute (struct die_info *die,
17888 struct dwarf2_cu *cu)
17889 {
17890 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17891 if (attr == nullptr)
17892 return false;
17893
17894 struct dwarf2_cu *scale_cu = cu;
17895 struct die_info *scale_die
17896 = follow_die_ref (die, attr, &scale_cu);
17897
17898 if (scale_die->tag != DW_TAG_constant)
17899 return false;
17900
17901 gdb_mpz num (1), denom (1);
17902 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
17903 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
17904 }
17905
17906 /* Initialise and return a floating point type of size BITS suitable for
17907 use as a component of a complex number. The NAME_HINT is passed through
17908 when initialising the floating point type and is the name of the complex
17909 type.
17910
17911 As DWARF doesn't currently provide an explicit name for the components
17912 of a complex number, but it can be helpful to have these components
17913 named, we try to select a suitable name based on the size of the
17914 component. */
17915 static struct type *
17916 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17917 struct objfile *objfile,
17918 int bits, const char *name_hint,
17919 enum bfd_endian byte_order)
17920 {
17921 gdbarch *gdbarch = objfile->arch ();
17922 struct type *tt = nullptr;
17923
17924 /* Try to find a suitable floating point builtin type of size BITS.
17925 We're going to use the name of this type as the name for the complex
17926 target type that we are about to create. */
17927 switch (cu->language)
17928 {
17929 case language_fortran:
17930 switch (bits)
17931 {
17932 case 32:
17933 tt = builtin_f_type (gdbarch)->builtin_real;
17934 break;
17935 case 64:
17936 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17937 break;
17938 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17939 case 128:
17940 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17941 break;
17942 }
17943 break;
17944 default:
17945 switch (bits)
17946 {
17947 case 32:
17948 tt = builtin_type (gdbarch)->builtin_float;
17949 break;
17950 case 64:
17951 tt = builtin_type (gdbarch)->builtin_double;
17952 break;
17953 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17954 case 128:
17955 tt = builtin_type (gdbarch)->builtin_long_double;
17956 break;
17957 }
17958 break;
17959 }
17960
17961 /* If the type we found doesn't match the size we were looking for, then
17962 pretend we didn't find a type at all, the complex target type we
17963 create will then be nameless. */
17964 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17965 tt = nullptr;
17966
17967 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17968 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17969 }
17970
17971 /* Find a representation of a given base type and install
17972 it in the TYPE field of the die. */
17973
17974 static struct type *
17975 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17976 {
17977 struct objfile *objfile = cu->per_objfile->objfile;
17978 struct type *type;
17979 struct attribute *attr;
17980 int encoding = 0, bits = 0;
17981 const char *name;
17982 gdbarch *arch;
17983
17984 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17985 if (attr != nullptr && attr->form_is_constant ())
17986 encoding = attr->constant_value (0);
17987 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17988 if (attr != nullptr)
17989 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17990 name = dwarf2_name (die, cu);
17991 if (!name)
17992 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17993
17994 arch = objfile->arch ();
17995 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17996
17997 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17998 if (attr != nullptr && attr->form_is_constant ())
17999 {
18000 int endianity = attr->constant_value (0);
18001
18002 switch (endianity)
18003 {
18004 case DW_END_big:
18005 byte_order = BFD_ENDIAN_BIG;
18006 break;
18007 case DW_END_little:
18008 byte_order = BFD_ENDIAN_LITTLE;
18009 break;
18010 default:
18011 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18012 break;
18013 }
18014 }
18015
18016 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18017 && cu->language == language_ada
18018 && has_zero_over_zero_small_attribute (die, cu))
18019 {
18020 /* brobecker/2018-02-24: This is a fixed point type for which
18021 the scaling factor is represented as fraction whose value
18022 does not make sense (zero divided by zero), so we should
18023 normally never see these. However, there is a small category
18024 of fixed point types for which GNAT is unable to provide
18025 the scaling factor via the standard DWARF mechanisms, and
18026 for which the info is provided via the GNAT encodings instead.
18027 This is likely what this DIE is about. */
18028 encoding = (encoding == DW_ATE_signed_fixed
18029 ? DW_ATE_signed
18030 : DW_ATE_unsigned);
18031 }
18032
18033 /* With GNAT encodings, fixed-point information will be encoded in
18034 the type name. Note that this can also occur with the above
18035 zero-over-zero case, which is why this is a separate "if" rather
18036 than an "else if". */
18037 const char *gnat_encoding_suffix = nullptr;
18038 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18039 && cu->language == language_ada
18040 && name != nullptr)
18041 {
18042 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18043 if (gnat_encoding_suffix != nullptr)
18044 {
18045 gdb_assert (startswith (gnat_encoding_suffix,
18046 GNAT_FIXED_POINT_SUFFIX));
18047 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18048 name, gnat_encoding_suffix - name);
18049 /* Use -1 here so that SUFFIX points at the "_" after the
18050 "XF". */
18051 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18052
18053 encoding = (encoding == DW_ATE_signed
18054 ? DW_ATE_signed_fixed
18055 : DW_ATE_unsigned_fixed);
18056 }
18057 }
18058
18059 switch (encoding)
18060 {
18061 case DW_ATE_address:
18062 /* Turn DW_ATE_address into a void * pointer. */
18063 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18064 type = init_pointer_type (objfile, bits, name, type);
18065 break;
18066 case DW_ATE_boolean:
18067 type = init_boolean_type (objfile, bits, 1, name);
18068 break;
18069 case DW_ATE_complex_float:
18070 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18071 byte_order);
18072 if (type->code () == TYPE_CODE_ERROR)
18073 {
18074 if (name == nullptr)
18075 {
18076 struct obstack *obstack
18077 = &cu->per_objfile->objfile->objfile_obstack;
18078 name = obconcat (obstack, "_Complex ", type->name (),
18079 nullptr);
18080 }
18081 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18082 }
18083 else
18084 type = init_complex_type (name, type);
18085 break;
18086 case DW_ATE_decimal_float:
18087 type = init_decfloat_type (objfile, bits, name);
18088 break;
18089 case DW_ATE_float:
18090 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18091 break;
18092 case DW_ATE_signed:
18093 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18094 break;
18095 case DW_ATE_unsigned:
18096 if (cu->language == language_fortran
18097 && name
18098 && startswith (name, "character("))
18099 type = init_character_type (objfile, bits, 1, name);
18100 else
18101 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18102 break;
18103 case DW_ATE_signed_char:
18104 if (cu->language == language_ada || cu->language == language_m2
18105 || cu->language == language_pascal
18106 || cu->language == language_fortran)
18107 type = init_character_type (objfile, bits, 0, name);
18108 else
18109 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18110 break;
18111 case DW_ATE_unsigned_char:
18112 if (cu->language == language_ada || cu->language == language_m2
18113 || cu->language == language_pascal
18114 || cu->language == language_fortran
18115 || cu->language == language_rust)
18116 type = init_character_type (objfile, bits, 1, name);
18117 else
18118 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18119 break;
18120 case DW_ATE_UTF:
18121 {
18122 if (bits == 16)
18123 type = builtin_type (arch)->builtin_char16;
18124 else if (bits == 32)
18125 type = builtin_type (arch)->builtin_char32;
18126 else
18127 {
18128 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18129 bits);
18130 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18131 }
18132 return set_die_type (die, type, cu);
18133 }
18134 break;
18135 case DW_ATE_signed_fixed:
18136 type = init_fixed_point_type (objfile, bits, 0, name);
18137 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18138 break;
18139 case DW_ATE_unsigned_fixed:
18140 type = init_fixed_point_type (objfile, bits, 1, name);
18141 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18142 break;
18143
18144 default:
18145 complaint (_("unsupported DW_AT_encoding: '%s'"),
18146 dwarf_type_encoding_name (encoding));
18147 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18148 break;
18149 }
18150
18151 if (name && strcmp (name, "char") == 0)
18152 type->set_has_no_signedness (true);
18153
18154 maybe_set_alignment (cu, die, type);
18155
18156 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18157
18158 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18159 {
18160 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18161 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18162 {
18163 unsigned real_bit_size = attr->as_unsigned ();
18164 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18165 /* Only use the attributes if they make sense together. */
18166 if (attr == nullptr
18167 || (attr->as_unsigned () + real_bit_size
18168 <= 8 * TYPE_LENGTH (type)))
18169 {
18170 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18171 = real_bit_size;
18172 if (attr != nullptr)
18173 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18174 = attr->as_unsigned ();
18175 }
18176 }
18177 }
18178
18179 return set_die_type (die, type, cu);
18180 }
18181
18182 /* Parse dwarf attribute if it's a block, reference or constant and put the
18183 resulting value of the attribute into struct bound_prop.
18184 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18185
18186 static int
18187 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18188 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18189 struct type *default_type)
18190 {
18191 struct dwarf2_property_baton *baton;
18192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18193 struct objfile *objfile = per_objfile->objfile;
18194 struct obstack *obstack = &objfile->objfile_obstack;
18195
18196 gdb_assert (default_type != NULL);
18197
18198 if (attr == NULL || prop == NULL)
18199 return 0;
18200
18201 if (attr->form_is_block ())
18202 {
18203 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18204 baton->property_type = default_type;
18205 baton->locexpr.per_cu = cu->per_cu;
18206 baton->locexpr.per_objfile = per_objfile;
18207
18208 struct dwarf_block *block = attr->as_block ();
18209 baton->locexpr.size = block->size;
18210 baton->locexpr.data = block->data;
18211 switch (attr->name)
18212 {
18213 case DW_AT_string_length:
18214 baton->locexpr.is_reference = true;
18215 break;
18216 default:
18217 baton->locexpr.is_reference = false;
18218 break;
18219 }
18220
18221 prop->set_locexpr (baton);
18222 gdb_assert (prop->baton () != NULL);
18223 }
18224 else if (attr->form_is_ref ())
18225 {
18226 struct dwarf2_cu *target_cu = cu;
18227 struct die_info *target_die;
18228 struct attribute *target_attr;
18229
18230 target_die = follow_die_ref (die, attr, &target_cu);
18231 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18232 if (target_attr == NULL)
18233 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18234 target_cu);
18235 if (target_attr == NULL)
18236 return 0;
18237
18238 switch (target_attr->name)
18239 {
18240 case DW_AT_location:
18241 if (target_attr->form_is_section_offset ())
18242 {
18243 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18244 baton->property_type = die_type (target_die, target_cu);
18245 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18246 prop->set_loclist (baton);
18247 gdb_assert (prop->baton () != NULL);
18248 }
18249 else if (target_attr->form_is_block ())
18250 {
18251 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18252 baton->property_type = die_type (target_die, target_cu);
18253 baton->locexpr.per_cu = cu->per_cu;
18254 baton->locexpr.per_objfile = per_objfile;
18255 struct dwarf_block *block = target_attr->as_block ();
18256 baton->locexpr.size = block->size;
18257 baton->locexpr.data = block->data;
18258 baton->locexpr.is_reference = true;
18259 prop->set_locexpr (baton);
18260 gdb_assert (prop->baton () != NULL);
18261 }
18262 else
18263 {
18264 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18265 "dynamic property");
18266 return 0;
18267 }
18268 break;
18269 case DW_AT_data_member_location:
18270 {
18271 LONGEST offset;
18272
18273 if (!handle_data_member_location (target_die, target_cu,
18274 &offset))
18275 return 0;
18276
18277 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18278 baton->property_type = read_type_die (target_die->parent,
18279 target_cu);
18280 baton->offset_info.offset = offset;
18281 baton->offset_info.type = die_type (target_die, target_cu);
18282 prop->set_addr_offset (baton);
18283 break;
18284 }
18285 }
18286 }
18287 else if (attr->form_is_constant ())
18288 prop->set_const_val (attr->constant_value (0));
18289 else
18290 {
18291 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18292 dwarf2_name (die, cu));
18293 return 0;
18294 }
18295
18296 return 1;
18297 }
18298
18299 /* See read.h. */
18300
18301 struct type *
18302 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18303 {
18304 struct type *int_type;
18305
18306 /* Helper macro to examine the various builtin types. */
18307 #define TRY_TYPE(F) \
18308 int_type = (unsigned_p \
18309 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18310 : objfile_type (objfile)->builtin_ ## F); \
18311 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18312 return int_type
18313
18314 TRY_TYPE (char);
18315 TRY_TYPE (short);
18316 TRY_TYPE (int);
18317 TRY_TYPE (long);
18318 TRY_TYPE (long_long);
18319
18320 #undef TRY_TYPE
18321
18322 gdb_assert_not_reached ("unable to find suitable integer type");
18323 }
18324
18325 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18326 present (which is valid) then compute the default type based on the
18327 compilation units address size. */
18328
18329 static struct type *
18330 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18331 {
18332 struct type *index_type = die_type (die, cu);
18333
18334 /* Dwarf-2 specifications explicitly allows to create subrange types
18335 without specifying a base type.
18336 In that case, the base type must be set to the type of
18337 the lower bound, upper bound or count, in that order, if any of these
18338 three attributes references an object that has a type.
18339 If no base type is found, the Dwarf-2 specifications say that
18340 a signed integer type of size equal to the size of an address should
18341 be used.
18342 For the following C code: `extern char gdb_int [];'
18343 GCC produces an empty range DIE.
18344 FIXME: muller/2010-05-28: Possible references to object for low bound,
18345 high bound or count are not yet handled by this code. */
18346 if (index_type->code () == TYPE_CODE_VOID)
18347 index_type = cu->addr_sized_int_type (false);
18348
18349 return index_type;
18350 }
18351
18352 /* Read the given DW_AT_subrange DIE. */
18353
18354 static struct type *
18355 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18356 {
18357 struct type *base_type, *orig_base_type;
18358 struct type *range_type;
18359 struct attribute *attr;
18360 struct dynamic_prop low, high;
18361 int low_default_is_valid;
18362 int high_bound_is_count = 0;
18363 const char *name;
18364 ULONGEST negative_mask;
18365
18366 orig_base_type = read_subrange_index_type (die, cu);
18367
18368 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18369 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18370 creating the range type, but we use the result of check_typedef
18371 when examining properties of the type. */
18372 base_type = check_typedef (orig_base_type);
18373
18374 /* The die_type call above may have already set the type for this DIE. */
18375 range_type = get_die_type (die, cu);
18376 if (range_type)
18377 return range_type;
18378
18379 high.set_const_val (0);
18380
18381 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18382 omitting DW_AT_lower_bound. */
18383 switch (cu->language)
18384 {
18385 case language_c:
18386 case language_cplus:
18387 low.set_const_val (0);
18388 low_default_is_valid = 1;
18389 break;
18390 case language_fortran:
18391 low.set_const_val (1);
18392 low_default_is_valid = 1;
18393 break;
18394 case language_d:
18395 case language_objc:
18396 case language_rust:
18397 low.set_const_val (0);
18398 low_default_is_valid = (cu->header.version >= 4);
18399 break;
18400 case language_ada:
18401 case language_m2:
18402 case language_pascal:
18403 low.set_const_val (1);
18404 low_default_is_valid = (cu->header.version >= 4);
18405 break;
18406 default:
18407 low.set_const_val (0);
18408 low_default_is_valid = 0;
18409 break;
18410 }
18411
18412 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18413 if (attr != nullptr)
18414 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18415 else if (!low_default_is_valid)
18416 complaint (_("Missing DW_AT_lower_bound "
18417 "- DIE at %s [in module %s]"),
18418 sect_offset_str (die->sect_off),
18419 objfile_name (cu->per_objfile->objfile));
18420
18421 struct attribute *attr_ub, *attr_count;
18422 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18423 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18424 {
18425 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18426 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18427 {
18428 /* If bounds are constant do the final calculation here. */
18429 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18430 high.set_const_val (low.const_val () + high.const_val () - 1);
18431 else
18432 high_bound_is_count = 1;
18433 }
18434 else
18435 {
18436 if (attr_ub != NULL)
18437 complaint (_("Unresolved DW_AT_upper_bound "
18438 "- DIE at %s [in module %s]"),
18439 sect_offset_str (die->sect_off),
18440 objfile_name (cu->per_objfile->objfile));
18441 if (attr_count != NULL)
18442 complaint (_("Unresolved DW_AT_count "
18443 "- DIE at %s [in module %s]"),
18444 sect_offset_str (die->sect_off),
18445 objfile_name (cu->per_objfile->objfile));
18446 }
18447 }
18448
18449 LONGEST bias = 0;
18450 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18451 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18452 bias = bias_attr->constant_value (0);
18453
18454 /* Normally, the DWARF producers are expected to use a signed
18455 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18456 But this is unfortunately not always the case, as witnessed
18457 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18458 is used instead. To work around that ambiguity, we treat
18459 the bounds as signed, and thus sign-extend their values, when
18460 the base type is signed. */
18461 negative_mask =
18462 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18463 if (low.kind () == PROP_CONST
18464 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18465 low.set_const_val (low.const_val () | negative_mask);
18466 if (high.kind () == PROP_CONST
18467 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18468 high.set_const_val (high.const_val () | negative_mask);
18469
18470 /* Check for bit and byte strides. */
18471 struct dynamic_prop byte_stride_prop;
18472 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18473 if (attr_byte_stride != nullptr)
18474 {
18475 struct type *prop_type = cu->addr_sized_int_type (false);
18476 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18477 prop_type);
18478 }
18479
18480 struct dynamic_prop bit_stride_prop;
18481 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18482 if (attr_bit_stride != nullptr)
18483 {
18484 /* It only makes sense to have either a bit or byte stride. */
18485 if (attr_byte_stride != nullptr)
18486 {
18487 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18488 "- DIE at %s [in module %s]"),
18489 sect_offset_str (die->sect_off),
18490 objfile_name (cu->per_objfile->objfile));
18491 attr_bit_stride = nullptr;
18492 }
18493 else
18494 {
18495 struct type *prop_type = cu->addr_sized_int_type (false);
18496 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18497 prop_type);
18498 }
18499 }
18500
18501 if (attr_byte_stride != nullptr
18502 || attr_bit_stride != nullptr)
18503 {
18504 bool byte_stride_p = (attr_byte_stride != nullptr);
18505 struct dynamic_prop *stride
18506 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18507
18508 range_type
18509 = create_range_type_with_stride (NULL, orig_base_type, &low,
18510 &high, bias, stride, byte_stride_p);
18511 }
18512 else
18513 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18514
18515 if (high_bound_is_count)
18516 range_type->bounds ()->flag_upper_bound_is_count = 1;
18517
18518 /* Ada expects an empty array on no boundary attributes. */
18519 if (attr == NULL && cu->language != language_ada)
18520 range_type->bounds ()->high.set_undefined ();
18521
18522 name = dwarf2_name (die, cu);
18523 if (name)
18524 range_type->set_name (name);
18525
18526 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18527 if (attr != nullptr)
18528 TYPE_LENGTH (range_type) = attr->constant_value (0);
18529
18530 maybe_set_alignment (cu, die, range_type);
18531
18532 set_die_type (die, range_type, cu);
18533
18534 /* set_die_type should be already done. */
18535 set_descriptive_type (range_type, die, cu);
18536
18537 return range_type;
18538 }
18539
18540 static struct type *
18541 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18542 {
18543 struct type *type;
18544
18545 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18546 type->set_name (dwarf2_name (die, cu));
18547
18548 /* In Ada, an unspecified type is typically used when the description
18549 of the type is deferred to a different unit. When encountering
18550 such a type, we treat it as a stub, and try to resolve it later on,
18551 when needed. */
18552 if (cu->language == language_ada)
18553 type->set_is_stub (true);
18554
18555 return set_die_type (die, type, cu);
18556 }
18557
18558 /* Read a single die and all its descendents. Set the die's sibling
18559 field to NULL; set other fields in the die correctly, and set all
18560 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18561 location of the info_ptr after reading all of those dies. PARENT
18562 is the parent of the die in question. */
18563
18564 static struct die_info *
18565 read_die_and_children (const struct die_reader_specs *reader,
18566 const gdb_byte *info_ptr,
18567 const gdb_byte **new_info_ptr,
18568 struct die_info *parent)
18569 {
18570 struct die_info *die;
18571 const gdb_byte *cur_ptr;
18572
18573 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18574 if (die == NULL)
18575 {
18576 *new_info_ptr = cur_ptr;
18577 return NULL;
18578 }
18579 store_in_ref_table (die, reader->cu);
18580
18581 if (die->has_children)
18582 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18583 else
18584 {
18585 die->child = NULL;
18586 *new_info_ptr = cur_ptr;
18587 }
18588
18589 die->sibling = NULL;
18590 die->parent = parent;
18591 return die;
18592 }
18593
18594 /* Read a die, all of its descendents, and all of its siblings; set
18595 all of the fields of all of the dies correctly. Arguments are as
18596 in read_die_and_children. */
18597
18598 static struct die_info *
18599 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18600 const gdb_byte *info_ptr,
18601 const gdb_byte **new_info_ptr,
18602 struct die_info *parent)
18603 {
18604 struct die_info *first_die, *last_sibling;
18605 const gdb_byte *cur_ptr;
18606
18607 cur_ptr = info_ptr;
18608 first_die = last_sibling = NULL;
18609
18610 while (1)
18611 {
18612 struct die_info *die
18613 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18614
18615 if (die == NULL)
18616 {
18617 *new_info_ptr = cur_ptr;
18618 return first_die;
18619 }
18620
18621 if (!first_die)
18622 first_die = die;
18623 else
18624 last_sibling->sibling = die;
18625
18626 last_sibling = die;
18627 }
18628 }
18629
18630 /* Read a die, all of its descendents, and all of its siblings; set
18631 all of the fields of all of the dies correctly. Arguments are as
18632 in read_die_and_children.
18633 This the main entry point for reading a DIE and all its children. */
18634
18635 static struct die_info *
18636 read_die_and_siblings (const struct die_reader_specs *reader,
18637 const gdb_byte *info_ptr,
18638 const gdb_byte **new_info_ptr,
18639 struct die_info *parent)
18640 {
18641 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18642 new_info_ptr, parent);
18643
18644 if (dwarf_die_debug)
18645 {
18646 fprintf_unfiltered (gdb_stdlog,
18647 "Read die from %s@0x%x of %s:\n",
18648 reader->die_section->get_name (),
18649 (unsigned) (info_ptr - reader->die_section->buffer),
18650 bfd_get_filename (reader->abfd));
18651 dump_die (die, dwarf_die_debug);
18652 }
18653
18654 return die;
18655 }
18656
18657 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18658 attributes.
18659 The caller is responsible for filling in the extra attributes
18660 and updating (*DIEP)->num_attrs.
18661 Set DIEP to point to a newly allocated die with its information,
18662 except for its child, sibling, and parent fields. */
18663
18664 static const gdb_byte *
18665 read_full_die_1 (const struct die_reader_specs *reader,
18666 struct die_info **diep, const gdb_byte *info_ptr,
18667 int num_extra_attrs)
18668 {
18669 unsigned int abbrev_number, bytes_read, i;
18670 const struct abbrev_info *abbrev;
18671 struct die_info *die;
18672 struct dwarf2_cu *cu = reader->cu;
18673 bfd *abfd = reader->abfd;
18674
18675 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18676 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18677 info_ptr += bytes_read;
18678 if (!abbrev_number)
18679 {
18680 *diep = NULL;
18681 return info_ptr;
18682 }
18683
18684 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18685 if (!abbrev)
18686 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18687 abbrev_number,
18688 bfd_get_filename (abfd));
18689
18690 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18691 die->sect_off = sect_off;
18692 die->tag = abbrev->tag;
18693 die->abbrev = abbrev_number;
18694 die->has_children = abbrev->has_children;
18695
18696 /* Make the result usable.
18697 The caller needs to update num_attrs after adding the extra
18698 attributes. */
18699 die->num_attrs = abbrev->num_attrs;
18700
18701 bool any_need_reprocess = false;
18702 for (i = 0; i < abbrev->num_attrs; ++i)
18703 {
18704 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18705 info_ptr);
18706 if (die->attrs[i].requires_reprocessing_p ())
18707 any_need_reprocess = true;
18708 }
18709
18710 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18711 if (attr != nullptr && attr->form_is_unsigned ())
18712 cu->str_offsets_base = attr->as_unsigned ();
18713
18714 attr = die->attr (DW_AT_loclists_base);
18715 if (attr != nullptr)
18716 cu->loclist_base = attr->as_unsigned ();
18717
18718 auto maybe_addr_base = die->addr_base ();
18719 if (maybe_addr_base.has_value ())
18720 cu->addr_base = *maybe_addr_base;
18721
18722 attr = die->attr (DW_AT_rnglists_base);
18723 if (attr != nullptr)
18724 cu->rnglists_base = attr->as_unsigned ();
18725
18726 if (any_need_reprocess)
18727 {
18728 for (i = 0; i < abbrev->num_attrs; ++i)
18729 {
18730 if (die->attrs[i].requires_reprocessing_p ())
18731 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18732 }
18733 }
18734 *diep = die;
18735 return info_ptr;
18736 }
18737
18738 /* Read a die and all its attributes.
18739 Set DIEP to point to a newly allocated die with its information,
18740 except for its child, sibling, and parent fields. */
18741
18742 static const gdb_byte *
18743 read_full_die (const struct die_reader_specs *reader,
18744 struct die_info **diep, const gdb_byte *info_ptr)
18745 {
18746 const gdb_byte *result;
18747
18748 result = read_full_die_1 (reader, diep, info_ptr, 0);
18749
18750 if (dwarf_die_debug)
18751 {
18752 fprintf_unfiltered (gdb_stdlog,
18753 "Read die from %s@0x%x of %s:\n",
18754 reader->die_section->get_name (),
18755 (unsigned) (info_ptr - reader->die_section->buffer),
18756 bfd_get_filename (reader->abfd));
18757 dump_die (*diep, dwarf_die_debug);
18758 }
18759
18760 return result;
18761 }
18762 \f
18763
18764 /* Returns nonzero if TAG represents a type that we might generate a partial
18765 symbol for. */
18766
18767 static int
18768 is_type_tag_for_partial (int tag, enum language lang)
18769 {
18770 switch (tag)
18771 {
18772 #if 0
18773 /* Some types that would be reasonable to generate partial symbols for,
18774 that we don't at present. Note that normally this does not
18775 matter, mainly because C compilers don't give names to these
18776 types, but instead emit DW_TAG_typedef. */
18777 case DW_TAG_file_type:
18778 case DW_TAG_ptr_to_member_type:
18779 case DW_TAG_set_type:
18780 case DW_TAG_string_type:
18781 case DW_TAG_subroutine_type:
18782 #endif
18783
18784 /* GNAT may emit an array with a name, but no typedef, so we
18785 need to make a symbol in this case. */
18786 case DW_TAG_array_type:
18787 return lang == language_ada;
18788
18789 case DW_TAG_base_type:
18790 case DW_TAG_class_type:
18791 case DW_TAG_interface_type:
18792 case DW_TAG_enumeration_type:
18793 case DW_TAG_structure_type:
18794 case DW_TAG_subrange_type:
18795 case DW_TAG_typedef:
18796 case DW_TAG_union_type:
18797 return 1;
18798 default:
18799 return 0;
18800 }
18801 }
18802
18803 /* Load all DIEs that are interesting for partial symbols into memory. */
18804
18805 static struct partial_die_info *
18806 load_partial_dies (const struct die_reader_specs *reader,
18807 const gdb_byte *info_ptr, int building_psymtab)
18808 {
18809 struct dwarf2_cu *cu = reader->cu;
18810 struct objfile *objfile = cu->per_objfile->objfile;
18811 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18812 unsigned int bytes_read;
18813 unsigned int load_all = 0;
18814 int nesting_level = 1;
18815
18816 parent_die = NULL;
18817 last_die = NULL;
18818
18819 gdb_assert (cu->per_cu != NULL);
18820 if (cu->load_all_dies)
18821 load_all = 1;
18822
18823 cu->partial_dies
18824 = htab_create_alloc_ex (cu->header.length / 12,
18825 partial_die_hash,
18826 partial_die_eq,
18827 NULL,
18828 &cu->comp_unit_obstack,
18829 hashtab_obstack_allocate,
18830 dummy_obstack_deallocate);
18831
18832 while (1)
18833 {
18834 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18835 &bytes_read);
18836
18837 /* A NULL abbrev means the end of a series of children. */
18838 if (abbrev == NULL)
18839 {
18840 if (--nesting_level == 0)
18841 return first_die;
18842
18843 info_ptr += bytes_read;
18844 last_die = parent_die;
18845 parent_die = parent_die->die_parent;
18846 continue;
18847 }
18848
18849 /* Check for template arguments. We never save these; if
18850 they're seen, we just mark the parent, and go on our way. */
18851 if (parent_die != NULL
18852 && cu->language == language_cplus
18853 && (abbrev->tag == DW_TAG_template_type_param
18854 || abbrev->tag == DW_TAG_template_value_param))
18855 {
18856 parent_die->has_template_arguments = 1;
18857
18858 if (!load_all)
18859 {
18860 /* We don't need a partial DIE for the template argument. */
18861 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18862 continue;
18863 }
18864 }
18865
18866 /* We only recurse into c++ subprograms looking for template arguments.
18867 Skip their other children. */
18868 if (!load_all
18869 && cu->language == language_cplus
18870 && parent_die != NULL
18871 && parent_die->tag == DW_TAG_subprogram
18872 && abbrev->tag != DW_TAG_inlined_subroutine)
18873 {
18874 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18875 continue;
18876 }
18877
18878 /* Check whether this DIE is interesting enough to save. Normally
18879 we would not be interested in members here, but there may be
18880 later variables referencing them via DW_AT_specification (for
18881 static members). */
18882 if (!load_all
18883 && !is_type_tag_for_partial (abbrev->tag, cu->language)
18884 && abbrev->tag != DW_TAG_constant
18885 && abbrev->tag != DW_TAG_enumerator
18886 && abbrev->tag != DW_TAG_subprogram
18887 && abbrev->tag != DW_TAG_inlined_subroutine
18888 && abbrev->tag != DW_TAG_lexical_block
18889 && abbrev->tag != DW_TAG_variable
18890 && abbrev->tag != DW_TAG_namespace
18891 && abbrev->tag != DW_TAG_module
18892 && abbrev->tag != DW_TAG_member
18893 && abbrev->tag != DW_TAG_imported_unit
18894 && abbrev->tag != DW_TAG_imported_declaration)
18895 {
18896 /* Otherwise we skip to the next sibling, if any. */
18897 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18898 continue;
18899 }
18900
18901 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18902 abbrev);
18903
18904 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18905
18906 /* This two-pass algorithm for processing partial symbols has a
18907 high cost in cache pressure. Thus, handle some simple cases
18908 here which cover the majority of C partial symbols. DIEs
18909 which neither have specification tags in them, nor could have
18910 specification tags elsewhere pointing at them, can simply be
18911 processed and discarded.
18912
18913 This segment is also optional; scan_partial_symbols and
18914 add_partial_symbol will handle these DIEs if we chain
18915 them in normally. When compilers which do not emit large
18916 quantities of duplicate debug information are more common,
18917 this code can probably be removed. */
18918
18919 /* Any complete simple types at the top level (pretty much all
18920 of them, for a language without namespaces), can be processed
18921 directly. */
18922 if (parent_die == NULL
18923 && pdi.has_specification == 0
18924 && pdi.is_declaration == 0
18925 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18926 || pdi.tag == DW_TAG_base_type
18927 || pdi.tag == DW_TAG_array_type
18928 || pdi.tag == DW_TAG_subrange_type))
18929 {
18930 if (building_psymtab && pdi.raw_name != NULL)
18931 add_partial_symbol (&pdi, cu);
18932
18933 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18934 continue;
18935 }
18936
18937 /* The exception for DW_TAG_typedef with has_children above is
18938 a workaround of GCC PR debug/47510. In the case of this complaint
18939 type_name_or_error will error on such types later.
18940
18941 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18942 it could not find the child DIEs referenced later, this is checked
18943 above. In correct DWARF DW_TAG_typedef should have no children. */
18944
18945 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18946 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18947 "- DIE at %s [in module %s]"),
18948 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18949
18950 /* If we're at the second level, and we're an enumerator, and
18951 our parent has no specification (meaning possibly lives in a
18952 namespace elsewhere), then we can add the partial symbol now
18953 instead of queueing it. */
18954 if (pdi.tag == DW_TAG_enumerator
18955 && parent_die != NULL
18956 && parent_die->die_parent == NULL
18957 && parent_die->tag == DW_TAG_enumeration_type
18958 && parent_die->has_specification == 0)
18959 {
18960 if (pdi.raw_name == NULL)
18961 complaint (_("malformed enumerator DIE ignored"));
18962 else if (building_psymtab)
18963 add_partial_symbol (&pdi, cu);
18964
18965 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18966 continue;
18967 }
18968
18969 struct partial_die_info *part_die
18970 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18971
18972 /* We'll save this DIE so link it in. */
18973 part_die->die_parent = parent_die;
18974 part_die->die_sibling = NULL;
18975 part_die->die_child = NULL;
18976
18977 if (last_die && last_die == parent_die)
18978 last_die->die_child = part_die;
18979 else if (last_die)
18980 last_die->die_sibling = part_die;
18981
18982 last_die = part_die;
18983
18984 if (first_die == NULL)
18985 first_die = part_die;
18986
18987 /* Maybe add the DIE to the hash table. Not all DIEs that we
18988 find interesting need to be in the hash table, because we
18989 also have the parent/sibling/child chains; only those that we
18990 might refer to by offset later during partial symbol reading.
18991
18992 For now this means things that might have be the target of a
18993 DW_AT_specification, DW_AT_abstract_origin, or
18994 DW_AT_extension. DW_AT_extension will refer only to
18995 namespaces; DW_AT_abstract_origin refers to functions (and
18996 many things under the function DIE, but we do not recurse
18997 into function DIEs during partial symbol reading) and
18998 possibly variables as well; DW_AT_specification refers to
18999 declarations. Declarations ought to have the DW_AT_declaration
19000 flag. It happens that GCC forgets to put it in sometimes, but
19001 only for functions, not for types.
19002
19003 Adding more things than necessary to the hash table is harmless
19004 except for the performance cost. Adding too few will result in
19005 wasted time in find_partial_die, when we reread the compilation
19006 unit with load_all_dies set. */
19007
19008 if (load_all
19009 || abbrev->tag == DW_TAG_constant
19010 || abbrev->tag == DW_TAG_subprogram
19011 || abbrev->tag == DW_TAG_variable
19012 || abbrev->tag == DW_TAG_namespace
19013 || part_die->is_declaration)
19014 {
19015 void **slot;
19016
19017 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19018 to_underlying (part_die->sect_off),
19019 INSERT);
19020 *slot = part_die;
19021 }
19022
19023 /* For some DIEs we want to follow their children (if any). For C
19024 we have no reason to follow the children of structures; for other
19025 languages we have to, so that we can get at method physnames
19026 to infer fully qualified class names, for DW_AT_specification,
19027 and for C++ template arguments. For C++, we also look one level
19028 inside functions to find template arguments (if the name of the
19029 function does not already contain the template arguments).
19030
19031 For Ada and Fortran, we need to scan the children of subprograms
19032 and lexical blocks as well because these languages allow the
19033 definition of nested entities that could be interesting for the
19034 debugger, such as nested subprograms for instance. */
19035 if (last_die->has_children
19036 && (load_all
19037 || last_die->tag == DW_TAG_namespace
19038 || last_die->tag == DW_TAG_module
19039 || last_die->tag == DW_TAG_enumeration_type
19040 || (cu->language == language_cplus
19041 && last_die->tag == DW_TAG_subprogram
19042 && (last_die->raw_name == NULL
19043 || strchr (last_die->raw_name, '<') == NULL))
19044 || (cu->language != language_c
19045 && (last_die->tag == DW_TAG_class_type
19046 || last_die->tag == DW_TAG_interface_type
19047 || last_die->tag == DW_TAG_structure_type
19048 || last_die->tag == DW_TAG_union_type))
19049 || ((cu->language == language_ada
19050 || cu->language == language_fortran)
19051 && (last_die->tag == DW_TAG_subprogram
19052 || last_die->tag == DW_TAG_lexical_block))))
19053 {
19054 nesting_level++;
19055 parent_die = last_die;
19056 continue;
19057 }
19058
19059 /* Otherwise we skip to the next sibling, if any. */
19060 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19061
19062 /* Back to the top, do it again. */
19063 }
19064 }
19065
19066 partial_die_info::partial_die_info (sect_offset sect_off_,
19067 const struct abbrev_info *abbrev)
19068 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19069 {
19070 }
19071
19072 /* See class definition. */
19073
19074 const char *
19075 partial_die_info::name (dwarf2_cu *cu)
19076 {
19077 if (!canonical_name && raw_name != nullptr)
19078 {
19079 struct objfile *objfile = cu->per_objfile->objfile;
19080 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19081 canonical_name = 1;
19082 }
19083
19084 return raw_name;
19085 }
19086
19087 /* Read a minimal amount of information into the minimal die structure.
19088 INFO_PTR should point just after the initial uleb128 of a DIE. */
19089
19090 const gdb_byte *
19091 partial_die_info::read (const struct die_reader_specs *reader,
19092 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19093 {
19094 struct dwarf2_cu *cu = reader->cu;
19095 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19096 unsigned int i;
19097 int has_low_pc_attr = 0;
19098 int has_high_pc_attr = 0;
19099 int high_pc_relative = 0;
19100
19101 for (i = 0; i < abbrev.num_attrs; ++i)
19102 {
19103 attribute attr;
19104 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19105 /* String and address offsets that need to do the reprocessing have
19106 already been read at this point, so there is no need to wait until
19107 the loop terminates to do the reprocessing. */
19108 if (attr.requires_reprocessing_p ())
19109 read_attribute_reprocess (reader, &attr, tag);
19110 /* Store the data if it is of an attribute we want to keep in a
19111 partial symbol table. */
19112 switch (attr.name)
19113 {
19114 case DW_AT_name:
19115 switch (tag)
19116 {
19117 case DW_TAG_compile_unit:
19118 case DW_TAG_partial_unit:
19119 case DW_TAG_type_unit:
19120 /* Compilation units have a DW_AT_name that is a filename, not
19121 a source language identifier. */
19122 case DW_TAG_enumeration_type:
19123 case DW_TAG_enumerator:
19124 /* These tags always have simple identifiers already; no need
19125 to canonicalize them. */
19126 canonical_name = 1;
19127 raw_name = attr.as_string ();
19128 break;
19129 default:
19130 canonical_name = 0;
19131 raw_name = attr.as_string ();
19132 break;
19133 }
19134 break;
19135 case DW_AT_linkage_name:
19136 case DW_AT_MIPS_linkage_name:
19137 /* Note that both forms of linkage name might appear. We
19138 assume they will be the same, and we only store the last
19139 one we see. */
19140 linkage_name = attr.as_string ();
19141 break;
19142 case DW_AT_low_pc:
19143 has_low_pc_attr = 1;
19144 lowpc = attr.as_address ();
19145 break;
19146 case DW_AT_high_pc:
19147 has_high_pc_attr = 1;
19148 highpc = attr.as_address ();
19149 if (cu->header.version >= 4 && attr.form_is_constant ())
19150 high_pc_relative = 1;
19151 break;
19152 case DW_AT_location:
19153 /* Support the .debug_loc offsets. */
19154 if (attr.form_is_block ())
19155 {
19156 d.locdesc = attr.as_block ();
19157 }
19158 else if (attr.form_is_section_offset ())
19159 {
19160 dwarf2_complex_location_expr_complaint ();
19161 }
19162 else
19163 {
19164 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19165 "partial symbol information");
19166 }
19167 break;
19168 case DW_AT_external:
19169 is_external = attr.as_boolean ();
19170 break;
19171 case DW_AT_declaration:
19172 is_declaration = attr.as_boolean ();
19173 break;
19174 case DW_AT_type:
19175 has_type = 1;
19176 break;
19177 case DW_AT_abstract_origin:
19178 case DW_AT_specification:
19179 case DW_AT_extension:
19180 has_specification = 1;
19181 spec_offset = attr.get_ref_die_offset ();
19182 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19183 || cu->per_cu->is_dwz);
19184 break;
19185 case DW_AT_sibling:
19186 /* Ignore absolute siblings, they might point outside of
19187 the current compile unit. */
19188 if (attr.form == DW_FORM_ref_addr)
19189 complaint (_("ignoring absolute DW_AT_sibling"));
19190 else
19191 {
19192 const gdb_byte *buffer = reader->buffer;
19193 sect_offset off = attr.get_ref_die_offset ();
19194 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19195
19196 if (sibling_ptr < info_ptr)
19197 complaint (_("DW_AT_sibling points backwards"));
19198 else if (sibling_ptr > reader->buffer_end)
19199 reader->die_section->overflow_complaint ();
19200 else
19201 sibling = sibling_ptr;
19202 }
19203 break;
19204 case DW_AT_byte_size:
19205 has_byte_size = 1;
19206 break;
19207 case DW_AT_const_value:
19208 has_const_value = 1;
19209 break;
19210 case DW_AT_calling_convention:
19211 /* DWARF doesn't provide a way to identify a program's source-level
19212 entry point. DW_AT_calling_convention attributes are only meant
19213 to describe functions' calling conventions.
19214
19215 However, because it's a necessary piece of information in
19216 Fortran, and before DWARF 4 DW_CC_program was the only
19217 piece of debugging information whose definition refers to
19218 a 'main program' at all, several compilers marked Fortran
19219 main programs with DW_CC_program --- even when those
19220 functions use the standard calling conventions.
19221
19222 Although DWARF now specifies a way to provide this
19223 information, we support this practice for backward
19224 compatibility. */
19225 if (attr.constant_value (0) == DW_CC_program
19226 && cu->language == language_fortran)
19227 main_subprogram = 1;
19228 break;
19229 case DW_AT_inline:
19230 {
19231 LONGEST value = attr.constant_value (-1);
19232 if (value == DW_INL_inlined
19233 || value == DW_INL_declared_inlined)
19234 may_be_inlined = 1;
19235 }
19236 break;
19237
19238 case DW_AT_import:
19239 if (tag == DW_TAG_imported_unit)
19240 {
19241 d.sect_off = attr.get_ref_die_offset ();
19242 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19243 || cu->per_cu->is_dwz);
19244 }
19245 break;
19246
19247 case DW_AT_main_subprogram:
19248 main_subprogram = attr.as_boolean ();
19249 break;
19250
19251 case DW_AT_ranges:
19252 {
19253 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19254 on DWARF version). */
19255 ULONGEST ranges_offset = attr.as_unsigned ();
19256
19257 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19258 this value. */
19259 if (tag != DW_TAG_compile_unit)
19260 ranges_offset += cu->gnu_ranges_base;
19261
19262 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19263 nullptr, tag))
19264 has_pc_info = 1;
19265 }
19266 break;
19267
19268 default:
19269 break;
19270 }
19271 }
19272
19273 /* For Ada, if both the name and the linkage name appear, we prefer
19274 the latter. This lets "catch exception" work better, regardless
19275 of the order in which the name and linkage name were emitted.
19276 Really, though, this is just a workaround for the fact that gdb
19277 doesn't store both the name and the linkage name. */
19278 if (cu->language == language_ada && linkage_name != nullptr)
19279 raw_name = linkage_name;
19280
19281 if (high_pc_relative)
19282 highpc += lowpc;
19283
19284 if (has_low_pc_attr && has_high_pc_attr)
19285 {
19286 /* When using the GNU linker, .gnu.linkonce. sections are used to
19287 eliminate duplicate copies of functions and vtables and such.
19288 The linker will arbitrarily choose one and discard the others.
19289 The AT_*_pc values for such functions refer to local labels in
19290 these sections. If the section from that file was discarded, the
19291 labels are not in the output, so the relocs get a value of 0.
19292 If this is a discarded function, mark the pc bounds as invalid,
19293 so that GDB will ignore it. */
19294 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19295 {
19296 struct objfile *objfile = per_objfile->objfile;
19297 struct gdbarch *gdbarch = objfile->arch ();
19298
19299 complaint (_("DW_AT_low_pc %s is zero "
19300 "for DIE at %s [in module %s]"),
19301 paddress (gdbarch, lowpc),
19302 sect_offset_str (sect_off),
19303 objfile_name (objfile));
19304 }
19305 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19306 else if (lowpc >= highpc)
19307 {
19308 struct objfile *objfile = per_objfile->objfile;
19309 struct gdbarch *gdbarch = objfile->arch ();
19310
19311 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19312 "for DIE at %s [in module %s]"),
19313 paddress (gdbarch, lowpc),
19314 paddress (gdbarch, highpc),
19315 sect_offset_str (sect_off),
19316 objfile_name (objfile));
19317 }
19318 else
19319 has_pc_info = 1;
19320 }
19321
19322 return info_ptr;
19323 }
19324
19325 /* Find a cached partial DIE at OFFSET in CU. */
19326
19327 struct partial_die_info *
19328 dwarf2_cu::find_partial_die (sect_offset sect_off)
19329 {
19330 struct partial_die_info *lookup_die = NULL;
19331 struct partial_die_info part_die (sect_off);
19332
19333 lookup_die = ((struct partial_die_info *)
19334 htab_find_with_hash (partial_dies, &part_die,
19335 to_underlying (sect_off)));
19336
19337 return lookup_die;
19338 }
19339
19340 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19341 except in the case of .debug_types DIEs which do not reference
19342 outside their CU (they do however referencing other types via
19343 DW_FORM_ref_sig8). */
19344
19345 static const struct cu_partial_die_info
19346 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19347 {
19348 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19349 struct objfile *objfile = per_objfile->objfile;
19350 struct partial_die_info *pd = NULL;
19351
19352 if (offset_in_dwz == cu->per_cu->is_dwz
19353 && cu->header.offset_in_cu_p (sect_off))
19354 {
19355 pd = cu->find_partial_die (sect_off);
19356 if (pd != NULL)
19357 return { cu, pd };
19358 /* We missed recording what we needed.
19359 Load all dies and try again. */
19360 }
19361 else
19362 {
19363 /* TUs don't reference other CUs/TUs (except via type signatures). */
19364 if (cu->per_cu->is_debug_types)
19365 {
19366 error (_("Dwarf Error: Type Unit at offset %s contains"
19367 " external reference to offset %s [in module %s].\n"),
19368 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19369 bfd_get_filename (objfile->obfd));
19370 }
19371 dwarf2_per_cu_data *per_cu
19372 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19373 per_objfile);
19374
19375 cu = per_objfile->get_cu (per_cu);
19376 if (cu == NULL || cu->partial_dies == NULL)
19377 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19378
19379 cu = per_objfile->get_cu (per_cu);
19380
19381 cu->last_used = 0;
19382 pd = cu->find_partial_die (sect_off);
19383 }
19384
19385 /* If we didn't find it, and not all dies have been loaded,
19386 load them all and try again. */
19387
19388 if (pd == NULL && cu->load_all_dies == 0)
19389 {
19390 cu->load_all_dies = 1;
19391
19392 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19393 THIS_CU->cu may already be in use. So we can't just free it and
19394 replace its DIEs with the ones we read in. Instead, we leave those
19395 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19396 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19397 set. */
19398 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19399
19400 pd = cu->find_partial_die (sect_off);
19401 }
19402
19403 if (pd == NULL)
19404 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19405 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19406 return { cu, pd };
19407 }
19408
19409 /* See if we can figure out if the class lives in a namespace. We do
19410 this by looking for a member function; its demangled name will
19411 contain namespace info, if there is any. */
19412
19413 static void
19414 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19415 struct dwarf2_cu *cu)
19416 {
19417 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19418 what template types look like, because the demangler
19419 frequently doesn't give the same name as the debug info. We
19420 could fix this by only using the demangled name to get the
19421 prefix (but see comment in read_structure_type). */
19422
19423 struct partial_die_info *real_pdi;
19424 struct partial_die_info *child_pdi;
19425
19426 /* If this DIE (this DIE's specification, if any) has a parent, then
19427 we should not do this. We'll prepend the parent's fully qualified
19428 name when we create the partial symbol. */
19429
19430 real_pdi = struct_pdi;
19431 while (real_pdi->has_specification)
19432 {
19433 auto res = find_partial_die (real_pdi->spec_offset,
19434 real_pdi->spec_is_dwz, cu);
19435 real_pdi = res.pdi;
19436 cu = res.cu;
19437 }
19438
19439 if (real_pdi->die_parent != NULL)
19440 return;
19441
19442 for (child_pdi = struct_pdi->die_child;
19443 child_pdi != NULL;
19444 child_pdi = child_pdi->die_sibling)
19445 {
19446 if (child_pdi->tag == DW_TAG_subprogram
19447 && child_pdi->linkage_name != NULL)
19448 {
19449 gdb::unique_xmalloc_ptr<char> actual_class_name
19450 (cu->language_defn->class_name_from_physname
19451 (child_pdi->linkage_name));
19452 if (actual_class_name != NULL)
19453 {
19454 struct objfile *objfile = cu->per_objfile->objfile;
19455 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19456 struct_pdi->canonical_name = 1;
19457 }
19458 break;
19459 }
19460 }
19461 }
19462
19463 /* Return true if a DIE with TAG may have the DW_AT_const_value
19464 attribute. */
19465
19466 static bool
19467 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19468 {
19469 switch (tag)
19470 {
19471 case DW_TAG_constant:
19472 case DW_TAG_enumerator:
19473 case DW_TAG_formal_parameter:
19474 case DW_TAG_template_value_param:
19475 case DW_TAG_variable:
19476 return true;
19477 }
19478
19479 return false;
19480 }
19481
19482 void
19483 partial_die_info::fixup (struct dwarf2_cu *cu)
19484 {
19485 /* Once we've fixed up a die, there's no point in doing so again.
19486 This also avoids a memory leak if we were to call
19487 guess_partial_die_structure_name multiple times. */
19488 if (fixup_called)
19489 return;
19490
19491 /* If we found a reference attribute and the DIE has no name, try
19492 to find a name in the referred to DIE. */
19493
19494 if (raw_name == NULL && has_specification)
19495 {
19496 struct partial_die_info *spec_die;
19497
19498 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19499 spec_die = res.pdi;
19500 cu = res.cu;
19501
19502 spec_die->fixup (cu);
19503
19504 if (spec_die->raw_name)
19505 {
19506 raw_name = spec_die->raw_name;
19507 canonical_name = spec_die->canonical_name;
19508
19509 /* Copy DW_AT_external attribute if it is set. */
19510 if (spec_die->is_external)
19511 is_external = spec_die->is_external;
19512 }
19513 }
19514
19515 if (!has_const_value && has_specification
19516 && can_have_DW_AT_const_value_p (tag))
19517 {
19518 struct partial_die_info *spec_die;
19519
19520 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19521 spec_die = res.pdi;
19522 cu = res.cu;
19523
19524 spec_die->fixup (cu);
19525
19526 if (spec_die->has_const_value)
19527 {
19528 /* Copy DW_AT_const_value attribute if it is set. */
19529 has_const_value = spec_die->has_const_value;
19530 }
19531 }
19532
19533 /* Set default names for some unnamed DIEs. */
19534
19535 if (raw_name == NULL && tag == DW_TAG_namespace)
19536 {
19537 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19538 canonical_name = 1;
19539 }
19540
19541 /* If there is no parent die to provide a namespace, and there are
19542 children, see if we can determine the namespace from their linkage
19543 name. */
19544 if (cu->language == language_cplus
19545 && !cu->per_objfile->per_bfd->types.empty ()
19546 && die_parent == NULL
19547 && has_children
19548 && (tag == DW_TAG_class_type
19549 || tag == DW_TAG_structure_type
19550 || tag == DW_TAG_union_type))
19551 guess_partial_die_structure_name (this, cu);
19552
19553 /* GCC might emit a nameless struct or union that has a linkage
19554 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19555 if (raw_name == NULL
19556 && (tag == DW_TAG_class_type
19557 || tag == DW_TAG_interface_type
19558 || tag == DW_TAG_structure_type
19559 || tag == DW_TAG_union_type)
19560 && linkage_name != NULL)
19561 {
19562 gdb::unique_xmalloc_ptr<char> demangled
19563 (gdb_demangle (linkage_name, DMGL_TYPES));
19564 if (demangled != nullptr)
19565 {
19566 const char *base;
19567
19568 /* Strip any leading namespaces/classes, keep only the base name.
19569 DW_AT_name for named DIEs does not contain the prefixes. */
19570 base = strrchr (demangled.get (), ':');
19571 if (base && base > demangled.get () && base[-1] == ':')
19572 base++;
19573 else
19574 base = demangled.get ();
19575
19576 struct objfile *objfile = cu->per_objfile->objfile;
19577 raw_name = objfile->intern (base);
19578 canonical_name = 1;
19579 }
19580 }
19581
19582 fixup_called = 1;
19583 }
19584
19585 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19586 contents from the given SECTION in the HEADER.
19587
19588 HEADER_OFFSET is the offset of the header in the section. */
19589 static void
19590 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19591 struct dwarf2_section_info *section,
19592 sect_offset header_offset)
19593 {
19594 unsigned int bytes_read;
19595 bfd *abfd = section->get_bfd_owner ();
19596 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19597
19598 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19599 info_ptr += bytes_read;
19600
19601 header->version = read_2_bytes (abfd, info_ptr);
19602 info_ptr += 2;
19603
19604 header->addr_size = read_1_byte (abfd, info_ptr);
19605 info_ptr += 1;
19606
19607 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19608 info_ptr += 1;
19609
19610 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19611 }
19612
19613 /* Return the DW_AT_loclists_base value for the CU. */
19614 static ULONGEST
19615 lookup_loclist_base (struct dwarf2_cu *cu)
19616 {
19617 /* For the .dwo unit, the loclist_base points to the first offset following
19618 the header. The header consists of the following entities-
19619 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19620 bit format)
19621 2. version (2 bytes)
19622 3. address size (1 byte)
19623 4. segment selector size (1 byte)
19624 5. offset entry count (4 bytes)
19625 These sizes are derived as per the DWARFv5 standard. */
19626 if (cu->dwo_unit != nullptr)
19627 {
19628 if (cu->header.initial_length_size == 4)
19629 return LOCLIST_HEADER_SIZE32;
19630 return LOCLIST_HEADER_SIZE64;
19631 }
19632 return cu->loclist_base;
19633 }
19634
19635 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19636 array of offsets in the .debug_loclists section. */
19637
19638 static sect_offset
19639 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19640 {
19641 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19642 struct objfile *objfile = per_objfile->objfile;
19643 bfd *abfd = objfile->obfd;
19644 ULONGEST loclist_header_size =
19645 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19646 : LOCLIST_HEADER_SIZE64);
19647 ULONGEST loclist_base = lookup_loclist_base (cu);
19648
19649 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19650 ULONGEST start_offset =
19651 loclist_base + loclist_index * cu->header.offset_size;
19652
19653 /* Get loclists section. */
19654 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19655
19656 /* Read the loclists section content. */
19657 section->read (objfile);
19658 if (section->buffer == NULL)
19659 error (_("DW_FORM_loclistx used without .debug_loclists "
19660 "section [in module %s]"), objfile_name (objfile));
19661
19662 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19663 so if loclist_base is smaller than the header size, we have a problem. */
19664 if (loclist_base < loclist_header_size)
19665 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19666 objfile_name (objfile));
19667
19668 /* Read the header of the loclists contribution. */
19669 struct loclists_rnglists_header header;
19670 read_loclists_rnglists_header (&header, section,
19671 (sect_offset) (loclist_base - loclist_header_size));
19672
19673 /* Verify the loclist index is valid. */
19674 if (loclist_index >= header.offset_entry_count)
19675 error (_("DW_FORM_loclistx pointing outside of "
19676 ".debug_loclists offset array [in module %s]"),
19677 objfile_name (objfile));
19678
19679 /* Validate that reading won't go beyond the end of the section. */
19680 if (start_offset + cu->header.offset_size > section->size)
19681 error (_("Reading DW_FORM_loclistx index beyond end of"
19682 ".debug_loclists section [in module %s]"),
19683 objfile_name (objfile));
19684
19685 const gdb_byte *info_ptr = section->buffer + start_offset;
19686
19687 if (cu->header.offset_size == 4)
19688 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19689 else
19690 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19691 }
19692
19693 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19694 array of offsets in the .debug_rnglists section. */
19695
19696 static sect_offset
19697 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19698 dwarf_tag tag)
19699 {
19700 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19701 struct objfile *objfile = dwarf2_per_objfile->objfile;
19702 bfd *abfd = objfile->obfd;
19703 ULONGEST rnglist_header_size =
19704 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19705 : RNGLIST_HEADER_SIZE64);
19706
19707 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19708 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19709 doesn't apply. */
19710 ULONGEST rnglist_base =
19711 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19712
19713 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19714 ULONGEST start_offset =
19715 rnglist_base + rnglist_index * cu->header.offset_size;
19716
19717 /* Get rnglists section. */
19718 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19719
19720 /* Read the rnglists section content. */
19721 section->read (objfile);
19722 if (section->buffer == nullptr)
19723 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19724 "[in module %s]"),
19725 objfile_name (objfile));
19726
19727 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19728 so if rnglist_base is smaller than the header size, we have a problem. */
19729 if (rnglist_base < rnglist_header_size)
19730 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19731 objfile_name (objfile));
19732
19733 /* Read the header of the rnglists contribution. */
19734 struct loclists_rnglists_header header;
19735 read_loclists_rnglists_header (&header, section,
19736 (sect_offset) (rnglist_base - rnglist_header_size));
19737
19738 /* Verify the rnglist index is valid. */
19739 if (rnglist_index >= header.offset_entry_count)
19740 error (_("DW_FORM_rnglistx index pointing outside of "
19741 ".debug_rnglists offset array [in module %s]"),
19742 objfile_name (objfile));
19743
19744 /* Validate that reading won't go beyond the end of the section. */
19745 if (start_offset + cu->header.offset_size > section->size)
19746 error (_("Reading DW_FORM_rnglistx index beyond end of"
19747 ".debug_rnglists section [in module %s]"),
19748 objfile_name (objfile));
19749
19750 const gdb_byte *info_ptr = section->buffer + start_offset;
19751
19752 if (cu->header.offset_size == 4)
19753 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19754 else
19755 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19756 }
19757
19758 /* Process the attributes that had to be skipped in the first round. These
19759 attributes are the ones that need str_offsets_base or addr_base attributes.
19760 They could not have been processed in the first round, because at the time
19761 the values of str_offsets_base or addr_base may not have been known. */
19762 static void
19763 read_attribute_reprocess (const struct die_reader_specs *reader,
19764 struct attribute *attr, dwarf_tag tag)
19765 {
19766 struct dwarf2_cu *cu = reader->cu;
19767 switch (attr->form)
19768 {
19769 case DW_FORM_addrx:
19770 case DW_FORM_GNU_addr_index:
19771 attr->set_address (read_addr_index (cu,
19772 attr->as_unsigned_reprocess ()));
19773 break;
19774 case DW_FORM_loclistx:
19775 {
19776 sect_offset loclists_sect_off
19777 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19778
19779 attr->set_unsigned (to_underlying (loclists_sect_off));
19780 }
19781 break;
19782 case DW_FORM_rnglistx:
19783 {
19784 sect_offset rnglists_sect_off
19785 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19786
19787 attr->set_unsigned (to_underlying (rnglists_sect_off));
19788 }
19789 break;
19790 case DW_FORM_strx:
19791 case DW_FORM_strx1:
19792 case DW_FORM_strx2:
19793 case DW_FORM_strx3:
19794 case DW_FORM_strx4:
19795 case DW_FORM_GNU_str_index:
19796 {
19797 unsigned int str_index = attr->as_unsigned_reprocess ();
19798 gdb_assert (!attr->canonical_string_p ());
19799 if (reader->dwo_file != NULL)
19800 attr->set_string_noncanonical (read_dwo_str_index (reader,
19801 str_index));
19802 else
19803 attr->set_string_noncanonical (read_stub_str_index (cu,
19804 str_index));
19805 break;
19806 }
19807 default:
19808 gdb_assert_not_reached (_("Unexpected DWARF form."));
19809 }
19810 }
19811
19812 /* Read an attribute value described by an attribute form. */
19813
19814 static const gdb_byte *
19815 read_attribute_value (const struct die_reader_specs *reader,
19816 struct attribute *attr, unsigned form,
19817 LONGEST implicit_const, const gdb_byte *info_ptr)
19818 {
19819 struct dwarf2_cu *cu = reader->cu;
19820 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19821 struct objfile *objfile = per_objfile->objfile;
19822 bfd *abfd = reader->abfd;
19823 struct comp_unit_head *cu_header = &cu->header;
19824 unsigned int bytes_read;
19825 struct dwarf_block *blk;
19826
19827 attr->form = (enum dwarf_form) form;
19828 switch (form)
19829 {
19830 case DW_FORM_ref_addr:
19831 if (cu_header->version == 2)
19832 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
19833 &bytes_read));
19834 else
19835 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19836 &bytes_read));
19837 info_ptr += bytes_read;
19838 break;
19839 case DW_FORM_GNU_ref_alt:
19840 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19841 &bytes_read));
19842 info_ptr += bytes_read;
19843 break;
19844 case DW_FORM_addr:
19845 {
19846 struct gdbarch *gdbarch = objfile->arch ();
19847 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
19848 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19849 attr->set_address (addr);
19850 info_ptr += bytes_read;
19851 }
19852 break;
19853 case DW_FORM_block2:
19854 blk = dwarf_alloc_block (cu);
19855 blk->size = read_2_bytes (abfd, info_ptr);
19856 info_ptr += 2;
19857 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19858 info_ptr += blk->size;
19859 attr->set_block (blk);
19860 break;
19861 case DW_FORM_block4:
19862 blk = dwarf_alloc_block (cu);
19863 blk->size = read_4_bytes (abfd, info_ptr);
19864 info_ptr += 4;
19865 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19866 info_ptr += blk->size;
19867 attr->set_block (blk);
19868 break;
19869 case DW_FORM_data2:
19870 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19871 info_ptr += 2;
19872 break;
19873 case DW_FORM_data4:
19874 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19875 info_ptr += 4;
19876 break;
19877 case DW_FORM_data8:
19878 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19879 info_ptr += 8;
19880 break;
19881 case DW_FORM_data16:
19882 blk = dwarf_alloc_block (cu);
19883 blk->size = 16;
19884 blk->data = read_n_bytes (abfd, info_ptr, 16);
19885 info_ptr += 16;
19886 attr->set_block (blk);
19887 break;
19888 case DW_FORM_sec_offset:
19889 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19890 &bytes_read));
19891 info_ptr += bytes_read;
19892 break;
19893 case DW_FORM_loclistx:
19894 {
19895 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19896 &bytes_read));
19897 info_ptr += bytes_read;
19898 }
19899 break;
19900 case DW_FORM_string:
19901 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19902 &bytes_read));
19903 info_ptr += bytes_read;
19904 break;
19905 case DW_FORM_strp:
19906 if (!cu->per_cu->is_dwz)
19907 {
19908 attr->set_string_noncanonical
19909 (read_indirect_string (per_objfile,
19910 abfd, info_ptr, cu_header,
19911 &bytes_read));
19912 info_ptr += bytes_read;
19913 break;
19914 }
19915 /* FALLTHROUGH */
19916 case DW_FORM_line_strp:
19917 if (!cu->per_cu->is_dwz)
19918 {
19919 attr->set_string_noncanonical
19920 (per_objfile->read_line_string (info_ptr, cu_header,
19921 &bytes_read));
19922 info_ptr += bytes_read;
19923 break;
19924 }
19925 /* FALLTHROUGH */
19926 case DW_FORM_GNU_strp_alt:
19927 {
19928 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19929 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19930 &bytes_read);
19931
19932 attr->set_string_noncanonical
19933 (dwz->read_string (objfile, str_offset));
19934 info_ptr += bytes_read;
19935 }
19936 break;
19937 case DW_FORM_exprloc:
19938 case DW_FORM_block:
19939 blk = dwarf_alloc_block (cu);
19940 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19941 info_ptr += bytes_read;
19942 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19943 info_ptr += blk->size;
19944 attr->set_block (blk);
19945 break;
19946 case DW_FORM_block1:
19947 blk = dwarf_alloc_block (cu);
19948 blk->size = read_1_byte (abfd, info_ptr);
19949 info_ptr += 1;
19950 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19951 info_ptr += blk->size;
19952 attr->set_block (blk);
19953 break;
19954 case DW_FORM_data1:
19955 case DW_FORM_flag:
19956 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19957 info_ptr += 1;
19958 break;
19959 case DW_FORM_flag_present:
19960 attr->set_unsigned (1);
19961 break;
19962 case DW_FORM_sdata:
19963 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19964 info_ptr += bytes_read;
19965 break;
19966 case DW_FORM_rnglistx:
19967 {
19968 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19969 &bytes_read));
19970 info_ptr += bytes_read;
19971 }
19972 break;
19973 case DW_FORM_udata:
19974 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19975 info_ptr += bytes_read;
19976 break;
19977 case DW_FORM_ref1:
19978 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19979 + read_1_byte (abfd, info_ptr)));
19980 info_ptr += 1;
19981 break;
19982 case DW_FORM_ref2:
19983 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19984 + read_2_bytes (abfd, info_ptr)));
19985 info_ptr += 2;
19986 break;
19987 case DW_FORM_ref4:
19988 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19989 + read_4_bytes (abfd, info_ptr)));
19990 info_ptr += 4;
19991 break;
19992 case DW_FORM_ref8:
19993 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19994 + read_8_bytes (abfd, info_ptr)));
19995 info_ptr += 8;
19996 break;
19997 case DW_FORM_ref_sig8:
19998 attr->set_signature (read_8_bytes (abfd, info_ptr));
19999 info_ptr += 8;
20000 break;
20001 case DW_FORM_ref_udata:
20002 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20003 + read_unsigned_leb128 (abfd, info_ptr,
20004 &bytes_read)));
20005 info_ptr += bytes_read;
20006 break;
20007 case DW_FORM_indirect:
20008 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20009 info_ptr += bytes_read;
20010 if (form == DW_FORM_implicit_const)
20011 {
20012 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20013 info_ptr += bytes_read;
20014 }
20015 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20016 info_ptr);
20017 break;
20018 case DW_FORM_implicit_const:
20019 attr->set_signed (implicit_const);
20020 break;
20021 case DW_FORM_addrx:
20022 case DW_FORM_GNU_addr_index:
20023 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20024 &bytes_read));
20025 info_ptr += bytes_read;
20026 break;
20027 case DW_FORM_strx:
20028 case DW_FORM_strx1:
20029 case DW_FORM_strx2:
20030 case DW_FORM_strx3:
20031 case DW_FORM_strx4:
20032 case DW_FORM_GNU_str_index:
20033 {
20034 ULONGEST str_index;
20035 if (form == DW_FORM_strx1)
20036 {
20037 str_index = read_1_byte (abfd, info_ptr);
20038 info_ptr += 1;
20039 }
20040 else if (form == DW_FORM_strx2)
20041 {
20042 str_index = read_2_bytes (abfd, info_ptr);
20043 info_ptr += 2;
20044 }
20045 else if (form == DW_FORM_strx3)
20046 {
20047 str_index = read_3_bytes (abfd, info_ptr);
20048 info_ptr += 3;
20049 }
20050 else if (form == DW_FORM_strx4)
20051 {
20052 str_index = read_4_bytes (abfd, info_ptr);
20053 info_ptr += 4;
20054 }
20055 else
20056 {
20057 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20058 info_ptr += bytes_read;
20059 }
20060 attr->set_unsigned_reprocess (str_index);
20061 }
20062 break;
20063 default:
20064 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20065 dwarf_form_name (form),
20066 bfd_get_filename (abfd));
20067 }
20068
20069 /* Super hack. */
20070 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20071 attr->form = DW_FORM_GNU_ref_alt;
20072
20073 /* We have seen instances where the compiler tried to emit a byte
20074 size attribute of -1 which ended up being encoded as an unsigned
20075 0xffffffff. Although 0xffffffff is technically a valid size value,
20076 an object of this size seems pretty unlikely so we can relatively
20077 safely treat these cases as if the size attribute was invalid and
20078 treat them as zero by default. */
20079 if (attr->name == DW_AT_byte_size
20080 && form == DW_FORM_data4
20081 && attr->as_unsigned () >= 0xffffffff)
20082 {
20083 complaint
20084 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20085 hex_string (attr->as_unsigned ()));
20086 attr->set_unsigned (0);
20087 }
20088
20089 return info_ptr;
20090 }
20091
20092 /* Read an attribute described by an abbreviated attribute. */
20093
20094 static const gdb_byte *
20095 read_attribute (const struct die_reader_specs *reader,
20096 struct attribute *attr, const struct attr_abbrev *abbrev,
20097 const gdb_byte *info_ptr)
20098 {
20099 attr->name = abbrev->name;
20100 attr->string_is_canonical = 0;
20101 attr->requires_reprocessing = 0;
20102 return read_attribute_value (reader, attr, abbrev->form,
20103 abbrev->implicit_const, info_ptr);
20104 }
20105
20106 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20107
20108 static const char *
20109 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20110 LONGEST str_offset)
20111 {
20112 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20113 str_offset, "DW_FORM_strp");
20114 }
20115
20116 /* Return pointer to string at .debug_str offset as read from BUF.
20117 BUF is assumed to be in a compilation unit described by CU_HEADER.
20118 Return *BYTES_READ_PTR count of bytes read from BUF. */
20119
20120 static const char *
20121 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20122 const gdb_byte *buf,
20123 const struct comp_unit_head *cu_header,
20124 unsigned int *bytes_read_ptr)
20125 {
20126 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20127
20128 return read_indirect_string_at_offset (per_objfile, str_offset);
20129 }
20130
20131 /* See read.h. */
20132
20133 const char *
20134 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20135 const struct comp_unit_head *cu_header,
20136 unsigned int *bytes_read_ptr)
20137 {
20138 bfd *abfd = objfile->obfd;
20139 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20140
20141 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20142 }
20143
20144 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20145 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20146 ADDR_SIZE is the size of addresses from the CU header. */
20147
20148 static CORE_ADDR
20149 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20150 gdb::optional<ULONGEST> addr_base, int addr_size)
20151 {
20152 struct objfile *objfile = per_objfile->objfile;
20153 bfd *abfd = objfile->obfd;
20154 const gdb_byte *info_ptr;
20155 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20156
20157 per_objfile->per_bfd->addr.read (objfile);
20158 if (per_objfile->per_bfd->addr.buffer == NULL)
20159 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20160 objfile_name (objfile));
20161 if (addr_base_or_zero + addr_index * addr_size
20162 >= per_objfile->per_bfd->addr.size)
20163 error (_("DW_FORM_addr_index pointing outside of "
20164 ".debug_addr section [in module %s]"),
20165 objfile_name (objfile));
20166 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20167 + addr_index * addr_size);
20168 if (addr_size == 4)
20169 return bfd_get_32 (abfd, info_ptr);
20170 else
20171 return bfd_get_64 (abfd, info_ptr);
20172 }
20173
20174 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20175
20176 static CORE_ADDR
20177 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20178 {
20179 return read_addr_index_1 (cu->per_objfile, addr_index,
20180 cu->addr_base, cu->header.addr_size);
20181 }
20182
20183 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20184
20185 static CORE_ADDR
20186 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20187 unsigned int *bytes_read)
20188 {
20189 bfd *abfd = cu->per_objfile->objfile->obfd;
20190 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20191
20192 return read_addr_index (cu, addr_index);
20193 }
20194
20195 /* See read.h. */
20196
20197 CORE_ADDR
20198 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20199 dwarf2_per_objfile *per_objfile,
20200 unsigned int addr_index)
20201 {
20202 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20203 gdb::optional<ULONGEST> addr_base;
20204 int addr_size;
20205
20206 /* We need addr_base and addr_size.
20207 If we don't have PER_CU->cu, we have to get it.
20208 Nasty, but the alternative is storing the needed info in PER_CU,
20209 which at this point doesn't seem justified: it's not clear how frequently
20210 it would get used and it would increase the size of every PER_CU.
20211 Entry points like dwarf2_per_cu_addr_size do a similar thing
20212 so we're not in uncharted territory here.
20213 Alas we need to be a bit more complicated as addr_base is contained
20214 in the DIE.
20215
20216 We don't need to read the entire CU(/TU).
20217 We just need the header and top level die.
20218
20219 IWBN to use the aging mechanism to let us lazily later discard the CU.
20220 For now we skip this optimization. */
20221
20222 if (cu != NULL)
20223 {
20224 addr_base = cu->addr_base;
20225 addr_size = cu->header.addr_size;
20226 }
20227 else
20228 {
20229 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20230 addr_base = reader.cu->addr_base;
20231 addr_size = reader.cu->header.addr_size;
20232 }
20233
20234 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20235 }
20236
20237 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20238 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20239 DWO file. */
20240
20241 static const char *
20242 read_str_index (struct dwarf2_cu *cu,
20243 struct dwarf2_section_info *str_section,
20244 struct dwarf2_section_info *str_offsets_section,
20245 ULONGEST str_offsets_base, ULONGEST str_index)
20246 {
20247 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20248 struct objfile *objfile = per_objfile->objfile;
20249 const char *objf_name = objfile_name (objfile);
20250 bfd *abfd = objfile->obfd;
20251 const gdb_byte *info_ptr;
20252 ULONGEST str_offset;
20253 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20254
20255 str_section->read (objfile);
20256 str_offsets_section->read (objfile);
20257 if (str_section->buffer == NULL)
20258 error (_("%s used without %s section"
20259 " in CU at offset %s [in module %s]"),
20260 form_name, str_section->get_name (),
20261 sect_offset_str (cu->header.sect_off), objf_name);
20262 if (str_offsets_section->buffer == NULL)
20263 error (_("%s used without %s section"
20264 " in CU at offset %s [in module %s]"),
20265 form_name, str_section->get_name (),
20266 sect_offset_str (cu->header.sect_off), objf_name);
20267 info_ptr = (str_offsets_section->buffer
20268 + str_offsets_base
20269 + str_index * cu->header.offset_size);
20270 if (cu->header.offset_size == 4)
20271 str_offset = bfd_get_32 (abfd, info_ptr);
20272 else
20273 str_offset = bfd_get_64 (abfd, info_ptr);
20274 if (str_offset >= str_section->size)
20275 error (_("Offset from %s pointing outside of"
20276 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20277 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20278 return (const char *) (str_section->buffer + str_offset);
20279 }
20280
20281 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20282
20283 static const char *
20284 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20285 {
20286 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20287 ? reader->cu->header.addr_size : 0;
20288 return read_str_index (reader->cu,
20289 &reader->dwo_file->sections.str,
20290 &reader->dwo_file->sections.str_offsets,
20291 str_offsets_base, str_index);
20292 }
20293
20294 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20295
20296 static const char *
20297 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20298 {
20299 struct objfile *objfile = cu->per_objfile->objfile;
20300 const char *objf_name = objfile_name (objfile);
20301 static const char form_name[] = "DW_FORM_GNU_str_index";
20302 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20303
20304 if (!cu->str_offsets_base.has_value ())
20305 error (_("%s used in Fission stub without %s"
20306 " in CU at offset 0x%lx [in module %s]"),
20307 form_name, str_offsets_attr_name,
20308 (long) cu->header.offset_size, objf_name);
20309
20310 return read_str_index (cu,
20311 &cu->per_objfile->per_bfd->str,
20312 &cu->per_objfile->per_bfd->str_offsets,
20313 *cu->str_offsets_base, str_index);
20314 }
20315
20316 /* Return the length of an LEB128 number in BUF. */
20317
20318 static int
20319 leb128_size (const gdb_byte *buf)
20320 {
20321 const gdb_byte *begin = buf;
20322 gdb_byte byte;
20323
20324 while (1)
20325 {
20326 byte = *buf++;
20327 if ((byte & 128) == 0)
20328 return buf - begin;
20329 }
20330 }
20331
20332 static void
20333 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20334 {
20335 switch (lang)
20336 {
20337 case DW_LANG_C89:
20338 case DW_LANG_C99:
20339 case DW_LANG_C11:
20340 case DW_LANG_C:
20341 case DW_LANG_UPC:
20342 cu->language = language_c;
20343 break;
20344 case DW_LANG_Java:
20345 case DW_LANG_C_plus_plus:
20346 case DW_LANG_C_plus_plus_11:
20347 case DW_LANG_C_plus_plus_14:
20348 cu->language = language_cplus;
20349 break;
20350 case DW_LANG_D:
20351 cu->language = language_d;
20352 break;
20353 case DW_LANG_Fortran77:
20354 case DW_LANG_Fortran90:
20355 case DW_LANG_Fortran95:
20356 case DW_LANG_Fortran03:
20357 case DW_LANG_Fortran08:
20358 cu->language = language_fortran;
20359 break;
20360 case DW_LANG_Go:
20361 cu->language = language_go;
20362 break;
20363 case DW_LANG_Mips_Assembler:
20364 cu->language = language_asm;
20365 break;
20366 case DW_LANG_Ada83:
20367 case DW_LANG_Ada95:
20368 cu->language = language_ada;
20369 break;
20370 case DW_LANG_Modula2:
20371 cu->language = language_m2;
20372 break;
20373 case DW_LANG_Pascal83:
20374 cu->language = language_pascal;
20375 break;
20376 case DW_LANG_ObjC:
20377 cu->language = language_objc;
20378 break;
20379 case DW_LANG_Rust:
20380 case DW_LANG_Rust_old:
20381 cu->language = language_rust;
20382 break;
20383 case DW_LANG_Cobol74:
20384 case DW_LANG_Cobol85:
20385 default:
20386 cu->language = language_minimal;
20387 break;
20388 }
20389 cu->language_defn = language_def (cu->language);
20390 }
20391
20392 /* Return the named attribute or NULL if not there. */
20393
20394 static struct attribute *
20395 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20396 {
20397 for (;;)
20398 {
20399 unsigned int i;
20400 struct attribute *spec = NULL;
20401
20402 for (i = 0; i < die->num_attrs; ++i)
20403 {
20404 if (die->attrs[i].name == name)
20405 return &die->attrs[i];
20406 if (die->attrs[i].name == DW_AT_specification
20407 || die->attrs[i].name == DW_AT_abstract_origin)
20408 spec = &die->attrs[i];
20409 }
20410
20411 if (!spec)
20412 break;
20413
20414 die = follow_die_ref (die, spec, &cu);
20415 }
20416
20417 return NULL;
20418 }
20419
20420 /* Return the string associated with a string-typed attribute, or NULL if it
20421 is either not found or is of an incorrect type. */
20422
20423 static const char *
20424 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20425 {
20426 struct attribute *attr;
20427 const char *str = NULL;
20428
20429 attr = dwarf2_attr (die, name, cu);
20430
20431 if (attr != NULL)
20432 {
20433 str = attr->as_string ();
20434 if (str == nullptr)
20435 complaint (_("string type expected for attribute %s for "
20436 "DIE at %s in module %s"),
20437 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20438 objfile_name (cu->per_objfile->objfile));
20439 }
20440
20441 return str;
20442 }
20443
20444 /* Return the dwo name or NULL if not present. If present, it is in either
20445 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20446 static const char *
20447 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20448 {
20449 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20450 if (dwo_name == nullptr)
20451 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20452 return dwo_name;
20453 }
20454
20455 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20456 and holds a non-zero value. This function should only be used for
20457 DW_FORM_flag or DW_FORM_flag_present attributes. */
20458
20459 static int
20460 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20461 {
20462 struct attribute *attr = dwarf2_attr (die, name, cu);
20463
20464 return attr != nullptr && attr->as_boolean ();
20465 }
20466
20467 static int
20468 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20469 {
20470 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20471 which value is non-zero. However, we have to be careful with
20472 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20473 (via dwarf2_flag_true_p) follows this attribute. So we may
20474 end up accidently finding a declaration attribute that belongs
20475 to a different DIE referenced by the specification attribute,
20476 even though the given DIE does not have a declaration attribute. */
20477 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20478 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20479 }
20480
20481 /* Return the die giving the specification for DIE, if there is
20482 one. *SPEC_CU is the CU containing DIE on input, and the CU
20483 containing the return value on output. If there is no
20484 specification, but there is an abstract origin, that is
20485 returned. */
20486
20487 static struct die_info *
20488 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20489 {
20490 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20491 *spec_cu);
20492
20493 if (spec_attr == NULL)
20494 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20495
20496 if (spec_attr == NULL)
20497 return NULL;
20498 else
20499 return follow_die_ref (die, spec_attr, spec_cu);
20500 }
20501
20502 /* A convenience function to find the proper .debug_line section for a CU. */
20503
20504 static struct dwarf2_section_info *
20505 get_debug_line_section (struct dwarf2_cu *cu)
20506 {
20507 struct dwarf2_section_info *section;
20508 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20509
20510 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20511 DWO file. */
20512 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20513 section = &cu->dwo_unit->dwo_file->sections.line;
20514 else if (cu->per_cu->is_dwz)
20515 {
20516 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20517
20518 section = &dwz->line;
20519 }
20520 else
20521 section = &per_objfile->per_bfd->line;
20522
20523 return section;
20524 }
20525
20526 /* Read the statement program header starting at OFFSET in
20527 .debug_line, or .debug_line.dwo. Return a pointer
20528 to a struct line_header, allocated using xmalloc.
20529 Returns NULL if there is a problem reading the header, e.g., if it
20530 has a version we don't understand.
20531
20532 NOTE: the strings in the include directory and file name tables of
20533 the returned object point into the dwarf line section buffer,
20534 and must not be freed. */
20535
20536 static line_header_up
20537 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20538 {
20539 struct dwarf2_section_info *section;
20540 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20541
20542 section = get_debug_line_section (cu);
20543 section->read (per_objfile->objfile);
20544 if (section->buffer == NULL)
20545 {
20546 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20547 complaint (_("missing .debug_line.dwo section"));
20548 else
20549 complaint (_("missing .debug_line section"));
20550 return 0;
20551 }
20552
20553 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20554 per_objfile, section, &cu->header);
20555 }
20556
20557 /* Subroutine of dwarf_decode_lines to simplify it.
20558 Return the file name of the psymtab for the given file_entry.
20559 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20560 If space for the result is malloc'd, *NAME_HOLDER will be set.
20561 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20562
20563 static const char *
20564 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20565 const dwarf2_psymtab *pst,
20566 const char *comp_dir,
20567 gdb::unique_xmalloc_ptr<char> *name_holder)
20568 {
20569 const char *include_name = fe.name;
20570 const char *include_name_to_compare = include_name;
20571 const char *pst_filename;
20572 int file_is_pst;
20573
20574 const char *dir_name = fe.include_dir (lh);
20575
20576 gdb::unique_xmalloc_ptr<char> hold_compare;
20577 if (!IS_ABSOLUTE_PATH (include_name)
20578 && (dir_name != NULL || comp_dir != NULL))
20579 {
20580 /* Avoid creating a duplicate psymtab for PST.
20581 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20582 Before we do the comparison, however, we need to account
20583 for DIR_NAME and COMP_DIR.
20584 First prepend dir_name (if non-NULL). If we still don't
20585 have an absolute path prepend comp_dir (if non-NULL).
20586 However, the directory we record in the include-file's
20587 psymtab does not contain COMP_DIR (to match the
20588 corresponding symtab(s)).
20589
20590 Example:
20591
20592 bash$ cd /tmp
20593 bash$ gcc -g ./hello.c
20594 include_name = "hello.c"
20595 dir_name = "."
20596 DW_AT_comp_dir = comp_dir = "/tmp"
20597 DW_AT_name = "./hello.c"
20598
20599 */
20600
20601 if (dir_name != NULL)
20602 {
20603 name_holder->reset (concat (dir_name, SLASH_STRING,
20604 include_name, (char *) NULL));
20605 include_name = name_holder->get ();
20606 include_name_to_compare = include_name;
20607 }
20608 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20609 {
20610 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20611 include_name, (char *) NULL));
20612 include_name_to_compare = hold_compare.get ();
20613 }
20614 }
20615
20616 pst_filename = pst->filename;
20617 gdb::unique_xmalloc_ptr<char> copied_name;
20618 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20619 {
20620 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20621 pst_filename, (char *) NULL));
20622 pst_filename = copied_name.get ();
20623 }
20624
20625 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20626
20627 if (file_is_pst)
20628 return NULL;
20629 return include_name;
20630 }
20631
20632 /* State machine to track the state of the line number program. */
20633
20634 class lnp_state_machine
20635 {
20636 public:
20637 /* Initialize a machine state for the start of a line number
20638 program. */
20639 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20640 bool record_lines_p);
20641
20642 file_entry *current_file ()
20643 {
20644 /* lh->file_names is 0-based, but the file name numbers in the
20645 statement program are 1-based. */
20646 return m_line_header->file_name_at (m_file);
20647 }
20648
20649 /* Record the line in the state machine. END_SEQUENCE is true if
20650 we're processing the end of a sequence. */
20651 void record_line (bool end_sequence);
20652
20653 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20654 nop-out rest of the lines in this sequence. */
20655 void check_line_address (struct dwarf2_cu *cu,
20656 const gdb_byte *line_ptr,
20657 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20658
20659 void handle_set_discriminator (unsigned int discriminator)
20660 {
20661 m_discriminator = discriminator;
20662 m_line_has_non_zero_discriminator |= discriminator != 0;
20663 }
20664
20665 /* Handle DW_LNE_set_address. */
20666 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20667 {
20668 m_op_index = 0;
20669 address += baseaddr;
20670 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20671 }
20672
20673 /* Handle DW_LNS_advance_pc. */
20674 void handle_advance_pc (CORE_ADDR adjust);
20675
20676 /* Handle a special opcode. */
20677 void handle_special_opcode (unsigned char op_code);
20678
20679 /* Handle DW_LNS_advance_line. */
20680 void handle_advance_line (int line_delta)
20681 {
20682 advance_line (line_delta);
20683 }
20684
20685 /* Handle DW_LNS_set_file. */
20686 void handle_set_file (file_name_index file);
20687
20688 /* Handle DW_LNS_negate_stmt. */
20689 void handle_negate_stmt ()
20690 {
20691 m_is_stmt = !m_is_stmt;
20692 }
20693
20694 /* Handle DW_LNS_const_add_pc. */
20695 void handle_const_add_pc ();
20696
20697 /* Handle DW_LNS_fixed_advance_pc. */
20698 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20699 {
20700 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20701 m_op_index = 0;
20702 }
20703
20704 /* Handle DW_LNS_copy. */
20705 void handle_copy ()
20706 {
20707 record_line (false);
20708 m_discriminator = 0;
20709 }
20710
20711 /* Handle DW_LNE_end_sequence. */
20712 void handle_end_sequence ()
20713 {
20714 m_currently_recording_lines = true;
20715 }
20716
20717 private:
20718 /* Advance the line by LINE_DELTA. */
20719 void advance_line (int line_delta)
20720 {
20721 m_line += line_delta;
20722
20723 if (line_delta != 0)
20724 m_line_has_non_zero_discriminator = m_discriminator != 0;
20725 }
20726
20727 struct dwarf2_cu *m_cu;
20728
20729 gdbarch *m_gdbarch;
20730
20731 /* True if we're recording lines.
20732 Otherwise we're building partial symtabs and are just interested in
20733 finding include files mentioned by the line number program. */
20734 bool m_record_lines_p;
20735
20736 /* The line number header. */
20737 line_header *m_line_header;
20738
20739 /* These are part of the standard DWARF line number state machine,
20740 and initialized according to the DWARF spec. */
20741
20742 unsigned char m_op_index = 0;
20743 /* The line table index of the current file. */
20744 file_name_index m_file = 1;
20745 unsigned int m_line = 1;
20746
20747 /* These are initialized in the constructor. */
20748
20749 CORE_ADDR m_address;
20750 bool m_is_stmt;
20751 unsigned int m_discriminator;
20752
20753 /* Additional bits of state we need to track. */
20754
20755 /* The last file that we called dwarf2_start_subfile for.
20756 This is only used for TLLs. */
20757 unsigned int m_last_file = 0;
20758 /* The last file a line number was recorded for. */
20759 struct subfile *m_last_subfile = NULL;
20760
20761 /* The address of the last line entry. */
20762 CORE_ADDR m_last_address;
20763
20764 /* Set to true when a previous line at the same address (using
20765 m_last_address) had m_is_stmt true. This is reset to false when a
20766 line entry at a new address (m_address different to m_last_address) is
20767 processed. */
20768 bool m_stmt_at_address = false;
20769
20770 /* When true, record the lines we decode. */
20771 bool m_currently_recording_lines = false;
20772
20773 /* The last line number that was recorded, used to coalesce
20774 consecutive entries for the same line. This can happen, for
20775 example, when discriminators are present. PR 17276. */
20776 unsigned int m_last_line = 0;
20777 bool m_line_has_non_zero_discriminator = false;
20778 };
20779
20780 void
20781 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20782 {
20783 CORE_ADDR addr_adj = (((m_op_index + adjust)
20784 / m_line_header->maximum_ops_per_instruction)
20785 * m_line_header->minimum_instruction_length);
20786 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20787 m_op_index = ((m_op_index + adjust)
20788 % m_line_header->maximum_ops_per_instruction);
20789 }
20790
20791 void
20792 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20793 {
20794 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20795 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20796 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20797 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20798 / m_line_header->maximum_ops_per_instruction)
20799 * m_line_header->minimum_instruction_length);
20800 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20801 m_op_index = ((m_op_index + adj_opcode_d)
20802 % m_line_header->maximum_ops_per_instruction);
20803
20804 int line_delta = m_line_header->line_base + adj_opcode_r;
20805 advance_line (line_delta);
20806 record_line (false);
20807 m_discriminator = 0;
20808 }
20809
20810 void
20811 lnp_state_machine::handle_set_file (file_name_index file)
20812 {
20813 m_file = file;
20814
20815 const file_entry *fe = current_file ();
20816 if (fe == NULL)
20817 dwarf2_debug_line_missing_file_complaint ();
20818 else if (m_record_lines_p)
20819 {
20820 const char *dir = fe->include_dir (m_line_header);
20821
20822 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20823 m_line_has_non_zero_discriminator = m_discriminator != 0;
20824 dwarf2_start_subfile (m_cu, fe->name, dir);
20825 }
20826 }
20827
20828 void
20829 lnp_state_machine::handle_const_add_pc ()
20830 {
20831 CORE_ADDR adjust
20832 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20833
20834 CORE_ADDR addr_adj
20835 = (((m_op_index + adjust)
20836 / m_line_header->maximum_ops_per_instruction)
20837 * m_line_header->minimum_instruction_length);
20838
20839 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20840 m_op_index = ((m_op_index + adjust)
20841 % m_line_header->maximum_ops_per_instruction);
20842 }
20843
20844 /* Return non-zero if we should add LINE to the line number table.
20845 LINE is the line to add, LAST_LINE is the last line that was added,
20846 LAST_SUBFILE is the subfile for LAST_LINE.
20847 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20848 had a non-zero discriminator.
20849
20850 We have to be careful in the presence of discriminators.
20851 E.g., for this line:
20852
20853 for (i = 0; i < 100000; i++);
20854
20855 clang can emit four line number entries for that one line,
20856 each with a different discriminator.
20857 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20858
20859 However, we want gdb to coalesce all four entries into one.
20860 Otherwise the user could stepi into the middle of the line and
20861 gdb would get confused about whether the pc really was in the
20862 middle of the line.
20863
20864 Things are further complicated by the fact that two consecutive
20865 line number entries for the same line is a heuristic used by gcc
20866 to denote the end of the prologue. So we can't just discard duplicate
20867 entries, we have to be selective about it. The heuristic we use is
20868 that we only collapse consecutive entries for the same line if at least
20869 one of those entries has a non-zero discriminator. PR 17276.
20870
20871 Note: Addresses in the line number state machine can never go backwards
20872 within one sequence, thus this coalescing is ok. */
20873
20874 static int
20875 dwarf_record_line_p (struct dwarf2_cu *cu,
20876 unsigned int line, unsigned int last_line,
20877 int line_has_non_zero_discriminator,
20878 struct subfile *last_subfile)
20879 {
20880 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20881 return 1;
20882 if (line != last_line)
20883 return 1;
20884 /* Same line for the same file that we've seen already.
20885 As a last check, for pr 17276, only record the line if the line
20886 has never had a non-zero discriminator. */
20887 if (!line_has_non_zero_discriminator)
20888 return 1;
20889 return 0;
20890 }
20891
20892 /* Use the CU's builder to record line number LINE beginning at
20893 address ADDRESS in the line table of subfile SUBFILE. */
20894
20895 static void
20896 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20897 unsigned int line, CORE_ADDR address, bool is_stmt,
20898 struct dwarf2_cu *cu)
20899 {
20900 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20901
20902 if (dwarf_line_debug)
20903 {
20904 fprintf_unfiltered (gdb_stdlog,
20905 "Recording line %u, file %s, address %s\n",
20906 line, lbasename (subfile->name),
20907 paddress (gdbarch, address));
20908 }
20909
20910 if (cu != nullptr)
20911 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20912 }
20913
20914 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20915 Mark the end of a set of line number records.
20916 The arguments are the same as for dwarf_record_line_1.
20917 If SUBFILE is NULL the request is ignored. */
20918
20919 static void
20920 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20921 CORE_ADDR address, struct dwarf2_cu *cu)
20922 {
20923 if (subfile == NULL)
20924 return;
20925
20926 if (dwarf_line_debug)
20927 {
20928 fprintf_unfiltered (gdb_stdlog,
20929 "Finishing current line, file %s, address %s\n",
20930 lbasename (subfile->name),
20931 paddress (gdbarch, address));
20932 }
20933
20934 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20935 }
20936
20937 void
20938 lnp_state_machine::record_line (bool end_sequence)
20939 {
20940 if (dwarf_line_debug)
20941 {
20942 fprintf_unfiltered (gdb_stdlog,
20943 "Processing actual line %u: file %u,"
20944 " address %s, is_stmt %u, discrim %u%s\n",
20945 m_line, m_file,
20946 paddress (m_gdbarch, m_address),
20947 m_is_stmt, m_discriminator,
20948 (end_sequence ? "\t(end sequence)" : ""));
20949 }
20950
20951 file_entry *fe = current_file ();
20952
20953 if (fe == NULL)
20954 dwarf2_debug_line_missing_file_complaint ();
20955 /* For now we ignore lines not starting on an instruction boundary.
20956 But not when processing end_sequence for compatibility with the
20957 previous version of the code. */
20958 else if (m_op_index == 0 || end_sequence)
20959 {
20960 fe->included_p = 1;
20961 if (m_record_lines_p)
20962 {
20963 /* When we switch files we insert an end maker in the first file,
20964 switch to the second file and add a new line entry. The
20965 problem is that the end marker inserted in the first file will
20966 discard any previous line entries at the same address. If the
20967 line entries in the first file are marked as is-stmt, while
20968 the new line in the second file is non-stmt, then this means
20969 the end marker will discard is-stmt lines so we can have a
20970 non-stmt line. This means that there are less addresses at
20971 which the user can insert a breakpoint.
20972
20973 To improve this we track the last address in m_last_address,
20974 and whether we have seen an is-stmt at this address. Then
20975 when switching files, if we have seen a stmt at the current
20976 address, and we are switching to create a non-stmt line, then
20977 discard the new line. */
20978 bool file_changed
20979 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20980 bool ignore_this_line
20981 = ((file_changed && !end_sequence && m_last_address == m_address
20982 && !m_is_stmt && m_stmt_at_address)
20983 || (!end_sequence && m_line == 0));
20984
20985 if ((file_changed && !ignore_this_line) || end_sequence)
20986 {
20987 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20988 m_currently_recording_lines ? m_cu : nullptr);
20989 }
20990
20991 if (!end_sequence && !ignore_this_line)
20992 {
20993 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20994
20995 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20996 m_line_has_non_zero_discriminator,
20997 m_last_subfile))
20998 {
20999 buildsym_compunit *builder = m_cu->get_builder ();
21000 dwarf_record_line_1 (m_gdbarch,
21001 builder->get_current_subfile (),
21002 m_line, m_address, is_stmt,
21003 m_currently_recording_lines ? m_cu : nullptr);
21004 }
21005 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21006 m_last_line = m_line;
21007 }
21008 }
21009 }
21010
21011 /* Track whether we have seen any m_is_stmt true at m_address in case we
21012 have multiple line table entries all at m_address. */
21013 if (m_last_address != m_address)
21014 {
21015 m_stmt_at_address = false;
21016 m_last_address = m_address;
21017 }
21018 m_stmt_at_address |= m_is_stmt;
21019 }
21020
21021 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21022 line_header *lh, bool record_lines_p)
21023 {
21024 m_cu = cu;
21025 m_gdbarch = arch;
21026 m_record_lines_p = record_lines_p;
21027 m_line_header = lh;
21028
21029 m_currently_recording_lines = true;
21030
21031 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21032 was a line entry for it so that the backend has a chance to adjust it
21033 and also record it in case it needs it. This is currently used by MIPS
21034 code, cf. `mips_adjust_dwarf2_line'. */
21035 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21036 m_is_stmt = lh->default_is_stmt;
21037 m_discriminator = 0;
21038
21039 m_last_address = m_address;
21040 m_stmt_at_address = false;
21041 }
21042
21043 void
21044 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21045 const gdb_byte *line_ptr,
21046 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21047 {
21048 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21049 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21050 located at 0x0. In this case, additionally check that if
21051 ADDRESS < UNRELOCATED_LOWPC. */
21052
21053 if ((address == 0 && address < unrelocated_lowpc)
21054 || address == (CORE_ADDR) -1)
21055 {
21056 /* This line table is for a function which has been
21057 GCd by the linker. Ignore it. PR gdb/12528 */
21058
21059 struct objfile *objfile = cu->per_objfile->objfile;
21060 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21061
21062 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21063 line_offset, objfile_name (objfile));
21064 m_currently_recording_lines = false;
21065 /* Note: m_currently_recording_lines is left as false until we see
21066 DW_LNE_end_sequence. */
21067 }
21068 }
21069
21070 /* Subroutine of dwarf_decode_lines to simplify it.
21071 Process the line number information in LH.
21072 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21073 program in order to set included_p for every referenced header. */
21074
21075 static void
21076 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21077 const int decode_for_pst_p, CORE_ADDR lowpc)
21078 {
21079 const gdb_byte *line_ptr, *extended_end;
21080 const gdb_byte *line_end;
21081 unsigned int bytes_read, extended_len;
21082 unsigned char op_code, extended_op;
21083 CORE_ADDR baseaddr;
21084 struct objfile *objfile = cu->per_objfile->objfile;
21085 bfd *abfd = objfile->obfd;
21086 struct gdbarch *gdbarch = objfile->arch ();
21087 /* True if we're recording line info (as opposed to building partial
21088 symtabs and just interested in finding include files mentioned by
21089 the line number program). */
21090 bool record_lines_p = !decode_for_pst_p;
21091
21092 baseaddr = objfile->text_section_offset ();
21093
21094 line_ptr = lh->statement_program_start;
21095 line_end = lh->statement_program_end;
21096
21097 /* Read the statement sequences until there's nothing left. */
21098 while (line_ptr < line_end)
21099 {
21100 /* The DWARF line number program state machine. Reset the state
21101 machine at the start of each sequence. */
21102 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21103 bool end_sequence = false;
21104
21105 if (record_lines_p)
21106 {
21107 /* Start a subfile for the current file of the state
21108 machine. */
21109 const file_entry *fe = state_machine.current_file ();
21110
21111 if (fe != NULL)
21112 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21113 }
21114
21115 /* Decode the table. */
21116 while (line_ptr < line_end && !end_sequence)
21117 {
21118 op_code = read_1_byte (abfd, line_ptr);
21119 line_ptr += 1;
21120
21121 if (op_code >= lh->opcode_base)
21122 {
21123 /* Special opcode. */
21124 state_machine.handle_special_opcode (op_code);
21125 }
21126 else switch (op_code)
21127 {
21128 case DW_LNS_extended_op:
21129 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21130 &bytes_read);
21131 line_ptr += bytes_read;
21132 extended_end = line_ptr + extended_len;
21133 extended_op = read_1_byte (abfd, line_ptr);
21134 line_ptr += 1;
21135 if (DW_LNE_lo_user <= extended_op
21136 && extended_op <= DW_LNE_hi_user)
21137 {
21138 /* Vendor extension, ignore. */
21139 line_ptr = extended_end;
21140 break;
21141 }
21142 switch (extended_op)
21143 {
21144 case DW_LNE_end_sequence:
21145 state_machine.handle_end_sequence ();
21146 end_sequence = true;
21147 break;
21148 case DW_LNE_set_address:
21149 {
21150 CORE_ADDR address
21151 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21152 line_ptr += bytes_read;
21153
21154 state_machine.check_line_address (cu, line_ptr,
21155 lowpc - baseaddr, address);
21156 state_machine.handle_set_address (baseaddr, address);
21157 }
21158 break;
21159 case DW_LNE_define_file:
21160 {
21161 const char *cur_file;
21162 unsigned int mod_time, length;
21163 dir_index dindex;
21164
21165 cur_file = read_direct_string (abfd, line_ptr,
21166 &bytes_read);
21167 line_ptr += bytes_read;
21168 dindex = (dir_index)
21169 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21170 line_ptr += bytes_read;
21171 mod_time =
21172 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21173 line_ptr += bytes_read;
21174 length =
21175 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21176 line_ptr += bytes_read;
21177 lh->add_file_name (cur_file, dindex, mod_time, length);
21178 }
21179 break;
21180 case DW_LNE_set_discriminator:
21181 {
21182 /* The discriminator is not interesting to the
21183 debugger; just ignore it. We still need to
21184 check its value though:
21185 if there are consecutive entries for the same
21186 (non-prologue) line we want to coalesce them.
21187 PR 17276. */
21188 unsigned int discr
21189 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21190 line_ptr += bytes_read;
21191
21192 state_machine.handle_set_discriminator (discr);
21193 }
21194 break;
21195 default:
21196 complaint (_("mangled .debug_line section"));
21197 return;
21198 }
21199 /* Make sure that we parsed the extended op correctly. If e.g.
21200 we expected a different address size than the producer used,
21201 we may have read the wrong number of bytes. */
21202 if (line_ptr != extended_end)
21203 {
21204 complaint (_("mangled .debug_line section"));
21205 return;
21206 }
21207 break;
21208 case DW_LNS_copy:
21209 state_machine.handle_copy ();
21210 break;
21211 case DW_LNS_advance_pc:
21212 {
21213 CORE_ADDR adjust
21214 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21215 line_ptr += bytes_read;
21216
21217 state_machine.handle_advance_pc (adjust);
21218 }
21219 break;
21220 case DW_LNS_advance_line:
21221 {
21222 int line_delta
21223 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21224 line_ptr += bytes_read;
21225
21226 state_machine.handle_advance_line (line_delta);
21227 }
21228 break;
21229 case DW_LNS_set_file:
21230 {
21231 file_name_index file
21232 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21233 &bytes_read);
21234 line_ptr += bytes_read;
21235
21236 state_machine.handle_set_file (file);
21237 }
21238 break;
21239 case DW_LNS_set_column:
21240 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21241 line_ptr += bytes_read;
21242 break;
21243 case DW_LNS_negate_stmt:
21244 state_machine.handle_negate_stmt ();
21245 break;
21246 case DW_LNS_set_basic_block:
21247 break;
21248 /* Add to the address register of the state machine the
21249 address increment value corresponding to special opcode
21250 255. I.e., this value is scaled by the minimum
21251 instruction length since special opcode 255 would have
21252 scaled the increment. */
21253 case DW_LNS_const_add_pc:
21254 state_machine.handle_const_add_pc ();
21255 break;
21256 case DW_LNS_fixed_advance_pc:
21257 {
21258 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21259 line_ptr += 2;
21260
21261 state_machine.handle_fixed_advance_pc (addr_adj);
21262 }
21263 break;
21264 default:
21265 {
21266 /* Unknown standard opcode, ignore it. */
21267 int i;
21268
21269 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21270 {
21271 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21272 line_ptr += bytes_read;
21273 }
21274 }
21275 }
21276 }
21277
21278 if (!end_sequence)
21279 dwarf2_debug_line_missing_end_sequence_complaint ();
21280
21281 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21282 in which case we still finish recording the last line). */
21283 state_machine.record_line (true);
21284 }
21285 }
21286
21287 /* Decode the Line Number Program (LNP) for the given line_header
21288 structure and CU. The actual information extracted and the type
21289 of structures created from the LNP depends on the value of PST.
21290
21291 1. If PST is NULL, then this procedure uses the data from the program
21292 to create all necessary symbol tables, and their linetables.
21293
21294 2. If PST is not NULL, this procedure reads the program to determine
21295 the list of files included by the unit represented by PST, and
21296 builds all the associated partial symbol tables.
21297
21298 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21299 It is used for relative paths in the line table.
21300 NOTE: When processing partial symtabs (pst != NULL),
21301 comp_dir == pst->dirname.
21302
21303 NOTE: It is important that psymtabs have the same file name (via strcmp)
21304 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21305 symtab we don't use it in the name of the psymtabs we create.
21306 E.g. expand_line_sal requires this when finding psymtabs to expand.
21307 A good testcase for this is mb-inline.exp.
21308
21309 LOWPC is the lowest address in CU (or 0 if not known).
21310
21311 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21312 for its PC<->lines mapping information. Otherwise only the filename
21313 table is read in. */
21314
21315 static void
21316 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21317 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21318 CORE_ADDR lowpc, int decode_mapping)
21319 {
21320 struct objfile *objfile = cu->per_objfile->objfile;
21321 const int decode_for_pst_p = (pst != NULL);
21322
21323 if (decode_mapping)
21324 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21325
21326 if (decode_for_pst_p)
21327 {
21328 /* Now that we're done scanning the Line Header Program, we can
21329 create the psymtab of each included file. */
21330 for (auto &file_entry : lh->file_names ())
21331 if (file_entry.included_p == 1)
21332 {
21333 gdb::unique_xmalloc_ptr<char> name_holder;
21334 const char *include_name =
21335 psymtab_include_file_name (lh, file_entry, pst,
21336 comp_dir, &name_holder);
21337 if (include_name != NULL)
21338 dwarf2_create_include_psymtab
21339 (cu->per_objfile->per_bfd, include_name, pst,
21340 cu->per_objfile->per_bfd->partial_symtabs.get (),
21341 objfile->per_bfd);
21342 }
21343 }
21344 else
21345 {
21346 /* Make sure a symtab is created for every file, even files
21347 which contain only variables (i.e. no code with associated
21348 line numbers). */
21349 buildsym_compunit *builder = cu->get_builder ();
21350 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21351
21352 for (auto &fe : lh->file_names ())
21353 {
21354 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21355 if (builder->get_current_subfile ()->symtab == NULL)
21356 {
21357 builder->get_current_subfile ()->symtab
21358 = allocate_symtab (cust,
21359 builder->get_current_subfile ()->name);
21360 }
21361 fe.symtab = builder->get_current_subfile ()->symtab;
21362 }
21363 }
21364 }
21365
21366 /* Start a subfile for DWARF. FILENAME is the name of the file and
21367 DIRNAME the name of the source directory which contains FILENAME
21368 or NULL if not known.
21369 This routine tries to keep line numbers from identical absolute and
21370 relative file names in a common subfile.
21371
21372 Using the `list' example from the GDB testsuite, which resides in
21373 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21374 of /srcdir/list0.c yields the following debugging information for list0.c:
21375
21376 DW_AT_name: /srcdir/list0.c
21377 DW_AT_comp_dir: /compdir
21378 files.files[0].name: list0.h
21379 files.files[0].dir: /srcdir
21380 files.files[1].name: list0.c
21381 files.files[1].dir: /srcdir
21382
21383 The line number information for list0.c has to end up in a single
21384 subfile, so that `break /srcdir/list0.c:1' works as expected.
21385 start_subfile will ensure that this happens provided that we pass the
21386 concatenation of files.files[1].dir and files.files[1].name as the
21387 subfile's name. */
21388
21389 static void
21390 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21391 const char *dirname)
21392 {
21393 gdb::unique_xmalloc_ptr<char> copy;
21394
21395 /* In order not to lose the line information directory,
21396 we concatenate it to the filename when it makes sense.
21397 Note that the Dwarf3 standard says (speaking of filenames in line
21398 information): ``The directory index is ignored for file names
21399 that represent full path names''. Thus ignoring dirname in the
21400 `else' branch below isn't an issue. */
21401
21402 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21403 {
21404 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21405 filename = copy.get ();
21406 }
21407
21408 cu->get_builder ()->start_subfile (filename);
21409 }
21410
21411 static void
21412 var_decode_location (struct attribute *attr, struct symbol *sym,
21413 struct dwarf2_cu *cu)
21414 {
21415 struct objfile *objfile = cu->per_objfile->objfile;
21416 struct comp_unit_head *cu_header = &cu->header;
21417
21418 /* NOTE drow/2003-01-30: There used to be a comment and some special
21419 code here to turn a symbol with DW_AT_external and a
21420 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21421 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21422 with some versions of binutils) where shared libraries could have
21423 relocations against symbols in their debug information - the
21424 minimal symbol would have the right address, but the debug info
21425 would not. It's no longer necessary, because we will explicitly
21426 apply relocations when we read in the debug information now. */
21427
21428 /* A DW_AT_location attribute with no contents indicates that a
21429 variable has been optimized away. */
21430 if (attr->form_is_block () && attr->as_block ()->size == 0)
21431 {
21432 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21433 return;
21434 }
21435
21436 /* Handle one degenerate form of location expression specially, to
21437 preserve GDB's previous behavior when section offsets are
21438 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21439 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21440
21441 if (attr->form_is_block ())
21442 {
21443 struct dwarf_block *block = attr->as_block ();
21444
21445 if ((block->data[0] == DW_OP_addr
21446 && block->size == 1 + cu_header->addr_size)
21447 || ((block->data[0] == DW_OP_GNU_addr_index
21448 || block->data[0] == DW_OP_addrx)
21449 && (block->size
21450 == 1 + leb128_size (&block->data[1]))))
21451 {
21452 unsigned int dummy;
21453
21454 if (block->data[0] == DW_OP_addr)
21455 SET_SYMBOL_VALUE_ADDRESS
21456 (sym, cu->header.read_address (objfile->obfd,
21457 block->data + 1,
21458 &dummy));
21459 else
21460 SET_SYMBOL_VALUE_ADDRESS
21461 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21462 &dummy));
21463 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21464 fixup_symbol_section (sym, objfile);
21465 SET_SYMBOL_VALUE_ADDRESS
21466 (sym,
21467 SYMBOL_VALUE_ADDRESS (sym)
21468 + objfile->section_offsets[sym->section_index ()]);
21469 return;
21470 }
21471 }
21472
21473 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21474 expression evaluator, and use LOC_COMPUTED only when necessary
21475 (i.e. when the value of a register or memory location is
21476 referenced, or a thread-local block, etc.). Then again, it might
21477 not be worthwhile. I'm assuming that it isn't unless performance
21478 or memory numbers show me otherwise. */
21479
21480 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21481
21482 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21483 cu->has_loclist = true;
21484 }
21485
21486 /* Given a pointer to a DWARF information entry, figure out if we need
21487 to make a symbol table entry for it, and if so, create a new entry
21488 and return a pointer to it.
21489 If TYPE is NULL, determine symbol type from the die, otherwise
21490 used the passed type.
21491 If SPACE is not NULL, use it to hold the new symbol. If it is
21492 NULL, allocate a new symbol on the objfile's obstack. */
21493
21494 static struct symbol *
21495 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21496 struct symbol *space)
21497 {
21498 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21499 struct objfile *objfile = per_objfile->objfile;
21500 struct gdbarch *gdbarch = objfile->arch ();
21501 struct symbol *sym = NULL;
21502 const char *name;
21503 struct attribute *attr = NULL;
21504 struct attribute *attr2 = NULL;
21505 CORE_ADDR baseaddr;
21506 struct pending **list_to_add = NULL;
21507
21508 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21509
21510 baseaddr = objfile->text_section_offset ();
21511
21512 name = dwarf2_name (die, cu);
21513 if (name)
21514 {
21515 int suppress_add = 0;
21516
21517 if (space)
21518 sym = space;
21519 else
21520 sym = new (&objfile->objfile_obstack) symbol;
21521 OBJSTAT (objfile, n_syms++);
21522
21523 /* Cache this symbol's name and the name's demangled form (if any). */
21524 sym->set_language (cu->language, &objfile->objfile_obstack);
21525 /* Fortran does not have mangling standard and the mangling does differ
21526 between gfortran, iFort etc. */
21527 const char *physname
21528 = (cu->language == language_fortran
21529 ? dwarf2_full_name (name, die, cu)
21530 : dwarf2_physname (name, die, cu));
21531 const char *linkagename = dw2_linkage_name (die, cu);
21532
21533 if (linkagename == nullptr || cu->language == language_ada)
21534 sym->set_linkage_name (physname);
21535 else
21536 {
21537 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21538 sym->set_linkage_name (linkagename);
21539 }
21540
21541 /* Default assumptions.
21542 Use the passed type or decode it from the die. */
21543 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21544 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21545 if (type != NULL)
21546 SYMBOL_TYPE (sym) = type;
21547 else
21548 SYMBOL_TYPE (sym) = die_type (die, cu);
21549 attr = dwarf2_attr (die,
21550 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21551 cu);
21552 if (attr != nullptr)
21553 SYMBOL_LINE (sym) = attr->constant_value (0);
21554
21555 attr = dwarf2_attr (die,
21556 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21557 cu);
21558 if (attr != nullptr && attr->is_nonnegative ())
21559 {
21560 file_name_index file_index
21561 = (file_name_index) attr->as_nonnegative ();
21562 struct file_entry *fe;
21563
21564 if (cu->line_header != NULL)
21565 fe = cu->line_header->file_name_at (file_index);
21566 else
21567 fe = NULL;
21568
21569 if (fe == NULL)
21570 complaint (_("file index out of range"));
21571 else
21572 symbol_set_symtab (sym, fe->symtab);
21573 }
21574
21575 switch (die->tag)
21576 {
21577 case DW_TAG_label:
21578 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21579 if (attr != nullptr)
21580 {
21581 CORE_ADDR addr;
21582
21583 addr = attr->as_address ();
21584 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21585 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21586 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21587 }
21588 else
21589 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21590 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21591 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21592 add_symbol_to_list (sym, cu->list_in_scope);
21593 break;
21594 case DW_TAG_subprogram:
21595 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21596 finish_block. */
21597 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21598 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21599 if ((attr2 != nullptr && attr2->as_boolean ())
21600 || cu->language == language_ada
21601 || cu->language == language_fortran)
21602 {
21603 /* Subprograms marked external are stored as a global symbol.
21604 Ada and Fortran subprograms, whether marked external or
21605 not, are always stored as a global symbol, because we want
21606 to be able to access them globally. For instance, we want
21607 to be able to break on a nested subprogram without having
21608 to specify the context. */
21609 list_to_add = cu->get_builder ()->get_global_symbols ();
21610 }
21611 else
21612 {
21613 list_to_add = cu->list_in_scope;
21614 }
21615 break;
21616 case DW_TAG_inlined_subroutine:
21617 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21618 finish_block. */
21619 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21620 SYMBOL_INLINED (sym) = 1;
21621 list_to_add = cu->list_in_scope;
21622 break;
21623 case DW_TAG_template_value_param:
21624 suppress_add = 1;
21625 /* Fall through. */
21626 case DW_TAG_constant:
21627 case DW_TAG_variable:
21628 case DW_TAG_member:
21629 /* Compilation with minimal debug info may result in
21630 variables with missing type entries. Change the
21631 misleading `void' type to something sensible. */
21632 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21633 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21634
21635 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21636 /* In the case of DW_TAG_member, we should only be called for
21637 static const members. */
21638 if (die->tag == DW_TAG_member)
21639 {
21640 /* dwarf2_add_field uses die_is_declaration,
21641 so we do the same. */
21642 gdb_assert (die_is_declaration (die, cu));
21643 gdb_assert (attr);
21644 }
21645 if (attr != nullptr)
21646 {
21647 dwarf2_const_value (attr, sym, cu);
21648 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21649 if (!suppress_add)
21650 {
21651 if (attr2 != nullptr && attr2->as_boolean ())
21652 list_to_add = cu->get_builder ()->get_global_symbols ();
21653 else
21654 list_to_add = cu->list_in_scope;
21655 }
21656 break;
21657 }
21658 attr = dwarf2_attr (die, DW_AT_location, cu);
21659 if (attr != nullptr)
21660 {
21661 var_decode_location (attr, sym, cu);
21662 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21663
21664 /* Fortran explicitly imports any global symbols to the local
21665 scope by DW_TAG_common_block. */
21666 if (cu->language == language_fortran && die->parent
21667 && die->parent->tag == DW_TAG_common_block)
21668 attr2 = NULL;
21669
21670 if (SYMBOL_CLASS (sym) == LOC_STATIC
21671 && SYMBOL_VALUE_ADDRESS (sym) == 0
21672 && !per_objfile->per_bfd->has_section_at_zero)
21673 {
21674 /* When a static variable is eliminated by the linker,
21675 the corresponding debug information is not stripped
21676 out, but the variable address is set to null;
21677 do not add such variables into symbol table. */
21678 }
21679 else if (attr2 != nullptr && attr2->as_boolean ())
21680 {
21681 if (SYMBOL_CLASS (sym) == LOC_STATIC
21682 && (objfile->flags & OBJF_MAINLINE) == 0
21683 && per_objfile->per_bfd->can_copy)
21684 {
21685 /* A global static variable might be subject to
21686 copy relocation. We first check for a local
21687 minsym, though, because maybe the symbol was
21688 marked hidden, in which case this would not
21689 apply. */
21690 bound_minimal_symbol found
21691 = (lookup_minimal_symbol_linkage
21692 (sym->linkage_name (), objfile));
21693 if (found.minsym != nullptr)
21694 sym->maybe_copied = 1;
21695 }
21696
21697 /* A variable with DW_AT_external is never static,
21698 but it may be block-scoped. */
21699 list_to_add
21700 = ((cu->list_in_scope
21701 == cu->get_builder ()->get_file_symbols ())
21702 ? cu->get_builder ()->get_global_symbols ()
21703 : cu->list_in_scope);
21704 }
21705 else
21706 list_to_add = cu->list_in_scope;
21707 }
21708 else
21709 {
21710 /* We do not know the address of this symbol.
21711 If it is an external symbol and we have type information
21712 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21713 The address of the variable will then be determined from
21714 the minimal symbol table whenever the variable is
21715 referenced. */
21716 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21717
21718 /* Fortran explicitly imports any global symbols to the local
21719 scope by DW_TAG_common_block. */
21720 if (cu->language == language_fortran && die->parent
21721 && die->parent->tag == DW_TAG_common_block)
21722 {
21723 /* SYMBOL_CLASS doesn't matter here because
21724 read_common_block is going to reset it. */
21725 if (!suppress_add)
21726 list_to_add = cu->list_in_scope;
21727 }
21728 else if (attr2 != nullptr && attr2->as_boolean ()
21729 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21730 {
21731 /* A variable with DW_AT_external is never static, but it
21732 may be block-scoped. */
21733 list_to_add
21734 = ((cu->list_in_scope
21735 == cu->get_builder ()->get_file_symbols ())
21736 ? cu->get_builder ()->get_global_symbols ()
21737 : cu->list_in_scope);
21738
21739 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21740 }
21741 else if (!die_is_declaration (die, cu))
21742 {
21743 /* Use the default LOC_OPTIMIZED_OUT class. */
21744 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21745 if (!suppress_add)
21746 list_to_add = cu->list_in_scope;
21747 }
21748 }
21749 break;
21750 case DW_TAG_formal_parameter:
21751 {
21752 /* If we are inside a function, mark this as an argument. If
21753 not, we might be looking at an argument to an inlined function
21754 when we do not have enough information to show inlined frames;
21755 pretend it's a local variable in that case so that the user can
21756 still see it. */
21757 struct context_stack *curr
21758 = cu->get_builder ()->get_current_context_stack ();
21759 if (curr != nullptr && curr->name != nullptr)
21760 SYMBOL_IS_ARGUMENT (sym) = 1;
21761 attr = dwarf2_attr (die, DW_AT_location, cu);
21762 if (attr != nullptr)
21763 {
21764 var_decode_location (attr, sym, cu);
21765 }
21766 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21767 if (attr != nullptr)
21768 {
21769 dwarf2_const_value (attr, sym, cu);
21770 }
21771
21772 list_to_add = cu->list_in_scope;
21773 }
21774 break;
21775 case DW_TAG_unspecified_parameters:
21776 /* From varargs functions; gdb doesn't seem to have any
21777 interest in this information, so just ignore it for now.
21778 (FIXME?) */
21779 break;
21780 case DW_TAG_template_type_param:
21781 suppress_add = 1;
21782 /* Fall through. */
21783 case DW_TAG_class_type:
21784 case DW_TAG_interface_type:
21785 case DW_TAG_structure_type:
21786 case DW_TAG_union_type:
21787 case DW_TAG_set_type:
21788 case DW_TAG_enumeration_type:
21789 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21790 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21791
21792 {
21793 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21794 really ever be static objects: otherwise, if you try
21795 to, say, break of a class's method and you're in a file
21796 which doesn't mention that class, it won't work unless
21797 the check for all static symbols in lookup_symbol_aux
21798 saves you. See the OtherFileClass tests in
21799 gdb.c++/namespace.exp. */
21800
21801 if (!suppress_add)
21802 {
21803 buildsym_compunit *builder = cu->get_builder ();
21804 list_to_add
21805 = (cu->list_in_scope == builder->get_file_symbols ()
21806 && cu->language == language_cplus
21807 ? builder->get_global_symbols ()
21808 : cu->list_in_scope);
21809
21810 /* The semantics of C++ state that "struct foo {
21811 ... }" also defines a typedef for "foo". */
21812 if (cu->language == language_cplus
21813 || cu->language == language_ada
21814 || cu->language == language_d
21815 || cu->language == language_rust)
21816 {
21817 /* The symbol's name is already allocated along
21818 with this objfile, so we don't need to
21819 duplicate it for the type. */
21820 if (SYMBOL_TYPE (sym)->name () == 0)
21821 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21822 }
21823 }
21824 }
21825 break;
21826 case DW_TAG_typedef:
21827 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21828 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21829 list_to_add = cu->list_in_scope;
21830 break;
21831 case DW_TAG_array_type:
21832 case DW_TAG_base_type:
21833 case DW_TAG_subrange_type:
21834 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21835 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21836 list_to_add = cu->list_in_scope;
21837 break;
21838 case DW_TAG_enumerator:
21839 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21840 if (attr != nullptr)
21841 {
21842 dwarf2_const_value (attr, sym, cu);
21843 }
21844 {
21845 /* NOTE: carlton/2003-11-10: See comment above in the
21846 DW_TAG_class_type, etc. block. */
21847
21848 list_to_add
21849 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21850 && cu->language == language_cplus
21851 ? cu->get_builder ()->get_global_symbols ()
21852 : cu->list_in_scope);
21853 }
21854 break;
21855 case DW_TAG_imported_declaration:
21856 case DW_TAG_namespace:
21857 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21858 list_to_add = cu->get_builder ()->get_global_symbols ();
21859 break;
21860 case DW_TAG_module:
21861 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21862 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21863 list_to_add = cu->get_builder ()->get_global_symbols ();
21864 break;
21865 case DW_TAG_common_block:
21866 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21867 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21868 add_symbol_to_list (sym, cu->list_in_scope);
21869 break;
21870 default:
21871 /* Not a tag we recognize. Hopefully we aren't processing
21872 trash data, but since we must specifically ignore things
21873 we don't recognize, there is nothing else we should do at
21874 this point. */
21875 complaint (_("unsupported tag: '%s'"),
21876 dwarf_tag_name (die->tag));
21877 break;
21878 }
21879
21880 if (suppress_add)
21881 {
21882 sym->hash_next = objfile->template_symbols;
21883 objfile->template_symbols = sym;
21884 list_to_add = NULL;
21885 }
21886
21887 if (list_to_add != NULL)
21888 add_symbol_to_list (sym, list_to_add);
21889
21890 /* For the benefit of old versions of GCC, check for anonymous
21891 namespaces based on the demangled name. */
21892 if (!cu->processing_has_namespace_info
21893 && cu->language == language_cplus)
21894 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21895 }
21896 return (sym);
21897 }
21898
21899 /* Given an attr with a DW_FORM_dataN value in host byte order,
21900 zero-extend it as appropriate for the symbol's type. The DWARF
21901 standard (v4) is not entirely clear about the meaning of using
21902 DW_FORM_dataN for a constant with a signed type, where the type is
21903 wider than the data. The conclusion of a discussion on the DWARF
21904 list was that this is unspecified. We choose to always zero-extend
21905 because that is the interpretation long in use by GCC. */
21906
21907 static gdb_byte *
21908 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21909 struct dwarf2_cu *cu, LONGEST *value, int bits)
21910 {
21911 struct objfile *objfile = cu->per_objfile->objfile;
21912 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21913 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21914 LONGEST l = attr->constant_value (0);
21915
21916 if (bits < sizeof (*value) * 8)
21917 {
21918 l &= ((LONGEST) 1 << bits) - 1;
21919 *value = l;
21920 }
21921 else if (bits == sizeof (*value) * 8)
21922 *value = l;
21923 else
21924 {
21925 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21926 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21927 return bytes;
21928 }
21929
21930 return NULL;
21931 }
21932
21933 /* Read a constant value from an attribute. Either set *VALUE, or if
21934 the value does not fit in *VALUE, set *BYTES - either already
21935 allocated on the objfile obstack, or newly allocated on OBSTACK,
21936 or, set *BATON, if we translated the constant to a location
21937 expression. */
21938
21939 static void
21940 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21941 const char *name, struct obstack *obstack,
21942 struct dwarf2_cu *cu,
21943 LONGEST *value, const gdb_byte **bytes,
21944 struct dwarf2_locexpr_baton **baton)
21945 {
21946 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21947 struct objfile *objfile = per_objfile->objfile;
21948 struct comp_unit_head *cu_header = &cu->header;
21949 struct dwarf_block *blk;
21950 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21951 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21952
21953 *value = 0;
21954 *bytes = NULL;
21955 *baton = NULL;
21956
21957 switch (attr->form)
21958 {
21959 case DW_FORM_addr:
21960 case DW_FORM_addrx:
21961 case DW_FORM_GNU_addr_index:
21962 {
21963 gdb_byte *data;
21964
21965 if (TYPE_LENGTH (type) != cu_header->addr_size)
21966 dwarf2_const_value_length_mismatch_complaint (name,
21967 cu_header->addr_size,
21968 TYPE_LENGTH (type));
21969 /* Symbols of this form are reasonably rare, so we just
21970 piggyback on the existing location code rather than writing
21971 a new implementation of symbol_computed_ops. */
21972 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21973 (*baton)->per_objfile = per_objfile;
21974 (*baton)->per_cu = cu->per_cu;
21975 gdb_assert ((*baton)->per_cu);
21976
21977 (*baton)->size = 2 + cu_header->addr_size;
21978 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21979 (*baton)->data = data;
21980
21981 data[0] = DW_OP_addr;
21982 store_unsigned_integer (&data[1], cu_header->addr_size,
21983 byte_order, attr->as_address ());
21984 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21985 }
21986 break;
21987 case DW_FORM_string:
21988 case DW_FORM_strp:
21989 case DW_FORM_strx:
21990 case DW_FORM_GNU_str_index:
21991 case DW_FORM_GNU_strp_alt:
21992 /* The string is already allocated on the objfile obstack, point
21993 directly to it. */
21994 *bytes = (const gdb_byte *) attr->as_string ();
21995 break;
21996 case DW_FORM_block1:
21997 case DW_FORM_block2:
21998 case DW_FORM_block4:
21999 case DW_FORM_block:
22000 case DW_FORM_exprloc:
22001 case DW_FORM_data16:
22002 blk = attr->as_block ();
22003 if (TYPE_LENGTH (type) != blk->size)
22004 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22005 TYPE_LENGTH (type));
22006 *bytes = blk->data;
22007 break;
22008
22009 /* The DW_AT_const_value attributes are supposed to carry the
22010 symbol's value "represented as it would be on the target
22011 architecture." By the time we get here, it's already been
22012 converted to host endianness, so we just need to sign- or
22013 zero-extend it as appropriate. */
22014 case DW_FORM_data1:
22015 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22016 break;
22017 case DW_FORM_data2:
22018 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22019 break;
22020 case DW_FORM_data4:
22021 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22022 break;
22023 case DW_FORM_data8:
22024 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22025 break;
22026
22027 case DW_FORM_sdata:
22028 case DW_FORM_implicit_const:
22029 *value = attr->as_signed ();
22030 break;
22031
22032 case DW_FORM_udata:
22033 *value = attr->as_unsigned ();
22034 break;
22035
22036 default:
22037 complaint (_("unsupported const value attribute form: '%s'"),
22038 dwarf_form_name (attr->form));
22039 *value = 0;
22040 break;
22041 }
22042 }
22043
22044
22045 /* Copy constant value from an attribute to a symbol. */
22046
22047 static void
22048 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22049 struct dwarf2_cu *cu)
22050 {
22051 struct objfile *objfile = cu->per_objfile->objfile;
22052 LONGEST value;
22053 const gdb_byte *bytes;
22054 struct dwarf2_locexpr_baton *baton;
22055
22056 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22057 sym->print_name (),
22058 &objfile->objfile_obstack, cu,
22059 &value, &bytes, &baton);
22060
22061 if (baton != NULL)
22062 {
22063 SYMBOL_LOCATION_BATON (sym) = baton;
22064 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22065 }
22066 else if (bytes != NULL)
22067 {
22068 SYMBOL_VALUE_BYTES (sym) = bytes;
22069 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22070 }
22071 else
22072 {
22073 SYMBOL_VALUE (sym) = value;
22074 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22075 }
22076 }
22077
22078 /* Return the type of the die in question using its DW_AT_type attribute. */
22079
22080 static struct type *
22081 die_type (struct die_info *die, struct dwarf2_cu *cu)
22082 {
22083 struct attribute *type_attr;
22084
22085 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22086 if (!type_attr)
22087 {
22088 struct objfile *objfile = cu->per_objfile->objfile;
22089 /* A missing DW_AT_type represents a void type. */
22090 return objfile_type (objfile)->builtin_void;
22091 }
22092
22093 return lookup_die_type (die, type_attr, cu);
22094 }
22095
22096 /* True iff CU's producer generates GNAT Ada auxiliary information
22097 that allows to find parallel types through that information instead
22098 of having to do expensive parallel lookups by type name. */
22099
22100 static int
22101 need_gnat_info (struct dwarf2_cu *cu)
22102 {
22103 /* Assume that the Ada compiler was GNAT, which always produces
22104 the auxiliary information. */
22105 return (cu->language == language_ada);
22106 }
22107
22108 /* Return the auxiliary type of the die in question using its
22109 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22110 attribute is not present. */
22111
22112 static struct type *
22113 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22114 {
22115 struct attribute *type_attr;
22116
22117 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22118 if (!type_attr)
22119 return NULL;
22120
22121 return lookup_die_type (die, type_attr, cu);
22122 }
22123
22124 /* If DIE has a descriptive_type attribute, then set the TYPE's
22125 descriptive type accordingly. */
22126
22127 static void
22128 set_descriptive_type (struct type *type, struct die_info *die,
22129 struct dwarf2_cu *cu)
22130 {
22131 struct type *descriptive_type = die_descriptive_type (die, cu);
22132
22133 if (descriptive_type)
22134 {
22135 ALLOCATE_GNAT_AUX_TYPE (type);
22136 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22137 }
22138 }
22139
22140 /* Return the containing type of the die in question using its
22141 DW_AT_containing_type attribute. */
22142
22143 static struct type *
22144 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22145 {
22146 struct attribute *type_attr;
22147 struct objfile *objfile = cu->per_objfile->objfile;
22148
22149 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22150 if (!type_attr)
22151 error (_("Dwarf Error: Problem turning containing type into gdb type "
22152 "[in module %s]"), objfile_name (objfile));
22153
22154 return lookup_die_type (die, type_attr, cu);
22155 }
22156
22157 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22158
22159 static struct type *
22160 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22161 {
22162 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22163 struct objfile *objfile = per_objfile->objfile;
22164 char *saved;
22165
22166 std::string message
22167 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22168 objfile_name (objfile),
22169 sect_offset_str (cu->header.sect_off),
22170 sect_offset_str (die->sect_off));
22171 saved = obstack_strdup (&objfile->objfile_obstack, message);
22172
22173 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22174 }
22175
22176 /* Look up the type of DIE in CU using its type attribute ATTR.
22177 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22178 DW_AT_containing_type.
22179 If there is no type substitute an error marker. */
22180
22181 static struct type *
22182 lookup_die_type (struct die_info *die, const struct attribute *attr,
22183 struct dwarf2_cu *cu)
22184 {
22185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22186 struct objfile *objfile = per_objfile->objfile;
22187 struct type *this_type;
22188
22189 gdb_assert (attr->name == DW_AT_type
22190 || attr->name == DW_AT_GNAT_descriptive_type
22191 || attr->name == DW_AT_containing_type);
22192
22193 /* First see if we have it cached. */
22194
22195 if (attr->form == DW_FORM_GNU_ref_alt)
22196 {
22197 struct dwarf2_per_cu_data *per_cu;
22198 sect_offset sect_off = attr->get_ref_die_offset ();
22199
22200 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22201 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22202 }
22203 else if (attr->form_is_ref ())
22204 {
22205 sect_offset sect_off = attr->get_ref_die_offset ();
22206
22207 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22208 }
22209 else if (attr->form == DW_FORM_ref_sig8)
22210 {
22211 ULONGEST signature = attr->as_signature ();
22212
22213 return get_signatured_type (die, signature, cu);
22214 }
22215 else
22216 {
22217 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22218 " at %s [in module %s]"),
22219 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22220 objfile_name (objfile));
22221 return build_error_marker_type (cu, die);
22222 }
22223
22224 /* If not cached we need to read it in. */
22225
22226 if (this_type == NULL)
22227 {
22228 struct die_info *type_die = NULL;
22229 struct dwarf2_cu *type_cu = cu;
22230
22231 if (attr->form_is_ref ())
22232 type_die = follow_die_ref (die, attr, &type_cu);
22233 if (type_die == NULL)
22234 return build_error_marker_type (cu, die);
22235 /* If we find the type now, it's probably because the type came
22236 from an inter-CU reference and the type's CU got expanded before
22237 ours. */
22238 this_type = read_type_die (type_die, type_cu);
22239 }
22240
22241 /* If we still don't have a type use an error marker. */
22242
22243 if (this_type == NULL)
22244 return build_error_marker_type (cu, die);
22245
22246 return this_type;
22247 }
22248
22249 /* Return the type in DIE, CU.
22250 Returns NULL for invalid types.
22251
22252 This first does a lookup in die_type_hash,
22253 and only reads the die in if necessary.
22254
22255 NOTE: This can be called when reading in partial or full symbols. */
22256
22257 static struct type *
22258 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22259 {
22260 struct type *this_type;
22261
22262 this_type = get_die_type (die, cu);
22263 if (this_type)
22264 return this_type;
22265
22266 return read_type_die_1 (die, cu);
22267 }
22268
22269 /* Read the type in DIE, CU.
22270 Returns NULL for invalid types. */
22271
22272 static struct type *
22273 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22274 {
22275 struct type *this_type = NULL;
22276
22277 switch (die->tag)
22278 {
22279 case DW_TAG_class_type:
22280 case DW_TAG_interface_type:
22281 case DW_TAG_structure_type:
22282 case DW_TAG_union_type:
22283 this_type = read_structure_type (die, cu);
22284 break;
22285 case DW_TAG_enumeration_type:
22286 this_type = read_enumeration_type (die, cu);
22287 break;
22288 case DW_TAG_subprogram:
22289 case DW_TAG_subroutine_type:
22290 case DW_TAG_inlined_subroutine:
22291 this_type = read_subroutine_type (die, cu);
22292 break;
22293 case DW_TAG_array_type:
22294 this_type = read_array_type (die, cu);
22295 break;
22296 case DW_TAG_set_type:
22297 this_type = read_set_type (die, cu);
22298 break;
22299 case DW_TAG_pointer_type:
22300 this_type = read_tag_pointer_type (die, cu);
22301 break;
22302 case DW_TAG_ptr_to_member_type:
22303 this_type = read_tag_ptr_to_member_type (die, cu);
22304 break;
22305 case DW_TAG_reference_type:
22306 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22307 break;
22308 case DW_TAG_rvalue_reference_type:
22309 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22310 break;
22311 case DW_TAG_const_type:
22312 this_type = read_tag_const_type (die, cu);
22313 break;
22314 case DW_TAG_volatile_type:
22315 this_type = read_tag_volatile_type (die, cu);
22316 break;
22317 case DW_TAG_restrict_type:
22318 this_type = read_tag_restrict_type (die, cu);
22319 break;
22320 case DW_TAG_string_type:
22321 this_type = read_tag_string_type (die, cu);
22322 break;
22323 case DW_TAG_typedef:
22324 this_type = read_typedef (die, cu);
22325 break;
22326 case DW_TAG_subrange_type:
22327 this_type = read_subrange_type (die, cu);
22328 break;
22329 case DW_TAG_base_type:
22330 this_type = read_base_type (die, cu);
22331 break;
22332 case DW_TAG_unspecified_type:
22333 this_type = read_unspecified_type (die, cu);
22334 break;
22335 case DW_TAG_namespace:
22336 this_type = read_namespace_type (die, cu);
22337 break;
22338 case DW_TAG_module:
22339 this_type = read_module_type (die, cu);
22340 break;
22341 case DW_TAG_atomic_type:
22342 this_type = read_tag_atomic_type (die, cu);
22343 break;
22344 default:
22345 complaint (_("unexpected tag in read_type_die: '%s'"),
22346 dwarf_tag_name (die->tag));
22347 break;
22348 }
22349
22350 return this_type;
22351 }
22352
22353 /* See if we can figure out if the class lives in a namespace. We do
22354 this by looking for a member function; its demangled name will
22355 contain namespace info, if there is any.
22356 Return the computed name or NULL.
22357 Space for the result is allocated on the objfile's obstack.
22358 This is the full-die version of guess_partial_die_structure_name.
22359 In this case we know DIE has no useful parent. */
22360
22361 static const char *
22362 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22363 {
22364 struct die_info *spec_die;
22365 struct dwarf2_cu *spec_cu;
22366 struct die_info *child;
22367 struct objfile *objfile = cu->per_objfile->objfile;
22368
22369 spec_cu = cu;
22370 spec_die = die_specification (die, &spec_cu);
22371 if (spec_die != NULL)
22372 {
22373 die = spec_die;
22374 cu = spec_cu;
22375 }
22376
22377 for (child = die->child;
22378 child != NULL;
22379 child = child->sibling)
22380 {
22381 if (child->tag == DW_TAG_subprogram)
22382 {
22383 const char *linkage_name = dw2_linkage_name (child, cu);
22384
22385 if (linkage_name != NULL)
22386 {
22387 gdb::unique_xmalloc_ptr<char> actual_name
22388 (cu->language_defn->class_name_from_physname (linkage_name));
22389 const char *name = NULL;
22390
22391 if (actual_name != NULL)
22392 {
22393 const char *die_name = dwarf2_name (die, cu);
22394
22395 if (die_name != NULL
22396 && strcmp (die_name, actual_name.get ()) != 0)
22397 {
22398 /* Strip off the class name from the full name.
22399 We want the prefix. */
22400 int die_name_len = strlen (die_name);
22401 int actual_name_len = strlen (actual_name.get ());
22402 const char *ptr = actual_name.get ();
22403
22404 /* Test for '::' as a sanity check. */
22405 if (actual_name_len > die_name_len + 2
22406 && ptr[actual_name_len - die_name_len - 1] == ':')
22407 name = obstack_strndup (
22408 &objfile->per_bfd->storage_obstack,
22409 ptr, actual_name_len - die_name_len - 2);
22410 }
22411 }
22412 return name;
22413 }
22414 }
22415 }
22416
22417 return NULL;
22418 }
22419
22420 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22421 prefix part in such case. See
22422 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22423
22424 static const char *
22425 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22426 {
22427 struct attribute *attr;
22428 const char *base;
22429
22430 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22431 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22432 return NULL;
22433
22434 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22435 return NULL;
22436
22437 attr = dw2_linkage_name_attr (die, cu);
22438 const char *attr_name = attr->as_string ();
22439 if (attr == NULL || attr_name == NULL)
22440 return NULL;
22441
22442 /* dwarf2_name had to be already called. */
22443 gdb_assert (attr->canonical_string_p ());
22444
22445 /* Strip the base name, keep any leading namespaces/classes. */
22446 base = strrchr (attr_name, ':');
22447 if (base == NULL || base == attr_name || base[-1] != ':')
22448 return "";
22449
22450 struct objfile *objfile = cu->per_objfile->objfile;
22451 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22452 attr_name,
22453 &base[-1] - attr_name);
22454 }
22455
22456 /* Return the name of the namespace/class that DIE is defined within,
22457 or "" if we can't tell. The caller should not xfree the result.
22458
22459 For example, if we're within the method foo() in the following
22460 code:
22461
22462 namespace N {
22463 class C {
22464 void foo () {
22465 }
22466 };
22467 }
22468
22469 then determine_prefix on foo's die will return "N::C". */
22470
22471 static const char *
22472 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22473 {
22474 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22475 struct die_info *parent, *spec_die;
22476 struct dwarf2_cu *spec_cu;
22477 struct type *parent_type;
22478 const char *retval;
22479
22480 if (cu->language != language_cplus
22481 && cu->language != language_fortran && cu->language != language_d
22482 && cu->language != language_rust)
22483 return "";
22484
22485 retval = anonymous_struct_prefix (die, cu);
22486 if (retval)
22487 return retval;
22488
22489 /* We have to be careful in the presence of DW_AT_specification.
22490 For example, with GCC 3.4, given the code
22491
22492 namespace N {
22493 void foo() {
22494 // Definition of N::foo.
22495 }
22496 }
22497
22498 then we'll have a tree of DIEs like this:
22499
22500 1: DW_TAG_compile_unit
22501 2: DW_TAG_namespace // N
22502 3: DW_TAG_subprogram // declaration of N::foo
22503 4: DW_TAG_subprogram // definition of N::foo
22504 DW_AT_specification // refers to die #3
22505
22506 Thus, when processing die #4, we have to pretend that we're in
22507 the context of its DW_AT_specification, namely the contex of die
22508 #3. */
22509 spec_cu = cu;
22510 spec_die = die_specification (die, &spec_cu);
22511 if (spec_die == NULL)
22512 parent = die->parent;
22513 else
22514 {
22515 parent = spec_die->parent;
22516 cu = spec_cu;
22517 }
22518
22519 if (parent == NULL)
22520 return "";
22521 else if (parent->building_fullname)
22522 {
22523 const char *name;
22524 const char *parent_name;
22525
22526 /* It has been seen on RealView 2.2 built binaries,
22527 DW_TAG_template_type_param types actually _defined_ as
22528 children of the parent class:
22529
22530 enum E {};
22531 template class <class Enum> Class{};
22532 Class<enum E> class_e;
22533
22534 1: DW_TAG_class_type (Class)
22535 2: DW_TAG_enumeration_type (E)
22536 3: DW_TAG_enumerator (enum1:0)
22537 3: DW_TAG_enumerator (enum2:1)
22538 ...
22539 2: DW_TAG_template_type_param
22540 DW_AT_type DW_FORM_ref_udata (E)
22541
22542 Besides being broken debug info, it can put GDB into an
22543 infinite loop. Consider:
22544
22545 When we're building the full name for Class<E>, we'll start
22546 at Class, and go look over its template type parameters,
22547 finding E. We'll then try to build the full name of E, and
22548 reach here. We're now trying to build the full name of E,
22549 and look over the parent DIE for containing scope. In the
22550 broken case, if we followed the parent DIE of E, we'd again
22551 find Class, and once again go look at its template type
22552 arguments, etc., etc. Simply don't consider such parent die
22553 as source-level parent of this die (it can't be, the language
22554 doesn't allow it), and break the loop here. */
22555 name = dwarf2_name (die, cu);
22556 parent_name = dwarf2_name (parent, cu);
22557 complaint (_("template param type '%s' defined within parent '%s'"),
22558 name ? name : "<unknown>",
22559 parent_name ? parent_name : "<unknown>");
22560 return "";
22561 }
22562 else
22563 switch (parent->tag)
22564 {
22565 case DW_TAG_namespace:
22566 parent_type = read_type_die (parent, cu);
22567 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22568 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22569 Work around this problem here. */
22570 if (cu->language == language_cplus
22571 && strcmp (parent_type->name (), "::") == 0)
22572 return "";
22573 /* We give a name to even anonymous namespaces. */
22574 return parent_type->name ();
22575 case DW_TAG_class_type:
22576 case DW_TAG_interface_type:
22577 case DW_TAG_structure_type:
22578 case DW_TAG_union_type:
22579 case DW_TAG_module:
22580 parent_type = read_type_die (parent, cu);
22581 if (parent_type->name () != NULL)
22582 return parent_type->name ();
22583 else
22584 /* An anonymous structure is only allowed non-static data
22585 members; no typedefs, no member functions, et cetera.
22586 So it does not need a prefix. */
22587 return "";
22588 case DW_TAG_compile_unit:
22589 case DW_TAG_partial_unit:
22590 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22591 if (cu->language == language_cplus
22592 && !per_objfile->per_bfd->types.empty ()
22593 && die->child != NULL
22594 && (die->tag == DW_TAG_class_type
22595 || die->tag == DW_TAG_structure_type
22596 || die->tag == DW_TAG_union_type))
22597 {
22598 const char *name = guess_full_die_structure_name (die, cu);
22599 if (name != NULL)
22600 return name;
22601 }
22602 return "";
22603 case DW_TAG_subprogram:
22604 /* Nested subroutines in Fortran get a prefix with the name
22605 of the parent's subroutine. */
22606 if (cu->language == language_fortran)
22607 {
22608 if ((die->tag == DW_TAG_subprogram)
22609 && (dwarf2_name (parent, cu) != NULL))
22610 return dwarf2_name (parent, cu);
22611 }
22612 return determine_prefix (parent, cu);
22613 case DW_TAG_enumeration_type:
22614 parent_type = read_type_die (parent, cu);
22615 if (parent_type->is_declared_class ())
22616 {
22617 if (parent_type->name () != NULL)
22618 return parent_type->name ();
22619 return "";
22620 }
22621 /* Fall through. */
22622 default:
22623 return determine_prefix (parent, cu);
22624 }
22625 }
22626
22627 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22628 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22629 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22630 an obconcat, otherwise allocate storage for the result. The CU argument is
22631 used to determine the language and hence, the appropriate separator. */
22632
22633 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22634
22635 static char *
22636 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22637 int physname, struct dwarf2_cu *cu)
22638 {
22639 const char *lead = "";
22640 const char *sep;
22641
22642 if (suffix == NULL || suffix[0] == '\0'
22643 || prefix == NULL || prefix[0] == '\0')
22644 sep = "";
22645 else if (cu->language == language_d)
22646 {
22647 /* For D, the 'main' function could be defined in any module, but it
22648 should never be prefixed. */
22649 if (strcmp (suffix, "D main") == 0)
22650 {
22651 prefix = "";
22652 sep = "";
22653 }
22654 else
22655 sep = ".";
22656 }
22657 else if (cu->language == language_fortran && physname)
22658 {
22659 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22660 DW_AT_MIPS_linkage_name is preferred and used instead. */
22661
22662 lead = "__";
22663 sep = "_MOD_";
22664 }
22665 else
22666 sep = "::";
22667
22668 if (prefix == NULL)
22669 prefix = "";
22670 if (suffix == NULL)
22671 suffix = "";
22672
22673 if (obs == NULL)
22674 {
22675 char *retval
22676 = ((char *)
22677 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22678
22679 strcpy (retval, lead);
22680 strcat (retval, prefix);
22681 strcat (retval, sep);
22682 strcat (retval, suffix);
22683 return retval;
22684 }
22685 else
22686 {
22687 /* We have an obstack. */
22688 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22689 }
22690 }
22691
22692 /* Get name of a die, return NULL if not found. */
22693
22694 static const char *
22695 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22696 struct objfile *objfile)
22697 {
22698 if (name && cu->language == language_cplus)
22699 {
22700 gdb::unique_xmalloc_ptr<char> canon_name
22701 = cp_canonicalize_string (name);
22702
22703 if (canon_name != nullptr)
22704 name = objfile->intern (canon_name.get ());
22705 }
22706
22707 return name;
22708 }
22709
22710 /* Get name of a die, return NULL if not found.
22711 Anonymous namespaces are converted to their magic string. */
22712
22713 static const char *
22714 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22715 {
22716 struct attribute *attr;
22717 struct objfile *objfile = cu->per_objfile->objfile;
22718
22719 attr = dwarf2_attr (die, DW_AT_name, cu);
22720 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22721 if (attr_name == nullptr
22722 && die->tag != DW_TAG_namespace
22723 && die->tag != DW_TAG_class_type
22724 && die->tag != DW_TAG_interface_type
22725 && die->tag != DW_TAG_structure_type
22726 && die->tag != DW_TAG_union_type)
22727 return NULL;
22728
22729 switch (die->tag)
22730 {
22731 case DW_TAG_compile_unit:
22732 case DW_TAG_partial_unit:
22733 /* Compilation units have a DW_AT_name that is a filename, not
22734 a source language identifier. */
22735 case DW_TAG_enumeration_type:
22736 case DW_TAG_enumerator:
22737 /* These tags always have simple identifiers already; no need
22738 to canonicalize them. */
22739 return attr_name;
22740
22741 case DW_TAG_namespace:
22742 if (attr_name != nullptr)
22743 return attr_name;
22744 return CP_ANONYMOUS_NAMESPACE_STR;
22745
22746 case DW_TAG_class_type:
22747 case DW_TAG_interface_type:
22748 case DW_TAG_structure_type:
22749 case DW_TAG_union_type:
22750 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22751 structures or unions. These were of the form "._%d" in GCC 4.1,
22752 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22753 and GCC 4.4. We work around this problem by ignoring these. */
22754 if (attr_name != nullptr
22755 && (startswith (attr_name, "._")
22756 || startswith (attr_name, "<anonymous")))
22757 return NULL;
22758
22759 /* GCC might emit a nameless typedef that has a linkage name. See
22760 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22761 if (!attr || attr_name == NULL)
22762 {
22763 attr = dw2_linkage_name_attr (die, cu);
22764 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22765 if (attr == NULL || attr_name == NULL)
22766 return NULL;
22767
22768 /* Avoid demangling attr_name the second time on a second
22769 call for the same DIE. */
22770 if (!attr->canonical_string_p ())
22771 {
22772 gdb::unique_xmalloc_ptr<char> demangled
22773 (gdb_demangle (attr_name, DMGL_TYPES));
22774 if (demangled == nullptr)
22775 return nullptr;
22776
22777 attr->set_string_canonical (objfile->intern (demangled.get ()));
22778 attr_name = attr->as_string ();
22779 }
22780
22781 /* Strip any leading namespaces/classes, keep only the
22782 base name. DW_AT_name for named DIEs does not
22783 contain the prefixes. */
22784 const char *base = strrchr (attr_name, ':');
22785 if (base && base > attr_name && base[-1] == ':')
22786 return &base[1];
22787 else
22788 return attr_name;
22789 }
22790 break;
22791
22792 default:
22793 break;
22794 }
22795
22796 if (!attr->canonical_string_p ())
22797 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22798 objfile));
22799 return attr->as_string ();
22800 }
22801
22802 /* Return the die that this die in an extension of, or NULL if there
22803 is none. *EXT_CU is the CU containing DIE on input, and the CU
22804 containing the return value on output. */
22805
22806 static struct die_info *
22807 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22808 {
22809 struct attribute *attr;
22810
22811 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22812 if (attr == NULL)
22813 return NULL;
22814
22815 return follow_die_ref (die, attr, ext_cu);
22816 }
22817
22818 static void
22819 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22820 {
22821 unsigned int i;
22822
22823 print_spaces (indent, f);
22824 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22825 dwarf_tag_name (die->tag), die->abbrev,
22826 sect_offset_str (die->sect_off));
22827
22828 if (die->parent != NULL)
22829 {
22830 print_spaces (indent, f);
22831 fprintf_unfiltered (f, " parent at offset: %s\n",
22832 sect_offset_str (die->parent->sect_off));
22833 }
22834
22835 print_spaces (indent, f);
22836 fprintf_unfiltered (f, " has children: %s\n",
22837 dwarf_bool_name (die->child != NULL));
22838
22839 print_spaces (indent, f);
22840 fprintf_unfiltered (f, " attributes:\n");
22841
22842 for (i = 0; i < die->num_attrs; ++i)
22843 {
22844 print_spaces (indent, f);
22845 fprintf_unfiltered (f, " %s (%s) ",
22846 dwarf_attr_name (die->attrs[i].name),
22847 dwarf_form_name (die->attrs[i].form));
22848
22849 switch (die->attrs[i].form)
22850 {
22851 case DW_FORM_addr:
22852 case DW_FORM_addrx:
22853 case DW_FORM_GNU_addr_index:
22854 fprintf_unfiltered (f, "address: ");
22855 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
22856 break;
22857 case DW_FORM_block2:
22858 case DW_FORM_block4:
22859 case DW_FORM_block:
22860 case DW_FORM_block1:
22861 fprintf_unfiltered (f, "block: size %s",
22862 pulongest (die->attrs[i].as_block ()->size));
22863 break;
22864 case DW_FORM_exprloc:
22865 fprintf_unfiltered (f, "expression: size %s",
22866 pulongest (die->attrs[i].as_block ()->size));
22867 break;
22868 case DW_FORM_data16:
22869 fprintf_unfiltered (f, "constant of 16 bytes");
22870 break;
22871 case DW_FORM_ref_addr:
22872 fprintf_unfiltered (f, "ref address: ");
22873 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22874 break;
22875 case DW_FORM_GNU_ref_alt:
22876 fprintf_unfiltered (f, "alt ref address: ");
22877 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22878 break;
22879 case DW_FORM_ref1:
22880 case DW_FORM_ref2:
22881 case DW_FORM_ref4:
22882 case DW_FORM_ref8:
22883 case DW_FORM_ref_udata:
22884 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22885 (long) (die->attrs[i].as_unsigned ()));
22886 break;
22887 case DW_FORM_data1:
22888 case DW_FORM_data2:
22889 case DW_FORM_data4:
22890 case DW_FORM_data8:
22891 case DW_FORM_udata:
22892 fprintf_unfiltered (f, "constant: %s",
22893 pulongest (die->attrs[i].as_unsigned ()));
22894 break;
22895 case DW_FORM_sec_offset:
22896 fprintf_unfiltered (f, "section offset: %s",
22897 pulongest (die->attrs[i].as_unsigned ()));
22898 break;
22899 case DW_FORM_ref_sig8:
22900 fprintf_unfiltered (f, "signature: %s",
22901 hex_string (die->attrs[i].as_signature ()));
22902 break;
22903 case DW_FORM_string:
22904 case DW_FORM_strp:
22905 case DW_FORM_line_strp:
22906 case DW_FORM_strx:
22907 case DW_FORM_GNU_str_index:
22908 case DW_FORM_GNU_strp_alt:
22909 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22910 die->attrs[i].as_string ()
22911 ? die->attrs[i].as_string () : "",
22912 die->attrs[i].canonical_string_p () ? "is" : "not");
22913 break;
22914 case DW_FORM_flag:
22915 if (die->attrs[i].as_boolean ())
22916 fprintf_unfiltered (f, "flag: TRUE");
22917 else
22918 fprintf_unfiltered (f, "flag: FALSE");
22919 break;
22920 case DW_FORM_flag_present:
22921 fprintf_unfiltered (f, "flag: TRUE");
22922 break;
22923 case DW_FORM_indirect:
22924 /* The reader will have reduced the indirect form to
22925 the "base form" so this form should not occur. */
22926 fprintf_unfiltered (f,
22927 "unexpected attribute form: DW_FORM_indirect");
22928 break;
22929 case DW_FORM_sdata:
22930 case DW_FORM_implicit_const:
22931 fprintf_unfiltered (f, "constant: %s",
22932 plongest (die->attrs[i].as_signed ()));
22933 break;
22934 default:
22935 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22936 die->attrs[i].form);
22937 break;
22938 }
22939 fprintf_unfiltered (f, "\n");
22940 }
22941 }
22942
22943 static void
22944 dump_die_for_error (struct die_info *die)
22945 {
22946 dump_die_shallow (gdb_stderr, 0, die);
22947 }
22948
22949 static void
22950 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22951 {
22952 int indent = level * 4;
22953
22954 gdb_assert (die != NULL);
22955
22956 if (level >= max_level)
22957 return;
22958
22959 dump_die_shallow (f, indent, die);
22960
22961 if (die->child != NULL)
22962 {
22963 print_spaces (indent, f);
22964 fprintf_unfiltered (f, " Children:");
22965 if (level + 1 < max_level)
22966 {
22967 fprintf_unfiltered (f, "\n");
22968 dump_die_1 (f, level + 1, max_level, die->child);
22969 }
22970 else
22971 {
22972 fprintf_unfiltered (f,
22973 " [not printed, max nesting level reached]\n");
22974 }
22975 }
22976
22977 if (die->sibling != NULL && level > 0)
22978 {
22979 dump_die_1 (f, level, max_level, die->sibling);
22980 }
22981 }
22982
22983 /* This is called from the pdie macro in gdbinit.in.
22984 It's not static so gcc will keep a copy callable from gdb. */
22985
22986 void
22987 dump_die (struct die_info *die, int max_level)
22988 {
22989 dump_die_1 (gdb_stdlog, 0, max_level, die);
22990 }
22991
22992 static void
22993 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22994 {
22995 void **slot;
22996
22997 slot = htab_find_slot_with_hash (cu->die_hash, die,
22998 to_underlying (die->sect_off),
22999 INSERT);
23000
23001 *slot = die;
23002 }
23003
23004 /* Follow reference or signature attribute ATTR of SRC_DIE.
23005 On entry *REF_CU is the CU of SRC_DIE.
23006 On exit *REF_CU is the CU of the result. */
23007
23008 static struct die_info *
23009 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23010 struct dwarf2_cu **ref_cu)
23011 {
23012 struct die_info *die;
23013
23014 if (attr->form_is_ref ())
23015 die = follow_die_ref (src_die, attr, ref_cu);
23016 else if (attr->form == DW_FORM_ref_sig8)
23017 die = follow_die_sig (src_die, attr, ref_cu);
23018 else
23019 {
23020 dump_die_for_error (src_die);
23021 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23022 objfile_name ((*ref_cu)->per_objfile->objfile));
23023 }
23024
23025 return die;
23026 }
23027
23028 /* Follow reference OFFSET.
23029 On entry *REF_CU is the CU of the source die referencing OFFSET.
23030 On exit *REF_CU is the CU of the result.
23031 Returns NULL if OFFSET is invalid. */
23032
23033 static struct die_info *
23034 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23035 struct dwarf2_cu **ref_cu)
23036 {
23037 struct die_info temp_die;
23038 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23039 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23040
23041 gdb_assert (cu->per_cu != NULL);
23042
23043 target_cu = cu;
23044
23045 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23046 "source CU contains target offset: %d",
23047 sect_offset_str (cu->per_cu->sect_off),
23048 sect_offset_str (sect_off),
23049 cu->header.offset_in_cu_p (sect_off));
23050
23051 if (cu->per_cu->is_debug_types)
23052 {
23053 /* .debug_types CUs cannot reference anything outside their CU.
23054 If they need to, they have to reference a signatured type via
23055 DW_FORM_ref_sig8. */
23056 if (!cu->header.offset_in_cu_p (sect_off))
23057 return NULL;
23058 }
23059 else if (offset_in_dwz != cu->per_cu->is_dwz
23060 || !cu->header.offset_in_cu_p (sect_off))
23061 {
23062 struct dwarf2_per_cu_data *per_cu;
23063
23064 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23065 per_objfile);
23066
23067 dwarf_read_debug_printf_v ("target CU offset: %s, "
23068 "target CU DIEs loaded: %d",
23069 sect_offset_str (per_cu->sect_off),
23070 per_objfile->get_cu (per_cu) != nullptr);
23071
23072 /* If necessary, add it to the queue and load its DIEs.
23073
23074 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23075 it doesn't mean they are currently loaded. Since we require them
23076 to be loaded, we must check for ourselves. */
23077 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23078 || per_objfile->get_cu (per_cu) == nullptr)
23079 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23080 false, cu->language);
23081
23082 target_cu = per_objfile->get_cu (per_cu);
23083 gdb_assert (target_cu != nullptr);
23084 }
23085 else if (cu->dies == NULL)
23086 {
23087 /* We're loading full DIEs during partial symbol reading. */
23088 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23089 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23090 language_minimal);
23091 }
23092
23093 *ref_cu = target_cu;
23094 temp_die.sect_off = sect_off;
23095
23096 if (target_cu != cu)
23097 target_cu->ancestor = cu;
23098
23099 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23100 &temp_die,
23101 to_underlying (sect_off));
23102 }
23103
23104 /* Follow reference attribute ATTR of SRC_DIE.
23105 On entry *REF_CU is the CU of SRC_DIE.
23106 On exit *REF_CU is the CU of the result. */
23107
23108 static struct die_info *
23109 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23110 struct dwarf2_cu **ref_cu)
23111 {
23112 sect_offset sect_off = attr->get_ref_die_offset ();
23113 struct dwarf2_cu *cu = *ref_cu;
23114 struct die_info *die;
23115
23116 die = follow_die_offset (sect_off,
23117 (attr->form == DW_FORM_GNU_ref_alt
23118 || cu->per_cu->is_dwz),
23119 ref_cu);
23120 if (!die)
23121 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23122 "at %s [in module %s]"),
23123 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23124 objfile_name (cu->per_objfile->objfile));
23125
23126 return die;
23127 }
23128
23129 /* See read.h. */
23130
23131 struct dwarf2_locexpr_baton
23132 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23133 dwarf2_per_cu_data *per_cu,
23134 dwarf2_per_objfile *per_objfile,
23135 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23136 bool resolve_abstract_p)
23137 {
23138 struct die_info *die;
23139 struct attribute *attr;
23140 struct dwarf2_locexpr_baton retval;
23141 struct objfile *objfile = per_objfile->objfile;
23142
23143 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23144 if (cu == nullptr)
23145 cu = load_cu (per_cu, per_objfile, false);
23146
23147 if (cu == nullptr)
23148 {
23149 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23150 Instead just throw an error, not much else we can do. */
23151 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23152 sect_offset_str (sect_off), objfile_name (objfile));
23153 }
23154
23155 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23156 if (!die)
23157 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23158 sect_offset_str (sect_off), objfile_name (objfile));
23159
23160 attr = dwarf2_attr (die, DW_AT_location, cu);
23161 if (!attr && resolve_abstract_p
23162 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23163 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23164 {
23165 CORE_ADDR pc = get_frame_pc ();
23166 CORE_ADDR baseaddr = objfile->text_section_offset ();
23167 struct gdbarch *gdbarch = objfile->arch ();
23168
23169 for (const auto &cand_off
23170 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23171 {
23172 struct dwarf2_cu *cand_cu = cu;
23173 struct die_info *cand
23174 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23175 if (!cand
23176 || !cand->parent
23177 || cand->parent->tag != DW_TAG_subprogram)
23178 continue;
23179
23180 CORE_ADDR pc_low, pc_high;
23181 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23182 if (pc_low == ((CORE_ADDR) -1))
23183 continue;
23184 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23185 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23186 if (!(pc_low <= pc && pc < pc_high))
23187 continue;
23188
23189 die = cand;
23190 attr = dwarf2_attr (die, DW_AT_location, cu);
23191 break;
23192 }
23193 }
23194
23195 if (!attr)
23196 {
23197 /* DWARF: "If there is no such attribute, then there is no effect.".
23198 DATA is ignored if SIZE is 0. */
23199
23200 retval.data = NULL;
23201 retval.size = 0;
23202 }
23203 else if (attr->form_is_section_offset ())
23204 {
23205 struct dwarf2_loclist_baton loclist_baton;
23206 CORE_ADDR pc = get_frame_pc ();
23207 size_t size;
23208
23209 fill_in_loclist_baton (cu, &loclist_baton, attr);
23210
23211 retval.data = dwarf2_find_location_expression (&loclist_baton,
23212 &size, pc);
23213 retval.size = size;
23214 }
23215 else
23216 {
23217 if (!attr->form_is_block ())
23218 error (_("Dwarf Error: DIE at %s referenced in module %s "
23219 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23220 sect_offset_str (sect_off), objfile_name (objfile));
23221
23222 struct dwarf_block *block = attr->as_block ();
23223 retval.data = block->data;
23224 retval.size = block->size;
23225 }
23226 retval.per_objfile = per_objfile;
23227 retval.per_cu = cu->per_cu;
23228
23229 per_objfile->age_comp_units ();
23230
23231 return retval;
23232 }
23233
23234 /* See read.h. */
23235
23236 struct dwarf2_locexpr_baton
23237 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23238 dwarf2_per_cu_data *per_cu,
23239 dwarf2_per_objfile *per_objfile,
23240 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23241 {
23242 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23243
23244 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23245 get_frame_pc);
23246 }
23247
23248 /* Write a constant of a given type as target-ordered bytes into
23249 OBSTACK. */
23250
23251 static const gdb_byte *
23252 write_constant_as_bytes (struct obstack *obstack,
23253 enum bfd_endian byte_order,
23254 struct type *type,
23255 ULONGEST value,
23256 LONGEST *len)
23257 {
23258 gdb_byte *result;
23259
23260 *len = TYPE_LENGTH (type);
23261 result = (gdb_byte *) obstack_alloc (obstack, *len);
23262 store_unsigned_integer (result, *len, byte_order, value);
23263
23264 return result;
23265 }
23266
23267 /* See read.h. */
23268
23269 const gdb_byte *
23270 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23271 dwarf2_per_cu_data *per_cu,
23272 dwarf2_per_objfile *per_objfile,
23273 obstack *obstack,
23274 LONGEST *len)
23275 {
23276 struct die_info *die;
23277 struct attribute *attr;
23278 const gdb_byte *result = NULL;
23279 struct type *type;
23280 LONGEST value;
23281 enum bfd_endian byte_order;
23282 struct objfile *objfile = per_objfile->objfile;
23283
23284 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23285 if (cu == nullptr)
23286 cu = load_cu (per_cu, per_objfile, false);
23287
23288 if (cu == nullptr)
23289 {
23290 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23291 Instead just throw an error, not much else we can do. */
23292 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23293 sect_offset_str (sect_off), objfile_name (objfile));
23294 }
23295
23296 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23297 if (!die)
23298 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23299 sect_offset_str (sect_off), objfile_name (objfile));
23300
23301 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23302 if (attr == NULL)
23303 return NULL;
23304
23305 byte_order = (bfd_big_endian (objfile->obfd)
23306 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23307
23308 switch (attr->form)
23309 {
23310 case DW_FORM_addr:
23311 case DW_FORM_addrx:
23312 case DW_FORM_GNU_addr_index:
23313 {
23314 gdb_byte *tem;
23315
23316 *len = cu->header.addr_size;
23317 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23318 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23319 result = tem;
23320 }
23321 break;
23322 case DW_FORM_string:
23323 case DW_FORM_strp:
23324 case DW_FORM_strx:
23325 case DW_FORM_GNU_str_index:
23326 case DW_FORM_GNU_strp_alt:
23327 /* The string is already allocated on the objfile obstack, point
23328 directly to it. */
23329 {
23330 const char *attr_name = attr->as_string ();
23331 result = (const gdb_byte *) attr_name;
23332 *len = strlen (attr_name);
23333 }
23334 break;
23335 case DW_FORM_block1:
23336 case DW_FORM_block2:
23337 case DW_FORM_block4:
23338 case DW_FORM_block:
23339 case DW_FORM_exprloc:
23340 case DW_FORM_data16:
23341 {
23342 struct dwarf_block *block = attr->as_block ();
23343 result = block->data;
23344 *len = block->size;
23345 }
23346 break;
23347
23348 /* The DW_AT_const_value attributes are supposed to carry the
23349 symbol's value "represented as it would be on the target
23350 architecture." By the time we get here, it's already been
23351 converted to host endianness, so we just need to sign- or
23352 zero-extend it as appropriate. */
23353 case DW_FORM_data1:
23354 type = die_type (die, cu);
23355 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23356 if (result == NULL)
23357 result = write_constant_as_bytes (obstack, byte_order,
23358 type, value, len);
23359 break;
23360 case DW_FORM_data2:
23361 type = die_type (die, cu);
23362 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23363 if (result == NULL)
23364 result = write_constant_as_bytes (obstack, byte_order,
23365 type, value, len);
23366 break;
23367 case DW_FORM_data4:
23368 type = die_type (die, cu);
23369 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23370 if (result == NULL)
23371 result = write_constant_as_bytes (obstack, byte_order,
23372 type, value, len);
23373 break;
23374 case DW_FORM_data8:
23375 type = die_type (die, cu);
23376 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23377 if (result == NULL)
23378 result = write_constant_as_bytes (obstack, byte_order,
23379 type, value, len);
23380 break;
23381
23382 case DW_FORM_sdata:
23383 case DW_FORM_implicit_const:
23384 type = die_type (die, cu);
23385 result = write_constant_as_bytes (obstack, byte_order,
23386 type, attr->as_signed (), len);
23387 break;
23388
23389 case DW_FORM_udata:
23390 type = die_type (die, cu);
23391 result = write_constant_as_bytes (obstack, byte_order,
23392 type, attr->as_unsigned (), len);
23393 break;
23394
23395 default:
23396 complaint (_("unsupported const value attribute form: '%s'"),
23397 dwarf_form_name (attr->form));
23398 break;
23399 }
23400
23401 return result;
23402 }
23403
23404 /* See read.h. */
23405
23406 struct type *
23407 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23408 dwarf2_per_cu_data *per_cu,
23409 dwarf2_per_objfile *per_objfile)
23410 {
23411 struct die_info *die;
23412
23413 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23414 if (cu == nullptr)
23415 cu = load_cu (per_cu, per_objfile, false);
23416
23417 if (cu == nullptr)
23418 return nullptr;
23419
23420 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23421 if (!die)
23422 return NULL;
23423
23424 return die_type (die, cu);
23425 }
23426
23427 /* See read.h. */
23428
23429 struct type *
23430 dwarf2_get_die_type (cu_offset die_offset,
23431 dwarf2_per_cu_data *per_cu,
23432 dwarf2_per_objfile *per_objfile)
23433 {
23434 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23435 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23436 }
23437
23438 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23439 On entry *REF_CU is the CU of SRC_DIE.
23440 On exit *REF_CU is the CU of the result.
23441 Returns NULL if the referenced DIE isn't found. */
23442
23443 static struct die_info *
23444 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23445 struct dwarf2_cu **ref_cu)
23446 {
23447 struct die_info temp_die;
23448 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23449 struct die_info *die;
23450 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23451
23452
23453 /* While it might be nice to assert sig_type->type == NULL here,
23454 we can get here for DW_AT_imported_declaration where we need
23455 the DIE not the type. */
23456
23457 /* If necessary, add it to the queue and load its DIEs.
23458
23459 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23460 it doesn't mean they are currently loaded. Since we require them
23461 to be loaded, we must check for ourselves. */
23462 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23463 language_minimal)
23464 || per_objfile->get_cu (sig_type) == nullptr)
23465 read_signatured_type (sig_type, per_objfile);
23466
23467 sig_cu = per_objfile->get_cu (sig_type);
23468 gdb_assert (sig_cu != NULL);
23469 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23470 temp_die.sect_off = sig_type->type_offset_in_section;
23471 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23472 to_underlying (temp_die.sect_off));
23473 if (die)
23474 {
23475 /* For .gdb_index version 7 keep track of included TUs.
23476 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23477 if (per_objfile->per_bfd->index_table != NULL
23478 && per_objfile->per_bfd->index_table->version <= 7)
23479 {
23480 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23481 }
23482
23483 *ref_cu = sig_cu;
23484 if (sig_cu != cu)
23485 sig_cu->ancestor = cu;
23486
23487 return die;
23488 }
23489
23490 return NULL;
23491 }
23492
23493 /* Follow signatured type referenced by ATTR in SRC_DIE.
23494 On entry *REF_CU is the CU of SRC_DIE.
23495 On exit *REF_CU is the CU of the result.
23496 The result is the DIE of the type.
23497 If the referenced type cannot be found an error is thrown. */
23498
23499 static struct die_info *
23500 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23501 struct dwarf2_cu **ref_cu)
23502 {
23503 ULONGEST signature = attr->as_signature ();
23504 struct signatured_type *sig_type;
23505 struct die_info *die;
23506
23507 gdb_assert (attr->form == DW_FORM_ref_sig8);
23508
23509 sig_type = lookup_signatured_type (*ref_cu, signature);
23510 /* sig_type will be NULL if the signatured type is missing from
23511 the debug info. */
23512 if (sig_type == NULL)
23513 {
23514 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23515 " from DIE at %s [in module %s]"),
23516 hex_string (signature), sect_offset_str (src_die->sect_off),
23517 objfile_name ((*ref_cu)->per_objfile->objfile));
23518 }
23519
23520 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23521 if (die == NULL)
23522 {
23523 dump_die_for_error (src_die);
23524 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23525 " from DIE at %s [in module %s]"),
23526 hex_string (signature), sect_offset_str (src_die->sect_off),
23527 objfile_name ((*ref_cu)->per_objfile->objfile));
23528 }
23529
23530 return die;
23531 }
23532
23533 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23534 reading in and processing the type unit if necessary. */
23535
23536 static struct type *
23537 get_signatured_type (struct die_info *die, ULONGEST signature,
23538 struct dwarf2_cu *cu)
23539 {
23540 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23541 struct signatured_type *sig_type;
23542 struct dwarf2_cu *type_cu;
23543 struct die_info *type_die;
23544 struct type *type;
23545
23546 sig_type = lookup_signatured_type (cu, signature);
23547 /* sig_type will be NULL if the signatured type is missing from
23548 the debug info. */
23549 if (sig_type == NULL)
23550 {
23551 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23552 " from DIE at %s [in module %s]"),
23553 hex_string (signature), sect_offset_str (die->sect_off),
23554 objfile_name (per_objfile->objfile));
23555 return build_error_marker_type (cu, die);
23556 }
23557
23558 /* If we already know the type we're done. */
23559 type = per_objfile->get_type_for_signatured_type (sig_type);
23560 if (type != nullptr)
23561 return type;
23562
23563 type_cu = cu;
23564 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23565 if (type_die != NULL)
23566 {
23567 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23568 is created. This is important, for example, because for c++ classes
23569 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23570 type = read_type_die (type_die, type_cu);
23571 if (type == NULL)
23572 {
23573 complaint (_("Dwarf Error: Cannot build signatured type %s"
23574 " referenced from DIE at %s [in module %s]"),
23575 hex_string (signature), sect_offset_str (die->sect_off),
23576 objfile_name (per_objfile->objfile));
23577 type = build_error_marker_type (cu, die);
23578 }
23579 }
23580 else
23581 {
23582 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23583 " from DIE at %s [in module %s]"),
23584 hex_string (signature), sect_offset_str (die->sect_off),
23585 objfile_name (per_objfile->objfile));
23586 type = build_error_marker_type (cu, die);
23587 }
23588
23589 per_objfile->set_type_for_signatured_type (sig_type, type);
23590
23591 return type;
23592 }
23593
23594 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23595 reading in and processing the type unit if necessary. */
23596
23597 static struct type *
23598 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23599 struct dwarf2_cu *cu) /* ARI: editCase function */
23600 {
23601 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23602 if (attr->form_is_ref ())
23603 {
23604 struct dwarf2_cu *type_cu = cu;
23605 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23606
23607 return read_type_die (type_die, type_cu);
23608 }
23609 else if (attr->form == DW_FORM_ref_sig8)
23610 {
23611 return get_signatured_type (die, attr->as_signature (), cu);
23612 }
23613 else
23614 {
23615 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23616
23617 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23618 " at %s [in module %s]"),
23619 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23620 objfile_name (per_objfile->objfile));
23621 return build_error_marker_type (cu, die);
23622 }
23623 }
23624
23625 /* Load the DIEs associated with type unit PER_CU into memory. */
23626
23627 static void
23628 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23629 dwarf2_per_objfile *per_objfile)
23630 {
23631 struct signatured_type *sig_type;
23632
23633 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23634 gdb_assert (! per_cu->type_unit_group_p ());
23635
23636 /* We have the per_cu, but we need the signatured_type.
23637 Fortunately this is an easy translation. */
23638 gdb_assert (per_cu->is_debug_types);
23639 sig_type = (struct signatured_type *) per_cu;
23640
23641 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23642
23643 read_signatured_type (sig_type, per_objfile);
23644
23645 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23646 }
23647
23648 /* Read in a signatured type and build its CU and DIEs.
23649 If the type is a stub for the real type in a DWO file,
23650 read in the real type from the DWO file as well. */
23651
23652 static void
23653 read_signatured_type (signatured_type *sig_type,
23654 dwarf2_per_objfile *per_objfile)
23655 {
23656 gdb_assert (sig_type->is_debug_types);
23657 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23658
23659 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23660
23661 if (!reader.dummy_p)
23662 {
23663 struct dwarf2_cu *cu = reader.cu;
23664 const gdb_byte *info_ptr = reader.info_ptr;
23665
23666 gdb_assert (cu->die_hash == NULL);
23667 cu->die_hash =
23668 htab_create_alloc_ex (cu->header.length / 12,
23669 die_hash,
23670 die_eq,
23671 NULL,
23672 &cu->comp_unit_obstack,
23673 hashtab_obstack_allocate,
23674 dummy_obstack_deallocate);
23675
23676 if (reader.comp_unit_die->has_children)
23677 reader.comp_unit_die->child
23678 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23679 reader.comp_unit_die);
23680 cu->dies = reader.comp_unit_die;
23681 /* comp_unit_die is not stored in die_hash, no need. */
23682
23683 /* We try not to read any attributes in this function, because
23684 not all CUs needed for references have been loaded yet, and
23685 symbol table processing isn't initialized. But we have to
23686 set the CU language, or we won't be able to build types
23687 correctly. Similarly, if we do not read the producer, we can
23688 not apply producer-specific interpretation. */
23689 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23690
23691 reader.keep ();
23692 }
23693
23694 sig_type->tu_read = 1;
23695 }
23696
23697 /* Decode simple location descriptions.
23698 Given a pointer to a dwarf block that defines a location, compute
23699 the location and return the value. If COMPUTED is non-null, it is
23700 set to true to indicate that decoding was successful, and false
23701 otherwise. If COMPUTED is null, then this function may emit a
23702 complaint. */
23703
23704 static CORE_ADDR
23705 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23706 {
23707 struct objfile *objfile = cu->per_objfile->objfile;
23708 size_t i;
23709 size_t size = blk->size;
23710 const gdb_byte *data = blk->data;
23711 CORE_ADDR stack[64];
23712 int stacki;
23713 unsigned int bytes_read, unsnd;
23714 gdb_byte op;
23715
23716 if (computed != nullptr)
23717 *computed = false;
23718
23719 i = 0;
23720 stacki = 0;
23721 stack[stacki] = 0;
23722 stack[++stacki] = 0;
23723
23724 while (i < size)
23725 {
23726 op = data[i++];
23727 switch (op)
23728 {
23729 case DW_OP_lit0:
23730 case DW_OP_lit1:
23731 case DW_OP_lit2:
23732 case DW_OP_lit3:
23733 case DW_OP_lit4:
23734 case DW_OP_lit5:
23735 case DW_OP_lit6:
23736 case DW_OP_lit7:
23737 case DW_OP_lit8:
23738 case DW_OP_lit9:
23739 case DW_OP_lit10:
23740 case DW_OP_lit11:
23741 case DW_OP_lit12:
23742 case DW_OP_lit13:
23743 case DW_OP_lit14:
23744 case DW_OP_lit15:
23745 case DW_OP_lit16:
23746 case DW_OP_lit17:
23747 case DW_OP_lit18:
23748 case DW_OP_lit19:
23749 case DW_OP_lit20:
23750 case DW_OP_lit21:
23751 case DW_OP_lit22:
23752 case DW_OP_lit23:
23753 case DW_OP_lit24:
23754 case DW_OP_lit25:
23755 case DW_OP_lit26:
23756 case DW_OP_lit27:
23757 case DW_OP_lit28:
23758 case DW_OP_lit29:
23759 case DW_OP_lit30:
23760 case DW_OP_lit31:
23761 stack[++stacki] = op - DW_OP_lit0;
23762 break;
23763
23764 case DW_OP_reg0:
23765 case DW_OP_reg1:
23766 case DW_OP_reg2:
23767 case DW_OP_reg3:
23768 case DW_OP_reg4:
23769 case DW_OP_reg5:
23770 case DW_OP_reg6:
23771 case DW_OP_reg7:
23772 case DW_OP_reg8:
23773 case DW_OP_reg9:
23774 case DW_OP_reg10:
23775 case DW_OP_reg11:
23776 case DW_OP_reg12:
23777 case DW_OP_reg13:
23778 case DW_OP_reg14:
23779 case DW_OP_reg15:
23780 case DW_OP_reg16:
23781 case DW_OP_reg17:
23782 case DW_OP_reg18:
23783 case DW_OP_reg19:
23784 case DW_OP_reg20:
23785 case DW_OP_reg21:
23786 case DW_OP_reg22:
23787 case DW_OP_reg23:
23788 case DW_OP_reg24:
23789 case DW_OP_reg25:
23790 case DW_OP_reg26:
23791 case DW_OP_reg27:
23792 case DW_OP_reg28:
23793 case DW_OP_reg29:
23794 case DW_OP_reg30:
23795 case DW_OP_reg31:
23796 stack[++stacki] = op - DW_OP_reg0;
23797 if (i < size)
23798 {
23799 if (computed == nullptr)
23800 dwarf2_complex_location_expr_complaint ();
23801 else
23802 return 0;
23803 }
23804 break;
23805
23806 case DW_OP_regx:
23807 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23808 i += bytes_read;
23809 stack[++stacki] = unsnd;
23810 if (i < size)
23811 {
23812 if (computed == nullptr)
23813 dwarf2_complex_location_expr_complaint ();
23814 else
23815 return 0;
23816 }
23817 break;
23818
23819 case DW_OP_addr:
23820 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23821 &bytes_read);
23822 i += bytes_read;
23823 break;
23824
23825 case DW_OP_const1u:
23826 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23827 i += 1;
23828 break;
23829
23830 case DW_OP_const1s:
23831 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23832 i += 1;
23833 break;
23834
23835 case DW_OP_const2u:
23836 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23837 i += 2;
23838 break;
23839
23840 case DW_OP_const2s:
23841 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23842 i += 2;
23843 break;
23844
23845 case DW_OP_const4u:
23846 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23847 i += 4;
23848 break;
23849
23850 case DW_OP_const4s:
23851 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23852 i += 4;
23853 break;
23854
23855 case DW_OP_const8u:
23856 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23857 i += 8;
23858 break;
23859
23860 case DW_OP_constu:
23861 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23862 &bytes_read);
23863 i += bytes_read;
23864 break;
23865
23866 case DW_OP_consts:
23867 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23868 i += bytes_read;
23869 break;
23870
23871 case DW_OP_dup:
23872 stack[stacki + 1] = stack[stacki];
23873 stacki++;
23874 break;
23875
23876 case DW_OP_plus:
23877 stack[stacki - 1] += stack[stacki];
23878 stacki--;
23879 break;
23880
23881 case DW_OP_plus_uconst:
23882 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23883 &bytes_read);
23884 i += bytes_read;
23885 break;
23886
23887 case DW_OP_minus:
23888 stack[stacki - 1] -= stack[stacki];
23889 stacki--;
23890 break;
23891
23892 case DW_OP_deref:
23893 /* If we're not the last op, then we definitely can't encode
23894 this using GDB's address_class enum. This is valid for partial
23895 global symbols, although the variable's address will be bogus
23896 in the psymtab. */
23897 if (i < size)
23898 {
23899 if (computed == nullptr)
23900 dwarf2_complex_location_expr_complaint ();
23901 else
23902 return 0;
23903 }
23904 break;
23905
23906 case DW_OP_GNU_push_tls_address:
23907 case DW_OP_form_tls_address:
23908 /* The top of the stack has the offset from the beginning
23909 of the thread control block at which the variable is located. */
23910 /* Nothing should follow this operator, so the top of stack would
23911 be returned. */
23912 /* This is valid for partial global symbols, but the variable's
23913 address will be bogus in the psymtab. Make it always at least
23914 non-zero to not look as a variable garbage collected by linker
23915 which have DW_OP_addr 0. */
23916 if (i < size)
23917 {
23918 if (computed == nullptr)
23919 dwarf2_complex_location_expr_complaint ();
23920 else
23921 return 0;
23922 }
23923 stack[stacki]++;
23924 break;
23925
23926 case DW_OP_GNU_uninit:
23927 if (computed != nullptr)
23928 return 0;
23929 break;
23930
23931 case DW_OP_addrx:
23932 case DW_OP_GNU_addr_index:
23933 case DW_OP_GNU_const_index:
23934 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23935 &bytes_read);
23936 i += bytes_read;
23937 break;
23938
23939 default:
23940 if (computed == nullptr)
23941 {
23942 const char *name = get_DW_OP_name (op);
23943
23944 if (name)
23945 complaint (_("unsupported stack op: '%s'"),
23946 name);
23947 else
23948 complaint (_("unsupported stack op: '%02x'"),
23949 op);
23950 }
23951
23952 return (stack[stacki]);
23953 }
23954
23955 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23956 outside of the allocated space. Also enforce minimum>0. */
23957 if (stacki >= ARRAY_SIZE (stack) - 1)
23958 {
23959 if (computed == nullptr)
23960 complaint (_("location description stack overflow"));
23961 return 0;
23962 }
23963
23964 if (stacki <= 0)
23965 {
23966 if (computed == nullptr)
23967 complaint (_("location description stack underflow"));
23968 return 0;
23969 }
23970 }
23971
23972 if (computed != nullptr)
23973 *computed = true;
23974 return (stack[stacki]);
23975 }
23976
23977 /* memory allocation interface */
23978
23979 static struct dwarf_block *
23980 dwarf_alloc_block (struct dwarf2_cu *cu)
23981 {
23982 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23983 }
23984
23985 static struct die_info *
23986 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23987 {
23988 struct die_info *die;
23989 size_t size = sizeof (struct die_info);
23990
23991 if (num_attrs > 1)
23992 size += (num_attrs - 1) * sizeof (struct attribute);
23993
23994 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23995 memset (die, 0, sizeof (struct die_info));
23996 return (die);
23997 }
23998
23999 \f
24000
24001 /* Macro support. */
24002
24003 /* An overload of dwarf_decode_macros that finds the correct section
24004 and ensures it is read in before calling the other overload. */
24005
24006 static void
24007 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24008 int section_is_gnu)
24009 {
24010 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24011 struct objfile *objfile = per_objfile->objfile;
24012 const struct line_header *lh = cu->line_header;
24013 unsigned int offset_size = cu->header.offset_size;
24014 struct dwarf2_section_info *section;
24015 const char *section_name;
24016
24017 if (cu->dwo_unit != nullptr)
24018 {
24019 if (section_is_gnu)
24020 {
24021 section = &cu->dwo_unit->dwo_file->sections.macro;
24022 section_name = ".debug_macro.dwo";
24023 }
24024 else
24025 {
24026 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24027 section_name = ".debug_macinfo.dwo";
24028 }
24029 }
24030 else
24031 {
24032 if (section_is_gnu)
24033 {
24034 section = &per_objfile->per_bfd->macro;
24035 section_name = ".debug_macro";
24036 }
24037 else
24038 {
24039 section = &per_objfile->per_bfd->macinfo;
24040 section_name = ".debug_macinfo";
24041 }
24042 }
24043
24044 section->read (objfile);
24045 if (section->buffer == nullptr)
24046 {
24047 complaint (_("missing %s section"), section_name);
24048 return;
24049 }
24050
24051 buildsym_compunit *builder = cu->get_builder ();
24052
24053 struct dwarf2_section_info *str_offsets_section;
24054 struct dwarf2_section_info *str_section;
24055 ULONGEST str_offsets_base;
24056
24057 if (cu->dwo_unit != nullptr)
24058 {
24059 str_offsets_section = &cu->dwo_unit->dwo_file
24060 ->sections.str_offsets;
24061 str_section = &cu->dwo_unit->dwo_file->sections.str;
24062 str_offsets_base = cu->header.addr_size;
24063 }
24064 else
24065 {
24066 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24067 str_section = &per_objfile->per_bfd->str;
24068 str_offsets_base = *cu->str_offsets_base;
24069 }
24070
24071 dwarf_decode_macros (per_objfile, builder, section, lh,
24072 offset_size, offset, str_section, str_offsets_section,
24073 str_offsets_base, section_is_gnu);
24074 }
24075
24076 /* Return the .debug_loc section to use for CU.
24077 For DWO files use .debug_loc.dwo. */
24078
24079 static struct dwarf2_section_info *
24080 cu_debug_loc_section (struct dwarf2_cu *cu)
24081 {
24082 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24083
24084 if (cu->dwo_unit)
24085 {
24086 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24087
24088 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24089 }
24090 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24091 : &per_objfile->per_bfd->loc);
24092 }
24093
24094 /* Return the .debug_rnglists section to use for CU. */
24095 static struct dwarf2_section_info *
24096 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24097 {
24098 if (cu->header.version < 5)
24099 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24100 cu->header.version);
24101 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24102
24103 /* Make sure we read the .debug_rnglists section from the file that
24104 contains the DW_AT_ranges attribute we are reading. Normally that
24105 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24106 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24107 program. */
24108 if (cu->dwo_unit != nullptr
24109 && tag != DW_TAG_compile_unit
24110 && tag != DW_TAG_skeleton_unit)
24111 {
24112 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24113
24114 if (sections->rnglists.size > 0)
24115 return &sections->rnglists;
24116 else
24117 error (_(".debug_rnglists section is missing from .dwo file."));
24118 }
24119 return &dwarf2_per_objfile->per_bfd->rnglists;
24120 }
24121
24122 /* A helper function that fills in a dwarf2_loclist_baton. */
24123
24124 static void
24125 fill_in_loclist_baton (struct dwarf2_cu *cu,
24126 struct dwarf2_loclist_baton *baton,
24127 const struct attribute *attr)
24128 {
24129 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24130 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24131
24132 section->read (per_objfile->objfile);
24133
24134 baton->per_objfile = per_objfile;
24135 baton->per_cu = cu->per_cu;
24136 gdb_assert (baton->per_cu);
24137 /* We don't know how long the location list is, but make sure we
24138 don't run off the edge of the section. */
24139 baton->size = section->size - attr->as_unsigned ();
24140 baton->data = section->buffer + attr->as_unsigned ();
24141 if (cu->base_address.has_value ())
24142 baton->base_address = *cu->base_address;
24143 else
24144 baton->base_address = 0;
24145 baton->from_dwo = cu->dwo_unit != NULL;
24146 }
24147
24148 static void
24149 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24150 struct dwarf2_cu *cu, int is_block)
24151 {
24152 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24153 struct objfile *objfile = per_objfile->objfile;
24154 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24155
24156 if (attr->form_is_section_offset ()
24157 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24158 the section. If so, fall through to the complaint in the
24159 other branch. */
24160 && attr->as_unsigned () < section->get_size (objfile))
24161 {
24162 struct dwarf2_loclist_baton *baton;
24163
24164 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24165
24166 fill_in_loclist_baton (cu, baton, attr);
24167
24168 if (!cu->base_address.has_value ())
24169 complaint (_("Location list used without "
24170 "specifying the CU base address."));
24171
24172 SYMBOL_ACLASS_INDEX (sym) = (is_block
24173 ? dwarf2_loclist_block_index
24174 : dwarf2_loclist_index);
24175 SYMBOL_LOCATION_BATON (sym) = baton;
24176 }
24177 else
24178 {
24179 struct dwarf2_locexpr_baton *baton;
24180
24181 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24182 baton->per_objfile = per_objfile;
24183 baton->per_cu = cu->per_cu;
24184 gdb_assert (baton->per_cu);
24185
24186 if (attr->form_is_block ())
24187 {
24188 /* Note that we're just copying the block's data pointer
24189 here, not the actual data. We're still pointing into the
24190 info_buffer for SYM's objfile; right now we never release
24191 that buffer, but when we do clean up properly this may
24192 need to change. */
24193 struct dwarf_block *block = attr->as_block ();
24194 baton->size = block->size;
24195 baton->data = block->data;
24196 }
24197 else
24198 {
24199 dwarf2_invalid_attrib_class_complaint ("location description",
24200 sym->natural_name ());
24201 baton->size = 0;
24202 }
24203
24204 SYMBOL_ACLASS_INDEX (sym) = (is_block
24205 ? dwarf2_locexpr_block_index
24206 : dwarf2_locexpr_index);
24207 SYMBOL_LOCATION_BATON (sym) = baton;
24208 }
24209 }
24210
24211 /* See read.h. */
24212
24213 const comp_unit_head *
24214 dwarf2_per_cu_data::get_header () const
24215 {
24216 if (!m_header_read_in)
24217 {
24218 const gdb_byte *info_ptr
24219 = this->section->buffer + to_underlying (this->sect_off);
24220
24221 memset (&m_header, 0, sizeof (m_header));
24222
24223 read_comp_unit_head (&m_header, info_ptr, this->section,
24224 rcuh_kind::COMPILE);
24225
24226 m_header_read_in = true;
24227 }
24228
24229 return &m_header;
24230 }
24231
24232 /* See read.h. */
24233
24234 int
24235 dwarf2_per_cu_data::addr_size () const
24236 {
24237 return this->get_header ()->addr_size;
24238 }
24239
24240 /* See read.h. */
24241
24242 int
24243 dwarf2_per_cu_data::offset_size () const
24244 {
24245 return this->get_header ()->offset_size;
24246 }
24247
24248 /* See read.h. */
24249
24250 int
24251 dwarf2_per_cu_data::ref_addr_size () const
24252 {
24253 const comp_unit_head *header = this->get_header ();
24254
24255 if (header->version == 2)
24256 return header->addr_size;
24257 else
24258 return header->offset_size;
24259 }
24260
24261 /* A helper function for dwarf2_find_containing_comp_unit that returns
24262 the index of the result, and that searches a vector. It will
24263 return a result even if the offset in question does not actually
24264 occur in any CU. This is separate so that it can be unit
24265 tested. */
24266
24267 static int
24268 dwarf2_find_containing_comp_unit
24269 (sect_offset sect_off,
24270 unsigned int offset_in_dwz,
24271 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24272 {
24273 int low, high;
24274
24275 low = 0;
24276 high = all_comp_units.size () - 1;
24277 while (high > low)
24278 {
24279 struct dwarf2_per_cu_data *mid_cu;
24280 int mid = low + (high - low) / 2;
24281
24282 mid_cu = all_comp_units[mid].get ();
24283 if (mid_cu->is_dwz > offset_in_dwz
24284 || (mid_cu->is_dwz == offset_in_dwz
24285 && mid_cu->sect_off + mid_cu->length > sect_off))
24286 high = mid;
24287 else
24288 low = mid + 1;
24289 }
24290 gdb_assert (low == high);
24291 return low;
24292 }
24293
24294 /* Locate the .debug_info compilation unit from CU's objfile which contains
24295 the DIE at OFFSET. Raises an error on failure. */
24296
24297 static struct dwarf2_per_cu_data *
24298 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24299 unsigned int offset_in_dwz,
24300 dwarf2_per_objfile *per_objfile)
24301 {
24302 int low = dwarf2_find_containing_comp_unit
24303 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24304 dwarf2_per_cu_data *this_cu
24305 = per_objfile->per_bfd->all_comp_units[low].get ();
24306
24307 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24308 {
24309 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24310 error (_("Dwarf Error: could not find partial DIE containing "
24311 "offset %s [in module %s]"),
24312 sect_offset_str (sect_off),
24313 bfd_get_filename (per_objfile->objfile->obfd));
24314
24315 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24316 <= sect_off);
24317 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
24318 }
24319 else
24320 {
24321 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24322 && sect_off >= this_cu->sect_off + this_cu->length)
24323 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24324 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24325 return this_cu;
24326 }
24327 }
24328
24329 #if GDB_SELF_TEST
24330
24331 namespace selftests {
24332 namespace find_containing_comp_unit {
24333
24334 static void
24335 run_test ()
24336 {
24337 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24338 dwarf2_per_cu_data *one_ptr = one.get ();
24339 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24340 dwarf2_per_cu_data *two_ptr = two.get ();
24341 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24342 dwarf2_per_cu_data *three_ptr = three.get ();
24343 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24344 dwarf2_per_cu_data *four_ptr = four.get ();
24345
24346 one->length = 5;
24347 two->sect_off = sect_offset (one->length);
24348 two->length = 7;
24349
24350 three->length = 5;
24351 three->is_dwz = 1;
24352 four->sect_off = sect_offset (three->length);
24353 four->length = 7;
24354 four->is_dwz = 1;
24355
24356 std::vector<dwarf2_per_cu_data_up> units;
24357 units.push_back (std::move (one));
24358 units.push_back (std::move (two));
24359 units.push_back (std::move (three));
24360 units.push_back (std::move (four));
24361
24362 int result;
24363
24364 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24365 SELF_CHECK (units[result].get () == one_ptr);
24366 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24367 SELF_CHECK (units[result].get () == one_ptr);
24368 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24369 SELF_CHECK (units[result].get () == two_ptr);
24370
24371 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24372 SELF_CHECK (units[result].get () == three_ptr);
24373 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24374 SELF_CHECK (units[result].get () == three_ptr);
24375 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24376 SELF_CHECK (units[result].get () == four_ptr);
24377 }
24378
24379 }
24380 }
24381
24382 #endif /* GDB_SELF_TEST */
24383
24384 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24385
24386 static void
24387 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24388 enum language pretend_language)
24389 {
24390 struct attribute *attr;
24391
24392 /* Set the language we're debugging. */
24393 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24394 if (attr != nullptr)
24395 set_cu_language (attr->constant_value (0), cu);
24396 else
24397 {
24398 cu->language = pretend_language;
24399 cu->language_defn = language_def (cu->language);
24400 }
24401
24402 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24403 }
24404
24405 /* See read.h. */
24406
24407 dwarf2_cu *
24408 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24409 {
24410 auto it = m_dwarf2_cus.find (per_cu);
24411 if (it == m_dwarf2_cus.end ())
24412 return nullptr;
24413
24414 return it->second;
24415 }
24416
24417 /* See read.h. */
24418
24419 void
24420 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24421 {
24422 gdb_assert (this->get_cu (per_cu) == nullptr);
24423
24424 m_dwarf2_cus[per_cu] = cu;
24425 }
24426
24427 /* See read.h. */
24428
24429 void
24430 dwarf2_per_objfile::age_comp_units ()
24431 {
24432 dwarf_read_debug_printf_v ("running");
24433
24434 /* This is not expected to be called in the middle of CU expansion. There is
24435 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24436 loaded in memory. Calling age_comp_units while the queue is in use could
24437 make us free the DIEs for a CU that is in the queue and therefore break
24438 that invariant. */
24439 gdb_assert (!this->per_bfd->queue.has_value ());
24440
24441 /* Start by clearing all marks. */
24442 for (auto pair : m_dwarf2_cus)
24443 pair.second->clear_mark ();
24444
24445 /* Traverse all CUs, mark them and their dependencies if used recently
24446 enough. */
24447 for (auto pair : m_dwarf2_cus)
24448 {
24449 dwarf2_cu *cu = pair.second;
24450
24451 cu->last_used++;
24452 if (cu->last_used <= dwarf_max_cache_age)
24453 cu->mark ();
24454 }
24455
24456 /* Delete all CUs still not marked. */
24457 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24458 {
24459 dwarf2_cu *cu = it->second;
24460
24461 if (!cu->is_marked ())
24462 {
24463 dwarf_read_debug_printf_v ("deleting old CU %s",
24464 sect_offset_str (cu->per_cu->sect_off));
24465 delete cu;
24466 it = m_dwarf2_cus.erase (it);
24467 }
24468 else
24469 it++;
24470 }
24471 }
24472
24473 /* See read.h. */
24474
24475 void
24476 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24477 {
24478 auto it = m_dwarf2_cus.find (per_cu);
24479 if (it == m_dwarf2_cus.end ())
24480 return;
24481
24482 delete it->second;
24483
24484 m_dwarf2_cus.erase (it);
24485 }
24486
24487 dwarf2_per_objfile::~dwarf2_per_objfile ()
24488 {
24489 remove_all_cus ();
24490 }
24491
24492 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24493 We store these in a hash table separate from the DIEs, and preserve them
24494 when the DIEs are flushed out of cache.
24495
24496 The CU "per_cu" pointer is needed because offset alone is not enough to
24497 uniquely identify the type. A file may have multiple .debug_types sections,
24498 or the type may come from a DWO file. Furthermore, while it's more logical
24499 to use per_cu->section+offset, with Fission the section with the data is in
24500 the DWO file but we don't know that section at the point we need it.
24501 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24502 because we can enter the lookup routine, get_die_type_at_offset, from
24503 outside this file, and thus won't necessarily have PER_CU->cu.
24504 Fortunately, PER_CU is stable for the life of the objfile. */
24505
24506 struct dwarf2_per_cu_offset_and_type
24507 {
24508 const struct dwarf2_per_cu_data *per_cu;
24509 sect_offset sect_off;
24510 struct type *type;
24511 };
24512
24513 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24514
24515 static hashval_t
24516 per_cu_offset_and_type_hash (const void *item)
24517 {
24518 const struct dwarf2_per_cu_offset_and_type *ofs
24519 = (const struct dwarf2_per_cu_offset_and_type *) item;
24520
24521 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24522 }
24523
24524 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24525
24526 static int
24527 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24528 {
24529 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24530 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24531 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24532 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24533
24534 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24535 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24536 }
24537
24538 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24539 table if necessary. For convenience, return TYPE.
24540
24541 The DIEs reading must have careful ordering to:
24542 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24543 reading current DIE.
24544 * Not trying to dereference contents of still incompletely read in types
24545 while reading in other DIEs.
24546 * Enable referencing still incompletely read in types just by a pointer to
24547 the type without accessing its fields.
24548
24549 Therefore caller should follow these rules:
24550 * Try to fetch any prerequisite types we may need to build this DIE type
24551 before building the type and calling set_die_type.
24552 * After building type call set_die_type for current DIE as soon as
24553 possible before fetching more types to complete the current type.
24554 * Make the type as complete as possible before fetching more types. */
24555
24556 static struct type *
24557 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24558 bool skip_data_location)
24559 {
24560 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24561 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24562 struct objfile *objfile = per_objfile->objfile;
24563 struct attribute *attr;
24564 struct dynamic_prop prop;
24565
24566 /* For Ada types, make sure that the gnat-specific data is always
24567 initialized (if not already set). There are a few types where
24568 we should not be doing so, because the type-specific area is
24569 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24570 where the type-specific area is used to store the floatformat).
24571 But this is not a problem, because the gnat-specific information
24572 is actually not needed for these types. */
24573 if (need_gnat_info (cu)
24574 && type->code () != TYPE_CODE_FUNC
24575 && type->code () != TYPE_CODE_FLT
24576 && type->code () != TYPE_CODE_METHODPTR
24577 && type->code () != TYPE_CODE_MEMBERPTR
24578 && type->code () != TYPE_CODE_METHOD
24579 && type->code () != TYPE_CODE_FIXED_POINT
24580 && !HAVE_GNAT_AUX_INFO (type))
24581 INIT_GNAT_SPECIFIC (type);
24582
24583 /* Read DW_AT_allocated and set in type. */
24584 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24585 if (attr != NULL)
24586 {
24587 struct type *prop_type = cu->addr_sized_int_type (false);
24588 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24589 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24590 }
24591
24592 /* Read DW_AT_associated and set in type. */
24593 attr = dwarf2_attr (die, DW_AT_associated, cu);
24594 if (attr != NULL)
24595 {
24596 struct type *prop_type = cu->addr_sized_int_type (false);
24597 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24598 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24599 }
24600
24601 /* Read DW_AT_data_location and set in type. */
24602 if (!skip_data_location)
24603 {
24604 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24605 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24606 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24607 }
24608
24609 if (per_objfile->die_type_hash == NULL)
24610 per_objfile->die_type_hash
24611 = htab_up (htab_create_alloc (127,
24612 per_cu_offset_and_type_hash,
24613 per_cu_offset_and_type_eq,
24614 NULL, xcalloc, xfree));
24615
24616 ofs.per_cu = cu->per_cu;
24617 ofs.sect_off = die->sect_off;
24618 ofs.type = type;
24619 slot = (struct dwarf2_per_cu_offset_and_type **)
24620 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24621 if (*slot)
24622 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24623 sect_offset_str (die->sect_off));
24624 *slot = XOBNEW (&objfile->objfile_obstack,
24625 struct dwarf2_per_cu_offset_and_type);
24626 **slot = ofs;
24627 return type;
24628 }
24629
24630 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24631 or return NULL if the die does not have a saved type. */
24632
24633 static struct type *
24634 get_die_type_at_offset (sect_offset sect_off,
24635 dwarf2_per_cu_data *per_cu,
24636 dwarf2_per_objfile *per_objfile)
24637 {
24638 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24639
24640 if (per_objfile->die_type_hash == NULL)
24641 return NULL;
24642
24643 ofs.per_cu = per_cu;
24644 ofs.sect_off = sect_off;
24645 slot = ((struct dwarf2_per_cu_offset_and_type *)
24646 htab_find (per_objfile->die_type_hash.get (), &ofs));
24647 if (slot)
24648 return slot->type;
24649 else
24650 return NULL;
24651 }
24652
24653 /* Look up the type for DIE in CU in die_type_hash,
24654 or return NULL if DIE does not have a saved type. */
24655
24656 static struct type *
24657 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24658 {
24659 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24660 }
24661
24662 /* Trivial hash function for partial_die_info: the hash value of a DIE
24663 is its offset in .debug_info for this objfile. */
24664
24665 static hashval_t
24666 partial_die_hash (const void *item)
24667 {
24668 const struct partial_die_info *part_die
24669 = (const struct partial_die_info *) item;
24670
24671 return to_underlying (part_die->sect_off);
24672 }
24673
24674 /* Trivial comparison function for partial_die_info structures: two DIEs
24675 are equal if they have the same offset. */
24676
24677 static int
24678 partial_die_eq (const void *item_lhs, const void *item_rhs)
24679 {
24680 const struct partial_die_info *part_die_lhs
24681 = (const struct partial_die_info *) item_lhs;
24682 const struct partial_die_info *part_die_rhs
24683 = (const struct partial_die_info *) item_rhs;
24684
24685 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24686 }
24687
24688 struct cmd_list_element *set_dwarf_cmdlist;
24689 struct cmd_list_element *show_dwarf_cmdlist;
24690
24691 static void
24692 show_check_physname (struct ui_file *file, int from_tty,
24693 struct cmd_list_element *c, const char *value)
24694 {
24695 fprintf_filtered (file,
24696 _("Whether to check \"physname\" is %s.\n"),
24697 value);
24698 }
24699
24700 void _initialize_dwarf2_read ();
24701 void
24702 _initialize_dwarf2_read ()
24703 {
24704 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24705 Set DWARF specific variables.\n\
24706 Configure DWARF variables such as the cache size."),
24707 &set_dwarf_cmdlist,
24708 0/*allow-unknown*/, &maintenance_set_cmdlist);
24709
24710 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24711 Show DWARF specific variables.\n\
24712 Show DWARF variables such as the cache size."),
24713 &show_dwarf_cmdlist,
24714 0/*allow-unknown*/, &maintenance_show_cmdlist);
24715
24716 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24717 &dwarf_max_cache_age, _("\
24718 Set the upper bound on the age of cached DWARF compilation units."), _("\
24719 Show the upper bound on the age of cached DWARF compilation units."), _("\
24720 A higher limit means that cached compilation units will be stored\n\
24721 in memory longer, and more total memory will be used. Zero disables\n\
24722 caching, which can slow down startup."),
24723 NULL,
24724 show_dwarf_max_cache_age,
24725 &set_dwarf_cmdlist,
24726 &show_dwarf_cmdlist);
24727
24728 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24729 Set debugging of the DWARF reader."), _("\
24730 Show debugging of the DWARF reader."), _("\
24731 When enabled (non-zero), debugging messages are printed during DWARF\n\
24732 reading and symtab expansion. A value of 1 (one) provides basic\n\
24733 information. A value greater than 1 provides more verbose information."),
24734 NULL,
24735 NULL,
24736 &setdebuglist, &showdebuglist);
24737
24738 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24739 Set debugging of the DWARF DIE reader."), _("\
24740 Show debugging of the DWARF DIE reader."), _("\
24741 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24742 The value is the maximum depth to print."),
24743 NULL,
24744 NULL,
24745 &setdebuglist, &showdebuglist);
24746
24747 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24748 Set debugging of the dwarf line reader."), _("\
24749 Show debugging of the dwarf line reader."), _("\
24750 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24751 A value of 1 (one) provides basic information.\n\
24752 A value greater than 1 provides more verbose information."),
24753 NULL,
24754 NULL,
24755 &setdebuglist, &showdebuglist);
24756
24757 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24758 Set cross-checking of \"physname\" code against demangler."), _("\
24759 Show cross-checking of \"physname\" code against demangler."), _("\
24760 When enabled, GDB's internal \"physname\" code is checked against\n\
24761 the demangler."),
24762 NULL, show_check_physname,
24763 &setdebuglist, &showdebuglist);
24764
24765 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24766 no_class, &use_deprecated_index_sections, _("\
24767 Set whether to use deprecated gdb_index sections."), _("\
24768 Show whether to use deprecated gdb_index sections."), _("\
24769 When enabled, deprecated .gdb_index sections are used anyway.\n\
24770 Normally they are ignored either because of a missing feature or\n\
24771 performance issue.\n\
24772 Warning: This option must be enabled before gdb reads the file."),
24773 NULL,
24774 NULL,
24775 &setlist, &showlist);
24776
24777 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24778 &dwarf2_locexpr_funcs);
24779 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24780 &dwarf2_loclist_funcs);
24781
24782 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24783 &dwarf2_block_frame_base_locexpr_funcs);
24784 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24785 &dwarf2_block_frame_base_loclist_funcs);
24786
24787 #if GDB_SELF_TEST
24788 selftests::register_test ("dw2_expand_symtabs_matching",
24789 selftests::dw2_expand_symtabs_matching::run_test);
24790 selftests::register_test ("dwarf2_find_containing_comp_unit",
24791 selftests::find_containing_comp_unit::run_test);
24792 #endif
24793 }